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 initialise a ldb context
52 The mem_ctx is required
53 The event_ctx is required
55 struct ldb_context *ldb_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev_ctx)
57 struct ldb_context *ldb;
60 ldb = talloc_zero(mem_ctx, struct ldb_context);
61 /* FIXME: Hack a new event context so that CMD line utilities work
62 * until we have them all converted */
64 ev_ctx = tevent_context_init(talloc_autofree_context());
67 ret = ldb_setup_wellknown_attributes(ldb);
73 ldb_set_utf8_default(ldb);
74 ldb_set_create_perms(ldb, 0666);
75 ldb_set_modules_dir(ldb, LDB_MODULESDIR);
76 ldb_set_event_context(ldb, ev_ctx);
78 /* TODO: get timeout from options if available there */
79 ldb->default_timeout = 300; /* set default to 5 minutes */
81 talloc_set_destructor((TALLOC_CTX *)ldb, ldb_context_destructor);
87 try to autodetect a basedn if none specified. This fixes one of my
88 pet hates about ldapsearch, which is that you have to get a long,
89 complex basedn right to make any use of it.
91 void ldb_set_default_dns(struct ldb_context *ldb)
95 struct ldb_result *res;
96 struct ldb_dn *tmp_dn=NULL;
97 static const char *attrs[] = {
98 "rootDomainNamingContext",
99 "configurationNamingContext",
100 "schemaNamingContext",
101 "defaultNamingContext",
105 tmp_ctx = talloc_new(ldb);
106 ret = ldb_search(ldb, tmp_ctx, &res, ldb_dn_new(tmp_ctx, ldb, NULL),
107 LDB_SCOPE_BASE, attrs, "(objectClass=*)");
108 if (ret != LDB_SUCCESS) {
109 talloc_free(tmp_ctx);
113 if (res->count != 1) {
114 talloc_free(tmp_ctx);
118 if (!ldb_get_opaque(ldb, "rootDomainNamingContext")) {
119 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
120 "rootDomainNamingContext");
121 ldb_set_opaque(ldb, "rootDomainNamingContext", tmp_dn);
124 if (!ldb_get_opaque(ldb, "configurationNamingContext")) {
125 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
126 "configurationNamingContext");
127 ldb_set_opaque(ldb, "configurationNamingContext", tmp_dn);
130 if (!ldb_get_opaque(ldb, "schemaNamingContext")) {
131 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
132 "schemaNamingContext");
133 ldb_set_opaque(ldb, "schemaNamingContext", tmp_dn);
136 if (!ldb_get_opaque(ldb, "defaultNamingContext")) {
137 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
138 "defaultNamingContext");
139 ldb_set_opaque(ldb, "defaultNamingContext", tmp_dn);
142 talloc_free(tmp_ctx);
145 struct ldb_dn *ldb_get_root_basedn(struct ldb_context *ldb)
147 void *opaque = ldb_get_opaque(ldb, "rootDomainNamingContext");
148 return talloc_get_type(opaque, struct ldb_dn);
151 struct ldb_dn *ldb_get_config_basedn(struct ldb_context *ldb)
153 void *opaque = ldb_get_opaque(ldb, "configurationNamingContext");
154 return talloc_get_type(opaque, struct ldb_dn);
157 struct ldb_dn *ldb_get_schema_basedn(struct ldb_context *ldb)
159 void *opaque = ldb_get_opaque(ldb, "schemaNamingContext");
160 return talloc_get_type(opaque, struct ldb_dn);
163 struct ldb_dn *ldb_get_default_basedn(struct ldb_context *ldb)
165 void *opaque = ldb_get_opaque(ldb, "defaultNamingContext");
166 return talloc_get_type(opaque, struct ldb_dn);
170 connect to a database. The URL can either be one of the following forms
174 flags is made up of LDB_FLG_*
176 the options are passed uninterpreted to the backend, and are
179 int ldb_connect(struct ldb_context *ldb, const char *url,
180 unsigned int flags, const char *options[])
184 /* We seem to need to do this here, or else some utilities don't
185 * get ldb backends */
189 url2 = talloc_strdup(ldb, url);
192 return LDB_ERR_OPERATIONS_ERROR;
194 ret = ldb_set_opaque(ldb, "ldb_url", talloc_strdup(ldb, url2));
195 if (ret != LDB_SUCCESS) {
199 ret = ldb_connect_backend(ldb, url, options, &ldb->modules);
200 if (ret != LDB_SUCCESS) {
204 if (ldb_load_modules(ldb, options) != LDB_SUCCESS) {
205 ldb_debug(ldb, LDB_DEBUG_FATAL,
206 "Unable to load modules for %s: %s\n",
207 url, ldb_errstring(ldb));
208 return LDB_ERR_OTHER;
211 /* set the default base dn */
212 ldb_set_default_dns(ldb);
217 void ldb_set_errstring(struct ldb_context *ldb, const char *err_string)
219 if (ldb->err_string) {
220 talloc_free(ldb->err_string);
222 ldb->err_string = talloc_strdup(ldb, err_string);
225 void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
228 char *old_string = NULL;
230 if (ldb->err_string) {
231 old_string = ldb->err_string;
234 va_start(ap, format);
235 ldb->err_string = talloc_vasprintf(ldb, format, ap);
237 talloc_free(old_string);
240 void ldb_reset_err_string(struct ldb_context *ldb)
242 if (ldb->err_string) {
243 talloc_free(ldb->err_string);
244 ldb->err_string = NULL;
248 #define FIRST_OP(ldb, op) do { \
249 module = ldb->modules; \
250 while (module && module->ops->op == NULL) module = module->next; \
251 if (module == NULL) { \
252 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
253 return LDB_ERR_OPERATIONS_ERROR; \
260 int ldb_transaction_start(struct ldb_context *ldb)
262 struct ldb_module *module;
265 ldb_debug(ldb, LDB_DEBUG_TRACE,
266 "start ldb transaction (nesting: %d)",
267 ldb->transaction_active);
269 /* explicit transaction active, count nested requests */
270 if (ldb->transaction_active) {
271 ldb->transaction_active++;
275 /* start a new transaction */
276 ldb->transaction_active++;
278 FIRST_OP(ldb, start_transaction);
280 ldb_reset_err_string(ldb);
282 status = module->ops->start_transaction(module);
283 if (status != LDB_SUCCESS) {
284 if (ldb->err_string == NULL) {
285 /* no error string was setup by the backend */
286 ldb_asprintf_errstring(ldb,
287 "ldb transaction start: %s (%d)",
288 ldb_strerror(status),
298 int ldb_transaction_commit(struct ldb_context *ldb)
300 struct ldb_module *module;
303 ldb->transaction_active--;
305 ldb_debug(ldb, LDB_DEBUG_TRACE,
306 "commit ldb transaction (nesting: %d)",
307 ldb->transaction_active);
309 /* commit only when all nested transactions are complete */
310 if (ldb->transaction_active > 0) {
314 if (ldb->transaction_active < 0) {
315 ldb_debug(ldb, LDB_DEBUG_FATAL,
316 "commit called but no ldb transactions are active!");
317 ldb->transaction_active = 0;
318 return LDB_ERR_OPERATIONS_ERROR;
321 FIRST_OP(ldb, end_transaction);
323 ldb_reset_err_string(ldb);
325 status = module->ops->end_transaction(module);
326 if (status != LDB_SUCCESS) {
327 if (ldb->err_string == NULL) {
328 /* no error string was setup by the backend */
329 ldb_asprintf_errstring(ldb,
330 "ldb transaction commit: %s (%d)",
331 ldb_strerror(status),
341 int ldb_transaction_cancel(struct ldb_context *ldb)
343 struct ldb_module *module;
346 ldb->transaction_active--;
348 ldb_debug(ldb, LDB_DEBUG_TRACE,
349 "cancel ldb transaction (nesting: %d)",
350 ldb->transaction_active);
352 /* really cancel only if all nested transactions are complete */
353 if (ldb->transaction_active > 0) {
357 if (ldb->transaction_active < 0) {
358 ldb_debug(ldb, LDB_DEBUG_FATAL,
359 "commit called but no ldb transactions are active!");
360 ldb->transaction_active = 0;
361 return LDB_ERR_OPERATIONS_ERROR;
364 FIRST_OP(ldb, del_transaction);
366 status = module->ops->del_transaction(module);
367 if (status != LDB_SUCCESS) {
368 if (ldb->err_string == NULL) {
369 /* no error string was setup by the backend */
370 ldb_asprintf_errstring(ldb,
371 "ldb transaction cancel: %s (%d)",
372 ldb_strerror(status),
379 /* autostarts a transacion if none active */
380 static int ldb_autotransaction_request(struct ldb_context *ldb,
381 struct ldb_request *req)
385 ret = ldb_transaction_start(ldb);
386 if (ret != LDB_SUCCESS) {
390 ret = ldb_request(ldb, req);
391 if (ret == LDB_SUCCESS) {
392 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
395 if (ret == LDB_SUCCESS) {
396 return ldb_transaction_commit(ldb);
398 ldb_transaction_cancel(ldb);
400 if (ldb->err_string == NULL) {
401 /* no error string was setup by the backend */
402 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
408 int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
410 struct tevent_context *ev;
414 return LDB_ERR_UNAVAILABLE;
417 if (handle->state == LDB_ASYNC_DONE) {
418 return handle->status;
421 ev = ldb_get_event_context(handle->ldb);
423 return LDB_ERR_OPERATIONS_ERROR;
428 ret = tevent_loop_once(ev);
430 return LDB_ERR_OPERATIONS_ERROR;
432 if (handle->state == LDB_ASYNC_DONE ||
433 handle->status != LDB_SUCCESS) {
434 return handle->status;
439 while (handle->state != LDB_ASYNC_DONE) {
440 ret = tevent_loop_once(ev);
442 return LDB_ERR_OPERATIONS_ERROR;
444 if (handle->status != LDB_SUCCESS) {
445 return handle->status;
448 return handle->status;
454 /* set the specified timeout or, if timeout is 0 set the default timeout */
455 int ldb_set_timeout(struct ldb_context *ldb,
456 struct ldb_request *req,
459 if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
462 req->timeout = timeout;
464 req->timeout = ldb->default_timeout;
466 req->starttime = time(NULL);
471 /* calculates the new timeout based on the previous starttime and timeout */
472 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb,
473 struct ldb_request *oldreq,
474 struct ldb_request *newreq)
476 if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
478 if (oldreq == NULL) {
479 return ldb_set_timeout(ldb, newreq, 0);
482 newreq->starttime = oldreq->starttime;
483 newreq->timeout = oldreq->timeout;
490 set the permissions for new files to be passed to open() in
491 backends that use local files
493 void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
495 ldb->create_perms = perms;
498 unsigned int ldb_get_create_perms(struct ldb_context *ldb)
500 return ldb->create_perms;
503 void ldb_set_event_context(struct ldb_context *ldb, struct tevent_context *ev)
508 struct tevent_context * ldb_get_event_context(struct ldb_context *ldb)
513 void ldb_request_set_state(struct ldb_request *req, int state)
515 req->handle->state = state;
518 int ldb_request_get_status(struct ldb_request *req)
520 return req->handle->status;
525 NOTE: the request must be a talloc context.
526 returns LDB_ERR_* on errors.
528 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
530 struct ldb_module *module;
533 if (req->callback == NULL) {
534 ldb_set_errstring(ldb, "Requests MUST define callbacks");
535 return LDB_ERR_UNWILLING_TO_PERFORM;
538 ldb_reset_err_string(ldb);
540 /* call the first module in the chain */
541 switch (req->operation) {
543 FIRST_OP(ldb, search);
544 ret = module->ops->search(module, req);
548 ret = module->ops->add(module, req);
551 FIRST_OP(ldb, modify);
552 ret = module->ops->modify(module, req);
556 ret = module->ops->del(module, req);
559 FIRST_OP(ldb, rename);
560 ret = module->ops->rename(module, req);
563 FIRST_OP(ldb, extended);
564 ret = module->ops->extended(module, req);
567 FIRST_OP(ldb, request);
568 ret = module->ops->request(module, req);
575 int ldb_request_done(struct ldb_request *req, int status)
577 req->handle->state = LDB_ASYNC_DONE;
578 req->handle->status = status;
583 search the database given a LDAP-like search expression
585 returns an LDB error code
587 Use talloc_free to free the ldb_message returned in 'res', if successful
590 int ldb_search_default_callback(struct ldb_request *req,
591 struct ldb_reply *ares)
593 struct ldb_result *res;
596 res = talloc_get_type(req->context, struct ldb_result);
599 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
601 if (ares->error != LDB_SUCCESS) {
602 return ldb_request_done(req, ares->error);
605 switch (ares->type) {
606 case LDB_REPLY_ENTRY:
607 res->msgs = talloc_realloc(res, res->msgs,
608 struct ldb_message *, res->count + 2);
610 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
613 res->msgs[res->count + 1] = NULL;
615 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
619 case LDB_REPLY_REFERRAL:
621 for (n = 0; res->refs[n]; n++) /*noop*/ ;
626 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
628 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
631 res->refs[n] = talloc_move(res->refs, &ares->referral);
632 res->refs[n + 1] = NULL;
636 /* TODO: we should really support controls on entries
637 * and referrals too! */
638 res->controls = talloc_move(res, &ares->controls);
640 /* this is the last message, and means the request is done */
641 /* we have to signal and eventual ldb_wait() waiting that the
642 * async request operation was completed */
643 return ldb_request_done(req, LDB_SUCCESS);
650 int ldb_op_default_callback(struct ldb_request *req, struct ldb_reply *ares)
655 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
658 if (ares->error != LDB_SUCCESS) {
661 return ldb_request_done(req, ret);
664 if (ares->type != LDB_REPLY_DONE) {
666 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
667 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
671 return ldb_request_done(req, LDB_SUCCESS);
674 int ldb_build_search_req_ex(struct ldb_request **ret_req,
675 struct ldb_context *ldb,
678 enum ldb_scope scope,
679 struct ldb_parse_tree *tree,
680 const char * const *attrs,
681 struct ldb_control **controls,
683 ldb_request_callback_t callback,
684 struct ldb_request *parent)
686 struct ldb_request *req;
690 req = talloc(mem_ctx, struct ldb_request);
693 return LDB_ERR_OPERATIONS_ERROR;
696 req->operation = LDB_SEARCH;
698 req->op.search.base = ldb_dn_new(req, ldb, NULL);
700 req->op.search.base = base;
702 req->op.search.scope = scope;
704 req->op.search.tree = tree;
705 if (req->op.search.tree == NULL) {
706 ldb_set_errstring(ldb, "'tree' can't be NULL");
708 return LDB_ERR_OPERATIONS_ERROR;
711 req->op.search.attrs = attrs;
712 req->controls = controls;
713 req->context = context;
714 req->callback = callback;
716 ldb_set_timeout_from_prev_req(ldb, parent, req);
718 req->handle = ldb_handle_new(req, ldb);
719 if (req->handle == NULL) {
721 return LDB_ERR_OPERATIONS_ERROR;
728 int ldb_build_search_req(struct ldb_request **ret_req,
729 struct ldb_context *ldb,
732 enum ldb_scope scope,
733 const char *expression,
734 const char * const *attrs,
735 struct ldb_control **controls,
737 ldb_request_callback_t callback,
738 struct ldb_request *parent)
740 struct ldb_parse_tree *tree;
743 tree = ldb_parse_tree(mem_ctx, expression);
745 ldb_set_errstring(ldb, "Unable to parse search expression");
746 return LDB_ERR_OPERATIONS_ERROR;
749 ret = ldb_build_search_req_ex(ret_req, ldb, mem_ctx, base,
750 scope, tree, attrs, controls,
751 context, callback, parent);
752 if (ret == LDB_SUCCESS) {
753 talloc_steal(*ret_req, tree);
758 int ldb_build_add_req(struct ldb_request **ret_req,
759 struct ldb_context *ldb,
761 const struct ldb_message *message,
762 struct ldb_control **controls,
764 ldb_request_callback_t callback,
765 struct ldb_request *parent)
767 struct ldb_request *req;
771 req = talloc(mem_ctx, struct ldb_request);
773 ldb_set_errstring(ldb, "Out of Memory");
774 return LDB_ERR_OPERATIONS_ERROR;
777 req->operation = LDB_ADD;
778 req->op.add.message = message;
779 req->controls = controls;
780 req->context = context;
781 req->callback = callback;
783 ldb_set_timeout_from_prev_req(ldb, parent, req);
785 req->handle = ldb_handle_new(req, ldb);
786 if (req->handle == NULL) {
788 return LDB_ERR_OPERATIONS_ERROR;
796 int ldb_build_mod_req(struct ldb_request **ret_req,
797 struct ldb_context *ldb,
799 const struct ldb_message *message,
800 struct ldb_control **controls,
802 ldb_request_callback_t callback,
803 struct ldb_request *parent)
805 struct ldb_request *req;
809 req = talloc(mem_ctx, struct ldb_request);
811 ldb_set_errstring(ldb, "Out of Memory");
812 return LDB_ERR_OPERATIONS_ERROR;
815 req->operation = LDB_MODIFY;
816 req->op.mod.message = message;
817 req->controls = controls;
818 req->context = context;
819 req->callback = callback;
821 ldb_set_timeout_from_prev_req(ldb, parent, req);
823 req->handle = ldb_handle_new(req, ldb);
824 if (req->handle == NULL) {
826 return LDB_ERR_OPERATIONS_ERROR;
834 int ldb_build_del_req(struct ldb_request **ret_req,
835 struct ldb_context *ldb,
838 struct ldb_control **controls,
840 ldb_request_callback_t callback,
841 struct ldb_request *parent)
843 struct ldb_request *req;
847 req = talloc(mem_ctx, struct ldb_request);
849 ldb_set_errstring(ldb, "Out of Memory");
850 return LDB_ERR_OPERATIONS_ERROR;
853 req->operation = LDB_DELETE;
855 req->controls = controls;
856 req->context = context;
857 req->callback = callback;
859 ldb_set_timeout_from_prev_req(ldb, parent, req);
861 req->handle = ldb_handle_new(req, ldb);
862 if (req->handle == NULL) {
864 return LDB_ERR_OPERATIONS_ERROR;
872 int ldb_build_rename_req(struct ldb_request **ret_req,
873 struct ldb_context *ldb,
875 struct ldb_dn *olddn,
876 struct ldb_dn *newdn,
877 struct ldb_control **controls,
879 ldb_request_callback_t callback,
880 struct ldb_request *parent)
882 struct ldb_request *req;
886 req = talloc(mem_ctx, struct ldb_request);
888 ldb_set_errstring(ldb, "Out of Memory");
889 return LDB_ERR_OPERATIONS_ERROR;
892 req->operation = LDB_RENAME;
893 req->op.rename.olddn = olddn;
894 req->op.rename.newdn = newdn;
895 req->controls = controls;
896 req->context = context;
897 req->callback = callback;
899 ldb_set_timeout_from_prev_req(ldb, parent, req);
901 req->handle = ldb_handle_new(req, ldb);
902 if (req->handle == NULL) {
904 return LDB_ERR_OPERATIONS_ERROR;
912 int ldb_extended_default_callback(struct ldb_request *req,
913 struct ldb_reply *ares)
915 struct ldb_result *res;
917 res = talloc_get_type(req->context, struct ldb_result);
920 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
922 if (ares->error != LDB_SUCCESS) {
923 return ldb_request_done(req, ares->error);
926 if (ares->type == LDB_REPLY_DONE) {
928 /* TODO: we should really support controls on entries and referrals too! */
929 res->extended = talloc_move(res, &ares->response);
930 res->controls = talloc_move(res, &ares->controls);
933 return ldb_request_done(req, LDB_SUCCESS);
937 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
938 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
941 int ldb_build_extended_req(struct ldb_request **ret_req,
942 struct ldb_context *ldb,
946 struct ldb_control **controls,
948 ldb_request_callback_t callback,
949 struct ldb_request *parent)
951 struct ldb_request *req;
955 req = talloc(mem_ctx, struct ldb_request);
957 ldb_set_errstring(ldb, "Out of Memory");
958 return LDB_ERR_OPERATIONS_ERROR;
961 req->operation = LDB_EXTENDED;
962 req->op.extended.oid = oid;
963 req->op.extended.data = data;
964 req->controls = controls;
965 req->context = context;
966 req->callback = callback;
968 ldb_set_timeout_from_prev_req(ldb, parent, req);
970 req->handle = ldb_handle_new(req, ldb);
971 if (req->handle == NULL) {
973 return LDB_ERR_OPERATIONS_ERROR;
981 int ldb_extended(struct ldb_context *ldb,
984 struct ldb_result **_res)
986 struct ldb_request *req;
988 struct ldb_result *res;
992 res = talloc_zero(ldb, struct ldb_result);
994 return LDB_ERR_OPERATIONS_ERROR;
997 ret = ldb_build_extended_req(&req, ldb, ldb,
999 res, ldb_extended_default_callback,
1001 if (ret != LDB_SUCCESS) goto done;
1003 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1005 ret = ldb_request(ldb, req);
1007 if (ret == LDB_SUCCESS) {
1008 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1014 if (ret != LDB_SUCCESS) {
1023 note that ldb_search() will automatically replace a NULL 'base' value
1024 with the defaultNamingContext from the rootDSE if available.
1026 int ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1027 struct ldb_result **result, struct ldb_dn *base,
1028 enum ldb_scope scope, const char * const *attrs,
1029 const char *exp_fmt, ...)
1031 struct ldb_request *req;
1032 struct ldb_result *res;
1041 res = talloc_zero(mem_ctx, struct ldb_result);
1043 return LDB_ERR_OPERATIONS_ERROR;
1047 va_start(ap, exp_fmt);
1048 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
1053 return LDB_ERR_OPERATIONS_ERROR;
1057 ret = ldb_build_search_req(&req, ldb, mem_ctx,
1058 base?base:ldb_get_default_basedn(ldb),
1064 ldb_search_default_callback,
1067 if (ret != LDB_SUCCESS) goto done;
1069 ret = ldb_request(ldb, req);
1071 if (ret == LDB_SUCCESS) {
1072 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1076 if (ret != LDB_SUCCESS) {
1081 talloc_free(expression);
1089 add a record to the database. Will fail if a record with the given class
1090 and key already exists
1092 int ldb_add(struct ldb_context *ldb,
1093 const struct ldb_message *message)
1095 struct ldb_request *req;
1098 ret = ldb_msg_sanity_check(ldb, message);
1099 if (ret != LDB_SUCCESS) {
1103 ret = ldb_build_add_req(&req, ldb, ldb,
1107 ldb_op_default_callback,
1110 if (ret != LDB_SUCCESS) return ret;
1112 /* do request and autostart a transaction */
1113 ret = ldb_autotransaction_request(ldb, req);
1120 modify the specified attributes of a record
1122 int ldb_modify(struct ldb_context *ldb,
1123 const struct ldb_message *message)
1125 struct ldb_request *req;
1128 ret = ldb_msg_sanity_check(ldb, message);
1129 if (ret != LDB_SUCCESS) {
1133 ret = ldb_build_mod_req(&req, ldb, ldb,
1137 ldb_op_default_callback,
1140 if (ret != LDB_SUCCESS) return ret;
1142 /* do request and autostart a transaction */
1143 ret = ldb_autotransaction_request(ldb, req);
1151 delete a record from the database
1153 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1155 struct ldb_request *req;
1158 ret = ldb_build_del_req(&req, ldb, ldb,
1162 ldb_op_default_callback,
1165 if (ret != LDB_SUCCESS) return ret;
1167 /* do request and autostart a transaction */
1168 ret = ldb_autotransaction_request(ldb, req);
1175 rename a record in the database
1177 int ldb_rename(struct ldb_context *ldb,
1178 struct ldb_dn *olddn, struct ldb_dn *newdn)
1180 struct ldb_request *req;
1183 ret = ldb_build_rename_req(&req, ldb, ldb,
1188 ldb_op_default_callback,
1191 if (ret != LDB_SUCCESS) return ret;
1193 /* do request and autostart a transaction */
1194 ret = ldb_autotransaction_request(ldb, req);
1202 return the global sequence number
1204 int ldb_sequence_number(struct ldb_context *ldb,
1205 enum ldb_sequence_type type, uint64_t *seq_num)
1207 struct ldb_seqnum_request *seq;
1208 struct ldb_seqnum_result *seqr;
1209 struct ldb_result *res;
1210 TALLOC_CTX *tmp_ctx;
1215 tmp_ctx = talloc_zero(ldb, struct ldb_request);
1216 if (tmp_ctx == NULL) {
1217 ldb_set_errstring(ldb, "Out of Memory");
1218 return LDB_ERR_OPERATIONS_ERROR;
1220 seq = talloc_zero(tmp_ctx, struct ldb_seqnum_request);
1222 ldb_set_errstring(ldb, "Out of Memory");
1223 ret = LDB_ERR_OPERATIONS_ERROR;
1228 ret = ldb_extended(ldb, LDB_EXTENDED_SEQUENCE_NUMBER, seq, &res);
1229 if (ret != LDB_SUCCESS) {
1232 talloc_steal(tmp_ctx, res);
1234 if (strcmp(LDB_EXTENDED_SEQUENCE_NUMBER, res->extended->oid) != 0) {
1235 ldb_set_errstring(ldb, "Invalid OID in reply");
1236 ret = LDB_ERR_OPERATIONS_ERROR;
1239 seqr = talloc_get_type(res->extended->data,
1240 struct ldb_seqnum_result);
1241 *seq_num = seqr->seq_num;
1244 talloc_free(tmp_ctx);
1249 return extended error information
1251 const char *ldb_errstring(struct ldb_context *ldb)
1253 if (ldb->err_string) {
1254 return ldb->err_string;
1261 return a string explaining what a ldb error constant meancs
1263 const char *ldb_strerror(int ldb_err)
1268 case LDB_ERR_OPERATIONS_ERROR:
1269 return "Operations error";
1270 case LDB_ERR_PROTOCOL_ERROR:
1271 return "Protocol error";
1272 case LDB_ERR_TIME_LIMIT_EXCEEDED:
1273 return "Time limit exceeded";
1274 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1275 return "Size limit exceeded";
1276 case LDB_ERR_COMPARE_FALSE:
1277 return "Compare false";
1278 case LDB_ERR_COMPARE_TRUE:
1279 return "Compare true";
1280 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1281 return "Auth method not supported";
1282 case LDB_ERR_STRONG_AUTH_REQUIRED:
1283 return "Strong auth required";
1285 case LDB_ERR_REFERRAL:
1286 return "Referral error";
1287 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1288 return "Admin limit exceeded";
1289 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1290 return "Unsupported critical extension";
1291 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1292 return "Confidentiality required";
1293 case LDB_ERR_SASL_BIND_IN_PROGRESS:
1294 return "SASL bind in progress";
1295 case LDB_ERR_NO_SUCH_ATTRIBUTE:
1296 return "No such attribute";
1297 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1298 return "Undefined attribute type";
1299 case LDB_ERR_INAPPROPRIATE_MATCHING:
1300 return "Inappropriate matching";
1301 case LDB_ERR_CONSTRAINT_VIOLATION:
1302 return "Constraint violation";
1303 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1304 return "Attribute or value exists";
1305 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1306 return "Invalid attribute syntax";
1308 case LDB_ERR_NO_SUCH_OBJECT:
1309 return "No such object";
1310 case LDB_ERR_ALIAS_PROBLEM:
1311 return "Alias problem";
1312 case LDB_ERR_INVALID_DN_SYNTAX:
1313 return "Invalid DN syntax";
1315 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1316 return "Alias dereferencing problem";
1318 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1319 return "Inappropriate authentication";
1320 case LDB_ERR_INVALID_CREDENTIALS:
1321 return "Invalid credentials";
1322 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1323 return "insufficient access rights";
1326 case LDB_ERR_UNAVAILABLE:
1327 return "Unavailable";
1328 case LDB_ERR_UNWILLING_TO_PERFORM:
1329 return "Unwilling to perform";
1330 case LDB_ERR_LOOP_DETECT:
1331 return "Loop detect";
1333 case LDB_ERR_NAMING_VIOLATION:
1334 return "Naming violation";
1335 case LDB_ERR_OBJECT_CLASS_VIOLATION:
1336 return "Object class violation";
1337 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1338 return "Not allowed on non-leaf";
1339 case LDB_ERR_NOT_ALLOWED_ON_RDN:
1340 return "Not allowed on RDN";
1341 case LDB_ERR_ENTRY_ALREADY_EXISTS:
1342 return "Entry already exists";
1343 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1344 return "Object class mods prohibited";
1345 /* 70 RESERVED FOR CLDAP */
1346 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1347 return "Affects multiple DSAs";
1353 return "Unknown error";
1357 set backend specific opaque parameters
1359 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1361 struct ldb_opaque *o;
1363 /* allow updating an existing value */
1364 for (o=ldb->opaque;o;o=o->next) {
1365 if (strcmp(o->name, name) == 0) {
1371 o = talloc(ldb, struct ldb_opaque);
1374 return LDB_ERR_OTHER;
1376 o->next = ldb->opaque;
1384 get a previously set opaque value
1386 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1388 struct ldb_opaque *o;
1389 for (o=ldb->opaque;o;o=o->next) {
1390 if (strcmp(o->name, name) == 0) {