
EUc           @   s  d  d l  Z  d  d l Z d  d l m Z d  d l m Z m Z m Z d  d l m	 Z	 d  d l
 m Z m Z d e f d     YZ d e f d	     YZ e d
   e j j   D  Z d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d      YZ d!   Z  d"   Z! d#   Z" d S($   iN(   t   dsdb(   t   drsblobst   drsuapit   misc(   t   dsdb_Dn(   t
   ndr_unpackt   ndr_packt   KCCErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR   $   s   t   NCTypec           B   s&   e  Z e d  d  \ Z Z Z Z Z RS(   i    i   (   R   R	   t   ranget   unknownt   schemat   domaint   configt   application(    (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR
   (   s   c         c   s1   |  ]' \ } } | d   d k r | | f Vq d S(   i   t   __N(    (   t   .0t   kt   v(    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pys	   <genexpr>,   s    t   NamingContextc           B   s_   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z RS(
   s   Base class for a naming context.

    Holds the DN, GUID, SID (if available) and type of the DN.
    Subclasses may inherit from this and specialize
    c         C   s+   | |  _  d |  _ d |  _ t j |  _ d S(   sK   Instantiate a NamingContext

        :param nc_dnstr: NC dn string
        N(   t   nc_dnstrt   Nonet   nc_guidt   nc_sidR
   R   t   nc_type(   t   selfR   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   __init__6   s    			c         C   s   d |  j  j f } | d |  j } | d t |  j  } |  j d k rW | d } n
 | d } | d t |  j |  j f } | S(   s!   Debug dump string output of classs   %s:s   
	nc_dnstr=%ss   
	nc_guid=%ss   
	nc_sid=<absent>s   
	nc_sid=<present>s   
	nc_type=%s (%s)N(	   t	   __class__R   R   t   strR   R   R   t
   nctype_lutR   (   R   t   text(    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   __str__@   s    
c         C   s   d d g } y( | j  d |  j d t j d |  } Wn5 t j k
 rk \ } } t d |  j | f   n X| d } d | k r t j | j d | d d   |  _	 n  d | k r | d d |  _
 n  |  j	 d  k	 s t  d  S(   Nt
   objectGUIDt	   objectSidt   baset   scopet   attrss)   Unable to find naming context (%s) - (%s)i    (   t   searchR   t   ldbt
   SCOPE_BASEt   LdbErrort	   ExceptionR   t   GUIDt   schema_format_valueR   R   R   t   AssertionError(   R   t   samdbR&   t   rest   enumt   estrt   msg(    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   load_ncO   s    	
c         C   s(   |  j  t j k s t  |  j  t j k S(   s   Return True if NC is schema(   R   R
   R   R.   R   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt	   is_schemab   s    c         C   s(   |  j  t j k s t  |  j  t j k S(   s   Return True if NC is domain(   R   R
   R   R.   R   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt	   is_domaing   s    c         C   s(   |  j  t j k s t  |  j  t j k S(   s    Return True if NC is application(   R   R
   R   R.   R   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   is_applicationl   s    c         C   s(   |  j  t j k s t  |  j  t j k S(   s   Return True if NC is config(   R   R
   R   R.   R   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt	   is_configq   s    c         C   s   |  j  d k r |  j |  n  |  j t | j    k rI t j |  _ nT |  j t | j	    k rs t j
 |  _ n* |  j d k	 r t j |  _ n t j |  _ d S(   so   Given an NC object, identify what type is is thru
           the samdb basedn strings and NC sid value
        N(   R   R   R4   R   R   t   get_schema_basednR
   R   R   t   get_config_basednR   R   R   R   (   R   R/   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   identify_by_basednv   s    c         C   sx   | d k r t  j |  _ n7 | d k r6 t  j |  _ n | d k rR |  j |  n  |  j t  j k rt |  j |  n  d S(   s>  Given an NC which has been discovered thru the
        nTDSDSA database object, determine what type of NC
        it is (i.e. schema, config, domain, application) via
        the use of the schema attribute under which the NC
        was found.

        :param attr: attr of nTDSDSA object where NC DN appears
        s   msDS-HasDomainNCst   hasPartialReplicaNCst   hasMasterNCsN(   R
   R   R   R;   R   (   R   R/   t   attr(    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   identify_by_dsa_attr   s    (   R   R	   t   __doc__R   R!   R4   R5   R6   R7   R8   R;   R?   (    (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR   /   s   	
							t	   NCReplicac           B   s   e  Z d  Z d   Z d   Z d d  Z d   Z d   Z d   Z	 d   Z
 d   Z d	   Z e d
  Z d   Z d   Z d   Z d   Z d   Z RS(   s   Naming context replica that is relative to a specific DSA.

    This is a more specific form of NamingContext class (inheriting from that
    class) and it identifies unique attributes of the DSA's replica for a NC.
    c         C   se   | |  _  | |  _ t |  _ t |  _ t |  _ d |  _ d |  _ g  |  _	 t |  _
 t j |  |  d S(   s   Instantiate a Naming Context Replica

        :param dsa_guid: GUID of DSA where replica appears
        :param nc_dnstr: NC dn string
        i    N(   t   rep_dsa_dnstrt   rep_dsa_guidt   Falset   rep_defaultt   rep_partialt   rep_rot   rep_instantiated_flagsR   t   rep_fsmo_role_ownert   rep_repsFromt   rep_present_criteria_oneR   R   (   R   t	   dsa_dnstrt   dsa_guidR   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR      s    									c         C   s   d |  j  j } | d |  j } | d |  j } | d |  j } | d |  j } | d |  j } | d |  j   } | d |  j } x |  j	 D] } | d	 | } q Wd
 t
 j |   | f S(   s!   Debug dump string output of classs   %s:s   
	dsa_dnstr=%ss   
	dsa_guid=%ss   
	default=%ss   
	ro=%ss   
	partial=%ss   
	present=%ss   
	fsmo_role_owner=%ss   
%ss   %s
%s(   R   R   RB   RC   RE   RG   RF   t
   is_presentRI   RJ   R   R!   (   R   R    t   rep(    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR!      s    c         C   s%   | d k r d |  _ n	 | |  _ d S(   s*   Set or clear NC replica instantiated flagsi    N(   R   RH   (   R   t   flags(    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   set_instantiated_flags   s    c         C   s   | d k r! t  |  _ t  |  _ n | d k r9 t  |  _ n | d k r t  |  _ |  j t | j    k r |  j t | j    k r t  |  _ q nB | d k r t  |  _ t  |  _ n! | d k r t  |  _ t	 |  _ n  t
 j |  | |  d S(   s   Given an NC which has been discovered thru the
        nTDSDSA database object, determine what type of NC
        replica it is (i.e. partial, read only, default)

        :param attr: attr of nTDSDSA object where NC DN appears
        R<   s   msDS-HasDomainNCsR=   s   msDS-hasFullReplicaNCss   msDS-hasMasterNCsN(   t   TrueRF   RK   RE   R   R   R9   R:   RG   RD   R   R?   (   R   R/   R>   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR?      s"    		
			c         C   s   |  j  S(   sM   Whether this is a default domain for the dsa that this NC appears on
        (   RE   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt
   is_default  s    c         C   s   |  j  S(   s&   Return True if NC replica is read only(   RG   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   is_ro  s    c         C   s   |  j  S(   s$   Return True if NC replica is partial(   RF   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt
   is_partial"  s    c         C   s'   |  j  r# |  j t j @d k r# t St S(   s7  Given an NC replica which has been discovered thru the
        nTDSDSA database object and populated with replica flags
        from the msDS-HasInstantiatedNCs; return whether the NC
        replica is present (true) or if the IT_NC_GOING flag is
        set then the NC replica is not present (false)
        i    (   RK   RH   R    t   INSTANCE_TYPE_NC_GOINGRR   RD   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyRN   &  s    	c         C   s   y+ | j  d |  j d t j d d g  } Wn5 t j k
 rb \ } } t d |  j | f   n X| d } d | k r xC | d D]4 } t |  j t t j	 |   } |  j
 j |  q Wn  d S(   s*  Given an NC replica which has been discovered thru the nTDSDSA
        database object, load the repsFrom attribute for the local replica.
        held by my dsa.  The repsFrom attribute is not replicated so this
        attribute is relative only to the local DSA that the samdb exists on
        R$   R%   R&   t   repsFroms!   Unable to find NC for (%s) - (%s)i    N(   R'   R   R(   R)   R*   R+   t
   RepsFromToR   R   t   repsFromToBlobRJ   t   append(   R   R/   R0   R1   R2   R3   t   valueRO   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   load_repsFrom2  s    
	c   	      C   s<  t  } g  } g  } xh |  j D]] } | j rD | j |  t } q n  | j   rc | j   t } n  | j t | j   q Wx | D] } |  j j	 |  q Wg  } | s | r d St
 j   } t
 j | |  j  | _ t
 j | t
 j d  | d <y | j |  Wn/ t
 j k
 r7} t d |  j | f   n Xd S(   s   Commit repsFrom to the databaseNRW   s&   Could not set repsFrom for (%s) - (%s)(   RD   RJ   t   to_be_deletedRZ   RR   t   is_modifiedt   set_unmodifiedR   t   ndr_blobt   removeR(   t   Messaget   DnR   t   dnt   MessageElementt   FLAG_MOD_REPLACEt   modifyR*   R+   (	   R   R/   t   roRg   t   newrepst   delrepsRW   t   mR2   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   commit_repsFromI  s2    	
	c         C   s   y+ | j  d |  j d t j d d g  } Wn5 t j k
 rb \ } } t d |  j | f   n X| d } d | k r | d d } t t j |  } | j	 d k r t
 d | j	   n  | j j |  _ n	 g  |  _ d	 S(
   sF  Given an NC replica which has been discovered thru the nTDSDSA
        database object, load the replUpToDateVector attribute for the
        local replica. held by my dsa. The replUpToDateVector
        attribute is not replicated so this attribute is relative only
        to the local DSA that the samdb exists on

        R$   R%   R&   t   replUpToDateVectors!   Unable to find NC for (%s) - (%s)i    i   s(   Unexpected replUpToDateVector version %dN(   R'   R   R(   R)   R*   R+   R   R   t   replUpToDateVectorBlobt   versiont   AttributeErrort   ctrt   cursorst   rep_replUpToDateVector_cursors(   R   R/   R0   R1   R2   R3   R[   t   blob(    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   load_replUpToDateVector  s     
		c         C   s   d j  d   |  j D  S(   Ns   
c         s   s$   |  ] } | j  r t |  Vq d  S(   N(   R]   R   (   R   t   x(    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pys	   <genexpr>  s    (   t   joinRJ   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   dumpstr_to_be_deleted  s    c         C   s   d j  d   |  j D  S(   Ns   
c         s   s'   |  ] } | j    r t |  Vq d  S(   N(   R^   R   (   R   Rv   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pys	   <genexpr>  s    (   Rw   RJ   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   dumpstr_to_be_modified  s    c         C   s   y+ | j  d |  j d t j d d g  } Wn5 t j k
 rb \ } } t d |  j | f   n X| d } d | k r | d |  _ n  d S(   s   Given an NC replica which has been discovered thru the nTDSDSA
        database object, load the fSMORoleOwner attribute.
        R$   R%   R&   t   fSMORoleOwners!   Unable to find NC for (%s) - (%s)i    N(   R'   R   R(   R)   R*   R+   RI   (   R   R/   R0   R1   R2   R3   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   load_fsmo_roles  s    
c         C   s&   |  j  d  k	 r" |  j  | k r" t St S(   N(   RI   R   RR   RD   (   R   RL   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   is_fsmo_role_owner  s    N(   R   R	   R@   R   R!   R   RQ   R?   RS   RT   RU   RN   R\   RD   Rl   Ru   Rx   Ry   R{   R|   (    (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyRA      s    			1					?	 			t   DirectoryServiceAgentc           B   s   e  Z d    Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z d   Z d   Z d   Z e d  Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   c         C   sg   | |  _  d |  _ d |  _ t |  _ t |  _ d |  _ d |  _ d |  _	 i  |  _
 i  |  _ i  |  _ d S(   s   Initialize DSA class.

        Class is subsequently fully populated by calling the load_dsa() method

        :param dsa_dnstr:  DN of the nTDSDSA
        i    N(   RL   R   RM   t   dsa_ividRD   t	   dsa_is_rot   dsa_is_istgt   dsa_optionst   dsa_behaviort   default_dnstrt   current_rep_tablet   needed_rep_tablet   connect_table(   R   RL   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR     s    										c         C   s  d |  j  j } |  j d k	 r3 | d |  j } n  |  j d k	 r\ | d t |  j  } n  |  j d k	 r | d t |  j  } n  | d |  j   } | d |  j   } | d |  j	   } | d } | d	 |  j
   } | d
 } | d	 |  j   } | d } | d	 |  j   } | S(   s!   Debug dump string output of classs   %s:s   
	dsa_dnstr=%ss   
	dsa_guid=%ss   
	dsa_ivid=%ss   
	ro=%ss   
	gc=%ss	   
	istg=%ss   
current_replica_table:s   
%ss   
needed_replica_table:s   
connect_table:N(   R   R   RL   R   RM   R   R~   RT   t   is_gct   is_istgt   dumpstr_current_replica_tablet   dumpstr_needed_replica_tablet   dumpstr_connect_table(   R   R    (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR!     s"    


c         C   s   |  j  j |  S(   N(   R   t   get(   R   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   get_current_replica  s    c         C   s   |  j  p |  j S(   sA   Returns True if dsa is intersite topology generator for it's site(   R   R   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR     s    c         C   s   |  j  S(   s1   Returns True if dsa a read only domain controller(   R   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyRT     s    c         C   s   |  j  t j @d k r t St S(   s*   Returns True if dsa hosts a global catalogi    (   t   optionsR    t   DS_NTDSDSA_OPT_IS_GCRR   RD   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR   
  s    c         C   s   |  j  | k r t St S(   s   Is dsa at minimum windows level greater than or equal to (version)

        :param version: Windows version to test against
            (e.g. DS_DOMAIN_FUNCTION_2008)
        (   R   RR   RD   (   R   Ro   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   is_minimum_behavior  s    c         C   s   |  j  t j @d k r t St S(   s>   Whether this allows NTDSConnection translation in its options.i    (   R   R    t%   DS_NTDSDSA_OPT_DISABLE_NTDSCONN_XLATERR   RD   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   is_translate_ntdsconn_disabled  s    c         C   s   |  j  |  j f S(   s5   Return DSA current and needed replica tables
        (   R   R   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   get_rep_tables   s    c         C   s   |  j  j d  \ } } } | S(   s(   Get the parent DN string of this object.t   ,(   RL   t	   partition(   R   t   headt   sept   tail(    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   get_parent_dnstr%  s    c         C   sy  d d d d d g } y( | j  d |  j d t j d |  } Wn5 t j k
 rt \ } } t d	 |  j | f   n X| d
 } t j | j d | d d
   |  _	 d | k r t j | j d | d d
   |  _
 n  d | k r t | d d
  |  _ n  d | k r,| d d
 d k r,t |  _ n	 t |  _ d | k r[t | d d
  |  _ n  |  j |  |  j |  d S(   s   Load a DSA from the samdb.

        Prior initialization has given us the DN of the DSA that we are to
        load.  This method initializes all other attributes, including loading
        the NC replica table for this DSA.
        R"   t   invocationIDR   s   msDS-isRODCs   msDS-Behavior-VersionR$   R%   R&   s&   Unable to find nTDSDSA for (%s) - (%s)i    t   invocationIdt   TRUEN(   R'   RL   R(   R)   R*   R+   R   R,   R-   RM   R~   t   intR   RR   R   RD   R   t   load_current_replica_tablet   load_connection_table(   R   R/   R&   R0   R1   R2   R3   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   load_dsa*  s4    	
 	c         C   s  d d d d d d g } y( | j  d |  j d t j d	 |  } Wn5 t j k
 rw \ } } t d
 |  j | f   n Xi  } t | d  d k rx| d j   D] } | d k r q n  x | d | D] } t | |  }	 |	 j	   }
 t
 |	 j  } | | k r-t |  j |  j |  } | | | <n
 | | } | d k rV| j |
  q n  | j | |  | j   r | |  _ q q Wq Wn t d |  j   | |  _ d S(   s#  Method to load the NC replica's listed for DSA object.

        This method queries the samdb for (hasMasterNCs, msDS-hasMasterNCs,
        hasPartialReplicaNCs, msDS-HasDomainNCs, msDS-hasFullReplicaNCs, and
        msDS-HasInstantiatedNCs) to determine complete list of NC replicas that
        are enumerated for the DSA.  Once a NC replica is loaded it is
        identified (schema, config, etc) and the other replica attributes
        (partial, ro, etc) are determined.

        :param samdb: database to query for DSA replica list
        R=   s   msDS-hasMasterNCsR<   s   msDS-HasDomainNCss   msDS-hasFullReplicaNCss   msDS-HasInstantiatedNCsR$   R%   R&   s*   Unable to find nTDSDSA NCs for (%s) - (%s)i    Rd   s   No nTDSDSA NCs for (%s)N(   R'   RL   R(   R)   R*   R+   t   lent   keysR   t   get_binary_integerR   Rd   RA   RM   RQ   R?   RS   R   R   (   R   R/   t   ncattrsR0   R1   R2   t	   tmp_tableR   R[   t   dsdnRP   t   dnstrRO   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR   Y  sB    	
c         C   s   | |  j  | j <d S(   s]   Method to add a NC replica that "should be present" to the
        needed_rep_table.
        N(   R   R   (   R   RO   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   add_needed_replica  s    c         C   s   y( | j  d |  j d t j d d  } Wn5 t j k
 r_ \ } } t d |  j | f   n XxX | D]P } t | j  } | |  j k r qg n  t	 |  } | j
 |  | |  j | <qg Wd S(   s   Method to load the nTDSConnections listed for DSA object.

        :param samdb: database to query for DSA connection list
        R$   R%   t
   expressions   (objectClass=nTDSConnection)s-   Unable to find nTDSConnection for (%s) - (%s)N(   R'   RL   R(   t   SCOPE_SUBTREER*   R+   R   Rd   R   t   NTDSConnectiont   load_connection(   R   R/   R0   R1   R2   R3   R   t   connect(    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR     s    	c         C   s   g  } x~ |  j  j   D]m \ } } | j r> | j | |  n  | j rZ | j | |  n  | j r | j | |  | j |  q q Wx | D] } |  j  | =q Wd S(   sz  Method to commit any uncommitted nTDSConnections
        modifications that are in our table.  These would be
        identified connections that are marked to be added or
        deleted

        :param samdb: database to commit DSA connection list to
        :param ro: if (true) then peform internal operations but
            do not write to the database (readonly)
        N(	   R   t   itemst   to_be_addedt   commit_addedt   to_be_modifiedt   commit_modifiedR]   t   commit_deletedRZ   (   R   R/   Rh   t   delconnR   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   commit_connections  s    
			c         C   s&   | |  j  k s t  | |  j  | <d  S(   N(   R   R.   (   R   R   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   add_connection  s    c         C   sF   g  } x9 |  j  j   D]( } | j   | k r | j |  q q W| S(   s   Scan DSA nTDSConnection table and return connection
        with a "fromServer" dn string equivalent to method
        input parameter.

        :param from_dnstr: search for this from server entry
        (   R   t   valuest   get_from_dnstrRZ   (   R   t
   from_dnstrt   answerR   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   get_connection_by_from_dnstr  s
    c         C   s   d j  d   |  j D  S(   s1   Debug dump string output of current replica tables   
c         s   s   |  ] } t  |  Vq d  S(   N(   R   (   R   Rv   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pys	   <genexpr>  s    (   Rw   R   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR     s    c         C   s   d j  d   |  j D  S(   s0   Debug dump string output of needed replica tables   
c         s   s   |  ] } t  |  Vq d  S(   N(   R   (   R   Rv   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pys	   <genexpr>  s    (   Rw   R   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR     s    c         C   s   d j  d   |  j D  S(   s)   Debug dump string output of connect tables   
c         s   s   |  ] } t  |  Vq d  S(   N(   R   (   R   Rv   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pys	   <genexpr>  s    (   Rw   R   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR      s    c         C   s   d t  t j    |  j } t |  } t | _ t | _ | | _ | | _	 | | _
 | d k	 r} | j | _ | j | _ n  | d k	 r | | _ n t   | _ |  j | |  | S(   s   Set up a new connection for the DSA based on input
        parameters.  Connection will be added to the DSA
        connect_table and will be marked as "to be added" pending
        a call to commit_connections()
        s   CN=%s,N(   R   t   uuidt   uuid4RL   R   RR   R   t   enabledR   R   RP   R   R   t   transport_dnstrt   guidt   transport_guidt   schedulet   new_connection_scheduleR   (   R   R   RP   t	   transportR   t   schedR   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   new_connection  s    					(   R   R	   R   R!   R   R   RT   R   R   R   R   R   R   R   R   R   RD   R   R   R   R   R   R   R   (    (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR}     s*   							
				/	R							R   c           B   s   e  Z d  Z d   Z d   Z d   Z d   Z e d  Z e d  Z	 e d  Z
 d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   s5   Class defines a nTDSConnection found under a DSA
    c         C   sy   | |  _  d  |  _ t |  _ d |  _ t |  _ t |  _ t |  _ d |  _	 d |  _
 d  |  _ d  |  _ d  |  _ d  |  _ d  S(   Ni    (   R   R   R   RD   R   t   whenCreatedR   R]   R   R   t   system_flagsR   R   R   R   (   R   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR   %  s    												c         C   s  d |  j  j |  j f } | d |  j } | d |  j } | d |  j } | d |  j } | d |  j } | d |  j } | d |  j	 } | d	 |  j
 } |  j d k	 r | d
 t |  j  } n  |  j d k	 r | d t |  j  } n  | d |  j } |  j d k	 r| d |  j j 7} | d |  j j 7} | d |  j j 7} x t |  j j  D]~ \ } } | d | | j f 7} | d | | j f 7} | d | 7} x) |  j j | j D] } | d | } qW| d } qbWn  | S(   s1   Debug dump string output of NTDSConnection objects
   %s:
	dn=%ss   
	enabled=%ss   
	to_be_added=%ss   
	to_be_deleted=%ss   
	to_be_modified=%ss   
	options=0x%08Xs   
	system_flags=0x%08Xs   
	whenCreated=%ds   
	transport_dn=%ss	   
	guid=%ss   
	transport_guid=%ss   
	from_dn=%ss   
	schedule.size=%ss   
	schedule.bandwidth=%ss   
	schedule.numberOfSchedules=%ss"   
	schedule.headerArray[%d].type=%ds$   
	schedule.headerArray[%d].offset=%ds    
	schedule.dataArray[%d].slots[ s   0x%X t   ]N(   R   R   R   R   R   R]   R   R   R   R   R   R   R   R   R   R   R   t   sizet	   bandwidtht   numberOfSchedulest	   enumeratet   headerArrayt   typet   offsett	   dataArrayt   slots(   R   R    t   it   headert   slot(    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR!   4  s:    c         C   s/  d d d d d d d d g } y( | j  d	 |  j d
 t j d |  } Wn5 t j k
 r} \ } } t d |  j | f   n X| d } d | k r t | d d  |  _ n  d | k r | d d j   j	   j
   d k r t |  _ q n  d | k rt | d d  |  _ n  d | k rJt j | j d | d d   |  _ n  d | k rt | | d d  } |  j | t | j   n  d | k rt t j | d d  |  _ n  d | k rt j | d d  |  _ n  d | k r+t | | d d  } t | j  |  _ |  j d k	 s+t  n  d S(   s   Given a NTDSConnection object with an prior initialization
        for the object's DN, search for the DN and load attributes
        from the samdb.
        R   t   enabledConnectionR   R   R"   t   transportTypet
   fromServert   systemFlagsR$   R%   R&   s-   Unable to find nTDSConnection for (%s) - (%s)i    R   N(   R'   R   R(   R)   R*   R+   R   R   t   uppert   lstript   rstripRR   R   R   R   R,   R-   R   R   t   load_connection_transportR   Rd   R   R   R   t   string_to_timeR   R   R   R.   (   R   R/   R&   R0   R1   R2   R3   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR   [  sH    	
& c         C   s   d g } y% | j  d | d t j d |  } Wn2 t j k
 rb \ } } t d | | f   n Xd | d k r | d } | |  _ t j | j d | d d   |  _	 n  |  j d k	 s t  |  j	 d k	 s t  d S(   s   Given a NTDSConnection object which enumerates a transport
        DN, load the transport information for the connection object

        :param tdnstr: transport DN to load
        R"   R$   R%   R&   s$   Unable to find transport (%s) - (%s)i    N(   R'   R(   R)   R*   R+   R   R   R,   R-   R   R   R.   (   R   R/   t   tdnstrR&   R0   R1   R2   R3   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR     s    	
	c         C   sr   |  j  s t  t |  _  | r" d Sy | j |  j  Wn5 t j k
 rm \ } } t d |  j | f   n Xd S(   s   Local helper routine for commit_connections() which
        handles committed connections that are to be deleted from
        the database database
        Ns/   Could not delete nTDSConnection for (%s) - (%s)(   R]   R.   RD   t   deleteR   R(   R*   R+   (   R   R/   Rh   R1   R2   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR     s    	c   	      C   s  |  j  s t  t |  _  | r" d St } y= | j d |  j d t j  } t |  d k rd t } n  WnG t j	 k
 r \ } } | t j
 k r t d |  j | f   q n X| r t d |  j   n  |  j r d } n d } t j   } t j | |  j  | _ t j d	 t j d
  | d
 <t j d t j d  | d <t j | t j d  | d <t j |  j t j d  | d <t j t |  j  t j d  | d <t j t |  j  t j d  | d <|  j d k	 rt j t |  j  t j d  | d <n  |  j d k	 r2t j t |  j  t j d  | d <n  y | j |  Wn5 t j	 k
 rz\ } } t d |  j | f   n Xd S(   s   Local helper routine for commit_connections() which
        handles committed connections that are to be added to the
        database
        NR$   R%   i    s    Unable to search for (%s) - (%s)s'   nTDSConnection for (%s) already exists!R   t   FALSEt   nTDSConnectiont   objectClasst   showInAdvancedViewOnlyR   R   R   R   R   R   s,   Could not add nTDSConnection for (%s) - (%s)(   R   R.   RD   R'   R   R(   R)   R   RR   R*   t   ERR_NO_SUCH_OBJECTR+   R   Rb   Rc   Rd   Re   t   FLAG_MOD_ADDR   R   R   R   R   R   R   R   t   add(	   R   R/   Rh   t   foundR3   R1   R2   t	   enablestrRk   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR     sT    			%c         C   sS  |  j  s t  t |  _  | r" d Sy  | j d |  j d t j  WnZ t j k
 r \ } } | t j k r t	 d |  j   n  t
 d |  j | f   n X|  j r d } n d } t j   } t j | |  j  | _ t j | t j d  | d <t j |  j t j d	  | d	 <t j t |  j  t j d
  | d
 <t j t |  j  t j d  | d <|  j d k	 rt j t |  j  t j d  | d <n t j g  t j d  | d <|  j d k	 rt j t |  j  t j d  | d <n t j g  t j d  | d <y | j |  Wn5 t j k
 rN\ } } t d |  j | f   n Xd S(   s   Local helper routine for commit_connections() which
        handles committed connections that are to be modified to the
        database
        NR$   R%   s&   nTDSConnection for (%s) doesn't exist!s    Unable to search for (%s) - (%s)R   R   R   R   R   R   R   R   s/   Could not modify nTDSConnection for (%s) - (%s)(   R   R.   RD   R'   R   R(   R)   R*   R   R   t   KccErrorR   Rb   Rc   Rd   Re   Rf   R   R   R   R   R   R   t   FLAG_MOD_DELETER   R   Rg   R+   (   R   R/   Rh   R1   R2   R   Rk   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR     sL    	 		c         C   s   | |  _  d  S(   N(   R   (   R   t	   truefalse(    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   set_modifiedD  s    c         C   s   | |  _  d  S(   N(   R   (   R   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt	   set_addedG  s    c         C   s   | |  _  d  S(   N(   R]   (   R   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   set_deletedJ  s    c         C   s_   |  j  d k s% |  j  j d d k r) t Sx/ |  j  j d j D] } | d @d k r= t Sq= Wt S(   sz   Returns True if our schedule includes at least one
        replication interval within the week.  False otherwise
        i    i   N(   R   R   R   RD   R   RR   (   R   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt!   is_schedule_minimum_once_per_weekM  s    %c         C   s1  |  j  d k	 r" | d k r2 t Sn | d k r2 t S|  j  j | j k sq |  j  j | j k sq |  j  j | j k ru t Sx t |  j  j  D] \ } } |  j  j | j	 | j | j	 k r t S|  j  j | j
 | j | j
 k r t SxD t |  j  j | j | j | j  D] \ } } | | k r	t Sq	Wq Wt S(   s   Returns True if our schedule is equivalent to the input
        comparison schedule.

        :param shed: schedule to compare to
        N(   R   R   RD   RR   R   R   R   R   R   R   R   t   zipR   R   (   R   R   R   R   t   at   b(    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   is_equivalent_schedule[  s(    #c         C   s   |  j  t j @d k r t St S(   sM   Returns True if NTDS Connection specifies RODC
        topology only
        i    (   R   R    t   NTDSCONN_OPT_RODC_TOPOLOGYRD   RR   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   is_rodc_topology{  s    c         C   s   |  j  t j @d k r t St S(   s   Returns True if NTDS Connection was generated by the
        KCC topology algorithm as opposed to set by the administrator
        i    (   R   R    t   NTDSCONN_OPT_IS_GENERATEDRD   RR   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   is_generated  s    c         C   s   |  j  t j @d k r t St S(   sG   Returns True if NTDS Connection should override notify default
        i    (   R   R    t$   NTDSCONN_OPT_OVERRIDE_NOTIFY_DEFAULTRD   RR   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   is_override_notify_default  s    c         C   s   |  j  t j @d k r t St S(   s:   Returns True if NTDS Connection should use notify
        i    (   R   R    t   NTDSCONN_OPT_USE_NOTIFYRD   RR   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   is_use_notify  s    c         C   s   |  j  t j @d k r t St S(   s?   Returns True if NTDS Connection should use twoway sync
        i    (   R   R    t   NTDSCONN_OPT_TWOWAY_SYNCRD   RR   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   is_twoway_sync  s    c         C   s   |  j  t j @d k r t St S(   sR   Returns True if NTDS Connection intersite compression
        is disabled
        i    (   R   R    t*   NTDSCONN_OPT_DISABLE_INTERSITE_COMPRESSIONRD   RR   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt!   is_intersite_compression_disabled  s    c         C   s   |  j  t j @d k r t St S(   sB   Returns True if NTDS Connection has a user owned schedule
        i    (   R   R    t    NTDSCONN_OPT_USER_OWNED_SCHEDULERD   RR   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   is_user_owned_schedule  s    c         C   s   |  j  S(   s3   Returns True if NTDS Connection is enabled
        (   R   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt
   is_enabled  s    c         C   s   |  j  S(   s%   Return fromServer dn string attribute(   R   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR     s    (   R   R	   R@   R   R!   R   R   RD   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R  R  R   (    (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR   "  s,   		'	5	EE					 								t	   Partitionc           B   sD   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   s!  A naming context discovered thru Partitions DN of the config schema.

    This is a more specific form of NamingContext class (inheriting from that
    class) and it identifies unique attributes enumerated in the Partitions
    such as which nTDSDSAs are cross referenced for replicas
    c         C   sA   | |  _  t |  _ d |  _ g  |  _ g  |  _ t j |  d   d  S(   Ni    (	   t   partstrRR   R   R   t   rw_location_listt   ro_location_listR   R   R   (   R   R  (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR     s    					c         C   s  d d d d d g } y( | j  d |  j d t j d |  } Wn5 t j k
 rt \ } } t d	 |  j | f   n X| d
 } x.| j   D] } | d k r q n  | d k r | | d
 j   j   j	   d k r t
 |  _ q t |  _ q n  | d k rt | | d
  |  _ q n  x | | D] } t | |  }	 t |	 j  }
 | d k rd|
 |  _ q%n  | d k r|  j j |
  q%n  | d k r%|  j j |
  q%q%q%Wq W|  j |  d S(   s  Given a Partition class object that has been initialized with its
        partition dn string, load the partition from the sam database, identify
        the type of the partition (schema, domain, etc) and record the list of
        nTDSDSAs that appear in the cross reference attributes
        msDS-NC-Replica-Locations and msDS-NC-RO-Replica-Locations.

        :param samdb: sam database to load partition from
        t   nCNamet   EnabledR   s   msDS-NC-Replica-Locationss   msDS-NC-RO-Replica-LocationsR$   R%   R&   s(   Unable to find partition for (%s) - (%s)i    Rd   R   N(   R'   R  R(   R)   R*   R+   R   R   R   R   RR   R   RD   R   R   R   R   Rd   R   R  RZ   R  R;   (   R   R/   R&   R0   R1   R2   R3   R   R[   R   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   load_partition  sH    		
&		c         C   s   |  j  S(   s-   Returns True if partition is enabled
        (   R  (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR    s    c         C   s"   |  j  t j @d k r t St Sd S(   s   Returns True if this is not an Active Directory NC in our
        forest but is instead something else (e.g. a foreign NC)
        i    N(   R   R    t   SYSTEM_FLAG_CR_NTDS_NCRR   RD   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt
   is_foreign  s    c         C   s?  t  } t  } |  j t j k pQ |  j t j k pQ |  j t j k oQ |  j | j k } |  j t j k r | j	   r | j
 |  j k r t } q q | j
 |  j k r t } q n  | j   r|  j t j k r|  j | j k r| j
 |  j k s| j
 |  j k rt } t } n  | r2| j	   s)| r2t } n  | | | f S(   s   Tests whether this partition should have an NC replica
        on the target dsa.  This method returns a tuple of
        needed=True/False, ro=True/False, partial=True/False

        :param target_dsa: should NC be present on target dsa
        (   RD   R   R
   R   R   R   R   R   R   RT   RL   R  RR   R  R   (   R   t
   target_dsaRh   t   partialt   needed(    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   should_be_present  s,    		c         C   sl   d t  j |   } | d |  j } x |  j D] } | d | } q. Wx |  j D] } | d | } qP W| S(   s!   Debug dump string output of classs   %ss   
	partdn=%ss   
	msDS-NC-Replica-Locations=%ss!   
	msDS-NC-RO-Replica-Locations=%s(   R   R!   R  R  R  (   R   R    R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR!   D  s    (	   R   R	   R@   R   R
  R  R  R  R!   (    (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR    s   		:				0t   Sitec           B   s   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z d   Z d   Z d   Z d   Z RS(   s   An individual site object discovered thru the configuration
    naming context.  Contains all DSAs that exist within the site
    c         C   sL   | |  _  d  |  _ d |  _ d  |  _ d |  _ i  |  _ i  |  _ | |  _ d  S(   Ni    (	   t
   site_dnstrR   t	   site_guidt   site_optionst   site_topo_generatort   site_topo_failovert	   dsa_tablet   rw_dsa_tablet   nt_now(   R   R  R  (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR   S  s    							c   	      C   si  d |  j  } d d d g } yL | j d | d t j d |  } | j d |  j  d t j d d g  } Wn2 t j k
 r \ } } t d	 | | f   n X| d
 } d | k r t | d d
  |  _ n  d | k r t | d d
  |  _	 n  d | k rt | d d
  |  _
 n  | d
 } d | k rXt j | j d | d d
   |  _ n  |  j |  d S(   s   Loads the NTDS Site Settions options attribute for the site
        as well as querying and loading all DSAs that appear within
        the site.
        s   CN=NTDS Site Settings,%sR   t   interSiteTopologyFailovert   interSiteTopologyGeneratorR$   R%   R&   R"   s,   Unable to find site settings for (%s) - (%s)i    N(   R  R'   R(   R)   R*   R+   R   R  R   R  R  R   R,   R-   R  t   load_all_dsa(	   R   R/   t   ssdnR&   R0   t   self_resR1   R2   R3   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt	   load_site]  s0    		

c         C   s   y% | j  |  j d t j d d } Wn, t j k
 rS \ } } t d |   n Xxt | D]l } t | j  } | |  j k r q[ n  t	 |  } | j
 |  | |  j | <| j   s[ | |  j | <q[ q[ Wd S(   s   Discover all nTDSDSA thru the sites entry and
        instantiate and load the DSAs.  Each dsa is inserted
        into the dsa_table by dn string.
        R%   R   s   (objectClass=nTDSDSA)s   Unable to find nTDSDSAs - (%s)N(   R'   R  R(   R   R*   R+   R   Rd   R  R}   R   RT   R  (   R   R/   R0   R1   R2   R3   R   t   dsa(    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR    s    	c         C   s7   x0 |  j  j   D] } t | j  | k r | Sq Wd  S(   N(   R  R   R   RM   R   (   R   t   guidstrR   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   get_dsa_by_guidstr  s    c         C   s   |  j  j |  S(   s   Return a previously loaded DSA object by consulting
        the sites dsa_table for the provided DSA dn string

        :return: None if DSA doesn't exist
        (   R  R   (   R   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   get_dsa  s    c         C   sa  | j  r" t | _ | j |  _ t St |  } | j |  | j |  t |  j	 j
   d t } |  j d k r{ d } n |  j d d } |  j j |  j  } | d k	 rJ| | k	 rJ| j |  } t }	 x- | j D]" }
 | j |
 j k r t }	 Pq q W|	 s| } d } qb|  j |
 j | k r8d } d } qb| } |
 j } n | j |  } |  j } | |  j | | t |  } | | } t | _ | | k	 rt S|  j | j k rt S| j |  _ | rt Sd |  j } t j   } t j | |  | _ t j | j t j d  | d <y | j |  Wn, t j  k
 r\} t! d | | f   n Xt S(   sl  Determine if my DC should be an intersite topology
        generator.  If my DC is the istg and is both a writeable
        DC and the database is opened in write mode then we perform
        an originating update to set the interSiteTopologyGenerator
        attribute in the NTDS Site Settings object.  An RODC always
        acts as an ISTG for itself.
        t   cmpi    i   i<   i s   CN=NTDS Site Settings,%sR  s8   Could not set interSiteTopologyGenerator for (%s) - (%s)ix   i   l    PC N("   R   RR   R   RL   R  t   get_dsa_config_repR\   Ru   t   sortedR  R   t   sort_dsa_by_guidR  R  R   R   t   indexRD   Rs   R~   t   source_dsa_invocation_idR  t   last_sync_successR   R  R(   Rb   Rc   Rd   Re   Rf   Rg   R*   R+   (   R   R/   t   mydsaRh   t   c_rept   D_sortt   ft   d_dsat   j_idxR   t   cursort   i_idxt   t_timet   k_idxR  Rk   R2   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   select_istg  sf    							

	c         C   s   |  j  t j @d k S(   s8   Returns True if intra-site topology is disabled for sitei    (   R  R    t-   DS_NTDSSETTINGS_OPT_IS_AUTO_TOPOLOGY_DISABLED(   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   is_intrasite_topology_disabledS  s    c         C   s   |  j  t j @d k S(   s8   Returns True if inter-site topology is disabled for sitei    (   R  R    t8   DS_NTDSSETTINGS_OPT_IS_INTER_SITE_AUTO_TOPOLOGY_DISABLED(   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   is_intersite_topology_disabledX  s    c         C   s   |  j  t j @d k S(   s:   Returns True if selection of random bridgehead is disabledi    (   R  R    t1   DS_NTDSSETTINGS_OPT_IS_RAND_BH_SELECTION_DISABLED(   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   is_random_bridgehead_disabled^  s    c         C   s   |  j  t j @d k S(   s1   Returns True if detect stale is disabled for sitei    (   R  R    t1   DS_NTDSSETTINGS_OPT_IS_TOPL_DETECT_STALE_DISABLED(   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   is_detect_stale_disabledc  s    c         C   s   |  j  t j @d k S(   s<   Returns True if NTDS Connection cleanup is disabled for sitei    (   R  R    t,   DS_NTDSSETTINGS_OPT_IS_TOPL_CLEANUP_DISABLED(   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   is_cleanup_ntdsconn_disabledh  s    c         C   s   |  j  | j  r t St S(   s"   Return True if dsa is in this site(   R#  RL   RR   RD   (   R   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt	   same_sitem  s    c         C   s2   t  |  j  d k r. t  |  j  d k r. t St S(   Ni    (   R   R  R  RR   RD   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   is_rodc_sites  s    *c         C   s   d |  j  j } | d |  j } | d |  j } | d |  j } | d |  j } x+ |  j j   D] \ } } | d | } qd W| S(   s!   Debug dump string output of classs   %s:s   
	dn=%ss   
	options=0x%Xs   
	topo_generator=%ss   
	topo_failover=%ds   
%s(   R   R   R  R  R  R  R  R   (   R   R    t   keyR   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR!   x  s    (   R   R	   R@   R   R  R  R"  R#  R5  R7  R9  R;  R=  R?  R@  RA  R!   (    (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR  O  s   	
	$											t	   GraphNodec           B   sD   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   s   A graph node describing a set of edges that should be directed to it.

    Each edge is a connection for a particular naming context replica directed
    from another node in the forest to this node.
    c         C   s   | |  _  | |  _ g  |  _ d S(   s   Instantiate the graph node according to a DSA dn string

        :param max_node_edges: maximum number of edges that should ever
            be directed to the node
        N(   t	   max_edgesRL   t	   edge_from(   R   RL   t   max_node_edges(    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR     s    		c         C   sj   d |  j  j } | d |  j } | d |  j } x1 t |  j  D]  \ } } | d | | f } qB W| S(   Ns   %s:s   
	dsa_dnstr=%ss   
	max_edges=%ds   
	edge_from[%d]=%s(   R   R   RL   RD  R   RE  (   R   R    R   t   edge(    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR!     s    c         C   sh   | d k	 s t  | |  j k r% t S| |  j k r8 t St |  j  |  j k rT t S|  j j |  t S(   s   Add an edge from the dsa to our graph nodes edge from list

        :param from_dsa_dnstr: the dsa that the edge emanates from
        N(	   R   R.   RL   RD   RE  R   RD  RZ   RR   (   R   t   from_dsa_dnstr(    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   add_edge_from  s    c         C   s.   x' | j  j   D] } |  j | j  q Wd S(   se  For each nTDSConnection object associated with a particular
        DSA, we test if it implies an edge to this graph node (i.e.
        the "fromServer" attribute).  If it does then we add an
        edge from the server unless we are over the max edges for this
        graph node

        :param dsa: dsa with a dnstr equivalent to his graph node
        N(   R   R   RI  R   (   R   R   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   add_edges_from_connections  s    	c         C   s   x |  j  D] } | j |  } t } x& | D] } | j   rD q, n  t } q, W| rZ q
 n  t j } t j t j B} | j	 | | d | d  q
 Wd S(   s   For each edge directed to this graph node, ensure there
           is a corresponding nTDSConnection object in the dsa.
        N(   RE  R   RD   R   RR   R    R   t   SYSTEM_FLAG_CONFIG_ALLOW_RENAMEt   SYSTEM_FLAG_CONFIG_ALLOW_MOVER   R   (   R   R   t
   edge_dnstrt   connectionst   found_validR   t   optRP   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   add_connections_from_edges  s    
	
c         C   s    t  |  j  |  j k r t St S(   s<   Return True if we have met the maximum "from edges" criteria(   R   RE  RD  RR   RD   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   has_sufficient_edges  s    (	   R   R	   R@   R   R!   RI  RJ  RQ  RR  (    (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyRC    s   	
					't	   Transportc           B   s)   e  Z d  Z d   Z d   Z d   Z RS(   s;   Class defines a Inter-site transport found under Sites
    c         C   s:   | |  _  d |  _ d  |  _ d  |  _ d  |  _ g  |  _ d  S(   Ni    (   R   R   R   R   t   namet   address_attrt   bridgehead_list(   R   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR     s    					c         C   s   d |  j  j |  j f } | d t |  j  } | d |  j } | d |  j } | d |  j } x |  j D] } | d | } qm W| S(   s,   Debug dump string output of Transport objects
   %s:
	dn=%ss	   
	guid=%ss   
	options=%ds   
	address_attr=%ss	   
	name=%ss   
	bridgehead_list=%s(	   R   R   R   R   R   R   RU  RT  RV  (   R   R    R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR!     s    c   
      C   s  d d d d d g } y( | j  d |  j d t j d |  } Wn5 t j k
 rt \ } } t d	 |  j | f   n X| d
 } t j | j d | d d
   |  _	 d | k r t
 | d d
  |  _ n  d | k r t | d d
  |  _ n  d | k rt | d d
  |  _ n  d | k r{xU | d D]F } t | |  } t | j  }	 |	 |  j k r.|  j j |	  q.q.Wn  d S(   s   Given a Transport object with an prior initialization
        for the object's DN, search for the DN and load attributes
        from the samdb.
        R"   R   RT  t   bridgeheadServerListBLt   transportAddressAttributeR$   R%   R&   s(   Unable to find Transport for (%s) - (%s)i    N(   R'   R   R(   R)   R*   R+   R   R,   R-   R   R   R   R   RU  RT  R   Rd   RV  RZ   (
   R   R/   R&   R0   R1   R2   R3   R[   R   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   load_transport  s4    	
(   R   R	   R@   R   R!   RY  (    (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyRS    s   		RX   c           B   sJ   e  Z d  Z d d d  Z d   Z d   Z d   Z d   Z d   Z	 RS(   s   Class encapsulation of the NDR repsFromToBlob.

    Removes the necessity of external code having to
    understand about other_info or manipulation of
    update flags.
    c         C   s  t  |  j d <| |  j d <d |  j d <| d  k r t j   |  j d <d |  j d _ d  |  j d <d  |  j d <t j   |  j d j _ |  j d	 <n | |  j d <| j j |  j d	 <| j d k r | j j j	 |  j d <d  |  j d <n, | j j j
 |  j d <| j j j |  j d <d  S(
   NR]   R   i    t   update_flagsR`   i   t	   dns_name1t	   dns_name2t
   other_info(   RD   t   __dict__R   R   RY   Ro   t   repsFromTo1OtherInfoRq   R]  t   dns_nameR[  R\  (   R   R   R`   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR   2  s     'c         C   s  d |  j  j } | d |  j 7} | d |  j 7} | d |  j 7} | d |  j 7} | d |  j 7} | d |  j 7} | d |  j 7} | d	 |  j	 7} | d
 |  j
 7} | d |  j 7} | d |  j 7} | d |  j 7} | d 7} x |  j D] } | d | 7} q W| d 7} | S(   s!   Debug dump string output of classs   %s:s
   
	dnstr=%ss   
	update_flags=0x%Xs   
	version=%ds   
	source_dsa_obj_guid=%ss   
	source_dsa_invocation_id=%ss   
	transport_guid=%ss   
	replica_flags=0x%Xs   
	consecutive_sync_failures=%ds   
	last_success=%ss   
	last_attempt=%ss   
	dns_name1=%ss   
	dns_name2=%ss   
	schedule[ s   0x%X R   (   R   R   R   RZ  Ro   t   source_dsa_obj_guidR)  R   t   replica_flagst   consecutive_sync_failurest   last_successt   last_attemptR[  R\  R   (   R   R    R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR!   f  s(    

c      	   C   s  | d k rs | d k r1 |  j  d	 c t j O<n% | d k rV |  j  d	 c t j O<n  t |  j  d
 j | |  n4| d k r | |  j  d <|  j  d
 j d k r |  j  d |  j  d
 j j _ q|  j  d |  j  d
 j j _	 n | d k r@| |  j  d <|  j  d
 j d k r t
 |   q|  j  d |  j  d
 j j _ ng | d k r\| |  j  d <nK | d k rx| |  j  d <n/ | d k rt
 d |   n t
 d |   |  j  d	 c t j O<d S(   s   Set an attribute and chyange update flag.

        Be aware that setting any RepsFromTo attribute will set the
        drsuapi.DRSUAPI_DRS_UPDATE_ADDRESS update flag.
        R   Rb  R   Ra  R)  Rc  Rd  Re  RZ  R`   R[  i   R\  R   R]   Ro   s$   Attempt to set readonly attribute %ss   Unknown attribute %sN(   s   schedules   replica_flagss   transport_guids   source_dsa_obj_guids   source_dsa_invocation_ids   consecutive_sync_failuress   last_successs   last_attempt(   s   replica_flags(   s   schedule(   s	   dns_name1(   s	   dns_name2(   s   nc_dnstr(   s   to_be_deleted(   s   version(   R^  R   t   DRSUAPI_DRS_UPDATE_FLAGSt   DRSUAPI_DRS_UPDATE_SCHEDULEt   setattrRq   Ro   R]  R`  R[  Rp   R\  t   DRSUAPI_DRS_UPDATE_ADDRESS(   R   t   itemR[   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   __setattr__  s6      	   c      	   C   s+  | d k r# t  |  j d	 j |  S| d k r= |  j d	 j S| d k r |  j d	 j d k rs |  j d	 j j j S|  j d	 j j j Sn | d k r |  j d	 j d k r t |   q|  j d	 j j j SnE | d k r |  j d S| d k r |  j d S| d k r|  j d St d |   d S(   sz   Overload of RepsFromTo attribute retrieval.

        Allows external code to ignore substructures within the blob
        R   Rb  R   Ra  R)  Rc  Rd  Re  R`   Ro   R[  i   R\  R]   R   RZ  s   Unknown attribute %sN(   s   schedules   replica_flagss   transport_guids   source_dsa_obj_guids   source_dsa_invocation_ids   consecutive_sync_failuress   last_successs   last_attempt(   s   version(   s	   dns_name1(   s	   dns_name2(   s   to_be_deleted(   s   nc_dnstr(   s   update_flags(	   t   getattrR^  Rq   Ro   R]  R`  R[  Rp   R\  (   R   Rj  (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   __getattr__  s,      	c         C   s   |  j  d k S(   Ni    (   RZ  (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR^     s    c         C   s   d |  j  d <d  S(   Ni    RZ  (   R^  (   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR_     s    N(
   R   R	   R@   R   R   R!   Rk  Rm  R^   R_   (    (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyRX   +  s   4		2	%	t   SiteLinkc           B   s)   e  Z d  Z d   Z d   Z d   Z RS(   s0   Class defines a site link found under sites
    c         C   sC   | |  _  d |  _ d |  _ d |  _ d  |  _ d  |  _ g  |  _ d  S(   Ni    (   R   R   R   t   costR   R   t   intervalt	   site_list(   R   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR     s    						c         C   sf  d |  j  j |  j f } | d |  j } | d |  j } | d |  j } | d |  j } |  j d k	 r@| d |  j j	 7} | d |  j j
 7} | d |  j j 7} x t |  j j  D]~ \ } } | d	 | | j f 7} | d
 | | j f 7} | d | } x) |  j j | j D] } | d | } qW| d } q Wn  x |  j D] } | d | } qJW| S(   s,   Debug dump string output of Transport objects
   %s:
	dn=%ss   
	options=%ds   
	system_flags=%ds	   
	cost=%ds   
	interval=%ss   
	schedule.size=%ss   
	schedule.bandwidth=%ss   
	schedule.numberOfSchedules=%ss"   
	schedule.headerArray[%d].type=%ds$   
	schedule.headerArray[%d].offset=%ds    
	schedule.dataArray[%d].slots[ s   0x%X R   s   
	site_list=%sN(   R   R   R   R   R   Ro  Rp  R   R   R   R   R   R   R   R   R   R   R   Rq  (   R   R    R   R   R   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR!     s,    c   
   	   C   s  d d d d d d g } y1 | j  d |  j d t j d	 | d
 d g  } Wn5 t j k
 r \ } } t d |  j | f   n X| d } d | k r t | d d  |  _ n  d | k r t | d d  |  _ n  d | k r t | d d  |  _	 n  d | k r#t | d d  |  _
 n  d | k rxa | d D]R } t | |  } t j | j j d   }	 |	 |  j k r:|  j j |	  q:q:Wn  d | k rt t j |  |  _ n t   |  _ d S(   s   Given a siteLink object with an prior initialization
        for the object's DN, search for the DN and load attributes
        from the samdb.
        R   R   Ro  R   t   replIntervalt   siteListR$   R%   R&   t   controlss   extended_dn:0s'   Unable to find SiteLink for (%s) - (%s)i    R,   N(   R'   R   R(   R)   R*   R+   R   R   R   Ro  Rp  R   R   R,   Rd   t   get_extended_componentRq  RZ   R   R   R   R   (
   R   R/   R&   R0   R1   R2   R3   R[   R   R   (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   load_sitelink  s<    	
(   R   R	   R@   R   R!   Rv  (    (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyRn    s   			t   KCCFailedObjectc           B   s   e  Z d    Z RS(   c         C   s1   | |  _  | |  _ | |  _ | |  _ | |  _ d  S(   N(   R   t   failure_countt   time_first_failuret   last_resultR`  (   R   R   Rx  Ry  Rz  R`  (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR   6  s
    				(   R   R	   R   (    (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyRw  5  s   c         C   sA   x' |  j  j   D] } | j   r | Sq Wt d |  j   d  S(   Ns)   Unable to find config NC replica for (%s)(   R   R   R8   R   RL   (   R   R,  (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR%  C  s
    c         C   s   t  t |  j  t | j   S(   sC   use ndr_pack for GUID comparison, as appears correct in some places(   R$  R   RM   (   t   dsa1t   dsa2(    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR'  M  s    c          C   s}   t  j   }  d |  _ d |  _ d |  _ t  j   } d | _ d | _ | g |  _ t  j	   } d g d | _
 | g |  _ |  S(   s   Create a default schedule for an NTDSConnection or Sitelink. This
    is packed differently from the repltimes schedule used elsewhere
    in KCC (where the 168 nibbles are packed into 84 bytes).
    i   i    i   i   i   (   R   R   R   R   R   t   scheduleHeaderR   R   R   t   scheduleSlotsR   R   (   R   R   t   data(    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyR   R  s    
					(#   R(   R   t   sambaR    t   samba.dcerpcR   R   R   t   samba.commonR   t	   samba.ndrR   R   R+   R   t   objectR
   t   dictR^  R   R   R   RA   R}   R   R  R  RC  RS  RX   Rn  Rw  R%  R'  R   (    (    (    s7   /usr/lib/python2.7/dist-packages/samba/kcc/kcc_utils.pyt   <module>   s4     \  6g@X	
	