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