Removed ldb_modify_ctrl from ldb, implemented as a static in ldap_backend.
[Samba/gebeck_regimport.git] / source4 / lib / ldb / common / ldb.c
bloba3472a60e57213db43d4f9648c36c8653d8a6479
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 != LDB_SUCCESS) {
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 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", 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);
260 if (ldb->flags & LDB_FLG_ENABLE_TRACING) {
261 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_set_errstring: %s", ldb->err_string);
265 void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
267 va_list ap;
268 char *old_string = NULL;
270 if (ldb->err_string) {
271 old_string = ldb->err_string;
274 va_start(ap, format);
275 ldb->err_string = talloc_vasprintf(ldb, format, ap);
276 va_end(ap);
277 talloc_free(old_string);
280 void ldb_reset_err_string(struct ldb_context *ldb)
282 if (ldb->err_string) {
283 talloc_free(ldb->err_string);
284 ldb->err_string = NULL;
288 #define FIRST_OP_NOERR(ldb, op) do { \
289 module = ldb->modules; \
290 while (module && module->ops->op == NULL) module = module->next; \
291 if ((ldb->flags & LDB_FLG_ENABLE_TRACING) && module) { \
292 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: (%s)->" #op, \
293 module->ops->name); \
295 } while (0)
297 #define FIRST_OP(ldb, op) do { \
298 FIRST_OP_NOERR(ldb, op); \
299 if (module == NULL) { \
300 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
301 return LDB_ERR_OPERATIONS_ERROR; \
303 } while (0)
307 start a transaction
309 int ldb_transaction_start(struct ldb_context *ldb)
311 struct ldb_module *module;
312 int status;
314 ldb_debug(ldb, LDB_DEBUG_TRACE,
315 "start ldb transaction (nesting: %d)",
316 ldb->transaction_active);
318 /* explicit transaction active, count nested requests */
319 if (ldb->transaction_active) {
320 ldb->transaction_active++;
321 return LDB_SUCCESS;
324 /* start a new transaction */
325 ldb->transaction_active++;
326 ldb->prepare_commit_done = false;
328 FIRST_OP(ldb, start_transaction);
330 ldb_reset_err_string(ldb);
332 status = module->ops->start_transaction(module);
333 if (status != LDB_SUCCESS) {
334 if (ldb->err_string == NULL) {
335 /* no error string was setup by the backend */
336 ldb_asprintf_errstring(ldb,
337 "ldb transaction start: %s (%d)",
338 ldb_strerror(status),
339 status);
342 if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
343 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "start ldb transaction error: %s",
344 ldb_errstring(module->ldb));
346 return status;
350 prepare for transaction commit (first phase of two phase commit)
352 int ldb_transaction_prepare_commit(struct ldb_context *ldb)
354 struct ldb_module *module;
355 int status;
357 if (ldb->prepare_commit_done) {
358 return LDB_SUCCESS;
361 /* commit only when all nested transactions are complete */
362 if (ldb->transaction_active > 1) {
363 return LDB_SUCCESS;
366 ldb->prepare_commit_done = true;
368 if (ldb->transaction_active < 0) {
369 ldb_debug(ldb, LDB_DEBUG_FATAL,
370 "prepare commit called but no ldb transactions are active!");
371 ldb->transaction_active = 0;
372 return LDB_ERR_OPERATIONS_ERROR;
375 /* call prepare transaction if available */
376 FIRST_OP_NOERR(ldb, prepare_commit);
377 if (module == NULL) {
378 return LDB_SUCCESS;
381 status = module->ops->prepare_commit(module);
382 if (status != LDB_SUCCESS) {
383 /* if a module fails the prepare then we need
384 to call the end transaction for everyone */
385 FIRST_OP(ldb, end_transaction);
386 module->ops->end_transaction(module);
387 if (ldb->err_string == NULL) {
388 /* no error string was setup by the backend */
389 ldb_asprintf_errstring(ldb,
390 "ldb transaction prepare commit: %s (%d)",
391 ldb_strerror(status),
392 status);
394 if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
395 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "prepare commit transaction error: %s",
396 ldb_errstring(module->ldb));
400 return status;
405 commit a transaction
407 int ldb_transaction_commit(struct ldb_context *ldb)
409 struct ldb_module *module;
410 int status;
412 status = ldb_transaction_prepare_commit(ldb);
413 if (status != LDB_SUCCESS) {
414 return status;
417 ldb->transaction_active--;
419 ldb_debug(ldb, LDB_DEBUG_TRACE,
420 "commit ldb transaction (nesting: %d)",
421 ldb->transaction_active);
423 /* commit only when all nested transactions are complete */
424 if (ldb->transaction_active > 0) {
425 return LDB_SUCCESS;
428 if (ldb->transaction_active < 0) {
429 ldb_debug(ldb, LDB_DEBUG_FATAL,
430 "commit called but no ldb transactions are active!");
431 ldb->transaction_active = 0;
432 return LDB_ERR_OPERATIONS_ERROR;
435 ldb_reset_err_string(ldb);
437 FIRST_OP(ldb, end_transaction);
438 status = module->ops->end_transaction(module);
439 if (status != LDB_SUCCESS) {
440 if (ldb->err_string == NULL) {
441 /* no error string was setup by the backend */
442 ldb_asprintf_errstring(ldb,
443 "ldb transaction commit: %s (%d)",
444 ldb_strerror(status),
445 status);
447 if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
448 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "commit ldb transaction error: %s",
449 ldb_errstring(module->ldb));
451 /* cancel the transaction */
452 FIRST_OP(ldb, del_transaction);
453 module->ops->del_transaction(module);
455 return status;
460 cancel a transaction
462 int ldb_transaction_cancel(struct ldb_context *ldb)
464 struct ldb_module *module;
465 int status;
467 ldb->transaction_active--;
469 ldb_debug(ldb, LDB_DEBUG_TRACE,
470 "cancel ldb transaction (nesting: %d)",
471 ldb->transaction_active);
473 /* really cancel only if all nested transactions are complete */
474 if (ldb->transaction_active > 0) {
475 return LDB_SUCCESS;
478 if (ldb->transaction_active < 0) {
479 ldb_debug(ldb, LDB_DEBUG_FATAL,
480 "cancel called but no ldb transactions are active!");
481 ldb->transaction_active = 0;
482 return LDB_ERR_OPERATIONS_ERROR;
485 FIRST_OP(ldb, del_transaction);
487 status = module->ops->del_transaction(module);
488 if (status != LDB_SUCCESS) {
489 if (ldb->err_string == NULL) {
490 /* no error string was setup by the backend */
491 ldb_asprintf_errstring(ldb,
492 "ldb transaction cancel: %s (%d)",
493 ldb_strerror(status),
494 status);
496 if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
497 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "cancel ldb transaction error: %s",
498 ldb_errstring(module->ldb));
501 return status;
505 cancel a transaction with no error if no transaction is pending
506 used when we fork() to clear any parent transactions
508 int ldb_transaction_cancel_noerr(struct ldb_context *ldb)
510 if (ldb->transaction_active > 0) {
511 return ldb_transaction_cancel(ldb);
513 return LDB_SUCCESS;
517 /* autostarts a transacion if none active */
518 static int ldb_autotransaction_request(struct ldb_context *ldb,
519 struct ldb_request *req)
521 int ret;
523 ret = ldb_transaction_start(ldb);
524 if (ret != LDB_SUCCESS) {
525 return ret;
528 ret = ldb_request(ldb, req);
529 if (ret == LDB_SUCCESS) {
530 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
533 if (ret == LDB_SUCCESS) {
534 return ldb_transaction_commit(ldb);
536 ldb_transaction_cancel(ldb);
538 if (ldb->err_string == NULL) {
539 /* no error string was setup by the backend */
540 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
543 return ret;
546 int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
548 struct tevent_context *ev;
549 int ret;
551 if (!handle) {
552 return LDB_ERR_UNAVAILABLE;
555 if (handle->state == LDB_ASYNC_DONE) {
556 return handle->status;
559 ev = ldb_get_event_context(handle->ldb);
560 if (NULL == ev) {
561 return LDB_ERR_OPERATIONS_ERROR;
564 switch (type) {
565 case LDB_WAIT_NONE:
566 ret = tevent_loop_once(ev);
567 if (ret != 0) {
568 return LDB_ERR_OPERATIONS_ERROR;
570 if (handle->state == LDB_ASYNC_DONE ||
571 handle->status != LDB_SUCCESS) {
572 return handle->status;
574 break;
576 case LDB_WAIT_ALL:
577 while (handle->state != LDB_ASYNC_DONE) {
578 ret = tevent_loop_once(ev);
579 if (ret != 0) {
580 return LDB_ERR_OPERATIONS_ERROR;
582 if (handle->status != LDB_SUCCESS) {
583 return handle->status;
586 return handle->status;
589 return LDB_SUCCESS;
592 /* set the specified timeout or, if timeout is 0 set the default timeout */
593 int ldb_set_timeout(struct ldb_context *ldb,
594 struct ldb_request *req,
595 int timeout)
597 if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
599 if (timeout != 0) {
600 req->timeout = timeout;
601 } else {
602 req->timeout = ldb->default_timeout;
604 req->starttime = time(NULL);
606 return LDB_SUCCESS;
609 /* calculates the new timeout based on the previous starttime and timeout */
610 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb,
611 struct ldb_request *oldreq,
612 struct ldb_request *newreq)
614 if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
616 if (oldreq == NULL) {
617 return ldb_set_timeout(ldb, newreq, 0);
620 newreq->starttime = oldreq->starttime;
621 newreq->timeout = oldreq->timeout;
623 return LDB_SUCCESS;
628 set the permissions for new files to be passed to open() in
629 backends that use local files
631 void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
633 ldb->create_perms = perms;
636 unsigned int ldb_get_create_perms(struct ldb_context *ldb)
638 return ldb->create_perms;
641 void ldb_set_event_context(struct ldb_context *ldb, struct tevent_context *ev)
643 ldb->ev_ctx = ev;
646 struct tevent_context * ldb_get_event_context(struct ldb_context *ldb)
648 return ldb->ev_ctx;
651 void ldb_request_set_state(struct ldb_request *req, int state)
653 req->handle->state = state;
656 int ldb_request_get_status(struct ldb_request *req)
658 return req->handle->status;
663 trace a ldb request
665 static void ldb_trace_request(struct ldb_context *ldb, struct ldb_request *req)
667 TALLOC_CTX *tmp_ctx = talloc_new(req);
668 int i;
670 switch (req->operation) {
671 case LDB_SEARCH:
672 ldb_debug_add(ldb, "ldb_trace_request: SEARCH\n");
673 ldb_debug_add(ldb, " dn: %s\n",
674 ldb_dn_is_null(req->op.search.base)?"<rootDSE>":
675 ldb_dn_get_linearized(req->op.search.base));
676 ldb_debug_add(ldb, " scope: %s\n",
677 req->op.search.scope==LDB_SCOPE_BASE?"base":
678 req->op.search.scope==LDB_SCOPE_ONELEVEL?"one":
679 req->op.search.scope==LDB_SCOPE_SUBTREE?"sub":"UNKNOWN");
680 ldb_debug_add(ldb, " expr: %s\n",
681 ldb_filter_from_tree(tmp_ctx, req->op.search.tree));
682 if (req->op.search.attrs == NULL) {
683 ldb_debug_add(ldb, " attr: <ALL>\n");
684 } else {
685 for (i=0; req->op.search.attrs[i]; i++) {
686 ldb_debug_add(ldb, " attr: %s\n", req->op.search.attrs[i]);
689 break;
690 case LDB_DELETE:
691 ldb_debug_add(ldb, "ldb_trace_request: DELETE\n");
692 ldb_debug_add(ldb, " dn: %s\n",
693 ldb_dn_get_linearized(req->op.del.dn));
694 break;
695 case LDB_RENAME:
696 ldb_debug_add(ldb, "ldb_trace_request: RENAME\n");
697 ldb_debug_add(ldb, " olddn: %s\n",
698 ldb_dn_get_linearized(req->op.rename.olddn));
699 ldb_debug_add(ldb, " newdn: %s\n",
700 ldb_dn_get_linearized(req->op.rename.newdn));
701 break;
702 case LDB_EXTENDED:
703 ldb_debug_add(ldb, "ldb_trace_request: EXTENDED\n");
704 ldb_debug_add(ldb, " oid: %s\n", req->op.extended.oid);
705 ldb_debug_add(ldb, " data: %s\n", req->op.extended.data?"yes":"no");
706 break;
707 case LDB_ADD:
708 ldb_debug_add(ldb, "ldb_trace_request: ADD\n");
709 ldb_debug_add(req->handle->ldb, "%s\n",
710 ldb_ldif_message_string(req->handle->ldb, tmp_ctx,
711 LDB_CHANGETYPE_ADD,
712 req->op.add.message));
713 break;
714 case LDB_MODIFY:
715 ldb_debug_add(ldb, "ldb_trace_request: MODIFY\n");
716 ldb_debug_add(req->handle->ldb, "%s\n",
717 ldb_ldif_message_string(req->handle->ldb, tmp_ctx,
718 LDB_CHANGETYPE_ADD,
719 req->op.mod.message));
720 break;
721 case LDB_REQ_REGISTER_CONTROL:
722 ldb_debug_add(ldb, "ldb_trace_request: REGISTER_CONTROL\n");
723 ldb_debug_add(req->handle->ldb, "%s\n",
724 req->op.reg_control.oid);
725 break;
726 case LDB_REQ_REGISTER_PARTITION:
727 ldb_debug_add(ldb, "ldb_trace_request: REGISTER_PARTITION\n");
728 ldb_debug_add(req->handle->ldb, "%s\n",
729 ldb_dn_get_linearized(req->op.reg_partition.dn));
730 break;
731 default:
732 ldb_debug_add(ldb, "ldb_trace_request: UNKNOWN(%u)\n",
733 req->operation);
734 break;
737 if (req->controls == NULL) {
738 ldb_debug_add(ldb, " control: <NONE>\n");
739 } else {
740 for (i=0; req->controls && req->controls[i]; i++) {
741 ldb_debug_add(ldb, " control: %s crit:%u data:%s\n",
742 req->controls[i]->oid,
743 req->controls[i]->critical,
744 req->controls[i]->data?"yes":"no");
748 ldb_debug_end(ldb, LDB_DEBUG_TRACE);
750 talloc_free(tmp_ctx);
755 start an ldb request
756 NOTE: the request must be a talloc context.
757 returns LDB_ERR_* on errors.
759 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
761 struct ldb_module *module;
762 int ret;
764 if (req->callback == NULL) {
765 ldb_set_errstring(ldb, "Requests MUST define callbacks");
766 return LDB_ERR_UNWILLING_TO_PERFORM;
769 ldb_reset_err_string(ldb);
771 if (ldb->flags & LDB_FLG_ENABLE_TRACING) {
772 ldb_trace_request(ldb, req);
775 /* call the first module in the chain */
776 switch (req->operation) {
777 case LDB_SEARCH:
778 FIRST_OP(ldb, search);
779 ret = module->ops->search(module, req);
780 break;
781 case LDB_ADD:
782 FIRST_OP(ldb, add);
783 ret = module->ops->add(module, req);
784 break;
785 case LDB_MODIFY:
786 FIRST_OP(ldb, modify);
787 ret = module->ops->modify(module, req);
788 break;
789 case LDB_DELETE:
790 FIRST_OP(ldb, del);
791 ret = module->ops->del(module, req);
792 break;
793 case LDB_RENAME:
794 if (!ldb_dn_validate(req->op.rename.olddn)) {
795 ldb_asprintf_errstring(ldb, "ldb_rename: invalid olddn '%s'",
796 ldb_dn_get_linearized(req->op.rename.olddn));
797 return LDB_ERR_INVALID_DN_SYNTAX;
799 if (!ldb_dn_validate(req->op.rename.newdn)) {
800 ldb_asprintf_errstring(ldb, "ldb_rename: invalid newdn '%s'",
801 ldb_dn_get_linearized(req->op.rename.newdn));
802 return LDB_ERR_INVALID_DN_SYNTAX;
804 FIRST_OP(ldb, rename);
805 ret = module->ops->rename(module, req);
806 break;
807 case LDB_EXTENDED:
808 FIRST_OP(ldb, extended);
809 ret = module->ops->extended(module, req);
810 break;
811 default:
812 FIRST_OP(ldb, request);
813 ret = module->ops->request(module, req);
814 break;
817 return ret;
820 int ldb_request_done(struct ldb_request *req, int status)
822 req->handle->state = LDB_ASYNC_DONE;
823 req->handle->status = status;
824 return status;
828 search the database given a LDAP-like search expression
830 returns an LDB error code
832 Use talloc_free to free the ldb_message returned in 'res', if successful
835 int ldb_search_default_callback(struct ldb_request *req,
836 struct ldb_reply *ares)
838 struct ldb_result *res;
839 int n;
841 res = talloc_get_type(req->context, struct ldb_result);
843 if (!ares) {
844 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
846 if (ares->error != LDB_SUCCESS) {
847 return ldb_request_done(req, ares->error);
850 switch (ares->type) {
851 case LDB_REPLY_ENTRY:
852 res->msgs = talloc_realloc(res, res->msgs,
853 struct ldb_message *, res->count + 2);
854 if (! res->msgs) {
855 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
858 res->msgs[res->count + 1] = NULL;
860 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
861 res->count++;
862 break;
864 case LDB_REPLY_REFERRAL:
865 if (res->refs) {
866 for (n = 0; res->refs[n]; n++) /*noop*/ ;
867 } else {
868 n = 0;
871 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
872 if (! res->refs) {
873 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
876 res->refs[n] = talloc_move(res->refs, &ares->referral);
877 res->refs[n + 1] = NULL;
878 break;
880 case LDB_REPLY_DONE:
881 /* TODO: we should really support controls on entries
882 * and referrals too! */
883 res->controls = talloc_move(res, &ares->controls);
885 /* this is the last message, and means the request is done */
886 /* we have to signal and eventual ldb_wait() waiting that the
887 * async request operation was completed */
888 talloc_free(ares);
889 return ldb_request_done(req, LDB_SUCCESS);
892 talloc_free(ares);
894 return LDB_SUCCESS;
897 int ldb_op_default_callback(struct ldb_request *req, struct ldb_reply *ares)
899 int ret;
901 if (!ares) {
902 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
905 if (ares->error != LDB_SUCCESS) {
906 ret = ares->error;
907 talloc_free(ares);
908 return ldb_request_done(req, ret);
911 if (ares->type != LDB_REPLY_DONE) {
912 talloc_free(ares);
913 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
914 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
917 talloc_free(ares);
918 return ldb_request_done(req, LDB_SUCCESS);
921 int ldb_build_search_req_ex(struct ldb_request **ret_req,
922 struct ldb_context *ldb,
923 void *mem_ctx,
924 struct ldb_dn *base,
925 enum ldb_scope scope,
926 struct ldb_parse_tree *tree,
927 const char * const *attrs,
928 struct ldb_control **controls,
929 void *context,
930 ldb_request_callback_t callback,
931 struct ldb_request *parent)
933 struct ldb_request *req;
935 *ret_req = NULL;
937 req = talloc(mem_ctx, struct ldb_request);
938 if (req == NULL) {
939 ldb_oom(ldb);
940 return LDB_ERR_OPERATIONS_ERROR;
943 req->operation = LDB_SEARCH;
944 if (base == NULL) {
945 req->op.search.base = ldb_dn_new(req, ldb, NULL);
946 } else {
947 req->op.search.base = base;
949 req->op.search.scope = scope;
951 req->op.search.tree = tree;
952 if (req->op.search.tree == NULL) {
953 ldb_set_errstring(ldb, "'tree' can't be NULL");
954 talloc_free(req);
955 return LDB_ERR_OPERATIONS_ERROR;
958 req->op.search.attrs = attrs;
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 if (parent) {
972 req->handle->nesting++;
975 *ret_req = req;
976 return LDB_SUCCESS;
979 int ldb_build_search_req(struct ldb_request **ret_req,
980 struct ldb_context *ldb,
981 void *mem_ctx,
982 struct ldb_dn *base,
983 enum ldb_scope scope,
984 const char *expression,
985 const char * const *attrs,
986 struct ldb_control **controls,
987 void *context,
988 ldb_request_callback_t callback,
989 struct ldb_request *parent)
991 struct ldb_parse_tree *tree;
992 int ret;
994 tree = ldb_parse_tree(mem_ctx, expression);
995 if (tree == NULL) {
996 ldb_set_errstring(ldb, "Unable to parse search expression");
997 return LDB_ERR_OPERATIONS_ERROR;
1000 ret = ldb_build_search_req_ex(ret_req, ldb, mem_ctx, base,
1001 scope, tree, attrs, controls,
1002 context, callback, parent);
1003 if (ret == LDB_SUCCESS) {
1004 talloc_steal(*ret_req, tree);
1006 return ret;
1009 int ldb_build_add_req(struct ldb_request **ret_req,
1010 struct ldb_context *ldb,
1011 void *mem_ctx,
1012 const struct ldb_message *message,
1013 struct ldb_control **controls,
1014 void *context,
1015 ldb_request_callback_t callback,
1016 struct ldb_request *parent)
1018 struct ldb_request *req;
1020 *ret_req = NULL;
1022 req = talloc(mem_ctx, struct ldb_request);
1023 if (req == NULL) {
1024 ldb_set_errstring(ldb, "Out of Memory");
1025 return LDB_ERR_OPERATIONS_ERROR;
1028 req->operation = LDB_ADD;
1029 req->op.add.message = message;
1030 req->controls = controls;
1031 req->context = context;
1032 req->callback = callback;
1034 ldb_set_timeout_from_prev_req(ldb, parent, req);
1036 req->handle = ldb_handle_new(req, ldb);
1037 if (req->handle == NULL) {
1038 ldb_oom(ldb);
1039 return LDB_ERR_OPERATIONS_ERROR;
1042 if (parent) {
1043 req->handle->nesting++;
1046 *ret_req = req;
1048 return LDB_SUCCESS;
1051 int ldb_build_mod_req(struct ldb_request **ret_req,
1052 struct ldb_context *ldb,
1053 void *mem_ctx,
1054 const struct ldb_message *message,
1055 struct ldb_control **controls,
1056 void *context,
1057 ldb_request_callback_t callback,
1058 struct ldb_request *parent)
1060 struct ldb_request *req;
1062 *ret_req = NULL;
1064 req = talloc(mem_ctx, struct ldb_request);
1065 if (req == NULL) {
1066 ldb_set_errstring(ldb, "Out of Memory");
1067 return LDB_ERR_OPERATIONS_ERROR;
1070 req->operation = LDB_MODIFY;
1071 req->op.mod.message = message;
1072 req->controls = controls;
1073 req->context = context;
1074 req->callback = callback;
1076 ldb_set_timeout_from_prev_req(ldb, parent, req);
1078 req->handle = ldb_handle_new(req, ldb);
1079 if (req->handle == NULL) {
1080 ldb_oom(ldb);
1081 return LDB_ERR_OPERATIONS_ERROR;
1084 if (parent) {
1085 req->handle->nesting++;
1088 *ret_req = req;
1090 return LDB_SUCCESS;
1093 int ldb_build_del_req(struct ldb_request **ret_req,
1094 struct ldb_context *ldb,
1095 void *mem_ctx,
1096 struct ldb_dn *dn,
1097 struct ldb_control **controls,
1098 void *context,
1099 ldb_request_callback_t callback,
1100 struct ldb_request *parent)
1102 struct ldb_request *req;
1104 *ret_req = NULL;
1106 req = talloc(mem_ctx, struct ldb_request);
1107 if (req == NULL) {
1108 ldb_set_errstring(ldb, "Out of Memory");
1109 return LDB_ERR_OPERATIONS_ERROR;
1112 req->operation = LDB_DELETE;
1113 req->op.del.dn = dn;
1114 req->controls = controls;
1115 req->context = context;
1116 req->callback = callback;
1118 ldb_set_timeout_from_prev_req(ldb, parent, req);
1120 req->handle = ldb_handle_new(req, ldb);
1121 if (req->handle == NULL) {
1122 ldb_oom(ldb);
1123 return LDB_ERR_OPERATIONS_ERROR;
1126 if (parent) {
1127 req->handle->nesting++;
1130 *ret_req = req;
1132 return LDB_SUCCESS;
1135 int ldb_build_rename_req(struct ldb_request **ret_req,
1136 struct ldb_context *ldb,
1137 void *mem_ctx,
1138 struct ldb_dn *olddn,
1139 struct ldb_dn *newdn,
1140 struct ldb_control **controls,
1141 void *context,
1142 ldb_request_callback_t callback,
1143 struct ldb_request *parent)
1145 struct ldb_request *req;
1147 *ret_req = NULL;
1149 req = talloc(mem_ctx, struct ldb_request);
1150 if (req == NULL) {
1151 ldb_set_errstring(ldb, "Out of Memory");
1152 return LDB_ERR_OPERATIONS_ERROR;
1155 req->operation = LDB_RENAME;
1156 req->op.rename.olddn = olddn;
1157 req->op.rename.newdn = newdn;
1158 req->controls = controls;
1159 req->context = context;
1160 req->callback = callback;
1162 ldb_set_timeout_from_prev_req(ldb, parent, req);
1164 req->handle = ldb_handle_new(req, ldb);
1165 if (req->handle == NULL) {
1166 ldb_oom(ldb);
1167 return LDB_ERR_OPERATIONS_ERROR;
1170 if (parent) {
1171 req->handle->nesting++;
1174 *ret_req = req;
1176 return LDB_SUCCESS;
1179 int ldb_extended_default_callback(struct ldb_request *req,
1180 struct ldb_reply *ares)
1182 struct ldb_result *res;
1184 res = talloc_get_type(req->context, struct ldb_result);
1186 if (!ares) {
1187 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1189 if (ares->error != LDB_SUCCESS) {
1190 return ldb_request_done(req, ares->error);
1193 if (ares->type == LDB_REPLY_DONE) {
1195 /* TODO: we should really support controls on entries and referrals too! */
1196 res->extended = talloc_move(res, &ares->response);
1197 res->controls = talloc_move(res, &ares->controls);
1199 talloc_free(ares);
1200 return ldb_request_done(req, LDB_SUCCESS);
1203 talloc_free(ares);
1204 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
1205 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1208 int ldb_build_extended_req(struct ldb_request **ret_req,
1209 struct ldb_context *ldb,
1210 void *mem_ctx,
1211 const char *oid,
1212 void *data,
1213 struct ldb_control **controls,
1214 void *context,
1215 ldb_request_callback_t callback,
1216 struct ldb_request *parent)
1218 struct ldb_request *req;
1220 *ret_req = NULL;
1222 req = talloc(mem_ctx, struct ldb_request);
1223 if (req == NULL) {
1224 ldb_set_errstring(ldb, "Out of Memory");
1225 return LDB_ERR_OPERATIONS_ERROR;
1228 req->operation = LDB_EXTENDED;
1229 req->op.extended.oid = oid;
1230 req->op.extended.data = data;
1231 req->controls = controls;
1232 req->context = context;
1233 req->callback = callback;
1235 ldb_set_timeout_from_prev_req(ldb, parent, req);
1237 req->handle = ldb_handle_new(req, ldb);
1238 if (req->handle == NULL) {
1239 ldb_oom(ldb);
1240 return LDB_ERR_OPERATIONS_ERROR;
1243 if (parent) {
1244 req->handle->nesting++;
1247 *ret_req = req;
1249 return LDB_SUCCESS;
1252 int ldb_extended(struct ldb_context *ldb,
1253 const char *oid,
1254 void *data,
1255 struct ldb_result **_res)
1257 struct ldb_request *req;
1258 int ret;
1259 struct ldb_result *res;
1261 *_res = NULL;
1263 res = talloc_zero(ldb, struct ldb_result);
1264 if (!res) {
1265 return LDB_ERR_OPERATIONS_ERROR;
1268 ret = ldb_build_extended_req(&req, ldb, ldb,
1269 oid, data, NULL,
1270 res, ldb_extended_default_callback,
1271 NULL);
1272 if (ret != LDB_SUCCESS) goto done;
1274 ldb_set_timeout(ldb, req, 0); /* use default timeout */
1276 ret = ldb_request(ldb, req);
1278 if (ret == LDB_SUCCESS) {
1279 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1282 talloc_free(req);
1284 done:
1285 if (ret != LDB_SUCCESS) {
1286 talloc_free(res);
1289 *_res = res;
1290 return ret;
1294 note that ldb_search() will automatically replace a NULL 'base' value
1295 with the defaultNamingContext from the rootDSE if available.
1297 int ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1298 struct ldb_result **result, struct ldb_dn *base,
1299 enum ldb_scope scope, const char * const *attrs,
1300 const char *exp_fmt, ...)
1302 struct ldb_request *req;
1303 struct ldb_result *res;
1304 char *expression;
1305 va_list ap;
1306 int ret;
1308 expression = NULL;
1309 *result = NULL;
1310 req = NULL;
1312 res = talloc_zero(mem_ctx, struct ldb_result);
1313 if (!res) {
1314 return LDB_ERR_OPERATIONS_ERROR;
1317 if (exp_fmt) {
1318 va_start(ap, exp_fmt);
1319 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
1320 va_end(ap);
1322 if (!expression) {
1323 talloc_free(res);
1324 return LDB_ERR_OPERATIONS_ERROR;
1328 ret = ldb_build_search_req(&req, ldb, mem_ctx,
1329 base?base:ldb_get_default_basedn(ldb),
1330 scope,
1331 expression,
1332 attrs,
1333 NULL,
1334 res,
1335 ldb_search_default_callback,
1336 NULL);
1338 if (ret != LDB_SUCCESS) goto done;
1340 ret = ldb_request(ldb, req);
1342 if (ret == LDB_SUCCESS) {
1343 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1346 done:
1347 if (ret != LDB_SUCCESS) {
1348 talloc_free(res);
1349 res = NULL;
1352 talloc_free(expression);
1353 talloc_free(req);
1355 *result = res;
1356 return ret;
1360 add a record to the database. Will fail if a record with the given class
1361 and key already exists
1363 int ldb_add(struct ldb_context *ldb,
1364 const struct ldb_message *message)
1366 struct ldb_request *req;
1367 int ret;
1369 ret = ldb_msg_sanity_check(ldb, message);
1370 if (ret != LDB_SUCCESS) {
1371 return ret;
1374 ret = ldb_build_add_req(&req, ldb, ldb,
1375 message,
1376 NULL,
1377 NULL,
1378 ldb_op_default_callback,
1379 NULL);
1381 if (ret != LDB_SUCCESS) return ret;
1383 /* do request and autostart a transaction */
1384 ret = ldb_autotransaction_request(ldb, req);
1386 talloc_free(req);
1387 return ret;
1391 modify the specified attributes of a record
1393 int ldb_modify(struct ldb_context *ldb,
1394 const struct ldb_message *message)
1396 struct ldb_request *req;
1397 int ret;
1399 ret = ldb_msg_sanity_check(ldb, message);
1400 if (ret != LDB_SUCCESS) {
1401 return ret;
1404 ret = ldb_build_mod_req(&req, ldb, ldb,
1405 message,
1406 NULL,
1407 NULL,
1408 ldb_op_default_callback,
1409 NULL);
1411 if (ret != LDB_SUCCESS) return ret;
1413 /* do request and autostart a transaction */
1414 ret = ldb_autotransaction_request(ldb, req);
1416 talloc_free(req);
1417 return ret;
1422 delete a record from the database
1424 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1426 struct ldb_request *req;
1427 int ret;
1429 ret = ldb_build_del_req(&req, ldb, ldb,
1431 NULL,
1432 NULL,
1433 ldb_op_default_callback,
1434 NULL);
1436 if (ret != LDB_SUCCESS) return ret;
1438 /* do request and autostart a transaction */
1439 ret = ldb_autotransaction_request(ldb, req);
1441 talloc_free(req);
1442 return ret;
1446 rename a record in the database
1448 int ldb_rename(struct ldb_context *ldb,
1449 struct ldb_dn *olddn, struct ldb_dn *newdn)
1451 struct ldb_request *req;
1452 int ret;
1454 ret = ldb_build_rename_req(&req, ldb, ldb,
1455 olddn,
1456 newdn,
1457 NULL,
1458 NULL,
1459 ldb_op_default_callback,
1460 NULL);
1462 if (ret != LDB_SUCCESS) return ret;
1464 /* do request and autostart a transaction */
1465 ret = ldb_autotransaction_request(ldb, req);
1467 talloc_free(req);
1468 return ret;
1473 return the global sequence number
1475 int ldb_sequence_number(struct ldb_context *ldb,
1476 enum ldb_sequence_type type, uint64_t *seq_num)
1478 struct ldb_seqnum_request *seq;
1479 struct ldb_seqnum_result *seqr;
1480 struct ldb_result *res;
1481 TALLOC_CTX *tmp_ctx;
1482 int ret;
1484 *seq_num = 0;
1486 tmp_ctx = talloc_zero(ldb, struct ldb_request);
1487 if (tmp_ctx == NULL) {
1488 ldb_set_errstring(ldb, "Out of Memory");
1489 return LDB_ERR_OPERATIONS_ERROR;
1491 seq = talloc_zero(tmp_ctx, struct ldb_seqnum_request);
1492 if (seq == NULL) {
1493 ldb_set_errstring(ldb, "Out of Memory");
1494 ret = LDB_ERR_OPERATIONS_ERROR;
1495 goto done;
1497 seq->type = type;
1499 ret = ldb_extended(ldb, LDB_EXTENDED_SEQUENCE_NUMBER, seq, &res);
1500 if (ret != LDB_SUCCESS) {
1501 goto done;
1503 talloc_steal(tmp_ctx, res);
1505 if (strcmp(LDB_EXTENDED_SEQUENCE_NUMBER, res->extended->oid) != 0) {
1506 ldb_set_errstring(ldb, "Invalid OID in reply");
1507 ret = LDB_ERR_OPERATIONS_ERROR;
1508 goto done;
1510 seqr = talloc_get_type(res->extended->data,
1511 struct ldb_seqnum_result);
1512 *seq_num = seqr->seq_num;
1514 done:
1515 talloc_free(tmp_ctx);
1516 return ret;
1520 return extended error information
1522 const char *ldb_errstring(struct ldb_context *ldb)
1524 if (ldb->err_string) {
1525 return ldb->err_string;
1528 return NULL;
1532 return a string explaining what a ldb error constant meancs
1534 const char *ldb_strerror(int ldb_err)
1536 switch (ldb_err) {
1537 case LDB_SUCCESS:
1538 return "Success";
1539 case LDB_ERR_OPERATIONS_ERROR:
1540 return "Operations error";
1541 case LDB_ERR_PROTOCOL_ERROR:
1542 return "Protocol error";
1543 case LDB_ERR_TIME_LIMIT_EXCEEDED:
1544 return "Time limit exceeded";
1545 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1546 return "Size limit exceeded";
1547 case LDB_ERR_COMPARE_FALSE:
1548 return "Compare false";
1549 case LDB_ERR_COMPARE_TRUE:
1550 return "Compare true";
1551 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1552 return "Auth method not supported";
1553 case LDB_ERR_STRONG_AUTH_REQUIRED:
1554 return "Strong auth required";
1555 /* 9 RESERVED */
1556 case LDB_ERR_REFERRAL:
1557 return "Referral error";
1558 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1559 return "Admin limit exceeded";
1560 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1561 return "Unsupported critical extension";
1562 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1563 return "Confidentiality required";
1564 case LDB_ERR_SASL_BIND_IN_PROGRESS:
1565 return "SASL bind in progress";
1566 case LDB_ERR_NO_SUCH_ATTRIBUTE:
1567 return "No such attribute";
1568 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1569 return "Undefined attribute type";
1570 case LDB_ERR_INAPPROPRIATE_MATCHING:
1571 return "Inappropriate matching";
1572 case LDB_ERR_CONSTRAINT_VIOLATION:
1573 return "Constraint violation";
1574 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1575 return "Attribute or value exists";
1576 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1577 return "Invalid attribute syntax";
1578 /* 22-31 unused */
1579 case LDB_ERR_NO_SUCH_OBJECT:
1580 return "No such object";
1581 case LDB_ERR_ALIAS_PROBLEM:
1582 return "Alias problem";
1583 case LDB_ERR_INVALID_DN_SYNTAX:
1584 return "Invalid DN syntax";
1585 /* 35 RESERVED */
1586 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1587 return "Alias dereferencing problem";
1588 /* 37-47 unused */
1589 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1590 return "Inappropriate authentication";
1591 case LDB_ERR_INVALID_CREDENTIALS:
1592 return "Invalid credentials";
1593 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1594 return "insufficient access rights";
1595 case LDB_ERR_BUSY:
1596 return "Busy";
1597 case LDB_ERR_UNAVAILABLE:
1598 return "Unavailable";
1599 case LDB_ERR_UNWILLING_TO_PERFORM:
1600 return "Unwilling to perform";
1601 case LDB_ERR_LOOP_DETECT:
1602 return "Loop detect";
1603 /* 55-63 unused */
1604 case LDB_ERR_NAMING_VIOLATION:
1605 return "Naming violation";
1606 case LDB_ERR_OBJECT_CLASS_VIOLATION:
1607 return "Object class violation";
1608 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1609 return "Not allowed on non-leaf";
1610 case LDB_ERR_NOT_ALLOWED_ON_RDN:
1611 return "Not allowed on RDN";
1612 case LDB_ERR_ENTRY_ALREADY_EXISTS:
1613 return "Entry already exists";
1614 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1615 return "Object class mods prohibited";
1616 /* 70 RESERVED FOR CLDAP */
1617 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1618 return "Affects multiple DSAs";
1619 /* 72-79 unused */
1620 case LDB_ERR_OTHER:
1621 return "Other";
1624 return "Unknown error";
1628 set backend specific opaque parameters
1630 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1632 struct ldb_opaque *o;
1634 /* allow updating an existing value */
1635 for (o=ldb->opaque;o;o=o->next) {
1636 if (strcmp(o->name, name) == 0) {
1637 o->value = value;
1638 return LDB_SUCCESS;
1642 o = talloc(ldb, struct ldb_opaque);
1643 if (o == NULL) {
1644 ldb_oom(ldb);
1645 return LDB_ERR_OTHER;
1647 o->next = ldb->opaque;
1648 o->name = name;
1649 o->value = value;
1650 ldb->opaque = o;
1651 return LDB_SUCCESS;
1655 get a previously set opaque value
1657 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1659 struct ldb_opaque *o;
1660 for (o=ldb->opaque;o;o=o->next) {
1661 if (strcmp(o->name, name) == 0) {
1662 return o->value;
1665 return NULL;
1668 int ldb_global_init(void)
1670 /* Provided for compatibility with some older versions of ldb */
1671 return 0;
1674 /* return the ldb flags */
1675 unsigned int ldb_get_flags(struct ldb_context *ldb)
1677 return ldb->flags;
1680 /* set the ldb flags */
1681 void ldb_set_flags(struct ldb_context *ldb, unsigned flags)
1683 ldb->flags = flags;