Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
68
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 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(0);
  71.     }
  72.     return(NOUSER);
  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(argc[1],"-l")==0){
  92.         res = confirm2(username, password);
  93.     }else{
  94.         res = confirm(username, password);
  95.     }
  96.  
  97.    
  98.     if(res == NOUSER){
  99.         printf("\n Unknown user or incorrectpassword.\n");
  100.     }
  101.   }
  102.  
  103.   printf("\nUser authenticated successfully.\n");
  104.   free(password);
  105.   return(0);
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement