s4:provision - Removed dependency on full Samba 3 schema from FDS
[samba.git] / source4 / scripting / python / samba / schema.py
index 0f34c4edda0e8cd25bf92637586e1e67e45c5749..6f45859ead4e3f04598944dc9d894fb917e46e00 100644 (file)
@@ -32,6 +32,7 @@ from samba import read_and_sub_file, substitute_var, check_all_substituted
 from samba import Ldb
 from samba.ndr import ndr_pack
 from ldb import SCOPE_SUBTREE, SCOPE_ONELEVEL, SCOPE_BASE
+import os
 
 def get_schema_descriptor(domain_sid):
     sddl = "O:SAG:SAD:(A;CI;RPLCLORC;;;AU)(A;CI;RPWPCRCCLCLORCWOWDSW;;;SA)" \
@@ -50,7 +51,7 @@ def get_schema_descriptor(domain_sid):
    
 class Schema(object):
     def __init__(self, setup_path, domain_sid, schemadn=None,
-                 serverdn=None, sambadn=None):
+                 serverdn=None, files=None, prefixmap=None):
         """Load schema for the SamDB from the AD schema files and samba4_schema.ldif
         
         :param samdb: Load a schema into a SamDB.
@@ -65,7 +66,11 @@ class Schema(object):
         self.ldb = Ldb()
         self.schema_data = read_ms_schema(setup_path('ad-schema/MS-AD_Schema_2K8_Attributes.txt'),
                                           setup_path('ad-schema/MS-AD_Schema_2K8_Classes.txt'))
-        self.schema_data += open(setup_path("schema_samba4.ldif"), 'r').read()
+
+        if files is not None:
+            for file in files:
+                self.schema_data += open(file, 'r').read()
+
         self.schema_data = substitute_var(self.schema_data, {"SCHEMADN": schemadn})
         check_all_substituted(self.schema_data)
 
@@ -80,13 +85,18 @@ class Schema(object):
                                                 "DESCRIPTOR": descr
                                                 })
 
-        prefixmap = open(setup_path("prefixMap.txt"), 'r').read()
-        prefixmap = b64encode(prefixmap)
+        self.prefixmap_data = open(setup_path("prefixMap.txt"), 'r').read()
+
+        if prefixmap is not None:
+            for map in prefixmap:
+                self.prefixmap_data += "%s\n" % map
+
+        self.prefixmap_data = b64encode(self.prefixmap_data)
 
         
 
         # We don't actually add this ldif, just parse it
-        prefixmap_ldif = "dn: cn=schema\nprefixMap:: %s\n\n" % prefixmap
+        prefixmap_ldif = "dn: cn=schema\nprefixMap:: %s\n\n" % self.prefixmap_data
         self.ldb.set_schema_from_ldif(prefixmap_ldif, self.schema_data)
 
     def write_to_tmp_ldb(self, schemadb_path):
@@ -138,3 +148,25 @@ def get_dnsyntax_attributes(schemadn,schemaldb):
         
     return attributes
 
+def ldb_with_schema(setup_dir=None, schemadn="cn=schema,cn=configuration,dc=example,dc=com", 
+                    serverdn="cn=server,cn=servers,cn=default-first-site-name,cn=sites,cn=cn=configuration,dc=example,dc=com",
+                    domainsid=None):
+    """Load schema for the SamDB from the AD schema files and samba4_schema.ldif
+    
+    :param setup_dir: Setup path
+    :param schemadn: DN of the schema
+    :param serverdn: DN of the server
+    
+    Returns the schema data loaded as an object, with .ldb being a
+    new ldb with the schema loaded.  This allows certain tests to
+    operate without a remote or local schema.
+    """
+    
+    def setup_path(file):
+        return os.path.join(setup_dir, file)
+
+    if domainsid is None:
+        domainsid = security.random_sid()
+    else:
+        domainsid = security.dom_sid(domainsid)
+    return Schema(setup_path, domainsid, schemadn=schemadn, serverdn=serverdn)