
    =OOf;                         d Z ddlZddlZddlmZmZ ddlmZmZm	Z	 dZ
	 ddlZddlmZ dZ
dZe
r	 ddlZdZd
dlmZmZ d
ZdZdZd Zd Zd Zd Zd Zd Zd Zy# e$ r Y 6w xY w# e$ r	  ed	      w xY w)
    N   )LDAPPackageUnavailableErrorLDAPCommunicationError)ReverseDnsSettingget_hostname_by_addr
is_ip_addrT)ChannelBindingsFz'package gssapi (or winkerberos) missing   )send_sasl_negotiationabort_sasl_negotiation      c                    	 | j                  d      }	 ddlm} ddlm} ddlm} |j                  | |             }|j                  }|j                  dv r&|j                  |j                          |             }n|j                  | |             }|j                  |       d|j                         z   }t         st#        |	      S t%        j&                  |	      S #  Y y xY w# t        $ r t        d      w xY w)
NTr   )x509)default_backend)hasheszpackage cryptography missing)md5sha1s   tls-server-end-point:)application_data)getpeercertcryptographyr   cryptography.hazmat.backendsr   cryptography.hazmat.primitivesr   ImportErrorr   load_der_x509_certificatesignature_hash_algorithmnameHashSHA256updatefinalizeposix_gssapi_unavailabler
   winkerberoschannelBindings)	
ssl_socketserver_certificater   r   r   certhash_algorithmdigestr   s	            ]/var/www/premiumrankchecker/venv/lib/python3.12/site-packages/ldap3/protocol/sasl/kerberos.pyget_channel_bindingsr,   <   s    '33D9J%@9 ))*<o>OPD22No-V]]_o.?@^_->?
MM$%/&//2CC#0@AA**<LMM-
  J)*HIIJs   C  C'  C$'C<c                 >    t         st        | |      S t        | |      S )a  
    Performs a bind using the Kerberos v5 ("GSSAPI") SASL mechanism
    from RFC 4752. Does not support any security layers, only authentication!

    sasl_credentials can be empty or a tuple with one or two elements.
    The first element determines which service principal to request a ticket for and can be one of the following:

    - None or False, to use the hostname from the Server object
    - True to perform a reverse DNS lookup to retrieve the canonical hostname for the hosts IP address
    - A string containing the hostname

    The optional second element is what authorization ID to request.

    - If omitted or None, the authentication ID is used as the authorization ID
    - If a string, the authorization ID to use. Should start with "dn:" or "user:".

    The optional third element is a raw gssapi credentials structure which can be used over
    the implicit use of a krb ccache.
    )r#   _posix_sasl_gssapi_windows_sasl_gssapi)
