python中没有根权限的pam身份验证

2024-04-24 21:34:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找一种让python程序通过pam处理身份验证的方法。 我正在使用http://code.google.com/p/web2py/source/browse/gluon/contrib/pam.py来实现这一点,只要我的python程序以根用户的身份运行,这一点就很好了,这在我看来并不理想。

如何使用pam进行用户名/密码验证而不需要根权限?


Tags: 方法用户py程序com身份验证httpsource
3条回答

最后我使用了pexpect并尝试su-username。 有点慢,但效果很好。 下面的例子还没有完善,但你会明白的。

干杯

杰伊

#!/usr/bin/python
import pexpect
def pam(username, password):
        '''Accepts username and password and tried to use PAM for authentication'''
        try:
                child = pexpect.spawn('/bin/su - %s'%(username))
                child.expect('Password:')
                child.sendline(password)
                result=child.expect(['su: Authentication failure',username])
                child.close()
        except Exception as err:
                child.close()
                print ("Error authenticating. Reason: "%(err))
                return False
        if result == 0:
                print ("Authentication failed for user %s."%(username))
                return False
        else:
                print ("Authentication succeeded for user %s."%(username))
                return True

if __name__ == '__main__':
        print pam(username='default',password='chandgeme')

简短:使用正确的Python PAM实现,正确设置PAM。

long:在正常的PAM设置中,您不需要root特权。最后,这是PAM提供的一个功能,特权分离。

pam_unix有办法为您检查密码。似乎PAM实现web2py(注意,它来自某个contrib子目录…)没有做正确的事情。可能您的PAM设置不正确,如果没有进一步的信息很难判断;这在很大程度上还取决于操作系统和风格/分布。

有多个针对Python的PAM绑定(不幸的是,标准库中没有任何绑定),请改用它们。对于配置,有很多教程,请为您的系统找到合适的教程。

旧的/错误的,不要这样做:你不需要是根用户,你只需要能够阅读/etc/shadow。此文件通常具有具有只读访问权限的shadow组。所以您只需要添加运行PAM的用户,签入shadow

groupadd <user> shadow应该能做到。

我认为pam模块是您的最佳选择,但您不必将其直接嵌入到程序中。您可以编写一个简单的服务,该服务绑定到本地主机上的端口,或侦听UNIX域套接字,并为同一主机上的其他进程填充PAM请求。然后让web2py应用程序连接到它进行用户/密码验证。

例如:

import asyncore
import pam
import socket

class Client(asyncore.dispatcher_with_send):

    def __init__(self, sock):
        asyncore.dispatcher_with_send.__init__(self, sock)
        self._buf = ''

    def handle_read(self):
        data = self._buf + self.recv(1024)
        if not data:
            self.close()
            return
        reqs, data = data.rsplit('\r\n', 1)
        self._buf = data
        for req in reqs.split('\r\n'):
            try:
                user, passwd = req.split()
            except:
                self.send('bad\r\n')
            else:
                if pam.authenticate(user, passwd):
                    self.send('ok\r\n')
                else:
                    self.send('fail\r\n')

    def handle_close(self):
        self.close()


class Service(asyncore.dispatcher_with_send):

    def __init__(self, addr):
        asyncore.dispatcher_with_send.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.set_reuse_addr()
        self.bind(addr)
        self.listen(1)

    def handle_accept(self):
        conn, _ = self.accept()
        Client(conn)

def main():
    addr = ('localhost', 8317)
    Service(addr)
    try:
        asyncore.loop()
    except KeyboardInterrupt:
        pass

if __name__ == '__main__':
    main()

用法:

% telnet localhost 8317
bob abc123
ok
larry badpass
fail
incomplete
bad

相关问题 更多 >