4 Copyright (C) Andrew Tridgell 2004
5 Copyright (C) Simo Sorce 2005-2008
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 required
40 The event_ctx is required
42 struct ldb_context *ldb_init(TALLOC_CTX *mem_ctx, struct event_context *ev_ctx)
44 struct ldb_context *ldb;
47 ldb = talloc_zero(mem_ctx, struct ldb_context);
48 /* FIXME: Hack a new event context so that CMD line utilities work
49 * until we have them all converted */
51 ev_ctx = event_context_init(talloc_autofree_context());
54 ret = ldb_setup_wellknown_attributes(ldb);
60 ldb_set_utf8_default(ldb);
61 ldb_set_create_perms(ldb, 0666);
62 ldb_set_modules_dir(ldb, LDB_MODULESDIR);
63 ldb_set_event_context(ldb, ev_ctx);
65 /* TODO: get timeout from options if available there */
66 ldb->default_timeout = 300; /* set default to 5 minutes */
71 static struct backends_list_entry {
72 struct ldb_backend_ops *ops;
73 struct backends_list_entry *prev, *next;
74 } *ldb_backends = NULL;
76 #ifndef STATIC_LIBLDB_BACKENDS
79 #define LDAP_INIT &ldb_ldap_backend_ops, \
80 &ldb_ldapi_backend_ops, \
81 &ldb_ldaps_backend_ops,
86 #ifdef HAVE_LDB_SQLITE3
87 #define SQLITE3_INIT &ldb_sqlite3_backend_ops,
92 #define STATIC_LIBLDB_BACKENDS \
95 &ldb_tdb_backend_ops, \
99 const static struct ldb_backend_ops *builtin_backends[] = {
100 STATIC_LIBLDB_BACKENDS
103 static ldb_connect_fn ldb_find_backend(const char *url)
105 struct backends_list_entry *backend;
108 for (i = 0; builtin_backends[i]; i++) {
109 if (strncmp(builtin_backends[i]->name, url,
110 strlen(builtin_backends[i]->name)) == 0)
111 return builtin_backends[i]->connect_fn;
114 for (backend = ldb_backends; backend; backend = backend->next) {
115 if (strncmp(backend->ops->name, url,
116 strlen(backend->ops->name)) == 0) {
117 return backend->ops->connect_fn;
125 register a new ldb backend
127 int ldb_register_backend(const char *url_prefix, ldb_connect_fn connectfn)
129 struct ldb_backend_ops *backend;
130 struct backends_list_entry *entry;
132 backend = talloc(talloc_autofree_context(), struct ldb_backend_ops);
133 if (!backend) return LDB_ERR_OPERATIONS_ERROR;
135 entry = talloc(talloc_autofree_context(), struct backends_list_entry);
137 talloc_free(backend);
138 return LDB_ERR_OPERATIONS_ERROR;
141 if (ldb_find_backend(url_prefix)) {
145 /* Maybe check for duplicity here later on? */
147 backend->name = talloc_strdup(backend, url_prefix);
148 backend->connect_fn = connectfn;
149 entry->ops = backend;
150 DLIST_ADD(ldb_backends, entry);
156 Return the ldb module form of a database.
157 The URL can either be one of the following forms
161 flags is made up of LDB_FLG_*
163 the options are passed uninterpreted to the backend, and are
166 This allows modules to get at only the backend module, for example where a
167 module may wish to direct certain requests at a particular backend.
169 int ldb_connect_backend(struct ldb_context *ldb,
171 const char *options[],
172 struct ldb_module **backend_module)
178 if (strchr(url, ':') != NULL) {
179 backend = talloc_strndup(ldb, url, strchr(url, ':')-url);
182 backend = talloc_strdup(ldb, "tdb");
185 fn = ldb_find_backend(backend);
188 struct ldb_backend_ops *ops;
189 char *symbol_name = talloc_asprintf(ldb, "ldb_%s_backend_ops", backend);
190 if (symbol_name == NULL) {
191 return LDB_ERR_OPERATIONS_ERROR;
193 ops = ldb_dso_load_symbol(ldb, backend, symbol_name);
195 fn = ops->connect_fn;
197 talloc_free(symbol_name);
200 talloc_free(backend);
203 ldb_debug(ldb, LDB_DEBUG_FATAL,
204 "Unable to find backend for '%s'\n", url);
205 return LDB_ERR_OTHER;
208 ret = fn(ldb, url, ldb->flags, options, backend_module);
210 if (ret != LDB_SUCCESS) {
211 ldb_debug(ldb, LDB_DEBUG_ERROR,
212 "Failed to connect to '%s'\n", url);
219 try to autodetect a basedn if none specified. This fixes one of my
220 pet hates about ldapsearch, which is that you have to get a long,
221 complex basedn right to make any use of it.
223 void ldb_set_default_dns(struct ldb_context *ldb)
227 struct ldb_result *res;
228 struct ldb_dn *tmp_dn=NULL;
229 static const char *attrs[] = {
230 "rootDomainNamingContext",
231 "configurationNamingContext",
232 "schemaNamingContext",
233 "defaultNamingContext",
237 tmp_ctx = talloc_new(ldb);
238 ret = ldb_search(ldb, ldb_dn_new(tmp_ctx, ldb, NULL), LDB_SCOPE_BASE,
239 "(objectClass=*)", attrs, &res);
240 if (ret != LDB_SUCCESS) {
241 talloc_free(tmp_ctx);
245 if (res->count != 1) {
250 if (!ldb_get_opaque(ldb, "rootDomainNamingContext")) {
251 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
252 "rootDomainNamingContext");
253 ldb_set_opaque(ldb, "rootDomainNamingContext", tmp_dn);
256 if (!ldb_get_opaque(ldb, "configurationNamingContext")) {
257 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
258 "configurationNamingContext");
259 ldb_set_opaque(ldb, "configurationNamingContext", tmp_dn);
262 if (!ldb_get_opaque(ldb, "schemaNamingContext")) {
263 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
264 "schemaNamingContext");
265 ldb_set_opaque(ldb, "schemaNamingContext", tmp_dn);
268 if (!ldb_get_opaque(ldb, "defaultNamingContext")) {
269 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
270 "defaultNamingContext");
271 ldb_set_opaque(ldb, "defaultNamingContext", tmp_dn);
275 talloc_free(tmp_ctx);
278 struct ldb_dn *ldb_get_root_basedn(struct ldb_context *ldb)
280 void *opaque = ldb_get_opaque(ldb, "rootDomainNamingContext");
281 return talloc_get_type(opaque, struct ldb_dn);
284 struct ldb_dn *ldb_get_config_basedn(struct ldb_context *ldb)
286 void *opaque = ldb_get_opaque(ldb, "configurationNamingContext");
287 return talloc_get_type(opaque, struct ldb_dn);
290 struct ldb_dn *ldb_get_schema_basedn(struct ldb_context *ldb)
292 void *opaque = ldb_get_opaque(ldb, "schemaNamingContext");
293 return talloc_get_type(opaque, struct ldb_dn);
296 struct ldb_dn *ldb_get_default_basedn(struct ldb_context *ldb)
298 void *opaque = ldb_get_opaque(ldb, "defaultNamingContext");
299 return talloc_get_type(opaque, struct ldb_dn);
303 connect to a database. The URL can either be one of the following forms
307 flags is made up of LDB_FLG_*
309 the options are passed uninterpreted to the backend, and are
312 int ldb_connect(struct ldb_context *ldb, const char *url,
313 unsigned int flags, const char *options[])
317 /* We seem to need to do this here, or else some utilities don't
318 * get ldb backends */
322 url2 = talloc_strdup(ldb, url);
325 return LDB_ERR_OPERATIONS_ERROR;
327 ret = ldb_set_opaque(ldb, "ldb_url", talloc_strdup(ldb, url2));
328 if (ret != LDB_SUCCESS) {
332 ret = ldb_connect_backend(ldb, url, options, &ldb->modules);
333 if (ret != LDB_SUCCESS) {
337 if (ldb_load_modules(ldb, options) != LDB_SUCCESS) {
338 ldb_debug(ldb, LDB_DEBUG_FATAL,
339 "Unable to load modules for %s: %s\n",
340 url, ldb_errstring(ldb));
341 return LDB_ERR_OTHER;
344 /* TODO: get timeout from options if available there */
345 ldb->default_timeout = 300; /* set default to 5 minutes */
347 /* set the default base dn */
348 ldb_set_default_dns(ldb);
353 void ldb_set_errstring(struct ldb_context *ldb, const char *err_string)
355 if (ldb->err_string) {
356 talloc_free(ldb->err_string);
358 ldb->err_string = talloc_strdup(ldb, err_string);
361 void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
364 char *old_string = NULL;
366 if (ldb->err_string) {
367 old_string = ldb->err_string;
370 va_start(ap, format);
371 ldb->err_string = talloc_vasprintf(ldb, format, ap);
373 talloc_free(old_string);
376 void ldb_reset_err_string(struct ldb_context *ldb)
378 if (ldb->err_string) {
379 talloc_free(ldb->err_string);
380 ldb->err_string = NULL;
384 #define FIRST_OP(ldb, op) do { \
385 module = ldb->modules; \
386 while (module && module->ops->op == NULL) module = module->next; \
387 if (module == NULL) { \
388 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
389 return LDB_ERR_OPERATIONS_ERROR; \
396 static int ldb_transaction_start_internal(struct ldb_context *ldb)
398 struct ldb_module *module;
400 FIRST_OP(ldb, start_transaction);
402 ldb_reset_err_string(ldb);
404 status = module->ops->start_transaction(module);
405 if (status != LDB_SUCCESS) {
406 if (ldb->err_string == NULL) {
407 /* no error string was setup by the backend */
408 ldb_asprintf_errstring(ldb,
409 "ldb transaction start: %s (%d)",
410 ldb_strerror(status),
420 static int ldb_transaction_commit_internal(struct ldb_context *ldb)
422 struct ldb_module *module;
424 FIRST_OP(ldb, end_transaction);
426 ldb_reset_err_string(ldb);
428 status = module->ops->end_transaction(module);
429 if (status != LDB_SUCCESS) {
430 if (ldb->err_string == NULL) {
431 /* no error string was setup by the backend */
432 ldb_asprintf_errstring(ldb,
433 "ldb transaction commit: %s (%d)",
434 ldb_strerror(status),
444 static int ldb_transaction_cancel_internal(struct ldb_context *ldb)
446 struct ldb_module *module;
448 FIRST_OP(ldb, del_transaction);
450 status = module->ops->del_transaction(module);
451 if (status != LDB_SUCCESS) {
452 if (ldb->err_string == NULL) {
453 /* no error string was setup by the backend */
454 ldb_asprintf_errstring(ldb,
455 "ldb transaction cancel: %s (%d)",
456 ldb_strerror(status),
463 int ldb_transaction_start(struct ldb_context *ldb)
465 /* disable autotransactions */
466 ldb->transaction_active++;
468 return ldb_transaction_start_internal(ldb);
471 int ldb_transaction_commit(struct ldb_context *ldb)
473 /* renable autotransactions (when we reach 0) */
474 if (ldb->transaction_active > 0)
475 ldb->transaction_active--;
477 return ldb_transaction_commit_internal(ldb);
480 int ldb_transaction_cancel(struct ldb_context *ldb)
482 /* renable autotransactions (when we reach 0) */
483 if (ldb->transaction_active > 0)
484 ldb->transaction_active--;
486 return ldb_transaction_cancel_internal(ldb);
489 static int ldb_autotransaction_start(struct ldb_context *ldb)
491 /* explicit transaction active, ignore autotransaction request */
492 if (ldb->transaction_active)
495 return ldb_transaction_start_internal(ldb);
498 static int ldb_autotransaction_commit(struct ldb_context *ldb)
500 /* explicit transaction active, ignore autotransaction request */
501 if (ldb->transaction_active)
504 return ldb_transaction_commit_internal(ldb);
507 static int ldb_autotransaction_cancel(struct ldb_context *ldb)
509 /* explicit transaction active, ignore autotransaction request */
510 if (ldb->transaction_active)
513 return ldb_transaction_cancel_internal(ldb);
516 /* autostarts a transacion if none active */
517 static int ldb_autotransaction_request(struct ldb_context *ldb,
518 struct ldb_request *req)
522 ret = ldb_autotransaction_start(ldb);
523 if (ret != LDB_SUCCESS) {
527 ret = ldb_request(ldb, req);
528 if (ret == LDB_SUCCESS) {
529 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
532 if (ret == LDB_SUCCESS) {
533 return ldb_autotransaction_commit(ldb);
535 ldb_autotransaction_cancel(ldb);
537 if (ldb->err_string == NULL) {
538 /* no error string was setup by the backend */
539 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
545 int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
552 ret = handle->module->ops->wait(handle, type);
553 if (!ldb_errstring(handle->module->ldb)) {
554 /* Set a default error string, to place the blame somewhere */
555 ldb_asprintf_errstring(handle->module->ldb,
556 "error waiting on module %s: %s (%d)",
557 handle->module->ops->name,
558 ldb_strerror(ret), ret);
563 /* set the specified timeout or, if timeout is 0 set the default timeout */
564 /* timeout == -1 means no timeout */
565 int ldb_set_timeout(struct ldb_context *ldb,
566 struct ldb_request *req,
569 if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
572 req->timeout = timeout;
574 req->timeout = ldb->default_timeout;
576 req->starttime = time(NULL);
581 /* calculates the new timeout based on the previous starttime and timeout */
582 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb,
583 struct ldb_request *oldreq,
584 struct ldb_request *newreq)
588 if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
593 return ldb_set_timeout(ldb, newreq, 0);
595 if ((now - oldreq->starttime) > oldreq->timeout) {
596 return LDB_ERR_TIME_LIMIT_EXCEEDED;
598 newreq->starttime = oldreq->starttime;
599 newreq->timeout = oldreq->timeout - (now - oldreq->starttime);
606 set the permissions for new files to be passed to open() in
607 backends that use local files
609 void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
611 ldb->create_perms = perms;
614 void ldb_set_event_context(struct ldb_context *ldb, struct event_context *ev)
619 struct event_context * ldb_get_event_context(struct ldb_context *ldb)
626 NOTE: the request must be a talloc context.
627 returns LDB_ERR_* on errors.
629 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
631 struct ldb_module *module;
634 ldb_reset_err_string(ldb);
636 /* call the first module in the chain */
637 switch (req->operation) {
639 FIRST_OP(ldb, search);
640 ret = module->ops->search(module, req);
644 ret = module->ops->add(module, req);
647 FIRST_OP(ldb, modify);
648 ret = module->ops->modify(module, req);
652 ret = module->ops->del(module, req);
655 FIRST_OP(ldb, rename);
656 ret = module->ops->rename(module, req);
659 FIRST_OP(ldb, extended);
660 ret = module->ops->extended(module, req);
662 case LDB_SEQUENCE_NUMBER:
663 FIRST_OP(ldb, sequence_number);
664 ret = module->ops->sequence_number(module, req);
667 FIRST_OP(ldb, request);
668 ret = module->ops->request(module, req);
676 search the database given a LDAP-like search expression
678 returns an LDB error code
680 Use talloc_free to free the ldb_message returned in 'res', if successful
683 int ldb_search_default_callback(struct ldb_context *ldb,
685 struct ldb_reply *ares)
687 struct ldb_result *res;
691 ldb_set_errstring(ldb, "NULL Context in callback");
692 return LDB_ERR_OPERATIONS_ERROR;
695 res = talloc_get_type(context, struct ldb_result);
698 ldb_set_errstring(ldb, "NULL res or ares in callback");
702 switch (ares->type) {
703 case LDB_REPLY_ENTRY:
704 res->msgs = talloc_realloc(res, res->msgs,
705 struct ldb_message *,
711 res->msgs[res->count + 1] = NULL;
713 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
716 case LDB_REPLY_REFERRAL:
718 for (n = 0; res->refs[n]; n++) /*noop*/ ;
723 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
728 res->refs[n] = talloc_move(res->refs, &ares->referral);
729 res->refs[n + 1] = NULL;
731 case LDB_REPLY_EXTENDED:
733 /* TODO: we should really support controls on entries
734 * and referrals too! */
735 res->controls = talloc_move(res, &ares->controls);
743 return LDB_ERR_OPERATIONS_ERROR;
746 int ldb_build_search_req(struct ldb_request **ret_req,
747 struct ldb_context *ldb,
750 enum ldb_scope scope,
751 const char *expression,
752 const char * const *attrs,
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_SEARCH;
769 req->op.search.base = ldb_dn_new(req, ldb, NULL);
771 req->op.search.base = base;
773 req->op.search.scope = scope;
775 req->op.search.tree = ldb_parse_tree(req, expression);
776 if (req->op.search.tree == NULL) {
777 ldb_set_errstring(ldb, "Unable to parse search expression");
779 return LDB_ERR_OPERATIONS_ERROR;
782 req->op.search.attrs = attrs;
783 req->controls = controls;
784 req->context = context;
785 req->callback = callback;
791 int ldb_build_add_req(struct ldb_request **ret_req,
792 struct ldb_context *ldb,
794 const struct ldb_message *message,
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_ADD;
810 req->op.add.message = message;
811 req->controls = controls;
812 req->context = context;
813 req->callback = callback;
820 int ldb_build_mod_req(struct ldb_request **ret_req,
821 struct ldb_context *ldb,
823 const struct ldb_message *message,
824 struct ldb_control **controls,
826 ldb_request_callback_t callback)
828 struct ldb_request *req;
832 req = talloc(mem_ctx, struct ldb_request);
834 ldb_set_errstring(ldb, "Out of Memory");
835 return LDB_ERR_OPERATIONS_ERROR;
838 req->operation = LDB_MODIFY;
839 req->op.mod.message = message;
840 req->controls = controls;
841 req->context = context;
842 req->callback = callback;
849 int ldb_build_del_req(struct ldb_request **ret_req,
850 struct ldb_context *ldb,
853 struct ldb_control **controls,
855 ldb_request_callback_t callback)
857 struct ldb_request *req;
861 req = talloc(mem_ctx, struct ldb_request);
863 ldb_set_errstring(ldb, "Out of Memory");
864 return LDB_ERR_OPERATIONS_ERROR;
867 req->operation = LDB_DELETE;
869 req->controls = controls;
870 req->context = context;
871 req->callback = callback;
878 int ldb_build_rename_req(struct ldb_request **ret_req,
879 struct ldb_context *ldb,
881 struct ldb_dn *olddn,
882 struct ldb_dn *newdn,
883 struct ldb_control **controls,
885 ldb_request_callback_t callback)
887 struct ldb_request *req;
891 req = talloc(mem_ctx, struct ldb_request);
893 ldb_set_errstring(ldb, "Out of Memory");
894 return LDB_ERR_OPERATIONS_ERROR;
897 req->operation = LDB_RENAME;
898 req->op.rename.olddn = olddn;
899 req->op.rename.newdn = newdn;
900 req->controls = controls;
901 req->context = context;
902 req->callback = callback;
909 int ldb_extended_default_callback(struct ldb_context *ldb,
911 struct ldb_reply *ares)
913 struct ldb_result *res;
916 ldb_set_errstring(ldb, "NULL Context in callback");
917 return LDB_ERR_OPERATIONS_ERROR;
920 res = talloc_get_type(context, struct ldb_result);
922 ldb_set_errstring(ldb, "NULL res or ares in callback");
926 switch (ares->type) {
927 case LDB_REPLY_ENTRY:
928 case LDB_REPLY_REFERRAL:
930 ldb_set_errstring(ldb, "invalid ares type in callback");
932 case LDB_REPLY_EXTENDED:
933 /* TODO: we should really support controls on entries and
935 res->extended = talloc_move(res, &ares->response);
936 res->controls = talloc_move(res, &ares->controls);
944 return LDB_ERR_OPERATIONS_ERROR;
947 int ldb_build_extended_req(struct ldb_request **ret_req,
948 struct ldb_context *ldb,
952 struct ldb_control **controls,
954 ldb_request_callback_t callback)
956 struct ldb_request *req;
960 req = talloc(mem_ctx, struct ldb_request);
962 ldb_set_errstring(ldb, "Out of Memory");
963 return LDB_ERR_OPERATIONS_ERROR;
966 req->operation = LDB_EXTENDED;
967 req->op.extended.oid = oid;
968 req->op.extended.data = data;
969 req->controls = controls;
970 req->context = context;
971 req->callback = callback;
978 int ldb_extended(struct ldb_context *ldb,
981 struct ldb_result **_res)
983 struct ldb_request *req;
985 struct ldb_result *res;
989 res = talloc_zero(ldb, struct ldb_result);
991 return LDB_ERR_OPERATIONS_ERROR;
994 ret = ldb_build_extended_req(&req, ldb, ldb,
996 res, ldb_extended_default_callback);
997 if (ret != LDB_SUCCESS) goto done;
999 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1001 ret = ldb_request(ldb, req);
1003 if (ret == LDB_SUCCESS) {
1004 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1010 if (ret != LDB_SUCCESS) {
1019 note that ldb_search() will automatically replace a NULL 'base' value
1020 with the defaultNamingContext from the rootDSE if available.
1022 int ldb_search(struct ldb_context *ldb,
1023 struct ldb_dn *base,
1024 enum ldb_scope scope,
1025 const char *expression,
1026 const char * const *attrs,
1027 struct ldb_result **_res)
1029 struct ldb_request *req;
1031 struct ldb_result *res;
1035 res = talloc_zero(ldb, struct ldb_result);
1037 return LDB_ERR_OPERATIONS_ERROR;
1040 ret = ldb_build_search_req(&req, ldb, ldb,
1041 base?base:ldb_get_default_basedn(ldb),
1047 ldb_search_default_callback);
1049 if (ret != LDB_SUCCESS) goto done;
1051 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1053 ret = ldb_request(ldb, req);
1055 if (ret == LDB_SUCCESS) {
1056 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1062 if (ret != LDB_SUCCESS) {
1071 a useful search function where you can easily define the expression and that
1072 takes a memory context where results are allocated
1075 int ldb_search_exp_fmt(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1076 struct ldb_result **result, struct ldb_dn *base,
1077 enum ldb_scope scope, const char * const *attrs,
1078 const char *exp_fmt, ...)
1080 struct ldb_result *res;
1088 va_start(ap, exp_fmt);
1089 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
1092 if ( ! expression) {
1093 return LDB_ERR_OPERATIONS_ERROR;
1096 ret = ldb_search(ldb, base, scope, expression, attrs, &res);
1098 if (ret == LDB_SUCCESS) {
1099 talloc_steal(mem_ctx, res);
1103 talloc_free(expression);
1109 add a record to the database. Will fail if a record with the
1110 given class and key already exists
1112 int ldb_add(struct ldb_context *ldb,
1113 const struct ldb_message *message)
1115 struct ldb_request *req;
1118 ret = ldb_msg_sanity_check(ldb, message);
1119 if (ret != LDB_SUCCESS) {
1123 ret = ldb_build_add_req(&req, ldb, ldb,
1129 if (ret != LDB_SUCCESS) return ret;
1131 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1133 /* do request and autostart a transaction */
1134 ret = ldb_autotransaction_request(ldb, req);
1141 modify the specified attributes of a record
1143 int ldb_modify(struct ldb_context *ldb,
1144 const struct ldb_message *message)
1146 struct ldb_request *req;
1149 ret = ldb_msg_sanity_check(ldb, message);
1150 if (ret != LDB_SUCCESS) {
1154 ret = ldb_build_mod_req(&req, ldb, ldb,
1160 if (ret != LDB_SUCCESS) return ret;
1162 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1164 /* do request and autostart a transaction */
1165 ret = ldb_autotransaction_request(ldb, req);
1173 delete a record from the database
1175 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1177 struct ldb_request *req;
1180 ret = ldb_build_del_req(&req, ldb, ldb,
1186 if (ret != LDB_SUCCESS) return ret;
1188 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1190 /* do request and autostart a transaction */
1191 ret = ldb_autotransaction_request(ldb, req);
1198 rename a record in the database
1200 int ldb_rename(struct ldb_context *ldb,
1201 struct ldb_dn *olddn, struct ldb_dn *newdn)
1203 struct ldb_request *req;
1206 ret = ldb_build_rename_req(&req, ldb, ldb,
1213 if (ret != LDB_SUCCESS) return ret;
1215 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1217 /* do request and autostart a transaction */
1218 ret = ldb_autotransaction_request(ldb, req);
1226 return the global sequence number
1228 int ldb_sequence_number(struct ldb_context *ldb,
1229 enum ldb_sequence_type type,
1232 struct ldb_request *req;
1235 req = talloc(ldb, struct ldb_request);
1237 ldb_set_errstring(ldb, "Out of Memory");
1238 return LDB_ERR_OPERATIONS_ERROR;
1241 req->operation = LDB_SEQUENCE_NUMBER;
1242 req->controls = NULL;
1243 req->context = NULL;
1244 req->callback = NULL;
1245 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1247 req->op.seq_num.type = type;
1248 /* do request and autostart a transaction */
1249 ret = ldb_request(ldb, req);
1251 if (ret == LDB_SUCCESS) {
1252 *seq_num = req->op.seq_num.seq_num;
1262 return extended error information
1264 const char *ldb_errstring(struct ldb_context *ldb)
1266 if (ldb->err_string) {
1267 return ldb->err_string;
1274 return a string explaining what a ldb error constant meancs
1276 const char *ldb_strerror(int ldb_err)
1281 case LDB_ERR_OPERATIONS_ERROR:
1282 return "Operations error";
1283 case LDB_ERR_PROTOCOL_ERROR:
1284 return "Protocol error";
1285 case LDB_ERR_TIME_LIMIT_EXCEEDED:
1286 return "Time limit exceeded";
1287 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1288 return "Size limit exceeded";
1289 case LDB_ERR_COMPARE_FALSE:
1290 return "Compare false";
1291 case LDB_ERR_COMPARE_TRUE:
1292 return "Compare true";
1293 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1294 return "Auth method not supported";
1295 case LDB_ERR_STRONG_AUTH_REQUIRED:
1296 return "Strong auth required";
1298 case LDB_ERR_REFERRAL:
1299 return "Referral error";
1300 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1301 return "Admin limit exceeded";
1302 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1303 return "Unsupported critical extension";
1304 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1305 return "Confidentiality required";
1306 case LDB_ERR_SASL_BIND_IN_PROGRESS:
1307 return "SASL bind in progress";
1308 case LDB_ERR_NO_SUCH_ATTRIBUTE:
1309 return "No such attribute";
1310 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1311 return "Undefined attribute type";
1312 case LDB_ERR_INAPPROPRIATE_MATCHING:
1313 return "Inappropriate matching";
1314 case LDB_ERR_CONSTRAINT_VIOLATION:
1315 return "Constraint violation";
1316 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1317 return "Attribute or value exists";
1318 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1319 return "Invalid attribute syntax";
1321 case LDB_ERR_NO_SUCH_OBJECT:
1322 return "No such object";
1323 case LDB_ERR_ALIAS_PROBLEM:
1324 return "Alias problem";
1325 case LDB_ERR_INVALID_DN_SYNTAX:
1326 return "Invalid DN syntax";
1328 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1329 return "Alias dereferencing problem";
1331 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1332 return "Inappropriate authentication";
1333 case LDB_ERR_INVALID_CREDENTIALS:
1334 return "Invalid credentials";
1335 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1336 return "insufficient access rights";
1339 case LDB_ERR_UNAVAILABLE:
1340 return "Unavailable";
1341 case LDB_ERR_UNWILLING_TO_PERFORM:
1342 return "Unwilling to perform";
1343 case LDB_ERR_LOOP_DETECT:
1344 return "Loop detect";
1346 case LDB_ERR_NAMING_VIOLATION:
1347 return "Naming violation";
1348 case LDB_ERR_OBJECT_CLASS_VIOLATION:
1349 return "Object class violation";
1350 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1351 return "Not allowed on non-leaf";
1352 case LDB_ERR_NOT_ALLOWED_ON_RDN:
1353 return "Not allowed on RDN";
1354 case LDB_ERR_ENTRY_ALREADY_EXISTS:
1355 return "Entry already exists";
1356 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1357 return "Object class mods prohibited";
1358 /* 70 RESERVED FOR CLDAP */
1359 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1360 return "Affects multiple DSAs";
1366 return "Unknown error";
1370 set backend specific opaque parameters
1372 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1374 struct ldb_opaque *o;
1376 /* allow updating an existing value */
1377 for (o=ldb->opaque;o;o=o->next) {
1378 if (strcmp(o->name, name) == 0) {
1384 o = talloc(ldb, struct ldb_opaque);
1387 return LDB_ERR_OTHER;
1389 o->next = ldb->opaque;
1397 get a previously set opaque value
1399 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1401 struct ldb_opaque *o;
1402 for (o=ldb->opaque;o;o=o->next) {
1403 if (strcmp(o->name, name) == 0) {