From 36956103004a99f59aa2258ff0880745d535f354 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 24 Jan 2023 19:51:01 +0100 Subject: [PATCH] Allow running pihole -g without root (run as user pihole) Signed-off-by: DL6ER --- gravity.sh | 2 +- pihole | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/gravity.sh b/gravity.sh index fe2097dd..5b950b28 100755 --- a/gravity.sh +++ b/gravity.sh @@ -912,7 +912,7 @@ gravity_ShowCount # Determine if DNS has been restarted by this instance of gravity if [[ -z "${dnsWasOffline:-}" ]]; then - "${PIHOLE_COMMAND}" restartdns reload + "${PIHOLE_COMMAND}" reloaddns fi gravity_Cleanup diff --git a/pihole b/pihole index 1d9ad82c..6796acc6 100755 --- a/pihole +++ b/pihole @@ -513,13 +513,13 @@ if [[ $# = 0 ]]; then fi # functions that do not require sudo power +need_root=1 case "${1}" in "-h" | "help" | "--help" ) helpFunc;; "-v" | "version" ) versionFunc "$@";; "-c" | "chronometer" ) chronometerFunc "$@";; "-q" | "query" ) queryFunc "$@";; "status" ) statusFunc "$2";; - "tricorder" ) tricorderFunc;; # we need to add all arguments that require sudo power to not trigger the * argument @@ -532,13 +532,14 @@ case "${1}" in "-f" | "flush" ) ;; "-up" | "updatePihole" ) ;; "-r" | "reconfigure" ) ;; - "-g" | "updateGravity" ) ;; "-l" | "logging" ) ;; "uninstall" ) ;; "enable" ) ;; "disable" ) ;; "-d" | "debug" ) ;; "restartdns" ) ;; + "-g" | "updateGravity" ) need_root=0;; + "reloaddns" ) need_root=0;; "-a" | "admin" ) ;; "checkout" ) ;; "updatechecker" ) ;; @@ -547,8 +548,8 @@ case "${1}" in * ) helpFunc;; esac -# Must be root to use this tool -if [[ ! $EUID -eq 0 ]];then +# 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 $? @@ -557,6 +558,16 @@ if [[ ! $EUID -eq 0 ]];then exit 1 fi 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 +fi # Handle redirecting to specific functions based on arguments case "${1}" in @@ -576,6 +587,7 @@ case "${1}" in "enable" ) piholeEnable 1;; "disable" ) piholeEnable 0 "$2";; "restartdns" ) restartDNS "$2";; + "reloaddns" ) restartDNS "reload";; "-a" | "admin" ) webpageFunc "$@";; "checkout" ) piholeCheckoutFunc "$@";; "updatechecker" ) shift; updateCheckFunc "$@";;