#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>       
#include "../global.h"
#include "../custom.h"


#include "shared_util.h"
#include "shared_file_util.h"
#include "shared_authenticate.h"
#include "shared_strtrm.h"
#include "shared_cs_util.h"
#include "shared_access.h"



void
log_this_access (const char *username, const char *base_dir, 
                           const char *sub_dir)
{
  FILE *fp;
  char logpath[MAX_PATH + 1];
  time_t now;

  now = time (NULL);
  

  snprintf (logpath, MAX_PATH + 1, "%s/%s/%s/%s", 
  			base_dir, sub_dir, username, ACCESS_LOG_FNAME);

  fp = fopen(logpath,"a");
  if(!fp)
      cs_critical_error(ERR_FOPEN_APPEND_FAILED,logpath); 
  fwrite (&now, sizeof (time_t), 1, fp);
  fclose (fp);
}


void
delete_stale_key (const char *username, const char *key_dir)
{
  SESSION session;
  char keyfile[MAX_PATH + 1];
  DIR *dir_p;
  struct dirent *dir_entry_p;
  FILE *fp;
  int read_ok;

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

  while (NULL != (dir_entry_p = readdir (dir_p)))
    {
      snprintf (keyfile, MAX_PATH + 1, "%s/%s", key_dir, dir_entry_p->d_name);
      fp = fopen (keyfile, "r");
      if (fp)                   /* if file can't be opened, that's ok */
        {
          read_ok = (fread (&session, sizeof (SESSION), 1, fp) == 1);
          fclose(fp);
          /* delete keys with this username ONLY if the key is not being used to capture a course */
          if (read_ok && !strcmp (session.username, username) && !user_capturing(&session))      /*shared_access.c */
                  unlink (keyfile);
        }
    }

  closedir (dir_p);
}





void
get_key_template(char *template)
{
  int i;

  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");
}

 

void
create_new_key (SESSION *user, const char *base_dir, 
				char *key, int key_expire)
{
  int fd;
  char current_dir[MAX_PATH + 1];
  char key_dir[MAX_PATH + 1];
  
  snprintf (key_dir, MAX_PATH + 1, "%s/keys", base_dir);

  /* delete stale key only if this user is not in 'capture' mode
  ** otherwise the 'real' owner of this username will get logged out
  */
  if(!user_capturing(user))    /* shared_access.c */
      delete_stale_key (user->username, key_dir);
  
  get_key_template (key);

  if(!getcwd(current_dir,MAX_PATH))
     cs_critical_error(ERR_GETCWD_FAILED,"in create_new_key()");

  if(chdir(key_dir))
    cs_critical_error(ERR_CHDIR_FAILED, key_dir);
  
  fd = mkstemp(key);
  if(fd == -1)
     cs_critical_error(ERR_MKSTEMP_FAILED,key);
  change_mkstemp_permission(key);
  
  user->expires = time (NULL) + key_expire;
  
  /* the session also stores the IP address of the user */
  get_ip_address(user->ip_address);   /* shared_util.c */
    
  
 if(write(fd,user,sizeof(SESSION)) != sizeof(SESSION))
   cs_critical_error (ERR_FWRITE_FAILED, "");
 close(fd);
 
 if(chdir(current_dir))
    cs_critical_error(ERR_CHDIR_FAILED,"");

         
}



