Switch to root when started as regular user

In collaborative builder machine, these scripts are often allowed to become root
via sudo. This patch avoid to prefix them by sudo each time or call su.

Signed-off-by: Pierre Schmitz <pierre@archlinux.de>
This commit is contained in:
Sébastien Luttringer 2014-01-08 01:15:37 +01:00 committed by Pierre Schmitz
parent 7aac293d76
commit 03611dc63e
5 changed files with 18 additions and 6 deletions

View File

@ -37,8 +37,8 @@ while getopts 'hC:M:c:' arg; do
done done
shift $(($OPTIND - 1)) shift $(($OPTIND - 1))
(( $EUID != 0 )) && die 'This script must be run as root.'
(( $# < 1 )) && die 'You must specify a directory.' (( $# < 1 )) && die 'You must specify a directory.'
check_root "$0" "$@"
working_dir=$(readlink -f "$1") working_dir=$(readlink -f "$1")
shift 1 shift 1

View File

@ -40,9 +40,7 @@ done
# Pass all arguments after -- right to makepkg # Pass all arguments after -- right to makepkg
makechrootpkg_args+=("${@:$OPTIND}") makechrootpkg_args+=("${@:$OPTIND}")
if (( EUID )); then check_root "$0" "$@"
die 'This script must be run as root.'
fi
if ${clean_first} || [[ ! -d "${chroots}/${repo}-${arch}" ]]; then if ${clean_first} || [[ ! -d "${chroots}/${repo}-${arch}" ]]; then
msg "Creating chroot for [${repo}] (${arch})..." msg "Creating chroot for [${repo}] (${arch})..."

View File

@ -228,3 +228,16 @@ find_cached_package() {
return 1 return 1
esac esac
} }
##
# usage : check_root ("$0" "$@")
##
check_root() {
(( EUID == 0 )) && return
if type -P sudo >/dev/null; then
exec sudo -- "$@"
else
exec su root -c "$(printf '%q' "$@")"
fi
die 'This script must be run as root.'
}

View File

@ -81,7 +81,7 @@ while getopts 'hcur:I:l:nTD:d:' arg; do
esac esac
done done
(( EUID != 0 )) && die 'This script must be run as root.' check_root "$0" "$@"
[[ ! -f PKGBUILD && -z "${install_pkgs[*]}" ]] && die 'This must be run in a directory containing a PKGBUILD.' [[ ! -f PKGBUILD && -z "${install_pkgs[*]}" ]] && die 'This must be run in a directory containing a PKGBUILD.'

View File

@ -35,9 +35,10 @@ while getopts 'hC:M:c:' arg; do
done done
shift $(($OPTIND - 1)) shift $(($OPTIND - 1))
(( $EUID != 0 )) && die 'This script must be run as root.'
(( $# < 2 )) && die 'You must specify a directory and one or more packages.' (( $# < 2 )) && die 'You must specify a directory and one or more packages.'
check_root "$0" "$@"
working_dir="$(readlink -f $1)" working_dir="$(readlink -f $1)"
shift 1 shift 1