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 2 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, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 * Component: ldb core API
31 * Description: core API routines interfacing to ldb backends
33 * Author: Andrew Tridgell
36 #include "ldb_includes.h"
39 initialise a ldb context
40 The mem_ctx is optional
42 struct ldb_context *ldb_init(void *mem_ctx)
44 struct ldb_context *ldb = talloc_zero(mem_ctx, struct ldb_context);
47 ret = ldb_setup_wellknown_attributes(ldb);
53 ldb_set_utf8_default(ldb);
54 ldb_set_create_perms(ldb, 0666);
59 static struct ldb_backend {
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, ...)
281 if (ldb->err_string) {
282 talloc_free(ldb->err_string);
285 va_start(ap, format);
286 ldb->err_string = talloc_vasprintf(ldb, format, ap);
290 void ldb_reset_err_string(struct ldb_context *ldb)
292 if (ldb->err_string) {
293 talloc_free(ldb->err_string);
294 ldb->err_string = NULL;
298 #define FIRST_OP(ldb, op) do { \
299 module = ldb->modules; \
300 while (module && module->ops->op == NULL) module = module->next; \
301 if (module == NULL) { \
302 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
303 return LDB_ERR_OPERATIONS_ERROR; \
310 static int ldb_transaction_start_internal(struct ldb_context *ldb)
312 struct ldb_module *module;
314 FIRST_OP(ldb, start_transaction);
316 ldb_reset_err_string(ldb);
318 status = module->ops->start_transaction(module);
319 if (status != LDB_SUCCESS) {
320 if (ldb->err_string == NULL) {
321 /* no error string was setup by the backend */
322 ldb_asprintf_errstring(ldb,
323 "ldb transaction start: %s (%d)",
324 ldb_strerror(status),
334 static int ldb_transaction_commit_internal(struct ldb_context *ldb)
336 struct ldb_module *module;
338 FIRST_OP(ldb, end_transaction);
340 ldb_reset_err_string(ldb);
342 status = module->ops->end_transaction(module);
343 if (status != LDB_SUCCESS) {
344 if (ldb->err_string == NULL) {
345 /* no error string was setup by the backend */
346 ldb_asprintf_errstring(ldb,
347 "ldb transaction commit: %s (%d)",
348 ldb_strerror(status),
358 static int ldb_transaction_cancel_internal(struct ldb_context *ldb)
360 struct ldb_module *module;
362 FIRST_OP(ldb, del_transaction);
364 status = module->ops->del_transaction(module);
365 if (status != LDB_SUCCESS) {
366 if (ldb->err_string == NULL) {
367 /* no error string was setup by the backend */
368 ldb_asprintf_errstring(ldb,
369 "ldb transaction cancel: %s (%d)",
370 ldb_strerror(status),
377 int ldb_transaction_start(struct ldb_context *ldb)
379 /* disable autotransactions */
380 ldb->transaction_active++;
382 return ldb_transaction_start_internal(ldb);
385 int ldb_transaction_commit(struct ldb_context *ldb)
387 /* renable autotransactions (when we reach 0) */
388 if (ldb->transaction_active > 0)
389 ldb->transaction_active--;
391 return ldb_transaction_commit_internal(ldb);
394 int ldb_transaction_cancel(struct ldb_context *ldb)
396 /* renable autotransactions (when we reach 0) */
397 if (ldb->transaction_active > 0)
398 ldb->transaction_active--;
400 return ldb_transaction_cancel_internal(ldb);
403 static int ldb_autotransaction_start(struct ldb_context *ldb)
405 /* explicit transaction active, ignore autotransaction request */
406 if (ldb->transaction_active)
409 return ldb_transaction_start_internal(ldb);
412 static int ldb_autotransaction_commit(struct ldb_context *ldb)
414 /* explicit transaction active, ignore autotransaction request */
415 if (ldb->transaction_active)
418 return ldb_transaction_commit_internal(ldb);
421 static int ldb_autotransaction_cancel(struct ldb_context *ldb)
423 /* explicit transaction active, ignore autotransaction request */
424 if (ldb->transaction_active)
427 return ldb_transaction_cancel_internal(ldb);
430 /* autostarts a transacion if none active */
431 static int ldb_autotransaction_request(struct ldb_context *ldb, struct ldb_request *req)
435 ret = ldb_autotransaction_start(ldb);
436 if (ret != LDB_SUCCESS) {
440 ret = ldb_request(ldb, req);
441 if (ret == LDB_SUCCESS) {
442 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
445 if (ret == LDB_SUCCESS) {
446 return ldb_autotransaction_commit(ldb);
448 ldb_autotransaction_cancel(ldb);
450 if (ldb->err_string == NULL) {
451 /* no error string was setup by the backend */
452 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
458 int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
464 return handle->module->ops->wait(handle, type);
467 /* set the specified timeout or, if timeout is 0 set the default timeout */
468 /* timeout == -1 means no timeout */
469 int ldb_set_timeout(struct ldb_context *ldb, struct ldb_request *req, int timeout)
471 if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
474 req->timeout = timeout;
476 req->timeout = ldb->default_timeout;
478 req->starttime = time(NULL);
483 /* calculates the new timeout based on the previous starttime and timeout */
484 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb, struct ldb_request *oldreq, struct ldb_request *newreq)
488 if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
493 return ldb_set_timeout(ldb, newreq, 0);
495 if ((now - oldreq->starttime) > oldreq->timeout) {
496 return LDB_ERR_TIME_LIMIT_EXCEEDED;
498 newreq->starttime = oldreq->starttime;
499 newreq->timeout = oldreq->timeout - (now - oldreq->starttime);
506 set the permissions for new files to be passed to open() in
507 backends that use local files
509 void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
511 ldb->create_perms = perms;
516 NOTE: the request must be a talloc context.
517 returns LDB_ERR_* on errors.
519 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
521 struct ldb_module *module;
524 ldb_reset_err_string(ldb);
526 /* call the first module in the chain */
527 switch (req->operation) {
529 FIRST_OP(ldb, search);
530 ret = module->ops->search(module, req);
534 ret = module->ops->add(module, req);
537 FIRST_OP(ldb, modify);
538 ret = module->ops->modify(module, req);
542 ret = module->ops->del(module, req);
545 FIRST_OP(ldb, rename);
546 ret = module->ops->rename(module, req);
549 FIRST_OP(ldb, extended);
550 ret = module->ops->extended(module, req);
552 case LDB_SEQUENCE_NUMBER:
553 FIRST_OP(ldb, sequence_number);
554 ret = module->ops->sequence_number(module, req);
557 FIRST_OP(ldb, request);
558 ret = module->ops->request(module, req);
566 search the database given a LDAP-like search expression
568 returns an LDB error code
570 Use talloc_free to free the ldb_message returned in 'res', if successful
573 int ldb_search_default_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
575 struct ldb_result *res;
579 ldb_set_errstring(ldb, "NULL Context in callback");
580 return LDB_ERR_OPERATIONS_ERROR;
583 res = talloc_get_type(context, struct ldb_result);
586 ldb_set_errstring(ldb, "NULL res or ares in callback");
590 switch (ares->type) {
591 case LDB_REPLY_ENTRY:
592 res->msgs = talloc_realloc(res, res->msgs, struct ldb_message *, res->count + 2);
597 res->msgs[res->count + 1] = NULL;
599 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
602 case LDB_REPLY_REFERRAL:
604 for (n = 0; res->refs[n]; n++) /*noop*/ ;
609 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
614 res->refs[n] = talloc_move(res->refs, &ares->referral);
615 res->refs[n + 1] = NULL;
617 case LDB_REPLY_EXTENDED:
619 /* TODO: we should really support controls on entries and referrals too! */
620 res->controls = talloc_move(res, &ares->controls);
628 return LDB_ERR_OPERATIONS_ERROR;
631 int ldb_build_search_req(struct ldb_request **ret_req,
632 struct ldb_context *ldb,
635 enum ldb_scope scope,
636 const char *expression,
637 const char * const *attrs,
638 struct ldb_control **controls,
640 ldb_request_callback_t callback)
642 struct ldb_request *req;
646 req = talloc(mem_ctx, struct ldb_request);
648 ldb_set_errstring(ldb, "Out of Memory");
649 return LDB_ERR_OPERATIONS_ERROR;
652 req->operation = LDB_SEARCH;
654 req->op.search.base = ldb_dn_new(req, ldb, NULL);
656 req->op.search.base = base;
658 req->op.search.scope = scope;
660 req->op.search.tree = ldb_parse_tree(req, expression);
661 if (req->op.search.tree == NULL) {
662 ldb_set_errstring(ldb, "Unable to parse search expression");
664 return LDB_ERR_OPERATIONS_ERROR;
667 req->op.search.attrs = attrs;
668 req->controls = controls;
669 req->context = context;
670 req->callback = callback;
676 int ldb_build_add_req(struct ldb_request **ret_req,
677 struct ldb_context *ldb,
679 const struct ldb_message *message,
680 struct ldb_control **controls,
682 ldb_request_callback_t callback)
684 struct ldb_request *req;
688 req = talloc(mem_ctx, struct ldb_request);
690 ldb_set_errstring(ldb, "Out of Memory");
691 return LDB_ERR_OPERATIONS_ERROR;
694 req->operation = LDB_ADD;
695 req->op.add.message = message;
696 req->controls = controls;
697 req->context = context;
698 req->callback = callback;
705 int ldb_build_mod_req(struct ldb_request **ret_req,
706 struct ldb_context *ldb,
708 const struct ldb_message *message,
709 struct ldb_control **controls,
711 ldb_request_callback_t callback)
713 struct ldb_request *req;
717 req = talloc(mem_ctx, struct ldb_request);
719 ldb_set_errstring(ldb, "Out of Memory");
720 return LDB_ERR_OPERATIONS_ERROR;
723 req->operation = LDB_MODIFY;
724 req->op.mod.message = message;
725 req->controls = controls;
726 req->context = context;
727 req->callback = callback;
734 int ldb_build_del_req(struct ldb_request **ret_req,
735 struct ldb_context *ldb,
738 struct ldb_control **controls,
740 ldb_request_callback_t callback)
742 struct ldb_request *req;
746 req = talloc(mem_ctx, struct ldb_request);
748 ldb_set_errstring(ldb, "Out of Memory");
749 return LDB_ERR_OPERATIONS_ERROR;
752 req->operation = LDB_DELETE;
754 req->controls = controls;
755 req->context = context;
756 req->callback = callback;
763 int ldb_build_rename_req(struct ldb_request **ret_req,
764 struct ldb_context *ldb,
766 struct ldb_dn *olddn,
767 struct ldb_dn *newdn,
768 struct ldb_control **controls,
770 ldb_request_callback_t callback)
772 struct ldb_request *req;
776 req = talloc(mem_ctx, struct ldb_request);
778 ldb_set_errstring(ldb, "Out of Memory");
779 return LDB_ERR_OPERATIONS_ERROR;
782 req->operation = LDB_RENAME;
783 req->op.rename.olddn = olddn;
784 req->op.rename.newdn = newdn;
785 req->controls = controls;
786 req->context = context;
787 req->callback = callback;
794 int ldb_extended_default_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
796 struct ldb_result *res;
799 ldb_set_errstring(ldb, "NULL Context in callback");
800 return LDB_ERR_OPERATIONS_ERROR;
803 res = talloc_get_type(context, struct ldb_result);
805 ldb_set_errstring(ldb, "NULL res or ares in callback");
809 switch (ares->type) {
810 case LDB_REPLY_ENTRY:
811 case LDB_REPLY_REFERRAL:
813 ldb_set_errstring(ldb, "invalid ares type in callback");
815 case LDB_REPLY_EXTENDED:
816 /* TODO: we should really support controls on entries and referrals too! */
817 res->extended = talloc_move(res, &ares->response);
818 res->controls = talloc_move(res, &ares->controls);
826 return LDB_ERR_OPERATIONS_ERROR;
829 int ldb_build_extended_req(struct ldb_request **ret_req,
830 struct ldb_context *ldb,
834 struct ldb_control **controls,
836 ldb_request_callback_t callback)
838 struct ldb_request *req;
842 req = talloc(mem_ctx, struct ldb_request);
844 ldb_set_errstring(ldb, "Out of Memory");
845 return LDB_ERR_OPERATIONS_ERROR;
848 req->operation = LDB_EXTENDED;
849 req->op.extended.oid = oid;
850 req->op.extended.data = data;
851 req->controls = controls;
852 req->context = context;
853 req->callback = callback;
860 int ldb_extended(struct ldb_context *ldb,
863 struct ldb_result **_res)
865 struct ldb_request *req;
867 struct ldb_result *res;
871 res = talloc_zero(ldb, struct ldb_result);
873 return LDB_ERR_OPERATIONS_ERROR;
876 ret = ldb_build_extended_req(&req, ldb, ldb,
878 res, ldb_extended_default_callback);
879 if (ret != LDB_SUCCESS) goto done;
881 ldb_set_timeout(ldb, req, 0); /* use default timeout */
883 ret = ldb_request(ldb, req);
885 if (ret == LDB_SUCCESS) {
886 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
892 if (ret != LDB_SUCCESS) {
901 note that ldb_search() will automatically replace a NULL 'base' value with the
902 defaultNamingContext from the rootDSE if available.
904 int ldb_search(struct ldb_context *ldb,
906 enum ldb_scope scope,
907 const char *expression,
908 const char * const *attrs,
909 struct ldb_result **_res)
911 struct ldb_request *req;
913 struct ldb_result *res;
917 res = talloc_zero(ldb, struct ldb_result);
919 return LDB_ERR_OPERATIONS_ERROR;
922 ret = ldb_build_search_req(&req, ldb, ldb,
923 base?base:ldb_get_default_basedn(ldb),
929 ldb_search_default_callback);
931 if (ret != LDB_SUCCESS) goto done;
933 ldb_set_timeout(ldb, req, 0); /* use default timeout */
935 ret = ldb_request(ldb, req);
937 if (ret == LDB_SUCCESS) {
938 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
944 if (ret != LDB_SUCCESS) {
953 a useful search function where you can easily define the expression and that
954 takes a memory context where results are allocated
957 int ldb_search_exp_fmt(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_result **result,
958 struct ldb_dn *base, enum ldb_scope scope, const char * const *attrs,
959 const char *exp_fmt, ...)
961 struct ldb_result *res;
969 va_start(ap, exp_fmt);
970 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
974 return LDB_ERR_OPERATIONS_ERROR;
977 ret = ldb_search(ldb, base, scope, expression, attrs, &res);
979 if (ret == LDB_SUCCESS) {
980 talloc_steal(mem_ctx, res);
984 talloc_free(expression);
990 add a record to the database. Will fail if a record with the given class and key
993 int ldb_add(struct ldb_context *ldb,
994 const struct ldb_message *message)
996 struct ldb_request *req;
999 ret = ldb_msg_sanity_check(ldb, message);
1000 if (ret != LDB_SUCCESS) {
1004 ret = ldb_build_add_req(&req, ldb, ldb,
1010 if (ret != LDB_SUCCESS) return ret;
1012 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1014 /* do request and autostart a transaction */
1015 ret = ldb_autotransaction_request(ldb, req);
1022 modify the specified attributes of a record
1024 int ldb_modify(struct ldb_context *ldb,
1025 const struct ldb_message *message)
1027 struct ldb_request *req;
1030 ret = ldb_msg_sanity_check(ldb, message);
1031 if (ret != LDB_SUCCESS) {
1035 ret = ldb_build_mod_req(&req, ldb, ldb,
1041 if (ret != LDB_SUCCESS) return ret;
1043 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1045 /* do request and autostart a transaction */
1046 ret = ldb_autotransaction_request(ldb, req);
1054 delete a record from the database
1056 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1058 struct ldb_request *req;
1061 ret = ldb_build_del_req(&req, ldb, ldb,
1067 if (ret != LDB_SUCCESS) return ret;
1069 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1071 /* do request and autostart a transaction */
1072 ret = ldb_autotransaction_request(ldb, req);
1079 rename a record in the database
1081 int ldb_rename(struct ldb_context *ldb, struct ldb_dn *olddn, struct ldb_dn *newdn)
1083 struct ldb_request *req;
1086 ret = ldb_build_rename_req(&req, ldb, ldb,
1093 if (ret != LDB_SUCCESS) return ret;
1095 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1097 /* do request and autostart a transaction */
1098 ret = ldb_autotransaction_request(ldb, req);
1106 return the global sequence number
1108 int ldb_sequence_number(struct ldb_context *ldb, enum ldb_sequence_type type, uint64_t *seq_num)
1110 struct ldb_request *req;
1113 req = talloc(ldb, struct ldb_request);
1115 ldb_set_errstring(ldb, "Out of Memory");
1116 return LDB_ERR_OPERATIONS_ERROR;
1119 req->operation = LDB_SEQUENCE_NUMBER;
1120 req->controls = NULL;
1121 req->context = NULL;
1122 req->callback = NULL;
1123 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1125 req->op.seq_num.type = type;
1126 /* do request and autostart a transaction */
1127 ret = ldb_request(ldb, req);
1129 if (ret == LDB_SUCCESS) {
1130 *seq_num = req->op.seq_num.seq_num;
1140 return extended error information
1142 const char *ldb_errstring(struct ldb_context *ldb)
1144 if (ldb->err_string) {
1145 return ldb->err_string;
1152 return a string explaining what a ldb error constant meancs
1154 const char *ldb_strerror(int ldb_err)
1159 case LDB_ERR_OPERATIONS_ERROR:
1160 return "Operations error";
1161 case LDB_ERR_PROTOCOL_ERROR:
1162 return "Protocol error";
1163 case LDB_ERR_TIME_LIMIT_EXCEEDED:
1164 return "Time limit exceeded";
1165 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1166 return "Size limit exceeded";
1167 case LDB_ERR_COMPARE_FALSE:
1168 return "Compare false";
1169 case LDB_ERR_COMPARE_TRUE:
1170 return "Compare true";
1171 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1172 return "Auth method not supported";
1173 case LDB_ERR_STRONG_AUTH_REQUIRED:
1174 return "Strong auth required";
1176 case LDB_ERR_REFERRAL:
1177 return "Referral error";
1178 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1179 return "Admin limit exceeded";
1180 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1181 return "Unsupported critical extension";
1182 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1183 return "Confidentiality required";
1184 case LDB_ERR_SASL_BIND_IN_PROGRESS:
1185 return "SASL bind in progress";
1186 case LDB_ERR_NO_SUCH_ATTRIBUTE:
1187 return "No such attribute";
1188 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1189 return "Undefined attribute type";
1190 case LDB_ERR_INAPPROPRIATE_MATCHING:
1191 return "Inappropriate matching";
1192 case LDB_ERR_CONSTRAINT_VIOLATION:
1193 return "Constraint violation";
1194 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1195 return "Attribute or value exists";
1196 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1197 return "Invalid attribute syntax";
1199 case LDB_ERR_NO_SUCH_OBJECT:
1200 return "No such object";
1201 case LDB_ERR_ALIAS_PROBLEM:
1202 return "Alias problem";
1203 case LDB_ERR_INVALID_DN_SYNTAX:
1204 return "Invalid DN syntax";
1206 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1207 return "Alias dereferencing problem";
1209 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1210 return "Inappropriate authentication";
1211 case LDB_ERR_INVALID_CREDENTIALS:
1212 return "Invalid credentials";
1213 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1214 return "insufficient access rights";
1217 case LDB_ERR_UNAVAILABLE:
1218 return "Unavailable";
1219 case LDB_ERR_UNWILLING_TO_PERFORM:
1220 return "Unwilling to perform";
1221 case LDB_ERR_LOOP_DETECT:
1222 return "Loop detect";
1224 case LDB_ERR_NAMING_VIOLATION:
1225 return "Naming violation";
1226 case LDB_ERR_OBJECT_CLASS_VIOLATION:
1227 return "Object class violation";
1228 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1229 return "Not allowed on non-leaf";
1230 case LDB_ERR_NOT_ALLOWED_ON_RDN:
1231 return "Not allowed on RDN";
1232 case LDB_ERR_ENTRY_ALREADY_EXISTS:
1233 return "Entry already exists";
1234 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1235 return "Object class mods prohibited";
1236 /* 70 RESERVED FOR CLDAP */
1237 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1238 return "Affects multiple DSAs";
1244 return "Unknown error";
1248 set backend specific opaque parameters
1250 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1252 struct ldb_opaque *o;
1254 /* allow updating an existing value */
1255 for (o=ldb->opaque;o;o=o->next) {
1256 if (strcmp(o->name, name) == 0) {
1262 o = talloc(ldb, struct ldb_opaque);
1265 return LDB_ERR_OTHER;
1267 o->next = ldb->opaque;
1275 get a previously set opaque value
1277 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1279 struct ldb_opaque *o;
1280 for (o=ldb->opaque;o;o=o->next) {
1281 if (strcmp(o->name, name) == 0) {