ldb_tdb: Call talloc_free(options_dn) as soon as we are done with options_dn
[Samba.git] / lib / ldb / ldb_tdb / ldb_cache.c
blob388b461ece1eaa1eb96821f2dbfa3b50cd2a7334
1 /*
2 ldb database library
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
8 ** under the LGPL
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/>.
25 * Name: ldb
27 * Component: ldb tdb cache functions
29 * Description: cache special records in a ldb/tdb
31 * Author: Andrew Tridgell
34 #include "ldb_tdb.h"
35 #include "ldb_private.h"
37 #define LTDB_FLAG_CASE_INSENSITIVE (1<<0)
38 #define LTDB_FLAG_INTEGER (1<<1)
39 #define LTDB_FLAG_HIDDEN (1<<2)
41 /* valid attribute flags */
42 static const struct {
43 const char *name;
44 int value;
45 } ltdb_valid_attr_flags[] = {
46 { "CASE_INSENSITIVE", LTDB_FLAG_CASE_INSENSITIVE },
47 { "INTEGER", LTDB_FLAG_INTEGER },
48 { "HIDDEN", LTDB_FLAG_HIDDEN },
49 { "NONE", 0 },
50 { NULL, 0 }
55 de-register any special handlers for @ATTRIBUTES
57 static void ltdb_attributes_unload(struct ldb_module *module)
59 struct ldb_context *ldb = ldb_module_get_ctx(module);
61 ldb_schema_attribute_remove_flagged(ldb, LDB_ATTR_FLAG_FROM_DB);
66 add up the attrib flags for a @ATTRIBUTES element
68 static int ltdb_attributes_flags(struct ldb_message_element *el, unsigned *v)
70 unsigned int i;
71 unsigned value = 0;
72 for (i=0;i<el->num_values;i++) {
73 unsigned int j;
74 for (j=0;ltdb_valid_attr_flags[j].name;j++) {
75 if (strcmp(ltdb_valid_attr_flags[j].name,
76 (char *)el->values[i].data) == 0) {
77 value |= ltdb_valid_attr_flags[j].value;
78 break;
81 if (ltdb_valid_attr_flags[j].name == NULL) {
82 return -1;
85 *v = value;
86 return 0;
89 static int ldb_schema_attribute_compare(const void *p1, const void *p2)
91 const struct ldb_schema_attribute *sa1 = (const struct ldb_schema_attribute *)p1;
92 const struct ldb_schema_attribute *sa2 = (const struct ldb_schema_attribute *)p2;
93 return ldb_attr_cmp(sa1->name, sa2->name);
97 register any special handlers from @ATTRIBUTES
99 static int ltdb_attributes_load(struct ldb_module *module)
101 struct ldb_schema_attribute *attrs;
102 struct ldb_context *ldb;
103 struct ldb_message *attrs_msg = NULL;
104 struct ldb_dn *dn;
105 unsigned int i;
106 unsigned int num_loaded_attrs = 0;
107 int r;
109 ldb = ldb_module_get_ctx(module);
111 if (ldb->schema.attribute_handler_override) {
112 /* we skip loading the @ATTRIBUTES record when a module is supplying
113 its own attribute handling */
114 return 0;
117 attrs_msg = ldb_msg_new(module);
118 if (attrs_msg == NULL) {
119 goto failed;
122 dn = ldb_dn_new(module, ldb, LTDB_ATTRIBUTES);
123 if (dn == NULL) goto failed;
125 r = ltdb_search_dn1(module, dn, attrs_msg,
126 LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC
127 |LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC
128 |LDB_UNPACK_DATA_FLAG_NO_DN);
129 talloc_free(dn);
130 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
131 goto failed;
133 if (r == LDB_ERR_NO_SUCH_OBJECT || attrs_msg->num_elements == 0) {
134 TALLOC_FREE(attrs_msg);
135 return 0;
138 attrs = talloc_array(attrs_msg,
139 struct ldb_schema_attribute,
140 attrs_msg->num_elements
141 + ldb->schema.num_attributes);
142 if (attrs == NULL) {
143 goto failed;
146 memcpy(attrs,
147 ldb->schema.attributes,
148 sizeof(ldb->schema.attributes[0]) * ldb->schema.num_attributes);
150 /* mapping these flags onto ldap 'syntaxes' isn't strictly correct,
151 but its close enough for now */
152 for (i=0;i<attrs_msg->num_elements;i++) {
153 unsigned flags;
154 const char *syntax;
155 const struct ldb_schema_syntax *s;
156 const struct ldb_schema_attribute *a =
157 ldb_schema_attribute_by_name(ldb,
158 attrs_msg->elements[i].name);
159 if (a != NULL && a->flags & LDB_ATTR_FLAG_FIXED) {
160 /* Must already be set in the array, and kept */
161 continue;
164 if (ltdb_attributes_flags(&attrs_msg->elements[i], &flags) != 0) {
165 ldb_debug(ldb, LDB_DEBUG_ERROR,
166 "Invalid @ATTRIBUTES element for '%s'",
167 attrs_msg->elements[i].name);
168 goto failed;
170 switch (flags & ~LTDB_FLAG_HIDDEN) {
171 case 0:
172 syntax = LDB_SYNTAX_OCTET_STRING;
173 break;
174 case LTDB_FLAG_CASE_INSENSITIVE:
175 syntax = LDB_SYNTAX_DIRECTORY_STRING;
176 break;
177 case LTDB_FLAG_INTEGER:
178 syntax = LDB_SYNTAX_INTEGER;
179 break;
180 default:
181 ldb_debug(ldb, LDB_DEBUG_ERROR,
182 "Invalid flag combination 0x%x for '%s' "
183 "in @ATTRIBUTES",
184 flags, attrs_msg->elements[i].name);
185 goto failed;
188 s = ldb_standard_syntax_by_name(ldb, syntax);
189 if (s == NULL) {
190 ldb_debug(ldb, LDB_DEBUG_ERROR,
191 "Invalid attribute syntax '%s' for '%s' "
192 "in @ATTRIBUTES",
193 syntax, attrs_msg->elements[i].name);
194 goto failed;
197 flags |= LDB_ATTR_FLAG_ALLOCATED | LDB_ATTR_FLAG_FROM_DB;
199 r = ldb_schema_attribute_fill_with_syntax(ldb,
200 attrs,
201 attrs_msg->elements[i].name,
202 flags, s,
203 &attrs[num_loaded_attrs + ldb->schema.num_attributes]);
204 if (r != 0) {
205 goto failed;
207 num_loaded_attrs++;
210 attrs = talloc_realloc(attrs_msg,
211 attrs, struct ldb_schema_attribute,
212 num_loaded_attrs + ldb->schema.num_attributes);
213 if (attrs == NULL) {
214 goto failed;
216 TYPESAFE_QSORT(attrs, num_loaded_attrs + ldb->schema.num_attributes,
217 ldb_schema_attribute_compare);
218 talloc_unlink(ldb, ldb->schema.attributes);
219 ldb->schema.attributes = talloc_steal(ldb, attrs);
220 ldb->schema.num_attributes = num_loaded_attrs + ldb->schema.num_attributes;
221 TALLOC_FREE(attrs_msg);
223 return 0;
224 failed:
225 TALLOC_FREE(attrs_msg);
226 return -1;
231 initialise the baseinfo record
233 static int ltdb_baseinfo_init(struct ldb_module *module)
235 struct ldb_context *ldb;
236 void *data = ldb_module_get_private(module);
237 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
238 struct ldb_message *msg;
239 struct ldb_message_element el;
240 struct ldb_val val;
241 int ret;
242 /* the initial sequence number must be different from the one
243 set in ltdb_cache_free(). Thanks to Jon for pointing this
244 out. */
245 const char *initial_sequence_number = "1";
247 ldb = ldb_module_get_ctx(module);
249 ltdb->sequence_number = atof(initial_sequence_number);
251 msg = ldb_msg_new(ltdb);
252 if (msg == NULL) {
253 goto failed;
256 msg->num_elements = 1;
257 msg->elements = &el;
258 msg->dn = ldb_dn_new(msg, ldb, LTDB_BASEINFO);
259 if (!msg->dn) {
260 goto failed;
262 el.name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
263 if (!el.name) {
264 goto failed;
266 el.values = &val;
267 el.num_values = 1;
268 el.flags = 0;
269 val.data = (uint8_t *)talloc_strdup(msg, initial_sequence_number);
270 if (!val.data) {
271 goto failed;
273 val.length = 1;
275 ret = ltdb_store(module, msg, TDB_INSERT);
277 talloc_free(msg);
279 return ret;
281 failed:
282 talloc_free(msg);
283 errno = ENOMEM;
284 return LDB_ERR_OPERATIONS_ERROR;
288 free any cache records
290 static void ltdb_cache_free(struct ldb_module *module)
292 void *data = ldb_module_get_private(module);
293 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
295 ltdb->sequence_number = 0;
296 talloc_free(ltdb->cache);
297 ltdb->cache = NULL;
301 force a cache reload
303 int ltdb_cache_reload(struct ldb_module *module)
305 ltdb_attributes_unload(module);
306 ltdb_cache_free(module);
307 return ltdb_cache_load(module);
311 load the cache records
313 int ltdb_cache_load(struct ldb_module *module)
315 struct ldb_context *ldb;
316 void *data = ldb_module_get_private(module);
317 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
318 struct ldb_dn *baseinfo_dn = NULL, *options_dn = NULL;
319 struct ldb_dn *indexlist_dn = NULL;
320 uint64_t seq;
321 struct ldb_message *baseinfo = NULL, *options = NULL;
322 int r;
324 ldb = ldb_module_get_ctx(module);
326 /* a very fast check to avoid extra database reads */
327 if (ltdb->cache != NULL &&
328 tdb_get_seqnum(ltdb->tdb) == ltdb->tdb_seqnum) {
329 return 0;
332 if (ltdb->cache == NULL) {
333 ltdb->cache = talloc_zero(ltdb, struct ltdb_cache);
334 if (ltdb->cache == NULL) goto failed;
335 ltdb->cache->indexlist = ldb_msg_new(ltdb->cache);
336 if (ltdb->cache->indexlist == NULL) {
337 goto failed;
341 baseinfo = ldb_msg_new(ltdb->cache);
342 if (baseinfo == NULL) goto failed;
344 baseinfo_dn = ldb_dn_new(baseinfo, ldb, LTDB_BASEINFO);
345 if (baseinfo_dn == NULL) goto failed;
347 r= ltdb_search_dn1(module, baseinfo_dn, baseinfo, 0);
348 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
349 goto failed;
352 /* possibly initialise the baseinfo */
353 if (r == LDB_ERR_NO_SUCH_OBJECT) {
355 if (tdb_transaction_start(ltdb->tdb) != 0) {
356 goto failed;
359 /* error handling for ltdb_baseinfo_init() is by
360 looking for the record again. */
361 ltdb_baseinfo_init(module);
363 tdb_transaction_commit(ltdb->tdb);
365 if (ltdb_search_dn1(module, baseinfo_dn, baseinfo, 0) != LDB_SUCCESS) {
366 goto failed;
370 ltdb->tdb_seqnum = tdb_get_seqnum(ltdb->tdb);
372 /* if the current internal sequence number is the same as the one
373 in the database then assume the rest of the cache is OK */
374 seq = ldb_msg_find_attr_as_uint64(baseinfo, LTDB_SEQUENCE_NUMBER, 0);
375 if (seq == ltdb->sequence_number) {
376 goto done;
378 ltdb->sequence_number = seq;
380 /* Read an interpret database options */
381 options = ldb_msg_new(ltdb->cache);
382 if (options == NULL) goto failed;
384 options_dn = ldb_dn_new(options, ldb, LTDB_OPTIONS);
385 if (options_dn == NULL) goto failed;
387 r= ltdb_search_dn1(module, options_dn, options, 0);
388 talloc_free(options_dn);
389 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
390 goto failed;
393 /* set flags if they do exist */
394 if (r == LDB_SUCCESS) {
395 ltdb->check_base = ldb_msg_find_attr_as_bool(options,
396 LTDB_CHECK_BASE,
397 false);
398 ltdb->disallow_dn_filter = ldb_msg_find_attr_as_bool(options,
399 LTDB_DISALLOW_DN_FILTER,
400 false);
401 } else {
402 ltdb->check_base = false;
403 ltdb->disallow_dn_filter = false;
406 talloc_free(ltdb->cache->indexlist);
408 * ltdb_attributes_unload() calls internally talloc_free() on
409 * any non-fixed elemnts in ldb->schema.attributes.
411 * NOTE WELL: This is per-ldb, not per module, so overwrites
412 * the handlers across all databases when used under Samba's
413 * partition module.
415 ltdb_attributes_unload(module);
416 ltdb->cache->indexlist = ldb_msg_new(ltdb->cache);
417 if (ltdb->cache->indexlist == NULL) {
418 goto failed;
420 ltdb->cache->one_level_indexes = false;
421 ltdb->cache->attribute_indexes = false;
423 indexlist_dn = ldb_dn_new(module, ldb, LTDB_INDEXLIST);
424 if (indexlist_dn == NULL) goto failed;
426 r = ltdb_search_dn1(module, indexlist_dn, ltdb->cache->indexlist,
427 LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC
428 |LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC
429 |LDB_UNPACK_DATA_FLAG_NO_DN);
430 if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
431 goto failed;
434 if (ldb_msg_find_element(ltdb->cache->indexlist, LTDB_IDXONE) != NULL) {
435 ltdb->cache->one_level_indexes = true;
437 if (ldb_msg_find_element(ltdb->cache->indexlist, LTDB_IDXATTR) != NULL) {
438 ltdb->cache->attribute_indexes = true;
442 * NOTE WELL: This is per-ldb, not per module, so overwrites
443 * the handlers across all databases when used under Samba's
444 * partition module.
446 if (ltdb_attributes_load(module) == -1) {
447 goto failed;
450 done:
451 talloc_free(options);
452 talloc_free(baseinfo);
453 talloc_free(indexlist_dn);
454 return 0;
456 failed:
457 talloc_free(options);
458 talloc_free(baseinfo);
459 talloc_free(indexlist_dn);
460 return -1;
465 increase the sequence number to indicate a database change
467 int ltdb_increase_sequence_number(struct ldb_module *module)
469 struct ldb_context *ldb;
470 void *data = ldb_module_get_private(module);
471 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
472 struct ldb_message *msg;
473 struct ldb_message_element el[2];
474 struct ldb_val val;
475 struct ldb_val val_time;
476 time_t t = time(NULL);
477 char *s = NULL;
478 int ret;
480 ldb = ldb_module_get_ctx(module);
482 msg = ldb_msg_new(ltdb);
483 if (msg == NULL) {
484 errno = ENOMEM;
485 return LDB_ERR_OPERATIONS_ERROR;
488 s = talloc_asprintf(msg, "%llu", ltdb->sequence_number+1);
489 if (!s) {
490 talloc_free(msg);
491 errno = ENOMEM;
492 return LDB_ERR_OPERATIONS_ERROR;
495 msg->num_elements = ARRAY_SIZE(el);
496 msg->elements = el;
497 msg->dn = ldb_dn_new(msg, ldb, LTDB_BASEINFO);
498 if (msg->dn == NULL) {
499 talloc_free(msg);
500 errno = ENOMEM;
501 return LDB_ERR_OPERATIONS_ERROR;
503 el[0].name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
504 if (el[0].name == NULL) {
505 talloc_free(msg);
506 errno = ENOMEM;
507 return LDB_ERR_OPERATIONS_ERROR;
509 el[0].values = &val;
510 el[0].num_values = 1;
511 el[0].flags = LDB_FLAG_MOD_REPLACE;
512 val.data = (uint8_t *)s;
513 val.length = strlen(s);
515 el[1].name = talloc_strdup(msg, LTDB_MOD_TIMESTAMP);
516 if (el[1].name == NULL) {
517 talloc_free(msg);
518 errno = ENOMEM;
519 return LDB_ERR_OPERATIONS_ERROR;
521 el[1].values = &val_time;
522 el[1].num_values = 1;
523 el[1].flags = LDB_FLAG_MOD_REPLACE;
525 s = ldb_timestring(msg, t);
526 if (s == NULL) {
527 talloc_free(msg);
528 return LDB_ERR_OPERATIONS_ERROR;
531 val_time.data = (uint8_t *)s;
532 val_time.length = strlen(s);
534 ret = ltdb_modify_internal(module, msg, NULL);
536 talloc_free(msg);
538 if (ret == LDB_SUCCESS) {
539 ltdb->sequence_number += 1;
542 /* updating the tdb_seqnum here avoids us reloading the cache
543 records due to our own modification */
544 ltdb->tdb_seqnum = tdb_get_seqnum(ltdb->tdb);
546 return ret;
549 int ltdb_check_at_attributes_values(const struct ldb_val *value)
551 unsigned int i;
553 for (i = 0; ltdb_valid_attr_flags[i].name != NULL; i++) {
554 if ((strcmp(ltdb_valid_attr_flags[i].name, (char *)value->data) == 0)) {
555 return 0;
559 return -1;