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 3 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, see <http://www.gnu.org/licenses/>.
28 * Component: ldb modules core
30 * Description: core modules routines
36 #include "ldb/include/includes.h"
38 #if (_SAMBA_BUILD_ >= 4)
40 #include "dynconfig.h"
43 #define LDB_MODULE_PREFIX "modules:"
44 #define LDB_MODULE_PREFIX_LEN 8
46 static char *ldb_modules_strdup_no_spaces(TALLOC_CTX *mem_ctx, const char *string)
51 trimmed = talloc_strdup(mem_ctx, string);
56 len = strlen(trimmed);
57 for (i = 0; trimmed[i] != '\0'; i++) {
62 memmove(&trimmed[i], &trimmed[i + 1], len -i -1);
71 /* modules are called in inverse order on the stack.
72 Lets place them as an admin would think the right order is.
73 Modules order is important */
74 const char **ldb_modules_list_from_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *string)
76 char **modules = NULL;
81 /* spaces not admitted */
82 modstr = ldb_modules_strdup_no_spaces(mem_ctx, string);
84 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_strdup_no_spaces()\n");
88 modules = talloc_realloc(mem_ctx, modules, char *, 2);
90 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
94 talloc_steal(modules, modstr);
97 /* The str*r*chr walks backwards: This is how we get the inverse order mentioned above */
98 while ((p = strrchr(modstr, ',')) != NULL) {
104 modules = talloc_realloc(mem_ctx, modules, char *, i + 2);
106 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
113 modules[i + 1] = NULL;
115 m = (const char **)modules;
120 static struct ops_list_entry {
121 const struct ldb_module_ops *ops;
122 struct ops_list_entry *next;
123 } *registered_modules = NULL;
125 static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
127 struct ops_list_entry *e;
129 for (e = registered_modules; e; e = e->next) {
130 if (strcmp(e->ops->name, name) == 0)
137 #ifndef STATIC_ldb_MODULES
140 #define LDAP_INIT ldb_ldap_init,
145 #ifdef HAVE_LDB_SQLITE3
146 #define SQLITE3_INIT ldb_sqlite3_init,
151 #define STATIC_ldb_MODULES \
156 ldb_operational_init, \
158 ldb_objectclass_init, \
159 ldb_paged_results_init, \
166 int ldb_global_init(void)
168 static int (*static_init_fns[])(void) = STATIC_ldb_MODULES;
170 static int initialized = 0;
178 for (i = 0; static_init_fns[i]; i++) {
179 if (static_init_fns[i]() == -1)
186 int ldb_register_module(const struct ldb_module_ops *ops)
188 struct ops_list_entry *entry = talloc(talloc_autofree_context(), struct ops_list_entry);
190 if (ldb_find_module_ops(ops->name) != NULL)
197 entry->next = registered_modules;
198 registered_modules = entry;
203 int ldb_try_load_dso(struct ldb_context *ldb, const char *name)
207 int (*init_fn) (void);
212 if (getenv("LD_LDB_MODULE_PATH") != NULL) {
213 modulesdir = talloc_strdup(ldb, getenv("LD_LDB_MODULE_PATH"));
216 modulesdir = talloc_asprintf(ldb, "%s/ldb", get_dyn_LIBDIR());
218 modulesdir = talloc_strdup(ldb, MODULESDIR);
222 path = talloc_asprintf(ldb, "%s/%s.%s", modulesdir, name, SHLIBEXT);
224 talloc_free(modulesdir);
226 ldb_debug(ldb, LDB_DEBUG_TRACE, "trying to load %s from %s\n", name, path);
228 handle = dlopen(path, RTLD_NOW);
229 if (handle == NULL) {
230 ldb_debug(ldb, LDB_DEBUG_WARNING, "unable to load %s from %s: %s\n", name, path, dlerror());
234 init_fn = (int (*)(void))dlsym(handle, "init_samba_module");
236 if (init_fn == NULL) {
237 ldb_debug(ldb, LDB_DEBUG_ERROR, "no symbol "
238 "`init_samba_module' found in %s: %s\n", path,
252 ldb_debug(ldb, LDB_DEBUG_TRACE, "no dlopen() - not trying to load %s module\n", name);
257 int ldb_load_modules_list(struct ldb_context *ldb, const char **module_list, struct ldb_module *backend, struct ldb_module **out)
259 struct ldb_module *module;
264 for (i = 0; module_list[i] != NULL; i++) {
265 struct ldb_module *current;
266 const struct ldb_module_ops *ops;
268 ops = ldb_find_module_ops(module_list[i]);
270 if (ldb_try_load_dso(ldb, module_list[i]) == 0) {
271 ops = ldb_find_module_ops(module_list[i]);
276 ldb_debug(ldb, LDB_DEBUG_WARNING, "WARNING: Module [%s] not found\n",
281 current = talloc_zero(ldb, struct ldb_module);
282 if (current == NULL) {
283 return LDB_ERR_OPERATIONS_ERROR;
285 talloc_set_name(current, "ldb_module: %s", module_list[i]);
290 DLIST_ADD(module, current);
296 int ldb_init_module_chain(struct ldb_context *ldb, struct ldb_module *module)
298 while (module && module->ops->init_context == NULL)
299 module = module->next;
301 if (module && module->ops->init_context &&
302 module->ops->init_context(module) != LDB_SUCCESS) {
303 ldb_debug(ldb, LDB_DEBUG_FATAL, "module initialization failed\n");
304 return LDB_ERR_OPERATIONS_ERROR;
310 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
312 const char **modules = NULL;
315 TALLOC_CTX *mem_ctx = talloc_new(ldb);
317 return LDB_ERR_OPERATIONS_ERROR;
320 /* find out which modules we are requested to activate */
322 /* check if we have a custom module list passd as ldb option */
324 for (i = 0; options[i] != NULL; i++) {
325 if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
326 modules = ldb_modules_list_from_string(ldb, mem_ctx, &options[i][LDB_MODULE_PREFIX_LEN]);
331 /* if not overloaded by options and the backend is not ldap try to load the modules list from ldb */
332 if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) {
333 const char * const attrs[] = { "@LIST" , NULL};
334 struct ldb_result *res = NULL;
335 struct ldb_dn *mods_dn;
337 mods_dn = ldb_dn_explode(mem_ctx, "@MODULES");
338 if (mods_dn == NULL) {
339 talloc_free(mem_ctx);
343 ret = ldb_search(ldb, ldb, &res, mods_dn, LDB_SCOPE_BASE, attrs, "");
344 talloc_steal(mods_dn, res);
345 if (ret == LDB_SUCCESS && (res->count == 0 || res->msgs[0]->num_elements == 0)) {
346 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db\n");
348 if (ret != LDB_SUCCESS) {
349 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb));
350 talloc_free(mem_ctx);
353 if (res->count > 1) {
354 ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found (%d), bailing out\n", res->count);
355 talloc_free(mem_ctx);
359 modules = ldb_modules_list_from_string(ldb, mem_ctx,
360 (const char *)res->msgs[0]->elements[0].values[0].data);
364 talloc_free(mods_dn);
367 if (modules != NULL) {
368 ret = ldb_load_modules_list(ldb, modules, ldb->modules, &ldb->modules);
369 talloc_free(modules);
370 if (ret != LDB_SUCCESS) {
374 ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database\n");
377 return ldb_init_module_chain(ldb, ldb->modules);
381 by using this we allow ldb modules to only implement the functions they care about,
382 which makes writing a module simpler, and makes it more likely to keep working
385 #define FIND_OP(module, op) do { \
386 struct ldb_context *ldb = module->ldb; \
387 module = module->next; \
388 while (module && module->ops->op == NULL) module = module->next; \
389 if (module == NULL) { \
390 ldb_asprintf_errstring(ldb, "Unable to find backend operation for " #op ); \
391 return LDB_ERR_OPERATIONS_ERROR; \
397 helper functions to call the next module in chain
400 int ldb_next_request(struct ldb_module *module, struct ldb_request *request)
402 switch (request->operation) {
404 FIND_OP(module, search);
405 return module->ops->search(module, request);
407 FIND_OP(module, add);
408 return module->ops->add(module, request);
410 FIND_OP(module, modify);
411 return module->ops->modify(module, request);
413 FIND_OP(module, del);
414 return module->ops->del(module, request);
416 FIND_OP(module, rename);
417 return module->ops->rename(module, request);
418 case LDB_SEQUENCE_NUMBER:
419 FIND_OP(module, sequence_number);
420 return module->ops->sequence_number(module, request);
422 FIND_OP(module, request);
423 return module->ops->request(module, request);
427 int ldb_next_init(struct ldb_module *module)
429 /* init is different in that it is not an error if modules
430 * do not require initialization */
432 module = module->next;
434 while (module && module->ops->init_context == NULL)
435 module = module->next;
440 return module->ops->init_context(module);
443 int ldb_next_start_trans(struct ldb_module *module)
445 FIND_OP(module, start_transaction);
446 return module->ops->start_transaction(module);
449 int ldb_next_end_trans(struct ldb_module *module)
451 FIND_OP(module, end_transaction);
452 return module->ops->end_transaction(module);
455 int ldb_next_del_trans(struct ldb_module *module)
457 FIND_OP(module, del_transaction);
458 return module->ops->del_transaction(module);