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
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/>.
30 * Component: ldb key value backend
32 * Description: core functions for ldb key value backend
34 * Author: Andrew Tridgell
35 * Author: Stefan Metzmacher
39 * - description: make the module use asynchronous calls
43 * - description: make it possible to use event contexts
47 * - description: fix up memory leaks and small bugs
49 * Author: Matthias Dieter Wallnöfer
53 #include "ldb_private.h"
56 prevent memory errors on callbacks
58 struct ldb_kv_req_spy {
59 struct ldb_kv_context *ctx;
63 * Determine if this key could hold a record. We allow the new GUID
64 * index, the old DN index and a possible future ID=
66 bool ldb_kv_key_is_record(struct ldb_val key)
72 if (memcmp(key.data, "DN=", 3) == 0) {
76 if (memcmp(key.data, "ID=", 3) == 0) {
80 if (key.length < sizeof(LDB_KV_GUID_KEY_PREFIX)) {
84 if (memcmp(key.data, LDB_KV_GUID_KEY_PREFIX,
85 sizeof(LDB_KV_GUID_KEY_PREFIX) - 1) == 0) {
93 form a ldb_val for a record key
96 note that the key for a record can depend on whether the
97 dn refers to a case sensitive index record or not
99 struct ldb_val ldb_kv_key_dn(struct ldb_module *module,
104 char *key_str = NULL;
105 const char *dn_folded = NULL;
108 most DNs are case insensitive. The exception is index DNs for
109 case sensitive attributes
111 there are 3 cases dealt with in this code:
113 1) if the dn doesn't start with @ then uppercase the attribute
114 names and the attributes values of case insensitive attributes
115 2) if the dn starts with @ then leave it alone -
116 the indexing code handles the rest
119 dn_folded = ldb_dn_get_casefold(dn);
124 key_str = talloc_strdup(mem_ctx, "DN=");
129 key_str = talloc_strdup_append_buffer(key_str, dn_folded);
134 key.data = (uint8_t *)key_str;
135 key.length = strlen(key_str) + 1;
146 /* The caller is to provide a correctly sized key */
147 int ldb_kv_guid_to_key(struct ldb_module *module,
148 struct ldb_kv_private *ldb_kv,
149 const struct ldb_val *GUID_val,
152 const char *GUID_prefix = LDB_KV_GUID_KEY_PREFIX;
153 const int GUID_prefix_len = sizeof(LDB_KV_GUID_KEY_PREFIX) - 1;
155 if (key->length != (GUID_val->length+GUID_prefix_len)) {
156 return LDB_ERR_OPERATIONS_ERROR;
159 memcpy(key->data, GUID_prefix, GUID_prefix_len);
160 memcpy(&key->data[GUID_prefix_len],
161 GUID_val->data, GUID_val->length);
166 * The caller is to provide a correctly sized key, used only in
167 * the GUID index mode
169 int ldb_kv_idx_to_key(struct ldb_module *module,
170 struct ldb_kv_private *ldb_kv,
172 const struct ldb_val *idx_val,
175 struct ldb_context *ldb = ldb_module_get_ctx(module);
178 if (ldb_kv->cache->GUID_index_attribute != NULL) {
179 return ldb_kv_guid_to_key(module, ldb_kv, idx_val, key);
182 dn = ldb_dn_from_ldb_val(mem_ctx, ldb, idx_val);
185 * LDB_ERR_INVALID_DN_SYNTAX would just be confusing
186 * to the caller, as this in an invalid index value
188 return LDB_ERR_OPERATIONS_ERROR;
191 *key = ldb_kv_key_dn(module, mem_ctx, dn);
194 return ldb_module_oom(module);
200 form a TDB_DATA for a record key
201 caller frees mem_ctx, which may or may not have the key
204 note that the key for a record can depend on whether a
205 GUID index is in use, or the DN is used as the key
207 struct ldb_val ldb_kv_key_msg(struct ldb_module *module,
209 const struct ldb_message *msg)
211 void *data = ldb_module_get_private(module);
212 struct ldb_kv_private *ldb_kv =
213 talloc_get_type(data, struct ldb_kv_private);
215 const struct ldb_val *guid_val;
218 if (ldb_kv->cache->GUID_index_attribute == NULL) {
219 return ldb_kv_key_dn(module, mem_ctx, msg->dn);
222 if (ldb_dn_is_special(msg->dn)) {
223 return ldb_kv_key_dn(module, mem_ctx, msg->dn);
227 ldb_msg_find_ldb_val(msg, ldb_kv->cache->GUID_index_attribute);
228 if (guid_val == NULL) {
229 ldb_asprintf_errstring(ldb_module_get_ctx(module),
230 "Did not find GUID attribute %s "
231 "in %s, required for TDB record "
232 "key in " LDB_KV_IDXGUID " mode.",
233 ldb_kv->cache->GUID_index_attribute,
234 ldb_dn_get_linearized(msg->dn));
241 /* In this case, allocate with talloc */
242 key.data = talloc_size(mem_ctx, LDB_KV_GUID_KEY_SIZE);
243 if (key.data == NULL) {
249 key.length = talloc_get_size(key.data);
251 ret = ldb_kv_guid_to_key(module, ldb_kv, guid_val, &key);
253 if (ret != LDB_SUCCESS) {
263 check special dn's have valid attributes
264 currently only @ATTRIBUTES is checked
266 static int ldb_kv_check_special_dn(struct ldb_module *module,
267 const struct ldb_message *msg)
269 struct ldb_context *ldb = ldb_module_get_ctx(module);
272 if (! ldb_dn_is_special(msg->dn) ||
273 ! ldb_dn_check_special(msg->dn, LDB_KV_ATTRIBUTES)) {
277 /* we have @ATTRIBUTES, let's check attributes are fine */
278 /* should we check that we deny multivalued attributes ? */
279 for (i = 0; i < msg->num_elements; i++) {
280 if (ldb_attr_cmp(msg->elements[i].name, "distinguishedName") == 0) continue;
282 for (j = 0; j < msg->elements[i].num_values; j++) {
283 if (ldb_kv_check_at_attributes_values(
284 &msg->elements[i].values[j]) != 0) {
285 ldb_set_errstring(ldb, "Invalid attribute value in an @ATTRIBUTES entry");
286 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
296 we've made a modification to a dn - possibly reindex and
297 update sequence number
299 static int ldb_kv_modified(struct ldb_module *module, struct ldb_dn *dn)
301 int ret = LDB_SUCCESS;
302 struct ldb_kv_private *ldb_kv = talloc_get_type(
303 ldb_module_get_private(module), struct ldb_kv_private);
305 /* only allow modifies inside a transaction, otherwise the
307 if (ldb_kv->kv_ops->transaction_active(ldb_kv) == false) {
308 ldb_set_errstring(ldb_module_get_ctx(module), "ltdb modify without transaction");
309 return LDB_ERR_OPERATIONS_ERROR;
312 if (ldb_dn_is_special(dn) &&
313 (ldb_dn_check_special(dn, LDB_KV_INDEXLIST) ||
314 ldb_dn_check_special(dn, LDB_KV_ATTRIBUTES)) )
316 if (ldb_kv->warn_reindex) {
317 ldb_debug(ldb_module_get_ctx(module),
319 "Reindexing %s due to modification on %s",
320 ldb_kv->kv_ops->name(ldb_kv),
321 ldb_dn_get_linearized(dn));
323 ret = ldb_kv_reindex(module);
326 /* If the modify was to a normal record, or any special except @BASEINFO, update the seq number */
327 if (ret == LDB_SUCCESS &&
328 !(ldb_dn_is_special(dn) &&
329 ldb_dn_check_special(dn, LDB_KV_BASEINFO)) ) {
330 ret = ldb_kv_increase_sequence_number(module);
333 /* If the modify was to @OPTIONS, reload the cache */
334 if (ret == LDB_SUCCESS &&
335 ldb_dn_is_special(dn) &&
336 (ldb_dn_check_special(dn, LDB_KV_OPTIONS)) ) {
337 ret = ldb_kv_cache_reload(module);
340 if (ret != LDB_SUCCESS) {
341 ldb_kv->reindex_failed = true;
347 store a record into the db
349 int ldb_kv_store(struct ldb_module *module,
350 const struct ldb_message *msg,
353 void *data = ldb_module_get_private(module);
354 struct ldb_kv_private *ldb_kv =
355 talloc_get_type(data, struct ldb_kv_private);
357 struct ldb_val ldb_data;
358 int ret = LDB_SUCCESS;
359 TALLOC_CTX *key_ctx = talloc_new(module);
361 if (key_ctx == NULL) {
362 return ldb_module_oom(module);
365 if (ldb_kv->read_only) {
366 talloc_free(key_ctx);
367 return LDB_ERR_UNWILLING_TO_PERFORM;
370 key = ldb_kv_key_msg(module, key_ctx, msg);
371 if (key.data == NULL) {
372 TALLOC_FREE(key_ctx);
373 return LDB_ERR_OTHER;
376 ret = ldb_pack_data(ldb_module_get_ctx(module),
379 TALLOC_FREE(key_ctx);
380 return LDB_ERR_OTHER;
383 ret = ldb_kv->kv_ops->store(ldb_kv, key, ldb_data, flgs);
385 bool is_special = ldb_dn_is_special(msg->dn);
386 ret = ldb_kv->kv_ops->error(ldb_kv);
389 * LDB_ERR_ENTRY_ALREADY_EXISTS means the DN, not
390 * the GUID, so re-map
392 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS && !is_special &&
393 ldb_kv->cache->GUID_index_attribute != NULL) {
394 ret = LDB_ERR_CONSTRAINT_VIOLATION;
400 TALLOC_FREE(key_ctx);
401 talloc_free(ldb_data.data);
408 check if a attribute is a single valued, for a given element
410 static bool ldb_kv_single_valued(const struct ldb_schema_attribute *a,
411 struct ldb_message_element *el)
413 if (!a) return false;
415 if (el->flags & LDB_FLAG_INTERNAL_FORCE_SINGLE_VALUE_CHECK) {
416 /* override from a ldb module, for example
417 used for the description field, which is
418 marked multi-valued in the schema but which
419 should not actually accept multiple
423 if (el->flags & LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK) {
424 /* override from a ldb module, for example used for
425 deleted linked attribute entries */
429 if (a->flags & LDB_ATTR_FLAG_SINGLE_VALUE) {
435 static int ldb_kv_add_internal(struct ldb_module *module,
436 struct ldb_kv_private *ldb_kv,
437 const struct ldb_message *msg,
438 bool check_single_value)
440 struct ldb_context *ldb = ldb_module_get_ctx(module);
441 int ret = LDB_SUCCESS;
444 for (i=0;i<msg->num_elements;i++) {
445 struct ldb_message_element *el = &msg->elements[i];
446 const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(ldb, el->name);
448 if (el->num_values == 0) {
449 ldb_asprintf_errstring(ldb, "attribute '%s' on '%s' specified, but with 0 values (illegal)",
450 el->name, ldb_dn_get_linearized(msg->dn));
451 return LDB_ERR_CONSTRAINT_VIOLATION;
453 if (check_single_value && el->num_values > 1 &&
454 ldb_kv_single_valued(a, el)) {
455 ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
456 el->name, ldb_dn_get_linearized(msg->dn));
457 return LDB_ERR_CONSTRAINT_VIOLATION;
460 /* Do not check "@ATTRIBUTES" for duplicated values */
461 if (ldb_dn_is_special(msg->dn) &&
462 ldb_dn_check_special(msg->dn, LDB_KV_ATTRIBUTES)) {
466 if (check_single_value &&
468 LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK)) {
469 struct ldb_val *duplicate = NULL;
471 ret = ldb_msg_find_duplicate_val(ldb, discard_const(msg),
473 if (ret != LDB_SUCCESS) {
476 if (duplicate != NULL) {
477 ldb_asprintf_errstring(
479 "attribute '%s': value '%.*s' on '%s' "
480 "provided more than once in ADD object",
482 (int)duplicate->length,
484 ldb_dn_get_linearized(msg->dn));
485 return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
490 ret = ldb_kv_store(module, msg, TDB_INSERT);
491 if (ret != LDB_SUCCESS) {
493 * Try really hard to get the right error code for
494 * a re-add situation, as this can matter!
496 if (ret == LDB_ERR_CONSTRAINT_VIOLATION) {
498 struct ldb_dn *dn2 = NULL;
499 TALLOC_CTX *mem_ctx = talloc_new(module);
500 if (mem_ctx == NULL) {
501 return ldb_module_operr(module);
504 ldb_kv_search_base(module, mem_ctx, msg->dn, &dn2);
505 TALLOC_FREE(mem_ctx);
506 if (ret2 == LDB_SUCCESS) {
507 ret = LDB_ERR_ENTRY_ALREADY_EXISTS;
510 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
511 ldb_asprintf_errstring(ldb,
512 "Entry %s already exists",
513 ldb_dn_get_linearized(msg->dn));
518 ret = ldb_kv_index_add_new(module, ldb_kv, msg);
519 if (ret != LDB_SUCCESS) {
521 * If we failed to index, delete the message again.
523 * This is particularly important for the GUID index
524 * case, which will only fail for a duplicate DN
527 * Note that the caller may not cancel the transation
528 * and this means the above add might really show up!
530 ldb_kv_delete_noindex(module, msg);
534 ret = ldb_kv_modified(module, msg->dn);
540 add a record to the database
542 static int ldb_kv_add(struct ldb_kv_context *ctx)
544 struct ldb_module *module = ctx->module;
545 struct ldb_request *req = ctx->req;
546 void *data = ldb_module_get_private(module);
547 struct ldb_kv_private *ldb_kv =
548 talloc_get_type(data, struct ldb_kv_private);
549 int ret = LDB_SUCCESS;
551 if (ldb_kv->max_key_length != 0 &&
552 ldb_kv->cache->GUID_index_attribute == NULL &&
553 !ldb_dn_is_special(req->op.add.message->dn)) {
554 ldb_set_errstring(ldb_module_get_ctx(module),
555 "Must operate ldb_mdb in GUID "
556 "index mode, but " LDB_KV_IDXGUID " not set.");
557 return LDB_ERR_UNWILLING_TO_PERFORM;
560 ret = ldb_kv_check_special_dn(module, req->op.add.message);
561 if (ret != LDB_SUCCESS) {
565 ldb_request_set_state(req, LDB_ASYNC_PENDING);
567 if (ldb_kv_cache_load(module) != 0) {
568 return LDB_ERR_OPERATIONS_ERROR;
571 ret = ldb_kv_add_internal(module, ldb_kv, req->op.add.message, true);
577 delete a record from the database, not updating indexes (used for deleting
580 int ldb_kv_delete_noindex(struct ldb_module *module,
581 const struct ldb_message *msg)
583 void *data = ldb_module_get_private(module);
584 struct ldb_kv_private *ldb_kv =
585 talloc_get_type(data, struct ldb_kv_private);
588 TALLOC_CTX *tdb_key_ctx = talloc_new(module);
590 if (tdb_key_ctx == NULL) {
591 return ldb_module_oom(module);
594 if (ldb_kv->read_only) {
595 talloc_free(tdb_key_ctx);
596 return LDB_ERR_UNWILLING_TO_PERFORM;
599 key = ldb_kv_key_msg(module, tdb_key_ctx, msg);
601 TALLOC_FREE(tdb_key_ctx);
602 return LDB_ERR_OTHER;
605 ret = ldb_kv->kv_ops->delete (ldb_kv, key);
606 TALLOC_FREE(tdb_key_ctx);
609 ret = ldb_kv->kv_ops->error(ldb_kv);
615 static int ldb_kv_delete_internal(struct ldb_module *module, struct ldb_dn *dn)
617 struct ldb_message *msg;
618 int ret = LDB_SUCCESS;
620 msg = ldb_msg_new(module);
622 return LDB_ERR_OPERATIONS_ERROR;
625 /* in case any attribute of the message was indexed, we need
626 to fetch the old record */
627 ret = ldb_kv_search_dn1(
628 module, dn, msg, LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC);
629 if (ret != LDB_SUCCESS) {
630 /* not finding the old record is an error */
634 ret = ldb_kv_delete_noindex(module, msg);
635 if (ret != LDB_SUCCESS) {
639 /* remove any indexed attributes */
640 ret = ldb_kv_index_delete(module, msg);
641 if (ret != LDB_SUCCESS) {
645 ret = ldb_kv_modified(module, dn);
646 if (ret != LDB_SUCCESS) {
656 delete a record from the database
658 static int ldb_kv_delete(struct ldb_kv_context *ctx)
660 struct ldb_module *module = ctx->module;
661 struct ldb_request *req = ctx->req;
662 int ret = LDB_SUCCESS;
664 ldb_request_set_state(req, LDB_ASYNC_PENDING);
666 if (ldb_kv_cache_load(module) != 0) {
667 return LDB_ERR_OPERATIONS_ERROR;
670 ret = ldb_kv_delete_internal(module, req->op.del.dn);
676 find an element by attribute name. At the moment this does a linear search,
677 it should be re-coded to use a binary search once all places that modify
678 records guarantee sorted order
680 return the index of the first matching element if found, otherwise -1
682 static int ldb_kv_find_element(const struct ldb_message *msg, const char *name)
685 for (i=0;i<msg->num_elements;i++) {
686 if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
695 add an element to an existing record. Assumes a elements array that we
696 can call re-alloc on, and assumed that we can re-use the data pointers from
697 the passed in additional values. Use with care!
699 returns 0 on success, -1 on failure (and sets errno)
701 static int ldb_kv_msg_add_element(struct ldb_message *msg,
702 struct ldb_message_element *el)
704 struct ldb_message_element *e2;
707 if (el->num_values == 0) {
708 /* nothing to do here - we don't add empty elements */
712 e2 = talloc_realloc(msg, msg->elements, struct ldb_message_element,
713 msg->num_elements+1);
721 e2 = &msg->elements[msg->num_elements];
724 e2->flags = el->flags;
725 e2->values = talloc_array(msg->elements,
726 struct ldb_val, el->num_values);
731 for (i=0;i<el->num_values;i++) {
732 e2->values[i] = el->values[i];
734 e2->num_values = el->num_values;
742 delete all elements having a specified attribute name
744 static int ldb_kv_msg_delete_attribute(struct ldb_module *module,
745 struct ldb_kv_private *ldb_kv,
746 struct ldb_message *msg,
751 struct ldb_message_element *el;
752 bool is_special = ldb_dn_is_special(msg->dn);
754 if (!is_special && ldb_kv->cache->GUID_index_attribute != NULL &&
755 ldb_attr_cmp(name, ldb_kv->cache->GUID_index_attribute) == 0) {
756 struct ldb_context *ldb = ldb_module_get_ctx(module);
757 ldb_asprintf_errstring(ldb,
758 "Must not modify GUID "
759 "attribute %s (used as DB index)",
760 ldb_kv->cache->GUID_index_attribute);
761 return LDB_ERR_CONSTRAINT_VIOLATION;
764 el = ldb_msg_find_element(msg, name);
766 return LDB_ERR_NO_SUCH_ATTRIBUTE;
768 i = el - msg->elements;
770 ret = ldb_kv_index_del_element(module, ldb_kv, msg, el);
771 if (ret != LDB_SUCCESS) {
775 talloc_free(el->values);
776 if (msg->num_elements > (i+1)) {
777 memmove(el, el+1, sizeof(*el) * (msg->num_elements - (i+1)));
780 msg->elements = talloc_realloc(msg, msg->elements,
781 struct ldb_message_element,
787 delete all elements matching an attribute name/value
789 return LDB Error on failure
791 static int ldb_kv_msg_delete_element(struct ldb_module *module,
792 struct ldb_kv_private *ldb_kv,
793 struct ldb_message *msg,
795 const struct ldb_val *val)
797 struct ldb_context *ldb = ldb_module_get_ctx(module);
800 struct ldb_message_element *el;
801 const struct ldb_schema_attribute *a;
803 found = ldb_kv_find_element(msg, name);
805 return LDB_ERR_NO_SUCH_ATTRIBUTE;
808 i = (unsigned int) found;
809 el = &(msg->elements[i]);
811 a = ldb_schema_attribute_by_name(ldb, el->name);
813 for (i=0;i<el->num_values;i++) {
815 if (a->syntax->operator_fn) {
816 ret = a->syntax->operator_fn(ldb, LDB_OP_EQUALITY, a,
817 &el->values[i], val, &matched);
818 if (ret != LDB_SUCCESS) return ret;
820 matched = (a->syntax->comparison_fn(ldb, ldb,
821 &el->values[i], val) == 0);
824 if (el->num_values == 1) {
825 return ldb_kv_msg_delete_attribute(
826 module, ldb_kv, msg, name);
830 ldb_kv_index_del_value(module, ldb_kv, msg, el, i);
831 if (ret != LDB_SUCCESS) {
835 if (i<el->num_values-1) {
836 memmove(&el->values[i], &el->values[i+1],
837 sizeof(el->values[i])*
838 (el->num_values-(i+1)));
842 /* per definition we find in a canonicalised message an
843 attribute value only once. So we are finished here */
849 return LDB_ERR_NO_SUCH_ATTRIBUTE;
853 modify a record - internal interface
855 yuck - this is O(n^2). Luckily n is usually small so we probably
856 get away with it, but if we ever have really large attribute lists
857 then we'll need to look at this again
859 'req' is optional, and is used to specify controls if supplied
861 int ldb_kv_modify_internal(struct ldb_module *module,
862 const struct ldb_message *msg,
863 struct ldb_request *req)
865 struct ldb_context *ldb = ldb_module_get_ctx(module);
866 void *data = ldb_module_get_private(module);
867 struct ldb_kv_private *ldb_kv =
868 talloc_get_type(data, struct ldb_kv_private);
869 struct ldb_message *msg2;
871 int ret = LDB_SUCCESS, idx;
872 struct ldb_control *control_permissive = NULL;
873 TALLOC_CTX *mem_ctx = talloc_new(req);
875 if (mem_ctx == NULL) {
876 return ldb_module_oom(module);
880 control_permissive = ldb_request_get_control(req,
881 LDB_CONTROL_PERMISSIVE_MODIFY_OID);
884 msg2 = ldb_msg_new(mem_ctx);
890 ret = ldb_kv_search_dn1(
891 module, msg->dn, msg2, LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC);
892 if (ret != LDB_SUCCESS) {
896 for (i=0; i<msg->num_elements; i++) {
897 struct ldb_message_element *el = &msg->elements[i], *el2;
898 struct ldb_val *vals;
899 const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(ldb, el->name);
901 uint32_t options = 0;
902 if (control_permissive != NULL) {
903 options |= LDB_MSG_FIND_COMMON_REMOVE_DUPLICATES;
906 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
907 case LDB_FLAG_MOD_ADD:
909 if (el->num_values == 0) {
910 ldb_asprintf_errstring(ldb,
911 "attribute '%s': attribute on '%s' specified, but with 0 values (illegal)",
912 el->name, ldb_dn_get_linearized(msg2->dn));
913 ret = LDB_ERR_CONSTRAINT_VIOLATION;
917 /* make a copy of the array so that a permissive
918 * control can remove duplicates without changing the
919 * original values, but do not copy data as we do not
920 * need to keep it around once the operation is
922 if (control_permissive) {
923 el = talloc(msg2, struct ldb_message_element);
928 *el = msg->elements[i];
929 el->values = talloc_array(el, struct ldb_val, el->num_values);
930 if (el->values == NULL) {
934 for (j = 0; j < el->num_values; j++) {
935 el->values[j] = msg->elements[i].values[j];
939 if (el->num_values > 1 && ldb_kv_single_valued(a, el)) {
940 ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
941 el->name, ldb_dn_get_linearized(msg2->dn));
942 ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
946 /* Checks if element already exists */
947 idx = ldb_kv_find_element(msg2, el->name);
949 if (ldb_kv_msg_add_element(msg2, el) != 0) {
953 ret = ldb_kv_index_add_element(
954 module, ldb_kv, msg2, el);
955 if (ret != LDB_SUCCESS) {
959 j = (unsigned int) idx;
960 el2 = &(msg2->elements[j]);
962 /* We cannot add another value on a existing one
963 if the attribute is single-valued */
964 if (ldb_kv_single_valued(a, el)) {
965 ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
966 el->name, ldb_dn_get_linearized(msg2->dn));
967 ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
971 /* Check that values don't exist yet on multi-
972 valued attributes or aren't provided twice */
974 LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK)) {
975 struct ldb_val *duplicate = NULL;
976 ret = ldb_msg_find_common_values(ldb,
983 LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS) {
984 ldb_asprintf_errstring(ldb,
985 "attribute '%s': value "
986 "#%u on '%s' already "
987 "exists", el->name, j,
988 ldb_dn_get_linearized(msg2->dn));
990 } else if (ret != LDB_SUCCESS) {
994 ret = ldb_msg_find_duplicate_val(
995 ldb, msg2, el, &duplicate, 0);
996 if (ret != LDB_SUCCESS) {
999 if (duplicate != NULL) {
1000 ldb_asprintf_errstring(
1002 "attribute '%s': value "
1004 "provided more than "
1007 (int)duplicate->length,
1009 ldb_dn_get_linearized(msg->dn));
1010 ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
1015 /* Now combine existing and new values to a new
1017 vals = talloc_realloc(msg2->elements,
1018 el2->values, struct ldb_val,
1019 el2->num_values + el->num_values);
1022 ret = LDB_ERR_OTHER;
1026 for (j=0; j<el->num_values; j++) {
1027 vals[el2->num_values + j] =
1028 ldb_val_dup(vals, &el->values[j]);
1032 el2->num_values += el->num_values;
1034 ret = ldb_kv_index_add_element(
1035 module, ldb_kv, msg2, el);
1036 if (ret != LDB_SUCCESS) {
1043 case LDB_FLAG_MOD_REPLACE:
1045 if (el->num_values > 1 && ldb_kv_single_valued(a, el)) {
1046 ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
1047 el->name, ldb_dn_get_linearized(msg2->dn));
1048 ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
1053 * We don't need to check this if we have been
1054 * pre-screened by the repl_meta_data module
1055 * in Samba, or someone else who can claim to
1056 * know what they are doing.
1058 if (!(el->flags & LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK)) {
1059 struct ldb_val *duplicate = NULL;
1061 ret = ldb_msg_find_duplicate_val(ldb, msg2, el,
1063 if (ret != LDB_SUCCESS) {
1066 if (duplicate != NULL) {
1067 ldb_asprintf_errstring(
1069 "attribute '%s': value '%.*s' "
1070 "on '%s' provided more than "
1073 (int)duplicate->length,
1075 ldb_dn_get_linearized(msg2->dn));
1076 ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
1081 /* Checks if element already exists */
1082 idx = ldb_kv_find_element(msg2, el->name);
1084 j = (unsigned int) idx;
1085 el2 = &(msg2->elements[j]);
1087 /* we consider two elements to be
1088 * equal only if the order
1089 * matches. This allows dbcheck to
1090 * fix the ordering on attributes
1091 * where order matters, such as
1094 if (ldb_msg_element_equal_ordered(el, el2)) {
1098 /* Delete the attribute if it exists in the DB */
1099 if (ldb_kv_msg_delete_attribute(
1100 module, ldb_kv, msg2, el->name) != 0) {
1101 ret = LDB_ERR_OTHER;
1106 /* Recreate it with the new values */
1107 if (ldb_kv_msg_add_element(msg2, el) != 0) {
1108 ret = LDB_ERR_OTHER;
1113 ldb_kv_index_add_element(module, ldb_kv, msg2, el);
1114 if (ret != LDB_SUCCESS) {
1120 case LDB_FLAG_MOD_DELETE:
1121 dn = ldb_dn_get_linearized(msg2->dn);
1123 ret = LDB_ERR_OTHER;
1127 if (msg->elements[i].num_values == 0) {
1128 /* Delete the whole attribute */
1129 ret = ldb_kv_msg_delete_attribute(
1133 msg->elements[i].name);
1134 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE &&
1135 control_permissive) {
1138 ldb_asprintf_errstring(ldb,
1139 "attribute '%s': no such attribute for delete on '%s'",
1140 msg->elements[i].name, dn);
1142 if (ret != LDB_SUCCESS) {
1146 /* Delete specified values from an attribute */
1147 for (j=0; j < msg->elements[i].num_values; j++) {
1148 ret = ldb_kv_msg_delete_element(
1152 msg->elements[i].name,
1153 &msg->elements[i].values[j]);
1154 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE &&
1155 control_permissive) {
1157 } else if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
1158 ldb_asprintf_errstring(ldb,
1159 "attribute '%s': no matching attribute value while deleting attribute on '%s'",
1160 msg->elements[i].name, dn);
1162 if (ret != LDB_SUCCESS) {
1169 ldb_asprintf_errstring(ldb,
1170 "attribute '%s': invalid modify flags on '%s': 0x%x",
1171 msg->elements[i].name, ldb_dn_get_linearized(msg->dn),
1172 msg->elements[i].flags & LDB_FLAG_MOD_MASK);
1173 ret = LDB_ERR_PROTOCOL_ERROR;
1178 ret = ldb_kv_store(module, msg2, TDB_MODIFY);
1179 if (ret != LDB_SUCCESS) {
1183 ret = ldb_kv_modified(module, msg2->dn);
1184 if (ret != LDB_SUCCESS) {
1189 TALLOC_FREE(mem_ctx);
1196 static int ldb_kv_modify(struct ldb_kv_context *ctx)
1198 struct ldb_module *module = ctx->module;
1199 struct ldb_request *req = ctx->req;
1200 int ret = LDB_SUCCESS;
1202 ret = ldb_kv_check_special_dn(module, req->op.mod.message);
1203 if (ret != LDB_SUCCESS) {
1207 ldb_request_set_state(req, LDB_ASYNC_PENDING);
1209 if (ldb_kv_cache_load(module) != 0) {
1210 return LDB_ERR_OPERATIONS_ERROR;
1213 ret = ldb_kv_modify_internal(module, req->op.mod.message, req);
1221 static int ldb_kv_rename(struct ldb_kv_context *ctx)
1223 struct ldb_module *module = ctx->module;
1224 void *data = ldb_module_get_private(module);
1225 struct ldb_kv_private *ldb_kv =
1226 talloc_get_type(data, struct ldb_kv_private);
1227 struct ldb_request *req = ctx->req;
1228 struct ldb_message *msg;
1229 int ret = LDB_SUCCESS;
1230 struct ldb_val key, key_old;
1231 struct ldb_dn *db_dn;
1233 ldb_request_set_state(req, LDB_ASYNC_PENDING);
1235 if (ldb_kv_cache_load(ctx->module) != 0) {
1236 return LDB_ERR_OPERATIONS_ERROR;
1239 msg = ldb_msg_new(ctx);
1241 return LDB_ERR_OPERATIONS_ERROR;
1244 /* we need to fetch the old record to re-add under the new name */
1245 ret = ldb_kv_search_dn1(module,
1246 req->op.rename.olddn,
1248 LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC);
1249 if (ret != LDB_SUCCESS) {
1250 /* not finding the old record is an error */
1254 /* We need to, before changing the DB, check if the new DN
1255 * exists, so we can return this error to the caller with an
1258 * Even in GUID index mode we use ltdb_key_dn() as we are
1259 * trying to figure out if this is just a case rename
1261 key = ldb_kv_key_dn(module, msg, req->op.rename.newdn);
1264 return LDB_ERR_OPERATIONS_ERROR;
1267 key_old = ldb_kv_key_dn(module, msg, req->op.rename.olddn);
1268 if (!key_old.data) {
1270 talloc_free(key.data);
1271 return LDB_ERR_OPERATIONS_ERROR;
1275 * Only declare a conflict if the new DN already exists,
1276 * and it isn't a case change on the old DN
1278 if (key_old.length != key.length
1279 || memcmp(key.data, key_old.data, key.length) != 0) {
1280 ret = ldb_kv_search_base(
1281 module, msg, req->op.rename.newdn, &db_dn);
1282 if (ret == LDB_SUCCESS) {
1283 ret = LDB_ERR_ENTRY_ALREADY_EXISTS;
1284 } else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1289 /* finding the new record already in the DB is an error */
1291 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
1292 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1293 "Entry %s already exists",
1294 ldb_dn_get_linearized(req->op.rename.newdn));
1296 if (ret != LDB_SUCCESS) {
1297 talloc_free(key_old.data);
1298 talloc_free(key.data);
1303 talloc_free(key_old.data);
1304 talloc_free(key.data);
1306 /* Always delete first then add, to avoid conflicts with
1307 * unique indexes. We rely on the transaction to make this
1310 ret = ldb_kv_delete_internal(module, msg->dn);
1311 if (ret != LDB_SUCCESS) {
1316 msg->dn = ldb_dn_copy(msg, req->op.rename.newdn);
1317 if (msg->dn == NULL) {
1319 return LDB_ERR_OPERATIONS_ERROR;
1322 /* We don't check single value as we can have more than 1 with
1323 * deleted attributes. We could go through all elements but that's
1324 * maybe not the most efficient way
1326 ret = ldb_kv_add_internal(module, ldb_kv, msg, false);
1333 static int ldb_kv_start_trans(struct ldb_module *module)
1335 void *data = ldb_module_get_private(module);
1336 struct ldb_kv_private *ldb_kv =
1337 talloc_get_type(data, struct ldb_kv_private);
1339 pid_t pid = getpid();
1341 if (ldb_kv->pid != pid) {
1342 ldb_asprintf_errstring(ldb_module_get_ctx(ldb_kv->module),
1344 ": Reusing ldb opend by pid %d in "
1348 return LDB_ERR_PROTOCOL_ERROR;
1351 /* Do not take out the transaction lock on a read-only DB */
1352 if (ldb_kv->read_only) {
1353 return LDB_ERR_UNWILLING_TO_PERFORM;
1356 if (ldb_kv->kv_ops->begin_write(ldb_kv) != 0) {
1357 return ldb_kv->kv_ops->error(ldb_kv);
1360 ldb_kv_index_transaction_start(module);
1362 ldb_kv->reindex_failed = false;
1368 * Forward declaration to allow prepare_commit to in fact abort the
1371 static int ldb_kv_del_trans(struct ldb_module *module);
1373 static int ldb_kv_prepare_commit(struct ldb_module *module)
1376 void *data = ldb_module_get_private(module);
1377 struct ldb_kv_private *ldb_kv =
1378 talloc_get_type(data, struct ldb_kv_private);
1379 pid_t pid = getpid();
1381 if (ldb_kv->pid != pid) {
1382 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1384 ": Reusing ldb opend by pid %d in "
1388 return LDB_ERR_PROTOCOL_ERROR;
1391 if (!ldb_kv->kv_ops->transaction_active(ldb_kv)) {
1392 ldb_set_errstring(ldb_module_get_ctx(module),
1393 "ltdb_prepare_commit() called "
1394 "without transaction active");
1395 return LDB_ERR_OPERATIONS_ERROR;
1399 * Check if the last re-index failed.
1401 * This can happen if for example a duplicate value was marked
1402 * unique. We must not write a partial re-index into the DB.
1404 if (ldb_kv->reindex_failed) {
1406 * We must instead abort the transaction so we get the
1407 * old values and old index back
1409 ldb_kv_del_trans(module);
1410 ldb_set_errstring(ldb_module_get_ctx(module),
1411 "Failure during re-index, so "
1412 "transaction must be aborted.");
1413 return LDB_ERR_OPERATIONS_ERROR;
1416 ret = ldb_kv_index_transaction_commit(module);
1417 if (ret != LDB_SUCCESS) {
1418 ldb_kv->kv_ops->abort_write(ldb_kv);
1422 if (ldb_kv->kv_ops->prepare_write(ldb_kv) != 0) {
1423 ret = ldb_kv->kv_ops->error(ldb_kv);
1424 ldb_debug_set(ldb_module_get_ctx(module),
1427 "prepare_write): %s -> %s",
1428 ldb_kv->kv_ops->errorstr(ldb_kv),
1433 ldb_kv->prepared_commit = true;
1438 static int ldb_kv_end_trans(struct ldb_module *module)
1441 void *data = ldb_module_get_private(module);
1442 struct ldb_kv_private *ldb_kv =
1443 talloc_get_type(data, struct ldb_kv_private);
1445 if (!ldb_kv->prepared_commit) {
1446 ret = ldb_kv_prepare_commit(module);
1447 if (ret != LDB_SUCCESS) {
1452 ldb_kv->prepared_commit = false;
1454 if (ldb_kv->kv_ops->finish_write(ldb_kv) != 0) {
1455 ret = ldb_kv->kv_ops->error(ldb_kv);
1456 ldb_asprintf_errstring(
1457 ldb_module_get_ctx(module),
1458 "Failure during tdb_transaction_commit(): %s -> %s",
1459 ldb_kv->kv_ops->errorstr(ldb_kv),
1467 static int ldb_kv_del_trans(struct ldb_module *module)
1469 void *data = ldb_module_get_private(module);
1470 struct ldb_kv_private *ldb_kv =
1471 talloc_get_type(data, struct ldb_kv_private);
1473 if (ldb_kv_index_transaction_cancel(module) != 0) {
1474 ldb_kv->kv_ops->abort_write(ldb_kv);
1475 return ldb_kv->kv_ops->error(ldb_kv);
1478 ldb_kv->kv_ops->abort_write(ldb_kv);
1483 return sequenceNumber from @BASEINFO
1485 static int ldb_kv_sequence_number(struct ldb_kv_context *ctx,
1486 struct ldb_extended **ext)
1488 struct ldb_context *ldb;
1489 struct ldb_module *module = ctx->module;
1490 struct ldb_request *req = ctx->req;
1491 void *data = ldb_module_get_private(module);
1492 struct ldb_kv_private *ldb_kv =
1493 talloc_get_type(data, struct ldb_kv_private);
1494 TALLOC_CTX *tmp_ctx = NULL;
1495 struct ldb_seqnum_request *seq;
1496 struct ldb_seqnum_result *res;
1497 struct ldb_message *msg = NULL;
1500 int ret = LDB_SUCCESS;
1502 ldb = ldb_module_get_ctx(module);
1504 seq = talloc_get_type(req->op.extended.data,
1505 struct ldb_seqnum_request);
1507 return LDB_ERR_OPERATIONS_ERROR;
1510 ldb_request_set_state(req, LDB_ASYNC_PENDING);
1512 if (ldb_kv->kv_ops->lock_read(module) != 0) {
1513 return LDB_ERR_OPERATIONS_ERROR;
1516 res = talloc_zero(req, struct ldb_seqnum_result);
1518 ret = LDB_ERR_OPERATIONS_ERROR;
1522 tmp_ctx = talloc_new(req);
1523 if (tmp_ctx == NULL) {
1524 ret = LDB_ERR_OPERATIONS_ERROR;
1528 dn = ldb_dn_new(tmp_ctx, ldb, LDB_KV_BASEINFO);
1530 ret = LDB_ERR_OPERATIONS_ERROR;
1534 msg = ldb_msg_new(tmp_ctx);
1536 ret = LDB_ERR_OPERATIONS_ERROR;
1540 ret = ldb_kv_search_dn1(module, dn, msg, 0);
1541 if (ret != LDB_SUCCESS) {
1545 switch (seq->type) {
1546 case LDB_SEQ_HIGHEST_SEQ:
1547 res->seq_num = ldb_msg_find_attr_as_uint64(msg, LDB_KV_SEQUENCE_NUMBER, 0);
1550 res->seq_num = ldb_msg_find_attr_as_uint64(msg, LDB_KV_SEQUENCE_NUMBER, 0);
1553 case LDB_SEQ_HIGHEST_TIMESTAMP:
1554 date = ldb_msg_find_attr_as_string(msg, LDB_KV_MOD_TIMESTAMP, NULL);
1556 res->seq_num = ldb_string_to_time(date);
1559 /* zero is as good as anything when we don't know */
1564 *ext = talloc_zero(req, struct ldb_extended);
1566 ret = LDB_ERR_OPERATIONS_ERROR;
1569 (*ext)->oid = LDB_EXTENDED_SEQUENCE_NUMBER;
1570 (*ext)->data = talloc_steal(*ext, res);
1573 talloc_free(tmp_ctx);
1575 ldb_kv->kv_ops->unlock_read(module);
1579 static void ldb_kv_request_done(struct ldb_kv_context *ctx, int error)
1581 struct ldb_context *ldb;
1582 struct ldb_request *req;
1583 struct ldb_reply *ares;
1585 ldb = ldb_module_get_ctx(ctx->module);
1588 /* if we already returned an error just return */
1589 if (ldb_request_get_status(req) != LDB_SUCCESS) {
1593 ares = talloc_zero(req, struct ldb_reply);
1596 req->callback(req, NULL);
1599 ares->type = LDB_REPLY_DONE;
1600 ares->error = error;
1602 req->callback(req, ares);
1605 static void ldb_kv_timeout(struct tevent_context *ev,
1606 struct tevent_timer *te,
1610 struct ldb_kv_context *ctx;
1611 ctx = talloc_get_type(private_data, struct ldb_kv_context);
1613 if (!ctx->request_terminated) {
1614 /* request is done now */
1615 ldb_kv_request_done(ctx, LDB_ERR_TIME_LIMIT_EXCEEDED);
1619 /* neutralize the spy */
1620 ctx->spy->ctx = NULL;
1626 static void ldb_kv_request_extended_done(struct ldb_kv_context *ctx,
1627 struct ldb_extended *ext,
1630 struct ldb_context *ldb;
1631 struct ldb_request *req;
1632 struct ldb_reply *ares;
1634 ldb = ldb_module_get_ctx(ctx->module);
1637 /* if we already returned an error just return */
1638 if (ldb_request_get_status(req) != LDB_SUCCESS) {
1642 ares = talloc_zero(req, struct ldb_reply);
1645 req->callback(req, NULL);
1648 ares->type = LDB_REPLY_DONE;
1649 ares->response = ext;
1650 ares->error = error;
1652 req->callback(req, ares);
1655 static void ldb_kv_handle_extended(struct ldb_kv_context *ctx)
1657 struct ldb_extended *ext = NULL;
1660 if (strcmp(ctx->req->op.extended.oid,
1661 LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1662 /* get sequence number */
1663 ret = ldb_kv_sequence_number(ctx, &ext);
1665 /* not recognized */
1666 ret = LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
1669 ldb_kv_request_extended_done(ctx, ext, ret);
1672 static void ldb_kv_callback(struct tevent_context *ev,
1673 struct tevent_timer *te,
1677 struct ldb_kv_context *ctx;
1680 ctx = talloc_get_type(private_data, struct ldb_kv_context);
1682 if (ctx->request_terminated) {
1686 switch (ctx->req->operation) {
1688 ret = ldb_kv_search(ctx);
1691 ret = ldb_kv_add(ctx);
1694 ret = ldb_kv_modify(ctx);
1697 ret = ldb_kv_delete(ctx);
1700 ret = ldb_kv_rename(ctx);
1703 ldb_kv_handle_extended(ctx);
1706 /* no other op supported */
1707 ret = LDB_ERR_PROTOCOL_ERROR;
1710 if (!ctx->request_terminated) {
1711 /* request is done now */
1712 ldb_kv_request_done(ctx, ret);
1717 /* neutralize the spy */
1718 ctx->spy->ctx = NULL;
1724 static int ldb_kv_request_destructor(void *ptr)
1726 struct ldb_kv_req_spy *spy =
1727 talloc_get_type(ptr, struct ldb_kv_req_spy);
1729 if (spy->ctx != NULL) {
1730 spy->ctx->spy = NULL;
1731 spy->ctx->request_terminated = true;
1738 static int ldb_kv_handle_request(struct ldb_module *module,
1739 struct ldb_request *req)
1741 struct ldb_control *control_permissive;
1742 struct ldb_context *ldb;
1743 struct tevent_context *ev;
1744 struct ldb_kv_context *ac;
1745 struct tevent_timer *te;
1749 ldb = ldb_module_get_ctx(module);
1751 control_permissive = ldb_request_get_control(req,
1752 LDB_CONTROL_PERMISSIVE_MODIFY_OID);
1754 for (i = 0; req->controls && req->controls[i]; i++) {
1755 if (req->controls[i]->critical &&
1756 req->controls[i] != control_permissive) {
1757 ldb_asprintf_errstring(ldb, "Unsupported critical extension %s",
1758 req->controls[i]->oid);
1759 return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
1763 if (req->starttime == 0 || req->timeout == 0) {
1764 ldb_set_errstring(ldb, "Invalid timeout settings");
1765 return LDB_ERR_TIME_LIMIT_EXCEEDED;
1768 ev = ldb_handle_get_event_context(req->handle);
1770 ac = talloc_zero(ldb, struct ldb_kv_context);
1773 return LDB_ERR_OPERATIONS_ERROR;
1776 ac->module = module;
1781 te = tevent_add_timer(ev, ac, tv, ldb_kv_callback, ac);
1784 return LDB_ERR_OPERATIONS_ERROR;
1787 if (req->timeout > 0) {
1788 tv.tv_sec = req->starttime + req->timeout;
1791 tevent_add_timer(ev, ac, tv, ldb_kv_timeout, ac);
1792 if (NULL == ac->timeout_event) {
1794 return LDB_ERR_OPERATIONS_ERROR;
1798 /* set a spy so that we do not try to use the request context
1799 * if it is freed before ltdb_callback fires */
1800 ac->spy = talloc(req, struct ldb_kv_req_spy);
1801 if (NULL == ac->spy) {
1803 return LDB_ERR_OPERATIONS_ERROR;
1807 talloc_set_destructor((TALLOC_CTX *)ac->spy, ldb_kv_request_destructor);
1812 static int ldb_kv_init_rootdse(struct ldb_module *module)
1814 /* ignore errors on this - we expect it for non-sam databases */
1815 ldb_mod_register_control(module, LDB_CONTROL_PERMISSIVE_MODIFY_OID);
1817 /* there can be no module beyond the backend, just return */
1821 static int ldb_kv_lock_read(struct ldb_module *module)
1823 void *data = ldb_module_get_private(module);
1824 struct ldb_kv_private *ldb_kv =
1825 talloc_get_type(data, struct ldb_kv_private);
1826 return ldb_kv->kv_ops->lock_read(module);
1829 static int ldb_kv_unlock_read(struct ldb_module *module)
1831 void *data = ldb_module_get_private(module);
1832 struct ldb_kv_private *ldb_kv =
1833 talloc_get_type(data, struct ldb_kv_private);
1834 return ldb_kv->kv_ops->unlock_read(module);
1837 static const struct ldb_module_ops ldb_kv_ops = {
1839 .init_context = ldb_kv_init_rootdse,
1840 .search = ldb_kv_handle_request,
1841 .add = ldb_kv_handle_request,
1842 .modify = ldb_kv_handle_request,
1843 .del = ldb_kv_handle_request,
1844 .rename = ldb_kv_handle_request,
1845 .extended = ldb_kv_handle_request,
1846 .start_transaction = ldb_kv_start_trans,
1847 .end_transaction = ldb_kv_end_trans,
1848 .prepare_commit = ldb_kv_prepare_commit,
1849 .del_transaction = ldb_kv_del_trans,
1850 .read_lock = ldb_kv_lock_read,
1851 .read_unlock = ldb_kv_unlock_read,
1854 int ldb_kv_init_store(struct ldb_kv_private *ldb_kv,
1856 struct ldb_context *ldb,
1857 const char *options[],
1858 struct ldb_module **_module)
1860 if (getenv("LDB_WARN_UNINDEXED")) {
1861 ldb_kv->warn_unindexed = true;
1864 if (getenv("LDB_WARN_REINDEX")) {
1865 ldb_kv->warn_reindex = true;
1868 ldb_kv->sequence_number = 0;
1870 ldb_kv->pid = getpid();
1872 ldb_kv->module = ldb_module_new(ldb, ldb, name, &ldb_kv_ops);
1873 if (!ldb_kv->module) {
1875 talloc_free(ldb_kv);
1876 return LDB_ERR_OPERATIONS_ERROR;
1878 ldb_module_set_private(ldb_kv->module, ldb_kv);
1879 talloc_steal(ldb_kv->module, ldb_kv);
1881 if (ldb_kv_cache_load(ldb_kv->module) != 0) {
1882 ldb_asprintf_errstring(ldb, "Unable to load ltdb cache "
1883 "records for backend '%s'", name);
1884 talloc_free(ldb_kv->module);
1885 return LDB_ERR_OPERATIONS_ERROR;
1888 *_module = ldb_kv->module;
1890 * Set or override the maximum key length
1892 * The ldb_mdb code will have set this to 511, but our tests
1893 * set this even smaller (to make the tests more practical).
1895 * This must only be used for the selftest as the length
1896 * becomes encoded in the index keys.
1899 const char *len_str =
1900 ldb_options_find(ldb, options,
1901 "max_key_len_for_self_test");
1902 if (len_str != NULL) {
1903 unsigned len = strtoul(len_str, NULL, 0);
1904 ldb_kv->max_key_length = len;
1909 * Override full DB scans
1911 * A full DB scan is expensive on a large database. This
1912 * option is for testing to show that the full DB scan is not
1916 const char *len_str =
1917 ldb_options_find(ldb, options,
1918 "disable_full_db_scan_for_self_test");
1919 if (len_str != NULL) {
1920 ldb_kv->disable_full_db_scan = true;