简单的 PAM 示例
我想使用 PAM 开发一个身份验证模块,但我无法让一个简单的示例正常工作。
对于初学者来说,我想做一个简单的 SSH 登录系统,如果用户输入用户名后门
,那么用户将无需密码即可登录(就像在 TRON Legacy 中一样)。
我尝试使用本指南作为模板,但我无法让它工作。这是到目前为止我的代码:
PAM_EXTERN int pam_sm_setcred( pam_handle_t *pamh, int flags, int argc, const char **argv ) {
return PAM_SUCCESS ;
}
PAM_EXTERN int pam_sm_authenticate( pam_handle_t *pamh, int flags,int argc, const char **argv ) {
int retval;
printf("I'm here");
const char* pUsername;
retval = pam_get_user(pamh, &pUsername, "Username: ");
if (retval != PAM_SUCCESS) {
return retval;
}
if (strcmp(pUsername, "backdoor") != 0) {
return PAM_AUTH_ERR;
}
return PAM_SUCCESS;
}
当我使用名称 backdoor
登录时,我的权限被拒绝。我已尝试创建用户帐户,但仍然提示输入密码。
当我使用有效用户登录时,我会看到“我在这里”的打印输出。有没有更好的方法来调试这样的东西,或者主要是反复试验?
编辑:
我在 @include common-auth 之后将其添加到我的 /etc/pam.d/sshd
中:
auth enough mypam.so
这是在其他 2 个 .so 文件之后,但我很确定它每次都会被执行。
我没有修改pam.conf(那里什么也没有)。我认为从 SSH 开始是最简单的,因为我不必每次都注销。
编辑:
我终于让它工作了。结果如下:
https://github.com/beatgammit/simple-pam
它是开源的,所以有兴趣的话就来看看吧!
I want to develop an authentication module using PAM, but I'm having trouble getting a simple example working.
For starters, I would like to do a simple SSH login system where if the user enters the username backdoor
, then the user will be logged in without a password (just like in TRON Legacy).
I tried using this guide as a template, but I can't get it to work. Here is my code so far:
PAM_EXTERN int pam_sm_setcred( pam_handle_t *pamh, int flags, int argc, const char **argv ) {
return PAM_SUCCESS ;
}
PAM_EXTERN int pam_sm_authenticate( pam_handle_t *pamh, int flags,int argc, const char **argv ) {
int retval;
printf("I'm here");
const char* pUsername;
retval = pam_get_user(pamh, &pUsername, "Username: ");
if (retval != PAM_SUCCESS) {
return retval;
}
if (strcmp(pUsername, "backdoor") != 0) {
return PAM_AUTH_ERR;
}
return PAM_SUCCESS;
}
When I log in with the name backdoor
, I get permission denied. I've tried creating the user account, but I still get prompted for the password.
When I log in with a valid user, I see the "I'm here" printout. Is there a better way to debug something like this or is it mostly trial and error?
EDIT:
I added this to my /etc/pam.d/sshd
after @include common-auth:
auth sufficient mypam.so
This comes after 2 other .so files, but I'm pretty sure it's getting executed every time.
I have not modified pam.conf (there isn't anything there). I figured that starting with SSH would be easiest because I don't have to log out each time.
EDIT:
I finally got it working. Here's the result:
https://github.com/beatgammit/simple-pam
It's open-source, so if you're interested, take a look!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,如果之前所需的模块失败了,sufficient 仍然会失败。既然您说您已将足够的行放在 common-auth 的包含之下,您可能会看到失败,因为 common-auth 中的某些必需模块已拒绝访问。另外,你还有 sshd 妨碍。
我会把所有这些东西都放在一边,这样你就知道你的测试实际上是对你的 pam 模块的测试,而不是与其他东西的进一步交互。我将从一个简单的测试程序开始,例如此处 使用 /etc/pam.d/check_user 列出您的模块而不是 pam_unix。
First off, sufficient will still fail if a previous required module has failed. Since you say you have put your sufficient line beneath the include of common-auth you may be seeing a failure because some required module in common-auth has denied access already. Plus you have have sshd getting in the way.
I'd get all this stuff out of the way so you know your test is really a test of your pam module and not some further interaction with other things. I'd start with a simple test program like the one here with /etc/pam.d/check_user listing your module instead of pam_unix.