Add helper function to get a module name, may be useful for debugging.
[Samba/ekacnet.git] / source4 / lib / ldb / common / ldb_modules.c
blob99a47767e12a7f65a14b62c3330c36573b43c4cd
1 /*
2 ldb database library
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
8 ** under the LGPL
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/>.
25 * Name: ldb
27 * Component: ldb modules core
29 * Description: core modules routines
31 * Author: Simo Sorce
34 #include "ldb_private.h"
36 #if (_SAMBA_BUILD_ >= 4)
37 #include "includes.h"
38 #endif
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,
44 const char *symbol);
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)
54 int i, len;
55 char *trimmed;
57 trimmed = talloc_strdup(mem_ctx, string);
58 if (!trimmed) {
59 return NULL;
62 len = strlen(trimmed);
63 for (i = 0; trimmed[i] != '\0'; i++) {
64 switch (trimmed[i]) {
65 case ' ':
66 case '\t':
67 case '\n':
68 memmove(&trimmed[i], &trimmed[i + 1], len -i -1);
69 break;
73 return trimmed;
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;
83 const char **m;
84 char *modstr, *p;
85 int i;
87 /* spaces not admitted */
88 modstr = ldb_modules_strdup_no_spaces(mem_ctx, string);
89 if ( ! modstr) {
90 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_strdup_no_spaces()\n");
91 return NULL;
94 modules = talloc_realloc(mem_ctx, modules, char *, 2);
95 if ( ! modules ) {
96 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
97 talloc_free(modstr);
98 return NULL;
100 talloc_steal(modules, modstr);
102 i = 0;
103 /* The str*r*chr walks backwards: This is how we get the inverse order mentioned above */
104 while ((p = strrchr(modstr, ',')) != NULL) {
105 *p = '\0';
106 p++;
107 modules[i] = p;
109 i++;
110 modules = talloc_realloc(mem_ctx, modules, char *, i + 2);
111 if ( ! modules ) {
112 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
113 return NULL;
117 modules[i] = modstr;
119 modules[i + 1] = NULL;
121 m = (const char **)modules;
123 return m;
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;
139 } builtins[];
141 static ldb_connect_fn ldb_find_backend(const char *url)
143 struct backends_list_entry *backend;
144 int i;
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;
162 return NULL;
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);
177 if (!entry) {
178 talloc_free(backend);
179 return LDB_ERR_OPERATIONS_ERROR;
182 if (ldb_find_backend(url_prefix)) {
183 return LDB_SUCCESS;
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);
193 return LDB_SUCCESS;
197 Return the ldb module form of a database.
198 The URL can either be one of the following forms
199 ldb://path
200 ldapi://path
202 flags is made up of LDB_FLG_*
204 the options are passed uninterpreted to the backend, and are
205 backend specific.
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,
211 const char *url,
212 const char *options[],
213 struct ldb_module **backend_module)
215 int ret;
216 char *backend;
217 ldb_connect_fn fn;
219 if (strchr(url, ':') != NULL) {
220 backend = talloc_strndup(ldb, url, strchr(url, ':')-url);
221 } else {
222 /* Default to tdb */
223 backend = talloc_strdup(ldb, "tdb");
226 fn = ldb_find_backend(backend);
228 if (fn == NULL) {
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);
235 if (ops != NULL) {
236 fn = ops->connect_fn;
238 talloc_free(symbol_name);
241 talloc_free(backend);
243 if (fn == NULL) {
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);
254 return ret;
256 return ret;
259 static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
261 struct ops_list_entry *e;
262 int i;
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)
273 return e->ops;
276 return NULL;
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)
285 return -1;
287 if (entry == NULL)
288 return -1;
290 entry->ops = ops;
291 entry->next = registered_modules;
292 registered_modules = entry;
294 return 0;
297 static void *ldb_dso_load_symbol(struct ldb_context *ldb, const char *name,
298 const char *symbol)
300 char *path;
301 void *handle;
302 void *sym;
304 if (ldb->modules_dir == NULL)
305 return NULL;
307 path = talloc_asprintf(ldb, "%s/%s.%s", ldb->modules_dir, name,
308 SHLIBEXT);
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());
315 return NULL;
318 sym = (int (*)(void))dlsym(handle, symbol);
320 if (sym == NULL) {
321 ldb_debug(ldb, LDB_DEBUG_ERROR, "no symbol `%s' found in %s: %s\n", symbol, path, dlerror());
322 return NULL;
325 talloc_free(path);
327 return sym;
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;
333 int i;
335 module = backend;
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) {
342 continue;
345 ops = ldb_find_module_ops(module_list[i]);
346 if (ops == NULL) {
347 char *symbol_name = talloc_asprintf(ldb, "ldb_%s_module_ops",
348 module_list[i]);
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);
356 if (ops == NULL) {
357 ldb_debug(ldb, LDB_DEBUG_WARNING, "WARNING: Module [%s] not found\n",
358 module_list[i]);
359 continue;
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]);
368 current->ldb = ldb;
369 current->ops = ops;
371 DLIST_ADD(module, current);
373 *out = module;
374 return LDB_SUCCESS;
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 */
385 if (module) {
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);
389 return ret;
393 return LDB_SUCCESS;
396 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
398 const char **modules = NULL;
399 int i;
400 int ret;
401 TALLOC_CTX *mem_ctx = talloc_new(ldb);
402 if (!mem_ctx) {
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 */
409 if (options) {
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);
426 return -1;
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);
436 return ret;
437 } else {
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);
444 return -1;
445 } else {
446 module_list = ldb_msg_find_attr_as_string(res->msgs[0], "@LIST", NULL);
447 if (!module_list) {
448 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
450 modules = ldb_modules_list_from_string(ldb, mem_ctx,
451 module_list);
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);
462 return ret;
464 } else {
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);
470 return ret;
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
476 when ldb is extended
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; \
486 } while (0)
489 struct ldb_module *ldb_module_new(TALLOC_CTX *memctx,
490 struct ldb_context *ldb,
491 const char *module_name,
492 const struct ldb_module_ops *ops)
494 struct ldb_module *module;
496 module = talloc(memctx, struct ldb_module);
497 if (!module) {
498 ldb_oom(ldb);
499 return NULL;
501 talloc_set_name_const(module, module_name);
502 module->ldb = ldb;
503 module->prev = module->next = NULL;
504 module->ops = ops;
506 return module;
509 const char * ldb_module_get_name(struct ldb_module *module)
511 return module->ops->name;
514 struct ldb_context *ldb_module_get_ctx(struct ldb_module *module)
516 return module->ldb;
519 void *ldb_module_get_private(struct ldb_module *module)
521 return module->private_data;
524 void ldb_module_set_private(struct ldb_module *module, void *private_data)
526 module->private_data = private_data;
530 helper functions to call the next module in chain
533 int ldb_next_request(struct ldb_module *module, struct ldb_request *request)
535 int ret;
537 if (request->callback == NULL) {
538 ldb_set_errstring(module->ldb, "Requests MUST define callbacks");
539 return LDB_ERR_UNWILLING_TO_PERFORM;
542 switch (request->operation) {
543 case LDB_SEARCH:
544 FIND_OP(module, search);
545 ret = module->ops->search(module, request);
546 break;
547 case LDB_ADD:
548 FIND_OP(module, add);
549 ret = module->ops->add(module, request);
550 break;
551 case LDB_MODIFY:
552 FIND_OP(module, modify);
553 ret = module->ops->modify(module, request);
554 break;
555 case LDB_DELETE:
556 FIND_OP(module, del);
557 ret = module->ops->del(module, request);
558 break;
559 case LDB_RENAME:
560 FIND_OP(module, rename);
561 ret = module->ops->rename(module, request);
562 break;
563 case LDB_EXTENDED:
564 FIND_OP(module, extended);
565 ret = module->ops->extended(module, request);
566 break;
567 default:
568 FIND_OP(module, request);
569 ret = module->ops->request(module, request);
570 break;
572 if (ret == LDB_SUCCESS) {
573 return ret;
575 if (!ldb_errstring(module->ldb)) {
576 /* Set a default error string, to place the blame somewhere */
577 ldb_asprintf_errstring(module->ldb, "error in module %s: %s (%d)", module->ops->name, ldb_strerror(ret), ret);
579 return ret;
582 int ldb_next_init(struct ldb_module *module)
584 module = module->next;
586 return ldb_init_module_chain(module->ldb, module);
589 int ldb_next_start_trans(struct ldb_module *module)
591 FIND_OP(module, start_transaction);
592 return module->ops->start_transaction(module);
595 int ldb_next_end_trans(struct ldb_module *module)
597 FIND_OP(module, end_transaction);
598 return module->ops->end_transaction(module);
601 int ldb_next_del_trans(struct ldb_module *module)
603 FIND_OP(module, del_transaction);
604 return module->ops->del_transaction(module);
607 struct ldb_handle *ldb_handle_new(TALLOC_CTX *mem_ctx, struct ldb_context *ldb)
609 struct ldb_handle *h;
611 h = talloc_zero(mem_ctx, struct ldb_handle);
612 if (h == NULL) {
613 ldb_set_errstring(ldb, "Out of Memory");
614 return NULL;
617 h->status = LDB_SUCCESS;
618 h->state = LDB_ASYNC_INIT;
619 h->ldb = ldb;
621 return h;
624 /* calls the request callback to send an entry
626 * params:
627 * req: the original request passed to your module
628 * msg: reply message (must be a talloc pointer, and it will be stolen
629 * on the ldb_reply that is sent to the callback)
630 * ctrls: controls to send in the reply (must be a talloc pointer, and it will be stolen
631 * on the ldb_reply that is sent to the callback)
634 int ldb_module_send_entry(struct ldb_request *req,
635 struct ldb_message *msg,
636 struct ldb_control **ctrls)
638 struct ldb_reply *ares;
640 ares = talloc_zero(req, struct ldb_reply);
641 if (!ares) {
642 ldb_oom(req->handle->ldb);
643 req->callback(req, NULL);
644 return LDB_ERR_OPERATIONS_ERROR;
646 ares->type = LDB_REPLY_ENTRY;
647 ares->message = talloc_steal(ares, msg);
648 ares->controls = talloc_steal(ares, ctrls);
649 ares->error = LDB_SUCCESS;
651 return req->callback(req, ares);
654 /* calls the request callback to send an referrals
656 * params:
657 * req: the original request passed to your module
658 * ref: referral string (must be a talloc pointeri, steal)
661 int ldb_module_send_referral(struct ldb_request *req,
662 char *ref)
664 struct ldb_reply *ares;
666 ares = talloc_zero(req, struct ldb_reply);
667 if (!ares) {
668 ldb_oom(req->handle->ldb);
669 req->callback(req, NULL);
670 return LDB_ERR_OPERATIONS_ERROR;
672 ares->type = LDB_REPLY_REFERRAL;
673 ares->referral = talloc_steal(ares, ref);
674 ares->error = LDB_SUCCESS;
676 return req->callback(req, ares);
679 /* calls the original request callback
681 * params:
682 * req: the original request passed to your module
683 * ctrls: controls to send in the reply (must be a talloc pointer, steal)
684 * response: results for extended request (steal)
685 * error: LDB_SUCCESS for a succesful return
686 * any other ldb error otherwise
688 int ldb_module_done(struct ldb_request *req,
689 struct ldb_control **ctrls,
690 struct ldb_extended *response,
691 int error)
693 struct ldb_reply *ares;
695 ares = talloc_zero(req, struct ldb_reply);
696 if (!ares) {
697 ldb_oom(req->handle->ldb);
698 req->callback(req, NULL);
699 return LDB_ERR_OPERATIONS_ERROR;
701 ares->type = LDB_REPLY_DONE;
702 ares->controls = talloc_steal(ares, ctrls);
703 ares->response = talloc_steal(ares, response);
704 ares->error = error;
706 req->callback(req, ares);
707 return error;
710 /* to be used *only* in modules init functions.
711 * this function i synchronous and will register
712 * the requested OID in the rootdse module if present
713 * otherwise it will return an error */
714 int ldb_mod_register_control(struct ldb_module *module, const char *oid)
716 struct ldb_request *req;
717 int ret;
719 req = talloc_zero(module, struct ldb_request);
720 if (req == NULL) {
721 return LDB_ERR_OPERATIONS_ERROR;
724 req->operation = LDB_REQ_REGISTER_CONTROL;
725 req->op.reg_control.oid = oid;
726 req->callback = ldb_op_default_callback;
728 ldb_set_timeout(module->ldb, req, 0);
730 req->handle = ldb_handle_new(req, module->ldb);
731 if (req->handle == NULL) {
732 return LDB_ERR_OPERATIONS_ERROR;
735 ret = ldb_request(module->ldb, req);
736 if (ret == LDB_SUCCESS) {
737 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
739 talloc_free(req);
741 return ret;
744 #ifndef STATIC_LIBLDB_MODULES
746 #ifdef HAVE_LDB_LDAP
747 #define LDAP_BACKEND LDB_BACKEND(ldap), LDB_BACKEND(ldapi), LDB_BACKEND(ldaps),
748 #else
749 #define LDAP_BACKEND
750 #endif
752 #ifdef HAVE_LDB_SQLITE3
753 #define SQLITE3_BACKEND LDB_BACKEND(sqlite3),
754 #else
755 #define SQLITE3_BACKEND
756 #endif
758 #define STATIC_LIBLDB_MODULES \
759 LDB_BACKEND(tdb), \
760 LDAP_BACKEND \
761 SQLITE3_BACKEND \
762 LDB_MODULE(operational), \
763 LDB_MODULE(rdn_name), \
764 LDB_MODULE(paged_results), \
765 LDB_MODULE(server_sort), \
766 LDB_MODULE(asq), \
767 NULL
768 #endif
771 * this is a bit hacked, as STATIC_LIBLDB_MODULES contains ','
772 * between the elements and we want to autogenerate the
773 * extern struct declarations, so we do some hacks and let the
774 * ',' appear in an unused function prototype.
776 #undef NULL
777 #define NULL LDB_MODULE(NULL),
779 #define LDB_BACKEND(name) \
780 int); \
781 extern const struct ldb_backend_ops ldb_ ## name ## _backend_ops;\
782 extern void ldb_noop ## name (int
783 #define LDB_MODULE(name) \
784 int); \
785 extern const struct ldb_module_ops ldb_ ## name ## _module_ops;\
786 extern void ldb_noop ## name (int
788 extern void ldb_start_noop(int,
789 STATIC_LIBLDB_MODULES
790 int);
792 #undef NULL
793 #define NULL { \
794 .backend_ops = (void *)0, \
795 .module_ops = (void *)0 \
798 #undef LDB_BACKEND
799 #define LDB_BACKEND(name) { \
800 .backend_ops = &ldb_ ## name ## _backend_ops, \
801 .module_ops = (void *)0 \
803 #undef LDB_MODULE
804 #define LDB_MODULE(name) { \
805 .backend_ops = (void *)0, \
806 .module_ops = &ldb_ ## name ## _module_ops \
809 static const struct ldb_builtins builtins[] = {
810 STATIC_LIBLDB_MODULES