| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # One-shot setup for ClonAgent's optional mail server. |
| 4 | # |
| 5 | # Prepares: |
| 6 | # 1. DNS records (A, MX) on the subdomain |
| 7 | # 2. Issues a Let's Encrypt cert for the mail hostname (so STARTTLS works) |
| 8 | # 3. Brings up docker-mailserver |
| 9 | # 4. Generates DKIM keys |
| 10 | # 5. Prints the DKIM/SPF/DMARC TXT records you must add to the DNS zone |
| 11 | # |
| 12 | # Run on the host (as a user with sudo + docker access): |
| 13 | # MAIL_DOMAIN=bot.utopiaia.com ./scripts/setup-mailserver.sh |
| 14 | # |
| 15 | set -euo pipefail |
| 16 | |
| 17 | MAIL_DOMAIN="${MAIL_DOMAIN:-bot.utopiaia.com}" |
| 18 | MAIL_HOSTNAME="${MAIL_HOSTNAME:-$MAIL_DOMAIN}" |
| 19 | PARENT_ZONE="${PARENT_ZONE:-utopiaia.com}" |
| 20 | # CoreDNS reads from project-manager (legacy path); fall back to agent-manager. |
| 21 | ZONE_FILE="${ZONE_FILE:-/home/ubuntu/project-manager/dns/zones/${PARENT_ZONE}.zone}" |
| 22 | [[ -f "$ZONE_FILE" ]] || ZONE_FILE="/home/ubuntu/agent-manager/dns/zones/${PARENT_ZONE}.zone" |
| 23 | SERVER_IP="${SERVER_IP:-145.239.65.26}" |
| 24 | LE_EMAIL="${LE_EMAIL:-kikocisneros@gmail.com}" |
| 25 | COMPOSE="${COMPOSE:-docker compose -f docker-compose.mail.yml}" |
| 26 | |
| 27 | PROJECT_ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 28 | cd "$PROJECT_ROOT" |
| 29 | |
| 30 | # Subdomain part — bot.utopiaia.com → "bot" |
| 31 | SUB="${MAIL_DOMAIN%.${PARENT_ZONE}}" |
| 32 | |
| 33 | say() { printf "\n\033[1;36m→ %s\033[0m\n" "$*"; } |
| 34 | warn() { printf "\033[1;33m! %s\033[0m\n" "$*" >&2; } |
| 35 | |
| 36 | # ── 1. DNS records ──────────────────────────────────────────────────────── |
| 37 | say "1. DNS records in $ZONE_FILE" |
| 38 | if [[ ! -f "$ZONE_FILE" ]]; then |
| 39 | warn "Zone file not found at $ZONE_FILE — skipping (edit DNS by hand)." |
| 40 | else |
| 41 | if grep -qE "^${SUB}\\s+IN\\s+A\\s+${SERVER_IP}" "$ZONE_FILE"; then |
| 42 | say " A record already present — skipping." |
| 43 | else |
| 44 | sudo cp "$ZONE_FILE" "${ZONE_FILE}.bak.$(date +%Y%m%d%H%M%S)" |
| 45 | sudo bash -c "cat >> '$ZONE_FILE' <<EOF |
| 46 | |
| 47 | ; ─── ClonAgent mail server (added by setup-mailserver.sh) ─── |
| 48 | ${SUB} IN A ${SERVER_IP} |
| 49 | ${SUB} IN MX 10 ${MAIL_HOSTNAME}. |
| 50 | EOF" |
| 51 | # bump zone serial (YYYYMMDDNN format → +1) |
| 52 | sudo sed -i -E "s/([0-9]{10}) ; serial.*/\$(date +%Y%m%d)01 ; serial (bumped by setup-mailserver.sh)/" "$ZONE_FILE" || true |
| 53 | say " Added A + MX records." |
| 54 | if docker ps --format '{{.Names}}' | grep -q '^coredns$'; then |
| 55 | docker restart coredns >/dev/null && say " Restarted CoreDNS." |
| 56 | fi |
| 57 | fi |
| 58 | fi |
| 59 | |
| 60 | # ── 2. Let's Encrypt cert for mail hostname ─────────────────────────────── |
| 61 | say "2. TLS cert for $MAIL_HOSTNAME" |
| 62 | if [[ -d "/etc/letsencrypt/live/$MAIL_HOSTNAME" ]]; then |
| 63 | say " Cert already exists." |
| 64 | else |
| 65 | if command -v certbot >/dev/null; then |
| 66 | sudo certbot certonly --nginx -d "$MAIL_HOSTNAME" --non-interactive --agree-tos --email "$LE_EMAIL" || \ |
| 67 | warn "certbot failed — run manually: sudo certbot certonly --nginx -d $MAIL_HOSTNAME" |
| 68 | else |
| 69 | warn "certbot not installed — install with: sudo apt-get install -y certbot python3-certbot-nginx" |
| 70 | fi |
| 71 | fi |
| 72 | |
| 73 | # ── 3. Bring up docker-mailserver ───────────────────────────────────────── |
| 74 | say "3. Bringing up docker-mailserver" |
| 75 | mkdir -p mail/mail-data mail/mail-state mail/mail-logs mail/config |
| 76 | MAIL_DOMAIN="$MAIL_DOMAIN" MAIL_HOSTNAME="$MAIL_HOSTNAME" $COMPOSE up -d |
| 77 | sleep 5 |
| 78 | |
| 79 | # ── 4. Generate DKIM keys ────────────────────────────────────────────────── |
| 80 | say "4. Generating DKIM keys" |
| 81 | docker exec mailserver setup config dkim domain "$MAIL_DOMAIN" || warn "DKIM setup failed — try again after the container has stabilized." |
| 82 | |
| 83 | # ── 5. Print the TXT records you must add to the DNS zone ────────────────── |
| 84 | say "5. TXT records for SPF, DKIM, DMARC — append these to $ZONE_FILE" |
| 85 | echo |
| 86 | echo "; ── SPF (sender policy) ──" |
| 87 | echo "${SUB} IN TXT \"v=spf1 mx ~all\"" |
| 88 | echo |
| 89 | echo "; ── DKIM ──" |
| 90 | DKIM_FILE="mail/config/opendkim/keys/${MAIL_DOMAIN}/mail.txt" |
| 91 | if [[ -f "$DKIM_FILE" ]]; then |
| 92 | cat "$DKIM_FILE" |
| 93 | else |
| 94 | warn "DKIM key file not found yet ($DKIM_FILE). Re-run after the mailserver is fully up:" |
| 95 | echo " docker exec mailserver setup config dkim domain $MAIL_DOMAIN" |
| 96 | fi |
| 97 | echo |
| 98 | echo "; ── DMARC ──" |
| 99 | echo "_dmarc.${SUB} IN TXT \"v=DMARC1; p=quarantine; rua=mailto:postmaster@${MAIL_DOMAIN}\"" |
| 100 | echo |
| 101 | say "Add the three TXT records above to $ZONE_FILE, bump the serial, and restart coredns." |
| 102 | |
| 103 | # ── 6. Test mailbox ─────────────────────────────────────────────────────── |
| 104 | say "6. Creating a test mailbox: postmaster@${MAIL_DOMAIN}" |
| 105 | docker exec mailserver setup email add "postmaster@${MAIL_DOMAIN}" "$(openssl rand -base64 16)" || warn "Mailbox creation failed — run manually." |
| 106 | |
| 107 | echo |
| 108 | say "Done. ClonAgent's UI (Mail page) can now provision per-agent mailboxes." |