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);
105 if (ret != LDB_SUCCESS) {
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", 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);
260 if (ldb->flags & LDB_FLG_ENABLE_TRACING) {
261 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_set_errstring: %s", ldb->err_string);
265 void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
268 char *old_string = NULL;
270 if (ldb->err_string) {
271 old_string = ldb->err_string;
274 va_start(ap, format);
275 ldb->err_string = talloc_vasprintf(ldb, format, ap);
277 talloc_free(old_string);
280 void ldb_reset_err_string(struct ldb_context *ldb)
282 if (ldb->err_string) {
283 talloc_free(ldb->err_string);
284 ldb->err_string = NULL;
291 set an ldb error based on file:line
293 int ldb_error_at(struct ldb_context *ldb, int ecode,
294 const char *reason, const char *file, int line)
296 if (reason == NULL) {
297 reason = ldb_strerror(ecode);
299 ldb_asprintf_errstring(ldb, "%s at %s:%d", reason, file, line);
304 #define FIRST_OP_NOERR(ldb, op) do { \
305 module = ldb->modules; \
306 while (module && module->ops->op == NULL) module = module->next; \
307 if ((ldb->flags & LDB_FLG_ENABLE_TRACING) && module) { \
308 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: (%s)->" #op, \
309 module->ops->name); \
313 #define FIRST_OP(ldb, op) do { \
314 FIRST_OP_NOERR(ldb, op); \
315 if (module == NULL) { \
316 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
317 return LDB_ERR_OPERATIONS_ERROR; \
325 int ldb_transaction_start(struct ldb_context *ldb)
327 struct ldb_module *module;
330 ldb_debug(ldb, LDB_DEBUG_TRACE,
331 "start ldb transaction (nesting: %d)",
332 ldb->transaction_active);
334 /* explicit transaction active, count nested requests */
335 if (ldb->transaction_active) {
336 ldb->transaction_active++;
340 /* start a new transaction */
341 ldb->transaction_active++;
342 ldb->prepare_commit_done = false;
344 FIRST_OP(ldb, start_transaction);
346 ldb_reset_err_string(ldb);
348 status = module->ops->start_transaction(module);
349 if (status != LDB_SUCCESS) {
350 if (ldb->err_string == NULL) {
351 /* no error string was setup by the backend */
352 ldb_asprintf_errstring(ldb,
353 "ldb transaction start: %s (%d)",
354 ldb_strerror(status),
358 if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
359 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "start ldb transaction error: %s",
360 ldb_errstring(module->ldb));
366 prepare for transaction commit (first phase of two phase commit)
368 int ldb_transaction_prepare_commit(struct ldb_context *ldb)
370 struct ldb_module *module;
373 if (ldb->prepare_commit_done) {
377 /* commit only when all nested transactions are complete */
378 if (ldb->transaction_active > 1) {
382 ldb->prepare_commit_done = true;
384 if (ldb->transaction_active < 0) {
385 ldb_debug(ldb, LDB_DEBUG_FATAL,
386 "prepare commit called but no ldb transactions are active!");
387 ldb->transaction_active = 0;
388 return LDB_ERR_OPERATIONS_ERROR;
391 /* call prepare transaction if available */
392 FIRST_OP_NOERR(ldb, prepare_commit);
393 if (module == NULL) {
397 status = module->ops->prepare_commit(module);
398 if (status != LDB_SUCCESS) {
399 /* if a module fails the prepare then we need
400 to call the end transaction for everyone */
401 FIRST_OP(ldb, del_transaction);
402 module->ops->del_transaction(module);
403 if (ldb->err_string == NULL) {
404 /* no error string was setup by the backend */
405 ldb_asprintf_errstring(ldb,
406 "ldb transaction prepare commit: %s (%d)",
407 ldb_strerror(status),
410 if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
411 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "prepare commit transaction error: %s",
412 ldb_errstring(module->ldb));
423 int ldb_transaction_commit(struct ldb_context *ldb)
425 struct ldb_module *module;
428 status = ldb_transaction_prepare_commit(ldb);
429 if (status != LDB_SUCCESS) {
433 ldb->transaction_active--;
435 ldb_debug(ldb, LDB_DEBUG_TRACE,
436 "commit ldb transaction (nesting: %d)",
437 ldb->transaction_active);
439 /* commit only when all nested transactions are complete */
440 if (ldb->transaction_active > 0) {
444 if (ldb->transaction_active < 0) {
445 ldb_debug(ldb, LDB_DEBUG_FATAL,
446 "commit called but no ldb transactions are active!");
447 ldb->transaction_active = 0;
448 return LDB_ERR_OPERATIONS_ERROR;
451 ldb_reset_err_string(ldb);
453 FIRST_OP(ldb, end_transaction);
454 status = module->ops->end_transaction(module);
455 if (status != LDB_SUCCESS) {
456 if (ldb->err_string == NULL) {
457 /* no error string was setup by the backend */
458 ldb_asprintf_errstring(ldb,
459 "ldb transaction commit: %s (%d)",
460 ldb_strerror(status),
463 if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
464 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "commit ldb transaction error: %s",
465 ldb_errstring(module->ldb));
467 /* cancel the transaction */
468 FIRST_OP(ldb, del_transaction);
469 module->ops->del_transaction(module);
478 int ldb_transaction_cancel(struct ldb_context *ldb)
480 struct ldb_module *module;
483 ldb->transaction_active--;
485 ldb_debug(ldb, LDB_DEBUG_TRACE,
486 "cancel ldb transaction (nesting: %d)",
487 ldb->transaction_active);
489 /* really cancel only if all nested transactions are complete */
490 if (ldb->transaction_active > 0) {
494 if (ldb->transaction_active < 0) {
495 ldb_debug(ldb, LDB_DEBUG_FATAL,
496 "cancel called but no ldb transactions are active!");
497 ldb->transaction_active = 0;
498 return LDB_ERR_OPERATIONS_ERROR;
501 FIRST_OP(ldb, del_transaction);
503 status = module->ops->del_transaction(module);
504 if (status != LDB_SUCCESS) {
505 if (ldb->err_string == NULL) {
506 /* no error string was setup by the backend */
507 ldb_asprintf_errstring(ldb,
508 "ldb transaction cancel: %s (%d)",
509 ldb_strerror(status),
512 if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
513 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "cancel ldb transaction error: %s",
514 ldb_errstring(module->ldb));
521 cancel a transaction with no error if no transaction is pending
522 used when we fork() to clear any parent transactions
524 int ldb_transaction_cancel_noerr(struct ldb_context *ldb)
526 if (ldb->transaction_active > 0) {
527 return ldb_transaction_cancel(ldb);
533 /* autostarts a transacion if none active */
534 static int ldb_autotransaction_request(struct ldb_context *ldb,
535 struct ldb_request *req)
539 ret = ldb_transaction_start(ldb);
540 if (ret != LDB_SUCCESS) {
544 ret = ldb_request(ldb, req);
545 if (ret == LDB_SUCCESS) {
546 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
549 if (ret == LDB_SUCCESS) {
550 return ldb_transaction_commit(ldb);
552 ldb_transaction_cancel(ldb);
554 if (ldb->err_string == NULL) {
555 /* no error string was setup by the backend */
556 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
562 int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
564 struct tevent_context *ev;
568 return LDB_ERR_UNAVAILABLE;
571 if (handle->state == LDB_ASYNC_DONE) {
572 return handle->status;
575 ev = ldb_get_event_context(handle->ldb);
577 return LDB_ERR_OPERATIONS_ERROR;
582 ret = tevent_loop_once(ev);
584 return LDB_ERR_OPERATIONS_ERROR;
586 if (handle->state == LDB_ASYNC_DONE ||
587 handle->status != LDB_SUCCESS) {
588 return handle->status;
593 while (handle->state != LDB_ASYNC_DONE) {
594 ret = tevent_loop_once(ev);
596 return LDB_ERR_OPERATIONS_ERROR;
598 if (handle->status != LDB_SUCCESS) {
599 return handle->status;
602 return handle->status;
608 /* set the specified timeout or, if timeout is 0 set the default timeout */
609 int ldb_set_timeout(struct ldb_context *ldb,
610 struct ldb_request *req,
613 if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
616 req->timeout = timeout;
618 req->timeout = ldb->default_timeout;
620 req->starttime = time(NULL);
625 /* calculates the new timeout based on the previous starttime and timeout */
626 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb,
627 struct ldb_request *oldreq,
628 struct ldb_request *newreq)
630 if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
632 if (oldreq == NULL) {
633 return ldb_set_timeout(ldb, newreq, 0);
636 newreq->starttime = oldreq->starttime;
637 newreq->timeout = oldreq->timeout;
644 set the permissions for new files to be passed to open() in
645 backends that use local files
647 void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
649 ldb->create_perms = perms;
652 unsigned int ldb_get_create_perms(struct ldb_context *ldb)
654 return ldb->create_perms;
657 void ldb_set_event_context(struct ldb_context *ldb, struct tevent_context *ev)
662 struct tevent_context * ldb_get_event_context(struct ldb_context *ldb)
667 void ldb_request_set_state(struct ldb_request *req, int state)
669 req->handle->state = state;
672 int ldb_request_get_status(struct ldb_request *req)
674 return req->handle->status;
681 static void ldb_trace_request(struct ldb_context *ldb, struct ldb_request *req)
683 TALLOC_CTX *tmp_ctx = talloc_new(req);
686 switch (req->operation) {
688 ldb_debug_add(ldb, "ldb_trace_request: SEARCH\n");
689 ldb_debug_add(ldb, " dn: %s\n",
690 ldb_dn_is_null(req->op.search.base)?"<rootDSE>":
691 ldb_dn_get_linearized(req->op.search.base));
692 ldb_debug_add(ldb, " scope: %s\n",
693 req->op.search.scope==LDB_SCOPE_BASE?"base":
694 req->op.search.scope==LDB_SCOPE_ONELEVEL?"one":
695 req->op.search.scope==LDB_SCOPE_SUBTREE?"sub":"UNKNOWN");
696 ldb_debug_add(ldb, " expr: %s\n",
697 ldb_filter_from_tree(tmp_ctx, req->op.search.tree));
698 if (req->op.search.attrs == NULL) {
699 ldb_debug_add(ldb, " attr: <ALL>\n");
701 for (i=0; req->op.search.attrs[i]; i++) {
702 ldb_debug_add(ldb, " attr: %s\n", req->op.search.attrs[i]);
707 ldb_debug_add(ldb, "ldb_trace_request: DELETE\n");
708 ldb_debug_add(ldb, " dn: %s\n",
709 ldb_dn_get_linearized(req->op.del.dn));
712 ldb_debug_add(ldb, "ldb_trace_request: RENAME\n");
713 ldb_debug_add(ldb, " olddn: %s\n",
714 ldb_dn_get_linearized(req->op.rename.olddn));
715 ldb_debug_add(ldb, " newdn: %s\n",
716 ldb_dn_get_linearized(req->op.rename.newdn));
719 ldb_debug_add(ldb, "ldb_trace_request: EXTENDED\n");
720 ldb_debug_add(ldb, " oid: %s\n", req->op.extended.oid);
721 ldb_debug_add(ldb, " data: %s\n", req->op.extended.data?"yes":"no");
724 ldb_debug_add(ldb, "ldb_trace_request: ADD\n");
725 ldb_debug_add(req->handle->ldb, "%s\n",
726 ldb_ldif_message_string(req->handle->ldb, tmp_ctx,
728 req->op.add.message));
731 ldb_debug_add(ldb, "ldb_trace_request: MODIFY\n");
732 ldb_debug_add(req->handle->ldb, "%s\n",
733 ldb_ldif_message_string(req->handle->ldb, tmp_ctx,
735 req->op.mod.message));
737 case LDB_REQ_REGISTER_CONTROL:
738 ldb_debug_add(ldb, "ldb_trace_request: REGISTER_CONTROL\n");
739 ldb_debug_add(req->handle->ldb, "%s\n",
740 req->op.reg_control.oid);
742 case LDB_REQ_REGISTER_PARTITION:
743 ldb_debug_add(ldb, "ldb_trace_request: REGISTER_PARTITION\n");
744 ldb_debug_add(req->handle->ldb, "%s\n",
745 ldb_dn_get_linearized(req->op.reg_partition.dn));
748 ldb_debug_add(ldb, "ldb_trace_request: UNKNOWN(%u)\n",
753 if (req->controls == NULL) {
754 ldb_debug_add(ldb, " control: <NONE>\n");
756 for (i=0; req->controls && req->controls[i]; i++) {
757 ldb_debug_add(ldb, " control: %s crit:%u data:%s\n",
758 req->controls[i]->oid,
759 req->controls[i]->critical,
760 req->controls[i]->data?"yes":"no");
764 ldb_debug_end(ldb, LDB_DEBUG_TRACE);
766 talloc_free(tmp_ctx);
772 NOTE: the request must be a talloc context.
773 returns LDB_ERR_* on errors.
775 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
777 struct ldb_module *module;
780 if (req->callback == NULL) {
781 ldb_set_errstring(ldb, "Requests MUST define callbacks");
782 return LDB_ERR_UNWILLING_TO_PERFORM;
785 ldb_reset_err_string(ldb);
787 if (ldb->flags & LDB_FLG_ENABLE_TRACING) {
788 ldb_trace_request(ldb, req);
791 /* call the first module in the chain */
792 switch (req->operation) {
794 FIRST_OP(ldb, search);
795 ret = module->ops->search(module, req);
798 /* we have to canonicalise here, as so many places
799 * in modules and backends assume we don't have two
800 * elements with the same name */
801 req->op.add.message = ldb_msg_canonicalize(ldb, req->op.add.message);
802 if (!req->op.add.message) {
804 return LDB_ERR_OPERATIONS_ERROR;
806 talloc_steal(req, req->op.add.message);
808 ret = module->ops->add(module, req);
811 FIRST_OP(ldb, modify);
812 ret = module->ops->modify(module, req);
816 ret = module->ops->del(module, req);
819 if (!ldb_dn_validate(req->op.rename.olddn)) {
820 ldb_asprintf_errstring(ldb, "ldb_rename: invalid olddn '%s'",
821 ldb_dn_get_linearized(req->op.rename.olddn));
822 return LDB_ERR_INVALID_DN_SYNTAX;
824 if (!ldb_dn_validate(req->op.rename.newdn)) {
825 ldb_asprintf_errstring(ldb, "ldb_rename: invalid newdn '%s'",
826 ldb_dn_get_linearized(req->op.rename.newdn));
827 return LDB_ERR_INVALID_DN_SYNTAX;
829 FIRST_OP(ldb, rename);
830 ret = module->ops->rename(module, req);
833 FIRST_OP(ldb, extended);
834 ret = module->ops->extended(module, req);
837 FIRST_OP(ldb, request);
838 ret = module->ops->request(module, req);
845 int ldb_request_done(struct ldb_request *req, int status)
847 req->handle->state = LDB_ASYNC_DONE;
848 req->handle->status = status;
853 search the database given a LDAP-like search expression
855 returns an LDB error code
857 Use talloc_free to free the ldb_message returned in 'res', if successful
860 int ldb_search_default_callback(struct ldb_request *req,
861 struct ldb_reply *ares)
863 struct ldb_result *res;
866 res = talloc_get_type(req->context, struct ldb_result);
869 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
871 if (ares->error != LDB_SUCCESS) {
872 return ldb_request_done(req, ares->error);
875 switch (ares->type) {
876 case LDB_REPLY_ENTRY:
877 res->msgs = talloc_realloc(res, res->msgs,
878 struct ldb_message *, res->count + 2);
880 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
883 res->msgs[res->count + 1] = NULL;
885 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
889 case LDB_REPLY_REFERRAL:
891 for (n = 0; res->refs[n]; n++) /*noop*/ ;
896 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
898 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
901 res->refs[n] = talloc_move(res->refs, &ares->referral);
902 res->refs[n + 1] = NULL;
906 /* TODO: we should really support controls on entries
907 * and referrals too! */
908 res->controls = talloc_move(res, &ares->controls);
910 /* this is the last message, and means the request is done */
911 /* we have to signal and eventual ldb_wait() waiting that the
912 * async request operation was completed */
914 return ldb_request_done(req, LDB_SUCCESS);
922 int ldb_modify_default_callback(struct ldb_request *req, struct ldb_reply *ares)
924 struct ldb_result *res;
928 res = talloc_get_type(req->context, struct ldb_result);
931 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
934 if (ares->error != LDB_SUCCESS) {
937 return ldb_request_done(req, ret);
940 switch (ares->type) {
941 case LDB_REPLY_REFERRAL:
943 for (n = 0; res->refs[n]; n++) /*noop*/ ;
948 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
950 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
953 res->refs[n] = talloc_move(res->refs, &ares->referral);
954 res->refs[n + 1] = NULL;
959 return ldb_request_done(req, LDB_SUCCESS);
962 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
963 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
967 return ldb_request_done(req, LDB_SUCCESS);
970 int ldb_op_default_callback(struct ldb_request *req, struct ldb_reply *ares)
975 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
978 if (ares->error != LDB_SUCCESS) {
981 return ldb_request_done(req, ret);
984 if (ares->type != LDB_REPLY_DONE) {
986 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
987 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
991 return ldb_request_done(req, LDB_SUCCESS);
994 int ldb_build_search_req_ex(struct ldb_request **ret_req,
995 struct ldb_context *ldb,
998 enum ldb_scope scope,
999 struct ldb_parse_tree *tree,
1000 const char * const *attrs,
1001 struct ldb_control **controls,
1003 ldb_request_callback_t callback,
1004 struct ldb_request *parent)
1006 struct ldb_request *req;
1010 req = talloc(mem_ctx, struct ldb_request);
1013 return LDB_ERR_OPERATIONS_ERROR;
1016 req->operation = LDB_SEARCH;
1018 req->op.search.base = ldb_dn_new(req, ldb, NULL);
1020 req->op.search.base = base;
1022 req->op.search.scope = scope;
1024 req->op.search.tree = tree;
1025 if (req->op.search.tree == NULL) {
1026 ldb_set_errstring(ldb, "'tree' can't be NULL");
1028 return LDB_ERR_OPERATIONS_ERROR;
1031 req->op.search.attrs = attrs;
1032 req->controls = controls;
1033 req->context = context;
1034 req->callback = callback;
1036 ldb_set_timeout_from_prev_req(ldb, parent, req);
1038 req->handle = ldb_handle_new(req, ldb);
1039 if (req->handle == NULL) {
1041 return LDB_ERR_OPERATIONS_ERROR;
1045 req->handle->nesting++;
1052 int ldb_build_search_req(struct ldb_request **ret_req,
1053 struct ldb_context *ldb,
1054 TALLOC_CTX *mem_ctx,
1055 struct ldb_dn *base,
1056 enum ldb_scope scope,
1057 const char *expression,
1058 const char * const *attrs,
1059 struct ldb_control **controls,
1061 ldb_request_callback_t callback,
1062 struct ldb_request *parent)
1064 struct ldb_parse_tree *tree;
1067 tree = ldb_parse_tree(mem_ctx, expression);
1069 ldb_set_errstring(ldb, "Unable to parse search expression");
1070 return LDB_ERR_OPERATIONS_ERROR;
1073 ret = ldb_build_search_req_ex(ret_req, ldb, mem_ctx, base,
1074 scope, tree, attrs, controls,
1075 context, callback, parent);
1076 if (ret == LDB_SUCCESS) {
1077 talloc_steal(*ret_req, tree);
1082 int ldb_build_add_req(struct ldb_request **ret_req,
1083 struct ldb_context *ldb,
1084 TALLOC_CTX *mem_ctx,
1085 const struct ldb_message *message,
1086 struct ldb_control **controls,
1088 ldb_request_callback_t callback,
1089 struct ldb_request *parent)
1091 struct ldb_request *req;
1095 req = talloc(mem_ctx, struct ldb_request);
1097 ldb_set_errstring(ldb, "Out of Memory");
1098 return LDB_ERR_OPERATIONS_ERROR;
1101 req->operation = LDB_ADD;
1102 req->op.add.message = message;
1103 req->controls = controls;
1104 req->context = context;
1105 req->callback = callback;
1107 ldb_set_timeout_from_prev_req(ldb, parent, req);
1109 req->handle = ldb_handle_new(req, ldb);
1110 if (req->handle == NULL) {
1112 return LDB_ERR_OPERATIONS_ERROR;
1116 req->handle->nesting++;
1124 int ldb_build_mod_req(struct ldb_request **ret_req,
1125 struct ldb_context *ldb,
1126 TALLOC_CTX *mem_ctx,
1127 const struct ldb_message *message,
1128 struct ldb_control **controls,
1130 ldb_request_callback_t callback,
1131 struct ldb_request *parent)
1133 struct ldb_request *req;
1137 req = talloc(mem_ctx, struct ldb_request);
1139 ldb_set_errstring(ldb, "Out of Memory");
1140 return LDB_ERR_OPERATIONS_ERROR;
1143 req->operation = LDB_MODIFY;
1144 req->op.mod.message = message;
1145 req->controls = controls;
1146 req->context = context;
1147 req->callback = callback;
1149 ldb_set_timeout_from_prev_req(ldb, parent, req);
1151 req->handle = ldb_handle_new(req, ldb);
1152 if (req->handle == NULL) {
1154 return LDB_ERR_OPERATIONS_ERROR;
1158 req->handle->nesting++;
1166 int ldb_build_del_req(struct ldb_request **ret_req,
1167 struct ldb_context *ldb,
1168 TALLOC_CTX *mem_ctx,
1170 struct ldb_control **controls,
1172 ldb_request_callback_t callback,
1173 struct ldb_request *parent)
1175 struct ldb_request *req;
1179 req = talloc(mem_ctx, struct ldb_request);
1181 ldb_set_errstring(ldb, "Out of Memory");
1182 return LDB_ERR_OPERATIONS_ERROR;
1185 req->operation = LDB_DELETE;
1186 req->op.del.dn = dn;
1187 req->controls = controls;
1188 req->context = context;
1189 req->callback = callback;
1191 ldb_set_timeout_from_prev_req(ldb, parent, req);
1193 req->handle = ldb_handle_new(req, ldb);
1194 if (req->handle == NULL) {
1196 return LDB_ERR_OPERATIONS_ERROR;
1200 req->handle->nesting++;
1208 int ldb_build_rename_req(struct ldb_request **ret_req,
1209 struct ldb_context *ldb,
1210 TALLOC_CTX *mem_ctx,
1211 struct ldb_dn *olddn,
1212 struct ldb_dn *newdn,
1213 struct ldb_control **controls,
1215 ldb_request_callback_t callback,
1216 struct ldb_request *parent)
1218 struct ldb_request *req;
1222 req = talloc(mem_ctx, struct ldb_request);
1224 ldb_set_errstring(ldb, "Out of Memory");
1225 return LDB_ERR_OPERATIONS_ERROR;
1228 req->operation = LDB_RENAME;
1229 req->op.rename.olddn = olddn;
1230 req->op.rename.newdn = newdn;
1231 req->controls = controls;
1232 req->context = context;
1233 req->callback = callback;
1235 ldb_set_timeout_from_prev_req(ldb, parent, req);
1237 req->handle = ldb_handle_new(req, ldb);
1238 if (req->handle == NULL) {
1240 return LDB_ERR_OPERATIONS_ERROR;
1244 req->handle->nesting++;
1252 int ldb_extended_default_callback(struct ldb_request *req,
1253 struct ldb_reply *ares)
1255 struct ldb_result *res;
1257 res = talloc_get_type(req->context, struct ldb_result);
1260 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1262 if (ares->error != LDB_SUCCESS) {
1263 return ldb_request_done(req, ares->error);
1266 if (ares->type == LDB_REPLY_DONE) {
1268 /* TODO: we should really support controls on entries and referrals too! */
1269 res->extended = talloc_move(res, &ares->response);
1270 res->controls = talloc_move(res, &ares->controls);
1273 return ldb_request_done(req, LDB_SUCCESS);
1277 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
1278 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1281 int ldb_build_extended_req(struct ldb_request **ret_req,
1282 struct ldb_context *ldb,
1283 TALLOC_CTX *mem_ctx,
1286 struct ldb_control **controls,
1288 ldb_request_callback_t callback,
1289 struct ldb_request *parent)
1291 struct ldb_request *req;
1295 req = talloc(mem_ctx, struct ldb_request);
1297 ldb_set_errstring(ldb, "Out of Memory");
1298 return LDB_ERR_OPERATIONS_ERROR;
1301 req->operation = LDB_EXTENDED;
1302 req->op.extended.oid = oid;
1303 req->op.extended.data = data;
1304 req->controls = controls;
1305 req->context = context;
1306 req->callback = callback;
1308 ldb_set_timeout_from_prev_req(ldb, parent, req);
1310 req->handle = ldb_handle_new(req, ldb);
1311 if (req->handle == NULL) {
1313 return LDB_ERR_OPERATIONS_ERROR;
1317 req->handle->nesting++;
1325 int ldb_extended(struct ldb_context *ldb,
1328 struct ldb_result **_res)
1330 struct ldb_request *req;
1332 struct ldb_result *res;
1336 res = talloc_zero(ldb, struct ldb_result);
1338 return LDB_ERR_OPERATIONS_ERROR;
1341 ret = ldb_build_extended_req(&req, ldb, ldb,
1343 res, ldb_extended_default_callback,
1345 if (ret != LDB_SUCCESS) goto done;
1347 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1349 ret = ldb_request(ldb, req);
1351 if (ret == LDB_SUCCESS) {
1352 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1358 if (ret != LDB_SUCCESS) {
1367 note that ldb_search() will automatically replace a NULL 'base' value
1368 with the defaultNamingContext from the rootDSE if available.
1370 int ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1371 struct ldb_result **result, struct ldb_dn *base,
1372 enum ldb_scope scope, const char * const *attrs,
1373 const char *exp_fmt, ...)
1375 struct ldb_request *req;
1376 struct ldb_result *res;
1385 res = talloc_zero(mem_ctx, struct ldb_result);
1387 return LDB_ERR_OPERATIONS_ERROR;
1391 va_start(ap, exp_fmt);
1392 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
1397 return LDB_ERR_OPERATIONS_ERROR;
1401 ret = ldb_build_search_req(&req, ldb, mem_ctx,
1402 base?base:ldb_get_default_basedn(ldb),
1408 ldb_search_default_callback,
1411 if (ret != LDB_SUCCESS) goto done;
1413 ret = ldb_request(ldb, req);
1415 if (ret == LDB_SUCCESS) {
1416 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1420 if (ret != LDB_SUCCESS) {
1425 talloc_free(expression);
1433 add a record to the database. Will fail if a record with the given class
1434 and key already exists
1436 int ldb_add(struct ldb_context *ldb,
1437 const struct ldb_message *message)
1439 struct ldb_request *req;
1442 ret = ldb_msg_sanity_check(ldb, message);
1443 if (ret != LDB_SUCCESS) {
1447 ret = ldb_build_add_req(&req, ldb, ldb,
1451 ldb_op_default_callback,
1454 if (ret != LDB_SUCCESS) return ret;
1456 /* do request and autostart a transaction */
1457 ret = ldb_autotransaction_request(ldb, req);
1464 modify the specified attributes of a record
1466 int ldb_modify(struct ldb_context *ldb,
1467 const struct ldb_message *message)
1469 struct ldb_request *req;
1472 ret = ldb_msg_sanity_check(ldb, message);
1473 if (ret != LDB_SUCCESS) {
1477 ret = ldb_build_mod_req(&req, ldb, ldb,
1481 ldb_op_default_callback,
1484 if (ret != LDB_SUCCESS) return ret;
1486 /* do request and autostart a transaction */
1487 ret = ldb_autotransaction_request(ldb, req);
1495 delete a record from the database
1497 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1499 struct ldb_request *req;
1502 ret = ldb_build_del_req(&req, ldb, ldb,
1506 ldb_op_default_callback,
1509 if (ret != LDB_SUCCESS) return ret;
1511 /* do request and autostart a transaction */
1512 ret = ldb_autotransaction_request(ldb, req);
1519 rename a record in the database
1521 int ldb_rename(struct ldb_context *ldb,
1522 struct ldb_dn *olddn, struct ldb_dn *newdn)
1524 struct ldb_request *req;
1527 ret = ldb_build_rename_req(&req, ldb, ldb,
1532 ldb_op_default_callback,
1535 if (ret != LDB_SUCCESS) return ret;
1537 /* do request and autostart a transaction */
1538 ret = ldb_autotransaction_request(ldb, req);
1546 return the global sequence number
1548 int ldb_sequence_number(struct ldb_context *ldb,
1549 enum ldb_sequence_type type, uint64_t *seq_num)
1551 struct ldb_seqnum_request *seq;
1552 struct ldb_seqnum_result *seqr;
1553 struct ldb_result *res;
1554 TALLOC_CTX *tmp_ctx;
1559 tmp_ctx = talloc_zero(ldb, struct ldb_request);
1560 if (tmp_ctx == NULL) {
1561 ldb_set_errstring(ldb, "Out of Memory");
1562 return LDB_ERR_OPERATIONS_ERROR;
1564 seq = talloc_zero(tmp_ctx, struct ldb_seqnum_request);
1566 ldb_set_errstring(ldb, "Out of Memory");
1567 ret = LDB_ERR_OPERATIONS_ERROR;
1572 ret = ldb_extended(ldb, LDB_EXTENDED_SEQUENCE_NUMBER, seq, &res);
1573 if (ret != LDB_SUCCESS) {
1576 talloc_steal(tmp_ctx, res);
1578 if (strcmp(LDB_EXTENDED_SEQUENCE_NUMBER, res->extended->oid) != 0) {
1579 ldb_set_errstring(ldb, "Invalid OID in reply");
1580 ret = LDB_ERR_OPERATIONS_ERROR;
1583 seqr = talloc_get_type(res->extended->data,
1584 struct ldb_seqnum_result);
1585 *seq_num = seqr->seq_num;
1588 talloc_free(tmp_ctx);
1593 return extended error information
1595 const char *ldb_errstring(struct ldb_context *ldb)
1597 if (ldb->err_string) {
1598 return ldb->err_string;
1605 return a string explaining what a ldb error constant meancs
1607 const char *ldb_strerror(int ldb_err)
1612 case LDB_ERR_OPERATIONS_ERROR:
1613 return "Operations error";
1614 case LDB_ERR_PROTOCOL_ERROR:
1615 return "Protocol error";
1616 case LDB_ERR_TIME_LIMIT_EXCEEDED:
1617 return "Time limit exceeded";
1618 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1619 return "Size limit exceeded";
1620 case LDB_ERR_COMPARE_FALSE:
1621 return "Compare false";
1622 case LDB_ERR_COMPARE_TRUE:
1623 return "Compare true";
1624 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1625 return "Auth method not supported";
1626 case LDB_ERR_STRONG_AUTH_REQUIRED:
1627 return "Strong auth required";
1629 case LDB_ERR_REFERRAL:
1630 return "Referral error";
1631 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1632 return "Admin limit exceeded";
1633 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1634 return "Unsupported critical extension";
1635 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1636 return "Confidentiality required";
1637 case LDB_ERR_SASL_BIND_IN_PROGRESS:
1638 return "SASL bind in progress";
1639 case LDB_ERR_NO_SUCH_ATTRIBUTE:
1640 return "No such attribute";
1641 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1642 return "Undefined attribute type";
1643 case LDB_ERR_INAPPROPRIATE_MATCHING:
1644 return "Inappropriate matching";
1645 case LDB_ERR_CONSTRAINT_VIOLATION:
1646 return "Constraint violation";
1647 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1648 return "Attribute or value exists";
1649 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1650 return "Invalid attribute syntax";
1652 case LDB_ERR_NO_SUCH_OBJECT:
1653 return "No such object";
1654 case LDB_ERR_ALIAS_PROBLEM:
1655 return "Alias problem";
1656 case LDB_ERR_INVALID_DN_SYNTAX:
1657 return "Invalid DN syntax";
1659 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1660 return "Alias dereferencing problem";
1662 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1663 return "Inappropriate authentication";
1664 case LDB_ERR_INVALID_CREDENTIALS:
1665 return "Invalid credentials";
1666 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1667 return "insufficient access rights";
1670 case LDB_ERR_UNAVAILABLE:
1671 return "Unavailable";
1672 case LDB_ERR_UNWILLING_TO_PERFORM:
1673 return "Unwilling to perform";
1674 case LDB_ERR_LOOP_DETECT:
1675 return "Loop detect";
1677 case LDB_ERR_NAMING_VIOLATION:
1678 return "Naming violation";
1679 case LDB_ERR_OBJECT_CLASS_VIOLATION:
1680 return "Object class violation";
1681 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1682 return "Not allowed on non-leaf";
1683 case LDB_ERR_NOT_ALLOWED_ON_RDN:
1684 return "Not allowed on RDN";
1685 case LDB_ERR_ENTRY_ALREADY_EXISTS:
1686 return "Entry already exists";
1687 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1688 return "Object class mods prohibited";
1689 /* 70 RESERVED FOR CLDAP */
1690 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1691 return "Affects multiple DSAs";
1697 return "Unknown error";
1701 set backend specific opaque parameters
1703 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1705 struct ldb_opaque *o;
1707 /* allow updating an existing value */
1708 for (o=ldb->opaque;o;o=o->next) {
1709 if (strcmp(o->name, name) == 0) {
1715 o = talloc(ldb, struct ldb_opaque);
1718 return LDB_ERR_OTHER;
1720 o->next = ldb->opaque;
1728 get a previously set opaque value
1730 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1732 struct ldb_opaque *o;
1733 for (o=ldb->opaque;o;o=o->next) {
1734 if (strcmp(o->name, name) == 0) {
1741 int ldb_global_init(void)
1743 /* Provided for compatibility with some older versions of ldb */
1747 /* return the ldb flags */
1748 unsigned int ldb_get_flags(struct ldb_context *ldb)
1753 /* set the ldb flags */
1754 void ldb_set_flags(struct ldb_context *ldb, unsigned flags)