一定要使用root权限运行此程序
编译时加上 -lcrypt 选项
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <shadow.h>
#include <unistd.h>
#include <string.h>
#include <crypt.h>
#define _XOPEN_SOURCE
int main(int argc, const char *argv[])
{
struct spwd * spwd = NULL;
char * pass_name = NULL;
char * crypt_pass = NULL;
if(argc < 2)
{
fprintf(stderr, "Usage %s <user_name>\n", argv[0]);
return -1;
}
pass_name = getpass("PassWord:");
spwd = getspnam(argv[1]);
puts(spwd->sp_pwdp);
crypt_pass = crypt(pass_name, spwd->sp_pwdp);
if(crypt_pass == NULL)
{
fprintf(stderr, "crypt error\n");
}
if(strcmp(crypt_pass, spwd->sp_pwdp) == 0)
puts("ok!");
else
puts("error!");
return 0;
}
测试结果