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_HIDDEN (1<<2)
41 /* valid attribute flags */
45 } ltdb_valid_attr_flags[] = {
46 { "CASE_INSENSITIVE", LTDB_FLAG_CASE_INSENSITIVE },
47 { "INTEGER", LTDB_FLAG_INTEGER },
48 { "HIDDEN", LTDB_FLAG_HIDDEN },
55 de-register any special handlers for @ATTRIBUTES
57 static void ltdb_attributes_unload(struct ldb_module *module)
59 struct ldb_context *ldb;
60 void *data = ldb_module_get_private(module);
61 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
62 struct ldb_message *msg;
65 ldb = ldb_module_get_ctx(module);
67 if (ltdb->cache->attributes == NULL) {
68 /* no previously loaded attributes */
72 msg = ltdb->cache->attributes;
73 for (i=0;i<msg->num_elements;i++) {
74 ldb_schema_attribute_remove(ldb, msg->elements[i].name);
77 talloc_free(ltdb->cache->attributes);
78 ltdb->cache->attributes = NULL;
82 add up the attrib flags for a @ATTRIBUTES element
84 static int ltdb_attributes_flags(struct ldb_message_element *el, unsigned *v)
88 for (i=0;i<el->num_values;i++) {
90 for (j=0;ltdb_valid_attr_flags[j].name;j++) {
91 if (strcmp(ltdb_valid_attr_flags[j].name,
92 (char *)el->values[i].data) == 0) {
93 value |= ltdb_valid_attr_flags[j].value;
97 if (ltdb_valid_attr_flags[j].name == NULL) {
106 register any special handlers from @ATTRIBUTES
108 static int ltdb_attributes_load(struct ldb_module *module)
110 struct ldb_context *ldb;
111 void *data = ldb_module_get_private(module);
112 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
113 struct ldb_message *msg = ltdb->cache->attributes;
117 ldb = ldb_module_get_ctx(module);
119 if (ldb->schema.attribute_handler_override) {
120 /* we skip loading the @ATTRIBUTES record when a module is supplying
121 its own attribute handling */
125 dn = ldb_dn_new(module, ldb, LTDB_ATTRIBUTES);
126 if (dn == NULL) goto failed;
128 r = ltdb_search_dn1(module, dn, msg);
130 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
133 if (r == LDB_ERR_NO_SUCH_OBJECT) {
136 /* mapping these flags onto ldap 'syntaxes' isn't strictly correct,
137 but its close enough for now */
138 for (i=0;i<msg->num_elements;i++) {
141 const struct ldb_schema_syntax *s;
143 if (ltdb_attributes_flags(&msg->elements[i], &flags) != 0) {
144 ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid @ATTRIBUTES element for '%s'", msg->elements[i].name);
147 switch (flags & ~LTDB_FLAG_HIDDEN) {
149 syntax = LDB_SYNTAX_OCTET_STRING;
151 case LTDB_FLAG_CASE_INSENSITIVE:
152 syntax = LDB_SYNTAX_DIRECTORY_STRING;
154 case LTDB_FLAG_INTEGER:
155 syntax = LDB_SYNTAX_INTEGER;
158 ldb_debug(ldb, LDB_DEBUG_ERROR,
159 "Invalid flag combination 0x%x for '%s' in @ATTRIBUTES",
160 flags, msg->elements[i].name);
164 s = ldb_standard_syntax_by_name(ldb, syntax);
166 ldb_debug(ldb, LDB_DEBUG_ERROR,
167 "Invalid attribute syntax '%s' for '%s' in @ATTRIBUTES",
168 syntax, msg->elements[i].name);
172 flags |= LDB_ATTR_FLAG_ALLOCATED;
173 if (ldb_schema_attribute_add_with_syntax(ldb, msg->elements[i].name, flags, s) != 0) {
185 initialise the baseinfo record
187 static int ltdb_baseinfo_init(struct ldb_module *module)
189 struct ldb_context *ldb;
190 void *data = ldb_module_get_private(module);
191 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
192 struct ldb_message *msg;
194 /* the initial sequence number must be different from the one
195 set in ltdb_cache_free(). Thanks to Jon for pointing this
197 const char *initial_sequence_number = "1";
199 ldb = ldb_module_get_ctx(module);
201 ltdb->sequence_number = atof(initial_sequence_number);
203 msg = ldb_msg_new(ltdb);
204 msg->dn = ldb_dn_new(msg, ldb, LTDB_BASEINFO);
209 if (ldb_msg_add_string(msg, LTDB_SEQUENCE_NUMBER, initial_sequence_number) != 0) {
213 if (ldb_msg_add_string(msg, LTDB_INDEX_VERSION, "1") != 0) {
217 ret = ltdb_store(module, msg, msg, TDB_INSERT);
226 return LDB_ERR_OPERATIONS_ERROR;
230 free any cache records
232 static void ltdb_cache_free(struct ldb_module *module)
234 void *data = ldb_module_get_private(module);
235 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
237 ltdb->sequence_number = 0;
238 talloc_free(ltdb->cache);
245 int ltdb_cache_reload(struct ldb_module *module)
247 ltdb_attributes_unload(module);
248 ltdb_cache_free(module);
249 return ltdb_cache_load(module);
253 load the cache records
255 int ltdb_cache_load(struct ldb_module *module)
257 struct ldb_context *ldb;
258 void *data = ldb_module_get_private(module);
259 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
260 struct ldb_dn *baseinfo_dn = NULL, *options_dn = NULL;
261 struct ldb_dn *indexlist_dn = NULL;
263 struct ldb_message *baseinfo = NULL, *options = NULL;
266 ldb = ldb_module_get_ctx(module);
268 /* a very fast check to avoid extra database reads */
269 if (ltdb->cache != NULL &&
270 tdb_get_seqnum(ltdb->tdb) == ltdb->tdb_seqnum) {
274 if (ltdb->cache == NULL) {
275 ltdb->cache = talloc_zero(ltdb, struct ltdb_cache);
276 if (ltdb->cache == NULL) goto failed;
277 ltdb->cache->indexlist = talloc_zero(ltdb->cache, struct ldb_message);
278 ltdb->cache->attributes = talloc_zero(ltdb->cache, struct ldb_message);
279 if (ltdb->cache->indexlist == NULL ||
280 ltdb->cache->attributes == NULL) {
285 baseinfo = talloc(ltdb->cache, struct ldb_message);
286 if (baseinfo == NULL) goto failed;
288 baseinfo_dn = ldb_dn_new(module, ldb, LTDB_BASEINFO);
289 if (baseinfo_dn == NULL) goto failed;
291 r= ltdb_search_dn1(module, baseinfo_dn, baseinfo);
292 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
296 /* possibly initialise the baseinfo */
297 if (r == LDB_ERR_NO_SUCH_OBJECT) {
298 if (ltdb_baseinfo_init(module) != LDB_SUCCESS) {
301 if (ltdb_search_dn1(module, baseinfo_dn, baseinfo) != LDB_SUCCESS) {
306 ltdb->tdb_seqnum = tdb_get_seqnum(ltdb->tdb);
308 /* if the current internal sequence number is the same as the one
309 in the database then assume the rest of the cache is OK */
310 seq = ldb_msg_find_attr_as_uint64(baseinfo, LTDB_SEQUENCE_NUMBER, 0);
311 if (seq == ltdb->sequence_number) {
314 ltdb->sequence_number = seq;
316 /* Determine what index format we are in (updated on reindex) */
317 ltdb->index_version = ldb_msg_find_attr_as_uint64(baseinfo, LTDB_INDEX_VERSION, 0);
319 if (ltdb->index_version > 1) {
320 ldb_debug(ldb, LDB_DEBUG_ERROR,
321 "Invalid index version %d on database. This ldb supports only index version 0 and 1",
322 ltdb->index_version);
326 /* Read an interpret database options */
327 options = talloc(ltdb->cache, struct ldb_message);
328 if (options == NULL) goto failed;
330 options_dn = ldb_dn_new(options, ldb, LTDB_OPTIONS);
331 if (options_dn == NULL) goto failed;
333 r= ltdb_search_dn1(module, options_dn, options);
334 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
338 /* set flag for checking base DN on searches */
339 if (r == LDB_SUCCESS) {
340 ltdb->check_base = ldb_msg_find_attr_as_bool(options, LTDB_CHECK_BASE, false);
342 ltdb->check_base = false;
345 talloc_free(ltdb->cache->last_attribute.name);
346 memset(<db->cache->last_attribute, 0, sizeof(ltdb->cache->last_attribute));
348 ltdb_attributes_unload(module);
350 talloc_free(ltdb->cache->indexlist);
352 ltdb->cache->indexlist = talloc_zero(ltdb->cache, struct ldb_message);
353 ltdb->cache->attributes = talloc_zero(ltdb->cache, struct ldb_message);
354 if (ltdb->cache->indexlist == NULL ||
355 ltdb->cache->attributes == NULL) {
359 indexlist_dn = ldb_dn_new(module, ldb, LTDB_INDEXLIST);
360 if (indexlist_dn == NULL) goto failed;
362 r = ltdb_search_dn1(module, indexlist_dn, ltdb->cache->indexlist);
363 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
367 if (ltdb_attributes_load(module) == -1) {
372 talloc_free(options);
373 talloc_free(baseinfo);
374 talloc_free(baseinfo_dn);
375 talloc_free(indexlist_dn);
379 talloc_free(options);
380 talloc_free(baseinfo);
381 talloc_free(baseinfo_dn);
382 talloc_free(indexlist_dn);
388 increase the sequence number to indicate a database change
390 int ltdb_increase_sequence_number(struct ldb_module *module)
392 struct ldb_context *ldb;
393 void *data = ldb_module_get_private(module);
394 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
395 struct ldb_message *msg;
396 struct ldb_message_element el[2];
398 struct ldb_val val_time;
399 time_t t = time(NULL);
403 ldb = ldb_module_get_ctx(module);
405 msg = talloc(ltdb, struct ldb_message);
408 return LDB_ERR_OPERATIONS_ERROR;
411 s = talloc_asprintf(msg, "%llu", ltdb->sequence_number+1);
414 return LDB_ERR_OPERATIONS_ERROR;
417 msg->num_elements = ARRAY_SIZE(el);
419 msg->dn = ldb_dn_new(msg, ldb, LTDB_BASEINFO);
420 if (msg->dn == NULL) {
423 return LDB_ERR_OPERATIONS_ERROR;
425 el[0].name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
426 if (el[0].name == NULL) {
429 return LDB_ERR_OPERATIONS_ERROR;
432 el[0].num_values = 1;
433 el[0].flags = LDB_FLAG_MOD_REPLACE;
434 val.data = (uint8_t *)s;
435 val.length = strlen(s);
437 el[1].name = talloc_strdup(msg, LTDB_MOD_TIMESTAMP);
438 if (el[1].name == NULL) {
441 return LDB_ERR_OPERATIONS_ERROR;
443 el[1].values = &val_time;
444 el[1].num_values = 1;
445 el[1].flags = LDB_FLAG_MOD_REPLACE;
447 s = ldb_timestring(msg, t);
451 return LDB_ERR_OPERATIONS_ERROR;
454 val_time.data = (uint8_t *)s;
455 val_time.length = strlen(s);
457 ret = ltdb_modify_internal(module, msg, msg);
461 if (ret == LDB_SUCCESS) {
462 ltdb->sequence_number += 1;
465 /* updating the tdb_seqnum here avoids us reloading the cache
466 records due to our own modification */
467 ltdb->tdb_seqnum = tdb_get_seqnum(ltdb->tdb);
473 increase the index version number to indicate a database change
475 int ltdb_set_casefold_index(struct ldb_module *module)
477 struct ldb_context *ldb;
478 void *data = ldb_module_get_private(module);
479 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
480 struct ldb_message *msg;
481 struct ldb_message_element *el;
485 ldb = ldb_module_get_ctx(module);
487 msg = ldb_msg_new(ltdb);
489 return LDB_ERR_OPERATIONS_ERROR;
491 msg->dn = ldb_dn_new(msg, ldb, LTDB_BASEINFO);
492 if (msg->dn == NULL) {
494 return LDB_ERR_OPERATIONS_ERROR;
497 if (ldb_msg_add_string(msg, LTDB_INDEX_VERSION, "1") != 0) {
499 return LDB_ERR_OPERATIONS_ERROR;
502 el = ldb_msg_find_element(msg, LTDB_INDEX_VERSION);
505 return LDB_ERR_OPERATIONS_ERROR;
507 el->flags = LDB_FLAG_MOD_REPLACE;
509 ret = ltdb_modify_internal(module, msg, msg);
516 int ltdb_check_at_attributes_values(const struct ldb_val *value)
520 for (i = 0; ltdb_valid_attr_flags[i].name != NULL; i++) {
521 if ((strcmp(ltdb_valid_attr_flags[i].name, (char *)value->data) == 0)) {