s4:dsdb Add new functions to help modules do an ldb_search()
authorAndrew Bartlett <abartlet@samba.org>
Fri, 9 Oct 2009 22:06:07 +0000 (09:06 +1100)
committerAndrew Bartlett <abartlet@samba.org>
Mon, 12 Oct 2009 05:50:50 +0000 (16:50 +1100)
These take an ldb_module argument, and avoid doing the search from the
top of the stack again.

(This will help when modules are initialised before being added to the
partition set)

Andrew Bartlett

source4/dsdb/samdb/ldb_modules/config.mk
source4/dsdb/samdb/ldb_modules/naming_fsmo.c
source4/dsdb/samdb/ldb_modules/pdc_fsmo.c
source4/dsdb/samdb/ldb_modules/util.c [new file with mode: 0644]
source4/dsdb/samdb/ldb_modules/util.h [new file with mode: 0644]

index 456ff5756c6e00e6f78c31aee747203a7f5db9bb..ea4e7228226e431f82b349a3b080c67a8a79ed6a 100644 (file)
@@ -1,3 +1,13 @@
+################################################
+# Start SUBSYSTEM DSDB_MODULE_HELPERS
+[SUBSYSTEM::DSDB_MODULE_HELPERS]
+PRIVATE_DEPENDENCIES = LIBLDB
+
+DSDB_MODULE_HELPERS_OBJ_FILES = $(dsdbsrcdir)/samdb/ldb_modules/util.o
+
+$(eval $(call proto_header_template,$(dsdbsrcdir)/samdb/ldb_modules/util_proto.h,$(DSDB_MODULE_HELPERS_OBJ_FILES:.o=.c)))
+
+
 ################################################
 # Start MODULE ldb_objectguid
 [MODULE::ldb_objectguid]
@@ -15,7 +25,7 @@ ldb_objectguid_OBJ_FILES = $(dsdbsrcdir)/samdb/ldb_modules/objectguid.o
 SUBSYSTEM = LIBLDB
 PRIVATE_DEPENDENCIES = SAMDB LIBTALLOC LIBEVENTS \
                        LIBNDR NDR_DRSUAPI \
-                       NDR_DRSBLOBS LIBNDR
+                       NDR_DRSBLOBS LIBNDR DSDB_MODULE_HELPERS
 INIT_FUNCTION = LDB_MODULE(repl_meta_data)
 # End MODULE ldb_repl_meta_data
 ################################################
@@ -39,7 +49,7 @@ ldb_dsdb_cache_OBJ_FILES = \
 # Start MODULE ldb_schema_fsmo
 [MODULE::ldb_schema_fsmo]
 SUBSYSTEM = LIBLDB
-PRIVATE_DEPENDENCIES = SAMDB LIBTALLOC LIBEVENTS
+PRIVATE_DEPENDENCIES = SAMDB LIBTALLOC LIBEVENTS DSDB_MODULE_HELPERS
 INIT_FUNCTION = LDB_MODULE(schema_fsmo)
 # End MODULE ldb_schema_fsmo
 ################################################
@@ -51,7 +61,7 @@ ldb_schema_fsmo_OBJ_FILES = \
 # Start MODULE ldb_naming_fsmo
 [MODULE::ldb_naming_fsmo]
 SUBSYSTEM = LIBLDB
-PRIVATE_DEPENDENCIES = SAMDB LIBTALLOC LIBEVENTS
+PRIVATE_DEPENDENCIES = SAMDB LIBTALLOC LIBEVENTS DSDB_MODULE_HELPERS
 INIT_FUNCTION = LDB_MODULE(naming_fsmo)
 # End MODULE ldb_naming_fsmo
 ################################################
@@ -63,7 +73,7 @@ ldb_naming_fsmo_OBJ_FILES = \
 # Start MODULE ldb_pdc_fsmo
 [MODULE::ldb_pdc_fsmo]
 SUBSYSTEM = LIBLDB
-PRIVATE_DEPENDENCIES = SAMDB LIBTALLOC LIBEVENTS
+PRIVATE_DEPENDENCIES = SAMDB LIBTALLOC LIBEVENTS DSDB_MODULE_HELPERS
 INIT_FUNCTION = LDB_MODULE(pdc_fsmo)
 # End MODULE ldb_pdc_fsmo
 ################################################
@@ -220,7 +230,7 @@ ldb_show_deleted_OBJ_FILES = $(dsdbsrcdir)/samdb/ldb_modules/show_deleted.o
 # Start MODULE ldb_partition
 [MODULE::ldb_partition]
 SUBSYSTEM = LIBLDB
