B
    ß%Q[$. ã               @   s$  d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	 ddl
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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'm(Z(m)Z)m*Z*m+Z+m,Z, d	d
l-m.Z.m/Z/m0Z0 ddl1m2Z2 d	dl3m4Z4 d	dl5m6Z6 ddl7m8Z8m9Z9 ddl:m;Z;m<Z< ddl=m>Z>m?Z? ddl@mAZAmBZB ddlCmDZDmEZE ddlFmGZGmHZH ddlImJZJmKZK ddlLmMZMmNZN ddlOmPZPmQZQ ddlRmSZSmTZT ddlUmVZV ddlWmXZX ddlYmZZZ ddl[m\Z\ ddl]m^Z^ ddl_m`Z` ddlambZb ddlcmdZd dd lemfZf dd!lgmhZh dd"limjZj dd#lkmlZl dd$lmmnZn d	d%lompZp d	d&lqmrZr d	d'lsmtZtmuZumvZvmwZwmxZxmyZymzZzm{Z{m|Z|m}Z}m~Z~mZm€Z€ dd(l�m‚Z‚mƒZƒm„Z„m…Z…m†Z† dd)l‡mˆZˆm‰Z‰mŠZŠm‹Z‹mŒZŒm�Z�mŽZŽ dd*l�m�Z� e)e*e+e,gZ‘eeeeee&e'egZ’d+d,„ Z“d-d.„ Z”G d/d0„ d0e•ƒZ–dS )1Ú
é    )Údeepcopy)Úlinesep)ÚRLockÚLock)ÚreduceNé   )!Ú	ANONYMOUSÚSIMPLEÚSASLÚ
MODIFY_ADDÚMODIFY_DELETEÚMODIFY_REPLACEÚget_config_parameterÚDEREF_ALWAYSÚSUBTREEÚASYNCÚSYNCÚNO_ATTRIBUTESÚALL_ATTRIBUTESÚALL_OPERATIONAL_ATTRIBUTESÚMODIFY_INCREMENTÚLDIFÚASYNC_STREAMÚRESTARTABLEÚROUND_ROBINÚREUSABLEÚAUTO_BIND_NONEÚAUTO_BIND_TLS_BEFORE_BINDÚAUTO_BIND_TLS_AFTER_BINDÚAUTO_BIND_NO_TLSÚSTRING_TYPESÚSEQUENCE_TYPESÚ	MOCK_SYNCÚ
MOCK_ASYNCÚNTLMÚEXTERNALÚ
DIGEST_MD5ÚGSSAPIÚPLAINé   )ÚRESULT_SUCCESSÚRESULT_COMPARE_TRUEÚRESULT_COMPARE_FALSE)ÚExtendedOperationsRoot)Ú
ServerPool)ÚServer)Úabandon_operationÚabandon_request_to_dict)Úadd_operationÚadd_request_to_dict)Úbind_operationÚbind_request_to_dict)Úcompare_operationÚcompare_request_to_dict)Údelete_operationÚdelete_request_to_dict)Úextended_operationÚextended_request_to_dict)Úmodify_operationÚmodify_request_to_dict)Úmodify_dn_operationÚmodify_dn_request_to_dict)Úsearch_operationÚsearch_request_to_dict)Úoperation_to_ldifÚadd_ldif_header)Úsasl_digest_md5)Úsasl_external)Ú
sasl_plain)ÚSyncStrategy)ÚMockAsyncStrategy)ÚAsyncStrategy)ÚReusableStrategy)ÚRestartableStrategy)ÚLdifProducerStrategy)ÚMockSyncStrategy)ÚAsyncStreamStrategy)Úunbind_operation)Úpaged_search_control)ÚConnectionUsage)ÚTls)ÚLDAPUnknownStrategyErrorÚLDAPBindErrorÚ$LDAPUnknownAuthenticationMethodErrorÚ"LDAPSASLMechanismNotSupportedErrorÚLDAPObjectClassErrorÚLDAPConnectionIsReadOnlyErrorÚLDAPChangeErrorÚLDAPExceptionErrorÚLDAPObjectErrorÚLDAPSocketReceiveErrorÚLDAPAttributeErrorÚLDAPInvalidValueErrorÚLDAPConfigurationError)Úescape_bytesÚprepare_for_streamÚcheck_json_dictÚformat_jsonÚ
to_unicode)ÚlogÚlog_enabledÚERRORÚBASICÚPROTOCOLÚEXTENDEDÚ#get_library_log_hide_sensitive_data)Úsafe_dnc             C   s~   | r,t | ƒdkr,t| d ƒd t| d ƒ S | r\t | ƒdkr\dt| d ƒ d t| d ƒ S yt| ƒS  tk
rx   dS X d S )	Nr   r   ú:r*   é   ú[z]:ú?)ÚlenÚstrÚ	Exception)Zendpoint© ru   úXC:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6i93dh7p\ldap3\ldap3\core\connection.pyÚ_format_socket_endpointY   s     rw   c             C   sp   | rly|   ¡ }W n tk
r(   d}Y nX y|  ¡ }W n tk
rN   d}Y nX dt|ƒ d t|ƒ d S dS )N)NNNNz<local: z - remote: ú>z<no socket>)Úgetsocknamert   Úgetpeernamerw   )ÚsockÚlocalÚremoteru   ru   rv   Ú_format_socket_endpointse   s    

r~   c               @   s†  e Zd ZdZddeddedddddddddddddddddddfdd„Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
edd„ ƒZejdd„ ƒZedd„ ƒZdd„ Zdd„ ZdEdd„ZdFdd„ZdGdd„Zeeddddddddddfd d!„ZdHd"d#„ZdId$d%„ZdJd&d'„ZdKd(d)„ZdLd*d+„ZdMd,d-„ZdNd.d/„ZdOd0d1„Zd2d3„ Zd4d5„ Z d6d7„ Z!dPd8d9„Z"dQd;d<„Z#dRd=d>„Z$dSd?d@„Z%edAdB„ ƒZ&dCdD„ Z'dS )TÚ
Connectiona�  Main ldap connection class.

    Controls, if used, must be a list of tuples. Each tuple must have 3
    elements, the control OID, a boolean meaning if the control is
    critical, a value.

    If the boolean is set to True the server must honor the control or
    refuse the operation

    Mixing controls must be defined in controls specification (as per
    RFC 4511)

    :param server: the Server object to connect to
    :type server: Server, str
    :param user: the user name for simple authentication
    :type user: str
    :param password: the password for simple authentication
    :type password: str
    :param auto_bind: specify if the bind will be performed automatically when defining the Connection object
    :type auto_bind: int, can be one of AUTO_BIND_NONE, AUTO_BIND_NO_TLS, AUTO_BIND_TLS_BEFORE_BIND, AUTO_BIND_TLS_AFTER_BIND as specified in ldap3
    :param version: LDAP version, default to 3
    :type version: int
    :param authentication: type of authentication
    :type authentication: int, can be one of AUTH_ANONYMOUS, AUTH_SIMPLE or AUTH_SASL, as specified in ldap3
    :param client_strategy: communication strategy used in the Connection
    :type client_strategy: can be one of STRATEGY_SYNC, STRATEGY_ASYNC_THREADED, STRATEGY_LDIF_PRODUCER, STRATEGY_SYNC_RESTARTABLE, STRATEGY_REUSABLE_THREADED as specified in ldap3
    :param auto_referrals: specify if the connection object must automatically follow referrals
    :type auto_referrals: bool
    :param sasl_mechanism: mechanism for SASL authentication, can be one of 'EXTERNAL', 'DIGEST-MD5', 'GSSAPI', 'PLAIN'
    :type sasl_mechanism: str
    :param sasl_credentials: credentials for SASL mechanism
    :type sasl_credentials: tuple
    :param check_names: if True the library will check names of attributes and object classes against the schema. Also values found in entries will be formatted as indicated by the schema
    :type check_names: bool
    :param collect_usage: collect usage metrics in the usage attribute
    :type collect_usage: bool
    :param read_only: disable operations that modify data in the LDAP server
    :type read_only: bool
    :param lazy: open and bind the connection only when an actual operation is performed
    :type lazy: bool
    :param raise_exceptions: raise exceptions when operations are not successful, if False operations return False if not successful but not raise exceptions
    :type raise_exceptions: bool
    :param pool_name: pool name for pooled strategies
    :type pool_name: str
    :param pool_size: pool size for pooled strategies
    :type pool_size: int
    :param pool_lifetime: pool lifetime for pooled strategies
    :type pool_lifetime: int
    :param use_referral_cache: keep referral connections open and reuse them
    :type use_referral_cache: bool
    :param auto_escape: automatic escaping of filter values
    :param auto_encode: automatic encoding of attribute values
    :type use_referral_cache: bool
    Né   TFc          	   C   sÖ  t dƒ}tƒ | _| j��´ |tkrJd| _ttƒr@ttd| j| ƒ t| jƒ‚|| _	|| _
|| _|sn| j
rnt| _nL|szt| _n@|ttttgkr’|| _n(d| _ttƒr°ttd| j| ƒ t| jƒ‚|| _|rÈdnd| _d | _d | _d | _d| _d| _d| _d | _|dk�r
t| _n|dk�rt| _n|| _|
| _|| _|�r:tƒ nd | _ d | _!d| _"d| _#|| _$g | _%d| _&d| _'d| _(d | _)d| _*|| _+|�rŒ|n|| _,|| _-|| _.|| _/d| _0|| _1|| _2|	�rÀdnd| _3t4| ƒ| _5g | _6|| _7|| _8|| _9|| _:|| _;|| _<t=|t>ƒ�rt?|ƒ}t=|t@ƒ�r*tA|tBddd�}t=|tAƒ�rX|| _C| jC D| ¡ | jC E| ¡| _Fnd | _C|| _F| j	tGk�r|tH| ƒ| _InØ| j	tJk�r”tK| ƒ| _InÀ| j	tLk�r¬tM| ƒ| _In¨| j	tNk�rÄtO| ƒ| _In�| j	tPk�râtQ| ƒ| _Id| _+nr| j	tRk�rútS| ƒ| _InZ| j	tTk�rtU| ƒ| _InB| j	tVk�r*tW| ƒ| _In*d| _ttƒ�rJttd| j| ƒ t| jƒ‚| jIjX| _X| jIjY| _Y| jIjZ| _Z| jIj[| _[| jIj\| _\| jIj]�s˜|  ^¡  tt_ƒ�rÈt`ƒ �r¼tt_d	|  a¡ ƒ ntt_d
| ƒ W d Q R X d S )NÚDEFAULT_THREADED_POOL_NAMEz"unknown client connection strategyz%s for <%s>zunknown authentication methodTF)ÚactiveZexhaustzunknown strategyzinstantiated Connection: <%s>zinstantiated Connection: <%r>)br   r   Úconnection_lockÚCLIENT_STRATEGIESÚ
last_errorrg   rh   rf   rT   Ústrategy_typeÚuserÚpasswordr
   Úauthenticationr	   r   r%   rV   ÚversionÚauto_referralsÚrequestÚresponseÚresultÚboundÚ	listeningÚclosedr   Ú	auto_bindr    Úsasl_mechanismÚsasl_credentialsrR   Ú_usageÚsocketÚtls_startedÚsasl_in_progressÚ	read_onlyÚ_context_stateÚ_deferred_openÚ_deferred_bindÚ_deferred_start_tlsÚ_bind_controlsÚ_executing_deferredÚlazyÚ	pool_nameÚ	pool_sizeÚpool_lifetimeÚpool_keepaliveZstarting_tlsÚcheck_namesÚraise_exceptionsÚ
auto_ranger.   ÚextendÚ_entriesÚfast_decoderÚreceive_timeoutÚempty_attributesÚuse_referral_cacheÚauto_escapeÚauto_encodeÚ
isinstancer!   r0   r"   r/   r   Úserver_poolÚ
initializeZget_current_serverÚserverr   rH   Ústrategyr   rJ   r   rM   r   rL   r   rK   r#   rN   r$   rI   r   rO   ÚsendÚopenÚget_responseÚpost_send_single_responseÚpost_send_searchZno_real_dsaÚdo_auto_bindri   rl   Ú!repr_with_sensitive_data_stripped)Úselfr³   r‡   rˆ   r’   rŠ   r‰   Zclient_strategyr‹   r§   r“   r”   r¥   Zcollect_usager™   r    r¦   r¡   r¢   r£   rª   r«   Zreturn_empty_attributesr­   r®   r¯   r¤   Úconf_default_pool_nameru   ru   rv   Ú__init__­   sÖ    
















zConnection.__init__c             C   sÚ   | j rÖ| j tkrÖttƒr$ttd| ƒ | jr6| jdd� | j tkrN| jdd� nF| j t	krr| j
dd� | jdd� n"| j tkr”| jdd� | j
dd� | jsÖd| jr¬d| j nd | _ttƒrÌttd| j| ƒ t| jƒ‚d S )	Nz"performing automatic bind for <%s>F)Úread_server_infoTzautomatic bind not successfulz - Ú z%s for <%s>)r’   r   rg   ri   rf   r‘   r¶   r    Úbindr   Ú	start_tlsr   r�   r…   rh   rU   )r¼   ru   ru   rv   rº   K  s$    


zConnection.do_auto_bindc             C   s°   | j rt| j ƒnddt| jƒ | jr(dnd| js4dn| jr>dnd| jrJdn| jrTd	nd
t| j	ƒ| j
shdn| jrrdnd| jr~dndt| dƒr”| jjjnd| jr dndg
}d |¡S )NÚNonezuser: r    znot lazyÚunboundzdeferred bindr�   r‘   zdeferred openr¶   ztls not startedzdeferred start_tlsztls startedr�   znot listeningr´   zNo strategyzinternal decoderzpyasn1 decoderz - )r³   rs   r‡   r    r�   rœ   r‘   r›   r~   r–   r—   r�   r�   Úhasattrr´   Ú	__class__Ú__name__rª   Újoin)r¼   Úsru   ru   rv   Ú__str___  s    zConnection.__str__c             C   s`  t dƒ}| jrd | ¡}n
d | ¡}|| jd kr4dnd | ¡7 }|| jd krPdnd | ¡7 }|| jd krldnd | ¡7 }|| jd krˆdnd | ¡7 }|| jd kr¤dnd	 | ¡7 }|| jd krÀdnd
 | ¡7 }|| j	d krÜdnd | ¡7 }|| j
d krødnd | ¡7 }|| jd k�rdnd | ¡7 }|| jd k�r4dnd | ¡7 }|| jd k�rRdnd| j�r`dnd 7 }|| jd k�rzdnd | ¡7 }|| jd k�r˜dnd | ¡7 }|| jd k�r¶dnd | ¡7 }|| jd k�sÜ| j|k�ràdnd | ¡7 }|| jd k�rþdnd | ¡7 }|| jd k�rdnd | ¡7 }|| jd k�r:dnd | ¡7 }|| jd k�rXdnd| j�rfdnd 7 }|| jd k�r€dnd| j�rŽdnd 7 }|| jd k�r¨dnd | ¡7 }|| jd k�rÆdnd| j�rÔdnd 7 }|| jd k�rîdnd| j�rüdnd 7 }|| jd k�rdnd| j�r$dnd 7 }|| jd k�r>dnd| j�rLdnd 7 }|d 7 }|S )!Nr�   z#Connection(server={0.server_pool!r}zConnection(server={0.server!r}rÀ   z, user={0.user!r}z, password={0.password!r}z, auto_bind={0.auto_bind!r}z, version={0.version!r}z%, authentication={0.authentication!r}z%, client_strategy={0.strategy_type!r}z%, auto_referrals={0.auto_referrals!r}z%, sasl_mechanism={0.sasl_mechanism!r}z), sasl_credentials={0.sasl_credentials!r}z, check_names={0.check_names!r}z, collect_usage=ÚTrueÚFalsez, read_only={0.read_only!r}z, lazy={0.lazy!r}z), raise_exceptions={0.raise_exceptions!r}z, pool_name={0.pool_name!r}z, pool_size={0.pool_size!r}z#, pool_lifetime={0.pool_lifetime!r}z%, pool_keepalive={0.pool_keepalive!r}z, fast_decoder=z, auto_range=z', receive_timeout={0.receive_timeout!r}z, return_empty_attributes=z, auto_encode=z, auto_escape=z, use_referral_cache=ú))r   r±   Úformatr‡   rˆ   r’   rŠ   r‰   r†   r‹   r“   r”   r¥   Úusager™   r    r¦   r¡   r¢   r£   r¤   rª   r§   r«   r¬   r¯   r®   r­   )r¼   r½   Úrru   ru   rv   Ú__repr__n  s>    
(*((((((zConnection.__repr__c          	   C   s²  t dƒ}| jrd | ¡}n
d | ¡}|| jd kr4dnd | ¡7 }|| jd krPdnd dt| jƒ ¡7 }|| jd krvdnd | ¡7 }|| jd kr’dnd	 | ¡7 }|| jd kr®dnd
 | ¡7 }|| j	d krÊdnd | ¡7 }|| j
d krædnd | ¡7 }|| jd k�rdnd | ¡7 }| jtk�rb|| jd k�r.dn.d | jd | jd dt| jd ƒ | jd ¡7 }n|| jd k�rtdnd | ¡7 }|| jd k�r’dnd | ¡7 }|| jd k�r°dn| j�r¼dnd7 }|| jd k�rÔdnd | ¡7 }|| jd k�ròdnd | ¡7 }|| jd k�rdnd | ¡7 }|| jd k�s6| j|k�r:dnd | ¡7 }|| jd k�rXdnd | ¡7 }|| jd k�rvdnd | ¡7 }|| jd k�r”dnd | ¡7 }|| jd k�r²dn| j�r¾dnd7 }|| jd k�rÖdnd | j�räd!nd 7 }|| jd k�rþdnd" | ¡7 }|| jd k�rdn| j�r(d#nd7 }|| jd k�r@dnd$| j�rNd!nd 7 }|| jd k�rhdnd%| j�rvd!nd 7 }|| jd k�r�dnd&| j�ržd!nd 7 }|d'7 }|S )(Nr�   z#Connection(server={0.server_pool!r}zConnection(server={0.server!r}rÀ   z, user={0.user!r}z, password='{0}'z*<stripped %d characters of sensitive data>z, auto_bind={0.auto_bind!r}z, version={0.version!r}z%, authentication={0.authentication!r}z%, client_strategy={0.strategy_type!r}z%, auto_referrals={0.auto_referrals!r}z%, sasl_mechanism={0.sasl_mechanism!r}z/, sasl_credentials=({0!r}, {1!r}, '{2}', {3!r})r   r*   Ú*r   r€   z), sasl_credentials={0.sasl_credentials!r}z, check_names={0.check_names!r}z, collect_usage=TruerÌ   z, read_only={0.read_only!r}z, lazy={0.lazy!r}z), raise_exceptions={0.raise_exceptions!r}z, pool_name={0.pool_name!r}z, pool_size={0.pool_size!r}z#, pool_lifetime={0.pool_lifetime!r}z%, pool_keepalive={0.pool_keepalive!r}z, fast_decoder=Truez, auto_range=rË   z', receive_timeout={0.receive_timeout!r}z, return_empty_attributes=Truez, auto_encode=z, auto_escape=z, use_referral_cache=rÍ   )r   r±   rÎ   r‡   rˆ   rr   r’   rŠ   r‰   r†   r‹   r“   r'   r”   r¥   rÏ   r™   r    r¦   r¡   r¢   r£   r¤   rª   r§   r«   r¬   r¯   r®   r­   )r¼   r½   rÐ   ru   ru   rv   r»   ‘  sB    
&F$*$($(((z,Connection.repr_with_sensitive_data_strippedc             C   s   | j jr| j  ¡ S dS )z»Used by the LDIFProducer strategy to accumulate the ldif-change operations with a single LDIF header
        :return: reference to the response stream if defined in the strategy.
        N)r´   Ú
can_streamÚ
get_stream)r¼   ru   ru   rv   Ústream·  s    zConnection.streamc          	   C   s*   | j � | jjr| j |¡ W d Q R X d S )N)rƒ   r´   rÓ   Z
set_stream)r¼   Úvalueru   ru   rv   rÕ   ¾  s    c             C   s\   | j s
dS | jjrV| j  ¡  x$| jjjD ]}|  j |jj7  _ q(W |  j | jjj7  _ | j S )zKUsage statistics for the connection.
        :return: Usage object
        N)	r•   r´   ÚpooledÚresetÚpoolÚworkersÚ
