Files

63 lines
2.1 KiB
Bash

# emailwiz - platform and package detection
# SPDX-License-Identifier: GPL-3.0-or-later
# shellcheck shell=sh
MAIL_PACKAGES="postfix postfix-pcre dovecot-core dovecot-imapd dovecot-pop3d dovecot-sieve
opendkim opendkim-tools spamd spamc fail2ban bind9-host openssl ca-certificates dns-root-data"
pkg_version() {
dpkg-query -W -f='${db:Status-Abbrev} ${Version}\n' "$1" 2>/dev/null |
awk '$1 ~ /^ii/ {print $2}'
}
pkg_installed() { [ -n "$(pkg_version "$1")" ]; }
# Fails with EX_UNSUPPORTED on anything but Debian 13. The Dovecot template
# uses 2.4 syntax, which Debian 12 (Dovecot 2.3) cannot parse.
detect_os() {
[ -r /etc/os-release ] || die "/etc/os-release missing" "$EX_UNSUPPORTED"
OS_ID=$(. /etc/os-release && echo "${ID:-}")
OS_VERSION=$(. /etc/os-release && echo "${VERSION_ID:-}")
OS_CODENAME=$(. /etc/os-release && echo "${VERSION_CODENAME:-}")
if [ "$OS_ID" != debian ] || [ "$OS_VERSION" != 13 ]; then
[ "${EMAILWIZ_ALLOW_UNSUPPORTED:-no}" = yes ] ||
die "unsupported OS: $OS_ID $OS_VERSION ($OS_CODENAME); only Debian 13 is supported" "$EX_UNSUPPORTED"
warn "running on unsupported OS $OS_ID $OS_VERSION (EMAILWIZ_ALLOW_UNSUPPORTED=yes)"
fi
}
detect_versions() {
V_DOVECOT=$(pkg_version dovecot-core)
case $V_DOVECOT in
'' ) ;;
1:2.4.* | 2.4.*) ;;
*) die "Dovecot $V_DOVECOT found; the template requires Dovecot 2.4" "$EX_UNSUPPORTED" ;;
esac
DOVECOT_CONFIG_VERSION=$(dovecot --version 2>/dev/null | awk '{print $1}' | sed 's/-.*//')
}
missing_packages() {
_m=
for _p in $MAIL_PACKAGES; do
[ "$SPAM_FILTER" = none ] && case $_p in spamd | spamc) continue ;; esac
[ "$FAIL2BAN" = no ] && [ "$_p" = fail2ban ] && continue
pkg_installed "$_p" || _m="$_m $_p"
done
[ "$TLS_MODE" = certbot ] && ! pkg_installed certbot && _m="$_m certbot"
printf '%s\n' "${_m# }"
}
# 0 if the host has a global-scope IPv6 address.
has_global_ipv6() {
[ -n "$(ip -6 -o addr show scope global 2>/dev/null)" ]
}
primary_ipv4() {
ip -4 -o route get 1.1.1.1 2>/dev/null | sed -n 's/.* src \([0-9.]*\).*/\1/p'
}
primary_ipv6() {
has_global_ipv6 || return 0
ip -6 -o route get 2001:4860:4860::8888 2>/dev/null | sed -n 's/.* src \([0-9a-f:]*\).*/\1/p'
}