4 Copyright (C) Andrew Tridgell 2004
5 Copyright (C) Simo Sorce 2005-2008
7 ** NOTE! The following LGPL license applies to the ldb
8 ** library. This does NOT imply that all of Samba is released
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 3 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 * Component: ldb core API
30 * Description: core API routines interfacing to ldb backends
32 * Author: Andrew Tridgell
35 #define TEVENT_DEPRECATED 1
36 #include "ldb_private.h"
38 static int ldb_context_destructor(void *ptr)
40 struct ldb_context *ldb = talloc_get_type(ptr, struct ldb_context);
42 if (ldb->transaction_active) {
43 ldb_debug(ldb, LDB_DEBUG_FATAL,
44 "A transaction is still active in ldb context [%p]",
52 this is used to catch debug messages from events
54 static void ldb_tevent_debug(void *context, enum tevent_debug_level level,
55 const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
57 static void ldb_tevent_debug(void *context, enum tevent_debug_level level,
58 const char *fmt, va_list ap)
60 struct ldb_context *ldb = talloc_get_type(context, struct ldb_context);
61 enum ldb_debug_level ldb_level = LDB_DEBUG_FATAL;
65 case TEVENT_DEBUG_FATAL:
66 ldb_level = LDB_DEBUG_FATAL;
68 case TEVENT_DEBUG_ERROR:
69 ldb_level = LDB_DEBUG_ERROR;
71 case TEVENT_DEBUG_WARNING:
72 ldb_level = LDB_DEBUG_WARNING;
74 case TEVENT_DEBUG_TRACE:
75 ldb_level = LDB_DEBUG_TRACE;
79 vasprintf(&s, fmt, ap);
81 ldb_debug(ldb, ldb_level, "tevent: %s", s);
86 initialise a ldb context
87 The mem_ctx is required
88 The event_ctx is required
90 struct ldb_context *ldb_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev_ctx)
92 struct ldb_context *ldb;
95 ldb = talloc_zero(mem_ctx, struct ldb_context);
96 /* FIXME: Hack a new event context so that CMD line utilities work
97 * until we have them all converted */
99 ev_ctx = tevent_context_init(talloc_autofree_context());
100 tevent_set_debug(ev_ctx, ldb_tevent_debug, ldb);
101 tevent_loop_allow_nesting(ev_ctx);
104 ret = ldb_setup_wellknown_attributes(ldb);
110 ldb_set_utf8_default(ldb);
111 ldb_set_create_perms(ldb, 0666);
112 ldb_set_modules_dir(ldb, LDB_MODULESDIR);
113 ldb_set_event_context(ldb, ev_ctx);
115 /* TODO: get timeout from options if available there */
116 ldb->default_timeout = 300; /* set default to 5 minutes */
118 talloc_set_destructor((TALLOC_CTX *)ldb, ldb_context_destructor);
124 try to autodetect a basedn if none specified. This fixes one of my
125 pet hates about ldapsearch, which is that you have to get a long,
126 complex basedn right to make any use of it.
128 void ldb_set_default_dns(struct ldb_context *ldb)
132 struct ldb_result *res;
133 struct ldb_dn *tmp_dn=NULL;
134 static const char *attrs[] = {
135 "rootDomainNamingContext",
136 "configurationNamingContext",
137 "schemaNamingContext",
138 "defaultNamingContext",
142 tmp_ctx = talloc_new(ldb);
143 ret = ldb_search(ldb, tmp_ctx, &res, ldb_dn_new(tmp_ctx, ldb, NULL),
144 LDB_SCOPE_BASE, attrs, "(objectClass=*)");
145 if (ret != LDB_SUCCESS) {
146 talloc_free(tmp_ctx);
150 if (res->count != 1) {
151 talloc_free(tmp_ctx);
155 if (!ldb_get_opaque(ldb, "rootDomainNamingContext")) {
156 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
157 "rootDomainNamingContext");
158 ldb_set_opaque(ldb, "rootDomainNamingContext", tmp_dn);
161 if (!ldb_get_opaque(ldb, "configurationNamingContext")) {
162 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
163 "configurationNamingContext");
164 ldb_set_opaque(ldb, "configurationNamingContext", tmp_dn);
167 if (!ldb_get_opaque(ldb, "schemaNamingContext")) {
168 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
169 "schemaNamingContext");
170 ldb_set_opaque(ldb, "schemaNamingContext", tmp_dn);
173 if (!ldb_get_opaque(ldb, "defaultNamingContext")) {
174 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
175 "defaultNamingContext");
176 ldb_set_opaque(ldb, "defaultNamingContext", tmp_dn);
179 talloc_free(tmp_ctx);
182 struct ldb_dn *ldb_get_root_basedn(struct ldb_context *ldb)
184 void *opaque = ldb_get_opaque(ldb, "rootDomainNamingContext");
185 return talloc_get_type(opaque, struct ldb_dn);
188 struct ldb_dn *ldb_get_config_basedn(struct ldb_context *ldb)
190 void *opaque = ldb_get_opaque(ldb, "configurationNamingContext");
191 return talloc_get_type(opaque, struct ldb_dn);
194 struct ldb_dn *ldb_get_schema_basedn(struct ldb_context *ldb)
196 void *opaque = ldb_get_opaque(ldb, "schemaNamingContext");
197 return talloc_get_type(opaque, struct ldb_dn);
200 struct ldb_dn *ldb_get_default_basedn(struct ldb_context *ldb)
202 void *opaque = ldb_get_opaque(ldb, "defaultNamingContext");
203 return talloc_get_type(opaque, struct ldb_dn);
207 connect to a database. The URL can either be one of the following forms
211 flags is made up of LDB_FLG_*
213 the options are passed uninterpreted to the backend, and are
216 int ldb_connect(struct ldb_context *ldb, const char *url,
217 unsigned int flags, const char *options[])
221 /* We seem to need to do this here, or else some utilities don't
222 * get ldb backends */
226 url2 = talloc_strdup(ldb, url);
229 return LDB_ERR_OPERATIONS_ERROR;
231 ret = ldb_set_opaque(ldb, "ldb_url", talloc_strdup(ldb, url2));
232 if (ret != LDB_SUCCESS) {
236 ret = ldb_connect_backend(ldb, url, options, &ldb->modules);
237 if (ret != LDB_SUCCESS) {
241 if (ldb_load_modules(ldb, options) != LDB_SUCCESS) {
242 ldb_debug(ldb, LDB_DEBUG_FATAL,
243 "Unable to load modules for %s: %s\n",
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(ldb, op) do { \
286 module = ldb->modules; \
287 while (module && module->ops->op == NULL) module = module->next; \
288 if (module == NULL) { \
289 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
290 return LDB_ERR_OPERATIONS_ERROR; \
297 int ldb_transaction_start(struct ldb_context *ldb)
299 struct ldb_module *module;
302 ldb_debug(ldb, LDB_DEBUG_TRACE,
303 "start ldb transaction (nesting: %d)",
304 ldb->transaction_active);
306 /* explicit transaction active, count nested requests */
307 if (ldb->transaction_active) {
308 ldb->transaction_active++;
312 /* start a new transaction */
313 ldb->transaction_active++;
315 FIRST_OP(ldb, start_transaction);
317 ldb_reset_err_string(ldb);
319 status = module->ops->start_transaction(module);
320 if (status != LDB_SUCCESS) {
321 if (ldb->err_string == NULL) {
322 /* no error string was setup by the backend */
323 ldb_asprintf_errstring(ldb,
324 "ldb transaction start: %s (%d)",
325 ldb_strerror(status),
335 int ldb_transaction_commit(struct ldb_context *ldb)
337 struct ldb_module *module;
340 ldb->transaction_active--;
342 ldb_debug(ldb, LDB_DEBUG_TRACE,
343 "commit ldb transaction (nesting: %d)",
344 ldb->transaction_active);
346 /* commit only when all nested transactions are complete */
347 if (ldb->transaction_active > 0) {
351 if (ldb->transaction_active < 0) {
352 ldb_debug(ldb, LDB_DEBUG_FATAL,
353 "commit called but no ldb transactions are active!");
354 ldb->transaction_active = 0;
355 return LDB_ERR_OPERATIONS_ERROR;
358 FIRST_OP(ldb, end_transaction);
360 ldb_reset_err_string(ldb);
362 status = module->ops->end_transaction(module);
363 if (status != LDB_SUCCESS) {
364 if (ldb->err_string == NULL) {
365 /* no error string was setup by the backend */
366 ldb_asprintf_errstring(ldb,
367 "ldb transaction commit: %s (%d)",
368 ldb_strerror(status),
378 int ldb_transaction_cancel(struct ldb_context *ldb)
380 struct ldb_module *module;
383 ldb->transaction_active--;
385 ldb_debug(ldb, LDB_DEBUG_TRACE,
386 "cancel ldb transaction (nesting: %d)",
387 ldb->transaction_active);
389 /* really cancel only if all nested transactions are complete */
390 if (ldb->transaction_active > 0) {
394 if (ldb->transaction_active < 0) {
395 ldb_debug(ldb, LDB_DEBUG_FATAL,
396 "commit called but no ldb transactions are active!");
397 ldb->transaction_active = 0;
398 return LDB_ERR_OPERATIONS_ERROR;
401 FIRST_OP(ldb, del_transaction);
403 status = module->ops->del_transaction(module);
404 if (status != LDB_SUCCESS) {
405 if (ldb->err_string == NULL) {
406 /* no error string was setup by the backend */
407 ldb_asprintf_errstring(ldb,
408 "ldb transaction cancel: %s (%d)",
409 ldb_strerror(status),
416 /* autostarts a transacion if none active */
417 static int ldb_autotransaction_request(struct ldb_context *ldb,
418 struct ldb_request *req)
422 ret = ldb_transaction_start(ldb);
423 if (ret != LDB_SUCCESS) {
427 ret = ldb_request(ldb, req);
428 if (ret == LDB_SUCCESS) {
429 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
432 if (ret == LDB_SUCCESS) {
433 return ldb_transaction_commit(ldb);
435 ldb_transaction_cancel(ldb);
437 if (ldb->err_string == NULL) {
438 /* no error string was setup by the backend */
439 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
445 int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
447 struct tevent_context *ev;
451 return LDB_ERR_UNAVAILABLE;
454 if (handle->state == LDB_ASYNC_DONE) {
455 return handle->status;
458 ev = ldb_get_event_context(handle->ldb);
460 return LDB_ERR_OPERATIONS_ERROR;
465 ret = tevent_loop_once(ev);
467 return LDB_ERR_OPERATIONS_ERROR;
469 if (handle->state == LDB_ASYNC_DONE ||
470 handle->status != LDB_SUCCESS) {
471 return handle->status;
476 while (handle->state != LDB_ASYNC_DONE) {
477 ret = tevent_loop_once(ev);
479 return LDB_ERR_OPERATIONS_ERROR;
481 if (handle->status != LDB_SUCCESS) {
482 return handle->status;
485 return handle->status;
491 /* set the specified timeout or, if timeout is 0 set the default timeout */
492 int ldb_set_timeout(struct ldb_context *ldb,
493 struct ldb_request *req,
496 if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
499 req->timeout = timeout;
501 req->timeout = ldb->default_timeout;
503 req->starttime = time(NULL);
508 /* calculates the new timeout based on the previous starttime and timeout */
509 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb,
510 struct ldb_request *oldreq,
511 struct ldb_request *newreq)
513 if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
515 if (oldreq == NULL) {
516 return ldb_set_timeout(ldb, newreq, 0);
519 newreq->starttime = oldreq->starttime;
520 newreq->timeout = oldreq->timeout;
527 set the permissions for new files to be passed to open() in
528 backends that use local files
530 void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
532 ldb->create_perms = perms;
535 unsigned int ldb_get_create_perms(struct ldb_context *ldb)
537 return ldb->create_perms;
540 void ldb_set_event_context(struct ldb_context *ldb, struct tevent_context *ev)
545 struct tevent_context * ldb_get_event_context(struct ldb_context *ldb)
550 void ldb_request_set_state(struct ldb_request *req, int state)
552 req->handle->state = state;
555 int ldb_request_get_status(struct ldb_request *req)
557 return req->handle->status;
562 NOTE: the request must be a talloc context.
563 returns LDB_ERR_* on errors.
565 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
567 struct ldb_module *module;
570 if (req->callback == NULL) {
571 ldb_set_errstring(ldb, "Requests MUST define callbacks");
572 return LDB_ERR_UNWILLING_TO_PERFORM;
575 ldb_reset_err_string(ldb);
577 /* call the first module in the chain */
578 switch (req->operation) {
580 FIRST_OP(ldb, search);
581 ret = module->ops->search(module, req);
585 ret = module->ops->add(module, req);
588 FIRST_OP(ldb, modify);
589 ret = module->ops->modify(module, req);
593 ret = module->ops->del(module, req);
596 FIRST_OP(ldb, rename);
597 ret = module->ops->rename(module, req);
600 FIRST_OP(ldb, extended);
601 ret = module->ops->extended(module, req);
604 FIRST_OP(ldb, request);
605 ret = module->ops->request(module, req);
612 int ldb_request_done(struct ldb_request *req, int status)
614 req->handle->state = LDB_ASYNC_DONE;
615 req->handle->status = status;
620 search the database given a LDAP-like search expression
622 returns an LDB error code
624 Use talloc_free to free the ldb_message returned in 'res', if successful
627 int ldb_search_default_callback(struct ldb_request *req,
628 struct ldb_reply *ares)
630 struct ldb_result *res;
633 res = talloc_get_type(req->context, struct ldb_result);
636 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
638 if (ares->error != LDB_SUCCESS) {
639 return ldb_request_done(req, ares->error);
642 switch (ares->type) {
643 case LDB_REPLY_ENTRY:
644 res->msgs = talloc_realloc(res, res->msgs,
645 struct ldb_message *, res->count + 2);
647 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
650 res->msgs[res->count + 1] = NULL;
652 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
656 case LDB_REPLY_REFERRAL:
658 for (n = 0; res->refs[n]; n++) /*noop*/ ;
663 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
665 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
668 res->refs[n] = talloc_move(res->refs, &ares->referral);
669 res->refs[n + 1] = NULL;
673 /* TODO: we should really support controls on entries
674 * and referrals too! */
675 res->controls = talloc_move(res, &ares->controls);
677 /* this is the last message, and means the request is done */
678 /* we have to signal and eventual ldb_wait() waiting that the
679 * async request operation was completed */
680 return ldb_request_done(req, LDB_SUCCESS);
687 int ldb_op_default_callback(struct ldb_request *req, struct ldb_reply *ares)
692 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
695 if (ares->error != LDB_SUCCESS) {
698 return ldb_request_done(req, ret);
701 if (ares->type != LDB_REPLY_DONE) {
703 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
704 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
708 return ldb_request_done(req, LDB_SUCCESS);
711 int ldb_build_search_req_ex(struct ldb_request **ret_req,
712 struct ldb_context *ldb,
715 enum ldb_scope scope,
716 struct ldb_parse_tree *tree,
717 const char * const *attrs,
718 struct ldb_control **controls,
720 ldb_request_callback_t callback,
721 struct ldb_request *parent)
723 struct ldb_request *req;
727 req = talloc(mem_ctx, struct ldb_request);
730 return LDB_ERR_OPERATIONS_ERROR;
733 req->operation = LDB_SEARCH;
735 req->op.search.base = ldb_dn_new(req, ldb, NULL);
737 req->op.search.base = base;
739 req->op.search.scope = scope;
741 req->op.search.tree = tree;
742 if (req->op.search.tree == NULL) {
743 ldb_set_errstring(ldb, "'tree' can't be NULL");
745 return LDB_ERR_OPERATIONS_ERROR;
748 req->op.search.attrs = attrs;
749 req->controls = controls;
750 req->context = context;
751 req->callback = callback;
753 ldb_set_timeout_from_prev_req(ldb, parent, req);
755 req->handle = ldb_handle_new(req, ldb);
756 if (req->handle == NULL) {
758 return LDB_ERR_OPERATIONS_ERROR;
765 int ldb_build_search_req(struct ldb_request **ret_req,
766 struct ldb_context *ldb,
769 enum ldb_scope scope,
770 const char *expression,
771 const char * const *attrs,
772 struct ldb_control **controls,
774 ldb_request_callback_t callback,
775 struct ldb_request *parent)
777 struct ldb_parse_tree *tree;
780 tree = ldb_parse_tree(mem_ctx, expression);
782 ldb_set_errstring(ldb, "Unable to parse search expression");
783 return LDB_ERR_OPERATIONS_ERROR;
786 ret = ldb_build_search_req_ex(ret_req, ldb, mem_ctx, base,
787 scope, tree, attrs, controls,
788 context, callback, parent);
789 if (ret == LDB_SUCCESS) {
790 talloc_steal(*ret_req, tree);
795 int ldb_build_add_req(struct ldb_request **ret_req,
796 struct ldb_context *ldb,
798 const struct ldb_message *message,
799 struct ldb_control **controls,
801 ldb_request_callback_t callback,
802 struct ldb_request *parent)
804 struct ldb_request *req;
808 req = talloc(mem_ctx, struct ldb_request);
810 ldb_set_errstring(ldb, "Out of Memory");
811 return LDB_ERR_OPERATIONS_ERROR;
814 req->operation = LDB_ADD;
815 req->op.add.message = message;
816 req->controls = controls;
817 req->context = context;
818 req->callback = callback;
820 ldb_set_timeout_from_prev_req(ldb, parent, req);
822 req->handle = ldb_handle_new(req, ldb);
823 if (req->handle == NULL) {
825 return LDB_ERR_OPERATIONS_ERROR;
833 int ldb_build_mod_req(struct ldb_request **ret_req,
834 struct ldb_context *ldb,
836 const struct ldb_message *message,
837 struct ldb_control **controls,
839 ldb_request_callback_t callback,
840 struct ldb_request *parent)
842 struct ldb_request *req;
846 req = talloc(mem_ctx, struct ldb_request);
848 ldb_set_errstring(ldb, "Out of Memory");
849 return LDB_ERR_OPERATIONS_ERROR;
852 req->operation = LDB_MODIFY;
853 req->op.mod.message = message;
854 req->controls = controls;
855 req->context = context;
856 req->callback = callback;
858 ldb_set_timeout_from_prev_req(ldb, parent, req);
860 req->handle = ldb_handle_new(req, ldb);
861 if (req->handle == NULL) {
863 return LDB_ERR_OPERATIONS_ERROR;
871 int ldb_build_del_req(struct ldb_request **ret_req,
872 struct ldb_context *ldb,
875 struct ldb_control **controls,
877 ldb_request_callback_t callback,
878 struct ldb_request *parent)
880 struct ldb_request *req;
884 req = talloc(mem_ctx, struct ldb_request);
886 ldb_set_errstring(ldb, "Out of Memory");
887 return LDB_ERR_OPERATIONS_ERROR;
890 req->operation = LDB_DELETE;
892 req->controls = controls;
893 req->context = context;
894 req->callback = callback;
896 ldb_set_timeout_from_prev_req(ldb, parent, req);
898 req->handle = ldb_handle_new(req, ldb);
899 if (req->handle == NULL) {
901 return LDB_ERR_OPERATIONS_ERROR;
909 int ldb_build_rename_req(struct ldb_request **ret_req,
910 struct ldb_context *ldb,
912 struct ldb_dn *olddn,
913 struct ldb_dn *newdn,
914 struct ldb_control **controls,
916 ldb_request_callback_t callback,
917 struct ldb_request *parent)
919 struct ldb_request *req;
923 req = talloc(mem_ctx, struct ldb_request);
925 ldb_set_errstring(ldb, "Out of Memory");
926 return LDB_ERR_OPERATIONS_ERROR;
929 req->operation = LDB_RENAME;
930 req->op.rename.olddn = olddn;
931 req->op.rename.newdn = newdn;
932 req->controls = controls;
933 req->context = context;
934 req->callback = callback;
936 ldb_set_timeout_from_prev_req(ldb, parent, req);
938 req->handle = ldb_handle_new(req, ldb);
939 if (req->handle == NULL) {
941 return LDB_ERR_OPERATIONS_ERROR;
949 int ldb_extended_default_callback(struct ldb_request *req,
950 struct ldb_reply *ares)
952 struct ldb_result *res;
954 res = talloc_get_type(req->context, struct ldb_result);
957 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
959 if (ares->error != LDB_SUCCESS) {
960 return ldb_request_done(req, ares->error);
963 if (ares->type == LDB_REPLY_DONE) {
965 /* TODO: we should really support controls on entries and referrals too! */
966 res->extended = talloc_move(res, &ares->response);
967 res->controls = talloc_move(res, &ares->controls);
970 return ldb_request_done(req, LDB_SUCCESS);
974 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
975 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
978 int ldb_build_extended_req(struct ldb_request **ret_req,
979 struct ldb_context *ldb,
983 struct ldb_control **controls,
985 ldb_request_callback_t callback,
986 struct ldb_request *parent)
988 struct ldb_request *req;
992 req = talloc(mem_ctx, struct ldb_request);
994 ldb_set_errstring(ldb, "Out of Memory");
995 return LDB_ERR_OPERATIONS_ERROR;
998 req->operation = LDB_EXTENDED;
999 req->op.extended.oid = oid;
1000 req->op.extended.data = data;
1001 req->controls = controls;
1002 req->context = context;
1003 req->callback = callback;
1005 ldb_set_timeout_from_prev_req(ldb, parent, req);
1007 req->handle = ldb_handle_new(req, ldb);
1008 if (req->handle == NULL) {
1010 return LDB_ERR_OPERATIONS_ERROR;
1018 int ldb_extended(struct ldb_context *ldb,
1021 struct ldb_result **_res)
1023 struct ldb_request *req;
1025 struct ldb_result *res;
1029 res = talloc_zero(ldb, struct ldb_result);
1031 return LDB_ERR_OPERATIONS_ERROR;
1034 ret = ldb_build_extended_req(&req, ldb, ldb,
1036 res, ldb_extended_default_callback,
1038 if (ret != LDB_SUCCESS) goto done;
1040 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1042 ret = ldb_request(ldb, req);
1044 if (ret == LDB_SUCCESS) {
1045 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1051 if (ret != LDB_SUCCESS) {
1060 note that ldb_search() will automatically replace a NULL 'base' value
1061 with the defaultNamingContext from the rootDSE if available.
1063 int ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1064 struct ldb_result **result, struct ldb_dn *base,
1065 enum ldb_scope scope, const char * const *attrs,
1066 const char *exp_fmt, ...)
1068 struct ldb_request *req;
1069 struct ldb_result *res;
1078 res = talloc_zero(mem_ctx, struct ldb_result);
1080 return LDB_ERR_OPERATIONS_ERROR;
1084 va_start(ap, exp_fmt);
1085 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
1090 return LDB_ERR_OPERATIONS_ERROR;
1094 ret = ldb_build_search_req(&req, ldb, mem_ctx,
1095 base?base:ldb_get_default_basedn(ldb),
1101 ldb_search_default_callback,
1104 if (ret != LDB_SUCCESS) goto done;
1106 ret = ldb_request(ldb, req);
1108 if (ret == LDB_SUCCESS) {
1109 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1113 if (ret != LDB_SUCCESS) {
1118 talloc_free(expression);
1126 add a record to the database. Will fail if a record with the given class
1127 and key already exists
1129 int ldb_add(struct ldb_context *ldb,
1130 const struct ldb_message *message)
1132 struct ldb_request *req;
1135 ret = ldb_msg_sanity_check(ldb, message);
1136 if (ret != LDB_SUCCESS) {
1140 ret = ldb_build_add_req(&req, ldb, ldb,
1144 ldb_op_default_callback,
1147 if (ret != LDB_SUCCESS) return ret;
1149 /* do request and autostart a transaction */
1150 ret = ldb_autotransaction_request(ldb, req);
1157 modify the specified attributes of a record
1159 int ldb_modify(struct ldb_context *ldb,
1160 const struct ldb_message *message)
1162 struct ldb_request *req;
1165 ret = ldb_msg_sanity_check(ldb, message);
1166 if (ret != LDB_SUCCESS) {
1170 ret = ldb_build_mod_req(&req, ldb, ldb,
1174 ldb_op_default_callback,
1177 if (ret != LDB_SUCCESS) return ret;
1179 /* do request and autostart a transaction */
1180 ret = ldb_autotransaction_request(ldb, req);
1188 delete a record from the database
1190 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1192 struct ldb_request *req;
1195 ret = ldb_build_del_req(&req, ldb, ldb,
1199 ldb_op_default_callback,
1202 if (ret != LDB_SUCCESS) return ret;
1204 /* do request and autostart a transaction */
1205 ret = ldb_autotransaction_request(ldb, req);
1212 rename a record in the database
1214 int ldb_rename(struct ldb_context *ldb,
1215 struct ldb_dn *olddn, struct ldb_dn *newdn)
1217 struct ldb_request *req;
1220 ret = ldb_build_rename_req(&req, ldb, ldb,
1225 ldb_op_default_callback,
1228 if (ret != LDB_SUCCESS) return ret;
1230 /* do request and autostart a transaction */
1231 ret = ldb_autotransaction_request(ldb, req);
1239 return the global sequence number
1241 int ldb_sequence_number(struct ldb_context *ldb,
1242 enum ldb_sequence_type type, uint64_t *seq_num)
1244 struct ldb_seqnum_request *seq;
1245 struct ldb_seqnum_result *seqr;
1246 struct ldb_result *res;
1247 TALLOC_CTX *tmp_ctx;
1252 tmp_ctx = talloc_zero(ldb, struct ldb_request);
1253 if (tmp_ctx == NULL) {
1254 ldb_set_errstring(ldb, "Out of Memory");
1255 return LDB_ERR_OPERATIONS_ERROR;
1257 seq = talloc_zero(tmp_ctx, struct ldb_seqnum_request);
1259 ldb_set_errstring(ldb, "Out of Memory");
1260 ret = LDB_ERR_OPERATIONS_ERROR;
1265 ret = ldb_extended(ldb, LDB_EXTENDED_SEQUENCE_NUMBER, seq, &res);
1266 if (ret != LDB_SUCCESS) {
1269 talloc_steal(tmp_ctx, res);
1271 if (strcmp(LDB_EXTENDED_SEQUENCE_NUMBER, res->extended->oid) != 0) {
1272 ldb_set_errstring(ldb, "Invalid OID in reply");
1273 ret = LDB_ERR_OPERATIONS_ERROR;
1276 seqr = talloc_get_type(res->extended->data,
1277 struct ldb_seqnum_result);
1278 *seq_num = seqr->seq_num;
1281 talloc_free(tmp_ctx);
1286 return extended error information
1288 const char *ldb_errstring(struct ldb_context *ldb)
1290 if (ldb->err_string) {
1291 return ldb->err_string;
1298 return a string explaining what a ldb error constant meancs
1300 const char *ldb_strerror(int ldb_err)
1305 case LDB_ERR_OPERATIONS_ERROR:
1306 return "Operations error";
1307 case LDB_ERR_PROTOCOL_ERROR:
1308 return "Protocol error";
1309 case LDB_ERR_TIME_LIMIT_EXCEEDED:
1310 return "Time limit exceeded";
1311 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1312 return "Size limit exceeded";
1313 case LDB_ERR_COMPARE_FALSE:
1314 return "Compare false";
1315 case LDB_ERR_COMPARE_TRUE:
1316 return "Compare true";
1317 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1318 return "Auth method not supported";
1319 case LDB_ERR_STRONG_AUTH_REQUIRED:
1320 return "Strong auth required";
1322 case LDB_ERR_REFERRAL:
1323 return "Referral error";
1324 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1325 return "Admin limit exceeded";
1326 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1327 return "Unsupported critical extension";
1328 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1329 return "Confidentiality required";
1330 case LDB_ERR_SASL_BIND_IN_PROGRESS:
1331 return "SASL bind in progress";
1332 case LDB_ERR_NO_SUCH_ATTRIBUTE:
1333 return "No such attribute";
1334 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1335 return "Undefined attribute type";
1336 case LDB_ERR_INAPPROPRIATE_MATCHING:
1337 return "Inappropriate matching";
1338 case LDB_ERR_CONSTRAINT_VIOLATION:
1339 return "Constraint violation";
1340 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1341 return "Attribute or value exists";
1342 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1343 return "Invalid attribute syntax";
1345 case LDB_ERR_NO_SUCH_OBJECT:
1346 return "No such object";
1347 case LDB_ERR_ALIAS_PROBLEM:
1348 return "Alias problem";
1349 case LDB_ERR_INVALID_DN_SYNTAX:
1350 return "Invalid DN syntax";
1352 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1353 return "Alias dereferencing problem";
1355 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1356 return "Inappropriate authentication";
1357 case LDB_ERR_INVALID_CREDENTIALS:
1358 return "Invalid credentials";
1359 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1360 return "insufficient access rights";
1363 case LDB_ERR_UNAVAILABLE:
1364 return "Unavailable";
1365 case LDB_ERR_UNWILLING_TO_PERFORM:
1366 return "Unwilling to perform";
1367 case LDB_ERR_LOOP_DETECT:
1368 return "Loop detect";
1370 case LDB_ERR_NAMING_VIOLATION:
1371 return "Naming violation";
1372 case LDB_ERR_OBJECT_CLASS_VIOLATION:
1373 return "Object class violation";
1374 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1375 return "Not allowed on non-leaf";
1376 case LDB_ERR_NOT_ALLOWED_ON_RDN:
1377 return "Not allowed on RDN";
1378 case LDB_ERR_ENTRY_ALREADY_EXISTS:
1379 return "Entry already exists";
1380 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1381 return "Object class mods prohibited";
1382 /* 70 RESERVED FOR CLDAP */
1383 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1384 return "Affects multiple DSAs";
1390 return "Unknown error";
1394 set backend specific opaque parameters
1396 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1398 struct ldb_opaque *o;
1400 /* allow updating an existing value */
1401 for (o=ldb->opaque;o;o=o->next) {
1402 if (strcmp(o->name, name) == 0) {
1408 o = talloc(ldb, struct ldb_opaque);
1411 return LDB_ERR_OTHER;
1413 o->next = ldb->opaque;
1421 get a previously set opaque value
1423 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1425 struct ldb_opaque *o;
1426 for (o=ldb->opaque;o;o=o->next) {
1427 if (strcmp(o->name, name) == 0) {