diff --git a/modules/email/mail-log-analyzer.sh b/modules/email/mail-log-analyzer.sh index 58124c9..73a4f38 100755 --- a/modules/email/mail-log-analyzer.sh +++ b/modules/email/mail-log-analyzer.sh @@ -22,7 +22,7 @@ source "$SCRIPT_DIR/lib/email-functions.sh" ANALYSIS_HOURS=24 SPAM_THRESHOLD=100 # Emails per hour considered spam TEMP_DIR=$(mktemp -d) || { print_error "Failed to create temp directory"; exit 1; } -REPORT_FILE=$TEMP_DIR/"mail-analysis-$(date +%Y%m%d-%H%M%S).txt" +REPORT_FILE="$TEMP_DIR/mail-analysis-$(date +%Y%m%d-%H%M%S).txt" # Cleanup trap - runs on EXIT or SIGINT trap 'rm -rf "$TEMP_DIR" 2>/dev/null' EXIT INT TERM @@ -39,19 +39,9 @@ declare -A AUTH_ATTACK_IPS declare -A FROZEN_MESSAGES declare -A CONNECTION_FLOODS -# NEW: Enhanced tracking arrays -declare -A DOMAIN_SENT # domain → count of sent messages -declare -A DOMAIN_DELIVERED # domain → count of delivered messages -declare -A DOMAIN_BOUNCED # domain → count of bounced messages -declare -A DOMAIN_ISSUES # domain → list of issues -declare -A USER_SENT # user@domain → count of sent -declare -A USER_ISSUES # user@domain → list of issues -declare -A TOP_RECIPIENTS # recipient@domain → count -declare -A TOP_SENDERS # sender@domain → count -declare -A HOURLY_VOLUME # hour → message count -declare -A ERROR_SAMPLES # error_type → sample log line -declare -A DELIVERY_TIMES # Track message delivery times -declare -A REJECTED_REASONS # rejection reason → count +# NOTE: Removed 12 unused array declarations (were never populated or used) +# Original design intended in-memory arrays, but implementation uses temp files +# Data tracked in: "$TEMP_DIR/domains_sent.$$", domains_delivered.$$, etc. # Statistics TOTAL_SENT=0 @@ -69,7 +59,7 @@ PANIC_LOG_EXISTS=0 # Detect blacklist rejections detect_blacklist_issues() { local log_file="$1" - local temp_file=$TEMP_DIR/"blacklist_detections.$$" + local temp_file="$TEMP_DIR/blacklist_detections.$$" print_info "Scanning for blacklist rejections..." @@ -80,7 +70,7 @@ detect_blacklist_issues() { # ENHANCED: Filter out false positives (same as email-diagnostics.sh) # Exclude negation keywords, question contexts, and non-RBL blocks if [ -s "$temp_file" ]; then - local temp_filtered=$TEMP_DIR/"blacklist_detections_filtered.$$" + local temp_filtered="$TEMP_DIR/blacklist_detections_filtered.$$" grep -vE "not blacklist|not listed|NOT listed|no.*longer|removed from|delisted|successfully delisted|you.*can.*now|check if|if.*server|if your|we block|some.*block|unlike|rarely|are rare|except|not.*block|not.*in|but.*policy|policy.*block|firewall|rate limit|internally|internal.*block|local.*block|rejected.*not.*blacklist|based on sender|blocks are" -- "$temp_file" > "$temp_filtered" 2>/dev/null || true if [ -s "$temp_filtered" ]; then @@ -156,7 +146,7 @@ detect_blacklist_issues() { # Detect spam accounts (high volume senders) detect_spam_accounts() { local log_file="$1" - local temp_file=$TEMP_DIR/"sender_counts.$$" + local temp_file="$TEMP_DIR/sender_counts.$$" print_info "Analyzing sender volumes..." @@ -195,7 +185,7 @@ detect_spam_accounts() { # Detect SPF/DKIM/DMARC failures detect_auth_failures() { local log_file="$1" - local temp_file=$TEMP_DIR/"auth_failures.$$" + local temp_file="$TEMP_DIR/auth_failures.$$" print_info "Checking email authentication failures..." @@ -243,7 +233,7 @@ detect_auth_failures() { # Analyze bounce reasons analyze_bounces() { local log_file="$1" - local temp_file=$TEMP_DIR/"bounces.$$" + local temp_file="$TEMP_DIR/bounces.$$" print_info "Analyzing bounce messages..." @@ -326,7 +316,7 @@ detect_config_issues() { # Detect HELO/EHLO violations detect_helo_violations() { local log_file="$1" - local temp_file=$TEMP_DIR/"helo_violations.$$" + local temp_file="$TEMP_DIR/helo_violations.$$" print_info "Checking for HELO/EHLO violations..." @@ -349,7 +339,7 @@ detect_helo_violations() { local helo_name="${BASH_REMATCH[1]}" # Track Windows machine names and other suspicious HELOs if [[ "$helo_name" =~ ^WIN- ]] || [[ "$helo_name" =~ ^[0-9.]+$ ]]; then - echo "$helo_name" >> $TEMP_DIR/"suspicious_helos.$$" + echo "$helo_name" >> "$TEMP_DIR/suspicious_helos.$$" fi fi done < "$temp_file" @@ -402,7 +392,7 @@ check_panic_log() { ISSUES_FOUND["panic_log"]=$panic_lines # Get recent panic entries - tail -20 "$panic_log" > $TEMP_DIR/"recent_panics.$$" + tail -20 "$panic_log" > "$TEMP_DIR/recent_panics.$$" RECOMMENDATIONS["panic_log"]="CRITICAL: Panic log exists with $panic_lines entries! Check /var/log/exim_paniclog immediately. This indicates serious mail system problems." elif [ -f "$alt_panic_log" ] && [ -s "$alt_panic_log" ]; then @@ -416,7 +406,7 @@ check_panic_log() { # Detect connection flooding detect_connection_flooding() { local log_file="$1" - local temp_file=$TEMP_DIR/"connection_floods.$$" + local temp_file="$TEMP_DIR/connection_floods.$$" print_info "Analyzing connection patterns for flooding..." @@ -426,14 +416,14 @@ detect_connection_flooding() { if [ -s "$temp_file" ]; then # Count by IP grep -oE '\[([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\]' -- "$temp_file" | \ - sed 's/\[//;s/\]//' | sort | uniq -c | sort -rn > $TEMP_DIR/"flood_ips.$$" + sed 's/\[//;s/\]//' | sort | uniq -c | sort -rn > "$TEMP_DIR/flood_ips.$$" # Flag IPs with >20 rapid disconnects while read count ip; do if [ "$count" -gt 20 ]; then CONNECTION_FLOODS["$ip"]=$count fi - done < $TEMP_DIR/"flood_ips.$$" + done < "$TEMP_DIR/flood_ips.$$" if [ ${#CONNECTION_FLOODS[@]} -gt 0 ]; then ISSUES_FOUND["connection_flooding"]=${#CONNECTION_FLOODS[@]} @@ -441,13 +431,13 @@ detect_connection_flooding() { fi fi - rm -f "$temp_file" $TEMP_DIR/"flood_ips.$$" + rm -f "$temp_file" "$TEMP_DIR/flood_ips.$$" } # Detect SMTP auth brute force attempts detect_smtp_auth_attacks() { local log_file="$1" - local temp_file=$TEMP_DIR/"smtp_auth_failures.$$" + local temp_file="$TEMP_DIR/smtp_auth_failures.$$" print_info "Detecting SMTP authentication failures..." @@ -459,14 +449,14 @@ detect_smtp_auth_attacks() { # Extract IPs with auth failures grep -oE '\[([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\]' -- "$temp_file" | \ - sed 's/\[//;s/\]//' | sort | uniq -c | sort -rn > $TEMP_DIR/"auth_attack_ips.$$" + sed 's/\[//;s/\]//' | sort | uniq -c | sort -rn > "$TEMP_DIR/auth_attack_ips.$$" # Flag IPs with >10 failures (brute force) while read count ip; do if [ "$count" -gt 10 ]; then AUTH_ATTACK_IPS["$ip"]=$count fi - done < $TEMP_DIR/"auth_attack_ips.$$" + done < "$TEMP_DIR/auth_attack_ips.$$" if [ ${#AUTH_ATTACK_IPS[@]} -gt 0 ]; then ISSUES_FOUND["auth_attacks"]=${#AUTH_ATTACK_IPS[@]} @@ -477,13 +467,13 @@ detect_smtp_auth_attacks() { fi fi - rm -f "$temp_file" $TEMP_DIR/"auth_attack_ips.$$" + rm -f "$temp_file" "$TEMP_DIR/auth_attack_ips.$$" } # Detect deferral loops detect_deferral_loops() { local log_file="$1" - local temp_file=$TEMP_DIR/"deferrals.$$" + local temp_file="$TEMP_DIR/deferrals.$$" print_info "Checking for deferral loops..." @@ -495,7 +485,7 @@ detect_deferral_loops() { # Extract domains with deferral issues grep -oE '@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}' -- "$temp_file" | \ - sed 's/@//' | sort | uniq -c | sort -rn | head -10 > $TEMP_DIR/"deferral_domains.$$" + sed 's/@//' | sort | uniq -c | sort -rn | head -10 > "$TEMP_DIR/deferral_domains.$$" ISSUES_FOUND["deferral_loops"]=$deferral_loop_count RECOMMENDATIONS["deferral_loops"]="Found $deferral_loop_count messages in deferral loops. These will eventually bounce. Check recipient domains and consider manual intervention." @@ -511,7 +501,7 @@ detect_deferral_loops() { # Detect TLS/SSL issues detect_tls_issues() { local log_file="$1" - local temp_file=$TEMP_DIR/"tls_issues.$$" + local temp_file="$TEMP_DIR/tls_issues.$$" print_info "Analyzing TLS/SSL errors..." @@ -541,7 +531,7 @@ detect_tls_issues() { if [ ${#TLS_IPS[@]} -gt 0 ]; then for ip in "${!TLS_IPS[@]}"; do echo "${TLS_IPS[$ip]} $ip" - done | sort -rn | head -10 > $TEMP_DIR/"tls_error_ips.$$" + done | sort -rn | head -10 > "$TEMP_DIR/tls_error_ips.$$" fi RECOMMENDATIONS["tls_errors"]="Found $count TLS/SSL errors. Most common: EOF ($ssl_eof), Broken pipe ($ssl_broken_pipe), Packet length ($ssl_packet_length). These are usually scanner/bot probes and can be safely ignored unless affecting legitimate traffic." @@ -553,7 +543,7 @@ detect_tls_issues() { # Detect message size rejections detect_size_rejections() { local log_file="$1" - local temp_file=$TEMP_DIR/"size_rejections.$$" + local temp_file="$TEMP_DIR/size_rejections.$$" print_info "Checking for message size rejections..." @@ -566,7 +556,7 @@ detect_size_rejections() { # Extract affected users/domains grep -oE '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}' -- "$temp_file" | \ - sort | uniq -c | sort -rn | head -10 > $TEMP_DIR/"size_reject_users.$$" + sort | uniq -c | sort -rn | head -10 > "$TEMP_DIR/size_reject_users.$$" RECOMMENDATIONS["size_rejections"]="Found $count message size rejections. Users are trying to send files that exceed size limits. Educate users about limits and suggest file-sharing alternatives (Dropbox, Google Drive, etc.)." fi @@ -577,7 +567,7 @@ detect_size_rejections() { # Detect routing/forwarding loops detect_routing_loops() { local log_file="$1" - local temp_file=$TEMP_DIR/"routing_loops.$$" + local temp_file="$TEMP_DIR/routing_loops.$$" print_info "Detecting mail routing loops..." @@ -590,7 +580,7 @@ detect_routing_loops() { # Extract affected addresses grep -oE '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}' -- "$temp_file" | \ - sort | uniq -c | sort -rn | head -10 > $TEMP_DIR/"loop_addresses.$$" + sort | uniq -c | sort -rn | head -10 > "$TEMP_DIR/loop_addresses.$$" RECOMMENDATIONS["routing_loops"]="Found $count routing loops. These are caused by misconfigured email forwards (.forward files, auto-forwards, etc.). Check forwarding rules for affected addresses and break the loops." fi @@ -854,9 +844,9 @@ display_issues() { fi # Show timeline - first and last occurrence - if [ -f $TEMP_DIR/"blacklist_detections.$$" ]; then - local first_occurrence=$(head -1 $TEMP_DIR/"blacklist_detections.$$" | awk '{print $1, $2}') - local last_occurrence=$(tail -1 $TEMP_DIR/"blacklist_detections.$$" | awk '{print $1, $2}') + if [ -f "$TEMP_DIR/blacklist_detections.$$" ]; then + local first_occurrence=$(head -1 "$TEMP_DIR/blacklist_detections.$$" | awk '{print $1, $2}') + local last_occurrence=$(tail -1 "$TEMP_DIR/blacklist_detections.$$" | awk '{print $1, $2}') echo " Timeline:" echo " First seen: $first_occurrence" @@ -873,9 +863,9 @@ display_issues() { fi # Show which domains/users triggered it (top 5) - if [ -f $TEMP_DIR/"blacklist_detections.$$" ]; then + if [ -f "$TEMP_DIR/blacklist_detections.$$" ]; then echo " Affected senders (top 5):" - grep -oE 'F=<[^>]+>' $TEMP_DIR/"blacklist_detections.$$" 2>/dev/null | \ + grep -oE 'F=<[^>]+>' "$TEMP_DIR/blacklist_detections.$$" 2>/dev/null | \ sed 's/F=//' | sort | uniq -c | sort -rn | head -5 | \ while read count sender; do printf " - %-45s %d times\n" "$sender" "$count" @@ -989,7 +979,7 @@ display_issues() { [ "$count" -ge 10 ] && break done fi - if [ -f $TEMP_DIR/"suspicious_helos.$$" ]; then + if [ -f "$TEMP_DIR/suspicious_helos.$$" ]; then echo "" echo " Suspicious HELO names detected:" sort "$TEMP_DIR/"suspicious_helos.$$ | uniq -c | sort -rn | head -5 | while read count helo; do @@ -1005,9 +995,9 @@ display_issues() { if [ -n "${ISSUES_FOUND[panic_log]}" ]; then echo -e "${RED}${BOLD}💥 CRITICAL - PANIC LOG EXISTS (${ISSUES_FOUND[panic_log]} entries)${NC}" echo "" - if [ -f $TEMP_DIR/"recent_panics.$$" ]; then + if [ -f "$TEMP_DIR/recent_panics.$$" ]; then echo " Recent panic log entries:" - cat $TEMP_DIR/"recent_panics.$$" | head -5 | sed 's/^/ /' + cat "$TEMP_DIR/recent_panics.$$" | head -5 | sed 's/^/ /' echo "" fi echo -e " ${RED}${BOLD}Action Required:${NC} ${RECOMMENDATIONS[panic_log]}" @@ -1062,9 +1052,9 @@ display_issues() { if [ -n "${ISSUES_FOUND[deferral_loops]}" ]; then echo -e "${YELLOW}${BOLD}🔄 DEFERRAL LOOPS (${ISSUES_FOUND[deferral_loops]} messages)${NC}" echo "" - if [ -f $TEMP_DIR/"deferral_domains.$$" ]; then + if [ -f "$TEMP_DIR/deferral_domains.$$" ]; then echo " Domains with deferral issues:" - head -5 $TEMP_DIR/"deferral_domains.$$" | while read count domain; do + head -5 "$TEMP_DIR/deferral_domains.$$" | while read count domain; do printf " - %-40s %d messages\n" "$domain" "$count" done echo "" @@ -1077,9 +1067,9 @@ display_issues() { if [ -n "${ISSUES_FOUND[tls_errors]}" ]; then echo -e "${YELLOW}${BOLD}🔒 TLS/SSL ERRORS (${ISSUES_FOUND[tls_errors]} occurrences)${NC}" echo "" - if [ -f $TEMP_DIR/"tls_error_ips.$$" ]; then + if [ -f "$TEMP_DIR/tls_error_ips.$$" ]; then echo " Top IPs with TLS errors:" - head -10 $TEMP_DIR/"tls_error_ips.$$" | while read count ip; do + head -10 "$TEMP_DIR/tls_error_ips.$$" | while read count ip; do printf " - %-40s %d errors\n" "$ip" "$count" done echo "" @@ -1092,9 +1082,9 @@ display_issues() { if [ -n "${ISSUES_FOUND[size_rejections]}" ]; then echo -e "${YELLOW}${BOLD}📦 MESSAGE SIZE REJECTIONS (${ISSUES_FOUND[size_rejections]} occurrences)${NC}" echo "" - if [ -f $TEMP_DIR/"size_reject_users.$$" ]; then + if [ -f "$TEMP_DIR/size_reject_users.$$" ]; then echo " Users affected by size limits:" - head -10 $TEMP_DIR/"size_reject_users.$$" | while read count user; do + head -10 "$TEMP_DIR/size_reject_users.$$" | while read count user; do printf " - %-40s %d rejections\n" "$user" "$count" done echo "" @@ -1107,9 +1097,9 @@ display_issues() { if [ -n "${ISSUES_FOUND[routing_loops]}" ]; then echo -e "${RED}${BOLD}♻️ ROUTING LOOPS (${ISSUES_FOUND[routing_loops]} detected)${NC}" echo "" - if [ -f $TEMP_DIR/"loop_addresses.$$" ]; then + if [ -f "$TEMP_DIR/loop_addresses.$$" ]; then echo " Addresses caught in loops:" - head -10 $TEMP_DIR/"loop_addresses.$$" | while read count address; do + head -10 "$TEMP_DIR/loop_addresses.$$" | while read count address; do printf " - %-40s %d times\n" "$address" "$count" done echo "" @@ -1361,12 +1351,8 @@ display_rejection_analysis() { echo "" } -# Display error samples - DISABLED to avoid information overload -display_error_samples() { - # This section is intentionally disabled - # The existing issue detection already shows relevant error details - return -} +# NOTE: display_error_samples() removed (was disabled and never displayed data) +# Error samples are included inline in other display functions # Save report to file save_report() { @@ -1378,7 +1364,6 @@ save_report() { display_user_analysis display_hourly_distribution display_rejection_analysis - display_error_samples } | tee "$REPORT_FILE" >/dev/null echo -e "${GREEN}Report saved to: $REPORT_FILE${NC}" @@ -1449,7 +1434,7 @@ main() { echo "" # Create temporary log file with time-filtered entries - TEMP_LOG=$TEMP_DIR/"mail_analysis_$$.log" + TEMP_LOG="$TEMP_DIR/"mail_analysis_$$".log" if [ "$ANALYSIS_HOURS" -eq 999999 ]; then # Use entire log @@ -1520,7 +1505,7 @@ main() { analyze_hourly_patterns "$TEMP_LOG" analyze_rejection_details "$TEMP_LOG" calculate_domain_success_rates "$TEMP_LOG" - capture_error_samples "$TEMP_LOG" + # NOTE: capture_error_samples removed (created orphaned data files never displayed) # Display results clear