4 Copyright (C) Andrew Tridgell 2004
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)
36 #include "ldb/include/ldb.h"
37 #include "ldb/include/ldb_private.h"
38 #include "ldb/ldb_sqlite3/ldb_sqlite3.h"
42 # define True (! False)
45 #define QUERY(lsqlite3, pppValues, pNumRows, bRollbackOnError, sql...) \
48 if (lsqlite3_query(lsqlite3, \
52 if (bRollbackOnError) { \
53 lsqlite3_query(lsqlite3, \
64 lsqlite3_query(const struct lsqlite3_private *lsqlite3,
74 lsqlite3_create_attr_table(struct ldb_module *module,
83 * we don't need this right now, but will once we add some backend options
85 * find an option in an option list (a null terminated list of strings)
87 * this assumes the list is short. If it ever gets long then we really should
88 * do this in some smarter way
91 lsqlite3_option_find(const struct lsqlite3_private *lsqlite3,
95 size_t len = strlen(name);
97 if (!lsqlite3->options) return NULL;
99 for (i=0;lsqlite3->options[i];i++) {
100 if (strncmp(lsqlite3->options[i], name, len) == 0 &&
101 lsqlite3->options[i][len] == '=') {
102 return &lsqlite3->options[i][len+1];
111 callback function used in call to ldb_dn_fold() for determining whether an
112 attribute type requires case folding.
114 static int lsqlite3_case_fold_attr_required(struct ldb_module *module,
117 #warning "currently, all attributes require case folding"
126 lsqlite3_rename(struct ldb_module *module,
130 /* ignore ltdb specials */
131 if (olddn[0] == '@' ||newdn[0] == '@') {
135 #warning "lsqlite3_rename() is not yet supported"
143 lsqlite3_delete(struct ldb_module *module,
146 /* ignore ltdb specials */
154 #if 0 /* not currently used * /
156 * free a search result
159 lsqlite3_search_free(struct ldb_module *module,
160 struct ldb_message **res)
169 * add a single set of ldap message values to a ldb_message
172 /* get things to compile before we actually implement this function */
178 #warning "lsqlite3_add_msg_attr() not yet implemented or used"
181 lsqlite3_add_msg_attr(struct ldb_context *ldb,
182 struct ldb_message *msg,
184 struct berval **bval)
188 struct ldb_message_element * el;
190 count = ldap_count_values_len(bval);
196 el = talloc_realloc(msg, msg->elements, struct ldb_message_element,
197 msg->num_elements + 1);
205 el = &msg->elements[msg->num_elements];
207 el->name = talloc_strdup(msg->elements, attr);
215 el->values = talloc_array(msg->elements, struct ldb_val, count);
221 for (i=0;i<count;i++) {
222 el->values[i].data = talloc_memdup(el->values, bval[i]->bv_val, bval[i]->bv_len);
223 if (!el->values[i].data) {
226 el->values[i].length = bval[i]->bv_len;
237 * search for matching records
240 lsqlite3_search(struct ldb_module *module,
242 enum ldb_scope scope,
243 const char *expression,
244 const char * const attrs[],
245 struct ldb_message ***res)
247 #warning "lsqlite3_search() not yet implemented"
251 struct ldb_context * ldb = module->ldb;
252 struct lsqlite3_private * lsqlite3 = module->private_data;
258 lsqlite3->last_rc = ldap_search_s(lsqlite3->ldap, base, (int)scope,
260 discard_const_p(char *, attrs),
262 if (lsqlite3->last_rc != LDAP_SUCCESS) {
266 count = ldap_count_entries(lsqlite3->ldap, ldapres);
267 if (count == -1 || count == 0) {
268 ldap_msgfree(ldapres);
272 (*res) = talloc_array(lsqlite3, struct ldb_message *, count+1);
274 ldap_msgfree(ldapres);
283 /* loop over all messages */
284 for (msg=ldap_first_entry(lsqlite3->ldap, ldapres);
286 msg=ldap_next_entry(lsqlite3->ldap, msg)) {
287 BerElement *berptr = NULL;
290 if (msg_count == count) {
291 /* hmm, got too many? */
292 ldb_debug(ldb, LDB_DEBUG_FATAL, "Fatal: ldap message count inconsistent\n");
296 (*res)[msg_count] = talloc(*res, struct ldb_message);
297 if (!(*res)[msg_count]) {
300 (*res)[msg_count+1] = NULL;
302 dn = ldap_get_dn(lsqlite3->ldap, msg);
307 (*res)[msg_count]->dn = talloc_strdup((*res)[msg_count], dn);
309 if (!(*res)[msg_count]->dn) {
314 (*res)[msg_count]->num_elements = 0;
315 (*res)[msg_count]->elements = NULL;
316 (*res)[msg_count]->private_data = NULL;
318 /* loop over all attributes */
319 for (attr=ldap_first_attribute(lsqlite3->ldap, msg, &berptr);
321 attr=ldap_next_attribute(lsqlite3->ldap, msg, berptr)) {
322 struct berval **bval;
323 bval = ldap_get_values_len(lsqlite3->ldap, msg, attr);
326 lsqlite3_add_msg_attr(ldb, (*res)[msg_count], attr, bval);
327 ldap_value_free_len(bval);
332 if (berptr) ber_free(berptr, 0);
337 ldap_msgfree(ldapres);
342 if (*res) lsqlite3_search_free(module, *res);
351 * Issue a series of SQL statements to implement the ADD/MODIFY/DELETE
352 * requests in the ldb_message
355 lsqlite3_msg_to_sql(struct ldb_module *module,
356 const struct ldb_message *msg,
363 struct lsqlite3_private * lsqlite3 = module->private_data;
365 for (i = 0; i < msg->num_elements; i++) {
366 const struct ldb_message_element *el = &msg->elements[i];
369 flags = LDB_FLAG_MOD_ADD;
371 flags = el->flags & LDB_FLAG_MOD_MASK;
374 if (flags == LDB_FLAG_MOD_ADD) {
375 /* Create the attribute table if it doesn't exist */
376 if (lsqlite3_create_attr_table(module,
382 /* For each value of the specified attribute name... */
383 for (j = 0; j < el->num_values; j++) {
385 /* ... bind the attribute value, if necessary */
387 case LDB_FLAG_MOD_ADD:
391 "INSERT INTO ldb_attr_%q "
392 " (eid, attr_value) "
395 eid, el->values[j].data);
401 " add_attr(entry_data, %Q, %Q) "
402 " WHERE eid = %lld;",
403 el->name, el->values[j].data, eid);
407 case LDB_FLAG_MOD_REPLACE:
411 "UPDATE ldb_attr_%q "
412 " SET attr_value = %Q "
413 " WHERE eid = %lld;",
414 el->values[j].data, eid);
420 " mod_attr(entry_data, %Q, %Q) "
421 " WHERE eid = %lld;",
422 el->name, el->values[j].data, eid);
425 case LDB_FLAG_MOD_DELETE:
426 /* No additional parameters to this query */
430 "DELETE FROM ldb_attr_%q "
432 " AND attr_value = %Q;",
433 eid, el->values[j].data);
439 " del_attr(entry_data, %Q, %Q) "
440 " WHERE eid = %lld;",
441 el->name, el->values[j].data, eid);
452 lsqlite3_insert_dn(struct lsqlite3_private * lsqlite3,
464 lsqlite3_add(struct ldb_module *module,
465 const struct ldb_message *msg)
468 struct lsqlite3_private * lsqlite3 = module->private_data;
470 /* ignore ltdb specials */
471 if (msg->dn[0] == '@') {
475 /* Begin a transaction */
476 QUERY(lsqlite3, NULL, NULL, False, "BEGIN EXCLUSIVE;");
479 * Build any portions of the directory tree that don't exist. If the
480 * final component already exists, it's an error.
482 if (lsqlite3_insert_dn(lsqlite3,
485 lsqlite3_case_fold_attr_required),
487 QUERY(lsqlite3, NULL, NULL, False, "ROLLBACK;");
491 /* Add attributes to this new entry */
492 if (lsqlite3_msg_to_sql(module, msg, eid, False) != 0) {
493 QUERY(lsqlite3, NULL, NULL, False, "ROLLBACK;");
497 /* Everything worked. Commit it! */
498 QUERY(lsqlite3, NULL, NULL, True, "COMMIT;");
507 lsqlite3_modify(struct ldb_module *module,
508 const struct ldb_message *msg)
513 struct lsqlite3_private * lsqlite3 = module->private_data;
515 /* ignore ltdb specials */
516 if (msg->dn[0] == '@') {
520 /* Begin a transaction */
521 QUERY(lsqlite3, NULL, NULL, False, "BEGIN EXCLUSIVE;");
523 /* Get the id of this DN. */
533 lsqlite3_case_fold_attr_required));
538 sqlite3_free_table(ppValues);
542 /* Retrieve the eid */
543 eid = strtoll(ppValues[1], NULL, 10);
545 /* Modify attributes as specified */
546 if (lsqlite3_msg_to_sql(module, msg, eid, False) != 0) {
547 QUERY(lsqlite3, NULL, NULL, False, "ROLLBACK;");
551 /* Everything worked. Commit it! */
552 QUERY(lsqlite3, NULL, NULL, True, "COMMIT;");
557 lsqlite3_lock(struct ldb_module *module,
558 const char *lockname)
560 if (lockname == NULL) {
564 /* TODO implement a local locking mechanism here */
570 lsqlite3_unlock(struct ldb_module *module,
571 const char *lockname)
573 if (lockname == NULL) {
577 /* TODO implement a local locking mechanism here */
583 * return extended error information
586 lsqlite3_errstring(struct ldb_module *module)
588 struct lsqlite3_private * lsqlite3 = module->private_data;
590 return sqlite3_errmsg(lsqlite3->sqlite);
594 static const struct ldb_module_ops lsqlite3_ops = {
608 lsqlite3_destructor(void *p)
610 struct lsqlite3_private * lsqlite3 = p;
612 (void) sqlite3_close(lsqlite3->sqlite);
617 lsqlite3_initialize(struct lsqlite3_private *lsqlite3,
621 int bNewDatabase = False;
626 const char * schema =
627 "-- ------------------------------------------------------"
629 "PRAGMA auto_vacuum=1;"
631 "-- ------------------------------------------------------"
635 "-- ------------------------------------------------------"
637 "CREATE TABLE ldb_info AS"
638 " SELECT 'LDB' AS database_type,"
641 "-- ------------------------------------------------------"
645 " * The entry table holds the information about an entry. "
646 " * This table is used to obtain the EID of the entry and to "
647 " * support scope=one and scope=base. The parent and child"
648 " * table is included in the entry table since all the other"
649 " * attributes are dependent on EID."
651 "CREATE TABLE ldb_entry"
653 " -- Unique identifier of this LDB entry"
654 " eid INTEGER PRIMARY KEY,"
656 " -- Unique identifier of the parent LDB entry"
657 " peid INTEGER REFERENCES ldb_entry,"
659 " -- Distinguished name of this entry"
662 " -- Time when the entry was created"
663 " create_timestamp INTEGER,"
665 " -- Time when the entry was last modified"
666 " modify_timestamp INTEGER,"
668 " -- Attributes of this entry, in the form"
669 " -- attr\1value\0[attr\1value\0]*\0"
675 " * The purpose of the descendant table is to support the"
676 " * subtree search feature. For each LDB entry with a unique"
677 " * ID (AEID), this table contains the unique identifiers"
678 " * (DEID) of the descendant entries."
680 " * For evern entry in the directory, a row exists in this"
681 " * table for each of its ancestors including itself. The "
682 " * size of the table depends on the depth of each entry. In "
683 " * the worst case, if all the entries were at the same "
684 " * depth, the number of rows in the table is O(nm) where "
685 " * n is the number of nodes in the directory and m is the "
686 " * depth of the tree. "
688 "CREATE TABLE ldb_descendants"
690 " -- The unique identifier of the ancestor LDB entry"
691 " aeid INTEGER REFERENCES ldb_entry,"
693 " -- The unique identifier of the descendant LDB entry"
694 " deid INTEGER REFERENCES ldb_entry"
698 "CREATE TABLE ldb_object_classes"
700 " -- Object classes are inserted into this table to track"
701 " -- their class hierarchy. 'top' is the top-level class"
702 " -- of which all other classes are subclasses."
703 " class_name TEXT PRIMARY KEY,"
705 " -- tree_key tracks the position of the class in"
707 " tree_key TEXT UNIQUE"
711 " * There is one attribute table per searchable attribute."
714 "CREATE TABLE ldb_attr_ATTRIBUTE_NAME"
716 " -- The unique identifier of the LDB entry"
717 " eid INTEGER REFERENCES ldb_entry,"
719 " -- Normalized attribute value"
725 "-- ------------------------------------------------------"
729 "-- ------------------------------------------------------"
732 "CREATE TRIGGER ldb_entry_insert_tr"
738 " SET create_timestamp = strftime('%s', 'now'),"
739 " modify_timestamp = strftime('%s', 'now')"
740 " WHERE eid = new.eid;"
743 "CREATE TRIGGER ldb_entry_update_tr"
749 " SET modify_timestamp = strftime('%s', 'now')"
750 " WHERE eid = old.eid;"
753 "-- ------------------------------------------------------"
754 "-- Table initialization"
756 "/* We need an implicit 'top' level object class */"
757 "INSERT INTO ldb_attributes (attr_name,"
761 "-- ------------------------------------------------------"
765 "-- ------------------------------------------------------"
768 /* Skip protocol indicator of url */
769 if ((p = strchr(url, ':')) == NULL) {
770 return SQLITE_MISUSE;
776 * See if we'll be creating a new database, or opening an existing one
778 if ((stat(p, &statbuf) < 0 && errno == ENOENT) ||
779 statbuf.st_size == 0) {
784 /* Try to open the (possibly empty/non-existent) database */
785 if ((ret = sqlite3_open(p, &lsqlite3->sqlite)) != SQLITE_OK) {
791 * Create the database schema
793 for (pTail = discard_const_p(char, schema); pTail != NULL; ) {
795 if ((ret = sqlite3_prepare(
800 &pTail)) != SQLITE_OK ||
801 (ret = sqlite3_step(stmt)) != SQLITE_DONE ||
802 (ret = sqlite3_finalize(stmt)) != SQLITE_OK) {
804 (void) sqlite3_close(lsqlite3->sqlite);
810 * Ensure that the database we opened is one of ours
812 if ((ret = sqlite3_prepare(
815 " FROM sqlite_master "
816 " WHERE type = 'table' "
820 " 'ldb_descendants', "
821 " 'ldb_object_classes' "
825 &pTail)) != SQLITE_OK ||
826 (ret = sqlite3_step(stmt)) != SQLITE_ROW ||
827 sqlite3_column_int(stmt, 0) != 3 ||
828 (ret = sqlite3_finalize(stmt)) != SQLITE_OK ||
830 (ret = sqlite3_prepare(
834 " WHERE database_type = 'LDB' "
835 " AND version = '1.0';",
838 &pTail)) != SQLITE_OK ||
839 (ret = sqlite3_step(stmt)) != SQLITE_ROW ||
840 (ret = sqlite3_finalize(stmt)) != SQLITE_OK) {
842 /* It's not one that we created. See ya! */
843 (void) sqlite3_close(lsqlite3->sqlite);
844 return SQLITE_MISUSE;
852 * connect to the database
855 lsqlite3_connect(const char *url,
857 const char *options[])
861 struct ldb_context * ldb = NULL;
862 struct lsqlite3_private * lsqlite3 = NULL;
864 ldb = talloc(NULL, struct ldb_context);
870 lsqlite3 = talloc(ldb, struct lsqlite3_private);
876 lsqlite3->sqlite = NULL;
877 lsqlite3->options = NULL;
878 lsqlite3->lock_count = 0;
880 ret = lsqlite3_initialize(&lsqlite3, url);
881 if (ret != SQLITE_OK) {
885 talloc_set_destructor(lsqlite3, lsqlite3_destructor);
887 ldb->modules = talloc(ldb, struct ldb_module);
892 ldb->modules->ldb = ldb;
893 ldb->modules->prev = ldb->modules->next = NULL;
894 ldb->modules->private_data = lsqlite3;
895 ldb->modules->ops = &lsqlite3_ops;
899 * take a copy of the options array, so we don't have to rely
900 * on the caller keeping it around (it might be dynamic)
902 for (i=0;options[i];i++) ;
904 lsqlite3->options = talloc_array(lsqlite3, char *, i+1);
905 if (!lsqlite3->options) {
909 for (i=0;options[i];i++) {
911 lsqlite3->options[i+1] = NULL;
912 lsqlite3->options[i] =
913 talloc_strdup(lsqlite3->options, options[i]);
914 if (!lsqlite3->options[i]) {
923 if (lsqlite3->sqlite != NULL) {
924 (void) sqlite3_close(lsqlite3->sqlite);