webinoly/lib/site-ssl
Cristhian Martínez Ochoa eb28ac2828 ssl improvements
- ssl for wpsubdom removed, wildcard is enough.
- ssl-root-path fixed.
- some minor code improvements.
2018-09-03 19:34:14 -06:00

147 lines
6.6 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# This is a library for Site Manager Plugin
# Functions for SSL On/Off
source /opt/webinoly/lib/general
site_ssl_on() {
local cermail=$(conf_read mail)
local root=$domain
# Some validations to prevent errors when creating certs.
if [[ $cache == "-root" && -n $value && -a /etc/nginx/sites-available/$value ]]; then
root="$value"
elif [[ $cache == "-root" && -n $value && ! -a /etc/nginx/sites-available/$value ]]; then
echo "${red}Root path domain is not a valid domain or is not found/hosted in this server!${end}"
exit 1
elif [[ $cache == "-root" && -z $value ]]; then
echo "${red}Please, enter a valid root path domain!${end}"
exit 1
fi
if [[ ! -d /var/www/$root/htdocs && $cache != "-root-path" ]]; then
echo "${red}Seems like you are trying to request an SSL Certificate for a Parked/Mapped Domain.${end}"
echo "${red}Please, use the '-root=domain.com' parameter to specify the main domain.${end}"
exit 1
fi
if [[ $cache == "-root-path" && ! -d $value ]]; then
echo "${red}[ERROR] Invalid root path!${end}"
exit 1
fi
# Check if Letsencrypt is installed
if [[ $(conf_read nginx-tool) != "true" || ! -a /usr/bin/letsencrypt ]]; then
echo "${red}[ERROR] Seems like Let's Encrypt tool is not installed!${end}"
exit 1
fi
echo "${gre}"
echo "*************************************************************************************************"
echo "** Please, be careful with the number of intents or certificates you try to get. **"
echo "** Lets Encrypt provides rate limits to ensure fair usage by as many people as possible. **"
echo "** **"
echo "** If you are getting errors or having issues when trying to get a new certificate **"
echo "** read about the Let's Encrypt rate limit - https://letsencrypt.org/docs/rate-limits/ **"
echo "** **"
echo "** Please, be sure your domain and www subdomain are currently pointing (DNS) to this server **"
echo "*************************************************************************************************${end}"
# We need an email to notify each renew intent (cron)
while [[ -z $cermail ]]
do
echo "${blu}"
read -p "Please, enter an email to register your new certificate: ${end}" cermail
if [[ "$cermail" =~ ^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$ ]]; then
conf_write mail $cermail
echo "${gre}Email address has been successfuly validated and saved! ${end}"
else
cermail=""
echo "${red}Please enter a valid email address!"
fi
echo "${end}"
done
# Create new certificate
local param="--email $cermail --no-eff-email --agree-tos --staple-ocsp --must-staple"
[[ $(conf_read debug) == "true" ]] && param="$param --test-cert"
[[ $subdomflag == 1 ]] && local domset="-d $domain" || local domset="-d $domain -d www.$domain"
# Wildcard
if [[ ! -a /etc/letsencrypt/live/$domain/fullchain.pem && $cache == "-wildcard" ]]; then
sudo certbot certonly --manual --preferred-challenges=dns --manual-public-ip-logging-ok -d $domain -d *.$domain $param
# Manual mode for Reverse Proxy sites
elif [[ ! -a /etc/letsencrypt/live/$domain/fullchain.pem && $cache == "-root-path" ]]; then
conf_write temp-path $value
sudo certbot certonly --manual --preferred-challenges=http --manual-auth-hook /opt/webinoly/lib/ex-ssl-authentication --manual-cleanup-hook /opt/webinoly/lib/ex-ssl-cleanup --manual-public-ip-logging-ok $domset $param
conf_delete temp-path
# Single cert
elif [[ ! -a /etc/letsencrypt/live/$domain/fullchain.pem ]]; then
sudo certbot certonly --webroot -w /var/www/$root/htdocs/ $domset $param
fi
# SSL Nginx Conf
if [[ -a /etc/letsencrypt/live/$domain/fullchain.pem ]]; then
sudo sed -i '/listen 80/c \ listen 443 ssl http2;' /etc/nginx/sites-available/$domain
sudo sed -i '/listen \[::\]:80/c \ listen [::]:443 ssl http2;' /etc/nginx/sites-available/$domain
sudo sed -i '/headers-http.conf/a \ include common/headers-https.conf;' /etc/nginx/sites-available/$domain
sudo sed -i '/server_name /r /opt/webinoly/templates/template-site-ssl' /etc/nginx/sites-available/$domain
sudo sed -i "/WebinolySSLstart/,/WebinolySSLend/{s/domain.com/$domain/}" /etc/nginx/sites-available/$domain
# HTTP to HTTPS Redirection
[[ $subdomflag == 1 ]] && local sername="server_name $domain;" || local sername="server_name $domain www.$domain;"
[[ $cache == "-wildcard" ]] && sername="server_name $domain *.$domain;"
sudo sed -i '1r /opt/webinoly/templates/template-site-sslredirect' /etc/nginx/sites-available/$domain
sudo sed -i "/#server_name;/c \ $sername" /etc/nginx/sites-available/$domain
# Auto-Renew Certificate
if [[ ! -a /var/spool/cron/crontabs/root ]]; then
sudo touch /var/spool/cron/crontabs/root
sudo chmod 600 /var/spool/cron/crontabs/root
sudo chown root:crontab /var/spool/cron/crontabs/root
fi
cronmail=$( sudo grep -F "MAILTO=" /var/spool/cron/crontabs/root )
cronrene=$( sudo grep -F "certbot renew" /var/spool/cron/crontabs/root )
[[ -z $cronmail && -n $cermail && -z $cronrene ]] && echo "MAILTO=${cermail}" | sudo tee -a /var/spool/cron/crontabs/root
[[ -z $cronrene ]] && echo '15 3 * * 7 certbot renew --post-hook "service nginx restart"' | sudo tee -a /var/spool/cron/crontabs/root
echo "${gre}SSL have been successfully enabled for site $domain!${end}"
else
echo "${red}"
echo "[ERROR] Unable to create the new certificate!"
echo "${end}"
fi
}
site_ssl_off() {
sudo sed -i '/listen 443/c \ listen 80;' /etc/nginx/sites-available/$domain
sudo sed -i '/listen \[::\]:443/c \ listen [::]:80;' /etc/nginx/sites-available/$domain
sudo sed -i '/headers-https.conf/d' /etc/nginx/sites-available/$domain
sudo sed -i '/WebinolySSLstart/,/WebinolySSLend/{/.*/d}' /etc/nginx/sites-available/$domain
sudo sed -i '/WebinolySSLredirectStart/,/WebinolySSLredirectEnd/{/.*/d}' /etc/nginx/sites-available/$domain
if [[ -n $value && $value == "force" ]]; then
answer=="N"
else
echo "${blu}"
echo "Do you want to delete and revoke this certificate [y/N]? "
while read -r -n 1 -s answer; do
answer=${answer:-n}
[[ $answer = [YyNn] ]] && break
done
echo "${end}"
fi
if [[ $answer == [Yy] ]]; then
[[ $(conf_read debug) == "true" ]] && local param="--test-cert" || local param=""
sudo certbot revoke --cert-path /etc/letsencrypt/live/$domain/cert.pem --delete-after-revoke $param
echo "${gre}"
echo "Certificate for your site $domain has been completely removed!"
echo "${end}"
fi
echo "${gre}SSL has been successfully disabled for site -${blu} $domain!${end}"
}