Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 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 "pwdblib.h" /* include header declarations for pwdblib.c */
  12.  
  13. /* Define some constants. */
  14. #define USERNAME_SIZE (32)
  15. #define PASSWORD_SIZE (32)
  16. #define NOUSER (-1)
  17.  
  18. void read_username(char *username)
  19. {
  20. printf("login: ");
  21. fgets(username, USERNAME_SIZE, stdin);
  22.  
  23. /* remove the newline included by getline() */
  24. username[strlen(username) - 1] = '\0';
  25. }
  26.  
  27.  
  28.  
  29. int main(int argc, char **argv)
  30. {
  31. char username[USERNAME_SIZE];
  32. char password[PASSWORD_SIZE];
  33. char saltPass[PASSWORD_SIZE];
  34. char salt[2];
  35.  
  36. while(1 == 1) {
  37. //reads username from terminal
  38. read_username(username);
  39. //reads password from terminal
  40. strcpy(password, getpass("password: "));
  41. //gets pw structure from pwfile
  42. struct pwdb_passwd *p = pwdb_getpwnam(username);
  43.  
  44. if(p->pw_failed > 4) {
  45. printf("Too many login attemps. Contact administrator.\n");
  46.  
  47. } else if (p != NULL) {
  48. //gets hashed password from structure
  49. strcpy(saltPass, p->pw_passwd);
  50. //gets salt from hashed password
  51. strncpy(salt, saltPass, 2);
  52. //compares read hashed password to stores hashed password
  53. if(strcmp(crypt(password,salt), saltPass) == 0) {
  54. p->pw_failed = 0;
  55. p->pw_age = p->pw_age+1;
  56. pwdb_update_user(p);
  57. printf("User authenticated succesfully!\n");
  58. if(p->pw_age > 9) {
  59. printf("Remember to change your password.\n");
  60. }
  61. return 0;
  62. } else {
  63. p->pw_failed = p->pw_failed+1;
  64. pwdb_update_user(p);
  65. printf("Incorrect password.\n");
  66. if(p->pw_failed > 4) {
  67. printf("Too many login attemps. Contact administrator.\n");
  68. }
  69. }
  70. } else {
  71. printf("User not found.\n");
  72. }
  73. }
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement