4 Copyright (C) Andrew Tridgell 2004
5 Copyright (C) Simo Sorce 2005-2008
7 ** NOTE! The following LGPL license applies to the ldb
8 ** library. This does NOT imply that all of Samba is released
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 3 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 * Component: ldb core API
30 * Description: core API routines interfacing to ldb backends
32 * Author: Andrew Tridgell
35 #define TEVENT_DEPRECATED 1
36 #include "ldb_private.h"
39 static int ldb_context_destructor(void *ptr
)
41 struct ldb_context
*ldb
= talloc_get_type(ptr
, struct ldb_context
);
43 if (ldb
->transaction_active
) {
44 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
45 "A transaction is still active in ldb context [%p] on %s",
46 ldb
, (const char *)ldb_get_opaque(ldb
, "ldb_url"));
53 this is used to catch debug messages from events
55 static void ldb_tevent_debug(void *context
, enum tevent_debug_level level
,
56 const char *fmt
, va_list ap
) PRINTF_ATTRIBUTE(3,0);
58 static void ldb_tevent_debug(void *context
, enum tevent_debug_level level
,
59 const char *fmt
, va_list ap
)
61 struct ldb_context
*ldb
= talloc_get_type(context
, struct ldb_context
);
62 enum ldb_debug_level ldb_level
= LDB_DEBUG_FATAL
;
65 case TEVENT_DEBUG_FATAL
:
66 ldb_level
= LDB_DEBUG_FATAL
;
68 case TEVENT_DEBUG_ERROR
:
69 ldb_level
= LDB_DEBUG_ERROR
;
71 case TEVENT_DEBUG_WARNING
:
72 ldb_level
= LDB_DEBUG_WARNING
;
74 case TEVENT_DEBUG_TRACE
:
75 ldb_level
= LDB_DEBUG_TRACE
;
79 /* There isn't a tevent: prefix here because to add it means
80 * actually printing the string, and most of the time we don't
82 ldb_vdebug(ldb
, ldb_level
, fmt
, ap
);
86 initialise a ldb context
87 The mem_ctx is required
88 The event_ctx is required
90 struct ldb_context
*ldb_init(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev_ctx
)
92 struct ldb_context
*ldb
;
94 const char *modules_path
= getenv("LDB_MODULES_PATH");
96 if (modules_path
== NULL
) {
97 modules_path
= LDB_MODULESDIR
;
100 ret
= ldb_modules_load(modules_path
, LDB_VERSION
);
101 if (ret
!= LDB_SUCCESS
) {
105 ldb
= talloc_zero(mem_ctx
, struct ldb_context
);
110 /* A new event context so that callers who don't want ldb
111 * operating on their global event context can work without
112 * having to provide their own private one explicitly */
113 if (ev_ctx
== NULL
) {
114 ev_ctx
= tevent_context_init(ldb
);
115 if (ev_ctx
== NULL
) {
119 tevent_set_debug(ev_ctx
, ldb_tevent_debug
, ldb
);
120 tevent_set_max_debug_level(ev_ctx
, TEVENT_DEBUG_TRACE
);
121 tevent_loop_allow_nesting(ev_ctx
);
124 ret
= ldb_setup_wellknown_attributes(ldb
);
125 if (ret
!= LDB_SUCCESS
) {
130 ldb_set_utf8_default(ldb
);
131 ldb_set_create_perms(ldb
, 0666);
132 ldb_set_modules_dir(ldb
, LDB_MODULESDIR
);
133 ldb_set_event_context(ldb
, ev_ctx
);
134 ret
= ldb_register_extended_match_rules(ldb
);
135 if (ret
!= LDB_SUCCESS
) {
140 /* TODO: get timeout from options if available there */
141 ldb
->default_timeout
= 300; /* set default to 5 minutes */
143 talloc_set_destructor((TALLOC_CTX
*)ldb
, ldb_context_destructor
);
149 try to autodetect a basedn if none specified. This fixes one of my
150 pet hates about ldapsearch, which is that you have to get a long,
151 complex basedn right to make any use of it.
153 void ldb_set_default_dns(struct ldb_context
*ldb
)
157 struct ldb_result
*res
;
158 struct ldb_dn
*tmp_dn
=NULL
;
159 static const char *attrs
[] = {
160 "rootDomainNamingContext",
161 "configurationNamingContext",
162 "schemaNamingContext",
163 "defaultNamingContext",
167 tmp_ctx
= talloc_new(ldb
);
168 ret
= ldb_search(ldb
, tmp_ctx
, &res
, ldb_dn_new(tmp_ctx
, ldb
, NULL
),
169 LDB_SCOPE_BASE
, attrs
, "(objectClass=*)");
170 if (ret
!= LDB_SUCCESS
) {
171 talloc_free(tmp_ctx
);
175 if (res
->count
!= 1) {
176 talloc_free(tmp_ctx
);
180 if (!ldb_get_opaque(ldb
, "rootDomainNamingContext")) {
181 tmp_dn
= ldb_msg_find_attr_as_dn(ldb
, ldb
, res
->msgs
[0],
182 "rootDomainNamingContext");
183 ldb_set_opaque(ldb
, "rootDomainNamingContext", tmp_dn
);
186 if (!ldb_get_opaque(ldb
, "configurationNamingContext")) {
187 tmp_dn
= ldb_msg_find_attr_as_dn(ldb
, ldb
, res
->msgs
[0],
188 "configurationNamingContext");
189 ldb_set_opaque(ldb
, "configurationNamingContext", tmp_dn
);
192 if (!ldb_get_opaque(ldb
, "schemaNamingContext")) {
193 tmp_dn
= ldb_msg_find_attr_as_dn(ldb
, ldb
, res
->msgs
[0],
194 "schemaNamingContext");
195 ldb_set_opaque(ldb
, "schemaNamingContext", tmp_dn
);
198 if (!ldb_get_opaque(ldb
, "defaultNamingContext")) {
199 tmp_dn
= ldb_msg_find_attr_as_dn(ldb
, ldb
, res
->msgs
[0],
200 "defaultNamingContext");
201 ldb_set_opaque(ldb
, "defaultNamingContext", tmp_dn
);
204 talloc_free(tmp_ctx
);
207 struct ldb_dn
*ldb_get_root_basedn(struct ldb_context
*ldb
)
209 void *opaque
= ldb_get_opaque(ldb
, "rootDomainNamingContext");
210 return talloc_get_type(opaque
, struct ldb_dn
);
213 struct ldb_dn
*ldb_get_config_basedn(struct ldb_context
*ldb
)
215 void *opaque
= ldb_get_opaque(ldb
, "configurationNamingContext");
216 return talloc_get_type(opaque
, struct ldb_dn
);
219 struct ldb_dn
*ldb_get_schema_basedn(struct ldb_context
*ldb
)
221 void *opaque
= ldb_get_opaque(ldb
, "schemaNamingContext");
222 return talloc_get_type(opaque
, struct ldb_dn
);
225 struct ldb_dn
*ldb_get_default_basedn(struct ldb_context
*ldb
)
227 void *opaque
= ldb_get_opaque(ldb
, "defaultNamingContext");
228 return talloc_get_type(opaque
, struct ldb_dn
);
232 connect to a database. The URL can either be one of the following forms
236 flags is made up of LDB_FLG_*
238 the options are passed uninterpreted to the backend, and are
241 int ldb_connect(struct ldb_context
*ldb
, const char *url
,
242 unsigned int flags
, const char *options
[])
247 const char *existing_url
= ldb_get_opaque(ldb
, "ldb_url");
248 if (existing_url
!= NULL
) {
249 ldb_asprintf_errstring(
251 "This LDB has already connected to '%s', and "
252 "cannot also connect to '%s'",
254 return LDB_ERR_OPERATIONS_ERROR
;
257 /* We seem to need to do this here, or else some utilities don't
258 * get ldb backends */
262 url2
= talloc_strdup(ldb
, url
);
265 return LDB_ERR_OPERATIONS_ERROR
;
267 ret
= ldb_set_opaque(ldb
, "ldb_url", url2
);
268 if (ret
!= LDB_SUCCESS
) {
273 * Take a copy of the options.
275 ldb
->options
= ldb_options_copy(ldb
, options
);
276 if (ldb
->options
== NULL
&& options
!= NULL
) {
278 return LDB_ERR_OPERATIONS_ERROR
;
281 ret
= ldb_module_connect_backend(ldb
, url
, options
, &ldb
->modules
);
282 if (ret
!= LDB_SUCCESS
) {
286 ret
= ldb_load_modules(ldb
, options
);
287 if (ret
!= LDB_SUCCESS
) {
288 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
289 "Unable to load modules for %s: %s",
290 url
, ldb_errstring(ldb
));
294 /* set the default base dn */
295 ldb_set_default_dns(ldb
);
300 void ldb_set_errstring(struct ldb_context
*ldb
, const char *err_string
)
302 ldb_asprintf_errstring(ldb
, "%s", err_string
);
305 void ldb_asprintf_errstring(struct ldb_context
*ldb
, const char *format
, ...)
308 char *old_err_string
= NULL
;
309 if (ldb
->err_string
) {
310 old_err_string
= ldb
->err_string
;
313 va_start(ap
, format
);
314 ldb
->err_string
= talloc_vasprintf(ldb
, format
, ap
);
317 TALLOC_FREE(old_err_string
);
319 if (ldb
->flags
& LDB_FLG_ENABLE_TRACING
) {
320 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "ldb_asprintf/set_errstring: %s",
325 void ldb_reset_err_string(struct ldb_context
*ldb
)
327 TALLOC_FREE(ldb
->err_string
);
333 set an ldb error based on file:line
335 int ldb_error_at(struct ldb_context
*ldb
, int ecode
,
336 const char *reason
, const char *file
, int line
)
338 if (reason
== NULL
) {
339 reason
= ldb_strerror(ecode
);
341 ldb_asprintf_errstring(ldb
, "%s at %s:%d", reason
, file
, line
);
346 #define FIRST_OP_NOERR(ldb, op) do { \
347 next_module = ldb->modules; \
348 while (next_module && next_module->ops->op == NULL) { \
349 next_module = next_module->next; \
351 if ((ldb->flags & LDB_FLG_ENABLE_TRACING) && next_module) { \
352 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: (%s)->" #op, \
353 next_module->ops->name); \
357 #define FIRST_OP(ldb, op) do { \
358 FIRST_OP_NOERR(ldb, op); \
359 if (next_module == NULL) { \
360 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
361 return LDB_ERR_OPERATIONS_ERROR; \
369 int ldb_transaction_start(struct ldb_context
*ldb
)
371 struct ldb_module
*next_module
;
374 ldb_debug(ldb
, LDB_DEBUG_TRACE
,
375 "start ldb transaction (nesting: %d)",
376 ldb
->transaction_active
);
378 /* explicit transaction active, count nested requests */
379 if (ldb
->transaction_active
) {
380 ldb
->transaction_active
++;
384 /* start a new transaction */
385 ldb
->transaction_active
++;
386 ldb
->prepare_commit_done
= false;
388 FIRST_OP(ldb
, start_transaction
);
390 ldb_reset_err_string(ldb
);
392 status
= next_module
->ops
->start_transaction(next_module
);
393 if (status
!= LDB_SUCCESS
) {
394 if (ldb
->err_string
== NULL
) {
395 /* no error string was setup by the backend */
396 ldb_asprintf_errstring(ldb
,
397 "ldb transaction start: %s (%d)",
398 ldb_strerror(status
),
400 ldb
->transaction_active
--;
402 if ((next_module
&& next_module
->ldb
->flags
& LDB_FLG_ENABLE_TRACING
)) {
403 ldb_debug(next_module
->ldb
, LDB_DEBUG_TRACE
, "start ldb transaction error: %s",
404 ldb_errstring(next_module
->ldb
));
407 if ((next_module
&& next_module
->ldb
->flags
& LDB_FLG_ENABLE_TRACING
)) {
408 ldb_debug(next_module
->ldb
, LDB_DEBUG_TRACE
, "start ldb transaction success");
415 prepare for transaction commit (first phase of two phase commit)
417 int ldb_transaction_prepare_commit(struct ldb_context
*ldb
)
419 struct ldb_module
*next_module
;
422 if (ldb
->prepare_commit_done
) {
426 /* commit only when all nested transactions are complete */
427 if (ldb
->transaction_active
> 1) {
431 ldb
->prepare_commit_done
= true;
433 if (ldb
->transaction_active
< 0) {
434 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
435 "prepare commit called but no ldb transactions are active!");
436 ldb
->transaction_active
= 0;
437 return LDB_ERR_OPERATIONS_ERROR
;
440 /* call prepare transaction if available */
441 FIRST_OP_NOERR(ldb
, prepare_commit
);
442 if (next_module
== NULL
) {
446 ldb_reset_err_string(ldb
);
448 status
= next_module
->ops
->prepare_commit(next_module
);
449 if (status
!= LDB_SUCCESS
) {
450 ldb
->transaction_active
--;
451 /* if a next_module fails the prepare then we need
452 to call the end transaction for everyone */
453 FIRST_OP(ldb
, del_transaction
);
454 next_module
->ops
->del_transaction(next_module
);
455 if (ldb
->err_string
== NULL
) {
456 /* no error string was setup by the backend */
457 ldb_asprintf_errstring(ldb
,
458 "ldb transaction prepare commit: %s (%d)",
459 ldb_strerror(status
),
462 if ((next_module
&& next_module
->ldb
->flags
& LDB_FLG_ENABLE_TRACING
)) {
463 ldb_debug(next_module
->ldb
, LDB_DEBUG_TRACE
, "prepare commit transaction error: %s",
464 ldb_errstring(next_module
->ldb
));
475 int ldb_transaction_commit(struct ldb_context
*ldb
)
477 struct ldb_module
*next_module
;
480 status
= ldb_transaction_prepare_commit(ldb
);
481 if (status
!= LDB_SUCCESS
) {
485 ldb
->transaction_active
--;
487 ldb_debug(ldb
, LDB_DEBUG_TRACE
,
488 "commit ldb transaction (nesting: %d)",
489 ldb
->transaction_active
);
491 /* commit only when all nested transactions are complete */
492 if (ldb
->transaction_active
> 0) {
496 if (ldb
->transaction_active
< 0) {
497 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
498 "commit called but no ldb transactions are active!");
499 ldb
->transaction_active
= 0;
500 return LDB_ERR_OPERATIONS_ERROR
;
503 ldb_reset_err_string(ldb
);
505 FIRST_OP(ldb
, end_transaction
);
506 status
= next_module
->ops
->end_transaction(next_module
);
507 if (status
!= LDB_SUCCESS
) {
508 if (ldb
->err_string
== NULL
) {
509 /* no error string was setup by the backend */
510 ldb_asprintf_errstring(ldb
,
511 "ldb transaction commit: %s (%d)",
512 ldb_strerror(status
),
515 if ((next_module
&& next_module
->ldb
->flags
& LDB_FLG_ENABLE_TRACING
)) {
516 ldb_debug(next_module
->ldb
, LDB_DEBUG_TRACE
, "commit ldb transaction error: %s",
517 ldb_errstring(next_module
->ldb
));
527 int ldb_transaction_cancel(struct ldb_context
*ldb
)
529 struct ldb_module
*next_module
;
532 ldb
->transaction_active
--;
534 ldb_debug(ldb
, LDB_DEBUG_TRACE
,
535 "cancel ldb transaction (nesting: %d)",
536 ldb
->transaction_active
);
538 /* really cancel only if all nested transactions are complete */
539 if (ldb
->transaction_active
> 0) {
543 if (ldb
->transaction_active
< 0) {
544 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
545 "cancel called but no ldb transactions are active!");
546 ldb
->transaction_active
= 0;
547 return LDB_ERR_OPERATIONS_ERROR
;
550 FIRST_OP(ldb
, del_transaction
);
552 status
= next_module
->ops
->del_transaction(next_module
);
553 if (status
!= LDB_SUCCESS
) {
554 if (ldb
->err_string
== NULL
) {
555 /* no error string was setup by the backend */
556 ldb_asprintf_errstring(ldb
,
557 "ldb transaction cancel: %s (%d)",
558 ldb_strerror(status
),
561 if ((next_module
&& next_module
->ldb
->flags
& LDB_FLG_ENABLE_TRACING
)) {
562 ldb_debug(next_module
->ldb
, LDB_DEBUG_TRACE
, "cancel ldb transaction error: %s",
563 ldb_errstring(next_module
->ldb
));
570 cancel a transaction with no error if no transaction is pending
571 used when we fork() to clear any parent transactions
573 int ldb_transaction_cancel_noerr(struct ldb_context
*ldb
)
575 if (ldb
->transaction_active
> 0) {
576 return ldb_transaction_cancel(ldb
);
582 /* autostarts a transaction if none active */
583 static int ldb_autotransaction_request(struct ldb_context
*ldb
,
584 struct ldb_request
*req
)
588 ret
= ldb_transaction_start(ldb
);
589 if (ret
!= LDB_SUCCESS
) {
593 ret
= ldb_request(ldb
, req
);
594 if (ret
== LDB_SUCCESS
) {
595 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
598 if (ret
== LDB_SUCCESS
) {
599 return ldb_transaction_commit(ldb
);
601 ldb_transaction_cancel(ldb
);
606 int ldb_wait(struct ldb_handle
*handle
, enum ldb_wait_type type
)
608 struct tevent_context
*ev
;
611 if (handle
== NULL
) {
612 return LDB_ERR_UNAVAILABLE
;
615 if (handle
->state
== LDB_ASYNC_DONE
) {
616 if ((handle
->status
!= LDB_SUCCESS
) &&
617 (handle
->ldb
->err_string
== NULL
)) {
618 /* if no error string was setup by the backend */
619 ldb_asprintf_errstring(handle
->ldb
,
620 "ldb_wait from %s with LDB_ASYNC_DONE: %s (%d)",
622 ldb_strerror(handle
->status
),
625 return handle
->status
;
628 ev
= ldb_handle_get_event_context(handle
);
630 return ldb_oom(handle
->ldb
);
635 ret
= tevent_loop_once(ev
);
637 return ldb_operr(handle
->ldb
);
639 if (handle
->status
== LDB_SUCCESS
) {
642 if (handle
->ldb
->err_string
!= NULL
) {
643 return handle
->status
;
646 * if no error string was setup by the backend
648 ldb_asprintf_errstring(handle
->ldb
,
649 "ldb_wait from %s with LDB_WAIT_NONE: %s (%d)",
651 ldb_strerror(handle
->status
),
653 return handle
->status
;
656 while (handle
->state
!= LDB_ASYNC_DONE
) {
657 ret
= tevent_loop_once(ev
);
659 return ldb_operr(handle
->ldb
);
661 if (handle
->status
!= LDB_SUCCESS
) {
662 if (handle
->ldb
->err_string
!= NULL
) {
663 return handle
->status
;
666 * if no error string was setup by the
669 ldb_asprintf_errstring(handle
->ldb
,
670 "ldb_wait from %s with "
671 "LDB_WAIT_ALL: %s (%d)",
673 ldb_strerror(handle
->status
),
675 return handle
->status
;
678 if (handle
->status
== LDB_SUCCESS
) {
681 if (handle
->ldb
->err_string
!= NULL
) {
682 return handle
->status
;
685 * if no error string was setup by the backend
687 ldb_asprintf_errstring(handle
->ldb
,
688 "ldb_wait from %s with LDB_WAIT_ALL,"
689 " LDB_ASYNC_DONE: %s (%d)",
691 ldb_strerror(handle
->status
),
693 return handle
->status
;
699 /* set the specified timeout or, if timeout is 0 set the default timeout */
700 int ldb_set_timeout(struct ldb_context
*ldb
,
701 struct ldb_request
*req
,
704 if (req
== NULL
) return LDB_ERR_OPERATIONS_ERROR
;
707 req
->timeout
= timeout
;
709 req
->timeout
= ldb
->default_timeout
;
711 req
->starttime
= time(NULL
);
716 /* calculates the new timeout based on the previous starttime and timeout */
717 int ldb_set_timeout_from_prev_req(struct ldb_context
*ldb
,
718 struct ldb_request
*oldreq
,
719 struct ldb_request
*newreq
)
721 if (newreq
== NULL
) return LDB_ERR_OPERATIONS_ERROR
;
723 if (oldreq
== NULL
) {
724 return ldb_set_timeout(ldb
, newreq
, 0);
727 newreq
->starttime
= oldreq
->starttime
;
728 newreq
->timeout
= oldreq
->timeout
;
734 struct ldb_handle
*ldb_handle_new(TALLOC_CTX
*mem_ctx
, struct ldb_context
*ldb
)
736 struct ldb_handle
*h
;
738 h
= talloc_zero(mem_ctx
, struct ldb_handle
);
740 ldb_set_errstring(ldb
, "Out of Memory");
744 h
->status
= LDB_SUCCESS
;
745 h
->state
= LDB_ASYNC_INIT
;
751 if (h
->ldb
->require_private_event_context
== true) {
752 h
->event_context
= tevent_context_init(h
);
753 if (h
->event_context
== NULL
) {
754 ldb_set_errstring(ldb
,
755 "Out of Memory allocating "
756 "event context for new handle");
759 tevent_set_debug(h
->event_context
, ldb_tevent_debug
, ldb
);
760 tevent_set_max_debug_level(h
->event_context
, TEVENT_DEBUG_TRACE
);
761 tevent_loop_allow_nesting(h
->event_context
);
767 static struct ldb_handle
*ldb_handle_new_child(TALLOC_CTX
*mem_ctx
,
768 struct ldb_request
*parent_req
)
770 struct ldb_handle
*h
;
772 h
= talloc_zero(mem_ctx
, struct ldb_handle
);
774 ldb_set_errstring(parent_req
->handle
->ldb
,
779 h
->status
= LDB_SUCCESS
;
780 h
->state
= LDB_ASYNC_INIT
;
781 h
->ldb
= parent_req
->handle
->ldb
;
782 h
->parent
= parent_req
;
783 h
->nesting
= parent_req
->handle
->nesting
+ 1;
784 h
->flags
= parent_req
->handle
->flags
;
785 h
->custom_flags
= parent_req
->handle
->custom_flags
;
786 h
->event_context
= parent_req
->handle
->event_context
;
792 set the permissions for new files to be passed to open() in
793 backends that use local files
795 void ldb_set_create_perms(struct ldb_context
*ldb
, unsigned int perms
)
797 ldb
->create_perms
= perms
;
800 unsigned int ldb_get_create_perms(struct ldb_context
*ldb
)
802 return ldb
->create_perms
;
805 void ldb_set_event_context(struct ldb_context
*ldb
, struct tevent_context
*ev
)
810 struct tevent_context
* ldb_get_event_context(struct ldb_context
*ldb
)
815 void ldb_request_set_state(struct ldb_request
*req
, int state
)
817 req
->handle
->state
= state
;
820 int ldb_request_get_status(struct ldb_request
*req
)
822 return req
->handle
->status
;
826 * This function obtains the private event context for the handle,
827 * which may have been created to avoid nested event loops during
828 * ldb_tdb with the locks held
830 struct tevent_context
*ldb_handle_get_event_context(struct ldb_handle
*handle
)
832 if (handle
->event_context
!= NULL
) {
833 return handle
->event_context
;
835 return ldb_get_event_context(handle
->ldb
);
839 * This function forces a specific ldb handle to use the global event
840 * context. This allows a nested event loop to operate, so any open
841 * transaction also needs to be aborted.
843 * Any events on this event context will be lost
845 * This is used in Samba when sending an IRPC to another part of the
846 * same process instead of making a local DB modification.
848 void ldb_handle_use_global_event_context(struct ldb_handle
*handle
)
850 TALLOC_FREE(handle
->event_context
);
853 void ldb_set_require_private_event_context(struct ldb_context
*ldb
)
855 ldb
->require_private_event_context
= true;
861 static void ldb_trace_request(struct ldb_context
*ldb
, struct ldb_request
*req
)
863 TALLOC_CTX
*tmp_ctx
= talloc_new(req
);
865 struct ldb_ldif ldif
;
867 switch (req
->operation
) {
869 ldb_debug_add(ldb
, "ldb_trace_request: SEARCH\n");
870 ldb_debug_add(ldb
, " dn: %s\n",
871 ldb_dn_is_null(req
->op
.search
.base
)?"<rootDSE>":
872 ldb_dn_get_linearized(req
->op
.search
.base
));
873 ldb_debug_add(ldb
, " scope: %s\n",
874 req
->op
.search
.scope
==LDB_SCOPE_BASE
?"base":
875 req
->op
.search
.scope
==LDB_SCOPE_ONELEVEL
?"one":
876 req
->op
.search
.scope
==LDB_SCOPE_SUBTREE
?"sub":"UNKNOWN");
877 ldb_debug_add(ldb
, " expr: %s\n",
878 ldb_filter_from_tree(tmp_ctx
, req
->op
.search
.tree
));
879 if (req
->op
.search
.attrs
== NULL
) {
880 ldb_debug_add(ldb
, " attr: <ALL>\n");
882 for (i
=0; req
->op
.search
.attrs
[i
]; i
++) {
883 ldb_debug_add(ldb
, " attr: %s\n", req
->op
.search
.attrs
[i
]);
888 ldb_debug_add(ldb
, "ldb_trace_request: DELETE\n");
889 ldb_debug_add(ldb
, " dn: %s\n",
890 ldb_dn_get_linearized(req
->op
.del
.dn
));
893 ldb_debug_add(ldb
, "ldb_trace_request: RENAME\n");
894 ldb_debug_add(ldb
, " olddn: %s\n",
895 ldb_dn_get_linearized(req
->op
.rename
.olddn
));
896 ldb_debug_add(ldb
, " newdn: %s\n",
897 ldb_dn_get_linearized(req
->op
.rename
.newdn
));
900 ldb_debug_add(ldb
, "ldb_trace_request: EXTENDED\n");
901 ldb_debug_add(ldb
, " oid: %s\n", req
->op
.extended
.oid
);
902 ldb_debug_add(ldb
, " data: %s\n", req
->op
.extended
.data
?"yes":"no");
905 ldif
.changetype
= LDB_CHANGETYPE_ADD
;
906 ldif
.msg
= discard_const_p(struct ldb_message
, req
->op
.add
.message
);
908 ldb_debug_add(ldb
, "ldb_trace_request: ADD\n");
912 * ldb_ldif_write_redacted_trace_string() is CRITICAL
913 * for security. It ensures that we do not output
914 * passwords into debug logs
917 ldb_debug_add(req
->handle
->ldb
, "%s\n",
918 ldb_ldif_write_redacted_trace_string(req
->handle
->ldb
, tmp_ctx
, &ldif
));
921 ldif
.changetype
= LDB_CHANGETYPE_MODIFY
;
922 ldif
.msg
= discard_const_p(struct ldb_message
, req
->op
.mod
.message
);
924 ldb_debug_add(ldb
, "ldb_trace_request: MODIFY\n");
928 * ldb_ldif_write_redacted_trace_string() is CRITICAL
929 * for security. It ensures that we do not output
930 * passwords into debug logs
933 ldb_debug_add(req
->handle
->ldb
, "%s\n",
934 ldb_ldif_write_redacted_trace_string(req
->handle
->ldb
, tmp_ctx
, &ldif
));
936 case LDB_REQ_REGISTER_CONTROL
:
937 ldb_debug_add(ldb
, "ldb_trace_request: REGISTER_CONTROL\n");
938 ldb_debug_add(req
->handle
->ldb
, "%s\n",
939 req
->op
.reg_control
.oid
);
941 case LDB_REQ_REGISTER_PARTITION
:
942 ldb_debug_add(ldb
, "ldb_trace_request: REGISTER_PARTITION\n");
943 ldb_debug_add(req
->handle
->ldb
, "%s\n",
944 ldb_dn_get_linearized(req
->op
.reg_partition
.dn
));
947 ldb_debug_add(ldb
, "ldb_trace_request: UNKNOWN(%u)\n",
952 if (req
->controls
== NULL
) {
953 ldb_debug_add(ldb
, " control: <NONE>\n");
955 for (i
=0; req
->controls
&& req
->controls
[i
]; i
++) {
956 if (req
->controls
[i
]->oid
) {
957 ldb_debug_add(ldb
, " control: %s crit:%u data:%s\n",
958 req
->controls
[i
]->oid
,
959 req
->controls
[i
]->critical
,
960 req
->controls
[i
]->data
?"yes":"no");
965 ldb_debug_end(ldb
, LDB_DEBUG_TRACE
);
967 talloc_free(tmp_ctx
);
971 check that the element flags don't have any internal bits set
973 static int ldb_msg_check_element_flags(struct ldb_context
*ldb
,
974 const struct ldb_message
*message
)
977 for (i
=0; i
<message
->num_elements
; i
++) {
978 if (message
->elements
[i
].flags
& LDB_FLAG_INTERNAL_MASK
) {
979 ldb_asprintf_errstring(ldb
, "Invalid element flags 0x%08x on element %s in %s\n",
980 message
->elements
[i
].flags
, message
->elements
[i
].name
,
981 ldb_dn_get_linearized(message
->dn
));
982 return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION
;
989 * This context allows us to make the unlock be a talloc destructor
991 * This ensures that a request started, but not waited on, will still
994 struct ldb_db_lock_context
{
995 struct ldb_request
*req
;
996 struct ldb_context
*ldb
;
1000 * We have to have the unlock on a destructor so that we unlock the
1001 * DB if a caller calls talloc_free(req). We trust that the ldb
1002 * context has not already gone away.
1004 static int ldb_db_lock_destructor(struct ldb_db_lock_context
*lock_context
)
1007 struct ldb_module
*next_module
;
1008 FIRST_OP_NOERR(lock_context
->ldb
, read_unlock
);
1009 if (next_module
!= NULL
) {
1010 ret
= next_module
->ops
->read_unlock(next_module
);
1015 if (ret
!= LDB_SUCCESS
) {
1016 ldb_debug(lock_context
->ldb
,
1018 "Failed to unlock db: %s / %s",
1019 ldb_errstring(lock_context
->ldb
),
1025 static int ldb_lock_backend_callback(struct ldb_request
*req
,
1026 struct ldb_reply
*ares
)
1028 struct ldb_db_lock_context
*lock_context
;
1031 if (req
->context
== NULL
) {
1033 * The usual way to get here is to ignore the return codes
1034 * and continuing processing after an error.
1038 lock_context
= talloc_get_type(req
->context
,
1039 struct ldb_db_lock_context
);
1042 return ldb_module_done(lock_context
->req
, NULL
, NULL
,
1043 LDB_ERR_OPERATIONS_ERROR
);
1045 if (ares
->error
!= LDB_SUCCESS
|| ares
->type
== LDB_REPLY_DONE
) {
1046 ret
= ldb_module_done(lock_context
->req
, ares
->controls
,
1047 ares
->response
, ares
->error
);
1049 * If this is a LDB_REPLY_DONE or an error, unlock the
1050 * DB by calling the destructor on this context
1052 TALLOC_FREE(req
->context
);
1056 /* Otherwise pass on the callback */
1057 switch (ares
->type
) {
1058 case LDB_REPLY_ENTRY
:
1059 return ldb_module_send_entry(lock_context
->req
, ares
->message
,
1062 case LDB_REPLY_REFERRAL
:
1063 return ldb_module_send_referral(lock_context
->req
,
1067 return LDB_ERR_OPERATIONS_ERROR
;
1072 * Do an ldb_search() with a lock held, but release it if the request
1073 * is freed with talloc_free()
1075 static int lock_search(struct ldb_module
*lock_module
, struct ldb_request
*req
)
1077 /* Used in FIRST_OP_NOERR to find where to send the lock request */
1078 struct ldb_module
*next_module
= NULL
;
1079 struct ldb_request
*down_req
= NULL
;
1080 struct ldb_db_lock_context
*lock_context
;
1081 struct ldb_context
*ldb
= ldb_module_get_ctx(lock_module
);
1084 lock_context
= talloc(req
, struct ldb_db_lock_context
);
1085 if (lock_context
== NULL
) {
1086 return ldb_oom(ldb
);
1089 lock_context
->ldb
= ldb
;
1090 lock_context
->req
= req
;
1092 ret
= ldb_build_search_req_ex(&down_req
, ldb
, req
,
1093 req
->op
.search
.base
,
1094 req
->op
.search
.scope
,
1095 req
->op
.search
.tree
,
1096 req
->op
.search
.attrs
,
1099 ldb_lock_backend_callback
,
1101 LDB_REQ_SET_LOCATION(down_req
);
1102 if (ret
!= LDB_SUCCESS
) {
1107 FIRST_OP_NOERR(ldb
, read_lock
);
1108 if (next_module
!= NULL
) {
1109 ret
= next_module
->ops
->read_lock(next_module
);
1111 ret
= LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION
;
1114 if (ret
== LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION
) {
1115 /* We might be talking LDAP */
1116 ldb_reset_err_string(ldb
);
1117 TALLOC_FREE(lock_context
);
1119 return ldb_next_request(lock_module
, req
);
1120 } else if ((ret
!= LDB_SUCCESS
) && (ldb
->err_string
== NULL
)) {
1121 /* if no error string was setup by the backend */
1122 ldb_asprintf_errstring(ldb
, "Failed to get DB lock: %s (%d)",
1123 ldb_strerror(ret
), ret
);
1125 talloc_set_destructor(lock_context
, ldb_db_lock_destructor
);
1128 if (ret
!= LDB_SUCCESS
) {
1132 return ldb_next_request(lock_module
, down_req
);
1136 start an ldb request
1137 NOTE: the request must be a talloc context.
1138 returns LDB_ERR_* on errors.
1140 int ldb_request(struct ldb_context
*ldb
, struct ldb_request
*req
)
1142 struct ldb_module
*next_module
;
1145 if (req
->callback
== NULL
) {
1146 ldb_set_errstring(ldb
, "Requests MUST define callbacks");
1147 return LDB_ERR_UNWILLING_TO_PERFORM
;
1150 ldb_reset_err_string(ldb
);
1152 if (ldb
->flags
& LDB_FLG_ENABLE_TRACING
) {
1153 ldb_trace_request(ldb
, req
);
1156 /* call the first module in the chain */
1157 switch (req
->operation
) {
1161 * A fake module to allow ldb_next_request() to be
1162 * re-used and to keep the locking out of this function.
1164 static const struct ldb_module_ops lock_module_ops
= {
1165 .name
= "lock_searches",
1166 .search
= lock_search
1168 struct ldb_module lock_module
= {
1170 .next
= ldb
->modules
,
1171 .ops
= &lock_module_ops
1173 next_module
= &lock_module
;
1175 /* due to "ldb_build_search_req" base DN always != NULL */
1176 if (!ldb_dn_validate(req
->op
.search
.base
)) {
1177 ldb_asprintf_errstring(ldb
, "ldb_search: invalid basedn '%s'",
1178 ldb_dn_get_linearized(req
->op
.search
.base
));
1179 return LDB_ERR_INVALID_DN_SYNTAX
;
1182 ret
= next_module
->ops
->search(next_module
, req
);
1186 if (!ldb_dn_validate(req
->op
.add
.message
->dn
)) {
1187 ldb_asprintf_errstring(ldb
, "ldb_add: invalid dn '%s'",
1188 ldb_dn_get_linearized(req
->op
.add
.message
->dn
));
1189 return LDB_ERR_INVALID_DN_SYNTAX
;
1192 * we have to normalize here, as so many places
1193 * in modules and backends assume we don't have two
1194 * elements with the same name
1196 ret
= ldb_msg_normalize(ldb
, req
, req
->op
.add
.message
,
1197 discard_const(&req
->op
.add
.message
));
1198 if (ret
!= LDB_SUCCESS
) {
1203 ret
= ldb_msg_check_element_flags(ldb
, req
->op
.add
.message
);
1204 if (ret
!= LDB_SUCCESS
) {
1206 * "ldb_msg_check_element_flags" generates an error
1211 ret
= next_module
->ops
->add(next_module
, req
);
1214 if (!ldb_dn_validate(req
->op
.mod
.message
->dn
)) {
1215 ldb_asprintf_errstring(ldb
, "ldb_modify: invalid dn '%s'",
1216 ldb_dn_get_linearized(req
->op
.mod
.message
->dn
));
1217 return LDB_ERR_INVALID_DN_SYNTAX
;
1219 FIRST_OP(ldb
, modify
);
1220 ret
= ldb_msg_check_element_flags(ldb
, req
->op
.mod
.message
);
1221 if (ret
!= LDB_SUCCESS
) {
1223 * "ldb_msg_check_element_flags" generates an error
1228 ret
= next_module
->ops
->modify(next_module
, req
);
1231 if (!ldb_dn_validate(req
->op
.del
.dn
)) {
1232 ldb_asprintf_errstring(ldb
, "ldb_delete: invalid dn '%s'",
1233 ldb_dn_get_linearized(req
->op
.del
.dn
));
1234 return LDB_ERR_INVALID_DN_SYNTAX
;
1237 ret
= next_module
->ops
->del(next_module
, req
);
1240 if (!ldb_dn_validate(req
->op
.rename
.olddn
)) {
1241 ldb_asprintf_errstring(ldb
, "ldb_rename: invalid olddn '%s'",
1242 ldb_dn_get_linearized(req
->op
.rename
.olddn
));
1243 return LDB_ERR_INVALID_DN_SYNTAX
;
1245 if (!ldb_dn_validate(req
->op
.rename
.newdn
)) {
1246 ldb_asprintf_errstring(ldb
, "ldb_rename: invalid newdn '%s'",
1247 ldb_dn_get_linearized(req
->op
.rename
.newdn
));
1248 return LDB_ERR_INVALID_DN_SYNTAX
;
1250 FIRST_OP(ldb
, rename
);
1251 ret
= next_module
->ops
->rename(next_module
, req
);
1254 FIRST_OP(ldb
, extended
);
1255 ret
= next_module
->ops
->extended(next_module
, req
);
1258 FIRST_OP(ldb
, request
);
1259 ret
= next_module
->ops
->request(next_module
, req
);
1263 if ((ret
!= LDB_SUCCESS
) && (ldb
->err_string
== NULL
)) {
1264 /* if no error string was setup by the backend */
1265 ldb_asprintf_errstring(ldb
, "ldb_request: %s (%d)",
1266 ldb_strerror(ret
), ret
);
1272 int ldb_request_done(struct ldb_request
*req
, int status
)
1274 req
->handle
->state
= LDB_ASYNC_DONE
;
1275 req
->handle
->status
= status
;
1280 search the database given a LDAP-like search expression
1282 returns an LDB error code
1284 Use talloc_free to free the ldb_message returned in 'res', if successful
1287 int ldb_search_default_callback(struct ldb_request
*req
,
1288 struct ldb_reply
*ares
)
1290 struct ldb_result
*res
;
1293 res
= talloc_get_type(req
->context
, struct ldb_result
);
1296 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
1298 if (ares
->error
!= LDB_SUCCESS
) {
1299 return ldb_request_done(req
, ares
->error
);
1302 switch (ares
->type
) {
1303 case LDB_REPLY_ENTRY
:
1304 res
->msgs
= talloc_realloc(res
, res
->msgs
,
1305 struct ldb_message
*, res
->count
+ 2);
1307 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
1310 res
->msgs
[res
->count
+ 1] = NULL
;
1312 res
->msgs
[res
->count
] = talloc_move(res
->msgs
, &ares
->message
);
1316 case LDB_REPLY_REFERRAL
:
1318 for (n
= 0; res
->refs
[n
]; n
++) /*noop*/ ;
1323 res
->refs
= talloc_realloc(res
, res
->refs
, char *, n
+ 2);
1325 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
1328 res
->refs
[n
] = talloc_move(res
->refs
, &ares
->referral
);
1329 res
->refs
[n
+ 1] = NULL
;
1332 case LDB_REPLY_DONE
:
1333 /* TODO: we should really support controls on entries
1334 * and referrals too! */
1335 res
->controls
= talloc_move(res
, &ares
->controls
);
1337 /* this is the last message, and means the request is done */
1338 /* we have to signal and eventual ldb_wait() waiting that the
1339 * async request operation was completed */
1341 return ldb_request_done(req
, LDB_SUCCESS
);
1349 int ldb_modify_default_callback(struct ldb_request
*req
, struct ldb_reply
*ares
)
1351 struct ldb_result
*res
;
1355 res
= talloc_get_type(req
->context
, struct ldb_result
);
1358 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
1361 if (ares
->error
!= LDB_SUCCESS
) {
1364 return ldb_request_done(req
, ret
);
1367 switch (ares
->type
) {
1368 case LDB_REPLY_REFERRAL
:
1370 for (n
= 0; res
->refs
[n
]; n
++) /*noop*/ ;
1375 res
->refs
= talloc_realloc(res
, res
->refs
, char *, n
+ 2);
1377 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
1380 res
->refs
[n
] = talloc_move(res
->refs
, &ares
->referral
);
1381 res
->refs
[n
+ 1] = NULL
;
1384 case LDB_REPLY_DONE
:
1386 return ldb_request_done(req
, LDB_SUCCESS
);
1389 ldb_asprintf_errstring(req
->handle
->ldb
, "Invalid LDB reply type %d", ares
->type
);
1390 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
1394 return ldb_request_done(req
, LDB_SUCCESS
);
1397 int ldb_op_default_callback(struct ldb_request
*req
, struct ldb_reply
*ares
)
1402 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
1405 if (ares
->error
!= LDB_SUCCESS
) {
1408 return ldb_request_done(req
, ret
);
1411 if (ares
->type
!= LDB_REPLY_DONE
) {
1412 ldb_asprintf_errstring(req
->handle
->ldb
, "Invalid LDB reply type %d", ares
->type
);
1414 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
1418 return ldb_request_done(req
, LDB_SUCCESS
);
1421 static struct ldb_request
*ldb_build_req_common(TALLOC_CTX
*mem_ctx
,
1422 struct ldb_context
*ldb
,
1423 struct ldb_control
**controls
,
1425 ldb_request_callback_t callback
,
1426 struct ldb_request
*parent
)
1428 struct ldb_request
*req
= NULL
;
1430 req
= talloc_zero(mem_ctx
, struct ldb_request
);
1434 req
->controls
= controls
;
1435 req
->context
= context
;
1436 req
->callback
= callback
;
1438 ldb_set_timeout_from_prev_req(ldb
, parent
, req
);
1440 if (parent
!= NULL
) {
1441 req
->handle
= ldb_handle_new_child(req
, parent
);
1442 if (req
->handle
== NULL
) {
1447 req
->handle
= ldb_handle_new(req
, ldb
);
1448 if (req
->handle
== NULL
) {
1457 int ldb_build_search_req_ex(struct ldb_request
**ret_req
,
1458 struct ldb_context
*ldb
,
1459 TALLOC_CTX
*mem_ctx
,
1460 struct ldb_dn
*base
,
1461 enum ldb_scope scope
,
1462 struct ldb_parse_tree
*tree
,
1463 const char * const *attrs
,
1464 struct ldb_control
**controls
,
1466 ldb_request_callback_t callback
,
1467 struct ldb_request
*parent
)
1469 struct ldb_request
*req
;
1473 req
= ldb_build_req_common(mem_ctx
, ldb
, controls
,
1474 context
, callback
, parent
);
1477 return LDB_ERR_OPERATIONS_ERROR
;
1480 req
->operation
= LDB_SEARCH
;
1482 req
->op
.search
.base
= ldb_dn_new(req
, ldb
, NULL
);
1483 if (req
->op
.search
.base
== NULL
) {
1485 return LDB_ERR_OPERATIONS_ERROR
;
1488 req
->op
.search
.base
= base
;
1490 req
->op
.search
.scope
= scope
;
1492 req
->op
.search
.tree
= tree
;
1493 if (req
->op
.search
.tree
== NULL
) {
1494 ldb_set_errstring(ldb
, "'tree' can't be NULL");
1496 return LDB_ERR_OPERATIONS_ERROR
;
1499 req
->op
.search
.attrs
= attrs
;
1504 int ldb_build_search_req(struct ldb_request
**ret_req
,
1505 struct ldb_context
*ldb
,
1506 TALLOC_CTX
*mem_ctx
,
1507 struct ldb_dn
*base
,
1508 enum ldb_scope scope
,
1509 const char *expression
,
1510 const char * const *attrs
,
1511 struct ldb_control
**controls
,
1513 ldb_request_callback_t callback
,
1514 struct ldb_request
*parent
)
1516 struct ldb_parse_tree
*tree
;
1519 tree
= ldb_parse_tree(mem_ctx
, expression
);
1521 ldb_set_errstring(ldb
, "Unable to parse search expression");
1522 return LDB_ERR_OPERATIONS_ERROR
;
1525 ret
= ldb_build_search_req_ex(ret_req
, ldb
, mem_ctx
, base
,
1526 scope
, tree
, attrs
, controls
,
1527 context
, callback
, parent
);
1528 if (ret
== LDB_SUCCESS
) {
1529 talloc_steal(*ret_req
, tree
);
1534 int ldb_build_add_req(struct ldb_request
**ret_req
,
1535 struct ldb_context
*ldb
,
1536 TALLOC_CTX
*mem_ctx
,
1537 const struct ldb_message
*message
,
1538 struct ldb_control
**controls
,
1540 ldb_request_callback_t callback
,
1541 struct ldb_request
*parent
)
1543 struct ldb_request
*req
;
1547 req
= ldb_build_req_common(mem_ctx
, ldb
, controls
,
1548 context
, callback
, parent
);
1550 ldb_set_errstring(ldb
, "Out of Memory");
1551 return LDB_ERR_OPERATIONS_ERROR
;
1554 req
->operation
= LDB_ADD
;
1555 req
->op
.add
.message
= message
;
1560 int ldb_build_mod_req(struct ldb_request
**ret_req
,
1561 struct ldb_context
*ldb
,
1562 TALLOC_CTX
*mem_ctx
,
1563 const struct ldb_message
*message
,
1564 struct ldb_control
**controls
,
1566 ldb_request_callback_t callback
,
1567 struct ldb_request
*parent
)
1569 struct ldb_request
*req
;
1573 req
= ldb_build_req_common(mem_ctx
, ldb
, controls
,
1574 context
, callback
, parent
);
1576 ldb_set_errstring(ldb
, "Out of Memory");
1577 return LDB_ERR_OPERATIONS_ERROR
;
1580 req
->operation
= LDB_MODIFY
;
1581 req
->op
.mod
.message
= message
;
1587 int ldb_build_del_req(struct ldb_request
**ret_req
,
1588 struct ldb_context
*ldb
,
1589 TALLOC_CTX
*mem_ctx
,
1591 struct ldb_control
**controls
,
1593 ldb_request_callback_t callback
,
1594 struct ldb_request
*parent
)
1596 struct ldb_request
*req
;
1600 req
= ldb_build_req_common(mem_ctx
, ldb
, controls
,
1601 context
, callback
, parent
);
1603 ldb_set_errstring(ldb
, "Out of Memory");
1604 return LDB_ERR_OPERATIONS_ERROR
;
1607 req
->operation
= LDB_DELETE
;
1608 req
->op
.del
.dn
= dn
;
1613 int ldb_build_rename_req(struct ldb_request
**ret_req
,
1614 struct ldb_context
*ldb
,
1615 TALLOC_CTX
*mem_ctx
,
1616 struct ldb_dn
*olddn
,
1617 struct ldb_dn
*newdn
,
1618 struct ldb_control
**controls
,
1620 ldb_request_callback_t callback
,
1621 struct ldb_request
*parent
)
1623 struct ldb_request
*req
;
1627 req
= ldb_build_req_common(mem_ctx
, ldb
, controls
,
1628 context
, callback
, parent
);
1630 ldb_set_errstring(ldb
, "Out of Memory");
1631 return LDB_ERR_OPERATIONS_ERROR
;
1634 req
->operation
= LDB_RENAME
;
1635 req
->op
.rename
.olddn
= olddn
;
1636 req
->op
.rename
.newdn
= newdn
;
1641 int ldb_extended_default_callback(struct ldb_request
*req
,
1642 struct ldb_reply
*ares
)
1644 struct ldb_result
*res
;
1646 res
= talloc_get_type(req
->context
, struct ldb_result
);
1649 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
1651 if (ares
->error
!= LDB_SUCCESS
) {
1652 return ldb_request_done(req
, ares
->error
);
1655 if (ares
->type
== LDB_REPLY_DONE
) {
1657 /* TODO: we should really support controls on entries and referrals too! */
1658 res
->extended
= talloc_move(res
, &ares
->response
);
1659 res
->controls
= talloc_move(res
, &ares
->controls
);
1662 return ldb_request_done(req
, LDB_SUCCESS
);
1666 ldb_asprintf_errstring(req
->handle
->ldb
, "Invalid LDB reply type %d", ares
->type
);
1667 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
1670 int ldb_build_extended_req(struct ldb_request
**ret_req
,
1671 struct ldb_context
*ldb
,
1672 TALLOC_CTX
*mem_ctx
,
1675 struct ldb_control
**controls
,
1677 ldb_request_callback_t callback
,
1678 struct ldb_request
*parent
)
1680 struct ldb_request
*req
;
1684 req
= ldb_build_req_common(mem_ctx
, ldb
, controls
,
1685 context
, callback
, parent
);
1687 ldb_set_errstring(ldb
, "Out of Memory");
1688 return LDB_ERR_OPERATIONS_ERROR
;
1691 req
->operation
= LDB_EXTENDED
;
1692 req
->op
.extended
.oid
= oid
;
1693 req
->op
.extended
.data
= data
;
1698 int ldb_extended(struct ldb_context
*ldb
,
1701 struct ldb_result
**_res
)
1703 struct ldb_request
*req
;
1705 struct ldb_result
*res
;
1710 res
= talloc_zero(ldb
, struct ldb_result
);
1712 return LDB_ERR_OPERATIONS_ERROR
;
1715 ret
= ldb_build_extended_req(&req
, ldb
, ldb
,
1717 res
, ldb_extended_default_callback
,
1719 ldb_req_set_location(req
, "ldb_extended");
1721 if (ret
!= LDB_SUCCESS
) goto done
;
1723 ldb_set_timeout(ldb
, req
, 0); /* use default timeout */
1725 ret
= ldb_request(ldb
, req
);
1727 if (ret
== LDB_SUCCESS
) {
1728 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
1732 if (ret
!= LDB_SUCCESS
) {
1744 note that ldb_search() will automatically replace a NULL 'base' value
1745 with the defaultNamingContext from the rootDSE if available.
1747 int ldb_search(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
1748 struct ldb_result
**result
, struct ldb_dn
*base
,
1749 enum ldb_scope scope
, const char * const *attrs
,
1750 const char *exp_fmt
, ...)
1752 struct ldb_request
*req
;
1753 struct ldb_result
*res
;
1762 res
= talloc_zero(mem_ctx
, struct ldb_result
);
1764 return LDB_ERR_OPERATIONS_ERROR
;
1768 va_start(ap
, exp_fmt
);
1769 expression
= talloc_vasprintf(mem_ctx
, exp_fmt
, ap
);
1774 return LDB_ERR_OPERATIONS_ERROR
;
1778 ret
= ldb_build_search_req(&req
, ldb
, mem_ctx
,
1779 base
?base
:ldb_get_default_basedn(ldb
),
1785 ldb_search_default_callback
,
1787 ldb_req_set_location(req
, "ldb_search");
1789 if (ret
!= LDB_SUCCESS
) goto done
;
1791 ret
= ldb_request(ldb
, req
);
1793 if (ret
== LDB_SUCCESS
) {
1794 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
1798 if (ret
!= LDB_SUCCESS
) {
1803 talloc_free(expression
);
1811 add a record to the database. Will fail if a record with the given class
1812 and key already exists
1814 int ldb_add(struct ldb_context
*ldb
,
1815 const struct ldb_message
*message
)
1817 struct ldb_request
*req
;
1820 ret
= ldb_msg_sanity_check(ldb
, message
);
1821 if (ret
!= LDB_SUCCESS
) {
1825 ret
= ldb_build_add_req(&req
, ldb
, ldb
,
1829 ldb_op_default_callback
,
1831 ldb_req_set_location(req
, "ldb_add");
1833 if (ret
!= LDB_SUCCESS
) return ret
;
1835 /* do request and autostart a transaction */
1836 ret
= ldb_autotransaction_request(ldb
, req
);
1843 modify the specified attributes of a record
1845 int ldb_modify(struct ldb_context
*ldb
,
1846 const struct ldb_message
*message
)
1848 struct ldb_request
*req
;
1851 ret
= ldb_msg_sanity_check(ldb
, message
);
1852 if (ret
!= LDB_SUCCESS
) {
1856 ret
= ldb_build_mod_req(&req
, ldb
, ldb
,
1860 ldb_op_default_callback
,
1862 ldb_req_set_location(req
, "ldb_modify");
1864 if (ret
!= LDB_SUCCESS
) return ret
;
1866 /* do request and autostart a transaction */
1867 ret
= ldb_autotransaction_request(ldb
, req
);
1875 delete a record from the database
1877 int ldb_delete(struct ldb_context
*ldb
, struct ldb_dn
*dn
)
1879 struct ldb_request
*req
;
1882 ret
= ldb_build_del_req(&req
, ldb
, ldb
,
1886 ldb_op_default_callback
,
1888 ldb_req_set_location(req
, "ldb_delete");
1890 if (ret
!= LDB_SUCCESS
) return ret
;
1892 /* do request and autostart a transaction */
1893 ret
= ldb_autotransaction_request(ldb
, req
);
1900 rename a record in the database
1902 int ldb_rename(struct ldb_context
*ldb
,
1903 struct ldb_dn
*olddn
, struct ldb_dn
*newdn
)
1905 struct ldb_request
*req
;
1908 ret
= ldb_build_rename_req(&req
, ldb
, ldb
,
1913 ldb_op_default_callback
,
1915 ldb_req_set_location(req
, "ldb_rename");
1917 if (ret
!= LDB_SUCCESS
) return ret
;
1919 /* do request and autostart a transaction */
1920 ret
= ldb_autotransaction_request(ldb
, req
);
1928 return the global sequence number
1930 int ldb_sequence_number(struct ldb_context
*ldb
,
1931 enum ldb_sequence_type type
, uint64_t *seq_num
)
1933 struct ldb_seqnum_request
*seq
;
1934 struct ldb_seqnum_result
*seqr
;
1935 struct ldb_result
*res
;
1936 TALLOC_CTX
*tmp_ctx
;
1941 tmp_ctx
= talloc_zero(ldb
, struct ldb_request
);
1942 if (tmp_ctx
== NULL
) {
1943 ldb_set_errstring(ldb
, "Out of Memory");
1944 return LDB_ERR_OPERATIONS_ERROR
;
1946 seq
= talloc_zero(tmp_ctx
, struct ldb_seqnum_request
);
1948 ldb_set_errstring(ldb
, "Out of Memory");
1949 ret
= LDB_ERR_OPERATIONS_ERROR
;
1954 ret
= ldb_extended(ldb
, LDB_EXTENDED_SEQUENCE_NUMBER
, seq
, &res
);
1955 if (ret
!= LDB_SUCCESS
) {
1958 talloc_steal(tmp_ctx
, res
);
1960 if (strcmp(LDB_EXTENDED_SEQUENCE_NUMBER
, res
->extended
->oid
) != 0) {
1961 ldb_set_errstring(ldb
, "Invalid OID in reply");
1962 ret
= LDB_ERR_OPERATIONS_ERROR
;
1965 seqr
= talloc_get_type(res
->extended
->data
,
1966 struct ldb_seqnum_result
);
1967 *seq_num
= seqr
->seq_num
;
1970 talloc_free(tmp_ctx
);
1975 return extended error information
1977 const char *ldb_errstring(struct ldb_context
*ldb
)
1979 return ldb
->err_string
;
1983 return a string explaining what a ldb error constant means
1985 const char *ldb_strerror(int ldb_err
)
1990 case LDB_ERR_OPERATIONS_ERROR
:
1991 return "Operations error";
1992 case LDB_ERR_PROTOCOL_ERROR
:
1993 return "Protocol error";
1994 case LDB_ERR_TIME_LIMIT_EXCEEDED
:
1995 return "Time limit exceeded";
1996 case LDB_ERR_SIZE_LIMIT_EXCEEDED
:
1997 return "Size limit exceeded";
1998 case LDB_ERR_COMPARE_FALSE
:
1999 return "Compare false";
2000 case LDB_ERR_COMPARE_TRUE
:
2001 return "Compare true";
2002 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED
:
2003 return "Auth method not supported";
2004 case LDB_ERR_STRONG_AUTH_REQUIRED
:
2005 return "Strong auth required";
2007 case LDB_ERR_REFERRAL
:
2008 return "Referral error";
2009 case LDB_ERR_ADMIN_LIMIT_EXCEEDED
:
2010 return "Admin limit exceeded";
2011 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION
:
2012 return "Unsupported critical extension";
2013 case LDB_ERR_CONFIDENTIALITY_REQUIRED
:
2014 return "Confidentiality required";
2015 case LDB_ERR_SASL_BIND_IN_PROGRESS
:
2016 return "SASL bind in progress";
2017 case LDB_ERR_NO_SUCH_ATTRIBUTE
:
2018 return "No such attribute";
2019 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE
:
2020 return "Undefined attribute type";
2021 case LDB_ERR_INAPPROPRIATE_MATCHING
:
2022 return "Inappropriate matching";
2023 case LDB_ERR_CONSTRAINT_VIOLATION
:
2024 return "Constraint violation";
2025 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
:
2026 return "Attribute or value exists";
2027 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
:
2028 return "Invalid attribute syntax";
2030 case LDB_ERR_NO_SUCH_OBJECT
:
2031 return "No such object";
2032 case LDB_ERR_ALIAS_PROBLEM
:
2033 return "Alias problem";
2034 case LDB_ERR_INVALID_DN_SYNTAX
:
2035 return "Invalid DN syntax";
2037 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM
:
2038 return "Alias dereferencing problem";
2040 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION
:
2041 return "Inappropriate authentication";
2042 case LDB_ERR_INVALID_CREDENTIALS
:
2043 return "Invalid credentials";
2044 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
:
2045 return "insufficient access rights";
2048 case LDB_ERR_UNAVAILABLE
:
2049 return "Unavailable";
2050 case LDB_ERR_UNWILLING_TO_PERFORM
:
2051 return "Unwilling to perform";
2052 case LDB_ERR_LOOP_DETECT
:
2053 return "Loop detect";
2055 case LDB_ERR_NAMING_VIOLATION
:
2056 return "Naming violation";
2057 case LDB_ERR_OBJECT_CLASS_VIOLATION
:
2058 return "Object class violation";
2059 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF
:
2060 return "Not allowed on non-leaf";
2061 case LDB_ERR_NOT_ALLOWED_ON_RDN
:
2062 return "Not allowed on RDN";
2063 case LDB_ERR_ENTRY_ALREADY_EXISTS
:
2064 return "Entry already exists";
2065 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED
:
2066 return "Object class mods prohibited";
2067 /* 70 RESERVED FOR CLDAP */
2068 case LDB_ERR_AFFECTS_MULTIPLE_DSAS
:
2069 return "Affects multiple DSAs";
2075 return "Unknown error";
2079 set backend specific opaque parameters
2081 int ldb_set_opaque(struct ldb_context
*ldb
, const char *name
, void *value
)
2083 struct ldb_opaque
*o
;
2085 /* allow updating an existing value */
2086 for (o
=ldb
->opaque
;o
;o
=o
->next
) {
2087 if (strcmp(o
->name
, name
) == 0) {
2093 o
= talloc(ldb
, struct ldb_opaque
);
2096 return LDB_ERR_OTHER
;
2098 o
->next
= ldb
->opaque
;
2106 get a previously set opaque value
2108 void *ldb_get_opaque(struct ldb_context
*ldb
, const char *name
)
2110 struct ldb_opaque
*o
;
2111 for (o
=ldb
->opaque
;o
;o
=o
->next
) {
2112 if (strcmp(o
->name
, name
) == 0) {
2119 int ldb_global_init(void)
2121 /* Provided for compatibility with some older versions of ldb */
2125 /* return the ldb flags */
2126 unsigned int ldb_get_flags(struct ldb_context
*ldb
)
2131 /* set the ldb flags */
2132 void ldb_set_flags(struct ldb_context
*ldb
, unsigned flags
)
2139 set the location in a ldb request. Used for debugging
2141 void ldb_req_set_location(struct ldb_request
*req
, const char *location
)
2143 if (req
&& req
->handle
) {
2144 req
->handle
->location
= location
;
2149 return the location set with dsdb_req_set_location
2151 const char *ldb_req_location(struct ldb_request
*req
)
2153 return req
->handle
->location
;
2157 mark a request as untrusted. This tells the rootdse module to remove
2158 unregistered controls
2160 void ldb_req_mark_untrusted(struct ldb_request
*req
)
2162 req
->handle
->flags
|= LDB_HANDLE_FLAG_UNTRUSTED
;
2166 mark a request as trusted.
2168 void ldb_req_mark_trusted(struct ldb_request
*req
)
2170 req
->handle
->flags
&= ~LDB_HANDLE_FLAG_UNTRUSTED
;
2174 set custom flags. Those flags are set by applications using ldb,
2175 they are application dependent and the same bit can have different
2176 meaning in different application.
2178 void ldb_req_set_custom_flags(struct ldb_request
*req
, uint32_t flags
)
2180 if (req
!= NULL
&& req
->handle
!= NULL
) {
2181 req
->handle
->custom_flags
= flags
;
2187 get custom flags. Those flags are set by applications using ldb,
2188 they are application dependent and the same bit can have different
2189 meaning in different application.
2191 uint32_t ldb_req_get_custom_flags(struct ldb_request
*req
)
2193 if (req
!= NULL
&& req
->handle
!= NULL
) {
2194 return req
->handle
->custom_flags
;
2198 * 0 is not something any better or worse than
2199 * anything else as req or the handle is NULL
2206 * return true if a request is untrusted
2208 bool ldb_req_is_untrusted(struct ldb_request
*req
)
2210 return (req
->handle
->flags
& LDB_HANDLE_FLAG_UNTRUSTED
) != 0;