
Vc           @   sh  d  Z  d Z d d l Z d d l Z d d l Z d d l Z d   Z d   Z d d l	 Z	 d d l
 m Z d e f d     YZ d	   Z d
   Z d   Z d d  Z d Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d d l 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% d S(   s   Samba 4.t   restructuredTextiNc          C   s   d d g }  x` |  D]X } t  j j t  j j t  j j t  |   } t  j j t  j j | d   r | Sq Wt d   d S(   s&   Return the top level source directory.s   ../../..s   ../../../..t   source4s)   unable to find top level source directoryN(   t   ost   patht   normpatht   joint   dirnamet   __file__t   existst   RuntimeError(   t   pathst   pt   topdir(    (    s2   /usr/lib/python2.7/dist-packages/samba/__init__.pyt   source_tree_topdir   s    -!c          C   s&   y t    }  Wn t k
 r! t SXt S(   s?   Return True if we are running from within the samba source tree(   R   R	   t   Falset   True(   R   (    (    s2   /usr/lib/python2.7/dist-packages/samba/__init__.pyt   in_source_tree)   s
    (   t   LdbR   c           B   s}   e  Z d  Z d
 d
 d
 d
 d
 d d
 d  Z d
 d
 e j d  Z d   Z d   Z	 d   Z
 d   Z d
 d  Z d
 d	  Z RS(   s  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.
    i    c   
      C   s(  | d k	 r |  j |  n% |  j t j j t j j   d   | d k	 r] |  j |  n  | d k	 ry |  j	 |  n  | d k	 r |  j
 |  n  |  j   d   } |  j   | d k	 r | j d d  }	 |	 d k	 r |	 r | t j O} q n  |  j d  | d k	 r$|  j | | |  n  d S(   sh  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).
        t   ldbc         S   s	   | GHd  S(   N(    (   t   lt   text(    (    s2   /usr/lib/python2.7/dist-packages/samba/__init__.pyt   msgd   s    t   nosynci  N(   t   Nonet   set_modules_dirR   R   R   t   sambat   paramt   modules_dirt   set_session_infot   set_credentialst   set_loadparmt   register_samba_handlerst   set_utf8_casefoldt   getR   t
   FLG_NOSYNCt   set_create_permst   connect(
   t   selft   urlt   lpR   t   session_infot   credentialst   flagst   optionsR   t   nosync_p(    (    s2   /usr/lib/python2.7/dist-packages/samba/__init__.pyt   __init__?   s&    %
	
c         C   s   |  j  | | | | g  } t |  d k sA | d | d k rE d St | d |  } t |  d k sq t  |  j | | j    S(   sG  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.
        i   i    N(   t   searcht   lenR   t   sett   AssertionErrort   schema_format_valuet   pop(   R%   t	   attributet   basednt
   expressiont   scopet   rest   values(    (    s2   /usr/lib/python2.7/dist-packages/samba/__init__.pyt	   searchoneu   s    
&c      	   C   s   y+ |  j  d | d t j d g  d d  } Wn2 t j k
 r_ \ } } | t j k rY d S  n Xy+ x$ | D] } |  j | j d g  qj WWn1 t j k
 r \ } } | t j k r   q n Xd S(   s   Erases 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.
        t   baseR7   t   attrsR6   s+   (|(objectclass=user)(objectclass=computer))Ns   relax:0(   R.   R   t   SCOPE_SUBTREEt   LdbErrort   ERR_NO_SUCH_OBJECTt   deletet   dn(   R%   RA   R8   t   errnot   _R   (    (    s2   /usr/lib/python2.7/dist-packages/samba/__init__.pyt   erase_users_computers   s    c         C   sA  d } |  j  |  x} |  j | t j d g  d d d g D]T } y |  j | j d g  Wq; t j k
 r \ } } | t j k r   q q; Xq; W|  j | t j d g  d d d g } t |  d k s t	  xh d d	 d
 d d g D]Q } y |  j | d g  Wq t j k
 r8\ } } | t j k r9  q9q Xq Wd S(   s~   Erase this ldb.

        :note: Removes all records, except those that are controlled by
            Samba4's schema.
        t    sJ   (&(|(objectclass=*)(distinguishedName=*))(!(distinguishedName=@BASEINFO)))t   controlss   show_deleted:0s   show_recycled:0s   relax:0i    s   @SUBCLASSESs   @MODULESs   @OPTIONSs
   @PARTITIONs
   @KLUDGEACLN(
   RD   R.   R   R=   R@   RA   R>   R?   R/   R1   (   R%   R5   R   RB   RC   R8   t   attr(    (    s2   /usr/lib/python2.7/dist-packages/samba/__init__.pyt   erase_except_schema_controlled   s*    	c         C   sp   |  j    x_ d d g D]Q } y |  j | d g  Wq t j k
 rg \ } } | t j k rh   qh q Xq Wd S(   s%   Erase this ldb, removing all records.s
   @INDEXLISTs   @ATTRIBUTESs   relax:0N(   RH   R@   R   R>   R?   (   R%   RG   RB   RC   (    (    s2   /usr/lib/python2.7/dist-packages/samba/__init__.pyt   erase   s    
c         C   s    |  j  t | d  j    d S(   sH   Load a LDIF file.

        :param ldif_path: Path to LDIF file.
        t   rN(   t   add_ldift   opent   read(   R%   t	   ldif_path(    (    s2   /usr/lib/python2.7/dist-packages/samba/__init__.pyt   load_ldif_file_add   s    c         C   sI   xB |  j  |  D]1 \ } } | t j k s1 t  |  j | |  q Wd S(   sJ   Add data based on a LDIF string.

        :param ldif: LDIF text.
        N(   t
   parse_ldifR   t   CHANGETYPE_NONER1   t   add(   R%   t   ldifRF   t
   changetypeR   (    (    s2   /usr/lib/python2.7/dist-packages/samba/__init__.pyRK      s    c         C   sV   xO |  j  |  D]> \ } } | t j k r> |  j | |  q |  j | |  q Wd S(   sQ   Modify database based on a LDIF string.

        :param ldif: LDIF text.
        N(   RP   R   t   CHANGETYPE_ADDRR   t   modify(   R%   RS   RF   RT   R   (    (    s2   /usr/lib/python2.7/dist-packages/samba/__init__.pyt   modify_ldif   s    N(   t   __name__t
   __module__t   __doc__R   R-   R   t
   SCOPE_BASER:   RD   RH   RI   RO   RK   RW   (    (    (    s2   /usr/lib/python2.7/dist-packages/samba/__init__.pyR   6   s   5		'			c         C   s{   xt | j    D]f \ } } t | t  s8 t d |   t | t  s] t d | | f   |  j d | |  }  q W|  S(   s   Substitute 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.
    s   %r is not a strings   Value %r for %s is not a strings   ${%s}(   t   itemst
   isinstancet   strR1   t   replace(   R   R9   t   namet   value(    (    s2   /usr/lib/python2.7/dist-packages/samba/__init__.pyt   substitute_var   s
    %c         C   sP   d |  k r d S|  j  d  } |  j  d |  } t d |  | | d !  d S(   s   Check that all substitution variables in a string have been replaced.

    If not, raise an exception.

    :param text: The text to search for substitution variables
    s   ${Nt   }s!   Not all variables substituted: %si   (   t   findt	   Exception(   R   t	   var_startt   var_end(    (    s2   /usr/lib/python2.7/dist-packages/samba/__init__.pyt   check_all_substituted   s    c         C   sA   t  |  d  j   } | d k	 r= t | |  } t |  n  | S(   s   Read 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.
    RJ   N(   RL   RM   R   Rb   Rh   (   t	   file_namet
   subst_varst   data(    (    s2   /usr/lib/python2.7/dist-packages/samba/__init__.pyt   read_and_sub_file  s
    c         C   sc   t  j j |  r" t  j |  n  t |  |  } t | d  } z | j |  Wd | j   Xd S(   s   Setup 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.
    t   wN(   R   R   R   t   unlinkRl   RL   t   writet   close(   t   templatet   fnameRj   Rk   t   f(    (    s2   /usr/lib/python2.7/dist-packages/samba/__init__.pyt
   setup_file  s    i   c         C   s   |  j    p |  d k S(   Ns    !#$%&'()-.@^_{}~(   t   isalnum(   t   c(    (    s2   /usr/lib/python2.7/dist-packages/samba/__init__.pyt   is_valid_netbios_char/  s    c         C   s;   t  |   t k r t Sx |  D] } t |  s t Sq Wt S(   s1   Check whether a name is valid as a NetBIOS name. (   R/   t   MAX_NETBIOS_NAME_LENR   Rw   R   (   R`   t   x(    (    s2   /usr/lib/python2.7/dist-packages/samba/__init__.pyt   valid_netbios_name3  s    c         C   s   t    r_ t j j t   | |  } | t j k rI t j j d |  n  t |   t j |  <n& t d | |  f d | g t j |  <d S(   s  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
    i    s   %s.%st   fromlistN(	   R   R   R   R   R   t   syst   insertt
   __import__t   modules(   t
   modulenamet   locationt   source_tree_containert	   namespacet
   extra_path(    (    s2   /usr/lib/python2.7/dist-packages/samba/__init__.pyt   import_bundled_package>  s    		c         C   s?   y t  |   Wn* t k
 r: t |  | d d d d n Xd S(   s   Add 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)
    R   t   third_partyR   s   samba.third_partyN(   R~   t   ImportErrorR   (   R   R   (    (    s2   /usr/lib/python2.7/dist-packages/samba/__init__.pyt   ensure_third_party_moduleW  s    c         C   s   d d j  |  j d   S(   s.   return a DN from a DNS name domain/forest roots   DC=s   ,DC=t   .(   R   t   split(   t	   dnsdomain(    (    s2   /usr/lib/python2.7/dist-packages/samba/__init__.pyt   dn_from_dns_namef  s    c           C   s   t  t j    S(   N(   t   intt   time(    (    (    s2   /usr/lib/python2.7/dist-packages/samba/__init__.pyt   current_unix_timej  s    c         C   sH   d g t  |   } x. t t  |    D] } t |  |  | | <q& W| S(   Ni    (   R/   t   ranget   ord(   t   stringt   blobt   i(    (    s2   /usr/lib/python2.7/dist-packages/samba/__init__.pyt   string_to_byte_arraym  s    c         C   s   y0 d d l  m } | j |   } | j |  SWn t k
 rE } n Xy- d d l m } | |   } | j |  SWn t k
 r } n Xt d d   d  S(   Ni(   t   ARC4(   t   RC4s   arcfour_encrypt() requires s.   python*-crypto or python*-m2crypto or m2crypto(	   t   Crypto.CipherR   t   newt   encryptR   t   M2Crypto.RC4R   t   updateRe   (   t   keyRk   R   Rv   t   eR   (    (    s2   /usr/lib/python2.7/dist-packages/samba/__init__.pyt   arcfour_encryptu  s    (&   RZ   t   __docformat__R   R|   R   t   samba.paramR   R   R   R   t
   samba._ldbR   t   _LdbRb   Rh   Rl   R   Rt   Rx   Rw   Rz   R   R   R   R   R   R   t   _gluet   versiont   interface_ipst   set_debug_levelt   get_debug_levelt   unix2nttimet   nttime2stringt   nttime2unixt   generate_random_passwordt   strcasecmp_mt   strstr_m(    (    (    s2   /usr/lib/python2.7/dist-packages/samba/__init__.pyt   <module>   sF   	
																							