4c00998fa9357da42dd2a93d351236c20dc6810f
[samba.git] / source / 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(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(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(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(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(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(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         msg->elements = talloc_realloc(msg,
719                                        msg->elements,
720                                        struct ldb_message_element,
721                                        msg->num_elements + 1);
722         if (msg->elements == NULL) return SQLITE_ABORT;
723
724         msg->elements[msg->num_elements].flags = 0;
725         msg->elements[msg->num_elements].name = talloc_strdup(msg->elements, cols[2]);
726         if (msg->elements[msg->num_elements].name == NULL) return SQLITE_ABORT;
727
728         msg->elements[msg->num_elements].num_values = 1;
729         msg->elements[msg->num_elements].values = talloc_array(msg->elements,
730                                                                 struct ldb_val, 1);
731         if (msg->elements[msg->num_elements].values == NULL) return SQLITE_ABORT;
732
733         msg->elements[msg->num_elements].values[0].length = strlen(cols[3]);
734         msg->elements[msg->num_elements].values[0].data = talloc_strdup(msg->elements, cols[3]);
735         if (msg->elements[msg->num_elements].values[0].data == NULL) return SQLITE_ABORT;
736
737         msg->num_elements++;
738
739         return SQLITE_OK;
740 }
741
742
743 /*
744  * lsqlite3_get_eid()
745  * lsqlite3_get_eid_ndn()
746  *
747  * These functions are used for the very common case of retrieving an EID value
748  * given a (normalized) DN.
749  */
750
751 static long long lsqlite3_get_eid_ndn(sqlite3 *sqlite, void *mem_ctx, const char *norm_dn)
752 {
753         char *errmsg;
754         char *query;
755         long long eid = -1;
756         long long ret;
757
758         /* get object eid */
759         query = lsqlite3_tprintf(mem_ctx, "SELECT eid "
760                                           "FROM ldb_entry "
761                                           "WHERE norm_dn = '%q';", norm_dn);
762         if (query == NULL) return -1;
763
764         ret = sqlite3_exec(sqlite, query, lsqlite3_eid_callback, &eid, &errmsg);
765         if (ret != SQLITE_OK) {
766                 if (errmsg) {
767                         printf("lsqlite3_get_eid: Fatal Error: %s\n", errmsg);
768                         free(errmsg);
769                 }
770                 return -1;
771         }
772
773         return eid;
774 }
775
776 static long long lsqlite3_get_eid(struct ldb_module *module, const struct ldb_dn *dn)
777 {
778         TALLOC_CTX *local_ctx;
779         struct lsqlite3_private *lsqlite3 = module->private_data;
780         long long eid = -1;
781         char *cdn;
782
783         /* ignore ltdb specials */
784         if (ldb_dn_is_special(dn)) {
785                 return -1;
786         }
787
788         /* create a local ctx */
789         local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_get_eid local context");
790         if (local_ctx == NULL) {
791                 return -1;
792         }
793
794         cdn = ldb_dn_linearize(local_ctx, ldb_dn_casefold(module->ldb, dn));
795         if (!cdn) goto done;
796
797         eid = lsqlite3_get_eid_ndn(lsqlite3->sqlite, local_ctx, cdn);
798
799 done:
800         talloc_free(local_ctx);
801         return eid;
802 }
803
804 /*
805  * Interface functions referenced by lsqlite3_ops
806  */
807
808 /* search for matching records, by tree */
809 static int lsqlite3_search_bytree(struct ldb_module * module, const struct ldb_dn* basedn,
810                                   enum ldb_scope scope, struct ldb_parse_tree * tree,
811                                   const char * const * attrs, struct ldb_result ** res)
812 {
813         TALLOC_CTX *local_ctx;
814         struct lsqlite3_private *lsqlite3 = module->private_data;
815         struct lsqlite3_msgs msgs;
816         char *norm_basedn;
817         char *sqlfilter;
818         char *errmsg;
819         char *query = NULL;
820         int ret, i;
821
822         /* create a local ctx */
823         local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_search_bytree local context");
824         if (local_ctx == NULL) {
825                 return -1;
826         }
827
828         if (basedn) {
829                 norm_basedn = ldb_dn_linearize(local_ctx, ldb_dn_casefold(module->ldb, basedn));
830                 if (norm_basedn == NULL) {
831                         ret = LDB_ERR_INVALID_DN_SYNTAX;
832                         goto failed;
833                 }
834         } else norm_basedn = talloc_strdup(local_ctx, "");
835
836         if (*norm_basedn == '\0' &&
837                 (scope == LDB_SCOPE_BASE || scope == LDB_SCOPE_ONELEVEL)) {
838                         ret = LDB_ERR_UNWILLING_TO_PERFORM;
839                         goto failed;
840                 }
841
842         /* Convert filter into a series of SQL conditions (constraints) */
843         sqlfilter = parsetree_to_sql(module, local_ctx, tree);
844         
845         switch(scope) {
846         case LDB_SCOPE_DEFAULT:
847         case LDB_SCOPE_SUBTREE:
848                 if (*norm_basedn != '\0') {
849                         query = lsqlite3_tprintf(local_ctx,
850                                 "SELECT entry.eid,\n"
851                                 "       entry.dn,\n"
852                                 "       av.attr_name,\n"
853                                 "       av.attr_value\n"
854                                 "  FROM ldb_entry AS entry\n"
855
856                                 "  LEFT OUTER JOIN ldb_attribute_values AS av\n"
857                                 "    ON av.eid = entry.eid\n"
858
859                                 "  WHERE entry.eid IN\n"
860                                 "    (SELECT DISTINCT ldb_entry.eid\n"
861                                 "       FROM ldb_entry\n"
862                                 "       WHERE (ldb_entry.norm_dn GLOB('*,%q')\n"
863                                 "       OR ldb_entry.norm_dn = '%q')\n"
864                                 "       AND ldb_entry.eid IN\n"
865                                 "         (%s)\n"
866                                 "    )\n"
867
868                                 "  ORDER BY entry.eid ASC;",
869                                 norm_basedn,
870                                 norm_basedn,
871                                 sqlfilter);
872                 } else {
873                         query = lsqlite3_tprintf(local_ctx,
874                                 "SELECT entry.eid,\n"
875                                 "       entry.dn,\n"
876                                 "       av.attr_name,\n"
877                                 "       av.attr_value\n"
878                                 "  FROM ldb_entry AS entry\n"
879
880                                 "  LEFT OUTER JOIN ldb_attribute_values AS av\n"
881                                 "    ON av.eid = entry.eid\n"
882
883                                 "  WHERE entry.eid IN\n"
884                                 "    (SELECT DISTINCT ldb_entry.eid\n"
885                                 "       FROM ldb_entry\n"
886                                 "       WHERE ldb_entry.eid IN\n"
887                                 "         (%s)\n"
888                                 "    )\n"
889
890                                 "  ORDER BY entry.eid ASC;",
891                                 sqlfilter);
892                 }
893
894                 break;
895                 
896         case LDB_SCOPE_BASE:
897                 query = lsqlite3_tprintf(local_ctx,
898                         "SELECT entry.eid,\n"
899                         "       entry.dn,\n"
900                         "       av.attr_name,\n"
901                         "       av.attr_value\n"
902                         "  FROM ldb_entry AS entry\n"
903
904                         "  LEFT OUTER JOIN ldb_attribute_values AS av\n"
905                         "    ON av.eid = entry.eid\n"
906
907                         "  WHERE entry.eid IN\n"
908                         "    (SELECT DISTINCT ldb_entry.eid\n"
909                         "       FROM ldb_entry\n"
910                         "       WHERE ldb_entry.norm_dn = '%q'\n"
911                         "         AND ldb_entry.eid IN\n"
912                         "           (%s)\n"
913                         "    )\n"
914
915                         "  ORDER BY entry.eid ASC;",
916                         norm_basedn,
917                         sqlfilter);
918                 break;
919                 
920         case LDB_SCOPE_ONELEVEL:
921                 query = lsqlite3_tprintf(local_ctx,
922                         "SELECT entry.eid,\n"
923                         "       entry.dn,\n"
924                         "       av.attr_name,\n"
925                         "       av.attr_value\n"
926                         "  FROM ldb_entry AS entry\n"
927
928                         "  LEFT OUTER JOIN ldb_attribute_values AS av\n"
929                         "    ON av.eid = entry.eid\n"
930
931                         "  WHERE entry.eid IN\n"
932                         "    (SELECT DISTINCT ldb_entry.eid\n"
933                         "       FROM ldb_entry\n"
934                         "       WHERE norm_dn GLOB('*,%q')\n"
935                         "         AND NOT norm_dn GLOB('*,*,%q')\n"
936                         "         AND ldb_entry.eid IN\n(%s)\n"
937                         "    )\n"
938
939                         "  ORDER BY entry.eid ASC;",
940                         norm_basedn,
941                         norm_basedn,
942                         sqlfilter);
943                 break;
944         }
945
946         if (query == NULL) {
947                 ret = LDB_ERR_OTHER;
948                 goto failed;
949         }
950
951         /* * /
952         printf ("%s\n", query);
953         / * */
954
955         msgs.msgs = NULL;
956         msgs.count = 0;
957         msgs.current_eid = 0;
958         msgs.mem_ctx = local_ctx;
959         msgs.attrs = attrs;
960
961         ret = sqlite3_exec(lsqlite3->sqlite, query, lsqlite3_search_callback, &msgs, &errmsg);
962         if (ret != SQLITE_OK) {
963                 if (errmsg) {
964                         ldb_set_errstring(module, talloc_strdup(module, errmsg));
965                         free(errmsg);
966                 }
967                 ret = LDB_ERR_OTHER;
968                 goto failed;
969         }
970
971         for (i = 0; i < msgs.count; i++) {
972                 msgs.msgs[i] = ldb_msg_canonicalize(module->ldb, msgs.msgs[i]);
973                 if (msgs.msgs[i] ==  NULL) {
974                         goto failed;
975                 }
976         }
977
978         *res = talloc(module, struct ldb_result);
979         if (! *res) {
980                 goto failed;
981         }
982
983         (*res)->msgs = talloc_steal(*res, msgs.msgs);
984         (*res)->count = msgs.count;
985         (*res)->controls = NULL;
986
987         talloc_free(local_ctx);
988         return LDB_SUCCESS;
989
990 /* If error, return error code; otherwise return number of results */
991 failed:
992         talloc_free(local_ctx);
993         return LDB_ERR_OTHER;
994 }
995
996
997 /* add a record */
998 static int lsqlite3_add(struct ldb_module *module, const struct ldb_message *msg)
999 {
1000         TALLOC_CTX *local_ctx;
1001         struct lsqlite3_private *lsqlite3 = module->private_data;
1002         long long eid;
1003         char *dn, *ndn;
1004         char *errmsg;
1005         char *query;
1006         int ret;
1007         int i;
1008         
1009         /* create a local ctx */
1010         local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_add local context");
1011         if (local_ctx == NULL) {
1012                 return LDB_ERR_OTHER;
1013         }
1014
1015         /* See if this is an ltdb special */
1016         if (ldb_dn_is_special(msg->dn)) {
1017                 struct ldb_dn *c;
1018
1019                 c = ldb_dn_explode(local_ctx, "@SUBCLASSES");
1020                 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
1021 #warning "insert subclasses into object class tree"
1022                         ret = LDB_ERR_UNWILLING_TO_PERFORM;
1023                         goto failed;
1024                 }
1025
1026 /*
1027                 c = ldb_dn_explode(local_ctx, "@INDEXLIST");
1028                 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
1029 #warning "should we handle indexes somehow ?"
1030                         goto failed;
1031                 }
1032 */
1033                 /* Others are implicitly ignored */
1034                 return LDB_SUCCESS;
1035         }
1036
1037         /* create linearized and normalized dns */
1038         dn = ldb_dn_linearize(local_ctx, msg->dn);
1039         ndn = ldb_dn_linearize(local_ctx, ldb_dn_casefold(module->ldb, msg->dn));
1040         if (dn == NULL || ndn == NULL) {
1041                 ret = LDB_ERR_OTHER;
1042                 goto failed;
1043         }
1044
1045         query = lsqlite3_tprintf(local_ctx,
1046                                    /* Add new entry */
1047                                    "INSERT OR ABORT INTO ldb_entry "
1048                                    "('dn', 'norm_dn') "
1049                                    "VALUES ('%q', '%q');",
1050                                 dn, ndn);
1051         if (query == NULL) {
1052                 ret = LDB_ERR_OTHER;
1053                 goto failed;
1054         }
1055
1056         ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1057         if (ret != SQLITE_OK) {
1058                 if (errmsg) {
1059                         ldb_set_errstring(module, talloc_strdup(module, errmsg));
1060                         free(errmsg);
1061                 }
1062                 ret = LDB_ERR_OTHER;
1063                 goto failed;
1064         }
1065
1066         eid = lsqlite3_get_eid_ndn(lsqlite3->sqlite, local_ctx, ndn);
1067         if (eid == -1) {
1068                 ret = LDB_ERR_OTHER;
1069                 goto failed;
1070         }
1071
1072         for (i = 0; i < msg->num_elements; i++) {
1073                 const struct ldb_message_element *el = &msg->elements[i];
1074                 const struct ldb_attrib_handler *h;
1075                 char *attr;
1076                 int j;
1077
1078                 /* Get a case-folded copy of the attribute name */
1079                 attr = ldb_casefold(local_ctx, el->name);
1080                 if (attr == NULL) {
1081                         ret = LDB_ERR_OTHER;
1082                         goto failed;
1083                 }
1084
1085                 h = ldb_attrib_handler(module->ldb, el->name);
1086
1087                 /* For each value of the specified attribute name... */
1088                 for (j = 0; j < el->num_values; j++) {
1089                         struct ldb_val value;
1090                         char *insert;
1091
1092                         /* Get a canonicalised copy of the data */
1093                         h->canonicalise_fn(module->ldb, local_ctx, &(el->values[j]), &value);
1094                         if (value.data == NULL) {
1095                                 ret = LDB_ERR_OTHER;
1096                                 goto failed;
1097                         }
1098
1099                         insert = lsqlite3_tprintf(local_ctx,
1100                                         "INSERT OR ROLLBACK INTO ldb_attribute_values "
1101                                         "('eid', 'attr_name', 'norm_attr_name',"
1102                                         " 'attr_value', 'norm_attr_value') "
1103                                         "VALUES ('%lld', '%q', '%q', '%q', '%q');",
1104                                         eid, el->name, attr,
1105                                         el->values[j].data, value.data);
1106                         if (insert == NULL) {
1107                                 ret = LDB_ERR_OTHER;
1108                                 goto failed;
1109                         }
1110
1111                         ret = sqlite3_exec(lsqlite3->sqlite, insert, NULL, NULL, &errmsg);
1112                         if (ret != SQLITE_OK) {
1113                                 if (errmsg) {
1114                                         ldb_set_errstring(module, talloc_strdup(module, errmsg));
1115                                         free(errmsg);
1116                                 }
1117                                 ret = LDB_ERR_OTHER;
1118                                 goto failed;
1119                         }
1120                 }
1121         }
1122
1123         talloc_free(local_ctx);
1124         return LDB_SUCCESS;
1125
1126 failed:
1127         talloc_free(local_ctx);
1128         return ret;
1129 }
1130
1131
1132 /* modify a record */
1133 static int lsqlite3_modify(struct ldb_module *module, const struct ldb_message *msg)
1134 {
1135         TALLOC_CTX *local_ctx;
1136         struct lsqlite3_private *lsqlite3 = module->private_data;
1137         long long eid;
1138         char *errmsg;
1139         int ret;
1140         int i;
1141         
1142         /* create a local ctx */
1143         local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_modify local context");
1144         if (local_ctx == NULL) {
1145                 return LDB_ERR_OTHER;
1146         }
1147
1148         /* See if this is an ltdb special */
1149         if (ldb_dn_is_special(msg->dn)) {
1150                 struct ldb_dn *c;
1151
1152                 c = ldb_dn_explode(local_ctx, "@SUBCLASSES");
1153                 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
1154 #warning "modify subclasses into object class tree"
1155                         ret = LDB_ERR_UNWILLING_TO_PERFORM;
1156                         goto failed;
1157                 }
1158
1159                 /* Others are implicitly ignored */
1160                 return LDB_SUCCESS;
1161         }
1162
1163         eid = lsqlite3_get_eid(module, msg->dn);
1164         if (eid == -1) {
1165                 ret = LDB_ERR_OTHER;
1166                 goto failed;
1167         }
1168
1169         for (i = 0; i < msg->num_elements; i++) {
1170                 const struct ldb_message_element *el = &msg->elements[i];
1171                 const struct ldb_attrib_handler *h;
1172                 int flags = el->flags & LDB_FLAG_MOD_MASK;
1173                 char *attr;
1174                 char *mod;
1175                 int j;
1176
1177                 /* Get a case-folded copy of the attribute name */
1178                 attr = ldb_casefold(local_ctx, el->name);
1179                 if (attr == NULL) {
1180                         ret = LDB_ERR_OTHER;
1181                         goto failed;
1182                 }
1183
1184                 h = ldb_attrib_handler(module->ldb, el->name);
1185
1186                 switch (flags) {
1187
1188                 case LDB_FLAG_MOD_REPLACE:
1189                         
1190                         /* remove all attributes before adding the replacements */
1191                         mod = lsqlite3_tprintf(local_ctx,
1192                                                 "DELETE FROM ldb_attribute_values "
1193                                                 "WHERE eid = '%lld' "
1194                                                 "AND norm_attr_name = '%q';",
1195                                                 eid, attr);
1196                         if (mod == NULL) {
1197                                 ret = LDB_ERR_OTHER;
1198                                 goto failed;
1199                         }
1200
1201                         ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1202                         if (ret != SQLITE_OK) {
1203                                 if (errmsg) {
1204                                         ldb_set_errstring(module, talloc_strdup(module, errmsg));
1205                                         free(errmsg);
1206                                 }
1207                                 ret = LDB_ERR_OTHER;
1208                                 goto failed;
1209                         }
1210
1211                         /* MISSING break is INTENTIONAL */
1212
1213                 case LDB_FLAG_MOD_ADD:
1214 #warning "We should throw an error if no value is provided!"
1215                         /* For each value of the specified attribute name... */
1216                         for (j = 0; j < el->num_values; j++) {
1217                                 struct ldb_val value;
1218
1219                                 /* Get a canonicalised copy of the data */
1220                                 h->canonicalise_fn(module->ldb, local_ctx, &(el->values[j]), &value);
1221                                 if (value.data == NULL) {
1222                                         ret = LDB_ERR_OTHER;
1223                                         goto failed;
1224                                 }
1225
1226                                 mod = lsqlite3_tprintf(local_ctx,
1227                                         "INSERT OR ROLLBACK INTO ldb_attribute_values "
1228                                         "('eid', 'attr_name', 'norm_attr_name',"
1229                                         " 'attr_value', 'norm_attr_value') "
1230                                         "VALUES ('%lld', '%q', '%q', '%q', '%q');",
1231                                         eid, el->name, attr,
1232                                         el->values[j].data, value.data);
1233
1234                                 if (mod == NULL) {
1235                                         ret = LDB_ERR_OTHER;
1236                                         goto failed;
1237                                 }
1238
1239                                 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1240                                 if (ret != SQLITE_OK) {
1241                                         if (errmsg) {
1242                                                 ldb_set_errstring(module, talloc_strdup(module, errmsg));
1243                                                 free(errmsg);
1244                                         }
1245                                         ret = LDB_ERR_OTHER;
1246                                         goto failed;
1247                                 }
1248                         }
1249
1250                         break;
1251
1252                 case LDB_FLAG_MOD_DELETE:
1253 #warning "We should throw an error if the attribute we are trying to delete does not exist!"
1254                         if (el->num_values == 0) {
1255                                 mod = lsqlite3_tprintf(local_ctx,
1256                                                         "DELETE FROM ldb_attribute_values "
1257                                                         "WHERE eid = '%lld' "
1258                                                         "AND norm_attr_name = '%q';",
1259                                                         eid, attr);
1260                                 if (mod == NULL) {
1261                                         ret = LDB_ERR_OTHER;
1262                                         goto failed;
1263                                 }
1264
1265                                 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1266                                 if (ret != SQLITE_OK) {
1267                                         if (errmsg) {
1268                                                 ldb_set_errstring(module, talloc_strdup(module, errmsg));
1269                                                 free(errmsg);
1270                                         }
1271                                         ret = LDB_ERR_OTHER;
1272                                         goto failed;
1273                                 }
1274                         }
1275
1276                         /* For each value of the specified attribute name... */
1277                         for (j = 0; j < el->num_values; j++) {
1278                                 struct ldb_val value;
1279
1280                                 /* Get a canonicalised copy of the data */
1281                                 h->canonicalise_fn(module->ldb, local_ctx, &(el->values[j]), &value);
1282                                 if (value.data == NULL) {
1283                                         ret = LDB_ERR_OTHER;
1284                                         goto failed;
1285                                 }
1286
1287                                 mod = lsqlite3_tprintf(local_ctx,
1288                                         "DELETE FROM ldb_attribute_values "
1289                                         "WHERE eid = '%lld' "
1290                                         "AND norm_attr_name = '%q' "
1291                                         "AND norm_attr_value = '%q';",
1292                                         eid, attr, value.data);
1293
1294                                 if (mod == NULL) {
1295                                         ret = LDB_ERR_OTHER;
1296                                         goto failed;
1297                                 }
1298
1299                                 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1300                                 if (ret != SQLITE_OK) {
1301                                         if (errmsg) {
1302                                                 ldb_set_errstring(module, talloc_strdup(module, errmsg));
1303                                                 free(errmsg);
1304                                         }
1305                                         ret = LDB_ERR_OTHER;
1306                                         goto failed;
1307                                 }
1308                         }
1309
1310                         break;
1311                 }
1312         }
1313
1314         talloc_free(local_ctx);
1315         return LDB_SUCCESS;
1316
1317 failed:
1318         talloc_free(local_ctx);
1319         return ret;
1320 }
1321
1322 /* delete a record */
1323 static int lsqlite3_delete(struct ldb_module *module, const struct ldb_dn *dn)
1324 {
1325         TALLOC_CTX *local_ctx;
1326         struct lsqlite3_private *lsqlite3 = module->private_data;
1327         long long eid;
1328         char *errmsg;
1329         char *query;
1330         int ret;
1331
1332         /* ignore ltdb specials */
1333         if (ldb_dn_is_special(dn)) {
1334                 return LDB_SUCCESS;
1335         }
1336
1337         /* create a local ctx */
1338         local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_delete local context");
1339         if (local_ctx == NULL) {
1340                 return LDB_ERR_OTHER;
1341         }
1342
1343         eid = lsqlite3_get_eid(module, dn);
1344         if (eid == -1) {
1345                 ret = LDB_ERR_OTHER;
1346                 goto failed;
1347         }
1348
1349         query = lsqlite3_tprintf(local_ctx,
1350                                    /* Delete entry */
1351                                    "DELETE FROM ldb_entry WHERE eid = %lld; "
1352                                    /* Delete attributes */
1353                                    "DELETE FROM ldb_attribute_values WHERE eid = %lld; ",
1354                                 eid, eid);
1355         if (query == NULL) {
1356                 ret = LDB_ERR_OTHER;
1357                 goto failed;
1358         }
1359
1360         ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1361         if (ret != SQLITE_OK) {
1362                 if (errmsg) {
1363                         ldb_set_errstring(module, talloc_strdup(module, errmsg));
1364                         free(errmsg);
1365                 }
1366                 ret = LDB_ERR_OTHER;
1367                 goto failed;
1368         }
1369
1370         talloc_free(local_ctx);
1371         return LDB_SUCCESS;
1372
1373 failed:
1374         talloc_free(local_ctx);
1375         return ret;
1376 }
1377
1378 /* rename a record */
1379 static int lsqlite3_rename(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
1380 {
1381         TALLOC_CTX *local_ctx;
1382         struct lsqlite3_private *lsqlite3 = module->private_data;
1383         char *new_dn, *new_cdn, *old_cdn;
1384         char *errmsg;
1385         char *query;
1386         int ret;
1387
1388         /* ignore ltdb specials */
1389         if (ldb_dn_is_special(olddn) || ldb_dn_is_special(newdn)) {
1390                 return LDB_SUCCESS;
1391         }
1392
1393         /* create a local ctx */
1394         local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_rename local context");
1395         if (local_ctx == NULL) {
1396                 return LDB_ERR_OTHER;
1397         }
1398
1399         /* create linearized and normalized dns */
1400         old_cdn = ldb_dn_linearize(local_ctx, ldb_dn_casefold(module->ldb, olddn));
1401         new_cdn = ldb_dn_linearize(local_ctx, ldb_dn_casefold(module->ldb, newdn));
1402         new_dn = ldb_dn_linearize(local_ctx, newdn);
1403         if (old_cdn == NULL || new_cdn == NULL || new_dn == NULL) {
1404                 ret = LDB_ERR_OTHER;
1405                 goto failed;
1406         }
1407
1408         /* build the SQL query */
1409         query = lsqlite3_tprintf(local_ctx,
1410                                  "UPDATE ldb_entry SET dn = '%q', norm_dn = '%q' "
1411                                  "WHERE norm_dn = '%q';",
1412                                  new_dn, new_cdn, old_cdn);
1413         if (query == NULL) {
1414                 ret = LDB_ERR_OTHER;
1415                 goto failed;
1416         }
1417
1418         /* execute */
1419         ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1420         if (ret != SQLITE_OK) {
1421                 if (errmsg) {
1422                         ldb_set_errstring(module, talloc_strdup(module, errmsg));
1423                         free(errmsg);
1424                 }
1425                 ret = LDB_ERR_OTHER;
1426                 goto failed;
1427         }
1428
1429         /* clean up and exit */
1430         talloc_free(local_ctx);
1431         return LDB_SUCCESS;
1432
1433 failed:
1434         talloc_free(local_ctx);
1435         return ret;
1436 }
1437
1438 static int lsqlite3_start_trans(struct ldb_module * module)
1439 {
1440         int ret;
1441         char *errmsg;
1442         struct lsqlite3_private *   lsqlite3 = module->private_data;
1443
1444         if (lsqlite3->trans_count == 0) {
1445                 ret = sqlite3_exec(lsqlite3->sqlite, "BEGIN IMMEDIATE;", NULL, NULL, &errmsg);
1446                 if (ret != SQLITE_OK) {
1447                         if (errmsg) {
1448                                 printf("lsqlite3_start_trans: error: %s\n", errmsg);
1449                                 free(errmsg);
1450                         }
1451                         return -1;
1452                 }
1453         };
1454
1455         lsqlite3->trans_count++;
1456
1457         return 0;
1458 }
1459
1460 static int lsqlite3_end_trans(struct ldb_module *module)
1461 {
1462         int ret;
1463         char *errmsg;
1464         struct lsqlite3_private *lsqlite3 = module->private_data;
1465
1466         if (lsqlite3->trans_count > 0) {
1467                 lsqlite3->trans_count--;
1468         } else return -1;
1469
1470         if (lsqlite3->trans_count == 0) {
1471                 ret = sqlite3_exec(lsqlite3->sqlite, "COMMIT;", NULL, NULL, &errmsg);
1472                 if (ret != SQLITE_OK) {
1473                         if (errmsg) {
1474                                 printf("lsqlite3_end_trans: error: %s\n", errmsg);
1475                                 free(errmsg);
1476                         }
1477                         return -1;
1478                 }
1479         }
1480
1481         return 0;
1482 }
1483
1484 static int lsqlite3_del_trans(struct ldb_module *module)
1485 {
1486         struct lsqlite3_private *lsqlite3 = module->private_data;
1487
1488         if (lsqlite3->trans_count > 0) {
1489                 lsqlite3->trans_count--;
1490         } else return -1;
1491
1492         if (lsqlite3->trans_count == 0) {
1493                 return lsqlite3_safe_rollback(lsqlite3->sqlite);
1494         }
1495
1496         return -1;
1497 }
1498
1499 /*
1500  * Static functions
1501  */
1502
1503 static int initialize(struct lsqlite3_private *lsqlite3,
1504                       struct ldb_context *ldb, const char *url, int flags)
1505 {
1506         TALLOC_CTX *local_ctx;
1507         long long queryInt;
1508         int rollback = 0;
1509         char *errmsg;
1510         char *schema;
1511         int ret;
1512
1513         /* create a local ctx */
1514         local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_rename local context");
1515         if (local_ctx == NULL) {
1516                 return -1;
1517         }
1518
1519         schema = lsqlite3_tprintf(local_ctx,
1520                 
1521                 
1522                 "CREATE TABLE ldb_info AS "
1523                 "  SELECT 'LDB' AS database_type,"
1524                 "         '1.0' AS version;"
1525                 
1526                 /*
1527                  * The entry table holds the information about an entry. 
1528                  * This table is used to obtain the EID of the entry and to 
1529                  * support scope=one and scope=base.  The parent and child
1530                  * table is included in the entry table since all the other
1531                  * attributes are dependent on EID.
1532                  */
1533                 "CREATE TABLE ldb_entry "
1534                 "("
1535                 "  eid     INTEGER PRIMARY KEY AUTOINCREMENT,"
1536                 "  dn      TEXT UNIQUE NOT NULL,"
1537                 "  norm_dn TEXT UNIQUE NOT NULL"
1538                 ");"
1539                 
1540
1541                 "CREATE TABLE ldb_object_classes"
1542                 "("
1543                 "  class_name            TEXT PRIMARY KEY,"
1544                 "  parent_class_name     TEXT,"
1545                 "  tree_key              TEXT UNIQUE,"
1546                 "  max_child_num         INTEGER DEFAULT 0"
1547                 ");"
1548                 
1549                 /*
1550                  * We keep a full listing of attribute/value pairs here
1551                  */
1552                 "CREATE TABLE ldb_attribute_values"
1553                 "("
1554                 "  eid             INTEGER REFERENCES ldb_entry,"
1555                 "  attr_name       TEXT,"
1556                 "  norm_attr_name  TEXT,"
1557                 "  attr_value      TEXT,"
1558                 "  norm_attr_value TEXT "
1559                 ");"
1560                 
1561                
1562                 /*
1563                  * Indexes
1564                  */
1565                 "CREATE INDEX ldb_attribute_values_eid_idx "
1566                 "  ON ldb_attribute_values (eid);"
1567                 
1568                 "CREATE INDEX ldb_attribute_values_name_value_idx "
1569                 "  ON ldb_attribute_values (attr_name, norm_attr_value);"
1570                 
1571                 
1572
1573                 /*
1574                  * Triggers
1575                  */
1576  
1577                 "CREATE TRIGGER ldb_object_classes_insert_tr"
1578                 "  AFTER INSERT"
1579                 "  ON ldb_object_classes"
1580                 "  FOR EACH ROW"
1581                 "    BEGIN"
1582                 "      UPDATE ldb_object_classes"
1583                 "        SET tree_key = COALESCE(tree_key, "
1584                 "              ("
1585                 "                SELECT tree_key || "
1586                 "                       (SELECT base160(max_child_num + 1)"
1587                 "                                FROM ldb_object_classes"
1588                 "                                WHERE class_name = "
1589                 "                                      new.parent_class_name)"
1590                 "                  FROM ldb_object_classes "
1591                 "                  WHERE class_name = new.parent_class_name "
1592                 "              ));"
1593                 "      UPDATE ldb_object_classes "
1594                 "        SET max_child_num = max_child_num + 1"
1595                 "        WHERE class_name = new.parent_class_name;"
1596                 "    END;"
1597
1598                 /*
1599                  * Table initialization
1600                  */
1601
1602                 "INSERT INTO ldb_object_classes "
1603                 "    (class_name, tree_key) "
1604                 "  VALUES "
1605                 "    ('TOP', '0001');");
1606         
1607         /* Skip protocol indicator of url  */
1608         if (strncmp(url, "sqlite://", 9) != 0) {
1609                 return SQLITE_MISUSE;
1610         }
1611         
1612         /* Update pointer to just after the protocol indicator */
1613         url += 9;
1614         
1615         /* Try to open the (possibly empty/non-existent) database */
1616         if ((ret = sqlite3_open(url, &lsqlite3->sqlite)) != SQLITE_OK) {
1617                 return ret;
1618         }
1619         
1620         /* In case this is a new database, enable auto_vacuum */
1621         ret = sqlite3_exec(lsqlite3->sqlite, "PRAGMA auto_vacuum = 1;", NULL, NULL, &errmsg);
1622         if (ret != SQLITE_OK) {
1623                 if (errmsg) {
1624                         printf("lsqlite3 initializaion error: %s\n", errmsg);
1625                         free(errmsg);
1626                 }
1627                 goto failed;
1628         }
1629         
1630         if (flags & LDB_FLG_NOSYNC) {
1631                 /* DANGEROUS */
1632                 ret = sqlite3_exec(lsqlite3->sqlite, "PRAGMA synchronous = OFF;", NULL, NULL, &errmsg);
1633                 if (ret != SQLITE_OK) {
1634                         if (errmsg) {
1635                                 printf("lsqlite3 initializaion error: %s\n", errmsg);
1636                                 free(errmsg);
1637                         }
1638                         goto failed;
1639                 }
1640         }
1641         
1642         /* */
1643         
1644         /* Establish a busy timeout of 30 seconds */
1645         if ((ret = sqlite3_busy_timeout(lsqlite3->sqlite,
1646                                         30000)) != SQLITE_OK) {
1647                 return ret;
1648         }
1649
1650         /* Create a function, callable from sql, to increment a tree_key */
1651         if ((ret =
1652              sqlite3_create_function(lsqlite3->sqlite,/* handle */
1653                                      "base160_next",  /* function name */
1654                                      1,               /* number of args */
1655                                      SQLITE_ANY,      /* preferred text type */
1656                                      NULL,            /* user data */
1657                                      base160next_sql, /* called func */
1658                                      NULL,            /* step func */
1659                                      NULL             /* final func */
1660                      )) != SQLITE_OK) {
1661                 return ret;
1662         }
1663
1664         /* Create a function, callable from sql, to convert int to base160 */
1665         if ((ret =
1666              sqlite3_create_function(lsqlite3->sqlite,/* handle */
1667                                      "base160",       /* function name */
1668                                      1,               /* number of args */
1669                                      SQLITE_ANY,      /* preferred text type */
1670                                      NULL,            /* user data */
1671                                      base160_sql,     /* called func */
1672                                      NULL,            /* step func */
1673                                      NULL             /* final func */
1674                      )) != SQLITE_OK) {
1675                 return ret;
1676         }
1677
1678         /* Create a function, callable from sql, to perform various comparisons */
1679         if ((ret =
1680              sqlite3_create_function(lsqlite3->sqlite, /* handle */
1681                                      "ldap_compare",   /* function name */
1682                                      4,                /* number of args */
1683                                      SQLITE_ANY,       /* preferred text type */
1684                                      ldb  ,            /* user data */
1685                                      lsqlite3_compare, /* called func */
1686                                      NULL,             /* step func */
1687                                      NULL              /* final func */
1688                      )) != SQLITE_OK) {
1689                 return ret;
1690         }
1691
1692         /* Begin a transaction */
1693         ret = sqlite3_exec(lsqlite3->sqlite, "BEGIN EXCLUSIVE;", NULL, NULL, &errmsg);
1694         if (ret != SQLITE_OK) {
1695                 if (errmsg) {
1696                         printf("lsqlite3: initialization error: %s\n", errmsg);
1697                         free(errmsg);
1698                 }
1699                 goto failed;
1700         }
1701         rollback = 1;
1702  
1703         /* Determine if this is a new database.  No tables means it is. */
1704         if (query_int(lsqlite3,
1705                       &queryInt,
1706                       "SELECT COUNT(*)\n"
1707                       "  FROM sqlite_master\n"
1708                       "  WHERE type = 'table';") != 0) {
1709                 goto failed;
1710         }
1711         
1712         if (queryInt == 0) {
1713                 /*
1714                  * Create the database schema
1715                  */
1716                 ret = sqlite3_exec(lsqlite3->sqlite, schema, NULL, NULL, &errmsg);
1717                 if (ret != SQLITE_OK) {
1718                         if (errmsg) {
1719                                 printf("lsqlite3 initializaion error: %s\n", errmsg);
1720                                 free(errmsg);
1721                         }
1722                         goto failed;
1723                 }
1724         } else {
1725                 /*
1726                  * Ensure that the database we opened is one of ours
1727                  */
1728                 if (query_int(lsqlite3,
1729                               &queryInt,
1730                               "SELECT "
1731                               "  (SELECT COUNT(*) = 2"
1732                               "     FROM sqlite_master "
1733                               "     WHERE type = 'table' "
1734                               "       AND name IN "
1735                               "         ("
1736                               "           'ldb_entry', "
1737                               "           'ldb_object_classes' "
1738                               "         ) "
1739                               "  ) "
1740                               "  AND "
1741                               "  (SELECT 1 "
1742                               "     FROM ldb_info "
1743                               "     WHERE database_type = 'LDB' "
1744                               "       AND version = '1.0'"
1745                               "  );") != 0 ||
1746                     queryInt != 1) {
1747                         
1748                         /* It's not one that we created.  See ya! */
1749                         goto failed;
1750                 }
1751         }
1752         
1753         /* Commit the transaction */
1754         ret = sqlite3_exec(lsqlite3->sqlite, "COMMIT;", NULL, NULL, &errmsg);
1755         if (ret != SQLITE_OK) {
1756                 if (errmsg) {
1757                         printf("lsqlite3: iniialization error: %s\n", errmsg);
1758                         free(errmsg);
1759                 }
1760                 goto failed;
1761         }
1762  
1763         return SQLITE_OK;
1764
1765 failed:
1766         if (rollback) lsqlite3_safe_rollback(lsqlite3->sqlite); 
1767         sqlite3_close(lsqlite3->sqlite);
1768         return -1;
1769 }
1770
1771 static int
1772 destructor(void *p)
1773 {
1774         struct lsqlite3_private *lsqlite3 = p;
1775         
1776         if (lsqlite3->sqlite) {
1777                 sqlite3_close(lsqlite3->sqlite);
1778         }
1779         return 0;
1780 }
1781
1782
1783 static int lsqlite3_request(struct ldb_module *module, struct ldb_request *req)
1784 {
1785         /* check for oustanding critical controls and return an error if found */
1786         if (check_critical_controls(req->controls)) {
1787                 return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
1788         }
1789         
1790         switch (req->operation) {
1791
1792         case LDB_REQ_SEARCH:
1793                 return lsqlite3_search_bytree(module,
1794                                           req->op.search.base,
1795                                           req->op.search.scope, 
1796                                           req->op.search.tree, 
1797                                           req->op.search.attrs, 
1798                                           &req->op.search.res);
1799
1800         case LDB_REQ_ADD:
1801                 return lsqlite3_add(module, req->op.add.message);
1802
1803         case LDB_REQ_MODIFY:
1804                 return lsqlite3_modify(module, req->op.mod.message);
1805
1806         case LDB_REQ_DELETE:
1807                 return lsqlite3_delete(module, req->op.del.dn);
1808
1809         case LDB_REQ_RENAME:
1810                 return lsqlite3_rename(module,
1811                                         req->op.rename.olddn,
1812                                         req->op.rename.newdn);
1813
1814         default:
1815                 return LDB_ERR_OPERATIONS_ERROR;
1816
1817         }
1818 }
1819
1820 static int lsqlite3_init_2(struct ldb_module *module)
1821 {
1822         return LDB_SUCCESS;
1823 }
1824
1825 /*
1826  * Table of operations for the sqlite3 backend
1827  */
1828 static const struct ldb_module_ops lsqlite3_ops = {
1829         .name              = "sqlite",
1830         .request           = lsqlite3_request,
1831         .start_transaction = lsqlite3_start_trans,
1832         .end_transaction   = lsqlite3_end_trans,
1833         .del_transaction   = lsqlite3_del_trans,
1834         .second_stage_init = lsqlite3_init_2
1835 };
1836
1837 /*
1838  * connect to the database
1839  */
1840 int lsqlite3_connect(struct ldb_context *ldb,
1841                      const char *url, 
1842                      unsigned int flags, 
1843                      const char *options[])
1844 {
1845         int                         i;
1846         int                         ret;
1847         struct lsqlite3_private *   lsqlite3 = NULL;
1848         
1849         lsqlite3 = talloc(ldb, struct lsqlite3_private);
1850         if (!lsqlite3) {
1851                 goto failed;
1852         }
1853         
1854         lsqlite3->sqlite = NULL;
1855         lsqlite3->options = NULL;
1856         lsqlite3->trans_count = 0;
1857         
1858         ret = initialize(lsqlite3, ldb, url, flags);
1859         if (ret != SQLITE_OK) {
1860                 goto failed;
1861         }
1862         
1863         talloc_set_destructor(lsqlite3, destructor);
1864         
1865         ldb->modules = talloc(ldb, struct ldb_module);
1866         if (!ldb->modules) {
1867                 goto failed;
1868         }
1869         ldb->modules->ldb = ldb;
1870         ldb->modules->prev = ldb->modules->next = NULL;
1871         ldb->modules->private_data = lsqlite3;
1872         ldb->modules->ops = &lsqlite3_ops;
1873         
1874         if (options) {
1875                 /*
1876                  * take a copy of the options array, so we don't have to rely
1877                  * on the caller keeping it around (it might be dynamic)
1878                  */
1879                 for (i=0;options[i];i++) ;
1880                 
1881                 lsqlite3->options = talloc_array(lsqlite3, char *, i+1);
1882                 if (!lsqlite3->options) {
1883                         goto failed;
1884                 }
1885                 
1886                 for (i=0;options[i];i++) {
1887                         
1888                         lsqlite3->options[i+1] = NULL;
1889                         lsqlite3->options[i] =
1890                                 talloc_strdup(lsqlite3->options, options[i]);
1891                         if (!lsqlite3->options[i]) {
1892                                 goto failed;
1893                         }
1894                 }
1895         }
1896         
1897         return 0;
1898         
1899 failed:
1900         if (lsqlite3->sqlite != NULL) {
1901                 (void) sqlite3_close(lsqlite3->sqlite);
1902         }
1903         talloc_free(lsqlite3);
1904         return -1;
1905 }
1906