Register
It is currently Fri Dec 19, 2014 7:09 pm

SMTPDD- Dual Delivery of Smtp


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Fri Oct 24, 2014 1:35 pm   

Joined: Fri Oct 24, 2014 1:24 pm
Posts: 1
The purpose of this script is to send an incoming postfix message to two or more servers, the reasons for this are testing/backup for email, I currently run both an exchange server for learning and a Bluemind groupware server as well, while they are not used in production I'm still a little miffed that i cannot get this script to run in the way i intend. I modified the script from here http://pjrlost.blogspot.ca/2012/11/smtp-delivery-to-two-mail-servers-via.html. The original could not send to multiple recipients properly so i used my very poor knowledge of bash to fix it, however it seems that the use of getopt could greatly improve this script and still the folders that hold the queued mail seems to sit there forever and not get deleted after a successful mail sending. Anyways i would greatly appreciate some assistance with this as I think that this could be a very useful tool for those getting familiar with email servers.
Code:
#!/bin/bash

MSMTP="/usr/bin/msmtp"
TMPDIR="/var/tmp/smtpdd"
TMPFILENAME="mailqfile.$RANDOM.`date +%s`.$$"
LOCKFILE="$1/.smtpdd.lck"
RUNT=0

EX_TEMPFAIL=75

printUsage()
{
        echo "Usage: `basename $0` queue-directory sender recipient hostname|ip:port:mode [hostname|ip:port:mode ...]"
        echo "     : `basename $0` queue-directory qrun";
        echo "Where mode is q for queuing delivery, d for just drop it or o to just queue the mail - defaults to q"
}

attemptDelivery()
{
        qd=$1
        mail=$2
        host=`basename $1`

        from=`cat $mail|cut -f 1 -d " "`
        to=`cat $mail|cut -f 2 -d " "`

        hostname=`echo $host|cut -f 1 -d :`
        port=`echo $host|cut -f 2 -d :`

        if [ ! -f $mail.body ]
        then
                echo "Cannot delivery mail, mail body is missing"
                exit $EX_TEMPFAIL
        fi

        #echo "attmpting delivery of $2 from $1 as $from and $to to host $hostname with port $port"
        $MSMTP --host $hostname --port $port -f $from $to < $mail.body
        if [ $? == 0 ]
        then
                echo "Message delivered, deleting"
                rm -f $mail $mail.body
        else
                echo "Message delivery failed, leaving in place"
        fi
}

