o
    ßý°jË  ã                   @   s|  d Z ddlZz
ddlmZ dZW n ey   dZY nw ddlmZ ddlmZ ddl	m
Z
 d	d
lmZmZmZmZmZmZmZmZ d	dlmZmZ d	dlmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z% d	dl&m'Z' d	dl(m)Z)m*Z*m+Z+m,Z, d	dl-m.Z.m/Z/ d	dl0m1Z1m2Z2 d	dl3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z; d	dl<m=Z=m>Z>m?Z?m@Z@mAZA d	dlBmCZCmDZD d	dlEmFZFmGZGmHZHmIZImJZJ d	dlKmLZL d	dlMmNZNmOZO d	dlPmQZQmRZR d	dlSmTZTmUZU d	dlVmWZW d	dlXmYZY d	dlZm[Z[ d	dl\m]Z] d	dl^m_Z_ d	dl`maZambZbmcZcmdZdmeZemfZfmgZgmhZh d	dlimjZjmkZkmlZlmmZm d	d lnmoZo d	d!lpmqZqmrZr d"Zsd#Ztd$ZuG d%d&„ d&evƒZwdS )'Ú
é    N)ÚAF_UNIXTF)Úpack)Úsystem)Úchoiceé   )ÚSYNCÚ	ANONYMOUSÚget_config_parameterÚBASEÚALL_ATTRIBUTESÚALL_OPERATIONAL_ATTRIBUTESÚNO_ATTRIBUTESÚ
DIGEST_MD5)ÚDO_NOT_RAISE_EXCEPTIONSÚRESULT_REFERRAL)ÚLDAPOperationResultÚLDAPSASLBindInProgressErrorÚLDAPSocketOpenErrorÚ"LDAPSessionTerminatedByServerErrorÚLDAPUnknownResponseErrorÚLDAPUnknownRequestErrorÚLDAPReferralErrorÚcommunication_exception_factoryÚLDAPStartTLSErrorÚLDAPSocketSendErrorÚLDAPExceptionErrorÚLDAPControlErrorÚLDAPResponseTimeoutErrorÚLDAPTransactionError)Ú	parse_uri)ÚLDAPMessageÚ
ProtocolOpÚ	MessageIDÚSearchResultEntry)Úadd_response_to_dictÚadd_request_to_dict)Úmodify_request_to_dictÚmodify_response_to_dict)Ú(search_result_reference_response_to_dictÚ#search_result_done_response_to_dictÚ$search_result_entry_response_to_dictÚsearch_request_to_dictÚ)search_result_entry_response_to_dict_fastÚ-search_result_reference_response_to_dict_fastÚattributes_to_dictÚattributes_to_dict_fast)Úbind_response_to_dictÚbind_request_to_dictÚsicily_bind_response_to_dictÚbind_response_to_dict_fastÚ!sicily_bind_response_to_dict_fast)Úcompare_response_to_dictÚcompare_request_to_dict)Úextended_request_to_dictÚextended_response_to_dictÚintermediate_response_to_dictÚextended_response_to_dict_fastÚ"intermediate_response_to_dict_fast)ÚServer)Úmodify_dn_request_to_dictÚmodify_dn_response_to_dict)Údelete_response_to_dictÚdelete_request_to_dict)Úprepare_changes_for_requestÚbuild_controls_list)Úabandon_request_to_dict)ÚTls)ÚOids)ÚRealSearchControlValue)ÚDirSyncControlResponseValue)ÚlogÚlog_enabledÚERRORÚBASICÚPROTOCOLÚNETWORKÚEXTENDEDÚformat_ldap_message)ÚencodeÚdecoderÚldap_result_to_dict_fastÚdecode_sequence)Ú
to_unicode)Úmd5_hÚmd5_hmacÚTERMINATED_BY_SERVERÚTRANSACTION_ERRORÚRESPONSE_FROM_SERVER_COMPLETEc                   @   s  e Zd ZdZdd„ Zdd„ Zd?dd„Zd	d
„ Zd@dd„Zdd„ Z	dd„ Z
dAdd„ZdBdd„Zedd„ ƒZdd„ Zdd„ Zedd„ ƒZedCdd „ƒZedAd!d"„ƒZd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Zd9d:„ Zd;d<„ Z d=d>„ Z!dS )DÚBaseStrategyz,
    Base class for connection strategy
    c                 C   sX   || _ d | _g | _d | _d | _d | _d | _i | _d| _t	t
ƒr*tt
d| jj| ƒ d S d S )NFzinstantiated <%s>: <%s>)Ú
connectionÚ_outstandingÚ
_referralsÚsyncÚno_real_dsaÚpooledÚ
can_streamÚreferral_cacheÚthread_saferJ   rL   rI   Ú	__class__Ú__name__)ÚselfÚldap_connection© ri   ú†/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/ldap3/strategy/base.pyÚ__init__N   s   ÿzBaseStrategy.__init__c                 C   sP   | j rt| j ƒnd| jrdnd| jrdnd| jrdnd| jr dnd	g}d
 |¡S )NÚNoner_   Úasynczno real DSAzreal DSAra   z
not pooledzcan stream outputzcannot stream outputú - )r\   Ústrr_   r`   ra   rb   Újoin)rg   Úsri   ri   rj   Ú__str__[   s   û
zBaseStrategy.__str__Tc                 C   s†  t tƒrttd| jƒ | jjr*| jjs*d| j_d| j_t tƒr(ttd| jƒ dS dS | jjs6| jjs6|  ¡  t	ƒ | _
| jjrK|sE| jjjsK| jj ¡  | jjrn| jj | j¡}| jj|krn|| j_| jjrn| jj jd7  _g }| j�s+| jj ¡ D ]W}z,t tƒr‹ttd|dd… ƒ | j|| jjj| jjjd	� || jj_| jj |d¡ W  n) tyÑ } z| jj |d¡ | t|ƒt|ƒƒ|d
 f¡ W Y d}~qzd}~ww | jjj�s|�rt|ƒdk�rt t ƒrþtt dt|d d ƒd t|d d ƒ | jƒ |d d ‚t t ƒ�rtt d| jƒ t!d|ƒ‚| jjj�s+t t ƒ�r'tt d| jƒ t!dƒ‚d| j_|  "¡  t tƒ�rAttd| jƒ dS dS )z^
        Open a socket to a server. Choose a server from the server pool if available
        zopening connection for <%s>TFz"deferring open connection for <%s>é   z try to open candidate address %sNéþÿÿÿ)Úunix_socketé   ú<%s> for <%s>r   ú zunable to open socket for <%s>zunable to open socketzinvalid server address for <%s>zinvalid server addresszconnection open for <%s>)#rJ   rN   rI   r\   ÚlazyÚ_executing_deferredÚ_deferred_openÚclosedÚcloseÚdictr]   ÚusageÚ_usageÚinitial_connection_start_timeÚstartÚserver_poolÚ
