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 2 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, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * Component: ldb tdb cache functions
30 * Description: cache special records in a ldb/tdb
32 * Author: Andrew Tridgell
35 #include "ldb_includes.h"
39 #define LTDB_FLAG_CASE_INSENSITIVE (1<<0)
40 #define LTDB_FLAG_INTEGER (1<<1)
41 #define LTDB_FLAG_HIDDEN (1<<2)
42 #define LTDB_FLAG_OBJECTCLASS (1<<3)
44 /* valid attribute flags */
48 } ltdb_valid_attr_flags[] = {
49 { "CASE_INSENSITIVE", LTDB_FLAG_CASE_INSENSITIVE },
50 { "INTEGER", LTDB_FLAG_INTEGER },
51 { "HIDDEN", LTDB_FLAG_HIDDEN },
58 de-register any special handlers for @ATTRIBUTES
60 static void ltdb_attributes_unload(struct ldb_module *module)
62 struct ltdb_private *ltdb = module->private_data;
63 struct ldb_message *msg;
66 if (ltdb->cache->attributes == NULL) {
67 /* no previously loaded attributes */
71 msg = ltdb->cache->attributes;
72 for (i=0;i<msg->num_elements;i++) {
73 ldb_schema_attribute_remove(module->ldb, msg->elements[i].name);
76 talloc_free(ltdb->cache->attributes);
77 ltdb->cache->attributes = NULL;
81 add up the attrib flags for a @ATTRIBUTES element
83 static int ltdb_attributes_flags(struct ldb_message_element *el, unsigned *v)
87 for (i=0;i<el->num_values;i++) {
89 for (j=0;ltdb_valid_attr_flags[j].name;j++) {
90 if (strcmp(ltdb_valid_attr_flags[j].name,
91 (char *)el->values[i].data) == 0) {
92 value |= ltdb_valid_attr_flags[j].value;
96 if (ltdb_valid_attr_flags[j].name == NULL) {
105 register any special handlers from @ATTRIBUTES
107 static int ltdb_attributes_load(struct ldb_module *module)
109 struct ltdb_private *ltdb = module->private_data;
110 struct ldb_message *msg = ltdb->cache->attributes;
114 dn = ldb_dn_new(module, module->ldb, LTDB_ATTRIBUTES);
115 if (dn == NULL) goto failed;
117 r = ltdb_search_dn1(module, dn, msg);
118 if (r != LDB_SUCCESS && 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 register any subclasses from @SUBCLASSES
174 static int ltdb_subclasses_load(struct ldb_module *module)
176 struct ltdb_private *ltdb = module->private_data;
177 struct ldb_message *msg = ltdb->cache->subclasses;
181 dn = ldb_dn_new(module, module->ldb, LTDB_SUBCLASSES);
182 if (dn == NULL) goto failed;
184 r = ltdb_search_dn1(module, dn, msg);
185 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
191 for (i=0;i<msg->num_elements;i++) {
192 struct ldb_message_element *el = &msg->elements[i];
193 for (j=0;j<el->num_values;j++) {
194 if (ldb_subclass_add(module->ldb, el->name,
195 (char *)el->values[j].data) != 0) {
208 de-register any @SUBCLASSES
210 static void ltdb_subclasses_unload(struct ldb_module *module)
212 struct ltdb_private *ltdb = module->private_data;
213 struct ldb_message *msg;
216 if (ltdb->cache->subclasses == NULL) {
217 /* no previously loaded subclasses */
221 msg = ltdb->cache->subclasses;
222 for (i=0;i<msg->num_elements;i++) {
223 ldb_subclass_remove(module->ldb, msg->elements[i].name);
226 talloc_free(ltdb->cache->subclasses);
227 ltdb->cache->subclasses = NULL;
232 initialise the baseinfo record
234 static int ltdb_baseinfo_init(struct ldb_module *module)
236 struct ltdb_private *ltdb = module->private_data;
237 struct ldb_message *msg;
238 struct ldb_message_element el;
241 /* the initial sequence number must be different from the one
242 set in ltdb_cache_free(). Thanks to Jon for pointing this
244 const char *initial_sequence_number = "1";
246 ltdb->sequence_number = atof(initial_sequence_number);
248 msg = talloc(ltdb, struct ldb_message);
253 msg->num_elements = 1;
255 msg->dn = ldb_dn_new(msg, module->ldb, LTDB_BASEINFO);
259 el.name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
266 val.data = (uint8_t *)talloc_strdup(msg, initial_sequence_number);
272 ret = ltdb_store(module, msg, TDB_INSERT);
281 return LDB_ERR_OPERATIONS_ERROR;
285 free any cache records
287 static void ltdb_cache_free(struct ldb_module *module)
289 struct ltdb_private *ltdb = module->private_data;
291 ltdb->sequence_number = 0;
292 talloc_free(ltdb->cache);
299 int ltdb_cache_reload(struct ldb_module *module)
301 ltdb_attributes_unload(module);
302 ltdb_subclasses_unload(module);
303 ltdb_cache_free(module);
304 return ltdb_cache_load(module);
308 load the cache records
310 int ltdb_cache_load(struct ldb_module *module)
312 struct ltdb_private *ltdb = module->private_data;
313 struct ldb_dn *baseinfo_dn = NULL;
314 struct ldb_dn *indexlist_dn = NULL;
316 struct ldb_message *baseinfo = NULL;
319 /* a very fast check to avoid extra database reads */
320 if (ltdb->cache != NULL &&
321 tdb_get_seqnum(ltdb->tdb) == ltdb->tdb_seqnum) {
325 if (ltdb->cache == NULL) {
326 ltdb->cache = talloc_zero(ltdb, struct ltdb_cache);
327 if (ltdb->cache == NULL) goto failed;
328 ltdb->cache->indexlist = talloc_zero(ltdb->cache, struct ldb_message);
329 ltdb->cache->subclasses = talloc_zero(ltdb->cache, struct ldb_message);
330 ltdb->cache->attributes = talloc_zero(ltdb->cache, struct ldb_message);
331 if (ltdb->cache->indexlist == NULL ||
332 ltdb->cache->subclasses == NULL ||
333 ltdb->cache->attributes == NULL) {
338 baseinfo = talloc(ltdb->cache, struct ldb_message);
339 if (baseinfo == NULL) goto failed;
341 baseinfo_dn = ldb_dn_new(module, module->ldb, LTDB_BASEINFO);
342 if (baseinfo_dn == NULL) goto failed;
344 r= ltdb_search_dn1(module, baseinfo_dn, baseinfo);
345 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
349 /* possibly initialise the baseinfo */
351 if (ltdb_baseinfo_init(module) != LDB_SUCCESS) {
354 if (ltdb_search_dn1(module, baseinfo_dn, baseinfo) != LDB_SUCCESS) {
359 ltdb->tdb_seqnum = tdb_get_seqnum(ltdb->tdb);
361 /* if the current internal sequence number is the same as the one
362 in the database then assume the rest of the cache is OK */
363 seq = ldb_msg_find_attr_as_uint64(baseinfo, LTDB_SEQUENCE_NUMBER, 0);
364 if (seq == ltdb->sequence_number) {
367 ltdb->sequence_number = seq;
369 talloc_free(ltdb->cache->last_attribute.name);
370 memset(<db->cache->last_attribute, 0, sizeof(ltdb->cache->last_attribute));
372 ltdb_attributes_unload(module);
373 ltdb_subclasses_unload(module);
375 talloc_free(ltdb->cache->indexlist);
376 talloc_free(ltdb->cache->subclasses);
378 ltdb->cache->indexlist = talloc_zero(ltdb->cache, struct ldb_message);
379 ltdb->cache->subclasses = talloc_zero(ltdb->cache, struct ldb_message);
380 ltdb->cache->attributes = talloc_zero(ltdb->cache, struct ldb_message);
381 if (ltdb->cache->indexlist == NULL ||
382 ltdb->cache->subclasses == NULL ||
383 ltdb->cache->attributes == NULL) {
387 indexlist_dn = ldb_dn_new(module, module->ldb, LTDB_INDEXLIST);
388 if (indexlist_dn == NULL) goto failed;
390 r = ltdb_search_dn1(module, indexlist_dn, ltdb->cache->indexlist);
391 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
395 if (ltdb_attributes_load(module) == -1) {
398 if (ltdb_subclasses_load(module) == -1) {
403 talloc_free(baseinfo);
404 talloc_free(baseinfo_dn);
405 talloc_free(indexlist_dn);
409 talloc_free(baseinfo);
410 talloc_free(baseinfo_dn);
411 talloc_free(indexlist_dn);
417 increase the sequence number to indicate a database change
419 int ltdb_increase_sequence_number(struct ldb_module *module)
421 struct ltdb_private *ltdb = module->private_data;
422 struct ldb_message *msg;
423 struct ldb_message_element el[2];
425 struct ldb_val val_time;
426 time_t t = time(NULL);
430 msg = talloc(ltdb, struct ldb_message);
433 return LDB_ERR_OPERATIONS_ERROR;
436 s = talloc_asprintf(msg, "%llu", ltdb->sequence_number+1);
439 return LDB_ERR_OPERATIONS_ERROR;
442 msg->num_elements = ARRAY_SIZE(el);
444 msg->dn = ldb_dn_new(msg, module->ldb, LTDB_BASEINFO);
445 if (msg->dn == NULL) {
448 return LDB_ERR_OPERATIONS_ERROR;
450 el[0].name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
451 if (el[0].name == NULL) {
454 return LDB_ERR_OPERATIONS_ERROR;
457 el[0].num_values = 1;
458 el[0].flags = LDB_FLAG_MOD_REPLACE;
459 val.data = (uint8_t *)s;
460 val.length = strlen(s);
462 el[1].name = talloc_strdup(msg, LTDB_MOD_TIMESTAMP);
463 if (el[1].name == NULL) {
466 return LDB_ERR_OPERATIONS_ERROR;
468 el[1].values = &val_time;
469 el[1].num_values = 1;
470 el[1].flags = LDB_FLAG_MOD_REPLACE;
472 s = ldb_timestring(msg, t);
474 return LDB_ERR_OPERATIONS_ERROR;
477 val_time.data = (uint8_t *)s;
478 val_time.length = strlen(s);
480 ret = ltdb_modify_internal(module, msg);
484 if (ret == LDB_SUCCESS) {
485 ltdb->sequence_number += 1;
488 /* updating the tdb_seqnum here avoids us reloading the cache
489 records due to our own modification */
490 ltdb->tdb_seqnum = tdb_get_seqnum(ltdb->tdb);
495 int ltdb_check_at_attributes_values(const struct ldb_val *value)
499 for (i = 0; ltdb_valid_attr_flags[i].name != NULL; i++) {
500 if ((strcmp(ltdb_valid_attr_flags[i].name, (char *)value->data) == 0)) {