From fc212c383a67b88f25727357909182ddd92ceb96 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 15 Nov 2022 19:33:47 +0100 Subject: [PATCH 1/2] Always ensure FTL messages are unique Signed-off-by: DL6ER --- src/database/message-table.c | 103 +++++++++++++++++------------------ 1 file changed, 50 insertions(+), 53 deletions(-) diff --git a/src/database/message-table.c b/src/database/message-table.c index 36a23855a..2e6867c67 100644 --- a/src/database/message-table.c +++ b/src/database/message-table.c @@ -150,7 +150,7 @@ bool flush_message_table(void) return true; } -static bool add_message(const enum message_type type, const bool unique, +static bool add_message(const enum message_type type, const char *message, const int count,...) { bool okay = false; @@ -166,55 +166,52 @@ static bool add_message(const enum message_type type, const bool unique, return false; } - // Ensure there are no duplicates when adding host name or rate-limiting messages - if(unique) - { - sqlite3_stmt* stmt = NULL; - const char *querystr = "DELETE FROM message WHERE type = ?1 AND message = ?2"; - int rc = sqlite3_prepare_v2(db, querystr, -1, &stmt, NULL); - if( rc != SQLITE_OK ){ - logg("add_message(type=%u, message=%s) - SQL error prepare DELETE: %s", - type, message, sqlite3_errstr(rc)); - goto end_of_add_message; - } - - // Bind type to prepared statement - if((rc = sqlite3_bind_text(stmt, 1, message_types[type], -1, SQLITE_STATIC)) != SQLITE_OK) - { - logg("add_message(type=%u, message=%s) - Failed to bind type DELETE: %s", - type, message, sqlite3_errstr(rc)); - sqlite3_reset(stmt); - sqlite3_finalize(stmt); - goto end_of_add_message; - } + // Ensure there are no duplicates when adding messages + sqlite3_stmt* stmt = NULL; + const char *querystr = "DELETE FROM message WHERE type = ?1 AND message = ?2"; + int rc = sqlite3_prepare_v2(db, querystr, -1, &stmt, NULL); + if( rc != SQLITE_OK ){ + logg("add_message(type=%u, message=%s) - SQL error prepare DELETE: %s", + type, message, sqlite3_errstr(rc)); + goto end_of_add_message; + } - // Bind message to prepared statement - if((rc = sqlite3_bind_text(stmt, 2, message, -1, SQLITE_STATIC)) != SQLITE_OK) - { - logg("add_message(type=%u, message=%s) - Failed to bind message DELETE: %s", - type, message, sqlite3_errstr(rc)); - sqlite3_reset(stmt); - sqlite3_finalize(stmt); - goto end_of_add_message; - } + // Bind type to prepared statement + if((rc = sqlite3_bind_text(stmt, 1, message_types[type], -1, SQLITE_STATIC)) != SQLITE_OK) + { + logg("add_message(type=%u, message=%s) - Failed to bind type DELETE: %s", + type, message, sqlite3_errstr(rc)); + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + goto end_of_add_message; + } - // Execute and finalize - if((rc = sqlite3_step(stmt)) != SQLITE_OK && rc != SQLITE_DONE) - { - logg("add_message(type=%u, message=%s) - SQL error step DELETE: %s", - type, message, sqlite3_errstr(rc)); - goto end_of_add_message; - } - sqlite3_clear_bindings(stmt); + // Bind message to prepared statement + if((rc = sqlite3_bind_text(stmt, 2, message, -1, SQLITE_STATIC)) != SQLITE_OK) + { + logg("add_message(type=%u, message=%s) - Failed to bind message DELETE: %s", + type, message, sqlite3_errstr(rc)); sqlite3_reset(stmt); sqlite3_finalize(stmt); + goto end_of_add_message; + } + + // Execute and finalize + if((rc = sqlite3_step(stmt)) != SQLITE_OK && rc != SQLITE_DONE) + { + logg("add_message(type=%u, message=%s) - SQL error step DELETE: %s", + type, message, sqlite3_errstr(rc)); + goto end_of_add_message; } + sqlite3_clear_bindings(stmt); + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + stmt = NULL; // Prepare SQLite statement - sqlite3_stmt* stmt = NULL; - const char *querystr = "INSERT INTO message (timestamp,type,message,blob1,blob2,blob3,blob4,blob5) " - "VALUES ((cast(strftime('%s', 'now') as int)),?,?,?,?,?,?,?);"; - int rc = sqlite3_prepare_v2(db, querystr, -1, &stmt, NULL); + querystr = "INSERT INTO message (timestamp,type,message,blob1,blob2,blob3,blob4,blob5) " + "VALUES ((cast(strftime('%s', 'now') as int)),?,?,?,?,?,?,?);"; + rc = sqlite3_prepare_v2(db, querystr, -1, &stmt, NULL); if( rc != SQLITE_OK ) { logg("add_message(type=%u, message=%s) - SQL error prepare: %s", @@ -317,7 +314,7 @@ void logg_regex_warning(const char *type, const char *warning, const int dbindex // Log to database only if not in CLI mode if(!cli_mode) - add_message(REGEX_MESSAGE, false, warning, 3, type, regex, dbindex); + add_message(REGEX_MESSAGE, warning, 3, type, regex, dbindex); } void logg_subnet_warning(const char *ip, const int matching_count, const char *matching_ids, @@ -332,7 +329,7 @@ void logg_subnet_warning(const char *ip, const int matching_count, const char *m // Log to database char *names = get_client_names_from_ids(matching_ids); - add_message(SUBNET_MESSAGE, false, ip, 5, matching_count, names, matching_ids, chosen_match_text, chosen_match_id); + add_message(SUBNET_MESSAGE, ip, 5, matching_count, names, matching_ids, chosen_match_text, chosen_match_id); free(names); } @@ -343,7 +340,7 @@ void logg_hostname_warning(const char *ip, const char *name, const unsigned int ip, name, pos); // Log to database - add_message(HOSTNAME_MESSAGE, true, ip, 2, name, (const int)pos); + add_message(HOSTNAME_MESSAGE, ip, 2, name, (const int)pos); } void logg_fatal_dnsmasq_message(const char *message) @@ -352,7 +349,7 @@ void logg_fatal_dnsmasq_message(const char *message) logg("FATAL ERROR in dnsmasq core: %s", message); // Log to database - add_message(DNSMASQ_CONFIG_MESSAGE, false, message, 0); + add_message(DNSMASQ_CONFIG_MESSAGE, message, 0); // FTL will dies after this point, so we should make sure to clean up // behind ourselves @@ -368,7 +365,7 @@ void logg_rate_limit_message(const char *clientIP, const unsigned int rate_limit clientIP, turnaround, turnaround == 1 ? "" : "s"); // Log to database - add_message(RATE_LIMIT_MESSAGE, true, clientIP, 2, config.rate_limit.count, config.rate_limit.interval); + add_message(RATE_LIMIT_MESSAGE, clientIP, 2, config.rate_limit.count, config.rate_limit.interval); } void logg_warn_dnsmasq_message(char *message) @@ -377,7 +374,7 @@ void logg_warn_dnsmasq_message(char *message) logg("WARNING in dnsmasq core: %s", message); // Log to database - add_message(DNSMASQ_WARN_MESSAGE, false, message, 0); + add_message(DNSMASQ_WARN_MESSAGE, message, 0); } void log_resource_shortage(const double load, const int nprocs, const int shmem, const int disk, const char *path, const char *msg) @@ -385,17 +382,17 @@ void log_resource_shortage(const double load, const int nprocs, const int shmem, if(load > 0.0) { logg("WARNING: Long-term load (15min avg) larger than number of processors: %.1f > %d", load, nprocs); - add_message(LOAD_MESSAGE, true, "excessive load", 2, load, nprocs); + add_message(LOAD_MESSAGE, "excessive load", 2, load, nprocs); } else if(shmem > -1) { logg("WARNING: RAM shortage (%s) ahead: %d%% is used (%s)", path, shmem, msg); - add_message(SHMEM_MESSAGE, true, path, 2, shmem, msg); + add_message(SHMEM_MESSAGE, path, 2, shmem, msg); } else if(disk > -1) { logg("WARNING: Disk shortage (%s) ahead: %d%% is used (%s)", path, disk, msg); - add_message(DISK_MESSAGE, true, path, 2, disk, msg); + add_message(DISK_MESSAGE, path, 2, disk, msg); } } @@ -405,5 +402,5 @@ void logg_inaccessible_adlist(const int dbindex, const char *address) logg("Adlist warning: Adlist with ID %d (%s) was inaccessible during last gravity run", dbindex, address); // Log to database - add_message(INACCESSIBLE_ADLIST_MESSAGE, false, address, 1, dbindex); + add_message(INACCESSIBLE_ADLIST_MESSAGE, address, 1, dbindex); } From af495664f9fc76b4f9dca48a8fc848e01cb356fe Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 15 Nov 2022 21:28:38 +0100 Subject: [PATCH 2/2] Adlist warning -> ADLIST WARNING Signed-off-by: DL6ER --- src/database/message-table.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/database/message-table.c b/src/database/message-table.c index 2e6867c67..ef1b28d5a 100644 --- a/src/database/message-table.c +++ b/src/database/message-table.c @@ -399,7 +399,7 @@ void log_resource_shortage(const double load, const int nprocs, const int shmem, void logg_inaccessible_adlist(const int dbindex, const char *address) { // Log to FTL.log - logg("Adlist warning: Adlist with ID %d (%s) was inaccessible during last gravity run", dbindex, address); + logg("ADLIST WARNING: Adlist with ID %d (%s) was inaccessible during last gravity run", dbindex, address); // Log to database add_message(INACCESSIBLE_ADLIST_MESSAGE, address, 1, dbindex);