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/include/ldb_explode_dn.h"
40 #include "ldb/ldb_sqlite3/ldb_sqlite3.h"
43 * Macros used throughout
48 # define TRUE (! FALSE)
51 #define FILTER_ATTR_TABLE "temp_filter_attrs"
52 #define RESULT_ATTR_TABLE "temp_result_attrs"
54 //#define TEMPTAB /* for testing, create non-temporary table */
55 #define TEMPTAB "TEMPORARY"
60 # define LOCK_DB(mod, name) lsqlite3_lock(mod, name)
61 # define UNLOCK_DB(mod, name) lsqlite3_unlock(mod, name)
63 # define LOCK_DB(mod, name) lock_debug(mod, name, __FILE__, __LINE__)
64 # define UNLOCK_DB(mod, name) unlock_debug(mod, name, __FILE__, __LINE__)
67 #define QUERY_NOROWS(lsqlite3, bRollbackOnError, sql...) \
69 if (query_norows(lsqlite3, sql) != 0) { \
70 if (bRollbackOnError) { \
71 UNLOCK_DB(module, "rollback"); \
77 #define QUERY_INT(lsqlite3, result_var, bRollbackOnError, sql...) \
79 if (query_int(lsqlite3, &result_var, sql) != 0) { \
80 if (bRollbackOnError) { \
81 UNLOCK_DB(module, "rollback"); \
87 #define GET_EID(lsqlite3, result_var, bRollbackOnError, pDN) \
89 if (getEID(lsqlite3, &result_var, pDN) != 0) { \
90 if (bRollbackOnError) { \
91 UNLOCK_DB(module, "rollback"); \
98 #define SQLITE3_DEBUG_QUERY (1 << 0)
99 #define SQLITE3_DEBUG_INIT (1 << 1)
100 #define SQLITE3_DEBUG_ADD (1 << 2)
101 #define SQLITE3_DEBUG_NEWDN (1 << 3)
102 #define SQLITE3_DEBUG_SEARCH (1 << 4)
107 static int lsqlite3_debug = FALSE;
108 sqlite3_stmt * stmtGetEID = NULL;
111 * Forward declarations
114 lsqlite3_rename(struct ldb_module * module,
119 lsqlite3_delete(struct ldb_module *module,
123 lsqlite3_search_bytree(struct ldb_module * module,
124 const char * pBaseDN,
125 enum ldb_scope scope,
126 struct ldb_parse_tree * pTree,
127 const char * const * attrs,
128 struct ldb_message *** pppRes);
131 lsqlite3_search(struct ldb_module * module,
132 const char * pBaseDN,
133 enum ldb_scope scope,
134 const char * pExpression,
135 const char * const attrs[],
136 struct ldb_message *** pppRes);
139 lsqlite3_add(struct ldb_module *module,
140 const struct ldb_message *msg);
143 lsqlite3_modify(struct ldb_module *module,
144 const struct ldb_message *msg);
147 lsqlite3_lock(struct ldb_module *module,
148 const char *lockname);
151 lsqlite3_unlock(struct ldb_module *module,
152 const char *lockname);
155 lsqlite3_errstring(struct ldb_module *module);
158 initialize(struct lsqlite3_private *lsqlite3,
165 query_norows(const struct lsqlite3_private *lsqlite3,
170 query_int(const struct lsqlite3_private * lsqlite3,
176 getEID(const struct lsqlite3_private * lsqlite3,
178 const char * pNormalizedDN);
180 static int case_fold_attr_required(void * hUserData,
183 static int case_fold_attr_not_required(void * hUserData,
187 add_msg_attr(void * hTalloc,
190 const char * pAttrName,
191 const char * pAttrValue,
194 struct ldb_message *** pppRes);
197 parsetree_to_sql(struct ldb_module *module,
199 const struct ldb_parse_tree *t);
202 parsetree_to_attrlist(struct ldb_module *module,
203 const struct ldb_parse_tree * t);
206 msg_to_sql(struct ldb_module * module,
207 const struct ldb_message * msg,
212 new_dn(struct ldb_module * module,
217 base160_sql(sqlite3_context * hContext,
219 sqlite3_value ** argv);
222 base160next_sql(sqlite3_context * hContext,
224 sqlite3_value ** argv);
227 static int lock_debug(struct ldb_module * module,
228 const char * lockname,
229 const char * pFileName,
232 static int unlock_debug(struct ldb_module * module,
233 const char * lockname,
234 const char * pFileName,
240 * Table of operations for the sqlite3 backend
242 static const struct ldb_module_ops lsqlite3_ops = {
244 .search = lsqlite3_search,
245 .search_bytree = lsqlite3_search_bytree,
246 .add_record = lsqlite3_add,
247 .modify_record = lsqlite3_modify,
248 .delete_record = lsqlite3_delete,
249 .rename_record = lsqlite3_rename,
250 .named_lock = lsqlite3_lock,
251 .named_unlock = lsqlite3_unlock,
252 .errstring = lsqlite3_errstring
264 * connect to the database
266 int lsqlite3_connect(struct ldb_context *ldb,
269 const char *options[])
273 struct lsqlite3_private * lsqlite3 = NULL;
275 lsqlite3 = talloc(ldb, struct lsqlite3_private);
280 lsqlite3->sqlite = NULL;
281 lsqlite3->options = NULL;
282 lsqlite3->lock_count = 0;
284 ret = initialize(lsqlite3, url);
285 if (ret != SQLITE_OK) {
289 talloc_set_destructor(lsqlite3, destructor);
291 ldb->modules = talloc(ldb, struct ldb_module);
295 ldb->modules->ldb = ldb;
296 ldb->modules->prev = ldb->modules->next = NULL;
297 ldb->modules->private_data = lsqlite3;
298 ldb->modules->ops = &lsqlite3_ops;
302 * take a copy of the options array, so we don't have to rely
303 * on the caller keeping it around (it might be dynamic)
305 for (i=0;options[i];i++) ;
307 lsqlite3->options = talloc_array(lsqlite3, char *, i+1);
308 if (!lsqlite3->options) {
312 for (i=0;options[i];i++) {
314 lsqlite3->options[i+1] = NULL;
315 lsqlite3->options[i] =
316 talloc_strdup(lsqlite3->options, options[i]);
317 if (!lsqlite3->options[i]) {
326 if (lsqlite3->sqlite != NULL) {
327 (void) sqlite3_close(lsqlite3->sqlite);
329 talloc_free(lsqlite3);
335 * Interface functions referenced by lsqlite3_ops
338 /* rename a record */
340 lsqlite3_rename(struct ldb_module * module,
344 const char *pOldNormalizedDN;
345 const char *pNewNormalizedDN;
347 struct lsqlite3_private * lsqlite3 = module->private_data;
349 /* ignore ltdb specials */
350 if (*pOldDN == '@' || *pNewDN == '@') {
354 /* Case-fold each of the DNs */
356 ldb_dn_fold(module->ldb, pOldDN, module, case_fold_attr_required);
358 ldb_dn_fold(module->ldb, pNewDN, module, case_fold_attr_required);
360 /* Begin a transaction */
361 if (LOCK_DB(module, "transaction") < 0) {
365 /* Determine the eid of the DN being renamed */
366 GET_EID(lsqlite3, eid, TRUE, pOldNormalizedDN);
368 QUERY_NOROWS(lsqlite3,
372 " normalized_dn = %Q "
373 " WHERE eid = %lld;",
374 pNewDN, pNewNormalizedDN, eid);
376 QUERY_NOROWS(lsqlite3,
378 "UPDATE ldb_attribute_values "
379 " SET attr_value = %Q, "
380 " attr_value_normalized = %Q "
382 " AND attr_name = 'DN';",
387 /* Commit the transaction */
388 if (UNLOCK_DB(module, "transaction") < 0) {
389 UNLOCK_DB(module, "rollback");
396 /* delete a record */
398 lsqlite3_delete(struct ldb_module * module,
403 struct lsqlite3_private * lsqlite3 = module->private_data;
405 /* ignore ltdb specials */
410 /* Begin a transaction */
411 if (LOCK_DB(module, "transaction") < 0) {
415 /* Case-fold the DNs */
417 ldb_dn_fold(module->ldb, pDN, module, case_fold_attr_required);
419 /* Determine the eid of the DN being deleted */
420 GET_EID(lsqlite3, eid, TRUE, pNormalizedDN);
422 /* Delete attribute/value table entries pertaining to this DN */
423 QUERY_NOROWS(lsqlite3,
425 "DELETE FROM ldb_attribute_values "
426 " WHERE eid = %lld;",
429 /* Delete this entry */
430 QUERY_NOROWS(lsqlite3,
432 "DELETE FROM ldb_entry "
433 " WHERE eid = %lld;",
436 /* Commit the transaction */
437 if (UNLOCK_DB(module, "transaction") < 0) {
438 UNLOCK_DB(module, "rollback");
445 /* search for matching records, by tree */
447 lsqlite3_search_bytree(struct ldb_module * module,
448 const char * pBaseDN,
449 enum ldb_scope scope,
450 struct ldb_parse_tree * pTree,
451 const char * const * attrs,
452 struct ldb_message *** pppRes)
460 char * pSqlConstraints;
461 char * hTalloc = NULL;
463 const char * pNormalizedBaseDN;
464 const char * pAttrName;
465 const char * pAttrValue;
466 const char * pResultAttrList;
467 const char * const * pRequestedAttrs;
468 sqlite3_stmt * pStmt;
469 struct lsqlite3_private * lsqlite3 = module->private_data;
471 /* Allocate a temporary talloc context */
472 if ((hTalloc = talloc_new(module->ldb)) == NULL) {
476 /* Case-fold the base DN */
477 if ((pNormalizedBaseDN =
479 pBaseDN == NULL ? "" : pBaseDN,
481 case_fold_attr_required)) == NULL) {
483 talloc_free(hTalloc);
487 /* Begin a transaction */
488 if (LOCK_DB(module, "transaction") < 0) {
489 talloc_free(hTalloc);
494 * Obtain the eid of the base DN
496 if ((ret = getEID(lsqlite3, &eid, pNormalizedBaseDN)) == SQLITE_DONE) {
497 UNLOCK_DB(module, "rollback");
498 talloc_free(hTalloc);
500 } else if (ret != SQLITE_OK) {
501 UNLOCK_DB(module, "rollback");
502 talloc_free(hTalloc);
506 /* Convert filter into a series of SQL conditions (constraints) */
507 pSqlConstraints = parsetree_to_sql(module, hTalloc, pTree);
509 /* Ensure we're starting with an empty result attribute table */
510 QUERY_NOROWS(lsqlite3,
512 "DELETE FROM " RESULT_ATTR_TABLE "\n"
513 " WHERE 1;");/* avoid a schema change with WHERE 1 */
515 /* Initially, we don't know what the requested attributes are */
516 pResultAttrList = NULL;
518 /* Insert the list of requested attributes into this table */
519 for (pRequestedAttrs = (const char * const *) attrs;
520 pRequestedAttrs != NULL && *pRequestedAttrs != NULL;
523 /* If any attribute in the list is "*" then... */
524 if (strcmp(*pRequestedAttrs, "*") == 0) {
525 /* we want all attribute types */
526 pResultAttrList = "";
530 /* otherwise, add this name to the resuult list */
531 QUERY_NOROWS(lsqlite3,
534 " INTO " RESULT_ATTR_TABLE "\n"
542 /* If we didn't get a "*" for all attributes in the result list... */
543 if (pResultAttrList == NULL) {
544 /* ... then we'll use the result attribute table */
546 " AND upper(av.attr_name) IN\n"
547 " (SELECT attr_name\n"
548 " FROM " RESULT_ATTR_TABLE ") ";
551 /* Ensure we're starting with an empty filter attribute table */
552 QUERY_NOROWS(lsqlite3,
554 "DELETE FROM " FILTER_ATTR_TABLE "\n"
555 " WHERE 1;");/* avoid a schema change with WHERE 1 */
558 * Create a table of unique attribute names for our extra table list
560 if ((ret = parsetree_to_attrlist(module, pTree)) != 0) {
566 case LDB_SCOPE_DEFAULT:
567 case LDB_SCOPE_SUBTREE:
568 pSql = sqlite3_mprintf(
569 "SELECT entry.eid,\n"
573 " FROM ldb_entry AS entry\n"
575 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
576 " ON av.eid = entry.eid\n"
579 " WHERE entry.eid IN\n"
580 " (SELECT DISTINCT ldb_entry.eid\n"
582 " WHERE ldb_entry.tree_key >=\n"
583 " (SELECT tree_key\n"
585 " WHERE eid = %lld)\n"
586 " AND ldb_entry.tree_key <\n"
587 " (SELECT base160_next(tree_key)\n"
589 " WHERE eid = %lld)\n"
590 " AND ldb_entry.eid IN\n(%s)\n"
592 " ORDER BY entry.tree_key DESC,\n"
593 " COALESCE(av.attr_name, '');",
601 pSql = sqlite3_mprintf(
602 "SELECT entry.eid,\n"
606 " FROM ldb_entry AS entry\n"
608 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
609 " ON av.eid = entry.eid\n"
612 " WHERE entry.eid IN\n"
613 " (SELECT DISTINCT ldb_entry.eid\n"
615 " WHERE ldb_entry.eid = %lld\n"
616 " AND ldb_entry.eid IN\n(%s)\n"
618 " ORDER BY entry.tree_key DESC,\n"
619 " COALESCE(av.attr_name, '');",
625 case LDB_SCOPE_ONELEVEL:
626 pSql = sqlite3_mprintf(
627 "SELECT entry.eid,\n"
631 " FROM ldb_entry AS entry\n"
633 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
634 " ON av.eid = entry.eid\n"
637 " WHERE entry.eid IN\n"
638 " (SELECT DISTINCT ldb_entry.eid\n"
640 " WHERE ldb_entry.tree_key >=\n"
641 " (SELECT tree_key\n"
643 " WHERE eid = %lld)\n"
644 " AND ldb_entry.tree_key <\n"
645 " (SELECT base160_next(tree_key)\n"
647 " WHERE eid = %lld)\n"
648 " AND length(ldb_entry.tree_key) =\n"
649 " (SELECT length(tree_key) + 4\n"
651 " WHERE eid = %lld)\n"
652 " AND ldb_entry.eid IN\n(%s)\n"
655 " ORDER BY entry.tree_key DESC,\n"
656 " COALESCE(av.attr_name, '');\n",
670 if (lsqlite3_debug & SQLITE3_DEBUG_SEARCH) {
671 printf("%s\n", pSql);
675 * Prepare and execute the SQL statement. Loop allows retrying on
676 * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
677 * requiring retrying the operation.
679 for (bLoop = TRUE; bLoop; ) {
680 /* There are no allocate message structures yet */
682 if (pppRes != NULL) {
686 /* Compile the SQL statement into sqlite virtual machine */
687 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
691 NULL)) == SQLITE_SCHEMA) {
692 if (stmtGetEID != NULL) {
693 sqlite3_finalize(stmtGetEID);
697 if (pppRes != NULL && *pppRes != NULL) {
698 talloc_free(*pppRes);
702 } else if (ret != SQLITE_OK) {
707 /* Initially, we have no previous eid */
710 /* Loop through the returned rows */
711 for (ret = SQLITE_ROW; ret == SQLITE_ROW; ) {
713 /* Get the next row */
714 if ((ret = sqlite3_step(pStmt)) == SQLITE_ROW) {
716 /* Get the values from this row */
717 eid = sqlite3_column_int64(pStmt, 0);
718 pDN = sqlite3_column_text(pStmt, 1);
719 pAttrName = sqlite3_column_text(pStmt, 2);
720 pAttrValue = sqlite3_column_text(pStmt, 3);
722 /* Add this result to the result set */
723 if (add_msg_attr(hTalloc,
732 (void) sqlite3_finalize(pStmt);
737 /* Save the most recent EID */
742 if (ret == SQLITE_SCHEMA) {
743 if (stmtGetEID != NULL) {
744 sqlite3_finalize(stmtGetEID);
748 (void) sqlite3_finalize(pStmt);
749 if (pppRes != NULL && *pppRes != NULL) {
750 talloc_free(*pppRes);
753 } else if (ret != SQLITE_DONE) {
754 (void) sqlite3_finalize(pStmt);
759 /* Free the virtual machine */
760 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
761 if (stmtGetEID != NULL) {
762 sqlite3_finalize(stmtGetEID);
766 if (pppRes != NULL && *pppRes != NULL) {
767 talloc_free(*pppRes);
770 } else if (ret != SQLITE_OK) {
771 (void) sqlite3_finalize(pStmt);
777 * Normal condition is only one time through loop. Loop is
778 * rerun in error conditions, via "continue", above.
784 /* We're alll done with this query */
787 /* End the transaction */
788 UNLOCK_DB(module, "rollback");
790 /* Were there any results? */
791 if (ret != 0 || allocated == 0) {
792 /* Nope. We can free the results. */
793 if (pppRes != NULL && *pppRes != NULL) {
794 talloc_free(*pppRes);
799 /* Clean up our temporary tables */
800 QUERY_NOROWS(lsqlite3,
802 "DELETE FROM " RESULT_ATTR_TABLE "\n"
803 " WHERE 1;");/* avoid a schema change with WHERE 1 */
805 QUERY_NOROWS(lsqlite3,
807 "DELETE FROM " FILTER_ATTR_TABLE "\n"
808 " WHERE 1;");/* avoid a schema change with WHERE 1 */
811 if (hTalloc != NULL) {
812 talloc_free(hTalloc);
815 /* If error, return error code; otherwise return number of results */
816 return ret == 0 ? allocated : ret;
819 /* search for matching records, by expression */
821 lsqlite3_search(struct ldb_module * module,
822 const char * pBaseDN,
823 enum ldb_scope scope,
824 const char * pExpression,
825 const char * const * attrs,
826 struct ldb_message *** pppRes)
829 struct ldb_parse_tree * pTree;
831 /* Handle tdb specials */
832 if (pBaseDN != NULL && *pBaseDN == '@') {
833 #warning "handle tdb specials"
838 /* (|(objectclass=*)(dn=*)) is passed by the command line tool now instead */
839 /* Handle the special case of requesting all */
840 if (pExpression != NULL && *pExpression == '\0') {
841 pExpression = "dn=*";
845 /* Parse the filter expression into a tree we can work with */
846 if ((pTree = ldb_parse_tree(module->ldb, pExpression)) == NULL) {
850 /* Now use the bytree function for the remainder of processing */
851 ret = lsqlite3_search_bytree(module, pBaseDN, scope,
852 pTree, attrs, pppRes);
854 /* Free the parse tree */
864 lsqlite3_add(struct ldb_module *module,
865 const struct ldb_message *msg)
869 /* See if this is an ltdb special */
870 if (*msg->dn == '@') {
871 /* Yup. We handle a few of these and ignore others */
872 if (strcmp(msg->dn, "@SUBCLASSES") == 0) {
873 #warning "insert subclasses into object class tree"
876 if (strcmp(msg->dn, "@INDEXLIST") == 0) {
877 /* explicitly ignored */
881 /* Others are implicitly ignored */
885 /* Begin a transaction */
886 if (LOCK_DB(module, "transaction") < 0) {
891 * Build any portions of the directory tree that don't exist. If the
892 * final component already exists, it's an error.
894 if (new_dn(module, msg->dn, &eid) != 0) {
895 UNLOCK_DB(module, "rollback");
899 /* Add attributes to this new entry */
900 if (msg_to_sql(module, msg, eid, FALSE) != 0) {
901 UNLOCK_DB(module, "rollback");
905 /* Everything worked. Commit it! */
906 if (UNLOCK_DB(module, "transaction") < 0) {
907 UNLOCK_DB(module, "rollback");
914 /* modify a record */
916 lsqlite3_modify(struct ldb_module * module,
917 const struct ldb_message * msg)
919 char * pNormalizedDN;
921 struct lsqlite3_private * lsqlite3 = module->private_data;
923 /* ignore ltdb specials */
924 if (*msg->dn == '@') {
928 /* Begin a transaction */
929 if (LOCK_DB(module, "transaction") < 0) {
933 /* Case-fold the DN so we can compare it to what's in the database */
934 pNormalizedDN = ldb_dn_fold(module->ldb, msg->dn,
935 module, case_fold_attr_required);
937 /* Determine the eid of the DN being deleted */
938 GET_EID(lsqlite3, eid, TRUE, pNormalizedDN);
940 /* Apply the message attributes */
941 if (msg_to_sql(module, msg, eid, TRUE) != 0) {
942 UNLOCK_DB(module, "rollback");
947 /* Everything worked. Commit it! */
948 if (UNLOCK_DB(module, "transaction") < 0) {
949 UNLOCK_DB(module, "rollback");
955 /* obtain a named lock */
957 lsqlite3_lock(struct ldb_module * module,
958 const char * lockname)
960 struct lsqlite3_private * lsqlite3 = module->private_data;
962 if (lockname == NULL) {
966 if (strcmp(lockname, "transaction") == 0) {
967 if (lsqlite3->lock_count == 0) {
968 if (query_norows(lsqlite3, "BEGIN EXCLUSIVE;") != 0) {
972 ++lsqlite3->lock_count;
978 /* release a named lock */
980 lsqlite3_unlock(struct ldb_module *module,
981 const char *lockname)
983 struct lsqlite3_private * lsqlite3 = module->private_data;
985 if (lockname == NULL) {
989 if (strcmp(lockname, "transaction") == 0) {
990 if (lsqlite3->lock_count == 1) {
991 if (query_norows(lsqlite3, "COMMIT;") != 0) {
992 query_norows(lsqlite3, "ROLLBACK;");
994 } else if (lsqlite3->lock_count > 0) {
995 --lsqlite3->lock_count;
997 } else if (strcmp(lockname, "rollback") == 0) {
998 query_norows(lsqlite3, "ROLLBACK;");
1004 /* return extended error information */
1006 lsqlite3_errstring(struct ldb_module *module)
1008 struct lsqlite3_private * lsqlite3 = module->private_data;
1010 return sqlite3_errmsg(lsqlite3->sqlite);
1021 initialize(struct lsqlite3_private *lsqlite3,
1027 sqlite3_stmt * stmt;
1028 const char * schema =
1031 "CREATE TABLE ldb_info AS "
1032 " SELECT 'LDB' AS database_type,"
1033 " '1.0' AS version;"
1036 * The entry table holds the information about an entry.
1037 * This table is used to obtain the EID of the entry and to
1038 * support scope=one and scope=base. The parent and child
1039 * table is included in the entry table since all the other
1040 * attributes are dependent on EID.
1042 "CREATE TABLE ldb_entry "
1044 " eid INTEGER PRIMARY KEY,"
1045 " peid INTEGER REFERENCES ldb_entry,"
1046 " dn TEXT UNIQUE NOT NULL,"
1047 " normalized_dn TEXT UNIQUE NOT NULL,"
1048 " tree_key TEXT UNIQUE,"
1049 " max_child_num INTEGER DEFAULT 0,"
1050 " create_timestamp INTEGER,"
1051 " modify_timestamp INTEGER"
1055 "CREATE TABLE ldb_object_classes"
1057 " class_name TEXT PRIMARY KEY,"
1058 " parent_class_name TEXT,"
1059 " tree_key TEXT UNIQUE,"
1060 " max_child_num INTEGER DEFAULT 0"
1064 * We keep a full listing of attribute/value pairs here
1066 "CREATE TABLE ldb_attribute_values"
1068 " eid INTEGER REFERENCES ldb_entry,"
1071 " attr_value_normalized TEXT "
1078 "CREATE INDEX ldb_entry_tree_key_idx "
1079 " ON ldb_entry (tree_key);"
1081 "CREATE INDEX ldb_attribute_values_eid_idx "
1082 " ON ldb_attribute_values (eid);"
1084 "CREATE INDEX ldb_attribute_values_name_value_idx "
1085 " ON ldb_attribute_values (attr_name, attr_value_normalized);"
1093 "CREATE TRIGGER ldb_entry_insert_tr"
1100 " SET create_timestamp = strftime('%s', 'now'),"
1101 " modify_timestamp = strftime('%s', 'now')"
1103 " tree_key = COALESCE(tree_key, "
1105 " SELECT tree_key || "
1106 " (SELECT base160(max_child_num + 1)"
1108 " WHERE eid = new.peid)"
1110 " WHERE eid = new.peid "
1112 " UPDATE ldb_entry "
1113 " SET max_child_num = max_child_num + 1"
1114 " WHERE eid = new.peid;"
1117 "CREATE TRIGGER ldb_entry_update_tr"
1123 " SET modify_timestamp = strftime('%s', 'now')"
1124 " WHERE eid = old.eid;"
1127 "CREATE TRIGGER ldb_object_classes_insert_tr"
1129 " ON ldb_object_classes"
1132 " UPDATE ldb_object_classes"
1133 " SET tree_key = COALESCE(tree_key, "
1135 " SELECT tree_key || "
1136 " (SELECT base160(max_child_num + 1)"
1137 " FROM ldb_object_classes"
1138 " WHERE class_name = "
1139 " new.parent_class_name)"
1140 " FROM ldb_object_classes "
1141 " WHERE class_name = new.parent_class_name "
1143 " UPDATE ldb_object_classes "
1144 " SET max_child_num = max_child_num + 1"
1145 " WHERE class_name = new.parent_class_name;"
1149 * Table initialization
1153 "INSERT INTO ldb_entry "
1154 " (eid, peid, dn, normalized_dn, tree_key) "
1156 " (0, NULL, '', '', '0001');"
1158 /* And the root node "dn" attribute */
1159 "INSERT INTO ldb_attribute_values "
1160 " (eid, attr_name, attr_value, attr_value_normalized) "
1162 " (0, 'DN', '', '');"
1164 "INSERT INTO ldb_object_classes "
1165 " (class_name, tree_key) "
1171 /* Skip protocol indicator of url */
1172 if (strncmp(url, "sqlite://", 9) != 0) {
1173 return SQLITE_MISUSE;
1176 /* Update pointer to just after the protocol indicator */
1179 /* Try to open the (possibly empty/non-existent) database */
1180 if ((ret = sqlite3_open(url, &lsqlite3->sqlite)) != SQLITE_OK) {
1184 /* In case this is a new database, enable auto_vacuum */
1185 if (query_norows(lsqlite3, "PRAGMA auto_vacuum=1;") != 0) {
1189 /* Establish a busy timeout of 30 seconds */
1190 if ((ret = sqlite3_busy_timeout(lsqlite3->sqlite,
1191 30000)) != SQLITE_OK) {
1195 /* Create a function, callable from sql, to increment a tree_key */
1197 sqlite3_create_function(lsqlite3->sqlite,/* handle */
1198 "base160_next", /* function name */
1199 1, /* number of args */
1200 SQLITE_ANY, /* preferred text type */
1201 NULL, /* user data */
1202 base160next_sql, /* called func */
1203 NULL, /* step func */
1204 NULL /* final func */
1209 /* Create a function, callable from sql, to convert int to base160 */
1211 sqlite3_create_function(lsqlite3->sqlite,/* handle */
1212 "base160", /* function name */
1213 1, /* number of args */
1214 SQLITE_ANY, /* preferred text type */
1215 NULL, /* user data */
1216 base160_sql, /* called func */
1217 NULL, /* step func */
1218 NULL /* final func */
1223 /* Begin a transaction */
1224 if ((ret = query_norows(lsqlite3, "BEGIN EXCLUSIVE;")) != 0) {
1228 /* Determine if this is a new database. No tables means it is. */
1229 if (query_int(lsqlite3,
1232 " FROM sqlite_master\n"
1233 " WHERE type = 'table';") != 0) {
1234 query_norows(lsqlite3, "ROLLBACK;");
1238 if (queryInt == 0) {
1240 * Create the database schema
1242 for (pTail = discard_const_p(char, schema);
1243 pTail != NULL && *pTail != '\0';
1246 if (lsqlite3_debug & SQLITE3_DEBUG_INIT) {
1247 printf("Execute first query in:\n%s\n", pTail);
1250 if ((ret = sqlite3_prepare(
1255 &pTail)) != SQLITE_OK ||
1256 (ret = sqlite3_step(stmt)) != SQLITE_DONE ||
1257 (ret = sqlite3_finalize(stmt)) != SQLITE_OK) {
1259 if (lsqlite3_debug & SQLITE3_DEBUG_INIT) {
1261 sqlite3_errmsg(lsqlite3->sqlite));
1262 printf("pTail = [%s]\n", pTail);
1265 query_norows(lsqlite3, "ROLLBACK;");
1266 (void) sqlite3_close(lsqlite3->sqlite);
1272 * Ensure that the database we opened is one of ours
1274 if (query_int(lsqlite3,
1277 " (SELECT COUNT(*) = 2"
1278 " FROM sqlite_master "
1279 " WHERE type = 'table' "
1283 " 'ldb_object_classes' "
1289 " WHERE database_type = 'LDB' "
1290 " AND version = '1.0'"
1294 /* It's not one that we created. See ya! */
1295 query_norows(lsqlite3, "ROLLBACK;");
1296 (void) sqlite3_close(lsqlite3->sqlite);
1297 return SQLITE_MISUSE;
1302 * Create a temporary table to hold attributes requested in the result
1305 query_norows(lsqlite3, "DROP TABLE " RESULT_ATTR_TABLE ";\n");
1307 query_norows(lsqlite3,
1308 "CREATE " TEMPTAB " TABLE " RESULT_ATTR_TABLE "\n"
1310 " attr_name TEXT PRIMARY KEY\n"
1312 query_norows(lsqlite3, "ROLLBACK;");
1317 * Create a temporary table to hold the attributes used by filters
1320 query_norows(lsqlite3, "DROP TABLE " FILTER_ATTR_TABLE ";\n");
1322 query_norows(lsqlite3,
1323 "CREATE " TEMPTAB " TABLE " FILTER_ATTR_TABLE "\n"
1325 " attr_name TEXT PRIMARY KEY\n"
1327 query_norows(lsqlite3, "ROLLBACK;");
1331 /* Commit the transaction */
1332 if ((ret = query_norows(lsqlite3, "COMMIT;")) != 0) {
1333 query_norows(lsqlite3, "ROLLBACK;");
1343 struct lsqlite3_private * lsqlite3 = p;
1345 if (lsqlite3->sqlite) {
1346 sqlite3_close(lsqlite3->sqlite);
1355 * This function is used for queries that are not expected to return any rows,
1356 * e.g. BEGIN, COMMIT, ROLLBACK, CREATE TABLE, INSERT, UPDATE, DELETE, etc.
1357 * There are no provisions here for returning data from rows in a table, so do
1358 * not pass SELECT queries to this function.
1361 query_norows(const struct lsqlite3_private *lsqlite3,
1368 sqlite3_stmt * pStmt;
1375 if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1376 gettimeofday(&tv, &tz);
1377 t0 = (double) tv.tv_sec + ((double) tv.tv_usec / 1000000.0);
1380 /* Begin access to variable argument list */
1381 va_start(args, pSql);
1383 /* Format the query */
1384 if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
1389 * Prepare and execute the SQL statement. Loop allows retrying on
1390 * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
1391 * requiring retrying the operation.
1393 for (bLoop = TRUE; bLoop; ) {
1395 /* Compile the SQL statement into sqlite virtual machine */
1396 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
1400 NULL)) == SQLITE_SCHEMA) {
1401 if (stmtGetEID != NULL) {
1402 sqlite3_finalize(stmtGetEID);
1406 } else if (ret != SQLITE_OK) {
1411 /* No rows expected, so just step through machine code once */
1412 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
1413 if (stmtGetEID != NULL) {
1414 sqlite3_finalize(stmtGetEID);
1417 (void) sqlite3_finalize(pStmt);
1419 } else if (ret != SQLITE_DONE) {
1420 (void) sqlite3_finalize(pStmt);
1425 /* Free the virtual machine */
1426 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
1427 if (stmtGetEID != NULL) {
1428 sqlite3_finalize(stmtGetEID);
1432 } else if (ret != SQLITE_OK) {
1433 (void) sqlite3_finalize(pStmt);
1439 * Normal condition is only one time through loop. Loop is
1440 * rerun in error conditions, via "continue", above.
1446 /* All done with variable argument list */
1449 if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1450 gettimeofday(&tv, NULL);
1451 t1 = (double) tv.tv_sec + ((double) tv.tv_usec / 1000000.0);
1452 printf("%1.6lf %s\n%s\n\n", t1 - t0,
1453 ret == 0 ? "SUCCESS" : "FAIL",
1457 /* Free the memory we allocated for our query string */
1467 * This function is used for the common case of queries that return a single
1470 * NOTE: If more than one value is returned by the query, all but the first
1471 * one will be ignored.
1474 query_int(const struct lsqlite3_private * lsqlite3,
1482 sqlite3_stmt * pStmt;
1489 if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1490 gettimeofday(&tv, &tz);
1491 t0 = (double) tv.tv_sec + ((double) tv.tv_usec / 1000000.0);
1494 /* Begin access to variable argument list */
1495 va_start(args, pSql);
1497 /* Format the query */
1498 if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
1499 return SQLITE_NOMEM;
1502 if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1507 * Prepare and execute the SQL statement. Loop allows retrying on
1508 * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
1509 * requiring retrying the operation.
1511 for (bLoop = TRUE; bLoop; ) {
1513 /* Compile the SQL statement into sqlite virtual machine */
1514 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
1518 NULL)) == SQLITE_SCHEMA) {
1519 if (stmtGetEID != NULL) {
1520 sqlite3_finalize(stmtGetEID);
1524 } else if (ret != SQLITE_OK) {
1528 /* One row expected */
1529 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
1530 if (stmtGetEID != NULL) {
1531 sqlite3_finalize(stmtGetEID);
1534 (void) sqlite3_finalize(pStmt);
1536 } else if (ret != SQLITE_ROW) {
1537 (void) sqlite3_finalize(pStmt);
1541 /* Get the value to be returned */
1542 *pRet = sqlite3_column_int64(pStmt, 0);
1544 /* Free the virtual machine */
1545 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
1546 if (stmtGetEID != NULL) {
1547 sqlite3_finalize(stmtGetEID);
1551 } else if (ret != SQLITE_OK) {
1552 (void) sqlite3_finalize(pStmt);
1557 * Normal condition is only one time through loop. Loop is
1558 * rerun in error conditions, via "continue", above.
1563 /* All done with variable argument list */
1567 if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1568 gettimeofday(&tv, NULL);
1569 t1 = (double) tv.tv_sec + ((double) tv.tv_usec / 1000000.0);
1570 printf("%1.6lf %s\n%s\n\n", t1 - t0,
1571 ret == 0 ? "SUCCESS" : "FAIL",
1575 /* Free the memory we allocated for our query string */
1585 * This function is used for the very common case of retrieving an EID value
1586 * given a normalized DN.
1588 * NOTE: If more than one value is returned by the query, all but the first
1589 * one will be ignored.
1592 getEID(const struct lsqlite3_private * lsqlite3,
1594 const char * pNormalizedDN)
1598 const char * query =
1600 " FROM ldb_attribute_values\n"
1601 " WHERE attr_name = 'DN'\n"
1602 " AND attr_value_normalized = :dn;";
1605 * Prepare and execute the SQL statement. Loop allows retrying on
1606 * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
1607 * requiring retrying the operation.
1609 for (bLoop = TRUE; bLoop; ) {
1611 /* Compile the SQL statement into sqlite virtual machine */
1613 if (stmtGetEID == NULL &&
1614 (ret = sqlite3_prepare(lsqlite3->sqlite,
1618 NULL)) == SQLITE_SCHEMA) {
1620 } else if (ret != SQLITE_OK) {
1624 /* Bind our parameter */
1625 if ((ret = sqlite3_bind_text(stmtGetEID,
1629 SQLITE_STATIC)) != SQLITE_OK) {
1633 /* One row expected */
1634 if ((ret = sqlite3_step(stmtGetEID)) == SQLITE_SCHEMA) {
1635 (void) sqlite3_finalize(stmtGetEID);
1638 } else if (ret != SQLITE_ROW) {
1639 (void) sqlite3_reset(stmtGetEID);
1643 /* Get the value to be returned */
1644 *pRet = sqlite3_column_int64(stmtGetEID, 0);
1646 /* Free the virtual machine */
1647 if ((ret = sqlite3_reset(stmtGetEID)) == SQLITE_SCHEMA) {
1648 (void) sqlite3_finalize(stmtGetEID);
1651 } else if (ret != SQLITE_OK) {
1652 (void) sqlite3_finalize(stmtGetEID);
1658 * Normal condition is only one time through loop. Loop is
1659 * rerun in error conditions, via "continue", above.
1669 callback function used in call to ldb_dn_fold() for determining whether an
1670 attribute type requires case folding.
1673 case_fold_attr_required(void * hUserData,
1676 // struct ldb_module * module = hUserData;
1682 case_fold_attr_not_required(void * hUserData,
1685 // struct ldb_module * module = hUserData;
1692 * add a single set of ldap message values to a ldb_message
1696 add_msg_attr(void * hTalloc,
1699 const char * pAttrName,
1700 const char * pAttrValue,
1703 struct ldb_message *** pppRes)
1706 struct ldb_message * msg;
1707 struct ldb_message_element * el;
1709 /* Is this a different EID than the previous one? */
1710 if (eid != prevEID) {
1711 /* Yup. Add another result to the result array */
1712 if ((x = talloc_realloc(hTalloc,
1713 *pAllocated == 0 ? NULL : *pppRes,
1714 struct ldb_message *,
1715 *pAllocated + 1)) == NULL) {
1720 /* Save the new result list */
1723 /* Allocate a new result structure */
1724 if ((x = talloc(*pppRes, struct ldb_message)) == NULL) {
1728 /* Save the new result */
1729 (*pppRes)[*pAllocated] = x;
1731 /* Steal the initial result and put it in its own context */
1732 talloc_steal(NULL, *pppRes);
1734 /* We've allocated one more result */
1737 /* Ensure that the message is initialized */
1739 if ((msg->dn = talloc_strdup(msg, pDN)) == NULL) {
1742 msg->num_elements = 0;
1743 msg->elements = NULL;
1744 msg->private_data = NULL;
1746 /* Same EID. Point to the previous most-recent message */
1747 msg = (*pppRes)[*pAllocated - 1];
1750 if (pAttrName != NULL && pAttrValue != NULL) {
1752 * Point to the most recent previous element. (If there are none,
1753 * this will point to non-allocated memory, but the pointer will
1754 * never be dereferenced.)
1756 el = &msg->elements[msg->num_elements - 1];
1759 * See if the most recent previous element has the same attr_name
1761 if (msg->num_elements == 0 || strcmp(el->name, pAttrName) != 0) {
1763 /* It's a new attr_name. Allocate another message element */
1764 if ((el = talloc_realloc(msg,
1766 struct ldb_message_element,
1767 msg->num_elements + 1)) == NULL) {
1771 /* Save the new element */
1774 /* Save the attribute name */
1776 talloc_strdup(msg->elements, pAttrName)) == NULL) {
1781 /* There's now one additional element */
1782 msg->num_elements++;
1787 /* Initialize number of attribute values for this type */
1792 /* Increase the value array size by 1 */
1795 el->num_values == 0 ? NULL : el->values,
1797 el->num_values + 1)) == NULL) {
1801 /* Save the new attribute value length */
1802 el->values[el->num_values].length = strlen(pAttrValue);
1804 /* Copy the new attribute value */
1805 if ((el->values[el->num_values].data =
1806 talloc_memdup(el->values,
1808 el->values[el->num_values].length)) == NULL) {
1812 /* We now have one additional value of this type */
1820 parsetree_to_sql(struct ldb_module *module,
1822 const struct ldb_parse_tree *t)
1825 char * pNormalizedDN;
1832 switch(t->operation) {
1836 case LDB_OP_EXTENDED:
1837 #warning "work out how to handle bitops"
1841 ret = parsetree_to_sql(module,
1843 t->u.list.elements[0]);
1845 for (i = 1; i < t->u.list.num_elements; i++) {
1850 t->u.list.elements[i]);
1851 ret = talloc_asprintf_append(ret,
1859 ret = talloc_asprintf(hTalloc,
1868 ret = parsetree_to_sql(module,
1870 t->u.list.elements[0]);
1872 for (i = 1; i < t->u.list.num_elements; i++) {
1877 t->u.list.elements[i]);
1878 ret = talloc_asprintf_append(ret,
1885 ret = talloc_asprintf(hTalloc,
1899 ret = talloc_asprintf(hTalloc,
1902 " WHERE eid NOT IN (%s)\n",
1908 /* should never occur */
1912 /* Get a case-folded copy of the attribute name */
1913 pAttrName = ldb_casefold((struct ldb_context *) module,
1917 * For simple searches, we want to retrieve the list of EIDs that
1918 * match the criteria.
1920 if (t->u.simple.value.length == 1 &&
1921 (*(const char *) t->u.simple.value.data) == '*') {
1923 * Special case for "attr_name=*". In this case, we want the
1924 * eid corresponding to all values in the specified attribute
1927 if ((p = sqlite3_mprintf(" SELECT eid\n"
1928 " FROM ldb_attribute_values\n"
1929 " WHERE attr_name = %Q",
1930 pAttrName)) == NULL) {
1934 if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1938 ret = talloc_strdup(hTalloc, p);
1941 } else if (strcasecmp(t->u.simple.attr, "objectclass") == 0) {
1943 * For object classes, we want to search for all objectclasses
1944 * that are subclasses as well.
1946 if ((p = sqlite3_mprintf(
1948 " FROM ldb_attribute_values\n"
1949 " WHERE attr_name = 'OBJECTCLASS' "
1950 " AND attr_value_normalized IN\n"
1951 " (SELECT class_name\n"
1952 " FROM ldb_object_classes\n"
1953 " WHERE tree_key GLOB\n"
1954 " (SELECT tree_key\n"
1955 " FROM ldb_object_classes\n"
1956 " WHERE class_name = upper(%Q)) "
1958 t->u.simple.value.data)) == NULL) {
1962 if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1966 ret = talloc_strdup(hTalloc, p);
1969 } else if (strcasecmp(t->u.simple.attr, "dn") == 0) {
1970 pNormalizedDN = ldb_dn_fold(module->ldb, t->u.simple.value.data,
1971 module, case_fold_attr_required);
1972 if ((p = sqlite3_mprintf(
1974 " FROM ldb_attribute_values\n"
1975 " WHERE attr_name = %Q\n"
1976 " AND attr_value_normalized = %Q\n",
1978 pNormalizedDN)) == NULL) {
1982 if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1986 ret = talloc_strdup(hTalloc, p);
1989 /* A normal query. */
1990 if ((p = sqlite3_mprintf(
1992 " FROM ldb_attribute_values\n"
1993 " WHERE attr_name = %Q\n"
1994 " AND attr_value_normalized = upper(%Q)\n",
1996 t->u.simple.value.data)) == NULL) {
2000 if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
2004 ret = talloc_strdup(hTalloc, p);
2013 parsetree_to_attrlist(struct ldb_module *module,
2014 const struct ldb_parse_tree * t)
2017 struct lsqlite3_private * lsqlite3 = module->private_data;
2019 switch(t->operation) {
2023 case LDB_OP_EXTENDED:
2024 #warning "work out how to handle bitops"
2028 if (parsetree_to_attrlist(
2030 t->u.list.elements[0]) != 0) {
2034 for (i = 1; i < t->u.list.num_elements; i++) {
2035 if (parsetree_to_attrlist(
2037 t->u.list.elements[i]) != 0) {
2045 if (parsetree_to_attrlist(
2047 t->u.list.elements[0]) != 0) {
2051 for (i = 1; i < t->u.list.num_elements; i++) {
2052 if (parsetree_to_attrlist(
2054 t->u.list.elements[i]) != 0) {
2062 if (parsetree_to_attrlist(module,
2063 t->u.not.child) != 0) {
2070 /* should never occur */
2074 QUERY_NOROWS(lsqlite3,
2076 "INSERT OR IGNORE INTO " FILTER_ATTR_TABLE "\n"
2086 * Issue a series of SQL statements to implement the ADD/MODIFY/DELETE
2087 * requests in the ldb_message
2090 msg_to_sql(struct ldb_module * module,
2091 const struct ldb_message * msg,
2099 struct lsqlite3_private * lsqlite3 = module->private_data;
2101 for (i = 0; i < msg->num_elements; i++) {
2102 const struct ldb_message_element *el = &msg->elements[i];
2105 flags = LDB_FLAG_MOD_ADD;
2107 flags = el->flags & LDB_FLAG_MOD_MASK;
2110 /* Get a case-folded copy of the attribute name */
2111 pAttrName = ldb_casefold((struct ldb_context *) module,
2114 /* For each value of the specified attribute name... */
2115 for (j = 0; j < el->num_values; j++) {
2117 /* ... bind the attribute value, if necessary */
2119 case LDB_FLAG_MOD_ADD:
2123 "INSERT INTO ldb_attribute_values\n"
2127 " attr_value_normalized)\n"
2129 " (%lld, %Q, %Q, upper(%Q));",
2132 el->values[j].data, /* FIX ME */
2133 el->values[j].data);
2135 /* Is this a special "objectclass"? */
2136 if (strcasecmp(pAttrName,
2137 "objectclass") != 0) {
2142 /* Handle special "objectclass" type */
2143 QUERY_NOROWS(lsqlite3,
2146 " INTO ldb_object_classes "
2148 " parent_class_name) "
2150 " (upper(%Q), 'TOP');",
2151 ldb_casefold(module,
2152 el->values[j].data));
2155 case LDB_FLAG_MOD_REPLACE:
2159 "UPDATE ldb_attribute_values\n"
2160 " SET attr_value = %Q,\n"
2161 " attr_value_normalized =\n"
2163 " WHERE eid = %lld\n"
2164 " AND attr_name = %Q;",
2165 el->values[j].data, /* FIX ME */
2171 case LDB_FLAG_MOD_DELETE:
2172 /* No additional parameters to this query */
2176 "DELETE FROM ldb_attribute_values"
2177 " WHERE eid = %lld "
2178 " AND attr_name = %Q "
2179 " AND attr_value_normalized =\n"
2183 el->values[j].data);
2195 new_dn(struct ldb_module * module,
2204 char * pPartialNormalizedDN;
2211 struct ldb_dn * pExplodedDN;
2212 struct ldb_dn_component * pComponent;
2213 struct ldb_context * ldb = module->ldb;
2214 struct lsqlite3_private * lsqlite3 = module->private_data;
2216 if (lsqlite3_debug & SQLITE3_DEBUG_NEWDN) {
2217 gettimeofday(&tv, &tz);
2218 t0 = (double) tv.tv_sec + ((double) tv.tv_usec / 1000000.0);
2221 /* Explode the DN */
2226 case_fold_attr_not_required)) == NULL) {
2230 if (lsqlite3_debug & SQLITE3_DEBUG_NEWDN) {
2231 gettimeofday(&tv, NULL);
2232 t1 = (double) tv.tv_sec + ((double) tv.tv_usec / 1000000.0);
2233 printf("%1.6lf loc 1\n", t1 - t0);
2237 /* Allocate a string to hold the partial DN of each component */
2238 if ((pPartialDN = talloc_strdup(ldb, "")) == NULL) {
2242 if ((pPartialNormalizedDN = talloc_strdup(pPartialDN, "")) == NULL) {
2246 /* For each component of the DN (starting with the last one)... */
2247 #warning "convert this loop to recursive, and search backwards instead"
2250 for (nComponent = pExplodedDN->comp_num - 1, bFirst = TRUE;
2252 nComponent--, bFirst = FALSE) {
2254 if (lsqlite3_debug & SQLITE3_DEBUG_NEWDN) {
2255 gettimeofday(&tv, NULL);
2256 t1 = ((double) tv.tv_sec +
2257 ((double) tv.tv_usec / 1000000.0));
2258 printf("%1.6lf loc 2\n", t1 - t0);
2262 /* Point to the component */
2263 pComponent = pExplodedDN->components[nComponent];
2265 /* Add this component on to the partial DN to date */
2266 if ((p = talloc_asprintf(ldb,
2268 pComponent->component,
2270 pPartialDN)) == NULL) {
2274 if (lsqlite3_debug & SQLITE3_DEBUG_NEWDN) {
2275 gettimeofday(&tv, NULL);
2276 t1 = ((double) tv.tv_sec +
2277 ((double) tv.tv_usec / 1000000.0));
2278 printf("%1.6lf loc 3\n", t1 - t0);
2282 /* No need for the old partial DN any more */
2283 talloc_free(pPartialDN);
2285 /* Save the new partial DN */
2287 pPartialNormalizedDN = ldb_dn_fold(pPartialDN,
2290 case_fold_attr_required);
2292 if (lsqlite3_debug & SQLITE3_DEBUG_NEWDN) {
2293 gettimeofday(&tv, NULL);
2294 t1 = ((double) tv.tv_sec +
2295 ((double) tv.tv_usec / 1000000.0));
2296 printf("%1.6lf loc 4\n", t1 - t0);
2301 * Ensure that an entry is in the ldb_entry table for this
2304 if ((ret = getEID(lsqlite3,
2306 pPartialNormalizedDN)) == SQLITE_DONE) {
2308 QUERY_NOROWS(lsqlite3,
2310 "INSERT INTO ldb_entry\n"
2311 " (peid, dn, normalized_dn)\n"
2314 eid, pPartialDN, pPartialNormalizedDN);
2316 if (lsqlite3_debug & SQLITE3_DEBUG_NEWDN) {
2317 gettimeofday(&tv, NULL);
2318 t1 = ((double) tv.tv_sec +
2319 ((double) tv.tv_usec / 1000000.0));
2320 printf("%1.6lf loc 5\n", t1 - t0);
2324 /* Get the EID of the just inserted row */
2325 eid = sqlite3_last_insert_rowid(lsqlite3->sqlite);
2327 if (lsqlite3_debug & SQLITE3_DEBUG_NEWDN) {
2328 gettimeofday(&tv, NULL);
2329 t1 = ((double) tv.tv_sec +
2330 ((double) tv.tv_usec / 1000000.0));
2331 printf("%1.6lf loc 8\n", t1 - t0);
2335 /* Also add DN attribute */
2336 QUERY_NOROWS(lsqlite3,
2338 "INSERT INTO ldb_attribute_values\n"
2342 " attr_value_normalized) "
2344 " (%lld, 'DN', %Q, %Q);",
2346 pPartialDN, /* FIX ME */
2347 pPartialNormalizedDN);
2349 } else if (ret != SQLITE_OK) {
2350 UNLOCK_DB(module, "rollback");
2354 /* Save the parent EID */
2358 if (lsqlite3_debug & SQLITE3_DEBUG_NEWDN) {
2359 gettimeofday(&tv, NULL);
2360 t1 = ((double) tv.tv_sec +
2361 ((double) tv.tv_usec / 1000000.0));
2362 printf("%1.6lf loc 9\n", t1 - t0);
2366 /* Give 'em what they came for! */
2373 static unsigned char base160tab[161] = {
2374 48 ,49 ,50 ,51 ,52 ,53 ,54 ,55 ,56 ,57 , /* 0-9 */
2375 58 ,59 ,65 ,66 ,67 ,68 ,69 ,70 ,71 ,72 , /* : ; A-H */
2376 73 ,74 ,75 ,76 ,77 ,78 ,79 ,80 ,81 ,82 , /* I-R */
2377 83 ,84 ,85 ,86 ,87 ,88 ,89 ,90 ,97 ,98 , /* S-Z , a-b */
2378 99 ,100,101,102,103,104,105,106,107,108, /* c-l */
2379 109,110,111,112,113,114,115,116,117,118, /* m-v */
2380 119,120,121,122,160,161,162,163,164,165, /* w-z, latin1 */
2381 166,167,168,169,170,171,172,173,174,175, /* latin1 */
2382 176,177,178,179,180,181,182,183,184,185, /* latin1 */
2383 186,187,188,189,190,191,192,193,194,195, /* latin1 */
2384 196,197,198,199,200,201,202,203,204,205, /* latin1 */
2385 206,207,208,209,210,211,212,213,214,215, /* latin1 */
2386 216,217,218,219,220,221,222,223,224,225, /* latin1 */
2387 226,227,228,229,230,231,232,233,234,235, /* latin1 */
2388 236,237,238,239,240,241,242,243,244,245, /* latin1 */
2389 246,247,248,249,250,251,252,253,254,255, /* latin1 */
2397 * Convert an unsigned long integer into a base160 representation of the
2402 * value to be converted
2405 * character array, 5 bytes long, into which the base160 representation
2406 * will be placed. The result will be a four-digit representation of the
2407 * number (with leading zeros prepended as necessary), and null
2414 base160_sql(sqlite3_context * hContext,
2416 sqlite3_value ** argv)
2422 val = sqlite3_value_int64(argv[0]);
2424 for (i = 3; i >= 0; i--) {
2426 result[i] = base160tab[val % 160];
2432 sqlite3_result_text(hContext, result, -1, SQLITE_TRANSIENT);
2439 * This function enhances sqlite by adding a "base160_next()" function which is
2440 * accessible via queries.
2442 * Retrieve the next-greater number in the base160 sequence for the terminal
2443 * tree node (the last four digits). Only one tree level (four digits) is
2447 * A character string: either an empty string (in which case no operation is
2448 * performed), or a string of base160 digits with a length of a multiple of
2452 * Upon return, the trailing four digits (one tree level) will have been
2456 base160next_sql(sqlite3_context * hContext,
2458 sqlite3_value ** argv)
2462 unsigned char * pTab;
2463 unsigned char * pBase160 =
2464 strdup(sqlite3_value_text(argv[0]));
2465 unsigned char * pStart = pBase160;
2468 * We need a minimum of four digits, and we will always get a multiple
2471 if (pBase160 != NULL &&
2472 (len = strlen(pBase160)) >= 4 &&
2475 if (pBase160 == NULL) {
2477 sqlite3_result_null(hContext);
2481 pBase160 += strlen(pBase160) - 1;
2483 /* We only carry through four digits: one level in the tree */
2484 for (i = 0; i < 4; i++) {
2486 /* What base160 value does this digit have? */
2487 pTab = strchr(base160tab, *pBase160);
2489 /* Is there a carry? */
2490 if (pTab < base160tab + sizeof(base160tab) - 1) {
2493 * Nope. Just increment this value and we're
2496 *pBase160 = *++pTab;
2501 * There's a carry. This value gets
2502 * base160tab[0], we decrement the buffer
2503 * pointer to get the next higher-order digit,
2504 * and continue in the loop.
2506 *pBase160-- = base160tab[0];
2510 sqlite3_result_text(hContext,
2515 sqlite3_result_value(hContext, argv[0]);
2516 if (pBase160 != NULL) {
2524 static int lock_debug(struct ldb_module * module,
2525 const char * lockname,
2526 const char * pFileName,
2530 struct lsqlite3_private * lsqlite3 = module->private_data;
2532 printf("%s(%d): LOCK (%d) ",
2533 pFileName, linenum, lsqlite3->lock_count);
2534 ret = lsqlite3_lock(module, lockname);
2535 printf("got %d\n", ret);
2541 static int unlock_debug(struct ldb_module * module,
2542 const char * lockname,
2543 const char * pFileName,
2547 struct lsqlite3_private * lsqlite3 = module->private_data;
2549 ret = lsqlite3_unlock(module, lockname);
2550 printf("%s(%d): UNLOCK (%d) got %d\n",
2551 pFileName, linenum, lsqlite3->lock_count, ret);