s4-ldb: merged with master
[ira/wip.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         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         int i;
87         unsigned value = 0;
88         for (i=0;i<el->num_values;i++) {
89                 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         int i, r;
116
117         ldb = ldb_module_get_ctx(module);
118
119         if (ldb->schema.attribute_handler_override) {
120                 /* we skip loading the @ATTRIBUTES record when a module is supplying
121                    its own attribute handling */
122                 return LDB_SUCCESS;
123         }
124
125         dn = ldb_dn_new(module, ldb, LTDB_ATTRIBUTES);
126         if (dn == NULL) goto failed;
127
128         r = ltdb_search_dn1(module, dn, msg);
129         talloc_free(dn);
130         if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
131                 goto failed;
132         }
133         if (r == LDB_ERR_NO_SUCH_OBJECT) {
134                 return 0;
135         }
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++) {
139                 unsigned flags;
140                 const char *syntax;
141                 const struct ldb_schema_syntax *s;
142
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);
145                         goto failed;
146                 }
147                 switch (flags & ~LTDB_FLAG_HIDDEN) {
148                 case 0:
149                         syntax = LDB_SYNTAX_OCTET_STRING;
150                         break;
151                 case LTDB_FLAG_CASE_INSENSITIVE:
152                         syntax = LDB_SYNTAX_DIRECTORY_STRING;
153                         break;
154                 case LTDB_FLAG_INTEGER:
155                         syntax = LDB_SYNTAX_INTEGER;
156                         break;
157                 default:
158                         ldb_debug(ldb, LDB_DEBUG_ERROR, 
159                                   "Invalid flag combination 0x%x for '%s' in @ATTRIBUTES",
160                                   flags, msg->elements[i].name);
161                         goto failed;
162                 }
163
164                 s = ldb_standard_syntax_by_name(ldb, syntax);
165                 if (s == NULL) {
166                         ldb_debug(ldb, LDB_DEBUG_ERROR, 
167                                   "Invalid attribute syntax '%s' for '%s' in @ATTRIBUTES",
168                                   syntax, msg->elements[i].name);
169                         goto failed;
170                 }
171
172                 flags |= LDB_ATTR_FLAG_ALLOCATED;
173                 if (ldb_schema_attribute_add_with_syntax(ldb, msg->elements[i].name, flags, s) != 0) {
174                         goto failed;
175                 }
176         }
177
178         return 0;
179 failed:
180         return -1;
181 }
182
183
184 /*
185   initialise the baseinfo record
186 */
187 static int ltdb_baseinfo_init(struct ldb_module *module)
188 {
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         int ret;
194         /* the initial sequence number must be different from the one
195            set in ltdb_cache_free(). Thanks to Jon for pointing this
196            out. */
197         const char *initial_sequence_number = "1";
198
199         ldb = ldb_module_get_ctx(module);
200
201         ltdb->sequence_number = atof(initial_sequence_number);
202
203         msg = ldb_msg_new(ltdb);
204         msg->dn = ldb_dn_new(msg, ldb, LTDB_BASEINFO);
205         if (!msg->dn) {
206                 goto failed;
207         }
208
209         if (ldb_msg_add_string(msg, LTDB_SEQUENCE_NUMBER, initial_sequence_number) != 0) {
210                 goto failed;
211         }
212
213         if (ldb_msg_add_string(msg, LTDB_INDEX_VERSION, "1") != 0) {
214                 goto failed;
215         }
216
217         ret = ltdb_store(module, msg, msg, TDB_INSERT);
218
219         talloc_free(msg);
220
221         return ret;
222
223 failed:
224         talloc_free(msg);
225         errno = ENOMEM;
226         return LDB_ERR_OPERATIONS_ERROR;
227 }
228
229 /*
230   free any cache records
231  */
232 static void ltdb_cache_free(struct ldb_module *module)
233 {
234         void *data = ldb_module_get_private(module);
235         struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
236
237         ltdb->sequence_number = 0;
238         talloc_free(ltdb->cache);
239         ltdb->cache = NULL;
240 }
241
242 /*
243   force a cache reload
244 */
245 int ltdb_cache_reload(struct ldb_module *module)
246 {
247         ltdb_attributes_unload(module);
248         ltdb_cache_free(module);
249         return ltdb_cache_load(module);
250 }
251
252 /*
253   load the cache records
254 */
255 int ltdb_cache_load(struct ldb_module *module)
256 {
257         struct ldb_context *ldb;
258         void *data = ldb_module_get_private(module);
259         struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
260         struct ldb_dn *baseinfo_dn = NULL, *options_dn = NULL;
261         struct ldb_dn *indexlist_dn = NULL;
262         uint64_t seq;
263         struct ldb_message *baseinfo = NULL, *options = NULL;
264         int r;
265
266         ldb = ldb_module_get_ctx(module);
267
268         /* a very fast check to avoid extra database reads */
269         if (ltdb->cache != NULL && 
270             tdb_get_seqnum(ltdb->tdb) == ltdb->tdb_seqnum) {
271                 return 0;
272         }
273
274         if (ltdb->cache == NULL) {
275                 ltdb->cache = talloc_zero(ltdb, struct ltdb_cache);
276                 if (ltdb->cache == NULL) goto failed;
277                 ltdb->cache->indexlist = talloc_zero(ltdb->cache, struct ldb_message);
278                 ltdb->cache->attributes = talloc_zero(ltdb->cache, struct ldb_message);
279                 if (ltdb->cache->indexlist == NULL ||
280                     ltdb->cache->attributes == NULL) {
281                         goto failed;
282                 }
283         }
284
285         baseinfo = talloc(ltdb->cache, struct ldb_message);
286         if (baseinfo == NULL) goto failed;
287
288         baseinfo_dn = ldb_dn_new(module, ldb, LTDB_BASEINFO);
289         if (baseinfo_dn == NULL) goto failed;
290
291         r= ltdb_search_dn1(module, baseinfo_dn, baseinfo);
292         if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
293                 goto failed;
294         }
295         
296         /* possibly initialise the baseinfo */
297         if (r == LDB_ERR_NO_SUCH_OBJECT) {
298                 if (ltdb_baseinfo_init(module) != LDB_SUCCESS) {
299                         goto failed;
300                 }
301                 if (ltdb_search_dn1(module, baseinfo_dn, baseinfo) != LDB_SUCCESS) {
302                         goto failed;
303                 }
304         }
305
306         ltdb->tdb_seqnum = tdb_get_seqnum(ltdb->tdb);
307
308         /* if the current internal sequence number is the same as the one
309            in the database then assume the rest of the cache is OK */
310         seq = ldb_msg_find_attr_as_uint64(baseinfo, LTDB_SEQUENCE_NUMBER, 0);
311         if (seq == ltdb->sequence_number) {
312                 goto done;
313         }
314         ltdb->sequence_number = seq;
315
316         /* Determine what index format we are in (updated on reindex) */
317         ltdb->index_version = ldb_msg_find_attr_as_uint64(baseinfo, LTDB_INDEX_VERSION, 0);
318
319         if (ltdb->index_version > 1) {
320                 ldb_debug(ldb, LDB_DEBUG_ERROR, 
321                           "Invalid index version %d on database.  This ldb supports only index version 0 and 1",
322                           ltdb->index_version);
323                 goto failed;
324         }
325
326         /* Read an interpret database options */
327         options = talloc(ltdb->cache, struct ldb_message);
328         if (options == NULL) goto failed;
329
330         options_dn = ldb_dn_new(options, ldb, LTDB_OPTIONS);
331         if (options_dn == NULL) goto failed;
332
333         r= ltdb_search_dn1(module, options_dn, options);
334         if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
335                 goto failed;
336         }
337         
338         /* set flag for checking base DN on searches */
339         if (r == LDB_SUCCESS) {
340                 ltdb->check_base = ldb_msg_find_attr_as_bool(options, LTDB_CHECK_BASE, false);
341         } else {
342                 ltdb->check_base = false;
343         }
344
345         talloc_free(ltdb->cache->last_attribute.name);
346         memset(&ltdb->cache->last_attribute, 0, sizeof(ltdb->cache->last_attribute));
347
348         ltdb_attributes_unload(module);
349
350         talloc_free(ltdb->cache->indexlist);
351
352         ltdb->cache->indexlist = talloc_zero(ltdb->cache, struct ldb_message);
353         ltdb->cache->attributes = talloc_zero(ltdb->cache, struct ldb_message);
354         if (ltdb->cache->indexlist == NULL ||
355             ltdb->cache->attributes == NULL) {
356                 goto failed;
357         }
358             
359         indexlist_dn = ldb_dn_new(module, ldb, LTDB_INDEXLIST);
360         if (indexlist_dn == NULL) goto failed;
361
362         r = ltdb_search_dn1(module, indexlist_dn, ltdb->cache->indexlist);
363         if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
364                 goto failed;
365         }
366
367         if (ltdb_attributes_load(module) == -1) {
368                 goto failed;
369         }
370
371 done:
372         talloc_free(options);
373         talloc_free(baseinfo);
374         talloc_free(baseinfo_dn);
375         talloc_free(indexlist_dn);
376         return 0;
377
378 failed:
379         talloc_free(options);
380         talloc_free(baseinfo);
381         talloc_free(baseinfo_dn);
382         talloc_free(indexlist_dn);
383         return -1;
384 }
385
386
387 /*
388   increase the sequence number to indicate a database change
389 */
390 int ltdb_increase_sequence_number(struct ldb_module *module)
391 {
392         struct ldb_context *ldb;
393         void *data = ldb_module_get_private(module);
394         struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
395         struct ldb_message *msg;
396         struct ldb_message_element el[2];
397         struct ldb_val val;
398         struct ldb_val val_time;
399         time_t t = time(NULL);
400         char *s = NULL;
401         int ret;
402
403         ldb = ldb_module_get_ctx(module);
404
405         msg = talloc(ltdb, struct ldb_message);
406         if (msg == NULL) {
407                 errno = ENOMEM;
408                 return LDB_ERR_OPERATIONS_ERROR;
409         }
410
411         s = talloc_asprintf(msg, "%llu", ltdb->sequence_number+1);
412         if (!s) {
413                 errno = ENOMEM;
414                 return LDB_ERR_OPERATIONS_ERROR;
415         }
416
417         msg->num_elements = ARRAY_SIZE(el);
418         msg->elements = el;
419         msg->dn = ldb_dn_new(msg, ldb, LTDB_BASEINFO);
420         if (msg->dn == NULL) {
421                 talloc_free(msg);
422                 errno = ENOMEM;
423                 return LDB_ERR_OPERATIONS_ERROR;
424         }
425         el[0].name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
426         if (el[0].name == NULL) {
427                 talloc_free(msg);
428                 errno = ENOMEM;
429                 return LDB_ERR_OPERATIONS_ERROR;
430         }
431         el[0].values = &val;
432         el[0].num_values = 1;
433         el[0].flags = LDB_FLAG_MOD_REPLACE;
434         val.data = (uint8_t *)s;
435         val.length = strlen(s);
436
437         el[1].name = talloc_strdup(msg, LTDB_MOD_TIMESTAMP);
438         if (el[1].name == NULL) {
439                 talloc_free(msg);
440                 errno = ENOMEM;
441                 return LDB_ERR_OPERATIONS_ERROR;
442         }
443         el[1].values = &val_time;
444         el[1].num_values = 1;
445         el[1].flags = LDB_FLAG_MOD_REPLACE;
446
447         s = ldb_timestring(msg, t);
448         if (s == NULL) {
449                 talloc_free(msg);
450                 errno = ENOMEM;
451                 return LDB_ERR_OPERATIONS_ERROR;
452         }
453
454         val_time.data = (uint8_t *)s;
455         val_time.length = strlen(s);
456
457         ret = ltdb_modify_internal(module, msg, msg);
458
459         talloc_free(msg);
460
461         if (ret == LDB_SUCCESS) {
462                 ltdb->sequence_number += 1;
463         }
464
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);
468
469         return ret;
470 }
471
472 /*
473   increase the index version number to indicate a database change
474 */
475 int ltdb_set_casefold_index(struct ldb_module *module)
476 {
477         struct ldb_context *ldb;
478         void *data = ldb_module_get_private(module);
479         struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
480         struct ldb_message *msg;
481         struct ldb_message_element *el;
482
483         int ret;
484                 
485         ldb = ldb_module_get_ctx(module);
486
487         msg = ldb_msg_new(ltdb);
488         if (msg == NULL) {
489                 return LDB_ERR_OPERATIONS_ERROR;
490         }
491         msg->dn = ldb_dn_new(msg, ldb, LTDB_BASEINFO);
492         if (msg->dn == NULL) {
493                 talloc_free(msg);
494                 return LDB_ERR_OPERATIONS_ERROR;
495         }
496
497         if (ldb_msg_add_string(msg, LTDB_INDEX_VERSION, "1") != 0) {
498                 talloc_free(msg);
499                 return LDB_ERR_OPERATIONS_ERROR;
500         }
501
502         el = ldb_msg_find_element(msg, LTDB_INDEX_VERSION);
503         if (!el) {
504                 talloc_free(msg);
505                 return LDB_ERR_OPERATIONS_ERROR;
506         }
507         el->flags = LDB_FLAG_MOD_REPLACE;
508
509         ret = ltdb_modify_internal(module, msg, msg);
510
511         talloc_free(msg);
512
513         return ret;
514 }
515
516 int ltdb_check_at_attributes_values(const struct ldb_val *value)
517 {
518         int i;
519
520         for (i = 0; ltdb_valid_attr_flags[i].name != NULL; i++) {
521                 if ((strcmp(ltdb_valid_attr_flags[i].name, (char *)value->data) == 0)) {
522                         return 0;
523                 }
524         }
525
526         return -1;
527 }
528