connectioncontrolss     r+   sasl_gssapir2   X   s!    ( $!*h77#J99    c                 H   | j                   r't        | j                         dk(  s| j                   d   sd| j                  j                  z   S | j                   d   du r-t	        | j
                  j                         d         }d|z   }|S | j                   d   t        j                  v r| j                   d   }| j
                  j                         d   }| j                  j                  }|t        j                  k(  rt	        |      }n~|t        j                  k(  rt        |      r`t	        |      }nT|t        j                  k(  rt	        |d      }|2|}n/|t        j                  k(  rt        |      rt	        |d      }||}d|z   }|S d| j                   d   z   }|S )a   Common logic for determining our target name for kerberos negotiation, regardless of whether we are using
    gssapi on a posix system or winkerberos on windows.
    Returns a string in the form "ldap@" + a target hostname.
    The hostname can either be user specified, come from the connection, or resolved using reverse DNS based
    on parameters set in sasl_credentials.
    The default if no sasl_credentials are specified is to use the host in the connection server object.
    r   zldap@TF)success_required)sasl_credentialslenserverhostr   socketgetpeernamer   SUPPORTED_VALUESREQUIRE_RESOLVE_ALL_ADDRESSES!REQUIRE_RESOLVE_IP_ADDRESSES_ONLYr	   OPTIONAL_RESOLVE_ALL_ADDRESSES"OPTIONAL_RESOLVE_IP_ADDRESSES_ONLY)r0   hostnametarget_namerdns_settingpeer_ipresolved_hostnames         r+   _common_determine_target_namerF   r   s    ''3z/J/J+Kq+P..q1**//// ""1%-'
(9(9(E(E(G(JK(> = 
	$	$Q	'+<+M+M	M!2215 ##//1!4$$)),JJJ+G4H.PPP(#/8.MMM !5Wu U ,,.QQQ (#$8SX$Y!$00H(  
 ; ;A >>r3   c                 ,   d}d}t         sM| j                  r?t        j                  t        j                  | j                        d| j
                        nd}| j                  rt        | j                        dk\  r-| j                  d   r| j                  d   j                  d      }t        | j                        dk\  rQ| j                  d   rBt         rt        d	      | j                  d   }t        j                  |d| j
                  
      }||fS )a   Given our connection, figure out the authorization id (i.e. the kerberos principal) and kerberos credentials
    being used for our SASL bind.
    On posix systems, we can actively negotiate with raw credentials and receive a kerberos client ticket during our
    SASL bind. So credentials can be specified.
    However, on windows systems, winkerberos expects to use the credentials cached by the system because windows
    machines are generally domain-joined. So no initiation is supported, as the TGT must already be valid prior
    to beginning the SASL bind, and the windows system credentials will be used as needed with that.
    r3   Ninitiate)r   usagestorer   r   utf-8r   zThe winkerberos package does not support specifying raw  credentialsto initiate GSSAPI Kerberos communication. A ticket granting ticket must have already been obtained for the user before beginning a SASL bind.)baserI   rJ   )
r#   usergssapiCredentialsName
cred_storer6   r7   encoder   )r0   authz_idcreds	raw_credss       r+   $_common_determine_authz_id_and_credsrV      s	    HE $ yC  yH  yH""JOO(DJ^h^s^st  NR""z**+q0Z5P5PQR5S!2215<<WEHz**+q0Z5P5PQR5S'1 3? @ @ #33A6I&&IZzOdOdeEU?r3   c                     t        |       dk7  rt        d      | d   }t        |t              st	        |      }|dt
        fv r| dd dk7  rt        d      |t
        z  st        d      t        t
        dddg      }|S )	a   Process the response we got at the end of our SASL negotiation wherein the server told us what
    minimum security layers we need, and return a bytearray for the client security layers we want.
    This function throws an error on a malformed token from the server.
    The ldap3 library does not support security layers, and only supports authentication with kerberos,
    so an error will be thrown for any tokens that indicate a security layer requirement.
    r   zIncorrect response from serverr   r   Nz   z5Server max buffer size must be 0 if no security layerz=Server requires a security layer, but this is not implemented)r7   r   
isinstanceintordNO_SECURITY_LAYER	bytearray)negotiated_tokenserver_security_layersclient_security_layerss      r+   -_common_process_end_token_get_security_layersr`      s     !$%EFF-a0,c2!$%;!<!%6!77AB>1()`aa"%66$%dee '(91a'CD!!r3   c                    t        j                  t        |       t         j                  j                        }t        |       \  }}t        j                  |t         j                  j                  |t        | j                              }d}	 	 |j                  |      }|d}t        | ||      }|d   }	 |j                  rn	 7|j!                  |      }	t#        |	j$                        }
|j'                  t)        |
      |z   d      }t        | ||j$                        S # t         j                  j                  $ r Y {w xY w# t         j                  j*                  t,        f$ r t/        | |        w xY w)z Performs a bind using the Kerberos v5 ("GSSAPI") SASL mechanism
    from RFC 4752 using the gssapi package that works natively on most
    posix operating systems.
    )r   mechrT   channel_bindingsN 	saslCredsF)rN   rP   rF   NameTypehostbased_servicerV   SecurityContextMechTypekerberosr,   r:   stepr   complete
exceptionsMissingContextErrorunwrapr`   messagewrapbytesGSSErrorr   r   )r0   r1   rB   rS   rT   ctxin_token	out_tokenresultunwrapped_tokenr_   s              r+   r.   r.      s[   
 ++;JGIjIjkK::FOHe

 
 k8P8PX]2FzGXGX2Y[CH*I 	*:xKFk*H <<    **X.!NOfOf!gHHU#9:8CUK	$Z9;L;LMM $$88  &&(>? z84s1   (E 6D  AE  E =E ?E  E 1E4c                    t        |       }t        |       \  }}t        j                  t        j                  z  t        j
                  z  t        j                  z  }t        j                  ||      \  }}d}	 d}|st        j                  |t        j                  |      j                  d      t        | j                              }	|	t        j                  k(  }t        j                  |      xs d}
t        j                   |
      }t#        | ||      }|d   xs d}|st        j$                  |t        j                  |      j                  d             d}t        j                  |      r(t        j&                  t        j                  |            }t)        |      }t        j                  t+        |      |z         j                  d      }t        j,                  ||       t        j                  |      xs d}
t#        | |t        j                   |
            S # t        j.                  t0        f$ r t3        | |        w xY w)z Performs a bind using the Kerberos v5 ("GSSAPI") SASL mechanism
    from RFC 4752 using the winkerberos package that works natively on most
    windows operating systems.
    )gssflagsr3   FrK   )rc   rd   re   )rF   rV   r$   GSS_C_MUTUAL_FLAGGSS_C_SEQUENCE_FLAGGSS_C_INTEG_FLAGGSS_C_CONF_FLAGauthGSSClientInitauthGSSClientStepbase64	b64encodedecoder,   r:   AUTH_GSS_COMPLETEauthGSSClientResponse	b64decoder   authGSSClientUnwrapstandard_b64decoder`   rr   authGSSClientWraprs   r   r   )r0   r1   rB   rS   _rz   rt   ru   negotiation_completestatusrv   out_token_bytesrw   r]   r_   authz_only_msgs                   r+   r/   r/      s   
 0
;K6zBKHa))++,(() ''(  **;JFAsH$&
 !2238H8H8R8Y8YZa8bDXYcYjYjDkmF %+k.K.K$K #99#>D"I$..y9O*:xQFk*1cH ' 	''V-=-=h-G-N-Nw-WX,,S1%889Z9Z[^9_`!NO_!`  ))%0F*G(*RSZZ[bc%%c>:55c:@b	$Z6;K;KI;VWW  "89 z84s   :B*H& %D H& &'I)__doc__r   r:   core.exceptionsr   r   	core.rdnsr   r   r	   r#   rN   
gssapi.rawr
   r   windows_gssapi_unavailabler$   saslr   r   r[   INTEGRITY_PROTECTIONCONFIDENTIALITY_PROTECTIONr,   r2   rF   rV   r`   r.   r/    r3   r+   <module>r      s   6   R L L 	*$ " U%*" @    N8:40f<"0!H/o  		  U)*STTUs   A A& A#"A#&A4