From 42424b515ba174fc60309f8363a193c91b7b444d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6nig?= Date: Sun, 20 Feb 2022 22:24:17 +0100 Subject: [PATCH] Add getFTLAPIPort function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add ftl_api_port function Signed-off-by: Christian König Use getFTLAPIPort in pihole Signed-off-by: Christian König Use default portfile as fallback Signed-off-by: Christian König Fix stickler Signed-off-by: Christian König Correct variables Signed-off-by: Christian König Apply suggestions from code review Co-authored-by: DL6ER Add test getFTLAPIPort returing default port Signed-off-by: Christian König Remove unused code from test_key_val_replacement_works Signed-off-by: Christian König Add getFTLAPIPort_custom test Signed-off-by: Christian König Fix output format Signed-off-by: Christian König Add debugging Signed-off-by: Christian König Remove debugging and fix function Signed-off-by: Christian König --- advanced/Scripts/utils.sh | 27 +++++++++++++++++++++++++++ pihole | 7 ++++--- test/test_any_utils.py | 26 ++++++++++++++++++++++++-- 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/advanced/Scripts/utils.sh b/advanced/Scripts/utils.sh index 887816cc..97dca952 100755 --- a/advanced/Scripts/utils.sh +++ b/advanced/Scripts/utils.sh @@ -33,3 +33,30 @@ addOrEditKeyValPair() { echo "${key}=${value}" >> "${file}" fi } + +####################### +# returns FTL's current telnet API port +####################### +getFTLAPIPort(){ + local -r FTLCONFFILE="/etc/pihole/pihole-FTL.conf" + local -r DEFAULT_PORT_FILE="/run/pihole-FTL.port" + local -r DEFAULT_FTL_PORT=4711 + local PORTFILE + local ftl_api_port + + if [[ -f "$FTLCONFFILE" ]]; then + # if PORTFILE is not set in pihole-FTL.conf, use the default path + PORTFILE="$( (grep "^PORTFILE=" $FTLCONFFILE || echo "$DEFAULT_PORT_FILE") | cut -d"=" -f2-)" + fi + + if [[ -s "$PORTFILE" ]]; then + # -s: FILE exists and has a size greater than zero + ftl_api_port=$(<"$PORTFILE") + # Exploit prevention: unset the variable if there is malicious content + # Verify that the value read from the file is numeric + [[ "$ftl_api_port" =~ [^[:digit:]] ]] && unset ftl_api_port + fi + + # echo the port found in the portfile or default to the default port + echo "${ftl_api_port:=$DEFAULT_FTL_PORT}" +} diff --git a/pihole b/pihole index 56d47eca..d73fd5aa 100755 --- a/pihole +++ b/pihole @@ -316,9 +316,10 @@ analyze_ports() { statusFunc() { # Determine if there is pihole-FTL service is listening - local listening pid port + local pid port ftl_api_port pid="$(getFTLPID)" + ftl_api_port="$(getFTLAPIPort)" if [[ "$pid" -eq "-1" ]]; then case "${1}" in "web") echo "-1";; @@ -326,8 +327,8 @@ statusFunc() { esac return 0 else - #get the port pihole-FTL is listening on by using FTL's telnet API - port="$(echo ">dns-port >quit" | nc 127.0.0.1 4711)" + #get the DNS port pihole-FTL is listening on by using FTL's telnet API + port="$(echo ">dns-port >quit" | nc 127.0.0.1 "$ftl_api_port")" if [[ "${port}" == "0" ]]; then case "${1}" in "web") echo "-1";; diff --git a/test/test_any_utils.py b/test/test_any_utils.py index ba9b2d23..8ad27997 100644 --- a/test/test_any_utils.py +++ b/test/test_any_utils.py @@ -1,16 +1,38 @@ def test_key_val_replacement_works(host): ''' Confirms addOrEditKeyValPair provides the expected output ''' host.run(''' - setupvars=./testoutput source /opt/pihole/utils.sh addOrEditKeyValPair "KEY_ONE" "value1" "./testoutput" addOrEditKeyValPair "KEY_TWO" "value2" "./testoutput" addOrEditKeyValPair "KEY_ONE" "value3" "./testoutput" addOrEditKeyValPair "KEY_FOUR" "value4" "./testoutput" - cat ./testoutput ''') output = host.run(''' cat ./testoutput ''') expected_stdout = 'KEY_ONE=value3\nKEY_TWO=value2\nKEY_FOUR=value4\n' assert expected_stdout == output.stdout + + +def test_getFTLAPIPort_default(host): + ''' Confirms getFTLAPIPort returns the default API port ''' + output = host.run(''' + source /opt/pihole/utils.sh + getFTLAPIPort + ''') + expected_stdout = '4711\n' + assert expected_stdout == output.stdout + + +def test_getFTLAPIPort_custom(host): + ''' Confirms getFTLAPIPort returns a custom API port in a custom PORTFILE location ''' + host.run(''' + echo "PORTFILE=/tmp/port.file" > /etc/pihole/pihole-FTL.conf + echo "1234" > /tmp/port.file + ''') + output = host.run(''' + source /opt/pihole/utils.sh + getFTLAPIPort + ''') + expected_stdout = '1234\n' + assert expected_stdout == output.stdout