Skip to content

Commit

Permalink
Add function for reject pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
filipnet committed Sep 24, 2021
1 parent 44675ab commit f680f75
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 48 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# postfix-bounce-report
The script generates an HTML report based on DDNS blacklist rejected messages. Another script continuously writes "recipient addresses" into a list. If an incoming message is rejected and is present in the list of "recipients", the subject of email is changing.
The script generates an HTML report based on rejected messages. Another script continuously writes "recipient addresses" into a list. If an incoming message is rejected and is present in the list of "recipients", the subject of email is changing.

<!-- TOC -->

- [postfix-bounce-report](#postfix-bounce-report)
- [FEATURES](#features)
- [HOW TO INSTALL](#how-to-install)
- [PREREQUISITES PERL INTERPRETER](#prerequisites-perl-interpreter)
- [CLONE REPOSITORY](#clone-repository)
- [HOW TO USE](#how-to-use)
- [CONFIGURATION](#configuration)
- [CREATE SCHEDULED TASKS](#create-scheduled-tasks)
- [ADDITIONAL INFORMATION](#additional-information)
- [LICENSE](#license)

<!-- /TOC -->

## FEATURES
- build_submission_recipients.sh : Analyzes the postfix maillog for outgoing e-mails and continuously creates a list of recipients
Expand Down
8 changes: 4 additions & 4 deletions config.xml.sample
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0"?>
<config>
<maillog>/var/log/maillog</maillog>
<maillog>/var/log/maillog</maillog>
<maillog_period>24</maillog_period>
<maillog_pattern>blocked using</maillog_pattern>
<logmail_from>do-not-reply@example.de</logmail_from>
<logmail_to>administrator@example.de</logmail_to>
<maillog_pattern>blocked\|reject</maillog_pattern>
<logmail_from>do-not-reply@example.de</logmail_from>
<logmail_to>administrator@example.de</logmail_to>
<logmail_subject>Postfix Bounce Report</logmail_subject>
<recipients_check>true</recipients_check>
<recipients_list>/etc/postfix/submission_recipient</recipients_list>
Expand Down
98 changes: 55 additions & 43 deletions postfix-bounce-report.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
#
# Script Name : postfix-bounce-report.sh
# Description : Analyzes the postfix logfile for bounced emails by DDNS blacklist,
# Description : Analyzes the postfix logfile for bounced and rejected emails,
# optionaly validate/cross check FROM-value against submission list.
# Script also generates HTML report and send via sendmail.
# Author : https://github.com/filipnet/postfix-bounce-report
Expand Down Expand Up @@ -29,13 +29,13 @@ COUNTBOUNCES=$( [ -n "$ALLBOUNCES" ] && echo "$ALLBOUNCES" | wc -l || echo 0 )
if [ ${COUNTBOUNCES} -gt 0 ]; then
MAILINFO='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><title></title>'
MAILINFO+='<style>'
MAILINFO+='table.blueTable { border: 1px solid #1C6EA4; background-color: #EEEEEE; width: 100%; text-align: left; border-collapse: collapse; }
MAILINFO+='table.blueTable { border: 1px solid #1C6EA4; background-color: #EEEEEE; width: 100%; text-align: left; border-collapse: collapse; }
table.blueTable td, table.blueTable th { border: 1px solid #AAAAAA; padding: 3px 2px; }'
MAILINFO+='table.blueTable tbody td { font-size: 10px; }'
MAILINFO+='table.blueTable tr:nth-child(even) { background: #D0E4F5; }'
MAILINFO+='table.blueTable thead { font-size: 10px; background: #1C6EA4; background: -moz-linear-gradient(top, #5592bb 0%, #327cad 66%, #1C6EA4 100%); background: -webkit-linear-gradient(top, #5592bb 0%, #327cad 66%, #1C6EA4 100%); background: linear-gradient(to bottom, #5592bb 0%, #327cad 66%, #1C6EA4 100%); border-bottom: 2px solid #444444; }'
MAILINFO+='table.blueTable thead th { font-size: 10px; font-weight: bold; color: #FFFFFF; border-left: 2px solid #D0E4F5; }'
MAILINFO+='table.blueTable thead th:first-child { border-left: none; }'
MAILINFO+='table.blueTable tbody td { font-size: 10px; }'
MAILINFO+='table.blueTable tr:nth-child(even) { background: #D0E4F5; }'
MAILINFO+='table.blueTable thead { font-size: 10px; background: #1C6EA4; background: -moz-linear-gradient(top, #5592bb 0%, #327cad 66%, #1C6EA4 100%); background: -webkit-linear-gradient(top, #5592bb 0%, #327cad 66%, #1C6EA4 100%); background: linear-gradient(to bottom, #5592bb 0%, #327cad 66%, #1C6EA4 100%); border-bottom: 2px solid #444444; }'
MAILINFO+='table.blueTable thead th { font-size: 10px; font-weight: bold; color: #FFFFFF; border-left: 2px solid #D0E4F5; }'
MAILINFO+='table.blueTable thead th:first-child { border-left: none; }'


MAILINFO+='</style>'
Expand All @@ -46,50 +46,62 @@ table.blueTable td, table.blueTable th { border: 1px solid #AAAAAA; padding: 3px
do
BOUNCE="${BOUNCE//$'\n'/ }"

DATETIME=$(perl -pe "s/^(\w+\s+\w+\s+\w+:\w+:\w+)\s.*/\1/g" <<< ${BOUNCE})

if [[ "$RECIPIENTS_CHECK" = true ]]; then
MAILFROM=$(perl -pe "s/.*?from=<(.*?)>.*/\1/gm" <<< ${BOUNCE})
if [[ "$MAILFROM" =~ $(echo ^\($(paste -sd'|' ${RECIPIENTS_LIST})\)$) ]]; then
#echo "$MAILFROM is in the list"
MAILFROM=$(perl -pe "s/.*?from=<(.*?)>.*/\1/gm" <<< ${BOUNCE})
MAILFROM="<span style='color:#FFFFFF; background-color:#FF0000'><b> ${MAILFROM} </b></span>"
BOUNCESEVERETY="[CRITICAL] "
else
#echo "$MAILFROM is not in the list"
MAILFROM=$(perl -pe "s/.*?from=<(.*?)>.*/\1/gm" <<< ${BOUNCE})
fi
else
MAILFROM=$(perl -pe "s/.*?from=<(.*?)>.*/\1/gm" <<< ${BOUNCE})
fi

MAILTO=$(perl -pe "s/.*to=<(.*?)>.*/\1/g" <<< ${BOUNCE})
HELO=$(perl -pe "s/.*helo=<(.*?)>.*/\1/g" <<< ${BOUNCE})
HOSTIP=$(awk 'match($0, /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/) {i[substr($0,RSTART,RLENGTH)]=1}END{for(ip in i){printf("%s\n", ip)}}' <<< ${BOUNCE})
REASON=$(perl -pe "s/.*blocked using (.*?);.*/\1/g" <<< ${BOUNCE})

DATETIME=$(perl -pe "s/^(\w+\s+\w+\s+\w+:\w+:\w+)\s.*/\1/g" <<< ${BOUNCE})

if [[ "$RECIPIENTS_CHECK" = true ]]; then
MAILFROM=$(perl -pe "s/.*?from=<(.*?)>.*/\1/gm" <<< ${BOUNCE})
if [[ "$MAILFROM" =~ $(echo ^\($(paste -sd'|' ${RECIPIENTS_LIST})\)$) ]]; then
#echo "$MAILFROM is in the list"
MAILFROM=$(perl -pe "s/.*?from=<(.*?)>.*/\1/gm" <<< ${BOUNCE})
MAILFROM="<span style='color:#FFFFFF; background-color:#FF0000'><b> ${MAILFROM} </b></span>"
BOUNCESEVERETY="[CRITICAL] "
else
#echo "$MAILFROM is not in the list"
MAILFROM=$(perl -pe "s/.*?from=<(.*?)>.*/\1/gm" <<< ${BOUNCE})
fi
else
MAILFROM=$(perl -pe "s/.*?from=<(.*?)>.*/\1/gm" <<< ${BOUNCE})
fi

MAILTO=$(perl -pe "s/.*to=<(.*?)>.*/\1/g" <<< ${BOUNCE})
HELO=$(perl -pe "s/.*helo=<(.*?)>.*/\1/g" <<< ${BOUNCE})
HOSTIP=$(awk 'match($0, /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/) {i[substr($0,RSTART,RLENGTH)]=1}END{for(ip in i){printf("%s\n", ip)}}' <<< ${BOUNCE})
if [[ $BOUNCE == *"blocked using"* ]]; then
REASON=$(perl -pe "s/.*blocked using (.*?);.*/\1/g" <<< ${BOUNCE})
elif [[ $BOUNCE == *"rejected"* ]]; then
REASON=$(perl -pe "s/.*rejected: */\1/g" <<< ${BOUNCE})
REASON=$(echo $REASON |sed -r 's/[<>]+//g')
elif [[ $BOUNCE == *"milter-reject"* ]]; then
REASON=$(perl -pe "s/.*milter-reject: */\1/g" <<< ${BOUNCE})
REASON=$(echo $REASON |sed -r 's/[<>!]+//g')
elif [[ $BOUNCE == *"reject"* ]]; then
REASON=$(perl -pe "s/.*reject: */\1/g" <<< ${BOUNCE})
REASON=$(echo $REASON |sed -r 's/[<>]+//g')
else
REASON="undefinied: $BOUNCE"
fi
MAILINFO+="<tr><td>${DATETIME}</td><td>${MAILFROM}</td><td>${MAILTO}</td><td>${HELO}</td><td>${HOSTIP}</td><td>${REASON}</td></tr>"

done <<< "$ALLBOUNCES"

MAILINFO+="</table>"
MAILINFO+="<br/>"
MAILINFO+="<table>"
MAILINFO+="<table>"
TIME_DIFF=$(($(date +"%s")-${TIME_START}))
MAILINFO+="<tr><td><strong>Script runtime:</strong></td><td>$((${TIME_DIFF} / 60)) Minutes</td><td>$((${TIME_DIFF} % 60)) Seconds</td><td></td></tr>"
MAILINFO+="</table></body></html>"

if [ ! $BOUNCESEVERETY ]; then
if [ ${COUNTBOUNCES} -gt "${BOUNCESEVERETY_THRESHOLD}" ]; then BOUNCESEVERETY="[WARNING] "; else BOUNCESEVERETY="[INFO] "; fi
fi

(
echo "From: ${LOGMAILFROM}"
echo "To: ${LOGMAILTO}"
echo "Subject: ${BOUNCESEVERETY}${LOGMAILSUBJECT}"
echo "Mime-Version: 1.0"
echo "Content-Type: text/html"
echo ${MAILINFO}
) | sendmail -t

fi
if [ ${COUNTBOUNCES} -gt "${BOUNCESEVERETY_THRESHOLD}" ]; then BOUNCESEVERETY="[WARNING] "; else BOUNCESEVERETY="[INFO] "; fi
fi

(
echo "From: ${LOGMAILFROM}"
echo "To: ${LOGMAILTO}"
echo "Subject: ${BOUNCESEVERETY}${LOGMAILSUBJECT}"
echo "Mime-Version: 1.0"
echo "Content-Type: text/html"
echo ${MAILINFO}
) | sendmail -t

fi

0 comments on commit f680f75

Please sign in to comment.