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