docker-mailserver/target/scripts/start-mailserver.sh

284 lines
10 KiB
Bash
Raw Normal View History

#! /bin/bash
# shellcheck source=./helpers/index.sh
source /usr/local/bin/helpers/index.sh
# shellcheck disable=SC2034
declare -A VARS
declare -a FUNCS_SETUP FUNCS_FIX FUNCS_CHECK FUNCS_MISC DAEMONS_START
_obtain_hostname_and_domainname
# ------------------------------------------------------------
# ? <<
# --
# ? >> Setup of default and global values / variables
# ------------------------------------------------------------
# These variables must be defined first; They are used as default values for other variables.
VARS[POSTMASTER_ADDRESS]="${POSTMASTER_ADDRESS:=postmaster@${DOMAINNAME}}"
VARS[REPORT_RECIPIENT]="${REPORT_RECIPIENT:=${POSTMASTER_ADDRESS}}"
VARS[REPORT_SENDER]="${REPORT_SENDER:=mailserver-report@${HOSTNAME}}"
VARS[AMAVIS_LOGLEVEL]="${AMAVIS_LOGLEVEL:=0}"
VARS[CLAMAV_MESSAGE_SIZE_LIMIT]="${CLAMAV_MESSAGE_SIZE_LIMIT:=25M}" # 25 MB
VARS[DEFAULT_RELAY_HOST]="${DEFAULT_RELAY_HOST:=}"
VARS[DMS_DEBUG]="${DMS_DEBUG:=0}"
VARS[DOVECOT_INET_PROTOCOLS]="${DOVECOT_INET_PROTOCOLS:=all}"
VARS[DOVECOT_MAILBOX_FORMAT]="${DOVECOT_MAILBOX_FORMAT:=maildir}"
VARS[DOVECOT_TLS]="${DOVECOT_TLS:=no}"
VARS[ENABLE_AMAVIS]="${ENABLE_AMAVIS:=1}"
VARS[ENABLE_CLAMAV]="${ENABLE_CLAMAV:=0}"
VARS[ENABLE_DNSBL]="${ENABLE_DNSBL:=0}"
VARS[ENABLE_FAIL2BAN]="${ENABLE_FAIL2BAN:=0}"
VARS[ENABLE_FETCHMAIL]="${ENABLE_FETCHMAIL:=0}"
VARS[ENABLE_LDAP]="${ENABLE_LDAP:=0}"
VARS[ENABLE_MANAGESIEVE]="${ENABLE_MANAGESIEVE:=0}"
VARS[ENABLE_POP3]="${ENABLE_POP3:=0}"
VARS[ENABLE_POSTGREY]="${ENABLE_POSTGREY:=0}"
VARS[ENABLE_QUOTAS]="${ENABLE_QUOTAS:=1}"
VARS[ENABLE_SASLAUTHD]="${ENABLE_SASLAUTHD:=0}"
VARS[ENABLE_SPAMASSASSIN]="${ENABLE_SPAMASSASSIN:=0}"
2022-02-21 09:48:28 +00:00
VARS[ENABLE_SPAMASSASSIN_KAM]="${ENABLE_SPAMASSASSIN_KAM:=0}"
VARS[ENABLE_SRS]="${ENABLE_SRS:=0}"
VARS[ENABLE_UPDATE_CHECK]="${ENABLE_UPDATE_CHECK:=1}"
VARS[FAIL2BAN_BLOCKTYPE]="${FAIL2BAN_BLOCKTYPE:=drop}"
VARS[FETCHMAIL_PARALLEL]="${FETCHMAIL_PARALLEL:=0}"
2021-11-02 10:51:36 +00:00
VARS[FETCHMAIL_POLL]="${FETCHMAIL_POLL:=300}"
VARS[LDAP_START_TLS]="${LDAP_START_TLS:=no}"
VARS[LOG_LEVEL]="${LOG_LEVEL:=info}"
VARS[LOGROTATE_INTERVAL]="${LOGROTATE_INTERVAL:=weekly}"
VARS[LOGWATCH_INTERVAL]="${LOGWATCH_INTERVAL:=none}"
VARS[LOGWATCH_RECIPIENT]="${LOGWATCH_RECIPIENT:=${REPORT_RECIPIENT}}"
VARS[LOGWATCH_SENDER]="${LOGWATCH_SENDER:=${REPORT_SENDER}}"
VARS[MOVE_SPAM_TO_JUNK]="${MOVE_SPAM_TO_JUNK:=1}"
VARS[NETWORK_INTERFACE]="${NETWORK_INTERFACE:=eth0}"
VARS[ONE_DIR]="${ONE_DIR:=1}"
VARS[OVERRIDE_HOSTNAME]="${OVERRIDE_HOSTNAME:-}"
VARS[PERMIT_DOCKER]="${PERMIT_DOCKER:=none}"
VARS[PFLOGSUMM_RECIPIENT]="${PFLOGSUMM_RECIPIENT:=${REPORT_RECIPIENT}}"
VARS[PFLOGSUMM_SENDER]="${PFLOGSUMM_SENDER:=${REPORT_SENDER}}"
VARS[PFLOGSUMM_TRIGGER]="${PFLOGSUMM_TRIGGER:=none}"
2021-11-02 10:51:36 +00:00
VARS[POSTFIX_INET_PROTOCOLS]="${POSTFIX_INET_PROTOCOLS:=all}"
VARS[POSTFIX_MAILBOX_SIZE_LIMIT]="${POSTFIX_MAILBOX_SIZE_LIMIT:=0}"
VARS[POSTFIX_MESSAGE_SIZE_LIMIT]="${POSTFIX_MESSAGE_SIZE_LIMIT:=10240000}" # ~10 MB
VARS[POSTGREY_AUTO_WHITELIST_CLIENTS]="${POSTGREY_AUTO_WHITELIST_CLIENTS:=5}"
VARS[POSTGREY_DELAY]="${POSTGREY_DELAY:=300}"
VARS[POSTGREY_MAX_AGE]="${POSTGREY_MAX_AGE:=35}"
VARS[POSTGREY_TEXT]="${POSTGREY_TEXT:=Delayed by Postgrey}"
VARS[POSTSCREEN_ACTION]="${POSTSCREEN_ACTION:=enforce}"
VARS[RELAY_HOST]="${RELAY_HOST:=}"
2021-11-02 10:51:36 +00:00
VARS[SA_KILL]=${SA_KILL:="6.31"}
VARS[SA_SPAM_SUBJECT]=${SA_SPAM_SUBJECT:="***SPAM*** "}
VARS[SA_TAG]=${SA_TAG:="2.0"}
VARS[SA_TAG2]=${SA_TAG2:="6.31"}
VARS[SMTP_ONLY]="${SMTP_ONLY:=0}"
VARS[SPAMASSASSIN_SPAM_TO_INBOX]="${SPAMASSASSIN_SPAM_TO_INBOX:=1}"
VARS[SPOOF_PROTECTION]="${SPOOF_PROTECTION:=0}"
VARS[SRS_SENDER_CLASSES]="${SRS_SENDER_CLASSES:=envelope_sender}"
VARS[SSL_TYPE]="${SSL_TYPE:=}"
VARS[SUPERVISOR_LOGLEVEL]="${SUPERVISOR_LOGLEVEL:=warn}"
VARS[TLS_LEVEL]="${TLS_LEVEL:=modern}"
VARS[UPDATE_CHECK_INTERVAL]="${UPDATE_CHECK_INTERVAL:=1d}"
VARS[VIRUSMAILS_DELETE_DELAY]="${VIRUSMAILS_DELETE_DELAY:=7}"
# ------------------------------------------------------------
# ? << Setup of default and global values / variables
# --
# ? >> Registering functions
# ------------------------------------------------------------
2016-12-01 14:45:40 +00:00
function register_functions
{
_log 'info' 'Initializing setup'
_log 'debug' 'Registering functions'
2016-12-01 14:45:40 +00:00
# ? >> Checks
2016-12-01 14:45:40 +00:00
_register_check_function '_check_hostname'
_register_check_function '_check_log_level'
2016-12-01 14:45:40 +00:00
# ? >> Setup
2016-12-01 14:45:40 +00:00
_register_setup_function '_setup_supervisor'
_register_setup_function '_setup_default_vars'
_register_setup_function '_setup_file_permissions'
if [[ ${SMTP_ONLY} -ne 1 ]]
then
_register_setup_function '_setup_dovecot'
_register_setup_function '_setup_dovecot_dhparam'
_register_setup_function '_setup_dovecot_quota'
_register_setup_function '_setup_dovecot_local_user'
fi
[[ ${ENABLE_LDAP} -eq 1 ]] && _register_setup_function '_setup_ldap'
[[ ${ENABLE_POSTGREY} -eq 1 ]] && _register_setup_function '_setup_postgrey'
[[ ${ENABLE_SASLAUTHD} -eq 1 ]] && _register_setup_function '_setup_saslauthd'
[[ ${POSTFIX_INET_PROTOCOLS} != 'all' ]] && _register_setup_function '_setup_postfix_inet_protocols'
[[ ${DOVECOT_INET_PROTOCOLS} != 'all' ]] && _register_setup_function '_setup_dovecot_inet_protocols'
[[ ${ENABLE_FAIL2BAN} -eq 1 ]] && _register_setup_function '_setup_fail2ban'
[[ ${ENABLE_DNSBL} -eq 0 ]] && _register_setup_function '_setup_dnsbl_disable'
[[ ${CLAMAV_MESSAGE_SIZE_LIMIT} != '25M' ]] && _register_setup_function '_setup_clamav_sizelimit'
_register_setup_function '_setup_dkim'
_register_setup_function '_setup_ssl'
_register_setup_function '_setup_docker_permit'
_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'
_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'
# needs to come after _setup_postfix_aliases
[[ ${SPOOF_PROTECTION} -eq 1 ]] && _register_setup_function '_setup_spoof_protection'
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
_register_setup_function '_setup_postfix_access_control'
fix: `check-for-changes.sh` should not fall out of sync with shared logic (#2260) Removes duplicate logic from `check-for-changes.sh` that is used/maintained elsewhere to avoid risk of problems, as this code is already starting to diverge / rot. --- Previously the change detection support has had code added for rebuilding config upon change detection which is the same as code run during startup scripts. Unfortunately over time this has fallen out of sync. Mostly the startup scripts would get maintenance and the contributor and reviewers may not have been aware of the duplicate code handled by `check-for-changes.sh`. That code was starting to diverge in addition to some changes in structure (_eg: relay host logic seems interleaved here vs separated out in startup scripts_). I wanted to address this before it risks becoming a much bigger headache. Rather than bloat `helper-functions.sh` further, I've added a `helpers/` folder extracting relevant common logic between startup scripts and `changedetector`. If you want to follow that process I've kept scoped commits to make those diffs easier. Some minor changes/improvements were added but nothing significant. --- - chore: Extract relay host logic to new `relay.sh` helper - chore: Extract `/etc/postfix/sasl_passwd` logic to new `sasl.sh` helper - chore: Extract `postfix-accounts.cf` logic to new `accounts.sh` helper - chore: Extract `/etc/aliases` logic to new `aliases.sh` helper - chore: Extract `/etc/postfix/vhost` logic to new `postfix.sh` helper - chore: Add inline docs for Postfix configs > These are possibly more verbose than needed and can be reduced at a later stage. > They are helpful during this refactor process while investigating that everything is handled correctly. `accounts.sh`: - Add note regarding potential bug for bare domain setups with `/etc/postfix/vhost` and `mydestination` sharing same domain value. `relay.sh`: - Remove the tabs for a single space delimiter, revised associated comment. - Add PR reference for original `_populate_relayhost_map` implementation which has some useful details. Co-authored-by: Georg Lauterbach <44545919+georglauterbach@users.noreply.github.com> Co-authored-by: Casper <casperklein@users.noreply.github.com>
2021-11-20 20:33:49 +00:00
_register_setup_function '_setup_postfix_relay_hosts'
2016-12-01 14:45:40 +00:00
[[ ${ENABLE_POSTFIX_VIRTUAL_TRANSPORT:-0} -eq 1 ]] && _register_setup_function '_setup_postfix_virtual_transport'
2016-12-01 14:45:40 +00:00
_register_setup_function '_setup_postfix_override_configuration'
_register_setup_function '_setup_logrotate'
_register_setup_function '_setup_mail_summary'
_register_setup_function '_setup_logwatch'
_register_setup_function '_setup_user_patches'
2016-12-01 14:45:40 +00:00
# needs to come last as configuration files are modified in-place
_register_setup_function '_setup_chksum_file'
2016-12-01 14:45:40 +00:00
# ? >> Fixes
_register_fix_function '_fix_var_mail_permissions'
[[ ${ENABLE_AMAVIS} -eq 1 ]] && _register_fix_function '_fix_var_amavis_permissions'
2016-12-01 14:45:40 +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
# ? >> Miscellaneous
2016-12-01 14:45:40 +00:00
_register_misc_function '_misc_save_states'
# ? >> Daemons
2016-12-01 14:45:40 +00:00
_register_start_daemon '_start_daemons_cron'
_register_start_daemon '_start_daemons_rsyslog'
2016-12-01 14:45:40 +00:00
[[ ${SMTP_ONLY} -ne 1 ]] && _register_start_daemon '_start_daemons_dovecot'
[[ ${ENABLE_UPDATE_CHECK} -eq 1 ]] && _register_start_daemon '_start_daemons_update_check'
2016-12-01 14:45:40 +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
# needs to be started before postfix
[[ ${ENABLE_POSTGREY} -eq 1 ]] && _register_start_daemon '_start_daemons_postgrey'
_register_start_daemon '_start_daemons_postfix'
# needs to be started after postfix
[[ ${ENABLE_SASLAUTHD} -eq 1 ]] && _register_start_daemon '_start_daemons_saslauthd'
[[ ${ENABLE_FAIL2BAN} -eq 1 ]] && _register_start_daemon '_start_daemons_fail2ban'
[[ ${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'
[[ ${ENABLE_AMAVIS} -eq 1 ]] && _register_start_daemon '_start_daemons_amavis'
2016-12-01 14:45:40 +00:00
}
function _register_start_daemon
{
DAEMONS_START+=("${1}")
_log 'trace' "${1}() registered"
2016-12-01 14:45:40 +00:00
}
function _register_setup_function
{
FUNCS_SETUP+=("${1}")
_log 'trace' "${1}() registered"
2016-12-01 14:45:40 +00:00
}
function _register_fix_function
{
FUNCS_FIX+=("${1}")
_log 'trace' "${1}() registered"
2016-12-01 14:45:40 +00:00
}
function _register_check_function
{
FUNCS_CHECK+=("${1}")
_log 'trace' "${1}() registered"
2016-12-01 14:45:40 +00:00
}
function _register_misc_function
{
FUNCS_MISC+=("${1}")
_log 'trace' "${1}() registered"
2016-12-01 14:45:40 +00:00
}
# ------------------------------------------------------------
# ? << Registering functions
# --
# ? >> Sourcing all stacks
# 1. Checks
# 2. Setup
# 3. Fixes
# 4. Miscellaneous
# 5. Daemons
# ------------------------------------------------------------
# shellcheck source=./startup/check-stack.sh
source /usr/local/bin/check-stack.sh
# shellcheck source=./startup/setup-stack.sh
source /usr/local/bin/setup-stack.sh
2016-12-01 14:45:40 +00:00
# shellcheck source=./startup/fixes-stack.sh
source /usr/local/bin/fixes-stack.sh
2016-12-01 14:45:40 +00:00
# shellcheck source=./startup/misc-stack.sh
source /usr/local/bin/misc-stack.sh
# shellcheck source=./startup/daemons-stack.sh
source /usr/local/bin/daemons-stack.sh
# ------------------------------------------------------------
# ? << Sourcing all stacks
# --
# ? >> Executing all stacks
# ------------------------------------------------------------
_log 'info' "Welcome to docker-mailserver $(</VERSION)"
register_functions
check
setup
[[ ${LOG_LEVEL} =~ (debug|trace) ]] && print-environment
fix
start_misc
start_daemons
2022-01-07 23:25:09 +00:00
# marker to check, if container was restarted
date >/CONTAINER_START
2022-01-07 23:25:09 +00:00
_log 'info' "${HOSTNAME} is up and running"
touch /var/log/mail/mail.log
2021-05-17 12:54:43 +00:00
tail -Fn 0 /var/log/mail/mail.log
2016-12-01 14:45:40 +00:00
exit 0