2013-06-03 51 views
1

我为Ubuntu创建了一个新的PAM模块。在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 ifconfig 
Sorry, try again. 
Sorry, try again. 
Sorry, try again. 
sudo: 3 incorrect password attempts 

而保存在/tmp/pass.txt中的用户和密码是正确的。

为什么pam_unix不接受我的模块传递的密码?

谢谢。

回答

0

您应该返回PAM_SUCCESS如果认证成功,PAM_AUTH_ERR否则

+0

我需要的所有其他模块执行。 –

0

pam_unix的接受你的模块通过了密码,但问题是,你使用:

auth required pam_unix.so 

后,该模块successed 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