Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2010
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. #!/usr/bin/python
  2. import sys
  3. import PAM
  4. if __name__ != "__main__":
  5.     from mod_python import apache
  6.    
  7. # out is the output stream used to print debug
  8. def auth(username, password, out):
  9.     def pam_conv(aut, query_list, user_data):
  10.         out.write("Query list: " + str(query_list) + "\n")
  11.        
  12.         # List to store the responses to the different queries
  13.         resp = []
  14.        
  15.         for item in query_list:
  16.             query, qtype = item
  17.            
  18.             # If PAM asks for an input, give the password
  19.             if qtype == PAM.PAM_PROMPT_ECHO_ON or qtype == PAM.PAM_PROMPT_ECHO_OFF:
  20.                 resp.append((str(password), 0))
  21.            
  22.             elif qtype == PAM.PAM_PROMPT_ERROR_MSG or qtype == PAM.PAM_PROMPT_TEXT_INFO:
  23.                 resp.append(('', 0))
  24.        
  25.         out.write("Our response: " + str(resp) + "\n")
  26.         return resp
  27.  
  28.     # If username of password is undefined, fail
  29.     if username is None or password is None:
  30.         return False
  31.  
  32.     service = 'login'
  33.     pam_ = PAM.pam()
  34.     pam_.start(service)
  35.    
  36.     # Set the username
  37.     pam_.set_item(PAM.PAM_USER, str(username))
  38.    
  39.     # Set the conversation callback
  40.     pam_.set_item(PAM.PAM_CONV, pam_conv)
  41.    
  42.     try:
  43.         pam_.authenticate()
  44.         pam_.acct_mgmt()
  45.     except PAM.error, resp:
  46.         out.write("Error: " + str(resp) + "\n")
  47.         return False
  48.     except:
  49.         return False
  50.    
  51.     # If we get here, the authentication worked
  52.     return True
  53.  
  54. my_username = "markys"
  55. my_good_password = "lalala"
  56. my_bad_password = "lololo"
  57.  
  58. def handler(req):
  59.     req.content_type = "text/plain"
  60.     req.write("1- " + str(auth(my_username,my_good_password,req)) + "\n")
  61.     req.write("2- " + str(auth(my_username,my_bad_password,req)) + "\n")
  62.     return apache.OK
  63.    
  64. if __name__ == "__main__":
  65.     print "1- " + str(auth(my_username,my_good_password,sys.__stdout__))
  66.     print "2- " + str(auth(my_username,my_bad_password,sys.__stdout__))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement