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
36 #define LTDB_FLAG_CASE_INSENSITIVE (1<<0)
37 #define LTDB_FLAG_INTEGER (1<<1)
38 #define LTDB_FLAG_HIDDEN (1<<2)
40 /* valid attribute flags */
44 } ltdb_valid_attr_flags
[] = {
45 { "CASE_INSENSITIVE", LTDB_FLAG_CASE_INSENSITIVE
},
46 { "INTEGER", LTDB_FLAG_INTEGER
},
47 { "HIDDEN", LTDB_FLAG_HIDDEN
},
54 de-register any special handlers for @ATTRIBUTES
56 static void ltdb_attributes_unload(struct ldb_module
*module
)
58 struct ldb_context
*ldb
;
59 void *data
= ldb_module_get_private(module
);
60 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
61 struct ldb_message
*msg
;
64 ldb
= ldb_module_get_ctx(module
);
66 if (ltdb
->cache
->attributes
== NULL
) {
67 /* no previously loaded attributes */
71 msg
= ltdb
->cache
->attributes
;
72 for (i
=0;i
<msg
->num_elements
;i
++) {
73 ldb_schema_attribute_remove(ldb
, msg
->elements
[i
].name
);
76 talloc_free(ltdb
->cache
->attributes
);
77 ltdb
->cache
->attributes
= NULL
;
81 add up the attrib flags for a @ATTRIBUTES element
83 static int ltdb_attributes_flags(struct ldb_message_element
*el
, unsigned *v
)
87 for (i
=0;i
<el
->num_values
;i
++) {
89 for (j
=0;ltdb_valid_attr_flags
[j
].name
;j
++) {
90 if (strcmp(ltdb_valid_attr_flags
[j
].name
,
91 (char *)el
->values
[i
].data
) == 0) {
92 value
|= ltdb_valid_attr_flags
[j
].value
;
96 if (ltdb_valid_attr_flags
[j
].name
== NULL
) {
105 register any special handlers from @ATTRIBUTES
107 static int ltdb_attributes_load(struct ldb_module
*module
)
109 struct ldb_context
*ldb
;
110 void *data
= ldb_module_get_private(module
);
111 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
112 struct ldb_message
*msg
= ltdb
->cache
->attributes
;
116 ldb
= ldb_module_get_ctx(module
);
118 dn
= ldb_dn_new(module
, ldb
, LTDB_ATTRIBUTES
);
119 if (dn
== NULL
) goto failed
;
121 r
= ltdb_search_dn1(module
, dn
, msg
);
123 if (r
!= LDB_SUCCESS
&& r
!= LDB_ERR_NO_SUCH_OBJECT
) {
126 if (r
== LDB_ERR_NO_SUCH_OBJECT
) {
129 /* mapping these flags onto ldap 'syntaxes' isn't strictly correct,
130 but its close enough for now */
131 for (i
=0;i
<msg
->num_elements
;i
++) {
134 const struct ldb_schema_syntax
*s
;
136 if (ltdb_attributes_flags(&msg
->elements
[i
], &flags
) != 0) {
137 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "Invalid @ATTRIBUTES element for '%s'\n", msg
->elements
[i
].name
);
140 switch (flags
& ~LTDB_FLAG_HIDDEN
) {
142 syntax
= LDB_SYNTAX_OCTET_STRING
;
144 case LTDB_FLAG_CASE_INSENSITIVE
:
145 syntax
= LDB_SYNTAX_DIRECTORY_STRING
;
147 case LTDB_FLAG_INTEGER
:
148 syntax
= LDB_SYNTAX_INTEGER
;
151 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
152 "Invalid flag combination 0x%x for '%s' in @ATTRIBUTES\n",
153 flags
, msg
->elements
[i
].name
);
157 s
= ldb_standard_syntax_by_name(ldb
, syntax
);
159 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
160 "Invalid attribute syntax '%s' for '%s' in @ATTRIBUTES\n",
161 syntax
, msg
->elements
[i
].name
);
165 flags
|= LDB_ATTR_FLAG_ALLOCATED
;
166 if (ldb_schema_attribute_add_with_syntax(ldb
, msg
->elements
[i
].name
, flags
, s
) != 0) {
178 initialise the baseinfo record
180 static int ltdb_baseinfo_init(struct ldb_module
*module
)
182 struct ldb_context
*ldb
;
183 void *data
= ldb_module_get_private(module
);
184 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
185 struct ldb_message
*msg
;
186 struct ldb_message_element el
;
189 /* the initial sequence number must be different from the one
190 set in ltdb_cache_free(). Thanks to Jon for pointing this
192 const char *initial_sequence_number
= "1";
194 ldb
= ldb_module_get_ctx(module
);
196 ltdb
->sequence_number
= atof(initial_sequence_number
);
198 msg
= talloc(ltdb
, struct ldb_message
);
203 msg
->num_elements
= 1;
205 msg
->dn
= ldb_dn_new(msg
, ldb
, LTDB_BASEINFO
);
209 el
.name
= talloc_strdup(msg
, LTDB_SEQUENCE_NUMBER
);
216 val
.data
= (uint8_t *)talloc_strdup(msg
, initial_sequence_number
);
222 ret
= ltdb_store(module
, msg
, TDB_INSERT
);
231 return LDB_ERR_OPERATIONS_ERROR
;
235 free any cache records
237 static void ltdb_cache_free(struct ldb_module
*module
)
239 void *data
= ldb_module_get_private(module
);
240 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
242 ltdb
->sequence_number
= 0;
243 talloc_free(ltdb
->cache
);
250 int ltdb_cache_reload(struct ldb_module
*module
)
252 ltdb_attributes_unload(module
);
253 ltdb_cache_free(module
);
254 return ltdb_cache_load(module
);
258 load the cache records
260 int ltdb_cache_load(struct ldb_module
*module
)
262 struct ldb_context
*ldb
;
263 void *data
= ldb_module_get_private(module
);
264 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
265 struct ldb_dn
*baseinfo_dn
= NULL
, *options_dn
= NULL
;
266 struct ldb_dn
*indexlist_dn
= NULL
;
268 struct ldb_message
*baseinfo
= NULL
, *options
= NULL
;
271 ldb
= ldb_module_get_ctx(module
);
273 /* a very fast check to avoid extra database reads */
274 if (ltdb
->cache
!= NULL
&&
275 tdb_get_seqnum(ltdb
->tdb
) == ltdb
->tdb_seqnum
) {
279 if (ltdb
->cache
== NULL
) {
280 ltdb
->cache
= talloc_zero(ltdb
, struct ltdb_cache
);
281 if (ltdb
->cache
== NULL
) goto failed
;
282 ltdb
->cache
->indexlist
= talloc_zero(ltdb
->cache
, struct ldb_message
);
283 ltdb
->cache
->attributes
= talloc_zero(ltdb
->cache
, struct ldb_message
);
284 if (ltdb
->cache
->indexlist
== NULL
||
285 ltdb
->cache
->attributes
== NULL
) {
290 baseinfo
= talloc(ltdb
->cache
, struct ldb_message
);
291 if (baseinfo
== NULL
) goto failed
;
293 baseinfo_dn
= ldb_dn_new(module
, ldb
, LTDB_BASEINFO
);
294 if (baseinfo_dn
== NULL
) goto failed
;
296 r
= ltdb_search_dn1(module
, baseinfo_dn
, baseinfo
);
297 if (r
!= LDB_SUCCESS
&& r
!= LDB_ERR_NO_SUCH_OBJECT
) {
301 /* possibly initialise the baseinfo */
302 if (r
== LDB_ERR_NO_SUCH_OBJECT
) {
303 if (ltdb_baseinfo_init(module
) != LDB_SUCCESS
) {
306 if (ltdb_search_dn1(module
, baseinfo_dn
, baseinfo
) != LDB_SUCCESS
) {
311 ltdb
->tdb_seqnum
= tdb_get_seqnum(ltdb
->tdb
);
313 /* if the current internal sequence number is the same as the one
314 in the database then assume the rest of the cache is OK */
315 seq
= ldb_msg_find_attr_as_uint64(baseinfo
, LTDB_SEQUENCE_NUMBER
, 0);
316 if (seq
== ltdb
->sequence_number
) {
319 ltdb
->sequence_number
= seq
;
321 /* Read an interpret database options */
322 options
= talloc(ltdb
->cache
, struct ldb_message
);
323 if (options
== NULL
) goto failed
;
325 options_dn
= ldb_dn_new(options
, ldb
, LTDB_OPTIONS
);
326 if (options_dn
== NULL
) goto failed
;
328 r
= ltdb_search_dn1(module
, options_dn
, options
);
329 if (r
!= LDB_SUCCESS
&& r
!= LDB_ERR_NO_SUCH_OBJECT
) {
333 /* set flag for checking base DN on searches */
334 if (r
== LDB_SUCCESS
) {
335 ltdb
->check_base
= ldb_msg_find_attr_as_bool(options
, LTDB_CHECK_BASE
, false);
337 ltdb
->check_base
= false;
340 talloc_free(ltdb
->cache
->last_attribute
.name
);
341 memset(<db
->cache
->last_attribute
, 0, sizeof(ltdb
->cache
->last_attribute
));
343 ltdb_attributes_unload(module
);
345 talloc_free(ltdb
->cache
->indexlist
);
347 ltdb
->cache
->indexlist
= talloc_zero(ltdb
->cache
, struct ldb_message
);
348 ltdb
->cache
->attributes
= talloc_zero(ltdb
->cache
, struct ldb_message
);
349 if (ltdb
->cache
->indexlist
== NULL
||
350 ltdb
->cache
->attributes
== NULL
) {
354 indexlist_dn
= ldb_dn_new(module
, ldb
, LTDB_INDEXLIST
);
355 if (indexlist_dn
== NULL
) goto failed
;
357 r
= ltdb_search_dn1(module
, indexlist_dn
, ltdb
->cache
->indexlist
);
358 if (r
!= LDB_SUCCESS
&& r
!= LDB_ERR_NO_SUCH_OBJECT
) {
362 if (ltdb_attributes_load(module
) == -1) {
367 talloc_free(options
);
368 talloc_free(baseinfo
);
369 talloc_free(baseinfo_dn
);
370 talloc_free(indexlist_dn
);
374 talloc_free(options
);
375 talloc_free(baseinfo
);
376 talloc_free(baseinfo_dn
);
377 talloc_free(indexlist_dn
);
383 increase the sequence number to indicate a database change
385 int ltdb_increase_sequence_number(struct ldb_module
*module
)
387 struct ldb_context
*ldb
;
388 void *data
= ldb_module_get_private(module
);
389 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
390 struct ldb_message
*msg
;
391 struct ldb_message_element el
[2];
393 struct ldb_val val_time
;
394 time_t t
= time(NULL
);
398 ldb
= ldb_module_get_ctx(module
);
400 msg
= talloc(ltdb
, struct ldb_message
);
403 return LDB_ERR_OPERATIONS_ERROR
;
406 s
= talloc_asprintf(msg
, "%llu", ltdb
->sequence_number
+1);
409 return LDB_ERR_OPERATIONS_ERROR
;
412 msg
->num_elements
= ARRAY_SIZE(el
);
414 msg
->dn
= ldb_dn_new(msg
, ldb
, LTDB_BASEINFO
);
415 if (msg
->dn
== NULL
) {
418 return LDB_ERR_OPERATIONS_ERROR
;
420 el
[0].name
= talloc_strdup(msg
, LTDB_SEQUENCE_NUMBER
);
421 if (el
[0].name
== NULL
) {
424 return LDB_ERR_OPERATIONS_ERROR
;
427 el
[0].num_values
= 1;
428 el
[0].flags
= LDB_FLAG_MOD_REPLACE
;
429 val
.data
= (uint8_t *)s
;
430 val
.length
= strlen(s
);
432 el
[1].name
= talloc_strdup(msg
, LTDB_MOD_TIMESTAMP
);
433 if (el
[1].name
== NULL
) {
436 return LDB_ERR_OPERATIONS_ERROR
;
438 el
[1].values
= &val_time
;
439 el
[1].num_values
= 1;
440 el
[1].flags
= LDB_FLAG_MOD_REPLACE
;
442 s
= ldb_timestring(msg
, t
);
444 return LDB_ERR_OPERATIONS_ERROR
;
447 val_time
.data
= (uint8_t *)s
;
448 val_time
.length
= strlen(s
);
450 ret
= ltdb_modify_internal(module
, msg
);
454 if (ret
== LDB_SUCCESS
) {
455 ltdb
->sequence_number
+= 1;
458 /* updating the tdb_seqnum here avoids us reloading the cache
459 records due to our own modification */
460 ltdb
->tdb_seqnum
= tdb_get_seqnum(ltdb
->tdb
);
465 int ltdb_check_at_attributes_values(const struct ldb_val
*value
)
469 for (i
= 0; ltdb_valid_attr_flags
[i
].name
!= NULL
; i
++) {
470 if ((strcmp(ltdb_valid_attr_flags
[i
].name
, (char *)value
->data
) == 0)) {