Skip to content

Commit

Permalink
Installer: Improve handling of su/sudo
Browse files Browse the repository at this point in the history
A couple of changes here:

* Check for sudo necessity and availability before doing any real work.
  Better to warn and exit quick and early!
* Remove the support for using `su`. It was broken for two reasons.
  First, unlike `sudo`, `su -c` expects the command as a single
  argument. Second, `su`, unlike `sudo`, reads the password from stdin
  which in this case "contains" the tarball being downloaded. The second
  point is particularly tricky to solve without saving the tarball to
  storage (and we want to avoid extra storage space and wear). And,
  anyway, users that can `su -c` can also `su` manually before
  installing.

Signed-off-by: Leandro Motta Barros <[email protected]>
Change-type: patch
  • Loading branch information
lmbarros committed Mar 21, 2023
1 parent a0a2855 commit 0a46c1f
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions contrib/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ for cmd in curl tar; do
done
[ $abort == 1 ] && exit 1

sudo=
if [ "$(id -u)" -ne 0 ]; then
if [ -z "$(command -v sudo)" ]; then
cat >&2 <<-EOF
Error: this installer needs the ability to run commands as root.
You are not running as root and we are unable to find "sudo" available.
EOF
exit 1
fi
sudo="sudo -E"
fi

# Detect the system architecture
machine=$(uname -m)

case "$machine" in
Expand All @@ -43,26 +56,9 @@ case "$machine" in
exit 1
esac

# Download and decompress into the target location
url="https://github.com/balena-os/balena-engine/releases/download/${tag}/balena-engine-${tag}-${arch}.tar.gz"

sudo=
if [[ $(id -u) -ne 0 ]]; then
if [[ $(command -v sudo) ]]; then
sudo='sudo -E'
fi
if [[ $(command -v su) ]]; then
sudo='su -c'
fi
if [[ -z $sudo ]]; then
cat >&2 <<-EOF
Error: this installer needs the ability to run commands as root.
We are unable to find either "sudo" or "su" available to make this happen.
EOF
exit 1
fi
fi


curl -sL "$url" | $sudo tar xzv -C /usr/local/bin --strip-components=1

cat <<-EOF
Expand Down

0 comments on commit 0a46c1f

Please sign in to comment.