setup: create user when needed and fix sudo autosetup

This commit is contained in:
southerntofu 2020-04-27 17:52:06 +02:00
parent 2dd1df7ca2
commit 3329beb33c
1 changed files with 12 additions and 2 deletions

View File

@ -14,11 +14,20 @@ else
owner="$1"
echo "[setup.sh] Setup for user $owner"
else
# Default value
owner="forgehook"
echo "[setup.sh] No setup user found. Using default value $owner"
fi
fi
# If the user doesn't exist, create it
if ! id -u "$owner" > /dev/null 2>&1; then
# We need to create the forgehook user
echo "[setup.sh] User $owner doesn't exist yet. Creating it."
sudo useradd --create-home --shell /bin/bash --user-group --system --home-dir /opt/forgehook "$owner"
fi
# TODO: Detect if user has UID > 1000 so if it's a real user we refuse to setup
# and try to escalate to generate a dedicated account.
# Please don't run me as root!
@ -55,11 +64,12 @@ echo "[setup.sh] Installed forgehook to $DEST for $owner"
SUDO_SETUP=0
if ! sudo grep "forgehook-backend" /etc/sudoers > /dev/null; then
SUDO_SETUP=1
sudo echo "ALL ALL=($owner) NOPASSWD: /usr/local/bin/forgehook-backend" >> /etc/sudoers
# Need to open a dedicated shell through sudo or we don't have permissions
sudo sh -c "echo ALL ALL=\("$owner"\) NOPASSWD: /usr/local/bin/forgehook-backend >> /etc/sudoers"
fi
if ! sudo grep "forgehook-run" /etc/sudoers > /dev/null; then
SUDO_SETUP=1
sudo echo "$owner ALL=NOPASSWD: /usr/local/bin/forgehook-run" >> /etc/sudoers
sudo sh -c "echo "$owner" ALL=NOPASSWD: /usr/local/bin/forgehook-run >> /etc/sudoers"
fi
[[ $SUDO_SETUP = 1 ]] && echo "[setup.sh] sudo rules have been installed for forgehook"