python - 在与 mod_python 一起运行的脚本中使用 pam_python

标签 python linux mod-python pam

我想开发一个网络界面,让 Linux 系统的用户可以执行与他们的帐户相关的某些任务。我决定在 Apache 上使用 Python 和 mod_python 编写网站的后端。为了对用户进行身份验证,我想我可以使用 python_pam查询 PAM 服务。我改编了 example与模块捆绑在一起并得到这个:

# out is the output stream used to print debug
def auth(username, password, out):
    def pam_conv(aut, query_list, user_data):
        out.write("Query list: " + str(query_list) + "\n")

        # List to store the responses to the different queries
        resp = []

        for item in query_list:
            query, qtype = item

            # If PAM asks for an input, give the password
            if qtype == PAM.PAM_PROMPT_ECHO_ON or qtype == PAM.PAM_PROMPT_ECHO_OFF:
                resp.append((str(password), 0))

            elif qtype == PAM.PAM_PROMPT_ERROR_MSG or qtype == PAM.PAM_PROMPT_TEXT_INFO:
                resp.append(('', 0))

        out.write("Our response: " + str(resp) + "\n")
        return resp

    # If username of password is undefined, fail
    if username is None or password is None:
        return False

    service = 'login'
    pam_ = PAM.pam()
    pam_.start(service)

    # Set the username
    pam_.set_item(PAM.PAM_USER, str(username))

    # Set the conversation callback
    pam_.set_item(PAM.PAM_CONV, pam_conv)

    try:
        pam_.authenticate()
        pam_.acct_mgmt()
    except PAM.error, resp:
        out.write("Error: " + str(resp) + "\n")
        return False
    except:
        return False

    # If we get here, the authentication worked
    return True 

我的问题是,无论是在简单脚本中还是通过 mod_python 使用该函数,它的行为都不一样。为了说明这一点,我写了这些简单的案例:

my_username = "markys"
my_good_password = "lalala"
my_bad_password = "lololo"

def handler(req):
 req.content_type = "text/plain"
 req.write("1- " + str(auth(my_username,my_good_password,req) + "\n"))
 req.write("2- " + str(auth(my_username,my_bad_password,req) + "\n"))
 return apache.OK

if __name__ == "__main__":
 print "1- " + str(auth(my_username,my_good_password,sys.__stdout__))
 print "2- " + str(auth(my_username,my_bad_password,sys.__stdout__))

脚本的结果是:

Query list: [('Password: ', 1)]
Our response: [('lalala', 0)]
1- True
Query list: [('Password: ', 1)]
Our response: [('lololo', 0)]
Error: ('Authentication failure', 7)
2- False

但是 mod_python 的结果是:

Query list: [('Password: ', 1)]
Our response: [('lalala', 0)]
Error: ('Authentication failure', 7)
1- False
Query list: [('Password: ', 1)]
Our response: [('lololo', 0)]
Error: ('Authentication failure', 7)
2- False

我不明白为什么 auth 函数在给定相同输入的情况下不返回相同的值。知道我在哪里弄错了吗? Here是原剧本,希望对你有帮助。

非常感谢!

编辑:好的,我发现了错误。我以 root 身份运行脚本。 mod_python 以网络服务器的用户身份运行脚本。只有 root 有读取 shadow 的权限。我不确定我将如何规避这个问题,但至少现在我知道问题出在哪里了!

最佳答案

看来您必须启用 Apache 才能使用 PAM 身份验证。看看这个网站:http://www.debianhelp.co.uk/apachepam.htm

您可能也想看看这个网站:http://inming.net/?p=86

关于python - 在与 mod_python 一起运行的脚本中使用 pam_python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2567705/

相关文章:

python - 计算特定行的平均值

python - 如何从记事本文件中获取数据并使用 fft(对其进行快速傅立叶变换)

python - 按月对分组

linux - 运行 run.sh 时出错(linux)

jquery - 从 mod_python 返回 xml 时 jQuery 中出现解析错误

python - 使用 Plone 发布地理空间数据

linux - 检查目录是否不存在

python - 使用带有 Mod_Python 和多个 Python 安装的 Apache 在 Virtualenv 中运行 Django

php - Python 服务器页面实现

linux - node.js:找不到模块 'request'