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, the second one is powered by Exchange within AD. qmail server also serves as a mailing list server hosting 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):

  1. #!/bin/sh
  2. if [ ! -f validrcptto.txt ]
  3. then
  4.  touch validrcptto.txt
  5. fi
  7. /var/qmail/bin/adexport | grep 'org$' | sort > /var/qmail/control/
  9. if ! diff -q /var/qmail/control/validrcptto.txt /var/qmail/control/ > /dev/null 2>&1
  11. then
  12.  echo changed
  14. /var/qmail/bin/mkvalidrcptto >> /var/qmail/control/
  15.  cdbmake-12 /var/qmail/control/validrcptto.tmp /var/qmail/control/validrcptto.$$ < /var/qmail/control/
  16.  chmod 644 /var/qmail/control/validrcptto.tmp
  17.  mv /var/qmail/control/validrcptto.tmp /var/qmail/control/validrcptto.cdb
  18.  mv /var/qmail/control/ /var/qmail/control/validrcptto.txt
  19. else
  20.  echo no change
  21.  rm
  23. fi

Then, I realized that if Exchange, for whatever reasons, goes down we will be in a bad situation because generated by adexport would be empty and since we update it with mkvalidrcptto at the last stage qmail will only accept recipients. The fact that Exchange went down doesn’t mean that we should reject 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:

  1. #!/bin/sh
  3. /var/qmail/bin/adexport | grep 'org$' | sort > /var/qmail/control/
  5. if test -s /var/qmail/control/
  6. then
  7.  echo changed
  9.  /var/qmail/bin/mkvalidrcptto >> /var/qmail/control/
  10.  cdbmake-12 /var/qmail/control/validrcptto.tmp /var/qmail/control/validrcptto.$$ < /var/qmail/control/
  11.  chmod 644 /var/qmail/control/validrcptto.tmp
  12.  mv /var/qmail/control/validrcptto.tmp /var/qmail/control/validrcptto.cdb
  14.  mv /var/qmail/control/ /var/qmail/control/validrcptto.txt
  15. else
  16.  echo oops, seems like exchange is down
  18. fi

Note “test -s” check – this was added to ensure that 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.

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

Tags: , , , ,

Leave a Reply