get_serverÚserverÚservers_from_poolr`   Úcandidate_addressesrL   Ú_open_socketÚsslÚipcÚcurrent_addressÚupdate_availabilityÚ	ExceptionÚappendÚtypero   ÚlenrK   r   Ú_start_listen)rg   Úreset_usageÚread_server_infoÚ
new_serverÚexception_historyÚcandidate_addressÚeri   ri   rj   Úopene   sh   ÿ
*€ý0



ÿzBaseStrategy.openc                 C   sî   t tƒrttd| jƒ | jjr/| jjs/| jjs| jjr/d| j_d| j_	t tƒr.ttd| jƒ n| jj	s>|  
¡  | js>|  ¡  t tƒrIttd| jƒ d| j_d| j_d| j_d| j_d| _g | _| jjjsid| jj_| jjru| jj ¡  dS dS )z"
        Close connection
        zclosing connection for <%s>FTz#deferred connection closed for <%s>zconnection closed for <%s>N)rJ   rN   rI   r\   ry   rz   Ú_deferred_bindr{   Ú	listeningr|   Ú_stop_listenr`   Ú_close_socketÚboundÚrequestÚresponseÚtls_startedr]   r^   Ústrategyr…   r‹   r   r€   Ústop©rg   ri   ri   rj   r}   ¡   s2    €

ÿzBaseStrategy.closeFc           	      C   s¨  zt j |dd… Ž | j_ W n1 ty> } z%dt|ƒ | j_ttƒr+ttd| jj| jƒ tt	t
|ƒt|ƒƒƒ| jjƒ‚d}~ww d}tr¯| jj jt jkr¯d}| jjD ]4}z| jj  | jj|f¡ d}W  n" ty… } z|}ttƒr{ttd| jj||ƒ W Y d}~qQd}~ww |s¯dt|ƒ | j_ttƒr¢ttd	| jj| jj| j|ƒ tt	t
|ƒt|ƒƒƒ|ƒ‚z| jjjr¿| jj  | jjj¡ | jj  |d
 ¡ W n2 t jyû } z%dt|ƒ | j_ttƒrèttd| jj| jƒ tt	t
