使用C对本地Unix用户进行身份验证

问题描述 投票:1回答:1

我可以使用C验证本地Unix用户吗?如果是这样,是否有人有代码片段?

c security unix
1个回答
2
投票

使用/ 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>来表示密码。肯定会描述使用哈希和盐进行计算的整个过程标头手册页中的某处。

© www.soinside.com 2019 - 2024. All rights reserved.