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
34 #include "ldb_includes.h"
38 #define LTDB_FLAG_CASE_INSENSITIVE (1<<0)
39 #define LTDB_FLAG_INTEGER (1<<1)
40 #define LTDB_FLAG_HIDDEN (1<<2)
42 /* valid attribute flags */
46 } ltdb_valid_attr_flags[] = {
47 { "CASE_INSENSITIVE", LTDB_FLAG_CASE_INSENSITIVE },
48 { "INTEGER", LTDB_FLAG_INTEGER },
49 { "HIDDEN", LTDB_FLAG_HIDDEN },
56 de-register any special handlers for @ATTRIBUTES
58 static void ltdb_attributes_unload(struct ldb_module *module)
60 struct ltdb_private *ltdb = (struct ltdb_private *)module->private_data;
61 struct ldb_message *msg;
64 if (ltdb->cache->attributes == NULL) {
65 /* no previously loaded attributes */
69 msg = ltdb->cache->attributes;
70 for (i=0;i<msg->num_elements;i++) {
71 ldb_schema_attribute_remove(module->ldb, msg->elements[i].name);
74 talloc_free(ltdb->cache->attributes);
75 ltdb->cache->attributes = NULL;
79 add up the attrib flags for a @ATTRIBUTES element
81 static int ltdb_attributes_flags(struct ldb_message_element *el, unsigned *v)
85 for (i=0;i<el->num_values;i++) {
87 for (j=0;ltdb_valid_attr_flags[j].name;j++) {
88 if (strcmp(ltdb_valid_attr_flags[j].name,
89 (char *)el->values[i].data) == 0) {
90 value |= ltdb_valid_attr_flags[j].value;
94 if (ltdb_valid_attr_flags[j].name == NULL) {
103 register any special handlers from @ATTRIBUTES
105 static int ltdb_attributes_load(struct ldb_module *module)
107 struct ltdb_private *ltdb = (struct ltdb_private *)module->private_data;
108 struct ldb_message *msg = ltdb->cache->attributes;
112 dn = ldb_dn_new(module, module->ldb, LTDB_ATTRIBUTES);
113 if (dn == NULL) goto failed;
115 r = ltdb_search_dn1(module, dn, msg);
117 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
120 if (r == LDB_ERR_NO_SUCH_OBJECT) {
123 /* mapping these flags onto ldap 'syntaxes' isn't strictly correct,
124 but its close enough for now */
125 for (i=0;i<msg->num_elements;i++) {
128 const struct ldb_schema_syntax *s;
130 if (ltdb_attributes_flags(&msg->elements[i], &flags) != 0) {
131 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Invalid @ATTRIBUTES element for '%s'\n", msg->elements[i].name);
134 switch (flags & ~LTDB_FLAG_HIDDEN) {
136 syntax = LDB_SYNTAX_OCTET_STRING;
138 case LTDB_FLAG_CASE_INSENSITIVE:
139 syntax = LDB_SYNTAX_DIRECTORY_STRING;
141 case LTDB_FLAG_INTEGER:
142 syntax = LDB_SYNTAX_INTEGER;
145 ldb_debug(module->ldb, LDB_DEBUG_ERROR,
146 "Invalid flag combination 0x%x for '%s' in @ATTRIBUTES\n",
147 flags, msg->elements[i].name);
151 s = ldb_standard_syntax_by_name(module->ldb, syntax);
153 ldb_debug(module->ldb, LDB_DEBUG_ERROR,
154 "Invalid attribute syntax '%s' for '%s' in @ATTRIBUTES\n",
155 syntax, msg->elements[i].name);
159 flags |= LDB_ATTR_FLAG_ALLOCATED;
160 if (ldb_schema_attribute_add_with_syntax(module->ldb, msg->elements[i].name, flags, s) != 0) {
172 initialise the baseinfo record
174 static int ltdb_baseinfo_init(struct ldb_module *module)
176 struct ltdb_private *ltdb = (struct ltdb_private *)module->private_data;
177 struct ldb_message *msg;
178 struct ldb_message_element el;
181 /* the initial sequence number must be different from the one
182 set in ltdb_cache_free(). Thanks to Jon for pointing this
184 const char *initial_sequence_number = "1";
186 ltdb->sequence_number = atof(initial_sequence_number);
188 msg = talloc(ltdb, struct ldb_message);
193 msg->num_elements = 1;
195 msg->dn = ldb_dn_new(msg, module->ldb, LTDB_BASEINFO);
199 el.name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
206 val.data = (uint8_t *)talloc_strdup(msg, initial_sequence_number);
212 ret = ltdb_store(module, msg, TDB_INSERT);
221 return LDB_ERR_OPERATIONS_ERROR;
225 free any cache records
227 static void ltdb_cache_free(struct ldb_module *module)
229 struct ltdb_private *ltdb = (struct ltdb_private *)module->private_data;
231 ltdb->sequence_number = 0;
232 talloc_free(ltdb->cache);
239 int ltdb_cache_reload(struct ldb_module *module)
241 ltdb_attributes_unload(module);
242 ltdb_cache_free(module);
243 return ltdb_cache_load(module);
247 load the cache records
249 int ltdb_cache_load(struct ldb_module *module)
251 struct ltdb_private *ltdb = (struct ltdb_private *)module->private_data;
252 struct ldb_dn *baseinfo_dn = NULL, *options_dn = NULL;
253 struct ldb_dn *indexlist_dn = NULL;
255 struct ldb_message *baseinfo = NULL, *options = NULL;
258 /* a very fast check to avoid extra database reads */
259 if (ltdb->cache != NULL &&
260 tdb_get_seqnum(ltdb->tdb) == ltdb->tdb_seqnum) {
264 if (ltdb->cache == NULL) {
265 ltdb->cache = talloc_zero(ltdb, struct ltdb_cache);
266 if (ltdb->cache == NULL) goto failed;
267 ltdb->cache->indexlist = talloc_zero(ltdb->cache, struct ldb_message);
268 ltdb->cache->attributes = talloc_zero(ltdb->cache, struct ldb_message);
269 if (ltdb->cache->indexlist == NULL ||
270 ltdb->cache->attributes == NULL) {
275 baseinfo = talloc(ltdb->cache, struct ldb_message);
276 if (baseinfo == NULL) goto failed;
278 baseinfo_dn = ldb_dn_new(module, module->ldb, LTDB_BASEINFO);
279 if (baseinfo_dn == NULL) goto failed;
281 r= ltdb_search_dn1(module, baseinfo_dn, baseinfo);
282 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
286 /* possibly initialise the baseinfo */
287 if (r == LDB_ERR_NO_SUCH_OBJECT) {
288 if (ltdb_baseinfo_init(module) != LDB_SUCCESS) {
291 if (ltdb_search_dn1(module, baseinfo_dn, baseinfo) != LDB_SUCCESS) {
296 ltdb->tdb_seqnum = tdb_get_seqnum(ltdb->tdb);
298 /* if the current internal sequence number is the same as the one
299 in the database then assume the rest of the cache is OK */
300 seq = ldb_msg_find_attr_as_uint64(baseinfo, LTDB_SEQUENCE_NUMBER, 0);
301 if (seq == ltdb->sequence_number) {
304 ltdb->sequence_number = seq;
306 /* Read an interpret database options */
307 options = talloc(ltdb->cache, struct ldb_message);
308 if (options == NULL) goto failed;
310 options_dn = ldb_dn_new(options, module->ldb, LTDB_OPTIONS);
311 if (options_dn == NULL) goto failed;
313 r= ltdb_search_dn1(module, options_dn, options);
314 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
318 /* set flag for checking base DN on searches */
319 if (r == LDB_SUCCESS) {
320 ltdb->check_base = ldb_msg_find_attr_as_bool(options, LTDB_CHECK_BASE, false);
322 ltdb->check_base = false;
325 talloc_free(ltdb->cache->last_attribute.name);
326 memset(<db->cache->last_attribute, 0, sizeof(ltdb->cache->last_attribute));
328 ltdb_attributes_unload(module);
330 talloc_free(ltdb->cache->indexlist);
332 ltdb->cache->indexlist = talloc_zero(ltdb->cache, struct ldb_message);
333 ltdb->cache->attributes = talloc_zero(ltdb->cache, struct ldb_message);
334 if (ltdb->cache->indexlist == NULL ||
335 ltdb->cache->attributes == NULL) {
339 indexlist_dn = ldb_dn_new(module, module->ldb, LTDB_INDEXLIST);
340 if (indexlist_dn == NULL) goto failed;
342 r = ltdb_search_dn1(module, indexlist_dn, ltdb->cache->indexlist);
343 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
347 if (ltdb_attributes_load(module) == -1) {
352 talloc_free(options);
353 talloc_free(baseinfo);
354 talloc_free(baseinfo_dn);
355 talloc_free(indexlist_dn);
359 talloc_free(options);
360 talloc_free(baseinfo);
361 talloc_free(baseinfo_dn);
362 talloc_free(indexlist_dn);
368 increase the sequence number to indicate a database change
370 int ltdb_increase_sequence_number(struct ldb_module *module)
372 struct ltdb_private *ltdb = (struct ltdb_private *)module->private_data;
373 struct ldb_message *msg;
374 struct ldb_message_element el[2];
376 struct ldb_val val_time;
377 time_t t = time(NULL);
381 msg = talloc(ltdb, struct ldb_message);
384 return LDB_ERR_OPERATIONS_ERROR;
387 s = talloc_asprintf(msg, "%llu", ltdb->sequence_number+1);
390 return LDB_ERR_OPERATIONS_ERROR;
393 msg->num_elements = ARRAY_SIZE(el);
395 msg->dn = ldb_dn_new(msg, module->ldb, LTDB_BASEINFO);
396 if (msg->dn == NULL) {
399 return LDB_ERR_OPERATIONS_ERROR;
401 el[0].name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
402 if (el[0].name == NULL) {
405 return LDB_ERR_OPERATIONS_ERROR;
408 el[0].num_values = 1;
409 el[0].flags = LDB_FLAG_MOD_REPLACE;
410 val.data = (uint8_t *)s;
411 val.length = strlen(s);
413 el[1].name = talloc_strdup(msg, LTDB_MOD_TIMESTAMP);
414 if (el[1].name == NULL) {
417 return LDB_ERR_OPERATIONS_ERROR;
419 el[1].values = &val_time;
420 el[1].num_values = 1;
421 el[1].flags = LDB_FLAG_MOD_REPLACE;
423 s = ldb_timestring(msg, t);
425 return LDB_ERR_OPERATIONS_ERROR;
428 val_time.data = (uint8_t *)s;
429 val_time.length = strlen(s);
431 ret = ltdb_modify_internal(module, msg);
435 if (ret == LDB_SUCCESS) {
436 ltdb->sequence_number += 1;
439 /* updating the tdb_seqnum here avoids us reloading the cache
440 records due to our own modification */
441 ltdb->tdb_seqnum = tdb_get_seqnum(ltdb->tdb);
446 int ltdb_check_at_attributes_values(const struct ldb_val *value)
450 for (i = 0; ltdb_valid_attr_flags[i].name != NULL; i++) {
451 if ((strcmp(ltdb_valid_attr_flags[i].name, (char *)value->data) == 0)) {