s4-dsdb: pass parent request to dsdb_module_*() functions
[samba.git] / source4 / dsdb / samdb / ldb_modules / samba_dsdb.c
index a461a9480614dcaa1082e470306ec10d8f2f243d..bc5784e5acf28cdea95fcdd2b9357ca434df7df9 100644 (file)
 #include "lib/ldb/include/ldb.h"
 #include "lib/ldb/include/ldb_errors.h"
 #include "lib/ldb/include/ldb_module.h"
-#include "lib/ldb/include/ldb_private.h"
-
 #include "dsdb/samdb/ldb_modules/util.h"
 #include "dsdb/samdb/samdb.h"
 #include "librpc/ndr/libndr.h"
 
 static int read_at_rootdse_record(struct ldb_context *ldb, struct ldb_module *module, TALLOC_CTX *mem_ctx,
-                                 struct ldb_message **msg)
+                                 struct ldb_message **msg, struct ldb_request *parent)
 {
        int ret;
        static const char *rootdse_attrs[] = { "defaultNamingContext", "configurationNamingContext", "schemaNamingContext", NULL };
@@ -49,18 +47,17 @@ static int read_at_rootdse_record(struct ldb_context *ldb, struct ldb_module *mo
        struct ldb_dn *rootdse_dn;
        TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
        if (!tmp_ctx) {
-               ldb_oom(ldb);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(ldb);
        }
 
        rootdse_dn = ldb_dn_new(tmp_ctx, ldb, "@ROOTDSE");
        if (!rootdse_dn) {
                talloc_free(tmp_ctx);
-               ldb_oom(ldb);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(ldb);
        }
 
-       ret = dsdb_module_search_dn(module, tmp_ctx, &rootdse_res, rootdse_dn, rootdse_attrs, 0);
+       ret = dsdb_module_search_dn(module, tmp_ctx, &rootdse_res, rootdse_dn,
+                                   rootdse_attrs, DSDB_FLAG_NEXT_MODULE, parent);
        if (ret != LDB_SUCCESS) {
                talloc_free(tmp_ctx);
                return ret;
@@ -87,8 +84,7 @@ static int prepare_modules_line(struct ldb_context *ldb,
        char *full_string;
        TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
        if (!tmp_ctx) {
-               ldb_oom(ldb);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(ldb);
        }
 
        if (backend_attr) {
@@ -112,22 +108,19 @@ static int prepare_modules_line(struct ldb_context *ldb,
        }
        if (!backend_full_list) {
                talloc_free(tmp_ctx);
-               ldb_oom(ldb);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(ldb);
        }
 
        backend_full_list = str_list_append_const(backend_full_list, backend_mod_list);
        if (!backend_full_list) {
                talloc_free(tmp_ctx);
-               ldb_oom(ldb);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(ldb);
        }
 
        mod_list_string = str_list_join(tmp_ctx, backend_full_list, ',');
        if (!mod_list_string) {
                talloc_free(tmp_ctx);
-               ldb_oom(ldb);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(ldb);
        }
 
        full_string = talloc_asprintf(tmp_ctx, "%s:%s", backend_dn, mod_list_string);
@@ -138,53 +131,6 @@ static int prepare_modules_line(struct ldb_context *ldb,
 
 
 
-/*
-  initialise the invocationID for a standalone server
- */
-static int initialise_invocation_id(struct ldb_module *module, struct GUID *guid)
-{
-       struct ldb_message *msg;
-       struct ldb_context *ldb = ldb_module_get_ctx(module);
-       int ret;
-
-       *guid = GUID_random();
-
-       msg = ldb_msg_new(module);
-       if (msg == NULL) {
-               ldb_module_oom(module);
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-       msg->dn = ldb_dn_new(msg, ldb, "@SAMBA_DSDB");
-       if (!msg->dn) {
-               ldb_module_oom(module);
-               talloc_free(msg);
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-       ret = dsdb_msg_add_guid(msg, guid, "invocationID");
-       if (ret != LDB_SUCCESS) {
-               ldb_module_oom(module);
-               talloc_free(msg);
-               return ret;
-       }
-       msg->elements[0].flags = LDB_FLAG_MOD_ADD;
-
-       ret = ldb_modify(ldb, msg);
-       if (ret != LDB_SUCCESS) {
-               ldb_asprintf_errstring(ldb, "Failed to setup standalone invocationID - %s",
-                                      ldb_errstring(ldb));
-               talloc_free(msg);
-               return ret;
-       }
-
-       DEBUG(1,("Initialised standalone invocationID to %s\n",
-                GUID_string(msg, guid)));
-
-       talloc_free(msg);
-
-       return LDB_SUCCESS;
-}
-
-
 static int samba_dsdb_init(struct ldb_module *module)
 {
        struct ldb_context *ldb = ldb_module_get_ctx(module);
@@ -202,9 +148,11 @@ static int samba_dsdb_init(struct ldb_module *module)
          Some Known ordering constraints:
          - rootdse must be first, as it makes redirects from "" -> cn=rootdse
          - extended_dn_in must be before objectclass.c, as it resolves the DN
-         - objectclass must be before password_hash, because password_hash checks
-           that the objectclass is of type person (filled in by objectclass
-           module when expanding the objectclass list)
+         - objectclass must be before password_hash and samldb since these LDB
+           modules require the expanded "objectClass" list
+         - objectclass_attrs must be behind operational in order to see all
+           attributes (the operational module protects and therefore
+           suppresses per default some important ones)
          - partition must be last
          - each partition has its own module list then
 
@@ -222,20 +170,25 @@ static int samba_dsdb_init(struct ldb_module *module)
                                             "asq",
                                             "extended_dn_store",
                                             "extended_dn_in",
-                                            "rdn_name",
                                             "objectclass",
                                             "descriptor",
                                             "acl",
+                                            "aclread",
                                             "samldb",
                                             "password_hash",
                                             "operational",
-                                            "kludge_acl",
                                             "schema_load",
                                             "instancetype",
+                                            "objectclass_attrs",
                                             NULL };
 
        const char **link_modules;
+       static const char *fedora_ds_modules[] = {
+               "rdn_name", NULL };
+       static const char *openldap_modules[] = {
+               NULL };
        static const char *tdb_modules_list[] = {
+               "rdn_name",
                "subtree_delete",
                "repl_meta_data",
                "subtree_rename",
@@ -254,23 +207,27 @@ static int samba_dsdb_init(struct ldb_module *module)
 
        const char **backend_modules;
        static const char *fedora_ds_backend_modules[] = {
-               "nsuniqueid", "paged_searches", NULL };
+               "nsuniqueid", "paged_searches", "simple_dn", NULL };
        static const char *openldap_backend_modules[] = {
-               "entryuuid", "paged_searches", NULL };
+               "entryuuid", "paged_searches", "simple_dn", NULL };
 
-       static const char *samba_dsdb_attrs[] = { "backendType", "serverRole", "invocationID", NULL };
+       static const char *samba_dsdb_attrs[] = { "backendType", "serverRole", NULL };
        const char *backendType, *serverRole;
 
        if (!tmp_ctx) {
-               ldb_oom(ldb);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(ldb);
+       }
+
+       ret = ldb_register_samba_handlers(ldb);
+       if (ret != LDB_SUCCESS) {
+               talloc_free(tmp_ctx);
+               return ret;
        }
 
        samba_dsdb_dn = ldb_dn_new(tmp_ctx, ldb, "@SAMBA_DSDB");
        if (!samba_dsdb_dn) {
                talloc_free(tmp_ctx);
-               ldb_oom(ldb);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(ldb);
        }
 
 #define CHECK_LDB_RET(check_ret)                               \
@@ -281,7 +238,8 @@ static int samba_dsdb_init(struct ldb_module *module)
                }                                               \
        } while (0)
 
-       ret = dsdb_module_search_dn(module, tmp_ctx, &res, samba_dsdb_dn, samba_dsdb_attrs, 0);
+       ret = dsdb_module_search_dn(module, tmp_ctx, &res, samba_dsdb_dn,
+                                   samba_dsdb_attrs, DSDB_FLAG_NEXT_MODULE, NULL);
        if (ret == LDB_ERR_NO_SUCH_OBJECT) {
                backendType = "ldb";
                serverRole = "domain controller";
@@ -293,46 +251,25 @@ static int samba_dsdb_init(struct ldb_module *module)
                return ret;
        }
 
-       if (strcmp(serverRole, "standalone") == 0 ||
-           strcmp(serverRole, "member server") == 0) {
-               struct GUID *guid;
-
-               guid = talloc(module, struct GUID);
-               if (!guid) {
-                       ldb_module_oom(module);
-                       return LDB_ERR_OPERATIONS_ERROR;
-               }
-
-               *guid = samdb_result_guid(res->msgs[0], "invocationID");
-               if (GUID_all_zero(guid)) {
-                       ret = initialise_invocation_id(module, guid);
-                       if (ret != LDB_SUCCESS) {
-                               talloc_free(tmp_ctx);
-                               return ret;
-                       }
-               }
-
-               /* cache the domain_sid in the ldb. See the matching
-                * code in samdb_ntds_invocation_id() */
-               ret = ldb_set_opaque(ldb, "cache.invocation_id", guid);
-               if (ret != LDB_SUCCESS) {
-                       talloc_free(tmp_ctx);
-                       return ret;
-               }
-       }
-
        backend_modules = NULL;
        if (strcasecmp(backendType, "ldb") == 0) {
                extended_dn_module = extended_dn_module_ldb;
                link_modules = tdb_modules_list;
        } else {
-               link_modules = NULL;
                if (strcasecmp(backendType, "fedora-ds") == 0) {
+                       link_modules = fedora_ds_modules;
                        backend_modules = fedora_ds_backend_modules;
                        extended_dn_module = extended_dn_module_fds;
                } else if (strcasecmp(backendType, "openldap") == 0) {
+                       link_modules = openldap_modules;
                        backend_modules = openldap_backend_modules;
                        extended_dn_module = extended_dn_module_openldap;
+               } else {
+                       return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR, "invalid backend type");
+               }
+               ret = ldb_set_opaque(ldb, "readOnlySchema", (void*)1);
+               if (ret != LDB_SUCCESS) {
+                       ldb_set_errstring(ldb, "Failed to set readOnlySchema opaque");
                }
        }
 
@@ -340,8 +277,7 @@ static int samba_dsdb_init(struct ldb_module *module)
        do {                                                    \
                if (!final_module_list) {                       \
                        talloc_free(tmp_ctx);                   \
-                       ldb_oom(ldb);                           \
-                       return LDB_ERR_OPERATIONS_ERROR;        \
+                       return ldb_oom(ldb);                    \
                }                                               \
        } while (0)
 
@@ -358,7 +294,7 @@ static int samba_dsdb_init(struct ldb_module *module)
        CHECK_MODULE_LIST;
 
 
-       ret = read_at_rootdse_record(ldb, module, tmp_ctx, &rootdse_msg);
+       ret = read_at_rootdse_record(ldb, module, tmp_ctx, &rootdse_msg, NULL);
        CHECK_LDB_RET(ret);
 
        partition_msg = ldb_msg_new(tmp_ctx);
@@ -399,8 +335,7 @@ static int samba_dsdb_init(struct ldb_module *module)
        reverse_module_list = talloc_array(tmp_ctx, const char *, len+1);
        if (!reverse_module_list) {
                talloc_free(tmp_ctx);
-               ldb_oom(ldb);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(ldb);
        }
        for (i=0; i < len; i++) {
                reverse_module_list[i] = final_module_list[(len - 1) - i];
@@ -410,18 +345,24 @@ static int samba_dsdb_init(struct ldb_module *module)
        /* The backend (at least until the partitions module
         * reconfigures things) is the next module in the currently
         * loaded chain */
-       backend_module = module->next;
-       ret = ldb_load_modules_list(ldb, reverse_module_list, backend_module, &module_chain);
+       backend_module = ldb_module_next(module);
+       ret = ldb_module_load_list(ldb, reverse_module_list, backend_module, &module_chain);
        CHECK_LDB_RET(ret);
 
        talloc_free(tmp_ctx);
        /* Set this as the 'next' module, so that we effectivly append it to module chain */
-       module->next = module_chain;
+       ldb_module_set_next(module, module_chain);
 
        return ldb_next_init(module);
 }
 
-const struct ldb_module_ops ldb_samba_dsdb_module_ops = {
+static const struct ldb_module_ops ldb_samba_dsdb_module_ops = {
        .name              = "samba_dsdb",
        .init_context      = samba_dsdb_init,
 };
+
+int ldb_samba_dsdb_module_init(const char *version)
+{
+       LDB_MODULE_CHECK_VERSION(version);
+       return ldb_register_module(&ldb_samba_dsdb_module_ops);
+}