
    g9                     t   S r 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Jr  SSK	J
r  S rS r " S S	\5      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\R2                  " S5      r\R2                  " S\R6                  S9rSS jrS r\R>                  r\R@                  r \RB                  r!\RD                  r"\RF                  r#\RH                  r$\RJ                  r%\RL                  r&\RN                  r'\RP                  r(\RR                  r)\RT                  r*\RV                  r+\RX                  r,\RZ                  r-\R\                  r.\R^                  r/\R`                  r0\Rb                  r1\Rd                  r2\Rf                  r3\Rh                  r4\Rj                  r5\Rl                  r6\Rn                  r7g)zSamba 4.restructuredText    N)_glue)Ldbc                  z   SS/n U  H  n[         R                  R                  [         R                  R                  [         R                  R	                  [
        5      U5      5      n[         R                  R                  [         R                  R                  US5      5      (       d  M  Us  $    [        S5      e)z&Return the top level source directory.z../../..z../../../..source4z)unable to find top level source directory)ospathnormpathjoindirname__file__existsRuntimeError)pathsptopdirs      0/usr/lib/python3/dist-packages/samba/__init__.pysource_tree_topdirr   "   s{    'E!!"'',,rwwx/H!"LM77>>"'',,vy9::M  B
CC    c                  :     [        5       n g! [         a     gf = f)z?Return True if we are running from within the samba source treeFT)r   r   )r   s    r   in_source_treer   ,   s(    #%   s   
 
c                   v    \ rS rSrSr  SS jrSS\R                  4S jrS r	S r
S rS	 rSS
 jrSS jrSrg)r   5   a  Simple Samba-specific LDB subclass that takes care
of setting up the modules dir, credentials pointers, etc.

Please note that this is intended to be for all Samba LDB files,
not necessarily the Sam database. For Sam-specific helper
functions see samdb.py.
Nc                 2   Ub  U R                  U5        OKU R                  [        R                  R                  [        R
                  R                  5       S5      5        Ub  U R                  U5        Ub  U R                  U5        Ub  U R                  U5        U R                  5         S nU R                  5         Ub.  UR                  S5      n	U	b  U	(       a  U[        R                  -  nU R                  S5        Ub  U R!                  XU5        gg)a  Opens a Samba Ldb file.

:param url: Optional LDB URL to open
:param lp: Optional loadparm object
:param modules_dir: Optional modules directory
:param session_info: Optional session information
:param credentials: Optional credentials, defaults to anonymous.
:param flags: Optional LDB flags
:param options: Additional options (optional)

This is different from a regular Ldb file in that the Samba-specific
modules-dir is used by default and that credentials and session_info
can be passed through (required by some modules).
Nldbc                     [        U5        g N)print)ltexts     r   msgLdb.__init__.<locals>.msgc   s	    $Kr   z
ldb:nosynci  )set_modules_dirr   r	   r   sambaparammodules_dirset_session_infoset_credentialsset_loadparmregister_samba_handlersset_utf8_casefoldgetr   
FLG_NOSYNCset_create_permsconnect)
selfurllpr&   session_infocredentialsflagsoptionsr!   nosync_ps
             r   __init__Ldb.__init__>   s    " "  -  ekk.E.E.G!OP#!!,/"  ->b!
 	$$&	 	  >vvl+H#'e$?LLW- r   c                     U R                  X$X1/5      n[        U5      S:w  d	  US   U   c  g[        US   U   5      n[        U5      S:X  d   eU R                  XR	                  5       5      $ )a  Search for one attribute as a string.

:param basedn: BaseDN for the search.
:param attribute: Name of the attribute
:param expression: Optional search expression.
:param scope: Search scope (defaults to base).
:return: Value of attribute as a string or None if it wasn't found.
   r   N)searchlensetschema_format_valuepop)r0   	attributebasedn
expressionscoperesvaluess          r   	searchoneLdb.searchonet   sn     kk&[As8q=CF9-5SVI&'6{a''	::<@@r   c                     U R                  U[        R                  / SS9n U H   nU R                  UR                  S/5        M"     g! [        R                   a-  nUR                  u  pEU[        R
                  :X  a   SnAge SnAff = f! [        R                   a-  nUR                  u  pEU[        R
                  :w  a  e  SnAgSnAff = f)zErases user and computer objects from our AD.

This is needed since the 'samldb' module denies the deletion of primary
groups. Therefore all groups shouldn't be primary somewhere anymore.
z+(|(objectclass=user)(objectclass=computer)))baserD   attrsrC   Nrelax:0)r<   r   SCOPE_SUBTREELdbErrorargsERR_NO_SUCH_OBJECTdeletedn)r0   rR   rE   errorerrnoestrr!   s          r   erase_users_computersLdb.erase_users_computers   s    		++2S->->b)V  XC	CFFYK0  || 	!JJMU...	 || 	!JJMU... /	s4    A
 &B 
B"BBBC"#C

Cc                 p   SnU R                  U5        U R                  U[        R                  S/ SS/S9 H!  n U R	                  UR
                  S/5        M#     U R                  U[        R                  S/ SS/S9n[        U5      S:X  d   eS	 H  n U R	                  US/5        M     g! [        R                   a.  nUR                  u  pEU[        R                  :w  a  e  SnAM  SnAff = f! [        R                   a.  nUR                  u  pEU[        R                  :w  a  e  SnAM  SnAff = f)
zfErase this ldb.

:note: Removes all records, except those that are controlled by
    Samba4's schema.
 zJ(&(|(objectclass=*)(distinguishedName=*))(!(distinguishedName=@BASEINFO)))zshow_deleted:0zshow_recycled:0)controlsrL   Nr   )z@SUBCLASSESz@MODULESz@OPTIONSz
@PARTITIONz
@KLUDGEACL)
rV   r<   r   rM   rQ   rR   rN   rO   rP   r=   )r0   rB   r!   rS   rT   rU   rE   attrs           r   erase_except_schema_controlled"Ldb.erase_except_schema_controlled   s6     	""6* ;;vs'8'8k!-=?P,Q  SCCFFYK0	S kk&#"3"3f(8:K'L  N 3x1}}=DD9+.= <<  %

C222 3  <<  %

C222 3s/   B.C3.C0#C++C03D5#D00D5c                     U R                  5         S H  n U R                  US/5        M     g! [        R                   a.  nUR                  u  p4U[        R
                  :w  a  e  SnAMY  SnAff = f)z%Erase this ldb, removing all records.)z
@INDEXLISTz@ATTRIBUTESrL   N)r\   rQ   r   rN   rO   rP   )r0   r[   rS   rT   rU   s        r   erase	Ldb.erase   sf    ++- 2DD9+. 2 <<  %

C222 3s   /A1#A,,A1c                     [        US5       nU R                  UR                  5       5        SSS5        g! , (       d  f       g= f)z8Load a LDIF file.

:param ldif_path: Path to LDIF file.
rN)openadd_ldifread)r0   	ldif_path	ldif_files      r   load_ldif_file_addLdb.load_ldif_file_add   s/    
 )S!YMM)..*+ "!!s	    6
Ac                     U R                  U5       H,  u  p4U[        R                  :X  d   eU R                  XB5        M.     g)z:Add data based on a LDIF string.

:param ldif: LDIF text.
N)
parse_ldifr   CHANGETYPE_NONEadd)r0   ldifrZ   
changetyper!   s        r   rd   Ldb.add_ldif   s9    
  $t4OJ!4!4444HHS#  5r   c                    U R                  U5       H  u  p4U[        R                  :X  a  [        R                  nU[        R                  :X  a  U R                  XB5        MP  U[        R                  :X  a  U R                  XB5        Mw  U[        R                  :X  a  UnU R                  XR5        M  U[        R                  :X  a3  US   nUS   nUS   nUSL a  [        S5      eU R                  XhU5        M  [        SX44-  5      e   g)zAModify database based on a LDIF string.

:param ldif: LDIF text.
olddndeleteoldrdnnewdnFz5Invalid ldb.CHANGETYPE_MODRDN with deleteoldrdn=FalsezInvalid ldb.CHANGETYPE_%u: %sN)rk   r   rl   CHANGETYPE_MODIFYCHANGETYPE_ADDrm   modifyCHANGETYPE_DELETErQ   CHANGETYPE_MODRDN
ValueErrorrename)	r0   rn   rZ   ro   r!   deldnrr   rs   rt   s	            r   modify_ldifLdb.modify_ldif   s    
  $t4OJS000 22
