Code: Select all
/* compile with:
* diet gcc chkpswd.c -Os -s -o check_password -lcrypt
*/
#include <stdlib.h>
#include <stdio.h>
#include <pwd.h>
#include <shadow.h>
#include <string.h>
#include <crypt.h>
#include <unistd.h>
#include <libgen.h>
/* example: ./check_password username password && echo "success" || echo "failure"
* returns 0 if username and password in /etc/shadow match - 1 if not.
*/
int check_pass(const char *plainpw, const char *cryptpw) {
return strcmp(crypt(plainpw,cryptpw), cryptpw) == 0;
}
int main(int argc, char **argv) {
if (argc != 3) {
printf("usage:\n\t%s [username] [password]\n", basename(argv[0]));
return 1;
}
if ( ! getuid() == 0) {
printf("You need to be root\n");
return 1;
}
if ( ! getpwnam(argv[1]) ) {
printf("No such user %s\n", argv[1]);
return 1;
}
if ( check_pass(argv[2], (getspnam(argv[1]))->sp_pwdp) ) {
//printf("Seems OK\n");
return 0;
} else {
//printf("NOT OK\n");
return 1;
}
}