2576e2c7bdee41932174322d2f346498db16664f
[metze/samba/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_includes.h"
35
36 #include "ldb_tdb.h"
37
38 #define LTDB_FLAG_CASE_INSENSITIVE (1<<0)
39 #define LTDB_FLAG_INTEGER          (1<<1)
40 #define LTDB_FLAG_HIDDEN           (1<<2)
41
42 /* valid attribute flags */
43 static const struct {
44         const char *name;
45         int value;
46 } ltdb_valid_attr_flags[] = {
47         { "CASE_INSENSITIVE", LTDB_FLAG_CASE_INSENSITIVE },
48         { "INTEGER", LTDB_FLAG_INTEGER },
49         { "HIDDEN", LTDB_FLAG_HIDDEN },
50         { "NONE", 0 },
51         { NULL, 0 }
52 };
53
54
55 /*
56   de-register any special handlers for @ATTRIBUTES
57 */
58 static void ltdb_attributes_unload(struct ldb_module *module)
59 {
60         struct ltdb_private *ltdb = (struct ltdb_private *)module->private_data;
61         struct ldb_message *msg;
62         int i;
63
64         if (ltdb->cache->attributes == NULL) {
65                 /* no previously loaded attributes */
66                 return;
67         }
68
69         msg = ltdb->cache->attributes;
70         for (i=0;i<msg->num_elements;i++) {
71                 ldb_schema_attribute_remove(module->ldb, msg->elements[i].name);
72         }
73
74         talloc_free(ltdb->cache->attributes);
75         ltdb->cache->attributes = NULL;
76 }
77
78 /*
79   add up the attrib flags for a @ATTRIBUTES element
80 */
81 static int ltdb_attributes_flags(struct ldb_message_element *el, unsigned *v)
82 {
83         int i;
84         unsigned value = 0;
85         for (i=0;i<el->num_values;i++) {
86                 int j;
87                 for (j=0;ltdb_valid_attr_flags[j].name;j++) {
88                         if (strcmp(ltdb_valid_attr_flags[j].name, 
89                                    (char *)el->values[i].data) == 0) {
90                                 value |= ltdb_valid_attr_flags[j].value;
91                                 break;
92                         }
93                 }
94                 if (ltdb_valid_attr_flags[j].name == NULL) {
95                         return -1;
96                 }
97         }
98         *v = value;
99         return 0;
100 }
101
102 /*
103   register any special handlers from @ATTRIBUTES
104 */
105 static int ltdb_attributes_load(struct ldb_module *module)
106 {
107         struct ltdb_private *ltdb = (struct ltdb_private *)module->private_data;
108         struct ldb_message *msg = ltdb->cache->attributes;
109         struct ldb_dn *dn;
110         int i, r;
111
112         dn = ldb_dn_new(module, module->ldb, LTDB_ATTRIBUTES);
113         if (dn == NULL) goto failed;
114
115         r = ltdb_search_dn1(module, dn, msg);
116         talloc_free(dn);
117         if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
118                 goto failed;
119         }
120         if (r == LDB_ERR_NO_SUCH_OBJECT) {
121                 return 0;
122         }
123         /* mapping these flags onto ldap 'syntaxes' isn't strictly correct,
124            but its close enough for now */
125         for (i=0;i<msg->num_elements;i++) {
126                 unsigned flags;
127                 const char *syntax;
128                 const struct ldb_schema_syntax *s;
129
130                 if (ltdb_attributes_flags(&msg->elements[i], &flags) != 0) {
131                         ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Invalid @ATTRIBUTES element for '%s'\n", msg->elements[i].name);
132                         goto failed;
133                 }
134                 switch (flags & ~LTDB_FLAG_HIDDEN) {
135                 case 0:
136                         syntax = LDB_SYNTAX_OCTET_STRING;
137                         break;
138                 case LTDB_FLAG_CASE_INSENSITIVE:
139                         syntax = LDB_SYNTAX_DIRECTORY_STRING;
140                         break;
141                 case LTDB_FLAG_INTEGER:
142                         syntax = LDB_SYNTAX_INTEGER;
143                         break;
144                 default:
145                         ldb_debug(module->ldb, LDB_DEBUG_ERROR, 
146                                   "Invalid flag combination 0x%x for '%s' in @ATTRIBUTES\n",
147                                   flags, msg->elements[i].name);
148                         goto failed;
149                 }
150
151                 s = ldb_standard_syntax_by_name(module->ldb, syntax);
152                 if (s == NULL) {
153                         ldb_debug(module->ldb, LDB_DEBUG_ERROR, 
154                                   "Invalid attribute syntax '%s' for '%s' in @ATTRIBUTES\n",
155                                   syntax, msg->elements[i].name);
156                         goto failed;
157                 }
158
159                 flags |= LDB_ATTR_FLAG_ALLOCATED;
160                 if (ldb_schema_attribute_add_with_syntax(module->ldb, msg->elements[i].name, flags, s) != 0) {
161                         goto failed;
162                 }
163         }
164
165         return 0;
166 failed:
167         return -1;
168 }
169
170
171 /*
172   initialise the baseinfo record
173 */
174 static int ltdb_baseinfo_init(struct ldb_module *module)
175 {
176         struct ltdb_private *ltdb = (struct ltdb_private *)module->private_data;
177         struct ldb_message *msg;
178         struct ldb_message_element el;
179         struct ldb_val val;
180         int ret;
181         /* the initial sequence number must be different from the one
182            set in ltdb_cache_free(). Thanks to Jon for pointing this
183            out. */
184         const char *initial_sequence_number = "1";
185
186         ltdb->sequence_number = atof(initial_sequence_number);
187
188         msg = talloc(ltdb, struct ldb_message);
189         if (msg == NULL) {
190                 goto failed;
191         }
192
193         msg->num_elements = 1;
194         msg->elements = &el;
195         msg->dn = ldb_dn_new(msg, module->ldb, LTDB_BASEINFO);
196         if (!msg->dn) {
197                 goto failed;
198         }
199         el.name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
200         if (!el.name) {
201                 goto failed;
202         }
203         el.values = &val;
204         el.num_values = 1;
205         el.flags = 0;
206         val.data = (uint8_t *)talloc_strdup(msg, initial_sequence_number);
207         if (!val.data) {
208                 goto failed;
209         }
210         val.length = 1;
211         
212         ret = ltdb_store(module, msg, TDB_INSERT);
213
214         talloc_free(msg);
215
216         return ret;
217
218 failed:
219         talloc_free(msg);
220         errno = ENOMEM;
221         return LDB_ERR_OPERATIONS_ERROR;
222 }
223
224 /*
225   free any cache records
226  */
227 static void ltdb_cache_free(struct ldb_module *module)
228 {
229         struct ltdb_private *ltdb = (struct ltdb_private *)module->private_data;
230
231         ltdb->sequence_number = 0;
232         talloc_free(ltdb->cache);
233         ltdb->cache = NULL;
234 }
235
236 /*
237   force a cache reload
238 */
239 int ltdb_cache_reload(struct ldb_module *module)
240 {
241         ltdb_attributes_unload(module);
242         ltdb_cache_free(module);
243         return ltdb_cache_load(module);
244 }
245
246 /*
247   load the cache records
248 */
249 int ltdb_cache_load(struct ldb_module *module)
250 {
251         struct ltdb_private *ltdb = (struct ltdb_private *)module->private_data;
252         struct ldb_dn *baseinfo_dn = NULL, *options_dn = NULL;
253         struct ldb_dn *indexlist_dn = NULL;
254         uint64_t seq;
255         struct ldb_message *baseinfo = NULL, *options = NULL;
256         int r;
257
258         /* a very fast check to avoid extra database reads */
259         if (ltdb->cache != NULL && 
260             tdb_get_seqnum(ltdb->tdb) == ltdb->tdb_seqnum) {
261                 return 0;
262         }
263
264         if (ltdb->cache == NULL) {
265                 ltdb->cache = talloc_zero(ltdb, struct ltdb_cache);
266                 if (ltdb->cache == NULL) goto failed;
267                 ltdb->cache->indexlist = talloc_zero(ltdb->cache, struct ldb_message);
268                 ltdb->cache->attributes = talloc_zero(ltdb->cache, struct ldb_message);
269                 if (ltdb->cache->indexlist == NULL ||
270                     ltdb->cache->attributes == NULL) {
271                         goto failed;
272                 }
273         }
274
275         baseinfo = talloc(ltdb->cache, struct ldb_message);
276         if (baseinfo == NULL) goto failed;
277
278         baseinfo_dn = ldb_dn_new(module, module->ldb, LTDB_BASEINFO);
279         if (baseinfo_dn == NULL) goto failed;
280
281         r= ltdb_search_dn1(module, baseinfo_dn, baseinfo);
282         if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
283                 goto failed;
284         }
285         
286         /* possibly initialise the baseinfo */
287         if (r == LDB_ERR_NO_SUCH_OBJECT) {
288                 if (ltdb_baseinfo_init(module) != LDB_SUCCESS) {
289                         goto failed;
290                 }
291                 if (ltdb_search_dn1(module, baseinfo_dn, baseinfo) != LDB_SUCCESS) {
292                         goto failed;
293                 }
294         }
295
296         ltdb->tdb_seqnum = tdb_get_seqnum(ltdb->tdb);
297
298         /* if the current internal sequence number is the same as the one
299            in the database then assume the rest of the cache is OK */
300         seq = ldb_msg_find_attr_as_uint64(baseinfo, LTDB_SEQUENCE_NUMBER, 0);
301         if (seq == ltdb->sequence_number) {
302                 goto done;
303         }
304         ltdb->sequence_number = seq;
305
306         /* Read an interpret database options */
307         options = talloc(ltdb->cache, struct ldb_message);
308         if (options == NULL) goto failed;
309
310         options_dn = ldb_dn_new(options, module->ldb, LTDB_OPTIONS);
311         if (options_dn == NULL) goto failed;
312
313         r= ltdb_search_dn1(module, options_dn, options);
314         if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
315                 goto failed;
316         }
317         
318         /* set flag for checking base DN on searches */
319         if (r == LDB_SUCCESS) {
320                 ltdb->check_base = ldb_msg_find_attr_as_bool(options, LTDB_CHECK_BASE, false);
321         } else {
322                 ltdb->check_base = false;
323         }
324
325         talloc_free(ltdb->cache->last_attribute.name);
326         memset(&ltdb->cache->last_attribute, 0, sizeof(ltdb->cache->last_attribute));
327
328         ltdb_attributes_unload(module);
329
330         talloc_free(ltdb->cache->indexlist);
331
332         ltdb->cache->indexlist = talloc_zero(ltdb->cache, struct ldb_message);
333         ltdb->cache->attributes = talloc_zero(ltdb->cache, struct ldb_message);
334         if (ltdb->cache->indexlist == NULL ||
335             ltdb->cache->attributes == NULL) {
336                 goto failed;
337         }
338             
339         indexlist_dn = ldb_dn_new(module, module->ldb, LTDB_INDEXLIST);
340         if (indexlist_dn == NULL) goto failed;
341
342         r = ltdb_search_dn1(module, indexlist_dn, ltdb->cache->indexlist);
343         if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
344                 goto failed;
345         }
346
347         if (ltdb_attributes_load(module) == -1) {
348                 goto failed;
349         }
350
351 done:
352         talloc_free(options);
353         talloc_free(baseinfo);
354         talloc_free(baseinfo_dn);
355         talloc_free(indexlist_dn);
356         return 0;
357
358 failed:
359         talloc_free(options);
360         talloc_free(baseinfo);
361         talloc_free(baseinfo_dn);
362         talloc_free(indexlist_dn);
363         return -1;
364 }
365
366
367 /*
368   increase the sequence number to indicate a database change
369 */
370 int ltdb_increase_sequence_number(struct ldb_module *module)
371 {
372         struct ltdb_private *ltdb = (struct ltdb_private *)module->private_data;
373         struct ldb_message *msg;
374         struct ldb_message_element el[2];
375         struct ldb_val val;
376         struct ldb_val val_time;
377         time_t t = time(NULL);
378         char *s = NULL;
379         int ret;
380
381         msg = talloc(ltdb, struct ldb_message);
382         if (msg == NULL) {
383                 errno = ENOMEM;
384                 return LDB_ERR_OPERATIONS_ERROR;
385         }
386
387         s = talloc_asprintf(msg, "%llu", ltdb->sequence_number+1);
388         if (!s) {
389                 errno = ENOMEM;
390                 return LDB_ERR_OPERATIONS_ERROR;
391         }
392
393         msg->num_elements = ARRAY_SIZE(el);
394         msg->elements = el;
395         msg->dn = ldb_dn_new(msg, module->ldb, LTDB_BASEINFO);
396         if (msg->dn == NULL) {
397                 talloc_free(msg);
398                 errno = ENOMEM;
399                 return LDB_ERR_OPERATIONS_ERROR;
400         }
401         el[0].name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
402         if (el[0].name == NULL) {
403                 talloc_free(msg);
404                 errno = ENOMEM;
405                 return LDB_ERR_OPERATIONS_ERROR;
406         }
407         el[0].values = &val;
408         el[0].num_values = 1;
409         el[0].flags = LDB_FLAG_MOD_REPLACE;
410         val.data = (uint8_t *)s;
411         val.length = strlen(s);
412
413         el[1].name = talloc_strdup(msg, LTDB_MOD_TIMESTAMP);
414         if (el[1].name == NULL) {
415                 talloc_free(msg);
416                 errno = ENOMEM;
417                 return LDB_ERR_OPERATIONS_ERROR;
418         }
419         el[1].values = &val_time;
420         el[1].num_values = 1;
421         el[1].flags = LDB_FLAG_MOD_REPLACE;
422
423         s = ldb_timestring(msg, t);
424         if (s == NULL) {
425                 return LDB_ERR_OPERATIONS_ERROR;
426         }
427
428         val_time.data = (uint8_t *)s;
429         val_time.length = strlen(s);
430
431         ret = ltdb_modify_internal(module, msg);
432
433         talloc_free(msg);
434
435         if (ret == LDB_SUCCESS) {
436                 ltdb->sequence_number += 1;
437         }
438
439         /* updating the tdb_seqnum here avoids us reloading the cache
440            records due to our own modification */
441         ltdb->tdb_seqnum = tdb_get_seqnum(ltdb->tdb);
442
443         return ret;
444 }
445
446 int ltdb_check_at_attributes_values(const struct ldb_val *value)
447 {
448         int i;
449
450         for (i = 0; ltdb_valid_attr_flags[i].name != NULL; i++) {
451                 if ((strcmp(ltdb_valid_attr_flags[i].name, (char *)value->data) == 0)) {
452                         return 0;
453                 }
454         }
455
456         return -1;
457 }
458