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 sqlite 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 we don't need this right now, but will once we add some backend
47 find an option in an option list (a null terminated list of strings)
49 this assumes the list is short. If it ever gets long then we really
50 should do this in some smarter way
52 static const char *lsqlite3_option_find(const struct lsqlite3_private *lsqlite3, const char *name)
55 size_t len = strlen(name);
57 if (!lsqlite3->options) return NULL;
59 for (i=0;lsqlite3->options[i];i++) {
60 if (strncmp(lsqlite3->options[i], name, len) == 0 &&
61 lsqlite3->options[i][len] == '=') {
62 return &lsqlite3->options[i][len+1];
73 static int lsqlite3_rename(struct ldb_module *module, const char *olddn, const char *newdn)
76 struct lsqlite3_private *lsqlite3 = module->private_data;
78 /* ignore ltdb specials */
79 if (olddn[0] == '@' ||newdn[0] == '@') {
83 /* Bind old distinguished names */
84 column = sqlite3_bind_parameter_index(lsqlite3->renameDN, ":oldDN");
85 if (sqlite3_bind_text(lsqlite3->renameDN, column,
87 SQLITE_STATIC) != SQLITE_OK) {
91 /* Bind new distinguished names */
92 column = sqlite3_bind_parameter_index(lsqlite3->renameDN, ":newDN");
93 if (sqlite3_bind_text(lsqlite3->renameDN, column,
95 SQLITE_STATIC) != SQLITE_OK) {
100 lsqlite3->last_rc = sqlite3_step(lsqlite3->renameDN);
101 (void) sqlite3_reset(lsqlite3->renameDN);
102 } while lsqlite3->last_rc == SQLITE3_BUSY;
104 return lsqlite3->last_rc == 0 ? 0 : -1;
110 static int lsqlite3_delete(struct ldb_module *module, const char *dn)
114 struct lsqlite3_private *lsqlite3 = module->private_data;
116 /* ignore ltdb specials */
121 /* Bind new distinguished names */
122 column = sqlite3_bind_parameter_index(lsqlite3->renameDN, ":dn");
123 if (sqlite3_bind_text(lsqlite3->deleteDN, column,
125 SQLITE_STATIC) != SQLITE_OK) {
130 lsqlite3->last_rc = sqlite3_step(lsqlite3->deleteDN);
131 (void) sqlite3_reset(lsqlite3->deleteDN);
132 } while lsqlite3->last_rc == SQLITE3_BUSY;
134 return lsqlite3->last_rc == 0 ? 0 : -1;
140 static int lsqlite3_search_free(struct ldb_module *module, struct ldb_message **res)
148 add a single set of ldap message values to a ldb_message
150 static int lsqlite3_add_msg_attr(struct ldb_context *ldb,
151 struct ldb_message *msg,
152 const char *attr, struct berval **bval)
155 struct ldb_message_element *el;
157 count = ldap_count_values_len(bval);
163 el = talloc_realloc(msg, msg->elements, struct ldb_message_element,
164 msg->num_elements + 1);
172 el = &msg->elements[msg->num_elements];
174 el->name = talloc_strdup(msg->elements, attr);
182 el->values = talloc_array(msg->elements, struct ldb_val, count);
188 for (i=0;i<count;i++) {
189 el->values[i].data = talloc_memdup(el->values, bval[i]->bv_val, bval[i]->bv_len);
190 if (!el->values[i].data) {
193 el->values[i].length = bval[i]->bv_len;
203 search for matching records
205 static int lsqlite3_search(struct ldb_module *module, const char *base,
206 enum ldb_scope scope, const char *expression,
207 const char * const *attrs, struct ldb_message ***res)
209 struct ldb_context *ldb = module->ldb;
210 struct lsqlite3_private *lsqlite3 = module->private_data;
211 int count, msg_count;
217 lsqlite3->last_rc = ldap_search_s(lsqlite3->ldap, base, (int)scope,
219 discard_const_p(char *, attrs),
221 if (lsqlite3->last_rc != LDAP_SUCCESS) {
225 count = ldap_count_entries(lsqlite3->ldap, ldapres);
226 if (count == -1 || count == 0) {
227 ldap_msgfree(ldapres);
231 (*res) = talloc_array(lsqlite3, struct ldb_message *, count+1);
233 ldap_msgfree(ldapres);
242 /* loop over all messages */
243 for (msg=ldap_first_entry(lsqlite3->ldap, ldapres);
245 msg=ldap_next_entry(lsqlite3->ldap, msg)) {
246 BerElement *berptr = NULL;
249 if (msg_count == count) {
250 /* hmm, got too many? */
251 ldb_debug(ldb, LDB_DEBUG_FATAL, "Fatal: ldap message count inconsistent\n");
255 (*res)[msg_count] = talloc(*res, struct ldb_message);
256 if (!(*res)[msg_count]) {
259 (*res)[msg_count+1] = NULL;
261 dn = ldap_get_dn(lsqlite3->ldap, msg);
266 (*res)[msg_count]->dn = talloc_strdup((*res)[msg_count], dn);
268 if (!(*res)[msg_count]->dn) {
273 (*res)[msg_count]->num_elements = 0;
274 (*res)[msg_count]->elements = NULL;
275 (*res)[msg_count]->private_data = NULL;
277 /* loop over all attributes */
278 for (attr=ldap_first_attribute(lsqlite3->ldap, msg, &berptr);
280 attr=ldap_next_attribute(lsqlite3->ldap, msg, berptr)) {
281 struct berval **bval;
282 bval = ldap_get_values_len(lsqlite3->ldap, msg, attr);
285 lsqlite3_add_msg_attr(ldb, (*res)[msg_count], attr, bval);
286 ldap_value_free_len(bval);
291 if (berptr) ber_free(berptr, 0);
296 ldap_msgfree(ldapres);
301 if (*res) lsqlite3_search_free(module, *res);
307 Issue a series of SQL statements to implement the requests in the ldb_message
309 static int lsqlite3_msg_to_sql(struct ldb_context *ldb,
310 const struct ldb_message *msg,
314 struct ldb_context *ldb = module->ldb;
315 struct lsqlite3_private *lsqlite3 = module->private_data;
316 sqlite3_stmt *stmt = NULL;
318 for (i=0;i<msg->num_elements;i++) {
319 const struct ldb_message_element *el = &msg->elements[i];
321 if (! modify_existing) {
322 /* This is a new DN. Bind new distinguished name */
324 sqlite3_bind_parameter_index(
325 lsqlite3->queries.newDN,
327 if (sqlite3_bind_text(lsqlite3->queries.newDN, column,
328 msg->dn, strlen(msg->dn),
329 SQLITE_STATIC) != SQLITE_OK) {
333 /* Add this new DN */
336 sqlite3_step(lsqlite3->queries.newDN);
337 (void) sqlite3_reset(lsqlite3->queries.newDN);
338 } while lsqlite3->last_rc == SQLITE_BUSY;
340 if (lsqlite3->last_rc != SQLITE_DONE) {
344 dn_id = last_insert_rowid(lsqlite3->sqlite3);
346 stmt = lsqlite3->queries.newAttribute;
349 /* Get the dn_id for the specified DN */
352 switch (el->flags & LDB_FLAG_MOD_MASK) {
353 case LDB_FLAG_MOD_ADD:
354 stmt = lsqlite3->queries.addAttrValuePair;
356 case LDB_FLAG_MOD_DELETE:
357 stmt = lsqlite3->queries.deleteAttrValuePairs;
359 case LDB_FLAG_MOD_REPLACE:
360 stmt = lsqlite3->queries.replaceAttrValuePairs;
366 for (j=0;j<el->num_values;j++) {
367 mods[num_mods]->mod_vals.modv_bvals[j] = talloc(mods[num_mods]->mod_vals.modv_bvals,
369 if (!mods[num_mods]->mod_vals.modv_bvals[j]) {
372 mods[num_mods]->mod_vals.modv_bvals[j]->bv_val = el->values[j].data;
373 mods[num_mods]->mod_vals.modv_bvals[j]->bv_len = el->values[j].length;
375 mods[num_mods]->mod_vals.modv_bvals[j] = NULL;
390 static int lsqlite3_add(struct ldb_module *module, const struct ldb_message *msg)
392 struct ldb_context *ldb = module->ldb;
393 struct lsqlite3_private *lsqlite3 = module->private_data;
397 /* ignore ltdb specials */
398 if (msg->dn[0] == '@') {
402 mods = lsqlite3_msg_to_mods(ldb, msg, 0);
404 lsqlite3->last_rc = ldap_add_s(lsqlite3->ldap, msg->dn, mods);
405 if (lsqlite3->last_rc != LDAP_SUCCESS) {
418 static int lsqlite3_modify(struct ldb_module *module, const struct ldb_message *msg)
420 struct ldb_context *ldb = module->ldb;
421 struct lsqlite3_private *lsqlite3 = module->private_data;
425 /* ignore ltdb specials */
426 if (msg->dn[0] == '@') {
430 mods = lsqlite3_msg_to_mods(ldb, msg, 1);
432 lsqlite3->last_rc = ldap_modify_s(lsqlite3->ldap, msg->dn, mods);
433 if (lsqlite3->last_rc != LDAP_SUCCESS) {
442 static int lsqlite3_lock(struct ldb_module *module, const char *lockname)
445 struct ldb_context *ldb = module->ldb;
446 struct lsqlite3_private *lsqlite3 = module->private_data;
448 if (lockname == NULL) {
452 /* If we're already locked, just update lock count */
453 if (++lsqlite3->lock_count > 1) {
457 /* Write-lock (but not read-lock) the database */
458 lsqlite3->last_rc = sqlite3_step(lsqlite3->begin);
460 /* Ready the compiled statememt for its next use */
461 (void ) sqlite_reset(lsqlite3->begin);
463 return lsqlite3->last_rc == 0 ? 0 : -1;
466 static int lsqlite3_unlock(struct ldb_module *module, const char *lockname)
469 struct ldb_context *ldb = module->ldb;
470 struct lsqlite3_private *lsqlite3 = module->private_data;
472 if (lockname == NULL) {
476 /* If we're not already locked, there's nothing to do */
477 if (lsqlite3->lock_count == 0) {
481 /* Decrement lock count */
482 if (--lsqlite3->lock_count == 0) {
484 /* Final unlock. Unlock the database */
485 lsqlite3->last_rc = sqlite3_step(lsqlite3->commit);
487 /* Ready the compiled statememt for its next use */
488 (void ) sqlite_reset(lsqlite3->commit);
491 return lsqlite3->last_rc == 0 ? 0 : -1;
495 return extended error information
497 static const char *lsqlite3_errstring(struct ldb_module *module)
499 struct lsqlite3_private *lsqlite3 = module->private_data;
500 return sqlite3_errmsg(lsqlite3->sqlite3);
504 static const struct ldb_module_ops lsqlite3_ops = {
507 lsqlite3_search_free,
518 static int lsqlite3_destructor(void *p)
520 struct lsqlite3_private *lsqlite3 = p;
521 (void) sqlite3_close(lsqlite3->sqlite3);
525 static int lsqlite3_initialize(lsqlite3_private *lsqlite3,
528 int bNewDatabase = False;
535 -- ------------------------------------------------------
537 PRAGMA auto_vacuum=1;
539 -- ------------------------------------------------------
543 -- ------------------------------------------------------
545 CREATE TABLE ldb_info AS
546 SELECT 'LDB' AS database_type,
549 CREATE TABLE ldb_distinguished_names
551 dn_id INTEGER PRIMARY KEY AUTOINCREMENT,
555 CREATE TABLE ldb_object_classes
557 class_name TEXT PRIMARY KEY,
559 max_child_num INTEGER
562 CREATE TABLE ldb_dn_object_classes
564 dn_id INTEGER REFERENCES ldb_distinguished_names,
565 class_name TEXT REFERENCES ldb_object_classes
568 CREATE TABLE ldb_attributes
570 attr_name TEXT PRIMARY KEY,
571 case_insensitive_p BOOLEAN DEFAULT FALSE,
572 wildcard_p BOOLEAN DEFAULT FALSE,
573 hidden_p BOOLEAN DEFAULT FALSE,
574 integer_p BOOLEAN DEFAULT FALSE
577 CREATE TABLE ldb_attr_value_pairs
579 dn_id INTEGER REFERENCES ldb_distinguished_names,
580 attr_name TEXT REFERENCES ldb_attributes,
584 -- ------------------------------------------------------
586 CREATE TRIGGER ldb_distinguished_names_delete_tr
588 ON ldb_distinguished_names
591 DELETE FROM ldb_attr_value_pairs
592 WHERE dn_id = old.dn_id;
593 DELETE FROM ldb_dn_object_classes
594 WHERE dn_id = old.dn_id;
597 CREATE TRIGGER ldb_attr_value_pairs_insert_tr
599 ON ldb_attr_value_pairs
602 INSERT OR IGNORE INTO ldb_attributes
608 CREATE TRIGGER ldb_attr_value_pairs_delete_tr
610 ON ldb_attr_value_pairs
613 DELETE FROM ldb_attributes
614 WHERE (SELECT COUNT(*)
615 FROM ldb_attr_value_pairs
616 WHERE attr_name = old.attr_name) = 0
617 AND attr_name = old.attr_name;
620 -- ------------------------------------------------------
622 CREATE INDEX ldb_distinguished_names_dn_idx
623 ON ldb_distinguished_names (dn);
625 CREATE INDEX ldb_object_classes_tree_key_idx
626 ON ldb_object_classes (tree_key);
629 CREATE INDEX ldb_dn_object_classes_dn_id_idx
630 ON ldb_dn_object_classes (dn_id);
632 CREATE INDEX ldb_dn_object_classes_class_name_idx
633 ON ldb_dn_object_classes (class_name);
636 CREATE INDEX ldb_attr_value_pairs_dn_id_name_case_idx
637 ON ldb_attr_value_pairs (dn_id, attr_name);
639 CREATE INDEX ldb_attr_value_pairs_dn_id_name_nocase_idx
640 ON ldb_attr_value_pairs (dn_id, attr_name COLLATE NOCASE);
642 -- ------------------------------------------------------
644 /* all defaults for dn, initially */
645 INSERT INTO ldb_attributes (attr_name)
648 /* We need an implicit "top" level object class */
649 INSERT INTO ldb_object_classes (class_name, tree_key)
650 SELECT 'top', /* next_tree_key(NULL) */ '0001';
652 -- ------------------------------------------------------
656 -- ------------------------------------------------------
659 /* Skip protocol indicator of url */
660 if ((p = strchr(url, ':')) == NULL) {
661 return SQLITE_MISUSE;
667 * See if we'll be creating a new database, or opening an existing one
669 if ((stat(p, &statbuf) < 0 && errno == ENOENT) ||
670 statbuf.st_size == 0) {
675 /* Try to open the (possibly empty/non-existent) database */
676 if ((lsqlite3->last_rc = sqlite3_open(p, &lsqlite3->sqlite3)) != SQLITE_SUCCESS) {
682 * Create the database schema
684 for (pTail = schema; pTail != NULL; ) {
686 if ((lsqlite3->last_rc = sqlite3_prepare(
691 &pTail)) != SQLITE_SUCCESS ||
692 (lsqlite3->last_rc = sqlite3_step(stmt)) != SQLITE_DONE ||
693 (lsqlite3->last_rc = sqlite_finalize(stmt)) != SQLITE_SUCCESS) {
695 (void) sqlite3_close(lsqlite3->sqlite3);
701 * Ensure that the database we opened is one of ours
703 if ((lsqlite3->last_rc = sqlite3_prepare(
706 " FROM sqlite_master "
707 " WHERE type = 'table' "
711 " 'ldb_distinguished_names', "
712 " 'ldb_object_classes', "
713 " 'ldb_dn_object_classes', "
714 " 'ldb_attributes', "
715 " 'ldb_attr_value_pairs' "
719 &pTail)) != SQLITE_SUCCESS ||
720 (lsqlite3->last_rc = sqlite3_step(stmt)) != SQLITE_ROW ||
721 sqlite3_column_int(stmt, 0) != 6 ||
722 (lsqlite3->last_rc = sqlite_finalize(stmt)) != SQLITE_SUCCESS ||
724 (lsqlite3->last_rc = sqlite3_prepare(
728 " WHERE database_type = 'LDB' "
729 " AND version = '1.0';",
732 &pTail)) != SQLITE_SUCCESS ||
733 (lsqlite3->last_rc = sqlite3_step(stmt)) != SQLITE_ROW ||
734 (lsqlite3->last_rc = sqlite_finalize(stmt)) != SQLITE_SUCCESS) {
736 /* It's not one that we created. See ya! */
737 (void) sqlite3_close(lsqlite3->sqlite3);
738 return SQLITE_MISUSE;
743 * Pre-compile each of the queries we'll be using.
746 if ((lsqlite3->last_rc = sqlite3_prepare(
750 &lsqlite3->queries.begin,
751 &pTail)) != SQLITE_SUCCESS ||
753 (lsqlite3->last_rc = sqlite3_prepare(
757 &lsqlite3->queries.commit,
758 &pTail)) != SQLITE_SUCCESS ||
760 (lsqlite3->last_rc = sqlite3_prepare(
764 &lsqlite3->queries.rollback,
765 &pTail)) != SQLITE_SUCCESS ||
767 (lsqlite3->last_rc = sqlite3_prepare(
769 "INSERT INTO ldb_distinguished_names (dn_id, dn) "
770 " VALUES (:dn_id, :dn);",
772 &lsqlite3->queries.newDN,
773 &pTail)) != SQLITE_SUCCESS ||
775 (lsqlite3->last_rc = sqlite3_prepare(
777 "UPDATE ldb_distinguished_names "
779 " WHERE dn = :oldDN;",
781 &lsqlite3->queries.renameDN,
782 &pTail)) != SQLITE_SUCCESS ||
784 (lsqlite3->last_rc = sqlite3_prepare(
786 "DELETE FROM ldb_distinguished_names "
789 &lsqlite3->queries.deleteDN,
790 &pTail)) != SQLITE_SUCCESS ||
792 (lsqlite3->last_rc = sqlite3_prepare(
794 "INSERT OR IGNORE INTO ldb_object_classes "
795 " (class_name, tree_key)"
796 " SELECT :class_name, next_tree_key(NULL);",
798 &lsqlite3->queries.newObjectClass,
799 &pTail)) != SQLITE_SUCCESS ||
801 (lsqlite3->last_rc = sqlite3_prepare(
803 "INSERT OR REPLACE INTO ldb_dn_object_classes "
804 " (dn_id, class_name) "
805 " VALUES (:dn_id, :class_name);",
807 &lsqlite3->queries.assignObjectClass,
808 &pTail)) != SQLITE_SUCCESS ||
810 (lsqlite3->last_rc = sqlite3_prepare(
812 "INSERT OR IGNORE INTO ldb_attributes (name) "
815 &lsqlite3->queries.newAttributeUseDefaults,
816 &pTail)) != SQLITE_SUCCESS ||
818 (lsqlite3->last_rc = sqlite3_prepare(
820 "INSERT OR REPLACE INTO ldb_attributes "
822 " case_insensitive_p, "
827 " :case_insensitive_p, "
832 &lsqlite3->queries.newAttribute,
833 &pTail)) != SQLITE_SUCCESS ||
835 (lsqlite3->last_rc = sqlite3_prepare(
837 "INSERT INTO ldb_attr_value_pairs "
838 " (dn_id, attr_name, attr_value) "
839 " VALUES (:dn_id, :attr_name, :attr_value);",
841 &lsqlite3->queries.addAttrValuePair,
842 &pTail)) != SQLITE_SUCCESS ||
844 (lsqlite3->last_rc = sqlite3_prepare(
846 "UPDATE ldb_attr_value_pairs "
847 " SET attr_value = :attr_value "
848 " WHERE dn_id = :dn_id "
849 " AND attr_name = :attr_name;",
851 &lsqlite3->queries.addAttrValuePair,
852 &pTail)) != SQLITE_SUCCESS ||
854 (lsqlite3->last_rc = sqlite3_prepare(
856 "DELETE FROM ldb_attr_value_pairs "
857 " WHERE dn_id = :dn_id "
858 " AND attr_name = :attr_name;"
860 &lsqlite3->queries.deleteAttrValuePair,
861 &pTail)) != SQLITE_SUCCESS ||
863 (lsqlite3->last_rc = sqlite3_prepare(
865 "INSERT OR REPLACE INTO ldb_object_classes "
866 " (class_name, tree_key) "
867 " SELECT :child_class, next_tree_key(:parent_class);"
869 &lsqlite3->queries.insertSubclass,
870 &pTail)) != SQLITE_SUCCESS) {
872 (void) sqlite3_close(lsqlite3->sqlite3);
876 return SQLITE_SUCCESS;
880 connect to the database
882 struct ldb_context *lsqlite3_connect(const char *url,
884 const char *options[])
886 struct ldb_context *ldb = NULL;
887 struct lsqlite3_private *lsqlite3 = NULL;
890 ldb = talloc(NULL, struct ldb_context);
896 lsqlite3 = talloc(ldb, struct lsqlite3_private);
902 lsqlite3->sqlite3 = NULL;
903 lsqlite3->options = NULL;
904 lsqlite3->lock_count = 0;
906 lsqlite3->last_rc = lsqlite3_initialize(&lsqlite3->sqlite3, url);
907 if (lsqlite3->last_rc != LDAP_SUCCESS) {
911 talloc_set_destructor(lsqlite3, lsqlite3_destructor);
913 ldb->modules = talloc(ldb, struct ldb_module);
918 ldb->modules->ldb = ldb;
919 ldb->modules->prev = ldb->modules->next = NULL;
920 ldb->modules->private_data = lsqlite3;
921 ldb->modules->ops = &lsqlite3_ops;
924 /* take a copy of the options array, so we don't have to rely
925 on the caller keeping it around (it might be dynamic) */
926 for (i=0;options[i];i++) ;
928 lsqlite3->options = talloc_array(lsqlite3, char *, i+1);
929 if (!lsqlite3->options) {
933 for (i=0;options[i];i++) {
934 lsqlite3->options[i+1] = NULL;
935 lsqlite3->options[i] = talloc_strdup(lsqlite3->options, options[i]);
936 if (!lsqlite3->options[i]) {
945 if (lsqlite3->sqlite3 != NULL) {
946 (void) sqlite3_close(lsqlite3->sqlite3);