o
    jB                 
   @   sh  d Z ddlmZmZ ddlmZ ddl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+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1 d	d
l2m3Z3m4Z4m5Z5 ddl6m7Z7 d	dl8m9Z9 d	dl:m;Z; ddl<m=Z=m>Z> ddl?m@Z@mAZA ddlBmCZCmDZD ddlEmFZFmGZG ddlHmIZImJZJ ddlKmLZLmMZM ddlNmOZOmPZP ddlQmRZRmSZS ddlTmUZUmVZV ddlWmXZXmYZY ddlZm[Z[ ddl\m]Z] ddl^m_Z_ ddl`maZa ddlbmcZc ddldmeZe ddlfmgZg ddlhmiZi dd ljmkZk dd!llmmZm dd"lnmoZo dd#lpmqZq dd$lrmsZs dd%ltmuZu dd&lvmwZw d	d'lxmyZy d	d(lzm{Z{ d	d)l|m}Z}m~Z~mZmZmZmZmZmZmZmZmZmZmZmZ dd*lmZmZmZmZmZ dd+lmZmZmZmZmZmZmZ dd,lmZ dd-lmZ e+e,e-e.gZee"e#eeeee(e)eg
Zd.d/ Zd0d1 ZG d2d3 d3eZdS )4
    )deepcopycopy)linesep)RLock)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_DEFAULTAUTO_BIND_NONEAUTO_BIND_TLS_BEFORE_BIND	SAFE_SYNCSAFE_RESTARTABLEAUTO_BIND_TLS_AFTER_BINDAUTO_BIND_NO_TLSSTRING_TYPESSEQUENCE_TYPES	MOCK_SYNC
MOCK_ASYNCNTLMEXTERNAL
DIGEST_MD5GSSAPIPLAINDSASCHEMAALL   )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)SafeSyncStrategy)SafeRestartableStrategy)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LDAPInvalidPortErrorLDAPStartTLSError)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_dn)check_port_and_port_listc                 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 zt| W S  ty=   Y dS w )	Nr   r   :r0      [z]:?)lenstr	Exception)endpoint r   /root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/ldap3/core/connection.py_format_socket_endpoint_   s    
r   c                 C   sl   | r4z|   }W n ty   d}Y nw z|  }W n ty%   d}Y nw dt| d t| d S dS )N)NNNNz<local: z - remote: >z<no socket>)getsocknamer~   getpeernamer   )socklocalremoter   r   r   _format_socket_endpointsk   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ddddfddZdGd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		dHddZ							dIddZ	dJdd Zeedd!d!dddddddfd"d#Z	dJd$d%Z			dKd&d'Z	dJd(d)Z	dJd*d+Z			dLd,d-Z	dJd.d/Z			dKd0d1ZdMd2d3Zd4d5 Z d6d7 Z!d8d9 Z"					dNd:d;Z#			<				dOd=d>Z$		<	dPd?d@Z%dJdAdBZ&edCdD Z'dEdF Z(dS )Q
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_DEFAULT, 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 ANONYMOUS, SIMPLE or SASL, as specified in ldap3
    :param client_strategy: communication strategy used in the Connection
    :type client_strategy: can be one of SYNC, ASYNC, LDIF, RESTARTABLE, REUSABLE 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 cred_store: credential store for gssapi
    :type cred_store: dict
    :param use_referral_cache: keep referral connections open and reuse them
    :type use_referral_cache: bool
    :param auto_escape: automatic escaping of filter values
    :type auto_escape: bool
    :param auto_encode: automatic encoding of attribute values
    :type auto_encode: bool
    :param source_address: the ip address or hostname to use as the source when opening the connection to the server
    :type source_address: str
    :param source_port: the source port to use when opening the connection to the server. Cannot be specified with source_port_list
    :type source_port: int
    :param source_port_list: a list of source ports to choose from when opening the connection to the server. Cannot be specified with source_port
    :type source_port_list: list
    N   TFc           !      C   s  t d}t | _| jI |tvr%d| _ttr ttd| j|  t| j|| _	|| _
|| _|s7| j
r7t| _n&|s=t| _n |ttttfv rI|| _nd| _ttrXttd| j|  t| j|| _|rddnd| _d | _d | _d | _d| _d| _d| _d | _|du rt| _n|du rt| _n|| _|
| _|| _|rt nd | _ d | _!d| _"d| _#|| _$g | _%d| _&d| _'d| _(d | _)d| _*|| _+|r|n|| _,|| _-|| _.|| _/|| _0d| _1|| _2|| _3|	rdnd| _4t5| | _6g | _7|| _8|| _9|| _:|| _;|| _<|| _=d | _>d | _?d| _@tA||} | rttrtt|  tB| |d ur!|nd| _Cdg| _D|d ur2|g| _Dn|d ur>|d d  | _DtE|tFrHtG|}tE|tHrVtI|tJddd	}tE|tIrm|| _K| jKL|  | jKM| | _Nnd | _K|| _N| j	tOkrtP| | _Qn| j	tRkrtS| | _Qnx| j	tTkrtU| | _Qnl| j	tVkrtW| | _Qn`| j	tXkrtY| | _QnT| j	tZkrt[| | _QnH| j	t\krt]| | _Qd| _+n9| j	t^krt_| | _Qn-| j	t`krta| | _Qn!| j	tbkrtc| | _Qnd
| _ttrttd| j|  t| j| jQjd| _d| jQje| _e| jQjf| _f| jQjg| _g| jQjh| _h| jQjis%| j  ttkrEtl r7ttkd| m  nttkd|  W d    d S W d    d S W d    d S 1 sYw   Y  d S )NDEFAULT_THREADED_POOL_NAMEz"unknown client connection strategy%s for <%s>unknown authentication methodTFr    )activeexhaustzunknown strategyzinstantiated Connection: <%s>zinstantiated Connection: <%r>)nr   r   connection_lockCLIENT_STRATEGIES
last_errorrp   rq   ro   r\   strategy_typeuserpasswordr
   authenticationr	   r   r(   r^   versionauto_referralsrequestresponseresultbound	listeningclosedr   	auto_bindr#   sasl_mechanismsasl_credentialsrZ   _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
cred_storepool_lifetimepool_keepalivestarting_tlscheck_namesraise_exceptions
auto_ranger4   extend_entriesfast_decoderreceive_timeoutempty_attributesuse_referral_cacheauto_escapeauto_encode_digest_md5_kic_digest_md5_kis_digest_md5_sec_numrw   rh   source_addresssource_port_list
isinstancer$   r6   r%   r5   r   server_pool
initializeget_current_serverserverr   rN   strategyr    rO   r!   rP   r   rR   r   rU   r   rT   r   rS   r&   rV   r'   rQ   r   rW   sendopenget_responsepost_send_single_responsepost_send_searchno_real_dsa_do_auto_bindrr   ru   !repr_with_sensitive_data_stripped)!selfr   r   r   r   r   r   client_strategyr   r   r   r   r   collect_usager   r   r   r   r   r   r   r   r   return_empty_attributesr   r   r   r   r   source_portr   conf_default_pool_nameport_errr   r   r   __init__   s   




















     $zConnection.__init__c                 C   sF   | j jr!| j}d | _| j}d | _|t| j|rt|nd t|fS |S N)r   thread_safer   r   r   r   r   )r   statusr   temp_responsetemp_requestr   r   r   _prepare_return_valueu  s   "z Connection._prepare_return_valuec                 C   sd  | j r| j ttfvrttrttd|  | jr| jdd | j tkr)| j	dd na| j t
krZ| jddr;| j	dd nOd| jrDd| j nd }ttrRttd||  |   t|| j tkr| j	dd | jddsd	| jrtd| j nd }ttrttd||  |   t|| jsd
| jrd| j nd }ttrttd||  |   t|d S d S d S )Nz"performing automatic bind for <%s>Fread_server_infoTz/automatic start_tls befored bind not successful - r   r   z-automatic start_tls after bind not successfulzautomatic bind not successful)r   r   r   rp   rr   ro   r   r   r#   bindr   	start_tlsr   rq   unbindri   r"   r   r]   )r   errorr   r   r   r   ~  s>   


zConnection._do_auto_bindc                 C   s   | j rt| j nddt| j | jrdnd| jsdn| jrdnd| jr%dn| jr*d	nd
t| j	| j
s4dn| jr9dnd| jr?dndt| drJ| jjjnd| jrPd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 decoderr   )r   r}   r   r   r   r   r   r   r   r   r   r   r   hasattrr   	__class____name__r   join)r   sr   r   r   __str__  s   
zConnection.__str__c                 C   sn  t d}| jrd| }nd| }|| jd u rdnd| 7 }|| jd u r(dnd| 7 }|| jd u r6dnd| 7 }|| jd u rDdnd| 7 }|| jd u rRdnd	| 7 }|| jd u r`dnd
| 7 }|| j	d u rndnd| 7 }|| j
d u r|dnd| 7 }|| jd u rdnd| 7 }|| jd u rdnd| 7 }|| jd u rdnd| jrdnd 7 }|| jd u rdnd| 7 }|| jd u rdnd| 7 }|| jd u rdnd| 7 }|| jd u s| j|krdnd| 7 }|| jd u rdnd| 7 }|| jd u rdnd| 7 }|| jd u rdnd| 7 }|| jd u r"dndt| j 7 }|| jd u r3dn	d| jr:dnd 7 }|| jd u rGdn	d| jrNdnd 7 }|| jd u r[dnd| 7 }|| jd u rjdn	d| jrqdnd 7 }|| jd u r~dn	d| jrdnd 7 }|| jd u rdn	d| jrdnd 7 }|| jd u rdn	d | jrdnd 7 }|d!7 }|S )"Nr   #Connection(server={0.server_pool!r}Connection(server={0.server!r}r   , user={0.user!r}z, password={0.password!r}, auto_bind={0.auto_bind!r}, version={0.version!r}%, authentication={0.authentication!r}%, client_strategy={0.strategy_type!r}%, auto_referrals={0.auto_referrals!r}%, sasl_mechanism={0.sasl_mechanism!r}), sasl_credentials={0.sasl_credentials!r}, check_names={0.check_names!r}z, collect_usage=TrueFalse, read_only={0.read_only!r}, lazy={0.lazy!r}), raise_exceptions={0.raise_exceptions!r}, pool_name={0.pool_name!r}, pool_size={0.pool_size!r}#, pool_lifetime={0.pool_lifetime!r}%, pool_keepalive={0.pool_keepalive!r}, cred_store=z, fast_decoder=, auto_range=', receive_timeout={0.receive_timeout!r}z, return_empty_attributes=, auto_encode=, auto_escape=, use_referral_cache=))r   r   formatr   r   r   r   r   r   r   r   r   r   usager   r   r   r   r   r   r   r   reprr   r   r   r   r   r   r   r   r   rr   r   r   __repr__  s@   
$&"((((((zConnection.__repr__c              	   C   s  t d}| jrd| }nd| }|| jd u rdnd| 7 }|| jd u r(dn	ddt| j 7 }|| jd u r;dnd| 7 }|| jd u rIdnd	| 7 }|| jd u rWdnd
| 7 }|| j	d u rednd| 7 }|| j
d u rsdnd| 7 }|| jd u rdnd| 7 }| jtkr|| jd u rdnd| jd | jd dt| jd  | jd 7 }n|| jd u rdnd| 7 }|| jd u rdnd| 7 }|| jd u rdn| jrdnd7 }|| jd u rdnd| 7 }|| jd u rdnd| 7 }|| jd u rdnd| 7 }|| jd u s| j|krdnd| 7 }|| jd u r"dnd| 7 }|| jd u r1dnd| 7 }|| jd u r@dnd| 7 }|| jd u rOdndt| j 7 }|| jd u r`dn| jrfd nd7 }|| jd u rrdn	d!| jryd"nd 7 }|| jd u rdnd#| 7 }|| jd u rdn| jrd$nd7 }|| jd u rdn	d%| jrd"nd 7 }|| jd u rdn	d&| jrd"nd 7 }|| jd u rdn	d'| jrd"nd 7 }|d(7 }|S ))Nr   r   r   r   r   z, password='{0}'z*<stripped %d characters of sensitive data>r   r   r   r   r   r   z/, sasl_credentials=({0!r}, {1!r}, '{2}', {3!r})r   r0   *r   r   r   r   z, collect_usage=Truer   r   r   r   r  r  r  r  r  z, fast_decoder=Truer  r   r  z, return_empty_attributes=Truer  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   r   r  r   r   r   r     sD   
&
D *"$($(((z,Connection.repr_with_sensitive_data_strippedc                 C   s   | j jr	| j  S dS )zUsed 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   r   r   r   stream  s   zConnection.streamc                 C   sP   | j  | jjr| j| W d    d S W d    d S 1 s!w   Y  d S r   )r   r   r  
set_stream)r   valuer   r   r   r    s   "c                 C   sX   | j sdS | jjr)| j   | jjjD ]}|  j |jj7  _ q|  j | jjj7  _ | j S )zKUsage statistics for the connection.
        :return: Usage object
        N)	r   r   pooledresetpoolworkers
