r16090: Fix standalone build after the rename of enum ldb_request_type in
[Samba.git] / source4 / lib / ldb / ldb_sqlite3 / ldb_sqlite3.c
blobc9b453587ec9a98fe33993d6fd0fd6652b59ad15
1 /*
2 ldb database library
4 Copyright (C) Derrell Lipman 2005
5 Copyright (C) Simo Sorce 2005
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
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2 of the License, or (at your option) any later version.
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.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 * Name: ldb
29 * Component: ldb sqlite3 backend
31 * Description: core files for SQLITE3 backend
33 * Author: Derrell Lipman (based on Andrew Tridgell's LDAP backend)
36 #include "includes.h"
37 #include "ldb/include/includes.h"
39 #include <sqlite3.h>
41 struct lsqlite3_private {
42 int trans_count;
43 char **options;
44 sqlite3 *sqlite;
47 struct lsql_async_context {
48 struct ldb_module *module;
50 /* search stuff */
51 long long current_eid;
52 const char * const * attrs;
53 struct ldb_async_result *ares;
55 /* async stuff */
56 void *context;
57 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *);
60 static struct ldb_async_handle *init_handle(struct lsqlite3_private *lsqlite3, struct ldb_module *module,
61 void *context,
62 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *))
64 struct lsql_async_context *ac;
65 struct ldb_async_handle *h;
67 h = talloc_zero(lsqlite3, struct ldb_async_handle);
68 if (h == NULL) {
69 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
70 return NULL;
73 h->module = module;
75 ac = talloc(h, struct lsql_async_context);
76 if (ac == NULL) {
77 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
78 talloc_free(h);
79 return NULL;
82 h->private_data = (void *)ac;
84 h->state = LDB_ASYNC_INIT;
85 h->status = LDB_SUCCESS;
87 ac->module = module;
88 ac->context = context;
89 ac->callback = callback;
91 return h;
95 * Macros used throughout
98 #ifndef FALSE
99 # define FALSE (0)
100 # define TRUE (! FALSE)
101 #endif
103 #define RESULT_ATTR_TABLE "temp_result_attrs"
105 //#define TEMPTAB /* for testing, create non-temporary table */
106 #define TEMPTAB "TEMPORARY"
109 * Static variables
111 sqlite3_stmt * stmtGetEID = NULL;
113 static char *lsqlite3_tprintf(TALLOC_CTX *mem_ctx, const char *fmt, ...)
115 char *str, *ret;
116 va_list ap;
118 va_start(ap, fmt);
119 str = sqlite3_vmprintf(fmt, ap);
120 va_end(ap);
122 if (str == NULL) return NULL;
124 ret = talloc_strdup(mem_ctx, str);
125 if (ret == NULL) {
126 sqlite3_free(str);
127 return NULL;
130 sqlite3_free(str);
131 return ret;
134 static char base160tab[161] = {
135 48 ,49 ,50 ,51 ,52 ,53 ,54 ,55 ,56 ,57 , /* 0-9 */
136 58 ,59 ,65 ,66 ,67 ,68 ,69 ,70 ,71 ,72 , /* : ; A-H */
137 73 ,74 ,75 ,76 ,77 ,78 ,79 ,80 ,81 ,82 , /* I-R */
138 83 ,84 ,85 ,86 ,87 ,88 ,89 ,90 ,97 ,98 , /* S-Z , a-b */
139 99 ,100,101,102,103,104,105,106,107,108, /* c-l */
140 109,110,111,112,113,114,115,116,117,118, /* m-v */
141 119,120,121,122,160,161,162,163,164,165, /* w-z, latin1 */
142 166,167,168,169,170,171,172,173,174,175, /* latin1 */
143 176,177,178,179,180,181,182,183,184,185, /* latin1 */
144 186,187,188,189,190,191,192,193,194,195, /* latin1 */
145 196,197,198,199,200,201,202,203,204,205, /* latin1 */
146 206,207,208,209,210,211,212,213,214,215, /* latin1 */
147 216,217,218,219,220,221,222,223,224,225, /* latin1 */
148 226,227,228,229,230,231,232,233,234,235, /* latin1 */
149 236,237,238,239,240,241,242,243,244,245, /* latin1 */
150 246,247,248,249,250,251,252,253,254,255, /* latin1 */
151 '\0'
156 * base160()
158 * Convert an unsigned long integer into a base160 representation of the
159 * number.
161 * Parameters:
162 * val --
163 * value to be converted
165 * result --
166 * character array, 5 bytes long, into which the base160 representation
167 * will be placed. The result will be a four-digit representation of the
168 * number (with leading zeros prepended as necessary), and null
169 * terminated.
171 * Returns:
172 * Nothing
174 static void
175 base160_sql(sqlite3_context * hContext,
176 int argc,
177 sqlite3_value ** argv)
179 int i;
180 long long val;
181 char result[5];
183 val = sqlite3_value_int64(argv[0]);
185 for (i = 3; i >= 0; i--) {
187 result[i] = base160tab[val % 160];
188 val /= 160;
191 result[4] = '\0';
193 sqlite3_result_text(hContext, result, -1, SQLITE_TRANSIENT);
198 * base160next_sql()
200 * This function enhances sqlite by adding a "base160_next()" function which is
201 * accessible via queries.
203 * Retrieve the next-greater number in the base160 sequence for the terminal
204 * tree node (the last four digits). Only one tree level (four digits) is
205 * operated on.
207 * Input:
208 * A character string: either an empty string (in which case no operation is
209 * performed), or a string of base160 digits with a length of a multiple of
210 * four digits.
212 * Output:
213 * Upon return, the trailing four digits (one tree level) will have been
214 * incremented by 1.
216 static void
217 base160next_sql(sqlite3_context * hContext,
218 int argc,
219 sqlite3_value ** argv)
221 int i;
222 int len;
223 char * pTab;
224 char * pBase160 = strdup((const char *)sqlite3_value_text(argv[0]));
225 char * pStart = pBase160;
228 * We need a minimum of four digits, and we will always get a multiple
229 * of four digits.
231 if (pBase160 != NULL &&
232 (len = strlen(pBase160)) >= 4 &&
233 len % 4 == 0) {
235 if (pBase160 == NULL) {
237 sqlite3_result_null(hContext);
238 return;
241 pBase160 += strlen(pBase160) - 1;
243 /* We only carry through four digits: one level in the tree */
244 for (i = 0; i < 4; i++) {
246 /* What base160 value does this digit have? */
247 pTab = strchr(base160tab, *pBase160);
249 /* Is there a carry? */
250 if (pTab < base160tab + sizeof(base160tab) - 1) {
253 * Nope. Just increment this value and we're
254 * done.
256 *pBase160 = *++pTab;
257 break;
258 } else {
261 * There's a carry. This value gets
262 * base160tab[0], we decrement the buffer
263 * pointer to get the next higher-order digit,
264 * and continue in the loop.
266 *pBase160-- = base160tab[0];
270 sqlite3_result_text(hContext,
271 pStart,
272 strlen(pStart),
273 free);
274 } else {
275 sqlite3_result_value(hContext, argv[0]);
276 if (pBase160 != NULL) {
277 free(pBase160);
282 static char *parsetree_to_sql(struct ldb_module *module,
283 void *mem_ctx,
284 const struct ldb_parse_tree *t)
286 const struct ldb_attrib_handler *h;
287 struct ldb_val value, subval;
288 char *wild_card_string;
289 char *child, *tmp;
290 char *ret = NULL;
291 char *attr;
292 int i;
295 switch(t->operation) {
296 case LDB_OP_AND:
298 tmp = parsetree_to_sql(module, mem_ctx, t->u.list.elements[0]);
299 if (tmp == NULL) return NULL;
301 for (i = 1; i < t->u.list.num_elements; i++) {
303 child = parsetree_to_sql(module, mem_ctx, t->u.list.elements[i]);
304 if (child == NULL) return NULL;
306 tmp = talloc_asprintf_append(tmp, " INTERSECT %s ", child);
307 if (tmp == NULL) return NULL;
310 ret = talloc_asprintf(mem_ctx, "SELECT * FROM ( %s )\n", tmp);
312 return ret;
314 case LDB_OP_OR:
316 tmp = parsetree_to_sql(module, mem_ctx, t->u.list.elements[0]);
317 if (tmp == NULL) return NULL;
319 for (i = 1; i < t->u.list.num_elements; i++) {
321 child = parsetree_to_sql(module, mem_ctx, t->u.list.elements[i]);
322 if (child == NULL) return NULL;
324 tmp = talloc_asprintf_append(tmp, " UNION %s ", child);
325 if (tmp == NULL) return NULL;
328 return talloc_asprintf(mem_ctx, "SELECT * FROM ( %s ) ", tmp);
330 case LDB_OP_NOT:
332 child = parsetree_to_sql(module, mem_ctx, t->u.isnot.child);
333 if (child == NULL) return NULL;
335 return talloc_asprintf(mem_ctx,
336 "SELECT eid FROM ldb_entry "
337 "WHERE eid NOT IN ( %s ) ", child);
339 case LDB_OP_EQUALITY:
341 * For simple searches, we want to retrieve the list of EIDs that
342 * match the criteria.
344 attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
345 if (attr == NULL) return NULL;
346 h = ldb_attrib_handler(module->ldb, attr);
348 /* Get a canonicalised copy of the data */
349 h->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
350 if (value.data == NULL) {
351 return NULL;
354 if (strcasecmp(t->u.equality.attr, "objectclass") == 0) {
356 * For object classes, we want to search for all objectclasses
357 * that are subclasses as well.
359 return lsqlite3_tprintf(mem_ctx,
360 "SELECT eid FROM ldb_attribute_values\n"
361 "WHERE norm_attr_name = 'OBJECTCLASS' "
362 "AND norm_attr_value IN\n"
363 " (SELECT class_name FROM ldb_object_classes\n"
364 " WHERE tree_key GLOB\n"
365 " (SELECT tree_key FROM ldb_object_classes\n"
366 " WHERE class_name = '%q'\n"
367 " ) || '*'\n"
368 " )\n", value.data);
370 } else if (strcasecmp(t->u.equality.attr, "dn") == 0) {
371 /* DN query is a special ldb case */
372 char *cdn = ldb_dn_linearize_casefold(module->ldb,
373 ldb_dn_explode(module->ldb,
374 (const char *)value.data));
376 return lsqlite3_tprintf(mem_ctx,
377 "SELECT eid FROM ldb_entry "
378 "WHERE norm_dn = '%q'", cdn);
380 } else {
381 /* A normal query. */
382 return lsqlite3_tprintf(mem_ctx,
383 "SELECT eid FROM ldb_attribute_values "
384 "WHERE norm_attr_name = '%q' "
385 "AND norm_attr_value = '%q'",
386 attr,
387 value.data);
391 case LDB_OP_SUBSTRING:
393 wild_card_string = talloc_strdup(mem_ctx,
394 (t->u.substring.start_with_wildcard)?"*":"");
395 if (wild_card_string == NULL) return NULL;
397 for (i = 0; t->u.substring.chunks[i]; i++) {
398 wild_card_string = talloc_asprintf_append(wild_card_string, "%s*",
399 t->u.substring.chunks[i]->data);
400 if (wild_card_string == NULL) return NULL;
403 if ( ! t->u.substring.end_with_wildcard ) {
404 /* remove last wildcard */
405 wild_card_string[strlen(wild_card_string) - 1] = '\0';
408 attr = ldb_attr_casefold(mem_ctx, t->u.substring.attr);
409 if (attr == NULL) return NULL;
410 h = ldb_attrib_handler(module->ldb, attr);
412 subval.data = (void *)wild_card_string;
413 subval.length = strlen(wild_card_string) + 1;
415 /* Get a canonicalised copy of the data */
416 h->canonicalise_fn(module->ldb, mem_ctx, &(subval), &value);
417 if (value.data == NULL) {
418 return NULL;
421 return lsqlite3_tprintf(mem_ctx,
422 "SELECT eid FROM ldb_attribute_values "
423 "WHERE norm_attr_name = '%q' "
424 "AND norm_attr_value GLOB '%q'",
425 attr,
426 value.data);
428 case LDB_OP_GREATER:
429 attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
430 if (attr == NULL) return NULL;
431 h = ldb_attrib_handler(module->ldb, attr);
433 /* Get a canonicalised copy of the data */
434 h->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
435 if (value.data == NULL) {
436 return NULL;
439 return lsqlite3_tprintf(mem_ctx,
440 "SELECT eid FROM ldb_attribute_values "
441 "WHERE norm_attr_name = '%q' "
442 "AND ldap_compare(norm_attr_value, '>=', '%q', '%q') ",
443 attr,
444 value.data,
445 attr);
447 case LDB_OP_LESS:
448 attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
449 if (attr == NULL) return NULL;
450 h = ldb_attrib_handler(module->ldb, attr);
452 /* Get a canonicalised copy of the data */
453 h->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
454 if (value.data == NULL) {
455 return NULL;
458 return lsqlite3_tprintf(mem_ctx,
459 "SELECT eid FROM ldb_attribute_values "
460 "WHERE norm_attr_name = '%q' "
461 "AND ldap_compare(norm_attr_value, '<=', '%q', '%q') ",
462 attr,
463 value.data,
464 attr);
466 case LDB_OP_PRESENT:
467 if (strcasecmp(t->u.present.attr, "dn") == 0) {
468 return talloc_strdup(mem_ctx, "SELECT eid FROM ldb_entry");
471 attr = ldb_attr_casefold(mem_ctx, t->u.present.attr);
472 if (attr == NULL) return NULL;
474 return lsqlite3_tprintf(mem_ctx,
475 "SELECT eid FROM ldb_attribute_values "
476 "WHERE norm_attr_name = '%q' ",
477 attr);
479 case LDB_OP_APPROX:
480 attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
481 if (attr == NULL) return NULL;
482 h = ldb_attrib_handler(module->ldb, attr);
484 /* Get a canonicalised copy of the data */
485 h->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
486 if (value.data == NULL) {
487 return NULL;
490 return lsqlite3_tprintf(mem_ctx,
491 "SELECT eid FROM ldb_attribute_values "
492 "WHERE norm_attr_name = '%q' "
493 "AND ldap_compare(norm_attr_value, '~%', 'q', '%q') ",
494 attr,
495 value.data,
496 attr);
498 case LDB_OP_EXTENDED:
499 #warning "work out how to handle bitops"
500 return NULL;
502 default:
503 break;
506 /* should never occur */
507 abort();
508 return NULL;
512 * query_int()
514 * This function is used for the common case of queries that return a single
515 * integer value.
517 * NOTE: If more than one value is returned by the query, all but the first
518 * one will be ignored.
520 static int
521 query_int(const struct lsqlite3_private * lsqlite3,
522 long long * pRet,
523 const char * pSql,
524 ...)
526 int ret;
527 int bLoop;
528 char * p;
529 sqlite3_stmt * pStmt;
530 va_list args;
532 /* Begin access to variable argument list */
533 va_start(args, pSql);
535 /* Format the query */
536 if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
537 return SQLITE_NOMEM;
541 * Prepare and execute the SQL statement. Loop allows retrying on
542 * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
543 * requiring retrying the operation.
545 for (bLoop = TRUE; bLoop; ) {
547 /* Compile the SQL statement into sqlite virtual machine */
548 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
551 &pStmt,
552 NULL)) == SQLITE_SCHEMA) {
553 if (stmtGetEID != NULL) {
554 sqlite3_finalize(stmtGetEID);
555 stmtGetEID = NULL;
557 continue;
558 } else if (ret != SQLITE_OK) {
559 break;
562 /* One row expected */
563 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
564 if (stmtGetEID != NULL) {
565 sqlite3_finalize(stmtGetEID);
566 stmtGetEID = NULL;
568 (void) sqlite3_finalize(pStmt);
569 continue;
570 } else if (ret != SQLITE_ROW) {
571 (void) sqlite3_finalize(pStmt);
572 break;
575 /* Get the value to be returned */
576 *pRet = sqlite3_column_int64(pStmt, 0);
578 /* Free the virtual machine */
579 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
580 if (stmtGetEID != NULL) {
581 sqlite3_finalize(stmtGetEID);
582 stmtGetEID = NULL;
584 continue;
585 } else if (ret != SQLITE_OK) {
586 (void) sqlite3_finalize(pStmt);
587 break;
591 * Normal condition is only one time through loop. Loop is
592 * rerun in error conditions, via "continue", above.
594 bLoop = FALSE;
597 /* All done with variable argument list */
598 va_end(args);
601 /* Free the memory we allocated for our query string */
602 sqlite3_free(p);
604 return ret;
608 * This is a bad hack to support ldap style comparisons whithin sqlite.
609 * val is the attribute in the row currently under test
610 * func is the desired test "<=" ">=" "~" ":"
611 * cmp is the value to compare against (eg: "test")
612 * attr is the attribute name the value of which we want to test
615 static void lsqlite3_compare(sqlite3_context *ctx, int argc,
616 sqlite3_value **argv)
618 struct ldb_context *ldb = (struct ldb_context *)sqlite3_user_data(ctx);
619 const char *val = (const char *)sqlite3_value_text(argv[0]);
620 const char *func = (const char *)sqlite3_value_text(argv[1]);
621 const char *cmp = (const char *)sqlite3_value_text(argv[2]);
622 const char *attr = (const char *)sqlite3_value_text(argv[3]);
623 const struct ldb_attrib_handler *h;
624 struct ldb_val valX;
625 struct ldb_val valY;
626 int ret;
628 switch (func[0]) {
629 /* greater */
630 case '>': /* >= */
631 h = ldb_attrib_handler(ldb, attr);
632 valX.data = (void *)cmp;
633 valX.length = strlen(cmp);
634 valY.data = (void *)val;
635 valY.length = strlen(val);
636 ret = h->comparison_fn(ldb, ldb, &valY, &valX);
637 if (ret >= 0)
638 sqlite3_result_int(ctx, 1);
639 else
640 sqlite3_result_int(ctx, 0);
641 return;
643 /* lesser */
644 case '<': /* <= */
645 h = ldb_attrib_handler(ldb, attr);
646 valX.data = (void *)cmp;
647 valX.length = strlen(cmp);
648 valY.data = (void *)val;
649 valY.length = strlen(val);
650 ret = h->comparison_fn(ldb, ldb, &valY, &valX);
651 if (ret <= 0)
652 sqlite3_result_int(ctx, 1);
653 else
654 sqlite3_result_int(ctx, 0);
655 return;
657 /* approx */
658 case '~':
659 /* TODO */
660 sqlite3_result_int(ctx, 0);
661 return;
663 /* bitops */
664 case ':':
665 /* TODO */
666 sqlite3_result_int(ctx, 0);
667 return;
669 default:
670 break;
673 sqlite3_result_error(ctx, "Value must start with a special operation char (<>~:)!", -1);
674 return;
678 /* rename a record */
679 static int lsqlite3_safe_rollback(sqlite3 *sqlite)
681 char *errmsg;
682 int ret;
684 /* execute */
685 ret = sqlite3_exec(sqlite, "ROLLBACK;", NULL, NULL, &errmsg);
686 if (ret != SQLITE_OK) {
687 if (errmsg) {
688 printf("lsqlite3_safe_rollback: Error: %s\n", errmsg);
689 free(errmsg);
691 return -1;
694 return 0;
697 /* return an eid as result */
698 static int lsqlite3_eid_callback(void *result, int col_num, char **cols, char **names)
700 long long *eid = (long long *)result;
702 if (col_num != 1) return SQLITE_ABORT;
703 if (strcasecmp(names[0], "eid") != 0) return SQLITE_ABORT;
705 *eid = atoll(cols[0]);
706 return SQLITE_OK;
710 * add a single set of ldap message values to a ldb_message
712 static int lsqlite3_search_callback(void *result, int col_num, char **cols, char **names)
714 struct ldb_async_handle *handle = talloc_get_type(result, struct ldb_async_handle);
715 struct lsql_async_context *ac = talloc_get_type(handle->private_data, struct lsql_async_context);
716 struct ldb_message *msg;
717 long long eid;
718 int i;
720 /* eid, dn, attr_name, attr_value */
721 if (col_num != 4)
722 return SQLITE_ABORT;
724 eid = atoll(cols[0]);
726 if (eid != ac->current_eid) { /* here begin a new entry */
728 /* call the async callback for the last entry
729 * except the first time */
730 if (ac->current_eid != 0) {
731 ac->ares->message = ldb_msg_canonicalize(ac->module->ldb, ac->ares->message);
732 if (ac->ares->message == NULL)
733 return SQLITE_ABORT;
735 handle->status = ac->callback(ac->module->ldb, ac->context, ac->ares);
736 if (handle->status != LDB_SUCCESS)
737 return SQLITE_ABORT;
740 /* start over */
741 ac->ares = talloc_zero(ac, struct ldb_async_result);
742 if (!ac->ares)
743 return SQLITE_ABORT;
745 ac->ares->message = ldb_msg_new(ac->ares);
746 if (!ac->ares->message)
747 return SQLITE_ABORT;
749 ac->ares->type = LDB_REPLY_ENTRY;
750 ac->current_eid = eid;
753 msg = ac->ares->message;
755 if (msg->dn == NULL) {
756 msg->dn = ldb_dn_explode(msg, cols[1]);
757 if (msg->dn == NULL)
758 return SQLITE_ABORT;
761 if (ac->attrs) {
762 int found = 0;
763 for (i = 0; ac->attrs[i]; i++) {
764 if (strcasecmp(cols[2], ac->attrs[i]) == 0) {
765 found = 1;
766 break;
769 if (!found) return SQLITE_OK;
772 if (ldb_msg_add_string(msg, cols[2], cols[3]) != 0) {
773 return SQLITE_ABORT;
776 return SQLITE_OK;
781 * lsqlite3_get_eid()
782 * lsqlite3_get_eid_ndn()
784 * These functions are used for the very common case of retrieving an EID value
785 * given a (normalized) DN.
788 static long long lsqlite3_get_eid_ndn(sqlite3 *sqlite, void *mem_ctx, const char *norm_dn)
790 char *errmsg;
791 char *query;
792 long long eid = -1;
793 long long ret;
795 /* get object eid */
796 query = lsqlite3_tprintf(mem_ctx, "SELECT eid "
797 "FROM ldb_entry "
798 "WHERE norm_dn = '%q';", norm_dn);
799 if (query == NULL) return -1;
801 ret = sqlite3_exec(sqlite, query, lsqlite3_eid_callback, &eid, &errmsg);
802 if (ret != SQLITE_OK) {
803 if (errmsg) {
804 printf("lsqlite3_get_eid: Fatal Error: %s\n", errmsg);
805 free(errmsg);
807 return -1;
810 return eid;
813 static long long lsqlite3_get_eid(struct ldb_module *module, const struct ldb_dn *dn)
815 TALLOC_CTX *local_ctx;
816 struct lsqlite3_private *lsqlite3 = module->private_data;
817 long long eid = -1;
818 char *cdn;
820 /* ignore ltdb specials */
821 if (ldb_dn_is_special(dn)) {
822 return -1;
825 /* create a local ctx */
826 local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_get_eid local context");
827 if (local_ctx == NULL) {
828 return -1;
831 cdn = ldb_dn_linearize(local_ctx, ldb_dn_casefold(module->ldb, dn));
832 if (!cdn) goto done;
834 eid = lsqlite3_get_eid_ndn(lsqlite3->sqlite, local_ctx, cdn);
836 done:
837 talloc_free(local_ctx);
838 return eid;
842 * Interface functions referenced by lsqlite3_ops
845 static int lsql_search_sync_callback(struct ldb_context *ldb, void *context, struct ldb_async_result *ares)
847 struct ldb_result *res = NULL;
849 if (!context) {
850 ldb_set_errstring(ldb, talloc_strdup(ldb, "NULL Context in callback"));
851 goto error;
854 res = *((struct ldb_result **)context);
856 if (!res || !ares) {
857 goto error;
860 if (ares->type == LDB_REPLY_ENTRY) {
861 res->msgs = talloc_realloc(res, res->msgs, struct ldb_message *, res->count + 2);
862 if (! res->msgs) {
863 goto error;
866 res->msgs[res->count + 1] = NULL;
868 res->msgs[res->count] = talloc_steal(res->msgs, ares->message);
869 if (! res->msgs[res->count]) {
870 goto error;
873 res->count++;
874 } else {
875 ldb_debug(ldb, LDB_DEBUG_ERROR, "unrecognized async reply in ltdb_search_sync_callback!\n");
876 goto error;
879 talloc_free(ares);
880 return LDB_SUCCESS;
882 error:
883 if (ares) talloc_free(ares);
884 if (res) talloc_free(res);
885 if (context) *((struct ldb_result **)context) = NULL;
886 return LDB_ERR_OPERATIONS_ERROR;
889 /* search for matching records, by tree */
890 int lsql_search_async(struct ldb_module *module, const struct ldb_dn *base,
891 enum ldb_scope scope, struct ldb_parse_tree *tree,
892 const char * const *attrs,
893 void *context,
894 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
895 struct ldb_async_handle **handle)
897 struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
898 struct lsql_async_context *lsql_ac;
899 char *norm_basedn;
900 char *sqlfilter;
901 char *errmsg;
902 char *query = NULL;
903 int ret;
905 *handle = init_handle(lsqlite3, module, context, callback);
906 if (*handle == NULL) {
907 talloc_free(*handle);
908 return LDB_ERR_OPERATIONS_ERROR;
911 lsql_ac = talloc_get_type((*handle)->private_data, struct lsql_async_context);
913 if (base) {
914 norm_basedn = ldb_dn_linearize(lsql_ac, ldb_dn_casefold(module->ldb, base));
915 if (norm_basedn == NULL) {
916 ret = LDB_ERR_INVALID_DN_SYNTAX;
917 goto failed;
919 } else norm_basedn = talloc_strdup(lsql_ac, "");
921 if (*norm_basedn == '\0' &&
922 (scope == LDB_SCOPE_BASE || scope == LDB_SCOPE_ONELEVEL)) {
923 ret = LDB_ERR_UNWILLING_TO_PERFORM;
924 goto failed;
927 /* Convert filter into a series of SQL conditions (constraints) */
928 sqlfilter = parsetree_to_sql(module, lsql_ac, tree);
930 switch(scope) {
931 case LDB_SCOPE_DEFAULT:
932 case LDB_SCOPE_SUBTREE:
933 if (*norm_basedn != '\0') {
934 query = lsqlite3_tprintf(lsql_ac,
935 "SELECT entry.eid,\n"
936 " entry.dn,\n"
937 " av.attr_name,\n"
938 " av.attr_value\n"
939 " FROM ldb_entry AS entry\n"
941 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
942 " ON av.eid = entry.eid\n"
944 " WHERE entry.eid IN\n"
945 " (SELECT DISTINCT ldb_entry.eid\n"
946 " FROM ldb_entry\n"
947 " WHERE (ldb_entry.norm_dn GLOB('*,%q')\n"
948 " OR ldb_entry.norm_dn = '%q')\n"
949 " AND ldb_entry.eid IN\n"
950 " (%s)\n"
951 " )\n"
953 " ORDER BY entry.eid ASC;",
954 norm_basedn,
955 norm_basedn,
956 sqlfilter);
957 } else {
958 query = lsqlite3_tprintf(lsql_ac,
959 "SELECT entry.eid,\n"
960 " entry.dn,\n"
961 " av.attr_name,\n"
962 " av.attr_value\n"
963 " FROM ldb_entry AS entry\n"
965 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
966 " ON av.eid = entry.eid\n"
968 " WHERE entry.eid IN\n"
969 " (SELECT DISTINCT ldb_entry.eid\n"
970 " FROM ldb_entry\n"
971 " WHERE ldb_entry.eid IN\n"
972 " (%s)\n"
973 " )\n"
975 " ORDER BY entry.eid ASC;",
976 sqlfilter);
979 break;
981 case LDB_SCOPE_BASE:
982 query = lsqlite3_tprintf(lsql_ac,
983 "SELECT entry.eid,\n"
984 " entry.dn,\n"
985 " av.attr_name,\n"
986 " av.attr_value\n"
987 " FROM ldb_entry AS entry\n"
989 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
990 " ON av.eid = entry.eid\n"
992 " WHERE entry.eid IN\n"
993 " (SELECT DISTINCT ldb_entry.eid\n"
994 " FROM ldb_entry\n"
995 " WHERE ldb_entry.norm_dn = '%q'\n"
996 " AND ldb_entry.eid IN\n"
997 " (%s)\n"
998 " )\n"
1000 " ORDER BY entry.eid ASC;",
1001 norm_basedn,
1002 sqlfilter);
1003 break;
1005 case LDB_SCOPE_ONELEVEL:
1006 query = lsqlite3_tprintf(lsql_ac,
1007 "SELECT entry.eid,\n"
1008 " entry.dn,\n"
1009 " av.attr_name,\n"
1010 " av.attr_value\n"
1011 " FROM ldb_entry AS entry\n"
1013 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
1014 " ON av.eid = entry.eid\n"
1016 " WHERE entry.eid IN\n"
1017 " (SELECT DISTINCT ldb_entry.eid\n"
1018 " FROM ldb_entry\n"
1019 " WHERE norm_dn GLOB('*,%q')\n"
1020 " AND NOT norm_dn GLOB('*,*,%q')\n"
1021 " AND ldb_entry.eid IN\n(%s)\n"
1022 " )\n"
1024 " ORDER BY entry.eid ASC;",
1025 norm_basedn,
1026 norm_basedn,
1027 sqlfilter);
1028 break;
1031 if (query == NULL) {
1032 goto failed;
1035 /* * /
1036 printf ("%s\n", query);
1037 / * */
1039 lsql_ac->current_eid = 0;
1040 lsql_ac->attrs = attrs;
1041 lsql_ac->ares = NULL;
1043 (*handle)->state = LDB_ASYNC_PENDING;
1045 ret = sqlite3_exec(lsqlite3->sqlite, query, lsqlite3_search_callback, *handle, &errmsg);
1046 if (ret != SQLITE_OK) {
1047 if (errmsg) {
1048 ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1049 free(errmsg);
1051 goto failed;
1054 /* complete the last message if any */
1055 if (lsql_ac->ares) {
1056 lsql_ac->ares->message = ldb_msg_canonicalize(module->ldb, lsql_ac->ares->message);
1057 if (lsql_ac->ares->message == NULL)
1058 goto failed;
1060 (*handle)->status = lsql_ac->callback(module->ldb, lsql_ac->context, lsql_ac->ares);
1061 if ((*handle)->status != LDB_SUCCESS)
1062 goto failed;
1065 (*handle)->state = LDB_ASYNC_DONE;
1067 return LDB_SUCCESS;
1069 failed:
1070 talloc_free(*handle);
1071 return LDB_ERR_OPERATIONS_ERROR;
1074 static int lsql_search_bytree(struct ldb_module * module, const struct ldb_dn* base,
1075 enum ldb_scope scope, struct ldb_parse_tree * tree,
1076 const char * const * attrs, struct ldb_result ** res)
1078 struct ldb_async_handle *handle;
1079 int ret;
1081 *res = talloc_zero(module, struct ldb_result);
1082 if (! *res) {
1083 return LDB_ERR_OPERATIONS_ERROR;
1086 ret = lsql_search_async(module, base, scope, tree, attrs,
1087 res, &lsql_search_sync_callback,
1088 &handle);
1090 if (ret == LDB_SUCCESS) {
1091 ret = ldb_async_wait(handle, LDB_WAIT_ALL);
1092 talloc_free(handle);
1095 if (ret != LDB_SUCCESS) {
1096 talloc_free(*res);
1099 return ret;
1102 /* add a record */
1103 static int lsql_add_async(struct ldb_module *module, struct ldb_message *msg,
1104 void *context,
1105 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
1106 struct ldb_async_handle **handle)
1108 struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1109 struct lsql_async_context *lsql_ac;
1110 long long eid;
1111 char *dn, *ndn;
1112 char *errmsg;
1113 char *query;
1114 int i;
1115 int ret = LDB_ERR_OPERATIONS_ERROR;
1117 *handle = init_handle(lsqlite3, module, context, callback);
1118 if (*handle == NULL) {
1119 goto failed;
1121 lsql_ac = talloc_get_type((*handle)->private_data, struct lsql_async_context);
1122 (*handle)->state = LDB_ASYNC_DONE;
1123 (*handle)->status = LDB_SUCCESS;
1125 /* See if this is an ltdb special */
1126 if (ldb_dn_is_special(msg->dn)) {
1127 struct ldb_dn *c;
1129 c = ldb_dn_explode(lsql_ac, "@SUBCLASSES");
1130 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
1131 #warning "insert subclasses into object class tree"
1132 ret = LDB_ERR_UNWILLING_TO_PERFORM;
1133 goto failed;
1137 c = ldb_dn_explode(local_ctx, "@INDEXLIST");
1138 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
1139 #warning "should we handle indexes somehow ?"
1140 goto failed;
1143 /* Others are implicitly ignored */
1144 return LDB_SUCCESS;
1147 /* create linearized and normalized dns */
1148 dn = ldb_dn_linearize(lsql_ac, msg->dn);
1149 ndn = ldb_dn_linearize(lsql_ac, ldb_dn_casefold(module->ldb, msg->dn));
1150 if (dn == NULL || ndn == NULL) {
1151 ret = LDB_ERR_OTHER;
1152 goto failed;
1155 query = lsqlite3_tprintf(lsql_ac,
1156 /* Add new entry */
1157 "INSERT OR ABORT INTO ldb_entry "
1158 "('dn', 'norm_dn') "
1159 "VALUES ('%q', '%q');",
1160 dn, ndn);
1161 if (query == NULL) {
1162 ret = LDB_ERR_OTHER;
1163 goto failed;
1166 ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1167 if (ret != SQLITE_OK) {
1168 if (errmsg) {
1169 ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1170 free(errmsg);
1172 ret = LDB_ERR_OTHER;
1173 goto failed;
1176 eid = lsqlite3_get_eid_ndn(lsqlite3->sqlite, lsql_ac, ndn);
1177 if (eid == -1) {
1178 ret = LDB_ERR_OTHER;
1179 goto failed;
1182 for (i = 0; i < msg->num_elements; i++) {
1183 const struct ldb_message_element *el = &msg->elements[i];
1184 const struct ldb_attrib_handler *h;
1185 char *attr;
1186 int j;
1188 /* Get a case-folded copy of the attribute name */
1189 attr = ldb_attr_casefold(lsql_ac, el->name);
1190 if (attr == NULL) {
1191 ret = LDB_ERR_OTHER;
1192 goto failed;
1195 h = ldb_attrib_handler(module->ldb, el->name);
1197 /* For each value of the specified attribute name... */
1198 for (j = 0; j < el->num_values; j++) {
1199 struct ldb_val value;
1200 char *insert;
1202 /* Get a canonicalised copy of the data */
1203 h->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
1204 if (value.data == NULL) {
1205 ret = LDB_ERR_OTHER;
1206 goto failed;
1209 insert = lsqlite3_tprintf(lsql_ac,
1210 "INSERT OR ROLLBACK INTO ldb_attribute_values "
1211 "('eid', 'attr_name', 'norm_attr_name',"
1212 " 'attr_value', 'norm_attr_value') "
1213 "VALUES ('%lld', '%q', '%q', '%q', '%q');",
1214 eid, el->name, attr,
1215 el->values[j].data, value.data);
1216 if (insert == NULL) {
1217 ret = LDB_ERR_OTHER;
1218 goto failed;
1221 ret = sqlite3_exec(lsqlite3->sqlite, insert, NULL, NULL, &errmsg);
1222 if (ret != SQLITE_OK) {
1223 if (errmsg) {
1224 ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1225 free(errmsg);
1227 ret = LDB_ERR_OTHER;
1228 goto failed;
1233 if (lsql_ac->callback)
1234 (*handle)->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1236 return LDB_SUCCESS;
1238 failed:
1239 talloc_free(*handle);
1240 return ret;
1243 static int lsql_add(struct ldb_module *module, const struct ldb_message *msg)
1245 struct ldb_async_handle *handle;
1246 int ret;
1248 ret = lsql_add_async(module, msg, NULL, NULL, &handle);
1250 if (ret != LDB_SUCCESS)
1251 return ret;
1253 ret = ldb_async_wait(handle, LDB_WAIT_ALL);
1255 talloc_free(handle);
1256 return ret;
1260 /* modify a record */
1261 static int lsql_modify_async(struct ldb_module *module, const struct ldb_message *msg,
1262 void *context,
1263 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
1264 struct ldb_async_handle **handle)
1266 struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1267 struct lsql_async_context *lsql_ac;
1268 long long eid;
1269 char *errmsg;
1270 int i;
1271 int ret = LDB_ERR_OPERATIONS_ERROR;
1273 *handle = init_handle(lsqlite3, module, context, callback);
1274 if (*handle == NULL) {
1275 goto failed;
1277 lsql_ac = talloc_get_type((*handle)->private_data, struct lsql_async_context);
1278 (*handle)->state = LDB_ASYNC_DONE;
1279 (*handle)->status = LDB_SUCCESS;
1281 /* See if this is an ltdb special */
1282 if (ldb_dn_is_special(msg->dn)) {
1283 struct ldb_dn *c;
1285 c = ldb_dn_explode(lsql_ac, "@SUBCLASSES");
1286 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
1287 #warning "modify subclasses into object class tree"
1288 ret = LDB_ERR_UNWILLING_TO_PERFORM;
1289 goto failed;
1292 /* Others are implicitly ignored */
1293 return LDB_SUCCESS;
1296 eid = lsqlite3_get_eid(module, msg->dn);
1297 if (eid == -1) {
1298 ret = LDB_ERR_OTHER;
1299 goto failed;
1302 for (i = 0; i < msg->num_elements; i++) {
1303 const struct ldb_message_element *el = &msg->elements[i];
1304 const struct ldb_attrib_handler *h;
1305 int flags = el->flags & LDB_FLAG_MOD_MASK;
1306 char *attr;
1307 char *mod;
1308 int j;
1310 /* Get a case-folded copy of the attribute name */
1311 attr = ldb_attr_casefold(lsql_ac, el->name);
1312 if (attr == NULL) {
1313 ret = LDB_ERR_OTHER;
1314 goto failed;
1317 h = ldb_attrib_handler(module->ldb, el->name);
1319 switch (flags) {
1321 case LDB_FLAG_MOD_REPLACE:
1323 /* remove all attributes before adding the replacements */
1324 mod = lsqlite3_tprintf(lsql_ac,
1325 "DELETE FROM ldb_attribute_values "
1326 "WHERE eid = '%lld' "
1327 "AND norm_attr_name = '%q';",
1328 eid, attr);
1329 if (mod == NULL) {
1330 ret = LDB_ERR_OTHER;
1331 goto failed;
1334 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1335 if (ret != SQLITE_OK) {
1336 if (errmsg) {
1337 ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1338 free(errmsg);
1340 ret = LDB_ERR_OTHER;
1341 goto failed;
1344 /* MISSING break is INTENTIONAL */
1346 case LDB_FLAG_MOD_ADD:
1347 #warning "We should throw an error if no value is provided!"
1348 /* For each value of the specified attribute name... */
1349 for (j = 0; j < el->num_values; j++) {
1350 struct ldb_val value;
1352 /* Get a canonicalised copy of the data */
1353 h->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
1354 if (value.data == NULL) {
1355 ret = LDB_ERR_OTHER;
1356 goto failed;
1359 mod = lsqlite3_tprintf(lsql_ac,
1360 "INSERT OR ROLLBACK INTO ldb_attribute_values "
1361 "('eid', 'attr_name', 'norm_attr_name',"
1362 " 'attr_value', 'norm_attr_value') "
1363 "VALUES ('%lld', '%q', '%q', '%q', '%q');",
1364 eid, el->name, attr,
1365 el->values[j].data, value.data);
1367 if (mod == NULL) {
1368 ret = LDB_ERR_OTHER;
1369 goto failed;
1372 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1373 if (ret != SQLITE_OK) {
1374 if (errmsg) {
1375 ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1376 free(errmsg);
1378 ret = LDB_ERR_OTHER;
1379 goto failed;
1383 break;
1385 case LDB_FLAG_MOD_DELETE:
1386 #warning "We should throw an error if the attribute we are trying to delete does not exist!"
1387 if (el->num_values == 0) {
1388 mod = lsqlite3_tprintf(lsql_ac,
1389 "DELETE FROM ldb_attribute_values "
1390 "WHERE eid = '%lld' "
1391 "AND norm_attr_name = '%q';",
1392 eid, attr);
1393 if (mod == NULL) {
1394 ret = LDB_ERR_OTHER;
1395 goto failed;
1398 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1399 if (ret != SQLITE_OK) {
1400 if (errmsg) {
1401 ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1402 free(errmsg);
1404 ret = LDB_ERR_OTHER;
1405 goto failed;
1409 /* For each value of the specified attribute name... */
1410 for (j = 0; j < el->num_values; j++) {
1411 struct ldb_val value;
1413 /* Get a canonicalised copy of the data */
1414 h->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
1415 if (value.data == NULL) {
1416 ret = LDB_ERR_OTHER;
1417 goto failed;
1420 mod = lsqlite3_tprintf(lsql_ac,
1421 "DELETE FROM ldb_attribute_values "
1422 "WHERE eid = '%lld' "
1423 "AND norm_attr_name = '%q' "
1424 "AND norm_attr_value = '%q';",
1425 eid, attr, value.data);
1427 if (mod == NULL) {
1428 ret = LDB_ERR_OTHER;
1429 goto failed;
1432 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1433 if (ret != SQLITE_OK) {
1434 if (errmsg) {
1435 ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1436 free(errmsg);
1438 ret = LDB_ERR_OTHER;
1439 goto failed;
1443 break;
1447 if (lsql_ac->callback)
1448 (*handle)->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1450 return LDB_SUCCESS;
1452 failed:
1453 talloc_free(*handle);
1454 return ret;
1457 static int lsql_modify(struct ldb_module *module, const struct ldb_message *msg)
1459 struct ldb_async_handle *handle;
1460 int ret;
1462 ret = lsql_modify_async(module, msg, NULL, NULL, &handle);
1464 if (ret != LDB_SUCCESS)
1465 return ret;
1467 ret = ldb_async_wait(handle, LDB_WAIT_ALL);
1469 talloc_free(handle);
1470 return ret;
1473 /* delete a record */
1474 static int lsql_delete_async(struct ldb_module *module, const struct ldb_dn *dn,
1475 void *context,
1476 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
1477 struct ldb_async_handle **handle)
1479 struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1480 struct lsql_async_context *lsql_ac;
1481 long long eid;
1482 char *errmsg;
1483 char *query;
1484 int ret = LDB_ERR_OPERATIONS_ERROR;
1487 *handle = init_handle(lsqlite3, module, context, callback);
1488 if (*handle == NULL) {
1489 goto failed;
1491 lsql_ac = talloc_get_type((*handle)->private_data, struct lsql_async_context);
1492 (*handle)->state = LDB_ASYNC_DONE;
1493 (*handle)->status = LDB_SUCCESS;
1495 eid = lsqlite3_get_eid(module, dn);
1496 if (eid == -1) {
1497 goto failed;
1500 query = lsqlite3_tprintf(lsql_ac,
1501 /* Delete entry */
1502 "DELETE FROM ldb_entry WHERE eid = %lld; "
1503 /* Delete attributes */
1504 "DELETE FROM ldb_attribute_values WHERE eid = %lld; ",
1505 eid, eid);
1506 if (query == NULL) {
1507 ret = LDB_ERR_OTHER;
1508 goto failed;
1511 ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1512 if (ret != SQLITE_OK) {
1513 if (errmsg) {
1514 ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1515 free(errmsg);
1517 ret = LDB_ERR_OPERATIONS_ERROR;
1518 goto failed;
1521 if (lsql_ac->callback)
1522 (*handle)->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1524 return LDB_SUCCESS;
1526 failed:
1527 talloc_free(*handle);
1528 return ret;
1531 static int lsql_delete(struct ldb_module *module, const struct ldb_dn *dn)
1533 struct ldb_async_handle *handle;
1534 int ret;
1536 /* ignore ltdb specials */
1537 if (ldb_dn_is_special(dn)) {
1538 return LDB_SUCCESS;
1541 ret = lsql_delete_async(module, dn, NULL, NULL, &handle);
1543 if (ret != LDB_SUCCESS)
1544 return ret;
1546 ret = ldb_async_wait(handle, LDB_WAIT_ALL);
1548 talloc_free(handle);
1549 return ret;
1552 /* rename a record */
1553 static int lsql_rename_async(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn,
1554 void *context,
1555 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
1556 struct ldb_async_handle **handle)
1558 struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1559 struct lsql_async_context *lsql_ac;
1560 char *new_dn, *new_cdn, *old_cdn;
1561 char *errmsg;
1562 char *query;
1563 int ret = LDB_ERR_OPERATIONS_ERROR;
1565 *handle = init_handle(lsqlite3, module, context, callback);
1566 if (*handle == NULL) {
1567 goto failed;
1569 lsql_ac = talloc_get_type((*handle)->private_data, struct lsql_async_context);
1570 (*handle)->state = LDB_ASYNC_DONE;
1571 (*handle)->status = LDB_SUCCESS;
1573 /* create linearized and normalized dns */
1574 old_cdn = ldb_dn_linearize(lsql_ac, ldb_dn_casefold(module->ldb, olddn));
1575 new_cdn = ldb_dn_linearize(lsql_ac, ldb_dn_casefold(module->ldb, newdn));
1576 new_dn = ldb_dn_linearize(lsql_ac, newdn);
1577 if (old_cdn == NULL || new_cdn == NULL || new_dn == NULL) {
1578 goto failed;
1581 /* build the SQL query */
1582 query = lsqlite3_tprintf(lsql_ac,
1583 "UPDATE ldb_entry SET dn = '%q', norm_dn = '%q' "
1584 "WHERE norm_dn = '%q';",
1585 new_dn, new_cdn, old_cdn);
1586 if (query == NULL) {
1587 goto failed;
1590 /* execute */
1591 ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1592 if (ret != SQLITE_OK) {
1593 if (errmsg) {
1594 ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1595 free(errmsg);
1597 ret = LDB_ERR_OPERATIONS_ERROR;
1598 goto failed;
1601 if (lsql_ac->callback)
1602 (*handle)->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1604 return LDB_SUCCESS;
1606 failed:
1607 talloc_free(*handle);
1608 return ret;
1611 static int lsql_rename(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
1613 struct ldb_async_handle *handle;
1614 int ret;
1616 /* ignore ltdb specials */
1617 if (ldb_dn_is_special(olddn) || ldb_dn_is_special(newdn)) {
1618 return LDB_SUCCESS;
1622 ret = lsql_rename_async(module, olddn, newdn, NULL, NULL, &handle);
1624 if (ret != LDB_SUCCESS)
1625 return ret;
1627 ret = ldb_async_wait(handle, LDB_WAIT_ALL);
1629 talloc_free(handle);
1630 return ret;
1633 static int lsql_start_trans(struct ldb_module * module)
1635 int ret;
1636 char *errmsg;
1637 struct lsqlite3_private * lsqlite3 = module->private_data;
1639 if (lsqlite3->trans_count == 0) {
1640 ret = sqlite3_exec(lsqlite3->sqlite, "BEGIN IMMEDIATE;", NULL, NULL, &errmsg);
1641 if (ret != SQLITE_OK) {
1642 if (errmsg) {
1643 printf("lsqlite3_start_trans: error: %s\n", errmsg);
1644 free(errmsg);
1646 return -1;
1650 lsqlite3->trans_count++;
1652 return 0;
1655 static int lsql_end_trans(struct ldb_module *module)
1657 int ret;
1658 char *errmsg;
1659 struct lsqlite3_private *lsqlite3 = module->private_data;
1661 if (lsqlite3->trans_count > 0) {
1662 lsqlite3->trans_count--;
1663 } else return -1;
1665 if (lsqlite3->trans_count == 0) {
1666 ret = sqlite3_exec(lsqlite3->sqlite, "COMMIT;", NULL, NULL, &errmsg);
1667 if (ret != SQLITE_OK) {
1668 if (errmsg) {
1669 printf("lsqlite3_end_trans: error: %s\n", errmsg);
1670 free(errmsg);
1672 return -1;
1676 return 0;
1679 static int lsql_del_trans(struct ldb_module *module)
1681 struct lsqlite3_private *lsqlite3 = module->private_data;
1683 if (lsqlite3->trans_count > 0) {
1684 lsqlite3->trans_count--;
1685 } else return -1;
1687 if (lsqlite3->trans_count == 0) {
1688 return lsqlite3_safe_rollback(lsqlite3->sqlite);
1691 return -1;
1695 * Static functions
1698 static int initialize(struct lsqlite3_private *lsqlite3,
1699 struct ldb_context *ldb, const char *url, int flags)
1701 TALLOC_CTX *local_ctx;
1702 long long queryInt;
1703 int rollback = 0;
1704 char *errmsg;
1705 char *schema;
1706 int ret;
1708 /* create a local ctx */
1709 local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_rename local context");
1710 if (local_ctx == NULL) {
1711 return -1;
1714 schema = lsqlite3_tprintf(local_ctx,
1717 "CREATE TABLE ldb_info AS "
1718 " SELECT 'LDB' AS database_type,"
1719 " '1.0' AS version;"
1722 * The entry table holds the information about an entry.
1723 * This table is used to obtain the EID of the entry and to
1724 * support scope=one and scope=base. The parent and child
1725 * table is included in the entry table since all the other
1726 * attributes are dependent on EID.
1728 "CREATE TABLE ldb_entry "
1730 " eid INTEGER PRIMARY KEY AUTOINCREMENT,"
1731 " dn TEXT UNIQUE NOT NULL,"
1732 " norm_dn TEXT UNIQUE NOT NULL"
1733 ");"
1736 "CREATE TABLE ldb_object_classes"
1738 " class_name TEXT PRIMARY KEY,"
1739 " parent_class_name TEXT,"
1740 " tree_key TEXT UNIQUE,"
1741 " max_child_num INTEGER DEFAULT 0"
1742 ");"
1745 * We keep a full listing of attribute/value pairs here
1747 "CREATE TABLE ldb_attribute_values"
1749 " eid INTEGER REFERENCES ldb_entry,"
1750 " attr_name TEXT,"
1751 " norm_attr_name TEXT,"
1752 " attr_value TEXT,"
1753 " norm_attr_value TEXT "
1754 ");"
1758 * Indexes
1760 "CREATE INDEX ldb_attribute_values_eid_idx "
1761 " ON ldb_attribute_values (eid);"
1763 "CREATE INDEX ldb_attribute_values_name_value_idx "
1764 " ON ldb_attribute_values (attr_name, norm_attr_value);"
1769 * Triggers
1772 "CREATE TRIGGER ldb_object_classes_insert_tr"
1773 " AFTER INSERT"
1774 " ON ldb_object_classes"
1775 " FOR EACH ROW"
1776 " BEGIN"
1777 " UPDATE ldb_object_classes"
1778 " SET tree_key = COALESCE(tree_key, "
1779 " ("
1780 " SELECT tree_key || "
1781 " (SELECT base160(max_child_num + 1)"
1782 " FROM ldb_object_classes"
1783 " WHERE class_name = "
1784 " new.parent_class_name)"
1785 " FROM ldb_object_classes "
1786 " WHERE class_name = new.parent_class_name "
1787 " ));"
1788 " UPDATE ldb_object_classes "
1789 " SET max_child_num = max_child_num + 1"
1790 " WHERE class_name = new.parent_class_name;"
1791 " END;"
1794 * Table initialization
1797 "INSERT INTO ldb_object_classes "
1798 " (class_name, tree_key) "
1799 " VALUES "
1800 " ('TOP', '0001');");
1802 /* Skip protocol indicator of url */
1803 if (strncmp(url, "sqlite://", 9) != 0) {
1804 return SQLITE_MISUSE;
1807 /* Update pointer to just after the protocol indicator */
1808 url += 9;
1810 /* Try to open the (possibly empty/non-existent) database */
1811 if ((ret = sqlite3_open(url, &lsqlite3->sqlite)) != SQLITE_OK) {
1812 return ret;
1815 /* In case this is a new database, enable auto_vacuum */
1816 ret = sqlite3_exec(lsqlite3->sqlite, "PRAGMA auto_vacuum = 1;", NULL, NULL, &errmsg);
1817 if (ret != SQLITE_OK) {
1818 if (errmsg) {
1819 printf("lsqlite3 initializaion error: %s\n", errmsg);
1820 free(errmsg);
1822 goto failed;
1825 if (flags & LDB_FLG_NOSYNC) {
1826 /* DANGEROUS */
1827 ret = sqlite3_exec(lsqlite3->sqlite, "PRAGMA synchronous = OFF;", NULL, NULL, &errmsg);
1828 if (ret != SQLITE_OK) {
1829 if (errmsg) {
1830 printf("lsqlite3 initializaion error: %s\n", errmsg);
1831 free(errmsg);
1833 goto failed;
1837 /* */
1839 /* Establish a busy timeout of 30 seconds */
1840 if ((ret = sqlite3_busy_timeout(lsqlite3->sqlite,
1841 30000)) != SQLITE_OK) {
1842 return ret;
1845 /* Create a function, callable from sql, to increment a tree_key */
1846 if ((ret =
1847 sqlite3_create_function(lsqlite3->sqlite,/* handle */
1848 "base160_next", /* function name */
1849 1, /* number of args */
1850 SQLITE_ANY, /* preferred text type */
1851 NULL, /* user data */
1852 base160next_sql, /* called func */
1853 NULL, /* step func */
1854 NULL /* final func */
1855 )) != SQLITE_OK) {
1856 return ret;
1859 /* Create a function, callable from sql, to convert int to base160 */
1860 if ((ret =
1861 sqlite3_create_function(lsqlite3->sqlite,/* handle */
1862 "base160", /* function name */
1863 1, /* number of args */
1864 SQLITE_ANY, /* preferred text type */
1865 NULL, /* user data */
1866 base160_sql, /* called func */
1867 NULL, /* step func */
1868 NULL /* final func */
1869 )) != SQLITE_OK) {
1870 return ret;
1873 /* Create a function, callable from sql, to perform various comparisons */
1874 if ((ret =
1875 sqlite3_create_function(lsqlite3->sqlite, /* handle */
1876 "ldap_compare", /* function name */
1877 4, /* number of args */
1878 SQLITE_ANY, /* preferred text type */
1879 ldb , /* user data */
1880 lsqlite3_compare, /* called func */
1881 NULL, /* step func */
1882 NULL /* final func */
1883 )) != SQLITE_OK) {
1884 return ret;
1887 /* Begin a transaction */
1888 ret = sqlite3_exec(lsqlite3->sqlite, "BEGIN EXCLUSIVE;", NULL, NULL, &errmsg);
1889 if (ret != SQLITE_OK) {
1890 if (errmsg) {
1891 printf("lsqlite3: initialization error: %s\n", errmsg);
1892 free(errmsg);
1894 goto failed;
1896 rollback = 1;
1898 /* Determine if this is a new database. No tables means it is. */
1899 if (query_int(lsqlite3,
1900 &queryInt,
1901 "SELECT COUNT(*)\n"
1902 " FROM sqlite_master\n"
1903 " WHERE type = 'table';") != 0) {
1904 goto failed;
1907 if (queryInt == 0) {
1909 * Create the database schema
1911 ret = sqlite3_exec(lsqlite3->sqlite, schema, NULL, NULL, &errmsg);
1912 if (ret != SQLITE_OK) {
1913 if (errmsg) {
1914 printf("lsqlite3 initializaion error: %s\n", errmsg);
1915 free(errmsg);
1917 goto failed;
1919 } else {
1921 * Ensure that the database we opened is one of ours
1923 if (query_int(lsqlite3,
1924 &queryInt,
1925 "SELECT "
1926 " (SELECT COUNT(*) = 2"
1927 " FROM sqlite_master "
1928 " WHERE type = 'table' "
1929 " AND name IN "
1930 " ("
1931 " 'ldb_entry', "
1932 " 'ldb_object_classes' "
1933 " ) "
1934 " ) "
1935 " AND "
1936 " (SELECT 1 "
1937 " FROM ldb_info "
1938 " WHERE database_type = 'LDB' "
1939 " AND version = '1.0'"
1940 " );") != 0 ||
1941 queryInt != 1) {
1943 /* It's not one that we created. See ya! */
1944 goto failed;
1948 /* Commit the transaction */
1949 ret = sqlite3_exec(lsqlite3->sqlite, "COMMIT;", NULL, NULL, &errmsg);
1950 if (ret != SQLITE_OK) {
1951 if (errmsg) {
1952 printf("lsqlite3: iniialization error: %s\n", errmsg);
1953 free(errmsg);
1955 goto failed;
1958 return SQLITE_OK;
1960 failed:
1961 if (rollback) lsqlite3_safe_rollback(lsqlite3->sqlite);
1962 sqlite3_close(lsqlite3->sqlite);
1963 return -1;
1966 static int destructor(struct lsqlite3_private *lsqlite3)
1968 if (lsqlite3->sqlite) {
1969 sqlite3_close(lsqlite3->sqlite);
1971 return 0;
1974 static int lsql_async_wait(struct ldb_async_handle *handle, enum ldb_async_wait_type type)
1976 return handle->status;
1979 static int lsql_request(struct ldb_module *module, struct ldb_request *req)
1981 /* check for oustanding critical controls and return an error if found */
1983 if (req->controls != NULL) {
1984 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "Controls should not reach the ldb_sqlite3 backend!\n");
1987 if (check_critical_controls(req->controls)) {
1988 return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
1991 switch (req->operation) {
1993 case LDB_SEARCH:
1994 return lsql_search_bytree(module,
1995 req->op.search.base,
1996 req->op.search.scope,
1997 req->op.search.tree,
1998 req->op.search.attrs,
1999 &req->op.search.res);
2001 case LDB_ADD:
2002 return lsql_add(module, req->op.add.message);
2004 case LDB_MODIFY:
2005 return lsql_modify(module, req->op.mod.message);
2007 case LDB_DELETE:
2008 return lsql_delete(module, req->op.del.dn);
2010 case LDB_RENAME:
2011 return lsql_rename(module,
2012 req->op.rename.olddn,
2013 req->op.rename.newdn);
2015 case LDB_SEARCH:
2016 return lsql_search_async(module,
2017 req->op.search.base,
2018 req->op.search.scope,
2019 req->op.search.tree,
2020 req->op.search.attrs,
2021 req->async.context,
2022 req->async.callback,
2023 &req->async.handle);
2025 case LDB_ADD:
2026 return lsql_add_async(module,
2027 req->op.add.message,
2028 req->async.context,
2029 req->async.callback,
2030 &req->async.handle);
2032 case LDB_MODIFY:
2033 return lsql_modify_async(module,
2034 req->op.mod.message,
2035 req->async.context,
2036 req->async.callback,
2037 &req->async.handle);
2039 case LDB_DELETE:
2040 return lsql_delete_async(module,
2041 req->op.del.dn,
2042 req->async.context,
2043 req->async.callback,
2044 &req->async.handle);
2046 case LDB_RENAME:
2047 return lsql_rename_async(module,
2048 req->op.rename.olddn,
2049 req->op.rename.newdn,
2050 req->async.context,
2051 req->async.callback,
2052 &req->async.handle);
2054 default:
2055 return LDB_ERR_OPERATIONS_ERROR;
2061 * Table of operations for the sqlite3 backend
2063 static const struct ldb_module_ops lsqlite3_ops = {
2064 .name = "sqlite",
2065 .request = lsql_request,
2066 .start_transaction = lsql_start_trans,
2067 .end_transaction = lsql_end_trans,
2068 .del_transaction = lsql_del_trans,
2069 .async_wait = lsql_async_wait,
2073 * connect to the database
2075 static int lsqlite3_connect(struct ldb_context *ldb,
2076 const char *url,
2077 unsigned int flags,
2078 const char *options[],
2079 struct ldb_module **module)
2081 int i;
2082 int ret;
2083 struct lsqlite3_private * lsqlite3 = NULL;
2085 lsqlite3 = talloc(ldb, struct lsqlite3_private);
2086 if (!lsqlite3) {
2087 goto failed;
2090 lsqlite3->sqlite = NULL;
2091 lsqlite3->options = NULL;
2092 lsqlite3->trans_count = 0;
2094 ret = initialize(lsqlite3, ldb, url, flags);
2095 if (ret != SQLITE_OK) {
2096 goto failed;
2099 talloc_set_destructor(lsqlite3, destructor);
2103 *module = talloc(ldb, struct ldb_module);
2104 if (!module) {
2105 ldb_oom(ldb);
2106 goto failed;
2108 (*module)->ldb = ldb;
2109 (*module)->prev = (*module)->next = NULL;
2110 (*module)->private_data = lsqlite3;
2111 (*module)->ops = &lsqlite3_ops;
2113 if (options) {
2115 * take a copy of the options array, so we don't have to rely
2116 * on the caller keeping it around (it might be dynamic)
2118 for (i=0;options[i];i++) ;
2120 lsqlite3->options = talloc_array(lsqlite3, char *, i+1);
2121 if (!lsqlite3->options) {
2122 goto failed;
2125 for (i=0;options[i];i++) {
2127 lsqlite3->options[i+1] = NULL;
2128 lsqlite3->options[i] =
2129 talloc_strdup(lsqlite3->options, options[i]);
2130 if (!lsqlite3->options[i]) {
2131 goto failed;
2136 return 0;
2138 failed:
2139 if (lsqlite3->sqlite != NULL) {
2140 (void) sqlite3_close(lsqlite3->sqlite);
2142 talloc_free(lsqlite3);
2143 return -1;
2146 int ldb_sqlite3_init(void)
2148 return ldb_register_backend("sqlite3", lsqlite3_connect);