9e81b61592f31bafaa9bb9599c6e29fe3529a426
[amitay/samba.git] / 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_UNIQUE_INDEX     (1<<2)
40
41 /* valid attribute flags */
42 static const struct {
43         const char *name;
44         int value;
45 } ldb_kv_valid_attr_flags[] = {
46         { "CASE_INSENSITIVE", LTDB_FLAG_CASE_INSENSITIVE },
47         { "INTEGER", LTDB_FLAG_INTEGER },
48         { "HIDDEN", 0 },
49         { "UNIQUE_INDEX",  LTDB_FLAG_UNIQUE_INDEX},
50         { "NONE", 0 },
51         { NULL, 0 }
52 };
53
54 /*
55   de-register any special handlers for @ATTRIBUTES
56 */
57 static void ldb_kv_attributes_unload(struct ldb_module *module)
58 {
59         struct ldb_context *ldb = ldb_module_get_ctx(module);
60
61         ldb_schema_attribute_remove_flagged(ldb, LDB_ATTR_FLAG_FROM_DB);
62
63 }
64
65 /*
66   add up the attrib flags for a @ATTRIBUTES element
67 */
68 static int ldb_kv_attributes_flags(struct ldb_message_element *el, unsigned *v)
69 {
70         unsigned int i;
71         unsigned value = 0;
72         for (i=0;i<el->num_values;i++) {
73                 unsigned int j;
74                 for (j = 0; ldb_kv_valid_attr_flags[j].name; j++) {
75                         if (strcmp(ldb_kv_valid_attr_flags[j].name,
76                                    (char *)el->values[i].data) == 0) {
77                                 value |= ldb_kv_valid_attr_flags[j].value;
78                                 break;
79                         }
80                 }
81                 if (ldb_kv_valid_attr_flags[j].name == NULL) {
82                         return -1;
83                 }
84         }
85         *v = value;
86         return 0;
87 }
88
89 static int ldb_schema_attribute_compare(const void *p1, const void *p2)
90 {
91         const struct ldb_schema_attribute *sa1 = (const struct ldb_schema_attribute *)p1;
92         const struct ldb_schema_attribute *sa2 = (const struct ldb_schema_attribute *)p2;
93         return ldb_attr_cmp(sa1->name, sa2->name);
94 }
95
96 /*
97   register any special handlers from @ATTRIBUTES
98 */
99 static int ldb_kv_attributes_load(struct ldb_module *module)
100 {
101         struct ldb_schema_attribute *attrs;
102         struct ldb_context *ldb;
103         struct ldb_message *attrs_msg = NULL;
104         struct ldb_dn *dn;
105         unsigned int i;
106         unsigned int num_loaded_attrs = 0;
107         int r;
108
109         ldb = ldb_module_get_ctx(module);
110
111         if (ldb->schema.attribute_handler_override) {
112                 /* we skip loading the @ATTRIBUTES record when a module is supplying
113                    its own attribute handling */
114                 return 0;
115         }
116
117         attrs_msg = ldb_msg_new(module);
118         if (attrs_msg == NULL) {
119                 goto failed;
120         }
121
122         dn = ldb_dn_new(module, ldb, LTDB_ATTRIBUTES);
123         if (dn == NULL) goto failed;
124
125         r = ldb_kv_search_dn1(module,
126                               dn,
127                               attrs_msg,
128                               LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
129                                   LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC |
130                                   LDB_UNPACK_DATA_FLAG_NO_DN);
131         talloc_free(dn);
132         if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
133                 goto failed;
134         }
135         if (r == LDB_ERR_NO_SUCH_OBJECT || attrs_msg->num_elements == 0) {
136                 TALLOC_FREE(attrs_msg);
137                 return 0;
138         }
139
140         attrs = talloc_array(attrs_msg,
141                              struct ldb_schema_attribute,
142                              attrs_msg->num_elements
143                              + ldb->schema.num_attributes);
144         if (attrs == NULL) {
145                 goto failed;
146         }
147
148         memcpy(attrs,
149                ldb->schema.attributes,
150                sizeof(ldb->schema.attributes[0]) * ldb->schema.num_attributes);
151
152         /* mapping these flags onto ldap 'syntaxes' isn't strictly correct,
153            but its close enough for now */
154         for (i=0;i<attrs_msg->num_elements;i++) {
155                 unsigned flags = 0, attr_flags = 0;
156                 const char *syntax;
157                 const struct ldb_schema_syntax *s;
158                 const struct ldb_schema_attribute *a =
159                         ldb_schema_attribute_by_name(ldb,
160                                                      attrs_msg->elements[i].name);
161                 if (a != NULL && a->flags & LDB_ATTR_FLAG_FIXED) {
162                         /* Must already be set in the array, and kept */
163                         continue;
164                 }
165
166                 if (ldb_kv_attributes_flags(&attrs_msg->elements[i], &flags) !=
167                     0) {
168                         ldb_debug(ldb, LDB_DEBUG_ERROR,
169                                   "Invalid @ATTRIBUTES element for '%s'",
170                                   attrs_msg->elements[i].name);
171                         goto failed;
172                 }
173
174                 if (flags & LTDB_FLAG_UNIQUE_INDEX) {
175                         attr_flags = LDB_ATTR_FLAG_UNIQUE_INDEX;
176                 }
177                 flags &= ~LTDB_FLAG_UNIQUE_INDEX;
178
179                 /* These are not currently flags, each is exclusive */
180                 if (flags == LTDB_FLAG_CASE_INSENSITIVE) {
181                         syntax = LDB_SYNTAX_DIRECTORY_STRING;
182                 } else if (flags == LTDB_FLAG_INTEGER) {
183                         syntax = LDB_SYNTAX_INTEGER;
184                 } else if (flags == 0) {
185                         syntax = LDB_SYNTAX_OCTET_STRING;
186                 } else {
187                         ldb_debug(ldb, LDB_DEBUG_ERROR, 
188                                   "Invalid flag combination 0x%x for '%s' "
189                                   "in @ATTRIBUTES",
190                                   flags, attrs_msg->elements[i].name);
191                         goto failed;
192                 }
193
194                 s = ldb_standard_syntax_by_name(ldb, syntax);
195                 if (s == NULL) {
196                         ldb_debug(ldb, LDB_DEBUG_ERROR, 
197                                   "Invalid attribute syntax '%s' for '%s' "
198                                   "in @ATTRIBUTES",
199                                   syntax, attrs_msg->elements[i].name);
200                         goto failed;
201                 }
202
203                 attr_flags |= LDB_ATTR_FLAG_ALLOCATED | LDB_ATTR_FLAG_FROM_DB;
204
205                 r = ldb_schema_attribute_fill_with_syntax(ldb,
206                                                           attrs,
207                                                           attrs_msg->elements[i].name,
208                                                           attr_flags, s,
209                                                           &attrs[num_loaded_attrs + ldb->schema.num_attributes]);
210                 if (r != 0) {
211                         goto failed;
212                 }
213                 num_loaded_attrs++;
214         }
215
216         attrs = talloc_realloc(attrs_msg,
217                                attrs, struct ldb_schema_attribute,
218                                num_loaded_attrs + ldb->schema.num_attributes);
219         if (attrs == NULL) {
220                 goto failed;
221         }
222         TYPESAFE_QSORT(attrs, num_loaded_attrs + ldb->schema.num_attributes,
223                        ldb_schema_attribute_compare);
224         talloc_unlink(ldb, ldb->schema.attributes);
225         ldb->schema.attributes = talloc_steal(ldb, attrs);
226         ldb->schema.num_attributes = num_loaded_attrs + ldb->schema.num_attributes;
227         TALLOC_FREE(attrs_msg);
228
229         return 0;
230 failed:
231         TALLOC_FREE(attrs_msg);
232         return -1;
233 }
234
235 /*
236   register any index records we find for the DB
237 */
238 static int ldb_kv_index_load(struct ldb_module *module,
239                              struct ldb_kv_private *ldb_kv)
240 {
241         struct ldb_context *ldb = ldb_module_get_ctx(module);
242         struct ldb_dn *indexlist_dn;
243         int r, lmdb_subdb_version;
244
245         if (ldb->schema.index_handler_override) {
246                 /*
247                  * we skip loading the @INDEXLIST record when a module is
248                  * supplying its own attribute handling
249                  */
250                 ldb_kv->cache->attribute_indexes = true;
251                 ldb_kv->cache->one_level_indexes =
252                     ldb->schema.one_level_indexes;
253                 ldb_kv->cache->GUID_index_attribute =
254                     ldb->schema.GUID_index_attribute;
255                 ldb_kv->cache->GUID_index_dn_component =
256                     ldb->schema.GUID_index_dn_component;
257                 return 0;
258         }
259
260         talloc_free(ldb_kv->cache->indexlist);
261
262         ldb_kv->cache->indexlist = ldb_msg_new(ldb_kv->cache);
263         if (ldb_kv->cache->indexlist == NULL) {
264                 return -1;
265         }
266         ldb_kv->cache->one_level_indexes = false;
267         ldb_kv->cache->attribute_indexes = false;
268
269         indexlist_dn = ldb_dn_new(ldb_kv, ldb, LTDB_INDEXLIST);
270         if (indexlist_dn == NULL) {
271                 return -1;
272         }
273
274         r = ldb_kv_search_dn1(module,
275                               indexlist_dn,
276                               ldb_kv->cache->indexlist,
277                               LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
278                                   LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC |
279                                   LDB_UNPACK_DATA_FLAG_NO_DN);
280         TALLOC_FREE(indexlist_dn);
281
282         if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
283                 return -1;
284         }
285
286         if (ldb_msg_find_element(ldb_kv->cache->indexlist, LTDB_IDXONE) !=
287             NULL) {
288                 ldb_kv->cache->one_level_indexes = true;
289         }
290         if (ldb_msg_find_element(ldb_kv->cache->indexlist, LTDB_IDXATTR) !=
291             NULL) {
292                 ldb_kv->cache->attribute_indexes = true;
293         }
294         ldb_kv->cache->GUID_index_attribute = ldb_msg_find_attr_as_string(
295             ldb_kv->cache->indexlist, LTDB_IDXGUID, NULL);
296         ldb_kv->cache->GUID_index_dn_component = ldb_msg_find_attr_as_string(
297             ldb_kv->cache->indexlist, LTDB_IDX_DN_GUID, NULL);
298
299         lmdb_subdb_version = ldb_msg_find_attr_as_int(
300             ldb_kv->cache->indexlist, LTDB_IDX_LMDB_SUBDB, 0);
301
302         if (lmdb_subdb_version != 0) {
303                 ldb_set_errstring(ldb,
304                                   "FATAL: This ldb_mdb database has "
305                                   "been written in a new verson of LDB "
306                                   "using a sub-database index that "
307                                   "is not understood by ldb "
308                                   LDB_VERSION);
309                 return -1;
310         }
311
312         return 0;
313 }
314
315 /*
316   initialise the baseinfo record
317 */
318 static int ldb_kv_baseinfo_init(struct ldb_module *module)
319 {
320         struct ldb_context *ldb;
321         void *data = ldb_module_get_private(module);
322         struct ldb_kv_private *ldb_kv =
323             talloc_get_type(data, struct ldb_kv_private);
324         struct ldb_message *msg;
325         struct ldb_message_element el;
326         struct ldb_val val;
327         int ret;
328         /* the initial sequence number must be different from the one
329            set in ltdb_cache_free(). Thanks to Jon for pointing this
330            out. */
331         const char *initial_sequence_number = "1";
332
333         ldb = ldb_module_get_ctx(module);
334
335         ldb_kv->sequence_number = atof(initial_sequence_number);
336
337         msg = ldb_msg_new(ldb_kv);
338         if (msg == NULL) {
339                 goto failed;
340         }
341
342         msg->num_elements = 1;
343         msg->elements = &el;
344         msg->dn = ldb_dn_new(msg, ldb, LTDB_BASEINFO);
345         if (!msg->dn) {
346                 goto failed;
347         }
348         el.name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
349         if (!el.name) {
350                 goto failed;
351         }
352         el.values = &val;
353         el.num_values = 1;
354         el.flags = 0;
355         val.data = (uint8_t *)talloc_strdup(msg, initial_sequence_number);
356         if (!val.data) {
357                 goto failed;
358         }
359         val.length = 1;
360
361         ret = ldb_kv_store(module, msg, TDB_INSERT);
362
363         talloc_free(msg);
364
365         return ret;
366
367 failed:
368         talloc_free(msg);
369         errno = ENOMEM;
370         return LDB_ERR_OPERATIONS_ERROR;
371 }
372
373 /*
374   free any cache records
375  */
376 static void ldb_kv_cache_free(struct ldb_module *module)
377 {
378         void *data = ldb_module_get_private(module);
379         struct ldb_kv_private *ldb_kv =
380             talloc_get_type(data, struct ldb_kv_private);
381
382         ldb_kv->sequence_number = 0;
383         talloc_free(ldb_kv->cache);
384         ldb_kv->cache = NULL;
385 }
386
387 /*
388   force a cache reload
389 */
390 int ldb_kv_cache_reload(struct ldb_module *module)
391 {
392         ldb_kv_attributes_unload(module);
393         ldb_kv_cache_free(module);
394         return ldb_kv_cache_load(module);
395 }
396
397 /*
398   load the cache records
399 */
400 int ldb_kv_cache_load(struct ldb_module *module)
401 {
402         struct ldb_context *ldb;
403         void *data = ldb_module_get_private(module);
404         struct ldb_kv_private *ldb_kv =
405             talloc_get_type(data, struct ldb_kv_private);
406         struct ldb_dn *baseinfo_dn = NULL, *options_dn = NULL;
407         uint64_t seq;
408         struct ldb_message *baseinfo = NULL, *options = NULL;
409         const struct ldb_schema_attribute *a;
410         bool have_write_txn = false;
411         int r;
412
413         ldb = ldb_module_get_ctx(module);
414
415         /* a very fast check to avoid extra database reads */
416         if (ldb_kv->cache != NULL && !ldb_kv->kv_ops->has_changed(ldb_kv)) {
417                 return 0;
418         }
419
420         if (ldb_kv->cache == NULL) {
421                 ldb_kv->cache = talloc_zero(ldb_kv, struct ldb_kv_cache);
422                 if (ldb_kv->cache == NULL)
423                         goto failed;
424         }
425
426         baseinfo = ldb_msg_new(ldb_kv->cache);
427         if (baseinfo == NULL) goto failed;
428
429         baseinfo_dn = ldb_dn_new(baseinfo, ldb, LTDB_BASEINFO);
430         if (baseinfo_dn == NULL) goto failed;
431
432         r = ldb_kv->kv_ops->lock_read(module);
433         if (r != LDB_SUCCESS) {
434                 goto failed;
435         }
436         r = ldb_kv_search_dn1(module, baseinfo_dn, baseinfo, 0);
437         if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
438                 goto failed_and_unlock;
439         }
440
441         /* possibly initialise the baseinfo */
442         if (r == LDB_ERR_NO_SUCH_OBJECT) {
443
444                 /* Give up the read lock, try again with a write lock */
445                 r = ldb_kv->kv_ops->unlock_read(module);
446                 if (r != LDB_SUCCESS) {
447                         goto failed;
448                 }
449
450                 if (ldb_kv->kv_ops->begin_write(ldb_kv) != 0) {
451                         goto failed;
452                 }
453
454                 have_write_txn = true;
455
456                 /* error handling for ltdb_baseinfo_init() is by
457                    looking for the record again. */
458                 ldb_kv_baseinfo_init(module);
459
460                 if (ldb_kv_search_dn1(module, baseinfo_dn, baseinfo, 0) !=
461                     LDB_SUCCESS) {
462                         goto failed_and_unlock;
463                 }
464
465         }
466
467         /* Ignore the result, and update the sequence number */
468         ldb_kv->kv_ops->has_changed(ldb_kv);
469
470         /* if the current internal sequence number is the same as the one
471            in the database then assume the rest of the cache is OK */
472         seq = ldb_msg_find_attr_as_uint64(baseinfo, LTDB_SEQUENCE_NUMBER, 0);
473         if (seq == ldb_kv->sequence_number) {
474                 goto done;
475         }
476         ldb_kv->sequence_number = seq;
477
478         /* Read an interpret database options */
479
480         options = ldb_msg_new(ldb_kv->cache);
481         if (options == NULL) goto failed_and_unlock;
482
483         options_dn = ldb_dn_new(options, ldb, LTDB_OPTIONS);
484         if (options_dn == NULL) goto failed_and_unlock;
485
486         r = ldb_kv_search_dn1(module, options_dn, options, 0);
487         talloc_free(options_dn);
488         if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
489                 goto failed_and_unlock;
490         }
491         
492         /* set flags if they do exist */
493         if (r == LDB_SUCCESS) {
494                 ldb_kv->check_base =
495                     ldb_msg_find_attr_as_bool(options, LTDB_CHECK_BASE, false);
496                 ldb_kv->disallow_dn_filter = ldb_msg_find_attr_as_bool(
497                     options, LTDB_DISALLOW_DN_FILTER, false);
498         } else {
499                 ldb_kv->check_base = false;
500                 ldb_kv->disallow_dn_filter = false;
501         }
502
503         /*
504          * ltdb_attributes_unload() calls internally talloc_free() on
505          * any non-fixed elemnts in ldb->schema.attributes.
506          *
507          * NOTE WELL: This is per-ldb, not per module, so overwrites
508          * the handlers across all databases when used under Samba's
509          * partition module.
510          */
511         ldb_kv_attributes_unload(module);
512
513         if (ldb_kv_index_load(module, ldb_kv) == -1) {
514                 goto failed_and_unlock;
515         }
516
517         /*
518          * NOTE WELL: This is per-ldb, not per module, so overwrites
519          * the handlers across all databases when used under Samba's
520          * partition module.
521          */
522         if (ldb_kv_attributes_load(module) == -1) {
523                 goto failed_and_unlock;
524         }
525
526         ldb_kv->GUID_index_syntax = NULL;
527         if (ldb_kv->cache->GUID_index_attribute != NULL) {
528                 /*
529                  * Now the attributes are loaded, set the guid_index_syntax.
530                  * This can't fail, it will return a default at worst
531                  */
532                 a = ldb_schema_attribute_by_name(
533                     ldb, ldb_kv->cache->GUID_index_attribute);
534                 ldb_kv->GUID_index_syntax = a->syntax;
535         }
536
537 done:
538         if (have_write_txn) {
539                 if (ldb_kv->kv_ops->finish_write(ldb_kv) != 0) {
540                         goto failed;
541                 }
542         } else {
543                 ldb_kv->kv_ops->unlock_read(module);
544         }
545
546         talloc_free(options);
547         talloc_free(baseinfo);
548         return 0;
549
550 failed_and_unlock:
551         if (have_write_txn) {
552                 ldb_kv->kv_ops->abort_write(ldb_kv);
553         } else {
554                 ldb_kv->kv_ops->unlock_read(module);
555         }
556
557 failed:
558         talloc_free(options);
559         talloc_free(baseinfo);
560         return -1;
561 }
562
563
564 /*
565   increase the sequence number to indicate a database change
566 */
567 int ldb_kv_increase_sequence_number(struct ldb_module *module)
568 {
569         struct ldb_context *ldb;
570         void *data = ldb_module_get_private(module);
571         struct ldb_kv_private *ldb_kv =
572             talloc_get_type(data, struct ldb_kv_private);
573         struct ldb_message *msg;
574         struct ldb_message_element el[2];
575         struct ldb_val val;
576         struct ldb_val val_time;
577         time_t t = time(NULL);
578         char *s = NULL;
579         int ret;
580
581         ldb = ldb_module_get_ctx(module);
582
583         msg = ldb_msg_new(ldb_kv);
584         if (msg == NULL) {
585                 errno = ENOMEM;
586                 return LDB_ERR_OPERATIONS_ERROR;
587         }
588
589         s = talloc_asprintf(msg, "%llu", ldb_kv->sequence_number + 1);
590         if (!s) {
591                 talloc_free(msg);
592                 errno = ENOMEM;
593                 return LDB_ERR_OPERATIONS_ERROR;
594         }
595
596         msg->num_elements = ARRAY_SIZE(el);
597         msg->elements = el;
598         msg->dn = ldb_dn_new(msg, ldb, LTDB_BASEINFO);
599         if (msg->dn == NULL) {
600                 talloc_free(msg);
601                 errno = ENOMEM;
602                 return LDB_ERR_OPERATIONS_ERROR;
603         }
604         el[0].name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
605         if (el[0].name == NULL) {
606                 talloc_free(msg);
607                 errno = ENOMEM;
608                 return LDB_ERR_OPERATIONS_ERROR;
609         }
610         el[0].values = &val;
611         el[0].num_values = 1;
612         el[0].flags = LDB_FLAG_MOD_REPLACE;
613         val.data = (uint8_t *)s;
614         val.length = strlen(s);
615
616         el[1].name = talloc_strdup(msg, LTDB_MOD_TIMESTAMP);
617         if (el[1].name == NULL) {
618                 talloc_free(msg);
619                 errno = ENOMEM;
620                 return LDB_ERR_OPERATIONS_ERROR;
621         }
622         el[1].values = &val_time;
623         el[1].num_values = 1;
624         el[1].flags = LDB_FLAG_MOD_REPLACE;
625
626         s = ldb_timestring(msg, t);
627         if (s == NULL) {
628                 talloc_free(msg);
629                 return LDB_ERR_OPERATIONS_ERROR;
630         }
631
632         val_time.data = (uint8_t *)s;
633         val_time.length = strlen(s);
634
635         ret = ldb_kv_modify_internal(module, msg, NULL);
636
637         talloc_free(msg);
638
639         if (ret == LDB_SUCCESS) {
640                 ldb_kv->sequence_number += 1;
641         }
642
643         /* updating the tdb_seqnum here avoids us reloading the cache
644            records due to our own modification */
645         ldb_kv->kv_ops->has_changed(ldb_kv);
646
647         return ret;
648 }
649
650 int ldb_kv_check_at_attributes_values(const struct ldb_val *value)
651 {
652         unsigned int i;
653
654         for (i = 0; ldb_kv_valid_attr_flags[i].name != NULL; i++) {
655                 if ((strcmp(ldb_kv_valid_attr_flags[i].name,
656                             (char *)value->data) == 0)) {
657                         return 0;
658                 }
659         }
660
661         return -1;
662 }
663