connectionr  terminated_usage)r   workerr   r   r   r    s   
zConnection.usagec                 C   s   | j 3 | j| j| jf | jtkr-| jtkrt| _| jr"| 	  | js-| 
 s-td| W  d    S 1 s9w   Y  d S )Nzunable to bind)r   r   appendr   r   r   r   r   r#   r   r   r]   r  r   r   r   	__enter__  s   

$zConnection.__enter__c              	   C   s   | j I | j \}}|s| js| jr#z|   W n	 ty"   Y nw |s,| jr,|   |d urDt	t
r;tt
d||  	 W d    dS W d    d S 1 sOw   Y  d S )Nr   F)r   r   popr   r  r   rc   r   r   rp   rq   ro   )r   exc_typeexc_valexc_tbcontext_boundcontext_closedr   r   r   __exit__!  s"   
"zConnection.__exit__c                 C   s\  t tr
ttd|  d| _| j | jr7| js7| jjr"| j	| d| _
|| _d| _t tr5ttd|  nd| _
d| _| jrF| jdd | jtkrt trUttd|  | jjs~t| j| j| jd| jd	}t trsttd
t||  | | d||}n| j	|}n| jtkrt trttd|  | jjst| j| j| j| j| jd	}t trttdt||  | | d||}n| j	|}n| jtkr| jtv rt trttd|  | jjs| |}n{| j	|}ntd| _t trttd| j|  t | j| jt!krI| jr4| jr4t"| j#ddkr4t tr"ttd|  | jjs-| $|}n1| j	|}n*d| _t trDttd| j|  t%| jd| _t trYttd| j|  t%| j| jj&s| jjs| jtt!fvr| '|\}}t trttd||  n:| jj&r| j(}t trttd||  n%| jjs| jtt!fv r|}nd| _t trttd| j|  t%| j|du rd| _n3|du rd| _n*|du rd| _n!|d t)krdnd| _| js|r|d r| js|d | _|r| jr| *  g | _+t trttd| j | ,| j| j(W  d   S 1 s'w   Y  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>Fr   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 supportedr   \r   zperforming NTLM BIND for <%s>z)NTLM needs domain\username and a passwordr   z-async BIND response id <%s> received via <%s>z$BIND response <%s> received via <%s>r   descriptionz done BIND operation, result <%s>)-rp   rr   ro   r   r   r   r   r   r  validate_bindr   r   r   r   r   r   r	   rs   r;   r   r   r   r<   r   r   r
   r   r   r   SASL_AVAILABLE_MECHANISMSdo_sasl_bindrq   r_   r(   r|   splitdo_ntlm_bindr^   syncr   r   r1   refresh_server_infor   r   )r   r   controlsr   r   _r   r   r   r   r   2  s   





&





$







 

&zConnection.bindc              	   C   s<  t tr
ttd|  d | _| j |r|| _|d ur|| _|s$|r$t| _|tt	t
tfv r0|| _n|d urHd| _t trCttd| j|  t| j|rM|| _|rR|| _| jjsz| ||W W  d    S  ty   d| _t trzttd| j|  t| jw | jj  | d| jW  d    S 1 sw   Y  d S )Nz!start (RE)BIND operation via <%s>r   r   z[Unable to rebind as a different user, furthermore the server abruptly closed the connectionT)rp   rr   ro   r   r   r   r   r
   r   r	   r   r(   rq   r^   r   r   r   r  r   re   r]   r  rebind_poolr   r   )r   r   r   r   r   r   r   r3  r   r   r   rebind  sD   


$zConnection.rebindc                 C   s   t tr
ttd|  | jr| j  d| _| jN | jr4| j	s4| j
s%| jr4| j  d| _d| _
d| _n| jsPt }t trDttd|  | d|| | j  t trZttdd | dW  d   S 1 siw   Y  dS )zUnbind 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)rp   rr   ro   r   r   unbind_referral_cacher   r   r   r   r   r   closer   r   rX   rs   r   r   )r   r3  r   r   r   r   r     s*   


$zConnection.unbindr   c                 C   s"  dd t dD }ttrttd|  | jr'|r't|}ttr'ttd||  | j[ |   |s6t	g}n|t
kr=t
g}t|trE|g}|	rRt|trR|t n|	r^t|tr^|tf7 }t|trttrqttd|t||  |
du rxg }
nt|
}
|
t||| | jr| jjr| jr|D ]7}d|v r|dd	 }n|}| jjr| |vr|| jjjvrd
| | _ttrttd| j|  t| jqt|||||||||du r| jn|| j| jr| jjnd| jj | jd}ttrttdt!||  | "| #d||
}g | _$t|tr|}ttrttd||  nR| j%d dkr,t&|d	kr,dnd}|sD| j%d t'fvrD| jsD| j%d | _ttrm|D ]!}|d dkr]ttd||  qK|d dkrkttd||  qKttrxttd| | j(|ddW  d   S 1 sw   Y  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      g | ]}|  qS r   lower.0vr   r   r   
<listcomp>      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   invalid attribute type r   	validatorr   z!SEARCH request <%s> sent via <%s>searchRequestz/async SEARCH response id <%s> received via <%s>typesearchResDoneTFr   r+  searchResEntryz,SEARCH response entry <%s> received via <%s>searchResRefz0SEARCH response reference <%s> received via <%s>z"done SEARCH operation, result <%s>r   ))r   rp   rr   ro   r   rv   rt   r   _fire_deferredr   r   r   r$   listr   r   tupleintrs   rj   rY   r   schemar/  r<  attribute_typesr   rq   rf   rG   r   r   custom_validatorrH   r   r   r   r   r|   r1   r   )r   search_basesearch_filtersearch_scopedereference_aliases
attributes
size_limit
time_limit
types_onlyget_operational_attributesr3  
paged_sizepaged_criticalitypaged_cookier   #conf_attributes_excluded_from_checkattribute_nameattribute_name_to_checkr   r   return_valueentryr   r   r   search  s   




"


& 

&zConnection.searchc           
   
   C   sJ  dd t dD }ttrttd|  d| _| jr(t|}ttr(ttd||  | jrh| jj	rh| jrhd|v r>|
dd }n|}| jj	jrh| |vrh|| jj	jvrhd	| | _ttrcttd
| j|  t| jt|trd| _ttr|ttd
| j|  t| j| j |   t|||| j| jr| jj	nd| jr| jjnd| jd}ttrttdt||  | | d||}g | _t|tr|}	ttrttd|	|  n2| jd dkr| jd tkrdnd}	|	s| jd ttfvr| js| jd | _ttrttd||  ttrttd|	 |  |	W  d   S 1 sw   Y  dS )z-
        Perform a compare operation
        c                 S   r:  r   r;  r=  r   r   r   r@  u  rA  z&Connection.compare.<locals>.<listcomp>rB  z start COMPARE operation via <%s>Nz3dn sanitized to <%s> for COMPARE operation via <%s>rC  r   rD  r   zvalue cannot be a sequencerE  z"COMPARE request <%s> sent via <%s>compareRequestz0async COMPARE response id <%s> received via <%s>rH  compareResponser   TFr+  z'COMPARE response <%s> received via <%s>z#done COMPARE operation, result <%s>)!r   rp   rr   ro   r   r   rv   rt   r   rQ  r/  rR  r<  rq   rf   r   r%   rg   r   rM  r=   r   rS  rs   r>   r   r   r   rP  r   r2   r3   r   )
r   dn	attributer  r3  r`  rb  r   r   rc  r   r   r   comparem  sV   $



6
$

&zConnection.comparec              	   C   s  dd t dD }dd t dD }ttrttd|  d| _t|}| jr5t|}ttr5ttd||  | j	 | 
  g }|du rGg }	nt|trPt|n|g}	d	}
|ry|D ]}| d
krw|}
t||
 trpt||
 n||
 g} nqYnt }|
sd}
dd |D }tdd |	| g ||
< ||
 sd| _ttrttd| j|  t| j| jr| jjr| jr||
 D ]'}| |vr|| jjjvrdt| | _ttrttd| j|  t| jq|D ]6}d|v r|dd }n|}| |vr|| jjjvrd| | _ttrttd| j|  t| jqt||| j| jr*| jjnd| jr3| jjnd| jd}ttrGttdt||  | |  d||}g | _!t|t"t#f rl|}ttrkttd||  n6ttrxttd||  | j$d dkr| j$d t%krdnd}|s| j$d t%fvr| js| j$d | _ttrttd | | &|W  d   S 1 sw   Y  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   r:  r   r;  r=  r   r   r   r@    rA  z"Connection.add.<locals>.<listcomp>rB  c                 S   r:  r   r;  r=  r   r   r   r@    rA  CLASSES_EXCLUDED_FROM_CHECKzstart ADD operation via <%s>Nz/dn sanitized to <%s> for ADD operation via <%s>r   objectclassobjectClassc                 S   s   g | ]}t |qS r   )rn   )r>  object_classr   r   r   r@    rA  c                 S   s   || vr	| |g S | S r   r   )xyr   r   r   <lambda>  s    z Connection.add.<locals>.<lambda>z"objectClass attribute is mandatoryr   zinvalid object class rC  r   rD  rE  zADD request <%s> sent via <%s>
addRequestz,async ADD response id <%s> received via <%s>z#ADD response <%s> received via <%s>rH  addResponser   TFr+  zdone ADD operation, result <%s>)'r   rp   rr   ro   r   r   r   rv   rt   r   rM  r   r%   rN  r<  dictr   rq   r`   r   rQ  object_classesr}   r/  rR  rf   r9   r   rS  rs   r:   r   r   r   r$   rP  r   r1   r   )r   rh  rn  rX  r3  r`   conf_classes_excluded_from_check_attributesattr_object_classparm_object_classobject_class_attr_nameattrobject_class_namera  rb  r   r   rc  r   r   r   add  s   
$




8


( 
&zConnection.addc                 C   s  t tr
ttd|  d| _| jrt|}t trttd||  | j |   | j	r>d| _t t
r9tt
d| j|  t| jt|}t trOttdt||  | | d||}g | _t|ttf rr|}t trqttd||  n0t tr}ttd	||  | jd
 dkr| jd tkrdnd}|s| jd tfvr| js| jd | _t trttd| | |W  d   S 1 sw   Y  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>connection is read-onlyr   z!DELETE request <%s> sent via <%s>
delRequestz/async DELETE response id <%s> received via <%s>z&DELETE response <%s> received via <%s>rH  delResponser   TFr+  z"done DELETE operation, result <%s>)rp   rr   ro   r   r   rv   rt   r   rM  r   rq   ra   r?   rs   r@   r   r   r   r   r$   rP  r   r1   r   )r   rh  r3  r   r   rc  r   r   r   delete  sB   
$$zConnection.deletec                 C   s  dd t dD }ttrttd|  d| _| jr(t|}ttr(ttd||  | j | 	  | j
rHd| _ttrCttd| j|  t| jt|tsad	| _ttr\ttd| j|  t| j|swd
| _ttrrttd| j|  t| jt }|D ]}| jr| jjr| jrd|v r|dd }n|}| jjjr| |vr|| jjjvrd| | _ttrttd| j|  t| j|| }t|tr|d ttttddddfv rt|dkrd| _ttrttd| j|  t| j|g||< q||D ]-}	t|	dks|	d ttttddddfvr$d| _ttrttd| j|  t| jq|||< q|t||| j| jr7| jjnd| jr@| jjnd| jd}
ttrTttdt |
|  | !| "d|
|}g | _#t|t$t%f ry|}ttrxttd||  n6ttrttd||  | j&d dkr| j&d t'krdnd}|s| j&d t'fvr| js| j&d | _ttrttd| | (|W  d   S 1 sw   Y  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   r:  r   r;  r=  r   r   r   r@  ?  rA  z%Connection.modify.<locals>.<listcomp>rB  zstart MODIFY operation via <%s>Nz2dn sanitized to <%s> for MODIFY operation via <%s>r~  r   zchanges must be a dictionaryzno changes in modify requestrC  r   rD  r0   r   r   zmalformed changezinvalid change listrE  z!MODIFY request <%s> sent via <%s>modifyRequestz/async MODIFY response id <%s> received via <%s>z&MODIFY response <%s> received via <%s>rH  modifyResponser   TFr+  z"done MODIFY operation, result <%s>))r   rp   rr   ro   r   r   rv   rt   r   rM  r   rq   ra   r   rt  rb   r   rQ  r/  rR  r<  rf   r%   r   r   r   r   r|   rC   r   rS  rs   rD   r   r   r   r$   rP  r   r1   r   )r   rh  changesr3  r`  
changelistra  rb  changechange_operationr   r   rc  r   r   r   modify4  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 |   | j	rMd| _t t
rHtt
d| j|  t| jt||||}t trattdt||  | | d||}g | _t|ttf r|}t trttd	||  n0t trttd
||  | jd dkr| jd tkrdnd}|s| jd tfvr| js| jd | _t trttd| | |W  d   S 1 sw   Y  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>r~  r   z$MODIFY DN request <%s> sent via <%s>modDNRequestz2async MODIFY DN response id <%s> received via <%s>z)MODIFY DN response <%s> received via <%s>rH  modDNResponser   TFr+  z%done MODIFY DN operation, result <%s>)rp   rr   ro   r   r   rv   rt   r   rM  r   rq   ra   rE   rs   rF   r   r   r   r   r$   rP  r   r1   r   )	r   rh  relative_dndelete_old_dnnew_superiorr3  r   r   rc  r   r   r   	modify_dn  sH   

$$zConnection.modify_dnc                 C   s  t tr
ttd|  d| _| jn |   d}| jjs|dkrh| jjr3|| jjv r3| jj| d dvs7|dkr[t|}t t	rHtt	dt
||  | d|| d| _d| _g | _d	}nt trhttd
t||  t trrttd| | |W  d   S 1 sw   Y  dS )z?
        Abandon the operation indicated by message_id
        z start ABANDON operation via <%s>NFr   rH  )abandonRequestr)  r7  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>)rp   rr   ro   r   r   rM  r   _outstandingr7   rs   r8   r   r   r   r   rq   r}   r   )r   
message_idr3  rc  r   r   r   r   abandon  s,   0$zConnection.abandonc                 C   s6  t tr
ttd|  d| _| j |   t|||d}t tr)ttdt||  | 	| 
d||}g | _t|trI|}t trHttd||  n0t trTttd||  | jd d	krd| jd
 tkrddnd}|sy| jd
 tfvry| jsy| jd | _t trttd| | j|ddW  d   S 1 sw   Y  dS )z0
        Performs an extended operation
        z!start EXTENDED operation via <%s>N)	no_encodez#EXTENDED request <%s> sent via <%s>extendedReqz1async EXTENDED response id <%s> received via <%s>z(EXTENDED response <%s> received via <%s>rH  extendedRespr   TFr+  z$done EXTENDED operation, result <%s>rL  )rp   rr   ro   r   r   rM  rA   rs   rB   r   r   r   r   rP  r   r1   r   )r   request_namerequest_valuer3  r  r   r   rc  r   r   r   extended  s0   
$$zConnection.extendedc                 C   s   t tr
ttd|  | jb d}d | _| jjst | j_| jr5| j	s5d| _
d| _d}t tr4ttd|  n$d| _
| jr?|   | jj| rS| jjrS|rP|   d}n| jjsYd}t trcttd| | |W  d    S 1 srw   Y  d S )Nz"start START TLS operation via <%s>FTzdeferring START TLS for <%s>z%done START TLS operation, result <%s>)rp   rr   ro   r   r   r   tlsr[   r   r   r   r   r   r   r   r   r1  r2  r   )r   r   rc  r   r   r   r     s6   
$zConnection.start_tlsc              	   C   s   t tr
ttd|  d | _| jW d }| jsTd| _z6| jtkr%t| |}n&| jt	kr0t
| |}n| jtkrAddlm} || |}n
| jdkrKt| |}W d| _nd| _w t tr^ttd| |W  d    S 1 sjw   Y  d S )Nz"start SASL BIND operation via <%s>Tr   )sasl_gssapir,   Fz%done SASL BIND operation, result <%s>)rp   rr   ro   r   r   r   r   r)   rL   r*   rK   r+   protocol.sasl.kerberosr  rM   )r   r3  r   r  r   r   r   r.  .  s.   




$zConnection.do_sasl_bindc              	   C   s0  t tr
ttd|  d | _| j | jsd| _zddlm} | j	dd\}}|||| j
d}t| jd|}t trAttd	|  | | d
||}| jjsW| |\}}	n|d }	d|	v r|	d d	d}
d|
v rt| jd|}t tr~ttd|  | | d
||}| jjs| |\}}	nt trttd|d |  |d }	|	d tkrt| jd||	d }t trttd|  | | d
||}| jjs| |\}}	nt trttd|d |  |d }	nd }	W d| _nd| _w t trttd|	 |	W  d    S W d    d S 1 sw   Y  d S )Nz"start NTLM BIND operation via <%s>Tr   )
NtlmClientr*  r0   )	user_namedomainr   SICILY_PACKAGE_DISCOVERYz3NTLM SICILY PACKAGE DISCOVERY request sent via <%s>r)  r   server_credsasciirC  r(   SICILY_NEGOTIATE_NTLMz+NTLM SICILY NEGOTIATE request sent via <%s>z5NTLM SICILY NEGOTIATE response <%s> received via <%s>r   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>)rp   rr   ro   r   r   r   
utils.ntlmr  r   r/  r   r;   r   rs   r   r   r   r1  r   decoder1   )r   r3  r  domain_namer  ntlm_clientr   r   r4  r   sicily_packagesr   r   r   r0  J  sj   
$zConnection.do_ntlm_bindc                 C   s   | j jsG| j6 | js5ttrttd|  | j}| j}| j	}| j
|  || _|| _|| _	W d    d S W d    d S 1 s@w   Y  d S ttrQttd|  | j j  d S )Nzrefreshing server info for <%s>z)refreshing server info from pool for <%s>)r   r  r   r   rp   rr   ro   r   r   r   r   get_info_from_serverr  )r   previous_responseprevious_resultprevious_entriesr   r   r   r2    s$   "zConnection.refresh_server_infoc           	      C   s   | j d |d u r| j}t|tr^td|||d}t|}|pt}||}|rJ| dkr?tdgd }|	t
|| |  |	t
|| |  ttrUttd||  |W  d    S 	 W d    d S 1 sjw   Y  d S )NsearchResponse)
sort_orderr   -z"building LDIF output <%s> for <%s>)r   r   r   r%   rI   rJ   r   r   tellwriterk   rp   rr   ro   )	r   search_result
all_base64line_separatorr  r  
ldif_linesldif_outputheaderr   r   r   response_to_ldif  s&   

$zConnection.response_to_ldifry   c              
      sV  | j  |d u r| j}t|trt }g |d< |D ]P  d dkrit }	 d |	d< |rF|s>t fdd d D |	d< nt d |	d< |rb|sZt fdd d	 D |	d	< nt d	 |	d
< |d |	 qttu rrt| t	j
|d||dtdd}
ttrttd|
|  |r||
 |
W  d    S W d    d S 1 sw   Y  d S )NentriesrH  rJ  rh  c                 3   s,    | ]} d  | r| d  | fV  qdS )rX  Nr   r>  keyrL  r   r   	<genexpr>     * z.Connection.response_to_json.<locals>.<genexpr>rX  c                 3   s,    | ]} d  | r| d | fV  qdS )zraw:attributesraw_attributesNr   r  rL  r   r   r    r  r  rawT),z: )ensure_ascii	sort_keysindentcheck_circulardefault
separatorsz"building JSON output <%s> for <%s>)r   r   r   r%   rt  r   r}   bytesrl   jsondumpsrm   rp   rr   ro   r  )r   r  r  r  sortr  checked_attributesinclude_empty	json_dictrd  json_outputr   rL  r   response_to_json  s>   	
  
"zConnection.response_to_jsonc                 C   s   | j 7 | jr2t|trt|d}ttrttd|  || j	|||d |
  W d    d S W d    d S 1 s=w   Y  d S )Nzw+z!writing response to file for <%s>)r  r  r  )r   r   r   r$   r   rp   rr   ro   
writelinesr  r9  )r   targetr  r  r  r   r   r   response_to_file  s   


"zConnection.response_to_filec                 C   s  | j  | jr| jsd| _ttrttd| j| j| j|  z~z`| jr(| j	dd | jrP| j
ddsPd| jr:d| j nd }ttrHttd||  |   t|| jr[| jd| jd	 |d u rk| jjsk| jjttfv sy| jjsw| jjttfv sy|r}|   W n ty } zttrttd||   d }~ww W d| _nd| _w W d    d S W d    d S W d    d S 1 sw   Y  d S )
NTz?executing deferred (open: %s, start_tls: %s, bind: %s) for <%s>Fr   z!deferred start_tls not successfulr   r   r   )r   r3  )r   r   r   rp   rr   ro   r   r   r   r   r   r   rq   r   ri   r   r   r   infoget_infor-   r/   rQ  r.   r2  rc   )r   	read_infor   er   r   r   rM    sD   <"zConnection._fire_deferredc                 C   s$   | j r| js| | j | j| _| jS r   )r   r   _get_entriesr   r  r   r   r   r    s   zConnection.entriesc                 C   s  | j  ddlm}m} g }|D ]}|d dkr)t|d  }||vr)|| q|jdd d g }|D ]}	|D ]}
|
|	krB nq:||	 q6g }|D ]/}	|| jj	d	}|t
|	7 }||	|| jjrq|| ||d
 |d |	dn|| |dd|	df qMg }|D ]@}|d dkrt|d  }|D ]}||d kr|d |}||  nqdt| | _ttrtt| j|  t| jqW d    |S 1 sw   Y  |S )Nr   )	ObjectDefReaderrH  rJ  rX  c                 S   s
   t |  S r   )r|   )ro  r   r   r   rq  %  s   
 z)Connection._get_entries.<locals>.<lambda>)r  )rQ  basefilter)rX  r   r   zattribute set not found for )r   r   r  r  setkeysr   r  r   rQ  rN  r   r1  _create_entryr}   r   rp   rq   ro   rd   )r   search_responsesearch_requestr  r  	attr_setsr   resp_attr_setunique_attr_setsattr_set
unique_setobject_defs
object_defr  rd  r   r   r   r    s\   

2


**zConnection._get_entries)F)TN)NNNNNTNr   )NNN)TNN)T)NFNNN)FNry   TNTT)Fry   T))r   
__module____qualname____doc__r   r   r   r   r   r   r  r   propertyr  setterr  r!  r(  r   r6  r   r   r   re  rj  r}  r  r  r  r  r  r   r.  r0  r2  r  r  r  rM  r  r  r   r   r   r   r   z   s    A
 
<	 $'



v
2
#
y
>
a
/
`
;
#

"!=

2


r   )r  r   r   osr   	threadingr   	functoolsr   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(   r)   r*   r+   r,   r-   r.   r/   resultsr1   r2   r3   r   r4   poolingr5   r   r6   operation.abandonr7   r8   operation.addr9   r:   operation.bindr;   r<   operation.comparer=   r>   operation.deleter?   r@   operation.extendedrA   rB   operation.modifyrC   rD   operation.modifyDnrE   rF   operation.searchrG   rH   protocol.rfc2849rI   rJ   protocol.sasl.digestMd5rK   protocol.sasl.externalrL   protocol.sasl.plainrM   strategy.syncrN   strategy.safeSyncrO   strategy.safeRestartablerP   strategy.mockAsyncrQ   strategy.asynchronousrR   strategy.reusablerS   strategy.restartablerT   strategy.ldifProducerrU   strategy.mockSyncrV   strategy.asyncStreamrW   operation.unbindrX   protocol.rfc2696rY   r  rZ   r  r[   
exceptionsr\   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   rg   rh   ri   
utils.convrj   rk   rl   rm   rn   	utils.logro   rp   rq   rr   rs   rt   ru   utils.dnrv   utils.port_validatorsrw   r-  r   r   r   objectr   r   r   r   r   <module>   s|    @$