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