2022-10-17 08:40:09 +00:00
|
|
|
#!/bin/bash
|
2022-09-21 07:31:28 +00:00
|
|
|
|
|
|
|
# -eE :: exit on error (do this in functions as well)
|
|
|
|
# -u :: show (and exit) when using unset variables
|
|
|
|
# -o pipefail :: exit on error in pipes
|
|
|
|
set -eE -u -o pipefail
|
|
|
|
|
|
|
|
# shellcheck source=../helpers/log.sh
|
|
|
|
source /usr/local/bin/helpers/log.sh
|
|
|
|
|
|
|
|
_log_level_is 'trace' && QUIET='-y' || QUIET='-qq'
|
|
|
|
|
2023-05-25 23:01:41 +00:00
|
|
|
function _pre_installation_steps() {
|
2022-09-21 07:31:28 +00:00
|
|
|
_log 'info' 'Starting package installation'
|
|
|
|
_log 'debug' 'Running pre-installation steps'
|
|
|
|
|
|
|
|
_log 'trace' 'Updating package signatures'
|
|
|
|
apt-get "${QUIET}" update
|
|
|
|
|
|
|
|
_log 'trace' 'Installing packages that are needed early'
|
|
|
|
apt-get "${QUIET}" install --no-install-recommends apt-utils 2>/dev/null
|
|
|
|
|
|
|
|
_log 'trace' 'Upgrading packages'
|
|
|
|
apt-get "${QUIET}" upgrade
|
|
|
|
}
|
|
|
|
|
2023-05-25 23:01:41 +00:00
|
|
|
function _install_postfix() {
|
2022-09-21 07:31:28 +00:00
|
|
|
_log 'debug' 'Installing Postfix'
|
|
|
|
|
|
|
|
_log 'warn' 'Applying workaround for Postfix bug (see https://github.com//issues/2023#issuecomment-855326403)'
|
|
|
|
|
|
|
|
# Debians postfix package has a post-install script that expects a valid FQDN hostname to work:
|
|
|
|
mv /bin/hostname /bin/hostname.bak
|
|
|
|
echo "echo 'docker-mailserver.invalid'" >/bin/hostname
|
|
|
|
chmod +x /bin/hostname
|
|
|
|
apt-get "${QUIET}" install --no-install-recommends postfix
|
|
|
|
mv /bin/hostname.bak /bin/hostname
|
2023-03-03 22:08:51 +00:00
|
|
|
|
|
|
|
# Irrelevant - Debian's default `chroot` jail config for Postfix needed a separate syslog socket:
|
|
|
|
rm /etc/rsyslog.d/postfix.conf
|
2022-09-21 07:31:28 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 23:01:41 +00:00
|
|
|
function _install_packages() {
|
2022-09-21 07:31:28 +00:00
|
|
|
_log 'debug' 'Installing all packages now'
|
|
|
|
|
|
|
|
ANTI_VIRUS_SPAM_PACKAGES=(
|
|
|
|
amavisd-new clamav clamav-daemon
|
2022-09-29 07:02:56 +00:00
|
|
|
pyzor razor spamassassin
|
2022-09-21 07:31:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
CODECS_PACKAGES=(
|
|
|
|
altermime arj bzip2
|
|
|
|
cabextract cpio file
|
|
|
|
gzip lhasa liblz4-tool
|
|
|
|
lrzip lzop nomarch
|
|
|
|
p7zip-full pax rpm2cpio
|
|
|
|
unrar-free unzip xz-utils
|
|
|
|
)
|
|
|
|
|
|
|
|
MISCELLANEOUS_PACKAGES=(
|
2023-10-16 07:51:48 +00:00
|
|
|
apt-transport-https binutils bsd-mailx
|
2022-09-21 07:31:28 +00:00
|
|
|
ca-certificates curl dbconfig-no-thanks
|
2023-10-16 07:51:48 +00:00
|
|
|
dumb-init gnupg iproute2 libdate-manip-perl
|
|
|
|
libldap-common libmail-spf-perl
|
|
|
|
libnet-dns-perl locales logwatch
|
|
|
|
netcat-openbsd nftables rsyslog
|
|
|
|
supervisor uuid whois
|
2022-09-21 07:31:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
POSTFIX_PACKAGES=(
|
|
|
|
pflogsumm postgrey postfix-ldap
|
|
|
|
postfix-pcre postfix-policyd-spf-python postsrsd
|
|
|
|
)
|
|
|
|
|
|
|
|
MAIL_PROGRAMS_PACKAGES=(
|
|
|
|
fetchmail opendkim opendkim-tools
|
|
|
|
opendmarc libsasl2-modules sasl2-bin
|
|
|
|
)
|
|
|
|
|
2023-10-16 07:51:48 +00:00
|
|
|
# `bind9-dnsutils` provides the `dig` command
|
|
|
|
# `iputils-ping` provides the `ping` command
|
|
|
|
DEBUG_PACKAGES=(
|
|
|
|
bind9-dnsutils iputils-ping less nano
|
|
|
|
)
|
|
|
|
|
2022-09-21 07:31:28 +00:00
|
|
|
apt-get "${QUIET}" --no-install-recommends install \
|
|
|
|
"${ANTI_VIRUS_SPAM_PACKAGES[@]}" \
|
|
|
|
"${CODECS_PACKAGES[@]}" \
|
|
|
|
"${MISCELLANEOUS_PACKAGES[@]}" \
|
|
|
|
"${POSTFIX_PACKAGES[@]}" \
|
2023-10-16 07:51:48 +00:00
|
|
|
"${MAIL_PROGRAMS_PACKAGES[@]}" \
|
|
|
|
"${DEBUG_PACKAGES[@]}"
|
2022-09-21 07:31:28 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 23:01:41 +00:00
|
|
|
function _install_dovecot() {
|
2022-09-29 21:26:45 +00:00
|
|
|
declare -a DOVECOT_PACKAGES
|
|
|
|
|
2023-11-08 21:18:17 +00:00
|
|
|
# Dovecot packages for officially supported features.
|
2022-09-29 21:26:45 +00:00
|
|
|
DOVECOT_PACKAGES=(
|
2023-06-01 08:50:31 +00:00
|
|
|
dovecot-core dovecot-imapd
|
2022-09-29 21:26:45 +00:00
|
|
|
dovecot-ldap dovecot-lmtpd dovecot-managesieved
|
|
|
|
dovecot-pop3d dovecot-sieve dovecot-solr
|
|
|
|
)
|
|
|
|
|
2023-11-08 21:18:17 +00:00
|
|
|
# Dovecot packages for community supported features.
|
|
|
|
DOVECOT_PACKAGES+=(dovecot-auth-lua)
|
|
|
|
|
|
|
|
# Dovecot's deb community repository only provides x86_64 packages, so do not include it
|
|
|
|
# when building for another architecture.
|
|
|
|
if [[ ${DOVECOT_COMMUNITY_REPO} -eq 1 ]] && [[ "$(uname --machine)" == "x86_64" ]]; then
|
2022-09-29 21:26:45 +00:00
|
|
|
_log 'trace' 'Using Dovecot community repository'
|
|
|
|
curl https://repo.dovecot.org/DOVECOT-REPO-GPG | gpg --import
|
|
|
|
gpg --export ED409DA1 > /etc/apt/trusted.gpg.d/dovecot.gpg
|
|
|
|
echo "deb https://repo.dovecot.org/ce-2.3-latest/debian/bullseye bullseye main" > /etc/apt/sources.list.d/dovecot.list
|
|
|
|
|
|
|
|
_log 'trace' 'Updating Dovecot package signatures'
|
|
|
|
apt-get "${QUIET}" update
|
2023-11-08 21:18:17 +00:00
|
|
|
|
|
|
|
# Additional community package needed for Lua support if the Dovecot community repository is used.
|
|
|
|
DOVECOT_PACKAGES+=(dovecot-lua)
|
2022-09-29 21:26:45 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
_log 'debug' 'Installing Dovecot'
|
|
|
|
apt-get "${QUIET}" --no-install-recommends install "${DOVECOT_PACKAGES[@]}"
|
2023-06-01 08:50:31 +00:00
|
|
|
|
|
|
|
# dependency for fts_xapian
|
|
|
|
apt-get "${QUIET}" --no-install-recommends install libxapian30
|
2022-09-29 21:26:45 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 23:01:41 +00:00
|
|
|
function _install_rspamd() {
|
2023-01-05 07:39:00 +00:00
|
|
|
_log 'trace' 'Adding Rspamd package signatures'
|
2023-02-19 11:36:43 +00:00
|
|
|
local DEB_FILE='/etc/apt/sources.list.d/rspamd.list'
|
|
|
|
local RSPAMD_PACKAGE_NAME
|
|
|
|
|
|
|
|
# We try getting the most recent version of Rspamd for aarch64 (from an official source, which
|
|
|
|
# is the backports repository). The version for aarch64 is 3.2; the most recent version for amd64
|
|
|
|
# that we get with the official PPA is 3.4.
|
|
|
|
#
|
|
|
|
# Not removing it later is fine as you have to explicitly opt into installing a backports package
|
|
|
|
# which is not something you could be doing by accident.
|
2023-05-24 07:06:59 +00:00
|
|
|
if [[ $(uname --machine) == 'aarch64' ]]; then
|
2023-02-19 11:36:43 +00:00
|
|
|
echo '# Official Rspamd PPA does not support aarch64, so we use the Bullseye backports' >"${DEB_FILE}"
|
|
|
|
echo 'deb [arch=arm64] http://deb.debian.org/debian bullseye-backports main' >>"${DEB_FILE}"
|
|
|
|
RSPAMD_PACKAGE_NAME='rspamd/bullseye-backports'
|
|
|
|
else
|
|
|
|
curl -sSfL https://rspamd.com/apt-stable/gpg.key | gpg --dearmor >/etc/apt/trusted.gpg.d/rspamd.gpg
|
|
|
|
local URL='[arch=amd64 signed-by=/etc/apt/trusted.gpg.d/rspamd.gpg] http://rspamd.com/apt-stable/ bullseye main'
|
|
|
|
echo "deb ${URL}" >"${DEB_FILE}"
|
|
|
|
echo "deb-src ${URL}" >>"${DEB_FILE}"
|
|
|
|
RSPAMD_PACKAGE_NAME='rspamd'
|
|
|
|
fi
|
2023-01-05 07:39:00 +00:00
|
|
|
|
|
|
|
_log 'debug' 'Installing Rspamd'
|
|
|
|
apt-get "${QUIET}" update
|
2023-02-19 11:36:43 +00:00
|
|
|
apt-get "${QUIET}" --no-install-recommends install "${RSPAMD_PACKAGE_NAME}" 'redis-server'
|
2023-01-05 07:39:00 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 23:01:41 +00:00
|
|
|
function _install_fail2ban() {
|
2023-01-04 16:57:08 +00:00
|
|
|
local FAIL2BAN_DEB_URL='https://github.com/fail2ban/fail2ban/releases/download/1.0.2/fail2ban_1.0.2-1.upstream1_all.deb'
|
2022-09-29 07:02:56 +00:00
|
|
|
local FAIL2BAN_DEB_ASC_URL="${FAIL2BAN_DEB_URL}.asc"
|
|
|
|
local FAIL2BAN_GPG_FINGERPRINT='8738 559E 26F6 71DF 9E2C 6D9E 683B F1BE BD0A 882C'
|
|
|
|
local FAIL2BAN_GPG_PUBLIC_KEY_ID='0x683BF1BEBD0A882C'
|
|
|
|
local FAIL2BAN_GPG_PUBLIC_KEY_SERVER='hkps://keyserver.ubuntu.com'
|
|
|
|
|
|
|
|
_log 'debug' 'Installing Fail2ban'
|
2023-01-29 12:35:57 +00:00
|
|
|
apt-get "${QUIET}" --no-install-recommends install python3-pyinotify python3-dnspython
|
2022-09-29 07:02:56 +00:00
|
|
|
|
|
|
|
gpg --keyserver "${FAIL2BAN_GPG_PUBLIC_KEY_SERVER}" --recv-keys "${FAIL2BAN_GPG_PUBLIC_KEY_ID}" 2>&1
|
|
|
|
|
|
|
|
curl -Lkso fail2ban.deb "${FAIL2BAN_DEB_URL}"
|
|
|
|
curl -Lkso fail2ban.deb.asc "${FAIL2BAN_DEB_ASC_URL}"
|
|
|
|
|
2022-10-02 11:00:15 +00:00
|
|
|
FINGERPRINT=$(LANG=C gpg --verify fail2ban.deb.asc fail2ban.deb |& sed -n 's#Primary key fingerprint: \(.*\)#\1#p')
|
2022-09-29 07:02:56 +00:00
|
|
|
|
2023-05-24 07:06:59 +00:00
|
|
|
if [[ -z ${FINGERPRINT} ]]; then
|
2022-09-29 07:02:56 +00:00
|
|
|
echo 'ERROR: Invalid GPG signature!' >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-05-24 07:06:59 +00:00
|
|
|
if [[ ${FINGERPRINT} != "${FAIL2BAN_GPG_FINGERPRINT}" ]]; then
|
2022-09-29 07:02:56 +00:00
|
|
|
echo "ERROR: Wrong GPG fingerprint!" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
dpkg -i fail2ban.deb 2>&1
|
|
|
|
rm fail2ban.deb fail2ban.deb.asc
|
2022-10-15 10:01:59 +00:00
|
|
|
|
|
|
|
_log 'debug' 'Patching Fail2ban to enable network bans'
|
|
|
|
# Enable network bans
|
|
|
|
# https://github.com/docker-mailserver/docker-mailserver/issues/2669
|
|
|
|
sedfile -i -r 's/^_nft_add_set = .+/_nft_add_set = <nftables> add set <table_family> <table> <addr_set> \\{ type <addr_type>\\; flags interval\\; \\}/' /etc/fail2ban/action.d/nftables.conf
|
2022-09-29 07:02:56 +00:00
|
|
|
}
|
|
|
|
|
2023-05-23 15:25:08 +00:00
|
|
|
# Presently the getmail6 package is v6.14, which is too old.
|
|
|
|
# v6.18 contains fixes for Google and Microsoft OAuth support.
|
|
|
|
# using pip to install getmail.
|
|
|
|
# TODO This can be removed when the base image is updated to Debian 12 (Bookworm)
|
2023-05-25 23:01:41 +00:00
|
|
|
function _install_getmail() {
|
2023-05-23 15:25:08 +00:00
|
|
|
_log 'debug' 'Installing getmail6'
|
|
|
|
apt-get "${QUIET}" --no-install-recommends install python3-pip
|
|
|
|
pip3 install --no-cache-dir 'getmail6~=6.18.12'
|
|
|
|
ln -s /usr/local/bin/getmail /usr/bin/getmail
|
|
|
|
ln -s /usr/local/bin/getmail-gmail-xoauth-tokens /usr/bin/getmail-gmail-xoauth-tokens
|
|
|
|
apt-get "${QUIET}" purge python3-pip
|
|
|
|
apt-get "${QUIET}" autoremove
|
|
|
|
}
|
|
|
|
|
2023-05-25 23:01:41 +00:00
|
|
|
function _remove_data_after_package_installations() {
|
2023-03-12 11:10:45 +00:00
|
|
|
_log 'debug' 'Deleting sensitive files (secrets)'
|
|
|
|
rm /etc/postsrsd.secret
|
|
|
|
|
|
|
|
_log 'debug' 'Deleting default logwatch cronjob'
|
|
|
|
rm /etc/cron.daily/00logwatch
|
|
|
|
}
|
|
|
|
|
2023-05-25 23:01:41 +00:00
|
|
|
function _post_installation_steps() {
|
2022-09-21 07:31:28 +00:00
|
|
|
_log 'debug' 'Running post-installation steps (cleanup)'
|
|
|
|
apt-get "${QUIET}" clean
|
|
|
|
rm -rf /var/lib/apt/lists/*
|
|
|
|
|
|
|
|
_log 'info' 'Finished installing packages'
|
|
|
|
}
|
|
|
|
|
|
|
|
_pre_installation_steps
|
|
|
|
_install_postfix
|
|
|
|
_install_packages
|
2022-09-29 21:26:45 +00:00
|
|
|
_install_dovecot
|
2023-01-05 07:39:00 +00:00
|
|
|
_install_rspamd
|
2022-09-29 07:02:56 +00:00
|
|
|
_install_fail2ban
|
2023-05-23 15:25:08 +00:00
|
|
|
_install_getmail
|
2023-03-12 11:10:45 +00:00
|
|
|
_remove_data_after_package_installations
|
2022-09-21 07:31:28 +00:00
|
|
|
_post_installation_steps
|