
    IdPO                         d dl mZmZmZ d dlmZ d dlmZ d dlm	Z	 d dlm
Z
 d dlmZ d dlZd dlZd dlmZmZmZ d dlZ G d	 d
e      ZddZd Zd Zd Zd Z G d de      Z G d de      Zy)    )drsuapimiscdrsblobs)Net)
ndr_unpack)dsdb)werror)WERRORErrorN)DRSUAPI_ATTID_name(DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8)DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V10c                       e Zd ZdZd Zd Zy)drsExceptionzBase element for drs errorsc                     || _         y Nvalue)selfr   s     1/usr/lib/python3/dist-packages/samba/drs_utils.py__init__zdrsException.__init__%   s	    
    c                      d| j                   z   S )NzdrsException: r   )r   s    r   __str__zdrsException.__str__(   s    $**,,r   N)__name__
__module____qualname____doc__r   r    r   r   r   r   "   s    %-r   r   c                 
   d}|j                         dk\  r|dz  }||d|  z  }d| d| d}n	d| d|d}	 t        j                  |||      }t        |      \  }}|||fS # t        $ r}	t	        d	| d
|	      d}	~	ww xY w)a  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
    :param ip: Forced target server name
    :return: A tuple with the drsuapi bind object, the drsuapi handle
                and the supported extensions.
    :raise drsException: if the connection fails
    seal	   z,printNz,target_hostname=zncacn_ip_tcp:[]zDRS connection to z	 failed: )	log_levelr   
drs_DsBind	Exceptionr   )
serverlpcredsipbinding_optionsbinding_stringdrsuapiBinddrsuapiHandlebindSupportedExtensionses
             r   drsuapi_connectr1   ,   s     O	||~8# 
~.vh77(Ao->a@  39/JLoonb%@3=k3J0/ (?@@  LJKKLs   %A# #	B,A==Bc                    t        j                         }||_        t        j                         }||_        ||_        t        j                  |      |_        	 | j                  |d|       y# t        $ r}t        d|z        d}~ww xY w)a  Send DS replica sync request.

    :param drsuapiBind: a drsuapi Bind object
    :param drsuapi_handle: a drsuapi handle 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
       zDsReplicaSync failed %sN)r   DsReplicaObjectIdentifierdnDsReplicaSyncRequest1naming_contextoptionsr   GUIDsource_dsa_guidDsReplicaSyncr&   r   )r-   drsuapi_handler:   r7   
req_optionncreq1estrs           r   sendDsReplicaSyncrA   L   s     
	*	*	,BBE((*DDDL99_5D=!!.!T: =4t;<<=s   A- -	B	6BB	c                 
   t        j                         }d|_        t        j                         |_        |j                  xj
                  t         j                  z  c_        |j                  xj
                  t         j                  z  c_        |j                  xj
                  t         j                  z  c_        |j                  xj
                  t         j                  z  c_        |j                  xj
                  t         j                  z  c_        |j                  xj
                  t         j                  z  c_        |j                  xj
                  t         j                  z  c_        |j                  xj
                  t         j                  z  c_        |j                  xj
                  t         j                  z  c_        |j                  xj
                  t         j                  z  c_        |j                  xj
                  t         j                   z  c_        |j                  xj
                  t         j"                  z  c_        |j                  xj
                  t         j$                  z  c_        |j                  xj
                  t         j&                  z  c_        |j                  xj
                  t         j(                  z  c_        |j                  xj
                  t         j*                  z  c_        |j                  xj
                  t         j,                  z  c_        |j                  xj
                  t         j.                  z  c_        |j                  xj
                  t         j0                  z  c_        |j                  xj
                  t         j2                  z  c_        |j                  xj
                  t         j4                  z  c_        |j                  xj
                  t         j6                  z  c_        |j                  xj
                  t         j8                  z  c_        |j                  xj
                  t         j:                  z  c_        |j                  xj
                  t         j<                  z  c_        |j                  xj
                  t         j>                  z  c_        |j                  xj
                  t         j@                  z  c_        |j                  xj
                  t         jB                  z  c_        | jE                  tG        jH                  t         jJ                        |      \  }}||j                  j
                  fS )z0make a DsBind call, returning the binding handle   )&r   DsBindInfoCtrlengthDsBindInfo28infosupported_extensions DRSUAPI_SUPPORTED_EXTENSION_BASE-DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION%DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI&DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2+DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS%DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V14DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION'DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE'DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V24DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION%DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V28DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD'DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND)DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO-DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION&DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V011DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP+DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY&DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3,DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2(DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6)DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCSr   *DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5*DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6,DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3*DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7)DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECTDsBindr   r9   DRSUAPI_DS_BIND_GUID)drs	bind_inforG   handles       r   r%   r%   g   sT   %%'II))+INNN''7+S+SS'NN''7+`+``'NN''7+X+XX'NN''7+Y+YY'NN''7+^+^^'NN''7+X+XX'NN''7+g+gg'NN''7+Z+ZZ'NN''7+Z+ZZ'NN''7+g+gg'NN''7+X+XX'NN''7+k+kk'NN''7+Z+ZZ'NN''7+\+\\'NN''7+`+``'NN''7+Y+YY'NN''7+d+dd'NN''7+^+^^'NN''7+Y+YY'NN''7+_+__'NN''7+[+[['NN''7+\+\\'NN''7+[+[['NN''7+]+]]'NN''7+]+]]'NN''7+_+__'NN''7+]+]]'NN''7+\+\\'ZZ		'*F*F GSNT6DII2233r   c                    t        j                         }d|_        g }| j                         }| j	                  |t
        j                  dg d      }|D ]  }t        |d   d         }d|v rJ|d   d   }t        |      t        j                  j                  t        j                  j                  z  z  rbd|v r/|d   d   }t        |      t        j                  j                  z  r| j                  |      }	|j                  t        |	              |j!                          ||_        t%        |      |_        |S )	z-get a list of attributes for RODC replicationr3   zobjectClass=attributeSchema)lDAPDisplayNamesystemFlagssearchFlags)basescope
expressionattrsrj   r   rk   rl   )r   DsPartialAttributeSetversionget_schema_basednsearchldbSCOPE_SUBTREEstrintsambar   DS_FLAG_ATTR_NOT_REPLICATEDDS_FLAG_ATTR_IS_CONSTRUCTEDSEARCH_FLAG_RODC_ATTRIBUTEget_attid_from_lDAPDisplayNameappendsortattidslen
num_attids)
samdbpartial_attribute_setr   	schema_dnresrldap_display_namesystem_flagssearch_flagsattids
             r   "drs_get_rodc_partial_attribute_setr      s;   #99;$%!F
 '')I
,,IS->->"?-  .C
  ""3 4Q 78A !- 0 3LL!UZZ%K%K%*ZZ%K%K&L MA]+A.LL!EJJ$I$II445FGc%j!"  KKM+1 '*6{$  r   c                 j    |j                   | _         |j                  | _        |j                  | _        y)z
    Copies the highwater mark by value, rather than by object reference. (This
    avoids lingering talloc references to old GetNCChanges reply messages).
    N)tmp_highest_usnreserved_usnhighest_usn)hwmnew_hwms     r   drs_copy_highwater_markr      s-    
 "11C++C))COr   c                   b    e Zd ZdZd Zd Zed        Zd Zd Z	de
j                  ddd	dd
fdZy)drs_ReplicatezDRS replication callsc                    t        j                   |||      | _        t        | j                        \  | _        | _        t        ||      | _        || _        t        |t        j                        st        d      |t        j                  d      k(  rt        d      | j                  j                  | j                  || j                  |      | _        d| _        y )N)r)   r(   z"Must supply GUID for invocation_id$00000000-0000-0000-0000-000000000000zGMust not set GUID 00000000-0000-0000-0000-000000000000 as invocation_idr   )r   rf   r%   
drs_handlesupports_extr   netr   
isinstancer   r9   RuntimeErrorreplicate_initreplication_state
more_flags)r   r,   r(   r)   r   invocation_ids         r   r   zdrs_Replicate.__init__   s    ??>2u=/9$((/C,$+Ur*
-3CDDDII&LMMhii!%!8!8RS`!ar   c                     | j                   }|t        j                  k(  xr+ |t        z  xr  |j                  t
        j                  z  dk(  S Nr   )r   r	   WERR_DS_DRA_RECYCLED_TARGETr   r   r   DRSUAPI_DRS_GET_TGT)r   
error_codereqr   s       r   _should_retry_with_get_tgtz(drs_Replicate._should_retry_with_get_tgt   sO    
 ((f@@@ DHHD'"="==!C	Er   c                 l    | t         j                  k(  xr  |j                  t        j                  z  dk7  S r   )r	   WERR_DS_DRA_MISSING_PARENTreplica_flagsr   DRSUAPI_DRS_GET_ANC)r   r   s     r   %_should_calculate_missing_anc_locallyz3drs_Replicate._should_calculate_missing_anc_locally   s7     v@@@ G""W%@%@@QF	Hr   c                 R   t               | _        |j                  }	 |y | j                  j                  t	        |j
                  j                  j                               |j                  |j                  t        j                  d      k7  rt	        |j                        | j                  vrjt        j                  | j                  |j
                  j                  j                        }|j                         }t!        d| d|j                   d       |j"                  })Nr   zObject z with GUID z) was not sent by the server in this chunk)set
guids_seenfirst_objectaddrw   object
identifierguidparent_object_guidr   r9   ru   Dnr   r5   parentprintnext_object)r   ctrobject_to_checkobj_dn	parent_dns        r   _calculate_missing_anc_locallyz,drs_Replicate._calculate_missing_anc_locally   s    %
 **&OOO$:$:$E$E$J$J KL11="55))BCD99:$//Q

O,B,B,M,M,P,PQ"MMO		{ +-@@A BAA B .99O! r   c                 Z    | j                   j                  | j                  |||||       y)5Processes a single chunk of received replication data)schema	req_levelr   N)r   replicate_chunkr   )r   levelr   r   r   r   first_chunks          r   process_chunkzdrs_Replicate.process_chunk   s/     	  !7!7(.) 	! 	Nr   FNTr   c           	      
   | j                   t        z  r+t        j                         }|
| j                  z  |_        d}nd}t        j
                         }||_        ||_        t        j                         |_	        ||j                  _
        d}t        j                         }d|_        d|_        d|_        |sS| j                  j!                  |t"        j$                  dg      }d|d   v rW|d   d   D ]L  }t'        t(        j*                  |      }|j,                  j                  |k(  s7|j,                  j.                  }N t        j0                         }d|_        d|_        d|_        g }t9        j:                  | j                  | j                  j=                               }|D ]I  }t        j>                         }|j                  |_        |j                  |_        |jA                  |       K ||_!        tE        |      |_#        ||_        ||_$        |||_%        n|t        jL                  k(  rd|_%        nt        jN                  t        jP                  z  t        jR                  z  t        jT                  z  t        jV                  z  |_%        |r$|xjJ                  t        jX                  z  c_%        n#|xjJ                  t        jZ                  z  c_%        |	r#|xjJ                  t        j\                  z  c_%        d|_/        d	|_0        ||_1        d|_2        d|_3        d|_4        d|jj                  _6        d|jj                  _7        |s|rtq        | j                        |_3        | j                   tr        z  sHd
}t        jt                         }tw        |      D ]"  }|d   dk7  sty        ||t{        ||             $ |}d}d}d}	 | j|                  j                  | j                  ||      \  }}|j                  '|j                  dk7  rt        d|j                  z        	 | j                  ||||||       d}||j                  z  }	 ||j                  z  }|j                  dk(  r	 ||fS t        |j.                  |j                         # t        $ r}| j                  |j                  d   |      r6t        d       |xj                  t        j                  z  c_        d}Y d}~)| j                  |j                  d   |      rt        d       | j                  |       |d}~ww xY w# t        $ r Y w xY w)zreplicate a single DN
      Nr   repsFrom)rm   rn   rp   r3   i  i"    _Tz6DsGetNCChanges: NULL first_object with object_count=%uz1Missing target object - retrying with DRS_GET_TGTz;Missing parent object - calculating missing objects locallyF)Qr   r   r   DsGetNCChangesRequest10r   DsGetNCChangesRequest8destination_dsa_guidsource_dsa_invocation_idr4   r7   r5   DsReplicaHighWaterMarkr   r   r   r   rt   ru   
SCOPE_BASEr   r   repsFromToBlobr   highwatermarkDsReplicaCursorCtrExrr   	reserved1	reserved2r   _dsdb_load_udv_v2get_default_basednDsReplicaCursorr~   cursorsr   countuptodateness_vectorr   DRSUAPI_EXOP_REPL_SECRETDRSUAPI_DRS_INIT_SYNCDRSUAPI_DRS_PER_SYNCr   DRSUAPI_DRS_NEVER_SYNCED$DRSUAPI_DRS_GET_ALL_GROUP_MEMBERSHIP%DRSUAPI_DRS_SPECIAL_SECRET_PROCESSINGDRSUAPI_DRS_WRIT_REPDRSUAPI_DRS_SYNC_FORCEDmax_object_countmax_ndr_sizeextended_op	fsmo_infor   partial_attribute_set_exmapping_ctrnum_mappingsmappingsr   r   DsGetNCChangesRequest5dirsetattrgetattrrf   DsGetNCChangesr   r   object_countr   r   r
   r   argsr   r   r   r   linked_attributes_countAttributeError	more_datar   new_highwatermark)r   r5   r   r   r   exoprodcr   	full_syncsync_forcedr   r   r   udvr   r   reps_from_packedreps_from_obj
