Fix critical logic errors in email diagnostics scripts

CRITICAL FIXES (5 issues):
1. email-diagnostics.sh: Fix inverted sender/recipient extraction logic
   - Lines 292-303: Corrected pattern matching to properly extract recipients and senders
   - Removed inverted grep patterns that were looking for wrong log entry types

2. mail-log-analyzer.sh: Fix string comparison with percent sign
   - Line 1184-1186: Properly extract numeric value before '%' character
   - Use sed to isolate leading digits for numeric comparison

3. email-diagnostics.sh: Fix malformed grep syntax
   - Line 525-527: Corrected grep command structure with -e options
   - Changed to -iE with pipe patterns and proper file argument placement

4. mail-log-analyzer.sh: Fix overly broad domain bounce pattern
   - Line 749: Changed from "^.*${domain}" to "\b${domain}$"
   - Prevents false positives from substring domain matches

5. mail-log-analyzer.sh: Fix undefined TEMP_LOG variable
   - Line 860: Changed TEMP_LOG to MAIL_LOG (the actual global variable)
   - Added error handling with 2>/dev/null

HIGH SEVERITY FIXES (2 issues):
6. mail-log-analyzer.sh: Fix AWK uninitialized variable
   - Lines 1447-1456: Added BEGIN block to initialize print_line = 0
   - Prevents first log entries from being incorrectly filtered

7. mail-log-analyzer.sh: Fix overly permissive bounce detection pattern
   - Line 247: Changed from "(==|defer)" to more specific pattern
   - Prevents false positives from non-bounce defer messages

MODERATE FIXES (3 issues):
8. mail-queue-inspector.sh: Fix queue message count mismatch
   - Line 41: Changed head -40 to head -20 to match label

9. deliverability-test.sh: Fix fragile SMTP connection test
   - Lines 102-106: Added nc availability check and fallback to bash TCP
   - Proper variable quoting and error handling

10. blacklist-check.sh: Replace deprecated host command with dig
    - Line 52: Changed from host to dig +short for consistency and timeout control

All scripts pass syntax validation.
Impact: Logic errors fixed, no security issues introduced, all existing functionality preserved.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
cschantz
2026-02-07 00:39:07 -05:00
parent a7a76e6bac
commit 89ad050222
9 changed files with 1883 additions and 26 deletions
+16 -2
View File
@@ -98,8 +98,22 @@ else
server=$(echo "$server" | sed 's/\.$//')
echo " • Priority $priority: $server"
# Try to connect to SMTP
if timeout 3 bash -c "echo 'QUIT' | nc -w 1 $server 25" &>/dev/null; then
# Try to connect to SMTP using multiple methods
local smtp_ok=0
# Try nc first if available
if command -v nc &>/dev/null; then
if timeout 3 bash -c "echo 'QUIT' | nc -z -w 1 \"$server\" 25" &>/dev/null; then
smtp_ok=1
fi
fi
# Try timeout with bash TCP if nc not available
if [ $smtp_ok -eq 0 ] && timeout 3 bash -c "exec 3<>/dev/tcp/$server/25 && echo QUIT >&3 && cat <&3" &>/dev/null; then
smtp_ok=1
fi
if [ $smtp_ok -eq 1 ]; then
print_success " ✓ SMTP port 25 responds"
else
print_warning " ⚠ SMTP port 25 not responding (may use port 587/465)"