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"
38 static int ldb_context_destructor(void *ptr
)
40 struct ldb_context
*ldb
= talloc_get_type(ptr
, struct ldb_context
);
42 if (ldb
->transaction_active
) {
43 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
44 "A transaction is still active in ldb context [%p]",
52 this is used to catch debug messages from events
54 static void ldb_tevent_debug(void *context
, enum tevent_debug_level level
,
55 const char *fmt
, va_list ap
) PRINTF_ATTRIBUTE(3,0);
57 static void ldb_tevent_debug(void *context
, enum tevent_debug_level level
,
58 const char *fmt
, va_list ap
)
60 struct ldb_context
*ldb
= talloc_get_type(context
, struct ldb_context
);
61 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 vasprintf(&s
, fmt
, ap
);
81 ldb_debug(ldb
, ldb_level
, "tevent: %s", s
);
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
;
95 ldb
= talloc_zero(mem_ctx
, struct ldb_context
);
96 /* FIXME: Hack a new event context so that CMD line utilities work
97 * until we have them all converted */
99 ev_ctx
= tevent_context_init(talloc_autofree_context());
100 tevent_set_debug(ev_ctx
, ldb_tevent_debug
, ldb
);
101 tevent_loop_allow_nesting(ev_ctx
);
104 ret
= ldb_setup_wellknown_attributes(ldb
);
110 ldb_set_utf8_default(ldb
);
111 ldb_set_create_perms(ldb
, 0666);
112 ldb_set_modules_dir(ldb
, LDB_MODULESDIR
);
113 ldb_set_event_context(ldb
, ev_ctx
);
115 /* TODO: get timeout from options if available there */
116 ldb
->default_timeout
= 300; /* set default to 5 minutes */
118 talloc_set_destructor((TALLOC_CTX
*)ldb
, ldb_context_destructor
);
124 try to autodetect a basedn if none specified. This fixes one of my
125 pet hates about ldapsearch, which is that you have to get a long,
126 complex basedn right to make any use of it.
128 void ldb_set_default_dns(struct ldb_context
*ldb
)
132 struct ldb_result
*res
;
133 struct ldb_dn
*tmp_dn
=NULL
;
134 static const char *attrs
[] = {
135 "rootDomainNamingContext",
136 "configurationNamingContext",
137 "schemaNamingContext",
138 "defaultNamingContext",
142 tmp_ctx
= talloc_new(ldb
);
143 ret
= ldb_search(ldb
, tmp_ctx
, &res
, ldb_dn_new(tmp_ctx
, ldb
, NULL
),
144 LDB_SCOPE_BASE
, attrs
, "(objectClass=*)");
145 if (ret
!= LDB_SUCCESS
) {
146 talloc_free(tmp_ctx
);
150 if (res
->count
!= 1) {
151 talloc_free(tmp_ctx
);
155 if (!ldb_get_opaque(ldb
, "rootDomainNamingContext")) {
156 tmp_dn
= ldb_msg_find_attr_as_dn(ldb
, ldb
, res
->msgs
[0],
157 "rootDomainNamingContext");
158 ldb_set_opaque(ldb
, "rootDomainNamingContext", tmp_dn
);
161 if (!ldb_get_opaque(ldb
, "configurationNamingContext")) {
162 tmp_dn
= ldb_msg_find_attr_as_dn(ldb
, ldb
, res
->msgs
[0],
163 "configurationNamingContext");
164 ldb_set_opaque(ldb
, "configurationNamingContext", tmp_dn
);
167 if (!ldb_get_opaque(ldb
, "schemaNamingContext")) {
168 tmp_dn
= ldb_msg_find_attr_as_dn(ldb
, ldb
, res
->msgs
[0],
169 "schemaNamingContext");
170 ldb_set_opaque(ldb
, "schemaNamingContext", tmp_dn
);
173 if (!ldb_get_opaque(ldb
, "defaultNamingContext")) {
174 tmp_dn
= ldb_msg_find_attr_as_dn(ldb
, ldb
, res
->msgs
[0],
175 "defaultNamingContext");
176 ldb_set_opaque(ldb
, "defaultNamingContext", tmp_dn
);
179 talloc_free(tmp_ctx
);
182 struct ldb_dn
*ldb_get_root_basedn(struct ldb_context
*ldb
)
184 void *opaque
= ldb_get_opaque(ldb
, "rootDomainNamingContext");
185 return talloc_get_type(opaque
, struct ldb_dn
);
188 struct ldb_dn
*ldb_get_config_basedn(struct ldb_context
*ldb
)
190 void *opaque
= ldb_get_opaque(ldb
, "configurationNamingContext");
191 return talloc_get_type(opaque
, struct ldb_dn
);
194 struct ldb_dn
*ldb_get_schema_basedn(struct ldb_context
*ldb
)
196 void *opaque
= ldb_get_opaque(ldb
, "schemaNamingContext");
197 return talloc_get_type(opaque
, struct ldb_dn
);
200 struct ldb_dn
*ldb_get_default_basedn(struct ldb_context
*ldb
)
202 void *opaque
= ldb_get_opaque(ldb
, "defaultNamingContext");
203 return talloc_get_type(opaque
, struct ldb_dn
);
207 connect to a database. The URL can either be one of the following forms
211 flags is made up of LDB_FLG_*
213 the options are passed uninterpreted to the backend, and are
216 int ldb_connect(struct ldb_context
*ldb
, const char *url
,
217 unsigned int flags
, const char *options
[])
221 /* We seem to need to do this here, or else some utilities don't
222 * get ldb backends */
226 url2
= talloc_strdup(ldb
, url
);
229 return LDB_ERR_OPERATIONS_ERROR
;
231 ret
= ldb_set_opaque(ldb
, "ldb_url", talloc_strdup(ldb
, url2
));
232 if (ret
!= LDB_SUCCESS
) {
236 ret
= ldb_connect_backend(ldb
, url
, options
, &ldb
->modules
);
237 if (ret
!= LDB_SUCCESS
) {
241 if (ldb_load_modules(ldb
, options
) != LDB_SUCCESS
) {
242 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
243 "Unable to load modules for %s: %s",
244 url
, ldb_errstring(ldb
));
245 return LDB_ERR_OTHER
;
248 /* set the default base dn */
249 ldb_set_default_dns(ldb
);
254 void ldb_set_errstring(struct ldb_context
*ldb
, const char *err_string
)
256 if (ldb
->err_string
) {
257 talloc_free(ldb
->err_string
);
259 ldb
->err_string
= talloc_strdup(ldb
, err_string
);
262 void ldb_asprintf_errstring(struct ldb_context
*ldb
, const char *format
, ...)
265 char *old_string
= NULL
;
267 if (ldb
->err_string
) {
268 old_string
= ldb
->err_string
;
271 va_start(ap
, format
);
272 ldb
->err_string
= talloc_vasprintf(ldb
, format
, ap
);
274 talloc_free(old_string
);
277 void ldb_reset_err_string(struct ldb_context
*ldb
)
279 if (ldb
->err_string
) {
280 talloc_free(ldb
->err_string
);
281 ldb
->err_string
= NULL
;
285 #define FIRST_OP_NOERR(ldb, op) do { \
286 module = ldb->modules; \
287 while (module && module->ops->op == NULL) module = module->next; \
290 #define FIRST_OP(ldb, op) do { \
291 FIRST_OP_NOERR(ldb, op); \
292 if (module == NULL) { \
293 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
294 return LDB_ERR_OPERATIONS_ERROR; \
302 int ldb_transaction_start(struct ldb_context
*ldb
)
304 struct ldb_module
*module
;
307 ldb_debug(ldb
, LDB_DEBUG_TRACE
,
308 "start ldb transaction (nesting: %d)",
309 ldb
->transaction_active
);
311 /* explicit transaction active, count nested requests */
312 if (ldb
->transaction_active
) {
313 ldb
->transaction_active
++;
317 /* start a new transaction */
318 ldb
->transaction_active
++;
319 ldb
->prepare_commit_done
= false;
321 FIRST_OP(ldb
, start_transaction
);
323 ldb_reset_err_string(ldb
);
325 status
= module
->ops
->start_transaction(module
);
326 if (status
!= LDB_SUCCESS
) {
327 if (ldb
->err_string
== NULL
) {
328 /* no error string was setup by the backend */
329 ldb_asprintf_errstring(ldb
,
330 "ldb transaction start: %s (%d)",
331 ldb_strerror(status
),
339 prepare for transaction commit (first phase of two phase commit)
341 int ldb_transaction_prepare_commit(struct ldb_context
*ldb
)
343 struct ldb_module
*module
;
346 if (ldb
->prepare_commit_done
) {
350 /* commit only when all nested transactions are complete */
351 if (ldb
->transaction_active
> 1) {
355 ldb
->prepare_commit_done
= true;
357 if (ldb
->transaction_active
< 0) {
358 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
359 "prepare commit called but no ldb transactions are active!");
360 ldb
->transaction_active
= 0;
361 return LDB_ERR_OPERATIONS_ERROR
;
364 /* call prepare transaction if available */
365 FIRST_OP_NOERR(ldb
, prepare_commit
);
366 if (module
== NULL
) {
370 status
= module
->ops
->prepare_commit(module
);
371 if (status
!= LDB_SUCCESS
) {
372 /* if a module fails the prepare then we need
373 to call the end transaction for everyone */
374 FIRST_OP(ldb
, end_transaction
);
375 module
->ops
->end_transaction(module
);
376 if (ldb
->err_string
== NULL
) {
377 /* no error string was setup by the backend */
378 ldb_asprintf_errstring(ldb
,
379 "ldb transaction prepare commit: %s (%d)",
380 ldb_strerror(status
),
392 int ldb_transaction_commit(struct ldb_context
*ldb
)
394 struct ldb_module
*module
;
397 status
= ldb_transaction_prepare_commit(ldb
);
398 if (status
!= LDB_SUCCESS
) {
402 ldb
->transaction_active
--;
404 ldb_debug(ldb
, LDB_DEBUG_TRACE
,
405 "commit ldb transaction (nesting: %d)",
406 ldb
->transaction_active
);
408 /* commit only when all nested transactions are complete */
409 if (ldb
->transaction_active
> 0) {
413 if (ldb
->transaction_active
< 0) {
414 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
415 "commit called but no ldb transactions are active!");
416 ldb
->transaction_active
= 0;
417 return LDB_ERR_OPERATIONS_ERROR
;
420 ldb_reset_err_string(ldb
);
422 FIRST_OP(ldb
, end_transaction
);
423 status
= module
->ops
->end_transaction(module
);
424 if (status
!= LDB_SUCCESS
) {
425 if (ldb
->err_string
== NULL
) {
426 /* no error string was setup by the backend */
427 ldb_asprintf_errstring(ldb
,
428 "ldb transaction commit: %s (%d)",
429 ldb_strerror(status
),
432 /* cancel the transaction */
433 FIRST_OP(ldb
, del_transaction
);
434 module
->ops
->del_transaction(module
);
443 int ldb_transaction_cancel(struct ldb_context
*ldb
)
445 struct ldb_module
*module
;
448 ldb
->transaction_active
--;
450 ldb_debug(ldb
, LDB_DEBUG_TRACE
,
451 "cancel ldb transaction (nesting: %d)",
452 ldb
->transaction_active
);
454 /* really cancel only if all nested transactions are complete */
455 if (ldb
->transaction_active
> 0) {
459 if (ldb
->transaction_active
< 0) {
460 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
461 "cancel called but no ldb transactions are active!");
462 ldb
->transaction_active
= 0;
463 return LDB_ERR_OPERATIONS_ERROR
;
466 FIRST_OP(ldb
, del_transaction
);
468 status
= module
->ops
->del_transaction(module
);
469 if (status
!= LDB_SUCCESS
) {
470 if (ldb
->err_string
== NULL
) {
471 /* no error string was setup by the backend */
472 ldb_asprintf_errstring(ldb
,
473 "ldb transaction cancel: %s (%d)",
474 ldb_strerror(status
),
481 /* autostarts a transacion if none active */
482 static int ldb_autotransaction_request(struct ldb_context
*ldb
,
483 struct ldb_request
*req
)
487 ret
= ldb_transaction_start(ldb
);
488 if (ret
!= LDB_SUCCESS
) {
492 ret
= ldb_request(ldb
, req
);
493 if (ret
== LDB_SUCCESS
) {
494 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
497 if (ret
== LDB_SUCCESS
) {
498 return ldb_transaction_commit(ldb
);
500 ldb_transaction_cancel(ldb
);
502 if (ldb
->err_string
== NULL
) {
503 /* no error string was setup by the backend */
504 ldb_asprintf_errstring(ldb
, "%s (%d)", ldb_strerror(ret
), ret
);
510 int ldb_wait(struct ldb_handle
*handle
, enum ldb_wait_type type
)
512 struct tevent_context
*ev
;
516 return LDB_ERR_UNAVAILABLE
;
519 if (handle
->state
== LDB_ASYNC_DONE
) {
520 return handle
->status
;
523 ev
= ldb_get_event_context(handle
->ldb
);
525 return LDB_ERR_OPERATIONS_ERROR
;
530 ret
= tevent_loop_once(ev
);
532 return LDB_ERR_OPERATIONS_ERROR
;
534 if (handle
->state
== LDB_ASYNC_DONE
||
535 handle
->status
!= LDB_SUCCESS
) {
536 return handle
->status
;
541 while (handle
->state
!= LDB_ASYNC_DONE
) {
542 ret
= tevent_loop_once(ev
);
544 return LDB_ERR_OPERATIONS_ERROR
;
546 if (handle
->status
!= LDB_SUCCESS
) {
547 return handle
->status
;
550 return handle
->status
;
556 /* set the specified timeout or, if timeout is 0 set the default timeout */
557 int ldb_set_timeout(struct ldb_context
*ldb
,
558 struct ldb_request
*req
,
561 if (req
== NULL
) return LDB_ERR_OPERATIONS_ERROR
;
564 req
->timeout
= timeout
;
566 req
->timeout
= ldb
->default_timeout
;
568 req
->starttime
= time(NULL
);
573 /* calculates the new timeout based on the previous starttime and timeout */
574 int ldb_set_timeout_from_prev_req(struct ldb_context
*ldb
,
575 struct ldb_request
*oldreq
,
576 struct ldb_request
*newreq
)
578 if (newreq
== NULL
) return LDB_ERR_OPERATIONS_ERROR
;
580 if (oldreq
== NULL
) {
581 return ldb_set_timeout(ldb
, newreq
, 0);
584 newreq
->starttime
= oldreq
->starttime
;
585 newreq
->timeout
= oldreq
->timeout
;
592 set the permissions for new files to be passed to open() in
593 backends that use local files
595 void ldb_set_create_perms(struct ldb_context
*ldb
, unsigned int perms
)
597 ldb
->create_perms
= perms
;
600 unsigned int ldb_get_create_perms(struct ldb_context
*ldb
)
602 return ldb
->create_perms
;
605 void ldb_set_event_context(struct ldb_context
*ldb
, struct tevent_context
*ev
)
610 struct tevent_context
* ldb_get_event_context(struct ldb_context
*ldb
)
615 void ldb_request_set_state(struct ldb_request
*req
, int state
)
617 req
->handle
->state
= state
;
620 int ldb_request_get_status(struct ldb_request
*req
)
622 return req
->handle
->status
;
627 NOTE: the request must be a talloc context.
628 returns LDB_ERR_* on errors.
630 int ldb_request(struct ldb_context
*ldb
, struct ldb_request
*req
)
632 struct ldb_module
*module
;
635 if (req
->callback
== NULL
) {
636 ldb_set_errstring(ldb
, "Requests MUST define callbacks");
637 return LDB_ERR_UNWILLING_TO_PERFORM
;
640 ldb_reset_err_string(ldb
);
642 /* call the first module in the chain */
643 switch (req
->operation
) {
645 FIRST_OP(ldb
, search
);
646 ret
= module
->ops
->search(module
, req
);
650 ret
= module
->ops
->add(module
, req
);
653 FIRST_OP(ldb
, modify
);
654 ret
= module
->ops
->modify(module
, req
);
658 ret
= module
->ops
->del(module
, req
);
661 FIRST_OP(ldb
, rename
);
662 ret
= module
->ops
->rename(module
, req
);
665 FIRST_OP(ldb
, extended
);
666 ret
= module
->ops
->extended(module
, req
);
669 FIRST_OP(ldb
, request
);
670 ret
= module
->ops
->request(module
, req
);
677 int ldb_request_done(struct ldb_request
*req
, int status
)
679 req
->handle
->state
= LDB_ASYNC_DONE
;
680 req
->handle
->status
= status
;
685 search the database given a LDAP-like search expression
687 returns an LDB error code
689 Use talloc_free to free the ldb_message returned in 'res', if successful
692 int ldb_search_default_callback(struct ldb_request
*req
,
693 struct ldb_reply
*ares
)
695 struct ldb_result
*res
;
698 res
= talloc_get_type(req
->context
, struct ldb_result
);
701 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
703 if (ares
->error
!= LDB_SUCCESS
) {
704 return ldb_request_done(req
, ares
->error
);
707 switch (ares
->type
) {
708 case LDB_REPLY_ENTRY
:
709 res
->msgs
= talloc_realloc(res
, res
->msgs
,
710 struct ldb_message
*, res
->count
+ 2);
712 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
715 res
->msgs
[res
->count
+ 1] = NULL
;
717 res
->msgs
[res
->count
] = talloc_move(res
->msgs
, &ares
->message
);
721 case LDB_REPLY_REFERRAL
:
723 for (n
= 0; res
->refs
[n
]; n
++) /*noop*/ ;
728 res
->refs
= talloc_realloc(res
, res
->refs
, char *, n
+ 2);
730 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
733 res
->refs
[n
] = talloc_move(res
->refs
, &ares
->referral
);
734 res
->refs
[n
+ 1] = NULL
;
738 /* TODO: we should really support controls on entries
739 * and referrals too! */
740 res
->controls
= talloc_move(res
, &ares
->controls
);
742 /* this is the last message, and means the request is done */
743 /* we have to signal and eventual ldb_wait() waiting that the
744 * async request operation was completed */
746 return ldb_request_done(req
, LDB_SUCCESS
);
754 int ldb_op_default_callback(struct ldb_request
*req
, struct ldb_reply
*ares
)
759 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
762 if (ares
->error
!= LDB_SUCCESS
) {
765 return ldb_request_done(req
, ret
);
768 if (ares
->type
!= LDB_REPLY_DONE
) {
770 ldb_set_errstring(req
->handle
->ldb
, "Invalid reply type!");
771 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
775 return ldb_request_done(req
, LDB_SUCCESS
);
778 int ldb_build_search_req_ex(struct ldb_request
**ret_req
,
779 struct ldb_context
*ldb
,
782 enum ldb_scope scope
,
783 struct ldb_parse_tree
*tree
,
784 const char * const *attrs
,
785 struct ldb_control
**controls
,
787 ldb_request_callback_t callback
,
788 struct ldb_request
*parent
)
790 struct ldb_request
*req
;
794 req
= talloc(mem_ctx
, struct ldb_request
);
797 return LDB_ERR_OPERATIONS_ERROR
;
800 req
->operation
= LDB_SEARCH
;
802 req
->op
.search
.base
= ldb_dn_new(req
, ldb
, NULL
);
804 req
->op
.search
.base
= base
;
806 req
->op
.search
.scope
= scope
;
808 req
->op
.search
.tree
= tree
;
809 if (req
->op
.search
.tree
== NULL
) {
810 ldb_set_errstring(ldb
, "'tree' can't be NULL");
812 return LDB_ERR_OPERATIONS_ERROR
;
815 req
->op
.search
.attrs
= attrs
;
816 req
->controls
= controls
;
817 req
->context
= context
;
818 req
->callback
= callback
;
820 ldb_set_timeout_from_prev_req(ldb
, parent
, req
);
822 req
->handle
= ldb_handle_new(req
, ldb
);
823 if (req
->handle
== NULL
) {
825 return LDB_ERR_OPERATIONS_ERROR
;
832 int ldb_build_search_req(struct ldb_request
**ret_req
,
833 struct ldb_context
*ldb
,
836 enum ldb_scope scope
,
837 const char *expression
,
838 const char * const *attrs
,
839 struct ldb_control
**controls
,
841 ldb_request_callback_t callback
,
842 struct ldb_request
*parent
)
844 struct ldb_parse_tree
*tree
;
847 tree
= ldb_parse_tree(mem_ctx
, expression
);
849 ldb_set_errstring(ldb
, "Unable to parse search expression");
850 return LDB_ERR_OPERATIONS_ERROR
;
853 ret
= ldb_build_search_req_ex(ret_req
, ldb
, mem_ctx
, base
,
854 scope
, tree
, attrs
, controls
,
855 context
, callback
, parent
);
856 if (ret
== LDB_SUCCESS
) {
857 talloc_steal(*ret_req
, tree
);
862 int ldb_build_add_req(struct ldb_request
**ret_req
,
863 struct ldb_context
*ldb
,
865 const struct ldb_message
*message
,
866 struct ldb_control
**controls
,
868 ldb_request_callback_t callback
,
869 struct ldb_request
*parent
)
871 struct ldb_request
*req
;
875 req
= talloc(mem_ctx
, struct ldb_request
);
877 ldb_set_errstring(ldb
, "Out of Memory");
878 return LDB_ERR_OPERATIONS_ERROR
;
881 req
->operation
= LDB_ADD
;
882 req
->op
.add
.message
= message
;
883 req
->controls
= controls
;
884 req
->context
= context
;
885 req
->callback
= callback
;
887 ldb_set_timeout_from_prev_req(ldb
, parent
, req
);
889 req
->handle
= ldb_handle_new(req
, ldb
);
890 if (req
->handle
== NULL
) {
892 return LDB_ERR_OPERATIONS_ERROR
;
900 int ldb_build_mod_req(struct ldb_request
**ret_req
,
901 struct ldb_context
*ldb
,
903 const struct ldb_message
*message
,
904 struct ldb_control
**controls
,
906 ldb_request_callback_t callback
,
907 struct ldb_request
*parent
)
909 struct ldb_request
*req
;
913 req
= talloc(mem_ctx
, struct ldb_request
);
915 ldb_set_errstring(ldb
, "Out of Memory");
916 return LDB_ERR_OPERATIONS_ERROR
;
919 req
->operation
= LDB_MODIFY
;
920 req
->op
.mod
.message
= message
;
921 req
->controls
= controls
;
922 req
->context
= context
;
923 req
->callback
= callback
;
925 ldb_set_timeout_from_prev_req(ldb
, parent
, req
);
927 req
->handle
= ldb_handle_new(req
, ldb
);
928 if (req
->handle
== NULL
) {
930 return LDB_ERR_OPERATIONS_ERROR
;
938 int ldb_build_del_req(struct ldb_request
**ret_req
,
939 struct ldb_context
*ldb
,
942 struct ldb_control
**controls
,
944 ldb_request_callback_t callback
,
945 struct ldb_request
*parent
)
947 struct ldb_request
*req
;
951 req
= talloc(mem_ctx
, struct ldb_request
);
953 ldb_set_errstring(ldb
, "Out of Memory");
954 return LDB_ERR_OPERATIONS_ERROR
;
957 req
->operation
= LDB_DELETE
;
959 req
->controls
= controls
;
960 req
->context
= context
;
961 req
->callback
= callback
;
963 ldb_set_timeout_from_prev_req(ldb
, parent
, req
);
965 req
->handle
= ldb_handle_new(req
, ldb
);
966 if (req
->handle
== NULL
) {
968 return LDB_ERR_OPERATIONS_ERROR
;
976 int ldb_build_rename_req(struct ldb_request
**ret_req
,
977 struct ldb_context
*ldb
,
979 struct ldb_dn
*olddn
,
980 struct ldb_dn
*newdn
,
981 struct ldb_control
**controls
,
983 ldb_request_callback_t callback
,
984 struct ldb_request
*parent
)
986 struct ldb_request
*req
;
990 req
= talloc(mem_ctx
, struct ldb_request
);
992 ldb_set_errstring(ldb
, "Out of Memory");
993 return LDB_ERR_OPERATIONS_ERROR
;
996 req
->operation
= LDB_RENAME
;
997 req
->op
.rename
.olddn
= olddn
;
998 req
->op
.rename
.newdn
= newdn
;
999 req
->controls
= controls
;
1000 req
->context
= context
;
1001 req
->callback
= callback
;
1003 ldb_set_timeout_from_prev_req(ldb
, parent
, req
);
1005 req
->handle
= ldb_handle_new(req
, ldb
);
1006 if (req
->handle
== NULL
) {
1008 return LDB_ERR_OPERATIONS_ERROR
;
1016 int ldb_extended_default_callback(struct ldb_request
*req
,
1017 struct ldb_reply
*ares
)
1019 struct ldb_result
*res
;
1021 res
= talloc_get_type(req
->context
, struct ldb_result
);
1024 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
1026 if (ares
->error
!= LDB_SUCCESS
) {
1027 return ldb_request_done(req
, ares
->error
);
1030 if (ares
->type
== LDB_REPLY_DONE
) {
1032 /* TODO: we should really support controls on entries and referrals too! */
1033 res
->extended
= talloc_move(res
, &ares
->response
);
1034 res
->controls
= talloc_move(res
, &ares
->controls
);
1037 return ldb_request_done(req
, LDB_SUCCESS
);
1041 ldb_set_errstring(req
->handle
->ldb
, "Invalid reply type!");
1042 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
1045 int ldb_build_extended_req(struct ldb_request
**ret_req
,
1046 struct ldb_context
*ldb
,
1050 struct ldb_control
**controls
,
1052 ldb_request_callback_t callback
,
1053 struct ldb_request
*parent
)
1055 struct ldb_request
*req
;
1059 req
= talloc(mem_ctx
, struct ldb_request
);
1061 ldb_set_errstring(ldb
, "Out of Memory");
1062 return LDB_ERR_OPERATIONS_ERROR
;
1065 req
->operation
= LDB_EXTENDED
;
1066 req
->op
.extended
.oid
= oid
;
1067 req
->op
.extended
.data
= data
;
1068 req
->controls
= controls
;
1069 req
->context
= context
;
1070 req
->callback
= callback
;
1072 ldb_set_timeout_from_prev_req(ldb
, parent
, req
);
1074 req
->handle
= ldb_handle_new(req
, ldb
);
1075 if (req
->handle
== NULL
) {
1077 return LDB_ERR_OPERATIONS_ERROR
;
1085 int ldb_extended(struct ldb_context
*ldb
,
1088 struct ldb_result
**_res
)
1090 struct ldb_request
*req
;
1092 struct ldb_result
*res
;
1096 res
= talloc_zero(ldb
, struct ldb_result
);
1098 return LDB_ERR_OPERATIONS_ERROR
;
1101 ret
= ldb_build_extended_req(&req
, ldb
, ldb
,
1103 res
, ldb_extended_default_callback
,
1105 if (ret
!= LDB_SUCCESS
) goto done
;
1107 ldb_set_timeout(ldb
, req
, 0); /* use default timeout */
1109 ret
= ldb_request(ldb
, req
);
1111 if (ret
== LDB_SUCCESS
) {
1112 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
1118 if (ret
!= LDB_SUCCESS
) {
1127 note that ldb_search() will automatically replace a NULL 'base' value
1128 with the defaultNamingContext from the rootDSE if available.
1130 int ldb_search(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
1131 struct ldb_result
**result
, struct ldb_dn
*base
,
1132 enum ldb_scope scope
, const char * const *attrs
,
1133 const char *exp_fmt
, ...)
1135 struct ldb_request
*req
;
1136 struct ldb_result
*res
;
1145 res
= talloc_zero(mem_ctx
, struct ldb_result
);
1147 return LDB_ERR_OPERATIONS_ERROR
;
1151 va_start(ap
, exp_fmt
);
1152 expression
= talloc_vasprintf(mem_ctx
, exp_fmt
, ap
);
1157 return LDB_ERR_OPERATIONS_ERROR
;
1161 ret
= ldb_build_search_req(&req
, ldb
, mem_ctx
,
1162 base
?base
:ldb_get_default_basedn(ldb
),
1168 ldb_search_default_callback
,
1171 if (ret
!= LDB_SUCCESS
) goto done
;
1173 ret
= ldb_request(ldb
, req
);
1175 if (ret
== LDB_SUCCESS
) {
1176 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
1180 if (ret
!= LDB_SUCCESS
) {
1185 talloc_free(expression
);
1193 add a record to the database. Will fail if a record with the given class
1194 and key already exists
1196 int ldb_add(struct ldb_context
*ldb
,
1197 const struct ldb_message
*message
)
1199 struct ldb_request
*req
;
1202 ret
= ldb_msg_sanity_check(ldb
, message
);
1203 if (ret
!= LDB_SUCCESS
) {
1207 ret
= ldb_build_add_req(&req
, ldb
, ldb
,
1211 ldb_op_default_callback
,
1214 if (ret
!= LDB_SUCCESS
) return ret
;
1216 /* do request and autostart a transaction */
1217 ret
= ldb_autotransaction_request(ldb
, req
);
1224 modify the specified attributes of a record
1226 int ldb_modify(struct ldb_context
*ldb
,
1227 const struct ldb_message
*message
)
1229 struct ldb_request
*req
;
1232 ret
= ldb_msg_sanity_check(ldb
, message
);
1233 if (ret
!= LDB_SUCCESS
) {
1237 ret
= ldb_build_mod_req(&req
, ldb
, ldb
,
1241 ldb_op_default_callback
,
1244 if (ret
!= LDB_SUCCESS
) return ret
;
1246 /* do request and autostart a transaction */
1247 ret
= ldb_autotransaction_request(ldb
, req
);
1255 delete a record from the database
1257 int ldb_delete(struct ldb_context
*ldb
, struct ldb_dn
*dn
)
1259 struct ldb_request
*req
;
1262 ret
= ldb_build_del_req(&req
, ldb
, ldb
,
1266 ldb_op_default_callback
,
1269 if (ret
!= LDB_SUCCESS
) return ret
;
1271 /* do request and autostart a transaction */
1272 ret
= ldb_autotransaction_request(ldb
, req
);
1279 rename a record in the database
1281 int ldb_rename(struct ldb_context
*ldb
,
1282 struct ldb_dn
*olddn
, struct ldb_dn
*newdn
)
1284 struct ldb_request
*req
;
1287 ret
= ldb_build_rename_req(&req
, ldb
, ldb
,
1292 ldb_op_default_callback
,
1295 if (ret
!= LDB_SUCCESS
) return ret
;
1297 /* do request and autostart a transaction */
1298 ret
= ldb_autotransaction_request(ldb
, req
);
1306 return the global sequence number
1308 int ldb_sequence_number(struct ldb_context
*ldb
,
1309 enum ldb_sequence_type type
, uint64_t *seq_num
)
1311 struct ldb_seqnum_request
*seq
;
1312 struct ldb_seqnum_result
*seqr
;
1313 struct ldb_result
*res
;
1314 TALLOC_CTX
*tmp_ctx
;
1319 tmp_ctx
= talloc_zero(ldb
, struct ldb_request
);
1320 if (tmp_ctx
== NULL
) {
1321 ldb_set_errstring(ldb
, "Out of Memory");
1322 return LDB_ERR_OPERATIONS_ERROR
;
1324 seq
= talloc_zero(tmp_ctx
, struct ldb_seqnum_request
);
1326 ldb_set_errstring(ldb
, "Out of Memory");
1327 ret
= LDB_ERR_OPERATIONS_ERROR
;
1332 ret
= ldb_extended(ldb
, LDB_EXTENDED_SEQUENCE_NUMBER
, seq
, &res
);
1333 if (ret
!= LDB_SUCCESS
) {
1336 talloc_steal(tmp_ctx
, res
);
1338 if (strcmp(LDB_EXTENDED_SEQUENCE_NUMBER
, res
->extended
->oid
) != 0) {
1339 ldb_set_errstring(ldb
, "Invalid OID in reply");
1340 ret
= LDB_ERR_OPERATIONS_ERROR
;
1343 seqr
= talloc_get_type(res
->extended
->data
,
1344 struct ldb_seqnum_result
);
1345 *seq_num
= seqr
->seq_num
;
1348 talloc_free(tmp_ctx
);
1353 return extended error information
1355 const char *ldb_errstring(struct ldb_context
*ldb
)
1357 if (ldb
->err_string
) {
1358 return ldb
->err_string
;
1365 return a string explaining what a ldb error constant meancs
1367 const char *ldb_strerror(int ldb_err
)
1372 case LDB_ERR_OPERATIONS_ERROR
:
1373 return "Operations error";
1374 case LDB_ERR_PROTOCOL_ERROR
:
1375 return "Protocol error";
1376 case LDB_ERR_TIME_LIMIT_EXCEEDED
:
1377 return "Time limit exceeded";
1378 case LDB_ERR_SIZE_LIMIT_EXCEEDED
:
1379 return "Size limit exceeded";
1380 case LDB_ERR_COMPARE_FALSE
:
1381 return "Compare false";
1382 case LDB_ERR_COMPARE_TRUE
:
1383 return "Compare true";
1384 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED
:
1385 return "Auth method not supported";
1386 case LDB_ERR_STRONG_AUTH_REQUIRED
:
1387 return "Strong auth required";
1389 case LDB_ERR_REFERRAL
:
1390 return "Referral error";
1391 case LDB_ERR_ADMIN_LIMIT_EXCEEDED
:
1392 return "Admin limit exceeded";
1393 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION
:
1394 return "Unsupported critical extension";
1395 case LDB_ERR_CONFIDENTIALITY_REQUIRED
:
1396 return "Confidentiality required";
1397 case LDB_ERR_SASL_BIND_IN_PROGRESS
:
1398 return "SASL bind in progress";
1399 case LDB_ERR_NO_SUCH_ATTRIBUTE
:
1400 return "No such attribute";
1401 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE
:
1402 return "Undefined attribute type";
1403 case LDB_ERR_INAPPROPRIATE_MATCHING
:
1404 return "Inappropriate matching";
1405 case LDB_ERR_CONSTRAINT_VIOLATION
:
1406 return "Constraint violation";
1407 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
:
1408 return "Attribute or value exists";
1409 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
:
1410 return "Invalid attribute syntax";
1412 case LDB_ERR_NO_SUCH_OBJECT
:
1413 return "No such object";
1414 case LDB_ERR_ALIAS_PROBLEM
:
1415 return "Alias problem";
1416 case LDB_ERR_INVALID_DN_SYNTAX
:
1417 return "Invalid DN syntax";
1419 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM
:
1420 return "Alias dereferencing problem";
1422 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION
:
1423 return "Inappropriate authentication";
1424 case LDB_ERR_INVALID_CREDENTIALS
:
1425 return "Invalid credentials";
1426 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
:
1427 return "insufficient access rights";
1430 case LDB_ERR_UNAVAILABLE
:
1431 return "Unavailable";
1432 case LDB_ERR_UNWILLING_TO_PERFORM
:
1433 return "Unwilling to perform";
1434 case LDB_ERR_LOOP_DETECT
:
1435 return "Loop detect";
1437 case LDB_ERR_NAMING_VIOLATION
:
1438 return "Naming violation";
1439 case LDB_ERR_OBJECT_CLASS_VIOLATION
:
1440 return "Object class violation";
1441 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF
:
1442 return "Not allowed on non-leaf";
1443 case LDB_ERR_NOT_ALLOWED_ON_RDN
:
1444 return "Not allowed on RDN";
1445 case LDB_ERR_ENTRY_ALREADY_EXISTS
:
1446 return "Entry already exists";
1447 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED
:
1448 return "Object class mods prohibited";
1449 /* 70 RESERVED FOR CLDAP */
1450 case LDB_ERR_AFFECTS_MULTIPLE_DSAS
:
1451 return "Affects multiple DSAs";
1457 return "Unknown error";
1461 set backend specific opaque parameters
1463 int ldb_set_opaque(struct ldb_context
*ldb
, const char *name
, void *value
)
1465 struct ldb_opaque
*o
;
1467 /* allow updating an existing value */
1468 for (o
=ldb
->opaque
;o
;o
=o
->next
) {
1469 if (strcmp(o
->name
, name
) == 0) {
1475 o
= talloc(ldb
, struct ldb_opaque
);
1478 return LDB_ERR_OTHER
;
1480 o
->next
= ldb
->opaque
;
1488 get a previously set opaque value
1490 void *ldb_get_opaque(struct ldb_context
*ldb
, const char *name
)
1492 struct ldb_opaque
*o
;
1493 for (o
=ldb
->opaque
;o
;o
=o
->next
) {
1494 if (strcmp(o
->name
, name
) == 0) {
1501 int ldb_global_init(void)
1503 /* Provided for compatibility with some older versions of ldb */
1507 /* return the ldb flags */
1508 unsigned int ldb_get_flags(struct ldb_context
*ldb
)