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 3 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, see <http://www.gnu.org/licenses/>.
28 * Component: ldb sqlite3 backend
30 * Description: core files for SQLITE3 backend
32 * Author: Derrell Lipman (based on Andrew Tridgell's LDAP backend)
35 #include "ldb_includes.h"
39 struct lsqlite3_private {
46 struct ldb_module *module;
47 struct ldb_request *req;
50 long long current_eid;
51 const char * const * attrs;
52 struct ldb_reply *ares;
56 * Macros used throughout
61 # define TRUE (! FALSE)
64 #define RESULT_ATTR_TABLE "temp_result_attrs"
66 //#define TEMPTAB /* for testing, create non-temporary table */
67 #define TEMPTAB "TEMPORARY"
72 sqlite3_stmt * stmtGetEID = NULL;
74 static char *lsqlite3_tprintf(TALLOC_CTX *mem_ctx, const char *fmt, ...)
80 str = sqlite3_vmprintf(fmt, ap);
83 if (str == NULL) return NULL;
85 ret = talloc_strdup(mem_ctx, str);
95 static char base160tab[161] = {
96 48 ,49 ,50 ,51 ,52 ,53 ,54 ,55 ,56 ,57 , /* 0-9 */
97 58 ,59 ,65 ,66 ,67 ,68 ,69 ,70 ,71 ,72 , /* : ; A-H */
98 73 ,74 ,75 ,76 ,77 ,78 ,79 ,80 ,81 ,82 , /* I-R */
99 83 ,84 ,85 ,86 ,87 ,88 ,89 ,90 ,97 ,98 , /* S-Z , a-b */
100 99 ,100,101,102,103,104,105,106,107,108, /* c-l */
101 109,110,111,112,113,114,115,116,117,118, /* m-v */
102 119,120,121,122,160,161,162,163,164,165, /* w-z, latin1 */
103 166,167,168,169,170,171,172,173,174,175, /* latin1 */
104 176,177,178,179,180,181,182,183,184,185, /* latin1 */
105 186,187,188,189,190,191,192,193,194,195, /* latin1 */
106 196,197,198,199,200,201,202,203,204,205, /* latin1 */
107 206,207,208,209,210,211,212,213,214,215, /* latin1 */
108 216,217,218,219,220,221,222,223,224,225, /* latin1 */
109 226,227,228,229,230,231,232,233,234,235, /* latin1 */
110 236,237,238,239,240,241,242,243,244,245, /* latin1 */
111 246,247,248,249,250,251,252,253,254,255, /* latin1 */
119 * Convert an unsigned long integer into a base160 representation of the
124 * value to be converted
127 * character array, 5 bytes long, into which the base160 representation
128 * will be placed. The result will be a four-digit representation of the
129 * number (with leading zeros prepended as necessary), and null
136 base160_sql(sqlite3_context * hContext,
138 sqlite3_value ** argv)
144 val = sqlite3_value_int64(argv[0]);
146 for (i = 3; i >= 0; i--) {
148 result[i] = base160tab[val % 160];
154 sqlite3_result_text(hContext, result, -1, SQLITE_TRANSIENT);
161 * This function enhances sqlite by adding a "base160_next()" function which is
162 * accessible via queries.
164 * Retrieve the next-greater number in the base160 sequence for the terminal
165 * tree node (the last four digits). Only one tree level (four digits) is
169 * A character string: either an empty string (in which case no operation is
170 * performed), or a string of base160 digits with a length of a multiple of
174 * Upon return, the trailing four digits (one tree level) will have been
178 base160next_sql(sqlite3_context * hContext,
180 sqlite3_value ** argv)
185 char * pBase160 = strdup((const char *)sqlite3_value_text(argv[0]));
186 char * pStart = pBase160;
189 * We need a minimum of four digits, and we will always get a multiple
192 if (pBase160 != NULL &&
193 (len = strlen(pBase160)) >= 4 &&
196 if (pBase160 == NULL) {
198 sqlite3_result_null(hContext);
202 pBase160 += strlen(pBase160) - 1;
204 /* We only carry through four digits: one level in the tree */
205 for (i = 0; i < 4; i++) {
207 /* What base160 value does this digit have? */
208 pTab = strchr(base160tab, *pBase160);
210 /* Is there a carry? */
211 if (pTab < base160tab + sizeof(base160tab) - 1) {
214 * Nope. Just increment this value and we're
222 * There's a carry. This value gets
223 * base160tab[0], we decrement the buffer
224 * pointer to get the next higher-order digit,
225 * and continue in the loop.
227 *pBase160-- = base160tab[0];
231 sqlite3_result_text(hContext,
236 sqlite3_result_value(hContext, argv[0]);
237 if (pBase160 != NULL) {
243 static char *parsetree_to_sql(struct ldb_module *module,
245 const struct ldb_parse_tree *t)
247 const struct ldb_schema_attribute *a;
248 struct ldb_val value, subval;
249 char *wild_card_string;
256 switch(t->operation) {
259 tmp = parsetree_to_sql(module, mem_ctx, t->u.list.elements[0]);
260 if (tmp == NULL) return NULL;
262 for (i = 1; i < t->u.list.num_elements; i++) {
264 child = parsetree_to_sql(module, mem_ctx, t->u.list.elements[i]);
265 if (child == NULL) return NULL;
267 tmp = talloc_asprintf_append(tmp, " INTERSECT %s ", child);
268 if (tmp == NULL) return NULL;
271 ret = talloc_asprintf(mem_ctx, "SELECT * FROM ( %s )\n", tmp);
277 tmp = parsetree_to_sql(module, mem_ctx, t->u.list.elements[0]);
278 if (tmp == NULL) return NULL;
280 for (i = 1; i < t->u.list.num_elements; i++) {
282 child = parsetree_to_sql(module, mem_ctx, t->u.list.elements[i]);
283 if (child == NULL) return NULL;
285 tmp = talloc_asprintf_append(tmp, " UNION %s ", child);
286 if (tmp == NULL) return NULL;
289 return talloc_asprintf(mem_ctx, "SELECT * FROM ( %s ) ", tmp);
293 child = parsetree_to_sql(module, mem_ctx, t->u.isnot.child);
294 if (child == NULL) return NULL;
296 return talloc_asprintf(mem_ctx,
297 "SELECT eid FROM ldb_entry "
298 "WHERE eid NOT IN ( %s ) ", child);
300 case LDB_OP_EQUALITY:
302 * For simple searches, we want to retrieve the list of EIDs that
303 * match the criteria.
305 attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
306 if (attr == NULL) return NULL;
307 a = ldb_schema_attribute_by_name(module->ldb, attr);
309 /* Get a canonicalised copy of the data */
310 a->syntax->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
311 if (value.data == NULL) {
315 if (strcasecmp(t->u.equality.attr, "dn") == 0) {
316 /* DN query is a special ldb case */
317 const char *cdn = ldb_dn_get_casefold(
318 ldb_dn_new(mem_ctx, module->ldb,
319 (const char *)value.data));
321 return lsqlite3_tprintf(mem_ctx,
322 "SELECT eid FROM ldb_entry "
323 "WHERE norm_dn = '%q'", cdn);
326 /* A normal query. */
327 return lsqlite3_tprintf(mem_ctx,
328 "SELECT eid FROM ldb_attribute_values "
329 "WHERE norm_attr_name = '%q' "
330 "AND norm_attr_value = '%q'",
336 case LDB_OP_SUBSTRING:
338 wild_card_string = talloc_strdup(mem_ctx,
339 (t->u.substring.start_with_wildcard)?"*":"");
340 if (wild_card_string == NULL) return NULL;
342 for (i = 0; t->u.substring.chunks[i]; i++) {
343 wild_card_string = talloc_asprintf_append(wild_card_string, "%s*",
344 t->u.substring.chunks[i]->data);
345 if (wild_card_string == NULL) return NULL;
348 if ( ! t->u.substring.end_with_wildcard ) {
349 /* remove last wildcard */
350 wild_card_string[strlen(wild_card_string) - 1] = '\0';
353 attr = ldb_attr_casefold(mem_ctx, t->u.substring.attr);
354 if (attr == NULL) return NULL;
355 a = ldb_schema_attribute_by_name(module->ldb, attr);
357 subval.data = (void *)wild_card_string;
358 subval.length = strlen(wild_card_string) + 1;
360 /* Get a canonicalised copy of the data */
361 a->syntax->canonicalise_fn(module->ldb, mem_ctx, &(subval), &value);
362 if (value.data == NULL) {
366 return lsqlite3_tprintf(mem_ctx,
367 "SELECT eid FROM ldb_attribute_values "
368 "WHERE norm_attr_name = '%q' "
369 "AND norm_attr_value GLOB '%q'",
374 attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
375 if (attr == NULL) return NULL;
376 a = ldb_schema_attribute_by_name(module->ldb, attr);
378 /* Get a canonicalised copy of the data */
379 a->syntax->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
380 if (value.data == NULL) {
384 return lsqlite3_tprintf(mem_ctx,
385 "SELECT eid FROM ldb_attribute_values "
386 "WHERE norm_attr_name = '%q' "
387 "AND ldap_compare(norm_attr_value, '>=', '%q', '%q') ",
393 attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
394 if (attr == NULL) return NULL;
395 a = ldb_schema_attribute_by_name(module->ldb, attr);
397 /* Get a canonicalised copy of the data */
398 a->syntax->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
399 if (value.data == NULL) {
403 return lsqlite3_tprintf(mem_ctx,
404 "SELECT eid FROM ldb_attribute_values "
405 "WHERE norm_attr_name = '%q' "
406 "AND ldap_compare(norm_attr_value, '<=', '%q', '%q') ",
412 if (strcasecmp(t->u.present.attr, "dn") == 0) {
413 return talloc_strdup(mem_ctx, "SELECT eid FROM ldb_entry");
416 attr = ldb_attr_casefold(mem_ctx, t->u.present.attr);
417 if (attr == NULL) return NULL;
419 return lsqlite3_tprintf(mem_ctx,
420 "SELECT eid FROM ldb_attribute_values "
421 "WHERE norm_attr_name = '%q' ",
425 attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
426 if (attr == NULL) return NULL;
427 a = ldb_schema_attribute_by_name(module->ldb, attr);
429 /* Get a canonicalised copy of the data */
430 a->syntax->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
431 if (value.data == NULL) {
435 return lsqlite3_tprintf(mem_ctx,
436 "SELECT eid FROM ldb_attribute_values "
437 "WHERE norm_attr_name = '%q' "
438 "AND ldap_compare(norm_attr_value, '~%', 'q', '%q') ",
443 case LDB_OP_EXTENDED:
444 #warning "work out how to handle bitops"
451 /* should never occur */
459 * This function is used for the common case of queries that return a single
462 * NOTE: If more than one value is returned by the query, all but the first
463 * one will be ignored.
466 query_int(const struct lsqlite3_private * lsqlite3,
474 sqlite3_stmt * pStmt;
477 /* Begin access to variable argument list */
478 va_start(args, pSql);
480 /* Format the query */
481 if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
486 * Prepare and execute the SQL statement. Loop allows retrying on
487 * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
488 * requiring retrying the operation.
490 for (bLoop = TRUE; bLoop; ) {
492 /* Compile the SQL statement into sqlite virtual machine */
493 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
497 NULL)) == SQLITE_SCHEMA) {
498 if (stmtGetEID != NULL) {
499 sqlite3_finalize(stmtGetEID);
503 } else if (ret != SQLITE_OK) {
507 /* One row expected */
508 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
509 if (stmtGetEID != NULL) {
510 sqlite3_finalize(stmtGetEID);
513 (void) sqlite3_finalize(pStmt);
515 } else if (ret != SQLITE_ROW) {
516 (void) sqlite3_finalize(pStmt);
520 /* Get the value to be returned */
521 *pRet = sqlite3_column_int64(pStmt, 0);
523 /* Free the virtual machine */
524 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
525 if (stmtGetEID != NULL) {
526 sqlite3_finalize(stmtGetEID);
530 } else if (ret != SQLITE_OK) {
531 (void) sqlite3_finalize(pStmt);
536 * Normal condition is only one time through loop. Loop is
537 * rerun in error conditions, via "continue", above.
542 /* All done with variable argument list */
546 /* Free the memory we allocated for our query string */
553 * This is a bad hack to support ldap style comparisons whithin sqlite.
554 * val is the attribute in the row currently under test
555 * func is the desired test "<=" ">=" "~" ":"
556 * cmp is the value to compare against (eg: "test")
557 * attr is the attribute name the value of which we want to test
560 static void lsqlite3_compare(sqlite3_context *ctx, int argc,
561 sqlite3_value **argv)
563 struct ldb_context *ldb = (struct ldb_context *)sqlite3_user_data(ctx);
564 const char *val = (const char *)sqlite3_value_text(argv[0]);
565 const char *func = (const char *)sqlite3_value_text(argv[1]);
566 const char *cmp = (const char *)sqlite3_value_text(argv[2]);
567 const char *attr = (const char *)sqlite3_value_text(argv[3]);
568 const struct ldb_schema_attribute *a;
576 a = ldb_schema_attribute_by_name(ldb, attr);
577 valX.data = (void *)cmp;
578 valX.length = strlen(cmp);
579 valY.data = (void *)val;
580 valY.length = strlen(val);
581 ret = a->syntax->comparison_fn(ldb, ldb, &valY, &valX);
583 sqlite3_result_int(ctx, 1);
585 sqlite3_result_int(ctx, 0);
590 a = ldb_schema_attribute_by_name(ldb, attr);
591 valX.data = (void *)cmp;
592 valX.length = strlen(cmp);
593 valY.data = (void *)val;
594 valY.length = strlen(val);
595 ret = a->syntax->comparison_fn(ldb, ldb, &valY, &valX);
597 sqlite3_result_int(ctx, 1);
599 sqlite3_result_int(ctx, 0);
605 sqlite3_result_int(ctx, 0);
611 sqlite3_result_int(ctx, 0);
618 sqlite3_result_error(ctx, "Value must start with a special operation char (<>~:)!", -1);
623 /* rename a record */
624 static int lsqlite3_safe_rollback(sqlite3 *sqlite)
630 ret = sqlite3_exec(sqlite, "ROLLBACK;", NULL, NULL, &errmsg);
631 if (ret != SQLITE_OK) {
633 printf("lsqlite3_safe_rollback: Error: %s\n", errmsg);
642 /* return an eid as result */
643 static int lsqlite3_eid_callback(void *result, int col_num, char **cols, char **names)
645 long long *eid = (long long *)result;
647 if (col_num != 1) return SQLITE_ABORT;
648 if (strcasecmp(names[0], "eid") != 0) return SQLITE_ABORT;
650 *eid = atoll(cols[0]);
655 * add a single set of ldap message values to a ldb_message
657 static int lsqlite3_search_callback(void *result, int col_num, char **cols, char **names)
659 struct ldb_handle *handle = talloc_get_type(result, struct ldb_handle);
660 struct lsql_context *ac = talloc_get_type(handle->private_data, struct lsql_context);
661 struct ldb_message *msg;
665 /* eid, dn, attr_name, attr_value */
669 eid = atoll(cols[0]);
671 if (eid != ac->current_eid) { /* here begin a new entry */
673 /* call the async callback for the last entry
674 * except the first time */
675 if (ac->current_eid != 0) {
676 ac->ares->message = ldb_msg_canonicalize(ac->module->ldb, ac->ares->message);
677 if (ac->ares->message == NULL)
680 handle->status = ac->callback(ac->module->ldb, ac->context, ac->ares);
681 if (handle->status != LDB_SUCCESS)
686 ac->ares = talloc_zero(ac, struct ldb_reply);
690 ac->ares->message = ldb_msg_new(ac->ares);
691 if (!ac->ares->message)
694 ac->ares->type = LDB_REPLY_ENTRY;
695 ac->current_eid = eid;
698 msg = ac->ares->message;
700 if (msg->dn == NULL) {
701 msg->dn = ldb_dn_new(msg, ac->module->ldb, cols[1]);
708 for (i = 0; ac->attrs[i]; i++) {
709 if (strcasecmp(cols[2], ac->attrs[i]) == 0) {
714 if (!found) return SQLITE_OK;
717 if (ldb_msg_add_string(msg, cols[2], cols[3]) != 0) {
727 * lsqlite3_get_eid_ndn()
729 * These functions are used for the very common case of retrieving an EID value
730 * given a (normalized) DN.
733 static long long lsqlite3_get_eid_ndn(sqlite3 *sqlite, void *mem_ctx, const char *norm_dn)
741 query = lsqlite3_tprintf(mem_ctx, "SELECT eid "
743 "WHERE norm_dn = '%q';", norm_dn);
744 if (query == NULL) return -1;
746 ret = sqlite3_exec(sqlite, query, lsqlite3_eid_callback, &eid, &errmsg);
747 if (ret != SQLITE_OK) {
749 printf("lsqlite3_get_eid: Fatal Error: %s\n", errmsg);
758 static long long lsqlite3_get_eid(struct ldb_module *module, struct ldb_dn *dn)
760 TALLOC_CTX *local_ctx;
761 struct lsqlite3_private *lsqlite3 = module->private_data;
765 /* ignore ltdb specials */
766 if (ldb_dn_is_special(dn)) {
770 /* create a local ctx */
771 local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_get_eid local context");
772 if (local_ctx == NULL) {
776 cdn = ldb_dn_alloc_casefold(local_ctx, dn);
779 eid = lsqlite3_get_eid_ndn(lsqlite3->sqlite, local_ctx, cdn);
782 talloc_free(local_ctx);
787 * Interface functions referenced by lsqlite3_ops
790 /* search for matching records, by tree */
791 int lsql_search(struct ldb_module *module, struct ldb_request *req)
793 struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
794 struct lsql_context *lsql_ac;
801 lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
803 if ((( ! ldb_dn_is_valid(req->op.search.base)) || ldb_dn_is_null(req->op.search.base)) &&
804 (req->op.search.scope == LDB_SCOPE_BASE || req->op.search.scope == LDB_SCOPE_ONELEVEL))
805 return LDB_ERR_OPERATIONS_ERROR;
807 if (req->op.search.base) {
808 norm_basedn = ldb_dn_alloc_casefold(lsql_ac, req->op.search.base);
809 if (norm_basedn == NULL) {
810 ret = LDB_ERR_INVALID_DN_SYNTAX;
813 } else norm_basedn = talloc_strdup(lsql_ac, "");
815 /* Convert filter into a series of SQL conditions (constraints) */
816 sqlfilter = parsetree_to_sql(module, lsql_ac, req->op.search.tree);
818 switch(req->op.search.scope) {
819 case LDB_SCOPE_DEFAULT:
820 case LDB_SCOPE_SUBTREE:
821 if (*norm_basedn != '\0') {
822 query = lsqlite3_tprintf(lsql_ac,
823 "SELECT entry.eid,\n"
827 " FROM ldb_entry AS entry\n"
829 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
830 " ON av.eid = entry.eid\n"
832 " WHERE entry.eid IN\n"
833 " (SELECT DISTINCT ldb_entry.eid\n"
835 " WHERE (ldb_entry.norm_dn GLOB('*,%q')\n"
836 " OR ldb_entry.norm_dn = '%q')\n"
837 " AND ldb_entry.eid IN\n"
841 " ORDER BY entry.eid ASC;",
846 query = lsqlite3_tprintf(lsql_ac,
847 "SELECT entry.eid,\n"
851 " FROM ldb_entry AS entry\n"
853 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
854 " ON av.eid = entry.eid\n"
856 " WHERE entry.eid IN\n"
857 " (SELECT DISTINCT ldb_entry.eid\n"
859 " WHERE ldb_entry.eid IN\n"
863 " ORDER BY entry.eid ASC;",
870 query = lsqlite3_tprintf(lsql_ac,
871 "SELECT entry.eid,\n"
875 " FROM ldb_entry AS entry\n"
877 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
878 " ON av.eid = entry.eid\n"
880 " WHERE entry.eid IN\n"
881 " (SELECT DISTINCT ldb_entry.eid\n"
883 " WHERE ldb_entry.norm_dn = '%q'\n"
884 " AND ldb_entry.eid IN\n"
888 " ORDER BY entry.eid ASC;",
893 case LDB_SCOPE_ONELEVEL:
894 query = lsqlite3_tprintf(lsql_ac,
895 "SELECT entry.eid,\n"
899 " FROM ldb_entry AS entry\n"
901 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
902 " ON av.eid = entry.eid\n"
904 " WHERE entry.eid IN\n"
905 " (SELECT DISTINCT ldb_entry.eid\n"
907 " WHERE norm_dn GLOB('*,%q')\n"
908 " AND NOT norm_dn GLOB('*,*,%q')\n"
909 " AND ldb_entry.eid IN\n(%s)\n"
912 " ORDER BY entry.eid ASC;",
924 printf ("%s\n", query);
927 lsql_ac->current_eid = 0;
928 lsql_ac->attrs = req->op.search.attrs;
929 lsql_ac->ares = NULL;
931 req->handle->state = LDB_ASYNC_PENDING;
933 ret = sqlite3_exec(lsqlite3->sqlite, query, lsqlite3_search_callback, req->handle, &errmsg);
934 if (ret != SQLITE_OK) {
936 ldb_set_errstring(module->ldb, errmsg);
942 /* complete the last message if any */
944 lsql_ac->ares->message = ldb_msg_canonicalize(module->ldb, lsql_ac->ares->message);
945 if (lsql_ac->ares->message == NULL)
948 req->handle->status = lsql_ac->callback(module->ldb, lsql_ac->context, lsql_ac->ares);
949 if (req->handle->status != LDB_SUCCESS)
953 req->handle->state = LDB_ASYNC_DONE;
958 return LDB_ERR_OPERATIONS_ERROR;
962 static int lsql_add(struct ldb_module *module, struct ldb_request *req)
964 struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
965 struct lsql_context *lsql_ac;
966 struct ldb_message *msg = req->op.add.message;
972 int ret = LDB_SUCCESS;
974 lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
975 req->handle->state = LDB_ASYNC_DONE;
976 req->handle->status = LDB_SUCCESS;
978 /* See if this is an ltdb special */
979 if (ldb_dn_is_special(msg->dn)) {
982 c = ldb_dn_new(local_ctx, module->ldb, "@INDEXLIST");
983 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
984 #warning "should we handle indexes somehow ?"
985 ret = LDB_ERR_UNWILLING_TO_PERFORM;
989 /* Others return an error */
990 ret = LDB_ERR_UNWILLING_TO_PERFORM;
994 /* create linearized and normalized dns */
995 dn = ldb_dn_alloc_linearized(lsql_ac, msg->dn);
996 ndn = ldb_dn_alloc_casefold(lsql_ac, msg->dn);
997 if (dn == NULL || ndn == NULL) {
1002 query = lsqlite3_tprintf(lsql_ac,
1004 "INSERT OR ABORT INTO ldb_entry "
1005 "('dn', 'norm_dn') "
1006 "VALUES ('%q', '%q');",
1008 if (query == NULL) {
1009 ret = LDB_ERR_OTHER;
1013 ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1014 if (ret != SQLITE_OK) {
1016 ldb_set_errstring(module->ldb, errmsg);
1019 ret = LDB_ERR_OTHER;
1023 eid = lsqlite3_get_eid_ndn(lsqlite3->sqlite, lsql_ac, ndn);
1025 ret = LDB_ERR_OTHER;
1029 for (i = 0; i < msg->num_elements; i++) {
1030 const struct ldb_message_element *el = &msg->elements[i];
1031 const struct ldb_schema_attribute *a;
1035 /* Get a case-folded copy of the attribute name */
1036 attr = ldb_attr_casefold(lsql_ac, el->name);
1038 ret = LDB_ERR_OTHER;
1042 a = ldb_schema_attribute_by_name(module->ldb, el->name);
1044 /* For each value of the specified attribute name... */
1045 for (j = 0; j < el->num_values; j++) {
1046 struct ldb_val value;
1049 /* Get a canonicalised copy of the data */
1050 a->syntax->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
1051 if (value.data == NULL) {
1052 ret = LDB_ERR_OTHER;
1056 insert = lsqlite3_tprintf(lsql_ac,
1057 "INSERT OR ROLLBACK INTO ldb_attribute_values "
1058 "('eid', 'attr_name', 'norm_attr_name',"
1059 " 'attr_value', 'norm_attr_value') "
1060 "VALUES ('%lld', '%q', '%q', '%q', '%q');",
1061 eid, el->name, attr,
1062 el->values[j].data, value.data);
1063 if (insert == NULL) {
1064 ret = LDB_ERR_OTHER;
1068 ret = sqlite3_exec(lsqlite3->sqlite, insert, NULL, NULL, &errmsg);
1069 if (ret != SQLITE_OK) {
1071 ldb_set_errstring(module->ldb, errmsg);
1074 ret = LDB_ERR_OTHER;
1080 if (lsql_ac->callback) {
1081 req->handle->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1085 req->handle->state = LDB_ASYNC_DONE;
1089 /* modify a record */
1090 static int lsql_modify(struct ldb_module *module, struct ldb_request *req)
1092 struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1093 struct lsql_context *lsql_ac;
1094 struct ldb_message *msg = req->op.mod.message;
1098 int ret = LDB_SUCCESS;
1100 lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
1101 req->handle->state = LDB_ASYNC_DONE;
1102 req->handle->status = LDB_SUCCESS;
1104 /* See if this is an ltdb special */
1105 if (ldb_dn_is_special(msg->dn)) {
1106 /* Others return an error */
1107 ret = LDB_ERR_UNWILLING_TO_PERFORM;
1111 eid = lsqlite3_get_eid(module, msg->dn);
1113 ret = LDB_ERR_OTHER;
1117 for (i = 0; i < msg->num_elements; i++) {
1118 const struct ldb_message_element *el = &msg->elements[i];
1119 const struct ldb_schema_attribute *a;
1120 int flags = el->flags & LDB_FLAG_MOD_MASK;
1125 /* Get a case-folded copy of the attribute name */
1126 attr = ldb_attr_casefold(lsql_ac, el->name);
1128 ret = LDB_ERR_OTHER;
1132 a = ldb_schema_attribute_by_name(module->ldb, el->name);
1136 case LDB_FLAG_MOD_REPLACE:
1138 /* remove all attributes before adding the replacements */
1139 mod = lsqlite3_tprintf(lsql_ac,
1140 "DELETE FROM ldb_attribute_values "
1141 "WHERE eid = '%lld' "
1142 "AND norm_attr_name = '%q';",
1145 ret = LDB_ERR_OTHER;
1149 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1150 if (ret != SQLITE_OK) {
1152 ldb_set_errstring(module->ldb, errmsg);
1155 ret = LDB_ERR_OTHER;
1159 /* MISSING break is INTENTIONAL */
1161 case LDB_FLAG_MOD_ADD:
1162 #warning "We should throw an error if no value is provided!"
1163 /* For each value of the specified attribute name... */
1164 for (j = 0; j < el->num_values; j++) {
1165 struct ldb_val value;
1167 /* Get a canonicalised copy of the data */
1168 a->syntax->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
1169 if (value.data == NULL) {
1170 ret = LDB_ERR_OTHER;
1174 mod = lsqlite3_tprintf(lsql_ac,
1175 "INSERT OR ROLLBACK INTO ldb_attribute_values "
1176 "('eid', 'attr_name', 'norm_attr_name',"
1177 " 'attr_value', 'norm_attr_value') "
1178 "VALUES ('%lld', '%q', '%q', '%q', '%q');",
1179 eid, el->name, attr,
1180 el->values[j].data, value.data);
1183 ret = LDB_ERR_OTHER;
1187 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1188 if (ret != SQLITE_OK) {
1190 ldb_set_errstring(module->ldb, errmsg);
1193 ret = LDB_ERR_OTHER;
1200 case LDB_FLAG_MOD_DELETE:
1201 #warning "We should throw an error if the attribute we are trying to delete does not exist!"
1202 if (el->num_values == 0) {
1203 mod = lsqlite3_tprintf(lsql_ac,
1204 "DELETE FROM ldb_attribute_values "
1205 "WHERE eid = '%lld' "
1206 "AND norm_attr_name = '%q';",
1209 ret = LDB_ERR_OTHER;
1213 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1214 if (ret != SQLITE_OK) {
1216 ldb_set_errstring(module->ldb, errmsg);
1219 ret = LDB_ERR_OTHER;
1224 /* For each value of the specified attribute name... */
1225 for (j = 0; j < el->num_values; j++) {
1226 struct ldb_val value;
1228 /* Get a canonicalised copy of the data */
1229 a->syntax->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
1230 if (value.data == NULL) {
1231 ret = LDB_ERR_OTHER;
1235 mod = lsqlite3_tprintf(lsql_ac,
1236 "DELETE FROM ldb_attribute_values "
1237 "WHERE eid = '%lld' "
1238 "AND norm_attr_name = '%q' "
1239 "AND norm_attr_value = '%q';",
1240 eid, attr, value.data);
1243 ret = LDB_ERR_OTHER;
1247 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1248 if (ret != SQLITE_OK) {
1250 ldb_set_errstring(module->ldb, errmsg);
1253 ret = LDB_ERR_OTHER;
1262 if (lsql_ac->callback) {
1263 req->handle->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1267 req->handle->state = LDB_ASYNC_DONE;
1271 /* delete a record */
1272 static int lsql_delete(struct ldb_module *module, struct ldb_request *req)
1274 struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1275 struct lsql_context *lsql_ac;
1279 int ret = LDB_SUCCESS;
1282 lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
1283 req->handle->state = LDB_ASYNC_DONE;
1284 req->handle->status = LDB_SUCCESS;
1286 eid = lsqlite3_get_eid(module, req->op.del.dn);
1291 query = lsqlite3_tprintf(lsql_ac,
1293 "DELETE FROM ldb_entry WHERE eid = %lld; "
1294 /* Delete attributes */
1295 "DELETE FROM ldb_attribute_values WHERE eid = %lld; ",
1297 if (query == NULL) {
1298 ret = LDB_ERR_OTHER;
1302 ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1303 if (ret != SQLITE_OK) {
1305 ldb_set_errstring(module->ldb, errmsg);
1308 req->handle->status = LDB_ERR_OPERATIONS_ERROR;
1312 if (lsql_ac->callback) {
1313 ret = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1317 req->handle->state = LDB_ASYNC_DONE;
1321 /* rename a record */
1322 static int lsql_rename(struct ldb_module *module, struct ldb_request *req)
1324 struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1325 struct lsql_context *lsql_ac;
1326 char *new_dn, *new_cdn, *old_cdn;
1329 int ret = LDB_SUCCESS;
1331 lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
1332 req->handle->state = LDB_ASYNC_DONE;
1333 req->handle->status = LDB_SUCCESS;
1335 /* create linearized and normalized dns */
1336 old_cdn = ldb_dn_alloc_casefold(lsql_ac, req->op.rename.olddn);
1337 new_cdn = ldb_dn_alloc_casefold(lsql_ac, req->op.rename.newdn);
1338 new_dn = ldb_dn_alloc_linearized(lsql_ac, req->op.rename.newdn);
1339 if (old_cdn == NULL || new_cdn == NULL || new_dn == NULL) {
1343 /* build the SQL query */
1344 query = lsqlite3_tprintf(lsql_ac,
1345 "UPDATE ldb_entry SET dn = '%q', norm_dn = '%q' "
1346 "WHERE norm_dn = '%q';",
1347 new_dn, new_cdn, old_cdn);
1348 if (query == NULL) {
1353 ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1354 if (ret != SQLITE_OK) {
1356 ldb_set_errstring(module->ldb, errmsg);
1359 ret = LDB_ERR_OPERATIONS_ERROR;
1363 if (lsql_ac->callback) {
1364 ret = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1368 req->handle->state = LDB_ASYNC_DONE;
1372 static int lsql_start_trans(struct ldb_module * module)
1376 struct lsqlite3_private * lsqlite3 = module->private_data;
1378 if (lsqlite3->trans_count == 0) {
1379 ret = sqlite3_exec(lsqlite3->sqlite, "BEGIN IMMEDIATE;", NULL, NULL, &errmsg);
1380 if (ret != SQLITE_OK) {
1382 printf("lsqlite3_start_trans: error: %s\n", errmsg);
1389 lsqlite3->trans_count++;
1394 static int lsql_end_trans(struct ldb_module *module)
1398 struct lsqlite3_private *lsqlite3 = module->private_data;
1400 if (lsqlite3->trans_count > 0) {
1401 lsqlite3->trans_count--;
1404 if (lsqlite3->trans_count == 0) {
1405 ret = sqlite3_exec(lsqlite3->sqlite, "COMMIT;", NULL, NULL, &errmsg);
1406 if (ret != SQLITE_OK) {
1408 printf("lsqlite3_end_trans: error: %s\n", errmsg);
1418 static int lsql_del_trans(struct ldb_module *module)
1420 struct lsqlite3_private *lsqlite3 = module->private_data;
1422 if (lsqlite3->trans_count > 0) {
1423 lsqlite3->trans_count--;
1426 if (lsqlite3->trans_count == 0) {
1427 return lsqlite3_safe_rollback(lsqlite3->sqlite);
1433 static int destructor(struct lsqlite3_private *lsqlite3)
1435 if (lsqlite3->sqlite) {
1436 sqlite3_close(lsqlite3->sqlite);
1441 static int lsql_request(struct ldb_module *module, struct ldb_request *req)
1443 return LDB_ERR_OPERATIONS_ERROR;
1446 static int lsql_run_request(struct ldb_module *module, struct ldb_request *req)
1448 switch (req->operation) {
1450 return lsql_search(module, req);
1453 return lsql_add(module, req);
1456 return lsql_modify(module, req);
1459 return lsql_delete(module, req);
1462 return lsql_rename(module, req);
1465 case LDB_SEQUENCE_NUMBER:
1466 return lsql_sequence_number(module, req);
1470 return lsql_request(module, req);
1474 return LDB_ERR_OPERATIONS_ERROR;
1477 static int lsql_handle_request(struct ldb_module *module, struct ldb_request *req)
1479 struct lsql_context *ac;
1481 if (check_critical_controls(req->controls)) {
1482 return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
1485 ac = talloc_zero(req, struct lsql_context);
1487 ldb_set_errstring(module->ldb, "Out of Memory");
1488 return LDB_ERR_OPERATIONS_ERROR;
1491 ac->module = module;
1494 req->handle = ldb_handle_new(req, lsql_run_request, ac);
1495 if (req->handle == NULL) {
1497 return LDB_ERR_OPERATIONS_ERROR;
1504 * Table of operations for the sqlite3 backend
1506 static const struct ldb_module_ops lsqlite3_ops = {
1508 .search = lsql_handle_request,
1509 .add = lsql_handle_request,
1510 .modify = lsql_handle_request,
1511 .del = lsql_handle_request,
1512 .rename = lsql_handle_request,
1513 .request = lsql_handle_request,
1514 .start_transaction = lsql_start_trans,
1515 .end_transaction = lsql_end_trans,
1516 .del_transaction = lsql_del_trans,
1517 /* TODO: .sequence_number = lsql_handle_request */
1524 static int initialize(struct lsqlite3_private *lsqlite3,
1525 struct ldb_context *ldb, const char *url, int flags)
1527 TALLOC_CTX *local_ctx;
1534 /* create a local ctx */
1535 local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_rename local context");
1536 if (local_ctx == NULL) {
1540 schema = lsqlite3_tprintf(local_ctx,
1543 "CREATE TABLE ldb_info AS "
1544 " SELECT 'LDB' AS database_type,"
1545 " '1.0' AS version;"
1548 * The entry table holds the information about an entry.
1549 * This table is used to obtain the EID of the entry and to
1550 * support scope=one and scope=base. The parent and child
1551 * table is included in the entry table since all the other
1552 * attributes are dependent on EID.
1554 "CREATE TABLE ldb_entry "
1556 " eid INTEGER PRIMARY KEY AUTOINCREMENT,"
1557 " dn TEXT UNIQUE NOT NULL,"
1558 " norm_dn TEXT UNIQUE NOT NULL"
1562 "CREATE TABLE ldb_object_classes"
1564 " class_name TEXT PRIMARY KEY,"
1565 " parent_class_name TEXT,"
1566 " tree_key TEXT UNIQUE,"
1567 " max_child_num INTEGER DEFAULT 0"
1571 * We keep a full listing of attribute/value pairs here
1573 "CREATE TABLE ldb_attribute_values"
1575 " eid INTEGER REFERENCES ldb_entry,"
1577 " norm_attr_name TEXT,"
1579 " norm_attr_value TEXT "
1586 "CREATE INDEX ldb_attribute_values_eid_idx "
1587 " ON ldb_attribute_values (eid);"
1589 "CREATE INDEX ldb_attribute_values_name_value_idx "
1590 " ON ldb_attribute_values (attr_name, norm_attr_value);"
1598 "CREATE TRIGGER ldb_object_classes_insert_tr"
1600 " ON ldb_object_classes"
1603 " UPDATE ldb_object_classes"
1604 " SET tree_key = COALESCE(tree_key, "
1606 " SELECT tree_key || "
1607 " (SELECT base160(max_child_num + 1)"
1608 " FROM ldb_object_classes"
1609 " WHERE class_name = "
1610 " new.parent_class_name)"
1611 " FROM ldb_object_classes "
1612 " WHERE class_name = new.parent_class_name "
1614 " UPDATE ldb_object_classes "
1615 " SET max_child_num = max_child_num + 1"
1616 " WHERE class_name = new.parent_class_name;"
1620 * Table initialization
1623 "INSERT INTO ldb_object_classes "
1624 " (class_name, tree_key) "
1626 " ('TOP', '0001');");
1628 /* Skip protocol indicator of url */
1629 if (strncmp(url, "sqlite3://", 10) != 0) {
1630 return SQLITE_MISUSE;
1633 /* Update pointer to just after the protocol indicator */
1636 /* Try to open the (possibly empty/non-existent) database */
1637 if ((ret = sqlite3_open(url, &lsqlite3->sqlite)) != SQLITE_OK) {
1641 /* In case this is a new database, enable auto_vacuum */
1642 ret = sqlite3_exec(lsqlite3->sqlite, "PRAGMA auto_vacuum = 1;", NULL, NULL, &errmsg);
1643 if (ret != SQLITE_OK) {
1645 printf("lsqlite3 initializaion error: %s\n", errmsg);
1651 if (flags & LDB_FLG_NOSYNC) {
1653 ret = sqlite3_exec(lsqlite3->sqlite, "PRAGMA synchronous = OFF;", NULL, NULL, &errmsg);
1654 if (ret != SQLITE_OK) {
1656 printf("lsqlite3 initializaion error: %s\n", errmsg);
1665 /* Establish a busy timeout of 30 seconds */
1666 if ((ret = sqlite3_busy_timeout(lsqlite3->sqlite,
1667 30000)) != SQLITE_OK) {
1671 /* Create a function, callable from sql, to increment a tree_key */
1673 sqlite3_create_function(lsqlite3->sqlite,/* handle */
1674 "base160_next", /* function name */
1675 1, /* number of args */
1676 SQLITE_ANY, /* preferred text type */
1677 NULL, /* user data */
1678 base160next_sql, /* called func */
1679 NULL, /* step func */
1680 NULL /* final func */
1685 /* Create a function, callable from sql, to convert int to base160 */
1687 sqlite3_create_function(lsqlite3->sqlite,/* handle */
1688 "base160", /* function name */
1689 1, /* number of args */
1690 SQLITE_ANY, /* preferred text type */
1691 NULL, /* user data */
1692 base160_sql, /* called func */
1693 NULL, /* step func */
1694 NULL /* final func */
1699 /* Create a function, callable from sql, to perform various comparisons */
1701 sqlite3_create_function(lsqlite3->sqlite, /* handle */
1702 "ldap_compare", /* function name */
1703 4, /* number of args */
1704 SQLITE_ANY, /* preferred text type */
1705 ldb , /* user data */
1706 lsqlite3_compare, /* called func */
1707 NULL, /* step func */
1708 NULL /* final func */
1713 /* Begin a transaction */
1714 ret = sqlite3_exec(lsqlite3->sqlite, "BEGIN EXCLUSIVE;", NULL, NULL, &errmsg);
1715 if (ret != SQLITE_OK) {
1717 printf("lsqlite3: initialization error: %s\n", errmsg);
1724 /* Determine if this is a new database. No tables means it is. */
1725 if (query_int(lsqlite3,
1728 " FROM sqlite_master\n"
1729 " WHERE type = 'table';") != 0) {
1733 if (queryInt == 0) {
1735 * Create the database schema
1737 ret = sqlite3_exec(lsqlite3->sqlite, schema, NULL, NULL, &errmsg);
1738 if (ret != SQLITE_OK) {
1740 printf("lsqlite3 initializaion error: %s\n", errmsg);
1747 * Ensure that the database we opened is one of ours
1749 if (query_int(lsqlite3,
1752 " (SELECT COUNT(*) = 2"
1753 " FROM sqlite_master "
1754 " WHERE type = 'table' "
1758 " 'ldb_object_classes' "
1764 " WHERE database_type = 'LDB' "
1765 " AND version = '1.0'"
1769 /* It's not one that we created. See ya! */
1774 /* Commit the transaction */
1775 ret = sqlite3_exec(lsqlite3->sqlite, "COMMIT;", NULL, NULL, &errmsg);
1776 if (ret != SQLITE_OK) {
1778 printf("lsqlite3: iniialization error: %s\n", errmsg);
1787 if (rollback) lsqlite3_safe_rollback(lsqlite3->sqlite);
1788 sqlite3_close(lsqlite3->sqlite);
1793 * connect to the database
1795 static int lsqlite3_connect(struct ldb_context *ldb,
1798 const char *options[],
1799 struct ldb_module **module)
1803 struct lsqlite3_private * lsqlite3 = NULL;
1805 lsqlite3 = talloc(ldb, struct lsqlite3_private);
1810 lsqlite3->sqlite = NULL;
1811 lsqlite3->options = NULL;
1812 lsqlite3->trans_count = 0;
1814 ret = initialize(lsqlite3, ldb, url, flags);
1815 if (ret != SQLITE_OK) {
1819 talloc_set_destructor(lsqlite3, destructor);
1823 *module = talloc(ldb, struct ldb_module);
1828 talloc_set_name_const(*module, "ldb_sqlite3 backend");
1829 (*module)->ldb = ldb;
1830 (*module)->prev = (*module)->next = NULL;
1831 (*module)->private_data = lsqlite3;
1832 (*module)->ops = &lsqlite3_ops;
1836 * take a copy of the options array, so we don't have to rely
1837 * on the caller keeping it around (it might be dynamic)
1839 for (i=0;options[i];i++) ;
1841 lsqlite3->options = talloc_array(lsqlite3, char *, i+1);
1842 if (!lsqlite3->options) {
1846 for (i=0;options[i];i++) {
1848 lsqlite3->options[i+1] = NULL;
1849 lsqlite3->options[i] =
1850 talloc_strdup(lsqlite3->options, options[i]);
1851 if (!lsqlite3->options[i]) {
1860 if (lsqlite3->sqlite != NULL) {
1861 (void) sqlite3_close(lsqlite3->sqlite);
1863 talloc_free(lsqlite3);
1867 const struct ldb_backend_ops ldb_sqlite3_backend_ops = {
1869 .connect_fn = lsqlite3_connect