Merge ldb_search() and ldb_search_exp_fmt() into a simgle function.
[Samba.git] / source4 / lib / ldb / common / ldb.c
blob3b73947b76b2fdce44ed579cb7cf389d07613549
1 /*
2 ldb database library
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
9 ** under the LGPL
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/>.
26 * Name: ldb
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;
45 int ret;
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 */
50 if (ev_ctx == NULL) {
51 ev_ctx = event_context_init(talloc_autofree_context());
54 ret = ldb_setup_wellknown_attributes(ldb);
55 if (ret != 0) {
56 talloc_free(ldb);
57 return NULL;
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 */
68 return ldb;
72 try to autodetect a basedn if none specified. This fixes one of my
73 pet hates about ldapsearch, which is that you have to get a long,
74 complex basedn right to make any use of it.
76 void ldb_set_default_dns(struct ldb_context *ldb)
78 TALLOC_CTX *tmp_ctx;
79 int ret;
80 struct ldb_result *res;
81 struct ldb_dn *tmp_dn=NULL;
82 static const char *attrs[] = {
83 "rootDomainNamingContext",
84 "configurationNamingContext",
85 "schemaNamingContext",
86 "defaultNamingContext",
87 NULL
90 tmp_ctx = talloc_new(ldb);
91 ret = ldb_search(ldb, tmp_ctx, &res, ldb_dn_new(tmp_ctx, ldb, NULL),
92 LDB_SCOPE_BASE, attrs, "(objectClass=*)");
93 if (ret != LDB_SUCCESS) {
94 talloc_free(tmp_ctx);
95 return;
98 if (res->count != 1) {
99 talloc_free(tmp_ctx);
100 return;
103 if (!ldb_get_opaque(ldb, "rootDomainNamingContext")) {
104 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
105 "rootDomainNamingContext");
106 ldb_set_opaque(ldb, "rootDomainNamingContext", tmp_dn);
109 if (!ldb_get_opaque(ldb, "configurationNamingContext")) {
110 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
111 "configurationNamingContext");
112 ldb_set_opaque(ldb, "configurationNamingContext", tmp_dn);
115 if (!ldb_get_opaque(ldb, "schemaNamingContext")) {
116 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
117 "schemaNamingContext");
118 ldb_set_opaque(ldb, "schemaNamingContext", tmp_dn);
121 if (!ldb_get_opaque(ldb, "defaultNamingContext")) {
122 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
123 "defaultNamingContext");
124 ldb_set_opaque(ldb, "defaultNamingContext", tmp_dn);
127 talloc_free(tmp_ctx);
130 struct ldb_dn *ldb_get_root_basedn(struct ldb_context *ldb)
132 void *opaque = ldb_get_opaque(ldb, "rootDomainNamingContext");
133 return talloc_get_type(opaque, struct ldb_dn);
136 struct ldb_dn *ldb_get_config_basedn(struct ldb_context *ldb)
138 void *opaque = ldb_get_opaque(ldb, "configurationNamingContext");
139 return talloc_get_type(opaque, struct ldb_dn);
142 struct ldb_dn *ldb_get_schema_basedn(struct ldb_context *ldb)
144 void *opaque = ldb_get_opaque(ldb, "schemaNamingContext");
145 return talloc_get_type(opaque, struct ldb_dn);
148 struct ldb_dn *ldb_get_default_basedn(struct ldb_context *ldb)
150 void *opaque = ldb_get_opaque(ldb, "defaultNamingContext");
151 return talloc_get_type(opaque, struct ldb_dn);
155 connect to a database. The URL can either be one of the following forms
156 ldb://path
157 ldapi://path
159 flags is made up of LDB_FLG_*
161 the options are passed uninterpreted to the backend, and are
162 backend specific
164 int ldb_connect(struct ldb_context *ldb, const char *url,
165 unsigned int flags, const char *options[])
167 int ret;
168 const char *url2;
169 /* We seem to need to do this here, or else some utilities don't
170 * get ldb backends */
172 ldb->flags = flags;
174 url2 = talloc_strdup(ldb, url);
175 if (!url2) {
176 ldb_oom(ldb);
177 return LDB_ERR_OPERATIONS_ERROR;
179 ret = ldb_set_opaque(ldb, "ldb_url", talloc_strdup(ldb, url2));
180 if (ret != LDB_SUCCESS) {
181 return ret;
184 ret = ldb_connect_backend(ldb, url, options, &ldb->modules);
185 if (ret != LDB_SUCCESS) {
186 return ret;
189 if (ldb_load_modules(ldb, options) != LDB_SUCCESS) {
190 ldb_debug(ldb, LDB_DEBUG_FATAL,
191 "Unable to load modules for %s: %s\n",
192 url, ldb_errstring(ldb));
193 return LDB_ERR_OTHER;
196 /* TODO: get timeout from options if available there */
197 ldb->default_timeout = 300; /* set default to 5 minutes */
199 /* set the default base dn */
200 ldb_set_default_dns(ldb);
202 return LDB_SUCCESS;
205 void ldb_set_errstring(struct ldb_context *ldb, const char *err_string)
207 if (ldb->err_string) {
208 talloc_free(ldb->err_string);
210 ldb->err_string = talloc_strdup(ldb, err_string);
213 void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
215 va_list ap;
216 char *old_string = NULL;
218 if (ldb->err_string) {
219 old_string = ldb->err_string;
222 va_start(ap, format);
223 ldb->err_string = talloc_vasprintf(ldb, format, ap);
224 va_end(ap);
225 talloc_free(old_string);
228 void ldb_reset_err_string(struct ldb_context *ldb)
230 if (ldb->err_string) {
231 talloc_free(ldb->err_string);
232 ldb->err_string = NULL;
236 #define FIRST_OP(ldb, op) do { \
237 module = ldb->modules; \
238 while (module && module->ops->op == NULL) module = module->next; \
239 if (module == NULL) { \
240 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
241 return LDB_ERR_OPERATIONS_ERROR; \
243 } while (0)
246 start a transaction
248 static int ldb_transaction_start_internal(struct ldb_context *ldb)
250 struct ldb_module *module;
251 int status;
252 FIRST_OP(ldb, start_transaction);
254 ldb_reset_err_string(ldb);
256 status = module->ops->start_transaction(module);
257 if (status != LDB_SUCCESS) {
258 if (ldb->err_string == NULL) {
259 /* no error string was setup by the backend */
260 ldb_asprintf_errstring(ldb,
261 "ldb transaction start: %s (%d)",
262 ldb_strerror(status),
263 status);
266 return status;
270 commit a transaction
272 static int ldb_transaction_commit_internal(struct ldb_context *ldb)
274 struct ldb_module *module;
275 int status;
276 FIRST_OP(ldb, end_transaction);
278 ldb_reset_err_string(ldb);
280 status = module->ops->end_transaction(module);
281 if (status != LDB_SUCCESS) {
282 if (ldb->err_string == NULL) {
283 /* no error string was setup by the backend */
284 ldb_asprintf_errstring(ldb,
285 "ldb transaction commit: %s (%d)",
286 ldb_strerror(status),
287 status);
290 return status;
294 cancel a transaction
296 static int ldb_transaction_cancel_internal(struct ldb_context *ldb)
298 struct ldb_module *module;
299 int status;
300 FIRST_OP(ldb, del_transaction);
302 status = module->ops->del_transaction(module);
303 if (status != LDB_SUCCESS) {
304 if (ldb->err_string == NULL) {
305 /* no error string was setup by the backend */
306 ldb_asprintf_errstring(ldb,
307 "ldb transaction cancel: %s (%d)",
308 ldb_strerror(status),
309 status);
312 return status;
315 int ldb_transaction_start(struct ldb_context *ldb)
317 /* disable autotransactions */
318 ldb->transaction_active++;
320 return ldb_transaction_start_internal(ldb);
323 int ldb_transaction_commit(struct ldb_context *ldb)
325 /* renable autotransactions (when we reach 0) */
326 if (ldb->transaction_active > 0)
327 ldb->transaction_active--;
329 return ldb_transaction_commit_internal(ldb);
332 int ldb_transaction_cancel(struct ldb_context *ldb)
334 /* renable autotransactions (when we reach 0) */
335 if (ldb->transaction_active > 0)
336 ldb->transaction_active--;
338 return ldb_transaction_cancel_internal(ldb);
341 static int ldb_autotransaction_start(struct ldb_context *ldb)
343 /* explicit transaction active, ignore autotransaction request */
344 if (ldb->transaction_active)
345 return LDB_SUCCESS;
347 return ldb_transaction_start_internal(ldb);
350 static int ldb_autotransaction_commit(struct ldb_context *ldb)
352 /* explicit transaction active, ignore autotransaction request */
353 if (ldb->transaction_active)
354 return LDB_SUCCESS;
356 return ldb_transaction_commit_internal(ldb);
359 static int ldb_autotransaction_cancel(struct ldb_context *ldb)
361 /* explicit transaction active, ignore autotransaction request */
362 if (ldb->transaction_active)
363 return LDB_SUCCESS;
365 return ldb_transaction_cancel_internal(ldb);
368 /* autostarts a transacion if none active */
369 static int ldb_autotransaction_request(struct ldb_context *ldb,
370 struct ldb_request *req)
372 int ret;
374 ret = ldb_autotransaction_start(ldb);
375 if (ret != LDB_SUCCESS) {
376 return ret;
379 ret = ldb_request(ldb, req);
380 if (ret == LDB_SUCCESS) {
381 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
384 if (ret == LDB_SUCCESS) {
385 return ldb_autotransaction_commit(ldb);
387 ldb_autotransaction_cancel(ldb);
389 if (ldb->err_string == NULL) {
390 /* no error string was setup by the backend */
391 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
394 return ret;
397 int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
399 int ret;
400 if (!handle) {
401 return LDB_SUCCESS;
404 ret = handle->module->ops->wait(handle, type);
405 if (!ldb_errstring(handle->module->ldb)) {
406 /* Set a default error string, to place the blame somewhere */
407 ldb_asprintf_errstring(handle->module->ldb,
408 "error waiting on module %s: %s (%d)",
409 handle->module->ops->name,
410 ldb_strerror(ret), ret);
412 return ret;
415 /* set the specified timeout or, if timeout is 0 set the default timeout */
416 /* timeout == -1 means no timeout */
417 int ldb_set_timeout(struct ldb_context *ldb,
418 struct ldb_request *req,
419 int timeout)
421 if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
423 if (timeout != 0) {
424 req->timeout = timeout;
425 } else {
426 req->timeout = ldb->default_timeout;
428 req->starttime = time(NULL);
430 return LDB_SUCCESS;
433 /* calculates the new timeout based on the previous starttime and timeout */
434 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb,
435 struct ldb_request *oldreq,
436 struct ldb_request *newreq)
438 time_t now;
440 if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
442 now = time(NULL);
444 if (oldreq == NULL)
445 return ldb_set_timeout(ldb, newreq, 0);
447 if ((now - oldreq->starttime) > oldreq->timeout) {
448 return LDB_ERR_TIME_LIMIT_EXCEEDED;
450 newreq->starttime = oldreq->starttime;
451 newreq->timeout = oldreq->timeout - (now - oldreq->starttime);
453 return LDB_SUCCESS;
458 set the permissions for new files to be passed to open() in
459 backends that use local files
461 void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
463 ldb->create_perms = perms;
466 void ldb_set_event_context(struct ldb_context *ldb, struct event_context *ev)
468 ldb->ev_ctx = ev;
471 struct event_context * ldb_get_event_context(struct ldb_context *ldb)
473 return ldb->ev_ctx;
477 start an ldb request
478 NOTE: the request must be a talloc context.
479 returns LDB_ERR_* on errors.
481 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
483 struct ldb_module *module;
484 int ret;
486 ldb_reset_err_string(ldb);
488 /* call the first module in the chain */
489 switch (req->operation) {
490 case LDB_SEARCH:
491 FIRST_OP(ldb, search);
492 ret = module->ops->search(module, req);
493 break;
494 case LDB_ADD:
495 FIRST_OP(ldb, add);
496 ret = module->ops->add(module, req);
497 break;
498 case LDB_MODIFY:
499 FIRST_OP(ldb, modify);
500 ret = module->ops->modify(module, req);
501 break;
502 case LDB_DELETE:
503 FIRST_OP(ldb, del);
504 ret = module->ops->del(module, req);
505 break;
506 case LDB_RENAME:
507 FIRST_OP(ldb, rename);
508 ret = module->ops->rename(module, req);
509 break;
510 case LDB_EXTENDED:
511 FIRST_OP(ldb, extended);
512 ret = module->ops->extended(module, req);
513 break;
514 case LDB_SEQUENCE_NUMBER:
515 FIRST_OP(ldb, sequence_number);
516 ret = module->ops->sequence_number(module, req);
517 break;
518 default:
519 FIRST_OP(ldb, request);
520 ret = module->ops->request(module, req);
521 break;
524 return ret;
528 search the database given a LDAP-like search expression
530 returns an LDB error code
532 Use talloc_free to free the ldb_message returned in 'res', if successful
535 int ldb_search_default_callback(struct ldb_context *ldb,
536 void *context,
537 struct ldb_reply *ares)
539 struct ldb_result *res;
540 int n;
542 if (!context) {
543 ldb_set_errstring(ldb, "NULL Context in callback");
544 return LDB_ERR_OPERATIONS_ERROR;
547 res = talloc_get_type(context, struct ldb_result);
549 if (!res || !ares) {
550 ldb_set_errstring(ldb, "NULL res or ares in callback");
551 goto error;
554 switch (ares->type) {
555 case LDB_REPLY_ENTRY:
556 res->msgs = talloc_realloc(res, res->msgs,
557 struct ldb_message *,
558 res->count + 2);
559 if (! res->msgs) {
560 goto error;
563 res->msgs[res->count + 1] = NULL;
565 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
566 res->count++;
567 break;
568 case LDB_REPLY_REFERRAL:
569 if (res->refs) {
570 for (n = 0; res->refs[n]; n++) /*noop*/ ;
571 } else {
572 n = 0;
575 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
576 if (! res->refs) {
577 goto error;
580 res->refs[n] = talloc_move(res->refs, &ares->referral);
581 res->refs[n + 1] = NULL;
582 break;
583 case LDB_REPLY_EXTENDED:
584 case LDB_REPLY_DONE:
585 /* TODO: we should really support controls on entries
586 * and referrals too! */
587 res->controls = talloc_move(res, &ares->controls);
588 break;
590 talloc_free(ares);
591 return LDB_SUCCESS;
593 error:
594 talloc_free(ares);
595 return LDB_ERR_OPERATIONS_ERROR;
598 int ldb_build_search_req(struct ldb_request **ret_req,
599 struct ldb_context *ldb,
600 void *mem_ctx,
601 struct ldb_dn *base,
602 enum ldb_scope scope,
603 const char *expression,
604 const char * const *attrs,
605 struct ldb_control **controls,
606 void *context,
607 ldb_request_callback_t callback)
609 struct ldb_request *req;
611 *ret_req = NULL;
613 req = talloc(mem_ctx, struct ldb_request);
614 if (req == NULL) {
615 ldb_set_errstring(ldb, "Out of Memory");
616 return LDB_ERR_OPERATIONS_ERROR;
619 req->operation = LDB_SEARCH;
620 if (base == NULL) {
621 req->op.search.base = ldb_dn_new(req, ldb, NULL);
622 } else {
623 req->op.search.base = base;
625 req->op.search.scope = scope;
627 req->op.search.tree = ldb_parse_tree(req, expression);
628 if (req->op.search.tree == NULL) {
629 ldb_set_errstring(ldb, "Unable to parse search expression");
630 talloc_free(req);
631 return LDB_ERR_OPERATIONS_ERROR;
634 req->op.search.attrs = attrs;
635 req->controls = controls;
636 req->context = context;
637 req->callback = callback;
639 *ret_req = req;
640 return LDB_SUCCESS;
643 int ldb_build_add_req(struct ldb_request **ret_req,
644 struct ldb_context *ldb,
645 void *mem_ctx,
646 const struct ldb_message *message,
647 struct ldb_control **controls,
648 void *context,
649 ldb_request_callback_t callback)
651 struct ldb_request *req;
653 *ret_req = NULL;
655 req = talloc(mem_ctx, struct ldb_request);
656 if (req == NULL) {
657 ldb_set_errstring(ldb, "Out of Memory");
658 return LDB_ERR_OPERATIONS_ERROR;
661 req->operation = LDB_ADD;
662 req->op.add.message = message;
663 req->controls = controls;
664 req->context = context;
665 req->callback = callback;
667 *ret_req = req;
669 return LDB_SUCCESS;
672 int ldb_build_mod_req(struct ldb_request **ret_req,
673 struct ldb_context *ldb,
674 void *mem_ctx,
675 const struct ldb_message *message,
676 struct ldb_control **controls,
677 void *context,
678 ldb_request_callback_t callback)
680 struct ldb_request *req;
682 *ret_req = NULL;
684 req = talloc(mem_ctx, struct ldb_request);
685 if (req == NULL) {
686 ldb_set_errstring(ldb, "Out of Memory");
687 return LDB_ERR_OPERATIONS_ERROR;
690 req->operation = LDB_MODIFY;
691 req->op.mod.message = message;
692 req->controls = controls;
693 req->context = context;
694 req->callback = callback;
696 *ret_req = req;
698 return LDB_SUCCESS;
701 int ldb_build_del_req(struct ldb_request **ret_req,
702 struct ldb_context *ldb,
703 void *mem_ctx,
704 struct ldb_dn *dn,
705 struct ldb_control **controls,
706 void *context,
707 ldb_request_callback_t callback)
709 struct ldb_request *req;
711 *ret_req = NULL;
713 req = talloc(mem_ctx, struct ldb_request);
714 if (req == NULL) {
715 ldb_set_errstring(ldb, "Out of Memory");
716 return LDB_ERR_OPERATIONS_ERROR;
719 req->operation = LDB_DELETE;
720 req->op.del.dn = dn;
721 req->controls = controls;
722 req->context = context;
723 req->callback = callback;
725 *ret_req = req;
727 return LDB_SUCCESS;
730 int ldb_build_rename_req(struct ldb_request **ret_req,
731 struct ldb_context *ldb,
732 void *mem_ctx,
733 struct ldb_dn *olddn,
734 struct ldb_dn *newdn,
735 struct ldb_control **controls,
736 void *context,
737 ldb_request_callback_t callback)
739 struct ldb_request *req;
741 *ret_req = NULL;
743 req = talloc(mem_ctx, struct ldb_request);
744 if (req == NULL) {
745 ldb_set_errstring(ldb, "Out of Memory");
746 return LDB_ERR_OPERATIONS_ERROR;
749 req->operation = LDB_RENAME;
750 req->op.rename.olddn = olddn;
751 req->op.rename.newdn = newdn;
752 req->controls = controls;
753 req->context = context;
754 req->callback = callback;
756 *ret_req = req;
758 return LDB_SUCCESS;
761 int ldb_extended_default_callback(struct ldb_context *ldb,
762 void *context,
763 struct ldb_reply *ares)
765 struct ldb_result *res;
767 if (!context) {
768 ldb_set_errstring(ldb, "NULL Context in callback");
769 return LDB_ERR_OPERATIONS_ERROR;
772 res = talloc_get_type(context, struct ldb_result);
773 if (!res || !ares) {
774 ldb_set_errstring(ldb, "NULL res or ares in callback");
775 goto error;
778 switch (ares->type) {
779 case LDB_REPLY_ENTRY:
780 case LDB_REPLY_REFERRAL:
781 case LDB_REPLY_DONE:
782 ldb_set_errstring(ldb, "invalid ares type in callback");
783 goto error;
784 case LDB_REPLY_EXTENDED:
785 /* TODO: we should really support controls on entries and
786 * referrals too! */
787 res->extended = talloc_move(res, &ares->response);
788 res->controls = talloc_move(res, &ares->controls);
789 break;
791 talloc_free(ares);
792 return LDB_SUCCESS;
794 error:
795 talloc_free(ares);
796 return LDB_ERR_OPERATIONS_ERROR;
799 int ldb_build_extended_req(struct ldb_request **ret_req,
800 struct ldb_context *ldb,
801 void *mem_ctx,
802 const char *oid,
803 void *data,
804 struct ldb_control **controls,
805 void *context,
806 ldb_request_callback_t callback)
808 struct ldb_request *req;
810 *ret_req = NULL;
812 req = talloc(mem_ctx, struct ldb_request);
813 if (req == NULL) {
814 ldb_set_errstring(ldb, "Out of Memory");
815 return LDB_ERR_OPERATIONS_ERROR;
818 req->operation = LDB_EXTENDED;
819 req->op.extended.oid = oid;
820 req->op.extended.data = data;
821 req->controls = controls;
822 req->context = context;
823 req->callback = callback;
825 *ret_req = req;
827 return LDB_SUCCESS;
830 int ldb_extended(struct ldb_context *ldb,
831 const char *oid,
832 void *data,
833 struct ldb_result **_res)
835 struct ldb_request *req;
836 int ret;
837 struct ldb_result *res;
839 *_res = NULL;
841 res = talloc_zero(ldb, struct ldb_result);
842 if (!res) {
843 return LDB_ERR_OPERATIONS_ERROR;
846 ret = ldb_build_extended_req(&req, ldb, ldb,
847 oid, data, NULL,
848 res, ldb_extended_default_callback);
849 if (ret != LDB_SUCCESS) goto done;
851 ldb_set_timeout(ldb, req, 0); /* use default timeout */
853 ret = ldb_request(ldb, req);
855 if (ret == LDB_SUCCESS) {
856 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
859 talloc_free(req);
861 done:
862 if (ret != LDB_SUCCESS) {
863 talloc_free(res);
866 *_res = res;
867 return ret;
871 note that ldb_search() will automatically replace a NULL 'base' value
872 with the defaultNamingContext from the rootDSE if available.
874 int ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
875 struct ldb_result **result, struct ldb_dn *base,
876 enum ldb_scope scope, const char * const *attrs,
877 const char *exp_fmt, ...)
879 struct ldb_request *req;
880 struct ldb_result *res;
881 char *expression;
882 va_list ap;
883 int ret;
885 expression = NULL;
886 *result = NULL;
887 req = NULL;
889 res = talloc_zero(mem_ctx, struct ldb_result);
890 if (!res) {
891 return LDB_ERR_OPERATIONS_ERROR;
894 if (exp_fmt) {
895 va_start(ap, exp_fmt);
896 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
897 va_end(ap);
899 if (!expression) {
900 talloc_free(res);
901 return LDB_ERR_OPERATIONS_ERROR;
905 ret = ldb_build_search_req(&req, ldb, mem_ctx,
906 base?base:ldb_get_default_basedn(ldb),
907 scope,
908 expression,
909 attrs,
910 NULL,
911 res,
912 ldb_search_default_callback);
914 if (ret != LDB_SUCCESS) goto done;
916 ldb_set_timeout(ldb, req, 0); /* use default timeout */
918 ret = ldb_request(ldb, req);
920 if (ret == LDB_SUCCESS) {
921 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
924 done:
925 if (ret != LDB_SUCCESS) {
926 talloc_free(res);
927 res = NULL;
930 talloc_free(expression);
931 talloc_free(req);
933 *result = res;
934 return ret;
938 add a record to the database. Will fail if a record with the
939 given class and key already exists
941 int ldb_add(struct ldb_context *ldb,
942 const struct ldb_message *message)
944 struct ldb_request *req;
945 int ret;
947 ret = ldb_msg_sanity_check(ldb, message);
948 if (ret != LDB_SUCCESS) {
949 return ret;
952 ret = ldb_build_add_req(&req, ldb, ldb,
953 message,
954 NULL,
955 NULL,
956 NULL);
958 if (ret != LDB_SUCCESS) return ret;
960 ldb_set_timeout(ldb, req, 0); /* use default timeout */
962 /* do request and autostart a transaction */
963 ret = ldb_autotransaction_request(ldb, req);
965 talloc_free(req);
966 return ret;
970 modify the specified attributes of a record
972 int ldb_modify(struct ldb_context *ldb,
973 const struct ldb_message *message)
975 struct ldb_request *req;
976 int ret;
978 ret = ldb_msg_sanity_check(ldb, message);
979 if (ret != LDB_SUCCESS) {
980 return ret;
983 ret = ldb_build_mod_req(&req, ldb, ldb,
984 message,
985 NULL,
986 NULL,
987 NULL);
989 if (ret != LDB_SUCCESS) return ret;
991 ldb_set_timeout(ldb, req, 0); /* use default timeout */
993 /* do request and autostart a transaction */
994 ret = ldb_autotransaction_request(ldb, req);
996 talloc_free(req);
997 return ret;
1002 delete a record from the database
1004 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1006 struct ldb_request *req;
1007 int ret;
1009 ret = ldb_build_del_req(&req, ldb, ldb,
1011 NULL,
1012 NULL,
1013 NULL);
1015 if (ret != LDB_SUCCESS) return ret;
1017 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1019 /* do request and autostart a transaction */
1020 ret = ldb_autotransaction_request(ldb, req);
1022 talloc_free(req);
1023 return ret;
1027 rename a record in the database
1029 int ldb_rename(struct ldb_context *ldb,
1030 struct ldb_dn *olddn, struct ldb_dn *newdn)
1032 struct ldb_request *req;
1033 int ret;
1035 ret = ldb_build_rename_req(&req, ldb, ldb,
1036 olddn,
1037 newdn,
1038 NULL,
1039 NULL,
1040 NULL);
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);
1049 talloc_free(req);
1050 return ret;
1055 return the global sequence number
1057 int ldb_sequence_number(struct ldb_context *ldb,
1058 enum ldb_sequence_type type,
1059 uint64_t *seq_num)
1061 struct ldb_request *req;
1062 int ret;
1064 req = talloc(ldb, struct ldb_request);
1065 if (req == NULL) {
1066 ldb_set_errstring(ldb, "Out of Memory");
1067 return LDB_ERR_OPERATIONS_ERROR;
1070 req->operation = LDB_SEQUENCE_NUMBER;
1071 req->controls = NULL;
1072 req->context = NULL;
1073 req->callback = NULL;
1074 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1076 req->op.seq_num.type = type;
1077 /* do request and autostart a transaction */
1078 ret = ldb_request(ldb, req);
1080 if (ret == LDB_SUCCESS) {
1081 *seq_num = req->op.seq_num.seq_num;
1084 talloc_free(req);
1085 return ret;
1091 return extended error information
1093 const char *ldb_errstring(struct ldb_context *ldb)
1095 if (ldb->err_string) {
1096 return ldb->err_string;
1099 return NULL;
1103 return a string explaining what a ldb error constant meancs
1105 const char *ldb_strerror(int ldb_err)
1107 switch (ldb_err) {
1108 case LDB_SUCCESS:
1109 return "Success";
1110 case LDB_ERR_OPERATIONS_ERROR:
1111 return "Operations error";
1112 case LDB_ERR_PROTOCOL_ERROR:
1113 return "Protocol error";
1114 case LDB_ERR_TIME_LIMIT_EXCEEDED:
1115 return "Time limit exceeded";
1116 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1117 return "Size limit exceeded";
1118 case LDB_ERR_COMPARE_FALSE:
1119 return "Compare false";
1120 case LDB_ERR_COMPARE_TRUE:
1121 return "Compare true";
1122 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1123 return "Auth method not supported";
1124 case LDB_ERR_STRONG_AUTH_REQUIRED:
1125 return "Strong auth required";
1126 /* 9 RESERVED */
1127 case LDB_ERR_REFERRAL:
1128 return "Referral error";
1129 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1130 return "Admin limit exceeded";
1131 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1132 return "Unsupported critical extension";
1133 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1134 return "Confidentiality required";
1135 case LDB_ERR_SASL_BIND_IN_PROGRESS:
1136 return "SASL bind in progress";
1137 case LDB_ERR_NO_SUCH_ATTRIBUTE:
1138 return "No such attribute";
1139 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1140 return "Undefined attribute type";
1141 case LDB_ERR_INAPPROPRIATE_MATCHING:
1142 return "Inappropriate matching";
1143 case LDB_ERR_CONSTRAINT_VIOLATION:
1144 return "Constraint violation";
1145 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1146 return "Attribute or value exists";
1147 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1148 return "Invalid attribute syntax";
1149 /* 22-31 unused */
1150 case LDB_ERR_NO_SUCH_OBJECT:
1151 return "No such object";
1152 case LDB_ERR_ALIAS_PROBLEM:
1153 return "Alias problem";
1154 case LDB_ERR_INVALID_DN_SYNTAX:
1155 return "Invalid DN syntax";
1156 /* 35 RESERVED */
1157 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1158 return "Alias dereferencing problem";
1159 /* 37-47 unused */
1160 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1161 return "Inappropriate authentication";
1162 case LDB_ERR_INVALID_CREDENTIALS:
1163 return "Invalid credentials";
1164 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1165 return "insufficient access rights";
1166 case LDB_ERR_BUSY:
1167 return "Busy";
1168 case LDB_ERR_UNAVAILABLE:
1169 return "Unavailable";
1170 case LDB_ERR_UNWILLING_TO_PERFORM:
1171 return "Unwilling to perform";
1172 case LDB_ERR_LOOP_DETECT:
1173 return "Loop detect";
1174 /* 55-63 unused */
1175 case LDB_ERR_NAMING_VIOLATION:
1176 return "Naming violation";
1177 case LDB_ERR_OBJECT_CLASS_VIOLATION:
1178 return "Object class violation";
1179 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1180 return "Not allowed on non-leaf";
1181 case LDB_ERR_NOT_ALLOWED_ON_RDN:
1182 return "Not allowed on RDN";
1183 case LDB_ERR_ENTRY_ALREADY_EXISTS:
1184 return "Entry already exists";
1185 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1186 return "Object class mods prohibited";
1187 /* 70 RESERVED FOR CLDAP */
1188 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1189 return "Affects multiple DSAs";
1190 /* 72-79 unused */
1191 case LDB_ERR_OTHER:
1192 return "Other";
1195 return "Unknown error";
1199 set backend specific opaque parameters
1201 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1203 struct ldb_opaque *o;
1205 /* allow updating an existing value */
1206 for (o=ldb->opaque;o;o=o->next) {
1207 if (strcmp(o->name, name) == 0) {
1208 o->value = value;
1209 return LDB_SUCCESS;
1213 o = talloc(ldb, struct ldb_opaque);
1214 if (o == NULL) {
1215 ldb_oom(ldb);
1216 return LDB_ERR_OTHER;
1218 o->next = ldb->opaque;
1219 o->name = name;
1220 o->value = value;
1221 ldb->opaque = o;
1222 return LDB_SUCCESS;
1226 get a previously set opaque value
1228 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1230 struct ldb_opaque *o;
1231 for (o=ldb->opaque;o;o=o->next) {
1232 if (strcmp(o->name, name) == 0) {
1233 return o->value;
1236 return NULL;