lib/ldb: Do not vasprintf() the tevent debug messages that will not be shown
[Samba/bb.git] / lib / ldb / common / ldb.c
blob887a8967b1638f2fd88a7a0f144b6d98974145cc
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"
37 #include "ldb.h"
39 static int ldb_context_destructor(void *ptr)
41 struct ldb_context *ldb = talloc_get_type(ptr, struct ldb_context);
43 if (ldb->transaction_active) {
44 ldb_debug(ldb, LDB_DEBUG_FATAL,
45 "A transaction is still active in ldb context [%p] on %s",
46 ldb, (const char *)ldb_get_opaque(ldb, "ldb_url"));
49 return 0;
53 this is used to catch debug messages from events
55 static void ldb_tevent_debug(void *context, enum tevent_debug_level level,
56 const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
58 static void ldb_tevent_debug(void *context, enum tevent_debug_level level,
59 const char *fmt, va_list ap)
61 struct ldb_context *ldb = talloc_get_type(context, struct ldb_context);
62 enum ldb_debug_level ldb_level = LDB_DEBUG_FATAL;
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 /* There isn't a tevent: prefix here because to add it means
80 * actually printing the string, and most of the time we don't
81 * want to show it */
82 ldb_vdebug(ldb, ldb_level, fmt, ap);
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;
94 const char *modules_path = getenv("LDB_MODULES_PATH");
96 if (modules_path == NULL) {
97 modules_path = LDB_MODULESDIR;
100 ret = ldb_modules_load(modules_path, LDB_VERSION);
101 if (ret != LDB_SUCCESS) {
102 return NULL;
105 ldb = talloc_zero(mem_ctx, struct ldb_context);
106 if (ldb == NULL) {
107 return NULL;
110 /* A new event context so that callers who don't want ldb
111 * operating on thier global event context can work without
112 * having to provide their own private one explicitly */
113 if (ev_ctx == NULL) {
114 ev_ctx = tevent_context_init(ldb);
115 tevent_set_debug(ev_ctx, ldb_tevent_debug, ldb);
116 tevent_loop_allow_nesting(ev_ctx);
119 ret = ldb_setup_wellknown_attributes(ldb);
120 if (ret != LDB_SUCCESS) {
121 talloc_free(ldb);
122 return NULL;
125 ldb_set_utf8_default(ldb);
126 ldb_set_create_perms(ldb, 0666);
127 ldb_set_modules_dir(ldb, LDB_MODULESDIR);
128 ldb_set_event_context(ldb, ev_ctx);
130 /* TODO: get timeout from options if available there */
131 ldb->default_timeout = 300; /* set default to 5 minutes */
133 talloc_set_destructor((TALLOC_CTX *)ldb, ldb_context_destructor);
135 return ldb;
139 try to autodetect a basedn if none specified. This fixes one of my
140 pet hates about ldapsearch, which is that you have to get a long,
141 complex basedn right to make any use of it.
143 void ldb_set_default_dns(struct ldb_context *ldb)
145 TALLOC_CTX *tmp_ctx;
146 int ret;
147 struct ldb_result *res;
148 struct ldb_dn *tmp_dn=NULL;
149 static const char *attrs[] = {
150 "rootDomainNamingContext",
151 "configurationNamingContext",
152 "schemaNamingContext",
153 "defaultNamingContext",
154 NULL
157 tmp_ctx = talloc_new(ldb);
158 ret = ldb_search(ldb, tmp_ctx, &res, ldb_dn_new(tmp_ctx, ldb, NULL),
159 LDB_SCOPE_BASE, attrs, "(objectClass=*)");
160 if (ret != LDB_SUCCESS) {
161 talloc_free(tmp_ctx);
162 return;
165 if (res->count != 1) {
166 talloc_free(tmp_ctx);
167 return;
170 if (!ldb_get_opaque(ldb, "rootDomainNamingContext")) {
171 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
172 "rootDomainNamingContext");
173 ldb_set_opaque(ldb, "rootDomainNamingContext", tmp_dn);
176 if (!ldb_get_opaque(ldb, "configurationNamingContext")) {
177 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
178 "configurationNamingContext");
179 ldb_set_opaque(ldb, "configurationNamingContext", tmp_dn);
182 if (!ldb_get_opaque(ldb, "schemaNamingContext")) {
183 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
184 "schemaNamingContext");
185 ldb_set_opaque(ldb, "schemaNamingContext", tmp_dn);
188 if (!ldb_get_opaque(ldb, "defaultNamingContext")) {
189 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
190 "defaultNamingContext");
191 ldb_set_opaque(ldb, "defaultNamingContext", tmp_dn);
194 talloc_free(tmp_ctx);
197 struct ldb_dn *ldb_get_root_basedn(struct ldb_context *ldb)
199 void *opaque = ldb_get_opaque(ldb, "rootDomainNamingContext");
200 return talloc_get_type(opaque, struct ldb_dn);
203 struct ldb_dn *ldb_get_config_basedn(struct ldb_context *ldb)
205 void *opaque = ldb_get_opaque(ldb, "configurationNamingContext");
206 return talloc_get_type(opaque, struct ldb_dn);
209 struct ldb_dn *ldb_get_schema_basedn(struct ldb_context *ldb)
211 void *opaque = ldb_get_opaque(ldb, "schemaNamingContext");
212 return talloc_get_type(opaque, struct ldb_dn);
215 struct ldb_dn *ldb_get_default_basedn(struct ldb_context *ldb)
217 void *opaque = ldb_get_opaque(ldb, "defaultNamingContext");
218 return talloc_get_type(opaque, struct ldb_dn);
222 connect to a database. The URL can either be one of the following forms
223 ldb://path
224 ldapi://path
226 flags is made up of LDB_FLG_*
228 the options are passed uninterpreted to the backend, and are
229 backend specific
231 int ldb_connect(struct ldb_context *ldb, const char *url,
232 unsigned int flags, const char *options[])
234 int ret;
235 char *url2;
236 /* We seem to need to do this here, or else some utilities don't
237 * get ldb backends */
239 ldb->flags = flags;
241 url2 = talloc_strdup(ldb, url);
242 if (!url2) {
243 ldb_oom(ldb);
244 return LDB_ERR_OPERATIONS_ERROR;
246 ret = ldb_set_opaque(ldb, "ldb_url", url2);
247 if (ret != LDB_SUCCESS) {
248 return ret;
251 ret = ldb_module_connect_backend(ldb, url, options, &ldb->modules);
252 if (ret != LDB_SUCCESS) {
253 return ret;
256 if (ldb_load_modules(ldb, options) != LDB_SUCCESS) {
257 ldb_debug(ldb, LDB_DEBUG_FATAL,
258 "Unable to load modules for %s: %s",
259 url, ldb_errstring(ldb));
260 return LDB_ERR_OTHER;
263 /* set the default base dn */
264 ldb_set_default_dns(ldb);
266 return LDB_SUCCESS;
269 void ldb_set_errstring(struct ldb_context *ldb, const char *err_string)
271 ldb_asprintf_errstring(ldb, "%s", err_string);
274 void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
276 va_list ap;
278 if (ldb->err_string) {
279 talloc_free(ldb->err_string);
282 va_start(ap, format);
283 ldb->err_string = talloc_vasprintf(ldb, format, ap);
284 va_end(ap);
286 if (ldb->flags & LDB_FLG_ENABLE_TRACING) {
287 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_asprintf/set_errstring: %s",
288 ldb->err_string);
292 void ldb_reset_err_string(struct ldb_context *ldb)
294 if (ldb->err_string) {
295 talloc_free(ldb->err_string);
296 ldb->err_string = NULL;
303 set an ldb error based on file:line
305 int ldb_error_at(struct ldb_context *ldb, int ecode,
306 const char *reason, const char *file, int line)
308 if (reason == NULL) {
309 reason = ldb_strerror(ecode);
311 ldb_asprintf_errstring(ldb, "%s at %s:%d", reason, file, line);
312 return ecode;
316 #define FIRST_OP_NOERR(ldb, op) do { \
317 module = ldb->modules; \
318 while (module && module->ops->op == NULL) module = module->next; \
319 if ((ldb->flags & LDB_FLG_ENABLE_TRACING) && module) { \
320 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: (%s)->" #op, \
321 module->ops->name); \
323 } while (0)
325 #define FIRST_OP(ldb, op) do { \
326 FIRST_OP_NOERR(ldb, op); \
327 if (module == NULL) { \
328 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
329 return LDB_ERR_OPERATIONS_ERROR; \
331 } while (0)
335 start a transaction
337 int ldb_transaction_start(struct ldb_context *ldb)
339 struct ldb_module *module;
340 int status;
342 ldb_debug(ldb, LDB_DEBUG_TRACE,
343 "start ldb transaction (nesting: %d)",
344 ldb->transaction_active);
346 /* explicit transaction active, count nested requests */
347 if (ldb->transaction_active) {
348 ldb->transaction_active++;
349 return LDB_SUCCESS;
352 /* start a new transaction */
353 ldb->transaction_active++;
354 ldb->prepare_commit_done = false;
356 FIRST_OP(ldb, start_transaction);
358 ldb_reset_err_string(ldb);
360 status = module->ops->start_transaction(module);
361 if (status != LDB_SUCCESS) {
362 if (ldb->err_string == NULL) {
363 /* no error string was setup by the backend */
364 ldb_asprintf_errstring(ldb,
365 "ldb transaction start: %s (%d)",
366 ldb_strerror(status),
367 status);
370 if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
371 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "start ldb transaction error: %s",
372 ldb_errstring(module->ldb));
374 return status;
378 prepare for transaction commit (first phase of two phase commit)
380 int ldb_transaction_prepare_commit(struct ldb_context *ldb)
382 struct ldb_module *module;
383 int status;
385 if (ldb->prepare_commit_done) {
386 return LDB_SUCCESS;
389 /* commit only when all nested transactions are complete */
390 if (ldb->transaction_active > 1) {
391 return LDB_SUCCESS;
394 ldb->prepare_commit_done = true;
396 if (ldb->transaction_active < 0) {
397 ldb_debug(ldb, LDB_DEBUG_FATAL,
398 "prepare commit called but no ldb transactions are active!");
399 ldb->transaction_active = 0;
400 return LDB_ERR_OPERATIONS_ERROR;
403 /* call prepare transaction if available */
404 FIRST_OP_NOERR(ldb, prepare_commit);
405 if (module == NULL) {
406 return LDB_SUCCESS;
409 status = module->ops->prepare_commit(module);
410 if (status != LDB_SUCCESS) {
411 /* if a module fails the prepare then we need
412 to call the end transaction for everyone */
413 FIRST_OP(ldb, del_transaction);
414 module->ops->del_transaction(module);
415 if (ldb->err_string == NULL) {
416 /* no error string was setup by the backend */
417 ldb_asprintf_errstring(ldb,
418 "ldb transaction prepare commit: %s (%d)",
419 ldb_strerror(status),
420 status);
422 if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
423 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "prepare commit transaction error: %s",
424 ldb_errstring(module->ldb));
428 return status;
433 commit a transaction
435 int ldb_transaction_commit(struct ldb_context *ldb)
437 struct ldb_module *module;
438 int status;
440 status = ldb_transaction_prepare_commit(ldb);
441 if (status != LDB_SUCCESS) {
442 return status;
445 ldb->transaction_active--;
447 ldb_debug(ldb, LDB_DEBUG_TRACE,
448 "commit ldb transaction (nesting: %d)",
449 ldb->transaction_active);
451 /* commit only when all nested transactions are complete */
452 if (ldb->transaction_active > 0) {
453 return LDB_SUCCESS;
456 if (ldb->transaction_active < 0) {
457 ldb_debug(ldb, LDB_DEBUG_FATAL,
458 "commit called but no ldb transactions are active!");
459 ldb->transaction_active = 0;
460 return LDB_ERR_OPERATIONS_ERROR;
463 ldb_reset_err_string(ldb);
465 FIRST_OP(ldb, end_transaction);
466 status = module->ops->end_transaction(module);
467 if (status != LDB_SUCCESS) {
468 if (ldb->err_string == NULL) {
469 /* no error string was setup by the backend */
470 ldb_asprintf_errstring(ldb,
471 "ldb transaction commit: %s (%d)",
472 ldb_strerror(status),
473 status);
475 if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
476 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "commit ldb transaction error: %s",
477 ldb_errstring(module->ldb));
479 /* cancel the transaction */
480 FIRST_OP(ldb, del_transaction);
481 module->ops->del_transaction(module);
483 return status;
488 cancel a transaction
490 int ldb_transaction_cancel(struct ldb_context *ldb)
492 struct ldb_module *module;
493 int status;
495 ldb->transaction_active--;
497 ldb_debug(ldb, LDB_DEBUG_TRACE,
498 "cancel ldb transaction (nesting: %d)",
499 ldb->transaction_active);
501 /* really cancel only if all nested transactions are complete */
502 if (ldb->transaction_active > 0) {
503 return LDB_SUCCESS;
506 if (ldb->transaction_active < 0) {
507 ldb_debug(ldb, LDB_DEBUG_FATAL,
508 "cancel called but no ldb transactions are active!");
509 ldb->transaction_active = 0;
510 return LDB_ERR_OPERATIONS_ERROR;
513 FIRST_OP(ldb, del_transaction);
515 status = module->ops->del_transaction(module);
516 if (status != LDB_SUCCESS) {
517 if (ldb->err_string == NULL) {
518 /* no error string was setup by the backend */
519 ldb_asprintf_errstring(ldb,
520 "ldb transaction cancel: %s (%d)",
521 ldb_strerror(status),
522 status);
524 if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
525 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "cancel ldb transaction error: %s",
526 ldb_errstring(module->ldb));
529 return status;
533 cancel a transaction with no error if no transaction is pending
534 used when we fork() to clear any parent transactions
536 int ldb_transaction_cancel_noerr(struct ldb_context *ldb)
538 if (ldb->transaction_active > 0) {
539 return ldb_transaction_cancel(ldb);
541 return LDB_SUCCESS;
545 /* autostarts a transaction if none active */
546 static int ldb_autotransaction_request(struct ldb_context *ldb,
547 struct ldb_request *req)
549 int ret;
551 ret = ldb_transaction_start(ldb);
552 if (ret != LDB_SUCCESS) {
553 return ret;
556 ret = ldb_request(ldb, req);
557 if (ret == LDB_SUCCESS) {
558 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
561 if (ret == LDB_SUCCESS) {
562 return ldb_transaction_commit(ldb);
564 ldb_transaction_cancel(ldb);
566 return ret;
569 int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
571 struct tevent_context *ev;
572 int ret;
574 if (!handle) {
575 return ldb_error(handle->ldb, LDB_ERR_UNAVAILABLE, NULL);
578 if (handle->state == LDB_ASYNC_DONE) {
579 if ((handle->status != LDB_SUCCESS) &&
580 (handle->ldb->err_string == NULL)) {
581 /* if no error string was setup by the backend */
582 ldb_asprintf_errstring(handle->ldb, "ldb_wait: %s (%d)",
583 ldb_strerror(handle->status),
584 handle->status);
586 return handle->status;
589 ev = ldb_get_event_context(handle->ldb);
590 if (NULL == ev) {
591 return ldb_oom(handle->ldb);
594 switch (type) {
595 case LDB_WAIT_NONE:
596 ret = tevent_loop_once(ev);
597 if (ret != 0) {
598 return ldb_operr(handle->ldb);
600 if (handle->status != LDB_SUCCESS) {
601 if (handle->ldb->err_string == NULL) {
603 * if no error string was setup by the backend
605 ldb_asprintf_errstring(handle->ldb,
606 "ldb_wait: %s (%d)",
607 ldb_strerror(handle->status),
608 handle->status);
610 return handle->status;
612 break;
614 case LDB_WAIT_ALL:
615 while (handle->state != LDB_ASYNC_DONE) {
616 ret = tevent_loop_once(ev);
617 if (ret != 0) {
618 return ldb_operr(handle->ldb);
620 if (handle->status != LDB_SUCCESS) {
621 if (handle->ldb->err_string == NULL) {
623 * if no error string was setup by the
624 * backend
626 ldb_asprintf_errstring(handle->ldb,
627 "ldb_wait: %s (%d)",
628 ldb_strerror(handle->status),
629 handle->status);
631 return handle->status;
634 if (handle->status != LDB_SUCCESS) {
635 if (handle->ldb->err_string == NULL) {
637 * if no error string was setup by the backend
639 ldb_asprintf_errstring(handle->ldb,
640 "ldb_wait: %s (%d)",
641 ldb_strerror(handle->status),
642 handle->status);
644 return handle->status;
646 break;
649 return LDB_SUCCESS;
652 /* set the specified timeout or, if timeout is 0 set the default timeout */
653 int ldb_set_timeout(struct ldb_context *ldb,
654 struct ldb_request *req,
655 int timeout)
657 if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
659 if (timeout != 0) {
660 req->timeout = timeout;
661 } else {
662 req->timeout = ldb->default_timeout;
664 req->starttime = time(NULL);
666 return LDB_SUCCESS;
669 /* calculates the new timeout based on the previous starttime and timeout */
670 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb,
671 struct ldb_request *oldreq,
672 struct ldb_request *newreq)
674 if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
676 if (oldreq == NULL) {
677 return ldb_set_timeout(ldb, newreq, 0);
680 newreq->starttime = oldreq->starttime;
681 newreq->timeout = oldreq->timeout;
683 return LDB_SUCCESS;
688 set the permissions for new files to be passed to open() in
689 backends that use local files
691 void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
693 ldb->create_perms = perms;
696 unsigned int ldb_get_create_perms(struct ldb_context *ldb)
698 return ldb->create_perms;
701 void ldb_set_event_context(struct ldb_context *ldb, struct tevent_context *ev)
703 ldb->ev_ctx = ev;
706 struct tevent_context * ldb_get_event_context(struct ldb_context *ldb)
708 return ldb->ev_ctx;
711 void ldb_request_set_state(struct ldb_request *req, int state)
713 req->handle->state = state;
716 int ldb_request_get_status(struct ldb_request *req)
718 return req->handle->status;
723 trace a ldb request
725 static void ldb_trace_request(struct ldb_context *ldb, struct ldb_request *req)
727 TALLOC_CTX *tmp_ctx = talloc_new(req);
728 unsigned int i;
730 switch (req->operation) {
731 case LDB_SEARCH:
732 ldb_debug_add(ldb, "ldb_trace_request: SEARCH\n");
733 ldb_debug_add(ldb, " dn: %s\n",
734 ldb_dn_is_null(req->op.search.base)?"<rootDSE>":
735 ldb_dn_get_linearized(req->op.search.base));
736 ldb_debug_add(ldb, " scope: %s\n",
737 req->op.search.scope==LDB_SCOPE_BASE?"base":
738 req->op.search.scope==LDB_SCOPE_ONELEVEL?"one":
739 req->op.search.scope==LDB_SCOPE_SUBTREE?"sub":"UNKNOWN");
740 ldb_debug_add(ldb, " expr: %s\n",
741 ldb_filter_from_tree(tmp_ctx, req->op.search.tree));
742 if (req->op.search.attrs == NULL) {
743 ldb_debug_add(ldb, " attr: <ALL>\n");
744 } else {
745 for (i=0; req->op.search.attrs[i]; i++) {
746 ldb_debug_add(ldb, " attr: %s\n", req->op.search.attrs[i]);
749 break;
750 case LDB_DELETE:
751 ldb_debug_add(ldb, "ldb_trace_request: DELETE\n");
752 ldb_debug_add(ldb, " dn: %s\n",
753 ldb_dn_get_linearized(req->op.del.dn));
754 break;
755 case LDB_RENAME:
756 ldb_debug_add(ldb, "ldb_trace_request: RENAME\n");
757 ldb_debug_add(ldb, " olddn: %s\n",
758 ldb_dn_get_linearized(req->op.rename.olddn));
759 ldb_debug_add(ldb, " newdn: %s\n",
760 ldb_dn_get_linearized(req->op.rename.newdn));
761 break;
762 case LDB_EXTENDED:
763 ldb_debug_add(ldb, "ldb_trace_request: EXTENDED\n");
764 ldb_debug_add(ldb, " oid: %s\n", req->op.extended.oid);
765 ldb_debug_add(ldb, " data: %s\n", req->op.extended.data?"yes":"no");
766 break;
767 case LDB_ADD:
768 ldb_debug_add(ldb, "ldb_trace_request: ADD\n");
769 ldb_debug_add(req->handle->ldb, "%s\n",
770 ldb_ldif_message_string(req->handle->ldb, tmp_ctx,
771 LDB_CHANGETYPE_ADD,
772 req->op.add.message));
773 break;
774 case LDB_MODIFY:
775 ldb_debug_add(ldb, "ldb_trace_request: MODIFY\n");
776 ldb_debug_add(req->handle->ldb, "%s\n",
777 ldb_ldif_message_string(req->handle->ldb, tmp_ctx,
778 LDB_CHANGETYPE_MODIFY,
779 req->op.mod.message));
780 break;
781 case LDB_REQ_REGISTER_CONTROL:
782 ldb_debug_add(ldb, "ldb_trace_request: REGISTER_CONTROL\n");
783 ldb_debug_add(req->handle->ldb, "%s\n",
784 req->op.reg_control.oid);
785 break;
786 case LDB_REQ_REGISTER_PARTITION:
787 ldb_debug_add(ldb, "ldb_trace_request: REGISTER_PARTITION\n");
788 ldb_debug_add(req->handle->ldb, "%s\n",
789 ldb_dn_get_linearized(req->op.reg_partition.dn));
790 break;
791 default:
792 ldb_debug_add(ldb, "ldb_trace_request: UNKNOWN(%u)\n",
793 req->operation);
794 break;
797 if (req->controls == NULL) {
798 ldb_debug_add(ldb, " control: <NONE>\n");
799 } else {
800 for (i=0; req->controls && req->controls[i]; i++) {
801 if (req->controls[i]->oid) {
802 ldb_debug_add(ldb, " control: %s crit:%u data:%s\n",
803 req->controls[i]->oid,
804 req->controls[i]->critical,
805 req->controls[i]->data?"yes":"no");
810 ldb_debug_end(ldb, LDB_DEBUG_TRACE);
812 talloc_free(tmp_ctx);
816 check that the element flags don't have any internal bits set
818 static int ldb_msg_check_element_flags(struct ldb_context *ldb,
819 const struct ldb_message *message)
821 unsigned i;
822 for (i=0; i<message->num_elements; i++) {
823 if (message->elements[i].flags & LDB_FLAG_INTERNAL_MASK) {
824 ldb_asprintf_errstring(ldb, "Invalid element flags 0x%08x on element %s in %s\n",
825 message->elements[i].flags, message->elements[i].name,
826 ldb_dn_get_linearized(message->dn));
827 return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
830 return LDB_SUCCESS;
835 start an ldb request
836 NOTE: the request must be a talloc context.
837 returns LDB_ERR_* on errors.
839 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
841 struct ldb_module *module;
842 int ret;
844 if (req->callback == NULL) {
845 ldb_set_errstring(ldb, "Requests MUST define callbacks");
846 return LDB_ERR_UNWILLING_TO_PERFORM;
849 ldb_reset_err_string(ldb);
851 if (ldb->flags & LDB_FLG_ENABLE_TRACING) {
852 ldb_trace_request(ldb, req);
855 /* call the first module in the chain */
856 switch (req->operation) {
857 case LDB_SEARCH:
858 /* due to "ldb_build_search_req" base DN always != NULL */
859 if (!ldb_dn_validate(req->op.search.base)) {
860 ldb_asprintf_errstring(ldb, "ldb_search: invalid basedn '%s'",
861 ldb_dn_get_linearized(req->op.search.base));
862 return LDB_ERR_INVALID_DN_SYNTAX;
864 FIRST_OP(ldb, search);
865 ret = module->ops->search(module, req);
866 break;
867 case LDB_ADD:
868 if (!ldb_dn_validate(req->op.add.message->dn)) {
869 ldb_asprintf_errstring(ldb, "ldb_add: invalid dn '%s'",
870 ldb_dn_get_linearized(req->op.add.message->dn));
871 return LDB_ERR_INVALID_DN_SYNTAX;
874 * we have to normalize here, as so many places
875 * in modules and backends assume we don't have two
876 * elements with the same name
878 ret = ldb_msg_normalize(ldb, req, req->op.add.message,
879 discard_const(&req->op.add.message));
880 if (ret != LDB_SUCCESS) {
881 ldb_oom(ldb);
882 return ret;
884 FIRST_OP(ldb, add);
885 ret = ldb_msg_check_element_flags(ldb, req->op.add.message);
886 if (ret != LDB_SUCCESS) {
888 * "ldb_msg_check_element_flags" generates an error
889 * string
891 return ret;
893 ret = module->ops->add(module, req);
894 break;
895 case LDB_MODIFY:
896 if (!ldb_dn_validate(req->op.mod.message->dn)) {
897 ldb_asprintf_errstring(ldb, "ldb_modify: invalid dn '%s'",
898 ldb_dn_get_linearized(req->op.mod.message->dn));
899 return LDB_ERR_INVALID_DN_SYNTAX;
901 FIRST_OP(ldb, modify);
902 ret = ldb_msg_check_element_flags(ldb, req->op.mod.message);
903 if (ret != LDB_SUCCESS) {
905 * "ldb_msg_check_element_flags" generates an error
906 * string
908 return ret;
910 ret = module->ops->modify(module, req);
911 break;
912 case LDB_DELETE:
913 if (!ldb_dn_validate(req->op.del.dn)) {
914 ldb_asprintf_errstring(ldb, "ldb_delete: invalid dn '%s'",
915 ldb_dn_get_linearized(req->op.del.dn));
916 return LDB_ERR_INVALID_DN_SYNTAX;
918 FIRST_OP(ldb, del);
919 ret = module->ops->del(module, req);
920 break;
921 case LDB_RENAME:
922 if (!ldb_dn_validate(req->op.rename.olddn)) {
923 ldb_asprintf_errstring(ldb, "ldb_rename: invalid olddn '%s'",
924 ldb_dn_get_linearized(req->op.rename.olddn));
925 return LDB_ERR_INVALID_DN_SYNTAX;
927 if (!ldb_dn_validate(req->op.rename.newdn)) {
928 ldb_asprintf_errstring(ldb, "ldb_rename: invalid newdn '%s'",
929 ldb_dn_get_linearized(req->op.rename.newdn));
930 return LDB_ERR_INVALID_DN_SYNTAX;
932 FIRST_OP(ldb, rename);
933 ret = module->ops->rename(module, req);
934 break;
935 case LDB_EXTENDED:
936 FIRST_OP(ldb, extended);
937 ret = module->ops->extended(module, req);
938 break;
939 default:
940 FIRST_OP(ldb, request);
941 ret = module->ops->request(module, req);
942 break;
945 if ((ret != LDB_SUCCESS) && (ldb->err_string == NULL)) {
946 /* if no error string was setup by the backend */
947 ldb_asprintf_errstring(ldb, "ldb_request: %s (%d)",
948 ldb_strerror(ret), ret);
951 return ret;
954 int ldb_request_done(struct ldb_request *req, int status)
956 req->handle->state = LDB_ASYNC_DONE;
957 req->handle->status = status;
958 return status;
962 search the database given a LDAP-like search expression
964 returns an LDB error code
966 Use talloc_free to free the ldb_message returned in 'res', if successful
969 int ldb_search_default_callback(struct ldb_request *req,
970 struct ldb_reply *ares)
972 struct ldb_result *res;
973 unsigned int n;
975 res = talloc_get_type(req->context, struct ldb_result);
977 if (!ares) {
978 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
980 if (ares->error != LDB_SUCCESS) {
981 return ldb_request_done(req, ares->error);
984 switch (ares->type) {
985 case LDB_REPLY_ENTRY:
986 res->msgs = talloc_realloc(res, res->msgs,
987 struct ldb_message *, res->count + 2);
988 if (! res->msgs) {
989 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
992 res->msgs[res->count + 1] = NULL;
994 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
995 res->count++;
996 break;
998 case LDB_REPLY_REFERRAL:
999 if (res->refs) {
1000 for (n = 0; res->refs[n]; n++) /*noop*/ ;
1001 } else {
1002 n = 0;
1005 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
1006 if (! res->refs) {
1007 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1010 res->refs[n] = talloc_move(res->refs, &ares->referral);
1011 res->refs[n + 1] = NULL;
1012 break;
1014 case LDB_REPLY_DONE:
1015 /* TODO: we should really support controls on entries
1016 * and referrals too! */
1017 res->controls = talloc_move(res, &ares->controls);
1019 /* this is the last message, and means the request is done */
1020 /* we have to signal and eventual ldb_wait() waiting that the
1021 * async request operation was completed */
1022 talloc_free(ares);
1023 return ldb_request_done(req, LDB_SUCCESS);
1026 talloc_free(ares);
1028 return LDB_SUCCESS;
1031 int ldb_modify_default_callback(struct ldb_request *req, struct ldb_reply *ares)
1033 struct ldb_result *res;
1034 unsigned int n;
1035 int ret;
1037 res = talloc_get_type(req->context, struct ldb_result);
1039 if (!ares) {
1040 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1043 if (ares->error != LDB_SUCCESS) {
1044 ret = ares->error;
1045 talloc_free(ares);
1046 return ldb_request_done(req, ret);
1049 switch (ares->type) {
1050 case LDB_REPLY_REFERRAL:
1051 if (res->refs) {
1052 for (n = 0; res->refs[n]; n++) /*noop*/ ;
1053 } else {
1054 n = 0;
1057 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
1058 if (! res->refs) {
1059 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1062 res->refs[n] = talloc_move(res->refs, &ares->referral);
1063 res->refs[n + 1] = NULL;
1064 break;
1066 case LDB_REPLY_DONE:
1067 talloc_free(ares);
1068 return ldb_request_done(req, LDB_SUCCESS);
1069 default:
1070 talloc_free(ares);
1071 ldb_asprintf_errstring(req->handle->ldb, "Invalid LDB reply type %d", ares->type);
1072 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1075 talloc_free(ares);
1076 return ldb_request_done(req, LDB_SUCCESS);
1079 int ldb_op_default_callback(struct ldb_request *req, struct ldb_reply *ares)
1081 int ret;
1083 if (!ares) {
1084 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1087 if (ares->error != LDB_SUCCESS) {
1088 ret = ares->error;
1089 talloc_free(ares);
1090 return ldb_request_done(req, ret);
1093 if (ares->type != LDB_REPLY_DONE) {
1094 talloc_free(ares);
1095 ldb_asprintf_errstring(req->handle->ldb, "Invalid LDB reply type %d", ares->type);
1096 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1099 talloc_free(ares);
1100 return ldb_request_done(req, LDB_SUCCESS);
1103 int ldb_build_search_req_ex(struct ldb_request **ret_req,
1104 struct ldb_context *ldb,
1105 TALLOC_CTX *mem_ctx,
1106 struct ldb_dn *base,
1107 enum ldb_scope scope,
1108 struct ldb_parse_tree *tree,
1109 const char * const *attrs,
1110 struct ldb_control **controls,
1111 void *context,
1112 ldb_request_callback_t callback,
1113 struct ldb_request *parent)
1115 struct ldb_request *req;
1117 *ret_req = NULL;
1119 req = talloc(mem_ctx, struct ldb_request);
1120 if (req == NULL) {
1121 ldb_oom(ldb);
1122 return LDB_ERR_OPERATIONS_ERROR;
1125 req->operation = LDB_SEARCH;
1126 if (base == NULL) {
1127 req->op.search.base = ldb_dn_new(req, ldb, NULL);
1128 } else {
1129 req->op.search.base = base;
1131 req->op.search.scope = scope;
1133 req->op.search.tree = tree;
1134 if (req->op.search.tree == NULL) {
1135 ldb_set_errstring(ldb, "'tree' can't be NULL");
1136 talloc_free(req);
1137 return LDB_ERR_OPERATIONS_ERROR;
1140 req->op.search.attrs = attrs;
1141 req->controls = controls;
1142 req->context = context;
1143 req->callback = callback;
1145 ldb_set_timeout_from_prev_req(ldb, parent, req);
1147 req->handle = ldb_handle_new(req, ldb);
1148 if (req->handle == NULL) {
1149 ldb_oom(ldb);
1150 return LDB_ERR_OPERATIONS_ERROR;
1153 if (parent) {
1154 req->handle->nesting++;
1155 req->handle->parent = parent;
1156 req->handle->flags = parent->handle->flags;
1157 req->handle->custom_flags = parent->handle->custom_flags;
1160 *ret_req = req;
1161 return LDB_SUCCESS;
1164 int ldb_build_search_req(struct ldb_request **ret_req,
1165 struct ldb_context *ldb,
1166 TALLOC_CTX *mem_ctx,
1167 struct ldb_dn *base,
1168 enum ldb_scope scope,
1169 const char *expression,
1170 const char * const *attrs,
1171 struct ldb_control **controls,
1172 void *context,
1173 ldb_request_callback_t callback,
1174 struct ldb_request *parent)
1176 struct ldb_parse_tree *tree;
1177 int ret;
1179 tree = ldb_parse_tree(mem_ctx, expression);
1180 if (tree == NULL) {
1181 ldb_set_errstring(ldb, "Unable to parse search expression");
1182 return LDB_ERR_OPERATIONS_ERROR;
1185 ret = ldb_build_search_req_ex(ret_req, ldb, mem_ctx, base,
1186 scope, tree, attrs, controls,
1187 context, callback, parent);
1188 if (ret == LDB_SUCCESS) {
1189 talloc_steal(*ret_req, tree);
1191 return ret;
1194 int ldb_build_add_req(struct ldb_request **ret_req,
1195 struct ldb_context *ldb,
1196 TALLOC_CTX *mem_ctx,
1197 const struct ldb_message *message,
1198 struct ldb_control **controls,
1199 void *context,
1200 ldb_request_callback_t callback,
1201 struct ldb_request *parent)
1203 struct ldb_request *req;
1205 *ret_req = NULL;
1207 req = talloc(mem_ctx, struct ldb_request);
1208 if (req == NULL) {
1209 ldb_set_errstring(ldb, "Out of Memory");
1210 return LDB_ERR_OPERATIONS_ERROR;
1213 req->operation = LDB_ADD;
1214 req->op.add.message = message;
1215 req->controls = controls;
1216 req->context = context;
1217 req->callback = callback;
1219 ldb_set_timeout_from_prev_req(ldb, parent, req);
1221 req->handle = ldb_handle_new(req, ldb);
1222 if (req->handle == NULL) {
1223 ldb_oom(ldb);
1224 return LDB_ERR_OPERATIONS_ERROR;
1227 if (parent) {
1228 req->handle->nesting++;
1229 req->handle->parent = parent;
1230 req->handle->flags = parent->handle->flags;
1231 req->handle->custom_flags = parent->handle->custom_flags;
1234 *ret_req = req;
1236 return LDB_SUCCESS;
1239 int ldb_build_mod_req(struct ldb_request **ret_req,
1240 struct ldb_context *ldb,
1241 TALLOC_CTX *mem_ctx,
1242 const struct ldb_message *message,
1243 struct ldb_control **controls,
1244 void *context,
1245 ldb_request_callback_t callback,
1246 struct ldb_request *parent)
1248 struct ldb_request *req;
1250 *ret_req = NULL;
1252 req = talloc(mem_ctx, struct ldb_request);
1253 if (req == NULL) {
1254 ldb_set_errstring(ldb, "Out of Memory");
1255 return LDB_ERR_OPERATIONS_ERROR;
1258 req->operation = LDB_MODIFY;
1259 req->op.mod.message = message;
1260 req->controls = controls;
1261 req->context = context;
1262 req->callback = callback;
1264 ldb_set_timeout_from_prev_req(ldb, parent, req);
1266 req->handle = ldb_handle_new(req, ldb);
1267 if (req->handle == NULL) {
1268 ldb_oom(ldb);
1269 return LDB_ERR_OPERATIONS_ERROR;
1272 if (parent) {
1273 req->handle->nesting++;
1274 req->handle->parent = parent;
1275 req->handle->flags = parent->handle->flags;
1276 req->handle->custom_flags = parent->handle->custom_flags;
1279 *ret_req = req;
1281 return LDB_SUCCESS;
1284 int ldb_build_del_req(struct ldb_request **ret_req,
1285 struct ldb_context *ldb,
1286 TALLOC_CTX *mem_ctx,
1287 struct ldb_dn *dn,
1288 struct ldb_control **controls,
1289 void *context,
1290 ldb_request_callback_t callback,
1291 struct ldb_request *parent)
1293 struct ldb_request *req;
1295 *ret_req = NULL;
1297 req = talloc(mem_ctx, struct ldb_request);
1298 if (req == NULL) {
1299 ldb_set_errstring(ldb, "Out of Memory");
1300 return LDB_ERR_OPERATIONS_ERROR;
1303 req->operation = LDB_DELETE;
1304 req->op.del.dn = dn;
1305 req->controls = controls;
1306 req->context = context;
1307 req->callback = callback;
1309 ldb_set_timeout_from_prev_req(ldb, parent, req);
1311 req->handle = ldb_handle_new(req, ldb);
1312 if (req->handle == NULL) {
1313 ldb_oom(ldb);
1314 return LDB_ERR_OPERATIONS_ERROR;
1317 if (parent) {
1318 req->handle->nesting++;
1319 req->handle->parent = parent;
1320 req->handle->flags = parent->handle->flags;
1321 req->handle->custom_flags = parent->handle->custom_flags;
1324 *ret_req = req;
1326 return LDB_SUCCESS;
1329 int ldb_build_rename_req(struct ldb_request **ret_req,
1330 struct ldb_context *ldb,
1331 TALLOC_CTX *mem_ctx,
1332 struct ldb_dn *olddn,
1333 struct ldb_dn *newdn,
1334 struct ldb_control **controls,
1335 void *context,
1336 ldb_request_callback_t callback,
1337 struct ldb_request *parent)
1339 struct ldb_request *req;
1341 *ret_req = NULL;
1343 req = talloc(mem_ctx, struct ldb_request);
1344 if (req == NULL) {
1345 ldb_set_errstring(ldb, "Out of Memory");
1346 return LDB_ERR_OPERATIONS_ERROR;
1349 req->operation = LDB_RENAME;
1350 req->op.rename.olddn = olddn;
1351 req->op.rename.newdn = newdn;
1352 req->controls = controls;
1353 req->context = context;
1354 req->callback = callback;
1356 ldb_set_timeout_from_prev_req(ldb, parent, req);
1358 req->handle = ldb_handle_new(req, ldb);
1359 if (req->handle == NULL) {
1360 ldb_oom(ldb);
1361 return LDB_ERR_OPERATIONS_ERROR;
1364 if (parent) {
1365 req->handle->nesting++;
1366 req->handle->parent = parent;
1367 req->handle->flags = parent->handle->flags;
1368 req->handle->custom_flags = parent->handle->custom_flags;
1371 *ret_req = req;
1373 return LDB_SUCCESS;
1376 int ldb_extended_default_callback(struct ldb_request *req,
1377 struct ldb_reply *ares)
1379 struct ldb_result *res;
1381 res = talloc_get_type(req->context, struct ldb_result);
1383 if (!ares) {
1384 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1386 if (ares->error != LDB_SUCCESS) {
1387 return ldb_request_done(req, ares->error);
1390 if (ares->type == LDB_REPLY_DONE) {
1392 /* TODO: we should really support controls on entries and referrals too! */
1393 res->extended = talloc_move(res, &ares->response);
1394 res->controls = talloc_move(res, &ares->controls);
1396 talloc_free(ares);
1397 return ldb_request_done(req, LDB_SUCCESS);
1400 talloc_free(ares);
1401 ldb_asprintf_errstring(req->handle->ldb, "Invalid LDB reply type %d", ares->type);
1402 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1405 int ldb_build_extended_req(struct ldb_request **ret_req,
1406 struct ldb_context *ldb,
1407 TALLOC_CTX *mem_ctx,
1408 const char *oid,
1409 void *data,
1410 struct ldb_control **controls,
1411 void *context,
1412 ldb_request_callback_t callback,
1413 struct ldb_request *parent)
1415 struct ldb_request *req;
1417 *ret_req = NULL;
1419 req = talloc(mem_ctx, struct ldb_request);
1420 if (req == NULL) {
1421 ldb_set_errstring(ldb, "Out of Memory");
1422 return LDB_ERR_OPERATIONS_ERROR;
1425 req->operation = LDB_EXTENDED;
1426 req->op.extended.oid = oid;
1427 req->op.extended.data = data;
1428 req->controls = controls;
1429 req->context = context;
1430 req->callback = callback;
1432 ldb_set_timeout_from_prev_req(ldb, parent, req);
1434 req->handle = ldb_handle_new(req, ldb);
1435 if (req->handle == NULL) {
1436 ldb_oom(ldb);
1437 return LDB_ERR_OPERATIONS_ERROR;
1440 if (parent) {
1441 req->handle->nesting++;
1442 req->handle->parent = parent;
1443 req->handle->flags = parent->handle->flags;
1444 req->handle->custom_flags = parent->handle->custom_flags;
1447 *ret_req = req;
1449 return LDB_SUCCESS;
1452 int ldb_extended(struct ldb_context *ldb,
1453 const char *oid,
1454 void *data,
1455 struct ldb_result **_res)
1457 struct ldb_request *req;
1458 int ret;
1459 struct ldb_result *res;
1461 *_res = NULL;
1462 req = NULL;
1464 res = talloc_zero(ldb, struct ldb_result);
1465 if (!res) {
1466 return LDB_ERR_OPERATIONS_ERROR;
1469 ret = ldb_build_extended_req(&req, ldb, ldb,
1470 oid, data, NULL,
1471 res, ldb_extended_default_callback,
1472 NULL);
1473 ldb_req_set_location(req, "ldb_extended");
1475 if (ret != LDB_SUCCESS) goto done;
1477 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1479 ret = ldb_request(ldb, req);
1481 if (ret == LDB_SUCCESS) {
1482 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1485 done:
1486 if (ret != LDB_SUCCESS) {
1487 talloc_free(res);
1488 res = NULL;
1491 talloc_free(req);
1493 *_res = res;
1494 return ret;
1498 note that ldb_search() will automatically replace a NULL 'base' value
1499 with the defaultNamingContext from the rootDSE if available.
1501 int ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1502 struct ldb_result **result, struct ldb_dn *base,
1503 enum ldb_scope scope, const char * const *attrs,
1504 const char *exp_fmt, ...)
1506 struct ldb_request *req;
1507 struct ldb_result *res;
1508 char *expression;
1509 va_list ap;
1510 int ret;
1512 expression = NULL;
1513 *result = NULL;
1514 req = NULL;
1516 res = talloc_zero(mem_ctx, struct ldb_result);
1517 if (!res) {
1518 return LDB_ERR_OPERATIONS_ERROR;
1521 if (exp_fmt) {
1522 va_start(ap, exp_fmt);
1523 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
1524 va_end(ap);
1526 if (!expression) {
1527 talloc_free(res);
1528 return LDB_ERR_OPERATIONS_ERROR;
1532 ret = ldb_build_search_req(&req, ldb, mem_ctx,
1533 base?base:ldb_get_default_basedn(ldb),
1534 scope,
1535 expression,
1536 attrs,
1537 NULL,
1538 res,
1539 ldb_search_default_callback,
1540 NULL);
1541 ldb_req_set_location(req, "ldb_search");
1543 if (ret != LDB_SUCCESS) goto done;
1545 ret = ldb_request(ldb, req);
1547 if (ret == LDB_SUCCESS) {
1548 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1551 done:
1552 if (ret != LDB_SUCCESS) {
1553 talloc_free(res);
1554 res = NULL;
1557 talloc_free(expression);
1558 talloc_free(req);
1560 *result = res;
1561 return ret;
1565 add a record to the database. Will fail if a record with the given class
1566 and key already exists
1568 int ldb_add(struct ldb_context *ldb,
1569 const struct ldb_message *message)
1571 struct ldb_request *req;
1572 int ret;
1574 ret = ldb_msg_sanity_check(ldb, message);
1575 if (ret != LDB_SUCCESS) {
1576 return ret;
1579 ret = ldb_build_add_req(&req, ldb, ldb,
1580 message,
1581 NULL,
1582 NULL,
1583 ldb_op_default_callback,
1584 NULL);
1585 ldb_req_set_location(req, "ldb_add");
1587 if (ret != LDB_SUCCESS) return ret;
1589 /* do request and autostart a transaction */
1590 ret = ldb_autotransaction_request(ldb, req);
1592 talloc_free(req);
1593 return ret;
1597 modify the specified attributes of a record
1599 int ldb_modify(struct ldb_context *ldb,
1600 const struct ldb_message *message)
1602 struct ldb_request *req;
1603 int ret;
1605 ret = ldb_msg_sanity_check(ldb, message);
1606 if (ret != LDB_SUCCESS) {
1607 return ret;
1610 ret = ldb_build_mod_req(&req, ldb, ldb,
1611 message,
1612 NULL,
1613 NULL,
1614 ldb_op_default_callback,
1615 NULL);
1616 ldb_req_set_location(req, "ldb_modify");
1618 if (ret != LDB_SUCCESS) return ret;
1620 /* do request and autostart a transaction */
1621 ret = ldb_autotransaction_request(ldb, req);
1623 talloc_free(req);
1624 return ret;
1629 delete a record from the database
1631 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1633 struct ldb_request *req;
1634 int ret;
1636 ret = ldb_build_del_req(&req, ldb, ldb,
1638 NULL,
1639 NULL,
1640 ldb_op_default_callback,
1641 NULL);
1642 ldb_req_set_location(req, "ldb_delete");
1644 if (ret != LDB_SUCCESS) return ret;
1646 /* do request and autostart a transaction */
1647 ret = ldb_autotransaction_request(ldb, req);
1649 talloc_free(req);
1650 return ret;
1654 rename a record in the database
1656 int ldb_rename(struct ldb_context *ldb,
1657 struct ldb_dn *olddn, struct ldb_dn *newdn)
1659 struct ldb_request *req;
1660 int ret;
1662 ret = ldb_build_rename_req(&req, ldb, ldb,
1663 olddn,
1664 newdn,
1665 NULL,
1666 NULL,
1667 ldb_op_default_callback,
1668 NULL);
1669 ldb_req_set_location(req, "ldb_rename");
1671 if (ret != LDB_SUCCESS) return ret;
1673 /* do request and autostart a transaction */
1674 ret = ldb_autotransaction_request(ldb, req);
1676 talloc_free(req);
1677 return ret;
1682 return the global sequence number
1684 int ldb_sequence_number(struct ldb_context *ldb,
1685 enum ldb_sequence_type type, uint64_t *seq_num)
1687 struct ldb_seqnum_request *seq;
1688 struct ldb_seqnum_result *seqr;
1689 struct ldb_result *res;
1690 TALLOC_CTX *tmp_ctx;
1691 int ret;
1693 *seq_num = 0;
1695 tmp_ctx = talloc_zero(ldb, struct ldb_request);
1696 if (tmp_ctx == NULL) {
1697 ldb_set_errstring(ldb, "Out of Memory");
1698 return LDB_ERR_OPERATIONS_ERROR;
1700 seq = talloc_zero(tmp_ctx, struct ldb_seqnum_request);
1701 if (seq == NULL) {
1702 ldb_set_errstring(ldb, "Out of Memory");
1703 ret = LDB_ERR_OPERATIONS_ERROR;
1704 goto done;
1706 seq->type = type;
1708 ret = ldb_extended(ldb, LDB_EXTENDED_SEQUENCE_NUMBER, seq, &res);
1709 if (ret != LDB_SUCCESS) {
1710 goto done;
1712 talloc_steal(tmp_ctx, res);
1714 if (strcmp(LDB_EXTENDED_SEQUENCE_NUMBER, res->extended->oid) != 0) {
1715 ldb_set_errstring(ldb, "Invalid OID in reply");
1716 ret = LDB_ERR_OPERATIONS_ERROR;
1717 goto done;
1719 seqr = talloc_get_type(res->extended->data,
1720 struct ldb_seqnum_result);
1721 *seq_num = seqr->seq_num;
1723 done:
1724 talloc_free(tmp_ctx);
1725 return ret;
1729 return extended error information
1731 const char *ldb_errstring(struct ldb_context *ldb)
1733 if (ldb->err_string) {
1734 return ldb->err_string;
1737 return NULL;
1741 return a string explaining what a ldb error constant meancs
1743 const char *ldb_strerror(int ldb_err)
1745 switch (ldb_err) {
1746 case LDB_SUCCESS:
1747 return "Success";
1748 case LDB_ERR_OPERATIONS_ERROR:
1749 return "Operations error";
1750 case LDB_ERR_PROTOCOL_ERROR:
1751 return "Protocol error";
1752 case LDB_ERR_TIME_LIMIT_EXCEEDED:
1753 return "Time limit exceeded";
1754 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1755 return "Size limit exceeded";
1756 case LDB_ERR_COMPARE_FALSE:
1757 return "Compare false";
1758 case LDB_ERR_COMPARE_TRUE:
1759 return "Compare true";
1760 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1761 return "Auth method not supported";
1762 case LDB_ERR_STRONG_AUTH_REQUIRED:
1763 return "Strong auth required";
1764 /* 9 RESERVED */
1765 case LDB_ERR_REFERRAL:
1766 return "Referral error";
1767 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1768 return "Admin limit exceeded";
1769 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1770 return "Unsupported critical extension";
1771 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1772 return "Confidentiality required";
1773 case LDB_ERR_SASL_BIND_IN_PROGRESS:
1774 return "SASL bind in progress";
1775 case LDB_ERR_NO_SUCH_ATTRIBUTE:
1776 return "No such attribute";
1777 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1778 return "Undefined attribute type";
1779 case LDB_ERR_INAPPROPRIATE_MATCHING:
1780 return "Inappropriate matching";
1781 case LDB_ERR_CONSTRAINT_VIOLATION:
1782 return "Constraint violation";
1783 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1784 return "Attribute or value exists";
1785 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1786 return "Invalid attribute syntax";
1787 /* 22-31 unused */
1788 case LDB_ERR_NO_SUCH_OBJECT:
1789 return "No such object";
1790 case LDB_ERR_ALIAS_PROBLEM:
1791 return "Alias problem";
1792 case LDB_ERR_INVALID_DN_SYNTAX:
1793 return "Invalid DN syntax";
1794 /* 35 RESERVED */
1795 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1796 return "Alias dereferencing problem";
1797 /* 37-47 unused */
1798 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1799 return "Inappropriate authentication";
1800 case LDB_ERR_INVALID_CREDENTIALS:
1801 return "Invalid credentials";
1802 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1803 return "insufficient access rights";
1804 case LDB_ERR_BUSY:
1805 return "Busy";
1806 case LDB_ERR_UNAVAILABLE:
1807 return "Unavailable";
1808 case LDB_ERR_UNWILLING_TO_PERFORM:
1809 return "Unwilling to perform";
1810 case LDB_ERR_LOOP_DETECT:
1811 return "Loop detect";
1812 /* 55-63 unused */
1813 case LDB_ERR_NAMING_VIOLATION:
1814 return "Naming violation";
1815 case LDB_ERR_OBJECT_CLASS_VIOLATION:
1816 return "Object class violation";
1817 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1818 return "Not allowed on non-leaf";
1819 case LDB_ERR_NOT_ALLOWED_ON_RDN:
1820 return "Not allowed on RDN";
1821 case LDB_ERR_ENTRY_ALREADY_EXISTS:
1822 return "Entry already exists";
1823 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1824 return "Object class mods prohibited";
1825 /* 70 RESERVED FOR CLDAP */
1826 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1827 return "Affects multiple DSAs";
1828 /* 72-79 unused */
1829 case LDB_ERR_OTHER:
1830 return "Other";
1833 return "Unknown error";
1837 set backend specific opaque parameters
1839 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1841 struct ldb_opaque *o;
1843 /* allow updating an existing value */
1844 for (o=ldb->opaque;o;o=o->next) {
1845 if (strcmp(o->name, name) == 0) {
1846 o->value = value;
1847 return LDB_SUCCESS;
1851 o = talloc(ldb, struct ldb_opaque);
1852 if (o == NULL) {
1853 ldb_oom(ldb);
1854 return LDB_ERR_OTHER;
1856 o->next = ldb->opaque;
1857 o->name = name;
1858 o->value = value;
1859 ldb->opaque = o;
1860 return LDB_SUCCESS;
1864 get a previously set opaque value
1866 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1868 struct ldb_opaque *o;
1869 for (o=ldb->opaque;o;o=o->next) {
1870 if (strcmp(o->name, name) == 0) {
1871 return o->value;
1874 return NULL;
1877 int ldb_global_init(void)
1879 /* Provided for compatibility with some older versions of ldb */
1880 return 0;
1883 /* return the ldb flags */
1884 unsigned int ldb_get_flags(struct ldb_context *ldb)
1886 return ldb->flags;
1889 /* set the ldb flags */
1890 void ldb_set_flags(struct ldb_context *ldb, unsigned flags)
1892 ldb->flags = flags;
1897 set the location in a ldb request. Used for debugging
1899 void ldb_req_set_location(struct ldb_request *req, const char *location)
1901 if (req && req->handle) {
1902 req->handle->location = location;
1907 return the location set with dsdb_req_set_location
1909 const char *ldb_req_location(struct ldb_request *req)
1911 return req->handle->location;
1915 mark a request as untrusted. This tells the rootdse module to remove
1916 unregistered controls
1918 void ldb_req_mark_untrusted(struct ldb_request *req)
1920 req->handle->flags |= LDB_HANDLE_FLAG_UNTRUSTED;
1924 mark a request as trusted.
1926 void ldb_req_mark_trusted(struct ldb_request *req)
1928 req->handle->flags &= ~LDB_HANDLE_FLAG_UNTRUSTED;
1932 set custom flags. Those flags are set by applications using ldb,
1933 they are application dependent and the same bit can have different
1934 meaning in different application.
1936 void ldb_req_set_custom_flags(struct ldb_request *req, uint32_t flags)
1938 if (req != NULL && req->handle != NULL) {
1939 req->handle->custom_flags = flags;
1945 get custom flags. Those flags are set by applications using ldb,
1946 they are application dependent and the same bit can have different
1947 meaning in different application.
1949 uint32_t ldb_req_get_custom_flags(struct ldb_request *req)
1951 if (req != NULL && req->handle != NULL) {
1952 return req->handle->custom_flags;
1956 * 0 is not something any better or worse than
1957 * anything else as req or the handle is NULL
1959 return 0;
1964 return true is a request is untrusted
1966 bool ldb_req_is_untrusted(struct ldb_request *req)
1968 return (req->handle->flags & LDB_HANDLE_FLAG_UNTRUSTED) != 0;