<s> """<STR_LIT>""" <EOL> __all__ = [ '<STR_LIT>' ] <EOL> from ctypes import CDLL , POINTER , Structure , CFUNCTYPE , cast , pointer , sizeof <EOL> from ctypes import c_void_p , c_uint , c_char_p , c_char , c_int <EOL> from ctypes . util import find_library <EOL> LIBPAM = CDLL ( find_library ( "<STR_LIT>" ) ) <EOL> LIBC = CDLL ( find_library ( "<STR_LIT:c>" ) ) <EOL> CALLOC = LIBC . calloc <EOL> CALLOC . restype = c_void_p <EOL> CALLOC . argtypes = [ c_uint , c_uint ] <EOL> STRDUP = LIBC . strdup <EOL> STRDUP . argstypes = [ c_char_p ] <EOL> STRDUP . restype = POINTER ( c_char ) <EOL> PAM_PROMPT_ECHO_OFF = <NUM_LIT:1> <EOL> PAM_PROMPT_ECHO_ON = <NUM_LIT:2> <EOL> PAM_ERROR_MSG = <NUM_LIT:3> <EOL> PAM_TEXT_INFO = <NUM_LIT:4> <EOL> class PamHandle ( Structure ) : <EOL> """<STR_LIT>""" <EOL> _fields_ = [ <EOL> ( "<STR_LIT>" , c_void_p ) <EOL> ] <EOL> def __init__ ( self ) : <EOL> Structure . __init__ ( self ) <EOL> self . handle = <NUM_LIT:0> <EOL> class PamMessage ( Structure ) : <EOL> """<STR_LIT>""" <EOL> _fields_ = [ <EOL> ( "<STR_LIT>" , c_int ) , <EOL> ( "<STR_LIT>" , c_char_p ) , <EOL> ] <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . msg_style , self . msg ) <EOL> class PamResponse ( Structure ) : <EOL> """<STR_LIT>""" <EOL> _fields_ = [ <EOL> ( "<STR_LIT>" , c_char_p ) , <EOL> ( "<STR_LIT>" , c_int ) , <EOL> ] <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . resp_retcode , self . resp ) <EOL> CONV_FUNC = CFUNCTYPE ( c_int , <EOL> c_int , POINTER ( POINTER ( PamMessage ) ) , <EOL> POINTER ( POINTER ( PamResponse ) ) , c_void_p ) <EOL> class PamConv ( Structure ) : <EOL> """<STR_LIT>""" <EOL> _fields_ = [ <EOL> ( "<STR_LIT>" , CONV_FUNC ) , <EOL> ( "<STR_LIT>" , c_void_p ) <EOL> ] <EOL> PAM_START = LIBPAM . pam_start <EOL> PAM_START . restype = c_int <EOL> PAM_START . argtypes = [ c_char_p , c_char_p , POINTER ( PamConv ) , <EOL> POINTER ( PamHandle ) ] <EOL> PAM_AUTHENTICATE = LIBPAM . pam_authenticate <EOL> PAM_AUTHENTICATE . restype = c_int <EOL> PAM_AUTHENTICATE . argtypes = [ PamHandle , c_int ] <EOL> PAM_ACCT_MGMT = LIBPAM . pam_acct_mgmt <EOL> PAM_ACCT_MGMT . restype = c_int <EOL> PAM_ACCT_MGMT . argtypes = [ PamHandle , c_int ] <EOL> PAM_END = LIBPAM . pam_end <EOL> PAM_END . restype = c_int <EOL> PAM_END . argtypes = [ PamHandle , c_int ] <EOL> def authenticate ( username , password , service = '<STR_LIT>' , enable_acct_mgrt = False ) : <EOL> """<STR_LIT>""" <EOL> @ CONV_FUNC <EOL> def my_conv ( n_messages , messages , p_response , app_data ) : <EOL> """<STR_LIT>""" <EOL> addr = CALLOC ( n_messages , sizeof ( PamResponse ) ) <EOL> p_response [ <NUM_LIT:0> ] = cast ( addr , POINTER ( PamResponse ) ) <EOL> for i in range ( n_messages ) : <EOL> if messages [ i ] . contents . msg_style == PAM_PROMPT_ECHO_OFF : <EOL> pw_copy = STRDUP ( str ( password ) ) <EOL> p_response . contents [ i ] . resp = cast ( pw_copy , c_char_p ) <EOL> p_response . contents [ i ] . resp_retcode = <NUM_LIT:0> <EOL> return <NUM_LIT:0> <EOL> handle = PamHandle ( ) <EOL> conv = PamConv ( my_conv , <NUM_LIT:0> ) <EOL> retval = PAM_START ( service , username , pointer ( conv ) , pointer ( handle ) ) <EOL> if retval != <NUM_LIT:0> : <EOL> return False <EOL> retval = PAM_AUTHENTICATE ( handle , <NUM_LIT:0> ) <EOL> if retval == <NUM_LIT:0> and enable_acct_mgrt : <EOL> retval = PAM_ACCT_MGMT ( handle , <NUM_LIT:0> ) <EOL> PAM_END ( handle , retval ) <EOL> return retval == <NUM_LIT:0> <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import getpass <EOL> print authenticate ( getpass . getuser ( ) , getpass . getpass ( ) ) </s>
