Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.16 KB | None | 0 0
  1. /*
  2.  * Shows user info from local pwfile.
  3.  *  
  4.  * Usage: userinfo username
  5.  */
  6.  
  7. #define _XOPEN_SOURCE
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <stdbool.h>
  13. #include "pwdblib.h"   /* include header declarations for pwdblib.c */
  14.  
  15. /* Define some constants. */
  16. #define USERNAME_SIZE (32)
  17. #define PASSWORD_SIZE (32)
  18. #define NOUSER (-1)
  19.  
  20.  
  21. int print_info(const char *username)
  22. {
  23.   struct pwdb_passwd *p = pwdb_getpwnam(username);
  24.   if (p != NULL) {
  25.     printf("Name: %s\n", p->pw_name);
  26.     printf("Passwd: %s\n", p->pw_passwd);
  27.     printf("Uid: %u\n", p->pw_uid);
  28.     printf("Gid: %u\n", p->pw_gid);
  29.     printf("Real name: %s\n", p->pw_gecos);
  30.     printf("Home dir: %s\n",p->pw_dir);
  31.     printf("Shell: %s\n", p->pw_shell);
  32.     return 0;
  33.   } else {
  34.     return NOUSER;
  35.   }
  36. }
  37.  
  38. void read_username(char *username)
  39. {
  40.   printf("login: ");
  41.   fgets(username, USERNAME_SIZE, stdin);
  42.  
  43.   /* remove the newline included by getline() */
  44.   username[strlen(username) - 1] = '\0';
  45. }
  46.  
  47. void read_password(char *password)
  48. {
  49.     strcpy(password, getpass("Enter password: "));
  50. }
  51.  
  52. void checkPassword(char *realPassword, const char *username)
  53. {
  54.   struct pwdb_passwd *p = pwdb_getpwnam(username);
  55.   if (p != NULL) {
  56.       strcpy(realPassword, p->pw_passwd);
  57.   }
  58. }
  59.  
  60. void increaseSuccessfulLogin(const char *username){
  61.     struct pwdb_passwd *p = pwdb_getpwnam(username);
  62.     if (p != NULL) {
  63.       int oldValue = p->pw_age;
  64.       printf("old value: %i \n", oldValue);
  65.       int newValue = oldValue + 1;
  66.       if (newValue > 10) {
  67.       printf("Your password is old, it is recommended that you update it! \n");
  68.     }
  69.       p->pw_age = newValue;
  70.       pwdb_update_user(p);
  71.     }
  72. }
  73.  
  74. void increaseFailedLogin(const char *username){
  75.     struct pwdb_passwd *p = pwdb_getpwnam(username);
  76.       if (p != NULL) {
  77.         int oldValue = p->pw_failed;
  78.         int newValue = oldValue + 1;
  79.         p->pw_failed = newValue;
  80.         pwdb_update_user(p);
  81.       }
  82. }
  83.  
  84. void zeroFails(const char *username){
  85.     struct pwdb_passwd *p = pwdb_getpwnam(username);
  86.     if (p != NULL) {
  87.       int newValue = 0;
  88.       p->pw_failed = newValue;
  89.       pwdb_update_user(p);
  90.     }  
  91. }
  92.  
  93. bool isLockedOut(const char *username){
  94.     struct pwdb_passwd *p = pwdb_getpwnam(username);
  95.     if (p != NULL) {
  96.       int failedLogins = p->pw_failed;
  97.       return failedLogins >= 5;
  98.     }  
  99. }
  100.  
  101. char* concat(const char *s1, const char *s2)
  102. {
  103.     char *result = malloc(strlen(s1) + strlen(s2) + 1); // +1 for the null-terminator
  104.     // in real code you would check for errors in malloc here
  105.     strcpy(result, s1);
  106.     strcat(result, s2);
  107.     return result;
  108. }
  109.  
  110. int main(int argc, char **argv)
  111. {
  112.  
  113.  
  114.   while(1) {
  115.  
  116.    char username[USERNAME_SIZE];
  117.    char password[PASSWORD_SIZE];
  118.    char realPassword[PASSWORD_SIZE];
  119.      
  120.   /*
  121.    * Write "login: " and read user input. Copies the username to the
  122.    * username variable.
  123.    */
  124.    read_username(username);
  125.    read_password(password);
  126.  
  127.    if (isLockedOut(username)) {
  128.    printf("Too many unsuccessful login attempts. Please contact admin.\n");
  129.    return 0;
  130.    }
  131.  
  132.    checkPassword(realPassword, username);
  133.  
  134.      if (realPassword!=NULL) {
  135.  
  136.          char salt[2];
  137.          salt[0] = realPassword[0];
  138.          salt[1] = realPassword[1];
  139.          const char *saltConst = salt;
  140.          const char *passwordConst = password;
  141.          const char *cryptedPw = crypt(passwordConst, saltConst);
  142.          const char *saltAndCryptedPassword = concat(salt, cryptedPw); // username changes somewhere in this if-statement!
  143.           if (strcmp(realPassword,cryptedPw) == 0) {
  144.              increaseSuccessfulLogin(username);
  145.              zeroFails(username);
  146.              printf("User authenticated successfully\n");
  147.              return 0;
  148.           }    
  149.      }
  150.    
  151.      increaseFailedLogin(username);
  152.      printf("Unknown user or incorrect password. \n");
  153.   }
  154.   /* Show user info from our local pwfile.
  155.    if (print_info(username) == NOUSER) {
  156.       /* if there are no user with that usename...
  157.       printf("\nFound no user with name: %s\n", username);  
  158.       return 0;
  159.   } */
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement