s4-ldb: expose ldb_transaction_prepare_commit() in ldb
[Samba/aatanasov.git] / source4 / lib / ldb / common / ldb.c
blob613451a7c2a2cba9ce634f7fc478fe95a52aae7b
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 #define TEVENT_DEPRECATED 1
36 #include "ldb_private.h"
38 static int ldb_context_destructor(void *ptr)
40 struct ldb_context *ldb = talloc_get_type(ptr, struct ldb_context);
42 if (ldb->transaction_active) {
43 ldb_debug(ldb, LDB_DEBUG_FATAL,
44 "A transaction is still active in ldb context [%p]",
45 ldb);
48 return 0;
52 this is used to catch debug messages from events
54 static void ldb_tevent_debug(void *context, enum tevent_debug_level level,
55 const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
57 static void ldb_tevent_debug(void *context, enum tevent_debug_level level,
58 const char *fmt, va_list ap)
60 struct ldb_context *ldb = talloc_get_type(context, struct ldb_context);
61 enum ldb_debug_level ldb_level = LDB_DEBUG_FATAL;
62 char *s = NULL;
64 switch (level) {
65 case TEVENT_DEBUG_FATAL:
66 ldb_level = LDB_DEBUG_FATAL;
67 break;
68 case TEVENT_DEBUG_ERROR:
69 ldb_level = LDB_DEBUG_ERROR;
70 break;
71 case TEVENT_DEBUG_WARNING:
72 ldb_level = LDB_DEBUG_WARNING;
73 break;
74 case TEVENT_DEBUG_TRACE:
75 ldb_level = LDB_DEBUG_TRACE;
76 break;
79 vasprintf(&s, fmt, ap);
80 if (!s) return;
81 ldb_debug(ldb, ldb_level, "tevent: %s", s);
82 free(s);
86 initialise a ldb context
87 The mem_ctx is required
88 The event_ctx is required
90 struct ldb_context *ldb_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev_ctx)
92 struct ldb_context *ldb;
93 int ret;
95 ldb = talloc_zero(mem_ctx, struct ldb_context);
96 /* FIXME: Hack a new event context so that CMD line utilities work
97 * until we have them all converted */
98 if (ev_ctx == NULL) {
99 ev_ctx = tevent_context_init(talloc_autofree_context());
100 tevent_set_debug(ev_ctx, ldb_tevent_debug, ldb);
101 tevent_loop_allow_nesting(ev_ctx);
104 ret = ldb_setup_wellknown_attributes(ldb);
105 if (ret != 0) {
106 talloc_free(ldb);
107 return NULL;
110 ldb_set_utf8_default(ldb);
111 ldb_set_create_perms(ldb, 0666);
112 ldb_set_modules_dir(ldb, LDB_MODULESDIR);
113 ldb_set_event_context(ldb, ev_ctx);
115 /* TODO: get timeout from options if available there */
116 ldb->default_timeout = 300; /* set default to 5 minutes */
118 talloc_set_destructor((TALLOC_CTX *)ldb, ldb_context_destructor);
120 return ldb;
124 try to autodetect a basedn if none specified. This fixes one of my
125 pet hates about ldapsearch, which is that you have to get a long,
126 complex basedn right to make any use of it.
128 void ldb_set_default_dns(struct ldb_context *ldb)
130 TALLOC_CTX *tmp_ctx;
131 int ret;
132 struct ldb_result *res;
133 struct ldb_dn *tmp_dn=NULL;
134 static const char *attrs[] = {
135 "rootDomainNamingContext",
136 "configurationNamingContext",
137 "schemaNamingContext",
138 "defaultNamingContext",
139 NULL
142 tmp_ctx = talloc_new(ldb);
143 ret = ldb_search(ldb, tmp_ctx, &res, ldb_dn_new(tmp_ctx, ldb, NULL),
144 LDB_SCOPE_BASE, attrs, "(objectClass=*)");
145 if (ret != LDB_SUCCESS) {
146 talloc_free(tmp_ctx);
147 return;
150 if (res->count != 1) {
151 talloc_free(tmp_ctx);
152 return;
155 if (!ldb_get_opaque(ldb, "rootDomainNamingContext")) {
156 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
157 "rootDomainNamingContext");
158 ldb_set_opaque(ldb, "rootDomainNamingContext", tmp_dn);
161 if (!ldb_get_opaque(ldb, "configurationNamingContext")) {
162 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
163 "configurationNamingContext");
164 ldb_set_opaque(ldb, "configurationNamingContext", tmp_dn);
167 if (!ldb_get_opaque(ldb, "schemaNamingContext")) {
168 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
169 "schemaNamingContext");
170 ldb_set_opaque(ldb, "schemaNamingContext", tmp_dn);
173 if (!ldb_get_opaque(ldb, "defaultNamingContext")) {
174 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
175 "defaultNamingContext");
176 ldb_set_opaque(ldb, "defaultNamingContext", tmp_dn);
179 talloc_free(tmp_ctx);
182 struct ldb_dn *ldb_get_root_basedn(struct ldb_context *ldb)
184 void *opaque = ldb_get_opaque(ldb, "rootDomainNamingContext");
185 return talloc_get_type(opaque, struct ldb_dn);
188 struct ldb_dn *ldb_get_config_basedn(struct ldb_context *ldb)
190 void *opaque = ldb_get_opaque(ldb, "configurationNamingContext");
191 return talloc_get_type(opaque, struct ldb_dn);
194 struct ldb_dn *ldb_get_schema_basedn(struct ldb_context *ldb)
196 void *opaque = ldb_get_opaque(ldb, "schemaNamingContext");
197 return talloc_get_type(opaque, struct ldb_dn);
200 struct ldb_dn *ldb_get_default_basedn(struct ldb_context *ldb)
202 void *opaque = ldb_get_opaque(ldb, "defaultNamingContext");
203 return talloc_get_type(opaque, struct ldb_dn);
207 connect to a database. The URL can either be one of the following forms
208 ldb://path
209 ldapi://path
211 flags is made up of LDB_FLG_*
213 the options are passed uninterpreted to the backend, and are
214 backend specific
216 int ldb_connect(struct ldb_context *ldb, const char *url,
217 unsigned int flags, const char *options[])
219 int ret;
220 const char *url2;
221 /* We seem to need to do this here, or else some utilities don't
222 * get ldb backends */
224 ldb->flags = flags;
226 url2 = talloc_strdup(ldb, url);
227 if (!url2) {
228 ldb_oom(ldb);
229 return LDB_ERR_OPERATIONS_ERROR;
231 ret = ldb_set_opaque(ldb, "ldb_url", talloc_strdup(ldb, url2));
232 if (ret != LDB_SUCCESS) {
233 return ret;
236 ret = ldb_connect_backend(ldb, url, options, &ldb->modules);
237 if (ret != LDB_SUCCESS) {
238 return ret;
241 if (ldb_load_modules(ldb, options) != LDB_SUCCESS) {
242 ldb_debug(ldb, LDB_DEBUG_FATAL,
243 "Unable to load modules for %s: %s",
244 url, ldb_errstring(ldb));
245 return LDB_ERR_OTHER;
248 /* set the default base dn */
249 ldb_set_default_dns(ldb);
251 return LDB_SUCCESS;
254 void ldb_set_errstring(struct ldb_context *ldb, const char *err_string)
256 if (ldb->err_string) {
257 talloc_free(ldb->err_string);
259 ldb->err_string = talloc_strdup(ldb, err_string);
262 void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
264 va_list ap;
265 char *old_string = NULL;
267 if (ldb->err_string) {
268 old_string = ldb->err_string;
271 va_start(ap, format);
272 ldb->err_string = talloc_vasprintf(ldb, format, ap);
273 va_end(ap);
274 talloc_free(old_string);
277 void ldb_reset_err_string(struct ldb_context *ldb)
279 if (ldb->err_string) {
280 talloc_free(ldb->err_string);
281 ldb->err_string = NULL;
285 #define FIRST_OP_NOERR(ldb, op) do { \
286 module = ldb->modules; \
287 while (module && module->ops->op == NULL) module = module->next; \
288 } while (0)
290 #define FIRST_OP(ldb, op) do { \
291 FIRST_OP_NOERR(ldb, op); \
292 if (module == NULL) { \
293 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
294 return LDB_ERR_OPERATIONS_ERROR; \
296 } while (0)
300 start a transaction
302 int ldb_transaction_start(struct ldb_context *ldb)
304 struct ldb_module *module;
305 int status;
307 ldb_debug(ldb, LDB_DEBUG_TRACE,
308 "start ldb transaction (nesting: %d)",
309 ldb->transaction_active);
311 /* explicit transaction active, count nested requests */
312 if (ldb->transaction_active) {
313 ldb->transaction_active++;
314 return LDB_SUCCESS;
317 /* start a new transaction */
318 ldb->transaction_active++;
319 ldb->prepare_commit_done = false;
321 FIRST_OP(ldb, start_transaction);
323 ldb_reset_err_string(ldb);
325 status = module->ops->start_transaction(module);
326 if (status != LDB_SUCCESS) {
327 if (ldb->err_string == NULL) {
328 /* no error string was setup by the backend */
329 ldb_asprintf_errstring(ldb,
330 "ldb transaction start: %s (%d)",
331 ldb_strerror(status),
332 status);
335 return status;
339 prepare for transaction commit (first phase of two phase commit)
341 int ldb_transaction_prepare_commit(struct ldb_context *ldb)
343 struct ldb_module *module;
344 int status;
346 if (ldb->prepare_commit_done) {
347 return LDB_SUCCESS;
350 /* commit only when all nested transactions are complete */
351 if (ldb->transaction_active > 1) {
352 return LDB_SUCCESS;
355 ldb->prepare_commit_done = true;
357 if (ldb->transaction_active < 0) {
358 ldb_debug(ldb, LDB_DEBUG_FATAL,
359 "prepare commit called but no ldb transactions are active!");
360 ldb->transaction_active = 0;
361 return LDB_ERR_OPERATIONS_ERROR;
364 /* call prepare transaction if available */
365 FIRST_OP_NOERR(ldb, prepare_commit);
366 if (module == NULL) {
367 return LDB_SUCCESS;
370 status = module->ops->prepare_commit(module);
371 if (status != LDB_SUCCESS) {
372 /* if a module fails the prepare then we need
373 to call the end transaction for everyone */
374 FIRST_OP(ldb, end_transaction);
375 module->ops->end_transaction(module);
376 if (ldb->err_string == NULL) {
377 /* no error string was setup by the backend */
378 ldb_asprintf_errstring(ldb,
379 "ldb transaction prepare commit: %s (%d)",
380 ldb_strerror(status),
381 status);
385 return status;
390 commit a transaction
392 int ldb_transaction_commit(struct ldb_context *ldb)
394 struct ldb_module *module;
395 int status;
397 status = ldb_transaction_prepare_commit(ldb);
398 if (status != LDB_SUCCESS) {
399 return status;
402 ldb->transaction_active--;
404 ldb_debug(ldb, LDB_DEBUG_TRACE,
405 "commit ldb transaction (nesting: %d)",
406 ldb->transaction_active);
408 /* commit only when all nested transactions are complete */
409 if (ldb->transaction_active > 0) {
410 return LDB_SUCCESS;
413 if (ldb->transaction_active < 0) {
414 ldb_debug(ldb, LDB_DEBUG_FATAL,
415 "commit called but no ldb transactions are active!");
416 ldb->transaction_active = 0;
417 return LDB_ERR_OPERATIONS_ERROR;
420 ldb_reset_err_string(ldb);
422 FIRST_OP(ldb, end_transaction);
423 status = module->ops->end_transaction(module);
424 if (status != LDB_SUCCESS) {
425 if (ldb->err_string == NULL) {
426 /* no error string was setup by the backend */
427 ldb_asprintf_errstring(ldb,
428 "ldb transaction commit: %s (%d)",
429 ldb_strerror(status),
430 status);
432 /* cancel the transaction */
433 FIRST_OP(ldb, del_transaction);
434 module->ops->del_transaction(module);
436 return status;
441 cancel a transaction
443 int ldb_transaction_cancel(struct ldb_context *ldb)
445 struct ldb_module *module;
446 int status;
448 ldb->transaction_active--;
450 ldb_debug(ldb, LDB_DEBUG_TRACE,
451 "cancel ldb transaction (nesting: %d)",
452 ldb->transaction_active);
454 /* really cancel only if all nested transactions are complete */
455 if (ldb->transaction_active > 0) {
456 return LDB_SUCCESS;
459 if (ldb->transaction_active < 0) {
460 ldb_debug(ldb, LDB_DEBUG_FATAL,
461 "cancel called but no ldb transactions are active!");
462 ldb->transaction_active = 0;
463 return LDB_ERR_OPERATIONS_ERROR;
466 FIRST_OP(ldb, del_transaction);
468 status = module->ops->del_transaction(module);
469 if (status != LDB_SUCCESS) {
470 if (ldb->err_string == NULL) {
471 /* no error string was setup by the backend */
472 ldb_asprintf_errstring(ldb,
473 "ldb transaction cancel: %s (%d)",
474 ldb_strerror(status),
475 status);
478 return status;
481 /* autostarts a transacion if none active */
482 static int ldb_autotransaction_request(struct ldb_context *ldb,
483 struct ldb_request *req)
485 int ret;
487 ret = ldb_transaction_start(ldb);
488 if (ret != LDB_SUCCESS) {
489 return ret;
492 ret = ldb_request(ldb, req);
493 if (ret == LDB_SUCCESS) {
494 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
497 if (ret == LDB_SUCCESS) {
498 return ldb_transaction_commit(ldb);
500 ldb_transaction_cancel(ldb);
502 if (ldb->err_string == NULL) {
503 /* no error string was setup by the backend */
504 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
507 return ret;
510 int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
512 struct tevent_context *ev;
513 int ret;
515 if (!handle) {
516 return LDB_ERR_UNAVAILABLE;
519 if (handle->state == LDB_ASYNC_DONE) {
520 return handle->status;
523 ev = ldb_get_event_context(handle->ldb);
524 if (NULL == ev) {
525 return LDB_ERR_OPERATIONS_ERROR;
528 switch (type) {
529 case LDB_WAIT_NONE:
530 ret = tevent_loop_once(ev);
531 if (ret != 0) {
532 return LDB_ERR_OPERATIONS_ERROR;
534 if (handle->state == LDB_ASYNC_DONE ||
535 handle->status != LDB_SUCCESS) {
536 return handle->status;
538 break;
540 case LDB_WAIT_ALL:
541 while (handle->state != LDB_ASYNC_DONE) {
542 ret = tevent_loop_once(ev);
543 if (ret != 0) {
544 return LDB_ERR_OPERATIONS_ERROR;
546 if (handle->status != LDB_SUCCESS) {
547 return handle->status;
550 return handle->status;
553 return LDB_SUCCESS;
556 /* set the specified timeout or, if timeout is 0 set the default timeout */
557 int ldb_set_timeout(struct ldb_context *ldb,
558 struct ldb_request *req,
559 int timeout)
561 if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
563 if (timeout != 0) {
564 req->timeout = timeout;
565 } else {
566 req->timeout = ldb->default_timeout;
568 req->starttime = time(NULL);
570 return LDB_SUCCESS;
573 /* calculates the new timeout based on the previous starttime and timeout */
574 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb,
575 struct ldb_request *oldreq,
576 struct ldb_request *newreq)
578 if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
580 if (oldreq == NULL) {
581 return ldb_set_timeout(ldb, newreq, 0);
584 newreq->starttime = oldreq->starttime;
585 newreq->timeout = oldreq->timeout;
587 return LDB_SUCCESS;
592 set the permissions for new files to be passed to open() in
593 backends that use local files
595 void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
597 ldb->create_perms = perms;
600 unsigned int ldb_get_create_perms(struct ldb_context *ldb)
602 return ldb->create_perms;
605 void ldb_set_event_context(struct ldb_context *ldb, struct tevent_context *ev)
607 ldb->ev_ctx = ev;
610 struct tevent_context * ldb_get_event_context(struct ldb_context *ldb)
612 return ldb->ev_ctx;
615 void ldb_request_set_state(struct ldb_request *req, int state)
617 req->handle->state = state;
620 int ldb_request_get_status(struct ldb_request *req)
622 return req->handle->status;
626 start an ldb request
627 NOTE: the request must be a talloc context.
628 returns LDB_ERR_* on errors.
630 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
632 struct ldb_module *module;
633 int ret;
635 if (req->callback == NULL) {
636 ldb_set_errstring(ldb, "Requests MUST define callbacks");
637 return LDB_ERR_UNWILLING_TO_PERFORM;
640 ldb_reset_err_string(ldb);
642 /* call the first module in the chain */
643 switch (req->operation) {
644 case LDB_SEARCH:
645 FIRST_OP(ldb, search);
646 ret = module->ops->search(module, req);
647 break;
648 case LDB_ADD:
649 FIRST_OP(ldb, add);
650 ret = module->ops->add(module, req);
651 break;
652 case LDB_MODIFY:
653 FIRST_OP(ldb, modify);
654 ret = module->ops->modify(module, req);
655 break;
656 case LDB_DELETE:
657 FIRST_OP(ldb, del);
658 ret = module->ops->del(module, req);
659 break;
660 case LDB_RENAME:
661 FIRST_OP(ldb, rename);
662 ret = module->ops->rename(module, req);
663 break;
664 case LDB_EXTENDED:
665 FIRST_OP(ldb, extended);
666 ret = module->ops->extended(module, req);
667 break;
668 default:
669 FIRST_OP(ldb, request);
670 ret = module->ops->request(module, req);
671 break;
674 return ret;
677 int ldb_request_done(struct ldb_request *req, int status)
679 req->handle->state = LDB_ASYNC_DONE;
680 req->handle->status = status;
681 return status;
685 search the database given a LDAP-like search expression
687 returns an LDB error code
689 Use talloc_free to free the ldb_message returned in 'res', if successful
692 int ldb_search_default_callback(struct ldb_request *req,
693 struct ldb_reply *ares)
695 struct ldb_result *res;
696 int n;
698 res = talloc_get_type(req->context, struct ldb_result);
700 if (!ares) {
701 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
703 if (ares->error != LDB_SUCCESS) {
704 return ldb_request_done(req, ares->error);
707 switch (ares->type) {
708 case LDB_REPLY_ENTRY:
709 res->msgs = talloc_realloc(res, res->msgs,
710 struct ldb_message *, res->count + 2);
711 if (! res->msgs) {
712 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
715 res->msgs[res->count + 1] = NULL;
717 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
718 res->count++;
719 break;
721 case LDB_REPLY_REFERRAL:
722 if (res->refs) {
723 for (n = 0; res->refs[n]; n++) /*noop*/ ;
724 } else {
725 n = 0;
728 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
729 if (! res->refs) {
730 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
733 res->refs[n] = talloc_move(res->refs, &ares->referral);
734 res->refs[n + 1] = NULL;
735 break;
737 case LDB_REPLY_DONE:
738 /* TODO: we should really support controls on entries
739 * and referrals too! */
740 res->controls = talloc_move(res, &ares->controls);
742 /* this is the last message, and means the request is done */
743 /* we have to signal and eventual ldb_wait() waiting that the
744 * async request operation was completed */
745 talloc_free(ares);
746 return ldb_request_done(req, LDB_SUCCESS);
749 talloc_free(ares);
751 return LDB_SUCCESS;
754 int ldb_op_default_callback(struct ldb_request *req, struct ldb_reply *ares)
756 int ret;
758 if (!ares) {
759 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
762 if (ares->error != LDB_SUCCESS) {
763 ret = ares->error;
764 talloc_free(ares);
765 return ldb_request_done(req, ret);
768 if (ares->type != LDB_REPLY_DONE) {
769 talloc_free(ares);
770 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
771 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
774 talloc_free(ares);
775 return ldb_request_done(req, LDB_SUCCESS);
778 int ldb_build_search_req_ex(struct ldb_request **ret_req,
779 struct ldb_context *ldb,
780 void *mem_ctx,
781 struct ldb_dn *base,
782 enum ldb_scope scope,
783 struct ldb_parse_tree *tree,
784 const char * const *attrs,
785 struct ldb_control **controls,
786 void *context,
787 ldb_request_callback_t callback,
788 struct ldb_request *parent)
790 struct ldb_request *req;
792 *ret_req = NULL;
794 req = talloc(mem_ctx, struct ldb_request);
795 if (req == NULL) {
796 ldb_oom(ldb);
797 return LDB_ERR_OPERATIONS_ERROR;
800 req->operation = LDB_SEARCH;
801 if (base == NULL) {
802 req->op.search.base = ldb_dn_new(req, ldb, NULL);
803 } else {
804 req->op.search.base = base;
806 req->op.search.scope = scope;
808 req->op.search.tree = tree;
809 if (req->op.search.tree == NULL) {
810 ldb_set_errstring(ldb, "'tree' can't be NULL");
811 talloc_free(req);
812 return LDB_ERR_OPERATIONS_ERROR;
815 req->op.search.attrs = attrs;
816 req->controls = controls;
817 req->context = context;
818 req->callback = callback;
820 ldb_set_timeout_from_prev_req(ldb, parent, req);
822 req->handle = ldb_handle_new(req, ldb);
823 if (req->handle == NULL) {
824 ldb_oom(ldb);
825 return LDB_ERR_OPERATIONS_ERROR;
828 *ret_req = req;
829 return LDB_SUCCESS;
832 int ldb_build_search_req(struct ldb_request **ret_req,
833 struct ldb_context *ldb,
834 void *mem_ctx,
835 struct ldb_dn *base,
836 enum ldb_scope scope,
837 const char *expression,
838 const char * const *attrs,
839 struct ldb_control **controls,
840 void *context,
841 ldb_request_callback_t callback,
842 struct ldb_request *parent)
844 struct ldb_parse_tree *tree;
845 int ret;
847 tree = ldb_parse_tree(mem_ctx, expression);
848 if (tree == NULL) {
849 ldb_set_errstring(ldb, "Unable to parse search expression");
850 return LDB_ERR_OPERATIONS_ERROR;
853 ret = ldb_build_search_req_ex(ret_req, ldb, mem_ctx, base,
854 scope, tree, attrs, controls,
855 context, callback, parent);
856 if (ret == LDB_SUCCESS) {
857 talloc_steal(*ret_req, tree);
859 return ret;
862 int ldb_build_add_req(struct ldb_request **ret_req,
863 struct ldb_context *ldb,
864 void *mem_ctx,
865 const struct ldb_message *message,
866 struct ldb_control **controls,
867 void *context,
868 ldb_request_callback_t callback,
869 struct ldb_request *parent)
871 struct ldb_request *req;
873 *ret_req = NULL;
875 req = talloc(mem_ctx, struct ldb_request);
876 if (req == NULL) {
877 ldb_set_errstring(ldb, "Out of Memory");
878 return LDB_ERR_OPERATIONS_ERROR;
881 req->operation = LDB_ADD;
882 req->op.add.message = message;
883 req->controls = controls;
884 req->context = context;
885 req->callback = callback;
887 ldb_set_timeout_from_prev_req(ldb, parent, req);
889 req->handle = ldb_handle_new(req, ldb);
890 if (req->handle == NULL) {
891 ldb_oom(ldb);
892 return LDB_ERR_OPERATIONS_ERROR;
895 *ret_req = req;
897 return LDB_SUCCESS;
900 int ldb_build_mod_req(struct ldb_request **ret_req,
901 struct ldb_context *ldb,
902 void *mem_ctx,
903 const struct ldb_message *message,
904 struct ldb_control **controls,
905 void *context,
906 ldb_request_callback_t callback,
907 struct ldb_request *parent)
909 struct ldb_request *req;
911 *ret_req = NULL;
913 req = talloc(mem_ctx, struct ldb_request);
914 if (req == NULL) {
915 ldb_set_errstring(ldb, "Out of Memory");
916 return LDB_ERR_OPERATIONS_ERROR;
919 req->operation = LDB_MODIFY;
920 req->op.mod.message = message;
921 req->controls = controls;
922 req->context = context;
923 req->callback = callback;
925 ldb_set_timeout_from_prev_req(ldb, parent, req);
927 req->handle = ldb_handle_new(req, ldb);
928 if (req->handle == NULL) {
929 ldb_oom(ldb);
930 return LDB_ERR_OPERATIONS_ERROR;
933 *ret_req = req;
935 return LDB_SUCCESS;
938 int ldb_build_del_req(struct ldb_request **ret_req,
939 struct ldb_context *ldb,
940 void *mem_ctx,
941 struct ldb_dn *dn,
942 struct ldb_control **controls,
943 void *context,
944 ldb_request_callback_t callback,
945 struct ldb_request *parent)
947 struct ldb_request *req;
949 *ret_req = NULL;
951 req = talloc(mem_ctx, struct ldb_request);
952 if (req == NULL) {
953 ldb_set_errstring(ldb, "Out of Memory");
954 return LDB_ERR_OPERATIONS_ERROR;
957 req->operation = LDB_DELETE;
958 req->op.del.dn = dn;
959 req->controls = controls;
960 req->context = context;
961 req->callback = callback;
963 ldb_set_timeout_from_prev_req(ldb, parent, req);
965 req->handle = ldb_handle_new(req, ldb);
966 if (req->handle == NULL) {
967 ldb_oom(ldb);
968 return LDB_ERR_OPERATIONS_ERROR;
971 *ret_req = req;
973 return LDB_SUCCESS;
976 int ldb_build_rename_req(struct ldb_request **ret_req,
977 struct ldb_context *ldb,
978 void *mem_ctx,
979 struct ldb_dn *olddn,
980 struct ldb_dn *newdn,
981 struct ldb_control **controls,
982 void *context,
983 ldb_request_callback_t callback,
984 struct ldb_request *parent)
986 struct ldb_request *req;
988 *ret_req = NULL;
990 req = talloc(mem_ctx, struct ldb_request);
991 if (req == NULL) {
992 ldb_set_errstring(ldb, "Out of Memory");
993 return LDB_ERR_OPERATIONS_ERROR;
996 req->operation = LDB_RENAME;
997 req->op.rename.olddn = olddn;
998 req->op.rename.newdn = newdn;
999 req->controls = controls;
1000 req->context = context;
1001 req->callback = callback;
1003 ldb_set_timeout_from_prev_req(ldb, parent, req);
1005 req->handle = ldb_handle_new(req, ldb);
1006 if (req->handle == NULL) {
1007 ldb_oom(ldb);
1008 return LDB_ERR_OPERATIONS_ERROR;
1011 *ret_req = req;
1013 return LDB_SUCCESS;
1016 int ldb_extended_default_callback(struct ldb_request *req,
1017 struct ldb_reply *ares)
1019 struct ldb_result *res;
1021 res = talloc_get_type(req->context, struct ldb_result);
1023 if (!ares) {
1024 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1026 if (ares->error != LDB_SUCCESS) {
1027 return ldb_request_done(req, ares->error);
1030 if (ares->type == LDB_REPLY_DONE) {
1032 /* TODO: we should really support controls on entries and referrals too! */
1033 res->extended = talloc_move(res, &ares->response);
1034 res->controls = talloc_move(res, &ares->controls);
1036 talloc_free(ares);
1037 return ldb_request_done(req, LDB_SUCCESS);
1040 talloc_free(ares);
1041 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
1042 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1045 int ldb_build_extended_req(struct ldb_request **ret_req,
1046 struct ldb_context *ldb,
1047 void *mem_ctx,
1048 const char *oid,
1049 void *data,
1050 struct ldb_control **controls,
1051 void *context,
1052 ldb_request_callback_t callback,
1053 struct ldb_request *parent)
1055 struct ldb_request *req;
1057 *ret_req = NULL;
1059 req = talloc(mem_ctx, struct ldb_request);
1060 if (req == NULL) {
1061 ldb_set_errstring(ldb, "Out of Memory");
1062 return LDB_ERR_OPERATIONS_ERROR;
1065 req->operation = LDB_EXTENDED;
1066 req->op.extended.oid = oid;
1067 req->op.extended.data = data;
1068 req->controls = controls;
1069 req->context = context;
1070 req->callback = callback;
1072 ldb_set_timeout_from_prev_req(ldb, parent, req);
1074 req->handle = ldb_handle_new(req, ldb);
1075 if (req->handle == NULL) {
1076 ldb_oom(ldb);
1077 return LDB_ERR_OPERATIONS_ERROR;
1080 *ret_req = req;
1082 return LDB_SUCCESS;
1085 int ldb_extended(struct ldb_context *ldb,
1086 const char *oid,
1087 void *data,
1088 struct ldb_result **_res)
1090 struct ldb_request *req;
1091 int ret;
1092 struct ldb_result *res;
1094 *_res = NULL;
1096 res = talloc_zero(ldb, struct ldb_result);
1097 if (!res) {
1098 return LDB_ERR_OPERATIONS_ERROR;
1101 ret = ldb_build_extended_req(&req, ldb, ldb,
1102 oid, data, NULL,
1103 res, ldb_extended_default_callback,
1104 NULL);
1105 if (ret != LDB_SUCCESS) goto done;
1107 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1109 ret = ldb_request(ldb, req);
1111 if (ret == LDB_SUCCESS) {
1112 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1115 talloc_free(req);
1117 done:
1118 if (ret != LDB_SUCCESS) {
1119 talloc_free(res);
1122 *_res = res;
1123 return ret;
1127 note that ldb_search() will automatically replace a NULL 'base' value
1128 with the defaultNamingContext from the rootDSE if available.
1130 int ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1131 struct ldb_result **result, struct ldb_dn *base,
1132 enum ldb_scope scope, const char * const *attrs,
1133 const char *exp_fmt, ...)
1135 struct ldb_request *req;
1136 struct ldb_result *res;
1137 char *expression;
1138 va_list ap;
1139 int ret;
1141 expression = NULL;
1142 *result = NULL;
1143 req = NULL;
1145 res = talloc_zero(mem_ctx, struct ldb_result);
1146 if (!res) {
1147 return LDB_ERR_OPERATIONS_ERROR;
1150 if (exp_fmt) {
1151 va_start(ap, exp_fmt);
1152 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
1153 va_end(ap);
1155 if (!expression) {
1156 talloc_free(res);
1157 return LDB_ERR_OPERATIONS_ERROR;
1161 ret = ldb_build_search_req(&req, ldb, mem_ctx,
1162 base?base:ldb_get_default_basedn(ldb),
1163 scope,
1164 expression,
1165 attrs,
1166 NULL,
1167 res,
1168 ldb_search_default_callback,
1169 NULL);
1171 if (ret != LDB_SUCCESS) goto done;
1173 ret = ldb_request(ldb, req);
1175 if (ret == LDB_SUCCESS) {
1176 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1179 done:
1180 if (ret != LDB_SUCCESS) {
1181 talloc_free(res);
1182 res = NULL;
1185 talloc_free(expression);
1186 talloc_free(req);
1188 *result = res;
1189 return ret;
1193 add a record to the database. Will fail if a record with the given class
1194 and key already exists
1196 int ldb_add(struct ldb_context *ldb,
1197 const struct ldb_message *message)
1199 struct ldb_request *req;
1200 int ret;
1202 ret = ldb_msg_sanity_check(ldb, message);
1203 if (ret != LDB_SUCCESS) {
1204 return ret;
1207 ret = ldb_build_add_req(&req, ldb, ldb,
1208 message,
1209 NULL,
1210 NULL,
1211 ldb_op_default_callback,
1212 NULL);
1214 if (ret != LDB_SUCCESS) return ret;
1216 /* do request and autostart a transaction */
1217 ret = ldb_autotransaction_request(ldb, req);
1219 talloc_free(req);
1220 return ret;
1224 modify the specified attributes of a record
1226 int ldb_modify(struct ldb_context *ldb,
1227 const struct ldb_message *message)
1229 struct ldb_request *req;
1230 int ret;
1232 ret = ldb_msg_sanity_check(ldb, message);
1233 if (ret != LDB_SUCCESS) {
1234 return ret;
1237 ret = ldb_build_mod_req(&req, ldb, ldb,
1238 message,
1239 NULL,
1240 NULL,
1241 ldb_op_default_callback,
1242 NULL);
1244 if (ret != LDB_SUCCESS) return ret;
1246 /* do request and autostart a transaction */
1247 ret = ldb_autotransaction_request(ldb, req);
1249 talloc_free(req);
1250 return ret;
1255 delete a record from the database
1257 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1259 struct ldb_request *req;
1260 int ret;
1262 ret = ldb_build_del_req(&req, ldb, ldb,
1264 NULL,
1265 NULL,
1266 ldb_op_default_callback,
1267 NULL);
1269 if (ret != LDB_SUCCESS) return ret;
1271 /* do request and autostart a transaction */
1272 ret = ldb_autotransaction_request(ldb, req);
1274 talloc_free(req);
1275 return ret;
1279 rename a record in the database
1281 int ldb_rename(struct ldb_context *ldb,
1282 struct ldb_dn *olddn, struct ldb_dn *newdn)
1284 struct ldb_request *req;
1285 int ret;
1287 ret = ldb_build_rename_req(&req, ldb, ldb,
1288 olddn,
1289 newdn,
1290 NULL,
1291 NULL,
1292 ldb_op_default_callback,
1293 NULL);
1295 if (ret != LDB_SUCCESS) return ret;
1297 /* do request and autostart a transaction */
1298 ret = ldb_autotransaction_request(ldb, req);
1300 talloc_free(req);
1301 return ret;
1306 return the global sequence number
1308 int ldb_sequence_number(struct ldb_context *ldb,
1309 enum ldb_sequence_type type, uint64_t *seq_num)
1311 struct ldb_seqnum_request *seq;
1312 struct ldb_seqnum_result *seqr;
1313 struct ldb_result *res;
1314 TALLOC_CTX *tmp_ctx;
1315 int ret;
1317 *seq_num = 0;
1319 tmp_ctx = talloc_zero(ldb, struct ldb_request);
1320 if (tmp_ctx == NULL) {
1321 ldb_set_errstring(ldb, "Out of Memory");
1322 return LDB_ERR_OPERATIONS_ERROR;
1324 seq = talloc_zero(tmp_ctx, struct ldb_seqnum_request);
1325 if (seq == NULL) {
1326 ldb_set_errstring(ldb, "Out of Memory");
1327 ret = LDB_ERR_OPERATIONS_ERROR;
1328 goto done;
1330 seq->type = type;
1332 ret = ldb_extended(ldb, LDB_EXTENDED_SEQUENCE_NUMBER, seq, &res);
1333 if (ret != LDB_SUCCESS) {
1334 goto done;
1336 talloc_steal(tmp_ctx, res);
1338 if (strcmp(LDB_EXTENDED_SEQUENCE_NUMBER, res->extended->oid) != 0) {
1339 ldb_set_errstring(ldb, "Invalid OID in reply");
1340 ret = LDB_ERR_OPERATIONS_ERROR;
1341 goto done;
1343 seqr = talloc_get_type(res->extended->data,
1344 struct ldb_seqnum_result);
1345 *seq_num = seqr->seq_num;
1347 done:
1348 talloc_free(tmp_ctx);
1349 return ret;
1353 return extended error information
1355 const char *ldb_errstring(struct ldb_context *ldb)
1357 if (ldb->err_string) {
1358 return ldb->err_string;
1361 return NULL;
1365 return a string explaining what a ldb error constant meancs
1367 const char *ldb_strerror(int ldb_err)
1369 switch (ldb_err) {
1370 case LDB_SUCCESS:
1371 return "Success";
1372 case LDB_ERR_OPERATIONS_ERROR:
1373 return "Operations error";
1374 case LDB_ERR_PROTOCOL_ERROR:
1375 return "Protocol error";
1376 case LDB_ERR_TIME_LIMIT_EXCEEDED:
1377 return "Time limit exceeded";
1378 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1379 return "Size limit exceeded";
1380 case LDB_ERR_COMPARE_FALSE:
1381 return "Compare false";
1382 case LDB_ERR_COMPARE_TRUE:
1383 return "Compare true";
1384 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1385 return "Auth method not supported";
1386 case LDB_ERR_STRONG_AUTH_REQUIRED:
1387 return "Strong auth required";
1388 /* 9 RESERVED */
1389 case LDB_ERR_REFERRAL:
1390 return "Referral error";
1391 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1392 return "Admin limit exceeded";
1393 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1394 return "Unsupported critical extension";
1395 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1396 return "Confidentiality required";
1397 case LDB_ERR_SASL_BIND_IN_PROGRESS:
1398 return "SASL bind in progress";
1399 case LDB_ERR_NO_SUCH_ATTRIBUTE:
1400 return "No such attribute";
1401 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1402 return "Undefined attribute type";
1403 case LDB_ERR_INAPPROPRIATE_MATCHING:
1404 return "Inappropriate matching";
1405 case LDB_ERR_CONSTRAINT_VIOLATION:
1406 return "Constraint violation";
1407 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1408 return "Attribute or value exists";
1409 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1410 return "Invalid attribute syntax";
1411 /* 22-31 unused */
1412 case LDB_ERR_NO_SUCH_OBJECT:
1413 return "No such object";
1414 case LDB_ERR_ALIAS_PROBLEM:
1415 return "Alias problem";
1416 case LDB_ERR_INVALID_DN_SYNTAX:
1417 return "Invalid DN syntax";
1418 /* 35 RESERVED */
1419 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1420 return "Alias dereferencing problem";
1421 /* 37-47 unused */
1422 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1423 return "Inappropriate authentication";
1424 case LDB_ERR_INVALID_CREDENTIALS:
1425 return "Invalid credentials";
1426 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1427 return "insufficient access rights";
1428 case LDB_ERR_BUSY:
1429 return "Busy";
1430 case LDB_ERR_UNAVAILABLE:
1431 return "Unavailable";
1432 case LDB_ERR_UNWILLING_TO_PERFORM:
1433 return "Unwilling to perform";
1434 case LDB_ERR_LOOP_DETECT:
1435 return "Loop detect";
1436 /* 55-63 unused */
1437 case LDB_ERR_NAMING_VIOLATION:
1438 return "Naming violation";
1439 case LDB_ERR_OBJECT_CLASS_VIOLATION:
1440 return "Object class violation";
1441 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1442 return "Not allowed on non-leaf";
1443 case LDB_ERR_NOT_ALLOWED_ON_RDN:
1444 return "Not allowed on RDN";
1445 case LDB_ERR_ENTRY_ALREADY_EXISTS:
1446 return "Entry already exists";
1447 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1448 return "Object class mods prohibited";
1449 /* 70 RESERVED FOR CLDAP */
1450 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1451 return "Affects multiple DSAs";
1452 /* 72-79 unused */
1453 case LDB_ERR_OTHER:
1454 return "Other";
1457 return "Unknown error";
1461 set backend specific opaque parameters
1463 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1465 struct ldb_opaque *o;
1467 /* allow updating an existing value */
1468 for (o=ldb->opaque;o;o=o->next) {
1469 if (strcmp(o->name, name) == 0) {
1470 o->value = value;
1471 return LDB_SUCCESS;
1475 o = talloc(ldb, struct ldb_opaque);
1476 if (o == NULL) {
1477 ldb_oom(ldb);
1478 return LDB_ERR_OTHER;
1480 o->next = ldb->opaque;
1481 o->name = name;
1482 o->value = value;
1483 ldb->opaque = o;
1484 return LDB_SUCCESS;
1488 get a previously set opaque value
1490 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1492 struct ldb_opaque *o;
1493 for (o=ldb->opaque;o;o=o->next) {
1494 if (strcmp(o->name, name) == 0) {
1495 return o->value;
1498 return NULL;
1501 int ldb_global_init(void)
1503 /* Provided for compatibility with some older versions of ldb */
1504 return 0;
1507 /* return the ldb flags */
1508 unsigned int ldb_get_flags(struct ldb_context *ldb)
1510 return ldb->flags;