我可以使用C验证本地Unix用户吗?如果是这样,是否有人有代码片段?
使用/ etc / shadow的旧方法,很好:
int sys_auth_user (const char*username, const char*password)
{
struct passwd*pw;
struct spwd*sp;
char*encrypted, *correct;
pw = getpwnam (username);
endpwent();
if (!pw) return 1; //user doesn't really exist
sp = getspnam (pw->pw_name);
endspent();
if (sp)
correct = sp->sp_pwdp;
else
correct = pw->pw_passwd;
encrypted = crypt (password, correct);
return strcmp (encrypted, correct) ? 2 : 0; // bad pw=2, success=0
}
您可能还需要包括<shadow.h>
和<pwd.h>
,以及<unistd.h>
来表示密码。肯定会描述使用哈希和盐进行计算的整个过程标头手册页中的某处。