cursors_v1
cursors_v2	cursor_v2	cursor_v1req5anum_objects	num_linksr   r   r   r0   s                                 r   	replicatezdrs_Replicate.replicate  s    HH113C(4??:CNII002C#7 '?$$>>@ " ,,.**##3>>+5, $ 8CSV#(+Az(: >$$.x/F/FHX$YM$((AAE]]+//==>
 ..0CCKCMCMJ//

04

0M0M0OQJ' -	#335	5>5W5W	2(1(=(=	%!!),	- %CKJCI"%$ -CW555 !C!(!>!>!(!=!=">!(!<!<"= ")!A!A"B ")!M!M	"NC
 !!BBD! !!W%A%AA!!@!@@"!$(!'+$'($#' $(J4::(VC%  #KKI113DY 6Q43;D!WS!_56 C	88224??IsSLUC'C,<,<,A"#[_b_o_o#pqq""5#vy#{S(  K3+++KS888	 }}!
 Y'' $C$5$5s7L7LMO   22166!9cBMNNNg&A&AAN #'K==affQi>ACWX77<#2 " s1   2R U 	T>'AT9<=T99T>	UU)r   r   r   r   r   r   staticmethodr   r   r   r   DRSUAPI_EXOP_NONEr  r   r   r   r   r      sP    
	E H H:6N W%>%>U $%TUK(r   r   c                   >     e Zd ZdZ fdZd Zd Zd Z fdZ xZ	S )drs_ReplicateRenamerz,Uses DRS replication to rename the entire DBc                 z    t         t        |   |||||       || _        || _        t
        j                  | _        y r   )superr  r   old_base_dnnew_base_dnr   r   r   )	r   r,   r(   r)   r   r   r  r  	__class__s	           r   r   zdrs_ReplicateRenamer.__init__  s>    "D2>2u38-	I&& "55r   c                 ^    t        j                  d| j                  z  | j                  |      S )z/Uses string substitution to replace the base DNz%s$)resubr  r  )r   dn_strs     r   	rename_dnzdrs_ReplicateRenamer.rename_dn  s&    vved...0@0@&IIr   c                 D   |j                   j                  D ]  }|j                  t        k(  st	        j
                  | j                  |j                  j                        }|j                         }|j                  d      |j                  j                  d   _         y)z3Updates the 'name' attribute for the base DN objectz	utf-16-ler   N)attribute_ctr
attributesr   r   ru   r   r   r   r5   get_rdn_valueencode	value_ctrvaluesblob)r   base_objattrbase_dnnew_names        r   update_name_attrz%drs_ReplicateRenamer.update_name_attr  sz    **55 	MDzz//&&X-@-@-C-CD"002080L%%a(-		Mr   c                 L   |j                   j                  }| j                  |j                   j                        |j                   _        t        d|d|j                   j                         |j                   j                  | j                  k(  r| j                  |       yy)z1Renames the first/top-level object in a partitionzRenaming partition z --> N)r   r5   r  r   r  r#  )r   	first_objold_dns      r   rename_top_level_objectz,drs_ReplicateRenamer.rename_top_level_object  s    %%(("&..1E1E1H1H"I	090D0D0G0GI 	J
 ""d&6&66!!), 7r   c                 "   |j                   r4| j                  |j                   j                        |j                   _        |r4|j                  dk7  r%| j	                  |j
                  j                         t        t        | '  ||||||       y)r   r   N)
r7   r  r5   r   r'  r   r   r  r  r   )r   r   r   r   r   r   r   r  s          r   r   z"drs_ReplicateRenamer.process_chunk  s~    
 $(NN33E3E3H3H$IC! 3++q0(()9)9)@)@A 	"D7sF8A38C	Er   )
r   r   r   r   r   r  r#  r'  r   __classcell__)r  s   @r   r  r    s(    66JM
-E Er   r  r   )samba.dcerpcr   r   r   	samba.netr   	samba.ndrr   ry   r   r	   r
   ru   samba.dcerpc.drsuapir   r   r   r  r&   r   r1   rA   r%   r   r   r   r   r  r   r   r   <module>r.     sy   ( 1 0        
M M 
-9 -A@=6#4L#!L*Q(F Q(l8E= 8Er   