validrcptto & merging two different recipient sources

Today I’ve been approached by one of our projects with the request to build qmail’s validrcptto.cdb from two different sources.

They use two email servers: the first one, powered by qmail and patched with great patch set by John Simpson, is providing SMTP relay services including virus/spam scanning for domain.org, the second one is powered by Exchange within AD. qmail server also serves as a mailing list server hosting lists.domain.org subdomain.

The idea is the following. First, get the list of valid recipients from Exchange server by using the script called adexport written by Brian Landers wrapped by John Simpson’s adexport-go script. Then merge it with the local list generated by John Simpson’s mkvalidrcptto script and finally build validrcptto.cdb.

Looks like a quite trivial task with all these handy scripts and wrappers made by John Simpson, so I ended up with the following adexport-go script (I omitted copyrights section, but no doubts all credits go to John):

#!/bin/sh
if [ ! -f validrcptto.txt ]
then
 touch validrcptto.txt
fi

/var/qmail/bin/adexport | grep 'org$' | sort > /var/qmail/control/validrcptto.new

if ! diff -q /var/qmail/control/validrcptto.txt /var/qmail/control/validrcptto.new > /dev/null 2>&1

then
 echo changed

/var/qmail/bin/mkvalidrcptto >> /var/qmail/control/validrcptto.new
 cdbmake-12 /var/qmail/control/validrcptto.tmp /var/qmail/control/validrcptto.$$ < /var/qmail/control/validrcptto.new
 chmod 644 /var/qmail/control/validrcptto.tmp
 mv /var/qmail/control/validrcptto.tmp /var/qmail/control/validrcptto.cdb
 mv /var/qmail/control/validrcptto.new /var/qmail/control/validrcptto.txt
else
 echo no change
 rm validrcptto.new

fi

Then, I realized that if Exchange, for whatever reasons, goes down we will be in a bad situation because validrcptto.new generated by adexport would be empty and since we update it with mkvalidrcptto at the last stage qmail will only accept lists.domain.org recipients. The fact that Exchange went down doesn't mean that we should reject domain.org recipients - qmail should accept and queue messages and that was the reason why it was put there. That brings the idea of getting some sort of assurance that the list we are receiving from Exchange is not empty, and if it is then we should stop building validrcptto.cdb and rely on the previous successful build.

After some twikking I changed adexport-go script to the following:

#!/bin/sh

/var/qmail/bin/adexport | grep 'org$' | sort > /var/qmail/control/validrcptto.new

if test -s /var/qmail/control/validrcptto.new 
then
 echo changed

 /var/qmail/bin/mkvalidrcptto >> /var/qmail/control/validrcptto.new
 cdbmake-12 /var/qmail/control/validrcptto.tmp /var/qmail/control/validrcptto.$$ < /var/qmail/control/validrcptto.new
 chmod 644 /var/qmail/control/validrcptto.tmp
 mv /var/qmail/control/validrcptto.tmp /var/qmail/control/validrcptto.cdb

 mv /var/qmail/control/validrcptto.new /var/qmail/control/validrcptto.txt
else
 echo oops, seems like exchange is down

fi

Note "test -s" check - this was added to ensure that validrcptto.new received from Exchange exists and has a size greater than zero. Check man test for more information.

That's it. I suspect that this is not the most efficient solution, but hey, it works.

PS: And yes, finally here is an entry in crontab - I fetch recipients every hour, from 8am till 7pm, from Monday till Friday.

0       8-19    *       *       1-5     root    /var/qmail/bin/adexport-go

Tags: , , , ,

Leave a Reply