
    >OOf;                         d Z ddlmZmZmZ ddlZddlmZmZ ddl	m
Z
mZmZmZ ddlmZmZ ddlmZ dd	lmZmZmZmZmZmZ dd
lmZmZ ddlmZ  G d de      Zy)
    )ThreadLockEventN   )get_config_parameter
DIGEST_MD5)LDAPSSLConfigurationErrorLDAPStartTLSErrorLDAPOperationResult$LDAPSignatureVerificationFailedError)BaseStrategyRESPONSE_COMPLETE)LDAPMessage)loglog_enabledformat_ldap_messageERRORNETWORKEXTENDED)decoderdecode_message_fast)md5_hmacc                   v    e Zd ZdZ G d de      Zd ZddZd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zy)AsyncStrategyaU  
    This strategy is asynchronous. You send the request and get the messageId of the request sent
    Receiving data from socket is managed in a separated thread in a blocking mode
    Requests return an int value to indicate the messageId of the requested Operation
    You get the response with get_response, it has a timeout to wait for response to appear
    Connection.response will contain the whole LDAP response for the messageId requested in a dict form
    Connection.request will contain the result LDAP message in a dict form
    Response appear in strategy._responses dictionary
    c                       e Zd ZdZd Zd Zy)"AsyncStrategy.ReceiverSocketThreadzE
        The thread that actually manage the receiver socket
        c                 \    t        j                  |        || _        t        d      | _        y )NSOCKET_SIZE)r   __init__
connectionr   socket_sizeselfldap_connections     \/var/www/premiumrankchecker/venv/lib/python3.12/site-packages/ldap3/strategy/asynchronous.pyr    z+AsyncStrategy.ReceiverSocketThread.__init__8   s"    OOD!-DO3MBD    c           	         d}d}d}d}d}d}d}d}|r|r	 | j                   j                  j                  | j                        }t        |      dkD  rX| j                   j                  t        k(  r2| j                   j                   r| j                   j"                  s||z   }|dk(  s|rt$        j'                  |dd d      }|dd }d}|t        |      z  }||z  }||k\  r||d	z
  |d
z
   }
||dz
  | }||d }|d|d	z
   }| j                   j                   }t(        j+                  t-        |||z         dd       }|