S///'s444C*s444E,s444G">2G5($%\]]E(3 !@JCT!TUU'  5r    )NNNNNr   Nr   )__name__
__module____qualname____firstlineno____doc__r8   r   
SCOPE_BASErG   rV   r\   r_   rh   rd   r}   __static_attributes__r   r   r   r   r   5   sK     JN484.l +/4A"6'R,$Vr   r   c                     UR                  5        H^  u  p#[        U[        5      (       d
   SU-  5       e[        U[        5      (       d   SU< SU< S35       eU R                  SU-  U5      n M`     U $ )zSubstitute strings of the form ${NAME} in str, replacing
with substitutions from values.

:param text: Text in which to substitute.
:param values: Dictionary with keys and values.
z%r is not a stringzValue z for z is not a stringz${%s})items
isinstancestrreplace)r    rF   namevalues       r   substitute_varr     sm      $$$A&:T&AA$%%%XESW'XX%||GdNE2 (
 Kr   c                 |    SU ;  a  gU R                  S5      nU R                  SU5      n[        SXUS-    -  5      e)zCheck that all substitution variables in a string have been replaced.

If not, raise an exception.

:param text: The text to search for substitution variables
z${N}z!Not all variables substituted: %sr;   )find	Exception)r    	var_startvar_ends      r   check_all_substitutedr     sM     4		$IiiY'G
77Q;/0 1 1r   c                     [        U SSS9 nUR                  5       nUb  [        X15      n[        U5        SSS5        U$ ! , (       d  f       W$ = f)zRead a file and sub in variables found in it

:param file_name: File to be read (typically from setup directory)
:param subst_vars: Optional variables to substitute in the file.
rb   zutf-8)encodingN)rc   re   r   r   )	file_name
subst_vars	data_filedatas       r   read_and_sub_filer   $  sR     
iw	/9~~!!$3D!$'	 
0
 K 
0	/
 Ks   *A  
Ac                    [         R                  R                  U5      (       a  [         R                  " U5        [	        X5      n[        US5      n UR                  U5        UR                  5         g! UR                  5         f = f)zSetup a file in the private dir.

:param template: Path of the template file.
:param fname: Path of the file to create.
:param subst_vars: Substitution variables.
wN)r   r	   r   unlinkr   rc   writeclose)templatefnamer   r   fs        r   
setup_filer   2  s\     
ww~~e
		%X2DUCA				s   A5 5B   c                 :    U R                  5       =(       d    U S;   $ )Nz !#$%&'()-.@^_{}~)isalnum)cs    r   is_valid_netbios_charr   G  s    IIK31 334r   c                 b    [        U 5      [        :  a  gU  H  n[        U5      (       a  M    g   g)z1Check whether a name is valid as a NetBIOS name. FT)r=   MAX_NETBIOS_NAME_LENr   )r   xs     r   valid_netbios_namer   K  s1     4y''$Q''  r   c                 H    SSR                  U R                  S5      5      -   $ )z.return a DN from a DNS name domain/forest rootzDC=z,DC=.)r   split)	dnsdomains    r   dn_from_dns_namer   V  s    6;;ys3444r   c                  >    [        [        R                  " 5       5      $ r   )inttimer   r   r   current_unix_timer   [  s    tyy{r   c                     SSK Jn  U" X5      $ )Nr   )arcfour_crypt_blob)samba.cryptor   )keyr   r   s      r   arcfour_encryptr   _  s    /d((r   c                 &    SSK Jn  U" U UUUU5      $ )Nr   )!aead_aes_256_cbc_hmac_sha512_blob)r   r   )	plaintextcekkey_saltmac_saltivr   s         r   aead_aes_256_cbc_hmac_sha512r   d  s!    >,
 r   z<[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})r5   c                 p    U(       a  [         R                  U 5      nO[        R                  U 5      nUc  gg)aF  Is the string an ordinary undecorated string GUID?

That is, like 12345678-abcd-1234-FEED-1234567890ab, and not like
variants which have surrounding curly brackets or lack hyphens.

If lower case_only is true, only lowercase hex characters are
accepted. This is tighter than we ever require, but matches what
we usually emit.
FT)GUID_RE	fullmatchGUID_MIXCASE_RE)stringlower_case_onlyms      r   string_is_guidr   w  s2     f%%%f-yr   c                      SSK Jn   SSK Jn  g)zTThis function modifies the samba.net.Net class to contain
an export_keytab() method.r   )net)dckeytabN)r$   r   r   )r   r   s     r   enable_net_export_keytabr     s
     r   r   )F)8r   __docformat__r   r   r   samba.paramr$   rer   
samba._ldbr   _Ldbr   r   r   r   r   r   r   r   r   r   r   r   r   compiler   
IGNORECASEr   r   r   versioninterface_ipsfault_setupset_debug_levelget_debug_levelfloat2nttimenttime2floatnttime2stringnttime2unixunix2nttimegenerate_random_password generate_random_machine_passwordcheck_password_qualitygenerate_random_bytesstrcasecmp_mstrstr_mis_ntvfs_fileserver_builtis_heimdal_builtis_ad_dc_builtis_selftest_enabledis_rust_builtNTSTATUSErrorHRESULTErrorWERRORErrorDsExtendedErrorr   r   r   <module>r      s  * " 	  
  	  "DKV$ KV\ 1"$  55
)
 **BD **BMM
." --##''''!!!!## 99 #(#I#I  55 33 !!>>!;; )) %%// ####!!''r   