#!/bin/bash # Resume Email Server Setup - Certificate and Final Steps # Run this after DNS records are configured set -euo pipefail # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # Get configuration from existing files or prompt get_config() { # Try to get hostname from existing Postfix config if [[ -f "/etc/postfix/main.cf" ]]; then HOSTNAME=$(grep "^myhostname" /etc/postfix/main.cf | cut -d= -f2 | tr -d ' ' || echo "") DOMAIN=$(grep "^mydomain" /etc/postfix/main.cf | cut -d= -f2 | tr -d ' ' || echo "") fi # If not found, prompt user if [[ -z "${HOSTNAME:-}" ]]; then read -p "Enter your hostname (e.g., mail.terrible.dev): " HOSTNAME fi if [[ -z "${DOMAIN:-}" ]]; then read -p "Enter your domain (e.g., terrible.dev): " DOMAIN fi if [[ -z "${ADMIN_EMAIL:-}" ]]; then read -p "Enter admin email: " ADMIN_EMAIL fi echo "Using:" echo " Hostname: $HOSTNAME" echo " Domain: $DOMAIN" echo " Admin Email: $ADMIN_EMAIL" echo } info() { echo -e "${BLUE}INFO: $1${NC}" } success() { echo -e "${GREEN}SUCCESS: $1${NC}" } warning() { echo -e "${YELLOW}WARNING: $1${NC}" } error() { echo -e "${RED}ERROR: $1${NC}" >&2 exit 1 } # Test DNS resolution test_dns() { info "Testing DNS resolution for $HOSTNAME..." if dig +short A "$HOSTNAME" | grep -q .; then IP=$(dig +short A "$HOSTNAME" | head -1) success "DNS resolution successful: $HOSTNAME -> $IP" return 0 else error "DNS resolution failed for $HOSTNAME. Please check your DNS records." return 1 fi } # Get SSL certificates get_ssl_certificates() { info "Obtaining Let's Encrypt certificates..." # Stop services that might be using port 80 systemctl stop apache2 2>/dev/null || true systemctl stop nginx 2>/dev/null || true # Get certificate if certbot certonly --standalone -d "$HOSTNAME" --email "$ADMIN_EMAIL" --agree-tos --non-interactive; then success "SSL certificates obtained successfully" else error "Failed to obtain SSL certificate. Check DNS and firewall." fi # Set up auto-renewal (crontab -l 2>/dev/null; echo "0 12 * * * /usr/bin/certbot renew --quiet") | crontab - success "SSL auto-renewal configured" } # Update configurations with SSL certificates update_ssl_configs() { info "Updating configurations with SSL certificates..." # Update Dovecot SSL config if [[ -f "/etc/dovecot/conf.d/10-ssl.conf" ]]; then cat > /etc/dovecot/conf.d/10-ssl.conf << EOF ssl = required ssl_cert =