
    AahxT                        d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
mZmZ ddlmZ ddlmZmZ dd	lmZ e	rdd
lmZ 	 ddlZn$# e$ r dZej        dk    r ed          dY nw xY w	 ddlZddlZn# e$ r dZdZY nw xY wddlmZ ej        dk    rdndZ G d de          Z G d de          Z G d de          ZdS )zKerberos Authentication Plugin.    N)abstractmethod)Path)TYPE_CHECKINGAnyOptionalTuple   )
ERR_STATUS)InterfaceErrorProgrammingError)logger)MySQLSocketntzwModule gssapi is required for GSSAPI authentication mechanism but was not found. Unable to authenticate with the server   )MySQLAuthPluginMySQLSSPIKerberosAuthPluginMySQLKerberosAuthPluginc                       e Zd ZdZedefd            Zedefd            Ze	de
e         dee
e         ef         fd            Zddd	ed
edefdZdS )MySQLBaseKerberosAuthPluginz8Base class for the MySQL Kerberos authentication plugin.returnc                     dS )zPlugin official name.authentication_kerberos_client selfs    h/var/www/lms/venv/lib/python3.11/site-packages/mysql/connector/plugins/authentication_kerberos_client.pynamez MySQLBaseKerberosAuthPlugin.nameL   s
     0/    c                     dS )z'Signals whether or not SSL is required.Fr   r   s    r   requires_sslz(MySQLBaseKerberosAuthPlugin.requires_sslQ   s	     ur   tgt_auth_challengec                     dS )!  Continue with the Kerberos TGT service request.

        With the TGT authentication service given response generate a TGT
        service request. This method must be invoked sequentially (in a loop)
        until the security context is completed and an empty response needs to
        be send to acknowledge the server.

        Args:
            tgt_auth_challenge: the challenge for the negotiation.

        Returns:
            tuple (bytearray TGS service request,
            bool True if context is completed otherwise False).
        Nr   )r   r!   s     r   auth_continuez)MySQLBaseKerberosAuthPlugin.auth_continueV   s      r   sockr   	auth_datakwargsc                    t          j        d|            | j        |fddi|}|t          d          t          j        d|t	          |                     |                    |           |                                }t          j        d|           |t          k    rd}t          j        d	           t          j        d
|d|dz                       t          j        dt	          |                     t          j        d           d}d}|s|dk     rt          j        dd|dz   d           t          j        d|           t          j        d|d|dz                       |                     ||d                   \  }	}|	r|                    |	           |rn6|                                }t          j        d|	           |dz  }|s|dk     |st          d| d|           t          j        d|t	          |                     |                                }t          j        d|           t          |          S )aS  Handles server's `auth switch request` response.

        Args:
            sock: Pointer to the socket connection.
            auth_data: Plugin provided data (extracted from a packet
                       representing an `auth switch request` response).
            kwargs: Custom configuration to be passed to the auth plugin
                    when invoked. The parameters defined here will override the ones
                    defined in the auth plugin itself.

        Returns:
            packet: Last server's response after back-and-forth
                    communication.
        z# auth_data: %signore_auth_dataFNzGot a NULL auth responsez# request: %s size: %sz# server response packet: %s   z%# Continue with GSSAPI authenticationz# Response header: %sr   z# Response size: %sz# Negotiate a service requestr   z%s Attempt %s %sz--------------------z<< Server response: %sz# Response code: %sz>> Response to server: %sz'Unable to fulfill server request after z! attempts. Last server response: z(Last response from server: %s length: %dz<< Ok packet from server: %s)
r   debugauth_responser   lensendrecvr
   r$   bytes)
r   r%   r&   r'   responsepacket
rcode_sizecompletetriestokens
             r   auth_switch_responsez0MySQLBaseKerberosAuthPlugin.auth_switch_responsei   s   " 	&	222%4%iRR%R6RR !;<<<-xXGGG		(3V<<<ZJL@AAAL0&9I:>9I2JKKKL.F<<<L8999HE 5199/519hOOO5v>>>2F;KZ!^;K4LMMM"&"4"4VJKK5H"I"Ix %IIe$$$ 8%@@@
  5199  $@e @ @7=@ @  
 L:F   YY[[FL7@@@V}}r   N)__name__
