
    Bi              
          d Z dgZdZdZdZddlZddlmZmZm	Z	m
Z
mZmZmZ ddlmZmZmZmZmZ dd	lmZ dd
lmZ  G d de	      Z G d de	      Z G d de	      Z e
ee e ee             e ee            e      Z G d de	      ZdZdZdZdZdZ e ed            Z  e ed            Z!e jD                  Z"ee"_#        eege"_$         e%e!d      re!jL                  Z&ee&_#        eege&_$        e!jN                  Z'ee'_#        ee ee       ee      ge'_$        e!jP                  Z(ee(_#        eege(_$        e!jR                  Z)ee)_#        eege)_$        e!jT                  Z*ee*_#        eege*_$         G d d      Z+d Z,e-dk(  rxddl.Z.ddl/Z/d Z0 e+       Z+ e0d e/jb                               Z2e+jY                  e2 e/j^                                 e3dji                  e+jj                  e+jl                               yy) z
PAM module for python

Provides an authenticate function that will allow the caller to authenticate
a user against the Pluggable Authentication Modules (PAM) on the system.

Implemented using ctypes, so no compilation is necessary.
pamz1.8.4z David Ford <david@blue-labs.org>z2018 June 15    N)CDLLPOINTER	Structure	CFUNCTYPEcastbyrefsizeof)c_void_pc_size_tc_char_pc_charc_int)memmove)find_libraryc                        e Zd ZdZdefgZd Zy)	PamHandlez&wrapper class for pam_handle_t pointerhandlec                 <    t        j                  |        d| _        y )Nr   )r   __init__r   selfs    ./usr/local/lib/python3.12/dist-packages/pam.pyr   zPamHandle.__init__'   s    4     N)__name__
__module____qualname____doc__r   _fields_r    r   r   r   r   #   s    0H%'Hr   r   c                   &    e Zd ZdZdefdefgZd Zy)
PamMessagez'wrapper class for pam_message structure	msg_stylemsgc                 8    d| j                   | j                  fz  S )Nz<PamMessage %i '%s'>)r#   r$   r   s    r   __repr__zPamMessage.__repr__/   s    %(BBBr   N)r   r   r   r   r   r   r   r&   r    r   r   r"   r"   +   s    1u%x'8:HCr   r"   c                   &    e Zd ZdZdefdefgZd Zy)PamResponsez(wrapper class for pam_response structurerespresp_retcodec                 8    d| j                   | j                  fz  S )Nz<PamResponse %i '%s'>)r*   r)   r   s    r   r&   zPamResponse.__repr__6   s    &$*;*;TYY)GGGr   N)r   r   r   r   r   r   r   r&   r    r   r   r(   r(   2   s    2(#ne%<>HHr   r(   c                        e Zd ZdZdefdefgZy)PamConvz$wrapper class for pam_conv structureconvappdata_ptrN)r   r   r   r   	conv_funcr   r   r    r   r   r-   r-   ;   s    .)$}h&?AHr   r-                  cpam_endc                   "    e Zd ZdZdZd ZddZy)r   r   Nc                      y Nr    r   s    r   r   zpam.__init__g   s    r   c                    t         fd       }t        j                  dk\  rdt        |t              r|j                  |      }t        t              rj                  |      t        |t              ru|j                  |      }nct        |t              r|j                  |      }t        t              rj                  |      t        |t              r|j                  |      }d|v sdv sd|v rd| _        d| _        yt              t               }t        |d      }t        ||t        |      t        |            }	|	dk7  r|	| _        d| _        yt        |d      }	|	dk(  }
|
r|rt        |t               }	|	| _        t#        ||	      | _        t        j                  dk\  r | j                  j%                  |      | _        t'        t(        d	      rt+        ||	       |
S )
a  username and password authentication for the given service.

           Returns True for success, or False for failure.

           self.code (integer) and self.reason (string) are always stored and may
           be referenced for the reason why authentication failed. 0/'Success' will
           be stored for success.

           Python3 expects bytes() for ctypes inputs.  This function will make
           necessary conversions using the supplied encoding.

        Inputs:
          username: username to authenticate
          password: password in plain text
          service:  PAM service to authenticate against, defaults to 'login'

        Returns:
          success:  True
          failure:  False
        c                 t   t        | t        t                    }t        |t	        t                    }||d<   t        |       D ]r  }||   j                  j                  t        k(  s$t        t        	      dz   t        t                    }t        |t        	             |||   _        d||   _        t y)zxSimple conversation function that responds to any
               prompt where the echo is off with the supplied passwordr   r1   )callocr
   r(   r   r   rangecontentsr#   PAM_PROMPT_ECHO_OFFlenr   r   r)   r*   )

n_messagesmessages
p_responseapp_dataaddrresponseidst	cpasswordpasswords
           r   my_convz!pam.authenticate.<locals>.my_conv   s    
 *f[&9:DD'+"67H$JqM:& 1A;''115HH Xq&.ACCCM:'*HQK$/0HQK,1 r   r3       r4   zstrings may not contain NULFr   zpam_start() failedr7   )r0   sysversion_info
isinstancestrencodeunicodecodereasonr   r   r-   	pam_startr	   pam_authenticatepam_setcredPAM_REINITIALIZE_CREDpam_strerrordecodehasattrlibpamr7   )r   usernamerK   serviceencoding
resetcredsrL   r   r.   retvalauth_successrJ   s     `        @r   authenticatezpam.authenticatej   s   , 
	 
	  t#(C(X__X5N((C(X__X5N('3'W^^H5M'(G,#??84(G,#??84'7+">>(3h'X"5G9KDI7DK X&	!$7HeDk5=IQ; DI.DK!&!,{J )>?F 	"662t#++,,X6DK69%FF#r   )loginzutf-8T)r   r   r   rU   rV   r   re   r    r   r   r   r   c   s    DFUr   c                  6     t               j                  | i |S )zB
    Compatibility function for older versions of python-pam.
    )r   re   )vargsdargss     r   re   re      s     35u...r   __main__c                     fd}t        j                  |       t        j                  dk\  rt	        |       }nt        |       }t        j                          |S )Nc                  X    t        j                          t        j                          y r:   )readlineinsert_text	redisplay)texts   r   hookz input_with_prefill.<locals>.hook   s      & r   rM   )rm   set_pre_input_hookrO   rP   input	raw_input)promptrp   rq   results    `  r   input_with_prefillrw      sJ    	! 	##D)t#6]Fv&F##%r   z
Username: z{} {})7r   __all____version__
__author____released__rO   ctypesr   r   r   r   r   r	   r
   r   r   r   r   r   r   ctypes.utilr   r   r"   r(   r0   r-   r@   PAM_PROMPT_ECHO_ONPAM_ERROR_MSGPAM_TEXT_INFOrZ   libcr^   r=   restypeargtypesr]   r7   rW   rY   r[   rX   r   re   r   rm   getpassrw   getuserr_   printformatrU   rV   r    r   r   <module>r      s)   w1
 
 K K K > >  $	 C CH) H eUGGJ,?$@''R]J^B_aij	Bi B
     c!23 e!45 KK$%x0 69 &G %GO!*E 2G",,	!	 %x1A79CUV	 "..! &. "//$ &. "33 !  &.  \ \~/ z %C!,0ABH Xw01	'..3::
.// r   