void write_pw(int entry, int value, struct pwdb_passwd *p) { if (entry == PW_FAILED) { p->pw_failed = value; } else if (entry == PW_AGE) { p->pw_age = value; } else { printf("Faulty entry passed"); return; } switch (entry) { case PW_FAILED: p->pw_failed = value; break; case PW_AGE: p->pw_age = value; break; default: printf("Erroneous entry passed to write_pw."); return; } if (pwdb_update_user(p) != 0) { printf("pwdb_update_user returned error %s\n", pwdb_err2str(pwdb_errno)); } }
int read_userpwd(char *username){ struct pwdb_passwd *pw_entry; pw_entry = pwdb_getpwnam(username); char *password; char salt[2]; strncpy(salt, pw_entry->pw_passwd, 2); password = getpass("password: "******"user name could not be found.\n"); }else if(pwdb_errno==PWDB_FILEERR){ printf("could not access the file.\n");} else if(pwdb_errno==PWDB_MEMERR){ printf("could not allocate memory for the struct pwdb_passwd.\n");} else if(pwdb_errno==PWDB_ENTRERR){ printf("line in PWFILENAME somehow has invalid format.\n"); }return(0); }else{*/ if(strcmp(crypt(password, salt), pw_entry-> pw_passwd)==0){ printf("User authenticated successfully. \n"); pw_entry->pw_failed = 0; pw_entry->pw_age++; pwdb_update_user(pw_entry); if(pw_entry->pw_age>=10){ printf("WOOPS, time to change PW. \n"); } return(1); }else{ if(pw_entry->pw_failed>=5){ printf("User locked. \n"); } pw_entry->pw_failed++; pwdb_update_user(pw_entry); printf("Unknown user or incorrect password. \n"); read_username(username); return(0); } }
void reset_password(struct pwdb_passwd *p){ // Unnecessary? Not sure about lab instructions. char password[PASSWORD_SIZE]; char confirm[PASSWORD_SIZE]; while (1){ read_password(password); read_password(confirm); if (strcmp(password, confirm) == 0) break; } char *crypted = crypt(password, p->pw_passwd); p->pw_passwd=crypted; pwdb_update_user(p); }
void login_failed(struct pwdb_passwd *p){ p->pw_failed++; if (p->pw_failed >= MAX_FAILED_LOGINS) p->pw_failed = -1; pwdb_update_user(p); }
void login_success(struct pwdb_passwd *p){ p->pw_failed = 0; p->pw_age++; if (p->pw_age >= MUST_RESET_PASSWORD) reset_password(p); else pwdb_update_user(p); }
int main(int argc,char **argv) { char *username; char *buf; size_t buflen, chread; struct pwdb_passwd *p, *oldp; int updt; if (argc<2) { printf("Usage: update_user username\n"); return(0); } username=argv[1]; oldp=pwdb_getpwnam(username); if (oldp!=NULL) updt=1; else if ((oldp==NULL) && (pwdb_errno==PWDB_NOUSER)) updt=0; else { printf("pwdb_getpwnam return error: %s\n",pwdb_err2str(pwdb_errno)); return(0); } p=(struct pwdb_passwd *)malloc(sizeof(struct pwdb_passwd)); buf=NULL; buflen=0; p->pw_name=username; chread=0; while (1) { printf("Password:"******"New user, so you must enter data\n"); if ((updt) && (chread<1)) { p->pw_passwd=oldp->pw_passwd; break; } if (chread>0) { p->pw_passwd=(char *)malloc(sizeof(char)*(chread+1)); strncpy(p->pw_passwd,buf,chread); p->pw_passwd[chread]='\0'; break; } } chread=0; while (1) { printf("Uid:"); chread=getline(&buf,&buflen,stdin); buf[chread]='\0'; chread--; /* remove '\n' included by getline */ if ((!updt) && (chread<1)) printf("New user, so you must enter data\n"); if ((updt) && (chread<1)) { p->pw_uid=oldp->pw_uid; break; } if (chread>0) { p->pw_uid=atoi(buf); break; } } chread=0; while (1) { printf("Gid:"); chread=getline(&buf,&buflen,stdin); buf[chread]='\0'; chread--; /* remove '\n' included by getline */ if ((!updt) && (chread<1)) printf("New user, so you must enter data\n"); if ((updt) && (chread<1)) { p->pw_gid=oldp->pw_gid; break; } if (chread>0) { p->pw_gid=atoi(buf); break; } } chread=0; while (1) { printf("Real name:"); chread=getline(&buf,&buflen,stdin); buf[chread]='\0'; chread--; /* remove '\n' included by getline */ if ((!updt) && (chread<1)) printf("New user, so you must enter data\n"); if ((updt) && (chread<1)) { p->pw_gecos=oldp->pw_gecos; break; } if (chread>0) { p->pw_gecos=(char *)malloc(sizeof(char)*(chread+1)); strncpy(p->pw_gecos,buf,chread); p->pw_gecos[chread]='\0'; break; } } chread=0; while (1) { printf("Home directory:"); chread=getline(&buf,&buflen,stdin); buf[chread]='\0'; chread--; /* remove '\n' included by getline */ if ((!updt) && (chread<1)) printf("New user, so you must enter data\n"); if ((updt) && (chread<1)) { p->pw_dir=oldp->pw_dir; break; } if (chread>0) { p->pw_dir=(char *)malloc(sizeof(char)*(chread+1)); strncpy(p->pw_dir,buf,chread); p->pw_dir[chread]='\0'; break; } } chread=0; while (1) { printf("Shell:"); chread=getline(&buf,&buflen,stdin); buf[chread]='\0'; chread--; /* remove '\n' included by getline */ if ((!updt) && (chread<1)) printf("New user, so you must enter data\n"); if ((updt) && (chread<1)) { p->pw_shell=oldp->pw_shell; break; } if (chread>0) { p->pw_shell=(char *)malloc(sizeof(char)*(chread+1)); strncpy(p->pw_shell,buf,chread); p->pw_shell[chread]='\0'; break; } } chread=0; while (1) { printf("Failed:"); chread=getline(&buf,&buflen,stdin); buf[chread]='\0'; chread--; /* remove '\n' included by getline */ if ((!updt) && (chread<1)) printf("New user, so you must enter data\n"); if ((updt) && (chread<1)) { p->pw_failed=oldp->pw_failed; break; } if (chread>0) { p->pw_failed=atoi(buf); break; } } chread=0; while (1) { printf("Age:"); chread=getline(&buf,&buflen,stdin); buf[chread]='\0'; chread--; /* remove '\n' included by getline */ if ((!updt) && (chread<1)) printf("New user, so you must enter data\n"); if ((updt) && (chread<1)) { p->pw_age=oldp->pw_age; break; } if (chread>0) { p->pw_age=atoi(buf); break; } } if (pwdb_update_user(p)!=0) { printf("pwdb_update_user returned error %s\n",pwdb_err2str(pwdb_errno)); } return(0); }