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/include/includes.h"
37 #include "ldb/ldb_tdb/ldb_tdb.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 int ltdb_attribute_flags(struct ldb_module *module, const char *attr_name);
46 /* valid attribute flags */
50 } ltdb_valid_attr_flags[] = {
51 { "CASE_INSENSITIVE", LTDB_FLAG_CASE_INSENSITIVE },
52 { "INTEGER", LTDB_FLAG_INTEGER },
53 { "HIDDEN", LTDB_FLAG_HIDDEN },
60 de-register any special handlers for @ATTRIBUTES
62 static void ltdb_attributes_unload(struct ldb_module *module)
64 struct ltdb_private *ltdb =
65 (struct ltdb_private *)module->private_data;
66 struct ldb_message *msg;
69 if (ltdb->cache->attributes == NULL) {
70 /* no previously loaded attributes */
74 msg = ltdb->cache->attributes;
75 for (i=0;i<msg->num_elements;i++) {
76 ldb_remove_attrib_handler(module->ldb, msg->elements[i].name);
79 talloc_free(ltdb->cache->attributes);
80 ltdb->cache->attributes = NULL;
84 add up the attrib flags for a @ATTRIBUTES element
86 static int ltdb_attributes_flags(struct ldb_message_element *el, unsigned *v)
90 for (i=0;i<el->num_values;i++) {
92 for (j=0;ltdb_valid_attr_flags[j].name;j++) {
93 if (strcmp(ltdb_valid_attr_flags[j].name,
94 (char *)el->values[i].data) == 0) {
95 value |= ltdb_valid_attr_flags[j].value;
99 if (ltdb_valid_attr_flags[j].name == NULL) {
108 register any special handlers from @ATTRIBUTES
110 static int ltdb_attributes_load(struct ldb_module *module)
112 struct ltdb_private *ltdb =
113 (struct ltdb_private *)module->private_data;
114 struct ldb_message *msg = ltdb->cache->attributes;
118 dn = ldb_dn_explode(module->ldb, LTDB_ATTRIBUTES);
119 if (dn == NULL) goto failed;
121 if (ltdb_search_dn1(module, dn, msg) == -1) {
126 /* mapping these flags onto ldap 'syntaxes' isn't strictly correct,
127 but its close enough for now */
128 for (i=0;i<msg->num_elements;i++) {
131 const struct ldb_attrib_handler *h;
132 struct ldb_attrib_handler h2;
134 if (ltdb_attributes_flags(&msg->elements[i], &flags) != 0) {
135 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Invalid @ATTRIBUTES element for '%s'\n", msg->elements[i].name);
138 switch (flags & ~LTDB_FLAG_HIDDEN) {
140 syntax = LDB_SYNTAX_OCTET_STRING;
142 case LTDB_FLAG_CASE_INSENSITIVE:
143 syntax = LDB_SYNTAX_DIRECTORY_STRING;
145 case LTDB_FLAG_INTEGER:
146 syntax = LDB_SYNTAX_INTEGER;
149 ldb_debug(module->ldb, LDB_DEBUG_ERROR,
150 "Invalid flag combination 0x%x for '%s' in @ATTRIBUTES\n",
151 flags, msg->elements[i].name);
155 h = ldb_attrib_handler_syntax(module->ldb, syntax);
157 ldb_debug(module->ldb, LDB_DEBUG_ERROR,
158 "Invalid attribute syntax '%s' for '%s' in @ATTRIBUTES\n",
159 syntax, msg->elements[i].name);
163 h2.attr = msg->elements[i].name;
164 h2.flags |= LDB_ATTR_FLAG_ALLOCATED;
165 if (ldb_set_attrib_handlers(module->ldb, &h2, 1) != 0) {
177 register any subclasses from @SUBCLASSES
179 static int ltdb_subclasses_load(struct ldb_module *module)
181 struct ltdb_private *ltdb =
182 (struct ltdb_private *)module->private_data;
183 struct ldb_message *msg = ltdb->cache->subclasses;
187 dn = ldb_dn_explode(module->ldb, LTDB_SUBCLASSES);
188 if (dn == NULL) goto failed;
190 if (ltdb_search_dn1(module, dn, msg) == -1) {
196 for (i=0;i<msg->num_elements;i++) {
197 struct ldb_message_element *el = &msg->elements[i];
198 for (j=0;j<el->num_values;j++) {
199 if (ldb_subclass_add(module->ldb, el->name,
200 (char *)el->values[j].data) != 0) {
213 de-register any @SUBCLASSES
215 static void ltdb_subclasses_unload(struct ldb_module *module)
217 struct ltdb_private *ltdb =
218 (struct ltdb_private *)module->private_data;
219 struct ldb_message *msg;
222 if (ltdb->cache->subclasses == NULL) {
223 /* no previously loaded subclasses */
227 msg = ltdb->cache->subclasses;
228 for (i=0;i<msg->num_elements;i++) {
229 ldb_subclass_remove(module->ldb, msg->elements[i].name);
232 talloc_free(ltdb->cache->subclasses);
233 ltdb->cache->subclasses = NULL;
238 initialise the baseinfo record
240 static int ltdb_baseinfo_init(struct ldb_module *module)
242 struct ltdb_private *ltdb =
243 (struct ltdb_private *)module->private_data;
244 struct ldb_message *msg;
245 struct ldb_message_element el;
248 /* the initial sequence number must be different from the one
249 set in ltdb_cache_free(). Thanks to Jon for pointing this
251 const char *initial_sequence_number = "1";
253 ltdb->sequence_number = atof(initial_sequence_number);
255 msg = talloc(ltdb, struct ldb_message);
260 msg->num_elements = 1;
262 msg->dn = ldb_dn_explode(msg, LTDB_BASEINFO);
266 el.name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
273 val.data = (uint8_t *)talloc_strdup(msg, initial_sequence_number);
279 ret = ltdb_store(module, msg, TDB_INSERT);
292 free any cache records
294 static void ltdb_cache_free(struct ldb_module *module)
296 struct ltdb_private *ltdb =
297 (struct ltdb_private *)module->private_data;
299 ltdb->sequence_number = 0;
300 talloc_free(ltdb->cache);
307 int ltdb_cache_reload(struct ldb_module *module)
309 ltdb_attributes_unload(module);
310 ltdb_subclasses_unload(module);
311 ltdb_cache_free(module);
312 return ltdb_cache_load(module);
316 load the cache records
318 int ltdb_cache_load(struct ldb_module *module)
320 struct ltdb_private *ltdb =
321 (struct ltdb_private *)module->private_data;
322 struct ldb_dn *baseinfo_dn = NULL;
323 struct ldb_dn *indexlist_dn = NULL;
325 struct ldb_message *baseinfo = NULL;
327 /* a very fast check to avoid extra database reads */
328 if (ltdb->cache != NULL &&
329 tdb_get_seqnum(ltdb->tdb) == ltdb->tdb_seqnum) {
333 if (ltdb->cache == NULL) {
334 ltdb->cache = talloc_zero(ltdb, struct ltdb_cache);
335 if (ltdb->cache == NULL) goto failed;
336 ltdb->cache->indexlist = talloc_zero(ltdb->cache, struct ldb_message);
337 ltdb->cache->subclasses = talloc_zero(ltdb->cache, struct ldb_message);
338 ltdb->cache->attributes = talloc_zero(ltdb->cache, struct ldb_message);
339 if (ltdb->cache->indexlist == NULL ||
340 ltdb->cache->subclasses == NULL ||
341 ltdb->cache->attributes == NULL) {
346 baseinfo = talloc(ltdb->cache, struct ldb_message);
347 if (baseinfo == NULL) goto failed;
349 baseinfo_dn = ldb_dn_explode(module->ldb, LTDB_BASEINFO);
350 if (baseinfo_dn == NULL) goto failed;
352 if (ltdb_search_dn1(module, baseinfo_dn, baseinfo) == -1) {
356 /* possibly initialise the baseinfo */
358 if (ltdb_baseinfo_init(module) != 0) {
361 if (ltdb_search_dn1(module, baseinfo_dn, baseinfo) != 1) {
366 ltdb->tdb_seqnum = tdb_get_seqnum(ltdb->tdb);
368 /* if the current internal sequence number is the same as the one
369 in the database then assume the rest of the cache is OK */
370 seq = ldb_msg_find_attr_as_uint64(baseinfo, LTDB_SEQUENCE_NUMBER, 0);
371 if (seq == ltdb->sequence_number) {
374 ltdb->sequence_number = seq;
376 talloc_free(ltdb->cache->last_attribute.name);
377 memset(<db->cache->last_attribute, 0, sizeof(ltdb->cache->last_attribute));
379 ltdb_attributes_unload(module);
380 ltdb_subclasses_unload(module);
382 talloc_free(ltdb->cache->indexlist);
383 talloc_free(ltdb->cache->subclasses);
385 ltdb->cache->indexlist = talloc_zero(ltdb->cache, struct ldb_message);
386 ltdb->cache->subclasses = talloc_zero(ltdb->cache, struct ldb_message);
387 ltdb->cache->attributes = talloc_zero(ltdb->cache, struct ldb_message);
388 if (ltdb->cache->indexlist == NULL ||
389 ltdb->cache->subclasses == NULL ||
390 ltdb->cache->attributes == NULL) {
394 indexlist_dn = ldb_dn_explode(module->ldb, LTDB_INDEXLIST);
395 if (indexlist_dn == NULL) goto failed;
397 if (ltdb_search_dn1(module, indexlist_dn, ltdb->cache->indexlist) == -1) {
401 if (ltdb_attributes_load(module) == -1) {
404 if (ltdb_subclasses_load(module) == -1) {
409 talloc_free(baseinfo);
410 talloc_free(baseinfo_dn);
411 talloc_free(indexlist_dn);
415 talloc_free(baseinfo);
416 talloc_free(baseinfo_dn);
417 talloc_free(indexlist_dn);
423 increase the sequence number to indicate a database change
425 int ltdb_increase_sequence_number(struct ldb_module *module)
427 struct ltdb_private *ltdb =
428 (struct ltdb_private *)module->private_data;
429 struct ldb_message *msg;
430 struct ldb_message_element el[2];
432 struct ldb_val val_time;
433 time_t t = time(NULL);
437 msg = talloc(ltdb, struct ldb_message);
443 s = talloc_asprintf(msg, "%llu", ltdb->sequence_number+1);
449 msg->num_elements = ARRAY_SIZE(el);
451 msg->dn = ldb_dn_explode(msg, LTDB_BASEINFO);
452 if (msg->dn == NULL) {
457 el[0].name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
458 if (el[0].name == NULL) {
464 el[0].num_values = 1;
465 el[0].flags = LDB_FLAG_MOD_REPLACE;
466 val.data = (uint8_t *)s;
467 val.length = strlen(s);
469 el[1].name = talloc_strdup(msg, LTDB_MOD_TIMESTAMP);
470 if (el[1].name == NULL) {
475 el[1].values = &val_time;
476 el[1].num_values = 1;
477 el[1].flags = LDB_FLAG_MOD_REPLACE;
479 s = ldb_timestring(msg, t);
484 val_time.data = (uint8_t *)s;
485 val_time.length = strlen(s);
487 ret = ltdb_modify_internal(module, msg);
492 ltdb->sequence_number += 1;
500 return the attribute flags from the @ATTRIBUTES record
501 for the given attribute
503 int ltdb_attribute_flags(struct ldb_module *module, const char *attr_name)
505 struct ltdb_private *ltdb =
506 (struct ltdb_private *)module->private_data;
507 const struct ldb_message_element *attr_el;
510 if (ltdb->cache->last_attribute.name &&
511 ldb_attr_cmp(ltdb->cache->last_attribute.name, attr_name) == 0) {
512 return ltdb->cache->last_attribute.flags;
515 /* objectclass is a special default case */
516 if (ldb_attr_cmp(attr_name, LTDB_OBJECTCLASS) == 0) {
517 ret = LTDB_FLAG_OBJECTCLASS | LTDB_FLAG_CASE_INSENSITIVE;
520 attr_el = ldb_msg_find_element(ltdb->cache->attributes, attr_name);
523 /* check if theres a wildcard attribute */
524 attr_el = ldb_msg_find_element(ltdb->cache->attributes, "*");
531 for (i = 0; i < attr_el->num_values; i++) {
532 for (j=0; ltdb_valid_attr_flags[j].name; j++) {
533 if (strcmp(ltdb_valid_attr_flags[j].name,
534 (char *)attr_el->values[i].data) == 0) {
535 ret |= ltdb_valid_attr_flags[j].value;
540 talloc_free(ltdb->cache->last_attribute.name);
542 ltdb->cache->last_attribute.name = talloc_strdup(ltdb->cache, attr_name);
543 ltdb->cache->last_attribute.flags = ret;
548 int ltdb_check_at_attributes_values(const struct ldb_val *value)
552 for (i = 0; ltdb_valid_attr_flags[i].name != NULL; i++) {
553 if ((strcmp(ltdb_valid_attr_flags[i].name, (char *)value->data) == 0)) {