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
37 #include "ldb/include/includes.h"
40 initialise a ldb context
41 The mem_ctx is optional
43 struct ldb_context *ldb_init(void *mem_ctx)
45 struct ldb_context *ldb = talloc_zero(mem_ctx, struct ldb_context);
48 ret = ldb_setup_wellknown_attributes(ldb);
54 ldb_set_utf8_default(ldb);
55 ldb_set_create_perms(ldb, 0666);
60 static struct ldb_backend {
62 ldb_connect_fn connect_fn;
63 struct ldb_backend *prev, *next;
64 } *ldb_backends = NULL;
67 static ldb_connect_fn ldb_find_backend(const char *url)
69 struct ldb_backend *backend;
71 for (backend = ldb_backends; backend; backend = backend->next) {
72 if (strncmp(backend->name, url, strlen(backend->name)) == 0) {
73 return backend->connect_fn;
81 register a new ldb backend
83 int ldb_register_backend(const char *url_prefix, ldb_connect_fn connectfn)
85 struct ldb_backend *backend = talloc(talloc_autofree_context(), struct ldb_backend);
87 if (ldb_find_backend(url_prefix)) {
91 /* Maybe check for duplicity here later on? */
93 backend->name = talloc_strdup(backend, url_prefix);
94 backend->connect_fn = connectfn;
95 DLIST_ADD(ldb_backends, backend);
101 Return the ldb module form of a database. The URL can either be one of the following forms
105 flags is made up of LDB_FLG_*
107 the options are passed uninterpreted to the backend, and are
110 This allows modules to get at only the backend module, for example where a module
111 may wish to direct certain requests at a particular backend.
113 int ldb_connect_backend(struct ldb_context *ldb, const char *url, const char *options[],
114 struct ldb_module **backend_module)
120 if (strchr(url, ':') != NULL) {
121 backend = talloc_strndup(ldb, url, strchr(url, ':')-url);
124 backend = talloc_strdup(ldb, "tdb");
127 fn = ldb_find_backend(backend);
130 if (ldb_try_load_dso(ldb, backend) == 0) {
131 fn = ldb_find_backend(backend);
135 talloc_free(backend);
138 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to find backend for '%s'\n", url);
139 return LDB_ERR_OTHER;
142 ret = fn(ldb, url, ldb->flags, options, backend_module);
144 if (ret != LDB_SUCCESS) {
145 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to '%s'\n", url);
152 try to autodetect a basedn if none specified. This fixes one of my
153 pet hates about ldapsearch, which is that you have to get a long,
154 complex basedn right to make any use of it.
156 void ldb_set_default_dns(struct ldb_context *ldb)
160 struct ldb_result *res;
161 struct ldb_dn *tmp_dn=NULL;
162 static const char *attrs[] = {
163 "rootDomainNamingContext",
164 "configurationNamingContext",
165 "schemaNamingContext",
166 "defaultNamingContext",
170 tmp_ctx = talloc_new(ldb);
171 ret = ldb_search(ldb, ldb_dn_new(tmp_ctx, ldb, NULL), LDB_SCOPE_BASE,
172 "(objectClass=*)", attrs, &res);
173 if (ret == LDB_SUCCESS) {
174 if (res->count == 1) {
175 if (!ldb_get_opaque(ldb, "rootDomainNamingContext")) {
176 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "rootDomainNamingContext");
177 ldb_set_opaque(ldb, "rootDomainNamingContext", tmp_dn);
180 if (!ldb_get_opaque(ldb, "configurationNamingContext")) {
181 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "configurationNamingContext");
182 ldb_set_opaque(ldb, "configurationNamingContext", tmp_dn);
185 if (!ldb_get_opaque(ldb, "schemaNamingContext")) {
186 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "schemaNamingContext");
187 ldb_set_opaque(ldb, "schemaNamingContext", tmp_dn);
190 if (!ldb_get_opaque(ldb, "defaultNamingContext")) {
191 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "defaultNamingContext");
192 ldb_set_opaque(ldb, "defaultNamingContext", tmp_dn);
198 talloc_free(tmp_ctx);
201 struct ldb_dn *ldb_get_root_basedn(struct ldb_context *ldb)
203 return talloc_get_type(ldb_get_opaque(ldb, "rootDomainNamingContext"), struct ldb_dn);
206 struct ldb_dn *ldb_get_config_basedn(struct ldb_context *ldb)
208 return talloc_get_type(ldb_get_opaque(ldb, "configurationNamingContext"), struct ldb_dn);
211 struct ldb_dn *ldb_get_schema_basedn(struct ldb_context *ldb)
213 return talloc_get_type(ldb_get_opaque(ldb, "schemaNamingContext"), struct ldb_dn);
216 struct ldb_dn *ldb_get_default_basedn(struct ldb_context *ldb)
218 return talloc_get_type(ldb_get_opaque(ldb, "defaultNamingContext"), struct ldb_dn);
222 connect to a database. The URL can either be one of the following forms
226 flags is made up of LDB_FLG_*
228 the options are passed uninterpreted to the backend, and are
231 int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[])
235 /* We seem to need to do this here, or else some utilities don't get ldb backends */
240 url2 = talloc_strdup(ldb, url);
243 return LDB_ERR_OPERATIONS_ERROR;
245 ret = ldb_set_opaque(ldb, "ldb_url", talloc_strdup(ldb, url2));
246 if (ret != LDB_SUCCESS) {
250 ret = ldb_connect_backend(ldb, url, options, &ldb->modules);
251 if (ret != LDB_SUCCESS) {
255 if (ldb_load_modules(ldb, options) != LDB_SUCCESS) {
256 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to load modules for %s: %s\n",
257 url, ldb_errstring(ldb));
258 return LDB_ERR_OTHER;
261 /* TODO: get timeout from options if available there */
262 ldb->default_timeout = 300; /* set default to 5 minutes */
264 /* set the default base dn */
265 ldb_set_default_dns(ldb);
270 void ldb_set_errstring(struct ldb_context *ldb, const char *err_string)
272 if (ldb->err_string) {
273 talloc_free(ldb->err_string);
275 ldb->err_string = talloc_strdup(ldb, err_string);
278 void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
282 if (ldb->err_string) {
283 talloc_free(ldb->err_string);
286 va_start(ap, format);
287 ldb->err_string = talloc_vasprintf(ldb, format, ap);
291 void ldb_reset_err_string(struct ldb_context *ldb)
293 if (ldb->err_string) {
294 talloc_free(ldb->err_string);
295 ldb->err_string = NULL;
299 #define FIRST_OP(ldb, op) do { \
300 module = ldb->modules; \
301 while (module && module->ops->op == NULL) module = module->next; \
302 if (module == NULL) { \
303 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
304 return LDB_ERR_OPERATIONS_ERROR; \
311 static int ldb_transaction_start_internal(struct ldb_context *ldb)
313 struct ldb_module *module;
315 FIRST_OP(ldb, start_transaction);
317 ldb_reset_err_string(ldb);
319 status = module->ops->start_transaction(module);
320 if (status != LDB_SUCCESS) {
321 if (ldb->err_string == NULL) {
322 /* no error string was setup by the backend */
323 ldb_asprintf_errstring(ldb,
324 "ldb transaction start: %s (%d)",
325 ldb_strerror(status),
335 static int ldb_transaction_commit_internal(struct ldb_context *ldb)
337 struct ldb_module *module;
339 FIRST_OP(ldb, end_transaction);
341 ldb_reset_err_string(ldb);
343 status = module->ops->end_transaction(module);
344 if (status != LDB_SUCCESS) {
345 if (ldb->err_string == NULL) {
346 /* no error string was setup by the backend */
347 ldb_asprintf_errstring(ldb,
348 "ldb transaction commit: %s (%d)",
349 ldb_strerror(status),
359 static int ldb_transaction_cancel_internal(struct ldb_context *ldb)
361 struct ldb_module *module;
363 FIRST_OP(ldb, del_transaction);
365 status = module->ops->del_transaction(module);
366 if (status != LDB_SUCCESS) {
367 if (ldb->err_string == NULL) {
368 /* no error string was setup by the backend */
369 ldb_asprintf_errstring(ldb,
370 "ldb transaction cancel: %s (%d)",
371 ldb_strerror(status),
378 int ldb_transaction_start(struct ldb_context *ldb)
380 /* disable autotransactions */
381 ldb->transaction_active++;
383 return ldb_transaction_start_internal(ldb);
386 int ldb_transaction_commit(struct ldb_context *ldb)
388 /* renable autotransactions (when we reach 0) */
389 if (ldb->transaction_active > 0)
390 ldb->transaction_active--;
392 return ldb_transaction_commit_internal(ldb);
395 int ldb_transaction_cancel(struct ldb_context *ldb)
397 /* renable autotransactions (when we reach 0) */
398 if (ldb->transaction_active > 0)
399 ldb->transaction_active--;
401 return ldb_transaction_cancel_internal(ldb);
404 static int ldb_autotransaction_start(struct ldb_context *ldb)
406 /* explicit transaction active, ignore autotransaction request */
407 if (ldb->transaction_active)
410 return ldb_transaction_start_internal(ldb);
413 static int ldb_autotransaction_commit(struct ldb_context *ldb)
415 /* explicit transaction active, ignore autotransaction request */
416 if (ldb->transaction_active)
419 return ldb_transaction_commit_internal(ldb);
422 static int ldb_autotransaction_cancel(struct ldb_context *ldb)
424 /* explicit transaction active, ignore autotransaction request */
425 if (ldb->transaction_active)
428 return ldb_transaction_cancel_internal(ldb);
431 /* autostarts a transacion if none active */
432 static int ldb_autotransaction_request(struct ldb_context *ldb, struct ldb_request *req)
436 ret = ldb_autotransaction_start(ldb);
437 if (ret != LDB_SUCCESS) {
441 ret = ldb_request(ldb, req);
442 if (ret == LDB_SUCCESS) {
443 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
446 if (ret == LDB_SUCCESS) {
447 return ldb_autotransaction_commit(ldb);
449 ldb_autotransaction_cancel(ldb);
451 if (ldb->err_string == NULL) {
452 /* no error string was setup by the backend */
453 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
459 int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
465 return handle->module->ops->wait(handle, type);
468 /* set the specified timeout or, if timeout is 0 set the default timeout */
469 /* timeout == -1 means no timeout */
470 int ldb_set_timeout(struct ldb_context *ldb, struct ldb_request *req, int timeout)
472 if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
475 req->timeout = timeout;
477 req->timeout = ldb->default_timeout;
479 req->starttime = time(NULL);
484 /* calculates the new timeout based on the previous starttime and timeout */
485 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb, struct ldb_request *oldreq, struct ldb_request *newreq)
489 if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
494 return ldb_set_timeout(ldb, newreq, 0);
496 if ((now - oldreq->starttime) > oldreq->timeout) {
497 return LDB_ERR_TIME_LIMIT_EXCEEDED;
499 newreq->starttime = oldreq->starttime;
500 newreq->timeout = oldreq->timeout - (now - oldreq->starttime);
507 set the permissions for new files to be passed to open() in
508 backends that use local files
510 void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
512 ldb->create_perms = perms;
517 NOTE: the request must be a talloc context.
518 returns LDB_ERR_* on errors.
520 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
522 struct ldb_module *module;
525 ldb_reset_err_string(ldb);
527 /* call the first module in the chain */
528 switch (req->operation) {
530 FIRST_OP(ldb, search);
531 ret = module->ops->search(module, req);
535 ret = module->ops->add(module, req);
538 FIRST_OP(ldb, modify);
539 ret = module->ops->modify(module, req);
543 ret = module->ops->del(module, req);
546 FIRST_OP(ldb, rename);
547 ret = module->ops->rename(module, req);
550 FIRST_OP(ldb, extended);
551 ret = module->ops->extended(module, req);
553 case LDB_SEQUENCE_NUMBER:
554 FIRST_OP(ldb, sequence_number);
555 ret = module->ops->sequence_number(module, req);
558 FIRST_OP(ldb, request);
559 ret = module->ops->request(module, req);
567 search the database given a LDAP-like search expression
569 returns an LDB error code
571 Use talloc_free to free the ldb_message returned in 'res', if successful
574 int ldb_search_default_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
576 struct ldb_result *res;
580 ldb_set_errstring(ldb, "NULL Context in callback");
581 return LDB_ERR_OPERATIONS_ERROR;
584 res = talloc_get_type(context, struct ldb_result);
587 ldb_set_errstring(ldb, "NULL res or ares in callback");
591 switch (ares->type) {
592 case LDB_REPLY_ENTRY:
593 res->msgs = talloc_realloc(res, res->msgs, struct ldb_message *, res->count + 2);
598 res->msgs[res->count + 1] = NULL;
600 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
603 case LDB_REPLY_REFERRAL:
605 for (n = 0; res->refs[n]; n++) /*noop*/ ;
610 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
615 res->refs[n] = talloc_move(res->refs, &ares->referral);
616 res->refs[n + 1] = NULL;
618 case LDB_REPLY_EXTENDED:
620 /* TODO: we should really support controls on entries and referrals too! */
621 res->controls = talloc_move(res, &ares->controls);
629 return LDB_ERR_OPERATIONS_ERROR;
632 int ldb_build_search_req(struct ldb_request **ret_req,
633 struct ldb_context *ldb,
636 enum ldb_scope scope,
637 const char *expression,
638 const char * const *attrs,
639 struct ldb_control **controls,
641 ldb_request_callback_t callback)
643 struct ldb_request *req;
647 req = talloc(mem_ctx, struct ldb_request);
649 ldb_set_errstring(ldb, "Out of Memory");
650 return LDB_ERR_OPERATIONS_ERROR;
653 req->operation = LDB_SEARCH;
655 req->op.search.base = ldb_dn_new(req, ldb, NULL);
657 req->op.search.base = base;
659 req->op.search.scope = scope;
661 req->op.search.tree = ldb_parse_tree(req, expression);
662 if (req->op.search.tree == NULL) {
663 ldb_set_errstring(ldb, "Unable to parse search expression");
665 return LDB_ERR_OPERATIONS_ERROR;
668 req->op.search.attrs = attrs;
669 req->controls = controls;
670 req->context = context;
671 req->callback = callback;
677 int ldb_build_add_req(struct ldb_request **ret_req,
678 struct ldb_context *ldb,
680 const struct ldb_message *message,
681 struct ldb_control **controls,
683 ldb_request_callback_t callback)
685 struct ldb_request *req;
689 req = talloc(mem_ctx, struct ldb_request);
691 ldb_set_errstring(ldb, "Out of Memory");
692 return LDB_ERR_OPERATIONS_ERROR;
695 req->operation = LDB_ADD;
696 req->op.add.message = message;
697 req->controls = controls;
698 req->context = context;
699 req->callback = callback;
706 int ldb_build_mod_req(struct ldb_request **ret_req,
707 struct ldb_context *ldb,
709 const struct ldb_message *message,
710 struct ldb_control **controls,
712 ldb_request_callback_t callback)
714 struct ldb_request *req;
718 req = talloc(mem_ctx, struct ldb_request);
720 ldb_set_errstring(ldb, "Out of Memory");
721 return LDB_ERR_OPERATIONS_ERROR;
724 req->operation = LDB_MODIFY;
725 req->op.mod.message = message;
726 req->controls = controls;
727 req->context = context;
728 req->callback = callback;
735 int ldb_build_del_req(struct ldb_request **ret_req,
736 struct ldb_context *ldb,
739 struct ldb_control **controls,
741 ldb_request_callback_t callback)
743 struct ldb_request *req;
747 req = talloc(mem_ctx, struct ldb_request);
749 ldb_set_errstring(ldb, "Out of Memory");
750 return LDB_ERR_OPERATIONS_ERROR;
753 req->operation = LDB_DELETE;
755 req->controls = controls;
756 req->context = context;
757 req->callback = callback;
764 int ldb_build_rename_req(struct ldb_request **ret_req,
765 struct ldb_context *ldb,
767 struct ldb_dn *olddn,
768 struct ldb_dn *newdn,
769 struct ldb_control **controls,
771 ldb_request_callback_t callback)
773 struct ldb_request *req;
777 req = talloc(mem_ctx, struct ldb_request);
779 ldb_set_errstring(ldb, "Out of Memory");
780 return LDB_ERR_OPERATIONS_ERROR;
783 req->operation = LDB_RENAME;
784 req->op.rename.olddn = olddn;
785 req->op.rename.newdn = newdn;
786 req->controls = controls;
787 req->context = context;
788 req->callback = callback;
795 int ldb_extended_default_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
797 struct ldb_result *res;
800 ldb_set_errstring(ldb, "NULL Context in callback");
801 return LDB_ERR_OPERATIONS_ERROR;
804 res = talloc_get_type(context, struct ldb_result);
806 ldb_set_errstring(ldb, "NULL res or ares in callback");
810 switch (ares->type) {
811 case LDB_REPLY_ENTRY:
812 case LDB_REPLY_REFERRAL:
814 ldb_set_errstring(ldb, "invalid ares type in callback");
816 case LDB_REPLY_EXTENDED:
817 /* TODO: we should really support controls on entries and referrals too! */
818 res->extended = talloc_move(res, &ares->response);
819 res->controls = talloc_move(res, &ares->controls);
827 return LDB_ERR_OPERATIONS_ERROR;
830 int ldb_build_extended_req(struct ldb_request **ret_req,
831 struct ldb_context *ldb,
835 struct ldb_control **controls,
837 ldb_request_callback_t callback)
839 struct ldb_request *req;
843 req = talloc(mem_ctx, struct ldb_request);
845 ldb_set_errstring(ldb, "Out of Memory");
846 return LDB_ERR_OPERATIONS_ERROR;
849 req->operation = LDB_EXTENDED;
850 req->op.extended.oid = oid;
851 req->op.extended.data = data;
852 req->controls = controls;
853 req->context = context;
854 req->callback = callback;
861 int ldb_extended(struct ldb_context *ldb,
864 struct ldb_result **_res)
866 struct ldb_request *req;
868 struct ldb_result *res;
872 res = talloc_zero(ldb, struct ldb_result);
874 return LDB_ERR_OPERATIONS_ERROR;
877 ret = ldb_build_extended_req(&req, ldb, ldb,
879 res, ldb_extended_default_callback);
880 if (ret != LDB_SUCCESS) goto done;
882 ldb_set_timeout(ldb, req, 0); /* use default timeout */
884 ret = ldb_request(ldb, req);
886 if (ret == LDB_SUCCESS) {
887 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
893 if (ret != LDB_SUCCESS) {
902 note that ldb_search() will automatically replace a NULL 'base' value with the
903 defaultNamingContext from the rootDSE if available.
905 int ldb_search(struct ldb_context *ldb,
907 enum ldb_scope scope,
908 const char *expression,
909 const char * const *attrs,
910 struct ldb_result **_res)
912 struct ldb_request *req;
914 struct ldb_result *res;
918 res = talloc_zero(ldb, struct ldb_result);
920 return LDB_ERR_OPERATIONS_ERROR;
923 ret = ldb_build_search_req(&req, ldb, ldb,
924 base?base:ldb_get_default_basedn(ldb),
930 ldb_search_default_callback);
932 if (ret != LDB_SUCCESS) goto done;
934 ldb_set_timeout(ldb, req, 0); /* use default timeout */
936 ret = ldb_request(ldb, req);
938 if (ret == LDB_SUCCESS) {
939 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
945 if (ret != LDB_SUCCESS) {
954 a useful search function where you can easily define the expression and that
955 takes a memory context where results are allocated
958 int ldb_search_exp_fmt(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_result **result,
959 struct ldb_dn *base, enum ldb_scope scope, const char * const *attrs,
960 const char *exp_fmt, ...)
962 struct ldb_result *res;
970 va_start(ap, exp_fmt);
971 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
975 return LDB_ERR_OPERATIONS_ERROR;
978 ret = ldb_search(ldb, base, scope, expression, attrs, &res);
980 if (ret == LDB_SUCCESS) {
981 talloc_steal(mem_ctx, res);
985 talloc_free(expression);
991 add a record to the database. Will fail if a record with the given class and key
994 int ldb_add(struct ldb_context *ldb,
995 const struct ldb_message *message)
997 struct ldb_request *req;
1000 ret = ldb_msg_sanity_check(ldb, message);
1001 if (ret != LDB_SUCCESS) {
1005 ret = ldb_build_add_req(&req, ldb, ldb,
1011 if (ret != LDB_SUCCESS) return ret;
1013 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1015 /* do request and autostart a transaction */
1016 ret = ldb_autotransaction_request(ldb, req);
1023 modify the specified attributes of a record
1025 int ldb_modify(struct ldb_context *ldb,
1026 const struct ldb_message *message)
1028 struct ldb_request *req;
1031 ret = ldb_msg_sanity_check(ldb, message);
1032 if (ret != LDB_SUCCESS) {
1036 ret = ldb_build_mod_req(&req, ldb, ldb,
1042 if (ret != LDB_SUCCESS) return ret;
1044 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1046 /* do request and autostart a transaction */
1047 ret = ldb_autotransaction_request(ldb, req);
1055 delete a record from the database
1057 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1059 struct ldb_request *req;
1062 ret = ldb_build_del_req(&req, ldb, ldb,
1068 if (ret != LDB_SUCCESS) return ret;
1070 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1072 /* do request and autostart a transaction */
1073 ret = ldb_autotransaction_request(ldb, req);
1080 rename a record in the database
1082 int ldb_rename(struct ldb_context *ldb, struct ldb_dn *olddn, struct ldb_dn *newdn)
1084 struct ldb_request *req;
1087 ret = ldb_build_rename_req(&req, ldb, ldb,
1094 if (ret != LDB_SUCCESS) return ret;
1096 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1098 /* do request and autostart a transaction */
1099 ret = ldb_autotransaction_request(ldb, req);
1107 return the global sequence number
1109 int ldb_sequence_number(struct ldb_context *ldb, enum ldb_sequence_type type, uint64_t *seq_num)
1111 struct ldb_request *req;
1114 req = talloc(ldb, struct ldb_request);
1116 ldb_set_errstring(ldb, "Out of Memory");
1117 return LDB_ERR_OPERATIONS_ERROR;
1120 req->operation = LDB_SEQUENCE_NUMBER;
1121 req->controls = NULL;
1122 req->context = NULL;
1123 req->callback = NULL;
1124 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1126 req->op.seq_num.type = type;
1127 /* do request and autostart a transaction */
1128 ret = ldb_request(ldb, req);
1130 if (ret == LDB_SUCCESS) {
1131 *seq_num = req->op.seq_num.seq_num;
1141 return extended error information
1143 const char *ldb_errstring(struct ldb_context *ldb)
1145 if (ldb->err_string) {
1146 return ldb->err_string;
1153 return a string explaining what a ldb error constant meancs
1155 const char *ldb_strerror(int ldb_err)
1160 case LDB_ERR_OPERATIONS_ERROR:
1161 return "Operations error";
1162 case LDB_ERR_PROTOCOL_ERROR:
1163 return "Protocol error";
1164 case LDB_ERR_TIME_LIMIT_EXCEEDED:
1165 return "Time limit exceeded";
1166 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1167 return "Size limit exceeded";
1168 case LDB_ERR_COMPARE_FALSE:
1169 return "Compare false";
1170 case LDB_ERR_COMPARE_TRUE:
1171 return "Compare true";
1172 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1173 return "Auth method not supported";
1174 case LDB_ERR_STRONG_AUTH_REQUIRED:
1175 return "Strong auth required";
1177 case LDB_ERR_REFERRAL:
1178 return "Referral error";
1179 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1180 return "Admin limit exceeded";
1181 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1182 return "Unsupported critical extension";
1183 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1184 return "Confidentiality required";
1185 case LDB_ERR_SASL_BIND_IN_PROGRESS:
1186 return "SASL bind in progress";
1187 case LDB_ERR_NO_SUCH_ATTRIBUTE:
1188 return "No such attribute";
1189 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1190 return "Undefined attribute type";
1191 case LDB_ERR_INAPPROPRIATE_MATCHING:
1192 return "Inappropriate matching";
1193 case LDB_ERR_CONSTRAINT_VIOLATION:
1194 return "Constraint violation";
1195 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1196 return "Attribute or value exists";
1197 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1198 return "Invalid attribute syntax";
1200 case LDB_ERR_NO_SUCH_OBJECT:
1201 return "No such object";
1202 case LDB_ERR_ALIAS_PROBLEM:
1203 return "Alias problem";
1204 case LDB_ERR_INVALID_DN_SYNTAX:
1205 return "Invalid DN syntax";
1207 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1208 return "Alias dereferencing problem";
1210 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1211 return "Inappropriate authentication";
1212 case LDB_ERR_INVALID_CREDENTIALS:
1213 return "Invalid credentials";
1214 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1215 return "insufficient access rights";
1218 case LDB_ERR_UNAVAILABLE:
1219 return "Unavailable";
1220 case LDB_ERR_UNWILLING_TO_PERFORM:
1221 return "Unwilling to perform";
1222 case LDB_ERR_LOOP_DETECT:
1223 return "Loop detect";
1225 case LDB_ERR_NAMING_VIOLATION:
1226 return "Naming violation";
1227 case LDB_ERR_OBJECT_CLASS_VIOLATION:
1228 return "Object class violation";
1229 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1230 return "Not allowed on non-leaf";
1231 case LDB_ERR_NOT_ALLOWED_ON_RDN:
1232 return "Not allowed on RDN";
1233 case LDB_ERR_ENTRY_ALREADY_EXISTS:
1234 return "Entry already exists";
1235 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1236 return "Object class mods prohibited";
1237 /* 70 RESERVED FOR CLDAP */
1238 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1239 return "Affects multiple DSAs";
1245 return "Unknown error";
1249 set backend specific opaque parameters
1251 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1253 struct ldb_opaque *o;
1255 /* allow updating an existing value */
1256 for (o=ldb->opaque;o;o=o->next) {
1257 if (strcmp(o->name, name) == 0) {
1263 o = talloc(ldb, struct ldb_opaque);
1266 return LDB_ERR_OTHER;
1268 o->next = ldb->opaque;
1276 get a previously set opaque value
1278 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1280 struct ldb_opaque *o;
1281 for (o=ldb->opaque;o;o=o->next) {
1282 if (strcmp(o->name, name) == 0) {