#!/bin/bash ############################################################################## # # qmail SPP-Plugin "goodmailto.sh" # See http://qmail-spp.sourceforge.net # Only accept "RCPT TO"-Addresses in "control/goodmailfrom" file # Version 1.1, Stefan Onderka, 2008-01-05 # # 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 ############################################################################## # RCPT TO - address from environment LOOKUP_ADDR=$SMTPRCPTTO # goodmailto - file GOODMT="/var/qmail/control/goodmailto" # Our smtp-error SMTP_ERROR="550 no mailbox or alias named" # Our smtp-error as number SMTP_ERROR_NUMBER="(#5.5.0)" # Our mail-header - added to see if a certain msg has been processed by plugin ############################################################################## CUST_HEADER="X-GoodMailTo:" CUST_FOUND="found" CUST_NOTFOUND="not found" CUST_SKIPPED="skipped" CUST_ERROR="file-error" # $NOGOODMAILTO set to "1" (via tcprules): skip check, accept and exit ############################################################################## if [ "$NOGOODMAILTO" == "1" ]; then echo "H$CUST_HEADER $CUST_SKIPPED" echo "goodmailto host: $SMTPHELOHOST from: $SMTPMAILFROM to: $SMTPRCPTTO status: $CUST_SKIPPED" >&2 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" echo "goodmailto host: $SMTPHELOHOST from: $SMTPMAILFROM to: $SMTPRCPTTO status: $CUST_FOUND" >&2 exit 0 else # Not found in goodmailto - deny echo "H$CUST_HEADER $CUST_NOTFOUND" echo "R$SMTP_ERROR $NAMEPART $SMTP_ERROR_NUMBER" echo "goodmailto host: $SMTPHELOHOST from: $SMTPMAILFROM to: $SMTPRCPTTO status: $CUST_NOTFOUND" >&2 exit 0 fi else # File /var/qmail/control/goodmailto not readable ########################################################################### # Accept echo "H$CUST_HEADER $CUST_ERROR" echo "goodmailto host: $SMTPHELOHOST from: $SMTPMAILFROM to: $SMTPRCPTTO status: $CUST_ERROR" >&2 fi