2020-10-21 16:16:32 +00:00
#! /bin/bash
2015-03-28 14:59:15 +00:00
2016-12-01 14:45:40 +00:00
##########################################################################
2021-01-16 09:16:05 +00:00
# >> SETUP DEFAULT VALUES
2016-12-01 14:45:40 +00:00
##########################################################################
2020-09-23 08:21:37 +00:00
2021-01-16 09:16:05 +00:00
DOVECOT_MAILBOX_FORMAT = " ${ DOVECOT_MAILBOX_FORMAT : =maildir } "
DOVECOT_TLS = " ${ DOVECOT_TLS : =no } "
ENABLE_CLAMAV = " ${ ENABLE_CLAMAV : =0 } "
ENABLE_FAIL2BAN = " ${ ENABLE_FAIL2BAN : =0 } "
ENABLE_FETCHMAIL = " ${ ENABLE_FETCHMAIL : =0 } "
ENABLE_LDAP = " ${ ENABLE_LDAP : =0 } "
ENABLE_MANAGESIEVE = " ${ ENABLE_MANAGESIEVE : =0 } "
ENABLE_POP3 = " ${ ENABLE_POP3 : =0 } "
ENABLE_POSTGREY = " ${ ENABLE_POSTGREY : =0 } "
ENABLE_QUOTAS = " ${ ENABLE_QUOTAS : =1 } "
ENABLE_SASLAUTHD = " ${ ENABLE_SASLAUTHD : =0 } "
ENABLE_SPAMASSASSIN = " ${ ENABLE_SPAMASSASSIN : =0 } "
ENABLE_SRS = " ${ ENABLE_SRS : =0 } "
FETCHMAIL_POLL = " ${ FETCHMAIL_POLL : =300 } "
2021-01-17 09:39:09 +00:00
FETCHMAIL_PARALLEL = " ${ FETCHMAIL_PARALLEL : =0 } "
2021-01-16 09:16:05 +00:00
LDAP_START_TLS = " ${ LDAP_START_TLS : =no } "
LOGROTATE_INTERVAL = " ${ LOGROTATE_INTERVAL : = ${ REPORT_INTERVAL :- daily } } "
LOGWATCH_INTERVAL = " ${ LOGWATCH_INTERVAL : =none } "
MOVE_SPAM_TO_JUNK = " ${ MOVE_SPAM_TO_JUNK : =1 } "
NETWORK_INTERFACE = " ${ NETWORK_INTERFACE : =eth0 } "
ONE_DIR = " ${ ONE_DIR : =0 } "
OVERRIDE_HOSTNAME = " ${ OVERRIDE_HOSTNAME } "
POSTGREY_AUTO_WHITELIST_CLIENTS = " ${ POSTGREY_AUTO_WHITELIST_CLIENTS : =5 } "
POSTGREY_DELAY = " ${ POSTGREY_DELAY : =300 } "
POSTGREY_MAX_AGE = " ${ POSTGREY_MAX_AGE : =35 } "
POSTGREY_TEXT = " ${ POSTGREY_TEXT : =Delayed by Postgrey } "
POSTFIX_INET_PROTOCOLS = " ${ POSTFIX_INET_PROTOCOLS : =all } "
POSTFIX_MAILBOX_SIZE_LIMIT = " ${ POSTFIX_MAILBOX_SIZE_LIMIT : =0 } " # no limit by default
POSTFIX_MESSAGE_SIZE_LIMIT = " ${ POSTFIX_MESSAGE_SIZE_LIMIT : =10240000 } " # ~10 MB by default
POSTSCREEN_ACTION = " ${ POSTSCREEN_ACTION : =enforce } "
REPORT_RECIPIENT = " ${ REPORT_RECIPIENT : = "0" } "
SMTP_ONLY = " ${ SMTP_ONLY : =0 } "
SPAMASSASSIN_SPAM_TO_INBOX_IS_SET = " $( if [ [ -n ${ SPAMASSASSIN_SPAM_TO_INBOX + 'set' } ] ] ; then echo true ; else echo false ; fi ) "
SPAMASSASSIN_SPAM_TO_INBOX = " ${ SPAMASSASSIN_SPAM_TO_INBOX : =0 } "
SPOOF_PROTECTION = " ${ SPOOF_PROTECTION : =0 } "
SRS_SENDER_CLASSES = " ${ SRS_SENDER_CLASSES : =envelope_sender } "
2021-01-25 23:09:58 +00:00
SSL_TYPE = " ${ SSL_TYPE : = } "
2021-01-22 09:03:31 +00:00
SUPERVISOR_LOGLEVEL = " ${ SUPERVISOR_LOGLEVEL : =warn } "
2021-01-16 09:16:05 +00:00
TLS_LEVEL = " ${ TLS_LEVEL : =modern } "
VIRUSMAILS_DELETE_DELAY = " ${ VIRUSMAILS_DELETE_DELAY : =7 } "
2018-04-04 16:48:55 +00:00
2016-12-01 14:45:40 +00:00
##########################################################################
2021-01-16 09:16:05 +00:00
# >> GLOBAL VARIABLES
2017-01-09 22:49:46 +00:00
##########################################################################
2020-09-23 08:21:37 +00:00
2017-01-09 22:49:46 +00:00
HOSTNAME = " $( hostname -f) "
DOMAINNAME = " $( hostname -d) "
2019-08-01 07:58:22 +00:00
CHKSUM_FILE = /tmp/docker-mailserver-config-chksum
2020-09-23 08:21:37 +00:00
2016-12-01 14:45:40 +00:00
##########################################################################
# >> REGISTER FUNCTIONS
2016-10-30 11:42:29 +00:00
#
2021-01-16 09:16:05 +00:00
# Add your new functions/methods here.
2016-10-08 17:02:47 +00:00
#
2021-01-16 09:16:05 +00:00
# NOTE: Position matters when registering a function in stacks.
# First in First out
#
# Execution Logic:
# > check functions
# > setup functions
# > fix functions
# > misc functions
# > start-daemons
2016-10-30 13:11:36 +00:00
#
2017-02-07 19:53:34 +00:00
# Example:
2021-01-16 09:16:05 +00:00
#
2020-09-23 08:21:37 +00:00
# if [[ CONDITION IS MET ]]
# then
2021-01-16 09:16:05 +00:00
# _register_{setup,fix,check,start}_{functions,daemons} "${FUNCNAME}"
2016-12-01 14:45:40 +00:00
# fi
2016-10-30 13:11:36 +00:00
#
2021-01-16 09:16:05 +00:00
# Implement them in the section-group: {check, setup, fix, start}
#
2016-12-01 14:45:40 +00:00
##########################################################################
2020-10-02 13:45:57 +00:00
function register_functions
2020-09-23 08:21:37 +00:00
{
2021-01-16 09:16:05 +00:00
_notify 'tasklog' 'Initializing setup'
_notify 'task' 'Registering check, setup, fix, misc and start-daemons functions'
2016-12-01 14:45:40 +00:00
2020-09-23 08:21:37 +00:00
################### >> check funcs
2016-12-01 14:45:40 +00:00
2020-09-23 08:21:37 +00:00
_register_check_function "_check_hostname"
2016-12-01 14:45:40 +00:00
2020-09-23 08:21:37 +00:00
################### >> setup funcs
2016-12-01 14:45:40 +00:00
2021-01-22 09:03:31 +00:00
_register_setup_function "_setup_supervisor"
2020-09-23 08:21:37 +00:00
_register_setup_function "_setup_default_vars"
_register_setup_function "_setup_file_permissions"
if [ [ ${ SMTP_ONLY } -ne 1 ] ]
then
_register_setup_function "_setup_dovecot"
2020-05-02 20:38:37 +00:00
_register_setup_function "_setup_dovecot_dhparam"
2020-09-23 08:21:37 +00:00
_register_setup_function "_setup_dovecot_quota"
_register_setup_function "_setup_dovecot_local_user"
2020-03-19 08:35:25 +00:00
fi
2018-03-18 19:15:06 +00:00
2020-09-23 08:21:37 +00:00
[ [ ${ ENABLE_LDAP } -eq 1 ] ] && _register_setup_function "_setup_ldap"
[ [ ${ ENABLE_SASLAUTHD } -eq 1 ] ] && _register_setup_function "_setup_saslauthd"
[ [ ${ ENABLE_POSTGREY } -eq 1 ] ] && _register_setup_function "_setup_postgrey"
2016-12-23 22:56:39 +00:00
2020-09-23 08:21:37 +00:00
_register_setup_function "_setup_dkim"
_register_setup_function "_setup_ssl"
2018-04-02 08:45:58 +00:00
2020-09-23 08:21:37 +00:00
[ [ ${ POSTFIX_INET_PROTOCOLS } != "all" ] ] && _register_setup_function "_setup_inet_protocols"
2019-01-19 10:10:31 +00:00
2020-09-23 08:21:37 +00:00
_register_setup_function "_setup_docker_permit"
2016-12-01 14:45:40 +00:00
2020-09-23 08:21:37 +00:00
_register_setup_function "_setup_mailname"
_register_setup_function "_setup_amavis"
_register_setup_function "_setup_dmarc_hostname"
_register_setup_function "_setup_postfix_hostname"
_register_setup_function "_setup_dovecot_hostname"
2017-01-09 22:52:36 +00:00
2020-09-23 08:21:37 +00:00
_register_setup_function "_setup_postfix_smtputf8"
_register_setup_function "_setup_postfix_sasl"
_register_setup_function "_setup_postfix_sasl_password"
_register_setup_function "_setup_security_stack"
_register_setup_function "_setup_postfix_aliases"
_register_setup_function "_setup_postfix_vhost"
_register_setup_function "_setup_postfix_dhparam"
_register_setup_function "_setup_postfix_postscreen"
_register_setup_function "_setup_postfix_sizelimits"
2019-09-15 16:29:46 +00:00
2021-01-19 11:31:45 +00:00
# needs to come after _setup_postfix_aliases
2020-09-23 08:21:37 +00:00
[ [ ${ SPOOF_PROTECTION } -eq 1 ] ] && _register_setup_function "_setup_spoof_protection"
2018-03-18 18:52:28 +00:00
2020-09-23 08:21:37 +00:00
if [ [ ${ ENABLE_SRS } -eq 1 ] ]
then
_register_setup_function "_setup_SRS"
_register_start_daemon "_start_daemons_postsrsd"
fi
2019-09-15 13:40:05 +00:00
2020-09-23 08:21:37 +00:00
_register_setup_function "_setup_postfix_access_control"
2020-03-19 08:35:25 +00:00
2021-01-16 09:16:05 +00:00
[ [ -n ${ DEFAULT_RELAY_HOST :- '' } ] ] && _register_setup_function "_setup_postfix_default_relay_host"
[ [ -n ${ RELAY_HOST :- '' } ] ] && _register_setup_function "_setup_postfix_relay_hosts"
[ [ ${ ENABLE_POSTFIX_VIRTUAL_TRANSPORT :- 0 } -eq 1 ] ] && _register_setup_function "_setup_postfix_virtual_transport"
2019-08-01 10:05:48 +00:00
2020-09-23 08:21:37 +00:00
_register_setup_function "_setup_postfix_override_configuration"
_register_setup_function "_setup_environment"
_register_setup_function "_setup_logrotate"
2016-12-01 14:45:40 +00:00
2020-10-21 17:45:47 +00:00
_register_setup_function "_setup_mail_summary"
_register_setup_function "_setup_logwatch"
2016-12-01 14:45:40 +00:00
2020-09-23 08:21:37 +00:00
_register_setup_function "_setup_user_patches"
2016-12-01 14:45:40 +00:00
2021-01-16 09:16:05 +00:00
# compute last as the config files are modified in-place
2020-09-23 08:21:37 +00:00
_register_setup_function "_setup_chksum_file"
2016-12-01 14:45:40 +00:00
2020-09-23 08:21:37 +00:00
################### >> fix funcs
2017-02-07 19:53:34 +00:00
2020-09-23 08:21:37 +00:00
_register_fix_function "_fix_var_mail_permissions"
_register_fix_function "_fix_var_amavis_permissions"
2016-12-01 14:45:40 +00:00
2020-09-23 08:21:37 +00:00
[ [ ${ ENABLE_CLAMAV } -eq 0 ] ] && _register_fix_function "_fix_cleanup_clamav"
[ [ ${ ENABLE_SPAMASSASSIN } -eq 0 ] ] && _register_fix_function "_fix_cleanup_spamassassin"
2016-12-01 14:45:40 +00:00
2020-09-23 08:21:37 +00:00
################### >> misc funcs
2016-12-01 14:45:40 +00:00
2020-09-23 08:21:37 +00:00
_register_misc_function "_misc_save_states"
2017-02-06 09:21:18 +00:00
2020-09-23 08:21:37 +00:00
################### >> daemon funcs
2016-12-01 14:45:40 +00:00
2020-09-23 08:21:37 +00:00
_register_start_daemon "_start_daemons_cron"
_register_start_daemon "_start_daemons_rsyslog"
2016-12-01 14:45:40 +00:00
2020-09-23 08:21:37 +00:00
[ [ ${ SMTP_ONLY } -ne 1 ] ] && _register_start_daemon "_start_daemons_dovecot"
2016-12-01 14:45:40 +00:00
2020-09-23 08:21:37 +00:00
# needs to be started before saslauthd
_register_start_daemon "_start_daemons_opendkim"
_register_start_daemon "_start_daemons_opendmarc"
2016-12-01 14:45:40 +00:00
2020-09-23 08:21:37 +00:00
#postfix uses postgrey, needs to be started before postfix
[ [ ${ ENABLE_POSTGREY } -eq 1 ] ] && _register_start_daemon "_start_daemons_postgrey"
_register_start_daemon "_start_daemons_postfix"
[ [ ${ ENABLE_SASLAUTHD } -eq 1 ] ] && _register_start_daemon "_start_daemons_saslauthd"
2021-01-16 09:16:05 +00:00
# care :: needs to run after postfix
[ [ ${ ENABLE_FAIL2BAN } -eq 1 ] ] && _register_start_daemon "_start_daemons_fail2ban"
2020-09-23 08:21:37 +00:00
[ [ ${ ENABLE_FETCHMAIL } -eq 1 ] ] && _register_start_daemon "_start_daemons_fetchmail"
[ [ ${ ENABLE_CLAMAV } -eq 1 ] ] && _register_start_daemon "_start_daemons_clamav"
[ [ ${ ENABLE_LDAP } -eq 0 ] ] && _register_start_daemon "_start_changedetector"
2017-02-06 09:21:18 +00:00
2020-09-23 08:21:37 +00:00
_register_start_daemon "_start_daemons_amavis"
2016-12-01 14:45:40 +00:00
}
2020-09-23 08:21:37 +00:00
2016-12-01 14:45:40 +00:00
##########################################################################
# << REGISTER FUNCTIONS
##########################################################################
2021-01-16 09:16:05 +00:00
# ! – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
# ! – – – CARE – BEGIN – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
# ! – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
2016-12-01 14:45:40 +00:00
##########################################################################
# >> CONSTANTS
##########################################################################
2020-09-23 08:21:37 +00:00
2016-12-01 14:45:40 +00:00
declare -a FUNCS_SETUP
declare -a FUNCS_FIX
declare -a FUNCS_CHECK
declare -a FUNCS_MISC
declare -a DAEMONS_START
2020-09-23 08:21:37 +00:00
2016-12-01 14:45:40 +00:00
##########################################################################
# << CONSTANTS
##########################################################################
##########################################################################
# >> protected register_functions
##########################################################################
2020-09-23 08:21:37 +00:00
2020-10-02 13:45:57 +00:00
function _register_start_daemon
2020-09-23 08:21:37 +00:00
{
DAEMONS_START += ( " ${ 1 } " )
_notify 'inf' " ${ 1 } () registered "
2016-12-01 14:45:40 +00:00
}
2020-10-02 13:45:57 +00:00
function _register_setup_function
2020-09-23 08:21:37 +00:00
{
FUNCS_SETUP += ( " ${ 1 } " )
_notify 'inf' " ${ 1 } () registered "
2016-12-01 14:45:40 +00:00
}
2020-10-02 13:45:57 +00:00
function _register_fix_function
2020-09-23 08:21:37 +00:00
{
FUNCS_FIX += ( " ${ 1 } " )
_notify 'inf' " ${ 1 } () registered "
2016-12-01 14:45:40 +00:00
}
2020-10-02 13:45:57 +00:00
function _register_check_function
2020-09-23 08:21:37 +00:00
{
FUNCS_CHECK += ( " ${ 1 } " )
_notify 'inf' " ${ 1 } () registered "
2016-12-01 14:45:40 +00:00
}
2020-10-02 13:45:57 +00:00
function _register_misc_function
2020-09-23 08:21:37 +00:00
{
FUNCS_MISC += ( " ${ 1 } " )
_notify 'inf' " ${ 1 } () registered "
2016-12-01 14:45:40 +00:00
}
2020-09-23 08:21:37 +00:00
2016-12-01 14:45:40 +00:00
##########################################################################
# << protected register_functions
##########################################################################
2020-10-02 13:45:57 +00:00
function _defunc
2020-09-23 08:21:37 +00:00
{
_notify 'fatal' "Please fix your configuration. Exiting..."
exit 1
2016-12-01 14:45:40 +00:00
}
2020-10-02 13:45:57 +00:00
function display_startup_daemon
2020-09-23 08:21:37 +00:00
{
${ 1 } & >/dev/null
local RES = ${ ? }
2021-01-16 09:16:05 +00:00
if [ [ ${ DMS_DEBUG } -eq 1 ] ]
2020-09-23 08:21:37 +00:00
then
if [ [ ${ RES } -eq 0 ] ]
then
2021-01-16 09:16:05 +00:00
_notify 'inf' " OK"
2020-09-23 08:21:37 +00:00
else
2021-01-16 09:16:05 +00:00
_notify 'err' " STARTUP FAILED"
2020-09-23 08:21:37 +00:00
fi
2016-12-23 22:56:39 +00:00
fi
2020-09-23 08:21:37 +00:00
return " ${ RES } "
2016-12-23 22:56:39 +00:00
}
2021-01-16 09:16:05 +00:00
# ! – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
# ! – – – CARE – END – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
# ! – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
2015-08-26 08:04:07 +00:00
2016-12-01 14:45:40 +00:00
##########################################################################
# >> Check Stack
2016-10-30 13:11:36 +00:00
#
2016-12-01 14:45:40 +00:00
# Description: Place functions for initial check of container sanity
##########################################################################
2020-09-23 08:21:37 +00:00
2020-10-02 13:45:57 +00:00
function check
2020-09-23 08:21:37 +00:00
{
2021-01-16 09:16:05 +00:00
_notify 'tasklog' 'Checking configuration'
2020-09-23 08:21:37 +00:00
for FUNC in " ${ FUNCS_CHECK [@] } "
do
if ! ${ FUNC }
then
_defunc
fi
done
2016-12-01 14:45:40 +00:00
}
2020-10-02 13:45:57 +00:00
function _check_hostname
2020-09-23 08:21:37 +00:00
{
2021-02-14 20:19:58 +00:00
_notify "task" " Check that hostname/domainname is provided or overridden [in ${ FUNCNAME [0] } ] "
2017-01-20 22:30:29 +00:00
2021-01-16 09:16:05 +00:00
if [ [ -n ${ OVERRIDE_HOSTNAME } ] ]
2020-09-23 08:21:37 +00:00
then
2021-01-16 09:16:05 +00:00
export HOSTNAME = ${ OVERRIDE_HOSTNAME }
2020-09-23 08:21:37 +00:00
export DOMAINNAME = " ${ HOSTNAME #*. } "
fi
2016-12-01 14:45:40 +00:00
2020-09-23 08:21:37 +00:00
_notify 'inf' " Domain has been set to ${ DOMAINNAME } "
_notify 'inf' " Hostname has been set to ${ HOSTNAME } "
2018-11-11 19:46:53 +00:00
2021-02-14 20:19:58 +00:00
if ! grep -q -E '^(\S+[.]\S+)$' <<< " ${ HOSTNAME } "
2020-09-23 08:21:37 +00:00
then
_notify 'err' "Setting hostname/domainname is required"
2020-09-23 19:53:07 +00:00
kill " $( < /var/run/supervisord.pid) " && return 1
2020-09-23 08:21:37 +00:00
fi
2016-12-01 14:45:40 +00:00
}
##########################################################################
# << Check Stack
##########################################################################
##########################################################################
# >> Setup Stack
2016-10-30 13:11:36 +00:00
#
2016-12-01 14:45:40 +00:00
# Description: Place functions for functional configurations here
##########################################################################
2020-09-23 08:21:37 +00:00
2020-10-02 13:45:57 +00:00
function setup
2020-09-23 08:21:37 +00:00
{
2021-01-16 09:16:05 +00:00
_notify 'tasklog' 'Configuring mail server'
2020-09-23 08:21:37 +00:00
for FUNC in " ${ FUNCS_SETUP [@] } "
do
${ FUNC }
done
}
2021-01-22 09:03:31 +00:00
function _setup_supervisor
{
2021-02-01 17:39:05 +00:00
if ! grep -q " loglevel = ${ SUPERVISOR_LOGLEVEL } " /etc/supervisor/supervisord.conf
then
case ${ SUPERVISOR_LOGLEVEL } in
critical | error | warn | info | debug )
sed -i -E \
2021-02-21 22:59:59 +00:00
" s|loglevel.*|loglevel = ${ SUPERVISOR_LOGLEVEL } |g " \
2021-02-01 17:39:05 +00:00
/etc/supervisor/supervisord.conf
; ;
* )
_notify 'error' \
" SUPERVISOR_LOGLEVEL value ' ${ SUPERVISOR_LOGLEVEL } ' unknown. Defaulting to 'warn' "
sed -i -E \
2021-02-21 22:59:59 +00:00
"s|loglevel.*|loglevel = warn|g" \
2021-02-01 17:39:05 +00:00
/etc/supervisor/supervisord.conf
; ;
esac
supervisorctl reload
fi
2021-01-22 09:03:31 +00:00
}
2020-10-02 13:45:57 +00:00
function _setup_default_vars
2020-09-23 08:21:37 +00:00
{
2021-01-16 09:16:05 +00:00
_notify 'task' "Setting up default variables"
2020-09-23 08:21:37 +00:00
# update POSTMASTER_ADDRESS - must be done done after _check_hostname
2021-01-16 09:16:05 +00:00
POSTMASTER_ADDRESS = " ${ POSTMASTER_ADDRESS : = " postmaster@ ${ DOMAINNAME } " } "
2020-09-23 08:21:37 +00:00
# update REPORT_SENDER - must be done done after _check_hostname
2021-01-16 09:16:05 +00:00
REPORT_SENDER = " ${ REPORT_SENDER : = " mailserver-report@ ${ HOSTNAME } " } "
PFLOGSUMM_SENDER = " ${ PFLOGSUMM_SENDER : = ${ REPORT_SENDER } } "
2020-09-23 08:21:37 +00:00
# set PFLOGSUMM_TRIGGER here for backwards compatibility
# when REPORT_RECIPIENT is on the old method should be used
2021-01-16 09:16:05 +00:00
# ! needs to be a string comparison
2020-10-17 20:17:59 +00:00
if [ [ ${ REPORT_RECIPIENT } = = "0" ] ]
2020-09-23 08:21:37 +00:00
then
2021-01-22 09:03:31 +00:00
PFLOGSUMM_TRIGGER = " ${ PFLOGSUMM_TRIGGER : =none } "
2020-09-23 08:21:37 +00:00
else
2021-01-22 09:03:31 +00:00
PFLOGSUMM_TRIGGER = " ${ PFLOGSUMM_TRIGGER : =logrotate } "
2020-09-23 08:21:37 +00:00
fi
# expand address to simplify the rest of the script
2021-02-22 21:17:01 +00:00
if [ [ ${ REPORT_RECIPIENT } = = "0" ] ] || [ [ ${ REPORT_RECIPIENT } = = "1" ] ]
2020-09-23 08:21:37 +00:00
then
REPORT_RECIPIENT = " ${ POSTMASTER_ADDRESS } "
2021-01-16 09:16:05 +00:00
fi
PFLOGSUMM_RECIPIENT = " ${ PFLOGSUMM_RECIPIENT : = ${ REPORT_RECIPIENT } } "
LOGWATCH_RECIPIENT = " ${ LOGWATCH_RECIPIENT : = ${ REPORT_RECIPIENT } } "
{
2021-01-26 11:20:35 +00:00
echo " export DOVECOT_MAILBOX_FORMAT=' ${ DOVECOT_MAILBOX_FORMAT } ' "
echo " export DOVECOT_TLS=' ${ DOVECOT_TLS } ' "
echo " export ENABLE_CLAMAV=' ${ ENABLE_CLAMAV } ' "
echo " export ENABLE_FAIL2BAN=' ${ ENABLE_FAIL2BAN } ' "
echo " export ENABLE_FETCHMAIL=' ${ ENABLE_FETCHMAIL } ' "
echo " export ENABLE_LDAP=' ${ ENABLE_LDAP } ' "
echo " export ENABLE_MANAGESIEVE=' ${ ENABLE_MANAGESIEVE } ' "
echo " export ENABLE_POP3=' ${ ENABLE_POP3 } ' "
echo " export ENABLE_POSTGREY=' ${ ENABLE_POSTGREY } ' "
echo " export ENABLE_QUOTAS=' ${ ENABLE_QUOTAS } ' "
echo " export ENABLE_SASLAUTHD=' ${ ENABLE_SASLAUTHD } ' "
echo " export ENABLE_SPAMASSASSIN=' ${ ENABLE_SPAMASSASSIN } ' "
echo " export ENABLE_SRS=' ${ ENABLE_SRS } ' "
echo " export FETCHMAIL_POLL=' ${ FETCHMAIL_POLL } ' "
echo " export FETCHMAIL_PARALLEL=' ${ FETCHMAIL_PARALLEL } ' "
echo " export LDAP_START_TLS=' ${ LDAP_START_TLS } ' "
echo " export LOGROTATE_INTERVAL=' ${ LOGROTATE_INTERVAL } ' "
echo " export LOGWATCH_INTERVAL=' ${ LOGWATCH_INTERVAL } ' "
2021-01-27 12:35:55 +00:00
echo " export LOGWATCH_RECIPIENT=' ${ LOGWATCH_RECIPIENT } ' "
2021-01-26 11:20:35 +00:00
echo " export MOVE_SPAM_TO_JUNK=' ${ MOVE_SPAM_TO_JUNK } ' "
echo " export NETWORK_INTERFACE=' ${ NETWORK_INTERFACE } ' "
echo " export ONE_DIR=' ${ ONE_DIR } ' "
echo " export OVERRIDE_HOSTNAME=' ${ OVERRIDE_HOSTNAME } ' "
2021-01-27 12:35:55 +00:00
echo " export PFLOGSUMM_SENDER=' ${ PFLOGSUMM_SENDER } ' "
echo " export PFLOGSUMM_TRIGGER=' ${ PFLOGSUMM_TRIGGER } ' "
echo " export PFLOGSUMM_RECIPIENT=' ${ PFLOGSUMM_RECIPIENT } ' "
echo " export POSTFIX_INET_PROTOCOLS=' ${ POSTFIX_INET_PROTOCOLS } ' "
echo " export POSTFIX_MAILBOX_SIZE_LIMIT=' ${ POSTFIX_MAILBOX_SIZE_LIMIT } ' "
echo " export POSTFIX_MESSAGE_SIZE_LIMIT=' ${ POSTFIX_MESSAGE_SIZE_LIMIT } ' "
2021-01-26 11:20:35 +00:00
echo " export POSTGREY_AUTO_WHITELIST_CLIENTS=' ${ POSTGREY_AUTO_WHITELIST_CLIENTS } ' "
echo " export POSTGREY_DELAY=' ${ POSTGREY_DELAY } ' "
echo " export POSTGREY_MAX_AGE=' ${ POSTGREY_MAX_AGE } ' "
echo " export POSTGREY_TEXT=' ${ POSTGREY_TEXT } ' "
2021-01-27 12:35:55 +00:00
echo " export POSTMASTER_ADDRESS=' ${ POSTMASTER_ADDRESS } ' "
2021-01-26 11:20:35 +00:00
echo " export POSTSCREEN_ACTION=' ${ POSTSCREEN_ACTION } ' "
2021-01-27 12:35:55 +00:00
echo " export REPORT_SENDER=' ${ REPORT_SENDER } ' "
2021-01-26 11:20:35 +00:00
echo " export REPORT_RECIPIENT=' ${ REPORT_RECIPIENT } ' "
echo " export SMTP_ONLY=' ${ SMTP_ONLY } ' "
echo " export SPAMASSASSIN_SPAM_TO_INBOX=' ${ SPAMASSASSIN_SPAM_TO_INBOX } ' "
echo " export SPOOF_PROTECTION=' ${ SPOOF_PROTECTION } ' "
echo " export SRS_SENDER_CLASSES=' ${ SRS_SENDER_CLASSES } ' "
echo " export SSL_TYPE=' ${ SSL_TYPE } ' "
echo " export SUPERVISOR_LOGLEVEL=' ${ SUPERVISOR_LOGLEVEL } ' "
echo " export TLS_LEVEL=' ${ TLS_LEVEL } ' "
echo " export VIRUSMAILS_DELETE_DELAY=' ${ VIRUSMAILS_DELETE_DELAY } ' "
echo " export DMS_DEBUG=' ${ DMS_DEBUG } ' "
2021-01-16 09:16:05 +00:00
} >>/root/.bashrc
2016-12-01 14:45:40 +00:00
}
2019-10-27 08:22:16 +00:00
# File/folder permissions are fine when using docker volumes, but may be wrong
# when file system folders are mounted into the container.
# Set the expected values and create missing folders/files just in case.
2020-10-02 13:45:57 +00:00
function _setup_file_permissions
2020-09-23 08:21:37 +00:00
{
_notify 'task' "Setting file/folder permissions"
2019-10-27 08:22:16 +00:00
2020-09-23 08:21:37 +00:00
mkdir -p /var/log/supervisor
2019-10-27 08:22:16 +00:00
2020-09-23 08:21:37 +00:00
mkdir -p /var/log/mail
chown syslog:root /var/log/mail
2019-10-27 08:22:16 +00:00
2020-09-23 08:21:37 +00:00
touch /var/log/mail/clamav.log
chown clamav:adm /var/log/mail/clamav.log
chmod 640 /var/log/mail/clamav.log
2019-10-27 08:22:16 +00:00
2020-09-23 08:21:37 +00:00
touch /var/log/mail/freshclam.log
chown clamav:adm /var/log/mail/freshclam.log
chmod 640 /var/log/mail/freshclam.log
2019-10-27 08:22:16 +00:00
}
2020-10-02 13:45:57 +00:00
function _setup_chksum_file
2020-09-23 08:21:37 +00:00
{
_notify 'task' "Setting up configuration checksum file"
2019-08-01 07:58:22 +00:00
2020-09-23 08:21:37 +00:00
if [ [ -d /tmp/docker-mailserver ] ]
then
_notify 'inf' " Creating ${ CHKSUM_FILE } "
_monitored_files_checksums >" ${ CHKSUM_FILE } "
else
# We could just skip the file, but perhaps config can be added later?
# If so it must be processed by the check for changes script
_notify 'inf' " Creating empty ${ CHKSUM_FILE } (no config) "
touch " ${ CHKSUM_FILE } "
fi
2019-08-01 07:58:22 +00:00
}
2020-10-02 13:45:57 +00:00
function _setup_mailname
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Setting up Mailname'
2016-12-01 14:45:40 +00:00
2020-09-23 08:21:37 +00:00
_notify 'inf' "Creating /etc/mailname"
echo " ${ DOMAINNAME } " > /etc/mailname
2016-12-01 14:45:40 +00:00
}
2020-10-02 13:45:57 +00:00
function _setup_amavis
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Setting up Amavis'
2017-01-20 22:30:29 +00:00
2020-09-23 08:21:37 +00:00
_notify 'inf' "Applying hostname to /etc/amavis/conf.d/05-node_id"
# shellcheck disable=SC2016
2021-02-21 22:49:37 +00:00
sed -i 's|^#\$myhostname = "mail.example.com";|\$myhostname = "' " ${ HOSTNAME } " '";|' /etc/amavis/conf.d/05-node_id
2017-01-20 22:30:29 +00:00
}
2020-10-02 13:45:57 +00:00
function _setup_dmarc_hostname
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Setting up dmarc'
2017-02-13 10:07:30 +00:00
2020-09-23 08:21:37 +00:00
_notify 'inf' "Applying hostname to /etc/opendmarc.conf"
2021-02-21 22:49:37 +00:00
sed -i -e 's|^AuthservID.*$|AuthservID ' " ${ HOSTNAME } " '|g' \
-e 's|^TrustedAuthservIDs.*$|TrustedAuthservIDs ' " ${ HOSTNAME } " '|g' /etc/opendmarc.conf
2017-02-13 10:07:30 +00:00
}
2020-10-02 13:45:57 +00:00
function _setup_postfix_hostname
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Applying hostname and domainname to Postfix'
2017-02-13 10:07:30 +00:00
2020-09-23 08:21:37 +00:00
_notify 'inf' "Applying hostname to /etc/postfix/main.cf"
postconf -e " myhostname = ${ HOSTNAME } "
postconf -e " mydomain = ${ DOMAINNAME } "
2017-02-13 10:07:30 +00:00
}
2020-10-02 13:45:57 +00:00
function _setup_dovecot_hostname
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Applying hostname to Dovecot'
2017-02-13 10:07:30 +00:00
2020-09-23 08:21:37 +00:00
_notify 'inf' "Applying hostname to /etc/dovecot/conf.d/15-lda.conf"
2021-02-21 22:49:37 +00:00
sed -i 's|^#hostname =.*$|hostname = ' " ${ HOSTNAME } " '|g' /etc/dovecot/conf.d/15-lda.conf
2017-02-13 10:07:30 +00:00
}
2020-10-02 13:45:57 +00:00
function _setup_dovecot
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Setting up Dovecot'
2016-12-01 14:45:40 +00:00
2020-09-23 08:21:37 +00:00
# moved from docker file, copy or generate default self-signed cert
if [ [ -f /var/mail-state/lib-dovecot/dovecot.pem ] ] && [ [ ${ ONE_DIR } -eq 1 ] ]
then
_notify 'inf' "Copying default dovecot cert"
cp /var/mail-state/lib-dovecot/dovecot.key /etc/dovecot/ssl/
cp /var/mail-state/lib-dovecot/dovecot.pem /etc/dovecot/ssl/
fi
2019-08-10 08:15:35 +00:00
2020-09-23 08:21:37 +00:00
if [ [ ! -f /etc/dovecot/ssl/dovecot.pem ] ]
then
_notify 'inf' "Generating default dovecot cert"
pushd /usr/share/dovecot || return 1
./mkcert.sh
popd || return 1
if [ [ ${ ONE_DIR } -eq 1 ] ]
then
mkdir -p /var/mail-state/lib-dovecot
cp /etc/dovecot/ssl/dovecot.key /var/mail-state/lib-dovecot/
cp /etc/dovecot/ssl/dovecot.pem /var/mail-state/lib-dovecot/
fi
fi
cp -a /usr/share/dovecot/protocols.d /etc/dovecot/
# disable pop3 (it will be eventually enabled later in the script, if requested)
mv /etc/dovecot/protocols.d/pop3d.protocol /etc/dovecot/protocols.d/pop3d.protocol.disab
mv /etc/dovecot/protocols.d/managesieved.protocol /etc/dovecot/protocols.d/managesieved.protocol.disab
2021-02-21 22:49:37 +00:00
sed -i -e 's|#ssl = yes|ssl = yes|g' /etc/dovecot/conf.d/10-master.conf
sed -i -e 's|#port = 993|port = 993|g' /etc/dovecot/conf.d/10-master.conf
sed -i -e 's|#port = 995|port = 995|g' /etc/dovecot/conf.d/10-master.conf
sed -i -e 's|#ssl = yes|ssl = required|g' /etc/dovecot/conf.d/10-ssl.conf
sed -i 's|^postmaster_address = .*$|postmaster_address = ' " ${ POSTMASTER_ADDRESS } " '|g' /etc/dovecot/conf.d/15-lda.conf
2020-09-23 08:21:37 +00:00
# set mail_location according to mailbox format
case " ${ DOVECOT_MAILBOX_FORMAT } " in
2020-12-06 18:57:38 +00:00
sdbox| mdbox )
2020-09-23 08:21:37 +00:00
_notify 'inf' " Dovecot ${ DOVECOT_MAILBOX_FORMAT } format configured "
2021-02-21 22:49:37 +00:00
sed -i -e 's|^mail_location = .*$|mail_location = ' " ${ DOVECOT_MAILBOX_FORMAT } " ':\/var\/mail\/%d\/%n|g' /etc/dovecot/conf.d/10-mail.conf
2020-12-06 18:57:38 +00:00
_notify 'inf' "Enabling cron job for dbox purge"
mv /etc/cron.d/dovecot-purge.disabled /etc/cron.d/dovecot-purge
chmod 644 /etc/cron.d/dovecot-purge
2020-09-23 08:21:37 +00:00
; ;
* )
_notify 'inf' "Dovecot maildir format configured (default)"
2021-02-21 22:49:37 +00:00
sed -i -e 's|^mail_location = .*$|mail_location = maildir:\/var\/mail\/%d\/%n|g' /etc/dovecot/conf.d/10-mail.conf
2020-09-23 08:21:37 +00:00
; ;
esac
# enable Managesieve service by setting the symlink
# to the configuration file Dovecot will actually find
if [ [ ${ ENABLE_MANAGESIEVE } -eq 1 ] ]
then
2021-02-09 11:12:36 +00:00
_notify 'inf' 'Sieve management enabled'
2020-09-23 08:21:37 +00:00
mv /etc/dovecot/protocols.d/managesieved.protocol.disab /etc/dovecot/protocols.d/managesieved.protocol
fi
# copy pipe and filter programs, if any
rm -f /usr/lib/dovecot/sieve-filter/*
rm -f /usr/lib/dovecot/sieve-pipe/*
[ [ -d /tmp/docker-mailserver/sieve-filter ] ] && cp /tmp/docker-mailserver/sieve-filter/* /usr/lib/dovecot/sieve-filter/
[ [ -d /tmp/docker-mailserver/sieve-pipe ] ] && cp /tmp/docker-mailserver/sieve-pipe/* /usr/lib/dovecot/sieve-pipe/
2020-05-03 22:24:10 +00:00
# create global sieve directories
2020-09-23 08:21:37 +00:00
mkdir -p /usr/lib/dovecot/sieve-global/before
mkdir -p /usr/lib/dovecot/sieve-global/after
if [ [ -f /tmp/docker-mailserver/before.dovecot.sieve ] ]
then
cp /tmp/docker-mailserver/before.dovecot.sieve /usr/lib/dovecot/sieve-global/before/50-before.dovecot.sieve
sievec /usr/lib/dovecot/sieve-global/before/50-before.dovecot.sieve
else
rm -f /usr/lib/dovecot/sieve-global/before/50-before.dovecot.sieve /usr/lib/dovecot/sieve-global/before/50-before.dovecot.svbin
fi
if [ [ -f /tmp/docker-mailserver/after.dovecot.sieve ] ]
then
cp /tmp/docker-mailserver/after.dovecot.sieve /usr/lib/dovecot/sieve-global/after/50-after.dovecot.sieve
sievec /usr/lib/dovecot/sieve-global/after/50-after.dovecot.sieve
else
rm -f /usr/lib/dovecot/sieve-global/after/50-after.dovecot.sieve /usr/lib/dovecot/sieve-global/after/50-after.dovecot.svbin
fi
2020-05-02 16:39:51 +00:00
# sieve will move spams to .Junk folder when SPAMASSASSIN_SPAM_TO_INBOX=1 and MOVE_SPAM_TO_JUNK=1
2020-09-23 08:21:37 +00:00
if [ [ ${ SPAMASSASSIN_SPAM_TO_INBOX } -eq 1 ] ] && [ [ ${ MOVE_SPAM_TO_JUNK } -eq 1 ] ]
then
_notify 'inf' "Spam messages will be moved to the Junk folder."
cp /etc/dovecot/sieve/before/60-spam.sieve /usr/lib/dovecot/sieve-global/before/
sievec /usr/lib/dovecot/sieve-global/before/60-spam.sieve
else
rm -f /usr/lib/dovecot/sieve-global/before/60-spam.sieve /usr/lib/dovecot/sieve-global/before/60-spam.svbin
fi
2020-05-02 16:39:51 +00:00
2020-09-23 08:21:37 +00:00
chown docker:docker -R /usr/lib/dovecot/sieve*
chmod 550 -R /usr/lib/dovecot/sieve*
chmod -f +x /usr/lib/dovecot/sieve-pipe/*
2016-12-01 14:45:40 +00:00
}
2020-10-02 13:45:57 +00:00
function _setup_dovecot_quota
2020-09-23 08:21:37 +00:00
{
2020-09-05 14:53:36 +00:00
_notify 'task' 'Setting up Dovecot quota'
2020-04-24 12:55:32 +00:00
2020-09-23 08:21:37 +00:00
# Dovecot quota is disabled when using LDAP or SMTP_ONLY or when explicitly disabled.
if [ [ ${ ENABLE_LDAP } -eq 1 ] ] || [ [ ${ SMTP_ONLY } -eq 1 ] ] || [ [ ${ ENABLE_QUOTAS } -eq 0 ] ]
then
2020-04-30 14:11:45 +00:00
# disable dovecot quota in docevot confs
2020-09-23 08:21:37 +00:00
if [ [ -f /etc/dovecot/conf.d/90-quota.conf ] ]
then
2020-04-24 12:55:32 +00:00
mv /etc/dovecot/conf.d/90-quota.conf /etc/dovecot/conf.d/90-quota.conf.disab
2021-02-21 22:49:37 +00:00
sed -i "s|mail_plugins = \$mail_plugins quota|mail_plugins = \$mail_plugins|g" /etc/dovecot/conf.d/10-mail.conf
sed -i "s|mail_plugins = \$mail_plugins imap_quota|mail_plugins = \$mail_plugins|g" /etc/dovecot/conf.d/20-imap.conf
2020-04-24 12:55:32 +00:00
fi
2020-04-30 14:11:45 +00:00
# disable quota policy check in postfix
2021-02-21 22:49:37 +00:00
sed -i "s|check_policy_service inet:localhost:65265||g" /etc/postfix/main.cf
2020-04-24 12:55:32 +00:00
else
2020-09-23 08:21:37 +00:00
if [ [ -f /etc/dovecot/conf.d/90-quota.conf.disab ] ]
then
2020-04-24 12:55:32 +00:00
mv /etc/dovecot/conf.d/90-quota.conf.disab /etc/dovecot/conf.d/90-quota.conf
2021-02-21 22:49:37 +00:00
sed -i "s|mail_plugins = \$mail_plugins|mail_plugins = \$mail_plugins quota|g" /etc/dovecot/conf.d/10-mail.conf
sed -i "s|mail_plugins = \$mail_plugin|mail_plugins = \$mail_plugins imap_quota|g" /etc/dovecot/conf.d/20-imap.conf
2020-04-24 12:55:32 +00:00
fi
2021-01-16 09:16:05 +00:00
local MESSAGE_SIZE_LIMIT_MB = $(( POSTFIX_MESSAGE_SIZE_LIMIT / 1000000 ))
local MAILBOX_LIMIT_MB = $(( POSTFIX_MAILBOX_SIZE_LIMIT / 1000000 ))
2020-04-24 12:55:32 +00:00
2021-02-21 22:49:37 +00:00
sed -i " s|quota_max_mail_size =.*|quota_max_mail_size = ${ MESSAGE_SIZE_LIMIT_MB } $( [ [ ${ MESSAGE_SIZE_LIMIT_MB } -eq 0 ] ] && echo "" || echo "M" ) |g " /etc/dovecot/conf.d/90-quota.conf
sed -i " s|quota_rule = \*:storage=.*|quota_rule = *:storage= ${ MAILBOX_LIMIT_MB } $( [ [ ${ MAILBOX_LIMIT_MB } -eq 0 ] ] && echo "" || echo "M" ) |g " /etc/dovecot/conf.d/90-quota.conf
2020-04-24 12:55:32 +00:00
2020-09-23 08:21:37 +00:00
if [ [ ! -f /tmp/docker-mailserver/dovecot-quotas.cf ] ]
then
2020-09-05 14:53:36 +00:00
_notify 'inf' "'config/docker-mailserver/dovecot-quotas.cf' is not provided. Using default quotas."
2020-10-06 12:45:55 +00:00
: >/tmp/docker-mailserver/dovecot-quotas.cf
2020-04-24 12:55:32 +00:00
fi
2020-04-30 14:11:45 +00:00
# enable quota policy check in postfix
2021-02-21 22:49:37 +00:00
sed -i "s|reject_unknown_recipient_domain, reject_rbl_client zen.spamhaus.org|reject_unknown_recipient_domain, check_policy_service inet:localhost:65265, reject_rbl_client zen.spamhaus.org|g" /etc/postfix/main.cf
2020-04-24 12:55:32 +00:00
fi
}
2020-10-02 13:45:57 +00:00
function _setup_dovecot_local_user
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Setting up Dovecot Local User'
2020-10-06 12:45:55 +00:00
: >/etc/postfix/vmailbox
: >/etc/dovecot/userdb
2020-09-23 08:21:37 +00:00
if [ [ -f /tmp/docker-mailserver/postfix-accounts.cf ] ] && [ [ ${ ENABLE_LDAP } -ne 1 ] ]
then
_notify 'inf' "Checking file line endings"
2021-02-21 22:49:37 +00:00
sed -i 's|\r||g' /tmp/docker-mailserver/postfix-accounts.cf
2020-09-23 08:21:37 +00:00
_notify 'inf' "Regenerating postfix user list"
echo "# WARNING: this file is auto-generated. Modify config/postfix-accounts.cf to edit user list." > /etc/postfix/vmailbox
# checking that /tmp/docker-mailserver/postfix-accounts.cf ends with a newline
# shellcheck disable=SC1003
sed -i -e '$a\' /tmp/docker-mailserver/postfix-accounts.cf
chown dovecot:dovecot /etc/dovecot/userdb
chmod 640 /etc/dovecot/userdb
sed -i -e '/\!include auth-ldap\.conf\.ext/s/^/#/' /etc/dovecot/conf.d/10-auth.conf
sed -i -e '/\!include auth-passwdfile\.inc/s/^#//' /etc/dovecot/conf.d/10-auth.conf
# creating users ; 'pass' is encrypted
# comments and empty lines are ignored
2021-02-07 18:02:09 +00:00
while IFS = $'|' read -r LOGIN PASS USER_ATTRIBUTES
2020-09-23 08:21:37 +00:00
do
# Setting variables for better readability
2020-10-02 13:45:57 +00:00
USER = $( echo " ${ LOGIN } " | cut -d @ -f1)
DOMAIN = $( echo " ${ LOGIN } " | cut -d @ -f2)
2020-09-23 08:21:37 +00:00
# test if user has a defined quota
if [ [ -f /tmp/docker-mailserver/dovecot-quotas.cf ] ]
then
declare -a USER_QUOTA
2020-10-02 13:45:57 +00:00
IFS = ':' ; read -r -a USER_QUOTA < <( grep " ${ USER } @ ${ DOMAIN } : " -i /tmp/docker-mailserver/dovecot-quotas.cf)
2020-09-23 08:21:37 +00:00
unset IFS
2021-02-07 18:02:09 +00:00
[ [ ${# USER_QUOTA [@] } -eq 2 ] ] && USER_ATTRIBUTES = " ${ USER_ATTRIBUTES } userdb_quota_rule=*:bytes= ${ USER_QUOTA [1] } "
2020-09-23 08:21:37 +00:00
fi
# Let's go!
2020-10-02 13:45:57 +00:00
_notify 'inf' " user ' ${ USER } ' for domain ' ${ DOMAIN } ' with password '********', attr= ${ USER_ATTRIBUTES } "
2020-09-23 08:21:37 +00:00
2020-10-02 13:45:57 +00:00
echo " ${ LOGIN } ${ DOMAIN } / ${ USER } / " >> /etc/postfix/vmailbox
2020-09-23 08:21:37 +00:00
# User database for dovecot has the following format:
# user:password:uid:gid:(gecos):home:(shell):extra_fields
# Example :
2020-10-02 13:45:57 +00:00
# ${LOGIN}:${PASS}:5000:5000::/var/mail/${DOMAIN}/${USER}::userdb_mail=maildir:/var/mail/${DOMAIN}/${USER}
echo " ${ LOGIN } : ${ PASS } :5000:5000::/var/mail/ ${ DOMAIN } / ${ USER } :: ${ USER_ATTRIBUTES } " >> /etc/dovecot/userdb
mkdir -p " /var/mail/ ${ DOMAIN } / ${ USER } "
2020-09-23 08:21:37 +00:00
# Copy user provided sieve file, if present
2020-10-02 13:45:57 +00:00
if [ [ -e " /tmp/docker-mailserver/ ${ LOGIN } .dovecot.sieve " ] ]
2020-09-23 08:21:37 +00:00
then
2020-10-02 13:45:57 +00:00
cp " /tmp/docker-mailserver/ ${ LOGIN } .dovecot.sieve " " /var/mail/ ${ DOMAIN } / ${ USER } /.dovecot.sieve "
2020-09-23 08:21:37 +00:00
fi
2020-10-02 13:45:57 +00:00
echo " ${ DOMAIN } " >> /tmp/vhost.tmp
2020-10-06 12:45:55 +00:00
done < <( grep -v " ^\s* $\|^\s*\# " /tmp/docker-mailserver/postfix-accounts.cf)
2020-09-23 08:21:37 +00:00
else
_notify 'inf' "'config/docker-mailserver/postfix-accounts.cf' is not provided. No mail account created."
fi
if ! grep '@' /tmp/docker-mailserver/postfix-accounts.cf | grep -q '|'
then
if [ [ ${ ENABLE_LDAP } -eq 0 ] ]
then
_notify 'fatal' "Unless using LDAP, you need at least 1 email account to start Dovecot."
_defunc
fi
fi
}
2020-10-02 13:45:57 +00:00
function _setup_ldap
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Setting up Ldap'
_notify 'inf' 'Checking for custom configs'
for i in 'users' 'groups' 'aliases' 'domains'
do
local FPATH = " /tmp/docker-mailserver/ldap- ${ i } .cf "
if [ [ -f ${ FPATH } ] ]
then
cp " ${ FPATH } " " /etc/postfix/ldap- ${ i } .cf "
fi
done
_notify 'inf' 'Starting to override configs'
local FILES = (
/etc/postfix/ldap-users.cf
/etc/postfix/ldap-groups.cf
/etc/postfix/ldap-aliases.cf
/etc/postfix/ldap-domains.cf
/etc/postfix/maps/sender_login_maps.ldap
)
for FILE in " ${ FILES [@] } "
do
[ [ ${ FILE } = ~ ldap-user ] ] && export LDAP_QUERY_FILTER = " ${ LDAP_QUERY_FILTER_USER } "
[ [ ${ FILE } = ~ ldap-group ] ] && export LDAP_QUERY_FILTER = " ${ LDAP_QUERY_FILTER_GROUP } "
[ [ ${ FILE } = ~ ldap-aliases ] ] && export LDAP_QUERY_FILTER = " ${ LDAP_QUERY_FILTER_ALIAS } "
[ [ ${ FILE } = ~ ldap-domains ] ] && export LDAP_QUERY_FILTER = " ${ LDAP_QUERY_FILTER_DOMAIN } "
configomat.sh "LDAP_" " ${ FILE } "
done
_notify 'inf' "Configuring dovecot LDAP"
declare -A _dovecot_ldap_mapping
_dovecot_ldap_mapping[ "DOVECOT_BASE" ] = " ${ DOVECOT_BASE : = " ${ LDAP_SEARCH_BASE } " } "
_dovecot_ldap_mapping[ "DOVECOT_DN" ] = " ${ DOVECOT_DN : = " ${ LDAP_BIND_DN } " } "
_dovecot_ldap_mapping[ "DOVECOT_DNPASS" ] = " ${ DOVECOT_DNPASS : = " ${ LDAP_BIND_PW } " } "
_dovecot_ldap_mapping[ "DOVECOT_HOSTS" ] = " ${ DOVECOT_HOSTS : = " ${ LDAP_SERVER_HOST } " } "
# Not sure whether this can be the same or not
# _dovecot_ldap_mapping["DOVECOT_PASS_FILTER"]="${DOVECOT_PASS_FILTER:="${LDAP_QUERY_FILTER_USER}"}"
# _dovecot_ldap_mapping["DOVECOT_USER_FILTER"]="${DOVECOT_USER_FILTER:="${LDAP_QUERY_FILTER_USER}"}"
for VAR in " ${ !_dovecot_ldap_mapping[@] } "
do
export " ${ VAR } = ${ _dovecot_ldap_mapping [ ${ VAR } ] } "
done
configomat.sh "DOVECOT_" "/etc/dovecot/dovecot-ldap.conf.ext"
2020-10-06 12:45:55 +00:00
# add domainname to vhost
2020-09-23 08:21:37 +00:00
echo " ${ DOMAINNAME } " >>/tmp/vhost.tmp
_notify 'inf' "Enabling dovecot LDAP authentification"
sed -i -e '/\!include auth-ldap\.conf\.ext/s/^#//' /etc/dovecot/conf.d/10-auth.conf
sed -i -e '/\!include auth-passwdfile\.inc/s/^/#/' /etc/dovecot/conf.d/10-auth.conf
_notify 'inf' "Configuring LDAP"
if [ [ -f /etc/postfix/ldap-users.cf ] ]
then
postconf -e "virtual_mailbox_maps = ldap:/etc/postfix/ldap-users.cf" || \
_notify 'inf' "==> Warning: /etc/postfix/ldap-user.cf not found"
fi
if [ [ -f /etc/postfix/ldap-domains.cf ] ]
then
postconf -e "virtual_mailbox_domains = /etc/postfix/vhost, ldap:/etc/postfix/ldap-domains.cf" || \
_notify 'inf' "==> Warning: /etc/postfix/ldap-domains.cf not found"
fi
if [ [ -f /etc/postfix/ldap-aliases.cf ] ] && [ [ -f /etc/postfix/ldap-groups.cf ] ]
then
postconf -e "virtual_alias_maps = ldap:/etc/postfix/ldap-aliases.cf, ldap:/etc/postfix/ldap-groups.cf" || \
_notify 'inf' "==> Warning: /etc/postfix/ldap-aliases.cf or /etc/postfix/ldap-groups.cf not found"
fi
2021-01-19 08:27:01 +00:00
# shellcheck disable=SC2016
2021-02-21 22:59:59 +00:00
sed -i 's|mydestination = \$myhostname, |mydestination = |' /etc/postfix/main.cf
2021-01-19 08:27:01 +00:00
2020-09-23 08:21:37 +00:00
return 0
}
2020-10-02 13:45:57 +00:00
function _setup_postgrey
2020-09-23 08:21:37 +00:00
{
_notify 'inf' "Configuring postgrey"
2021-01-31 15:05:05 +00:00
sed -i -E \
2021-02-21 22:59:59 +00:00
's|, reject_rbl_client zen.spamhaus.org$|, reject_rbl_client zen.spamhaus.org, check_policy_service inet:127.0.0.1:10023|' \
2021-01-31 15:05:05 +00:00
/etc/postfix/main.cf
sed -i -e \
2021-02-21 22:49:37 +00:00
" s|\"--inet=127.0.0.1:10023\"|\"--inet=127.0.0.1:10023 --delay= ${ POSTGREY_DELAY } --max-age= ${ POSTGREY_MAX_AGE } --auto-whitelist-clients= ${ POSTGREY_AUTO_WHITELIST_CLIENTS } \"| " \
2021-01-31 15:05:05 +00:00
/etc/default/postgrey
2020-09-23 08:21:37 +00:00
TEXT_FOUND = $( grep -c -i "POSTGREY_TEXT" /etc/default/postgrey)
if [ [ ${ TEXT_FOUND } -eq 0 ] ]
then
printf "POSTGREY_TEXT=\"%s\"\n\n" " ${ POSTGREY_TEXT } " >> /etc/default/postgrey
fi
if [ [ -f /tmp/docker-mailserver/whitelist_clients.local ] ]
then
cp -f /tmp/docker-mailserver/whitelist_clients.local /etc/postgrey/whitelist_clients.local
fi
if [ [ -f /tmp/docker-mailserver/whitelist_recipients ] ]
then
cp -f /tmp/docker-mailserver/whitelist_recipients /etc/postgrey/whitelist_recipients
fi
}
2020-10-02 13:45:57 +00:00
function _setup_postfix_postscreen
2020-09-23 08:21:37 +00:00
{
_notify 'inf' "Configuring postscreen"
2021-02-21 22:49:37 +00:00
sed -i -e " s|postscreen_dnsbl_action = enforce|postscreen_dnsbl_action = ${ POSTSCREEN_ACTION } | " \
-e " s|postscreen_greet_action = enforce|postscreen_greet_action = ${ POSTSCREEN_ACTION } | " \
-e " s|postscreen_bare_newline_action = enforce|postscreen_bare_newline_action = ${ POSTSCREEN_ACTION } | " /etc/postfix/main.cf
2018-10-15 19:17:45 +00:00
}
2020-10-02 13:45:57 +00:00
function _setup_postfix_sizelimits
2020-09-23 08:21:37 +00:00
{
_notify 'inf' "Configuring postfix message size limit"
2021-01-16 09:16:05 +00:00
postconf -e " message_size_limit = ${ POSTFIX_MESSAGE_SIZE_LIMIT } "
2020-09-23 08:21:37 +00:00
_notify 'inf' "Configuring postfix mailbox size limit"
2021-01-16 09:16:05 +00:00
postconf -e " mailbox_size_limit = ${ POSTFIX_MAILBOX_SIZE_LIMIT } "
2020-09-23 08:21:37 +00:00
_notify 'inf' "Configuring postfix virtual mailbox size limit"
2021-01-16 09:16:05 +00:00
postconf -e " virtual_mailbox_limit = ${ POSTFIX_MAILBOX_SIZE_LIMIT } "
2019-08-11 15:14:00 +00:00
}
2020-09-23 08:21:37 +00:00
2020-10-02 13:45:57 +00:00
function _setup_postfix_smtputf8
2020-09-23 08:21:37 +00:00
{
_notify 'inf' "Configuring postfix smtputf8 support (disable)"
postconf -e "smtputf8_enable = no"
2018-03-07 18:33:43 +00:00
}
2020-10-02 13:45:57 +00:00
function _setup_spoof_protection
2020-09-23 08:21:37 +00:00
{
_notify 'inf' "Configuring Spoof Protection"
2021-01-19 11:31:45 +00:00
sed -i \
2021-02-21 22:59:59 +00:00
's|smtpd_sender_restrictions =|smtpd_sender_restrictions = reject_authenticated_sender_login_mismatch,|' \
2021-01-19 11:31:45 +00:00
/etc/postfix/main.cf
2020-09-23 08:21:37 +00:00
2021-01-19 11:31:45 +00:00
if [ [ ${ ENABLE_LDAP } -eq 1 ] ]
then
postconf -e "smtpd_sender_login_maps = ldap:/etc/postfix/ldap-users.cf ldap:/etc/postfix/ldap-aliases.cf ldap:/etc/postfix/ldap-groups.cf"
else
if [ [ -f /etc/postfix/regexp ] ]
then
postconf -e "smtpd_sender_login_maps = unionmap:{ texthash:/etc/postfix/virtual, hash:/etc/aliases, pcre:/etc/postfix/maps/sender_login_maps.pcre, pcre:/etc/postfix/regexp }"
else
postconf -e "smtpd_sender_login_maps = texthash:/etc/postfix/virtual, hash:/etc/aliases, pcre:/etc/postfix/maps/sender_login_maps.pcre"
fi
fi
2020-09-23 08:21:37 +00:00
}
2020-10-02 13:45:57 +00:00
function _setup_postfix_access_control
2020-09-23 08:21:37 +00:00
{
2021-02-07 17:11:33 +00:00
_notify 'inf' 'Configuring user access'
2020-09-23 08:21:37 +00:00
if [ [ -f /tmp/docker-mailserver/postfix-send-access.cf ] ]
then
sed -i 's|smtpd_sender_restrictions =|smtpd_sender_restrictions = check_sender_access texthash:/tmp/docker-mailserver/postfix-send-access.cf,|' /etc/postfix/main.cf
fi
if [ [ -f /tmp/docker-mailserver/postfix-receive-access.cf ] ]
then
sed -i 's|smtpd_recipient_restrictions =|smtpd_recipient_restrictions = check_recipient_access texthash:/tmp/docker-mailserver/postfix-receive-access.cf,|' /etc/postfix/main.cf
fi
2018-02-18 12:29:43 +00:00
}
2020-10-02 13:45:57 +00:00
function _setup_postfix_sasl
2020-09-23 08:21:37 +00:00
{
2021-02-07 17:11:33 +00:00
if [ [ ${ ENABLE_SASLAUTHD } -eq 1 ] ] && [ [ ! -f /etc/postfix/sasl/smtpd.conf ] ]
2020-09-23 08:21:37 +00:00
then
2021-02-07 17:11:33 +00:00
cat > /etc/postfix/sasl/smtpd.conf << EOF
2016-10-30 13:11:36 +00:00
pwcheck_method: saslauthd
mech_list: plain login
EOF
2020-09-23 08:21:37 +00:00
fi
2017-04-26 12:56:33 +00:00
2021-02-07 17:11:33 +00:00
if [ [ ${ ENABLE_SASLAUTHD } -eq 0 ] ] && [ [ ${ SMTP_ONLY } -eq 1 ] ]
2020-09-23 08:21:37 +00:00
then
2021-02-07 17:11:33 +00:00
sed -i -E \
2021-02-21 22:59:59 +00:00
's|^smtpd_sasl_auth_enable =.*|smtpd_sasl_auth_enable = no|g' \
2021-02-07 17:11:33 +00:00
/etc/postfix/main.cf
sed -i -E \
2021-02-21 22:59:59 +00:00
's|^ -o smtpd_sasl_auth_enable=.*| -o smtpd_sasl_auth_enable=no|g' \
2021-02-07 17:11:33 +00:00
/etc/postfix/master.cf
2020-09-23 08:21:37 +00:00
fi
2016-12-01 14:45:40 +00:00
}
2016-10-30 13:11:36 +00:00
2020-10-02 13:45:57 +00:00
function _setup_saslauthd
2020-09-23 08:21:37 +00:00
{
2021-02-07 17:11:33 +00:00
_notify 'task' "Setting up SASLAUTHD"
2016-12-01 14:45:40 +00:00
2020-09-23 08:21:37 +00:00
# checking env vars and setting defaults
2021-01-16 09:16:05 +00:00
[ [ -z ${ SASLAUTHD_MECHANISMS :- } ] ] && SASLAUTHD_MECHANISMS = pam
[ [ ${ SASLAUTHD_MECHANISMS :- } = = ldap ] ] && [ [ -z ${ SASLAUTHD_LDAP_SEARCH_BASE } ] ] && SASLAUTHD_MECHANISMS = pam
2020-09-23 08:21:37 +00:00
[ [ -z ${ SASLAUTHD_LDAP_SERVER } ] ] && SASLAUTHD_LDAP_SERVER = localhost
[ [ -z ${ SASLAUTHD_LDAP_FILTER } ] ] && SASLAUTHD_LDAP_FILTER = '(&(uniqueIdentifier=%u)(mailEnabled=TRUE))'
2016-12-01 14:45:40 +00:00
2020-09-23 08:21:37 +00:00
if [ [ -z ${ SASLAUTHD_LDAP_SSL } ] ] || [ [ ${ SASLAUTHD_LDAP_SSL } -eq 0 ] ]
then
SASLAUTHD_LDAP_PROTO = 'ldap://' || SASLAUTHD_LDAP_PROTO = 'ldaps://'
fi
[ [ -z ${ SASLAUTHD_LDAP_START_TLS } ] ] && SASLAUTHD_LDAP_START_TLS = no
[ [ -z ${ SASLAUTHD_LDAP_TLS_CHECK_PEER } ] ] && SASLAUTHD_LDAP_TLS_CHECK_PEER = no
2020-12-08 14:29:49 +00:00
[ [ -z ${ SASLAUTHD_LDAP_AUTH_METHOD } ] ] && SASLAUTHD_LDAP_AUTH_METHOD = bind
2020-09-23 08:21:37 +00:00
2020-12-08 14:07:01 +00:00
if [ [ -z ${ SASLAUTHD_LDAP_TLS_CACERT_FILE } ] ]
then
SASLAUTHD_LDAP_TLS_CACERT_FILE = ""
else
SASLAUTHD_LDAP_TLS_CACERT_FILE = " ldap_tls_cacert_file: ${ SASLAUTHD_LDAP_TLS_CACERT_FILE } "
fi
if [ [ -z ${ SASLAUTHD_LDAP_TLS_CACERT_DIR } ] ]
then
SASLAUTHD_LDAP_TLS_CACERT_DIR = ""
else
SASLAUTHD_LDAP_TLS_CACERT_DIR = " ldap_tls_cacert_dir: ${ SASLAUTHD_LDAP_TLS_CACERT_DIR } "
fi
2020-12-08 14:29:49 +00:00
if [ [ -z ${ SASLAUTHD_LDAP_PASSWORD_ATTR } ] ]
then
SASLAUTHD_LDAP_PASSWORD_ATTR = ""
else
SASLAUTHD_LDAP_PASSWORD_ATTR = " ldap_password_attr: ${ SASLAUTHD_LDAP_PASSWORD_ATTR } "
fi
if [ [ -z ${ SASLAUTHD_LDAP_MECH } ] ]
then
SASLAUTHD_LDAP_MECH = ""
else
SASLAUTHD_LDAP_MECH = " ldap_mech: ${ SASLAUTHD_LDAP_MECH } "
fi
2020-09-23 08:21:37 +00:00
if [ [ ! -f /etc/saslauthd.conf ] ]
then
2021-02-09 11:12:36 +00:00
_notify 'inf' 'Creating /etc/saslauthd.conf'
2020-09-23 08:21:37 +00:00
cat > /etc/saslauthd.conf << EOF
2016-10-30 13:11:36 +00:00
ldap_servers: ${ SASLAUTHD_LDAP_PROTO } ${ SASLAUTHD_LDAP_SERVER }
2020-12-08 14:29:49 +00:00
ldap_auth_method: ${ SASLAUTHD_LDAP_AUTH_METHOD }
2016-10-30 13:11:36 +00:00
ldap_bind_dn: ${ SASLAUTHD_LDAP_BIND_DN }
ldap_bind_pw: ${ SASLAUTHD_LDAP_PASSWORD }
ldap_search_base: ${ SASLAUTHD_LDAP_SEARCH_BASE }
ldap_filter: ${ SASLAUTHD_LDAP_FILTER }
2020-09-23 08:21:37 +00:00
ldap_start_tls: ${ SASLAUTHD_LDAP_START_TLS }
ldap_tls_check_peer: ${ SASLAUTHD_LDAP_TLS_CHECK_PEER }
2018-06-02 19:16:16 +00:00
2020-12-08 14:07:01 +00:00
${ SASLAUTHD_LDAP_TLS_CACERT_FILE }
${ SASLAUTHD_LDAP_TLS_CACERT_DIR }
2020-12-08 14:29:49 +00:00
${ SASLAUTHD_LDAP_PASSWORD_ATTR }
${ SASLAUTHD_LDAP_MECH }
2020-12-08 14:07:01 +00:00
2016-10-30 13:11:36 +00:00
ldap_referrals: yes
log_level: 10
EOF
2020-09-23 08:21:37 +00:00
fi
sed -i \
-e "/^[^#].*smtpd_sasl_type.*/s/^/#/g" \
-e "/^[^#].*smtpd_sasl_path.*/s/^/#/g" \
/etc/postfix/master.cf
sed -i \
-e "/smtpd_sasl_path =.*/d" \
-e "/smtpd_sasl_type =.*/d" \
-e "/dovecot_destination_recipient_limit =.*/d" \
/etc/postfix/main.cf
gpasswd -a postfix sasl
}
2020-10-02 13:45:57 +00:00
function _setup_postfix_aliases
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Setting up Postfix Aliases'
2020-10-06 12:45:55 +00:00
: >/etc/postfix/virtual
: >/etc/postfix/regexp
2020-09-23 08:21:37 +00:00
if [ [ -f /tmp/docker-mailserver/postfix-virtual.cf ] ]
then
# fixing old virtual user file
if grep -q " , $" /tmp/docker-mailserver/postfix-virtual.cf
then
2021-02-21 22:49:37 +00:00
sed -i -e "s|, |,|g" -e " s|, $||g " /tmp/docker-mailserver/postfix-virtual.cf
2020-09-23 08:21:37 +00:00
fi
cp -f /tmp/docker-mailserver/postfix-virtual.cf /etc/postfix/virtual
# the `to` is important, don't delete it
# shellcheck disable=SC2034
2020-10-06 12:45:55 +00:00
while read -r FROM TO
2020-09-23 08:21:37 +00:00
do
2020-10-02 13:45:57 +00:00
UNAME = $( echo " ${ FROM } " | cut -d @ -f1)
DOMAIN = $( echo " ${ FROM } " | cut -d @ -f2)
2020-09-23 08:21:37 +00:00
# if they are equal it means the line looks like: "user1 other@domain.tld"
2020-10-21 14:00:35 +00:00
[ [ ${ UNAME } != " ${ DOMAIN } " ] ] && echo " ${ DOMAIN } " >>/tmp/vhost.tmp
2020-10-06 12:45:55 +00:00
done < <( grep -v " ^\s* $\|^\s*\# " /tmp/docker-mailserver/postfix-virtual.cf || true )
2020-09-23 08:21:37 +00:00
else
_notify 'inf' "Warning 'config/postfix-virtual.cf' is not provided. No mail alias/forward created."
fi
if [ [ -f /tmp/docker-mailserver/postfix-regexp.cf ] ]
then
_notify 'inf' "Adding regexp alias file postfix-regexp.cf"
cp -f /tmp/docker-mailserver/postfix-regexp.cf /etc/postfix/regexp
2021-01-19 11:31:45 +00:00
sed -i -E \
2021-02-21 22:59:59 +00:00
's|virtual_alias_maps(.*)|virtual_alias_maps\1 pcre:/etc/postfix/regexp|g' \
2021-01-19 11:31:45 +00:00
/etc/postfix/main.cf
2020-09-23 08:21:37 +00:00
fi
2021-02-09 11:12:36 +00:00
_notify 'inf' 'Configuring root alias'
2020-09-23 08:21:37 +00:00
echo " root: ${ POSTMASTER_ADDRESS } " > /etc/aliases
if [ [ -f /tmp/docker-mailserver/postfix-aliases.cf ] ]
then
cat /tmp/docker-mailserver/postfix-aliases.cf >> /etc/aliases
else
_notify 'inf' "'config/postfix-aliases.cf' is not provided and will be auto created."
2020-10-06 12:45:55 +00:00
: >/tmp/docker-mailserver/postfix-aliases.cf
2020-09-23 08:21:37 +00:00
fi
postalias /etc/aliases
}
2020-10-02 13:45:57 +00:00
function _setup_SRS
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Setting up SRS'
postconf -e "sender_canonical_maps = tcp:localhost:10001"
postconf -e " sender_canonical_classes = ${ SRS_SENDER_CLASSES } "
postconf -e "recipient_canonical_maps = tcp:localhost:10002"
postconf -e "recipient_canonical_classes = envelope_recipient,header_recipient"
}
2020-10-02 13:45:57 +00:00
function _setup_dkim
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Setting up DKIM'
2021-02-21 21:05:35 +00:00
mkdir -p /etc/opendkim
2020-09-23 08:21:37 +00:00
2021-02-21 21:05:35 +00:00
# Check if any keys are available
2020-09-23 08:21:37 +00:00
if [ [ -e "/tmp/docker-mailserver/opendkim/KeyTable" ] ]
then
cp -a /tmp/docker-mailserver/opendkim/* /etc/opendkim/
_notify 'inf' " DKIM keys added for: $( ls -C /etc/opendkim/keys/) "
_notify 'inf' "Changing permissions on /etc/opendkim"
chown -R opendkim:opendkim /etc/opendkim/
2021-02-21 21:05:35 +00:00
chmod -R 0700 /etc/opendkim/keys/
2020-09-23 08:21:37 +00:00
else
2021-02-21 21:05:35 +00:00
_notify 'warn' "No DKIM key provided. Check the documentation on how to get your keys."
2020-09-23 08:21:37 +00:00
fi
# setup nameservers paramater from /etc/resolv.conf if not defined
if ! grep '^Nameservers' /etc/opendkim.conf
then
echo " Nameservers $( grep '^nameserver' /etc/resolv.conf | awk -F " " '{print $2}' | paste -sd ',' -) " >> /etc/opendkim.conf
_notify 'inf' "Nameservers added to /etc/opendkim.conf"
fi
}
2020-10-02 13:45:57 +00:00
function _setup_ssl
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Setting up SSL'
2016-12-01 14:45:40 +00:00
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
local POSTFIX_CONFIG_MAIN = '/etc/postfix/main.cf'
local DOVECOT_CONFIG_SSL = '/etc/dovecot/conf.d/10-ssl.conf'
# Primary certificate to serve for TLS
function _set_certificate
{
local POSTFIX_KEY_WITH_FULLCHAIN = $1
local DOVECOT_KEY = $1
local DOVECOT_CERT = $1
# If 2nd param is provided, we've been provided separate key and cert instead of a fullkeychain
if [ [ -n $2 ] ]
then
local PRIVATE_KEY = $1
local CERT_CHAIN = $2
POSTFIX_KEY_WITH_FULLCHAIN = " ${ PRIVATE_KEY } ${ CERT_CHAIN } "
DOVECOT_KEY = " ${ PRIVATE_KEY } "
DOVECOT_CERT = " ${ CERT_CHAIN } "
fi
# Postfix configuration
# NOTE: `smtpd_tls_chain_files` expects private key defined before public cert chain
# May be a single PEM file or a sequence of files, so long as the order is key->leaf->chain
2021-02-21 22:49:37 +00:00
sed -i " s|^smtpd_tls_chain_files =.*|smtpd_tls_chain_files = ${ POSTFIX_KEY_WITH_FULLCHAIN } | " " ${ POSTFIX_CONFIG_MAIN } "
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
# Dovecot configuration
2021-02-21 22:49:37 +00:00
sed -i " s|^ssl_key = <.*|ssl_key = < ${ DOVECOT_KEY } | " " ${ DOVECOT_CONFIG_SSL } "
sed -i " s|^ssl_cert = <.*|ssl_cert = < ${ DOVECOT_CERT } | " " ${ DOVECOT_CONFIG_SSL } "
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
}
# Enables supporting two certificate types such as ECDSA with an RSA fallback
function _set_alt_certificate
{
local COPY_KEY_FROM_PATH = $1
local COPY_CERT_FROM_PATH = $2
local PRIVATE_KEY_ALT = '/etc/postfix/ssl/fallback_key'
local CERT_CHAIN_ALT = '/etc/postfix/ssl/fallback_cert'
cp " ${ COPY_KEY_FROM_PATH } " " ${ PRIVATE_KEY_ALT } "
cp " ${ COPY_CERT_FROM_PATH } " " ${ CERT_CHAIN_ALT } "
chmod 600 " ${ PRIVATE_KEY_ALT } "
chmod 600 " ${ CERT_CHAIN_ALT } "
# Postfix configuration
# NOTE: This operation doesn't replace the line, it appends to the end of the line.
# Thus this method should only be used when this line has explicitly been replaced earlier in the script.
# Otherwise without `docker-compose down` first, a `docker-compose up` may
# persist previous container state and cause a failure in postfix configuration.
2021-02-21 22:49:37 +00:00
sed -i " s|^smtpd_tls_chain_files =.*|& ${ PRIVATE_KEY_ALT } ${ CERT_CHAIN_ALT } | " " ${ POSTFIX_CONFIG_MAIN } "
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
# Dovecot configuration
# Conditionally checks for `#`, in the event that internal container state is accidentally persisted,
# can be caused by: `docker-compose up` run again after a `ctrl+c`, without running `docker-compose down`
2021-02-21 22:49:37 +00:00
sed -i " s|^#\?ssl_alt_key = <.*|ssl_alt_key = < ${ PRIVATE_KEY_ALT } | " " ${ DOVECOT_CONFIG_SSL } "
sed -i " s|^#\?ssl_alt_cert = <.*|ssl_alt_cert = < ${ CERT_CHAIN_ALT } | " " ${ DOVECOT_CONFIG_SSL } "
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
}
function _apply_tls_level
2021-02-18 09:24:34 +00:00
{
local TLS_CIPHERS_ALLOW = $1
local TLS_PROTOCOL_IGNORE = $2
local TLS_PROTOCOL_MINIMUM = $3
# Postfix configuration
2021-02-21 22:49:37 +00:00
sed -i " s|^smtpd_tls_mandatory_protocols =.*|smtpd_tls_mandatory_protocols = ${ TLS_PROTOCOL_IGNORE } | " " ${ POSTFIX_CONFIG_MAIN } "
sed -i " s|^smtpd_tls_protocols =.*|smtpd_tls_protocols = ${ TLS_PROTOCOL_IGNORE } | " " ${ POSTFIX_CONFIG_MAIN } "
sed -i " s|^smtp_tls_protocols =.*|smtp_tls_protocols = ${ TLS_PROTOCOL_IGNORE } | " " ${ POSTFIX_CONFIG_MAIN } "
sed -i " s|^tls_high_cipherlist =.*|tls_high_cipherlist = ${ TLS_CIPHERS_ALLOW } | " " ${ POSTFIX_CONFIG_MAIN } "
2021-02-18 09:24:34 +00:00
# Dovecot configuration (secure by default though)
2021-02-21 22:49:37 +00:00
sed -i " s|^ssl_min_protocol =.*|ssl_min_protocol = ${ TLS_PROTOCOL_MINIMUM } | " " ${ DOVECOT_CONFIG_SSL } "
sed -i " s|^ssl_cipher_list =.*|ssl_cipher_list = ${ TLS_CIPHERS_ALLOW } | " " ${ DOVECOT_CONFIG_SSL } "
2021-02-18 09:24:34 +00:00
}
2018-02-22 07:36:12 +00:00
# TLS strength/level configuration
2020-09-23 08:21:37 +00:00
case " ${ TLS_LEVEL } " in
2018-02-22 07:36:12 +00:00
"modern" )
2021-02-18 09:24:34 +00:00
local TLS_MODERN_SUITE = 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384'
local TLS_MODERN_IGNORE = '!SSLv2,!SSLv3,!TLSv1,!TLSv1.1'
local TLS_MODERN_MIN = 'TLSv1.2'
2018-02-22 07:36:12 +00:00
2021-02-18 09:24:34 +00:00
_apply_tls_level " ${ TLS_MODERN_SUITE } " " ${ TLS_MODERN_IGNORE } " " ${ TLS_MODERN_MIN } "
2018-02-22 07:36:12 +00:00
2020-09-05 14:53:36 +00:00
_notify 'inf' "TLS configured with 'modern' ciphers"
2020-09-23 08:21:37 +00:00
; ;
2021-01-16 09:16:05 +00:00
2018-02-22 07:36:12 +00:00
"intermediate" )
2021-02-18 09:24:34 +00:00
local TLS_INTERMEDIATE_SUITE = 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA'
local TLS_INTERMEDIATE_IGNORE = '!SSLv2,!SSLv3'
local TLS_INTERMEDIATE_MIN = 'TLSv1'
_apply_tls_level " ${ TLS_INTERMEDIATE_SUITE } " " ${ TLS_INTERMEDIATE_IGNORE } " " ${ TLS_INTERMEDIATE_MIN } "
2018-02-22 07:36:12 +00:00
2020-09-05 14:53:36 +00:00
_notify 'inf' "TLS configured with 'intermediate' ciphers"
2020-09-23 08:21:37 +00:00
; ;
2021-01-16 09:16:05 +00:00
* )
_notify 'err' 'TLS_LEVEL not found [ in _setup_ssl ]'
; ;
2018-02-22 07:36:12 +00:00
esac
2020-09-23 08:21:37 +00:00
# SSL certificate Configuration
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
# TODO: Refactor this feature, it's been extended multiple times for specific inputs/providers unnecessarily.
# NOTE: Some `SSL_TYPE` logic uses mounted certs/keys directly, some make an internal copy either retaining filename or renaming, chmod inconsistent.
2020-09-23 08:21:37 +00:00
case " ${ SSL_TYPE } " in
"letsencrypt" )
2020-09-05 14:53:36 +00:00
_notify 'inf' "Configuring SSL using 'letsencrypt'"
2020-05-12 03:36:46 +00:00
# letsencrypt folders and files mounted in /etc/letsencrypt
local LETSENCRYPT_DOMAIN = ""
local LETSENCRYPT_KEY = ""
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
# 2020 feature intended for Traefik v2 support only:
# https://github.com/docker-mailserver/docker-mailserver/pull/1553
# Uses `key.pem` and `fullchain.pem`
2020-12-11 03:51:53 +00:00
if [ [ -f /etc/letsencrypt/acme.json ] ]
then
if ! _extract_certs_from_acme " ${ SSL_DOMAIN } "
then
if ! _extract_certs_from_acme " ${ HOSTNAME } "
then
_extract_certs_from_acme " ${ DOMAINNAME } "
fi
fi
fi
2020-06-30 20:43:22 +00:00
2020-05-12 03:36:46 +00:00
# first determine the letsencrypt domain by checking both the full hostname or just the domainname if a SAN is used in the cert
2020-09-23 08:21:37 +00:00
if [ [ -e /etc/letsencrypt/live/${ HOSTNAME } /fullchain.pem ] ]
then
LETSENCRYPT_DOMAIN = ${ HOSTNAME }
elif [ [ -e /etc/letsencrypt/live/${ DOMAINNAME } /fullchain.pem ] ]
then
LETSENCRYPT_DOMAIN = ${ DOMAINNAME }
else
_notify 'err' " Cannot access '/etc/letsencrypt/live/ ${ HOSTNAME } /fullchain.pem' or '/etc/letsencrypt/live/ ${ DOMAINNAME } /fullchain.pem' "
2020-05-12 03:36:46 +00:00
return 1
2020-09-23 08:21:37 +00:00
fi
2020-05-12 03:36:46 +00:00
# then determine the keyfile to use
2020-09-23 08:21:37 +00:00
if [ [ -n ${ LETSENCRYPT_DOMAIN } ] ]
then
if [ [ -e /etc/letsencrypt/live/${ LETSENCRYPT_DOMAIN } /privkey.pem ] ]
then
LETSENCRYPT_KEY = "privkey"
elif [ [ -e /etc/letsencrypt/live/${ LETSENCRYPT_DOMAIN } /key.pem ] ]
then
LETSENCRYPT_KEY = "key"
else
_notify 'err' " Cannot access '/etc/letsencrypt/live/ ${ LETSENCRYPT_DOMAIN } /privkey.pem' nor 'key.pem' "
2020-05-12 03:36:46 +00:00
return 1
2020-09-23 08:21:37 +00:00
fi
2020-05-12 03:36:46 +00:00
fi
2016-12-01 14:45:40 +00:00
2020-05-12 03:36:46 +00:00
# finally, make the changes to the postfix and dovecot configurations
2020-09-23 08:21:37 +00:00
if [ [ -n ${ LETSENCRYPT_KEY } ] ]
then
_notify 'inf' " Adding ${ LETSENCRYPT_DOMAIN } SSL certificate to the postfix and dovecot configuration "
2016-12-01 14:45:40 +00:00
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
# LetsEncrypt `fullchain.pem` and `privkey.pem` contents are detailed here from CertBot:
# https://certbot.eff.org/docs/using.html#where-are-my-certificates
# `key.pem` was added for `simp_le` support (2016): https://github.com/docker-mailserver/docker-mailserver/pull/288
# `key.pem` is also a filename used by the `_extract_certs_from_acme` method (implemented for Traefik v2 only)
local PRIVATE_KEY = " /etc/letsencrypt/live/ ${ LETSENCRYPT_DOMAIN } / ${ LETSENCRYPT_KEY } .pem "
local CERT_CHAIN = " /etc/letsencrypt/live/ ${ LETSENCRYPT_DOMAIN } /fullchain.pem "
2016-12-01 14:45:40 +00:00
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
_set_certificate " ${ PRIVATE_KEY } " " ${ CERT_CHAIN } "
2020-05-12 03:36:46 +00:00
2020-09-05 14:53:36 +00:00
_notify 'inf' "SSL configured with 'letsencrypt' certificates"
2020-05-12 03:36:46 +00:00
fi
return 0
2020-09-23 08:21:37 +00:00
; ;
"custom" )
# Adding CA signed SSL certificate if provided in 'postfix/ssl' folder
if [ [ -e /tmp/docker-mailserver/ssl/${ HOSTNAME } -full.pem ] ]
then
_notify 'inf' " Adding ${ HOSTNAME } SSL certificate "
mkdir -p /etc/postfix/ssl
cp " /tmp/docker-mailserver/ssl/ ${ HOSTNAME } -full.pem " /etc/postfix/ssl
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
# Private key with full certificate chain all in a single PEM file
# NOTE: Dovecot works fine still as both values are bundled into the keychain
local KEY_WITH_FULLCHAIN = '/etc/postfix/ssl/' " ${ HOSTNAME } " '-full.pem'
2018-12-02 11:59:16 +00:00
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
_set_certificate " ${ KEY_WITH_FULLCHAIN } "
2018-12-02 11:59:16 +00:00
2020-09-23 08:21:37 +00:00
_notify 'inf' "SSL configured with 'CA signed/custom' certificates"
fi
; ;
"manual" )
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
# Lets you manually specify the location of the SSL Certs to use. This gives you some more control over this whole processes (like using kube-lego to generate certs)
2020-09-23 08:21:37 +00:00
if [ [ -n ${ SSL_CERT_PATH } ] ] && [ [ -n ${ SSL_KEY_PATH } ] ]
then
_notify 'inf' " Configuring certificates using cert ${ SSL_CERT_PATH } and key ${ SSL_KEY_PATH } "
mkdir -p /etc/postfix/ssl
cp " ${ SSL_KEY_PATH } " /etc/postfix/ssl/key
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
cp " ${ SSL_CERT_PATH } " /etc/postfix/ssl/cert
2020-09-23 08:21:37 +00:00
chmod 600 /etc/postfix/ssl/key
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
chmod 600 /etc/postfix/ssl/cert
local PRIVATE_KEY = '/etc/postfix/ssl/key'
local CERT_CHAIN = '/etc/postfix/ssl/cert'
2020-09-23 08:21:37 +00:00
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
_set_certificate " ${ PRIVATE_KEY } " " ${ CERT_CHAIN } "
2020-09-23 08:21:37 +00:00
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
# Support for a fallback certificate, useful for hybrid/dual ECDSA + RSA certs
if [ [ -n ${ SSL_ALT_KEY_PATH } ] ] && [ [ -n ${ SSL_ALT_CERT_PATH } ] ]
then
_notify 'inf' " Configuring alternative certificates using cert ${ SSL_ALT_CERT_PATH } and key ${ SSL_ALT_KEY_PATH } "
_set_alt_certificate " ${ SSL_ALT_KEY_PATH } " " ${ SSL_ALT_CERT_PATH } "
else
# If the Dovecot settings for alt cert has been enabled (doesn't start with `#`),
# but required ENV var is missing, reset to disabled state:
2021-02-21 22:49:37 +00:00
sed -i 's|^ssl_alt_key = <.*|#ssl_alt_key = </path/to/alternative/key.pem|' " ${ DOVECOT_CONFIG_SSL } "
sed -i 's|^ssl_alt_cert = <.*|#ssl_alt_cert = </path/to/alternative/cert.pem|' " ${ DOVECOT_CONFIG_SSL } "
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
fi
2020-09-23 08:21:37 +00:00
_notify 'inf' "SSL configured with 'Manual' certificates"
fi
; ;
"self-signed" )
# Adding self-signed SSL certificate if provided in 'postfix/ssl' folder
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
if [ [ -e /tmp/docker-mailserver/ssl/${ HOSTNAME } -key.pem ] ] \
&& [ [ -e /tmp/docker-mailserver/ssl/${ HOSTNAME } -cert.pem ] ] \
2020-09-23 08:21:37 +00:00
&& [ [ -e /tmp/docker-mailserver/ssl/demoCA/cacert.pem ] ]
then
_notify 'inf' " Adding ${ HOSTNAME } SSL certificate "
mkdir -p /etc/postfix/ssl
cp " /tmp/docker-mailserver/ssl/ ${ HOSTNAME } -key.pem " /etc/postfix/ssl
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
cp " /tmp/docker-mailserver/ssl/ ${ HOSTNAME } -cert.pem " /etc/postfix/ssl
2020-09-23 08:21:37 +00:00
chmod 600 " /etc/postfix/ssl/ ${ HOSTNAME } -key.pem "
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
local PRIVATE_KEY = " /etc/postfix/ssl/ ${ HOSTNAME } -key.pem "
local CERT_CHAIN = " /etc/postfix/ssl/ ${ HOSTNAME } -cert.pem "
2020-09-23 08:21:37 +00:00
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
_set_certificate " ${ PRIVATE_KEY } " " ${ CERT_CHAIN } "
2020-09-23 08:21:37 +00:00
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
cp /tmp/docker-mailserver/ssl/demoCA/cacert.pem /etc/postfix/ssl
# Have Postfix trust the self-signed CA (which is not installed within the OS trust store)
2021-02-21 22:49:37 +00:00
sed -i -r 's|^#?smtpd_tls_CAfile =.*|smtpd_tls_CAfile = /etc/postfix/ssl/cacert.pem|' " ${ POSTFIX_CONFIG_MAIN } "
sed -i -r 's|^#?smtp_tls_CAfile =.*|smtp_tls_CAfile = /etc/postfix/ssl/cacert.pem|' " ${ POSTFIX_CONFIG_MAIN } "
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
# Part of the original `self-signed` support, unclear why this symlink was required?
# May have been to support the now removed `Courier` (Dovecot replaced it):
# https://github.com/docker-mailserver/docker-mailserver/commit/1fb3aeede8ac9707cc9ea11d603e3a7b33b5f8d5
local PRIVATE_CA = " /etc/ssl/certs/cacert- ${ HOSTNAME } .pem "
ln -s /etc/postfix/ssl/cacert.pem " ${ PRIVATE_CA } "
2020-09-23 08:21:37 +00:00
_notify 'inf' "SSL configured with 'self-signed' certificates"
fi
; ;
'' )
2021-01-16 09:16:05 +00:00
# no SSL certificate, plain text access
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
# TODO: Postfix configuration still responds to TLS negotiations using snakeoil cert from default config
# TODO: Dovecot `ssl = yes` also allows TLS, both cases this is insecure and should probably instead enforce no TLS?
2020-09-23 08:21:37 +00:00
# Dovecot configuration
2021-02-21 22:49:37 +00:00
# WARNING: This may not be corrected(reset?) if `SSL_TYPE` is changed and internal config state persisted
sed -i -e 's|^#disable_plaintext_auth = yes|disable_plaintext_auth = no|g' /etc/dovecot/conf.d/10-auth.conf
sed -i -e 's|^ssl = required|ssl = yes|g' " ${ DOVECOT_CONFIG_SSL } "
2020-09-23 08:21:37 +00:00
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
_notify 'warn' "(INSECURE!) SSL configured with plain text access. DO NOT USE FOR PRODUCTION DEPLOYMENT."
2020-09-23 08:21:37 +00:00
; ;
2018-12-02 11:59:16 +00:00
* )
2020-09-23 08:21:37 +00:00
# Unknown option, default behavior, no action is required
Dual certificate support (eg ECDSA with RSA fallback) (#1801)
* feat: Change Postfix smtpd_tls key and cert files to chain_files
Since Postfix 3.4, `smtpd_tls_cert_file` and `smtpd_tls_key_file` have been deprecated in favor of `smtpd_tls_chain_files` which supports a list of values where a single or sequence of file paths provide a private key followed by it's certificate chain.
* feat: Dual certificate support
`smtpd_tls_chain_files` allows for multiple key+cert bundles so that you can provide different key types, such as ECDSA and RSA.
To maintain compatibility with the current CERT/KEY ENV vars only a 2nd certificate is supported.
Since Dovecot 2.2.31 a related feature is also available, but it is limited to only providing one alternative certificate via separate cert and key settings.
---
This feature enables support for multiple certificates, eg for serving modern ECDSA certs with RSA as fallback.
* chore: Refactor variable names to meet style guide
Improved some comments too.
* chore: Have function definitions respect style guide
* chore: Minor edits to comments
* chore: Expand on comments for maintenance, alert of insecure config
When `SSL_TYPE` isn't properly setup, we're still offering SSL connections but not warning in logs about the insecurity of such, or why a misconfiguration may have occurred.
This commit more clearly communicates to the user that they should look into the issue before considering deploying to production.
The `TODO` comments communicate to any future maintainer to consider treating these improper configs as disabling TLS instead.
* fix: Use `snakeoil` cert
I mistakenly thought this was placeholder text, which broke some tests. This adds the two files in the correct order (private key followed by cert/chain), to fix that issue.
* fix: Disable alt cert for Dovecot if necessary
Certain scenarios may persist state of previously configured alt cert via ENV vars that are removed from a future run. If the config is not reset to original immutable state, this will correctly disable the config from using alt cert unintentionally.
* fix: Satisfy ShellCheck lint
By switching from string var to array / list expansion, this better stores the extracted result and applies it in a manner that ShellCheck linting approves, removing the need to disable the rule.
* feat: Support dual cert test
Few tweaks to the test script allows re-purposing it for covering dual cert support as well.
* chore: Rearranged cert and key lines
A little reorganization, mostly placing private key ahead of related cert lines.
* chore: Refactor `_set_certificate`
This should make the parameters a little less confusing.
Previously was 3 parameters, but the Postfix parameter (1st) may look like two variables if you don't pay attention to the surrounding quotes; while the Dovecot parameters (2nd + 3rd) would have an opposing order. There was also a variant where the `FULLKEYCHAIN` var was passed in three times.
Now it's two params, with the 2nd param as an optional one. If the 2nd param is provided, then the two params are in the order of private key then certificate, otherwise if only a single parameter it's a single PEM file with the full cert chain and private key bundled.
This avoids implying that Postfix and Dovecot might use different files.
* chore: Document current state of `SSL_TYPE` logic better
Inlined for the benefit of anyone else maintaining this section if I'm unable to address the concerns within my own time.
* docs: ENV vars
`TLS_LEVEL=old` isn't in the codebase anymore, not likely to be relevant to retain.
No point in documenting what is considered invalid / unsupported config value in the first place for `SSL_TYPE`.
`SSL_TYPE=manual` was missing documentation for both related file path ENV vars, they've been added along with their alt fallback variants.
* chore: Update Dovecot LMTP SSL test config
Not sure how relevant this is, the file isn't complete sync with the main dovecot `10-ssl.conf` config, adding the support just in case.
* chore: Rename `FULLKEYCHAIN` to avoid confusion
There doesn't appear to be a standardized name for this type of file bundle, and `keychain` may be misleading (fullkeychain often provides macOS keychain results on search engines).
Opting for a more explicit `KEY_WITH_FULLCHAIN` name instead.
* fix: Invalid var name
`_set_certificate` refactor commit accidentally changed a var name and committed that breaking the dual cert support (thanks tests!).
* test: Refactor `mail_ssl_manual.bats`
Proper test return values instead of `wc -l` based checking.
Tests with dual cert support active, tests that feature (to better detect failure case.
Third test case was unable to verify new self-signed certificate, added new certs signed with self-signed root CA.
Adjusted openssl `CApath` parameter to use `CAfile` instead as `letsencrypt` cert was replaced thus CA cert is missing from the system trust store.
* test: Properly check for files in `mail_ssl_manual.bats`
Fixes lint error.
Also realized I was accidentally asserting a file exists in the test environment, not within the container.
Resolved that and also added an additional test case to ensure the ENV var files are valid when passed in, in the event a change misconfigures them and that the issue is identified earlier.
* chore: Apply PR review feedback
Better format some strings that had mixed quotes when they weren't necessary.
Additionally DRYed up the config path for Postfix and Dovecot within the `_setup_ssl` method.
Co-authored-by: Georg Lauterbach <infrastructure@itbsd.com>
2021-02-21 22:43:41 +00:00
_notify 'warn' "(INSECURE!) ENV var 'SSL_TYPE' is invalid. DO NOT USE FOR PRODUCTION DEPLOYMENT."
2020-09-23 08:21:37 +00:00
; ;
esac
2016-12-01 14:45:40 +00:00
}
2020-10-02 13:45:57 +00:00
function _setup_postfix_vhost
2020-09-23 08:21:37 +00:00
{
_notify 'task' "Setting up Postfix vhost"
2016-12-01 14:45:40 +00:00
2020-09-23 08:21:37 +00:00
if [ [ -f /tmp/vhost.tmp ] ]
then
sort < /tmp/vhost.tmp | uniq > /etc/postfix/vhost && rm /tmp/vhost.tmp
elif [ [ ! -f /etc/postfix/vhost ] ]
then
2019-12-03 13:43:43 +00:00
touch /etc/postfix/vhost
2020-09-23 08:21:37 +00:00
fi
2016-12-01 14:45:40 +00:00
}
2020-10-02 13:45:57 +00:00
function _setup_inet_protocols
2020-09-23 08:21:37 +00:00
{
2020-09-05 14:53:36 +00:00
_notify 'task' 'Setting up POSTFIX_INET_PROTOCOLS option'
2020-09-23 08:21:37 +00:00
postconf -e " inet_protocols = ${ POSTFIX_INET_PROTOCOLS } "
}
2020-10-02 13:45:57 +00:00
function _setup_docker_permit
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Setting up PERMIT_DOCKER Option'
2020-09-26 13:11:52 +00:00
local CONTAINER_IP CONTAINER_NETWORK
2021-01-16 09:16:05 +00:00
2020-09-26 13:11:52 +00:00
unset CONTAINER_NETWORKS
declare -a CONTAINER_NETWORKS
2021-02-21 22:49:37 +00:00
CONTAINER_IP = $( ip addr show " ${ NETWORK_INTERFACE } " | grep 'inet ' | sed 's|[^0-9\.\/]*||g' | cut -d '/' -f 1)
2020-09-26 13:11:52 +00:00
CONTAINER_NETWORK = " $( echo " ${ CONTAINER_IP } " | cut -d '.' -f1-2) .0.0 "
while read -r IP
do
CONTAINER_NETWORKS += ( " ${ IP } " )
done < <( ip -o -4 addr show type veth | grep -E -o '[0-9\.]+/[0-9]+' )
2020-09-23 08:21:37 +00:00
case ${ PERMIT_DOCKER } in
"host" )
2020-09-26 13:11:52 +00:00
_notify 'inf' " Adding ${ CONTAINER_NETWORK } /16 to my networks "
postconf -e " $( postconf | grep '^mynetworks =' ) ${ CONTAINER_NETWORK } /16 "
echo " ${ CONTAINER_NETWORK } /16 " >> /etc/opendmarc/ignore.hosts
echo " ${ CONTAINER_NETWORK } /16 " >> /etc/opendkim/TrustedHosts
2020-09-23 08:21:37 +00:00
; ;
"network" )
_notify 'inf' "Adding docker network in my networks"
postconf -e " $( postconf | grep '^mynetworks =' ) 172.16.0.0/12 "
echo 172.16.0.0/12 >> /etc/opendmarc/ignore.hosts
echo 172.16.0.0/12 >> /etc/opendkim/TrustedHosts
; ;
"connected-networks" )
2020-09-26 13:11:52 +00:00
for NETWORK in " ${ CONTAINER_NETWORKS [@] } "
2020-09-23 08:21:37 +00:00
do
2020-09-26 13:11:52 +00:00
NETWORK = $( _sanitize_ipv4_to_subnet_cidr " ${ NETWORK } " )
_notify 'inf' " Adding docker network ${ NETWORK } in my networks "
postconf -e " $( postconf | grep '^mynetworks =' ) ${ NETWORK } "
echo " ${ NETWORK } " >> /etc/opendmarc/ignore.hosts
echo " ${ NETWORK } " >> /etc/opendkim/TrustedHosts
2020-09-23 08:21:37 +00:00
done
; ;
* )
_notify 'inf' "Adding container ip in my networks"
2020-09-26 13:11:52 +00:00
postconf -e " $( postconf | grep '^mynetworks =' ) ${ CONTAINER_IP } /32 "
echo " ${ CONTAINER_IP } /32 " >> /etc/opendmarc/ignore.hosts
echo " ${ CONTAINER_IP } /32 " >> /etc/opendkim/TrustedHosts
2020-09-23 08:21:37 +00:00
; ;
esac
}
2020-10-02 13:45:57 +00:00
function _setup_postfix_virtual_transport
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Setting up Postfix virtual transport'
[ [ -z ${ POSTFIX_DAGENT } ] ] && echo " ${ POSTFIX_DAGENT } not set. " && \
2020-09-23 19:53:07 +00:00
kill -15 " $( < /var/run/supervisord.pid) " && return 1
2020-09-23 08:21:37 +00:00
postconf -e " virtual_transport = ${ POSTFIX_DAGENT } "
}
2020-10-02 13:45:57 +00:00
function _setup_postfix_override_configuration
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Setting up Postfix Override configuration'
if [ [ -f /tmp/docker-mailserver/postfix-main.cf ] ]
then
2020-10-19 14:29:25 +00:00
while read -r LINE
2020-09-23 08:21:37 +00:00
do
# all valid postfix options start with a lower case letter
# http://www.postfix.org/postconf.5.html
2020-10-19 14:29:25 +00:00
if [ [ ${ LINE } = ~ ^[ a-z] ] ]
2020-09-23 08:21:37 +00:00
then
2020-10-19 14:29:25 +00:00
postconf -e " ${ LINE } "
2020-09-23 08:21:37 +00:00
fi
done < /tmp/docker-mailserver/postfix-main.cf
_notify 'inf' "Loaded 'config/postfix-main.cf'"
else
_notify 'inf' "No extra postfix settings loaded because optional '/tmp/docker-mailserver/postfix-main.cf' not provided."
fi
if [ [ -f /tmp/docker-mailserver/postfix-master.cf ] ]
then
2020-10-19 14:29:25 +00:00
while read -r LINE
2020-09-23 08:21:37 +00:00
do
2020-10-19 14:29:25 +00:00
if [ [ ${ LINE } = ~ ^[ 0-9a-z] ] ]
2020-09-23 08:21:37 +00:00
then
2020-10-19 14:29:25 +00:00
postconf -P " ${ LINE } "
2020-09-23 08:21:37 +00:00
fi
done < /tmp/docker-mailserver/postfix-master.cf
_notify 'inf' "Loaded 'config/postfix-master.cf'"
else
_notify 'inf' "No extra postfix settings loaded because optional '/tmp/docker-mailserver/postfix-master.cf' not provided."
fi
_notify 'inf' "set the compatibility level to 2"
postconf compatibility_level = 2
}
2020-10-02 13:45:57 +00:00
function _setup_postfix_sasl_password
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Setting up Postfix SASL Password'
# support general SASL password
rm -f /etc/postfix/sasl_passwd
if [ [ -n ${ SASL_PASSWD } ] ]
then
echo " ${ SASL_PASSWD } " >> /etc/postfix/sasl_passwd
fi
# install SASL passwords
if [ [ -f /etc/postfix/sasl_passwd ] ]
then
chown root:root /etc/postfix/sasl_passwd
chmod 0600 /etc/postfix/sasl_passwd
_notify 'inf' "Loaded SASL_PASSWD"
else
_notify 'inf' "Warning: 'SASL_PASSWD' is not provided. /etc/postfix/sasl_passwd not created."
fi
}
2020-10-02 13:45:57 +00:00
function _setup_postfix_default_relay_host
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Applying default relay host to Postfix'
_notify 'inf' " Applying default relay host ${ DEFAULT_RELAY_HOST } to /etc/postfix/main.cf "
postconf -e " relayhost = ${ DEFAULT_RELAY_HOST } "
}
2020-10-02 13:45:57 +00:00
function _setup_postfix_relay_hosts
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Setting up Postfix Relay Hosts'
2021-01-16 09:16:05 +00:00
[ [ -z ${ RELAY_PORT } ] ] && RELAY_PORT = 25
2020-09-23 08:21:37 +00:00
_notify 'inf' " Setting up outgoing email relaying via ${ RELAY_HOST } : ${ RELAY_PORT } "
# setup /etc/postfix/sasl_passwd
# --
# @domain1.com postmaster@domain1.com:your-password-1
# @domain2.com postmaster@domain2.com:your-password-2
# @domain3.com postmaster@domain3.com:your-password-3
#
# [smtp.mailgun.org]:587 postmaster@domain2.com:your-password-2
if [ [ -f /tmp/docker-mailserver/postfix-sasl-password.cf ] ]
then
_notify 'inf' "Adding relay authentication from postfix-sasl-password.cf"
2020-10-19 14:29:25 +00:00
while read -r LINE
2020-09-23 08:21:37 +00:00
do
2020-10-19 14:29:25 +00:00
if ! echo " ${ LINE } " | grep -q -e "^\s*#"
2020-09-23 08:21:37 +00:00
then
2020-10-19 14:29:25 +00:00
echo " ${ LINE } " >> /etc/postfix/sasl_passwd
2020-09-23 08:21:37 +00:00
fi
done < /tmp/docker-mailserver/postfix-sasl-password.cf
fi
# add default relay
if [ [ -n ${ RELAY_USER } ] ] && [ [ -n ${ RELAY_PASSWORD } ] ]
then
echo " [ ${ RELAY_HOST } ]: ${ RELAY_PORT } ${ RELAY_USER } : ${ RELAY_PASSWORD } " >> /etc/postfix/sasl_passwd
else
if [ [ ! -f /tmp/docker-mailserver/postfix-sasl-password.cf ] ]
then
_notify 'warn' "No relay auth file found and no default set"
fi
fi
if [ [ -f /etc/postfix/sasl_passwd ] ]
then
chown root:root /etc/postfix/sasl_passwd
chmod 0600 /etc/postfix/sasl_passwd
fi
# end /etc/postfix/sasl_passwd
_populate_relayhost_map
postconf -e \
"smtp_sasl_auth_enable = yes" \
"smtp_sasl_security_options = noanonymous" \
"smtp_sasl_password_maps = texthash:/etc/postfix/sasl_passwd" \
"smtp_use_tls = yes" \
"smtp_tls_security_level = encrypt" \
"smtp_tls_note_starttls_offer = yes" \
"smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt" \
"sender_dependent_relayhost_maps = texthash:/etc/postfix/relayhost_map" \
"smtp_sender_dependent_authentication = yes"
}
2020-10-02 13:45:57 +00:00
function _setup_postfix_dhparam
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Setting up Postfix dhparam'
if [ [ ${ ONE_DIR } -eq 1 ] ]
then
DHPARAMS_FILE = /var/mail-state/lib-shared/dhparams.pem
if [ [ ! -f ${ DHPARAMS_FILE } ] ]
then
_notify 'inf' "Use ffdhe4096 for dhparams (postfix)"
2020-10-21 14:00:35 +00:00
cp -f /etc/postfix/shared/ffdhe4096.pem /etc/postfix/dhparams.pem
2020-09-23 08:21:37 +00:00
else
_notify 'inf' "Use postfix dhparams that was generated previously"
2020-09-05 14:53:36 +00:00
_notify 'warn' "Using self-generated dhparams is considered as insecure."
_notify 'warn' "Unless you known what you are doing, please remove /var/mail-state/lib-shared/dhparams.pem."
2017-09-06 21:41:23 +00:00
2020-04-22 09:06:03 +00:00
# Copy from the state directory to the working location
2020-10-21 14:00:35 +00:00
cp -f " ${ DHPARAMS_FILE } " /etc/postfix/dhparams.pem
2020-09-23 08:21:37 +00:00
fi
else
if [ [ ! -f /etc/postfix/dhparams.pem ] ]
then
if [ [ -f /etc/dovecot/dh.pem ] ]
then
_notify 'inf' "Copy dovecot dhparams to postfix"
cp /etc/dovecot/dh.pem /etc/postfix/dhparams.pem
elif [ [ -f /tmp/docker-mailserver/dhparams.pem ] ]
then
_notify 'inf' "Copy pre-generated dhparams to postfix"
_notify 'warn' "Using self-generated dhparams is considered as insecure."
_notify 'warn' "Unless you known what you are doing, please remove /var/mail-state/lib-shared/dhparams.pem."
cp /tmp/docker-mailserver/dhparams.pem /etc/postfix/dhparams.pem
else
_notify 'inf' "Use ffdhe4096 for dhparams (postfix)"
cp /etc/postfix/shared/ffdhe4096.pem /etc/postfix/dhparams.pem
fi
else
_notify 'inf' "Use existing postfix dhparams"
_notify 'warn' "Using self-generated dhparams is considered insecure."
_notify 'warn' "Unless you known what you are doing, please remove /etc/postfix/dhparams.pem."
fi
fi
2019-07-23 14:12:12 +00:00
}
2020-10-02 13:45:57 +00:00
function _setup_dovecot_dhparam
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Setting up Dovecot dhparam'
2019-01-29 14:48:42 +00:00
2020-09-23 08:21:37 +00:00
if [ [ ${ ONE_DIR } -eq 1 ] ]
then
DHPARAMS_FILE = /var/mail-state/lib-shared/dhparams.pem
2019-01-29 14:48:42 +00:00
2020-09-23 08:21:37 +00:00
if [ [ ! -f ${ DHPARAMS_FILE } ] ]
then
_notify 'inf' "Use ffdhe4096 for dhparams (dovecot)"
2020-10-21 14:00:35 +00:00
cp -f /etc/postfix/shared/ffdhe4096.pem /etc/dovecot/dh.pem
2020-09-23 08:21:37 +00:00
else
_notify 'inf' "Use dovecot dhparams that was generated previously"
_notify 'warn' "Using self-generated dhparams is considered as insecure."
_notify 'warn' "Unless you known what you are doing, please remove /var/mail-state/lib-shared/dhparams.pem."
2020-04-22 09:06:03 +00:00
2020-09-23 08:21:37 +00:00
# Copy from the state directory to the working location
2020-10-21 14:00:35 +00:00
cp -f " ${ DHPARAMS_FILE } " /etc/dovecot/dh.pem
2020-09-23 08:21:37 +00:00
fi
else
if [ [ ! -f /etc/dovecot/dh.pem ] ]
then
if [ [ -f /etc/postfix/dhparams.pem ] ]
then
_notify 'inf' "Copy postfix dhparams to dovecot"
cp /etc/postfix/dhparams.pem /etc/dovecot/dh.pem
elif [ [ -f /tmp/docker-mailserver/dhparams.pem ] ]
2020-10-21 14:00:35 +00:00
then
2020-09-23 08:21:37 +00:00
_notify 'inf' "Copy pre-generated dhparams to dovecot"
_notify 'warn' "Using self-generated dhparams is considered as insecure."
_notify 'warn' "Unless you known what you are doing, please remove /tmp/docker-mailserver/dhparams.pem."
cp /tmp/docker-mailserver/dhparams.pem /etc/dovecot/dh.pem
else
2020-10-21 14:00:35 +00:00
_notify 'inf' "Use ffdhe4096 for dhparams (dovecot)"
2020-09-23 08:21:37 +00:00
cp /etc/postfix/shared/ffdhe4096.pem /etc/dovecot/dh.pem
fi
else
_notify 'inf' "Use existing dovecot dhparams"
_notify 'warn' "Using self-generated dhparams is considered as insecure."
_notify 'warn' "Unless you known what you are doing, please remove /etc/dovecot/dh.pem."
fi
fi
}
2020-04-22 09:06:03 +00:00
2020-10-02 13:45:57 +00:00
function _setup_security_stack
2020-09-23 08:21:37 +00:00
{
_notify 'task' "Setting up Security Stack"
2020-05-02 16:39:51 +00:00
2020-09-23 08:21:37 +00:00
# recreate auto-generated file
local DMS_AMAVIS_FILE = /etc/amavis/conf.d/61-dms_auto_generated
echo "# WARNING: this file is auto-generated." >" ${ DMS_AMAVIS_FILE } "
echo "use strict;" >>" ${ DMS_AMAVIS_FILE } "
# Spamassassin
if [ [ ${ ENABLE_SPAMASSASSIN } -eq 0 ] ]
then
_notify 'warn' "Spamassassin is disabled. You can enable it with 'ENABLE_SPAMASSASSIN=1'"
echo "@bypass_spam_checks_maps = (1);" >>" ${ DMS_AMAVIS_FILE } "
elif [ [ ${ ENABLE_SPAMASSASSIN } -eq 1 ] ]
then
_notify 'inf' "Enabling and configuring spamassassin"
# shellcheck disable=SC2016
2021-02-21 22:49:37 +00:00
SA_TAG = ${ SA_TAG : = "2.0" } && sed -i -r 's|^\$sa_tag_level_deflt (.*);|\$sa_tag_level_deflt = ' " ${ SA_TAG } " ';|g' /etc/amavis/conf.d/20-debian_defaults
2020-09-23 08:21:37 +00:00
# shellcheck disable=SC2016
2021-02-21 22:49:37 +00:00
SA_TAG2 = ${ SA_TAG2 : = "6.31" } && sed -i -r 's|^\$sa_tag2_level_deflt (.*);|\$sa_tag2_level_deflt = ' " ${ SA_TAG2 } " ';|g' /etc/amavis/conf.d/20-debian_defaults
2020-09-23 08:21:37 +00:00
# shellcheck disable=SC2016
2021-02-21 22:49:37 +00:00
SA_KILL = ${ SA_KILL : = "6.31" } && sed -i -r 's|^\$sa_kill_level_deflt (.*);|\$sa_kill_level_deflt = ' " ${ SA_KILL } " ';|g' /etc/amavis/conf.d/20-debian_defaults
2020-09-23 08:21:37 +00:00
SA_SPAM_SUBJECT = ${ SA_SPAM_SUBJECT : = "***SPAM*** " }
if [ [ ${ SA_SPAM_SUBJECT } = = "undef" ] ]
then
# shellcheck disable=SC2016
2021-02-21 22:49:37 +00:00
sed -i -r 's|^\$sa_spam_subject_tag (.*);|\$sa_spam_subject_tag = undef;|g' /etc/amavis/conf.d/20-debian_defaults
2020-09-23 08:21:37 +00:00
else
# shellcheck disable=SC2016
2021-02-21 22:49:37 +00:00
sed -i -r 's|^\$sa_spam_subject_tag (.*);|\$sa_spam_subject_tag = ' " ' ${ SA_SPAM_SUBJECT } ' " ';|g' /etc/amavis/conf.d/20-debian_defaults
2020-09-23 08:21:37 +00:00
fi
# activate short circuits when SA BAYES is certain it has spam or ham.
if [ [ ${ SA_SHORTCIRCUIT_BAYES_SPAM } -eq 1 ] ]
then
# automatically activate the Shortcircuit Plugin
2021-02-21 22:49:37 +00:00
sed -i -r 's|^# loadplugin Mail::SpamAssassin::Plugin::Shortcircuit|loadplugin Mail::SpamAssassin::Plugin::Shortcircuit|g' /etc/spamassassin/v320.pre
sed -i -r 's|^# shortcircuit BAYES_99|shortcircuit BAYES_99|g' /etc/spamassassin/local.cf
2020-09-23 08:21:37 +00:00
fi
if [ [ ${ SA_SHORTCIRCUIT_BAYES_HAM } -eq 1 ] ]
then
# automatically activate the Shortcircuit Plugin
2021-02-21 22:49:37 +00:00
sed -i -r 's|^# loadplugin Mail::SpamAssassin::Plugin::Shortcircuit|loadplugin Mail::SpamAssassin::Plugin::Shortcircuit|g' /etc/spamassassin/v320.pre
sed -i -r 's|^# shortcircuit BAYES_00|shortcircuit BAYES_00|g' /etc/spamassassin/local.cf
2020-09-23 08:21:37 +00:00
fi
if [ [ -e /tmp/docker-mailserver/spamassassin-rules.cf ] ]
then
cp /tmp/docker-mailserver/spamassassin-rules.cf /etc/spamassassin/
fi
if [ [ ${ SPAMASSASSIN_SPAM_TO_INBOX } -eq 1 ] ]
then
_notify 'inf' "Configure Spamassassin/Amavis to put SPAM inbox"
2021-02-21 22:49:37 +00:00
sed -i " s|\$final_spam_destiny.*=.* $|\$final_spam_destiny = D_PASS;|g " /etc/amavis/conf.d/49-docker-mailserver
sed -i " s|\$final_bad_header_destiny.*=.* $|\$final_bad_header_destiny = D_PASS;|g " /etc/amavis/conf.d/49-docker-mailserver
2020-09-23 08:21:37 +00:00
else
2021-02-21 22:49:37 +00:00
sed -i " s|\$final_spam_destiny.*=.* $|\$final_spam_destiny = D_BOUNCE;|g " /etc/amavis/conf.d/49-docker-mailserver
sed -i " s|\$final_bad_header_destiny.*=.* $|\$final_bad_header_destiny = D_BOUNCE;|g " /etc/amavis/conf.d/49-docker-mailserver
2020-09-23 08:21:37 +00:00
2021-01-16 09:16:05 +00:00
if ! ${ SPAMASSASSIN_SPAM_TO_INBOX_IS_SET }
2020-09-23 08:21:37 +00:00
then
2020-09-24 12:54:21 +00:00
_notify 'warn' "Spam messages WILL NOT BE DELIVERED, you will NOT be notified of ANY message bounced. Please define SPAMASSASSIN_SPAM_TO_INBOX explicitly."
2020-09-23 08:21:37 +00:00
fi
fi
fi
# Clamav
if [ [ ${ ENABLE_CLAMAV } -eq 0 ] ]
then
_notify 'warn' "Clamav is disabled. You can enable it with 'ENABLE_CLAMAV=1'"
echo "@bypass_virus_checks_maps = (1);" >>" ${ DMS_AMAVIS_FILE } "
elif [ [ ${ ENABLE_CLAMAV } -eq 1 ] ]
then
_notify 'inf' "Enabling clamav"
fi
echo "1; # ensure a defined return" >>" ${ DMS_AMAVIS_FILE } "
chmod 444 " ${ DMS_AMAVIS_FILE } "
# Fail2ban
if [ [ ${ ENABLE_FAIL2BAN } -eq 1 ] ]
then
_notify 'inf' "Fail2ban enabled"
if [ [ -e /tmp/docker-mailserver/fail2ban-fail2ban.cf ] ]
then
cp /tmp/docker-mailserver/fail2ban-fail2ban.cf /etc/fail2ban/fail2ban.local
fi
if [ [ -e /tmp/docker-mailserver/fail2ban-jail.cf ] ]
then
cp /tmp/docker-mailserver/fail2ban-jail.cf /etc/fail2ban/jail.local
fi
else
# disable logrotate config for fail2ban if not enabled
rm -f /etc/logrotate.d/fail2ban
fi
# fix cron.daily for spamassassin
2021-02-21 22:49:37 +00:00
sed -i -e 's|invoke-rc.d spamassassin reload|/etc/init\.d/spamassassin reload|g' /etc/cron.daily/spamassassin
2020-09-23 08:21:37 +00:00
# copy user provided configuration files if provided
if [ [ -f /tmp/docker-mailserver/amavis.cf ] ]
then
cp /tmp/docker-mailserver/amavis.cf /etc/amavis/conf.d/50-user
fi
}
2020-10-02 13:45:57 +00:00
function _setup_logrotate
2020-09-23 08:21:37 +00:00
{
_notify 'inf' "Setting up logrotate"
LOGROTATE = '/var/log/mail/mail.log\n{\n compress\n copytruncate\n delaycompress\n'
case " ${ LOGROTATE_INTERVAL } " in
"daily" )
_notify 'inf' "Setting postfix logrotate interval to daily"
2020-11-06 23:54:50 +00:00
LOGROTATE = " ${ LOGROTATE } rotate 4\n daily\n "
2020-09-23 08:21:37 +00:00
; ;
"weekly" )
_notify 'inf' "Setting postfix logrotate interval to weekly"
2020-11-06 23:54:50 +00:00
LOGROTATE = " ${ LOGROTATE } rotate 4\n weekly\n "
2020-09-23 08:21:37 +00:00
; ;
"monthly" )
_notify 'inf' "Setting postfix logrotate interval to monthly"
2020-11-06 23:54:50 +00:00
LOGROTATE = " ${ LOGROTATE } rotate 4\n monthly\n "
2020-09-23 08:21:37 +00:00
; ;
* ) _notify 'warn' 'LOGROTATE_INTERVAL not found in _setup_logrotate' ; ;
esac
LOGROTATE = " ${ LOGROTATE } } "
echo -e " ${ LOGROTATE } " > /etc/logrotate.d/maillog
}
2020-10-02 13:45:57 +00:00
function _setup_mail_summary
2020-09-23 08:21:37 +00:00
{
_notify 'inf' " Enable postfix summary with recipient ${ PFLOGSUMM_RECIPIENT } "
case " ${ PFLOGSUMM_TRIGGER } " in
"daily_cron" )
_notify 'inf' "Creating daily cron job for pflogsumm report"
2021-01-02 11:04:19 +00:00
echo "#! /bin/bash" > /etc/cron.daily/postfix-summary
2020-09-23 08:21:37 +00:00
echo " /usr/local/bin/report-pflogsumm-yesterday ${ HOSTNAME } ${ PFLOGSUMM_RECIPIENT } ${ PFLOGSUMM_SENDER } " >> /etc/cron.daily/postfix-summary
chmod +x /etc/cron.daily/postfix-summary
; ;
"logrotate" )
_notify 'inf' "Add postrotate action for pflogsumm report"
sed -i " s|}| postrotate\n /usr/local/bin/postfix-summary ${ HOSTNAME } ${ PFLOGSUMM_RECIPIENT } ${ PFLOGSUMM_SENDER } \n endscript\n}\n| " /etc/logrotate.d/maillog
; ;
2020-10-21 17:45:47 +00:00
"none" ) _notify 'inf' "Postfix log summary reports disabled. You can enable them with 'PFLOGSUMM_TRIGGER=daily_cron' or 'PFLOGSUMM_TRIGGER=logrotate'" ; ;
2020-09-23 08:21:37 +00:00
* ) _notify 'err' 'PFLOGSUMM_TRIGGER not found in _setup_mail_summery' ; ;
esac
}
2020-10-02 13:45:57 +00:00
function _setup_logwatch
2020-09-23 08:21:37 +00:00
{
_notify 'inf' " Enable logwatch reports with recipient ${ LOGWATCH_RECIPIENT } "
2020-05-02 20:38:37 +00:00
2020-01-13 22:58:34 +00:00
echo "LogFile = /var/log/mail/freshclam.log" >> /etc/logwatch/conf/logfiles/clam-update.conf
2020-09-23 08:21:37 +00:00
case " ${ LOGWATCH_INTERVAL } " in
"daily" )
_notify 'inf' "Creating daily cron job for logwatch reports"
2021-01-02 11:04:19 +00:00
echo "#! /bin/bash" > /etc/cron.daily/logwatch
2020-09-23 08:21:37 +00:00
echo " /usr/sbin/logwatch --range Yesterday --hostname ${ HOSTNAME } --mailto ${ LOGWATCH_RECIPIENT } " \
>> /etc/cron.daily/logwatch
chmod 744 /etc/cron.daily/logwatch
; ;
"weekly" )
_notify 'inf' "Creating weekly cron job for logwatch reports"
2021-01-02 11:04:19 +00:00
echo "#! /bin/bash" > /etc/cron.weekly/logwatch
2020-09-23 08:21:37 +00:00
echo " /usr/sbin/logwatch --range 'between -7 days and -1 days' --hostname ${ HOSTNAME } --mailto ${ LOGWATCH_RECIPIENT } " \
>> /etc/cron.weekly/logwatch
chmod 744 /etc/cron.weekly/logwatch
; ;
2020-10-21 17:45:47 +00:00
"none" ) _notify 'inf' "Logwatch reports disabled. You can enable them with 'LOGWATCH_INTERVAL=daily' or 'LOGWATCH_INTERVAL=weekly'" ; ;
2020-09-23 08:21:37 +00:00
* ) _notify 'warn' 'LOGWATCH_INTERVAL not found in _setup_logwatch' ; ;
esac
}
2021-01-16 09:16:05 +00:00
function _setup_user_patches
2020-09-23 08:21:37 +00:00
{
if [ [ -f /tmp/docker-mailserver/user-patches.sh ] ]
then
2020-09-28 09:42:50 +00:00
_notify 'inf' 'Executing user-patches.sh'
chmod +x /tmp/docker-mailserver/user-patches.sh & >/dev/null || true
if [ [ -x /tmp/docker-mailserver/user-patches.sh ] ]
then
/tmp/docker-mailserver/user-patches.sh
_notify 'inf' "Executed 'config/user-patches.sh'"
else
_notify 'err' "Could not execute user-patches.sh. Not executable!"
fi
2020-09-23 08:21:37 +00:00
else
_notify 'inf' "No user patches executed because optional '/tmp/docker-mailserver/user-patches.sh' is not provided."
fi
2019-10-16 16:56:06 +00:00
}
2021-01-16 09:16:05 +00:00
function _setup_environment
{
2020-09-23 08:21:37 +00:00
_notify 'task' 'Setting up /etc/environment'
2021-01-16 09:16:05 +00:00
local BANNER = "# Docker Environment"
2020-09-23 08:21:37 +00:00
2021-01-16 09:16:05 +00:00
if ! grep -q " ${ BANNER } " /etc/environment
2020-09-23 08:21:37 +00:00
then
2021-01-16 09:16:05 +00:00
echo " ${ BANNER } " >> /etc/environment
echo " VIRUSMAILS_DELETE_DELAY= ${ VIRUSMAILS_DELETE_DELAY } " >> /etc/environment
2020-09-23 08:21:37 +00:00
fi
2017-03-14 14:22:19 +00:00
}
2016-12-01 14:45:40 +00:00
##########################################################################
# << Setup Stack
##########################################################################
##########################################################################
# >> Fix Stack
2016-04-11 22:04:33 +00:00
#
2016-12-01 14:45:40 +00:00
# Description: Place functions for temporary workarounds and fixes here
##########################################################################
2017-08-24 16:07:20 +00:00
2017-09-10 13:26:21 +00:00
2020-10-02 13:45:57 +00:00
function fix
2020-09-23 08:21:37 +00:00
{
_notify 'taskgrg' "Post-configuration checks..."
for FUNC in " ${ FUNCS_FIX [@] } "
do
if ! ${ FUNC }
then
_defunc
fi
done
_notify 'taskgrg' "Remove leftover pid files from a stop/start"
rm -rf /var/run/*.pid /var/run/*/*.pid
touch /dev/shm/supervisor.sock
2016-12-01 14:45:40 +00:00
}
2020-10-02 13:45:57 +00:00
function _fix_var_mail_permissions
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Checking /var/mail permissions'
2016-12-01 14:45:40 +00:00
2020-09-23 08:21:37 +00:00
# dix permissions, but skip this if 3 levels deep the user id is already set
if [ [ $( find /var/mail -maxdepth 3 -a \( \! -user 5000 -o \! -group 5000 \) | grep -c .) -ne 0 ] ]
then
_notify 'inf' "Fixing /var/mail permissions"
chown -R 5000:5000 /var/mail
else
_notify 'inf' "Permissions in /var/mail look OK"
return 0
fi
2016-12-01 14:45:40 +00:00
}
2017-02-07 19:53:34 +00:00
2020-10-02 13:45:57 +00:00
function _fix_var_amavis_permissions
2020-09-23 08:21:37 +00:00
{
if [ [ ${ ONE_DIR } -eq 0 ] ]
then
amavis_state_dir = /var/lib/amavis
else
amavis_state_dir = /var/mail-state/lib-amavis
fi
2017-02-08 22:31:56 +00:00
2020-09-23 08:21:37 +00:00
# shellcheck disable=SC2016
_notify 'task' 'Checking $amavis_state_dir permissions'
2017-02-07 19:53:34 +00:00
2020-09-23 08:21:37 +00:00
amavis_permissions_status = $( find -H " ${ amavis_state_dir } " -maxdepth 3 -a \( \! -user amavis -o \! -group amavis \) )
if [ [ -n ${ amavis_permissions_status } ] ]
then
_notify 'inf' " Fixing ${ amavis_state_dir } permissions "
chown -hR amavis:amavis " ${ amavis_state_dir } "
else
_notify 'inf' " Permissions in ${ amavis_state_dir } look OK "
return 0
fi
2017-02-07 19:53:34 +00:00
}
2020-10-02 13:45:57 +00:00
function _fix_cleanup_clamav
2020-09-23 08:21:37 +00:00
{
2020-09-05 14:53:36 +00:00
_notify 'task' 'Cleaning up disabled Clamav'
2017-07-23 19:31:41 +00:00
rm -f /etc/logrotate.d/clamav-*
2019-07-29 09:15:49 +00:00
rm -f /etc/cron.d/clamav-freshclam
2017-07-23 19:31:41 +00:00
}
2020-10-02 13:45:57 +00:00
function _fix_cleanup_spamassassin
2020-09-23 08:21:37 +00:00
{
2020-09-05 14:53:36 +00:00
_notify 'task' 'Cleaning up disabled spamassassin'
2019-08-11 07:52:50 +00:00
rm -f /etc/cron.daily/spamassassin
}
2016-12-01 14:45:40 +00:00
##########################################################################
# << Fix Stack
##########################################################################
##########################################################################
# >> Misc Stack
2016-04-11 22:04:33 +00:00
#
2016-12-01 14:45:40 +00:00
# Description: Place functions that do not fit in the sections above here
##########################################################################
2020-09-23 08:21:37 +00:00
2020-10-02 13:45:57 +00:00
function misc
2020-09-23 08:21:37 +00:00
{
2021-01-23 18:10:15 +00:00
_notify 'inf' 'Starting misc'
2020-09-23 08:21:37 +00:00
for FUNC in " ${ FUNCS_MISC [@] } "
do
if ! ${ FUNC }
then
_defunc
fi
done
}
2021-02-09 11:12:36 +00:00
# consolidate all states into a single directory
# (/var/mail-state) to allow persistence using docker volumes
2020-10-02 13:45:57 +00:00
function _misc_save_states
2020-09-23 08:21:37 +00:00
{
2021-02-09 11:12:36 +00:00
local STATEDIR FILE FILES
2020-09-23 08:21:37 +00:00
2021-02-09 11:12:36 +00:00
STATEDIR = '/var/mail-state'
2021-02-07 17:11:33 +00:00
if [ [ ${ ONE_DIR } -eq 1 ] ] && [ [ -d ${ STATEDIR } ] ]
2020-09-23 08:21:37 +00:00
then
2021-02-07 17:11:33 +00:00
_notify 'inf' " Consolidating all state onto ${ STATEDIR } "
2020-09-23 08:21:37 +00:00
2021-02-09 11:12:36 +00:00
FILES = (
2021-02-07 17:11:33 +00:00
spool/postfix
lib/postfix
lib/amavis
lib/clamav
lib/spamassassin
lib/fail2ban
lib/postgrey
lib/dovecot
2020-09-23 08:21:37 +00:00
)
2021-02-07 17:11:33 +00:00
for FILE in " ${ FILES [@] } "
2020-09-23 08:21:37 +00:00
do
2021-02-07 17:11:33 +00:00
DEST = " ${ STATEDIR } / ${ FILE // \/ /- } "
2021-02-09 11:12:36 +00:00
FILE = " /var/ ${ FILE } "
2020-09-23 08:21:37 +00:00
2021-02-07 17:11:33 +00:00
if [ [ -d ${ DEST } ] ]
2020-09-23 08:21:37 +00:00
then
2021-02-07 17:11:33 +00:00
_notify 'inf' " Destination ${ DEST } exists, linking ${ FILE } to it "
rm -rf " ${ FILE } "
ln -s " ${ DEST } " " ${ FILE } "
elif [ [ -d ${ FILE } ] ]
2020-09-23 08:21:37 +00:00
then
2021-02-07 17:11:33 +00:00
_notify 'inf' " Moving contents of ${ FILE } to ${ DEST } : " " $( ls " ${ FILE } " ) "
mv " ${ FILE } " " ${ DEST } "
ln -s " ${ DEST } " " ${ FILE } "
2020-09-23 08:21:37 +00:00
else
2021-02-07 17:11:33 +00:00
_notify 'inf' " Linking ${ FILE } to ${ DEST } "
mkdir -p " ${ DEST } "
ln -s " ${ DEST } " " ${ FILE } "
2020-09-23 08:21:37 +00:00
fi
done
_notify 'inf' 'Fixing /var/mail-state/* permissions'
chown -R clamav /var/mail-state/lib-clamav
chown -R postfix /var/mail-state/lib-postfix
chown -R postgrey /var/mail-state/lib-postgrey
chown -R debian-spamd /var/mail-state/lib-spamassassin
chown -R postfix /var/mail-state/spool-postfix
fi
2016-12-01 14:45:40 +00:00
}
##########################################################################
# >> Start Daemons
##########################################################################
2020-10-02 13:45:57 +00:00
function start_daemons
2020-09-23 08:21:37 +00:00
{
2021-01-16 09:16:05 +00:00
_notify 'tasklog' 'Starting mail server'
2016-12-01 14:45:40 +00:00
2020-09-23 08:21:37 +00:00
for FUNC in " ${ DAEMONS_START [@] } "
do
if ! ${ FUNC }
then
_defunc
fi
done
2016-12-23 22:56:39 +00:00
}
2016-12-01 14:45:40 +00:00
2020-10-02 13:45:57 +00:00
function _start_daemons_cron
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Starting cron' 'n'
supervisorctl start cron
2016-12-01 14:45:40 +00:00
}
2020-10-02 13:45:57 +00:00
function _start_daemons_rsyslog
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Starting rsyslog ' 'n'
supervisorctl start rsyslog
2016-12-01 14:45:40 +00:00
}
2020-10-02 13:45:57 +00:00
function _start_daemons_saslauthd
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Starting saslauthd' 'n'
supervisorctl start " saslauthd_ ${ SASLAUTHD_MECHANISMS } "
2016-12-01 14:45:40 +00:00
}
2020-10-02 13:45:57 +00:00
function _start_daemons_fail2ban
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Starting fail2ban ' 'n'
touch /var/log/auth.log
# delete fail2ban.sock that probably was left here after container restart
if [ [ -e /var/run/fail2ban/fail2ban.sock ] ]
then
rm /var/run/fail2ban/fail2ban.sock
fi
supervisorctl start fail2ban
2016-12-01 14:45:40 +00:00
}
2020-10-02 13:45:57 +00:00
function _start_daemons_opendkim
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Starting opendkim ' 'n'
supervisorctl start opendkim
2016-12-01 14:45:40 +00:00
}
2020-10-02 13:45:57 +00:00
function _start_daemons_opendmarc
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Starting opendmarc ' 'n'
supervisorctl start opendmarc
2018-03-18 19:15:06 +00:00
}
2020-10-02 13:45:57 +00:00
function _start_daemons_postsrsd
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Starting postsrsd ' 'n'
supervisorctl start postsrsd
2016-12-01 14:45:40 +00:00
}
2020-10-02 13:45:57 +00:00
function _start_daemons_postfix
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Starting postfix' 'n'
supervisorctl start postfix
}
2017-08-07 15:39:40 +00:00
2020-10-02 13:45:57 +00:00
function _start_daemons_dovecot
2020-09-23 08:21:37 +00:00
{
# Here we are starting sasl and imap, not pop3 because it's disabled by default
_notify 'task' 'Starting dovecot services' 'n'
2016-12-01 14:45:40 +00:00
2020-09-23 08:21:37 +00:00
if [ [ ${ ENABLE_POP3 } -eq 1 ] ]
then
_notify 'task' 'Starting pop3 services' 'n'
mv /etc/dovecot/protocols.d/pop3d.protocol.disab /etc/dovecot/protocols.d/pop3d.protocol
fi
2016-12-01 14:45:40 +00:00
2020-09-23 08:21:37 +00:00
if [ [ -f /tmp/docker-mailserver/dovecot.cf ] ]
then
cp /tmp/docker-mailserver/dovecot.cf /etc/dovecot/local.conf
fi
2016-12-01 14:45:40 +00:00
2020-09-23 08:21:37 +00:00
supervisorctl start dovecot
2017-08-07 15:39:40 +00:00
2020-09-23 08:21:37 +00:00
# TODO fix: on integration test
# doveadm: Error: userdb lookup: connect(/var/run/dovecot/auth-userdb) failed: No such file or directory
# doveadm: Fatal: user listing failed
2016-12-01 14:45:40 +00:00
2020-09-23 08:21:37 +00:00
# if [[ ${ENABLE_LDAP} -ne 1 ]]
# then
# echo "Listing users"
# /usr/sbin/dovecot user '*'
# fi
2016-12-01 14:45:40 +00:00
}
2020-10-02 13:45:57 +00:00
function _start_daemons_fetchmail
2020-09-23 08:21:37 +00:00
{
2021-01-17 09:39:09 +00:00
_notify 'task' 'Preparing fetchmail config'
2020-09-23 08:21:37 +00:00
/usr/local/bin/setup-fetchmail
2021-01-17 09:39:09 +00:00
if [ [ ${ FETCHMAIL_PARALLEL } -eq 1 ] ]
then
mkdir /etc/fetchmailrc.d/
/usr/local/bin/fetchmailrc_split
COUNTER = 0
for RC in /etc/fetchmailrc.d/fetchmail-*.rc
do
COUNTER = $(( COUNTER+1))
cat <<EOF > "/etc/supervisor/conf.d/fetchmail-${COUNTE R} .conf"
[ program:fetchmail-${ COUNTER } ]
startsecs = 0
autostart = false
autorestart = true
stdout_logfile = /var/log/supervisor/%( program_name) s.log
stderr_logfile = /var/log/supervisor/%( program_name) s.log
user = fetchmail
command = /usr/bin/fetchmail -f ${ RC } -v --nodetach --daemon %( ENV_FETCHMAIL_POLL) s -i /var/lib/fetchmail/.fetchmail-UIDL-cache --pidfile /var/run/fetchmail/%( program_name) s.pid
EOF
chmod 700 " ${ RC } "
chown fetchmail:root " ${ RC } "
done
supervisorctl reread
supervisorctl update
COUNTER = 0
for _ in /etc/fetchmailrc.d/fetchmail-*.rc
do
COUNTER = $(( COUNTER+1))
_notify 'task' " Starting fetchmail instance ${ COUNTER } " 'n'
supervisorctl start " fetchmail- ${ COUNTER } "
done
else
_notify 'task' 'Starting fetchmail' 'n'
supervisorctl start fetchmail
fi
2016-12-01 14:45:40 +00:00
}
2020-10-02 13:45:57 +00:00
function _start_daemons_clamav
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Starting clamav' 'n'
supervisorctl start clamav
2016-12-01 14:45:40 +00:00
}
2020-10-02 13:45:57 +00:00
function _start_daemons_postgrey
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Starting postgrey' 'n'
rm -f /var/run/postgrey/postgrey.pid
supervisorctl start postgrey
2017-02-06 09:21:18 +00:00
}
2020-10-02 13:45:57 +00:00
function _start_daemons_amavis
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Starting amavis' 'n'
supervisorctl start amavis
2016-12-01 14:45:40 +00:00
}
2017-01-20 22:30:29 +00:00
2016-12-01 14:45:40 +00:00
##########################################################################
# << Start Daemons
##########################################################################
2017-10-10 06:15:18 +00:00
##########################################################################
# Start check for update postfix-accounts and postfix-virtual
##########################################################################
2016-12-01 14:45:40 +00:00
2020-10-02 13:45:57 +00:00
function _start_changedetector
2020-09-23 08:21:37 +00:00
{
_notify 'task' 'Starting changedetector' 'n'
supervisorctl start changedetector
2017-10-10 06:15:18 +00:00
}
2016-12-01 14:45:40 +00:00
2021-01-16 09:16:05 +00:00
# ! – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
# ! – – – CARE – BEGIN – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
# ! – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
2016-12-01 14:45:40 +00:00
2020-10-21 16:16:32 +00:00
# shellcheck source=./helper-functions.sh
. /usr/local/bin/helper-functions.sh
2019-08-07 00:24:56 +00:00
2021-01-16 09:16:05 +00:00
if [ [ ${ DMS_DEBUG :- 0 } -eq 1 ] ]
2020-09-23 08:21:37 +00:00
then
2021-01-22 09:03:31 +00:00
_notify 'inf' 'ENVIRONMENT'
2020-09-23 08:21:37 +00:00
printenv
2016-12-23 22:56:39 +00:00
fi
2021-01-22 09:03:31 +00:00
_notify 'inf' 'Welcome to docker-mailserver!'
2016-12-01 14:45:40 +00:00
register_functions
2017-02-07 19:53:34 +00:00
check
2016-12-01 14:45:40 +00:00
setup
fix
misc
start_daemons
2021-01-16 09:16:05 +00:00
_notify 'tasklog' " ${ HOSTNAME } is up and running "
2016-12-23 22:56:39 +00:00
2017-08-07 15:39:40 +00:00
touch /var/log/mail/mail.log
2016-12-23 22:56:39 +00:00
tail -fn 0 /var/log/mail/mail.log
2016-12-01 14:45:40 +00:00
2021-01-16 09:16:05 +00:00
# ! – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
# ! – – – CARE – END – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
# ! – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
2016-12-01 14:45:40 +00:00
exit 0