|
| 1 | +#!/bin/bash |
| 2 | +####################################################################################################################### |
| 3 | +# Add self signed SSL certificates to Guacamole with Nginx reverse proxy |
| 4 | +# For Ubuntu / Debian / Raspian |
| 5 | +# 4a of 4 |
| 6 | +# David Harrop |
| 7 | +# April 2023 |
| 8 | +####################################################################################################################### |
| 9 | + |
| 10 | +# To run manually and regenerate certificates, this script must be run in the current user enviroment [-E switch] |
| 11 | +# to provide certifacate outputs correctly. Runing just as sudo will save certs to sudo's home path |
| 12 | +# sudo -E ./4a-install-ssl-self-signed-nginx.sh [your-dns-name.local] [3650] |
| 13 | + |
| 14 | +# Hack to assist with displaying "$" symbols and " ' quotes in a (cut/pasteable) bash screen output format for Nginx configs |
| 15 | +SHOWASTEXT1='$mypwd' |
| 16 | +SHOWASTEXT2='"Cert:\LocalMachine\Root"' |
| 17 | + |
| 18 | +# Discover all IPv4 interfaces addresses to bind to new SSL certficates |
| 19 | +echo |
| 20 | + echo -e "${GREY}Discovering the default route interface and DNS names to bind with the new SSL certificate..." |
| 21 | + # Dump interface info and copy this output to a temp file |
| 22 | + DUMP_IPS=$(ip -o addr show up primary scope global | while read -r num dev fam addr rest; do echo ${addr%/*}; done) |
| 23 | + echo $DUMP_IPS > $TMP_DIR/dump_ips.txt |
| 24 | +
|
| 25 | + # Filter out anything but numerical characters, then add output to a temporary list |
| 26 | + grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" $TMP_DIR/dump_ips.txt > $TMP_DIR/ip_list.txt |
| 27 | +
|
| 28 | + # Separate each row in the temporary ip_list.txt file and further split each single row into a separate new temp file for each individual IP address found |
| 29 | + sed -n '1p' $TMP_DIR/ip_list.txt > $TMP_DIR/1st_ip.txt |
| 30 | + #sed -n '2p' $TMP_DIR/ip_list.txt > $TMP_DIR/2nd_ip.txt # uncomment for 2nd interface |
| 31 | + #sed -n '3p' $TMP_DIR/ip_list.txt > $TMP_DIR/3rd_ip.txt # uncomment for 3rd interface etc |
| 32 | +
|
| 33 | + # Assign each individual IP address temp file a discreet variable for use in the certificate parameters setup |
| 34 | + IP1=$(cat $TMP_DIR/1st_ip.txt) |
| 35 | + #IP2=$(cat $TMP_DIR/2nd_ip.txt) # uncomment for 2nd interface |
| 36 | + #IP3=$(cat $TMP_DIR/3rd_ip.txt) # uncomment for 3rd interface etc |
| 37 | +if [ $? -ne 0 ]; then |
| 38 | + echo -e "${RED}Failed. See ${LOG_LOCATION}${GREY}" 1>&2 |
| 39 | + exit 1 |
| 40 | + else |
| 41 | + echo -e "${GREEN}OK${GREY}" |
| 42 | + echo |
| 43 | +fi |
| 44 | +
|
| 45 | +echo |
| 46 | +echo -e "${GREY}New self signed SSL certificate attributes are shown below...${GREY}" |
| 47 | +echo -e "${DGREY}" |
| 48 | +
|
| 49 | +# Display the new SSL cert parameters. Prompt for change if required |
| 50 | +cat <<EOF | tee -a $TMP_DIR/cert_attributes.txt |
| 51 | +[req] |
| 52 | +distinguished_name = req_distinguished_name |
| 53 | +x509_extensions = v3_req |
| 54 | +prompt = no |
| 55 | +string_mask = utf8only |
| 56 | +
|
| 57 | +[req_distinguished_name] |
| 58 | +C = $CERT_COUNTRY |
| 59 | +ST = $CERT_STATE |
| 60 | +L = $CERT_LOCATION |
| 61 | +O = $CERT_ORG |
| 62 | +OU = $CERT_OU |
| 63 | +CN = $PROXY_SITE |
| 64 | +
|
| 65 | +[v3_req] |
| 66 | +keyUsage = nonRepudiation, digitalSignature, keyEncipherment |
| 67 | +extendedKeyUsage = serverAuth, clientAuth, codeSigning, emailProtection |
| 68 | +subjectAltName = @alt_names |
| 69 | +
|
| 70 | +[alt_names] |
| 71 | +DNS.1 = $PROXY_SITE |
| 72 | +IP.1 = $IP1 |
| 73 | +EOF |
| 74 | +# Add IP.2 & IP.3 into the above cat <<EOF as needed. |
| 75 | +#IP.2 = $IP3 |
| 76 | +#IP.3 = $IP3 |
| 77 | +# Additional DNS names can also be manually added into the above cat <<EOF as needed. |
| 78 | +#DNS.2 = |
| 79 | +#DNS.3 = |
| 80 | +
|
| 81 | +# Set default certificate file destinations. These can be adapted for any other SSL application. |
| 82 | +DIR_SSL_CERT="/etc/nginx/ssl/cert" |
| 83 | +DIR_SSL_KEY="/etc/nginx/ssl/private" |
| 84 | +
|
| 85 | +# Setup SSL certificate variables |
| 86 | +SSLNAME=$1 |
| 87 | +SSLDAYS=$2 |
| 88 | +
|
| 89 | +if [[ $SSLDAYS == "" ]]; then |
| 90 | +$SSLDAYS = 3650 |
| 91 | +fi |
| 92 | +
|
| 93 | +echo "Creating a new Certificate ..." |
| 94 | +openssl req -x509 -nodes -newkey rsa:2048 -keyout $SSLNAME.key -out $SSLNAME.crt -days $SSLDAYS -config $TMP_DIR/cert_attributes.txt |
| 95 | +
|
| 96 | +# Make directories to place SSL Certificate if they don't exist |
| 97 | +if [[ ! -d $DIR_SSL_KEY ]]; then |
| 98 | + sudo mkdir -p $DIR_SSL_KEY |
| 99 | +fi |
| 100 | +
|
| 101 | +if [[ ! -d $DIR_SSL_CERT ]]; then |
| 102 | + sudo mkdir -p $DIR_SSL_CERT |
| 103 | +fi |
| 104 | +
|
| 105 | +# Place SSL Certificate within defined path |
| 106 | + sudo cp $SSLNAME.key $DIR_SSL_KEY/$SSLNAME.key |
| 107 | + sudo cp $SSLNAME.crt $DIR_SSL_CERT/$SSLNAME.crt |
| 108 | +
|
| 109 | +# Create a PFX formatted key for easier import to Windows hosts and change permissions to enable copying elsewhere |
| 110 | + sudo openssl pkcs12 -export -out $SSLNAME.pfx -inkey $SSLNAME.key -in $SSLNAME.crt -password pass:1234 |
| 111 | + sudo chmod 0774 $SSLNAME.pfx |
| 112 | + echo -e "${GREY}Creating a selection of self signed certificates for Nginx and Windows/Linux browser clients...${GREY}" |
| 113 | +if [ $? -ne 0 ]; then |
| 114 | + echo -e "${RED}Failed. See ${LOG_LOCATION}${GREY}" 1>&2 |
| 115 | + exit 1 |
| 116 | + else |
| 117 | + echo -e "${GREEN}OK${GREY}" |
| 118 | + echo |
| 119 | +fi |
| 120 | +
|
| 121 | +# Backup the current Nginx config before update |
| 122 | +cp /etc/nginx/sites-enabled/${PROXY_SITE} $DOWNLOAD_DIR/${PROXY_SITE}-nginx.bak |
| 123 | +echo -e "${GREY}Backing up previous Nginx proxy to $DOWNLOAD_DIR/$PROXY_SITE-nginx.bak" |
| 124 | +if [ $? -ne 0 ]; then |
| 125 | + echo -e "${RED}Failed. See ${LOG_LOCATION}${GREY}" 1>&2 |
| 126 | + exit 1 |
| 127 | + else |
| 128 | + echo -e "${GREEN}OK${GREY}" |
| 129 | + echo |
| 130 | +fi |
| 131 | +
|
| 132 | +# Update Nginx config to accept the new certificates |
| 133 | +cat > /etc/nginx/sites-available/$PROXY_SITE <<EOL | > /dev/null |
| 134 | +server { |
| 135 | + #listen 80 default_server; |
| 136 | + root /var/www/html; |
| 137 | + index index.html index.htm index.nginx-debian.html; |
| 138 | + server_name $PROXY_SITE; |
| 139 | + location / { |
| 140 | + proxy_pass $GUAC_URL; |
| 141 | + proxy_buffering off; |
| 142 | + proxy_http_version 1.1; |
| 143 | + proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; |
| 144 | + proxy_set_header Upgrade \$http_upgrade; |
| 145 | + proxy_set_header Connection \$http_connection; |
| 146 | + access_log off; |
| 147 | + } |
| 148 | + listen 443 ssl; |
| 149 | + ssl_certificate /etc/nginx/ssl/cert/$SSLNAME.crt; |
| 150 | + ssl_certificate_key /etc/nginx/ssl/private/$SSLNAME.key; |
| 151 | + ssl_session_cache shared:SSL:1m; |
| 152 | + ssl_session_timeout 5m; |
| 153 | +} |
| 154 | +server { |
| 155 | + return 301 https://\$host\$request_uri; |
| 156 | + listen 80 default_server; |
| 157 | + root /var/www/html; |
| 158 | + index index.html index.htm index.nginx-debian.html; |
| 159 | + server_name $PROXY_SITE; |
| 160 | + location / { |
| 161 | + proxy_pass $GUAC_URL; |
| 162 | + proxy_buffering off; |
| 163 | + proxy_http_version 1.1; |
| 164 | + proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; |
| 165 | + proxy_set_header Upgrade \$http_upgrade; |
| 166 | + proxy_set_header Connection \$http_connection; |
| 167 | + access_log off; |
| 168 | + } |
| 169 | +} |
| 170 | +EOL |
| 171 | +
|
| 172 | +echo -e "${GREY}Configuring Nginx proxy to use self signed SSL certificates and setting up automatic HTTP to HTTPS redirect...${GREY}" |
| 173 | +if [ $? -ne 0 ]; then |
| 174 | + echo -e "${RED}Failed. See ${LOG_LOCATION}${GREY}" 1>&2 |
| 175 | + exit 1 |
| 176 | + else |
| 177 | + echo -e "${GREEN}OK${GREY}" |
| 178 | + echo |
| 179 | +fi |
| 180 | +
|
| 181 | +printf "${GREY}+------------------------------------------------------------------------------------------------------------- |
| 182 | +${GREEN}+ WINDOWS CLIENT SELF SIGNED SSL BROWSER CONFIG - SAVE THIS BEFORE CONTINUING!${GREY} |
| 183 | ++ |
| 184 | ++ 1. In ${DOWNLOAD_DIR} is a Windows friendly version of the new certificate ${LYELLOW}$SSLNAME.pfx${GREY} |
| 185 | ++ 2. Copy this .pfx file to a location accessible by Windows. |
| 186 | ++ 3. Import the PFX file into your Windows client with the below Powershell commands (as administrator): |
| 187 | +\n" |
| 188 | +echo -e "${SHOWASTEXT1} = ConvertTo-SecureString -String "1234" -Force -AsPlainText" |
| 189 | +echo -e "Import-pfxCertificate -FilePath $SSLNAME.pfx -Password "${SHOWASTEXT1}" -CertStoreLocation "${SHOWASTEXT2}"" |
| 190 | +echo -e "(Clear your browser cache and restart your browser to test.)" |
| 191 | +printf "${GREY}+------------------------------------------------------------------------------------------------------------- |
| 192 | +${GREEN}+ LINUX CLIENT SELF SIGNED SSL BROWSER CONFIG - SAVE THIS BEFORE CONTINUING!${GREY} |
| 193 | ++ |
| 194 | ++ 1. In In ${DOWNLOAD_DIR} is also the Linux native OpenSSL certificate ${LYELLOW}$SSLNAME.crt${GREY} |
| 195 | ++ 2. Copy this file to a location accessible by Linux. |
| 196 | ++ 3. Import the CRT file into your Linux client certificate store with the below command (as sudo): |
| 197 | +\n" |
| 198 | +echo -e "certutil -d sql:$HOME/.pki/nssdb -A -t "CT,C,c" -n $SSLNAME -i $SSLNAME.crt" |
| 199 | +echo -e "(If certutil is not installed, run apt-get install libnss3-tools)" |
| 200 | +printf "+-------------------------------------------------------------------------------------------------------------\n" |
| 201 | +echo |
| 202 | +echo -e "${LYELLOW}The above SSL browser config instructions are also saved in ${LGREEN}$LOG_LOCATION${GREY}" |
| 203 | +echo |
| 204 | +
|
| 205 | +# Reload everything |
| 206 | +echo -e "${GREY}Restaring Guacamole & Ngnix..." |
| 207 | +sudo systemctl restart $TOMCAT_VERSION |
| 208 | +sudo systemctl restart guacd |
| 209 | +sudo systemctl restart nginx |
| 210 | +if [ $? -ne 0 ]; then |
| 211 | + echo -e "${RED}Failed. See ${LOG_LOCATION}${GREY}" 1>&2 |
| 212 | + exit 1 |
| 213 | +else |
| 214 | + echo -e "${LGREEN}OK${GREY}" |
| 215 | + echo |
| 216 | +fi |
| 217 | +
|
| 218 | +# Done |
| 219 | +echo -e ${NC} |
0 commit comments