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)
41 #define LDB_MODULE_PREFIX "modules:"
42 #define LDB_MODULE_PREFIX_LEN 8
44 void ldb_set_modules_dir(struct ldb_context *ldb, const char *path)
46 talloc_free(ldb->modules_dir);
47 ldb->modules_dir = talloc_strdup(ldb, path);
50 static char *ldb_modules_strdup_no_spaces(TALLOC_CTX *mem_ctx, const char *string)
55 trimmed = talloc_strdup(mem_ctx, string);
60 len = strlen(trimmed);
61 for (i = 0; trimmed[i] != '\0'; i++) {
66 memmove(&trimmed[i], &trimmed[i + 1], len -i -1);
75 /* modules are called in inverse order on the stack.
76 Lets place them as an admin would think the right order is.
77 Modules order is important */
78 const char **ldb_modules_list_from_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *string)
80 char **modules = NULL;
85 /* spaces not admitted */
86 modstr = ldb_modules_strdup_no_spaces(mem_ctx, string);
88 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_strdup_no_spaces()\n");
92 modules = talloc_realloc(mem_ctx, modules, char *, 2);
94 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
98 talloc_steal(modules, modstr);
101 /* The str*r*chr walks backwards: This is how we get the inverse order mentioned above */
102 while ((p = strrchr(modstr, ',')) != NULL) {
108 modules = talloc_realloc(mem_ctx, modules, char *, i + 2);
110 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
117 modules[i + 1] = NULL;
119 m = (const char **)modules;
124 static struct ops_list_entry {
125 const struct ldb_module_ops *ops;
126 struct ops_list_entry *next;
127 } *registered_modules = NULL;
129 #define LDB_MODULE(name) (&ldb_ ## name ## _module_ops)
131 #ifndef STATIC_LIBLDB_MODULES
133 #define STATIC_LIBLDB_MODULES \
134 LDB_MODULE(operational), \
135 LDB_MODULE(rdn_name), \
136 LDB_MODULE(paged_results), \
137 LDB_MODULE(server_sort), \
142 const static struct ldb_module_ops *builtin_modules[] = {
143 STATIC_LIBLDB_MODULES
146 static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
148 struct ops_list_entry *e;
151 for (i = 0; builtin_modules[i]; i++) {
152 if (strcmp(builtin_modules[i]->name, name) == 0)
153 return builtin_modules[i];
156 for (e = registered_modules; e; e = e->next) {
157 if (strcmp(e->ops->name, name) == 0)
165 int ldb_register_module(const struct ldb_module_ops *ops)
167 struct ops_list_entry *entry = talloc(talloc_autofree_context(), struct ops_list_entry);
169 if (ldb_find_module_ops(ops->name) != NULL)
176 entry->next = registered_modules;
177 registered_modules = entry;
182 void *ldb_dso_load_symbol(struct ldb_context *ldb, const char *name,
189 if (ldb->modules_dir == NULL)
192 path = talloc_asprintf(ldb, "%s/%s.%s", ldb->modules_dir, name,
195 ldb_debug(ldb, LDB_DEBUG_TRACE, "trying to load %s from %s\n", name, path);
197 handle = dlopen(path, RTLD_NOW);
198 if (handle == NULL) {
199 ldb_debug(ldb, LDB_DEBUG_WARNING, "unable to load %s from %s: %s\n", name, path, dlerror());
203 sym = (int (*)(void))dlsym(handle, symbol);
206 ldb_debug(ldb, LDB_DEBUG_ERROR, "no symbol `%s' found in %s: %s\n", symbol, path, dlerror());
215 int ldb_load_modules_list(struct ldb_context *ldb, const char **module_list, struct ldb_module *backend, struct ldb_module **out)
217 struct ldb_module *module;
222 for (i = 0; module_list[i] != NULL; i++) {
223 struct ldb_module *current;
224 const struct ldb_module_ops *ops;
226 ops = ldb_find_module_ops(module_list[i]);
228 int (*init_fn) (void);
230 init_fn = ldb_dso_load_symbol(ldb, module_list[i],
232 if (init_fn != NULL && init_fn() == 0) {
233 ops = ldb_find_module_ops(module_list[i]);
238 char *symbol_name = talloc_asprintf(ldb, "ldb_%s_module_ops",
240 if (symbol_name == NULL) {
241 return LDB_ERR_OPERATIONS_ERROR;
243 ops = ldb_dso_load_symbol(ldb, module_list[i], symbol_name);
244 talloc_free(symbol_name);
248 ldb_debug(ldb, LDB_DEBUG_WARNING, "WARNING: Module [%s] not found\n",
253 current = talloc_zero(ldb, struct ldb_module);
254 if (current == NULL) {
255 return LDB_ERR_OPERATIONS_ERROR;
257 talloc_set_name(current, "ldb_module: %s", module_list[i]);
262 DLIST_ADD(module, current);
268 int ldb_init_module_chain(struct ldb_context *ldb, struct ldb_module *module)
270 while (module && module->ops->init_context == NULL)
271 module = module->next;
273 /* init is different in that it is not an error if modules
274 * do not require initialization */
277 int ret = module->ops->init_context(module);
278 if (ret != LDB_SUCCESS) {
279 ldb_debug(ldb, LDB_DEBUG_FATAL, "module %s initialization failed\n", module->ops->name);
287 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
289 const char **modules = NULL;
292 TALLOC_CTX *mem_ctx = talloc_new(ldb);
294 return LDB_ERR_OPERATIONS_ERROR;
297 /* find out which modules we are requested to activate */
299 /* check if we have a custom module list passd as ldb option */
301 for (i = 0; options[i] != NULL; i++) {
302 if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
303 modules = ldb_modules_list_from_string(ldb, mem_ctx, &options[i][LDB_MODULE_PREFIX_LEN]);
308 /* if not overloaded by options and the backend is not ldap try to load the modules list from ldb */
309 if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) {
310 const char * const attrs[] = { "@LIST" , NULL};
311 struct ldb_result *res = NULL;
312 struct ldb_dn *mods_dn;
314 mods_dn = ldb_dn_new(mem_ctx, ldb, "@MODULES");
315 if (mods_dn == NULL) {
316 talloc_free(mem_ctx);
320 ret = ldb_search_exp_fmt(ldb, mods_dn, &res, mods_dn, LDB_SCOPE_BASE, attrs, "@LIST=*");
322 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
323 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
324 } else if (ret != LDB_SUCCESS) {
325 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb));
326 talloc_free(mem_ctx);
329 const char *module_list;
330 if (res->count == 0) {
331 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
332 } else if (res->count > 1) {
333 ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found (%d), bailing out\n", res->count);
334 talloc_free(mem_ctx);
337 module_list = ldb_msg_find_attr_as_string(res->msgs[0], "@LIST", NULL);
339 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
341 modules = ldb_modules_list_from_string(ldb, mem_ctx,
346 talloc_free(mods_dn);
349 if (modules != NULL) {
350 ret = ldb_load_modules_list(ldb, modules, ldb->modules, &ldb->modules);
351 if (ret != LDB_SUCCESS) {
352 talloc_free(mem_ctx);
356 ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database");
359 ret = ldb_init_module_chain(ldb, ldb->modules);
360 talloc_free(mem_ctx);
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)
387 switch (request->operation) {
389 FIND_OP(module, search);
390 ret = module->ops->search(module, request);
393 FIND_OP(module, add);
394 ret = module->ops->add(module, request);
397 FIND_OP(module, modify);
398 ret = module->ops->modify(module, request);
401 FIND_OP(module, del);
402 ret = module->ops->del(module, request);
405 FIND_OP(module, rename);
406 ret = module->ops->rename(module, request);
409 FIND_OP(module, extended);
410 ret = module->ops->extended(module, request);
412 case LDB_SEQUENCE_NUMBER:
413 FIND_OP(module, sequence_number);
414 ret = module->ops->sequence_number(module, request);
417 FIND_OP(module, request);
418 ret = module->ops->request(module, request);
421 if (ret == LDB_SUCCESS) {
424 if (!ldb_errstring(module->ldb)) {
425 /* Set a default error string, to place the blame somewhere */
426 ldb_asprintf_errstring(module->ldb, "error in module %s: %s (%d)", module->ops->name, ldb_strerror(ret), ret);
431 int ldb_next_init(struct ldb_module *module)
433 module = module->next;
435 return ldb_init_module_chain(module->ldb, module);
438 int ldb_next_start_trans(struct ldb_module *module)
440 FIND_OP(module, start_transaction);
441 return module->ops->start_transaction(module);
444 int ldb_next_end_trans(struct ldb_module *module)
446 FIND_OP(module, end_transaction);
447 return module->ops->end_transaction(module);
450 int ldb_next_del_trans(struct ldb_module *module)
452 FIND_OP(module, del_transaction);
453 return module->ops->del_transaction(module);