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