Compare commits

..

21 Commits

Author SHA1 Message Date
yubiuser
9356d7bbb1 Remove unnecessary case in uninstall script (#4692)
* Remove unnecessary case in uninstall script

* Better answer

Signed-off-by: Christian König <ckoenig@posteo.de>
2022-04-12 11:36:49 -07:00
Adam Warner
29a867d5ae Merge pull request #4690 from pi-hole/utils-sourcing
Prevent issues cause by declaring `utilsfile` as `readonly`
2022-04-12 19:17:37 +01:00
Adam Warner
86dd612882 remove readonly directive from declaration of utilsfile, it is unnecassery
Signed-off-by: Adam Warner <me@adamwarner.co.uk>
2022-04-12 17:13:50 +01:00
yubiuser
42d3368955 Merge pull request #4663 from pi-hole/no_root
Only use sudo power if needed
2022-04-10 12:34:05 +02:00
yubiuser
21ae81ffdb Merge pull request #4682 from pi-hole/loop
Exit installer if dpkg lock is held for more then 30 seconds
2022-04-07 15:26:27 +02:00
yubiuser
b33434d02a Let uses know how long they need to wait (max)
Co-authored-by: Adam Warner <me@adamwarner.co.uk>
2022-04-07 09:11:53 +02:00
yubiuser
d3e94cbceb Merge pull request #4653 from pi-hole/refactor-utils-redux
Tweaks to functions in utils.sh and refactored usages in webpage.sh
2022-04-04 23:25:54 +02:00
yubiuser
9b4f6c84cd Minor review comments 2022-04-04 23:14:10 +02:00
Adam Warner
4d31d5ee11 Address review comments
Signed-off-by: Adam Warner <me@adamwarner.co.uk>
2022-04-04 22:02:26 +01:00
Adam Warner
9878477896 Merge pull request #4681 from pi-hole/adlist
Rename block lists to adlists within the query list functions
2022-04-04 16:42:43 +01:00
Christian König
c0a2ab7b77 Fix indention
Signed-off-by: Christian König <ckoenig@posteo.de>
2022-04-04 16:59:13 +02:00
Christian König
30ba79f6a0 Let users know what's going on
Signed-off-by: Christian König <ckoenig@posteo.de>
2022-04-04 16:48:34 +02:00
Christian König
cd3c97f113 Exit installer if dpkg lock is held for more then 30 seconds
Signed-off-by: Christian König <ckoenig@posteo.de>
2022-04-04 16:38:30 +02:00
Christian König
e4a1f3a175 Rename block lists to adlists within the query list functions
Signed-off-by: Christian König <ckoenig@posteo.de>
2022-04-04 13:52:26 +02:00
yubiuser
6121c162ff Fix typo
Co-authored-by: Marius Hanl <66004280+Maran23@users.noreply.github.com>
2022-04-03 13:49:43 +02:00
Adam Warner
7fa8cdd03e Address:
- Review Comments
 - Stickler Complaints
2022-04-02 12:46:07 +01:00
Christian König
722a716de3 Add exit code to status function
Signed-off-by: Christian König <ckoenig@posteo.de>
2022-03-30 22:40:14 +02:00
Christian König
614d18cd3d Debug need sudo
Signed-off-by: Christian König <ckoenig@posteo.de>
2022-03-30 21:24:51 +02:00
Christian König
54ce8c2622 Only use sudo power if needed
Signed-off-by: Christian König <ckoenig@posteo.de>
2022-03-30 21:04:36 +02:00
Adam Warner
59fc3804be Make utils.sh posix compatible per request
Signed-off-by: Adam Warner <me@adamwarner.co.uk>
2022-03-16 20:40:25 +00:00
Adam Warner
48138d32b6 Adjust addOrEditKeyValPair to optionally take two or three arguments (adjust test to suit)
Add a removeKey function with test

update webpage.sh to reference functions in utils.sh (this can likely be abstracted/refactored further)

Signed-off-by: Adam Warner <me@adamwarner.co.uk>
2022-03-16 17:42:01 +00:00
8 changed files with 161 additions and 115 deletions

View File

@@ -64,8 +64,8 @@ Example: 'pihole -q -exact domain.com'
Query the adlists for a specified domain Query the adlists for a specified domain
Options: Options:
-exact Search the block lists for exact domain matches -exact Search the adlists for exact domain matches
-all Return all query matches within a block list -all Return all query matches within the adlists
-h, --help Show this help dialog" -h, --help Show this help dialog"
exit 0 exit 0
fi fi
@@ -210,7 +210,7 @@ mapfile -t results <<< "$(scanDatabaseTable "${domainQuery}" "gravity")"
# Handle notices # Handle notices
if [[ -z "${wbMatch:-}" ]] && [[ -z "${wcMatch:-}" ]] && [[ -z "${results[*]}" ]]; then if [[ -z "${wbMatch:-}" ]] && [[ -z "${wcMatch:-}" ]] && [[ -z "${results[*]}" ]]; then
echo -e " ${INFO} No ${exact/t/t }results found for ${COL_BOLD}${domainQuery}${COL_NC} within the block lists" echo -e " ${INFO} No ${exact/t/t }results found for ${COL_BOLD}${domainQuery}${COL_NC} within the adlists"
exit 0 exit 0
elif [[ -z "${results[*]}" ]]; then elif [[ -z "${results[*]}" ]]; then
# Result found in WL/BL/Wildcards # Result found in WL/BL/Wildcards

View File

@@ -1,4 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env sh
# shellcheck disable=SC3043 #https://github.com/koalaman/shellcheck/wiki/SC3043#exceptions
# Pi-hole: A black hole for Internet advertisements # Pi-hole: A black hole for Internet advertisements
# (c) 2017 Pi-hole, LLC (https://pi-hole.net) # (c) 2017 Pi-hole, LLC (https://pi-hole.net)
# Network-wide ad blocking via your own hardware. # Network-wide ad blocking via your own hardware.
@@ -15,46 +17,74 @@
# - New functions must have a test added for them in test/test_any_utils.py # - New functions must have a test added for them in test/test_any_utils.py
####################### #######################
# Takes three arguments key, value, and file. # Takes either
# - Three arguments: file, key, and value.
# - Two arguments: file, and key.
#
# Checks the target file for the existence of the key # Checks the target file for the existence of the key
# - If it exists, it changes the value # - If it exists, it changes the value
# - If it does not exist, it adds the value # - If it does not exist, it adds the value
# #
# Example usage: # Example usage:
# addOrEditKeyValuePair "BLOCKING_ENABLED" "true" "/etc/pihole/setupVars.conf" # addOrEditKeyValuePair "/etc/pihole/setupVars.conf" "BLOCKING_ENABLED" "true"
####################### #######################
addOrEditKeyValPair() { addOrEditKeyValPair() {
local key="${1}" local file="${1}"
local value="${2}" local key="${2}"
local file="${3}" local value="${3}"
if grep -q "^${key}=" "${file}"; then
sed -i "/^${key}=/c\\${key}=${value}" "${file}" if [ "${value}" != "" ]; then
# value has a value, so it is a key-value pair
if grep -q "^${key}=" "${file}"; then
# Key already exists in file, modify the value
sed -i "/^${key}=/c\\${key}=${value}" "${file}"
else
# Key does not already exist, add it and it's value
echo "${key}=${value}" >> "${file}"
fi
else else
echo "${key}=${value}" >> "${file}" # value has no value, so it is just a key. Add it if it does not already exist
if ! grep -q "^${key}" "${file}"; then
# Key does not exist, add it.
echo "${key}" >> "${file}"
fi
fi fi
} }
#######################
# Takes two arguments file, and key.
# Deletes a key from target file
#
# Example usage:
# removeKey "/etc/pihole/setupVars.conf" "PIHOLE_DNS_1"
#######################
removeKey() {
local file="${1}"
local key="${2}"
sed -i "/^${key}/d" "${file}"
}
####################### #######################
# returns FTL's current telnet API port # returns FTL's current telnet API port
####################### #######################
getFTLAPIPort(){ getFTLAPIPort(){
local -r FTLCONFFILE="/etc/pihole/pihole-FTL.conf" local FTLCONFFILE="/etc/pihole/pihole-FTL.conf"
local -r DEFAULT_PORT_FILE="/run/pihole-FTL.port" local DEFAULT_PORT_FILE="/run/pihole-FTL.port"
local -r DEFAULT_FTL_PORT=4711 local DEFAULT_FTL_PORT=4711
local PORTFILE local PORTFILE
local ftl_api_port local ftl_api_port
if [[ -f "$FTLCONFFILE" ]]; then if [ -f "$FTLCONFFILE" ]; then
# if PORTFILE is not set in pihole-FTL.conf, use the default path # if PORTFILE is not set in pihole-FTL.conf, use the default path
PORTFILE="$( (grep "^PORTFILE=" $FTLCONFFILE || echo "$DEFAULT_PORT_FILE") | cut -d"=" -f2-)" PORTFILE="$( (grep "^PORTFILE=" $FTLCONFFILE || echo "$DEFAULT_PORT_FILE") | cut -d"=" -f2-)"
fi fi
if [[ -s "$PORTFILE" ]]; then if [ -s "$PORTFILE" ]; then
# -s: FILE exists and has a size greater than zero # -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 # Exploit prevention: unset the variable if there is malicious content
# Verify that the value read from the file is numeric # Verify that the value read from the file is numeric
[[ "$ftl_api_port" =~ [^[:digit:]] ]] && unset ftl_api_port expr "$ftl_api_port" : "[^[:digit:]]" > /dev/null && unset ftl_api_port
fi fi
# echo the port found in the portfile or default to the default port # echo the port found in the portfile or default to the default port

View File

@@ -1,5 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# shellcheck disable=SC1090 # shellcheck disable=SC1090
# shellcheck disable=SC2154
# Pi-hole: A black hole for Internet advertisements # Pi-hole: A black hole for Internet advertisements
# (c) 2017 Pi-hole, LLC (https://pi-hole.net) # (c) 2017 Pi-hole, LLC (https://pi-hole.net)
@@ -26,6 +28,9 @@ readonly PI_HOLE_FILES_DIR="/etc/.pihole"
PH_TEST="true" PH_TEST="true"
source "${PI_HOLE_FILES_DIR}/automated install/basic-install.sh" source "${PI_HOLE_FILES_DIR}/automated install/basic-install.sh"
utilsfile="/opt/pihole/utils.sh"
source "${utilsfile}"
coltable="/opt/pihole/COL_TABLE" coltable="/opt/pihole/COL_TABLE"
if [[ -f ${coltable} ]]; then if [[ -f ${coltable} ]]; then
source ${coltable} source ${coltable}
@@ -51,41 +56,35 @@ Options:
} }
add_setting() { add_setting() {
echo "${1}=${2}" >> "${setupVars}" addOrEditKeyValPair "${setupVars}" "${1}" "${2}"
} }
delete_setting() { delete_setting() {
sed -i "/^${1}/d" "${setupVars}" removeKey "${setupVars}" "${1}"
} }
change_setting() { change_setting() {
delete_setting "${1}" addOrEditKeyValPair "${setupVars}" "${1}" "${2}"
add_setting "${1}" "${2}"
} }
addFTLsetting() { addFTLsetting() {
echo "${1}=${2}" >> "${FTLconf}" addOrEditKeyValPair "${FTLconf}" "${1}" "${2}"
} }
deleteFTLsetting() { deleteFTLsetting() {
sed -i "/^${1}/d" "${FTLconf}" removeKey "${FTLconf}" "${1}"
} }
changeFTLsetting() { changeFTLsetting() {
deleteFTLsetting "${1}" addOrEditKeyValPair "${FTLconf}" "${1}" "${2}"
addFTLsetting "${1}" "${2}"
} }
add_dnsmasq_setting() { add_dnsmasq_setting() {
if [[ "${2}" != "" ]]; then addOrEditKeyValPair "${dnsmasqconfig}" "${1}" "${2}"
echo "${1}=${2}" >> "${dnsmasqconfig}"
else
echo "${1}" >> "${dnsmasqconfig}"
fi
} }
delete_dnsmasq_setting() { delete_dnsmasq_setting() {
sed -i "/^${1}/d" "${dnsmasqconfig}" removeKey "${dnsmasqconfig}" "${1}"
} }
SetTemperatureUnit() { SetTemperatureUnit() {
@@ -183,7 +182,7 @@ ProcessDNSSettings() {
fi fi
delete_dnsmasq_setting "dnssec" delete_dnsmasq_setting "dnssec"
delete_dnsmasq_setting "trust-anchor=" delete_dnsmasq_setting "trust-anchor"
if [[ "${DNSSEC}" == true ]]; then if [[ "${DNSSEC}" == true ]]; then
echo "dnssec echo "dnssec

View File

@@ -9,39 +9,8 @@
# Description: Enable service provided by pihole-FTL daemon # Description: Enable service provided by pihole-FTL daemon
### END INIT INFO ### END INIT INFO
# Get PID of main pihole-FTL process
FTLCONFFILE="/etc/pihole/pihole-FTL.conf"
DEFAULT_PID_FILE="/run/pihole-FTL.pid"
getFTLPID() {
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
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 to the caller
echo "${FTL_PID:=-1}"
}
is_running() { is_running() {
FTL_PID="$(getFTLPID)" pgrep -xo "pihole-FTL" > /dev/null
if [ "$FTL_PID" -eq "-1" ]; then
return 1
else
echo 0
fi
} }
@@ -52,6 +21,8 @@ start() {
else else
# Touch files to ensure they exist (create if non-existing, preserve if existing) # Touch files to ensure they exist (create if non-existing, preserve if existing)
mkdir -pm 0755 /run/pihole mkdir -pm 0755 /run/pihole
[ ! -f /run/pihole-FTL.pid ] && install -m 644 -o pihole -g pihole /dev/null /run/pihole-FTL.pid
[ ! -f /run/pihole-FTL.port ] && install -m 644 -o pihole -g pihole /dev/null /run/pihole-FTL.port
[ ! -f /var/log/pihole-FTL.log ] && install -m 644 -o pihole -g pihole /dev/null /var/log/pihole-FTL.log [ ! -f /var/log/pihole-FTL.log ] && install -m 644 -o pihole -g pihole /dev/null /var/log/pihole-FTL.log
[ ! -f /var/log/pihole.log ] && install -m 644 -o pihole -g pihole /dev/null /var/log/pihole.log [ ! -f /var/log/pihole.log ] && install -m 644 -o pihole -g pihole /dev/null /var/log/pihole.log
[ ! -f /etc/pihole/dhcp.leases ] && install -m 644 -o pihole -g pihole /dev/null /etc/pihole/dhcp.leases [ ! -f /etc/pihole/dhcp.leases ] && install -m 644 -o pihole -g pihole /dev/null /etc/pihole/dhcp.leases
@@ -76,7 +47,7 @@ start() {
# Stop the service # Stop the service
stop() { stop() {
if is_running; then if is_running; then
pkill -F "${FTL_PID_FILE}" pkill -xo "pihole-FTL"
for i in 1 2 3 4 5; do for i in 1 2 3 4 5; do
if ! is_running; then if ! is_running; then
break break
@@ -89,8 +60,8 @@ stop() {
if is_running; then if is_running; then
echo "Not stopped; may still be shutting down or shutdown may have failed, killing now" echo "Not stopped; may still be shutting down or shutdown may have failed, killing now"
pkill -9 -F "${FTL_PID_FILE}" pkill -xo -9 "pihole-FTL"
exit 0 exit 1
else else
echo "Stopped" echo "Stopped"
fi fi
@@ -98,7 +69,7 @@ stop() {
echo "Not running" echo "Not running"
fi fi
# Cleanup # Cleanup
rm -f /run/pihole/FTL.sock /dev/shm/FTL-* "${FTL_PID_FILE}" rm -f /run/pihole/FTL.sock /dev/shm/FTL-*
echo echo
} }

View File

@@ -259,6 +259,29 @@ os_check() {
fi fi
} }
# This function waits for dpkg to unlock, which signals that the previous apt-get command has finished.
test_dpkg_lock() {
i=0
printf " %b Waiting for package manager to finish (up to 30 seconds)\\n" "${INFO}"
# fuser is a program to show which processes use the named files, sockets, or filesystems
# So while the lock is held,
while fuser /var/lib/dpkg/lock >/dev/null 2>&1
do
# we wait half a second,
sleep 0.5
# increase the iterator,
((i=i+1))
# exit if waiting for more then 30 seconds
if [[ $i -gt 60 ]]; then
printf " %b %bError: Could not verify package manager finished and released lock. %b\\n" "${CROSS}" "${COL_LIGHT_RED}" "${COL_NC}"
printf " Attempt to install packages manually and retry.\\n"
exit 1;
fi
done
# and then report success once dpkg is unlocked.
return 0
}
# Compatibility # Compatibility
package_manager_detect() { package_manager_detect() {
# First check to see if apt-get is installed. # First check to see if apt-get is installed.
@@ -302,22 +325,6 @@ package_manager_detect() {
# and config file # and config file
LIGHTTPD_CFG="lighttpd.conf.debian" LIGHTTPD_CFG="lighttpd.conf.debian"
# This function waits for dpkg to unlock, which signals that the previous apt-get command has finished.
test_dpkg_lock() {
i=0
# fuser is a program to show which processes use the named files, sockets, or filesystems
# So while the lock is held,
while fuser /var/lib/dpkg/lock >/dev/null 2>&1
do
# we wait half a second,
sleep 0.5
# increase the iterator,
((i=i+1))
done
# and then report success once dpkg is unlocked.
return 0
}
# If apt-get is not found, check for rpm. # If apt-get is not found, check for rpm.
elif is_command rpm ; then elif is_command rpm ; then
# Then check if dnf or yum is the package manager # Then check if dnf or yum is the package manager

View File

@@ -11,10 +11,9 @@
source "/opt/pihole/COL_TABLE" source "/opt/pihole/COL_TABLE"
while true; do while true; do
read -rp " ${QST} Are you sure you would like to remove ${COL_WHITE}Pi-hole${COL_NC}? [y/N] " yn read -rp " ${QST} Are you sure you would like to remove ${COL_WHITE}Pi-hole${COL_NC}? [y/N] " answer
case ${yn} in case ${answer} in
[Yy]* ) break;; [Yy]* ) break;;
[Nn]* ) echo -e "${OVER} ${COL_LIGHT_GREEN}Uninstall has been canceled${COL_NC}"; exit 0;;
* ) echo -e "${OVER} ${COL_LIGHT_GREEN}Uninstall has been canceled${COL_NC}"; exit 0;; * ) echo -e "${OVER} ${COL_LIGHT_GREEN}Uninstall has been canceled${COL_NC}"; exit 0;;
esac esac
done done
@@ -76,8 +75,8 @@ removeAndPurge() {
for i in "${DEPS[@]}"; do for i in "${DEPS[@]}"; do
if package_check "${i}" > /dev/null; then if package_check "${i}" > /dev/null; then
while true; do while true; do
read -rp " ${QST} Do you wish to remove ${COL_WHITE}${i}${COL_NC} from your system? [Y/N] " yn read -rp " ${QST} Do you wish to remove ${COL_WHITE}${i}${COL_NC} from your system? [Y/N] " answer
case ${yn} in case ${answer} in
[Yy]* ) [Yy]* )
echo -ne " ${INFO} Removing ${i}..."; echo -ne " ${INFO} Removing ${i}...";
${SUDO} "${PKG_REMOVE[@]}" "${i}" &> /dev/null; ${SUDO} "${PKG_REMOVE[@]}" "${i}" &> /dev/null;
@@ -215,8 +214,8 @@ while true; do
echo -n "${i} " echo -n "${i} "
done done
echo "${COL_NC}" echo "${COL_NC}"
read -rp " ${QST} Do you wish to go through each dependency for removal? (Choosing No will leave all dependencies installed) [Y/n] " yn read -rp " ${QST} Do you wish to go through each dependency for removal? (Choosing No will leave all dependencies installed) [Y/n] " answer
case ${yn} in case ${answer} in
[Yy]* ) removeAndPurge; break;; [Yy]* ) removeAndPurge; break;;
[Nn]* ) removeNoPurge; break;; [Nn]* ) removeNoPurge; break;;
* ) removeAndPurge; break;; * ) removeAndPurge; break;;

54
pihole
View File

@@ -21,7 +21,7 @@ readonly FTL_PID_FILE="/run/pihole-FTL.pid"
readonly colfile="${PI_HOLE_SCRIPT_DIR}/COL_TABLE" readonly colfile="${PI_HOLE_SCRIPT_DIR}/COL_TABLE"
source "${colfile}" source "${colfile}"
readonly utilsfile="${PI_HOLE_SCRIPT_DIR}/utils.sh" utilsfile="${PI_HOLE_SCRIPT_DIR}/utils.sh"
source "${utilsfile}" source "${utilsfile}"
webpageFunc() { webpageFunc() {
@@ -226,7 +226,7 @@ Time:
fi fi
local str="Pi-hole Disabled" local str="Pi-hole Disabled"
addOrEditKeyValPair "BLOCKING_ENABLED" "false" "${setupVars}" addOrEditKeyValPair "${setupVars}" "BLOCKING_ENABLED" "false"
fi fi
else else
# Enable Pi-hole # Enable Pi-hole
@@ -238,7 +238,7 @@ Time:
echo -e " ${INFO} Enabling blocking" echo -e " ${INFO} Enabling blocking"
local str="Pi-hole Enabled" local str="Pi-hole Enabled"
addOrEditKeyValPair "BLOCKING_ENABLED" "true" "${setupVars}" addOrEditKeyValPair "${setupVars}" "BLOCKING_ENABLED" "true"
fi fi
restartDNS reload-lists restartDNS reload-lists
@@ -260,8 +260,8 @@ Options:
exit 0 exit 0
elif [[ "${1}" == "off" ]]; then elif [[ "${1}" == "off" ]]; then
# Disable logging # Disable logging
sed -i 's/^log-queries/#log-queries/' /etc/dnsmasq.d/01-pihole.conf addOrEditKeyValPair /etc/dnsmasq.d/01-pihole.conf "log-queries"
addOrEditKeyValPair "QUERY_LOGGING" "false" "${setupVars}" addOrEditKeyValPair "${setupVars}" "QUERY_LOGGING" "false"
if [[ "${2}" != "noflush" ]]; then if [[ "${2}" != "noflush" ]]; then
# Flush logs # Flush logs
"${PI_HOLE_BIN_DIR}"/pihole -f "${PI_HOLE_BIN_DIR}"/pihole -f
@@ -270,8 +270,8 @@ Options:
local str="Logging has been disabled!" local str="Logging has been disabled!"
elif [[ "${1}" == "on" ]]; then elif [[ "${1}" == "on" ]]; then
# Enable logging # Enable logging
sed -i 's/^#log-queries/log-queries/' /etc/dnsmasq.d/01-pihole.conf removeKey /etc/dnsmasq.d/01-pihole.conf "log-queries"
addOrEditKeyValPair "QUERY_LOGGING" "true" "${setupVars}" addOrEditKeyValPair "${setupVars}" "QUERY_LOGGING" "true"
echo -e " ${INFO} Enabling logging..." echo -e " ${INFO} Enabling logging..."
local str="Logging has been enabled!" local str="Logging has been enabled!"
else else
@@ -365,7 +365,7 @@ statusFunc() {
# Enable blocking # Enable blocking
"${PI_HOLE_BIN_DIR}"/pihole enable "${PI_HOLE_BIN_DIR}"/pihole enable
fi fi
exit 0
} }
tailFunc() { tailFunc() {
@@ -496,8 +496,38 @@ if [[ $# = 0 ]]; then
helpFunc helpFunc
fi fi
# functions that do not require sudo power
case "${1}" in case "${1}" in
"-h" | "help" | "--help" ) helpFunc;; "-h" | "help" | "--help" ) helpFunc;;
"-v" | "version" ) versionFunc "$@";;
"-c" | "chronometer" ) chronometerFunc "$@";;
"-q" | "query" ) queryFunc "$@";;
"status" ) statusFunc "$2";;
"-t" | "tail" ) tailFunc "$2";;
"tricorder" ) tricorderFunc;;
# we need to add all arguments that require sudo power to not trigger the * argument
"-w" | "whitelist" ) ;;
"-b" | "blacklist" ) ;;
"--wild" | "wildcard" ) ;;
"--regex" | "regex" ) ;;
"--white-regex" | "white-regex" ) ;;
"--white-wild" | "white-wild" ) ;;
"-f" | "flush" ) ;;
"-up" | "updatePihole" ) ;;
"-r" | "reconfigure" ) ;;
"-g" | "updateGravity" ) ;;
"-l" | "logging" ) ;;
"uninstall" ) ;;
"enable" ) ;;
"disable" ) ;;
"-d" | "debug" ) ;;
"restartdns" ) ;;
"-a" | "admin" ) ;;
"checkout" ) ;;
"updatechecker" ) ;;
"arpflush" ) ;;
* ) helpFunc;;
esac esac
# Must be root to use this tool # Must be root to use this tool
@@ -524,21 +554,13 @@ case "${1}" in
"-up" | "updatePihole" ) updatePiholeFunc "$@";; "-up" | "updatePihole" ) updatePiholeFunc "$@";;
"-r" | "reconfigure" ) reconfigurePiholeFunc;; "-r" | "reconfigure" ) reconfigurePiholeFunc;;
"-g" | "updateGravity" ) updateGravityFunc "$@";; "-g" | "updateGravity" ) updateGravityFunc "$@";;
"-c" | "chronometer" ) chronometerFunc "$@";;
"-h" | "help" ) helpFunc;;
"-v" | "version" ) versionFunc "$@";;
"-q" | "query" ) queryFunc "$@";;
"-l" | "logging" ) piholeLogging "$@";; "-l" | "logging" ) piholeLogging "$@";;
"uninstall" ) uninstallFunc;; "uninstall" ) uninstallFunc;;
"enable" ) piholeEnable 1;; "enable" ) piholeEnable 1;;
"disable" ) piholeEnable 0 "$2";; "disable" ) piholeEnable 0 "$2";;
"status" ) statusFunc "$2";;
"restartdns" ) restartDNS "$2";; "restartdns" ) restartDNS "$2";;
"-a" | "admin" ) webpageFunc "$@";; "-a" | "admin" ) webpageFunc "$@";;
"-t" | "tail" ) tailFunc "$2";;
"checkout" ) piholeCheckoutFunc "$@";; "checkout" ) piholeCheckoutFunc "$@";;
"tricorder" ) tricorderFunc;;
"updatechecker" ) updateCheckFunc "$@";; "updatechecker" ) updateCheckFunc "$@";;
"arpflush" ) arpFunc "$@";; "arpflush" ) arpFunc "$@";;
* ) helpFunc;;
esac esac

View File

@@ -2,15 +2,33 @@ def test_key_val_replacement_works(host):
''' Confirms addOrEditKeyValPair provides the expected output ''' ''' Confirms addOrEditKeyValPair provides the expected output '''
host.run(''' host.run('''
source /opt/pihole/utils.sh source /opt/pihole/utils.sh
addOrEditKeyValPair "KEY_ONE" "value1" "./testoutput" addOrEditKeyValPair "./testoutput" "KEY_ONE" "value1"
addOrEditKeyValPair "KEY_TWO" "value2" "./testoutput" addOrEditKeyValPair "./testoutput" "KEY_TWO" "value2"
addOrEditKeyValPair "KEY_ONE" "value3" "./testoutput" addOrEditKeyValPair "./testoutput" "KEY_ONE" "value3"
addOrEditKeyValPair "KEY_FOUR" "value4" "./testoutput" addOrEditKeyValPair "./testoutput" "KEY_FOUR" "value4"
addOrEditKeyValPair "./testoutput" "KEY_FIVE_NO_VALUE"
addOrEditKeyValPair "./testoutput" "KEY_FIVE_NO_VALUE"
''') ''')
output = host.run(''' output = host.run('''
cat ./testoutput cat ./testoutput
''') ''')
expected_stdout = 'KEY_ONE=value3\nKEY_TWO=value2\nKEY_FOUR=value4\n' expected_stdout = 'KEY_ONE=value3\nKEY_TWO=value2\nKEY_FOUR=value4\nKEY_FIVE_NO_VALUE\n'
assert expected_stdout == output.stdout
def test_key_val_removal_works(host):
''' Confirms removeKey provides the expected output '''
host.run('''
source /opt/pihole/utils.sh
addOrEditKeyValPair "./testoutput" "KEY_ONE" "value1"
addOrEditKeyValPair "./testoutput" "KEY_TWO" "value2"
addOrEditKeyValPair "./testoutput" "KEY_THREE" "value3"
removeKey "./testoutput" "KEY_TWO"
''')
output = host.run('''
cat ./testoutput
''')
expected_stdout = 'KEY_ONE=value1\nKEY_THREE=value3\n'
assert expected_stdout == output.stdout assert expected_stdout == output.stdout