1
0
mirror of https://github.com/pi-hole/pi-hole synced 2024-12-22 06:48:07 +00:00

Add getFTLAPIPort function

Add ftl_api_port function

Signed-off-by: Christian König <ckoenig@posteo.de>

Use getFTLAPIPort in pihole

Signed-off-by: Christian König <ckoenig@posteo.de>

Use default portfile as fallback

Signed-off-by: Christian König <ckoenig@posteo.de>

Fix stickler

Signed-off-by: Christian König <ckoenig@posteo.de>

Correct variables

Signed-off-by: Christian König <ckoenig@posteo.de>

Apply suggestions from code review

Co-authored-by: DL6ER <DL6ER@users.noreply.github.com>

Add test getFTLAPIPort returing default port

Signed-off-by: Christian König <ckoenig@posteo.de>

Remove unused code from test_key_val_replacement_works

Signed-off-by: Christian König <ckoenig@posteo.de>

Add getFTLAPIPort_custom test

Signed-off-by: Christian König <ckoenig@posteo.de>

Fix output format

Signed-off-by: Christian König <ckoenig@posteo.de>

Add debugging

Signed-off-by: Christian König <ckoenig@posteo.de>

Remove debugging and fix function

Signed-off-by: Christian König <ckoenig@posteo.de>
This commit is contained in:
Christian König 2022-02-20 22:24:17 +01:00
parent bd956b5f16
commit 42424b515b
3 changed files with 55 additions and 5 deletions

View File

@ -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}"
}

7
pihole
View File

@ -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";;

View File

@ -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