4 Copyright (C) Simo Sorce 2004
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 3 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, see <http://www.gnu.org/licenses/>.
27 * Component: ldb modules core
29 * Description: core modules routines
34 #include "ldb_includes.h"
36 #if (_SAMBA_BUILD_ >= 4)
39 #include "dynconfig.h"
42 #define LDB_MODULE_PREFIX "modules:"
43 #define LDB_MODULE_PREFIX_LEN 8
45 static char *ldb_modules_strdup_no_spaces(TALLOC_CTX *mem_ctx, const char *string)
50 trimmed = talloc_strdup(mem_ctx, string);
55 len = strlen(trimmed);
56 for (i = 0; trimmed[i] != '\0'; i++) {
61 memmove(&trimmed[i], &trimmed[i + 1], len -i -1);
70 /* modules are called in inverse order on the stack.
71 Lets place them as an admin would think the right order is.
72 Modules order is important */
73 const char **ldb_modules_list_from_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *string)
75 char **modules = NULL;
80 /* spaces not admitted */
81 modstr = ldb_modules_strdup_no_spaces(mem_ctx, string);
83 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_strdup_no_spaces()\n");
87 modules = talloc_realloc(mem_ctx, modules, char *, 2);
89 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
93 talloc_steal(modules, modstr);
96 /* The str*r*chr walks backwards: This is how we get the inverse order mentioned above */
97 while ((p = strrchr(modstr, ',')) != NULL) {
103 modules = talloc_realloc(mem_ctx, modules, char *, i + 2);
105 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
112 modules[i + 1] = NULL;
114 m = (const char **)modules;
119 static struct ops_list_entry {
120 const struct ldb_module_ops *ops;
121 struct ops_list_entry *next;
122 } *registered_modules = NULL;
124 static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
126 struct ops_list_entry *e;
128 for (e = registered_modules; e; e = e->next) {
129 if (strcmp(e->ops->name, name) == 0)
136 #ifndef STATIC_LIBLDB_MODULES
139 #define LDAP_INIT ldb_ldap_init,
144 #ifdef HAVE_LDB_SQLITE3
145 #define SQLITE3_INIT ldb_sqlite3_init,
150 #define STATIC_LIBLDB_MODULES \
155 ldb_operational_init, \
157 ldb_paged_results_init, \
164 int ldb_global_init(void)
166 static int (*static_init_fns[])(void) = STATIC_LIBLDB_MODULES;
168 static int initialized = 0;
176 for (i = 0; static_init_fns[i]; i++) {
177 if (static_init_fns[i]() == -1)
184 int ldb_register_module(const struct ldb_module_ops *ops)
186 struct ops_list_entry *entry = talloc(talloc_autofree_context(), struct ops_list_entry);
188 if (ldb_find_module_ops(ops->name) != NULL)
195 entry->next = registered_modules;
196 registered_modules = entry;
201 int ldb_try_load_dso(struct ldb_context *ldb, const char *name)
205 int (*init_fn) (void);
208 if (getenv("LD_LDB_MODULE_PATH") != NULL) {
209 modulesdir = talloc_strdup(ldb, getenv("LD_LDB_MODULE_PATH"));
212 modulesdir = talloc_asprintf(ldb, "%s/ldb", dyn_MODULESDIR);
214 modulesdir = talloc_strdup(ldb, MODULESDIR);
218 path = talloc_asprintf(ldb, "%s/%s.%s", modulesdir, name, SHLIBEXT);
220 talloc_free(modulesdir);
222 ldb_debug(ldb, LDB_DEBUG_TRACE, "trying to load %s from %s\n", name, path);
224 handle = dlopen(path, RTLD_NOW);
225 if (handle == NULL) {
226 ldb_debug(ldb, LDB_DEBUG_WARNING, "unable to load %s from %s: %s\n", name, path, dlerror());
230 init_fn = (int (*)(void))dlsym(handle, "init_module");
232 if (init_fn == NULL) {
233 ldb_debug(ldb, LDB_DEBUG_ERROR, "no symbol `init_module' found in %s: %s\n", path, dlerror());
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;
270 talloc_set_name(current, "ldb_module: %s", module_list[i]);
275 DLIST_ADD(module, current);
281 int ldb_init_module_chain(struct ldb_context *ldb, struct ldb_module *module)
283 while (module && module->ops->init_context == NULL)
284 module = module->next;
286 if (module && module->ops->init_context &&
287 module->ops->init_context(module) != LDB_SUCCESS) {
288 ldb_debug(ldb, LDB_DEBUG_FATAL, "module initialization failed\n");
289 return LDB_ERR_OPERATIONS_ERROR;
295 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
297 const char **modules = NULL;
300 TALLOC_CTX *mem_ctx = talloc_new(ldb);
302 return LDB_ERR_OPERATIONS_ERROR;
305 /* find out which modules we are requested to activate */
307 /* check if we have a custom module list passd as ldb option */
309 for (i = 0; options[i] != NULL; i++) {
310 if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
311 modules = ldb_modules_list_from_string(ldb, mem_ctx, &options[i][LDB_MODULE_PREFIX_LEN]);
316 /* if not overloaded by options and the backend is not ldap try to load the modules list from ldb */
317 if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) {
318 const char * const attrs[] = { "@LIST" , NULL};
319 struct ldb_result *res = NULL;
320 struct ldb_dn *mods_dn;
322 mods_dn = ldb_dn_new(mem_ctx, ldb, "@MODULES");
323 if (mods_dn == NULL) {
324 talloc_free(mem_ctx);
328 ret = ldb_search(ldb, mods_dn, LDB_SCOPE_BASE, "", attrs, &res);
329 talloc_steal(mods_dn, res);
330 if (ret == LDB_SUCCESS && (res->count == 0 || res->msgs[0]->num_elements == 0)) {
331 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db\n");
333 if (ret != LDB_SUCCESS) {
334 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb));
335 talloc_free(mem_ctx);
338 if (res->count > 1) {
339 ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found (%d), bailing out\n", res->count);
340 talloc_free(mem_ctx);
344 modules = ldb_modules_list_from_string(ldb, mem_ctx,
345 (const char *)res->msgs[0]->elements[0].values[0].data);
349 talloc_free(mods_dn);
352 if (modules != NULL) {
353 ret = ldb_load_modules_list(ldb, modules, ldb->modules, &ldb->modules);
354 talloc_free(modules);
355 if (ret != LDB_SUCCESS) {
359 ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database\n");
362 return ldb_init_module_chain(ldb, ldb->modules);
366 by using this we allow ldb modules to only implement the functions they care about,
367 which makes writing a module simpler, and makes it more likely to keep working
370 #define FIND_OP(module, op) do { \
371 struct ldb_context *ldb = module->ldb; \
372 module = module->next; \
373 while (module && module->ops->op == NULL) module = module->next; \
374 if (module == NULL) { \
375 ldb_asprintf_errstring(ldb, "Unable to find backend operation for " #op ); \
376 return LDB_ERR_OPERATIONS_ERROR; \
382 helper functions to call the next module in chain
385 int ldb_next_request(struct ldb_module *module, struct ldb_request *request)
388 switch (request->operation) {
390 FIND_OP(module, search);
391 ret = module->ops->search(module, request);
394 FIND_OP(module, add);
395 ret = module->ops->add(module, request);
398 FIND_OP(module, modify);
399 ret = module->ops->modify(module, request);
402 FIND_OP(module, del);
403 ret = module->ops->del(module, request);
406 FIND_OP(module, rename);
407 ret = module->ops->rename(module, request);
410 FIND_OP(module, extended);
411 ret = module->ops->extended(module, request);
413 case LDB_SEQUENCE_NUMBER:
414 FIND_OP(module, sequence_number);
415 ret = module->ops->sequence_number(module, request);
418 FIND_OP(module, request);
419 ret = module->ops->request(module, request);
422 if (ret == LDB_SUCCESS) {
425 if (!ldb_errstring(module->ldb)) {
426 /* Set a default error string, to place the blame somewhere */
427 ldb_asprintf_errstring(module->ldb, "error in module %s: %s (%d)", module->ops->name, ldb_strerror(ret), ret);
432 int ldb_next_init(struct ldb_module *module)
434 /* init is different in that it is not an error if modules
435 * do not require initialization */
437 module = module->next;
439 while (module && module->ops->init_context == NULL)
440 module = module->next;
445 return module->ops->init_context(module);
448 int ldb_next_start_trans(struct ldb_module *module)
450 FIND_OP(module, start_transaction);
451 return module->ops->start_transaction(module);
454 int ldb_next_end_trans(struct ldb_module *module)
456 FIND_OP(module, end_transaction);
457 return module->ops->end_transaction(module);
460 int ldb_next_del_trans(struct ldb_module *module)
462 FIND_OP(module, del_transaction);
463 return module->ops->del_transaction(module);