167 lines
5.7 KiB
Bash
167 lines
5.7 KiB
Bash
# emailwiz - `setup`: guided first install (the emailwiz.sh experience, done safely)
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
# shellcheck shell=sh
|
|
#
|
|
# Every step reuses a regular command and is safe to rerun: an interrupted
|
|
# setup can simply be started again. Nothing is purged or overwritten silently.
|
|
|
|
setup_usage() {
|
|
cat <<'EOF'
|
|
usage: emailwiz setup [--non-interactive]
|
|
|
|
Interactive by default. --non-interactive takes everything from the existing
|
|
config file and these environment variables, and never prompts:
|
|
DOMAIN MAIL_HOSTNAME ROOT_ALIAS TLS_MODE TLS_CERT TLS_KEY CERTBOT_EMAIL
|
|
SETUP_UFW=yes|no add UFW allow rules for the mail ports
|
|
SETUP_USER=name create this mail account (set its password later with passwd)
|
|
SETUP_OVERWRITE_CONFIG=yes replace an existing, different /etc/emailwiz/emailwiz.conf
|
|
EOF
|
|
}
|
|
|
|
setup_step() { printf '\n%s[%s/8]%s %s\n' "$C_BLU" "$1" "$C_OFF" "$2" >&2; }
|
|
|
|
# Warn (never fail) when DNS does not point at this host yet.
|
|
setup_dns_hint() {
|
|
_ip=$(primary_ipv4)
|
|
_a=$(dns_q A "$MAIL_HOSTNAME" | awk '/has address/ {print $NF}')
|
|
if printf '%s\n' "$_a" | grep -qx "$_ip"; then
|
|
ok "$MAIL_HOSTNAME resolves to this host ($_ip)"
|
|
else
|
|
warn "$MAIL_HOSTNAME resolves to '${_a:-nothing}', this host is $_ip"
|
|
warn "add: $MAIL_HOSTNAME. A $_ip (certbot needs it; mail delivery needs it plus MX)"
|
|
fi
|
|
}
|
|
|
|
setup_config() {
|
|
if [ -f "$EMAILWIZ_CONF" ]; then
|
|
# Values given in the environment override the file.
|
|
_envset=
|
|
for _v in DOMAIN MAIL_HOSTNAME ROOT_ALIAS TLS_MODE TLS_CERT TLS_KEY; do
|
|
eval "_val=\${$_v:-}"
|
|
[ -n "$_val" ] && _envset="$_envset $_v=$(squote "$_val")"
|
|
done
|
|
config_load
|
|
[ -z "$_envset" ] || eval "$_envset"
|
|
info "existing configuration: $EMAILWIZ_CONF"
|
|
else
|
|
config_detect "${DOMAIN:-}"
|
|
config_defaults
|
|
fi
|
|
ask DOMAIN "Mail domain (addresses will be user@DOMAIN)" "$DOMAIN"
|
|
is_hostname "$DOMAIN" || die "'$DOMAIN' is not a valid domain" "$EX_USAGE"
|
|
[ "$MAIL_HOSTNAME" = "mail." ] && MAIL_HOSTNAME=
|
|
ask MAIL_HOSTNAME "Mail server host name (MX)" "${MAIL_HOSTNAME:-mail.$DOMAIN}"
|
|
ask ROOT_ALIAS "Local user who receives postmaster/root mail" "${ROOT_ALIAS:-${SUDO_USER:-}}"
|
|
[ -z "$ROOT_ALIAS" ] || getent passwd "$ROOT_ALIAS" >/dev/null ||
|
|
die "user '$ROOT_ALIAS' does not exist; create it first or choose another" "$EX_USAGE"
|
|
|
|
if [ "$NONINTERACTIVE" != yes ]; then
|
|
# Default: keep a usable existing certificate, otherwise obtain one.
|
|
_d=certbot
|
|
[ "$TLS_MODE" = external ] && [ -r "${TLS_CERT:-/nonexistent}" ] && _d=external
|
|
TLS_MODE=
|
|
ask TLS_MODE "TLS certificate: 'certbot' (obtain for $MAIL_HOSTNAME) or 'external' (existing files)" "$_d"
|
|
fi
|
|
case $TLS_MODE in
|
|
certbot)
|
|
TLS_CERT="$(tls_live_dir)/fullchain.pem"
|
|
TLS_KEY="$(tls_live_dir)/privkey.pem"
|
|
;;
|
|
external)
|
|
ask TLS_CERT "Certificate (full chain) file" "$TLS_CERT"
|
|
ask TLS_KEY "Private key file" "$TLS_KEY"
|
|
[ -r "$TLS_CERT" ] && [ -r "$TLS_KEY" ] || die "certificate or key not readable" "$EX_USAGE"
|
|
;;
|
|
*) die "TLS_MODE must be certbot or external" "$EX_USAGE" ;;
|
|
esac
|
|
|
|
_t=$(mktemp)
|
|
config_render >"$_t"
|
|
if [ -f "$EMAILWIZ_CONF" ] && ! diff -q -I '^# Generated' "$EMAILWIZ_CONF" "$_t" >/dev/null; then
|
|
diff -u --label current --label new "$EMAILWIZ_CONF" "$_t" | grep '^[-+][A-Z]' >&2 || :
|
|
if ! confirm "Update $EMAILWIZ_CONF with these values?" SETUP_OVERWRITE_CONFIG; then
|
|
rm -f "$_t"
|
|
die "configuration not changed; edit $EMAILWIZ_CONF or rerun setup" "$EX_USAGE"
|
|
fi
|
|
cp -p "$EMAILWIZ_CONF" "$EMAILWIZ_CONF.bak"
|
|
info "previous config saved as $EMAILWIZ_CONF.bak"
|
|
fi
|
|
rm -f "$_t"
|
|
config_write
|
|
config_load
|
|
config_validate
|
|
ok "configuration: $EMAILWIZ_CONF"
|
|
}
|
|
|
|
cmd_setup() {
|
|
case ${1:-} in
|
|
--non-interactive) NONINTERACTIVE=yes ;;
|
|
'') ;;
|
|
-h | --help) setup_usage; return 0 ;;
|
|
*) setup_usage >&2; exit "$EX_USAGE" ;;
|
|
esac
|
|
need_root setup
|
|
MODE=apply
|
|
detect_os
|
|
|
|
setup_step 1 "Configuration"
|
|
setup_config
|
|
|
|
setup_step 2 "Packages (installs what is missing, never removes anything)"
|
|
( cmd_install_packages )
|
|
|
|
setup_step 3 "DNS pre-check (read-only)"
|
|
setup_dns_hint
|
|
|
|
setup_step 4 "Firewall"
|
|
( cmd_firewall )
|
|
|
|
setup_step 5 "TLS certificate"
|
|
if [ "$TLS_MODE" = certbot ]; then
|
|
tls_obtain
|
|
else
|
|
ok "using existing certificate $TLS_CERT"
|
|
fi
|
|
|
|
setup_step 6 "Mail configuration (plan, then apply with backup and validation)"
|
|
( cmd_plan --no-diff )
|
|
if [ "$NONINTERACTIVE" != yes ] && ! confirm "Apply this configuration now?"; then
|
|
die "stopped before apply; rerun 'sudo emailwiz setup' or 'sudo emailwiz apply' later" "$EX_ERR"
|
|
fi
|
|
( cmd_apply --yes )
|
|
|
|
setup_step 7 "First mail account"
|
|
_u=${SETUP_USER:-}
|
|
if [ "$NONINTERACTIVE" != yes ] && confirm "Create a mail account now?"; then
|
|
ask _u "Account name (letters, digits, - and _)" ""
|
|
fi
|
|
if [ -n "$_u" ]; then
|
|
( cmd_user add "$_u" )
|
|
if [ "$NONINTERACTIVE" != yes ]; then
|
|
info "set the password for $_u (used for IMAP and SMTP login):"
|
|
passwd "$_u"
|
|
else
|
|
info "set its password with: sudo passwd $_u"
|
|
fi
|
|
else
|
|
info "skipped; later: sudo emailwiz user add NAME && sudo passwd NAME"
|
|
fi
|
|
|
|
setup_step 8 "Validation"
|
|
_vrc=0
|
|
( cmd_validate --no-dns --deliver-test ) || _vrc=$?
|
|
( umask 077; cmd_dns >/root/emailwiz-dns.txt ) && info "DNS records saved to /root/emailwiz-dns.txt"
|
|
( validate_dns ) || :
|
|
|
|
printf '\n' >&2
|
|
if [ "$_vrc" -eq 0 ]; then
|
|
ok "setup complete. Remaining manual steps (outside this server):"
|
|
else
|
|
warn "setup finished with validation failures (see above). Also required:"
|
|
fi
|
|
info "1. publish the DNS records in /root/emailwiz-dns.txt at your DNS provider"
|
|
info "2. ask your hosting provider to set reverse DNS (PTR) of $(primary_ipv4) to $MAIL_HOSTNAME"
|
|
info "3. then: sudo emailwiz validate && sudo emailwiz validate --external-test you@example.com"
|
|
return "$_vrc"
|
|
}
|