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] on %s",
45 ldb
, (const char *)ldb_get_opaque(ldb
, "ldb_url"));
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
;
629 static void ldb_trace_request(struct ldb_context
*ldb
, struct ldb_request
*req
)
631 TALLOC_CTX
*tmp_ctx
= talloc_new(req
);
634 switch (req
->operation
) {
636 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "ldb_trace_request: SEARCH");
637 ldb_debug(ldb
, LDB_DEBUG_TRACE
, " dn: %s",
638 ldb_dn_get_linearized(req
->op
.search
.base
));
639 ldb_debug(ldb
, LDB_DEBUG_TRACE
, " scope: %s",
640 req
->op
.search
.scope
==LDB_SCOPE_BASE
?"base":
641 req
->op
.search
.scope
==LDB_SCOPE_ONELEVEL
?"one":
642 req
->op
.search
.scope
==LDB_SCOPE_SUBTREE
?"sub":"UNKNOWN");
643 ldb_debug(ldb
, LDB_DEBUG_TRACE
, " expr: %s",
644 ldb_filter_from_tree(tmp_ctx
, req
->op
.search
.tree
));
645 if (req
->op
.search
.attrs
== NULL
) {
646 ldb_debug(ldb
, LDB_DEBUG_TRACE
, " attr: <ALL>");
648 for (i
=0; req
->op
.search
.attrs
[i
]; i
++) {
649 ldb_debug(ldb
, LDB_DEBUG_TRACE
, " attr: %s", req
->op
.search
.attrs
[i
]);
654 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "ldb_trace_request: DELETE");
655 ldb_debug(ldb
, LDB_DEBUG_TRACE
, " dn: %s",
656 ldb_dn_get_linearized(req
->op
.del
.dn
));
659 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "ldb_trace_request: RENAME");
660 ldb_debug(ldb
, LDB_DEBUG_TRACE
, " olddn: %s",
661 ldb_dn_get_linearized(req
->op
.rename
.olddn
));
662 ldb_debug(ldb
, LDB_DEBUG_TRACE
, " newdn: %s",
663 ldb_dn_get_linearized(req
->op
.rename
.newdn
));
666 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "ldb_trace_request: EXTENDED");
667 ldb_debug(ldb
, LDB_DEBUG_TRACE
, " oid: %s", req
->op
.extended
.oid
);
668 ldb_debug(ldb
, LDB_DEBUG_TRACE
, " data: %s", req
->op
.extended
.data
?"yes":"no");
671 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "ldb_trace_request: ADD");
672 ldb_debug(req
->handle
->ldb
, LDB_DEBUG_TRACE
, "%s",
673 ldb_ldif_message_string(req
->handle
->ldb
, tmp_ctx
,
674 LDB_CHANGETYPE_ADD
, req
->op
.add
.message
));
677 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "ldb_trace_request: MODIFY");
678 ldb_debug(req
->handle
->ldb
, LDB_DEBUG_TRACE
, "%s",
679 ldb_ldif_message_string(req
->handle
->ldb
, tmp_ctx
,
680 LDB_CHANGETYPE_ADD
, req
->op
.mod
.message
));
682 case LDB_REQ_REGISTER_CONTROL
:
683 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "ldb_trace_request: REGISTER_CONTROL");
684 ldb_debug(req
->handle
->ldb
, LDB_DEBUG_TRACE
, "%s",
685 req
->op
.reg_control
.oid
);
687 case LDB_REQ_REGISTER_PARTITION
:
688 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "ldb_trace_request: REGISTER_PARTITION");
689 ldb_debug(req
->handle
->ldb
, LDB_DEBUG_TRACE
, "%s",
690 ldb_dn_get_linearized(req
->op
.reg_partition
.dn
));
693 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "ldb_trace_request: UNKNOWN(%u)",
698 if (req
->controls
== NULL
) {
699 ldb_debug(ldb
, LDB_DEBUG_TRACE
, " control: <NONE>");
701 for (i
=0; req
->controls
&& req
->controls
[i
]; i
++) {
702 ldb_debug(ldb
, LDB_DEBUG_TRACE
, " control: %s crit:%u data:%s",
703 req
->controls
[i
]->oid
,
704 req
->controls
[i
]->critical
,
705 req
->controls
[i
]->data
?"yes":"no");
709 talloc_free(tmp_ctx
);
715 NOTE: the request must be a talloc context.
716 returns LDB_ERR_* on errors.
718 int ldb_request(struct ldb_context
*ldb
, struct ldb_request
*req
)
720 struct ldb_module
*module
;
723 if (req
->callback
== NULL
) {
724 ldb_set_errstring(ldb
, "Requests MUST define callbacks");
725 return LDB_ERR_UNWILLING_TO_PERFORM
;
728 ldb_reset_err_string(ldb
);
730 if (ldb
->flags
& LDB_FLG_ENABLE_TRACING
) {
731 ldb_trace_request(ldb
, req
);
734 /* call the first module in the chain */
735 switch (req
->operation
) {
737 FIRST_OP(ldb
, search
);
738 ret
= module
->ops
->search(module
, req
);
742 ret
= module
->ops
->add(module
, req
);
745 FIRST_OP(ldb
, modify
);
746 ret
= module
->ops
->modify(module
, req
);
750 ret
= module
->ops
->del(module
, req
);
753 FIRST_OP(ldb
, rename
);
754 ret
= module
->ops
->rename(module
, req
);
757 FIRST_OP(ldb
, extended
);
758 ret
= module
->ops
->extended(module
, req
);
761 FIRST_OP(ldb
, request
);
762 ret
= module
->ops
->request(module
, req
);
769 int ldb_request_done(struct ldb_request
*req
, int status
)
771 req
->handle
->state
= LDB_ASYNC_DONE
;
772 req
->handle
->status
= status
;
777 search the database given a LDAP-like search expression
779 returns an LDB error code
781 Use talloc_free to free the ldb_message returned in 'res', if successful
784 int ldb_search_default_callback(struct ldb_request
*req
,
785 struct ldb_reply
*ares
)
787 struct ldb_result
*res
;
790 res
= talloc_get_type(req
->context
, struct ldb_result
);
793 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
795 if (ares
->error
!= LDB_SUCCESS
) {
796 return ldb_request_done(req
, ares
->error
);
799 switch (ares
->type
) {
800 case LDB_REPLY_ENTRY
:
801 res
->msgs
= talloc_realloc(res
, res
->msgs
,
802 struct ldb_message
*, res
->count
+ 2);
804 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
807 res
->msgs
[res
->count
+ 1] = NULL
;
809 res
->msgs
[res
->count
] = talloc_move(res
->msgs
, &ares
->message
);
813 case LDB_REPLY_REFERRAL
:
815 for (n
= 0; res
->refs
[n
]; n
++) /*noop*/ ;
820 res
->refs
= talloc_realloc(res
, res
->refs
, char *, n
+ 2);
822 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
825 res
->refs
[n
] = talloc_move(res
->refs
, &ares
->referral
);
826 res
->refs
[n
+ 1] = NULL
;
830 /* TODO: we should really support controls on entries
831 * and referrals too! */
832 res
->controls
= talloc_move(res
, &ares
->controls
);
834 /* this is the last message, and means the request is done */
835 /* we have to signal and eventual ldb_wait() waiting that the
836 * async request operation was completed */
838 return ldb_request_done(req
, LDB_SUCCESS
);
846 int ldb_op_default_callback(struct ldb_request
*req
, struct ldb_reply
*ares
)
851 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
854 if (ares
->error
!= LDB_SUCCESS
) {
857 return ldb_request_done(req
, ret
);
860 if (ares
->type
!= LDB_REPLY_DONE
) {
862 ldb_set_errstring(req
->handle
->ldb
, "Invalid reply type!");
863 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
867 return ldb_request_done(req
, LDB_SUCCESS
);
870 int ldb_build_search_req_ex(struct ldb_request
**ret_req
,
871 struct ldb_context
*ldb
,
874 enum ldb_scope scope
,
875 struct ldb_parse_tree
*tree
,
876 const char * const *attrs
,
877 struct ldb_control
**controls
,
879 ldb_request_callback_t callback
,
880 struct ldb_request
*parent
)
882 struct ldb_request
*req
;
886 req
= talloc(mem_ctx
, struct ldb_request
);
889 return LDB_ERR_OPERATIONS_ERROR
;
892 req
->operation
= LDB_SEARCH
;
894 req
->op
.search
.base
= ldb_dn_new(req
, ldb
, NULL
);
896 req
->op
.search
.base
= base
;
898 req
->op
.search
.scope
= scope
;
900 req
->op
.search
.tree
= tree
;
901 if (req
->op
.search
.tree
== NULL
) {
902 ldb_set_errstring(ldb
, "'tree' can't be NULL");
904 return LDB_ERR_OPERATIONS_ERROR
;
907 req
->op
.search
.attrs
= attrs
;
908 req
->controls
= controls
;
909 req
->context
= context
;
910 req
->callback
= callback
;
912 ldb_set_timeout_from_prev_req(ldb
, parent
, req
);
914 req
->handle
= ldb_handle_new(req
, ldb
);
915 if (req
->handle
== NULL
) {
917 return LDB_ERR_OPERATIONS_ERROR
;
924 int ldb_build_search_req(struct ldb_request
**ret_req
,
925 struct ldb_context
*ldb
,
928 enum ldb_scope scope
,
929 const char *expression
,
930 const char * const *attrs
,
931 struct ldb_control
**controls
,
933 ldb_request_callback_t callback
,
934 struct ldb_request
*parent
)
936 struct ldb_parse_tree
*tree
;
939 tree
= ldb_parse_tree(mem_ctx
, expression
);
941 ldb_set_errstring(ldb
, "Unable to parse search expression");
942 return LDB_ERR_OPERATIONS_ERROR
;
945 ret
= ldb_build_search_req_ex(ret_req
, ldb
, mem_ctx
, base
,
946 scope
, tree
, attrs
, controls
,
947 context
, callback
, parent
);
948 if (ret
== LDB_SUCCESS
) {
949 talloc_steal(*ret_req
, tree
);
954 int ldb_build_add_req(struct ldb_request
**ret_req
,
955 struct ldb_context
*ldb
,
957 const struct ldb_message
*message
,
958 struct ldb_control
**controls
,
960 ldb_request_callback_t callback
,
961 struct ldb_request
*parent
)
963 struct ldb_request
*req
;
967 req
= talloc(mem_ctx
, struct ldb_request
);
969 ldb_set_errstring(ldb
, "Out of Memory");
970 return LDB_ERR_OPERATIONS_ERROR
;
973 req
->operation
= LDB_ADD
;
974 req
->op
.add
.message
= message
;
975 req
->controls
= controls
;
976 req
->context
= context
;
977 req
->callback
= callback
;
979 ldb_set_timeout_from_prev_req(ldb
, parent
, req
);
981 req
->handle
= ldb_handle_new(req
, ldb
);
982 if (req
->handle
== NULL
) {
984 return LDB_ERR_OPERATIONS_ERROR
;
992 int ldb_build_mod_req(struct ldb_request
**ret_req
,
993 struct ldb_context
*ldb
,
995 const struct ldb_message
*message
,
996 struct ldb_control
**controls
,
998 ldb_request_callback_t callback
,
999 struct ldb_request
*parent
)
1001 struct ldb_request
*req
;
1005 req
= talloc(mem_ctx
, struct ldb_request
);
1007 ldb_set_errstring(ldb
, "Out of Memory");
1008 return LDB_ERR_OPERATIONS_ERROR
;
1011 req
->operation
= LDB_MODIFY
;
1012 req
->op
.mod
.message
= message
;
1013 req
->controls
= controls
;
1014 req
->context
= context
;
1015 req
->callback
= callback
;
1017 ldb_set_timeout_from_prev_req(ldb
, parent
, req
);
1019 req
->handle
= ldb_handle_new(req
, ldb
);
1020 if (req
->handle
== NULL
) {
1022 return LDB_ERR_OPERATIONS_ERROR
;
1030 int ldb_build_del_req(struct ldb_request
**ret_req
,
1031 struct ldb_context
*ldb
,
1034 struct ldb_control
**controls
,
1036 ldb_request_callback_t callback
,
1037 struct ldb_request
*parent
)
1039 struct ldb_request
*req
;
1043 req
= talloc(mem_ctx
, struct ldb_request
);
1045 ldb_set_errstring(ldb
, "Out of Memory");
1046 return LDB_ERR_OPERATIONS_ERROR
;
1049 req
->operation
= LDB_DELETE
;
1050 req
->op
.del
.dn
= dn
;
1051 req
->controls
= controls
;
1052 req
->context
= context
;
1053 req
->callback
= callback
;
1055 ldb_set_timeout_from_prev_req(ldb
, parent
, req
);
1057 req
->handle
= ldb_handle_new(req
, ldb
);
1058 if (req
->handle
== NULL
) {
1060 return LDB_ERR_OPERATIONS_ERROR
;
1068 int ldb_build_rename_req(struct ldb_request
**ret_req
,
1069 struct ldb_context
*ldb
,
1071 struct ldb_dn
*olddn
,
1072 struct ldb_dn
*newdn
,
1073 struct ldb_control
**controls
,
1075 ldb_request_callback_t callback
,
1076 struct ldb_request
*parent
)
1078 struct ldb_request
*req
;
1082 req
= talloc(mem_ctx
, struct ldb_request
);
1084 ldb_set_errstring(ldb
, "Out of Memory");
1085 return LDB_ERR_OPERATIONS_ERROR
;
1088 req
->operation
= LDB_RENAME
;
1089 req
->op
.rename
.olddn
= olddn
;
1090 req
->op
.rename
.newdn
= newdn
;
1091 req
->controls
= controls
;
1092 req
->context
= context
;
1093 req
->callback
= callback
;
1095 ldb_set_timeout_from_prev_req(ldb
, parent
, req
);
1097 req
->handle
= ldb_handle_new(req
, ldb
);
1098 if (req
->handle
== NULL
) {
1100 return LDB_ERR_OPERATIONS_ERROR
;
1108 int ldb_extended_default_callback(struct ldb_request
*req
,
1109 struct ldb_reply
*ares
)
1111 struct ldb_result
*res
;
1113 res
= talloc_get_type(req
->context
, struct ldb_result
);
1116 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
1118 if (ares
->error
!= LDB_SUCCESS
) {
1119 return ldb_request_done(req
, ares
->error
);
1122 if (ares
->type
== LDB_REPLY_DONE
) {
1124 /* TODO: we should really support controls on entries and referrals too! */
1125 res
->extended
= talloc_move(res
, &ares
->response
);
1126 res
->controls
= talloc_move(res
, &ares
->controls
);
1129 return ldb_request_done(req
, LDB_SUCCESS
);
1133 ldb_set_errstring(req
->handle
->ldb
, "Invalid reply type!");
1134 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
1137 int ldb_build_extended_req(struct ldb_request
**ret_req
,
1138 struct ldb_context
*ldb
,
1142 struct ldb_control
**controls
,
1144 ldb_request_callback_t callback
,
1145 struct ldb_request
*parent
)
1147 struct ldb_request
*req
;
1151 req
= talloc(mem_ctx
, struct ldb_request
);
1153 ldb_set_errstring(ldb
, "Out of Memory");
1154 return LDB_ERR_OPERATIONS_ERROR
;
1157 req
->operation
= LDB_EXTENDED
;
1158 req
->op
.extended
.oid
= oid
;
1159 req
->op
.extended
.data
= data
;
1160 req
->controls
= controls
;
1161 req
->context
= context
;
1162 req
->callback
= callback
;
1164 ldb_set_timeout_from_prev_req(ldb
, parent
, req
);
1166 req
->handle
= ldb_handle_new(req
, ldb
);
1167 if (req
->handle
== NULL
) {
1169 return LDB_ERR_OPERATIONS_ERROR
;
1177 int ldb_extended(struct ldb_context
*ldb
,
1180 struct ldb_result
**_res
)
1182 struct ldb_request
*req
;
1184 struct ldb_result
*res
;
1188 res
= talloc_zero(ldb
, struct ldb_result
);
1190 return LDB_ERR_OPERATIONS_ERROR
;
1193 ret
= ldb_build_extended_req(&req
, ldb
, ldb
,
1195 res
, ldb_extended_default_callback
,
1197 if (ret
!= LDB_SUCCESS
) goto done
;
1199 ldb_set_timeout(ldb
, req
, 0); /* use default timeout */
1201 ret
= ldb_request(ldb
, req
);
1203 if (ret
== LDB_SUCCESS
) {
1204 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
1210 if (ret
!= LDB_SUCCESS
) {
1219 note that ldb_search() will automatically replace a NULL 'base' value
1220 with the defaultNamingContext from the rootDSE if available.
1222 int ldb_search(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
1223 struct ldb_result
**result
, struct ldb_dn
*base
,
1224 enum ldb_scope scope
, const char * const *attrs
,
1225 const char *exp_fmt
, ...)
1227 struct ldb_request
*req
;
1228 struct ldb_result
*res
;
1237 res
= talloc_zero(mem_ctx
, struct ldb_result
);
1239 return LDB_ERR_OPERATIONS_ERROR
;
1243 va_start(ap
, exp_fmt
);
1244 expression
= talloc_vasprintf(mem_ctx
, exp_fmt
, ap
);
1249 return LDB_ERR_OPERATIONS_ERROR
;
1253 ret
= ldb_build_search_req(&req
, ldb
, mem_ctx
,
1254 base
?base
:ldb_get_default_basedn(ldb
),
1260 ldb_search_default_callback
,
1263 if (ret
!= LDB_SUCCESS
) goto done
;
1265 ret
= ldb_request(ldb
, req
);
1267 if (ret
== LDB_SUCCESS
) {
1268 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
1272 if (ret
!= LDB_SUCCESS
) {
1277 talloc_free(expression
);
1285 add a record to the database. Will fail if a record with the given class
1286 and key already exists
1288 int ldb_add(struct ldb_context
*ldb
,
1289 const struct ldb_message
*message
)
1291 struct ldb_request
*req
;
1294 ret
= ldb_msg_sanity_check(ldb
, message
);
1295 if (ret
!= LDB_SUCCESS
) {
1299 ret
= ldb_build_add_req(&req
, ldb
, ldb
,
1303 ldb_op_default_callback
,
1306 if (ret
!= LDB_SUCCESS
) return ret
;
1308 /* do request and autostart a transaction */
1309 ret
= ldb_autotransaction_request(ldb
, req
);
1316 modify the specified attributes of a record
1318 int ldb_modify(struct ldb_context
*ldb
,
1319 const struct ldb_message
*message
)
1321 struct ldb_request
*req
;
1324 ret
= ldb_msg_sanity_check(ldb
, message
);
1325 if (ret
!= LDB_SUCCESS
) {
1329 ret
= ldb_build_mod_req(&req
, ldb
, ldb
,
1333 ldb_op_default_callback
,
1336 if (ret
!= LDB_SUCCESS
) return ret
;
1338 /* do request and autostart a transaction */
1339 ret
= ldb_autotransaction_request(ldb
, req
);
1347 delete a record from the database
1349 int ldb_delete(struct ldb_context
*ldb
, struct ldb_dn
*dn
)
1351 struct ldb_request
*req
;
1354 ret
= ldb_build_del_req(&req
, ldb
, ldb
,
1358 ldb_op_default_callback
,
1361 if (ret
!= LDB_SUCCESS
) return ret
;
1363 /* do request and autostart a transaction */
1364 ret
= ldb_autotransaction_request(ldb
, req
);
1371 rename a record in the database
1373 int ldb_rename(struct ldb_context
*ldb
,
1374 struct ldb_dn
*olddn
, struct ldb_dn
*newdn
)
1376 struct ldb_request
*req
;
1379 ret
= ldb_build_rename_req(&req
, ldb
, ldb
,
1384 ldb_op_default_callback
,
1387 if (ret
!= LDB_SUCCESS
) return ret
;
1389 /* do request and autostart a transaction */
1390 ret
= ldb_autotransaction_request(ldb
, req
);
1398 return the global sequence number
1400 int ldb_sequence_number(struct ldb_context
*ldb
,
1401 enum ldb_sequence_type type
, uint64_t *seq_num
)
1403 struct ldb_seqnum_request
*seq
;
1404 struct ldb_seqnum_result
*seqr
;
1405 struct ldb_result
*res
;
1406 TALLOC_CTX
*tmp_ctx
;
1411 tmp_ctx
= talloc_zero(ldb
, struct ldb_request
);
1412 if (tmp_ctx
== NULL
) {
1413 ldb_set_errstring(ldb
, "Out of Memory");
1414 return LDB_ERR_OPERATIONS_ERROR
;
1416 seq
= talloc_zero(tmp_ctx
, struct ldb_seqnum_request
);
1418 ldb_set_errstring(ldb
, "Out of Memory");
1419 ret
= LDB_ERR_OPERATIONS_ERROR
;
1424 ret
= ldb_extended(ldb
, LDB_EXTENDED_SEQUENCE_NUMBER
, seq
, &res
);
1425 if (ret
!= LDB_SUCCESS
) {
1428 talloc_steal(tmp_ctx
, res
);
1430 if (strcmp(LDB_EXTENDED_SEQUENCE_NUMBER
, res
->extended
->oid
) != 0) {
1431 ldb_set_errstring(ldb
, "Invalid OID in reply");
1432 ret
= LDB_ERR_OPERATIONS_ERROR
;
1435 seqr
= talloc_get_type(res
->extended
->data
,
1436 struct ldb_seqnum_result
);
1437 *seq_num
= seqr
->seq_num
;
1440 talloc_free(tmp_ctx
);
1445 return extended error information
1447 const char *ldb_errstring(struct ldb_context
*ldb
)
1449 if (ldb
->err_string
) {
1450 return ldb
->err_string
;
1457 return a string explaining what a ldb error constant meancs
1459 const char *ldb_strerror(int ldb_err
)
1464 case LDB_ERR_OPERATIONS_ERROR
:
1465 return "Operations error";
1466 case LDB_ERR_PROTOCOL_ERROR
:
1467 return "Protocol error";
1468 case LDB_ERR_TIME_LIMIT_EXCEEDED
:
1469 return "Time limit exceeded";
1470 case LDB_ERR_SIZE_LIMIT_EXCEEDED
:
1471 return "Size limit exceeded";
1472 case LDB_ERR_COMPARE_FALSE
:
1473 return "Compare false";
1474 case LDB_ERR_COMPARE_TRUE
:
1475 return "Compare true";
1476 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED
:
1477 return "Auth method not supported";
1478 case LDB_ERR_STRONG_AUTH_REQUIRED
:
1479 return "Strong auth required";
1481 case LDB_ERR_REFERRAL
:
1482 return "Referral error";
1483 case LDB_ERR_ADMIN_LIMIT_EXCEEDED
:
1484 return "Admin limit exceeded";
1485 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION
:
1486 return "Unsupported critical extension";
1487 case LDB_ERR_CONFIDENTIALITY_REQUIRED
:
1488 return "Confidentiality required";
1489 case LDB_ERR_SASL_BIND_IN_PROGRESS
:
1490 return "SASL bind in progress";
1491 case LDB_ERR_NO_SUCH_ATTRIBUTE
:
1492 return "No such attribute";
1493 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE
:
1494 return "Undefined attribute type";
1495 case LDB_ERR_INAPPROPRIATE_MATCHING
:
1496 return "Inappropriate matching";
1497 case LDB_ERR_CONSTRAINT_VIOLATION
:
1498 return "Constraint violation";
1499 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
:
1500 return "Attribute or value exists";
1501 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
:
1502 return "Invalid attribute syntax";
1504 case LDB_ERR_NO_SUCH_OBJECT
:
1505 return "No such object";
1506 case LDB_ERR_ALIAS_PROBLEM
:
1507 return "Alias problem";
1508 case LDB_ERR_INVALID_DN_SYNTAX
:
1509 return "Invalid DN syntax";
1511 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM
:
1512 return "Alias dereferencing problem";
1514 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION
:
1515 return "Inappropriate authentication";
1516 case LDB_ERR_INVALID_CREDENTIALS
:
1517 return "Invalid credentials";
1518 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
:
1519 return "insufficient access rights";
1522 case LDB_ERR_UNAVAILABLE
:
1523 return "Unavailable";
1524 case LDB_ERR_UNWILLING_TO_PERFORM
:
1525 return "Unwilling to perform";
1526 case LDB_ERR_LOOP_DETECT
:
1527 return "Loop detect";
1529 case LDB_ERR_NAMING_VIOLATION
:
1530 return "Naming violation";
1531 case LDB_ERR_OBJECT_CLASS_VIOLATION
:
1532 return "Object class violation";
1533 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF
:
1534 return "Not allowed on non-leaf";
1535 case LDB_ERR_NOT_ALLOWED_ON_RDN
:
1536 return "Not allowed on RDN";
1537 case LDB_ERR_ENTRY_ALREADY_EXISTS
:
1538 return "Entry already exists";
1539 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED
:
1540 return "Object class mods prohibited";
1541 /* 70 RESERVED FOR CLDAP */
1542 case LDB_ERR_AFFECTS_MULTIPLE_DSAS
:
1543 return "Affects multiple DSAs";
1549 return "Unknown error";
1553 set backend specific opaque parameters
1555 int ldb_set_opaque(struct ldb_context
*ldb
, const char *name
, void *value
)
1557 struct ldb_opaque
*o
;
1559 /* allow updating an existing value */
1560 for (o
=ldb
->opaque
;o
;o
=o
->next
) {
1561 if (strcmp(o
->name
, name
) == 0) {
1567 o
= talloc(ldb
, struct ldb_opaque
);
1570 return LDB_ERR_OTHER
;
1572 o
->next
= ldb
->opaque
;
1580 get a previously set opaque value
1582 void *ldb_get_opaque(struct ldb_context
*ldb
, const char *name
)
1584 struct ldb_opaque
*o
;
1585 for (o
=ldb
->opaque
;o
;o
=o
->next
) {
1586 if (strcmp(o
->name
, name
) == 0) {
1593 int ldb_global_init(void)
1595 /* Provided for compatibility with some older versions of ldb */
1599 /* return the ldb flags */
1600 unsigned int ldb_get_flags(struct ldb_context
*ldb
)
1605 /* set the ldb flags */
1606 void ldb_set_flags(struct ldb_context
*ldb
, unsigned flags
)