2013-06-03 96 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