|k7  rVt/        dt        t$        j'                  |d            z   dz   |j1                         z   dz   |
j1                         z   dz         d}||z  }d}n||z  }d}nd}t3        j4                  |      }|dk(  st        |      |k  rd}nt        |      |k\  r| j                   j6                  rP| j                   j8                  j;                  |       t        t<              rt        t<        d|| j                          | j                   j>                  r4tA        |d|       }| j                   jB                  jE                  |      }nJtG        jH                  |d| tK                     d   }| j                   jB                  jM                  |      }t%        |d         }t        t<              rt        t<        d| j                          t        tN              r&t        tN        d| j                   tQ        |d             |d   dk(  rk|d   dk(  stS        | j                   d      rL|d   dk(  r| j                   jT                  jV                  r:| j                   jT                  jV                  jY                  | j                          nd| j                   _-        t        t              r0t        t        d| j                   jZ                  | j                          t]        | j                   jZ                        d| j                   _-        t        t              r0t        t        d| j                   jZ                  | j                          t_        | j                   jZ                        | j                   `0|dk7  rr| j                   jB                  jb                  5  || j                   jB                  jd                  v r3| j                   jB                  jd                  |   jg                  |       n$|g| j                   jB                  jd                  |<   |d   dvr[| j                   jB                  jd                  |   jg                  th               | j                   jB                  jk                  |       ddd       | j                   jB                  jl                  r&| j                   jB                  jo                  ||       ||d }|rdnd}| j                   jp                  s|rdnd}nz|d   d k(  rd}nod!| j                   _-        t        t              r0t        t        d| j                   jZ                  | j                          t_        | j                   jZ                        |r| j                   jB                  js                          y# t        t        j
                  t        f$ r | j                   j                  rY Y t        $ r:}	t        t              r%t        t        dt        |	      | j                           d}	~	ww xY w# 1 sw Y   xY w)"z
            Waits for data on socket, computes the length of the message and waits for enough bytes to decode the message
            Message are appended to strategy._responses
            r'   Tr   z<%s> for <%s>N   big         zCSignature verification failed for the recieved LDAP message number z. Expected signature z	 but got .Fzreceived %d bytes via <%s>)asn1Spec	messageIDz received 1 ldap message via <%s>z!ldap message received via <%s>:%sz<<typeextendedRespresponseNamez1.3.6.1.4.1.1466.20037_awaiting_for_async_start_tlsresultzno Tls object defined in Serverzasynchronous StartTls failed)searchResEntrysearchResRefintermediateResponsez1.3.6.1.4.1.1466.20036z,unknown unsolicited notification from server):r!   socketrecvr"   OSErrorerrorAttributeErrorreceive_timeout	Exceptionr   r   r   strlensasl_mechanismr	   _digest_md5_kissasl_in_progressint
from_bytesbytesfromhexr   r   hexr   compute_ldap_message_sizeusage_usageupdate_received_messager   fast_decoderr   strategydecode_response_fastr   decoder   decode_responser   r   hasattrservertls
_start_tls
last_errorr
   r   r5   
async_lock
_responsesappendr   set_event_for_message
can_streamaccumulate_stream	listeningclose)r$   unprocessedget_more_datalistendatasasl_total_bytes_recievedsasl_received_datasasl_next_packetsasl_buffer_lengthesasl_signaturesasl_sec_numkiscalculated_signaturelength	ldap_respdict_response
message_ids                     r&   runz&AsyncStrategy.ReceiverSocketThread.run=   s   
 K MFD()%!$"!#  #55::4;K;KL 4y1}??99ZGDOOLkLktx  uD  uD  uU  uU#3d#:D1S8<L58^^D1Iu5U 2'+ABx/2,5TB5.$6.8<NN 2DDVY[D[\nqr\r1s/ABTWXBXYk/l3EFXFY3Z 05GH`I[^`I`5a 2&*oo&E&E7<}}XcS_btStEuvwxzE{7| 4#15I#I*N  PU  X[  \_  \j  \j  kw  y~  \  X@  P@  CZ  PZ  ]q  ]u  ]u  ]w  Pw  zE  PE  HV  HZ  HZ  H\  P\  _b  Pb  +c  %c<= 9 +/A A58 2'4/K"!&%??LR<3{#3f#<$(M%/,,..FFvN&w/)Evt_33$7GV8L$M	(,(@(@(U(UV_(`$+NN;w3GR]R_$`ab$c	(,(@(@(P(PQZ([!$Y{%;!<J"7+G%GY"8,H&I4??\opy{  ]A  B$V,>MR`Dae}D}  BI  JN  JY  JY  [z  B{(2a7#5599 $ 6 6 : : E Edoo V=^ :#.u#5$'@Z@Z\`\k\k$l&?@Z@Z&[ [9WDOO6*51 #E?DOO<V<VX\XgXg h"3DOO4N4N"OO OOI!Q!__55@@ [)T__-E-E-P-PP $ 8 8 C CJ O V VWd eS`Ra 8 8 C CJ O,V4<vv $ 8 8 C CJ O V VWh i $ 8 8 N Nz Z[  ??33>> OO44FFzS`a&1&'&:1<$)-)B)BkW\(8<TT%*F9gDOO6*51 #E?DOO<V<VX\XgXg h"3DOO4N4N"OOO P OO$$**,I $V\\>B %??::$ ;$ &u-APV[ [s*   /[! ,C] !4]]#5]] ]*N)__name__
__module____qualname____doc__r    rr    r'   r&   ReceiverSocketThreadr   3   s    		C
x	-r'   rx   c                     t        j                  | |       d| _        d| _        d| _        d | _        d | _        d| _        d | _        t               | _
        t               | _        i | _        y )NF)r   r    syncno_real_dsapooledrZ   	_requestsr]   receiverr   rY   
