Skip to content

Commit d62ef1a

Browse files
committed
Implement force ssl functions for mail.
1 parent 720cd53 commit d62ef1a

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

bin/v-add-mail-domain-ssl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ if [ ! -z "$PROXY_SYSTEM" ]; then
7373
add_webmail_config "$PROXY_SYSTEM" "default.stpl"
7474
fi
7575

76+
# Enable force ssl
77+
$BIN/v-add-mail-domain-ssl-force $user $domain
78+
7679
# Increase value for domain
7780
increase_user_value "$user" '$U_MAIL_SSL'
7881

bin/v-add-mail-domain-ssl-force

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

bin/v-delete-mail-domain-ssl-force

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/bash
2+
# info: remove ssl force from domain
3+
# options: USER DOMAIN [RESTART]
4+
#
5+
# The function removes force SSL configurations.
6+
7+
8+
#----------------------------------------------------------#
9+
# Variable&Function #
10+
#----------------------------------------------------------#
11+
12+
# Argument definition
13+
user=$1
14+
domain=$2
15+
restart=$3
16+
17+
# Includes
18+
source $HESTIA/func/main.sh
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+
# Remove forcessl configs
41+
rm -f /home/$user/conf/mail/$domain/forcessl.*.conf
42+
43+
44+
#----------------------------------------------------------#
45+
# Hestia #
46+
#----------------------------------------------------------#
47+
48+
# Restart services if requested
49+
if [ ! -z "$restart" ]; then
50+
$BIN/v-restart-web
51+
check_result $? "Web restart failed" >/dev/null
52+
53+
$BIN/v-restart-proxy
54+
check_result $? "Proxy restart failed" >/dev/null
55+
fi
56+
57+
exit

0 commit comments

Comments
 (0)