#include <stdio.h>		/* standard io functions */
#include <string.h>
#include <ctype.h>
#include <unistd.h>		/* for unlink() */
#include <sys/types.h>
#include <dirent.h>		/* Directory information.       */
#include <sys/stat.h>		/* for stat() */

#include "global.h"

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_authenticate.h"
#include "manhat-lib/shared_central_user.h"
#include "manhat-lib/shared_add_util.h"
#include "manhat-lib/shared_encrypt.h"
#include "manhat-lib/shared_access.h"
#include "manhat-lib/shared_central_log.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_system_data.h"

void
read_parameters ( USER_DATA *person, char *course, char *key)
{

  cs_get_required_parameter ("crs", course, MAX_PATH);
  cs_get_required_parameter ("id", key, MAX_KEY);
  cs_get_required_parameter ("username", person->username, MAX_USERNAME);
  cs_get_required_parameter ("realname", person->realname, MAX_NAME);
  cs_get_required_parameter ("ssn", person->id, MAX_ID);
}







static void
report_success ( USER_DATA * new_info, const char *course, 
				const char *key,  char *password , CONFIG_STRUCT *conf)
{
  char url[MAX_PATH +1];

  snprintf (url, MAX_PATH +1, "%s?crs=%s&amp;id=%s", "admin_reset_pass_form", course, key);
  cs_set_value("back_url", url);
  cs_set_current_time();
  cs_set_value("username", new_info->username);
  cs_set_value("realname", new_info->realname);
  cs_set_value("user_id", new_info->id);
  cs_set_value("password", password);
  cs_set_int_value("success", 1);
	     

}



int
main ()
{

  USER_DATA new_info;		/* data come from form */
  char course[MAX_PATH + 1];	/* from crs=? command line */
  char key[MAX_KEY + 1];	/* from id=? command line */
  SESSION user;
  CONFIG_STRUCT conf;		/* the configuration read from config file */
  char password[MAX_PASSWORD + 1];
  char firstname[MAX_NAME + 1];
  char lastname[MAX_NAME + 1];
  
  cs_cgi_init();

  if(!system_data_int("ALLOW_PASSWORD_RESET")) /* shared_system_data.h */
    cs_critical_error(ERR_REQUEST_DENIED, ""); 

  read_parameters (&new_info, course, key);
  read_configuration_file (course, &conf);	/* shared_util.c */
  validate_key (key, &user, &conf);	/* shared_util.c */



  if(!has_write_permission(&user, &conf))   /* shared_access.c */
     access_denied_error();                 /* shared_access.c */

  if ((user.group != FACULTY) && (user.group != ADMIN))
    cs_critical_error (ERR_REQUEST_DENIED, "");
  
  cs_set_course_info(&conf);

  get_first_last_names(new_info.realname,firstname, lastname);    /* shared_authenticate.c */

  derive_password (new_info.username, firstname, lastname,        /* shared_authenticate.c */
                        new_info.id, password, MAX_PASSWORD);
                        
  if(conf.standalone)
    write_password(new_info.username, password, conf.course_path);   /* shared_encrypt.c */
  else
     write_central_password_file (new_info.username, password);     /* shared_encrypt.c */

  if(!cs_set_event_msg())
       cs_critical_error(ERR_CANT_GET_EVENT_STRING, "reset password event");

  write_central_log(new_info.username, hdf_get_value(global_cgi->hdf, "log_event.reset_password", ""), user.group==ADMIN? "Admin":user.realname, 0);   /* shared_central_log.c */

  report_success (&new_info, course, key, password, &conf);


  /** Note that the cs file is the same as for admin_reset_pass_form !!! */
  cs_cgi_display("admin_reset_pass_form", 1);
  cs_cgi_destroy();
  return 0;
}

