From 6d82f29f090d36d2f3d7191e16c0a10f025af366 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sun, 22 Apr 2018 12:08:44 +0200 Subject: [PATCH] Add BLOCKINGMODE=IP|NXDOMAIN config flag for selecting if FTLDNS should reply with IPv4/IPv6 addresses (need 2 cache slots per domain) or with NXDOMAIN (need 1 cache slot per domain) Signed-off-by: DL6ER --- FTL.h | 2 ++ README.md | 1 + config.c | 21 +++++++++++++++++++++ dnsmasq_interface.c | 10 +++++++--- 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/FTL.h b/FTL.h index 30d96646..8569e695 100644 --- a/FTL.h +++ b/FTL.h @@ -132,6 +132,7 @@ typedef struct { int maxlogage; int privacylevel; bool ignore_localhost; + unsigned char blockingmode; } ConfigStruct; // Dynamic structs @@ -212,6 +213,7 @@ enum { QUERY_UNKNOWN, QUERY_GRAVITY, QUERY_FORWARDED, QUERY_CACHE, QUERY_WILDCAR enum { TYPE_A = 1, TYPE_AAAA, TYPE_ANY, TYPE_SRV, TYPE_SOA, TYPE_PTR, TYPE_TXT, TYPE_MAX }; enum { REPLY_UNKNOWN, REPLY_NODATA, REPLY_NXDOMAIN, REPLY_CNAME, REPLY_IP }; enum { PRIVACY_SHOW_ALL = 0, PRIVACY_HIDE_DOMAINS, PRIVACY_HIDE_DOMAINS_CLIENTS, PRIVACY_MAXIMUM }; +enum { MODE_IP, MODE_NX }; // Used to check memory integrity in various structs #define MAGICBYTE 0x57 diff --git a/README.md b/README.md index 11ec74ed..8d0e0d62 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,7 @@ Possible settings (**the option shown first is the default**): - `FTLPORT=4711` (On which port should FTL be listening?) - `PRIVACYLEVEL=0` (Which privacy level is used? Can be 0 (permissive) to 3 (very restrictive), see below) - `IGNORE_LOCALHOST=no|yes` (Should `FTL` ignore queries coming from the local machine?) +- `BLOCKINGMODE=IP|NXDOMAIN` (Should `FTL` reply queries to blocked domains with IPs or NXDOMAIN?) ### Privacy levels Specifies if we want to anonymize the DNS queries somehow, available options are: diff --git a/config.c b/config.c index 94f53dbe..3bb6c02e 100644 --- a/config.c +++ b/config.c @@ -187,6 +187,27 @@ void read_FTLconf(void) else logg(" IGNORE_LOCALHOST: Show queries from localhost"); + // BLOCKINGMODE + // defaults to: MODE_IP + config.blockingmode = MODE_IP; + buffer = parse_FTLconf(fp, "BLOCKINGMODE"); + + if(buffer != NULL) + { + if(strcasecmp(buffer, "NXDOMAIN") == 0) + config.blockingmode = MODE_NX; + } + + switch(config.blockingmode) + { + case MODE_NX: + logg(" BLOCKINGMODE: NXDOMAIN for blocked domains"); + break; + default: + logg(" BLOCKINGMODE: Pi-hole's IP for blocked domains"); + break; + } + logg("Finished config file parsing"); // Release memory diff --git a/dnsmasq_interface.c b/dnsmasq_interface.c index 97b96f6d..518ddd20 100644 --- a/dnsmasq_interface.c +++ b/dnsmasq_interface.c @@ -500,6 +500,8 @@ void FTL_cache(unsigned int flags, char *name, struct all_addr *addr, char *arg, requesttype = QUERY_GRAVITY; else if(arg != NULL && strstr(arg, "/black.list") != NULL) requesttype = QUERY_BLACKLIST; + else if(flags & F_NXDOMAIN) + requesttype = QUERY_GRAVITY; else // local.list, hostname.list, /etc/hosts and others requesttype = QUERY_CACHE; } @@ -846,20 +848,22 @@ int FTL_listsfile(char* filename, unsigned int index, FILE *f, int cache_size, s { strcpy(cache4->name.sname, domain); cache4->flags = F_HOSTS | F_IMMORTAL | F_FORWARD | F_REVERSE | F_IPV4; + if(config.blockingmode == MODE_NX) cache4->flags |= F_NEG | F_NXDOMAIN; cache4->ttd = daemon->local_ttl; add_hosts_entry(cache4, &addr4, INADDRSZ, index, rhash, hashsz); + name_count++; } - // Add IPv6 record - if ((cache6 = malloc(sizeof(struct crec) + strlen(domain)+1-SMALLDNAME))) + // Add IPv6 record only if we respond with an IP address to blocked domains + if (config.blockingmode == MODE_IP && (cache6 = malloc(sizeof(struct crec) + strlen(domain)+1-SMALLDNAME))) { strcpy(cache6->name.sname, domain); cache6->flags = F_HOSTS | F_IMMORTAL | F_FORWARD | F_REVERSE | F_IPV6; cache6->ttd = daemon->local_ttl; add_hosts_entry(cache6, &addr6, IN6ADDRSZ, index, rhash, hashsz); + name_count++; } added++; - name_count++; } // Free allocated memory