Created
November 5, 2014 11:38
-
-
Save KlausTrainer/b3d156091049157fd7a0 to your computer and use it in GitHub Desktop.
Checking System Account Passwords on GNU/Linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <string.h> | |
#include <crypt.h> | |
#include <shadow.h> | |
int check_password(const char *plain_password, const char *crypt_password) | |
{ | |
return strcmp(crypt(plain_password, crypt_password), crypt_password) == 0; | |
} | |
int main(int argc, char* argv[]) | |
{ | |
struct spwd* spwd; | |
if (argc != 3) { | |
// invalid number of arguments | |
return EXIT_FAILURE; | |
} | |
spwd = getspnam(argv[1]); | |
if (spwd == NULL) { | |
// we're either not running as root | |
// or the user doesn't exist | |
return EXIT_FAILURE; | |
} | |
if (!check_password(argv[2], spwd->sp_pwdp)) { | |
// the provided password is incorrect | |
return EXIT_FAILURE; | |
} | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment