VERSION: Bump version number up to 4.0.4.
[samba.git] / source4 / scripting / python / samba / __init__.py
index dd876f910d27ed6a5ed8c5949870fe1eaa7f4968..cd2a309fc0aea445139081ec4e2117fd1b95bea0 100644 (file)
@@ -1,5 +1,3 @@
-#!/usr/bin/python
-
 # Unix SMB/CIFS implementation.
 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
 #
 __docformat__ = "restructuredText"
 
 import os
+import sys
+import samba.param
+
 
-def _in_source_tree():
-    """Check whether the script is being run from the source dir. """
-    return os.path.exists("%s/../../../selftest/skip" % os.path.dirname(__file__))
+def source_tree_topdir():
+    """Return the top level source directory."""
+    paths = ["../../..", "../../../.."]
+    for p in paths:
+        topdir = os.path.normpath(os.path.join(os.path.dirname(__file__), p))
+        if os.path.exists(os.path.join(topdir, 'source4')):
+            return topdir
+    raise RuntimeError("unable to find top level source directory")
 
 
-# When running, in-tree, make sure bin/python is in the PYTHONPATH
-if _in_source_tree():
-    import sys
-    srcdir = "%s/../../.." % os.path.dirname(__file__)
-    sys.path.append("%s/bin/python" % srcdir)
-    default_ldb_modules_dir = "%s/bin/modules/ldb" % srcdir
-else:
-    default_ldb_modules_dir = None
+def in_source_tree():
+    """Return True if we are running from within the samba source tree"""
+    try:
+        topdir = source_tree_topdir()
+    except RuntimeError:
+        return False
+    return True
 
 
 import ldb
-import dsdb
-import glue
-
+from samba._ldb import Ldb as _Ldb
 
 
-class Ldb(ldb.Ldb):
+class Ldb(_Ldb):
     """Simple Samba-specific LDB subclass that takes care
     of setting up the modules dir, credentials pointers, etc.
 
@@ -55,6 +58,7 @@ class Ldb(ldb.Ldb):
     not necessarily the Sam database. For Sam-specific helper
     functions see samdb.py.
     """
+
     def __init__(self, url=None, lp=None, modules_dir=None, session_info=None,
                  credentials=None, flags=0, options=None):
         """Opens a Samba Ldb file.
@@ -74,10 +78,8 @@ class Ldb(ldb.Ldb):
 
         if modules_dir is not None:
             self.set_modules_dir(modules_dir)
-        elif default_ldb_modules_dir is not None:
-            self.set_modules_dir(default_ldb_modules_dir)
-        elif lp is not None:
-            self.set_modules_dir(os.path.join(lp.get("modules dir"), "ldb"))
+        else:
+            self.set_modules_dir(os.path.join(samba.param.modules_dir(), "ldb"))
 
         if session_info is not None:
             self.set_session_info(session_info)
@@ -91,40 +93,26 @@ class Ldb(ldb.Ldb):
         # This must be done before we load the schema, as these handlers for
         # objectSid and objectGUID etc must take precedence over the 'binary
         # attribute' declaration in the schema
-        glue.ldb_register_samba_handlers(self)
+        self.register_samba_handlers()
 
         # TODO set debug
-        def msg(l,text):
+        def msg(l, text):
             print text
         #self.set_debug(msg)
 
-        glue.ldb_set_utf8_casefold(self)
+        self.set_utf8_casefold()
 
         # Allow admins to force non-sync ldb for all databases
         if lp is not None:
             nosync_p = lp.get("nosync", "ldb")
-            if nosync_p is not None and nosync_p == True:
-                flags |= FLG_NOSYNC
+            if nosync_p is not None and nosync_p:
+                flags |= ldb.FLG_NOSYNC
 
-        self.set_create_perms()
+        self.set_create_perms(0600)
 
         if url is not None:
             self.connect(url, flags, options)
 
-    def set_session_info(self, session_info):
-        glue.ldb_set_session_info(self, session_info)
-
-    def set_credentials(self, credentials):
-        glue.ldb_set_credentials(self, credentials)
-
-    def set_loadparm(self, lp_ctx):
-        glue.ldb_set_loadparm(self, lp_ctx)
-
-    def set_create_perms(self, perms=0600):
-        # we usually want Samba databases to be private. If we later find we
-        # need one public, we will have to change this here
-        super(Ldb, self).set_create_perms(perms)
-
     def searchone(self, attribute, basedn=None, expression=None,
                   scope=ldb.SCOPE_BASE):
         """Search for one attribute as a string.
@@ -143,7 +131,11 @@ class Ldb(ldb.Ldb):
         return self.schema_format_value(attribute, values.pop())
 
     def erase_users_computers(self, dn):
-        """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."""
+        """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.
+        """
 
         try:
             res = self.search(base=dn, scope=ldb.SCOPE_SUBTREE, attrs=[],
@@ -157,41 +149,46 @@ class Ldb(ldb.Ldb):
 
         try:
             for msg in res:
-                self.delete(msg.dn)
+                self.delete(msg.dn, ["relax:0"])
         except ldb.LdbError, (errno, _):
             if errno != ldb.ERR_NO_SUCH_OBJECT:
                 # Ignore no such object errors
                 raise
 
     def erase_except_schema_controlled(self):
-        """Erase this ldb, removing all records, except those that are controlled by Samba4's schema."""
+        """Erase this ldb.
+
+        :note: Removes all records, except those that are controlled by
+            Samba4's schema.
+        """
 
         basedn = ""
 
         # Try to delete user/computer accounts to allow deletion of groups
         self.erase_users_computers(basedn)
 
-        # Delete the 'visible' records, and the invisble 'deleted' records (if this DB supports it)
+        # Delete the 'visible' records, and the invisble 'deleted' records (if
+        # this DB supports it)
         for msg in self.search(basedn, ldb.SCOPE_SUBTREE,
-                               "(&(|(objectclass=*)(distinguishedName=*))(!(distinguishedName=@BASEINFO)))",
-                               [], controls=["show_deleted:0"]):
+                       "(&(|(objectclass=*)(distinguishedName=*))(!(distinguishedName=@BASEINFO)))",
+                       [], controls=["show_deleted:0", "show_recycled:0"]):
             try:
-                self.delete(msg.dn)
+                self.delete(msg.dn, ["relax:0"])
             except ldb.LdbError, (errno, _):
                 if errno != ldb.ERR_NO_SUCH_OBJECT:
                     # Ignore no such object errors
                     raise
 
         res = self.search(basedn, ldb.SCOPE_SUBTREE,
-                          "(&(|(objectclass=*)(distinguishedName=*))(!(distinguishedName=@BASEINFO)))",
-                          [], controls=["show_deleted:0"])
+            "(&(|(objectclass=*)(distinguishedName=*))(!(distinguishedName=@BASEINFO)))",
+            [], controls=["show_deleted:0", "show_recycled:0"])
         assert len(res) == 0
 
         # delete the specials
         for attr in ["@SUBCLASSES", "@MODULES",
                      "@OPTIONS", "@PARTITION", "@KLUDGEACL"]:
             try:
-                self.delete(attr)
+                self.delete(attr, ["relax:0"])
             except ldb.LdbError, (errno, _):
                 if errno != ldb.ERR_NO_SUCH_OBJECT:
                     # Ignore missing dn errors
@@ -199,51 +196,17 @@ class Ldb(ldb.Ldb):
 
     def erase(self):
         """Erase this ldb, removing all records."""
-
         self.erase_except_schema_controlled()
 
         # delete the specials
         for attr in ["@INDEXLIST", "@ATTRIBUTES"]:
             try:
-                self.delete(attr)
+                self.delete(attr, ["relax:0"])
             except ldb.LdbError, (errno, _):
                 if errno != ldb.ERR_NO_SUCH_OBJECT:
                     # Ignore missing dn errors
                     raise
 
-    def erase_partitions(self):
-        """Erase an ldb, removing all records."""
-
-        def erase_recursive(self, dn):
-            try:
-                res = self.search(base=dn, scope=ldb.SCOPE_ONELEVEL, attrs=[],
-                                  controls=["show_deleted:0"])
-            except ldb.LdbError, (errno, _):
-                if errno == ldb.ERR_NO_SUCH_OBJECT:
-                    # Ignore no such object errors
-                    return
-
-            for msg in res:
-                erase_recursive(self, msg.dn)
-
-            try:
-                self.delete(dn)
-            except ldb.LdbError, (errno, _):
-                if errno != ldb.ERR_NO_SUCH_OBJECT:
-                    # Ignore no such object errors
-                    raise
-
-        res = self.search("", ldb.SCOPE_BASE, "(objectClass=*)",
-                         ["namingContexts"])
-        assert len(res) == 1
-        if not "namingContexts" in res[0]:
-            return
-        for basedn in res[0]["namingContexts"]:
-            # Try to delete user/computer accounts to allow deletion of groups
-            self.erase_users_computers(basedn)
-            # Try and erase from the bottom-up in the tree
-            erase_recursive(self, basedn)
-
     def load_ldif_file_add(self, ldif_path):
         """Load a LDIF file.
 
@@ -258,7 +221,7 @@ class Ldb(ldb.Ldb):
         """
         for changetype, msg in self.parse_ldif(ldif):
             assert changetype == ldb.CHANGETYPE_NONE
-            self.add(msg,controls)
+            self.add(msg, controls)
 
     def modify_ldif(self, ldif, controls=None):
         """Modify database based on a LDIF string.
@@ -266,67 +229,15 @@ class Ldb(ldb.Ldb):
         :param ldif: LDIF text.
         """
         for changetype, msg in self.parse_ldif(ldif):
-            if (changetype == ldb.CHANGETYPE_ADD):
+            if changetype == ldb.CHANGETYPE_ADD:
                 self.add(msg, controls)
             else:
                 self.modify(msg, controls)
 
-    def set_domain_sid(self, sid):
-        """Change the domain SID used by this LDB.
-
-        :param sid: The new domain sid to use.
-        """
-        glue.samdb_set_domain_sid(self, sid)
-
-    def domain_sid(self):
-        """Read the domain SID used by this LDB.
-
-        """
-        glue.samdb_get_domain_sid(self)
-
-    def set_schema_from_ldif(self, pf, df):
-        glue.dsdb_set_schema_from_ldif(self, pf, df)
-
-    def set_schema_from_ldb(self, ldb):
-        glue.dsdb_set_schema_from_ldb(self, ldb)
-
-    def write_prefixes_from_schema(self):
-        glue.dsdb_write_prefixes_from_schema_to_ldb(self)
-
-    def convert_schema_to_openldap(self, target, mapping):
-        return glue.dsdb_convert_schema_to_openldap(self, target, mapping)
-
-    def set_invocation_id(self, invocation_id):
-        """Set the invocation id for this SamDB handle.
-
-        :param invocation_id: GUID of the invocation id.
-        """
-        glue.dsdb_set_ntds_invocation_id(self, invocation_id)
-
-    def get_invocation_id(self):
-        "Get the invocation_id id"
-        return glue.samdb_ntds_invocation_id(self)
-
-    def get_ntds_GUID(self):
-        "Get the NTDS objectGUID"
-        return glue.samdb_ntds_objectGUID(self)
-
-    def server_site_name(self):
-        "Get the server site name"
-        return dsdb.samdb_server_site_name(self)
-
-    def set_opaque_integer(self, name, value):
-        """Set an integer as an opaque (a flag or other value) value on the database
-
-        :param name: The name for the opaque value
-        :param value: The integer value
-        """
-        glue.dsdb_set_opaque_integer(self, name, value)
-
 
 def substitute_var(text, values):
-    """substitute strings of the form ${NAME} in str, replacing
-    with substitutions from subobj.
+    """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.
@@ -341,7 +252,8 @@ def substitute_var(text, values):
 
 
 def check_all_substituted(text):
