4 Copyright (C) Derrell Lipman 2005
5 Copyright (C) Simo Sorce 2005-2006
7 ** NOTE! The following LGPL license applies to the ldb
8 ** library. This does NOT imply that all of Samba is released
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
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 "ldb_includes.h"
40 struct lsqlite3_private {
47 struct ldb_module *module;
50 long long current_eid;
51 const char * const * attrs;
52 struct ldb_reply *ares;
56 int (*callback)(struct ldb_context *, void *, struct ldb_reply *);
59 static struct ldb_handle *init_handle(struct lsqlite3_private *lsqlite3,
60 struct ldb_module *module,
61 struct ldb_request *req)
63 struct lsql_context *ac;
66 h = talloc_zero(lsqlite3, struct ldb_handle);
68 ldb_set_errstring(module->ldb, "Out of Memory");
74 ac = talloc(h, struct lsql_context);
76 ldb_set_errstring(module->ldb, "Out of Memory");
81 h->private_data = (void *)ac;
83 h->state = LDB_ASYNC_INIT;
84 h->status = LDB_SUCCESS;
87 ac->context = req->context;
88 ac->callback = req->callback;
94 * Macros used throughout
99 # define TRUE (! FALSE)
102 #define RESULT_ATTR_TABLE "temp_result_attrs"
104 //#define TEMPTAB /* for testing, create non-temporary table */
105 #define TEMPTAB "TEMPORARY"
110 sqlite3_stmt * stmtGetEID = NULL;
112 static char *lsqlite3_tprintf(TALLOC_CTX *mem_ctx, const char *fmt, ...)
118 str = sqlite3_vmprintf(fmt, ap);
121 if (str == NULL) return NULL;
123 ret = talloc_strdup(mem_ctx, str);
133 static char base160tab[161] = {
134 48 ,49 ,50 ,51 ,52 ,53 ,54 ,55 ,56 ,57 , /* 0-9 */
135 58 ,59 ,65 ,66 ,67 ,68 ,69 ,70 ,71 ,72 , /* : ; A-H */
136 73 ,74 ,75 ,76 ,77 ,78 ,79 ,80 ,81 ,82 , /* I-R */
137 83 ,84 ,85 ,86 ,87 ,88 ,89 ,90 ,97 ,98 , /* S-Z , a-b */
138 99 ,100,101,102,103,104,105,106,107,108, /* c-l */
139 109,110,111,112,113,114,115,116,117,118, /* m-v */
140 119,120,121,122,160,161,162,163,164,165, /* w-z, latin1 */
141 166,167,168,169,170,171,172,173,174,175, /* latin1 */
142 176,177,178,179,180,181,182,183,184,185, /* latin1 */
143 186,187,188,189,190,191,192,193,194,195, /* latin1 */
144 196,197,198,199,200,201,202,203,204,205, /* latin1 */
145 206,207,208,209,210,211,212,213,214,215, /* latin1 */
146 216,217,218,219,220,221,222,223,224,225, /* latin1 */
147 226,227,228,229,230,231,232,233,234,235, /* latin1 */
148 236,237,238,239,240,241,242,243,244,245, /* latin1 */
149 246,247,248,249,250,251,252,253,254,255, /* latin1 */
157 * Convert an unsigned long integer into a base160 representation of the
162 * value to be converted
165 * character array, 5 bytes long, into which the base160 representation
166 * will be placed. The result will be a four-digit representation of the
167 * number (with leading zeros prepended as necessary), and null
174 base160_sql(sqlite3_context * hContext,
176 sqlite3_value ** argv)
182 val = sqlite3_value_int64(argv[0]);
184 for (i = 3; i >= 0; i--) {
186 result[i] = base160tab[val % 160];
192 sqlite3_result_text(hContext, result, -1, SQLITE_TRANSIENT);
199 * This function enhances sqlite by adding a "base160_next()" function which is
200 * accessible via queries.
202 * Retrieve the next-greater number in the base160 sequence for the terminal
203 * tree node (the last four digits). Only one tree level (four digits) is
207 * A character string: either an empty string (in which case no operation is
208 * performed), or a string of base160 digits with a length of a multiple of
212 * Upon return, the trailing four digits (one tree level) will have been
216 base160next_sql(sqlite3_context * hContext,
218 sqlite3_value ** argv)
223 char * pBase160 = strdup((const char *)sqlite3_value_text(argv[0]));
224 char * pStart = pBase160;
227 * We need a minimum of four digits, and we will always get a multiple
230 if (pBase160 != NULL &&
231 (len = strlen(pBase160)) >= 4 &&
234 if (pBase160 == NULL) {
236 sqlite3_result_null(hContext);
240 pBase160 += strlen(pBase160) - 1;
242 /* We only carry through four digits: one level in the tree */
243 for (i = 0; i < 4; i++) {
245 /* What base160 value does this digit have? */
246 pTab = strchr(base160tab, *pBase160);
248 /* Is there a carry? */
249 if (pTab < base160tab + sizeof(base160tab) - 1) {
252 * Nope. Just increment this value and we're
260 * There's a carry. This value gets
261 * base160tab[0], we decrement the buffer
262 * pointer to get the next higher-order digit,
263 * and continue in the loop.
265 *pBase160-- = base160tab[0];
269 sqlite3_result_text(hContext,
274 sqlite3_result_value(hContext, argv[0]);
275 if (pBase160 != NULL) {
281 static char *parsetree_to_sql(struct ldb_module *module,
283 const struct ldb_parse_tree *t)
285 const struct ldb_schema_attribute *a;
286 struct ldb_val value, subval;
287 char *wild_card_string;
294 switch(t->operation) {
297 tmp = parsetree_to_sql(module, mem_ctx, t->u.list.elements[0]);
298 if (tmp == NULL) return NULL;
300 for (i = 1; i < t->u.list.num_elements; i++) {
302 child = parsetree_to_sql(module, mem_ctx, t->u.list.elements[i]);
303 if (child == NULL) return NULL;
305 tmp = talloc_asprintf_append(tmp, " INTERSECT %s ", child);
306 if (tmp == NULL) return NULL;
309 ret = talloc_asprintf(mem_ctx, "SELECT * FROM ( %s )\n", tmp);
315 tmp = parsetree_to_sql(module, mem_ctx, t->u.list.elements[0]);
316 if (tmp == NULL) return NULL;
318 for (i = 1; i < t->u.list.num_elements; i++) {
320 child = parsetree_to_sql(module, mem_ctx, t->u.list.elements[i]);
321 if (child == NULL) return NULL;
323 tmp = talloc_asprintf_append(tmp, " UNION %s ", child);
324 if (tmp == NULL) return NULL;
327 return talloc_asprintf(mem_ctx, "SELECT * FROM ( %s ) ", tmp);
331 child = parsetree_to_sql(module, mem_ctx, t->u.isnot.child);
332 if (child == NULL) return NULL;
334 return talloc_asprintf(mem_ctx,
335 "SELECT eid FROM ldb_entry "
336 "WHERE eid NOT IN ( %s ) ", child);
338 case LDB_OP_EQUALITY:
340 * For simple searches, we want to retrieve the list of EIDs that
341 * match the criteria.
343 attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
344 if (attr == NULL) return NULL;
345 a = ldb_schema_attribute_by_name(module->ldb, attr);
347 /* Get a canonicalised copy of the data */
348 a->syntax->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
349 if (value.data == NULL) {
353 if (strcasecmp(t->u.equality.attr, "objectclass") == 0) {
355 * For object classes, we want to search for all objectclasses
356 * that are subclasses as well.
358 return lsqlite3_tprintf(mem_ctx,
359 "SELECT eid FROM ldb_attribute_values\n"
360 "WHERE norm_attr_name = 'OBJECTCLASS' "
361 "AND norm_attr_value IN\n"
362 " (SELECT class_name FROM ldb_object_classes\n"
363 " WHERE tree_key GLOB\n"
364 " (SELECT tree_key FROM ldb_object_classes\n"
365 " WHERE class_name = '%q'\n"
369 } else if (strcasecmp(t->u.equality.attr, "dn") == 0) {
370 /* DN query is a special ldb case */
371 const char *cdn = ldb_dn_get_casefold(
372 ldb_dn_new(mem_ctx, module->ldb,
373 (const char *)value.data));
375 return lsqlite3_tprintf(mem_ctx,
376 "SELECT eid FROM ldb_entry "
377 "WHERE norm_dn = '%q'", cdn);
380 /* A normal query. */
381 return lsqlite3_tprintf(mem_ctx,
382 "SELECT eid FROM ldb_attribute_values "
383 "WHERE norm_attr_name = '%q' "
384 "AND norm_attr_value = '%q'",
390 case LDB_OP_SUBSTRING:
392 wild_card_string = talloc_strdup(mem_ctx,
393 (t->u.substring.start_with_wildcard)?"*":"");
394 if (wild_card_string == NULL) return NULL;
396 for (i = 0; t->u.substring.chunks[i]; i++) {
397 wild_card_string = talloc_asprintf_append(wild_card_string, "%s*",
398 t->u.substring.chunks[i]->data);
399 if (wild_card_string == NULL) return NULL;
402 if ( ! t->u.substring.end_with_wildcard ) {
403 /* remove last wildcard */
404 wild_card_string[strlen(wild_card_string) - 1] = '\0';
407 attr = ldb_attr_casefold(mem_ctx, t->u.substring.attr);
408 if (attr == NULL) return NULL;
409 a = ldb_schema_attribute_by_name(module->ldb, attr);
411 subval.data = (void *)wild_card_string;
412 subval.length = strlen(wild_card_string) + 1;
414 /* Get a canonicalised copy of the data */
415 a->syntax->canonicalise_fn(module->ldb, mem_ctx, &(subval), &value);
416 if (value.data == NULL) {
420 return lsqlite3_tprintf(mem_ctx,
421 "SELECT eid FROM ldb_attribute_values "
422 "WHERE norm_attr_name = '%q' "
423 "AND norm_attr_value GLOB '%q'",
428 attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
429 if (attr == NULL) return NULL;
430 a = ldb_schema_attribute_by_name(module->ldb, attr);
432 /* Get a canonicalised copy of the data */
433 a->syntax->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
434 if (value.data == NULL) {
438 return lsqlite3_tprintf(mem_ctx,
439 "SELECT eid FROM ldb_attribute_values "
440 "WHERE norm_attr_name = '%q' "
441 "AND ldap_compare(norm_attr_value, '>=', '%q', '%q') ",
447 attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
448 if (attr == NULL) return NULL;
449 a = ldb_schema_attribute_by_name(module->ldb, attr);
451 /* Get a canonicalised copy of the data */
452 a->syntax->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
453 if (value.data == NULL) {
457 return lsqlite3_tprintf(mem_ctx,
458 "SELECT eid FROM ldb_attribute_values "
459 "WHERE norm_attr_name = '%q' "
460 "AND ldap_compare(norm_attr_value, '<=', '%q', '%q') ",
466 if (strcasecmp(t->u.present.attr, "dn") == 0) {
467 return talloc_strdup(mem_ctx, "SELECT eid FROM ldb_entry");
470 attr = ldb_attr_casefold(mem_ctx, t->u.present.attr);
471 if (attr == NULL) return NULL;
473 return lsqlite3_tprintf(mem_ctx,
474 "SELECT eid FROM ldb_attribute_values "
475 "WHERE norm_attr_name = '%q' ",
479 attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
480 if (attr == NULL) return NULL;
481 a = ldb_schema_attribute_by_name(module->ldb, attr);
483 /* Get a canonicalised copy of the data */
484 a->syntax->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
485 if (value.data == NULL) {
489 return lsqlite3_tprintf(mem_ctx,
490 "SELECT eid FROM ldb_attribute_values "
491 "WHERE norm_attr_name = '%q' "
492 "AND ldap_compare(norm_attr_value, '~%', 'q', '%q') ",
497 case LDB_OP_EXTENDED:
498 #warning "work out how to handle bitops"
505 /* should never occur */
513 * This function is used for the common case of queries that return a single
516 * NOTE: If more than one value is returned by the query, all but the first
517 * one will be ignored.
520 query_int(const struct lsqlite3_private * lsqlite3,
528 sqlite3_stmt * pStmt;
531 /* Begin access to variable argument list */
532 va_start(args, pSql);
534 /* Format the query */
535 if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
540 * Prepare and execute the SQL statement. Loop allows retrying on
541 * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
542 * requiring retrying the operation.
544 for (bLoop = TRUE; bLoop; ) {
546 /* Compile the SQL statement into sqlite virtual machine */
547 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
551 NULL)) == SQLITE_SCHEMA) {
552 if (stmtGetEID != NULL) {
553 sqlite3_finalize(stmtGetEID);
557 } else if (ret != SQLITE_OK) {
561 /* One row expected */
562 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
563 if (stmtGetEID != NULL) {
564 sqlite3_finalize(stmtGetEID);
567 (void) sqlite3_finalize(pStmt);
569 } else if (ret != SQLITE_ROW) {
570 (void) sqlite3_finalize(pStmt);
574 /* Get the value to be returned */
575 *pRet = sqlite3_column_int64(pStmt, 0);
577 /* Free the virtual machine */
578 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
579 if (stmtGetEID != NULL) {
580 sqlite3_finalize(stmtGetEID);
584 } else if (ret != SQLITE_OK) {
585 (void) sqlite3_finalize(pStmt);
590 * Normal condition is only one time through loop. Loop is
591 * rerun in error conditions, via "continue", above.
596 /* All done with variable argument list */
600 /* Free the memory we allocated for our query string */
607 * This is a bad hack to support ldap style comparisons whithin sqlite.
608 * val is the attribute in the row currently under test
609 * func is the desired test "<=" ">=" "~" ":"
610 * cmp is the value to compare against (eg: "test")
611 * attr is the attribute name the value of which we want to test
614 static void lsqlite3_compare(sqlite3_context *ctx, int argc,
615 sqlite3_value **argv)
617 struct ldb_context *ldb = (struct ldb_context *)sqlite3_user_data(ctx);
618 const char *val = (const char *)sqlite3_value_text(argv[0]);
619 const char *func = (const char *)sqlite3_value_text(argv[1]);
620 const char *cmp = (const char *)sqlite3_value_text(argv[2]);
621 const char *attr = (const char *)sqlite3_value_text(argv[3]);
622 const struct ldb_schema_attribute *a;
630 a = ldb_schema_attribute_by_name(ldb, attr);
631 valX.data = (void *)cmp;
632 valX.length = strlen(cmp);
633 valY.data = (void *)val;
634 valY.length = strlen(val);
635 ret = a->syntax->comparison_fn(ldb, ldb, &valY, &valX);
637 sqlite3_result_int(ctx, 1);
639 sqlite3_result_int(ctx, 0);
644 a = ldb_schema_attribute_by_name(ldb, attr);
645 valX.data = (void *)cmp;
646 valX.length = strlen(cmp);
647 valY.data = (void *)val;
648 valY.length = strlen(val);
649 ret = a->syntax->comparison_fn(ldb, ldb, &valY, &valX);
651 sqlite3_result_int(ctx, 1);
653 sqlite3_result_int(ctx, 0);
659 sqlite3_result_int(ctx, 0);
665 sqlite3_result_int(ctx, 0);
672 sqlite3_result_error(ctx, "Value must start with a special operation char (<>~:)!", -1);
677 /* rename a record */
678 static int lsqlite3_safe_rollback(sqlite3 *sqlite)
684 ret = sqlite3_exec(sqlite, "ROLLBACK;", NULL, NULL, &errmsg);
685 if (ret != SQLITE_OK) {
687 printf("lsqlite3_safe_rollback: Error: %s\n", errmsg);
696 /* return an eid as result */
697 static int lsqlite3_eid_callback(void *result, int col_num, char **cols, char **names)
699 long long *eid = (long long *)result;
701 if (col_num != 1) return SQLITE_ABORT;
702 if (strcasecmp(names[0], "eid") != 0) return SQLITE_ABORT;
704 *eid = atoll(cols[0]);
709 * add a single set of ldap message values to a ldb_message
711 static int lsqlite3_search_callback(void *result, int col_num, char **cols, char **names)
713 struct ldb_handle *handle = talloc_get_type(result, struct ldb_handle);
714 struct lsql_context *ac = talloc_get_type(handle->private_data, struct lsql_context);
715 struct ldb_message *msg;
719 /* eid, dn, attr_name, attr_value */
723 eid = atoll(cols[0]);
725 if (eid != ac->current_eid) { /* here begin a new entry */
727 /* call the async callback for the last entry
728 * except the first time */
729 if (ac->current_eid != 0) {
730 ac->ares->message = ldb_msg_canonicalize(ac->module->ldb, ac->ares->message);
731 if (ac->ares->message == NULL)
734 handle->status = ac->callback(ac->module->ldb, ac->context, ac->ares);
735 if (handle->status != LDB_SUCCESS)
740 ac->ares = talloc_zero(ac, struct ldb_reply);
744 ac->ares->message = ldb_msg_new(ac->ares);
745 if (!ac->ares->message)
748 ac->ares->type = LDB_REPLY_ENTRY;
749 ac->current_eid = eid;
752 msg = ac->ares->message;
754 if (msg->dn == NULL) {
755 msg->dn = ldb_dn_new(msg, ac->module->ldb, cols[1]);
762 for (i = 0; ac->attrs[i]; i++) {
763 if (strcasecmp(cols[2], ac->attrs[i]) == 0) {
768 if (!found) return SQLITE_OK;
771 if (ldb_msg_add_string(msg, cols[2], cols[3]) != 0) {
781 * lsqlite3_get_eid_ndn()
783 * These functions are used for the very common case of retrieving an EID value
784 * given a (normalized) DN.
787 static long long lsqlite3_get_eid_ndn(sqlite3 *sqlite, void *mem_ctx, const char *norm_dn)
795 query = lsqlite3_tprintf(mem_ctx, "SELECT eid "
797 "WHERE norm_dn = '%q';", norm_dn);
798 if (query == NULL) return -1;
800 ret = sqlite3_exec(sqlite, query, lsqlite3_eid_callback, &eid, &errmsg);
801 if (ret != SQLITE_OK) {
803 printf("lsqlite3_get_eid: Fatal Error: %s\n", errmsg);
812 static long long lsqlite3_get_eid(struct ldb_module *module, struct ldb_dn *dn)
814 TALLOC_CTX *local_ctx;
815 struct lsqlite3_private *lsqlite3 = module->private_data;
819 /* ignore ltdb specials */
820 if (ldb_dn_is_special(dn)) {
824 /* create a local ctx */
825 local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_get_eid local context");
826 if (local_ctx == NULL) {
830 cdn = ldb_dn_alloc_casefold(local_ctx, dn);
833 eid = lsqlite3_get_eid_ndn(lsqlite3->sqlite, local_ctx, cdn);
836 talloc_free(local_ctx);
841 * Interface functions referenced by lsqlite3_ops
844 /* search for matching records, by tree */
845 int lsql_search(struct ldb_module *module, struct ldb_request *req)
847 struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
848 struct lsql_context *lsql_ac;
855 req->handle = init_handle(lsqlite3, module, req);
856 if (req->handle == NULL) {
857 return LDB_ERR_OPERATIONS_ERROR;
860 lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
862 if ((( ! ldb_dn_is_valid(req->op.search.base)) || ldb_dn_is_null(req->op.search.base)) &&
863 (req->op.search.scope == LDB_SCOPE_BASE || req->op.search.scope == LDB_SCOPE_ONELEVEL))
864 return LDB_ERR_OPERATIONS_ERROR;
866 if (req->op.search.base) {
867 norm_basedn = ldb_dn_alloc_casefold(lsql_ac, req->op.search.base);
868 if (norm_basedn == NULL) {
869 ret = LDB_ERR_INVALID_DN_SYNTAX;
872 } else norm_basedn = talloc_strdup(lsql_ac, "");
874 /* Convert filter into a series of SQL conditions (constraints) */
875 sqlfilter = parsetree_to_sql(module, lsql_ac, req->op.search.tree);
877 switch(req->op.search.scope) {
878 case LDB_SCOPE_DEFAULT:
879 case LDB_SCOPE_SUBTREE:
880 if (*norm_basedn != '\0') {
881 query = lsqlite3_tprintf(lsql_ac,
882 "SELECT entry.eid,\n"
886 " FROM ldb_entry AS entry\n"
888 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
889 " ON av.eid = entry.eid\n"
891 " WHERE entry.eid IN\n"
892 " (SELECT DISTINCT ldb_entry.eid\n"
894 " WHERE (ldb_entry.norm_dn GLOB('*,%q')\n"
895 " OR ldb_entry.norm_dn = '%q')\n"
896 " AND ldb_entry.eid IN\n"
900 " ORDER BY entry.eid ASC;",
905 query = lsqlite3_tprintf(lsql_ac,
906 "SELECT entry.eid,\n"
910 " FROM ldb_entry AS entry\n"
912 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
913 " ON av.eid = entry.eid\n"
915 " WHERE entry.eid IN\n"
916 " (SELECT DISTINCT ldb_entry.eid\n"
918 " WHERE ldb_entry.eid IN\n"
922 " ORDER BY entry.eid ASC;",
929 query = lsqlite3_tprintf(lsql_ac,
930 "SELECT entry.eid,\n"
934 " FROM ldb_entry AS entry\n"
936 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
937 " ON av.eid = entry.eid\n"
939 " WHERE entry.eid IN\n"
940 " (SELECT DISTINCT ldb_entry.eid\n"
942 " WHERE ldb_entry.norm_dn = '%q'\n"
943 " AND ldb_entry.eid IN\n"
947 " ORDER BY entry.eid ASC;",
952 case LDB_SCOPE_ONELEVEL:
953 query = lsqlite3_tprintf(lsql_ac,
954 "SELECT entry.eid,\n"
958 " FROM ldb_entry AS entry\n"
960 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
961 " ON av.eid = entry.eid\n"
963 " WHERE entry.eid IN\n"
964 " (SELECT DISTINCT ldb_entry.eid\n"
966 " WHERE norm_dn GLOB('*,%q')\n"
967 " AND NOT norm_dn GLOB('*,*,%q')\n"
968 " AND ldb_entry.eid IN\n(%s)\n"
971 " ORDER BY entry.eid ASC;",
983 printf ("%s\n", query);
986 lsql_ac->current_eid = 0;
987 lsql_ac->attrs = req->op.search.attrs;
988 lsql_ac->ares = NULL;
990 req->handle->state = LDB_ASYNC_PENDING;
992 ret = sqlite3_exec(lsqlite3->sqlite, query, lsqlite3_search_callback, req->handle, &errmsg);
993 if (ret != SQLITE_OK) {
995 ldb_set_errstring(module->ldb, errmsg);
1001 /* complete the last message if any */
1002 if (lsql_ac->ares) {
1003 lsql_ac->ares->message = ldb_msg_canonicalize(module->ldb, lsql_ac->ares->message);
1004 if (lsql_ac->ares->message == NULL)
1007 req->handle->status = lsql_ac->callback(module->ldb, lsql_ac->context, lsql_ac->ares);
1008 if (req->handle->status != LDB_SUCCESS)
1012 req->handle->state = LDB_ASYNC_DONE;
1017 return LDB_ERR_OPERATIONS_ERROR;
1021 static int lsql_add(struct ldb_module *module, struct ldb_request *req)
1023 struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1024 struct lsql_context *lsql_ac;
1025 struct ldb_message *msg = req->op.add.message;
1031 int ret = LDB_SUCCESS;
1033 req->handle = init_handle(lsqlite3, module, req);
1034 if (req->handle == NULL) {
1035 return LDB_ERR_OPERATIONS_ERROR;
1037 lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
1038 req->handle->state = LDB_ASYNC_DONE;
1039 req->handle->status = LDB_SUCCESS;
1041 /* See if this is an ltdb special */
1042 if (ldb_dn_is_special(msg->dn)) {
1045 c = ldb_dn_new(lsql_ac, module->ldb, "@SUBCLASSES");
1046 if (ldb_dn_compare(msg->dn, c) == 0) {
1047 #warning "insert subclasses into object class tree"
1048 ret = LDB_ERR_UNWILLING_TO_PERFORM;
1053 c = ldb_dn_new(local_ctx, module->ldb, "@INDEXLIST");
1054 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
1055 #warning "should we handle indexes somehow ?"
1056 ret = LDB_ERR_UNWILLING_TO_PERFORM;
1060 /* Others return an error */
1061 ret = LDB_ERR_UNWILLING_TO_PERFORM;
1065 /* create linearized and normalized dns */
1066 dn = ldb_dn_alloc_linearized(lsql_ac, msg->dn);
1067 ndn = ldb_dn_alloc_casefold(lsql_ac, msg->dn);
1068 if (dn == NULL || ndn == NULL) {
1069 ret = LDB_ERR_OTHER;
1073 query = lsqlite3_tprintf(lsql_ac,
1075 "INSERT OR ABORT INTO ldb_entry "
1076 "('dn', 'norm_dn') "
1077 "VALUES ('%q', '%q');",
1079 if (query == NULL) {
1080 ret = LDB_ERR_OTHER;
1084 ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1085 if (ret != SQLITE_OK) {
1087 ldb_set_errstring(module->ldb, errmsg);
1090 ret = LDB_ERR_OTHER;
1094 eid = lsqlite3_get_eid_ndn(lsqlite3->sqlite, lsql_ac, ndn);
1096 ret = LDB_ERR_OTHER;
1100 for (i = 0; i < msg->num_elements; i++) {
1101 const struct ldb_message_element *el = &msg->elements[i];
1102 const struct ldb_schema_attribute *a;
1106 /* Get a case-folded copy of the attribute name */
1107 attr = ldb_attr_casefold(lsql_ac, el->name);
1109 ret = LDB_ERR_OTHER;
1113 a = ldb_schema_attribute_by_name(module->ldb, el->name);
1115 /* For each value of the specified attribute name... */
1116 for (j = 0; j < el->num_values; j++) {
1117 struct ldb_val value;
1120 /* Get a canonicalised copy of the data */
1121 a->syntax->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
1122 if (value.data == NULL) {
1123 ret = LDB_ERR_OTHER;
1127 insert = lsqlite3_tprintf(lsql_ac,
1128 "INSERT OR ROLLBACK INTO ldb_attribute_values "
1129 "('eid', 'attr_name', 'norm_attr_name',"
1130 " 'attr_value', 'norm_attr_value') "
1131 "VALUES ('%lld', '%q', '%q', '%q', '%q');",
1132 eid, el->name, attr,
1133 el->values[j].data, value.data);
1134 if (insert == NULL) {
1135 ret = LDB_ERR_OTHER;
1139 ret = sqlite3_exec(lsqlite3->sqlite, insert, NULL, NULL, &errmsg);
1140 if (ret != SQLITE_OK) {
1142 ldb_set_errstring(module->ldb, errmsg);
1145 ret = LDB_ERR_OTHER;
1151 if (lsql_ac->callback) {
1152 req->handle->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1156 req->handle->state = LDB_ASYNC_DONE;
1160 /* modify a record */
1161 static int lsql_modify(struct ldb_module *module, struct ldb_request *req)
1163 struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1164 struct lsql_context *lsql_ac;
1165 struct ldb_message *msg = req->op.mod.message;
1169 int ret = LDB_SUCCESS;
1171 req->handle = init_handle(lsqlite3, module, req);
1172 if (req->handle == NULL) {
1173 return LDB_ERR_OPERATIONS_ERROR;
1175 lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
1176 req->handle->state = LDB_ASYNC_DONE;
1177 req->handle->status = LDB_SUCCESS;
1179 /* See if this is an ltdb special */
1180 if (ldb_dn_is_special(msg->dn)) {
1183 c = ldb_dn_new(lsql_ac, module->ldb, "@SUBCLASSES");
1184 if (ldb_dn_compare(msg->dn, c) == 0) {
1185 #warning "modify subclasses into object class tree"
1186 ret = LDB_ERR_UNWILLING_TO_PERFORM;
1190 /* Others return an error */
1191 ret = LDB_ERR_UNWILLING_TO_PERFORM;
1195 eid = lsqlite3_get_eid(module, msg->dn);
1197 ret = LDB_ERR_OTHER;
1201 for (i = 0; i < msg->num_elements; i++) {
1202 const struct ldb_message_element *el = &msg->elements[i];
1203 const struct ldb_schema_attribute *a;
1204 int flags = el->flags & LDB_FLAG_MOD_MASK;
1209 /* Get a case-folded copy of the attribute name */
1210 attr = ldb_attr_casefold(lsql_ac, el->name);
1212 ret = LDB_ERR_OTHER;
1216 a = ldb_schema_attribute_by_name(module->ldb, el->name);
1220 case LDB_FLAG_MOD_REPLACE:
1222 /* remove all attributes before adding the replacements */
1223 mod = lsqlite3_tprintf(lsql_ac,
1224 "DELETE FROM ldb_attribute_values "
1225 "WHERE eid = '%lld' "
1226 "AND norm_attr_name = '%q';",
1229 ret = LDB_ERR_OTHER;
1233 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1234 if (ret != SQLITE_OK) {
1236 ldb_set_errstring(module->ldb, errmsg);
1239 ret = LDB_ERR_OTHER;
1243 /* MISSING break is INTENTIONAL */
1245 case LDB_FLAG_MOD_ADD:
1246 #warning "We should throw an error if no value is provided!"
1247 /* For each value of the specified attribute name... */
1248 for (j = 0; j < el->num_values; j++) {
1249 struct ldb_val value;
1251 /* Get a canonicalised copy of the data */
1252 a->syntax->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
1253 if (value.data == NULL) {
1254 ret = LDB_ERR_OTHER;
1258 mod = lsqlite3_tprintf(lsql_ac,
1259 "INSERT OR ROLLBACK INTO ldb_attribute_values "
1260 "('eid', 'attr_name', 'norm_attr_name',"
1261 " 'attr_value', 'norm_attr_value') "
1262 "VALUES ('%lld', '%q', '%q', '%q', '%q');",
1263 eid, el->name, attr,
1264 el->values[j].data, value.data);
1267 ret = LDB_ERR_OTHER;
1271 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1272 if (ret != SQLITE_OK) {
1274 ldb_set_errstring(module->ldb, errmsg);
1277 ret = LDB_ERR_OTHER;
1284 case LDB_FLAG_MOD_DELETE:
1285 #warning "We should throw an error if the attribute we are trying to delete does not exist!"
1286 if (el->num_values == 0) {
1287 mod = lsqlite3_tprintf(lsql_ac,
1288 "DELETE FROM ldb_attribute_values "
1289 "WHERE eid = '%lld' "
1290 "AND norm_attr_name = '%q';",
1293 ret = LDB_ERR_OTHER;
1297 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1298 if (ret != SQLITE_OK) {
1300 ldb_set_errstring(module->ldb, errmsg);
1303 ret = LDB_ERR_OTHER;
1308 /* For each value of the specified attribute name... */
1309 for (j = 0; j < el->num_values; j++) {
1310 struct ldb_val value;
1312 /* Get a canonicalised copy of the data */
1313 a->syntax->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
1314 if (value.data == NULL) {
1315 ret = LDB_ERR_OTHER;
1319 mod = lsqlite3_tprintf(lsql_ac,
1320 "DELETE FROM ldb_attribute_values "
1321 "WHERE eid = '%lld' "
1322 "AND norm_attr_name = '%q' "
1323 "AND norm_attr_value = '%q';",
1324 eid, attr, value.data);
1327 ret = LDB_ERR_OTHER;
1331 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1332 if (ret != SQLITE_OK) {
1334 ldb_set_errstring(module->ldb, errmsg);
1337 ret = LDB_ERR_OTHER;
1346 if (lsql_ac->callback) {
1347 req->handle->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1351 req->handle->state = LDB_ASYNC_DONE;
1355 /* delete a record */
1356 static int lsql_delete(struct ldb_module *module, struct ldb_request *req)
1358 struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1359 struct lsql_context *lsql_ac;
1363 int ret = LDB_SUCCESS;
1366 req->handle = init_handle(lsqlite3, module, req);
1367 if (req->handle == NULL) {
1368 return LDB_ERR_OPERATIONS_ERROR;
1370 lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
1371 req->handle->state = LDB_ASYNC_DONE;
1372 req->handle->status = LDB_SUCCESS;
1374 eid = lsqlite3_get_eid(module, req->op.del.dn);
1379 query = lsqlite3_tprintf(lsql_ac,
1381 "DELETE FROM ldb_entry WHERE eid = %lld; "
1382 /* Delete attributes */
1383 "DELETE FROM ldb_attribute_values WHERE eid = %lld; ",
1385 if (query == NULL) {
1386 ret = LDB_ERR_OTHER;
1390 ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1391 if (ret != SQLITE_OK) {
1393 ldb_set_errstring(module->ldb, errmsg);
1396 req->handle->status = LDB_ERR_OPERATIONS_ERROR;
1400 if (lsql_ac->callback) {
1401 ret = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1405 req->handle->state = LDB_ASYNC_DONE;
1409 /* rename a record */
1410 static int lsql_rename(struct ldb_module *module, struct ldb_request *req)
1412 struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1413 struct lsql_context *lsql_ac;
1414 char *new_dn, *new_cdn, *old_cdn;
1417 int ret = LDB_SUCCESS;
1419 req->handle = init_handle(lsqlite3, module, req);
1420 if (req->handle == NULL) {
1421 return LDB_ERR_OPERATIONS_ERROR;
1423 lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
1424 req->handle->state = LDB_ASYNC_DONE;
1425 req->handle->status = LDB_SUCCESS;
1427 /* create linearized and normalized dns */
1428 old_cdn = ldb_dn_alloc_casefold(lsql_ac, req->op.rename.olddn);
1429 new_cdn = ldb_dn_alloc_casefold(lsql_ac, req->op.rename.newdn);
1430 new_dn = ldb_dn_alloc_linearized(lsql_ac, req->op.rename.newdn);
1431 if (old_cdn == NULL || new_cdn == NULL || new_dn == NULL) {
1435 /* build the SQL query */
1436 query = lsqlite3_tprintf(lsql_ac,
1437 "UPDATE ldb_entry SET dn = '%q', norm_dn = '%q' "
1438 "WHERE norm_dn = '%q';",
1439 new_dn, new_cdn, old_cdn);
1440 if (query == NULL) {
1445 ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1446 if (ret != SQLITE_OK) {
1448 ldb_set_errstring(module->ldb, errmsg);
1451 ret = LDB_ERR_OPERATIONS_ERROR;
1455 if (lsql_ac->callback) {
1456 ret = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1460 req->handle->state = LDB_ASYNC_DONE;
1464 static int lsql_start_trans(struct ldb_module * module)
1468 struct lsqlite3_private * lsqlite3 = module->private_data;
1470 if (lsqlite3->trans_count == 0) {
1471 ret = sqlite3_exec(lsqlite3->sqlite, "BEGIN IMMEDIATE;", NULL, NULL, &errmsg);
1472 if (ret != SQLITE_OK) {
1474 printf("lsqlite3_start_trans: error: %s\n", errmsg);
1481 lsqlite3->trans_count++;
1486 static int lsql_end_trans(struct ldb_module *module)
1490 struct lsqlite3_private *lsqlite3 = module->private_data;
1492 if (lsqlite3->trans_count > 0) {
1493 lsqlite3->trans_count--;
1496 if (lsqlite3->trans_count == 0) {
1497 ret = sqlite3_exec(lsqlite3->sqlite, "COMMIT;", NULL, NULL, &errmsg);
1498 if (ret != SQLITE_OK) {
1500 printf("lsqlite3_end_trans: error: %s\n", errmsg);
1510 static int lsql_del_trans(struct ldb_module *module)
1512 struct lsqlite3_private *lsqlite3 = module->private_data;
1514 if (lsqlite3->trans_count > 0) {
1515 lsqlite3->trans_count--;
1518 if (lsqlite3->trans_count == 0) {
1519 return lsqlite3_safe_rollback(lsqlite3->sqlite);
1525 static int destructor(struct lsqlite3_private *lsqlite3)
1527 if (lsqlite3->sqlite) {
1528 sqlite3_close(lsqlite3->sqlite);
1533 static int lsql_request(struct ldb_module *module, struct ldb_request *req)
1535 return LDB_ERR_OPERATIONS_ERROR;
1538 static int lsql_wait(struct ldb_handle *handle, enum ldb_wait_type type)
1540 return handle->status;
1544 * Table of operations for the sqlite3 backend
1546 static const struct ldb_module_ops lsqlite3_ops = {
1548 .search = lsql_search,
1550 .modify = lsql_modify,
1552 .rename = lsql_rename,
1553 .request = lsql_request,
1554 .start_transaction = lsql_start_trans,
1555 .end_transaction = lsql_end_trans,
1556 .del_transaction = lsql_del_trans,
1564 static int initialize(struct lsqlite3_private *lsqlite3,
1565 struct ldb_context *ldb, const char *url, int flags)
1567 TALLOC_CTX *local_ctx;
1574 /* create a local ctx */
1575 local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_rename local context");
1576 if (local_ctx == NULL) {
1580 schema = lsqlite3_tprintf(local_ctx,
1583 "CREATE TABLE ldb_info AS "
1584 " SELECT 'LDB' AS database_type,"
1585 " '1.0' AS version;"
1588 * The entry table holds the information about an entry.
1589 * This table is used to obtain the EID of the entry and to
1590 * support scope=one and scope=base. The parent and child
1591 * table is included in the entry table since all the other
1592 * attributes are dependent on EID.
1594 "CREATE TABLE ldb_entry "
1596 " eid INTEGER PRIMARY KEY AUTOINCREMENT,"
1597 " dn TEXT UNIQUE NOT NULL,"
1598 " norm_dn TEXT UNIQUE NOT NULL"
1602 "CREATE TABLE ldb_object_classes"
1604 " class_name TEXT PRIMARY KEY,"
1605 " parent_class_name TEXT,"
1606 " tree_key TEXT UNIQUE,"
1607 " max_child_num INTEGER DEFAULT 0"
1611 * We keep a full listing of attribute/value pairs here
1613 "CREATE TABLE ldb_attribute_values"
1615 " eid INTEGER REFERENCES ldb_entry,"
1617 " norm_attr_name TEXT,"
1619 " norm_attr_value TEXT "
1626 "CREATE INDEX ldb_attribute_values_eid_idx "
1627 " ON ldb_attribute_values (eid);"
1629 "CREATE INDEX ldb_attribute_values_name_value_idx "
1630 " ON ldb_attribute_values (attr_name, norm_attr_value);"
1638 "CREATE TRIGGER ldb_object_classes_insert_tr"
1640 " ON ldb_object_classes"
1643 " UPDATE ldb_object_classes"
1644 " SET tree_key = COALESCE(tree_key, "
1646 " SELECT tree_key || "
1647 " (SELECT base160(max_child_num + 1)"
1648 " FROM ldb_object_classes"
1649 " WHERE class_name = "
1650 " new.parent_class_name)"
1651 " FROM ldb_object_classes "
1652 " WHERE class_name = new.parent_class_name "
1654 " UPDATE ldb_object_classes "
1655 " SET max_child_num = max_child_num + 1"
1656 " WHERE class_name = new.parent_class_name;"
1660 * Table initialization
1663 "INSERT INTO ldb_object_classes "
1664 " (class_name, tree_key) "
1666 " ('TOP', '0001');");
1668 /* Skip protocol indicator of url */
1669 if (strncmp(url, "sqlite3://", 10) != 0) {
1670 return SQLITE_MISUSE;
1673 /* Update pointer to just after the protocol indicator */
1676 /* Try to open the (possibly empty/non-existent) database */
1677 if ((ret = sqlite3_open(url, &lsqlite3->sqlite)) != SQLITE_OK) {
1681 /* In case this is a new database, enable auto_vacuum */
1682 ret = sqlite3_exec(lsqlite3->sqlite, "PRAGMA auto_vacuum = 1;", NULL, NULL, &errmsg);
1683 if (ret != SQLITE_OK) {
1685 printf("lsqlite3 initializaion error: %s\n", errmsg);
1691 if (flags & LDB_FLG_NOSYNC) {
1693 ret = sqlite3_exec(lsqlite3->sqlite, "PRAGMA synchronous = OFF;", NULL, NULL, &errmsg);
1694 if (ret != SQLITE_OK) {
1696 printf("lsqlite3 initializaion error: %s\n", errmsg);
1705 /* Establish a busy timeout of 30 seconds */
1706 if ((ret = sqlite3_busy_timeout(lsqlite3->sqlite,
1707 30000)) != SQLITE_OK) {
1711 /* Create a function, callable from sql, to increment a tree_key */
1713 sqlite3_create_function(lsqlite3->sqlite,/* handle */
1714 "base160_next", /* function name */
1715 1, /* number of args */
1716 SQLITE_ANY, /* preferred text type */
1717 NULL, /* user data */
1718 base160next_sql, /* called func */
1719 NULL, /* step func */
1720 NULL /* final func */
1725 /* Create a function, callable from sql, to convert int to base160 */
1727 sqlite3_create_function(lsqlite3->sqlite,/* handle */
1728 "base160", /* function name */
1729 1, /* number of args */
1730 SQLITE_ANY, /* preferred text type */
1731 NULL, /* user data */
1732 base160_sql, /* called func */
1733 NULL, /* step func */
1734 NULL /* final func */
1739 /* Create a function, callable from sql, to perform various comparisons */
1741 sqlite3_create_function(lsqlite3->sqlite, /* handle */
1742 "ldap_compare", /* function name */
1743 4, /* number of args */
1744 SQLITE_ANY, /* preferred text type */
1745 ldb , /* user data */
1746 lsqlite3_compare, /* called func */
1747 NULL, /* step func */
1748 NULL /* final func */
1753 /* Begin a transaction */
1754 ret = sqlite3_exec(lsqlite3->sqlite, "BEGIN EXCLUSIVE;", NULL, NULL, &errmsg);
1755 if (ret != SQLITE_OK) {
1757 printf("lsqlite3: initialization error: %s\n", errmsg);
1764 /* Determine if this is a new database. No tables means it is. */
1765 if (query_int(lsqlite3,
1768 " FROM sqlite_master\n"
1769 " WHERE type = 'table';") != 0) {
1773 if (queryInt == 0) {
1775 * Create the database schema
1777 ret = sqlite3_exec(lsqlite3->sqlite, schema, NULL, NULL, &errmsg);
1778 if (ret != SQLITE_OK) {
1780 printf("lsqlite3 initializaion error: %s\n", errmsg);
1787 * Ensure that the database we opened is one of ours
1789 if (query_int(lsqlite3,
1792 " (SELECT COUNT(*) = 2"
1793 " FROM sqlite_master "
1794 " WHERE type = 'table' "
1798 " 'ldb_object_classes' "
1804 " WHERE database_type = 'LDB' "
1805 " AND version = '1.0'"
1809 /* It's not one that we created. See ya! */
1814 /* Commit the transaction */
1815 ret = sqlite3_exec(lsqlite3->sqlite, "COMMIT;", NULL, NULL, &errmsg);
1816 if (ret != SQLITE_OK) {
1818 printf("lsqlite3: iniialization error: %s\n", errmsg);
1827 if (rollback) lsqlite3_safe_rollback(lsqlite3->sqlite);
1828 sqlite3_close(lsqlite3->sqlite);
1833 * connect to the database
1835 static int lsqlite3_connect(struct ldb_context *ldb,
1838 const char *options[],
1839 struct ldb_module **module)
1843 struct lsqlite3_private * lsqlite3 = NULL;
1845 lsqlite3 = talloc(ldb, struct lsqlite3_private);
1850 lsqlite3->sqlite = NULL;
1851 lsqlite3->options = NULL;
1852 lsqlite3->trans_count = 0;
1854 ret = initialize(lsqlite3, ldb, url, flags);
1855 if (ret != SQLITE_OK) {
1859 talloc_set_destructor(lsqlite3, destructor);
1863 *module = talloc(ldb, struct ldb_module);
1868 talloc_set_name_const(*module, "ldb_sqlite3 backend");
1869 (*module)->ldb = ldb;
1870 (*module)->prev = (*module)->next = NULL;
1871 (*module)->private_data = lsqlite3;
1872 (*module)->ops = &lsqlite3_ops;
1876 * take a copy of the options array, so we don't have to rely
1877 * on the caller keeping it around (it might be dynamic)
1879 for (i=0;options[i];i++) ;
1881 lsqlite3->options = talloc_array(lsqlite3, char *, i+1);
1882 if (!lsqlite3->options) {
1886 for (i=0;options[i];i++) {
1888 lsqlite3->options[i+1] = NULL;
1889 lsqlite3->options[i] =
1890 talloc_strdup(lsqlite3->options, options[i]);
1891 if (!lsqlite3->options[i]) {
1900 if (lsqlite3->sqlite != NULL) {
1901 (void) sqlite3_close(lsqlite3->sqlite);
1903 talloc_free(lsqlite3);
1907 int ldb_sqlite3_init(void)
1909 return ldb_register_backend("sqlite3", lsqlite3_connect);