s4-ldb: added ldb_module_get_ops()
[Samba/fernandojvsilva.git] / source4 / lib / ldb / common / ldb_modules.c
blobe49d46a987ee4d3c6c1b721a1ecd66be15ce89a6
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"
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,
41 const char *symbol);
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)
51 int i, len;
52 char *trimmed;
54 trimmed = talloc_strdup(mem_ctx, string);
55 if (!trimmed) {
56 return NULL;
59 len = strlen(trimmed);
60 for (i = 0; trimmed[i] != '\0'; i++) {
61 switch (trimmed[i]) {
62 case ' ':
63 case '\t':
64 case '\n':
65 memmove(&trimmed[i], &trimmed[i + 1], len -i -1);
66 break;
70 return trimmed;
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;
80 const char **m;
81 char *modstr, *p;
82 int i;
84 /* spaces not admitted */
85 modstr = ldb_modules_strdup_no_spaces(mem_ctx, string);
86 if ( ! modstr) {
87 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_strdup_no_spaces()");
88 return NULL;
91 modules = talloc_realloc(mem_ctx, modules, char *, 2);
92 if ( ! modules ) {
93 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()");
94 talloc_free(modstr);
95 return NULL;
97 talloc_steal(modules, modstr);
99 if (modstr[0] == '\0') {
100 modules[0] = NULL;
101 m = (const char **)modules;
102 return m;
105 i = 0;
106 /* The str*r*chr walks backwards: This is how we get the inverse order mentioned above */
107 while ((p = strrchr(modstr, ',')) != NULL) {
108 *p = '\0';
109 p++;
110 modules[i] = p;
112 i++;
113 modules = talloc_realloc(mem_ctx, modules, char *, i + 2);
114 if ( ! modules ) {
115 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()");
116 return NULL;
120 modules[i] = modstr;
122 modules[i + 1] = NULL;
124 m = (const char **)modules;
126 return m;
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;
142 } builtins[];
144 static ldb_connect_fn ldb_find_backend(const char *url)
146 struct backends_list_entry *backend;
147 int i;
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;
165 return NULL;
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);
180 if (!entry) {
181 talloc_free(backend);
182 return LDB_ERR_OPERATIONS_ERROR;
185 if (ldb_find_backend(url_prefix)) {
186 return LDB_SUCCESS;
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);
196 return LDB_SUCCESS;
200 Return the ldb module form of a database.
201 The URL can either be one of the following forms
202 ldb://path
203 ldapi://path
205 flags is made up of LDB_FLG_*
207 the options are passed uninterpreted to the backend, and are
208 backend specific.
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,
214 const char *url,
215 const char *options[],
216 struct ldb_module **backend_module)
218 int ret;
219 char *backend;
220 ldb_connect_fn fn;
222 if (strchr(url, ':') != NULL) {
223 backend = talloc_strndup(ldb, url, strchr(url, ':')-url);
224 } else {
225 /* Default to tdb */
226 backend = talloc_strdup(ldb, "tdb");
229 fn = ldb_find_backend(backend);
231 if (fn == NULL) {
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);
238 if (ops != NULL) {
239 fn = ops->connect_fn;
241 talloc_free(symbol_name);
244 talloc_free(backend);
246 if (fn == NULL) {
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);
257 return ret;
259 return ret;
262 static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
264 struct ops_list_entry *e;
265 int i;
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)
276 return e->ops;
279 return NULL;
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)
288 return -1;
290 if (entry == NULL)
291 return -1;
293 entry->ops = ops;
294 entry->next = registered_modules;
295 registered_modules = entry;
297 return 0;
300 static void *ldb_dso_load_symbol(struct ldb_context *ldb, const char *name,
301 const char *symbol)
303 char *path;
304 void *handle;
305 void *sym;
307 if (ldb->modules_dir == NULL)
308 return NULL;
310 path = talloc_asprintf(ldb, "%s/%s.%s", ldb->modules_dir, name,
311 SHLIBEXT);
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());
318 return NULL;
321 sym = (int (*)(void))dlsym(handle, symbol);
323 if (sym == NULL) {
324 ldb_debug(ldb, LDB_DEBUG_ERROR, "no symbol `%s' found in %s: %s", symbol, path, dlerror());
325 return NULL;
328 talloc_free(path);
330 return sym;
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;
336 int i;
338 module = backend;
340 for (i = 0; module_list && module_list[i] != NULL; i++) {
341 struct ldb_module *current;
342 const struct ldb_module_ops *ops;
344 if (strcmp(module_list[i], "") == 0) {
345 continue;
348 ops = ldb_find_module_ops(module_list[i]);
349 if (ops == NULL) {
350 char *symbol_name = talloc_asprintf(ldb, "ldb_%s_module_ops",
351 module_list[i]);
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);
359 if (ops == NULL) {
360 ldb_debug(ldb, LDB_DEBUG_WARNING, "WARNING: Module [%s] not found",
361 module_list[i]);
362 continue;
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]);
371 current->ldb = ldb;
372 current->ops = ops;
374 DLIST_ADD(module, current);
376 *out = module;
377 return LDB_SUCCESS;
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 */
388 if (module) {
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);
392 return ret;
396 return LDB_SUCCESS;
399 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
401 const char **modules = NULL;
402 int i;
403 int ret;
404 TALLOC_CTX *mem_ctx = talloc_new(ldb);
405 if (!mem_ctx) {
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 */
412 if (options) {
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);
429 return -1;
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);
439 return ret;
440 } else {
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);
447 return -1;
448 } else {
449 module_list = ldb_msg_find_attr_as_string(res->msgs[0], "@LIST", NULL);
450 if (!module_list) {
451 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
453 modules = ldb_modules_list_from_string(ldb, mem_ctx,
454 module_list);
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);
465 return ret;
467 } else {
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);
473 return ret;
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
479 when ldb is extended
481 #define FIND_OP_NOERR(module, op) do { \
482 module = module->next; \
483 while (module && module->ops->op == NULL) module = module->next; \
484 if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) { \
485 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "ldb_trace_next_request: (%s)->" #op, \
486 module->ops->name); \
488 } while (0)
490 #define FIND_OP(module, op) do { \
491 struct ldb_context *ldb = module->ldb; \
492 FIND_OP_NOERR(module, op); \
493 if (module == NULL) { \
494 ldb_asprintf_errstring(ldb, "Unable to find backend operation for " #op ); \
495 return LDB_ERR_OPERATIONS_ERROR; \
497 } while (0)
500 struct ldb_module *ldb_module_new(TALLOC_CTX *memctx,
501 struct ldb_context *ldb,
502 const char *module_name,
503 const struct ldb_module_ops *ops)
505 struct ldb_module *module;
507 module = talloc(memctx, struct ldb_module);
508 if (!module) {
509 ldb_oom(ldb);
510 return NULL;
512 talloc_set_name_const(module, module_name);
513 module->ldb = ldb;
514 module->prev = module->next = NULL;
515 module->ops = ops;
517 return module;
520 const char * ldb_module_get_name(struct ldb_module *module)
522 return module->ops->name;
525 struct ldb_context *ldb_module_get_ctx(struct ldb_module *module)
527 return module->ldb;
530 const struct ldb_module_ops *ldb_module_get_ops(struct ldb_module *module)
532 return module->ops;
535 void *ldb_module_get_private(struct ldb_module *module)
537 return module->private_data;
540 void ldb_module_set_private(struct ldb_module *module, void *private_data)
542 module->private_data = private_data;
546 helper functions to call the next module in chain
549 int ldb_next_request(struct ldb_module *module, struct ldb_request *request)
551 int ret;
553 if (request->callback == NULL) {
554 ldb_set_errstring(module->ldb, "Requests MUST define callbacks");
555 return LDB_ERR_UNWILLING_TO_PERFORM;
558 request->handle->nesting++;
560 switch (request->operation) {
561 case LDB_SEARCH:
562 FIND_OP(module, search);
563 ret = module->ops->search(module, request);
564 break;
565 case LDB_ADD:
566 FIND_OP(module, add);
567 ret = module->ops->add(module, request);
568 break;
569 case LDB_MODIFY:
570 FIND_OP(module, modify);
571 ret = module->ops->modify(module, request);
572 break;
573 case LDB_DELETE:
574 FIND_OP(module, del);
575 ret = module->ops->del(module, request);
576 break;
577 case LDB_RENAME:
578 FIND_OP(module, rename);
579 ret = module->ops->rename(module, request);
580 break;
581 case LDB_EXTENDED:
582 FIND_OP(module, extended);
583 ret = module->ops->extended(module, request);
584 break;
585 default:
586 FIND_OP(module, request);
587 ret = module->ops->request(module, request);
588 break;
591 request->handle->nesting--;
593 if (ret == LDB_SUCCESS) {
594 return ret;
596 if (!ldb_errstring(module->ldb)) {
597 /* Set a default error string, to place the blame somewhere */
598 ldb_asprintf_errstring(module->ldb, "error in module %s: %s (%d)", module->ops->name, ldb_strerror(ret), ret);
601 if (!(request->handle->flags & LDB_HANDLE_FLAG_DONE_CALLED)) {
602 /* It is _extremely_ common that a module returns a
603 * failure without calling ldb_module_done(), but that
604 * guarantees we will end up hanging in
605 * ldb_wait(). This fixes it without having to rewrite
606 * all our modules, and leaves us one less sharp
607 * corner for module developers to cut themselves on
609 ldb_module_done(request, NULL, NULL, ret);
611 return ret;
614 int ldb_next_init(struct ldb_module *module)
616 module = module->next;
618 return ldb_init_module_chain(module->ldb, module);
621 int ldb_next_start_trans(struct ldb_module *module)
623 int ret;
624 FIND_OP(module, start_transaction);
625 ret = module->ops->start_transaction(module);
626 if (ret == LDB_SUCCESS) {
627 return ret;
629 if (!ldb_errstring(module->ldb)) {
630 /* Set a default error string, to place the blame somewhere */
631 ldb_asprintf_errstring(module->ldb, "start_trans error in module %s: %s (%d)", module->ops->name, ldb_strerror(ret), ret);
633 if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
634 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "ldb_next_start_trans error: %s",
635 ldb_errstring(module->ldb));
637 return ret;
640 int ldb_next_end_trans(struct ldb_module *module)
642 int ret;
643 FIND_OP(module, end_transaction);
644 ret = module->ops->end_transaction(module);
645 if (ret == LDB_SUCCESS) {
646 return ret;
648 if (!ldb_errstring(module->ldb)) {
649 /* Set a default error string, to place the blame somewhere */
650 ldb_asprintf_errstring(module->ldb, "end_trans error in module %s: %s (%d)", module->ops->name, ldb_strerror(ret), ret);
652 if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
653 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "ldb_next_end_trans error: %s",
654 ldb_errstring(module->ldb));
656 return ret;
659 int ldb_next_prepare_commit(struct ldb_module *module)
661 int ret;
662 FIND_OP_NOERR(module, prepare_commit);
663 if (module == NULL) {
664 /* we are allowed to have no prepare commit in
665 backends */
666 return LDB_SUCCESS;
668 ret = module->ops->prepare_commit(module);
669 if (ret == LDB_SUCCESS) {
670 return ret;
672 if (!ldb_errstring(module->ldb)) {
673 /* Set a default error string, to place the blame somewhere */
674 ldb_asprintf_errstring(module->ldb, "prepare_commit error in module %s: %s (%d)", module->ops->name, ldb_strerror(ret), ret);
676 if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
677 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "ldb_next_prepare_commit error: %s",
678 ldb_errstring(module->ldb));
680 return ret;
683 int ldb_next_del_trans(struct ldb_module *module)
685 int ret;
686 FIND_OP(module, del_transaction);
687 ret = module->ops->del_transaction(module);
688 if (ret == LDB_SUCCESS) {
689 return ret;
691 if (!ldb_errstring(module->ldb)) {
692 /* Set a default error string, to place the blame somewhere */
693 ldb_asprintf_errstring(module->ldb, "del_trans error in module %s: %s (%d)", module->ops->name, ldb_strerror(ret), ret);
695 if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
696 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "ldb_next_del_trans error: %s",
697 ldb_errstring(module->ldb));
699 return ret;
702 struct ldb_handle *ldb_handle_new(TALLOC_CTX *mem_ctx, struct ldb_context *ldb)
704 struct ldb_handle *h;
706 h = talloc_zero(mem_ctx, struct ldb_handle);
707 if (h == NULL) {
708 ldb_set_errstring(ldb, "Out of Memory");
709 return NULL;
712 h->status = LDB_SUCCESS;
713 h->state = LDB_ASYNC_INIT;
714 h->ldb = ldb;
715 h->flags = 0;
717 return h;
720 /* calls the request callback to send an entry
722 * params:
723 * req: the original request passed to your module
724 * msg: reply message (must be a talloc pointer, and it will be stolen
725 * on the ldb_reply that is sent to the callback)
726 * ctrls: controls to send in the reply (must be a talloc pointer, and it will be stolen
727 * on the ldb_reply that is sent to the callback)
730 int ldb_module_send_entry(struct ldb_request *req,
731 struct ldb_message *msg,
732 struct ldb_control **ctrls)
734 struct ldb_reply *ares;
736 ares = talloc_zero(req, struct ldb_reply);
737 if (!ares) {
738 ldb_oom(req->handle->ldb);
739 req->callback(req, NULL);
740 return LDB_ERR_OPERATIONS_ERROR;
742 ares->type = LDB_REPLY_ENTRY;
743 ares->message = talloc_steal(ares, msg);
744 ares->controls = talloc_steal(ares, ctrls);
745 ares->error = LDB_SUCCESS;
747 if ((req->handle->ldb->flags & LDB_FLG_ENABLE_TRACING) &&
748 req->handle->nesting == 0) {
749 char *s;
750 ldb_debug_add(req->handle->ldb, "ldb_trace_response: ENTRY\n");
751 s = ldb_ldif_message_string(req->handle->ldb, msg, LDB_CHANGETYPE_NONE, msg);
752 ldb_debug_add(req->handle->ldb, "%s\n", s);
753 talloc_free(s);
754 ldb_debug_end(req->handle->ldb, LDB_DEBUG_TRACE);
757 return req->callback(req, ares);
760 /* calls the request callback to send an referrals
762 * params:
763 * req: the original request passed to your module
764 * ref: referral string (must be a talloc pointeri, steal)
767 int ldb_module_send_referral(struct ldb_request *req,
768 char *ref)
770 struct ldb_reply *ares;
772 ares = talloc_zero(req, struct ldb_reply);
773 if (!ares) {
774 ldb_oom(req->handle->ldb);
775 req->callback(req, NULL);
776 return LDB_ERR_OPERATIONS_ERROR;
778 ares->type = LDB_REPLY_REFERRAL;
779 ares->referral = talloc_steal(ares, ref);
780 ares->error = LDB_SUCCESS;
782 if ((req->handle->ldb->flags & LDB_FLG_ENABLE_TRACING) &&
783 req->handle->nesting == 0) {
784 ldb_debug_add(req->handle->ldb, "ldb_trace_response: REFERRAL\n");
785 ldb_debug_add(req->handle->ldb, "ref: %s\n", ref);
786 ldb_debug_end(req->handle->ldb, LDB_DEBUG_TRACE);
789 return req->callback(req, ares);
792 /* calls the original request callback
794 * params:
795 * req: the original request passed to your module
796 * ctrls: controls to send in the reply (must be a talloc pointer, steal)
797 * response: results for extended request (steal)
798 * error: LDB_SUCCESS for a succesful return
799 * any other ldb error otherwise
801 int ldb_module_done(struct ldb_request *req,
802 struct ldb_control **ctrls,
803 struct ldb_extended *response,
804 int error)
806 struct ldb_reply *ares;
808 ares = talloc_zero(req, struct ldb_reply);
809 if (!ares) {
810 ldb_oom(req->handle->ldb);
811 req->callback(req, NULL);
812 return LDB_ERR_OPERATIONS_ERROR;
814 ares->type = LDB_REPLY_DONE;
815 ares->controls = talloc_steal(ares, ctrls);
816 ares->response = talloc_steal(ares, response);
817 ares->error = error;
819 req->handle->flags |= LDB_HANDLE_FLAG_DONE_CALLED;
821 if ((req->handle->ldb->flags & LDB_FLG_ENABLE_TRACING) &&
822 req->handle->nesting == 0) {
823 ldb_debug_add(req->handle->ldb, "ldb_trace_response: DONE\n");
824 ldb_debug_add(req->handle->ldb, "error: %u\n", error);
825 if (ldb_errstring(req->handle->ldb)) {
826 ldb_debug_add(req->handle->ldb, "msg: %s\n",
827 ldb_errstring(req->handle->ldb));
829 ldb_debug_end(req->handle->ldb, LDB_DEBUG_TRACE);
832 req->callback(req, ares);
833 return error;
836 /* to be used *only* in modules init functions.
837 * this function i synchronous and will register
838 * the requested OID in the rootdse module if present
839 * otherwise it will return an error */
840 int ldb_mod_register_control(struct ldb_module *module, const char *oid)
842 struct ldb_request *req;
843 int ret;
845 req = talloc_zero(module, struct ldb_request);
846 if (req == NULL) {
847 return LDB_ERR_OPERATIONS_ERROR;
850 req->operation = LDB_REQ_REGISTER_CONTROL;
851 req->op.reg_control.oid = oid;
852 req->callback = ldb_op_default_callback;
854 ldb_set_timeout(module->ldb, req, 0);
856 req->handle = ldb_handle_new(req, module->ldb);
857 if (req->handle == NULL) {
858 return LDB_ERR_OPERATIONS_ERROR;
861 ret = ldb_request(module->ldb, req);
862 if (ret == LDB_SUCCESS) {
863 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
865 talloc_free(req);
867 return ret;
870 #ifndef STATIC_LIBLDB_MODULES
872 #ifdef HAVE_LDB_LDAP
873 #define LDAP_BACKEND LDB_BACKEND(ldap), LDB_BACKEND(ldapi), LDB_BACKEND(ldaps),
874 #else
875 #define LDAP_BACKEND
876 #endif
878 #ifdef HAVE_LDB_SQLITE3
879 #define SQLITE3_BACKEND LDB_BACKEND(sqlite3),
880 #else
881 #define SQLITE3_BACKEND
882 #endif
884 #define STATIC_LIBLDB_MODULES \
885 LDB_BACKEND(tdb), \
886 LDAP_BACKEND \
887 SQLITE3_BACKEND \
888 LDB_MODULE(rdn_name), \
889 LDB_MODULE(paged_results), \
890 LDB_MODULE(server_sort), \
891 LDB_MODULE(asq), \
892 NULL
893 #endif
896 * this is a bit hacked, as STATIC_LIBLDB_MODULES contains ','
897 * between the elements and we want to autogenerate the
898 * extern struct declarations, so we do some hacks and let the
899 * ',' appear in an unused function prototype.
901 #undef NULL
902 #define NULL LDB_MODULE(NULL),
904 #define LDB_BACKEND(name) \
905 int); \
906 extern const struct ldb_backend_ops ldb_ ## name ## _backend_ops;\
907 extern void ldb_noop ## name (int
908 #define LDB_MODULE(name) \
909 int); \
910 extern const struct ldb_module_ops ldb_ ## name ## _module_ops;\
911 extern void ldb_noop ## name (int
913 extern void ldb_start_noop(int,
914 STATIC_LIBLDB_MODULES
915 int);
917 #undef NULL
918 #define NULL { \
919 .backend_ops = (void *)0, \
920 .module_ops = (void *)0 \
923 #undef LDB_BACKEND
924 #define LDB_BACKEND(name) { \
925 .backend_ops = &ldb_ ## name ## _backend_ops, \
926 .module_ops = (void *)0 \
928 #undef LDB_MODULE
929 #define LDB_MODULE(name) { \
930 .backend_ops = (void *)0, \
931 .module_ops = &ldb_ ## name ## _module_ops \
934 static const struct ldb_builtins builtins[] = {
935 STATIC_LIBLDB_MODULES