密码存储在/etc/shadow,是被加密了的密码。
得不到原本的密码,只能得到加密的密码。
struct spwd *getspnam(const char *name);
得到一个包含有加密密码的结构体。
char *getpass(const char *s)
返回值是输入的密码,传参内容可以是任意值。
char *crypt(const char *key, const char *salt)
可以把密码计算成加密的密码。
#define _XOPEN_SOURCE /* See feature_test_macros(7) */
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i;
struct spwd *spw = NULL;
char *pass = NULL;
char *cry_key = NULL;
if(argc < 2)
exit(1);
spw = getspnam(argv[1]);
if(NULL == spw)
{
perror("getspnam()");
exit(1);
}
pass = getpass("passwd:");
if(NULL == pass)
{
perror("getpass:");
exit(1);
}
cry_key = crypt(pass, spw->sp_pwdp);
if(NULL == cry_key)
{
perror("crypt");
exit(1);
}
if(strcmp(cry_key, spw->sp_pwdp) == 0)
printf("验证正确\n");
else
printf("验证错误\n");
exit(0);
}