Authentication of local Unix user using C
Can I a开发者_StackOverflow社区uthenticate a local Unix users using C? If so does anyone have a code snippet?
Good old way to do that, using /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
}
You will also probably need to include <shadow.h>
and <pwd.h>
, and <unistd.h>
for crypt.
The whole process of calculations with hash&salt is certainly described
somewhere in header's manual pages.
精彩评论