int print_info(const char *username) { struct pwdb_passwd *p = pwdb_getpwnam(username); if (p != NULL) { printf("Name: %s\n", p->pw_name); printf("Passwd: %s\n", p->pw_passwd); printf("Uid: %u\n", p->pw_uid); printf("Gid: %u\n", p->pw_gid); printf("Real name: %s\n", p->pw_gecos); printf("Home dir: %s\n",p->pw_dir); printf("Shell: %s\n", p->pw_shell); return 0; } else { return NOUSER; } }
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); } }
int main(int argc,char **argv){ struct pwdb_passwd *p; if (argc<2) { printf("Usage: pwdb_showuser username\n"); return(0); } p=pwdb_getpwnam(argv[1]); if (p==NULL) { printf("pwdb_getpwnam returned error: %s\n",pwdb_err2str(pwdb_errno)); return(0); } printf("User info:\n"); print_info(p); return(0); }
int print_userinfo(const char *username){ struct pwdb_passwd *pw_entry; pw_entry=pwdb_getpwnam(username); if (pw_entry!=NULL) { printf("\nInfo from getpwnam() for user: %s\n",username); printf("Name: %s\n",pw_entry->pw_name); printf("Passwd: %s\n",pw_entry->pw_passwd); printf("Uid: %d\n",pw_entry->pw_uid); printf("Gid: %d\n",pw_entry->pw_gid); printf("Real name: %s\n",pw_entry->pw_gecos); printf("Shell: %s\n\n",pw_entry->pw_shell); } else return(NOUSER); return(0); }
void login () { char username[USERNAME_SIZE]; int logged_in = 0; while (!logged_in) { read_username(username); struct pwdb_passwd *p = pwdb_getpwnam(username); if (p == NULL) { printf("\nFound no user with name: %s\n", username); } else { char *passwd = crypt(getpass("password: "******"\nUser authenticated successfully\n"); logged_in = 1; } else { printf("\nUnknown user or incorrect password.\n"); } } } }
int login(struct pwdb_passwd *info){ char username[USERNAME_SIZE]; char password[PASSWORD_SIZE]; /* * Write "login: "******"Locked out!\n"); return 1; } read_password(password); if (was_interrupted) return 1; char *crypted = crypt(password, info->pw_passwd); if (strncmp(info->pw_passwd, crypted, HASH_SIZE) == 0){ login_success(info); printf("Logged in!\n"); return 0; } else { login_failed(info); printf("No!\n"); return 1; } }
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); }
int main(int argc, char **argv) { char username[USERNAME_SIZE]; char *password; // We store in heap, remember to free the memory. char salt[SALT_SIZE]; struct pwdb_passwd *p; // Disable CTRL-C termination signal(SIGINT, SIG_IGN); /* * Write "login: "******"Unsuccessful login."); return 0; } /* * Write "password: "******"password: "******"This account has been locked, please contact an administrator.\n"); } else if (strcmp(password, p->pw_passwd) == 0) { printf("Successful login.\n"); printf("Previous failed logins: %d\n", p->pw_failed); if (p->pw_age > 10) printf("Your password is old, please consider changing it.\n"); write_pw(PW_AGE, p->pw_age + 1, p); write_pw(PW_FAILED, 0, p); start_userterm(p); } else { printf("Unsuccessful login.\n"); write_pw(PW_FAILED, p->pw_failed + 1, p); if (PW_FAILED > 5) write_pw(PW_FAILED, -1, p); } memset(password, '0', strlen(password)); return 0; }