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 int (*init_fn) (void);
131 init_fn = ldb_dso_load_symbol(ldb, backend,
133 if (init_fn != NULL && init_fn() == 0) {
134 fn = ldb_find_backend(backend);
138 talloc_free(backend);
141 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to find backend for '%s'\n", url);
142 return LDB_ERR_OTHER;
145 ret = fn(ldb, url, ldb->flags, options, backend_module);
147 if (ret != LDB_SUCCESS) {
148 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to '%s'\n", url);
155 try to autodetect a basedn if none specified. This fixes one of my
156 pet hates about ldapsearch, which is that you have to get a long,
157 complex basedn right to make any use of it.
159 void ldb_set_default_dns(struct ldb_context *ldb)
163 struct ldb_result *res;
164 struct ldb_dn *tmp_dn=NULL;
165 const char *attrs[] = {
166 "rootDomainNamingContext",
167 "configurationNamingContext",
168 "schemaNamingContext",
169 "defaultNamingContext",
173 tmp_ctx = talloc_new(ldb);
174 ret = ldb_search(ldb, ldb_dn_new(tmp_ctx, ldb, NULL), LDB_SCOPE_BASE,
175 "(objectClass=*)", attrs, &res);
176 if (ret == LDB_SUCCESS) {
177 if (res->count == 1) {
178 if (!ldb_get_opaque(ldb, "rootDomainNamingContext")) {
179 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "rootDomainNamingContext");
180 ldb_set_opaque(ldb, "rootDomainNamingContext", tmp_dn);
183 if (!ldb_get_opaque(ldb, "configurationNamingContext")) {
184 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "configurationNamingContext");
185 ldb_set_opaque(ldb, "configurationNamingContext", tmp_dn);
188 if (!ldb_get_opaque(ldb, "schemaNamingContext")) {
189 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "schemaNamingContext");
190 ldb_set_opaque(ldb, "schemaNamingContext", tmp_dn);
193 if (!ldb_get_opaque(ldb, "defaultNamingContext")) {
194 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "defaultNamingContext");
195 ldb_set_opaque(ldb, "defaultNamingContext", tmp_dn);
201 talloc_free(tmp_ctx);
204 struct ldb_dn *ldb_get_root_basedn(struct ldb_context *ldb)
206 return talloc_get_type(ldb_get_opaque(ldb, "rootDomainNamingContext"), struct ldb_dn);
209 struct ldb_dn *ldb_get_config_basedn(struct ldb_context *ldb)
211 return talloc_get_type(ldb_get_opaque(ldb, "configurationNamingContext"), struct ldb_dn);
214 struct ldb_dn *ldb_get_schema_basedn(struct ldb_context *ldb)
216 return talloc_get_type(ldb_get_opaque(ldb, "schemaNamingContext"), struct ldb_dn);
219 struct ldb_dn *ldb_get_default_basedn(struct ldb_context *ldb)
221 return talloc_get_type(ldb_get_opaque(ldb, "defaultNamingContext"), struct ldb_dn);
225 connect to a database. The URL can either be one of the following forms
229 flags is made up of LDB_FLG_*
231 the options are passed uninterpreted to the backend, and are
234 int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[])
238 /* We seem to need to do this here, or else some utilities don't get ldb backends */
243 url2 = talloc_strdup(ldb, url);
246 return LDB_ERR_OPERATIONS_ERROR;
248 ret = ldb_set_opaque(ldb, "ldb_url", talloc_strdup(ldb, url2));
249 if (ret != LDB_SUCCESS) {
253 ret = ldb_connect_backend(ldb, url, options, &ldb->modules);
254 if (ret != LDB_SUCCESS) {
258 if (ldb_load_modules(ldb, options) != LDB_SUCCESS) {
259 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to load modules for %s: %s\n",
260 url, ldb_errstring(ldb));
261 return LDB_ERR_OTHER;
264 /* TODO: get timeout from options if available there */
265 ldb->default_timeout = 300; /* set default to 5 minutes */
267 /* set the default base dn */
268 ldb_set_default_dns(ldb);
273 void ldb_set_errstring(struct ldb_context *ldb, const char *err_string)
275 if (ldb->err_string) {
276 talloc_free(ldb->err_string);
278 ldb->err_string = talloc_strdup(ldb, err_string);
281 void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
284 char *old_string = NULL;
286 if (ldb->err_string) {
287 old_string = ldb->err_string;
290 va_start(ap, format);
291 ldb->err_string = talloc_vasprintf(ldb, format, ap);
293 talloc_free(old_string);
296 void ldb_reset_err_string(struct ldb_context *ldb)
298 if (ldb->err_string) {
299 talloc_free(ldb->err_string);
300 ldb->err_string = NULL;
304 #define FIRST_OP(ldb, op) do { \
305 module = ldb->modules; \
306 while (module && module->ops->op == NULL) module = module->next; \
307 if (module == NULL) { \
308 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
309 return LDB_ERR_OPERATIONS_ERROR; \
316 static int ldb_transaction_start_internal(struct ldb_context *ldb)
318 struct ldb_module *module;
320 FIRST_OP(ldb, start_transaction);
322 ldb_reset_err_string(ldb);
324 status = module->ops->start_transaction(module);
325 if (status != LDB_SUCCESS) {
326 if (ldb->err_string == NULL) {
327 /* no error string was setup by the backend */
328 ldb_asprintf_errstring(ldb,
329 "ldb transaction start: %s (%d)",
330 ldb_strerror(status),
340 static int ldb_transaction_commit_internal(struct ldb_context *ldb)
342 struct ldb_module *module;
344 FIRST_OP(ldb, end_transaction);
346 ldb_reset_err_string(ldb);
348 status = module->ops->end_transaction(module);
349 if (status != LDB_SUCCESS) {
350 if (ldb->err_string == NULL) {
351 /* no error string was setup by the backend */
352 ldb_asprintf_errstring(ldb,
353 "ldb transaction commit: %s (%d)",
354 ldb_strerror(status),
364 static int ldb_transaction_cancel_internal(struct ldb_context *ldb)
366 struct ldb_module *module;
368 FIRST_OP(ldb, del_transaction);
370 status = module->ops->del_transaction(module);
371 if (status != LDB_SUCCESS) {
372 if (ldb->err_string == NULL) {
373 /* no error string was setup by the backend */
374 ldb_asprintf_errstring(ldb,
375 "ldb transaction cancel: %s (%d)",
376 ldb_strerror(status),
383 int ldb_transaction_start(struct ldb_context *ldb)
385 /* disable autotransactions */
386 ldb->transaction_active++;
388 return ldb_transaction_start_internal(ldb);
391 int ldb_transaction_commit(struct ldb_context *ldb)
393 /* renable autotransactions (when we reach 0) */
394 if (ldb->transaction_active > 0)
395 ldb->transaction_active--;
397 return ldb_transaction_commit_internal(ldb);
400 int ldb_transaction_cancel(struct ldb_context *ldb)
402 /* renable autotransactions (when we reach 0) */
403 if (ldb->transaction_active > 0)
404 ldb->transaction_active--;
406 return ldb_transaction_cancel_internal(ldb);
409 static int ldb_autotransaction_start(struct ldb_context *ldb)
411 /* explicit transaction active, ignore autotransaction request */
412 if (ldb->transaction_active)
415 return ldb_transaction_start_internal(ldb);
418 static int ldb_autotransaction_commit(struct ldb_context *ldb)
420 /* explicit transaction active, ignore autotransaction request */
421 if (ldb->transaction_active)
424 return ldb_transaction_commit_internal(ldb);
427 static int ldb_autotransaction_cancel(struct ldb_context *ldb)
429 /* explicit transaction active, ignore autotransaction request */
430 if (ldb->transaction_active)
433 return ldb_transaction_cancel_internal(ldb);
436 /* autostarts a transacion if none active */
437 static int ldb_autotransaction_request(struct ldb_context *ldb, struct ldb_request *req)
441 ret = ldb_autotransaction_start(ldb);
442 if (ret != LDB_SUCCESS) {
446 ret = ldb_request(ldb, req);
447 if (ret == LDB_SUCCESS) {
448 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
451 if (ret == LDB_SUCCESS) {
452 return ldb_autotransaction_commit(ldb);
454 ldb_autotransaction_cancel(ldb);
456 if (ldb->err_string == NULL) {
457 /* no error string was setup by the backend */
458 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
464 int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
470 return handle->module->ops->wait(handle, type);
473 /* set the specified timeout or, if timeout is 0 set the default timeout */
474 /* timeout == -1 means no timeout */
475 int ldb_set_timeout(struct ldb_context *ldb, struct ldb_request *req, int timeout)
477 if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
480 req->timeout = timeout;
482 req->timeout = ldb->default_timeout;
484 req->starttime = time(NULL);
489 /* calculates the new timeout based on the previous starttime and timeout */
490 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb, struct ldb_request *oldreq, struct ldb_request *newreq)
494 if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
499 return ldb_set_timeout(ldb, newreq, 0);
501 if ((now - oldreq->starttime) > oldreq->timeout) {
502 return LDB_ERR_TIME_LIMIT_EXCEEDED;
504 newreq->starttime = oldreq->starttime;
505 newreq->timeout = oldreq->timeout - (now - oldreq->starttime);
512 set the permissions for new files to be passed to open() in
513 backends that use local files
515 void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
517 ldb->create_perms = perms;
522 NOTE: the request must be a talloc context.
523 returns LDB_ERR_* on errors.
525 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
527 struct ldb_module *module;
530 ldb_reset_err_string(ldb);
532 /* call the first module in the chain */
533 switch (req->operation) {
535 FIRST_OP(ldb, search);
536 ret = module->ops->search(module, req);
540 ret = module->ops->add(module, req);
543 FIRST_OP(ldb, modify);
544 ret = module->ops->modify(module, req);
548 ret = module->ops->del(module, req);
551 FIRST_OP(ldb, rename);
552 ret = module->ops->rename(module, req);
555 FIRST_OP(ldb, extended);
556 ret = module->ops->extended(module, req);
558 case LDB_SEQUENCE_NUMBER:
559 FIRST_OP(ldb, sequence_number);
560 ret = module->ops->sequence_number(module, req);
563 FIRST_OP(ldb, request);
564 ret = module->ops->request(module, req);
572 search the database given a LDAP-like search expression
574 returns an LDB error code
576 Use talloc_free to free the ldb_message returned in 'res', if successful
579 int ldb_search_default_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
581 struct ldb_result *res;
585 ldb_set_errstring(ldb, "NULL Context in callback");
586 return LDB_ERR_OPERATIONS_ERROR;
589 res = talloc_get_type(context, struct ldb_result);
592 ldb_set_errstring(ldb, "NULL res or ares in callback");
596 switch (ares->type) {
597 case LDB_REPLY_ENTRY:
598 res->msgs = talloc_realloc(res, res->msgs, struct ldb_message *, res->count + 2);
603 res->msgs[res->count + 1] = NULL;
605 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
608 case LDB_REPLY_REFERRAL:
610 for (n = 0; res->refs[n]; n++) /*noop*/ ;
615 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
620 res->refs[n] = talloc_move(res->refs, &ares->referral);
621 res->refs[n + 1] = NULL;
623 case LDB_REPLY_EXTENDED:
625 /* TODO: we should really support controls on entries and referrals too! */
626 res->controls = talloc_move(res, &ares->controls);
634 return LDB_ERR_OPERATIONS_ERROR;
637 int ldb_build_search_req(struct ldb_request **ret_req,
638 struct ldb_context *ldb,
641 enum ldb_scope scope,
642 const char *expression,
643 const char * const *attrs,
644 struct ldb_control **controls,
646 ldb_request_callback_t callback)
648 struct ldb_request *req;
652 req = talloc(mem_ctx, struct ldb_request);
654 ldb_set_errstring(ldb, "Out of Memory");
655 return LDB_ERR_OPERATIONS_ERROR;
658 req->operation = LDB_SEARCH;
660 req->op.search.base = ldb_dn_new(req, ldb, NULL);
662 req->op.search.base = base;
664 req->op.search.scope = scope;
666 req->op.search.tree = ldb_parse_tree(req, expression);
667 if (req->op.search.tree == NULL) {
668 ldb_set_errstring(ldb, "Unable to parse search expression");
670 return LDB_ERR_OPERATIONS_ERROR;
673 req->op.search.attrs = attrs;
674 req->controls = controls;
675 req->context = context;
676 req->callback = callback;
682 int ldb_build_add_req(struct ldb_request **ret_req,
683 struct ldb_context *ldb,
685 const struct ldb_message *message,
686 struct ldb_control **controls,
688 ldb_request_callback_t callback)
690 struct ldb_request *req;
694 req = talloc(mem_ctx, struct ldb_request);
696 ldb_set_errstring(ldb, "Out of Memory");
697 return LDB_ERR_OPERATIONS_ERROR;
700 req->operation = LDB_ADD;
701 req->op.add.message = message;
702 req->controls = controls;
703 req->context = context;
704 req->callback = callback;
711 int ldb_build_mod_req(struct ldb_request **ret_req,
712 struct ldb_context *ldb,
714 const struct ldb_message *message,
715 struct ldb_control **controls,
717 ldb_request_callback_t callback)
719 struct ldb_request *req;
723 req = talloc(mem_ctx, struct ldb_request);
725 ldb_set_errstring(ldb, "Out of Memory");
726 return LDB_ERR_OPERATIONS_ERROR;
729 req->operation = LDB_MODIFY;
730 req->op.mod.message = message;
731 req->controls = controls;
732 req->context = context;
733 req->callback = callback;
740 int ldb_build_del_req(struct ldb_request **ret_req,
741 struct ldb_context *ldb,
744 struct ldb_control **controls,
746 ldb_request_callback_t callback)
748 struct ldb_request *req;
752 req = talloc(mem_ctx, struct ldb_request);
754 ldb_set_errstring(ldb, "Out of Memory");
755 return LDB_ERR_OPERATIONS_ERROR;
758 req->operation = LDB_DELETE;
760 req->controls = controls;
761 req->context = context;
762 req->callback = callback;
769 int ldb_build_rename_req(struct ldb_request **ret_req,
770 struct ldb_context *ldb,
772 struct ldb_dn *olddn,
773 struct ldb_dn *newdn,
774 struct ldb_control **controls,
776 ldb_request_callback_t callback)
778 struct ldb_request *req;
782 req = talloc(mem_ctx, struct ldb_request);
784 ldb_set_errstring(ldb, "Out of Memory");
785 return LDB_ERR_OPERATIONS_ERROR;
788 req->operation = LDB_RENAME;
789 req->op.rename.olddn = olddn;
790 req->op.rename.newdn = newdn;
791 req->controls = controls;
792 req->context = context;
793 req->callback = callback;
800 int ldb_extended_default_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
802 struct ldb_result *res;
805 ldb_set_errstring(ldb, "NULL Context in callback");
806 return LDB_ERR_OPERATIONS_ERROR;
809 res = talloc_get_type(context, struct ldb_result);
811 ldb_set_errstring(ldb, "NULL res or ares in callback");
815 switch (ares->type) {
816 case LDB_REPLY_ENTRY:
817 case LDB_REPLY_REFERRAL:
819 ldb_set_errstring(ldb, "invalid ares type in callback");
821 case LDB_REPLY_EXTENDED:
822 /* TODO: we should really support controls on entries and referrals too! */
823 res->extended = talloc_move(res, &ares->response);
824 res->controls = talloc_move(res, &ares->controls);
832 return LDB_ERR_OPERATIONS_ERROR;
835 int ldb_build_extended_req(struct ldb_request **ret_req,
836 struct ldb_context *ldb,
840 struct ldb_control **controls,
842 ldb_request_callback_t callback)
844 struct ldb_request *req;
848 req = talloc(mem_ctx, struct ldb_request);
850 ldb_set_errstring(ldb, "Out of Memory");
851 return LDB_ERR_OPERATIONS_ERROR;
854 req->operation = LDB_EXTENDED;
855 req->op.extended.oid = oid;
856 req->op.extended.data = data;
857 req->controls = controls;
858 req->context = context;
859 req->callback = callback;
866 int ldb_extended(struct ldb_context *ldb,
869 struct ldb_result **_res)
871 struct ldb_request *req;
873 struct ldb_result *res;
877 res = talloc_zero(ldb, struct ldb_result);
879 return LDB_ERR_OPERATIONS_ERROR;
882 ret = ldb_build_extended_req(&req, ldb, ldb,
884 res, ldb_extended_default_callback);
885 if (ret != LDB_SUCCESS) goto done;
887 ldb_set_timeout(ldb, req, 0); /* use default timeout */
889 ret = ldb_request(ldb, req);
891 if (ret == LDB_SUCCESS) {
892 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
898 if (ret != LDB_SUCCESS) {
907 note that ldb_search() will automatically replace a NULL 'base' value with the
908 defaultNamingContext from the rootDSE if available.
910 int ldb_search(struct ldb_context *ldb,
912 enum ldb_scope scope,
913 const char *expression,
914 const char * const *attrs,
915 struct ldb_result **_res)
917 struct ldb_request *req;
919 struct ldb_result *res;
923 res = talloc_zero(ldb, struct ldb_result);
925 return LDB_ERR_OPERATIONS_ERROR;
928 ret = ldb_build_search_req(&req, ldb, ldb,
929 base?base:ldb_get_default_basedn(ldb),
935 ldb_search_default_callback);
937 if (ret != LDB_SUCCESS) goto done;
939 ldb_set_timeout(ldb, req, 0); /* use default timeout */
941 ret = ldb_request(ldb, req);
943 if (ret == LDB_SUCCESS) {
944 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
950 if (ret != LDB_SUCCESS) {
959 a useful search function where you can easily define the expression and that
960 takes a memory context where results are allocated
963 int ldb_search_exp_fmt(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_result **result,
964 struct ldb_dn *base, enum ldb_scope scope, const char * const *attrs,
965 const char *exp_fmt, ...)
967 struct ldb_result *res;
975 va_start(ap, exp_fmt);
976 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
980 return LDB_ERR_OPERATIONS_ERROR;
983 ret = ldb_search(ldb, base, scope, expression, attrs, &res);
985 if (ret == LDB_SUCCESS) {
986 talloc_steal(mem_ctx, res);
990 talloc_free(expression);
996 add a record to the database. Will fail if a record with the given class and key
999 int ldb_add(struct ldb_context *ldb,
1000 const struct ldb_message *message)
1002 struct ldb_request *req;
1005 ret = ldb_msg_sanity_check(ldb, message);
1006 if (ret != LDB_SUCCESS) {
1010 ret = ldb_build_add_req(&req, ldb, ldb,
1016 if (ret != LDB_SUCCESS) return ret;
1018 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1020 /* do request and autostart a transaction */
1021 ret = ldb_autotransaction_request(ldb, req);
1028 modify the specified attributes of a record
1030 int ldb_modify(struct ldb_context *ldb,
1031 const struct ldb_message *message)
1033 struct ldb_request *req;
1036 ret = ldb_msg_sanity_check(ldb, message);
1037 if (ret != LDB_SUCCESS) {
1041 ret = ldb_build_mod_req(&req, ldb, ldb,
1047 if (ret != LDB_SUCCESS) return ret;
1049 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1051 /* do request and autostart a transaction */
1052 ret = ldb_autotransaction_request(ldb, req);
1060 delete a record from the database
1062 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1064 struct ldb_request *req;
1067 ret = ldb_build_del_req(&req, ldb, ldb,
1073 if (ret != LDB_SUCCESS) return ret;
1075 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1077 /* do request and autostart a transaction */
1078 ret = ldb_autotransaction_request(ldb, req);
1085 rename a record in the database
1087 int ldb_rename(struct ldb_context *ldb, struct ldb_dn *olddn, struct ldb_dn *newdn)
1089 struct ldb_request *req;
1092 ret = ldb_build_rename_req(&req, ldb, ldb,
1099 if (ret != LDB_SUCCESS) return ret;
1101 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1103 /* do request and autostart a transaction */
1104 ret = ldb_autotransaction_request(ldb, req);
1112 return the global sequence number
1114 int ldb_sequence_number(struct ldb_context *ldb, enum ldb_sequence_type type, uint64_t *seq_num)
1116 struct ldb_request *req;
1119 req = talloc(ldb, struct ldb_request);
1121 ldb_set_errstring(ldb, "Out of Memory");
1122 return LDB_ERR_OPERATIONS_ERROR;
1125 req->operation = LDB_SEQUENCE_NUMBER;
1126 req->controls = NULL;
1127 req->context = NULL;
1128 req->callback = NULL;
1129 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1131 req->op.seq_num.type = type;
1132 /* do request and autostart a transaction */
1133 ret = ldb_request(ldb, req);
1135 if (ret == LDB_SUCCESS) {
1136 *seq_num = req->op.seq_num.seq_num;
1146 return extended error information
1148 const char *ldb_errstring(struct ldb_context *ldb)
1150 if (ldb->err_string) {
1151 return ldb->err_string;
1158 return a string explaining what a ldb error constant meancs
1160 const char *ldb_strerror(int ldb_err)
1165 case LDB_ERR_OPERATIONS_ERROR:
1166 return "Operations error";
1167 case LDB_ERR_PROTOCOL_ERROR:
1168 return "Protocol error";
1169 case LDB_ERR_TIME_LIMIT_EXCEEDED:
1170 return "Time limit exceeded";
1171 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1172 return "Size limit exceeded";
1173 case LDB_ERR_COMPARE_FALSE:
1174 return "Compare false";
1175 case LDB_ERR_COMPARE_TRUE:
1176 return "Compare true";
1177 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1178 return "Auth method not supported";
1179 case LDB_ERR_STRONG_AUTH_REQUIRED:
1180 return "Strong auth required";
1182 case LDB_ERR_REFERRAL:
1183 return "Referral error";
1184 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1185 return "Admin limit exceeded";
1186 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1187 return "Unsupported critical extension";
1188 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1189 return "Confidentiality required";
1190 case LDB_ERR_SASL_BIND_IN_PROGRESS:
1191 return "SASL bind in progress";
1192 case LDB_ERR_NO_SUCH_ATTRIBUTE:
1193 return "No such attribute";
1194 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1195 return "Undefined attribute type";
1196 case LDB_ERR_INAPPROPRIATE_MATCHING:
1197 return "Inappropriate matching";
1198 case LDB_ERR_CONSTRAINT_VIOLATION:
1199 return "Constraint violation";
1200 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1201 return "Attribute or value exists";
1202 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1203 return "Invalid attribute syntax";
1205 case LDB_ERR_NO_SUCH_OBJECT:
1206 return "No such object";
1207 case LDB_ERR_ALIAS_PROBLEM:
1208 return "Alias problem";
1209 case LDB_ERR_INVALID_DN_SYNTAX:
1210 return "Invalid DN syntax";
1212 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1213 return "Alias dereferencing problem";
1215 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1216 return "Inappropriate authentication";
1217 case LDB_ERR_INVALID_CREDENTIALS:
1218 return "Invalid credentials";
1219 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1220 return "insufficient access rights";
1223 case LDB_ERR_UNAVAILABLE:
1224 return "Unavailable";
1225 case LDB_ERR_UNWILLING_TO_PERFORM:
1226 return "Unwilling to perform";
1227 case LDB_ERR_LOOP_DETECT:
1228 return "Loop detect";
1230 case LDB_ERR_NAMING_VIOLATION:
1231 return "Naming violation";
1232 case LDB_ERR_OBJECT_CLASS_VIOLATION:
1233 return "Object class violation";
1234 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1235 return "Not allowed on non-leaf";
1236 case LDB_ERR_NOT_ALLOWED_ON_RDN:
1237 return "Not allowed on RDN";
1238 case LDB_ERR_ENTRY_ALREADY_EXISTS:
1239 return "Entry already exists";
1240 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1241 return "Object class mods prohibited";
1242 /* 70 RESERVED FOR CLDAP */
1243 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1244 return "Affects multiple DSAs";
1250 return "Unknown error";
1254 set backend specific opaque parameters
1256 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1258 struct ldb_opaque *o;
1260 /* allow updating an existing value */
1261 for (o=ldb->opaque;o;o=o->next) {
1262 if (strcmp(o->name, name) == 0) {
1268 o = talloc(ldb, struct ldb_opaque);
1271 return LDB_ERR_OTHER;
1273 o->next = ldb->opaque;
1281 get a previously set opaque value
1283 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1285 struct ldb_opaque *o;
1286 for (o=ldb->opaque;o;o=o->next) {
1287 if (strcmp(o->name, name) == 0) {