如何使用没有root权限的PAM验证用户名/密码

Vla*_*rik 4 linux authentication pam

我有用C编写的程序.它使用2个参数用户名/密码,并尝试使用PAM验证此用户.当我是root用户时它工作正常.当我是'普通'用户时,它适用于该用户,但不适用于另一个用户.我认为,这是由于使用了影子密码..

作为我正在使用的服务:

retval = pam_start("test", username, &local_conversation, &local_auth_handle);
Run Code Online (Sandbox Code Playgroud)

我将它添加到/etc/pam.d/test

#%PAM-1.0
auth    required    pam_unix.so shadow nullok
account required    pam_unix.so
session required    pam_unix.so
Run Code Online (Sandbox Code Playgroud)

请问你能帮帮我吗?非常感谢!

Vla*_*rik 7

该应用程序需要能够阅读/etc/shadow.

有关这种方法的一种方法,请参阅我的帖子.

编辑:如果链接中断,请从上面的链接添加帖子

我在C++中编写了身份验证模块,它可以通过Linux中的PAM检查用户名/密码(我正在使用Fedora Linux).我想和你分享,我做了什么:-).那么,我们走了:-)

Prerequisities:

Install package pam-devel
(This step is necessary when you use shadow password) Create new Linux user and group. Set this group as default for this user. Then
Run Code Online (Sandbox Code Playgroud)

按照以下步骤操作:转到/ etc以root身份登录(su)将组更改为新文件阴影组(chgrp new_group shadow)为此组设置'read'privilage(chmod 0440 shadow)

写下这段代码:(authModule.c)view plaincopy to clipboardprint?

#include <stdio.h>  
#include <security/pam_appl.h>  
#include <unistd.h>  
#include <stdlib.h>  
#include <string.h>  

struct pam_response *reply;  

// //function used to get user input  
int function_conversation(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr)  
{  
    *resp = reply;  
        return PAM_SUCCESS;  
}  

int authenticate_system(const char *username, const char *password)   
{  
    const struct pam_conv local_conversation = { function_conversation, NULL };  
    pam_handle_t *local_auth_handle = NULL; // this gets set by pam_start  

    int retval;  
    retval = pam_start("su", username, &local_conversation, &local_auth_handle);  

    if (retval != PAM_SUCCESS)  
    {  
            printf("pam_start returned: %d\n ", retval);  
            return 0;  
    }  

    reply = (struct pam_response *)malloc(sizeof(struct pam_response));  

    reply[0].resp = strdup(password);  
    reply[0].resp_retcode = 0;  
    retval = pam_authenticate(local_auth_handle, 0);  

    if (retval != PAM_SUCCESS)  
    {  
            if (retval == PAM_AUTH_ERR)  
            {  
                    printf("Authentication failure.\n");  
            }  
            else  
            {  
                printf("pam_authenticate returned %d\n", retval);  
            }  
            return 0;  
    }  

    printf("Authenticated.\n");  
    retval = pam_end(local_auth_handle, retval);  

    if (retval != PAM_SUCCESS)  
    {  
            printf("pam_end returned\n");  
            return 0;  
    }  

    return 1;  
}  

int main(int argc, char** argv)  
{  
    char* login;  
    char* password;  

    printf("Authentication module\n");  

    if (argc != 3)  
    {  
        printf("Invalid count of arguments %d.\n", argc);  
        printf("./authModule <username> <password>");  
        return 1;  
    }  

    login = argv[1];  
    password = argv[2];  

    if (authenticate_system(login, password) == 1)  
    {  
        printf("Authenticate with %s - %s through system\n", login, password);  
        return 0;  
    }     

    printf("Authentication failed!\n");  
    return 1;  
}  
Run Code Online (Sandbox Code Playgroud)

编译代码:

gcc -o authModule authModule.c -lpam  
Run Code Online (Sandbox Code Playgroud)

运行代码(作为新用户!):

./authModule user password  
Run Code Online (Sandbox Code Playgroud)

就这样!!:-) 希望能帮助到你!