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);
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;
288 #define FIRST_OP_NOERR(ldb, op) do { \
289 module = ldb->modules; \
290 while (module && module->ops->op == NULL) module = module->next; \
293 #define FIRST_OP(ldb, op) do { \
294 FIRST_OP_NOERR(ldb, op); \
295 if (module == NULL) { \
296 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
297 return LDB_ERR_OPERATIONS_ERROR; \
305 int ldb_transaction_start(struct ldb_context *ldb)
307 struct ldb_module *module;
310 ldb_debug(ldb, LDB_DEBUG_TRACE,
311 "start ldb transaction (nesting: %d)",
312 ldb->transaction_active);
314 /* explicit transaction active, count nested requests */
315 if (ldb->transaction_active) {
316 ldb->transaction_active++;
320 /* start a new transaction */
321 ldb->transaction_active++;
322 ldb->prepare_commit_done = false;
324 FIRST_OP(ldb, start_transaction);
326 ldb_reset_err_string(ldb);
328 status = module->ops->start_transaction(module);
329 if (status != LDB_SUCCESS) {
330 if (ldb->err_string == NULL) {
331 /* no error string was setup by the backend */
332 ldb_asprintf_errstring(ldb,
333 "ldb transaction start: %s (%d)",
334 ldb_strerror(status),
342 prepare for transaction commit (first phase of two phase commit)
344 int ldb_transaction_prepare_commit(struct ldb_context *ldb)
346 struct ldb_module *module;
349 if (ldb->prepare_commit_done) {
353 /* commit only when all nested transactions are complete */
354 if (ldb->transaction_active > 1) {
358 ldb->prepare_commit_done = true;
360 if (ldb->transaction_active < 0) {
361 ldb_debug(ldb, LDB_DEBUG_FATAL,
362 "prepare commit called but no ldb transactions are active!");
363 ldb->transaction_active = 0;
364 return LDB_ERR_OPERATIONS_ERROR;
367 /* call prepare transaction if available */
368 FIRST_OP_NOERR(ldb, prepare_commit);
369 if (module == NULL) {
373 status = module->ops->prepare_commit(module);
374 if (status != LDB_SUCCESS) {
375 /* if a module fails the prepare then we need
376 to call the end transaction for everyone */
377 FIRST_OP(ldb, end_transaction);
378 module->ops->end_transaction(module);
379 if (ldb->err_string == NULL) {
380 /* no error string was setup by the backend */
381 ldb_asprintf_errstring(ldb,
382 "ldb transaction prepare commit: %s (%d)",
383 ldb_strerror(status),
395 int ldb_transaction_commit(struct ldb_context *ldb)
397 struct ldb_module *module;
400 status = ldb_transaction_prepare_commit(ldb);
401 if (status != LDB_SUCCESS) {
405 ldb->transaction_active--;
407 ldb_debug(ldb, LDB_DEBUG_TRACE,
408 "commit ldb transaction (nesting: %d)",
409 ldb->transaction_active);
411 /* commit only when all nested transactions are complete */
412 if (ldb->transaction_active > 0) {
416 if (ldb->transaction_active < 0) {
417 ldb_debug(ldb, LDB_DEBUG_FATAL,
418 "commit called but no ldb transactions are active!");
419 ldb->transaction_active = 0;
420 return LDB_ERR_OPERATIONS_ERROR;
423 ldb_reset_err_string(ldb);
425 FIRST_OP(ldb, end_transaction);
426 status = module->ops->end_transaction(module);
427 if (status != LDB_SUCCESS) {
428 if (ldb->err_string == NULL) {
429 /* no error string was setup by the backend */
430 ldb_asprintf_errstring(ldb,
431 "ldb transaction commit: %s (%d)",
432 ldb_strerror(status),
435 /* cancel the transaction */
436 FIRST_OP(ldb, del_transaction);
437 module->ops->del_transaction(module);
446 int ldb_transaction_cancel(struct ldb_context *ldb)
448 struct ldb_module *module;
451 ldb->transaction_active--;
453 ldb_debug(ldb, LDB_DEBUG_TRACE,
454 "cancel ldb transaction (nesting: %d)",
455 ldb->transaction_active);
457 /* really cancel only if all nested transactions are complete */
458 if (ldb->transaction_active > 0) {
462 if (ldb->transaction_active < 0) {
463 ldb_debug(ldb, LDB_DEBUG_FATAL,
464 "cancel called but no ldb transactions are active!");
465 ldb->transaction_active = 0;
466 return LDB_ERR_OPERATIONS_ERROR;
469 FIRST_OP(ldb, del_transaction);
471 status = module->ops->del_transaction(module);
472 if (status != LDB_SUCCESS) {
473 if (ldb->err_string == NULL) {
474 /* no error string was setup by the backend */
475 ldb_asprintf_errstring(ldb,
476 "ldb transaction cancel: %s (%d)",
477 ldb_strerror(status),
484 /* autostarts a transacion if none active */
485 static int ldb_autotransaction_request(struct ldb_context *ldb,
486 struct ldb_request *req)
490 ret = ldb_transaction_start(ldb);
491 if (ret != LDB_SUCCESS) {
495 ret = ldb_request(ldb, req);
496 if (ret == LDB_SUCCESS) {
497 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
500 if (ret == LDB_SUCCESS) {
501 return ldb_transaction_commit(ldb);
503 ldb_transaction_cancel(ldb);
505 if (ldb->err_string == NULL) {
506 /* no error string was setup by the backend */
507 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
513 int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
515 struct tevent_context *ev;
519 return LDB_ERR_UNAVAILABLE;
522 if (handle->state == LDB_ASYNC_DONE) {
523 return handle->status;
526 ev = ldb_get_event_context(handle->ldb);
528 return LDB_ERR_OPERATIONS_ERROR;
533 ret = tevent_loop_once(ev);
535 return LDB_ERR_OPERATIONS_ERROR;
537 if (handle->state == LDB_ASYNC_DONE ||
538 handle->status != LDB_SUCCESS) {
539 return handle->status;
544 while (handle->state != LDB_ASYNC_DONE) {
545 ret = tevent_loop_once(ev);
547 return LDB_ERR_OPERATIONS_ERROR;
549 if (handle->status != LDB_SUCCESS) {
550 return handle->status;
553 return handle->status;
559 /* set the specified timeout or, if timeout is 0 set the default timeout */
560 int ldb_set_timeout(struct ldb_context *ldb,
561 struct ldb_request *req,
564 if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
567 req->timeout = timeout;
569 req->timeout = ldb->default_timeout;
571 req->starttime = time(NULL);
576 /* calculates the new timeout based on the previous starttime and timeout */
577 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb,
578 struct ldb_request *oldreq,
579 struct ldb_request *newreq)
581 if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
583 if (oldreq == NULL) {
584 return ldb_set_timeout(ldb, newreq, 0);
587 newreq->starttime = oldreq->starttime;
588 newreq->timeout = oldreq->timeout;
595 set the permissions for new files to be passed to open() in
596 backends that use local files
598 void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
600 ldb->create_perms = perms;
603 unsigned int ldb_get_create_perms(struct ldb_context *ldb)
605 return ldb->create_perms;
608 void ldb_set_event_context(struct ldb_context *ldb, struct tevent_context *ev)
613 struct tevent_context * ldb_get_event_context(struct ldb_context *ldb)
618 void ldb_request_set_state(struct ldb_request *req, int state)
620 req->handle->state = state;
623 int ldb_request_get_status(struct ldb_request *req)
625 return req->handle->status;
632 static void ldb_trace_request(struct ldb_context *ldb, struct ldb_request *req)
634 TALLOC_CTX *tmp_ctx = talloc_new(req);
637 switch (req->operation) {
639 ldb_debug_add(ldb, "ldb_trace_request: SEARCH\n");
640 ldb_debug_add(ldb, " dn: %s\n",
641 ldb_dn_is_null(req->op.search.base)?"<rootDSE>":
642 ldb_dn_get_linearized(req->op.search.base));
643 ldb_debug_add(ldb, " scope: %s\n",
644 req->op.search.scope==LDB_SCOPE_BASE?"base":
645 req->op.search.scope==LDB_SCOPE_ONELEVEL?"one":
646 req->op.search.scope==LDB_SCOPE_SUBTREE?"sub":"UNKNOWN");
647 ldb_debug_add(ldb, " expr: %s\n",
648 ldb_filter_from_tree(tmp_ctx, req->op.search.tree));
649 if (req->op.search.attrs == NULL) {
650 ldb_debug_add(ldb, " attr: <ALL>\n");
652 for (i=0; req->op.search.attrs[i]; i++) {
653 ldb_debug_add(ldb, " attr: %s\n", req->op.search.attrs[i]);
658 ldb_debug_add(ldb, "ldb_trace_request: DELETE\n");
659 ldb_debug_add(ldb, " dn: %s\n",
660 ldb_dn_get_linearized(req->op.del.dn));
663 ldb_debug_add(ldb, "ldb_trace_request: RENAME\n");
664 ldb_debug_add(ldb, " olddn: %s\n",
665 ldb_dn_get_linearized(req->op.rename.olddn));
666 ldb_debug_add(ldb, " newdn: %s\n",
667 ldb_dn_get_linearized(req->op.rename.newdn));
670 ldb_debug_add(ldb, "ldb_trace_request: EXTENDED\n");
671 ldb_debug_add(ldb, " oid: %s\n", req->op.extended.oid);
672 ldb_debug_add(ldb, " data: %s\n", req->op.extended.data?"yes":"no");
675 ldb_debug_add(ldb, "ldb_trace_request: ADD\n");
676 ldb_debug_add(req->handle->ldb, "%s\n",
677 ldb_ldif_message_string(req->handle->ldb, tmp_ctx,
679 req->op.add.message));
682 ldb_debug_add(ldb, "ldb_trace_request: MODIFY\n");
683 ldb_debug_add(req->handle->ldb, "%s\n",
684 ldb_ldif_message_string(req->handle->ldb, tmp_ctx,
686 req->op.mod.message));
688 case LDB_REQ_REGISTER_CONTROL:
689 ldb_debug_add(ldb, "ldb_trace_request: REGISTER_CONTROL\n");
690 ldb_debug_add(req->handle->ldb, "%s\n",
691 req->op.reg_control.oid);
693 case LDB_REQ_REGISTER_PARTITION:
694 ldb_debug_add(ldb, "ldb_trace_request: REGISTER_PARTITION\n");
695 ldb_debug_add(req->handle->ldb, "%s\n",
696 ldb_dn_get_linearized(req->op.reg_partition.dn));
699 ldb_debug_add(ldb, "ldb_trace_request: UNKNOWN(%u)\n",
704 if (req->controls == NULL) {
705 ldb_debug_add(ldb, " control: <NONE>\n");
707 for (i=0; req->controls && req->controls[i]; i++) {
708 ldb_debug_add(ldb, " control: %s crit:%u data:%s\n",
709 req->controls[i]->oid,
710 req->controls[i]->critical,
711 req->controls[i]->data?"yes":"no");
715 ldb_debug_end(ldb, LDB_DEBUG_TRACE);
717 talloc_free(tmp_ctx);
723 NOTE: the request must be a talloc context.
724 returns LDB_ERR_* on errors.
726 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
728 struct ldb_module *module;
731 if (req->callback == NULL) {
732 ldb_set_errstring(ldb, "Requests MUST define callbacks");
733 return LDB_ERR_UNWILLING_TO_PERFORM;
736 ldb_reset_err_string(ldb);
738 if (ldb->flags & LDB_FLG_ENABLE_TRACING) {
739 ldb_trace_request(ldb, req);
742 /* call the first module in the chain */
743 switch (req->operation) {
745 FIRST_OP(ldb, search);
746 ret = module->ops->search(module, req);
750 ret = module->ops->add(module, req);
753 FIRST_OP(ldb, modify);
754 ret = module->ops->modify(module, req);
758 ret = module->ops->del(module, req);
761 FIRST_OP(ldb, rename);
762 ret = module->ops->rename(module, req);
765 FIRST_OP(ldb, extended);
766 ret = module->ops->extended(module, req);
769 FIRST_OP(ldb, request);
770 ret = module->ops->request(module, req);
777 int ldb_request_done(struct ldb_request *req, int status)
779 req->handle->state = LDB_ASYNC_DONE;
780 req->handle->status = status;
785 search the database given a LDAP-like search expression
787 returns an LDB error code
789 Use talloc_free to free the ldb_message returned in 'res', if successful
792 int ldb_search_default_callback(struct ldb_request *req,
793 struct ldb_reply *ares)
795 struct ldb_result *res;
798 res = talloc_get_type(req->context, struct ldb_result);
801 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
803 if (ares->error != LDB_SUCCESS) {
804 return ldb_request_done(req, ares->error);
807 switch (ares->type) {
808 case LDB_REPLY_ENTRY:
809 res->msgs = talloc_realloc(res, res->msgs,
810 struct ldb_message *, res->count + 2);
812 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
815 res->msgs[res->count + 1] = NULL;
817 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
821 case LDB_REPLY_REFERRAL:
823 for (n = 0; res->refs[n]; n++) /*noop*/ ;
828 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
830 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
833 res->refs[n] = talloc_move(res->refs, &ares->referral);
834 res->refs[n + 1] = NULL;
838 /* TODO: we should really support controls on entries
839 * and referrals too! */
840 res->controls = talloc_move(res, &ares->controls);
842 /* this is the last message, and means the request is done */
843 /* we have to signal and eventual ldb_wait() waiting that the
844 * async request operation was completed */
846 return ldb_request_done(req, LDB_SUCCESS);
854 int ldb_op_default_callback(struct ldb_request *req, struct ldb_reply *ares)
859 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
862 if (ares->error != LDB_SUCCESS) {
865 return ldb_request_done(req, ret);
868 if (ares->type != LDB_REPLY_DONE) {
870 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
871 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
875 return ldb_request_done(req, LDB_SUCCESS);
878 int ldb_build_search_req_ex(struct ldb_request **ret_req,
879 struct ldb_context *ldb,
882 enum ldb_scope scope,
883 struct ldb_parse_tree *tree,
884 const char * const *attrs,
885 struct ldb_control **controls,
887 ldb_request_callback_t callback,
888 struct ldb_request *parent)
890 struct ldb_request *req;
894 req = talloc(mem_ctx, struct ldb_request);
897 return LDB_ERR_OPERATIONS_ERROR;
900 req->operation = LDB_SEARCH;
902 req->op.search.base = ldb_dn_new(req, ldb, NULL);
904 req->op.search.base = base;
906 req->op.search.scope = scope;
908 req->op.search.tree = tree;
909 if (req->op.search.tree == NULL) {
910 ldb_set_errstring(ldb, "'tree' can't be NULL");
912 return LDB_ERR_OPERATIONS_ERROR;
915 req->op.search.attrs = attrs;
916 req->controls = controls;
917 req->context = context;
918 req->callback = callback;
920 ldb_set_timeout_from_prev_req(ldb, parent, req);
922 req->handle = ldb_handle_new(req, ldb);
923 if (req->handle == NULL) {
925 return LDB_ERR_OPERATIONS_ERROR;
929 req->handle->nesting++;
936 int ldb_build_search_req(struct ldb_request **ret_req,
937 struct ldb_context *ldb,
940 enum ldb_scope scope,
941 const char *expression,
942 const char * const *attrs,
943 struct ldb_control **controls,
945 ldb_request_callback_t callback,
946 struct ldb_request *parent)
948 struct ldb_parse_tree *tree;
951 tree = ldb_parse_tree(mem_ctx, expression);
953 ldb_set_errstring(ldb, "Unable to parse search expression");
954 return LDB_ERR_OPERATIONS_ERROR;
957 ret = ldb_build_search_req_ex(ret_req, ldb, mem_ctx, base,
958 scope, tree, attrs, controls,
959 context, callback, parent);
960 if (ret == LDB_SUCCESS) {
961 talloc_steal(*ret_req, tree);
966 int ldb_build_add_req(struct ldb_request **ret_req,
967 struct ldb_context *ldb,
969 const struct ldb_message *message,
970 struct ldb_control **controls,
972 ldb_request_callback_t callback,
973 struct ldb_request *parent)
975 struct ldb_request *req;
979 req = talloc(mem_ctx, struct ldb_request);
981 ldb_set_errstring(ldb, "Out of Memory");
982 return LDB_ERR_OPERATIONS_ERROR;
985 req->operation = LDB_ADD;
986 req->op.add.message = message;
987 req->controls = controls;
988 req->context = context;
989 req->callback = callback;
991 ldb_set_timeout_from_prev_req(ldb, parent, req);
993 req->handle = ldb_handle_new(req, ldb);
994 if (req->handle == NULL) {
996 return LDB_ERR_OPERATIONS_ERROR;
1000 req->handle->nesting++;
1008 int ldb_build_mod_req(struct ldb_request **ret_req,
1009 struct ldb_context *ldb,
1011 const struct ldb_message *message,
1012 struct ldb_control **controls,
1014 ldb_request_callback_t callback,
1015 struct ldb_request *parent)
1017 struct ldb_request *req;
1021 req = talloc(mem_ctx, struct ldb_request);
1023 ldb_set_errstring(ldb, "Out of Memory");
1024 return LDB_ERR_OPERATIONS_ERROR;
1027 req->operation = LDB_MODIFY;
1028 req->op.mod.message = message;
1029 req->controls = controls;
1030 req->context = context;
1031 req->callback = callback;
1033 ldb_set_timeout_from_prev_req(ldb, parent, req);
1035 req->handle = ldb_handle_new(req, ldb);
1036 if (req->handle == NULL) {
1038 return LDB_ERR_OPERATIONS_ERROR;
1042 req->handle->nesting++;
1050 int ldb_build_del_req(struct ldb_request **ret_req,
1051 struct ldb_context *ldb,
1054 struct ldb_control **controls,
1056 ldb_request_callback_t callback,
1057 struct ldb_request *parent)
1059 struct ldb_request *req;
1063 req = talloc(mem_ctx, struct ldb_request);
1065 ldb_set_errstring(ldb, "Out of Memory");
1066 return LDB_ERR_OPERATIONS_ERROR;
1069 req->operation = LDB_DELETE;
1070 req->op.del.dn = dn;
1071 req->controls = controls;
1072 req->context = context;
1073 req->callback = callback;
1075 ldb_set_timeout_from_prev_req(ldb, parent, req);
1077 req->handle = ldb_handle_new(req, ldb);
1078 if (req->handle == NULL) {
1080 return LDB_ERR_OPERATIONS_ERROR;
1084 req->handle->nesting++;
1092 int ldb_build_rename_req(struct ldb_request **ret_req,
1093 struct ldb_context *ldb,
1095 struct ldb_dn *olddn,
1096 struct ldb_dn *newdn,
1097 struct ldb_control **controls,
1099 ldb_request_callback_t callback,
1100 struct ldb_request *parent)
1102 struct ldb_request *req;
1106 req = talloc(mem_ctx, struct ldb_request);
1108 ldb_set_errstring(ldb, "Out of Memory");
1109 return LDB_ERR_OPERATIONS_ERROR;
1112 req->operation = LDB_RENAME;
1113 req->op.rename.olddn = olddn;
1114 req->op.rename.newdn = newdn;
1115 req->controls = controls;
1116 req->context = context;
1117 req->callback = callback;
1119 ldb_set_timeout_from_prev_req(ldb, parent, req);
1121 req->handle = ldb_handle_new(req, ldb);
1122 if (req->handle == NULL) {
1124 return LDB_ERR_OPERATIONS_ERROR;
1128 req->handle->nesting++;
1136 int ldb_extended_default_callback(struct ldb_request *req,
1137 struct ldb_reply *ares)
1139 struct ldb_result *res;
1141 res = talloc_get_type(req->context, struct ldb_result);
1144 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1146 if (ares->error != LDB_SUCCESS) {
1147 return ldb_request_done(req, ares->error);
1150 if (ares->type == LDB_REPLY_DONE) {
1152 /* TODO: we should really support controls on entries and referrals too! */
1153 res->extended = talloc_move(res, &ares->response);
1154 res->controls = talloc_move(res, &ares->controls);
1157 return ldb_request_done(req, LDB_SUCCESS);
1161 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
1162 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1165 int ldb_build_extended_req(struct ldb_request **ret_req,
1166 struct ldb_context *ldb,
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_EXTENDED;
1186 req->op.extended.oid = oid;
1187 req->op.extended.data = data;
1188 req->controls = controls;
1189 req->context = context;
1190 req->callback = callback;
1192 ldb_set_timeout_from_prev_req(ldb, parent, req);
1194 req->handle = ldb_handle_new(req, ldb);
1195 if (req->handle == NULL) {
1197 return LDB_ERR_OPERATIONS_ERROR;
1201 req->handle->nesting++;
1209 int ldb_extended(struct ldb_context *ldb,
1212 struct ldb_result **_res)
1214 struct ldb_request *req;
1216 struct ldb_result *res;
1220 res = talloc_zero(ldb, struct ldb_result);
1222 return LDB_ERR_OPERATIONS_ERROR;
1225 ret = ldb_build_extended_req(&req, ldb, ldb,
1227 res, ldb_extended_default_callback,
1229 if (ret != LDB_SUCCESS) goto done;
1231 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1233 ret = ldb_request(ldb, req);
1235 if (ret == LDB_SUCCESS) {
1236 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1242 if (ret != LDB_SUCCESS) {
1251 note that ldb_search() will automatically replace a NULL 'base' value
1252 with the defaultNamingContext from the rootDSE if available.
1254 int ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1255 struct ldb_result **result, struct ldb_dn *base,
1256 enum ldb_scope scope, const char * const *attrs,
1257 const char *exp_fmt, ...)
1259 struct ldb_request *req;
1260 struct ldb_result *res;
1269 res = talloc_zero(mem_ctx, struct ldb_result);
1271 return LDB_ERR_OPERATIONS_ERROR;
1275 va_start(ap, exp_fmt);
1276 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
1281 return LDB_ERR_OPERATIONS_ERROR;
1285 ret = ldb_build_search_req(&req, ldb, mem_ctx,
1286 base?base:ldb_get_default_basedn(ldb),
1292 ldb_search_default_callback,
1295 if (ret != LDB_SUCCESS) goto done;
1297 ret = ldb_request(ldb, req);
1299 if (ret == LDB_SUCCESS) {
1300 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1304 if (ret != LDB_SUCCESS) {
1309 talloc_free(expression);
1317 add a record to the database. Will fail if a record with the given class
1318 and key already exists
1320 int ldb_add(struct ldb_context *ldb,
1321 const struct ldb_message *message)
1323 struct ldb_request *req;
1326 ret = ldb_msg_sanity_check(ldb, message);
1327 if (ret != LDB_SUCCESS) {
1331 ret = ldb_build_add_req(&req, ldb, ldb,
1335 ldb_op_default_callback,
1338 if (ret != LDB_SUCCESS) return ret;
1340 /* do request and autostart a transaction */
1341 ret = ldb_autotransaction_request(ldb, req);
1348 modify the specified attributes of a record
1350 int ldb_modify(struct ldb_context *ldb,
1351 const struct ldb_message *message)
1353 struct ldb_request *req;
1356 ret = ldb_msg_sanity_check(ldb, message);
1357 if (ret != LDB_SUCCESS) {
1361 if (message->num_elements == 0) {
1362 /* this needs also to be returned when the specified object
1363 doesn't exist. Therefore this test is located here. */
1364 ldb_asprintf_errstring(ldb, "LDB message has to have elements/attributes (%s)!",
1365 ldb_dn_get_linearized(message->dn));
1366 return LDB_ERR_UNWILLING_TO_PERFORM;
1369 ret = ldb_build_mod_req(&req, ldb, ldb,
1373 ldb_op_default_callback,
1376 if (ret != LDB_SUCCESS) return ret;
1378 /* do request and autostart a transaction */
1379 ret = ldb_autotransaction_request(ldb, req);
1387 delete a record from the database
1389 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1391 struct ldb_request *req;
1394 ret = ldb_build_del_req(&req, ldb, ldb,
1398 ldb_op_default_callback,
1401 if (ret != LDB_SUCCESS) return ret;
1403 /* do request and autostart a transaction */
1404 ret = ldb_autotransaction_request(ldb, req);
1411 rename a record in the database
1413 int ldb_rename(struct ldb_context *ldb,
1414 struct ldb_dn *olddn, struct ldb_dn *newdn)
1416 struct ldb_request *req;
1419 ret = ldb_build_rename_req(&req, ldb, ldb,
1424 ldb_op_default_callback,
1427 if (ret != LDB_SUCCESS) return ret;
1429 /* do request and autostart a transaction */
1430 ret = ldb_autotransaction_request(ldb, req);
1438 return the global sequence number
1440 int ldb_sequence_number(struct ldb_context *ldb,
1441 enum ldb_sequence_type type, uint64_t *seq_num)
1443 struct ldb_seqnum_request *seq;
1444 struct ldb_seqnum_result *seqr;
1445 struct ldb_result *res;
1446 TALLOC_CTX *tmp_ctx;
1451 tmp_ctx = talloc_zero(ldb, struct ldb_request);
1452 if (tmp_ctx == NULL) {
1453 ldb_set_errstring(ldb, "Out of Memory");
1454 return LDB_ERR_OPERATIONS_ERROR;
1456 seq = talloc_zero(tmp_ctx, struct ldb_seqnum_request);
1458 ldb_set_errstring(ldb, "Out of Memory");
1459 ret = LDB_ERR_OPERATIONS_ERROR;
1464 ret = ldb_extended(ldb, LDB_EXTENDED_SEQUENCE_NUMBER, seq, &res);
1465 if (ret != LDB_SUCCESS) {
1468 talloc_steal(tmp_ctx, res);
1470 if (strcmp(LDB_EXTENDED_SEQUENCE_NUMBER, res->extended->oid) != 0) {
1471 ldb_set_errstring(ldb, "Invalid OID in reply");
1472 ret = LDB_ERR_OPERATIONS_ERROR;
1475 seqr = talloc_get_type(res->extended->data,
1476 struct ldb_seqnum_result);
1477 *seq_num = seqr->seq_num;
1480 talloc_free(tmp_ctx);
1485 return extended error information
1487 const char *ldb_errstring(struct ldb_context *ldb)
1489 if (ldb->err_string) {
1490 return ldb->err_string;
1497 return a string explaining what a ldb error constant meancs
1499 const char *ldb_strerror(int ldb_err)
1504 case LDB_ERR_OPERATIONS_ERROR:
1505 return "Operations error";
1506 case LDB_ERR_PROTOCOL_ERROR:
1507 return "Protocol error";
1508 case LDB_ERR_TIME_LIMIT_EXCEEDED:
1509 return "Time limit exceeded";
1510 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1511 return "Size limit exceeded";
1512 case LDB_ERR_COMPARE_FALSE:
1513 return "Compare false";
1514 case LDB_ERR_COMPARE_TRUE:
1515 return "Compare true";
1516 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1517 return "Auth method not supported";
1518 case LDB_ERR_STRONG_AUTH_REQUIRED:
1519 return "Strong auth required";
1521 case LDB_ERR_REFERRAL:
1522 return "Referral error";
1523 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1524 return "Admin limit exceeded";
1525 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1526 return "Unsupported critical extension";
1527 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1528 return "Confidentiality required";
1529 case LDB_ERR_SASL_BIND_IN_PROGRESS:
1530 return "SASL bind in progress";
1531 case LDB_ERR_NO_SUCH_ATTRIBUTE:
1532 return "No such attribute";
1533 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1534 return "Undefined attribute type";
1535 case LDB_ERR_INAPPROPRIATE_MATCHING:
1536 return "Inappropriate matching";
1537 case LDB_ERR_CONSTRAINT_VIOLATION:
1538 return "Constraint violation";
1539 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1540 return "Attribute or value exists";
1541 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1542 return "Invalid attribute syntax";
1544 case LDB_ERR_NO_SUCH_OBJECT:
1545 return "No such object";
1546 case LDB_ERR_ALIAS_PROBLEM:
1547 return "Alias problem";
1548 case LDB_ERR_INVALID_DN_SYNTAX:
1549 return "Invalid DN syntax";
1551 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1552 return "Alias dereferencing problem";
1554 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1555 return "Inappropriate authentication";
1556 case LDB_ERR_INVALID_CREDENTIALS:
1557 return "Invalid credentials";
1558 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1559 return "insufficient access rights";
1562 case LDB_ERR_UNAVAILABLE:
1563 return "Unavailable";
1564 case LDB_ERR_UNWILLING_TO_PERFORM:
1565 return "Unwilling to perform";
1566 case LDB_ERR_LOOP_DETECT:
1567 return "Loop detect";
1569 case LDB_ERR_NAMING_VIOLATION:
1570 return "Naming violation";
1571 case LDB_ERR_OBJECT_CLASS_VIOLATION:
1572 return "Object class violation";
1573 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1574 return "Not allowed on non-leaf";
1575 case LDB_ERR_NOT_ALLOWED_ON_RDN:
1576 return "Not allowed on RDN";
1577 case LDB_ERR_ENTRY_ALREADY_EXISTS:
1578 return "Entry already exists";
1579 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1580 return "Object class mods prohibited";
1581 /* 70 RESERVED FOR CLDAP */
1582 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1583 return "Affects multiple DSAs";
1589 return "Unknown error";
1593 set backend specific opaque parameters
1595 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1597 struct ldb_opaque *o;
1599 /* allow updating an existing value */
1600 for (o=ldb->opaque;o;o=o->next) {
1601 if (strcmp(o->name, name) == 0) {
1607 o = talloc(ldb, struct ldb_opaque);
1610 return LDB_ERR_OTHER;
1612 o->next = ldb->opaque;
1620 get a previously set opaque value
1622 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1624 struct ldb_opaque *o;
1625 for (o=ldb->opaque;o;o=o->next) {
1626 if (strcmp(o->name, name) == 0) {
1633 int ldb_global_init(void)
1635 /* Provided for compatibility with some older versions of ldb */
1639 /* return the ldb flags */
1640 unsigned int ldb_get_flags(struct ldb_context *ldb)
1645 /* set the ldb flags */
1646 void ldb_set_flags(struct ldb_context *ldb, unsigned flags)