Easy-Guacamole-Installer/guac-optional-features/add-tls-guac-daemon.sh

105 lines
3 KiB
Bash
Raw Normal View History

2023-08-14 14:12:08 +10:00
#!/bin/bash
#######################################################################################################################
2023-08-21 01:27:46 +10:00
# Harden Guacd <-> Guac client traffic in TLS wrapper
2023-08-14 14:12:08 +10:00
# For Ubuntu / Debian / Raspbian
# David Harrop
# April 2023
#######################################################################################################################
2023-09-06 19:59:44 +10:00
# To delete and reissue a new cert
# sudo keytool -delete -alias guacd -noprompt -cacerts -storepass changeit -file guacd.crt
2023-08-14 14:12:08 +10:00
# Prepare text output colours
GREY='\033[0;37m'
DGREY='\033[0;90m'
GREYB='\033[1;37m'
LRED='\033[0;91m'
LGREEN='\033[0;92m'
LYELLOW='\033[0;93m'
NC='\033[0m' #No Colour
2023-09-06 19:59:44 +10:00
# Check if user is root or sudo
if ! [[ $(id -u) = 0 ]]; then
echo
2023-09-11 14:01:56 +10:00
echo -e "${LRED}Please run this script as sudo or root${NC}" 1>&2
exit 1
2023-09-06 19:59:44 +10:00
fi
TOMCAT_VERSION=$(ls /etc/ | grep tomcat)
RSA_KEY_LENGTH=2048
2023-09-11 14:01:56 +10:00
# Below variables are automatically updated by the 1-setup.sh script with the respective values given at install (manually update if blank)
2023-08-14 14:12:08 +10:00
CERT_COUNTRY=
CERT_STATE=
CERT_LOCATION=
CERT_ORG=
CERT_OU=
2023-09-06 19:59:44 +10:00
CERT_DAYS=
2023-08-14 14:12:08 +10:00
clear
2023-08-21 01:27:46 +10:00
# Create the special directory for guacd tls certificate and key.
2023-09-06 19:59:44 +10:00
mkdir -p /etc/guacamole/ssl
2023-08-14 14:12:08 +10:00
echo
2023-09-06 19:59:44 +10:00
cat <<EOF | tee cert_attributes.txt
2023-08-14 14:12:08 +10:00
[req]
distinguished_name = req_distinguished_name
x509_extensions = v3_req
prompt = no
string_mask = utf8only
[req_distinguished_name]
C = $CERT_COUNTRY
ST = $CERT_STATE
L = $CERT_LOCATION
O = $CERT_ORG
OU = $CERT_OU
CN = localhost
[v3_req]
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth, clientAuth, codeSigning, emailProtection
subjectAltName = @alt_names
[alt_names]
DNS.1 = localhost
IP.1 = 127.0.0.1
EOF
2023-08-21 01:27:46 +10:00
# Create the self signing request, certificate & key
2023-09-06 19:59:44 +10:00
openssl req -x509 -nodes -days $CERT_DAYS -newkey rsa:$RSA_KEY_LENGTH -keyout /etc/guacamole/ssl/guacd.key -out /etc/guacamole/ssl/guacd.crt -config cert_attributes.txt
2023-08-14 14:12:08 +10:00
rm -f cert_attributes.txt
2023-08-21 01:27:46 +10:00
# Point Guacamole config file to certificate and key
2023-09-06 19:59:44 +10:00
cp /etc/guacamole/guacd.conf /etc/guacamole/guacd.conf.bak
cat <<EOF | sudo tee /etc/guacamole/guacd.conf
2023-08-14 14:12:08 +10:00
[server]
bind_host = 127.0.0.1
bind_port = 4822
[ssl]
server_certificate = /etc/guacamole/ssl/guacd.crt
server_key = /etc/guacamole/ssl/guacd.key
EOF
2023-08-21 01:27:46 +10:00
# Enable TLS backend
2023-09-06 19:59:44 +10:00
cat <<EOF | sudo tee -a /etc/guacamole/guacamole.properties
2023-08-14 14:12:08 +10:00
guacd-ssl: true
EOF
# Fix required permissions as guacd only runs as daemon
2023-09-06 19:59:44 +10:00
chown daemon:daemon /etc/guacamole/ssl
chown daemon:daemon /etc/guacamole/ssl/guacd.key
chown daemon:daemon /etc/guacamole/ssl/guacd.crt
chmod 644 /etc/guacamole/ssl/guacd.crt
chmod 644 /etc/guacamole/ssl/guacd.key
2023-08-14 14:12:08 +10:00
# Add the new certificate into the Java Runtime certificate store and set JRE to trust it.
cd /etc/guacamole/ssl
2023-09-06 19:59:44 +10:00
keytool -importcert -alias guacd -noprompt -cacerts -storepass changeit -file guacd.crt
systemctl restart guacd
systemctl restart ${TOMCAT_VERSION}
2023-08-14 14:12:08 +10:00
echo
echo "Done!"
echo -e ${NC}