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_includes.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 void ldb_set_event_context(struct ldb_context *ldb, struct tevent_context *ev)
489 struct tevent_context * ldb_get_event_context(struct ldb_context *ldb)
496 NOTE: the request must be a talloc context.
497 returns LDB_ERR_* on errors.
499 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
501 struct ldb_module *module;
504 if (req->callback == NULL) {
505 ldb_set_errstring(ldb, "Requests MUST define callbacks");
506 return LDB_ERR_UNWILLING_TO_PERFORM;
509 ldb_reset_err_string(ldb);
511 /* call the first module in the chain */
512 switch (req->operation) {
514 FIRST_OP(ldb, search);
515 ret = module->ops->search(module, req);
519 ret = module->ops->add(module, req);
522 FIRST_OP(ldb, modify);
523 ret = module->ops->modify(module, req);
527 ret = module->ops->del(module, req);
530 FIRST_OP(ldb, rename);
531 ret = module->ops->rename(module, req);
534 FIRST_OP(ldb, extended);
535 ret = module->ops->extended(module, req);
538 FIRST_OP(ldb, request);
539 ret = module->ops->request(module, req);
546 int ldb_request_done(struct ldb_request *req, int status)
548 req->handle->state = LDB_ASYNC_DONE;
549 req->handle->status = status;
554 search the database given a LDAP-like search expression
556 returns an LDB error code
558 Use talloc_free to free the ldb_message returned in 'res', if successful
561 int ldb_search_default_callback(struct ldb_request *req,
562 struct ldb_reply *ares)
564 struct ldb_result *res;
567 res = talloc_get_type(req->context, struct ldb_result);
570 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
572 if (ares->error != LDB_SUCCESS) {
573 return ldb_request_done(req, ares->error);
576 switch (ares->type) {
577 case LDB_REPLY_ENTRY:
578 res->msgs = talloc_realloc(res, res->msgs,
579 struct ldb_message *, res->count + 2);
581 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
584 res->msgs[res->count + 1] = NULL;
586 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
590 case LDB_REPLY_REFERRAL:
592 for (n = 0; res->refs[n]; n++) /*noop*/ ;
597 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
599 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
602 res->refs[n] = talloc_move(res->refs, &ares->referral);
603 res->refs[n + 1] = NULL;
607 /* TODO: we should really support controls on entries
608 * and referrals too! */
609 res->controls = talloc_move(res, &ares->controls);
611 /* this is the last message, and means the request is done */
612 /* we have to signal and eventual ldb_wait() waiting that the
613 * async request operation was completed */
614 return ldb_request_done(req, LDB_SUCCESS);
621 int ldb_op_default_callback(struct ldb_request *req, struct ldb_reply *ares)
626 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
629 if (ares->error != LDB_SUCCESS) {
632 return ldb_request_done(req, ret);
635 if (ares->type != LDB_REPLY_DONE) {
637 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
638 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
642 return ldb_request_done(req, LDB_SUCCESS);
645 int ldb_build_search_req_ex(struct ldb_request **ret_req,
646 struct ldb_context *ldb,
649 enum ldb_scope scope,
650 struct ldb_parse_tree *tree,
651 const char * const *attrs,
652 struct ldb_control **controls,
654 ldb_request_callback_t callback,
655 struct ldb_request *parent)
657 struct ldb_request *req;
661 req = talloc(mem_ctx, struct ldb_request);
664 return LDB_ERR_OPERATIONS_ERROR;
667 req->operation = LDB_SEARCH;
669 req->op.search.base = ldb_dn_new(req, ldb, NULL);
671 req->op.search.base = base;
673 req->op.search.scope = scope;
675 req->op.search.tree = tree;
676 if (req->op.search.tree == NULL) {
677 ldb_set_errstring(ldb, "'tree' can't be NULL");
679 return LDB_ERR_OPERATIONS_ERROR;
682 req->op.search.attrs = attrs;
683 req->controls = controls;
684 req->context = context;
685 req->callback = callback;
687 ldb_set_timeout_from_prev_req(ldb, parent, req);
689 req->handle = ldb_handle_new(req, ldb);
690 if (req->handle == NULL) {
692 return LDB_ERR_OPERATIONS_ERROR;
699 int ldb_build_search_req(struct ldb_request **ret_req,
700 struct ldb_context *ldb,
703 enum ldb_scope scope,
704 const char *expression,
705 const char * const *attrs,
706 struct ldb_control **controls,
708 ldb_request_callback_t callback,
709 struct ldb_request *parent)
711 struct ldb_parse_tree *tree;
714 tree = ldb_parse_tree(mem_ctx, expression);
716 ldb_set_errstring(ldb, "Unable to parse search expression");
717 return LDB_ERR_OPERATIONS_ERROR;
720 ret = ldb_build_search_req_ex(ret_req, ldb, mem_ctx, base,
721 scope, tree, attrs, controls,
722 context, callback, parent);
723 if (ret == LDB_SUCCESS) {
724 talloc_steal(*ret_req, tree);
729 int ldb_build_add_req(struct ldb_request **ret_req,
730 struct ldb_context *ldb,
732 const struct ldb_message *message,
733 struct ldb_control **controls,
735 ldb_request_callback_t callback,
736 struct ldb_request *parent)
738 struct ldb_request *req;
742 req = talloc(mem_ctx, struct ldb_request);
744 ldb_set_errstring(ldb, "Out of Memory");
745 return LDB_ERR_OPERATIONS_ERROR;
748 req->operation = LDB_ADD;
749 req->op.add.message = message;
750 req->controls = controls;
751 req->context = context;
752 req->callback = callback;
754 ldb_set_timeout_from_prev_req(ldb, parent, req);
756 req->handle = ldb_handle_new(req, ldb);
757 if (req->handle == NULL) {
759 return LDB_ERR_OPERATIONS_ERROR;
767 int ldb_build_mod_req(struct ldb_request **ret_req,
768 struct ldb_context *ldb,
770 const struct ldb_message *message,
771 struct ldb_control **controls,
773 ldb_request_callback_t callback,
774 struct ldb_request *parent)
776 struct ldb_request *req;
780 req = talloc(mem_ctx, struct ldb_request);
782 ldb_set_errstring(ldb, "Out of Memory");
783 return LDB_ERR_OPERATIONS_ERROR;
786 req->operation = LDB_MODIFY;
787 req->op.mod.message = message;
788 req->controls = controls;
789 req->context = context;
790 req->callback = callback;
792 ldb_set_timeout_from_prev_req(ldb, parent, req);
794 req->handle = ldb_handle_new(req, ldb);
795 if (req->handle == NULL) {
797 return LDB_ERR_OPERATIONS_ERROR;
805 int ldb_build_del_req(struct ldb_request **ret_req,
806 struct ldb_context *ldb,
809 struct ldb_control **controls,
811 ldb_request_callback_t callback,
812 struct ldb_request *parent)
814 struct ldb_request *req;
818 req = talloc(mem_ctx, struct ldb_request);
820 ldb_set_errstring(ldb, "Out of Memory");
821 return LDB_ERR_OPERATIONS_ERROR;
824 req->operation = LDB_DELETE;
826 req->controls = controls;
827 req->context = context;
828 req->callback = callback;
830 ldb_set_timeout_from_prev_req(ldb, parent, req);
832 req->handle = ldb_handle_new(req, ldb);
833 if (req->handle == NULL) {
835 return LDB_ERR_OPERATIONS_ERROR;
843 int ldb_build_rename_req(struct ldb_request **ret_req,
844 struct ldb_context *ldb,
846 struct ldb_dn *olddn,
847 struct ldb_dn *newdn,
848 struct ldb_control **controls,
850 ldb_request_callback_t callback,
851 struct ldb_request *parent)
853 struct ldb_request *req;
857 req = talloc(mem_ctx, struct ldb_request);
859 ldb_set_errstring(ldb, "Out of Memory");
860 return LDB_ERR_OPERATIONS_ERROR;
863 req->operation = LDB_RENAME;
864 req->op.rename.olddn = olddn;
865 req->op.rename.newdn = newdn;
866 req->controls = controls;
867 req->context = context;
868 req->callback = callback;
870 ldb_set_timeout_from_prev_req(ldb, parent, req);
872 req->handle = ldb_handle_new(req, ldb);
873 if (req->handle == NULL) {
875 return LDB_ERR_OPERATIONS_ERROR;
883 int ldb_extended_default_callback(struct ldb_request *req,
884 struct ldb_reply *ares)
886 struct ldb_result *res;
888 res = talloc_get_type(req->context, struct ldb_result);
891 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
893 if (ares->error != LDB_SUCCESS) {
894 return ldb_request_done(req, ares->error);
897 if (ares->type == LDB_REPLY_DONE) {
899 /* TODO: we should really support controls on entries and referrals too! */
900 res->extended = talloc_move(res, &ares->response);
901 res->controls = talloc_move(res, &ares->controls);
904 return ldb_request_done(req, LDB_SUCCESS);
908 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
909 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
912 int ldb_build_extended_req(struct ldb_request **ret_req,
913 struct ldb_context *ldb,
917 struct ldb_control **controls,
919 ldb_request_callback_t callback,
920 struct ldb_request *parent)
922 struct ldb_request *req;
926 req = talloc(mem_ctx, struct ldb_request);
928 ldb_set_errstring(ldb, "Out of Memory");
929 return LDB_ERR_OPERATIONS_ERROR;
932 req->operation = LDB_EXTENDED;
933 req->op.extended.oid = oid;
934 req->op.extended.data = data;
935 req->controls = controls;
936 req->context = context;
937 req->callback = callback;
939 ldb_set_timeout_from_prev_req(ldb, parent, req);
941 req->handle = ldb_handle_new(req, ldb);
942 if (req->handle == NULL) {
944 return LDB_ERR_OPERATIONS_ERROR;
952 int ldb_extended(struct ldb_context *ldb,
955 struct ldb_result **_res)
957 struct ldb_request *req;
959 struct ldb_result *res;
963 res = talloc_zero(ldb, struct ldb_result);
965 return LDB_ERR_OPERATIONS_ERROR;
968 ret = ldb_build_extended_req(&req, ldb, ldb,
970 res, ldb_extended_default_callback,
972 if (ret != LDB_SUCCESS) goto done;
974 ldb_set_timeout(ldb, req, 0); /* use default timeout */
976 ret = ldb_request(ldb, req);
978 if (ret == LDB_SUCCESS) {
979 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
985 if (ret != LDB_SUCCESS) {
994 note that ldb_search() will automatically replace a NULL 'base' value
995 with the defaultNamingContext from the rootDSE if available.
997 int ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
998 struct ldb_result **result, struct ldb_dn *base,
999 enum ldb_scope scope, const char * const *attrs,
1000 const char *exp_fmt, ...)
1002 struct ldb_request *req;
1003 struct ldb_result *res;
1012 res = talloc_zero(mem_ctx, struct ldb_result);
1014 return LDB_ERR_OPERATIONS_ERROR;
1018 va_start(ap, exp_fmt);
1019 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
1024 return LDB_ERR_OPERATIONS_ERROR;
1028 ret = ldb_build_search_req(&req, ldb, mem_ctx,
1029 base?base:ldb_get_default_basedn(ldb),
1035 ldb_search_default_callback,
1038 if (ret != LDB_SUCCESS) goto done;
1040 ret = ldb_request(ldb, req);
1042 if (ret == LDB_SUCCESS) {
1043 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1047 if (ret != LDB_SUCCESS) {
1052 talloc_free(expression);
1060 add a record to the database. Will fail if a record with the given class
1061 and key already exists
1063 int ldb_add(struct ldb_context *ldb,
1064 const struct ldb_message *message)
1066 struct ldb_request *req;
1069 ret = ldb_msg_sanity_check(ldb, message);
1070 if (ret != LDB_SUCCESS) {
1074 ret = ldb_build_add_req(&req, ldb, ldb,
1078 ldb_op_default_callback,
1081 if (ret != LDB_SUCCESS) return ret;
1083 /* do request and autostart a transaction */
1084 ret = ldb_autotransaction_request(ldb, req);
1091 modify the specified attributes of a record
1093 int ldb_modify(struct ldb_context *ldb,
1094 const struct ldb_message *message)
1096 struct ldb_request *req;
1099 ret = ldb_msg_sanity_check(ldb, message);
1100 if (ret != LDB_SUCCESS) {
1104 ret = ldb_build_mod_req(&req, ldb, ldb,
1108 ldb_op_default_callback,
1111 if (ret != LDB_SUCCESS) return ret;
1113 /* do request and autostart a transaction */
1114 ret = ldb_autotransaction_request(ldb, req);
1122 delete a record from the database
1124 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1126 struct ldb_request *req;
1129 ret = ldb_build_del_req(&req, ldb, ldb,
1133 ldb_op_default_callback,
1136 if (ret != LDB_SUCCESS) return ret;
1138 /* do request and autostart a transaction */
1139 ret = ldb_autotransaction_request(ldb, req);
1146 rename a record in the database
1148 int ldb_rename(struct ldb_context *ldb,
1149 struct ldb_dn *olddn, struct ldb_dn *newdn)
1151 struct ldb_request *req;
1154 ret = ldb_build_rename_req(&req, ldb, ldb,
1159 ldb_op_default_callback,
1162 if (ret != LDB_SUCCESS) return ret;
1164 /* do request and autostart a transaction */
1165 ret = ldb_autotransaction_request(ldb, req);
1173 return the global sequence number
1175 int ldb_sequence_number(struct ldb_context *ldb,
1176 enum ldb_sequence_type type, uint64_t *seq_num)
1178 struct ldb_seqnum_request *seq;
1179 struct ldb_seqnum_result *seqr;
1180 struct ldb_result *res;
1181 TALLOC_CTX *tmp_ctx;
1186 tmp_ctx = talloc_zero(ldb, struct ldb_request);
1187 if (tmp_ctx == NULL) {
1188 ldb_set_errstring(ldb, "Out of Memory");
1189 return LDB_ERR_OPERATIONS_ERROR;
1191 seq = talloc_zero(tmp_ctx, struct ldb_seqnum_request);
1193 ldb_set_errstring(ldb, "Out of Memory");
1194 ret = LDB_ERR_OPERATIONS_ERROR;
1199 ret = ldb_extended(ldb, LDB_EXTENDED_SEQUENCE_NUMBER, seq, &res);
1200 if (ret != LDB_SUCCESS) {
1203 talloc_steal(tmp_ctx, res);
1205 if (strcmp(LDB_EXTENDED_SEQUENCE_NUMBER, res->extended->oid) != 0) {
1206 ldb_set_errstring(ldb, "Invalid OID in reply");
1207 ret = LDB_ERR_OPERATIONS_ERROR;
1210 seqr = talloc_get_type(res->extended->data,
1211 struct ldb_seqnum_result);
1212 *seq_num = seqr->seq_num;
1215 talloc_free(tmp_ctx);
1220 return extended error information
1222 const char *ldb_errstring(struct ldb_context *ldb)
1224 if (ldb->err_string) {
1225 return ldb->err_string;
1232 return a string explaining what a ldb error constant meancs
1234 const char *ldb_strerror(int ldb_err)
1239 case LDB_ERR_OPERATIONS_ERROR:
1240 return "Operations error";
1241 case LDB_ERR_PROTOCOL_ERROR:
1242 return "Protocol error";
1243 case LDB_ERR_TIME_LIMIT_EXCEEDED:
1244 return "Time limit exceeded";
1245 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1246 return "Size limit exceeded";
1247 case LDB_ERR_COMPARE_FALSE:
1248 return "Compare false";
1249 case LDB_ERR_COMPARE_TRUE:
1250 return "Compare true";
1251 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1252 return "Auth method not supported";
1253 case LDB_ERR_STRONG_AUTH_REQUIRED:
1254 return "Strong auth required";
1256 case LDB_ERR_REFERRAL:
1257 return "Referral error";
1258 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1259 return "Admin limit exceeded";
1260 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1261 return "Unsupported critical extension";
1262 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1263 return "Confidentiality required";
1264 case LDB_ERR_SASL_BIND_IN_PROGRESS:
1265 return "SASL bind in progress";
1266 case LDB_ERR_NO_SUCH_ATTRIBUTE:
1267 return "No such attribute";
1268 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1269 return "Undefined attribute type";
1270 case LDB_ERR_INAPPROPRIATE_MATCHING:
1271 return "Inappropriate matching";
1272 case LDB_ERR_CONSTRAINT_VIOLATION:
1273 return "Constraint violation";
1274 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1275 return "Attribute or value exists";
1276 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1277 return "Invalid attribute syntax";
1279 case LDB_ERR_NO_SUCH_OBJECT:
1280 return "No such object";
1281 case LDB_ERR_ALIAS_PROBLEM:
1282 return "Alias problem";
1283 case LDB_ERR_INVALID_DN_SYNTAX:
1284 return "Invalid DN syntax";
1286 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1287 return "Alias dereferencing problem";
1289 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1290 return "Inappropriate authentication";
1291 case LDB_ERR_INVALID_CREDENTIALS:
1292 return "Invalid credentials";
1293 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1294 return "insufficient access rights";
1297 case LDB_ERR_UNAVAILABLE:
1298 return "Unavailable";
1299 case LDB_ERR_UNWILLING_TO_PERFORM:
1300 return "Unwilling to perform";
1301 case LDB_ERR_LOOP_DETECT:
1302 return "Loop detect";
1304 case LDB_ERR_NAMING_VIOLATION:
1305 return "Naming violation";
1306 case LDB_ERR_OBJECT_CLASS_VIOLATION:
1307 return "Object class violation";
1308 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1309 return "Not allowed on non-leaf";
1310 case LDB_ERR_NOT_ALLOWED_ON_RDN:
1311 return "Not allowed on RDN";
1312 case LDB_ERR_ENTRY_ALREADY_EXISTS:
1313 return "Entry already exists";
1314 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1315 return "Object class mods prohibited";
1316 /* 70 RESERVED FOR CLDAP */
1317 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1318 return "Affects multiple DSAs";
1324 return "Unknown error";
1328 set backend specific opaque parameters
1330 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1332 struct ldb_opaque *o;
1334 /* allow updating an existing value */
1335 for (o=ldb->opaque;o;o=o->next) {
1336 if (strcmp(o->name, name) == 0) {
1342 o = talloc(ldb, struct ldb_opaque);
1345 return LDB_ERR_OTHER;
1347 o->next = ldb->opaque;
1355 get a previously set opaque value
1357 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1359 struct ldb_opaque *o;
1360 for (o=ldb->opaque;o;o=o->next) {
1361 if (strcmp(o->name, name) == 0) {