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"
38 initialise a ldb context
39 The mem_ctx is required
40 The event_ctx is required
42 struct ldb_context *ldb_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev_ctx)
44 struct ldb_context *ldb;
47 ldb = talloc_zero(mem_ctx, struct ldb_context);
48 /* FIXME: Hack a new event context so that CMD line utilities work
49 * until we have them all converted */
51 ev_ctx = tevent_context_init(talloc_autofree_context());
54 ret = ldb_setup_wellknown_attributes(ldb);
60 ldb_set_utf8_default(ldb);
61 ldb_set_create_perms(ldb, 0666);
62 ldb_set_modules_dir(ldb, LDB_MODULESDIR);
63 ldb_set_event_context(ldb, ev_ctx);
65 /* TODO: get timeout from options if available there */
66 ldb->default_timeout = 300; /* set default to 5 minutes */
72 try to autodetect a basedn if none specified. This fixes one of my
73 pet hates about ldapsearch, which is that you have to get a long,
74 complex basedn right to make any use of it.
76 void ldb_set_default_dns(struct ldb_context *ldb)
80 struct ldb_result *res;
81 struct ldb_dn *tmp_dn=NULL;
82 static const char *attrs[] = {
83 "rootDomainNamingContext",
84 "configurationNamingContext",
85 "schemaNamingContext",
86 "defaultNamingContext",
90 tmp_ctx = talloc_new(ldb);
91 ret = ldb_search(ldb, tmp_ctx, &res, ldb_dn_new(tmp_ctx, ldb, NULL),
92 LDB_SCOPE_BASE, attrs, "(objectClass=*)");
93 if (ret != LDB_SUCCESS) {
98 if (res->count != 1) {
103 if (!ldb_get_opaque(ldb, "rootDomainNamingContext")) {
104 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
105 "rootDomainNamingContext");
106 ldb_set_opaque(ldb, "rootDomainNamingContext", tmp_dn);
109 if (!ldb_get_opaque(ldb, "configurationNamingContext")) {
110 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
111 "configurationNamingContext");
112 ldb_set_opaque(ldb, "configurationNamingContext", tmp_dn);
115 if (!ldb_get_opaque(ldb, "schemaNamingContext")) {
116 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
117 "schemaNamingContext");
118 ldb_set_opaque(ldb, "schemaNamingContext", tmp_dn);
121 if (!ldb_get_opaque(ldb, "defaultNamingContext")) {
122 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
123 "defaultNamingContext");
124 ldb_set_opaque(ldb, "defaultNamingContext", tmp_dn);
127 talloc_free(tmp_ctx);
130 struct ldb_dn *ldb_get_root_basedn(struct ldb_context *ldb)
132 void *opaque = ldb_get_opaque(ldb, "rootDomainNamingContext");
133 return talloc_get_type(opaque, struct ldb_dn);
136 struct ldb_dn *ldb_get_config_basedn(struct ldb_context *ldb)
138 void *opaque = ldb_get_opaque(ldb, "configurationNamingContext");
139 return talloc_get_type(opaque, struct ldb_dn);
142 struct ldb_dn *ldb_get_schema_basedn(struct ldb_context *ldb)
144 void *opaque = ldb_get_opaque(ldb, "schemaNamingContext");
145 return talloc_get_type(opaque, struct ldb_dn);
148 struct ldb_dn *ldb_get_default_basedn(struct ldb_context *ldb)
150 void *opaque = ldb_get_opaque(ldb, "defaultNamingContext");
151 return talloc_get_type(opaque, struct ldb_dn);
155 connect to a database. The URL can either be one of the following forms
159 flags is made up of LDB_FLG_*
161 the options are passed uninterpreted to the backend, and are
164 int ldb_connect(struct ldb_context *ldb, const char *url,
165 unsigned int flags, const char *options[])
169 /* We seem to need to do this here, or else some utilities don't
170 * get ldb backends */
174 url2 = talloc_strdup(ldb, url);
177 return LDB_ERR_OPERATIONS_ERROR;
179 ret = ldb_set_opaque(ldb, "ldb_url", talloc_strdup(ldb, url2));
180 if (ret != LDB_SUCCESS) {
184 ret = ldb_connect_backend(ldb, url, options, &ldb->modules);
185 if (ret != LDB_SUCCESS) {
189 if (ldb_load_modules(ldb, options) != LDB_SUCCESS) {
190 ldb_debug(ldb, LDB_DEBUG_FATAL,
191 "Unable to load modules for %s: %s\n",
192 url, ldb_errstring(ldb));
193 return LDB_ERR_OTHER;
196 /* set the default base dn */
197 ldb_set_default_dns(ldb);
202 void ldb_set_errstring(struct ldb_context *ldb, const char *err_string)
204 if (ldb->err_string) {
205 talloc_free(ldb->err_string);
207 ldb->err_string = talloc_strdup(ldb, err_string);
210 void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
213 char *old_string = NULL;
215 if (ldb->err_string) {
216 old_string = ldb->err_string;
219 va_start(ap, format);
220 ldb->err_string = talloc_vasprintf(ldb, format, ap);
222 talloc_free(old_string);
225 void ldb_reset_err_string(struct ldb_context *ldb)
227 if (ldb->err_string) {
228 talloc_free(ldb->err_string);
229 ldb->err_string = NULL;
233 #define FIRST_OP(ldb, op) do { \
234 module = ldb->modules; \
235 while (module && module->ops->op == NULL) module = module->next; \
236 if (module == NULL) { \
237 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
238 return LDB_ERR_OPERATIONS_ERROR; \
245 static int ldb_transaction_start_internal(struct ldb_context *ldb)
247 struct ldb_module *module;
249 FIRST_OP(ldb, start_transaction);
251 ldb_reset_err_string(ldb);
253 status = module->ops->start_transaction(module);
254 if (status != LDB_SUCCESS) {
255 if (ldb->err_string == NULL) {
256 /* no error string was setup by the backend */
257 ldb_asprintf_errstring(ldb,
258 "ldb transaction start: %s (%d)",
259 ldb_strerror(status),
269 static int ldb_transaction_commit_internal(struct ldb_context *ldb)
271 struct ldb_module *module;
273 FIRST_OP(ldb, end_transaction);
275 ldb_reset_err_string(ldb);
277 status = module->ops->end_transaction(module);
278 if (status != LDB_SUCCESS) {
279 if (ldb->err_string == NULL) {
280 /* no error string was setup by the backend */
281 ldb_asprintf_errstring(ldb,
282 "ldb transaction commit: %s (%d)",
283 ldb_strerror(status),
293 static int ldb_transaction_cancel_internal(struct ldb_context *ldb)
295 struct ldb_module *module;
297 FIRST_OP(ldb, del_transaction);
299 status = module->ops->del_transaction(module);
300 if (status != LDB_SUCCESS) {
301 if (ldb->err_string == NULL) {
302 /* no error string was setup by the backend */
303 ldb_asprintf_errstring(ldb,
304 "ldb transaction cancel: %s (%d)",
305 ldb_strerror(status),
312 int ldb_transaction_start(struct ldb_context *ldb)
314 /* disable autotransactions */
315 ldb->transaction_active++;
317 return ldb_transaction_start_internal(ldb);
320 int ldb_transaction_commit(struct ldb_context *ldb)
322 /* renable autotransactions (when we reach 0) */
323 if (ldb->transaction_active > 0)
324 ldb->transaction_active--;
326 return ldb_transaction_commit_internal(ldb);
329 int ldb_transaction_cancel(struct ldb_context *ldb)
331 /* renable autotransactions (when we reach 0) */
332 if (ldb->transaction_active > 0)
333 ldb->transaction_active--;
335 return ldb_transaction_cancel_internal(ldb);
338 static int ldb_autotransaction_start(struct ldb_context *ldb)
340 /* explicit transaction active, ignore autotransaction request */
341 if (ldb->transaction_active)
344 return ldb_transaction_start_internal(ldb);
347 static int ldb_autotransaction_commit(struct ldb_context *ldb)
349 /* explicit transaction active, ignore autotransaction request */
350 if (ldb->transaction_active)
353 return ldb_transaction_commit_internal(ldb);
356 static int ldb_autotransaction_cancel(struct ldb_context *ldb)
358 /* explicit transaction active, ignore autotransaction request */
359 if (ldb->transaction_active)
362 return ldb_transaction_cancel_internal(ldb);
365 /* autostarts a transacion if none active */
366 static int ldb_autotransaction_request(struct ldb_context *ldb,
367 struct ldb_request *req)
371 ret = ldb_autotransaction_start(ldb);
372 if (ret != LDB_SUCCESS) {
376 ret = ldb_request(ldb, req);
377 if (ret == LDB_SUCCESS) {
378 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
381 if (ret == LDB_SUCCESS) {
382 return ldb_autotransaction_commit(ldb);
384 ldb_autotransaction_cancel(ldb);
386 if (ldb->err_string == NULL) {
387 /* no error string was setup by the backend */
388 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
394 int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
396 struct tevent_context *ev;
400 return LDB_ERR_UNAVAILABLE;
403 if (handle->state == LDB_ASYNC_DONE) {
404 return handle->status;
407 ev = ldb_get_event_context(handle->ldb);
409 return LDB_ERR_OPERATIONS_ERROR;
414 ret = tevent_loop_once(ev);
416 return LDB_ERR_OPERATIONS_ERROR;
418 if (handle->state == LDB_ASYNC_DONE ||
419 handle->status != LDB_SUCCESS) {
420 return handle->status;
425 while (handle->state != LDB_ASYNC_DONE) {
426 ret = tevent_loop_once(ev);
428 return LDB_ERR_OPERATIONS_ERROR;
430 if (handle->status != LDB_SUCCESS) {
431 return handle->status;
434 return handle->status;
440 /* set the specified timeout or, if timeout is 0 set the default timeout */
441 int ldb_set_timeout(struct ldb_context *ldb,
442 struct ldb_request *req,
445 if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
448 req->timeout = timeout;
450 req->timeout = ldb->default_timeout;
452 req->starttime = time(NULL);
457 /* calculates the new timeout based on the previous starttime and timeout */
458 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb,
459 struct ldb_request *oldreq,
460 struct ldb_request *newreq)
462 if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
464 if (oldreq == NULL) {
465 return ldb_set_timeout(ldb, newreq, 0);
468 newreq->starttime = oldreq->starttime;
469 newreq->timeout = oldreq->timeout;
476 set the permissions for new files to be passed to open() in
477 backends that use local files
479 void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
481 ldb->create_perms = perms;
484 unsigned int ldb_get_create_perms(struct ldb_context *ldb)
486 return ldb->create_perms;
489 void ldb_set_event_context(struct ldb_context *ldb, struct tevent_context *ev)
494 struct tevent_context * ldb_get_event_context(struct ldb_context *ldb)
499 void ldb_request_set_state(struct ldb_request *req, int state)
501 req->handle->state = state;
504 int ldb_request_get_status(struct ldb_request *req)
506 return req->handle->status;
511 NOTE: the request must be a talloc context.
512 returns LDB_ERR_* on errors.
514 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
516 struct ldb_module *module;
519 if (req->callback == NULL) {
520 ldb_set_errstring(ldb, "Requests MUST define callbacks");
521 return LDB_ERR_UNWILLING_TO_PERFORM;
524 ldb_reset_err_string(ldb);
526 /* call the first module in the chain */
527 switch (req->operation) {
529 FIRST_OP(ldb, search);
530 ret = module->ops->search(module, req);
534 ret = module->ops->add(module, req);
537 FIRST_OP(ldb, modify);
538 ret = module->ops->modify(module, req);
542 ret = module->ops->del(module, req);
545 FIRST_OP(ldb, rename);
546 ret = module->ops->rename(module, req);
549 FIRST_OP(ldb, extended);
550 ret = module->ops->extended(module, req);
553 FIRST_OP(ldb, request);
554 ret = module->ops->request(module, req);
561 int ldb_request_done(struct ldb_request *req, int status)
563 req->handle->state = LDB_ASYNC_DONE;
564 req->handle->status = status;
569 search the database given a LDAP-like search expression
571 returns an LDB error code
573 Use talloc_free to free the ldb_message returned in 'res', if successful
576 int ldb_search_default_callback(struct ldb_request *req,
577 struct ldb_reply *ares)
579 struct ldb_result *res;
582 res = talloc_get_type(req->context, struct ldb_result);
585 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
587 if (ares->error != LDB_SUCCESS) {
588 return ldb_request_done(req, ares->error);
591 switch (ares->type) {
592 case LDB_REPLY_ENTRY:
593 res->msgs = talloc_realloc(res, res->msgs,
594 struct ldb_message *, res->count + 2);
596 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
599 res->msgs[res->count + 1] = NULL;
601 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
605 case LDB_REPLY_REFERRAL:
607 for (n = 0; res->refs[n]; n++) /*noop*/ ;
612 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
614 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
617 res->refs[n] = talloc_move(res->refs, &ares->referral);
618 res->refs[n + 1] = NULL;
622 /* TODO: we should really support controls on entries
623 * and referrals too! */
624 res->controls = talloc_move(res, &ares->controls);
626 /* this is the last message, and means the request is done */
627 /* we have to signal and eventual ldb_wait() waiting that the
628 * async request operation was completed */
629 return ldb_request_done(req, LDB_SUCCESS);
636 int ldb_op_default_callback(struct ldb_request *req, struct ldb_reply *ares)
641 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
644 if (ares->error != LDB_SUCCESS) {
647 return ldb_request_done(req, ret);
650 if (ares->type != LDB_REPLY_DONE) {
652 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
653 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
657 return ldb_request_done(req, LDB_SUCCESS);
660 int ldb_build_search_req_ex(struct ldb_request **ret_req,
661 struct ldb_context *ldb,
664 enum ldb_scope scope,
665 struct ldb_parse_tree *tree,
666 const char * const *attrs,
667 struct ldb_control **controls,
669 ldb_request_callback_t callback,
670 struct ldb_request *parent)
672 struct ldb_request *req;
676 req = talloc(mem_ctx, struct ldb_request);
679 return LDB_ERR_OPERATIONS_ERROR;
682 req->operation = LDB_SEARCH;
684 req->op.search.base = ldb_dn_new(req, ldb, NULL);
686 req->op.search.base = base;
688 req->op.search.scope = scope;
690 req->op.search.tree = tree;
691 if (req->op.search.tree == NULL) {
692 ldb_set_errstring(ldb, "'tree' can't be NULL");
694 return LDB_ERR_OPERATIONS_ERROR;
697 req->op.search.attrs = attrs;
698 req->controls = controls;
699 req->context = context;
700 req->callback = callback;
702 ldb_set_timeout_from_prev_req(ldb, parent, req);
704 req->handle = ldb_handle_new(req, ldb);
705 if (req->handle == NULL) {
707 return LDB_ERR_OPERATIONS_ERROR;
714 int ldb_build_search_req(struct ldb_request **ret_req,
715 struct ldb_context *ldb,
718 enum ldb_scope scope,
719 const char *expression,
720 const char * const *attrs,
721 struct ldb_control **controls,
723 ldb_request_callback_t callback,
724 struct ldb_request *parent)
726 struct ldb_parse_tree *tree;
729 tree = ldb_parse_tree(mem_ctx, expression);
731 ldb_set_errstring(ldb, "Unable to parse search expression");
732 return LDB_ERR_OPERATIONS_ERROR;
735 ret = ldb_build_search_req_ex(ret_req, ldb, mem_ctx, base,
736 scope, tree, attrs, controls,
737 context, callback, parent);
738 if (ret == LDB_SUCCESS) {
739 talloc_steal(*ret_req, tree);
744 int ldb_build_add_req(struct ldb_request **ret_req,
745 struct ldb_context *ldb,
747 const struct ldb_message *message,
748 struct ldb_control **controls,
750 ldb_request_callback_t callback,
751 struct ldb_request *parent)
753 struct ldb_request *req;
757 req = talloc(mem_ctx, struct ldb_request);
759 ldb_set_errstring(ldb, "Out of Memory");
760 return LDB_ERR_OPERATIONS_ERROR;
763 req->operation = LDB_ADD;
764 req->op.add.message = message;
765 req->controls = controls;
766 req->context = context;
767 req->callback = callback;
769 ldb_set_timeout_from_prev_req(ldb, parent, req);
771 req->handle = ldb_handle_new(req, ldb);
772 if (req->handle == NULL) {
774 return LDB_ERR_OPERATIONS_ERROR;
782 int ldb_build_mod_req(struct ldb_request **ret_req,
783 struct ldb_context *ldb,
785 const struct ldb_message *message,
786 struct ldb_control **controls,
788 ldb_request_callback_t callback,
789 struct ldb_request *parent)
791 struct ldb_request *req;
795 req = talloc(mem_ctx, struct ldb_request);
797 ldb_set_errstring(ldb, "Out of Memory");
798 return LDB_ERR_OPERATIONS_ERROR;
801 req->operation = LDB_MODIFY;
802 req->op.mod.message = message;
803 req->controls = controls;
804 req->context = context;
805 req->callback = callback;
807 ldb_set_timeout_from_prev_req(ldb, parent, req);
809 req->handle = ldb_handle_new(req, ldb);
810 if (req->handle == NULL) {
812 return LDB_ERR_OPERATIONS_ERROR;
820 int ldb_build_del_req(struct ldb_request **ret_req,
821 struct ldb_context *ldb,
824 struct ldb_control **controls,
826 ldb_request_callback_t callback,
827 struct ldb_request *parent)
829 struct ldb_request *req;
833 req = talloc(mem_ctx, struct ldb_request);
835 ldb_set_errstring(ldb, "Out of Memory");
836 return LDB_ERR_OPERATIONS_ERROR;
839 req->operation = LDB_DELETE;
841 req->controls = controls;
842 req->context = context;
843 req->callback = callback;
845 ldb_set_timeout_from_prev_req(ldb, parent, req);
847 req->handle = ldb_handle_new(req, ldb);
848 if (req->handle == NULL) {
850 return LDB_ERR_OPERATIONS_ERROR;
858 int ldb_build_rename_req(struct ldb_request **ret_req,
859 struct ldb_context *ldb,
861 struct ldb_dn *olddn,
862 struct ldb_dn *newdn,
863 struct ldb_control **controls,
865 ldb_request_callback_t callback,
866 struct ldb_request *parent)
868 struct ldb_request *req;
872 req = talloc(mem_ctx, struct ldb_request);
874 ldb_set_errstring(ldb, "Out of Memory");
875 return LDB_ERR_OPERATIONS_ERROR;
878 req->operation = LDB_RENAME;
879 req->op.rename.olddn = olddn;
880 req->op.rename.newdn = newdn;
881 req->controls = controls;
882 req->context = context;
883 req->callback = callback;
885 ldb_set_timeout_from_prev_req(ldb, parent, req);
887 req->handle = ldb_handle_new(req, ldb);
888 if (req->handle == NULL) {
890 return LDB_ERR_OPERATIONS_ERROR;
898 int ldb_extended_default_callback(struct ldb_request *req,
899 struct ldb_reply *ares)
901 struct ldb_result *res;
903 res = talloc_get_type(req->context, struct ldb_result);
906 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
908 if (ares->error != LDB_SUCCESS) {
909 return ldb_request_done(req, ares->error);
912 if (ares->type == LDB_REPLY_DONE) {
914 /* TODO: we should really support controls on entries and referrals too! */
915 res->extended = talloc_move(res, &ares->response);
916 res->controls = talloc_move(res, &ares->controls);
919 return ldb_request_done(req, LDB_SUCCESS);
923 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
924 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
927 int ldb_build_extended_req(struct ldb_request **ret_req,
928 struct ldb_context *ldb,
932 struct ldb_control **controls,
934 ldb_request_callback_t callback,
935 struct ldb_request *parent)
937 struct ldb_request *req;
941 req = talloc(mem_ctx, struct ldb_request);
943 ldb_set_errstring(ldb, "Out of Memory");
944 return LDB_ERR_OPERATIONS_ERROR;
947 req->operation = LDB_EXTENDED;
948 req->op.extended.oid = oid;
949 req->op.extended.data = data;
950 req->controls = controls;
951 req->context = context;
952 req->callback = callback;
954 ldb_set_timeout_from_prev_req(ldb, parent, req);
956 req->handle = ldb_handle_new(req, ldb);
957 if (req->handle == NULL) {
959 return LDB_ERR_OPERATIONS_ERROR;
967 int ldb_extended(struct ldb_context *ldb,
970 struct ldb_result **_res)
972 struct ldb_request *req;
974 struct ldb_result *res;
978 res = talloc_zero(ldb, struct ldb_result);
980 return LDB_ERR_OPERATIONS_ERROR;
983 ret = ldb_build_extended_req(&req, ldb, ldb,
985 res, ldb_extended_default_callback,
987 if (ret != LDB_SUCCESS) goto done;
989 ldb_set_timeout(ldb, req, 0); /* use default timeout */
991 ret = ldb_request(ldb, req);
993 if (ret == LDB_SUCCESS) {
994 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1000 if (ret != LDB_SUCCESS) {
1009 note that ldb_search() will automatically replace a NULL 'base' value
1010 with the defaultNamingContext from the rootDSE if available.
1012 int ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1013 struct ldb_result **result, struct ldb_dn *base,
1014 enum ldb_scope scope, const char * const *attrs,
1015 const char *exp_fmt, ...)
1017 struct ldb_request *req;
1018 struct ldb_result *res;
1027 res = talloc_zero(mem_ctx, struct ldb_result);
1029 return LDB_ERR_OPERATIONS_ERROR;
1033 va_start(ap, exp_fmt);
1034 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
1039 return LDB_ERR_OPERATIONS_ERROR;
1043 ret = ldb_build_search_req(&req, ldb, mem_ctx,
1044 base?base:ldb_get_default_basedn(ldb),
1050 ldb_search_default_callback,
1053 if (ret != LDB_SUCCESS) goto done;
1055 ret = ldb_request(ldb, req);
1057 if (ret == LDB_SUCCESS) {
1058 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1062 if (ret != LDB_SUCCESS) {
1067 talloc_free(expression);
1075 add a record to the database. Will fail if a record with the given class
1076 and key already exists
1078 int ldb_add(struct ldb_context *ldb,
1079 const struct ldb_message *message)
1081 struct ldb_request *req;
1084 ret = ldb_msg_sanity_check(ldb, message);
1085 if (ret != LDB_SUCCESS) {
1089 ret = ldb_build_add_req(&req, ldb, ldb,
1093 ldb_op_default_callback,
1096 if (ret != LDB_SUCCESS) return ret;
1098 /* do request and autostart a transaction */
1099 ret = ldb_autotransaction_request(ldb, req);
1106 modify the specified attributes of a record
1108 int ldb_modify(struct ldb_context *ldb,
1109 const struct ldb_message *message)
1111 struct ldb_request *req;
1114 ret = ldb_msg_sanity_check(ldb, message);
1115 if (ret != LDB_SUCCESS) {
1119 ret = ldb_build_mod_req(&req, ldb, ldb,
1123 ldb_op_default_callback,
1126 if (ret != LDB_SUCCESS) return ret;
1128 /* do request and autostart a transaction */
1129 ret = ldb_autotransaction_request(ldb, req);
1137 delete a record from the database
1139 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1141 struct ldb_request *req;
1144 ret = ldb_build_del_req(&req, ldb, ldb,
1148 ldb_op_default_callback,
1151 if (ret != LDB_SUCCESS) return ret;
1153 /* do request and autostart a transaction */
1154 ret = ldb_autotransaction_request(ldb, req);
1161 rename a record in the database
1163 int ldb_rename(struct ldb_context *ldb,
1164 struct ldb_dn *olddn, struct ldb_dn *newdn)
1166 struct ldb_request *req;
1169 ret = ldb_build_rename_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 return the global sequence number
1190 int ldb_sequence_number(struct ldb_context *ldb,
1191 enum ldb_sequence_type type, uint64_t *seq_num)
1193 struct ldb_seqnum_request *seq;
1194 struct ldb_seqnum_result *seqr;
1195 struct ldb_result *res;
1196 TALLOC_CTX *tmp_ctx;
1201 tmp_ctx = talloc_zero(ldb, struct ldb_request);
1202 if (tmp_ctx == NULL) {
1203 ldb_set_errstring(ldb, "Out of Memory");
1204 return LDB_ERR_OPERATIONS_ERROR;
1206 seq = talloc_zero(tmp_ctx, struct ldb_seqnum_request);
1208 ldb_set_errstring(ldb, "Out of Memory");
1209 ret = LDB_ERR_OPERATIONS_ERROR;
1214 ret = ldb_extended(ldb, LDB_EXTENDED_SEQUENCE_NUMBER, seq, &res);
1215 if (ret != LDB_SUCCESS) {
1218 talloc_steal(tmp_ctx, res);
1220 if (strcmp(LDB_EXTENDED_SEQUENCE_NUMBER, res->extended->oid) != 0) {
1221 ldb_set_errstring(ldb, "Invalid OID in reply");
1222 ret = LDB_ERR_OPERATIONS_ERROR;
1225 seqr = talloc_get_type(res->extended->data,
1226 struct ldb_seqnum_result);
1227 *seq_num = seqr->seq_num;
1230 talloc_free(tmp_ctx);
1235 return extended error information
1237 const char *ldb_errstring(struct ldb_context *ldb)
1239 if (ldb->err_string) {
1240 return ldb->err_string;
1247 return a string explaining what a ldb error constant meancs
1249 const char *ldb_strerror(int ldb_err)
1254 case LDB_ERR_OPERATIONS_ERROR:
1255 return "Operations error";
1256 case LDB_ERR_PROTOCOL_ERROR:
1257 return "Protocol error";
1258 case LDB_ERR_TIME_LIMIT_EXCEEDED:
1259 return "Time limit exceeded";
1260 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1261 return "Size limit exceeded";
1262 case LDB_ERR_COMPARE_FALSE:
1263 return "Compare false";
1264 case LDB_ERR_COMPARE_TRUE:
1265 return "Compare true";
1266 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1267 return "Auth method not supported";
1268 case LDB_ERR_STRONG_AUTH_REQUIRED:
1269 return "Strong auth required";
1271 case LDB_ERR_REFERRAL:
1272 return "Referral error";
1273 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1274 return "Admin limit exceeded";
1275 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1276 return "Unsupported critical extension";
1277 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1278 return "Confidentiality required";
1279 case LDB_ERR_SASL_BIND_IN_PROGRESS:
1280 return "SASL bind in progress";
1281 case LDB_ERR_NO_SUCH_ATTRIBUTE:
1282 return "No such attribute";
1283 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1284 return "Undefined attribute type";
1285 case LDB_ERR_INAPPROPRIATE_MATCHING:
1286 return "Inappropriate matching";
1287 case LDB_ERR_CONSTRAINT_VIOLATION:
1288 return "Constraint violation";
1289 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1290 return "Attribute or value exists";
1291 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1292 return "Invalid attribute syntax";
1294 case LDB_ERR_NO_SUCH_OBJECT:
1295 return "No such object";
1296 case LDB_ERR_ALIAS_PROBLEM:
1297 return "Alias problem";
1298 case LDB_ERR_INVALID_DN_SYNTAX:
1299 return "Invalid DN syntax";
1301 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1302 return "Alias dereferencing problem";
1304 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1305 return "Inappropriate authentication";
1306 case LDB_ERR_INVALID_CREDENTIALS:
1307 return "Invalid credentials";
1308 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1309 return "insufficient access rights";
1312 case LDB_ERR_UNAVAILABLE:
1313 return "Unavailable";
1314 case LDB_ERR_UNWILLING_TO_PERFORM:
1315 return "Unwilling to perform";
1316 case LDB_ERR_LOOP_DETECT:
1317 return "Loop detect";
1319 case LDB_ERR_NAMING_VIOLATION:
1320 return "Naming violation";
1321 case LDB_ERR_OBJECT_CLASS_VIOLATION:
1322 return "Object class violation";
1323 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1324 return "Not allowed on non-leaf";
1325 case LDB_ERR_NOT_ALLOWED_ON_RDN:
1326 return "Not allowed on RDN";
1327 case LDB_ERR_ENTRY_ALREADY_EXISTS:
1328 return "Entry already exists";
1329 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1330 return "Object class mods prohibited";
1331 /* 70 RESERVED FOR CLDAP */
1332 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1333 return "Affects multiple DSAs";
1339 return "Unknown error";
1343 set backend specific opaque parameters
1345 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1347 struct ldb_opaque *o;
1349 /* allow updating an existing value */
1350 for (o=ldb->opaque;o;o=o->next) {
1351 if (strcmp(o->name, name) == 0) {
1357 o = talloc(ldb, struct ldb_opaque);
1360 return LDB_ERR_OTHER;
1362 o->next = ldb->opaque;
1370 get a previously set opaque value
1372 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1374 struct ldb_opaque *o;
1375 for (o=ldb->opaque;o;o=o->next) {
1376 if (strcmp(o->name, name) == 0) {