event_lock_eventsr#   s     r&   r    zAsyncStrategy.__init__   s]    dO4	 &&r'   c                    | j                   j                  5  t               | _        t               | _        t        j                  | ||       ddd       |r	 | j                   j                          yy# 1 sw Y   (xY w# t        $ r9 d| j                   j                  _
        d| j                   j                  _        Y yw xY w)zV
        Open connection and start listen on the socket in a different thread
        N)r!   connection_lockdictrZ   r}   r   openrefresh_server_infor   rU   	_dsa_info_schema_info)r$   reset_usageread_server_infos      r&   r   zAsyncStrategy.open   s     __,, 	C"fDO!VDNdK1AB	C
 ;335 	C 	C ' ;37&&06:&&3;s   6A4B  4A= ?CCc                     | j                   j                  5  t        j                  |        ddd       y# 1 sw Y   yxY w)z9
        Close connection and stop socket thread
        N)r!   r   r   r`   r$   s    r&   r`   zAsyncStrategy.close   s4     __,, 	%t$	% 	% 	%s   6?c                     | j                   5  || j                  vrt               | j                  |<   d d d        y # 1 sw Y   y xY wN)r   r   r   r$   rq   s     r&   _add_event_for_messagez$AsyncStrategy._add_event_for_message   s:    __ 	3-+07Z(	3 	3 	3s	   &<Ac                     | j                   5  || j                  vrt               | j                  |<   | j                  |   j                          d d d        y # 1 sw Y   y xY wr   )r   r   r   setr   s     r&   r\   z#AsyncStrategy.set_event_for_message   sQ    __ 	+ -+07Z(LL$((*	+ 	+ 	+s   AAA"c                     | j                   5  || j                  vrt        dj                  |            | j                  |   cd d d        S # 1 sw Y   y xY w)Nz?Event for message[{}] should have been created before accessing)r   r   RuntimeErrorformatr   s     r&   _get_event_for_messagez$AsyncStrategy._get_event_for_message   sN    __ 	,-"#d#k#klv#wxx<<
+	, 	, 	,s   7AAc                     d| j                   _        d| j                   _        d| j                   _        | j	                  |       |S )zB
        Clears connection.response and returns messageId
        Nr!   responserequestr6   r   r   s     r&   post_send_searchzAsyncStrategy.post_send_search   <     $( "&!%##J/r'   c                     d| j                   _        d| j                   _        d| j                   _        | j	                  |       |S )zC
        Clears connection.response and returns messageId.
        Nr   r   s     r&   post_send_single_responsez'AsyncStrategy.post_send_single_response   r   r'   c                     | j                   j                  sat        j                  | j                         | _        d| j                   _        d| j                  _        | j                  j                          yy)z-
        Start thread in daemon mode
        TN)r!   r_   r   rx   r~   daemonstartr   s    r&   _start_listenzAsyncStrategy._start_listen  sT     (()>>tODM(,DOO%#'DMM MM!	 )r'   c                     | j                  |      }|j                  |      }|sy| j                  j                  |       | j                  5  | j
                  j                  |      cddd       S # 1 sw Y   yxY w)z
        Performs the capture of LDAP response for this strategy
        The response is only complete after the event been set
        N)r   waitr   poprY   rZ   )r$   rq   timeouteventflags        r&   _get_responsezAsyncStrategy._get_response  sj    
 ++J7zz'" 	$__ 	3??&&z2	3 	3 	3s   A22A;c                     t         r   NotImplementedErrorr   s    r&   	receivingzAsyncStrategy.receiving      !!r'   c                     t         r   r   r   s    r&   
get_streamzAsyncStrategy.get_stream   r   r'   c                     t         r   r   )r$   values     r&   
set_streamzAsyncStrategy.set_stream#  r   r'   N)TT)rs   rt   ru   rv   r   rx   r    r   r`   r   r\   r   r   r   r   r   r   r   r   rw   r'   r&   r   r   '   sW    B-v B-H; %3+,"3 """r'   r   ) rv   	threadingr   r   r   r:    r   r	   core.exceptionsr
   r   r   r   strategy.baser   r   protocol.rfc4511r   	utils.logr   r   r   r   r   r   
utils.asn1r   r   protocol.sasl.digestMd5r   r   rw   r'   r&   <module>r      sD   2 * )  / F  F ; * W W 5 .}"L }"r'   