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"
36 #if (_SAMBA_BUILD_ >= 4)
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
,
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
)
57 trimmed
= talloc_strdup(mem_ctx
, string
);
62 len
= strlen(trimmed
);
63 for (i
= 0; trimmed
[i
] != '\0'; i
++) {
68 memmove(&trimmed
[i
], &trimmed
[i
+ 1], len
-i
-1);
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
;
87 /* spaces not admitted */
88 modstr
= ldb_modules_strdup_no_spaces(mem_ctx
, string
);
90 ldb_debug(ldb
, LDB_DEBUG_FATAL
, "Out of Memory in ldb_modules_strdup_no_spaces()\n");
94 modules
= talloc_realloc(mem_ctx
, modules
, char *, 2);
96 ldb_debug(ldb
, LDB_DEBUG_FATAL
, "Out of Memory in ldb_modules_list_from_string()\n");
100 talloc_steal(modules
, modstr
);
103 /* The str*r*chr walks backwards: This is how we get the inverse order mentioned above */
104 while ((p
= strrchr(modstr
, ',')) != NULL
) {
110 modules
= talloc_realloc(mem_ctx
, modules
, char *, i
+ 2);
112 ldb_debug(ldb
, LDB_DEBUG_FATAL
, "Out of Memory in ldb_modules_list_from_string()\n");
119 modules
[i
+ 1] = NULL
;
121 m
= (const char **)modules
;
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
;
141 static ldb_connect_fn
ldb_find_backend(const char *url
)
143 struct backends_list_entry
*backend
;
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
;
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
);
178 talloc_free(backend
);
179 return LDB_ERR_OPERATIONS_ERROR
;
182 if (ldb_find_backend(url_prefix
)) {
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
);
197 Return the ldb module form of a database.
198 The URL can either be one of the following forms
202 flags is made up of LDB_FLG_*
204 the options are passed uninterpreted to the backend, and are
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
,
212 const char *options
[],
213 struct ldb_module
**backend_module
)
219 if (strchr(url
, ':') != NULL
) {
220 backend
= talloc_strndup(ldb
, url
, strchr(url
, ':')-url
);
223 backend
= talloc_strdup(ldb
, "tdb");
226 fn
= ldb_find_backend(backend
);
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
);
236 fn
= ops
->connect_fn
;
238 talloc_free(symbol_name
);
241 talloc_free(backend
);
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
);
259 static const struct ldb_module_ops
*ldb_find_module_ops(const char *name
)
261 struct ops_list_entry
*e
;
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)
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
)
291 entry
->next
= registered_modules
;
292 registered_modules
= entry
;
297 static void *ldb_dso_load_symbol(struct ldb_context
*ldb
, const char *name
,
304 if (ldb
->modules_dir
== NULL
)
307 path
= talloc_asprintf(ldb
, "%s/%s.%s", ldb
->modules_dir
, name
,
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());
318 sym
= (int (*)(void))dlsym(handle
, symbol
);
321 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "no symbol `%s' found in %s: %s\n", symbol
, path
, dlerror());
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
;
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) {
345 ops
= ldb_find_module_ops(module_list
[i
]);
347 char *symbol_name
= talloc_asprintf(ldb
, "ldb_%s_module_ops",
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
);
357 ldb_debug(ldb
, LDB_DEBUG_WARNING
, "WARNING: Module [%s] not found\n",
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
]);
371 DLIST_ADD(module
, current
);
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 */
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
);
396 int ldb_load_modules(struct ldb_context
*ldb
, const char *options
[])
398 const char **modules
= NULL
;
401 TALLOC_CTX
*mem_ctx
= talloc_new(ldb
);
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 */
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
);
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
);
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
);
446 module_list
= ldb_msg_find_attr_as_string(res
->msgs
[0], "@LIST", NULL
);
448 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "no modules required by the db");
450 modules
= ldb_modules_list_from_string(ldb
, mem_ctx
,
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
);
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
);
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
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; \
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
);
501 talloc_set_name_const(module
, module_name
);
503 module
->prev
= module
->next
= NULL
;
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
)
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
)
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
) {
544 FIND_OP(module
, search
);
545 ret
= module
->ops
->search(module
, request
);
548 FIND_OP(module
, add
);
549 ret
= module
->ops
->add(module
, request
);
552 FIND_OP(module
, modify
);
553 ret
= module
->ops
->modify(module
, request
);
556 FIND_OP(module
, del
);
557 ret
= module
->ops
->del(module
, request
);
560 FIND_OP(module
, rename
);
561 ret
= module
->ops
->rename(module
, request
);
564 FIND_OP(module
, extended
);
565 ret
= module
->ops
->extended(module
, request
);
568 FIND_OP(module
, request
);
569 ret
= module
->ops
->request(module
, request
);
572 if (ret
== LDB_SUCCESS
) {
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
);
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
);
613 ldb_set_errstring(ldb
, "Out of Memory");
617 h
->status
= LDB_SUCCESS
;
618 h
->state
= LDB_ASYNC_INIT
;
624 /* calls the request callback to send an entry
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
);
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
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
,
664 struct ldb_reply
*ares
;
666 ares
= talloc_zero(req
, struct ldb_reply
);
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
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
,
693 struct ldb_reply
*ares
;
695 ares
= talloc_zero(req
, struct ldb_reply
);
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
);
706 req
->callback(req
, ares
);
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
;
719 req
= talloc_zero(module
, struct ldb_request
);
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
);
744 #ifndef STATIC_LIBLDB_MODULES
747 #define LDAP_BACKEND LDB_BACKEND(ldap), LDB_BACKEND(ldapi), LDB_BACKEND(ldaps),
752 #ifdef HAVE_LDB_SQLITE3
753 #define SQLITE3_BACKEND LDB_BACKEND(sqlite3),
755 #define SQLITE3_BACKEND
758 #define STATIC_LIBLDB_MODULES \
762 LDB_MODULE(operational), \
763 LDB_MODULE(rdn_name), \
764 LDB_MODULE(paged_results), \
765 LDB_MODULE(server_sort), \
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.
777 #define NULL LDB_MODULE(NULL),
779 #define LDB_BACKEND(name) \
781 extern const struct ldb_backend_ops ldb_ ## name ## _backend_ops;\
782 extern void ldb_noop ## name (int
783 #define LDB_MODULE(name) \
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
794 .backend_ops = (void *)0, \
795 .module_ops = (void *)0 \
799 #define LDB_BACKEND(name) { \
800 .backend_ops = &ldb_ ## name ## _backend_ops, \
801 .module_ops = (void *)0 \
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