4 Copyright (C) Andrew Tridgell 2004
5 Copyright (C) Simo Sorce 2005-2006
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 optional
41 struct ldb_context *ldb_init(void *mem_ctx)
43 struct ldb_context *ldb = talloc_zero(mem_ctx, struct ldb_context);
46 ret = ldb_setup_wellknown_attributes(ldb);
52 ldb_set_utf8_default(ldb);
53 ldb_set_create_perms(ldb, 0666);
58 static struct ldb_backend {
60 ldb_connect_fn connect_fn;
61 struct ldb_backend *prev, *next;
62 } *ldb_backends = NULL;
65 static ldb_connect_fn ldb_find_backend(const char *url)
67 struct ldb_backend *backend;
69 for (backend = ldb_backends; backend; backend = backend->next) {
70 if (strncmp(backend->name, url, strlen(backend->name)) == 0) {
71 return backend->connect_fn;
79 register a new ldb backend
81 int ldb_register_backend(const char *url_prefix, ldb_connect_fn connectfn)
83 struct ldb_backend *backend = talloc(talloc_autofree_context(), struct ldb_backend);
85 if (ldb_find_backend(url_prefix)) {
89 /* Maybe check for duplicity here later on? */
91 backend->name = talloc_strdup(backend, url_prefix);
92 backend->connect_fn = connectfn;
93 DLIST_ADD(ldb_backends, backend);
99 Return the ldb module form of a database. The URL can either be one of the following forms
103 flags is made up of LDB_FLG_*
105 the options are passed uninterpreted to the backend, and are
108 This allows modules to get at only the backend module, for example where a module
109 may wish to direct certain requests at a particular backend.
111 int ldb_connect_backend(struct ldb_context *ldb, const char *url, const char *options[],
112 struct ldb_module **backend_module)
118 if (strchr(url, ':') != NULL) {
119 backend = talloc_strndup(ldb, url, strchr(url, ':')-url);
122 backend = talloc_strdup(ldb, "tdb");
125 fn = ldb_find_backend(backend);
128 if (ldb_try_load_dso(ldb, backend) == 0) {
129 fn = ldb_find_backend(backend);
133 talloc_free(backend);
136 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to find backend for '%s'\n", url);
137 return LDB_ERR_OTHER;
140 ret = fn(ldb, url, ldb->flags, options, backend_module);
142 if (ret != LDB_SUCCESS) {
143 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to '%s'\n", url);
150 try to autodetect a basedn if none specified. This fixes one of my
151 pet hates about ldapsearch, which is that you have to get a long,
152 complex basedn right to make any use of it.
154 void ldb_set_default_dns(struct ldb_context *ldb)
158 struct ldb_result *res;
159 struct ldb_dn *tmp_dn=NULL;
160 static const char *attrs[] = {
161 "rootDomainNamingContext",
162 "configurationNamingContext",
163 "schemaNamingContext",
164 "defaultNamingContext",
168 tmp_ctx = talloc_new(ldb);
169 ret = ldb_search(ldb, ldb_dn_new(tmp_ctx, ldb, NULL), LDB_SCOPE_BASE,
170 "(objectClass=*)", attrs, &res);
171 if (ret == LDB_SUCCESS) {
172 if (res->count == 1) {
173 if (!ldb_get_opaque(ldb, "rootDomainNamingContext")) {
174 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "rootDomainNamingContext");
175 ldb_set_opaque(ldb, "rootDomainNamingContext", tmp_dn);
178 if (!ldb_get_opaque(ldb, "configurationNamingContext")) {
179 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "configurationNamingContext");
180 ldb_set_opaque(ldb, "configurationNamingContext", tmp_dn);
183 if (!ldb_get_opaque(ldb, "schemaNamingContext")) {
184 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "schemaNamingContext");
185 ldb_set_opaque(ldb, "schemaNamingContext", tmp_dn);
188 if (!ldb_get_opaque(ldb, "defaultNamingContext")) {
189 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "defaultNamingContext");
190 ldb_set_opaque(ldb, "defaultNamingContext", tmp_dn);
196 talloc_free(tmp_ctx);
199 struct ldb_dn *ldb_get_root_basedn(struct ldb_context *ldb)
201 return talloc_get_type(ldb_get_opaque(ldb, "rootDomainNamingContext"), struct ldb_dn);
204 struct ldb_dn *ldb_get_config_basedn(struct ldb_context *ldb)
206 return talloc_get_type(ldb_get_opaque(ldb, "configurationNamingContext"), struct ldb_dn);
209 struct ldb_dn *ldb_get_schema_basedn(struct ldb_context *ldb)
211 return talloc_get_type(ldb_get_opaque(ldb, "schemaNamingContext"), struct ldb_dn);
214 struct ldb_dn *ldb_get_default_basedn(struct ldb_context *ldb)
216 return talloc_get_type(ldb_get_opaque(ldb, "defaultNamingContext"), struct ldb_dn);
220 connect to a database. The URL can either be one of the following forms
224 flags is made up of LDB_FLG_*
226 the options are passed uninterpreted to the backend, and are
229 int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[])
233 /* We seem to need to do this here, or else some utilities don't get ldb backends */
238 url2 = talloc_strdup(ldb, url);
241 return LDB_ERR_OPERATIONS_ERROR;
243 ret = ldb_set_opaque(ldb, "ldb_url", talloc_strdup(ldb, url2));
244 if (ret != LDB_SUCCESS) {
248 ret = ldb_connect_backend(ldb, url, options, &ldb->modules);
249 if (ret != LDB_SUCCESS) {
253 if (ldb_load_modules(ldb, options) != LDB_SUCCESS) {
254 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to load modules for %s: %s\n",
255 url, ldb_errstring(ldb));
256 return LDB_ERR_OTHER;
259 /* TODO: get timeout from options if available there */
260 ldb->default_timeout = 300; /* set default to 5 minutes */
262 /* set the default base dn */
263 ldb_set_default_dns(ldb);
268 void ldb_set_errstring(struct ldb_context *ldb, const char *err_string)
270 if (ldb->err_string) {
271 talloc_free(ldb->err_string);
273 ldb->err_string = talloc_strdup(ldb, err_string);
276 void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
280 if (ldb->err_string) {
281 talloc_free(ldb->err_string);
284 va_start(ap, format);
285 ldb->err_string = talloc_vasprintf(ldb, format, ap);
289 void ldb_reset_err_string(struct ldb_context *ldb)
291 if (ldb->err_string) {
292 talloc_free(ldb->err_string);
293 ldb->err_string = NULL;
297 #define FIRST_OP(ldb, op) do { \
298 module = ldb->modules; \
299 while (module && module->ops->op == NULL) module = module->next; \
300 if (module == NULL) { \
301 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
302 return LDB_ERR_OPERATIONS_ERROR; \
309 static int ldb_transaction_start_internal(struct ldb_context *ldb)
311 struct ldb_module *module;
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 static int ldb_transaction_commit_internal(struct ldb_context *ldb)
335 struct ldb_module *module;
337 FIRST_OP(ldb, end_transaction);
339 ldb_reset_err_string(ldb);
341 status = module->ops->end_transaction(module);
342 if (status != LDB_SUCCESS) {
343 if (ldb->err_string == NULL) {
344 /* no error string was setup by the backend */
345 ldb_asprintf_errstring(ldb,
346 "ldb transaction commit: %s (%d)",
347 ldb_strerror(status),
357 static int ldb_transaction_cancel_internal(struct ldb_context *ldb)
359 struct ldb_module *module;
361 FIRST_OP(ldb, del_transaction);
363 status = module->ops->del_transaction(module);
364 if (status != LDB_SUCCESS) {
365 if (ldb->err_string == NULL) {
366 /* no error string was setup by the backend */
367 ldb_asprintf_errstring(ldb,
368 "ldb transaction cancel: %s (%d)",
369 ldb_strerror(status),
376 int ldb_transaction_start(struct ldb_context *ldb)
378 /* disable autotransactions */
379 ldb->transaction_active++;
381 return ldb_transaction_start_internal(ldb);
384 int ldb_transaction_commit(struct ldb_context *ldb)
386 /* renable autotransactions (when we reach 0) */
387 if (ldb->transaction_active > 0)
388 ldb->transaction_active--;
390 return ldb_transaction_commit_internal(ldb);
393 int ldb_transaction_cancel(struct ldb_context *ldb)
395 /* renable autotransactions (when we reach 0) */
396 if (ldb->transaction_active > 0)
397 ldb->transaction_active--;
399 return ldb_transaction_cancel_internal(ldb);
402 static int ldb_autotransaction_start(struct ldb_context *ldb)
404 /* explicit transaction active, ignore autotransaction request */
405 if (ldb->transaction_active)
408 return ldb_transaction_start_internal(ldb);
411 static int ldb_autotransaction_commit(struct ldb_context *ldb)
413 /* explicit transaction active, ignore autotransaction request */
414 if (ldb->transaction_active)
417 return ldb_transaction_commit_internal(ldb);
420 static int ldb_autotransaction_cancel(struct ldb_context *ldb)
422 /* explicit transaction active, ignore autotransaction request */
423 if (ldb->transaction_active)
426 return ldb_transaction_cancel_internal(ldb);
429 /* autostarts a transacion if none active */
430 static int ldb_autotransaction_request(struct ldb_context *ldb, struct ldb_request *req)
434 ret = ldb_autotransaction_start(ldb);
435 if (ret != LDB_SUCCESS) {
439 ret = ldb_request(ldb, req);
440 if (ret == LDB_SUCCESS) {
441 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
444 if (ret == LDB_SUCCESS) {
445 return ldb_autotransaction_commit(ldb);
447 ldb_autotransaction_cancel(ldb);
449 if (ldb->err_string == NULL) {
450 /* no error string was setup by the backend */
451 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
457 int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
463 return handle->module->ops->wait(handle, type);
466 /* set the specified timeout or, if timeout is 0 set the default timeout */
467 /* timeout == -1 means no timeout */
468 int ldb_set_timeout(struct ldb_context *ldb, struct ldb_request *req, int timeout)
470 if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
473 req->timeout = timeout;
475 req->timeout = ldb->default_timeout;
477 req->starttime = time(NULL);
482 /* calculates the new timeout based on the previous starttime and timeout */
483 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb, struct ldb_request *oldreq, struct ldb_request *newreq)
487 if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
492 return ldb_set_timeout(ldb, newreq, 0);
494 if ((now - oldreq->starttime) > oldreq->timeout) {
495 return LDB_ERR_TIME_LIMIT_EXCEEDED;
497 newreq->starttime = oldreq->starttime;
498 newreq->timeout = oldreq->timeout - (now - oldreq->starttime);
505 set the permissions for new files to be passed to open() in
506 backends that use local files
508 void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
510 ldb->create_perms = perms;
515 NOTE: the request must be a talloc context.
516 returns LDB_ERR_* on errors.
518 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
520 struct ldb_module *module;
523 ldb_reset_err_string(ldb);
525 /* call the first module in the chain */
526 switch (req->operation) {
528 FIRST_OP(ldb, search);
529 ret = module->ops->search(module, req);
533 ret = module->ops->add(module, req);
536 FIRST_OP(ldb, modify);
537 ret = module->ops->modify(module, req);
541 ret = module->ops->del(module, req);
544 FIRST_OP(ldb, rename);
545 ret = module->ops->rename(module, req);
548 FIRST_OP(ldb, extended);
549 ret = module->ops->extended(module, req);
551 case LDB_SEQUENCE_NUMBER:
552 FIRST_OP(ldb, sequence_number);
553 ret = module->ops->sequence_number(module, req);
556 FIRST_OP(ldb, request);
557 ret = module->ops->request(module, req);
565 search the database given a LDAP-like search expression
567 returns an LDB error code
569 Use talloc_free to free the ldb_message returned in 'res', if successful
572 int ldb_search_default_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
574 struct ldb_result *res;
578 ldb_set_errstring(ldb, "NULL Context in callback");
579 return LDB_ERR_OPERATIONS_ERROR;
582 res = talloc_get_type(context, struct ldb_result);
585 ldb_set_errstring(ldb, "NULL res or ares in callback");
589 switch (ares->type) {
590 case LDB_REPLY_ENTRY:
591 res->msgs = talloc_realloc(res, res->msgs, struct ldb_message *, res->count + 2);
596 res->msgs[res->count + 1] = NULL;
598 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
601 case LDB_REPLY_REFERRAL:
603 for (n = 0; res->refs[n]; n++) /*noop*/ ;
608 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
613 res->refs[n] = talloc_move(res->refs, &ares->referral);
614 res->refs[n + 1] = NULL;
616 case LDB_REPLY_EXTENDED:
618 /* TODO: we should really support controls on entries and referrals too! */
619 res->controls = talloc_move(res, &ares->controls);
627 return LDB_ERR_OPERATIONS_ERROR;
630 int ldb_build_search_req(struct ldb_request **ret_req,
631 struct ldb_context *ldb,
634 enum ldb_scope scope,
635 const char *expression,
636 const char * const *attrs,
637 struct ldb_control **controls,
639 ldb_request_callback_t callback)
641 struct ldb_request *req;
645 req = talloc(mem_ctx, struct ldb_request);
647 ldb_set_errstring(ldb, "Out of Memory");
648 return LDB_ERR_OPERATIONS_ERROR;
651 req->operation = LDB_SEARCH;
653 req->op.search.base = ldb_dn_new(req, ldb, NULL);
655 req->op.search.base = base;
657 req->op.search.scope = scope;
659 req->op.search.tree = ldb_parse_tree(req, expression);
660 if (req->op.search.tree == NULL) {
661 ldb_set_errstring(ldb, "Unable to parse search expression");
663 return LDB_ERR_OPERATIONS_ERROR;
666 req->op.search.attrs = attrs;
667 req->controls = controls;
668 req->context = context;
669 req->callback = callback;
675 int ldb_build_add_req(struct ldb_request **ret_req,
676 struct ldb_context *ldb,
678 const struct ldb_message *message,
679 struct ldb_control **controls,
681 ldb_request_callback_t callback)
683 struct ldb_request *req;
687 req = talloc(mem_ctx, struct ldb_request);
689 ldb_set_errstring(ldb, "Out of Memory");
690 return LDB_ERR_OPERATIONS_ERROR;
693 req->operation = LDB_ADD;
694 req->op.add.message = message;
695 req->controls = controls;
696 req->context = context;
697 req->callback = callback;
704 int ldb_build_mod_req(struct ldb_request **ret_req,
705 struct ldb_context *ldb,
707 const struct ldb_message *message,
708 struct ldb_control **controls,
710 ldb_request_callback_t callback)
712 struct ldb_request *req;
716 req = talloc(mem_ctx, struct ldb_request);
718 ldb_set_errstring(ldb, "Out of Memory");
719 return LDB_ERR_OPERATIONS_ERROR;
722 req->operation = LDB_MODIFY;
723 req->op.mod.message = message;
724 req->controls = controls;
725 req->context = context;
726 req->callback = callback;
733 int ldb_build_del_req(struct ldb_request **ret_req,
734 struct ldb_context *ldb,
737 struct ldb_control **controls,
739 ldb_request_callback_t callback)
741 struct ldb_request *req;
745 req = talloc(mem_ctx, struct ldb_request);
747 ldb_set_errstring(ldb, "Out of Memory");
748 return LDB_ERR_OPERATIONS_ERROR;
751 req->operation = LDB_DELETE;
753 req->controls = controls;
754 req->context = context;
755 req->callback = callback;
762 int ldb_build_rename_req(struct ldb_request **ret_req,
763 struct ldb_context *ldb,
765 struct ldb_dn *olddn,
766 struct ldb_dn *newdn,
767 struct ldb_control **controls,
769 ldb_request_callback_t callback)
771 struct ldb_request *req;
775 req = talloc(mem_ctx, struct ldb_request);
777 ldb_set_errstring(ldb, "Out of Memory");
778 return LDB_ERR_OPERATIONS_ERROR;
781 req->operation = LDB_RENAME;
782 req->op.rename.olddn = olddn;
783 req->op.rename.newdn = newdn;
784 req->controls = controls;
785 req->context = context;
786 req->callback = callback;
793 int ldb_extended_default_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
795 struct ldb_result *res;
798 ldb_set_errstring(ldb, "NULL Context in callback");
799 return LDB_ERR_OPERATIONS_ERROR;
802 res = talloc_get_type(context, struct ldb_result);
804 ldb_set_errstring(ldb, "NULL res or ares in callback");
808 switch (ares->type) {
809 case LDB_REPLY_ENTRY:
810 case LDB_REPLY_REFERRAL:
812 ldb_set_errstring(ldb, "invalid ares type in callback");
814 case LDB_REPLY_EXTENDED:
815 /* TODO: we should really support controls on entries and referrals too! */
816 res->extended = talloc_move(res, &ares->response);
817 res->controls = talloc_move(res, &ares->controls);
825 return LDB_ERR_OPERATIONS_ERROR;
828 int ldb_build_extended_req(struct ldb_request **ret_req,
829 struct ldb_context *ldb,
833 struct ldb_control **controls,
835 ldb_request_callback_t callback)
837 struct ldb_request *req;
841 req = talloc(mem_ctx, struct ldb_request);
843 ldb_set_errstring(ldb, "Out of Memory");
844 return LDB_ERR_OPERATIONS_ERROR;
847 req->operation = LDB_EXTENDED;
848 req->op.extended.oid = oid;
849 req->op.extended.data = data;
850 req->controls = controls;
851 req->context = context;
852 req->callback = callback;
859 int ldb_extended(struct ldb_context *ldb,
862 struct ldb_result **_res)
864 struct ldb_request *req;
866 struct ldb_result *res;
870 res = talloc_zero(ldb, struct ldb_result);
872 return LDB_ERR_OPERATIONS_ERROR;
875 ret = ldb_build_extended_req(&req, ldb, ldb,
877 res, ldb_extended_default_callback);
878 if (ret != LDB_SUCCESS) goto done;
880 ldb_set_timeout(ldb, req, 0); /* use default timeout */
882 ret = ldb_request(ldb, req);
884 if (ret == LDB_SUCCESS) {
885 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
891 if (ret != LDB_SUCCESS) {
900 note that ldb_search() will automatically replace a NULL 'base' value with the
901 defaultNamingContext from the rootDSE if available.
903 int ldb_search(struct ldb_context *ldb,
905 enum ldb_scope scope,
906 const char *expression,
907 const char * const *attrs,
908 struct ldb_result **_res)
910 struct ldb_request *req;
912 struct ldb_result *res;
916 res = talloc_zero(ldb, struct ldb_result);
918 return LDB_ERR_OPERATIONS_ERROR;
921 ret = ldb_build_search_req(&req, ldb, ldb,
922 base?base:ldb_get_default_basedn(ldb),
928 ldb_search_default_callback);
930 if (ret != LDB_SUCCESS) goto done;
932 ldb_set_timeout(ldb, req, 0); /* use default timeout */
934 ret = ldb_request(ldb, req);
936 if (ret == LDB_SUCCESS) {
937 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
943 if (ret != LDB_SUCCESS) {
952 a useful search function where you can easily define the expression and that
953 takes a memory context where results are allocated
956 int ldb_search_exp_fmt(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_result **result,
957 struct ldb_dn *base, enum ldb_scope scope, const char * const *attrs,
958 const char *exp_fmt, ...)
960 struct ldb_result *res;
968 va_start(ap, exp_fmt);
969 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
973 return LDB_ERR_OPERATIONS_ERROR;
976 ret = ldb_search(ldb, base, scope, expression, attrs, &res);
978 if (ret == LDB_SUCCESS) {
979 talloc_steal(mem_ctx, res);
983 talloc_free(expression);
989 add a record to the database. Will fail if a record with the given class and key
992 int ldb_add(struct ldb_context *ldb,
993 const struct ldb_message *message)
995 struct ldb_request *req;
998 ret = ldb_msg_sanity_check(ldb, message);
999 if (ret != LDB_SUCCESS) {
1003 ret = ldb_build_add_req(&req, ldb, ldb,
1009 if (ret != LDB_SUCCESS) return ret;
1011 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1013 /* do request and autostart a transaction */
1014 ret = ldb_autotransaction_request(ldb, req);
1021 modify the specified attributes of a record
1023 int ldb_modify(struct ldb_context *ldb,
1024 const struct ldb_message *message)
1026 struct ldb_request *req;
1029 ret = ldb_msg_sanity_check(ldb, message);
1030 if (ret != LDB_SUCCESS) {
1034 ret = ldb_build_mod_req(&req, ldb, ldb,
1040 if (ret != LDB_SUCCESS) return ret;
1042 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1044 /* do request and autostart a transaction */
1045 ret = ldb_autotransaction_request(ldb, req);
1053 delete a record from the database
1055 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1057 struct ldb_request *req;
1060 ret = ldb_build_del_req(&req, ldb, ldb,
1066 if (ret != LDB_SUCCESS) return ret;
1068 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1070 /* do request and autostart a transaction */
1071 ret = ldb_autotransaction_request(ldb, req);
1078 rename a record in the database
1080 int ldb_rename(struct ldb_context *ldb, struct ldb_dn *olddn, struct ldb_dn *newdn)
1082 struct ldb_request *req;
1085 ret = ldb_build_rename_req(&req, ldb, ldb,
1092 if (ret != LDB_SUCCESS) return ret;
1094 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1096 /* do request and autostart a transaction */
1097 ret = ldb_autotransaction_request(ldb, req);
1105 return the global sequence number
1107 int ldb_sequence_number(struct ldb_context *ldb, enum ldb_sequence_type type, uint64_t *seq_num)
1109 struct ldb_request *req;
1112 req = talloc(ldb, struct ldb_request);
1114 ldb_set_errstring(ldb, "Out of Memory");
1115 return LDB_ERR_OPERATIONS_ERROR;
1118 req->operation = LDB_SEQUENCE_NUMBER;
1119 req->controls = NULL;
1120 req->context = NULL;
1121 req->callback = NULL;
1122 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1124 req->op.seq_num.type = type;
1125 /* do request and autostart a transaction */
1126 ret = ldb_request(ldb, req);
1128 if (ret == LDB_SUCCESS) {
1129 *seq_num = req->op.seq_num.seq_num;
1139 return extended error information
1141 const char *ldb_errstring(struct ldb_context *ldb)
1143 if (ldb->err_string) {
1144 return ldb->err_string;
1151 return a string explaining what a ldb error constant meancs
1153 const char *ldb_strerror(int ldb_err)
1158 case LDB_ERR_OPERATIONS_ERROR:
1159 return "Operations error";
1160 case LDB_ERR_PROTOCOL_ERROR:
1161 return "Protocol error";
1162 case LDB_ERR_TIME_LIMIT_EXCEEDED:
1163 return "Time limit exceeded";
1164 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1165 return "Size limit exceeded";
1166 case LDB_ERR_COMPARE_FALSE:
1167 return "Compare false";
1168 case LDB_ERR_COMPARE_TRUE:
1169 return "Compare true";
1170 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1171 return "Auth method not supported";
1172 case LDB_ERR_STRONG_AUTH_REQUIRED:
1173 return "Strong auth required";
1175 case LDB_ERR_REFERRAL:
1176 return "Referral error";
1177 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1178 return "Admin limit exceeded";
1179 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1180 return "Unsupported critical extension";
1181 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1182 return "Confidentiality required";
1183 case LDB_ERR_SASL_BIND_IN_PROGRESS:
1184 return "SASL bind in progress";
1185 case LDB_ERR_NO_SUCH_ATTRIBUTE:
1186 return "No such attribute";
1187 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1188 return "Undefined attribute type";
1189 case LDB_ERR_INAPPROPRIATE_MATCHING:
1190 return "Inappropriate matching";
1191 case LDB_ERR_CONSTRAINT_VIOLATION:
1192 return "Constraint violation";
1193 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1194 return "Attribute or value exists";
1195 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1196 return "Invalid attribute syntax";
1198 case LDB_ERR_NO_SUCH_OBJECT:
1199 return "No such object";
1200 case LDB_ERR_ALIAS_PROBLEM:
1201 return "Alias problem";
1202 case LDB_ERR_INVALID_DN_SYNTAX:
1203 return "Invalid DN syntax";
1205 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1206 return "Alias dereferencing problem";
1208 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1209 return "Inappropriate authentication";
1210 case LDB_ERR_INVALID_CREDENTIALS:
1211 return "Invalid credentials";
1212 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1213 return "insufficient access rights";
1216 case LDB_ERR_UNAVAILABLE:
1217 return "Unavailable";
1218 case LDB_ERR_UNWILLING_TO_PERFORM:
1219 return "Unwilling to perform";
1220 case LDB_ERR_LOOP_DETECT:
1221 return "Loop detect";
1223 case LDB_ERR_NAMING_VIOLATION:
1224 return "Naming violation";
1225 case LDB_ERR_OBJECT_CLASS_VIOLATION:
1226 return "Object class violation";
1227 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1228 return "Not allowed on non-leaf";
1229 case LDB_ERR_NOT_ALLOWED_ON_RDN:
1230 return "Not allowed on RDN";
1231 case LDB_ERR_ENTRY_ALREADY_EXISTS:
1232 return "Entry already exists";
1233 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1234 return "Object class mods prohibited";
1235 /* 70 RESERVED FOR CLDAP */
1236 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1237 return "Affects multiple DSAs";
1243 return "Unknown error";
1247 set backend specific opaque parameters
1249 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1251 struct ldb_opaque *o;
1253 /* allow updating an existing value */
1254 for (o=ldb->opaque;o;o=o->next) {
1255 if (strcmp(o->name, name) == 0) {
1261 o = talloc(ldb, struct ldb_opaque);
1264 return LDB_ERR_OTHER;
1266 o->next = ldb->opaque;
1274 get a previously set opaque value
1276 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1278 struct ldb_opaque *o;
1279 for (o=ldb->opaque;o;o=o->next) {
1280 if (strcmp(o->name, name) == 0) {