5 Copyright (C) Simo Sorce 2004
7 ** NOTE! The following LGPL license applies to the ldb
8 ** library. This does NOT imply that all of Samba is released
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 * Component: ldb modules core
31 * Description: core modules routines
37 #include "dlinklist.h"
38 #include <sys/types.h>
42 #define LDB_MODULE_PREFIX "modules"
43 #define LDB_MODULE_PREFIX_LEN 7
44 #define LDB_MODULE_SEP ':'
46 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
48 struct ldb_module *current;
53 /* find out which modules we are requested to activate */
59 for (i = 0; options[i] != NULL; i++) {
61 if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
63 p = q = ldb_strdup(ldb, &options[i][LDB_MODULE_PREFIX_LEN]);
72 modules = ldb_realloc_array(ldb, modules, sizeof(char *), pn);
74 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in register_modules()\n");
78 } while ((p = strchr(q, LDB_MODULE_SEP)));
83 if (!modules && strcmp("ldap", ldb->modules->ops->name)) { /* no modules in the options, look for @MODULES in the db (not for ldap) */
85 char * attrs[] = { "@MODULE" };
86 struct ldb_message **msg;
88 ret = ldb_search(ldb, "", LDB_SCOPE_BASE, "dn=@MODULES", (const char * const *)attrs, &msg);
90 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db\n");
93 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb));
97 ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found, bailing out\n");
101 for (j = 0; j < msg[0]->num_elements; j++) {
102 for (k = 0; k < msg[0]->elements[j].num_values; k++) {
104 modules = ldb_realloc_array(ldb, modules, sizeof(char *), pn);
106 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in register_modules()\n");
109 modules[pn - 1] = ldb_strndup(ldb, msg[0]->elements[j].values[k].data, msg[0]->elements[j].values[k].length);
110 if (!modules[pn - 1]) {
111 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in register_modules()\n");
117 ldb_search_free(ldb, msg);
122 for (i = 0; i < pn; i++) {
124 if (strcmp(modules[i], "timestamps") == 0) {
125 current = timestamps_module_init(ldb, options);
127 ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
130 DLIST_ADD(ldb->modules, current);
134 if (strcmp(modules[i], "schema") == 0) {
135 current = schema_module_init(ldb, options);
137 ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
140 DLIST_ADD(ldb->modules, current);
144 #ifdef HAVE_DLOPEN_DISABLED
147 init_ldb_module_function init;
151 if (stat(modules[i], &st) < 0) {
152 ldb_debug(ldb, LDB_DEBUG_FATAL, "Required module [%s] not found, bailing out!\n", modules[i]);
156 handle = dlopen(modules[i], RTLD_LAZY);
159 ldb_debug(ldb, LDB_DEBUG_FATAL, "Error loading module %s [%s]\n", modules[i], dlerror());
163 init = (init_ldb_module_function)dlsym(handle, "init_module");
167 ldb_debug(ldb, LDB_DEBUG_FATAL, "Error trying to resolve symbol 'init_module' in %s [%s]\n", modules[i], errstr);
171 current = init(ldb, options);
173 ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
176 DLIST_ADD(ldb->modules, current);
179 ldb_debug(ldb, LDB_DEBUG_FATAL, "Required module [%s] not found, bailing out!\n", modules[i]);
189 helper functions to call the next module in chain
191 int ldb_next_close(struct ldb_module *module)
196 return module->next->ops->close(module->next);
199 int ldb_next_search(struct ldb_module *module,
201 enum ldb_scope scope,
202 const char *expression,
203 const char * const *attrs, struct ldb_message ***res)
208 return module->next->ops->search(module->next, base, scope, expression, attrs, res);
211 int ldb_next_search_free(struct ldb_module *module, struct ldb_message **msg)
216 return module->next->ops->search_free(module->next, msg);
219 int ldb_next_add_record(struct ldb_module *module, const struct ldb_message *message)
224 return module->next->ops->add_record(module->next, message);
227 int ldb_next_modify_record(struct ldb_module *module, const struct ldb_message *message)
232 return module->next->ops->modify_record(module->next, message);
235 int ldb_next_delete_record(struct ldb_module *module, const char *dn)
240 return module->next->ops->delete_record(module->next, dn);
243 int ldb_next_rename_record(struct ldb_module *module, const char *olddn, const char *newdn)
248 return module->next->ops->rename_record(module->next, olddn, newdn);
251 const char *ldb_next_errstring(struct ldb_module *module)
256 return module->next->ops->errstring(module->next);
259 void ldb_next_cache_free(struct ldb_module *module)
264 module->next->ops->cache_free(module->next);