Skip to content

Instantly share code, notes, and snippets.

@KlausTrainer
Created November 5, 2014 11:38
Show Gist options
  • Save KlausTrainer/b3d156091049157fd7a0 to your computer and use it in GitHub Desktop.
Save KlausTrainer/b3d156091049157fd7a0 to your computer and use it in GitHub Desktop.
Checking System Account Passwords on GNU/Linux
#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