#!/bin/bash ############################################################################## # # qmail SPP-Plugin "goodmailto.sh" # See http://qmail-spp.sourceforge.net # Only accept "RCPT TO"-Addresses in "control/goodmailfrom" file # Version 1.4, Stefan Onderka, 2016-02-10 # # Location: # /var/qmail/plugins/goodmailto.sh # # Activation (in /var/qmail/control/smtpplugins): # [rcpt] # :plugins/goodmailto.sh # # Don't forget the ":" to exec a shell # # We have: # $SMTPRCPTTO - "argument of last 'RCPT' command" # $SMTPHELOHOST - HELO hostname # goodmailto - file with "user"-parts of existing accounts # $NOGOODMAILTO - If set to 1 test is skipped (mail in -> out f.e.) # Else test is performed (mail out -> in f.e.) # # We respond: # "H" # ... if $SMTPRCPTTO is accepted (existing mail account) # # "H" # "E" # ... if $SMTPRCPTTO is not accepted (non-existing mail account) # # "H" # ... if "goodmailto" is not readable (In dubio pro reo) # # Outputs (see http://qmail-spp.sourceforge.net/doc/): # "Hmsg" - Sets mail-header msg # "Rmsg" - Sends smtp-message xxx and drops connection # ############################################################################## # # SMTP Code 550 (permanent error): # Requested action not taken: mailbox unavailable (e.g., mailbox not # found, no access, or command rejected for policy reasons) # # Variables ############################################################################## # PID MY_PID=$$ # Logfile LOGFILE="/var/log/mail/mail.goodmailto_plugin" # RCPT TO - address from environment LOOKUP_ADDR=$SMTPRCPTTO # Logging prefix LOG_STRING="plugins/goodmailto [${MY_PID}]" # goodmailto - file GOODMT="/var/qmail/control/goodmailto" # Response: RCPT TO not OK SMTP_ERROR="550 No mailbox or alias named" # Response: RCPT TO OK SMTP_OK="250 ok" # Our smtp-error as number SMTP_ERROR_NUMBER="(#5.1.1)" # Timestamp TIMESTAMP=`date +"%Y-%m-%d %H:%M:%S"` # Our mail-header - added to see if a certain msg has been processed by plugin ############################################################################## CUST_HEADER="X-GoodMailTo:" CUST_FOUND="Rcpt found" CUST_NOTFOUND="Rcpt not found" CUST_SKIPPED="Skipped" CUST_ERROR="File Error (/var/qmail/control/goodmailto)" # $NOGOODMAILTO set? (via tcprules): skip check, accept and exit ############################################################################## if [ -n "${NOGOODMAILTO+x}" ]; then echo "H$CUST_HEADER $CUST_SKIPPED" echo $SMTP_OK echo "${LOG_STRING}: [$SMTPHELOHOST] from: $SMTPMAILFROM to: $SMTPRCPTTO status: $CUST_SKIPPED" >&2 #echo "$TIMESTAMP ${LOG_STRING}: $SMTPHELOHOST $SMTPMAILFROM to: $SMTPRCPTTO status: $CUST_SKIPPED" >> $LOGFILE exit 0 fi if [ -r $GOODMT ]; then # File /var/qmail/control/goodmailto readable ########################################################################### GOODMAILTO_ALL=`cat $GOODMT` # "User"-part only - checking for a valid domain is not our job... NAMEPART=`echo $LOOKUP_ADDR | cut -d"@" -f1` # Find RegEx - case insensitive, on one line, no more, no less ########################################################################### if `cat ${GOODMT} | egrep -i "^${NAMEPART}$" 1> /dev/null 2>&1` then # Found in goodmailto - accept echo "H$CUST_HEADER $CUST_FOUND (${NAMEPART})" echo $SMTP_OK echo "${LOG_STRING}: $SMTPHELOHOST from: $SMTPMAILFROM to: $SMTPRCPTTO status: $CUST_FOUND" >&2 #echo "$TIMESTAMP ${LOG_STRING}: $SMTPHELOHOST from: $SMTPMAILFROM to: $SMTPRCPTTO status: $CUST_FOUND" >> $LOGFILE exit 0 else # Not found in goodmailto - deny echo "H$CUST_HEADER $CUST_NOTFOUND (${NAMEPART})" echo "E$SMTP_ERROR $NAMEPART $SMTP_ERROR_NUMBER" echo "${LOG_STRING}: $SMTPHELOHOST from: $SMTPMAILFROM to: $SMTPRCPTTO status: $CUST_NOTFOUND" >&2 #echo "$TIMESTAMP ${LOG_STRING}: $SMTPHELOHOST from: $SMTPMAILFROM to: $SMTPRCPTTO status: $CUST_NOTFOUND" >> $LOGFILE exit 0 fi else # File /var/qmail/control/goodmailto not readable ########################################################################### # Accept echo "H$CUST_HEADER $CUST_ERROR" echo $SMTP_OK echo "${LOG_STRING}: $SMTPHELOHOST from: $SMTPMAILFROM to: $SMTPRCPTTO status: $CUST_ERROR" >&2 #echo "$TIMESTAMP ${LOG_STRING}: $SMTPHELOHOST from: $SMTPMAILFROM to: $SMTPRCPTTO status: $CUST_ERROR" >> $LOGFILE fi