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 "ldb/include/ldb.h"
38 #include "ldb/include/ldb_private.h"
39 #include "dlinklist.h"
40 #include <sys/types.h>
44 #ifdef HAVE_DLOPEN_DISABLED
48 #define LDB_MODULE_PREFIX "modules"
49 #define LDB_MODULE_PREFIX_LEN 7
50 #define LDB_MODULE_SEP ':'
52 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
54 struct ldb_module *current;
58 /* find out which modules we are requested to activate */
65 for (i = 0; options[i] != NULL; i++) {
66 if (strncmp(options[i], LDB_MODULE_PREFIX,
67 LDB_MODULE_PREFIX_LEN) == 0) {
68 p = q = talloc_strdup(ldb, &options[i][LDB_MODULE_PREFIX_LEN]);
77 modules = talloc_realloc(ldb, modules, char *, mnum);
79 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_load_modules()\n");
82 modules[mnum - 1] = q;
83 } while ((p = strchr(q, LDB_MODULE_SEP)));
88 if (!modules && strcmp("ldap", ldb->modules->ops->name)) {
89 /* no modules in the options, look for @MODULES in the
92 const char * const attrs[] = { "@LIST" , NULL};
93 struct ldb_message **msg = NULL;
96 ret = ldb_search(ldb, "", LDB_SCOPE_BASE, "dn=@MODULES", attrs, &msg);
98 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db\n");
101 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb));
105 ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found, bailing out\n");
110 for (j = 0; j < msg[0]->num_elements; j++) {
111 for (k = 0; k < msg[0]->elements[j].num_values; k++) {
113 modules = talloc_realloc(ldb, modules, char *, pn);
115 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in register_modules()\n");
118 modules[pn - 1] = talloc_strndup(modules, msg[0]->elements[j].values[k].data, msg[0]->elements[j].values[k].length);
119 if (!modules[pn - 1]) {
120 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in register_modules()\n");
126 modstr = msg[0]->elements[0].values[0].data;
127 for (c = modstr, mnum = 0; c != NULL; mnum++) {
131 if (*c == '\0') { /* avoid failing if the modules string lasts with ',' */
138 modules = talloc_array(ldb, char *, mnum);
140 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_load_modules()\n");
144 for (p = c = modstr, i = 0; mnum > i; i++) {
149 /* modules are seeked in inverse order. Lets place them as an admin would think the right order is */
150 modules[mnum - i - 1] = talloc_strdup(modules, p);
158 for (i = 0; i < mnum; i++) {
159 #ifdef HAVE_DLOPEN_DISABLED
161 ldb_module_init_function init;
167 if (strcmp(modules[i], "schema") == 0) {
168 current = schema_module_init(ldb, options);
170 ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
173 DLIST_ADD(ldb->modules, current);
177 if (strcmp(modules[i], "timestamps") == 0) {
178 current = timestamps_module_init(ldb, options);
180 ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
183 DLIST_ADD(ldb->modules, current);
187 if (strcmp(modules[i], "samldb") == 0) {
188 current = samldb_module_init(ldb, options);
190 ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
193 DLIST_ADD(ldb->modules, current);
197 #ifdef HAVE_DLOPEN_DISABLED
198 filename = talloc_asprintf(ldb, "%s.so", modules[i]);
200 ldb_debug(ldb, LDB_DEBUG_FATAL, "Talloc failed!\n");
204 if (stat(filename, &st) < 0) {
205 ldb_debug(ldb, LDB_DEBUG_FATAL, "Required module [%s] not found, bailing out!\n", modules[i]);
209 handle = dlopen(filename, RTLD_LAZY);
212 ldb_debug(ldb, LDB_DEBUG_FATAL, "Error loading module %s [%s]\n", modules[i], dlerror());
216 init = (ldb_module_init_function)dlsym(handle, "init_module");
220 ldb_debug(ldb, LDB_DEBUG_FATAL, "Error trying to resolve symbol 'init_module' in %s [%s]\n", modules[i], errstr);
224 current = init(ldb, options);
226 ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
229 DLIST_ADD(ldb->modules, current);
231 ldb_debug(ldb, LDB_DEBUG_FATAL, "Required module [%s] not found, bailing out!\n", modules[i]);
241 helper functions to call the next module in chain
244 int ldb_next_search(struct ldb_module *module,
246 enum ldb_scope scope,
247 const char *expression,
248 const char * const *attrs, struct ldb_message ***res)
253 return module->next->ops->search(module->next, base, scope, expression, attrs, res);
256 int ldb_next_search_free(struct ldb_module *module, struct ldb_message **msg)
261 return module->next->ops->search_free(module->next, msg);
264 int ldb_next_add_record(struct ldb_module *module, const struct ldb_message *message)
269 return module->next->ops->add_record(module->next, message);
272 int ldb_next_modify_record(struct ldb_module *module, const struct ldb_message *message)
277 return module->next->ops->modify_record(module->next, message);
280 int ldb_next_delete_record(struct ldb_module *module, const char *dn)
285 return module->next->ops->delete_record(module->next, dn);
288 int ldb_next_rename_record(struct ldb_module *module, const char *olddn, const char *newdn)
293 return module->next->ops->rename_record(module->next, olddn, newdn);
296 int ldb_next_named_lock(struct ldb_module *module, const char *lockname)
301 return module->next->ops->named_lock(module->next, lockname);
304 int ldb_next_named_unlock(struct ldb_module *module, const char *lockname)
309 return module->next->ops->named_unlock(module->next, lockname);
312 const char *ldb_next_errstring(struct ldb_module *module)
317 return module->next->ops->errstring(module->next);