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