-PRIVATE_DEPENDENCIES = LIBTALLOC LIBEVENTS SAMDB
+PRIVATE_DEPENDENCIES = LIBTALLOC LIBEVENTS SAMDB DSDB_MODULE_HELPERS
 INIT_FUNCTION = LDB_MODULE(partition)
 # End MODULE ldb_partition
 ################################################
index 607bf054d22c319aa6858a9f5cb56e99d5d221c7..15cad259ce8eea2173062fa69f115e1708130089 100644 (file)
@@ -28,6 +28,7 @@
 #include "librpc/gen_ndr/ndr_drsuapi.h"
 #include "librpc/gen_ndr/ndr_drsblobs.h"
 #include "../lib/util/dlinklist.h"
+#include "dsdb/samdb/ldb_modules/util.h"
 
 static int naming_fsmo_init(struct ldb_module *module)
 {
@@ -65,34 +66,15 @@ static int naming_fsmo_init(struct ldb_module *module)
        }
        ldb_module_set_private(module, naming_fsmo);
 
-       ret = ldb_search(ldb, mem_ctx, &naming_res,
-                        naming_dn, LDB_SCOPE_BASE,
-                        naming_attrs, NULL);
+       ret = dsdb_module_search_dn(module, mem_ctx, &naming_res,
+                                   naming_dn, 
+                                   naming_attrs);
        if (ret == LDB_ERR_NO_SUCH_OBJECT) {
                ldb_debug(ldb, LDB_DEBUG_WARNING,
                          "naming_fsmo_init: no partitions dn present: (skip loading of naming contexts details)\n");
                talloc_free(mem_ctx);
                return ldb_next_init(module);
        }
