]> git.feebdaed.xyz Git - 0xmirror/pi-hole.git/commitdiff
Invert need_root logic and check if set/unset
authorChristian König <github@yubiuser.dev>
Wed, 16 Jul 2025 06:52:25 +0000 (08:52 +0200)
committerChristian König <github@yubiuser.dev>
Wed, 23 Jul 2025 06:09:26 +0000 (08:09 +0200)
Signed-off-by: Christian König <github@yubiuser.dev>
Co-authored-by: Dan Schaper <dan.schaper@pi-hole.net>
pihole

diff --git a/pihole b/pihole
index c68575a968321ae90dc2a6ca354f84b54c2be2ae..5e4465ea4b61881b214ddea4b294c3b1a876de67 100755 (executable)
--- a/pihole
+++ b/pihole
@@ -542,7 +542,7 @@ if [[ $# = 0 ]]; then
 fi
 
 # functions that do not require sudo power
-need_root=1
+need_root=
 case "${1}" in
   "-h" | "help" | "--help"        ) helpFunc;;
   "-v" | "version"                ) versionFunc;;
@@ -552,30 +552,30 @@ case "${1}" in
   "tricorder"                     ) tricorderFunc;;
 
   # we need to add all arguments that require sudo power to not trigger the * argument
-  "allow" | "allowlist"           ) need_root=0;;
-  "deny" | "denylist"             ) need_root=0;;
-  "--wild" | "wildcard"           ) need_root=0;;
-  "--regex" | "regex"             ) need_root=0;;
-  "--allow-regex" | "allow-regex" ) need_root=0;;
-  "--allow-wild" | "allow-wild"   ) need_root=0;;
-  "-f" | "flush"                  ) ;;
-  "-up" | "updatePihole"          ) ;;
-  "-r"  | "repair"                ) ;;
-  "-l" | "logging"                ) ;;
-  "uninstall"                     ) ;;
-  "enable"                        ) need_root=0;;
-  "disable"                       ) need_root=0;;
-  "-d" | "debug"                  ) ;;
-  "-g" | "updateGravity"          ) ;;
-  "reloaddns"                     ) ;;
-  "reloadlists"                   ) ;;
-  "setpassword"                   ) ;;
-  "checkout"                      ) ;;
-  "updatechecker"                 ) ;;
-  "arpflush"                      ) ;; # Deprecated, use networkflush instead
-  "networkflush"                  ) ;;
-  "-t" | "tail"                   ) ;;
-  "api"                           ) need_root=0;;
+  "allow" | "allowlist"           ) ;;
+  "deny" | "denylist"             ) ;;
+  "--wild" | "wildcard"           ) ;;
+  "--regex" | "regex"             ) ;;
+  "--allow-regex" | "allow-regex" ) ;;
+  "--allow-wild" | "allow-wild"   ) ;;
+  "-f" | "flush"                  ) need_root=true;;
+  "-up" | "updatePihole"          ) need_root=true;;
+  "-r"  | "repair"                ) need_root=true;;
+  "-l" | "logging"                ) need_root=true;;
+  "uninstall"                     ) need_root=true;;
+  "enable"                        ) ;;
+  "disable"                       ) ;;
+  "-d" | "debug"                  ) need_root=true;;
+  "-g" | "updateGravity"          ) need_root=true;;
+  "reloaddns"                     ) need_root=true;;
+  "reloadlists"                   ) need_root=true;;
+  "setpassword"                   ) need_root=true;;
+  "checkout"                      ) need_root=true;;
+  "updatechecker"                 ) need_root=true;;
+  "arpflush"                      ) need_root=true;; # Deprecated, use networkflush instead
+  "networkflush"                  ) need_root=true;;
+  "-t" | "tail"                   ) need_root=true;;
+  "api"                           ) ;;
   *                               ) helpFunc;;
 esac
 
@@ -588,8 +588,8 @@ fi
 # Check if the current user is not root and if the command
 # requires root. If so, exit with an error message.
 # Add an exception for the user "pihole" to allow the webserver running gravity
-if [[ ( $EUID -ne 0 && ${USER} != "pihole" ) && need_root -eq 1 ]]; then
-  echo -e "  ${CROSS} The Pi-hole command requires root privileges, try:"
+if [[ ( $EUID -ne 0 && ${USER} != "pihole" ) && -n "${need_root}" ]]; then
+  echo -e "  ${CROSS} This Pi-hole command requires root privileges, try:"
   echo -e "      ${COL_GREEN}sudo pihole $*${COL_NC}"
   exit 1
 fi