Ejemplo n.º 1
0
main()
{
  FILE *password_file_pointer, *account_file_pointer;

  char indata[10000];                 /* data received from standard input */
  char tempdata[10000];  
  char user_name[100];                /* character array for requested user name */
  char user_login_name[100];          /* character array for requested login name */
  char temp_password[100];           
  char encoded_user_password[100]; 
  char user_password[100];            /* character array for requested user password */
  char verify_user_password[100];     /* character array to verify user password */
  char user_email[100];               /* character array for user's email address */
  char user_address[100];             /* character array for user's residence */
  char user_city[100];                /* character array for user's city of residence */
  char user_state[100];               /* character array for user's state of residence */
  char user_zip_code[100];            /* character array for user's zip code */
  char user_area_code[100];           /* character array for user's area code */
  char user_phone_number[100];        /* character array for user's phone number */

  int isValidForm = 0;                /* valid form flag variable */
  int isValidAccount = 0;             /* valid account flag variable */

 /* retrieve data from standard input (a.k.a. POST method of HTML form) */
  gets(indata);  

  /* separate data into respective character arrays */
  strcpy(user_name, get_variable(indata, "user_name="));
  strcpy(user_login_name, get_variable(indata, "&user_login_name="));
  strcpy(user_password, get_variable(indata, "&user_password="******"&verify_user_password="******"&user_email="));
  strcpy(user_address, get_variable(indata, "&user_address="));
  strcpy(user_city, get_variable(indata, "&user_city="));
  strcpy(user_state, get_variable(indata, "&user_state="));
  strcpy(user_zip_code, get_variable(indata, "&user_zip_code="));
  strcpy(user_area_code, get_variable(indata, "&user_area_code="));
  strcpy(user_phone_number, get_variable(indata, "&user_phone_number="));

  /* valid form (10=valid; 11=inconsistent passwords; 12=file error) */
  isValidForm = verify_form(user_name, user_login_name,
			    user_password, verify_user_password,
			    user_email, user_address,
			    user_city, user_state, user_zip_code,
			    user_area_code, user_phone_number);

  /* valid account (10=non-valid account; 11=valid account) */   
  isValidAccount = verify_account(password_file_pointer, user_login_name, user_password);
  
  /* if valid form and valid account, write data into password and account files */
  if (isValidForm == 10) 
  { /* open account file */
    if ((account_file_pointer = fopen(ACCOUNT_FILE, "ab+")) == NULL)
      ErrorInCreateAccount(0); /* error in opening account file */
    else 
    {
      if ((isValidForm == 10) && (isValidAccount == 11))
      { /* write user data into account file */
        fprintf(account_file_pointer, "%s\n", user_name);
        fprintf(account_file_pointer, "%s %s\n", user_login_name, user_password);
        fprintf(account_file_pointer, "%s\n", user_email);
        fprintf(account_file_pointer, "%s\n", user_address);
        fprintf(account_file_pointer, "%s %s %s\n", user_city, user_state,
user_zip_code);
        fprintf(account_file_pointer, "%s %s\n", user_area_code, user_phone_number);
      }
      else
        ErrorInCreateAccount(1);        /* inconsistent passwords */
    }
    fclose(account_file_pointer);		/* close account file */
    /* open password file */ 
    if ((password_file_pointer = fopen(PASSWORD_FILE, "ab+")) == NULL) 
      ErrorInCreateAccount(0);			/* error in opening password file */
    else /* write user login name and password into password file */
      fprintf(password_file_pointer, "%s %s\n", user_login_name, user_password); 
    fclose(password_file_pointer);      /* close password file */
    /* let user know that account was created successfully */
    AccountCreated(user_name, user_login_name, user_email, user_address, 
                    user_city, user_state, user_zip_code, user_area_code, user_phone_number);  
  } 
  else if (isValidForm == 11)           /* error--inconsistent passwords */
    ErrorInCreateAccount(1);
  else if (isValidForm == 12)
    ErrorInCreateAccount(2);			/* error--file error */
  return 0;
}
Ejemplo n.º 2
0
/**
 * Do everything before the fork and do everything the exec fork
 * 
 * @param   argc  The number of command line arguments
 * @param   argv  The command line arguments
 */
