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 for (i=0; req->op.search.attrs && req->op.search.attrs[i]; i++) {
646 ldb_debug(ldb, LDB_DEBUG_TRACE, " attr: %s", req->op.search.attrs[i]);
650 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: DELETE");
651 ldb_debug(ldb, LDB_DEBUG_TRACE, " dn: %s",
652 ldb_dn_get_linearized(req->op.del.dn));
655 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: RENAME");
656 ldb_debug(ldb, LDB_DEBUG_TRACE, " olddn: %s",
657 ldb_dn_get_linearized(req->op.rename.olddn));
658 ldb_debug(ldb, LDB_DEBUG_TRACE, " newdn: %s",
659 ldb_dn_get_linearized(req->op.rename.newdn));
662 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: EXTENDED");
663 ldb_debug(ldb, LDB_DEBUG_TRACE, " oid: %s", req->op.extended.oid);
664 ldb_debug(ldb, LDB_DEBUG_TRACE, " data: %s", req->op.extended.data?"yes":"no");
667 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: ADD");
668 ldb_debug(req->handle->ldb, LDB_DEBUG_TRACE, "%s",
669 ldb_ldif_message_string(req->handle->ldb, tmp_ctx,
670 LDB_CHANGETYPE_ADD, req->op.add.message));
673 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: MODIFY");
674 ldb_debug(req->handle->ldb, LDB_DEBUG_TRACE, "%s",
675 ldb_ldif_message_string(req->handle->ldb, tmp_ctx,
676 LDB_CHANGETYPE_ADD, req->op.mod.message));
678 case LDB_REQ_REGISTER_CONTROL:
679 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: REGISTER_CONTROL");
680 ldb_debug(req->handle->ldb, LDB_DEBUG_TRACE, "%s",
681 req->op.reg_control.oid);
683 case LDB_REQ_REGISTER_PARTITION:
684 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: REGISTER_PARTITION");
685 ldb_debug(req->handle->ldb, LDB_DEBUG_TRACE, "%s",
686 ldb_dn_get_linearized(req->op.reg_partition.dn));
689 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: UNKNOWN(%u)",
694 for (i=0; req->controls && req->controls[i]; i++) {
695 ldb_debug(ldb, LDB_DEBUG_TRACE, " control: %s crit:%u data:%s",
696 req->controls[i]->oid,
697 req->controls[i]->critical,
698 req->controls[i]->data?"yes":"no");
701 talloc_free(tmp_ctx);
707 NOTE: the request must be a talloc context.
708 returns LDB_ERR_* on errors.
710 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
712 struct ldb_module *module;
715 if (req->callback == NULL) {
716 ldb_set_errstring(ldb, "Requests MUST define callbacks");
717 return LDB_ERR_UNWILLING_TO_PERFORM;
720 ldb_reset_err_string(ldb);
722 if (ldb->flags & LDB_FLG_ENABLE_TRACING) {
723 ldb_trace_request(ldb, req);
726 /* call the first module in the chain */
727 switch (req->operation) {
729 FIRST_OP(ldb, search);
730 ret = module->ops->search(module, req);
734 ret = module->ops->add(module, req);
737 FIRST_OP(ldb, modify);
738 ret = module->ops->modify(module, req);
742 ret = module->ops->del(module, req);
745 FIRST_OP(ldb, rename);
746 ret = module->ops->rename(module, req);
749 FIRST_OP(ldb, extended);
750 ret = module->ops->extended(module, req);
753 FIRST_OP(ldb, request);
754 ret = module->ops->request(module, req);
761 int ldb_request_done(struct ldb_request *req, int status)
763 req->handle->state = LDB_ASYNC_DONE;
764 req->handle->status = status;
769 search the database given a LDAP-like search expression
771 returns an LDB error code
773 Use talloc_free to free the ldb_message returned in 'res', if successful
776 int ldb_search_default_callback(struct ldb_request *req,
777 struct ldb_reply *ares)
779 struct ldb_result *res;
782 res = talloc_get_type(req->context, struct ldb_result);
785 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
787 if (ares->error != LDB_SUCCESS) {
788 return ldb_request_done(req, ares->error);
791 switch (ares->type) {
792 case LDB_REPLY_ENTRY:
793 res->msgs = talloc_realloc(res, res->msgs,
794 struct ldb_message *, res->count + 2);
796 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
799 res->msgs[res->count + 1] = NULL;
801 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
805 case LDB_REPLY_REFERRAL:
807 for (n = 0; res->refs[n]; n++) /*noop*/ ;
812 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
814 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
817 res->refs[n] = talloc_move(res->refs, &ares->referral);
818 res->refs[n + 1] = NULL;
822 /* TODO: we should really support controls on entries
823 * and referrals too! */
824 res->controls = talloc_move(res, &ares->controls);
826 /* this is the last message, and means the request is done */
827 /* we have to signal and eventual ldb_wait() waiting that the
828 * async request operation was completed */
830 return ldb_request_done(req, LDB_SUCCESS);
838 int ldb_op_default_callback(struct ldb_request *req, struct ldb_reply *ares)
843 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
846 if (ares->error != LDB_SUCCESS) {
849 return ldb_request_done(req, ret);
852 if (ares->type != LDB_REPLY_DONE) {
854 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
855 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
859 return ldb_request_done(req, LDB_SUCCESS);
862 int ldb_build_search_req_ex(struct ldb_request **ret_req,
863 struct ldb_context *ldb,
866 enum ldb_scope scope,
867 struct ldb_parse_tree *tree,
868 const char * const *attrs,
869 struct ldb_control **controls,
871 ldb_request_callback_t callback,
872 struct ldb_request *parent)
874 struct ldb_request *req;
878 req = talloc(mem_ctx, struct ldb_request);
881 return LDB_ERR_OPERATIONS_ERROR;
884 req->operation = LDB_SEARCH;
886 req->op.search.base = ldb_dn_new(req, ldb, NULL);
888 req->op.search.base = base;
890 req->op.search.scope = scope;
892 req->op.search.tree = tree;
893 if (req->op.search.tree == NULL) {
894 ldb_set_errstring(ldb, "'tree' can't be NULL");
896 return LDB_ERR_OPERATIONS_ERROR;
899 req->op.search.attrs = attrs;
900 req->controls = controls;
901 req->context = context;
902 req->callback = callback;
904 ldb_set_timeout_from_prev_req(ldb, parent, req);
906 req->handle = ldb_handle_new(req, ldb);
907 if (req->handle == NULL) {
909 return LDB_ERR_OPERATIONS_ERROR;
916 int ldb_build_search_req(struct ldb_request **ret_req,
917 struct ldb_context *ldb,
920 enum ldb_scope scope,
921 const char *expression,
922 const char * const *attrs,
923 struct ldb_control **controls,
925 ldb_request_callback_t callback,
926 struct ldb_request *parent)
928 struct ldb_parse_tree *tree;
931 tree = ldb_parse_tree(mem_ctx, expression);
933 ldb_set_errstring(ldb, "Unable to parse search expression");
934 return LDB_ERR_OPERATIONS_ERROR;
937 ret = ldb_build_search_req_ex(ret_req, ldb, mem_ctx, base,
938 scope, tree, attrs, controls,
939 context, callback, parent);
940 if (ret == LDB_SUCCESS) {
941 talloc_steal(*ret_req, tree);
946 int ldb_build_add_req(struct ldb_request **ret_req,
947 struct ldb_context *ldb,
949 const struct ldb_message *message,
950 struct ldb_control **controls,
952 ldb_request_callback_t callback,
953 struct ldb_request *parent)
955 struct ldb_request *req;
959 req = talloc(mem_ctx, struct ldb_request);
961 ldb_set_errstring(ldb, "Out of Memory");
962 return LDB_ERR_OPERATIONS_ERROR;
965 req->operation = LDB_ADD;
966 req->op.add.message = message;
967 req->controls = controls;
968 req->context = context;
969 req->callback = callback;
971 ldb_set_timeout_from_prev_req(ldb, parent, req);
973 req->handle = ldb_handle_new(req, ldb);
974 if (req->handle == NULL) {
976 return LDB_ERR_OPERATIONS_ERROR;
984 int ldb_build_mod_req(struct ldb_request **ret_req,
985 struct ldb_context *ldb,
987 const struct ldb_message *message,
988 struct ldb_control **controls,
990 ldb_request_callback_t callback,
991 struct ldb_request *parent)
993 struct ldb_request *req;
997 req = talloc(mem_ctx, struct ldb_request);
999 ldb_set_errstring(ldb, "Out of Memory");
1000 return LDB_ERR_OPERATIONS_ERROR;
1003 req->operation = LDB_MODIFY;
1004 req->op.mod.message = message;
1005 req->controls = controls;
1006 req->context = context;
1007 req->callback = callback;
1009 ldb_set_timeout_from_prev_req(ldb, parent, req);
1011 req->handle = ldb_handle_new(req, ldb);
1012 if (req->handle == NULL) {
1014 return LDB_ERR_OPERATIONS_ERROR;
1022 int ldb_build_del_req(struct ldb_request **ret_req,
1023 struct ldb_context *ldb,
1026 struct ldb_control **controls,
1028 ldb_request_callback_t callback,
1029 struct ldb_request *parent)
1031 struct ldb_request *req;
1035 req = talloc(mem_ctx, struct ldb_request);
1037 ldb_set_errstring(ldb, "Out of Memory");
1038 return LDB_ERR_OPERATIONS_ERROR;
1041 req->operation = LDB_DELETE;
1042 req->op.del.dn = dn;
1043 req->controls = controls;
1044 req->context = context;
1045 req->callback = callback;
1047 ldb_set_timeout_from_prev_req(ldb, parent, req);
1049 req->handle = ldb_handle_new(req, ldb);
1050 if (req->handle == NULL) {
1052 return LDB_ERR_OPERATIONS_ERROR;
1060 int ldb_build_rename_req(struct ldb_request **ret_req,
1061 struct ldb_context *ldb,
1063 struct ldb_dn *olddn,
1064 struct ldb_dn *newdn,
1065 struct ldb_control **controls,
1067 ldb_request_callback_t callback,
1068 struct ldb_request *parent)
1070 struct ldb_request *req;
1074 req = talloc(mem_ctx, struct ldb_request);
1076 ldb_set_errstring(ldb, "Out of Memory");
1077 return LDB_ERR_OPERATIONS_ERROR;
1080 req->operation = LDB_RENAME;
1081 req->op.rename.olddn = olddn;
1082 req->op.rename.newdn = newdn;
1083 req->controls = controls;
1084 req->context = context;
1085 req->callback = callback;
1087 ldb_set_timeout_from_prev_req(ldb, parent, req);
1089 req->handle = ldb_handle_new(req, ldb);
1090 if (req->handle == NULL) {
1092 return LDB_ERR_OPERATIONS_ERROR;
1100 int ldb_extended_default_callback(struct ldb_request *req,
1101 struct ldb_reply *ares)
1103 struct ldb_result *res;
1105 res = talloc_get_type(req->context, struct ldb_result);
1108 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1110 if (ares->error != LDB_SUCCESS) {
1111 return ldb_request_done(req, ares->error);
1114 if (ares->type == LDB_REPLY_DONE) {
1116 /* TODO: we should really support controls on entries and referrals too! */
1117 res->extended = talloc_move(res, &ares->response);
1118 res->controls = talloc_move(res, &ares->controls);
1121 return ldb_request_done(req, LDB_SUCCESS);
1125 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
1126 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1129 int ldb_build_extended_req(struct ldb_request **ret_req,
1130 struct ldb_context *ldb,
1134 struct ldb_control **controls,
1136 ldb_request_callback_t callback,
1137 struct ldb_request *parent)
1139 struct ldb_request *req;
1143 req = talloc(mem_ctx, struct ldb_request);
1145 ldb_set_errstring(ldb, "Out of Memory");
1146 return LDB_ERR_OPERATIONS_ERROR;
1149 req->operation = LDB_EXTENDED;
1150 req->op.extended.oid = oid;
1151 req->op.extended.data = data;
1152 req->controls = controls;
1153 req->context = context;
1154 req->callback = callback;
1156 ldb_set_timeout_from_prev_req(ldb, parent, req);
1158 req->handle = ldb_handle_new(req, ldb);
1159 if (req->handle == NULL) {
1161 return LDB_ERR_OPERATIONS_ERROR;
1169 int ldb_extended(struct ldb_context *ldb,
1172 struct ldb_result **_res)
1174 struct ldb_request *req;
1176 struct ldb_result *res;
1180 res = talloc_zero(ldb, struct ldb_result);
1182 return LDB_ERR_OPERATIONS_ERROR;
1185 ret = ldb_build_extended_req(&req, ldb, ldb,
1187 res, ldb_extended_default_callback,
1189 if (ret != LDB_SUCCESS) goto done;
1191 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1193 ret = ldb_request(ldb, req);
1195 if (ret == LDB_SUCCESS) {
1196 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1202 if (ret != LDB_SUCCESS) {
1211 note that ldb_search() will automatically replace a NULL 'base' value
1212 with the defaultNamingContext from the rootDSE if available.
1214 int ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1215 struct ldb_result **result, struct ldb_dn *base,
1216 enum ldb_scope scope, const char * const *attrs,
1217 const char *exp_fmt, ...)
1219 struct ldb_request *req;
1220 struct ldb_result *res;
1229 res = talloc_zero(mem_ctx, struct ldb_result);
1231 return LDB_ERR_OPERATIONS_ERROR;
1235 va_start(ap, exp_fmt);
1236 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
1241 return LDB_ERR_OPERATIONS_ERROR;
1245 ret = ldb_build_search_req(&req, ldb, mem_ctx,
1246 base?base:ldb_get_default_basedn(ldb),
1252 ldb_search_default_callback,
1255 if (ret != LDB_SUCCESS) goto done;
1257 ret = ldb_request(ldb, req);
1259 if (ret == LDB_SUCCESS) {
1260 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1264 if (ret != LDB_SUCCESS) {
1269 talloc_free(expression);
1277 add a record to the database. Will fail if a record with the given class
1278 and key already exists
1280 int ldb_add(struct ldb_context *ldb,
1281 const struct ldb_message *message)
1283 struct ldb_request *req;
1286 ret = ldb_msg_sanity_check(ldb, message);
1287 if (ret != LDB_SUCCESS) {
1291 ret = ldb_build_add_req(&req, ldb, ldb,
1295 ldb_op_default_callback,
1298 if (ret != LDB_SUCCESS) return ret;
1300 /* do request and autostart a transaction */
1301 ret = ldb_autotransaction_request(ldb, req);
1308 modify the specified attributes of a record
1310 int ldb_modify(struct ldb_context *ldb,
1311 const struct ldb_message *message)
1313 struct ldb_request *req;
1316 ret = ldb_msg_sanity_check(ldb, message);
1317 if (ret != LDB_SUCCESS) {
1321 ret = ldb_build_mod_req(&req, ldb, ldb,
1325 ldb_op_default_callback,
1328 if (ret != LDB_SUCCESS) return ret;
1330 /* do request and autostart a transaction */
1331 ret = ldb_autotransaction_request(ldb, req);
1339 delete a record from the database
1341 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1343 struct ldb_request *req;
1346 ret = ldb_build_del_req(&req, ldb, ldb,
1350 ldb_op_default_callback,
1353 if (ret != LDB_SUCCESS) return ret;
1355 /* do request and autostart a transaction */
1356 ret = ldb_autotransaction_request(ldb, req);
1363 rename a record in the database
1365 int ldb_rename(struct ldb_context *ldb,
1366 struct ldb_dn *olddn, struct ldb_dn *newdn)
1368 struct ldb_request *req;
1371 ret = ldb_build_rename_req(&req, ldb, ldb,
1376 ldb_op_default_callback,
1379 if (ret != LDB_SUCCESS) return ret;
1381 /* do request and autostart a transaction */
1382 ret = ldb_autotransaction_request(ldb, req);
1390 return the global sequence number
1392 int ldb_sequence_number(struct ldb_context *ldb,
1393 enum ldb_sequence_type type, uint64_t *seq_num)
1395 struct ldb_seqnum_request *seq;
1396 struct ldb_seqnum_result *seqr;
1397 struct ldb_result *res;
1398 TALLOC_CTX *tmp_ctx;
1403 tmp_ctx = talloc_zero(ldb, struct ldb_request);
1404 if (tmp_ctx == NULL) {
1405 ldb_set_errstring(ldb, "Out of Memory");
1406 return LDB_ERR_OPERATIONS_ERROR;
1408 seq = talloc_zero(tmp_ctx, struct ldb_seqnum_request);
1410 ldb_set_errstring(ldb, "Out of Memory");
1411 ret = LDB_ERR_OPERATIONS_ERROR;
1416 ret = ldb_extended(ldb, LDB_EXTENDED_SEQUENCE_NUMBER, seq, &res);
1417 if (ret != LDB_SUCCESS) {
1420 talloc_steal(tmp_ctx, res);
1422 if (strcmp(LDB_EXTENDED_SEQUENCE_NUMBER, res->extended->oid) != 0) {
1423 ldb_set_errstring(ldb, "Invalid OID in reply");
1424 ret = LDB_ERR_OPERATIONS_ERROR;
1427 seqr = talloc_get_type(res->extended->data,
1428 struct ldb_seqnum_result);
1429 *seq_num = seqr->seq_num;
1432 talloc_free(tmp_ctx);
1437 return extended error information
1439 const char *ldb_errstring(struct ldb_context *ldb)
1441 if (ldb->err_string) {
1442 return ldb->err_string;
1449 return a string explaining what a ldb error constant meancs
1451 const char *ldb_strerror(int ldb_err)
1456 case LDB_ERR_OPERATIONS_ERROR:
1457 return "Operations error";
1458 case LDB_ERR_PROTOCOL_ERROR:
1459 return "Protocol error";
1460 case LDB_ERR_TIME_LIMIT_EXCEEDED:
1461 return "Time limit exceeded";
1462 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1463 return "Size limit exceeded";
1464 case LDB_ERR_COMPARE_FALSE:
1465 return "Compare false";
1466 case LDB_ERR_COMPARE_TRUE:
1467 return "Compare true";
1468 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1469 return "Auth method not supported";
1470 case LDB_ERR_STRONG_AUTH_REQUIRED:
1471 return "Strong auth required";
1473 case LDB_ERR_REFERRAL:
1474 return "Referral error";
1475 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1476 return "Admin limit exceeded";
1477 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1478 return "Unsupported critical extension";
1479 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1480 return "Confidentiality required";
1481 case LDB_ERR_SASL_BIND_IN_PROGRESS:
1482 return "SASL bind in progress";
1483 case LDB_ERR_NO_SUCH_ATTRIBUTE:
1484 return "No such attribute";
1485 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1486 return "Undefined attribute type";
1487 case LDB_ERR_INAPPROPRIATE_MATCHING:
1488 return "Inappropriate matching";
1489 case LDB_ERR_CONSTRAINT_VIOLATION:
1490 return "Constraint violation";
1491 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1492 return "Attribute or value exists";
1493 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1494 return "Invalid attribute syntax";
1496 case LDB_ERR_NO_SUCH_OBJECT:
1497 return "No such object";
1498 case LDB_ERR_ALIAS_PROBLEM:
1499 return "Alias problem";
1500 case LDB_ERR_INVALID_DN_SYNTAX:
1501 return "Invalid DN syntax";
1503 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1504 return "Alias dereferencing problem";
1506 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1507 return "Inappropriate authentication";
1508 case LDB_ERR_INVALID_CREDENTIALS:
1509 return "Invalid credentials";
1510 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1511 return "insufficient access rights";
1514 case LDB_ERR_UNAVAILABLE:
1515 return "Unavailable";
1516 case LDB_ERR_UNWILLING_TO_PERFORM:
1517 return "Unwilling to perform";
1518 case LDB_ERR_LOOP_DETECT:
1519 return "Loop detect";
1521 case LDB_ERR_NAMING_VIOLATION:
1522 return "Naming violation";
1523 case LDB_ERR_OBJECT_CLASS_VIOLATION:
1524 return "Object class violation";
1525 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1526 return "Not allowed on non-leaf";
1527 case LDB_ERR_NOT_ALLOWED_ON_RDN:
1528 return "Not allowed on RDN";
1529 case LDB_ERR_ENTRY_ALREADY_EXISTS:
1530 return "Entry already exists";
1531 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1532 return "Object class mods prohibited";
1533 /* 70 RESERVED FOR CLDAP */
1534 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1535 return "Affects multiple DSAs";
1541 return "Unknown error";
1545 set backend specific opaque parameters
1547 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1549 struct ldb_opaque *o;
1551 /* allow updating an existing value */
1552 for (o=ldb->opaque;o;o=o->next) {
1553 if (strcmp(o->name, name) == 0) {
1559 o = talloc(ldb, struct ldb_opaque);
1562 return LDB_ERR_OTHER;
1564 o->next = ldb->opaque;
1572 get a previously set opaque value
1574 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1576 struct ldb_opaque *o;
1577 for (o=ldb->opaque;o;o=o->next) {
1578 if (strcmp(o->name, name) == 0) {
1585 int ldb_global_init(void)
1587 /* Provided for compatibility with some older versions of ldb */
1591 /* return the ldb flags */
1592 unsigned int ldb_get_flags(struct ldb_context *ldb)
1597 /* set the ldb flags */
1598 void ldb_set_flags(struct ldb_context *ldb, unsigned flags)