Skip to content

Commit

Permalink
Merge branch 'development' into new/be_less_nice
Browse files Browse the repository at this point in the history
Signed-off-by: DL6ER <dl6er@dl6er.de>
  • Loading branch information
DL6ER committed Jun 4, 2020
2 parents 5970c05 + 7382d7c commit 16c2f2e
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 62 deletions.
80 changes: 49 additions & 31 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ static char *parse_FTLconf(FILE *fp, const char * key);
static void release_config_memory(void);
static void getpath(FILE* fp, const char *option, const char *defaultloc, char **pointer);
static void set_nice(const char *buffer, int fallback);
static bool read_bool(const char *option, const bool fallback);

void getLogFilePath(void)
{
Expand Down Expand Up @@ -116,11 +117,8 @@ void read_FTLconf(void)

// AAAA_QUERY_ANALYSIS
// defaults to: Yes
config.analyze_AAAA = true;
buffer = parse_FTLconf(fp, "AAAA_QUERY_ANALYSIS");

if(buffer != NULL && strcasecmp(buffer, "no") == 0)
config.analyze_AAAA = false;
config.analyze_AAAA = read_bool(buffer, true);

if(config.analyze_AAAA)
logg(" AAAA_QUERY_ANALYSIS: Show AAAA queries");
Expand All @@ -144,11 +142,8 @@ void read_FTLconf(void)

// RESOLVE_IPV6
// defaults to: Yes
config.resolveIPv6 = true;
buffer = parse_FTLconf(fp, "RESOLVE_IPV6");

if(buffer != NULL && strcasecmp(buffer, "no") == 0)
config.resolveIPv6 = false;
config.resolveIPv6 = read_bool(buffer, true);

if(config.resolveIPv6)
logg(" RESOLVE_IPV6: Resolve IPv6 addresses");
Expand All @@ -157,10 +152,9 @@ void read_FTLconf(void)

// RESOLVE_IPV4
// defaults to: Yes
config.resolveIPv4 = true;
buffer = parse_FTLconf(fp, "RESOLVE_IPV4");
if(buffer != NULL && strcasecmp(buffer, "no") == 0)
config.resolveIPv4 = false;
config.resolveIPv4 = read_bool(buffer, true);

if(config.resolveIPv4)
logg(" RESOLVE_IPV4: Resolve IPv4 addresses");
else
Expand Down Expand Up @@ -248,8 +242,8 @@ void read_FTLconf(void)

// IGNORE_LOCALHOST
// defaults to: false
config.ignore_localhost = false;
buffer = parse_FTLconf(fp, "IGNORE_LOCALHOST");
config.ignore_localhost = read_bool(buffer, false);

if(buffer != NULL && strcasecmp(buffer, "yes") == 0)
config.ignore_localhost = true;
Expand Down Expand Up @@ -283,8 +277,8 @@ void read_FTLconf(void)

// ANALYZE_ONLY_A_AND_AAAA
// defaults to: false
config.analyze_only_A_AAAA = false;
buffer = parse_FTLconf(fp, "ANALYZE_ONLY_A_AND_AAAA");
config.analyze_only_A_AAAA = read_bool(buffer, false);

if(buffer != NULL && strcasecmp(buffer, "true") == 0)
config.analyze_only_A_AAAA = true;
Expand All @@ -296,10 +290,9 @@ void read_FTLconf(void)

// DBIMPORT
// defaults to: Yes
config.DBimport = true;
buffer = parse_FTLconf(fp, "DBIMPORT");
if(buffer != NULL && strcasecmp(buffer, "no") == 0)
config.DBimport = false;
config.DBimport = read_bool(buffer, true);

if(config.DBimport)
logg(" DBIMPORT: Importing history from database");
else
Expand All @@ -325,11 +318,8 @@ void read_FTLconf(void)

// PARSE_ARP_CACHE
// defaults to: true
config.parse_arp_cache = true;
buffer = parse_FTLconf(fp, "PARSE_ARP_CACHE");

if(buffer != NULL && strcasecmp(buffer, "false") == 0)
config.parse_arp_cache = false;
config.parse_arp_cache = read_bool(buffer, true);

if(config.parse_arp_cache)
logg(" PARSE_ARP_CACHE: Active");
Expand All @@ -338,11 +328,8 @@ void read_FTLconf(void)

// CNAME_DEEP_INSPECT
// defaults to: true
config.cname_inspection = true;
buffer = parse_FTLconf(fp, "CNAME_DEEP_INSPECT");

if(buffer != NULL && strcasecmp(buffer, "false") == 0)
config.cname_inspection = false;
config.cname_inspection = read_bool(buffer, true);

if(config.cname_inspection)
logg(" CNAME_DEEP_INSPECT: Active");
Expand All @@ -356,19 +343,14 @@ void read_FTLconf(void)
config.delay_startup = 0;
if(buffer != NULL && sscanf(buffer, "%u", &config.delay_startup) &&
(config.delay_startup > 0 && config.delay_startup <= 300))
{
logg(" DELAY_STARTUP: Requested to wait %u seconds during startup.", config.delay_startup);
}
else
logg(" DELAY_STARTUP: No delay requested.");

// BLOCK_ESNI
// defaults to: true
config.block_esni = true;
buffer = parse_FTLconf(fp, "BLOCK_ESNI");

if(buffer != NULL && strcasecmp(buffer, "false") == 0)
config.block_esni = false;
config.block_esni = read_bool(buffer, true);

// NICE
// Shall we change the nice of the current process?
Expand All @@ -389,6 +371,25 @@ void read_FTLconf(void)
else
logg(" BLOCK_ESNI: Disabled");

// NAMES_FROM_NETDB
// Should we use the fallback option to try to obtain client names from
// checking the network table? Assume this is an IPv6 client without a
// host names itself but the network table tells us that this is the same
// device where we have a host names for its IPv4 address. In this case,
// we use the host name associated to the other address as this is the same
// device. This behavior can be disabled using NAMES_FROM_NETDB=false
// defaults to: true
config.names_from_netdb = true;
buffer = parse_FTLconf(fp, "NAMES_FROM_NETDB");

if(buffer != NULL && strcasecmp(buffer, "false") == 0)
config.names_from_netdb = false;

if(config.names_from_netdb)
logg(" NAMES_FROM_NETDB: Enabled, trying to get names from network database");
else
logg(" NAMES_FROM_NETDB: Disabled");

// Read DEBUG_... setting from pihole-FTL.conf
read_debuging_settings(fp);

Expand Down Expand Up @@ -579,7 +580,7 @@ static void setDebugOption(FILE* fp, const char* option, int16_t bitmask)
return;

// Set bit if value equals "true", clear bit otherwise
if(strcasecmp(buffer, "true") == 0)
if(read_bool(buffer, false))
config.debug |= bitmask;
else
config.debug &= ~bitmask;
Expand Down Expand Up @@ -707,6 +708,7 @@ void read_debuging_settings(FILE *fp)
}
}


static void set_nice(const char *buffer, const int fallback)
{
int value, nice_set, nice_target = fallback;
Expand Down Expand Up @@ -748,3 +750,19 @@ static void set_nice(const char *buffer, const int fallback)
nice_set, nice_target);
}
}

static bool read_bool(const char *option, const bool fallback)
{
if(option == NULL)
return fallback;

else if(strcasecmp(option, "false") == 0 ||
strcasecmp(option, "no") == 0)
return false;

else if(strcasecmp(option, "true") == 0 ||
strcasecmp(option, "yes") == 0)
return true;

return fallback;
}
1 change: 1 addition & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ typedef struct {
bool parse_arp_cache;
bool cname_inspection;
bool block_esni;
bool names_from_netdb;
} ConfigStruct;

typedef struct {
Expand Down
44 changes: 25 additions & 19 deletions src/database/gravity-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ static bool get_client_groupids(clientsData* client)
{
char *querystr = NULL;
const char *ip = getstr(client->ippos);
client->groups = NULL;
client->found_group = false;
client->groupspos = 0u;

// Do not proceed when database is not available
if(!gravityDB_opened && !gravityDB_open())
Expand Down Expand Up @@ -257,7 +258,8 @@ static bool get_client_groupids(clientsData* client)
{
// Found no record for this client in the database
// This makes this client qualify for the special "all" group
client->groups = strdup("0");
client->groupspos = addstr("0");
client->found_group = true;
}
else
{
Expand All @@ -273,9 +275,10 @@ static bool get_client_groupids(clientsData* client)
free(querystr);
querystr = NULL;

if(client->groups != NULL)
if(client->found_group)
{
// The client is not configured through the client table, return early
// The client is not configured through the client table, we
// substituted the default group. Return early here.
return true;
}

Expand Down Expand Up @@ -327,15 +330,17 @@ static bool get_client_groupids(clientsData* client)
// There is a record for this client in the database
const char* result = (const char*)sqlite3_column_text(table_stmt, 0);
if(result != NULL)
client->groups = strdup(result);
else
client->groups = strdup("");
{
client->groupspos = addstr(result);
client->found_group = true;
}
}
else if(rc == SQLITE_DONE)
{
// Found no record for this client in the database
// -> No associated groups
client->groups = strdup("");
client->groupspos = addstr("");
client->found_group = true;
}
else
{
Expand Down Expand Up @@ -421,7 +426,7 @@ bool gravityDB_prepare_client_statements(const int clientID, clientsData *client

// Get associated groups for this client (if defined)
char *querystr = NULL;
if(client->groups == NULL && !get_client_groupids(client))
if(!client->found_group && !get_client_groupids(client))
return false;

// Prepare whitelist statement
Expand All @@ -432,7 +437,7 @@ bool gravityDB_prepare_client_statements(const int clientID, clientsData *client
// of EXISTS().
if(config.debug & DEBUG_DATABASE)
logg("gravityDB_open(): Preparing vw_whitelist statement for client %s", clientip);
querystr = get_client_querystr("vw_whitelist", client->groups);
querystr = get_client_querystr("vw_whitelist", getstr(client->groupspos));
sqlite3_stmt* stmt = NULL;
int rc = sqlite3_prepare_v2(gravity_db, querystr, -1, &stmt, NULL);
if( rc != SQLITE_OK )
Expand All @@ -447,7 +452,7 @@ bool gravityDB_prepare_client_statements(const int clientID, clientsData *client
// Prepare gravity statement
if(config.debug & DEBUG_DATABASE)
logg("gravityDB_open(): Preparing vw_gravity statement for client %s", clientip);
querystr = get_client_querystr("vw_gravity", client->groups);
querystr = get_client_querystr("vw_gravity", getstr(client->groupspos));
rc = sqlite3_prepare_v2(gravity_db, querystr, -1, &stmt, NULL);
if( rc != SQLITE_OK )
{
Expand All @@ -461,7 +466,7 @@ bool gravityDB_prepare_client_statements(const int clientID, clientsData *client
// Prepare blacklist statement
if(config.debug & DEBUG_DATABASE)
logg("gravityDB_open(): Preparing vw_blacklist statement for client %s", clientip);
querystr = get_client_querystr("vw_blacklist", client->groups);
querystr = get_client_querystr("vw_blacklist", getstr(client->groupspos));
rc = sqlite3_prepare_v2(gravity_db, querystr, -1, &stmt, NULL);
if( rc != SQLITE_OK )
{
Expand Down Expand Up @@ -497,12 +502,12 @@ static inline void gravityDB_finalize_client_statements(const int clientID)
gravity_stmt->set(gravity_stmt, clientID, NULL);
}

// Free group memory
// Unset group found property to trigger a check next time the
// client sends a query
clientsData* client = getClient(clientID, true);
if(client != NULL && client->groups != NULL)
if(client != NULL)
{
free(client->groups);
client->groups = NULL;
client->found_group = false;
}
}

Expand Down Expand Up @@ -888,13 +893,14 @@ bool gravityDB_get_regex_client_groups(clientsData* client, const int numregex,
gravityDB_check_fork();

char *querystr = NULL;
if(client->groups == NULL && !get_client_groupids(client))
if(!client->found_group && !get_client_groupids(client))
return false;

// Group filtering
if(asprintf(&querystr, "SELECT id from %s WHERE group_id IN (%s);", table, client->groups) < 1)
const char *groups = getstr(client->groupspos);
if(asprintf(&querystr, "SELECT id from %s WHERE group_id IN (%s);", table, groups) < 1)
{
logg("gravityDB_get_regex_client_groups(%s, %s) - asprintf() error", table, client->groups);
logg("gravityDB_get_regex_client_groups(%s, %s) - asprintf() error", table, groups);
return false;
}

Expand Down
19 changes: 19 additions & 0 deletions src/database/network-table.c
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,25 @@ void updateMACVendorRecords(void)

char* __attribute__((malloc)) getDatabaseHostname(const char* ipaddr)
{
// Test if this is an IPv6 address
bool IPv6 = false;
if(ipaddr != NULL && strstr(ipaddr,":") != NULL)
{
IPv6 = true;
}

// Do we want to resolve IPv4/IPv6 names at all?
if( (IPv6 && !config.resolveIPv6) ||
(!IPv6 && !config.resolveIPv4))
{
if(config.debug & DEBUG_RESOLVER)
{
logg(" ---> \"\" (configured to not resolve %s host names)",
IPv6 ? "IPv6" : "IPv4");
}
return strdup("");
}

// Open pihole-FTL.db database file
if(!dbopen())
{
Expand Down
5 changes: 3 additions & 2 deletions src/datastructure.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,9 @@ int findClientID(const char *clientIP, const bool count)
// No query seen so far
client->lastQuery = 0;
client->numQueriesARP = client->count;
// Coonfigured groups are yet unknown
client->groups = NULL;
// Configured groups are yet unknown
client->found_group = false;
client->groupspos = 0u;

// Initialize client-specific overTime data
for(int i = 0; i < OVERTIME_SLOTS; i++)
Expand Down
3 changes: 2 additions & 1 deletion src/datastructure.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ typedef struct {
typedef struct {
unsigned char magic;
bool new;
bool found_group;
int count;
int blockedcount;
int overTime[OVERTIME_SLOTS];
unsigned int numQueriesARP;
char *groups;
size_t groupspos;
size_t ippos;
size_t namepos;
time_t lastQuery;
Expand Down
4 changes: 4 additions & 0 deletions src/dnsmasq/rfc1035.c
Original file line number Diff line number Diff line change
Expand Up @@ -1941,12 +1941,16 @@ size_t answer_request(struct dns_header *header, char *limit, size_t qlen,
if (crecp->flags & F_NXDOMAIN)
nxdomain = 1;
if (!dryrun)
{
log_query(crecp->flags, name, NULL, NULL);
FTL_cache(crecp->flags, name, NULL, NULL, daemon->log_display_id);
}
}
else if (!dryrun)
{
char *target = blockdata_retrieve(crecp->addr.srv.target, crecp->addr.srv.targetlen, NULL);
log_query(crecp->flags, name, NULL, 0);
FTL_cache(crecp->flags, name, NULL, NULL, daemon->log_display_id);

if (add_resource_record(header, limit, &trunc, nameoffset, &ansp,
crec_ttl(crecp, now), NULL, T_SRV, C_IN, "sssd",
Expand Down
Loading

0 comments on commit 16c2f2e

Please sign in to comment.