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);
139 char *symbol_name = talloc_asprintf(ldb, "ldb_%s_connect", backend);
140 if (symbol_name == NULL) {
141 return LDB_ERR_OPERATIONS_ERROR;
143 fn = ldb_dso_load_symbol(ldb, backend, symbol_name);
144 talloc_free(symbol_name);
147 talloc_free(backend);
150 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to find backend for '%s'\n", url);
151 return LDB_ERR_OTHER;
154 ret = fn(ldb, url, ldb->flags, options, backend_module);
156 if (ret != LDB_SUCCESS) {
157 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to '%s'\n", url);
164 try to autodetect a basedn if none specified. This fixes one of my
165 pet hates about ldapsearch, which is that you have to get a long,
166 complex basedn right to make any use of it.
168 void ldb_set_default_dns(struct ldb_context *ldb)
172 struct ldb_result *res;
173 struct ldb_dn *tmp_dn=NULL;
174 static const char *attrs[] = {
175 "rootDomainNamingContext",
176 "configurationNamingContext",
177 "schemaNamingContext",
178 "defaultNamingContext",
182 tmp_ctx = talloc_new(ldb);
183 ret = ldb_search(ldb, ldb_dn_new(tmp_ctx, ldb, NULL), LDB_SCOPE_BASE,
184 "(objectClass=*)", attrs, &res);
185 if (ret == LDB_SUCCESS) {
186 if (res->count == 1) {
187 if (!ldb_get_opaque(ldb, "rootDomainNamingContext")) {
188 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "rootDomainNamingContext");
189 ldb_set_opaque(ldb, "rootDomainNamingContext", tmp_dn);
192 if (!ldb_get_opaque(ldb, "configurationNamingContext")) {
193 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "configurationNamingContext");
194 ldb_set_opaque(ldb, "configurationNamingContext", tmp_dn);
197 if (!ldb_get_opaque(ldb, "schemaNamingContext")) {
198 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "schemaNamingContext");
199 ldb_set_opaque(ldb, "schemaNamingContext", tmp_dn);
202 if (!ldb_get_opaque(ldb, "defaultNamingContext")) {
203 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "defaultNamingContext");
204 ldb_set_opaque(ldb, "defaultNamingContext", tmp_dn);
210 talloc_free(tmp_ctx);
213 struct ldb_dn *ldb_get_root_basedn(struct ldb_context *ldb)
215 return talloc_get_type(ldb_get_opaque(ldb, "rootDomainNamingContext"), struct ldb_dn);
218 struct ldb_dn *ldb_get_config_basedn(struct ldb_context *ldb)
220 return talloc_get_type(ldb_get_opaque(ldb, "configurationNamingContext"), struct ldb_dn);
223 struct ldb_dn *ldb_get_schema_basedn(struct ldb_context *ldb)
225 return talloc_get_type(ldb_get_opaque(ldb, "schemaNamingContext"), struct ldb_dn);
228 struct ldb_dn *ldb_get_default_basedn(struct ldb_context *ldb)
230 return talloc_get_type(ldb_get_opaque(ldb, "defaultNamingContext"), struct ldb_dn);
234 connect to a database. The URL can either be one of the following forms
238 flags is made up of LDB_FLG_*
240 the options are passed uninterpreted to the backend, and are
243 int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[])
247 /* We seem to need to do this here, or else some utilities don't get ldb backends */
252 url2 = talloc_strdup(ldb, url);
255 return LDB_ERR_OPERATIONS_ERROR;
257 ret = ldb_set_opaque(ldb, "ldb_url", talloc_strdup(ldb, url2));
258 if (ret != LDB_SUCCESS) {
262 ret = ldb_connect_backend(ldb, url, options, &ldb->modules);
263 if (ret != LDB_SUCCESS) {
267 if (ldb_load_modules(ldb, options) != LDB_SUCCESS) {
268 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to load modules for %s: %s\n",
269 url, ldb_errstring(ldb));
270 return LDB_ERR_OTHER;
273 /* TODO: get timeout from options if available there */
274 ldb->default_timeout = 300; /* set default to 5 minutes */
276 /* set the default base dn */
277 ldb_set_default_dns(ldb);
282 void ldb_set_errstring(struct ldb_context *ldb, const char *err_string)
284 if (ldb->err_string) {
285 talloc_free(ldb->err_string);
287 ldb->err_string = talloc_strdup(ldb, err_string);
290 void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
293 char *old_string = NULL;
295 if (ldb->err_string) {
296 old_string = ldb->err_string;
299 va_start(ap, format);
300 ldb->err_string = talloc_vasprintf(ldb, format, ap);
302 talloc_free(old_string);
305 void ldb_reset_err_string(struct ldb_context *ldb)
307 if (ldb->err_string) {
308 talloc_free(ldb->err_string);
309 ldb->err_string = NULL;
313 #define FIRST_OP(ldb, op) do { \
314 module = ldb->modules; \
315 while (module && module->ops->op == NULL) module = module->next; \
316 if (module == NULL) { \
317 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
318 return LDB_ERR_OPERATIONS_ERROR; \
325 static int ldb_transaction_start_internal(struct ldb_context *ldb)
327 struct ldb_module *module;
329 FIRST_OP(ldb, start_transaction);
331 ldb_reset_err_string(ldb);
333 status = module->ops->start_transaction(module);
334 if (status != LDB_SUCCESS) {
335 if (ldb->err_string == NULL) {
336 /* no error string was setup by the backend */
337 ldb_asprintf_errstring(ldb,
338 "ldb transaction start: %s (%d)",
339 ldb_strerror(status),
349 static int ldb_transaction_commit_internal(struct ldb_context *ldb)
351 struct ldb_module *module;
353 FIRST_OP(ldb, end_transaction);
355 ldb_reset_err_string(ldb);
357 status = module->ops->end_transaction(module);
358 if (status != LDB_SUCCESS) {
359 if (ldb->err_string == NULL) {
360 /* no error string was setup by the backend */
361 ldb_asprintf_errstring(ldb,
362 "ldb transaction commit: %s (%d)",
363 ldb_strerror(status),
373 static int ldb_transaction_cancel_internal(struct ldb_context *ldb)
375 struct ldb_module *module;
377 FIRST_OP(ldb, del_transaction);
379 status = module->ops->del_transaction(module);
380 if (status != LDB_SUCCESS) {
381 if (ldb->err_string == NULL) {
382 /* no error string was setup by the backend */
383 ldb_asprintf_errstring(ldb,
384 "ldb transaction cancel: %s (%d)",
385 ldb_strerror(status),
392 int ldb_transaction_start(struct ldb_context *ldb)
394 /* disable autotransactions */
395 ldb->transaction_active++;
397 return ldb_transaction_start_internal(ldb);
400 int ldb_transaction_commit(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_commit_internal(ldb);
409 int ldb_transaction_cancel(struct ldb_context *ldb)
411 /* renable autotransactions (when we reach 0) */
412 if (ldb->transaction_active > 0)
413 ldb->transaction_active--;
415 return ldb_transaction_cancel_internal(ldb);
418 static int ldb_autotransaction_start(struct ldb_context *ldb)
420 /* explicit transaction active, ignore autotransaction request */
421 if (ldb->transaction_active)
424 return ldb_transaction_start_internal(ldb);
427 static int ldb_autotransaction_commit(struct ldb_context *ldb)
429 /* explicit transaction active, ignore autotransaction request */
430 if (ldb->transaction_active)
433 return ldb_transaction_commit_internal(ldb);
436 static int ldb_autotransaction_cancel(struct ldb_context *ldb)
438 /* explicit transaction active, ignore autotransaction request */
439 if (ldb->transaction_active)
442 return ldb_transaction_cancel_internal(ldb);
445 /* autostarts a transacion if none active */
446 static int ldb_autotransaction_request(struct ldb_context *ldb, struct ldb_request *req)
450 ret = ldb_autotransaction_start(ldb);
451 if (ret != LDB_SUCCESS) {
455 ret = ldb_request(ldb, req);
456 if (ret == LDB_SUCCESS) {
457 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
460 if (ret == LDB_SUCCESS) {
461 return ldb_autotransaction_commit(ldb);
463 ldb_autotransaction_cancel(ldb);
465 if (ldb->err_string == NULL) {
466 /* no error string was setup by the backend */
467 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
473 int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
479 return handle->module->ops->wait(handle, type);
482 /* set the specified timeout or, if timeout is 0 set the default timeout */
483 /* timeout == -1 means no timeout */
484 int ldb_set_timeout(struct ldb_context *ldb, struct ldb_request *req, int timeout)
486 if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
489 req->timeout = timeout;
491 req->timeout = ldb->default_timeout;
493 req->starttime = time(NULL);
498 /* calculates the new timeout based on the previous starttime and timeout */
499 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb, struct ldb_request *oldreq, struct ldb_request *newreq)
503 if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
508 return ldb_set_timeout(ldb, newreq, 0);
510 if ((now - oldreq->starttime) > oldreq->timeout) {
511 return LDB_ERR_TIME_LIMIT_EXCEEDED;
513 newreq->starttime = oldreq->starttime;
514 newreq->timeout = oldreq->timeout - (now - oldreq->starttime);
521 set the permissions for new files to be passed to open() in
522 backends that use local files
524 void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
526 ldb->create_perms = perms;
531 NOTE: the request must be a talloc context.
532 returns LDB_ERR_* on errors.
534 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
536 struct ldb_module *module;
539 ldb_reset_err_string(ldb);
541 /* call the first module in the chain */
542 switch (req->operation) {
544 FIRST_OP(ldb, search);
545 ret = module->ops->search(module, req);
549 ret = module->ops->add(module, req);
552 FIRST_OP(ldb, modify);
553 ret = module->ops->modify(module, req);
557 ret = module->ops->del(module, req);
560 FIRST_OP(ldb, rename);
561 ret = module->ops->rename(module, req);
564 FIRST_OP(ldb, extended);
565 ret = module->ops->extended(module, req);
567 case LDB_SEQUENCE_NUMBER:
568 FIRST_OP(ldb, sequence_number);
569 ret = module->ops->sequence_number(module, req);
572 FIRST_OP(ldb, request);
573 ret = module->ops->request(module, req);
581 search the database given a LDAP-like search expression
583 returns an LDB error code
585 Use talloc_free to free the ldb_message returned in 'res', if successful
588 int ldb_search_default_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
590 struct ldb_result *res;
594 ldb_set_errstring(ldb, "NULL Context in callback");
595 return LDB_ERR_OPERATIONS_ERROR;
598 res = talloc_get_type(context, struct ldb_result);
601 ldb_set_errstring(ldb, "NULL res or ares in callback");
605 switch (ares->type) {
606 case LDB_REPLY_ENTRY:
607 res->msgs = talloc_realloc(res, res->msgs, struct ldb_message *, res->count + 2);
612 res->msgs[res->count + 1] = NULL;
614 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
617 case LDB_REPLY_REFERRAL:
619 for (n = 0; res->refs[n]; n++) /*noop*/ ;
624 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
629 res->refs[n] = talloc_move(res->refs, &ares->referral);
630 res->refs[n + 1] = NULL;
632 case LDB_REPLY_EXTENDED:
634 /* TODO: we should really support controls on entries and referrals too! */
635 res->controls = talloc_move(res, &ares->controls);
643 return LDB_ERR_OPERATIONS_ERROR;
646 int ldb_build_search_req(struct ldb_request **ret_req,
647 struct ldb_context *ldb,
650 enum ldb_scope scope,
651 const char *expression,
652 const char * const *attrs,
653 struct ldb_control **controls,
655 ldb_request_callback_t callback)
657 struct ldb_request *req;
661 req = talloc(mem_ctx, struct ldb_request);
663 ldb_set_errstring(ldb, "Out of Memory");
664 return LDB_ERR_OPERATIONS_ERROR;
667 req->operation = LDB_SEARCH;
669 req->op.search.base = ldb_dn_new(req, ldb, NULL);
671 req->op.search.base = base;
673 req->op.search.scope = scope;
675 req->op.search.tree = ldb_parse_tree(req, expression);
676 if (req->op.search.tree == NULL) {
677 ldb_set_errstring(ldb, "Unable to parse search expression");
679 return LDB_ERR_OPERATIONS_ERROR;
682 req->op.search.attrs = attrs;
683 req->controls = controls;
684 req->context = context;
685 req->callback = callback;
691 int ldb_build_add_req(struct ldb_request **ret_req,
692 struct ldb_context *ldb,
694 const struct ldb_message *message,
695 struct ldb_control **controls,
697 ldb_request_callback_t callback)
699 struct ldb_request *req;
703 req = talloc(mem_ctx, struct ldb_request);
705 ldb_set_errstring(ldb, "Out of Memory");
706 return LDB_ERR_OPERATIONS_ERROR;
709 req->operation = LDB_ADD;
710 req->op.add.message = message;
711 req->controls = controls;
712 req->context = context;
713 req->callback = callback;
720 int ldb_build_mod_req(struct ldb_request **ret_req,
721 struct ldb_context *ldb,
723 const struct ldb_message *message,
724 struct ldb_control **controls,
726 ldb_request_callback_t callback)
728 struct ldb_request *req;
732 req = talloc(mem_ctx, struct ldb_request);
734 ldb_set_errstring(ldb, "Out of Memory");
735 return LDB_ERR_OPERATIONS_ERROR;
738 req->operation = LDB_MODIFY;
739 req->op.mod.message = message;
740 req->controls = controls;
741 req->context = context;
742 req->callback = callback;
749 int ldb_build_del_req(struct ldb_request **ret_req,
750 struct ldb_context *ldb,
753 struct ldb_control **controls,
755 ldb_request_callback_t callback)
757 struct ldb_request *req;
761 req = talloc(mem_ctx, struct ldb_request);
763 ldb_set_errstring(ldb, "Out of Memory");
764 return LDB_ERR_OPERATIONS_ERROR;
767 req->operation = LDB_DELETE;
769 req->controls = controls;
770 req->context = context;
771 req->callback = callback;
778 int ldb_build_rename_req(struct ldb_request **ret_req,
779 struct ldb_context *ldb,
781 struct ldb_dn *olddn,
782 struct ldb_dn *newdn,
783 struct ldb_control **controls,
785 ldb_request_callback_t callback)
787 struct ldb_request *req;
791 req = talloc(mem_ctx, struct ldb_request);
793 ldb_set_errstring(ldb, "Out of Memory");
794 return LDB_ERR_OPERATIONS_ERROR;
797 req->operation = LDB_RENAME;
798 req->op.rename.olddn = olddn;
799 req->op.rename.newdn = newdn;
800 req->controls = controls;
801 req->context = context;
802 req->callback = callback;
809 int ldb_extended_default_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
811 struct ldb_result *res;
814 ldb_set_errstring(ldb, "NULL Context in callback");
815 return LDB_ERR_OPERATIONS_ERROR;
818 res = talloc_get_type(context, struct ldb_result);
820 ldb_set_errstring(ldb, "NULL res or ares in callback");
824 switch (ares->type) {
825 case LDB_REPLY_ENTRY:
826 case LDB_REPLY_REFERRAL:
828 ldb_set_errstring(ldb, "invalid ares type in callback");
830 case LDB_REPLY_EXTENDED:
831 /* TODO: we should really support controls on entries and referrals too! */
832 res->extended = talloc_move(res, &ares->response);
833 res->controls = talloc_move(res, &ares->controls);
841 return LDB_ERR_OPERATIONS_ERROR;
844 int ldb_build_extended_req(struct ldb_request **ret_req,
845 struct ldb_context *ldb,
849 struct ldb_control **controls,
851 ldb_request_callback_t callback)
853 struct ldb_request *req;
857 req = talloc(mem_ctx, struct ldb_request);
859 ldb_set_errstring(ldb, "Out of Memory");
860 return LDB_ERR_OPERATIONS_ERROR;
863 req->operation = LDB_EXTENDED;
864 req->op.extended.oid = oid;
865 req->op.extended.data = data;
866 req->controls = controls;
867 req->context = context;
868 req->callback = callback;
875 int ldb_extended(struct ldb_context *ldb,
878 struct ldb_result **_res)
880 struct ldb_request *req;
882 struct ldb_result *res;
886 res = talloc_zero(ldb, struct ldb_result);
888 return LDB_ERR_OPERATIONS_ERROR;
891 ret = ldb_build_extended_req(&req, ldb, ldb,
893 res, ldb_extended_default_callback);
894 if (ret != LDB_SUCCESS) goto done;
896 ldb_set_timeout(ldb, req, 0); /* use default timeout */
898 ret = ldb_request(ldb, req);
900 if (ret == LDB_SUCCESS) {
901 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
907 if (ret != LDB_SUCCESS) {
916 note that ldb_search() will automatically replace a NULL 'base' value with the
917 defaultNamingContext from the rootDSE if available.
919 int ldb_search(struct ldb_context *ldb,
921 enum ldb_scope scope,
922 const char *expression,
923 const char * const *attrs,
924 struct ldb_result **_res)
926 struct ldb_request *req;
928 struct ldb_result *res;
932 res = talloc_zero(ldb, struct ldb_result);
934 return LDB_ERR_OPERATIONS_ERROR;
937 ret = ldb_build_search_req(&req, ldb, ldb,
938 base?base:ldb_get_default_basedn(ldb),
944 ldb_search_default_callback);
946 if (ret != LDB_SUCCESS) goto done;
948 ldb_set_timeout(ldb, req, 0); /* use default timeout */
950 ret = ldb_request(ldb, req);
952 if (ret == LDB_SUCCESS) {
953 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
959 if (ret != LDB_SUCCESS) {
968 a useful search function where you can easily define the expression and that
969 takes a memory context where results are allocated
972 int ldb_search_exp_fmt(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_result **result,
973 struct ldb_dn *base, enum ldb_scope scope, const char * const *attrs,
974 const char *exp_fmt, ...)
976 struct ldb_result *res;
984 va_start(ap, exp_fmt);
985 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
989 return LDB_ERR_OPERATIONS_ERROR;
992 ret = ldb_search(ldb, base, scope, expression, attrs, &res);
994 if (ret == LDB_SUCCESS) {
995 talloc_steal(mem_ctx, res);
999 talloc_free(expression);
1005 add a record to the database. Will fail if a record with the given class and key
1008 int ldb_add(struct ldb_context *ldb,
1009 const struct ldb_message *message)
1011 struct ldb_request *req;
1014 ret = ldb_msg_sanity_check(ldb, message);
1015 if (ret != LDB_SUCCESS) {
1019 ret = ldb_build_add_req(&req, ldb, ldb,
1025 if (ret != LDB_SUCCESS) return ret;
1027 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1029 /* do request and autostart a transaction */
1030 ret = ldb_autotransaction_request(ldb, req);
1037 modify the specified attributes of a record
1039 int ldb_modify(struct ldb_context *ldb,
1040 const struct ldb_message *message)
1042 struct ldb_request *req;
1045 ret = ldb_msg_sanity_check(ldb, message);
1046 if (ret != LDB_SUCCESS) {
1050 ret = ldb_build_mod_req(&req, ldb, ldb,
1056 if (ret != LDB_SUCCESS) return ret;
1058 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1060 /* do request and autostart a transaction */
1061 ret = ldb_autotransaction_request(ldb, req);
1069 delete a record from the database
1071 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1073 struct ldb_request *req;
1076 ret = ldb_build_del_req(&req, ldb, ldb,
1082 if (ret != LDB_SUCCESS) return ret;
1084 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1086 /* do request and autostart a transaction */
1087 ret = ldb_autotransaction_request(ldb, req);
1094 rename a record in the database
1096 int ldb_rename(struct ldb_context *ldb, struct ldb_dn *olddn, struct ldb_dn *newdn)
1098 struct ldb_request *req;
1101 ret = ldb_build_rename_req(&req, ldb, ldb,
1108 if (ret != LDB_SUCCESS) return ret;
1110 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1112 /* do request and autostart a transaction */
1113 ret = ldb_autotransaction_request(ldb, req);
1121 return the global sequence number
1123 int ldb_sequence_number(struct ldb_context *ldb, enum ldb_sequence_type type, uint64_t *seq_num)
1125 struct ldb_request *req;
1128 req = talloc(ldb, struct ldb_request);
1130 ldb_set_errstring(ldb, "Out of Memory");
1131 return LDB_ERR_OPERATIONS_ERROR;
1134 req->operation = LDB_SEQUENCE_NUMBER;
1135 req->controls = NULL;
1136 req->context = NULL;
1137 req->callback = NULL;
1138 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1140 req->op.seq_num.type = type;
1141 /* do request and autostart a transaction */
1142 ret = ldb_request(ldb, req);
1144 if (ret == LDB_SUCCESS) {
1145 *seq_num = req->op.seq_num.seq_num;
1155 return extended error information
1157 const char *ldb_errstring(struct ldb_context *ldb)
1159 if (ldb->err_string) {
1160 return ldb->err_string;
1167 return a string explaining what a ldb error constant meancs
1169 const char *ldb_strerror(int ldb_err)
1174 case LDB_ERR_OPERATIONS_ERROR:
1175 return "Operations error";
1176 case LDB_ERR_PROTOCOL_ERROR:
1177 return "Protocol error";
1178 case LDB_ERR_TIME_LIMIT_EXCEEDED:
1179 return "Time limit exceeded";
1180 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1181 return "Size limit exceeded";
1182 case LDB_ERR_COMPARE_FALSE:
1183 return "Compare false";
1184 case LDB_ERR_COMPARE_TRUE:
1185 return "Compare true";
1186 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1187 return "Auth method not supported";
1188 case LDB_ERR_STRONG_AUTH_REQUIRED:
1189 return "Strong auth required";
1191 case LDB_ERR_REFERRAL:
1192 return "Referral error";
1193 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1194 return "Admin limit exceeded";
1195 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1196 return "Unsupported critical extension";
1197 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1198 return "Confidentiality required";
1199 case LDB_ERR_SASL_BIND_IN_PROGRESS:
1200 return "SASL bind in progress";
1201 case LDB_ERR_NO_SUCH_ATTRIBUTE:
1202 return "No such attribute";
1203 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1204 return "Undefined attribute type";
1205 case LDB_ERR_INAPPROPRIATE_MATCHING:
1206 return "Inappropriate matching";
1207 case LDB_ERR_CONSTRAINT_VIOLATION:
1208 return "Constraint violation";
1209 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1210 return "Attribute or value exists";
1211 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1212 return "Invalid attribute syntax";
1214 case LDB_ERR_NO_SUCH_OBJECT:
1215 return "No such object";
1216 case LDB_ERR_ALIAS_PROBLEM:
1217 return "Alias problem";
1218 case LDB_ERR_INVALID_DN_SYNTAX:
1219 return "Invalid DN syntax";
1221 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1222 return "Alias dereferencing problem";
1224 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1225 return "Inappropriate authentication";
1226 case LDB_ERR_INVALID_CREDENTIALS:
1227 return "Invalid credentials";
1228 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1229 return "insufficient access rights";
1232 case LDB_ERR_UNAVAILABLE:
1233 return "Unavailable";
1234 case LDB_ERR_UNWILLING_TO_PERFORM:
1235 return "Unwilling to perform";
1236 case LDB_ERR_LOOP_DETECT:
1237 return "Loop detect";
1239 case LDB_ERR_NAMING_VIOLATION:
1240 return "Naming violation";
1241 case LDB_ERR_OBJECT_CLASS_VIOLATION:
1242 return "Object class violation";
1243 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1244 return "Not allowed on non-leaf";
1245 case LDB_ERR_NOT_ALLOWED_ON_RDN:
1246 return "Not allowed on RDN";
1247 case LDB_ERR_ENTRY_ALREADY_EXISTS:
1248 return "Entry already exists";
1249 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1250 return "Object class mods prohibited";
1251 /* 70 RESERVED FOR CLDAP */
1252 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1253 return "Affects multiple DSAs";
1259 return "Unknown error";
1263 set backend specific opaque parameters
1265 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1267 struct ldb_opaque *o;
1269 /* allow updating an existing value */
1270 for (o=ldb->opaque;o;o=o->next) {
1271 if (strcmp(o->name, name) == 0) {
1277 o = talloc(ldb, struct ldb_opaque);
1280 return LDB_ERR_OTHER;
1282 o->next = ldb->opaque;
1290 get a previously set opaque value
1292 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1294 struct ldb_opaque *o;
1295 for (o=ldb->opaque;o;o=o->next) {
1296 if (strcmp(o->name, name) == 0) {