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(TDB_DATA key)
72 if (memcmp(key.dptr, "DN=", 3) == 0) {
76 if (memcmp(key.dptr, "ID=", 3) == 0) {
80 if (key.dsize < sizeof(LDB_KV_GUID_KEY_PREFIX)) {
84 if (memcmp(key.dptr, LDB_KV_GUID_KEY_PREFIX,
85 sizeof(LDB_KV_GUID_KEY_PREFIX) - 1) == 0) {
93 form a TDB_DATA 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 TDB_DATA 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.dptr = (uint8_t *)key_str;
135 key.dsize = 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->dsize != (GUID_val->length+GUID_prefix_len)) {
156 return LDB_ERR_OPERATIONS_ERROR;
159 memcpy(key->dptr, GUID_prefix, GUID_prefix_len);
160 memcpy(&key->dptr[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 TDB_DATA 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.dptr = talloc_size(mem_ctx, LDB_KV_GUID_KEY_SIZE);
243 if (key.dptr == NULL) {
249 key.dsize = talloc_get_size(key.dptr);
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_key;
358 struct ldb_val ldb_data;
359 int ret = LDB_SUCCESS;
360 TALLOC_CTX *key_ctx = talloc_new(module);
362 if (key_ctx == NULL) {
363 return ldb_module_oom(module);
366 if (ldb_kv->read_only) {
367 talloc_free(key_ctx);
368 return LDB_ERR_UNWILLING_TO_PERFORM;
371 tdb_key = ldb_kv_key_msg(module, key_ctx, msg);
372 if (tdb_key.dptr == NULL) {
373 TALLOC_FREE(key_ctx);
374 return LDB_ERR_OTHER;
377 ret = ldb_pack_data(ldb_module_get_ctx(module),
380 TALLOC_FREE(key_ctx);
381 return LDB_ERR_OTHER;
384 ldb_key.data = tdb_key.dptr;
385 ldb_key.length = tdb_key.dsize;
387 ret = ldb_kv->kv_ops->store(ldb_kv, ldb_key, ldb_data, flgs);
389 bool is_special = ldb_dn_is_special(msg->dn);
390 ret = ldb_kv->kv_ops->error(ldb_kv);
393 * LDB_ERR_ENTRY_ALREADY_EXISTS means the DN, not
394 * the GUID, so re-map
396 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS && !is_special &&
397 ldb_kv->cache->GUID_index_attribute != NULL) {
398 ret = LDB_ERR_CONSTRAINT_VIOLATION;
404 TALLOC_FREE(key_ctx);
405 talloc_free(ldb_data.data);
412 check if a attribute is a single valued, for a given element
414 static bool ldb_kv_single_valued(const struct ldb_schema_attribute *a,
415 struct ldb_message_element *el)
417 if (!a) return false;
419 if (el->flags & LDB_FLAG_INTERNAL_FORCE_SINGLE_VALUE_CHECK) {
420 /* override from a ldb module, for example
421 used for the description field, which is
422 marked multi-valued in the schema but which
423 should not actually accept multiple
427 if (el->flags & LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK) {
428 /* override from a ldb module, for example used for
429 deleted linked attribute entries */
433 if (a->flags & LDB_ATTR_FLAG_SINGLE_VALUE) {
439 static int ldb_kv_add_internal(struct ldb_module *module,
440 struct ldb_kv_private *ldb_kv,
441 const struct ldb_message *msg,
442 bool check_single_value)
444 struct ldb_context *ldb = ldb_module_get_ctx(module);
445 int ret = LDB_SUCCESS;
448 for (i=0;i<msg->num_elements;i++) {
449 struct ldb_message_element *el = &msg->elements[i];
450 const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(ldb, el->name);
452 if (el->num_values == 0) {
453 ldb_asprintf_errstring(ldb, "attribute '%s' on '%s' specified, but with 0 values (illegal)",
454 el->name, ldb_dn_get_linearized(msg->dn));
455 return LDB_ERR_CONSTRAINT_VIOLATION;
457 if (check_single_value && el->num_values > 1 &&
458 ldb_kv_single_valued(a, el)) {
459 ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
460 el->name, ldb_dn_get_linearized(msg->dn));
461 return LDB_ERR_CONSTRAINT_VIOLATION;
464 /* Do not check "@ATTRIBUTES" for duplicated values */
465 if (ldb_dn_is_special(msg->dn) &&
466 ldb_dn_check_special(msg->dn, LDB_KV_ATTRIBUTES)) {
470 if (check_single_value &&
472 LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK)) {
473 struct ldb_val *duplicate = NULL;
475 ret = ldb_msg_find_duplicate_val(ldb, discard_const(msg),
477 if (ret != LDB_SUCCESS) {
480 if (duplicate != NULL) {
481 ldb_asprintf_errstring(
483 "attribute '%s': value '%.*s' on '%s' "
484 "provided more than once in ADD object",
486 (int)duplicate->length,
488 ldb_dn_get_linearized(msg->dn));
489 return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
494 ret = ldb_kv_store(module, msg, TDB_INSERT);
495 if (ret != LDB_SUCCESS) {
497 * Try really hard to get the right error code for
498 * a re-add situation, as this can matter!
500 if (ret == LDB_ERR_CONSTRAINT_VIOLATION) {
502 struct ldb_dn *dn2 = NULL;
503 TALLOC_CTX *mem_ctx = talloc_new(module);
504 if (mem_ctx == NULL) {
505 return ldb_module_operr(module);
508 ldb_kv_search_base(module, mem_ctx, msg->dn, &dn2);
509 TALLOC_FREE(mem_ctx);
510 if (ret2 == LDB_SUCCESS) {
511 ret = LDB_ERR_ENTRY_ALREADY_EXISTS;
514 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
515 ldb_asprintf_errstring(ldb,
516 "Entry %s already exists",
517 ldb_dn_get_linearized(msg->dn));
522 ret = ldb_kv_index_add_new(module, ldb_kv, msg);
523 if (ret != LDB_SUCCESS) {
525 * If we failed to index, delete the message again.
527 * This is particularly important for the GUID index
528 * case, which will only fail for a duplicate DN
531 * Note that the caller may not cancel the transation
532 * and this means the above add might really show up!
534 ldb_kv_delete_noindex(module, msg);
538 ret = ldb_kv_modified(module, msg->dn);
544 add a record to the database
546 static int ldb_kv_add(struct ldb_kv_context *ctx)
548 struct ldb_module *module = ctx->module;
549 struct ldb_request *req = ctx->req;
550 void *data = ldb_module_get_private(module);
551 struct ldb_kv_private *ldb_kv =
552 talloc_get_type(data, struct ldb_kv_private);
553 int ret = LDB_SUCCESS;
555 if (ldb_kv->max_key_length != 0 &&
556 ldb_kv->cache->GUID_index_attribute == NULL &&
557 !ldb_dn_is_special(req->op.add.message->dn)) {
558 ldb_set_errstring(ldb_module_get_ctx(module),
559 "Must operate ldb_mdb in GUID "
560 "index mode, but " LDB_KV_IDXGUID " not set.");
561 return LDB_ERR_UNWILLING_TO_PERFORM;
564 ret = ldb_kv_check_special_dn(module, req->op.add.message);
565 if (ret != LDB_SUCCESS) {
569 ldb_request_set_state(req, LDB_ASYNC_PENDING);
571 if (ldb_kv_cache_load(module) != 0) {
572 return LDB_ERR_OPERATIONS_ERROR;
575 ret = ldb_kv_add_internal(module, ldb_kv, req->op.add.message, true);
581 delete a record from the database, not updating indexes (used for deleting
584 int ldb_kv_delete_noindex(struct ldb_module *module,
585 const struct ldb_message *msg)
587 void *data = ldb_module_get_private(module);
588 struct ldb_kv_private *ldb_kv =
589 talloc_get_type(data, struct ldb_kv_private);
590 struct ldb_val ldb_key;
593 TALLOC_CTX *tdb_key_ctx = talloc_new(module);
595 if (tdb_key_ctx == NULL) {
596 return ldb_module_oom(module);
599 if (ldb_kv->read_only) {
600 talloc_free(tdb_key_ctx);
601 return LDB_ERR_UNWILLING_TO_PERFORM;
604 tdb_key = ldb_kv_key_msg(module, tdb_key_ctx, msg);
606 TALLOC_FREE(tdb_key_ctx);
607 return LDB_ERR_OTHER;
610 ldb_key.data = tdb_key.dptr;
611 ldb_key.length = tdb_key.dsize;
613 ret = ldb_kv->kv_ops->delete (ldb_kv, ldb_key);
614 TALLOC_FREE(tdb_key_ctx);
617 ret = ldb_kv->kv_ops->error(ldb_kv);
623 static int ldb_kv_delete_internal(struct ldb_module *module, struct ldb_dn *dn)
625 struct ldb_message *msg;
626 int ret = LDB_SUCCESS;
628 msg = ldb_msg_new(module);
630 return LDB_ERR_OPERATIONS_ERROR;
633 /* in case any attribute of the message was indexed, we need
634 to fetch the old record */
635 ret = ldb_kv_search_dn1(
636 module, dn, msg, LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC);
637 if (ret != LDB_SUCCESS) {
638 /* not finding the old record is an error */
642 ret = ldb_kv_delete_noindex(module, msg);
643 if (ret != LDB_SUCCESS) {
647 /* remove any indexed attributes */
648 ret = ldb_kv_index_delete(module, msg);
649 if (ret != LDB_SUCCESS) {
653 ret = ldb_kv_modified(module, dn);
654 if (ret != LDB_SUCCESS) {
664 delete a record from the database
666 static int ldb_kv_delete(struct ldb_kv_context *ctx)
668 struct ldb_module *module = ctx->module;
669 struct ldb_request *req = ctx->req;
670 int ret = LDB_SUCCESS;
672 ldb_request_set_state(req, LDB_ASYNC_PENDING);
674 if (ldb_kv_cache_load(module) != 0) {
675 return LDB_ERR_OPERATIONS_ERROR;
678 ret = ldb_kv_delete_internal(module, req->op.del.dn);
684 find an element by attribute name. At the moment this does a linear search,
685 it should be re-coded to use a binary search once all places that modify
686 records guarantee sorted order
688 return the index of the first matching element if found, otherwise -1
690 static int ldb_kv_find_element(const struct ldb_message *msg, const char *name)
693 for (i=0;i<msg->num_elements;i++) {
694 if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
703 add an element to an existing record. Assumes a elements array that we
704 can call re-alloc on, and assumed that we can re-use the data pointers from
705 the passed in additional values. Use with care!
707 returns 0 on success, -1 on failure (and sets errno)
709 static int ldb_kv_msg_add_element(struct ldb_message *msg,
710 struct ldb_message_element *el)
712 struct ldb_message_element *e2;
715 if (el->num_values == 0) {
716 /* nothing to do here - we don't add empty elements */
720 e2 = talloc_realloc(msg, msg->elements, struct ldb_message_element,
721 msg->num_elements+1);
729 e2 = &msg->elements[msg->num_elements];
732 e2->flags = el->flags;
733 e2->values = talloc_array(msg->elements,
734 struct ldb_val, el->num_values);
739 for (i=0;i<el->num_values;i++) {
740 e2->values[i] = el->values[i];
742 e2->num_values = el->num_values;
750 delete all elements having a specified attribute name
752 static int ldb_kv_msg_delete_attribute(struct ldb_module *module,
753 struct ldb_kv_private *ldb_kv,
754 struct ldb_message *msg,
759 struct ldb_message_element *el;
760 bool is_special = ldb_dn_is_special(msg->dn);
762 if (!is_special && ldb_kv->cache->GUID_index_attribute != NULL &&
763 ldb_attr_cmp(name, ldb_kv->cache->GUID_index_attribute) == 0) {
764 struct ldb_context *ldb = ldb_module_get_ctx(module);
765 ldb_asprintf_errstring(ldb,
766 "Must not modify GUID "
767 "attribute %s (used as DB index)",
768 ldb_kv->cache->GUID_index_attribute);
769 return LDB_ERR_CONSTRAINT_VIOLATION;
772 el = ldb_msg_find_element(msg, name);
774 return LDB_ERR_NO_SUCH_ATTRIBUTE;
776 i = el - msg->elements;
778 ret = ldb_kv_index_del_element(module, ldb_kv, msg, el);
779 if (ret != LDB_SUCCESS) {
783 talloc_free(el->values);
784 if (msg->num_elements > (i+1)) {
785 memmove(el, el+1, sizeof(*el) * (msg->num_elements - (i+1)));
788 msg->elements = talloc_realloc(msg, msg->elements,
789 struct ldb_message_element,
795 delete all elements matching an attribute name/value
797 return LDB Error on failure
799 static int ldb_kv_msg_delete_element(struct ldb_module *module,
800 struct ldb_kv_private *ldb_kv,
801 struct ldb_message *msg,
803 const struct ldb_val *val)
805 struct ldb_context *ldb = ldb_module_get_ctx(module);
808 struct ldb_message_element *el;
809 const struct ldb_schema_attribute *a;
811 found = ldb_kv_find_element(msg, name);
813 return LDB_ERR_NO_SUCH_ATTRIBUTE;
816 i = (unsigned int) found;
817 el = &(msg->elements[i]);
819 a = ldb_schema_attribute_by_name(ldb, el->name);
821 for (i=0;i<el->num_values;i++) {
823 if (a->syntax->operator_fn) {
824 ret = a->syntax->operator_fn(ldb, LDB_OP_EQUALITY, a,
825 &el->values[i], val, &matched);
826 if (ret != LDB_SUCCESS) return ret;
828 matched = (a->syntax->comparison_fn(ldb, ldb,
829 &el->values[i], val) == 0);
832 if (el->num_values == 1) {
833 return ldb_kv_msg_delete_attribute(
834 module, ldb_kv, msg, name);
838 ldb_kv_index_del_value(module, ldb_kv, msg, el, i);
839 if (ret != LDB_SUCCESS) {
843 if (i<el->num_values-1) {
844 memmove(&el->values[i], &el->values[i+1],
845 sizeof(el->values[i])*
846 (el->num_values-(i+1)));
850 /* per definition we find in a canonicalised message an
851 attribute value only once. So we are finished here */
857 return LDB_ERR_NO_SUCH_ATTRIBUTE;
861 modify a record - internal interface
863 yuck - this is O(n^2). Luckily n is usually small so we probably
864 get away with it, but if we ever have really large attribute lists
865 then we'll need to look at this again
867 'req' is optional, and is used to specify controls if supplied
869 int ldb_kv_modify_internal(struct ldb_module *module,
870 const struct ldb_message *msg,
871 struct ldb_request *req)
873 struct ldb_context *ldb = ldb_module_get_ctx(module);
874 void *data = ldb_module_get_private(module);
875 struct ldb_kv_private *ldb_kv =
876 talloc_get_type(data, struct ldb_kv_private);
877 struct ldb_message *msg2;
879 int ret = LDB_SUCCESS, idx;
880 struct ldb_control *control_permissive = NULL;
881 TALLOC_CTX *mem_ctx = talloc_new(req);
883 if (mem_ctx == NULL) {
884 return ldb_module_oom(module);
888 control_permissive = ldb_request_get_control(req,
889 LDB_CONTROL_PERMISSIVE_MODIFY_OID);
892 msg2 = ldb_msg_new(mem_ctx);
898 ret = ldb_kv_search_dn1(
899 module, msg->dn, msg2, LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC);
900 if (ret != LDB_SUCCESS) {
904 for (i=0; i<msg->num_elements; i++) {
905 struct ldb_message_element *el = &msg->elements[i], *el2;
906 struct ldb_val *vals;
907 const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(ldb, el->name);
909 uint32_t options = 0;
910 if (control_permissive != NULL) {
911 options |= LDB_MSG_FIND_COMMON_REMOVE_DUPLICATES;
914 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
915 case LDB_FLAG_MOD_ADD:
917 if (el->num_values == 0) {
918 ldb_asprintf_errstring(ldb,
919 "attribute '%s': attribute on '%s' specified, but with 0 values (illegal)",
920 el->name, ldb_dn_get_linearized(msg2->dn));
921 ret = LDB_ERR_CONSTRAINT_VIOLATION;
925 /* make a copy of the array so that a permissive
926 * control can remove duplicates without changing the
927 * original values, but do not copy data as we do not
928 * need to keep it around once the operation is
930 if (control_permissive) {
931 el = talloc(msg2, struct ldb_message_element);
936 *el = msg->elements[i];
937 el->values = talloc_array(el, struct ldb_val, el->num_values);
938 if (el->values == NULL) {
942 for (j = 0; j < el->num_values; j++) {
943 el->values[j] = msg->elements[i].values[j];
947 if (el->num_values > 1 && ldb_kv_single_valued(a, el)) {
948 ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
949 el->name, ldb_dn_get_linearized(msg2->dn));
950 ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
954 /* Checks if element already exists */
955 idx = ldb_kv_find_element(msg2, el->name);
957 if (ldb_kv_msg_add_element(msg2, el) != 0) {
961 ret = ldb_kv_index_add_element(
962 module, ldb_kv, msg2, el);
963 if (ret != LDB_SUCCESS) {
967 j = (unsigned int) idx;
968 el2 = &(msg2->elements[j]);
970 /* We cannot add another value on a existing one
971 if the attribute is single-valued */
972 if (ldb_kv_single_valued(a, el)) {
973 ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
974 el->name, ldb_dn_get_linearized(msg2->dn));
975 ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
979 /* Check that values don't exist yet on multi-
980 valued attributes or aren't provided twice */
982 LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK)) {
983 struct ldb_val *duplicate = NULL;
984 ret = ldb_msg_find_common_values(ldb,
991 LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS) {
992 ldb_asprintf_errstring(ldb,
993 "attribute '%s': value "
994 "#%u on '%s' already "
995 "exists", el->name, j,
996 ldb_dn_get_linearized(msg2->dn));
998 } else if (ret != LDB_SUCCESS) {
1002 ret = ldb_msg_find_duplicate_val(
1003 ldb, msg2, el, &duplicate, 0);
1004 if (ret != LDB_SUCCESS) {
1007 if (duplicate != NULL) {
1008 ldb_asprintf_errstring(
1010 "attribute '%s': value "
1012 "provided more than "
1015 (int)duplicate->length,
1017 ldb_dn_get_linearized(msg->dn));
1018 ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
1023 /* Now combine existing and new values to a new
1025 vals = talloc_realloc(msg2->elements,
1026 el2->values, struct ldb_val,
1027 el2->num_values + el->num_values);
1030 ret = LDB_ERR_OTHER;
1034 for (j=0; j<el->num_values; j++) {
1035 vals[el2->num_values + j] =
1036 ldb_val_dup(vals, &el->values[j]);
1040 el2->num_values += el->num_values;
1042 ret = ldb_kv_index_add_element(
1043 module, ldb_kv, msg2, el);
1044 if (ret != LDB_SUCCESS) {
1051 case LDB_FLAG_MOD_REPLACE:
1053 if (el->num_values > 1 && ldb_kv_single_valued(a, el)) {
1054 ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
1055 el->name, ldb_dn_get_linearized(msg2->dn));
1056 ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
1061 * We don't need to check this if we have been
1062 * pre-screened by the repl_meta_data module
1063 * in Samba, or someone else who can claim to
1064 * know what they are doing.
1066 if (!(el->flags & LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK)) {
1067 struct ldb_val *duplicate = NULL;
1069 ret = ldb_msg_find_duplicate_val(ldb, msg2, el,
1071 if (ret != LDB_SUCCESS) {
1074 if (duplicate != NULL) {
1075 ldb_asprintf_errstring(
1077 "attribute '%s': value '%.*s' "
1078 "on '%s' provided more than "
1081 (int)duplicate->length,
1083 ldb_dn_get_linearized(msg2->dn));
1084 ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
1089 /* Checks if element already exists */
1090 idx = ldb_kv_find_element(msg2, el->name);
1092 j = (unsigned int) idx;
1093 el2 = &(msg2->elements[j]);
1095 /* we consider two elements to be
1096 * equal only if the order
1097 * matches. This allows dbcheck to
1098 * fix the ordering on attributes
1099 * where order matters, such as
1102 if (ldb_msg_element_equal_ordered(el, el2)) {
1106 /* Delete the attribute if it exists in the DB */
1107 if (ldb_kv_msg_delete_attribute(
1108 module, ldb_kv, msg2, el->name) != 0) {
1109 ret = LDB_ERR_OTHER;
1114 /* Recreate it with the new values */
1115 if (ldb_kv_msg_add_element(msg2, el) != 0) {
1116 ret = LDB_ERR_OTHER;
1121 ldb_kv_index_add_element(module, ldb_kv, msg2, el);
1122 if (ret != LDB_SUCCESS) {
1128 case LDB_FLAG_MOD_DELETE:
1129 dn = ldb_dn_get_linearized(msg2->dn);
1131 ret = LDB_ERR_OTHER;
1135 if (msg->elements[i].num_values == 0) {
1136 /* Delete the whole attribute */
1137 ret = ldb_kv_msg_delete_attribute(
1141 msg->elements[i].name);
1142 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE &&
1143 control_permissive) {
1146 ldb_asprintf_errstring(ldb,
1147 "attribute '%s': no such attribute for delete on '%s'",
1148 msg->elements[i].name, dn);
1150 if (ret != LDB_SUCCESS) {
1154 /* Delete specified values from an attribute */
1155 for (j=0; j < msg->elements[i].num_values; j++) {
1156 ret = ldb_kv_msg_delete_element(
1160 msg->elements[i].name,
1161 &msg->elements[i].values[j]);
1162 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE &&
1163 control_permissive) {
1165 } else if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
1166 ldb_asprintf_errstring(ldb,
1167 "attribute '%s': no matching attribute value while deleting attribute on '%s'",
1168 msg->elements[i].name, dn);
1170 if (ret != LDB_SUCCESS) {
1177 ldb_asprintf_errstring(ldb,
1178 "attribute '%s': invalid modify flags on '%s': 0x%x",
1179 msg->elements[i].name, ldb_dn_get_linearized(msg->dn),
1180 msg->elements[i].flags & LDB_FLAG_MOD_MASK);
1181 ret = LDB_ERR_PROTOCOL_ERROR;
1186 ret = ldb_kv_store(module, msg2, TDB_MODIFY);
1187 if (ret != LDB_SUCCESS) {
1191 ret = ldb_kv_modified(module, msg2->dn);
1192 if (ret != LDB_SUCCESS) {
1197 TALLOC_FREE(mem_ctx);
1204 static int ldb_kv_modify(struct ldb_kv_context *ctx)
1206 struct ldb_module *module = ctx->module;
1207 struct ldb_request *req = ctx->req;
1208 int ret = LDB_SUCCESS;
1210 ret = ldb_kv_check_special_dn(module, req->op.mod.message);
1211 if (ret != LDB_SUCCESS) {
1215 ldb_request_set_state(req, LDB_ASYNC_PENDING);
1217 if (ldb_kv_cache_load(module) != 0) {
1218 return LDB_ERR_OPERATIONS_ERROR;
1221 ret = ldb_kv_modify_internal(module, req->op.mod.message, req);
1229 static int ldb_kv_rename(struct ldb_kv_context *ctx)
1231 struct ldb_module *module = ctx->module;
1232 void *data = ldb_module_get_private(module);
1233 struct ldb_kv_private *ldb_kv =
1234 talloc_get_type(data, struct ldb_kv_private);
1235 struct ldb_request *req = ctx->req;
1236 struct ldb_message *msg;
1237 int ret = LDB_SUCCESS;
1238 TDB_DATA tdb_key, tdb_key_old;
1239 struct ldb_dn *db_dn;
1241 ldb_request_set_state(req, LDB_ASYNC_PENDING);
1243 if (ldb_kv_cache_load(ctx->module) != 0) {
1244 return LDB_ERR_OPERATIONS_ERROR;
1247 msg = ldb_msg_new(ctx);
1249 return LDB_ERR_OPERATIONS_ERROR;
1252 /* we need to fetch the old record to re-add under the new name */
1253 ret = ldb_kv_search_dn1(module,
1254 req->op.rename.olddn,
1256 LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC);
1257 if (ret != LDB_SUCCESS) {
1258 /* not finding the old record is an error */
1262 /* We need to, before changing the DB, check if the new DN
1263 * exists, so we can return this error to the caller with an
1266 * Even in GUID index mode we use ltdb_key_dn() as we are
1267 * trying to figure out if this is just a case rename
1269 tdb_key = ldb_kv_key_dn(module, msg, req->op.rename.newdn);
1270 if (!tdb_key.dptr) {
1272 return LDB_ERR_OPERATIONS_ERROR;
1275 tdb_key_old = ldb_kv_key_dn(module, msg, req->op.rename.olddn);
1276 if (!tdb_key_old.dptr) {
1278 talloc_free(tdb_key.dptr);
1279 return LDB_ERR_OPERATIONS_ERROR;
1283 * Only declare a conflict if the new DN already exists,
1284 * and it isn't a case change on the old DN
1286 if (tdb_key_old.dsize != tdb_key.dsize
1287 || memcmp(tdb_key.dptr, tdb_key_old.dptr, tdb_key.dsize) != 0) {
1288 ret = ldb_kv_search_base(
1289 module, msg, req->op.rename.newdn, &db_dn);
1290 if (ret == LDB_SUCCESS) {
1291 ret = LDB_ERR_ENTRY_ALREADY_EXISTS;
1292 } else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1297 /* finding the new record already in the DB is an error */
1299 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
1300 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1301 "Entry %s already exists",
1302 ldb_dn_get_linearized(req->op.rename.newdn));
1304 if (ret != LDB_SUCCESS) {
1305 talloc_free(tdb_key_old.dptr);
1306 talloc_free(tdb_key.dptr);
1311 talloc_free(tdb_key_old.dptr);
1312 talloc_free(tdb_key.dptr);
1314 /* Always delete first then add, to avoid conflicts with
1315 * unique indexes. We rely on the transaction to make this
1318 ret = ldb_kv_delete_internal(module, msg->dn);
1319 if (ret != LDB_SUCCESS) {
1324 msg->dn = ldb_dn_copy(msg, req->op.rename.newdn);
1325 if (msg->dn == NULL) {
1327 return LDB_ERR_OPERATIONS_ERROR;
1330 /* We don't check single value as we can have more than 1 with
1331 * deleted attributes. We could go through all elements but that's
1332 * maybe not the most efficient way
1334 ret = ldb_kv_add_internal(module, ldb_kv, msg, false);
1341 static int ldb_kv_start_trans(struct ldb_module *module)
1343 void *data = ldb_module_get_private(module);
1344 struct ldb_kv_private *ldb_kv =
1345 talloc_get_type(data, struct ldb_kv_private);
1347 pid_t pid = getpid();
1349 if (ldb_kv->pid != pid) {
1350 ldb_asprintf_errstring(ldb_module_get_ctx(ldb_kv->module),
1352 ": Reusing ldb opend by pid %d in "
1356 return LDB_ERR_PROTOCOL_ERROR;
1359 /* Do not take out the transaction lock on a read-only DB */
1360 if (ldb_kv->read_only) {
1361 return LDB_ERR_UNWILLING_TO_PERFORM;
1364 if (ldb_kv->kv_ops->begin_write(ldb_kv) != 0) {
1365 return ldb_kv->kv_ops->error(ldb_kv);
1368 ldb_kv_index_transaction_start(module);
1370 ldb_kv->reindex_failed = false;
1376 * Forward declaration to allow prepare_commit to in fact abort the
1379 static int ldb_kv_del_trans(struct ldb_module *module);
1381 static int ldb_kv_prepare_commit(struct ldb_module *module)
1384 void *data = ldb_module_get_private(module);
1385 struct ldb_kv_private *ldb_kv =
1386 talloc_get_type(data, struct ldb_kv_private);
1387 pid_t pid = getpid();
1389 if (ldb_kv->pid != pid) {
1390 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1392 ": Reusing ldb opend by pid %d in "
1396 return LDB_ERR_PROTOCOL_ERROR;
1399 if (!ldb_kv->kv_ops->transaction_active(ldb_kv)) {
1400 ldb_set_errstring(ldb_module_get_ctx(module),
1401 "ltdb_prepare_commit() called "
1402 "without transaction active");
1403 return LDB_ERR_OPERATIONS_ERROR;
1407 * Check if the last re-index failed.
1409 * This can happen if for example a duplicate value was marked
1410 * unique. We must not write a partial re-index into the DB.
1412 if (ldb_kv->reindex_failed) {
1414 * We must instead abort the transaction so we get the
1415 * old values and old index back
1417 ldb_kv_del_trans(module);
1418 ldb_set_errstring(ldb_module_get_ctx(module),
1419 "Failure during re-index, so "
1420 "transaction must be aborted.");
1421 return LDB_ERR_OPERATIONS_ERROR;
1424 ret = ldb_kv_index_transaction_commit(module);
1425 if (ret != LDB_SUCCESS) {
1426 ldb_kv->kv_ops->abort_write(ldb_kv);
1430 if (ldb_kv->kv_ops->prepare_write(ldb_kv) != 0) {
1431 ret = ldb_kv->kv_ops->error(ldb_kv);
1432 ldb_debug_set(ldb_module_get_ctx(module),
1435 "prepare_write): %s -> %s",
1436 ldb_kv->kv_ops->errorstr(ldb_kv),
1441 ldb_kv->prepared_commit = true;
1446 static int ldb_kv_end_trans(struct ldb_module *module)
1449 void *data = ldb_module_get_private(module);
1450 struct ldb_kv_private *ldb_kv =
1451 talloc_get_type(data, struct ldb_kv_private);
1453 if (!ldb_kv->prepared_commit) {
1454 ret = ldb_kv_prepare_commit(module);
1455 if (ret != LDB_SUCCESS) {
1460 ldb_kv->prepared_commit = false;
1462 if (ldb_kv->kv_ops->finish_write(ldb_kv) != 0) {
1463 ret = ldb_kv->kv_ops->error(ldb_kv);
1464 ldb_asprintf_errstring(
1465 ldb_module_get_ctx(module),
1466 "Failure during tdb_transaction_commit(): %s -> %s",
1467 ldb_kv->kv_ops->errorstr(ldb_kv),
1475 static int ldb_kv_del_trans(struct ldb_module *module)
1477 void *data = ldb_module_get_private(module);
1478 struct ldb_kv_private *ldb_kv =
1479 talloc_get_type(data, struct ldb_kv_private);
1481 if (ldb_kv_index_transaction_cancel(module) != 0) {
1482 ldb_kv->kv_ops->abort_write(ldb_kv);
1483 return ldb_kv->kv_ops->error(ldb_kv);
1486 ldb_kv->kv_ops->abort_write(ldb_kv);
1491 return sequenceNumber from @BASEINFO
1493 static int ldb_kv_sequence_number(struct ldb_kv_context *ctx,
1494 struct ldb_extended **ext)
1496 struct ldb_context *ldb;
1497 struct ldb_module *module = ctx->module;
1498 struct ldb_request *req = ctx->req;
1499 void *data = ldb_module_get_private(module);
1500 struct ldb_kv_private *ldb_kv =
1501 talloc_get_type(data, struct ldb_kv_private);
1502 TALLOC_CTX *tmp_ctx = NULL;
1503 struct ldb_seqnum_request *seq;
1504 struct ldb_seqnum_result *res;
1505 struct ldb_message *msg = NULL;
1508 int ret = LDB_SUCCESS;
1510 ldb = ldb_module_get_ctx(module);
1512 seq = talloc_get_type(req->op.extended.data,
1513 struct ldb_seqnum_request);
1515 return LDB_ERR_OPERATIONS_ERROR;
1518 ldb_request_set_state(req, LDB_ASYNC_PENDING);
1520 if (ldb_kv->kv_ops->lock_read(module) != 0) {
1521 return LDB_ERR_OPERATIONS_ERROR;
1524 res = talloc_zero(req, struct ldb_seqnum_result);
1526 ret = LDB_ERR_OPERATIONS_ERROR;
1530 tmp_ctx = talloc_new(req);
1531 if (tmp_ctx == NULL) {
1532 ret = LDB_ERR_OPERATIONS_ERROR;
1536 dn = ldb_dn_new(tmp_ctx, ldb, LDB_KV_BASEINFO);
1538 ret = LDB_ERR_OPERATIONS_ERROR;
1542 msg = ldb_msg_new(tmp_ctx);
1544 ret = LDB_ERR_OPERATIONS_ERROR;
1548 ret = ldb_kv_search_dn1(module, dn, msg, 0);
1549 if (ret != LDB_SUCCESS) {
1553 switch (seq->type) {
1554 case LDB_SEQ_HIGHEST_SEQ:
1555 res->seq_num = ldb_msg_find_attr_as_uint64(msg, LDB_KV_SEQUENCE_NUMBER, 0);
1558 res->seq_num = ldb_msg_find_attr_as_uint64(msg, LDB_KV_SEQUENCE_NUMBER, 0);
1561 case LDB_SEQ_HIGHEST_TIMESTAMP:
1562 date = ldb_msg_find_attr_as_string(msg, LDB_KV_MOD_TIMESTAMP, NULL);
1564 res->seq_num = ldb_string_to_time(date);
1567 /* zero is as good as anything when we don't know */
1572 *ext = talloc_zero(req, struct ldb_extended);
1574 ret = LDB_ERR_OPERATIONS_ERROR;
1577 (*ext)->oid = LDB_EXTENDED_SEQUENCE_NUMBER;
1578 (*ext)->data = talloc_steal(*ext, res);
1581 talloc_free(tmp_ctx);
1583 ldb_kv->kv_ops->unlock_read(module);
1587 static void ldb_kv_request_done(struct ldb_kv_context *ctx, int error)
1589 struct ldb_context *ldb;
1590 struct ldb_request *req;
1591 struct ldb_reply *ares;
1593 ldb = ldb_module_get_ctx(ctx->module);
1596 /* if we already returned an error just return */
1597 if (ldb_request_get_status(req) != LDB_SUCCESS) {
1601 ares = talloc_zero(req, struct ldb_reply);
1604 req->callback(req, NULL);
1607 ares->type = LDB_REPLY_DONE;
1608 ares->error = error;
1610 req->callback(req, ares);
1613 static void ldb_kv_timeout(struct tevent_context *ev,
1614 struct tevent_timer *te,
1618 struct ldb_kv_context *ctx;
1619 ctx = talloc_get_type(private_data, struct ldb_kv_context);
1621 if (!ctx->request_terminated) {
1622 /* request is done now */
1623 ldb_kv_request_done(ctx, LDB_ERR_TIME_LIMIT_EXCEEDED);
1627 /* neutralize the spy */
1628 ctx->spy->ctx = NULL;
1634 static void ldb_kv_request_extended_done(struct ldb_kv_context *ctx,
1635 struct ldb_extended *ext,
1638 struct ldb_context *ldb;
1639 struct ldb_request *req;
1640 struct ldb_reply *ares;
1642 ldb = ldb_module_get_ctx(ctx->module);
1645 /* if we already returned an error just return */
1646 if (ldb_request_get_status(req) != LDB_SUCCESS) {
1650 ares = talloc_zero(req, struct ldb_reply);
1653 req->callback(req, NULL);
1656 ares->type = LDB_REPLY_DONE;
1657 ares->response = ext;
1658 ares->error = error;
1660 req->callback(req, ares);
1663 static void ldb_kv_handle_extended(struct ldb_kv_context *ctx)
1665 struct ldb_extended *ext = NULL;
1668 if (strcmp(ctx->req->op.extended.oid,
1669 LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1670 /* get sequence number */
1671 ret = ldb_kv_sequence_number(ctx, &ext);
1673 /* not recognized */
1674 ret = LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
1677 ldb_kv_request_extended_done(ctx, ext, ret);
1680 static void ldb_kv_callback(struct tevent_context *ev,
1681 struct tevent_timer *te,
1685 struct ldb_kv_context *ctx;
1688 ctx = talloc_get_type(private_data, struct ldb_kv_context);
1690 if (ctx->request_terminated) {
1694 switch (ctx->req->operation) {
1696 ret = ldb_kv_search(ctx);
1699 ret = ldb_kv_add(ctx);
1702 ret = ldb_kv_modify(ctx);
1705 ret = ldb_kv_delete(ctx);
1708 ret = ldb_kv_rename(ctx);
1711 ldb_kv_handle_extended(ctx);
1714 /* no other op supported */
1715 ret = LDB_ERR_PROTOCOL_ERROR;
1718 if (!ctx->request_terminated) {
1719 /* request is done now */
1720 ldb_kv_request_done(ctx, ret);
1725 /* neutralize the spy */
1726 ctx->spy->ctx = NULL;
1732 static int ldb_kv_request_destructor(void *ptr)
1734 struct ldb_kv_req_spy *spy =
1735 talloc_get_type(ptr, struct ldb_kv_req_spy);
1737 if (spy->ctx != NULL) {
1738 spy->ctx->spy = NULL;
1739 spy->ctx->request_terminated = true;
1746 static int ldb_kv_handle_request(struct ldb_module *module,
1747 struct ldb_request *req)
1749 struct ldb_control *control_permissive;
1750 struct ldb_context *ldb;
1751 struct tevent_context *ev;
1752 struct ldb_kv_context *ac;
1753 struct tevent_timer *te;
1757 ldb = ldb_module_get_ctx(module);
1759 control_permissive = ldb_request_get_control(req,
1760 LDB_CONTROL_PERMISSIVE_MODIFY_OID);
1762 for (i = 0; req->controls && req->controls[i]; i++) {
1763 if (req->controls[i]->critical &&
1764 req->controls[i] != control_permissive) {
1765 ldb_asprintf_errstring(ldb, "Unsupported critical extension %s",
1766 req->controls[i]->oid);
1767 return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
1771 if (req->starttime == 0 || req->timeout == 0) {
1772 ldb_set_errstring(ldb, "Invalid timeout settings");
1773 return LDB_ERR_TIME_LIMIT_EXCEEDED;
1776 ev = ldb_handle_get_event_context(req->handle);
1778 ac = talloc_zero(ldb, struct ldb_kv_context);
1781 return LDB_ERR_OPERATIONS_ERROR;
1784 ac->module = module;
1789 te = tevent_add_timer(ev, ac, tv, ldb_kv_callback, ac);
1792 return LDB_ERR_OPERATIONS_ERROR;
1795 if (req->timeout > 0) {
1796 tv.tv_sec = req->starttime + req->timeout;
1799 tevent_add_timer(ev, ac, tv, ldb_kv_timeout, ac);
1800 if (NULL == ac->timeout_event) {
1802 return LDB_ERR_OPERATIONS_ERROR;
1806 /* set a spy so that we do not try to use the request context
1807 * if it is freed before ltdb_callback fires */
1808 ac->spy = talloc(req, struct ldb_kv_req_spy);
1809 if (NULL == ac->spy) {
1811 return LDB_ERR_OPERATIONS_ERROR;
1815 talloc_set_destructor((TALLOC_CTX *)ac->spy, ldb_kv_request_destructor);
1820 static int ldb_kv_init_rootdse(struct ldb_module *module)
1822 /* ignore errors on this - we expect it for non-sam databases */
1823 ldb_mod_register_control(module, LDB_CONTROL_PERMISSIVE_MODIFY_OID);
1825 /* there can be no module beyond the backend, just return */
1829 static int ldb_kv_lock_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->lock_read(module);
1837 static int ldb_kv_unlock_read(struct ldb_module *module)
1839 void *data = ldb_module_get_private(module);
1840 struct ldb_kv_private *ldb_kv =
1841 talloc_get_type(data, struct ldb_kv_private);
1842 return ldb_kv->kv_ops->unlock_read(module);
1845 static const struct ldb_module_ops ldb_kv_ops = {
1847 .init_context = ldb_kv_init_rootdse,
1848 .search = ldb_kv_handle_request,
1849 .add = ldb_kv_handle_request,
1850 .modify = ldb_kv_handle_request,
1851 .del = ldb_kv_handle_request,
1852 .rename = ldb_kv_handle_request,
1853 .extended = ldb_kv_handle_request,
1854 .start_transaction = ldb_kv_start_trans,
1855 .end_transaction = ldb_kv_end_trans,
1856 .prepare_commit = ldb_kv_prepare_commit,
1857 .del_transaction = ldb_kv_del_trans,
1858 .read_lock = ldb_kv_lock_read,
1859 .read_unlock = ldb_kv_unlock_read,
1862 int ldb_kv_init_store(struct ldb_kv_private *ldb_kv,
1864 struct ldb_context *ldb,
1865 const char *options[],
1866 struct ldb_module **_module)
1868 if (getenv("LDB_WARN_UNINDEXED")) {
1869 ldb_kv->warn_unindexed = true;
1872 if (getenv("LDB_WARN_REINDEX")) {
1873 ldb_kv->warn_reindex = true;
1876 ldb_kv->sequence_number = 0;
1878 ldb_kv->pid = getpid();
1880 ldb_kv->module = ldb_module_new(ldb, ldb, name, &ldb_kv_ops);
1881 if (!ldb_kv->module) {
1883 talloc_free(ldb_kv);
1884 return LDB_ERR_OPERATIONS_ERROR;
1886 ldb_module_set_private(ldb_kv->module, ldb_kv);
1887 talloc_steal(ldb_kv->module, ldb_kv);
1889 if (ldb_kv_cache_load(ldb_kv->module) != 0) {
1890 ldb_asprintf_errstring(ldb, "Unable to load ltdb cache "
1891 "records for backend '%s'", name);
1892 talloc_free(ldb_kv->module);
1893 return LDB_ERR_OPERATIONS_ERROR;
1896 *_module = ldb_kv->module;
1898 * Set or override the maximum key length
1900 * The ldb_mdb code will have set this to 511, but our tests
1901 * set this even smaller (to make the tests more practical).
1903 * This must only be used for the selftest as the length
1904 * becomes encoded in the index keys.
1907 const char *len_str =
1908 ldb_options_find(ldb, options,
1909 "max_key_len_for_self_test");
1910 if (len_str != NULL) {
1911 unsigned len = strtoul(len_str, NULL, 0);
1912 ldb_kv->max_key_length = len;
1917 * Override full DB scans
1919 * A full DB scan is expensive on a large database. This
1920 * option is for testing to show that the full DB scan is not
1924 const char *len_str =
1925 ldb_options_find(ldb, options,
1926 "disable_full_db_scan_for_self_test");
1927 if (len_str != NULL) {
1928 ldb_kv->disable_full_db_scan = true;