better setup script

This commit is contained in:
Claudio Maradonna 2020-07-31 16:18:50 +02:00
parent fe0aa9e412
commit dd4fc8d0b2
Signed by: claudiomaradonna
GPG Key ID: 0CBA58694C5680D9

View File

@ -1,81 +1,116 @@
#!/bin/bash #!/bin/bash
if [[ $EUID -ne 0 ]]; then read -p "Did you run this script on the remote host? " -n 1 -r
echo "This script must be run as root." echo
exit 1 if [[ ! $REPLY =~ ^[Yy]$ ]]
then
echo "Please rerun this script on the remote host as root user."
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi fi
echo "OK: Root user detected." read -p "Did you setup various targets and adjusted configurations as described in README?" -n 1 -r
echo "NEXT: Checking for apt-get executable." echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
APT_GET_CMD=$(which apt-get) then
echo "Please read README and rerun this script."
if [[ -z $APT_GET_CMD ]]; then [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
echo "No apt-get executable found. Exiting."
exit 1;
fi fi
echo "OK: apt-get found." REMOTE_USER="root"
echo "NEXT: Check and eventually install necessary packages."
REQUIRED_PKG="autossh" function check_if_running_as_root {
PKG_OK=$(dpkg-query -W --showformat='${Status}\n' $REQUIRED_PKG|grep "install ok installed") if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root."
exit 1
fi
echo Checking for $REQUIRED_PKG: $PKG_OK echo "OK: Root user detected."
}
if [ "" = "$PKG_OK" ]; then function check_necessary_packages {
echo "No $REQUIRED_PKG. Setting up $REQUIRED_PKG." echo "NEXT: Checking for valid package manager."
apt-get --yes install $REQUIRED_PKG
fi
echo "NEXT: Check for existence of autossh dedicated user." APT_GET_CMD=$(which apt-get)
YUM_CMD=$(which yum)
if [ ! id -u autossh >/dev/null 2>&1 ]; then if [[ ! -z $APT_GET_CMD ]]; then
echo "The user is missing so we will create for you." echo "OK: apt-get found."
useradd -m -s /bin/false autossh apt-get --yes install autossh
fi elif [[ ! -z $YUM_CMD ]]; then
echo "OK: yum found."
yum install autossh
else
echo "No valid package manager found. Exiting."
exit 1;
fi
if [ ! id -u autossh >/dev/null 2>&1 ]; then echo "OK: Autossh installed"
echo "There are some problems with user creation. Exiting." }
exit 1;
fi
echo "NEXT: Setup autossh home." function check_for_autossh_user {
echo "NEXT: Check for existence of autossh dedicated user."
mkdir -p "/home/autossh/.ssh" if [ ! id -u autossh >/dev/null 2>&1 ]; then
touch -a /home/autossh/.ssh/authorized_keys echo "The user is missing so we will create for you."
useradd -m -s /bin/false autossh
fi
if [ ! -s authorized_keys ]; then if [ ! id -u autossh >/dev/null 2>&1 ]; then
echo "WARNING: authorized_keys in setup folder seems empty so you should manually setup host authorized_keys or rerun this script." echo "There are some problems with user creation. Exiting."
fi exit 1;
cat authorized_keys >> /home/autossh/.ssh/authorized_keys fi
}
echo "OK: Files and content ready." function adjust_ssh_folder_for {
echo "NEXT: Setup file and folder permissions." homedir=$( getent passwd $REMOTE_USER | cut -d: -f6 )
chown -R autossh:autossh /home/autossh/.ssh echo "NEXT: Setup ${1} home: ${homedir}."
chmod 700 /home/autossh/.ssh
chmod 600 /home/autossh/.ssh/authorized_keys
echo "OK: File and folder permissions setup." mkdir -p "${homedir}/.ssh"
echo "NEXT: Checking for systemd." touch -a $homedir/.ssh/authorized_keys
SYSTEMCTL_CMD=$(which systemctl) if [ ! -s authorized_keys ]; then
if [[ ! -z $SYSTEMCTL_CMD ]]; then echo "WARNING: authorized_keys in setup folder seems empty so you should manually setup host authorized_keys or rerun this script."
echo "NEXT: Copy targets into /etc/default." fi
cat authorized_keys >> $homedir/.ssh/authorized_keys
cp -n targets/* /etc/default/ echo "OK: Files and content ready."
echo "NEXT: Setup file and folder permissions."
echo "OK: Targets copied." chown -R $1:$1 $homedir/.ssh
echo "NEXT: Setup systemd service." chmod 700 $homedir/.ssh
chmod 600 $homedir/.ssh/authorized_keys
cp secure-tunnel@.service /etc/systemd/system/ echo "OK: File and folder permissions setup."
systemctl daemon-reload }
echo "OK: Systemd service created." function setup_systemd_service_if_available {
else echo "NEXT: Checking for systemd."
echo "WARNING: No systemd installation found. You should manually setup an autossh service to keep tunnel alive."
fi SYSTEMCTL_CMD=$(which systemctl)
if [[ ! -z $SYSTEMCTL_CMD ]]; then
echo "NEXT: Copy targets into /etc/default."
cp -n targets/* /etc/default/
echo "OK: Targets copied."
echo "NEXT: Setup systemd service."
cp secure-tunnel@.service /etc/systemd/system/
systemctl daemon-reload
echo "OK: Systemd service created."
else
echo "WARNING: No systemd installation found. You should manually setup an autossh service to keep tunnel alive."
fi
}
check_if_running_as_root
check_necessary_packages
check_for_autossh_user
adjust_ssh_folder_for ${REMOTE_USER}
setup_systemd_service_if_available
echo "All done. What you need to do now:\n" echo "All done. What you need to do now:\n"
echo "- generate an ssh keypair with ssh-keygen for user autossh and push signature to the jump server" echo "- generate an ssh keypair with ssh-keygen for user autossh and push signature to the jump server"