From f680f75eafe86084fddf2684c665f5b83543728f Mon Sep 17 00:00:00 2001 From: filipnet Date: Fri, 24 Sep 2021 19:48:47 +0200 Subject: [PATCH] Add function for reject pattern --- README.md | 17 ++++++- config.xml.sample | 8 ++-- postfix-bounce-report.sh | 98 ++++++++++++++++++++++------------------ 3 files changed, 75 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index 3b09a46..0f7c4fe 100644 --- a/README.md +++ b/README.md @@ -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. + + + +- [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) + + ## FEATURES - build_submission_recipients.sh : Analyzes the postfix maillog for outgoing e-mails and continuously creates a list of recipients diff --git a/config.xml.sample b/config.xml.sample index b71a323..a0da587 100644 --- a/config.xml.sample +++ b/config.xml.sample @@ -1,10 +1,10 @@ - /var/log/maillog + /var/log/maillog 24 - blocked using - do-not-reply@example.de - administrator@example.de + blocked\|reject + do-not-reply@example.de + administrator@example.de Postfix Bounce Report true /etc/postfix/submission_recipient diff --git a/postfix-bounce-report.sh b/postfix-bounce-report.sh index 7040c44..9d3796b 100755 --- a/postfix-bounce-report.sh +++ b/postfix-bounce-report.sh @@ -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 @@ -29,13 +29,13 @@ COUNTBOUNCES=$( [ -n "$ALLBOUNCES" ] && echo "$ALLBOUNCES" | wc -l || echo 0 ) if [ ${COUNTBOUNCES} -gt 0 ]; then MAILINFO='' MAILINFO+='' @@ -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=" ${MAILFROM} " - 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=" ${MAILFROM} " + 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+="${DATETIME}${MAILFROM}${MAILTO}${HELO}${HOSTIP}${REASON}" done <<< "$ALLBOUNCES" MAILINFO+="" MAILINFO+="
" - MAILINFO+="" + MAILINFO+="
" TIME_DIFF=$(($(date +"%s")-${TIME_START})) MAILINFO+="" MAILINFO+="
Script runtime:$((${TIME_DIFF} / 60)) Minutes$((${TIME_DIFF} % 60)) Seconds
" 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 \ No newline at end of file