__module____qualname____doc__propertystrr   boolr    r   r   r0   r   r$   r   r7   r   r   r   r   r   I   s        BB0c 0 0 0 X0 d    X "*5/	x$	%   ^$C!C.3C?BC	C C C C C Cr   r   c                   F   e Zd ZU dZdZeej                 ed<   e	de
fd            Ze	defd            Zde
dej        j        j        fdZe	d	edee
e
f         fd
            Z	 ddee         dedee         fdZdee         deee         ef         fdZdedefdZdS )r   z3Implement the MySQL Kerberos authentication plugin.Ncontextr   c                  "   	 t          j        d          } t          | j                  }|                    d          dk    r|                    dd          \  }}|S # t           j        j        j        $ r t          j
                    cY S w xY w)z(Get user from credentials without realm.initiateusage@r   )gssapiCredentialsr=   r   findsplitrawmiscGSSErrorgetpassgetuser)credsuser_s      r   get_user_from_credentialsz1MySQLKerberosAuthPlugin.get_user_from_credentials   s    	%&Z888Euz??Dyy~~##**S!,,aKz' 	% 	% 	%?$$$$$	%s   AA ,BBc                  R   t           j                            dt           j        dk    rdt          j                     n!t          d                              d                    } | st          d          t          j	        d|            dd	|  
                    d
          i}|S )zGet a credentials store dictionary.

        Returns:
            dict: Credentials store dictionary with the krb5 ccache name.

        Raises:
            InterfaceError: If 'KRB5CCNAME' environment variable is empty.
        
KRB5CCNAMEposixz/tmp/krb5cc_z%TEMP%krb5ccz5The 'KRB5CCNAME' environment variable is set to emptyzUsing krb5 ccache name: FILE:%ss   ccachezFILE:utf-8)osenvirongetr   getuidr   joinpathr   r   r+   encode)
krb5ccnamestores     r   	get_storez!MySQLKerberosAuthPlugin.get_store   s     Z^^ 7g%% -ry{{,,,(^^,,X66
 

  	 G   	6
CCC0J0077@@Ar   upnc                    t          j        d           t          j        |t          j        j                  }| j                            d          }	 t          j        	                    ||d          }|j
        }t          j                            |                                 |t          j        j        dd           n4# t          j        j        j        $ r}t#          d|           |d}~ww xY w|S )	zAcquire and store credentials through provided password.

        Args:
            upn (str): User Principal Name.

        Returns:
            gssapi.raw.creds.Creds: GSSAPI credentials.
        z8Attempt to acquire credentials through provided passwordrX   rB   rC   T)rP   mech	overwriteset_defaultz7Unable to acquire credentials with the given password: N)r   r+   rG   NameNameTyperQ   	_passwordr^   rK   acquire_cred_with_passwordrP   store_cred_intora   MechTypekerberosrL   rM   r   )r   rb   rQ   passwordacquire_cred_resultrP   errs          r   _acquire_cred_with_passwordz3MySQLKerberosAuthPlugin._acquire_cred_with_password   s    	OPPP{3 455>((11	"(*"G"Ghj #H # # (-EJ&&  _-  '     z' 	 	 	"O#OO 	 s   A.C C4C//C4r2   c                    t          j        d| dd                   d         }| dd         } t          j        d| d| d|                   d         }| |d         } t          j        d| dd                   d         }t          j        d| d| dd                   d         }|                                |                                fS aY  Parse authentication data.

        Get the SPN and REALM from the authentication data packet.

        Format:
            SPN string length two bytes <B1> <B2> +
            SPN string +
            UPN realm string length two bytes <B1> <B2> +
            UPN realm string

        Returns:
            tuple: With 'spn' and 'realm'.
        z<HNr	   r   <sstructunpackdecoder2   spn_lenspn	realm_lenrealms        r   _parse_auth_dataz(MySQLKerberosAuthPlugin._parse_auth_data        -fRaRj11!4mNNNNF8G8,<==a@!M$rr
33A6	.)...qrr
;;A>zz||U\\^^++r   r&   r'   c                    d}d}|rZ|                     dd          sD	 |                     |          \  }}n*# t          j        $ r}t	          d|           |d}~ww xY w|| j                                        dz   S | j        r| j         d| nd}t          j	        d|           t          j	        d|           	 t          j        d	
          }t          |j                  }t          j	        d           t          j	        d|           |                    d          dk    r|                    dd          \  }	}
