1
0
mirror of https://github.com/pi-hole/pi-hole synced 2024-11-18 06:08:21 +00:00

Allow running pihole -g without root (run as user pihole)

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER 2023-01-24 19:51:01 +01:00 committed by Adam Warner
parent bf16fe4a37
commit 3695610300
No known key found for this signature in database
2 changed files with 17 additions and 5 deletions

View File

@ -912,7 +912,7 @@ gravity_ShowCount
# Determine if DNS has been restarted by this instance of gravity # Determine if DNS has been restarted by this instance of gravity
if [[ -z "${dnsWasOffline:-}" ]]; then if [[ -z "${dnsWasOffline:-}" ]]; then
"${PIHOLE_COMMAND}" restartdns reload "${PIHOLE_COMMAND}" reloaddns
fi fi
gravity_Cleanup gravity_Cleanup

20
pihole
View File

@ -513,13 +513,13 @@ if [[ $# = 0 ]]; then
fi fi
# functions that do not require sudo power # functions that do not require sudo power
need_root=1
case "${1}" in case "${1}" in
"-h" | "help" | "--help" ) helpFunc;; "-h" | "help" | "--help" ) helpFunc;;
"-v" | "version" ) versionFunc "$@";; "-v" | "version" ) versionFunc "$@";;
"-c" | "chronometer" ) chronometerFunc "$@";; "-c" | "chronometer" ) chronometerFunc "$@";;
"-q" | "query" ) queryFunc "$@";; "-q" | "query" ) queryFunc "$@";;
"status" ) statusFunc "$2";; "status" ) statusFunc "$2";;
"tricorder" ) tricorderFunc;; "tricorder" ) tricorderFunc;;
# we need to add all arguments that require sudo power to not trigger the * argument # we need to add all arguments that require sudo power to not trigger the * argument
@ -532,13 +532,14 @@ case "${1}" in
"-f" | "flush" ) ;; "-f" | "flush" ) ;;
"-up" | "updatePihole" ) ;; "-up" | "updatePihole" ) ;;
"-r" | "reconfigure" ) ;; "-r" | "reconfigure" ) ;;
"-g" | "updateGravity" ) ;;
"-l" | "logging" ) ;; "-l" | "logging" ) ;;
"uninstall" ) ;; "uninstall" ) ;;
"enable" ) ;; "enable" ) ;;
"disable" ) ;; "disable" ) ;;
"-d" | "debug" ) ;; "-d" | "debug" ) ;;
"restartdns" ) ;; "restartdns" ) ;;
"-g" | "updateGravity" ) need_root=0;;
"reloaddns" ) need_root=0;;
"-a" | "admin" ) ;; "-a" | "admin" ) ;;
"checkout" ) ;; "checkout" ) ;;
"updatechecker" ) ;; "updatechecker" ) ;;
@ -547,8 +548,8 @@ case "${1}" in
* ) helpFunc;; * ) helpFunc;;
esac esac
# Must be root to use this tool # Must be root to use this tool for most functions
if [[ ! $EUID -eq 0 ]];then if [[ ! $EUID -eq 0 && need_root -eq 1 ]];then
if [[ -x "$(command -v sudo)" ]]; then if [[ -x "$(command -v sudo)" ]]; then
exec sudo bash "$0" "$@" exec sudo bash "$0" "$@"
exit $? exit $?
@ -557,6 +558,16 @@ if [[ ! $EUID -eq 0 ]];then
exit 1 exit 1
fi fi
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 # Handle redirecting to specific functions based on arguments
case "${1}" in case "${1}" in
@ -576,6 +587,7 @@ case "${1}" in
"enable" ) piholeEnable 1;; "enable" ) piholeEnable 1;;
"disable" ) piholeEnable 0 "$2";; "disable" ) piholeEnable 0 "$2";;
"restartdns" ) restartDNS "$2";; "restartdns" ) restartDNS "$2";;
"reloaddns" ) restartDNS "reload";;
"-a" | "admin" ) webpageFunc "$@";; "-a" | "admin" ) webpageFunc "$@";;
"checkout" ) piholeCheckoutFunc "$@";; "checkout" ) piholeCheckoutFunc "$@";;
"updatechecker" ) shift; updateCheckFunc "$@";; "updatechecker" ) shift; updateCheckFunc "$@";;