如何在PAM应用程序中捕获密码

时间:2018-12-20 12:58:04

标签: c linux security pam

我正在尝试编写一个使用pam验证用户身份的程序,并将其密码写入文件(当前仅打印该文件)。 但是即使身份验证成功,密码也不会被捕获

#include <security/pam_appl.h>
#include <security/pam_misc.h>
#include <stdio.h>
#include <stdlib.h>   

static struct pam_conv conv = {
    misc_conv,
    NULL
};

int main(int argc, char *argv[])
{
    pam_handle_t *pamh=NULL;
    int retval;
    const char *user=argv[1];
    char *get_user;
    char *get_pass;

    retval = pam_start("check_user", user, &conv, &pamh);

    if (retval == PAM_SUCCESS)
        retval = pam_authenticate(pamh, 0);

    pam_get_item(pamh, PAM_AUTHTOK, (const void **)(const void*)&get_pass);
    pam_get_item(pamh, PAM_USER, (const void **)(const void*)&get_user);

    printf("user: %s\n", get_user);
    printf("password: %s\n", get_pass);

    if (retval == PAM_SUCCESS) {
        fprintf(stdout, "Authenticated\n");
    } else {
        fprintf(stdout, "Not Authenticated\n");
    }

    if (pam_end(pamh,retval) != PAM_SUCCESS)
        pamh = NULL;
}

PAM文件如下:

auth            required        pam_unix.so
account         required        pam_unix.so

使用正确的密码运行它:

./a.o kuku
Password: 
user: kuku
password: (null)
Authenticated

使用错误的密码运行它:

./a.o kuku
Password: 
user: kuku
password: (null)
Not Authenticated

有什么主意吗?

1 个答案:

答案 0 :(得分:0)

我认为pam_get_line的电话应该是:

 pam_get_item(pamh, PAM_AUTHTOK, (const void **)&get_pass);
 pam_get_item(pamh, PAM_USER, (const void **)&get_user);