From 1ccfd9a081490a2ae5ca84321c1ec0af09cfffa2 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 20 Aug 2022 15:27:38 +0200 Subject: [PATCH] Ensure the API does not try to create DNS cache records for queries that don't have one Signed-off-by: DL6ER --- src/api/api.c | 2 +- src/database/query-table.c | 4 ++-- src/datastructure.c | 5 ++++- src/datastructure.h | 4 ++-- src/dnsmasq_interface.c | 6 +++--- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/api/api.c b/src/api/api.c index 67da3334..441f1626 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -1012,7 +1012,7 @@ void getAllQueries(const char *client_message, const int sock, const bool isteln int domainlist_id = -1; if (config.privacylevel < PRIVACY_HIDE_DOMAINS) { - unsigned int cacheID = findCacheID(query->domainID, query->clientID, query->type); + unsigned int cacheID = findCacheID(query->domainID, query->clientID, query->type, false); DNSCacheData *dns_cache = getDNSCache(cacheID, true); if(dns_cache != NULL) domainlist_id = dns_cache->domainlist_id; diff --git a/src/database/query-table.c b/src/database/query-table.c index 3b50b66f..ed532066 100644 --- a/src/database/query-table.c +++ b/src/database/query-table.c @@ -373,7 +373,7 @@ int DB_save_queries(sqlite3 *db) sqlite3_bind_null(query_stmt, 7); } - const int cacheID = findCacheID(query->domainID, query->clientID, query->type); + const int cacheID = findCacheID(query->domainID, query->clientID, query->type, false); DNSCacheData *cache = getDNSCache(cacheID, true); // ADDITIONAL_INFO @@ -933,7 +933,7 @@ void DB_read_queries(void) // Set ID of the domainlist entry that was the reason for permitting/blocking this query // We assume the value in this field is said ID when it is not a CNAME-related domain // (checked above) and the value of additional_info is not NULL (0 bytes storage size) - const int cacheID = findCacheID(query->domainID, query->clientID, query->type); + const int cacheID = findCacheID(query->domainID, query->clientID, query->type, true); DNSCacheData *cache = getDNSCache(cacheID, true); // Only load if // a) we have a cache entry diff --git a/src/datastructure.c b/src/datastructure.c index e1aa775a..904e49ca 100644 --- a/src/datastructure.c +++ b/src/datastructure.c @@ -319,7 +319,7 @@ void change_clientcount(clientsData *client, int total, int blocked, int overTim } } -int _findCacheID(int domainID, int clientID, enum query_types query_type, const char *func, int line, const char *file) +int _findCacheID(const int domainID, const int clientID, const enum query_types query_type, const bool create_new, const char *func, int line, const char *file) { // Compare content of client against known client IP addresses for(int cacheID = 0; cacheID < counters->dns_cache_size; cacheID++) @@ -339,6 +339,9 @@ int _findCacheID(int domainID, int clientID, enum query_types query_type, const } } + if(!create_new) + return -1; + // Get ID of new cache entry const int cacheID = counters->dns_cache_size; diff --git a/src/datastructure.h b/src/datastructure.h index e1ef2779..a37db29c 100644 --- a/src/datastructure.h +++ b/src/datastructure.h @@ -113,8 +113,8 @@ int findQueryID(const int id); int findUpstreamID(const char * upstream, const in_port_t port); int findDomainID(const char *domain, const bool count); int findClientID(const char *client, const bool count, const bool aliasclient); -#define findCacheID(domainID, clientID, query_type) _findCacheID(domainID, clientID, query_type, __FUNCTION__, __LINE__, __FILE__) -int _findCacheID(int domainID, int clientID, enum query_types query_type, const char *func, const int line, const char *file); +#define findCacheID(domainID, clientID, query_type, create_new) _findCacheID(domainID, clientID, query_type, create_new, __FUNCTION__, __LINE__, __FILE__) +int _findCacheID(const int domainID, const int clientID, const enum query_types query_type, const bool create_new, const char *func, const int line, const char *file); bool isValidIPv4(const char *addr); bool isValidIPv6(const char *addr); diff --git a/src/dnsmasq_interface.c b/src/dnsmasq_interface.c index 4c31c9f2..a652c44a 100644 --- a/src/dnsmasq_interface.c +++ b/src/dnsmasq_interface.c @@ -1249,7 +1249,7 @@ static bool _FTL_check_blocking(int queryID, int domainID, int clientID, const c } // Get cache pointer - unsigned int cacheID = findCacheID(domainID, clientID, query->type); + unsigned int cacheID = findCacheID(domainID, clientID, query->type, true); DNSCacheData *dns_cache = getDNSCache(cacheID, true); if(dns_cache == NULL) { @@ -1581,8 +1581,8 @@ bool _FTL_CNAME(const char *domain, const struct crec *cpp, const int id, const else if(query->status == QUERY_REGEX) { // Get parent and child DNS cache entries - const int parent_cacheID = findCacheID(parent_domainID, clientID, query->type); - const int child_cacheID = findCacheID(child_domainID, clientID, query->type); + const int parent_cacheID = findCacheID(parent_domainID, clientID, query->type, false); + const int child_cacheID = findCacheID(child_domainID, clientID, query->type, false); // Get cache pointers DNSCacheData *parent_cache = getDNSCache(parent_cacheID, true);