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 tdb cache functions
29 * Description: cache special records in a ldb/tdb
31 * Author: Andrew Tridgell
35 #include "ldb_private.h"
37 #define LTDB_FLAG_CASE_INSENSITIVE (1<<0)
38 #define LTDB_FLAG_INTEGER (1<<1)
39 #define LTDB_FLAG_UNIQUE_INDEX (1<<2)
41 /* valid attribute flags */
45 } ldb_kv_valid_attr_flags[] = {
46 { "CASE_INSENSITIVE", LTDB_FLAG_CASE_INSENSITIVE },
47 { "INTEGER", LTDB_FLAG_INTEGER },
49 { "UNIQUE_INDEX", LTDB_FLAG_UNIQUE_INDEX},
55 de-register any special handlers for @ATTRIBUTES
57 static void ldb_kv_attributes_unload(struct ldb_module *module)
59 struct ldb_context *ldb = ldb_module_get_ctx(module);
61 ldb_schema_attribute_remove_flagged(ldb, LDB_ATTR_FLAG_FROM_DB);
66 add up the attrib flags for a @ATTRIBUTES element
68 static int ldb_kv_attributes_flags(struct ldb_message_element *el, unsigned *v)
72 for (i=0;i<el->num_values;i++) {
74 for (j = 0; ldb_kv_valid_attr_flags[j].name; j++) {
75 if (strcmp(ldb_kv_valid_attr_flags[j].name,
76 (char *)el->values[i].data) == 0) {
77 value |= ldb_kv_valid_attr_flags[j].value;
81 if (ldb_kv_valid_attr_flags[j].name == NULL) {
89 static int ldb_schema_attribute_compare(const void *p1, const void *p2)
91 const struct ldb_schema_attribute *sa1 = (const struct ldb_schema_attribute *)p1;
92 const struct ldb_schema_attribute *sa2 = (const struct ldb_schema_attribute *)p2;
93 return ldb_attr_cmp(sa1->name, sa2->name);
97 register any special handlers from @ATTRIBUTES
99 static int ldb_kv_attributes_load(struct ldb_module *module)
101 struct ldb_schema_attribute *attrs;
102 struct ldb_context *ldb;
103 struct ldb_message *attrs_msg = NULL;
106 unsigned int num_loaded_attrs = 0;
109 ldb = ldb_module_get_ctx(module);
111 if (ldb->schema.attribute_handler_override) {
112 /* we skip loading the @ATTRIBUTES record when a module is supplying
113 its own attribute handling */
117 attrs_msg = ldb_msg_new(module);
118 if (attrs_msg == NULL) {
122 dn = ldb_dn_new(module, ldb, LTDB_ATTRIBUTES);
123 if (dn == NULL) goto failed;
125 r = ldb_kv_search_dn1(module,
128 LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
129 LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC |
130 LDB_UNPACK_DATA_FLAG_NO_DN);
132 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
135 if (r == LDB_ERR_NO_SUCH_OBJECT || attrs_msg->num_elements == 0) {
136 TALLOC_FREE(attrs_msg);
140 attrs = talloc_array(attrs_msg,
141 struct ldb_schema_attribute,
142 attrs_msg->num_elements
143 + ldb->schema.num_attributes);
149 ldb->schema.attributes,
150 sizeof(ldb->schema.attributes[0]) * ldb->schema.num_attributes);
152 /* mapping these flags onto ldap 'syntaxes' isn't strictly correct,
153 but its close enough for now */
154 for (i=0;i<attrs_msg->num_elements;i++) {
155 unsigned flags = 0, attr_flags = 0;
157 const struct ldb_schema_syntax *s;
158 const struct ldb_schema_attribute *a =
159 ldb_schema_attribute_by_name(ldb,
160 attrs_msg->elements[i].name);
161 if (a != NULL && a->flags & LDB_ATTR_FLAG_FIXED) {
162 /* Must already be set in the array, and kept */
166 if (ldb_kv_attributes_flags(&attrs_msg->elements[i], &flags) !=
168 ldb_debug(ldb, LDB_DEBUG_ERROR,
169 "Invalid @ATTRIBUTES element for '%s'",
170 attrs_msg->elements[i].name);
174 if (flags & LTDB_FLAG_UNIQUE_INDEX) {
175 attr_flags = LDB_ATTR_FLAG_UNIQUE_INDEX;
177 flags &= ~LTDB_FLAG_UNIQUE_INDEX;
179 /* These are not currently flags, each is exclusive */
180 if (flags == LTDB_FLAG_CASE_INSENSITIVE) {
181 syntax = LDB_SYNTAX_DIRECTORY_STRING;
182 } else if (flags == LTDB_FLAG_INTEGER) {
183 syntax = LDB_SYNTAX_INTEGER;
184 } else if (flags == 0) {
185 syntax = LDB_SYNTAX_OCTET_STRING;
187 ldb_debug(ldb, LDB_DEBUG_ERROR,
188 "Invalid flag combination 0x%x for '%s' "
190 flags, attrs_msg->elements[i].name);
194 s = ldb_standard_syntax_by_name(ldb, syntax);
196 ldb_debug(ldb, LDB_DEBUG_ERROR,
197 "Invalid attribute syntax '%s' for '%s' "
199 syntax, attrs_msg->elements[i].name);
203 attr_flags |= LDB_ATTR_FLAG_ALLOCATED | LDB_ATTR_FLAG_FROM_DB;
205 r = ldb_schema_attribute_fill_with_syntax(ldb,
207 attrs_msg->elements[i].name,
209 &attrs[num_loaded_attrs + ldb->schema.num_attributes]);
216 attrs = talloc_realloc(attrs_msg,
217 attrs, struct ldb_schema_attribute,
218 num_loaded_attrs + ldb->schema.num_attributes);
222 TYPESAFE_QSORT(attrs, num_loaded_attrs + ldb->schema.num_attributes,
223 ldb_schema_attribute_compare);
224 talloc_unlink(ldb, ldb->schema.attributes);
225 ldb->schema.attributes = talloc_steal(ldb, attrs);
226 ldb->schema.num_attributes = num_loaded_attrs + ldb->schema.num_attributes;
227 TALLOC_FREE(attrs_msg);
231 TALLOC_FREE(attrs_msg);
236 register any index records we find for the DB
238 static int ldb_kv_index_load(struct ldb_module *module,
239 struct ldb_kv_private *ldb_kv)
241 struct ldb_context *ldb = ldb_module_get_ctx(module);
242 struct ldb_dn *indexlist_dn;
243 int r, lmdb_subdb_version;
245 if (ldb->schema.index_handler_override) {
247 * we skip loading the @INDEXLIST record when a module is
248 * supplying its own attribute handling
250 ldb_kv->cache->attribute_indexes = true;
251 ldb_kv->cache->one_level_indexes =
252 ldb->schema.one_level_indexes;
253 ldb_kv->cache->GUID_index_attribute =
254 ldb->schema.GUID_index_attribute;
255 ldb_kv->cache->GUID_index_dn_component =
256 ldb->schema.GUID_index_dn_component;
260 talloc_free(ldb_kv->cache->indexlist);
262 ldb_kv->cache->indexlist = ldb_msg_new(ldb_kv->cache);
263 if (ldb_kv->cache->indexlist == NULL) {
266 ldb_kv->cache->one_level_indexes = false;
267 ldb_kv->cache->attribute_indexes = false;
269 indexlist_dn = ldb_dn_new(ldb_kv, ldb, LTDB_INDEXLIST);
270 if (indexlist_dn == NULL) {
274 r = ldb_kv_search_dn1(module,
276 ldb_kv->cache->indexlist,
277 LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
278 LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC |
279 LDB_UNPACK_DATA_FLAG_NO_DN);
280 TALLOC_FREE(indexlist_dn);
282 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
286 if (ldb_msg_find_element(ldb_kv->cache->indexlist, LTDB_IDXONE) !=
288 ldb_kv->cache->one_level_indexes = true;
290 if (ldb_msg_find_element(ldb_kv->cache->indexlist, LTDB_IDXATTR) !=
292 ldb_kv->cache->attribute_indexes = true;
294 ldb_kv->cache->GUID_index_attribute = ldb_msg_find_attr_as_string(
295 ldb_kv->cache->indexlist, LTDB_IDXGUID, NULL);
296 ldb_kv->cache->GUID_index_dn_component = ldb_msg_find_attr_as_string(
297 ldb_kv->cache->indexlist, LTDB_IDX_DN_GUID, NULL);
299 lmdb_subdb_version = ldb_msg_find_attr_as_int(
300 ldb_kv->cache->indexlist, LTDB_IDX_LMDB_SUBDB, 0);
302 if (lmdb_subdb_version != 0) {
303 ldb_set_errstring(ldb,
304 "FATAL: This ldb_mdb database has "
305 "been written in a new verson of LDB "
306 "using a sub-database index that "
307 "is not understood by ldb "
316 initialise the baseinfo record
318 static int ldb_kv_baseinfo_init(struct ldb_module *module)
320 struct ldb_context *ldb;
321 void *data = ldb_module_get_private(module);
322 struct ldb_kv_private *ldb_kv =
323 talloc_get_type(data, struct ldb_kv_private);
324 struct ldb_message *msg;
325 struct ldb_message_element el;
328 /* the initial sequence number must be different from the one
329 set in ltdb_cache_free(). Thanks to Jon for pointing this
331 const char *initial_sequence_number = "1";
333 ldb = ldb_module_get_ctx(module);
335 ldb_kv->sequence_number = atof(initial_sequence_number);
337 msg = ldb_msg_new(ldb_kv);
342 msg->num_elements = 1;
344 msg->dn = ldb_dn_new(msg, ldb, LTDB_BASEINFO);
348 el.name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
355 val.data = (uint8_t *)talloc_strdup(msg, initial_sequence_number);
361 ret = ldb_kv_store(module, msg, TDB_INSERT);
370 return LDB_ERR_OPERATIONS_ERROR;
374 free any cache records
376 static void ldb_kv_cache_free(struct ldb_module *module)
378 void *data = ldb_module_get_private(module);
379 struct ldb_kv_private *ldb_kv =
380 talloc_get_type(data, struct ldb_kv_private);
382 ldb_kv->sequence_number = 0;
383 talloc_free(ldb_kv->cache);
384 ldb_kv->cache = NULL;
390 int ldb_kv_cache_reload(struct ldb_module *module)
392 ldb_kv_attributes_unload(module);
393 ldb_kv_cache_free(module);
394 return ldb_kv_cache_load(module);
398 load the cache records
400 int ldb_kv_cache_load(struct ldb_module *module)
402 struct ldb_context *ldb;
403 void *data = ldb_module_get_private(module);
404 struct ldb_kv_private *ldb_kv =
405 talloc_get_type(data, struct ldb_kv_private);
406 struct ldb_dn *baseinfo_dn = NULL, *options_dn = NULL;
408 struct ldb_message *baseinfo = NULL, *options = NULL;
409 const struct ldb_schema_attribute *a;
410 bool have_write_txn = false;
413 ldb = ldb_module_get_ctx(module);
415 /* a very fast check to avoid extra database reads */
416 if (ldb_kv->cache != NULL && !ldb_kv->kv_ops->has_changed(ldb_kv)) {
420 if (ldb_kv->cache == NULL) {
421 ldb_kv->cache = talloc_zero(ldb_kv, struct ldb_kv_cache);
422 if (ldb_kv->cache == NULL)
426 baseinfo = ldb_msg_new(ldb_kv->cache);
427 if (baseinfo == NULL) goto failed;
429 baseinfo_dn = ldb_dn_new(baseinfo, ldb, LTDB_BASEINFO);
430 if (baseinfo_dn == NULL) goto failed;
432 r = ldb_kv->kv_ops->lock_read(module);
433 if (r != LDB_SUCCESS) {
436 r = ldb_kv_search_dn1(module, baseinfo_dn, baseinfo, 0);
437 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
438 goto failed_and_unlock;
441 /* possibly initialise the baseinfo */
442 if (r == LDB_ERR_NO_SUCH_OBJECT) {
444 /* Give up the read lock, try again with a write lock */
445 r = ldb_kv->kv_ops->unlock_read(module);
446 if (r != LDB_SUCCESS) {
450 if (ldb_kv->kv_ops->begin_write(ldb_kv) != 0) {
454 have_write_txn = true;
456 /* error handling for ltdb_baseinfo_init() is by
457 looking for the record again. */
458 ldb_kv_baseinfo_init(module);
460 if (ldb_kv_search_dn1(module, baseinfo_dn, baseinfo, 0) !=
462 goto failed_and_unlock;
467 /* Ignore the result, and update the sequence number */
468 ldb_kv->kv_ops->has_changed(ldb_kv);
470 /* if the current internal sequence number is the same as the one
471 in the database then assume the rest of the cache is OK */
472 seq = ldb_msg_find_attr_as_uint64(baseinfo, LTDB_SEQUENCE_NUMBER, 0);
473 if (seq == ldb_kv->sequence_number) {
476 ldb_kv->sequence_number = seq;
478 /* Read an interpret database options */
480 options = ldb_msg_new(ldb_kv->cache);
481 if (options == NULL) goto failed_and_unlock;
483 options_dn = ldb_dn_new(options, ldb, LTDB_OPTIONS);
484 if (options_dn == NULL) goto failed_and_unlock;
486 r = ldb_kv_search_dn1(module, options_dn, options, 0);
487 talloc_free(options_dn);
488 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
489 goto failed_and_unlock;
492 /* set flags if they do exist */
493 if (r == LDB_SUCCESS) {
495 ldb_msg_find_attr_as_bool(options, LTDB_CHECK_BASE, false);
496 ldb_kv->disallow_dn_filter = ldb_msg_find_attr_as_bool(
497 options, LTDB_DISALLOW_DN_FILTER, false);
499 ldb_kv->check_base = false;
500 ldb_kv->disallow_dn_filter = false;
504 * ltdb_attributes_unload() calls internally talloc_free() on
505 * any non-fixed elemnts in ldb->schema.attributes.
507 * NOTE WELL: This is per-ldb, not per module, so overwrites
508 * the handlers across all databases when used under Samba's
511 ldb_kv_attributes_unload(module);
513 if (ldb_kv_index_load(module, ldb_kv) == -1) {
514 goto failed_and_unlock;
518 * NOTE WELL: This is per-ldb, not per module, so overwrites
519 * the handlers across all databases when used under Samba's
522 if (ldb_kv_attributes_load(module) == -1) {
523 goto failed_and_unlock;
526 ldb_kv->GUID_index_syntax = NULL;
527 if (ldb_kv->cache->GUID_index_attribute != NULL) {
529 * Now the attributes are loaded, set the guid_index_syntax.
530 * This can't fail, it will return a default at worst
532 a = ldb_schema_attribute_by_name(
533 ldb, ldb_kv->cache->GUID_index_attribute);
534 ldb_kv->GUID_index_syntax = a->syntax;
538 if (have_write_txn) {
539 if (ldb_kv->kv_ops->finish_write(ldb_kv) != 0) {
543 ldb_kv->kv_ops->unlock_read(module);
546 talloc_free(options);
547 talloc_free(baseinfo);
551 if (have_write_txn) {
552 ldb_kv->kv_ops->abort_write(ldb_kv);
554 ldb_kv->kv_ops->unlock_read(module);
558 talloc_free(options);
559 talloc_free(baseinfo);
565 increase the sequence number to indicate a database change
567 int ldb_kv_increase_sequence_number(struct ldb_module *module)
569 struct ldb_context *ldb;
570 void *data = ldb_module_get_private(module);
571 struct ldb_kv_private *ldb_kv =
572 talloc_get_type(data, struct ldb_kv_private);
573 struct ldb_message *msg;
574 struct ldb_message_element el[2];
576 struct ldb_val val_time;
577 time_t t = time(NULL);
581 ldb = ldb_module_get_ctx(module);
583 msg = ldb_msg_new(ldb_kv);
586 return LDB_ERR_OPERATIONS_ERROR;
589 s = talloc_asprintf(msg, "%llu", ldb_kv->sequence_number + 1);
593 return LDB_ERR_OPERATIONS_ERROR;
596 msg->num_elements = ARRAY_SIZE(el);
598 msg->dn = ldb_dn_new(msg, ldb, LTDB_BASEINFO);
599 if (msg->dn == NULL) {
602 return LDB_ERR_OPERATIONS_ERROR;
604 el[0].name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
605 if (el[0].name == NULL) {
608 return LDB_ERR_OPERATIONS_ERROR;
611 el[0].num_values = 1;
612 el[0].flags = LDB_FLAG_MOD_REPLACE;
613 val.data = (uint8_t *)s;
614 val.length = strlen(s);
616 el[1].name = talloc_strdup(msg, LTDB_MOD_TIMESTAMP);
617 if (el[1].name == NULL) {
620 return LDB_ERR_OPERATIONS_ERROR;
622 el[1].values = &val_time;
623 el[1].num_values = 1;
624 el[1].flags = LDB_FLAG_MOD_REPLACE;
626 s = ldb_timestring(msg, t);
629 return LDB_ERR_OPERATIONS_ERROR;
632 val_time.data = (uint8_t *)s;
633 val_time.length = strlen(s);
635 ret = ldb_kv_modify_internal(module, msg, NULL);
639 if (ret == LDB_SUCCESS) {
640 ldb_kv->sequence_number += 1;
643 /* updating the tdb_seqnum here avoids us reloading the cache
644 records due to our own modification */
645 ldb_kv->kv_ops->has_changed(ldb_kv);
650 int ldb_kv_check_at_attributes_values(const struct ldb_val *value)
654 for (i = 0; ldb_kv_valid_attr_flags[i].name != NULL; i++) {
655 if ((strcmp(ldb_kv_valid_attr_flags[i].name,
656 (char *)value->data) == 0)) {