Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Program userinfo.c
- *
- * This program prompts the user for a login name, and tries to
- * extract user information from the /etc/passwd file.
- *
- */
- #define _GNU_SOURCE
- #define _XOPEN_SOURCE
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <pwd.h>
- #include <sys/types.h>
- #include <string.h>
- #include "pwdblib.c"
- /* define some error constants */
- #define NOUSER -1
- #define LOCKEDUSER = -2
- /* define max size of a username */
- #define USERNAME_SIZE 32
- int read_username(char *username){
- printf("login: ");
- fgets(username,USERNAME_SIZE,stdin);
- /* remove the CR included by getline() */
- username[strlen(username)-1]='\0';
- return(0);
- }
- int confirm(char *username, char *password){
- struct passwd *pw_entry;
- pw_entry=getpwnam(username);
- if (pw_entry==NULL) {return(NOUSER)};
- password = crypt(password, strndup(pw_entry->pw_passwd, 2));
- if (strcmp(password, pw_entry->pw_passwd) != 0) {return(NOUSER)};
- return(0);
- }
- int confirm2(char *username, char *password){
- struct pwdb_passwd *pw_entry;
- pw_entry=getpwnam(username);
- if (pw_entry==NULL) {return(NOUSER)};
- password = crypt(password, strndup(pw_entry->pw_passwd, 2));
- if (strcmp(password, pw_entry->pw_passwd) != 0) {
- pw_entry->pw_failed++;
- pwdb_update_user(pw_entry);
- if (pw_entry->pw_failed > 5) {return(LOCKEDUSER)};
- return(NOUSER);
- } else {
- pw_entry->pw_failed = 0;
- pw_entry->pw_age++;
- pwdb_update_user(pw_entry);
- if (pw_entry->pw_age > 10) {
- printf("Password is too old, change it asap");
- }
- }
- return(0);
- }
- int main(int argc,char **argv) {
- char username[USERNAME_SIZE];
- /* write "login:" and read user input */
- read_username(username);
- int res = NOUSER;
- char *password;
- while(res == NOUSER){
- read_username(username);
- password = getpass("password: ");
- if (argc > 1 && strcmp(argv[1],"-l")==0){
- res = confirm2(username, password);
- }else{
- res = confirm(username, password);
- }
- if (res == LOCKEDUSER) {
- printf("\nUser locked.\n");
- }
- if(res == NOUSER){
- printf("\n Unknown user or incorrectpassword.\n");
- }
- }
- printf("\nUser authenticated successfully.\n");
- free(password);
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement