Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.22 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) {
  61.       return(LOCKEDUSER);
  62.     } else {
  63.       pw_entry->pw_failed = 0;
  64.       pw_entry->pw_age++;
  65.       pwdb_update_user(pw_entry);
  66.  
  67.       if (pw_entry->pw_age > 10) {
  68.         printf("Password is too old, change it asap");
  69.       }
  70.       return(NOUSER);
  71.  
  72.     }
  73.    
  74.   }
  75.   return(0);
  76. }
  77.  
  78.  
  79. int main(int argc,char **argv) {
  80.   char username[USERNAME_SIZE];
  81.  
  82.   /* write "login:" and read user input */
  83.   read_username(username);
  84.  
  85.   int res = NOUSER;
  86.   char *password;
  87.  
  88.   while(res == NOUSER){
  89.     read_username(username);
  90.     password = getpass("password: ");
  91.  
  92.     if (argc > 1 && strcmp(argc[1],"-l")==0){
  93.         res = confirm2(username, password);
  94.     }else{
  95.         res = confirm(username, password);
  96.     }
  97.  
  98.    
  99.     if(res == NOUSER){
  100.         printf("\n Unknown user or incorrectpassword.\n");
  101.     }
  102.   }
  103.  
  104.   printf("\nUser authenticated successfully.\n");
  105.   free(password);
  106.   return(0);
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement