From 424e825bd9a28a416766067dc22821ae81bd139f Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 22 Jun 2024 20:41:19 +0200 Subject: [PATCH] Do not auto-sudo in the pihole command Signed-off-by: DL6ER --- pihole | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/pihole b/pihole index d9a22c63..cae0cfb2 100755 --- a/pihole +++ b/pihole @@ -552,8 +552,8 @@ case "${1}" in "disable" ) ;; "-d" | "debug" ) ;; "restartdns" ) ;; - "-g" | "updateGravity" ) need_root=0;; - "reloaddns" ) need_root=0;; + "-g" | "updateGravity" ) ;; + "reloaddns" ) ;; "setpassword" ) ;; "checkout" ) ;; "updatechecker" ) ;; @@ -562,32 +562,18 @@ case "${1}" in * ) helpFunc;; esac -# Must be root to use this tool for most functions -if [[ ! $EUID -eq 0 && need_root -eq 1 ]];then - if [[ -x "$(command -v sudo)" ]]; then - exec sudo bash "$0" "$@" - exit $? - else - echo -e " ${CROSS} sudo is needed to run pihole commands. Please run this script as root or install sudo." - exit 1 - fi -fi - # In the case of alpine running in a container, the USER variable appears to be blank # which prevents the next trap from working correctly. Set it by running whoami if [[ -z ${USER} ]]; then USER=$(whoami) fi -# Can also be user pihole for other functions -if [[ ${USER} != "pihole" && need_root -eq 0 ]];then - if [[ -x "$(command -v sudo)" ]]; then - exec sudo -u pihole bash "$0" "$@" - exit $? - else - echo -e " ${CROSS} sudo is needed to run pihole commands. Please run this script as root or install sudo." - exit 1 - fi +# Check if the current user is neither root nor pihole and if the command +# requires root. If so, exit with an error message. +if [[ $EUID -ne 0 && ${USER} != "pihole" && need_root -eq 1 ]];then + echo -e " ${CROSS} The Pi-hole command requires root privileges, try:" + echo -e " ${COL_GREEN}sudo pihole ${@}${COL_NC}" + exit 1 fi # Handle redirecting to specific functions based on arguments