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);
59 static struct backends_list_entry {
60 struct ldb_backend_ops *ops;
61 struct backends_list_entry *prev, *next;
62 } *ldb_backends = NULL;
64 #ifndef STATIC_LIBLDB_BACKENDS
67 #define LDAP_INIT &ldb_ldap_backend_ops, \
68 &ldb_ldapi_backend_ops, \
69 &ldb_ldaps_backend_ops,
74 #ifdef HAVE_LDB_SQLITE3
75 #define SQLITE3_INIT &ldb_sqlite3_backend_ops,
80 #define STATIC_LIBLDB_BACKENDS \
83 &ldb_tdb_backend_ops, \
87 const static struct ldb_backend_ops *builtin_backends[] = {
88 STATIC_LIBLDB_BACKENDS
91 static ldb_connect_fn ldb_find_backend(const char *url)
93 struct backends_list_entry *backend;
96 for (i = 0; builtin_backends[i]; i++) {
97 if (strncmp(builtin_backends[i]->name, url, strlen(builtin_backends[i]->name)) == 0)
98 return builtin_backends[i]->connect_fn;
101 for (backend = ldb_backends; backend; backend = backend->next) {
102 if (strncmp(backend->ops->name, url, strlen(backend->ops->name)) == 0) {
103 return backend->ops->connect_fn;
111 register a new ldb backend
113 int ldb_register_backend(const char *url_prefix, ldb_connect_fn connectfn)
115 struct ldb_backend_ops *backend = talloc(talloc_autofree_context(), struct ldb_backend_ops);
116 struct backends_list_entry *entry = talloc(talloc_autofree_context(), struct backends_list_entry);
118 if (ldb_find_backend(url_prefix)) {
122 /* Maybe check for duplicity here later on? */
124 backend->name = talloc_strdup(backend, url_prefix);
125 backend->connect_fn = connectfn;
126 entry->ops = backend;
127 DLIST_ADD(ldb_backends, entry);
133 Return the ldb module form of a database. The URL can either be one of the following forms
137 flags is made up of LDB_FLG_*
139 the options are passed uninterpreted to the backend, and are
142 This allows modules to get at only the backend module, for example where a module
143 may wish to direct certain requests at a particular backend.
145 int ldb_connect_backend(struct ldb_context *ldb, const char *url, const char *options[],
146 struct ldb_module **backend_module)
152 if (strchr(url, ':') != NULL) {
153 backend = talloc_strndup(ldb, url, strchr(url, ':')-url);
156 backend = talloc_strdup(ldb, "tdb");
159 fn = ldb_find_backend(backend);
162 int (*init_fn) (void);
164 init_fn = ldb_dso_load_symbol(ldb, backend,
166 if (init_fn != NULL && init_fn() == 0) {
167 fn = ldb_find_backend(backend);
172 struct ldb_backend_ops *ops;
173 char *symbol_name = talloc_asprintf(ldb, "ldb_%s_backend_ops", backend);
174 if (symbol_name == NULL) {
175 return LDB_ERR_OPERATIONS_ERROR;
177 ops = ldb_dso_load_symbol(ldb, backend, symbol_name);
179 fn = ops->connect_fn;
181 talloc_free(symbol_name);
184 talloc_free(backend);
187 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to find backend for '%s'\n", url);
188 return LDB_ERR_OTHER;
191 ret = fn(ldb, url, ldb->flags, options, backend_module);
193 if (ret != LDB_SUCCESS) {
194 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to '%s'\n", url);
201 try to autodetect a basedn if none specified. This fixes one of my
202 pet hates about ldapsearch, which is that you have to get a long,
203 complex basedn right to make any use of it.
205 void ldb_set_default_dns(struct ldb_context *ldb)
209 struct ldb_result *res;
210 struct ldb_dn *tmp_dn=NULL;
211 static const char *attrs[] = {
212 "rootDomainNamingContext",
213 "configurationNamingContext",
214 "schemaNamingContext",
215 "defaultNamingContext",
219 tmp_ctx = talloc_new(ldb);
220 ret = ldb_search(ldb, ldb_dn_new(tmp_ctx, ldb, NULL), LDB_SCOPE_BASE,
221 "(objectClass=*)", attrs, &res);
222 if (ret == LDB_SUCCESS) {
223 if (res->count == 1) {
224 if (!ldb_get_opaque(ldb, "rootDomainNamingContext")) {
225 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "rootDomainNamingContext");
226 ldb_set_opaque(ldb, "rootDomainNamingContext", tmp_dn);
229 if (!ldb_get_opaque(ldb, "configurationNamingContext")) {
230 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "configurationNamingContext");
231 ldb_set_opaque(ldb, "configurationNamingContext", tmp_dn);
234 if (!ldb_get_opaque(ldb, "schemaNamingContext")) {
235 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "schemaNamingContext");
236 ldb_set_opaque(ldb, "schemaNamingContext", tmp_dn);
239 if (!ldb_get_opaque(ldb, "defaultNamingContext")) {
240 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "defaultNamingContext");
241 ldb_set_opaque(ldb, "defaultNamingContext", tmp_dn);
247 talloc_free(tmp_ctx);
250 struct ldb_dn *ldb_get_root_basedn(struct ldb_context *ldb)
252 return talloc_get_type(ldb_get_opaque(ldb, "rootDomainNamingContext"), struct ldb_dn);
255 struct ldb_dn *ldb_get_config_basedn(struct ldb_context *ldb)
257 return talloc_get_type(ldb_get_opaque(ldb, "configurationNamingContext"), struct ldb_dn);
260 struct ldb_dn *ldb_get_schema_basedn(struct ldb_context *ldb)
262 return talloc_get_type(ldb_get_opaque(ldb, "schemaNamingContext"), struct ldb_dn);
265 struct ldb_dn *ldb_get_default_basedn(struct ldb_context *ldb)
267 return talloc_get_type(ldb_get_opaque(ldb, "defaultNamingContext"), struct ldb_dn);
271 connect to a database. The URL can either be one of the following forms
275 flags is made up of LDB_FLG_*
277 the options are passed uninterpreted to the backend, and are
280 int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[])
284 /* We seem to need to do this here, or else some utilities don't get ldb backends */
288 url2 = talloc_strdup(ldb, url);
291 return LDB_ERR_OPERATIONS_ERROR;
293 ret = ldb_set_opaque(ldb, "ldb_url", talloc_strdup(ldb, url2));
294 if (ret != LDB_SUCCESS) {
298 ret = ldb_connect_backend(ldb, url, options, &ldb->modules);
299 if (ret != LDB_SUCCESS) {
303 if (ldb_load_modules(ldb, options) != LDB_SUCCESS) {
304 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to load modules for %s: %s\n",
305 url, ldb_errstring(ldb));
306 return LDB_ERR_OTHER;
309 /* TODO: get timeout from options if available there */
310 ldb->default_timeout = 300; /* set default to 5 minutes */
312 /* set the default base dn */
313 ldb_set_default_dns(ldb);
318 void ldb_set_errstring(struct ldb_context *ldb, const char *err_string)
320 if (ldb->err_string) {
321 talloc_free(ldb->err_string);
323 ldb->err_string = talloc_strdup(ldb, err_string);
326 void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
329 char *old_string = NULL;
331 if (ldb->err_string) {
332 old_string = ldb->err_string;
335 va_start(ap, format);
336 ldb->err_string = talloc_vasprintf(ldb, format, ap);
338 talloc_free(old_string);
341 void ldb_reset_err_string(struct ldb_context *ldb)
343 if (ldb->err_string) {
344 talloc_free(ldb->err_string);
345 ldb->err_string = NULL;
349 #define FIRST_OP(ldb, op) do { \
350 module = ldb->modules; \
351 while (module && module->ops->op == NULL) module = module->next; \
352 if (module == NULL) { \
353 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
354 return LDB_ERR_OPERATIONS_ERROR; \
361 static int ldb_transaction_start_internal(struct ldb_context *ldb)
363 struct ldb_module *module;
365 FIRST_OP(ldb, start_transaction);
367 ldb_reset_err_string(ldb);
369 status = module->ops->start_transaction(module);
370 if (status != LDB_SUCCESS) {
371 if (ldb->err_string == NULL) {
372 /* no error string was setup by the backend */
373 ldb_asprintf_errstring(ldb,
374 "ldb transaction start: %s (%d)",
375 ldb_strerror(status),
385 static int ldb_transaction_commit_internal(struct ldb_context *ldb)
387 struct ldb_module *module;
389 FIRST_OP(ldb, end_transaction);
391 ldb_reset_err_string(ldb);
393 status = module->ops->end_transaction(module);
394 if (status != LDB_SUCCESS) {
395 if (ldb->err_string == NULL) {
396 /* no error string was setup by the backend */
397 ldb_asprintf_errstring(ldb,
398 "ldb transaction commit: %s (%d)",
399 ldb_strerror(status),
409 static int ldb_transaction_cancel_internal(struct ldb_context *ldb)
411 struct ldb_module *module;
413 FIRST_OP(ldb, del_transaction);
415 status = module->ops->del_transaction(module);
416 if (status != LDB_SUCCESS) {
417 if (ldb->err_string == NULL) {
418 /* no error string was setup by the backend */
419 ldb_asprintf_errstring(ldb,
420 "ldb transaction cancel: %s (%d)",
421 ldb_strerror(status),
428 int ldb_transaction_start(struct ldb_context *ldb)
430 /* disable autotransactions */
431 ldb->transaction_active++;
433 return ldb_transaction_start_internal(ldb);
436 int ldb_transaction_commit(struct ldb_context *ldb)
438 /* renable autotransactions (when we reach 0) */
439 if (ldb->transaction_active > 0)
440 ldb->transaction_active--;
442 return ldb_transaction_commit_internal(ldb);
445 int ldb_transaction_cancel(struct ldb_context *ldb)
447 /* renable autotransactions (when we reach 0) */
448 if (ldb->transaction_active > 0)
449 ldb->transaction_active--;
451 return ldb_transaction_cancel_internal(ldb);
454 static int ldb_autotransaction_start(struct ldb_context *ldb)
456 /* explicit transaction active, ignore autotransaction request */
457 if (ldb->transaction_active)
460 return ldb_transaction_start_internal(ldb);
463 static int ldb_autotransaction_commit(struct ldb_context *ldb)
465 /* explicit transaction active, ignore autotransaction request */
466 if (ldb->transaction_active)
469 return ldb_transaction_commit_internal(ldb);
472 static int ldb_autotransaction_cancel(struct ldb_context *ldb)
474 /* explicit transaction active, ignore autotransaction request */
475 if (ldb->transaction_active)
478 return ldb_transaction_cancel_internal(ldb);
481 /* autostarts a transacion if none active */
482 static int ldb_autotransaction_request(struct ldb_context *ldb, struct ldb_request *req)
486 ret = ldb_autotransaction_start(ldb);
487 if (ret != LDB_SUCCESS) {
491 ret = ldb_request(ldb, req);
492 if (ret == LDB_SUCCESS) {
493 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
496 if (ret == LDB_SUCCESS) {
497 return ldb_autotransaction_commit(ldb);
499 ldb_autotransaction_cancel(ldb);
501 if (ldb->err_string == NULL) {
502 /* no error string was setup by the backend */
503 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
509 int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
516 ret = handle->module->ops->wait(handle, type);
517 if (!ldb_errstring(handle->module->ldb)) {
518 /* Set a default error string, to place the blame somewhere */
519 ldb_asprintf_errstring(handle->module->ldb, "error waiting on module %s: %s (%d)", handle->module->ops->name, ldb_strerror(ret), ret);
524 /* set the specified timeout or, if timeout is 0 set the default timeout */
525 /* timeout == -1 means no timeout */
526 int ldb_set_timeout(struct ldb_context *ldb, struct ldb_request *req, int timeout)
528 if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
531 req->timeout = timeout;
533 req->timeout = ldb->default_timeout;
535 req->starttime = time(NULL);
540 /* calculates the new timeout based on the previous starttime and timeout */
541 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb, struct ldb_request *oldreq, struct ldb_request *newreq)
545 if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
550 return ldb_set_timeout(ldb, newreq, 0);
552 if ((now - oldreq->starttime) > oldreq->timeout) {
553 return LDB_ERR_TIME_LIMIT_EXCEEDED;
555 newreq->starttime = oldreq->starttime;
556 newreq->timeout = oldreq->timeout - (now - oldreq->starttime);
563 set the permissions for new files to be passed to open() in
564 backends that use local files
566 void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
568 ldb->create_perms = perms;
573 NOTE: the request must be a talloc context.
574 returns LDB_ERR_* on errors.
576 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
578 struct ldb_module *module;
581 ldb_reset_err_string(ldb);
583 /* call the first module in the chain */
584 switch (req->operation) {
586 FIRST_OP(ldb, search);
587 ret = module->ops->search(module, req);
591 ret = module->ops->add(module, req);
594 FIRST_OP(ldb, modify);
595 ret = module->ops->modify(module, req);
599 ret = module->ops->del(module, req);
602 FIRST_OP(ldb, rename);
603 ret = module->ops->rename(module, req);
606 FIRST_OP(ldb, extended);
607 ret = module->ops->extended(module, req);
609 case LDB_SEQUENCE_NUMBER:
610 FIRST_OP(ldb, sequence_number);
611 ret = module->ops->sequence_number(module, req);
614 FIRST_OP(ldb, request);
615 ret = module->ops->request(module, req);
623 search the database given a LDAP-like search expression
625 returns an LDB error code
627 Use talloc_free to free the ldb_message returned in 'res', if successful
630 int ldb_search_default_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
632 struct ldb_result *res;
636 ldb_set_errstring(ldb, "NULL Context in callback");
637 return LDB_ERR_OPERATIONS_ERROR;
640 res = talloc_get_type(context, struct ldb_result);
643 ldb_set_errstring(ldb, "NULL res or ares in callback");
647 switch (ares->type) {
648 case LDB_REPLY_ENTRY:
649 res->msgs = talloc_realloc(res, res->msgs, struct ldb_message *, res->count + 2);
654 res->msgs[res->count + 1] = NULL;
656 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
659 case LDB_REPLY_REFERRAL:
661 for (n = 0; res->refs[n]; n++) /*noop*/ ;
666 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
671 res->refs[n] = talloc_move(res->refs, &ares->referral);
672 res->refs[n + 1] = NULL;
674 case LDB_REPLY_EXTENDED:
676 /* TODO: we should really support controls on entries and referrals too! */
677 res->controls = talloc_move(res, &ares->controls);
685 return LDB_ERR_OPERATIONS_ERROR;
688 int ldb_build_search_req(struct ldb_request **ret_req,
689 struct ldb_context *ldb,
692 enum ldb_scope scope,
693 const char *expression,
694 const char * const *attrs,
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_SEARCH;
711 req->op.search.base = ldb_dn_new(req, ldb, NULL);
713 req->op.search.base = base;
715 req->op.search.scope = scope;
717 req->op.search.tree = ldb_parse_tree(req, expression);
718 if (req->op.search.tree == NULL) {
719 ldb_set_errstring(ldb, "Unable to parse search expression");
721 return LDB_ERR_OPERATIONS_ERROR;
724 req->op.search.attrs = attrs;
725 req->controls = controls;
726 req->context = context;
727 req->callback = callback;
733 int ldb_build_add_req(struct ldb_request **ret_req,
734 struct ldb_context *ldb,
736 const struct ldb_message *message,
737 struct ldb_control **controls,
739 ldb_request_callback_t callback)
741 struct ldb_request *req;
745 req = talloc(mem_ctx, struct ldb_request);
747 ldb_set_errstring(ldb, "Out of Memory");
748 return LDB_ERR_OPERATIONS_ERROR;
751 req->operation = LDB_ADD;
752 req->op.add.message = message;
753 req->controls = controls;
754 req->context = context;
755 req->callback = callback;
762 int ldb_build_mod_req(struct ldb_request **ret_req,
763 struct ldb_context *ldb,
765 const struct ldb_message *message,
766 struct ldb_control **controls,
768 ldb_request_callback_t callback)
770 struct ldb_request *req;
774 req = talloc(mem_ctx, struct ldb_request);
776 ldb_set_errstring(ldb, "Out of Memory");
777 return LDB_ERR_OPERATIONS_ERROR;
780 req->operation = LDB_MODIFY;
781 req->op.mod.message = message;
782 req->controls = controls;
783 req->context = context;
784 req->callback = callback;
791 int ldb_build_del_req(struct ldb_request **ret_req,
792 struct ldb_context *ldb,
795 struct ldb_control **controls,
797 ldb_request_callback_t callback)
799 struct ldb_request *req;
803 req = talloc(mem_ctx, struct ldb_request);
805 ldb_set_errstring(ldb, "Out of Memory");
806 return LDB_ERR_OPERATIONS_ERROR;
809 req->operation = LDB_DELETE;
811 req->controls = controls;
812 req->context = context;
813 req->callback = callback;
820 int ldb_build_rename_req(struct ldb_request **ret_req,
821 struct ldb_context *ldb,
823 struct ldb_dn *olddn,
824 struct ldb_dn *newdn,
825 struct ldb_control **controls,
827 ldb_request_callback_t callback)
829 struct ldb_request *req;
833 req = talloc(mem_ctx, struct ldb_request);
835 ldb_set_errstring(ldb, "Out of Memory");
836 return LDB_ERR_OPERATIONS_ERROR;
839 req->operation = LDB_RENAME;
840 req->op.rename.olddn = olddn;
841 req->op.rename.newdn = newdn;
842 req->controls = controls;
843 req->context = context;
844 req->callback = callback;
851 int ldb_extended_default_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
853 struct ldb_result *res;
856 ldb_set_errstring(ldb, "NULL Context in callback");
857 return LDB_ERR_OPERATIONS_ERROR;
860 res = talloc_get_type(context, struct ldb_result);
862 ldb_set_errstring(ldb, "NULL res or ares in callback");
866 switch (ares->type) {
867 case LDB_REPLY_ENTRY:
868 case LDB_REPLY_REFERRAL:
870 ldb_set_errstring(ldb, "invalid ares type in callback");
872 case LDB_REPLY_EXTENDED:
873 /* TODO: we should really support controls on entries and referrals too! */
874 res->extended = talloc_move(res, &ares->response);
875 res->controls = talloc_move(res, &ares->controls);
883 return LDB_ERR_OPERATIONS_ERROR;
886 int ldb_build_extended_req(struct ldb_request **ret_req,
887 struct ldb_context *ldb,
891 struct ldb_control **controls,
893 ldb_request_callback_t callback)
895 struct ldb_request *req;
899 req = talloc(mem_ctx, struct ldb_request);
901 ldb_set_errstring(ldb, "Out of Memory");
902 return LDB_ERR_OPERATIONS_ERROR;
905 req->operation = LDB_EXTENDED;
906 req->op.extended.oid = oid;
907 req->op.extended.data = data;
908 req->controls = controls;
909 req->context = context;
910 req->callback = callback;
917 int ldb_extended(struct ldb_context *ldb,
920 struct ldb_result **_res)
922 struct ldb_request *req;
924 struct ldb_result *res;
928 res = talloc_zero(ldb, struct ldb_result);
930 return LDB_ERR_OPERATIONS_ERROR;
933 ret = ldb_build_extended_req(&req, ldb, ldb,
935 res, ldb_extended_default_callback);
936 if (ret != LDB_SUCCESS) goto done;
938 ldb_set_timeout(ldb, req, 0); /* use default timeout */
940 ret = ldb_request(ldb, req);
942 if (ret == LDB_SUCCESS) {
943 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
949 if (ret != LDB_SUCCESS) {
958 note that ldb_search() will automatically replace a NULL 'base' value with the
959 defaultNamingContext from the rootDSE if available.
961 int ldb_search(struct ldb_context *ldb,
963 enum ldb_scope scope,
964 const char *expression,
965 const char * const *attrs,
966 struct ldb_result **_res)
968 struct ldb_request *req;
970 struct ldb_result *res;
974 res = talloc_zero(ldb, struct ldb_result);
976 return LDB_ERR_OPERATIONS_ERROR;
979 ret = ldb_build_search_req(&req, ldb, ldb,
980 base?base:ldb_get_default_basedn(ldb),
986 ldb_search_default_callback);
988 if (ret != LDB_SUCCESS) goto done;
990 ldb_set_timeout(ldb, req, 0); /* use default timeout */
992 ret = ldb_request(ldb, req);
994 if (ret == LDB_SUCCESS) {
995 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1001 if (ret != LDB_SUCCESS) {
1010 a useful search function where you can easily define the expression and that
1011 takes a memory context where results are allocated
1014 int ldb_search_exp_fmt(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_result **result,
1015 struct ldb_dn *base, enum ldb_scope scope, const char * const *attrs,
1016 const char *exp_fmt, ...)
1018 struct ldb_result *res;
1026 va_start(ap, exp_fmt);
1027 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
1030 if ( ! expression) {
1031 return LDB_ERR_OPERATIONS_ERROR;
1034 ret = ldb_search(ldb, base, scope, expression, attrs, &res);
1036 if (ret == LDB_SUCCESS) {
1037 talloc_steal(mem_ctx, res);
1041 talloc_free(expression);
1047 add a record to the database. Will fail if a record with the given class and key
1050 int ldb_add(struct ldb_context *ldb,
1051 const struct ldb_message *message)
1053 struct ldb_request *req;
1056 ret = ldb_msg_sanity_check(ldb, message);
1057 if (ret != LDB_SUCCESS) {
1061 ret = ldb_build_add_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 modify the specified attributes of a record
1081 int ldb_modify(struct ldb_context *ldb,
1082 const struct ldb_message *message)
1084 struct ldb_request *req;
1087 ret = ldb_msg_sanity_check(ldb, message);
1088 if (ret != LDB_SUCCESS) {
1092 ret = ldb_build_mod_req(&req, ldb, ldb,
1098 if (ret != LDB_SUCCESS) return ret;
1100 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1102 /* do request and autostart a transaction */
1103 ret = ldb_autotransaction_request(ldb, req);
1111 delete a record from the database
1113 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1115 struct ldb_request *req;
1118 ret = ldb_build_del_req(&req, ldb, ldb,
1124 if (ret != LDB_SUCCESS) return ret;
1126 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1128 /* do request and autostart a transaction */
1129 ret = ldb_autotransaction_request(ldb, req);
1136 rename a record in the database
1138 int ldb_rename(struct ldb_context *ldb, struct ldb_dn *olddn, struct ldb_dn *newdn)
1140 struct ldb_request *req;
1143 ret = ldb_build_rename_req(&req, ldb, ldb,
1150 if (ret != LDB_SUCCESS) return ret;
1152 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1154 /* do request and autostart a transaction */
1155 ret = ldb_autotransaction_request(ldb, req);
1163 return the global sequence number
1165 int ldb_sequence_number(struct ldb_context *ldb, enum ldb_sequence_type type, uint64_t *seq_num)
1167 struct ldb_request *req;
1170 req = talloc(ldb, struct ldb_request);
1172 ldb_set_errstring(ldb, "Out of Memory");
1173 return LDB_ERR_OPERATIONS_ERROR;
1176 req->operation = LDB_SEQUENCE_NUMBER;
1177 req->controls = NULL;
1178 req->context = NULL;
1179 req->callback = NULL;
1180 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1182 req->op.seq_num.type = type;
1183 /* do request and autostart a transaction */
1184 ret = ldb_request(ldb, req);
1186 if (ret == LDB_SUCCESS) {
1187 *seq_num = req->op.seq_num.seq_num;
1197 return extended error information
1199 const char *ldb_errstring(struct ldb_context *ldb)
1201 if (ldb->err_string) {
1202 return ldb->err_string;
1209 return a string explaining what a ldb error constant meancs
1211 const char *ldb_strerror(int ldb_err)
1216 case LDB_ERR_OPERATIONS_ERROR:
1217 return "Operations error";
1218 case LDB_ERR_PROTOCOL_ERROR:
1219 return "Protocol error";
1220 case LDB_ERR_TIME_LIMIT_EXCEEDED:
1221 return "Time limit exceeded";
1222 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1223 return "Size limit exceeded";
1224 case LDB_ERR_COMPARE_FALSE:
1225 return "Compare false";
1226 case LDB_ERR_COMPARE_TRUE:
1227 return "Compare true";
1228 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1229 return "Auth method not supported";
1230 case LDB_ERR_STRONG_AUTH_REQUIRED:
1231 return "Strong auth required";
1233 case LDB_ERR_REFERRAL:
1234 return "Referral error";
1235 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1236 return "Admin limit exceeded";
1237 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1238 return "Unsupported critical extension";
1239 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1240 return "Confidentiality required";
1241 case LDB_ERR_SASL_BIND_IN_PROGRESS:
1242 return "SASL bind in progress";
1243 case LDB_ERR_NO_SUCH_ATTRIBUTE:
1244 return "No such attribute";
1245 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1246 return "Undefined attribute type";
1247 case LDB_ERR_INAPPROPRIATE_MATCHING:
1248 return "Inappropriate matching";
1249 case LDB_ERR_CONSTRAINT_VIOLATION:
1250 return "Constraint violation";
1251 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1252 return "Attribute or value exists";
1253 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1254 return "Invalid attribute syntax";
1256 case LDB_ERR_NO_SUCH_OBJECT:
1257 return "No such object";
1258 case LDB_ERR_ALIAS_PROBLEM:
1259 return "Alias problem";
1260 case LDB_ERR_INVALID_DN_SYNTAX:
1261 return "Invalid DN syntax";
1263 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1264 return "Alias dereferencing problem";
1266 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1267 return "Inappropriate authentication";
1268 case LDB_ERR_INVALID_CREDENTIALS:
1269 return "Invalid credentials";
1270 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1271 return "insufficient access rights";
1274 case LDB_ERR_UNAVAILABLE:
1275 return "Unavailable";
1276 case LDB_ERR_UNWILLING_TO_PERFORM:
1277 return "Unwilling to perform";
1278 case LDB_ERR_LOOP_DETECT:
1279 return "Loop detect";
1281 case LDB_ERR_NAMING_VIOLATION:
1282 return "Naming violation";
1283 case LDB_ERR_OBJECT_CLASS_VIOLATION:
1284 return "Object class violation";
1285 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1286 return "Not allowed on non-leaf";
1287 case LDB_ERR_NOT_ALLOWED_ON_RDN:
1288 return "Not allowed on RDN";
1289 case LDB_ERR_ENTRY_ALREADY_EXISTS:
1290 return "Entry already exists";
1291 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1292 return "Object class mods prohibited";
1293 /* 70 RESERVED FOR CLDAP */
1294 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1295 return "Affects multiple DSAs";
1301 return "Unknown error";
1305 set backend specific opaque parameters
1307 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1309 struct ldb_opaque *o;
1311 /* allow updating an existing value */
1312 for (o=ldb->opaque;o;o=o->next) {
1313 if (strcmp(o->name, name) == 0) {
1319 o = talloc(ldb, struct ldb_opaque);
1322 return LDB_ERR_OTHER;
1324 o->next = ldb->opaque;
1332 get a previously set opaque value
1334 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1336 struct ldb_opaque *o;
1337 for (o=ldb->opaque;o;o=o->next) {
1338 if (strcmp(o->name, name) == 0) {