4 Copyright (C) Derrell Lipman 2005
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * Component: ldb sqlite3 backend
30 * Description: core files for SQLITE3 backend
32 * Author: Derrell Lipman (based on Andrew Tridgell's LDAP backend)
37 #include "ldb/include/ldb.h"
38 #include "ldb/include/ldb_private.h"
39 #include "ldb/ldb_sqlite3/ldb_sqlite3.h"
42 * Macros used throughout
47 # define TRUE (! FALSE)
50 #define RESULT_ATTR_TABLE "temp_result_attrs"
52 //#define TEMPTAB /* for testing, create non-temporary table */
53 #define TEMPTAB "TEMPORARY"
58 sqlite3_stmt * stmtGetEID = NULL;
60 static char *lsqlite3_tprintf(TALLOC_CTX *mem_ctx, const char *fmt, ...)
66 str = sqlite3_vmprintf(fmt, ap);
69 if (str == NULL) return NULL;
71 ret = talloc_strdup(mem_ctx, str);
81 static unsigned char base160tab[161] = {
82 48 ,49 ,50 ,51 ,52 ,53 ,54 ,55 ,56 ,57 , /* 0-9 */
83 58 ,59 ,65 ,66 ,67 ,68 ,69 ,70 ,71 ,72 , /* : ; A-H */
84 73 ,74 ,75 ,76 ,77 ,78 ,79 ,80 ,81 ,82 , /* I-R */
85 83 ,84 ,85 ,86 ,87 ,88 ,89 ,90 ,97 ,98 , /* S-Z , a-b */
86 99 ,100,101,102,103,104,105,106,107,108, /* c-l */
87 109,110,111,112,113,114,115,116,117,118, /* m-v */
88 119,120,121,122,160,161,162,163,164,165, /* w-z, latin1 */
89 166,167,168,169,170,171,172,173,174,175, /* latin1 */
90 176,177,178,179,180,181,182,183,184,185, /* latin1 */
91 186,187,188,189,190,191,192,193,194,195, /* latin1 */
92 196,197,198,199,200,201,202,203,204,205, /* latin1 */
93 206,207,208,209,210,211,212,213,214,215, /* latin1 */
94 216,217,218,219,220,221,222,223,224,225, /* latin1 */
95 226,227,228,229,230,231,232,233,234,235, /* latin1 */
96 236,237,238,239,240,241,242,243,244,245, /* latin1 */
97 246,247,248,249,250,251,252,253,254,255, /* latin1 */
105 * Convert an unsigned long integer into a base160 representation of the
110 * value to be converted
113 * character array, 5 bytes long, into which the base160 representation
114 * will be placed. The result will be a four-digit representation of the
115 * number (with leading zeros prepended as necessary), and null
122 base160_sql(sqlite3_context * hContext,
124 sqlite3_value ** argv)
130 val = sqlite3_value_int64(argv[0]);
132 for (i = 3; i >= 0; i--) {
134 result[i] = base160tab[val % 160];
140 sqlite3_result_text(hContext, result, -1, SQLITE_TRANSIENT);
147 * This function enhances sqlite by adding a "base160_next()" function which is
148 * accessible via queries.
150 * Retrieve the next-greater number in the base160 sequence for the terminal
151 * tree node (the last four digits). Only one tree level (four digits) is
155 * A character string: either an empty string (in which case no operation is
156 * performed), or a string of base160 digits with a length of a multiple of
160 * Upon return, the trailing four digits (one tree level) will have been
164 base160next_sql(sqlite3_context * hContext,
166 sqlite3_value ** argv)
170 unsigned char * pTab;
171 unsigned char * pBase160 =
172 strdup(sqlite3_value_text(argv[0]));
173 unsigned char * pStart = pBase160;
176 * We need a minimum of four digits, and we will always get a multiple
179 if (pBase160 != NULL &&
180 (len = strlen(pBase160)) >= 4 &&
183 if (pBase160 == NULL) {
185 sqlite3_result_null(hContext);
189 pBase160 += strlen(pBase160) - 1;
191 /* We only carry through four digits: one level in the tree */
192 for (i = 0; i < 4; i++) {
194 /* What base160 value does this digit have? */
195 pTab = strchr(base160tab, *pBase160);
197 /* Is there a carry? */
198 if (pTab < base160tab + sizeof(base160tab) - 1) {
201 * Nope. Just increment this value and we're
209 * There's a carry. This value gets
210 * base160tab[0], we decrement the buffer
211 * pointer to get the next higher-order digit,
212 * and continue in the loop.
214 *pBase160-- = base160tab[0];
218 sqlite3_result_text(hContext,
223 sqlite3_result_value(hContext, argv[0]);
224 if (pBase160 != NULL) {
230 static char *parsetree_to_sql(struct ldb_module *module,
232 const struct ldb_parse_tree *t)
234 const struct ldb_attrib_handler *h;
235 struct ldb_val value, subval;
236 char *wild_card_string;
242 switch(t->operation) {
245 tmp = parsetree_to_sql(module, mem_ctx, t->u.list.elements[0]);
246 if (tmp == NULL) return NULL;
248 for (i = 1; i < t->u.list.num_elements; i++) {
250 child = parsetree_to_sql(module, mem_ctx, t->u.list.elements[i]);
251 if (child == NULL) return NULL;
253 tmp = talloc_asprintf_append(tmp, "INTERSECT %s ", child);
254 if (tmp == NULL) return NULL;
257 ret = talloc_asprintf(mem_ctx, "SELECT * FROM ( %s )\n", tmp);
263 tmp = parsetree_to_sql(module, mem_ctx, t->u.list.elements[0]);
264 if (tmp == NULL) return NULL;
266 for (i = 1; i < t->u.list.num_elements; i++) {
268 child = parsetree_to_sql(module, mem_ctx, t->u.list.elements[i]);
269 if (child == NULL) return NULL;
271 tmp = talloc_asprintf_append(tmp, "UNION %s ", child);
272 if (tmp == NULL) return NULL;
275 return talloc_asprintf(mem_ctx, "SELECT * FROM ( %s )", tmp);
279 child = parsetree_to_sql(module, mem_ctx, t->u.isnot.child);
280 if (child == NULL) return NULL;
282 return talloc_asprintf(mem_ctx,
283 "SELECT eid FROM ldb_entry "
284 "WHERE eid NOT IN ( %s )", child);
286 case LDB_OP_EQUALITY:
288 * For simple searches, we want to retrieve the list of EIDs that
289 * match the criteria.
291 h = ldb_attrib_handler(module->ldb, t->u.equality.attr);
293 /* Get a canonicalised copy of the data */
294 h->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
295 if (value.data == NULL) {
299 if (strcasecmp(t->u.equality.attr, "objectclass") == 0) {
301 * For object classes, we want to search for all objectclasses
302 * that are subclasses as well.
304 return lsqlite3_tprintf(mem_ctx,
305 "SELECT eid FROM ldb_attribute_values\n"
306 "WHERE norm_attr_name = 'OBJECTCLASS' "
307 "AND norm_attr_value IN\n"
308 " (SELECT class_name FROM ldb_object_classes\n"
309 " WHERE tree_key GLOB\n"
310 " (SELECT tree_key FROM ldb_object_classes\n"
311 " WHERE class_name = '%q'\n"
315 } else if (strcasecmp(t->u.equality.attr, "dn") == 0) {
316 /* DN query is a special ldb case */
317 char *cdn = ldb_dn_linearize_casefold(module->ldb,
318 ldb_dn_explode(module->ldb,
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 = upper('%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]);
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 h = ldb_attrib_handler(module->ldb, t->u.substring.attr);
355 subval.data = wild_card_string;
356 subval.length = strlen(wild_card_string) + 1;
358 /* Get a canonicalised copy of the data */
359 h->canonicalise_fn(module->ldb, mem_ctx, &(subval), &value);
360 if (value.data == NULL) {
364 return lsqlite3_tprintf(mem_ctx,
365 "SELECT eid FROM ldb_attribute_values "
366 "WHERE norm_attr_name = upper('%q') "
367 "AND norm_attr_value GLOB '%q'",
372 h = ldb_attrib_handler(module->ldb, t->u.equality.attr);
374 /* Get a canonicalised copy of the data */
375 h->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
376 if (value.data == NULL) {
380 return lsqlite3_tprintf(mem_ctx,
381 "SELECT eid FROM ldb_attribute_values "
382 "WHERE norm_attr_name = upper('%q') "
383 "AND norm_attr_value >= '%q'",
388 h = ldb_attrib_handler(module->ldb, t->u.equality.attr);
390 /* Get a canonicalised copy of the data */
391 h->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
392 if (value.data == NULL) {
396 return lsqlite3_tprintf(mem_ctx,
397 "SELECT eid FROM ldb_attribute_values "
398 "WHERE norm_attr_name = upper('%q') "
399 "AND norm_attr_value <= '%q'",
404 if (strcasecmp(t->u.present.attr, "dn")) {
405 return talloc_strdup(mem_ctx, "SELECT eid FROM ldb_entry");
408 return lsqlite3_tprintf(mem_ctx,
409 "SELECT eid FROM ldb_attribute_values "
410 "WHERE norm_attr_name = upper('%q')",
414 h = ldb_attrib_handler(module->ldb, t->u.equality.attr);
416 /* Get a canonicalised copy of the data */
417 h->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
418 if (value.data == NULL) {
422 return lsqlite3_tprintf(mem_ctx,
423 "SELECT eid FROM ldb_attribute_values "
424 "WHERE norm_attr_name = upper('%q') "
425 "AND norm_attr_value LIKE '%q'",
429 case LDB_OP_EXTENDED:
430 #warning "work out how to handle bitops"
437 /* should never occur */
446 * This function is used for queries that are not expected to return any rows,
447 * e.g. BEGIN, COMMIT, ROLLBACK, CREATE TABLE, INSERT, UPDATE, DELETE, etc.
448 * There are no provisions here for returning data from rows in a table, so do
449 * not pass SELECT queries to this function.
452 query_norows(const struct lsqlite3_private *lsqlite3,
459 sqlite3_stmt * pStmt;
462 /* Begin access to variable argument list */
463 va_start(args, pSql);
465 /* Format the query */
466 if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
471 * Prepare and execute the SQL statement. Loop allows retrying on
472 * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
473 * requiring retrying the operation.
475 for (bLoop = TRUE; bLoop; ) {
477 /* Compile the SQL statement into sqlite virtual machine */
478 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
482 NULL)) == SQLITE_SCHEMA) {
483 if (stmtGetEID != NULL) {
484 sqlite3_finalize(stmtGetEID);
488 } else if (ret != SQLITE_OK) {
493 /* No rows expected, so just step through machine code once */
494 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
495 if (stmtGetEID != NULL) {
496 sqlite3_finalize(stmtGetEID);
499 (void) sqlite3_finalize(pStmt);
501 } else if (ret != SQLITE_DONE) {
502 (void) sqlite3_finalize(pStmt);
507 /* Free the virtual machine */
508 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
509 if (stmtGetEID != NULL) {
510 sqlite3_finalize(stmtGetEID);
514 } else if (ret != SQLITE_OK) {
515 (void) sqlite3_finalize(pStmt);
521 * Normal condition is only one time through loop. Loop is
522 * rerun in error conditions, via "continue", above.
528 /* All done with variable argument list */
531 /* Free the memory we allocated for our query string */
538 /* obtain a named lock */
540 lsqlite3_lock(struct ldb_module * module,
541 const char * lockname)
543 struct lsqlite3_private * lsqlite3 = module->private_data;
546 if (lockname == NULL) {
550 if (strcmp(lockname, "transaction") == 0) {
551 if (lsqlite3->lock_count == 0) {
552 if (query_norows(lsqlite3, "BEGIN EXCLUSIVE;") != 0) {
556 ++lsqlite3->lock_count;
562 /* release a named lock */
564 lsqlite3_unlock(struct ldb_module *module,
565 const char *lockname)
567 struct lsqlite3_private * lsqlite3 = module->private_data;
570 if (lockname == NULL) {
574 if (strcmp(lockname, "transaction") == 0) {
575 if (lsqlite3->lock_count == 1) {
576 if (query_norows(lsqlite3, "COMMIT;") != 0) {
577 query_norows(lsqlite3, "ROLLBACK;");
579 } else if (lsqlite3->lock_count > 0) {
580 --lsqlite3->lock_count;
582 } else if (strcmp(lockname, "rollback") == 0) {
583 query_norows(lsqlite3, "ROLLBACK;");
592 * This function is used for the common case of queries that return a single
595 * NOTE: If more than one value is returned by the query, all but the first
596 * one will be ignored.
599 query_int(const struct lsqlite3_private * lsqlite3,
607 sqlite3_stmt * pStmt;
610 /* Begin access to variable argument list */
611 va_start(args, pSql);
613 /* Format the query */
614 if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
619 * Prepare and execute the SQL statement. Loop allows retrying on
620 * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
621 * requiring retrying the operation.
623 for (bLoop = TRUE; bLoop; ) {
625 /* Compile the SQL statement into sqlite virtual machine */
626 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
630 NULL)) == SQLITE_SCHEMA) {
631 if (stmtGetEID != NULL) {
632 sqlite3_finalize(stmtGetEID);
636 } else if (ret != SQLITE_OK) {
640 /* One row expected */
641 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
642 if (stmtGetEID != NULL) {
643 sqlite3_finalize(stmtGetEID);
646 (void) sqlite3_finalize(pStmt);
648 } else if (ret != SQLITE_ROW) {
649 (void) sqlite3_finalize(pStmt);
653 /* Get the value to be returned */
654 *pRet = sqlite3_column_int64(pStmt, 0);
656 /* Free the virtual machine */
657 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
658 if (stmtGetEID != NULL) {
659 sqlite3_finalize(stmtGetEID);
663 } else if (ret != SQLITE_OK) {
664 (void) sqlite3_finalize(pStmt);
669 * Normal condition is only one time through loop. Loop is
670 * rerun in error conditions, via "continue", above.
675 /* All done with variable argument list */
679 /* Free the memory we allocated for our query string */
686 /* rename a record */
687 static int lsqlite3_safe_rollback(sqlite3 *sqlite)
693 ret = sqlite3_exec(sqlite, "ROLLBACK;", NULL, NULL, &errmsg);
694 if (ret != SQLITE_OK) {
696 printf("lsqlite3_safe_rollback: Serious Error: %s\n", errmsg);
705 /* return an eid as result */
706 static int lsqlite3_eid_callback(void *result, int col_num, char **cols, char **names)
708 long long *eid = (long long *)result;
710 if (col_num != 1) return SQLITE_ABORT;
711 if (strcasecmp(names[0], "eid") != 0) return SQLITE_ABORT;
713 *eid = atoll(cols[0]);
717 struct lsqlite3_msgs {
719 struct ldb_message **msgs;
720 long long current_eid;
725 * add a single set of ldap message values to a ldb_message
728 static int lsqlite3_search_callback(void *result, int col_num, char **cols, char **names)
730 struct lsqlite3_msgs *msgs = (struct lsqlite3_msgs *)result;
731 struct ldb_message *msg;
734 /* eid, dn, attr_name, attr_value */
735 if (col_num != 4) return SQLITE_ABORT;
737 eid = atoll(cols[0]);
739 if (eid != msgs->current_eid) {
740 msgs->msgs = talloc_realloc(msgs->mem_ctx,
742 struct ldb_message *,
744 if (msgs->msgs == NULL) return SQLITE_ABORT;
746 msgs->msgs[msgs->count] = talloc(msgs->msgs, struct ldb_message);
747 if (msgs->msgs[msgs->count] == NULL) return SQLITE_ABORT;
749 msgs->msgs[msgs->count]->dn = NULL;
750 msgs->msgs[msgs->count]->num_elements = 0;
751 msgs->msgs[msgs->count]->elements = NULL;
752 msgs->msgs[msgs->count]->private_data = NULL;
755 msgs->current_eid = eid;
758 msg = msgs->msgs[msgs->count -1];
760 if (msg->dn == NULL) {
761 msg->dn = ldb_dn_explode(msg, cols[1]);
762 if (msg->dn == NULL) return SQLITE_ABORT;
765 msg->elements = talloc_realloc(msg,
767 struct ldb_message_element,
768 msg->num_elements + 1);
769 if (msg->elements == NULL) return SQLITE_ABORT;
771 msg->elements[msg->num_elements].flags = 0;
772 msg->elements[msg->num_elements].name = talloc_strdup(msg->elements, cols[2]);
773 if (msg->elements[msg->num_elements].name == NULL) return SQLITE_ABORT;
775 msg->elements[msg->num_elements].num_values = 1;
776 msg->elements[msg->num_elements].values = talloc_array(msg->elements,
778 if (msg->elements[msg->num_elements].values == NULL) return SQLITE_ABORT;
780 msg->elements[msg->num_elements].values[0].length = strlen(cols[3]);
781 msg->elements[msg->num_elements].values[0].data = talloc_strdup(msg->elements, cols[3]);
782 if (msg->elements[msg->num_elements].values[0].data == NULL) return SQLITE_ABORT;
792 * lsqlite3_get_eid_ndn()
794 * These functions are used for the very common case of retrieving an EID value
795 * given a (normalized) DN.
798 static long long lsqlite3_get_eid_ndn(sqlite3 *sqlite, void *mem_ctx, const char *norm_dn)
806 query = lsqlite3_tprintf(mem_ctx, "SELECT eid "
808 "WHERE norm_dn = '%q';", norm_dn);
809 if (query == NULL) return -1;
811 ret = sqlite3_exec(sqlite, query, lsqlite3_eid_callback, &eid, &errmsg);
812 if (ret != SQLITE_OK) {
814 printf("lsqlite3_get_eid: Fatal Error: %s\n", errmsg);
823 static long long lsqlite3_get_eid(struct ldb_module *module, const struct ldb_dn *dn)
825 TALLOC_CTX *local_ctx;
826 struct lsqlite3_private *lsqlite3 = module->private_data;
830 /* ignore ltdb specials */
831 if (ldb_dn_is_special(dn)) {
835 /* create a local ctx */
836 local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_get_eid local context");
837 if (local_ctx == NULL) {
841 cdn = ldb_dn_linearize(local_ctx, ldb_dn_casefold(module->ldb, dn));
844 eid = lsqlite3_get_eid_ndn(lsqlite3->sqlite, local_ctx, cdn);
847 talloc_free(local_ctx);
852 * Interface functions referenced by lsqlite3_ops
855 /* search for matching records, by tree */
856 static int lsqlite3_search_bytree(struct ldb_module * module, const struct ldb_dn* basedn,
857 enum ldb_scope scope, struct ldb_parse_tree * tree,
858 const char * const * attrs, struct ldb_message *** res)
860 TALLOC_CTX *local_ctx;
861 struct lsqlite3_private *lsqlite3 = module->private_data;
862 struct lsqlite3_msgs msgs;
870 /* create a local ctx */
871 local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_search_by_tree local context");
872 if (local_ctx == NULL) {
877 norm_basedn = ldb_dn_linearize(local_ctx, ldb_dn_casefold(module->ldb, basedn));
878 if (norm_basedn == NULL) goto failed;
879 } else norm_basedn = talloc_strdup(local_ctx, "");
881 if (*norm_basedn == '\0' &&
882 (scope == LDB_SCOPE_BASE || scope == LDB_SCOPE_ONELEVEL))
885 /* Convert filter into a series of SQL conditions (constraints) */
886 sqlfilter = parsetree_to_sql(module, local_ctx, tree);
888 /* Initially, we don't know what the requested attributes are */
890 attr_list = talloc_strdup(local_ctx, "AND norm_attr_name IN (");
891 if (attr_list == NULL) goto failed;
893 for (i = 0; attrs[i]; i++) {
894 char *norm_attr_name;
896 /* If any attribute in the list is "*" then... */
897 if (strcmp(attrs[i], "*") == 0) {
898 /* we want all attribute types */
899 attr_list = talloc_strdup(local_ctx, "");
900 if (attr_list == NULL) goto failed;
904 norm_attr_name = ldb_casefold(local_ctx, attrs[i]);
905 if (norm_attr_name == NULL) goto failed;
907 attr_list = talloc_asprintf_append(attr_list, "'%q', ",
909 if (attr_list == NULL) goto failed;
913 /* substitute the last ',' with ')' */
914 attr_list[strlen(attr_list)-2] = ')';
917 attr_list = talloc_strdup(local_ctx, "");
918 if (attr_list == NULL) goto failed;
922 case LDB_SCOPE_DEFAULT:
923 case LDB_SCOPE_SUBTREE:
924 if (*norm_basedn != '\0') {
925 query = lsqlite3_tprintf(local_ctx,
926 "SELECT entry.eid,\n"
930 " FROM ldb_entry AS entry\n"
932 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
933 " ON av.eid = entry.eid\n"
935 " WHERE entry.eid IN\n"
936 " (SELECT DISTINCT ldb_entry.eid\n"
938 " WHERE (ldb_entry.norm_dn GLOB('*,%q')\n"
939 " OR ldb_entry.norm_dn = '%q')\n"
940 " AND ldb_entry.eid IN\n"
946 " ORDER BY entry.eid ASC;",
952 query = lsqlite3_tprintf(local_ctx,
953 "SELECT entry.eid,\n"
957 " FROM ldb_entry AS entry\n"
959 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
960 " ON av.eid = entry.eid\n"
962 " WHERE entry.eid IN\n"
963 " (SELECT DISTINCT ldb_entry.eid\n"
965 " WHERE ldb_entry.eid IN\n"
971 " ORDER BY entry.eid ASC;",
979 query = lsqlite3_tprintf(local_ctx,
980 "SELECT entry.eid,\n"
984 " FROM ldb_entry AS entry\n"
986 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
987 " ON av.eid = entry.eid\n"
989 " WHERE entry.eid IN\n"
990 " (SELECT DISTINCT ldb_entry.eid\n"
992 " WHERE ldb_entry.norm_dn = '%q'\n"
993 " AND ldb_entry.eid IN\n"
999 " ORDER BY entry.eid ASC;",
1005 case LDB_SCOPE_ONELEVEL:
1006 query = lsqlite3_tprintf(local_ctx,
1007 "SELECT entry.eid,\n"
1011 " FROM ldb_entry AS entry\n"
1013 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
1014 " ON av.eid = entry.eid\n"
1016 " WHERE entry.eid IN\n"
1017 " (SELECT DISTINCT ldb_entry.eid\n"
1019 " WHERE norm_dn GLOB('*,%q')\n"
1020 " AND NOT norm_dn GLOB('*,*,%q')\n"
1021 " AND ldb_entry.eid IN\n(%s)\n"
1026 " ORDER BY entry.eid ASC;",
1034 if (query == NULL) {
1039 /* printf ("%s\n", query); */
1043 msgs.current_eid = 0;
1044 msgs.mem_ctx = local_ctx;
1046 ret = sqlite3_exec(lsqlite3->sqlite, query, lsqlite3_search_callback, &msgs, &errmsg);
1047 if (ret != SQLITE_OK) {
1049 printf("lsqlite3_search_bytree: Fatal Error: %s\n", errmsg);
1055 for (i = 0; i < msgs.count; i++) {
1056 msgs.msgs[i] = ldb_msg_canonicalize(module->ldb, msgs.msgs[i]);
1057 if (msgs.msgs[i] == NULL) goto failed;
1060 *res = talloc_steal(module, msgs.msgs);
1063 talloc_free(local_ctx);
1066 /* If error, return error code; otherwise return number of results */
1068 talloc_free(local_ctx);
1072 /* search for matching records, by expression */
1073 static int lsqlite3_search(struct ldb_module * module, const struct ldb_dn *basedn,
1074 enum ldb_scope scope, const char * expression,
1075 const char * const *attrs, struct ldb_message *** res)
1077 struct ldb_parse_tree * tree;
1080 /* Handle tdb specials */
1081 if (ldb_dn_is_special(basedn)) {
1082 #warning "handle tdb specials"
1087 /* (|(objectclass=*)(dn=*)) is passed by the command line tool now instead */
1088 /* Handle the special case of requesting all */
1089 if (pExpression != NULL && *pExpression == '\0') {
1090 pExpression = "dn=*";
1094 /* Parse the filter expression into a tree we can work with */
1095 if ((tree = ldb_parse_tree(module->ldb, expression)) == NULL) {
1099 /* Now use the bytree function for the remainder of processing */
1100 ret = lsqlite3_search_bytree(module, basedn, scope, tree, attrs, res);
1102 /* Free the parse tree */
1111 static int lsqlite3_add(struct ldb_module *module, const struct ldb_message *msg)
1113 TALLOC_CTX *local_ctx;
1114 struct lsqlite3_private *lsqlite3 = module->private_data;
1123 /* create a local ctx */
1124 local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_add local context");
1125 if (local_ctx == NULL) {
1129 /* See if this is an ltdb special */
1130 if (ldb_dn_is_special(msg->dn)) {
1133 c = ldb_dn_explode(local_ctx, "@SUBCLASSES");
1134 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
1135 #warning "insert subclasses into object class tree"
1139 c = ldb_dn_explode(local_ctx, "@INDEXLIST");
1140 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
1141 #warning "should we handle indexes somehow ?"
1145 /* Others are implicitly ignored */
1149 /* create linearized and normalized dns */
1150 dn = ldb_dn_linearize(local_ctx, msg->dn);
1151 ndn = ldb_dn_linearize(local_ctx, ldb_dn_casefold(module->ldb, msg->dn));
1152 if (dn == NULL || ndn == NULL) goto failed;
1154 query = lsqlite3_tprintf(local_ctx,
1155 /* Begin the transaction */
1158 "INSERT OR ABORT INTO ldb_entry "
1159 "('dn', 'norm_dn') "
1160 "VALUES ('%q', '%q');",
1162 if (query == NULL) goto failed;
1164 ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1165 if (ret != SQLITE_OK) {
1167 printf("lsqlite3_add: exec error: %s\n", errmsg);
1170 lsqlite3_safe_rollback(lsqlite3->sqlite);
1175 eid = lsqlite3_get_eid_ndn(lsqlite3->sqlite, local_ctx, ndn);
1176 if (eid == -1) goto failed;
1178 for (i = 0; i < msg->num_elements; i++) {
1179 const struct ldb_message_element *el = &msg->elements[i];
1180 const struct ldb_attrib_handler *h;
1184 /* Get a case-folded copy of the attribute name */
1185 attr = ldb_casefold(local_ctx, el->name);
1186 if (attr == NULL) goto failed;
1188 h = ldb_attrib_handler(module->ldb, el->name);
1190 /* For each value of the specified attribute name... */
1191 for (j = 0; j < el->num_values; j++) {
1192 struct ldb_val value;
1195 /* Get a canonicalised copy of the data */
1196 h->canonicalise_fn(module->ldb, local_ctx, &(el->values[j]), &value);
1197 if (value.data == NULL) goto failed;
1199 insert = lsqlite3_tprintf(local_ctx,
1200 "INSERT OR ROLLBACK INTO ldb_attribute_values "
1201 "('eid', 'attr_name', 'norm_attr_name',"
1202 " 'attr_value', 'norm_attr_value') "
1203 "VALUES ('%lld', '%q', '%q', '%q', '%q');",
1204 eid, el->name, attr,
1205 el->values[j].data, value.data);
1206 if (insert == NULL) goto failed;
1208 ret = sqlite3_exec(lsqlite3->sqlite, insert, NULL, NULL, &errmsg);
1209 if (ret != SQLITE_OK) {
1211 printf("lsqlite3_add: insert error: %s\n", errmsg);
1219 ret = sqlite3_exec(lsqlite3->sqlite, "COMMIT;", NULL, NULL, &errmsg);
1220 if (ret != SQLITE_OK) {
1222 printf("lsqlite3_add: commit error: %s\n", errmsg);
1228 talloc_free(local_ctx);
1232 if (rollback) lsqlite3_safe_rollback(lsqlite3->sqlite);
1233 talloc_free(local_ctx);
1238 /* modify a record */
1239 static int lsqlite3_modify(struct ldb_module *module, const struct ldb_message *msg)
1241 TALLOC_CTX *local_ctx;
1242 struct lsqlite3_private *lsqlite3 = module->private_data;
1249 /* create a local ctx */
1250 local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_modify local context");
1251 if (local_ctx == NULL) {
1255 /* See if this is an ltdb special */
1256 if (ldb_dn_is_special(msg->dn)) {
1259 c = ldb_dn_explode(local_ctx, "@SUBCLASSES");
1260 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
1261 #warning "modify subclasses into object class tree"
1265 c = ldb_dn_explode(local_ctx, "@INDEXLIST");
1266 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
1267 #warning "should we handle indexes somehow ?"
1271 /* Others are implicitly ignored */
1275 ret = sqlite3_exec(lsqlite3->sqlite, "BEGIN EXCLUSIVE;", NULL, NULL, &errmsg);
1276 if (ret != SQLITE_OK) {
1278 printf("lsqlite3_modify: error: %s\n", errmsg);
1285 eid = lsqlite3_get_eid(module, msg->dn);
1290 for (i = 0; i < msg->num_elements; i++) {
1291 const struct ldb_message_element *el = &msg->elements[i];
1292 const struct ldb_attrib_handler *h;
1293 int flags = el->flags & LDB_FLAG_MOD_MASK;
1298 /* Get a case-folded copy of the attribute name */
1299 attr = ldb_casefold(local_ctx, el->name);
1304 h = ldb_attrib_handler(module->ldb, el->name);
1308 case LDB_FLAG_MOD_REPLACE:
1310 /* remove all attributes before adding the replacements */
1311 mod = lsqlite3_tprintf(local_ctx,
1312 "DELETE FROM ldb_attribute_values "
1313 "WHERE eid = '%lld' "
1314 "AND norm_attr_name = '%q';",
1316 if (mod == NULL) goto failed;
1318 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1319 if (ret != SQLITE_OK) {
1321 printf("lsqlite3_modify: error: %s\n", errmsg);
1327 /* MISSING break is INTENTIONAL */
1329 case LDB_FLAG_MOD_ADD:
1330 #warning "We should throw an error if no value is provided!"
1331 /* For each value of the specified attribute name... */
1332 for (j = 0; j < el->num_values; j++) {
1333 struct ldb_val value;
1335 /* Get a canonicalised copy of the data */
1336 h->canonicalise_fn(module->ldb, local_ctx, &(el->values[j]), &value);
1337 if (value.data == NULL) {
1341 mod = lsqlite3_tprintf(local_ctx,
1342 "INSERT OR ROLLBACK INTO ldb_attribute_values "
1343 "('eid', 'attr_name', 'norm_attr_name',"
1344 " 'attr_value', 'norm_attr_value') "
1345 "VALUES ('%lld', '%q', '%q', '%q', '%q');",
1346 eid, el->name, attr,
1347 el->values[j].data, value.data);
1349 if (mod == NULL) goto failed;
1351 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1352 if (ret != SQLITE_OK) {
1354 printf("lsqlite3_modify: error: %s\n", errmsg);
1363 case LDB_FLAG_MOD_DELETE:
1364 #warning "We should throw an error if the attribute we are trying to delete does not exist!"
1365 if (el->num_values == 0) {
1366 mod = lsqlite3_tprintf(local_ctx,
1367 "DELETE FROM ldb_attribute_values "
1368 "WHERE eid = '%lld' "
1369 "AND norm_attr_name = '%q';",
1371 if (mod == NULL) goto failed;
1373 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1374 if (ret != SQLITE_OK) {
1376 printf("lsqlite3_modify: error: %s\n", errmsg);
1383 /* For each value of the specified attribute name... */
1384 for (j = 0; j < el->num_values; j++) {
1385 struct ldb_val value;
1387 /* Get a canonicalised copy of the data */
1388 h->canonicalise_fn(module->ldb, local_ctx, &(el->values[j]), &value);
1389 if (value.data == NULL) {
1393 mod = lsqlite3_tprintf(local_ctx,
1394 "DELETE FROM ldb_attribute_values "
1395 "WHERE eid = '%lld' "
1396 "AND norm_attr_name = '%q' "
1397 "AND norm_attr_value = '%q';",
1398 eid, attr, value.data);
1400 if (mod == NULL) goto failed;
1402 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1403 if (ret != SQLITE_OK) {
1405 printf("lsqlite3_modify: error: %s\n", errmsg);
1416 ret = sqlite3_exec(lsqlite3->sqlite, "COMMIT;", NULL, NULL, &errmsg);
1417 if (ret != SQLITE_OK) {
1419 printf("lsqlite3_modify: error: %s\n", errmsg);
1425 talloc_free(local_ctx);
1429 if (rollback) lsqlite3_safe_rollback(lsqlite3->sqlite);
1430 talloc_free(local_ctx);
1434 /* delete a record */
1435 static int lsqlite3_delete(struct ldb_module *module, const struct ldb_dn *dn)
1437 TALLOC_CTX *local_ctx;
1438 struct lsqlite3_private *lsqlite3 = module->private_data;
1444 /* ignore ltdb specials */
1445 if (ldb_dn_is_special(dn)) {
1449 /* create a local ctx */
1450 local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_delete local context");
1451 if (local_ctx == NULL) {
1455 eid = lsqlite3_get_eid(module, dn);
1456 if (eid == -1) goto failed;
1458 query = lsqlite3_tprintf(local_ctx,
1459 /* Begin the transaction */
1462 "DELETE FROM ldb_entry WHERE eid = %lld; "
1463 /* Delete attributes */
1464 "DELETE FROM ldb_attribute_values WHERE eid = %lld; "
1468 if (query == NULL) goto failed;
1470 ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1471 if (ret != SQLITE_OK) {
1473 printf("lsqlite3_delete: error getting eid: %s\n", errmsg);
1476 lsqlite3_safe_rollback(lsqlite3->sqlite);
1480 talloc_free(local_ctx);
1484 talloc_free(local_ctx);
1488 /* rename a record */
1489 static int lsqlite3_rename(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
1491 TALLOC_CTX *local_ctx;
1492 struct lsqlite3_private *lsqlite3 = module->private_data;
1493 char *new_dn, *new_cdn, *old_cdn;
1498 /* ignore ltdb specials */
1499 if (ldb_dn_is_special(olddn) || ldb_dn_is_special(newdn)) {
1503 /* create a local ctx */
1504 local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_rename local context");
1505 if (local_ctx == NULL) {
1509 /* create linearized and normalized dns */
1510 old_cdn = ldb_dn_linearize(local_ctx, ldb_dn_casefold(module->ldb, olddn));
1511 new_cdn = ldb_dn_linearize(local_ctx, ldb_dn_casefold(module->ldb, newdn));
1512 new_dn = ldb_dn_linearize(local_ctx, newdn);
1513 if (old_cdn == NULL || new_cdn == NULL || new_dn == NULL) goto failed;
1515 /* build the SQL query */
1516 query = lsqlite3_tprintf(local_ctx,
1517 "UPDATE ldb_entry SET dn = '%q', norm_dn = '%q' "
1518 "WHERE norm_dn = '%q';",
1519 new_dn, new_cdn, old_cdn);
1520 if (query == NULL) goto failed;
1523 ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1524 if (ret != SQLITE_OK) {
1526 printf("lsqlite3_rename: sqlite3_exec error: %s\n", errmsg);
1532 /* clean up and exit */
1533 talloc_free(local_ctx);
1537 talloc_free(local_ctx);
1540 /* return extended error information */
1542 lsqlite3_errstring(struct ldb_module *module)
1544 struct lsqlite3_private * lsqlite3 = module->private_data;
1546 return sqlite3_errmsg(lsqlite3->sqlite);
1556 static int initialize(struct lsqlite3_private *lsqlite3, const char *url)
1562 const char *schema =
1565 "CREATE TABLE ldb_info AS "
1566 " SELECT 'LDB' AS database_type,"
1567 " '1.0' AS version;"
1570 * The entry table holds the information about an entry.
1571 * This table is used to obtain the EID of the entry and to
1572 * support scope=one and scope=base. The parent and child
1573 * table is included in the entry table since all the other
1574 * attributes are dependent on EID.
1576 "CREATE TABLE ldb_entry "
1578 " eid INTEGER PRIMARY KEY AUTOINCREMENT,"
1579 " dn TEXT UNIQUE NOT NULL,"
1580 " norm_dn TEXT UNIQUE NOT NULL"
1584 "CREATE TABLE ldb_object_classes"
1586 " class_name TEXT PRIMARY KEY,"
1587 " parent_class_name TEXT,"
1588 " tree_key TEXT UNIQUE,"
1589 " max_child_num INTEGER DEFAULT 0"
1593 * We keep a full listing of attribute/value pairs here
1595 "CREATE TABLE ldb_attribute_values"
1597 " eid INTEGER REFERENCES ldb_entry,"
1599 " norm_attr_name TEXT,"
1601 " norm_attr_value TEXT "
1608 "CREATE INDEX ldb_attribute_values_eid_idx "
1609 " ON ldb_attribute_values (eid);"
1611 "CREATE INDEX ldb_attribute_values_name_value_idx "
1612 " ON ldb_attribute_values (attr_name, norm_attr_value);"
1620 "CREATE TRIGGER ldb_object_classes_insert_tr"
1622 " ON ldb_object_classes"
1625 " UPDATE ldb_object_classes"
1626 " SET tree_key = COALESCE(tree_key, "
1628 " SELECT tree_key || "
1629 " (SELECT base160(max_child_num + 1)"
1630 " FROM ldb_object_classes"
1631 " WHERE class_name = "
1632 " new.parent_class_name)"
1633 " FROM ldb_object_classes "
1634 " WHERE class_name = new.parent_class_name "
1636 " UPDATE ldb_object_classes "
1637 " SET max_child_num = max_child_num + 1"
1638 " WHERE class_name = new.parent_class_name;"
1642 * Table initialization
1645 "INSERT INTO ldb_object_classes "
1646 " (class_name, tree_key) "
1652 /* Skip protocol indicator of url */
1653 if (strncmp(url, "sqlite://", 9) != 0) {
1654 return SQLITE_MISUSE;
1657 /* Update pointer to just after the protocol indicator */
1660 /* Try to open the (possibly empty/non-existent) database */
1661 if ((ret = sqlite3_open(url, &lsqlite3->sqlite)) != SQLITE_OK) {
1665 /* In case this is a new database, enable auto_vacuum */
1666 if (query_norows(lsqlite3, "PRAGMA auto_vacuum=1;") != 0) {
1670 /* Establish a busy timeout of 30 seconds */
1671 if ((ret = sqlite3_busy_timeout(lsqlite3->sqlite,
1672 30000)) != SQLITE_OK) {
1676 /* Create a function, callable from sql, to increment a tree_key */
1678 sqlite3_create_function(lsqlite3->sqlite,/* handle */
1679 "base160_next", /* function name */
1680 1, /* number of args */
1681 SQLITE_ANY, /* preferred text type */
1682 NULL, /* user data */
1683 base160next_sql, /* called func */
1684 NULL, /* step func */
1685 NULL /* final func */
1690 /* Create a function, callable from sql, to convert int to base160 */
1692 sqlite3_create_function(lsqlite3->sqlite,/* handle */
1693 "base160", /* function name */
1694 1, /* number of args */
1695 SQLITE_ANY, /* preferred text type */
1696 NULL, /* user data */
1697 base160_sql, /* called func */
1698 NULL, /* step func */
1699 NULL /* final func */
1704 /* Begin a transaction */
1705 if ((ret = query_norows(lsqlite3, "BEGIN EXCLUSIVE;")) != 0) {
1709 /* Determine if this is a new database. No tables means it is. */
1710 if (query_int(lsqlite3,
1713 " FROM sqlite_master\n"
1714 " WHERE type = 'table';") != 0) {
1715 query_norows(lsqlite3, "ROLLBACK;");
1719 if (queryInt == 0) {
1721 * Create the database schema
1723 for (pTail = discard_const_p(char, schema);
1724 pTail != NULL && *pTail != '\0';
1727 if ((ret = sqlite3_prepare(
1732 &pTail)) != SQLITE_OK ||
1733 (ret = sqlite3_step(stmt)) != SQLITE_DONE ||
1734 (ret = sqlite3_finalize(stmt)) != SQLITE_OK) {
1736 query_norows(lsqlite3, "ROLLBACK;");
1737 (void) sqlite3_close(lsqlite3->sqlite);
1743 * Ensure that the database we opened is one of ours
1745 if (query_int(lsqlite3,
1748 " (SELECT COUNT(*) = 2"
1749 " FROM sqlite_master "
1750 " WHERE type = 'table' "
1754 " 'ldb_object_classes' "
1760 " WHERE database_type = 'LDB' "
1761 " AND version = '1.0'"
1765 /* It's not one that we created. See ya! */
1766 query_norows(lsqlite3, "ROLLBACK;");
1767 (void) sqlite3_close(lsqlite3->sqlite);
1768 return SQLITE_MISUSE;
1773 * Create a temporary table to hold attributes requested in the result
1776 query_norows(lsqlite3, "DROP TABLE " RESULT_ATTR_TABLE ";\n");
1778 query_norows(lsqlite3,
1779 "CREATE " TEMPTAB " TABLE " RESULT_ATTR_TABLE "\n"
1781 " attr_name TEXT PRIMARY KEY\n"
1783 query_norows(lsqlite3, "ROLLBACK;");
1787 /* Commit the transaction */
1788 if ((ret = query_norows(lsqlite3, "COMMIT;")) != 0) {
1789 query_norows(lsqlite3, "ROLLBACK;");
1799 struct lsqlite3_private *lsqlite3 = p;
1801 if (lsqlite3->sqlite) {
1802 sqlite3_close(lsqlite3->sqlite);
1810 * Table of operations for the sqlite3 backend
1812 static const struct ldb_module_ops lsqlite3_ops = {
1814 .search = lsqlite3_search,
1815 .search_bytree = lsqlite3_search_bytree,
1816 .add_record = lsqlite3_add,
1817 .modify_record = lsqlite3_modify,
1818 .delete_record = lsqlite3_delete,
1819 .rename_record = lsqlite3_rename,
1820 .named_lock = lsqlite3_lock,
1821 .named_unlock = lsqlite3_unlock,
1822 .errstring = lsqlite3_errstring
1826 * connect to the database
1828 int lsqlite3_connect(struct ldb_context *ldb,
1831 const char *options[])
1835 struct lsqlite3_private * lsqlite3 = NULL;
1837 lsqlite3 = talloc(ldb, struct lsqlite3_private);
1842 lsqlite3->sqlite = NULL;
1843 lsqlite3->options = NULL;
1844 lsqlite3->lock_count = 0;
1846 ret = initialize(lsqlite3, url);
1847 if (ret != SQLITE_OK) {
1851 talloc_set_destructor(lsqlite3, destructor);
1853 ldb->modules = talloc(ldb, struct ldb_module);
1854 if (!ldb->modules) {
1857 ldb->modules->ldb = ldb;
1858 ldb->modules->prev = ldb->modules->next = NULL;
1859 ldb->modules->private_data = lsqlite3;
1860 ldb->modules->ops = &lsqlite3_ops;
1864 * take a copy of the options array, so we don't have to rely
1865 * on the caller keeping it around (it might be dynamic)
1867 for (i=0;options[i];i++) ;
1869 lsqlite3->options = talloc_array(lsqlite3, char *, i+1);
1870 if (!lsqlite3->options) {
1874 for (i=0;options[i];i++) {
1876 lsqlite3->options[i+1] = NULL;
1877 lsqlite3->options[i] =
1878 talloc_strdup(lsqlite3->options, options[i]);
1879 if (!lsqlite3->options[i]) {
1888 if (lsqlite3->sqlite != NULL) {
1889 (void) sqlite3_close(lsqlite3->sqlite);
1891 talloc_free(lsqlite3);