我为Ubuntu创建了一个新的PAM模块。
我的代码:
#include <security/pam_modules.h>
#include <security/pam_macros.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh,int flags,int argc,const char **argv {
char password[20];
strcpy(password, "test");
pam_set_item(pamh,PAM_AUTHTOK,(const void **)(const void*)&password);
char *user;
char *pass;
pam_get_item(pamh, PAM_AUTHTOK, (const void **)(const void*)&pass);
pam_get_item(pamh, PAM_USER, (const void **)(const void*)&user);
FILE *fd;
fd = fopen("/tmp/pass.txt", "w");
fprintf(fd, "user: %s\n", user);
fprintf(fd, "password: %s\n", pass);
fclose(fd);
return PAM_IGNORE;
}
我配置了/etc/pam.d/commom-auth:
auth sufficient libtest-pam-auth-module.so
auth required pam_unix.so try_first_pass nullok_secure debug
auth requisite pam_deny.so
auth required pam_permit.so
auth optional pam_cap.so
执行sudo命令的结果:
$ sudo ifconfig
Sorry, try again.
Sorry, try again.
Sorry, try again.
sudo: 3 incorrect password attempts
保存在/tmp/pass.txt中的用户和密码是正确的。
为什么pam_unix不接受我的模块传递的密码?
感谢。
答案 0 :(得分:1)
pam_unix接受模块传递的密码,但问题是您使用:
auth required pam_unix.so
在此模块成功完成后,pam将在下一行调用该模块。 pam_deny.so是一个为每次调用返回失败的模块。 如果模块返回成功,则可以指定必须跳过下一行。你可以用这个来做到这一点:
auth [success=1 default=ignore] pam_unix.so try_first_pass nullok_secure debug
在这种情况下,如果模块返回成功,它将跳过下一个“1”行。
使用它来解决问题:
auth sufficient libtest-pam-auth-module.so
auth [success=1 default=ignore] pam_unix.so try_first_pass nullok_secure debug
auth requisite pam_deny.so
auth required pam_permit.so
auth optional pam_cap.so
答案 1 :(得分:0)
如果验证成功,则应返回PAM_SUCCESS,否则返回PAM_AUTH_ERR