Guest User

authtest.py

a guest
Feb 7th, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.87 KB | None | 0 0
  1. #/usr/bin/env ruby
  2. # (c) 2007 Chris AtLee <chris@atlee.ca>
  3. # Licensed under the MIT license:
  4. # http://www.opensource.org/licenses/mit-license.php
  5. """
  6. PAM module for python
  7.  
  8. Provides an authenticate function that will allow the caller to authenticate
  9. a user against the Pluggable Authentication Modules (PAM) on the system.
  10.  
  11. Implemented using ctypes, so no compilation is necessary.
  12. """
  13.  
  14. __all__ = ['authenticate']
  15.  
  16. from ctypes import CDLL, POINTER, Structure, CFUNCTYPE, cast, pointer, sizeof
  17. from ctypes import c_void_p, c_uint, c_char_p, c_char, c_int
  18. from ctypes.util import find_library
  19.  
  20. LIBPAM = CDLL(find_library("pam"))
  21. LIBC = CDLL(find_library("c"))
  22.  
  23. CALLOC = LIBC.calloc
  24. CALLOC.restype = c_void_p
  25. CALLOC.argtypes = [c_uint, c_uint]
  26.  
  27. STRDUP = LIBC.strdup
  28. STRDUP.argstypes = [c_char_p]
  29. STRDUP.restype = POINTER(c_char)  # NOT c_char_p !!!!
  30.  
  31. # Various constants
  32. PAM_PROMPT_ECHO_OFF = 1
  33. PAM_PROMPT_ECHO_ON = 2
  34. PAM_ERROR_MSG = 3
  35. PAM_TEXT_INFO = 4
  36.  
  37.  
  38. class PamHandle(Structure):
  39.     """wrapper class for pam_handle_t"""
  40.     _fields_ = [
  41.         ("handle", c_void_p)
  42.     ]
  43.  
  44.     def __init__(self):
  45.         Structure.__init__(self)
  46.         self.handle = 0
  47.  
  48.  
  49. class PamMessage(Structure):
  50.     """wrapper class for pam_message structure"""
  51.     _fields_ = [
  52.         ("msg_style", c_int),
  53.         ("msg", c_char_p),
  54.     ]
  55.  
  56.     def __repr__(self):
  57.         return ""
  58.         #return "<PamMessage %i '%s'>" % (self.msg_style, self.msg)
  59.  
  60.  
  61. class PamResponse(Structure):
  62.     """wrapper class for pam_response structure"""
  63.     _fields_ = [
  64.         ("resp", c_char_p),
  65.         ("resp_retcode", c_int),
  66.     ]
  67.  
  68.     def __repr__(self):
  69.         return "<PamResponse %i '%s'>" % (self.resp_retcode, self.resp)
  70.  
  71. CONV_FUNC = CFUNCTYPE(c_int,
  72.                       c_int, POINTER(POINTER(PamMessage)),
  73.                       POINTER(POINTER(PamResponse)), c_void_p)
  74.  
  75.  
  76. class PamConv(Structure):
  77.     """wrapper class for pam_conv structure"""
  78.     _fields_ = [
  79.         ("conv", CONV_FUNC),
  80.         ("appdata_ptr", c_void_p)
  81.     ]
  82.  
  83. PAM_START = LIBPAM.pam_start
  84. PAM_START.restype = c_int
  85. PAM_START.argtypes = [c_char_p, c_char_p, POINTER(PamConv),
  86.                       POINTER(PamHandle)]
  87.  
  88. PAM_AUTHENTICATE = LIBPAM.pam_authenticate
  89. PAM_AUTHENTICATE.restype = c_int
  90. PAM_AUTHENTICATE.argtypes = [PamHandle, c_int]
  91.  
  92.  
  93. def authenticate(username, password, service='login'):
  94.     """Returns True if the given username and password authenticate for the
  95.    given service.  Returns False otherwise
  96.  
  97.    ``username``: the username to authenticate
  98.  
  99.    ``password``: the password in plain text
  100.  
  101.    ``service``: the PAM service to authenticate against.
  102.                 Defaults to 'login'"""
  103.     @CONV_FUNC
  104.     def my_conv(n_messages, messages, p_response, app_data):
  105.         """Simple conversation function that responds to any
  106.        prompt where the echo is off with the supplied password"""
  107.         # Create an array of n_messages response objects
  108.         addr = CALLOC(n_messages, sizeof(PamResponse))
  109.         p_response[0] = cast(addr, POINTER(PamResponse))
  110.         for i in range(n_messages):
  111.             if messages[i].contents.msg_style == PAM_PROMPT_ECHO_OFF:
  112.                 pw_copy = STRDUP(str(password))
  113.                 p_response.contents[i].resp = cast(pw_copy, c_char_p)
  114.                 p_response.contents[i].resp_retcode = 0
  115.         return 0
  116.  
  117.     handle = PamHandle()
  118.     conv = PamConv(my_conv, 0)
  119.     retval = PAM_START(service, username, pointer(conv), pointer(handle))
  120.  
  121.  
  122.  
  123.     retval = PAM_AUTHENTICATE(handle, 0)
  124.    
  125.     if retval != 0:
  126.         sys.exit(1)
  127.     else:
  128.         sys.exit(0)
  129.     #    retval = 1
  130.    
  131.     #return retval #== 0
  132.  
  133. if __name__ == "__main__":
  134.     import getpass
  135.     import sys
  136.    
  137.     username = raw_input("")
  138.     passwort = raw_input("")
  139.  
  140.    
  141.    
  142.     authenticate(username, passwort,service='system-auth')
Add Comment
Please, Sign In to add comment