ldb:ldb_tdb submodule - use "ldb_msg_new" where appropriate
[abartlet/samba.git/.git] / source4 / lib / ldb / ldb_tdb / ldb_cache.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004
5
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
9    
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.
14
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.
19
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/>.
22 */
23
24 /*
25  *  Name: ldb
26  *
27  *  Component: ldb tdb cache functions
28  *
29  *  Description: cache special records in a ldb/tdb
30  *
31  *  Author: Andrew Tridgell
32  */
33
34 #include "ldb_tdb.h"
35 #include "ldb_private.h"
36
37 #define LTDB_FLAG_CASE_INSENSITIVE (1<<0)
38 #define LTDB_FLAG_INTEGER          (1<<1)
39 #define LTDB_FLAG_HIDDEN           (1<<2)
40
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 }
51 };
52
53
54 /*
55   de-register any special handlers for @ATTRIBUTES
56 */
57 static void ltdb_attributes_unload(struct ldb_module *module)
58 {
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;
63         unsigned int i;
64
65         ldb = ldb_module_get_ctx(module);
66
67         if (ltdb->cache->attributes == NULL) {
68                 /* no previously loaded attributes */
69                 return;
70         }
71
72         msg = ltdb->cache->attributes;
73         for (i=0;i<msg->num_elements;i++) {
74                 ldb_schema_attribute_remove(ldb, msg->elements[i].name);
75         }
76
77         talloc_free(ltdb->cache->attributes);
78         ltdb->cache->attributes = NULL;
79 }
80
81 /*
82   add up the attrib flags for a @ATTRIBUTES element
83 */
84 static int ltdb_attributes_flags(struct ldb_message_element *el, unsigned *v)
85 {
86         unsigned int i;
87         unsigned value = 0;
88         for (i=0;i<el->num_values;i++) {
89                 unsigned int j;
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;
94                                 break;
95                         }
96                 }
97                 if (ltdb_valid_attr_flags[j].name == NULL) {
98                         return -1;
99                 }
100         }
101         *v = value;
102         return 0;
103 }
104
105 /*
106   register any special handlers from @ATTRIBUTES
107 */
108 static int ltdb_attributes_load(struct ldb_module *module)
109 {
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;
114         struct ldb_dn *dn;
115         unsigned int i;
116         int r;
117
118         ldb = ldb_module_get_ctx(module);
119
120         if (ldb->schema.attribute_handler_override) {
121                 /* we skip loading the @ATTRIBUTES record when a module is supplying
122                    its own attribute handling */
123                 return LDB_SUCCESS;
124         }
125
126         dn = ldb_dn_new(module, ldb, LTDB_ATTRIBUTES);
127         if (dn == NULL) goto failed;
128
129         r = ltdb_search_dn1(module, dn, msg);
130         talloc_free(dn);
131         if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
132                 goto failed;
133         }
134         if (r == LDB_ERR_NO_SUCH_OBJECT) {
135                 return 0;
136         }
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++) {
140                 unsigned flags;
141                 const char *syntax;
142                 const struct ldb_schema_syntax *s;
143
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);
146                         goto failed;
147                 }
148                 switch (flags & ~LTDB_FLAG_HIDDEN) {
149                 case 0:
150                         syntax = LDB_SYNTAX_OCTET_STRING;
151                         break;
152                 case LTDB_FLAG_CASE_INSENSITIVE:
153                         syntax = LDB_SYNTAX_DIRECTORY_STRING;
154                         break;
155                 case LTDB_FLAG_INTEGER:
156                         syntax = LDB_SYNTAX_INTEGER;
157                         break;
158                 default:
159                         ldb_debug(ldb, LDB_DEBUG_ERROR, 
160                                   "Invalid flag combination 0x%x for '%s' in @ATTRIBUTES",
161                                   flags, msg->elements[i].name);
162                         goto failed;
163                 }
164
165                 s = ldb_standard_syntax_by_name(ldb, syntax);
166                 if (s == NULL) {
167                         ldb_debug(ldb, LDB_DEBUG_ERROR, 
168                                   "Invalid attribute syntax '%s' for '%s' in @ATTRIBUTES",
169                                   syntax, msg->elements[i].name);
170                         goto failed;
171                 }
172
173                 flags |= LDB_ATTR_FLAG_ALLOCATED;
174                 if (ldb_schema_attribute_add_with_syntax(ldb, msg->elements[i].name, flags, s) != 0) {
175                         goto failed;
176                 }
177         }
178
179         return 0;
180 failed:
181         return -1;
182 }
183
184
185 /*
186   initialise the baseinfo record
187 */
188 static int ltdb_baseinfo_init(struct ldb_module *module)
189 {
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;
195         struct ldb_val val;
196         int ret;
197         /* the initial sequence number must be different from the one
198            set in ltdb_cache_free(). Thanks to Jon for pointing this
199            out. */
200         const char *initial_sequence_number = "1";
201
202         ldb = ldb_module_get_ctx(module);
203
204         ltdb->sequence_number = atof(initial_sequence_number);
205
206         msg = ldb_msg_new(ltdb);
207         if (msg == NULL) {
208                 goto failed;
209         }
210
211         msg->num_elements = 1;
212         msg->elements = &el;
213         msg->dn = ldb_dn_new(msg, ldb, LTDB_BASEINFO);
214         if (!msg->dn) {
215                 goto failed;
216         }
217         el.name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
218         if (!el.name) {
219                 goto failed;
220         }
221         el.values = &val;
222         el.num_values = 1;
223         el.flags = 0;
224         val.data = (uint8_t *)talloc_strdup(msg, initial_sequence_number);
225         if (!val.data) {
226                 goto failed;
227         }
228         val.length = 1;
229         
230         ret = ltdb_store(module, msg, TDB_INSERT);
231
232         talloc_free(msg);
233
234         return ret;
235
236 failed:
237         talloc_free(msg);
238         errno = ENOMEM;
239         return LDB_ERR_OPERATIONS_ERROR;
240 }
241
242 /*
243   free any cache records
244  */
245 static void ltdb_cache_free(struct ldb_module *module)
246 {
247         void *data = ldb_module_get_private(module);
248         struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
249
250         ltdb->sequence_number = 0;
251         talloc_free(ltdb->cache);
252         ltdb->cache = NULL;
253 }
254
255 /*
256   force a cache reload
257 */
258 int ltdb_cache_reload(struct ldb_module *module)
259 {
260         ltdb_attributes_unload(module);
261         ltdb_cache_free(module);
262         return ltdb_cache_load(module);
263 }
264
265 /*
266   load the cache records
267 */
268 int ltdb_cache_load(struct ldb_module *module)
269 {
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;
275         uint64_t seq;
276         struct ldb_message *baseinfo = NULL, *options = NULL;
277         int r;
278
279         ldb = ldb_module_get_ctx(module);
280
281         /* a very fast check to avoid extra database reads */
282         if (ltdb->cache != NULL && 
283             tdb_get_seqnum(ltdb->tdb) == ltdb->tdb_seqnum) {
284                 return 0;
285         }
286
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) {
294                         goto failed;
295                 }
296         }
297
298         baseinfo = ldb_msg_new(ltdb->cache);
299         if (baseinfo == NULL) goto failed;
300
301         baseinfo_dn = ldb_dn_new(module, ldb, LTDB_BASEINFO);
302         if (baseinfo_dn == NULL) goto failed;
303
304         r= ltdb_search_dn1(module, baseinfo_dn, baseinfo);
305         if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
306                 goto failed;
307         }
308         
309         /* possibly initialise the baseinfo */
310         if (r == LDB_ERR_NO_SUCH_OBJECT) {
311                 if (ltdb_baseinfo_init(module) != LDB_SUCCESS) {
312                         goto failed;
313                 }
314                 if (ltdb_search_dn1(module, baseinfo_dn, baseinfo) != LDB_SUCCESS) {
315                         goto failed;
316                 }
317         }
318
319         ltdb->tdb_seqnum = tdb_get_seqnum(ltdb->tdb);
320
321         /* if the current internal sequence number is the same as the one
322            in the database then assume the rest of the cache is OK */
323         seq = ldb_msg_find_attr_as_uint64(baseinfo, LTDB_SEQUENCE_NUMBER, 0);
324         if (seq == ltdb->sequence_number) {
325                 goto done;
326         }
327         ltdb->sequence_number = seq;
328
329         /* Read an interpret database options */
330         options = ldb_msg_new(ltdb->cache);
331         if (options == NULL) goto failed;
332
333         options_dn = ldb_dn_new(options, ldb, LTDB_OPTIONS);
334         if (options_dn == NULL) goto failed;
335
336         r= ltdb_search_dn1(module, options_dn, options);
337         if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
338                 goto failed;
339         }
340         
341         /* set flag for checking base DN on searches */
342         if (r == LDB_SUCCESS) {
343                 ltdb->check_base = ldb_msg_find_attr_as_bool(options, LTDB_CHECK_BASE, false);
344         } else {
345                 ltdb->check_base = false;
346         }
347
348         talloc_free(ltdb->cache->last_attribute.name);
349         memset(&ltdb->cache->last_attribute, 0, sizeof(ltdb->cache->last_attribute));
350
351         ltdb_attributes_unload(module);
352
353         talloc_free(ltdb->cache->indexlist);
354
355         ltdb->cache->indexlist = ldb_msg_new(ltdb->cache);
356         ltdb->cache->attributes = ldb_msg_new(ltdb->cache);
357         if (ltdb->cache->indexlist == NULL ||
358             ltdb->cache->attributes == NULL) {
359                 goto failed;
360         }
361         ltdb->cache->one_level_indexes = false;
362         ltdb->cache->attribute_indexes = false;
363             
364         indexlist_dn = ldb_dn_new(module, ldb, LTDB_INDEXLIST);
365         if (indexlist_dn == NULL) goto failed;
366
367         r = ltdb_search_dn1(module, indexlist_dn, ltdb->cache->indexlist);
368         if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
369                 goto failed;
370         }
371
372         if (ldb_msg_find_element(ltdb->cache->indexlist, LTDB_IDXONE) != NULL) {
373                 ltdb->cache->one_level_indexes = true;
374         }
375         if (ldb_msg_find_element(ltdb->cache->indexlist, LTDB_IDXATTR) != NULL) {
376                 ltdb->cache->attribute_indexes = true;
377         }
378
379         if (ltdb_attributes_load(module) == -1) {
380                 goto failed;
381         }
382
383 done:
384         talloc_free(options);
385         talloc_free(baseinfo);
386         talloc_free(baseinfo_dn);
387         talloc_free(indexlist_dn);
388         return 0;
389
390 failed:
391         talloc_free(options);
392         talloc_free(baseinfo);
393         talloc_free(baseinfo_dn);
394         talloc_free(indexlist_dn);
395         return -1;
396 }
397
398
399 /*
400   increase the sequence number to indicate a database change
401 */
402 int ltdb_increase_sequence_number(struct ldb_module *module)
403 {
404         struct ldb_context *ldb;
405         void *data = ldb_module_get_private(module);
406         struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
407         struct ldb_message *msg;
408         struct ldb_message_element el[2];
409         struct ldb_val val;
410         struct ldb_val val_time;
411         time_t t = time(NULL);
412         char *s = NULL;
413         int ret;
414
415         ldb = ldb_module_get_ctx(module);
416
417         msg = ldb_msg_new(ltdb);
418         if (msg == NULL) {
419                 errno = ENOMEM;
420                 return LDB_ERR_OPERATIONS_ERROR;
421         }
422
423         s = talloc_asprintf(msg, "%llu", ltdb->sequence_number+1);
424         if (!s) {
425                 errno = ENOMEM;
426                 return LDB_ERR_OPERATIONS_ERROR;
427         }
428
429         msg->num_elements = ARRAY_SIZE(el);
430         msg->elements = el;
431         msg->dn = ldb_dn_new(msg, ldb, LTDB_BASEINFO);
432         if (msg->dn == NULL) {
433                 talloc_free(msg);
434                 errno = ENOMEM;
435                 return LDB_ERR_OPERATIONS_ERROR;
436         }
437         el[0].name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
438         if (el[0].name == NULL) {
439                 talloc_free(msg);
440                 errno = ENOMEM;
441                 return LDB_ERR_OPERATIONS_ERROR;
442         }
443         el[0].values = &val;
444         el[0].num_values = 1;
445         el[0].flags = LDB_FLAG_MOD_REPLACE;
446         val.data = (uint8_t *)s;
447         val.length = strlen(s);
448
449         el[1].name = talloc_strdup(msg, LTDB_MOD_TIMESTAMP);
450         if (el[1].name == NULL) {
451                 talloc_free(msg);
452                 errno = ENOMEM;
453                 return LDB_ERR_OPERATIONS_ERROR;
454         }
455         el[1].values = &val_time;
456         el[1].num_values = 1;
457         el[1].flags = LDB_FLAG_MOD_REPLACE;
458
459         s = ldb_timestring(msg, t);
460         if (s == NULL) {
461                 return LDB_ERR_OPERATIONS_ERROR;
462         }
463
464         val_time.data = (uint8_t *)s;
465         val_time.length = strlen(s);
466
467         ret = ltdb_modify_internal(module, msg, NULL);
468
469         talloc_free(msg);
470
471         if (ret == LDB_SUCCESS) {
472                 ltdb->sequence_number += 1;
473         }
474
475         /* updating the tdb_seqnum here avoids us reloading the cache
476            records due to our own modification */
477         ltdb->tdb_seqnum = tdb_get_seqnum(ltdb->tdb);
478
479         return ret;
480 }
481
482 int ltdb_check_at_attributes_values(const struct ldb_val *value)
483 {
484         unsigned int i;
485
486         for (i = 0; ltdb_valid_attr_flags[i].name != NULL; i++) {
487                 if ((strcmp(ltdb_valid_attr_flags[i].name, (char *)value->data) == 0)) {
488                         return 0;
489                 }
490         }
491
492         return -1;
493 }
494