4 Copyright (C) Andrew Tridgell 2004-2009
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 3 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, see <http://www.gnu.org/licenses/>.
27 * Component: ldb key value backend - indexing
29 * Description: indexing routines for ldb key value backend
31 * Author: Andrew Tridgell
36 LDB Index design and choice of key:
37 =======================================
39 LDB has index records held as LDB objects with a special record like:
43 value may be base64 encoded, if it is deemed not printable:
45 dn: @INDEX:attr::base64-value
47 In each record, there is two possible formats:
49 The original format is:
50 -----------------------
52 dn: @INDEX:NAME:DNSUPDATEPROXY
54 @IDX: CN=DnsUpdateProxy,CN=Users,DC=addom,DC=samba,DC=example,DC=com
56 In this format, @IDX is multi-valued, one entry for each match
58 The corrosponding entry is stored in a TDB record with key:
60 DN=CN=DNSUPDATEPROXY,CN=USERS,DC=ADDOM,DC=SAMBA,DC=EXAMPLE,DC=COM
62 (This allows a scope BASE search to directly find the record via
63 a simple casefold of the DN).
65 The original mixed-case DN is stored in the entry iself.
68 The new 'GUID index' format is:
69 -------------------------------
71 dn: @INDEX:NAME:DNSUPDATEPROXY
73 @IDX: <binary GUID>[<binary GUID>[...]]
75 The binary guid is 16 bytes, as bytes and not expanded as hexidecimal
76 or pretty-printed. The GUID is chosen from the message to be stored
77 by the @IDXGUID attribute on @INDEXLIST.
79 If there are multiple values the @IDX value simply becomes longer,
82 The corrosponding entry is stored in a TDB record with key:
86 This allows a very quick translation between the fixed-length index
87 values and the TDB key, while seperating entries from other data
88 in the TDB, should they be unlucky enough to start with the bytes of
91 Additionally, this allows a scope BASE search to directly find the
92 record via a simple match on a GUID= extended DN, controlled via
93 @IDX_DN_GUID on @INDEXLIST
95 Exception for special @ DNs:
97 @BASEINFO, @INDEXLIST and all other special DNs are stored as per the
98 original format, as they are never referenced in an index and are used
99 to bootstrap the database.
102 Control points for choice of index mode
103 ---------------------------------------
105 The choice of index and TDB key mode is made based (for example, from
106 Samba) on entries in the @INDEXLIST DN:
112 By default, the original DN format is used.
115 Control points for choosing indexed attributes
116 ----------------------------------------------
118 @IDXATTR controls if an attribute is indexed
121 @IDXATTR: samAccountName
122 @IDXATTR: nETBIOSName
128 void ldb_schema_set_override_GUID_index(struct ldb_context *ldb,
129 const char *GUID_index_attribute,
130 const char *GUID_index_dn_component)
132 This is used, particularly in combination with the below, instead of
133 the @IDXGUID and @IDX_DN_GUID values in @INDEXLIST.
135 void ldb_schema_set_override_indexlist(struct ldb_context *ldb,
136 bool one_level_indexes);
137 void ldb_schema_attribute_set_override_handler(struct ldb_context *ldb,
138 ldb_attribute_handler_override_fn_t override,
141 When the above two functions are called in combination, the @INDEXLIST
142 values are not read from the DB, so
143 ldb_schema_set_override_GUID_index() must be called.
148 #include "../ldb_tdb/ldb_tdb.h"
149 #include "ldb_private.h"
150 #include "lib/util/binsearch.h"
156 * Do not optimise the intersection of this list,
157 * we must never return an entry not in this
158 * list. This allows the index for
159 * SCOPE_ONELEVEL to be trusted.
164 struct ldb_kv_idxptr {
165 struct tdb_context *itdb;
169 enum key_truncation {
174 static int ldb_kv_write_index_dn_guid(struct ldb_module *module,
175 const struct ldb_message *msg,
177 static int ldb_kv_index_dn_base_dn(struct ldb_module *module,
178 struct ldb_kv_private *ldb_kv,
179 struct ldb_dn *base_dn,
180 struct dn_list *dn_list,
181 enum key_truncation *truncation);
183 static void ldb_kv_dn_list_sort(struct ldb_kv_private *ldb_kv,
184 struct dn_list *list);
186 /* we put a @IDXVERSION attribute on index entries. This
187 allows us to tell if it was written by an older version
189 #define LDB_KV_INDEXING_VERSION 2
191 #define LDB_KV_GUID_INDEXING_VERSION 3
193 static unsigned ldb_kv_max_key_length(struct ldb_kv_private *ldb_kv)
195 if (ldb_kv->max_key_length == 0) {
198 return ldb_kv->max_key_length;
201 /* enable the idxptr mode when transactions start */
202 int ldb_kv_index_transaction_start(
203 struct ldb_module *module,
206 struct ldb_kv_private *ldb_kv = talloc_get_type(
207 ldb_module_get_private(module), struct ldb_kv_private);
208 ldb_kv->idxptr = talloc_zero(ldb_kv, struct ldb_kv_idxptr);
209 if (ldb_kv->idxptr == NULL) {
210 return ldb_oom(ldb_module_get_ctx(module));
213 ldb_kv->idxptr->itdb = tdb_open(
219 if (ldb_kv->idxptr->itdb == NULL) {
220 return LDB_ERR_OPERATIONS_ERROR;
227 see if two ldb_val structures contain exactly the same data
228 return -1 or 1 for a mismatch, 0 for match
230 static int ldb_val_equal_exact_for_qsort(const struct ldb_val *v1,
231 const struct ldb_val *v2)
233 if (v1->length > v2->length) {
236 if (v1->length < v2->length) {
239 return memcmp(v1->data, v2->data, v1->length);
243 see if two ldb_val structures contain exactly the same data
244 return -1 or 1 for a mismatch, 0 for match
246 static int ldb_val_equal_exact_ordered(const struct ldb_val v1,
247 const struct ldb_val *v2)
249 if (v1.length > v2->length) {
252 if (v1.length < v2->length) {
255 return memcmp(v1.data, v2->data, v1.length);
260 find a entry in a dn_list, using a ldb_val. Uses a case sensitive
261 binary-safe comparison for the 'dn' returns -1 if not found
263 This is therefore safe when the value is a GUID in the future
265 static int ldb_kv_dn_list_find_val(struct ldb_kv_private *ldb_kv,
266 const struct dn_list *list,
267 const struct ldb_val *v)
270 struct ldb_val *exact = NULL, *next = NULL;
272 if (ldb_kv->cache->GUID_index_attribute == NULL) {
273 for (i=0; i<list->count; i++) {
274 if (ldb_val_equal_exact(&list->dn[i], v) == 1) {
281 BINARY_ARRAY_SEARCH_GTE(list->dn, list->count,
282 *v, ldb_val_equal_exact_ordered,
287 /* Not required, but keeps the compiler quiet */
292 i = exact - list->dn;
297 find a entry in a dn_list. Uses a case sensitive comparison with the dn
298 returns -1 if not found
300 static int ldb_kv_dn_list_find_msg(struct ldb_kv_private *ldb_kv,
301 struct dn_list *list,
302 const struct ldb_message *msg)
305 const struct ldb_val *key_val;
306 if (ldb_kv->cache->GUID_index_attribute == NULL) {
307 const char *dn_str = ldb_dn_get_linearized(msg->dn);
308 v.data = discard_const_p(unsigned char, dn_str);
309 v.length = strlen(dn_str);
311 key_val = ldb_msg_find_ldb_val(
312 msg, ldb_kv->cache->GUID_index_attribute);
313 if (key_val == NULL) {
318 return ldb_kv_dn_list_find_val(ldb_kv, list, &v);
322 this is effectively a cast function, but with lots of paranoia
323 checks and also copes with CPUs that are fussy about pointer
326 static struct dn_list *ldb_kv_index_idxptr(struct ldb_module *module,
330 struct dn_list *list;
331 if (rec.dsize != sizeof(void *)) {
332 ldb_asprintf_errstring(ldb_module_get_ctx(module),
333 "Bad data size for idxptr %u", (unsigned)rec.dsize);
336 /* note that we can't just use a cast here, as rec.dptr may
337 not be aligned sufficiently for a pointer. A cast would cause
338 platforms like some ARM CPUs to crash */
339 memcpy(&list, rec.dptr, sizeof(void *));
340 list = talloc_get_type(list, struct dn_list);
342 ldb_asprintf_errstring(ldb_module_get_ctx(module),
343 "Bad type '%s' for idxptr",
344 talloc_get_name(list));
347 if (check_parent && list->dn && talloc_parent(list->dn) != list) {
348 ldb_asprintf_errstring(ldb_module_get_ctx(module),
349 "Bad parent '%s' for idxptr",
350 talloc_get_name(talloc_parent(list->dn)));
357 return the @IDX list in an index entry for a dn as a
360 static int ldb_kv_dn_list_load(struct ldb_module *module,
361 struct ldb_kv_private *ldb_kv,
363 struct dn_list *list)
365 struct ldb_message *msg;
367 struct ldb_message_element *el;
369 struct dn_list *list2;
375 /* see if we have any in-memory index entries */
376 if (ldb_kv->idxptr == NULL || ldb_kv->idxptr->itdb == NULL) {
380 key.dptr = discard_const_p(unsigned char, ldb_dn_get_linearized(dn));
381 key.dsize = strlen((char *)key.dptr);
383 rec = tdb_fetch(ldb_kv->idxptr->itdb, key);
384 if (rec.dptr == NULL) {
388 /* we've found an in-memory index entry */
389 list2 = ldb_kv_index_idxptr(module, rec, true);
392 return LDB_ERR_OPERATIONS_ERROR;
400 msg = ldb_msg_new(list);
402 return LDB_ERR_OPERATIONS_ERROR;
405 ret = ldb_kv_search_dn1(module,
408 LDB_UNPACK_DATA_FLAG_NO_DN |
410 * The entry point ldb_kv_search_indexed is
411 * only called from the read-locked
414 LDB_UNPACK_DATA_FLAG_READ_LOCKED);
415 if (ret != LDB_SUCCESS) {
420 el = ldb_msg_find_element(msg, LDB_KV_IDX);
426 version = ldb_msg_find_attr_as_int(msg, LDB_KV_IDXVERSION, 0);
429 * we avoid copying the strings by stealing the list. We have
430 * to steal msg onto el->values (which looks odd) because
431 * the memory is allocated on msg, not on each value.
433 if (ldb_kv->cache->GUID_index_attribute == NULL) {
434 /* check indexing version number */
435 if (version != LDB_KV_INDEXING_VERSION) {
436 ldb_debug_set(ldb_module_get_ctx(module),
438 "Wrong DN index version %d "
439 "expected %d for %s",
440 version, LDB_KV_INDEXING_VERSION,
441 ldb_dn_get_linearized(dn));
443 return LDB_ERR_OPERATIONS_ERROR;
446 talloc_steal(el->values, msg);
447 list->dn = talloc_steal(list, el->values);
448 list->count = el->num_values;
451 if (version != LDB_KV_GUID_INDEXING_VERSION) {
452 /* This is quite likely during the DB startup
453 on first upgrade to using a GUID index */
454 ldb_debug_set(ldb_module_get_ctx(module),
456 "Wrong GUID index version %d "
457 "expected %d for %s",
458 version, LDB_KV_GUID_INDEXING_VERSION,
459 ldb_dn_get_linearized(dn));
461 return LDB_ERR_OPERATIONS_ERROR;
464 if (el->num_values == 0) {
466 return LDB_ERR_OPERATIONS_ERROR;
469 if ((el->values[0].length % LDB_KV_GUID_SIZE) != 0) {
471 return LDB_ERR_OPERATIONS_ERROR;
474 list->count = el->values[0].length / LDB_KV_GUID_SIZE;
475 list->dn = talloc_array(list, struct ldb_val, list->count);
476 if (list->dn == NULL) {
478 return LDB_ERR_OPERATIONS_ERROR;
482 * The actual data is on msg.
484 talloc_steal(list->dn, msg);
485 for (i = 0; i < list->count; i++) {
487 = &el->values[0].data[i * LDB_KV_GUID_SIZE];
488 list->dn[i].length = LDB_KV_GUID_SIZE;
492 /* We don't need msg->elements any more */
493 talloc_free(msg->elements);
497 int ldb_kv_key_dn_from_idx(struct ldb_module *module,
498 struct ldb_kv_private *ldb_kv,
501 struct ldb_val *ldb_key)
503 struct ldb_context *ldb = ldb_module_get_ctx(module);
506 enum key_truncation truncation = KEY_NOT_TRUNCATED;
507 struct dn_list *list = talloc(mem_ctx, struct dn_list);
510 return LDB_ERR_OPERATIONS_ERROR;
513 ret = ldb_kv_index_dn_base_dn(module, ldb_kv, dn, list, &truncation);
514 if (ret != LDB_SUCCESS) {
519 if (list->count == 0) {
521 return LDB_ERR_NO_SUCH_OBJECT;
524 if (list->count > 1 && truncation == KEY_NOT_TRUNCATED) {
525 const char *dn_str = ldb_dn_get_linearized(dn);
526 ldb_asprintf_errstring(ldb_module_get_ctx(module),
528 ": Failed to read DN index "
529 "against %s for %s: too many "
531 ldb_kv->cache->GUID_index_attribute,
535 return LDB_ERR_CONSTRAINT_VIOLATION;
538 if (list->count > 0 && truncation == KEY_TRUNCATED) {
540 * DN key has been truncated, need to inspect the actual
541 * records to locate the actual DN
545 for (i=0; i < list->count; i++) {
546 uint8_t guid_key[LDB_KV_GUID_KEY_SIZE];
547 struct ldb_val key = {
549 .length = sizeof(guid_key)
551 const int flags = LDB_UNPACK_DATA_FLAG_NO_ATTRS;
552 struct ldb_message *rec = ldb_msg_new(ldb);
555 return LDB_ERR_OPERATIONS_ERROR;
558 ret = ldb_kv_idx_to_key(
559 module, ldb_kv, ldb, &list->dn[i], &key);
560 if (ret != LDB_SUCCESS) {
567 ldb_kv_search_key(module, ldb_kv, key, rec, flags);
568 if (key.data != guid_key) {
569 TALLOC_FREE(key.data);
571 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
573 * the record has disappeared?
574 * yes, this can happen
580 if (ret != LDB_SUCCESS) {
581 /* an internal error */
584 return LDB_ERR_OPERATIONS_ERROR;
588 * We found the actual DN that we wanted from in the
589 * multiple values that matched the index
590 * (due to truncation), so return that.
593 if (ldb_dn_compare(dn, rec->dn) == 0) {
601 * We matched the index but the actual DN we wanted
606 return LDB_ERR_NO_SUCH_OBJECT;
610 /* The ldb_key memory is allocated by the caller */
611 ret = ldb_kv_guid_to_key(&list->dn[index], ldb_key);
614 if (ret != LDB_SUCCESS) {
615 return LDB_ERR_OPERATIONS_ERROR;
624 save a dn_list into a full @IDX style record
626 static int ldb_kv_dn_list_store_full(struct ldb_module *module,
627 struct ldb_kv_private *ldb_kv,
629 struct dn_list *list)
631 struct ldb_message *msg;
634 msg = ldb_msg_new(module);
636 return ldb_module_oom(module);
641 if (list->count == 0) {
642 ret = ldb_kv_delete_noindex(module, msg);
643 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
650 if (ldb_kv->cache->GUID_index_attribute == NULL) {
651 ret = ldb_msg_add_fmt(msg, LDB_KV_IDXVERSION, "%u",
652 LDB_KV_INDEXING_VERSION);
653 if (ret != LDB_SUCCESS) {
655 return ldb_module_oom(module);
658 ret = ldb_msg_add_fmt(msg, LDB_KV_IDXVERSION, "%u",
659 LDB_KV_GUID_INDEXING_VERSION);
660 if (ret != LDB_SUCCESS) {
662 return ldb_module_oom(module);
666 if (list->count > 0) {
667 struct ldb_message_element *el;
669 ret = ldb_msg_add_empty(msg, LDB_KV_IDX, LDB_FLAG_MOD_ADD, &el);
670 if (ret != LDB_SUCCESS) {
672 return ldb_module_oom(module);
675 if (ldb_kv->cache->GUID_index_attribute == NULL) {
676 el->values = list->dn;
677 el->num_values = list->count;
681 el->values = talloc_array(msg,
683 if (el->values == NULL) {
685 return ldb_module_oom(module);
688 v.data = talloc_array_size(el->values,
691 if (v.data == NULL) {
693 return ldb_module_oom(module);
696 v.length = talloc_get_size(v.data);
698 for (i = 0; i < list->count; i++) {
699 if (list->dn[i].length !=
702 return ldb_module_operr(module);
704 memcpy(&v.data[LDB_KV_GUID_SIZE*i],
713 ret = ldb_kv_store(module, msg, TDB_REPLACE);
719 save a dn_list into the database, in either @IDX or internal format
721 static int ldb_kv_dn_list_store(struct ldb_module *module,
723 struct dn_list *list)
725 struct ldb_kv_private *ldb_kv = talloc_get_type(
726 ldb_module_get_private(module), struct ldb_kv_private);
729 struct dn_list *list2;
731 if (ldb_kv->idxptr == NULL) {
732 return ldb_kv_dn_list_store_full(module, ldb_kv, dn, list);
735 key.dptr = discard_const_p(unsigned char, ldb_dn_get_linearized(dn));
736 if (key.dptr == NULL) {
737 return LDB_ERR_OPERATIONS_ERROR;
739 key.dsize = strlen((char *)key.dptr);
741 rec = tdb_fetch(ldb_kv->idxptr->itdb, key);
742 if (rec.dptr != NULL) {
743 list2 = ldb_kv_index_idxptr(module, rec, false);
746 return LDB_ERR_OPERATIONS_ERROR;
749 list2->dn = talloc_steal(list2, list->dn);
750 list2->count = list->count;
754 list2 = talloc(ldb_kv->idxptr, struct dn_list);
756 return LDB_ERR_OPERATIONS_ERROR;
758 list2->dn = talloc_steal(list2, list->dn);
759 list2->count = list->count;
761 rec.dptr = (uint8_t *)&list2;
762 rec.dsize = sizeof(void *);
766 * This is not a store into the main DB, but into an in-memory
767 * TDB, so we don't need a guard on ltdb->read_only
769 ret = tdb_store(ldb_kv->idxptr->itdb, key, rec, TDB_INSERT);
771 return ltdb_err_map(tdb_error(ldb_kv->idxptr->itdb));
777 traverse function for storing the in-memory index entries on disk
779 static int ldb_kv_index_traverse_store(struct tdb_context *tdb,
784 struct ldb_module *module = state;
785 struct ldb_kv_private *ldb_kv = talloc_get_type(
786 ldb_module_get_private(module), struct ldb_kv_private);
788 struct ldb_context *ldb = ldb_module_get_ctx(module);
790 struct dn_list *list;
792 list = ldb_kv_index_idxptr(module, data, true);
794 ldb_kv->idxptr->error = LDB_ERR_OPERATIONS_ERROR;
799 v.length = strnlen((char *)key.dptr, key.dsize);
801 dn = ldb_dn_from_ldb_val(module, ldb, &v);
803 ldb_asprintf_errstring(ldb, "Failed to parse index key %*.*s as an LDB DN", (int)v.length, (int)v.length, (const char *)v.data);
804 ldb_kv->idxptr->error = LDB_ERR_OPERATIONS_ERROR;
808 ldb_kv->idxptr->error =
809 ldb_kv_dn_list_store_full(module, ldb_kv, dn, list);
811 if (ldb_kv->idxptr->error != 0) {
817 /* cleanup the idxptr mode when transaction commits */
818 int ldb_kv_index_transaction_commit(struct ldb_module *module)
820 struct ldb_kv_private *ldb_kv = talloc_get_type(
821 ldb_module_get_private(module), struct ldb_kv_private);
824 struct ldb_context *ldb = ldb_module_get_ctx(module);
826 ldb_reset_err_string(ldb);
828 if (ldb_kv->idxptr->itdb) {
830 ldb_kv->idxptr->itdb, ldb_kv_index_traverse_store, module);
831 tdb_close(ldb_kv->idxptr->itdb);
834 ret = ldb_kv->idxptr->error;
835 if (ret != LDB_SUCCESS) {
836 if (!ldb_errstring(ldb)) {
837 ldb_set_errstring(ldb, ldb_strerror(ret));
839 ldb_asprintf_errstring(ldb, "Failed to store index records in transaction commit: %s", ldb_errstring(ldb));
842 talloc_free(ldb_kv->idxptr);
843 ldb_kv->idxptr = NULL;
847 /* cleanup the idxptr mode when transaction cancels */
848 int ldb_kv_index_transaction_cancel(struct ldb_module *module)
850 struct ldb_kv_private *ldb_kv = talloc_get_type(
851 ldb_module_get_private(module), struct ldb_kv_private);
852 if (ldb_kv->idxptr && ldb_kv->idxptr->itdb) {
853 tdb_close(ldb_kv->idxptr->itdb);
855 talloc_free(ldb_kv->idxptr);
856 ldb_kv->idxptr = NULL;
862 return the dn key to be used for an index
863 the caller is responsible for freeing
865 static struct ldb_dn *ldb_kv_index_key(struct ldb_context *ldb,
866 struct ldb_kv_private *ldb_kv,
868 const struct ldb_val *value,
869 const struct ldb_schema_attribute **ap,
870 enum key_truncation *truncation)
874 const struct ldb_schema_attribute *a = NULL;
875 char *attr_folded = NULL;
876 const char *attr_for_dn = NULL;
878 bool should_b64_encode;
880 unsigned int max_key_length = ldb_kv_max_key_length(ldb_kv);
883 const size_t indx_len = sizeof(LDB_KV_INDEX) - 1;
884 unsigned frmt_len = 0;
885 const size_t additional_key_length = 4;
886 unsigned int num_separators = 3; /* Estimate for overflow check */
887 const size_t min_data = 1;
888 const size_t min_key_length = additional_key_length
889 + indx_len + num_separators + min_data;
890 struct ldb_val empty;
893 * Accept a NULL value as a request for a key with no value. This is
894 * different from passing an empty value, which might be given
895 * significance by some canonicalise functions.
897 bool empty_val = value == NULL;
900 empty.data = discard_const_p(unsigned char, "");
904 if (attr[0] == '@') {
911 attr_folded = ldb_attr_casefold(ldb, attr);
916 attr_for_dn = attr_folded;
918 a = ldb_schema_attribute_by_name(ldb, attr);
926 ldb_attr_handler_t fn;
927 if (a->syntax->index_format_fn &&
928 ldb_kv->cache->GUID_index_attribute != NULL) {
929 fn = a->syntax->index_format_fn;
931 fn = a->syntax->canonicalise_fn;
933 r = fn(ldb, ldb, value, &v);
934 if (r != LDB_SUCCESS) {
935 const char *errstr = ldb_errstring(ldb);
936 /* canonicalisation can be refused. For
937 example, a attribute that takes wildcards
938 will refuse to canonicalise if the value
939 contains a wildcard */
940 ldb_asprintf_errstring(ldb,
943 "attribute '%s':%s%s%s",
944 attr, ldb_strerror(r),
947 talloc_free(attr_folded);
952 attr_len = strlen(attr_for_dn);
955 * Check if there is any hope this will fit into the DB.
956 * Overflow here is not actually critical the code below
957 * checks again to make the printf and the DB does another
958 * check for too long keys
960 if (max_key_length - attr_len < min_key_length) {
961 ldb_asprintf_errstring(
963 __location__ ": max_key_length "
964 "is too small (%u) < (%u)",
966 (unsigned)(min_key_length + attr_len));
967 talloc_free(attr_folded);
972 * ltdb_key_dn() makes something 4 bytes longer, it adds a leading
973 * "DN=" and a trailing string terminator
975 max_key_length -= additional_key_length;
978 * We do not base 64 encode a DN in a key, it has already been
979 * casefold and lineraized, that is good enough. That already
980 * avoids embedded NUL etc.
982 if (ldb_kv->cache->GUID_index_attribute != NULL) {
983 if (strcmp(attr, LDB_KV_IDXDN) == 0) {
984 should_b64_encode = false;
985 } else if (strcmp(attr, LDB_KV_IDXONE) == 0) {
987 * We can only change the behaviour for IDXONE
988 * when the GUID index is enabled
990 should_b64_encode = false;
993 = ldb_should_b64_encode(ldb, &v);
996 should_b64_encode = ldb_should_b64_encode(ldb, &v);
999 if (should_b64_encode) {
1000 size_t vstr_len = 0;
1001 char *vstr = ldb_base64_encode(ldb, (char *)v.data, v.length);
1003 talloc_free(attr_folded);
1006 vstr_len = strlen(vstr);
1008 * Overflow here is not critical as we only use this
1009 * to choose the printf truncation
1011 key_len = num_separators + indx_len + attr_len + vstr_len;
1012 if (key_len > max_key_length) {
1013 size_t excess = key_len - max_key_length;
1014 frmt_len = vstr_len - excess;
1015 *truncation = KEY_TRUNCATED;
1017 * Truncated keys are placed in a separate key space
1018 * from the non truncated keys
1019 * Note: the double hash "##" is not a typo and
1020 * indicates that the following value is base64 encoded
1022 ret = ldb_dn_new_fmt(ldb, ldb, "%s#%s##%.*s",
1023 LDB_KV_INDEX, attr_for_dn,
1026 frmt_len = vstr_len;
1027 *truncation = KEY_NOT_TRUNCATED;
1029 * Note: the double colon "::" is not a typo and
1030 * indicates that the following value is base64 encoded
1032 ret = ldb_dn_new_fmt(ldb, ldb, "%s:%s::%.*s",
1033 LDB_KV_INDEX, attr_for_dn,
1038 /* Only need two seperators */
1042 * Overflow here is not critical as we only use this
1043 * to choose the printf truncation
1045 key_len = num_separators + indx_len + attr_len + (int)v.length;
1046 if (key_len > max_key_length) {
1047 size_t excess = key_len - max_key_length;
1048 frmt_len = v.length - excess;
1049 *truncation = KEY_TRUNCATED;
1051 * Truncated keys are placed in a separate key space
1052 * from the non truncated keys
1054 ret = ldb_dn_new_fmt(ldb, ldb, "%s#%s#%.*s",
1055 LDB_KV_INDEX, attr_for_dn,
1056 frmt_len, (char *)v.data);
1058 frmt_len = v.length;
1059 *truncation = KEY_NOT_TRUNCATED;
1060 ret = ldb_dn_new_fmt(ldb, ldb, "%s:%s:%.*s",
1061 LDB_KV_INDEX, attr_for_dn,
1062 frmt_len, (char *)v.data);
1066 if (v.data != value->data && !empty_val) {
1067 talloc_free(v.data);
1069 talloc_free(attr_folded);
1075 see if a attribute value is in the list of indexed attributes
1077 static bool ldb_kv_is_indexed(struct ldb_module *module,
1078 struct ldb_kv_private *ldb_kv,
1081 struct ldb_context *ldb = ldb_module_get_ctx(module);
1083 struct ldb_message_element *el;
1085 if ((ldb_kv->cache->GUID_index_attribute != NULL) &&
1086 (ldb_attr_cmp(attr, ldb_kv->cache->GUID_index_attribute) == 0)) {
1087 /* Implicity covered, this is the index key */
1090 if (ldb->schema.index_handler_override) {
1091 const struct ldb_schema_attribute *a
1092 = ldb_schema_attribute_by_name(ldb, attr);
1098 if (a->flags & LDB_ATTR_FLAG_INDEXED) {
1105 if (!ldb_kv->cache->attribute_indexes) {
1109 el = ldb_msg_find_element(ldb_kv->cache->indexlist, LDB_KV_IDXATTR);
1114 /* TODO: this is too expensive! At least use a binary search */
1115 for (i=0; i<el->num_values; i++) {
1116 if (ldb_attr_cmp((char *)el->values[i].data, attr) == 0) {
1124 in the following logic functions, the return value is treated as
1127 LDB_SUCCESS: we found some matching index values
1129 LDB_ERR_NO_SUCH_OBJECT: we know for sure that no object matches
1131 LDB_ERR_OPERATIONS_ERROR: indexing could not answer the call,
1132 we'll need a full search
1136 return a list of dn's that might match a simple indexed search (an
1137 equality search only)
1139 static int ldb_kv_index_dn_simple(struct ldb_module *module,
1140 struct ldb_kv_private *ldb_kv,
1141 const struct ldb_parse_tree *tree,
1142 struct dn_list *list)
1144 struct ldb_context *ldb;
1147 enum key_truncation truncation = KEY_NOT_TRUNCATED;
1149 ldb = ldb_module_get_ctx(module);
1154 /* if the attribute isn't in the list of indexed attributes then
1155 this node needs a full search */
1156 if (!ldb_kv_is_indexed(module, ldb_kv, tree->u.equality.attr)) {
1157 return LDB_ERR_OPERATIONS_ERROR;
1160 /* the attribute is indexed. Pull the list of DNs that match the
1162 dn = ldb_kv_index_key(ldb,
1164 tree->u.equality.attr,
1165 &tree->u.equality.value,
1169 * We ignore truncation here and allow multi-valued matches
1170 * as ltdb_search_indexed will filter out the wrong one in
1171 * ltdb_index_filter() which calls ldb_match_message().
1173 if (!dn) return LDB_ERR_OPERATIONS_ERROR;
1175 ret = ldb_kv_dn_list_load(module, ldb_kv, dn, list);
1180 static bool list_union(struct ldb_context *ldb,
1181 struct ldb_kv_private *ldb_kv,
1182 struct dn_list *list,
1183 struct dn_list *list2);
1186 return a list of dn's that might match a leaf indexed search
1188 static int ldb_kv_index_dn_leaf(struct ldb_module *module,
1189 struct ldb_kv_private *ldb_kv,
1190 const struct ldb_parse_tree *tree,
1191 struct dn_list *list)
1193 if (ldb_kv->disallow_dn_filter &&
1194 (ldb_attr_cmp(tree->u.equality.attr, "dn") == 0)) {
1195 /* in AD mode we do not support "(dn=...)" search filters */
1200 if (tree->u.equality.attr[0] == '@') {
1201 /* Do not allow a indexed search against an @ */
1206 if (ldb_attr_dn(tree->u.equality.attr) == 0) {
1207 enum key_truncation truncation = KEY_NOT_TRUNCATED;
1208 bool valid_dn = false;
1210 = ldb_dn_from_ldb_val(list,
1211 ldb_module_get_ctx(module),
1212 &tree->u.equality.value);
1214 /* If we can't parse it, no match */
1220 valid_dn = ldb_dn_validate(dn);
1221 if (valid_dn == false) {
1222 /* If we can't parse it, no match */
1229 * Re-use the same code we use for a SCOPE_BASE
1232 * We can't call TALLOC_FREE(dn) as this must belong
1233 * to list for the memory to remain valid.
1235 return ldb_kv_index_dn_base_dn(
1236 module, ldb_kv, dn, list, &truncation);
1238 * We ignore truncation here and allow multi-valued matches
1239 * as ltdb_search_indexed will filter out the wrong one in
1240 * ltdb_index_filter() which calls ldb_match_message().
1243 } else if ((ldb_kv->cache->GUID_index_attribute != NULL) &&
1244 (ldb_attr_cmp(tree->u.equality.attr,
1245 ldb_kv->cache->GUID_index_attribute) == 0)) {
1247 struct ldb_context *ldb = ldb_module_get_ctx(module);
1248 list->dn = talloc_array(list, struct ldb_val, 1);
1249 if (list->dn == NULL) {
1250 ldb_module_oom(module);
1251 return LDB_ERR_OPERATIONS_ERROR;
1254 * We need to go via the canonicalise_fn() to
1255 * ensure we get the index in binary, rather
1258 ret = ldb_kv->GUID_index_syntax->canonicalise_fn(
1259 ldb, list->dn, &tree->u.equality.value, &list->dn[0]);
1260 if (ret != LDB_SUCCESS) {
1261 return LDB_ERR_OPERATIONS_ERROR;
1267 return ldb_kv_index_dn_simple(module, ldb_kv, tree, list);
1275 static bool list_intersect(struct ldb_context *ldb,
1276 struct ldb_kv_private *ldb_kv,
1277 struct dn_list *list,
1278 const struct dn_list *list2)
1280 const struct dn_list *short_list, *long_list;
1281 struct dn_list *list3;
1284 if (list->count == 0) {
1288 if (list2->count == 0) {
1296 * In both of the below we check for strict and in that
1297 * case do not optimise the intersection of this list,
1298 * we must never return an entry not in this
1299 * list. This allows the index for
1300 * SCOPE_ONELEVEL to be trusted.
1303 /* the indexing code is allowed to return a longer list than
1304 what really matches, as all results are filtered by the
1305 full expression at the end - this shortcut avoids a lot of
1306 work in some cases */
1307 if (list->count < 2 && list2->count > 10 && list2->strict == false) {
1310 if (list2->count < 2 && list->count > 10 && list->strict == false) {
1311 list->count = list2->count;
1312 list->dn = list2->dn;
1313 /* note that list2 may not be the parent of list2->dn,
1314 as list2->dn may be owned by ltdb->idxptr. In that
1315 case we expect this reparent call to fail, which is
1317 talloc_reparent(list2, list, list2->dn);
1321 if (list->count > list2->count) {
1329 list3 = talloc_zero(list, struct dn_list);
1330 if (list3 == NULL) {
1334 list3->dn = talloc_array(list3, struct ldb_val,
1335 MIN(list->count, list2->count));
1342 for (i=0;i<short_list->count;i++) {
1343 /* For the GUID index case, this is a binary search */
1344 if (ldb_kv_dn_list_find_val(
1345 ldb_kv, long_list, &short_list->dn[i]) != -1) {
1346 list3->dn[list3->count] = short_list->dn[i];
1351 list->strict |= list2->strict;
1352 list->dn = talloc_steal(list, list3->dn);
1353 list->count = list3->count;
1364 static bool list_union(struct ldb_context *ldb,
1365 struct ldb_kv_private *ldb_kv,
1366 struct dn_list *list,
1367 struct dn_list *list2)
1369 struct ldb_val *dn3;
1370 unsigned int i = 0, j = 0, k = 0;
1372 if (list2->count == 0) {
1377 if (list->count == 0) {
1379 list->count = list2->count;
1380 list->dn = list2->dn;
1381 /* note that list2 may not be the parent of list2->dn,
1382 as list2->dn may be owned by ltdb->idxptr. In that
1383 case we expect this reparent call to fail, which is
1385 talloc_reparent(list2, list, list2->dn);
1390 * Sort the lists (if not in GUID DN mode) so we can do
1391 * the de-duplication during the merge
1393 * NOTE: This can sort the in-memory index values, as list or
1394 * list2 might not be a copy!
1396 ldb_kv_dn_list_sort(ldb_kv, list);
1397 ldb_kv_dn_list_sort(ldb_kv, list2);
1399 dn3 = talloc_array(list, struct ldb_val, list->count + list2->count);
1405 while (i < list->count || j < list2->count) {
1407 if (i >= list->count) {
1409 } else if (j >= list2->count) {
1412 cmp = ldb_val_equal_exact_ordered(list->dn[i],
1418 dn3[k] = list->dn[i];
1421 } else if (cmp > 0) {
1423 dn3[k] = list2->dn[j];
1427 /* Equal, take list */
1428 dn3[k] = list->dn[i];
1441 static int ldb_kv_index_dn(struct ldb_module *module,
1442 struct ldb_kv_private *ldb_kv,
1443 const struct ldb_parse_tree *tree,
1444 struct dn_list *list);
1447 process an OR list (a union)
1449 static int ldb_kv_index_dn_or(struct ldb_module *module,
1450 struct ldb_kv_private *ldb_kv,
1451 const struct ldb_parse_tree *tree,
1452 struct dn_list *list)
1454 struct ldb_context *ldb;
1457 ldb = ldb_module_get_ctx(module);
1462 for (i=0; i<tree->u.list.num_elements; i++) {
1463 struct dn_list *list2;
1466 list2 = talloc_zero(list, struct dn_list);
1467 if (list2 == NULL) {
1468 return LDB_ERR_OPERATIONS_ERROR;
1471 ret = ldb_kv_index_dn(
1472 module, ldb_kv, tree->u.list.elements[i], list2);
1474 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1480 if (ret != LDB_SUCCESS) {
1486 if (!list_union(ldb, ldb_kv, list, list2)) {
1488 return LDB_ERR_OPERATIONS_ERROR;
1492 if (list->count == 0) {
1493 return LDB_ERR_NO_SUCH_OBJECT;
1501 NOT an index results
1503 static int ldb_kv_index_dn_not(struct ldb_module *module,
1504 struct ldb_kv_private *ldb_kv,
1505 const struct ldb_parse_tree *tree,
1506 struct dn_list *list)
1508 /* the only way to do an indexed not would be if we could
1509 negate the not via another not or if we knew the total
1510 number of database elements so we could know that the
1511 existing expression covered the whole database.
1513 instead, we just give up, and rely on a full index scan
1514 (unless an outer & manages to reduce the list)
1516 return LDB_ERR_OPERATIONS_ERROR;
1520 * These things are unique, so avoid a full scan if this is a search
1521 * by GUID, DN or a unique attribute
1523 static bool ldb_kv_index_unique(struct ldb_context *ldb,
1524 struct ldb_kv_private *ldb_kv,
1527 const struct ldb_schema_attribute *a;
1528 if (ldb_kv->cache->GUID_index_attribute != NULL) {
1529 if (ldb_attr_cmp(attr, ldb_kv->cache->GUID_index_attribute) ==
1534 if (ldb_attr_dn(attr) == 0) {
1538 a = ldb_schema_attribute_by_name(ldb, attr);
1539 if (a->flags & LDB_ATTR_FLAG_UNIQUE_INDEX) {
1546 process an AND expression (intersection)
1548 static int ldb_kv_index_dn_and(struct ldb_module *module,
1549 struct ldb_kv_private *ldb_kv,
1550 const struct ldb_parse_tree *tree,
1551 struct dn_list *list)
1553 struct ldb_context *ldb;
1557 ldb = ldb_module_get_ctx(module);
1562 /* in the first pass we only look for unique simple
1563 equality tests, in the hope of avoiding having to look
1565 for (i=0; i<tree->u.list.num_elements; i++) {
1566 const struct ldb_parse_tree *subtree = tree->u.list.elements[i];
1569 if (subtree->operation != LDB_OP_EQUALITY ||
1570 !ldb_kv_index_unique(
1571 ldb, ldb_kv, subtree->u.equality.attr)) {
1575 ret = ldb_kv_index_dn(module, ldb_kv, subtree, list);
1576 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1578 return LDB_ERR_NO_SUCH_OBJECT;
1580 if (ret == LDB_SUCCESS) {
1581 /* a unique index match means we can
1582 * stop. Note that we don't care if we return
1583 * a few too many objects, due to later
1589 /* now do a full intersection */
1592 for (i=0; i<tree->u.list.num_elements; i++) {
1593 const struct ldb_parse_tree *subtree = tree->u.list.elements[i];
1594 struct dn_list *list2;
1597 list2 = talloc_zero(list, struct dn_list);
1598 if (list2 == NULL) {
1599 return ldb_module_oom(module);
1602 ret = ldb_kv_index_dn(module, ldb_kv, subtree, list2);
1604 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1609 return LDB_ERR_NO_SUCH_OBJECT;
1612 if (ret != LDB_SUCCESS) {
1613 /* this didn't adding anything */
1619 talloc_reparent(list2, list, list->dn);
1620 list->dn = list2->dn;
1621 list->count = list2->count;
1623 } else if (!list_intersect(ldb, ldb_kv, list, list2)) {
1625 return LDB_ERR_OPERATIONS_ERROR;
1628 if (list->count == 0) {
1630 return LDB_ERR_NO_SUCH_OBJECT;
1633 if (list->count < 2) {
1634 /* it isn't worth loading the next part of the tree */
1640 /* none of the attributes were indexed */
1641 return LDB_ERR_OPERATIONS_ERROR;
1647 struct ldb_kv_ordered_index_context {
1648 struct ldb_module *module;
1650 struct dn_list *dn_list;
1653 static int traverse_range_index(struct ldb_kv_private *ldb_kv,
1655 struct ldb_val data,
1659 struct ldb_context *ldb;
1660 struct ldb_kv_ordered_index_context *ctx =
1661 (struct ldb_kv_ordered_index_context *)state;
1662 struct ldb_module *module = ctx->module;
1663 struct ldb_message_element *el = NULL;
1664 struct ldb_message *msg = NULL;
1666 size_t dn_array_size, additional_length;
1669 ldb = ldb_module_get_ctx(module);
1671 msg = ldb_msg_new(module);
1673 ctx->error = ldb_unpack_data_flags(ldb, &data, msg,
1674 LDB_UNPACK_DATA_FLAG_NO_DN);
1676 if (ctx->error != LDB_SUCCESS) {
1681 el = ldb_msg_find_element(msg, LDB_KV_IDX);
1687 version = ldb_msg_find_attr_as_int(msg, LDB_KV_IDXVERSION, 0);
1690 * we avoid copying the strings by stealing the list. We have
1691 * to steal msg onto el->values (which looks odd) because
1692 * the memory is allocated on msg, not on each value.
1694 if (version != LDB_KV_GUID_INDEXING_VERSION) {
1695 /* This is quite likely during the DB startup
1696 on first upgrade to using a GUID index */
1697 ldb_debug_set(ldb_module_get_ctx(module),
1698 LDB_DEBUG_ERROR, __location__
1699 ": Wrong GUID index version %d expected %d",
1700 version, LDB_KV_GUID_INDEXING_VERSION);
1702 ctx->error = LDB_ERR_OPERATIONS_ERROR;
1706 if (el->num_values == 0) {
1708 ctx->error = LDB_ERR_OPERATIONS_ERROR;
1712 if ((el->values[0].length % LDB_KV_GUID_SIZE) != 0
1713 || el->values[0].length == 0) {
1715 ctx->error = LDB_ERR_OPERATIONS_ERROR;
1719 dn_array_size = talloc_array_length(ctx->dn_list->dn);
1721 additional_length = el->values[0].length / LDB_KV_GUID_SIZE;
1723 if (ctx->dn_list->count + additional_length < ctx->dn_list->count) {
1725 ctx->error = LDB_ERR_OPERATIONS_ERROR;
1729 if ((ctx->dn_list->count + additional_length) >= dn_array_size) {
1730 size_t new_array_length;
1732 if (dn_array_size * 2 < dn_array_size) {
1734 ctx->error = LDB_ERR_OPERATIONS_ERROR;
1738 new_array_length = MAX(ctx->dn_list->count + additional_length,
1741 ctx->dn_list->dn = talloc_realloc(ctx->dn_list,
1747 if (ctx->dn_list->dn == NULL) {
1749 ctx->error = LDB_ERR_OPERATIONS_ERROR;
1754 * The actual data is on msg.
1756 talloc_steal(ctx->dn_list->dn, msg);
1757 for (i = 0; i < additional_length; i++) {
1758 ctx->dn_list->dn[i + ctx->dn_list->count].data
1759 = &el->values[0].data[i * LDB_KV_GUID_SIZE];
1760 ctx->dn_list->dn[i + ctx->dn_list->count].length = LDB_KV_GUID_SIZE;
1764 ctx->dn_list->count += additional_length;
1766 talloc_free(msg->elements);
1772 * >= and <= indexing implemented using lexicographically sorted keys
1774 * We only run this in GUID indexing mode and when there is no write
1775 * transaction (only implicit read locks are being held). Otherwise, we would
1776 * have to deal with the in-memory index cache.
1778 * We rely on the implementation of index_format_fn on a schema syntax which
1779 * will can help us to construct keys which can be ordered correctly, and we
1780 * terminate using schema agnostic start and end keys.
1782 * index_format_fn must output values which can be memcmp-able to produce the
1783 * correct ordering as defined by the schema syntax class.
1785 static int ldb_kv_index_dn_ordered(struct ldb_module *module,
1786 struct ldb_kv_private *ldb_kv,
1787 const struct ldb_parse_tree *tree,
1788 struct dn_list *list, bool ascending)
1790 enum key_truncation truncation = KEY_NOT_TRUNCATED;
1791 struct ldb_context *ldb = ldb_module_get_ctx(module);
1793 struct ldb_val ldb_key = { 0 }, ldb_key2 = { 0 };
1794 struct ldb_val start_key, end_key;
1795 struct ldb_dn *key_dn = NULL;
1796 const struct ldb_schema_attribute *a = NULL;
1798 struct ldb_kv_ordered_index_context ctx;
1801 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1803 if (!ldb_kv_is_indexed(module, ldb_kv, tree->u.comparison.attr)) {
1804 return LDB_ERR_OPERATIONS_ERROR;
1807 if (ldb_kv->cache->GUID_index_attribute == NULL) {
1808 return LDB_ERR_OPERATIONS_ERROR;
1811 /* bail out if we're in a transaction, full search instead. */
1812 if (ldb_kv->kv_ops->transaction_active(ldb_kv)) {
1813 return LDB_ERR_OPERATIONS_ERROR;
1816 if (ldb_kv->disallow_dn_filter &&
1817 (ldb_attr_cmp(tree->u.comparison.attr, "dn") == 0)) {
1818 /* in AD mode we do not support "(dn=...)" search filters */
1823 if (tree->u.comparison.attr[0] == '@') {
1824 /* Do not allow a indexed search against an @ */
1830 a = ldb_schema_attribute_by_name(ldb, tree->u.comparison.attr);
1833 * If there's no index format function defined for this attr, then
1834 * the lexicographic order in the database doesn't correspond to the
1835 * attr's ordering, so we can't use the iterate_range op.
1837 if (a->syntax->index_format_fn == NULL) {
1838 return LDB_ERR_OPERATIONS_ERROR;
1841 key_dn = ldb_kv_index_key(ldb, ldb_kv, tree->u.comparison.attr,
1842 &tree->u.comparison.value,
1845 return LDB_ERR_OPERATIONS_ERROR;
1846 } else if (truncation == KEY_TRUNCATED) {
1847 ldb_debug(ldb, LDB_DEBUG_WARNING,
1849 ": ordered index violation: key dn truncated: %s\n",
1850 ldb_dn_get_linearized(key_dn));
1851 return LDB_ERR_OPERATIONS_ERROR;
1853 ldb_key = ldb_kv_key_dn(tmp_ctx, key_dn);
1854 talloc_free(key_dn);
1855 if (ldb_key.data == NULL) {
1856 return LDB_ERR_OPERATIONS_ERROR;
1859 key_dn = ldb_kv_index_key(ldb, ldb_kv, tree->u.comparison.attr,
1860 NULL, NULL, &truncation);
1862 return LDB_ERR_OPERATIONS_ERROR;
1863 } else if (truncation == KEY_TRUNCATED) {
1864 ldb_debug(ldb, LDB_DEBUG_WARNING,
1866 ": ordered index violation: key dn truncated: %s\n",
1867 ldb_dn_get_linearized(key_dn));
1868 return LDB_ERR_OPERATIONS_ERROR;
1871 ldb_key2 = ldb_kv_key_dn(tmp_ctx, key_dn);
1872 talloc_free(key_dn);
1873 if (ldb_key2.data == NULL) {
1874 return LDB_ERR_OPERATIONS_ERROR;
1878 * In order to avoid defining a start and end key for the search, we
1879 * notice that each index key is of the form:
1881 * DN=@INDEX:<ATTRIBUTE>:<VALUE>\0.
1883 * We can simply make our start key DN=@INDEX:<ATTRIBUTE>: and our end
1884 * key DN=@INDEX:<ATTRIBUTE>; to return all index entries for a
1885 * particular attribute.
1887 * Our LMDB backend uses the default memcmp for key comparison.
1890 /* Eliminate NUL byte at the end of the empty key */
1894 /* : becomes ; for pseudo end-key */
1895 ldb_key2.data[ldb_key2.length-1]++;
1896 start_key = ldb_key;
1899 start_key = ldb_key2;
1903 ctx.module = module;
1906 ctx.dn_list->count = 0;
1907 ctx.dn_list->dn = talloc_zero_array(ctx.dn_list, struct ldb_val, 2);
1909 ret = ldb_kv->kv_ops->iterate_range(ldb_kv, start_key, end_key,
1910 traverse_range_index, &ctx);
1912 if (ret != LDB_SUCCESS || ctx.error != LDB_SUCCESS) {
1913 return LDB_ERR_OPERATIONS_ERROR;
1916 TYPESAFE_QSORT(ctx.dn_list->dn, ctx.dn_list->count,
1917 ldb_val_equal_exact_for_qsort);
1919 talloc_free(tmp_ctx);
1924 static int ldb_kv_index_dn_greater(struct ldb_module *module,
1925 struct ldb_kv_private *ldb_kv,
1926 const struct ldb_parse_tree *tree,
1927 struct dn_list *list)
1929 return ldb_kv_index_dn_ordered(module,
1935 static int ldb_kv_index_dn_less(struct ldb_module *module,
1936 struct ldb_kv_private *ldb_kv,
1937 const struct ldb_parse_tree *tree,
1938 struct dn_list *list)
1940 return ldb_kv_index_dn_ordered(module,
1947 return a list of matching objects using a one-level index
1949 static int ldb_kv_index_dn_attr(struct ldb_module *module,
1950 struct ldb_kv_private *ldb_kv,
1953 struct dn_list *list,
1954 enum key_truncation *truncation)
1956 struct ldb_context *ldb;
1961 ldb = ldb_module_get_ctx(module);
1963 /* work out the index key from the parent DN */
1964 val.data = (uint8_t *)((uintptr_t)ldb_dn_get_casefold(dn));
1965 if (val.data == NULL) {
1966 const char *dn_str = ldb_dn_get_linearized(dn);
1967 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1969 ": Failed to get casefold DN "
1972 return LDB_ERR_OPERATIONS_ERROR;
1974 val.length = strlen((char *)val.data);
1975 key = ldb_kv_index_key(ldb, ldb_kv, attr, &val, NULL, truncation);
1978 return LDB_ERR_OPERATIONS_ERROR;
1981 ret = ldb_kv_dn_list_load(module, ldb_kv, key, list);
1983 if (ret != LDB_SUCCESS) {
1987 if (list->count == 0) {
1988 return LDB_ERR_NO_SUCH_OBJECT;
1995 return a list of matching objects using a one-level index
1997 static int ldb_kv_index_dn_one(struct ldb_module *module,
1998 struct ldb_kv_private *ldb_kv,
1999 struct ldb_dn *parent_dn,
2000 struct dn_list *list,
2001 enum key_truncation *truncation)
2004 * Ensure we do not shortcut on intersection for this list.
2005 * We must never be lazy and return an entry not in this
2006 * list. This allows the index for
2007 * SCOPE_ONELEVEL to be trusted.
2010 list->strict = true;
2011 return ldb_kv_index_dn_attr(
2012 module, ldb_kv, LDB_KV_IDXONE, parent_dn, list, truncation);
2016 return a list of matching objects using the DN index
2018 static int ldb_kv_index_dn_base_dn(struct ldb_module *module,
2019 struct ldb_kv_private *ldb_kv,
2020 struct ldb_dn *base_dn,
2021 struct dn_list *dn_list,
2022 enum key_truncation *truncation)
2024 const struct ldb_val *guid_val = NULL;
2025 if (ldb_kv->cache->GUID_index_attribute == NULL) {
2026 dn_list->dn = talloc_array(dn_list, struct ldb_val, 1);
2027 if (dn_list->dn == NULL) {
2028 return ldb_module_oom(module);
2030 dn_list->dn[0].data = discard_const_p(unsigned char,
2031 ldb_dn_get_linearized(base_dn));
2032 if (dn_list->dn[0].data == NULL) {
2033 talloc_free(dn_list->dn);
2034 return ldb_module_oom(module);
2036 dn_list->dn[0].length = strlen((char *)dn_list->dn[0].data);
2042 if (ldb_kv->cache->GUID_index_dn_component != NULL) {
2043 guid_val = ldb_dn_get_extended_component(
2044 base_dn, ldb_kv->cache->GUID_index_dn_component);
2047 if (guid_val != NULL) {
2048 dn_list->dn = talloc_array(dn_list, struct ldb_val, 1);
2049 if (dn_list->dn == NULL) {
2050 return ldb_module_oom(module);
2052 dn_list->dn[0].data = guid_val->data;
2053 dn_list->dn[0].length = guid_val->length;
2059 return ldb_kv_index_dn_attr(
2060 module, ldb_kv, LDB_KV_IDXDN, base_dn, dn_list, truncation);
2064 return a list of dn's that might match a indexed search or
2065 an error. return LDB_ERR_NO_SUCH_OBJECT for no matches, or LDB_SUCCESS for matches
2067 static int ldb_kv_index_dn(struct ldb_module *module,
2068 struct ldb_kv_private *ldb_kv,
2069 const struct ldb_parse_tree *tree,
2070 struct dn_list *list)
2072 int ret = LDB_ERR_OPERATIONS_ERROR;
2074 switch (tree->operation) {
2076 ret = ldb_kv_index_dn_and(module, ldb_kv, tree, list);
2080 ret = ldb_kv_index_dn_or(module, ldb_kv, tree, list);
2084 ret = ldb_kv_index_dn_not(module, ldb_kv, tree, list);
2087 case LDB_OP_EQUALITY:
2088 ret = ldb_kv_index_dn_leaf(module, ldb_kv, tree, list);
2091 case LDB_OP_GREATER:
2092 ret = ldb_kv_index_dn_greater(module, ldb_kv, tree, list);
2096 ret = ldb_kv_index_dn_less(module, ldb_kv, tree, list);
2099 case LDB_OP_SUBSTRING:
2100 case LDB_OP_PRESENT:
2102 case LDB_OP_EXTENDED:
2103 /* we can't index with fancy bitops yet */
2104 ret = LDB_ERR_OPERATIONS_ERROR;
2112 filter a candidate dn_list from an indexed search into a set of results
2113 extracting just the given attributes
2115 static int ldb_kv_index_filter(struct ldb_kv_private *ldb_kv,
2116 const struct dn_list *dn_list,
2117 struct ldb_kv_context *ac,
2118 uint32_t *match_count,
2119 enum key_truncation scope_one_truncation)
2121 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
2122 struct ldb_message *msg;
2123 struct ldb_message *filtered_msg;
2125 unsigned int num_keys = 0;
2126 uint8_t previous_guid_key[LDB_KV_GUID_KEY_SIZE] = {};
2127 struct ldb_val *keys = NULL;
2130 * We have to allocate the key list (rather than just walk the
2131 * caller supplied list) as the callback could change the list
2132 * (by modifying an indexed attribute hosted in the in-memory
2135 keys = talloc_array(ac, struct ldb_val, dn_list->count);
2137 return ldb_module_oom(ac->module);
2140 if (ldb_kv->cache->GUID_index_attribute != NULL) {
2142 * We speculate that the keys will be GUID based and so
2143 * pre-fill in enough space for a GUID (avoiding a pile of
2144 * small allocations)
2146 struct guid_tdb_key {
2147 uint8_t guid_key[LDB_KV_GUID_KEY_SIZE];
2148 } *key_values = NULL;
2150 key_values = talloc_array(keys,
2151 struct guid_tdb_key,
2154 if (key_values == NULL) {
2156 return ldb_module_oom(ac->module);
2158 for (i = 0; i < dn_list->count; i++) {
2159 keys[i].data = key_values[i].guid_key;
2160 keys[i].length = sizeof(key_values[i].guid_key);
2163 for (i = 0; i < dn_list->count; i++) {
2164 keys[i].data = NULL;
2169 for (i = 0; i < dn_list->count; i++) {
2172 ret = ldb_kv_idx_to_key(
2173 ac->module, ldb_kv, keys, &dn_list->dn[i], &keys[num_keys]);
2174 if (ret != LDB_SUCCESS) {
2179 if (ldb_kv->cache->GUID_index_attribute != NULL) {
2181 * If we are in GUID index mode, then the dn_list is
2182 * sorted. If we got a duplicate, forget about it, as
2183 * otherwise we would send the same entry back more
2186 * This is needed in the truncated DN case, or if a
2187 * duplicate was forced in via
2188 * LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK
2191 if (memcmp(previous_guid_key,
2192 keys[num_keys].data,
2193 sizeof(previous_guid_key)) == 0) {
2197 memcpy(previous_guid_key,
2198 keys[num_keys].data,
2199 sizeof(previous_guid_key));
2206 * Now that the list is a safe copy, send the callbacks
2208 for (i = 0; i < num_keys; i++) {
2211 msg = ldb_msg_new(ac);
2214 return LDB_ERR_OPERATIONS_ERROR;
2218 ldb_kv_search_key(ac->module,
2222 LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC |
2224 * The entry point ldb_kv_search_indexed is
2225 * only called from the read-locked
2228 LDB_UNPACK_DATA_FLAG_READ_LOCKED);
2229 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2231 * the record has disappeared? yes, this can
2232 * happen if the entry is deleted by something
2233 * operating in the callback (not another
2234 * process, as we have a read lock)
2240 if (ret != LDB_SUCCESS && ret != LDB_ERR_NO_SUCH_OBJECT) {
2241 /* an internal error */
2244 return LDB_ERR_OPERATIONS_ERROR;
2248 * We trust the index for LDB_SCOPE_ONELEVEL
2249 * unless the index key has been truncated.
2251 * LDB_SCOPE_BASE is not passed in by our only caller.
2253 if (ac->scope == LDB_SCOPE_ONELEVEL &&
2254 ldb_kv->cache->one_level_indexes &&
2255 scope_one_truncation == KEY_NOT_TRUNCATED) {
2256 ret = ldb_match_message(ldb, msg, ac->tree,
2257 ac->scope, &matched);
2259 ret = ldb_match_msg_error(ldb, msg,
2261 ac->scope, &matched);
2264 if (ret != LDB_SUCCESS) {
2274 filtered_msg = ldb_msg_new(ac);
2275 if (filtered_msg == NULL) {
2278 return LDB_ERR_OPERATIONS_ERROR;
2281 filtered_msg->dn = talloc_steal(filtered_msg, msg->dn);
2283 /* filter the attributes that the user wants */
2284 ret = ldb_kv_filter_attrs(ldb, msg, ac->attrs, filtered_msg);
2289 TALLOC_FREE(filtered_msg);
2291 return LDB_ERR_OPERATIONS_ERROR;
2294 ret = ldb_module_send_entry(ac->req, filtered_msg, NULL);
2295 if (ret != LDB_SUCCESS) {
2296 /* Regardless of success or failure, the msg
2297 * is the callbacks responsiblity, and should
2298 * not be talloc_free()'ed */
2299 ac->request_terminated = true;
2314 static void ldb_kv_dn_list_sort(struct ldb_kv_private *ltdb,
2315 struct dn_list *list)
2317 if (list->count < 2) {
2321 /* We know the list is sorted when using the GUID index */
2322 if (ltdb->cache->GUID_index_attribute != NULL) {
2326 TYPESAFE_QSORT(list->dn, list->count,
2327 ldb_val_equal_exact_for_qsort);
2331 search the database with a LDAP-like expression using indexes
2332 returns -1 if an indexed search is not possible, in which
2333 case the caller should call ltdb_search_full()
2335 int ldb_kv_search_indexed(struct ldb_kv_context *ac, uint32_t *match_count)
2337 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
2338 struct ldb_kv_private *ldb_kv = talloc_get_type(
2339 ldb_module_get_private(ac->module), struct ldb_kv_private);
2340 struct dn_list *dn_list;
2342 enum ldb_scope index_scope;
2343 enum key_truncation scope_one_truncation = KEY_NOT_TRUNCATED;
2345 /* see if indexing is enabled */
2346 if (!ldb_kv->cache->attribute_indexes &&
2347 !ldb_kv->cache->one_level_indexes && ac->scope != LDB_SCOPE_BASE) {
2348 /* fallback to a full search */
2349 return LDB_ERR_OPERATIONS_ERROR;
2352 dn_list = talloc_zero(ac, struct dn_list);
2353 if (dn_list == NULL) {
2354 return ldb_module_oom(ac->module);
2358 * For the purposes of selecting the switch arm below, if we
2359 * don't have a one-level index then treat it like a subtree
2362 if (ac->scope == LDB_SCOPE_ONELEVEL &&
2363 !ldb_kv->cache->one_level_indexes) {
2364 index_scope = LDB_SCOPE_SUBTREE;
2366 index_scope = ac->scope;
2369 switch (index_scope) {
2370 case LDB_SCOPE_BASE:
2372 * The only caller will have filtered the operation out
2373 * so we should never get here
2375 return ldb_operr(ldb);
2377 case LDB_SCOPE_ONELEVEL:
2380 * First, load all the one-level child objects (regardless of
2381 * whether they match the search filter or not). The database
2382 * maintains a one-level index, so retrieving this is quick.
2384 ret = ldb_kv_index_dn_one(ac->module,
2388 &scope_one_truncation);
2389 if (ret != LDB_SUCCESS) {
2390 talloc_free(dn_list);
2395 * If we have too many children, running ldb_kv_index_filter()
2396 * over all the child objects can be quite expensive. So next
2397 * we do a separate indexed query using the search filter.
2399 * This should be quick, but it may return objects that are not
2400 * the direct one-level child objects we're interested in.
2402 * We only do this in the GUID index mode, which is
2403 * O(n*log(m)) otherwise the intersection below will
2404 * be too costly at O(n*m).
2406 * We don't set a heuristic for 'too many' but instead
2407 * do it always and rely on the index lookup being
2408 * fast enough in the small case.
2410 if (ldb_kv->cache->GUID_index_attribute != NULL) {
2411 struct dn_list *indexed_search_result
2412 = talloc_zero(ac, struct dn_list);
2413 if (indexed_search_result == NULL) {
2414 talloc_free(dn_list);
2415 return ldb_module_oom(ac->module);
2418 if (!ldb_kv->cache->attribute_indexes) {
2419 talloc_free(indexed_search_result);
2420 talloc_free(dn_list);
2421 return LDB_ERR_OPERATIONS_ERROR;
2425 * Try to do an indexed database search
2427 ret = ldb_kv_index_dn(
2428 ac->module, ldb_kv, ac->tree,
2429 indexed_search_result);
2432 * We can stop if we're sure the object doesn't exist
2434 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2435 talloc_free(indexed_search_result);
2436 talloc_free(dn_list);
2437 return LDB_ERR_NO_SUCH_OBJECT;
2441 * Once we have a successful search result, we
2442 * intersect it with the one-level children (dn_list).
2443 * This should give us exactly the result we're after
2444 * (we still need to run ldb_kv_index_filter() to
2445 * handle potential index truncation cases).
2447 * The indexed search may fail because we don't support
2448 * indexing on that type of search operation, e.g.
2449 * matching against '*'. In which case we fall through
2450 * and run ldb_kv_index_filter() over all the one-level
2451 * children (which is still better than bailing out here
2452 * and falling back to a full DB scan).
2454 if (ret == LDB_SUCCESS) {
2455 if (!list_intersect(ldb,
2458 indexed_search_result)) {
2459 talloc_free(indexed_search_result);
2460 talloc_free(dn_list);
2461 return LDB_ERR_OPERATIONS_ERROR;
2467 case LDB_SCOPE_SUBTREE:
2468 case LDB_SCOPE_DEFAULT:
2469 if (!ldb_kv->cache->attribute_indexes) {
2470 talloc_free(dn_list);
2471 return LDB_ERR_OPERATIONS_ERROR;
2474 * Here we load the index for the tree. We have no
2475 * index for the subtree.
2477 ret = ldb_kv_index_dn(ac->module, ldb_kv, ac->tree, dn_list);
2478 if (ret != LDB_SUCCESS) {
2479 talloc_free(dn_list);
2486 * It is critical that this function do the re-filter even
2487 * on things found by the index as the index can over-match
2488 * in cases of truncation (as well as when it decides it is
2489 * not worth further filtering)
2491 * If this changes, then the index code above would need to
2492 * pass up a flag to say if any index was truncated during
2493 * processing as the truncation here refers only to the
2494 * SCOPE_ONELEVEL index.
2496 ret = ldb_kv_index_filter(
2497 ldb_kv, dn_list, ac, match_count, scope_one_truncation);
2498 talloc_free(dn_list);
2503 * @brief Add a DN in the index list of a given attribute name/value pair
2505 * This function will add the DN in the index list for the index for
2506 * the given attribute name and value.
2508 * @param[in] module A ldb_module structure
2510 * @param[in] dn The string representation of the DN as it
2511 * will be stored in the index entry
2513 * @param[in] el A ldb_message_element array, one of the entry
2514 * referred by the v_idx is the attribute name and
2515 * value pair which will be used to construct the
2518 * @param[in] v_idx The index of element in the el array to use
2520 * @return An ldb error code
2522 static int ldb_kv_index_add1(struct ldb_module *module,
2523 struct ldb_kv_private *ldb_kv,
2524 const struct ldb_message *msg,
2525 struct ldb_message_element *el,
2528 struct ldb_context *ldb;
2529 struct ldb_dn *dn_key;
2531 const struct ldb_schema_attribute *a;
2532 struct dn_list *list;
2534 enum key_truncation truncation = KEY_TRUNCATED;
2537 ldb = ldb_module_get_ctx(module);
2539 list = talloc_zero(module, struct dn_list);
2541 return LDB_ERR_OPERATIONS_ERROR;
2544 dn_key = ldb_kv_index_key(
2545 ldb, ldb_kv, el->name, &el->values[v_idx], &a, &truncation);
2548 return LDB_ERR_OPERATIONS_ERROR;
2551 * Samba only maintains unique indexes on the objectSID and objectGUID
2552 * so if a unique index key exceeds the maximum length there is a
2555 if ((truncation == KEY_TRUNCATED) && (a != NULL &&
2556 (a->flags & LDB_ATTR_FLAG_UNIQUE_INDEX ||
2557 (el->flags & LDB_FLAG_INTERNAL_FORCE_UNIQUE_INDEX)))) {
2559 ldb_asprintf_errstring(
2561 __location__ ": unique index key on %s in %s, "
2562 "exceeds maximum key length of %u (encoded).",
2564 ldb_dn_get_linearized(msg->dn),
2565 ldb_kv->max_key_length);
2567 return LDB_ERR_CONSTRAINT_VIOLATION;
2569 talloc_steal(list, dn_key);
2571 ret = ldb_kv_dn_list_load(module, ldb_kv, dn_key, list);
2572 if (ret != LDB_SUCCESS && ret != LDB_ERR_NO_SUCH_OBJECT) {
2578 * Check for duplicates in the @IDXDN DN -> GUID record
2580 * This is very normal, it just means a duplicate DN creation
2581 * was attempted, so don't set the error string or print scary
2584 if (list->count > 0 &&
2585 ldb_attr_cmp(el->name, LDB_KV_IDXDN) == 0 &&
2586 truncation == KEY_NOT_TRUNCATED) {
2589 return LDB_ERR_CONSTRAINT_VIOLATION;
2591 } else if (list->count > 0
2592 && ldb_attr_cmp(el->name, LDB_KV_IDXDN) == 0) {
2595 * At least one existing entry in the DN->GUID index, which
2596 * arises when the DN indexes have been truncated
2598 * So need to pull the DN's to check if it's really a duplicate
2601 for (i=0; i < list->count; i++) {
2602 uint8_t guid_key[LDB_KV_GUID_KEY_SIZE];
2603 struct ldb_val key = {
2605 .length = sizeof(guid_key)
2607 const int flags = LDB_UNPACK_DATA_FLAG_NO_ATTRS;
2608 struct ldb_message *rec = ldb_msg_new(ldb);
2610 return LDB_ERR_OPERATIONS_ERROR;
2613 ret = ldb_kv_idx_to_key(
2614 module, ldb_kv, ldb, &list->dn[i], &key);
2615 if (ret != LDB_SUCCESS) {
2622 ldb_kv_search_key(module, ldb_kv, key, rec, flags);
2623 if (key.data != guid_key) {
2624 TALLOC_FREE(key.data);
2626 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2628 * the record has disappeared?
2629 * yes, this can happen
2635 if (ret != LDB_SUCCESS) {
2636 /* an internal error */
2639 return LDB_ERR_OPERATIONS_ERROR;
2642 * The DN we are trying to add to the DB and index
2643 * is already here, so we must deny the addition
2645 if (ldb_dn_compare(msg->dn, rec->dn) == 0) {
2648 return LDB_ERR_CONSTRAINT_VIOLATION;
2654 * Check for duplicates in unique indexes
2656 * We don't need to do a loop test like the @IDXDN case
2657 * above as we have a ban on long unique index values
2658 * at the start of this function.
2660 if (list->count > 0 &&
2662 && (a->flags & LDB_ATTR_FLAG_UNIQUE_INDEX ||
2663 (el->flags & LDB_FLAG_INTERNAL_FORCE_UNIQUE_INDEX))))) {
2665 * We do not want to print info about a possibly
2666 * confidential DN that the conflict was with in the
2667 * user-visible error string
2670 if (ldb_kv->cache->GUID_index_attribute == NULL) {
2671 ldb_debug(ldb, LDB_DEBUG_WARNING,
2673 ": unique index violation on %s in %s, "
2674 "conflicts with %*.*s in %s",
2675 el->name, ldb_dn_get_linearized(msg->dn),
2676 (int)list->dn[0].length,
2677 (int)list->dn[0].length,
2679 ldb_dn_get_linearized(dn_key));
2681 /* This can't fail, gives a default at worst */
2682 const struct ldb_schema_attribute *attr =
2683 ldb_schema_attribute_by_name(
2684 ldb, ldb_kv->cache->GUID_index_attribute);
2686 ret = attr->syntax->ldif_write_fn(ldb, list,
2688 if (ret == LDB_SUCCESS) {
2692 ": unique index violation on %s in "
2693 "%s, conflicts with %s %*.*s in %s",
2695 ldb_dn_get_linearized(msg->dn),
2696 ldb_kv->cache->GUID_index_attribute,
2700 ldb_dn_get_linearized(dn_key));
2703 ldb_asprintf_errstring(ldb,
2704 __location__ ": unique index violation "
2707 ldb_dn_get_linearized(msg->dn));
2709 return LDB_ERR_CONSTRAINT_VIOLATION;
2712 /* overallocate the list a bit, to reduce the number of
2713 * realloc trigered copies */
2714 alloc_len = ((list->count+1)+7) & ~7;
2715 list->dn = talloc_realloc(list, list->dn, struct ldb_val, alloc_len);
2716 if (list->dn == NULL) {
2718 return LDB_ERR_OPERATIONS_ERROR;
2721 if (ldb_kv->cache->GUID_index_attribute == NULL) {
2722 const char *dn_str = ldb_dn_get_linearized(msg->dn);
2723 list->dn[list->count].data
2724 = (uint8_t *)talloc_strdup(list->dn, dn_str);
2725 if (list->dn[list->count].data == NULL) {
2727 return LDB_ERR_OPERATIONS_ERROR;
2729 list->dn[list->count].length = strlen(dn_str);
2731 const struct ldb_val *key_val;
2732 struct ldb_val *exact = NULL, *next = NULL;
2733 key_val = ldb_msg_find_ldb_val(
2734 msg, ldb_kv->cache->GUID_index_attribute);
2735 if (key_val == NULL) {
2737 return ldb_module_operr(module);
2740 if (key_val->length != LDB_KV_GUID_SIZE) {
2742 return ldb_module_operr(module);
2745 BINARY_ARRAY_SEARCH_GTE(list->dn, list->count,
2746 *key_val, ldb_val_equal_exact_ordered,
2750 * Give a warning rather than fail, this could be a
2751 * duplicate value in the record allowed by a caller
2752 * forcing in the value with
2753 * LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK
2755 if (exact != NULL && truncation == KEY_NOT_TRUNCATED) {
2756 /* This can't fail, gives a default at worst */
2757 const struct ldb_schema_attribute *attr =
2758 ldb_schema_attribute_by_name(
2759 ldb, ldb_kv->cache->GUID_index_attribute);
2761 ret = attr->syntax->ldif_write_fn(ldb, list,
2763 if (ret == LDB_SUCCESS) {
2767 ": duplicate attribute value in %s "
2769 "duplicate of %s %*.*s in %s",
2770 ldb_dn_get_linearized(msg->dn),
2772 ldb_kv->cache->GUID_index_attribute,
2776 ldb_dn_get_linearized(dn_key));
2781 next = &list->dn[list->count];
2783 memmove(&next[1], next,
2784 sizeof(*next) * (list->count - (next - list->dn)));
2786 *next = ldb_val_dup(list->dn, key_val);
2787 if (next->data == NULL) {
2789 return ldb_module_operr(module);
2794 ret = ldb_kv_dn_list_store(module, dn_key, list);
2802 add index entries for one elements in a message
2804 static int ldb_kv_index_add_el(struct ldb_module *module,
2805 struct ldb_kv_private *ldb_kv,
2806 const struct ldb_message *msg,
2807 struct ldb_message_element *el)
2810 for (i = 0; i < el->num_values; i++) {
2811 int ret = ldb_kv_index_add1(module, ldb_kv, msg, el, i);
2812 if (ret != LDB_SUCCESS) {
2821 add index entries for all elements in a message
2823 static int ldb_kv_index_add_all(struct ldb_module *module,
2824 struct ldb_kv_private *ldb_kv,
2825 const struct ldb_message *msg)
2827 struct ldb_message_element *elements = msg->elements;
2832 if (ldb_dn_is_special(msg->dn)) {
2836 dn_str = ldb_dn_get_linearized(msg->dn);
2837 if (dn_str == NULL) {
2838 return LDB_ERR_OPERATIONS_ERROR;
2841 ret = ldb_kv_write_index_dn_guid(module, msg, 1);
2842 if (ret != LDB_SUCCESS) {
2846 if (!ldb_kv->cache->attribute_indexes) {
2847 /* no indexed fields */
2851 for (i = 0; i < msg->num_elements; i++) {
2852 if (!ldb_kv_is_indexed(module, ldb_kv, elements[i].name)) {
2855 ret = ldb_kv_index_add_el(module, ldb_kv, msg, &elements[i]);
2856 if (ret != LDB_SUCCESS) {
2857 struct ldb_context *ldb = ldb_module_get_ctx(module);
2858 ldb_asprintf_errstring(ldb,
2859 __location__ ": Failed to re-index %s in %s - %s",
2860 elements[i].name, dn_str,
2861 ldb_errstring(ldb));
2871 insert a DN index for a message
2873 static int ldb_kv_modify_index_dn(struct ldb_module *module,
2874 struct ldb_kv_private *ldb_kv,
2875 const struct ldb_message *msg,
2880 struct ldb_message_element el;
2884 val.data = (uint8_t *)((uintptr_t)ldb_dn_get_casefold(dn));
2885 if (val.data == NULL) {
2886 const char *dn_str = ldb_dn_get_linearized(dn);
2887 ldb_asprintf_errstring(ldb_module_get_ctx(module),
2888 __location__ ": Failed to modify %s "
2889 "against %s in %s: failed "
2890 "to get casefold DN",
2892 ldb_kv->cache->GUID_index_attribute,
2894 return LDB_ERR_OPERATIONS_ERROR;
2897 val.length = strlen((char *)val.data);
2903 ret = ldb_kv_index_add1(module, ldb_kv, msg, &el, 0);
2904 } else { /* delete */
2905 ret = ldb_kv_index_del_value(module, ldb_kv, msg, &el, 0);
2908 if (ret != LDB_SUCCESS) {
2909 struct ldb_context *ldb = ldb_module_get_ctx(module);
2910 const char *dn_str = ldb_dn_get_linearized(dn);
2911 ldb_asprintf_errstring(ldb,
2912 __location__ ": Failed to modify %s "
2913 "against %s in %s - %s",
2915 ldb_kv->cache->GUID_index_attribute,
2917 ldb_errstring(ldb));
2924 insert a one level index for a message
2926 static int ldb_kv_index_onelevel(struct ldb_module *module,
2927 const struct ldb_message *msg,
2930 struct ldb_kv_private *ldb_kv = talloc_get_type(
2931 ldb_module_get_private(module), struct ldb_kv_private);
2935 /* We index for ONE Level only if requested */
2936 if (!ldb_kv->cache->one_level_indexes) {
2940 pdn = ldb_dn_get_parent(module, msg->dn);
2942 return LDB_ERR_OPERATIONS_ERROR;
2945 ldb_kv_modify_index_dn(module, ldb_kv, msg, pdn, LDB_KV_IDXONE, add);
2953 insert a one level index for a message
2955 static int ldb_kv_write_index_dn_guid(struct ldb_module *module,
2956 const struct ldb_message *msg,
2960 struct ldb_kv_private *ldb_kv = talloc_get_type(
2961 ldb_module_get_private(module), struct ldb_kv_private);
2963 /* We index for DN only if using a GUID index */
2964 if (ldb_kv->cache->GUID_index_attribute == NULL) {
2968 ret = ldb_kv_modify_index_dn(
2969 module, ldb_kv, msg, msg->dn, LDB_KV_IDXDN, add);
2971 if (ret == LDB_ERR_CONSTRAINT_VIOLATION) {
2972 ldb_asprintf_errstring(ldb_module_get_ctx(module),
2973 "Entry %s already exists",
2974 ldb_dn_get_linearized(msg->dn));
2975 ret = LDB_ERR_ENTRY_ALREADY_EXISTS;
2981 add the index entries for a new element in a record
2982 The caller guarantees that these element values are not yet indexed
2984 int ldb_kv_index_add_element(struct ldb_module *module,
2985 struct ldb_kv_private *ldb_kv,
2986 const struct ldb_message *msg,
2987 struct ldb_message_element *el)
2989 if (ldb_dn_is_special(msg->dn)) {
2992 if (!ldb_kv_is_indexed(module, ldb_kv, el->name)) {
2995 return ldb_kv_index_add_el(module, ldb_kv, msg, el);
2999 add the index entries for a new record
3001 int ldb_kv_index_add_new(struct ldb_module *module,
3002 struct ldb_kv_private *ldb_kv,
3003 const struct ldb_message *msg)
3007 if (ldb_dn_is_special(msg->dn)) {
3011 ret = ldb_kv_index_add_all(module, ldb_kv, msg);
3012 if (ret != LDB_SUCCESS) {
3014 * Because we can't trust the caller to be doing
3015 * transactions properly, clean up any index for this
3016 * entry rather than relying on a transaction
3020 ldb_kv_index_delete(module, msg);
3024 ret = ldb_kv_index_onelevel(module, msg, 1);
3025 if (ret != LDB_SUCCESS) {
3027 * Because we can't trust the caller to be doing
3028 * transactions properly, clean up any index for this
3029 * entry rather than relying on a transaction
3032 ldb_kv_index_delete(module, msg);
3040 delete an index entry for one message element
3042 int ldb_kv_index_del_value(struct ldb_module *module,
3043 struct ldb_kv_private *ldb_kv,
3044 const struct ldb_message *msg,
3045 struct ldb_message_element *el,
3048 struct ldb_context *ldb;
3049 struct ldb_dn *dn_key;
3053 struct dn_list *list;
3054 struct ldb_dn *dn = msg->dn;
3055 enum key_truncation truncation = KEY_NOT_TRUNCATED;
3057 ldb = ldb_module_get_ctx(module);
3059 dn_str = ldb_dn_get_linearized(dn);
3060 if (dn_str == NULL) {
3061 return LDB_ERR_OPERATIONS_ERROR;
3064 if (dn_str[0] == '@') {
3068 dn_key = ldb_kv_index_key(
3069 ldb, ldb_kv, el->name, &el->values[v_idx], NULL, &truncation);
3071 * We ignore key truncation in ltdb_index_add1() so
3072 * match that by ignoring it here as well
3074 * Multiple values are legitimate and accepted
3077 return LDB_ERR_OPERATIONS_ERROR;
3080 list = talloc_zero(dn_key, struct dn_list);
3082 talloc_free(dn_key);
3083 return LDB_ERR_OPERATIONS_ERROR;
3086 ret = ldb_kv_dn_list_load(module, ldb_kv, dn_key, list);
3087 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
3088 /* it wasn't indexed. Did we have an earlier error? If we did then
3090 talloc_free(dn_key);
3094 if (ret != LDB_SUCCESS) {
3095 talloc_free(dn_key);
3100 * Find one of the values matching this message to remove
3102 i = ldb_kv_dn_list_find_msg(ldb_kv, list, msg);
3104 /* nothing to delete */
3105 talloc_free(dn_key);
3109 j = (unsigned int) i;
3110 if (j != list->count - 1) {
3111 memmove(&list->dn[j], &list->dn[j+1], sizeof(list->dn[0])*(list->count - (j+1)));
3114 if (list->count == 0) {
3115 talloc_free(list->dn);
3118 list->dn = talloc_realloc(list, list->dn, struct ldb_val, list->count);
3121 ret = ldb_kv_dn_list_store(module, dn_key, list);
3123 talloc_free(dn_key);
3129 delete the index entries for a element
3130 return -1 on failure
3132 int ldb_kv_index_del_element(struct ldb_module *module,
3133 struct ldb_kv_private *ldb_kv,
3134 const struct ldb_message *msg,
3135 struct ldb_message_element *el)
3141 if (!ldb_kv->cache->attribute_indexes) {
3142 /* no indexed fields */
3146 dn_str = ldb_dn_get_linearized(msg->dn);
3147 if (dn_str == NULL) {
3148 return LDB_ERR_OPERATIONS_ERROR;
3151 if (dn_str[0] == '@') {
3155 if (!ldb_kv_is_indexed(module, ldb_kv, el->name)) {
3158 for (i = 0; i < el->num_values; i++) {
3159 ret = ldb_kv_index_del_value(module, ldb_kv, msg, el, i);
3160 if (ret != LDB_SUCCESS) {
3169 delete the index entries for a record
3170 return -1 on failure
3172 int ldb_kv_index_delete(struct ldb_module *module,
3173 const struct ldb_message *msg)
3175 struct ldb_kv_private *ldb_kv = talloc_get_type(
3176 ldb_module_get_private(module), struct ldb_kv_private);
3180 if (ldb_dn_is_special(msg->dn)) {
3184 ret = ldb_kv_index_onelevel(module, msg, 0);
3185 if (ret != LDB_SUCCESS) {
3189 ret = ldb_kv_write_index_dn_guid(module, msg, 0);
3190 if (ret != LDB_SUCCESS) {
3194 if (!ldb_kv->cache->attribute_indexes) {
3195 /* no indexed fields */
3199 for (i = 0; i < msg->num_elements; i++) {
3200 ret = ldb_kv_index_del_element(
3201 module, ldb_kv, msg, &msg->elements[i]);
3202 if (ret != LDB_SUCCESS) {
3212 traversal function that deletes all @INDEX records in the in-memory
3215 This does not touch the actual DB, that is done at transaction
3216 commit, which in turn greatly reduces DB churn as we will likely
3217 be able to do a direct update into the old record.
3219 static int delete_index(struct ldb_kv_private *ldb_kv,
3221 struct ldb_val data,
3224 struct ldb_module *module = state;
3225 const char *dnstr = "DN=" LDB_KV_INDEX ":";
3226 struct dn_list list;
3231 if (strncmp((char *)key.data, dnstr, strlen(dnstr)) != 0) {
3234 /* we need to put a empty list in the internal tdb for this
3239 /* the offset of 3 is to remove the DN= prefix. */
3240 v.data = key.data + 3;
3241 v.length = strnlen((char *)key.data, key.length) - 3;
3243 dn = ldb_dn_from_ldb_val(ldb_kv, ldb_module_get_ctx(module), &v);
3246 * This does not actually touch the DB quite yet, just
3247 * the in-memory index cache
3249 ret = ldb_kv_dn_list_store(module, dn, &list);
3250 if (ret != LDB_SUCCESS) {
3251 ldb_asprintf_errstring(ldb_module_get_ctx(module),
3252 "Unable to store null index for %s\n",
3253 ldb_dn_get_linearized(dn));
3262 traversal function that adds @INDEX records during a re index TODO wrong comment
3264 static int re_key(struct ldb_kv_private *ldb_kv,
3269 struct ldb_context *ldb;
3270 struct ldb_kv_reindex_context *ctx =
3271 (struct ldb_kv_reindex_context *)state;
3272 struct ldb_module *module = ldb_kv->module;
3273 struct ldb_message *msg;
3275 struct ldb_val key2;
3278 ldb = ldb_module_get_ctx(module);
3280 is_record = ldb_kv_key_is_normal_record(key);
3281 if (is_record == false) {
3285 msg = ldb_msg_new(module);
3290 ret = ldb_unpack_data(ldb, &val, msg);
3292 ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid data for index %s\n",
3293 ldb_dn_get_linearized(msg->dn));
3299 if (msg->dn == NULL) {
3300 ldb_debug(ldb, LDB_DEBUG_ERROR,
3301 "Refusing to re-index as GUID "
3302 "key %*.*s with no DN\n",
3303 (int)key.length, (int)key.length,
3309 /* check if the DN key has changed, perhaps due to the case
3310 insensitivity of an element changing, or a change from DN
3312 key2 = ldb_kv_key_msg(module, msg, msg);
3313 if (key2.data == NULL) {
3314 /* probably a corrupt record ... darn */
3315 ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid DN in re_index: %s",
3316 ldb_dn_get_linearized(msg->dn));
3320 if (key.length != key2.length ||
3321 (memcmp(key.data, key2.data, key.length) != 0)) {
3322 ldb_kv->kv_ops->update_in_iterate(
3323 ldb_kv, key, key2, val, ctx);
3325 talloc_free(key2.data);
3330 if (ctx->count % 10000 == 0) {
3331 ldb_debug(ldb, LDB_DEBUG_WARNING,
3332 "Reindexing: re-keyed %u records so far",
3340 traversal function that adds @INDEX records during a re index
3342 static int re_index(struct ldb_kv_private *ldb_kv,
3347 struct ldb_context *ldb;
3348 struct ldb_kv_reindex_context *ctx =
3349 (struct ldb_kv_reindex_context *)state;
3350 struct ldb_module *module = ldb_kv->module;
3351 struct ldb_message *msg;
3355 ldb = ldb_module_get_ctx(module);
3357 is_record = ldb_kv_key_is_normal_record(key);
3358 if (is_record == false) {
3362 msg = ldb_msg_new(module);
3367 ret = ldb_unpack_data(ldb, &val, msg);
3369 ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid data for index %s\n",
3370 ldb_dn_get_linearized(msg->dn));
3376 if (msg->dn == NULL) {
3377 ldb_debug(ldb, LDB_DEBUG_ERROR,
3378 "Refusing to re-index as GUID "
3379 "key %*.*s with no DN\n",
3380 (int)key.length, (int)key.length,
3386 ret = ldb_kv_index_onelevel(module, msg, 1);
3387 if (ret != LDB_SUCCESS) {
3388 ldb_debug(ldb, LDB_DEBUG_ERROR,
3389 "Adding special ONE LEVEL index failed (%s)!",
3390 ldb_dn_get_linearized(msg->dn));
3395 ret = ldb_kv_index_add_all(module, ldb_kv, msg);
3397 if (ret != LDB_SUCCESS) {
3406 if (ctx->count % 10000 == 0) {
3407 ldb_debug(ldb, LDB_DEBUG_WARNING,
3408 "Reindexing: re-indexed %u records so far",
3415 static int re_pack(struct ldb_kv_private *ldb_kv,
3420 struct ldb_context *ldb;
3421 struct ldb_message *msg;
3422 struct ldb_module *module = ldb_kv->module;
3423 struct ldb_kv_repack_context *ctx =
3424 (struct ldb_kv_repack_context *)state;
3427 ldb = ldb_module_get_ctx(module);
3429 msg = ldb_msg_new(module);
3434 ret = ldb_unpack_data(ldb, &val, msg);
3436 ldb_debug(ldb, LDB_DEBUG_ERROR, "Repack: unpack failed: %s\n",
3437 ldb_dn_get_linearized(msg->dn));
3443 ret = ldb_kv_store(module, msg, TDB_MODIFY);
3444 if (ret != LDB_SUCCESS) {
3445 ldb_debug(ldb, LDB_DEBUG_ERROR, "Repack: store failed: %s\n",
3446 ldb_dn_get_linearized(msg->dn));
3453 * Warn the user that we're repacking the first time we see a normal
3454 * record. This means we never warn if we're repacking a database with
3455 * only @ records. This is because during database initialisation,
3456 * we might repack as initial settings are written out, and we don't
3457 * want to spam the log.
3459 if ((!ctx->normal_record_seen) && (!ldb_dn_is_special(msg->dn))) {
3460 ldb_debug(ldb, LDB_DEBUG_WARNING,
3461 "Repacking database with format %#010x",
3462 ldb_kv->pack_format_version);
3463 ctx->normal_record_seen = true;
3467 if (ctx->count % 10000 == 0) {
3468 ldb_debug(ldb, LDB_DEBUG_WARNING,
3469 "Repack: re-packed %u records so far",
3476 int ldb_kv_repack(struct ldb_module *module)
3478 struct ldb_kv_private *ldb_kv = talloc_get_type(
3479 ldb_module_get_private(module), struct ldb_kv_private);
3480 struct ldb_context *ldb = ldb_module_get_ctx(module);
3481 struct ldb_kv_repack_context ctx;
3485 ctx.error = LDB_SUCCESS;
3486 ctx.normal_record_seen = false;
3488 /* Iterate all database records and repack them in the new format */
3489 ret = ldb_kv->kv_ops->iterate(ldb_kv, re_pack, &ctx);
3491 ldb_debug(ldb, LDB_DEBUG_ERROR, "Repack traverse failed: %s",
3492 ldb_errstring(ldb));
3493 return LDB_ERR_OPERATIONS_ERROR;
3496 if (ctx.error != LDB_SUCCESS) {
3497 ldb_debug(ldb, LDB_DEBUG_ERROR, "Repack failed: %s",
3498 ldb_errstring(ldb));
3506 force a complete reindex of the database
3508 int ldb_kv_reindex(struct ldb_module *module)
3510 struct ldb_kv_private *ldb_kv = talloc_get_type(
3511 ldb_module_get_private(module), struct ldb_kv_private);
3513 struct ldb_kv_reindex_context ctx;
3514 size_t index_cache_size = 0;
3517 * Only triggered after a modification, but make clear we do
3518 * not re-index a read-only DB
3520 if (ldb_kv->read_only) {
3521 return LDB_ERR_UNWILLING_TO_PERFORM;
3524 if (ldb_kv_cache_reload(module) != 0) {
3525 return LDB_ERR_OPERATIONS_ERROR;
3529 * Ensure we read (and so remove) the entries from the real
3530 * DB, no values stored so far are any use as we want to do a
3533 ldb_kv_index_transaction_cancel(module);
3536 * Calculate the size of the index cache that we'll need for
3539 index_cache_size = ldb_kv->kv_ops->get_size(ldb_kv);
3540 if (index_cache_size < DEFAULT_INDEX_CACHE_SIZE) {
3541 index_cache_size = DEFAULT_INDEX_CACHE_SIZE;
3544 ret = ldb_kv_index_transaction_start(module, index_cache_size);
3545 if (ret != LDB_SUCCESS) {
3549 /* first traverse the database deleting any @INDEX records by
3550 * putting NULL entries in the in-memory tdb
3552 ret = ldb_kv->kv_ops->iterate(ldb_kv, delete_index, module);
3554 struct ldb_context *ldb = ldb_module_get_ctx(module);
3555 ldb_asprintf_errstring(ldb, "index deletion traverse failed: %s",
3556 ldb_errstring(ldb));
3557 return LDB_ERR_OPERATIONS_ERROR;
3563 ret = ldb_kv->kv_ops->iterate(ldb_kv, re_key, &ctx);
3565 struct ldb_context *ldb = ldb_module_get_ctx(module);
3566 ldb_asprintf_errstring(ldb, "key correction traverse failed: %s",
3567 ldb_errstring(ldb));
3568 return LDB_ERR_OPERATIONS_ERROR;
3571 if (ctx.error != LDB_SUCCESS) {
3572 struct ldb_context *ldb = ldb_module_get_ctx(module);
3573 ldb_asprintf_errstring(ldb, "reindexing failed: %s", ldb_errstring(ldb));
3580 /* now traverse adding any indexes for normal LDB records */
3581 ret = ldb_kv->kv_ops->iterate(ldb_kv, re_index, &ctx);
3583 struct ldb_context *ldb = ldb_module_get_ctx(module);
3584 ldb_asprintf_errstring(ldb, "reindexing traverse failed: %s",
3585 ldb_errstring(ldb));
3586 return LDB_ERR_OPERATIONS_ERROR;
3589 if (ctx.error != LDB_SUCCESS) {
3590 struct ldb_context *ldb = ldb_module_get_ctx(module);
3591 ldb_asprintf_errstring(ldb, "reindexing failed: %s", ldb_errstring(ldb));
3595 if (ctx.count > 10000) {
3596 ldb_debug(ldb_module_get_ctx(module),
3598 "Reindexing: re_index successful on %s, "
3599 "final index write-out will be in transaction commit",
3600 ldb_kv->kv_ops->name(ldb_kv));