8acbac4cc3aad9bcbd95d1880265258c3885bda7
[abartlet/samba.git/.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                 return SQLITE_NOMEM;
489         }
490
491         /*
492          * Prepare and execute the SQL statement.  Loop allows retrying on
493          * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
494          * requiring retrying the operation.
495          */
496         for (bLoop = TRUE; bLoop; ) {
497
498                 /* Compile the SQL statement into sqlite virtual machine */
499                 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
500                                            p,
501                                            -1,
502                                            &pStmt,
503                                            NULL)) == SQLITE_SCHEMA) {
504                         if (stmtGetEID != NULL) {
505                                 sqlite3_finalize(stmtGetEID);
506                                 stmtGetEID = NULL;
507                         }
508                         continue;
509                 } else if (ret != SQLITE_OK) {
510                         break;
511                 }
512
513                 /* One row expected */
514                 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
515                         if (stmtGetEID != NULL) {
516                                 sqlite3_finalize(stmtGetEID);
517                                 stmtGetEID = NULL;
518                         }
519                         (void) sqlite3_finalize(pStmt);
520                         continue;
521                 } else if (ret != SQLITE_ROW) {
522                         (void) sqlite3_finalize(pStmt);
523                         break;
524                 }
525
526                 /* Get the value to be returned */
527                 *pRet = sqlite3_column_int64(pStmt, 0);
528
529                 /* Free the virtual machine */
530                 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
531                         if (stmtGetEID != NULL) {
532                                 sqlite3_finalize(stmtGetEID);
533                                 stmtGetEID = NULL;
534                         }
535                         continue;
536                 } else if (ret != SQLITE_OK) {
537                         (void) sqlite3_finalize(pStmt);
538                         break;
539                 }
540
541                 /*
542                  * Normal condition is only one time through loop.  Loop is
543                  * rerun in error conditions, via "continue", above.
544                  */
545                 bLoop = FALSE;
546         }
547
548         /* All done with variable argument list */
549         va_end(args);
550
551
552         /* Free the memory we allocated for our query string */
553         sqlite3_free(p);
554
555         return ret;
556 }
557
558 /*
559  * This is a bad hack to support ldap style comparisons whithin sqlite.
560  * val is the attribute in the row currently under test
561  * func is the desired test "<=" ">=" "~" ":"
562  * cmp is the value to compare against (eg: "test")
563  * attr is the attribute name the value of which we want to test
564  */
565
566 static void lsqlite3_compare(sqlite3_context *ctx, int argc,
567                                         sqlite3_value **argv)
568 {
569         struct ldb_context *ldb = (struct ldb_context *)sqlite3_user_data(ctx);
570         const char *val = (const char *)sqlite3_value_text(argv[0]);
571         const char *func = (const char *)sqlite3_value_text(argv[1]);
572         const char *cmp = (const char *)sqlite3_value_text(argv[2]);
573         const char *attr = (const char *)sqlite3_value_text(argv[3]);
574         const struct ldb_schema_attribute *a;
575         struct ldb_val valX;
576         struct ldb_val valY;
577         int ret;
578
579         switch (func[0]) {
580         /* greater */
581         case '>': /* >= */
582                 a = ldb_schema_attribute_by_name(ldb, attr);
583                 valX.data = (uint8_t *)cmp;
584                 valX.length = strlen(cmp);
585                 valY.data = (uint8_t *)val;
586                 valY.length = strlen(val);
587                 ret = a->syntax->comparison_fn(ldb, ldb, &valY, &valX);
588                 if (ret >= 0)
589                         sqlite3_result_int(ctx, 1);
590                 else
591                         sqlite3_result_int(ctx, 0);
592                 return;
593
594         /* lesser */
595         case '<': /* <= */
596                 a = ldb_schema_attribute_by_name(ldb, attr);
597                 valX.data = (uint8_t *)cmp;
598                 valX.length = strlen(cmp);
599                 valY.data = (uint8_t *)val;
600                 valY.length = strlen(val);
601                 ret = a->syntax->comparison_fn(ldb, ldb, &valY, &valX);
602                 if (ret <= 0)
603                         sqlite3_result_int(ctx, 1);
604                 else
605                         sqlite3_result_int(ctx, 0);
606                 return;
607
608         /* approx */
609         case '~':
610                 /* TODO */
611                 sqlite3_result_int(ctx, 0);
612                 return;
613
614         /* bitops */
615         case ':':
616                 /* TODO */
617                 sqlite3_result_int(ctx, 0);
618                 return;
619
620         default:
621                 break;
622         }
623
624         sqlite3_result_error(ctx, "Value must start with a special operation char (<>~:)!", -1);
625         return;
626 }
627
628
629 /* rename a record */
630 static int lsqlite3_safe_rollback(sqlite3 *sqlite)
631 {
632         char *errmsg;
633         int ret;
634
635         /* execute */
636         ret = sqlite3_exec(sqlite, "ROLLBACK;", NULL, NULL, &errmsg);
637         if (ret != SQLITE_OK) {
638                 if (errmsg) {
639                         printf("lsqlite3_safe_rollback: Error: %s\n", errmsg);
640                         free(errmsg);
641                 }
642                 return -1;
643         }
644
645         return 0;
646 }
647
648 /* return an eid as result */
649 static int lsqlite3_eid_callback(void *result, int col_num, char **cols, char **names)
650 {
651         long long *eid = (long long *)result;
652
653         if (col_num != 1) return SQLITE_ABORT;
654         if (strcasecmp(names[0], "eid") != 0) return SQLITE_ABORT;
655
656         *eid = atoll(cols[0]);
657         return SQLITE_OK;
658 }
659
660 /*
661  * add a single set of ldap message values to a ldb_message
662  */
663 static int lsqlite3_search_callback(void *result, int col_num, char **cols, char **names)
664 {
665         struct ldb_context *ldb;
666         struct lsql_context *ac;
667         struct ldb_message *msg;
668         long long eid;
669         int i, ret;
670
671         ac = talloc_get_type(result, struct lsql_context);
672         ldb = ldb_module_get_ctx(ac->module);
673
674         /* eid, dn, attr_name, attr_value */
675         if (col_num != 4) return SQLITE_ABORT;
676
677         eid = atoll(cols[0]);
678
679         if (ac->ares) {
680                 msg = ac->ares->message;
681         }
682
683         if (eid != ac->current_eid) { /* here begin a new entry */
684
685                 /* call the async callback for the last entry
686                  * except the first time */
687                 if (ac->current_eid != 0) {
688                         msg = ldb_msg_canonicalize(ldb, msg);
689                         if (!msg) return SQLITE_ABORT;
690
691                         ret = ldb_module_send_entry(ac->req, msg, NULL);
692                         if (ret != LDB_SUCCESS) {
693                                 ac->callback_failed = true;
694                                 return SQLITE_ABORT;
695                         }
696                 }
697
698                 /* start over */
699                 ac->ares = talloc_zero(ac, struct ldb_reply);
700                 if (!ac->ares) return SQLITE_ABORT;
701
702                 msg = ldb_msg_new(ac->ares);
703                 if (!msg) return SQLITE_ABORT;
704
705                 ac->ares->type = LDB_REPLY_ENTRY;
706                 ac->current_eid = eid;
707         }
708
709         if (msg->dn == NULL) {
710                 msg->dn = ldb_dn_new(msg, ldb, cols[1]);
711                 if (msg->dn == NULL)
712                         return SQLITE_ABORT;
713         }
714
715         if (ac->attrs) {
716                 int found = 0;
717                 for (i = 0; ac->attrs[i]; i++) {
718                         if (strcasecmp(cols[2], ac->attrs[i]) == 0) {
719                                 found = 1;
720                                 break;
721                         }
722                 }
723                 if (!found) goto done;
724         }
725
726         if (ldb_msg_add_string(msg, cols[2], cols[3]) != 0) {
727                 return SQLITE_ABORT;
728         }
729
730 done:
731         ac->ares->message = msg;
732         return SQLITE_OK;
733 }
734
735
736 /*
737  * lsqlite3_get_eid()
738  * lsqlite3_get_eid_ndn()
739  *
740  * These functions are used for the very common case of retrieving an EID value
741  * given a (normalized) DN.
742  */
743
744 static long long lsqlite3_get_eid_ndn(sqlite3 *sqlite, void *mem_ctx, const char *norm_dn)
745 {
746         char *errmsg;
747         char *query;
748         long long eid = -1;
749         long long ret;
750
751         /* get object eid */
752         query = lsqlite3_tprintf(mem_ctx, "SELECT eid "
753                                           "FROM ldb_entry "
754                                           "WHERE norm_dn = '%q';", norm_dn);
755         if (query == NULL) return -1;
756
757         ret = sqlite3_exec(sqlite, query, lsqlite3_eid_callback, &eid, &errmsg);
758         if (ret != SQLITE_OK) {
759                 if (errmsg) {
760                         printf("lsqlite3_get_eid: Fatal Error: %s\n", errmsg);
761                         free(errmsg);
762                 }
763                 return -1;
764         }
765
766         return eid;
767 }
768
769 static long long lsqlite3_get_eid(struct lsqlite3_private *lsqlite3,
770                                   struct ldb_dn *dn)
771 {
772         TALLOC_CTX *local_ctx;
773         long long eid = -1;
774         char *cdn;
775
776         /* ignore ltdb specials */
777         if (ldb_dn_is_special(dn)) {
778                 return -1;
779         }
780
781         /* create a local ctx */
782         local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_get_eid local context");
783         if (local_ctx == NULL) {
784                 return -1;
785         }
786
787         cdn = ldb_dn_alloc_casefold(local_ctx, dn);
788         if (!cdn) goto done;
789
790         eid = lsqlite3_get_eid_ndn(lsqlite3->sqlite, local_ctx, cdn);
791
792 done:
793         talloc_free(local_ctx);
794         return eid;
795 }
796
797 /*
798  * Interface functions referenced by lsqlite3_ops
799  */
800
801 /* search for matching records, by tree */
802 int lsql_search(struct lsql_context *ctx)
803 {
804         struct ldb_module *module = ctx->module;
805         struct ldb_request *req = ctx->req;
806         struct lsqlite3_private *lsqlite3;
807         struct ldb_context *ldb;
808         char *norm_basedn;
809         char *sqlfilter;
810         char *errmsg;
811         char *query = NULL;
812         int ret;
813
814         ldb = ldb_module_get_ctx(module);
815         lsqlite3 = talloc_get_type(ldb_module_get_private(module),
816                                    struct lsqlite3_private);
817
818         if ((( ! ldb_dn_is_valid(req->op.search.base)) ||
819              ldb_dn_is_null(req->op.search.base)) &&
820             (req->op.search.scope == LDB_SCOPE_BASE ||
821              req->op.search.scope == LDB_SCOPE_ONELEVEL)) {
822                 return LDB_ERR_OPERATIONS_ERROR;
823         }
824
825         if (req->op.search.base) {
826                 norm_basedn = ldb_dn_alloc_casefold(ctx, req->op.search.base);
827                 if (norm_basedn == NULL) {
828                         return LDB_ERR_OPERATIONS_ERROR;
829                 }
830         } else norm_basedn = talloc_strdup(ctx, "");
831
832         /* Convert filter into a series of SQL conditions (constraints) */
833         sqlfilter = parsetree_to_sql(module, ctx, req->op.search.tree);
834
835         switch(req->op.search.scope) {
836         case LDB_SCOPE_DEFAULT:
837         case LDB_SCOPE_SUBTREE:
838                 if (*norm_basedn != '\0') {
839                         query = lsqlite3_tprintf(ctx,
840                                 "SELECT entry.eid,\n"
841                                 "       entry.dn,\n"
842                                 "       av.attr_name,\n"
843                                 "       av.attr_value\n"
844                                 "  FROM ldb_entry AS entry\n"
845
846                                 "  LEFT OUTER JOIN ldb_attribute_values AS av\n"
847                                 "    ON av.eid = entry.eid\n"
848
849                                 "  WHERE entry.eid IN\n"
850                                 "    (SELECT DISTINCT ldb_entry.eid\n"
851                                 "       FROM ldb_entry\n"
852                                 "       WHERE (ldb_entry.norm_dn GLOB('*,%q')\n"
853                                 "       OR ldb_entry.norm_dn = '%q')\n"
854                                 "       AND ldb_entry.eid IN\n"
855                                 "         (%s)\n"
856                                 "    )\n"
857
858                                 "  ORDER BY entry.eid ASC;",
859                                 norm_basedn,
860                                 norm_basedn,
861                                 sqlfilter);
862                 } else {
863                         query = lsqlite3_tprintf(ctx,
864                                 "SELECT entry.eid,\n"
865                                 "       entry.dn,\n"
866                                 "       av.attr_name,\n"
867                                 "       av.attr_value\n"
868                                 "  FROM ldb_entry AS entry\n"
869
870                                 "  LEFT OUTER JOIN ldb_attribute_values AS av\n"
871                                 "    ON av.eid = entry.eid\n"
872
873                                 "  WHERE entry.eid IN\n"
874                                 "    (SELECT DISTINCT ldb_entry.eid\n"
875                                 "       FROM ldb_entry\n"
876                                 "       WHERE ldb_entry.eid IN\n"
877                                 "         (%s)\n"
878                                 "    )\n"
879
880                                 "  ORDER BY entry.eid ASC;",
881                                 sqlfilter);
882                 }
883
884                 break;
885
886         case LDB_SCOPE_BASE:
887                 query = lsqlite3_tprintf(ctx,
888                         "SELECT entry.eid,\n"
889                         "       entry.dn,\n"
890                         "       av.attr_name,\n"
891                         "       av.attr_value\n"
892                         "  FROM ldb_entry AS entry\n"
893
894                         "  LEFT OUTER JOIN ldb_attribute_values AS av\n"
895                         "    ON av.eid = entry.eid\n"
896
897                         "  WHERE entry.eid IN\n"
898                         "    (SELECT DISTINCT ldb_entry.eid\n"
899                         "       FROM ldb_entry\n"
900                         "       WHERE ldb_entry.norm_dn = '%q'\n"
901                         "         AND ldb_entry.eid IN\n"
902                         "           (%s)\n"
903                         "    )\n"
904
905                         "  ORDER BY entry.eid ASC;",
906                         norm_basedn,
907                         sqlfilter);
908                 break;
909
910         case LDB_SCOPE_ONELEVEL:
911                 query = lsqlite3_tprintf(ctx,
912                         "SELECT entry.eid,\n"
913                         "       entry.dn,\n"
914                         "       av.attr_name,\n"
915                         "       av.attr_value\n"
916                         "  FROM ldb_entry AS entry\n"
917
918                         "  LEFT OUTER JOIN ldb_attribute_values AS av\n"
919                         "    ON av.eid = entry.eid\n"
920
921                         "  WHERE entry.eid IN\n"
922                         "    (SELECT DISTINCT ldb_entry.eid\n"
923                         "       FROM ldb_entry\n"
924                         "       WHERE norm_dn GLOB('*,%q')\n"
925                         "         AND NOT norm_dn GLOB('*,*,%q')\n"
926                         "         AND ldb_entry.eid IN\n(%s)\n"
927                         "    )\n"
928
929                         "  ORDER BY entry.eid ASC;",
930                         norm_basedn,
931                         norm_basedn,
932                         sqlfilter);
933                 break;
934         }
935
936         if (query == NULL) {
937                 return LDB_ERR_OPERATIONS_ERROR;
938         }
939
940         /* * /
941         printf ("%s\n", query);
942         / * */
943
944         ctx->current_eid = 0;
945         ctx->attrs = req->op.search.attrs;
946         ctx->ares = NULL;
947
948         ldb_request_set_state(req, LDB_ASYNC_PENDING);
949
950         ret = sqlite3_exec(lsqlite3->sqlite, query, lsqlite3_search_callback, ctx, &errmsg);
951         if (ret != SQLITE_OK) {
952                 if (errmsg) {
953                         ldb_set_errstring(ldb, errmsg);
954                         free(errmsg);
955                 }
956                 return LDB_ERR_OPERATIONS_ERROR;
957         }
958
959         /* complete the last message if any */
960         if (ctx->ares) {
961                 ctx->ares->message = ldb_msg_canonicalize(ldb, ctx->ares->message);
962                 if (ctx->ares->message == NULL) {
963                         return LDB_ERR_OPERATIONS_ERROR;
964                 }
965
966                 ret = ldb_module_send_entry(req, ctx->ares->message, NULL);
967                 if (ret != LDB_SUCCESS) {
968                         return ret;
969                 }
970         }
971
972
973         return LDB_SUCCESS;
974 }
975
976 /* add a record */
977 static int lsql_add(struct lsql_context *ctx)
978 {
979         struct ldb_module *module = ctx->module;
980         struct ldb_request *req = ctx->req;
981         struct lsqlite3_private *lsqlite3;
982         struct ldb_context *ldb;
983         struct ldb_message *msg = req->op.add.message;
984         long long eid;
985         char *dn, *ndn;
986         char *errmsg;
987         char *query;
988         int i;
989         int ret;
990
991         ldb = ldb_module_get_ctx(module);
992         lsqlite3 = talloc_get_type(ldb_module_get_private(module),
993                                    struct lsqlite3_private);
994
995         /* See if this is an ltdb special */
996         if (ldb_dn_is_special(msg->dn)) {
997 /*
998                 struct ldb_dn *c;
999                 c = ldb_dn_new(local_ctx, ldb, "@INDEXLIST");
1000                 if (ldb_dn_compare(ldb, msg->dn, c) == 0) {
1001 #warning "should we handle indexes somehow ?"
1002                         ret = LDB_ERR_UNWILLING_TO_PERFORM;
1003                         goto done;
1004                 }
1005 */
1006                 /* Others return an error */
1007                 return LDB_ERR_UNWILLING_TO_PERFORM;
1008         }
1009
1010         /* create linearized and normalized dns */
1011         dn = ldb_dn_alloc_linearized(ctx, msg->dn);
1012         ndn = ldb_dn_alloc_casefold(ctx, msg->dn);
1013         if (dn == NULL || ndn == NULL) {
1014                 return LDB_ERR_OPERATIONS_ERROR;
1015         }
1016
1017         query = lsqlite3_tprintf(ctx,
1018                                    /* Add new entry */
1019                                    "INSERT OR ABORT INTO ldb_entry "
1020                                    "('dn', 'norm_dn') "
1021                                    "VALUES ('%q', '%q');",
1022                                 dn, ndn);
1023         if (query == NULL) {
1024                 return LDB_ERR_OPERATIONS_ERROR;
1025         }
1026
1027         ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1028         if (ret != SQLITE_OK) {
1029                 if (errmsg) {
1030                         ldb_set_errstring(ldb, errmsg);
1031                         free(errmsg);
1032                 }
1033                 return LDB_ERR_OPERATIONS_ERROR;
1034         }
1035
1036         eid = lsqlite3_get_eid_ndn(lsqlite3->sqlite, ctx, ndn);
1037         if (eid == -1) {
1038                 return LDB_ERR_OPERATIONS_ERROR;
1039         }
1040
1041         for (i = 0; i < msg->num_elements; i++) {
1042                 const struct ldb_message_element *el = &msg->elements[i];
1043                 const struct ldb_schema_attribute *a;
1044                 char *attr;
1045                 int j;
1046
1047                 /* Get a case-folded copy of the attribute name */
1048                 attr = ldb_attr_casefold(ctx, el->name);
1049                 if (attr == NULL) {
1050                         return LDB_ERR_OPERATIONS_ERROR;
1051                 }
1052
1053                 a = ldb_schema_attribute_by_name(ldb, el->name);
1054
1055                 /* For each value of the specified attribute name... */
1056                 for (j = 0; j < el->num_values; j++) {
1057                         struct ldb_val value;
1058                         char *insert;
1059
1060                         /* Get a canonicalised copy of the data */
1061                         a->syntax->canonicalise_fn(ldb, ctx, &(el->values[j]), &value);
1062                         if (value.data == NULL) {
1063                                 return LDB_ERR_OPERATIONS_ERROR;
1064                         }
1065
1066                         insert = lsqlite3_tprintf(ctx,
1067                                         "INSERT OR ROLLBACK INTO ldb_attribute_values "
1068                                         "('eid', 'attr_name', 'norm_attr_name',"
1069                                         " 'attr_value', 'norm_attr_value') "
1070                                         "VALUES ('%lld', '%q', '%q', '%q', '%q');",
1071                                         eid, el->name, attr,
1072                                         el->values[j].data, value.data);
1073                         if (insert == NULL) {
1074                                 return LDB_ERR_OPERATIONS_ERROR;
1075                         }
1076
1077                         ret = sqlite3_exec(lsqlite3->sqlite, insert, NULL, NULL, &errmsg);
1078                         if (ret != SQLITE_OK) {
1079                                 if (errmsg) {
1080                                         ldb_set_errstring(ldb, errmsg);
1081                                         free(errmsg);
1082                                 }
1083                                 return LDB_ERR_OPERATIONS_ERROR;
1084                         }
1085                 }
1086         }
1087
1088         return LDB_SUCCESS;
1089 }
1090
1091 /* modify a record */
1092 static int lsql_modify(struct lsql_context *ctx)
1093 {
1094         struct ldb_module *module = ctx->module;
1095         struct ldb_request *req = ctx->req;
1096         struct lsqlite3_private *lsqlite3;
1097         struct ldb_context *ldb;
1098         struct ldb_message *msg = req->op.mod.message;
1099         long long eid;
1100         char *errmsg;
1101         int i;
1102         int ret;
1103
1104         ldb = ldb_module_get_ctx(module);
1105         lsqlite3 = talloc_get_type(ldb_module_get_private(module),
1106                                    struct lsqlite3_private);
1107
1108         /* See if this is an ltdb special */
1109         if (ldb_dn_is_special(msg->dn)) {
1110                 /* Others return an error */
1111                 return LDB_ERR_UNWILLING_TO_PERFORM;
1112         }
1113
1114         eid = lsqlite3_get_eid(lsqlite3, msg->dn);
1115         if (eid == -1) {
1116                 return LDB_ERR_OPERATIONS_ERROR;
1117         }
1118
1119         for (i = 0; i < msg->num_elements; i++) {
1120                 const struct ldb_message_element *el = &msg->elements[i];
1121                 const struct ldb_schema_attribute *a;
1122                 int flags = el->flags & LDB_FLAG_MOD_MASK;
1123                 char *attr;
1124                 char *mod;
1125                 int j;
1126
1127                 /* Get a case-folded copy of the attribute name */
1128                 attr = ldb_attr_casefold(ctx, el->name);
1129                 if (attr == NULL) {
1130                         return LDB_ERR_OPERATIONS_ERROR;
1131                 }
1132
1133                 a = ldb_schema_attribute_by_name(ldb, el->name);
1134
1135                 switch (flags) {
1136
1137                 case LDB_FLAG_MOD_REPLACE:
1138
1139                         /* remove all attributes before adding the replacements */
1140                         mod = lsqlite3_tprintf(ctx,
1141                                                 "DELETE FROM ldb_attribute_values "
1142                                                 "WHERE eid = '%lld' "
1143                                                 "AND norm_attr_name = '%q';",
1144                                                 eid, attr);
1145                         if (mod == NULL) {
1146                                 return LDB_ERR_OPERATIONS_ERROR;
1147                         }
1148
1149                         ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1150                         if (ret != SQLITE_OK) {
1151                                 if (errmsg) {
1152                                         ldb_set_errstring(ldb, errmsg);
1153                                         free(errmsg);
1154                                 }
1155                                 return LDB_ERR_OPERATIONS_ERROR;
1156                         }
1157
1158                         /* MISSING break is INTENTIONAL */
1159
1160                 case LDB_FLAG_MOD_ADD:
1161 #warning "We should throw an error if no value is provided!"
1162                         /* For each value of the specified attribute name... */
1163                         for (j = 0; j < el->num_values; j++) {
1164                                 struct ldb_val value;
1165
1166                                 /* Get a canonicalised copy of the data */
1167                                 a->syntax->canonicalise_fn(ldb, ctx, &(el->values[j]), &value);
1168                                 if (value.data == NULL) {
1169                                         return LDB_ERR_OPERATIONS_ERROR;
1170                                 }
1171
1172                                 mod = lsqlite3_tprintf(ctx,
1173                                         "INSERT OR ROLLBACK INTO ldb_attribute_values "
1174                                         "('eid', 'attr_name', 'norm_attr_name',"
1175                                         " 'attr_value', 'norm_attr_value') "
1176                                         "VALUES ('%lld', '%q', '%q', '%q', '%q');",
1177                                         eid, el->name, attr,
1178                                         el->values[j].data, value.data);
1179
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
1194                         break;
1195
1196                 case LDB_FLAG_MOD_DELETE:
1197 #warning "We should throw an error if the attribute we are trying to delete does not exist!"
1198                         if (el->num_values == 0) {
1199                                 mod = lsqlite3_tprintf(ctx,
1200                                                         "DELETE FROM ldb_attribute_values "
1201                                                         "WHERE eid = '%lld' "
1202                                                         "AND norm_attr_name = '%q';",
1203                                                         eid, attr);
1204                                 if (mod == NULL) {
1205                                         return LDB_ERR_OPERATIONS_ERROR;
1206                                 }
1207
1208                                 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1209                                 if (ret != SQLITE_OK) {
1210                                         if (errmsg) {
1211                                                 ldb_set_errstring(ldb, errmsg);
1212                                                 free(errmsg);
1213                                         }
1214                                         return LDB_ERR_OPERATIONS_ERROR;
1215                                 }
1216                         }
1217
1218                         /* For each value of the specified attribute name... */
1219                         for (j = 0; j < el->num_values; j++) {
1220                                 struct ldb_val value;
1221
1222                                 /* Get a canonicalised copy of the data */
1223                                 a->syntax->canonicalise_fn(ldb, ctx, &(el->values[j]), &value);
1224                                 if (value.data == NULL) {
1225                                         return LDB_ERR_OPERATIONS_ERROR;
1226                                 }
1227
1228                                 mod = lsqlite3_tprintf(ctx,
1229                                         "DELETE FROM ldb_attribute_values "
1230                                         "WHERE eid = '%lld' "
1231                                         "AND norm_attr_name = '%q' "
1232                                         "AND norm_attr_value = '%q';",
1233                                         eid, attr, value.data);
1234
1235                                 if (mod == NULL) {
1236                                         return LDB_ERR_OPERATIONS_ERROR;
1237                                 }
1238
1239                                 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1240                                 if (ret != SQLITE_OK) {
1241                                         if (errmsg) {
1242                                                 ldb_set_errstring(ldb, errmsg);
1243                                                 free(errmsg);
1244                                         }
1245                                         return LDB_ERR_OPERATIONS_ERROR;
1246                                 }
1247                         }
1248
1249                         break;
1250                 }
1251         }
1252
1253         return LDB_SUCCESS;
1254 }
1255
1256 /* delete a record */
1257 static int lsql_delete(struct lsql_context *ctx)
1258 {
1259         struct ldb_module *module = ctx->module;
1260         struct ldb_request *req = ctx->req;
1261         struct lsqlite3_private *lsqlite3;
1262         struct ldb_context *ldb;
1263         long long eid;
1264         char *errmsg;
1265         char *query;
1266         int ret;
1267
1268         ldb = ldb_module_get_ctx(module);
1269         lsqlite3 = talloc_get_type(ldb_module_get_private(module),
1270                                    struct lsqlite3_private);
1271
1272         eid = lsqlite3_get_eid(lsqlite3, req->op.del.dn);
1273         if (eid == -1) {
1274                 return LDB_ERR_OPERATIONS_ERROR;
1275         }
1276
1277         query = lsqlite3_tprintf(ctx,
1278                                    /* Delete entry */
1279                                    "DELETE FROM ldb_entry WHERE eid = %lld; "
1280                                    /* Delete attributes */
1281                                    "DELETE FROM ldb_attribute_values WHERE eid = %lld; ",
1282                                 eid, eid);
1283         if (query == NULL) {
1284                 return LDB_ERR_OPERATIONS_ERROR;
1285         }
1286
1287         ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1288         if (ret != SQLITE_OK) {
1289                 if (errmsg) {
1290                         ldb_set_errstring(ldb, errmsg);
1291                         free(errmsg);
1292                 }
1293                 return LDB_ERR_OPERATIONS_ERROR;
1294         }
1295
1296         return LDB_SUCCESS;
1297 }
1298
1299 /* rename a record */
1300 static int lsql_rename(struct lsql_context *ctx)
1301 {
1302         struct ldb_module *module = ctx->module;
1303         struct ldb_request *req = ctx->req;
1304         struct lsqlite3_private *lsqlite3;
1305         struct ldb_context *ldb;
1306         char *new_dn, *new_cdn, *old_cdn;
1307         char *errmsg;
1308         char *query;
1309         int ret;
1310
1311         ldb = ldb_module_get_ctx(module);
1312         lsqlite3 = talloc_get_type(ldb_module_get_private(module),
1313                                    struct lsqlite3_private);
1314
1315         /* create linearized and normalized dns */
1316         old_cdn = ldb_dn_alloc_casefold(ctx, req->op.rename.olddn);
1317         new_cdn = ldb_dn_alloc_casefold(ctx, req->op.rename.newdn);
1318         new_dn = ldb_dn_alloc_linearized(ctx, req->op.rename.newdn);
1319         if (old_cdn == NULL || new_cdn == NULL || new_dn == NULL) {
1320                 return LDB_ERR_OPERATIONS_ERROR;
1321         }
1322
1323         /* build the SQL query */
1324         query = lsqlite3_tprintf(ctx,
1325                                  "UPDATE ldb_entry SET dn = '%q', norm_dn = '%q' "
1326                                  "WHERE norm_dn = '%q';",
1327                                  new_dn, new_cdn, old_cdn);
1328         if (query == NULL) {
1329                 return LDB_ERR_OPERATIONS_ERROR;
1330         }
1331
1332         /* execute */
1333         ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1334         if (ret != SQLITE_OK) {
1335                 if (errmsg) {
1336                         ldb_set_errstring(ldb, errmsg);
1337                         free(errmsg);
1338                 }
1339                 return LDB_ERR_OPERATIONS_ERROR;
1340         }
1341
1342         return LDB_SUCCESS;
1343 }
1344
1345 static int lsql_start_trans(struct ldb_module * module)
1346 {
1347         int ret;
1348         char *errmsg;
1349         struct lsqlite3_private *lsqlite3;
1350
1351         lsqlite3 = talloc_get_type(ldb_module_get_private(module),
1352                                    struct lsqlite3_private);
1353
1354         if (lsqlite3->trans_count == 0) {
1355                 ret = sqlite3_exec(lsqlite3->sqlite, "BEGIN IMMEDIATE;", NULL, NULL, &errmsg);
1356                 if (ret != SQLITE_OK) {
1357                         if (errmsg) {
1358                                 printf("lsqlite3_start_trans: error: %s\n", errmsg);
1359                                 free(errmsg);
1360                         }
1361                         return -1;
1362                 }
1363         };
1364
1365         lsqlite3->trans_count++;
1366
1367         return 0;
1368 }
1369
1370 static int lsql_end_trans(struct ldb_module *module)
1371 {
1372         int ret;
1373         char *errmsg;
1374         struct lsqlite3_private *lsqlite3;
1375
1376         lsqlite3 = talloc_get_type(ldb_module_get_private(module),
1377                                    struct lsqlite3_private);
1378
1379         if (lsqlite3->trans_count > 0) {
1380                 lsqlite3->trans_count--;
1381         } else return -1;
1382
1383         if (lsqlite3->trans_count == 0) {
1384                 ret = sqlite3_exec(lsqlite3->sqlite, "COMMIT;", NULL, NULL, &errmsg);
1385                 if (ret != SQLITE_OK) {
1386                         if (errmsg) {
1387                                 printf("lsqlite3_end_trans: error: %s\n", errmsg);
1388                                 free(errmsg);
1389                         }
1390                         return -1;
1391                 }
1392         }
1393
1394         return 0;
1395 }
1396
1397 static int lsql_del_trans(struct ldb_module *module)
1398 {
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                 lsqlite3->trans_count--;
1406         } else return -1;
1407
1408         if (lsqlite3->trans_count == 0) {
1409                 return lsqlite3_safe_rollback(lsqlite3->sqlite);
1410         }
1411
1412         return -1;
1413 }
1414
1415 static int destructor(struct lsqlite3_private *lsqlite3)
1416 {
1417         if (lsqlite3->sqlite) {
1418                 sqlite3_close(lsqlite3->sqlite);
1419         }
1420         return 0;
1421 }
1422
1423 static void lsql_request_done(struct lsql_context *ctx, int error)
1424 {
1425         struct ldb_context *ldb;
1426         struct ldb_request *req;
1427         struct ldb_reply *ares;
1428
1429         ldb = ldb_module_get_ctx(ctx->module);
1430         req = ctx->req;
1431
1432         /* if we already returned an error just return */
1433         if (ldb_request_get_status(req) != LDB_SUCCESS) {
1434                 return;
1435         }
1436
1437         ares = talloc_zero(req, struct ldb_reply);
1438         if (!ares) {
1439                 ldb_oom(ldb);
1440                 req->callback(req, NULL);
1441                 return;
1442         }
1443         ares->type = LDB_REPLY_DONE;
1444         ares->error = error;
1445
1446         req->callback(req, ares);
1447 }
1448
1449 static void lsql_timeout(struct tevent_context *ev,
1450                          struct tevent_timer *te,
1451                          struct timeval t,
1452                          void *private_data)
1453 {
1454         struct lsql_context *ctx;
1455         ctx = talloc_get_type(private_data, struct lsql_context);
1456
1457         lsql_request_done(ctx, LDB_ERR_TIME_LIMIT_EXCEEDED);
1458 }
1459
1460 static void lsql_callback(struct tevent_context *ev,
1461                           struct tevent_timer *te,
1462                           struct timeval t,
1463                           void *private_data)
1464 {
1465         struct lsql_context *ctx;
1466         int ret;
1467
1468         ctx = talloc_get_type(private_data, struct lsql_context);
1469
1470         switch (ctx->req->operation) {
1471         case LDB_SEARCH:
1472                 ret = lsql_search(ctx);
1473                 break;
1474         case LDB_ADD:
1475                 ret = lsql_add(ctx);
1476                 break;
1477         case LDB_MODIFY:
1478                 ret = lsql_modify(ctx);
1479                 break;
1480         case LDB_DELETE:
1481                 ret = lsql_delete(ctx);
1482                 break;
1483         case LDB_RENAME:
1484                 ret = lsql_rename(ctx);
1485                 break;
1486 /* TODO:
1487         case LDB_EXTENDED:
1488                 ret = lsql_extended(ctx);
1489                 break;
1490  */
1491         default:
1492                 /* no other op supported */
1493                 ret = LDB_ERR_UNWILLING_TO_PERFORM;
1494         }
1495
1496         if (!ctx->callback_failed) {
1497                 /* Once we are done, we do not need timeout events */
1498                 talloc_free(ctx->timeout_event);
1499                 lsql_request_done(ctx, ret);
1500         }
1501 }
1502
1503 static int lsql_handle_request(struct ldb_module *module, struct ldb_request *req)
1504 {
1505         struct ldb_context *ldb;
1506         struct tevent_context *ev;
1507         struct lsql_context *ac;
1508         struct tevent_timer *te;
1509         struct timeval tv;
1510
1511         if (check_critical_controls(req->controls)) {
1512                 return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
1513         }
1514
1515         if (req->starttime == 0 || req->timeout == 0) {
1516                 ldb_set_errstring(ldb, "Invalid timeout settings");
1517                 return LDB_ERR_TIME_LIMIT_EXCEEDED;
1518         }
1519
1520         ldb = ldb_module_get_ctx(module);
1521         ev = ldb_get_event_context(ldb);
1522
1523         ac = talloc_zero(req, struct lsql_context);
1524         if (ac == NULL) {
1525                 ldb_set_errstring(ldb, "Out of Memory");
1526                 return LDB_ERR_OPERATIONS_ERROR;
1527         }
1528
1529         ac->module = module;
1530         ac->req = req;
1531
1532         tv.tv_sec = 0;
1533         tv.tv_usec = 0;
1534         te = tevent_add_timer(ev, ac, tv, lsql_callback, ac);
1535         if (NULL == te) {
1536                 return LDB_ERR_OPERATIONS_ERROR;
1537         }
1538
1539         tv.tv_sec = req->starttime + req->timeout;
1540         ac->timeout_event = tevent_add_timer(ev, ac, tv, lsql_timeout, ac);
1541         if (NULL == ac->timeout_event) {
1542                 return LDB_ERR_OPERATIONS_ERROR;
1543         }
1544
1545         return LDB_SUCCESS;
1546 }
1547
1548 /*
1549  * Table of operations for the sqlite3 backend
1550  */
1551 static const struct ldb_module_ops lsqlite3_ops = {
1552         .name              = "sqlite",
1553         .search            = lsql_handle_request,
1554         .add               = lsql_handle_request,
1555         .modify            = lsql_handle_request,
1556         .del               = lsql_handle_request,
1557         .rename            = lsql_handle_request,
1558         .extended          = lsql_handle_request,
1559         .start_transaction = lsql_start_trans,
1560         .end_transaction   = lsql_end_trans,
1561         .del_transaction   = lsql_del_trans,
1562 };
1563
1564 /*
1565  * Static functions
1566  */
1567
1568 static int initialize(struct lsqlite3_private *lsqlite3,
1569                       struct ldb_context *ldb, const char *url, int flags)
1570 {
1571         TALLOC_CTX *local_ctx;
1572         long long queryInt;
1573         int rollback = 0;
1574         char *errmsg;
1575         char *schema;
1576         int ret;
1577
1578         /* create a local ctx */
1579         local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_rename local context");
1580         if (local_ctx == NULL) {
1581                 return -1;
1582         }
1583
1584         schema = lsqlite3_tprintf(local_ctx,
1585
1586
1587                 "CREATE TABLE ldb_info AS "
1588                 "  SELECT 'LDB' AS database_type,"
1589                 "         '1.0' AS version;"
1590
1591                 /*
1592                  * The entry table holds the information about an entry.
1593                  * This table is used to obtain the EID of the entry and to
1594                  * support scope=one and scope=base.  The parent and child
1595                  * table is included in the entry table since all the other
1596                  * attributes are dependent on EID.
1597                  */
1598                 "CREATE TABLE ldb_entry "
1599                 "("
1600                 "  eid     INTEGER PRIMARY KEY AUTOINCREMENT,"
1601                 "  dn      TEXT UNIQUE NOT NULL,"
1602                 "  norm_dn TEXT UNIQUE NOT NULL"
1603                 ");"
1604
1605
1606                 "CREATE TABLE ldb_object_classes"
1607                 "("
1608                 "  class_name            TEXT PRIMARY KEY,"
1609                 "  parent_class_name     TEXT,"
1610                 "  tree_key              TEXT UNIQUE,"
1611                 "  max_child_num         INTEGER DEFAULT 0"
1612                 ");"
1613
1614                 /*
1615                  * We keep a full listing of attribute/value pairs here
1616                  */
1617                 "CREATE TABLE ldb_attribute_values"
1618                 "("
1619                 "  eid             INTEGER REFERENCES ldb_entry,"
1620                 "  attr_name       TEXT,"
1621                 "  norm_attr_name  TEXT,"
1622                 "  attr_value      TEXT,"
1623                 "  norm_attr_value TEXT "
1624                 ");"
1625
1626
1627                 /*
1628                  * Indexes
1629                  */
1630                 "CREATE INDEX ldb_attribute_values_eid_idx "
1631                 "  ON ldb_attribute_values (eid);"
1632
1633                 "CREATE INDEX ldb_attribute_values_name_value_idx "
1634                 "  ON ldb_attribute_values (attr_name, norm_attr_value);"
1635
1636
1637
1638                 /*
1639                  * Triggers
1640                  */
1641
1642                 "CREATE TRIGGER ldb_object_classes_insert_tr"
1643                 "  AFTER INSERT"
1644                 "  ON ldb_object_classes"
1645                 "  FOR EACH ROW"
1646                 "    BEGIN"
1647                 "      UPDATE ldb_object_classes"
1648                 "        SET tree_key = COALESCE(tree_key, "
1649                 "              ("
1650                 "                SELECT tree_key || "
1651                 "                       (SELECT base160(max_child_num + 1)"
1652                 "                                FROM ldb_object_classes"
1653                 "                                WHERE class_name = "
1654                 "                                      new.parent_class_name)"
1655                 "                  FROM ldb_object_classes "
1656                 "                  WHERE class_name = new.parent_class_name "
1657                 "              ));"
1658                 "      UPDATE ldb_object_classes "
1659                 "        SET max_child_num = max_child_num + 1"
1660                 "        WHERE class_name = new.parent_class_name;"
1661                 "    END;"
1662
1663                 /*
1664                  * Table initialization
1665                  */
1666
1667                 "INSERT INTO ldb_object_classes "
1668                 "    (class_name, tree_key) "
1669                 "  VALUES "
1670                 "    ('TOP', '0001');");
1671
1672         /* Skip protocol indicator of url  */
1673         if (strncmp(url, "sqlite3://", 10) != 0) {
1674                 return SQLITE_MISUSE;
1675         }
1676
1677         /* Update pointer to just after the protocol indicator */
1678         url += 10;
1679
1680         /* Try to open the (possibly empty/non-existent) database */
1681         if ((ret = sqlite3_open(url, &lsqlite3->sqlite)) != SQLITE_OK) {
1682                 return ret;
1683         }
1684
1685         /* In case this is a new database, enable auto_vacuum */
1686         ret = sqlite3_exec(lsqlite3->sqlite, "PRAGMA auto_vacuum = 1;", NULL, NULL, &errmsg);
1687         if (ret != SQLITE_OK) {
1688                 if (errmsg) {
1689                         printf("lsqlite3 initializaion error: %s\n", errmsg);
1690                         free(errmsg);
1691                 }
1692                 goto failed;
1693         }
1694
1695         if (flags & LDB_FLG_NOSYNC) {
1696                 /* DANGEROUS */
1697                 ret = sqlite3_exec(lsqlite3->sqlite, "PRAGMA synchronous = OFF;", NULL, NULL, &errmsg);
1698                 if (ret != SQLITE_OK) {
1699                         if (errmsg) {
1700                                 printf("lsqlite3 initializaion error: %s\n", errmsg);
1701                                 free(errmsg);
1702                         }
1703                         goto failed;
1704                 }
1705         }
1706
1707         /* */
1708
1709         /* Establish a busy timeout of 30 seconds */
1710         if ((ret = sqlite3_busy_timeout(lsqlite3->sqlite,
1711                                         30000)) != SQLITE_OK) {
1712                 return ret;
1713         }
1714
1715         /* Create a function, callable from sql, to increment a tree_key */
1716         if ((ret =
1717              sqlite3_create_function(lsqlite3->sqlite,/* handle */
1718                                      "base160_next",  /* function name */
1719                                      1,               /* number of args */
1720                                      SQLITE_ANY,      /* preferred text type */
1721                                      NULL,            /* user data */
1722                                      base160next_sql, /* called func */
1723                                      NULL,            /* step func */
1724                                      NULL             /* final func */
1725                      )) != SQLITE_OK) {
1726                 return ret;
1727         }
1728
1729         /* Create a function, callable from sql, to convert int to base160 */
1730         if ((ret =
1731              sqlite3_create_function(lsqlite3->sqlite,/* handle */
1732                                      "base160",       /* function name */
1733                                      1,               /* number of args */
1734                                      SQLITE_ANY,      /* preferred text type */
1735                                      NULL,            /* user data */
1736                                      base160_sql,     /* called func */
1737                                      NULL,            /* step func */
1738                                      NULL             /* final func */
1739                      )) != SQLITE_OK) {
1740                 return ret;
1741         }
1742
1743         /* Create a function, callable from sql, to perform various comparisons */
1744         if ((ret =
1745              sqlite3_create_function(lsqlite3->sqlite, /* handle */
1746                                      "ldap_compare",   /* function name */
1747                                      4,                /* number of args */
1748                                      SQLITE_ANY,       /* preferred text type */
1749                                      ldb  ,            /* user data */
1750                                      lsqlite3_compare, /* called func */
1751                                      NULL,             /* step func */
1752                                      NULL              /* final func */
1753                      )) != SQLITE_OK) {
1754                 return ret;
1755         }
1756
1757         /* Begin a transaction */
1758         ret = sqlite3_exec(lsqlite3->sqlite, "BEGIN EXCLUSIVE;", NULL, NULL, &errmsg);
1759         if (ret != SQLITE_OK) {
1760                 if (errmsg) {
1761                         printf("lsqlite3: initialization error: %s\n", errmsg);
1762                         free(errmsg);
1763                 }
1764                 goto failed;
1765         }
1766         rollback = 1;
1767
1768         /* Determine if this is a new database.  No tables means it is. */
1769         if (query_int(lsqlite3,
1770                       &queryInt,
1771                       "SELECT COUNT(*)\n"
1772                       "  FROM sqlite_master\n"
1773                       "  WHERE type = 'table';") != 0) {
1774                 goto failed;
1775         }
1776
1777         if (queryInt == 0) {
1778                 /*
1779                  * Create the database schema
1780                  */
1781                 ret = sqlite3_exec(lsqlite3->sqlite, schema, NULL, NULL, &errmsg);
1782                 if (ret != SQLITE_OK) {
1783                         if (errmsg) {
1784                                 printf("lsqlite3 initializaion error: %s\n", errmsg);
1785                                 free(errmsg);
1786                         }
1787                         goto failed;
1788                 }
1789         } else {
1790                 /*
1791                  * Ensure that the database we opened is one of ours
1792                  */
1793                 if (query_int(lsqlite3,
1794                               &queryInt,
1795                               "SELECT "
1796                               "  (SELECT COUNT(*) = 2"
1797                               "     FROM sqlite_master "
1798                               "     WHERE type = 'table' "
1799                               "       AND name IN "
1800                               "         ("
1801                               "           'ldb_entry', "
1802                               "           'ldb_object_classes' "
1803                               "         ) "
1804                               "  ) "
1805                               "  AND "
1806                               "  (SELECT 1 "
1807                               "     FROM ldb_info "
1808                               "     WHERE database_type = 'LDB' "
1809                               "       AND version = '1.0'"
1810                               "  );") != 0 ||
1811                     queryInt != 1) {
1812
1813                         /* It's not one that we created.  See ya! */
1814                         goto failed;
1815                 }
1816         }
1817
1818         /* Commit the transaction */
1819         ret = sqlite3_exec(lsqlite3->sqlite, "COMMIT;", NULL, NULL, &errmsg);
1820         if (ret != SQLITE_OK) {
1821                 if (errmsg) {
1822                         printf("lsqlite3: iniialization error: %s\n", errmsg);
1823                         free(errmsg);
1824                 }
1825                 goto failed;
1826         }
1827
1828         return SQLITE_OK;
1829
1830 failed:
1831         if (rollback) lsqlite3_safe_rollback(lsqlite3->sqlite);
1832         sqlite3_close(lsqlite3->sqlite);
1833         return -1;
1834 }
1835
1836 /*
1837  * connect to the database
1838  */
1839 static int lsqlite3_connect(struct ldb_context *ldb,
1840                             const char *url,
1841                             unsigned int flags,
1842                             const char *options[],
1843                             struct ldb_module **_module)
1844 {
1845         struct ldb_module *module;
1846         struct lsqlite3_private *lsqlite3;
1847         int i, ret;
1848
1849         module = ldb_module_new(ldb, ldb, "ldb_sqlite3 backend", &lsqlite3_ops);
1850         if (!module) return -1;
1851
1852         lsqlite3 = talloc(module, struct lsqlite3_private);
1853         if (!lsqlite3) {
1854                 goto failed;
1855         }
1856
1857         lsqlite3->sqlite = NULL;
1858         lsqlite3->options = NULL;
1859         lsqlite3->trans_count = 0;
1860
1861         ret = initialize(lsqlite3, ldb, url, flags);
1862         if (ret != SQLITE_OK) {
1863                 goto failed;
1864         }
1865
1866         talloc_set_destructor(lsqlite3, destructor);
1867
1868         ldb_module_set_private(module, lsqlite3);
1869
1870         if (options) {
1871                 /*
1872                  * take a copy of the options array, so we don't have to rely
1873                  * on the caller keeping it around (it might be dynamic)
1874                  */
1875                 for (i=0;options[i];i++) ;
1876
1877                 lsqlite3->options = talloc_array(lsqlite3, char *, i+1);
1878                 if (!lsqlite3->options) {
1879                         goto failed;
1880                 }
1881
1882                 for (i=0;options[i];i++) {
1883
1884                         lsqlite3->options[i+1] = NULL;
1885                         lsqlite3->options[i] =
1886                                 talloc_strdup(lsqlite3->options, options[i]);
1887                         if (!lsqlite3->options[i]) {
1888                                 goto failed;
1889                         }
1890                 }
1891         }
1892
1893         *_module = module;
1894         return 0;
1895
1896 failed:
1897         if (lsqlite3 && lsqlite3->sqlite != NULL) {
1898                 (void) sqlite3_close(lsqlite3->sqlite);
1899         }
1900         talloc_free(lsqlite3);
1901         return -1;
1902 }
1903
1904 const struct ldb_backend_ops ldb_sqlite3_backend_ops = {
1905         .name = "sqlite3",
1906         .connect_fn = lsqlite3_connect
1907 };