install.sh: properly handle superuser permissions

This script is based around having superuser permissions. The way it handles them is by using `sudo`. This has two main problems:

1. It could potentially require someone to input their password many, many times depending on their sudo configuration.
2. Even when running as root, the script will break for anyone without sudo installed.

The way to fix this is simple: require the script itself be run as root instead of using sudo. 

This patch adds a check at the beginning which verifies if the user is root. If the user is not root, it explains what must be done to rectify the problem. It then also removes all usages of sudo throughout the script.

This also aligns the script with more typical scriptwriting conventions, overall improving the quality of the script.
This commit is contained in:
Amy 2023-07-19 20:32:49 -07:00 committed by GitHub
parent 4170dc1f80
commit bd815bf26a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,12 @@
#!/bin/bash
# Many things here need root access, so verify user is root
# If not, fail out and give an explanation
if [ $(id -u) -ne 0 ]; then
echo "error: must be root (use sudo)"
exit 1
fi
# Get Username
uname=$(whoami)
admintoken=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c16)
@ -67,14 +74,14 @@ PREREQARCH="bind"
echo "Installing prerequisites"
if [ "${ID}" = "debian" ] || [ "$OS" = "Ubuntu" ] || [ "$OS" = "Debian" ] || [ "${UPSTREAM_ID}" = "ubuntu" ] || [ "${UPSTREAM_ID}" = "debian" ]; then
sudo apt-get update
sudo apt-get install -y ${PREREQ} ${PREREQDEB} # git
apt-get update
apt-get install -y ${PREREQ} ${PREREQDEB} # git
elif [ "$OS" = "CentOS" ] || [ "$OS" = "RedHat" ] || [ "${UPSTREAM_ID}" = "rhel" ] ; then
# opensuse 15.4 fails to run the relay service and hangs waiting for it
# needs more work before it can be enabled
# || [ "${UPSTREAM_ID}" = "suse" ]
sudo yum update -y
sudo yum install -y ${PREREQ} ${PREREQRPM} # git
yum update -y
yum install -y ${PREREQ} ${PREREQRPM} # git
elif [ "${ID}" = "arch" ] || [ "${UPSTREAM_ID}" = "arch" ]; then
sudo pacman -Syu
sudo pacman -S ${PREREQ} ${PREREQARCH}
@ -113,9 +120,9 @@ done
# Make Folder /opt/rustdesk/
if [ ! -d "/opt/rustdesk" ]; then
echo "Creating /opt/rustdesk"
sudo mkdir -p /opt/rustdesk/
mkdir -p /opt/rustdesk/
fi
sudo chown "${uname}" -R /opt/rustdesk
chown "${uname}" -R /opt/rustdesk
cd /opt/rustdesk/ || exit 1
@ -144,9 +151,9 @@ chmod +x /opt/rustdesk/hbbr
# Make Folder /var/log/rustdesk/
if [ ! -d "/var/log/rustdesk" ]; then
echo "Creating /var/log/rustdesk"
sudo mkdir -p /var/log/rustdesk/
mkdir -p /var/log/rustdesk/
fi
sudo chown "${uname}" -R /var/log/rustdesk/
chown "${uname}" -R /var/log/rustdesk/
# Setup Systemd to launch hbbs
rustdesksignal="$(cat << EOF
@ -168,10 +175,10 @@ RestartSec=10
WantedBy=multi-user.target
EOF
)"
echo "${rustdesksignal}" | sudo tee /etc/systemd/system/rustdesksignal.service > /dev/null
sudo systemctl daemon-reload
sudo systemctl enable rustdesksignal.service
sudo systemctl start rustdesksignal.service
echo "${rustdesksignal}" | tee /etc/systemd/system/rustdesksignal.service > /dev/null
systemctl daemon-reload
systemctl enable rustdesksignal.service
systemctl start rustdesksignal.service
# Setup Systemd to launch hbbr
rustdeskrelay="$(cat << EOF
@ -193,13 +200,13 @@ RestartSec=10
WantedBy=multi-user.target
EOF
)"
echo "${rustdeskrelay}" | sudo tee /etc/systemd/system/rustdeskrelay.service > /dev/null
sudo systemctl daemon-reload
sudo systemctl enable rustdeskrelay.service
sudo systemctl start rustdeskrelay.service
echo "${rustdeskrelay}" | tee /etc/systemd/system/rustdeskrelay.service > /dev/null
systemctl daemon-reload
systemctl enable rustdeskrelay.service
systemctl start rustdeskrelay.service
while ! [[ $CHECK_RUSTDESK_READY ]]; do
CHECK_RUSTDESK_READY=$(sudo systemctl status rustdeskrelay.service | grep "Active: active (running)")
CHECK_RUSTDESK_READY=$(systemctl status rustdeskrelay.service | grep "Active: active (running)")
echo -ne "Rustdesk Relay not ready yet...${NC}\n"
sleep 3
done
@ -229,22 +236,22 @@ case $EXTRAOPT in
# Create windows install script
wget https://raw.githubusercontent.com/dinger1986/rustdeskinstall/master/WindowsAgentAIOInstall.ps1
sudo sed -i "s|wanipreg|${wanip}|g" WindowsAgentAIOInstall.ps1
sudo sed -i "s|keyreg|${key}|g" WindowsAgentAIOInstall.ps1
sed -i "s|wanipreg|${wanip}|g" WindowsAgentAIOInstall.ps1
sed -i "s|keyreg|${key}|g" WindowsAgentAIOInstall.ps1
# Create linux install script
wget https://raw.githubusercontent.com/dinger1986/rustdeskinstall/master/linuxclientinstall.sh
sudo sed -i "s|wanipreg|${wanip}|g" linuxclientinstall.sh
sudo sed -i "s|keyreg|${key}|g" linuxclientinstall.sh
sed -i "s|wanipreg|${wanip}|g" linuxclientinstall.sh
sed -i "s|keyreg|${key}|g" linuxclientinstall.sh
# Download and install gohttpserver
# Make Folder /opt/gohttp/
if [ ! -d "/opt/gohttp" ]; then
echo "Creating /opt/gohttp"
sudo mkdir -p /opt/gohttp/
sudo mkdir -p /opt/gohttp/public
mkdir -p /opt/gohttp/
mkdir -p /opt/gohttp/public
fi
sudo chown "${uname}" -R /opt/gohttp
chown "${uname}" -R /opt/gohttp
cd /opt/gohttp
GOHTTPLATEST=$(curl https://api.github.com/repos/codeskyblue/gohttpserver/releases/latest -s | grep "tag_name"| awk '{print substr($2, 2, length($2)-3) }')
@ -269,9 +276,9 @@ mv /opt/rustdesk/linuxclientinstall.sh /opt/gohttp/public/
# Make gohttp log folders
if [ ! -d "/var/log/gohttp" ]; then
echo "Creating /var/log/gohttp"
sudo mkdir -p /var/log/gohttp/
mkdir -p /var/log/gohttp/
fi
sudo chown "${uname}" -R /var/log/gohttp/
chown "${uname}" -R /var/log/gohttp/
echo "Tidying up Go HTTP Server Install"
if [ "${ARCH}" = "x86_64" ] ; then
@ -301,10 +308,10 @@ RestartSec=10
WantedBy=multi-user.target
EOF
)"
echo "${gohttpserver}" | sudo tee /etc/systemd/system/gohttpserver.service > /dev/null
sudo systemctl daemon-reload
sudo systemctl enable gohttpserver.service
sudo systemctl start gohttpserver.service
echo "${gohttpserver}" | tee /etc/systemd/system/gohttpserver.service > /dev/null
systemctl daemon-reload
systemctl enable gohttpserver.service
systemctl start gohttpserver.service
echo -e "Your IP/DNS Address is ${wanip}"