|ƒt|ƒƒƒ| jjƒ‚d}~ww | jjdu�rnz6| jj  | jj¡ tƒ  ¡ dk�r'| jj  t jt jtd| jj ƒ¡ n| jj  t jt jtd| jjdƒ¡ W n4 t j�ym } z&dt|ƒ | j_ttƒ�rZttd| jj| jƒ tt	t
|ƒt|ƒƒƒ| jjƒ‚d}~ww |�rÀz| jjjj | jdd� | jj!�r‹| jj" j#d7  _#W n3 t�y¿ } z&dt|ƒ | j_ttƒ�r¬ttd| jj| jƒ tt	t
|ƒt|ƒƒƒ| jjƒ‚d}~ww | jj!�rÎ| jj" j$d7  _$d| j_%dS )z…
        Tries to open and connect a socket to a Server
        raise LDAPExceptionError if unable to open or connect socket
        Né   zsocket creation error: rw   FTzFUnable to bind to local address <%s> with source port <%s> due to <%s>z/socket connection error while locally binding: zmUnable to locally bind to local address <%s> with any of the source ports <%s> for connection <%s due to <%s>rv   z'socket connection error while opening: Úwindowsiè  ÚLLr   z5unable to set receive timeout for socket connection: )Údo_handshakers   zsocket ssl wrapping error: )&Úsocketr\   r�   ro   Ú
last_errorrJ   rK   rI   r   r   r�   Úunix_socket_availableÚfamilyr   Úsource_port_listÚbindÚsource_addressrN   r…   Úconnect_timeoutÚ
settimeoutÚconnectÚerrorÚreceive_timeoutr   ÚlowerÚ
setsockoptÚ
SOL_SOCKETÚSO_RCVTIMEOÚintr   ÚtlsÚwrap_socketr   r€   Úwrapped_socketsÚopen_socketsr|   )	rg   ÚaddressÚuse_sslru   r—   Úlast_bind_excÚsocket_bind_succeededÚsource_portÚbind_exri   ri   rj   rˆ   À   s’   €û
ÿ€ûÿ
€û$"€
€÷
€
€ü
zBaseStrategy._open_socketc                 C   s~   z
| j j tj¡ W n	 ty   Y nw z| j j ¡  W n	 ty%   Y nw d| j _d| j _| j jr=| j j j	d7  _	dS dS )z€
        Try to close a socket
        don't raise exception if unable to close socket, assume socket is already closed
        NTrs   )
r\   r¨   ÚshutdownÚ	SHUT_RDWRr�   r}   r|   r   r€   Úclosed_socketsr£   ri   ri   rj   rœ     s   ÿÿÿzBaseStrategy._close_socketc                 C   s   d| j _d S ©NF)r\   rš   r£   ri   ri   rj   r›   3  s   zBaseStrategy._stop_listenNc                 C   sö   d| j _| j jrc| j jr(|dvr(d| j _ttƒr"ttd| j j| j ƒ t| j jƒ‚| j j	 
¡ }tƒ }t|ƒ|d< tƒ  ||¡|d< t|ƒ}|durL||d< t |||¡| j _| j j| j|< |  |¡ |S d| j _ttƒruttd| j j| j ƒ t| j jƒ‚)	zE
        Send an LDAP message
        Returns the message_id
        N)ÚbindRequestz=cannot send operation requests while SASL bind is in progressrw   Ú	messageIDÚ
