Skip to content

Commit 3404fe8

Browse files
author
Kristan Kenney
committed
Include SSL enforcement in domain functions
1 parent c949809 commit 3404fe8

File tree

4 files changed

+58
-143
lines changed

4 files changed

+58
-143
lines changed

bin/v-add-mail-domain-ssl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ 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-
7976
# Increase value for domain
8077
increase_user_value "$user" '$U_MAIL_SSL'
8178

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

Lines changed: 0 additions & 77 deletions
This file was deleted.

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

Lines changed: 0 additions & 57 deletions
This file was deleted.

func/domain.sh

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -643,14 +643,25 @@ add_webmail_config() {
643643
fi
644644
if [ "$2" = "default.stpl" ]; then
645645
if [ ! -z "$WEB_SYSTEM" ]; then
646+
forcessl="$HOMEDIR/$user/conf/mail/$domain/$WEB_SYSTEM.forcessl.conf"
646647
rm -f /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
647648
ln -s $conf /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
648649
fi
649650
if [ ! -z "$PROXY_SYSTEM" ]; then
651+
forcessl="$HOMEDIR/$user/conf/mail/$domain/$PROXY_SYSTEM.forcessl.conf"
650652
rm -f /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
651653
ln -s $conf /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
652654
fi
653-
# Clear old configurations
655+
656+
# Add rewrite rules to force HTTPS/SSL connections
657+
if [ ! -z "$PROXY_SYSTEM" ]; then
658+
echo 'return 301 https://$server_name$request_uri;' > $forcessl
659+
else
660+
echo 'RewriteEngine On' > $forcessl
661+
echo 'RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]' >> $forcessl
662+
fi
663+
664+
# Remove old configurations
654665
rm -rf $HOMEDIR/$user/conf/mail/$domain.*
655666
rm -rf $HOMEDIR/$user/conf/mail/ssl.$domain.*
656667
rm -rf $HOMEDIR/$user/conf/mail/*nginx.$domain.*
@@ -714,6 +725,47 @@ add_mail_ssl_config() {
714725
chown -h $user:mail /usr/local/hestia/ssl/mail/*
715726
}
716727

728+
check_mail_ssl_config(){
729+
if [ -f $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.crt ]; then
730+
SSL='yes'
731+
else
732+
echo "Error: SSL certificate not available."
733+
exit $E_NOTEXIST
734+
fi
735+
}
736+
737+
repair_mail_ssl_config(){
738+
if [ -f $USER_DATA/ssl/mail.$domain.crt ]; then
739+
740+
# Ensure SSL directory exists
741+
if [ ! -d $HOMEDIR/$user/conf/mail/$domain/ssl/ ]; then
742+
mkdir -p $HOMEDIR/$user/conf/mail/$domain/ssl/
743+
fi
744+
745+
# Remove existing certificates
746+
rm -rf $HOMEDIR/$user/conf/mail/$domain/ssl/*
747+
748+
# Add certificates to user home directory
749+
cp -f $USER_DATA/ssl/mail.$domain.crt $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.crt
750+
cp -f $USER_DATA/ssl/mail.$domain.key $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.key
751+
cp -f $USER_DATA/ssl/mail.$domain.pem $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.pem
752+
753+
if [ -e "$USER_DATA/ssl/mail.$domain.ca" ]; then
754+
cp -f $USER_DATA/ssl/mail.$domain.ca $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.ca
755+
fi
756+
757+
# Increase value for domain
758+
increase_user_value "$user" '$U_MAIL_SSL'
759+
760+
# Set SSL as enabled in configuration
761+
update_object_value 'mail' 'DOMAIN' "$domain" '$SSL' "yes"
762+
763+
else
764+
echo "Error: no available SSL certificates for $domain."
765+
exit $E_NOTEXIST
766+
fi
767+
}
768+
717769
# Delete SSL support for mail domain
718770
del_mail_ssl_config() {
719771

@@ -724,7 +776,7 @@ del_mail_ssl_config() {
724776
rm -f /etc/dovecot/conf.d/domains/mail.$domain.conf
725777

726778
# Remove SSL vhost configuration
727-
rm -f $HOMEDIR/$user/conf/mail/$domain/*.ssl.conf
779+
rm -f $HOMEDIR/$user/conf/mail/$domain/*.*ssl.conf
728780
rm -f /etc/$WEB_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
729781
rm -f /etc/$PROXY_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
730782

@@ -742,16 +794,16 @@ del_mail_ssl_certificates(){
742794
# Delete webmail support
743795
del_webmail_config() {
744796
if [ ! -z "$WEB_SYSTEM" ]; then
745-
rm -f $HOMEDIR/$user/conf/mail/$domain/$WEB_SYSTEM.conf
797+
rm -f $HOMEDIR/$user/conf/mail/$domain/$WEB_SYSTEM.*conf
746798
rm -f /etc/$WEB_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
747-
rm -f $HOMEDIR/$user/conf/mail/$domain/$WEB_SYSTEM.ssl.conf
799+
rm -f $HOMEDIR/$user/conf/mail/$domain/$WEB_SYSTEM.*ssl.conf
748800
rm -f /etc/$WEB_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
749801
fi
750802

751803
if [ ! -z "$PROXY_SYSTEM" ]; then
752-
rm -f $HOMEDIR/$user/conf/mail/$domain/$PROXY_SYSTEM.conf
804+
rm -f $HOMEDIR/$user/conf/mail/$domain/$PROXY_SYSTEM.*conf
753805
rm -f /etc/$PROXY_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
754-
rm -f $HOMEDIR/$user/conf/mail/$domain/$PROXY_SYSTEM.ssl.conf
806+
rm -f $HOMEDIR/$user/conf/mail/$domain/$PROXY_SYSTEM.*ssl.conf
755807
rm -f /etc/$PROXY_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
756808
fi
757809
}

0 commit comments

Comments
 (0)