Skip to content

Commit

Permalink
Merge pull request #2100 from pi-hole/fix/client_suggestions
Browse files Browse the repository at this point in the history
API /client/_suggestions should only return unconfigured clients
  • Loading branch information
DL6ER authored Oct 27, 2024
2 parents 68e493d + 355ebc7 commit 66c93b8
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/api/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ int api_client_suggestions(struct ftl_conn *api)
"FROM network_addresses na "
"WHERE na.network_id = n.id) "
"FROM network n "
"WHERE n.hwaddr NOT IN (SELECT lower(ip) FROM g.client)" // real hardware addresses
"AND n.hwaddr NOT IN (SELECT CONCAT('ip-',lower(ip)) FROM g.client)" // mock hardware addresses built from IP addresses
"ORDER BY lastQuery DESC LIMIT ?";

if(sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK)
Expand Down Expand Up @@ -439,4 +441,3 @@ int api_client_suggestions(struct ftl_conn *api)
JSON_ADD_ITEM_TO_OBJECT(json, "clients", clients);
JSON_SEND_OBJECT(json);
}

15 changes: 15 additions & 0 deletions src/database/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,21 @@ void db_init(void)
dbversion = db_get_int(db, DB_VERSION);
}

// Update to version 20 if lower
if(dbversion < 20)
{
// Update to version 20: Add additional column for the network table
log_info("Updating long-term database to version 20");
if(!create_network_addresses_network_id_index(db))
{
log_info("Network addresses network_id index cannot be added, database not available");
dbclose(&db);
return;
}
// Get updated version
dbversion = db_get_int(db, DB_VERSION);
}

/* * * * * * * * * * * * * IMPORTANT * * * * * * * * * * * * *
* If you add a new database version, check if the in-memory
* schema needs to be update as well (always recreated from
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 @@ -202,6 +202,25 @@ bool create_network_addresses_with_names_table(sqlite3 *db)
return true;
}

bool create_network_addresses_network_id_index(sqlite3 *db)
{
// Return early if database is known to be broken
if(FTLDBerror())
return false;

// Create index on network_id column in network_addresses table
SQL_bool(db, "CREATE INDEX IF NOT EXISTS network_addresses_network_id_index ON network_addresses (network_id);");

// Update database version to 20
if(!db_set_FTL_property(db, DB_VERSION, 20))
{
log_warn("create_network_addresses_with_names_table(): Failed to update database version!");
return false;
}

return true;
}

// Try to find device by recent usage of this IP address
static int find_device_by_recent_ip(sqlite3 *db, const char *ipaddr)
{
Expand Down
1 change: 1 addition & 0 deletions src/database/network-table.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
bool create_network_table(sqlite3 *db);
bool create_network_addresses_table(sqlite3 *db);
bool create_network_addresses_with_names_table(sqlite3 *db);
bool create_network_addresses_network_id_index(sqlite3 *db);
void parse_neighbor_cache(sqlite3 *db);
bool updateMACVendorRecords(sqlite3 *db);
bool unify_hwaddr(sqlite3 *db);
Expand Down
2 changes: 1 addition & 1 deletion src/database/query-table.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"client TEXT NOT NULL, " \
"forward TEXT );"

#define MEMDB_VERSION 19
#define MEMDB_VERSION 20
#define CREATE_QUERY_STORAGE_TABLE "CREATE TABLE query_storage ( id INTEGER PRIMARY KEY AUTOINCREMENT, " \
"timestamp INTEGER NOT NULL, " \
"type INTEGER NOT NULL, " \
Expand Down
4 changes: 3 additions & 1 deletion test/test_suite.bats
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@
[[ "${lines[@]}" == *"CREATE TABLE IF NOT EXISTS \"network\" (id INTEGER PRIMARY KEY NOT NULL, hwaddr TEXT UNIQUE NOT NULL, interface TEXT NOT NULL, firstSeen INTEGER NOT NULL, lastQuery INTEGER NOT NULL, numQueries INTEGER NOT NULL, macVendor TEXT, aliasclient_id INTEGER);"* ]]
[[ "${lines[@]}" == *"CREATE TABLE IF NOT EXISTS \"network_addresses\" (network_id INTEGER NOT NULL, ip TEXT UNIQUE NOT NULL, lastSeen INTEGER NOT NULL DEFAULT (cast(strftime('%s', 'now') as int)), name TEXT, nameUpdated INTEGER, FOREIGN KEY(network_id) REFERENCES network(id));"* ]]
[[ "${lines[@]}" == *"CREATE TABLE aliasclient (id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, comment TEXT);"* ]]
[[ "${lines[@]}" == *"INSERT INTO ftl VALUES(0,19,'Database version');"* ]]
[[ "${lines[@]}" == *"INSERT INTO ftl VALUES(0,20,'Database version');"* ]]
# vvv This has been added in version 10 vvv
[[ "${lines[@]}" == *"CREATE VIEW queries AS SELECT id, timestamp, type, status, CASE typeof(domain) WHEN 'integer' THEN (SELECT domain FROM domain_by_id d WHERE d.id = q.domain) ELSE domain END domain,CASE typeof(client) WHEN 'integer' THEN (SELECT ip FROM client_by_id c WHERE c.id = q.client) ELSE client END client,CASE typeof(forward) WHEN 'integer' THEN (SELECT forward FROM forward_by_id f WHERE f.id = q.forward) ELSE forward END forward,CASE typeof(additional_info) WHEN 'integer' THEN (SELECT content FROM addinfo_by_id a WHERE a.id = q.additional_info) ELSE additional_info END additional_info, reply_type, reply_time, dnssec, list_id FROM query_storage q;"* ]]
[[ "${lines[@]}" == *"CREATE TABLE domain_by_id (id INTEGER PRIMARY KEY, domain TEXT NOT NULL);"* ]]
Expand All @@ -712,6 +712,8 @@
[[ "${lines[@]}" == *"CREATE UNIQUE INDEX addinfo_by_id_idx ON addinfo_by_id(type,content);"* ]]
# vvv This has been added in version 15 vvv
[[ "${lines[@]}" == *"CREATE TABLE session (id INTEGER PRIMARY KEY, login_at TIMESTAMP NOT NULL, valid_until TIMESTAMP NOT NULL, remote_addr TEXT NOT NULL, user_agent TEXT, sid TEXT NOT NULL, csrf TEXT NOT NULL, tls_login BOOL, tls_mixed BOOL, app BOOL, cli BOOL, x_forwarded_for TEXT);"* ]]
# vvv This has been added in version 20 vvv
[[ "${lines[@]}" == *"CREATE INDEX network_addresses_network_id_index ON network_addresses (network_id);"* ]]
}

@test "Ownership, permissions and type of pihole-FTL.db correct" {
Expand Down

0 comments on commit 66c93b8

Please sign in to comment.