connectionrÏ   Zterminated_usage)r¼   Zworkerru   ru   rv   rÏ   Ä  s    
zConnection.usagec          	   C   sF   | j �6 | j | j| jf¡ | jr*|  ¡  | js8|  ¡  | S Q R X d S )N)rƒ   rš   Úappendr�   r‘   r¶   rÁ   )r¼   ru   ru   rv   Ú	__enter__Ò  s    zConnection.__enter__c          	   C   sŠ   | j �z | j ¡ \}}|s | js&| jrHy|  ¡  W n tk
rF   Y nX |sZ| jrZ|  ¡  |d k	r|t	t
ƒrxtt
d|| ƒ dS W d Q R X d S )Nz%s for <%s>F)rƒ   rš   Úpopr�   rÕ   Úunbindr[   r‘   r¶   rg   rh   rf   )r¼   Úexc_typeÚexc_valÚexc_tbZcontext_boundZcontext_closedru   ru   rv   Ú__exit__Ý  s    
zConnection.__exit__c          	   C   sT  t tƒrttd| ƒ d| _| j��( | jrn| jsn| jjrD| j 	|¡ d| _
|| _d| _t tƒrjttd| ƒ �n¸d| _
d| _| jrŒ| jdd� | jtk�rt tƒr¬ttd| ƒ | jjsþt| j| j| jd| jd	�}t tƒrèttd
t|ƒ| ƒ |  |  d||¡¡}n| j 	|¡}�nÊ| jtk�r˜t tƒ�r0ttd| ƒ | jj�sˆt| j| j| j| j| jd	�}t tƒ�rrttdt|ƒ| ƒ |  |  d||¡¡}n| j 	|¡}�n@| jtk�r| jtk�rêt tƒ�rÆttd| ƒ | jj�sÜ|  |¡}n| j 	|¡}n*d| _t tƒ�r
ttd| j| ƒ t | jƒ‚nÂ| jt!k�r®| j�r‚| j�r‚t"| j #d¡ƒdk�r‚t tƒ�r^ttd| ƒ | jj�st|  $|¡}n| j 	|¡}n*d| _t tƒ�r¢ttd| j| ƒ t%| jƒ‚n*d| _t tƒ�rÎttd| j| ƒ t%| jƒ‚| jj&�s$| jj�s$| jtt!fk�r$|  '|¡\}}t tƒ�r˜ttd|| ƒ nt| jj&�rN| j(}t tƒ�r˜ttd|| ƒ nJ| jj�sh| jtt!fk�rn|}n*d| _t tƒ�rŽttd| j| ƒ t%| jƒ‚|dk�rªd| _nf|dk�r¼d| _nT|dk�rÎd| _nB|d t)k�ràdnd| _| j�s|�r|d �r| j�s|d | _|�r&| j�r&|  *¡  g | _+t tƒ�rDttd| jƒ | jS Q R X dS )a%  Bind to ldap Server with the authentication method and the user defined in the connection

        :param read_server_info: reads info from server
        :param controls: LDAP controls to send along with the bind operation
        :type controls: list of tuple
        :return: bool

        zstart BIND operation via <%s>NTzdeferring bind for <%s>F)r¿   z"performing anonymous BIND for <%s>rÀ   )r¯   z)anonymous BIND request <%s> sent via <%s>ÚbindRequestzperforming simple BIND for <%s>z&simple BIND request <%s> sent via <%s>zperforming SASL BIND for <%s>z&requested SASL mechanism not supportedz%s for <%s>ú\r   zperforming NTLM BIND for <%s>z)NTLM needs domain\username and a passwordzunknown authentication methodz-async BIND response id <%s> received via <%s>z$BIND response <%s> received via <%s>rŽ   Údescriptionz done BIND operation, result <%s>),rg   ri   rf   r…   rƒ   r    rŸ   r´   r×   Zvalidate_bindrœ   rž   r�   r‘   r¶   r‰   r	   rj   r5   rŠ   r‡   r¯   r6   r¸   rµ   r
   rˆ   r   r“   ÚSASL_AVAILABLE_MECHANISMSÚdo_sasl_bindrh   rW   r%   rr   ÚsplitÚdo_ntlm_bindrV   Úsyncr·   rŽ   r+   Úrefresh_server_infor©   )r¼   r¿   ÚcontrolsrŒ   r�   Ú_rŽ   ru   ru   rv   rÁ   î  s²    






&




$







 

zConnection.bindc          	   C   sö   t tƒrttd| ƒ d | _| j�Ì |r,|| _|d k	r:|| _|sH|rHt| _|tt	t
tgkr`|| _n0|d k	r�d| _t tƒr†ttd| j| ƒ t| jƒ‚|rš|| _|r¤|| _| jjsØy|  ||¡S  tk
rÔ   tdƒ‚Y qèX n| jj ¡  dS W d Q R X d S )Nz!start (RE)BIND operation via <%s>zunknown authentication methodz%s for <%s>z[Unable to rebind as a different user, furthermore the server abruptly closed the connectionT)rg   ri   rf   r…   rƒ   r‡   rˆ   r
   r‰   r	   r   r%   rh   rV   r“   r”   r´   r×   rÁ   r]   rU   rÙ   Zrebind_pool)r¼   r‡   rˆ   r‰   r“   r”   r¿   rí   ru   ru   rv   Úrebindc  s8    

zConnection.rebindc          	   C   sÂ   t tƒrttd| ƒ | jr$| j ¡  d| _| j�ˆ | jrh| j	sh| j
sJ| jrh| j ¡  d| _d| _
d| _n8| js tƒ }t tƒrˆttd| ƒ |  d||¡ | j ¡  t tƒr´ttddƒ dS Q R X dS )z¦Unbind the connected user. Unbind implies closing session as per RFC4511 (4.3)

        :param controls: LDAP controls to send along with the bind operation

        zstart UNBIND operation via <%s>NFzUNBIND request sent via <%s>ÚunbindRequestz"done UNBIND operation, result <%s>T)rg   ri   rf   r­   r´   Zunbind_referral_cacher…   rƒ   r    rŸ   rœ   r›   Úcloser�   r‘   rP   rj   rµ   )r¼   rí   rŒ   ru   ru   rv   rß   ‘  s(    


zConnection.unbindr   c             C   sü  dd„ t dƒD ƒ}ttƒr&ttd| ƒ | jrN|rNt|ƒ}ttƒrNttd|| ƒ | j��œ |  ¡  |slt	g}n|t
krzt
g}t|tƒrŠ|g}|	r¤t|tƒr¤| t¡ n|	r¼t|tƒr¼|tf7 }t|tƒ�rttƒrättd|t|ƒ| ƒ |
dkròg }
nt|
ƒ}
|
 t|||ƒ¡ | j�rˆ| jj�rˆ| j�rˆx`|D ]X}d|k�rJ| d¡d	 }n|}| jj�r,| ¡ |k�r,|| jjjk�r,td
| ƒ‚�q,W t|||||||||dk�rª| jn|| j| j�rÀ| jjnd| jj| jd�}ttƒ�rîttdt|ƒ| ƒ |   |  !d||
¡¡}g | _"t|tƒ�r2|}ttƒ�rØttd|| ƒ n¦| j#d dk�rTt$|ƒd	k�rTdnd}|�s„| j#d t%gk�r„| j&�s„| j#d | _&ttƒ�rØxH|D ]@}|d dk�r¶ttd|| ƒ n|d dk�r”ttd|| ƒ �q”W ttƒ�rîttd|ƒ |S Q R X dS )a)  
        Perform an ldap search:

        - If attributes is empty noRFC2696 with the specified size
        - If paged is 0 and cookie is present the search is abandoned on
          server attribute is returned
        - If attributes is ALL_ATTRIBUTES all attributes are returned
        - If paged_size is an int greater than 0 a simple paged search
          is tried as described in
        - Cookie is an opaque string received in the last paged search
          and must be used on the next paged search response
        - If lazy == True open and bind will be deferred until another
          LDAP operation is performed
        - If mssing_attributes == True then an attribute not returned by the server is set to None
        - If auto_escape is set it overrides the Connection auto_escape
        c             S   s   g | ]}|  ¡ ‘qS ru   )Úlower)Ú.0Úvru   ru   rv   ú
<listcomp>Ð  s    z%Connection.search.<locals>.<listcomp>ÚATTRIBUTES_EXCLUDED_FROM_CHECKzstart SEARCH operation via <%s>z;search base sanitized to <%s> for SEARCH operation via <%s>z>performing paged search for %d items with cookie <%s> for <%s>Nú;r   zinvalid attribute type )Ú	validatorr¥   z!SEARCH request <%s> sent via <%s>ZsearchRequestz/async SEARCH response id <%s> received via <%s>ÚtypeZsearchResDoneTFrŽ   ræ   ÚsearchResEntryz,SEARCH response entry <%s> received via <%s>ZsearchResRefz0SEARCH response reference <%s> received via <%s>z"done SEARCH operation, result <%s>)'r   rg   ri   rf   r¥   rm   rk   rƒ   Ú_fire_deferredr   r   r°   r!   ÚlistrÜ   r   ÚtupleÚintrj   ra   rQ   r³   Úschemaré   rò   Úattribute_typesr^   rA   r®   r¯   Úcustom_validatorrB   r¹   rµ   r©   rŽ   rr   r+   r…   )r¼   Zsearch_baseZsearch_filterZsearch_scopeZdereference_aliasesÚ
attributesZ
size_limitZ
time_limitZ
types_onlyZget_operational_attributesrí   Z
paged_sizeZpaged_criticalityZpaged_cookier®   Ú#conf_attributes_excluded_from_checkÚattribute_nameÚattribute_name_to_checkrŒ   r�   Úreturn_valueÚentryru   ru   rv   Úsearch±  s„    





(


& 


zConnection.searchc       
   
   C   sü  dd„ t dƒD ƒ}ttƒr&ttd| ƒ d| _| jrPt|ƒ}ttƒrPttd|| ƒ | jr°| jj	r°| jr°d|kr|| 
d¡d }n|}| jj	jr°| ¡ |kr°|| jj	jkr°td	| ƒ‚t|tƒrÂtd
ƒ‚| j��( |  ¡  t|||| j| jrî| jj	nd| jrþ| jjnd| jd�}ttƒ�r&ttdt|ƒ| ƒ |  |  d||¡¡}g | _t|tƒ�rj|}	ttƒ�rØttd|	| ƒ nn| jd dk�rŽ| jd tk�rŽdnd}	|	�sÀ| jd ttgk�rÀ| j�sÀ| jd | _ttƒ�rØttd|| ƒ ttƒ�rîttd|	ƒ |	S Q R X dS )z-
        Perform a compare operation
        c             S   s   g | ]}|  ¡ ‘qS ru   )rò   )ró   rô   ru   ru   rv   rõ   +  s    z&Connection.compare.<locals>.<listcomp>rö   z start COMPARE operation via <%s>Nz3dn sanitized to <%s> for COMPARE operation via <%s>r÷   r   zinvalid attribute type zvalue cannot be a sequence)rø   r¥   z"COMPARE request <%s> sent via <%s>ZcompareRequestz0async COMPARE response id <%s> received via <%s>rù   ZcompareResponserŽ   TFræ   z'COMPARE response <%s> received via <%s>z#done COMPARE operation, result <%s>)r   rg   ri   rf   r…   r¥   rm   rk   r³   rÿ   ré   r   rò   r^   r°   r"   r_   rƒ   rû   r7   r¯   r  rj   r8   r¸   rµ   r©   rþ   rŽ   r,   r-   )
r¼   ÚdnÚ	attributerÖ   rí   r  r  rŒ   r�   r  ru   ru   rv   Úcompare#  sF    $

6

("

zConnection.comparec          	   C   s@  dd„ t dƒD ƒ}dd„ t dƒD ƒ}ttƒr8ttd| ƒ d| _t|ƒ}| jrjt|ƒ}ttƒrjttd|| ƒ | j	��Ä |  
¡  g }|dkrŽg }	nt|tƒr t|ƒn|g}	d	}
|rôxJ|D ]:}| ¡ d
kr´|}
t||
 tƒrât||
 ƒn||
 g}P q´W ntƒ }|
�sd}
dd„ |D ƒ}tdd„ |	| g ƒ||
< ||
 �s^d| _ttƒ�rTttd| j| ƒ t| jƒ‚| j�r| jj�r| j�rx@||
 D ]4}| ¡ |k�r‚|| jjjk�r‚tdt|ƒ ƒ‚�q‚W xV|D ]N}d|k�rÞ| d¡d }n|}| ¡ |k�rÀ|| jjjk�rÀtd| ƒ‚�qÀW t||| j| j�r,| jjnd| j�r>| jjnd| jd�}ttƒ�rfttdt|ƒ| ƒ |  |   d||¡¡}g | _!t|t"t#f ƒ�r°|}ttƒ�rttd|| ƒ nlttƒ�rÈttd|| ƒ | j$d dk�rì| j$d t%k�rìdnd}|�s| j$d t%gk�r| j�s| j$d | _ttƒ�r2ttd |ƒ |S Q R X dS )!zë
        Add dn to the DIT, object_class is None, a class name or a list
        of class names.

        Attributes is a dictionary in the form 'attr': 'val' or 'attr':
        ['val1', 'val2', ...] for multivalued attributes
        c             S   s   g | ]}|  ¡ ‘qS ru   )rò   )ró   rô   ru   ru   rv   rõ   e  s    z"Connection.add.<locals>.<listcomp>rö   c             S   s   g | ]}|  ¡ ‘qS ru   )rò   )ró   rô   ru   ru   rv   rõ   f  s    ZCLASSES_EXCLUDED_FROM_CHECKzstart ADD operation via <%s>Nz/dn sanitized to <%s> for ADD operation via <%s>rÀ   ZobjectclassZobjectClassc             S   s   g | ]}t |ƒ‘qS ru   )re   )ró   Úobject_classru   ru   rv   rõ   …  s    c             S   s   || kr| |g S | S )Nru   )ÚxÚyru   ru   rv   Ú<lambda>†  s    z Connection.add.<locals>.<lambda>z"objectClass attribute is mandatoryz%s for <%s>zinvalid object class r÷   r   zinvalid attribute type )rø   r¥   zADD request <%s> sent via <%s>Z
