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/includes.h"
39 #if (_SAMBA_BUILD_ >= 4)
41 #include "dynconfig.h"
44 #define LDB_MODULE_PREFIX "modules:"
45 #define LDB_MODULE_PREFIX_LEN 8
47 static char *talloc_strdup_no_spaces(struct ldb_context *ldb, const char *string)
52 trimmed = talloc_strdup(ldb, string);
54 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in talloc_strdup_trim_spaces()\n");
58 len = strlen(trimmed);
59 for (i = 0; trimmed[i] != '\0'; i++) {
64 memmove(&trimmed[i], &trimmed[i + 1], len -i -1);
73 /* modules are called in inverse order on the stack.
74 Lets place them as an admin would think the right order is.
75 Modules order is important */
76 const char **ldb_modules_list_from_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *string)
78 char **modules = NULL;
83 /* spaces not admitted */
84 modstr = talloc_strdup_no_spaces((struct ldb_context *)mem_ctx,
90 modules = talloc_realloc(mem_ctx, modules, char *, 2);
92 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
96 talloc_steal(modules, modstr);
99 /* The str*r*chr walks backwards: This is how we get the inverse order mentioned above */
100 while ((p = strrchr(modstr, ',')) != NULL) {
106 modules = talloc_realloc(mem_ctx, modules, char *, i + 2);
108 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
115 modules[i + 1] = NULL;
117 m = (const char **)modules;
122 static struct ops_list_entry {
123 const struct ldb_module_ops *ops;
124 struct ops_list_entry *next;
125 } *registered_modules = NULL;
127 static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
129 struct ops_list_entry *e;
131 for (e = registered_modules; e; e = e->next) {
132 if (strcmp(e->ops->name, name) == 0)
139 #ifndef STATIC_ldb_MODULES
142 #define LDAP_INIT ldb_ldap_init,
147 #ifdef HAVE_LDB_SQLITE3
148 #define SQLITE3_INIT ldb_sqlite3_init,
153 #define STATIC_ldb_MODULES \
159 ldb_operational_init, \
161 ldb_objectclass_init, \
162 ldb_paged_results_init, \
168 int ldb_global_init(void)
170 static int (*static_init_fns[])(void) = STATIC_ldb_MODULES;
172 static int initialized = 0;
180 for (i = 0; static_init_fns[i]; i++) {
181 if (static_init_fns[i]() == -1)
188 int ldb_register_module(const struct ldb_module_ops *ops)
190 struct ops_list_entry *entry = talloc(talloc_autofree_context(), struct ops_list_entry);
192 if (ldb_find_module_ops(ops->name) != NULL)
199 entry->next = registered_modules;
200 registered_modules = entry;
205 int ldb_try_load_dso(struct ldb_context *ldb, const char *name)
209 int (*init_fn) (void);
213 path = talloc_asprintf(ldb, "%s/ldb/%s.%s", dyn_MODULESDIR, name, dyn_SHLIBEXT);
215 path = talloc_asprintf(ldb, "%s/%s.%s", MODULESDIR, name, SHLIBEXT);
218 ldb_debug(ldb, LDB_DEBUG_TRACE, "trying to load %s from %s\n", name, path);
220 handle = dlopen(path, 0);
221 if (handle == NULL) {
222 ldb_debug(ldb, LDB_DEBUG_WARNING, "unable to load %s from %s: %s\n", name, path, dlerror());
226 init_fn = (int (*)(void))dlsym(handle, "init_module");
228 if (init_fn == NULL) {
229 ldb_debug(ldb, LDB_DEBUG_ERROR, "no symbol `init_module' found in %s: %s\n", path, dlerror());
237 ldb_debug(ldb, LDB_DEBUG_TRACE, "no dlopen() - not trying to load %s module\n", name);
242 int ldb_load_modules_list(struct ldb_context *ldb, const char **module_list, struct ldb_module *backend, struct ldb_module **out)
244 struct ldb_module *module;
249 for (i = 0; module_list[i] != NULL; i++) {
250 struct ldb_module *current;
251 const struct ldb_module_ops *ops;
253 ops = ldb_find_module_ops(module_list[i]);
255 if (ldb_try_load_dso(ldb, module_list[i]) == 0) {
256 ops = ldb_find_module_ops(module_list[i]);
261 ldb_debug(ldb, LDB_DEBUG_WARNING, "WARNING: Module [%s] not found\n",
266 current = talloc_zero(ldb, struct ldb_module);
267 if (current == NULL) {
268 return LDB_ERR_OPERATIONS_ERROR;
274 DLIST_ADD(module, current);
280 int ldb_init_module_chain(struct ldb_context *ldb, struct ldb_module *module)
282 while (module && module->ops->init_context == NULL)
283 module = module->next;
285 if (module && module->ops->init_context &&
286 module->ops->init_context(module) != LDB_SUCCESS) {
287 ldb_debug(ldb, LDB_DEBUG_FATAL, "module initialization failed\n");
288 return LDB_ERR_OPERATIONS_ERROR;
294 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
296 const char **modules = NULL;
299 TALLOC_CTX *mem_ctx = talloc_new(ldb);
301 return LDB_ERR_OPERATIONS_ERROR;
304 /* find out which modules we are requested to activate */
306 /* check if we have a custom module list passd as ldb option */
308 for (i = 0; options[i] != NULL; i++) {
309 if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
310 modules = ldb_modules_list_from_string(ldb, mem_ctx, &options[i][LDB_MODULE_PREFIX_LEN]);
315 /* if not overloaded by options and the backend is not ldap try to load the modules list from ldb */
316 if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) {
317 const char * const attrs[] = { "@LIST" , NULL};
318 struct ldb_result *res = NULL;
319 struct ldb_dn *mods_dn;
321 mods_dn = ldb_dn_explode(mem_ctx, "@MODULES");
322 if (mods_dn == NULL) {
323 talloc_free(mem_ctx);
327 ret = ldb_search(ldb, mods_dn, LDB_SCOPE_BASE, "", attrs, &res);
328 talloc_steal(mods_dn, res);
329 if (ret == LDB_SUCCESS && (res->count == 0 || res->msgs[0]->num_elements == 0)) {
330 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db\n");
332 if (ret != LDB_SUCCESS) {
333 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb));
334 talloc_free(mem_ctx);
337 if (res->count > 1) {
338 ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found (%d), bailing out\n", res->count);
339 talloc_free(mem_ctx);
343 modules = ldb_modules_list_from_string(ldb, mem_ctx,
344 (const char *)res->msgs[0]->elements[0].values[0].data);
348 talloc_free(mods_dn);
351 if (modules != NULL) {
352 ret = ldb_load_modules_list(ldb, modules, ldb->modules, &ldb->modules);
353 talloc_free(modules);
354 if (ret != LDB_SUCCESS) {
358 ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database\n");
361 return ldb_init_module_chain(ldb, ldb->modules);
365 by using this we allow ldb modules to only implement the functions they care about,
366 which makes writing a module simpler, and makes it more likely to keep working
369 #define FIND_OP(module, op) do { \
370 struct ldb_context *ldb = module->ldb; \
371 module = module->next; \
372 while (module && module->ops->op == NULL) module = module->next; \
373 if (module == NULL) { \
374 ldb_asprintf_errstring(ldb, "Unable to find backend operation for " #op ); \
375 return LDB_ERR_OPERATIONS_ERROR; \
381 helper functions to call the next module in chain
384 int ldb_next_request(struct ldb_module *module, struct ldb_request *request)
386 switch (request->operation) {
388 FIND_OP(module, search);
389 return module->ops->search(module, request);
391 FIND_OP(module, add);
392 return module->ops->add(module, request);
394 FIND_OP(module, modify);
395 return module->ops->modify(module, request);
397 FIND_OP(module, del);
398 return module->ops->del(module, request);
400 FIND_OP(module, rename);
401 return module->ops->rename(module, request);
402 case LDB_SEQUENCE_NUMBER:
403 FIND_OP(module, sequence_number);
404 return module->ops->sequence_number(module, request);
406 FIND_OP(module, request);
407 return module->ops->request(module, request);
411 int ldb_next_init(struct ldb_module *module)
413 /* init is different in that it is not an error if modules
414 * do not require initialization */
416 module = module->next;
418 while (module && module->ops->init_context == NULL)
419 module = module->next;
424 return module->ops->init_context(module);
427 int ldb_next_start_trans(struct ldb_module *module)
429 FIND_OP(module, start_transaction);
430 return module->ops->start_transaction(module);
433 int ldb_next_end_trans(struct ldb_module *module)
435 FIND_OP(module, end_transaction);
436 return module->ops->end_transaction(module);
439 int ldb_next_del_trans(struct ldb_module *module)
441 FIND_OP(module, del_transaction);
442 return module->ops->del_transaction(module);