Skip to content

Commit

Permalink
Make utils.sh posix compatible per request
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Warner <me@adamwarner.co.uk>
  • Loading branch information
PromoFaux committed Mar 16, 2022
1 parent 48138d3 commit bedf472
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions advanced/Scripts/utils.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh
# Pi-hole: A black hole for Internet advertisements
# (c) 2017 Pi-hole, LLC (https://pi-hole.net)
# Network-wide ad blocking via your own hardware.
Expand Down Expand Up @@ -27,9 +27,9 @@
# addOrEditKeyValuePair "BLOCKING_ENABLED" "true" "/etc/pihole/setupVars.conf"
#######################
addOrEditKeyValPair() {
local key="${1}"
local value
local file
key="${1}"
value
file

# If two arguments have been passed, then the second one is the file - there is no value
if [ $# -lt 3 ]; then
Expand All @@ -39,7 +39,7 @@ addOrEditKeyValPair() {
file="${3}"
fi

if [[ "${value}" != "" ]]; then
if [ "${value}" != "" ]; then
# value has a value, so it is a key pair
if grep -q "^${key}=" "${file}"; then
# Key already exists in file, modify the value
Expand All @@ -55,6 +55,10 @@ addOrEditKeyValPair() {
echo "${key}" >> "${file}"
fi
fi

unset key
unset value
unset file
}

#######################
Expand All @@ -65,32 +69,35 @@ addOrEditKeyValPair() {
# removeKey "PIHOLE_DNS_1" "/etc/pihole/setupVars.conf"
#######################
removeKey() {
local key="${1}"
local file="${2}"
key="${1}"
file="${2}"
sed -i "/^${key}/d" "${file}"

unset key
unset file
}

#######################
# 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
FTLCONFFILE="/etc/pihole/pihole-FTL.conf"
DEFAULT_PORT_FILE="/run/pihole-FTL.port"
DEFAULT_FTL_PORT=4711
PORTFILE
ftl_api_port

if [[ -f "$FTLCONFFILE" ]]; then
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
if [ -s "$PORTFILE" ]; then
# -s: FILE exists and has a size greater than zero
ftl_api_port=$(<"$PORTFILE")
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
[[ "$ftl_api_port" =~ [^[:digit:]] ]] && unset ftl_api_port
# 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
Expand Down

0 comments on commit bedf472

Please sign in to comment.