be3decfec2f22c3f4af89650b6c25e4998467388
[samba.git] / source / 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 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 "ldb_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_schema_attribute *a;
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                 a = ldb_schema_attribute_by_name(module->ldb, attr);
346
347                 /* Get a canonicalised copy of the data */
348                 a->syntax->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                         const char *cdn = ldb_dn_get_casefold(
372                                                 ldb_dn_new(mem_ctx, module->ldb,
373                                                               (const char *)value.data));
374
375                         return lsqlite3_tprintf(mem_ctx,
376                                                 "SELECT eid FROM ldb_entry "
377                                                 "WHERE norm_dn = '%q'", cdn);
378
379                 } else {
380                         /* A normal query. */
381                         return lsqlite3_tprintf(mem_ctx,
382                                                 "SELECT eid FROM ldb_attribute_values "
383                                                 "WHERE norm_attr_name = '%q' "
384                                                 "AND norm_attr_value = '%q'",
385                                                 attr,
386                                                 value.data);
387
388                 }
389
390         case LDB_OP_SUBSTRING:
391
392                 wild_card_string = talloc_strdup(mem_ctx,
393                                         (t->u.substring.start_with_wildcard)?"*":"");
394                 if (wild_card_string == NULL) return NULL;
395
396                 for (i = 0; t->u.substring.chunks[i]; i++) {
397                         wild_card_string = talloc_asprintf_append(wild_card_string, "%s*",
398                                                         t->u.substring.chunks[i]->data);
399                         if (wild_card_string == NULL) return NULL;
400                 }
401
402                 if ( ! t->u.substring.end_with_wildcard ) {
403                         /* remove last wildcard */
404                         wild_card_string[strlen(wild_card_string) - 1] = '\0';
405                 }
406
407                 attr = ldb_attr_casefold(mem_ctx, t->u.substring.attr);
408                 if (attr == NULL) return NULL;
409                 a = ldb_schema_attribute_by_name(module->ldb, attr);
410
411                 subval.data = (void *)wild_card_string;
412                 subval.length = strlen(wild_card_string) + 1;
413
414                 /* Get a canonicalised copy of the data */
415                 a->syntax->canonicalise_fn(module->ldb, mem_ctx, &(subval), &value);
416                 if (value.data == NULL) {
417                         return NULL;
418                 }
419
420                 return lsqlite3_tprintf(mem_ctx,
421                                         "SELECT eid FROM ldb_attribute_values "
422                                         "WHERE norm_attr_name = '%q' "
423                                         "AND norm_attr_value GLOB '%q'",
424                                         attr,
425                                         value.data);
426
427         case LDB_OP_GREATER:
428                 attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
429                 if (attr == NULL) return NULL;
430                 a = ldb_schema_attribute_by_name(module->ldb, attr);
431
432                 /* Get a canonicalised copy of the data */
433                 a->syntax->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
434                 if (value.data == NULL) {
435                         return NULL;
436                 }
437
438                 return lsqlite3_tprintf(mem_ctx,
439                                         "SELECT eid FROM ldb_attribute_values "
440                                         "WHERE norm_attr_name = '%q' "
441                                         "AND ldap_compare(norm_attr_value, '>=', '%q', '%q') ",
442                                         attr,
443                                         value.data,
444                                         attr);
445
446         case LDB_OP_LESS:
447                 attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
448                 if (attr == NULL) return NULL;
449                 a = ldb_schema_attribute_by_name(module->ldb, attr);
450
451                 /* Get a canonicalised copy of the data */
452                 a->syntax->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
453                 if (value.data == NULL) {
454                         return NULL;
455                 }
456
457                 return lsqlite3_tprintf(mem_ctx,
458                                         "SELECT eid FROM ldb_attribute_values "
459                                         "WHERE norm_attr_name = '%q' "
460                                         "AND ldap_compare(norm_attr_value, '<=', '%q', '%q') ",
461                                         attr,
462                                         value.data,
463                                         attr);
464
465         case LDB_OP_PRESENT:
466                 if (strcasecmp(t->u.present.attr, "dn") == 0) {
467                         return talloc_strdup(mem_ctx, "SELECT eid FROM ldb_entry");
468                 }
469
470                 attr = ldb_attr_casefold(mem_ctx, t->u.present.attr);
471                 if (attr == NULL) return NULL;
472
473                 return lsqlite3_tprintf(mem_ctx,
474                                         "SELECT eid FROM ldb_attribute_values "
475                                         "WHERE norm_attr_name = '%q' ",
476                                         attr);
477
478         case LDB_OP_APPROX:
479                 attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
480                 if (attr == NULL) return NULL;
481                 a = ldb_schema_attribute_by_name(module->ldb, attr);
482
483                 /* Get a canonicalised copy of the data */
484                 a->syntax->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
485                 if (value.data == NULL) {
486                         return NULL;
487                 }
488
489                 return lsqlite3_tprintf(mem_ctx,
490                                         "SELECT eid FROM ldb_attribute_values "
491                                         "WHERE norm_attr_name = '%q' "
492                                         "AND ldap_compare(norm_attr_value, '~%', 'q', '%q') ",
493                                         attr,
494                                         value.data,
495                                         attr);
496                 
497         case LDB_OP_EXTENDED:
498 #warning  "work out how to handle bitops"
499                 return NULL;
500
501         default:
502                 break;
503         };
504
505         /* should never occur */
506         abort();
507         return NULL;
508 }
509
510 /*
511  * query_int()
512  *
513  * This function is used for the common case of queries that return a single
514  * integer value.
515  *
516  * NOTE: If more than one value is returned by the query, all but the first
517  * one will be ignored.
518  */
519 static int
520 query_int(const struct lsqlite3_private * lsqlite3,
521           long long * pRet,
522           const char * pSql,
523           ...)
524 {
525         int             ret;
526         int             bLoop;
527         char *          p;
528         sqlite3_stmt *  pStmt;
529         va_list         args;
530         
531         /* Begin access to variable argument list */
532         va_start(args, pSql);
533         
534         /* Format the query */
535         if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
536                 return SQLITE_NOMEM;
537         }
538         
539         /*
540          * Prepare and execute the SQL statement.  Loop allows retrying on
541          * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
542          * requiring retrying the operation.
543          */
544         for (bLoop = TRUE; bLoop; ) {
545                 
546                 /* Compile the SQL statement into sqlite virtual machine */
547                 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
548                                            p,
549                                            -1,
550                                            &pStmt,
551                                            NULL)) == SQLITE_SCHEMA) {
552                         if (stmtGetEID != NULL) {
553                                 sqlite3_finalize(stmtGetEID);
554                                 stmtGetEID = NULL;
555                         }
556                         continue;
557                 } else if (ret != SQLITE_OK) {
558                         break;
559                 }
560                 
561                 /* One row expected */
562                 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
563                         if (stmtGetEID != NULL) {
564                                 sqlite3_finalize(stmtGetEID);
565                                 stmtGetEID = NULL;
566                         }
567                         (void) sqlite3_finalize(pStmt);
568                         continue;
569                 } else if (ret != SQLITE_ROW) {
570                         (void) sqlite3_finalize(pStmt);
571                         break;
572                 }
573                 
574                 /* Get the value to be returned */
575                 *pRet = sqlite3_column_int64(pStmt, 0);
576                 
577                 /* Free the virtual machine */
578                 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
579                         if (stmtGetEID != NULL) {
580                                 sqlite3_finalize(stmtGetEID);
581                                 stmtGetEID = NULL;
582                         }
583                         continue;
584                 } else if (ret != SQLITE_OK) {
585                         (void) sqlite3_finalize(pStmt);
586                         break;
587                 }
588                 
589                 /*
590                  * Normal condition is only one time through loop.  Loop is
591                  * rerun in error conditions, via "continue", above.
592                  */
593                 bLoop = FALSE;
594         }
595         
596         /* All done with variable argument list */
597         va_end(args);
598         
599
600         /* Free the memory we allocated for our query string */
601         sqlite3_free(p);
602         
603         return ret;
604 }
605
606 /*
607  * This is a bad hack to support ldap style comparisons whithin sqlite.
608  * val is the attribute in the row currently under test
609  * func is the desired test "<=" ">=" "~" ":"
610  * cmp is the value to compare against (eg: "test")
611  * attr is the attribute name the value of which we want to test
612  */
613
614 static void lsqlite3_compare(sqlite3_context *ctx, int argc,
615                                         sqlite3_value **argv)
616 {
617         struct ldb_context *ldb = (struct ldb_context *)sqlite3_user_data(ctx);
618         const char *val = (const char *)sqlite3_value_text(argv[0]);
619         const char *func = (const char *)sqlite3_value_text(argv[1]);
620         const char *cmp = (const char *)sqlite3_value_text(argv[2]);
621         const char *attr = (const char *)sqlite3_value_text(argv[3]);
622         const struct ldb_schema_attribute *a;
623         struct ldb_val valX;
624         struct ldb_val valY;
625         int ret;
626
627         switch (func[0]) {
628         /* greater */
629         case '>': /* >= */
630                 a = ldb_schema_attribute_by_name(ldb, attr);
631                 valX.data = (void *)cmp;
632                 valX.length = strlen(cmp);
633                 valY.data = (void *)val;
634                 valY.length = strlen(val);
635                 ret = a->syntax->comparison_fn(ldb, ldb, &valY, &valX);
636                 if (ret >= 0)
637                         sqlite3_result_int(ctx, 1);
638                 else
639                         sqlite3_result_int(ctx, 0);
640                 return;
641
642         /* lesser */
643         case '<': /* <= */
644                 a = ldb_schema_attribute_by_name(ldb, attr);
645                 valX.data = (void *)cmp;
646                 valX.length = strlen(cmp);
647                 valY.data = (void *)val;
648                 valY.length = strlen(val);
649                 ret = a->syntax->comparison_fn(ldb, ldb, &valY, &valX);
650                 if (ret <= 0)
651                         sqlite3_result_int(ctx, 1);
652                 else
653                         sqlite3_result_int(ctx, 0);
654                 return;
655
656         /* approx */
657         case '~':
658                 /* TODO */
659                 sqlite3_result_int(ctx, 0);
660                 return;
661
662         /* bitops */
663         case ':':
664                 /* TODO */
665                 sqlite3_result_int(ctx, 0);
666                 return;
667
668         default:
669                 break;
670         }
671
672         sqlite3_result_error(ctx, "Value must start with a special operation char (<>~:)!", -1);
673         return;
674 }
675
676
677 /* rename a record */
678 static int lsqlite3_safe_rollback(sqlite3 *sqlite)
679 {
680         char *errmsg;
681         int ret;
682
683         /* execute */
684         ret = sqlite3_exec(sqlite, "ROLLBACK;", NULL, NULL, &errmsg);
685         if (ret != SQLITE_OK) {
686                 if (errmsg) {
687                         printf("lsqlite3_safe_rollback: Error: %s\n", errmsg);
688                         free(errmsg);
689                 }
690                 return -1;
691         }
692
693         return 0;
694 }
695
696 /* return an eid as result */
697 static int lsqlite3_eid_callback(void *result, int col_num, char **cols, char **names)
698 {
699         long long *eid = (long long *)result;
700
701         if (col_num != 1) return SQLITE_ABORT;
702         if (strcasecmp(names[0], "eid") != 0) return SQLITE_ABORT;
703
704         *eid = atoll(cols[0]);
705         return SQLITE_OK;
706 }
707
708 /*
709  * add a single set of ldap message values to a ldb_message
710  */
711 static int lsqlite3_search_callback(void *result, int col_num, char **cols, char **names)
712 {
713         struct ldb_handle *handle = talloc_get_type(result, struct ldb_handle);
714         struct lsql_context *ac = talloc_get_type(handle->private_data, struct lsql_context);
715         struct ldb_message *msg;
716         long long eid;
717         int i;
718
719         /* eid, dn, attr_name, attr_value */
720         if (col_num != 4)
721                 return SQLITE_ABORT;
722
723         eid = atoll(cols[0]);
724
725         if (eid != ac->current_eid) { /* here begin a new entry */
726
727                 /* call the async callback for the last entry
728                  * except the first time */
729                 if (ac->current_eid != 0) {
730                         ac->ares->message = ldb_msg_canonicalize(ac->module->ldb, ac->ares->message);
731                         if (ac->ares->message == NULL)
732                                 return SQLITE_ABORT;
733                         
734                         handle->status = ac->callback(ac->module->ldb, ac->context, ac->ares);
735                         if (handle->status != LDB_SUCCESS)
736                                 return SQLITE_ABORT;
737                 }
738
739                 /* start over */
740                 ac->ares = talloc_zero(ac, struct ldb_reply);
741                 if (!ac->ares)
742                         return SQLITE_ABORT;
743
744                 ac->ares->message = ldb_msg_new(ac->ares);
745                 if (!ac->ares->message)
746                         return SQLITE_ABORT;
747
748                 ac->ares->type = LDB_REPLY_ENTRY;
749                 ac->current_eid = eid;
750         }
751
752         msg = ac->ares->message;
753
754         if (msg->dn == NULL) {
755                 msg->dn = ldb_dn_new(msg, ac->module->ldb, cols[1]);
756                 if (msg->dn == NULL)
757                         return SQLITE_ABORT;
758         }
759
760         if (ac->attrs) {
761                 int found = 0;
762                 for (i = 0; ac->attrs[i]; i++) {
763                         if (strcasecmp(cols[2], ac->attrs[i]) == 0) {
764                                 found = 1;
765                                 break;
766                         }
767                 }
768                 if (!found) return SQLITE_OK;
769         }
770
771         if (ldb_msg_add_string(msg, cols[2], cols[3]) != 0) {
772                 return SQLITE_ABORT;
773         }
774
775         return SQLITE_OK;
776 }
777
778
779 /*
780  * lsqlite3_get_eid()
781  * lsqlite3_get_eid_ndn()
782  *
783  * These functions are used for the very common case of retrieving an EID value
784  * given a (normalized) DN.
785  */
786
787 static long long lsqlite3_get_eid_ndn(sqlite3 *sqlite, void *mem_ctx, const char *norm_dn)
788 {
789         char *errmsg;
790         char *query;
791         long long eid = -1;
792         long long ret;
793
794         /* get object eid */
795         query = lsqlite3_tprintf(mem_ctx, "SELECT eid "
796                                           "FROM ldb_entry "
797                                           "WHERE norm_dn = '%q';", norm_dn);
798         if (query == NULL) return -1;
799
800         ret = sqlite3_exec(sqlite, query, lsqlite3_eid_callback, &eid, &errmsg);
801         if (ret != SQLITE_OK) {
802                 if (errmsg) {
803                         printf("lsqlite3_get_eid: Fatal Error: %s\n", errmsg);
804                         free(errmsg);
805                 }
806                 return -1;
807         }
808
809         return eid;
810 }
811
812 static long long lsqlite3_get_eid(struct ldb_module *module, struct ldb_dn *dn)
813 {
814         TALLOC_CTX *local_ctx;
815         struct lsqlite3_private *lsqlite3 = module->private_data;
816         long long eid = -1;
817         char *cdn;
818
819         /* ignore ltdb specials */
820         if (ldb_dn_is_special(dn)) {
821                 return -1;
822         }
823
824         /* create a local ctx */
825         local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_get_eid local context");
826         if (local_ctx == NULL) {
827                 return -1;
828         }
829
830         cdn = ldb_dn_alloc_casefold(local_ctx, dn);
831         if (!cdn) goto done;
832
833         eid = lsqlite3_get_eid_ndn(lsqlite3->sqlite, local_ctx, cdn);
834
835 done:
836         talloc_free(local_ctx);
837         return eid;
838 }
839
840 /*
841  * Interface functions referenced by lsqlite3_ops
842  */
843
844 /* search for matching records, by tree */
845 int lsql_search(struct ldb_module *module, struct ldb_request *req)
846 {
847         struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
848         struct lsql_context *lsql_ac;
849         char *norm_basedn;
850         char *sqlfilter;
851         char *errmsg;
852         char *query = NULL;
853         int ret;
854
855         req->handle = init_handle(lsqlite3, module, req);
856         if (req->handle == NULL) {
857                 return LDB_ERR_OPERATIONS_ERROR;
858         }
859
860         lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
861
862         if ((( ! ldb_dn_is_valid(req->op.search.base)) || ldb_dn_is_null(req->op.search.base)) &&
863             (req->op.search.scope == LDB_SCOPE_BASE || req->op.search.scope == LDB_SCOPE_ONELEVEL))
864                 return LDB_ERR_OPERATIONS_ERROR;
865
866         if (req->op.search.base) {
867                 norm_basedn = ldb_dn_alloc_casefold(lsql_ac, req->op.search.base);
868                 if (norm_basedn == NULL) {
869                         ret = LDB_ERR_INVALID_DN_SYNTAX;
870                         goto failed;
871                 }
872         } else norm_basedn = talloc_strdup(lsql_ac, "");
873
874         /* Convert filter into a series of SQL conditions (constraints) */
875         sqlfilter = parsetree_to_sql(module, lsql_ac, req->op.search.tree);
876         
877         switch(req->op.search.scope) {
878         case LDB_SCOPE_DEFAULT:
879         case LDB_SCOPE_SUBTREE:
880                 if (*norm_basedn != '\0') {
881                         query = lsqlite3_tprintf(lsql_ac,
882                                 "SELECT entry.eid,\n"
883                                 "       entry.dn,\n"
884                                 "       av.attr_name,\n"
885                                 "       av.attr_value\n"
886                                 "  FROM ldb_entry AS entry\n"
887
888                                 "  LEFT OUTER JOIN ldb_attribute_values AS av\n"
889                                 "    ON av.eid = entry.eid\n"
890
891                                 "  WHERE entry.eid IN\n"
892                                 "    (SELECT DISTINCT ldb_entry.eid\n"
893                                 "       FROM ldb_entry\n"
894                                 "       WHERE (ldb_entry.norm_dn GLOB('*,%q')\n"
895                                 "       OR ldb_entry.norm_dn = '%q')\n"
896                                 "       AND ldb_entry.eid IN\n"
897                                 "         (%s)\n"
898                                 "    )\n"
899
900                                 "  ORDER BY entry.eid ASC;",
901                                 norm_basedn,
902                                 norm_basedn,
903                                 sqlfilter);
904                 } else {
905                         query = lsqlite3_tprintf(lsql_ac,
906                                 "SELECT entry.eid,\n"
907                                 "       entry.dn,\n"
908                                 "       av.attr_name,\n"
909                                 "       av.attr_value\n"
910                                 "  FROM ldb_entry AS entry\n"
911
912                                 "  LEFT OUTER JOIN ldb_attribute_values AS av\n"
913                                 "    ON av.eid = entry.eid\n"
914
915                                 "  WHERE entry.eid IN\n"
916                                 "    (SELECT DISTINCT ldb_entry.eid\n"
917                                 "       FROM ldb_entry\n"
918                                 "       WHERE ldb_entry.eid IN\n"
919                                 "         (%s)\n"
920                                 "    )\n"
921
922                                 "  ORDER BY entry.eid ASC;",
923                                 sqlfilter);
924                 }
925
926                 break;
927                 
928         case LDB_SCOPE_BASE:
929                 query = lsqlite3_tprintf(lsql_ac,
930                         "SELECT entry.eid,\n"
931                         "       entry.dn,\n"
932                         "       av.attr_name,\n"
933                         "       av.attr_value\n"
934                         "  FROM ldb_entry AS entry\n"
935
936                         "  LEFT OUTER JOIN ldb_attribute_values AS av\n"
937                         "    ON av.eid = entry.eid\n"
938
939                         "  WHERE entry.eid IN\n"
940                         "    (SELECT DISTINCT ldb_entry.eid\n"
941                         "       FROM ldb_entry\n"
942                         "       WHERE ldb_entry.norm_dn = '%q'\n"
943                         "         AND ldb_entry.eid IN\n"
944                         "           (%s)\n"
945                         "    )\n"
946
947                         "  ORDER BY entry.eid ASC;",
948                         norm_basedn,
949                         sqlfilter);
950                 break;
951                 
952         case LDB_SCOPE_ONELEVEL:
953                 query = lsqlite3_tprintf(lsql_ac,
954                         "SELECT entry.eid,\n"
955                         "       entry.dn,\n"
956                         "       av.attr_name,\n"
957                         "       av.attr_value\n"
958                         "  FROM ldb_entry AS entry\n"
959
960                         "  LEFT OUTER JOIN ldb_attribute_values AS av\n"
961                         "    ON av.eid = entry.eid\n"
962
963                         "  WHERE entry.eid IN\n"
964                         "    (SELECT DISTINCT ldb_entry.eid\n"
965                         "       FROM ldb_entry\n"
966                         "       WHERE norm_dn GLOB('*,%q')\n"
967                         "         AND NOT norm_dn GLOB('*,*,%q')\n"
968                         "         AND ldb_entry.eid IN\n(%s)\n"
969                         "    )\n"
970
971                         "  ORDER BY entry.eid ASC;",
972                         norm_basedn,
973                         norm_basedn,
974                         sqlfilter);
975                 break;
976         }
977
978         if (query == NULL) {
979                 goto failed;
980         }
981
982         /* * /
983         printf ("%s\n", query);
984         / * */
985
986         lsql_ac->current_eid = 0;
987         lsql_ac->attrs = req->op.search.attrs;
988         lsql_ac->ares = NULL;
989
990         req->handle->state = LDB_ASYNC_PENDING;
991
992         ret = sqlite3_exec(lsqlite3->sqlite, query, lsqlite3_search_callback, req->handle, &errmsg);
993         if (ret != SQLITE_OK) {
994                 if (errmsg) {
995                         ldb_set_errstring(module->ldb, errmsg);
996                         free(errmsg);
997                 }
998                 goto failed;
999         }
1000
1001         /* complete the last message if any */
1002         if (lsql_ac->ares) {
1003                 lsql_ac->ares->message = ldb_msg_canonicalize(module->ldb, lsql_ac->ares->message);
1004                 if (lsql_ac->ares->message == NULL)
1005                         goto failed;
1006                         
1007                 req->handle->status = lsql_ac->callback(module->ldb, lsql_ac->context, lsql_ac->ares);
1008                 if (req->handle->status != LDB_SUCCESS)
1009                         goto failed;
1010         }
1011
1012         req->handle->state = LDB_ASYNC_DONE;
1013
1014         return LDB_SUCCESS;
1015
1016 failed:
1017         return LDB_ERR_OPERATIONS_ERROR;
1018 }
1019
1020 /* add a record */
1021 static int lsql_add(struct ldb_module *module, struct ldb_request *req)
1022 {
1023         struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1024         struct lsql_context *lsql_ac;
1025         struct ldb_message *msg = req->op.add.message;
1026         long long eid;
1027         char *dn, *ndn;
1028         char *errmsg;
1029         char *query;
1030         int i;
1031         int ret = LDB_SUCCESS;
1032
1033         req->handle = init_handle(lsqlite3, module, req);
1034         if (req->handle == NULL) {
1035                 return LDB_ERR_OPERATIONS_ERROR;
1036         }
1037         lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
1038         req->handle->state = LDB_ASYNC_DONE;
1039         req->handle->status = LDB_SUCCESS;
1040
1041         /* See if this is an ltdb special */
1042         if (ldb_dn_is_special(msg->dn)) {
1043                 struct ldb_dn *c;
1044
1045                 c = ldb_dn_new(lsql_ac, module->ldb, "@SUBCLASSES");
1046                 if (ldb_dn_compare(msg->dn, c) == 0) {
1047 #warning "insert subclasses into object class tree"
1048                         ret = LDB_ERR_UNWILLING_TO_PERFORM;
1049                         goto done;
1050                 }
1051
1052 /*
1053                 c = ldb_dn_new(local_ctx, module->ldb, "@INDEXLIST");
1054                 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
1055 #warning "should we handle indexes somehow ?"
1056                         ret = LDB_ERR_UNWILLING_TO_PERFORM;
1057                         goto done;
1058                 }
1059 */
1060                 /* Others return an error */
1061                 ret = LDB_ERR_UNWILLING_TO_PERFORM;
1062                 goto done;
1063         }
1064
1065         /* create linearized and normalized dns */
1066         dn = ldb_dn_alloc_linearized(lsql_ac, msg->dn);
1067         ndn = ldb_dn_alloc_casefold(lsql_ac, msg->dn);
1068         if (dn == NULL || ndn == NULL) {
1069                 ret = LDB_ERR_OTHER;
1070                 goto done;
1071         }
1072
1073         query = lsqlite3_tprintf(lsql_ac,
1074                                    /* Add new entry */
1075                                    "INSERT OR ABORT INTO ldb_entry "
1076                                    "('dn', 'norm_dn') "
1077                                    "VALUES ('%q', '%q');",
1078                                 dn, ndn);
1079         if (query == NULL) {
1080                 ret = LDB_ERR_OTHER;
1081                 goto done;
1082         }
1083
1084         ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1085         if (ret != SQLITE_OK) {
1086                 if (errmsg) {
1087                         ldb_set_errstring(module->ldb, errmsg);
1088                         free(errmsg);
1089                 }
1090                 ret = LDB_ERR_OTHER;
1091                 goto done;
1092         }
1093
1094         eid = lsqlite3_get_eid_ndn(lsqlite3->sqlite, lsql_ac, ndn);
1095         if (eid == -1) {
1096                 ret = LDB_ERR_OTHER;
1097                 goto done;
1098         }
1099
1100         for (i = 0; i < msg->num_elements; i++) {
1101                 const struct ldb_message_element *el = &msg->elements[i];
1102                 const struct ldb_schema_attribute *a;
1103                 char *attr;
1104                 int j;
1105
1106                 /* Get a case-folded copy of the attribute name */
1107                 attr = ldb_attr_casefold(lsql_ac, el->name);
1108                 if (attr == NULL) {
1109                         ret = LDB_ERR_OTHER;
1110                         goto done;
1111                 }
1112
1113                 a = ldb_schema_attribute_by_name(module->ldb, el->name);
1114
1115                 /* For each value of the specified attribute name... */
1116                 for (j = 0; j < el->num_values; j++) {
1117                         struct ldb_val value;
1118                         char *insert;
1119
1120                         /* Get a canonicalised copy of the data */
1121                         a->syntax->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
1122                         if (value.data == NULL) {
1123                                 ret = LDB_ERR_OTHER;
1124                                 goto done;
1125                         }
1126
1127                         insert = lsqlite3_tprintf(lsql_ac,
1128                                         "INSERT OR ROLLBACK INTO ldb_attribute_values "
1129                                         "('eid', 'attr_name', 'norm_attr_name',"
1130                                         " 'attr_value', 'norm_attr_value') "
1131                                         "VALUES ('%lld', '%q', '%q', '%q', '%q');",
1132                                         eid, el->name, attr,
1133                                         el->values[j].data, value.data);
1134                         if (insert == NULL) {
1135                                 ret = LDB_ERR_OTHER;
1136                                 goto done;
1137                         }
1138
1139                         ret = sqlite3_exec(lsqlite3->sqlite, insert, NULL, NULL, &errmsg);
1140                         if (ret != SQLITE_OK) {
1141                                 if (errmsg) {
1142                                         ldb_set_errstring(module->ldb, errmsg);
1143                                         free(errmsg);
1144                                 }
1145                                 ret = LDB_ERR_OTHER;
1146                                 goto done;
1147                         }
1148                 }
1149         }
1150
1151         if (lsql_ac->callback) {
1152                 req->handle->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1153         }
1154         
1155 done:
1156         req->handle->state = LDB_ASYNC_DONE;
1157         return ret;
1158 }
1159
1160 /* modify a record */
1161 static int lsql_modify(struct ldb_module *module, struct ldb_request *req)
1162 {
1163         struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1164         struct lsql_context *lsql_ac;
1165         struct ldb_message *msg = req->op.mod.message;
1166         long long eid;
1167         char *errmsg;
1168         int i;
1169         int ret = LDB_SUCCESS;
1170
1171         req->handle = init_handle(lsqlite3, module, req);
1172         if (req->handle == NULL) {
1173                 return LDB_ERR_OPERATIONS_ERROR;
1174         }
1175         lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
1176         req->handle->state = LDB_ASYNC_DONE;
1177         req->handle->status = LDB_SUCCESS;
1178
1179         /* See if this is an ltdb special */
1180         if (ldb_dn_is_special(msg->dn)) {
1181                 struct ldb_dn *c;
1182
1183                 c = ldb_dn_new(lsql_ac, module->ldb, "@SUBCLASSES");
1184                 if (ldb_dn_compare(msg->dn, c) == 0) {
1185 #warning "modify subclasses into object class tree"
1186                         ret = LDB_ERR_UNWILLING_TO_PERFORM;
1187                         goto done;
1188                 }
1189
1190                 /* Others return an error */
1191                 ret = LDB_ERR_UNWILLING_TO_PERFORM;
1192                 goto done;
1193         }
1194
1195         eid = lsqlite3_get_eid(module, msg->dn);
1196         if (eid == -1) {
1197                 ret = LDB_ERR_OTHER;
1198                 goto done;
1199         }
1200
1201         for (i = 0; i < msg->num_elements; i++) {
1202                 const struct ldb_message_element *el = &msg->elements[i];
1203                 const struct ldb_schema_attribute *a;
1204                 int flags = el->flags & LDB_FLAG_MOD_MASK;
1205                 char *attr;
1206                 char *mod;
1207                 int j;
1208
1209                 /* Get a case-folded copy of the attribute name */
1210                 attr = ldb_attr_casefold(lsql_ac, el->name);
1211                 if (attr == NULL) {
1212                         ret = LDB_ERR_OTHER;
1213                         goto done;
1214                 }
1215
1216                 a = ldb_schema_attribute_by_name(module->ldb, el->name);
1217
1218                 switch (flags) {
1219
1220                 case LDB_FLAG_MOD_REPLACE:
1221                         
1222                         /* remove all attributes before adding the replacements */
1223                         mod = lsqlite3_tprintf(lsql_ac,
1224                                                 "DELETE FROM ldb_attribute_values "
1225                                                 "WHERE eid = '%lld' "
1226                                                 "AND norm_attr_name = '%q';",
1227                                                 eid, attr);
1228                         if (mod == NULL) {
1229                                 ret = LDB_ERR_OTHER;
1230                                 goto done;
1231                         }
1232
1233                         ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1234                         if (ret != SQLITE_OK) {
1235                                 if (errmsg) {
1236                                         ldb_set_errstring(module->ldb, errmsg);
1237                                         free(errmsg);
1238                                 }
1239                                 ret = LDB_ERR_OTHER;
1240                                 goto done;
1241                         }
1242
1243                         /* MISSING break is INTENTIONAL */
1244
1245                 case LDB_FLAG_MOD_ADD:
1246 #warning "We should throw an error if no value is provided!"
1247                         /* For each value of the specified attribute name... */
1248                         for (j = 0; j < el->num_values; j++) {
1249                                 struct ldb_val value;
1250
1251                                 /* Get a canonicalised copy of the data */
1252                                 a->syntax->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
1253                                 if (value.data == NULL) {
1254                                         ret = LDB_ERR_OTHER;
1255                                         goto done;
1256                                 }
1257
1258                                 mod = lsqlite3_tprintf(lsql_ac,
1259                                         "INSERT OR ROLLBACK INTO ldb_attribute_values "
1260                                         "('eid', 'attr_name', 'norm_attr_name',"
1261                                         " 'attr_value', 'norm_attr_value') "
1262                                         "VALUES ('%lld', '%q', '%q', '%q', '%q');",
1263                                         eid, el->name, attr,
1264                                         el->values[j].data, value.data);
1265
1266                                 if (mod == NULL) {
1267                                         ret = LDB_ERR_OTHER;
1268                                         goto done;
1269                                 }
1270
1271                                 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1272                                 if (ret != SQLITE_OK) {
1273                                         if (errmsg) {
1274                                                 ldb_set_errstring(module->ldb, errmsg);
1275                                                 free(errmsg);
1276                                         }
1277                                         ret = LDB_ERR_OTHER;
1278                                         goto done;
1279                                 }
1280                         }
1281
1282                         break;
1283
1284                 case LDB_FLAG_MOD_DELETE:
1285 #warning "We should throw an error if the attribute we are trying to delete does not exist!"
1286                         if (el->num_values == 0) {
1287                                 mod = lsqlite3_tprintf(lsql_ac,
1288                                                         "DELETE FROM ldb_attribute_values "
1289                                                         "WHERE eid = '%lld' "
1290                                                         "AND norm_attr_name = '%q';",
1291                                                         eid, attr);
1292                                 if (mod == NULL) {
1293                                         ret = LDB_ERR_OTHER;
1294                                         goto done;
1295                                 }
1296
1297                                 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1298                                 if (ret != SQLITE_OK) {
1299                                         if (errmsg) {
1300                                                 ldb_set_errstring(module->ldb, errmsg);
1301                                                 free(errmsg);
1302                                         }
1303                                         ret = LDB_ERR_OTHER;
1304                                         goto done;
1305                                 }
1306                         }
1307
1308                         /* For each value of the specified attribute name... */
1309                         for (j = 0; j < el->num_values; j++) {
1310                                 struct ldb_val value;
1311
1312                                 /* Get a canonicalised copy of the data */
1313                                 a->syntax->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
1314                                 if (value.data == NULL) {
1315                                         ret = LDB_ERR_OTHER;
1316                                         goto done;
1317                                 }
1318
1319                                 mod = lsqlite3_tprintf(lsql_ac,
1320                                         "DELETE FROM ldb_attribute_values "
1321                                         "WHERE eid = '%lld' "
1322                                         "AND norm_attr_name = '%q' "
1323                                         "AND norm_attr_value = '%q';",
1324                                         eid, attr, value.data);
1325
1326                                 if (mod == NULL) {
1327                                         ret = LDB_ERR_OTHER;
1328                                         goto done;
1329                                 }
1330
1331                                 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1332                                 if (ret != SQLITE_OK) {
1333                                         if (errmsg) {
1334                                                 ldb_set_errstring(module->ldb, errmsg);
1335                                                 free(errmsg);
1336                                         }
1337                                         ret = LDB_ERR_OTHER;
1338                                         goto done;
1339                                 }
1340                         }
1341
1342                         break;
1343                 }
1344         }
1345
1346         if (lsql_ac->callback) {
1347                 req->handle->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1348         }
1349         
1350 done:
1351         req->handle->state = LDB_ASYNC_DONE;
1352         return ret;
1353 }
1354
1355 /* delete a record */
1356 static int lsql_delete(struct ldb_module *module, struct ldb_request *req)
1357 {
1358         struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1359         struct lsql_context *lsql_ac;
1360         long long eid;
1361         char *errmsg;
1362         char *query;
1363         int ret = LDB_SUCCESS;
1364
1365
1366         req->handle = init_handle(lsqlite3, module, req);
1367         if (req->handle == NULL) {
1368                 return LDB_ERR_OPERATIONS_ERROR;
1369         }
1370         lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
1371         req->handle->state = LDB_ASYNC_DONE;
1372         req->handle->status = LDB_SUCCESS;
1373
1374         eid = lsqlite3_get_eid(module, req->op.del.dn);
1375         if (eid == -1) {
1376                 goto done;
1377         }
1378
1379         query = lsqlite3_tprintf(lsql_ac,
1380                                    /* Delete entry */
1381                                    "DELETE FROM ldb_entry WHERE eid = %lld; "
1382                                    /* Delete attributes */
1383                                    "DELETE FROM ldb_attribute_values WHERE eid = %lld; ",
1384                                 eid, eid);
1385         if (query == NULL) {
1386                 ret = LDB_ERR_OTHER;
1387                 goto done;
1388         }
1389
1390         ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1391         if (ret != SQLITE_OK) {
1392                 if (errmsg) {
1393                         ldb_set_errstring(module->ldb, errmsg);
1394                         free(errmsg);
1395                 }
1396                 req->handle->status = LDB_ERR_OPERATIONS_ERROR;
1397                 goto done;
1398         }
1399
1400         if (lsql_ac->callback) {
1401                 ret = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1402         }
1403         
1404 done:
1405         req->handle->state = LDB_ASYNC_DONE;
1406         return ret;
1407 }
1408
1409 /* rename a record */
1410 static int lsql_rename(struct ldb_module *module, struct ldb_request *req)
1411 {
1412         struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1413         struct lsql_context *lsql_ac;
1414         char *new_dn, *new_cdn, *old_cdn;
1415         char *errmsg;
1416         char *query;
1417         int ret = LDB_SUCCESS;
1418
1419         req->handle = init_handle(lsqlite3, module, req);
1420         if (req->handle == NULL) {
1421                 return LDB_ERR_OPERATIONS_ERROR;
1422         }
1423         lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
1424         req->handle->state = LDB_ASYNC_DONE;
1425         req->handle->status = LDB_SUCCESS;
1426
1427         /* create linearized and normalized dns */
1428         old_cdn = ldb_dn_alloc_casefold(lsql_ac, req->op.rename.olddn);
1429         new_cdn = ldb_dn_alloc_casefold(lsql_ac, req->op.rename.newdn);
1430         new_dn = ldb_dn_alloc_linearized(lsql_ac, req->op.rename.newdn);
1431         if (old_cdn == NULL || new_cdn == NULL || new_dn == NULL) {
1432                 goto done;
1433         }
1434
1435         /* build the SQL query */
1436         query = lsqlite3_tprintf(lsql_ac,
1437                                  "UPDATE ldb_entry SET dn = '%q', norm_dn = '%q' "
1438                                  "WHERE norm_dn = '%q';",
1439                                  new_dn, new_cdn, old_cdn);
1440         if (query == NULL) {
1441                 goto done;
1442         }
1443
1444         /* execute */
1445         ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1446         if (ret != SQLITE_OK) {
1447                 if (errmsg) {
1448                         ldb_set_errstring(module->ldb, errmsg);
1449                         free(errmsg);
1450                 }
1451                 ret = LDB_ERR_OPERATIONS_ERROR;
1452                 goto done;
1453         }
1454
1455         if (lsql_ac->callback) {
1456                 ret = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1457         }
1458
1459 done:
1460         req->handle->state = LDB_ASYNC_DONE;
1461         return ret;
1462 }
1463
1464 static int lsql_start_trans(struct ldb_module * module)
1465 {
1466         int ret;
1467         char *errmsg;
1468         struct lsqlite3_private *   lsqlite3 = module->private_data;
1469
1470         if (lsqlite3->trans_count == 0) {
1471                 ret = sqlite3_exec(lsqlite3->sqlite, "BEGIN IMMEDIATE;", NULL, NULL, &errmsg);
1472                 if (ret != SQLITE_OK) {
1473                         if (errmsg) {
1474                                 printf("lsqlite3_start_trans: error: %s\n", errmsg);
1475                                 free(errmsg);
1476                         }
1477                         return -1;
1478                 }
1479         };
1480
1481         lsqlite3->trans_count++;
1482
1483         return 0;
1484 }
1485
1486 static int lsql_end_trans(struct ldb_module *module)
1487 {
1488         int ret;
1489         char *errmsg;
1490         struct lsqlite3_private *lsqlite3 = module->private_data;
1491
1492         if (lsqlite3->trans_count > 0) {
1493                 lsqlite3->trans_count--;
1494         } else return -1;
1495
1496         if (lsqlite3->trans_count == 0) {
1497                 ret = sqlite3_exec(lsqlite3->sqlite, "COMMIT;", NULL, NULL, &errmsg);
1498                 if (ret != SQLITE_OK) {
1499                         if (errmsg) {
1500                                 printf("lsqlite3_end_trans: error: %s\n", errmsg);
1501                                 free(errmsg);
1502                         }
1503                         return -1;
1504                 }
1505         }
1506
1507         return 0;
1508 }
1509
1510 static int lsql_del_trans(struct ldb_module *module)
1511 {
1512         struct lsqlite3_private *lsqlite3 = module->private_data;
1513
1514         if (lsqlite3->trans_count > 0) {
1515                 lsqlite3->trans_count--;
1516         } else return -1;
1517
1518         if (lsqlite3->trans_count == 0) {
1519                 return lsqlite3_safe_rollback(lsqlite3->sqlite);
1520         }
1521
1522         return -1;
1523 }
1524
1525 static int destructor(struct lsqlite3_private *lsqlite3)
1526 {        
1527         if (lsqlite3->sqlite) {
1528                 sqlite3_close(lsqlite3->sqlite);
1529         }
1530         return 0;
1531 }
1532
1533 static int lsql_request(struct ldb_module *module, struct ldb_request *req)
1534 {
1535         return LDB_ERR_OPERATIONS_ERROR;
1536 }
1537
1538 static int lsql_wait(struct ldb_handle *handle, enum ldb_wait_type type)
1539 {
1540         return handle->status;
1541 }
1542
1543 /*
1544  * Table of operations for the sqlite3 backend
1545  */
1546 static const struct ldb_module_ops lsqlite3_ops = {
1547         .name              = "sqlite",
1548         .search            = lsql_search,
1549         .add               = lsql_add,
1550         .modify            = lsql_modify,
1551         .del               = lsql_delete,
1552         .rename            = lsql_rename,
1553         .request           = lsql_request,
1554         .start_transaction = lsql_start_trans,
1555         .end_transaction   = lsql_end_trans,
1556         .del_transaction   = lsql_del_trans,
1557         .wait              = lsql_wait,
1558 };
1559
1560 /*
1561  * Static functions
1562  */
1563
1564 static int initialize(struct lsqlite3_private *lsqlite3,
1565                       struct ldb_context *ldb, const char *url, int flags)
1566 {
1567         TALLOC_CTX *local_ctx;
1568         long long queryInt;
1569         int rollback = 0;
1570         char *errmsg;
1571         char *schema;
1572         int ret;
1573
1574         /* create a local ctx */
1575         local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_rename local context");
1576         if (local_ctx == NULL) {
1577                 return -1;
1578         }
1579
1580         schema = lsqlite3_tprintf(local_ctx,
1581                 
1582                 
1583                 "CREATE TABLE ldb_info AS "
1584                 "  SELECT 'LDB' AS database_type,"
1585                 "         '1.0' AS version;"
1586                 
1587                 /*
1588                  * The entry table holds the information about an entry. 
1589                  * This table is used to obtain the EID of the entry and to 
1590                  * support scope=one and scope=base.  The parent and child
1591                  * table is included in the entry table since all the other
1592                  * attributes are dependent on EID.
1593                  */
1594                 "CREATE TABLE ldb_entry "
1595                 "("
1596                 "  eid     INTEGER PRIMARY KEY AUTOINCREMENT,"
1597                 "  dn      TEXT UNIQUE NOT NULL,"
1598                 "  norm_dn TEXT UNIQUE NOT NULL"
1599                 ");"
1600                 
1601
1602                 "CREATE TABLE ldb_object_classes"
1603                 "("
1604                 "  class_name            TEXT PRIMARY KEY,"
1605                 "  parent_class_name     TEXT,"
1606                 "  tree_key              TEXT UNIQUE,"
1607                 "  max_child_num         INTEGER DEFAULT 0"
1608                 ");"
1609                 
1610                 /*
1611                  * We keep a full listing of attribute/value pairs here
1612                  */
1613                 "CREATE TABLE ldb_attribute_values"
1614                 "("
1615                 "  eid             INTEGER REFERENCES ldb_entry,"
1616                 "  attr_name       TEXT,"
1617                 "  norm_attr_name  TEXT,"
1618                 "  attr_value      TEXT,"
1619                 "  norm_attr_value TEXT "
1620                 ");"
1621                 
1622                
1623                 /*
1624                  * Indexes
1625                  */
1626                 "CREATE INDEX ldb_attribute_values_eid_idx "
1627                 "  ON ldb_attribute_values (eid);"
1628                 
1629                 "CREATE INDEX ldb_attribute_values_name_value_idx "
1630                 "  ON ldb_attribute_values (attr_name, norm_attr_value);"
1631                 
1632                 
1633
1634                 /*
1635                  * Triggers
1636                  */
1637  
1638                 "CREATE TRIGGER ldb_object_classes_insert_tr"
1639                 "  AFTER INSERT"
1640                 "  ON ldb_object_classes"
1641                 "  FOR EACH ROW"
1642                 "    BEGIN"
1643                 "      UPDATE ldb_object_classes"
1644                 "        SET tree_key = COALESCE(tree_key, "
1645                 "              ("
1646                 "                SELECT tree_key || "
1647                 "                       (SELECT base160(max_child_num + 1)"
1648                 "                                FROM ldb_object_classes"
1649                 "                                WHERE class_name = "
1650                 "                                      new.parent_class_name)"
1651                 "                  FROM ldb_object_classes "
1652                 "                  WHERE class_name = new.parent_class_name "
1653                 "              ));"
1654                 "      UPDATE ldb_object_classes "
1655                 "        SET max_child_num = max_child_num + 1"
1656                 "        WHERE class_name = new.parent_class_name;"
1657                 "    END;"
1658
1659                 /*
1660                  * Table initialization
1661                  */
1662
1663                 "INSERT INTO ldb_object_classes "
1664                 "    (class_name, tree_key) "
1665                 "  VALUES "
1666                 "    ('TOP', '0001');");
1667         
1668         /* Skip protocol indicator of url  */
1669         if (strncmp(url, "sqlite3://", 10) != 0) {
1670                 return SQLITE_MISUSE;
1671         }
1672         
1673         /* Update pointer to just after the protocol indicator */
1674         url += 10;
1675         
1676         /* Try to open the (possibly empty/non-existent) database */
1677         if ((ret = sqlite3_open(url, &lsqlite3->sqlite)) != SQLITE_OK) {
1678                 return ret;
1679         }
1680         
1681         /* In case this is a new database, enable auto_vacuum */
1682         ret = sqlite3_exec(lsqlite3->sqlite, "PRAGMA auto_vacuum = 1;", NULL, NULL, &errmsg);
1683         if (ret != SQLITE_OK) {
1684                 if (errmsg) {
1685                         printf("lsqlite3 initializaion error: %s\n", errmsg);
1686                         free(errmsg);
1687                 }
1688                 goto failed;
1689         }
1690         
1691         if (flags & LDB_FLG_NOSYNC) {
1692                 /* DANGEROUS */
1693                 ret = sqlite3_exec(lsqlite3->sqlite, "PRAGMA synchronous = OFF;", NULL, NULL, &errmsg);
1694                 if (ret != SQLITE_OK) {
1695                         if (errmsg) {
1696                                 printf("lsqlite3 initializaion error: %s\n", errmsg);
1697                                 free(errmsg);
1698                         }
1699                         goto failed;
1700                 }
1701         }
1702         
1703         /* */
1704         
1705         /* Establish a busy timeout of 30 seconds */
1706         if ((ret = sqlite3_busy_timeout(lsqlite3->sqlite,
1707                                         30000)) != SQLITE_OK) {
1708                 return ret;
1709         }
1710
1711         /* Create a function, callable from sql, to increment a tree_key */
1712         if ((ret =
1713              sqlite3_create_function(lsqlite3->sqlite,/* handle */
1714                                      "base160_next",  /* function name */
1715                                      1,               /* number of args */
1716                                      SQLITE_ANY,      /* preferred text type */
1717                                      NULL,            /* user data */
1718                                      base160next_sql, /* called func */
1719                                      NULL,            /* step func */
1720                                      NULL             /* final func */
1721                      )) != SQLITE_OK) {
1722                 return ret;
1723         }
1724
1725         /* Create a function, callable from sql, to convert int to base160 */
1726         if ((ret =
1727              sqlite3_create_function(lsqlite3->sqlite,/* handle */
1728                                      "base160",       /* function name */
1729                                      1,               /* number of args */
1730                                      SQLITE_ANY,      /* preferred text type */
1731                                      NULL,            /* user data */
1732                                      base160_sql,     /* called func */
1733                                      NULL,            /* step func */
1734                                      NULL             /* final func */
1735                      )) != SQLITE_OK) {
1736                 return ret;
1737         }
1738
1739         /* Create a function, callable from sql, to perform various comparisons */
1740         if ((ret =
1741              sqlite3_create_function(lsqlite3->sqlite, /* handle */
1742                                      "ldap_compare",   /* function name */
1743                                      4,                /* number of args */
1744                                      SQLITE_ANY,       /* preferred text type */
1745                                      ldb  ,            /* user data */
1746                                      lsqlite3_compare, /* called func */
1747                                      NULL,             /* step func */
1748                                      NULL              /* final func */
1749                      )) != SQLITE_OK) {
1750                 return ret;
1751         }
1752
1753         /* Begin a transaction */
1754         ret = sqlite3_exec(lsqlite3->sqlite, "BEGIN EXCLUSIVE;", NULL, NULL, &errmsg);
1755         if (ret != SQLITE_OK) {
1756                 if (errmsg) {
1757                         printf("lsqlite3: initialization error: %s\n", errmsg);
1758                         free(errmsg);
1759                 }
1760                 goto failed;
1761         }
1762         rollback = 1;
1763  
1764         /* Determine if this is a new database.  No tables means it is. */
1765         if (query_int(lsqlite3,
1766                       &queryInt,
1767                       "SELECT COUNT(*)\n"
1768                       "  FROM sqlite_master\n"
1769                       "  WHERE type = 'table';") != 0) {
1770                 goto failed;
1771         }
1772         
1773         if (queryInt == 0) {
1774                 /*
1775                  * Create the database schema
1776                  */
1777                 ret = sqlite3_exec(lsqlite3->sqlite, schema, NULL, NULL, &errmsg);
1778                 if (ret != SQLITE_OK) {
1779                         if (errmsg) {
1780                                 printf("lsqlite3 initializaion error: %s\n", errmsg);
1781                                 free(errmsg);
1782                         }
1783                         goto failed;
1784                 }
1785         } else {
1786                 /*
1787                  * Ensure that the database we opened is one of ours
1788                  */
1789                 if (query_int(lsqlite3,
1790                               &queryInt,
1791                               "SELECT "
1792                               "  (SELECT COUNT(*) = 2"
1793                               "     FROM sqlite_master "
1794                               "     WHERE type = 'table' "
1795                               "       AND name IN "
1796                               "         ("
1797                               "           'ldb_entry', "
1798                               "           'ldb_object_classes' "
1799                               "         ) "
1800                               "  ) "
1801                               "  AND "
1802                               "  (SELECT 1 "
1803                               "     FROM ldb_info "
1804                               "     WHERE database_type = 'LDB' "
1805                               "       AND version = '1.0'"
1806                               "  );") != 0 ||
1807                     queryInt != 1) {
1808                         
1809                         /* It's not one that we created.  See ya! */
1810                         goto failed;
1811                 }
1812         }
1813         
1814         /* Commit the transaction */
1815         ret = sqlite3_exec(lsqlite3->sqlite, "COMMIT;", NULL, NULL, &errmsg);
1816         if (ret != SQLITE_OK) {
1817                 if (errmsg) {
1818                         printf("lsqlite3: iniialization error: %s\n", errmsg);
1819                         free(errmsg);
1820                 }
1821                 goto failed;
1822         }
1823  
1824         return SQLITE_OK;
1825
1826 failed:
1827         if (rollback) lsqlite3_safe_rollback(lsqlite3->sqlite); 
1828         sqlite3_close(lsqlite3->sqlite);
1829         return -1;
1830 }
1831
1832 /*
1833  * connect to the database
1834  */
1835 static int lsqlite3_connect(struct ldb_context *ldb,
1836                             const char *url, 
1837                             unsigned int flags, 
1838                             const char *options[],
1839                             struct ldb_module **module)
1840 {
1841         int                         i;
1842         int                         ret;
1843         struct lsqlite3_private *   lsqlite3 = NULL;
1844         
1845         lsqlite3 = talloc(ldb, struct lsqlite3_private);
1846         if (!lsqlite3) {
1847                 goto failed;
1848         }
1849         
1850         lsqlite3->sqlite = NULL;
1851         lsqlite3->options = NULL;
1852         lsqlite3->trans_count = 0;
1853         
1854         ret = initialize(lsqlite3, ldb, url, flags);
1855         if (ret != SQLITE_OK) {
1856                 goto failed;
1857         }
1858         
1859         talloc_set_destructor(lsqlite3, destructor);
1860         
1861
1862
1863         *module = talloc(ldb, struct ldb_module);
1864         if (!module) {
1865                 ldb_oom(ldb);
1866                 goto failed;
1867         }
1868         talloc_set_name_const(*module, "ldb_sqlite3 backend");
1869         (*module)->ldb = ldb;
1870         (*module)->prev = (*module)->next = NULL;
1871         (*module)->private_data = lsqlite3;
1872         (*module)->ops = &lsqlite3_ops;
1873
1874         if (options) {
1875                 /*
1876                  * take a copy of the options array, so we don't have to rely
1877                  * on the caller keeping it around (it might be dynamic)
1878                  */
1879                 for (i=0;options[i];i++) ;
1880                 
1881                 lsqlite3->options = talloc_array(lsqlite3, char *, i+1);
1882                 if (!lsqlite3->options) {
1883                         goto failed;
1884                 }
1885                 
1886                 for (i=0;options[i];i++) {
1887                         
1888                         lsqlite3->options[i+1] = NULL;
1889                         lsqlite3->options[i] =
1890                                 talloc_strdup(lsqlite3->options, options[i]);
1891                         if (!lsqlite3->options[i]) {
1892                                 goto failed;
1893                         }
1894                 }
1895         }
1896         
1897         return 0;
1898         
1899 failed:
1900         if (lsqlite3->sqlite != NULL) {
1901                 (void) sqlite3_close(lsqlite3->sqlite);
1902         }
1903         talloc_free(lsqlite3);
1904         return -1;
1905 }
1906
1907 int ldb_sqlite3_init(void)
1908 {
1909         return ldb_register_backend("sqlite3", lsqlite3_connect);
1910 }