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