4 Copyright (C) Andrew Tridgell 2004
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 cache functions
29 * Description: cache special records in a ldb/tdb
31 * Author: Andrew Tridgell
35 #include "ldb_private.h"
37 #define LDB_KV_FLAG_CASE_INSENSITIVE (1<<0)
38 #define LDB_KV_FLAG_INTEGER (1<<1)
39 #define LDB_KV_FLAG_UNIQUE_INDEX (1<<2)
40 #define LDB_KV_FLAG_ORDERED_INTEGER (1<<3)
42 /* valid attribute flags */
46 } ldb_kv_valid_attr_flags[] = {
47 { "CASE_INSENSITIVE", LDB_KV_FLAG_CASE_INSENSITIVE },
48 { "INTEGER", LDB_KV_FLAG_INTEGER },
49 { "ORDERED_INTEGER", LDB_KV_FLAG_ORDERED_INTEGER },
51 { "UNIQUE_INDEX", LDB_KV_FLAG_UNIQUE_INDEX},
57 de-register any special handlers for @ATTRIBUTES
59 static void ldb_kv_attributes_unload(struct ldb_module *module)
61 struct ldb_context *ldb = ldb_module_get_ctx(module);
63 ldb_schema_attribute_remove_flagged(ldb, LDB_ATTR_FLAG_FROM_DB);
68 add up the attrib flags for a @ATTRIBUTES element
70 static int ldb_kv_attributes_flags(struct ldb_message_element *el, unsigned *v)
74 for (i=0;i<el->num_values;i++) {
76 for (j = 0; ldb_kv_valid_attr_flags[j].name; j++) {
77 if (strcmp(ldb_kv_valid_attr_flags[j].name,
78 (char *)el->values[i].data) == 0) {
79 value |= ldb_kv_valid_attr_flags[j].value;
83 if (ldb_kv_valid_attr_flags[j].name == NULL) {
91 static int ldb_schema_attribute_compare(const void *p1, const void *p2)
93 const struct ldb_schema_attribute *sa1 = (const struct ldb_schema_attribute *)p1;
94 const struct ldb_schema_attribute *sa2 = (const struct ldb_schema_attribute *)p2;
95 return ldb_attr_cmp(sa1->name, sa2->name);
99 register any special handlers from @ATTRIBUTES
101 static int ldb_kv_attributes_load(struct ldb_module *module)
103 struct ldb_schema_attribute *attrs;
104 struct ldb_context *ldb;
105 struct ldb_message *attrs_msg = NULL;
108 unsigned int num_loaded_attrs = 0;
111 ldb = ldb_module_get_ctx(module);
113 if (ldb->schema.attribute_handler_override) {
114 /* we skip loading the @ATTRIBUTES record when a module is supplying
115 its own attribute handling */
119 attrs_msg = ldb_msg_new(module);
120 if (attrs_msg == NULL) {
124 dn = ldb_dn_new(module, ldb, LDB_KV_ATTRIBUTES);
125 if (dn == NULL) goto failed;
127 r = ldb_kv_search_dn1(module,
130 LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
131 LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC |
132 LDB_UNPACK_DATA_FLAG_NO_DN);
134 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
137 if (r == LDB_ERR_NO_SUCH_OBJECT || attrs_msg->num_elements == 0) {
138 TALLOC_FREE(attrs_msg);
142 attrs = talloc_array(attrs_msg,
143 struct ldb_schema_attribute,
144 attrs_msg->num_elements
145 + ldb->schema.num_attributes);
151 ldb->schema.attributes,
152 sizeof(ldb->schema.attributes[0]) * ldb->schema.num_attributes);
154 /* mapping these flags onto ldap 'syntaxes' isn't strictly correct,
155 but its close enough for now */
156 for (i=0;i<attrs_msg->num_elements;i++) {
157 unsigned flags = 0, attr_flags = 0;
159 const struct ldb_schema_syntax *s;
160 const struct ldb_schema_attribute *a =
161 ldb_schema_attribute_by_name(ldb,
162 attrs_msg->elements[i].name);
163 if (a != NULL && a->flags & LDB_ATTR_FLAG_FIXED) {
164 /* Must already be set in the array, and kept */
168 if (ldb_kv_attributes_flags(&attrs_msg->elements[i], &flags) !=
170 ldb_debug(ldb, LDB_DEBUG_ERROR,
171 "Invalid @ATTRIBUTES element for '%s'",
172 attrs_msg->elements[i].name);
176 if (flags & LDB_KV_FLAG_UNIQUE_INDEX) {
177 attr_flags = LDB_ATTR_FLAG_UNIQUE_INDEX;
179 flags &= ~LDB_KV_FLAG_UNIQUE_INDEX;
181 /* These are not currently flags, each is exclusive */
182 if (flags == LDB_KV_FLAG_CASE_INSENSITIVE) {
183 syntax = LDB_SYNTAX_DIRECTORY_STRING;
184 } else if (flags == LDB_KV_FLAG_INTEGER) {
185 syntax = LDB_SYNTAX_INTEGER;
186 } else if (flags == LDB_KV_FLAG_ORDERED_INTEGER) {
187 syntax = LDB_SYNTAX_ORDERED_INTEGER;
188 } else if (flags == 0) {
189 syntax = LDB_SYNTAX_OCTET_STRING;
191 ldb_debug(ldb, LDB_DEBUG_ERROR,
192 "Invalid flag combination 0x%x for '%s' "
194 flags, attrs_msg->elements[i].name);
198 s = ldb_standard_syntax_by_name(ldb, syntax);
200 ldb_debug(ldb, LDB_DEBUG_ERROR,
201 "Invalid attribute syntax '%s' for '%s' "
203 syntax, attrs_msg->elements[i].name);
207 attr_flags |= LDB_ATTR_FLAG_ALLOCATED | LDB_ATTR_FLAG_FROM_DB;
209 r = ldb_schema_attribute_fill_with_syntax(ldb,
211 attrs_msg->elements[i].name,
213 &attrs[num_loaded_attrs + ldb->schema.num_attributes]);
220 attrs = talloc_realloc(attrs_msg,
221 attrs, struct ldb_schema_attribute,
222 num_loaded_attrs + ldb->schema.num_attributes);
226 TYPESAFE_QSORT(attrs, num_loaded_attrs + ldb->schema.num_attributes,
227 ldb_schema_attribute_compare);
228 talloc_unlink(ldb, ldb->schema.attributes);
229 ldb->schema.attributes = talloc_steal(ldb, attrs);
230 ldb->schema.num_attributes = num_loaded_attrs + ldb->schema.num_attributes;
231 TALLOC_FREE(attrs_msg);
235 TALLOC_FREE(attrs_msg);
240 register any index records we find for the DB
242 static int ldb_kv_index_load(struct ldb_module *module,
243 struct ldb_kv_private *ldb_kv)
245 struct ldb_context *ldb = ldb_module_get_ctx(module);
246 struct ldb_dn *indexlist_dn;
247 int r, lmdb_subdb_version;
249 if (ldb->schema.index_handler_override) {
251 * we skip loading the @INDEXLIST record when a module is
252 * supplying its own attribute handling
254 ldb_kv->cache->attribute_indexes = true;
255 ldb_kv->cache->one_level_indexes =
256 ldb->schema.one_level_indexes;
257 ldb_kv->cache->GUID_index_attribute =
258 ldb->schema.GUID_index_attribute;
259 ldb_kv->cache->GUID_index_dn_component =
260 ldb->schema.GUID_index_dn_component;
264 talloc_free(ldb_kv->cache->indexlist);
266 ldb_kv->cache->indexlist = ldb_msg_new(ldb_kv->cache);
267 if (ldb_kv->cache->indexlist == NULL) {
270 ldb_kv->cache->one_level_indexes = false;
271 ldb_kv->cache->attribute_indexes = false;
273 indexlist_dn = ldb_dn_new(ldb_kv, ldb, LDB_KV_INDEXLIST);
274 if (indexlist_dn == NULL) {
278 r = ldb_kv_search_dn1(module,
280 ldb_kv->cache->indexlist,
281 LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
282 LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC |
283 LDB_UNPACK_DATA_FLAG_NO_DN);
284 TALLOC_FREE(indexlist_dn);
286 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
290 if (ldb_msg_find_element(ldb_kv->cache->indexlist, LDB_KV_IDXONE) !=
292 ldb_kv->cache->one_level_indexes = true;
294 if (ldb_msg_find_element(ldb_kv->cache->indexlist, LDB_KV_IDXATTR) !=
296 ldb_kv->cache->attribute_indexes = true;
298 ldb_kv->cache->GUID_index_attribute = ldb_msg_find_attr_as_string(
299 ldb_kv->cache->indexlist, LDB_KV_IDXGUID, NULL);
300 ldb_kv->cache->GUID_index_dn_component = ldb_msg_find_attr_as_string(
301 ldb_kv->cache->indexlist, LDB_KV_IDX_DN_GUID, NULL);
303 lmdb_subdb_version = ldb_msg_find_attr_as_int(
304 ldb_kv->cache->indexlist, LDB_KV_IDX_LMDB_SUBDB, 0);
306 if (lmdb_subdb_version != 0) {
307 ldb_set_errstring(ldb,
308 "FATAL: This ldb_mdb database has "
309 "been written in a new version of LDB "
310 "using a sub-database index that "
311 "is not understood by ldb "
320 initialise the baseinfo record
322 static int ldb_kv_baseinfo_init(struct ldb_module *module)
324 struct ldb_context *ldb;
325 void *data = ldb_module_get_private(module);
326 struct ldb_kv_private *ldb_kv =
327 talloc_get_type(data, struct ldb_kv_private);
328 struct ldb_message *msg;
329 struct ldb_message_element el;
332 /* the initial sequence number must be different from the one
333 set in ltdb_cache_free(). Thanks to Jon for pointing this
335 const char *initial_sequence_number = "1";
337 ldb = ldb_module_get_ctx(module);
339 ldb_kv->sequence_number = atof(initial_sequence_number);
341 msg = ldb_msg_new(ldb_kv);
346 msg->num_elements = 1;
348 msg->dn = ldb_dn_new(msg, ldb, LDB_KV_BASEINFO);
352 el.name = talloc_strdup(msg, LDB_KV_SEQUENCE_NUMBER);
359 val.data = (uint8_t *)talloc_strdup(msg, initial_sequence_number);
365 ret = ldb_kv_store(module, msg, TDB_INSERT);
374 return LDB_ERR_OPERATIONS_ERROR;
378 free any cache records
380 static void ldb_kv_cache_free(struct ldb_module *module)
382 void *data = ldb_module_get_private(module);
383 struct ldb_kv_private *ldb_kv =
384 talloc_get_type(data, struct ldb_kv_private);
386 ldb_kv->sequence_number = 0;
387 talloc_free(ldb_kv->cache);
388 ldb_kv->cache = NULL;
394 int ldb_kv_cache_reload(struct ldb_module *module)
396 ldb_kv_attributes_unload(module);
397 ldb_kv_cache_free(module);
398 return ldb_kv_cache_load(module);
402 load the cache records
404 int ldb_kv_cache_load(struct ldb_module *module)
406 struct ldb_context *ldb;
407 void *data = ldb_module_get_private(module);
408 struct ldb_kv_private *ldb_kv =
409 talloc_get_type(data, struct ldb_kv_private);
410 struct ldb_dn *baseinfo_dn = NULL, *options_dn = NULL;
412 struct ldb_message *baseinfo = NULL, *options = NULL;
413 const struct ldb_schema_attribute *a;
414 bool have_write_txn = false;
417 ldb = ldb_module_get_ctx(module);
419 /* a very fast check to avoid extra database reads */
420 if (ldb_kv->cache != NULL && !ldb_kv->kv_ops->has_changed(ldb_kv)) {
424 if (ldb_kv->cache == NULL) {
425 ldb_kv->cache = talloc_zero(ldb_kv, struct ldb_kv_cache);
426 if (ldb_kv->cache == NULL)
430 baseinfo = ldb_msg_new(ldb_kv->cache);
431 if (baseinfo == NULL) goto failed;
433 baseinfo_dn = ldb_dn_new(baseinfo, ldb, LDB_KV_BASEINFO);
434 if (baseinfo_dn == NULL) goto failed;
436 r = ldb_kv->kv_ops->lock_read(module);
437 if (r != LDB_SUCCESS) {
440 r = ldb_kv_search_dn1(module, baseinfo_dn, baseinfo, 0);
441 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
442 goto failed_and_unlock;
445 /* possibly initialise the baseinfo */
446 if (r == LDB_ERR_NO_SUCH_OBJECT) {
448 /* Give up the read lock, try again with a write lock */
449 r = ldb_kv->kv_ops->unlock_read(module);
450 if (r != LDB_SUCCESS) {
454 if (ldb_kv->kv_ops->begin_write(ldb_kv) != 0) {
458 have_write_txn = true;
460 /* error handling for ltdb_baseinfo_init() is by
461 looking for the record again. */
462 ldb_kv_baseinfo_init(module);
464 if (ldb_kv_search_dn1(module, baseinfo_dn, baseinfo, 0) !=
466 goto failed_and_unlock;
471 /* Ignore the result, and update the sequence number */
472 ldb_kv->kv_ops->has_changed(ldb_kv);
474 /* if the current internal sequence number is the same as the one
475 in the database then assume the rest of the cache is OK */
476 seq = ldb_msg_find_attr_as_uint64(baseinfo, LDB_KV_SEQUENCE_NUMBER, 0);
477 if (seq == ldb_kv->sequence_number) {
480 ldb_kv->sequence_number = seq;
482 /* Read an interpret database options */
484 options = ldb_msg_new(ldb_kv->cache);
485 if (options == NULL) goto failed_and_unlock;
487 options_dn = ldb_dn_new(options, ldb, LDB_KV_OPTIONS);
488 if (options_dn == NULL) goto failed_and_unlock;
490 r = ldb_kv_search_dn1(module, options_dn, options, 0);
491 talloc_free(options_dn);
492 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
493 goto failed_and_unlock;
496 /* set flags if they do exist */
497 if (r == LDB_SUCCESS) {
499 ldb_msg_find_attr_as_bool(options, LDB_KV_CHECK_BASE, false);
500 ldb_kv->disallow_dn_filter = ldb_msg_find_attr_as_bool(
501 options, LDB_KV_DISALLOW_DN_FILTER, false);
503 ldb_kv->check_base = false;
504 ldb_kv->disallow_dn_filter = false;
508 * ltdb_attributes_unload() calls internally talloc_free() on
509 * any non-fixed elemnts in ldb->schema.attributes.
511 * NOTE WELL: This is per-ldb, not per module, so overwrites
512 * the handlers across all databases when used under Samba's
515 ldb_kv_attributes_unload(module);
517 if (ldb_kv_index_load(module, ldb_kv) == -1) {
518 goto failed_and_unlock;
522 * NOTE WELL: This is per-ldb, not per module, so overwrites
523 * the handlers across all databases when used under Samba's
526 if (ldb_kv_attributes_load(module) == -1) {
527 goto failed_and_unlock;
530 ldb_kv->GUID_index_syntax = NULL;
531 if (ldb_kv->cache->GUID_index_attribute != NULL) {
533 * Now the attributes are loaded, set the guid_index_syntax.
534 * This can't fail, it will return a default at worst
536 a = ldb_schema_attribute_by_name(
537 ldb, ldb_kv->cache->GUID_index_attribute);
538 ldb_kv->GUID_index_syntax = a->syntax;
542 if (have_write_txn) {
543 if (ldb_kv->kv_ops->finish_write(ldb_kv) != 0) {
547 ldb_kv->kv_ops->unlock_read(module);
550 talloc_free(options);
551 talloc_free(baseinfo);
555 if (have_write_txn) {
556 ldb_kv->kv_ops->abort_write(ldb_kv);
558 ldb_kv->kv_ops->unlock_read(module);
562 talloc_free(options);
563 talloc_free(baseinfo);
569 increase the sequence number to indicate a database change
571 int ldb_kv_increase_sequence_number(struct ldb_module *module)
573 struct ldb_context *ldb;
574 void *data = ldb_module_get_private(module);
575 struct ldb_kv_private *ldb_kv =
576 talloc_get_type(data, struct ldb_kv_private);
577 struct ldb_message *msg;
578 struct ldb_message_element el[2];
580 struct ldb_val val_time;
581 time_t t = time(NULL);
585 ldb = ldb_module_get_ctx(module);
587 msg = ldb_msg_new(ldb_kv);
590 return LDB_ERR_OPERATIONS_ERROR;
593 s = talloc_asprintf(msg, "%llu", ldb_kv->sequence_number + 1);
597 return LDB_ERR_OPERATIONS_ERROR;
600 msg->num_elements = ARRAY_SIZE(el);
602 msg->dn = ldb_dn_new(msg, ldb, LDB_KV_BASEINFO);
603 if (msg->dn == NULL) {
606 return LDB_ERR_OPERATIONS_ERROR;
608 el[0].name = talloc_strdup(msg, LDB_KV_SEQUENCE_NUMBER);
609 if (el[0].name == NULL) {
612 return LDB_ERR_OPERATIONS_ERROR;
615 el[0].num_values = 1;
616 el[0].flags = LDB_FLAG_MOD_REPLACE;
617 val.data = (uint8_t *)s;
618 val.length = strlen(s);
620 el[1].name = talloc_strdup(msg, LDB_KV_MOD_TIMESTAMP);
621 if (el[1].name == NULL) {
624 return LDB_ERR_OPERATIONS_ERROR;
626 el[1].values = &val_time;
627 el[1].num_values = 1;
628 el[1].flags = LDB_FLAG_MOD_REPLACE;
630 s = ldb_timestring(msg, t);
633 return LDB_ERR_OPERATIONS_ERROR;
636 val_time.data = (uint8_t *)s;
637 val_time.length = strlen(s);
639 ret = ldb_kv_modify_internal(module, msg, NULL);
643 if (ret == LDB_SUCCESS) {
644 ldb_kv->sequence_number += 1;
647 /* updating the tdb_seqnum here avoids us reloading the cache
648 records due to our own modification */
649 ldb_kv->kv_ops->has_changed(ldb_kv);
654 int ldb_kv_check_at_attributes_values(const struct ldb_val *value)
658 for (i = 0; ldb_kv_valid_attr_flags[i].name != NULL; i++) {
659 if ((strcmp(ldb_kv_valid_attr_flags[i].name,
660 (char *)value->data) == 0)) {