void do_login(int argc, char** argv)
{
  char* username = NULL;
  char* hostname = NULL;
  char preserve_env = 0;
  int ret;
  #ifdef USE_TTY_GROUP
  struct group* group;
  #endif
  
  
  #if AUTH > 0
  /* Disable echoing */
  passphrase_disable_echo1(STDIN_FILENO /* Will be the terminal. */);
  /* This should be done as early and quickly as possible so as little
     as possible of the passphrase gets leaked to the output if the user
     begins entering the passphrase directly after the username. */
  #endif
  
  
  /* Set process group ID */
  setpgrp();
  
  
  /* Parse command line arguments */
  {
    char double_dashed = 0;
    char hostname_on_next = 0;
    int i;
    for (i = 1; i < argc; i++)
      {
	char *arg = *(argv + i);
	char c;
	
	if (*arg == 0)
	  ;
	else if ((*arg == '-') && (double_dashed == 0))
	  while ((c = *(++arg)))
	    if (c == 'H')
	      ;
	    else if (c == 'V')
	      _exit(2);
	    else if (c == 'p')
	      preserve_env = 1;
	    else if (c == 'h')
	      {
		if (*(arg + 1))
		  hostname = arg + 1;
		else
		  hostname_on_next = 1;
		break;
	      }
	    else if (c == 'f')
	      {
		if (*(arg + 1))
		  username = arg + 1;
		skip_auth = 1;
		break;
	      }
	    else if (c == '-')
	      {
		double_dashed = 1;
		break;
	      }
	    else
	      printf("%s: unrecognised options: -%c\n", *argv, c);
	else if (hostname_on_next)
	  {
	    hostname = arg;
	    hostname_on_next = 0;
	  }
	else
	  username = arg;
      }
  }
  
  
  /* Change that a username has been specified */
  if (username == NULL)
    {
      printf("%s: no username specified\n", *argv);
      sleep(ERROR_SLEEP);
      _exit(2);
    }
  
  
  /* Verify that the user may login */
  ret = fork_exec_wait_hook(HOOK_VERIFY, argc, argv);
  if ((ret >= 0) && WIFEXITED(ret) && (WEXITSTATUS(ret) == 1))
    {
      sleep(ERROR_SLEEP);
      _exit(2);
    }
  
  
  if (skip_auth)
    {
      /* Reset terminal settings */
      passphrase_reenable_echo1(STDIN_FILENO);
      
      /* Only root may bypass authentication */
      if (getuid())
	{
	  printf("%s: only root by use the -f option\n", *argv);
	  sleep(ERROR_SLEEP);
	  _exit(2);
	}
    }
  /* Print ant we want a passphrase, if -f has not been used */
  else
    {
      printf("Passphrase: ");
      fflush(stdout);
    }
  /* Done early to make to program look like it is even faster than it is */
  
  
  /* Make sure nopony is spying */
  #ifdef USE_TTY_GROUP
  if ((group = getgrnam(TTY_GROUP)))
    tty_group = group->gr_gid;
  endgrent();
  #endif
  secure_tty(tty_group);
  
  #if AUTH > 0
  if (skip_auth == 0)
    {
      /* Redisable echoing */
      passphrase_disable_echo1(STDIN_FILENO);
    }
  #endif
  
  
  /* Set up clean quiting and time out */
  signal(SIGALRM, timeout_quit);
  signal(SIGQUIT, user_quit);
  signal(SIGINT, user_quit);
  siginterrupt(SIGALRM, 1);
  siginterrupt(SIGQUIT, 1);
  siginterrupt(SIGINT, 1);
  #if AUTH > 0
  alarm(TIMEOUT_SECONDS);
  #endif
  
  
  /* Get user information */
  if ((entry = getpwnam(username)) == NULL)
    {
      if (errno)
	perror("getpwnam");
      else
	printf("User does not exist\n");
      sleep(ERROR_SLEEP);
      _exit(1);
    }
  endpwent();
  username = entry->pw_name;
  
  
  /* Verify passphrase or other token, if -f has not been used */
  ret = 2;
  #if AUTH == 0
  (void) hostname;
  #else
  initialise_login(hostname, username, read_passphrase);
  if (skip_auth == 0)
    ret = authenticate_login();
  /* Passphrase entered, turn off timeout */
  alarm(0);
  #endif
  if (ret == 2)
    printf("(auto-authenticated)\n");
  if (ret == 0)
    {
      preexit();
      fork_exec_wait_hook(HOOK_DENIED, argc, argv);
      xsleep(FAILURE_SLEEP);
      _exit(1);
    }
  
  preexit();
  
  
  /* Verify account, such as that it is enabled */
  verify_account();
  
  
  /* Run login hook */
  fork_exec_wait_hook(HOOK_LOGIN, argc, argv);
  
  
  /* Partial login */
  chown_tty(entry->pw_uid, tty_group, 0);
  chdir_home(entry);
  ensure_shell(entry);
  set_environ(entry, preserve_env);
  open_login_session();
  
  
  /* Stop signal handling */
  signal(SIGALRM, SIG_DFL);
  signal(SIGQUIT, SIG_DFL);
  signal(SIGTSTP, SIG_IGN);
  
  
  child_pid = fork();
  /* vfork cannot be used as the child changes the user,
     the parent would not be able to chown the TTY */
  
  if (child_pid == -1)
    {
      perror("fork");
      close_login_session();
      sleep(ERROR_SLEEP);
      _exit(1);
    }
  else if (child_pid)
    return; /* Do not go beyond this in the parent */
  
  /* In case the shell does not do this */
  setsid();
  
  /* Set controlling terminal */
  if (ioctl(STDIN_FILENO, TIOCSCTTY, 1))
    perror("TIOCSCTTY");
  signal(SIGINT, SIG_DFL);
  
  /* Partial login */
  ret = entry->pw_uid
    ? initgroups(username, entry->pw_gid) /* supplemental groups for user, can require network     */
    : setgroups(0, NULL);                 /* supplemental groups for root, does not require netork */
  if (ret == -1)
    {
      perror(entry->pw_uid ? "initgroups" : "setgroups");
      sleep(ERROR_SLEEP);
      _exit(1);
    }
  set_user(entry);
  exec_shell(entry);
}