-       if (ret != LDB_SUCCESS) {
-               ldb_debug_set(ldb, LDB_DEBUG_FATAL,
-                             "naming_fsmo_init: failed to search the cross-ref container: %s: %s",
-                             ldb_strerror(ret), ldb_errstring(ldb));
-               talloc_free(mem_ctx);
-               return ret;
-       }
-       if (naming_res->count == 0) {
-               ldb_debug(ldb, LDB_DEBUG_WARNING,
-                         "naming_fsmo_init: no cross-ref container present: (skip loading of naming contexts details)\n");
-               talloc_free(mem_ctx);
-               return ldb_next_init(module);
-       } else if (naming_res->count > 1) {
-               ldb_debug_set(ldb, LDB_DEBUG_FATAL,
-                             "naming_fsmo_init: [%u] cross-ref containers found on a base search",
-                             naming_res->count);
-               talloc_free(mem_ctx);
-               return LDB_ERR_CONSTRAINT_VIOLATION;
-       }
 
        naming_fsmo->master_dn = ldb_msg_find_attr_as_dn(ldb, naming_fsmo, naming_res->msgs[0], "fSMORoleOwner");
        if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), naming_fsmo->master_dn) == 0) {
index 950f87eb74817f4532637b9c0123ac0e0ca2de52..6d814f9334f0e03661ef550898f12433ca331b62 100644 (file)
@@ -27,6 +27,7 @@
 #include "librpc/gen_ndr/ndr_drsuapi.h"
 #include "librpc/gen_ndr/ndr_drsblobs.h"
 #include "../lib/util/dlinklist.h"
+#include "dsdb/samdb/ldb_modules/util.h"
 
 static int pdc_fsmo_init(struct ldb_module *module)
 {
@@ -64,9 +65,9 @@ static int pdc_fsmo_init(struct ldb_module *module)
        }
        ldb_module_set_private(module, pdc_fsmo);
 
-       ret = ldb_search(ldb, mem_ctx, &pdc_res,
-                        pdc_dn, LDB_SCOPE_BASE,
-                        pdc_attrs, NULL);
+       ret = dsdb_module_search_dn(module, mem_ctx, &pdc_res,
+                                   pdc_dn, 
+                                   pdc_attrs);
        if (ret == LDB_ERR_NO_SUCH_OBJECT) {
                ldb_debug(ldb, LDB_DEBUG_WARNING,
                          "pdc_fsmo_init: no domain object present: (skip loading of domain details)\n");
@@ -79,19 +80,6 @@ static int pdc_fsmo_init(struct ldb_module *module)
                talloc_free(mem_ctx);
                return ret;
        }
-       if (pdc_res->count == 0) {
-               ldb_debug(ldb, LDB_DEBUG_WARNING,
-                         "pdc_fsmo_init: no domain object present: (skip loading of domain details)\n");
-               talloc_free(mem_ctx);
-               return ldb_next_init(module);
-       } else if (pdc_res->count > 1) {
-               ldb_debug_set(ldb, LDB_DEBUG_FATAL,
-                             "pdc_fsmo_init: [%u] domain objects found on a base search",
-                             pdc_res->count);
-               DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
-               talloc_free(mem_ctx);
-               return LDB_ERR_CONSTRAINT_VIOLATION;
-       }
 
        pdc_fsmo->master_dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx, pdc_res->msgs[0], "fSMORoleOwner");
        if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), pdc_fsmo->master_dn) == 0) {
diff --git a/source4/dsdb/samdb/ldb_modules/util.c b/source4/dsdb/samdb/ldb_modules/util.c
new file mode 100644 (file)
index 0000000..476eb08
--- /dev/null
@@ -0,0 +1,128 @@
+/* 
+   Unix SMB/CIFS implementation.
+   Samba utility functions
+
+   Copyright (C) Andrew Tridgell 2009
+   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2009
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "ldb.h"
+#include "ldb_module.h"
+
+/*
+  search for attrs on one DN, in the modules below
+ */
+int dsdb_module_search_dn(struct ldb_module *module,
+                         TALLOC_CTX *mem_ctx,
+                         struct ldb_result **_res,
+                         struct ldb_dn *basedn,
+                         const char * const *attrs)
+{
+       int ret;
+       struct ldb_request *req;
+       TALLOC_CTX *tmp_ctx;
+       struct ldb_result *res;
+
+       tmp_ctx = talloc_new(mem_ctx);
+
+       res = talloc_zero(tmp_ctx, struct ldb_result);
+       if (!res) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       ret = ldb_build_search_req(&req, ldb_module_get_ctx(module), tmp_ctx,
+                                  basedn,
+                                  LDB_SCOPE_BASE,
+                                  NULL,
+                                  attrs,
+                                  NULL,
+                                  res,
+                                  ldb_search_default_callback,
+                                  NULL);
+       if (ret != LDB_SUCCESS) {
+               talloc_free(tmp_ctx);
+               return ret;
+       }
+
+       ret = ldb_next_request(module, req);
+       if (ret == LDB_SUCCESS) {
+               ret = ldb_wait(req->handle, LDB_WAIT_ALL);
+       }
+
+       if (ret != LDB_SUCCESS) {
+               talloc_free(tmp_ctx);
+               return ret;
+       }
+
+       if (res->count != 1) {
+               /* we may be reading a DB that does not have the 'check base on search' option... */
+               ret = LDB_ERR_NO_SUCH_OBJECT;
+       } else {
+               *_res = talloc_steal(mem_ctx, res);
+       }
+       talloc_free(tmp_ctx);
+       return ret;
+}
+
+/*
+  search for attrs in the modules below
+ */
+int dsdb_module_search(struct ldb_module *module,
+                      TALLOC_CTX *mem_ctx,
+                      struct ldb_result **_res,
+                      struct ldb_dn *basedn, enum ldb_scope scope, 
+                      const char * const *attrs,
+                      const char *expression)
+{
+       int ret;
+       struct ldb_request *req;
+       TALLOC_CTX *tmp_ctx;
+       struct ldb_result *res;
+
+       tmp_ctx = talloc_new(mem_ctx);
+
+       res = talloc_zero(tmp_ctx, struct ldb_result);
+       if (!res) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       ret = ldb_build_search_req(&req, ldb_module_get_ctx(module), tmp_ctx,
+                                  basedn,
+                                  scope,
+                                  expression,
+                                  attrs,
+                                  NULL,
+                                  res,
+                                  ldb_search_default_callback,
+                                  NULL);
+       if (ret != LDB_SUCCESS) {
+               talloc_free(tmp_ctx);
+               return ret;
+       }
+
+       ret = ldb_next_request(module, req);
+       if (ret == LDB_SUCCESS) {
+               ret = ldb_wait(req->handle, LDB_WAIT_ALL);
+       }
+
+       talloc_free(req);
+       if (ret == LDB_SUCCESS) {
+               *_res = talloc_steal(mem_ctx, res);
+       }
+       talloc_free(tmp_ctx);
+       return ret;
+}
+
diff --git a/source4/dsdb/samdb/ldb_modules/util.h b/source4/dsdb/samdb/ldb_modules/util.h
new file mode 100644 (file)
index 0000000..0a1ab83
--- /dev/null
@@ -0,0 +1,22 @@
+/* 
+   Unix SMB/CIFS implementation.
+   Samba utility functions
+
+   Copyright (C) Andrew Tridgell 2009
+   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2009
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "dsdb/samdb/ldb_modules/util_proto.h"