From 08b00d2b0539a7f0f9847ca63efe2018ee4a7cb5 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 13 Jul 2020 20:39:48 +0200 Subject: [PATCH] Add additional_info column to queries table. We fill it with the domain that caused blocking the entire CNAME chain. Signed-off-by: DL6ER --- src/database/common.c | 18 ++++++++++++++++++ src/database/query-table.c | 17 ++++++++++++++++- src/dnsmasq_interface.c | 4 ++-- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/database/common.c b/src/database/common.c index ddffa2e5..51c7b73b 100644 --- a/src/database/common.c +++ b/src/database/common.c @@ -361,6 +361,24 @@ void db_init(void) dbversion = db_get_FTL_property(DB_VERSION); } + // Update to version 7 if lower + if(dbversion < 7) + { + // Update to version 7: Create message table + logg("Updating long-term database to version 7"); + if(dbquery("ALTER TABLE queries ADD COLUMN additional_info TEXT;") != SQLITE_OK || + !db_set_FTL_property(DB_VERSION, 7)) + { + logg("Column additional_info not initialized, database not available"); + dbclose(); + + database = false; + return; + } + // Get updated version + dbversion = db_get_FTL_property(DB_VERSION); + } + // Close database to prevent having it opened all time // We already closed the database when we returned earlier dbclose(); diff --git a/src/database/query-table.c b/src/database/query-table.c index e27c8e50..b4424979 100644 --- a/src/database/query-table.c +++ b/src/database/query-table.c @@ -85,7 +85,7 @@ void DB_save_queries(void) return; } - rc = sqlite3_prepare_v2(FTL_db, "INSERT INTO queries VALUES (NULL,?,?,?,?,?,?)", -1, &stmt, NULL); + rc = sqlite3_prepare_v2(FTL_db, "INSERT INTO queries VALUES (NULL,?,?,?,?,?,?,?)", -1, &stmt, NULL); if( rc != SQLITE_OK ) { const char *text, *spaces; @@ -169,6 +169,21 @@ void DB_save_queries(void) sqlite3_bind_null(stmt, 6); } + // Fill additional information column + if(query->status == QUERY_GRAVITY_CNAME || + query->status == QUERY_REGEX_CNAME || + query->status == QUERY_BLACKLIST_CNAME) + { + // Get domain blocked during deep CNAME inspection, if applicable + const char* cname = getCNAMEDomainString(query); + sqlite3_bind_text(stmt, 7, cname, -1, SQLITE_STATIC); + } + else + { + // Nothing to add here + sqlite3_bind_null(stmt, 7); + } + // Step and check if successful rc = sqlite3_step(stmt); sqlite3_clear_bindings(stmt); diff --git a/src/dnsmasq_interface.c b/src/dnsmasq_interface.c index 00b50be6..8e9d8564 100644 --- a/src/dnsmasq_interface.c +++ b/src/dnsmasq_interface.c @@ -163,7 +163,7 @@ static bool _FTL_check_blocking(int queryID, int domainID, int clientID, const c } // Do not block if the entire query is to be permitted - // as sometving along the CNAME path hit the whitelist + // as something along the CNAME path hit the whitelist if(!query->whitelisted) { query_blocked(query, domain, client, QUERY_BLACKLIST); @@ -357,7 +357,7 @@ bool _FTL_CNAME(const char *domain, const struct crec *cpp, const int id, const // If we find during a CNAME inspection that we want to block the entire chain, // the originally queried domain itself was not counted as blocked (but as - // (permitted). Later in the chain, when we find that this is a bad guy, we + // permitted). Later in the chain, when we find that this is a bad guy, we // short-circuit it. We need to correct the domain counter of the domain at the // head of the chain, otherwise, the data for the top lists is misleading. // For this, we go back the entire path and change the original request to blocked