Add additional checks when running as root

This commit is contained in:
Ben Peddell 2021-03-14 06:14:31 +10:00
parent 86b22b4dea
commit 7c86d40eff

View File

@ -229,18 +229,26 @@ runAsRoot(){
elif [ "$1" == "uninstall-tools" ]; then elif [ "$1" == "uninstall-tools" ]; then
doUninstallTools doUninstallTools
elif [ "$steamcmd_user" = "$USERNAME" ]; then elif [ "$steamcmd_user" = "$USERNAME" ]; then
echo "Error: steamcmd_user is root; aborting" echo "Error: steamcmd_user is root; aborting" >&2
exit 1 exit 1
else else
exec /sbin/runuser "$steamcmd_user" -s "$steamcmd_user_shellexec" -c "$(printf "%q" "$0")$(printf " %q" "$@")" echo "Attempting to drop privileges; re-running as $steamcmd_user" >&2
exec /sbin/runuser "$steamcmd_user" -s "$steamcmd_user_shellexec" -c "$(printf "%q" "$0") --runfromroot$(printf " %q" "$@")"
exit 1 exit 1
fi fi
} }
# Check the user is not currently running this script as root # Check the user is not currently running this script as root
if [ "$(id -u)" == "0" ]; then if [ "$(id -u)" == "0" ]; then
runAsRoot "$@" if [ "$1" != "--run-as-root-i-know-what-im-doing" ]; then
exit 0 echo "Warning: running as root is dangerous" >&2
elif [ "$1" = "--runfromroot" ]; then
echo "Error: still running as root after attempting to drop privileges; aborting" >&2
exit 1
else
runAsRoot "$@"
exit 0
fi
fi fi
#--------------------- #---------------------
@ -259,6 +267,7 @@ if [ -f "${HOME}/${arkstUserCfgFile}" ]; then
fi fi
if [[ -n "${steamcmd_user}" && "${steamcmd_user}" != "--me" && "$USERNAME" != "${steamcmd_user}" && -n "$usesudo" ]]; then if [[ -n "${steamcmd_user}" && "${steamcmd_user}" != "--me" && "$USERNAME" != "${steamcmd_user}" && -n "$usesudo" ]]; then
echo "Attempting to run as $steamcmd_user" >&2
exec sudo --user="${steamcmd_user}" "$0" "$@" exec sudo --user="${steamcmd_user}" "$0" "$@"
fi fi
@ -3791,6 +3800,12 @@ main(){
inCronJob=true inCronJob=true
continue continue
;; ;;
--runfromroot)
continue
;;
--run-as-root-i-know-what-im-doing)
continue
;;
esac esac
# get the number of arguments for commands that take arguments # get the number of arguments for commands that take arguments