4 Copyright (C) Simo Sorce 2004-2008
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)
40 #define LDB_MODULE_PREFIX "modules:"
41 #define LDB_MODULE_PREFIX_LEN 8
43 static void *ldb_dso_load_symbol(struct ldb_context *ldb, const char *name,
46 void ldb_set_modules_dir(struct ldb_context *ldb, const char *path)
48 talloc_free(ldb->modules_dir);
49 ldb->modules_dir = talloc_strdup(ldb, path);
52 static char *ldb_modules_strdup_no_spaces(TALLOC_CTX *mem_ctx, const char *string)
57 trimmed = talloc_strdup(mem_ctx, string);
62 len = strlen(trimmed);
63 for (i = 0; trimmed[i] != '\0'; i++) {
68 memmove(&trimmed[i], &trimmed[i + 1], len -i -1);
77 /* modules are called in inverse order on the stack.
78 Lets place them as an admin would think the right order is.
79 Modules order is important */
80 const char **ldb_modules_list_from_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *string)
82 char **modules = NULL;
87 /* spaces not admitted */
88 modstr = ldb_modules_strdup_no_spaces(mem_ctx, string);
90 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_strdup_no_spaces()\n");
94 modules = talloc_realloc(mem_ctx, modules, char *, 2);
96 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
100 talloc_steal(modules, modstr);
103 /* The str*r*chr walks backwards: This is how we get the inverse order mentioned above */
104 while ((p = strrchr(modstr, ',')) != NULL) {
110 modules = talloc_realloc(mem_ctx, modules, char *, i + 2);
112 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
119 modules[i + 1] = NULL;
121 m = (const char **)modules;
126 static struct backends_list_entry {
127 struct ldb_backend_ops *ops;
128 struct backends_list_entry *prev, *next;
129 } *ldb_backends = NULL;
131 static struct ops_list_entry {
132 const struct ldb_module_ops *ops;
133 struct ops_list_entry *next;
134 } *registered_modules = NULL;
136 static const struct ldb_builtins {
137 const struct ldb_backend_ops *backend_ops;
138 const struct ldb_module_ops *module_ops;
141 static ldb_connect_fn ldb_find_backend(const char *url)
143 struct backends_list_entry *backend;
146 for (i = 0; builtins[i].backend_ops || builtins[i].module_ops; i++) {
147 if (builtins[i].backend_ops == NULL) continue;
149 if (strncmp(builtins[i].backend_ops->name, url,
150 strlen(builtins[i].backend_ops->name)) == 0) {
151 return builtins[i].backend_ops->connect_fn;
155 for (backend = ldb_backends; backend; backend = backend->next) {
156 if (strncmp(backend->ops->name, url,
157 strlen(backend->ops->name)) == 0) {
158 return backend->ops->connect_fn;
166 register a new ldb backend
168 int ldb_register_backend(const char *url_prefix, ldb_connect_fn connectfn)
170 struct ldb_backend_ops *backend;
171 struct backends_list_entry *entry;
173 backend = talloc(talloc_autofree_context(), struct ldb_backend_ops);
174 if (!backend) return LDB_ERR_OPERATIONS_ERROR;
176 entry = talloc(talloc_autofree_context(), struct backends_list_entry);
178 talloc_free(backend);
179 return LDB_ERR_OPERATIONS_ERROR;
182 if (ldb_find_backend(url_prefix)) {
186 /* Maybe check for duplicity here later on? */
188 backend->name = talloc_strdup(backend, url_prefix);
189 backend->connect_fn = connectfn;
190 entry->ops = backend;
191 DLIST_ADD(ldb_backends, entry);
197 Return the ldb module form of a database.
198 The URL can either be one of the following forms
202 flags is made up of LDB_FLG_*
204 the options are passed uninterpreted to the backend, and are
207 This allows modules to get at only the backend module, for example where a
208 module may wish to direct certain requests at a particular backend.
210 int ldb_connect_backend(struct ldb_context *ldb,
212 const char *options[],
213 struct ldb_module **backend_module)
219 if (strchr(url, ':') != NULL) {
220 backend = talloc_strndup(ldb, url, strchr(url, ':')-url);
223 backend = talloc_strdup(ldb, "tdb");
226 fn = ldb_find_backend(backend);
229 struct ldb_backend_ops *ops;
230 char *symbol_name = talloc_asprintf(ldb, "ldb_%s_backend_ops", backend);
231 if (symbol_name == NULL) {
232 return LDB_ERR_OPERATIONS_ERROR;
234 ops = ldb_dso_load_symbol(ldb, backend, symbol_name);
236 fn = ops->connect_fn;
238 talloc_free(symbol_name);
241 talloc_free(backend);
244 ldb_debug(ldb, LDB_DEBUG_FATAL,
245 "Unable to find backend for '%s'\n", url);
246 return LDB_ERR_OTHER;
249 ret = fn(ldb, url, ldb->flags, options, backend_module);
251 if (ret != LDB_SUCCESS) {
252 ldb_debug(ldb, LDB_DEBUG_ERROR,
253 "Failed to connect to '%s'\n", url);
259 static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
261 struct ops_list_entry *e;
264 for (i = 0; builtins[i].backend_ops || builtins[i].module_ops; i++) {
265 if (builtins[i].module_ops == NULL) continue;
267 if (strcmp(builtins[i].module_ops->name, name) == 0)
268 return builtins[i].module_ops;
271 for (e = registered_modules; e; e = e->next) {
272 if (strcmp(e->ops->name, name) == 0)
280 int ldb_register_module(const struct ldb_module_ops *ops)
282 struct ops_list_entry *entry = talloc(talloc_autofree_context(), struct ops_list_entry);
284 if (ldb_find_module_ops(ops->name) != NULL)
291 entry->next = registered_modules;
292 registered_modules = entry;
297 static void *ldb_dso_load_symbol(struct ldb_context *ldb, const char *name,
304 if (ldb->modules_dir == NULL)
307 path = talloc_asprintf(ldb, "%s/%s.%s", ldb->modules_dir, name,
310 ldb_debug(ldb, LDB_DEBUG_TRACE, "trying to load %s from %s\n", name, path);
312 handle = dlopen(path, RTLD_NOW);
313 if (handle == NULL) {
314 ldb_debug(ldb, LDB_DEBUG_WARNING, "unable to load %s from %s: %s\n", name, path, dlerror());
318 sym = (int (*)(void))dlsym(handle, symbol);
321 ldb_debug(ldb, LDB_DEBUG_ERROR, "no symbol `%s' found in %s: %s\n", symbol, path, dlerror());
330 int ldb_load_modules_list(struct ldb_context *ldb, const char **module_list, struct ldb_module *backend, struct ldb_module **out)
332 struct ldb_module *module;
337 for (i = 0; module_list[i] != NULL; i++) {
338 struct ldb_module *current;
339 const struct ldb_module_ops *ops;
341 if (strcmp(module_list[i], "") == 0) {
345 ops = ldb_find_module_ops(module_list[i]);
347 char *symbol_name = talloc_asprintf(ldb, "ldb_%s_module_ops",
349 if (symbol_name == NULL) {
350 return LDB_ERR_OPERATIONS_ERROR;
352 ops = ldb_dso_load_symbol(ldb, module_list[i], symbol_name);
353 talloc_free(symbol_name);
357 ldb_debug(ldb, LDB_DEBUG_WARNING, "WARNING: Module [%s] not found\n",
362 current = talloc_zero(ldb, struct ldb_module);
363 if (current == NULL) {
364 return LDB_ERR_OPERATIONS_ERROR;
366 talloc_set_name(current, "ldb_module: %s", module_list[i]);
371 DLIST_ADD(module, current);
377 int ldb_init_module_chain(struct ldb_context *ldb, struct ldb_module *module)
379 while (module && module->ops->init_context == NULL)
380 module = module->next;
382 /* init is different in that it is not an error if modules
383 * do not require initialization */
386 int ret = module->ops->init_context(module);
387 if (ret != LDB_SUCCESS) {
388 ldb_debug(ldb, LDB_DEBUG_FATAL, "module %s initialization failed\n", module->ops->name);
396 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
398 const char **modules = NULL;
401 TALLOC_CTX *mem_ctx = talloc_new(ldb);
403 return LDB_ERR_OPERATIONS_ERROR;
406 /* find out which modules we are requested to activate */
408 /* check if we have a custom module list passd as ldb option */
410 for (i = 0; options[i] != NULL; i++) {
411 if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
412 modules = ldb_modules_list_from_string(ldb, mem_ctx, &options[i][LDB_MODULE_PREFIX_LEN]);
417 /* if not overloaded by options and the backend is not ldap try to load the modules list from ldb */
418 if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) {
419 const char * const attrs[] = { "@LIST" , NULL};
420 struct ldb_result *res = NULL;
421 struct ldb_dn *mods_dn;
423 mods_dn = ldb_dn_new(mem_ctx, ldb, "@MODULES");
424 if (mods_dn == NULL) {
425 talloc_free(mem_ctx);
429 ret = ldb_search(ldb, mods_dn, &res, mods_dn, LDB_SCOPE_BASE, attrs, "@LIST=*");
431 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
432 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
433 } else if (ret != LDB_SUCCESS) {
434 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb));
435 talloc_free(mem_ctx);
438 const char *module_list;
439 if (res->count == 0) {
440 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
441 } else if (res->count > 1) {
442 ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found (%d), bailing out\n", res->count);
443 talloc_free(mem_ctx);
446 module_list = ldb_msg_find_attr_as_string(res->msgs[0], "@LIST", NULL);
448 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
450 modules = ldb_modules_list_from_string(ldb, mem_ctx,
455 talloc_free(mods_dn);
458 if (modules != NULL) {
459 ret = ldb_load_modules_list(ldb, modules, ldb->modules, &ldb->modules);
460 if (ret != LDB_SUCCESS) {
461 talloc_free(mem_ctx);
465 ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database");
468 ret = ldb_init_module_chain(ldb, ldb->modules);
469 talloc_free(mem_ctx);
474 by using this we allow ldb modules to only implement the functions they care about,
475 which makes writing a module simpler, and makes it more likely to keep working
478 #define FIND_OP(module, op) do { \
479 struct ldb_context *ldb = module->ldb; \
480 module = module->next; \
481 while (module && module->ops->op == NULL) module = module->next; \
482 if (module == NULL) { \
483 ldb_asprintf_errstring(ldb, "Unable to find backend operation for " #op ); \
484 return LDB_ERR_OPERATIONS_ERROR; \
490 helper functions to call the next module in chain
493 int ldb_next_request(struct ldb_module *module, struct ldb_request *request)
497 if (request->callback == NULL) {
498 ldb_set_errstring(module->ldb, "Requests MUST define callbacks");
499 return LDB_ERR_UNWILLING_TO_PERFORM;
502 switch (request->operation) {
504 FIND_OP(module, search);
505 ret = module->ops->search(module, request);
508 FIND_OP(module, add);
509 ret = module->ops->add(module, request);
512 FIND_OP(module, modify);
513 ret = module->ops->modify(module, request);
516 FIND_OP(module, del);
517 ret = module->ops->del(module, request);
520 FIND_OP(module, rename);
521 ret = module->ops->rename(module, request);
524 FIND_OP(module, extended);
525 ret = module->ops->extended(module, request);
528 FIND_OP(module, request);
529 ret = module->ops->request(module, request);
532 if (ret == LDB_SUCCESS) {
535 if (!ldb_errstring(module->ldb)) {
536 /* Set a default error string, to place the blame somewhere */
537 ldb_asprintf_errstring(module->ldb, "error in module %s: %s (%d)", module->ops->name, ldb_strerror(ret), ret);
542 int ldb_next_init(struct ldb_module *module)
544 module = module->next;
546 return ldb_init_module_chain(module->ldb, module);
549 int ldb_next_start_trans(struct ldb_module *module)
551 FIND_OP(module, start_transaction);
552 return module->ops->start_transaction(module);
555 int ldb_next_end_trans(struct ldb_module *module)
557 FIND_OP(module, end_transaction);
558 return module->ops->end_transaction(module);
561 int ldb_next_del_trans(struct ldb_module *module)
563 FIND_OP(module, del_transaction);
564 return module->ops->del_transaction(module);
567 struct ldb_handle *ldb_handle_new(TALLOC_CTX *mem_ctx, struct ldb_context *ldb)
569 struct ldb_handle *h;
571 h = talloc_zero(mem_ctx, struct ldb_handle);
573 ldb_set_errstring(ldb, "Out of Memory");
577 h->status = LDB_SUCCESS;
578 h->state = LDB_ASYNC_INIT;
584 /* calls the request callback to send an entry
587 * req: the original request passed to your module
588 * msg: reply message (must be a talloc pointer, and it will be stolen
589 * on the ldb_reply that is sent to the callback)
590 * ctrls: controls to send in the reply (must be a talloc pointer, and it will be stolen
591 * on the ldb_reply that is sent to the callback)
594 int ldb_module_send_entry(struct ldb_request *req,
595 struct ldb_message *msg,
596 struct ldb_control **ctrls)
598 struct ldb_reply *ares;
600 ares = talloc_zero(req, struct ldb_reply);
602 ldb_oom(req->handle->ldb);
603 req->callback(req, NULL);
604 return LDB_ERR_OPERATIONS_ERROR;
606 ares->type = LDB_REPLY_ENTRY;
607 ares->message = talloc_steal(ares, msg);
608 ares->controls = talloc_steal(ares, ctrls);
609 ares->error = LDB_SUCCESS;
611 return req->callback(req, ares);
614 /* calls the request callback to send an referrals
617 * req: the original request passed to your module
618 * ref: referral string (must be a talloc pointeri, steal)
621 int ldb_module_send_referral(struct ldb_request *req,
624 struct ldb_reply *ares;
626 ares = talloc_zero(req, struct ldb_reply);
628 ldb_oom(req->handle->ldb);
629 req->callback(req, NULL);
630 return LDB_ERR_OPERATIONS_ERROR;
632 ares->type = LDB_REPLY_REFERRAL;
633 ares->referral = talloc_steal(ares, ref);
634 ares->error = LDB_SUCCESS;
636 return req->callback(req, ares);
639 /* calls the original request callback
642 * req: the original request passed to your module
643 * ctrls: controls to send in the reply (must be a talloc pointer, steal)
644 * response: results for extended request (steal)
645 * error: LDB_SUCCESS for a succesful return
646 * any other ldb error otherwise
648 int ldb_module_done(struct ldb_request *req,
649 struct ldb_control **ctrls,
650 struct ldb_extended *response,
653 struct ldb_reply *ares;
655 ares = talloc_zero(req, struct ldb_reply);
657 ldb_oom(req->handle->ldb);
658 req->callback(req, NULL);
659 return LDB_ERR_OPERATIONS_ERROR;
661 ares->type = LDB_REPLY_DONE;
662 ares->controls = talloc_steal(ares, ctrls);
663 ares->response = talloc_steal(ares, response);
666 req->callback(req, ares);
670 /* to be used *only* in modules init functions.
671 * this function i synchronous and will register
672 * the requested OID in the rootdse module if present
673 * otherwise it will return an error */
674 int ldb_mod_register_control(struct ldb_module *module, const char *oid)
676 struct ldb_request *req;
679 req = talloc_zero(module, struct ldb_request);
681 return LDB_ERR_OPERATIONS_ERROR;
684 req->operation = LDB_REQ_REGISTER_CONTROL;
685 req->op.reg_control.oid = oid;
686 req->callback = ldb_op_default_callback;
688 ldb_set_timeout(module->ldb, req, 0);
690 req->handle = ldb_handle_new(req, module->ldb);
691 if (req->handle == NULL) {
692 return LDB_ERR_OPERATIONS_ERROR;
695 ret = ldb_request(module->ldb, req);
696 if (ret == LDB_SUCCESS) {
697 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
704 #ifndef STATIC_LIBLDB_MODULES
707 #define LDAP_BACKEND LDB_BACKEND(ldap), LDB_BACKEND(ldapi), LDB_BACKEND(ldaps),
712 #ifdef HAVE_LDB_SQLITE3
713 #define SQLITE3_BACKEND LDB_BACKEND(sqlite3),
715 #define SQLITE3_BACKEND
718 #define STATIC_LIBLDB_MODULES \
722 LDB_MODULE(operational), \
723 LDB_MODULE(rdn_name), \
724 LDB_MODULE(paged_results), \
725 LDB_MODULE(server_sort), \
731 * this is a bit hacked, as STATIC_LIBLDB_MODULES contains ','
732 * between the elements and we want to autogenerate the
733 * extern struct declarations, so we do some hacks and let the
734 * ',' appear in an unused function prototype.
737 #define NULL LDB_MODULE(NULL),
739 #define LDB_BACKEND(name) \
741 extern const struct ldb_backend_ops ldb_ ## name ## _backend_ops;\
742 extern void ldb_noop ## name (int
743 #define LDB_MODULE(name) \
745 extern const struct ldb_module_ops ldb_ ## name ## _module_ops;\
746 extern void ldb_noop ## name (int
748 extern void ldb_start_noop(int,
749 STATIC_LIBLDB_MODULES
754 .backend_ops = (void *)0, \
755 .module_ops = (void *)0 \
759 #define LDB_BACKEND(name) { \
760 .backend_ops = &ldb_ ## name ## _backend_ops, \
761 .module_ops = (void *)0 \
764 #define LDB_MODULE(name) { \
765 .backend_ops = (void *)0, \
766 .module_ops = &ldb_ ## name ## _module_ops \
769 static const struct ldb_builtins builtins[] = {
770 STATIC_LIBLDB_MODULES