Index: misctools/become =================================================================== --- misctools/become +++ misctools/become @@ -1,6 +1,8 @@ #!/bin/bash # # Copyright © 2013 Marc-André Pelletier +# 2024 Marc-André Pelletier +# Valerio Bozzolan # # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above @@ -15,25 +17,38 @@ # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # -prefix=$(/bin/cat /etc/wmcs-project) -if [ $# -lt 1 -o "$1" = "--help" ]; then - echo "usage: $(basename $0) [command [args...]]" >&2 - exit 1 +# NOTE: This script relies on the Bash feature "$EUID". +# So, the shebang cannot use /bin/sh. + +# Be strict. In case of an error, die without proceeding. +set -e + +# This contains a short string. Example: "tools". +prefix=$(< /etc/wmcs-project) + +# Require the right number of arguments. +if [ $# -lt 1 ] || [ "$1" = "--help" ]; then + echo "usage: $(basename "$0") [command [args...]]" >&2 + exit 1 fi + +# This is the tool name. Example: "croptool". tool="$1" + +# Discard the first argument, that is, the name of this script. shift # Test whether the given tool exists. if ! id "$prefix.$tool" >/dev/null 2>&1 || ! [ -d "/data/project/$tool" ]; then - echo "$(basename $0): no such tool '$tool'" >&2 - exit 1 + echo "$(basename "$0"): no such tool '$tool'" >&2 + exit 1 fi # Test whether the user is a member of the tool's group in this # session unless the user is root. -if [ $EUID -ne 0 ] && ! echo " $(groups) " | fgrep -q " $prefix.$tool "; then +if [ $EUID -ne 0 ] && ! echo " $(groups) " | grep --fixed-strings --quiet " $prefix.$tool "; then # Test whether the user is a member of the tool's group at all. - if echo ",$(getent group "$prefix.$tool" | cut -d : -f 4)," | fgrep -q ",$(id -nu),"; then + if echo ",$(getent group "$prefix.$tool" | cut -d : -f 4)," | grep --fixed-strings --quiet ",$(id -nu),"; then echo "You were added to the group $prefix.$tool after you started this login session." >&2 echo "You need to log out and in again to be able to \"become $tool\"." >&2 else @@ -45,4 +60,4 @@ fi # Execute sudo. -exec /usr/bin/sudo -niu "$prefix.$tool" "$@" +exec /usr/bin/sudo --non-interactive --login --user="$prefix.$tool" "$@"