n|}	d}
| j        r| j         d| n|}| j        r;| j        |	k    r0t          j	        d           | j        |                     |          }|
r"|
|k    r| j        |                     |          }n# t          j        j        j        $ r<}|r| j        |                     |          }nt)          d|           |Y d}~n[d}~wt          j        j        j        $ r<}|r| j        |                     |          }nt)          d|           |Y d}~nd}~ww xY wt          j        j        t          j        j        t          j        j        f}t          j        |t          j        j                  }|                    t          j        j                   }t          j!        ||tE          |          d	          | _#        	 | j#        $                                }n4# t          j        j        j        $ r}t)          d|           |d}~ww xY wt          j	        d|           |S )z(Prepare the first message to the server.Nr)   TInvalid authentication data:     rE   Service Principal: %s	Realm: %srB   rC   zCached credentials foundzCached credentials UPN: %srF   r   zBThe user from cached credentials doesn't match with the given userzCredentials has expired: z-Unable to retrieve cached credentials error: )	name_type)r   rP   flagsrD   %Unable to initiate security context: Initial client token: %s)%r[   r   rw   errorInterruptedErrorri   r^   	_usernamer   r+   rG   rH   r=   r   rI   rJ   rq   rK   
exceptionsExpiredCredentialsErrorr   rL   rM   RequirementFlagmutual_authenticationextended_errordelegate_to_peerrg   rh   kerberos_principalcanonicalizerl   rm   SecurityContextsumr@   step)r   r&   r'   r|   r~   rp   rb   rP   	creds_upn
creds_usercreds_realmr   r   cnameinitial_client_tokens                  r   r,   z%MySQLKerberosAuthPlugin.auth_response  s     	WVZZ(:DAA 	WW!229==
UU< W W W&'Ls'L'LMMSVVW ;>((**W44-1^E))%))),c222[%(((&	+*===EEJIL3444L5yAAA ~~c""b((*3//#q*A*A'
KK&
"15NT^--e---YC ~ B$.J">">!   >- <<SAAE >{e338R88==z$< 	Q 	Q 	Q Qt~188==$%F%F%FGGSP  z' 	 	 	 t~188==$ICII  	 "8"1"3

 {3&/*LMMM!!&/":;;-e3u::Z
 
 
	Y48L4E4E4G4G  z' 	Y 	Y 	Y !N!N!NOOUXX	Y 	/1EFFF##sR   7 AAA?DG I,2HI,02I''I,L/ /M MM r!   c                     t          j        d|           | j                            |          }t          j        d|           t          j        d| j        j                   || j        j        fS )r#   tgt_auth challenge: %szContext step response: %sContext completed?: %s)r   r+   r@   r   r4   )r   r!   resps      r   r$   z%MySQLKerberosAuthPlugin.auth_continued  si    " 	-/ABBB $ 1 12D E E0$777-t|/DEEET\***r   messagec                    | j         j        st          d          t          j        d|           t          j        d| j         j                   	 | j                             |          }t          j        d|           nI# t          j        j	        j
        $ r-}t          j        d|           t          d|           |d}~ww xY wt          j        d|           t          d	          }t          j        d
|           | j                             |d          }t          j        d|d         t          |d                              |j        S )a_  Accept handshake and generate closing handshake message for server.

        This method verifies the server authenticity from the given message
        and included signature and generates the closing handshake for the
        server.

        When this method is invoked the security context is already established
        and the client and server can send GSSAPI formated secure messages.

        To finish the authentication handshake the server sends a message
        with the security layer availability and the maximum buffer size.

        Since the connector only uses the GSSAPI authentication mechanism to
        authenticate the user with the server, the server will verify clients
        message signature and terminate the GSSAPI authentication and send two
        messages; an authentication acceptance b'  ' and a
        OK packet (that must be received after sent the returned message from
        this method).

        Args:
            message: a wrapped gssapi message from the server.

        Returns:
            bytearray (closing handshake message to be send to the server).
        z!Security context is not completedzServer message: %szGSSAPI flags in use: %szUnwraped: %sz#Unable to unwrap server message: %sz!Unable to unwrap server message: NzUnwrapped server message: %ss      zMessage response: %sF)encryptz(Wrapped message response: %s, length: %dr   )r@   r4   r   r   r+   actual_flagsunwraprG   rK   r   BadMICErrorr   	bytearraywrapr-   r   )r   r   unwrapedrp   r1   wrapeds         r   auth_accept_close_handshakez3MySQLKerberosAuthPlugin.auth_accept_close_handshake~  s[   4 |$ 	H"#FGGG)7333.0IJJJ	U|**733HL2222z$0 	U 	U 	UL>DDD !JS!J!JKKQTT	U 	3X>>>
 /00+X666""8U";;61Iq	NN	
 	
 	
 ~s   /B C(CCN)r8   r9   r:   r;   r@   r   rG   r   __annotations__staticmethodr=   rS   dictra   rK   rP   Credsrq   r0   r   r   r   r,   r>   r$   r   r   r   r   r   r      s        ==04GXf,-444	%s 	% 	% 	% \	% t    \2s vz7G7M    > , ,5c? , , , \,4 ,0N$ N$!%N$;>N$	%N$ N$ N$ N$`+"*5/+	x$	%+ + + +445 4U 4 4 4 4 4 4r   c                       e Zd ZU dZdZeed<   dZeed<   ede	de
eef         fd            Z	 ddee	         d	edee	         fd
Zdee	         de
ee	         ef         fdZdS )r   zDImplement the MySQL Kerberos authentication plugin with Windows SSPINr@   
clientauthr2   r   c                    t          j        d| dd                   d         }| dd         } t          j        d| d| d|                   d         }| |d         } t          j        d| dd                   d         }t          j        d| d| dd                   d         }|                                |                                fS rs   rv   rz   s        r   r   z,MySQLSSPIKerberosAuthPlugin._parse_auth_data  r   r   r&   r'   c                 R   t          j        d           d}d}|rZ|                    dd          sD	 |                     |          \  }}n*# t          j        $ r}t          d|           |d}~ww xY wt          j        d|           t          j        d|           t          t          t          d          t          j
        t          j        f}| j        r| j        r| j        || j        f}nd}|}t          j        d	|           t          j        d
|du            t          j        d||t          |          t          j                  | _        	 d}	| j                            |	          \  }}
t          j        d|           t          j        d|
           t          j        d| j        j                   |
d         j        }t          j        d| j        j                   n%# t,          $ r}t/          d|           |d}~ww xY wt          j        d|           |S )zPrepare the first message to the server.

        Args:
            kwargs:
                ignore_auth_data (bool): if True, the provided auth data is ignored.
        zauth_response for sspiNr)   Tr   r   r   zKPackage "pywin32" (Python for Win32 (pywin32) extensions) is not installed.ztargetspn: %sz_auth_info is None: %s	Negotiate)	targetspn	auth_infoscflagsdatarepContext step err: %sContext step out_buf: %sr   r   zpkg_info: %sr   r   )r   r+   r[   r   rw   r   r   sspiconsspir   ISC_REQ_MUTUAL_AUTHISC_REQ_DELEGATEr   ri   
ClientAuthr   SECURITY_NETWORK_DREPr   	authorizeauthenticatedBufferpkg_info	Exceptionr   )r   r&   r'   r|   r~   rp   r   
_auth_infor   dataout_bufr   s               r   r,   z)MySQLSSPIKerberosAuthPlugin.auth_response  sc    	-... 	WVZZ(:DAA 	WW!229==
UU< W W W&'Ls'L'LMMSVVW 	,c222[%(((?dl"%  
 ,g.FG> 	dn 	.%@JJJ	_i000-zT/ABBB* / JJ1
 
 
		YD?44T::LCL/555L3W===L14?3PQQQ#*1:#4 L)ABBBB 	Y 	Y 	Y !N!N!NOOUXX	Y 	/1EFFF##s0   A A2A--A2BG- -
H7H

Hr!   c                 X   t          j        d|           | j                            |          \  }}t          j        d|           t          j        d|           |d         j        }t          j        d|           t          j        d| j        j                   || j        j        fS )r#   r   r   r   r   zContext step resp: %sr   )r   r+   r   r   r   r   )r   r!   rp   r   r   s        r   r$   z)MySQLSSPIKerberosAuthPlugin.auth_continue%  s    " 	-/ABBB001CDDW+S111/999qz ,d333-t/LMMMT_222r   r   )r8   r9   r:   r;   r@   r   r   r   r   r0   r   r=   r   r   r,   r>   r$   r   r   r   r   r     s         NNGSJ, ,5c? , , , \,4 ,0N$ N$!%N$;>N$	%N$ N$ N$ N$`3"*5/3	x$	%3 3 3 3 3 3r   ) r;   rN   rY   rw   abcr   pathlibr   typingr   r   r   r   authenticationr
   errorsr   r   r   networkr   rG   ImportErrorr   r   r    r   AUTHENTICATION_PLUGIN_CLASSr   r   r   r   r   r   <module>r      s>  > & %  				              6 6 6 6 6 6 6 6 6 6 6 6 ' ' ' ' ' ' 5 5 5 5 5 5 5 5       &%%%%%%	MMMM   F	w$
 
 		 KKKNNNN   DGGG       &(W__!!:S 
c c c c c/ c c cNB B B B B9 B B BJK3 K3 K3 K3 K3"= K3 K3 K3 K3 K3s$   A	 	A*)A*.A7 7	BB