#include #include #include #include #include #include #include void getpwd(char* password){ struct termios raw; tcgetattr(STDIN_FILENO, &raw); raw.c_lflag &= ~ECHO; tcsetattr(STDIN_FILENO, 0, &raw); printf("Password: "); fgets(password, 128, stdin); password[strcspn(password, "\n")] = 0; raw.c_lflag |= ECHO; tcsetattr(STDIN_FILENO, 0, &raw); printf("\n"); } void runprog(int argc, char** argv) { for(int i=0; ipw_uid; if (uid != ruid && ruid != 0) { fprintf(stderr, "You are not in the username file.\n"); return -4; } struct spwd* shadowEntry = getspnam(username); if (!shadowEntry) { fprintf(stderr, "Could not get shadow entry, suid bit not set or username invalid?\n"); return -5; } int tries = 0; char password[128]; while (tries < 3) { getpwd(password); if (strcmp(shadowEntry->sp_pwdp, crypt(password, shadowEntry->sp_pwdp)) == 0) { runprog(argc, argv); return 0; } fprintf(stderr, "Wrong password.\n"); tries++; } return -5; }