a
     {a5                     @   s\  d Z dZddlZddlZddlZddlZddlZddlmZ ddl	m
Z ddlmZ dd Zd	d
 ZG dd deZ
dd Zdd Zdd Zd&ddZdZdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% ZejZejZejZej Z ej!Z!ej"Z"ej#Z#ej$Z$ej"Z"ej%Z%ej&Z&ej'Z'ej(Z(ej)Z)ej*Z*ej+Z+ej,Z,ej-Z-ej.Z.ej/Z/ej0Z0dS )'zSamba 4.ZrestructuredText    N)_glue)Ldb)string_typesc                  C   sZ   ddg} | D ]@}t jt jt jt|}t jt j|dr|  S qtddS )z&Return the top level source directory.z../../..z../../../..Zsource4z)unable to find top level source directoryN)ospathnormpathjoindirname__file__existsRuntimeError)pathsptopdir r   0/usr/lib/python3/dist-packages/samba/__init__.pysource_tree_topdir#   s    
r   c                  C   s$   z
t  } W n ty   Y dS 0 dS )z?Return True if we are running from within the samba source treeFT)r   r   )r   r   r   r   in_source_tree-   s
    
r   c                   @   s`   e Zd ZdZdddZddejfddZdd	 Zd
d Z	dd Z
dd ZdddZdddZdS )r   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.
    Nr   c           
      C   s   |dur|  | n|  tjtj d |dur@| | |durR| | |durd| 	| | 
  dd }|   |dur|d}	|	dur|	r|tjO }| d |dur| ||| dS )ah  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                 S   s   t | d S N)print)ltextr   r   r   msgd   s    zLdb.__init__.<locals>.msgz
ldb:nosynci  )Zset_modules_dirr   r   r   sambaZparammodules_dirZset_session_infoZset_credentialsZset_loadparmZregister_samba_handlersZset_utf8_casefoldgetr   Z
FLG_NOSYNCZset_create_permsZconnect)
selfZurlZlpr   Zsession_infoZcredentialsflagsZoptionsr   Znosync_pr   r   r   __init__?   s&    





zLdb.__init__c                 C   sb   |  ||||g}t|dks.|d | du r2dS t|d | }t|dksRJ | || S )aG  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setZschema_format_valuepop)r   Z	attributebasedn
expressionscoperesvaluesr   r   r   	searchoneu   s    
zLdb.searchonec              
   C   s   z| j |tjg dd}W nJ tjyb } z0|j\}}|tjkrLW Y d}~dS  W Y d}~n
d}~0 0 z|D ]}| |jdg qjW n< tjy } z"|j\}}|tjkr W Y d}~n
d}~0 0 dS )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)))baser'   attrsr&   Nrelax:0)r!   r   SCOPE_SUBTREELdbErrorargsERR_NO_SUCH_OBJECTdeletedn)r   r3   r(   errorerrnoestrr   r   r   r   erase_users_computers   s     




zLdb.erase_users_computersc                 C   s
  d}|  | | j|tjdg ddgdD ]V}z| |jdg W q( tjy| } z"|j\}}|tjkrh W Y d}~q(d}~0 0 q(| j|tjdg ddgd}t	|dksJ d	D ]V}z| |dg W q tjy } z"|j\}}|tjkr W Y d}~qd}~0 0 qdS )
z~Erase 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)controlsr-   Nr   )z@SUBCLASSESz@MODULESz@OPTIONSz
@PARTITIONz
@KLUDGEACL)
r7   r!   r   r.   r2   r3   r/   r0   r1   r"   )r   r%   r   r4   r5   r6   r(   attrr   r   r   erase_except_schema_controlled   s0    







z"Ldb.erase_except_schema_controlledc                 C   sf   |    dD ]T}z| |dg W q tjy^ } z"|j\}}|tjkrJ W Y d}~qd}~0 0 qdS )z%Erase this ldb, removing all records.)z
@INDEXLISTz@ATTRIBUTESr-   N)r;   r2   r   r/   r0   r1   )r   r:   r4   r5   r6   r   r   r   erase   s    

z	Ldb.erasec                 C   s<   t |d}| |  W d   n1 s.0    Y  dS )zHLoad a LDIF file.

        :param ldif_path: Path to LDIF file.
        rN)openadd_ldifread)r   Z	ldif_pathZ	ldif_filer   r   r   load_ldif_file_add   s    zLdb.load_ldif_file_addc                 C   s2   |  |D ]"\}}|tjks J | || q
dS )zJAdd data based on a LDIF string.

        :param ldif: LDIF text.
        N)
parse_ldifr   ZCHANGETYPE_NONEaddr   Zldifr9   Z
changetyper   r   r   r   r?      s    zLdb.add_ldifc                 C   s<   |  |D ],\}}|tjkr*| || q
| || q
dS )zQModify database based on a LDIF string.

        :param ldif: LDIF text.
        N)rB   r   ZCHANGETYPE_ADDrC   ZmodifyrD   r   r   r   modify_ldif   s    
zLdb.modify_ldif)NNNNNr   N)N)N)__name__
__module____qualname____doc__r   r   Z
SCOPE_BASEr*   r7   r;   r<   rA   r?   rE   r   r   r   r   r   6   s     
6
)
	r   c                 C   sV   |  D ]H\}}t|ts&J d| t|ts@J d||f | d| |} q| S )zSubstitute strings of the form ${NAME} in str, replacing
    with substitutions from values.

    :param text: Text in which to subsitute.
    :param values: Dictionary with keys and values.
    z%r is not a stringzValue %r for %s is not a stringz${%s})items
isinstancer   replace)r   r)   namevaluer   r   r   substitute_var   s
    rO   c                 C   s>   d| vrdS |  d}|  d|}td| ||d   dS )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   Z	var_startZvar_endr   r   r   check_all_substituted  s    
rS   c                 C   sT   t | ddd2}| }|dur2t||}t| W d   n1 sF0    Y  |S )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 subsitute in the file.
    r=   zutf-8)encodingN)r>   r@   rO   rS   )	file_name
subst_varsZ	data_filedatar   r   r   read_and_sub_file  s    
&rX   c                 C   sP   t j|rt | t| |}t|d}z|| W |  n
|  0 dS )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   unlinkrX   r>   writeclose)templatefnamerV   rW   fr   r   r   
setup_file$  s    


r`      c                 C   s   |   p| dv S )Nz !#$%&'()-.@^_{}~)isalnum)cr   r   r   is_valid_netbios_char9  s    rd   c                 C   s,   t | tkrdS | D ]}t|s dS qdS )z1Check whether a name is valid as a NetBIOS name. FT)r"   MAX_NETBIOS_NAME_LENrd   )rM   xr   r   r   valid_netbios_name=  s    rg   c                 C   s`   t  r@tjt ||}|tjvr0tjd| t| tj| < ntd|| f |gdtj| < dS )a  Import the bundled version of a package.

    :note: This should only be called if the system version of the package
        is not adequate.

    :param modulename: Module name to import
    :param location: Location to add to sys.path (can be relative to
        ${srcdir}/${source_tree_container})
    :param source_tree_container: Directory under source root that
        contains the bundled third party modules.
    :param namespace: Namespace to import module from, when not in source tree
    r   z%s.%s)fromlistN)	r   r   r   r   r   sysinsert
__import__modules)
modulenamelocationsource_tree_container	namespaceZ
extra_pathr   r   r   import_bundled_packageH  s    
rq   c              	   C   s4   zt |  W n" ty.   t| |ddd Y n0 dS )zAdd a location to sys.path if a third party dependency can't be found.

    :param modulename: Module name to import
    :param location: Location to add to sys.path (can be relative to
        ${srcdir}/third_party)
    Zthird_partyzsamba.third_party)ro   rp   N)rk   ImportErrorrq   )rm   rn   r   r   r   ensure_third_party_modulea  s    rs   c                 C   s   dd | d S )z.return a DN from a DNS name domain/forest rootzDC=z,DC=.)r   split)Z	dnsdomainr   r   r   dn_from_dns_namep  s    rv   c                   C   s   t t S r   )inttimer   r   r   r   current_unix_timeu  s    ry   c                 C   sJ   dgt |  }tt | D ]*}t| | tr4| | n
t| | ||< q|S )Nr   )r"   rangerK   rw   ord)stringZblobir   r   r   string_to_byte_arrayy  s    (r~   c                 C   s   ddl m} ||| S )Nr   )arcfour_crypt_blob)Zsamba.cryptor   )keyrW   r   r   r   r   arcfour_encrypt  s    r   )N)1rI   Z__docformat__r   ri   rx   r   Zsamba.paramr   r   Z
samba._ldbr   Z_LdbZsamba.compatr   r   r   rO   rS   rX   r`   re   rd   rg   rq   rs   rv   ry   r~   r   versionZinterface_ipsZfault_setupZset_debug_levelZget_debug_levelZunix2nttimeZnttime2stringZnttime2unixZgenerate_random_passwordZ generate_random_machine_passwordZcheck_password_qualityZgenerate_random_bytesZstrcasecmp_mZstrstr_mZis_ntvfs_fileserver_builtZis_heimdal_builtZNTSTATUSErrorZHRESULTErrorZWERRORErrorZDsExtendedErrorr   r   r   r   <module>   s^   
	 @
	