开发者

Authentication of local Unix user using C

开发者 https://www.devze.com 2023-03-16 23:27 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消