#include <stdio.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include "global.h"

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_super_util.h"
#include "manhat-lib/shared_authenticate.h"
#include "manhat-lib/shared_strtrm.h"
#include "manhat-lib/shared_encrypt.h"
#include "manhat-lib/shared_central_log.h"
#include "manhat-lib/shared_cs_util.h"

static void
read_parameters ( char *key,
                 char *current, char *new_passwd, char *verify)
{



   cs_get_required_parameter ("id", key, MAX_KEY );
   cs_get_required_parameter ("current", current, MAX_PASSWORD );
   cs_get_required_parameter ("new", new_passwd, MAX_PASSWORD );
   cs_get_required_parameter ("verify", verify, MAX_PASSWORD );





}


void
check_validity (char *current, char *new_passwd, char *verify,
                char *username)
{
  int len;
  char *ptr;



  if (strcmp (new_passwd, verify))
    cs_critical_error (ERR_PASSWORDS_DONT_MATCH, "");


  if (!strcmp (username, new_passwd))
    cs_critical_error (ERR_PASSWD_AND_USERNAME_SAME, "");

  len = strlen (new_passwd);
  if ((len < 6) || (len > 15))
    cs_critical_error (ERR_PASSWORD_WRONG_LEN, "");

  for (ptr = new_passwd; *ptr; ptr++)
    if (!isdigit (*ptr) && !isalpha (*ptr))
      cs_critical_error (ERR_INVALID_PASSWORD, "");

}



static void
verify_current_passwd(const char *typed_plaintext_passwd, const char *username)
{
    char passwd_file[MAX_PATH + 1];
    char current_encrypted_passwd[MAX_ENCRYPTED_PASSWORD + 1];
    FILE *fp;

    snprintf(passwd_file, MAX_PATH, "../%s/%s/%c/%s/%s",
              USERS_DIR, ADMIN_DIR, sub_dir_name(username), username,  PASSWORD_FNAME);
    fp = fopen(passwd_file, "r");
    if(!fp)
       cs_critical_error (ERR_FOPEN_READ_FAILED, passwd_file);
    if(!fgets(current_encrypted_passwd, MAX_ENCRYPTED_PASSWORD + 1, fp))
       cs_critical_error (ERR_FREAD_FAILED, passwd_file);
    fclose(fp);
    strtrm(current_encrypted_passwd);
    if(strcmp(current_encrypted_passwd, encrypted_password(typed_plaintext_passwd, username)) != 0)  /* shared_encrypted.c */
      cs_critical_error (ERR_INCORRECT_PASSWD_ENTERED, "");

}




void
change_server_password(const char *new_plaintext_passwd, const char *username)
{
    char passwd_file[MAX_PATH + 1];
    FILE *fp;

    snprintf(passwd_file, MAX_PATH, "../%s/%s/%c/%s/%s",
             USERS_DIR, ADMIN_DIR, sub_dir_name(username), username, PASSWORD_FNAME);
             
    fp = fopen(passwd_file, "w");
    if(!fp)
       cs_critical_error (ERR_FOPEN_WRITE_FAILED, "");
    fputs(encrypted_password(new_plaintext_passwd, username), fp);   /* shared_encrypted.c */
    fclose(fp);

}




int
main (void)
{

  char key[MAX_KEY + 1];	/* from id=? command line */
  char current[MAX_PASSWORD + 1], new_passwd[MAX_PASSWORD + 1],
  verify[MAX_PASSWORD + 1];
  SESSION user;

  cs_cgi_init();
  read_parameters( key, current, new_passwd, verify);
  validate_super_key(key, &user);
  verify_current_passwd(current, user.username);
  
  check_validity (current, new_passwd, verify, user.username);
  change_server_password(new_passwd, user.username);
  cs_set_event_msg();
  write_central_log(user.username, hdf_get_value(global_cgi->hdf, "log_event.change_password",""),  "", 1); /* shared_central_log.c */
  printf("Location:%s?id=%s\n\n", "super_admin_page",  key);
  cs_cgi_destroy();

  return 0;

}


