s4:provision Add C binding to get at the generate schema
authorAndrew Bartlett <abartlet@samba.org>
Tue, 10 Nov 2009 04:18:52 +0000 (15:18 +1100)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 11 Nov 2009 21:11:20 +0000 (08:11 +1100)
This will allow us to do local tests against that schema

source4/param/provision.c
source4/param/provision.h
source4/scripting/python/samba/schema.py

index 2f5f78abe65e78e2f4f39ed5ef0b85087bce0e61..8c6e1c0f684b61f6ed89713f9bf2e0c7559462df 100644 (file)
@@ -44,6 +44,14 @@ static PyObject *provision_module(void)
        return PyImport_Import(name);
 }
 
+static PyObject *schema_module(void)
+{
+       PyObject *name = PyString_FromString("samba.schema");
+       if (name == NULL)
+               return NULL;
+       return PyImport_Import(name);
+}
+
 NTSTATUS provision_bare(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx,
                        struct provision_settings *settings, 
                        struct provision_result *result)
@@ -298,3 +306,56 @@ failure:
        PyErr_Clear();
        return NT_STATUS_UNSUCCESSFUL;
 }
+
+
+struct ldb_context *provision_get_schema(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
+{
+       const char *setupdir;
+       PyObject *schema_mod, *schema_dict, *schema_fn, *py_result, *parameters;
+       
+       DEBUG(0,("Schema for DRS tests using python\n"));
+
+       py_load_samba_modules();
+       Py_Initialize();
+       py_update_path("bin"); /* FIXME: Can't assume this is always the case */
+
+       schema_mod = schema_module();
+
+       if (schema_mod == NULL) {
+               PyErr_Print();
+               DEBUG(0, ("Unable to import schema Python module.\n"));
+               return NULL;
+       }
+
+       schema_dict = PyModule_GetDict(schema_mod);
+
+       if (schema_dict == NULL) {
+               DEBUG(0, ("Unable to get dictionary for schema module\n"));
+               return NULL;
+       }
+
+       schema_fn = PyDict_GetItemString(schema_dict, "ldb_with_schema");
+       if (schema_fn == NULL) {
+               PyErr_Print();
+               DEBUG(0, ("Unable to get schema_get_ldb function\n"));
+               return NULL;
+       }
+       
+       parameters = PyDict_New();
+
+       setupdir = lp_setupdir(lp_ctx);
+       PyDict_SetItemString(parameters, "setup_dir", 
+                            PyString_FromString(setupdir));
+
+       py_result = PyEval_CallObjectWithKeywords(schema_fn, NULL, parameters);
+
+       Py_DECREF(parameters);
+
+       if (py_result == NULL) {
+               PyErr_Print();
+               PyErr_Clear();
+               return NULL;
+       }
+
+       return PyLdb_AsLdbContext(PyObject_GetAttrString(py_result, "ldb"));
+}
index c3b3bb88d80a58fd2997ccb4786dfa41700e2f58..b8277c358d9c64485264f4ccadedd4afd0406046 100644 (file)
@@ -64,4 +64,6 @@ NTSTATUS provision_store_self_join(TALLOC_CTX *mem_ctx, struct loadparm_context
                                   struct provision_store_self_join_settings *settings,
                                   const char **error_string);
 
+struct ldb_context *provision_get_schema(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx);
+
 #endif /* _PROVISION_H_ */
index 958a77d40f8b8dadd54a185328ecba7e83d47e21..2f90ac7122dbe0a61928d7d791cf0abdb8923c69 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)" \
@@ -138,3 +139,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)