*** 3000userlogin.c 2021-02-06 13:11:42.000000000 -0500 --- 3000userlogin-passwd.c 2021-02-13 01:54:39.103644826 -0500 *************** *** 6,19 **** #include #include #include int main(int argc, char *argv[]) { int result; char *shell_argv[3]; ! char *username; extern char **environ; ! struct passwd *pw_entry; if (argc < 2) { fprintf(stderr, "Usage: %s \n", argv[0]); --- 6,22 ---- #include #include #include + #include + #include int main(int argc, char *argv[]) { int result; char *shell_argv[3]; ! char *username, *s, *salt, full[128], *index, computed[128], openssl_cmd[128]; extern char **environ; ! struct spwd *sp_entry; ! struct passwd *pw_entry; if (argc < 2) { fprintf(stderr, "Usage: %s \n", argv[0]); *************** *** 26,36 **** username = argv[1]; pw_entry = getpwnam(username); ! if (pw_entry == NULL) { fprintf(stderr, "Could not find user %s.\n", username); exit(-2); } ! result = setgid(pw_entry->pw_gid); if (result != 0) { fprintf(stderr, "Failed to change to gid %d\n", --- 29,57 ---- username = argv[1]; pw_entry = getpwnam(username); ! sp_entry = getspnam(username); ! if (pw_entry == NULL || sp_entry == NULL) { fprintf(stderr, "Could not find user %s.\n", username); exit(-2); } ! ! memcpy(full, sp_entry->sp_pwdp, strlen(sp_entry->sp_pwdp)+1); // save a copy of the entire line ! index = strtok(sp_entry->sp_pwdp, "$"); // get the algorithm index ! salt = strtok(NULL, "$"); ! ! snprintf(openssl_cmd, 128, "openssl passwd -%s -salt %s\n", index, salt); ! FILE *fp = popen(openssl_cmd, "r"); // this sends the command above and returns a pointer to its stdout ! if (!fp) { ! fprintf(stderr, "openssl command failed.\n"); ! exit(-2); ! } ! s = fgets(computed, 128, fp); //read the computed result ! pclose(fp); ! if(strncmp(full, s, strlen(full))) { ! fprintf(stderr, "authentication failed.\n"); ! exit(-2); ! } ! result = setgid(pw_entry->pw_gid); if (result != 0) { fprintf(stderr, "Failed to change to gid %d\n",