From fc9bc6ed13980a6921ed9ba572176f389107898d Mon Sep 17 00:00:00 2001 From: Iain Henderson Date: Sat, 22 Feb 2025 13:42:53 -0500 Subject: [PATCH 1/3] Update config/env.c to accept conventional environment variables in addition to exact matches of configuration names Signed-off-by: Iain Henderson --- src/config/env.c | 19 ++++++++++++++++++- test/run.sh | 1 + test/test_suite.bats | 7 +++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/config/env.c b/src/config/env.c index b22e05803..43496309b 100644 --- a/src/config/env.c +++ b/src/config/env.c @@ -147,15 +147,32 @@ void printFTLenv(void) static struct env_item *__attribute__((pure)) getFTLenv(const char *key) { + // Normalize the environment variable to conventional names, + // i.e. uppercase letters, digits, and the ( '_' ) + // Exact case matches will still work. + // See: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html + char *env_key = strdup(key); + int env_length = strlen(env_key); + for (int i = 0; i < env_length; i++) + { + if (env_key[i] == '.') + env_key[i] = '_'; // keys are pre-processed, but just in case + else + env_key[i] = toupper(env_key[i]); + } // Iterate over all known FTLCONF environment variables for(struct env_item *item = env_list; item != NULL; item = item->next) { // Check if this is the requested key - if(strcmp(item->key, key) == 0) + if(strcmp(item->key, key) == 0 || strcmp(item->key, env_key) == 0) + { + free(env_key); return item; + } } // Return NULL if the key was not found + free(env_key); return NULL; } diff --git a/test/run.sh b/test/run.sh index 100b82a94..8c5810bae 100755 --- a/test/run.sh +++ b/test/run.sh @@ -77,6 +77,7 @@ umask 0022 export FTLCONF_misc_nice="-11" export FTLCONF_dns_upstrrr="-11" export FTLCONF_debug_api="not_a_bool" +export FTLCONF_MISC_CHECK_SHMEM=91 # Prepare gdb session echo "handle SIGHUP nostop SIGPIPE nostop SIGTERM nostop SIG32 nostop SIG33 nostop SIG34 nostop SIG35 nostop SIG41 nostop" > /root/.gdbinit diff --git a/test/test_suite.bats b/test/test_suite.bats index 1891abde8..e739f6370 100644 --- a/test/test_suite.bats +++ b/test/test_suite.bats @@ -1622,6 +1622,13 @@ [[ ${lines[0]} == " nice = -11 ### CHANGED (env), default = -10" ]] } +@test "Capitalized Environmental variable is used and favored over config file" { + # The config file has 90 but we set FTLCONF_MISC_CHECK_SHMEM="91" + run bash -c 'grep "shmem = 91" /etc/pihole/pihole.toml' + printf "%s\n" "${lines[@]}" + [[ ${lines[0]} == " shmem = 91 ### CHANGED (env), default = 90" ]] +} + @test "Correct number of environmental variables is logged" { run bash -c 'grep -q "3 FTLCONF environment variables found (1 used, 1 invalid, 1 ignored)" /var/log/pihole/FTL.log' printf "%s\n" "${lines[@]}" From a2c1cfa787e17dab9f10cf2d226bd4a3f0a27b0d Mon Sep 17 00:00:00 2001 From: Iain Henderson Date: Sat, 22 Feb 2025 13:42:53 -0500 Subject: [PATCH 2/3] Update config/env.c to accept conventional environment variables in addition to exact matches of configuration names Signed-off-by: Iain Henderson --- src/config/env.c | 19 ++++++++++++++++++- test/pihole.toml | 5 +++-- test/run.sh | 1 + test/test_suite.bats | 9 ++++++++- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/config/env.c b/src/config/env.c index b22e05803..43496309b 100644 --- a/src/config/env.c +++ b/src/config/env.c @@ -147,15 +147,32 @@ void printFTLenv(void) static struct env_item *__attribute__((pure)) getFTLenv(const char *key) { + // Normalize the environment variable to conventional names, + // i.e. uppercase letters, digits, and the ( '_' ) + // Exact case matches will still work. + // See: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html + char *env_key = strdup(key); + int env_length = strlen(env_key); + for (int i = 0; i < env_length; i++) + { + if (env_key[i] == '.') + env_key[i] = '_'; // keys are pre-processed, but just in case + else + env_key[i] = toupper(env_key[i]); + } // Iterate over all known FTLCONF environment variables for(struct env_item *item = env_list; item != NULL; item = item->next) { // Check if this is the requested key - if(strcmp(item->key, key) == 0) + if(strcmp(item->key, key) == 0 || strcmp(item->key, env_key) == 0) + { + free(env_key); return item; + } } // Return NULL if the key was not found + free(env_key); return NULL; } diff --git a/test/pihole.toml b/test/pihole.toml index e40e2a800..313ec2a47 100644 --- a/test/pihole.toml +++ b/test/pihole.toml @@ -1011,7 +1011,7 @@ # By default, FTL warns if the shared-memory usage exceeds 90%. You can set any # integer limit between 0 to 100 (interpreted as percentages) where 0 means that # checking of shared-memory usage is disabled. - shmem = 0 ### CHANGED, default = 90 + shmem = 91 ### CHANGED (env), default = 90 # FTL stores its long-term history in a database file on disk. Furthermore, FTL stores # log files. By default, FTL warns if usage of the disk holding any crucial file @@ -1147,6 +1147,7 @@ # Configuration statistics: # 152 total entries out of which 95 entries are default # --> 57 entries are modified -# 2 entries are forced through environment: +# 3 entries are forced through environment: # - misc.nice +# - misc.check.shmem # - debug.api diff --git a/test/run.sh b/test/run.sh index 100b82a94..8c5810bae 100755 --- a/test/run.sh +++ b/test/run.sh @@ -77,6 +77,7 @@ umask 0022 export FTLCONF_misc_nice="-11" export FTLCONF_dns_upstrrr="-11" export FTLCONF_debug_api="not_a_bool" +export FTLCONF_MISC_CHECK_SHMEM=91 # Prepare gdb session echo "handle SIGHUP nostop SIGPIPE nostop SIGTERM nostop SIG32 nostop SIG33 nostop SIG34 nostop SIG35 nostop SIG41 nostop" > /root/.gdbinit diff --git a/test/test_suite.bats b/test/test_suite.bats index 1891abde8..4e4332f9f 100644 --- a/test/test_suite.bats +++ b/test/test_suite.bats @@ -1622,8 +1622,15 @@ [[ ${lines[0]} == " nice = -11 ### CHANGED (env), default = -10" ]] } +@test "Capitalized Environmental variable is used and favored over config file" { + # The config file has 90 but we set FTLCONF_MISC_CHECK_SHMEM="91" + run bash -c 'grep "shmem = 91" /etc/pihole/pihole.toml' + printf "%s\n" "${lines[@]}" + [[ ${lines[0]} == " shmem = 91 ### CHANGED (env), default = 90" ]] +} + @test "Correct number of environmental variables is logged" { - run bash -c 'grep -q "3 FTLCONF environment variables found (1 used, 1 invalid, 1 ignored)" /var/log/pihole/FTL.log' + run bash -c 'grep -q "4 FTLCONF environment variables found (2 used, 1 invalid, 1 ignored)" /var/log/pihole/FTL.log' printf "%s\n" "${lines[@]}" [[ $status == 0 ]] } From c6d0b800f307162ed26b677bd892c12c009d49f9 Mon Sep 17 00:00:00 2001 From: Iain Henderson Date: Sun, 23 Feb 2025 17:47:41 -0500 Subject: [PATCH 3/3] Simplify checking configurations against environment variables in config/env.c Signed-off-by: Iain Henderson --- src/config/env.c | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/src/config/env.c b/src/config/env.c index 43496309b..043b1f315 100644 --- a/src/config/env.c +++ b/src/config/env.c @@ -147,32 +147,17 @@ void printFTLenv(void) static struct env_item *__attribute__((pure)) getFTLenv(const char *key) { - // Normalize the environment variable to conventional names, - // i.e. uppercase letters, digits, and the ( '_' ) - // Exact case matches will still work. + // "Normalize" the environment variable to conventional names by using a case insensitive comparison, // See: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html - char *env_key = strdup(key); - int env_length = strlen(env_key); - for (int i = 0; i < env_length; i++) - { - if (env_key[i] == '.') - env_key[i] = '_'; // keys are pre-processed, but just in case - else - env_key[i] = toupper(env_key[i]); - } // Iterate over all known FTLCONF environment variables for(struct env_item *item = env_list; item != NULL; item = item->next) { // Check if this is the requested key - if(strcmp(item->key, key) == 0 || strcmp(item->key, env_key) == 0) - { - free(env_key); + if(strcasecmp(item->key, key) == 0) return item; - } } // Return NULL if the key was not found - free(env_key); return NULL; }