在与mod_python一起运行的脚本中使用pam_python
我想开发一个web界面,允许Linux系统的用户执行与其帐户相关的某些任务。我决定在Apache上使用Python和mod_Python编写站点的后端。为了对用户进行身份验证,我想我可以使用它来查询PAM服务。我调整了与模块捆绑在一起的模块,得到了以下结果:在与mod_python一起运行的脚本中使用pam_python,python,linux,mod-python,pam,Python,Linux,Mod Python,Pam,我想开发一个web界面,允许Linux系统的用户执行与其帐户相关的某些任务。我决定在Apache上使用Python和mod_Python编写站点的后端。为了对用户进行身份验证,我想我可以使用它来查询PAM服务。我调整了与模块捆绑在一起的模块,得到了以下结果: # out is the output stream used to print debug def auth(username, password, out): def pam_conv(aut, query_list, user
# 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函数在输入相同的情况下不返回相同的值。知道我哪里弄错了吗?是原始脚本,如果这对你有帮助的话
非常感谢
编辑:好的,我发现了错误。我以root用户身份运行脚本。mod_python作为web服务器的用户运行脚本。只有root有权读取shadow。我不知道我将如何绕过这一点,但至少现在我知道了问题所在 似乎必须启用Apache才能使用PAM身份验证。看看这个网站:
您可能也想看看这个站点:看起来您必须启用Apache来使用PAM身份验证。看看这个网站:
您可能也想看看这个站点:我认为这个模块允许您在弹出的登录框中使用HTTP身份验证,并使用PAM验证凭据,并且不会干扰Python。虽然我仍然尝试过,但效果并没有更好。我认为这个模块允许用户使用HTTP身份验证弹出式登录框,并使用PAM验证凭据,并且不会干扰Python。虽然我还是试过了,但效果没有更好。