-    """Make sure that all substitution variables in a string have been replaced.
+    """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
@@ -352,16 +264,17 @@ def check_all_substituted(text):
     var_start = text.find("${")
     var_end = text.find("}", var_start)
 
-    raise Exception("Not all variables substituted: %s" % text[var_start:var_end+1])
+    raise Exception("Not all variables substituted: %s" %
+        text[var_start:var_end+1])
 
 
-def read_and_sub_file(file, subst_vars):
+def read_and_sub_file(file_name, subst_vars):
     """Read a file and sub in variables found in it
 
-    :param file: File to be read (typically from setup directory)
+    :param file_name: File to be read (typically from setup directory)
      param subst_vars: Optional variables to subsitute in the file.
     """
-    data = open(file, 'r').read()
+    data = open(file_name, 'r').read()
     if subst_vars is not None:
         data = substitute_var(data, subst_vars)
         check_all_substituted(data)
@@ -375,48 +288,76 @@ def setup_file(template, fname, subst_vars=None):
     :param fname: Path of the file to create.
     :param subst_vars: Substitution variables.
     """
-    f = fname
-
-    if os.path.exists(f):
-        os.unlink(f)
+    if os.path.exists(fname):
+        os.unlink(fname)
 
     data = read_and_sub_file(template, subst_vars)
-    open(f, 'w').write(data)
+    f = open(fname, 'w')
+    try:
+        f.write(data)
+    finally:
+        f.close()
+
+MAX_NETBIOS_NAME_LEN = 15
+def is_valid_netbios_char(c):
+    return (c.isalnum() or c in " !#$%&'()-.@^_{}~")
 
 
 def valid_netbios_name(name):
     """Check whether a name is valid as a NetBIOS name. """
     # See crh's book (1.4.1.1)
-    if len(name) > 15:
+    if len(name) > MAX_NETBIOS_NAME_LEN:
         return False
     for x in name:
-        if not x.isalnum() and not x in " !#$%&'()-.@^_{}~":
+        if not is_valid_netbios_char(x):
             return False
     return True
 
 
+def import_bundled_package(modulename, location):
+    """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}/lib)
+    """
+    if in_source_tree():
+        sys.path.insert(0, os.path.join(source_tree_topdir(), "lib", location))
+        sys.modules[modulename] = __import__(modulename)
+    else:
+        sys.modules[modulename] = __import__(
+            "samba.external.%s" % modulename, fromlist=["samba.external"])
+
+
 def ensure_external_module(modulename, location):
     """Add a location to sys.path if an external dependency can't be found.
 
     :param modulename: Module name to import
-    :param location: Location to add to sys.path (can be relative to 
-        ${srcdir}/lib
+    :param location: Location to add to sys.path (can be relative to
+        ${srcdir}/lib)
     """
     try:
         __import__(modulename)
     except ImportError:
-        import sys
-        if _in_source_tree():
-            sys.path.insert(0, 
-                os.path.join(os.path.dirname(__file__),
-                             "../../../../lib", location))
-            __import__(modulename)
-        else:
-            sys.modules[modulename] = __import__(
-                "samba.external.%s" % modulename, fromlist=["samba.external"])
-
-version = glue.version
-interface_ips = glue.interface_ips
-set_debug_level = glue.set_debug_level
-unix2nttime = glue.unix2nttime
-generate_random_password = glue.generate_random_password
+        import_bundled_package(modulename, location)
+
+
+def dn_from_dns_name(dnsdomain):
+    """return a DN from a DNS name domain/forest root"""
+    return "DC=" + ",DC=".join(dnsdomain.split("."))
+
+import _glue
+version = _glue.version
+interface_ips = _glue.interface_ips
+set_debug_level = _glue.set_debug_level
+get_debug_level = _glue.get_debug_level
+unix2nttime = _glue.unix2nttime
+nttime2string = _glue.nttime2string
+nttime2unix = _glue.nttime2unix
+unix2nttime = _glue.unix2nttime
+generate_random_password = _glue.generate_random_password
+strcasecmp_m = _glue.strcasecmp_m
+strstr_m = _glue.strstr_m