Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Shows user info from local pwfile.
- *
- * Usage: userinfo username
- */
- #define _XOPEN_SOURCE
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "pwdblib.h" /* include header declarations for pwdblib.c */
- /* Define some constants. */
- #define USERNAME_SIZE (32)
- #define PASSWORD_SIZE (32)
- #define NOUSER (-1)
- void read_username(char *username)
- {
- printf("login: ");
- fgets(username, USERNAME_SIZE, stdin);
- /* remove the newline included by getline() */
- username[strlen(username) - 1] = '\0';
- }
- int main(int argc, char **argv)
- {
- char username[USERNAME_SIZE];
- char password[PASSWORD_SIZE];
- char saltPass[PASSWORD_SIZE];
- char salt[2];
- while(1 == 1) {
- //reads username from terminal
- read_username(username);
- //reads password from terminal
- strcpy(password, getpass("password: "));
- //gets pw structure from pwfile
- struct pwdb_passwd *p = pwdb_getpwnam(username);
- if(p->pw_failed > 4) {
- printf("Too many login attemps. Contact administrator.\n");
- } else if (p != NULL) {
- //gets hashed password from structure
- strcpy(saltPass, p->pw_passwd);
- //gets salt from hashed password
- strncpy(salt, saltPass, 2);
- //compares read hashed password to stores hashed password
- if(strcmp(crypt(password,salt), saltPass) == 0) {
- p->pw_failed = 0;
- p->pw_age = p->pw_age+1;
- pwdb_update_user(p);
- printf("User authenticated succesfully!\n");
- if(p->pw_age > 9) {
- printf("Remember to change your password.\n");
- }
- return 0;
- } else {
- p->pw_failed = p->pw_failed+1;
- pwdb_update_user(p);
- printf("Incorrect password.\n");
- if(p->pw_failed > 4) {
- printf("Too many login attemps. Contact administrator.\n");
- }
- }
- } else {
- printf("User not found.\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement