|
| 1 | +#!/bin/bash |
| 2 | +# info: Adding force SSL for a mail domain |
| 3 | +# options: USER DOMAIN |
| 4 | +# |
| 5 | +# The function forces SSL for the requested domain. |
| 6 | + |
| 7 | + |
| 8 | +#----------------------------------------------------------# |
| 9 | +# Variable&Function # |
| 10 | +#----------------------------------------------------------# |
| 11 | + |
| 12 | +# Argument definition |
| 13 | +user=$1 |
| 14 | +domain=$2 |
| 15 | + |
| 16 | +# Includes |
| 17 | +source $HESTIA/func/main.sh |
| 18 | +source $HESTIA/conf/hestia.conf |
| 19 | + |
| 20 | + |
| 21 | +#----------------------------------------------------------# |
| 22 | +# Verifications # |
| 23 | +#----------------------------------------------------------# |
| 24 | + |
| 25 | +check_args '2' "$#" 'USER DOMAIN' |
| 26 | +is_format_valid 'user' 'domain' 'ssl_dir' |
| 27 | +is_object_valid 'user' 'USER' "$user" |
| 28 | +is_object_unsuspended 'user' 'USER' "$user" |
| 29 | +is_object_valid 'mail' 'DOMAIN' "$domain" |
| 30 | +is_object_unsuspended 'mail' 'DOMAIN' "$domain" |
| 31 | + |
| 32 | + |
| 33 | +#----------------------------------------------------------# |
| 34 | +# Action # |
| 35 | +#----------------------------------------------------------# |
| 36 | + |
| 37 | +# Load domain data |
| 38 | +eval $(grep "DOMAIN='$domain'" $USER_DATA/mail.conf) |
| 39 | + |
| 40 | +# Check if SSL is enabled |
| 41 | +if [ "$SSL" != 'yes' ]; then |
| 42 | + echo "Error: SSL is not enabled" |
| 43 | + exit $E_NOTEXIST |
| 44 | +fi |
| 45 | + |
| 46 | +# Check if proxy is active |
| 47 | +if [ ! -z "$PROXY_SYSTEM" ] || [ ! -z "$PROXY" ]; then |
| 48 | + if ! grep --quiet "forcessl" $HESTIA/data/templates/mail/nginx/default.tpl; then |
| 49 | + $BIN/v-update-web-templates |
| 50 | + fi |
| 51 | + forcessl="/home/$user/conf/mail/$domain/forcessl.$PROXY_SYSTEM.conf" |
| 52 | +else |
| 53 | + if ! grep --quiet "forcessl" $HESTIA/data/templates/mail/nginx/default.tpl; then |
| 54 | + $BIN/v-update-web-templates |
| 55 | + fi |
| 56 | + forcessl="/home/$user/conf/mail/$domain/forcessl.$WEB_SYSTEM.conf" |
| 57 | +fi |
| 58 | + |
| 59 | +# Insert redirect commands |
| 60 | +if [ ! -z $PROXY ]; then |
| 61 | + echo 'return 301 https://$server_name$request_uri;' > $forcessl |
| 62 | +else |
| 63 | + echo 'RewriteEngine On' > $forcessl |
| 64 | + echo 'RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]' >> $forcessl |
| 65 | +fi |
| 66 | + |
| 67 | + |
| 68 | +#----------------------------------------------------------# |
| 69 | +# Hestia # |
| 70 | +#----------------------------------------------------------# |
| 71 | + |
| 72 | +# Restart web server |
| 73 | +$BIN/v-restart-web |
| 74 | +check_result $? "Web restart failed" > /dev/null |
| 75 | + |
| 76 | +# Restart proxy |
| 77 | +$BIN/v-restart-proxy |
| 78 | +check_result $? "Proxy restart failed" > /dev/null |
| 79 | + |
| 80 | +exit |
0 commit comments