Do not auto-sudo in the pihole command

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER 2024-06-22 20:41:19 +02:00
parent 7e91b9ab47
commit 424e825bd9
No known key found for this signature in database
GPG key ID: 00135ACBD90B28DD

30
pihole
View file

@ -552,8 +552,8 @@ case "${1}" in
"disable" ) ;; "disable" ) ;;
"-d" | "debug" ) ;; "-d" | "debug" ) ;;
"restartdns" ) ;; "restartdns" ) ;;
"-g" | "updateGravity" ) need_root=0;; "-g" | "updateGravity" ) ;;
"reloaddns" ) need_root=0;; "reloaddns" ) ;;
"setpassword" ) ;; "setpassword" ) ;;
"checkout" ) ;; "checkout" ) ;;
"updatechecker" ) ;; "updatechecker" ) ;;
@ -562,32 +562,18 @@ case "${1}" in
* ) helpFunc;; * ) helpFunc;;
esac 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 # 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 # which prevents the next trap from working correctly. Set it by running whoami
if [[ -z ${USER} ]]; then if [[ -z ${USER} ]]; then
USER=$(whoami) USER=$(whoami)
fi fi
# Can also be user pihole for other functions # Check if the current user is neither root nor pihole and if the command
if [[ ${USER} != "pihole" && need_root -eq 0 ]];then # requires root. If so, exit with an error message.
if [[ -x "$(command -v sudo)" ]]; then if [[ $EUID -ne 0 && ${USER} != "pihole" && need_root -eq 1 ]];then
exec sudo -u pihole bash "$0" "$@" echo -e " ${CROSS} The Pi-hole command requires root privileges, try:"
exit $? echo -e " ${COL_GREEN}sudo pihole ${@}${COL_NC}"
else exit 1
echo -e " ${CROSS} sudo is needed to run pihole commands. Please run this script as root or install sudo."
exit 1
fi
fi fi
# Handle redirecting to specific functions based on arguments # Handle redirecting to specific functions based on arguments