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