diff --git a/FTL.h b/FTL.h index 7719da4c..d86b766f 100644 --- a/FTL.h +++ b/FTL.h @@ -141,10 +141,6 @@ typedef struct { } FTLFileNamesStruct; typedef struct { - char* whitelist; - char* blacklist; - char* gravity; - char* regexlist; char* setupVars; char* auditlist; } logFileNamesStruct; diff --git a/config.c b/config.c index 408570cd..1d7257e7 100644 --- a/config.c +++ b/config.c @@ -288,18 +288,6 @@ void read_FTLconf(void) // SOCKETFILE getpath(fp, "SOCKETFILE", "/var/run/pihole/FTL.sock", &FTLfiles.socketfile); - // WHITELISTFILE - getpath(fp, "WHITELISTFILE", "/etc/pihole/whitelist.txt", &files.whitelist); - - // BLACKLISTFILE - getpath(fp, "BLACKLISTFILE", "/etc/pihole/black.list", &files.blacklist); - - // GRAVITYFILE - getpath(fp, "GRAVITYFILE", "/etc/pihole/gravity.list", &files.gravity); - - // REGEXLISTFILE - getpath(fp, "REGEXLISTFILE", "/etc/pihole/regex.list", &files.regexlist); - // SETUPVARSFILE getpath(fp, "SETUPVARSFILE", "/etc/pihole/setupVars.conf", &files.setupVars); diff --git a/dnsmasq/cache.c b/dnsmasq/cache.c index 77afd391..f43cfb48 100644 --- a/dnsmasq/cache.c +++ b/dnsmasq/cache.c @@ -981,9 +981,6 @@ int read_hostsfile(char *filename, unsigned int index, int cache_size, struct cr eatspace(f); - name_count = FTL_listsfile(filename, index, f, cache_size, rhash, hashsz); - addr_count = name_count - cache_size; - while ((atnl = gettok(f, token)) != EOF) { lineno++; @@ -1185,6 +1182,10 @@ void cache_reload(void) } else { + /*------------------------------- Pi-hole modification -------------------------------*/ + total_size = FTL_database_import(total_size, (struct crec **)daemon->packet, revhashsz); + /*------------------------------------------------------------------------------------*/ + if (!option_bool(OPT_NO_HOSTS)) total_size = read_hostsfile(HOSTSFILE, SRC_HOSTS, total_size, (struct crec **)daemon->packet, revhashsz); @@ -1688,7 +1689,11 @@ char *record_source(unsigned int index) return HOSTSFILE; /*----- Pi-hole modification -----*/ else if (index == SRC_REGEX) - return (char*)regexlistname; + return "regex"; + else if (index == SRC_GRAVITY) + return "gravity"; + else if (index == SRC_BLACK) + return "blacklist"; /*--------------------------------*/ for (ah = daemon->addn_hosts; ah; ah = ah->next) diff --git a/dnsmasq/dnsmasq.h b/dnsmasq/dnsmasq.h index 4eb64557..52b39968 100644 --- a/dnsmasq/dnsmasq.h +++ b/dnsmasq/dnsmasq.h @@ -490,13 +490,16 @@ struct crec { #define SRC_INTERFACE UID_NONE #define SRC_CONFIG 1 #define SRC_HOSTS 2 -/*----- Pi-hole modification -----*/ -// ID 3 will be used for the regex list file name -// ID 4 will be used as starting index for any Additional Hosts (AH) files +/*------------------------- Pi-hole modification -------------------------*/ +// ID 3 will be used for the regex table +// ID 3 will be used for the gravity table +// ID 3 will be used for the blacklist table +// ID 6 will be used as starting index for any Additional Hosts (AH) files #define SRC_REGEX 3 -#define SRC_AH 4 -const char *regexlistname; -/*--------------------------------*/ +#define SRC_GRAVITY 4 +#define SRC_BLACK 5 +#define SRC_AH 6 +/*------------------------------------------------------------------------*/ /* struct sockaddr is not large enough to hold any address, and specifically not big enough to hold an IPv6 address. diff --git a/dnsmasq_interface.c b/dnsmasq_interface.c index df3439f4..7fbd4ed3 100644 --- a/dnsmasq_interface.c +++ b/dnsmasq_interface.c @@ -1323,75 +1323,28 @@ static void block_single_domain_regex(const char *domain) // Get IPv4/v6 addresses for blocking depending on user configures blocking mode prepare_blocking_mode(&addr4, &addr6, &has_IPv4, &has_IPv6); - regexlistname = files.regexlist; add_blocked_domain(&addr4, &addr6, has_IPv4, has_IPv6, domain, strlen(domain), NULL, 0, SRC_REGEX); if(config.debug & DEBUG_QUERIES) logg("Added %s to cache", domain); } -int FTL_listsfile(const char* filename, unsigned int index, FILE *f, int cache_size, struct crec **rhash, int hashsz) +static int FTL_table_import(const char *tablename, const unsigned char list, const unsigned int index, + struct all_addr addr4, struct all_addr addr6, bool has_IPv4, bool has_IPv6, + int cache_size, struct crec **rhash, int hashsz) { - int name_count = cache_size; - int added = 0; - struct all_addr addr4 = {{{ 0 }}}, addr6 = {{{ 0 }}}; - bool has_IPv4 = false, has_IPv6 = false; - unsigned char list = UNKNOWN_LIST; - - // Handle only gravity.list and black.list - // Skip all other files (they are interpreted in the usual format) - const char* tablename = NULL; - if(strcmp(filename, files.gravity) == 0) - { - list = GRAVITY_LIST; - tablename = "gravity"; - } - else if(strcmp(filename, files.blacklist) == 0) - { - list = BLACK_LIST; - tablename = "blacklist"; - } - else - return cache_size; - - // Jump to end of file to ensure dnsmasq does not - // try to read whatever might be in the file on - // disk when we return - fseek(f, 0, SEEK_END); - - if(blockingstatus == BLOCKING_DISABLED) - { - logg("Skipping import of %s because blocking is disabled", tablename); - return cache_size; - } - else - { - logg("Importing %s...", tablename); - } + // Variables + int name_count = cache_size, added = 0; // Start timer for list analysis timer_start(LISTS_TIMER); - // Start database interaction + // Get database table handle if(!gravityDB_getTable(list)) { logg("FTL_listsfile(): Error getting %s table from database", tablename); return name_count; } - // Get IPv4/v6 addresses for blocking depending on user configured blocking mode - prepare_blocking_mode(&addr4, &addr6, &has_IPv4, &has_IPv6); - - // If we have neither a valid IPv4 nor a valid IPv6 but the user asked for - // blocking modes MODE_IP or MODE_IP_NODATA_AAAA then we cannot add any entries here - if(!has_IPv4 && !has_IPv6) - { - logg("ERROR: Cannot add domains from gravity because pihole-FTL found\n" \ - " neither a valid IPV4_ADDRESS nor IPV6_ADDRESS in setupVars.conf" \ - " This is an impossible configuration. Please contact the Pi-hole" \ - " support if you need assistance."); - return cache_size; - } - // Walk database table const char *domain = NULL; while((domain = gravityDB_getDomain()) != NULL) @@ -1425,6 +1378,43 @@ int FTL_listsfile(const char* filename, unsigned int index, FILE *f, int cache_s // Final logging logg("Database (%s): imported %i domains (took %.1f ms)", tablename, added, timer_elapsed_msec(LISTS_TIMER)); - counters->gravity += added; - return name_count; + + return added; +} + +int FTL_database_import(int cache_size, struct crec **rhash, int hashsz) +{ + struct all_addr addr4 = {{{ 0 }}}, addr6 = {{{ 0 }}}; + bool has_IPv4 = false, has_IPv6 = false; + + if(blockingstatus == BLOCKING_DISABLED) + { + logg("Skipping import of database tables because blocking is disabled"); + return cache_size; + } + + // Get IPv4/v6 addresses for blocking depending on user configured blocking mode + prepare_blocking_mode(&addr4, &addr6, &has_IPv4, &has_IPv6); + + // If we have neither a valid IPv4 nor a valid IPv6 but the user asked for + // blocking modes MODE_IP or MODE_IP_NODATA_AAAA then we cannot add any entries here + if(!has_IPv4 && !has_IPv6) + { + logg("ERROR: Cannot add domains from gravity because pihole-FTL found\n" \ + " neither a valid IPV4_ADDRESS nor IPV6_ADDRESS in setupVars.conf" \ + " This is an impossible configuration. Please contact the Pi-hole" \ + " support if you need assistance."); + return cache_size; + } + + // Import gravity and blacklist domains + int added; + added = FTL_table_import("gravity", GRAVITY_LIST, SRC_GRAVITY, addr4, addr6, has_IPv4, has_IPv6, cache_size, rhash, hashsz); + added += FTL_table_import("blacklist", BLACK_LIST, SRC_BLACK, addr4, addr6, has_IPv4, has_IPv6, cache_size, rhash, hashsz); + + // Update counter of blocked domains + counters->gravity += added; + + // Return new cache size which now includes more domains than before + return cache_size + added; } diff --git a/dnsmasq_interface.h b/dnsmasq_interface.h index 77437552..7c2df6f6 100644 --- a/dnsmasq_interface.h +++ b/dnsmasq_interface.h @@ -40,6 +40,6 @@ void _FTL_upstream_error(const unsigned int rcode, const int id, const char* fil void FTL_dnsmasq_reload(void); void FTL_fork_and_bind_sockets(struct passwd *ent_pw); -int FTL_listsfile(const char* filename, unsigned int index, FILE *f, int cache_size, struct crec **rhash, int hashsz); +int FTL_database_import(int cache_size, struct crec **rhash, int hashsz); #endif // DNSMASQ_INTERFACE_H diff --git a/memory.c b/memory.c index dcb6513f..ec8e0bf0 100644 --- a/memory.c +++ b/memory.c @@ -26,10 +26,6 @@ FTLFileNamesStruct FTLfiles = { }; logFileNamesStruct files = { - NULL, - NULL, - NULL, - NULL, NULL, NULL };