1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
U
£ý°dNDã@sdZddlZddlZddlZddlmZddlmZmZm    Z    ddl
m Z m Z ddl m Z z ddlZWn,ek
rdZejdkrŒe d    ƒd‚YnXzddlZddlZWnek
rÂdZdZYnXd
d lmZejdkrÞd nd ZGdd „d eƒZGdd „d eƒZdS)zKerberos Authentication Plugin.éN)ÚPath)ÚAnyÚOptionalÚTupleé)ÚInterfaceErrorÚProgrammingError)ÚloggerÚntzwModule gssapi is required for GSSAPI authentication mechanism but was not found. Unable to authenticate with the serveré)ÚBaseAuthPluginÚMySQLSSPIKerberosAuthPluginÚMySQLKerberosAuthPluginc@sÜeZdZUdZdZeed<dZeed<dZ    e
e j ed<e edœd    d
„ƒZe edœd d „ƒZee jjjd œdd„Ze eeeefdœdd„ƒZde
ee
edœdd„Ze
eee
eefdœdd„Zeedœdd„ZdS)rz3Implement the MySQL Kerberos authentication plugin.Úauthentication_kerberos_clientÚ plugin_nameFÚ requires_sslNÚcontext)ÚreturncCsbz:tjdd}t|jƒ}| d¡dkr6| dd¡\}}|WStjjjk
r\t     
¡YSXdS)z(Get user from credentials without realm.Úinitiate©Úusageú@éÿÿÿÿr N) ÚgssapiÚ CredentialsÚstrÚnameÚfindÚsplitÚrawÚmiscÚGSSErrorÚgetpassÚgetuser)ÚcredsÚuserÚ_©r'úmd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\mysql/connector/plugins/authentication_kerberos_client.pyÚget_user_from_credentialsLs 
z1MySQLKerberosAuthPlugin.get_user_from_credentialscCs`tj dtjdkr dt ¡›n tdƒ d¡¡}|s<tdƒ‚t     d|¡dd    |› 
d
¡i}|S) zäGet a credentials store dictionary.
 
        Returns:
            dict: Credentials store dictionary with the krb5 ccache name.
 
        Raises:
            InterfaceError: If 'KRB5CCNAME' environment variable is empty.
        Z
KRB5CCNAMEÚposixz /tmp/krb5cc_z%TEMP%Zkrb5ccz5The 'KRB5CCNAME' environment variable is set to emptyzUsing krb5 ccache name: FILE:%ssccachezFILE:úutf-8) ÚosÚenvironÚgetrÚgetuidrÚjoinpathrr    ÚdebugÚencode)Z
krb5ccnameÚstorer'r'r(Ú    get_storeXs
ÿ üÿ z!MySQLKerberosAuthPlugin.get_store)Úupnrc
Csžt d¡t |tjj¡}|j d¡}z:tjj    ||dd}|j
}tjj |  ¡|tj jdddWn8tjjjk
r˜}ztd|›ƒ|‚W5d}~XYnX|S)    zÆAcquire 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 passwordr+rrT)r$ZmechÚ    overwriteÚ set_defaultz7Unable to acquire credentials with the given password: N)r    r1rÚNameÚNameTyper%Ú    _passwordr2rZacquire_cred_with_passwordr$Zstore_cred_intor4ÚMechTypeÚkerberosr r!r)Úselfr5r%ÚpasswordZacquire_cred_resultr$Úerrr'r'r(Ú_acquire_cred_with_passwordps0    
 ÿû
ÿþz3MySQLKerberosAuthPlugin._acquire_cred_with_password©ÚpacketrcCs˜t d|dd…¡d}|dd…}t d|›d|d|…¡d}||d…}t d|dd…¡d}t d|›d|dd…¡d}| ¡| ¡fS©aYParse 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<HNrrú<Ús©ÚstructÚunpackÚdecode©rBZspn_lenÚspnZ    realm_lenÚrealmr'r'r(Ú_parse_auth_datas    z(MySQLKerberosAuthPlugin._parse_auth_data©Ú    auth_datarc
Cs¼d}d}|rTz| |¡\}}Wn4tjk
rR}ztd|›ƒ|‚W5d}~XYnX|dkrd| ¡S|jrz|j›d|›nd}t d|¡t d|¡zÎtj    dd}t
|j ƒ}t d¡t d    |¡|  d¡d
krä|  dd ¡\}}    n|}d}    |jr|j›d|›n|}|jr<|j|kr<t d ¡|jdk    r<| |¡}|    rb|    |krb|jdk    rb| |¡}Wn®tjjjk
r¼}z2|rœ|jdk    rœ| |¡}ntd |›ƒ|‚W5d}~XYnXtjjjk
r}z2|rò|jdk    rò| |¡}ntd|›ƒ|‚W5d}~XYnXtjjtjjtjjf}
tj|tjjd} |  tjj¡} tj | |t!|
ƒdd|_"z|j" #¡} Wn:tjjjk
rª}ztd|›ƒ|‚W5d}~XYnXt d| ¡| S)ú(Prepare the first message to the server.NúInvalid authentication data: rúService Principal: %sú    Realm: %srrzCached credentials foundzCached credentials UPN: %srr zBThe user from cached credentials doesn't match with the given userzCredentials has expired: z-Unable to retrieve cached credentials error: )Z    name_type)rr$Úflagsrú%Unable to initiate security context: úInitial client token: %s)$rMrGÚerrorÚInterruptedErrorZprepare_passwordÚ    _usernamer    r1rrrrrrr:r@rÚ
exceptionsZExpiredCredentialsErrorrr r!ZRequirementFlagZmutual_authenticationZextended_errorZdelegate_to_peerr8r9Zkerberos_principalÚ canonicalizer;r<ÚSecurityContextÚsumrÚstep)r=rOrKrLr?r5r$Z    creds_upnZ
creds_userZ creds_realmrTrÚcnameÚinitial_client_tokenr'r'r(Ú auth_response©sx"   
 
 ÿ 
   ÿþýÿ" z%MySQLKerberosAuthPlugin.auth_response©Útgt_auth_challengercCs@t d|¡|j |¡}t d|¡t d|jj¡||jjfS)á!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).
        útgt_auth challenge: %szContext step response: %súContext completed?: %s)r    r1rr^Úcomplete)r=rcÚrespr'r'r(Ú auth_continue÷s
   z%MySQLKerberosAuthPlugin.auth_continue)Úmessagerc
CsÞ|jjstdƒ‚t d|¡t d|jj¡z|j |¡}t d|¡WnDtjj    j
k
rŒ}z t d|¡t d|›ƒ|‚W5d}~XYnXt d|¡t d    ƒ}t d
|¡|jj |d d }t d |dt|dƒ¡|jS)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 completedzServer message: %szGSSAPI flags in use: %sz Unwraped: %sz#Unable to unwrap server message: %sz!Unable to unwrap server message: NzUnwrapped server message: %sszMessage response: %sF)Zencryptz(Wrapped message response: %s, length: %dr)rrgrr    r1Z actual_flagsÚunwraprrrZZ BadMICErrorrÚ    bytearrayÚwrapÚlenrj)r=rjZunwrapedr?ÚresponseZwrapedr'r'r(Úauth_accept_close_handshakes(   "  
ýz3MySQLKerberosAuthPlugin.auth_accept_close_handshake)N)Ú__name__Ú
__module__Ú __qualname__Ú__doc__rrÚ__annotations__rÚboolrrrr\Ú staticmethodr)Údictr4rr$ZCredsr@ÚbytesrrMrarirpr'r'r'r(rEs 
   Oþ c@s˜eZdZUdZdZeed<dZeed<dZ    e
ed<dZ e
ed<e e eeefd    œd
d „ƒZdee ee d œd d„Zee eee efdœdd„ZdS)r zDImplement the MySQL Kerberos authentication plugin with Windows SSPIrrFrNrÚ
clientauthrAcCs˜t d|dd…¡d}|dd…}t d|›d|d|…¡d}||d…}t d|dd…¡d}t d|›d|dd…¡d}| ¡| ¡fSrCrFrJr'r'r(rMPs    z,MySQLSSPIKerberosAuthPlugin._parse_auth_datarNc
Cs–t d¡d}d}|r^z| |¡\}}Wn4tjk
r\}ztd|›ƒ|‚W5d}~XYnXt d|¡t d|¡tdks†tdkrŽtdƒ‚tj    tj
f}|j r¶|j r¶|j ||j f}nd}|}t d|¡t d|dk¡tj d    ||t|ƒtjd
|_zZd}|j |¡\}}    t d |¡t d |    ¡t d |jj¡|    dj}
t d|jj¡Wn4tk
r„}ztd|›ƒ|‚W5d}~XYnXt d|
¡|
S)rPzauth_response for sspiNrQrRrSzKPackage "pywin32" (Python for Win32 (pywin32) extensions) is not installed.z targetspn: %sz_auth_info is None: %sZ    Negotiate)Ú    targetspnZ    auth_infoZscflagsZdatarepúContext step err: %súContext step out_buf: %srfrz pkg_info: %srUrV)r    r1rMrGrWrXÚsspiconÚsspirZISC_REQ_MUTUAL_AUTHZISC_REQ_DELEGATErYr:Z
ClientAuthr]ZSECURITY_NETWORK_DREPrzÚ    authorizeÚ authenticatedÚBufferÚpkg_infoÚ    Exceptionr) r=rOrKrLr?rTZ
_auth_infor{ÚdataÚout_bufr`r'r'r(rajsP
"  ÿ   û  
" z)MySQLSSPIKerberosAuthPlugin.auth_responserbcCsft d|¡|j |¡\}}t d|¡t d|¡|dj}t d|¡t d|jj¡||jjfS)rdrer|r}rzContext step resp: %srf)r    r1rzr€r‚r)r=rcr?r†rhr'r'r(ri³s   
 z)MySQLSSPIKerberosAuthPlugin.auth_continue)N)rqrrrsrtrrrurrvrrrzrwryrrMrrarir'r'r'r(r Hs
    Jþ)rtr"r,rGÚpathlibrÚtypingrrrÚerrorsrrr    rÚ ImportErrorrrr~Úr ZAUTHENTICATION_PLUGIN_CLASSrr r'r'r'r(Ú<module>s<   
ÿü 
 ÿ