4 Copyright (C) Andrew Tridgell 2004
5 Copyright (C) Derrell Lipman 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_private.h"
40 #include "ldb/ldb_sqlite3/ldb_sqlite3.h"
44 # define TRUE (! FALSE)
47 #define QUERY_NOROWS(lsqlite3, bRollbackOnError, sql...) \
49 if (lsqlite3_query_norows(lsqlite3, sql) != 0) { \
50 if (bRollbackOnError) { \
51 lsqlite3_query_norows(lsqlite3, \
60 * lsqlite3_query_norows()
62 * This function is used for queries that are not expected to return any rows,
63 * e.g. BEGIN, COMMIT, ROLLBACK, CREATE TABLE, INSERT, UPDATE, DELETE, etc.
64 * There are no provisions here for returning data from rows in a table, so do
65 * not pass SELECT queries to this function.
68 lsqlite3_query_norows(const struct lsqlite3_private *lsqlite3,
79 /* Begin access to variable argument list */
82 /* Format the query */
83 if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
88 * Prepare and execute the SQL statement. Loop allows retrying on
89 * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
90 * requiring retrying the operation.
92 for (bLoop = TRUE; bLoop; ) {
94 /* Compile the SQL statement into sqlite virtual machine */
95 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
99 &pTail)) != SQLITE_OK) {
104 /* No rows expected, so just step through machine code once */
105 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
106 (void) sqlite3_finalize(pStmt);
108 } else if (ret != SQLITE_DONE) {
109 (void) sqlite3_finalize(pStmt);
114 /* Free the virtual machine */
115 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
116 (void) sqlite3_finalize(pStmt);
118 } else if (ret != SQLITE_OK) {
119 (void) sqlite3_finalize(pStmt);
125 * Normal condition is only one time through loop. Loop is
126 * rerun in error conditions, via "continue", above.
132 /* All done with variable argument list */
135 /* Free the memory we allocated for our query string */
144 * we don't need this right now, but will once we add some backend options
146 * find an option in an option list (a null terminated list of strings)
148 * this assumes the list is short. If it ever gets long then we really should
149 * do this in some smarter way
152 lsqlite3_option_find(const struct lsqlite3_private *lsqlite3,
156 size_t len = strlen(name);
158 if (!lsqlite3->options) return NULL;
160 for (i=0;lsqlite3->options[i];i++) {
161 if (strncmp(lsqlite3->options[i], name, len) == 0 &&
162 lsqlite3->options[i][len] == '=') {
163 return &lsqlite3->options[i][len+1];
172 callback function used in call to ldb_dn_fold() for determining whether an
173 attribute type requires case folding.
175 static int lsqlite3_case_fold_attr_required(struct ldb_module *module,
178 #warning "currently, all attributes require case folding"
187 lsqlite3_rename(struct ldb_module *module,
191 /* ignore ltdb specials */
192 if (olddn[0] == '@' ||newdn[0] == '@') {
196 #warning "lsqlite3_rename() is not yet supported"
204 lsqlite3_delete(struct ldb_module *module,
207 /* ignore ltdb specials */
215 #if 0 /* not currently used */
217 * free a search result
220 lsqlite3_search_free(struct ldb_module *module,
221 struct ldb_message **res)
230 * add a single set of ldap message values to a ldb_message
233 /* get things to compile before we actually implement this function */
239 #warning "lsqlite3_add_msg_attr() not yet implemented or used"
242 lsqlite3_add_msg_attr(struct ldb_context *ldb,
243 struct ldb_message *msg,
245 struct berval **bval)
249 struct ldb_message_element * el;
251 count = ldap_count_values_len(bval);
257 el = talloc_realloc(msg, msg->elements, struct ldb_message_element,
258 msg->num_elements + 1);
266 el = &msg->elements[msg->num_elements];
268 el->name = talloc_strdup(msg->elements, attr);
276 el->values = talloc_array(msg->elements, struct ldb_val, count);
282 for (i=0;i<count;i++) {
283 el->values[i].data = talloc_memdup(el->values, bval[i]->bv_val, bval[i]->bv_len);
284 if (!el->values[i].data) {
287 el->values[i].length = bval[i]->bv_len;
298 * search for matching records
301 lsqlite3_search(struct ldb_module *module,
303 enum ldb_scope scope,
304 const char *expression,
305 const char * const attrs[],
306 struct ldb_message ***res)
308 #warning "lsqlite3_search() not yet implemented"
312 struct ldb_context * ldb = module->ldb;
313 struct lsqlite3_private * lsqlite3 = module->private_data;
319 lsqlite3->last_rc = ldap_search_s(lsqlite3->ldap, base, (int)scope,
321 discard_const_p(char *, attrs),
323 if (lsqlite3->last_rc != LDAP_SUCCESS) {
327 count = ldap_count_entries(lsqlite3->ldap, ldapres);
328 if (count == -1 || count == 0) {
329 ldap_msgfree(ldapres);
333 (*res) = talloc_array(lsqlite3, struct ldb_message *, count+1);
335 ldap_msgfree(ldapres);
344 /* loop over all messages */
345 for (msg=ldap_first_entry(lsqlite3->ldap, ldapres);
347 msg=ldap_next_entry(lsqlite3->ldap, msg)) {
348 BerElement *berptr = NULL;
351 if (msg_count == count) {
352 /* hmm, got too many? */
353 ldb_debug(ldb, LDB_DEBUG_FATAL, "Fatal: ldap message count inconsistent\n");
357 (*res)[msg_count] = talloc(*res, struct ldb_message);
358 if (!(*res)[msg_count]) {
361 (*res)[msg_count+1] = NULL;
363 dn = ldap_get_dn(lsqlite3->ldap, msg);
368 (*res)[msg_count]->dn = talloc_strdup((*res)[msg_count], dn);
370 if (!(*res)[msg_count]->dn) {
375 (*res)[msg_count]->num_elements = 0;
376 (*res)[msg_count]->elements = NULL;
377 (*res)[msg_count]->private_data = NULL;
379 /* loop over all attributes */
380 for (attr=ldap_first_attribute(lsqlite3->ldap, msg, &berptr);
382 attr=ldap_next_attribute(lsqlite3->ldap, msg, berptr)) {
383 struct berval **bval;
384 bval = ldap_get_values_len(lsqlite3->ldap, msg, attr);
387 lsqlite3_add_msg_attr(ldb, (*res)[msg_count], attr, bval);
388 ldap_value_free_len(bval);
393 if (berptr) ber_free(berptr, 0);
398 ldap_msgfree(ldapres);
403 if (*res) lsqlite3_search_free(module, *res);
412 lsqlite3_new_attr(struct lsqlite3_private * lsqlite3,
415 QUERY_NOROWS(lsqlite3,
417 "CREATE TABLE ldb_attr_%q "
419 " eid INTEGER REFERENCES ldb_entry, "
428 * Issue a series of SQL statements to implement the ADD/MODIFY/DELETE
429 * requests in the ldb_message
432 lsqlite3_msg_to_sql(struct ldb_module *module,
433 const struct ldb_message *msg,
440 struct lsqlite3_private * lsqlite3 = module->private_data;
442 for (i = 0; i < msg->num_elements; i++) {
443 const struct ldb_message_element *el = &msg->elements[i];
446 flags = LDB_FLAG_MOD_ADD;
448 flags = el->flags & LDB_FLAG_MOD_MASK;
451 if (flags == LDB_FLAG_MOD_ADD) {
452 /* Create the attribute table if it doesn't exist */
453 if (lsqlite3_new_attr(lsqlite3, el->name) != 0) {
458 /* For each value of the specified attribute name... */
459 for (j = 0; j < el->num_values; j++) {
461 /* ... bind the attribute value, if necessary */
463 case LDB_FLAG_MOD_ADD:
464 QUERY_NOROWS(lsqlite3,
466 "INSERT INTO ldb_attr_%q "
467 " (eid, attr_value) "
470 eid, el->values[j].data);
471 QUERY_NOROWS(lsqlite3,
475 " add_attr(entry_data, "
477 " WHERE eid = %lld;",
478 el->name, el->values[j].data,
483 case LDB_FLAG_MOD_REPLACE:
484 QUERY_NOROWS(lsqlite3,
486 "UPDATE ldb_attr_%q "
487 " SET attr_value = %Q "
488 " WHERE eid = %lld;",
491 QUERY_NOROWS(lsqlite3,
495 " mod_attr(entry_data, "
497 " WHERE eid = %lld;",
498 el->name, el->values[j].data,
502 case LDB_FLAG_MOD_DELETE:
503 /* No additional parameters to this query */
504 QUERY_NOROWS(lsqlite3,
506 "DELETE FROM ldb_attr_%q "
508 " AND attr_value = %Q;",
511 QUERY_NOROWS(lsqlite3,
515 " del_attr(entry_data, "
517 " WHERE eid = %lld;",
518 el->name, el->values[j].data,
530 lsqlite3_new_dn(struct ldb_module *module,
537 /* Normalize the distinguished name */
538 pDN = ldb_dn_fold(module, pDN, lsqlite3_case_fold_attr_required);
540 /* Parse the DN into its constituent components */
541 #warning "this simple parse of DN ignores escaped '=' and ','. fix it."
542 while (pDN != NULL) {
543 pName = strsep(&pDN, ",");
546 /* Attribute name with value? Should not occur. */
551 strsep(&pValue, "=");
553 #warning "*** lsqlite3_new_dn() not yet fully implemented ***"
564 lsqlite3_add(struct ldb_module *module,
565 const struct ldb_message *msg)
568 struct lsqlite3_private * lsqlite3 = module->private_data;
570 /* ignore ltdb specials */
571 if (msg->dn[0] == '@') {
575 /* Begin a transaction */
576 QUERY_NOROWS(lsqlite3, FALSE, "BEGIN EXCLUSIVE;");
579 * Build any portions of the directory tree that don't exist. If the
580 * final component already exists, it's an error.
582 if (lsqlite3_new_dn(module, msg->dn, &eid) != 0) {
583 QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
587 /* Add attributes to this new entry */
588 if (lsqlite3_msg_to_sql(module, msg, eid, FALSE) != 0) {
589 QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
593 /* Everything worked. Commit it! */
594 QUERY_NOROWS(lsqlite3, TRUE, "COMMIT;");
603 lsqlite3_modify(struct ldb_module *module,
604 const struct ldb_message *msg)
611 sqlite3_stmt * pStmt;
612 struct lsqlite3_private * lsqlite3 = module->private_data;
614 /* ignore ltdb specials */
615 if (msg->dn[0] == '@') {
619 /* Begin a transaction */
620 QUERY_NOROWS(lsqlite3, FALSE, "BEGIN EXCLUSIVE;");
622 /* Format the query */
623 if ((p = sqlite3_mprintf(
629 lsqlite3_case_fold_attr_required))) == NULL) {
633 /* Get the id of this DN. */
634 for (bLoop = TRUE; bLoop; ) {
636 /* Compile the SQL statement into sqlite virtual machine */
637 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
641 &pTail)) != SQLITE_OK) {
646 /* One row expected */
647 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
648 (void) sqlite3_finalize(pStmt);
650 } else if (ret != SQLITE_ROW) {
651 (void) sqlite3_finalize(pStmt);
656 /* Retrieve the EID */
657 eid = sqlite3_column_int64(pStmt, 0);
659 /* Free the virtual machine */
660 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
661 (void) sqlite3_finalize(pStmt);
663 } else if (ret != SQLITE_OK) {
664 (void) sqlite3_finalize(pStmt);
669 /* Modify attributes as specified */
670 if (lsqlite3_msg_to_sql(module, msg, eid, FALSE) != 0) {
671 QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
676 * Normal condition is only one time through loop. Loop is
677 * rerun in error conditions, via "continue", above.
684 QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
688 /* Everything worked. Commit it! */
689 QUERY_NOROWS(lsqlite3, TRUE, "COMMIT;");
694 lsqlite3_lock(struct ldb_module *module,
695 const char *lockname)
697 if (lockname == NULL) {
701 /* TODO implement a local locking mechanism here */
707 lsqlite3_unlock(struct ldb_module *module,
708 const char *lockname)
710 if (lockname == NULL) {
714 /* TODO implement a local locking mechanism here */
720 * return extended error information
723 lsqlite3_errstring(struct ldb_module *module)
725 struct lsqlite3_private * lsqlite3 = module->private_data;
727 return sqlite3_errmsg(lsqlite3->sqlite);
731 static const struct ldb_module_ops lsqlite3_ops = {
745 lsqlite3_destructor(void *p)
747 struct lsqlite3_private * lsqlite3 = p;
749 (void) sqlite3_close(lsqlite3->sqlite);
754 lsqlite3_initialize(struct lsqlite3_private *lsqlite3,
758 int bNewDatabase = FALSE;
763 const char * schema =
764 "-- ------------------------------------------------------"
766 "PRAGMA auto_vacuum=1;"
768 "-- ------------------------------------------------------"
772 "-- ------------------------------------------------------"
774 "CREATE TABLE ldb_info AS"
775 " SELECT 'LDB' AS database_type,"
778 "-- ------------------------------------------------------"
782 " * The entry table holds the information about an entry. "
783 " * This table is used to obtain the EID of the entry and to "
784 " * support scope=one and scope=base. The parent and child"
785 " * table is included in the entry table since all the other"
786 " * attributes are dependent on EID."
788 "CREATE TABLE ldb_entry"
790 " -- Unique identifier of this LDB entry"
791 " eid INTEGER PRIMARY KEY,"
793 " -- Unique identifier of the parent LDB entry"
794 " peid INTEGER REFERENCES ldb_entry,"
796 " -- Distinguished name of this entry"
799 " -- Time when the entry was created"
800 " create_timestamp INTEGER,"
802 " -- Time when the entry was last modified"
803 " modify_timestamp INTEGER,"
805 " -- Attributes of this entry, in the form"
806 " -- attr\1value\0[attr\1value\0]*\0"
812 " * The purpose of the descendant table is to support the"
813 " * subtree search feature. For each LDB entry with a unique"
814 " * ID (AEID), this table contains the unique identifiers"
815 " * (DEID) of the descendant entries."
817 " * For evern entry in the directory, a row exists in this"
818 " * table for each of its ancestors including itself. The "
819 " * size of the table depends on the depth of each entry. In "
820 " * the worst case, if all the entries were at the same "
821 " * depth, the number of rows in the table is O(nm) where "
822 " * n is the number of nodes in the directory and m is the "
823 " * depth of the tree. "
825 "CREATE TABLE ldb_descendants"
827 " -- The unique identifier of the ancestor LDB entry"
828 " aeid INTEGER REFERENCES ldb_entry,"
830 " -- The unique identifier of the descendant LDB entry"
831 " deid INTEGER REFERENCES ldb_entry"
835 "CREATE TABLE ldb_object_classes"
837 " -- Object classes are inserted into this table to track"
838 " -- their class hierarchy. 'top' is the top-level class"
839 " -- of which all other classes are subclasses."
840 " class_name TEXT PRIMARY KEY,"
842 " -- tree_key tracks the position of the class in"
844 " tree_key TEXT UNIQUE"
848 " * There is one attribute table per searchable attribute."
851 "CREATE TABLE ldb_attr_ATTRIBUTE_NAME"
853 " -- The unique identifier of the LDB entry"
854 " eid INTEGER REFERENCES ldb_entry,"
856 " -- Normalized attribute value"
862 "-- ------------------------------------------------------"
866 "-- ------------------------------------------------------"
869 "CREATE TRIGGER ldb_entry_insert_tr"
875 " SET create_timestamp = strftime('%s', 'now'),"
876 " modify_timestamp = strftime('%s', 'now')"
877 " WHERE eid = new.eid;"
880 "CREATE TRIGGER ldb_entry_update_tr"
886 " SET modify_timestamp = strftime('%s', 'now')"
887 " WHERE eid = old.eid;"
890 "-- ------------------------------------------------------"
891 "-- Table initialization"
893 "/* We need an implicit 'top' level object class */"
894 "INSERT INTO ldb_attributes (attr_name,"
898 "-- ------------------------------------------------------"
902 "-- ------------------------------------------------------"
905 /* Skip protocol indicator of url */
906 if ((p = strchr(url, ':')) == NULL) {
907 return SQLITE_MISUSE;
913 * See if we'll be creating a new database, or opening an existing one
915 #warning "eliminate stat() here; concurrent processes could conflict"
916 if ((stat(p, &statbuf) < 0 && errno == ENOENT) ||
917 statbuf.st_size == 0) {
922 /* Try to open the (possibly empty/non-existent) database */
923 if ((ret = sqlite3_open(p, &lsqlite3->sqlite)) != SQLITE_OK) {
929 * Create the database schema
931 for (pTail = discard_const_p(char, schema); pTail != NULL; ) {
933 if ((ret = sqlite3_prepare(
938 &pTail)) != SQLITE_OK ||
939 (ret = sqlite3_step(stmt)) != SQLITE_DONE ||
940 (ret = sqlite3_finalize(stmt)) != SQLITE_OK) {
942 (void) sqlite3_close(lsqlite3->sqlite);
948 * Ensure that the database we opened is one of ours
950 if ((ret = sqlite3_prepare(
953 " FROM sqlite_master "
954 " WHERE type = 'table' "
958 " 'ldb_descendants', "
959 " 'ldb_object_classes' "
963 &pTail)) != SQLITE_OK ||
964 (ret = sqlite3_step(stmt)) != SQLITE_ROW ||
965 sqlite3_column_int(stmt, 0) != 3 ||
966 (ret = sqlite3_finalize(stmt)) != SQLITE_OK ||
968 (ret = sqlite3_prepare(
972 " WHERE database_type = 'LDB' "
973 " AND version = '1.0';",
976 &pTail)) != SQLITE_OK ||
977 (ret = sqlite3_step(stmt)) != SQLITE_ROW ||
978 (ret = sqlite3_finalize(stmt)) != SQLITE_OK) {
980 /* It's not one that we created. See ya! */
981 (void) sqlite3_close(lsqlite3->sqlite);
982 return SQLITE_MISUSE;
990 * connect to the database
993 lsqlite3_connect(const char *url,
995 const char *options[])
999 struct ldb_context * ldb = NULL;
1000 struct lsqlite3_private * lsqlite3 = NULL;
1002 ldb = talloc(NULL, struct ldb_context);
1008 lsqlite3 = talloc(ldb, struct lsqlite3_private);
1014 lsqlite3->sqlite = NULL;
1015 lsqlite3->options = NULL;
1016 lsqlite3->lock_count = 0;
1018 ret = lsqlite3_initialize(lsqlite3, url);
1019 if (ret != SQLITE_OK) {
1023 talloc_set_destructor(lsqlite3, lsqlite3_destructor);
1025 ldb->modules = talloc(ldb, struct ldb_module);
1026 if (!ldb->modules) {
1030 ldb->modules->ldb = ldb;
1031 ldb->modules->prev = ldb->modules->next = NULL;
1032 ldb->modules->private_data = lsqlite3;
1033 ldb->modules->ops = &lsqlite3_ops;
1037 * take a copy of the options array, so we don't have to rely
1038 * on the caller keeping it around (it might be dynamic)
1040 for (i=0;options[i];i++) ;
1042 lsqlite3->options = talloc_array(lsqlite3, char *, i+1);
1043 if (!lsqlite3->options) {
1047 for (i=0;options[i];i++) {
1049 lsqlite3->options[i+1] = NULL;
1050 lsqlite3->options[i] =
1051 talloc_strdup(lsqlite3->options, options[i]);
1052 if (!lsqlite3->options[i]) {
1061 if (lsqlite3->sqlite != NULL) {
1062 (void) sqlite3_close(lsqlite3->sqlite);