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