
    [kh                    6   S r SSKrSSKrSSKrSSKrSSKrSSKrSSKJrJr  SSK	J
r
Jr  SSKJrJr  SSKJr  SSKJrJr  SSKJr  SS	KJr  SS
KJr  SSKJrJr  SSKrSrS r " S S\5      r " S S\5      r " S S\R<                  5      r " S S\ 5      r!g)z(Convenience functions for using the SAM.    N)dsdbdsdb_dns)
ndr_unpackndr_pack)drsblobsmisc)normalise_int32)	get_bytescmp)security)is_ad_dc_built)string_is_guid)NTSTATUSErrorntstatusrestructuredTextc                      g)Ntdb r       -/usr/lib/python3/dist-packages/samba/samdb.pyget_default_backend_storer   -   s    r   c                       \ rS rSrSrg)
SamDBError0   r   N__name__
__module____qualname____firstlineno____static_attributes__r   r   r   r   r   0       r   r   c                       \ rS rSrSrg)SamDBNotFoundError3   r   Nr   r   r   r   r#   r#   3   r!   r   r#   c            
       B  ^  \ rS rSrSr0 r " S S5      rSSSSS\R                  SSSS4
U 4S jjr	SbU 4S jjr
S	 r\rS
 rS rS rS rS rS r  ScS jrS rS r   SdS jrS rS r   SeS jrS r  SfS jr         SgS jr               ShS jr   SiS jrS r SjS jr SkS jr!S r"S  r#\$" \#\"S!S"9r%S# r&\$" \&S$S"9r'S% r(S& r)\$" \)\(S'S"9r*S( r+ SkS) jr,S* r-S+ r.S, r/S- r0S. r1S/ r2S0 r3S1 r4S2 r5S3 r6S4 r7S5 r8S6 r9S7 r:S8 r;S9 r<SlS: jr=SlS; jr>S< r?S= r@S> rAS? rBS@ rCSA rD SkSB jrESC rFSD rGSE rHSF rISG rJSH rKSI rLSJ rMSK rNSL rOSM rPSN rQSO rRSP rSSmSQ jrTU 4SR jrUSS rVST rWSnSU jrXSV rYSW rZSX r[ SnSY jr\SZ r]S[ r^S\ r_S] r`S^ raS_ rbS` rcSardU =re$ )oSamDB6   zThe SAM database.c                   &    \ rS rSrS rS rS rSrg)SamDB._CleanUpOnError;   c                     Xl         X l        g N)samdbdn)selfr-   r.   s      r   __init__SamDB._CleanUpOnError.__init__<   s    JGr   c                     g r,   r   r/   s    r   	__enter__SamDB._CleanUpOnError.__enter__@   s    r   c                 T    Ub%  U R                   R                  U R                  5        g)NF)r-   deleter.   )r/   exc_typeexc_valexc_tbs       r   __exit__SamDB._CleanUpOnError.__exit__C   s%    # 

!!$''* r   )r.   r-   N)r   r   r   r   r0   r4   r;   r    r   r   r   _CleanUpOnErrorr)   ;   s    		
	r   r=   NTc           
         > X l         U	(       d  S nOUc  Ub  UR                  5       nXl        [        TU ]  XUXEUUS9  U(       a  [
        R                  " U 5        U
b  [
        R                  " X
5        g g )N)urllpmodules_dirsession_infocredentialsflagsoptions)r@   	samdb_urlr?   superr0   r   _dsdb_set_global_schema_dsdb_set_am_rodc)r/   r?   r@   rA   rB   rC   rD   rE   global_schemaauto_connectam_rodc	__class__s              r   r0   SamDB.__init__O   sv     C[R^,,.CS[&2SX!( 	 	* ((.""41 r   c                    > U R                   b?  [        R                  R                  U5      (       d  U R                   R	                  U5      nXl        [        TU ]  XUS9  g)zconnect to the databaseN)r?   rD   rE   )r@   ospathexistsprivate_pathr?   rG   connect)r/   r?   rD   rE   rM   s       r   rT   SamDB.connecte   sH    77rww~~c':':''&&s+CCg>r   c                 ~    U R                   (       a  S[        U 5      S SU R                    S3$ S[        U 5      S S3$ )Nz<SamDB xz (z)>z (no connection)>)r?   idr3   s    r   __repr__SamDB.__repr__m   s>    88RXaL488*B77D!$566r   c                 .    [         R                  " U 5      $ )zreturn True if we are an RODC)r   _am_rodcr3   s    r   rL   SamDB.am_rodcu   s    }}T""r   c                 .    [         R                  " U 5      $ )z%return True if we are an PDC emulator)r   _am_pdcr3   s    r   am_pdcSamDB.am_pdcy   s    ||D!!r   c                 4    [        U R                  5       5      $ )zreturn the domain DN)strget_default_basednr3   s    r   	domain_dnSamDB.domain_dn}   s    4**,--r   c                 4    [        U R                  5       5      $ )zreturn the schema partition dn)rc   get_schema_basednr3   s    r   	schema_dnSamDB.schema_dn   s    4))+,,r   c                 X    [         R                  R                  nU R                  XSS9  g)zeDisables an account

:param search_filter: LDAP filter to find the user (eg
    sAMAccountName=name)
TonN)sambar   UF_ACCOUNTDISABLEtoggle_userAccountFlagsr/   search_filterrD   s      r   disable_accountSamDB.disable_account   s&     

,,$$]d$Cr   c                     [         R                  R                  [         R                  R                  -  nU R	                  XSS9  g)zdEnables an account

:param search_filter: LDAP filter to find the user (eg
    sAMAccountName=name)
Frl   N)rn   r   ro   UF_PASSWD_NOTREQDrp   rq   s      r   enable_accountSamDB.enable_account   s5     

,,uzz/K/KK$$]e$Dr   c                    U R                  U R                  5       [        R                  US/S9n[	        U5      S:X  a  [        SU-  5      e[	        U5      S:X  d   eUS   R                  n[        US   S   S   5      nU(       a%  U(       a  X-  (       a  SU-  n	[        U	5      eX-  n
O%U(       a  X-  (       d  SU-  n	[        U	5      eX) -  n
X:X  a  gS	XxU
4-  nU R                  U5        g)
a7  Toggle_userAccountFlags

:param search_filter: LDAP filter to find the user (eg
    sAMAccountName=name)
:param flags: samba.dsdb.UF_* flags
:param on: on=True (default) => set, on=False => unset
:param strict: strict=False (default) ignore if no action is needed
         strict=True raises an Exception if...
userAccountControlbasescope
expressionattrsr   z!Unable to find account where '%s'   z Account flag(s) '%s' already setz"Account flag(s) '%s' already unsetNz|
dn: %s
changetype: modify
delete: userAccountControl
userAccountControl: %u
add: userAccountControl
userAccountControl: %u
)	searchre   ldbSCOPE_SUBTREElen	Exceptionr.   intmodify_ldif)r/   rr   rD   	flags_strrm   strictres
account_dnold_uacerrornew_uacmods               r   rp   SamDB.toggle_userAccountFlags   s     kkt~~/s7H7H%2;O:P  Rs8q= Cm STT3x1}}VYY
c!f121567?:YF&&oGw<yH&&&G G$% 	r   c                    U R                  U R                  5       [        R                  U/ S9n[	        U5      S:X  a  [        SU-  5      e[	        U5      S:X  d   eUS   R                  nSU-  nU R                  U5        g)zxForces a password change at next login

:param search_filter: LDAP filter to find the user (eg
    sAMAccountName=name)
r{   r   Unable to find user "%s"r   z=
dn: %s
changetype: modify
replace: pwdLastSet
pwdLastSet: 0
N)r   re   r   r   r   r   r.   r   r/   rr   r   user_dnr   s        r   #force_password_change_at_next_login)SamDB.force_password_change_at_next_login   s     kkt~~/s7H7H%2"  >s8q= :] JKK3x1}}a&))
  	r   c                    U R                  U R                  5       [        R                  U/ S9n[	        U5      S:X  a  [        SU-  5      e[	        U5      S:w  a  [        SU-  5      eUS   R                  nSU-  nU R                  U5        g)zUnlock a user account by resetting lockoutTime to 0.
This does also reset the badPwdCount to 0.

:param search_filter: LDAP filter to find the user (e.g.
    sAMAccountName=username)
r{   r   r   r   zUser "%s" is not uniquez?
dn: %s
changetype: modify
replace: lockoutTime
lockoutTime: 0
N)	r   re   r   r   r   r#   r   r.   r   r   s        r   unlock_accountSamDB.unlock_account   s     kkt~~/ # 1 1%2 "  $ s8q=$%?-%OPPs8q=6FGGa&))
  	r   c
                    U(       a  SU< SU< SU R                  5       < 3n
O6SU< SU R                  U R                  5       [        R                  5      < 3n
U
USS.nUb  [        U5      US'   Ub  XKS'   Ub  X[S'   Ub  XkS	'   Ub  [        U5      US
'   U	b  XS'   XS'   Ub  [        U5      US'   U R                  U5        g)a  Adds a new group with additional parameters

:param groupname: Name of the new group
:param grouptype: Type of the new group
:param description: Description of the new group
:param mailaddress: Email address of the new group
:param notes: Notes of the new group
:param gidnumber: GID Number of the new group
:param nisdomain: NIS Domain Name of the new group
:param sd: security descriptor of the object
CN=,groupr.   sAMAccountNameobjectClassN	groupTypedescriptionmailinfo	gidNumbermsSFU30NamemsSFU30NisDomainnTSecurityDescriptor)re   get_wellknown_dnrd   r   DS_GUID_USERS_CONTAINERr	   r   add)r/   	groupnamegroupou	grouptyper   mailaddressnotessd	gidnumber	nisdomaingroup_dn
ldbmessages               r   newgroupSamDB.newgroup   s     (17DNN<LMH  &/0E0E(,(?(?(A(,(D(D1F GH %(1%,.
  &5i&@J{#"(3}%"!,v!&v &5i&@J{# (1}%-6)*>19"J-.r   c                    S[         R                  " U5      < SS< SU R                  5       < S3nU R                  5          U R	                  U R                  5       [         R
                  U/ S9n[        U5      S:X  a  [        SU-  5      e[        U5      S	:X  d   eU R                  US   R                  5        U R                  5         g
!   U R                  5         e = f)z<Deletes a group

:param groupname: Name of the target group
(&(sAMAccountName=)(objectCategory=#CN=Group,CN=Schema,CN=Configurationr   ))r{   r   Unable to find group "%s"r   Nr   binary_encodere   transaction_startr   r   r   r   r7   r.   transaction_committransaction_cancel)r/   r   groupfiltertargetgroups       r   deletegroupSamDB.deletegroup(  s     ILHYHYZcHd  gL  NR  N\  N\  N^  _ 	&++4>>+;3CTCT1<B & HK;1$ ;i GHH{#q()(KKA))*
 ##%		##%   A:C C!c           	         Sn/ SQnSU;   a  UnU H+  nXT;  d  M
  [        SU< SSR                  U5      < S35      e   SU;   a,  US	[        R                  " U5      [        R
                  4-  -  nS
U;   a  US[        R                  " U5      -  -  nSU;   a<  UnUS   S:w  a  SU-  nUS[        R                  [        R                  " U5      4-  -  nSU;   a<  UnUS   S:w  a  SU-  nUS[        R                  [        R                  " U5      4-  -  nSU;   a  US[        R                  " U5      -  -  nSU-  nU$ )N )userr   computerserviceaccountcontactallzInvalid group member type "z". Valid types are z, z	 and all.r   z)(&(sAMAccountName=%s)(samAccountType=%d))r   zQ(&(sAMAccountName=%s)(objectClass=group)(!(groupType:1.2.840.113556.1.4.803:=1)))r   $%s$zW(&(samAccountType=%d)(!(objectCategory=msDS-ManagedServiceAccount))(sAMAccountName=%s))r   zT(&(samAccountType=%d)(objectCategory=msDS-ManagedServiceAccount)(sAMAccountName=%s))r   z3(&(objectCategory=Person)(!(objectSid=*))(name=%s))z(|%s))r   joinr   r   r   ATYPE_NORMAL_ACCOUNTATYPE_WORKSTATION_TRUST)r/   membermember_typesfilterall_member_typesmember_typesamaccountnames          r   group_member_filterSamDB.group_member_filter=  s    L +L'K2!,dii8H.I!K L L ( \!B))&143L3LMN OFl" C ((01 2F %#NbzS !& . 44)).9;; <F
 |+#NbzS !& . 44)).9;; <F
 $L((01 2F 6!r   c                 	   Uc  / SQnUc  U R                  5       nSnSn [        R                  " U5      nUb  SnSnUc,  [	        U5      (       a   [
        R                  " U5      nUb  SnUc  Sn [        U[        R                  5      (       a&  [        R                  " XR                  S5      5      nO![        R                  " U [        U5      5      n Ub}  UR!                  S5      nUR!                  S5      nUb  [#        [        R                  U5      nSnO;Ub  [#        [
        R                  U5      nSnO[        U5      n[        R$                  nUb  SU-  n[        R$                  nUb  SU-  n[        R$                  nUc9  U R                  5       n[        R&                  nS	[        R(                  " U5      -  nS
U< SS< SU R                  5       < S3nU R+                  5          U R-                  WWUS/S/S9n[/        U5      S:X  a  [1        SU-  5      e[/        U5      S:X  d   eSnUb  SU-  nO!Ub  SU-  nO[        US   R2                  5      nSU-  nU GH  nSnSn [        R                  " U5      nS[        U5      -  nUc`   [        R                  " U U5      nUR5                  5       UR                  S5      :X  a  U R7                  U5      nOUnUR                  S5      nUc  U R9                  UU5      nU R-                  U[        R&                  U/ S9n[/        U5      S:  a5  SnU H  nUSUR;                  S5      -  -  nM     [1        SU< SU< 35      e[/        U5      S:w  a  [1        SU-  5      eUS   R2                  R                  S5      nS nU" U US   UU5      n USL a  U (       d  SnUS[        U5      -  -  nGMw  USL d  GM  U (       d  GM  SnUS [        U5      -  -  nGM     USL a  U R=                  U5        U R?                  5         g! [         a     GNf = f! [         a/  n	U	R                  u  pU
[        R                  :w  a  U	e Sn	A	GNSn	A	ff = f! [         a     GNf = f! [         a     GNf = f! [         a  n	 Sn	A	GNSn	A	ff = f!   U RA                  5         e = f)!ag  Adds or removes group members

:param group: sAMAccountName, DN, SID or GUID of the target group
:param members: list of group members
:param add_members_operation: Defines if its an add or remove
    operation
:param member_types: List of object types, used to filter the search
    for the specified members
:param member_base_dn: Base dn for member search
N)r   r   r   z(objectClass=*)r   SIDGUIDz<SID=%s>z	<GUID=%s>z(sAMAccountName=%s)z(&z(objectCategory=r   r   r   zextended_dn:1:1r   )r|   r}   r~   controlsr   r   r   Fz
dn: %s
changetype: modify
r{   r   z%s
r.   zFound multiple results for "z":
z)Unable to find "%s". Operation cancelled.c                 .   UR                  S5      c  gUR                  S5       Hm  n[        R                  " U [        U5      5      nUR	                  S5      nU(       a!  [        [        R                  U5      nX7:X  a    gU[        U5      :X  d  Mm    g   g)Nr   Fr   T)getr   Dnrc   get_extended_componentr   r   dom_sid)r-   r   	member_dn
member_sidmm_ext_dnm_binary_sidm_sids           r   
_is_member2SamDB.add_remove_group_members.<locals>._is_member	  s}    yy*2$"YYx0#&66%Q#8'/'F'Fu'M'$.x/?/?$NE)2'+$H5#' 1 !r   Tzadd: member
member: %s
zdelete: member
member: %s
)!re   r   r   
ValueErrorr   r   r   r   argsr   NT_STATUS_INVALID_PARAMETER
isinstancer   r   extended_strrc   r   r   
SCOPE_BASEr   r   r   r   r   r   r.   get_linearizednormalize_dn_in_domainr   r   r   r   r   )!r/   r   membersadd_members_operationr   member_base_dnpartial_groupfilter	group_sid
group_guidestatus_r   group_b_sidgroup_b_guidsearch_basesearch_scoper   r   modifiedtargetgroup_dnaddtargettogroupr   targetmember_dn	membersidr   full_member_dnrr   targetmembertargetmemberlist_strmsgr   	is_members!                                    r   add_remove_group_membersSamDB.add_remove_group_membersr  s&    8L!!^^-N" 		 ((/I  "3
&>%+@+@!YYu-

 %&7#&HeSVV,,"vvd,>,>q,ABH"vvdCJ7H #&==eD'>>vF* *8+;+;[ II*;'!-!+DII|!DJ*;'"%h-K#&>>L $y0K>>L!%
2K>>L&..*K,,L"7!!%(#*  1NN
 	 d	&++;,81<0A/B-5J	 & 8K
 ;1$ ;e CDD{#q()(H$!+i!7'!,z!9!$[^%6%6!7   
 ""& 	 ( 0 0 8I&03y>&AO #*$'FF4$8	$3359O9OPQ9RR-1-H-H-SN-6N*8*E*Ea*H #*$($<$<V\$RM#';;N585F5F:G57 $/ $9L
 <(1,/1,#/C0FSWWT]4JJ0 $0')/1E)G H H<(A-'(SV\(\]]&21o&8&8&E&Ea&HO!  't'21~'6'02	 )D0#H$ )
?) $ +e3		#H$ )
?) $C "J 4  !12 ##%}  		 ! ffXAAAG B  B "  & n	##%s   P0 Q 2AQ= 7 Q= 2A>R7 1$RR7 AR8C*R7 'R7 1.R7 0
P>=P>
Q:$Q55Q:=
R
R
RR7 RR7 
R4)R7 /R44R7 7S
c                 R   Uc  gX2;   a  [        U5      [        X#   5      :X  a  g[        U5      S:X  a=  X2;   a7  [        R                  " / [        R                  U5      nUR                  U5        g[        R                  " U[        R                  U5      nUR                  U5        g)a!  Changes the MessageElement with the given attr_name of the
given Message. If the value is "" set an empty value and the flag
FLAG_MOD_DELETE, otherwise set the new value and FLAG_MOD_REPLACE.
If the value is None or the Message contains the attr_name with this
value, nothing will changed.Nr   )rc   r   r   MessageElementFLAG_MOD_DELETEr   FLAG_MOD_REPLACE)r/   r  old	attr_namevalueels         r   prepare_attr_replaceSamDB.prepare_attr_replace2  s     =E
c#..A A u:?''C,?,?K s';';YGr   c                 \   Uc  0 nUUUS.nUR                  5        H   u  pxUb  M
  Xt;   d  M  [        XG   5      Xg'   M"     US   (       a&  US   R                  S5      (       d  US==   S-  ss'   [        [	        SUR                  5       5      5      n	SR                  U	5      n
U
S:X  a  U$ U
$ )zPrepares new combined fullname, using the name parts.
Used for things like displayName or cn.
Use the original name values, if no new one is specified.N)	givenNameinitialssnr  . r   )itemsrc   endswithlistr   valuesr   )r/   
given_namer  surname	old_attrsfallback_defaultr   r  
attr_valueattrs_valuesfullnames              r   fullname_from_namesSamDB.fullname_from_namesI  s    
 I(% 
 &+[[]!I!i&<#&y';#<  &3 U:%6%?%?%D%D*$ F489 88L)r>##r   c                    U R                  UUUS9nUn Uc  US:w  a  Un U(       a  SU < SU< SU R                  5       < 3n!O6SU < SU R                  U R                  5       [        R
                  5      < 3n![        R                  " X R                  5       5      R                  5       R                  SS5      n"U< SU"< 3n#U!UU#SS	.n$U(       a/  [        [        R                  [        R                  -  5      U$S
'   SnUb  UU$S'   Ub  UU$S'   US:w  a
  UU$S'   UU$S'   Ub  SU-  U$S'   U	b  U	U$S'   U
b  U
U$S'   Ub  UU$S'   Ub  UU$S'   Ub  UU$S'   Ub  UU$S'   Ub  UU$S'   Ub  UU$S'   Ub  UU$S'   Ub  UU$S'   Ub  UU$S'   Ub  UU$S'   Ub  [        U5      U$S'   Sn%[        [        S UUUUUUU45      5      (       Ga  [        R                   " 5       n%[        R                  " U U!5      U%l        Ub2  [        R$                  " [        U5      [        R&                  S 5      U%S '   Ub2  [        R$                  " [        U5      [        R&                  S!5      U%S!'   Ub2  [        R$                  " [        U5      [        R&                  S"5      U%S"'   Ub2  [        R$                  " [        U5      [        R&                  S#5      U%S#'   Ub2  [        R$                  " [        U5      [        R&                  S$5      U%S$'   Ub2  [        R$                  " [        U5      [        R&                  S%5      U%S%'   Ub  [        R$                  " [        U5      [        R&                  S&5      U%S&'   [        R$                  " [        U5      [        R&                  S'5      U%S''   [        R$                  " S([        R&                  S)5      U%S)'   U R)                  5          U R+                  U$5        U R-                  U U!5         U%(       a  U R/                  U%5        U(       a*  U R1                  S*[        R2                  " U!5      -  UU5        SSS5        U R5                  5         g! , (       d  f       N= f!   U R7                  5         e = f)+al  Adds a new user with additional parameters

:param username: Name of the new user
:param password: Password for the new user
:param force_password_change_at_next_login_req: Force password change
:param useusernameascn: Use username as cn rather that firstname +
    initials + lastname
:param userou: Object container (without domainDN postfix) for new user
:param surname: Surname of the new user
:param givenname: First name of the new user
:param initials: Initials of the new user
:param profilepath: Profile path of the new user
:param scriptpath: Logon script path of the new user
:param homedrive: Home drive of the new user
:param homedirectory: Home directory of the new user
:param jobtitle: Job title of the new user
:param department: Department of the new user
:param company: Company of the new user
:param description: of the new user
:param mailaddress: Email address of the new user
:param internetaddress: Home page of the new user
:param telephonenumber: Phone number of the new user
:param physicaldeliveryoffice: Office location of the new user
:param sd: security descriptor of the object
:param setpassword: optionally disable password reset
:param uidnumber: RFC2307 Unix numeric UID of the new user
:param gidnumber: RFC2307 Unix primary GID of the new user
:param gecos: RFC2307 Unix GECOS field of the new user
:param loginshell: RFC2307 Unix login shell of the new user
:param uid: RFC2307 Unix username of the new user
:param nisdomain: RFC2307 Unix NIS domain of the new user
:param unixhome: RFC2307 Unix home directory of the new user
:param smartcard_required: set the UF_SMARTCARD_REQUIRED bit of the new user
r#  r  r$  Nr   r   r   /@r   )r.   r   userPrincipalNamer   rz   Fr  r  displayNamename%s.r  profilePath
scriptPath	homeDrivehomeDirectorytitle
departmentcompanyr   r   wWWHomePagetelephoneNumberphysicalDeliveryOfficeNamer   c                 
    U S L$ r,   r   )bs    r   <lambda>SamDB.newuser.<locals>.<lambda>  s    Qd]r   uid	uidNumberr   gecos
loginShellunixHomeDirectoryr   r   zABCD!efgh12345$67890unixUserPassword(distinguishedName=%s))r*  re   r   rd   r   r   r   r   canonical_strreplacerc   UF_NORMAL_ACCOUNTUF_SMARTCARD_REQUIREDr   anymapMessager.   r  r  r   r   r=   modifysetpasswordr   r   r   )&r/   usernamepassword'force_password_change_at_next_login_requseusernameascnuserour$  	givennamer  profilepath
scriptpath	homedrivehomedirectoryjobtitler9  r:  r   r   internetaddresstelephonenumberphysicaldeliveryofficer   rQ  	uidnumberr   rD  
loginshellrB  r   unixhomesmartcard_requireddisplaynamecnr   	dnsdomainuser_principal_namer   ldbmessage2s&                                         r   newuserSamDB.newuserj  s   Z ..)8@7> / @ "{b'8B')64>>3CDG  %'(=(=(,(?(?(A(,(D(D)F GG FF4!12@@BJJ3PRS	)19= $(0+>%+-

 /243I3I373M3M4N 0OJ+,K&Jt &/J{#"(3J}%!,Jv%*X%5Jz""(3J}%!'1J|$ &/J{#$*7J'"*Jw!'1J|$$+Jy!"(3J}%"!,Jv&(7J}%&,;J()!-7MJ34>19"J-.s*S)Y-7H-N O P P++-K VVD'2KN%(%7%7C#BVBVX]%^E"$+.+=+=c)ncNbNbdo+pK($+.+=+=c)ncNbNbdo+pK( '*'9'9#e*cFZFZ\c'dG$%,/,>,>s:PSPdPdfr,sL)#363E3EM3#7#79L4N/0$252D2D	NC$8$8:L3N./-0-?-?M3#7#7.HM*252D2D*C,@,@&3(./ 	 	&HHZ %%dG4KK, $$&>&)&7&7&@'A%-%LN 5 ##% 54	##%s%   #Q) 5A
Q?Q) 
Q&"Q) )Q<c                    U R                  UUUS9nUb  UnUc  US:X  a  [        S5      eU R                  5       nU(       a  U R                  U5      nSU< SU< 3nUSS.nUb  UUS'   Ub  UUS	'   Ub  UUS
'   Ub  SU-  US'   Ub  UUS'   Ub  UUS'   U	b  U	US'   U
b  U
US'   Ub  UUS'   Ub  UUS'   Ub  UUS'   Ub  UUS'   Ub  UUS'   U R	                  U5        U$ )a3  Adds a new contact with additional parameters

:param fullcontactname: Optional full name of the new contact
:param ou: Object container for new contact
:param surname: Surname of the new contact
:param givenname: First name of the new contact
:param initials: Initials of the new contact
:param displayname: displayName of the new contact
:param jobtitle: Job title of the new contact
:param department: Department of the new contact
:param company: Company of the new contact
:param description: Description of the new contact
:param mailaddress: Email address of the new contact
:param internetaddress: Home page of the new contact
:param telephonenumber: Phone number of the new contact
:param mobilenumber: Primary mobile number of the new contact
:param physicaldeliveryoffice: Office location of the new contact
r-  r   zNo name for contact specifiedr   r   r   r.   r   r  r  r1  r3  r  r8  r9  r:  r   r   r;  r<  mobiler=  )r*  r   re   r   r   )r/   fullcontactnameour$  rW  r  rd  r\  r9  r:  r   r   r]  r^  mobilenumberr_  re  contactcontainer_dn
contact_dnr   s                       r   
newcontactSamDB.newcontact  sk   H %%/7.5 & 7 & B"rRx;<<"nn."&"="=b"A#%':;
&%.
 &Jt &/J{#"(3J}%%*X%5Jz""*Jw!'1J|$$+Jy!"(3J}%"!,Jv&(7J}%&,;J()##/Jx !-7MJ34	r   c                    [         R                  " SSU5      nUR                  S5      (       a  [        SU-  5      eSU-  nU R	                  U R                  5       [        R                  5      n	U(       a  U R                  U5      n	SU< SU	< 3n
U
USS	.nUb  X;S'   U(       a  XkS'   [        [        R                  [        R                  -  5      nU(       a  [        [        R                  5      nXS'   U(       a"  SR                  XpR                  5       5      US'   U R                  5          U R                  U5        U(       aT  UR!                  5       nU R#                  X
5         U R%                  S[&        R(                  " U
5      -  US5        S
S
S
5        U R+                  5         g
! , (       d  f       N= f!   U R-                  5         e = f)a  Adds a new user with additional parameters

:param computername: Name of the new computer
:param computerou: Object container for new computer
:param description: Description of the new computer
:param prepare_oldjoin: Preset computer password for oldjoin mechanism
:param ip_address_list: ip address list for DNS A or AAAA record
:param service_principal_name_list: string list of servicePincipalName
z\$$r   r   zIllegal computername "%s"r   r   r   r   r   Nr   servicePrincipalNamerz   z{}.{}dNSHostNamerH  F)resubcountr   r   rd   r   DS_GUID_COMPUTERS_CONTAINERr   rc   UF_WORKSTATION_TRUST_ACCOUNTro   formatdomain_dns_namer   r   lowerr=   rQ  r   r   r   r   )r/   computername
computerour   prepare_oldjoinip_address_listservice_principal_name_listre  r   computercontainer_dncomputer_dnr   accountcontrolrS  s                 r   newcomputerSamDB.newcomputer|  s    VVFB-88C==7,FGG#44T5L5L5N.2.N.N P#'#>#>z#J $&(<='(6%/

 "(3}%&1L-.T>>!334 5 !B!BCN+9'((/((*),J}% 	 	&HHZ 88:))$<$$&>&)&7&7&D'E%-u6 = ##% =<	##%s$   29G  ++F/G  /
F=9G   Gc                    S[         R                  " U5      < SS< SU R                  5       < S3nU R                  5          U R	                  U R                  5       [         R
                  U/ S9n[        U5      S:X  a  [        SU-  5      e[        U5      S	:X  d   eU R                  US   R                  5        U R                  5         g
!   U R                  5         e = f)z9Deletes a user

:param username: Name of the target user
r   r   z$CN=Person,CN=Schema,CN=Configurationr   r   r{   r   r   r   Nr   )r/   rR  r   targets       r   
deleteuserSamDB.deleteuser  s     DGCTCTU]C^  aG  IM  IW  IW  IY  Z 	&[[dnn&6c>O>O,2" ! >F6{a :X EFFv;!#$#KKq	%
 ##%		##%r   c                    U R                  5          U R                  U R                  5       [        R                  U/ S9n[        U5      S:X  a  [        SU=(       d    U-  5      e[        U5      S:  a  [        S[        U5      U4-  5      eUS   R                  n[        U[        5      (       d  UR                  S5      nOUnSU-   S-   R                  S5      nS	U< S
[        R                  " U5      R                  S5      < S3nU R                  U5        U(       a   U R                  S[        U5      -   S-   5        U R!                  U5        U R#                  5         g!   U R%                  5         e = f)zSets the password for a user

:param search_filter: LDAP filter to find the user (eg
    sAMAccountName=name)
:param password: Password for the user
:param force_change_at_next_login: Force password change
r{   r   r   r   z*Matched %u multiple users with filter "%s"utf-8"z	utf-16-lez
dn: z5
changetype: modify
replace: unicodePwd
unicodePwd:: 
z(distinguishedName=)N)r   r   re   r   r   r   r   r.   r   rc   decodeencodebase64	b64encoder   r   rw   r   r   )	r/   rr   rS  force_change_at_next_loginrR  r   r   pwsetpws	            r   rQ  SamDB.setpassword  sY    	  	&++4>>#33;L;L)6b  BC3x1} :h>W- XYY3x!| LPSTWPXZgOh hii!fiiGh,,__W-(S.((5B   $++G46E U#)88)CL83>@ .
 ##%		##%s   EE% %E8c                 6   U R                  5          U R                  U R                  5       [        R                  USS/S9n[        U5      S:X  a  [        SU-  5      e[        U5      S:X  d   eUS   R                  n[        US   S   S   5      nU(       a  US-  nSnO:US-  n[        R                  " U[        [        R                  " 5       5      -   5      nS	XVU4-  nU R                  U5        U R                  5         g
!   U R                  5         e = f)zSets the account expiry for a user

:param search_filter: LDAP filter to find the user (eg
    sAMAccountName=name)
:param expiry_seconds: expiry time from now in seconds
:param no_expiry_req: if set, then don't expire password
rz   accountExpiresr{   r   r   r   i   izy
dn: %s
changetype: modify
replace: userAccountControl
userAccountControl: %u
replace: accountExpires
accountExpires: %u
N)r   r   re   r   r   r   r   r.   r   rn   unix2nttimetimer   r   r   )	r/   rr   expiry_secondsno_expiry_reqr   r   rz   r  setexps	            r   	setexpirySamDB.setexpiry  s#    	 	&++4>>#33;L;L)6%9;K$L  NC 3x1} :] JKKs8q=!=!fiiG!$SV,@%A!%D!E%7'%A"!"%7(%B"!&!2!2>C		DT3T!U N34F V$
 ##%		##%s   C"D Dc                 0    [         R                  " X5        g)zPChange the domain SID used by this LDB.

:param sid: The new domain sid to use.
N)r   _samdb_set_domain_sid)r/   sids     r   set_domain_sidSamDB.set_domain_sid%  s    
 	""4-r   c                 .    [         R                  " U 5      $ )z&Read the domain SID used by this LDB. )r   _samdb_get_domain_sidr3   s    r   get_domain_sidSamDB.get_domain_sid,  s    ))$//r   zSID for the domain)docc                     U R                  S[        R                  S/S9S   n[        [	        [
        R                  US   S   5      5      $ )z&Returns the SID of the connected user.r   tokenGroupsr|   r}   r   r   )r   r   r   rc   r   r   r   )r/   r  s     r   get_connecting_user_sidSamDB.get_connecting_user_sid3  sD    kkrkOPQR:h..M0B10EFGGr   zSID of the connecting userc                 0    [         R                  " X5        g)z_Set the invocation id for this SamDB handle.

:param invocation_id: GUID of the invocation id.
N)r   _dsdb_set_ntds_invocation_id)r/   invocation_ids     r   set_invocation_idSamDB.set_invocation_id;  s    
 	))$>r   c                 .    [         R                  " U 5      $ )zGet the invocation_id id)r   _samdb_ntds_invocation_idr3   s    r   get_invocation_idSamDB.get_invocation_idB  s    --d33r   zInvocation ID GUIDc                 .    [         R                  " X5      $ r,   )r   _dsdb_get_oid_from_attidr/   attids     r   get_oid_from_attidSamDB.get_oid_from_attidI  s    ,,T99r   c                 0    [         R                  " U X5      $ )zNreturn the attribute ID for a LDAP attribute as an integer as found in DRSUAPI)r   $_dsdb_get_attid_from_lDAPDisplayName)r/   ldap_display_nameis_schema_ncs      r   get_attid_from_lDAPDisplayName$SamDB.get_attid_from_lDAPDisplayNameL  s     889JZ 	Zr   c                 .    [         R                  " X5      $ )z6return the syntax OID for a LDAP attribute as a string)r   )_dsdb_get_syntax_oid_from_lDAPDisplayNamer/   r  s     r   #get_syntax_oid_from_lDAPDisplayName)SamDB.get_syntax_oid_from_lDAPDisplayNameR  s    ==dVVr   c                 .    [         R                  " X5      $ )z8return the systemFlags for a LDAP attribute as a integer)r   *_dsdb_get_systemFlags_from_lDAPDisplayNamer  s     r   $get_systemFlags_from_lDAPDisplayName*SamDB.get_systemFlags_from_lDAPDisplayNameV  s    >>tWWr   c                 .    [         R                  " X5      $ )z3return the linkID for a LDAP attribute as a integer)r   %_dsdb_get_linkId_from_lDAPDisplayNamer  s     r   get_linkId_from_lDAPDisplayName%SamDB.get_linkId_from_lDAPDisplayNameZ  s    99$RRr   c                 .    [         R                  " X5      $ )z;return the lDAPDisplayName from an integer DRS attribute ID)r   "_dsdb_get_lDAPDisplayName_by_attidr  s     r   get_lDAPDisplayName_by_attid"SamDB.get_lDAPDisplayName_by_attid^  s    66tCCr   c                 .    [         R                  " X5      $ )zreturn the attribute name of the corresponding backlink from the name
of a forward link attribute. If there is no backlink return None)r   '_dsdb_get_backlink_from_lDAPDisplayNamer  s     r   !get_backlink_from_lDAPDisplayName'SamDB.get_backlink_from_lDAPDisplayNameb  s     ;;DTTr   c                 0    [         R                  " X5        g)zSet the NTDS Settings DN, as would be returned on the dsServiceName
rootDSE attribute.

This allows the DN to be set before the database fully exists

:param ntds_settings_dn: The new DN to use
N)r   _samdb_set_ntds_settings_dn)r/   ntds_settings_dns     r   set_ntds_settings_dnSamDB.set_ntds_settings_dng  s     	((@r   c                 .    [         R                  " U 5      $ )zGet the NTDS objectGUID)r   _samdb_ntds_objectGUIDr3   s    r   get_ntds_GUIDSamDB.get_ntds_GUIDq  s    **400r   c                 j    U R                  S[        R                  S/S9n[        US   S   S   5      $ )z/Get the current time as generalized time stringr   currentTimer  r   r   r   r   rc   r/   r   s     r   get_timestrSamDB.get_timestru  s>    kkr #!.  1 3q6-(+,,r   c                 J    [         R                  " U R                  5       5      $ )z!Get the current time as UNIX time)r   string_to_timer  r3   s    r   get_timeSamDB.get_time|  s    !!$"2"2"455r   c                 J    [         R                  " U R                  5       5      $ )zGet the current time as NT time)rn   r  r  r3   s    r   
get_nttimeSamDB.get_nttime  s      11r   c                 .    [         R                  " U 5      $ )zGet the server site name)r   _samdb_server_site_namer3   s    r   server_site_nameSamDB.server_site_name  s    ++D11r   c                 j    U R                  S[        R                  S/S9n[        US   S   S   5      $ )z return the DNS name of this hostr   rw  r  r   r  r  s     r   host_dns_nameSamDB.host_dns_name  s5    kkrkO3q6-(+,,r   c                 f    U R                  5       nUR                  5       R                  S5      S   $ )z&return the DNS name of the domain rootr.  r   )rd   rI  split)r/   re   s     r   r~  SamDB.domain_dns_name  s/    ++-	&&(..s3A66r   c                    U R                  5       nU R                  5       nSU< SU< S3nU R                  5       nU R                  U[        R
                  US9n US   S   S   R                  5       nU$ ! [         a     gf = f)z*return the NetBIOS name of the domain rootz/(&(objectClass=crossRef)(nETBIOSName=*)(ncName=z
)(dnsroot=r   )r}   r~   r   nETBIOSNameN)rd   r~  get_partitions_dnr   r   SCOPE_ONELEVELr  
IndexError)r/   re   dns_namer   partitions_dnr   netbios_domains          r   domain_netbios_nameSamDB.domain_netbios_name  s    ++-	'')V_aij..0kk- # 2 2%+  -	 VM215<<>N   		s   A7 7
BBc                 f    U R                  5       nUR                  5       R                  S5      S   $ )z&return the DNS name of the forest rootr.  r   )get_root_basednrI  r  )r/   	forest_dns     r   forest_dns_nameSamDB.forest_dns_name  s/    ((*	&&(..s3A66r   c                 .    [         R                  " X5      $ r,   )r   _dsdb_load_partition_usn)r/   base_dns     r   load_partition_usnSamDB.load_partition_usn  s    ,,T;;r   c                 8    U R                  UR                  US9  g )N)write_indices_and_attributes)set_schema_from_ldbr   )r/   schemar  s      r   
set_schemaSamDB.set_schema  s      Jf gr   c                 2    [         R                  " XU5        g r,   )r   _dsdb_set_schema_from_ldb)r/   ldb_connr  s      r   r  SamDB.set_schema_from_ldb  s    &&t7STr   c                 *    SnU R                  U5        g )Nz@
dn:
changetype: modify
add: schemaUpdateNow
schemaUpdateNow: 1
)r   )r/   ldifs     r   set_schema_update_nowSamDB.set_schema_update_now  s     	r   c                 0    [         R                  " XU5      $ )zBconvert a list of attribute values to a DRSUAPI DsReplicaAttribute)r   _dsdb_DsReplicaAttributer/   r   r  ldif_elementss       r   dsdb_DsReplicaAttributeSamDB.dsdb_DsReplicaAttribute  s    ,,S]SSr   c                 0    [         R                  " XU5      $ )z$normalise a list of attribute values)r   _dsdb_normalise_attributesr!  s       r   dsdb_normalise_attributesSamDB.dsdb_normalise_attributes  s    ..s}UUr   c                     [        U R                  R                  5       5      S:X  a  U R                  5         U R	                  U5      U R                  ;   a  U R                  U R	                  U5         $ g)zGet from an attid the associated attribute

:param attid: The attribute id for searched attribute
:return: The name of the attribute associated with this id
r   N)r   hash_oid_namekeys_populate_oid_attidr  r  s     r   get_attribute_from_attidSamDB.get_attribute_from_attid  sd     t!!&&()Q.$$&""5)T-?-??%%d&=&=e&DEEr   c                     0 U l         U R                  SS/SS/S9n[        U5      S:  aJ  U HC  n[        UR	                  S5      5      nX0R                   [        UR	                  S5      5      '   ME     gg)zwPopulate the hash hash_oid_name.

This hash contains the oid of the attribute as a key and
its display name as a value
zobjectClass=attributeSchemasearch_options:1:2attributeIDlDAPDisplayName)r~   r   r   r   N)r*  r   r   rc   r   )r/   r   r   
strDisplays       r   r,  SamDB._populate_oid_attid  s      kk%B$8#9!.!2!4  5 s8a< '8!9:
@J""3quu]';#<=  r   c                 2   U R                  SU-  [        R                  S/S/S9n[        U5      S:X  a  g[	        [
        R                  US   S   S   5      nUR                  n[        U R                  R                  5       5      S:X  a  U R                  5         UR                   Hl  nU R                  UR                  5      nXpR                  ;   d  M/  UR                  5       U R                  U   R                  5       :X  d  M`  UR                  s  $    g)a+  Get the version field trom the replPropertyMetaData for
the given field

:param dn: The on which we want to get the version
:param att: The name of the attribute
:return: The value of the version field in the replPropertyMetaData
    for the given attribute. None if the attribute is not replicated
distinguishedName=%sr0  replPropertyMetaDatar~   r}   r   r   r   N)r   r   r   r   r   r   replPropertyMetaDataBlobctrr*  r+  r,  arrayr  r  r  version)r/   r.   attr   replr:  oatt_oids           r   "get_attribute_replmetadata_version(SamDB.get_attribute_replmetadata_version  s     kk%;b%@ # 1 1$8#9!7 8  : s8q=(;;a&!78;=hht!!&&()Q.$$&A--agg6G,,,yy{d009??AAyy   r   c                 $   U R                  SU-  [        R                  S/S/S9n[        U5      S:X  a  g [	        [
        R                  US   S   S   5      nUR                  n[        R                  " [        [        R                  " 5       5      5      nSn	[        U R                  R                  5       5      S:X  a  U R                  5         UR                   H  n
U R!                  U
R"                  5      nXR                  ;   d  M/  UR%                  5       U R                  U   R%                  5       :X  d  M`  Sn	U R'                  [        R(                  5      nX:l        Xl        [.        R0                  " U R3                  5       5      U
l        Xl        Xl        M     U	(       d  U(       a  [        UR                  5      S:  a  [
        R:                  " 5       nSUl        U R!                  UR"                  5      nU R'                  [        R(                  5      nX=l        Xl        [.        R0                  " U R3                  5       5      Ul        Xl        Xl        Sn	UR                  nUR=                  U5        UR>                  S	-   Ul        Xl        U	(       aq  [A        U5      n[        RB                  " 5       nUS   RD                  Ul"        [        RF                  " U[        RH                  S5      US'   U RK                  US
/5        g g )Nr6  r0  r7  r8  r   FTiZ 	 r   z#local_oid:1.3.6.1.4.1.7165.4.3.14:0)&r   r   r   r   r   r   r9  r:  rn   r  r   r  r*  r+  r,  r;  r  r  r  sequence_numberSEQ_NEXTr<  originating_change_timer   r   r  originating_invocation_idoriginating_usn	local_usnreplPropertyMetaData1appendrz  r   rO  r.   r  r  rP  )r/   r.   r=  r  addifnotexistr   r>  r:  nowfoundr?  r@  seqo2tabreplBlobr  s                    r   "set_attribute_replmetadata_version(SamDB.set_attribute_replmetadata_version  sf   kk%;b%@ # 1 1$8#9!7 8  : s8q=(;;a&!78;=hhDIIK 01t!!&&()Q.$$&A--agg6G,,,yy{d009??AA**3<<8!	,/).2ii8N8N8P.Q+$'!!  3syy>A+=//1BBH--bhh7G&&s||4CJ),&+/99T5K5K5M+NB(!$LE))CJJrN		ACII~H++-CVYYCF""8#&#7#7#9; &' KKCDE r   c                 0    [         R                  " U 5        g r,   )r   '_dsdb_write_prefixes_from_schema_to_ldbr3   s    r   write_prefixes_from_schema SamDB.write_prefixes_from_schema2  s    44T:r   c                 .    [         R                  " U 5      $ r,   )r   _dsdb_get_partitions_dnr3   s    r   r  SamDB.get_partitions_dn5  s    ++D11r   c                 .    [         R                  " X5      $ r,   )r   _dsdb_get_nc_root)r/   r.   s     r   get_nc_rootSamDB.get_nc_root8  s    %%d//r   c                 0    [         R                  " XU5      $ r,   )r   _dsdb_get_wellknown_dn)r/   nc_rootwkguids      r   r   SamDB.get_wellknown_dn;  s    **4&AAr   c                 R   [        U[        5      (       d  [        U5      R                  S5      n[        R
                  " 5       n[        R                  " X R                  5       5      Ul        [        R                  " U[        R                  S5      US'   U R                  U5        g )Nutf8	minPwdAger   bytesrc   r  r   rO  r   re   r.   r  r  rP  r/   r  r   s      r   set_minPwdAgeSamDB.set_minPwdAge>  n    %''J%%f-EKKMvvdNN,-++E33G3GU+Ar   c                     U R                  U R                  5       [        R                  S/S9n[	        U5      S:X  a  g SUS   ;  a  g [        US   S   S   5      $ )Nrg  r}   r   r   r   re   r   r   r   r   r  s     r   get_minPwdAgeSamDB.get_minPwdAgeF  Y    kk$..*#..kVs8q=A&s1vk*1-..r   c                 R   [        U[        5      (       d  [        U5      R                  S5      n[        R
                  " 5       n[        R                  " X R                  5       5      Ul        [        R                  " U[        R                  S5      US'   U R                  U5        g )Nrf  	maxPwdAgerh  rj  s      r   set_maxPwdAgeSamDB.set_maxPwdAgeO  rm  r   c                     U R                  U R                  5       [        R                  S/S9n[	        U5      S:X  a  g SUS   ;  a  g [        US   S   S   5      $ )Nru  ro  r   rp  r  s     r   get_maxPwdAgeSamDB.get_maxPwdAgeW  rs  r   c                 R   [        U[        5      (       d  [        U5      R                  S5      n[        R
                  " 5       n[        R                  " X R                  5       5      Ul        [        R                  " U[        R                  S5      US'   U R                  U5        g )Nrf  minPwdLengthrh  rj  s      r   set_minPwdLengthSamDB.set_minPwdLength`  so    %''J%%f-EKKMvvdNN,-..uc6J6JN[.Ar   c                     U R                  U R                  5       [        R                  S/S9n[	        U5      S:X  a  g SUS   ;  a  g [        US   S   S   5      $ )Nr|  ro  r   rp  r  s     r   get_minPwdLengthSamDB.get_minPwdLengthh  sZ    kk$..*#..HXkYs8q=3q6)s1vn-a011r   c                 R   [        U[        5      (       d  [        U5      R                  S5      n[        R
                  " 5       n[        R                  " X R                  5       5      Ul        [        R                  " U[        R                  S5      US'   U R                  U5        g )Nrf  pwdPropertiesrh  rj  s      r   set_pwdPropertiesSamDB.set_pwdPropertiesq  so    %''J%%f-EKKMvvdNN,- //s7K7K_]/Ar   c                     U R                  U R                  5       [        R                  S/S9n[	        U5      S:X  a  g SUS   ;  a  g [        US   S   S   5      $ )Nr  ro  r   rp  r  s     r   get_pwdPropertiesSamDB.get_pwdPropertiesy  sZ    kk$..*#..HYkZs8q=CF*s1vo.q122r   c                 r   [         R                  " 5       n[         R                  " U SU R                  5       R	                  5       -  5      Ul        Ub*  [         R                  " U[         R                  S5      US'   O)[         R                  " / [         R                  S5      US'   U R                  U5        g )N1CN=Directory Service,CN=Windows NT,CN=Services,%sdSHeuristics)
r   rO  r   get_config_basednr   r.   r  r  r  rP  )r/   dsheuristicsr   s      r   set_dsheuristicsSamDB.set_dsheuristics  s    KKMvvdO..0??AB C#""<#&#7#7#13 n ""2s':':#13 n 	Ar   c                     U R                  SU R                  5       R                  5       -  [        R                  S/S9n[        U5      S:X  a  S nU$ SUS   ;   a  US   S   S   nU$ S nU$ )Nr  r  ro  r   )r   r  r   r   r   r   )r/   r   r  s      r   get_dsheuristicsSamDB.get_dsheuristics  s    kkM 224CCEF #~6F  H s8q=L  s1v%q6.1!4L   Lr   c                     USS.nU(       a  X%S'   U(       a  X5S'   U(       a  [        U5      US'   U R                  U5        g)zCreates an organizationalUnit object
:param ou_dn: dn of the new object
:param description: description attribute
:param name: name attribute
:param sd: security descriptor of the object, can be
an SDDL string or security.descriptor type
organizationalUnitrl  r   r2  r   N)r   r   )r/   ou_dnr   r2  r   r   s         r   	create_ouSamDB.create_ou  sE     02 *mfI(0A$%r   c                    > U R                  5          [        TU ]	  U5      nU R                  5         U$ !   U R	                  5         e = f)zsReturns the value of the sequence number according to the requested type
:param seq_type: type of sequence number
 )r   rG   rD  r   r   )r/   seq_typerO  rM   s      r   rD  SamDB.sequence_number  sN     	 	&')(3C
 ##%
	##%s	   4 Ac                 j    U R                  S[        R                  S/S9n[        US   S   S   5      $ )z get the NTDS DN from the rootDSEr   dsServiceNamer  r   r  r  s     r   get_dsServiceNameSamDB.get_dsServiceName  s6    kkr?PkQ3q6/*1-..r   c                 j    U R                  S[        R                  S/S9n[        US   S   S   5      $ )z"get the server DN from the rootDSEr   
serverNamer  r   r  r  s     r   get_serverNameSamDB.get_serverName  s5    kkr~kN3q6,'*++r   c                 ^    Uc  [         R                  " X5      $ [         R                  " XUS9$ )zDDo a DNS lookup in the database, returns the NDR database structures)dns_partition)r   lookup)r/   r  r  s      r   
dns_lookupSamDB.dns_lookup  s/     ??422??41>@ @r   c                 .    [         R                  " X5      $ )z;Return the NDR database structures from a dnsRecord element)r   extract)r/   r  s     r   dns_extractSamDB.dns_extract  s    ))r   c                 0    [         R                  " XU5      $ )zVDo a DNS modification on the database, sets the NDR database
structures on a DNS name
)r   rJ  )r/   r  new_recordss      r   dns_replaceSamDB.dns_replace  s     <<r   c                 0    [         R                  " XU5      $ )zDo a DNS modification on the database, sets the NDR database
structures on a LDB DN

This routine is important because if the last record on the DN
is removed, this routine will put a tombstone in the record.
)r   replace_by_dn)r/   r.   r  s      r   dns_replace_by_dnSamDB.dns_replace_by_dn  s     %%d<<r   c                     [        5       (       d  [        S5      eUc  [        R                  " XU5      $ [        R                  " XUU5      $ )z{garbage_collect_tombstones(lp, samdb, [dn], current_time, tombstone_lifetime)
-> (num_objects_expunged, num_links_expunged)z6Cannot garbage collect tombstones: AD DC was not built)r   r   r    _dsdb_garbage_collect_tombstones)r/   r.   current_timetombstone_lifetimes       r   garbage_collect_tombstones SamDB.garbage_collect_tombstones  s`    
  & ' ' %889EG G 889E9KM Mr   c                 .    [         R                  " U 5      $ )zcreate a RID set for this DSA)r   _dsdb_create_own_rid_setr3   s    r   create_own_rid_setSamDB.create_own_rid_set  s    ,,T22r   c                 .    [         R                  " U 5      $ )z.return a new RID from the RID Pool on this DSA)r   _dsdb_allocate_ridr3   s    r   allocate_ridSamDB.allocate_rid  s    &&t,,r   c                 *    U R                  5       u  pU$ )a  return the next free RID from the RID Pool on this DSA.

:note: This function is not intended for general use, and care must be
    taken if it is used to generate objectSIDs. The returned RID is not
    formally reserved for use, creating the possibility of duplicate
    objectSIDs.
)free_rid_bounds)r/   ridr   s      r   next_free_ridSamDB.next_free_rid  s     %%'
r   c                 "   [         R                  " X R                  5       5      nU R                  U[         R                  S/S9n US   S   n[         R                  " XS   R                  S5      5      nU R                  U[         R                  S/S9n US   S   n[         R                  " XS   R                  S5      5      nU R                  U[         R                  / S	QS9nS
nSn [        US   S   S   5      n	X:X  a(  [         R                  " [         R                  SU-  5      e [        US   S   S   5      n
 [        US   S   S   5      nX:X  d  X:X  a  U	n
X-  nOUS-  nX-  nU
S-	  nX:  a7  X:X  a%  [         R                  " [         R                  S5      eU	n
X-  nU
S-	  nUnX:  d  X:  a*  [         R                  " [         R                  SXU4-  5      eX4$ ! [
         a*    [         R                  " [         R                  SU-  5      Sef = f! [
         a*    [         R                  " [         R                  SU-  5      Sef = f! [
         a    Un	 GNff = f! [
         a    Un
 GN7f = f! [
         a    Un GN5f = f)aa  return the low and high bounds (inclusive) of RIDs that are
    available for use in this DSA's current RID pool.

:note: This function is not intended for general use, and care must be
    taken if it is used to generate objectSIDs. The returned range of
    RIDs is not formally reserved for use, creating the possibility of
    duplicate objectSIDs.
serverReferencer  r   zUNo RID Set DN - Cannot find attribute serverReference of %s to calculate reference dnNr  rIDSetReferenceszVNo RID Set DN - Cannot find attribute rIDSetReferences of %s to calculate reference dn)rIDAllocationPoolrIDPreviousAllocationPool
rIDNextRIDl    l    r  zBad RID Set %sr  r  r       zRID pools out of RIDsz"Bad RID chosen %d from range %d-%d)r   r   r  r   r   KeyErrorLdbErrorERR_NO_SUCH_ATTRIBUTEr  r   ERR_OPERATIONS_ERROR)r/   server_name_dnr   
server_refserver_ref_dnrid_set_refs
rid_set_dn
uint32_max
uint64_max
alloc_pool	prev_poolnext_ridprev_pool_loprev_pool_his                 r   r  SamDB.free_rid_bounds	  s    &9&9&;<kk~ #!2 3  5	HQ 12J t]%9%9'%BCkk} #!3 4  6	Gq6"45L VVDq/"8"8"AB
 kkz #!/  0 

	$SV$78;<J #,,s77/*<> >	#CF#>?BCI	"3q6,/23H
 "h&<"I -HMH !- B" &ll3#;#;#:< < #I$1L$?L#H"h&=,,s77C (E FG G %%Y  	H,,)),.<=> DH	H	H  	G,,)),.;<= CG	G	G(  	$#J	$  	#"I	#  	"!H	"sN   G* H! -I /I+ I> *4H!4II('I(+I;:I;>JJc                    [         R                  " X R                  5       5      n[        U[         R                  5      (       a  [	        U5      n[         R                  " X5      nUR                  U5      (       d  UR                  U5        U$ )zreturn a new DN expanded by adding the domain DN

If the dn is already a child of the domain DN, just
return it as-is.

:param dn: relative dn
)r   r   re   r   rc   is_child_ofadd_base)r/   r.   re   full_dns       r   r   SamDB.normalize_dn_in_domaing  se     FF4!12	b#&&!!RB&&"""9--Y'r   c                 :    [         R                  " U /UQ70 UD6nU$ )r   )r   _dsdb_create_gkdi_root_key)r/   r   kwargsr.   s       r   new_gkdi_root_keySamDB.new_gkdi_root_keyy  s!    ,,TCDCFC	r   c                     U R                  SS[        R                  S/S9nU R                  SUS   S   S   5      R	                  S5      $ )Nr   r  )r|   r~   r}   r   r   rf  )r   r   r   schema_format_valuer  r  s     r   get_admin_sidSamDB.get_admin_sid~  sW    kk#..  Q ''3q6-0355;VF^	Dr   )r*  r@   r?   )Nr   N)NTF)NNNNNNNN)TNN)NNNNr   )FFNNNNNNNNNNNNNNNNNTNNNNNNNF)NNNNNNNNNNNNNNN)NNFNN)FN)F)T)NNNr,   )fr   r   r   r   __doc__r*  r=   r   FLG_DONT_CREATE_DBr0   rT   rY   __str__rL   r`   re   ri   rs   rw   rp   r   r   r   r   r   r  r  r*  ri  rs  r  r  rQ  r  r  r  property
domain_sidr  connecting_user_sidr  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r~  r  r
  r  r  r  r  r#  r'  r-  r,  rA  rS  rW  r  r^  r   rk  rq  rv  ry  r}  r  r  r  r  r  r  rD  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r    __classcell__)rM   s   @r   r&   r&   6   s   M (  Dd!)?)?T"D2,?7 G#".-DE GK05+Z*2 ;?DH+/2h&*3l 8<.204~&@. KO=?D 9>LPLPLPDHFJHLDH#(j&Z $(! # "###'#' $*.dL FJ;?04<&|&, @D*&X(&T.0 ..24JH
 ##:'CE?4 .0A!57M: 5:ZWXSDU
A1-622-
7
7
<hUTVK > :?5Fn;20B//23(/
,
@*== 7;M"3-	\&|$
D Dr   r&   c                   ^    \ rS rSrSrSS jrS rS rS rS r	S	 r
S
 rS rS rS rS rSrg)dsdb_Dni  za class for binary DNNc                    Uc^  UR                  S5      (       a  [        R                  nO7UR                  S5      (       a  [        R                  nO[        R                  nU[        R                  [        R                  4;   a  UR                  S5      n[        U5      S:  a  [        SU-  5      eS[        US   5      -   [        US   5      -   nUSU U l	        U R                  S	[        US   5      -   S
 U l
        X%S U l        OX l        SU l	        SU l
        [        R                  " XR                  5      U l        g)zcreate a dsdb_DnNzB:zS::   zInvalid DN %sr   r      r   r   )
startswithr   DSDB_SYNTAX_BINARY_DNDSDB_SYNTAX_STRING_DNDSDB_SYNTAX_OR_NAMEr  r   RuntimeErrorr   prefixbinarydnstringr   r   r.   )r/   r-   r  
syntax_oidcolons
prefix_lens         r   r0   dsdb_Dn.__init__  s   ""4((!77
$$T**!77
!55
$44d6P6PQQ^^C(F6{Q"?X#=>>S^+c&)n<J"1Z0DK++a#fQi.&8<DK$[1DM$MDKDK&&.r   c                 `    U R                   [        U R                  R                  SS95      -   $ )Nr   )mode)r  rc   r.   r   r3   s    r   r  dsdb_Dn.__str__  s(    {{S!5!51!5!=>>>r   c                     U nUnUR                   R                  S5      nUR                   R                  S5      n[        XE5      nUS:w  a  U$ [        UR                  UR                  5      nU$ )z5compare dsdb_Dn values similar to parsed_dn_compare()r   r   )r.   r   r   r   )r/   otherdn1dn2guid1guid2vs          r   __cmp__dsdb_Dn.__cmp__  sc    --f5--f56H

CJJ'r   c                 *    U R                  U5      S:H  $ Nr   r  r/   r
  s     r   __eq__dsdb_Dn.__eq__      ||E"a''r   c                 *    U R                  U5      S:g  $ r  r  r  s     r   __ne__dsdb_Dn.__ne__  r  r   c                 *    U R                  U5      S:  $ r  r  r  s     r   __lt__dsdb_Dn.__lt__      ||E"Q&&r   c                 *    U R                  U5      S:*  $ r  r  r  s     r   __le__dsdb_Dn.__le__  r  r   c                 *    U R                  U5      S:  $ r  r  r  s     r   __gt__dsdb_Dn.__gt__  r  r   c                 *    U R                  U5      S:  $ r  r  r  s     r   __ge__dsdb_Dn.__ge__  r  r   c                 P    U R                   S:X  a  g[        U R                  S5      $ )z6return binary part of a dsdb_Dn as an integer, or Noner   N   )r  r   r   r3   s    r   get_binary_integerdsdb_Dn.get_binary_integer  s"    ;;"4;;##r   c                 B    [         R                  " U R                  5      $ )zreturn binary as a byte string)binascii	unhexlifyr   r3   s    r   r
   dsdb_Dn.get_bytes  s    !!$++..r   )r   r.   r  r  r,   )r   r   r   r   r  r0   r  r  r  r  r  r!  r$  r'  r+  r
   r    r   r   r   r  r    s<    /2?(('('($/r   r  )"r  rn   r   r  r  rP   rx  r   r   	samba.ndrr   r   samba.dcerpcr   r   samba.commonr	   r
   r   r   r   r   r   r   r.  __docformat__r   r   r   r#   Ldbr&   objectr  r   r   r   <module>r7     s   . /  
   	 	   * ' ( ' !     ) "	 		 	MDEII MD`2G/f G/r   