免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4288 | 回复: 3
打印 上一主题 下一主题

[C] 【已解决】关于pam_unix中比较登录密码和shadow中取得的密码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-06-08 14:58 |只看该作者 |倒序浏览
本帖最后由 雨夜流星 于 2011-06-09 10:18 编辑

想修改一下pam_unix.so

找了一份pam_unix.c源码,编译替换pam_unix.so后,发现登录验证总是不对,后来查找,发现是密码比较部分失败。


  1. struct passwd *pwd;
  2. struct spwd *sp;

  3. if ((!pwd->pw_passwd[0] && (flags & PAM_DISALLOW_NULL_AUTHTOK)) ||
  4.             (crypt_password = crypt(password, pwd->pw_passwd)) == NULL ||
  5.             strcmp(crypt_password, pwd->pw_passwd) != 0)
复制代码
这里strcmp(crypt_password, pwd->pw_passwd)失败,crypt_password为输入登录密码后加密的结果:<xxNozt....>,
pwd->pw_passwd结果为x,说明使用了shadow,后来加上shadow,

sp = getspnam(user)
sp->sp_pwdp的结果为shadow中的内容:$1$Jw6ju9gz$Uo....

但是crypt_password和sp->sp_pwdp的值依然不相同,所以登录验证失败,

这里想请教一下:
密码比较,是我哪里取得不对,少了什么步骤呢?如何才能密码验证成功?

论坛徽章:
0
2 [报告]
发表于 2011-06-08 17:07 |只看该作者
我前几天写的,用户和密码使用环境变量来传:

参考:http://www.linuxforum.net/books/upfaq/x541.htm#AEN557
  1. #define _XOPEN_SOURCE
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <shadow.h>
  6. #include <unistd.h>
  7. #include <errno.h>

  8. int check_password (const char *user, const char *password)
  9. {
  10.     struct spwd *user_data;

  11.     if (strcmp (user, "root") == 0)
  12.     {
  13.         fprintf (stderr, "root not allowed\n");
  14.         return 3;
  15.     }

  16.     errno = 0;
  17.     user_data = getspnam (user);
  18.     if (user_data == NULL)
  19.     {
  20.         fprintf (stderr, "No such user %s, or error %s\n", user, strerror (errno));
  21.         sleep (2);
  22.         return 1;
  23.     }

  24.     if (strcmp (crypt (password, user_data->sp_pwdp), user_data->sp_pwdp) != 0)
  25.     {
  26.         fprintf (stderr, "Auth user %s failed\n", user);
  27.         sleep (2);
  28.         return 2;
  29.     }

  30.     return 0;
  31. }

  32. int main (void)
  33. {
  34.     char *user, *password;

  35.     if ((user= getenv("USER")) == NULL) exit(2);
  36.     if ((password= getenv("PASS")) == NULL) exit(3);

  37.     exit (check_password(user, password));
  38. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2011-06-09 10:17 |只看该作者
回复 2# lyreopera


    非常感谢,按照您的提示,已经解决了问题!

论坛徽章:
0
4 [报告]
发表于 2011-06-10 10:49 |只看该作者
本帖最后由 satfire 于 2011-06-14 17:06 编辑

centos 5.5系统。,无论用root,还是普通用户,
  if ((password= getenv("PASS")) == NULL) exit(3);就退出了。

怎么回事?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP