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|rÿz
| 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‚|r’t |dd… d	¡}	|dd… }d}|t|ƒ7 }||7 }||	krú||	d
 |	d … }||	d |	… }||	d… }|d|	d
 … }| j j}t t||| ƒdd… ¡}||kròt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r€t  S d| j_ttƒr’ttd| jj| jƒ t| jjƒ‚t|d ƒ|kr¾|d dkr¾d| j_ttƒr¸ttd| jj| jƒ t| jjƒ‚d| j_ttƒrÐt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      