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);
100 /* The str*r*chr walks backwards: This is how we get the inverse order mentioned above */
101 while ((p = strrchr(modstr, ',')) != NULL) {
107 modules = talloc_realloc(mem_ctx, modules, char *, i + 2);
109 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()");
116 modules[i + 1] = NULL;
118 m = (const char **)modules;
123 static struct backends_list_entry {
124 struct ldb_backend_ops *ops;
125 struct backends_list_entry *prev, *next;
126 } *ldb_backends = NULL;
128 static struct ops_list_entry {
129 const struct ldb_module_ops *ops;
130 struct ops_list_entry *next;
131 } *registered_modules = NULL;
133 static const struct ldb_builtins {
134 const struct ldb_backend_ops *backend_ops;
135 const struct ldb_module_ops *module_ops;
138 static ldb_connect_fn ldb_find_backend(const char *url)
140 struct backends_list_entry *backend;
143 for (i = 0; builtins[i].backend_ops || builtins[i].module_ops; i++) {
144 if (builtins[i].backend_ops == NULL) continue;
146 if (strncmp(builtins[i].backend_ops->name, url,
147 strlen(builtins[i].backend_ops->name)) == 0) {
148 return builtins[i].backend_ops->connect_fn;
152 for (backend = ldb_backends; backend; backend = backend->next) {
153 if (strncmp(backend->ops->name, url,
154 strlen(backend->ops->name)) == 0) {
155 return backend->ops->connect_fn;
163 register a new ldb backend
165 int ldb_register_backend(const char *url_prefix, ldb_connect_fn connectfn)
167 struct ldb_backend_ops *backend;
168 struct backends_list_entry *entry;
170 backend = talloc(talloc_autofree_context(), struct ldb_backend_ops);
171 if (!backend) return LDB_ERR_OPERATIONS_ERROR;
173 entry = talloc(talloc_autofree_context(), struct backends_list_entry);
175 talloc_free(backend);
176 return LDB_ERR_OPERATIONS_ERROR;
179 if (ldb_find_backend(url_prefix)) {
183 /* Maybe check for duplicity here later on? */
185 backend->name = talloc_strdup(backend, url_prefix);
186 backend->connect_fn = connectfn;
187 entry->ops = backend;
188 DLIST_ADD(ldb_backends, entry);
194 Return the ldb module form of a database.
195 The URL can either be one of the following forms
199 flags is made up of LDB_FLG_*
201 the options are passed uninterpreted to the backend, and are
204 This allows modules to get at only the backend module, for example where a
205 module may wish to direct certain requests at a particular backend.
207 int ldb_connect_backend(struct ldb_context *ldb,
209 const char *options[],
210 struct ldb_module **backend_module)
216 if (strchr(url, ':') != NULL) {
217 backend = talloc_strndup(ldb, url, strchr(url, ':')-url);
220 backend = talloc_strdup(ldb, "tdb");
223 fn = ldb_find_backend(backend);
226 struct ldb_backend_ops *ops;
227 char *symbol_name = talloc_asprintf(ldb, "ldb_%s_backend_ops", backend);
228 if (symbol_name == NULL) {
229 return LDB_ERR_OPERATIONS_ERROR;
231 ops = ldb_dso_load_symbol(ldb, backend, symbol_name);
233 fn = ops->connect_fn;
235 talloc_free(symbol_name);
238 talloc_free(backend);
241 ldb_debug(ldb, LDB_DEBUG_FATAL,
242 "Unable to find backend for '%s'", url);
243 return LDB_ERR_OTHER;
246 ret = fn(ldb, url, ldb->flags, options, backend_module);
248 if (ret != LDB_SUCCESS) {
249 ldb_debug(ldb, LDB_DEBUG_ERROR,
250 "Failed to connect to '%s'", url);
256 static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
258 struct ops_list_entry *e;
261 for (i = 0; builtins[i].backend_ops || builtins[i].module_ops; i++) {
262 if (builtins[i].module_ops == NULL) continue;
264 if (strcmp(builtins[i].module_ops->name, name) == 0)
265 return builtins[i].module_ops;
268 for (e = registered_modules; e; e = e->next) {
269 if (strcmp(e->ops->name, name) == 0)
277 int ldb_register_module(const struct ldb_module_ops *ops)
279 struct ops_list_entry *entry = talloc(talloc_autofree_context(), struct ops_list_entry);
281 if (ldb_find_module_ops(ops->name) != NULL)
288 entry->next = registered_modules;
289 registered_modules = entry;
294 static void *ldb_dso_load_symbol(struct ldb_context *ldb, const char *name,
301 if (ldb->modules_dir == NULL)
304 path = talloc_asprintf(ldb, "%s/%s.%s", ldb->modules_dir, name,
307 ldb_debug(ldb, LDB_DEBUG_TRACE, "trying to load %s from %s", name, path);
309 handle = dlopen(path, RTLD_NOW);
310 if (handle == NULL) {
311 ldb_debug(ldb, LDB_DEBUG_WARNING, "unable to load %s from %s: %s", name, path, dlerror());
315 sym = (int (*)(void))dlsym(handle, symbol);
318 ldb_debug(ldb, LDB_DEBUG_ERROR, "no symbol `%s' found in %s: %s", symbol, path, dlerror());
327 int ldb_load_modules_list(struct ldb_context *ldb, const char **module_list, struct ldb_module *backend, struct ldb_module **out)
329 struct ldb_module *module;
334 for (i = 0; module_list[i] != NULL; i++) {
335 struct ldb_module *current;
336 const struct ldb_module_ops *ops;
338 if (strcmp(module_list[i], "") == 0) {
342 ops = ldb_find_module_ops(module_list[i]);
344 char *symbol_name = talloc_asprintf(ldb, "ldb_%s_module_ops",
346 if (symbol_name == NULL) {
347 return LDB_ERR_OPERATIONS_ERROR;
349 ops = ldb_dso_load_symbol(ldb, module_list[i], symbol_name);
350 talloc_free(symbol_name);
354 ldb_debug(ldb, LDB_DEBUG_WARNING, "WARNING: Module [%s] not found",
359 current = talloc_zero(ldb, struct ldb_module);
360 if (current == NULL) {
361 return LDB_ERR_OPERATIONS_ERROR;
363 talloc_set_name(current, "ldb_module: %s", module_list[i]);
368 DLIST_ADD(module, current);
374 int ldb_init_module_chain(struct ldb_context *ldb, struct ldb_module *module)
376 while (module && module->ops->init_context == NULL)
377 module = module->next;
379 /* init is different in that it is not an error if modules
380 * do not require initialization */
383 int ret = module->ops->init_context(module);
384 if (ret != LDB_SUCCESS) {
385 ldb_debug(ldb, LDB_DEBUG_FATAL, "module %s initialization failed", module->ops->name);
393 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
395 const char **modules = NULL;
398 TALLOC_CTX *mem_ctx = talloc_new(ldb);
400 return LDB_ERR_OPERATIONS_ERROR;
403 /* find out which modules we are requested to activate */
405 /* check if we have a custom module list passd as ldb option */
407 for (i = 0; options[i] != NULL; i++) {
408 if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
409 modules = ldb_modules_list_from_string(ldb, mem_ctx, &options[i][LDB_MODULE_PREFIX_LEN]);
414 /* if not overloaded by options and the backend is not ldap try to load the modules list from ldb */
415 if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) {
416 const char * const attrs[] = { "@LIST" , NULL};
417 struct ldb_result *res = NULL;
418 struct ldb_dn *mods_dn;
420 mods_dn = ldb_dn_new(mem_ctx, ldb, "@MODULES");
421 if (mods_dn == NULL) {
422 talloc_free(mem_ctx);
426 ret = ldb_search(ldb, mods_dn, &res, mods_dn, LDB_SCOPE_BASE, attrs, "@LIST=*");
428 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
429 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
430 } else if (ret != LDB_SUCCESS) {
431 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out", ldb_errstring(ldb));
432 talloc_free(mem_ctx);
435 const char *module_list;
436 if (res->count == 0) {
437 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
438 } else if (res->count > 1) {
439 ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found (%d), bailing out", res->count);
440 talloc_free(mem_ctx);
443 module_list = ldb_msg_find_attr_as_string(res->msgs[0], "@LIST", NULL);
445 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
447 modules = ldb_modules_list_from_string(ldb, mem_ctx,
452 talloc_free(mods_dn);
455 if (modules != NULL) {
456 ret = ldb_load_modules_list(ldb, modules, ldb->modules, &ldb->modules);
457 if (ret != LDB_SUCCESS) {
458 talloc_free(mem_ctx);
462 ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database");
465 ret = ldb_init_module_chain(ldb, ldb->modules);
466 talloc_free(mem_ctx);
471 by using this we allow ldb modules to only implement the functions they care about,
472 which makes writing a module simpler, and makes it more likely to keep working
475 #define FIND_OP(module, op) do { \
476 struct ldb_context *ldb = module->ldb; \
477 module = module->next; \
478 while (module && module->ops->op == NULL) module = module->next; \
479 if (module == NULL) { \
480 ldb_asprintf_errstring(ldb, "Unable to find backend operation for " #op ); \
481 return LDB_ERR_OPERATIONS_ERROR; \
486 struct ldb_module *ldb_module_new(TALLOC_CTX *memctx,
487 struct ldb_context *ldb,
488 const char *module_name,
489 const struct ldb_module_ops *ops)
491 struct ldb_module *module;
493 module = talloc(memctx, struct ldb_module);
498 talloc_set_name_const(module, module_name);
500 module->prev = module->next = NULL;
506 const char * ldb_module_get_name(struct ldb_module *module)
508 return module->ops->name;
511 struct ldb_context *ldb_module_get_ctx(struct ldb_module *module)
516 void *ldb_module_get_private(struct ldb_module *module)
518 return module->private_data;
521 void ldb_module_set_private(struct ldb_module *module, void *private_data)
523 module->private_data = private_data;
527 helper functions to call the next module in chain
530 int ldb_next_request(struct ldb_module *module, struct ldb_request *request)
534 if (request->callback == NULL) {
535 ldb_set_errstring(module->ldb, "Requests MUST define callbacks");
536 return LDB_ERR_UNWILLING_TO_PERFORM;
539 switch (request->operation) {
541 FIND_OP(module, search);
542 ret = module->ops->search(module, request);
545 FIND_OP(module, add);
546 ret = module->ops->add(module, request);
549 FIND_OP(module, modify);
550 ret = module->ops->modify(module, request);
553 FIND_OP(module, del);
554 ret = module->ops->del(module, request);
557 FIND_OP(module, rename);
558 ret = module->ops->rename(module, request);
561 FIND_OP(module, extended);
562 ret = module->ops->extended(module, request);
565 FIND_OP(module, request);
566 ret = module->ops->request(module, request);
569 if (ret == LDB_SUCCESS) {
572 if (!ldb_errstring(module->ldb)) {
573 /* Set a default error string, to place the blame somewhere */
574 ldb_asprintf_errstring(module->ldb, "error in module %s: %s (%d)", module->ops->name, ldb_strerror(ret), ret);
579 int ldb_next_init(struct ldb_module *module)
581 module = module->next;
583 return ldb_init_module_chain(module->ldb, module);
586 int ldb_next_start_trans(struct ldb_module *module)
588 FIND_OP(module, start_transaction);
589 return module->ops->start_transaction(module);
592 int ldb_next_end_trans(struct ldb_module *module)
594 FIND_OP(module, end_transaction);
595 return module->ops->end_transaction(module);
598 int ldb_next_del_trans(struct ldb_module *module)
600 FIND_OP(module, del_transaction);
601 return module->ops->del_transaction(module);
604 struct ldb_handle *ldb_handle_new(TALLOC_CTX *mem_ctx, struct ldb_context *ldb)
606 struct ldb_handle *h;
608 h = talloc_zero(mem_ctx, struct ldb_handle);
610 ldb_set_errstring(ldb, "Out of Memory");
614 h->status = LDB_SUCCESS;
615 h->state = LDB_ASYNC_INIT;
621 /* calls the request callback to send an entry
624 * req: the original request passed to your module
625 * msg: reply message (must be a talloc pointer, and it will be stolen
626 * on the ldb_reply that is sent to the callback)
627 * ctrls: controls to send in the reply (must be a talloc pointer, and it will be stolen
628 * on the ldb_reply that is sent to the callback)
631 int ldb_module_send_entry(struct ldb_request *req,
632 struct ldb_message *msg,
633 struct ldb_control **ctrls)
635 struct ldb_reply *ares;
637 ares = talloc_zero(req, struct ldb_reply);
639 ldb_oom(req->handle->ldb);
640 req->callback(req, NULL);
641 return LDB_ERR_OPERATIONS_ERROR;
643 ares->type = LDB_REPLY_ENTRY;
644 ares->message = talloc_steal(ares, msg);
645 ares->controls = talloc_steal(ares, ctrls);
646 ares->error = LDB_SUCCESS;
648 return req->callback(req, ares);
651 /* calls the request callback to send an referrals
654 * req: the original request passed to your module
655 * ref: referral string (must be a talloc pointeri, steal)
658 int ldb_module_send_referral(struct ldb_request *req,
661 struct ldb_reply *ares;
663 ares = talloc_zero(req, struct ldb_reply);
665 ldb_oom(req->handle->ldb);
666 req->callback(req, NULL);
667 return LDB_ERR_OPERATIONS_ERROR;
669 ares->type = LDB_REPLY_REFERRAL;
670 ares->referral = talloc_steal(ares, ref);
671 ares->error = LDB_SUCCESS;
673 return req->callback(req, ares);
676 /* calls the original request callback
679 * req: the original request passed to your module
680 * ctrls: controls to send in the reply (must be a talloc pointer, steal)
681 * response: results for extended request (steal)
682 * error: LDB_SUCCESS for a succesful return
683 * any other ldb error otherwise
685 int ldb_module_done(struct ldb_request *req,
686 struct ldb_control **ctrls,
687 struct ldb_extended *response,
690 struct ldb_reply *ares;
692 ares = talloc_zero(req, struct ldb_reply);
694 ldb_oom(req->handle->ldb);
695 req->callback(req, NULL);
696 return LDB_ERR_OPERATIONS_ERROR;
698 ares->type = LDB_REPLY_DONE;
699 ares->controls = talloc_steal(ares, ctrls);
700 ares->response = talloc_steal(ares, response);
703 req->callback(req, ares);
707 /* to be used *only* in modules init functions.
708 * this function i synchronous and will register
709 * the requested OID in the rootdse module if present
710 * otherwise it will return an error */
711 int ldb_mod_register_control(struct ldb_module *module, const char *oid)
713 struct ldb_request *req;
716 req = talloc_zero(module, struct ldb_request);
718 return LDB_ERR_OPERATIONS_ERROR;
721 req->operation = LDB_REQ_REGISTER_CONTROL;
722 req->op.reg_control.oid = oid;
723 req->callback = ldb_op_default_callback;
725 ldb_set_timeout(module->ldb, req, 0);
727 req->handle = ldb_handle_new(req, module->ldb);
728 if (req->handle == NULL) {
729 return LDB_ERR_OPERATIONS_ERROR;
732 ret = ldb_request(module->ldb, req);
733 if (ret == LDB_SUCCESS) {
734 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
741 #ifndef STATIC_LIBLDB_MODULES
744 #define LDAP_BACKEND LDB_BACKEND(ldap), LDB_BACKEND(ldapi), LDB_BACKEND(ldaps),
749 #ifdef HAVE_LDB_SQLITE3
750 #define SQLITE3_BACKEND LDB_BACKEND(sqlite3),
752 #define SQLITE3_BACKEND
755 #define STATIC_LIBLDB_MODULES \
759 LDB_MODULE(operational), \
760 LDB_MODULE(rdn_name), \
761 LDB_MODULE(paged_results), \
762 LDB_MODULE(server_sort), \
768 * this is a bit hacked, as STATIC_LIBLDB_MODULES contains ','
769 * between the elements and we want to autogenerate the
770 * extern struct declarations, so we do some hacks and let the
771 * ',' appear in an unused function prototype.
774 #define NULL LDB_MODULE(NULL),
776 #define LDB_BACKEND(name) \
778 extern const struct ldb_backend_ops ldb_ ## name ## _backend_ops;\
779 extern void ldb_noop ## name (int
780 #define LDB_MODULE(name) \
782 extern const struct ldb_module_ops ldb_ ## name ## _module_ops;\
783 extern void ldb_noop ## name (int
785 extern void ldb_start_noop(int,
786 STATIC_LIBLDB_MODULES
791 .backend_ops = (void *)0, \
792 .module_ops = (void *)0 \
796 #define LDB_BACKEND(name) { \
797 .backend_ops = &ldb_ ## name ## _backend_ops, \
798 .module_ops = (void *)0 \
801 #define LDB_MODULE(name) { \
802 .backend_ops = (void *)0, \
803 .module_ops = &ldb_ ## name ## _module_ops \
806 static const struct ldb_builtins builtins[] = {
807 STATIC_LIBLDB_MODULES