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