r23798: updated old Temple Place FSF addresses to new URL
[ira/wip.git] / source3 / 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-2006
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 3 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, see <http://www.gnu.org/licenses/>.
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldb sqlite3 backend
29  *
30  *  Description: core files for SQLITE3 backend
31  *
32  *  Author: Derrell Lipman (based on Andrew Tridgell's LDAP backend)
33  */
34
35 #include "includes.h"
36 #include "ldb/include/includes.h"
37
38 #include <sqlite3.h>
39
40 struct lsqlite3_private {
41         int trans_count;
42         char **options;
43         sqlite3 *sqlite;
44 };
45
46 struct lsql_context {
47         struct ldb_module *module;
48
49         /* search stuff */
50         long long current_eid;
51         const char * const * attrs;
52         struct ldb_reply *ares;
53
54         /* async stuff */
55         void *context;
56         int (*callback)(struct ldb_context *, void *, struct ldb_reply *);
57 };
58
59 static struct ldb_handle *init_handle(struct lsqlite3_private *lsqlite3,
60                                         struct ldb_module *module,
61                                         struct ldb_request *req)
62 {
63         struct lsql_context *ac;
64         struct ldb_handle *h;
65
66         h = talloc_zero(lsqlite3, struct ldb_handle);
67         if (h == NULL) {
68                 ldb_set_errstring(module->ldb, "Out of Memory");
69                 return NULL;
70         }
71
72         h->module = module;
73
74         ac = talloc(h, struct lsql_context);
75         if (ac == NULL) {
76                 ldb_set_errstring(module->ldb, "Out of Memory");
77                 talloc_free(h);
78                 return NULL;
79         }
80
81         h->private_data = (void *)ac;
82
83         h->state = LDB_ASYNC_INIT;
84         h->status = LDB_SUCCESS;
85
86         ac->module = module;
87         ac->context = req->context;
88         ac->callback = req->callback;
89
90         return h;
91 }
92
93 /*
94  * Macros used throughout
95  */
96
97 #ifndef FALSE
98 # define FALSE  (0)
99 # define TRUE   (! FALSE)
100 #endif
101
102 #define RESULT_ATTR_TABLE       "temp_result_attrs"
103
104 //#define TEMPTAB                 /* for testing, create non-temporary table */
105 #define TEMPTAB                 "TEMPORARY"
106
107 /*
108  * Static variables
109  */
110 sqlite3_stmt *  stmtGetEID = NULL;
111
112 static char *lsqlite3_tprintf(TALLOC_CTX *mem_ctx, const char *fmt, ...)
113 {
114         char *str, *ret;
115         va_list ap;
116
117         va_start(ap, fmt);
118         str = sqlite3_vmprintf(fmt, ap);
119         va_end(ap);
120
121         if (str == NULL) return NULL;
122
123         ret = talloc_strdup(mem_ctx, str);
124         if (ret == NULL) {
125                 sqlite3_free(str);
126                 return NULL;
127         }
128
129         sqlite3_free(str);
130         return ret;
131 }
132
133 static char base160tab[161] = {
134         48 ,49 ,50 ,51 ,52 ,53 ,54 ,55 ,56 ,57 , /* 0-9 */
135         58 ,59 ,65 ,66 ,67 ,68 ,69 ,70 ,71 ,72 , /* : ; A-H */
136         73 ,74 ,75 ,76 ,77 ,78 ,79 ,80 ,81 ,82 , /* I-R */
137         83 ,84 ,85 ,86 ,87 ,88 ,89 ,90 ,97 ,98 , /* S-Z , a-b */
138         99 ,100,101,102,103,104,105,106,107,108, /* c-l */
139         109,110,111,112,113,114,115,116,117,118, /* m-v */
140         119,120,121,122,160,161,162,163,164,165, /* w-z, latin1 */
141         166,167,168,169,170,171,172,173,174,175, /* latin1 */
142         176,177,178,179,180,181,182,183,184,185, /* latin1 */
143         186,187,188,189,190,191,192,193,194,195, /* latin1 */
144         196,197,198,199,200,201,202,203,204,205, /* latin1 */
145         206,207,208,209,210,211,212,213,214,215, /* latin1 */
146         216,217,218,219,220,221,222,223,224,225, /* latin1 */
147         226,227,228,229,230,231,232,233,234,235, /* latin1 */
148         236,237,238,239,240,241,242,243,244,245, /* latin1 */
149         246,247,248,249,250,251,252,253,254,255, /* latin1 */
150         '\0'
151 };
152
153
154 /*
155  * base160()
156  *
157  * Convert an unsigned long integer into a base160 representation of the
158  * number.
159  *
160  * Parameters:
161  *   val --
162  *     value to be converted
163  *
164  *   result --
165  *     character array, 5 bytes long, into which the base160 representation
166  *     will be placed.  The result will be a four-digit representation of the
167  *     number (with leading zeros prepended as necessary), and null
168  *     terminated.
169  *
170  * Returns:
171  *   Nothing
172  */
173 static void
174 base160_sql(sqlite3_context * hContext,
175             int argc,
176             sqlite3_value ** argv)
177 {
178     int             i;
179     long long       val;
180     char            result[5];
181
182     val = sqlite3_value_int64(argv[0]);
183
184     for (i = 3; i >= 0; i--) {
185         
186         result[i] = base160tab[val % 160];
187         val /= 160;
188     }
189
190     result[4] = '\0';
191
192     sqlite3_result_text(hContext, result, -1, SQLITE_TRANSIENT);
193 }
194
195
196 /*
197  * base160next_sql()
198  *
199  * This function enhances sqlite by adding a "base160_next()" function which is
200  * accessible via queries.
201  *
202  * Retrieve the next-greater number in the base160 sequence for the terminal
203  * tree node (the last four digits).  Only one tree level (four digits) is
204  * operated on.
205  *
206  * Input:
207  *   A character string: either an empty string (in which case no operation is
208  *   performed), or a string of base160 digits with a length of a multiple of
209  *   four digits.
210  *
211  * Output:
212  *   Upon return, the trailing four digits (one tree level) will have been
213  *   incremented by 1.
214  */
215 static void
216 base160next_sql(sqlite3_context * hContext,
217                 int argc,
218                 sqlite3_value ** argv)
219 {
220         int                         i;
221         int                         len;
222         char *             pTab;
223         char *             pBase160 = strdup((const char *)sqlite3_value_text(argv[0]));
224         char *             pStart = pBase160;
225
226         /*
227          * We need a minimum of four digits, and we will always get a multiple
228          * of four digits.
229          */
230         if (pBase160 != NULL &&
231             (len = strlen(pBase160)) >= 4 &&
232             len % 4 == 0) {
233
234                 if (pBase160 == NULL) {
235
236                         sqlite3_result_null(hContext);
237                         return;
238                 }
239
240                 pBase160 += strlen(pBase160) - 1;
241
242                 /* We only carry through four digits: one level in the tree */
243                 for (i = 0; i < 4; i++) {
244
245                         /* What base160 value does this digit have? */
246                         pTab = strchr(base160tab, *pBase160);
247
248                         /* Is there a carry? */
249                         if (pTab < base160tab + sizeof(base160tab) - 1) {
250
251                                 /*
252                                  * Nope.  Just increment this value and we're
253                                  * done.
254                                  */
255                                 *pBase160 = *++pTab;
256                                 break;
257                         } else {
258
259                                 /*
260                                  * There's a carry.  This value gets
261                                  * base160tab[0], we decrement the buffer
262                                  * pointer to get the next higher-order digit,
263                                  * and continue in the loop.
264                                  */
265                                 *pBase160-- = base160tab[0];
266                         }
267                 }
268
269                 sqlite3_result_text(hContext,
270                                     pStart,
271                                     strlen(pStart),
272                                     free);
273         } else {
274                 sqlite3_result_value(hContext, argv[0]);
275                 if (pBase160 != NULL) {
276                         free(pBase160);
277                 }
278         }
279 }
280
281 static char *parsetree_to_sql(struct ldb_module *module,
282                               void *mem_ctx,
283                               const struct ldb_parse_tree *t)
284 {
285         const struct ldb_attrib_handler *h;
286         struct ldb_val value, subval;
287         char *wild_card_string;
288         char *child, *tmp;
289         char *ret = NULL;
290         char *attr;
291         int i;
292
293
294         switch(t->operation) {
295         case LDB_OP_AND:
296
297                 tmp = parsetree_to_sql(module, mem_ctx, t->u.list.elements[0]);
298                 if (tmp == NULL) return NULL;
299
300                 for (i = 1; i < t->u.list.num_elements; i++) {
301
302                         child = parsetree_to_sql(module, mem_ctx, t->u.list.elements[i]);
303                         if (child == NULL) return NULL;
304
305                         tmp = talloc_asprintf_append(tmp, " INTERSECT %s ", child);
306                         if (tmp == NULL) return NULL;
307                 }
308
309                 ret = talloc_asprintf(mem_ctx, "SELECT * FROM ( %s )\n", tmp);
310
311                 return ret;
312                 
313         case LDB_OP_OR:
314
315                 tmp = parsetree_to_sql(module, mem_ctx, t->u.list.elements[0]);
316                 if (tmp == NULL) return NULL;
317
318                 for (i = 1; i < t->u.list.num_elements; i++) {
319
320                         child = parsetree_to_sql(module, mem_ctx, t->u.list.elements[i]);
321                         if (child == NULL) return NULL;
322
323                         tmp = talloc_asprintf_append(tmp, " UNION %s ", child);
324                         if (tmp == NULL) return NULL;
325                 }
326
327                 return talloc_asprintf(mem_ctx, "SELECT * FROM ( %s ) ", tmp);
328
329         case LDB_OP_NOT:
330
331                 child = parsetree_to_sql(module, mem_ctx, t->u.isnot.child);
332                 if (child == NULL) return NULL;
333
334                 return talloc_asprintf(mem_ctx,
335                                         "SELECT eid FROM ldb_entry "
336                                         "WHERE eid NOT IN ( %s ) ", child);
337
338         case LDB_OP_EQUALITY:
339                 /*
340                  * For simple searches, we want to retrieve the list of EIDs that
341                  * match the criteria.
342                 */
343                 attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
344                 if (attr == NULL) return NULL;
345                 h = ldb_attrib_handler(module->ldb, attr);
346
347                 /* Get a canonicalised copy of the data */
348                 h->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
349                 if (value.data == NULL) {
350                         return NULL;
351                 }
352
353                 if (strcasecmp(t->u.equality.attr, "objectclass") == 0) {
354                 /*
355                  * For object classes, we want to search for all objectclasses
356                  * that are subclasses as well.
357                 */
358                         return lsqlite3_tprintf(mem_ctx,
359                                         "SELECT eid  FROM ldb_attribute_values\n"
360                                         "WHERE norm_attr_name = 'OBJECTCLASS' "
361                                         "AND norm_attr_value IN\n"
362                                         "  (SELECT class_name FROM ldb_object_classes\n"
363                                         "   WHERE tree_key GLOB\n"
364                                         "     (SELECT tree_key FROM ldb_object_classes\n"
365                                         "      WHERE class_name = '%q'\n"
366                                         "     ) || '*'\n"
367                                         "  )\n", value.data);
368
369                 } else if (strcasecmp(t->u.equality.attr, "dn") == 0) {
370                         /* DN query is a special ldb case */
371                         char *cdn = ldb_dn_linearize_casefold(module->ldb,
372                                                               mem_ctx,
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_handle *handle = talloc_get_type(result, struct ldb_handle);
715         struct lsql_context *ac = talloc_get_type(handle->private_data, struct lsql_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_reply);
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, local_ctx, 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 /* search for matching records, by tree */
846 int lsql_search(struct ldb_module *module, struct ldb_request *req)
847 {
848         struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
849         struct lsql_context *lsql_ac;
850         char *norm_basedn;
851         char *sqlfilter;
852         char *errmsg;
853         char *query = NULL;
854         int ret;
855
856         req->handle = init_handle(lsqlite3, module, req);
857         if (req->handle == NULL) {
858                 return LDB_ERR_OPERATIONS_ERROR;
859         }
860
861         lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
862
863         if ((req->op.search.base == NULL || req->op.search.base->comp_num == 0) &&
864             (req->op.search.scope == LDB_SCOPE_BASE || req->op.search.scope == LDB_SCOPE_ONELEVEL))
865                 return LDB_ERR_OPERATIONS_ERROR;
866
867         if (req->op.search.base) {
868                 norm_basedn = ldb_dn_linearize(lsql_ac, ldb_dn_casefold(module->ldb, lsql_ac, req->op.search.base));
869                 if (norm_basedn == NULL) {
870                         ret = LDB_ERR_INVALID_DN_SYNTAX;
871                         goto failed;
872                 }
873         } else norm_basedn = talloc_strdup(lsql_ac, "");
874
875         /* Convert filter into a series of SQL conditions (constraints) */
876         sqlfilter = parsetree_to_sql(module, lsql_ac, req->op.search.tree);
877         
878         switch(req->op.search.scope) {
879         case LDB_SCOPE_DEFAULT:
880         case LDB_SCOPE_SUBTREE:
881                 if (*norm_basedn != '\0') {
882                         query = lsqlite3_tprintf(lsql_ac,
883                                 "SELECT entry.eid,\n"
884                                 "       entry.dn,\n"
885                                 "       av.attr_name,\n"
886                                 "       av.attr_value\n"
887                                 "  FROM ldb_entry AS entry\n"
888
889                                 "  LEFT OUTER JOIN ldb_attribute_values AS av\n"
890                                 "    ON av.eid = entry.eid\n"
891
892                                 "  WHERE entry.eid IN\n"
893                                 "    (SELECT DISTINCT ldb_entry.eid\n"
894                                 "       FROM ldb_entry\n"
895                                 "       WHERE (ldb_entry.norm_dn GLOB('*,%q')\n"
896                                 "       OR ldb_entry.norm_dn = '%q')\n"
897                                 "       AND ldb_entry.eid IN\n"
898                                 "         (%s)\n"
899                                 "    )\n"
900
901                                 "  ORDER BY entry.eid ASC;",
902                                 norm_basedn,
903                                 norm_basedn,
904                                 sqlfilter);
905                 } else {
906                         query = lsqlite3_tprintf(lsql_ac,
907                                 "SELECT entry.eid,\n"
908                                 "       entry.dn,\n"
909                                 "       av.attr_name,\n"
910                                 "       av.attr_value\n"
911                                 "  FROM ldb_entry AS entry\n"
912
913                                 "  LEFT OUTER JOIN ldb_attribute_values AS av\n"
914                                 "    ON av.eid = entry.eid\n"
915
916                                 "  WHERE entry.eid IN\n"
917                                 "    (SELECT DISTINCT ldb_entry.eid\n"
918                                 "       FROM ldb_entry\n"
919                                 "       WHERE ldb_entry.eid IN\n"
920                                 "         (%s)\n"
921                                 "    )\n"
922
923                                 "  ORDER BY entry.eid ASC;",
924                                 sqlfilter);
925                 }
926
927                 break;
928                 
929         case LDB_SCOPE_BASE:
930                 query = lsqlite3_tprintf(lsql_ac,
931                         "SELECT entry.eid,\n"
932                         "       entry.dn,\n"
933                         "       av.attr_name,\n"
934                         "       av.attr_value\n"
935                         "  FROM ldb_entry AS entry\n"
936
937                         "  LEFT OUTER JOIN ldb_attribute_values AS av\n"
938                         "    ON av.eid = entry.eid\n"
939
940                         "  WHERE entry.eid IN\n"
941                         "    (SELECT DISTINCT ldb_entry.eid\n"
942                         "       FROM ldb_entry\n"
943                         "       WHERE ldb_entry.norm_dn = '%q'\n"
944                         "         AND ldb_entry.eid IN\n"
945                         "           (%s)\n"
946                         "    )\n"
947
948                         "  ORDER BY entry.eid ASC;",
949                         norm_basedn,
950                         sqlfilter);
951                 break;
952                 
953         case LDB_SCOPE_ONELEVEL:
954                 query = lsqlite3_tprintf(lsql_ac,
955                         "SELECT entry.eid,\n"
956                         "       entry.dn,\n"
957                         "       av.attr_name,\n"
958                         "       av.attr_value\n"
959                         "  FROM ldb_entry AS entry\n"
960
961                         "  LEFT OUTER JOIN ldb_attribute_values AS av\n"
962                         "    ON av.eid = entry.eid\n"
963
964                         "  WHERE entry.eid IN\n"
965                         "    (SELECT DISTINCT ldb_entry.eid\n"
966                         "       FROM ldb_entry\n"
967                         "       WHERE norm_dn GLOB('*,%q')\n"
968                         "         AND NOT norm_dn GLOB('*,*,%q')\n"
969                         "         AND ldb_entry.eid IN\n(%s)\n"
970                         "    )\n"
971
972                         "  ORDER BY entry.eid ASC;",
973                         norm_basedn,
974                         norm_basedn,
975                         sqlfilter);
976                 break;
977         }
978
979         if (query == NULL) {
980                 goto failed;
981         }
982
983         /* * /
984         printf ("%s\n", query);
985         / * */
986
987         lsql_ac->current_eid = 0;
988         lsql_ac->attrs = req->op.search.attrs;
989         lsql_ac->ares = NULL;
990
991         req->handle->state = LDB_ASYNC_PENDING;
992
993         ret = sqlite3_exec(lsqlite3->sqlite, query, lsqlite3_search_callback, req->handle, &errmsg);
994         if (ret != SQLITE_OK) {
995                 if (errmsg) {
996                         ldb_set_errstring(module->ldb, errmsg);
997                         free(errmsg);
998                 }
999                 goto failed;
1000         }
1001
1002         /* complete the last message if any */
1003         if (lsql_ac->ares) {
1004                 lsql_ac->ares->message = ldb_msg_canonicalize(module->ldb, lsql_ac->ares->message);
1005                 if (lsql_ac->ares->message == NULL)
1006                         goto failed;
1007                         
1008                 req->handle->status = lsql_ac->callback(module->ldb, lsql_ac->context, lsql_ac->ares);
1009                 if (req->handle->status != LDB_SUCCESS)
1010                         goto failed;
1011         }
1012
1013         req->handle->state = LDB_ASYNC_DONE;
1014
1015         return LDB_SUCCESS;
1016
1017 failed:
1018         return LDB_ERR_OPERATIONS_ERROR;
1019 }
1020
1021 /* add a record */
1022 static int lsql_add(struct ldb_module *module, struct ldb_request *req)
1023 {
1024         struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1025         struct lsql_context *lsql_ac;
1026         struct ldb_message *msg = req->op.add.message;
1027         long long eid;
1028         char *dn, *ndn;
1029         char *errmsg;
1030         char *query;
1031         int i;
1032         int ret = LDB_SUCCESS;
1033
1034         req->handle = init_handle(lsqlite3, module, req);
1035         if (req->handle == NULL) {
1036                 return LDB_ERR_OPERATIONS_ERROR;
1037         }
1038         lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
1039         req->handle->state = LDB_ASYNC_DONE;
1040         req->handle->status = LDB_SUCCESS;
1041
1042         /* See if this is an ltdb special */
1043         if (ldb_dn_is_special(msg->dn)) {
1044                 struct ldb_dn *c;
1045
1046                 c = ldb_dn_explode(lsql_ac, "@SUBCLASSES");
1047                 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
1048 #warning "insert subclasses into object class tree"
1049                         ret = LDB_ERR_UNWILLING_TO_PERFORM;
1050                         goto done;
1051                 }
1052
1053 /*
1054                 c = ldb_dn_explode(local_ctx, "@INDEXLIST");
1055                 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
1056 #warning "should we handle indexes somehow ?"
1057                         ret = LDB_ERR_UNWILLING_TO_PERFORM;
1058                         goto done;
1059                 }
1060 */
1061                 /* Others return an error */
1062                 ret = LDB_ERR_UNWILLING_TO_PERFORM;
1063                 goto done;
1064         }
1065
1066         /* create linearized and normalized dns */
1067         dn = ldb_dn_linearize(lsql_ac, msg->dn);
1068         ndn = ldb_dn_linearize(lsql_ac, ldb_dn_casefold(module->ldb, lsql_ac, msg->dn));
1069         if (dn == NULL || ndn == NULL) {
1070                 ret = LDB_ERR_OTHER;
1071                 goto done;
1072         }
1073
1074         query = lsqlite3_tprintf(lsql_ac,
1075                                    /* Add new entry */
1076                                    "INSERT OR ABORT INTO ldb_entry "
1077                                    "('dn', 'norm_dn') "
1078                                    "VALUES ('%q', '%q');",
1079                                 dn, ndn);
1080         if (query == NULL) {
1081                 ret = LDB_ERR_OTHER;
1082                 goto done;
1083         }
1084
1085         ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1086         if (ret != SQLITE_OK) {
1087                 if (errmsg) {
1088                         ldb_set_errstring(module->ldb, errmsg);
1089                         free(errmsg);
1090                 }
1091                 ret = LDB_ERR_OTHER;
1092                 goto done;
1093         }
1094
1095         eid = lsqlite3_get_eid_ndn(lsqlite3->sqlite, lsql_ac, ndn);
1096         if (eid == -1) {
1097                 ret = LDB_ERR_OTHER;
1098                 goto done;
1099         }
1100
1101         for (i = 0; i < msg->num_elements; i++) {
1102                 const struct ldb_message_element *el = &msg->elements[i];
1103                 const struct ldb_attrib_handler *h;
1104                 char *attr;
1105                 int j;
1106
1107                 /* Get a case-folded copy of the attribute name */
1108                 attr = ldb_attr_casefold(lsql_ac, el->name);
1109                 if (attr == NULL) {
1110                         ret = LDB_ERR_OTHER;
1111                         goto done;
1112                 }
1113
1114                 h = ldb_attrib_handler(module->ldb, el->name);
1115
1116                 /* For each value of the specified attribute name... */
1117                 for (j = 0; j < el->num_values; j++) {
1118                         struct ldb_val value;
1119                         char *insert;
1120
1121                         /* Get a canonicalised copy of the data */
1122                         h->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
1123                         if (value.data == NULL) {
1124                                 ret = LDB_ERR_OTHER;
1125                                 goto done;
1126                         }
1127
1128                         insert = lsqlite3_tprintf(lsql_ac,
1129                                         "INSERT OR ROLLBACK INTO ldb_attribute_values "
1130                                         "('eid', 'attr_name', 'norm_attr_name',"
1131                                         " 'attr_value', 'norm_attr_value') "
1132                                         "VALUES ('%lld', '%q', '%q', '%q', '%q');",
1133                                         eid, el->name, attr,
1134                                         el->values[j].data, value.data);
1135                         if (insert == NULL) {
1136                                 ret = LDB_ERR_OTHER;
1137                                 goto done;
1138                         }
1139
1140                         ret = sqlite3_exec(lsqlite3->sqlite, insert, NULL, NULL, &errmsg);
1141                         if (ret != SQLITE_OK) {
1142                                 if (errmsg) {
1143                                         ldb_set_errstring(module->ldb, errmsg);
1144                                         free(errmsg);
1145                                 }
1146                                 ret = LDB_ERR_OTHER;
1147                                 goto done;
1148                         }
1149                 }
1150         }
1151
1152         if (lsql_ac->callback) {
1153                 req->handle->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1154         }
1155         
1156 done:
1157         req->handle->state = LDB_ASYNC_DONE;
1158         return ret;
1159 }
1160
1161 /* modify a record */
1162 static int lsql_modify(struct ldb_module *module, struct ldb_request *req)
1163 {
1164         struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1165         struct lsql_context *lsql_ac;
1166         struct ldb_message *msg = req->op.mod.message;
1167         long long eid;
1168         char *errmsg;
1169         int i;
1170         int ret = LDB_SUCCESS;
1171
1172         req->handle = init_handle(lsqlite3, module, req);
1173         if (req->handle == NULL) {
1174                 return LDB_ERR_OPERATIONS_ERROR;
1175         }
1176         lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
1177         req->handle->state = LDB_ASYNC_DONE;
1178         req->handle->status = LDB_SUCCESS;
1179
1180         /* See if this is an ltdb special */
1181         if (ldb_dn_is_special(msg->dn)) {
1182                 struct ldb_dn *c;
1183
1184                 c = ldb_dn_explode(lsql_ac, "@SUBCLASSES");
1185                 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
1186 #warning "modify subclasses into object class tree"
1187                         ret = LDB_ERR_UNWILLING_TO_PERFORM;
1188                         goto done;
1189                 }
1190
1191                 /* Others return an error */
1192                 ret = LDB_ERR_UNWILLING_TO_PERFORM;
1193                 goto done;
1194         }
1195
1196         eid = lsqlite3_get_eid(module, msg->dn);
1197         if (eid == -1) {
1198                 ret = LDB_ERR_OTHER;
1199                 goto done;
1200         }
1201
1202         for (i = 0; i < msg->num_elements; i++) {
1203                 const struct ldb_message_element *el = &msg->elements[i];
1204                 const struct ldb_attrib_handler *h;
1205                 int flags = el->flags & LDB_FLAG_MOD_MASK;
1206                 char *attr;
1207                 char *mod;
1208                 int j;
1209
1210                 /* Get a case-folded copy of the attribute name */
1211                 attr = ldb_attr_casefold(lsql_ac, el->name);
1212                 if (attr == NULL) {
1213                         ret = LDB_ERR_OTHER;
1214                         goto done;
1215                 }
1216
1217                 h = ldb_attrib_handler(module->ldb, el->name);
1218
1219                 switch (flags) {
1220
1221                 case LDB_FLAG_MOD_REPLACE:
1222                         
1223                         /* remove all attributes before adding the replacements */
1224                         mod = lsqlite3_tprintf(lsql_ac,
1225                                                 "DELETE FROM ldb_attribute_values "
1226                                                 "WHERE eid = '%lld' "
1227                                                 "AND norm_attr_name = '%q';",
1228                                                 eid, attr);
1229                         if (mod == NULL) {
1230                                 ret = LDB_ERR_OTHER;
1231                                 goto done;
1232                         }
1233
1234                         ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1235                         if (ret != SQLITE_OK) {
1236                                 if (errmsg) {
1237                                         ldb_set_errstring(module->ldb, errmsg);
1238                                         free(errmsg);
1239                                 }
1240                                 ret = LDB_ERR_OTHER;
1241                                 goto done;
1242                         }
1243
1244                         /* MISSING break is INTENTIONAL */
1245
1246                 case LDB_FLAG_MOD_ADD:
1247 #warning "We should throw an error if no value is provided!"
1248                         /* For each value of the specified attribute name... */
1249                         for (j = 0; j < el->num_values; j++) {
1250                                 struct ldb_val value;
1251
1252                                 /* Get a canonicalised copy of the data */
1253                                 h->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
1254                                 if (value.data == NULL) {
1255                                         ret = LDB_ERR_OTHER;
1256                                         goto done;
1257                                 }
1258
1259                                 mod = lsqlite3_tprintf(lsql_ac,
1260                                         "INSERT OR ROLLBACK INTO ldb_attribute_values "
1261                                         "('eid', 'attr_name', 'norm_attr_name',"
1262                                         " 'attr_value', 'norm_attr_value') "
1263                                         "VALUES ('%lld', '%q', '%q', '%q', '%q');",
1264                                         eid, el->name, attr,
1265                                         el->values[j].data, value.data);
1266
1267                                 if (mod == NULL) {
1268                                         ret = LDB_ERR_OTHER;
1269                                         goto done;
1270                                 }
1271
1272                                 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1273                                 if (ret != SQLITE_OK) {
1274                                         if (errmsg) {
1275                                                 ldb_set_errstring(module->ldb, errmsg);
1276                                                 free(errmsg);
1277                                         }
1278                                         ret = LDB_ERR_OTHER;
1279                                         goto done;
1280                                 }
1281                         }
1282
1283                         break;
1284
1285                 case LDB_FLAG_MOD_DELETE:
1286 #warning "We should throw an error if the attribute we are trying to delete does not exist!"
1287                         if (el->num_values == 0) {
1288                                 mod = lsqlite3_tprintf(lsql_ac,
1289                                                         "DELETE FROM ldb_attribute_values "
1290                                                         "WHERE eid = '%lld' "
1291                                                         "AND norm_attr_name = '%q';",
1292                                                         eid, attr);
1293                                 if (mod == NULL) {
1294                                         ret = LDB_ERR_OTHER;
1295                                         goto done;
1296                                 }
1297
1298                                 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1299                                 if (ret != SQLITE_OK) {
1300                                         if (errmsg) {
1301                                                 ldb_set_errstring(module->ldb, errmsg);
1302                                                 free(errmsg);
1303                                         }
1304                                         ret = LDB_ERR_OTHER;
1305                                         goto done;
1306                                 }
1307                         }
1308
1309                         /* For each value of the specified attribute name... */
1310                         for (j = 0; j < el->num_values; j++) {
1311                                 struct ldb_val value;
1312
1313                                 /* Get a canonicalised copy of the data */
1314                                 h->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
1315                                 if (value.data == NULL) {
1316                                         ret = LDB_ERR_OTHER;
1317                                         goto done;
1318                                 }
1319
1320                                 mod = lsqlite3_tprintf(lsql_ac,
1321                                         "DELETE FROM ldb_attribute_values "
1322                                         "WHERE eid = '%lld' "
1323                                         "AND norm_attr_name = '%q' "
1324                                         "AND norm_attr_value = '%q';",
1325                                         eid, attr, value.data);
1326
1327                                 if (mod == NULL) {
1328                                         ret = LDB_ERR_OTHER;
1329                                         goto done;
1330                                 }
1331
1332                                 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1333                                 if (ret != SQLITE_OK) {
1334                                         if (errmsg) {
1335                                                 ldb_set_errstring(module->ldb, errmsg);
1336                                                 free(errmsg);
1337                                         }
1338                                         ret = LDB_ERR_OTHER;
1339                                         goto done;
1340                                 }
1341                         }
1342
1343                         break;
1344                 }
1345         }
1346
1347         if (lsql_ac->callback) {
1348                 req->handle->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1349         }
1350         
1351 done:
1352         req->handle->state = LDB_ASYNC_DONE;
1353         return ret;
1354 }
1355
1356 /* delete a record */
1357 static int lsql_delete(struct ldb_module *module, struct ldb_request *req)
1358 {
1359         struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1360         struct lsql_context *lsql_ac;
1361         long long eid;
1362         char *errmsg;
1363         char *query;
1364         int ret = LDB_SUCCESS;
1365
1366
1367         req->handle = init_handle(lsqlite3, module, req);
1368         if (req->handle == NULL) {
1369                 return LDB_ERR_OPERATIONS_ERROR;
1370         }
1371         lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
1372         req->handle->state = LDB_ASYNC_DONE;
1373         req->handle->status = LDB_SUCCESS;
1374
1375         eid = lsqlite3_get_eid(module, req->op.del.dn);
1376         if (eid == -1) {
1377                 goto done;
1378         }
1379
1380         query = lsqlite3_tprintf(lsql_ac,
1381                                    /* Delete entry */
1382                                    "DELETE FROM ldb_entry WHERE eid = %lld; "
1383                                    /* Delete attributes */
1384                                    "DELETE FROM ldb_attribute_values WHERE eid = %lld; ",
1385                                 eid, eid);
1386         if (query == NULL) {
1387                 ret = LDB_ERR_OTHER;
1388                 goto done;
1389         }
1390
1391         ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1392         if (ret != SQLITE_OK) {
1393                 if (errmsg) {
1394                         ldb_set_errstring(module->ldb, errmsg);
1395                         free(errmsg);
1396                 }
1397                 req->handle->status = LDB_ERR_OPERATIONS_ERROR;
1398                 goto done;
1399         }
1400
1401         if (lsql_ac->callback) {
1402                 ret = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1403         }
1404         
1405 done:
1406         req->handle->state = LDB_ASYNC_DONE;
1407         return ret;
1408 }
1409
1410 /* rename a record */
1411 static int lsql_rename(struct ldb_module *module, struct ldb_request *req)
1412 {
1413         struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1414         struct lsql_context *lsql_ac;
1415         char *new_dn, *new_cdn, *old_cdn;
1416         char *errmsg;
1417         char *query;
1418         int ret = LDB_SUCCESS;
1419
1420         req->handle = init_handle(lsqlite3, module, req);
1421         if (req->handle == NULL) {
1422                 return LDB_ERR_OPERATIONS_ERROR;
1423         }
1424         lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
1425         req->handle->state = LDB_ASYNC_DONE;
1426         req->handle->status = LDB_SUCCESS;
1427
1428         /* create linearized and normalized dns */
1429         old_cdn = ldb_dn_linearize(lsql_ac, ldb_dn_casefold(module->ldb, lsql_ac, req->op.rename.olddn));
1430         new_cdn = ldb_dn_linearize(lsql_ac, ldb_dn_casefold(module->ldb, lsql_ac, req->op.rename.newdn));
1431         new_dn = ldb_dn_linearize(lsql_ac, req->op.rename.newdn);
1432         if (old_cdn == NULL || new_cdn == NULL || new_dn == NULL) {
1433                 goto done;
1434         }
1435
1436         /* build the SQL query */
1437         query = lsqlite3_tprintf(lsql_ac,
1438                                  "UPDATE ldb_entry SET dn = '%q', norm_dn = '%q' "
1439                                  "WHERE norm_dn = '%q';",
1440                                  new_dn, new_cdn, old_cdn);
1441         if (query == NULL) {
1442                 goto done;
1443         }
1444
1445         /* execute */
1446         ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1447         if (ret != SQLITE_OK) {
1448                 if (errmsg) {
1449                         ldb_set_errstring(module->ldb, errmsg);
1450                         free(errmsg);
1451                 }
1452                 ret = LDB_ERR_OPERATIONS_ERROR;
1453                 goto done;
1454         }
1455
1456         if (lsql_ac->callback) {
1457                 ret = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1458         }
1459
1460 done:
1461         req->handle->state = LDB_ASYNC_DONE;
1462         return ret;
1463 }
1464
1465 static int lsql_start_trans(struct ldb_module * module)
1466 {
1467         int ret;
1468         char *errmsg;
1469         struct lsqlite3_private *   lsqlite3 = module->private_data;
1470
1471         if (lsqlite3->trans_count == 0) {
1472                 ret = sqlite3_exec(lsqlite3->sqlite, "BEGIN IMMEDIATE;", NULL, NULL, &errmsg);
1473                 if (ret != SQLITE_OK) {
1474                         if (errmsg) {
1475                                 printf("lsqlite3_start_trans: error: %s\n", errmsg);
1476                                 free(errmsg);
1477                         }
1478                         return -1;
1479                 }
1480         };
1481
1482         lsqlite3->trans_count++;
1483
1484         return 0;
1485 }
1486
1487 static int lsql_end_trans(struct ldb_module *module)
1488 {
1489         int ret;
1490         char *errmsg;
1491         struct lsqlite3_private *lsqlite3 = module->private_data;
1492
1493         if (lsqlite3->trans_count > 0) {
1494                 lsqlite3->trans_count--;
1495         } else return -1;
1496
1497         if (lsqlite3->trans_count == 0) {
1498                 ret = sqlite3_exec(lsqlite3->sqlite, "COMMIT;", NULL, NULL, &errmsg);
1499                 if (ret != SQLITE_OK) {
1500                         if (errmsg) {
1501                                 printf("lsqlite3_end_trans: error: %s\n", errmsg);
1502                                 free(errmsg);
1503                         }
1504                         return -1;
1505                 }
1506         }
1507
1508         return 0;
1509 }
1510
1511 static int lsql_del_trans(struct ldb_module *module)
1512 {
1513         struct lsqlite3_private *lsqlite3 = module->private_data;
1514
1515         if (lsqlite3->trans_count > 0) {
1516                 lsqlite3->trans_count--;
1517         } else return -1;
1518
1519         if (lsqlite3->trans_count == 0) {
1520                 return lsqlite3_safe_rollback(lsqlite3->sqlite);
1521         }
1522
1523         return -1;
1524 }
1525
1526 static int destructor(struct lsqlite3_private *lsqlite3)
1527 {        
1528         if (lsqlite3->sqlite) {
1529                 sqlite3_close(lsqlite3->sqlite);
1530         }
1531         return 0;
1532 }
1533
1534 static int lsql_request(struct ldb_module *module, struct ldb_request *req)
1535 {
1536         return LDB_ERR_OPERATIONS_ERROR;
1537 }
1538
1539 static int lsql_wait(struct ldb_handle *handle, enum ldb_wait_type type)
1540 {
1541         return handle->status;
1542 }
1543
1544 /*
1545  * Table of operations for the sqlite3 backend
1546  */
1547 static const struct ldb_module_ops lsqlite3_ops = {
1548         .name              = "sqlite",
1549         .search            = lsql_search,
1550         .add               = lsql_add,
1551         .modify            = lsql_modify,
1552         .del               = lsql_delete,
1553         .rename            = lsql_rename,
1554         .request           = lsql_request,
1555         .start_transaction = lsql_start_trans,
1556         .end_transaction   = lsql_end_trans,
1557         .del_transaction   = lsql_del_trans,
1558         .wait              = lsql_wait,
1559 };
1560
1561 /*
1562  * Static functions
1563  */
1564
1565 static int initialize(struct lsqlite3_private *lsqlite3,
1566                       struct ldb_context *ldb, const char *url, int flags)
1567 {
1568         TALLOC_CTX *local_ctx;
1569         long long queryInt;
1570         int rollback = 0;
1571         char *errmsg;
1572         char *schema;
1573         int ret;
1574
1575         /* create a local ctx */
1576         local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_rename local context");
1577         if (local_ctx == NULL) {
1578                 return -1;
1579         }
1580
1581         schema = lsqlite3_tprintf(local_ctx,
1582                 
1583                 
1584                 "CREATE TABLE ldb_info AS "
1585                 "  SELECT 'LDB' AS database_type,"
1586                 "         '1.0' AS version;"
1587                 
1588                 /*
1589                  * The entry table holds the information about an entry. 
1590                  * This table is used to obtain the EID of the entry and to 
1591                  * support scope=one and scope=base.  The parent and child
1592                  * table is included in the entry table since all the other
1593                  * attributes are dependent on EID.
1594                  */
1595                 "CREATE TABLE ldb_entry "
1596                 "("
1597                 "  eid     INTEGER PRIMARY KEY AUTOINCREMENT,"
1598                 "  dn      TEXT UNIQUE NOT NULL,"
1599                 "  norm_dn TEXT UNIQUE NOT NULL"
1600                 ");"
1601                 
1602
1603                 "CREATE TABLE ldb_object_classes"
1604                 "("
1605                 "  class_name            TEXT PRIMARY KEY,"
1606                 "  parent_class_name     TEXT,"
1607                 "  tree_key              TEXT UNIQUE,"
1608                 "  max_child_num         INTEGER DEFAULT 0"
1609                 ");"
1610                 
1611                 /*
1612                  * We keep a full listing of attribute/value pairs here
1613                  */
1614                 "CREATE TABLE ldb_attribute_values"
1615                 "("
1616                 "  eid             INTEGER REFERENCES ldb_entry,"
1617                 "  attr_name       TEXT,"
1618                 "  norm_attr_name  TEXT,"
1619                 "  attr_value      TEXT,"
1620                 "  norm_attr_value TEXT "
1621                 ");"
1622                 
1623                
1624                 /*
1625                  * Indexes
1626                  */
1627                 "CREATE INDEX ldb_attribute_values_eid_idx "
1628                 "  ON ldb_attribute_values (eid);"
1629                 
1630                 "CREATE INDEX ldb_attribute_values_name_value_idx "
1631                 "  ON ldb_attribute_values (attr_name, norm_attr_value);"
1632                 
1633                 
1634
1635                 /*
1636                  * Triggers
1637                  */
1638  
1639                 "CREATE TRIGGER ldb_object_classes_insert_tr"
1640                 "  AFTER INSERT"
1641                 "  ON ldb_object_classes"
1642                 "  FOR EACH ROW"
1643                 "    BEGIN"
1644                 "      UPDATE ldb_object_classes"
1645                 "        SET tree_key = COALESCE(tree_key, "
1646                 "              ("
1647                 "                SELECT tree_key || "
1648                 "                       (SELECT base160(max_child_num + 1)"
1649                 "                                FROM ldb_object_classes"
1650                 "                                WHERE class_name = "
1651                 "                                      new.parent_class_name)"
1652                 "                  FROM ldb_object_classes "
1653                 "                  WHERE class_name = new.parent_class_name "
1654                 "              ));"
1655                 "      UPDATE ldb_object_classes "
1656                 "        SET max_child_num = max_child_num + 1"
1657                 "        WHERE class_name = new.parent_class_name;"
1658                 "    END;"
1659
1660                 /*
1661                  * Table initialization
1662                  */
1663
1664                 "INSERT INTO ldb_object_classes "
1665                 "    (class_name, tree_key) "
1666                 "  VALUES "
1667                 "    ('TOP', '0001');");
1668         
1669         /* Skip protocol indicator of url  */
1670         if (strncmp(url, "sqlite3://", 10) != 0) {
1671                 return SQLITE_MISUSE;
1672         }
1673         
1674         /* Update pointer to just after the protocol indicator */
1675         url += 10;
1676         
1677         /* Try to open the (possibly empty/non-existent) database */
1678         if ((ret = sqlite3_open(url, &lsqlite3->sqlite)) != SQLITE_OK) {
1679                 return ret;
1680         }
1681         
1682         /* In case this is a new database, enable auto_vacuum */
1683         ret = sqlite3_exec(lsqlite3->sqlite, "PRAGMA auto_vacuum = 1;", NULL, NULL, &errmsg);
1684         if (ret != SQLITE_OK) {
1685                 if (errmsg) {
1686                         printf("lsqlite3 initializaion error: %s\n", errmsg);
1687                         free(errmsg);
1688                 }
1689                 goto failed;
1690         }
1691         
1692         if (flags & LDB_FLG_NOSYNC) {
1693                 /* DANGEROUS */
1694                 ret = sqlite3_exec(lsqlite3->sqlite, "PRAGMA synchronous = OFF;", NULL, NULL, &errmsg);
1695                 if (ret != SQLITE_OK) {
1696                         if (errmsg) {
1697                                 printf("lsqlite3 initializaion error: %s\n", errmsg);
1698                                 free(errmsg);
1699                         }
1700                         goto failed;
1701                 }
1702         }
1703         
1704         /* */
1705         
1706         /* Establish a busy timeout of 30 seconds */
1707         if ((ret = sqlite3_busy_timeout(lsqlite3->sqlite,
1708                                         30000)) != SQLITE_OK) {
1709                 return ret;
1710         }
1711
1712         /* Create a function, callable from sql, to increment a tree_key */
1713         if ((ret =
1714              sqlite3_create_function(lsqlite3->sqlite,/* handle */
1715                                      "base160_next",  /* function name */
1716                                      1,               /* number of args */
1717                                      SQLITE_ANY,      /* preferred text type */
1718                                      NULL,            /* user data */
1719                                      base160next_sql, /* called func */
1720                                      NULL,            /* step func */
1721                                      NULL             /* final func */
1722                      )) != SQLITE_OK) {
1723                 return ret;
1724         }
1725
1726         /* Create a function, callable from sql, to convert int to base160 */
1727         if ((ret =
1728              sqlite3_create_function(lsqlite3->sqlite,/* handle */
1729                                      "base160",       /* function name */
1730                                      1,               /* number of args */
1731                                      SQLITE_ANY,      /* preferred text type */
1732                                      NULL,            /* user data */
1733                                      base160_sql,     /* called func */
1734                                      NULL,            /* step func */
1735                                      NULL             /* final func */
1736                      )) != SQLITE_OK) {
1737                 return ret;
1738         }
1739
1740         /* Create a function, callable from sql, to perform various comparisons */
1741         if ((ret =
1742              sqlite3_create_function(lsqlite3->sqlite, /* handle */
1743                                      "ldap_compare",   /* function name */
1744                                      4,                /* number of args */
1745                                      SQLITE_ANY,       /* preferred text type */
1746                                      ldb  ,            /* user data */
1747                                      lsqlite3_compare, /* called func */
1748                                      NULL,             /* step func */
1749                                      NULL              /* final func */
1750                      )) != SQLITE_OK) {
1751                 return ret;
1752         }
1753
1754         /* Begin a transaction */
1755         ret = sqlite3_exec(lsqlite3->sqlite, "BEGIN EXCLUSIVE;", NULL, NULL, &errmsg);
1756         if (ret != SQLITE_OK) {
1757                 if (errmsg) {
1758                         printf("lsqlite3: initialization error: %s\n", errmsg);
1759                         free(errmsg);
1760                 }
1761                 goto failed;
1762         }
1763         rollback = 1;
1764  
1765         /* Determine if this is a new database.  No tables means it is. */
1766         if (query_int(lsqlite3,
1767                       &queryInt,
1768                       "SELECT COUNT(*)\n"
1769                       "  FROM sqlite_master\n"
1770                       "  WHERE type = 'table';") != 0) {
1771                 goto failed;
1772         }
1773         
1774         if (queryInt == 0) {
1775                 /*
1776                  * Create the database schema
1777                  */
1778                 ret = sqlite3_exec(lsqlite3->sqlite, schema, NULL, NULL, &errmsg);
1779                 if (ret != SQLITE_OK) {
1780                         if (errmsg) {
1781                                 printf("lsqlite3 initializaion error: %s\n", errmsg);
1782                                 free(errmsg);
1783                         }
1784                         goto failed;
1785                 }
1786         } else {
1787                 /*
1788                  * Ensure that the database we opened is one of ours
1789                  */
1790                 if (query_int(lsqlite3,
1791                               &queryInt,
1792                               "SELECT "
1793                               "  (SELECT COUNT(*) = 2"
1794                               "     FROM sqlite_master "
1795                               "     WHERE type = 'table' "
1796                               "       AND name IN "
1797                               "         ("
1798                               "           'ldb_entry', "
1799                               "           'ldb_object_classes' "
1800                               "         ) "
1801                               "  ) "
1802                               "  AND "
1803                               "  (SELECT 1 "
1804                               "     FROM ldb_info "
1805                               "     WHERE database_type = 'LDB' "
1806                               "       AND version = '1.0'"
1807                               "  );") != 0 ||
1808                     queryInt != 1) {
1809                         
1810                         /* It's not one that we created.  See ya! */
1811                         goto failed;
1812                 }
1813         }
1814         
1815         /* Commit the transaction */
1816         ret = sqlite3_exec(lsqlite3->sqlite, "COMMIT;", NULL, NULL, &errmsg);
1817         if (ret != SQLITE_OK) {
1818                 if (errmsg) {
1819                         printf("lsqlite3: iniialization error: %s\n", errmsg);
1820                         free(errmsg);
1821                 }
1822                 goto failed;
1823         }
1824  
1825         return SQLITE_OK;
1826
1827 failed:
1828         if (rollback) lsqlite3_safe_rollback(lsqlite3->sqlite); 
1829         sqlite3_close(lsqlite3->sqlite);
1830         return -1;
1831 }
1832
1833 /*
1834  * connect to the database
1835  */
1836 static int lsqlite3_connect(struct ldb_context *ldb,
1837                             const char *url, 
1838                             unsigned int flags, 
1839                             const char *options[],
1840                             struct ldb_module **module)
1841 {
1842         int                         i;
1843         int                         ret;
1844         struct lsqlite3_private *   lsqlite3 = NULL;
1845         
1846         lsqlite3 = talloc(ldb, struct lsqlite3_private);
1847         if (!lsqlite3) {
1848                 goto failed;
1849         }
1850         
1851         lsqlite3->sqlite = NULL;
1852         lsqlite3->options = NULL;
1853         lsqlite3->trans_count = 0;
1854         
1855         ret = initialize(lsqlite3, ldb, url, flags);
1856         if (ret != SQLITE_OK) {
1857                 goto failed;
1858         }
1859         
1860         talloc_set_destructor(lsqlite3, destructor);
1861         
1862
1863
1864         *module = talloc(ldb, struct ldb_module);
1865         if (!module) {
1866                 ldb_oom(ldb);
1867                 goto failed;
1868         }
1869         talloc_set_name_const(*module, "ldb_sqlite3 backend");
1870         (*module)->ldb = ldb;
1871         (*module)->prev = (*module)->next = NULL;
1872         (*module)->private_data = lsqlite3;
1873         (*module)->ops = &lsqlite3_ops;
1874
1875         if (options) {
1876                 /*
1877                  * take a copy of the options array, so we don't have to rely
1878                  * on the caller keeping it around (it might be dynamic)
1879                  */
1880                 for (i=0;options[i];i++) ;
1881                 
1882                 lsqlite3->options = talloc_array(lsqlite3, char *, i+1);
1883                 if (!lsqlite3->options) {
1884                         goto failed;
1885                 }
1886                 
1887                 for (i=0;options[i];i++) {
1888                         
1889                         lsqlite3->options[i+1] = NULL;
1890                         lsqlite3->options[i] =
1891                                 talloc_strdup(lsqlite3->options, options[i]);
1892                         if (!lsqlite3->options[i]) {
1893                                 goto failed;
1894                         }
1895                 }
1896         }
1897         
1898         return 0;
1899         
1900 failed:
1901         if (lsqlite3->sqlite != NULL) {
1902                 (void) sqlite3_close(lsqlite3->sqlite);
1903         }
1904         talloc_free(lsqlite3);
1905         return -1;
1906 }
1907
1908 int ldb_sqlite3_init(void)
1909 {
1910         return ldb_register_backend("sqlite3", lsqlite3_connect);
1911 }