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);
54 ldb_set_modules_dir(ldb, LDB_MODULESDIR);
61 ldb_connect_fn connect_fn;
62 struct ldb_backend *prev, *next;
63 } *ldb_backends = NULL;
66 static ldb_connect_fn ldb_find_backend(const char *url)
68 struct ldb_backend *backend;
70 for (backend = ldb_backends; backend; backend = backend->next) {
71 if (strncmp(backend->name, url, strlen(backend->name)) == 0) {
72 return backend->connect_fn;
80 register a new ldb backend
82 int ldb_register_backend(const char *url_prefix, ldb_connect_fn connectfn)
84 struct ldb_backend *backend = talloc(talloc_autofree_context(), struct ldb_backend);
86 if (ldb_find_backend(url_prefix)) {
90 /* Maybe check for duplicity here later on? */
92 backend->name = talloc_strdup(backend, url_prefix);
93 backend->connect_fn = connectfn;
94 DLIST_ADD(ldb_backends, backend);
100 Return the ldb module form of a database. The URL can either be one of the following forms
104 flags is made up of LDB_FLG_*
106 the options are passed uninterpreted to the backend, and are
109 This allows modules to get at only the backend module, for example where a module
110 may wish to direct certain requests at a particular backend.
112 int ldb_connect_backend(struct ldb_context *ldb, const char *url, const char *options[],
113 struct ldb_module **backend_module)
119 if (strchr(url, ':') != NULL) {
120 backend = talloc_strndup(ldb, url, strchr(url, ':')-url);
123 backend = talloc_strdup(ldb, "tdb");
126 fn = ldb_find_backend(backend);
129 if (ldb_try_load_dso(ldb, backend) == 0) {
130 fn = ldb_find_backend(backend);
134 talloc_free(backend);
137 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to find backend for '%s'\n", url);
138 return LDB_ERR_OTHER;
141 ret = fn(ldb, url, ldb->flags, options, backend_module);
143 if (ret != LDB_SUCCESS) {
144 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to '%s'\n", url);
151 try to autodetect a basedn if none specified. This fixes one of my
152 pet hates about ldapsearch, which is that you have to get a long,
153 complex basedn right to make any use of it.
155 void ldb_set_default_dns(struct ldb_context *ldb)
159 struct ldb_result *res;
160 struct ldb_dn *tmp_dn=NULL;
161 static const char *attrs[] = {
162 "rootDomainNamingContext",
163 "configurationNamingContext",
164 "schemaNamingContext",
165 "defaultNamingContext",
169 tmp_ctx = talloc_new(ldb);
170 ret = ldb_search(ldb, ldb_dn_new(tmp_ctx, ldb, NULL), LDB_SCOPE_BASE,
171 "(objectClass=*)", attrs, &res);
172 if (ret == LDB_SUCCESS) {
173 if (res->count == 1) {
174 if (!ldb_get_opaque(ldb, "rootDomainNamingContext")) {
175 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "rootDomainNamingContext");
176 ldb_set_opaque(ldb, "rootDomainNamingContext", tmp_dn);
179 if (!ldb_get_opaque(ldb, "configurationNamingContext")) {
180 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "configurationNamingContext");
181 ldb_set_opaque(ldb, "configurationNamingContext", tmp_dn);
184 if (!ldb_get_opaque(ldb, "schemaNamingContext")) {
185 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "schemaNamingContext");
186 ldb_set_opaque(ldb, "schemaNamingContext", tmp_dn);
189 if (!ldb_get_opaque(ldb, "defaultNamingContext")) {
190 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "defaultNamingContext");
191 ldb_set_opaque(ldb, "defaultNamingContext", tmp_dn);
197 talloc_free(tmp_ctx);
200 struct ldb_dn *ldb_get_root_basedn(struct ldb_context *ldb)
202 return talloc_get_type(ldb_get_opaque(ldb, "rootDomainNamingContext"), struct ldb_dn);
205 struct ldb_dn *ldb_get_config_basedn(struct ldb_context *ldb)
207 return talloc_get_type(ldb_get_opaque(ldb, "configurationNamingContext"), struct ldb_dn);
210 struct ldb_dn *ldb_get_schema_basedn(struct ldb_context *ldb)
212 return talloc_get_type(ldb_get_opaque(ldb, "schemaNamingContext"), struct ldb_dn);
215 struct ldb_dn *ldb_get_default_basedn(struct ldb_context *ldb)
217 return talloc_get_type(ldb_get_opaque(ldb, "defaultNamingContext"), struct ldb_dn);
221 connect to a database. The URL can either be one of the following forms
225 flags is made up of LDB_FLG_*
227 the options are passed uninterpreted to the backend, and are
230 int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[])
234 /* We seem to need to do this here, or else some utilities don't get ldb backends */
239 url2 = talloc_strdup(ldb, url);
242 return LDB_ERR_OPERATIONS_ERROR;
244 ret = ldb_set_opaque(ldb, "ldb_url", talloc_strdup(ldb, url2));
245 if (ret != LDB_SUCCESS) {
249 ret = ldb_connect_backend(ldb, url, options, &ldb->modules);
250 if (ret != LDB_SUCCESS) {
254 if (ldb_load_modules(ldb, options) != LDB_SUCCESS) {
255 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to load modules for %s: %s\n",
256 url, ldb_errstring(ldb));
257 return LDB_ERR_OTHER;
260 /* TODO: get timeout from options if available there */
261 ldb->default_timeout = 300; /* set default to 5 minutes */
263 /* set the default base dn */
264 ldb_set_default_dns(ldb);
269 void ldb_set_errstring(struct ldb_context *ldb, const char *err_string)
271 if (ldb->err_string) {
272 talloc_free(ldb->err_string);
274 ldb->err_string = talloc_strdup(ldb, err_string);
277 void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
280 char *old_string = NULL;
282 if (ldb->err_string) {
283 old_string = ldb->err_string;
286 va_start(ap, format);
287 ldb->err_string = talloc_vasprintf(ldb, format, ap);
289 talloc_free(old_string);
292 void ldb_reset_err_string(struct ldb_context *ldb)
294 if (ldb->err_string) {
295 talloc_free(ldb->err_string);
296 ldb->err_string = NULL;
300 #define FIRST_OP(ldb, op) do { \
301 module = ldb->modules; \
302 while (module && module->ops->op == NULL) module = module->next; \
303 if (module == NULL) { \
304 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
305 return LDB_ERR_OPERATIONS_ERROR; \
312 static int ldb_transaction_start_internal(struct ldb_context *ldb)
314 struct ldb_module *module;
316 FIRST_OP(ldb, start_transaction);
318 ldb_reset_err_string(ldb);
320 status = module->ops->start_transaction(module);
321 if (status != LDB_SUCCESS) {
322 if (ldb->err_string == NULL) {
323 /* no error string was setup by the backend */
324 ldb_asprintf_errstring(ldb,
325 "ldb transaction start: %s (%d)",
326 ldb_strerror(status),
336 static int ldb_transaction_commit_internal(struct ldb_context *ldb)
338 struct ldb_module *module;
340 FIRST_OP(ldb, end_transaction);
342 ldb_reset_err_string(ldb);
344 status = module->ops->end_transaction(module);
345 if (status != LDB_SUCCESS) {
346 if (ldb->err_string == NULL) {
347 /* no error string was setup by the backend */
348 ldb_asprintf_errstring(ldb,
349 "ldb transaction commit: %s (%d)",
350 ldb_strerror(status),
360 static int ldb_transaction_cancel_internal(struct ldb_context *ldb)
362 struct ldb_module *module;
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 int ldb_transaction_start(struct ldb_context *ldb)
381 /* disable autotransactions */
382 ldb->transaction_active++;
384 return ldb_transaction_start_internal(ldb);
387 int ldb_transaction_commit(struct ldb_context *ldb)
389 /* renable autotransactions (when we reach 0) */
390 if (ldb->transaction_active > 0)
391 ldb->transaction_active--;
393 return ldb_transaction_commit_internal(ldb);
396 int ldb_transaction_cancel(struct ldb_context *ldb)
398 /* renable autotransactions (when we reach 0) */
399 if (ldb->transaction_active > 0)
400 ldb->transaction_active--;
402 return ldb_transaction_cancel_internal(ldb);
405 static int ldb_autotransaction_start(struct ldb_context *ldb)
407 /* explicit transaction active, ignore autotransaction request */
408 if (ldb->transaction_active)
411 return ldb_transaction_start_internal(ldb);
414 static int ldb_autotransaction_commit(struct ldb_context *ldb)
416 /* explicit transaction active, ignore autotransaction request */
417 if (ldb->transaction_active)
420 return ldb_transaction_commit_internal(ldb);
423 static int ldb_autotransaction_cancel(struct ldb_context *ldb)
425 /* explicit transaction active, ignore autotransaction request */
426 if (ldb->transaction_active)
429 return ldb_transaction_cancel_internal(ldb);
432 /* autostarts a transacion if none active */
433 static int ldb_autotransaction_request(struct ldb_context *ldb, struct ldb_request *req)
437 ret = ldb_autotransaction_start(ldb);
438 if (ret != LDB_SUCCESS) {
442 ret = ldb_request(ldb, req);
443 if (ret == LDB_SUCCESS) {
444 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
447 if (ret == LDB_SUCCESS) {
448 return ldb_autotransaction_commit(ldb);
450 ldb_autotransaction_cancel(ldb);
452 if (ldb->err_string == NULL) {
453 /* no error string was setup by the backend */
454 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
460 int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
466 return handle->module->ops->wait(handle, type);
469 /* set the specified timeout or, if timeout is 0 set the default timeout */
470 /* timeout == -1 means no timeout */
471 int ldb_set_timeout(struct ldb_context *ldb, struct ldb_request *req, int timeout)
473 if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
476 req->timeout = timeout;
478 req->timeout = ldb->default_timeout;
480 req->starttime = time(NULL);
485 /* calculates the new timeout based on the previous starttime and timeout */
486 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb, struct ldb_request *oldreq, struct ldb_request *newreq)
490 if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
495 return ldb_set_timeout(ldb, newreq, 0);
497 if ((now - oldreq->starttime) > oldreq->timeout) {
498 return LDB_ERR_TIME_LIMIT_EXCEEDED;
500 newreq->starttime = oldreq->starttime;
501 newreq->timeout = oldreq->timeout - (now - oldreq->starttime);
508 set the permissions for new files to be passed to open() in
509 backends that use local files
511 void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
513 ldb->create_perms = perms;
518 NOTE: the request must be a talloc context.
519 returns LDB_ERR_* on errors.
521 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
523 struct ldb_module *module;
526 ldb_reset_err_string(ldb);
528 /* call the first module in the chain */
529 switch (req->operation) {
531 FIRST_OP(ldb, search);
532 ret = module->ops->search(module, req);
536 ret = module->ops->add(module, req);
539 FIRST_OP(ldb, modify);
540 ret = module->ops->modify(module, req);
544 ret = module->ops->del(module, req);
547 FIRST_OP(ldb, rename);
548 ret = module->ops->rename(module, req);
551 FIRST_OP(ldb, extended);
552 ret = module->ops->extended(module, req);
554 case LDB_SEQUENCE_NUMBER:
555 FIRST_OP(ldb, sequence_number);
556 ret = module->ops->sequence_number(module, req);
559 FIRST_OP(ldb, request);
560 ret = module->ops->request(module, req);
568 search the database given a LDAP-like search expression
570 returns an LDB error code
572 Use talloc_free to free the ldb_message returned in 'res', if successful
575 int ldb_search_default_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
577 struct ldb_result *res;
581 ldb_set_errstring(ldb, "NULL Context in callback");
582 return LDB_ERR_OPERATIONS_ERROR;
585 res = talloc_get_type(context, struct ldb_result);
588 ldb_set_errstring(ldb, "NULL res or ares in callback");
592 switch (ares->type) {
593 case LDB_REPLY_ENTRY:
594 res->msgs = talloc_realloc(res, res->msgs, struct ldb_message *, res->count + 2);
599 res->msgs[res->count + 1] = NULL;
601 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
604 case LDB_REPLY_REFERRAL:
606 for (n = 0; res->refs[n]; n++) /*noop*/ ;
611 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
616 res->refs[n] = talloc_move(res->refs, &ares->referral);
617 res->refs[n + 1] = NULL;
619 case LDB_REPLY_EXTENDED:
621 /* TODO: we should really support controls on entries and referrals too! */
622 res->controls = talloc_move(res, &ares->controls);
630 return LDB_ERR_OPERATIONS_ERROR;
633 int ldb_build_search_req(struct ldb_request **ret_req,
634 struct ldb_context *ldb,
637 enum ldb_scope scope,
638 const char *expression,
639 const char * const *attrs,
640 struct ldb_control **controls,
642 ldb_request_callback_t callback)
644 struct ldb_request *req;
648 req = talloc(mem_ctx, struct ldb_request);
650 ldb_set_errstring(ldb, "Out of Memory");
651 return LDB_ERR_OPERATIONS_ERROR;
654 req->operation = LDB_SEARCH;
656 req->op.search.base = ldb_dn_new(req, ldb, NULL);
658 req->op.search.base = base;
660 req->op.search.scope = scope;
662 req->op.search.tree = ldb_parse_tree(req, expression);
663 if (req->op.search.tree == NULL) {
664 ldb_set_errstring(ldb, "Unable to parse search expression");
666 return LDB_ERR_OPERATIONS_ERROR;
669 req->op.search.attrs = attrs;
670 req->controls = controls;
671 req->context = context;
672 req->callback = callback;
678 int ldb_build_add_req(struct ldb_request **ret_req,
679 struct ldb_context *ldb,
681 const struct ldb_message *message,
682 struct ldb_control **controls,
684 ldb_request_callback_t callback)
686 struct ldb_request *req;
690 req = talloc(mem_ctx, struct ldb_request);
692 ldb_set_errstring(ldb, "Out of Memory");
693 return LDB_ERR_OPERATIONS_ERROR;
696 req->operation = LDB_ADD;
697 req->op.add.message = message;
698 req->controls = controls;
699 req->context = context;
700 req->callback = callback;
707 int ldb_build_mod_req(struct ldb_request **ret_req,
708 struct ldb_context *ldb,
710 const struct ldb_message *message,
711 struct ldb_control **controls,
713 ldb_request_callback_t callback)
715 struct ldb_request *req;
719 req = talloc(mem_ctx, struct ldb_request);
721 ldb_set_errstring(ldb, "Out of Memory");
722 return LDB_ERR_OPERATIONS_ERROR;
725 req->operation = LDB_MODIFY;
726 req->op.mod.message = message;
727 req->controls = controls;
728 req->context = context;
729 req->callback = callback;
736 int ldb_build_del_req(struct ldb_request **ret_req,
737 struct ldb_context *ldb,
740 struct ldb_control **controls,
742 ldb_request_callback_t callback)
744 struct ldb_request *req;
748 req = talloc(mem_ctx, struct ldb_request);
750 ldb_set_errstring(ldb, "Out of Memory");
751 return LDB_ERR_OPERATIONS_ERROR;
754 req->operation = LDB_DELETE;
756 req->controls = controls;
757 req->context = context;
758 req->callback = callback;
765 int ldb_build_rename_req(struct ldb_request **ret_req,
766 struct ldb_context *ldb,
768 struct ldb_dn *olddn,
769 struct ldb_dn *newdn,
770 struct ldb_control **controls,
772 ldb_request_callback_t callback)
774 struct ldb_request *req;
778 req = talloc(mem_ctx, struct ldb_request);
780 ldb_set_errstring(ldb, "Out of Memory");
781 return LDB_ERR_OPERATIONS_ERROR;
784 req->operation = LDB_RENAME;
785 req->op.rename.olddn = olddn;
786 req->op.rename.newdn = newdn;
787 req->controls = controls;
788 req->context = context;
789 req->callback = callback;
796 int ldb_extended_default_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
798 struct ldb_result *res;
801 ldb_set_errstring(ldb, "NULL Context in callback");
802 return LDB_ERR_OPERATIONS_ERROR;
805 res = talloc_get_type(context, struct ldb_result);
807 ldb_set_errstring(ldb, "NULL res or ares in callback");
811 switch (ares->type) {
812 case LDB_REPLY_ENTRY:
813 case LDB_REPLY_REFERRAL:
815 ldb_set_errstring(ldb, "invalid ares type in callback");
817 case LDB_REPLY_EXTENDED:
818 /* TODO: we should really support controls on entries and referrals too! */
819 res->extended = talloc_move(res, &ares->response);
820 res->controls = talloc_move(res, &ares->controls);
828 return LDB_ERR_OPERATIONS_ERROR;
831 int ldb_build_extended_req(struct ldb_request **ret_req,
832 struct ldb_context *ldb,
836 struct ldb_control **controls,
838 ldb_request_callback_t callback)
840 struct ldb_request *req;
844 req = talloc(mem_ctx, struct ldb_request);
846 ldb_set_errstring(ldb, "Out of Memory");
847 return LDB_ERR_OPERATIONS_ERROR;
850 req->operation = LDB_EXTENDED;
851 req->op.extended.oid = oid;
852 req->op.extended.data = data;
853 req->controls = controls;
854 req->context = context;
855 req->callback = callback;
862 int ldb_extended(struct ldb_context *ldb,
865 struct ldb_result **_res)
867 struct ldb_request *req;
869 struct ldb_result *res;
873 res = talloc_zero(ldb, struct ldb_result);
875 return LDB_ERR_OPERATIONS_ERROR;
878 ret = ldb_build_extended_req(&req, ldb, ldb,
880 res, ldb_extended_default_callback);
881 if (ret != LDB_SUCCESS) goto done;
883 ldb_set_timeout(ldb, req, 0); /* use default timeout */
885 ret = ldb_request(ldb, req);
887 if (ret == LDB_SUCCESS) {
888 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
894 if (ret != LDB_SUCCESS) {
903 note that ldb_search() will automatically replace a NULL 'base' value with the
904 defaultNamingContext from the rootDSE if available.
906 int ldb_search(struct ldb_context *ldb,
908 enum ldb_scope scope,
909 const char *expression,
910 const char * const *attrs,
911 struct ldb_result **_res)
913 struct ldb_request *req;
915 struct ldb_result *res;
919 res = talloc_zero(ldb, struct ldb_result);
921 return LDB_ERR_OPERATIONS_ERROR;
924 ret = ldb_build_search_req(&req, ldb, ldb,
925 base?base:ldb_get_default_basedn(ldb),
931 ldb_search_default_callback);
933 if (ret != LDB_SUCCESS) goto done;
935 ldb_set_timeout(ldb, req, 0); /* use default timeout */
937 ret = ldb_request(ldb, req);
939 if (ret == LDB_SUCCESS) {
940 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
946 if (ret != LDB_SUCCESS) {
955 a useful search function where you can easily define the expression and that
956 takes a memory context where results are allocated
959 int ldb_search_exp_fmt(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_result **result,
960 struct ldb_dn *base, enum ldb_scope scope, const char * const *attrs,
961 const char *exp_fmt, ...)
963 struct ldb_result *res;
971 va_start(ap, exp_fmt);
972 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
976 return LDB_ERR_OPERATIONS_ERROR;
979 ret = ldb_search(ldb, base, scope, expression, attrs, &res);
981 if (ret == LDB_SUCCESS) {
982 talloc_steal(mem_ctx, res);
986 talloc_free(expression);
992 add a record to the database. Will fail if a record with the given class and key
995 int ldb_add(struct ldb_context *ldb,
996 const struct ldb_message *message)
998 struct ldb_request *req;
1001 ret = ldb_msg_sanity_check(ldb, message);
1002 if (ret != LDB_SUCCESS) {
1006 ret = ldb_build_add_req(&req, ldb, ldb,
1012 if (ret != LDB_SUCCESS) return ret;
1014 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1016 /* do request and autostart a transaction */
1017 ret = ldb_autotransaction_request(ldb, req);
1024 modify the specified attributes of a record
1026 int ldb_modify(struct ldb_context *ldb,
1027 const struct ldb_message *message)
1029 struct ldb_request *req;
1032 ret = ldb_msg_sanity_check(ldb, message);
1033 if (ret != LDB_SUCCESS) {
1037 ret = ldb_build_mod_req(&req, ldb, ldb,
1043 if (ret != LDB_SUCCESS) return ret;
1045 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1047 /* do request and autostart a transaction */
1048 ret = ldb_autotransaction_request(ldb, req);
1056 delete a record from the database
1058 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1060 struct ldb_request *req;
1063 ret = ldb_build_del_req(&req, ldb, ldb,
1069 if (ret != LDB_SUCCESS) return ret;
1071 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1073 /* do request and autostart a transaction */
1074 ret = ldb_autotransaction_request(ldb, req);
1081 rename a record in the database
1083 int ldb_rename(struct ldb_context *ldb, struct ldb_dn *olddn, struct ldb_dn *newdn)
1085 struct ldb_request *req;
1088 ret = ldb_build_rename_req(&req, ldb, ldb,
1095 if (ret != LDB_SUCCESS) return ret;
1097 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1099 /* do request and autostart a transaction */
1100 ret = ldb_autotransaction_request(ldb, req);
1108 return the global sequence number
1110 int ldb_sequence_number(struct ldb_context *ldb, enum ldb_sequence_type type, uint64_t *seq_num)
1112 struct ldb_request *req;
1115 req = talloc(ldb, struct ldb_request);
1117 ldb_set_errstring(ldb, "Out of Memory");
1118 return LDB_ERR_OPERATIONS_ERROR;
1121 req->operation = LDB_SEQUENCE_NUMBER;
1122 req->controls = NULL;
1123 req->context = NULL;
1124 req->callback = NULL;
1125 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1127 req->op.seq_num.type = type;
1128 /* do request and autostart a transaction */
1129 ret = ldb_request(ldb, req);
1131 if (ret == LDB_SUCCESS) {
1132 *seq_num = req->op.seq_num.seq_num;
1142 return extended error information
1144 const char *ldb_errstring(struct ldb_context *ldb)
1146 if (ldb->err_string) {
1147 return ldb->err_string;
1154 return a string explaining what a ldb error constant meancs
1156 const char *ldb_strerror(int ldb_err)
1161 case LDB_ERR_OPERATIONS_ERROR:
1162 return "Operations error";
1163 case LDB_ERR_PROTOCOL_ERROR:
1164 return "Protocol error";
1165 case LDB_ERR_TIME_LIMIT_EXCEEDED:
1166 return "Time limit exceeded";
1167 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1168 return "Size limit exceeded";
1169 case LDB_ERR_COMPARE_FALSE:
1170 return "Compare false";
1171 case LDB_ERR_COMPARE_TRUE:
1172 return "Compare true";
1173 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1174 return "Auth method not supported";
1175 case LDB_ERR_STRONG_AUTH_REQUIRED:
1176 return "Strong auth required";
1178 case LDB_ERR_REFERRAL:
1179 return "Referral error";
1180 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1181 return "Admin limit exceeded";
1182 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1183 return "Unsupported critical extension";
1184 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1185 return "Confidentiality required";
1186 case LDB_ERR_SASL_BIND_IN_PROGRESS:
1187 return "SASL bind in progress";
1188 case LDB_ERR_NO_SUCH_ATTRIBUTE:
1189 return "No such attribute";
1190 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1191 return "Undefined attribute type";
1192 case LDB_ERR_INAPPROPRIATE_MATCHING:
1193 return "Inappropriate matching";
1194 case LDB_ERR_CONSTRAINT_VIOLATION:
1195 return "Constraint violation";
1196 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1197 return "Attribute or value exists";
1198 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1199 return "Invalid attribute syntax";
1201 case LDB_ERR_NO_SUCH_OBJECT:
1202 return "No such object";
1203 case LDB_ERR_ALIAS_PROBLEM:
1204 return "Alias problem";
1205 case LDB_ERR_INVALID_DN_SYNTAX:
1206 return "Invalid DN syntax";
1208 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1209 return "Alias dereferencing problem";
1211 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1212 return "Inappropriate authentication";
1213 case LDB_ERR_INVALID_CREDENTIALS:
1214 return "Invalid credentials";
1215 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1216 return "insufficient access rights";
1219 case LDB_ERR_UNAVAILABLE:
1220 return "Unavailable";
1221 case LDB_ERR_UNWILLING_TO_PERFORM:
1222 return "Unwilling to perform";
1223 case LDB_ERR_LOOP_DETECT:
1224 return "Loop detect";
1226 case LDB_ERR_NAMING_VIOLATION:
1227 return "Naming violation";
1228 case LDB_ERR_OBJECT_CLASS_VIOLATION:
1229 return "Object class violation";
1230 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1231 return "Not allowed on non-leaf";
1232 case LDB_ERR_NOT_ALLOWED_ON_RDN:
1233 return "Not allowed on RDN";
1234 case LDB_ERR_ENTRY_ALREADY_EXISTS:
1235 return "Entry already exists";
1236 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1237 return "Object class mods prohibited";
1238 /* 70 RESERVED FOR CLDAP */
1239 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1240 return "Affects multiple DSAs";
1246 return "Unknown error";
1250 set backend specific opaque parameters
1252 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1254 struct ldb_opaque *o;
1256 /* allow updating an existing value */
1257 for (o=ldb->opaque;o;o=o->next) {
1258 if (strcmp(o->name, name) == 0) {
1264 o = talloc(ldb, struct ldb_opaque);
1267 return LDB_ERR_OTHER;
1269 o->next = ldb->opaque;
1277 get a previously set opaque value
1279 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1281 struct ldb_opaque *o;
1282 for (o=ldb->opaque;o;o=o->next) {
1283 if (strcmp(o->name, name) == 0) {