o
    jl6                     @   s   d Z ddlZddlmZmZmZ ddlmZmZm	Z	m
Z
mZ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mZmZmZmZ dd	lmZmZ dd
lmZ e Z G dd deZ!dS )
    N   )SEQUENCE_TYPESget_config_parameter
DIGEST_MD5)LDAPSocketReceiveErrorcommunication_exception_factoryLDAPExceptionErrorLDAPExtensionErrorLDAPOperationResult$LDAPSignatureVerificationFailedError)BaseStrategySESSION_TERMINATED_BY_SERVERRESPONSE_COMPLETETRANSACTION_ERROR)LDAPMessage)loglog_enabledERRORNETWORKEXTENDEDformat_ldap_message)decoderdecode_message_fast)md5_hmacc                   @   sZ   e Zd ZdZdd ZdddZdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd ZdS )SyncStrategyaZ  
    This strategy is synchronous. You send the request and get the response
    Requests return a boolean value to indicate the result of the requested Operation
    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
    c                 C   s2   t | | d| _d| _d| _d| _td| _d S )NTFSOCKET_SIZE)r   __init__syncno_real_dsapooled
can_streamr   socket_size)selfldap_connection r%   /root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/ldap3/strategy/sync.pyr   0   s   zSyncStrategy.__init__Tc                 C   s\   t | || |r*| jjs,z| j  W d S  ty)   d | jj_d | jj_Y d S w d S d S N)	r   open
connection_deferred_openrefresh_server_infor   server	_dsa_info_schema_info)r#   reset_usageread_server_infor%   r%   r&   r(   8   s   
zSyncStrategy.openc                 C   s$   | j js| j jsd| j _d S d S d S )NT)r)   	listeningclosedr#   r%   r%   r&   _start_listenA   s   zSyncStrategy._start_listenc                 C   s  g }d}d}d}d}d}d}d}d}	|rO|rz
| j j| j}W nH ttjtfyi }
 z8dt|
 | j _z| 	  W n tjt
fyG   Y nw ttrVttd| j j| j  ttt|
t|
| j jd}
~
ww | j jtkr| j jr| j js|| }|dks|rt|dd d	}	|dd }d}|t|7 }||7 }||	kr||	d
 |	d  }||	d |	 }||	d }|d|	d
  }| j j}tt||| dd }||krtdtt|d	 d |  d |  d d}||7 }d}n||7 }t|dkrKt|}|dkrd}qt||k rd}n0ttr0ttdt|d| | j  | |d|  ||d }d}t|dkrJd}nd}|sttr^ttdt|| j  |S )zV
        Receives data over the socket
        Checks if the socket is closed
        T    r   zerror receiving data: <%s> for <%s>N   big         zCSignature verification failed for the recieved LDAP message number z. Expected signature z	 but got .zreceived %d bytes via <%s>Fz"received %d ldap messages via <%s>)!r)   socketrecvr"   OSErrorerrorAttributeErrorstr
last_errorcloser	   r   r   r   r   r   typesasl_mechanismr   _digest_md5_kissasl_in_progressint
from_byteslenbytesfromhexr   r   hexr   compute_ldap_message_sizer   append)r#   messages	receivingunprocesseddataget_more_datasasl_total_bytes_recievedsasl_received_datasasl_next_packetsasl_buffer_lengthesasl_signaturesasl_sec_numkiscalculated_signaturelengthr%   r%   r&   rS   E   s   4



AzSyncStrategy.receivingc                 C   sv   |  |\}}|| j_|d dkr4|D ] }|d dkr3d| j_ttr-ttd| jj| j t| jjq|| |S )zp
        Executed after an Operation Request (except Search)
        Returns the result message or None
        rF   intermediateResponsez multiple messages received errorr7   )	get_responser)   resultrD   r   r   r   r   rQ   )r#   
message_id	responsesrc   responser%   r%   r&   post_send_single_response   s   
z&SyncStrategy.post_send_single_responsec                 C   sd   |  |\}}|| j_t|tr|dd | j_|S d| j_ttr,t	td| jj| j t
| jj)z
        Executed after a search request
        Returns the result message and store in connection.response the objects found
        Nzerror receiving responser7   )rb   r)   rc   
isinstancer   rf   rD   r   r   r   r   )r#   rd   re   rc   r%   r%   r&   post_send_search   s   
zSyncStrategy.post_send_searchc           
      C   s  g }d}|s|   }|r|D ]}t|dkr| jjr#| jjt| | jjr1t|}| |}nt	j
|td\}}	| |}ttrNttd| jt|d t|d |krd|| |d dvrcd	}qt|d dkr|d
 dkrvt  S |d
 dkrt  S d| j_ttrttd| jj| j t| jjt|d |kr|d dkrd| j_ttrttd| jj| j t| jjd| j_ttrttd| jj| j t| jjqntS |r|t |S )zH
        Performs the capture of LDAP response for SyncStrategy
        Fr   )asn1Specz!ldap message received via <%s>:%sz<<	messageIDrF   )searchResEntrysearchResRefra   TresponseNamez1.3.6.1.4.1.1466.20036z2.16.840.1.113719.1.27.103.4z,unknown unsolicited notification from serverr7   extendedRespz8multiple extended responses to a single extended requestzinvalid messageId received)rS   rL   r)   usage_usageupdate_received_messagefast_decoderr   decode_response_fastr   decodeLDAP_MESSAGE_TEMPLATEdecode_responser   r   r   r   rJ   rQ   r   r   rD   r   r   r
   r   )
r#   rd   timeoutldap_responsesresponse_completere   rf   	ldap_respdict_response_r%   r%   r&   _get_response   sZ   

,
0zSyncStrategy._get_responsec                 C      t r'   NotImplementedError)r#   valuer%   r%   r&   
set_stream      zSyncStrategy.set_streamc                 C   r   r'   r   r3   r%   r%   r&   
get_stream   r   zSyncStrategy.get_streamN)TT)__name__
__module____qualname____doc__r   r(   r4   rS   rg   ri   r~   r   r   r%   r%   r%   r&   r   (   s    
	V:r   )"r   r>    r   r   r   core.exceptionsr   r   r	   r
   r   r   strategy.baser   r   r   r   protocol.rfc4511r   	utils.logr   r   r   r   r   r   
utils.asn1r   r   protocol.sasl.digestMd5r   rv   r   r%   r%   r%   r&   <module>   s      