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_private.h"
35 #include "dlinklist.h"
37 #define LDB_MODULE_PREFIX "modules:"
38 #define LDB_MODULE_PREFIX_LEN 8
40 static void *ldb_dso_load_symbol(struct ldb_context *ldb, const char *name,
43 void ldb_set_modules_dir(struct ldb_context *ldb, const char *path)
45 talloc_free(ldb->modules_dir);
46 ldb->modules_dir = talloc_strdup(ldb, path);
49 static char *ldb_modules_strdup_no_spaces(TALLOC_CTX *mem_ctx, const char *string)
54 trimmed = talloc_strdup(mem_ctx, string);
59 len = strlen(trimmed);
60 for (i = 0; trimmed[i] != '\0'; i++) {
65 memmove(&trimmed[i], &trimmed[i + 1], len -i -1);
74 /* modules are called in inverse order on the stack.
75 Lets place them as an admin would think the right order is.
76 Modules order is important */
77 const char **ldb_modules_list_from_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *string)
79 char **modules = NULL;
84 /* spaces not admitted */
85 modstr = ldb_modules_strdup_no_spaces(mem_ctx, string);
87 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_strdup_no_spaces()");
91 modules = talloc_realloc(mem_ctx, modules, char *, 2);
93 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()");
97 talloc_steal(modules, modstr);
99 if (modstr[0] == '\0') {
101 m = (const char **)modules;
106 /* The str*r*chr walks backwards: This is how we get the inverse order mentioned above */
107 while ((p = strrchr(modstr, ',')) != NULL) {
113 modules = talloc_realloc(mem_ctx, modules, char *, i + 2);
115 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()");
122 modules[i + 1] = NULL;
124 m = (const char **)modules;
129 static struct backends_list_entry {
130 struct ldb_backend_ops *ops;
131 struct backends_list_entry *prev, *next;
132 } *ldb_backends = NULL;
134 static struct ops_list_entry {
135 const struct ldb_module_ops *ops;
136 struct ops_list_entry *next;
137 } *registered_modules = NULL;
139 static const struct ldb_builtins {
140 const struct ldb_backend_ops *backend_ops;
141 const struct ldb_module_ops *module_ops;
144 static ldb_connect_fn ldb_find_backend(const char *url)
146 struct backends_list_entry *backend;
149 for (i = 0; builtins[i].backend_ops || builtins[i].module_ops; i++) {
150 if (builtins[i].backend_ops == NULL) continue;
152 if (strncmp(builtins[i].backend_ops->name, url,
153 strlen(builtins[i].backend_ops->name)) == 0) {
154 return builtins[i].backend_ops->connect_fn;
158 for (backend = ldb_backends; backend; backend = backend->next) {
159 if (strncmp(backend->ops->name, url,
160 strlen(backend->ops->name)) == 0) {
161 return backend->ops->connect_fn;
169 register a new ldb backend
171 int ldb_register_backend(const char *url_prefix, ldb_connect_fn connectfn)
173 struct ldb_backend_ops *backend;
174 struct backends_list_entry *entry;
176 backend = talloc(talloc_autofree_context(), struct ldb_backend_ops);
177 if (!backend) return LDB_ERR_OPERATIONS_ERROR;
179 entry = talloc(talloc_autofree_context(), struct backends_list_entry);
181 talloc_free(backend);
182 return LDB_ERR_OPERATIONS_ERROR;
185 if (ldb_find_backend(url_prefix)) {
189 /* Maybe check for duplicity here later on? */
191 backend->name = talloc_strdup(backend, url_prefix);
192 backend->connect_fn = connectfn;
193 entry->ops = backend;
194 DLIST_ADD(ldb_backends, entry);
200 Return the ldb module form of a database.
201 The URL can either be one of the following forms
205 flags is made up of LDB_FLG_*
207 the options are passed uninterpreted to the backend, and are
210 This allows modules to get at only the backend module, for example where a
211 module may wish to direct certain requests at a particular backend.
213 int ldb_connect_backend(struct ldb_context *ldb,
215 const char *options[],
216 struct ldb_module **backend_module)
222 if (strchr(url, ':') != NULL) {
223 backend = talloc_strndup(ldb, url, strchr(url, ':')-url);
226 backend = talloc_strdup(ldb, "tdb");
229 fn = ldb_find_backend(backend);
232 struct ldb_backend_ops *ops;
233 char *symbol_name = talloc_asprintf(ldb, "ldb_%s_backend_ops", backend);
234 if (symbol_name == NULL) {
235 return LDB_ERR_OPERATIONS_ERROR;
237 ops = ldb_dso_load_symbol(ldb, backend, symbol_name);
239 fn = ops->connect_fn;
241 talloc_free(symbol_name);
244 talloc_free(backend);
247 ldb_debug(ldb, LDB_DEBUG_FATAL,
248 "Unable to find backend for '%s'", url);
249 return LDB_ERR_OTHER;
252 ret = fn(ldb, url, ldb->flags, options, backend_module);
254 if (ret != LDB_SUCCESS) {
255 ldb_debug(ldb, LDB_DEBUG_ERROR,
256 "Failed to connect to '%s'", url);
262 static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
264 struct ops_list_entry *e;
267 for (i = 0; builtins[i].backend_ops || builtins[i].module_ops; i++) {
268 if (builtins[i].module_ops == NULL) continue;
270 if (strcmp(builtins[i].module_ops->name, name) == 0)
271 return builtins[i].module_ops;
274 for (e = registered_modules; e; e = e->next) {
275 if (strcmp(e->ops->name, name) == 0)
283 int ldb_register_module(const struct ldb_module_ops *ops)
285 struct ops_list_entry *entry = talloc(talloc_autofree_context(), struct ops_list_entry);
287 if (ldb_find_module_ops(ops->name) != NULL)
294 entry->next = registered_modules;
295 registered_modules = entry;
300 static void *ldb_dso_load_symbol(struct ldb_context *ldb, const char *name,
307 if (ldb->modules_dir == NULL)
310 path = talloc_asprintf(ldb, "%s/%s.%s", ldb->modules_dir, name,
313 ldb_debug(ldb, LDB_DEBUG_TRACE, "trying to load %s from %s", name, path);
315 handle = dlopen(path, RTLD_NOW);
316 if (handle == NULL) {
317 ldb_debug(ldb, LDB_DEBUG_WARNING, "unable to load %s from %s: %s", name, path, dlerror());
321 sym = (int (*)(void))dlsym(handle, symbol);
324 ldb_debug(ldb, LDB_DEBUG_ERROR, "no symbol `%s' found in %s: %s", symbol, path, dlerror());
333 int ldb_load_modules_list(struct ldb_context *ldb, const char **module_list, struct ldb_module *backend, struct ldb_module **out)
335 struct ldb_module *module;
340 for (i = 0; module_list[i] != NULL; i++) {
341 struct ldb_module *current;
342 const struct ldb_module_ops *ops;
344 if (strcmp(module_list[i], "") == 0) {
348 ops = ldb_find_module_ops(module_list[i]);
350 char *symbol_name = talloc_asprintf(ldb, "ldb_%s_module_ops",
352 if (symbol_name == NULL) {
353 return LDB_ERR_OPERATIONS_ERROR;
355 ops = ldb_dso_load_symbol(ldb, module_list[i], symbol_name);
356 talloc_free(symbol_name);
360 ldb_debug(ldb, LDB_DEBUG_WARNING, "WARNING: Module [%s] not found",
365 current = talloc_zero(ldb, struct ldb_module);
366 if (current == NULL) {
367 return LDB_ERR_OPERATIONS_ERROR;
369 talloc_set_name(current, "ldb_module: %s", module_list[i]);
374 DLIST_ADD(module, current);
380 int ldb_init_module_chain(struct ldb_context *ldb, struct ldb_module *module)
382 while (module && module->ops->init_context == NULL)
383 module = module->next;
385 /* init is different in that it is not an error if modules
386 * do not require initialization */
389 int ret = module->ops->init_context(module);
390 if (ret != LDB_SUCCESS) {
391 ldb_debug(ldb, LDB_DEBUG_FATAL, "module %s initialization failed", module->ops->name);
399 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
401 const char **modules = NULL;
404 TALLOC_CTX *mem_ctx = talloc_new(ldb);
406 return LDB_ERR_OPERATIONS_ERROR;
409 /* find out which modules we are requested to activate */
411 /* check if we have a custom module list passd as ldb option */
413 for (i = 0; options[i] != NULL; i++) {
414 if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
415 modules = ldb_modules_list_from_string(ldb, mem_ctx, &options[i][LDB_MODULE_PREFIX_LEN]);
420 /* if not overloaded by options and the backend is not ldap try to load the modules list from ldb */
421 if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) {
422 const char * const attrs[] = { "@LIST" , NULL};
423 struct ldb_result *res = NULL;
424 struct ldb_dn *mods_dn;
426 mods_dn = ldb_dn_new(mem_ctx, ldb, "@MODULES");
427 if (mods_dn == NULL) {
428 talloc_free(mem_ctx);
432 ret = ldb_search(ldb, mods_dn, &res, mods_dn, LDB_SCOPE_BASE, attrs, "@LIST=*");
434 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
435 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
436 } else if (ret != LDB_SUCCESS) {
437 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out", ldb_errstring(ldb));
438 talloc_free(mem_ctx);
441 const char *module_list;
442 if (res->count == 0) {
443 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
444 } else if (res->count > 1) {
445 ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found (%d), bailing out", res->count);
446 talloc_free(mem_ctx);
449 module_list = ldb_msg_find_attr_as_string(res->msgs[0], "@LIST", NULL);
451 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
453 modules = ldb_modules_list_from_string(ldb, mem_ctx,
458 talloc_free(mods_dn);
461 if (modules != NULL) {
462 ret = ldb_load_modules_list(ldb, modules, ldb->modules, &ldb->modules);
463 if (ret != LDB_SUCCESS) {
464 talloc_free(mem_ctx);
468 ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database");
471 ret = ldb_init_module_chain(ldb, ldb->modules);
472 talloc_free(mem_ctx);
477 by using this we allow ldb modules to only implement the functions they care about,
478 which makes writing a module simpler, and makes it more likely to keep working
481 #define FIND_OP_NOERR(module, op) do { \
482 module = module->next; \
483 while (module && module->ops->op == NULL) module = module->next; \
486 #define FIND_OP(module, op) do { \
487 struct ldb_context *ldb = module->ldb; \
488 FIND_OP_NOERR(module, op); \
489 if (module == NULL) { \
490 ldb_asprintf_errstring(ldb, "Unable to find backend operation for " #op ); \
491 return LDB_ERR_OPERATIONS_ERROR; \
496 struct ldb_module *ldb_module_new(TALLOC_CTX *memctx,
497 struct ldb_context *ldb,
498 const char *module_name,
499 const struct ldb_module_ops *ops)
501 struct ldb_module *module;
503 module = talloc(memctx, struct ldb_module);
508 talloc_set_name_const(module, module_name);
510 module->prev = module->next = NULL;
516 const char * ldb_module_get_name(struct ldb_module *module)
518 return module->ops->name;
521 struct ldb_context *ldb_module_get_ctx(struct ldb_module *module)
526 void *ldb_module_get_private(struct ldb_module *module)
528 return module->private_data;
531 void ldb_module_set_private(struct ldb_module *module, void *private_data)
533 module->private_data = private_data;
537 helper functions to call the next module in chain
540 int ldb_next_request(struct ldb_module *module, struct ldb_request *request)
544 if (request->callback == NULL) {
545 ldb_set_errstring(module->ldb, "Requests MUST define callbacks");
546 return LDB_ERR_UNWILLING_TO_PERFORM;
549 request->handle->nesting++;
551 switch (request->operation) {
553 FIND_OP(module, search);
554 ret = module->ops->search(module, request);
557 FIND_OP(module, add);
558 ret = module->ops->add(module, request);
561 FIND_OP(module, modify);
562 ret = module->ops->modify(module, request);
565 FIND_OP(module, del);
566 ret = module->ops->del(module, request);
569 FIND_OP(module, rename);
570 ret = module->ops->rename(module, request);
573 FIND_OP(module, extended);
574 ret = module->ops->extended(module, request);
577 FIND_OP(module, request);
578 ret = module->ops->request(module, request);
582 request->handle->nesting--;
584 if (ret == LDB_SUCCESS) {
587 if (!ldb_errstring(module->ldb)) {
588 /* Set a default error string, to place the blame somewhere */
589 ldb_asprintf_errstring(module->ldb, "error in module %s: %s (%d)", module->ops->name, ldb_strerror(ret), ret);
592 if (!(request->handle->flags & LDB_HANDLE_FLAG_DONE_CALLED)) {
593 /* It is _extremely_ common that a module returns a
594 * failure without calling ldb_module_done(), but that
595 * guarantees we will end up hanging in
596 * ldb_wait(). This fixes it without having to rewrite
597 * all our modules, and leaves us one less sharp
598 * corner for module developers to cut themselves on
600 ldb_module_done(request, NULL, NULL, ret);
605 int ldb_next_init(struct ldb_module *module)
607 module = module->next;
609 return ldb_init_module_chain(module->ldb, module);
612 int ldb_next_start_trans(struct ldb_module *module)
614 FIND_OP(module, start_transaction);
615 return module->ops->start_transaction(module);
618 int ldb_next_end_trans(struct ldb_module *module)
620 FIND_OP(module, end_transaction);
621 return module->ops->end_transaction(module);
624 int ldb_next_prepare_commit(struct ldb_module *module)
626 FIND_OP_NOERR(module, prepare_commit);
627 if (module == NULL) {
628 /* we are allowed to have no prepare commit in
632 return module->ops->prepare_commit(module);
635 int ldb_next_del_trans(struct ldb_module *module)
637 FIND_OP(module, del_transaction);
638 return module->ops->del_transaction(module);
641 struct ldb_handle *ldb_handle_new(TALLOC_CTX *mem_ctx, struct ldb_context *ldb)
643 struct ldb_handle *h;
645 h = talloc_zero(mem_ctx, struct ldb_handle);
647 ldb_set_errstring(ldb, "Out of Memory");
651 h->status = LDB_SUCCESS;
652 h->state = LDB_ASYNC_INIT;
659 /* calls the request callback to send an entry
662 * req: the original request passed to your module
663 * msg: reply message (must be a talloc pointer, and it will be stolen
664 * on the ldb_reply that is sent to the callback)
665 * ctrls: controls to send in the reply (must be a talloc pointer, and it will be stolen
666 * on the ldb_reply that is sent to the callback)
669 int ldb_module_send_entry(struct ldb_request *req,
670 struct ldb_message *msg,
671 struct ldb_control **ctrls)
673 struct ldb_reply *ares;
675 ares = talloc_zero(req, struct ldb_reply);
677 ldb_oom(req->handle->ldb);
678 req->callback(req, NULL);
679 return LDB_ERR_OPERATIONS_ERROR;
681 ares->type = LDB_REPLY_ENTRY;
682 ares->message = talloc_steal(ares, msg);
683 ares->controls = talloc_steal(ares, ctrls);
684 ares->error = LDB_SUCCESS;
686 if ((req->handle->ldb->flags & LDB_FLG_ENABLE_TRACING) &&
687 req->handle->nesting == 0) {
689 ldb_debug_add(req->handle->ldb, "ldb_trace_response: ENTRY\n");
690 s = ldb_ldif_message_string(req->handle->ldb, msg, LDB_CHANGETYPE_NONE, msg);
691 ldb_debug_add(req->handle->ldb, "%s\n", s);
693 ldb_debug_end(req->handle->ldb, LDB_DEBUG_TRACE);
696 return req->callback(req, ares);
699 /* calls the request callback to send an referrals
702 * req: the original request passed to your module
703 * ref: referral string (must be a talloc pointeri, steal)
706 int ldb_module_send_referral(struct ldb_request *req,
709 struct ldb_reply *ares;
711 ares = talloc_zero(req, struct ldb_reply);
713 ldb_oom(req->handle->ldb);
714 req->callback(req, NULL);
715 return LDB_ERR_OPERATIONS_ERROR;
717 ares->type = LDB_REPLY_REFERRAL;
718 ares->referral = talloc_steal(ares, ref);
719 ares->error = LDB_SUCCESS;
721 if ((req->handle->ldb->flags & LDB_FLG_ENABLE_TRACING) &&
722 req->handle->nesting == 0) {
723 ldb_debug_add(req->handle->ldb, "ldb_trace_response: REFERRAL\n");
724 ldb_debug_add(req->handle->ldb, "ref: %s\n", ref);
725 ldb_debug_end(req->handle->ldb, LDB_DEBUG_TRACE);
728 return req->callback(req, ares);
731 /* calls the original request callback
734 * req: the original request passed to your module
735 * ctrls: controls to send in the reply (must be a talloc pointer, steal)
736 * response: results for extended request (steal)
737 * error: LDB_SUCCESS for a succesful return
738 * any other ldb error otherwise
740 int ldb_module_done(struct ldb_request *req,
741 struct ldb_control **ctrls,
742 struct ldb_extended *response,
745 struct ldb_reply *ares;
747 ares = talloc_zero(req, struct ldb_reply);
749 ldb_oom(req->handle->ldb);
750 req->callback(req, NULL);
751 return LDB_ERR_OPERATIONS_ERROR;
753 ares->type = LDB_REPLY_DONE;
754 ares->controls = talloc_steal(ares, ctrls);
755 ares->response = talloc_steal(ares, response);
758 req->handle->flags |= LDB_HANDLE_FLAG_DONE_CALLED;
760 if ((req->handle->ldb->flags & LDB_FLG_ENABLE_TRACING) &&
761 req->handle->nesting == 0) {
762 ldb_debug_add(req->handle->ldb, "ldb_trace_response: DONE\n");
763 ldb_debug_add(req->handle->ldb, "error: %u\n", error);
764 if (ldb_errstring(req->handle->ldb)) {
765 ldb_debug_add(req->handle->ldb, "msg: %s\n",
766 ldb_errstring(req->handle->ldb));
768 ldb_debug_end(req->handle->ldb, LDB_DEBUG_TRACE);
771 req->callback(req, ares);
775 /* to be used *only* in modules init functions.
776 * this function i synchronous and will register
777 * the requested OID in the rootdse module if present
778 * otherwise it will return an error */
779 int ldb_mod_register_control(struct ldb_module *module, const char *oid)
781 struct ldb_request *req;
784 req = talloc_zero(module, struct ldb_request);
786 return LDB_ERR_OPERATIONS_ERROR;
789 req->operation = LDB_REQ_REGISTER_CONTROL;
790 req->op.reg_control.oid = oid;
791 req->callback = ldb_op_default_callback;
793 ldb_set_timeout(module->ldb, req, 0);
795 req->handle = ldb_handle_new(req, module->ldb);
796 if (req->handle == NULL) {
797 return LDB_ERR_OPERATIONS_ERROR;
800 ret = ldb_request(module->ldb, req);
801 if (ret == LDB_SUCCESS) {
802 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
809 #ifndef STATIC_LIBLDB_MODULES
812 #define LDAP_BACKEND LDB_BACKEND(ldap), LDB_BACKEND(ldapi), LDB_BACKEND(ldaps),
817 #ifdef HAVE_LDB_SQLITE3
818 #define SQLITE3_BACKEND LDB_BACKEND(sqlite3),
820 #define SQLITE3_BACKEND
823 #define STATIC_LIBLDB_MODULES \
827 LDB_MODULE(rdn_name), \
828 LDB_MODULE(paged_results), \
829 LDB_MODULE(server_sort), \
835 * this is a bit hacked, as STATIC_LIBLDB_MODULES contains ','
836 * between the elements and we want to autogenerate the
837 * extern struct declarations, so we do some hacks and let the
838 * ',' appear in an unused function prototype.
841 #define NULL LDB_MODULE(NULL),
843 #define LDB_BACKEND(name) \
845 extern const struct ldb_backend_ops ldb_ ## name ## _backend_ops;\
846 extern void ldb_noop ## name (int
847 #define LDB_MODULE(name) \
849 extern const struct ldb_module_ops ldb_ ## name ## _module_ops;\
850 extern void ldb_noop ## name (int
852 extern void ldb_start_noop(int,
853 STATIC_LIBLDB_MODULES
858 .backend_ops = (void *)0, \
859 .module_ops = (void *)0 \
863 #define LDB_BACKEND(name) { \
864 .backend_ops = &ldb_ ## name ## _backend_ops, \
865 .module_ops = (void *)0 \
868 #define LDB_MODULE(name) { \
869 .backend_ops = (void *)0, \
870 .module_ops = &ldb_ ## name ## _module_ops \
873 static const struct ldb_builtins builtins[] = {
874 STATIC_LIBLDB_MODULES