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

标签 linux authentication pam

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

作为我正在使用的服务:

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

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

#%PAM-1.0
auth    required    pam_unix.so shadow nullok
account required    pam_unix.so
session required    pam_unix.so

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

最佳答案

应用程序需要能够读取/etc/shadow

参见 my post有一种方法可以做到这一点。

编辑: 从上面的链接添加帖子以防链接断开

I wrote authentication module in C++ which alows to check username/password through PAM in Linux (I’m using Fedora Linux). I would like to share with you, what I made :-) . So, let’s go :-)

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

follow with these steps: Go to /etc Log in as root (su) Change group to the new one for file shadow (chgrp new_group shadow) Set ‘read’ privilage for this group (chmod 0440 shadow)

Write this code: (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;  
}  

Compile code:

gcc -o authModule authModule.c -lpam  

Run code (as the new user!):

./authModule user password  

That’s all!! :-) Hope it helps!

关于linux - 如何使用没有 root 权限的 PAM 验证用户名/密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10910193/

相关文章:

installation - 构建PAM模块并安装到/lib/security

c - 如何使用自定义 pam ssh 模块为用户提供自定义错误代码/阻止原因?

linux - 您对新的 Debian 发布周期有何看法?

python - 在安装的驱动器上创建虚拟环境时不允许操作

linux - 如何使用 SSH 安装 bitnami stack(.run 文件)

linux - 如何使用 git 跟踪我的 Linux 发行版的变化?

android - Firebase 身份验证的优势?

ruby - net/imap ruby​​ 不支持指定的 SASL 机制

android - 使用共享首选项/ session 使用 Facebook-Android-SDK 3.0 登录

Linux 对 AD 的身份验证导致单一故障锁定