From fddbd11baa97295d903834bacf7280857256b1ba Mon Sep 17 00:00:00 2001 From: Dominique Liberda Date: Sat, 8 Jul 2023 06:31:05 +0200 Subject: [PATCH] Meta: Implement proper checks for sudo alternatives This introduces support for building with doas. --- Meta/build-image-extlinux.sh | 2 +- Meta/build-image-grub.sh | 2 +- Meta/build-image-limine.sh | 2 +- Meta/build-image-qemu.sh | 2 +- Meta/build-native-partition.sh | 2 +- Meta/shell_include.sh | 22 ++++++++++++++++------ 6 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Meta/build-image-extlinux.sh b/Meta/build-image-extlinux.sh index 3647c677661..d16659beb03 100755 --- a/Meta/build-image-extlinux.sh +++ b/Meta/build-image-extlinux.sh @@ -8,7 +8,7 @@ script_path=$(cd -P -- "$(dirname -- "$0")" && pwd -P) if [ "$(id -u)" != 0 ]; then set +e - ${SUDO} -- sh -c "\"$0\" $* || exit 42" + ${SUDO} "${SHELL}" -c -- "\"$0\" $* || exit 42" case $? in 1) die "this script needs to run as root" diff --git a/Meta/build-image-grub.sh b/Meta/build-image-grub.sh index 4b486156136..a130723df93 100755 --- a/Meta/build-image-grub.sh +++ b/Meta/build-image-grub.sh @@ -8,7 +8,7 @@ script_path=$(cd -P -- "$(dirname -- "$0")" && pwd -P) if [ "$(id -u)" != 0 ]; then set +e - ${SUDO} -- sh -c "\"$0\" $* || exit 42" + ${SUDO} "${SHELL}" -c -- "\"$0\" $* || exit 42" case $? in 1) die "this script needs to run as root" diff --git a/Meta/build-image-limine.sh b/Meta/build-image-limine.sh index e68ee4dcf2c..1be38b25557 100755 --- a/Meta/build-image-limine.sh +++ b/Meta/build-image-limine.sh @@ -17,7 +17,7 @@ fi if [ "$(id -u)" != 0 ]; then set +e - ${SUDO} -- sh -c "\"$0\" $* || exit 42" + ${SUDO} "${SHELL}" -c -- "\"$0\" $* || exit 42" case $? in 1) die "this script needs to run as root" diff --git a/Meta/build-image-qemu.sh b/Meta/build-image-qemu.sh index f58734ea448..9d9e2eaf742 100755 --- a/Meta/build-image-qemu.sh +++ b/Meta/build-image-qemu.sh @@ -12,7 +12,7 @@ if [ "$(id -u)" != 0 ]; then USE_FUSE2FS=1 else set +e - ${SUDO} -- sh -c "\"$0\" $* || exit 42" + ${SUDO} "${SHELL}" -c -- "\"$0\" $* || exit 42" case $? in 1) die "this script needs to run as root" diff --git a/Meta/build-native-partition.sh b/Meta/build-native-partition.sh index 8ebb3ba6a62..c79f6a052a5 100755 --- a/Meta/build-native-partition.sh +++ b/Meta/build-native-partition.sh @@ -16,7 +16,7 @@ cleanup() { if [ "$(id -u)" != 0 ]; then set +e - ${SUDO} -- sh -c "\"$0\" $* || exit 42" + ${SUDO} "${SHELL}" -c -- "\"$0\" $* || exit 42" case $? in 1) die "this script needs to run as root" diff --git a/Meta/shell_include.sh b/Meta/shell_include.sh index 11da2e1999e..f1a28291d74 100644 --- a/Meta/shell_include.sh +++ b/Meta/shell_include.sh @@ -6,17 +6,27 @@ # NOTE: If using another privilege escalation binary make sure it is configured or has the appropiate flag # to keep the current environment variables in the launched process (in sudo's case this is achieved # through the -E flag described in sudo(8). -SUDO="sudo -E" - -if [ "$(uname -s)" = "SerenityOS" ]; then - SUDO="pls -E" -fi - die() { echo "die: $*" exit 1 } +if [ "$(uname -s)" = "SerenityOS" ]; then + SUDO="pls -E" +elif command -v sudo >/dev/null; then + SUDO="sudo -E" +elif command -v doas >/dev/null; then + if [ "$SUDO_UID" = '' ]; then + SUDO_UID=$(id -u) + SUDO_GID=$(id -g) + export SUDO_UID SUDO_GID + fi + # To make doas work, you have to make sure you use the "keepenv" flag in doas.conf + SUDO="doas" +else + die "You need sudo, doas or pls to build Serenity..." +fi + exit_if_running_as_root() { if [ "$(id -u)" -eq 0 ]; then die "$*"