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"
36 #include "system/dir.h"
38 static char *ldb_modules_strdup_no_spaces(TALLOC_CTX
*mem_ctx
, const char *string
)
43 trimmed
= talloc_strdup(mem_ctx
, string
);
48 len
= strlen(trimmed
);
49 for (i
= 0; trimmed
[i
] != '\0'; i
++) {
54 memmove(&trimmed
[i
], &trimmed
[i
+ 1], len
-i
-1);
63 /* modules are called in inverse order on the stack.
64 Lets place them as an admin would think the right order is.
65 Modules order is important */
66 const char **ldb_modules_list_from_string(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, const char *string
)
68 char **modules
= NULL
;
73 /* spaces not admitted */
74 modstr
= ldb_modules_strdup_no_spaces(mem_ctx
, string
);
76 ldb_debug(ldb
, LDB_DEBUG_FATAL
, "Out of Memory in ldb_modules_strdup_no_spaces()");
80 modules
= talloc_realloc(mem_ctx
, modules
, char *, 2);
82 ldb_debug(ldb
, LDB_DEBUG_FATAL
, "Out of Memory in ldb_modules_list_from_string()");
86 talloc_steal(modules
, modstr
);
88 if (modstr
[0] == '\0') {
90 m
= discard_const_p(const char *, modules
);
95 /* The str*r*chr walks backwards: This is how we get the inverse order mentioned above */
96 while ((p
= strrchr(modstr
, ',')) != NULL
) {
102 modules
= talloc_realloc(mem_ctx
, modules
, char *, i
+ 2);
104 ldb_debug(ldb
, LDB_DEBUG_FATAL
, "Out of Memory in ldb_modules_list_from_string()");
111 modules
[i
+ 1] = NULL
;
113 m
= discard_const_p(const char *, modules
);
118 static struct backends_list_entry
{
119 struct ldb_backend_ops
*ops
;
120 struct backends_list_entry
*prev
, *next
;
121 } *ldb_backends
= NULL
;
123 static struct ops_list_entry
{
124 const struct ldb_module_ops
*ops
;
125 struct ops_list_entry
*next
;
126 } *registered_modules
= NULL
;
128 static struct backends_list_entry
*ldb_find_backend(const char *url_prefix
)
130 struct backends_list_entry
*backend
;
132 for (backend
= ldb_backends
; backend
; backend
= backend
->next
) {
133 if (strcmp(backend
->ops
->name
, url_prefix
) == 0) {
142 register a new ldb backend
144 if override is true, then override any existing backend for this prefix
146 int ldb_register_backend(const char *url_prefix
, ldb_connect_fn connectfn
, bool override
)
148 struct backends_list_entry
*be
;
150 be
= ldb_find_backend(url_prefix
);
156 be
= talloc_zero(ldb_backends
, struct backends_list_entry
);
158 return LDB_ERR_OPERATIONS_ERROR
;
160 be
->ops
= talloc_zero(be
, struct ldb_backend_ops
);
163 return LDB_ERR_OPERATIONS_ERROR
;
165 DLIST_ADD_END(ldb_backends
, be
);
168 be
->ops
->name
= url_prefix
;
169 be
->ops
->connect_fn
= connectfn
;
175 Return the ldb module form of a database.
176 The URL looks something like this:
181 PATH (unadorned PATH defaults to tdb://)
183 for a complete list of backends (including possibly unmaintained ones) grep
184 for calls to ldb_register_backend().
186 the options are passed uninterpreted to the backend, and are
189 This allows modules to get at only the backend module, for example where a
190 module may wish to direct certain requests at a particular backend.
192 int ldb_module_connect_backend(struct ldb_context
*ldb
,
194 const char *options
[],
195 struct ldb_module
**backend_module
)
199 struct backends_list_entry
*be
;
202 colon
= strchr(url
, ':');
204 backend
= talloc_strndup(ldb
, url
, colon
-url
);
207 backend
= talloc_strdup(ldb
, "tdb");
209 if (backend
== NULL
) {
213 be
= ldb_find_backend(backend
);
215 talloc_free(backend
);
218 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
219 "Unable to find backend for '%s' - do you need to set LDB_MODULES_PATH?", url
);
220 return LDB_ERR_OTHER
;
223 ret
= be
->ops
->connect_fn(ldb
, url
, ldb
->flags
, options
, backend_module
);
225 if (ret
!= LDB_SUCCESS
) {
226 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
227 "Failed to connect to '%s' with backend '%s': %s", url
, be
->ops
->name
, ldb_errstring(ldb
));
233 static struct ldb_hooks
{
234 struct ldb_hooks
*next
, *prev
;
239 register a ldb hook function
241 int ldb_register_hook(ldb_hook_fn hook_fn
)
243 struct ldb_hooks
*lc
;
244 lc
= talloc_zero(ldb_hooks
, struct ldb_hooks
);
246 return LDB_ERR_OPERATIONS_ERROR
;
248 lc
->hook_fn
= hook_fn
;
249 DLIST_ADD_END(ldb_hooks
, lc
);
254 call ldb hooks of a given type
256 int ldb_modules_hook(struct ldb_context
*ldb
, enum ldb_module_hook_type t
)
258 struct ldb_hooks
*lc
;
259 for (lc
= ldb_hooks
; lc
; lc
=lc
->next
) {
260 int ret
= lc
->hook_fn(ldb
, t
);
261 if (ret
!= LDB_SUCCESS
) {
269 static const struct ldb_module_ops
*ldb_find_module_ops(const char *name
)
271 struct ops_list_entry
*e
;
273 for (e
= registered_modules
; e
; e
= e
->next
) {
274 if (strcmp(e
->ops
->name
, name
) == 0)
282 int ldb_register_module(const struct ldb_module_ops
*ops
)
284 struct ops_list_entry
*entry
;
286 if (ldb_find_module_ops(ops
->name
) != NULL
)
287 return LDB_ERR_ENTRY_ALREADY_EXISTS
;
290 * ldb modules are not (yet) unloaded and
291 * are only loaded once (the above check
292 * makes sure of this). Allocate off the NULL
293 * context. We never want this to be freed
294 * until process shutdown. If eventually we
295 * want to unload ldb modules we can add a
296 * deregister function that walks and
299 entry
= talloc(NULL
, struct ops_list_entry
);
301 return LDB_ERR_OPERATIONS_ERROR
;
305 entry
->next
= registered_modules
;
306 registered_modules
= entry
;
312 load a list of modules
314 int ldb_module_load_list(struct ldb_context
*ldb
, const char **module_list
,
315 struct ldb_module
*backend
, struct ldb_module
**out
)
317 struct ldb_module
*module
;
322 for (i
= 0; module_list
&& module_list
[i
] != NULL
; i
++) {
323 struct ldb_module
*current
;
324 const struct ldb_module_ops
*ops
;
326 if (strcmp(module_list
[i
], "") == 0) {
330 ops
= ldb_find_module_ops(module_list
[i
]);
333 ldb_debug(ldb
, LDB_DEBUG_FATAL
, "WARNING: Module [%s] not found - do you need to set LDB_MODULES_PATH?",
335 return LDB_ERR_OPERATIONS_ERROR
;
338 current
= talloc_zero(ldb
, struct ldb_module
);
339 if (current
== NULL
) {
340 return LDB_ERR_OPERATIONS_ERROR
;
342 talloc_set_name(current
, "ldb_module: %s", module_list
[i
]);
347 DLIST_ADD(module
, current
);
354 initialise a chain of modules
356 int ldb_module_init_chain(struct ldb_context
*ldb
, struct ldb_module
*module
)
358 while (module
&& module
->ops
->init_context
== NULL
)
359 module
= module
->next
;
361 /* init is different in that it is not an error if modules
362 * do not require initialization */
365 int ret
= module
->ops
->init_context(module
);
366 if (ret
!= LDB_SUCCESS
) {
367 ldb_debug(ldb
, LDB_DEBUG_FATAL
, "module %s initialization failed : %s",
368 module
->ops
->name
, ldb_strerror(ret
));
376 int ldb_load_modules(struct ldb_context
*ldb
, const char *options
[])
378 const char *modules_string
;
379 const char **modules
= NULL
;
381 TALLOC_CTX
*mem_ctx
= talloc_new(ldb
);
386 /* find out which modules we are requested to activate */
388 /* check if we have a custom module list passd as ldb option */
390 modules_string
= ldb_options_find(ldb
, options
, "modules");
391 if (modules_string
) {
392 modules
= ldb_modules_list_from_string(ldb
, mem_ctx
, modules_string
);
396 /* if not overloaded by options and the backend is not ldap try to load the modules list from ldb */
397 if ((modules
== NULL
) && (strcmp("ldap", ldb
->modules
->ops
->name
) != 0)) {
398 const char * const attrs
[] = { "@LIST" , NULL
};
399 struct ldb_result
*res
= NULL
;
400 struct ldb_dn
*mods_dn
;
402 mods_dn
= ldb_dn_new(mem_ctx
, ldb
, "@MODULES");
403 if (mods_dn
== NULL
) {
404 talloc_free(mem_ctx
);
408 ret
= ldb_search(ldb
, mods_dn
, &res
, mods_dn
, LDB_SCOPE_BASE
, attrs
, "@LIST=*");
410 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
411 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "no modules required by the db");
412 } else if (ret
!= LDB_SUCCESS
) {
413 ldb_debug(ldb
, LDB_DEBUG_FATAL
, "ldb error (%s) occurred searching for modules, bailing out", ldb_errstring(ldb
));
414 talloc_free(mem_ctx
);
417 const char *module_list
;
418 if (res
->count
== 0) {
419 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "no modules required by the db");
420 } else if (res
->count
> 1) {
421 ldb_debug(ldb
, LDB_DEBUG_FATAL
, "Too many records found (%u), bailing out", res
->count
);
422 talloc_free(mem_ctx
);
423 return LDB_ERR_OPERATIONS_ERROR
;
425 module_list
= ldb_msg_find_attr_as_string(res
->msgs
[0], "@LIST", NULL
);
427 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "no modules required by the db");
429 modules
= ldb_modules_list_from_string(ldb
, mem_ctx
,
434 talloc_free(mods_dn
);
437 if (modules
!= NULL
) {
438 ret
= ldb_module_load_list(ldb
, modules
, ldb
->modules
, &ldb
->modules
);
439 if (ret
!= LDB_SUCCESS
) {
440 talloc_free(mem_ctx
);
444 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "No modules specified for this database");
447 ret
= ldb_module_init_chain(ldb
, ldb
->modules
);
448 talloc_free(mem_ctx
);
453 by using this we allow ldb modules to only implement the functions they care about,
454 which makes writing a module simpler, and makes it more likely to keep working
457 #define FIND_OP_NOERR(module, op) do { \
458 module = module->next; \
459 while (module && module->ops->op == NULL) module = module->next; \
460 if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) { \
461 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "ldb_trace_next_request: (%s)->" #op, \
462 module->ops->name); \
466 #define FIND_OP(module, op) do { \
467 struct ldb_context *ldb = module->ldb; \
468 FIND_OP_NOERR(module, op); \
469 if (module == NULL) { \
470 ldb_asprintf_errstring(ldb, "Unable to find backend operation for " #op ); \
471 return LDB_ERR_OPERATIONS_ERROR; \
476 struct ldb_module
*ldb_module_new(TALLOC_CTX
*memctx
,
477 struct ldb_context
*ldb
,
478 const char *module_name
,
479 const struct ldb_module_ops
*ops
)
481 struct ldb_module
*module
;
483 module
= talloc(memctx
, struct ldb_module
);
488 talloc_set_name_const(module
, module_name
);
490 module
->prev
= module
->next
= NULL
;
496 const char * ldb_module_get_name(struct ldb_module
*module
)
498 return module
->ops
->name
;
501 struct ldb_context
*ldb_module_get_ctx(struct ldb_module
*module
)
506 const struct ldb_module_ops
*ldb_module_get_ops(struct ldb_module
*module
)
511 void *ldb_module_get_private(struct ldb_module
*module
)
513 return module
->private_data
;
516 void ldb_module_set_private(struct ldb_module
*module
, void *private_data
)
518 module
->private_data
= private_data
;
522 helper functions to call the next module in chain
525 int ldb_next_request(struct ldb_module
*module
, struct ldb_request
*request
)
529 if (request
->callback
== NULL
) {
530 ldb_set_errstring(module
->ldb
, "Requests MUST define callbacks");
531 return LDB_ERR_UNWILLING_TO_PERFORM
;
534 request
->handle
->nesting
++;
536 switch (request
->operation
) {
538 FIND_OP(module
, search
);
539 ret
= module
->ops
->search(module
, request
);
542 FIND_OP(module
, add
);
543 ret
= module
->ops
->add(module
, request
);
546 FIND_OP(module
, modify
);
547 ret
= module
->ops
->modify(module
, request
);
550 FIND_OP(module
, del
);
551 ret
= module
->ops
->del(module
, request
);
554 FIND_OP(module
, rename
);
555 ret
= module
->ops
->rename(module
, request
);
558 FIND_OP(module
, extended
);
559 ret
= module
->ops
->extended(module
, request
);
562 FIND_OP(module
, request
);
563 ret
= module
->ops
->request(module
, request
);
567 request
->handle
->nesting
--;
569 if (ret
== LDB_SUCCESS
) {
572 if (!ldb_errstring(module
->ldb
)) {
574 switch (request
->operation
) {
598 /* Set a default error string, to place the blame somewhere */
599 ldb_asprintf_errstring(module
->ldb
, "error in module %s: %s during %s (%d)", module
->ops
->name
, ldb_strerror(ret
), op
, ret
);
602 if (!(request
->handle
->flags
& LDB_HANDLE_FLAG_DONE_CALLED
)) {
603 /* It is _extremely_ common that a module returns a
604 * failure without calling ldb_module_done(), but that
605 * guarantees we will end up hanging in
606 * ldb_wait(). This fixes it without having to rewrite
607 * all our modules, and leaves us one less sharp
608 * corner for module developers to cut themselves on
610 ret
= ldb_module_done(request
, NULL
, NULL
, ret
);
615 int ldb_next_init(struct ldb_module
*module
)
617 module
= module
->next
;
619 return ldb_module_init_chain(module
->ldb
, module
);
622 int ldb_next_start_trans(struct ldb_module
*module
)
625 FIND_OP(module
, start_transaction
);
626 ret
= module
->ops
->start_transaction(module
);
627 if (ret
== LDB_SUCCESS
) {
630 if (!ldb_errstring(module
->ldb
)) {
631 /* Set a default error string, to place the blame somewhere */
632 ldb_asprintf_errstring(module
->ldb
, "start_trans error in module %s: %s (%d)", module
->ops
->name
, ldb_strerror(ret
), ret
);
634 if ((module
&& module
->ldb
->flags
& LDB_FLG_ENABLE_TRACING
)) {
635 ldb_debug(module
->ldb
, LDB_DEBUG_TRACE
, "ldb_next_start_trans error: %s",
636 ldb_errstring(module
->ldb
));
641 int ldb_next_end_trans(struct ldb_module
*module
)
644 FIND_OP(module
, end_transaction
);
645 ret
= module
->ops
->end_transaction(module
);
646 if (ret
== LDB_SUCCESS
) {
649 if (!ldb_errstring(module
->ldb
)) {
650 /* Set a default error string, to place the blame somewhere */
651 ldb_asprintf_errstring(module
->ldb
, "end_trans error in module %s: %s (%d)", module
->ops
->name
, ldb_strerror(ret
), ret
);
653 if ((module
&& module
->ldb
->flags
& LDB_FLG_ENABLE_TRACING
)) {
654 ldb_debug(module
->ldb
, LDB_DEBUG_TRACE
, "ldb_next_end_trans error: %s",
655 ldb_errstring(module
->ldb
));
660 int ldb_next_read_lock(struct ldb_module
*module
)
663 FIND_OP(module
, read_lock
);
664 ret
= module
->ops
->read_lock(module
);
665 if (ret
== LDB_SUCCESS
) {
668 if (!ldb_errstring(module
->ldb
)) {
669 /* Set a default error string, to place the blame somewhere */
670 ldb_asprintf_errstring(module
->ldb
,
671 "read_lock error in module %s: %s (%d)",
672 module
->ops
->name
, ldb_strerror(ret
),
675 if ((module
&& module
->ldb
->flags
& LDB_FLG_ENABLE_TRACING
)) {
676 ldb_debug(module
->ldb
, LDB_DEBUG_TRACE
,
677 "ldb_next_read_lock error: %s",
678 ldb_errstring(module
->ldb
));
683 int ldb_next_read_unlock(struct ldb_module
*module
)
686 FIND_OP(module
, read_unlock
);
687 ret
= module
->ops
->read_unlock(module
);
688 if (ret
== LDB_SUCCESS
) {
691 if (!ldb_errstring(module
->ldb
)) {
692 /* Set a default error string, to place the blame somewhere */
693 ldb_asprintf_errstring(module
->ldb
,
694 "read_unlock error in module %s: %s (%d)",
695 module
->ops
->name
, ldb_strerror(ret
),
698 if ((module
&& module
->ldb
->flags
& LDB_FLG_ENABLE_TRACING
)) {
699 ldb_debug(module
->ldb
, LDB_DEBUG_TRACE
,
700 "ldb_next_read_unlock error: %s",
701 ldb_errstring(module
->ldb
));
706 int ldb_next_prepare_commit(struct ldb_module
*module
)
709 FIND_OP_NOERR(module
, prepare_commit
);
710 if (module
== NULL
) {
711 /* we are allowed to have no prepare commit in
715 ret
= module
->ops
->prepare_commit(module
);
716 if (ret
== LDB_SUCCESS
) {
719 if (!ldb_errstring(module
->ldb
)) {
720 /* Set a default error string, to place the blame somewhere */
721 ldb_asprintf_errstring(module
->ldb
, "prepare_commit error in module %s: %s (%d)", module
->ops
->name
, ldb_strerror(ret
), ret
);
723 if ((module
&& module
->ldb
->flags
& LDB_FLG_ENABLE_TRACING
)) {
724 ldb_debug(module
->ldb
, LDB_DEBUG_TRACE
, "ldb_next_prepare_commit error: %s",
725 ldb_errstring(module
->ldb
));
730 int ldb_next_del_trans(struct ldb_module
*module
)
733 FIND_OP(module
, del_transaction
);
734 ret
= module
->ops
->del_transaction(module
);
735 if (ret
== LDB_SUCCESS
) {
738 if (!ldb_errstring(module
->ldb
)) {
739 /* Set a default error string, to place the blame somewhere */
740 ldb_asprintf_errstring(module
->ldb
, "del_trans error in module %s: %s (%d)", module
->ops
->name
, ldb_strerror(ret
), ret
);
742 if ((module
&& module
->ldb
->flags
& LDB_FLG_ENABLE_TRACING
)) {
743 ldb_debug(module
->ldb
, LDB_DEBUG_TRACE
, "ldb_next_del_trans error: %s",
744 ldb_errstring(module
->ldb
));
749 /* calls the request callback to send an entry
752 * req: the original request passed to your module
753 * msg: reply message (must be a talloc pointer, and it will be stolen
754 * on the ldb_reply that is sent to the callback)
755 * ctrls: controls to send in the reply (must be a talloc pointer, and it will be stolen
756 * on the ldb_reply that is sent to the callback)
759 int ldb_module_send_entry(struct ldb_request
*req
,
760 struct ldb_message
*msg
,
761 struct ldb_control
**ctrls
)
763 struct ldb_reply
*ares
;
765 ares
= talloc_zero(req
, struct ldb_reply
);
767 ldb_oom(req
->handle
->ldb
);
768 req
->callback(req
, NULL
);
769 return LDB_ERR_OPERATIONS_ERROR
;
771 ares
->type
= LDB_REPLY_ENTRY
;
772 ares
->message
= talloc_steal(ares
, msg
);
773 ares
->controls
= talloc_steal(ares
, ctrls
);
774 ares
->error
= LDB_SUCCESS
;
776 if ((req
->handle
->ldb
->flags
& LDB_FLG_ENABLE_TRACING
) &&
777 req
->handle
->nesting
== 0) {
779 struct ldb_ldif ldif
;
781 ldif
.changetype
= LDB_CHANGETYPE_NONE
;
782 ldif
.msg
= discard_const_p(struct ldb_message
, msg
);
784 ldb_debug_add(req
->handle
->ldb
, "ldb_trace_response: ENTRY\n");
788 * ldb_ldif_write_redacted_trace_string() is CRITICAL
789 * for security. It ensures that we do not output
790 * passwords into debug logs
793 s
= ldb_ldif_write_redacted_trace_string(req
->handle
->ldb
, msg
, &ldif
);
794 ldb_debug_add(req
->handle
->ldb
, "%s\n", s
);
796 ldb_debug_end(req
->handle
->ldb
, LDB_DEBUG_TRACE
);
799 return req
->callback(req
, ares
);
802 /* calls the request callback to send a referral
805 * req: the original request passed to your module
806 * ref: referral string (must be a talloc pointer, steal)
809 int ldb_module_send_referral(struct ldb_request
*req
,
812 struct ldb_reply
*ares
;
814 ares
= talloc_zero(req
, struct ldb_reply
);
816 ldb_oom(req
->handle
->ldb
);
817 req
->callback(req
, NULL
);
818 return LDB_ERR_OPERATIONS_ERROR
;
820 ares
->type
= LDB_REPLY_REFERRAL
;
821 ares
->referral
= talloc_steal(ares
, ref
);
822 ares
->error
= LDB_SUCCESS
;
824 if ((req
->handle
->ldb
->flags
& LDB_FLG_ENABLE_TRACING
) &&
825 req
->handle
->nesting
== 0) {
826 ldb_debug_add(req
->handle
->ldb
, "ldb_trace_response: REFERRAL\n");
827 ldb_debug_add(req
->handle
->ldb
, "ref: %s\n", ref
);
828 ldb_debug_end(req
->handle
->ldb
, LDB_DEBUG_TRACE
);
831 return req
->callback(req
, ares
);
834 /* calls the original request callback
837 * req: the original request passed to your module
838 * ctrls: controls to send in the reply (must be a talloc pointer, steal)
839 * response: results for extended request (steal)
840 * error: LDB_SUCCESS for a successful return
841 * any other ldb error otherwise
843 int ldb_module_done(struct ldb_request
*req
,
844 struct ldb_control
**ctrls
,
845 struct ldb_extended
*response
,
848 struct ldb_reply
*ares
;
850 ares
= talloc_zero(req
, struct ldb_reply
);
852 ldb_oom(req
->handle
->ldb
);
853 req
->callback(req
, NULL
);
854 return LDB_ERR_OPERATIONS_ERROR
;
856 ares
->type
= LDB_REPLY_DONE
;
857 ares
->controls
= talloc_steal(ares
, ctrls
);
858 ares
->response
= talloc_steal(ares
, response
);
861 req
->handle
->flags
|= LDB_HANDLE_FLAG_DONE_CALLED
;
863 if ((req
->handle
->ldb
->flags
& LDB_FLG_ENABLE_TRACING
) &&
864 req
->handle
->nesting
== 0) {
865 ldb_debug_add(req
->handle
->ldb
, "ldb_trace_response: DONE\n");
866 ldb_debug_add(req
->handle
->ldb
, "error: %d\n", error
);
867 if (ldb_errstring(req
->handle
->ldb
)) {
868 ldb_debug_add(req
->handle
->ldb
, "msg: %s\n",
869 ldb_errstring(req
->handle
->ldb
));
871 ldb_debug_end(req
->handle
->ldb
, LDB_DEBUG_TRACE
);
874 return req
->callback(req
, ares
);
877 /* to be used *only* in modules init functions.
878 * this function is synchronous and will register
879 * the requested OID in the rootdse module if present
880 * otherwise it will return an error */
881 int ldb_mod_register_control(struct ldb_module
*module
, const char *oid
)
883 struct ldb_request
*req
;
886 req
= talloc_zero(module
, struct ldb_request
);
888 return LDB_ERR_OPERATIONS_ERROR
;
891 req
->operation
= LDB_REQ_REGISTER_CONTROL
;
892 req
->op
.reg_control
.oid
= oid
;
893 req
->callback
= ldb_op_default_callback
;
895 ldb_set_timeout(module
->ldb
, req
, 0);
897 req
->handle
= ldb_handle_new(req
, module
->ldb
);
898 if (req
->handle
== NULL
) {
899 return LDB_ERR_OPERATIONS_ERROR
;
902 ret
= ldb_request(module
->ldb
, req
);
903 if (ret
== LDB_SUCCESS
) {
904 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
911 static int ldb_modules_load_dir(const char *modules_dir
, const char *version
);
915 load one module. A static list of loaded module inode numbers is
916 used to prevent a module being loaded twice
918 dlopen() is used on the module, and dlsym() is then used to look for
919 a ldb_init_module() function. If present, that function is called
920 with the ldb version number as an argument.
922 The ldb_init_module() function will typically call
923 ldb_register_module() and ldb_register_backend() to register a
924 module or backend, but it may also be used to register command line
925 handling functions, ldif handlers or any other local
928 The ldb_init_module() function does not get a ldb_context passed in,
929 as modules will be used for multiple ldb context handles. The call
930 from the first ldb_init() is just a convenient way to ensure it is
933 static int ldb_modules_load_path(const char *path
, const char *version
)
936 int (*init_fn
)(const char *);
939 static struct loaded
{
940 struct loaded
*next
, *prev
;
948 bool deepbind_enabled
= (getenv("LDB_MODULES_ENABLE_DEEPBIND") != NULL
);
951 ret
= stat(path
, &st
);
953 fprintf(stderr
, "ldb: unable to stat module %s : %s\n", path
, strerror(errno
));
954 return LDB_ERR_UNAVAILABLE
;
957 for (le
=loaded
; le
; le
=le
->next
) {
958 if (le
->st_ino
== st
.st_ino
&&
959 le
->st_dev
== st
.st_dev
) {
960 /* its already loaded */
965 le
= talloc(loaded
, struct loaded
);
967 fprintf(stderr
, "ldb: unable to allocated loaded entry\n");
968 return LDB_ERR_UNAVAILABLE
;
971 le
->st_ino
= st
.st_ino
;
972 le
->st_dev
= st
.st_dev
;
974 DLIST_ADD_END(loaded
, le
);
976 /* if it is a directory, recurse */
977 if (S_ISDIR(st
.st_mode
)) {
978 return ldb_modules_load_dir(path
, version
);
981 dlopen_flags
= RTLD_NOW
;
984 * On systems where e.g. different kerberos libraries are used, like a
985 * mix of Heimdal and MIT Kerberos, LDB_MODULES_ENABLE_DEEPBIND should
986 * be set to avoid issues.
988 * By default Linux distributions only have one Kerberos library.
990 if (deepbind_enabled
) {
991 dlopen_flags
|= RTLD_DEEPBIND
;
995 handle
= dlopen(path
, dlopen_flags
);
996 if (handle
== NULL
) {
997 fprintf(stderr
, "ldb: unable to dlopen %s : %s\n", path
, dlerror());
1001 init_fn
= dlsym(handle
, "ldb_init_module");
1002 if (init_fn
== NULL
) {
1003 /* ignore it, it could be an old-style
1004 * module. Once we've converted all modules we
1005 * could consider this an error */
1010 ret
= init_fn(version
);
1011 if (ret
== LDB_ERR_ENTRY_ALREADY_EXISTS
) {
1012 /* the module is already registered - ignore this, as
1013 * it can happen if LDB_MODULES_PATH points at both
1014 * the build and install directory
1021 static int qsort_string(const char **s1
, const char **s2
)
1023 return strcmp(*s1
, *s2
);
1028 load all modules from the given ldb modules directory. This is run once
1029 during the first ldb_init() call.
1031 Modules are loaded in alphabetical order to ensure that any module
1032 load ordering dependencies are reproducible. Modules should avoid
1033 relying on load order
1035 static int ldb_modules_load_dir(const char *modules_dir
, const char *version
)
1039 const char **modlist
= NULL
;
1040 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
1041 unsigned i
, num_modules
= 0;
1043 dir
= opendir(modules_dir
);
1045 if (errno
== ENOENT
) {
1046 talloc_free(tmp_ctx
);
1047 /* we don't have any modules */
1050 talloc_free(tmp_ctx
);
1051 fprintf(stderr
, "ldb: unable to open modules directory '%s' - %s\n",
1052 modules_dir
, strerror(errno
));
1053 return LDB_ERR_UNAVAILABLE
;
1057 while ((de
= readdir(dir
))) {
1058 if (ISDOT(de
->d_name
) || ISDOTDOT(de
->d_name
))
1061 modlist
= talloc_realloc(tmp_ctx
, modlist
, const char *, num_modules
+1);
1062 if (modlist
== NULL
) {
1063 talloc_free(tmp_ctx
);
1065 fprintf(stderr
, "ldb: unable to allocate modules list\n");
1066 return LDB_ERR_UNAVAILABLE
;
1068 modlist
[num_modules
] = talloc_asprintf(modlist
, "%s/%s", modules_dir
, de
->d_name
);
1069 if (modlist
[num_modules
] == NULL
) {
1070 talloc_free(tmp_ctx
);
1072 fprintf(stderr
, "ldb: unable to allocate module list entry\n");
1073 return LDB_ERR_UNAVAILABLE
;
1080 /* sort the directory, so we get consistent load ordering */
1081 TYPESAFE_QSORT(modlist
, num_modules
, qsort_string
);
1083 for (i
=0; i
<num_modules
; i
++) {
1084 int ret
= ldb_modules_load_path(modlist
[i
], version
);
1085 if (ret
!= LDB_SUCCESS
) {
1086 fprintf(stderr
, "ldb: failed to initialise module %s : %s\n",
1087 modlist
[i
], ldb_strerror(ret
));
1088 talloc_free(tmp_ctx
);
1093 talloc_free(tmp_ctx
);
1099 load any additional modules from the given directory
1101 void ldb_set_modules_dir(struct ldb_context
*ldb
, const char *path
)
1103 int ret
= ldb_modules_load_dir(path
, LDB_VERSION
);
1104 if (ret
!= LDB_SUCCESS
) {
1105 ldb_asprintf_errstring(ldb
, "Failed to load modules from: %s\n", path
);
1111 load all modules static (builtin) modules
1113 static int ldb_modules_load_static(const char *version
)
1115 static bool initialised
;
1116 #define _MODULE_PROTO(init) extern int init(const char *);
1117 STATIC_ldb_MODULES_PROTO
;
1118 const ldb_module_init_fn static_init_functions
[] = { STATIC_ldb_MODULES
};
1126 for (i
=0; static_init_functions
[i
]; i
++) {
1127 int ret
= static_init_functions
[i
](version
);
1128 if (ret
!= LDB_SUCCESS
) {
1136 load all modules from the given ldb modules path, colon
1139 modules are loaded recursively for all subdirectories in the paths
1141 int ldb_modules_load(const char *modules_path
, const char *version
)
1143 char *tok
, *path
, *tok_ptr
=NULL
;
1146 ret
= ldb_modules_load_static(version
);
1147 if (ret
!= LDB_SUCCESS
) {
1151 path
= talloc_strdup(NULL
, modules_path
);
1153 fprintf(stderr
, "ldb: failed to allocate modules_path\n");
1154 return LDB_ERR_UNAVAILABLE
;
1157 for (tok
=strtok_r(path
, ":", &tok_ptr
);
1159 tok
=strtok_r(NULL
, ":", &tok_ptr
)) {
1160 ret
= ldb_modules_load_path(tok
, version
);
1161 if (ret
!= LDB_SUCCESS
) {
1173 return a string representation of the calling chain for the given
1176 char *ldb_module_call_chain(struct ldb_request
*req
, TALLOC_CTX
*mem_ctx
)
1181 ret
= talloc_strdup(mem_ctx
, "");
1186 while (req
&& req
->handle
) {
1187 talloc_asprintf_addbuf(&ret
, "req[%u] %p : %s\n",
1188 i
++, req
, ldb_req_location(req
));
1189 req
= req
->handle
->parent
;
1196 return the next module in the chain
1198 struct ldb_module
*ldb_module_next(struct ldb_module
*module
)
1200 return module
->next
;
1204 set the next module in the module chain
1206 void ldb_module_set_next(struct ldb_module
*module
, struct ldb_module
*next
)
1208 module
->next
= next
;
1213 get the popt_options pointer in the ldb structure. This allows a ldb
1214 module to change the command line parsing
1216 struct poptOption
**ldb_module_popt_options(struct ldb_context
*ldb
)
1218 return &ldb
->popt_options
;
1223 return the current ldb flags LDB_FLG_*
1225 uint32_t ldb_module_flags(struct ldb_context
*ldb
)