ó
E®Uc           @   s’   d  d l  m Z m Z d  d l m Z d  d l Z d  d l Z d e f d „  ƒ  YZ d „  Z	 d „  Z
 d „  Z d	 „  Z d
 e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   drsuapit   misc(   t   NetNt   drsExceptionc           B   s    e  Z d  Z d „  Z d „  Z RS(   s   Base element for drs errorsc         C   s   | |  _  d  S(   N(   t   value(   t   selfR   (    (    s3   /usr/lib/python2.7/dist-packages/samba/drs_utils.pyt   __init__   s    c         C   s   d |  j  S(   Ns   drsException: (   R   (   R   (    (    s3   /usr/lib/python2.7/dist-packages/samba/drs_utils.pyt   __str__   s    (   t   __name__t
   __module__t   __doc__R   R   (    (    (    s3   /usr/lib/python2.7/dist-packages/samba/drs_utils.pyR      s   	c   	      C   s¢   d } t  | j d ƒ ƒ d k r. | d 7} n  d |  | f } y+ t j | | | ƒ } t | ƒ \ } } Wn) t k
 r” } t d |  | f ƒ ‚ n X| | | f S(   sm  Make a DRSUAPI connection to the server.

    :param server: the name of the server to connect to
    :param lp: a samba line parameter object
    :param creds: credential used for the connection
    :return: A tuple with the drsuapi bind object, the drsuapi handle
                and the supported extensions.
    :raise drsException: if the connection fails
    t   seals	   log leveli   s   ,prints   ncacn_ip_tcp:%s[%s]s   DRS connection to %s failed: %s(   t   intt   getR    t
   drs_DsBindt	   ExceptionR   (	   t   servert   lpt   credst   binding_optionst   binding_stringt   drsuapiBindt   drsuapiHandlet   bindSupportedExtensionst   e(    (    s3   /usr/lib/python2.7/dist-packages/samba/drs_utils.pyt   drsuapi_connect#   s    c         C   s†   t  j ƒ  } | | _ t  j ƒ  } | | _ | | _ t j | ƒ | _ y |  j	 | d | ƒ Wn# t
 k
 r } t d | ƒ ‚ n Xd S(   sÝ  Send DS replica sync request.

    :param drsuapiBind: a drsuapi Bind object
    :param drsuapi_handle: a drsuapi hanle on the drsuapi connection
    :param source_dsa_guid: the guid of the source dsa for the replication
    :param naming_context: the DN of the naming context to replicate
    :param req_options: replication options for the DsReplicaSync call
    :raise drsException: if any error occur while sending and receiving the
        reply for the dsReplicaSync
    i   s   DsReplicaSync failed %sN(   R    t   DsReplicaObjectIdentifiert   dnt   DsReplicaSyncRequest1t   naming_contextt   optionsR   t   GUIDt   source_dsa_guidt   DsReplicaSyncR   R   (   R   t   drsuapi_handleR    R   t
   req_optiont   nct   req1t   estr(    (    s3   /usr/lib/python2.7/dist-packages/samba/drs_utils.pyt   sendDsReplicaSync;   s    			c         C   st   yJ t  j ƒ  } t | ƒ | _ t | ƒ | _ d | _ |  j | d | ƒ Wn# t k
 ro } t d | ƒ ‚ n Xd S(   s—  Send RemoveDSServer request.

    :param drsuapiBind: a drsuapi Bind object
    :param drsuapi_handle: a drsuapi hanle on the drsuapi connection
    :param server_dsa_dn: a DN object of the server's dsa that we want to
        demote
    :param domain: a DN object of the server's domain
    :raise drsException: if any error occur while sending and receiving the
        reply for the DsRemoveDSServer
    i   s   DsRemoveDSServer failed %sN(	   R    t   DsRemoveDSServerRequest1t   strt	   server_dnt	   domain_dnt   committ   DsRemoveDSServerR   R   (   R   R"   t   server_dsa_dnt   domainR%   R&   (    (    s3   /usr/lib/python2.7/dist-packages/samba/drs_utils.pyt   sendRemoveDsServerV   s    	c         C   s¤  t  j ƒ  } d | _ t  j ƒ  | _ | j j t  j O_ | j j t  j O_ | j j t  j O_ | j j t  j	 O_ | j j t  j
 O_ | j j t  j O_ | j j t  j O_ | j j t  j O_ | j j t  j O_ | j j t  j O_ | j j t  j O_ | j j t  j O_ | j j t  j O_ | j j t  j O_ | j j t  j O_ | j j t  j O_ | j j t  j O_ | j j t  j O_ | j j t  j O_ | j j t  j O_ | j j t  j O_ | j j t  j O_ | j j t  j O_ | j j t  j O_ | j j t  j O_ | j j t  j O_ | j j t  j  O_ | j j t  j! O_ |  j" t# j$ t  j% ƒ | ƒ \ } } | | j j f S(   s0   make a DsBind call, returning the binding handlei   (&   R    t   DsBindInfoCtrt   lengtht   DsBindInfo28t   infot   supported_extensionst    DRSUAPI_SUPPORTED_EXTENSION_BASEt-   DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATIONt%   DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPIt&   DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2t+   DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESSt%   DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1t4   DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATIONt'   DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTEt'   DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2t4   DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATIONt%   DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2t8   DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MODt'   DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BINDt)   DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFOt-   DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTIONt&   DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01t1   DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIPt+   DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORYt&   DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3t,   DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2t(   DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6t)   DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCSt(   DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8t*   DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5t*   DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6t,   DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3t*   DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7t)   DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECTt   DsBindR   R   t   DRSUAPI_DS_BIND_GUID(   t   drst	   bind_infoR4   t   handle(    (    s3   /usr/lib/python2.7/dist-packages/samba/drs_utils.pyR   m   sB    	$t   drs_Replicatec           B   s8   e  Z d  Z d „  Z d „  Z e e j e d d „ Z	 RS(   s   DRS replication callsc         C   sÁ   t  j  | | | ƒ |  _ t |  j ƒ \ |  _ |  _ t d | d | ƒ |  _ | |  _ t | t	 j
 ƒ su t d ƒ ‚ n  | t	 j
 d ƒ k r™ t d ƒ ‚ n  |  j j |  j | |  j | ƒ |  _ d  S(   NR   R   s"   Must supply GUID for invocation_ids$   00000000-0000-0000-0000-000000000000sG   Must not set GUID 00000000-0000-0000-0000-000000000000 as invocation_id(   R    RT   R   t
   drs_handleR5   R   t   nett   samdbt
   isinstanceR   R   t   RuntimeErrort   replicate_initt   replication_state(   R   R   R   R   RZ   t   invocation_id(    (    s3   /usr/lib/python2.7/dist-packages/samba/drs_utils.pyR   –   s    	c   
      C   sC  t  j ƒ  } d | _ g  } |  j j ƒ  } |  j j d | d t j d d d d d d	 g ƒ } x½ | D]µ } | d d
 } d | k r» | d d
 } t | ƒ t	 j
 j t	 j
 j B@r» qd q» n  d	 | k rô | d	 d
 } t | ƒ t	 j
 j @rô qd qô n  |  j j | ƒ }	 | j t |	 ƒ ƒ qd W| j ƒ  | | _ t | ƒ | _ | S(   s-   get a list of attributes for RODC replicationi   t   baset   scopet
   expressions   objectClass=attributeSchemat   attrst   lDAPDisplayNamet   systemFlagst   searchFlagsi    (   R    t   DsPartialAttributeSett   versionRZ   t   get_schema_basednt   searcht   ldbt   SCOPE_SUBTREER   t   sambat   dsdbt   DS_FLAG_ATTR_NOT_REPLICATEDt   DS_FLAG_ATTR_IS_CONSTRUCTEDt   SEARCH_FLAG_RODC_ATTRIBUTEt   get_attid_from_lDAPDisplayNamet   appendt   sortt   attidst   lent
   num_attids(
   R   t   partial_attribute_setRu   t	   schema_dnt   rest   rt   ldap_display_namet   system_flagst   search_flagst   attid(    (    s3   /usr/lib/python2.7/dist-packages/samba/drs_utils.pyt"   drs_get_rodc_partial_attribute_set¡   s2    			
	c         C   s  t  j ƒ  } | | _ | | _ t  j ƒ  | _ | | j _ t  j ƒ  | _ d | j _	 d | j _
 d | j _ d | _ | d k	 r | | _ ni | t  j k r¨ d | _ nN t  j t  j Bt  j Bt  j B| _ | rä | j t  j O_ n | j t  j O_ d | _ d | _ | | _ d | _ d | _ d | _ d | j _ d | j _ | rc| rc|  j ƒ  | _ n  |  j  t  j! @r‚d }	 | }
 n[ d }	 t  j" ƒ  } x@ t# | ƒ D]2 } | d d k r¡t$ | | t% | | ƒ ƒ q¡q¡W| }
 x© t& rˆ|  j' j( |  j) |	 |
 ƒ \ } } | j* d k r;| j+ d k r;t, d | j+ ƒ ‚ n  |  j- j. |  j/ | | d | d	 |	 d
 |
 ƒ| j0 d k ryPn  | j1 |
 _ qàWd S(   s   replicate a single DNi    i’  iÄ" i   i   t   _s6   DsGetNCChanges: NULL first_object with object_count=%ut   schemat	   req_levelt   reqN(2   R    t   DsGetNCChangesRequest8t   destination_dsa_guidt   source_dsa_invocation_idR   R   R   t   DsReplicaHighWaterMarkt   highwatermarkt   tmp_highest_usnt   reserved_usnt   highest_usnt   Nonet   uptodateness_vectort   replica_flagst   DRSUAPI_EXOP_REPL_SECRETt   DRSUAPI_DRS_INIT_SYNCt   DRSUAPI_DRS_PER_SYNCt   DRSUAPI_DRS_GET_ANCt   DRSUAPI_DRS_NEVER_SYNCEDt%   DRSUAPI_DRS_SPECIAL_SECRET_PROCESSINGt   DRSUAPI_DRS_WRIT_REPt   max_object_countt   max_ndr_sizet   extended_opt	   fsmo_infoRx   t   partial_attribute_set_ext   mapping_ctrt   num_mappingst   mappingsR€   R5   RL   t   DsGetNCChangesRequest5t   dirt   setattrt   getattrt   TrueRT   t   DsGetNCChangesRX   t   first_objectt   object_countR\   RY   t   replicate_chunkR^   t	   more_datat   new_highwatermark(   R   R   R‡   R†   R‚   t   exopt   rodcR   t   req8Rƒ   R„   t   req5t   at   levelt   ctr(    (    s3   /usr/lib/python2.7/dist-packages/samba/drs_utils.pyt	   replicateÆ   s`    										 	!N(
   R   R	   R
   R   R€   t   FalseR    t   DRSUAPI_EXOP_NONER   R±   (    (    (    s3   /usr/lib/python2.7/dist-packages/samba/drs_utils.pyRW   “   s
   		&(   t   samba.dcerpcR    R   t	   samba.netR   Rm   Rk   R   R   R   R'   R0   R   t   objectRW   (    (    (    s3   /usr/lib/python2.7/dist-packages/samba/drs_utils.pyt   <module>   s   
				&