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
36 #include "ldb/include/includes.h"
38 #include "ldb/ldb_tdb/ldb_tdb.h"
40 #define LTDB_FLAG_CASE_INSENSITIVE (1<<0)
41 #define LTDB_FLAG_INTEGER (1<<1)
42 #define LTDB_FLAG_HIDDEN (1<<2)
43 #define LTDB_FLAG_OBJECTCLASS (1<<3)
45 /* valid attribute flags */
49 } ltdb_valid_attr_flags[] = {
50 { "CASE_INSENSITIVE", LTDB_FLAG_CASE_INSENSITIVE },
51 { "INTEGER", LTDB_FLAG_INTEGER },
52 { "HIDDEN", LTDB_FLAG_HIDDEN },
59 de-register any special handlers for @ATTRIBUTES
61 static void ltdb_attributes_unload(struct ldb_module *module)
63 struct ltdb_private *ltdb = module->private_data;
64 struct ldb_message *msg;
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_remove_attrib_handler(module->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 ltdb_private *ltdb = module->private_data;
111 struct ldb_message *msg = ltdb->cache->attributes;
115 dn = ldb_dn_explode(module->ldb, LTDB_ATTRIBUTES);
116 if (dn == NULL) goto failed;
118 if (ltdb_search_dn1(module, dn, msg) == -1) {
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_attrib_handler *h;
129 struct ldb_attrib_handler h2;
131 if (ltdb_attributes_flags(&msg->elements[i], &flags) != 0) {
132 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Invalid @ATTRIBUTES element for '%s'\n", msg->elements[i].name);
135 switch (flags & ~LTDB_FLAG_HIDDEN) {
137 syntax = LDB_SYNTAX_OCTET_STRING;
139 case LTDB_FLAG_CASE_INSENSITIVE:
140 syntax = LDB_SYNTAX_DIRECTORY_STRING;
142 case LTDB_FLAG_INTEGER:
143 syntax = LDB_SYNTAX_INTEGER;
146 ldb_debug(module->ldb, LDB_DEBUG_ERROR,
147 "Invalid flag combination 0x%x for '%s' in @ATTRIBUTES\n",
148 flags, msg->elements[i].name);
152 h = ldb_attrib_handler_syntax(module->ldb, syntax);
154 ldb_debug(module->ldb, LDB_DEBUG_ERROR,
155 "Invalid attribute syntax '%s' for '%s' in @ATTRIBUTES\n",
156 syntax, msg->elements[i].name);
160 h2.attr = msg->elements[i].name;
161 h2.flags |= LDB_ATTR_FLAG_ALLOCATED;
162 if (ldb_set_attrib_handlers(module->ldb, &h2, 1) != 0) {
174 register any subclasses from @SUBCLASSES
176 static int ltdb_subclasses_load(struct ldb_module *module)
178 struct ltdb_private *ltdb = module->private_data;
179 struct ldb_message *msg = ltdb->cache->subclasses;
183 dn = ldb_dn_explode(module->ldb, LTDB_SUBCLASSES);
184 if (dn == NULL) goto failed;
186 if (ltdb_search_dn1(module, dn, msg) == -1) {
192 for (i=0;i<msg->num_elements;i++) {
193 struct ldb_message_element *el = &msg->elements[i];
194 for (j=0;j<el->num_values;j++) {
195 if (ldb_subclass_add(module->ldb, el->name,
196 (char *)el->values[j].data) != 0) {
209 de-register any @SUBCLASSES
211 static void ltdb_subclasses_unload(struct ldb_module *module)
213 struct ltdb_private *ltdb = module->private_data;
214 struct ldb_message *msg;
217 if (ltdb->cache->subclasses == NULL) {
218 /* no previously loaded subclasses */
222 msg = ltdb->cache->subclasses;
223 for (i=0;i<msg->num_elements;i++) {
224 ldb_subclass_remove(module->ldb, msg->elements[i].name);
227 talloc_free(ltdb->cache->subclasses);
228 ltdb->cache->subclasses = NULL;
233 initialise the baseinfo record
235 static int ltdb_baseinfo_init(struct ldb_module *module)
237 struct ltdb_private *ltdb = module->private_data;
238 struct ldb_message *msg;
239 struct ldb_message_element el;
242 /* the initial sequence number must be different from the one
243 set in ltdb_cache_free(). Thanks to Jon for pointing this
245 const char *initial_sequence_number = "1";
247 ltdb->sequence_number = atof(initial_sequence_number);
249 msg = talloc(ltdb, struct ldb_message);
254 msg->num_elements = 1;
256 msg->dn = ldb_dn_explode(msg, LTDB_BASEINFO);
260 el.name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
267 val.data = (uint8_t *)talloc_strdup(msg, initial_sequence_number);
273 ret = ltdb_store(module, msg, TDB_INSERT);
286 free any cache records
288 static void ltdb_cache_free(struct ldb_module *module)
290 struct ltdb_private *ltdb = module->private_data;
292 ltdb->sequence_number = 0;
293 talloc_free(ltdb->cache);
300 int ltdb_cache_reload(struct ldb_module *module)
302 ltdb_attributes_unload(module);
303 ltdb_subclasses_unload(module);
304 ltdb_cache_free(module);
305 return ltdb_cache_load(module);
309 load the cache records
311 int ltdb_cache_load(struct ldb_module *module)
313 struct ltdb_private *ltdb = module->private_data;
314 struct ldb_dn *baseinfo_dn = NULL;
315 struct ldb_dn *indexlist_dn = NULL;
317 struct ldb_message *baseinfo;
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_explode(module->ldb, LTDB_BASEINFO);
342 if (baseinfo_dn == NULL) goto failed;
344 if (ltdb_search_dn1(module, baseinfo_dn, baseinfo) == -1) {
348 /* possibly initialise the baseinfo */
350 if (ltdb_baseinfo_init(module) != 0) {
353 if (ltdb_search_dn1(module, baseinfo_dn, baseinfo) != 1) {
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_explode(module->ldb, LTDB_INDEXLIST);
387 if (indexlist_dn == NULL) goto failed;
389 if (ltdb_search_dn1(module, indexlist_dn, ltdb->cache->indexlist) == -1) {
393 if (ltdb_attributes_load(module) == -1) {
396 if (ltdb_subclasses_load(module) == -1) {
401 talloc_free(baseinfo);
402 talloc_free(baseinfo_dn);
403 talloc_free(indexlist_dn);
407 talloc_free(baseinfo);
408 talloc_free(baseinfo_dn);
409 talloc_free(indexlist_dn);
415 increase the sequence number to indicate a database change
417 int ltdb_increase_sequence_number(struct ldb_module *module)
419 struct ltdb_private *ltdb = module->private_data;
420 struct ldb_message *msg;
421 struct ldb_message_element el[2];
423 struct ldb_val val_time;
424 time_t t = time(NULL);
428 msg = talloc(ltdb, struct ldb_message);
434 s = talloc_asprintf(msg, "%llu", ltdb->sequence_number+1);
440 msg->num_elements = ARRAY_SIZE(el);
442 msg->dn = ldb_dn_explode(msg, LTDB_BASEINFO);
443 if (msg->dn == NULL) {
448 el[0].name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
449 if (el[0].name == NULL) {
455 el[0].num_values = 1;
456 el[0].flags = LDB_FLAG_MOD_REPLACE;
457 val.data = (uint8_t *)s;
458 val.length = strlen(s);
460 el[1].name = talloc_strdup(msg, LTDB_MOD_TIMESTAMP);
461 if (el[1].name == NULL) {
466 el[1].values = &val_time;
467 el[1].num_values = 1;
468 el[1].flags = LDB_FLAG_MOD_REPLACE;
470 s = ldb_timestring(msg, t);
475 val_time.data = (uint8_t *)s;
476 val_time.length = strlen(s);
478 ret = ltdb_modify_internal(module, msg);
483 ltdb->sequence_number += 1;
491 return the attribute flags from the @ATTRIBUTES record
492 for the given attribute
494 int ltdb_attribute_flags(struct ldb_module *module, const char *attr_name)
496 struct ltdb_private *ltdb = module->private_data;
497 const struct ldb_message_element *attr_el;
500 if (ltdb->cache->last_attribute.name &&
501 ldb_attr_cmp(ltdb->cache->last_attribute.name, attr_name) == 0) {
502 return ltdb->cache->last_attribute.flags;
505 /* objectclass is a special default case */
506 if (ldb_attr_cmp(attr_name, LTDB_OBJECTCLASS) == 0) {
507 ret = LTDB_FLAG_OBJECTCLASS | LTDB_FLAG_CASE_INSENSITIVE;
510 attr_el = ldb_msg_find_element(ltdb->cache->attributes, attr_name);
513 /* check if theres a wildcard attribute */
514 attr_el = ldb_msg_find_element(ltdb->cache->attributes, "*");
521 for (i = 0; i < attr_el->num_values; i++) {
522 for (j=0; ltdb_valid_attr_flags[j].name; j++) {
523 if (strcmp(ltdb_valid_attr_flags[j].name,
524 (char *)attr_el->values[i].data) == 0) {
525 ret |= ltdb_valid_attr_flags[j].value;
530 talloc_free(ltdb->cache->last_attribute.name);
532 ltdb->cache->last_attribute.name = talloc_strdup(ltdb->cache, attr_name);
533 ltdb->cache->last_attribute.flags = ret;
538 int ltdb_check_at_attributes_values(const struct ldb_val *value)
542 for (i = 0; ltdb_valid_attr_flags[i].name != NULL; i++) {
543 if ((strcmp(ltdb_valid_attr_flags[i].name, (char *)value->data) == 0)) {