s3:net_idmap_dump deal with idmap config * : backend config style
[Samba/gebeck_regimport.git] / lib / ldb / ldb_tdb / ldb_tdb.c
blob860ee146a605ffe6d6e4b8b833424dc556ae1a16
1 /*
2 ldb database library
4 Copyright (C) Andrew Tridgell 2004
5 Copyright (C) Stefan Metzmacher 2004
6 Copyright (C) Simo Sorce 2006-2008
7 Copyright (C) Matthias Dieter Wallnöfer 2009-2010
9 ** NOTE! The following LGPL license applies to the ldb
10 ** library. This does NOT imply that all of Samba is released
11 ** under the LGPL
13 This library is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 3 of the License, or (at your option) any later version.
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 * Name: ldb_tdb
30 * Component: ldb tdb backend
32 * Description: core functions for tdb backend
34 * Author: Andrew Tridgell
35 * Author: Stefan Metzmacher
37 * Modifications:
39 * - description: make the module use asynchronous calls
40 * date: Feb 2006
41 * Author: Simo Sorce
43 * - description: make it possible to use event contexts
44 * date: Jan 2008
45 * Author: Simo Sorce
47 * - description: fix up memory leaks and small bugs
48 * date: Oct 2009
49 * Author: Matthias Dieter Wallnöfer
52 #include "ldb_tdb.h"
53 #include "ldb_private.h"
54 #include <tdb.h>
57 prevent memory errors on callbacks
59 struct ltdb_req_spy {
60 struct ltdb_context *ctx;
64 map a tdb error code to a ldb error code
66 int ltdb_err_map(enum TDB_ERROR tdb_code)
68 switch (tdb_code) {
69 case TDB_SUCCESS:
70 return LDB_SUCCESS;
71 case TDB_ERR_CORRUPT:
72 case TDB_ERR_OOM:
73 case TDB_ERR_EINVAL:
74 return LDB_ERR_OPERATIONS_ERROR;
75 case TDB_ERR_IO:
76 return LDB_ERR_PROTOCOL_ERROR;
77 case TDB_ERR_LOCK:
78 case TDB_ERR_NOLOCK:
79 return LDB_ERR_BUSY;
80 case TDB_ERR_LOCK_TIMEOUT:
81 return LDB_ERR_TIME_LIMIT_EXCEEDED;
82 case TDB_ERR_EXISTS:
83 return LDB_ERR_ENTRY_ALREADY_EXISTS;
84 case TDB_ERR_NOEXIST:
85 return LDB_ERR_NO_SUCH_OBJECT;
86 case TDB_ERR_RDONLY:
87 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
88 default:
89 break;
91 return LDB_ERR_OTHER;
95 lock the database for read - use by ltdb_search and ltdb_sequence_number
97 int ltdb_lock_read(struct ldb_module *module)
99 void *data = ldb_module_get_private(module);
100 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
101 int ret = 0;
103 if (ltdb->in_transaction == 0 &&
104 ltdb->read_lock_count == 0) {
105 ret = tdb_lockall_read(ltdb->tdb);
107 if (ret == 0) {
108 ltdb->read_lock_count++;
110 return ret;
114 unlock the database after a ltdb_lock_read()
116 int ltdb_unlock_read(struct ldb_module *module)
118 void *data = ldb_module_get_private(module);
119 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
120 if (ltdb->in_transaction == 0 && ltdb->read_lock_count == 1) {
121 tdb_unlockall_read(ltdb->tdb);
122 return 0;
124 ltdb->read_lock_count--;
125 return 0;
130 form a TDB_DATA for a record key
131 caller frees
133 note that the key for a record can depend on whether the
134 dn refers to a case sensitive index record or not
136 TDB_DATA ltdb_key(struct ldb_module *module, struct ldb_dn *dn)
138 struct ldb_context *ldb = ldb_module_get_ctx(module);
139 TDB_DATA key;
140 char *key_str = NULL;
141 const char *dn_folded = NULL;
144 most DNs are case insensitive. The exception is index DNs for
145 case sensitive attributes
147 there are 3 cases dealt with in this code:
149 1) if the dn doesn't start with @ then uppercase the attribute
150 names and the attributes values of case insensitive attributes
151 2) if the dn starts with @ then leave it alone -
152 the indexing code handles the rest
155 dn_folded = ldb_dn_get_casefold(dn);
156 if (!dn_folded) {
157 goto failed;
160 key_str = talloc_strdup(ldb, "DN=");
161 if (!key_str) {
162 goto failed;
165 key_str = talloc_strdup_append_buffer(key_str, dn_folded);
166 if (!key_str) {
167 goto failed;
170 key.dptr = (uint8_t *)key_str;
171 key.dsize = strlen(key_str) + 1;
173 return key;
175 failed:
176 errno = ENOMEM;
177 key.dptr = NULL;
178 key.dsize = 0;
179 return key;
183 check special dn's have valid attributes
184 currently only @ATTRIBUTES is checked
186 static int ltdb_check_special_dn(struct ldb_module *module,
187 const struct ldb_message *msg)
189 struct ldb_context *ldb = ldb_module_get_ctx(module);
190 unsigned int i, j;
192 if (! ldb_dn_is_special(msg->dn) ||
193 ! ldb_dn_check_special(msg->dn, LTDB_ATTRIBUTES)) {
194 return LDB_SUCCESS;
197 /* we have @ATTRIBUTES, let's check attributes are fine */
198 /* should we check that we deny multivalued attributes ? */
199 for (i = 0; i < msg->num_elements; i++) {
200 if (ldb_attr_cmp(msg->elements[i].name, "distinguishedName") == 0) continue;
202 for (j = 0; j < msg->elements[i].num_values; j++) {
203 if (ltdb_check_at_attributes_values(&msg->elements[i].values[j]) != 0) {
204 ldb_set_errstring(ldb, "Invalid attribute value in an @ATTRIBUTES entry");
205 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
210 return LDB_SUCCESS;
215 we've made a modification to a dn - possibly reindex and
216 update sequence number
218 static int ltdb_modified(struct ldb_module *module, struct ldb_dn *dn)
220 int ret = LDB_SUCCESS;
221 struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module), struct ltdb_private);
223 /* only allow modifies inside a transaction, otherwise the
224 * ldb is unsafe */
225 if (ltdb->in_transaction == 0) {
226 ldb_set_errstring(ldb_module_get_ctx(module), "ltdb modify without transaction");
227 return LDB_ERR_OPERATIONS_ERROR;
230 if (ldb_dn_is_special(dn) &&
231 (ldb_dn_check_special(dn, LTDB_INDEXLIST) ||
232 ldb_dn_check_special(dn, LTDB_ATTRIBUTES)) ) {
233 ret = ltdb_reindex(module);
236 /* If the modify was to a normal record, or any special except @BASEINFO, update the seq number */
237 if (ret == LDB_SUCCESS &&
238 !(ldb_dn_is_special(dn) &&
239 ldb_dn_check_special(dn, LTDB_BASEINFO)) ) {
240 ret = ltdb_increase_sequence_number(module);
243 /* If the modify was to @OPTIONS, reload the cache */
244 if (ret == LDB_SUCCESS &&
245 ldb_dn_is_special(dn) &&
246 (ldb_dn_check_special(dn, LTDB_OPTIONS)) ) {
247 ret = ltdb_cache_reload(module);
250 return ret;
254 store a record into the db
256 int ltdb_store(struct ldb_module *module, const struct ldb_message *msg, int flgs)
258 void *data = ldb_module_get_private(module);
259 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
260 TDB_DATA tdb_key, tdb_data;
261 int ret = LDB_SUCCESS;
263 tdb_key = ltdb_key(module, msg->dn);
264 if (tdb_key.dptr == NULL) {
265 return LDB_ERR_OTHER;
268 ret = ldb_pack_data(module, msg, (struct ldb_val *)&tdb_data);
269 if (ret == -1) {
270 talloc_free(tdb_key.dptr);
271 return LDB_ERR_OTHER;
274 ret = tdb_store(ltdb->tdb, tdb_key, tdb_data, flgs);
275 if (ret != 0) {
276 ret = ltdb_err_map(tdb_error(ltdb->tdb));
277 goto done;
280 done:
281 talloc_free(tdb_key.dptr);
282 talloc_free(tdb_data.dptr);
284 return ret;
289 check if a attribute is a single valued, for a given element
291 static bool ldb_tdb_single_valued(const struct ldb_schema_attribute *a,
292 struct ldb_message_element *el)
294 if (!a) return false;
295 if (el != NULL) {
296 if (el->flags & LDB_FLAG_INTERNAL_FORCE_SINGLE_VALUE_CHECK) {
297 /* override from a ldb module, for example
298 used for the description field, which is
299 marked multi-valued in the schema but which
300 should not actually accept multiple
301 values */
302 return true;
304 if (el->flags & LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK) {
305 /* override from a ldb module, for example used for
306 deleted linked attribute entries */
307 return false;
310 if (a->flags & LDB_ATTR_FLAG_SINGLE_VALUE) {
311 return true;
313 return false;
316 static int ltdb_add_internal(struct ldb_module *module,
317 const struct ldb_message *msg,
318 bool check_single_value)
320 struct ldb_context *ldb = ldb_module_get_ctx(module);
321 int ret = LDB_SUCCESS;
322 unsigned int i, j;
324 for (i=0;i<msg->num_elements;i++) {
325 struct ldb_message_element *el = &msg->elements[i];
326 const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(ldb, el->name);
328 if (el->num_values == 0) {
329 ldb_asprintf_errstring(ldb, "attribute '%s' on '%s' specified, but with 0 values (illegal)",
330 el->name, ldb_dn_get_linearized(msg->dn));
331 return LDB_ERR_CONSTRAINT_VIOLATION;
333 if (check_single_value &&
334 el->num_values > 1 &&
335 ldb_tdb_single_valued(a, el)) {
336 ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
337 el->name, ldb_dn_get_linearized(msg->dn));
338 return LDB_ERR_CONSTRAINT_VIOLATION;
341 /* Do not check "@ATTRIBUTES" for duplicated values */
342 if (ldb_dn_is_special(msg->dn) &&
343 ldb_dn_check_special(msg->dn, LTDB_ATTRIBUTES)) {
344 continue;
347 /* TODO: This is O(n^2) - replace with more efficient check */
348 for (j=0; j<el->num_values; j++) {
349 if (ldb_msg_find_val(el, &el->values[j]) != &el->values[j]) {
350 ldb_asprintf_errstring(ldb,
351 "attribute '%s': value #%u on '%s' provided more than once",
352 el->name, j, ldb_dn_get_linearized(msg->dn));
353 return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
358 ret = ltdb_store(module, msg, TDB_INSERT);
359 if (ret != LDB_SUCCESS) {
360 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
361 ldb_asprintf_errstring(ldb,
362 "Entry %s already exists",
363 ldb_dn_get_linearized(msg->dn));
365 return ret;
368 ret = ltdb_index_add_new(module, msg);
369 if (ret != LDB_SUCCESS) {
370 return ret;
373 ret = ltdb_modified(module, msg->dn);
375 return ret;
379 add a record to the database
381 static int ltdb_add(struct ltdb_context *ctx)
383 struct ldb_module *module = ctx->module;
384 struct ldb_request *req = ctx->req;
385 int ret = LDB_SUCCESS;
387 ret = ltdb_check_special_dn(module, req->op.add.message);
388 if (ret != LDB_SUCCESS) {
389 return ret;
392 ldb_request_set_state(req, LDB_ASYNC_PENDING);
394 if (ltdb_cache_load(module) != 0) {
395 return LDB_ERR_OPERATIONS_ERROR;
398 ret = ltdb_add_internal(module, req->op.add.message, true);
400 return ret;
404 delete a record from the database, not updating indexes (used for deleting
405 index records)
407 int ltdb_delete_noindex(struct ldb_module *module, struct ldb_dn *dn)
409 void *data = ldb_module_get_private(module);
410 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
411 TDB_DATA tdb_key;
412 int ret;
414 tdb_key = ltdb_key(module, dn);
415 if (!tdb_key.dptr) {
416 return LDB_ERR_OTHER;
419 ret = tdb_delete(ltdb->tdb, tdb_key);
420 talloc_free(tdb_key.dptr);
422 if (ret != 0) {
423 ret = ltdb_err_map(tdb_error(ltdb->tdb));
426 return ret;
429 static int ltdb_delete_internal(struct ldb_module *module, struct ldb_dn *dn)
431 struct ldb_message *msg;
432 int ret = LDB_SUCCESS;
434 msg = ldb_msg_new(module);
435 if (msg == NULL) {
436 return LDB_ERR_OPERATIONS_ERROR;
439 /* in case any attribute of the message was indexed, we need
440 to fetch the old record */
441 ret = ltdb_search_dn1(module, dn, msg);
442 if (ret != LDB_SUCCESS) {
443 /* not finding the old record is an error */
444 goto done;
447 ret = ltdb_delete_noindex(module, dn);
448 if (ret != LDB_SUCCESS) {
449 goto done;
452 /* remove any indexed attributes */
453 ret = ltdb_index_delete(module, msg);
454 if (ret != LDB_SUCCESS) {
455 goto done;
458 ret = ltdb_modified(module, dn);
459 if (ret != LDB_SUCCESS) {
460 goto done;
463 done:
464 talloc_free(msg);
465 return ret;
469 delete a record from the database
471 static int ltdb_delete(struct ltdb_context *ctx)
473 struct ldb_module *module = ctx->module;
474 struct ldb_request *req = ctx->req;
475 int ret = LDB_SUCCESS;
477 ldb_request_set_state(req, LDB_ASYNC_PENDING);
479 if (ltdb_cache_load(module) != 0) {
480 return LDB_ERR_OPERATIONS_ERROR;
483 ret = ltdb_delete_internal(module, req->op.del.dn);
485 return ret;
489 find an element by attribute name. At the moment this does a linear search,
490 it should be re-coded to use a binary search once all places that modify
491 records guarantee sorted order
493 return the index of the first matching element if found, otherwise -1
495 static int find_element(const struct ldb_message *msg, const char *name)
497 unsigned int i;
498 for (i=0;i<msg->num_elements;i++) {
499 if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
500 return i;
503 return -1;
508 add an element to an existing record. Assumes a elements array that we
509 can call re-alloc on, and assumed that we can re-use the data pointers from
510 the passed in additional values. Use with care!
512 returns 0 on success, -1 on failure (and sets errno)
514 static int ltdb_msg_add_element(struct ldb_context *ldb,
515 struct ldb_message *msg,
516 struct ldb_message_element *el)
518 struct ldb_message_element *e2;
519 unsigned int i;
521 if (el->num_values == 0) {
522 /* nothing to do here - we don't add empty elements */
523 return 0;
526 e2 = talloc_realloc(msg, msg->elements, struct ldb_message_element,
527 msg->num_elements+1);
528 if (!e2) {
529 errno = ENOMEM;
530 return -1;
533 msg->elements = e2;
535 e2 = &msg->elements[msg->num_elements];
537 e2->name = el->name;
538 e2->flags = el->flags;
539 e2->values = talloc_array(msg->elements,
540 struct ldb_val, el->num_values);
541 if (!e2->values) {
542 errno = ENOMEM;
543 return -1;
545 for (i=0;i<el->num_values;i++) {
546 e2->values[i] = el->values[i];
548 e2->num_values = el->num_values;
550 ++msg->num_elements;
552 return 0;
556 delete all elements having a specified attribute name
558 static int msg_delete_attribute(struct ldb_module *module,
559 struct ldb_context *ldb,
560 struct ldb_message *msg, const char *name)
562 unsigned int i;
563 int ret;
564 struct ldb_message_element *el;
566 el = ldb_msg_find_element(msg, name);
567 if (el == NULL) {
568 return LDB_ERR_NO_SUCH_ATTRIBUTE;
570 i = el - msg->elements;
572 ret = ltdb_index_del_element(module, msg->dn, el);
573 if (ret != LDB_SUCCESS) {
574 return ret;
577 talloc_free(el->values);
578 if (msg->num_elements > (i+1)) {
579 memmove(el, el+1, sizeof(*el) * (msg->num_elements - (i+1)));
581 msg->num_elements--;
582 msg->elements = talloc_realloc(msg, msg->elements,
583 struct ldb_message_element,
584 msg->num_elements);
585 return LDB_SUCCESS;
589 delete all elements matching an attribute name/value
591 return LDB Error on failure
593 static int msg_delete_element(struct ldb_module *module,
594 struct ldb_message *msg,
595 const char *name,
596 const struct ldb_val *val)
598 struct ldb_context *ldb = ldb_module_get_ctx(module);
599 unsigned int i;
600 int found, ret;
601 struct ldb_message_element *el;
602 const struct ldb_schema_attribute *a;
604 found = find_element(msg, name);
605 if (found == -1) {
606 return LDB_ERR_NO_SUCH_ATTRIBUTE;
609 i = (unsigned int) found;
610 el = &(msg->elements[i]);
612 a = ldb_schema_attribute_by_name(ldb, el->name);
614 for (i=0;i<el->num_values;i++) {
615 bool matched;
616 if (a->syntax->operator_fn) {
617 ret = a->syntax->operator_fn(ldb, LDB_OP_EQUALITY, a,
618 &el->values[i], val, &matched);
619 if (ret != LDB_SUCCESS) return ret;
620 } else {
621 matched = (a->syntax->comparison_fn(ldb, ldb,
622 &el->values[i], val) == 0);
624 if (matched) {
625 if (el->num_values == 1) {
626 return msg_delete_attribute(module, ldb, msg, name);
629 ret = ltdb_index_del_value(module, msg->dn, el, i);
630 if (ret != LDB_SUCCESS) {
631 return ret;
634 if (i<el->num_values-1) {
635 memmove(&el->values[i], &el->values[i+1],
636 sizeof(el->values[i])*
637 (el->num_values-(i+1)));
639 el->num_values--;
641 /* per definition we find in a canonicalised message an
642 attribute value only once. So we are finished here */
643 return LDB_SUCCESS;
647 /* Not found */
648 return LDB_ERR_NO_SUCH_ATTRIBUTE;
653 modify a record - internal interface
655 yuck - this is O(n^2). Luckily n is usually small so we probably
656 get away with it, but if we ever have really large attribute lists
657 then we'll need to look at this again
659 'req' is optional, and is used to specify controls if supplied
661 int ltdb_modify_internal(struct ldb_module *module,
662 const struct ldb_message *msg,
663 struct ldb_request *req)
665 struct ldb_context *ldb = ldb_module_get_ctx(module);
666 void *data = ldb_module_get_private(module);
667 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
668 TDB_DATA tdb_key, tdb_data;
669 struct ldb_message *msg2;
670 unsigned int i, j, k;
671 int ret = LDB_SUCCESS, idx;
672 struct ldb_control *control_permissive = NULL;
674 if (req) {
675 control_permissive = ldb_request_get_control(req,
676 LDB_CONTROL_PERMISSIVE_MODIFY_OID);
679 tdb_key = ltdb_key(module, msg->dn);
680 if (!tdb_key.dptr) {
681 return LDB_ERR_OTHER;
684 tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
685 if (!tdb_data.dptr) {
686 talloc_free(tdb_key.dptr);
687 return ltdb_err_map(tdb_error(ltdb->tdb));
690 msg2 = ldb_msg_new(tdb_key.dptr);
691 if (msg2 == NULL) {
692 free(tdb_data.dptr);
693 ret = LDB_ERR_OTHER;
694 goto done;
697 ret = ldb_unpack_data(ldb_module_get_ctx(module), (struct ldb_val *)&tdb_data, msg2);
698 free(tdb_data.dptr);
699 if (ret == -1) {
700 ret = LDB_ERR_OTHER;
701 goto done;
704 if (!msg2->dn) {
705 msg2->dn = msg->dn;
708 for (i=0; i<msg->num_elements; i++) {
709 struct ldb_message_element *el = &msg->elements[i], *el2;
710 struct ldb_val *vals;
711 const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(ldb, el->name);
712 const char *dn;
714 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
715 case LDB_FLAG_MOD_ADD:
717 if (el->num_values == 0) {
718 ldb_asprintf_errstring(ldb,
719 "attribute '%s': attribute on '%s' specified, but with 0 values (illegal)",
720 el->name, ldb_dn_get_linearized(msg2->dn));
721 ret = LDB_ERR_CONSTRAINT_VIOLATION;
722 goto done;
725 /* make a copy of the array so that a permissive
726 * control can remove duplicates without changing the
727 * original values, but do not copy data as we do not
728 * need to keep it around once the operation is
729 * finished */
730 if (control_permissive) {
731 el = talloc(msg2, struct ldb_message_element);
732 if (!el) {
733 ret = LDB_ERR_OTHER;
734 goto done;
736 *el = msg->elements[i];
737 el->values = talloc_array(el, struct ldb_val, el->num_values);
738 if (el->values == NULL) {
739 ret = LDB_ERR_OTHER;
740 goto done;
742 for (j = 0; j < el->num_values; j++) {
743 el->values[j] = msg->elements[i].values[j];
747 if (el->num_values > 1 && ldb_tdb_single_valued(a, el)) {
748 ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
749 el->name, ldb_dn_get_linearized(msg2->dn));
750 ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
751 goto done;
754 /* Checks if element already exists */
755 idx = find_element(msg2, el->name);
756 if (idx == -1) {
757 if (ltdb_msg_add_element(ldb, msg2, el) != 0) {
758 ret = LDB_ERR_OTHER;
759 goto done;
761 ret = ltdb_index_add_element(module, msg2->dn,
762 el);
763 if (ret != LDB_SUCCESS) {
764 goto done;
766 } else {
767 j = (unsigned int) idx;
768 el2 = &(msg2->elements[j]);
770 /* We cannot add another value on a existing one
771 if the attribute is single-valued */
772 if (ldb_tdb_single_valued(a, el)) {
773 ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
774 el->name, ldb_dn_get_linearized(msg2->dn));
775 ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
776 goto done;
779 /* Check that values don't exist yet on multi-
780 valued attributes or aren't provided twice */
781 /* TODO: This is O(n^2) - replace with more efficient check */
782 for (j = 0; j < el->num_values; j++) {
783 if (ldb_msg_find_val(el2, &el->values[j]) != NULL) {
784 if (control_permissive) {
785 /* remove this one as if it was never added */
786 el->num_values--;
787 for (k = j; k < el->num_values; k++) {
788 el->values[k] = el->values[k + 1];
790 j--; /* rewind */
792 continue;
795 ldb_asprintf_errstring(ldb,
796 "attribute '%s': value #%u on '%s' already exists",
797 el->name, j, ldb_dn_get_linearized(msg2->dn));
798 ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
799 goto done;
801 if (ldb_msg_find_val(el, &el->values[j]) != &el->values[j]) {
802 ldb_asprintf_errstring(ldb,
803 "attribute '%s': value #%u on '%s' provided more than once",
804 el->name, j, ldb_dn_get_linearized(msg2->dn));
805 ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
806 goto done;
810 /* Now combine existing and new values to a new
811 attribute record */
812 vals = talloc_realloc(msg2->elements,
813 el2->values, struct ldb_val,
814 el2->num_values + el->num_values);
815 if (vals == NULL) {
816 ldb_oom(ldb);
817 ret = LDB_ERR_OTHER;
818 goto done;
821 for (j=0; j<el->num_values; j++) {
822 vals[el2->num_values + j] =
823 ldb_val_dup(vals, &el->values[j]);
826 el2->values = vals;
827 el2->num_values += el->num_values;
829 ret = ltdb_index_add_element(module, msg2->dn, el);
830 if (ret != LDB_SUCCESS) {
831 goto done;
835 break;
837 case LDB_FLAG_MOD_REPLACE:
839 if (el->num_values > 1 && ldb_tdb_single_valued(a, el)) {
840 ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
841 el->name, ldb_dn_get_linearized(msg2->dn));
842 ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
843 goto done;
846 /* TODO: This is O(n^2) - replace with more efficient check */
847 for (j=0; j<el->num_values; j++) {
848 if (ldb_msg_find_val(el, &el->values[j]) != &el->values[j]) {
849 ldb_asprintf_errstring(ldb,
850 "attribute '%s': value #%u on '%s' provided more than once",
851 el->name, j, ldb_dn_get_linearized(msg2->dn));
852 ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
853 goto done;
857 /* Checks if element already exists */
858 idx = find_element(msg2, el->name);
859 if (idx != -1) {
860 j = (unsigned int) idx;
861 el2 = &(msg2->elements[j]);
863 /* we consider two elements to be
864 * equal only if the order
865 * matches. This allows dbcheck to
866 * fix the ordering on attributes
867 * where order matters, such as
868 * objectClass
870 if (ldb_msg_element_equal_ordered(el, el2)) {
871 continue;
874 /* Delete the attribute if it exists in the DB */
875 if (msg_delete_attribute(module, ldb, msg2,
876 el->name) != 0) {
877 ret = LDB_ERR_OTHER;
878 goto done;
882 /* Recreate it with the new values */
883 if (ltdb_msg_add_element(ldb, msg2, el) != 0) {
884 ret = LDB_ERR_OTHER;
885 goto done;
888 ret = ltdb_index_add_element(module, msg2->dn, el);
889 if (ret != LDB_SUCCESS) {
890 goto done;
893 break;
895 case LDB_FLAG_MOD_DELETE:
896 dn = ldb_dn_get_linearized(msg2->dn);
897 if (dn == NULL) {
898 ret = LDB_ERR_OTHER;
899 goto done;
902 if (msg->elements[i].num_values == 0) {
903 /* Delete the whole attribute */
904 ret = msg_delete_attribute(module, ldb, msg2,
905 msg->elements[i].name);
906 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE &&
907 control_permissive) {
908 ret = LDB_SUCCESS;
909 } else {
910 ldb_asprintf_errstring(ldb,
911 "attribute '%s': no such attribute for delete on '%s'",
912 msg->elements[i].name, dn);
914 if (ret != LDB_SUCCESS) {
915 goto done;
917 } else {
918 /* Delete specified values from an attribute */
919 for (j=0; j < msg->elements[i].num_values; j++) {
920 ret = msg_delete_element(module,
921 msg2,
922 msg->elements[i].name,
923 &msg->elements[i].values[j]);
924 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE &&
925 control_permissive) {
926 ret = LDB_SUCCESS;
927 } else {
928 ldb_asprintf_errstring(ldb,
929 "attribute '%s': no matching attribute value while deleting attribute on '%s'",
930 msg->elements[i].name, dn);
932 if (ret != LDB_SUCCESS) {
933 goto done;
937 break;
938 default:
939 ldb_asprintf_errstring(ldb,
940 "attribute '%s': invalid modify flags on '%s': 0x%x",
941 msg->elements[i].name, ldb_dn_get_linearized(msg->dn),
942 msg->elements[i].flags & LDB_FLAG_MOD_MASK);
943 ret = LDB_ERR_PROTOCOL_ERROR;
944 goto done;
948 ret = ltdb_store(module, msg2, TDB_MODIFY);
949 if (ret != LDB_SUCCESS) {
950 goto done;
953 ret = ltdb_modified(module, msg2->dn);
954 if (ret != LDB_SUCCESS) {
955 goto done;
958 done:
959 talloc_free(tdb_key.dptr);
960 return ret;
964 modify a record
966 static int ltdb_modify(struct ltdb_context *ctx)
968 struct ldb_module *module = ctx->module;
969 struct ldb_request *req = ctx->req;
970 int ret = LDB_SUCCESS;
972 ret = ltdb_check_special_dn(module, req->op.mod.message);
973 if (ret != LDB_SUCCESS) {
974 return ret;
977 ldb_request_set_state(req, LDB_ASYNC_PENDING);
979 if (ltdb_cache_load(module) != 0) {
980 return LDB_ERR_OPERATIONS_ERROR;
983 ret = ltdb_modify_internal(module, req->op.mod.message, req);
985 return ret;
989 rename a record
991 static int ltdb_rename(struct ltdb_context *ctx)
993 struct ldb_module *module = ctx->module;
994 void *data = ldb_module_get_private(module);
995 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
996 struct ldb_request *req = ctx->req;
997 struct ldb_message *msg;
998 int ret = LDB_SUCCESS;
999 TDB_DATA tdb_key, tdb_key_old;
1001 ldb_request_set_state(req, LDB_ASYNC_PENDING);
1003 if (ltdb_cache_load(ctx->module) != 0) {
1004 return LDB_ERR_OPERATIONS_ERROR;
1007 msg = ldb_msg_new(ctx);
1008 if (msg == NULL) {
1009 return LDB_ERR_OPERATIONS_ERROR;
1012 /* we need to fetch the old record to re-add under the new name */
1013 ret = ltdb_search_dn1(module, req->op.rename.olddn, msg);
1014 if (ret != LDB_SUCCESS) {
1015 /* not finding the old record is an error */
1016 return ret;
1019 /* We need to, before changing the DB, check if the new DN
1020 * exists, so we can return this error to the caller with an
1021 * unmodified DB */
1022 tdb_key = ltdb_key(module, req->op.rename.newdn);
1023 if (!tdb_key.dptr) {
1024 talloc_free(msg);
1025 return LDB_ERR_OPERATIONS_ERROR;
1028 tdb_key_old = ltdb_key(module, req->op.rename.olddn);
1029 if (!tdb_key_old.dptr) {
1030 talloc_free(msg);
1031 talloc_free(tdb_key.dptr);
1032 return LDB_ERR_OPERATIONS_ERROR;
1035 /* Only declare a conflict if the new DN already exists, and it isn't a case change on the old DN */
1036 if (tdb_key_old.dsize != tdb_key.dsize || memcmp(tdb_key.dptr, tdb_key_old.dptr, tdb_key.dsize) != 0) {
1037 if (tdb_exists(ltdb->tdb, tdb_key)) {
1038 talloc_free(tdb_key_old.dptr);
1039 talloc_free(tdb_key.dptr);
1040 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1041 "Entry %s already exists",
1042 ldb_dn_get_linearized(msg->dn));
1043 /* finding the new record already in the DB is an error */
1044 talloc_free(msg);
1045 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1048 talloc_free(tdb_key_old.dptr);
1049 talloc_free(tdb_key.dptr);
1051 /* Always delete first then add, to avoid conflicts with
1052 * unique indexes. We rely on the transaction to make this
1053 * atomic
1055 ret = ltdb_delete_internal(module, msg->dn);
1056 if (ret != LDB_SUCCESS) {
1057 talloc_free(msg);
1058 return ret;
1061 msg->dn = ldb_dn_copy(msg, req->op.rename.newdn);
1062 if (msg->dn == NULL) {
1063 talloc_free(msg);
1064 return LDB_ERR_OPERATIONS_ERROR;
1067 /* We don't check single value as we can have more than 1 with
1068 * deleted attributes. We could go through all elements but that's
1069 * maybe not the most efficient way
1071 ret = ltdb_add_internal(module, msg, false);
1073 talloc_free(msg);
1075 return ret;
1078 static int ltdb_start_trans(struct ldb_module *module)
1080 void *data = ldb_module_get_private(module);
1081 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
1083 if (tdb_transaction_start(ltdb->tdb) != 0) {
1084 return ltdb_err_map(tdb_error(ltdb->tdb));
1087 ltdb->in_transaction++;
1089 ltdb_index_transaction_start(module);
1091 return LDB_SUCCESS;
1094 static int ltdb_prepare_commit(struct ldb_module *module)
1096 void *data = ldb_module_get_private(module);
1097 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
1099 if (ltdb->in_transaction != 1) {
1100 return LDB_SUCCESS;
1103 if (ltdb_index_transaction_commit(module) != 0) {
1104 tdb_transaction_cancel(ltdb->tdb);
1105 ltdb->in_transaction--;
1106 return ltdb_err_map(tdb_error(ltdb->tdb));
1109 if (tdb_transaction_prepare_commit(ltdb->tdb) != 0) {
1110 ltdb->in_transaction--;
1111 return ltdb_err_map(tdb_error(ltdb->tdb));
1114 ltdb->prepared_commit = true;
1116 return LDB_SUCCESS;
1119 static int ltdb_end_trans(struct ldb_module *module)
1121 void *data = ldb_module_get_private(module);
1122 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
1124 if (!ltdb->prepared_commit) {
1125 int ret = ltdb_prepare_commit(module);
1126 if (ret != LDB_SUCCESS) {
1127 return ret;
1131 ltdb->in_transaction--;
1132 ltdb->prepared_commit = false;
1134 if (tdb_transaction_commit(ltdb->tdb) != 0) {
1135 return ltdb_err_map(tdb_error(ltdb->tdb));
1138 return LDB_SUCCESS;
1141 static int ltdb_del_trans(struct ldb_module *module)
1143 void *data = ldb_module_get_private(module);
1144 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
1146 ltdb->in_transaction--;
1148 if (ltdb_index_transaction_cancel(module) != 0) {
1149 tdb_transaction_cancel(ltdb->tdb);
1150 return ltdb_err_map(tdb_error(ltdb->tdb));
1153 tdb_transaction_cancel(ltdb->tdb);
1154 return LDB_SUCCESS;
1158 return sequenceNumber from @BASEINFO
1160 static int ltdb_sequence_number(struct ltdb_context *ctx,
1161 struct ldb_extended **ext)
1163 struct ldb_context *ldb;
1164 struct ldb_module *module = ctx->module;
1165 struct ldb_request *req = ctx->req;
1166 TALLOC_CTX *tmp_ctx = NULL;
1167 struct ldb_seqnum_request *seq;
1168 struct ldb_seqnum_result *res;
1169 struct ldb_message *msg = NULL;
1170 struct ldb_dn *dn;
1171 const char *date;
1172 int ret = LDB_SUCCESS;
1174 ldb = ldb_module_get_ctx(module);
1176 seq = talloc_get_type(req->op.extended.data,
1177 struct ldb_seqnum_request);
1178 if (seq == NULL) {
1179 return LDB_ERR_OPERATIONS_ERROR;
1182 ldb_request_set_state(req, LDB_ASYNC_PENDING);
1184 if (ltdb_lock_read(module) != 0) {
1185 return LDB_ERR_OPERATIONS_ERROR;
1188 res = talloc_zero(req, struct ldb_seqnum_result);
1189 if (res == NULL) {
1190 ret = LDB_ERR_OPERATIONS_ERROR;
1191 goto done;
1194 tmp_ctx = talloc_new(req);
1195 if (tmp_ctx == NULL) {
1196 ret = LDB_ERR_OPERATIONS_ERROR;
1197 goto done;
1200 dn = ldb_dn_new(tmp_ctx, ldb, LTDB_BASEINFO);
1201 if (dn == NULL) {
1202 ret = LDB_ERR_OPERATIONS_ERROR;
1203 goto done;
1206 msg = ldb_msg_new(tmp_ctx);
1207 if (msg == NULL) {
1208 ret = LDB_ERR_OPERATIONS_ERROR;
1209 goto done;
1212 ret = ltdb_search_dn1(module, dn, msg);
1213 if (ret != LDB_SUCCESS) {
1214 goto done;
1217 switch (seq->type) {
1218 case LDB_SEQ_HIGHEST_SEQ:
1219 res->seq_num = ldb_msg_find_attr_as_uint64(msg, LTDB_SEQUENCE_NUMBER, 0);
1220 break;
1221 case LDB_SEQ_NEXT:
1222 res->seq_num = ldb_msg_find_attr_as_uint64(msg, LTDB_SEQUENCE_NUMBER, 0);
1223 res->seq_num++;
1224 break;
1225 case LDB_SEQ_HIGHEST_TIMESTAMP:
1226 date = ldb_msg_find_attr_as_string(msg, LTDB_MOD_TIMESTAMP, NULL);
1227 if (date) {
1228 res->seq_num = ldb_string_to_time(date);
1229 } else {
1230 res->seq_num = 0;
1231 /* zero is as good as anything when we don't know */
1233 break;
1236 *ext = talloc_zero(req, struct ldb_extended);
1237 if (*ext == NULL) {
1238 ret = LDB_ERR_OPERATIONS_ERROR;
1239 goto done;
1241 (*ext)->oid = LDB_EXTENDED_SEQUENCE_NUMBER;
1242 (*ext)->data = talloc_steal(*ext, res);
1244 done:
1245 talloc_free(tmp_ctx);
1246 ltdb_unlock_read(module);
1247 return ret;
1250 static void ltdb_request_done(struct ltdb_context *ctx, int error)
1252 struct ldb_context *ldb;
1253 struct ldb_request *req;
1254 struct ldb_reply *ares;
1256 ldb = ldb_module_get_ctx(ctx->module);
1257 req = ctx->req;
1259 /* if we already returned an error just return */
1260 if (ldb_request_get_status(req) != LDB_SUCCESS) {
1261 return;
1264 ares = talloc_zero(req, struct ldb_reply);
1265 if (!ares) {
1266 ldb_oom(ldb);
1267 req->callback(req, NULL);
1268 return;
1270 ares->type = LDB_REPLY_DONE;
1271 ares->error = error;
1273 req->callback(req, ares);
1276 static void ltdb_timeout(struct tevent_context *ev,
1277 struct tevent_timer *te,
1278 struct timeval t,
1279 void *private_data)
1281 struct ltdb_context *ctx;
1282 ctx = talloc_get_type(private_data, struct ltdb_context);
1284 if (!ctx->request_terminated) {
1285 /* request is done now */
1286 ltdb_request_done(ctx, LDB_ERR_TIME_LIMIT_EXCEEDED);
1289 if (ctx->spy) {
1290 /* neutralize the spy */
1291 ctx->spy->ctx = NULL;
1292 ctx->spy = NULL;
1294 talloc_free(ctx);
1297 static void ltdb_request_extended_done(struct ltdb_context *ctx,
1298 struct ldb_extended *ext,
1299 int error)
1301 struct ldb_context *ldb;
1302 struct ldb_request *req;
1303 struct ldb_reply *ares;
1305 ldb = ldb_module_get_ctx(ctx->module);
1306 req = ctx->req;
1308 /* if we already returned an error just return */
1309 if (ldb_request_get_status(req) != LDB_SUCCESS) {
1310 return;
1313 ares = talloc_zero(req, struct ldb_reply);
1314 if (!ares) {
1315 ldb_oom(ldb);
1316 req->callback(req, NULL);
1317 return;
1319 ares->type = LDB_REPLY_DONE;
1320 ares->response = ext;
1321 ares->error = error;
1323 req->callback(req, ares);
1326 static void ltdb_handle_extended(struct ltdb_context *ctx)
1328 struct ldb_extended *ext = NULL;
1329 int ret;
1331 if (strcmp(ctx->req->op.extended.oid,
1332 LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1333 /* get sequence number */
1334 ret = ltdb_sequence_number(ctx, &ext);
1335 } else {
1336 /* not recognized */
1337 ret = LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
1340 ltdb_request_extended_done(ctx, ext, ret);
1343 static void ltdb_callback(struct tevent_context *ev,
1344 struct tevent_timer *te,
1345 struct timeval t,
1346 void *private_data)
1348 struct ltdb_context *ctx;
1349 int ret;
1351 ctx = talloc_get_type(private_data, struct ltdb_context);
1353 if (ctx->request_terminated) {
1354 goto done;
1357 switch (ctx->req->operation) {
1358 case LDB_SEARCH:
1359 ret = ltdb_search(ctx);
1360 break;
1361 case LDB_ADD:
1362 ret = ltdb_add(ctx);
1363 break;
1364 case LDB_MODIFY:
1365 ret = ltdb_modify(ctx);
1366 break;
1367 case LDB_DELETE:
1368 ret = ltdb_delete(ctx);
1369 break;
1370 case LDB_RENAME:
1371 ret = ltdb_rename(ctx);
1372 break;
1373 case LDB_EXTENDED:
1374 ltdb_handle_extended(ctx);
1375 goto done;
1376 default:
1377 /* no other op supported */
1378 ret = LDB_ERR_PROTOCOL_ERROR;
1381 if (!ctx->request_terminated) {
1382 /* request is done now */
1383 ltdb_request_done(ctx, ret);
1386 done:
1387 if (ctx->spy) {
1388 /* neutralize the spy */
1389 ctx->spy->ctx = NULL;
1390 ctx->spy = NULL;
1392 talloc_free(ctx);
1395 static int ltdb_request_destructor(void *ptr)
1397 struct ltdb_req_spy *spy = talloc_get_type(ptr, struct ltdb_req_spy);
1399 if (spy->ctx != NULL) {
1400 spy->ctx->spy = NULL;
1401 spy->ctx->request_terminated = true;
1402 spy->ctx = NULL;
1405 return 0;
1408 static int ltdb_handle_request(struct ldb_module *module,
1409 struct ldb_request *req)
1411 struct ldb_control *control_permissive;
1412 struct ldb_context *ldb;
1413 struct tevent_context *ev;
1414 struct ltdb_context *ac;
1415 struct tevent_timer *te;
1416 struct timeval tv;
1417 unsigned int i;
1419 ldb = ldb_module_get_ctx(module);
1421 control_permissive = ldb_request_get_control(req,
1422 LDB_CONTROL_PERMISSIVE_MODIFY_OID);
1424 for (i = 0; req->controls && req->controls[i]; i++) {
1425 if (req->controls[i]->critical &&
1426 req->controls[i] != control_permissive) {
1427 ldb_asprintf_errstring(ldb, "Unsupported critical extension %s",
1428 req->controls[i]->oid);
1429 return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
1433 if (req->starttime == 0 || req->timeout == 0) {
1434 ldb_set_errstring(ldb, "Invalid timeout settings");
1435 return LDB_ERR_TIME_LIMIT_EXCEEDED;
1438 ev = ldb_get_event_context(ldb);
1440 ac = talloc_zero(ldb, struct ltdb_context);
1441 if (ac == NULL) {
1442 ldb_oom(ldb);
1443 return LDB_ERR_OPERATIONS_ERROR;
1446 ac->module = module;
1447 ac->req = req;
1449 tv.tv_sec = 0;
1450 tv.tv_usec = 0;
1451 te = tevent_add_timer(ev, ac, tv, ltdb_callback, ac);
1452 if (NULL == te) {
1453 talloc_free(ac);
1454 return LDB_ERR_OPERATIONS_ERROR;
1457 tv.tv_sec = req->starttime + req->timeout;
1458 ac->timeout_event = tevent_add_timer(ev, ac, tv, ltdb_timeout, ac);
1459 if (NULL == ac->timeout_event) {
1460 talloc_free(ac);
1461 return LDB_ERR_OPERATIONS_ERROR;
1464 /* set a spy so that we do not try to use the request context
1465 * if it is freed before ltdb_callback fires */
1466 ac->spy = talloc(req, struct ltdb_req_spy);
1467 if (NULL == ac->spy) {
1468 talloc_free(ac);
1469 return LDB_ERR_OPERATIONS_ERROR;
1471 ac->spy->ctx = ac;
1473 talloc_set_destructor((TALLOC_CTX *)ac->spy, ltdb_request_destructor);
1475 return LDB_SUCCESS;
1478 static int ltdb_init_rootdse(struct ldb_module *module)
1480 /* ignore errors on this - we expect it for non-sam databases */
1481 ldb_mod_register_control(module, LDB_CONTROL_PERMISSIVE_MODIFY_OID);
1483 /* there can be no module beyond the backend, just return */
1484 return LDB_SUCCESS;
1487 static const struct ldb_module_ops ltdb_ops = {
1488 .name = "tdb",
1489 .init_context = ltdb_init_rootdse,
1490 .search = ltdb_handle_request,
1491 .add = ltdb_handle_request,
1492 .modify = ltdb_handle_request,
1493 .del = ltdb_handle_request,
1494 .rename = ltdb_handle_request,
1495 .extended = ltdb_handle_request,
1496 .start_transaction = ltdb_start_trans,
1497 .end_transaction = ltdb_end_trans,
1498 .prepare_commit = ltdb_prepare_commit,
1499 .del_transaction = ltdb_del_trans,
1503 connect to the database
1505 static int ltdb_connect(struct ldb_context *ldb, const char *url,
1506 unsigned int flags, const char *options[],
1507 struct ldb_module **_module)
1509 struct ldb_module *module;
1510 const char *path;
1511 int tdb_flags, open_flags;
1512 struct ltdb_private *ltdb;
1514 /* parse the url */
1515 if (strchr(url, ':')) {
1516 if (strncmp(url, "tdb://", 6) != 0) {
1517 ldb_debug(ldb, LDB_DEBUG_ERROR,
1518 "Invalid tdb URL '%s'", url);
1519 return LDB_ERR_OPERATIONS_ERROR;
1521 path = url+6;
1522 } else {
1523 path = url;
1526 tdb_flags = TDB_DEFAULT | TDB_SEQNUM;
1528 /* check for the 'nosync' option */
1529 if (flags & LDB_FLG_NOSYNC) {
1530 tdb_flags |= TDB_NOSYNC;
1533 /* and nommap option */
1534 if (flags & LDB_FLG_NOMMAP) {
1535 tdb_flags |= TDB_NOMMAP;
1538 if (flags & LDB_FLG_RDONLY) {
1539 open_flags = O_RDONLY;
1540 } else {
1541 open_flags = O_CREAT | O_RDWR;
1544 ltdb = talloc_zero(ldb, struct ltdb_private);
1545 if (!ltdb) {
1546 ldb_oom(ldb);
1547 return LDB_ERR_OPERATIONS_ERROR;
1550 /* note that we use quite a large default hash size */
1551 ltdb->tdb = ltdb_wrap_open(ltdb, path, 10000,
1552 tdb_flags, open_flags,
1553 ldb_get_create_perms(ldb), ldb);
1554 if (!ltdb->tdb) {
1555 ldb_asprintf_errstring(ldb,
1556 "Unable to open tdb '%s'", path);
1557 ldb_debug(ldb, LDB_DEBUG_ERROR,
1558 "Unable to open tdb '%s'", path);
1559 talloc_free(ltdb);
1560 return LDB_ERR_OPERATIONS_ERROR;
1563 if (getenv("LDB_WARN_UNINDEXED")) {
1564 ltdb->warn_unindexed = true;
1567 ltdb->sequence_number = 0;
1569 module = ldb_module_new(ldb, ldb, "ldb_tdb backend", &ltdb_ops);
1570 if (!module) {
1571 ldb_oom(ldb);
1572 talloc_free(ltdb);
1573 return LDB_ERR_OPERATIONS_ERROR;
1575 ldb_module_set_private(module, ltdb);
1576 talloc_steal(module, ltdb);
1578 if (ltdb_cache_load(module) != 0) {
1579 ldb_asprintf_errstring(ldb,
1580 "Unable to load ltdb cache records of tdb '%s'", path);
1581 talloc_free(module);
1582 return LDB_ERR_OPERATIONS_ERROR;
1585 *_module = module;
1586 return LDB_SUCCESS;
1589 int ldb_tdb_init(const char *version)
1591 LDB_MODULE_CHECK_VERSION(version);
1592 return ldb_register_backend("tdb", ltdb_connect, false);