Created
February 21, 2013 04:46
-
-
Save Zolomon/5002163 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define _BSD_SOURCE /* Get getpass() declaration from <unistd.h> */ | |
| #define _XOPEN_SOURCE /* Get crypt() declaration from <unistd.h> */ | |
| #include <unistd.h> | |
| #include <limits.h> | |
| #include <pwd.h> | |
| #include <shadow.h> | |
| #include <stdlib.h> | |
| #include <stdio.h> | |
| #include <sys/types.h> | |
| #include <string.h> | |
| #include <errno.h> | |
| #include <termios.h> | |
| #include <crypt.h> | |
| #include "pwdblib.h" | |
| int main(int argc, char const *argv[]) | |
| { | |
| char *username, *password, *encrypted, *p; | |
| struct pwdb_passwd *pwd; | |
| int authOk; | |
| size_t len; | |
| long lnmax; | |
| lnmax = sysconf(_SC_LOGIN_NAME_MAX); | |
| if (lnmax == -1) /* If limit is indeterminate */ | |
| lnmax = 256; /* make a guess */ | |
| username = malloc(lnmax); | |
| if (username == NULL) | |
| perror("malloc"); | |
| while (1) { | |
| printf("login: "); | |
| fflush(stdout); | |
| if (fgets(username, lnmax, stdin) == NULL) | |
| exit(EXIT_FAILURE); | |
| len = strlen(username); | |
| if (username[len - 1] == '\n') | |
| username[len - 1] = '\0'; /* Remove trailing 'n'*/ | |
| pwd = pwdb_getpwnam(username); | |
| int found_user = 1; | |
| if (pwd == NULL) { | |
| //printf("pwd null\n"); | |
| if (pwdb_errno == PWDB_NOUSER) { | |
| // No user found... | |
| //printf("No user found..."); | |
| found_user = -1; | |
| //exit(EXIT_FAILURE); | |
| } | |
| if (pwdb_errno == PWDB_FILEERR) { | |
| perror("can't read pwdb file"); | |
| exit(EXIT_FAILURE); | |
| } | |
| if (pwdb_errno == PWDB_MEMERR) { | |
| perror("out of memory"); | |
| exit(EXIT_FAILURE); | |
| } | |
| if (pwdb_errno == PWDB_ENTRERR) { | |
| perror("pwdb entry corrupt"); | |
| exit(EXIT_FAILURE); | |
| } | |
| } | |
| password = getpass("Password: "); | |
| /* Encrypt password and erase cleartext version immediately */ | |
| if (found_user == 1) { | |
| encrypted = crypt(password, pwd->pw_passwd); | |
| for (p = password; *p != '\0'; ) { | |
| *p++ = '\0'; | |
| } | |
| if (encrypted == NULL) | |
| perror("crypt"); | |
| authOk = strcmp(encrypted, pwd->pw_passwd) == 0; | |
| } | |
| if (!authOk || found_user == -1) { | |
| printf("Unknown user or incorrect password\n"); | |
| if (found_user == 1) { | |
| pwd->pw_failed++; | |
| printf("failed...\n"); | |
| } | |
| if (pwd->pw_age >= 5) { | |
| pwd->pw_passwd[0] = '*'; | |
| pwd->pw_passwd[1] = '\0'; | |
| printf("locking out...\n"); | |
| } | |
| if (pwdb_update_user(pwd) != 0) { | |
| printf("pwfile corrupt"); | |
| exit(EXIT_FAILURE); | |
| } | |
| //free(pwd); | |
| //free(spwd); | |
| //free(password); | |
| //free(encrypted); | |
| continue; | |
| } | |
| pwd->pw_failed = 0; | |
| pwd->pw_age++; | |
| printf("resetting fails, increasing age... %d\n", pwd->pw_age); | |
| if (pwdb_update_user(pwd) != 0) { | |
| printf("pwfile corrupt"); | |
| exit(EXIT_FAILURE); | |
| } | |
| printf("User authenticated successfully\n"); | |
| return 0; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment