
    ?DeH                        d Z ddlmZ ddl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 ddlmZmZmZ dgZ G d d	ej                  Z G d
 dej                  Z G d dej                  Z G d dej                  Z G d de          Z G d dej                  Z G d dej                  Z G d de          Zeeej         <    G d dej                  Z! G d de          Z"e"ee"j         <    G d dej                  Z# G d dej                  Z$ G d  d!ej%                  Z& G d" d#ej                  Z' G d$ d%ej(                  Z) G d& d'          Z* G d( d          Z+d)S )*zz
ldap.syncrepl - for implementing syncrepl consumer (see RFC 4533)

See https://www.python-ldap.org/ for project details.
    )UUID)tag	namedtypenamedvaluniv
constraint)encoderdecoder)__version__
__author____license__)RequestControlResponseControlKNOWN_RESPONSE_CONTROLSSyncreplConsumerc                   4    e Zd ZdZ ej        dd          ZdS )SyncUUIDz.
    syncUUID ::= OCTET STRING (SIZE(16))
       N)__name__
__module____qualname____doc__r   ValueSizeConstraintsubtypeSpec     /./ldap/syncrepl.pyr   r      s.          1*0R88KKKr   r   c                       e Zd ZdZdS )
SyncCookiez%
    syncCookie ::= OCTET STRING
    N)r   r   r   r   r   r   r   r   r      s           r   r   c                   p    e Zd ZdZ ej        dd          Zej        j	         e
j        dd          z   Z	dS )SyncRequestModez
           mode ENUMERATED {
               -- 0 unused
               refreshOnly       (1),
               -- 2 reserved
               refreshAndPersist (3)
           },
    )refreshOnly   )refreshAndPersist   r#   r%   Nr   r   r   r   r   NamedValuesnamedValuesr   
Enumeratedr   r   SingleValueConstraintr   r   r   r!   r!   "   sW          '(&  K /-0P
0PQRTU0V0VVKKKr   r!   c            
           e Zd ZdZ ej         ej        d e                       ej        d e	                       ej
        d ej        d                              ZdS )SyncRequestValuea:  
       syncRequestValue ::= SEQUENCE {
           mode ENUMERATED {
               -- 0 unused
               refreshOnly       (1),
               -- 2 reserved
               refreshAndPersist (3)
           },
           cookie     syncCookie OPTIONAL,
           reloadHint BOOLEAN DEFAULT FALSE
       }
    modecookie
reloadHintFN)r   r   r   r   r   
NamedTypes	NamedTyper!   OptionalNamedTyper   DefaultedNamedTyper   BooleancomponentTyper   r   r   r,   r,   2   s~          )I(	FOO$5$566#	#Hjjll;;$	$\<4<3F3FGG MMMr   r,   c                   $    e Zd ZdZdZd	dZd ZdS )
SyncRequestControlan  
    The Sync Request Control is an LDAP Control [RFC4511] where the
    controlType is the object identifier 1.3.6.1.4.1.4203.1.9.1.1 and the
    controlValue, an OCTET STRING, contains a BER-encoded
    syncRequestValue.  The criticality field is either TRUE or FALSE.
    [..]
    The Sync Request Control is only applicable to the SearchRequest
    Message.
    z1.3.6.1.4.1.4203.1.9.1.1r#   Nr"   Fc                 >    || _         || _        || _        || _        d S )N)criticalityr.   r-   r/   )selfr9   r.   r-   r/   s        r   __init__zSyncRequestControl.__init__R   s#    &	$r   c                 \   t                      }|                    dt          | j                             | j        (|                    dt          | j                             | j        r-|                    dt          j        | j                             t          j
        |          S )Nr-   r.   r/   )r,   setComponentByNamer!   r-   r.   r   r/   r   r4   r	   encode)r:   rcvs     r   encodeControlValuez%SyncRequestControl.encodeControlValueX   s      vty'A'ABBB;"""8Z-D-DEEE? 	P""<do1N1NOOO~c"""r   )r#   Nr"   F)r   r   r   r   controlTyper;   r@   r   r   r   r7   r7   F   sH          -K% % % %# # # # #r   r7   c                   x    e Zd ZdZ ej        dddd          Zej        j	         e
j        dddd	          z   Z	d
S )SyncStateOpz
           state ENUMERATED {
               present (0),
               add (1),
               modify (2),
               delete (3)
           },
    )presentr   )addr#   )modify   )deleter%   r   r#   rG   r%   Nr&   r   r   r   rC   rC   b   sc          '(&	 K /-0P
0PQRTUWXZ[0\0\\KKKr   rC   c            	           e Zd ZdZ ej         ej        d e                       ej        d e                       ej	        d e
                                ZdS )SyncStateValuea  
       syncStateValue ::= SEQUENCE {
           state ENUMERATED {
               present (0),
               add (1),
               modify (2),
               delete (3)
           },
           entryUUID syncUUID,
           cookie    syncCookie OPTIONAL
       }
    state	entryUUIDr.   N)r   r   r   r   r   r0   r1   rC   r   r2   r   r5   r   r   r   rJ   rJ   t   sv          )I(	G[[]]33	K44#	#Hjjll;; MMMr   rJ   c                        e Zd ZdZdZdZd ZdS )SyncStateControlam  
    The Sync State Control is an LDAP Control [RFC4511] where the
    controlType is the object identifier 1.3.6.1.4.1.4203.1.9.1.2 and the
    controlValue, an OCTET STRING, contains a BER-encoded SyncStateValue.
    The criticality is FALSE.
    [..]
    The Sync State Control is only applicable to SearchResultEntry and
    SearchResultReference Messages.
    z1.3.6.1.4.1.4203.1.9.1.2)rD   rE   rF   rH   c                    t          j        |t                                }|d                             d          }t	          t          |d                             d                              }|d                             d          }|)|                                rt          |          | _        nd | _        | j	        j
        t          |                   | _        t          |          | _        d S )Nasn1Specr   rK   rL   bytesr.   )r
   decoderJ   getComponentByNamer   rS   hasValuestrr.   	__class__opnamesintrK   rL   )r:   encodedControlValuedrK   uuidr.   s         r   decodeControlValuez#SyncStateControl.decodeControlValue   s    N.9I9IJJJ!''00%! 7 7 D DEEFFF1((22&//"3"3f++DKKDK^+CJJ7
Tr   N)r   r   r   r   rA   rY   r^   r   r   r   rN   rN      s9          -K4G
# 
# 
# 
# 
#r   rN   c            	           e Zd ZdZ ej         ej        d e                       ej        d e	j
        d                              ZdS )SyncDoneValuez
       syncDoneValue ::= SEQUENCE {
           cookie          syncCookie OPTIONAL,
           refreshDeletes  BOOLEAN DEFAULT FALSE
       }
    r.   refreshDeletesFNr   r   r   r   r   r0   r2   r   r3   r   r4   r5   r   r   r   r`   r`      se          )I(#	#Hjjll;;$	$%5|t|E7J7JKK MMMr   r`   c                       e Zd ZdZdZd ZdS )SyncDoneControlaT  
    The Sync Done Control is an LDAP Control [RFC4511] where the
    controlType is the object identifier 1.3.6.1.4.1.4203.1.9.1.3 and the
    controlValue contains a BER-encoded syncDoneValue.  The criticality
    is FALSE (and hence absent).
    [..]
    The Sync Done Control is only applicable to the SearchResultDone
    Message.
    z1.3.6.1.4.1.4203.1.9.1.3c                 x   t          j        |t                                }|d                             d          }|                                rt          |          | _        nd | _        |d                             d          }|                                rt          |          | _        d S d | _        d S )NrP   r   r.   ra   )	r
   rT   r`   rU   rV   rW   r.   boolra   )r:   r[   r\   r.   refresh_deletess        r   r^   z"SyncDoneControl.decodeControlValue   s    N.III1((22?? 	f++DKKDKA$112BCC##%% 	'"&"7"7D"&Dr   N)r   r   r   r   rA   r^   r   r   r   rd   rd      s4          -K' ' ' ' 'r   rd   c            	           e Zd ZdZ ej         ej        d e                       ej        d e	j
        d                              ZdS )RefreshDeletez
           refreshDelete  [1] SEQUENCE {
               cookie         syncCookie OPTIONAL,
               refreshDone    BOOLEAN DEFAULT TRUE
           },
    r.   refreshDoneTNrb   r   r   r   ri   ri      d          )I(#	#Hjjll;;$	$]LDL4F4FGG MMMr   ri   c            	           e Zd ZdZ ej         ej        d e                       ej        d e	j
        d                              ZdS )RefreshPresentz
           refreshPresent [2] SEQUENCE {
               cookie         syncCookie OPTIONAL,
               refreshDone    BOOLEAN DEFAULT TRUE
           },
    r.   rj   TNrb   r   r   r   rm   rm      rk   r   rm   c                   &    e Zd ZdZ e            ZdS )	SyncUUIDsz(
    syncUUIDs      SET OF syncUUID
    N)r   r   r   r   r   r5   r   r   r   ro   ro      s%          HJJMMMr   ro   c            	           e Zd ZdZ ej         ej        d e                       ej        d e	j
        d                     ej        d e                                ZdS )	SyncIdSetz
     syncIdSet      [3] SEQUENCE {
         cookie         syncCookie OPTIONAL,
         refreshDeletes BOOLEAN DEFAULT FALSE,
         syncUUIDs      SET OF syncUUID
     }
    r.   ra   F	syncUUIDsN)r   r   r   r   r   r0   r2   r   r3   r   r4   r1   ro   r5   r   r   r   rq   rq      s}          )I(#	#Hjjll;;$	$%5|t|E7J7JKK	K55 MMMr   rq   c                   `   e Zd ZdZ ej         ej        d e                                 e	j
        e	j        e	j        d                               ej        d e                                 e	j
        e	j        e	j        d                               ej        d e                                 e	j
        e	j        e	j        d                               ej        d	 e                                 e	j
        e	j        e	j        d
                                        ZdS )SyncInfoValuea`  
       syncInfoValue ::= CHOICE {
           newcookie      [0] syncCookie,
           refreshDelete  [1] SEQUENCE {
               cookie         syncCookie OPTIONAL,
               refreshDone    BOOLEAN DEFAULT TRUE
           },
           refreshPresent [2] SEQUENCE {
               cookie         syncCookie OPTIONAL,
               refreshDone    BOOLEAN DEFAULT TRUE
           },
           syncIdSet      [3] SEQUENCE {
               cookie         syncCookie OPTIONAL,
               refreshDeletes BOOLEAN DEFAULT FALSE,
               syncUUIDs      SET OF syncUUID
           }
       }
    	newcookier   )implicitTagrefreshDeleter#   refreshPresentrG   	syncIdSetr%   N)r   r   r   r   r   r0   r1   r   subtyper   TagtagClassContexttagFormatSimpleri   rm   rq   r5   r   r   r   rt   rt      sf        $ )I(	JLL  #CGC$79LaPP !  	
 	
 		MOO###CGC$79LaPP $  	
 	
 		N$$#CGC$79LaPP %  	
 	
 		IKK#CGC$79LaPP    	
 	
' MMMr   rt   c                       e Zd ZdZdZd ZdS )SyncInfoMessagea	  
    The Sync Info Message is an LDAP Intermediate Response Message
    [RFC4511] where responseName is the object identifier
    1.3.6.1.4.1.4203.1.9.1.4 and responseValue contains a BER-encoded
    syncInfoValue.  The criticality is FALSE (and hence absent).
    z1.3.6.1.4.1.4203.1.9.1.4c                    t          j        |t                                }d | _        d | _        d | _        d | _        |d                                         }|d                                         }|t|	                                ra|dk    rt          |          | _        d S i }|                    d          }|	                                rt          |          |d<   |                    d          r&t          |                    d                    |d<   n|dk    rg }|                    d          }t          t          |                    D ]T}	t!          t#          |                    |	                    	          }
|                    t          |
                     U||d<   t          |                    d
                    |d
<   t)          | ||           d S d S d S )NrP   r   ru   r.   refreshrj   ry   rr   rR   ra   )r
   rT   rt   ru   rw   rx   ry   getNamegetComponentrV   rW   rU   
startswithrf   rangelenr   rS   getComponentByPositionappendsetattr)r:   encodedMessager\   attrcompvalr.   uuidsidsir]   s              r   r;   zSyncInfoMessage.__init__5  s   N>MOODDD!"
 t||~~t  ""{""!$TC,,X66F   , #FHy)) 	X%)$*A*A-*P*P%Q%QM""$$--k::s3xx , ,AeC,F,Fq,I,I&J&JKKKDLLT++++#(K (,T-D-DEU-V-V(W(W$%D$$$$$$- r   N)r   r   r   r   responseNamer;   r   r   r   r   r   ,  s4          .L#% #% #% #% #%r   r   c                   H    e Zd ZdZddZddZd Zd	 ZddZd Z	d Z
d ZdS )r   z;
    SyncreplConsumer - LDAP syncrepl consumer object.
    r"   Nc                     ||                                  }t          ||          }d|v r|dxx         |gz  cc<   n|g|d<   d| _         | j        ||fi |S )a  
        Starts syncrepl search operation.

        base, scope, and search_args are passed along to
        self.search_ext unmodified (aside from adding a Sync
        Request control to any serverctrls provided).

        mode provides syncrepl mode. Can be 'refreshOnly'
        to finish after synchronization, or
        'refreshAndPersist' to persist (continue to
        receive updates) after synchronization.

        cookie: an opaque value representing the replication
        state of the client.  Subclasses should override
        the syncrepl_set_cookie() and syncrepl_get_cookie()
        methods to store the cookie appropriately, rather than
        passing it.

        Only a single syncrepl search may be active on a SyncreplConsumer
        object.  Multiple concurrent syncrepl searches require multiple
        separate SyncreplConsumer objects and thus multiple connections
        (LDAPObject instances).
        N)r.   r-   serverctrlsF)syncrepl_get_cookier7   _SyncreplConsumer__refreshDone
search_ext)r:   basescoper-   r.   search_argssyncreqs          r   syncrepl_searchz SyncreplConsumer.syncrepl_search`  s    0 >--//F$F>>>K''&&&7)3&&&&*1K&"ttU::k:::r   r   c                 .   	 |                      ||ddd          \  }}}}}}	|dk    rU|D ]P}
|
j        j        dk    r|                     d|
j                   |
j        |                     |
j                   Qd	S |d
k    r|D ]}|\  }}}|D ]}
|
j        j        dk    r|
j        dk    r|                     |
j        g           ng|
j        dk    r| 	                    |
j        g           n@| 
                    |||
j                   | j        d	u r|                     |
j        g           |
j        |                     |
j                    ΐn|dk    r|D ]}|\  }}}|t          j        k    rt          |          }|j        |                     |j                   K|j        i|                     dd	           d|j        v r |                     |j        d                    |j        d         rd| _        |                                  |j        j|                     dd           d|j        v r |                     |j        d                    |j        d         rd| _        |                                  ,|j        y|j        d         du r!| 	                    |j        d                    n |                     |j        d                    d|j        v r |                     |j        d                    |dk    rdS )a  
        polls for and processes responses to the syncrepl_search() operation.
        Returns False when operation finishes, True if it is in progress, or
        raises an exception on error.

        If timeout is specified, raises ldap.TIMEOUT in the event of a timeout.

        If all is set to a nonzero value, poll() will return only when finished
        or when an exception is raised.

        Tr#   r   )msgidtimeoutadd_intermediates	add_ctrlsalle   rd   N)ra   Fd   rN   rD   rH   y   r.   rj   ra   rr   )result4rX   r   syncrepl_presentra   r.   syncrepl_set_cookierK   rL   syncrepl_deletesyncrepl_entryr   r   r   ru   rx   syncrepl_refreshdonerw   ry   )r:   r   r   r   typemsgmidctrlsnvcmdnattrsrnamerespsims                    r   syncrepl_pollzSyncreplConsumer.syncrepl_poll  s   I	*.,,"# +7 + +'D#sE1a s{{  ; ;A{+/@@@ ))$q?O)PPPx+00:::u  A'($Bu"  ;/3EEE$7i// 111;-@@@@W00 00!+???? //E1;GGG#1U:: $ 5 5q{m D D D8/ 44QX>>>"  N NA)*&E4 <<< )$//C}000????+7--d5-III#s'999 44S5G5QRRR-m< 815D. 55777*6--d4-HHH#s'888 44S5Fx5PQQQ,]; 815D. 557772=)9:dBB 00{1KLLLL 11#-2LMMM#s}44 44S]85LMMMaxxtSI	r   c                     dS )zY
        Called by syncrepl_poll() to store a new cookie provided by the server.
        Nr   )r:   r.   s     r   r   z$SyncreplConsumer.syncrepl_set_cookie  	     	r   c                     dS )zd
        Called by syncrepl_search() to retrieve the cookie stored by syncrepl_set_cookie()
        Nr   r:   s    r   r   z$SyncreplConsumer.syncrepl_get_cookie  r   r   Fc                     dS )aF  
        Called by syncrepl_poll() whenever entry UUIDs are presented to the client.
        syncrepl_present() is given a list of entry UUIDs (uuids) and a flag
        (refreshDeletes) which indicates whether the server explicitly deleted
        non-present entries during the refresh operation.

        If called with a list of uuids, the syncrepl_present() implementation
        should record those uuids as present in the directory.

        If called with uuids set to None and refreshDeletes set to False,
        syncrepl_present() should delete all non-present entries from the local
        mirror, and reset the list of recorded uuids.

        If called with uuids set to None and refreshDeletes set to True,
        syncrepl_present() should reset the list of recorded uuids, without
        deleting any entries.
        Nr   )r:   r   ra   s      r   r   z!SyncreplConsumer.syncrepl_present  s	    $ 	r   c                     dS )z
        Called by syncrepl_poll() to delete entries. A list
        of UUIDs of the entries to be deleted is given in the
        uuids parameter.
        Nr   )r:   r   s     r   r   z SyncreplConsumer.syncrepl_delete  s	     	r   c                     dS )z
        Called by syncrepl_poll() for any added or modified entries.

        The provided uuid is used to identify the provided entry in
        any future modification (including dn modification), deletion,
        and presentation operations.
        Nr   )r:   r   r   r]   s       r   r   zSyncreplConsumer.syncrepl_entry  s	     	r   c                     dS )z
        Called by syncrepl_poll() between refresh and persist phase.

        It indicates that initial synchronization is done and persist phase
        follows.
        Nr   r   s    r   r   z%SyncreplConsumer.syncrepl_refreshdone  s	     	r   )r"   N)r   Nr   )F)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   [  s         #; #; #; #;JU U U Ut       (        r   N),r   r]   r   pyasn1.typer   r   r   r   r   pyasn1.codec.berr	   r
   ldap.pkginfor   r   r   ldap.controlsr   r   r   __all__OctetStringr   r   r)   r!   Sequencer,   r7   rC   rJ   rN   rA   r`   rd   ri   rm   SetOfro   rq   Choicert   r   r   r   r   r   <module>r      s          C B B B B B B B B B B B B B - - - - - - - - = = = = = = = = = = R R R R R R R R R R 
9 9 9 9 9t 9 9 9    !   W W W W Wdo W W W     t}   (# # # # # # # #8] ] ] ] ]$/ ] ] ]$    T]   (# # # # # # # #2 9I (4 5
 
 
 
 
DM 
 
 
' ' ' ' 'o ' ' '2 8G 3 4
 
 
 
 
DM 
 
 

 
 
 
 
T] 
 
 
    
          , , , , ,DK , , ,^,% ,% ,% ,% ,% ,% ,% ,%^} } } } } } } } } }r   