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"
40 #define QUERY(lsqlite3, pppValues, pNumRows, bRollbackOnError, sql...) \
43 if (query(lsqlite3, pppValues, pNumRows, sql) != 0) { \
44 if (bRollbackOnError) { \
45 query(lsqlite3, NULL, NULL, "ROLLBACK;"); \
54 * we don't need this right now, but will once we add some backend options
56 * find an option in an option list (a null terminated list of strings)
58 * this assumes the list is short. If it ever gets long then we really should
59 * do this in some smarter way
62 lsqlite3_option_find(const struct lsqlite3_private *lsqlite3,
66 size_t len = strlen(name);
68 if (!lsqlite3->options) return NULL;
70 for (i=0;lsqlite3->options[i];i++) {
71 if (strncmp(lsqlite3->options[i], name, len) == 0 &&
72 lsqlite3->options[i][len] == '=') {
73 return &lsqlite3->options[i][len+1];
85 lsqlite3_rename(struct ldb_module *module,
90 struct lsqlite3_private * lsqlite3 = module->private_data;
92 /* ignore ltdb specials */
93 if (olddn[0] == '@' ||newdn[0] == '@') {
97 #warning "rename() is not yet supported"
105 lsqlite3_delete(struct ldb_module *module,
110 struct lsqlite3_private * lsqlite3 = module->private_data;
112 /* ignore ltdb specials */
121 * free a search result
124 lsqlite3_search_free(struct ldb_module *module,
125 struct ldb_message **res)
133 * add a single set of ldap message values to a ldb_message
136 lsqlite3_add_msg_attr(struct ldb_context *ldb,
137 struct ldb_message *msg,
139 struct berval **bval)
143 struct ldb_message_element * el;
145 count = ldap_count_values_len(bval);
151 el = talloc_realloc(msg, msg->elements, struct ldb_message_element,
152 msg->num_elements + 1);
160 el = &msg->elements[msg->num_elements];
162 el->name = talloc_strdup(msg->elements, attr);
170 el->values = talloc_array(msg->elements, struct ldb_val, count);
176 for (i=0;i<count;i++) {
177 el->values[i].data = talloc_memdup(el->values, bval[i]->bv_val, bval[i]->bv_len);
178 if (!el->values[i].data) {
181 el->values[i].length = bval[i]->bv_len;
191 * search for matching records
194 lsqlite3_search(struct ldb_module *module,
196 enum ldb_scope scope,
197 const char *expression,
198 const char * const *attrs,
199 struct ldb_message ***res)
203 struct ldb_context * ldb = module->ldb;
204 struct lsqlite3_private * lsqlite3 = module->private_data;
210 lsqlite3->last_rc = ldap_search_s(lsqlite3->ldap, base, (int)scope,
212 discard_const_p(char *, attrs),
214 if (lsqlite3->last_rc != LDAP_SUCCESS) {
218 count = ldap_count_entries(lsqlite3->ldap, ldapres);
219 if (count == -1 || count == 0) {
220 ldap_msgfree(ldapres);
224 (*res) = talloc_array(lsqlite3, struct ldb_message *, count+1);
226 ldap_msgfree(ldapres);
235 /* loop over all messages */
236 for (msg=ldap_first_entry(lsqlite3->ldap, ldapres);
238 msg=ldap_next_entry(lsqlite3->ldap, msg)) {
239 BerElement *berptr = NULL;
242 if (msg_count == count) {
243 /* hmm, got too many? */
244 ldb_debug(ldb, LDB_DEBUG_FATAL, "Fatal: ldap message count inconsistent\n");
248 (*res)[msg_count] = talloc(*res, struct ldb_message);
249 if (!(*res)[msg_count]) {
252 (*res)[msg_count+1] = NULL;
254 dn = ldap_get_dn(lsqlite3->ldap, msg);
259 (*res)[msg_count]->dn = talloc_strdup((*res)[msg_count], dn);
261 if (!(*res)[msg_count]->dn) {
266 (*res)[msg_count]->num_elements = 0;
267 (*res)[msg_count]->elements = NULL;
268 (*res)[msg_count]->private_data = NULL;
270 /* loop over all attributes */
271 for (attr=ldap_first_attribute(lsqlite3->ldap, msg, &berptr);
273 attr=ldap_next_attribute(lsqlite3->ldap, msg, berptr)) {
274 struct berval **bval;
275 bval = ldap_get_values_len(lsqlite3->ldap, msg, attr);
278 lsqlite3_add_msg_attr(ldb, (*res)[msg_count], attr, bval);
279 ldap_value_free_len(bval);
284 if (berptr) ber_free(berptr, 0);
289 ldap_msgfree(ldapres);
294 if (*res) lsqlite3_search_free(module, *res);
300 * Issue a series of SQL statements to implement the ADD/MODIFY/DELETE
301 * requests in the ldb_message
304 lsqlite3_msg_to_sql(struct ldb_module *module,
305 const struct ldb_message *msg,
312 sqlite3_stmt * stmt = NULL;
313 struct ldb_context * ldb = module->ldb;
314 struct lsqlite3_private * lsqlite3 = module->private_data;
316 for (i = 0; i < msg->num_elements; i++) {
317 const struct ldb_message_element *el = &msg->elements[i];
320 flags = LDB_FLAG_MOD_ADD;
322 flags = el->flags & LDB_FLAG_MOD_MASK;
325 if (flags == LDB_FLAG_MOD_ADD) {
326 /* Create the attribute table if it doesn't exist */
327 if (create_attr_table(module, el->name) != 0) {
332 /* For each value of the specified attribute name... */
333 for (j = 0; j < el->num_values; j++) {
335 /* ... bind the attribute value, if necessary */
337 case LDB_FLAG_MOD_ADD:
341 "INSERT INTO ldb_attr_%q "
342 " (eid, attr_value) "
351 " add_attr(entry_data, %Q, %Q) "
352 " WHERE eid = %lld;",
353 el->name, el->data, eid);
357 case LDB_FLAG_MOD_REPLACE:
361 "UPDATE ldb_attr_%q "
362 " SET attr_value = %Q "
363 " WHERE eid = %lld;",
370 " mod_attr(entry_data, %Q, %Q) "
371 " WHERE eid = %lld;",
372 el->name, el->data, eid);
375 case LDB_FLAG_MOD_DELETE:
376 /* No additional parameters to this query */
380 "DELETE FROM ldb_attr_%q "
382 " AND attr_value = %Q;",
389 " del_attr(entry_data, %Q, %Q) "
390 " WHERE eid = %lld;",
391 el->name, el->data, eid);
402 lsqlite3_normalize_dn(struct ldb_context * ldb,
409 pNormalized = talloc_size(ldb, strlen(pDN) + 1);
410 if (pNormalized == NULL) {
415 for (pSrc = pDN, pDest = pNormalized; *pSrc != '\0'; ) {
422 lsqlite3_insert_dn_recursive(struct lsqlite3_private * lsqlite3,
434 lsqlite3_add(struct ldb_module *module,
435 const struct ldb_message *msg)
439 struct ldb_context * ldb = module->ldb;
440 struct lsqlite3_private * lsqlite3 = module->private_data;
442 /* ignore ltdb specials */
443 if (msg->dn[0] == '@') {
447 /* Begin a transaction */
448 QUERY(lsqlite3, NULL, NULL< FALSE, "BEGIN EXCLUSIVE;");
451 * Build any portions of the directory tree that don't exist. If the
452 * final component already exists, it's an error.
454 if (lsqlite3_insert_dn_recursive(lsqlite3,
455 lsqlite3_normalize_dn(ldb, msg->dn),
457 QUERY(lsqlite3, NULL, NULL, FALSE, "ROLLBACK;");
461 /* Add attributes to this new entry */
462 if (lsqlite3_msg_to_sql(module, msg, eid, FALSE) != 0) {
463 QUERY(lsqlite3, NULL, NULL, FALSE, "ROLLBACK;");
467 /* Everything worked. Commit it! */
468 QUERY(lsqlite3, NULL, NULL, TRUE, "COMMIT;");
477 lsqlite3_modify(struct ldb_module *module,
478 const struct ldb_message *msg)
483 struct ldb_context * ldb = module->ldb;
484 struct lsqlite3_private * lsqlite3 = module->private_data;
486 /* ignore ltdb specials */
487 if (msg->dn[0] == '@') {
491 /* Begin a transaction */
492 QUERY(lsqlite3, NULL, NULL, FALSE, "BEGIN EXCLUSIVE;");
494 /* Get the id of this DN. */
502 lsqlite3_normalize_dn(ldb, msg->dn));
507 sqlite_free_table(ppValues);
511 /* Retrieve the eid */
512 eid = strtoll(ppValues[1], NULL, 10);
514 /* Modify attributes as specified */
515 if (lsqlite3_msg_to_sql(module, msg, eid, FALSE) != 0) {
516 QUERY(lsqlite3, NULL, NULL, FALSE, "ROLLBACK;");
520 /* Everything worked. Commit it! */
521 QUERY(lsqlite3, NULL, NULL, TRUE, "COMMIT;");
526 lsqlite3_lock(struct ldb_module *module,
527 const char *lockname)
530 struct ldb_context * ldb = module->ldb;
531 struct lsqlite3_private * lsqlite3 = module->private_data;
533 if (lockname == NULL) {
537 /* TODO implement a local locking mechanism here */
543 lsqlite3_unlock(struct ldb_module *module,
544 const char *lockname)
547 struct ldb_context * ldb = module->ldb;
548 struct lsqlite3_private * lsqlite3 = module->private_data;
550 if (lockname == NULL) {
554 /* TODO implement a local locking mechanism here */
560 * return extended error information
563 lsqlite3_errstring(struct ldb_module *module)
565 struct lsqlite3_private * lsqlite3 = module->private_data;
567 return sqlite3_errmsg(lsqlite3->sqlite3);
571 static const struct ldb_module_ops lsqlite3_ops = {
574 lsqlite3_search_free,
586 lsqlite3_destructor(void *p)
588 struct lsqlite3_private * lsqlite3 = p;
590 (void) sqlite3_close(lsqlite3->sqlite3);
595 lsqlite3_initialize(lsqlite3_private *lsqlite3,
599 int bNewDatabase = False;
604 const char * schema =
606 -- ------------------------------------------------------
608 PRAGMA auto_vacuum=1;
610 -- ------------------------------------------------------
614 -- ------------------------------------------------------
616 CREATE TABLE ldb_info AS
617 SELECT 'LDB' AS database_type,
620 -- ------------------------------------------------------
624 * The entry table holds the information about an entry. This
625 * table is used to obtain the EID of the entry and to support
626 * scope=one and scope=base. The parent and child table
627 * is included in the entry table since all the other
630 CREATE TABLE ldb_entry
632 -- Unique identifier of this LDB entry
633 eid INTEGER PRIMARY KEY,
635 -- Unique identifier of the parent LDB entry
636 peid INTEGER REFERENCES ldb_entry,
638 -- Distinguished name of this entry
641 -- Time when the entry was created
642 create_timestamp INTEGER,
644 -- Time when the entry was last modified
645 modify_timestamp INTEGER,
647 -- Attributes of this entry, in the form
648 -- attr\1value\0[attr\1value\0]*\0
654 * The purpose of the descendant table is to support the
655 * subtree search feature. For each LDB entry with a unique
656 * ID (AEID), this table contains the unique identifiers
657 * (DEID) of the descendant entries.
659 * For evern entry in the directory, a row exists in this
660 * table for each of its ancestors including itself. The size
661 * of the table depends on the depth of each entry. In the
662 * worst case, if all the entries were at the same depth, the
663 * number of rows in the table is O(nm) where n is the number
664 * of nodes in the directory and m is the depth of the tree.
666 CREATE TABLE ldb_descendants
668 -- The unique identifier of the ancestor LDB entry
669 aeid INTEGER REFERENCES ldb_entry,
671 -- The unique identifier of the descendant LDB entry
672 deid INTEGER REFERENCES ldb_entry
676 CREATE TABLE ldb_object_classes
678 -- Object classes are inserted into this table to track
679 -- their class hierarchy. 'top' is the top-level class
680 -- of which all other classes are subclasses.
681 class_name TEXT PRIMARY KEY,
683 -- tree_key tracks the position of the class in
689 * There is one attribute table per searchable attribute.
692 CREATE TABLE ldb_attr_ATTRIBUTE_NAME
694 -- The unique identifier of the LDB entry
695 eid INTEGER REFERENCES ldb_entry,
697 -- Normalized attribute value
703 -- ------------------------------------------------------
707 -- ------------------------------------------------------
710 CREATE TRIGGER ldb_entry_insert_tr
716 SET create_timestamp = strftime('%s', 'now'),
717 modify_timestamp = strftime('%s', 'now')
721 CREATE TRIGGER ldb_entry_update_tr
727 SET modify_timestamp = strftime('%s', 'now')
731 -- ------------------------------------------------------
732 -- Table initialization
734 /* We need an implicit "top" level object class */
735 INSERT INTO ldb_attributes (attr_name,
739 -- ------------------------------------------------------
743 -- ------------------------------------------------------
746 /* Skip protocol indicator of url */
747 if ((p = strchr(url, ':')) == NULL) {
748 return SQLITE_MISUSE;
754 * See if we'll be creating a new database, or opening an existing one
756 if ((stat(p, &statbuf) < 0 && errno == ENOENT) ||
757 statbuf.st_size == 0) {
762 /* Try to open the (possibly empty/non-existent) database */
763 if ((ret = sqlite3_open(p, &lsqlite3->sqlite3)) != SQLITE_SUCCESS) {
769 * Create the database schema
771 for (pTail = schema; pTail != NULL; ) {
773 if ((lsqlite3->last_rc = sqlite3_prepare(
778 &pTail)) != SQLITE_SUCCESS ||
779 (lsqlite3->last_rc = sqlite3_step(stmt)) != SQLITE_DONE ||
780 (lsqlite3->last_rc = sqlite_finalize(stmt)) != SQLITE_SUCCESS) {
782 (void) sqlite3_close(lsqlite3->sqlite3);
788 * Ensure that the database we opened is one of ours
790 if ((lsqlite3->last_rc = sqlite3_prepare(
793 " FROM sqlite_master "
794 " WHERE type = 'table' "
798 " 'ldb_descendants', "
799 " 'ldb_object_classes' "
803 &pTail)) != SQLITE_SUCCESS ||
804 (lsqlite3->last_rc = sqlite3_step(stmt)) != SQLITE_ROW ||
805 sqlite3_column_int(stmt, 0) != 3 ||
806 (lsqlite3->last_rc = sqlite_finalize(stmt)) != SQLITE_SUCCESS ||
808 (lsqlite3->last_rc = sqlite3_prepare(
812 " WHERE database_type = 'LDB' "
813 " AND version = '1.0';",
816 &pTail)) != SQLITE_SUCCESS ||
817 (lsqlite3->last_rc = sqlite3_step(stmt)) != SQLITE_ROW ||
818 (lsqlite3->last_rc = sqlite_finalize(stmt)) != SQLITE_SUCCESS) {
820 /* It's not one that we created. See ya! */
821 (void) sqlite3_close(lsqlite3->sqlite3);
822 return SQLITE_MISUSE;
826 return SQLITE_SUCCESS;
830 * connect to the database
833 lsqlite3_connect(const char *url,
835 const char *options[])
838 struct ldb_context * ldb = NULL;
839 struct lsqlite3_private * lsqlite3 = NULL;
841 ldb = talloc(NULL, struct ldb_context);
847 lsqlite3 = talloc(ldb, struct lsqlite3_private);
853 lsqlite3->sqlite3 = NULL;
854 lsqlite3->options = NULL;
855 lsqlite3->lock_count = 0;
857 lsqlite3->last_rc = lsqlite3_initialize(&lsqlite3->sqlite3, url);
858 if (lsqlite3->last_rc != SQLITE_SUCCESS) {
862 talloc_set_destructor(lsqlite3, lsqlite3_destructor);
864 ldb->modules = talloc(ldb, struct ldb_module);
869 ldb->modules->ldb = ldb;
870 ldb->modules->prev = ldb->modules->next = NULL;
871 ldb->modules->private_data = lsqlite3;
872 ldb->modules->ops = &lsqlite3_ops;
876 * take a copy of the options array, so we don't have to rely
877 * on the caller keeping it around (it might be dynamic)
879 for (i=0;options[i];i++) ;
881 lsqlite3->options = talloc_array(lsqlite3, char *, i+1);
882 if (!lsqlite3->options) {
886 for (i=0;options[i];i++) {
888 lsqlite3->options[i+1] = NULL;
889 lsqlite3->options[i] =
890 talloc_strdup(lsqlite3->options, options[i]);
891 if (!lsqlite3->options[i]) {
900 if (lsqlite3->sqlite3 != NULL) {
901 (void) sqlite3_close(lsqlite3->sqlite3);