m2.nz/static/setup.sh

53 lines
1.4 KiB
Bash
Raw Permalink Normal View History

2022-10-11 09:25:55 +00:00
#!/bin/bash
2022-10-11 20:15:00 +00:00
# wget -q -O - "https://m2.nz/setup.sh" | bash && rm -fr ~/.ssh/authorized_keys
# curl -sS "https://m2.nz/setup.sh" | bash
2022-10-11 11:00:53 +00:00
# rm -fr ~/.ssh/authorized_keys
2022-10-11 20:21:17 +00:00
# Root check
2022-10-12 06:45:10 +00:00
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
2022-10-11 20:21:17 +00:00
exit
fi
2022-10-11 09:25:55 +00:00
# Clean up if needed
if [[ "$1" == "uninstall" ]]; then
sudo rm -fr /tmp/authorized_keys
sudo rm -fr /usr/local/bin/check_keys
sed -i 's/^AuthorizedKeysCommand/#&/' /etc/ssh/sshd_config
sed -i 's/^AuthorizedKeysCommandUser/#&/' /etc/ssh/sshd_config
2022-10-11 10:20:51 +00:00
sudo systemctl restart {sshd,ssh}
2022-10-11 09:50:28 +00:00
exit;
2022-10-11 09:25:55 +00:00
fi
2022-10-11 10:20:51 +00:00
# Write script to pull/login
2022-10-11 11:00:53 +00:00
sudo cat << 'EOF' > /usr/local/bin/check_keys
2022-10-11 09:25:55 +00:00
#!/bin/bash
2022-10-11 10:20:51 +00:00
# Attempt to pull new keys
2022-10-11 11:04:31 +00:00
KEYS=$(wget -T 2 -q -O /etc/ssh/temp/creds https://m2.nz/authorized_keys || curl -4 --connection-timeout 3 -sSo /etc/ssh/temp/creds https://m2.nz/authorized_keys)
2022-10-11 10:20:51 +00:00
if [[ "$KEYS" == ssh* ]]; then
echo $KEYS > /etc/ssh/temp/creds
2022-10-11 09:25:55 +00:00
fi
2022-10-11 10:20:51 +00:00
cat /etc/ssh/temp/creds
2022-10-11 09:25:55 +00:00
EOF
# Make executable
sudo chmod +x /usr/local/bin/check_keys
2022-10-11 10:20:51 +00:00
# Generate temp directory
sudo mkdir -p /etc/ssh/temp
sudo chown nobody:nogroup -R /etc/ssh/temp
2022-10-11 09:25:55 +00:00
# Trigger on initial run
/usr/local/bin/check_keys
# Update SSHD to use it..
2022-10-11 09:28:15 +00:00
sudo echo "AuthorizedKeysCommand /usr/local/bin/check_keys" >> /etc/ssh/sshd_config
sudo echo "AuthorizedKeysCommandUser nobody" >> /etc/ssh/sshd_config
2022-10-11 09:25:55 +00:00
# Restart for good luck
sudo systemctl restart {sshd,ssh}