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