addRequestz,async ADD response id <%s> received via <%s>z#ADD response <%s> received via <%s>rù   ZaddResponserŽ   TFræ   zdone ADD operation, result <%s>)&r   rg   ri   rf   r…   r   r¥   rm   rk   rƒ   rû   r°   r"   rü   rò   Údictr   rh   rX   r³   rÿ   Zobject_classesrs   ré   r   r^   r3   r¯   r  rj   r4   r¸   rµ   r©   r!   rþ   rŽ   r+   )r¼   r	  r  r  rí   r  Z conf_classes_excluded_from_checkÚ_attributesZattr_object_classZparm_object_classZobject_class_attr_nameÚattrZobject_class_namer  r  rŒ   r�   r  ru   ru   rv   ÚaddY  sx    

$




8


( 
zConnection.addc          	   C   st  t tƒrttd| ƒ d| _| jr>t|ƒ}t tƒr>ttd|| ƒ | j��$ |  ¡  | j	r~d| _t t
ƒrttt
d| j| ƒ t| jƒ‚t|ƒ}t tƒr ttdt|ƒ| ƒ |  |  d||¡¡}g | _t|ttf ƒræ|}t tƒrättd|| ƒ njt tƒrüttd	|| ƒ | jd
 dk�r | jd tk�r dnd}|�sP| jd tgk�rP| j�sP| jd | _t tƒ�rfttd|ƒ |S Q R X dS )zE
        Delete the entry identified by the DN from the DIB.
        zstart DELETE operation via <%s>Nz2dn sanitized to <%s> for DELETE operation via <%s>zconnection is read-onlyz%s for <%s>z!DELETE request <%s> sent via <%s>Z
delRequestz/async DELETE response id <%s> received via <%s>z&DELETE response <%s> received via <%s>rù   ZdelResponserŽ   TFræ   z"done DELETE operation, result <%s>)rg   ri   rf   r…   r¥   rm   rk   rƒ   rû   r™   rh   rY   r9   rj   r:   r¸   rµ   r©   r°   r!   rþ   rŽ   r+   )r¼   r	  rí   rŒ   r�   r  ru   ru   rv   Údelete²  s>    

( 
zConnection.deletec             C   sx  dd„ t dƒD ƒ}ttƒr&ttd| ƒ d| _| jrPt|ƒ}ttƒrPttd|| ƒ | j�� |  	¡  | j
r�d| _ttƒr†ttd| j| ƒ t| jƒ‚t|tƒsÂd	| _ttƒr¸ttd| j| ƒ t| jƒ‚|sîd
| _ttƒrättd| j| ƒ t| jƒ‚�xX|D �]N}| j�rj| jj�rj| j�rjd|k�r0| d¡d }n|}| jjj�rj| ¡ |k�rj|| jjjk�rjtd| ƒ‚|| }t|tƒ�rà|d ttttddddgk�ràt|ƒdk�rÔd| _ttƒ�rÊttd| j| ƒ t| jƒ‚|g||< qöxd|D ]\}t|ƒdk�s|d ttttddddgk�ræd| _ttƒ�r6ttd| j| ƒ t| jƒ‚�qæW qöW t||| j| j�rd| jjnd| j�rv| jjnd| jd�}	ttƒ�ržttdt |	ƒ| ƒ |  !|  "d|	|¡¡}
g | _#t|
t$t%f ƒ�rè|
}ttƒ�rTttd|| ƒ nlttƒ�r ttd|
| ƒ | j&d dk�r$| j&d t'k�r$dnd}|�sT| j&d t'gk�rT| j�sT| j&d | _ttƒ�rjttd|ƒ |S Q R X dS )a7  
        Modify attributes of entry

        - changes is a dictionary in the form {'attribute1': change), 'attribute2': [change, change, ...], ...}
        - change is (operation, [value1, value2, ...])
        - operation is 0 (MODIFY_ADD), 1 (MODIFY_DELETE), 2 (MODIFY_REPLACE), 3 (MODIFY_INCREMENT)
        c             S   s   g | ]}|  ¡ ‘qS ru   )rò   )ró   rô   ru   ru   rv   rõ   é  s    z%Connection.modify.<locals>.<listcomp>rö   zstart MODIFY operation via <%s>Nz2dn sanitized to <%s> for MODIFY operation via <%s>zconnection is read-onlyz%s for <%s>zchanges must be a dictionaryzno changes in modify requestr÷   r   zinvalid attribute type r*   r   r€   zmalformed changezinvalid change list)rø   r¥   z!MODIFY request <%s> sent via <%s>ZmodifyRequestz/async MODIFY response id <%s> received via <%s>z&MODIFY response <%s> received via <%s>rù   ZmodifyResponserŽ   TFræ   z"done MODIFY operation, result <%s>)(r   rg   ri   rf   r…   r¥   rm   rk   rƒ   rû   r™   rh   rY   r°   r  rZ   r³   rÿ   ré   r   rò   r^   r"   r   r   r   r   rr   r=   r¯   r  rj   r>   r¸   rµ   r©   r!   rþ   rŽ   r+   )r¼   r	  Zchangesrí   r  r  r  ZchangeZchange_operationrŒ   r�   r  ru   ru   rv   ÚmodifyÞ  s~    





**


,
8


( 
zConnection.modifyc       	   	   C   sÔ  t tƒrttd| ƒ d| _| jr\t|ƒ}t tƒr>ttd|| ƒ t|ƒ}t tƒr\ttd|| ƒ | j��f |  ¡  | j	rœd| _t t
ƒr’tt
d| j| ƒ t| jƒ‚|rÒ| |¡sÒd| _t t
ƒrÈtt
d| j| ƒ t| jƒ‚t||||ƒ}t tƒrúttdt|ƒ| ƒ |  |  d	||¡¡}g | _t|ttf ƒ�rD|}t tƒ�r°ttd
|| ƒ nlt tƒ�r\ttd|| ƒ | jd dk�r€| jd tk�r€dnd}|�s°| jd tgk�r°| j�s°| jd | _t tƒ�rÆttd|ƒ |S Q R X dS )z\
        Modify DN of the entry or performs a move of the entry in the
        DIT.
        z"start MODIFY DN operation via <%s>Nz5dn sanitized to <%s> for MODIFY DN operation via <%s>z>relative dn sanitized to <%s> for MODIFY DN operation via <%s>zconnection is read-onlyz%s for <%s>z(DN cannot change while performing movingz$MODIFY DN request <%s> sent via <%s>ZmodDNRequestz2async MODIFY DN response id <%s> received via <%s>z)MODIFY DN response <%s> received via <%s>rù   ZmodDNResponserŽ   TFræ   z%done MODIFY DN operation, result <%s>)rg   ri   rf   r…   r¥   rm   rk   rƒ   rû   r™   rh   rY   Ú
startswithrZ   r?   rj   r@   r¸   rµ   r©   r°   r!   rþ   rŽ   r+   )	r¼   r	  Zrelative_dnZdelete_old_dnZnew_superiorrí   rŒ   r�   r  ru   ru   rv   Ú	modify_dn6  sN    





( 
zConnection.modify_dnc          	   C   sò   t tƒrttd| ƒ d| _| j�È |  ¡  d}| jjs>|dkrÐ| jjrf|| jjkrf| jj| d dksn|dkr¶t|ƒ}t t	ƒr�tt	dt
|ƒ| ƒ |  d||¡ d| _d| _g | _d	}nt tƒrÐttd
t|ƒ| ƒ t tƒrättd|ƒ |S Q R X dS )z?
        Abandon the operation indicated by message_id
        z start ABANDON operation via <%s>NFr   rù   )ÚabandonRequesträ   rð   z#ABANDON request: <%s> sent via <%s>r  Tz\cannot abandon a Bind, an Unbind or an Abandon operation or message ID %s not found via <%s>z#done ABANDON operation, result <%s>)rg   ri   rf   r…   rƒ   rû   r´   Z_outstandingr1   rj   r2   rµ   rŽ   r�   r©   rh   rs   )r¼   Z
message_idrí   r  rŒ   ru   ru   rv   Úabandono  s*    0zConnection.abandonc          	   C   s  t tƒrttd| ƒ d| _| j�ì |  ¡  t|||d�}t tƒrRttdt|ƒ| ƒ |  	|  
d||¡¡}g | _t|tƒr’|}t tƒròttd|| ƒ n`t tƒr¨ttd|| ƒ | jd d	krÈ| jd
 tkrÈdnd}|sò| jd
 tgkrò| jsò| jd | _t tƒ�rttd|ƒ |S Q R X dS )z0
        Performs an extended operation
        z!start EXTENDED operation via <%s>N)Ú	no_encodez#EXTENDED request <%s> sent via <%s>ZextendedReqz1async EXTENDED response id <%s> received via <%s>z(EXTENDED response <%s> received via <%s>rù   ZextendedResprŽ   TFræ   z$done EXTENDED operation, result <%s>)rg   ri   rf   r…   rƒ   rû   r;   rj   r<   r¸   rµ   r©   r°   rþ   rŽ   r+   )r¼   Zrequest_nameZrequest_valuerí   r  rŒ   r�   r  ru   ru   rv   Úextended�  s,    
$
zConnection.extendedc          	   C   sÀ   t tƒrttd| ƒ | j�œ d}| jjs2tƒ | j_| jrd| jsdd| _	d| _
d}t tƒržttd| ƒ n:d| _	| jj | ¡r’| jjr’|rŒ|  ¡  d}n| jjsžd}t tƒr²ttd|ƒ |S Q R X d S )Nz"start START TLS operation via <%s>FTzdeferring START TLS for <%s>z%done START TLS operation, result <%s>)rg   ri   rf   rƒ   r³   ÚtlsrS   r    rŸ   r�   r—   rÂ   r´   rë   rì   )r¼   r¿   r  ru   ru   rv   rÂ   ²  s,    
zConnection.start_tlsc          	   C   sÄ   t tƒrttd| ƒ d | _| j�š d }| js¢d| _zf| jtkrJt| |ƒ}nL| jt	kr`t
| |ƒ}n6| jtkr‚ddlm} || |ƒ}n| jdkr–t| |ƒ}W d d| _X t tƒr¶ttd|ƒ |S Q R X d S )Nz"start SASL BIND operation via <%s>Tr   )Úsasl_gssapir)   Fz%done SASL BIND operation, result <%s>)rg   ri   rf   r…   rƒ   r˜   r“   r&   rF   r'   rE   r(   Zprotocol.sasl.kerberosr  rG   )r¼   rí   rŽ   r  ru   ru   rv   rè   Ð  s*    



zConnection.do_sasl_bindc          	   C   s  t tƒrttd| ƒ d | _| j��æ d }| j�sîd| _ddlm} | j 	dd¡\}}|||| j
d�}t| jd|ƒ}t tƒr†ttd	| ƒ |  |  d
||¡¡}| jjs²|  |¡\}	}n|d }d|k�rä|d  d¡ 	d¡}
d|
k�rèt| jd|ƒ}t tƒ�rttd| ƒ |  |  d
||¡¡}| jj�s4|  |¡\}	}n$t tƒ�rPttd|d | ƒ |d }|d tk�rèt| jd||d ƒ}t tƒ�r�ttd| ƒ |  |  d
||¡¡}| jj�s¾|  |¡\}	}n$t tƒ�rÚttd|d | ƒ |d }nd }d| _t tƒ�rttd|ƒ |S Q R X d S )Nz"start NTLM BIND operation via <%s>Tr   )Ú
NtlmClientrå   r*   )Ú	user_nameÚdomainrˆ   ZSICILY_PACKAGE_DISCOVERYz3NTLM SICILY PACKAGE DISCOVERY request sent via <%s>rä   r   Zserver_credsÚasciir÷   r%   ZSICILY_NEGOTIATE_NTLMz+NTLM SICILY NEGOTIATE request sent via <%s>z5NTLM SICILY NEGOTIATE response <%s> received via <%s>rŽ   ZSICILY_RESPONSE_NTLMz/NTLM SICILY RESPONSE NTLM request sent via <%s>z)NTLM BIND response <%s> received via <%s>Fz%done SASL NTLM operation, result <%s>)rg   ri   rf   r…   rƒ   r˜   Z
utils.ntlmr  r‡   ré   rˆ   r5   rŠ   rj   r¸   rµ   r´   rë   r·   Údecoder+   )r¼   rí   rŽ   r  Zdomain_namer  Zntlm_clientrŒ   r�   rî   Zsicily_packagesru   ru   rv   rê   ì  sX    










zConnection.do_ntlm_bindc          	   C   sŠ   | j jsf| j�P | jsZttƒr*ttd| ƒ | j}| j}| j	}| j
 | ¡ || _|| _|| _	W d Q R X n ttƒrzttd| ƒ | j j ¡  d S )Nzrefreshing server info for <%s>z)refreshing server info from pool for <%s>)r´   r×   rƒ   r‘   rg   ri   rf   r�   rŽ   r©   r³   Zget_info_from_serverrÙ   )r¼   Zprevious_responseZprevious_resultZprevious_entriesru   ru   rv   rì   &  s    zConnection.refresh_server_infoc       	   	   C   s¼   | j �¬ |d kr| j}t|tƒr®td|||d�}t|ƒ}|p>t}| |¡}|r”| ¡ dkr~tdgƒd }| 	t
