s4-ldb: bit prettier output
[Samba/gbeck.git] / source4 / lib / ldb / common / ldb.c
blob02298c1dfff5010366a49297e5f82b4ce13fd0f4
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] on %s",
45 ldb, (const char *)ldb_get_opaque(ldb, "ldb_url"));
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;
627 trace a ldb request
629 static void ldb_trace_request(struct ldb_context *ldb, struct ldb_request *req)
631 TALLOC_CTX *tmp_ctx = talloc_new(req);
632 int i;
634 switch (req->operation) {
635 case LDB_SEARCH:
636 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: SEARCH");
637 ldb_debug(ldb, LDB_DEBUG_TRACE, " dn: %s",
638 ldb_dn_get_linearized(req->op.search.base));
639 ldb_debug(ldb, LDB_DEBUG_TRACE, " scope: %s",
640 req->op.search.scope==LDB_SCOPE_BASE?"base":
641 req->op.search.scope==LDB_SCOPE_ONELEVEL?"one":
642 req->op.search.scope==LDB_SCOPE_SUBTREE?"sub":"UNKNOWN");
643 ldb_debug(ldb, LDB_DEBUG_TRACE, " expr: %s",
644 ldb_filter_from_tree(tmp_ctx, req->op.search.tree));
645 if (req->op.search.attrs == NULL) {
646 ldb_debug(ldb, LDB_DEBUG_TRACE, " attr: <ALL>");
647 } else {
648 for (i=0; req->op.search.attrs[i]; i++) {
649 ldb_debug(ldb, LDB_DEBUG_TRACE, " attr: %s", req->op.search.attrs[i]);
652 break;
653 case LDB_DELETE:
654 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: DELETE");
655 ldb_debug(ldb, LDB_DEBUG_TRACE, " dn: %s",
656 ldb_dn_get_linearized(req->op.del.dn));
657 break;
658 case LDB_RENAME:
659 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: RENAME");
660 ldb_debug(ldb, LDB_DEBUG_TRACE, " olddn: %s",
661 ldb_dn_get_linearized(req->op.rename.olddn));
662 ldb_debug(ldb, LDB_DEBUG_TRACE, " newdn: %s",
663 ldb_dn_get_linearized(req->op.rename.newdn));
664 break;
665 case LDB_EXTENDED:
666 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: EXTENDED");
667 ldb_debug(ldb, LDB_DEBUG_TRACE, " oid: %s", req->op.extended.oid);
668 ldb_debug(ldb, LDB_DEBUG_TRACE, " data: %s", req->op.extended.data?"yes":"no");
669 break;
670 case LDB_ADD:
671 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: ADD");
672 ldb_debug(req->handle->ldb, LDB_DEBUG_TRACE, "%s",
673 ldb_ldif_message_string(req->handle->ldb, tmp_ctx,
674 LDB_CHANGETYPE_ADD, req->op.add.message));
675 break;
676 case LDB_MODIFY:
677 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: MODIFY");
678 ldb_debug(req->handle->ldb, LDB_DEBUG_TRACE, "%s",
679 ldb_ldif_message_string(req->handle->ldb, tmp_ctx,
680 LDB_CHANGETYPE_ADD, req->op.mod.message));
681 break;
682 case LDB_REQ_REGISTER_CONTROL:
683 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: REGISTER_CONTROL");
684 ldb_debug(req->handle->ldb, LDB_DEBUG_TRACE, "%s",
685 req->op.reg_control.oid);
686 break;
687 case LDB_REQ_REGISTER_PARTITION:
688 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: REGISTER_PARTITION");
689 ldb_debug(req->handle->ldb, LDB_DEBUG_TRACE, "%s",
690 ldb_dn_get_linearized(req->op.reg_partition.dn));
691 break;
692 default:
693 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: UNKNOWN(%u)",
694 req->operation);
695 break;
698 if (req->controls == NULL) {
699 ldb_debug(ldb, LDB_DEBUG_TRACE, " control: <NONE>");
700 } else {
701 for (i=0; req->controls && req->controls[i]; i++) {
702 ldb_debug(ldb, LDB_DEBUG_TRACE, " control: %s crit:%u data:%s",
703 req->controls[i]->oid,
704 req->controls[i]->critical,
705 req->controls[i]->data?"yes":"no");
709 talloc_free(tmp_ctx);
714 start an ldb request
715 NOTE: the request must be a talloc context.
716 returns LDB_ERR_* on errors.
718 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
720 struct ldb_module *module;
721 int ret;
723 if (req->callback == NULL) {
724 ldb_set_errstring(ldb, "Requests MUST define callbacks");
725 return LDB_ERR_UNWILLING_TO_PERFORM;
728 ldb_reset_err_string(ldb);
730 if (ldb->flags & LDB_FLG_ENABLE_TRACING) {
731 ldb_trace_request(ldb, req);
734 /* call the first module in the chain */
735 switch (req->operation) {
736 case LDB_SEARCH:
737 FIRST_OP(ldb, search);
738 ret = module->ops->search(module, req);
739 break;
740 case LDB_ADD:
741 FIRST_OP(ldb, add);
742 ret = module->ops->add(module, req);
743 break;
744 case LDB_MODIFY:
745 FIRST_OP(ldb, modify);
746 ret = module->ops->modify(module, req);
747 break;
748 case LDB_DELETE:
749 FIRST_OP(ldb, del);
750 ret = module->ops->del(module, req);
751 break;
752 case LDB_RENAME:
753 FIRST_OP(ldb, rename);
754 ret = module->ops->rename(module, req);
755 break;
756 case LDB_EXTENDED:
757 FIRST_OP(ldb, extended);
758 ret = module->ops->extended(module, req);
759 break;
760 default:
761 FIRST_OP(ldb, request);
762 ret = module->ops->request(module, req);
763 break;
766 return ret;
769 int ldb_request_done(struct ldb_request *req, int status)
771 req->handle->state = LDB_ASYNC_DONE;
772 req->handle->status = status;
773 return status;
777 search the database given a LDAP-like search expression
779 returns an LDB error code
781 Use talloc_free to free the ldb_message returned in 'res', if successful
784 int ldb_search_default_callback(struct ldb_request *req,
785 struct ldb_reply *ares)
787 struct ldb_result *res;
788 int n;
790 res = talloc_get_type(req->context, struct ldb_result);
792 if (!ares) {
793 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
795 if (ares->error != LDB_SUCCESS) {
796 return ldb_request_done(req, ares->error);
799 switch (ares->type) {
800 case LDB_REPLY_ENTRY:
801 res->msgs = talloc_realloc(res, res->msgs,
802 struct ldb_message *, res->count + 2);
803 if (! res->msgs) {
804 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
807 res->msgs[res->count + 1] = NULL;
809 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
810 res->count++;
811 break;
813 case LDB_REPLY_REFERRAL:
814 if (res->refs) {
815 for (n = 0; res->refs[n]; n++) /*noop*/ ;
816 } else {
817 n = 0;
820 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
821 if (! res->refs) {
822 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
825 res->refs[n] = talloc_move(res->refs, &ares->referral);
826 res->refs[n + 1] = NULL;
827 break;
829 case LDB_REPLY_DONE:
830 /* TODO: we should really support controls on entries
831 * and referrals too! */
832 res->controls = talloc_move(res, &ares->controls);
834 /* this is the last message, and means the request is done */
835 /* we have to signal and eventual ldb_wait() waiting that the
836 * async request operation was completed */
837 talloc_free(ares);
838 return ldb_request_done(req, LDB_SUCCESS);
841 talloc_free(ares);
843 return LDB_SUCCESS;
846 int ldb_op_default_callback(struct ldb_request *req, struct ldb_reply *ares)
848 int ret;
850 if (!ares) {
851 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
854 if (ares->error != LDB_SUCCESS) {
855 ret = ares->error;
856 talloc_free(ares);
857 return ldb_request_done(req, ret);
860 if (ares->type != LDB_REPLY_DONE) {
861 talloc_free(ares);
862 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
863 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
866 talloc_free(ares);
867 return ldb_request_done(req, LDB_SUCCESS);
870 int ldb_build_search_req_ex(struct ldb_request **ret_req,
871 struct ldb_context *ldb,
872 void *mem_ctx,
873 struct ldb_dn *base,
874 enum ldb_scope scope,
875 struct ldb_parse_tree *tree,
876 const char * const *attrs,
877 struct ldb_control **controls,
878 void *context,
879 ldb_request_callback_t callback,
880 struct ldb_request *parent)
882 struct ldb_request *req;
884 *ret_req = NULL;
886 req = talloc(mem_ctx, struct ldb_request);
887 if (req == NULL) {
888 ldb_oom(ldb);
889 return LDB_ERR_OPERATIONS_ERROR;
892 req->operation = LDB_SEARCH;
893 if (base == NULL) {
894 req->op.search.base = ldb_dn_new(req, ldb, NULL);
895 } else {
896 req->op.search.base = base;
898 req->op.search.scope = scope;
900 req->op.search.tree = tree;
901 if (req->op.search.tree == NULL) {
902 ldb_set_errstring(ldb, "'tree' can't be NULL");
903 talloc_free(req);
904 return LDB_ERR_OPERATIONS_ERROR;
907 req->op.search.attrs = attrs;
908 req->controls = controls;
909 req->context = context;
910 req->callback = callback;
912 ldb_set_timeout_from_prev_req(ldb, parent, req);
914 req->handle = ldb_handle_new(req, ldb);
915 if (req->handle == NULL) {
916 ldb_oom(ldb);
917 return LDB_ERR_OPERATIONS_ERROR;
920 *ret_req = req;
921 return LDB_SUCCESS;
924 int ldb_build_search_req(struct ldb_request **ret_req,
925 struct ldb_context *ldb,
926 void *mem_ctx,
927 struct ldb_dn *base,
928 enum ldb_scope scope,
929 const char *expression,
930 const char * const *attrs,
931 struct ldb_control **controls,
932 void *context,
933 ldb_request_callback_t callback,
934 struct ldb_request *parent)
936 struct ldb_parse_tree *tree;
937 int ret;
939 tree = ldb_parse_tree(mem_ctx, expression);
940 if (tree == NULL) {
941 ldb_set_errstring(ldb, "Unable to parse search expression");
942 return LDB_ERR_OPERATIONS_ERROR;
945 ret = ldb_build_search_req_ex(ret_req, ldb, mem_ctx, base,
946 scope, tree, attrs, controls,
947 context, callback, parent);
948 if (ret == LDB_SUCCESS) {
949 talloc_steal(*ret_req, tree);
951 return ret;
954 int ldb_build_add_req(struct ldb_request **ret_req,
955 struct ldb_context *ldb,
956 void *mem_ctx,
957 const struct ldb_message *message,
958 struct ldb_control **controls,
959 void *context,
960 ldb_request_callback_t callback,
961 struct ldb_request *parent)
963 struct ldb_request *req;
965 *ret_req = NULL;
967 req = talloc(mem_ctx, struct ldb_request);
968 if (req == NULL) {
969 ldb_set_errstring(ldb, "Out of Memory");
970 return LDB_ERR_OPERATIONS_ERROR;
973 req->operation = LDB_ADD;
974 req->op.add.message = message;
975 req->controls = controls;
976 req->context = context;
977 req->callback = callback;
979 ldb_set_timeout_from_prev_req(ldb, parent, req);
981 req->handle = ldb_handle_new(req, ldb);
982 if (req->handle == NULL) {
983 ldb_oom(ldb);
984 return LDB_ERR_OPERATIONS_ERROR;
987 *ret_req = req;
989 return LDB_SUCCESS;
992 int ldb_build_mod_req(struct ldb_request **ret_req,
993 struct ldb_context *ldb,
994 void *mem_ctx,
995 const struct ldb_message *message,
996 struct ldb_control **controls,
997 void *context,
998 ldb_request_callback_t callback,
999 struct ldb_request *parent)
1001 struct ldb_request *req;
1003 *ret_req = NULL;
1005 req = talloc(mem_ctx, struct ldb_request);
1006 if (req == NULL) {
1007 ldb_set_errstring(ldb, "Out of Memory");
1008 return LDB_ERR_OPERATIONS_ERROR;
1011 req->operation = LDB_MODIFY;
1012 req->op.mod.message = message;
1013 req->controls = controls;
1014 req->context = context;
1015 req->callback = callback;
1017 ldb_set_timeout_from_prev_req(ldb, parent, req);
1019 req->handle = ldb_handle_new(req, ldb);
1020 if (req->handle == NULL) {
1021 ldb_oom(ldb);
1022 return LDB_ERR_OPERATIONS_ERROR;
1025 *ret_req = req;
1027 return LDB_SUCCESS;
1030 int ldb_build_del_req(struct ldb_request **ret_req,
1031 struct ldb_context *ldb,
1032 void *mem_ctx,
1033 struct ldb_dn *dn,
1034 struct ldb_control **controls,
1035 void *context,
1036 ldb_request_callback_t callback,
1037 struct ldb_request *parent)
1039 struct ldb_request *req;
1041 *ret_req = NULL;
1043 req = talloc(mem_ctx, struct ldb_request);
1044 if (req == NULL) {
1045 ldb_set_errstring(ldb, "Out of Memory");
1046 return LDB_ERR_OPERATIONS_ERROR;
1049 req->operation = LDB_DELETE;
1050 req->op.del.dn = dn;
1051 req->controls = controls;
1052 req->context = context;
1053 req->callback = callback;
1055 ldb_set_timeout_from_prev_req(ldb, parent, req);
1057 req->handle = ldb_handle_new(req, ldb);
1058 if (req->handle == NULL) {
1059 ldb_oom(ldb);
1060 return LDB_ERR_OPERATIONS_ERROR;
1063 *ret_req = req;
1065 return LDB_SUCCESS;
1068 int ldb_build_rename_req(struct ldb_request **ret_req,
1069 struct ldb_context *ldb,
1070 void *mem_ctx,
1071 struct ldb_dn *olddn,
1072 struct ldb_dn *newdn,
1073 struct ldb_control **controls,
1074 void *context,
1075 ldb_request_callback_t callback,
1076 struct ldb_request *parent)
1078 struct ldb_request *req;
1080 *ret_req = NULL;
1082 req = talloc(mem_ctx, struct ldb_request);
1083 if (req == NULL) {
1084 ldb_set_errstring(ldb, "Out of Memory");
1085 return LDB_ERR_OPERATIONS_ERROR;
1088 req->operation = LDB_RENAME;
1089 req->op.rename.olddn = olddn;
1090 req->op.rename.newdn = newdn;
1091 req->controls = controls;
1092 req->context = context;
1093 req->callback = callback;
1095 ldb_set_timeout_from_prev_req(ldb, parent, req);
1097 req->handle = ldb_handle_new(req, ldb);
1098 if (req->handle == NULL) {
1099 ldb_oom(ldb);
1100 return LDB_ERR_OPERATIONS_ERROR;
1103 *ret_req = req;
1105 return LDB_SUCCESS;
1108 int ldb_extended_default_callback(struct ldb_request *req,
1109 struct ldb_reply *ares)
1111 struct ldb_result *res;
1113 res = talloc_get_type(req->context, struct ldb_result);
1115 if (!ares) {
1116 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1118 if (ares->error != LDB_SUCCESS) {
1119 return ldb_request_done(req, ares->error);
1122 if (ares->type == LDB_REPLY_DONE) {
1124 /* TODO: we should really support controls on entries and referrals too! */
1125 res->extended = talloc_move(res, &ares->response);
1126 res->controls = talloc_move(res, &ares->controls);
1128 talloc_free(ares);
1129 return ldb_request_done(req, LDB_SUCCESS);
1132 talloc_free(ares);
1133 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
1134 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1137 int ldb_build_extended_req(struct ldb_request **ret_req,
1138 struct ldb_context *ldb,
1139 void *mem_ctx,
1140 const char *oid,
1141 void *data,
1142 struct ldb_control **controls,
1143 void *context,
1144 ldb_request_callback_t callback,
1145 struct ldb_request *parent)
1147 struct ldb_request *req;
1149 *ret_req = NULL;
1151 req = talloc(mem_ctx, struct ldb_request);
1152 if (req == NULL) {
1153 ldb_set_errstring(ldb, "Out of Memory");
1154 return LDB_ERR_OPERATIONS_ERROR;
1157 req->operation = LDB_EXTENDED;
1158 req->op.extended.oid = oid;
1159 req->op.extended.data = data;
1160 req->controls = controls;
1161 req->context = context;
1162 req->callback = callback;
1164 ldb_set_timeout_from_prev_req(ldb, parent, req);
1166 req->handle = ldb_handle_new(req, ldb);
1167 if (req->handle == NULL) {
1168 ldb_oom(ldb);
1169 return LDB_ERR_OPERATIONS_ERROR;
1172 *ret_req = req;
1174 return LDB_SUCCESS;
1177 int ldb_extended(struct ldb_context *ldb,
1178 const char *oid,
1179 void *data,
1180 struct ldb_result **_res)
1182 struct ldb_request *req;
1183 int ret;
1184 struct ldb_result *res;
1186 *_res = NULL;
1188 res = talloc_zero(ldb, struct ldb_result);
1189 if (!res) {
1190 return LDB_ERR_OPERATIONS_ERROR;
1193 ret = ldb_build_extended_req(&req, ldb, ldb,
1194 oid, data, NULL,
1195 res, ldb_extended_default_callback,
1196 NULL);
1197 if (ret != LDB_SUCCESS) goto done;
1199 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1201 ret = ldb_request(ldb, req);
1203 if (ret == LDB_SUCCESS) {
1204 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1207 talloc_free(req);
1209 done:
1210 if (ret != LDB_SUCCESS) {
1211 talloc_free(res);
1214 *_res = res;
1215 return ret;
1219 note that ldb_search() will automatically replace a NULL 'base' value
1220 with the defaultNamingContext from the rootDSE if available.
1222 int ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1223 struct ldb_result **result, struct ldb_dn *base,
1224 enum ldb_scope scope, const char * const *attrs,
1225 const char *exp_fmt, ...)
1227 struct ldb_request *req;
1228 struct ldb_result *res;
1229 char *expression;
1230 va_list ap;
1231 int ret;
1233 expression = NULL;
1234 *result = NULL;
1235 req = NULL;
1237 res = talloc_zero(mem_ctx, struct ldb_result);
1238 if (!res) {
1239 return LDB_ERR_OPERATIONS_ERROR;
1242 if (exp_fmt) {
1243 va_start(ap, exp_fmt);
1244 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
1245 va_end(ap);
1247 if (!expression) {
1248 talloc_free(res);
1249 return LDB_ERR_OPERATIONS_ERROR;
1253 ret = ldb_build_search_req(&req, ldb, mem_ctx,
1254 base?base:ldb_get_default_basedn(ldb),
1255 scope,
1256 expression,
1257 attrs,
1258 NULL,
1259 res,
1260 ldb_search_default_callback,
1261 NULL);
1263 if (ret != LDB_SUCCESS) goto done;
1265 ret = ldb_request(ldb, req);
1267 if (ret == LDB_SUCCESS) {
1268 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1271 done:
1272 if (ret != LDB_SUCCESS) {
1273 talloc_free(res);
1274 res = NULL;
1277 talloc_free(expression);
1278 talloc_free(req);
1280 *result = res;
1281 return ret;
1285 add a record to the database. Will fail if a record with the given class
1286 and key already exists
1288 int ldb_add(struct ldb_context *ldb,
1289 const struct ldb_message *message)
1291 struct ldb_request *req;
1292 int ret;
1294 ret = ldb_msg_sanity_check(ldb, message);
1295 if (ret != LDB_SUCCESS) {
1296 return ret;
1299 ret = ldb_build_add_req(&req, ldb, ldb,
1300 message,
1301 NULL,
1302 NULL,
1303 ldb_op_default_callback,
1304 NULL);
1306 if (ret != LDB_SUCCESS) return ret;
1308 /* do request and autostart a transaction */
1309 ret = ldb_autotransaction_request(ldb, req);
1311 talloc_free(req);
1312 return ret;
1316 modify the specified attributes of a record
1318 int ldb_modify(struct ldb_context *ldb,
1319 const struct ldb_message *message)
1321 struct ldb_request *req;
1322 int ret;
1324 ret = ldb_msg_sanity_check(ldb, message);
1325 if (ret != LDB_SUCCESS) {
1326 return ret;
1329 ret = ldb_build_mod_req(&req, ldb, ldb,
1330 message,
1331 NULL,
1332 NULL,
1333 ldb_op_default_callback,
1334 NULL);
1336 if (ret != LDB_SUCCESS) return ret;
1338 /* do request and autostart a transaction */
1339 ret = ldb_autotransaction_request(ldb, req);
1341 talloc_free(req);
1342 return ret;
1347 delete a record from the database
1349 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1351 struct ldb_request *req;
1352 int ret;
1354 ret = ldb_build_del_req(&req, ldb, ldb,
1356 NULL,
1357 NULL,
1358 ldb_op_default_callback,
1359 NULL);
1361 if (ret != LDB_SUCCESS) return ret;
1363 /* do request and autostart a transaction */
1364 ret = ldb_autotransaction_request(ldb, req);
1366 talloc_free(req);
1367 return ret;
1371 rename a record in the database
1373 int ldb_rename(struct ldb_context *ldb,
1374 struct ldb_dn *olddn, struct ldb_dn *newdn)
1376 struct ldb_request *req;
1377 int ret;
1379 ret = ldb_build_rename_req(&req, ldb, ldb,
1380 olddn,
1381 newdn,
1382 NULL,
1383 NULL,
1384 ldb_op_default_callback,
1385 NULL);
1387 if (ret != LDB_SUCCESS) return ret;
1389 /* do request and autostart a transaction */
1390 ret = ldb_autotransaction_request(ldb, req);
1392 talloc_free(req);
1393 return ret;
1398 return the global sequence number
1400 int ldb_sequence_number(struct ldb_context *ldb,
1401 enum ldb_sequence_type type, uint64_t *seq_num)
1403 struct ldb_seqnum_request *seq;
1404 struct ldb_seqnum_result *seqr;
1405 struct ldb_result *res;
1406 TALLOC_CTX *tmp_ctx;
1407 int ret;
1409 *seq_num = 0;
1411 tmp_ctx = talloc_zero(ldb, struct ldb_request);
1412 if (tmp_ctx == NULL) {
1413 ldb_set_errstring(ldb, "Out of Memory");
1414 return LDB_ERR_OPERATIONS_ERROR;
1416 seq = talloc_zero(tmp_ctx, struct ldb_seqnum_request);
1417 if (seq == NULL) {
1418 ldb_set_errstring(ldb, "Out of Memory");
1419 ret = LDB_ERR_OPERATIONS_ERROR;
1420 goto done;
1422 seq->type = type;
1424 ret = ldb_extended(ldb, LDB_EXTENDED_SEQUENCE_NUMBER, seq, &res);
1425 if (ret != LDB_SUCCESS) {
1426 goto done;
1428 talloc_steal(tmp_ctx, res);
1430 if (strcmp(LDB_EXTENDED_SEQUENCE_NUMBER, res->extended->oid) != 0) {
1431 ldb_set_errstring(ldb, "Invalid OID in reply");
1432 ret = LDB_ERR_OPERATIONS_ERROR;
1433 goto done;
1435 seqr = talloc_get_type(res->extended->data,
1436 struct ldb_seqnum_result);
1437 *seq_num = seqr->seq_num;
1439 done:
1440 talloc_free(tmp_ctx);
1441 return ret;
1445 return extended error information
1447 const char *ldb_errstring(struct ldb_context *ldb)
1449 if (ldb->err_string) {
1450 return ldb->err_string;
1453 return NULL;
1457 return a string explaining what a ldb error constant meancs
1459 const char *ldb_strerror(int ldb_err)
1461 switch (ldb_err) {
1462 case LDB_SUCCESS:
1463 return "Success";
1464 case LDB_ERR_OPERATIONS_ERROR:
1465 return "Operations error";
1466 case LDB_ERR_PROTOCOL_ERROR:
1467 return "Protocol error";
1468 case LDB_ERR_TIME_LIMIT_EXCEEDED:
1469 return "Time limit exceeded";
1470 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1471 return "Size limit exceeded";
1472 case LDB_ERR_COMPARE_FALSE:
1473 return "Compare false";
1474 case LDB_ERR_COMPARE_TRUE:
1475 return "Compare true";
1476 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1477 return "Auth method not supported";
1478 case LDB_ERR_STRONG_AUTH_REQUIRED:
1479 return "Strong auth required";
1480 /* 9 RESERVED */
1481 case LDB_ERR_REFERRAL:
1482 return "Referral error";
1483 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1484 return "Admin limit exceeded";
1485 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1486 return "Unsupported critical extension";
1487 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1488 return "Confidentiality required";
1489 case LDB_ERR_SASL_BIND_IN_PROGRESS:
1490 return "SASL bind in progress";
1491 case LDB_ERR_NO_SUCH_ATTRIBUTE:
1492 return "No such attribute";
1493 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1494 return "Undefined attribute type";
1495 case LDB_ERR_INAPPROPRIATE_MATCHING:
1496 return "Inappropriate matching";
1497 case LDB_ERR_CONSTRAINT_VIOLATION:
1498 return "Constraint violation";
1499 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1500 return "Attribute or value exists";
1501 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1502 return "Invalid attribute syntax";
1503 /* 22-31 unused */
1504 case LDB_ERR_NO_SUCH_OBJECT:
1505 return "No such object";
1506 case LDB_ERR_ALIAS_PROBLEM:
1507 return "Alias problem";
1508 case LDB_ERR_INVALID_DN_SYNTAX:
1509 return "Invalid DN syntax";
1510 /* 35 RESERVED */
1511 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1512 return "Alias dereferencing problem";
1513 /* 37-47 unused */
1514 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1515 return "Inappropriate authentication";
1516 case LDB_ERR_INVALID_CREDENTIALS:
1517 return "Invalid credentials";
1518 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1519 return "insufficient access rights";
1520 case LDB_ERR_BUSY:
1521 return "Busy";
1522 case LDB_ERR_UNAVAILABLE:
1523 return "Unavailable";
1524 case LDB_ERR_UNWILLING_TO_PERFORM:
1525 return "Unwilling to perform";
1526 case LDB_ERR_LOOP_DETECT:
1527 return "Loop detect";
1528 /* 55-63 unused */
1529 case LDB_ERR_NAMING_VIOLATION:
1530 return "Naming violation";
1531 case LDB_ERR_OBJECT_CLASS_VIOLATION:
1532 return "Object class violation";
1533 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1534 return "Not allowed on non-leaf";
1535 case LDB_ERR_NOT_ALLOWED_ON_RDN:
1536 return "Not allowed on RDN";
1537 case LDB_ERR_ENTRY_ALREADY_EXISTS:
1538 return "Entry already exists";
1539 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1540 return "Object class mods prohibited";
1541 /* 70 RESERVED FOR CLDAP */
1542 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1543 return "Affects multiple DSAs";
1544 /* 72-79 unused */
1545 case LDB_ERR_OTHER:
1546 return "Other";
1549 return "Unknown error";
1553 set backend specific opaque parameters
1555 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1557 struct ldb_opaque *o;
1559 /* allow updating an existing value */
1560 for (o=ldb->opaque;o;o=o->next) {
1561 if (strcmp(o->name, name) == 0) {
1562 o->value = value;
1563 return LDB_SUCCESS;
1567 o = talloc(ldb, struct ldb_opaque);
1568 if (o == NULL) {
1569 ldb_oom(ldb);
1570 return LDB_ERR_OTHER;
1572 o->next = ldb->opaque;
1573 o->name = name;
1574 o->value = value;
1575 ldb->opaque = o;
1576 return LDB_SUCCESS;
1580 get a previously set opaque value
1582 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1584 struct ldb_opaque *o;
1585 for (o=ldb->opaque;o;o=o->next) {
1586 if (strcmp(o->name, name) == 0) {
1587 return o->value;
1590 return NULL;
1593 int ldb_global_init(void)
1595 /* Provided for compatibility with some older versions of ldb */
1596 return 0;
1599 /* return the ldb flags */
1600 unsigned int ldb_get_flags(struct ldb_context *ldb)
1602 return ldb->flags;
1605 /* set the ldb flags */
1606 void ldb_set_flags(struct ldb_context *ldb, unsigned flags)
1608 ldb->flags = flags;