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
;
117 ldb
= ldb_module_get_ctx(module
);
119 if (ldb
->schema
.attribute_handler_override
) {
120 /* we skip loading the @ATTRIBUTES record when a module is supplying
121 its own attribute handling */
125 dn
= ldb_dn_new(module
, ldb
, LTDB_ATTRIBUTES
);
126 if (dn
== NULL
) goto failed
;
128 r
= ltdb_search_dn1(module
, dn
, msg
);
130 if (r
!= LDB_SUCCESS
&& r
!= LDB_ERR_NO_SUCH_OBJECT
) {
133 if (r
== LDB_ERR_NO_SUCH_OBJECT
) {
136 /* mapping these flags onto ldap 'syntaxes' isn't strictly correct,
137 but its close enough for now */
138 for (i
=0;i
<msg
->num_elements
;i
++) {
141 const struct ldb_schema_syntax
*s
;
143 if (ltdb_attributes_flags(&msg
->elements
[i
], &flags
) != 0) {
144 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "Invalid @ATTRIBUTES element for '%s'", msg
->elements
[i
].name
);
147 switch (flags
& ~LTDB_FLAG_HIDDEN
) {
149 syntax
= LDB_SYNTAX_OCTET_STRING
;
151 case LTDB_FLAG_CASE_INSENSITIVE
:
152 syntax
= LDB_SYNTAX_DIRECTORY_STRING
;
154 case LTDB_FLAG_INTEGER
:
155 syntax
= LDB_SYNTAX_INTEGER
;
158 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
159 "Invalid flag combination 0x%x for '%s' in @ATTRIBUTES",
160 flags
, msg
->elements
[i
].name
);
164 s
= ldb_standard_syntax_by_name(ldb
, syntax
);
166 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
167 "Invalid attribute syntax '%s' for '%s' in @ATTRIBUTES",
168 syntax
, msg
->elements
[i
].name
);
172 flags
|= LDB_ATTR_FLAG_ALLOCATED
;
173 if (ldb_schema_attribute_add_with_syntax(ldb
, msg
->elements
[i
].name
, flags
, s
) != 0) {
185 initialise the baseinfo record
187 static int ltdb_baseinfo_init(struct ldb_module
*module
)
189 struct ldb_context
*ldb
;
190 void *data
= ldb_module_get_private(module
);
191 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
192 struct ldb_message
*msg
;
193 struct ldb_message_element el
;
196 /* the initial sequence number must be different from the one
197 set in ltdb_cache_free(). Thanks to Jon for pointing this
199 const char *initial_sequence_number
= "1";
201 ldb
= ldb_module_get_ctx(module
);
203 ltdb
->sequence_number
= atof(initial_sequence_number
);
205 msg
= talloc(ltdb
, struct ldb_message
);
210 msg
->num_elements
= 1;
212 msg
->dn
= ldb_dn_new(msg
, ldb
, LTDB_BASEINFO
);
216 el
.name
= talloc_strdup(msg
, LTDB_SEQUENCE_NUMBER
);
223 val
.data
= (uint8_t *)talloc_strdup(msg
, initial_sequence_number
);
229 ret
= ltdb_store(module
, msg
, TDB_INSERT
);
238 return LDB_ERR_OPERATIONS_ERROR
;
242 free any cache records
244 static void ltdb_cache_free(struct ldb_module
*module
)
246 void *data
= ldb_module_get_private(module
);
247 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
249 ltdb
->sequence_number
= 0;
250 talloc_free(ltdb
->cache
);
257 int ltdb_cache_reload(struct ldb_module
*module
)
259 ltdb_attributes_unload(module
);
260 ltdb_cache_free(module
);
261 return ltdb_cache_load(module
);
265 load the cache records
267 int ltdb_cache_load(struct ldb_module
*module
)
269 struct ldb_context
*ldb
;
270 void *data
= ldb_module_get_private(module
);
271 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
272 struct ldb_dn
*baseinfo_dn
= NULL
, *options_dn
= NULL
;
273 struct ldb_dn
*indexlist_dn
= NULL
;
275 struct ldb_message
*baseinfo
= NULL
, *options
= NULL
;
278 ldb
= ldb_module_get_ctx(module
);
280 /* a very fast check to avoid extra database reads */
281 if (ltdb
->cache
!= NULL
&&
282 tdb_get_seqnum(ltdb
->tdb
) == ltdb
->tdb_seqnum
) {
286 if (ltdb
->cache
== NULL
) {
287 ltdb
->cache
= talloc_zero(ltdb
, struct ltdb_cache
);
288 if (ltdb
->cache
== NULL
) goto failed
;
289 ltdb
->cache
->indexlist
= talloc_zero(ltdb
->cache
, struct ldb_message
);
290 ltdb
->cache
->attributes
= talloc_zero(ltdb
->cache
, struct ldb_message
);
291 if (ltdb
->cache
->indexlist
== NULL
||
292 ltdb
->cache
->attributes
== NULL
) {
297 baseinfo
= talloc(ltdb
->cache
, struct ldb_message
);
298 if (baseinfo
== NULL
) goto failed
;
300 baseinfo_dn
= ldb_dn_new(module
, ldb
, LTDB_BASEINFO
);
301 if (baseinfo_dn
== NULL
) goto failed
;
303 r
= ltdb_search_dn1(module
, baseinfo_dn
, baseinfo
);
304 if (r
!= LDB_SUCCESS
&& r
!= LDB_ERR_NO_SUCH_OBJECT
) {
308 /* possibly initialise the baseinfo */
309 if (r
== LDB_ERR_NO_SUCH_OBJECT
) {
310 if (ltdb_baseinfo_init(module
) != LDB_SUCCESS
) {
313 if (ltdb_search_dn1(module
, baseinfo_dn
, baseinfo
) != LDB_SUCCESS
) {
318 ltdb
->tdb_seqnum
= tdb_get_seqnum(ltdb
->tdb
);
320 /* if the current internal sequence number is the same as the one
321 in the database then assume the rest of the cache is OK */
322 seq
= ldb_msg_find_attr_as_uint64(baseinfo
, LTDB_SEQUENCE_NUMBER
, 0);
323 if (seq
== ltdb
->sequence_number
) {
326 ltdb
->sequence_number
= seq
;
328 /* Read an interpret database options */
329 options
= talloc(ltdb
->cache
, struct ldb_message
);
330 if (options
== NULL
) goto failed
;
332 options_dn
= ldb_dn_new(options
, ldb
, LTDB_OPTIONS
);
333 if (options_dn
== NULL
) goto failed
;
335 r
= ltdb_search_dn1(module
, options_dn
, options
);
336 if (r
!= LDB_SUCCESS
&& r
!= LDB_ERR_NO_SUCH_OBJECT
) {
340 /* set flag for checking base DN on searches */
341 if (r
== LDB_SUCCESS
) {
342 ltdb
->check_base
= ldb_msg_find_attr_as_bool(options
, LTDB_CHECK_BASE
, false);
344 ltdb
->check_base
= false;
347 talloc_free(ltdb
->cache
->last_attribute
.name
);
348 memset(<db
->cache
->last_attribute
, 0, sizeof(ltdb
->cache
->last_attribute
));
350 ltdb_attributes_unload(module
);
352 talloc_free(ltdb
->cache
->indexlist
);
354 ltdb
->cache
->indexlist
= talloc_zero(ltdb
->cache
, struct ldb_message
);
355 ltdb
->cache
->attributes
= talloc_zero(ltdb
->cache
, struct ldb_message
);
356 if (ltdb
->cache
->indexlist
== NULL
||
357 ltdb
->cache
->attributes
== NULL
) {
361 indexlist_dn
= ldb_dn_new(module
, ldb
, LTDB_INDEXLIST
);
362 if (indexlist_dn
== NULL
) goto failed
;
364 r
= ltdb_search_dn1(module
, indexlist_dn
, ltdb
->cache
->indexlist
);
365 if (r
!= LDB_SUCCESS
&& r
!= LDB_ERR_NO_SUCH_OBJECT
) {
369 if (ltdb_attributes_load(module
) == -1) {
374 talloc_free(options
);
375 talloc_free(baseinfo
);
376 talloc_free(baseinfo_dn
);
377 talloc_free(indexlist_dn
);
381 talloc_free(options
);
382 talloc_free(baseinfo
);
383 talloc_free(baseinfo_dn
);
384 talloc_free(indexlist_dn
);
390 increase the sequence number to indicate a database change
392 int ltdb_increase_sequence_number(struct ldb_module
*module
)
394 struct ldb_context
*ldb
;
395 void *data
= ldb_module_get_private(module
);
396 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
397 struct ldb_message
*msg
;
398 struct ldb_message_element el
[2];
400 struct ldb_val val_time
;
401 time_t t
= time(NULL
);
405 ldb
= ldb_module_get_ctx(module
);
407 msg
= talloc(ltdb
, struct ldb_message
);
410 return LDB_ERR_OPERATIONS_ERROR
;
413 s
= talloc_asprintf(msg
, "%llu", ltdb
->sequence_number
+1);
416 return LDB_ERR_OPERATIONS_ERROR
;
419 msg
->num_elements
= ARRAY_SIZE(el
);
421 msg
->dn
= ldb_dn_new(msg
, ldb
, LTDB_BASEINFO
);
422 if (msg
->dn
== NULL
) {
425 return LDB_ERR_OPERATIONS_ERROR
;
427 el
[0].name
= talloc_strdup(msg
, LTDB_SEQUENCE_NUMBER
);
428 if (el
[0].name
== NULL
) {
431 return LDB_ERR_OPERATIONS_ERROR
;
434 el
[0].num_values
= 1;
435 el
[0].flags
= LDB_FLAG_MOD_REPLACE
;
436 val
.data
= (uint8_t *)s
;
437 val
.length
= strlen(s
);
439 el
[1].name
= talloc_strdup(msg
, LTDB_MOD_TIMESTAMP
);
440 if (el
[1].name
== NULL
) {
443 return LDB_ERR_OPERATIONS_ERROR
;
445 el
[1].values
= &val_time
;
446 el
[1].num_values
= 1;
447 el
[1].flags
= LDB_FLAG_MOD_REPLACE
;
449 s
= ldb_timestring(msg
, t
);
451 return LDB_ERR_OPERATIONS_ERROR
;
454 val_time
.data
= (uint8_t *)s
;
455 val_time
.length
= strlen(s
);
457 ret
= ltdb_modify_internal(module
, msg
);
461 if (ret
== LDB_SUCCESS
) {
462 ltdb
->sequence_number
+= 1;
465 /* updating the tdb_seqnum here avoids us reloading the cache
466 records due to our own modification */
467 ltdb
->tdb_seqnum
= tdb_get_seqnum(ltdb
->tdb
);
472 int ltdb_check_at_attributes_values(const struct ldb_val
*value
)
476 for (i
= 0; ltdb_valid_attr_flags
[i
].name
!= NULL
; i
++) {
477 if ((strcmp(ltdb_valid_attr_flags
[i
].name
, (char *)value
->data
) == 0)) {