|| | ƒ¡ | 	t
|| | ƒ¡ ttƒrªttd|| ƒ |S d S Q R X d S )NZsearchResponse)Ú
sort_orderr   ú-z"building LDIF output <%s> for <%s>)rƒ   r�   r°   r"   rC   rD   r   rÈ   ÚtellÚwriterb   rg   ri   rf   )	r¼   Úsearch_resultZ
all_base64Zline_separatorr#  rÕ   Z
ldif_linesZldif_outputÚheaderru   ru   rv   Úresponse_to_ldif;  s"    

zConnection.response_to_ldifro   c          
      s>  | j ��, |d kr| j}t|tƒ�r0tƒ }g |d< x¨|D ] ‰ ˆ d dkr8tƒ }	ˆ d |	d< |r’|s‚t‡ fdd„ˆ d D ƒƒ|	d< ntˆ d ƒ|	d< |rÊ|sºt‡ fdd„ˆ d	 D ƒƒ|	d	< ntˆ d	 ƒ|	d
< |d  |	¡ q8W ttkrìt|ƒ t	j
|d||dtdd�}
ttƒ�rttd|
| ƒ |�r,| |
¡ |
S W d Q R X d S )NÚentriesrù   rú   r	  c             3   s*   | ]"}ˆ d  | r|ˆ d  | fV  qdS )r  Nru   )ró   Úkey)r�   ru   rv   ú	<genexpr>n  s    z.Connection.response_to_json.<locals>.<genexpr>r  c             3   s*   | ]"}ˆ d  | r|ˆ d | fV  qdS )zraw:attributesÚraw_attributesNru   )ró   r+  )r�   ru   rv   r,  t  s    r-  ÚrawT)ú,z: )Úensure_asciiÚ	sort_keysÚindentÚcheck_circularÚdefaultÚ
separatorsz"building JSON output <%s> for <%s>)rƒ   r�   r°   r"   r  rÜ   rs   Úbytesrc   ÚjsonÚdumpsrd   rg   ri   rf   r&  )r¼   r.  r'  r2  ÚsortrÕ   Zchecked_attributesZinclude_emptyZ	json_dictr  Zjson_outputru   )r�   rv   Úresponse_to_jsonU  s6    	

  

zConnection.response_to_jsonc          	   C   sb   | j �R | jrTt|tƒr"t|dƒ}ttƒr6ttd| ƒ | | j	|||d�¡ | 
¡  W d Q R X d S )Nzw+z!writing response to file for <%s>)r.  r2  r9  )rƒ   r�   r°   r!   r¶   rg   ri   rf   Ú
writelinesr:  rñ   )r¼   Útargetr.  r2  r9  ru   ru   rv   Úresponse_to_file…  s    

zConnection.response_to_filec             C   sÜ   | j �Ì | jrÎ| jsÎd| _ttƒr:ttd| j| j| j| ƒ zŠyJ| jrP| j	dd� | jrb| j
dd� | jrx| jd| jd� |r„|  ¡  W n: tk
rÀ } zttƒr®ttd|| ƒ ‚ W d d }~X Y nX W d d| _X W d Q R X d S )NTz?executing deferred (open: %s, start_tls: %s, bind: %s) for <%s>F)r¿   )r¿   rí   z%s for <%s>)rƒ   r    rŸ   rg   ri   rf   r›   r�   rœ   r¶   rÂ   rÁ   rž   rì   r[   rh   )r¼   Z	read_infoÚeru   ru   rv   rû   •  s&    zConnection._fire_deferredc             C   s    | j r| js|  | j ¡| _| jS )N)r�   r©   Ú_get_entries)r¼   ru   ru   rv   r*  ¬  s    zConnection.entriesc             C   s®  | j ��œ ddlm}m} g }x:|D ]2}|d dkr$t|d  ¡ ƒ}||kr$| |¡ q$W |jdd„ d� g }x.|D ]&}x |D ]}	|	|kr~P q~W | |¡ qtW g }
xj|D ]b}|| jj	d	�}|t
|ƒ7 }|
 ||| jjrô|| || jd
 | jd |d�n|| |dd|d�f¡ q¨W g }xŒ|D ]„}|d dk�rt|d  ¡ ƒ}x^|
D ].}||d k�r@|d  |¡}| |¡ P �q@W ttƒ�rŠttd|| ƒ tdt|ƒ ƒ‚�qW W d Q R X |S )Nr   )Ú	ObjectDefÚReaderrù   rú   r  c             S   s
   t | ƒ S )N)rr   )r  ru   ru   rv   r  ¾  s    z)Connection._get_entries.<locals>.<lambda>)r+  )rÿ   ÚbaseÚfilter)r  rÀ   r   z&attribute set not found for %s in <%s>zattribute set not found for )rƒ   rÀ   r@  rA  ÚsetÚkeysrÜ   r9  r³   rÿ   rü   r´   rë   rŒ   Z_create_entryrg   rh   rf   r\   rs   )r¼   Zsearch_responser@  rA  Z	attr_setsr�   Zresp_attr_setZunique_attr_setsZattr_setZ
unique_setZobject_defsZ
object_defr*  r  ru   ru   rv   r?  ³  sF    




@



 zConnection._get_entries)TN)NNNNNTN)N)N)NNN)N)N)TNN)N)NNN)T)NFNNN)FNro   TNTT)Fro   T)T)(rÇ   Ú
__module__Ú__qualname__Ú__doc__r   r   r¾   rº   rÊ   rÑ   r»   ÚpropertyrÕ   ÚsetterrÏ   rÝ   rã   rÁ   rï   rß   r   r   r  r  r  r  r  r  r  r  rÂ   rè   rê   rì   r)  r:  r=  rû   r*  r?  ru   ru   ru   rv   r   u   sº   6 #& 
t      
(
"h
4  
W
-
X  
6
!  

:    
      
+  

r   )—rH  Úcopyr   Úosr   Ú	threadingr   r   Ú	functoolsr   r7  rÀ   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   Úresultsr+   r,   r-   r¨   r.   Zpoolingr/   r³   r0   Zoperation.abandonr1   r2   Zoperation.addr3   r4   Zoperation.bindr5   r6   Zoperation.comparer7   r8   Zoperation.deleter9   r:   Zoperation.extendedr;   r<   Zoperation.modifyr=   r>   Zoperation.modifyDnr?   r@   Zoperation.searchrA   rB   Zprotocol.rfc2849rC   rD   Zprotocol.sasl.digestMd5rE   Zprotocol.sasl.externalrF   Zprotocol.sasl.plainrG   Zstrategy.syncrH   Zstrategy.mockAsyncrI   Zstrategy.asynchronousrJ   Zstrategy.reusablerK   Zstrategy.restartablerL   Zstrategy.ldifProducerrM   Zstrategy.mockSyncrN   Zstrategy.asyncStreamrO   Zoperation.unbindrP   Zprotocol.rfc2696rQ   rÏ   rR   r  rS   Ú
exceptionsrT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   Z
utils.convra   rb   rc   rd   re   Z	utils.logrf   rg   rh   ri   rj   rk   rl   Zutils.dnrm   rç   r„   rw   r~   Úobjectr   ru   ru   ru   rv   Ú<module>   sl   Œ<$