r4714: move the ldb code to the new talloc interface (eg remove _p suffix)
[jra/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    
7
8      ** NOTE! The following LGPL license applies to the ldb
9      ** library. This does NOT imply that all of Samba is released
10      ** under the LGPL
11    
12    This library is free software; you can redistribute it and/or
13    modify it under the terms of the GNU Lesser General Public
14    License as published by the Free Software Foundation; either
15    version 2 of the License, or (at your option) any later version.
16
17    This library is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20    Lesser General Public License for more details.
21
22    You should have received a copy of the GNU Lesser General Public
23    License along with this library; if not, write to the Free Software
24    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25 */
26
27 /*
28  *  Name: ldb
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
38 #include "includes.h"
39 #include "ldb/include/ldb.h"
40 #include "ldb/include/ldb_private.h"
41 #include "ldb/ldb_tdb/ldb_tdb.h"
42
43 #define LDBLOCK "INT_LDBLOCK"
44
45 /*
46   form a TDB_DATA for a record key
47   caller frees
48
49   note that the key for a record can depend on whether the 
50   dn refers to a case sensitive index record or not
51 */
52 struct TDB_DATA ltdb_key(struct ldb_module *module, const char *dn)
53 {
54         struct ldb_context *ldb = module->ldb;
55         TDB_DATA key;
56         char *key_str = NULL;
57         char *dn_folded = NULL;
58         const char *prefix = LTDB_INDEX ":";
59         const char *s;
60         int flags;
61
62         /*
63           most DNs are case insensitive. The exception is index DNs for
64           case sensitive attributes
65
66           there are 3 cases dealt with in this code:
67
68           1) if the dn doesn't start with @INDEX: then uppercase whole dn
69           2) if the dn starts with @INDEX:attr and 'attr' is a case insensitive
70              attribute then uppercase whole dn
71           3) if the dn starts with @INDEX:attr and 'attr' is a case sensitive
72              attribute then uppercase up to the value of the attribute, but 
73              not the value itself
74         */
75         if (strncmp(dn, prefix, strlen(prefix)) == 0 &&
76             (s = strchr(dn+strlen(prefix), ':'))) {
77                 char *attr_name, *attr_name_folded;
78                 attr_name = talloc_strndup(ldb, dn+strlen(prefix), (s-(dn+strlen(prefix))));
79                 if (!attr_name) {
80                         goto failed;
81                 }
82                 flags = ltdb_attribute_flags(module, attr_name);
83                 
84                 if (flags & LTDB_FLAG_CASE_INSENSITIVE) {
85                         dn_folded = ldb_casefold(ldb, dn);
86                 } else {
87                         attr_name_folded = ldb_casefold(ldb, attr_name);
88                         if (!attr_name_folded) {
89                                 goto failed;
90                         }
91                         dn_folded = talloc_asprintf(ldb, "%s:%s:%s",
92                                                     prefix, attr_name_folded,
93                                                     s+1);
94                         talloc_free(attr_name_folded);
95                 }
96                 talloc_free(attr_name);
97         } else {
98                 dn_folded = ldb_casefold(ldb, dn);
99         }
100
101         if (!dn_folded) {
102                 goto failed;
103         }
104
105         key_str = talloc_asprintf(ldb, "DN=%s", dn_folded);
106         talloc_free(dn_folded);
107
108         if (!key_str) {
109                 goto failed;
110         }
111
112         key.dptr = key_str;
113         key.dsize = strlen(key_str)+1;
114
115         return key;
116
117 failed:
118         errno = ENOMEM;
119         key.dptr = NULL;
120         key.dsize = 0;
121         return key;
122 }
123
124 /*
125   lock the database for write - currently a single lock is used
126 */
127 static int ltdb_lock(struct ldb_module *module, const char *lockname)
128 {
129         struct ltdb_private *ltdb = module->private_data;
130         TDB_DATA key;
131         int ret;
132
133         if (lockname == NULL) {
134                 return -1;
135         }
136
137         key = ltdb_key(module, lockname);
138         if (!key.dptr) {
139                 return -1;
140         }
141
142         ret = tdb_chainlock(ltdb->tdb, key);
143
144         talloc_free(key.dptr);
145
146         return ret;
147 }
148
149 /*
150   unlock the database after a ltdb_lock()
151 */
152 static int ltdb_unlock(struct ldb_module *module, const char *lockname)
153 {
154         struct ltdb_private *ltdb = module->private_data;
155         TDB_DATA key;
156
157         if (lockname == NULL) {
158                 return -1;
159         }
160
161         key = ltdb_key(module, lockname);
162         if (!key.dptr) {
163                 return -1;
164         }
165
166         tdb_chainunlock(ltdb->tdb, key);
167
168         talloc_free(key.dptr);
169
170         return 0;
171 }
172
173
174 /*
175   we've made a modification to a dn - possibly reindex and 
176   update sequence number
177 */
178 static int ltdb_modified(struct ldb_module *module, const char *dn)
179 {
180         int ret = 0;
181
182         if (strcmp(dn, LTDB_INDEXLIST) == 0 ||
183             strcmp(dn, LTDB_ATTRIBUTES) == 0) {
184                 ret = ltdb_reindex(module);
185         }
186
187         if (ret == 0 &&
188             strcmp(dn, LTDB_BASEINFO) != 0) {
189                 ret = ltdb_increase_sequence_number(module);
190         }
191
192         return ret;
193 }
194
195 /*
196   store a record into the db
197 */
198 int ltdb_store(struct ldb_module *module, const struct ldb_message *msg, int flgs)
199 {
200         struct ltdb_private *ltdb = module->private_data;
201         TDB_DATA tdb_key, tdb_data;
202         int ret;
203
204         tdb_key = ltdb_key(module, msg->dn);
205         if (!tdb_key.dptr) {
206                 return -1;
207         }
208
209         ret = ltdb_pack_data(module, msg, &tdb_data);
210         if (ret == -1) {
211                 talloc_free(tdb_key.dptr);
212                 return -1;
213         }
214
215         ret = tdb_store(ltdb->tdb, tdb_key, tdb_data, flgs);
216         if (ret == -1) {
217                 goto done;
218         }
219         
220         ret = ltdb_index_add(module, msg);
221         if (ret == -1) {
222                 tdb_delete(ltdb->tdb, tdb_key);
223         }
224
225 done:
226         talloc_free(tdb_key.dptr);
227         talloc_free(tdb_data.dptr);
228
229         return ret;
230 }
231
232
233 /*
234   add a record to the database
235 */
236 static int ltdb_add(struct ldb_module *module, const struct ldb_message *msg)
237 {
238         struct ltdb_private *ltdb = module->private_data;
239         int ret;
240
241         ltdb->last_err_string = NULL;
242
243         if (ltdb_lock(module, LDBLOCK) != 0) {
244                 return -1;
245         }
246
247         if (ltdb_cache_load(module) != 0) {
248                 ltdb_unlock(module, LDBLOCK);
249                 return -1;
250         }
251         
252         ret = ltdb_store(module, msg, TDB_INSERT);
253
254         if (ret == 0) {
255                 ltdb_modified(module, msg->dn);
256         }
257
258         ltdb_unlock(module, LDBLOCK);
259         return ret;
260 }
261
262
263 /*
264   delete a record from the database, not updating indexes (used for deleting
265   index records)
266 */
267 int ltdb_delete_noindex(struct ldb_module *module, const char *dn)
268 {
269         struct ltdb_private *ltdb = module->private_data;
270         TDB_DATA tdb_key;
271         int ret;
272
273         tdb_key = ltdb_key(module, dn);
274         if (!tdb_key.dptr) {
275                 return -1;
276         }
277
278         ret = tdb_delete(ltdb->tdb, tdb_key);
279         talloc_free(tdb_key.dptr);
280
281         return ret;
282 }
283
284 /*
285   delete a record from the database
286 */
287 static int ltdb_delete(struct ldb_module *module, const char *dn)
288 {
289         struct ltdb_private *ltdb = module->private_data;
290         int ret;
291         struct ldb_message *msg = NULL;
292
293         ltdb->last_err_string = NULL;
294
295         if (ltdb_lock(module, LDBLOCK) != 0) {
296                 return -1;
297         }
298
299         if (ltdb_cache_load(module) != 0) {
300                 goto failed;
301         }
302
303         msg = talloc(module, struct ldb_message);
304         if (msg == NULL) {
305                 goto failed;
306         }
307
308         /* in case any attribute of the message was indexed, we need
309            to fetch the old record */
310         ret = ltdb_search_dn1(module, dn, msg);
311         if (ret != 1) {
312                 /* not finding the old record is an error */
313                 goto failed;
314         }
315
316         ret = ltdb_delete_noindex(module, dn);
317         if (ret == -1) {
318                 goto failed;
319         }
320
321         /* remove any indexed attributes */
322         ret = ltdb_index_del(module, msg);
323
324         if (ret == 0) {
325                 ltdb_modified(module, dn);
326         }
327
328         talloc_free(msg);
329         ltdb_unlock(module, LDBLOCK);
330         return ret;
331
332 failed:
333         talloc_free(msg);
334         ltdb_unlock(module, LDBLOCK);
335         return -1;
336 }
337
338
339 /*
340   find an element by attribute name. At the moment this does a linear search, it should
341   be re-coded to use a binary search once all places that modify records guarantee
342   sorted order
343
344   return the index of the first matching element if found, otherwise -1
345 */
346 static int find_element(const struct ldb_message *msg, const char *name)
347 {
348         unsigned int i;
349         for (i=0;i<msg->num_elements;i++) {
350                 if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
351                         return i;
352                 }
353         }
354         return -1;
355 }
356
357
358 /*
359   add an element to an existing record. Assumes a elements array that we
360   can call re-alloc on, and assumed that we can re-use the data pointers from the 
361   passed in additional values. Use with care!
362
363   returns 0 on success, -1 on failure (and sets errno)
364 */
365 static int msg_add_element(struct ldb_context *ldb,
366                            struct ldb_message *msg, struct ldb_message_element *el)
367 {
368         struct ldb_message_element *e2;
369         unsigned int i;
370
371         e2 = talloc_realloc(msg, msg->elements, struct ldb_message_element, 
372                               msg->num_elements+1);
373         if (!e2) {
374                 errno = ENOMEM;
375                 return -1;
376         }
377
378         msg->elements = e2;
379
380         e2 = &msg->elements[msg->num_elements];
381
382         e2->name = el->name;
383         e2->flags = el->flags;
384         e2->values = NULL;
385         if (el->num_values != 0) {
386                 e2->values = talloc_array(msg->elements, struct ldb_val, el->num_values);
387                 if (!e2->values) {
388                         errno = ENOMEM;
389                         return -1;
390                 }
391         }
392         for (i=0;i<el->num_values;i++) {
393                 e2->values[i] = el->values[i];
394         }
395         e2->num_values = el->num_values;
396
397         msg->num_elements++;
398
399         return 0;
400 }
401
402 /*
403   delete all elements having a specified attribute name
404 */
405 static int msg_delete_attribute(struct ldb_module *module,
406                                 struct ldb_context *ldb,
407                                 struct ldb_message *msg, const char *name)
408 {
409         unsigned int i, j;
410
411         for (i=0;i<msg->num_elements;i++) {
412                 if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
413                         for (j=0;j<msg->elements[i].num_values;j++) {
414                                 ltdb_index_del_value(module, msg->dn, &msg->elements[i], j);
415                         }
416                         talloc_free(msg->elements[i].values);
417                         if (msg->num_elements > (i+1)) {
418                                 memmove(&msg->elements[i], 
419                                         &msg->elements[i+1], 
420                                         sizeof(struct ldb_message_element)*
421                                         (msg->num_elements - (i+1)));
422                         }
423                         msg->num_elements--;
424                         i--;
425                         msg->elements = talloc_realloc(msg, msg->elements, 
426                                                          struct ldb_message_element, 
427                                                          msg->num_elements);
428                 }
429         }
430
431         return 0;
432 }
433
434 /*
435   delete all elements matching an attribute name/value 
436
437   return 0 on success, -1 on failure
438 */
439 static int msg_delete_element(struct ldb_module *module,
440                               struct ldb_message *msg, 
441                               const char *name,
442                               const struct ldb_val *val)
443 {
444         struct ldb_context *ldb = module->ldb;
445         unsigned int i;
446         int found;
447         struct ldb_message_element *el;
448
449         found = find_element(msg, name);
450         if (found == -1) {
451                 return -1;
452         }
453
454         el = &msg->elements[found];
455
456         for (i=0;i<el->num_values;i++) {
457                 if (ltdb_val_equal(module, msg->elements[i].name, &el->values[i], val)) {
458                         if (i<el->num_values-1) {
459                                 memmove(&el->values[i], &el->values[i+1],
460                                         sizeof(el->values[i])*(el->num_values-(i+1)));
461                         }
462                         el->num_values--;
463                         if (el->num_values == 0) {
464                                 return msg_delete_attribute(module, ldb, msg, name);
465                         }
466                         return 0;
467                 }
468         }
469
470         return -1;
471 }
472
473
474 /*
475   modify a record - internal interface
476
477   yuck - this is O(n^2). Luckily n is usually small so we probably
478   get away with it, but if we ever have really large attribute lists 
479   then we'll need to look at this again
480 */
481 int ltdb_modify_internal(struct ldb_module *module, const struct ldb_message *msg)
482 {
483         struct ldb_context *ldb = module->ldb;
484         struct ltdb_private *ltdb = module->private_data;
485         TDB_DATA tdb_key, tdb_data;
486         struct ldb_message *msg2;
487         unsigned i, j;
488         int ret;
489
490         tdb_key = ltdb_key(module, msg->dn);
491         if (!tdb_key.dptr) {
492                 return -1;
493         }
494
495         tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
496         if (!tdb_data.dptr) {
497                 talloc_free(tdb_key.dptr);
498                 return -1;
499         }
500
501         msg2 = talloc(tdb_key.dptr, struct ldb_message);
502         if (msg2 == NULL) {
503                 talloc_free(tdb_key.dptr);
504                 return -1;
505         }
506
507         ret = ltdb_unpack_data(module, &tdb_data, msg2);
508         if (ret == -1) {
509                 talloc_free(tdb_key.dptr);
510                 free(tdb_data.dptr);
511                 return -1;
512         }
513
514         if (!msg2->dn) {
515                 msg2->dn = msg->dn;
516         }
517
518         for (i=0;i<msg->num_elements;i++) {
519                 struct ldb_message_element *el = &msg->elements[i];
520                 struct ldb_message_element *el2;
521                 struct ldb_val *vals;
522
523                 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
524
525                 case LDB_FLAG_MOD_ADD:
526                         /* add this element to the message. fail if it
527                            already exists */
528                         ret = find_element(msg2, el->name);
529
530                         if (ret == -1) {
531                                 if (msg_add_element(ldb, msg2, el) != 0) {
532                                         goto failed;
533                                 }
534                                 continue;
535                         }
536
537                         el2 = &msg2->elements[ret];
538
539                         /* An attribute with this name already exists, add all
540                          * values if they don't already exist. */
541
542                         for (j=0;j<el->num_values;j++) {
543                                 if (ldb_msg_find_val(el2, &el->values[j])) {
544                                         ltdb->last_err_string =
545                                                 "Type or value exists";
546                                         goto failed;
547                                 }
548                         }
549
550                         vals = talloc_realloc(msg2->elements, el2->values, struct ldb_val,
551                                                 el2->num_values + el->num_values);
552
553                         if (vals == NULL)
554                                 goto failed;
555
556                         for (j=0;j<el->num_values;j++) {
557                                 vals[el2->num_values + j] =
558                                         ldb_val_dup(vals, &el->values[j]);
559                         }
560
561                         el2->values = vals;
562                         el2->num_values += el->num_values;
563
564                         break;
565
566                 case LDB_FLAG_MOD_REPLACE:
567                         /* replace all elements of this attribute name with the elements
568                            listed. The attribute not existing is not an error */
569                         msg_delete_attribute(module, ldb, msg2, msg->elements[i].name);
570
571                         /* add the replacement element, if not empty */
572                         if (msg->elements[i].num_values != 0 &&
573                             msg_add_element(ldb, msg2, &msg->elements[i]) != 0) {
574                                 goto failed;
575                         }
576                         break;
577
578                 case LDB_FLAG_MOD_DELETE:
579                         /* we could be being asked to delete all
580                            values or just some values */
581                         if (msg->elements[i].num_values == 0) {
582                                 if (msg_delete_attribute(module, ldb, msg2, 
583                                                          msg->elements[i].name) != 0) {
584                                         ltdb->last_err_string = "No such attribute";
585                                         goto failed;
586                                 }
587                                 break;
588                         }
589                         for (j=0;j<msg->elements[i].num_values;j++) {
590                                 if (msg_delete_element(module,
591                                                        msg2, 
592                                                        msg->elements[i].name,
593                                                        &msg->elements[i].values[j]) != 0) {
594                                         ltdb->last_err_string = "No such attribute";
595                                         goto failed;
596                                 }
597                                 if (ltdb_index_del_value(module, msg->dn, &msg->elements[i], j) != 0) {
598                                         goto failed;
599                                 }
600                         }
601                         break;
602                 }
603         }
604
605         /* we've made all the mods - save the modified record back into the database */
606         ret = ltdb_store(module, msg2, TDB_MODIFY);
607
608         talloc_free(tdb_key.dptr);
609         free(tdb_data.dptr);
610         return ret;
611
612 failed:
613         talloc_free(tdb_key.dptr);
614         free(tdb_data.dptr);
615         return -1;
616 }
617
618 /*
619   modify a record
620 */
621 static int ltdb_modify(struct ldb_module *module, const struct ldb_message *msg)
622 {
623         struct ltdb_private *ltdb = module->private_data;
624         int ret;
625
626         ltdb->last_err_string = NULL;
627
628         if (ltdb_lock(module, LDBLOCK) != 0) {
629                 return -1;
630         }
631
632         if (ltdb_cache_load(module) != 0) {
633                 ltdb_unlock(module, LDBLOCK);
634                 return -1;
635         }
636
637         ret = ltdb_modify_internal(module, msg);
638
639         if (ret == 0) {
640                 ltdb_modified(module, msg->dn);
641         }
642
643         ltdb_unlock(module, LDBLOCK);
644
645         return ret;
646 }
647
648 /*
649   rename a record
650 */
651 static int ltdb_rename(struct ldb_module *module, const char *olddn, const char *newdn)
652 {
653         struct ltdb_private *ltdb = module->private_data;
654         int ret;
655         struct ldb_message *msg;
656         const char *error_str;
657
658         ltdb->last_err_string = NULL;
659
660         if (ltdb_lock(module, LDBLOCK) != 0) {
661                 return -1;
662         }
663
664         msg = talloc(module, struct ldb_message);
665         if (msg == NULL) {
666                 goto failed;
667         }
668
669         /* in case any attribute of the message was indexed, we need
670            to fetch the old record */
671         ret = ltdb_search_dn1(module, olddn, msg);
672         if (ret != 1) {
673                 /* not finding the old record is an error */
674                 goto failed;
675         }
676
677         msg->dn = talloc_strdup(msg, newdn);
678         if (!msg->dn) {
679                 goto failed;
680         }
681
682         ret = ltdb_add(module, msg);
683         if (ret == -1) {
684                 goto failed;
685         }
686
687         ret = ltdb_delete(module, olddn);
688         error_str = ltdb->last_err_string;
689         if (ret == -1) {
690                 ltdb_delete(module, newdn);
691         }
692
693         ltdb->last_err_string = error_str;
694
695         talloc_free(msg);
696         ltdb_unlock(module, LDBLOCK);
697
698         return ret;
699
700 failed:
701         talloc_free(msg);
702         ltdb_unlock(module, LDBLOCK);
703         return -1;
704 }
705
706 /*
707   close database
708 */
709 static int ltdb_close(struct ldb_module *module)
710 {
711         struct ldb_context *ldb = module->ldb;
712         talloc_free(ldb);
713         return 0;
714 }
715                       
716
717 /*
718   return extended error information
719 */
720 static const char *ltdb_errstring(struct ldb_module *module)
721 {
722         struct ltdb_private *ltdb = module->private_data;
723         if (ltdb->last_err_string) {
724                 return ltdb->last_err_string;
725         }
726         return tdb_errorstr(ltdb->tdb);
727 }
728
729
730 static const struct ldb_module_ops ltdb_ops = {
731         "tdb",
732         ltdb_close, 
733         ltdb_search,
734         ltdb_search_free,
735         ltdb_add,
736         ltdb_modify,
737         ltdb_delete,
738         ltdb_rename,
739         ltdb_lock,
740         ltdb_unlock,
741         ltdb_errstring
742 };
743
744
745 /*
746   destroy the ltdb context
747 */
748 static int ltdb_destructor(void *p)
749 {
750         struct ltdb_private *ltdb = p;
751         tdb_close(ltdb->tdb);
752         return 0;
753 }
754
755 /*
756   connect to the database
757 */
758 struct ldb_context *ltdb_connect(const char *url, 
759                                  unsigned int flags, 
760                                  const char *options[])
761 {
762         const char *path;
763         int tdb_flags, open_flags;
764         struct ltdb_private *ltdb;
765         TDB_CONTEXT *tdb;
766         struct ldb_context *ldb;
767
768         ldb = talloc_zero(NULL, struct ldb_context);
769         if (!ldb) {
770                 errno = ENOMEM;
771                 return NULL;
772         }
773
774         /* parse the url */
775         if (strchr(url, ':')) {
776                 if (strncmp(url, "tdb://", 6) != 0) {
777                         errno = EINVAL;
778                         talloc_free(ldb);
779                         return NULL;
780                 }
781                 path = url+6;
782         } else {
783                 path = url;
784         }
785
786         tdb_flags = TDB_DEFAULT;
787
788         if (flags & LDB_FLG_RDONLY) {
789                 open_flags = O_RDONLY;
790         } else {
791                 open_flags = O_CREAT | O_RDWR;
792         }
793
794         /* note that we use quite a large default hash size */
795         tdb = tdb_open(path, 10000, tdb_flags, open_flags, 0666);
796         if (!tdb) {
797                 talloc_free(ldb);
798                 return NULL;
799         }
800
801         ltdb = talloc_zero(ldb, struct ltdb_private);
802         if (!ltdb) {
803                 tdb_close(tdb);
804                 talloc_free(ldb);
805                 errno = ENOMEM;
806                 return NULL;
807         }
808
809         ltdb->tdb = tdb;
810         ltdb->sequence_number = 0;
811
812         talloc_set_destructor(ltdb, ltdb_destructor);
813
814         ldb->modules = talloc(ldb, struct ldb_module);
815         if (!ldb->modules) {
816                 talloc_free(ldb);
817                 errno = ENOMEM;
818                 return NULL;
819         }
820         ldb->modules->ldb = ldb;
821         ldb->modules->prev = ldb->modules->next = NULL;
822         ldb->modules->private_data = ltdb;
823         ldb->modules->ops = &ltdb_ops;
824
825         return ldb;
826 }