diff --git a/bootstrap/email/README.md b/bootstrap/email/README.md index 00c61fd..cf7102d 100644 --- a/bootstrap/email/README.md +++ b/bootstrap/email/README.md @@ -1,9 +1,10 @@ # OCI email learning VM This is an independent, opt-in OpenTofu stack. It creates one OCI compute VM -inside an existing compartment and subnet; it does not install a mail server, -change DNS, or open SMTP ports. Keep `enabled=false` until the OCI image, -network, and SSH inputs are known. +inside an existing compartment and subnet. The separate `email-vm setup` +command installs an idempotent Postfix + Dovecot baseline over SSH; it does +not change DNS or open mail ingress unless explicitly enabled. Keep +`enabled=false` until the OCI image, network, and SSH inputs are known. The default `VM.Standard.E2.1.Micro` shape and Canonical Ubuntu 24.04 Minimal image are intended for OCI Always Free capacity in `us-ashburn-1`. @@ -23,8 +24,17 @@ export LAB_EMAIL_DOMAIN=example.org export LAB_EMAIL_NSG_IDS='["ocid1.networksecuritygroup..."]' ./jeannie email-vm plan ./jeannie email-vm apply +./jeannie email-vm setup ``` +`email-vm setup` defaults to the Ubuntu `ubuntu` account and the local +`~/.ssh/id_ed25519` key. Set `LAB_EMAIL_SSH_USER` or +`LAB_EMAIL_SSH_PRIVATE_KEY` when the image uses different SSH credentials. +Without a domain it uses a temporary self-signed certificate and keeps ports +25, 465, 587, and 993 closed. After choosing the domain, rerun setup with +`LAB_EMAIL_DOMAIN=mail.example.org`; then add DNS A/MX, SPF, DKIM, DMARC, and +PTR records and request OCI SMTP port 25 access before opening mail ingress. + The provider reads OCI credentials from the normal OCI CLI configuration (`~/.oci/config`) and never stores private keys in this repository. `destroy` requires `--yes`. The stack is intentionally separate from `platform`, `apps`, diff --git a/jeannie b/jeannie index a15dd67..5b4e9a3 100755 --- a/jeannie +++ b/jeannie @@ -122,7 +122,8 @@ Observability And GitOps platform [all|calico|metrics|dns|metallb|ingress|storage|argocd|security|logging|monitoring|tools] Deploy one platform component or the full platform stack. edge Deploy/check the OCI edge stack only. - email-vm {status|plan|apply|destroy --yes} Manage the independent OCI email learning VM. + email-vm {status|plan|apply|setup|status-server|destroy --yes} + Manage the OCI email VM and its mail-server baseline. oci-foundation {status|plan|apply|destroy --yes} Create the opt-in OCI compartment, network, edge VM, and email VM. oci-auth {install|init [--force]|check} Install/check OCI CLI and API-key authentication on Debian. diff --git a/lib/jeannie/commands.sh b/lib/jeannie/commands.sh index 9c669f9..2047947 100644 --- a/lib/jeannie/commands.sh +++ b/lib/jeannie/commands.sh @@ -3288,6 +3288,15 @@ pimox_oob() { email_vm_manage() { require_debian_server "email-vm" + if [[ "${1:-}" == setup || "${1:-}" == status-server ]]; then + local email_action="${1}" + shift || true + case "${email_action}" in + setup) LAB_EMAIL_DOMAIN="${LAB_EMAIL_DOMAIN:-}" LAB_EMAIL_OPEN_MAIL_PORTS="${LAB_EMAIL_OPEN_MAIL_PORTS:-false}" "${REPO_ROOT}/scripts/email-server" install ;; + status-server) "${REPO_ROOT}/scripts/email-server" status ;; + esac + return + fi export_email_vm_tf_vars local action="${1:-status}" shift || true @@ -3321,7 +3330,7 @@ email_vm_manage() { tofu -chdir="${stack}" destroy "$@" -auto-approve ;; *) - echo "Usage: ./jeannie email-vm {status|plan|apply|destroy --yes}" >&2 + echo "Usage: ./jeannie email-vm {status|plan|apply|setup|status-server|destroy --yes}" >&2 return 2 ;; esac diff --git a/scripts/email-server b/scripts/email-server new file mode 100755 index 0000000..8e6d6b2 --- /dev/null +++ b/scripts/email-server @@ -0,0 +1,120 @@ +#!/usr/bin/env bash +set -euo pipefail + +usage() { + cat <<'EOF' +Usage: scripts/email-server {install|status} + +Install an idempotent Postfix + Dovecot baseline on the OCI email VM. +Set LAB_EMAIL_DOMAIN when the mail domain is available. Mail ingress remains +closed unless LAB_EMAIL_OPEN_MAIL_PORTS=true is explicitly set. +EOF +} + +action="${1:-install}" +if [[ "${action}" != install && "${action}" != status ]]; then + usage >&2 + exit 2 +fi + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +stack="${repo_root}/bootstrap/oci" +email_ip="${LAB_EMAIL_PUBLIC_IP:-}" +if [[ -z "${email_ip}" ]]; then + email_ip="$(tofu -chdir="${stack}" output -raw email_public_ip 2>/dev/null || true)" +fi +[[ -n "${email_ip}" && "${email_ip}" != null ]] || { + echo 'Email public IP is unavailable. Apply the OCI foundation first.' >&2 + exit 1 +} + +ssh_user="${LAB_EMAIL_SSH_USER:-ubuntu}" +ssh_key="${LAB_EMAIL_SSH_PRIVATE_KEY:-${HOME}/.ssh/id_ed25519}" +ssh_target="${ssh_user}@${email_ip}" +ssh_opts=(-i "${ssh_key}" -o ConnectTimeout=15 -o StrictHostKeyChecking=accept-new) + +if [[ "${action}" == status ]]; then + ssh "${ssh_opts[@]}" "${ssh_target}" 'sudo systemctl is-active postfix dovecot fail2ban; sudo postfix check; sudo ss -ltnp | grep -E ":(25|465|587|993)\b" || true' + exit $? +fi + +domain="${LAB_EMAIL_DOMAIN:-}" +open_mail="${LAB_EMAIL_OPEN_MAIL_PORTS:-false}" +echo "Preparing Postfix and Dovecot on ${ssh_target} (domain=${domain:-pending}, mail_ports=${open_mail})." +ssh "${ssh_opts[@]}" "${ssh_target}" bash -s -- "${domain}" "${open_mail}" <<'REMOTE' +set -euo pipefail +domain="${1:-}" +open_mail="${2:-false}" +hostname_value="${domain:+mail.${domain}}" +hostname_value="${hostname_value:-homelab-mail}" + +export DEBIAN_FRONTEND=noninteractive +echo 'postfix postfix/mailname string' | debconf-set-selections +echo 'postfix postfix/main_mailer_type select Internet Site' | debconf-set-selections +apt-get update +apt-get install -y postfix dovecot-imapd dovecot-lmtpd fail2ban ufw openssl + +hostnamectl set-hostname "${hostname_value}" 2>/dev/null || true +printf '%s\n' "${hostname_value}" >/etc/mailname + +install -d -m 0750 /etc/ssl/private /etc/ssl/certs +if [[ ! -s /etc/ssl/private/homelab-mail.key || ! -s /etc/ssl/certs/homelab-mail.crt ]]; then + openssl req -x509 -nodes -newkey rsa:2048 -days 825 \ + -keyout /etc/ssl/private/homelab-mail.key \ + -out /etc/ssl/certs/homelab-mail.crt \ + -subj "/CN=${hostname_value}" >/dev/null 2>&1 + chmod 0600 /etc/ssl/private/homelab-mail.key +fi + +postconf -e "myhostname = ${hostname_value}" +postconf -e "mydomain = ${domain:-localdomain}" +postconf -e 'myorigin = $mydomain' +postconf -e 'inet_interfaces = all' +postconf -e 'inet_protocols = ipv4' +postconf -e 'home_mailbox = Maildir/' +postconf -e 'disable_vrfy_command = yes' +postconf -e 'smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination' +postconf -e 'smtpd_tls_security_level = may' +postconf -e 'smtpd_tls_auth_only = yes' +postconf -e 'smtpd_tls_cert_file = /etc/ssl/certs/homelab-mail.crt' +postconf -e 'smtpd_tls_key_file = /etc/ssl/private/homelab-mail.key' +postconf -e 'smtpd_sasl_auth_enable = yes' +postconf -e 'smtpd_sasl_type = dovecot' +postconf -e 'smtpd_sasl_path = private/auth' +postconf -e 'smtpd_sasl_security_options = noanonymous' + +cat >/etc/dovecot/conf.d/99-homelab-mail.conf </dev/null +ufw default deny incoming +ufw default allow outgoing +ufw allow 22/tcp +if [[ "${open_mail}" == true ]]; then + ufw allow 25/tcp + ufw allow 465/tcp + ufw allow 587/tcp + ufw allow 993/tcp +fi +ufw --force enable >/dev/null + +systemctl enable --now postfix dovecot fail2ban +systemctl restart postfix dovecot +postfix check +systemctl is-active postfix dovecot fail2ban +echo "Email baseline ready. Domain-dependent DNS, PTR, DKIM, DMARC, and trusted TLS remain to be configured." +REMOTE