protocolOpÚcontrolsz*unable to send message, socket is not open)r\   rž   rš   Úsasl_in_progressr©   rJ   rK   rI   r   r…   Únext_message_idr!   r#   r"   ÚsetComponentByNamerC   r[   Údecode_requestr]   Úsendingr   )rg   Úmessage_typerž   rÊ   Ú
message_idÚldap_messageÚmessage_controlsri   ri   rj   Úsend6  s,   
ûzBaseStrategy.sendc              	   C   s  |du rt dƒ}d}d}| j�rë|| jv �rë|  ||¡}|s-ttƒr)ttd| jƒ tdƒ‚|tkr\z|  	¡  W n t
jtfyC   Y nw d| j_ttƒrVttd| jj| jƒ t| jjƒ‚|tkrxd| j_ttƒrrttd| jj| jƒ t| jjƒ‚|d d	 tkrº| jjr�| jj jd
7  _| jjrº|  | j| |d d ¡\}}|dur¯||g }| t¡ n|dur·|tg}g | _|rÎ|d }|dd… }d| j_d| j_| jj�r|�r|d	 tv�rttƒréttd|| jƒ | j |¡ |  ¡ | j_t!|d	 |d |d |d |d d�‚| jj"�r_t#| dƒ�s_t$dd„ |D ƒƒ�r_|  ¡ | _%|dd… }	|  &| j| |¡�r]|	D ]#}
|
d dk�rVdd„ |
d D ƒ}|D ]}|
d |= |
d |= �qH�q4|	}| j%}| `%| jj'�rä|D ]}}|d dk�râ| j| d D ],}||d v�r¡|t(t)t*fv�r¡t+ƒ |d |< t+ƒ |d |< ttƒ�r¡ttd|| ƒ �qv| jj"�sâg }|d D ]}d| ,¡ v �rÄ| -d¡\}}}| |¡ �q®|D ]}ttƒ�rÖttd|| ƒ |d |= |d |= �qÈ�qf| j |¡}nttƒ�r÷ttd| jƒ tdƒ‚|�r|||fS ||fS ) aŠ  
        Get response LDAP messages
        Responses are returned by the underlying connection strategy
        Check if message_id LDAP message is still outstanding and wait for timeout to see if it appears in _get_response
        Result is stored in connection.result
        Responses without result is stored in connection.response
        A tuple (responses, result) is returned
        NÚRESPONSE_WAITING_TIMEOUTz0socket timeout, no response from server for <%s>zno response from serverzsession terminated by serverrw   ztransaction errorrt   Úresultrs   Ú	referralszoperation result <%s> for <%s>ÚdescriptionÚdnÚmessager�   )rÖ   rØ   rÙ   rÚ   Úresponse_typeÚ_auto_range_searchingc                 s   s2   � | ]}d |v r|d  D ]	}d|v rdV  qqdS )Úraw_attributesú;range=TNri   )Ú.0ÚrespÚnameri   ri   rj   Ú	<genexpr>—  s   €0 z,BaseStrategy.get_response.<locals>.<genexpr>ÚsearchResEntryc                 S   s   g | ]}d |v r|‘qS )rÞ   ri   )rß   Úkeyri   ri   rj   Ú
<listcomp>�  s    z-BaseStrategy.get_response.<locals>.<listcomp>rÝ   Ú
attributesz>attribute set to empty list for missing attribute <%s> in <%s>z;rangeú;ziattribute type <%s> removed in response because of same attribute returned as range by the server in <%s>z,message id not in outstanding queue for <%s>z#message id not in outstanding queue).r
   r]   Ú_get_responserJ   rK   rI   r\   r   ÚSESSION_TERMINATED_BY_SERVERr}   r¨   r²   r   r©   r   rY   r   r   r   r€   Úreferrals_receivedÚauto_referralsÚdo_operation_on_referralrŽ   ÚRESPONSE_COMPLETEr^   rÖ   rŸ   Úraise_exceptionsr   rM   ÚpopÚcopyr   Ú
auto_rangeÚhasattrÚanyrÜ   Údo_search_on_auto_rangeÚempty_attributesr   r   r   Úlistr´   Ú	partition)rg   rÑ   ÚtimeoutÚget_requestrŸ   rÖ   Ú	responsesÚref_responseÚ
ref_resultÚtemp_responserà   Úkeysrä   ÚentryÚattribute_typeÚattrs_to_removeÚ	orig_attrÚ_rž   ri   ri   rj   Úget_responseT  s¶   	ÿ
&*

€

€

€

€

zBaseStrategy.get_responsec                 C   s    t | tƒr	t| ƒ} d}t| ƒdkrN| d dkr| d d }|S | d d }t| ƒ|d krNd}|}| dd| … D ]}|d8 }||d|  7 }q9|d | }|S )z�
        Compute LDAP Message size according to BER definite length rules
        Returns -1 if too few data to compute message length
        éÿÿÿÿr   rs   é   é€   r   é   )Ú
isinstancero   Ú	bytearrayr�   )ÚdataÚ	ret_valueÚbytes_lengthÚvalue_lengthÚcontÚbyteri   ri   rj   Úcompute_ldap_message_sizeÆ  s    
÷z&BaseStrategy.compute_ldap_message_sizec                 C   sŒ  |  d¡ ¡ }|d  ¡ }|d  ¡ r|d nd}|dkr0t|d ƒ d¡s+t|ƒ}nzt|ƒ}nu|dkrDt|| j	j
j| j	j
j| j	jƒ}na|dkrMt|ƒ}nX|d	krVt|ƒ}nO|d
kr_t|ƒ}nF|dkrht|ƒ}n=|dkrqt|ƒ}n4|dkrzt|ƒ}n+|dkrƒt|ƒ}n"|dkrŒt|ƒ}n|dkr•t|ƒ}nttƒr¡ttd|| j	ƒ tdƒ‚||d< |rÄtƒ |d< |D ]}|  |¡}|d |d |d < q²|S )z8
        Convert received LDAPMessage to a dict
        rÉ   rÊ   NÚbindResponseÚ	matchedDNó   NTLMrã   ÚsearchResDoneÚsearchResRefÚmodifyResponseÚaddResponseÚdelResponseÚmodDNResponseÚcompareResponseÚextendedRespÚintermediateResponseúunknown response <%s> for <%s>úunknown responser�   rs   r   )ÚgetComponentByNameÚgetNameÚgetComponentÚhasValueÚbytesÚ
startswithr1   r3   r+   r\   r…   ÚschemaÚcustom_formatterÚcheck_namesr*   r)   r(   r%   r@   r?   r6   r9   r:   rJ   rK   rI   r   r~   Údecode_control)rg   rÒ   rÐ   Ú	componentrÊ   rÖ   ÚcontrolÚdecoded_controlri   ri   rj   Údecode_responseß  sJ   

 










zBaseStrategy.decode_responsec                 C   s  |d dkr#|d d d   d¡st|d ƒ}nt|d ƒ}d|d< nÇ|d dkr?t|d | jjj| jjj| jjƒ}d	|d< n«|d d
krPt	|d ƒ}d|d< nš|d dkrat
|d ƒ}d|d< n‰|d dkrrt	|d ƒ}d|d< nx|d dkrƒt	|d ƒ}d|d< ng|d dkr”t	|d ƒ}d|d< nV|d dkr¥t	|d ƒ}d|d< nE|d dkr¶t	|d ƒ}d|d< n4|d dkrÇt|d ƒ}d|d< n#|d dkrØt|d ƒ}d|d< nttƒrættd|d | jƒ tdƒ‚|d �rtƒ |d< |d D ]}|  |d ¡}|d |d |d < qø|S ) zN
        Convert received LDAPMessage from fast ber decoder to a dict
        rÉ   rs   Úpayloadr¤   r  r  r�   rv   rã   é   r  é   r  é   r  é	   r  é   r  é   r  é   r  é   r  é   r  r  r  rÊ   r   )r%  r4   r5   r-   r\   r…   r&  r'  r(  rS   r.   r;   r<   rJ   rK   rI   r   r~   Údecode_control_fast)rg   rÒ   rÖ   r+  r,  ri   ri   rj   Údecode_response_fast  sX   
"











z!BaseStrategy.decode_response_fastc                 C   s   t | d ƒ}t| d ƒ}t| d ƒ}d}|dkr6tj|tƒ d�\}}tƒ }t|d ƒ|d< t|d ƒ|d< n?|d	krXtj|tƒ d�\}}tƒ }t|d
 ƒ|d< t|d ƒ|d< n|dks`|dkrutj|t	ƒ d�\}}tƒ }t
