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