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
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)
38 #include "ldb/include/ldb.h"
39 #include "ldb/include/ldb_errors.h"
40 #include "ldb/include/ldb_private.h"
41 #include "ldb/ldb_sqlite3/ldb_sqlite3.h"
44 * Macros used throughout
49 # define TRUE (! FALSE)
52 #define RESULT_ATTR_TABLE "temp_result_attrs"
54 //#define TEMPTAB /* for testing, create non-temporary table */
55 #define TEMPTAB "TEMPORARY"
60 sqlite3_stmt * stmtGetEID = NULL;
62 static char *lsqlite3_tprintf(TALLOC_CTX *mem_ctx, const char *fmt, ...)
68 str = sqlite3_vmprintf(fmt, ap);
71 if (str == NULL) return NULL;
73 ret = talloc_strdup(mem_ctx, str);
83 static unsigned char base160tab[161] = {
84 48 ,49 ,50 ,51 ,52 ,53 ,54 ,55 ,56 ,57 , /* 0-9 */
85 58 ,59 ,65 ,66 ,67 ,68 ,69 ,70 ,71 ,72 , /* : ; A-H */
86 73 ,74 ,75 ,76 ,77 ,78 ,79 ,80 ,81 ,82 , /* I-R */
87 83 ,84 ,85 ,86 ,87 ,88 ,89 ,90 ,97 ,98 , /* S-Z , a-b */
88 99 ,100,101,102,103,104,105,106,107,108, /* c-l */
89 109,110,111,112,113,114,115,116,117,118, /* m-v */
90 119,120,121,122,160,161,162,163,164,165, /* w-z, latin1 */
91 166,167,168,169,170,171,172,173,174,175, /* latin1 */
92 176,177,178,179,180,181,182,183,184,185, /* latin1 */
93 186,187,188,189,190,191,192,193,194,195, /* latin1 */
94 196,197,198,199,200,201,202,203,204,205, /* latin1 */
95 206,207,208,209,210,211,212,213,214,215, /* latin1 */
96 216,217,218,219,220,221,222,223,224,225, /* latin1 */
97 226,227,228,229,230,231,232,233,234,235, /* latin1 */
98 236,237,238,239,240,241,242,243,244,245, /* latin1 */
99 246,247,248,249,250,251,252,253,254,255, /* latin1 */
107 * Convert an unsigned long integer into a base160 representation of the
112 * value to be converted
115 * character array, 5 bytes long, into which the base160 representation
116 * will be placed. The result will be a four-digit representation of the
117 * number (with leading zeros prepended as necessary), and null
124 base160_sql(sqlite3_context * hContext,
126 sqlite3_value ** argv)
132 val = sqlite3_value_int64(argv[0]);
134 for (i = 3; i >= 0; i--) {
136 result[i] = base160tab[val % 160];
142 sqlite3_result_text(hContext, result, -1, SQLITE_TRANSIENT);
149 * This function enhances sqlite by adding a "base160_next()" function which is
150 * accessible via queries.
152 * Retrieve the next-greater number in the base160 sequence for the terminal
153 * tree node (the last four digits). Only one tree level (four digits) is
157 * A character string: either an empty string (in which case no operation is
158 * performed), or a string of base160 digits with a length of a multiple of
162 * Upon return, the trailing four digits (one tree level) will have been
166 base160next_sql(sqlite3_context * hContext,
168 sqlite3_value ** argv)
172 unsigned char * pTab;
173 unsigned char * pBase160 =
174 strdup(sqlite3_value_text(argv[0]));
175 unsigned char * pStart = pBase160;
178 * We need a minimum of four digits, and we will always get a multiple
181 if (pBase160 != NULL &&
182 (len = strlen(pBase160)) >= 4 &&
185 if (pBase160 == NULL) {
187 sqlite3_result_null(hContext);
191 pBase160 += strlen(pBase160) - 1;
193 /* We only carry through four digits: one level in the tree */
194 for (i = 0; i < 4; i++) {
196 /* What base160 value does this digit have? */
197 pTab = strchr(base160tab, *pBase160);
199 /* Is there a carry? */
200 if (pTab < base160tab + sizeof(base160tab) - 1) {
203 * Nope. Just increment this value and we're
211 * There's a carry. This value gets
212 * base160tab[0], we decrement the buffer
213 * pointer to get the next higher-order digit,
214 * and continue in the loop.
216 *pBase160-- = base160tab[0];
220 sqlite3_result_text(hContext,
225 sqlite3_result_value(hContext, argv[0]);
226 if (pBase160 != NULL) {
232 static char *parsetree_to_sql(struct ldb_module *module,
234 const struct ldb_parse_tree *t)
236 const struct ldb_attrib_handler *h;
237 struct ldb_val value, subval;
238 char *wild_card_string;
245 switch(t->operation) {
248 tmp = parsetree_to_sql(module, mem_ctx, t->u.list.elements[0]);
249 if (tmp == NULL) return NULL;
251 for (i = 1; i < t->u.list.num_elements; i++) {
253 child = parsetree_to_sql(module, mem_ctx, t->u.list.elements[i]);
254 if (child == NULL) return NULL;
256 tmp = talloc_asprintf_append(tmp, " INTERSECT %s ", child);
257 if (tmp == NULL) return NULL;
260 ret = talloc_asprintf(mem_ctx, "SELECT * FROM ( %s )\n", tmp);
266 tmp = parsetree_to_sql(module, mem_ctx, t->u.list.elements[0]);
267 if (tmp == NULL) return NULL;
269 for (i = 1; i < t->u.list.num_elements; i++) {
271 child = parsetree_to_sql(module, mem_ctx, t->u.list.elements[i]);
272 if (child == NULL) return NULL;
274 tmp = talloc_asprintf_append(tmp, " UNION %s ", child);
275 if (tmp == NULL) return NULL;
278 return talloc_asprintf(mem_ctx, "SELECT * FROM ( %s ) ", tmp);
282 child = parsetree_to_sql(module, mem_ctx, t->u.isnot.child);
283 if (child == NULL) return NULL;
285 return talloc_asprintf(mem_ctx,
286 "SELECT eid FROM ldb_entry "
287 "WHERE eid NOT IN ( %s ) ", child);
289 case LDB_OP_EQUALITY:
291 * For simple searches, we want to retrieve the list of EIDs that
292 * match the criteria.
294 attr = ldb_casefold(mem_ctx, t->u.equality.attr);
295 if (attr == NULL) return NULL;
296 h = ldb_attrib_handler(module->ldb, attr);
298 /* Get a canonicalised copy of the data */
299 h->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
300 if (value.data == NULL) {
304 if (strcasecmp(t->u.equality.attr, "objectclass") == 0) {
306 * For object classes, we want to search for all objectclasses
307 * that are subclasses as well.
309 return lsqlite3_tprintf(mem_ctx,
310 "SELECT eid FROM ldb_attribute_values\n"
311 "WHERE norm_attr_name = 'OBJECTCLASS' "
312 "AND norm_attr_value IN\n"
313 " (SELECT class_name FROM ldb_object_classes\n"
314 " WHERE tree_key GLOB\n"
315 " (SELECT tree_key FROM ldb_object_classes\n"
316 " WHERE class_name = '%q'\n"
320 } else if (strcasecmp(t->u.equality.attr, "dn") == 0) {
321 /* DN query is a special ldb case */
322 char *cdn = ldb_dn_linearize_casefold(module->ldb,
323 ldb_dn_explode(module->ldb,
326 return lsqlite3_tprintf(mem_ctx,
327 "SELECT eid FROM ldb_entry "
328 "WHERE norm_dn = '%q'", cdn);
331 /* A normal query. */
332 return lsqlite3_tprintf(mem_ctx,
333 "SELECT eid FROM ldb_attribute_values "
334 "WHERE norm_attr_name = '%q' "
335 "AND norm_attr_value = '%q'",
341 case LDB_OP_SUBSTRING:
343 wild_card_string = talloc_strdup(mem_ctx,
344 (t->u.substring.start_with_wildcard)?"*":"");
345 if (wild_card_string == NULL) return NULL;
347 for (i = 0; t->u.substring.chunks[i]; i++) {
348 wild_card_string = talloc_asprintf_append(wild_card_string, "%s*",
349 t->u.substring.chunks[i]->data);
350 if (wild_card_string == NULL) return NULL;
353 if ( ! t->u.substring.end_with_wildcard ) {
354 /* remove last wildcard */
355 wild_card_string[strlen(wild_card_string) - 1] = '\0';
358 attr = ldb_casefold(mem_ctx, t->u.substring.attr);
359 if (attr == NULL) return NULL;
360 h = ldb_attrib_handler(module->ldb, attr);
362 subval.data = wild_card_string;
363 subval.length = strlen(wild_card_string) + 1;
365 /* Get a canonicalised copy of the data */
366 h->canonicalise_fn(module->ldb, mem_ctx, &(subval), &value);
367 if (value.data == NULL) {
371 return lsqlite3_tprintf(mem_ctx,
372 "SELECT eid FROM ldb_attribute_values "
373 "WHERE norm_attr_name = '%q' "
374 "AND norm_attr_value GLOB '%q'",
379 attr = ldb_casefold(mem_ctx, t->u.equality.attr);
380 if (attr == NULL) return NULL;
381 h = ldb_attrib_handler(module->ldb, attr);
383 /* Get a canonicalised copy of the data */
384 h->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
385 if (value.data == NULL) {
389 return lsqlite3_tprintf(mem_ctx,
390 "SELECT eid FROM ldb_attribute_values "
391 "WHERE norm_attr_name = '%q' "
392 "AND ldap_compare(norm_attr_value, '>=', '%q', '%q') ",
398 attr = ldb_casefold(mem_ctx, t->u.equality.attr);
399 if (attr == NULL) return NULL;
400 h = ldb_attrib_handler(module->ldb, attr);
402 /* Get a canonicalised copy of the data */
403 h->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
404 if (value.data == NULL) {
408 return lsqlite3_tprintf(mem_ctx,
409 "SELECT eid FROM ldb_attribute_values "
410 "WHERE norm_attr_name = '%q' "
411 "AND ldap_compare(norm_attr_value, '<=', '%q', '%q') ",
417 if (strcasecmp(t->u.present.attr, "dn") == 0) {
418 return talloc_strdup(mem_ctx, "SELECT eid FROM ldb_entry");
421 attr = ldb_casefold(mem_ctx, t->u.present.attr);
422 if (attr == NULL) return NULL;
424 return lsqlite3_tprintf(mem_ctx,
425 "SELECT eid FROM ldb_attribute_values "
426 "WHERE norm_attr_name = '%q' ",
430 attr = ldb_casefold(mem_ctx, t->u.equality.attr);
431 if (attr == NULL) return NULL;
432 h = ldb_attrib_handler(module->ldb, attr);
434 /* Get a canonicalised copy of the data */
435 h->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
436 if (value.data == NULL) {
440 return lsqlite3_tprintf(mem_ctx,
441 "SELECT eid FROM ldb_attribute_values "
442 "WHERE norm_attr_name = '%q' "
443 "AND ldap_compare(norm_attr_value, '~%', 'q', '%q') ",
448 case LDB_OP_EXTENDED:
449 #warning "work out how to handle bitops"
456 /* should never occur */
464 * This function is used for the common case of queries that return a single
467 * NOTE: If more than one value is returned by the query, all but the first
468 * one will be ignored.
471 query_int(const struct lsqlite3_private * lsqlite3,
479 sqlite3_stmt * pStmt;
482 /* Begin access to variable argument list */
483 va_start(args, pSql);
485 /* Format the query */
486 if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
491 * Prepare and execute the SQL statement. Loop allows retrying on
492 * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
493 * requiring retrying the operation.
495 for (bLoop = TRUE; bLoop; ) {
497 /* Compile the SQL statement into sqlite virtual machine */
498 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
502 NULL)) == SQLITE_SCHEMA) {
503 if (stmtGetEID != NULL) {
504 sqlite3_finalize(stmtGetEID);
508 } else if (ret != SQLITE_OK) {
512 /* One row expected */
513 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
514 if (stmtGetEID != NULL) {
515 sqlite3_finalize(stmtGetEID);
518 (void) sqlite3_finalize(pStmt);
520 } else if (ret != SQLITE_ROW) {
521 (void) sqlite3_finalize(pStmt);
525 /* Get the value to be returned */
526 *pRet = sqlite3_column_int64(pStmt, 0);
528 /* Free the virtual machine */
529 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
530 if (stmtGetEID != NULL) {
531 sqlite3_finalize(stmtGetEID);
535 } else if (ret != SQLITE_OK) {
536 (void) sqlite3_finalize(pStmt);
541 * Normal condition is only one time through loop. Loop is
542 * rerun in error conditions, via "continue", above.
547 /* All done with variable argument list */
551 /* Free the memory we allocated for our query string */
558 * This is a bad hack to support ldap style comparisons whithin sqlite.
559 * val is the attribute in the row currently under test
560 * func is the desired test "<=" ">=" "~" ":"
561 * cmp is the value to compare against (eg: "test")
562 * attr is the attribute name the value of which we want to test
565 static void lsqlite3_compare(sqlite3_context *ctx, int argc,
566 sqlite3_value **argv)
568 struct ldb_context *ldb = (struct ldb_context *)sqlite3_user_data(ctx);
569 const unsigned char *val = sqlite3_value_text(argv[0]);
570 const unsigned char *func = sqlite3_value_text(argv[1]);
571 const unsigned char *cmp = sqlite3_value_text(argv[2]);
572 const unsigned char *attr = sqlite3_value_text(argv[3]);
573 const struct ldb_attrib_handler *h;
581 h = ldb_attrib_handler(ldb, attr);
583 valX.length = strlen(cmp);
585 valY.length = strlen(val);
586 ret = h->comparison_fn(ldb, ldb, &valY, &valX);
588 sqlite3_result_int(ctx, 1);
590 sqlite3_result_int(ctx, 0);
595 h = ldb_attrib_handler(ldb, attr);
597 valX.length = strlen(cmp);
599 valY.length = strlen(val);
600 ret = h->comparison_fn(ldb, ldb, &valY, &valX);
602 sqlite3_result_int(ctx, 1);
604 sqlite3_result_int(ctx, 0);
610 sqlite3_result_int(ctx, 0);
616 sqlite3_result_int(ctx, 0);
623 sqlite3_result_error(ctx, "Value must start with a special operation char (<>~:)!", -1);
628 /* rename a record */
629 static int lsqlite3_safe_rollback(sqlite3 *sqlite)
635 ret = sqlite3_exec(sqlite, "ROLLBACK;", NULL, NULL, &errmsg);
636 if (ret != SQLITE_OK) {
638 printf("lsqlite3_safe_rollback: Error: %s\n", errmsg);
647 /* return an eid as result */
648 static int lsqlite3_eid_callback(void *result, int col_num, char **cols, char **names)
650 long long *eid = (long long *)result;
652 if (col_num != 1) return SQLITE_ABORT;
653 if (strcasecmp(names[0], "eid") != 0) return SQLITE_ABORT;
655 *eid = atoll(cols[0]);
659 struct lsqlite3_msgs {
661 struct ldb_message **msgs;
662 long long current_eid;
663 const char * const * attrs;
668 * add a single set of ldap message values to a ldb_message
671 static int lsqlite3_search_callback(void *result, int col_num, char **cols, char **names)
673 struct lsqlite3_msgs *msgs = (struct lsqlite3_msgs *)result;
674 struct ldb_message *msg;
678 /* eid, dn, attr_name, attr_value */
679 if (col_num != 4) return SQLITE_ABORT;
681 eid = atoll(cols[0]);
683 if (eid != msgs->current_eid) {
684 msgs->msgs = talloc_realloc(msgs->mem_ctx,
686 struct ldb_message *,
688 if (msgs->msgs == NULL) return SQLITE_ABORT;
690 msgs->msgs[msgs->count] = talloc(msgs->msgs, struct ldb_message);
691 if (msgs->msgs[msgs->count] == NULL) return SQLITE_ABORT;
693 msgs->msgs[msgs->count]->dn = NULL;
694 msgs->msgs[msgs->count]->num_elements = 0;
695 msgs->msgs[msgs->count]->elements = NULL;
696 msgs->msgs[msgs->count]->private_data = NULL;
699 msgs->current_eid = eid;
702 msg = msgs->msgs[msgs->count -1];
704 if (msg->dn == NULL) {
705 msg->dn = ldb_dn_explode(msg, cols[1]);
706 if (msg->dn == NULL) return SQLITE_ABORT;
711 for (i = 0; msgs->attrs[i]; i++) {
712 if (strcasecmp(cols[2], msgs->attrs[i]) == 0) {
717 if (!found) return 0;
720 msg->elements = talloc_realloc(msg,
722 struct ldb_message_element,
723 msg->num_elements + 1);
724 if (msg->elements == NULL) return SQLITE_ABORT;
726 msg->elements[msg->num_elements].flags = 0;
727 msg->elements[msg->num_elements].name = talloc_strdup(msg->elements, cols[2]);
728 if (msg->elements[msg->num_elements].name == NULL) return SQLITE_ABORT;
730 msg->elements[msg->num_elements].num_values = 1;
731 msg->elements[msg->num_elements].values = talloc_array(msg->elements,
733 if (msg->elements[msg->num_elements].values == NULL) return SQLITE_ABORT;
735 msg->elements[msg->num_elements].values[0].length = strlen(cols[3]);
736 msg->elements[msg->num_elements].values[0].data = talloc_strdup(msg->elements, cols[3]);
737 if (msg->elements[msg->num_elements].values[0].data == NULL) return SQLITE_ABORT;
747 * lsqlite3_get_eid_ndn()
749 * These functions are used for the very common case of retrieving an EID value
750 * given a (normalized) DN.
753 static long long lsqlite3_get_eid_ndn(sqlite3 *sqlite, void *mem_ctx, const char *norm_dn)
761 query = lsqlite3_tprintf(mem_ctx, "SELECT eid "
763 "WHERE norm_dn = '%q';", norm_dn);
764 if (query == NULL) return -1;
766 ret = sqlite3_exec(sqlite, query, lsqlite3_eid_callback, &eid, &errmsg);
767 if (ret != SQLITE_OK) {
769 printf("lsqlite3_get_eid: Fatal Error: %s\n", errmsg);
778 static long long lsqlite3_get_eid(struct ldb_module *module, const struct ldb_dn *dn)
780 TALLOC_CTX *local_ctx;
781 struct lsqlite3_private *lsqlite3 = module->private_data;
785 /* ignore ltdb specials */
786 if (ldb_dn_is_special(dn)) {
790 /* create a local ctx */
791 local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_get_eid local context");
792 if (local_ctx == NULL) {
796 cdn = ldb_dn_linearize(local_ctx, ldb_dn_casefold(module->ldb, dn));
799 eid = lsqlite3_get_eid_ndn(lsqlite3->sqlite, local_ctx, cdn);
802 talloc_free(local_ctx);
807 * Interface functions referenced by lsqlite3_ops
810 /* search for matching records, by tree */
811 static int lsqlite3_search_bytree(struct ldb_module * module, const struct ldb_dn* basedn,
812 enum ldb_scope scope, struct ldb_parse_tree * tree,
813 const char * const * attrs, struct ldb_result ** res)
815 TALLOC_CTX *local_ctx;
816 struct lsqlite3_private *lsqlite3 = module->private_data;
817 struct lsqlite3_msgs msgs;
824 /* create a local ctx */
825 local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_search_bytree local context");
826 if (local_ctx == NULL) {
831 norm_basedn = ldb_dn_linearize(local_ctx, ldb_dn_casefold(module->ldb, basedn));
832 if (norm_basedn == NULL) {
833 ret = LDB_ERR_INVALID_DN_SYNTAX;
836 } else norm_basedn = talloc_strdup(local_ctx, "");
838 if (*norm_basedn == '\0' &&
839 (scope == LDB_SCOPE_BASE || scope == LDB_SCOPE_ONELEVEL)) {
840 ret = LDB_ERR_UNWILLING_TO_PERFORM;
844 /* Convert filter into a series of SQL conditions (constraints) */
845 sqlfilter = parsetree_to_sql(module, local_ctx, tree);
848 case LDB_SCOPE_DEFAULT:
849 case LDB_SCOPE_SUBTREE:
850 if (*norm_basedn != '\0') {
851 query = lsqlite3_tprintf(local_ctx,
852 "SELECT entry.eid,\n"
856 " FROM ldb_entry AS entry\n"
858 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
859 " ON av.eid = entry.eid\n"
861 " WHERE entry.eid IN\n"
862 " (SELECT DISTINCT ldb_entry.eid\n"
864 " WHERE (ldb_entry.norm_dn GLOB('*,%q')\n"
865 " OR ldb_entry.norm_dn = '%q')\n"
866 " AND ldb_entry.eid IN\n"
870 " ORDER BY entry.eid ASC;",
875 query = lsqlite3_tprintf(local_ctx,
876 "SELECT entry.eid,\n"
880 " FROM ldb_entry AS entry\n"
882 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
883 " ON av.eid = entry.eid\n"
885 " WHERE entry.eid IN\n"
886 " (SELECT DISTINCT ldb_entry.eid\n"
888 " WHERE ldb_entry.eid IN\n"
892 " ORDER BY entry.eid ASC;",
899 query = lsqlite3_tprintf(local_ctx,
900 "SELECT entry.eid,\n"
904 " FROM ldb_entry AS entry\n"
906 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
907 " ON av.eid = entry.eid\n"
909 " WHERE entry.eid IN\n"
910 " (SELECT DISTINCT ldb_entry.eid\n"
912 " WHERE ldb_entry.norm_dn = '%q'\n"
913 " AND ldb_entry.eid IN\n"
917 " ORDER BY entry.eid ASC;",
922 case LDB_SCOPE_ONELEVEL:
923 query = lsqlite3_tprintf(local_ctx,
924 "SELECT entry.eid,\n"
928 " FROM ldb_entry AS entry\n"
930 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
931 " ON av.eid = entry.eid\n"
933 " WHERE entry.eid IN\n"
934 " (SELECT DISTINCT ldb_entry.eid\n"
936 " WHERE norm_dn GLOB('*,%q')\n"
937 " AND NOT norm_dn GLOB('*,*,%q')\n"
938 " AND ldb_entry.eid IN\n(%s)\n"
941 " ORDER BY entry.eid ASC;",
954 printf ("%s\n", query);
959 msgs.current_eid = 0;
960 msgs.mem_ctx = local_ctx;
963 ret = sqlite3_exec(lsqlite3->sqlite, query, lsqlite3_search_callback, &msgs, &errmsg);
964 if (ret != SQLITE_OK) {
966 ldb_set_errstring(module, talloc_strdup(module, errmsg));
973 for (i = 0; i < msgs.count; i++) {
974 msgs.msgs[i] = ldb_msg_canonicalize(module->ldb, msgs.msgs[i]);
975 if (msgs.msgs[i] == NULL) {
980 *res = talloc(module, struct ldb_result);
985 (*res)->msgs = talloc_steal(*res, msgs.msgs);
986 (*res)->count = msgs.count;
987 (*res)->controls = NULL;
989 talloc_free(local_ctx);
992 /* If error, return error code; otherwise return number of results */
994 talloc_free(local_ctx);
995 return LDB_ERR_OTHER;
1000 static int lsqlite3_add(struct ldb_module *module, const struct ldb_message *msg)
1002 TALLOC_CTX *local_ctx;
1003 struct lsqlite3_private *lsqlite3 = module->private_data;
1011 /* create a local ctx */
1012 local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_add local context");
1013 if (local_ctx == NULL) {
1014 return LDB_ERR_OTHER;
1017 /* See if this is an ltdb special */
1018 if (ldb_dn_is_special(msg->dn)) {
1021 c = ldb_dn_explode(local_ctx, "@SUBCLASSES");
1022 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
1023 #warning "insert subclasses into object class tree"
1024 ret = LDB_ERR_UNWILLING_TO_PERFORM;
1029 c = ldb_dn_explode(local_ctx, "@INDEXLIST");
1030 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
1031 #warning "should we handle indexes somehow ?"
1035 /* Others are implicitly ignored */
1039 /* create linearized and normalized dns */
1040 dn = ldb_dn_linearize(local_ctx, msg->dn);
1041 ndn = ldb_dn_linearize(local_ctx, ldb_dn_casefold(module->ldb, msg->dn));
1042 if (dn == NULL || ndn == NULL) {
1043 ret = LDB_ERR_OTHER;
1047 query = lsqlite3_tprintf(local_ctx,
1049 "INSERT OR ABORT INTO ldb_entry "
1050 "('dn', 'norm_dn') "
1051 "VALUES ('%q', '%q');",
1053 if (query == NULL) {
1054 ret = LDB_ERR_OTHER;
1058 ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1059 if (ret != SQLITE_OK) {
1061 ldb_set_errstring(module, talloc_strdup(module, errmsg));
1064 ret = LDB_ERR_OTHER;
1068 eid = lsqlite3_get_eid_ndn(lsqlite3->sqlite, local_ctx, ndn);
1070 ret = LDB_ERR_OTHER;
1074 for (i = 0; i < msg->num_elements; i++) {
1075 const struct ldb_message_element *el = &msg->elements[i];
1076 const struct ldb_attrib_handler *h;
1080 /* Get a case-folded copy of the attribute name */
1081 attr = ldb_casefold(local_ctx, el->name);
1083 ret = LDB_ERR_OTHER;
1087 h = ldb_attrib_handler(module->ldb, el->name);
1089 /* For each value of the specified attribute name... */
1090 for (j = 0; j < el->num_values; j++) {
1091 struct ldb_val value;
1094 /* Get a canonicalised copy of the data */
1095 h->canonicalise_fn(module->ldb, local_ctx, &(el->values[j]), &value);
1096 if (value.data == NULL) {
1097 ret = LDB_ERR_OTHER;
1101 insert = lsqlite3_tprintf(local_ctx,
1102 "INSERT OR ROLLBACK INTO ldb_attribute_values "
1103 "('eid', 'attr_name', 'norm_attr_name',"
1104 " 'attr_value', 'norm_attr_value') "
1105 "VALUES ('%lld', '%q', '%q', '%q', '%q');",
1106 eid, el->name, attr,
1107 el->values[j].data, value.data);
1108 if (insert == NULL) {
1109 ret = LDB_ERR_OTHER;
1113 ret = sqlite3_exec(lsqlite3->sqlite, insert, NULL, NULL, &errmsg);
1114 if (ret != SQLITE_OK) {
1116 ldb_set_errstring(module, talloc_strdup(module, errmsg));
1119 ret = LDB_ERR_OTHER;
1125 talloc_free(local_ctx);
1129 talloc_free(local_ctx);
1134 /* modify a record */
1135 static int lsqlite3_modify(struct ldb_module *module, const struct ldb_message *msg)
1137 TALLOC_CTX *local_ctx;
1138 struct lsqlite3_private *lsqlite3 = module->private_data;
1144 /* create a local ctx */
1145 local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_modify local context");
1146 if (local_ctx == NULL) {
1147 return LDB_ERR_OTHER;
1150 /* See if this is an ltdb special */
1151 if (ldb_dn_is_special(msg->dn)) {
1154 c = ldb_dn_explode(local_ctx, "@SUBCLASSES");
1155 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
1156 #warning "modify subclasses into object class tree"
1157 ret = LDB_ERR_UNWILLING_TO_PERFORM;
1161 /* Others are implicitly ignored */
1165 eid = lsqlite3_get_eid(module, msg->dn);
1167 ret = LDB_ERR_OTHER;
1171 for (i = 0; i < msg->num_elements; i++) {
1172 const struct ldb_message_element *el = &msg->elements[i];
1173 const struct ldb_attrib_handler *h;
1174 int flags = el->flags & LDB_FLAG_MOD_MASK;
1179 /* Get a case-folded copy of the attribute name */
1180 attr = ldb_casefold(local_ctx, el->name);
1182 ret = LDB_ERR_OTHER;
1186 h = ldb_attrib_handler(module->ldb, el->name);
1190 case LDB_FLAG_MOD_REPLACE:
1192 /* remove all attributes before adding the replacements */
1193 mod = lsqlite3_tprintf(local_ctx,
1194 "DELETE FROM ldb_attribute_values "
1195 "WHERE eid = '%lld' "
1196 "AND norm_attr_name = '%q';",
1199 ret = LDB_ERR_OTHER;
1203 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1204 if (ret != SQLITE_OK) {
1206 ldb_set_errstring(module, talloc_strdup(module, errmsg));
1209 ret = LDB_ERR_OTHER;
1213 /* MISSING break is INTENTIONAL */
1215 case LDB_FLAG_MOD_ADD:
1216 #warning "We should throw an error if no value is provided!"
1217 /* For each value of the specified attribute name... */
1218 for (j = 0; j < el->num_values; j++) {
1219 struct ldb_val value;
1221 /* Get a canonicalised copy of the data */
1222 h->canonicalise_fn(module->ldb, local_ctx, &(el->values[j]), &value);
1223 if (value.data == NULL) {
1224 ret = LDB_ERR_OTHER;
1228 mod = lsqlite3_tprintf(local_ctx,
1229 "INSERT OR ROLLBACK INTO ldb_attribute_values "
1230 "('eid', 'attr_name', 'norm_attr_name',"
1231 " 'attr_value', 'norm_attr_value') "
1232 "VALUES ('%lld', '%q', '%q', '%q', '%q');",
1233 eid, el->name, attr,
1234 el->values[j].data, value.data);
1237 ret = LDB_ERR_OTHER;
1241 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1242 if (ret != SQLITE_OK) {
1244 ldb_set_errstring(module, talloc_strdup(module, errmsg));
1247 ret = LDB_ERR_OTHER;
1254 case LDB_FLAG_MOD_DELETE:
1255 #warning "We should throw an error if the attribute we are trying to delete does not exist!"
1256 if (el->num_values == 0) {
1257 mod = lsqlite3_tprintf(local_ctx,
1258 "DELETE FROM ldb_attribute_values "
1259 "WHERE eid = '%lld' "
1260 "AND norm_attr_name = '%q';",
1263 ret = LDB_ERR_OTHER;
1267 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1268 if (ret != SQLITE_OK) {
1270 ldb_set_errstring(module, talloc_strdup(module, errmsg));
1273 ret = LDB_ERR_OTHER;
1278 /* For each value of the specified attribute name... */
1279 for (j = 0; j < el->num_values; j++) {
1280 struct ldb_val value;
1282 /* Get a canonicalised copy of the data */
1283 h->canonicalise_fn(module->ldb, local_ctx, &(el->values[j]), &value);
1284 if (value.data == NULL) {
1285 ret = LDB_ERR_OTHER;
1289 mod = lsqlite3_tprintf(local_ctx,
1290 "DELETE FROM ldb_attribute_values "
1291 "WHERE eid = '%lld' "
1292 "AND norm_attr_name = '%q' "
1293 "AND norm_attr_value = '%q';",
1294 eid, attr, value.data);
1297 ret = LDB_ERR_OTHER;
1301 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1302 if (ret != SQLITE_OK) {
1304 ldb_set_errstring(module, talloc_strdup(module, errmsg));
1307 ret = LDB_ERR_OTHER;
1316 talloc_free(local_ctx);
1320 talloc_free(local_ctx);
1324 /* delete a record */
1325 static int lsqlite3_delete(struct ldb_module *module, const struct ldb_dn *dn)
1327 TALLOC_CTX *local_ctx;
1328 struct lsqlite3_private *lsqlite3 = module->private_data;
1334 /* ignore ltdb specials */
1335 if (ldb_dn_is_special(dn)) {
1339 /* create a local ctx */
1340 local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_delete local context");
1341 if (local_ctx == NULL) {
1342 return LDB_ERR_OTHER;
1345 eid = lsqlite3_get_eid(module, dn);
1347 ret = LDB_ERR_OTHER;
1351 query = lsqlite3_tprintf(local_ctx,
1353 "DELETE FROM ldb_entry WHERE eid = %lld; "
1354 /* Delete attributes */
1355 "DELETE FROM ldb_attribute_values WHERE eid = %lld; ",
1357 if (query == NULL) {
1358 ret = LDB_ERR_OTHER;
1362 ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1363 if (ret != SQLITE_OK) {
1365 ldb_set_errstring(module, talloc_strdup(module, errmsg));
1368 ret = LDB_ERR_OTHER;
1372 talloc_free(local_ctx);
1376 talloc_free(local_ctx);
1380 /* rename a record */
1381 static int lsqlite3_rename(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
1383 TALLOC_CTX *local_ctx;
1384 struct lsqlite3_private *lsqlite3 = module->private_data;
1385 char *new_dn, *new_cdn, *old_cdn;
1390 /* ignore ltdb specials */
1391 if (ldb_dn_is_special(olddn) || ldb_dn_is_special(newdn)) {
1395 /* create a local ctx */
1396 local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_rename local context");
1397 if (local_ctx == NULL) {
1398 return LDB_ERR_OTHER;
1401 /* create linearized and normalized dns */
1402 old_cdn = ldb_dn_linearize(local_ctx, ldb_dn_casefold(module->ldb, olddn));
1403 new_cdn = ldb_dn_linearize(local_ctx, ldb_dn_casefold(module->ldb, newdn));
1404 new_dn = ldb_dn_linearize(local_ctx, newdn);
1405 if (old_cdn == NULL || new_cdn == NULL || new_dn == NULL) {
1406 ret = LDB_ERR_OTHER;
1410 /* build the SQL query */
1411 query = lsqlite3_tprintf(local_ctx,
1412 "UPDATE ldb_entry SET dn = '%q', norm_dn = '%q' "
1413 "WHERE norm_dn = '%q';",
1414 new_dn, new_cdn, old_cdn);
1415 if (query == NULL) {
1416 ret = LDB_ERR_OTHER;
1421 ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1422 if (ret != SQLITE_OK) {
1424 ldb_set_errstring(module, talloc_strdup(module, errmsg));
1427 ret = LDB_ERR_OTHER;
1431 /* clean up and exit */
1432 talloc_free(local_ctx);
1436 talloc_free(local_ctx);
1440 static int lsqlite3_start_trans(struct ldb_module * module)
1444 struct lsqlite3_private * lsqlite3 = module->private_data;
1446 if (lsqlite3->trans_count == 0) {
1447 ret = sqlite3_exec(lsqlite3->sqlite, "BEGIN IMMEDIATE;", NULL, NULL, &errmsg);
1448 if (ret != SQLITE_OK) {
1450 printf("lsqlite3_start_trans: error: %s\n", errmsg);
1457 lsqlite3->trans_count++;
1462 static int lsqlite3_end_trans(struct ldb_module *module)
1466 struct lsqlite3_private *lsqlite3 = module->private_data;
1468 if (lsqlite3->trans_count > 0) {
1469 lsqlite3->trans_count--;
1472 if (lsqlite3->trans_count == 0) {
1473 ret = sqlite3_exec(lsqlite3->sqlite, "COMMIT;", NULL, NULL, &errmsg);
1474 if (ret != SQLITE_OK) {
1476 printf("lsqlite3_end_trans: error: %s\n", errmsg);
1486 static int lsqlite3_del_trans(struct ldb_module *module)
1488 struct lsqlite3_private *lsqlite3 = module->private_data;
1490 if (lsqlite3->trans_count > 0) {
1491 lsqlite3->trans_count--;
1494 if (lsqlite3->trans_count == 0) {
1495 return lsqlite3_safe_rollback(lsqlite3->sqlite);
1505 static int initialize(struct lsqlite3_private *lsqlite3,
1506 struct ldb_context *ldb, const char *url, int flags)
1508 TALLOC_CTX *local_ctx;
1515 /* create a local ctx */
1516 local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_rename local context");
1517 if (local_ctx == NULL) {
1521 schema = lsqlite3_tprintf(local_ctx,
1524 "CREATE TABLE ldb_info AS "
1525 " SELECT 'LDB' AS database_type,"
1526 " '1.0' AS version;"
1529 * The entry table holds the information about an entry.
1530 * This table is used to obtain the EID of the entry and to
1531 * support scope=one and scope=base. The parent and child
1532 * table is included in the entry table since all the other
1533 * attributes are dependent on EID.
1535 "CREATE TABLE ldb_entry "
1537 " eid INTEGER PRIMARY KEY AUTOINCREMENT,"
1538 " dn TEXT UNIQUE NOT NULL,"
1539 " norm_dn TEXT UNIQUE NOT NULL"
1543 "CREATE TABLE ldb_object_classes"
1545 " class_name TEXT PRIMARY KEY,"
1546 " parent_class_name TEXT,"
1547 " tree_key TEXT UNIQUE,"
1548 " max_child_num INTEGER DEFAULT 0"
1552 * We keep a full listing of attribute/value pairs here
1554 "CREATE TABLE ldb_attribute_values"
1556 " eid INTEGER REFERENCES ldb_entry,"
1558 " norm_attr_name TEXT,"
1560 " norm_attr_value TEXT "
1567 "CREATE INDEX ldb_attribute_values_eid_idx "
1568 " ON ldb_attribute_values (eid);"
1570 "CREATE INDEX ldb_attribute_values_name_value_idx "
1571 " ON ldb_attribute_values (attr_name, norm_attr_value);"
1579 "CREATE TRIGGER ldb_object_classes_insert_tr"
1581 " ON ldb_object_classes"
1584 " UPDATE ldb_object_classes"
1585 " SET tree_key = COALESCE(tree_key, "
1587 " SELECT tree_key || "
1588 " (SELECT base160(max_child_num + 1)"
1589 " FROM ldb_object_classes"
1590 " WHERE class_name = "
1591 " new.parent_class_name)"
1592 " FROM ldb_object_classes "
1593 " WHERE class_name = new.parent_class_name "
1595 " UPDATE ldb_object_classes "
1596 " SET max_child_num = max_child_num + 1"
1597 " WHERE class_name = new.parent_class_name;"
1601 * Table initialization
1604 "INSERT INTO ldb_object_classes "
1605 " (class_name, tree_key) "
1607 " ('TOP', '0001');");
1609 /* Skip protocol indicator of url */
1610 if (strncmp(url, "sqlite://", 9) != 0) {
1611 return SQLITE_MISUSE;
1614 /* Update pointer to just after the protocol indicator */
1617 /* Try to open the (possibly empty/non-existent) database */
1618 if ((ret = sqlite3_open(url, &lsqlite3->sqlite)) != SQLITE_OK) {
1622 /* In case this is a new database, enable auto_vacuum */
1623 ret = sqlite3_exec(lsqlite3->sqlite, "PRAGMA auto_vacuum = 1;", NULL, NULL, &errmsg);
1624 if (ret != SQLITE_OK) {
1626 printf("lsqlite3 initializaion error: %s\n", errmsg);
1632 if (flags & LDB_FLG_NOSYNC) {
1634 ret = sqlite3_exec(lsqlite3->sqlite, "PRAGMA synchronous = OFF;", NULL, NULL, &errmsg);
1635 if (ret != SQLITE_OK) {
1637 printf("lsqlite3 initializaion error: %s\n", errmsg);
1646 /* Establish a busy timeout of 30 seconds */
1647 if ((ret = sqlite3_busy_timeout(lsqlite3->sqlite,
1648 30000)) != SQLITE_OK) {
1652 /* Create a function, callable from sql, to increment a tree_key */
1654 sqlite3_create_function(lsqlite3->sqlite,/* handle */
1655 "base160_next", /* function name */
1656 1, /* number of args */
1657 SQLITE_ANY, /* preferred text type */
1658 NULL, /* user data */
1659 base160next_sql, /* called func */
1660 NULL, /* step func */
1661 NULL /* final func */
1666 /* Create a function, callable from sql, to convert int to base160 */
1668 sqlite3_create_function(lsqlite3->sqlite,/* handle */
1669 "base160", /* function name */
1670 1, /* number of args */
1671 SQLITE_ANY, /* preferred text type */
1672 NULL, /* user data */
1673 base160_sql, /* called func */
1674 NULL, /* step func */
1675 NULL /* final func */
1680 /* Create a function, callable from sql, to perform various comparisons */
1682 sqlite3_create_function(lsqlite3->sqlite, /* handle */
1683 "ldap_compare", /* function name */
1684 4, /* number of args */
1685 SQLITE_ANY, /* preferred text type */
1686 ldb , /* user data */
1687 lsqlite3_compare, /* called func */
1688 NULL, /* step func */
1689 NULL /* final func */
1694 /* Begin a transaction */
1695 ret = sqlite3_exec(lsqlite3->sqlite, "BEGIN EXCLUSIVE;", NULL, NULL, &errmsg);
1696 if (ret != SQLITE_OK) {
1698 printf("lsqlite3: initialization error: %s\n", errmsg);
1705 /* Determine if this is a new database. No tables means it is. */
1706 if (query_int(lsqlite3,
1709 " FROM sqlite_master\n"
1710 " WHERE type = 'table';") != 0) {
1714 if (queryInt == 0) {
1716 * Create the database schema
1718 ret = sqlite3_exec(lsqlite3->sqlite, schema, NULL, NULL, &errmsg);
1719 if (ret != SQLITE_OK) {
1721 printf("lsqlite3 initializaion error: %s\n", errmsg);
1728 * Ensure that the database we opened is one of ours
1730 if (query_int(lsqlite3,
1733 " (SELECT COUNT(*) = 2"
1734 " FROM sqlite_master "
1735 " WHERE type = 'table' "
1739 " 'ldb_object_classes' "
1745 " WHERE database_type = 'LDB' "
1746 " AND version = '1.0'"
1750 /* It's not one that we created. See ya! */
1755 /* Commit the transaction */
1756 ret = sqlite3_exec(lsqlite3->sqlite, "COMMIT;", NULL, NULL, &errmsg);
1757 if (ret != SQLITE_OK) {
1759 printf("lsqlite3: iniialization error: %s\n", errmsg);
1768 if (rollback) lsqlite3_safe_rollback(lsqlite3->sqlite);
1769 sqlite3_close(lsqlite3->sqlite);
1776 struct lsqlite3_private *lsqlite3 = p;
1778 if (lsqlite3->sqlite) {
1779 sqlite3_close(lsqlite3->sqlite);
1785 static int lsqlite3_request(struct ldb_module *module, struct ldb_request *req)
1787 /* check for oustanding critical controls and return an error if found */
1788 if (check_critical_controls(req->controls)) {
1789 return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
1792 switch (req->operation) {
1794 case LDB_REQ_SEARCH:
1795 return lsqlite3_search_bytree(module,
1796 req->op.search.base,
1797 req->op.search.scope,
1798 req->op.search.tree,
1799 req->op.search.attrs,
1800 &req->op.search.res);
1803 return lsqlite3_add(module, req->op.add.message);
1805 case LDB_REQ_MODIFY:
1806 return lsqlite3_modify(module, req->op.mod.message);
1808 case LDB_REQ_DELETE:
1809 return lsqlite3_delete(module, req->op.del.dn);
1811 case LDB_REQ_RENAME:
1812 return lsqlite3_rename(module,
1813 req->op.rename.olddn,
1814 req->op.rename.newdn);
1817 return LDB_ERR_OPERATIONS_ERROR;
1822 static int lsqlite3_init_2(struct ldb_module *module)
1828 * Table of operations for the sqlite3 backend
1830 static const struct ldb_module_ops lsqlite3_ops = {
1832 .request = lsqlite3_request,
1833 .start_transaction = lsqlite3_start_trans,
1834 .end_transaction = lsqlite3_end_trans,
1835 .del_transaction = lsqlite3_del_trans,
1836 .second_stage_init = lsqlite3_init_2
1840 * connect to the database
1842 int lsqlite3_connect(struct ldb_context *ldb,
1845 const char *options[])
1849 struct lsqlite3_private * lsqlite3 = NULL;
1851 lsqlite3 = talloc(ldb, struct lsqlite3_private);
1856 lsqlite3->sqlite = NULL;
1857 lsqlite3->options = NULL;
1858 lsqlite3->trans_count = 0;
1860 ret = initialize(lsqlite3, ldb, url, flags);
1861 if (ret != SQLITE_OK) {
1865 talloc_set_destructor(lsqlite3, destructor);
1867 ldb->modules = talloc(ldb, struct ldb_module);
1868 if (!ldb->modules) {
1871 ldb->modules->ldb = ldb;
1872 ldb->modules->prev = ldb->modules->next = NULL;
1873 ldb->modules->private_data = lsqlite3;
1874 ldb->modules->ops = &lsqlite3_ops;
1878 * take a copy of the options array, so we don't have to rely
1879 * on the caller keeping it around (it might be dynamic)
1881 for (i=0;options[i];i++) ;
1883 lsqlite3->options = talloc_array(lsqlite3, char *, i+1);
1884 if (!lsqlite3->options) {
1888 for (i=0;options[i];i++) {
1890 lsqlite3->options[i+1] = NULL;
1891 lsqlite3->options[i] =
1892 talloc_strdup(lsqlite3->options, options[i]);
1893 if (!lsqlite3->options[i]) {
1902 if (lsqlite3->sqlite != NULL) {
1903 (void) sqlite3_close(lsqlite3->sqlite);
1905 talloc_free(lsqlite3);