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 #include "ldb_private.h"
37 static int ldb_context_destructor(void *ptr)
39 struct ldb_context *ldb = talloc_get_type(ptr, struct ldb_context);
41 if (ldb->transaction_active) {
42 ldb_debug(ldb, LDB_DEBUG_FATAL,
43 "A transaction is still active in ldb context [%p]",
51 this is used to catch debug messages from events
53 static void ldb_tevent_debug(void *context, enum tevent_debug_level level,
54 const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
56 static void ldb_tevent_debug(void *context, enum tevent_debug_level level,
57 const char *fmt, va_list ap)
59 struct ldb_context = talloc_get_type_abort(context, struct ldb_context);
60 enum ldb_debug_level ldb_level = LDB_DEBUG_FATAL;
64 case TEVENT_DEBUG_FATAL:
65 ldb_level = LDB_DEBUG_FATAL;
67 case TEVENT_DEBUG_ERROR:
68 ldb_level = LDB_DEBUG_ERROR;
70 case TEVENT_DEBUG_WARNING:
71 ldb_level = LDB_DEBUG_WARNING;
73 case TEVENT_DEBUG_TRACE:
74 ldb_level = LDB_DEBUG_TRACE;
78 vasprintf(&s, fmt, ap);
80 ldb_debug(ldb, ldb_level, "tevent: %s", s);
85 initialise a ldb context
86 The mem_ctx is required
87 The event_ctx is required
89 struct ldb_context *ldb_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev_ctx)
91 struct ldb_context *ldb;
94 ldb = talloc_zero(mem_ctx, struct ldb_context);
95 /* FIXME: Hack a new event context so that CMD line utilities work
96 * until we have them all converted */
98 ev_ctx = tevent_context_init(talloc_autofree_context());
99 tevent_set_debug(ev_ctx, ldb_tevent_debug, ldb);
102 ret = ldb_setup_wellknown_attributes(ldb);
108 ldb_set_utf8_default(ldb);
109 ldb_set_create_perms(ldb, 0666);
110 ldb_set_modules_dir(ldb, LDB_MODULESDIR);
111 ldb_set_event_context(ldb, ev_ctx);
113 /* TODO: get timeout from options if available there */
114 ldb->default_timeout = 300; /* set default to 5 minutes */
116 talloc_set_destructor((TALLOC_CTX *)ldb, ldb_context_destructor);
122 try to autodetect a basedn if none specified. This fixes one of my
123 pet hates about ldapsearch, which is that you have to get a long,
124 complex basedn right to make any use of it.
126 void ldb_set_default_dns(struct ldb_context *ldb)
130 struct ldb_result *res;
131 struct ldb_dn *tmp_dn=NULL;
132 static const char *attrs[] = {
133 "rootDomainNamingContext",
134 "configurationNamingContext",
135 "schemaNamingContext",
136 "defaultNamingContext",
140 tmp_ctx = talloc_new(ldb);
141 ret = ldb_search(ldb, tmp_ctx, &res, ldb_dn_new(tmp_ctx, ldb, NULL),
142 LDB_SCOPE_BASE, attrs, "(objectClass=*)");
143 if (ret != LDB_SUCCESS) {
144 talloc_free(tmp_ctx);
148 if (res->count != 1) {
149 talloc_free(tmp_ctx);
153 if (!ldb_get_opaque(ldb, "rootDomainNamingContext")) {
154 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
155 "rootDomainNamingContext");
156 ldb_set_opaque(ldb, "rootDomainNamingContext", tmp_dn);
159 if (!ldb_get_opaque(ldb, "configurationNamingContext")) {
160 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
161 "configurationNamingContext");
162 ldb_set_opaque(ldb, "configurationNamingContext", tmp_dn);
165 if (!ldb_get_opaque(ldb, "schemaNamingContext")) {
166 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
167 "schemaNamingContext");
168 ldb_set_opaque(ldb, "schemaNamingContext", tmp_dn);
171 if (!ldb_get_opaque(ldb, "defaultNamingContext")) {
172 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
173 "defaultNamingContext");
174 ldb_set_opaque(ldb, "defaultNamingContext", tmp_dn);
177 talloc_free(tmp_ctx);
180 struct ldb_dn *ldb_get_root_basedn(struct ldb_context *ldb)
182 void *opaque = ldb_get_opaque(ldb, "rootDomainNamingContext");
183 return talloc_get_type(opaque, struct ldb_dn);
186 struct ldb_dn *ldb_get_config_basedn(struct ldb_context *ldb)
188 void *opaque = ldb_get_opaque(ldb, "configurationNamingContext");
189 return talloc_get_type(opaque, struct ldb_dn);
192 struct ldb_dn *ldb_get_schema_basedn(struct ldb_context *ldb)
194 void *opaque = ldb_get_opaque(ldb, "schemaNamingContext");
195 return talloc_get_type(opaque, struct ldb_dn);
198 struct ldb_dn *ldb_get_default_basedn(struct ldb_context *ldb)
200 void *opaque = ldb_get_opaque(ldb, "defaultNamingContext");
201 return talloc_get_type(opaque, struct ldb_dn);
205 connect to a database. The URL can either be one of the following forms
209 flags is made up of LDB_FLG_*
211 the options are passed uninterpreted to the backend, and are
214 int ldb_connect(struct ldb_context *ldb, const char *url,
215 unsigned int flags, const char *options[])
219 /* We seem to need to do this here, or else some utilities don't
220 * get ldb backends */
224 url2 = talloc_strdup(ldb, url);
227 return LDB_ERR_OPERATIONS_ERROR;
229 ret = ldb_set_opaque(ldb, "ldb_url", talloc_strdup(ldb, url2));
230 if (ret != LDB_SUCCESS) {
234 ret = ldb_connect_backend(ldb, url, options, &ldb->modules);
235 if (ret != LDB_SUCCESS) {
239 if (ldb_load_modules(ldb, options) != LDB_SUCCESS) {
240 ldb_debug(ldb, LDB_DEBUG_FATAL,
241 "Unable to load modules for %s: %s\n",
242 url, ldb_errstring(ldb));
243 return LDB_ERR_OTHER;
246 /* set the default base dn */
247 ldb_set_default_dns(ldb);
252 void ldb_set_errstring(struct ldb_context *ldb, const char *err_string)
254 if (ldb->err_string) {
255 talloc_free(ldb->err_string);
257 ldb->err_string = talloc_strdup(ldb, err_string);
260 void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
263 char *old_string = NULL;
265 if (ldb->err_string) {
266 old_string = ldb->err_string;
269 va_start(ap, format);
270 ldb->err_string = talloc_vasprintf(ldb, format, ap);
272 talloc_free(old_string);
275 void ldb_reset_err_string(struct ldb_context *ldb)
277 if (ldb->err_string) {
278 talloc_free(ldb->err_string);
279 ldb->err_string = NULL;
283 #define FIRST_OP(ldb, op) do { \
284 module = ldb->modules; \
285 while (module && module->ops->op == NULL) module = module->next; \
286 if (module == NULL) { \
287 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
288 return LDB_ERR_OPERATIONS_ERROR; \
295 int ldb_transaction_start(struct ldb_context *ldb)
297 struct ldb_module *module;
300 ldb_debug(ldb, LDB_DEBUG_TRACE,
301 "start ldb transaction (nesting: %d)",
302 ldb->transaction_active);
304 /* explicit transaction active, count nested requests */
305 if (ldb->transaction_active) {
306 ldb->transaction_active++;
310 /* start a new transaction */
311 ldb->transaction_active++;
313 FIRST_OP(ldb, start_transaction);
315 ldb_reset_err_string(ldb);
317 status = module->ops->start_transaction(module);
318 if (status != LDB_SUCCESS) {
319 if (ldb->err_string == NULL) {
320 /* no error string was setup by the backend */
321 ldb_asprintf_errstring(ldb,
322 "ldb transaction start: %s (%d)",
323 ldb_strerror(status),
333 int ldb_transaction_commit(struct ldb_context *ldb)
335 struct ldb_module *module;
338 ldb->transaction_active--;
340 ldb_debug(ldb, LDB_DEBUG_TRACE,
341 "commit ldb transaction (nesting: %d)",
342 ldb->transaction_active);
344 /* commit only when all nested transactions are complete */
345 if (ldb->transaction_active > 0) {
349 if (ldb->transaction_active < 0) {
350 ldb_debug(ldb, LDB_DEBUG_FATAL,
351 "commit called but no ldb transactions are active!");
352 ldb->transaction_active = 0;
353 return LDB_ERR_OPERATIONS_ERROR;
356 FIRST_OP(ldb, end_transaction);
358 ldb_reset_err_string(ldb);
360 status = module->ops->end_transaction(module);
361 if (status != LDB_SUCCESS) {
362 if (ldb->err_string == NULL) {
363 /* no error string was setup by the backend */
364 ldb_asprintf_errstring(ldb,
365 "ldb transaction commit: %s (%d)",
366 ldb_strerror(status),
376 int ldb_transaction_cancel(struct ldb_context *ldb)
378 struct ldb_module *module;
381 ldb->transaction_active--;
383 ldb_debug(ldb, LDB_DEBUG_TRACE,
384 "cancel ldb transaction (nesting: %d)",
385 ldb->transaction_active);
387 /* really cancel only if all nested transactions are complete */
388 if (ldb->transaction_active > 0) {
392 if (ldb->transaction_active < 0) {
393 ldb_debug(ldb, LDB_DEBUG_FATAL,
394 "commit called but no ldb transactions are active!");
395 ldb->transaction_active = 0;
396 return LDB_ERR_OPERATIONS_ERROR;
399 FIRST_OP(ldb, del_transaction);
401 status = module->ops->del_transaction(module);
402 if (status != LDB_SUCCESS) {
403 if (ldb->err_string == NULL) {
404 /* no error string was setup by the backend */
405 ldb_asprintf_errstring(ldb,
406 "ldb transaction cancel: %s (%d)",
407 ldb_strerror(status),
414 /* autostarts a transacion if none active */
415 static int ldb_autotransaction_request(struct ldb_context *ldb,
416 struct ldb_request *req)
420 ret = ldb_transaction_start(ldb);
421 if (ret != LDB_SUCCESS) {
425 ret = ldb_request(ldb, req);
426 if (ret == LDB_SUCCESS) {
427 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
430 if (ret == LDB_SUCCESS) {
431 return ldb_transaction_commit(ldb);
433 ldb_transaction_cancel(ldb);
435 if (ldb->err_string == NULL) {
436 /* no error string was setup by the backend */
437 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
443 int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
445 struct tevent_context *ev;
449 return LDB_ERR_UNAVAILABLE;
452 if (handle->state == LDB_ASYNC_DONE) {
453 return handle->status;
456 ev = ldb_get_event_context(handle->ldb);
458 return LDB_ERR_OPERATIONS_ERROR;
463 ret = tevent_loop_once(ev);
465 return LDB_ERR_OPERATIONS_ERROR;
467 if (handle->state == LDB_ASYNC_DONE ||
468 handle->status != LDB_SUCCESS) {
469 return handle->status;
474 while (handle->state != LDB_ASYNC_DONE) {
475 ret = tevent_loop_once(ev);
477 return LDB_ERR_OPERATIONS_ERROR;
479 if (handle->status != LDB_SUCCESS) {
480 return handle->status;
483 return handle->status;
489 /* set the specified timeout or, if timeout is 0 set the default timeout */
490 int ldb_set_timeout(struct ldb_context *ldb,
491 struct ldb_request *req,
494 if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
497 req->timeout = timeout;
499 req->timeout = ldb->default_timeout;
501 req->starttime = time(NULL);
506 /* calculates the new timeout based on the previous starttime and timeout */
507 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb,
508 struct ldb_request *oldreq,
509 struct ldb_request *newreq)
511 if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
513 if (oldreq == NULL) {
514 return ldb_set_timeout(ldb, newreq, 0);
517 newreq->starttime = oldreq->starttime;
518 newreq->timeout = oldreq->timeout;
525 set the permissions for new files to be passed to open() in
526 backends that use local files
528 void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
530 ldb->create_perms = perms;
533 unsigned int ldb_get_create_perms(struct ldb_context *ldb)
535 return ldb->create_perms;
538 void ldb_set_event_context(struct ldb_context *ldb, struct tevent_context *ev)
543 struct tevent_context * ldb_get_event_context(struct ldb_context *ldb)
548 void ldb_request_set_state(struct ldb_request *req, int state)
550 req->handle->state = state;
553 int ldb_request_get_status(struct ldb_request *req)
555 return req->handle->status;
560 NOTE: the request must be a talloc context.
561 returns LDB_ERR_* on errors.
563 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
565 struct ldb_module *module;
568 if (req->callback == NULL) {
569 ldb_set_errstring(ldb, "Requests MUST define callbacks");
570 return LDB_ERR_UNWILLING_TO_PERFORM;
573 ldb_reset_err_string(ldb);
575 /* call the first module in the chain */
576 switch (req->operation) {
578 FIRST_OP(ldb, search);
579 ret = module->ops->search(module, req);
583 ret = module->ops->add(module, req);
586 FIRST_OP(ldb, modify);
587 ret = module->ops->modify(module, req);
591 ret = module->ops->del(module, req);
594 FIRST_OP(ldb, rename);
595 ret = module->ops->rename(module, req);
598 FIRST_OP(ldb, extended);
599 ret = module->ops->extended(module, req);
602 FIRST_OP(ldb, request);
603 ret = module->ops->request(module, req);
610 int ldb_request_done(struct ldb_request *req, int status)
612 req->handle->state = LDB_ASYNC_DONE;
613 req->handle->status = status;
618 search the database given a LDAP-like search expression
620 returns an LDB error code
622 Use talloc_free to free the ldb_message returned in 'res', if successful
625 int ldb_search_default_callback(struct ldb_request *req,
626 struct ldb_reply *ares)
628 struct ldb_result *res;
631 res = talloc_get_type(req->context, struct ldb_result);
634 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
636 if (ares->error != LDB_SUCCESS) {
637 return ldb_request_done(req, ares->error);
640 switch (ares->type) {
641 case LDB_REPLY_ENTRY:
642 res->msgs = talloc_realloc(res, res->msgs,
643 struct ldb_message *, res->count + 2);
645 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
648 res->msgs[res->count + 1] = NULL;
650 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
654 case LDB_REPLY_REFERRAL:
656 for (n = 0; res->refs[n]; n++) /*noop*/ ;
661 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
663 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
666 res->refs[n] = talloc_move(res->refs, &ares->referral);
667 res->refs[n + 1] = NULL;
671 /* TODO: we should really support controls on entries
672 * and referrals too! */
673 res->controls = talloc_move(res, &ares->controls);
675 /* this is the last message, and means the request is done */
676 /* we have to signal and eventual ldb_wait() waiting that the
677 * async request operation was completed */
678 return ldb_request_done(req, LDB_SUCCESS);
685 int ldb_op_default_callback(struct ldb_request *req, struct ldb_reply *ares)
690 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
693 if (ares->error != LDB_SUCCESS) {
696 return ldb_request_done(req, ret);
699 if (ares->type != LDB_REPLY_DONE) {
701 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
702 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
706 return ldb_request_done(req, LDB_SUCCESS);
709 int ldb_build_search_req_ex(struct ldb_request **ret_req,
710 struct ldb_context *ldb,
713 enum ldb_scope scope,
714 struct ldb_parse_tree *tree,
715 const char * const *attrs,
716 struct ldb_control **controls,
718 ldb_request_callback_t callback,
719 struct ldb_request *parent)
721 struct ldb_request *req;
725 req = talloc(mem_ctx, struct ldb_request);
728 return LDB_ERR_OPERATIONS_ERROR;
731 req->operation = LDB_SEARCH;
733 req->op.search.base = ldb_dn_new(req, ldb, NULL);
735 req->op.search.base = base;
737 req->op.search.scope = scope;
739 req->op.search.tree = tree;
740 if (req->op.search.tree == NULL) {
741 ldb_set_errstring(ldb, "'tree' can't be NULL");
743 return LDB_ERR_OPERATIONS_ERROR;
746 req->op.search.attrs = attrs;
747 req->controls = controls;
748 req->context = context;
749 req->callback = callback;
751 ldb_set_timeout_from_prev_req(ldb, parent, req);
753 req->handle = ldb_handle_new(req, ldb);
754 if (req->handle == NULL) {
756 return LDB_ERR_OPERATIONS_ERROR;
763 int ldb_build_search_req(struct ldb_request **ret_req,
764 struct ldb_context *ldb,
767 enum ldb_scope scope,
768 const char *expression,
769 const char * const *attrs,
770 struct ldb_control **controls,
772 ldb_request_callback_t callback,
773 struct ldb_request *parent)
775 struct ldb_parse_tree *tree;
778 tree = ldb_parse_tree(mem_ctx, expression);
780 ldb_set_errstring(ldb, "Unable to parse search expression");
781 return LDB_ERR_OPERATIONS_ERROR;
784 ret = ldb_build_search_req_ex(ret_req, ldb, mem_ctx, base,
785 scope, tree, attrs, controls,
786 context, callback, parent);
787 if (ret == LDB_SUCCESS) {
788 talloc_steal(*ret_req, tree);
793 int ldb_build_add_req(struct ldb_request **ret_req,
794 struct ldb_context *ldb,
796 const struct ldb_message *message,
797 struct ldb_control **controls,
799 ldb_request_callback_t callback,
800 struct ldb_request *parent)
802 struct ldb_request *req;
806 req = talloc(mem_ctx, struct ldb_request);
808 ldb_set_errstring(ldb, "Out of Memory");
809 return LDB_ERR_OPERATIONS_ERROR;
812 req->operation = LDB_ADD;
813 req->op.add.message = message;
814 req->controls = controls;
815 req->context = context;
816 req->callback = callback;
818 ldb_set_timeout_from_prev_req(ldb, parent, req);
820 req->handle = ldb_handle_new(req, ldb);
821 if (req->handle == NULL) {
823 return LDB_ERR_OPERATIONS_ERROR;
831 int ldb_build_mod_req(struct ldb_request **ret_req,
832 struct ldb_context *ldb,
834 const struct ldb_message *message,
835 struct ldb_control **controls,
837 ldb_request_callback_t callback,
838 struct ldb_request *parent)
840 struct ldb_request *req;
844 req = talloc(mem_ctx, struct ldb_request);
846 ldb_set_errstring(ldb, "Out of Memory");
847 return LDB_ERR_OPERATIONS_ERROR;
850 req->operation = LDB_MODIFY;
851 req->op.mod.message = message;
852 req->controls = controls;
853 req->context = context;
854 req->callback = callback;
856 ldb_set_timeout_from_prev_req(ldb, parent, req);
858 req->handle = ldb_handle_new(req, ldb);
859 if (req->handle == NULL) {
861 return LDB_ERR_OPERATIONS_ERROR;
869 int ldb_build_del_req(struct ldb_request **ret_req,
870 struct ldb_context *ldb,
873 struct ldb_control **controls,
875 ldb_request_callback_t callback,
876 struct ldb_request *parent)
878 struct ldb_request *req;
882 req = talloc(mem_ctx, struct ldb_request);
884 ldb_set_errstring(ldb, "Out of Memory");
885 return LDB_ERR_OPERATIONS_ERROR;
888 req->operation = LDB_DELETE;
890 req->controls = controls;
891 req->context = context;
892 req->callback = callback;
894 ldb_set_timeout_from_prev_req(ldb, parent, req);
896 req->handle = ldb_handle_new(req, ldb);
897 if (req->handle == NULL) {
899 return LDB_ERR_OPERATIONS_ERROR;
907 int ldb_build_rename_req(struct ldb_request **ret_req,
908 struct ldb_context *ldb,
910 struct ldb_dn *olddn,
911 struct ldb_dn *newdn,
912 struct ldb_control **controls,
914 ldb_request_callback_t callback,
915 struct ldb_request *parent)
917 struct ldb_request *req;
921 req = talloc(mem_ctx, struct ldb_request);
923 ldb_set_errstring(ldb, "Out of Memory");
924 return LDB_ERR_OPERATIONS_ERROR;
927 req->operation = LDB_RENAME;
928 req->op.rename.olddn = olddn;
929 req->op.rename.newdn = newdn;
930 req->controls = controls;
931 req->context = context;
932 req->callback = callback;
934 ldb_set_timeout_from_prev_req(ldb, parent, req);
936 req->handle = ldb_handle_new(req, ldb);
937 if (req->handle == NULL) {
939 return LDB_ERR_OPERATIONS_ERROR;
947 int ldb_extended_default_callback(struct ldb_request *req,
948 struct ldb_reply *ares)
950 struct ldb_result *res;
952 res = talloc_get_type(req->context, struct ldb_result);
955 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
957 if (ares->error != LDB_SUCCESS) {
958 return ldb_request_done(req, ares->error);
961 if (ares->type == LDB_REPLY_DONE) {
963 /* TODO: we should really support controls on entries and referrals too! */
964 res->extended = talloc_move(res, &ares->response);
965 res->controls = talloc_move(res, &ares->controls);
968 return ldb_request_done(req, LDB_SUCCESS);
972 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
973 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
976 int ldb_build_extended_req(struct ldb_request **ret_req,
977 struct ldb_context *ldb,
981 struct ldb_control **controls,
983 ldb_request_callback_t callback,
984 struct ldb_request *parent)
986 struct ldb_request *req;
990 req = talloc(mem_ctx, struct ldb_request);
992 ldb_set_errstring(ldb, "Out of Memory");
993 return LDB_ERR_OPERATIONS_ERROR;
996 req->operation = LDB_EXTENDED;
997 req->op.extended.oid = oid;
998 req->op.extended.data = data;
999 req->controls = controls;
1000 req->context = context;
1001 req->callback = callback;
1003 ldb_set_timeout_from_prev_req(ldb, parent, req);
1005 req->handle = ldb_handle_new(req, ldb);
1006 if (req->handle == NULL) {
1008 return LDB_ERR_OPERATIONS_ERROR;
1016 int ldb_extended(struct ldb_context *ldb,
1019 struct ldb_result **_res)
1021 struct ldb_request *req;
1023 struct ldb_result *res;
1027 res = talloc_zero(ldb, struct ldb_result);
1029 return LDB_ERR_OPERATIONS_ERROR;
1032 ret = ldb_build_extended_req(&req, ldb, ldb,
1034 res, ldb_extended_default_callback,
1036 if (ret != LDB_SUCCESS) goto done;
1038 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1040 ret = ldb_request(ldb, req);
1042 if (ret == LDB_SUCCESS) {
1043 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1049 if (ret != LDB_SUCCESS) {
1058 note that ldb_search() will automatically replace a NULL 'base' value
1059 with the defaultNamingContext from the rootDSE if available.
1061 int ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1062 struct ldb_result **result, struct ldb_dn *base,
1063 enum ldb_scope scope, const char * const *attrs,
1064 const char *exp_fmt, ...)
1066 struct ldb_request *req;
1067 struct ldb_result *res;
1076 res = talloc_zero(mem_ctx, struct ldb_result);
1078 return LDB_ERR_OPERATIONS_ERROR;
1082 va_start(ap, exp_fmt);
1083 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
1088 return LDB_ERR_OPERATIONS_ERROR;
1092 ret = ldb_build_search_req(&req, ldb, mem_ctx,
1093 base?base:ldb_get_default_basedn(ldb),
1099 ldb_search_default_callback,
1102 if (ret != LDB_SUCCESS) goto done;
1104 ret = ldb_request(ldb, req);
1106 if (ret == LDB_SUCCESS) {
1107 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1111 if (ret != LDB_SUCCESS) {
1116 talloc_free(expression);
1124 add a record to the database. Will fail if a record with the given class
1125 and key already exists
1127 int ldb_add(struct ldb_context *ldb,
1128 const struct ldb_message *message)
1130 struct ldb_request *req;
1133 ret = ldb_msg_sanity_check(ldb, message);
1134 if (ret != LDB_SUCCESS) {
1138 ret = ldb_build_add_req(&req, ldb, ldb,
1142 ldb_op_default_callback,
1145 if (ret != LDB_SUCCESS) return ret;
1147 /* do request and autostart a transaction */
1148 ret = ldb_autotransaction_request(ldb, req);
1155 modify the specified attributes of a record
1157 int ldb_modify(struct ldb_context *ldb,
1158 const struct ldb_message *message)
1160 struct ldb_request *req;
1163 ret = ldb_msg_sanity_check(ldb, message);
1164 if (ret != LDB_SUCCESS) {
1168 ret = ldb_build_mod_req(&req, ldb, ldb,
1172 ldb_op_default_callback,
1175 if (ret != LDB_SUCCESS) return ret;
1177 /* do request and autostart a transaction */
1178 ret = ldb_autotransaction_request(ldb, req);
1186 delete a record from the database
1188 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1190 struct ldb_request *req;
1193 ret = ldb_build_del_req(&req, ldb, ldb,
1197 ldb_op_default_callback,
1200 if (ret != LDB_SUCCESS) return ret;
1202 /* do request and autostart a transaction */
1203 ret = ldb_autotransaction_request(ldb, req);
1210 rename a record in the database
1212 int ldb_rename(struct ldb_context *ldb,
1213 struct ldb_dn *olddn, struct ldb_dn *newdn)
1215 struct ldb_request *req;
1218 ret = ldb_build_rename_req(&req, ldb, ldb,
1223 ldb_op_default_callback,
1226 if (ret != LDB_SUCCESS) return ret;
1228 /* do request and autostart a transaction */
1229 ret = ldb_autotransaction_request(ldb, req);
1237 return the global sequence number
1239 int ldb_sequence_number(struct ldb_context *ldb,
1240 enum ldb_sequence_type type, uint64_t *seq_num)
1242 struct ldb_seqnum_request *seq;
1243 struct ldb_seqnum_result *seqr;
1244 struct ldb_result *res;
1245 TALLOC_CTX *tmp_ctx;
1250 tmp_ctx = talloc_zero(ldb, struct ldb_request);
1251 if (tmp_ctx == NULL) {
1252 ldb_set_errstring(ldb, "Out of Memory");
1253 return LDB_ERR_OPERATIONS_ERROR;
1255 seq = talloc_zero(tmp_ctx, struct ldb_seqnum_request);
1257 ldb_set_errstring(ldb, "Out of Memory");
1258 ret = LDB_ERR_OPERATIONS_ERROR;
1263 ret = ldb_extended(ldb, LDB_EXTENDED_SEQUENCE_NUMBER, seq, &res);
1264 if (ret != LDB_SUCCESS) {
1267 talloc_steal(tmp_ctx, res);
1269 if (strcmp(LDB_EXTENDED_SEQUENCE_NUMBER, res->extended->oid) != 0) {
1270 ldb_set_errstring(ldb, "Invalid OID in reply");
1271 ret = LDB_ERR_OPERATIONS_ERROR;
1274 seqr = talloc_get_type(res->extended->data,
1275 struct ldb_seqnum_result);
1276 *seq_num = seqr->seq_num;
1279 talloc_free(tmp_ctx);
1284 return extended error information
1286 const char *ldb_errstring(struct ldb_context *ldb)
1288 if (ldb->err_string) {
1289 return ldb->err_string;
1296 return a string explaining what a ldb error constant meancs
1298 const char *ldb_strerror(int ldb_err)
1303 case LDB_ERR_OPERATIONS_ERROR:
1304 return "Operations error";
1305 case LDB_ERR_PROTOCOL_ERROR:
1306 return "Protocol error";
1307 case LDB_ERR_TIME_LIMIT_EXCEEDED:
1308 return "Time limit exceeded";
1309 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1310 return "Size limit exceeded";
1311 case LDB_ERR_COMPARE_FALSE:
1312 return "Compare false";
1313 case LDB_ERR_COMPARE_TRUE:
1314 return "Compare true";
1315 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1316 return "Auth method not supported";
1317 case LDB_ERR_STRONG_AUTH_REQUIRED:
1318 return "Strong auth required";
1320 case LDB_ERR_REFERRAL:
1321 return "Referral error";
1322 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1323 return "Admin limit exceeded";
1324 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1325 return "Unsupported critical extension";
1326 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1327 return "Confidentiality required";
1328 case LDB_ERR_SASL_BIND_IN_PROGRESS:
1329 return "SASL bind in progress";
1330 case LDB_ERR_NO_SUCH_ATTRIBUTE:
1331 return "No such attribute";
1332 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1333 return "Undefined attribute type";
1334 case LDB_ERR_INAPPROPRIATE_MATCHING:
1335 return "Inappropriate matching";
1336 case LDB_ERR_CONSTRAINT_VIOLATION:
1337 return "Constraint violation";
1338 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1339 return "Attribute or value exists";
1340 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1341 return "Invalid attribute syntax";
1343 case LDB_ERR_NO_SUCH_OBJECT:
1344 return "No such object";
1345 case LDB_ERR_ALIAS_PROBLEM:
1346 return "Alias problem";
1347 case LDB_ERR_INVALID_DN_SYNTAX:
1348 return "Invalid DN syntax";
1350 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1351 return "Alias dereferencing problem";
1353 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1354 return "Inappropriate authentication";
1355 case LDB_ERR_INVALID_CREDENTIALS:
1356 return "Invalid credentials";
1357 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1358 return "insufficient access rights";
1361 case LDB_ERR_UNAVAILABLE:
1362 return "Unavailable";
1363 case LDB_ERR_UNWILLING_TO_PERFORM:
1364 return "Unwilling to perform";
1365 case LDB_ERR_LOOP_DETECT:
1366 return "Loop detect";
1368 case LDB_ERR_NAMING_VIOLATION:
1369 return "Naming violation";
1370 case LDB_ERR_OBJECT_CLASS_VIOLATION:
1371 return "Object class violation";
1372 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1373 return "Not allowed on non-leaf";
1374 case LDB_ERR_NOT_ALLOWED_ON_RDN:
1375 return "Not allowed on RDN";
1376 case LDB_ERR_ENTRY_ALREADY_EXISTS:
1377 return "Entry already exists";
1378 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1379 return "Object class mods prohibited";
1380 /* 70 RESERVED FOR CLDAP */
1381 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1382 return "Affects multiple DSAs";
1388 return "Unknown error";
1392 set backend specific opaque parameters
1394 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1396 struct ldb_opaque *o;
1398 /* allow updating an existing value */
1399 for (o=ldb->opaque;o;o=o->next) {
1400 if (strcmp(o->name, name) == 0) {
1406 o = talloc(ldb, struct ldb_opaque);
1409 return LDB_ERR_OTHER;
1411 o->next = ldb->opaque;
1419 get a previously set opaque value
1421 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1423 struct ldb_opaque *o;
1424 for (o=ldb->opaque;o;o=o->next) {
1425 if (strcmp(o->name, name) == 0) {