r26574: Do not call functions in the variable declaration, fix checking for tmp_ctx,
[samba.git] / source4 / lib / ldb / ldb_tdb / ldb_tdb.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004
5    Copyright (C) Stefan Metzmacher  2004
6    Copyright (C) Simo Sorce       2006
7    
8
9      ** NOTE! The following LGPL license applies to the ldb
10      ** library. This does NOT imply that all of Samba is released
11      ** under the LGPL
12    
13    This library is free software; you can redistribute it and/or
14    modify it under the terms of the GNU Lesser General Public
15    License as published by the Free Software Foundation; either
16    version 3 of the License, or (at your option) any later version.
17
18    This library is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21    Lesser General Public License for more details.
22
23    You should have received a copy of the GNU Lesser General Public
24    License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 */
26
27 /*
28  *  Name: ldb_tdb
29  *
30  *  Component: ldb tdb backend
31  *
32  *  Description: core functions for tdb backend
33  *
34  *  Author: Andrew Tridgell
35  *  Author: Stefan Metzmacher
36  *
37  *  Modifications:
38  *
39  *  - description: make the module use asyncronous calls
40  *    date: Feb 2006
41  *    Author: Simo Sorce
42  */
43
44 #include "ldb_includes.h"
45
46 #include "ldb_tdb.h"
47
48
49 /*
50   map a tdb error code to a ldb error code
51 */
52 static int ltdb_err_map(enum TDB_ERROR tdb_code)
53 {
54         switch (tdb_code) {
55         case TDB_SUCCESS:
56                 return LDB_SUCCESS;
57         case TDB_ERR_CORRUPT:
58         case TDB_ERR_OOM:
59         case TDB_ERR_EINVAL:
60                 return LDB_ERR_OPERATIONS_ERROR;
61         case TDB_ERR_IO:
62                 return LDB_ERR_PROTOCOL_ERROR;
63         case TDB_ERR_LOCK:
64         case TDB_ERR_NOLOCK:
65                 return LDB_ERR_BUSY;
66         case TDB_ERR_LOCK_TIMEOUT:
67                 return LDB_ERR_TIME_LIMIT_EXCEEDED;
68         case TDB_ERR_EXISTS:
69                 return LDB_ERR_ENTRY_ALREADY_EXISTS;
70         case TDB_ERR_NOEXIST:
71                 return LDB_ERR_NO_SUCH_OBJECT;
72         case TDB_ERR_RDONLY:
73                 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
74         }
75         return LDB_ERR_OTHER;
76 }
77
78
79 struct ldb_handle *init_ltdb_handle(struct ltdb_private *ltdb, struct ldb_module *module,
80                                     struct ldb_request *req)
81 {
82         struct ltdb_context *ac;
83         struct ldb_handle *h;
84
85         h = talloc_zero(req, struct ldb_handle);
86         if (h == NULL) {
87                 ldb_set_errstring(module->ldb, "Out of Memory");
88                 return NULL;
89         }
90
91         h->module = module;
92
93         ac = talloc_zero(h, struct ltdb_context);
94         if (ac == NULL) {
95                 ldb_set_errstring(module->ldb, "Out of Memory");
96                 talloc_free(h);
97                 return NULL;
98         }
99
100         h->private_data = (void *)ac;
101
102         h->state = LDB_ASYNC_INIT;
103         h->status = LDB_SUCCESS;
104
105         ac->module = module;
106         ac->context = req->context;
107         ac->callback = req->callback;
108
109         return h;
110 }
111
112 /*
113   form a TDB_DATA for a record key
114   caller frees
115
116   note that the key for a record can depend on whether the 
117   dn refers to a case sensitive index record or not
118 */
119 struct TDB_DATA ltdb_key(struct ldb_module *module, struct ldb_dn *dn)
120 {
121         struct ldb_context *ldb = module->ldb;
122         TDB_DATA key;
123         char *key_str = NULL;
124         const char *dn_folded = NULL;
125
126         /*
127           most DNs are case insensitive. The exception is index DNs for
128           case sensitive attributes
129
130           there are 3 cases dealt with in this code:
131
132           1) if the dn doesn't start with @ then uppercase the attribute
133              names and the attributes values of case insensitive attributes
134           2) if the dn starts with @ then leave it alone - the indexing code handles
135              the rest
136         */
137
138         dn_folded = ldb_dn_get_casefold(dn);
139         if (!dn_folded) {
140                 goto failed;
141         }
142
143         key_str = talloc_strdup(ldb, "DN=");
144         if (!key_str) {
145                 goto failed;
146         }
147
148         key_str = talloc_strdup_append_buffer(key_str, dn_folded);
149         if (!key_str) {
150                 goto failed;
151         }
152
153         key.dptr = (uint8_t *)key_str;
154         key.dsize = strlen(key_str) + 1;
155
156         return key;
157
158 failed:
159         errno = ENOMEM;
160         key.dptr = NULL;
161         key.dsize = 0;
162         return key;
163 }
164
165 /*
166   check special dn's have valid attributes
167   currently only @ATTRIBUTES is checked
168 */
169 int ltdb_check_special_dn(struct ldb_module *module, const struct ldb_message *msg)
170 {
171         int i, j;
172  
173         if (! ldb_dn_is_special(msg->dn) ||
174             ! ldb_dn_check_special(msg->dn, LTDB_ATTRIBUTES)) {
175                 return 0;
176         }
177
178         /* we have @ATTRIBUTES, let's check attributes are fine */
179         /* should we check that we deny multivalued attributes ? */
180         for (i = 0; i < msg->num_elements; i++) {
181                 for (j = 0; j < msg->elements[i].num_values; j++) {
182                         if (ltdb_check_at_attributes_values(&msg->elements[i].values[j]) != 0) {
183                                 ldb_set_errstring(module->ldb, "Invalid attribute value in an @ATTRIBUTES entry");
184                                 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
185                         }
186                 }
187         }
188
189         return 0;
190 }
191
192
193 /*
194   we've made a modification to a dn - possibly reindex and 
195   update sequence number
196 */
197 static int ltdb_modified(struct ldb_module *module, struct ldb_dn *dn)
198 {
199         int ret = LDB_SUCCESS;
200
201         if (ldb_dn_is_special(dn) &&
202             (ldb_dn_check_special(dn, LTDB_INDEXLIST) ||
203              ldb_dn_check_special(dn, LTDB_ATTRIBUTES)) ) {
204                 ret = ltdb_reindex(module);
205         }
206
207         if (ret == LDB_SUCCESS &&
208             !(ldb_dn_is_special(dn) &&
209               ldb_dn_check_special(dn, LTDB_BASEINFO)) ) {
210                 ret = ltdb_increase_sequence_number(module);
211         }
212
213         return ret;
214 }
215
216 /*
217   store a record into the db
218 */
219 int ltdb_store(struct ldb_module *module, const struct ldb_message *msg, int flgs)
220 {
221         struct ltdb_private *ltdb =
222                 talloc_get_type(module->private_data, struct ltdb_private);
223         TDB_DATA tdb_key, tdb_data;
224         int ret;
225
226         tdb_key = ltdb_key(module, msg->dn);
227         if (!tdb_key.dptr) {
228                 return LDB_ERR_OTHER;
229         }
230
231         ret = ltdb_pack_data(module, msg, &tdb_data);
232         if (ret == -1) {
233                 talloc_free(tdb_key.dptr);
234                 return LDB_ERR_OTHER;
235         }
236
237         ret = tdb_store(ltdb->tdb, tdb_key, tdb_data, flgs);
238         if (ret == -1) {
239                 ret = ltdb_err_map(tdb_error(ltdb->tdb));
240                 goto done;
241         }
242         
243         ret = ltdb_index_add(module, msg);
244         if (ret != LDB_SUCCESS) {
245                 tdb_delete(ltdb->tdb, tdb_key);
246         }
247
248 done:
249         talloc_free(tdb_key.dptr);
250         talloc_free(tdb_data.dptr);
251
252         return ret;
253 }
254
255
256 static int ltdb_add_internal(struct ldb_module *module, const struct ldb_message *msg)
257 {
258         int ret;
259         
260         ret = ltdb_check_special_dn(module, msg);
261         if (ret != LDB_SUCCESS) {
262                 return ret;
263         }
264         
265         if (ltdb_cache_load(module) != 0) {
266                 return LDB_ERR_OPERATIONS_ERROR;
267         }
268
269         ret = ltdb_store(module, msg, TDB_INSERT);
270
271         if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
272                 ldb_asprintf_errstring(module->ldb, "Entry %s already exists", ldb_dn_get_linearized(msg->dn));
273                 return ret;
274         }
275         
276         if (ret == LDB_SUCCESS) {
277                 ret = ltdb_index_one(module, msg, 1);
278                 if (ret != LDB_SUCCESS) {
279                         return ret;
280                 }
281
282                 ret = ltdb_modified(module, msg->dn);
283                 if (ret != LDB_SUCCESS) {
284                         return ret;
285                 }
286         }
287
288         return ret;
289 }
290
291 /*
292   add a record to the database
293 */
294 static int ltdb_add(struct ldb_module *module, struct ldb_request *req)
295 {
296         struct ltdb_private *ltdb = talloc_get_type(module->private_data, struct ltdb_private);
297         struct ltdb_context *ltdb_ac;
298         int tret, ret = LDB_SUCCESS;
299
300         if (check_critical_controls(req->controls)) {
301                 return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
302         }
303         
304         req->handle = init_ltdb_handle(ltdb, module, req);
305         if (req->handle == NULL) {
306                 return LDB_ERR_OPERATIONS_ERROR;
307         }
308         ltdb_ac = talloc_get_type(req->handle->private_data, struct ltdb_context);
309
310         tret = ltdb_add_internal(module, req->op.add.message);
311         if (tret != LDB_SUCCESS) {
312                 req->handle->status = tret;
313                 goto done;
314         }
315         
316         if (ltdb_ac->callback) {
317                 ret = ltdb_ac->callback(module->ldb, ltdb_ac->context, NULL);
318         }
319 done:
320         req->handle->state = LDB_ASYNC_DONE;
321         return ret;
322 }
323
324 /*
325   delete a record from the database, not updating indexes (used for deleting
326   index records)
327 */
328 int ltdb_delete_noindex(struct ldb_module *module, struct ldb_dn *dn)
329 {
330         struct ltdb_private *ltdb =
331                 talloc_get_type(module->private_data, struct ltdb_private);
332         TDB_DATA tdb_key;
333         int ret;
334
335         tdb_key = ltdb_key(module, dn);
336         if (!tdb_key.dptr) {
337                 return LDB_ERR_OTHER;
338         }
339
340         ret = tdb_delete(ltdb->tdb, tdb_key);
341         talloc_free(tdb_key.dptr);
342
343         if (ret != 0) {
344                 ret = ltdb_err_map(tdb_error(ltdb->tdb));
345         }
346
347         return ret;
348 }
349
350 static int ltdb_delete_internal(struct ldb_module *module, struct ldb_dn *dn)
351 {
352         struct ldb_message *msg;
353         int ret;
354
355         msg = talloc(module, struct ldb_message);
356         if (msg == NULL) {
357                 return LDB_ERR_OPERATIONS_ERROR;
358         }
359
360         /* in case any attribute of the message was indexed, we need
361            to fetch the old record */
362         ret = ltdb_search_dn1(module, dn, msg);
363         if (ret != LDB_SUCCESS) {
364                 /* not finding the old record is an error */
365                 goto done;
366         }
367
368         ret = ltdb_delete_noindex(module, dn);
369         if (ret != LDB_SUCCESS) {
370                 goto done;
371         }
372
373         /* remove one level attribute */
374         ret = ltdb_index_one(module, msg, 0);
375         if (ret != LDB_SUCCESS) {
376                 goto done;
377         }
378
379         /* remove any indexed attributes */
380         ret = ltdb_index_del(module, msg);
381         if (ret != LDB_SUCCESS) {
382                 goto done;
383         }
384
385         ret = ltdb_modified(module, dn);
386         if (ret != LDB_SUCCESS) {
387                 goto done;
388         }
389
390 done:
391         talloc_free(msg);
392         return ret;
393 }
394
395 /*
396   delete a record from the database
397 */
398 static int ltdb_delete(struct ldb_module *module, struct ldb_request *req)
399 {
400         struct ltdb_private *ltdb = talloc_get_type(module->private_data, struct ltdb_private);
401         struct ltdb_context *ltdb_ac;
402         int tret, ret = LDB_SUCCESS;
403
404         if (check_critical_controls(req->controls)) {
405                 return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
406         }
407         
408         req->handle = NULL;
409
410         if (ltdb_cache_load(module) != 0) {
411                 return LDB_ERR_OPERATIONS_ERROR;
412         }
413
414         req->handle = init_ltdb_handle(ltdb, module, req);
415         if (req->handle == NULL) {
416                 return LDB_ERR_OPERATIONS_ERROR;
417         }
418         ltdb_ac = talloc_get_type(req->handle->private_data, struct ltdb_context);
419
420         tret = ltdb_delete_internal(module, req->op.del.dn);
421         if (tret != LDB_SUCCESS) {
422                 req->handle->status = tret; 
423                 goto done;
424         }
425
426         if (ltdb_ac->callback) {
427                 ret = ltdb_ac->callback(module->ldb, ltdb_ac->context, NULL);
428         }
429 done:
430         req->handle->state = LDB_ASYNC_DONE;
431         return ret;
432 }
433
434 /*
435   find an element by attribute name. At the moment this does a linear search, it should
436   be re-coded to use a binary search once all places that modify records guarantee
437   sorted order
438
439   return the index of the first matching element if found, otherwise -1
440 */
441 static int find_element(const struct ldb_message *msg, const char *name)
442 {
443         unsigned int i;
444         for (i=0;i<msg->num_elements;i++) {
445                 if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
446                         return i;
447                 }
448         }
449         return -1;
450 }
451
452
453 /*
454   add an element to an existing record. Assumes a elements array that we
455   can call re-alloc on, and assumed that we can re-use the data pointers from the 
456   passed in additional values. Use with care!
457
458   returns 0 on success, -1 on failure (and sets errno)
459 */
460 static int msg_add_element(struct ldb_context *ldb,
461                            struct ldb_message *msg, struct ldb_message_element *el)
462 {
463         struct ldb_message_element *e2;
464         unsigned int i;
465
466         e2 = talloc_realloc(msg, msg->elements, struct ldb_message_element, 
467                               msg->num_elements+1);
468         if (!e2) {
469                 errno = ENOMEM;
470                 return -1;
471         }
472
473         msg->elements = e2;
474
475         e2 = &msg->elements[msg->num_elements];
476
477         e2->name = el->name;
478         e2->flags = el->flags;
479         e2->values = NULL;
480         if (el->num_values != 0) {
481                 e2->values = talloc_array(msg->elements, struct ldb_val, el->num_values);
482                 if (!e2->values) {
483                         errno = ENOMEM;
484                         return -1;
485                 }
486         }
487         for (i=0;i<el->num_values;i++) {
488                 e2->values[i] = el->values[i];
489         }
490         e2->num_values = el->num_values;
491
492         msg->num_elements++;
493
494         return 0;
495 }
496
497 /*
498   delete all elements having a specified attribute name
499 */
500 static int msg_delete_attribute(struct ldb_module *module,
501                                 struct ldb_context *ldb,
502                                 struct ldb_message *msg, const char *name)
503 {
504         const char *dn;
505         unsigned int i, j;
506
507         dn = ldb_dn_get_linearized(msg->dn);
508         if (dn == NULL) {
509                 return -1;
510         }
511
512         for (i=0;i<msg->num_elements;i++) {
513                 if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
514                         for (j=0;j<msg->elements[i].num_values;j++) {
515                                 ltdb_index_del_value(module, dn, &msg->elements[i], j);
516                         }
517                         talloc_free(msg->elements[i].values);
518                         if (msg->num_elements > (i+1)) {
519                                 memmove(&msg->elements[i], 
520                                         &msg->elements[i+1], 
521                                         sizeof(struct ldb_message_element)*
522                                         (msg->num_elements - (i+1)));
523                         }
524                         msg->num_elements--;
525                         i--;
526                         msg->elements = talloc_realloc(msg, msg->elements, 
527                                                          struct ldb_message_element, 
528                                                          msg->num_elements);
529                 }
530         }
531
532         return 0;
533 }
534
535 /*
536   delete all elements matching an attribute name/value 
537
538   return 0 on success, -1 on failure
539 */
540 static int msg_delete_element(struct ldb_module *module,
541                               struct ldb_message *msg, 
542                               const char *name,
543                               const struct ldb_val *val)
544 {
545         struct ldb_context *ldb = module->ldb;
546         unsigned int i;
547         int found;
548         struct ldb_message_element *el;
549         const struct ldb_schema_attribute *a;
550
551         found = find_element(msg, name);
552         if (found == -1) {
553                 return -1;
554         }
555
556         el = &msg->elements[found];
557
558         a = ldb_schema_attribute_by_name(ldb, el->name);
559
560         for (i=0;i<el->num_values;i++) {
561                 if (a->syntax->comparison_fn(ldb, ldb, &el->values[i], val) == 0) {
562                         if (i<el->num_values-1) {
563                                 memmove(&el->values[i], &el->values[i+1],
564                                         sizeof(el->values[i])*(el->num_values-(i+1)));
565                         }
566                         el->num_values--;
567                         if (el->num_values == 0) {
568                                 return msg_delete_attribute(module, ldb, msg, name);
569                         }
570                         return 0;
571                 }
572         }
573
574         return -1;
575 }
576
577
578 /*
579   modify a record - internal interface
580
581   yuck - this is O(n^2). Luckily n is usually small so we probably
582   get away with it, but if we ever have really large attribute lists 
583   then we'll need to look at this again
584 */
585 int ltdb_modify_internal(struct ldb_module *module, const struct ldb_message *msg)
586 {
587         struct ldb_context *ldb = module->ldb;
588         struct ltdb_private *ltdb =
589                 talloc_get_type(module->private_data, struct ltdb_private);
590         TDB_DATA tdb_key, tdb_data;
591         struct ldb_message *msg2;
592         unsigned i, j;
593         int ret, idx;
594
595         tdb_key = ltdb_key(module, msg->dn);
596         if (!tdb_key.dptr) {
597                 return LDB_ERR_OTHER;
598         }
599
600         tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
601         if (!tdb_data.dptr) {
602                 talloc_free(tdb_key.dptr);
603                 return ltdb_err_map(tdb_error(ltdb->tdb));
604         }
605
606         msg2 = talloc(tdb_key.dptr, struct ldb_message);
607         if (msg2 == NULL) {
608                 talloc_free(tdb_key.dptr);
609                 return LDB_ERR_OTHER;
610         }
611
612         ret = ltdb_unpack_data(module, &tdb_data, msg2);
613         if (ret == -1) {
614                 ret = LDB_ERR_OTHER;
615                 goto failed;
616         }
617
618         if (!msg2->dn) {
619                 msg2->dn = msg->dn;
620         }
621
622         for (i=0;i<msg->num_elements;i++) {
623                 struct ldb_message_element *el = &msg->elements[i];
624                 struct ldb_message_element *el2;
625                 struct ldb_val *vals;
626                 const char *dn;
627
628                 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
629
630                 case LDB_FLAG_MOD_ADD:
631                         /* add this element to the message. fail if it
632                            already exists */
633                         idx = find_element(msg2, el->name);
634
635                         if (idx == -1) {
636                                 if (msg_add_element(ldb, msg2, el) != 0) {
637                                         ret = LDB_ERR_OTHER;
638                                         goto failed;
639                                 }
640                                 continue;
641                         }
642
643                         el2 = &msg2->elements[idx];
644
645                         /* An attribute with this name already exists,
646                          * add all values if they don't already exist
647                          * (check both the other elements to be added,
648                          * and those already in the db). */
649
650                         for (j=0;j<el->num_values;j++) {
651                                 if (ldb_msg_find_val(el2, &el->values[j])) {
652                                         ldb_asprintf_errstring(module->ldb, "%s: value #%d already exists", el->name, j);
653                                         ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
654                                         goto failed;
655                                 }
656                                 if (ldb_msg_find_val(el, &el->values[j]) != &el->values[j]) {
657                                         ldb_asprintf_errstring(module->ldb, "%s: value #%d provided more than once", el->name, j);
658                                         ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
659                                         goto failed;
660                                 }
661                         }
662
663                         vals = talloc_realloc(msg2->elements, el2->values, struct ldb_val,
664                                                 el2->num_values + el->num_values);
665
666                         if (vals == NULL) {
667                                 ret = LDB_ERR_OTHER;
668                                 goto failed;
669                         }
670
671                         for (j=0;j<el->num_values;j++) {
672                                 vals[el2->num_values + j] =
673                                         ldb_val_dup(vals, &el->values[j]);
674                         }
675
676                         el2->values = vals;
677                         el2->num_values += el->num_values;
678
679                         break;
680
681                 case LDB_FLAG_MOD_REPLACE:
682                         /* replace all elements of this attribute name with the elements
683                            listed. The attribute not existing is not an error */
684                         msg_delete_attribute(module, ldb, msg2, el->name);
685
686                         for (j=0;j<el->num_values;j++) {
687                                 if (ldb_msg_find_val(el, &el->values[j]) != &el->values[j]) {
688                                         ldb_asprintf_errstring(module->ldb, "%s: value #%d provided more than once", el->name, j);
689                                         ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
690                                         goto failed;
691                                 }
692                         }
693
694                         /* add the replacement element, if not empty */
695                         if (el->num_values != 0 &&
696                             msg_add_element(ldb, msg2, el) != 0) {
697                                 ret = LDB_ERR_OTHER;
698                                 goto failed;
699                         }
700                         break;
701
702                 case LDB_FLAG_MOD_DELETE:
703
704                         dn = ldb_dn_get_linearized(msg->dn);
705                         if (dn == NULL) {
706                                 ret = LDB_ERR_OTHER;
707                                 goto failed;
708                         }
709
710                         /* we could be being asked to delete all
711                            values or just some values */
712                         if (msg->elements[i].num_values == 0) {
713                                 if (msg_delete_attribute(module, ldb, msg2, 
714                                                          msg->elements[i].name) != 0) {
715                                         ldb_asprintf_errstring(module->ldb, "No such attribute: %s for delete on %s", msg->elements[i].name, dn);
716                                         ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
717                                         goto failed;
718                                 }
719                                 break;
720                         }
721                         for (j=0;j<msg->elements[i].num_values;j++) {
722                                 if (msg_delete_element(module,
723                                                        msg2, 
724                                                        msg->elements[i].name,
725                                                        &msg->elements[i].values[j]) != 0) {
726                                         ldb_asprintf_errstring(module->ldb, "No matching attribute value when deleting attribute: %s on %s", msg->elements[i].name, dn);
727                                         ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
728                                         goto failed;
729                                 }
730                                 ret = ltdb_index_del_value(module, dn, &msg->elements[i], j);
731                                 if (ret != LDB_SUCCESS) {
732                                         goto failed;
733                                 }
734                         }
735                         break;
736                 default:
737                         ldb_asprintf_errstring(module->ldb, "Invalid ldb_modify flags on %s: 0x%x", 
738                                                              msg->elements[i].name, 
739                                                              msg->elements[i].flags & LDB_FLAG_MOD_MASK);
740                         ret = LDB_ERR_PROTOCOL_ERROR;
741                         goto failed;
742                 }
743         }
744
745         /* we've made all the mods - save the modified record back into the database */
746         ret = ltdb_store(module, msg2, TDB_MODIFY);
747         if (ret != LDB_SUCCESS) {
748                 goto failed;
749         }
750
751         ret = ltdb_modified(module, msg->dn);
752         if (ret != LDB_SUCCESS) {
753                 goto failed;
754         }
755
756         talloc_free(tdb_key.dptr);
757         free(tdb_data.dptr);
758         return ret;
759
760 failed:
761         talloc_free(tdb_key.dptr);
762         free(tdb_data.dptr);
763         return ret;
764 }
765
766 /*
767   modify a record
768 */
769 static int ltdb_modify(struct ldb_module *module, struct ldb_request *req)
770 {
771         struct ltdb_private *ltdb = talloc_get_type(module->private_data, struct ltdb_private);
772         struct ltdb_context *ltdb_ac;
773         int tret, ret = LDB_SUCCESS;
774
775         if (check_critical_controls(req->controls)) {
776                 return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
777         }
778         
779         req->handle = NULL;
780
781         req->handle = init_ltdb_handle(ltdb, module, req);
782         if (req->handle == NULL) {
783                 return LDB_ERR_OPERATIONS_ERROR;
784         }
785         ltdb_ac = talloc_get_type(req->handle->private_data, struct ltdb_context);
786
787         tret = ltdb_check_special_dn(module, req->op.mod.message);
788         if (tret != LDB_SUCCESS) {
789                 req->handle->status = tret;
790                 goto done;
791         }
792         
793         if (ltdb_cache_load(module) != 0) {
794                 ret = LDB_ERR_OPERATIONS_ERROR;
795                 goto done;
796         }
797
798         tret = ltdb_modify_internal(module, req->op.mod.message);
799         if (tret != LDB_SUCCESS) {
800                 req->handle->status = tret;
801                 goto done;
802         }
803
804         if (ltdb_ac->callback) {
805                 ret = ltdb_ac->callback(module->ldb, ltdb_ac->context, NULL);
806         }
807 done:
808         req->handle->state = LDB_ASYNC_DONE;
809         return ret;
810 }
811
812 /*
813   rename a record
814 */
815 static int ltdb_rename(struct ldb_module *module, struct ldb_request *req)
816 {
817         struct ltdb_private *ltdb = talloc_get_type(module->private_data, struct ltdb_private);
818         struct ltdb_context *ltdb_ac;
819         struct ldb_message *msg;
820         int tret, ret = LDB_SUCCESS;
821
822         if (check_critical_controls(req->controls)) {
823                 return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
824         }
825         
826         req->handle = NULL;
827
828         if (ltdb_cache_load(module) != 0) {
829                 return LDB_ERR_OPERATIONS_ERROR;
830         }
831
832         req->handle = init_ltdb_handle(ltdb, module, req);
833         if (req->handle == NULL) {
834                 return LDB_ERR_OPERATIONS_ERROR;
835         }
836         ltdb_ac = talloc_get_type(req->handle->private_data, struct ltdb_context);
837
838         msg = talloc(ltdb_ac, struct ldb_message);
839         if (msg == NULL) {
840                 ret = LDB_ERR_OPERATIONS_ERROR;
841                 goto done;
842         }
843
844         /* in case any attribute of the message was indexed, we need
845            to fetch the old record */
846         tret = ltdb_search_dn1(module, req->op.rename.olddn, msg);
847         if (tret != LDB_SUCCESS) {
848                 /* not finding the old record is an error */
849                 req->handle->status = tret;
850                 goto done;
851         }
852
853         msg->dn = ldb_dn_copy(msg, req->op.rename.newdn);
854         if (!msg->dn) {
855                 ret = LDB_ERR_OPERATIONS_ERROR;
856                 goto done;
857         }
858
859         if (ldb_dn_compare(req->op.rename.olddn, req->op.rename.newdn) == 0) {
860                 /* The rename operation is apparently only changing case -
861                    the DNs are the same.  Delete the old DN before adding
862                    the new one to avoid a TDB_ERR_EXISTS error.
863
864                    The only drawback to this is that if the delete
865                    succeeds but the add fails, we rely on the
866                    transaction to roll this all back. */
867                 ret = ltdb_delete_internal(module, req->op.rename.olddn);
868                 if (ret != LDB_SUCCESS) {
869                         goto done;
870                 }
871
872                 ret = ltdb_add_internal(module, msg);
873                 if (ret != LDB_SUCCESS) {
874                         goto done;
875                 }
876         } else {
877                 /* The rename operation is changing DNs.  Try to add the new
878                    DN first to avoid clobbering another DN not related to
879                    this rename operation. */
880                 ret = ltdb_add_internal(module, msg);
881                 if (ret != LDB_SUCCESS) {
882                         goto done;
883                 }
884
885                 tret = ltdb_delete_internal(module, req->op.rename.olddn);
886                 if (tret != LDB_SUCCESS) {
887                         ltdb_delete_internal(module, req->op.rename.newdn);
888                         ret = LDB_ERR_OPERATIONS_ERROR;
889                         goto done;
890                 }
891         }
892
893         if (ltdb_ac->callback) {
894                 ret = ltdb_ac->callback(module->ldb, ltdb_ac->context, NULL);
895         }
896 done:
897         req->handle->state = LDB_ASYNC_DONE;
898         return ret;
899 }
900
901 static int ltdb_start_trans(struct ldb_module *module)
902 {
903         struct ltdb_private *ltdb =
904                 talloc_get_type(module->private_data, struct ltdb_private);
905
906         if (tdb_transaction_start(ltdb->tdb) != 0) {
907                 return ltdb_err_map(tdb_error(ltdb->tdb));
908         }
909
910         ltdb->in_transaction++;
911
912         return LDB_SUCCESS;
913 }
914
915 static int ltdb_end_trans(struct ldb_module *module)
916 {
917         struct ltdb_private *ltdb =
918                 talloc_get_type(module->private_data, struct ltdb_private);
919
920         ltdb->in_transaction--;
921
922         if (tdb_transaction_commit(ltdb->tdb) != 0) {
923                 return ltdb_err_map(tdb_error(ltdb->tdb));
924         }
925
926         return LDB_SUCCESS;
927 }
928
929 static int ltdb_del_trans(struct ldb_module *module)
930 {
931         struct ltdb_private *ltdb =
932                 talloc_get_type(module->private_data, struct ltdb_private);
933
934         ltdb->in_transaction--;
935
936         if (tdb_transaction_cancel(ltdb->tdb) != 0) {
937                 return ltdb_err_map(tdb_error(ltdb->tdb));
938         }
939
940         return LDB_SUCCESS;
941 }
942
943 static int ltdb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
944 {
945         return handle->status;
946 }
947
948 static int ltdb_request(struct ldb_module *module, struct ldb_request *req)
949 {
950         /* check for oustanding critical controls and return an error if found */
951         if (check_critical_controls(req->controls)) {
952                 return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
953         }
954         
955         /* search, add, modify, delete, rename are handled by their own, no other op supported */
956         return LDB_ERR_OPERATIONS_ERROR;
957 }
958
959 /*
960   return sequenceNumber from @BASEINFO
961 */
962 static int ltdb_sequence_number(struct ldb_module *module, struct ldb_request *req)
963 {
964         TALLOC_CTX *tmp_ctx;
965         struct ldb_message *msg = NULL;
966         struct ldb_dn *dn;
967         const char *date;
968         int tret;
969
970         tmp_ctx = talloc_new(req);
971         if (tmp_ctx == NULL) {
972                 talloc_free(tmp_ctx);
973                 return LDB_ERR_OPERATIONS_ERROR;
974         }
975
976         dn = ldb_dn_new(tmp_ctx, module->ldb, LTDB_BASEINFO);
977
978         msg = talloc(tmp_ctx, struct ldb_message);
979         if (msg == NULL) {
980                 talloc_free(tmp_ctx);
981                 return LDB_ERR_OPERATIONS_ERROR;
982         }
983
984         req->op.seq_num.flags = 0;
985
986         tret = ltdb_search_dn1(module, dn, msg);
987         if (tret != LDB_SUCCESS) {
988                 talloc_free(tmp_ctx);
989                 /* zero is as good as anything when we don't know */
990                 req->op.seq_num.seq_num = 0;
991                 return LDB_SUCCESS;
992         }
993
994         switch (req->op.seq_num.type) {
995         case LDB_SEQ_HIGHEST_SEQ:
996                 req->op.seq_num.seq_num = ldb_msg_find_attr_as_uint64(msg, LTDB_SEQUENCE_NUMBER, 0);
997                 break;
998         case LDB_SEQ_NEXT:
999                 req->op.seq_num.seq_num = ldb_msg_find_attr_as_uint64(msg, LTDB_SEQUENCE_NUMBER, 0);
1000                 req->op.seq_num.seq_num++;
1001                 break;
1002         case LDB_SEQ_HIGHEST_TIMESTAMP:
1003                 date = ldb_msg_find_attr_as_string(msg, LTDB_MOD_TIMESTAMP, NULL);
1004                 if (date) {
1005                         req->op.seq_num.seq_num = ldb_string_to_time(date);
1006                 } else {
1007                         req->op.seq_num.seq_num = 0;
1008                         /* zero is as good as anything when we don't know */
1009                 }
1010                 break;
1011         }
1012         talloc_free(tmp_ctx);
1013         return LDB_SUCCESS;
1014 }
1015
1016 static const struct ldb_module_ops ltdb_ops = {
1017         .name              = "tdb",
1018         .search            = ltdb_search,
1019         .add               = ltdb_add,
1020         .modify            = ltdb_modify,
1021         .del               = ltdb_delete,
1022         .rename            = ltdb_rename,
1023         .request           = ltdb_request,
1024         .start_transaction = ltdb_start_trans,
1025         .end_transaction   = ltdb_end_trans,
1026         .del_transaction   = ltdb_del_trans,
1027         .wait              = ltdb_wait,
1028         .sequence_number   = ltdb_sequence_number
1029 };
1030
1031 /*
1032   connect to the database
1033 */
1034 static int ltdb_connect(struct ldb_context *ldb, const char *url, 
1035                         unsigned int flags, const char *options[],
1036                         struct ldb_module **module)
1037 {
1038         const char *path;
1039         int tdb_flags, open_flags;
1040         struct ltdb_private *ltdb;
1041
1042         /* parse the url */
1043         if (strchr(url, ':')) {
1044                 if (strncmp(url, "tdb://", 6) != 0) {
1045                         ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid tdb URL '%s'", url);
1046                         return -1;
1047                 }
1048                 path = url+6;
1049         } else {
1050                 path = url;
1051         }
1052
1053         tdb_flags = TDB_DEFAULT | TDB_SEQNUM;
1054
1055         /* check for the 'nosync' option */
1056         if (flags & LDB_FLG_NOSYNC) {
1057                 tdb_flags |= TDB_NOSYNC;
1058         }
1059
1060         /* and nommap option */
1061         if (flags & LDB_FLG_NOMMAP) {
1062                 tdb_flags |= TDB_NOMMAP;
1063         }
1064
1065         if (flags & LDB_FLG_RDONLY) {
1066                 open_flags = O_RDONLY;
1067         } else {
1068                 open_flags = O_CREAT | O_RDWR;
1069         }
1070
1071         ltdb = talloc_zero(ldb, struct ltdb_private);
1072         if (!ltdb) {
1073                 ldb_oom(ldb);
1074                 return -1;
1075         }
1076
1077         /* note that we use quite a large default hash size */
1078         ltdb->tdb = ltdb_wrap_open(ltdb, path, 10000, 
1079                                    tdb_flags, open_flags, 
1080                                    ldb->create_perms, ldb);
1081         if (!ltdb->tdb) {
1082                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Unable to open tdb '%s'\n", path);
1083                 talloc_free(ltdb);
1084                 return -1;
1085         }
1086
1087         ltdb->sequence_number = 0;
1088
1089         *module = talloc(ldb, struct ldb_module);
1090         if (!module) {
1091                 ldb_oom(ldb);
1092                 talloc_free(ltdb);
1093                 return -1;
1094         }
1095         talloc_set_name_const(*module, "ldb_tdb backend");
1096         (*module)->ldb = ldb;
1097         (*module)->prev = (*module)->next = NULL;
1098         (*module)->private_data = ltdb;
1099         (*module)->ops = &ltdb_ops;
1100
1101         if (ltdb_cache_load(*module) != 0) {
1102                 talloc_free(*module);
1103                 talloc_free(ltdb);
1104                 return -1;
1105         }
1106
1107         return 0;
1108 }
1109
1110 int ldb_tdb_init(void)
1111 {
1112         return ldb_register_backend("tdb", ltdb_connect);
1113 }