queueRun()
{
        for dirs in $1/*
        do
                if [ -d $dirs ]
                then
                        old_pwd=$PWD
                        cd $dirs
                        for mail in *.qf
                        do
                                if [ -f $mail ]
                                then
                                        attemptDelivery "$dirs" "$mail"
                                fi
                        done
                        cd $old_pwd
                fi
        done
}

mainRun()
{
        if [ ! -d $TMPDIR ]
        then
                mkdir $TMPDIR
                if [ $? != 0 ]
                then
                        echo "Tempdirectory configuration problem with $TMPDIR - cannot create directory"
                        logger -p mail.error -t smtpdd "Temp directory configuration problem with $TMPDIR - cannot create directory"
                        exit $EX_TEMPFAIL
                fi
        fi

        chmod 0700 $TMPDIR

        queuedir=$1
        from=$2
        to=$3

        if [[ $RUNT == 0 ]]
        then
                cat > $TMPDIR/$TMPFILENAME
                RUNT=1
        fi

        for host in ${@:4}
        do
                if [[ $host != *@* ]]
                then
                        #echo "attempting $2 to $3 for $host"

                        hostname=`echo $host|cut -f 1 -d :`
                        port=`echo $host|cut -f 2 -d :`
                        mode=`echo $host|cut -f 3 -d :`
                        if [ "x$mode" == "x" ]
                        then
                                mode="q"
                        fi

                        queueit="no"

                        logger -p mail.info -t smtpdd "Attempting delivery of mail from $2 to $3 for host $hostname on $port with mode of $mode"

                        if [ "$mode" != "o" ]
                        then
                                # attempt real delivery
                                $MSMTP --host $hostname --port $port -f $from $to < $TMPDIR/$TMPFILENAME > /dev/null 2>&1
                                RET=$?
                                if [ $RET != 0 ]
                                then
                                        if [ $RET != "75" ]
                                        then
                                                logger -p mail.info -t smtpdd "Mail delivery for $3 has failed"
                                                echo "Mail delivery for $3 has failed (unknown user?)"
                                                break
                                        fi
                                        # we failed to deliver ....
                                        # in queue mode, we deliver to the queue
                                        if [ "$mode" == "q" ]
                                        then
                                                #echo "Delivery of mail to $hostname on $port from $from to $to has failed, but will be queued ($RET)"
                                                logger -p mail.warning -t smtpdd "Delivery of mail to $hostname on $port from $from to $to has failed, but will be queued ($RET)"
                                                queueit="yes"
                                        fi

                                        # delete mode
                                        if [ "$mode" == "d" ]
                                        then
                                                logger -p mail.info -t smtpdd "Delivery of mail to $hostname on $port from $from to $to has failed and will not be re-tried (delete mode)"
                                                randomtext="asdf"
                                        fi
                                else
                                        logger -p mail.info -t smtpdd "Delivery of mail to $hostname on $port from $from to $to has succeeded"
                                        randomtext="asdf"
                                fi
                        else
                                echo "In queue only mode, will queue"
                                logger -p mail.info -t smtpdd "In queue-only mode, will queue mail until a queue run is performed"
                                queueit="yes"
                        fi

                        if [ $queueit == "yes" ]
                        then
                                echo "queueing"
                                mkdir -p $queuedir/$hostname:$port
                                i=0
                                while [ -f $queuedir/$hostname:$port/mailf.$$.$i.qf ]
                                do
                                        i=$(( $i + 1 ))
                                done
                                cp $TMPDIR/$TMPFILENAME $queuedir/$hostname:$port/mailf.$$.$i.qf.body
                                echo $from $to > $queuedir/$hostname:$port/mailf.$$.$i.qf
                        fi
                fi
        done

}


if [ "x$1" == "x" ]
then
        printUsage
        exit $EX_TEMPFAIL
fi

if [ ! -d $1 ]
then
        echo "queue-directory specified, $1, must exist"
        exit $EX_TEMPFAIL
fi

if ! touch $1/.fml
then
        echo "queue-directory specified, $1, must exist and be writable"
        exit $EX_TEMPFAIL
else
        rm -f $1/.fml
fi

chmod 0700 $1

exec 8>> $LOCKFILE

if [ "x$2" == "xqrun" ]
then
        if flock -n -e 8
        then
                queueRun "$1"
                exit 0
        else
                logger -p mail.warning -t smtpdd "Queue directory locked, must exit from qrun"
                exit $EX_TEMPFAIL
        fi
fi

if [ "x$4" == "x" ]
then
        printUsage
        exit $EX_TEMPFAIL
fi

if flock -n -s 8
then
        logger -p mail.info -t smtpdd "${@}"
        rno=3
        for recip in ${@:3}
        do
                if [[ $recip == *@* ]]
                then
                        mainRun $1 $2 $recip ${@:$rno}
                        rno=$rno+1
                fi
        done
        rm -f $TMPDIR/$TMPFILENAME
else
        logger -p mail.warning -t smtpdd "Cannot obtain shared lock - will not deliver right now"
        rm -rf $LOCKFILE
        exit $EX_TEMPFAIL
fi


Top
 Profile  
 PostPosted: Wed Oct 29, 2014 11:01 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 16
very poor code.
It uses POSIX statements, allbeit explicitly bash

The syntax
Code:
`someCommand`
is POSIX.
bash got a "better" one:
Code:
$( someCommand )


Next it is a bad idea to use var names all uppercase.
It is true, that all system vars and internal bash vars are uppercase.
Hence it is always better to not use uppercase var names in order to avoid accidentially overwriting system or internal vars. Use camel case or whatever, but avoid all uppercase.

One can use this very old getopts.
Coding it on your own is easier.
Code:
while (( $# > 0 )); do   # while number of args greater zero; do 
          # "(( ))" is an arithmetical expression; it compares the special bash var "#"
          #  with zero. The var "#" holds the count of actual args.
     
    case $1 in        # test argument number 1
       -h|--help  )   # is arg1 -h OR --help
                display_usage;  # call the appropriate function
                shift # and "shift away $1. i.e. -h gets deleted and $2 becomes $1
                      # shift 2  shifts away the first two args.
                      # and $3 becomes $1, $4 becomes $2 etc.
   esac
done
hth


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP