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