r15854: more talloc_set_destructor() typesafe fixes
[bbaumbach/samba-autobuild/.git] / source4 / lib / ldb / ldb_sqlite3 / ldb_sqlite3.c
1 /* 
2    ldb database library
3    
4    Copyright (C) Derrell Lipman  2005
5    Copyright (C) Simo Sorce 2005
6    
7    ** NOTE! The following LGPL license applies to the ldb
8    ** library. This does NOT imply that all of Samba is released
9    ** under the LGPL
10    
11    This library is free software; you can redistribute it and/or
12    modify it under the terms of the GNU Lesser General Public
13    License as published by the Free Software Foundation; either
14    version 2 of the License, or (at your option) any later version.
15    
16    This library is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19    Lesser General Public License for more details.
20    
21    You should have received a copy of the GNU Lesser General Public
22    License along with this library; if not, write to the Free Software
23    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24 */
25
26 /*
27  *  Name: ldb
28  *
29  *  Component: ldb sqlite3 backend
30  *
31  *  Description: core files for SQLITE3 backend
32  *
33  *  Author: Derrell Lipman (based on Andrew Tridgell's LDAP backend)
34  */
35
36 #include "includes.h"
37 #include "ldb/include/includes.h"
38
39 #include <sqlite3.h>
40
41 struct lsqlite3_private {
42         int trans_count;
43         char **options;
44         sqlite3 *sqlite;
45 };
46
47 struct lsql_async_context {
48         struct ldb_module *module;
49
50         /* search stuff */
51         long long current_eid;
52         const char * const * attrs;
53         struct ldb_async_result *ares;
54
55         /* async stuff */
56         void *context;
57         int (*callback)(struct ldb_context *, void *, struct ldb_async_result *);
58 };
59
60 static struct ldb_async_handle *init_handle(struct lsqlite3_private *lsqlite3, struct ldb_module *module,
61                                             void *context,
62                                             int (*callback)(struct ldb_context *, void *, struct ldb_async_result *))
63 {
64         struct lsql_async_context *ac;
65         struct ldb_async_handle *h;
66
67         h = talloc_zero(lsqlite3, struct ldb_async_handle);
68         if (h == NULL) {
69                 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
70                 return NULL;
71         }
72
73         h->module = module;
74
75         ac = talloc(h, struct lsql_async_context);
76         if (ac == NULL) {
77                 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
78                 talloc_free(h);
79                 return NULL;
80         }
81
82         h->private_data = (void *)ac;
83
84         h->state = LDB_ASYNC_INIT;
85         h->status = LDB_SUCCESS;
86
87         ac->module = module;
88         ac->context = context;
89         ac->callback = callback;
90
91         return h;
92 }
93
94 /*
95  * Macros used throughout
96  */
97
98 #ifndef FALSE
99 # define FALSE  (0)
100 # define TRUE   (! FALSE)
101 #endif
102
103 #define RESULT_ATTR_TABLE       "temp_result_attrs"
104
105 //#define TEMPTAB                 /* for testing, create non-temporary table */
106 #define TEMPTAB                 "TEMPORARY"
107
108 /*
109  * Static variables
110  */
111 sqlite3_stmt *  stmtGetEID = NULL;
112
113 static char *lsqlite3_tprintf(TALLOC_CTX *mem_ctx, const char *fmt, ...)
114 {
115         char *str, *ret;
116         va_list ap;
117
118         va_start(ap, fmt);
119         str = sqlite3_vmprintf(fmt, ap);
120         va_end(ap);
121
122         if (str == NULL) return NULL;
123
124         ret = talloc_strdup(mem_ctx, str);
125         if (ret == NULL) {
126                 sqlite3_free(str);
127                 return NULL;
128         }
129
130         sqlite3_free(str);
131         return ret;
132 }
133
134 static char base160tab[161] = {
135         48 ,49 ,50 ,51 ,52 ,53 ,54 ,55 ,56 ,57 , /* 0-9 */
136         58 ,59 ,65 ,66 ,67 ,68 ,69 ,70 ,71 ,72 , /* : ; A-H */
137         73 ,74 ,75 ,76 ,77 ,78 ,79 ,80 ,81 ,82 , /* I-R */
138         83 ,84 ,85 ,86 ,87 ,88 ,89 ,90 ,97 ,98 , /* S-Z , a-b */
139         99 ,100,101,102,103,104,105,106,107,108, /* c-l */
140         109,110,111,112,113,114,115,116,117,118, /* m-v */
141         119,120,121,122,160,161,162,163,164,165, /* w-z, latin1 */
142         166,167,168,169,170,171,172,173,174,175, /* latin1 */
143         176,177,178,179,180,181,182,183,184,185, /* latin1 */
144         186,187,188,189,190,191,192,193,194,195, /* latin1 */
145         196,197,198,199,200,201,202,203,204,205, /* latin1 */
146         206,207,208,209,210,211,212,213,214,215, /* latin1 */
147         216,217,218,219,220,221,222,223,224,225, /* latin1 */
148         226,227,228,229,230,231,232,233,234,235, /* latin1 */
149         236,237,238,239,240,241,242,243,244,245, /* latin1 */
150         246,247,248,249,250,251,252,253,254,255, /* latin1 */
151         '\0'
152 };
153
154
155 /*
156  * base160()
157  *
158  * Convert an unsigned long integer into a base160 representation of the
159  * number.
160  *
161  * Parameters:
162  *   val --
163  *     value to be converted
164  *
165  *   result --
166  *     character array, 5 bytes long, into which the base160 representation
167  *     will be placed.  The result will be a four-digit representation of the
168  *     number (with leading zeros prepended as necessary), and null
169  *     terminated.
170  *
171  * Returns:
172  *   Nothing
173  */
174 static void
175 base160_sql(sqlite3_context * hContext,
176             int argc,
177             sqlite3_value ** argv)
178 {
179     int             i;
180     long long       val;
181     char            result[5];
182
183     val = sqlite3_value_int64(argv[0]);
184
185     for (i = 3; i >= 0; i--) {
186         
187         result[i] = base160tab[val % 160];
188         val /= 160;
189     }
190
191     result[4] = '\0';
192
193     sqlite3_result_text(hContext, result, -1, SQLITE_TRANSIENT);
194 }
195
196
197 /*
198  * base160next_sql()
199  *
200  * This function enhances sqlite by adding a "base160_next()" function which is
201  * accessible via queries.
202  *
203  * Retrieve the next-greater number in the base160 sequence for the terminal
204  * tree node (the last four digits).  Only one tree level (four digits) is
205  * operated on.
206  *
207  * Input:
208  *   A character string: either an empty string (in which case no operation is
209  *   performed), or a string of base160 digits with a length of a multiple of
210  *   four digits.
211  *
212  * Output:
213  *   Upon return, the trailing four digits (one tree level) will have been
214  *   incremented by 1.
215  */
216 static void
217 base160next_sql(sqlite3_context * hContext,
218                 int argc,
219                 sqlite3_value ** argv)
220 {
221         int                         i;
222         int                         len;
223         char *             pTab;
224         char *             pBase160 = strdup((const char *)sqlite3_value_text(argv[0]));
225         char *             pStart = pBase160;
226
227         /*
228          * We need a minimum of four digits, and we will always get a multiple
229          * of four digits.
230          */
231         if (pBase160 != NULL &&
232             (len = strlen(pBase160)) >= 4 &&
233             len % 4 == 0) {
234
235                 if (pBase160 == NULL) {
236
237                         sqlite3_result_null(hContext);
238                         return;
239                 }
240
241                 pBase160 += strlen(pBase160) - 1;
242
243                 /* We only carry through four digits: one level in the tree */
244                 for (i = 0; i < 4; i++) {
245
246                         /* What base160 value does this digit have? */
247                         pTab = strchr(base160tab, *pBase160);
248
249                         /* Is there a carry? */
250                         if (pTab < base160tab + sizeof(base160tab) - 1) {
251
252                                 /*
253                                  * Nope.  Just increment this value and we're
254                                  * done.
255                                  */
256                                 *pBase160 = *++pTab;
257                                 break;
258                         } else {
259
260                                 /*
261                                  * There's a carry.  This value gets
262                                  * base160tab[0], we decrement the buffer
263                                  * pointer to get the next higher-order digit,
264                                  * and continue in the loop.
265                                  */
266                                 *pBase160-- = base160tab[0];
267                         }
268                 }
269
270                 sqlite3_result_text(hContext,
271                                     pStart,
272                                     strlen(pStart),
273                                     free);
274         } else {
275                 sqlite3_result_value(hContext, argv[0]);
276                 if (pBase160 != NULL) {
277                         free(pBase160);
278                 }
279         }
280 }
281
282 static char *parsetree_to_sql(struct ldb_module *module,
283                               void *mem_ctx,
284                               const struct ldb_parse_tree *t)
285 {
286         const struct ldb_attrib_handler *h;
287         struct ldb_val value, subval;
288         char *wild_card_string;
289         char *child, *tmp;
290         char *ret = NULL;
291         char *attr;
292         int i;
293
294
295         switch(t->operation) {
296         case LDB_OP_AND:
297
298                 tmp = parsetree_to_sql(module, mem_ctx, t->u.list.elements[0]);
299                 if (tmp == NULL) return NULL;
300
301                 for (i = 1; i < t->u.list.num_elements; i++) {
302
303                         child = parsetree_to_sql(module, mem_ctx, t->u.list.elements[i]);
304                         if (child == NULL) return NULL;
305
306                         tmp = talloc_asprintf_append(tmp, " INTERSECT %s ", child);
307                         if (tmp == NULL) return NULL;
308                 }
309
310                 ret = talloc_asprintf(mem_ctx, "SELECT * FROM ( %s )\n", tmp);
311
312                 return ret;
313                 
314         case LDB_OP_OR:
315
316                 tmp = parsetree_to_sql(module, mem_ctx, t->u.list.elements[0]);
317                 if (tmp == NULL) return NULL;
318
319                 for (i = 1; i < t->u.list.num_elements; i++) {
320
321                         child = parsetree_to_sql(module, mem_ctx, t->u.list.elements[i]);
322                         if (child == NULL) return NULL;
323
324                         tmp = talloc_asprintf_append(tmp, " UNION %s ", child);
325                         if (tmp == NULL) return NULL;
326                 }
327
328                 return talloc_asprintf(mem_ctx, "SELECT * FROM ( %s ) ", tmp);
329
330         case LDB_OP_NOT:
331
332                 child = parsetree_to_sql(module, mem_ctx, t->u.isnot.child);
333                 if (child == NULL) return NULL;
334
335                 return talloc_asprintf(mem_ctx,
336                                         "SELECT eid FROM ldb_entry "
337                                         "WHERE eid NOT IN ( %s ) ", child);
338
339         case LDB_OP_EQUALITY:
340                 /*
341                  * For simple searches, we want to retrieve the list of EIDs that
342                  * match the criteria.
343                 */
344                 attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
345                 if (attr == NULL) return NULL;
346                 h = ldb_attrib_handler(module->ldb, attr);
347
348                 /* Get a canonicalised copy of the data */
349                 h->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
350                 if (value.data == NULL) {
351                         return NULL;
352                 }
353
354                 if (strcasecmp(t->u.equality.attr, "objectclass") == 0) {
355                 /*
356                  * For object classes, we want to search for all objectclasses
357                  * that are subclasses as well.
358                 */
359                         return lsqlite3_tprintf(mem_ctx,
360                                         "SELECT eid  FROM ldb_attribute_values\n"
361                                         "WHERE norm_attr_name = 'OBJECTCLASS' "
362                                         "AND norm_attr_value IN\n"
363                                         "  (SELECT class_name FROM ldb_object_classes\n"
364                                         "   WHERE tree_key GLOB\n"
365                                         "     (SELECT tree_key FROM ldb_object_classes\n"
366                                         "      WHERE class_name = '%q'\n"
367                                         "     ) || '*'\n"
368                                         "  )\n", value.data);
369
370                 } else if (strcasecmp(t->u.equality.attr, "dn") == 0) {
371                         /* DN query is a special ldb case */
372                         char *cdn = ldb_dn_linearize_casefold(module->ldb,
373                                                               ldb_dn_explode(module->ldb,
374                                                               (const char *)value.data));
375
376                         return lsqlite3_tprintf(mem_ctx,
377                                                 "SELECT eid FROM ldb_entry "
378                                                 "WHERE norm_dn = '%q'", cdn);
379
380                 } else {
381                         /* A normal query. */
382                         return lsqlite3_tprintf(mem_ctx,
383                                                 "SELECT eid FROM ldb_attribute_values "
384                                                 "WHERE norm_attr_name = '%q' "
385                                                 "AND norm_attr_value = '%q'",
386                                                 attr,
387                                                 value.data);
388
389                 }
390
391         case LDB_OP_SUBSTRING:
392
393                 wild_card_string = talloc_strdup(mem_ctx,
394                                         (t->u.substring.start_with_wildcard)?"*":"");
395                 if (wild_card_string == NULL) return NULL;
396
397                 for (i = 0; t->u.substring.chunks[i]; i++) {
398                         wild_card_string = talloc_asprintf_append(wild_card_string, "%s*",
399                                                         t->u.substring.chunks[i]->data);
400                         if (wild_card_string == NULL) return NULL;
401                 }
402
403                 if ( ! t->u.substring.end_with_wildcard ) {
404                         /* remove last wildcard */
405                         wild_card_string[strlen(wild_card_string) - 1] = '\0';
406                 }
407
408                 attr = ldb_attr_casefold(mem_ctx, t->u.substring.attr);
409                 if (attr == NULL) return NULL;
410                 h = ldb_attrib_handler(module->ldb, attr);
411
412                 subval.data = (void *)wild_card_string;
413                 subval.length = strlen(wild_card_string) + 1;
414
415                 /* Get a canonicalised copy of the data */
416                 h->canonicalise_fn(module->ldb, mem_ctx, &(subval), &value);
417                 if (value.data == NULL) {
418                         return NULL;
419                 }
420
421                 return lsqlite3_tprintf(mem_ctx,
422                                         "SELECT eid FROM ldb_attribute_values "
423                                         "WHERE norm_attr_name = '%q' "
424                                         "AND norm_attr_value GLOB '%q'",
425                                         attr,
426                                         value.data);
427
428         case LDB_OP_GREATER:
429                 attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
430                 if (attr == NULL) return NULL;
431                 h = ldb_attrib_handler(module->ldb, attr);
432
433                 /* Get a canonicalised copy of the data */
434                 h->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
435                 if (value.data == NULL) {
436                         return NULL;
437                 }
438
439                 return lsqlite3_tprintf(mem_ctx,
440                                         "SELECT eid FROM ldb_attribute_values "
441                                         "WHERE norm_attr_name = '%q' "
442                                         "AND ldap_compare(norm_attr_value, '>=', '%q', '%q') ",
443                                         attr,
444                                         value.data,
445                                         attr);
446
447         case LDB_OP_LESS:
448                 attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
449                 if (attr == NULL) return NULL;
450                 h = ldb_attrib_handler(module->ldb, attr);
451
452                 /* Get a canonicalised copy of the data */
453                 h->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
454                 if (value.data == NULL) {
455                         return NULL;
456                 }
457
458                 return lsqlite3_tprintf(mem_ctx,
459                                         "SELECT eid FROM ldb_attribute_values "
460                                         "WHERE norm_attr_name = '%q' "
461                                         "AND ldap_compare(norm_attr_value, '<=', '%q', '%q') ",
462                                         attr,
463                                         value.data,
464                                         attr);
465
466         case LDB_OP_PRESENT:
467                 if (strcasecmp(t->u.present.attr, "dn") == 0) {
468                         return talloc_strdup(mem_ctx, "SELECT eid FROM ldb_entry");
469                 }
470
471                 attr = ldb_attr_casefold(mem_ctx, t->u.present.attr);
472                 if (attr == NULL) return NULL;
473
474                 return lsqlite3_tprintf(mem_ctx,
475                                         "SELECT eid FROM ldb_attribute_values "
476                                         "WHERE norm_attr_name = '%q' ",
477                                         attr);
478
479         case LDB_OP_APPROX:
480                 attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
481                 if (attr == NULL) return NULL;
482                 h = ldb_attrib_handler(module->ldb, attr);
483
484                 /* Get a canonicalised copy of the data */
485                 h->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
486                 if (value.data == NULL) {
487                         return NULL;
488                 }
489
490                 return lsqlite3_tprintf(mem_ctx,
491                                         "SELECT eid FROM ldb_attribute_values "
492                                         "WHERE norm_attr_name = '%q' "
493                                         "AND ldap_compare(norm_attr_value, '~%', 'q', '%q') ",
494                                         attr,
495                                         value.data,
496                                         attr);
497                 
498         case LDB_OP_EXTENDED:
499 #warning  "work out how to handle bitops"
500                 return NULL;
501
502         default:
503                 break;
504         };
505
506         /* should never occur */
507         abort();
508         return NULL;
509 }
510
511 /*
512  * query_int()
513  *
514  * This function is used for the common case of queries that return a single
515  * integer value.
516  *
517  * NOTE: If more than one value is returned by the query, all but the first
518  * one will be ignored.
519  */
520 static int
521 query_int(const struct lsqlite3_private * lsqlite3,
522           long long * pRet,
523           const char * pSql,
524           ...)
525 {
526         int             ret;
527         int             bLoop;
528         char *          p;
529         sqlite3_stmt *  pStmt;
530         va_list         args;
531         
532         /* Begin access to variable argument list */
533         va_start(args, pSql);
534         
535         /* Format the query */
536         if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
537                 return SQLITE_NOMEM;
538         }
539         
540         /*
541          * Prepare and execute the SQL statement.  Loop allows retrying on
542          * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
543          * requiring retrying the operation.
544          */
545         for (bLoop = TRUE; bLoop; ) {
546                 
547                 /* Compile the SQL statement into sqlite virtual machine */
548                 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
549                                            p,
550                                            -1,
551                                            &pStmt,
552                                            NULL)) == SQLITE_SCHEMA) {
553                         if (stmtGetEID != NULL) {
554                                 sqlite3_finalize(stmtGetEID);
555                                 stmtGetEID = NULL;
556                         }
557                         continue;
558                 } else if (ret != SQLITE_OK) {
559                         break;
560                 }
561                 
562                 /* One row expected */
563                 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
564                         if (stmtGetEID != NULL) {
565                                 sqlite3_finalize(stmtGetEID);
566                                 stmtGetEID = NULL;
567                         }
568                         (void) sqlite3_finalize(pStmt);
569                         continue;
570                 } else if (ret != SQLITE_ROW) {
571                         (void) sqlite3_finalize(pStmt);
572                         break;
573                 }
574                 
575                 /* Get the value to be returned */
576                 *pRet = sqlite3_column_int64(pStmt, 0);
577                 
578                 /* Free the virtual machine */
579                 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
580                         if (stmtGetEID != NULL) {
581                                 sqlite3_finalize(stmtGetEID);
582                                 stmtGetEID = NULL;
583                         }
584                         continue;
585                 } else if (ret != SQLITE_OK) {
586                         (void) sqlite3_finalize(pStmt);
587                         break;
588                 }
589                 
590                 /*
591                  * Normal condition is only one time through loop.  Loop is
592                  * rerun in error conditions, via "continue", above.
593                  */
594                 bLoop = FALSE;
595         }
596         
597         /* All done with variable argument list */
598         va_end(args);
599         
600
601         /* Free the memory we allocated for our query string */
602         sqlite3_free(p);
603         
604         return ret;
605 }
606
607 /*
608  * This is a bad hack to support ldap style comparisons whithin sqlite.
609  * val is the attribute in the row currently under test
610  * func is the desired test "<=" ">=" "~" ":"
611  * cmp is the value to compare against (eg: "test")
612  * attr is the attribute name the value of which we want to test
613  */
614
615 static void lsqlite3_compare(sqlite3_context *ctx, int argc,
616                                         sqlite3_value **argv)
617 {
618         struct ldb_context *ldb = (struct ldb_context *)sqlite3_user_data(ctx);
619         const char *val = (const char *)sqlite3_value_text(argv[0]);
620         const char *func = (const char *)sqlite3_value_text(argv[1]);
621         const char *cmp = (const char *)sqlite3_value_text(argv[2]);
622         const char *attr = (const char *)sqlite3_value_text(argv[3]);
623         const struct ldb_attrib_handler *h;
624         struct ldb_val valX;
625         struct ldb_val valY;
626         int ret;
627
628         switch (func[0]) {
629         /* greater */
630         case '>': /* >= */
631                 h = ldb_attrib_handler(ldb, attr);
632                 valX.data = (void *)cmp;
633                 valX.length = strlen(cmp);
634                 valY.data = (void *)val;
635                 valY.length = strlen(val);
636                 ret = h->comparison_fn(ldb, ldb, &valY, &valX);
637                 if (ret >= 0)
638                         sqlite3_result_int(ctx, 1);
639                 else
640                         sqlite3_result_int(ctx, 0);
641                 return;
642
643         /* lesser */
644         case '<': /* <= */
645                 h = ldb_attrib_handler(ldb, attr);
646                 valX.data = (void *)cmp;
647                 valX.length = strlen(cmp);
648                 valY.data = (void *)val;
649                 valY.length = strlen(val);
650                 ret = h->comparison_fn(ldb, ldb, &valY, &valX);
651                 if (ret <= 0)
652                         sqlite3_result_int(ctx, 1);
653                 else
654                         sqlite3_result_int(ctx, 0);
655                 return;
656
657         /* approx */
658         case '~':
659                 /* TODO */
660                 sqlite3_result_int(ctx, 0);
661                 return;
662
663         /* bitops */
664         case ':':
665                 /* TODO */
666                 sqlite3_result_int(ctx, 0);
667                 return;
668
669         default:
670                 break;
671         }
672
673         sqlite3_result_error(ctx, "Value must start with a special operation char (<>~:)!", -1);
674         return;
675 }
676
677
678 /* rename a record */
679 static int lsqlite3_safe_rollback(sqlite3 *sqlite)
680 {
681         char *errmsg;
682         int ret;
683
684         /* execute */
685         ret = sqlite3_exec(sqlite, "ROLLBACK;", NULL, NULL, &errmsg);
686         if (ret != SQLITE_OK) {
687                 if (errmsg) {
688                         printf("lsqlite3_safe_rollback: Error: %s\n", errmsg);
689                         free(errmsg);
690                 }
691                 return -1;
692         }
693
694         return 0;
695 }
696
697 /* return an eid as result */
698 static int lsqlite3_eid_callback(void *result, int col_num, char **cols, char **names)
699 {
700         long long *eid = (long long *)result;
701
702         if (col_num != 1) return SQLITE_ABORT;
703         if (strcasecmp(names[0], "eid") != 0) return SQLITE_ABORT;
704
705         *eid = atoll(cols[0]);
706         return SQLITE_OK;
707 }
708
709 /*
710  * add a single set of ldap message values to a ldb_message
711  */
712 static int lsqlite3_search_callback(void *result, int col_num, char **cols, char **names)
713 {
714         struct ldb_async_handle *handle = talloc_get_type(result, struct ldb_async_handle);
715         struct lsql_async_context *ac = talloc_get_type(handle->private_data, struct lsql_async_context);
716         struct ldb_message *msg;
717         long long eid;
718         int i;
719
720         /* eid, dn, attr_name, attr_value */
721         if (col_num != 4)
722                 return SQLITE_ABORT;
723
724         eid = atoll(cols[0]);
725
726         if (eid != ac->current_eid) { /* here begin a new entry */
727
728                 /* call the async callback for the last entry
729                  * except the first time */
730                 if (ac->current_eid != 0) {
731                         ac->ares->message = ldb_msg_canonicalize(ac->module->ldb, ac->ares->message);
732                         if (ac->ares->message == NULL)
733                                 return SQLITE_ABORT;
734                         
735                         handle->status = ac->callback(ac->module->ldb, ac->context, ac->ares);
736                         if (handle->status != LDB_SUCCESS)
737                                 return SQLITE_ABORT;
738                 }
739
740                 /* start over */
741                 ac->ares = talloc_zero(ac, struct ldb_async_result);
742                 if (!ac->ares)
743                         return SQLITE_ABORT;
744
745                 ac->ares->message = ldb_msg_new(ac->ares);
746                 if (!ac->ares->message)
747                         return SQLITE_ABORT;
748
749                 ac->ares->type = LDB_REPLY_ENTRY;
750                 ac->current_eid = eid;
751         }
752
753         msg = ac->ares->message;
754
755         if (msg->dn == NULL) {
756                 msg->dn = ldb_dn_explode(msg, cols[1]);
757                 if (msg->dn == NULL)
758                         return SQLITE_ABORT;
759         }
760
761         if (ac->attrs) {
762                 int found = 0;
763                 for (i = 0; ac->attrs[i]; i++) {
764                         if (strcasecmp(cols[2], ac->attrs[i]) == 0) {
765                                 found = 1;
766                                 break;
767                         }
768                 }
769                 if (!found) return SQLITE_OK;
770         }
771
772         if (ldb_msg_add_string(msg, cols[2], cols[3]) != 0) {
773                 return SQLITE_ABORT;
774         }
775
776         return SQLITE_OK;
777 }
778
779
780 /*
781  * lsqlite3_get_eid()
782  * lsqlite3_get_eid_ndn()
783  *
784  * These functions are used for the very common case of retrieving an EID value
785  * given a (normalized) DN.
786  */
787
788 static long long lsqlite3_get_eid_ndn(sqlite3 *sqlite, void *mem_ctx, const char *norm_dn)
789 {
790         char *errmsg;
791         char *query;
792         long long eid = -1;
793         long long ret;
794
795         /* get object eid */
796         query = lsqlite3_tprintf(mem_ctx, "SELECT eid "
797                                           "FROM ldb_entry "
798                                           "WHERE norm_dn = '%q';", norm_dn);
799         if (query == NULL) return -1;
800
801         ret = sqlite3_exec(sqlite, query, lsqlite3_eid_callback, &eid, &errmsg);
802         if (ret != SQLITE_OK) {
803                 if (errmsg) {
804                         printf("lsqlite3_get_eid: Fatal Error: %s\n", errmsg);
805                         free(errmsg);
806                 }
807                 return -1;
808         }
809
810         return eid;
811 }
812
813 static long long lsqlite3_get_eid(struct ldb_module *module, const struct ldb_dn *dn)
814 {
815         TALLOC_CTX *local_ctx;
816         struct lsqlite3_private *lsqlite3 = module->private_data;
817         long long eid = -1;
818         char *cdn;
819
820         /* ignore ltdb specials */
821         if (ldb_dn_is_special(dn)) {
822                 return -1;
823         }
824
825         /* create a local ctx */
826         local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_get_eid local context");
827         if (local_ctx == NULL) {
828                 return -1;
829         }
830
831         cdn = ldb_dn_linearize(local_ctx, ldb_dn_casefold(module->ldb, dn));
832         if (!cdn) goto done;
833
834         eid = lsqlite3_get_eid_ndn(lsqlite3->sqlite, local_ctx, cdn);
835
836 done:
837         talloc_free(local_ctx);
838         return eid;
839 }
840
841 /*
842  * Interface functions referenced by lsqlite3_ops
843  */
844
845 static int lsql_search_sync_callback(struct ldb_context *ldb, void *context, struct ldb_async_result *ares)
846 {
847         struct ldb_result *res = NULL;
848         
849         if (!context) {
850                 ldb_set_errstring(ldb, talloc_strdup(ldb, "NULL Context in callback"));
851                 goto error;
852         }       
853
854         res = *((struct ldb_result **)context);
855
856         if (!res || !ares) {
857                 goto error;
858         }
859
860         if (ares->type == LDB_REPLY_ENTRY) {
861                 res->msgs = talloc_realloc(res, res->msgs, struct ldb_message *, res->count + 2);
862                 if (! res->msgs) {
863                         goto error;
864                 }
865
866                 res->msgs[res->count + 1] = NULL;
867
868                 res->msgs[res->count] = talloc_steal(res->msgs, ares->message);
869                 if (! res->msgs[res->count]) {
870                         goto error;
871                 }
872
873                 res->count++;
874         } else {
875                 ldb_debug(ldb, LDB_DEBUG_ERROR, "unrecognized async reply in ltdb_search_sync_callback!\n");
876                 goto error;
877         }
878
879         talloc_free(ares);
880         return LDB_SUCCESS;
881
882 error:
883         if (ares) talloc_free(ares);
884         if (res) talloc_free(res);
885         if (context) *((struct ldb_result **)context) = NULL;
886         return LDB_ERR_OPERATIONS_ERROR;
887 }
888
889 /* search for matching records, by tree */
890 int lsql_search_async(struct ldb_module *module, const struct ldb_dn *base,
891                       enum ldb_scope scope, struct ldb_parse_tree *tree,
892                       const char * const *attrs,
893                       void *context,
894                       int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
895                       struct ldb_async_handle **handle)
896 {
897         struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
898         struct lsql_async_context *lsql_ac;
899         char *norm_basedn;
900         char *sqlfilter;
901         char *errmsg;
902         char *query = NULL;
903         int ret;
904
905         *handle = init_handle(lsqlite3, module, context, callback);
906         if (*handle == NULL) {
907                 talloc_free(*handle);
908                 return LDB_ERR_OPERATIONS_ERROR;
909         }
910
911         lsql_ac = talloc_get_type((*handle)->private_data, struct lsql_async_context);
912
913         if (base) {
914                 norm_basedn = ldb_dn_linearize(lsql_ac, ldb_dn_casefold(module->ldb, base));
915                 if (norm_basedn == NULL) {
916                         ret = LDB_ERR_INVALID_DN_SYNTAX;
917                         goto failed;
918                 }
919         } else norm_basedn = talloc_strdup(lsql_ac, "");
920
921         if (*norm_basedn == '\0' &&
922                 (scope == LDB_SCOPE_BASE || scope == LDB_SCOPE_ONELEVEL)) {
923                         ret = LDB_ERR_UNWILLING_TO_PERFORM;
924                         goto failed;
925                 }
926
927         /* Convert filter into a series of SQL conditions (constraints) */
928         sqlfilter = parsetree_to_sql(module, lsql_ac, tree);
929         
930         switch(scope) {
931         case LDB_SCOPE_DEFAULT:
932         case LDB_SCOPE_SUBTREE:
933                 if (*norm_basedn != '\0') {
934                         query = lsqlite3_tprintf(lsql_ac,
935                                 "SELECT entry.eid,\n"
936                                 "       entry.dn,\n"
937                                 "       av.attr_name,\n"
938                                 "       av.attr_value\n"
939                                 "  FROM ldb_entry AS entry\n"
940
941                                 "  LEFT OUTER JOIN ldb_attribute_values AS av\n"
942                                 "    ON av.eid = entry.eid\n"
943
944                                 "  WHERE entry.eid IN\n"
945                                 "    (SELECT DISTINCT ldb_entry.eid\n"
946                                 "       FROM ldb_entry\n"
947                                 "       WHERE (ldb_entry.norm_dn GLOB('*,%q')\n"
948                                 "       OR ldb_entry.norm_dn = '%q')\n"
949                                 "       AND ldb_entry.eid IN\n"
950                                 "         (%s)\n"
951                                 "    )\n"
952
953                                 "  ORDER BY entry.eid ASC;",
954                                 norm_basedn,
955                                 norm_basedn,
956                                 sqlfilter);
957                 } else {
958                         query = lsqlite3_tprintf(lsql_ac,
959                                 "SELECT entry.eid,\n"
960                                 "       entry.dn,\n"
961                                 "       av.attr_name,\n"
962                                 "       av.attr_value\n"
963                                 "  FROM ldb_entry AS entry\n"
964
965                                 "  LEFT OUTER JOIN ldb_attribute_values AS av\n"
966                                 "    ON av.eid = entry.eid\n"
967
968                                 "  WHERE entry.eid IN\n"
969                                 "    (SELECT DISTINCT ldb_entry.eid\n"
970                                 "       FROM ldb_entry\n"
971                                 "       WHERE ldb_entry.eid IN\n"
972                                 "         (%s)\n"
973                                 "    )\n"
974
975                                 "  ORDER BY entry.eid ASC;",
976                                 sqlfilter);
977                 }
978
979                 break;
980                 
981         case LDB_SCOPE_BASE:
982                 query = lsqlite3_tprintf(lsql_ac,
983                         "SELECT entry.eid,\n"
984                         "       entry.dn,\n"
985                         "       av.attr_name,\n"
986                         "       av.attr_value\n"
987                         "  FROM ldb_entry AS entry\n"
988
989                         "  LEFT OUTER JOIN ldb_attribute_values AS av\n"
990                         "    ON av.eid = entry.eid\n"
991
992                         "  WHERE entry.eid IN\n"
993                         "    (SELECT DISTINCT ldb_entry.eid\n"
994                         "       FROM ldb_entry\n"
995                         "       WHERE ldb_entry.norm_dn = '%q'\n"
996                         "         AND ldb_entry.eid IN\n"
997                         "           (%s)\n"
998                         "    )\n"
999
1000                         "  ORDER BY entry.eid ASC;",
1001                         norm_basedn,
1002                         sqlfilter);
1003                 break;
1004                 
1005         case LDB_SCOPE_ONELEVEL:
1006                 query = lsqlite3_tprintf(lsql_ac,
1007                         "SELECT entry.eid,\n"
1008                         "       entry.dn,\n"
1009                         "       av.attr_name,\n"
1010                         "       av.attr_value\n"
1011                         "  FROM ldb_entry AS entry\n"
1012
1013                         "  LEFT OUTER JOIN ldb_attribute_values AS av\n"
1014                         "    ON av.eid = entry.eid\n"
1015
1016                         "  WHERE entry.eid IN\n"
1017                         "    (SELECT DISTINCT ldb_entry.eid\n"
1018                         "       FROM ldb_entry\n"
1019                         "       WHERE norm_dn GLOB('*,%q')\n"
1020                         "         AND NOT norm_dn GLOB('*,*,%q')\n"
1021                         "         AND ldb_entry.eid IN\n(%s)\n"
1022                         "    )\n"
1023
1024                         "  ORDER BY entry.eid ASC;",
1025                         norm_basedn,
1026                         norm_basedn,
1027                         sqlfilter);
1028                 break;
1029         }
1030
1031         if (query == NULL) {
1032                 goto failed;
1033         }
1034
1035         /* * /
1036         printf ("%s\n", query);
1037         / * */
1038
1039         lsql_ac->current_eid = 0;
1040         lsql_ac->attrs = attrs;
1041         lsql_ac->ares = NULL;
1042
1043         (*handle)->state = LDB_ASYNC_PENDING;
1044
1045         ret = sqlite3_exec(lsqlite3->sqlite, query, lsqlite3_search_callback, *handle, &errmsg);
1046         if (ret != SQLITE_OK) {
1047                 if (errmsg) {
1048                         ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1049                         free(errmsg);
1050                 }
1051                 goto failed;
1052         }
1053
1054         /* complete the last message if any */
1055         if (lsql_ac->ares) {
1056                 lsql_ac->ares->message = ldb_msg_canonicalize(module->ldb, lsql_ac->ares->message);
1057                 if (lsql_ac->ares->message == NULL)
1058                         goto failed;
1059                         
1060                 (*handle)->status = lsql_ac->callback(module->ldb, lsql_ac->context, lsql_ac->ares);
1061                 if ((*handle)->status != LDB_SUCCESS)
1062                         goto failed;
1063         }
1064
1065         (*handle)->state = LDB_ASYNC_DONE;
1066
1067         return LDB_SUCCESS;
1068
1069 failed:
1070         talloc_free(*handle);
1071         return LDB_ERR_OPERATIONS_ERROR;
1072 }
1073
1074 static int lsql_search_bytree(struct ldb_module * module, const struct ldb_dn* base,
1075                               enum ldb_scope scope, struct ldb_parse_tree * tree,
1076                               const char * const * attrs, struct ldb_result ** res)
1077 {
1078         struct ldb_async_handle *handle;
1079         int ret;
1080
1081         *res = talloc_zero(module, struct ldb_result);
1082         if (! *res) {
1083                 return LDB_ERR_OPERATIONS_ERROR;
1084         }
1085
1086         ret = lsql_search_async(module, base, scope, tree, attrs,
1087                                 res, &lsql_search_sync_callback,
1088                                 &handle);
1089
1090         if (ret == LDB_SUCCESS) {
1091                 ret = ldb_async_wait(handle, LDB_WAIT_ALL);
1092                 talloc_free(handle);
1093         }
1094
1095         if (ret != LDB_SUCCESS) {
1096                 talloc_free(*res);
1097         }
1098
1099         return ret;
1100 }
1101
1102 /* add a record */
1103 static int lsql_add_async(struct ldb_module *module, struct ldb_message *msg,
1104                           void *context,
1105                           int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
1106                           struct ldb_async_handle **handle)
1107 {
1108         struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1109         struct lsql_async_context *lsql_ac;
1110         long long eid;
1111         char *dn, *ndn;
1112         char *errmsg;
1113         char *query;
1114         int i;
1115         int ret = LDB_ERR_OPERATIONS_ERROR;
1116
1117         *handle = init_handle(lsqlite3, module, context, callback);
1118         if (*handle == NULL) {
1119                 goto failed;
1120         }
1121         lsql_ac = talloc_get_type((*handle)->private_data, struct lsql_async_context);
1122         (*handle)->state = LDB_ASYNC_DONE;
1123         (*handle)->status = LDB_SUCCESS;
1124
1125         /* See if this is an ltdb special */
1126         if (ldb_dn_is_special(msg->dn)) {
1127                 struct ldb_dn *c;
1128
1129                 c = ldb_dn_explode(lsql_ac, "@SUBCLASSES");
1130                 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
1131 #warning "insert subclasses into object class tree"
1132                         ret = LDB_ERR_UNWILLING_TO_PERFORM;
1133                         goto failed;
1134                 }
1135
1136 /*
1137                 c = ldb_dn_explode(local_ctx, "@INDEXLIST");
1138                 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
1139 #warning "should we handle indexes somehow ?"
1140                         goto failed;
1141                 }
1142 */
1143                 /* Others are implicitly ignored */
1144                 return LDB_SUCCESS;
1145         }
1146
1147         /* create linearized and normalized dns */
1148         dn = ldb_dn_linearize(lsql_ac, msg->dn);
1149         ndn = ldb_dn_linearize(lsql_ac, ldb_dn_casefold(module->ldb, msg->dn));
1150         if (dn == NULL || ndn == NULL) {
1151                 ret = LDB_ERR_OTHER;
1152                 goto failed;
1153         }
1154
1155         query = lsqlite3_tprintf(lsql_ac,
1156                                    /* Add new entry */
1157                                    "INSERT OR ABORT INTO ldb_entry "
1158                                    "('dn', 'norm_dn') "
1159                                    "VALUES ('%q', '%q');",
1160                                 dn, ndn);
1161         if (query == NULL) {
1162                 ret = LDB_ERR_OTHER;
1163                 goto failed;
1164         }
1165
1166         ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1167         if (ret != SQLITE_OK) {
1168                 if (errmsg) {
1169                         ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1170                         free(errmsg);
1171                 }
1172                 ret = LDB_ERR_OTHER;
1173                 goto failed;
1174         }
1175
1176         eid = lsqlite3_get_eid_ndn(lsqlite3->sqlite, lsql_ac, ndn);
1177         if (eid == -1) {
1178                 ret = LDB_ERR_OTHER;
1179                 goto failed;
1180         }
1181
1182         for (i = 0; i < msg->num_elements; i++) {
1183                 const struct ldb_message_element *el = &msg->elements[i];
1184                 const struct ldb_attrib_handler *h;
1185                 char *attr;
1186                 int j;
1187
1188                 /* Get a case-folded copy of the attribute name */
1189                 attr = ldb_attr_casefold(lsql_ac, el->name);
1190                 if (attr == NULL) {
1191                         ret = LDB_ERR_OTHER;
1192                         goto failed;
1193                 }
1194
1195                 h = ldb_attrib_handler(module->ldb, el->name);
1196
1197                 /* For each value of the specified attribute name... */
1198                 for (j = 0; j < el->num_values; j++) {
1199                         struct ldb_val value;
1200                         char *insert;
1201
1202                         /* Get a canonicalised copy of the data */
1203                         h->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
1204                         if (value.data == NULL) {
1205                                 ret = LDB_ERR_OTHER;
1206                                 goto failed;
1207                         }
1208
1209                         insert = lsqlite3_tprintf(lsql_ac,
1210                                         "INSERT OR ROLLBACK INTO ldb_attribute_values "
1211                                         "('eid', 'attr_name', 'norm_attr_name',"
1212                                         " 'attr_value', 'norm_attr_value') "
1213                                         "VALUES ('%lld', '%q', '%q', '%q', '%q');",
1214                                         eid, el->name, attr,
1215                                         el->values[j].data, value.data);
1216                         if (insert == NULL) {
1217                                 ret = LDB_ERR_OTHER;
1218                                 goto failed;
1219                         }
1220
1221                         ret = sqlite3_exec(lsqlite3->sqlite, insert, NULL, NULL, &errmsg);
1222                         if (ret != SQLITE_OK) {
1223                                 if (errmsg) {
1224                                         ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1225                                         free(errmsg);
1226                                 }
1227                                 ret = LDB_ERR_OTHER;
1228                                 goto failed;
1229                         }
1230                 }
1231         }
1232
1233         if (lsql_ac->callback)
1234                 (*handle)->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1235         
1236         return LDB_SUCCESS;
1237
1238 failed:
1239         talloc_free(*handle);
1240         return ret;
1241 }
1242
1243 static int lsql_add(struct ldb_module *module, const struct ldb_message *msg)
1244 {
1245         struct ldb_async_handle *handle;
1246         int ret;
1247
1248         ret = lsql_add_async(module, msg, NULL, NULL, &handle);
1249
1250         if (ret != LDB_SUCCESS)
1251                 return ret;
1252
1253         ret = ldb_async_wait(handle, LDB_WAIT_ALL);
1254
1255         talloc_free(handle);
1256         return ret;
1257 }
1258
1259
1260 /* modify a record */
1261 static int lsql_modify_async(struct ldb_module *module, const struct ldb_message *msg,
1262                              void *context,
1263                              int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
1264                              struct ldb_async_handle **handle)
1265 {
1266         struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1267         struct lsql_async_context *lsql_ac;
1268         long long eid;
1269         char *errmsg;
1270         int i;
1271         int ret = LDB_ERR_OPERATIONS_ERROR;
1272
1273         *handle = init_handle(lsqlite3, module, context, callback);
1274         if (*handle == NULL) {
1275                 goto failed;
1276         }
1277         lsql_ac = talloc_get_type((*handle)->private_data, struct lsql_async_context);
1278         (*handle)->state = LDB_ASYNC_DONE;
1279         (*handle)->status = LDB_SUCCESS;
1280
1281         /* See if this is an ltdb special */
1282         if (ldb_dn_is_special(msg->dn)) {
1283                 struct ldb_dn *c;
1284
1285                 c = ldb_dn_explode(lsql_ac, "@SUBCLASSES");
1286                 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
1287 #warning "modify subclasses into object class tree"
1288                         ret = LDB_ERR_UNWILLING_TO_PERFORM;
1289                         goto failed;
1290                 }
1291
1292                 /* Others are implicitly ignored */
1293                 return LDB_SUCCESS;
1294         }
1295
1296         eid = lsqlite3_get_eid(module, msg->dn);
1297         if (eid == -1) {
1298                 ret = LDB_ERR_OTHER;
1299                 goto failed;
1300         }
1301
1302         for (i = 0; i < msg->num_elements; i++) {
1303                 const struct ldb_message_element *el = &msg->elements[i];
1304                 const struct ldb_attrib_handler *h;
1305                 int flags = el->flags & LDB_FLAG_MOD_MASK;
1306                 char *attr;
1307                 char *mod;
1308                 int j;
1309
1310                 /* Get a case-folded copy of the attribute name */
1311                 attr = ldb_attr_casefold(lsql_ac, el->name);
1312                 if (attr == NULL) {
1313                         ret = LDB_ERR_OTHER;
1314                         goto failed;
1315                 }
1316
1317                 h = ldb_attrib_handler(module->ldb, el->name);
1318
1319                 switch (flags) {
1320
1321                 case LDB_FLAG_MOD_REPLACE:
1322                         
1323                         /* remove all attributes before adding the replacements */
1324                         mod = lsqlite3_tprintf(lsql_ac,
1325                                                 "DELETE FROM ldb_attribute_values "
1326                                                 "WHERE eid = '%lld' "
1327                                                 "AND norm_attr_name = '%q';",
1328                                                 eid, attr);
1329                         if (mod == NULL) {
1330                                 ret = LDB_ERR_OTHER;
1331                                 goto failed;
1332                         }
1333
1334                         ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1335                         if (ret != SQLITE_OK) {
1336                                 if (errmsg) {
1337                                         ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1338                                         free(errmsg);
1339                                 }
1340                                 ret = LDB_ERR_OTHER;
1341                                 goto failed;
1342                         }
1343
1344                         /* MISSING break is INTENTIONAL */
1345
1346                 case LDB_FLAG_MOD_ADD:
1347 #warning "We should throw an error if no value is provided!"
1348                         /* For each value of the specified attribute name... */
1349                         for (j = 0; j < el->num_values; j++) {
1350                                 struct ldb_val value;
1351
1352                                 /* Get a canonicalised copy of the data */
1353                                 h->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
1354                                 if (value.data == NULL) {
1355                                         ret = LDB_ERR_OTHER;
1356                                         goto failed;
1357                                 }
1358
1359                                 mod = lsqlite3_tprintf(lsql_ac,
1360                                         "INSERT OR ROLLBACK INTO ldb_attribute_values "
1361                                         "('eid', 'attr_name', 'norm_attr_name',"
1362                                         " 'attr_value', 'norm_attr_value') "
1363                                         "VALUES ('%lld', '%q', '%q', '%q', '%q');",
1364                                         eid, el->name, attr,
1365                                         el->values[j].data, value.data);
1366
1367                                 if (mod == NULL) {
1368                                         ret = LDB_ERR_OTHER;
1369                                         goto failed;
1370                                 }
1371
1372                                 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1373                                 if (ret != SQLITE_OK) {
1374                                         if (errmsg) {
1375                                                 ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1376                                                 free(errmsg);
1377                                         }
1378                                         ret = LDB_ERR_OTHER;
1379                                         goto failed;
1380                                 }
1381                         }
1382
1383                         break;
1384
1385                 case LDB_FLAG_MOD_DELETE:
1386 #warning "We should throw an error if the attribute we are trying to delete does not exist!"
1387                         if (el->num_values == 0) {
1388                                 mod = lsqlite3_tprintf(lsql_ac,
1389                                                         "DELETE FROM ldb_attribute_values "
1390                                                         "WHERE eid = '%lld' "
1391                                                         "AND norm_attr_name = '%q';",
1392                                                         eid, attr);
1393                                 if (mod == NULL) {
1394                                         ret = LDB_ERR_OTHER;
1395                                         goto failed;
1396                                 }
1397
1398                                 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1399                                 if (ret != SQLITE_OK) {
1400                                         if (errmsg) {
1401                                                 ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1402                                                 free(errmsg);
1403                                         }
1404                                         ret = LDB_ERR_OTHER;
1405                                         goto failed;
1406                                 }
1407                         }
1408
1409                         /* For each value of the specified attribute name... */
1410                         for (j = 0; j < el->num_values; j++) {
1411                                 struct ldb_val value;
1412
1413                                 /* Get a canonicalised copy of the data */
1414                                 h->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
1415                                 if (value.data == NULL) {
1416                                         ret = LDB_ERR_OTHER;
1417                                         goto failed;
1418                                 }
1419
1420                                 mod = lsqlite3_tprintf(lsql_ac,
1421                                         "DELETE FROM ldb_attribute_values "
1422                                         "WHERE eid = '%lld' "
1423                                         "AND norm_attr_name = '%q' "
1424                                         "AND norm_attr_value = '%q';",
1425                                         eid, attr, value.data);
1426
1427                                 if (mod == NULL) {
1428                                         ret = LDB_ERR_OTHER;
1429                                         goto failed;
1430                                 }
1431
1432                                 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1433                                 if (ret != SQLITE_OK) {
1434                                         if (errmsg) {
1435                                                 ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1436                                                 free(errmsg);
1437                                         }
1438                                         ret = LDB_ERR_OTHER;
1439                                         goto failed;
1440                                 }
1441                         }
1442
1443                         break;
1444                 }
1445         }
1446
1447         if (lsql_ac->callback)
1448                 (*handle)->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1449         
1450         return LDB_SUCCESS;
1451
1452 failed:
1453         talloc_free(*handle);
1454         return ret;
1455 }
1456
1457 static int lsql_modify(struct ldb_module *module, const struct ldb_message *msg)
1458 {
1459         struct ldb_async_handle *handle;
1460         int ret;
1461
1462         ret = lsql_modify_async(module, msg, NULL, NULL, &handle);
1463
1464         if (ret != LDB_SUCCESS)
1465                 return ret;
1466
1467         ret = ldb_async_wait(handle, LDB_WAIT_ALL);
1468
1469         talloc_free(handle);
1470         return ret;
1471 }
1472
1473 /* delete a record */
1474 static int lsql_delete_async(struct ldb_module *module, const struct ldb_dn *dn,
1475                              void *context,
1476                              int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
1477                              struct ldb_async_handle **handle)
1478 {
1479         struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1480         struct lsql_async_context *lsql_ac;
1481         long long eid;
1482         char *errmsg;
1483         char *query;
1484         int ret = LDB_ERR_OPERATIONS_ERROR;
1485
1486
1487         *handle = init_handle(lsqlite3, module, context, callback);
1488         if (*handle == NULL) {
1489                 goto failed;
1490         }
1491         lsql_ac = talloc_get_type((*handle)->private_data, struct lsql_async_context);
1492         (*handle)->state = LDB_ASYNC_DONE;
1493         (*handle)->status = LDB_SUCCESS;
1494
1495         eid = lsqlite3_get_eid(module, dn);
1496         if (eid == -1) {
1497                 goto failed;
1498         }
1499
1500         query = lsqlite3_tprintf(lsql_ac,
1501                                    /* Delete entry */
1502                                    "DELETE FROM ldb_entry WHERE eid = %lld; "
1503                                    /* Delete attributes */
1504                                    "DELETE FROM ldb_attribute_values WHERE eid = %lld; ",
1505                                 eid, eid);
1506         if (query == NULL) {
1507                 ret = LDB_ERR_OTHER;
1508                 goto failed;
1509         }
1510
1511         ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1512         if (ret != SQLITE_OK) {
1513                 if (errmsg) {
1514                         ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1515                         free(errmsg);
1516                 }
1517                 ret = LDB_ERR_OPERATIONS_ERROR;
1518                 goto failed;
1519         }
1520
1521         if (lsql_ac->callback)
1522                 (*handle)->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1523         
1524         return LDB_SUCCESS;
1525
1526 failed:
1527         talloc_free(*handle);
1528         return ret;
1529 }
1530
1531 static int lsql_delete(struct ldb_module *module, const struct ldb_dn *dn)
1532 {
1533         struct ldb_async_handle *handle;
1534         int ret;
1535
1536         /* ignore ltdb specials */
1537         if (ldb_dn_is_special(dn)) {
1538                 return LDB_SUCCESS;
1539         }
1540
1541         ret = lsql_delete_async(module, dn, NULL, NULL, &handle);
1542
1543         if (ret != LDB_SUCCESS)
1544                 return ret;
1545
1546         ret = ldb_async_wait(handle, LDB_WAIT_ALL);
1547
1548         talloc_free(handle);
1549         return ret;
1550 }
1551
1552 /* rename a record */
1553 static int lsql_rename_async(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn,
1554                              void *context,
1555                              int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
1556                              struct ldb_async_handle **handle)
1557 {
1558         struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1559         struct lsql_async_context *lsql_ac;
1560         char *new_dn, *new_cdn, *old_cdn;
1561         char *errmsg;
1562         char *query;
1563         int ret = LDB_ERR_OPERATIONS_ERROR;
1564
1565         *handle = init_handle(lsqlite3, module, context, callback);
1566         if (*handle == NULL) {
1567                 goto failed;
1568         }
1569         lsql_ac = talloc_get_type((*handle)->private_data, struct lsql_async_context);
1570         (*handle)->state = LDB_ASYNC_DONE;
1571         (*handle)->status = LDB_SUCCESS;
1572
1573         /* create linearized and normalized dns */
1574         old_cdn = ldb_dn_linearize(lsql_ac, ldb_dn_casefold(module->ldb, olddn));
1575         new_cdn = ldb_dn_linearize(lsql_ac, ldb_dn_casefold(module->ldb, newdn));
1576         new_dn = ldb_dn_linearize(lsql_ac, newdn);
1577         if (old_cdn == NULL || new_cdn == NULL || new_dn == NULL) {
1578                 goto failed;
1579         }
1580
1581         /* build the SQL query */
1582         query = lsqlite3_tprintf(lsql_ac,
1583                                  "UPDATE ldb_entry SET dn = '%q', norm_dn = '%q' "
1584                                  "WHERE norm_dn = '%q';",
1585                                  new_dn, new_cdn, old_cdn);
1586         if (query == NULL) {
1587                 goto failed;
1588         }
1589
1590         /* execute */
1591         ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1592         if (ret != SQLITE_OK) {
1593                 if (errmsg) {
1594                         ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1595                         free(errmsg);
1596                 }
1597                 ret = LDB_ERR_OPERATIONS_ERROR;
1598                 goto failed;
1599         }
1600
1601         if (lsql_ac->callback)
1602                 (*handle)->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1603         
1604         return LDB_SUCCESS;
1605
1606 failed:
1607         talloc_free(*handle);
1608         return ret;
1609 }
1610
1611 static int lsql_rename(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
1612 {
1613         struct ldb_async_handle *handle;
1614         int ret;
1615
1616         /* ignore ltdb specials */
1617         if (ldb_dn_is_special(olddn) || ldb_dn_is_special(newdn)) {
1618                 return LDB_SUCCESS;
1619         }
1620
1621
1622         ret = lsql_rename_async(module, olddn, newdn, NULL, NULL, &handle);
1623
1624         if (ret != LDB_SUCCESS)
1625                 return ret;
1626
1627         ret = ldb_async_wait(handle, LDB_WAIT_ALL);
1628
1629         talloc_free(handle);
1630         return ret;
1631 }
1632
1633 static int lsql_start_trans(struct ldb_module * module)
1634 {
1635         int ret;
1636         char *errmsg;
1637         struct lsqlite3_private *   lsqlite3 = module->private_data;
1638
1639         if (lsqlite3->trans_count == 0) {
1640                 ret = sqlite3_exec(lsqlite3->sqlite, "BEGIN IMMEDIATE;", NULL, NULL, &errmsg);
1641                 if (ret != SQLITE_OK) {
1642                         if (errmsg) {
1643                                 printf("lsqlite3_start_trans: error: %s\n", errmsg);
1644                                 free(errmsg);
1645                         }
1646                         return -1;
1647                 }
1648         };
1649
1650         lsqlite3->trans_count++;
1651
1652         return 0;
1653 }
1654
1655 static int lsql_end_trans(struct ldb_module *module)
1656 {
1657         int ret;
1658         char *errmsg;
1659         struct lsqlite3_private *lsqlite3 = module->private_data;
1660
1661         if (lsqlite3->trans_count > 0) {
1662                 lsqlite3->trans_count--;
1663         } else return -1;
1664
1665         if (lsqlite3->trans_count == 0) {
1666                 ret = sqlite3_exec(lsqlite3->sqlite, "COMMIT;", NULL, NULL, &errmsg);
1667                 if (ret != SQLITE_OK) {
1668                         if (errmsg) {
1669                                 printf("lsqlite3_end_trans: error: %s\n", errmsg);
1670                                 free(errmsg);
1671                         }
1672                         return -1;
1673                 }
1674         }
1675
1676         return 0;
1677 }
1678
1679 static int lsql_del_trans(struct ldb_module *module)
1680 {
1681         struct lsqlite3_private *lsqlite3 = module->private_data;
1682
1683         if (lsqlite3->trans_count > 0) {
1684                 lsqlite3->trans_count--;
1685         } else return -1;
1686
1687         if (lsqlite3->trans_count == 0) {
1688                 return lsqlite3_safe_rollback(lsqlite3->sqlite);
1689         }
1690
1691         return -1;
1692 }
1693
1694 /*
1695  * Static functions
1696  */
1697
1698 static int initialize(struct lsqlite3_private *lsqlite3,
1699                       struct ldb_context *ldb, const char *url, int flags)
1700 {
1701         TALLOC_CTX *local_ctx;
1702         long long queryInt;
1703         int rollback = 0;
1704         char *errmsg;
1705         char *schema;
1706         int ret;
1707
1708         /* create a local ctx */
1709         local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_rename local context");
1710         if (local_ctx == NULL) {
1711                 return -1;
1712         }
1713
1714         schema = lsqlite3_tprintf(local_ctx,
1715                 
1716                 
1717                 "CREATE TABLE ldb_info AS "
1718                 "  SELECT 'LDB' AS database_type,"
1719                 "         '1.0' AS version;"
1720                 
1721                 /*
1722                  * The entry table holds the information about an entry. 
1723                  * This table is used to obtain the EID of the entry and to 
1724                  * support scope=one and scope=base.  The parent and child
1725                  * table is included in the entry table since all the other
1726                  * attributes are dependent on EID.
1727                  */
1728                 "CREATE TABLE ldb_entry "
1729                 "("
1730                 "  eid     INTEGER PRIMARY KEY AUTOINCREMENT,"
1731                 "  dn      TEXT UNIQUE NOT NULL,"
1732                 "  norm_dn TEXT UNIQUE NOT NULL"
1733                 ");"
1734                 
1735
1736                 "CREATE TABLE ldb_object_classes"
1737                 "("
1738                 "  class_name            TEXT PRIMARY KEY,"
1739                 "  parent_class_name     TEXT,"
1740                 "  tree_key              TEXT UNIQUE,"
1741                 "  max_child_num         INTEGER DEFAULT 0"
1742                 ");"
1743                 
1744                 /*
1745                  * We keep a full listing of attribute/value pairs here
1746                  */
1747                 "CREATE TABLE ldb_attribute_values"
1748                 "("
1749                 "  eid             INTEGER REFERENCES ldb_entry,"
1750                 "  attr_name       TEXT,"
1751                 "  norm_attr_name  TEXT,"
1752                 "  attr_value      TEXT,"
1753                 "  norm_attr_value TEXT "
1754                 ");"
1755                 
1756                
1757                 /*
1758                  * Indexes
1759                  */
1760                 "CREATE INDEX ldb_attribute_values_eid_idx "
1761                 "  ON ldb_attribute_values (eid);"
1762                 
1763                 "CREATE INDEX ldb_attribute_values_name_value_idx "
1764                 "  ON ldb_attribute_values (attr_name, norm_attr_value);"
1765                 
1766                 
1767
1768                 /*
1769                  * Triggers
1770                  */
1771  
1772                 "CREATE TRIGGER ldb_object_classes_insert_tr"
1773                 "  AFTER INSERT"
1774                 "  ON ldb_object_classes"
1775                 "  FOR EACH ROW"
1776                 "    BEGIN"
1777                 "      UPDATE ldb_object_classes"
1778                 "        SET tree_key = COALESCE(tree_key, "
1779                 "              ("
1780                 "                SELECT tree_key || "
1781                 "                       (SELECT base160(max_child_num + 1)"
1782                 "                                FROM ldb_object_classes"
1783                 "                                WHERE class_name = "
1784                 "                                      new.parent_class_name)"
1785                 "                  FROM ldb_object_classes "
1786                 "                  WHERE class_name = new.parent_class_name "
1787                 "              ));"
1788                 "      UPDATE ldb_object_classes "
1789                 "        SET max_child_num = max_child_num + 1"
1790                 "        WHERE class_name = new.parent_class_name;"
1791                 "    END;"
1792
1793                 /*
1794                  * Table initialization
1795                  */
1796
1797                 "INSERT INTO ldb_object_classes "
1798                 "    (class_name, tree_key) "
1799                 "  VALUES "
1800                 "    ('TOP', '0001');");
1801         
1802         /* Skip protocol indicator of url  */
1803         if (strncmp(url, "sqlite://", 9) != 0) {
1804                 return SQLITE_MISUSE;
1805         }
1806         
1807         /* Update pointer to just after the protocol indicator */
1808         url += 9;
1809         
1810         /* Try to open the (possibly empty/non-existent) database */
1811         if ((ret = sqlite3_open(url, &lsqlite3->sqlite)) != SQLITE_OK) {
1812                 return ret;
1813         }
1814         
1815         /* In case this is a new database, enable auto_vacuum */
1816         ret = sqlite3_exec(lsqlite3->sqlite, "PRAGMA auto_vacuum = 1;", NULL, NULL, &errmsg);
1817         if (ret != SQLITE_OK) {
1818                 if (errmsg) {
1819                         printf("lsqlite3 initializaion error: %s\n", errmsg);
1820                         free(errmsg);
1821                 }
1822                 goto failed;
1823         }
1824         
1825         if (flags & LDB_FLG_NOSYNC) {
1826                 /* DANGEROUS */
1827                 ret = sqlite3_exec(lsqlite3->sqlite, "PRAGMA synchronous = OFF;", NULL, NULL, &errmsg);
1828                 if (ret != SQLITE_OK) {
1829                         if (errmsg) {
1830                                 printf("lsqlite3 initializaion error: %s\n", errmsg);
1831                                 free(errmsg);
1832                         }
1833                         goto failed;
1834                 }
1835         }
1836         
1837         /* */
1838         
1839         /* Establish a busy timeout of 30 seconds */
1840         if ((ret = sqlite3_busy_timeout(lsqlite3->sqlite,
1841                                         30000)) != SQLITE_OK) {
1842                 return ret;
1843         }
1844
1845         /* Create a function, callable from sql, to increment a tree_key */
1846         if ((ret =
1847              sqlite3_create_function(lsqlite3->sqlite,/* handle */
1848                                      "base160_next",  /* function name */
1849                                      1,               /* number of args */
1850                                      SQLITE_ANY,      /* preferred text type */
1851                                      NULL,            /* user data */
1852                                      base160next_sql, /* called func */
1853                                      NULL,            /* step func */
1854                                      NULL             /* final func */
1855                      )) != SQLITE_OK) {
1856                 return ret;
1857         }
1858
1859         /* Create a function, callable from sql, to convert int to base160 */
1860         if ((ret =
1861              sqlite3_create_function(lsqlite3->sqlite,/* handle */
1862                                      "base160",       /* function name */
1863                                      1,               /* number of args */
1864                                      SQLITE_ANY,      /* preferred text type */
1865                                      NULL,            /* user data */
1866                                      base160_sql,     /* called func */
1867                                      NULL,            /* step func */
1868                                      NULL             /* final func */
1869                      )) != SQLITE_OK) {
1870                 return ret;
1871         }
1872
1873         /* Create a function, callable from sql, to perform various comparisons */
1874         if ((ret =
1875              sqlite3_create_function(lsqlite3->sqlite, /* handle */
1876                                      "ldap_compare",   /* function name */
1877                                      4,                /* number of args */
1878                                      SQLITE_ANY,       /* preferred text type */
1879                                      ldb  ,            /* user data */
1880                                      lsqlite3_compare, /* called func */
1881                                      NULL,             /* step func */
1882                                      NULL              /* final func */
1883                      )) != SQLITE_OK) {
1884                 return ret;
1885         }
1886
1887         /* Begin a transaction */
1888         ret = sqlite3_exec(lsqlite3->sqlite, "BEGIN EXCLUSIVE;", NULL, NULL, &errmsg);
1889         if (ret != SQLITE_OK) {
1890                 if (errmsg) {
1891                         printf("lsqlite3: initialization error: %s\n", errmsg);
1892                         free(errmsg);
1893                 }
1894                 goto failed;
1895         }
1896         rollback = 1;
1897  
1898         /* Determine if this is a new database.  No tables means it is. */
1899         if (query_int(lsqlite3,
1900                       &queryInt,
1901                       "SELECT COUNT(*)\n"
1902                       "  FROM sqlite_master\n"
1903                       "  WHERE type = 'table';") != 0) {
1904                 goto failed;
1905         }
1906         
1907         if (queryInt == 0) {
1908                 /*
1909                  * Create the database schema
1910                  */
1911                 ret = sqlite3_exec(lsqlite3->sqlite, schema, NULL, NULL, &errmsg);
1912                 if (ret != SQLITE_OK) {
1913                         if (errmsg) {
1914                                 printf("lsqlite3 initializaion error: %s\n", errmsg);
1915                                 free(errmsg);
1916                         }
1917                         goto failed;
1918                 }
1919         } else {
1920                 /*
1921                  * Ensure that the database we opened is one of ours
1922                  */
1923                 if (query_int(lsqlite3,
1924                               &queryInt,
1925                               "SELECT "
1926                               "  (SELECT COUNT(*) = 2"
1927                               "     FROM sqlite_master "
1928                               "     WHERE type = 'table' "
1929                               "       AND name IN "
1930                               "         ("
1931                               "           'ldb_entry', "
1932                               "           'ldb_object_classes' "
1933                               "         ) "
1934                               "  ) "
1935                               "  AND "
1936                               "  (SELECT 1 "
1937                               "     FROM ldb_info "
1938                               "     WHERE database_type = 'LDB' "
1939                               "       AND version = '1.0'"
1940                               "  );") != 0 ||
1941                     queryInt != 1) {
1942                         
1943                         /* It's not one that we created.  See ya! */
1944                         goto failed;
1945                 }
1946         }
1947         
1948         /* Commit the transaction */
1949         ret = sqlite3_exec(lsqlite3->sqlite, "COMMIT;", NULL, NULL, &errmsg);
1950         if (ret != SQLITE_OK) {
1951                 if (errmsg) {
1952                         printf("lsqlite3: iniialization error: %s\n", errmsg);
1953                         free(errmsg);
1954                 }
1955                 goto failed;
1956         }
1957  
1958         return SQLITE_OK;
1959
1960 failed:
1961         if (rollback) lsqlite3_safe_rollback(lsqlite3->sqlite); 
1962         sqlite3_close(lsqlite3->sqlite);
1963         return -1;
1964 }
1965
1966 static int destructor(struct lsqlite3_private *lsqlite3)
1967 {        
1968         if (lsqlite3->sqlite) {
1969                 sqlite3_close(lsqlite3->sqlite);
1970         }
1971         return 0;
1972 }
1973
1974 static int lsql_async_wait(struct ldb_async_handle *handle, enum ldb_async_wait_type type)
1975 {
1976         return handle->status;
1977 }
1978
1979 static int lsql_request(struct ldb_module *module, struct ldb_request *req)
1980 {
1981         /* check for oustanding critical controls and return an error if found */
1982
1983         if (req->controls != NULL) {
1984                 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "Controls should not reach the ldb_sqlite3 backend!\n");
1985         }
1986
1987         if (check_critical_controls(req->controls)) {
1988                 return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
1989         }
1990         
1991         switch (req->operation) {
1992
1993         case LDB_REQ_SEARCH:
1994                 return lsql_search_bytree(module,
1995                                           req->op.search.base,
1996                                           req->op.search.scope, 
1997                                           req->op.search.tree, 
1998                                           req->op.search.attrs, 
1999                                           &req->op.search.res);
2000
2001         case LDB_REQ_ADD:
2002                 return lsql_add(module, req->op.add.message);
2003
2004         case LDB_REQ_MODIFY:
2005                 return lsql_modify(module, req->op.mod.message);
2006
2007         case LDB_REQ_DELETE:
2008                 return lsql_delete(module, req->op.del.dn);
2009
2010         case LDB_REQ_RENAME:
2011                 return lsql_rename(module,
2012                                         req->op.rename.olddn,
2013                                         req->op.rename.newdn);
2014
2015         case LDB_ASYNC_SEARCH:
2016                 return lsql_search_async(module,
2017                                         req->op.search.base,
2018                                         req->op.search.scope, 
2019                                         req->op.search.tree, 
2020                                         req->op.search.attrs,
2021                                         req->async.context,
2022                                         req->async.callback,
2023                                         &req->async.handle);
2024 /*
2025         case LDB_ASYNC_ADD:
2026                 return lsql_add_async(module,
2027                                         req->op.add.message,
2028                                         req->async.context,
2029                                         req->async.callback,
2030                                         &req->async.handle);
2031
2032         case LDB_ASYNC_MODIFY:
2033                 return lsql_modify_async(module,
2034                                         req->op.mod.message,
2035                                         req->async.context,
2036                                         req->async.callback,
2037                                         &req->async.handle);
2038 */
2039         case LDB_ASYNC_DELETE:
2040                 return lsql_delete_async(module,
2041                                         req->op.del.dn,
2042                                         req->async.context,
2043                                         req->async.callback,
2044                                         &req->async.handle);
2045
2046         case LDB_ASYNC_RENAME:
2047                 return lsql_rename_async(module,
2048                                         req->op.rename.olddn,
2049                                         req->op.rename.newdn,
2050                                         req->async.context,
2051                                         req->async.callback,
2052                                         &req->async.handle);
2053
2054         default:
2055                 return LDB_ERR_OPERATIONS_ERROR;
2056
2057         }
2058 }
2059
2060 /*
2061  * Table of operations for the sqlite3 backend
2062  */
2063 static const struct ldb_module_ops lsqlite3_ops = {
2064         .name              = "sqlite",
2065         .request           = lsql_request,
2066         .start_transaction = lsql_start_trans,
2067         .end_transaction   = lsql_end_trans,
2068         .del_transaction   = lsql_del_trans,
2069         .async_wait        = lsql_async_wait,
2070 };
2071
2072 /*
2073  * connect to the database
2074  */
2075 static int lsqlite3_connect(struct ldb_context *ldb,
2076                      const char *url, 
2077                      unsigned int flags, 
2078                      const char *options[])
2079 {
2080         int                         i;
2081         int                         ret;
2082         struct lsqlite3_private *   lsqlite3 = NULL;
2083         
2084         lsqlite3 = talloc(ldb, struct lsqlite3_private);
2085         if (!lsqlite3) {
2086                 goto failed;
2087         }
2088         
2089         lsqlite3->sqlite = NULL;
2090         lsqlite3->options = NULL;
2091         lsqlite3->trans_count = 0;
2092         
2093         ret = initialize(lsqlite3, ldb, url, flags);
2094         if (ret != SQLITE_OK) {
2095                 goto failed;
2096         }
2097         
2098         talloc_set_destructor(lsqlite3, destructor);
2099         
2100         ldb->modules = talloc(ldb, struct ldb_module);
2101         if (!ldb->modules) {
2102                 goto failed;
2103         }
2104         ldb->modules->ldb = ldb;
2105         ldb->modules->prev = ldb->modules->next = NULL;
2106         ldb->modules->private_data = lsqlite3;
2107         ldb->modules->ops = &lsqlite3_ops;
2108         
2109         if (options) {
2110                 /*
2111                  * take a copy of the options array, so we don't have to rely
2112                  * on the caller keeping it around (it might be dynamic)
2113                  */
2114                 for (i=0;options[i];i++) ;
2115                 
2116                 lsqlite3->options = talloc_array(lsqlite3, char *, i+1);
2117                 if (!lsqlite3->options) {
2118                         goto failed;
2119                 }
2120                 
2121                 for (i=0;options[i];i++) {
2122                         
2123                         lsqlite3->options[i+1] = NULL;
2124                         lsqlite3->options[i] =
2125                                 talloc_strdup(lsqlite3->options, options[i]);
2126                         if (!lsqlite3->options[i]) {
2127                                 goto failed;
2128                         }
2129                 }
2130         }
2131         
2132         return 0;
2133         
2134 failed:
2135         if (lsqlite3->sqlite != NULL) {
2136                 (void) sqlite3_close(lsqlite3->sqlite);
2137         }
2138         talloc_free(lsqlite3);
2139         return -1;
2140 }
2141
2142 int ldb_sqlite3_init(void)
2143 {
2144         return ldb_register_backend("sqlite3", lsqlite3_connect);
2145 }