Skip to content

Commit

Permalink
Translate anti-/gravity list IDs to negative numbers so they can be d…
Browse files Browse the repository at this point in the history
…istinguish from domains rather easily. Users are free to foil this method when they force negative IDs into the database but they will never be automatically created

Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Dec 25, 2023
1 parent 1bc8e7f commit 5f59125
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/database/gravity-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ bool gravityDB_prepare_client_statements(clientsData *client)

// Prepare gravity statement
log_debug(DEBUG_DATABASE, "gravityDB_open(): Preparing vw_gravity statement for client %s", clientip);
querystr = get_client_querystr("vw_gravity", "domain", getstr(client->groupspos));
querystr = get_client_querystr("vw_gravity", "adlist_id", getstr(client->groupspos));
rc = sqlite3_prepare_v3(gravity_db, querystr, -1, SQLITE_PREPARE_PERSISTENT, &stmt, NULL);
if( rc != SQLITE_OK )
{
Expand All @@ -870,7 +870,7 @@ bool gravityDB_prepare_client_statements(clientsData *client)

// Prepare antigravity statement
log_debug(DEBUG_DATABASE, "gravityDB_open(): Preparing vw_antigravity statement for client %s", clientip);
querystr = get_client_querystr("vw_antigravity", "domain", getstr(client->groupspos));
querystr = get_client_querystr("vw_antigravity", "adlist_id", getstr(client->groupspos));
rc = sqlite3_prepare_v3(gravity_db, querystr, -1, SQLITE_PREPARE_PERSISTENT, &stmt, NULL);
if( rc != SQLITE_OK )
{
Expand Down
4 changes: 2 additions & 2 deletions src/database/query-table.c
Original file line number Diff line number Diff line change
Expand Up @@ -1508,7 +1508,7 @@ bool queries_to_database(void)
break;
}
}
else if(cache != NULL && cache->list_id > -1)
else if(cache != NULL && cache->list_id != -1)
{
// Restore regex ID if applicable
sqlite3_bind_int(query_stmt, 9, ADDINFO_LIST_ID);
Expand Down Expand Up @@ -1548,7 +1548,7 @@ bool queries_to_database(void)
sqlite3_bind_int(query_stmt, 13, query->dnssec);

// LIST_ID
if(cache != NULL && cache->list_id > -1)
if(cache != NULL && cache->list_id != -1)
sqlite3_bind_int(query_stmt, 14, cache->list_id);
else
// Not applicable, setting NULL
Expand Down
10 changes: 8 additions & 2 deletions src/dnsmasq_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,12 @@ static bool check_domain_blocked(const char *domain, const int clientID,
log_debug(DEBUG_QUERIES, "Allowing query due to antigravity match (list ID %i)", list_id);

// Store ID of the matching antigravity list
dns_cache->list_id = list_id;
// positive values (incl. 0) are used for domainlists
// -1 means "not set"
// -2 is gravity list 0
// -3 is gravity list 1
// ...
dns_cache->list_id = -1 * (list_id + 2);

return false;
}
Expand All @@ -1165,7 +1170,8 @@ static bool check_domain_blocked(const char *domain, const int clientID,
log_debug(DEBUG_QUERIES, "Blocking query due to gravity match (list ID %i)", list_id);

// Store ID of the matching gravity list
dns_cache->list_id = list_id;
// see remarks above for the list_id values
dns_cache->list_id = -1 * (list_id + 2);

// We block this domain
return true;
Expand Down
4 changes: 2 additions & 2 deletions test/gravity.db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,14 @@ CREATE VIEW vw_regex_blacklist AS SELECT domain, domainlist.id AS id, domainlist
AND domainlist.type = 3
ORDER BY domainlist.id;

CREATE VIEW vw_gravity AS SELECT domain, adlist_by_group.group_id AS group_id
CREATE VIEW vw_gravity AS SELECT domain, adlist.id AS adlist_id, adlist_by_group.group_id AS group_id
FROM gravity
LEFT JOIN adlist_by_group ON adlist_by_group.adlist_id = gravity.adlist_id
LEFT JOIN adlist ON adlist.id = gravity.adlist_id
LEFT JOIN "group" ON "group".id = adlist_by_group.group_id
WHERE adlist.enabled = 1 AND (adlist_by_group.group_id IS NULL OR "group".enabled = 1) AND adlist.type = 0;

CREATE VIEW vw_antigravity AS SELECT domain, adlist_by_group.group_id AS group_id
CREATE VIEW vw_antigravity AS SELECT domain, adlist.id AS adlist_id, adlist_by_group.group_id AS group_id
FROM antigravity
LEFT JOIN adlist_by_group ON adlist_by_group.adlist_id = antigravity.adlist_id
LEFT JOIN adlist ON adlist.id = antigravity.adlist_id
Expand Down

0 comments on commit 5f59125

Please sign in to comment.