Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.27 KB | None | 0 0
  1. /*
  2.  * Program userinfo.c
  3.  *
  4.  * This program prompts the user for a login name, and tries to
  5.  * extract user information from the /etc/passwd file.
  6.  *
  7.  */
  8.  
  9. #define _GNU_SOURCE
  10. #define _XOPEN_SOURCE
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <unistd.h>
  14. #include <pwd.h>
  15. #include <sys/types.h>
  16. #include <string.h>
  17. #include "pwdblib.c"
  18.  
  19. /* define some error constants */
  20. #define NOUSER -1
  21. #define LOCKEDUSER = -2
  22.  
  23. /* define max size of a username */
  24. #define USERNAME_SIZE 32
  25.  
  26.  
  27.  
  28.  
  29. int read_username(char *username){
  30.  
  31.   printf("login: ");
  32.   fgets(username,USERNAME_SIZE,stdin);
  33.  
  34.  /* remove the CR included by getline() */
  35.   username[strlen(username)-1]='\0';  
  36.   return(0);
  37. }
  38.  
  39.  
  40.  
  41.  
  42. int confirm(char *username, char *password){
  43.   struct passwd *pw_entry;
  44.   pw_entry=getpwnam(username);
  45.   if (pw_entry==NULL) {return(NOUSER)};
  46.   password = crypt(password, strndup(pw_entry->pw_passwd, 2));
  47.   if (strcmp(password, pw_entry->pw_passwd) != 0) {return(NOUSER)};
  48.   return(0);
  49. }
  50.  
  51. int confirm2(char *username, char *password){
  52.   struct pwdb_passwd *pw_entry;
  53.   pw_entry=getpwnam(username);
  54.   if (pw_entry==NULL) {return(NOUSER)};
  55.   password = crypt(password, strndup(pw_entry->pw_passwd, 2));
  56.  
  57.   if (strcmp(password, pw_entry->pw_passwd) != 0) {
  58.     pw_entry->pw_failed++;
  59.     pwdb_update_user(pw_entry);
  60.     if (pw_entry->pw_failed > 5) {return(LOCKEDUSER)};
  61.  
  62.        return(NOUSER);
  63.     } else {
  64.       pw_entry->pw_failed = 0;
  65.       pw_entry->pw_age++;
  66.       pwdb_update_user(pw_entry);
  67.  
  68.       if (pw_entry->pw_age > 10) {
  69.         printf("Password is too old, change it asap");
  70.       }
  71.  
  72.     }
  73.    
  74.   return(0);
  75. }
  76.  
  77.  
  78. int main(int argc,char **argv) {
  79.   char username[USERNAME_SIZE];
  80.  
  81.   /* write "login:" and read user input */
  82.   read_username(username);
  83.  
  84.   int res = NOUSER;
  85.   char *password;
  86.  
  87.   while(res == NOUSER){
  88.     read_username(username);
  89.     password = getpass("password: ");
  90.  
  91.     if (argc > 1 && strcmp(argv[1],"-l")==0){
  92.         res = confirm2(username, password);
  93.     }else{
  94.         res = confirm(username, password);
  95.     }
  96.  
  97.     if (res == LOCKEDUSER) {
  98.      printf("\nUser locked.\n");
  99. }
  100.  
  101.     if(res == NOUSER){
  102.         printf("\n Unknown user or incorrectpassword.\n");
  103.     }
  104.   }
  105.  
  106.   printf("\nUser authenticated successfully.\n");
  107.   free(password);
  108.   return(0);
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement