Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/pi-hole/pi-hole.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian König <ckoenig@posteo.de>2022-07-26 15:38:03 +0300
committerChristian König <ckoenig@posteo.de>2022-07-26 15:38:03 +0300
commit7b77d991df712722ae48568e325c5a0bc3df1c65 (patch)
tree188561a37c989f948cb3630de021086280fc2710 /advanced
parentf59749b1c3aa3554b299099588f240cb6d44365e (diff)
Move FTL port and PID functions to utils.sh
Signed-off-by: Christian König <ckoenig@posteo.de>
Diffstat (limited to 'advanced')
-rwxr-xr-xadvanced/Scripts/utils.sh95
-rw-r--r--advanced/Templates/pihole-FTL.service52
2 files changed, 84 insertions, 63 deletions
diff --git a/advanced/Scripts/utils.sh b/advanced/Scripts/utils.sh
index cf24c098..a9e05692 100755
--- a/advanced/Scripts/utils.sh
+++ b/advanced/Scripts/utils.sh
@@ -71,28 +71,87 @@ removeKey() {
}
#######################
-# returns FTL's current telnet API port
+# returns path of FTL's port file
+#######################
+getFTLAPIPortFile() {
+ local FTLCONFFILE="/etc/pihole/pihole-FTL.conf"
+ local DEFAULT_PORT_FILE="/run/pihole-FTL.port"
+ local FTL_APIPORT_FILE
+
+ if [ -s "${FTLCONFFILE}" ]; then
+ # if PORTFILE is not set in pihole-FTL.conf, use the default path
+ FTL_APIPORT_FILE="$({ grep '^PORTFILE=' "${FTLCONFFILE}" || echo "${DEFAULT_PORT_FILE}"; } | cut -d'=' -f2-)"
+ else
+ # if there is no pihole-FTL.conf, use the default path
+ FTL_APIPORT_FILE="${DEFAULT_PORT_FILE}"
+ fi
+
+ echo "${FTL_APIPORT_FILE}"
+}
+
+
+#######################
+# returns FTL's current telnet API port based on the content of the pihole-FTL.port file
+#
+# Takes one argument: path to pihole-FTL.port
+# Example getFTLAPIPort "/run/pihole-FTL.port"
#######################
getFTLAPIPort(){
+ local PORTFILE="${1}"
+ local DEFAULT_FTL_PORT=4711
+ local ftl_api_port
+
+ if [ -s "$PORTFILE" ]; then
+ # -s: FILE exists and has a size greater than zero
+ ftl_api_port=$(cat "${PORTFILE}")
+ # Exploit prevention: unset the variable if there is malicious content
+ # Verify that the value read from the file is numeric
+ expr "$ftl_api_port" : "[^[:digit:]]" > /dev/null && 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}"
+}
+
+#######################
+# returns path of FTL's PID file
+#######################
+getFTLPIDFile() {
local FTLCONFFILE="/etc/pihole/pihole-FTL.conf"
- local DEFAULT_PORT_FILE="/run/pihole-FTL.port"
- local 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
+ local DEFAULT_PID_FILE="/run/pihole-FTL.pid"
+ local FTL_PID_FILE
- if [ -s "$PORTFILE" ]; then
- # -s: FILE exists and has a size greater than zero
- ftl_api_port=$(cat "${PORTFILE}")
- # Exploit prevention: unset the variable if there is malicious content
- # Verify that the value read from the file is numeric
- expr "$ftl_api_port" : "[^[:digit:]]" > /dev/null && unset ftl_api_port
+ if [ -s "${FTLCONFFILE}" ]; then
+ # if PIDFILE is not set in pihole-FTL.conf, use the default path
+ FTL_PID_FILE="$({ grep '^PIDFILE=' "${FTLCONFFILE}" || echo "${DEFAULT_PID_FILE}"; } | cut -d'=' -f2-)"
+ else
+ # if there is no pihole-FTL.conf, use the default path
+ FTL_PID_FILE="${DEFAULT_PID_FILE}"
fi
- # echo the port found in the portfile or default to the default port
- echo "${ftl_api_port:=$DEFAULT_FTL_PORT}"
+ echo "${FTL_PID_FILE}"
+}
+
+#######################
+# returns FTL's PID based on the content of the pihole-FTL.pid file
+#
+# Takes one argument: path to pihole-FTL.pid
+# Example getFTLPID "/run/pihole-FTL.pid"
+#######################
+getFTLPID() {
+ local FTL_PID_FILE="${1}"
+ local FTL_PID
+
+ if [ -s "${FTL_PID_FILE}" ]; then
+ # -s: FILE exists and has a size greater than zero
+ FTL_PID="$(cat "${FTL_PID_FILE}")"
+ # Exploit prevention: unset the variable if there is malicious content
+ # Verify that the value read from the file is numeric
+ expr "${FTL_PID}" : "[^[:digit:]]" > /dev/null && unset FTL_PID
+ fi
+
+ # If FTL is not running, or the PID file contains malicious stuff, substitute
+ # negative PID to signal this
+ FTL_PID=${FTL_PID:=-1}
+ echo "${FTL_PID}"
}
diff --git a/advanced/Templates/pihole-FTL.service b/advanced/Templates/pihole-FTL.service
index f5abfcea..7346dc20 100644
--- a/advanced/Templates/pihole-FTL.service
+++ b/advanced/Templates/pihole-FTL.service
@@ -9,48 +9,10 @@
# Description: Enable service provided by pihole-FTL daemon
### END INIT INFO
-# Global variables
-FTLCONFFILE="/etc/pihole/pihole-FTL.conf"
-DEFAULT_PID_FILE="/run/pihole-FTL.pid"
-DEFAULT_PORT_FILE="/run/pihole-FTL.port"
-FTL_PID=''
-
-# Get the file path of the pihole-FTL.pid file
-getFTLPIDFile() {
- if [ -s "${FTLCONFFILE}" ]; then
- # if PIDFILE is not set in pihole-FTL.conf, use the default path
- FTL_PID_FILE="$({ grep '^PIDFILE=' "${FTLCONFFILE}" || echo "${DEFAULT_PID_FILE}"; } | cut -d'=' -f2-)"
- else
- # if there is no pihole-FTL.conf, use the default path
- FTL_PID_FILE="${DEFAULT_PID_FILE}"
- fi
-}
-
-# Get the PID of the FTL process based on the content of the pihole-FTL.pid file
-getFTLPID() {
- if [ -s "${FTL_PID_FILE}" ]; then
- # -s: FILE exists and has a size greater than zero
- FTL_PID="$(cat "${FTL_PID_FILE}")"
- # Exploit prevention: unset the variable if there is malicious content
- # Verify that the value read from the file is numeric
- expr "${FTL_PID}" : "[^[:digit:]]" > /dev/null && unset FTL_PID
- fi
-
- # If FTL is not running, or the PID file contains malicious stuff, substitute
- # negative PID to signal this
- FTL_PID=${FTL_PID:=-1}
-}
-
-# Get the file path of the pihole-FTL.port file
-getFTLPortFile() {
- if [ -s "${FTLCONFFILE}" ]; then
- # if PORTFILE is not set in pihole-FTL.conf, use the default path
- FTL_PORT_FILE="$({ grep '^PORTFILE=' "${FTLCONFFILE}" || echo "${DEFAULT_PORT_FILE}"; } | cut -d'=' -f2-)"
- else
- # if there is no pihole-FTL.conf, use the default path
- FTL_PORT_FILE="${DEFAULT_PORT_FILE}"
-fi
-}
+#source utils.sh for getFTLPIDFile(), getFTLPID (), getFTLAPIPortFile()
+PI_HOLE_SCRIPT_DIR="/opt/pihole"
+utilsfile="${PI_HOLE_SCRIPT_DIR}/utils.sh"
+. "${utilsfile}"
is_running() {
@@ -148,11 +110,11 @@ status() {
### main logic ###
# Get file paths
-getFTLPIDFile
-getFTLPortFile
+FTL_PID_FILE="$(getFTLPIDFile)"
+FTL_PORT_FILE="$(getFTLAPIPortFile)"
# Get FTL's current PID
-getFTLPID
+FTL_PID="$(getFTLPID ${FTL_PID_FILE})"
case "$1" in
stop)