#include "shared_super_key.h"
#define L126N_ADMIN_REALNAME "Admin"

void
delete_super_key(const char *key_dir, const char *username)
{
  char keyfile[MAX_PATH + 1];
  DIR *dir_p;
  struct dirent *dir_entry_p;
  SESSION user;
  FILE *fp;
  int flag = 0;


  dir_p = opendir (key_dir);
  if (!dir_p)
     cs_critical_error(ERR_OPENDIR_FAILED, "");

  while (NULL != (dir_entry_p = readdir (dir_p)))
  {
      if(*(dir_entry_p->d_name) != '.')
      {
        snprintf (keyfile, MAX_PATH + 1, "%s/%s", key_dir, dir_entry_p->d_name);
        fp = fopen(keyfile, "r");
        if(fp)
        {
         if(fread(&user, sizeof(SESSION), 1, fp)==1)
           if(strcmp(user.username, username)==0)
           {
               fclose(fp);
               unlink(keyfile);
               flag = 1;
            }
           if(flag == 0)
               fclose(fp);
        }
      }
     flag = 0;
  }
  closedir (dir_p);
}



static void
get_keyname (char *key, char *keydir)
{
     char template[15], *ptr;
     int i, fd;
      char keypath[MAX_PATH + 1];

      srand ((int) time (NULL));

      for(i = 0; i<8; i++)
         template[i] = 97 + (int) (26.0 * rand () / (RAND_MAX + 1.0));
      template[i] = '\0';
      strcat (template, "XXXXXX");
      snprintf (keypath, MAX_PATH + 1, "%s/%s", keydir, template);
      fd = mkstemp(keypath);
      if(fd == -1)
          cs_critical_error(ERR_MKSTEMP_FAILED, "");
       close(fd);
      change_mkstemp_permission(keypath);  /* shared_file_util.c */
      ptr = strrchr(keypath, '/');
      *ptr = '\0';
      ptr++;
     strncpy(key, ptr, MAX_KEY);

}

void
create_super_key (const char *username, char *key)
{
  char keydir[MAX_PATH + 1];
   time_t expires;
   FILE *fp;
   SESSION user;
   char this_ip[MAX_IP +1];

    get_ip_address(this_ip);
    expires = time(NULL) + MAX_KEY_EXPIRE;  /* 10,800 secs in 3 hrs */
    snprintf (keydir, MAX_PATH + 1, "../%s/%s/%s", USERS_DIR, ADMIN_DIR, KEY_DIR);
    delete_super_key (keydir, username);

     get_keyname (key, keydir);
     snprintf (keydir, MAX_PATH + 1, "../%s/%s/%s/%s", USERS_DIR, ADMIN_DIR,KEY_DIR,  key);
     strncpy(user.username, username, MAX_USERNAME +1);
     strncpy(user.realname, L126N_ADMIN_REALNAME, MAX_NAME +1);
     strncpy(user.id, "00", MAX_ID +1);
     strncpy(user.alias, "00", MAX_NAME +1);
     user.group = ADMIN;
     user.expires = expires;
     strncpy(user.ip_address, this_ip, MAX_IP+1); 
     fp = fopen (keydir, "w");
     if (!fp)
        cs_critical_error (ERR_FOPEN_WRITE_FAILED, "");
     if (fwrite (&user, sizeof (SESSION), 1, fp) != 1)
        cs_critical_error (ERR_FWRITE_FAILED, "");
     fclose (fp);
}







void
validate_super_key(const char *key, SESSION *user)
{
  #define MAX_MSG 40
    FILE *fp;
    time_t now;
    char key_path_fname[MAX_PATH +1];
    char key_filename[MAX_PATH +1];
    char this_ip[MAX_IP+1];
    char msg[MAX_MSG+1];

    secure_server_check();   /* shared_util.c */
    check_key_for_cookie(key, key_filename);
    
    snprintf(key_path_fname, MAX_PATH +1, "../%s/%s/%s/%s", 
		    USERS_DIR, ADMIN_DIR, KEY_DIR,key_filename);

    fp =fopen(key_path_fname, "r+");
    if(!fp)
      cs_critical_error(ERR_SORRY_YOUVE_BEEN_LOGGED_OUT, "");

    if(fread(user, sizeof(SESSION), 1, fp) != 1)
     {
      cs_critical_error (ERR_ERROR_READING_KEY_FILE, key_path_fname);
    }

    if(SUPER_IP_CHECK_ENABLED) /* see custom.h */
    {
      get_ip_address(this_ip);
      if(strcmp(user->ip_address, this_ip))
          cs_critical_error(ERR_REQUEST_DENIED, "");
    }      

    now = time(NULL);
    if(user->expires < now)
    {
      snprintf (msg, MAX_MSG, "%d", (MAX_KEY_EXPIRE) / 60);
      cs_critical_error (ERR_YOUR_SESSION_HAS_TIMED_OUT, msg);

    }

    user->expires = now + MAX_KEY_EXPIRE;
    
    if(fseek(fp, 0, SEEK_SET))
         cs_critical_error(ERR_FSEEK_FAILED, "");
    if (fwrite (user, sizeof (SESSION), 1, fp) != 1)
	cs_critical_error (ERR_ERROR_WRITING_KEY, "");

    fclose (fp);
  #undef MAX_MSG
}







char *new_str(const char *str)
{
   char *tempstr;
   
   tempstr = (char *) malloc(sizeof(char) * (strlen(str) + 1));

   if (tempstr != NULL)
      strcpy(tempstr,str);
      
    return tempstr;
}