|d ƒ|d< |r„ttƒr€ttdƒ tdƒ‚|t |d¡||dœfS )úØ
        decode control, return a 2-element tuple where the first element is the control oid
        and the second element is a dictionary with description (from Oids), criticality and decoded control value
        ÚcontrolTypeÚcriticalityÚcontrolValueNú1.2.840.113556.1.4.319)Úasn1SpecÚsizeÚcookieú1.2.840.113556.1.4.841ÚMoreResultsÚmore_resultsÚCookieServerú1.3.6.1.1.13.1ú1.3.6.1.1.13.2ræ   rÖ   z)unprocessed control response in substrateÚ ©rØ   r<  Úvalue)ro   Úboolr$  rR   ÚdecoderG   r~   r¸   rH   r$   r/   rJ   rK   rI   r   rF   Úget)r+  Úcontrol_typer<  Úcontrol_valueÚunprocessedÚcontrol_respri   ri   rj   r)  >  s.   
zBaseStrategy.decode_controlc                 C   sf  t t| d d |d�ƒ}d}d}| dd… D ]}|d dkr#|d }q|d dkr+dnd	}q|d
krZt|dt|ƒƒ}tƒ }t|d d d d ƒ|d< t|d d d d ƒ|d< nM|dkr†t|dt|ƒƒ}tƒ }|d d d d rud	nd|d< |d d d d |d< n!|dksŽ|dkr§t|dt|ƒƒ}tƒ }t|d d d d ƒ|d< |t 	|d¡||dœfS )r:  r   r¤   )Úfrom_serverFNrs   r   rv   Tr>  r@  rA  rB  rD  rF  rG  rÖ   rH  rI  )
ro   rU   rT   r�   r~   r¸   r$  r0   rF   rM  )r+  rR  rN  r<  rO  ÚrrQ  ri   ri   rj   r8  \  s,   
 z BaseStrategy.decode_control_fastc                 C   sâ   | dkr	t |ƒ}n^| dkrtƒ }nV| dkrt|ƒ}nM| dkr#t|ƒ}nD| dkr,t|ƒ}n;| dkr5t|ƒ}n2| dkr>t|ƒ}n)| dkrGt|ƒ}n | d	krPt|ƒ}n| d
krYt	|ƒ}nt
tƒrcttd| ƒ tdƒ‚| |d< ||d< |S )NrÇ   ÚunbindRequestÚ
addRequestÚcompareRequestÚ
delRequestÚextendedReqÚmodifyRequestÚmodDNRequestÚsearchRequestÚabandonRequestzunknown request <%s>zunknown requestr�   rÊ   )r2   r~   r&   r7   rA   r8   r'   r>   r,   rD   rJ   rK   rI   r   )rÐ   r*  rÊ   rÖ   ri   ri   rj   rÎ   z  s4   








zBaseStrategy.decode_requestc                 C   st   g }|D ]3}t |ƒ}|r7| jjjD ]%}|d |d ks!|d dkr6|d | jvr6|d  |d< | |¡  nqq|S )Nr   ÚhostÚ*rs   ÚanonymousBindOnly)r    r\   r…   Úallowed_referral_hostsr^   rŽ   )rg   r×   Úreferral_listÚreferralÚcandidate_referralÚref_hostri   ri   rj   Úvalid_referral_list›  s   
€z BaseStrategy.valid_referral_listc              	   C   s¬  d}|}|sÔ|  d¡\}}}|  d¡\}}}	|d |  |d | 7  < |d |  |d | 7  < |	dkrÎttƒrHttdtt|	ƒd ƒ| jƒ |d tt|	ƒd ƒ d	 }
| jj|d
 dt|d |d tt|	ƒd ƒ d	 gd�}| jj	j
r~|\}}}}n
|}| jj}| jj}| jj	jr—|r”|d }nd}n|  |¡\}}|d }|sÎ|
|d v r¾t|d |
 ƒdkr¾|d |
= |d |
= ttdd„ |d  ¡ ƒƒd }qd}|rd S d S )NFrÞ   ú-rÝ   ræ   r^  z2performing next search on auto-range <%s> via <%s>rs   z-*rÙ   z(objectclass=*)ÚdereferenceAlias)Úsearch_baseÚsearch_filterÚsearch_scopeÚdereference_aliasesræ   r   Tc                 S   s   d| v S )NrÞ   ri   )Úari   ri   rj   Ú<lambda>Î  s    z3BaseStrategy.do_next_range_search.<locals>.<lambda>)r÷   rJ   rM   rI   ro   r¸   r\   Úsearchr   r¡   rd   rÖ   rŸ   r_   r  r�   rö   Úfilterrþ   )rg   rž   rŸ   Ú	attr_nameÚdoneÚcurrent_responseÚ	attr_typer  Úreturned_rangeÚ
high_rangeÚrequested_rangerÖ   ÚstatusÚ	_responseri   ri   rj   Údo_next_range_search©  sH   ü


 

Üz!BaseStrategy.do_next_range_searchc                 C   s¸   dd„ |D ƒD ]R}t |d  ¡ ƒD ]G}d|v rX| d¡\}}}|dv r'  dS ||d vs5|d | d u r<t ƒ |d |< ||d vsJ|d | d u rQt ƒ |d |< |  |||¡ qqdS )	Nc                 S   s   g | ]
}|d  dkr|‘qS )r�   rã   ri   )rß   rS  ri   ri   rj   rå   Ó  s    z8BaseStrategy.do_search_on_auto_range.<locals>.<listcomp>rÝ   rÞ   )z1-1z0-0Fræ   T)rö   rþ   r÷   ry  )rg   rž   rŸ   rà   rp  rs  r  Úrange_valuesri   ri   rj   rô   Ò  s   €÷
z$BaseStrategy.do_search_on_auto_rangec           
         sP  d }d }d }ˆ   |¡}|�r#‡ fdd„|D ƒ}|rt|ƒnt|ƒ}|d |d p,ˆ jjj|d f}ˆ jjr@|ˆ jv r@ˆ j| }nÕt|d |d pLˆ jjj|d ˆ jjjˆ jjj	ˆ jjj
ˆ jjjˆ jjj|d r„tˆ jjjjˆ jjjjˆ jjjjˆ jjjjˆ jjjjd�nd d�	}dd	lm} |||d
 s˜ˆ jjnd |d
 s¡ˆ jjnd ˆ jj|d
 s­ˆ jjnttdˆ jjˆ jjˆ jjˆ jjˆ jjˆ jj ˆ jj!d�}ˆ jj"rÕˆ jj# j$d7  _$| %¡  ˆ j&|j'_&ˆ jj(�r|j)�s| *¡ �sd|j+rõd|j+ nd }	t,t-ƒ�rt.t-d|	ˆ ƒ ˆ  /¡  t0|	ƒ‚ˆ jj1�r| 2¡  ˆ jj"�r#ˆ jj# j3d7  _3|||fS )Nc                    s"   g | ]}|d  ˆ j jjkr|‘qS )r‰   )r\   r…   r‰   )rß   rb  r£   ri   rj   rå   æ  s    ÿz;BaseStrategy.create_referral_connection.<locals>.<listcomp>r]  Úportr‰   )Úlocal_private_key_fileÚlocal_certificate_fileÚvalidateÚversionÚca_certs_file)	r]  r{  r¾   Úget_infoÚ	formatterr¯   Úmoder`  r¹   r   )Ú
Connectionr_  T)r…   ÚuserÚpasswordr  ÚauthenticationÚclient_strategyrë   Ú	read_onlyr(  rî   Úfast_decoderr³   Úsasl_mechanismÚsasl_credentialsrs   z$start_tls in referral not successfulrn   rH  z%s for <%s>)4re  r   r\   r…   r{  Úuse_referral_cacherc   r=   r�  r'  r¯   rƒ  r`  rE   r¹   Úprivate_key_fileÚcertificate_filer~  r  r€  Úcore.connectionr„  r…  r†  r‡  r	   r   r‰  r(  rî   rŠ  r³   r‹  rŒ  r   r€   Úreferrals_connectionsr˜   r^   r¡   r    r‰   Ú	start_tlsr©   rJ   rK   rI   Úunbindr   r�   r­   Úreferrals_followed)
rg   r×   Úreferral_connectionÚselected_referralÚcachekeyre  Úpreferred_referral_listÚreferral_serverr„  r²   ri   r£   rj   Úcreate_referral_connectionà  sz   
ÿ û



üóó





z'BaseStrategy.create_referral_connectionc                 C   sJ  t tƒrttd| jƒ |  |¡\}}}|�r|d dkrM|j|d p%|d |d p,|d |d p3|d |d |d p=|d |d	 |d
 |d |d d�	 n´|d dkrg|j|d p\|d d |d |d d� nš|d dkrƒ|j|d pv|d |d |d |d d� n~|d dkr™|j|d p’|d |d d� nh|d dkr¯|j	|d |d |d dd� nR|d dkrÊ|j
|d p¾|d t|d ƒ|d d� n7|d dkré|j|d pÙ|d |d |d |d |d d� nd| j_t tƒrûttd| jj| jƒ t| jjƒ‚|j}|j}| jj�r|| j|< ||fS | ¡  ||fS d }d }||fS ) Nzfollowing referral for <%s>r�   r[  Úbasero  Úscoperg  ræ   Ú	sizeLimitÚ	timeLimitÚ	typesOnlyrÊ   )rÊ   rU  rÿ   rV  Ú	attributerJ  rW  rX  rá   T)rÊ   Ú	no_encoderY  ÚchangesrZ  ÚnewRdnÚdeleteOldRdnÚnewSuperiorz referral operation not permittedrw   )rJ   rM   rI   r\   rš  rn  ÚaddÚcompareÚdeleteÚextendedÚmodifyrB   Ú	modify_dnr©   rK   r   rŸ   rÖ   r�  rc   r“  )rg   rž   r×   r–  r•  r—  rŸ   rÖ   ri   ri   rj   rì   $  s~   ø	ýýÿ
ý
þü

ûýz%BaseStrategy.do_operation_on_referralc              
   C   s¨  t tƒrttd| jƒ z€t|ƒ}| jjtkrf| jjrf| jjsf| jj	}| jj}t
 t|t|ƒ dd¡| ƒdd… ¡}tt|ƒd d d ƒ dd¡| | tdƒ dd¡ t|ƒ dd¡ }| j j	d7  _	| jj |¡ t tƒr|ttd	| jt|d
ƒƒ t tƒrŠttdt|ƒ| jƒ W n4 tjy¿ } z'dt|ƒ | j_d }t tƒr¬ttd| jj| jƒ ttt|ƒt|ƒƒƒ| jjƒ‚d }~ww | jjrÒ| jj | jjt|ƒ¡ d S d S )Nzsending 1 ldap message for <%s>rv   Úbigr   é   r   é
   rs   zldap message sent via <%s>:%sz>>zsent %d bytes via <%s>zsocket sending errorrw   )rJ   rN   rI   r\   rQ   r‹  r   Ú_digest_md5_kicrË   Ú_digest_md5_sec_numr$  ÚfromhexrW   r¸   Úto_bytesr�   r¨   ÚsendallrO   rP   r²   ro   r©   rK   r   r   r�   r   r€   Úupdate_transmitted_messagerž   )rg   rÒ   Úencoded_messageÚsec_numÚkicÚ	signaturer—   ri   ri   rj   rÏ   b  s6   (H€€úÿzBaseStrategy.sendingc                 C   ó   t ‚©N©ÚNotImplementedErrorr£   ri   ri   rj   r‘   €  ó   zBaseStrategy._start_listenc                 C   r¹  rº  r»  )rg   rÑ   rø   ri   ri   rj   rè   „  r½  zBaseStrategy._get_responsec                 C   r¹  rº  r»  r£   ri   ri   rj   Ú	receivingˆ  r½  zBaseStrategy.receivingc                 C   r¹  rº  r»  ©rg   rÑ   ri   ri   rj   Úpost_send_single_responseŒ  r½  z&BaseStrategy.post_send_single_responsec                 C   r¹  rº  r»  r¿  ri   ri   rj   Úpost_send_search�  r½  zBaseStrategy.post_send_searchc                 C   r¹  rº  r»  r£   ri   ri   rj   Ú
get_stream”  ó   zBaseStrategy.get_streamc                 C   r¹  rº  r»  )rg   rJ  ri   ri   rj   Ú
set_stream—  rÃ  zBaseStrategy.set_streamc                 C   s:   t | jƒdkr| j ¡ \}}| ¡  t | jƒdksd S d S )Nr   )r�   rc   Úpopitemr“  )rg   r—  r•  ri   ri   rj   Úunbind_referral_cacheš  s   þz"BaseStrategy.unbind_referral_cache)TT)FFrº  rÆ   )T)"rf   Ú
__module__Ú__qualname__Ú__doc__rk   rr   r˜   r}   rˆ   rœ   r›   rÔ   r  Ústaticmethodr  r-  r9  r)  r8  rÎ   re  ry  rô   rš  rì   rÏ   r‘   rè   r¾  rÀ  rÁ  rÂ  rÄ  rÆ  ri   ri   ri   rj   r[   I   sF    

<
]

r
,3
 )D>r[   )xrÉ  r¨   r   rª   ÚImportErrorÚstructr   Úplatformr   Úrandomr   rH  r   r	   r
   r   r   r   r   r   Úcore.resultsr   r   Úcore.exceptionsr   r   r   r   r   r   r   r   r   r   r   r   r   r   Ú	utils.urir    Úprotocol.rfc4511r!   r"   r#   r$   Úoperation.addr%   r&   Úoperation.modifyr'   r(   Úoperation.searchr)   r*   r+   r,   r-   r.   r/   r0   Úoperation.bindr1   r2   r3   r4   r5   Úoperation.comparer6   r7   Úoperation.extendedr8   r9   r:   r;   r<   Úcore.serverr=   Úoperation.modifyDnr>   r?   Úoperation.deleter@   rA   Úprotocol.convertrB   rC   Úoperation.abandonrD   Úcore.tlsrE   Úprotocol.oidrF   Úprotocol.rfc2696rG   Úprotocol.microsoftrH   Ú	utils.logrI   rJ   rK   rL   rM   rN   rO   rP   Ú
utils.asn1rQ   rR   rS   rT   Ú
utils.convrU   Úprotocol.sasl.digestMd5rV   rW   ré   rY   rí   Úobjectr[   ri   ri   ri   rj   Ú<module>   sN    ÿ(@((