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)
41 #define LTDB_FLAG_OBJECTCLASS (1<<3)
43 /* valid attribute flags */
47 } ltdb_valid_attr_flags[] = {
48 { "CASE_INSENSITIVE", LTDB_FLAG_CASE_INSENSITIVE },
49 { "INTEGER", LTDB_FLAG_INTEGER },
50 { "HIDDEN", LTDB_FLAG_HIDDEN },
57 de-register any special handlers for @ATTRIBUTES
59 static void ltdb_attributes_unload(struct ldb_module *module)
61 struct ltdb_private *ltdb = module->private_data;
62 struct ldb_message *msg;
65 if (ltdb->cache->attributes == NULL) {
66 /* no previously loaded attributes */
70 msg = ltdb->cache->attributes;
71 for (i=0;i<msg->num_elements;i++) {
72 ldb_schema_attribute_remove(module->ldb, msg->elements[i].name);
75 talloc_free(ltdb->cache->attributes);
76 ltdb->cache->attributes = NULL;
80 add up the attrib flags for a @ATTRIBUTES element
82 static int ltdb_attributes_flags(struct ldb_message_element *el, unsigned *v)
86 for (i=0;i<el->num_values;i++) {
88 for (j=0;ltdb_valid_attr_flags[j].name;j++) {
89 if (strcmp(ltdb_valid_attr_flags[j].name,
90 (char *)el->values[i].data) == 0) {
91 value |= ltdb_valid_attr_flags[j].value;
95 if (ltdb_valid_attr_flags[j].name == NULL) {
104 register any special handlers from @ATTRIBUTES
106 static int ltdb_attributes_load(struct ldb_module *module)
108 struct ltdb_private *ltdb = module->private_data;
109 struct ldb_message *msg = ltdb->cache->attributes;
113 dn = ldb_dn_new(module, module->ldb, LTDB_ATTRIBUTES);
114 if (dn == NULL) goto failed;
116 r = ltdb_search_dn1(module, dn, msg);
117 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
122 /* mapping these flags onto ldap 'syntaxes' isn't strictly correct,
123 but its close enough for now */
124 for (i=0;i<msg->num_elements;i++) {
127 const struct ldb_schema_syntax *s;
129 if (ltdb_attributes_flags(&msg->elements[i], &flags) != 0) {
130 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Invalid @ATTRIBUTES element for '%s'\n", msg->elements[i].name);
133 switch (flags & ~LTDB_FLAG_HIDDEN) {
135 syntax = LDB_SYNTAX_OCTET_STRING;
137 case LTDB_FLAG_CASE_INSENSITIVE:
138 syntax = LDB_SYNTAX_DIRECTORY_STRING;
140 case LTDB_FLAG_INTEGER:
141 syntax = LDB_SYNTAX_INTEGER;
144 ldb_debug(module->ldb, LDB_DEBUG_ERROR,
145 "Invalid flag combination 0x%x for '%s' in @ATTRIBUTES\n",
146 flags, msg->elements[i].name);
150 s = ldb_standard_syntax_by_name(module->ldb, syntax);
152 ldb_debug(module->ldb, LDB_DEBUG_ERROR,
153 "Invalid attribute syntax '%s' for '%s' in @ATTRIBUTES\n",
154 syntax, msg->elements[i].name);
158 flags |= LDB_ATTR_FLAG_ALLOCATED;
159 if (ldb_schema_attribute_add_with_syntax(module->ldb, msg->elements[i].name, flags, s) != 0) {
171 register any subclasses from @SUBCLASSES
173 static int ltdb_subclasses_load(struct ldb_module *module)
175 struct ltdb_private *ltdb = module->private_data;
176 struct ldb_message *msg = ltdb->cache->subclasses;
180 dn = ldb_dn_new(module, module->ldb, LTDB_SUBCLASSES);
181 if (dn == NULL) goto failed;
183 r = ltdb_search_dn1(module, dn, msg);
184 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
190 for (i=0;i<msg->num_elements;i++) {
191 struct ldb_message_element *el = &msg->elements[i];
192 for (j=0;j<el->num_values;j++) {
193 if (ldb_subclass_add(module->ldb, el->name,
194 (char *)el->values[j].data) != 0) {
207 de-register any @SUBCLASSES
209 static void ltdb_subclasses_unload(struct ldb_module *module)
211 struct ltdb_private *ltdb = module->private_data;
212 struct ldb_message *msg;
215 if (ltdb->cache->subclasses == NULL) {
216 /* no previously loaded subclasses */
220 msg = ltdb->cache->subclasses;
221 for (i=0;i<msg->num_elements;i++) {
222 ldb_subclass_remove(module->ldb, msg->elements[i].name);
225 talloc_free(ltdb->cache->subclasses);
226 ltdb->cache->subclasses = NULL;
231 initialise the baseinfo record
233 static int ltdb_baseinfo_init(struct ldb_module *module)
235 struct ltdb_private *ltdb = module->private_data;
236 struct ldb_message *msg;
237 struct ldb_message_element el;
240 /* the initial sequence number must be different from the one
241 set in ltdb_cache_free(). Thanks to Jon for pointing this
243 const char *initial_sequence_number = "1";
245 ltdb->sequence_number = atof(initial_sequence_number);
247 msg = talloc(ltdb, struct ldb_message);
252 msg->num_elements = 1;
254 msg->dn = ldb_dn_new(msg, module->ldb, LTDB_BASEINFO);
258 el.name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
265 val.data = (uint8_t *)talloc_strdup(msg, initial_sequence_number);
271 ret = ltdb_store(module, msg, TDB_INSERT);
280 return LDB_ERR_OPERATIONS_ERROR;
284 free any cache records
286 static void ltdb_cache_free(struct ldb_module *module)
288 struct ltdb_private *ltdb = module->private_data;
290 ltdb->sequence_number = 0;
291 talloc_free(ltdb->cache);
298 int ltdb_cache_reload(struct ldb_module *module)
300 ltdb_attributes_unload(module);
301 ltdb_subclasses_unload(module);
302 ltdb_cache_free(module);
303 return ltdb_cache_load(module);
307 load the cache records
309 int ltdb_cache_load(struct ldb_module *module)
311 struct ltdb_private *ltdb = module->private_data;
312 struct ldb_dn *baseinfo_dn = NULL;
313 struct ldb_dn *indexlist_dn = NULL;
315 struct ldb_message *baseinfo = NULL;
318 /* a very fast check to avoid extra database reads */
319 if (ltdb->cache != NULL &&
320 tdb_get_seqnum(ltdb->tdb) == ltdb->tdb_seqnum) {
324 if (ltdb->cache == NULL) {
325 ltdb->cache = talloc_zero(ltdb, struct ltdb_cache);
326 if (ltdb->cache == NULL) goto failed;
327 ltdb->cache->indexlist = talloc_zero(ltdb->cache, struct ldb_message);
328 ltdb->cache->subclasses = talloc_zero(ltdb->cache, struct ldb_message);
329 ltdb->cache->attributes = talloc_zero(ltdb->cache, struct ldb_message);
330 if (ltdb->cache->indexlist == NULL ||
331 ltdb->cache->subclasses == NULL ||
332 ltdb->cache->attributes == NULL) {
337 baseinfo = talloc(ltdb->cache, struct ldb_message);
338 if (baseinfo == NULL) goto failed;
340 baseinfo_dn = ldb_dn_new(module, module->ldb, LTDB_BASEINFO);
341 if (baseinfo_dn == NULL) goto failed;
343 r= ltdb_search_dn1(module, baseinfo_dn, baseinfo);
344 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
348 /* possibly initialise the baseinfo */
350 if (ltdb_baseinfo_init(module) != LDB_SUCCESS) {
353 if (ltdb_search_dn1(module, baseinfo_dn, baseinfo) != LDB_SUCCESS) {
358 ltdb->tdb_seqnum = tdb_get_seqnum(ltdb->tdb);
360 /* if the current internal sequence number is the same as the one
361 in the database then assume the rest of the cache is OK */
362 seq = ldb_msg_find_attr_as_uint64(baseinfo, LTDB_SEQUENCE_NUMBER, 0);
363 if (seq == ltdb->sequence_number) {
366 ltdb->sequence_number = seq;
368 talloc_free(ltdb->cache->last_attribute.name);
369 memset(<db->cache->last_attribute, 0, sizeof(ltdb->cache->last_attribute));
371 ltdb_attributes_unload(module);
372 ltdb_subclasses_unload(module);
374 talloc_free(ltdb->cache->indexlist);
375 talloc_free(ltdb->cache->subclasses);
377 ltdb->cache->indexlist = talloc_zero(ltdb->cache, struct ldb_message);
378 ltdb->cache->subclasses = talloc_zero(ltdb->cache, struct ldb_message);
379 ltdb->cache->attributes = talloc_zero(ltdb->cache, struct ldb_message);
380 if (ltdb->cache->indexlist == NULL ||
381 ltdb->cache->subclasses == NULL ||
382 ltdb->cache->attributes == NULL) {
386 indexlist_dn = ldb_dn_new(module, module->ldb, LTDB_INDEXLIST);
387 if (indexlist_dn == NULL) goto failed;
389 r = ltdb_search_dn1(module, indexlist_dn, ltdb->cache->indexlist);
390 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
394 if (ltdb_attributes_load(module) == -1) {
397 if (ltdb_subclasses_load(module) == -1) {
402 talloc_free(baseinfo);
403 talloc_free(baseinfo_dn);
404 talloc_free(indexlist_dn);
408 talloc_free(baseinfo);
409 talloc_free(baseinfo_dn);
410 talloc_free(indexlist_dn);
416 increase the sequence number to indicate a database change
418 int ltdb_increase_sequence_number(struct ldb_module *module)
420 struct ltdb_private *ltdb = module->private_data;
421 struct ldb_message *msg;
422 struct ldb_message_element el[2];
424 struct ldb_val val_time;
425 time_t t = time(NULL);
429 msg = talloc(ltdb, struct ldb_message);
432 return LDB_ERR_OPERATIONS_ERROR;
435 s = talloc_asprintf(msg, "%llu", ltdb->sequence_number+1);
438 return LDB_ERR_OPERATIONS_ERROR;
441 msg->num_elements = ARRAY_SIZE(el);
443 msg->dn = ldb_dn_new(msg, module->ldb, LTDB_BASEINFO);
444 if (msg->dn == NULL) {
447 return LDB_ERR_OPERATIONS_ERROR;
449 el[0].name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
450 if (el[0].name == NULL) {
453 return LDB_ERR_OPERATIONS_ERROR;
456 el[0].num_values = 1;
457 el[0].flags = LDB_FLAG_MOD_REPLACE;
458 val.data = (uint8_t *)s;
459 val.length = strlen(s);
461 el[1].name = talloc_strdup(msg, LTDB_MOD_TIMESTAMP);
462 if (el[1].name == NULL) {
465 return LDB_ERR_OPERATIONS_ERROR;
467 el[1].values = &val_time;
468 el[1].num_values = 1;
469 el[1].flags = LDB_FLAG_MOD_REPLACE;
471 s = ldb_timestring(msg, t);
473 return LDB_ERR_OPERATIONS_ERROR;
476 val_time.data = (uint8_t *)s;
477 val_time.length = strlen(s);
479 ret = ltdb_modify_internal(module, msg);
483 if (ret == LDB_SUCCESS) {
484 ltdb->sequence_number += 1;
487 /* updating the tdb_seqnum here avoids us reloading the cache
488 records due to our own modification */
489 ltdb->tdb_seqnum = tdb_get_seqnum(ltdb->tdb);
494 int ltdb_check_at_attributes_values(const struct ldb_val *value)
498 for (i = 0; ltdb_valid_attr_flags[i].name != NULL; i++) {
499 if ((strcmp(ltdb_valid_attr_flags[i].name, (char *)value->data) == 0)) {