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_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 */
45 } ltdb_valid_attr_flags
[] = {
46 { "CASE_INSENSITIVE", LTDB_FLAG_CASE_INSENSITIVE
},
47 { "INTEGER", LTDB_FLAG_INTEGER
},
48 { "HIDDEN", LTDB_FLAG_HIDDEN
},
55 de-register any special handlers for @ATTRIBUTES
57 static void ltdb_attributes_unload(struct ldb_module
*module
)
59 struct ldb_context
*ldb
;
60 void *data
= ldb_module_get_private(module
);
61 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
62 struct ldb_message
*msg
;
65 ldb
= ldb_module_get_ctx(module
);
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_schema_attribute_remove(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 ldb_context
*ldb
;
111 void *data
= ldb_module_get_private(module
);
112 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
113 struct ldb_message
*msg
= ltdb
->cache
->attributes
;
118 ldb
= ldb_module_get_ctx(module
);
120 if (ldb
->schema
.attribute_handler_override
) {
121 /* we skip loading the @ATTRIBUTES record when a module is supplying
122 its own attribute handling */
126 dn
= ldb_dn_new(module
, ldb
, LTDB_ATTRIBUTES
);
127 if (dn
== NULL
) goto failed
;
129 r
= ltdb_search_dn1(module
, dn
, msg
);
131 if (r
!= LDB_SUCCESS
&& r
!= LDB_ERR_NO_SUCH_OBJECT
) {
134 if (r
== LDB_ERR_NO_SUCH_OBJECT
) {
137 /* mapping these flags onto ldap 'syntaxes' isn't strictly correct,
138 but its close enough for now */
139 for (i
=0;i
<msg
->num_elements
;i
++) {
142 const struct ldb_schema_syntax
*s
;
144 if (ltdb_attributes_flags(&msg
->elements
[i
], &flags
) != 0) {
145 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "Invalid @ATTRIBUTES element for '%s'", msg
->elements
[i
].name
);
148 switch (flags
& ~LTDB_FLAG_HIDDEN
) {
150 syntax
= LDB_SYNTAX_OCTET_STRING
;
152 case LTDB_FLAG_CASE_INSENSITIVE
:
153 syntax
= LDB_SYNTAX_DIRECTORY_STRING
;
155 case LTDB_FLAG_INTEGER
:
156 syntax
= LDB_SYNTAX_INTEGER
;
159 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
160 "Invalid flag combination 0x%x for '%s' in @ATTRIBUTES",
161 flags
, msg
->elements
[i
].name
);
165 s
= ldb_standard_syntax_by_name(ldb
, syntax
);
167 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
168 "Invalid attribute syntax '%s' for '%s' in @ATTRIBUTES",
169 syntax
, msg
->elements
[i
].name
);
173 flags
|= LDB_ATTR_FLAG_ALLOCATED
;
174 if (ldb_schema_attribute_add_with_syntax(ldb
, msg
->elements
[i
].name
, flags
, s
) != 0) {
186 initialise the baseinfo record
188 static int ltdb_baseinfo_init(struct ldb_module
*module
)
190 struct ldb_context
*ldb
;
191 void *data
= ldb_module_get_private(module
);
192 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
193 struct ldb_message
*msg
;
194 struct ldb_message_element el
;
197 /* the initial sequence number must be different from the one
198 set in ltdb_cache_free(). Thanks to Jon for pointing this
200 const char *initial_sequence_number
= "1";
202 ldb
= ldb_module_get_ctx(module
);
204 ltdb
->sequence_number
= atof(initial_sequence_number
);
206 msg
= ldb_msg_new(ltdb
);
211 msg
->num_elements
= 1;
213 msg
->dn
= ldb_dn_new(msg
, ldb
, LTDB_BASEINFO
);
217 el
.name
= talloc_strdup(msg
, LTDB_SEQUENCE_NUMBER
);
224 val
.data
= (uint8_t *)talloc_strdup(msg
, initial_sequence_number
);
230 ret
= ltdb_store(module
, msg
, TDB_INSERT
);
239 return LDB_ERR_OPERATIONS_ERROR
;
243 free any cache records
245 static void ltdb_cache_free(struct ldb_module
*module
)
247 void *data
= ldb_module_get_private(module
);
248 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
250 ltdb
->sequence_number
= 0;
251 talloc_free(ltdb
->cache
);
258 int ltdb_cache_reload(struct ldb_module
*module
)
260 ltdb_attributes_unload(module
);
261 ltdb_cache_free(module
);
262 return ltdb_cache_load(module
);
266 load the cache records
268 int ltdb_cache_load(struct ldb_module
*module
)
270 struct ldb_context
*ldb
;
271 void *data
= ldb_module_get_private(module
);
272 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
273 struct ldb_dn
*baseinfo_dn
= NULL
, *options_dn
= NULL
;
274 struct ldb_dn
*indexlist_dn
= NULL
;
276 struct ldb_message
*baseinfo
= NULL
, *options
= NULL
;
279 ldb
= ldb_module_get_ctx(module
);
281 /* a very fast check to avoid extra database reads */
282 if (ltdb
->cache
!= NULL
&&
283 tdb_get_seqnum(ltdb
->tdb
) == ltdb
->tdb_seqnum
) {
287 if (ltdb
->cache
== NULL
) {
288 ltdb
->cache
= talloc_zero(ltdb
, struct ltdb_cache
);
289 if (ltdb
->cache
== NULL
) goto failed
;
290 ltdb
->cache
->indexlist
= ldb_msg_new(ltdb
->cache
);
291 ltdb
->cache
->attributes
= ldb_msg_new(ltdb
->cache
);
292 if (ltdb
->cache
->indexlist
== NULL
||
293 ltdb
->cache
->attributes
== NULL
) {
298 baseinfo
= ldb_msg_new(ltdb
->cache
);
299 if (baseinfo
== NULL
) goto failed
;
301 baseinfo_dn
= ldb_dn_new(baseinfo
, ldb
, LTDB_BASEINFO
);
302 if (baseinfo_dn
== NULL
) goto failed
;
304 r
= ltdb_search_dn1(module
, baseinfo_dn
, baseinfo
);
305 if (r
!= LDB_SUCCESS
&& r
!= LDB_ERR_NO_SUCH_OBJECT
) {
309 /* possibly initialise the baseinfo */
310 if (r
== LDB_ERR_NO_SUCH_OBJECT
) {
312 if (tdb_transaction_start(ltdb
->tdb
) != 0) {
316 /* error handling for ltdb_baseinfo_init() is by
317 looking for the record again. */
318 ltdb_baseinfo_init(module
);
320 tdb_transaction_commit(ltdb
->tdb
);
322 if (ltdb_search_dn1(module
, baseinfo_dn
, baseinfo
) != LDB_SUCCESS
) {
327 ltdb
->tdb_seqnum
= tdb_get_seqnum(ltdb
->tdb
);
329 /* if the current internal sequence number is the same as the one
330 in the database then assume the rest of the cache is OK */
331 seq
= ldb_msg_find_attr_as_uint64(baseinfo
, LTDB_SEQUENCE_NUMBER
, 0);
332 if (seq
== ltdb
->sequence_number
) {
335 ltdb
->sequence_number
= seq
;
337 /* Read an interpret database options */
338 options
= ldb_msg_new(ltdb
->cache
);
339 if (options
== NULL
) goto failed
;
341 options_dn
= ldb_dn_new(options
, ldb
, LTDB_OPTIONS
);
342 if (options_dn
== NULL
) goto failed
;
344 r
= ltdb_search_dn1(module
, options_dn
, options
);
345 if (r
!= LDB_SUCCESS
&& r
!= LDB_ERR_NO_SUCH_OBJECT
) {
349 /* set flags if they do exist */
350 if (r
== LDB_SUCCESS
) {
351 ltdb
->check_base
= ldb_msg_find_attr_as_bool(options
,
354 ltdb
->disallow_dn_filter
= ldb_msg_find_attr_as_bool(options
,
355 LTDB_DISALLOW_DN_FILTER
,
358 ltdb
->check_base
= false;
359 ltdb
->disallow_dn_filter
= false;
362 talloc_free(ltdb
->cache
->indexlist
);
363 ltdb_attributes_unload(module
); /* calls internally "talloc_free" */
365 ltdb
->cache
->indexlist
= ldb_msg_new(ltdb
->cache
);
366 ltdb
->cache
->attributes
= ldb_msg_new(ltdb
->cache
);
367 if (ltdb
->cache
->indexlist
== NULL
||
368 ltdb
->cache
->attributes
== NULL
) {
371 ltdb
->cache
->one_level_indexes
= false;
372 ltdb
->cache
->attribute_indexes
= false;
374 indexlist_dn
= ldb_dn_new(module
, ldb
, LTDB_INDEXLIST
);
375 if (indexlist_dn
== NULL
) goto failed
;
377 r
= ltdb_search_dn1(module
, indexlist_dn
, ltdb
->cache
->indexlist
);
378 if (r
!= LDB_SUCCESS
&& r
!= LDB_ERR_NO_SUCH_OBJECT
) {
382 if (ldb_msg_find_element(ltdb
->cache
->indexlist
, LTDB_IDXONE
) != NULL
) {
383 ltdb
->cache
->one_level_indexes
= true;
385 if (ldb_msg_find_element(ltdb
->cache
->indexlist
, LTDB_IDXATTR
) != NULL
) {
386 ltdb
->cache
->attribute_indexes
= true;
389 if (ltdb_attributes_load(module
) == -1) {
394 talloc_free(options
);
395 talloc_free(baseinfo
);
396 talloc_free(indexlist_dn
);
400 talloc_free(options
);
401 talloc_free(baseinfo
);
402 talloc_free(indexlist_dn
);
408 increase the sequence number to indicate a database change
410 int ltdb_increase_sequence_number(struct ldb_module
*module
)
412 struct ldb_context
*ldb
;
413 void *data
= ldb_module_get_private(module
);
414 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
415 struct ldb_message
*msg
;
416 struct ldb_message_element el
[2];
418 struct ldb_val val_time
;
419 time_t t
= time(NULL
);
423 ldb
= ldb_module_get_ctx(module
);
425 msg
= ldb_msg_new(ltdb
);
428 return LDB_ERR_OPERATIONS_ERROR
;
431 s
= talloc_asprintf(msg
, "%llu", ltdb
->sequence_number
+1);
435 return LDB_ERR_OPERATIONS_ERROR
;
438 msg
->num_elements
= ARRAY_SIZE(el
);
440 msg
->dn
= ldb_dn_new(msg
, ldb
, LTDB_BASEINFO
);
441 if (msg
->dn
== NULL
) {
444 return LDB_ERR_OPERATIONS_ERROR
;
446 el
[0].name
= talloc_strdup(msg
, LTDB_SEQUENCE_NUMBER
);
447 if (el
[0].name
== NULL
) {
450 return LDB_ERR_OPERATIONS_ERROR
;
453 el
[0].num_values
= 1;
454 el
[0].flags
= LDB_FLAG_MOD_REPLACE
;
455 val
.data
= (uint8_t *)s
;
456 val
.length
= strlen(s
);
458 el
[1].name
= talloc_strdup(msg
, LTDB_MOD_TIMESTAMP
);
459 if (el
[1].name
== NULL
) {
462 return LDB_ERR_OPERATIONS_ERROR
;
464 el
[1].values
= &val_time
;
465 el
[1].num_values
= 1;
466 el
[1].flags
= LDB_FLAG_MOD_REPLACE
;
468 s
= ldb_timestring(msg
, t
);
471 return LDB_ERR_OPERATIONS_ERROR
;
474 val_time
.data
= (uint8_t *)s
;
475 val_time
.length
= strlen(s
);
477 ret
= ltdb_modify_internal(module
, msg
, NULL
);
481 if (ret
== LDB_SUCCESS
) {
482 ltdb
->sequence_number
+= 1;
485 /* updating the tdb_seqnum here avoids us reloading the cache
486 records due to our own modification */
487 ltdb
->tdb_seqnum
= tdb_get_seqnum(ltdb
->tdb
);
492 int ltdb_check_at_attributes_values(const struct ldb_val
*value
)
496 for (i
= 0; ltdb_valid_attr_flags
[i
].name
!= NULL
; i
++) {
497 if ((strcmp(ltdb_valid_attr_flags
[i
].name
, (char *)value
->data
) == 0)) {