Skip to content

Commit

Permalink
Merge branch 'development-v6' into new/misc_readOnly
Browse files Browse the repository at this point in the history
Signed-off-by: DL6ER <dl6er@dl6er.de>
  • Loading branch information
DL6ER committed Jun 6, 2024
2 parents 3d2fd6d + 61826a4 commit 85fdaff
Show file tree
Hide file tree
Showing 18 changed files with 128 additions and 188 deletions.
4 changes: 2 additions & 2 deletions src/api/auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ int check_client_auth(struct ftl_conn *api, const bool is_api)
// Debug logging
if(config.debug.api.v.b)
{
char timestr[128];
char timestr[TIMESTR_SIZE];
get_timestr(timestr, auth_data[user_id].valid_until, false, false);
log_debug(DEBUG_API, "Recognized known user: user_id %i, valid_until: %s, remote_addr %s (%s at login)",
user_id, timestr, api->request->remote_addr, auth_data[user_id].remote_addr);
Expand Down Expand Up @@ -631,7 +631,7 @@ int api_auth(struct ftl_conn *api)
// Debug logging
if(config.debug.api.v.b && user_id > API_AUTH_UNAUTHORIZED)
{
char timestr[128];
char timestr[TIMESTR_SIZE];
get_timestr(timestr, auth_data[user_id].valid_until, false, false);
log_debug(DEBUG_API, "API: Registered new user: user_id %i valid_until: %s remote_addr %s (accepted due to %s)",
user_id, timestr, auth_data[user_id].remote_addr,
Expand Down
1 change: 0 additions & 1 deletion src/api/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,6 @@ static int api_config_get(struct ftl_conn *api)
// Add config item flags
cJSON *flags = JSON_NEW_OBJECT();
JSON_ADD_BOOL_TO_OBJECT(flags, "restart_dnsmasq", conf_item->f & FLAG_RESTART_FTL);
JSON_ADD_BOOL_TO_OBJECT(flags, "advanced", conf_item->f & FLAG_ADVANCED_SETTING);
JSON_ADD_BOOL_TO_OBJECT(flags, "session_reset", conf_item->f & FLAG_INVALIDATE_SESSIONS);
JSON_ADD_BOOL_TO_OBJECT(flags, "env_var", conf_item->f & FLAG_ENV_VAR);
JSON_ADD_ITEM_TO_OBJECT(leaf, "flags", flags);
Expand Down
2 changes: 1 addition & 1 deletion src/args.c
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ void parse_args(int argc, char* argv[])
// Create a socket
struct sockaddr_in dest;
const int sock = create_socket(tcp, &dest);
char *name = resolveHostname(sock, &dest, tcp, argv[2], true);
char *name = resolveHostname(sock, tcp, &dest, argv[2], true, NULL);

// Close the socket
close(sock);
Expand Down
119 changes: 31 additions & 88 deletions src/config/config.c

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions src/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,12 @@ enum conf_type {
#define MAX_CONFIG_PATH_DEPTH 6

#define FLAG_RESTART_FTL (1 << 0)
#define FLAG_ADVANCED_SETTING (1 << 1)
#define FLAG_PSEUDO_ITEM (1 << 2)
#define FLAG_INVALIDATE_SESSIONS (1 << 3)
#define FLAG_WRITE_ONLY (1 << 4)
#define FLAG_ENV_VAR (1 << 5)
#define FLAG_CONF_IMPORTED (1 << 6)
#define FLAG_READ_ONLY (1 << 7)
#define FLAG_PSEUDO_ITEM (1 << 1)
#define FLAG_INVALIDATE_SESSIONS (1 << 2)
#define FLAG_WRITE_ONLY (1 << 3)
#define FLAG_ENV_VAR (1 << 4)
#define FLAG_CONF_IMPORTED (1 << 5)
#define FLAG_READ_ONLY (1 << 6)

struct conf_item {
const char *k; // item Key
Expand Down
2 changes: 1 addition & 1 deletion src/config/dnsmasq_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ char *get_dnsmasq_line(const unsigned int lineno)
static void write_config_header(FILE *fp, const char *description)
{
const time_t now = time(NULL);
char timestring[TIMESTR_SIZE] = "";
char timestring[TIMESTR_SIZE];
get_timestr(timestring, now, false, false);
fputs("# Pi-hole: A black hole for Internet advertisements\n", fp);
fprintf(fp, "# (c) %u Pi-hole, LLC (https://pi-hole.net)\n", get_year(now));
Expand Down
2 changes: 1 addition & 1 deletion src/config/toml_writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ bool writeFTLtoml(const bool verbose)
fprintf(fp, "# Pi-hole configuration file (%s)\n", get_FTL_version());
fputs("# Encoding: UTF-8\n", fp);
fputs("# This file is managed by pihole-FTL\n", fp);
char timestring[TIMESTR_SIZE] = "";
char timestring[TIMESTR_SIZE];
get_timestr(timestring, time(NULL), false, false);
fputs("# Last updated on ", fp);
fputs(timestring, fp);
Expand Down
10 changes: 5 additions & 5 deletions src/database/message-table.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ static enum message_type get_message_type_from_string(const char *typestr)

static unsigned char message_blob_types[MAX_MESSAGE][5] =
{
{ // REGEX_MESSAGE: The message column contains the regex warning text
{ // REGEX_MESSAGE: The message column contains the regex text (the erroring regex filter itself)
SQLITE_TEXT, // regex type ("deny", "allow")
SQLITE_TEXT, // regex text (the erroring regex filter itself)
SQLITE_TEXT, // regex warning text
SQLITE_INTEGER, // database index of regex (so the dashboard can show a link)
SQLITE_NULL, // not used
SQLITE_NULL // not used
Expand Down Expand Up @@ -993,9 +993,9 @@ bool format_messages(cJSON *array)
{
case REGEX_MESSAGE:
{
const char *warning = (const char*)sqlite3_column_text(stmt, 3);
const char *regex = (const char*)sqlite3_column_text(stmt, 3);
const char *type = (const char*)sqlite3_column_text(stmt, 4);
const char *regex = (const char*)sqlite3_column_text(stmt, 5);
const char *warning = (const char*)sqlite3_column_text(stmt, 5);
const int dbindex = sqlite3_column_int(stmt, 6);

format_regex_message(plain, sizeof(plain), html, sizeof(html),
Expand Down Expand Up @@ -1206,7 +1206,7 @@ void logg_regex_warning(const char *type, const char *warning, const int dbindex
return;

// Add to database
const int rowid = add_message(REGEX_MESSAGE, warning, type, regex, dbindex);
const int rowid = add_message(REGEX_MESSAGE, regex, type, warning, dbindex);
if(rowid == -1)
log_err("logg_regex_warning(): Failed to add message to database");
}
Expand Down
21 changes: 12 additions & 9 deletions src/database/network-table.c
Original file line number Diff line number Diff line change
Expand Up @@ -2009,7 +2009,7 @@ char *__attribute__((malloc)) getNameFromIP(sqlite3 *db, const char *ipaddr)
// Check if we want to resolve host names
if(!resolve_this_name(ipaddr))
{
log_debug(DEBUG_DATABASE, "getNameFromIP(\"%s\") - configured to not resolve host name", ipaddr);
log_debug(DEBUG_RESOLVER, "getNameFromIP(\"%s\") - configured to not resolve host name", ipaddr);
return NULL;
}

Expand Down Expand Up @@ -2056,14 +2056,16 @@ char *__attribute__((malloc)) getNameFromIP(sqlite3 *db, const char *ipaddr)
return NULL;
}

log_debug(DEBUG_RESOLVER, "Check for a host name associated with IP address %s", ipaddr);

char *name = NULL;
rc = sqlite3_step(stmt);
if(rc == SQLITE_ROW)
{
// Database record found (result might be empty)
name = strdup((char*)sqlite3_column_text(stmt, 0));

log_debug(DEBUG_DATABASE, "Found database host name (same address) %s -> %s", ipaddr, name);
log_debug(DEBUG_RESOLVER, "Found database host name (same address) %s -> %s", ipaddr, name);
}
else if(rc != SQLITE_DONE)
{
Expand All @@ -2084,6 +2086,8 @@ char *__attribute__((malloc)) getNameFromIP(sqlite3 *db, const char *ipaddr)
return name;
}

log_debug(DEBUG_RESOLVER, " ---> not found");

// Nothing found for the exact IP address
// Check for a host name associated with the same device (but another IP address)
querystr = "SELECT name FROM network_addresses "
Expand Down Expand Up @@ -2114,6 +2118,8 @@ char *__attribute__((malloc)) getNameFromIP(sqlite3 *db, const char *ipaddr)
return NULL;
}

log_debug(DEBUG_RESOLVER, "Checking for a host name associated with the same device (but another IP address)");

rc = sqlite3_step(stmt);
if(rc == SQLITE_ROW)
{
Expand Down Expand Up @@ -2153,8 +2159,6 @@ char *__attribute__((malloc)) getNameFromMAC(const char *client)
if(FTLDBerror())
return NULL;

log_debug(DEBUG_DATABASE,"Looking up host name for %s", client);

// Open pihole-FTL.db database file
sqlite3 *db = NULL;
if((db = dbopen(false, false)) == NULL)
Expand Down Expand Up @@ -2192,6 +2196,8 @@ char *__attribute__((malloc)) getNameFromMAC(const char *client)
return NULL;
}

log_debug(DEBUG_RESOLVER, "Check for a host name associated with MAC address %s", client);

char *name = NULL;
rc = sqlite3_step(stmt);
if(rc == SQLITE_ROW)
Expand Down Expand Up @@ -2262,11 +2268,8 @@ char *__attribute__((malloc)) getIfaceFromIP(sqlite3 *db, const char *ipaddr)
return NULL;
}

if(config.debug.resolver.v.b)
{
log_debug(DEBUG_RESOLVER, "getIfaceFromIP(): \"%s\" with ? = \"%s\"",
querystr, ipaddr);
}
log_debug(DEBUG_DATABASE, "getIfaceFromIP(): \"%s\" with ? = \"%s\"",
querystr, ipaddr);

// Bind ipaddr to prepared statement
if((rc = sqlite3_bind_text(stmt, 1, ipaddr, -1, SQLITE_STATIC)) != SQLITE_OK)
Expand Down
2 changes: 1 addition & 1 deletion src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ void runGC(const time_t now, time_t *lastGCrun, const bool flush)
if(config.debug.gc.v.b)
{
timer_start(GC_TIMER);
char timestring[TIMESTR_SIZE] = "";
char timestring[TIMESTR_SIZE];
get_timestr(timestring, mintime, false, false);
log_debug(DEBUG_GC, "GC starting, mintime: %s (%lu), counters->queries = %d",
timestring, (unsigned long)mintime, counters->queries);
Expand Down
18 changes: 10 additions & 8 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ double double_time(void)
return tp.tv_sec + 1e-9*tp.tv_nsec;
}

// The size of 84 bytes has been carefully selected for all possible timestamps
// to always fit into the available space without buffer overflows
// Get a human-readable time string
void get_timestr(char timestring[TIMESTR_SIZE], const time_t timein, const bool millis, const bool uri_compatible)
{
struct tm tm;
Expand All @@ -105,16 +104,19 @@ void get_timestr(char timestring[TIMESTR_SIZE], const time_t timein, const bool
gettimeofday(&tv, NULL);
const int millisec = tv.tv_usec/1000;

sprintf(timestring,"%d-%02d-%02d%c%02d%c%02d%c%02d.%03i",
snprintf(timestring, TIMESTR_SIZE, "%d-%02d-%02d%c%02d%c%02d%c%02d.%03i%c%s",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, space,
tm.tm_hour, colon, tm.tm_min, colon, tm.tm_sec, millisec);
tm.tm_hour, colon, tm.tm_min, colon, tm.tm_sec, millisec, space, tm.tm_zone);
}
else
{
sprintf(timestring,"%d-%02d-%02d%c%02d%c%02d%c%02d",
snprintf(timestring, TIMESTR_SIZE, "%d-%02d-%02d%c%02d%c%02d%c%02d%c%s",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, space,
tm.tm_hour, colon, tm.tm_min, colon, tm.tm_sec);
tm.tm_hour, colon, tm.tm_min, colon, tm.tm_sec, space, tm.tm_zone);
}

// Ensure that the string is zero-terminated
timestring[TIMESTR_SIZE - 1] = '\0';
}

// Return the current year
Expand Down Expand Up @@ -227,7 +229,7 @@ const char *debugstr(const enum debug_flag flag)

void __attribute__ ((format (printf, 3, 4))) _FTL_log(const int priority, const enum debug_flag flag, const char *format, ...)
{
char timestring[TIMESTR_SIZE] = "";
char timestring[TIMESTR_SIZE];
va_list args;

// We have been explicitly asked to not print anything to the log
Expand Down Expand Up @@ -324,7 +326,7 @@ void __attribute__ ((format (printf, 3, 4))) _FTL_log(const int priority, const

void __attribute__ ((format (printf, 1, 2))) log_web(const char *format, ...)
{
char timestring[TIMESTR_SIZE] = "";
char timestring[TIMESTR_SIZE];
const time_t now = time(NULL);
va_list args;

Expand Down
2 changes: 1 addition & 1 deletion src/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <stdint.h>

#define DEBUG_ANY 0
#define TIMESTR_SIZE 84
#define TIMESTR_SIZE 128

// Credit: https://stackoverflow.com/a/75116514
#define LEFT(str, w) \
Expand Down
4 changes: 2 additions & 2 deletions src/overTime.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ unsigned int _getOverTimeID(time_t timestamp, const char *file, const int line)
// This is definitely wrong. We warn about this (but only once)
if(!warned_about_hwclock)
{
char timestampStr[TIMESTR_SIZE] = "";
char timestampStr[TIMESTR_SIZE];
get_timestr(timestampStr, timestamp, false, false);

const time_t lastTimestamp = overTime[OVERTIME_SLOTS-1].timestamp;
char lastTimestampStr[TIMESTR_SIZE] = "";
char lastTimestampStr[TIMESTR_SIZE];
get_timestr(lastTimestampStr, lastTimestamp, false, false);

log_warn("Found database entries in the future (%s (%lu), last timestamp for importing: %s (%lu)). "
Expand Down
2 changes: 1 addition & 1 deletion src/regex.c
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ static void read_regex_table(const enum regex_type regexid)
if(!compile_regex(regex_string, &regex[index], &message) && message != NULL)
{
logg_regex_warning(regextype[regexid], message,
regex->database_id, regex_string);
rowid, regex_string);
free(message);
}

Expand Down
Loading

0 comments on commit 85fdaff

Please sign in to comment.