Saturday 9 March 2013

Getting Sendmail Relaying Everything He Doesn't Know About

Sendmail is a really powerful email tool but if like me it isn't always the final delivery point in your organisation it can be quite tricky to setup, this is especially true if you'd still like sendmail to be the delivery point for some of your emails (those for mailing lists, scripts, bit buckets etc where sendmail is so handy).

I basically wanted all email domains that I didn't host on this sendmail server  (i.e. not @my.domain) to go to an external relay host. And any email addressed @my.domain and unqualified addresses should be treated the same and go through the aliasing process then delivered to another server.

So I wanted:

blah@other.domain -> relay host

blah -> aliased or if nothing to alias or result of aliasing -> relay host as blah@my.domain (name could have been changed by aliasing)

blah@my.domain -> aliased or if nothing to alias or result of aliasing -> relay host as blah@my.domain (name could have been changed by aliasing)

This is surprisingly tricky. I searched for a while about how to do this and found a few approaches.

This guys approach of using a modified nullclient got me close:
http://brandonhutchinson.com/wiki/Nullclient_with_alias_processing

As I remember though this failed for me on the blah@my.domain I could never get to alias.

The correct approach seemed to be to set,

define(`SMART_HOST', `exchange.my.domain')dnl
define(`LOCAL_RELAY', `exchange.my.domain')dnl

provided masquerading is setup properly, this worked for all cases for me with one fatal flaw, if an address was unqualified the LOCAL_RELAY option sends the email onto the relay as blah@exchange.my.domain not blah@my.domain. In our case Exchange and then that just gets rejected.

I struggled with this for a while. Other's have modified LUSER_RELAY so that it takes in the domain name you'd like to append rather than adding the LOCAL_RELAY name. Such as here:

http://www.jmaimon.com/sendmail/anfi.homeunix.net/sendmail/relaycd.html

I quite liked this but didn't want to maintain modified base distributed m4 files that might get overwritten by OS upgrades.

So I took the approach of creating a copy of the relay delivery agent (with a new name (modrelay), but I then added a single rule to MasqSMTP (and created a new rule name for this (MasqSMTPTwist) and a copy of all it's lines below my new one) to remove the relay name from the email address and replace it with the preferred domain name. So I added this to the bottom of my sendmail.mc file:


LOCAL_RULE_0
Mmodrelay,              P=[IPC], F=mDFMuXa8, S=EnvFromSMTP/HdrFromSMTP, R=MasqSM
TPTwist, E=\r\n, L=2040,
                T=DNS/RFC822/SMTP,
                A=TCP $h


LOCAL_RULESETS
SMasqSMTPTwist
R$* < @ exchange.my.domain > $*              $@ $1 < @ my.domain > $2
                already fully qualified
R$* < @ $* > $*         $@ $1 < @ $2 > $3               already fully qualified
R$+                     $@ $1 < @ *LOCAL* >             add local qualification


Then I called this from LOCAL_RELAY by having "modrelay:" in front of the relay's name e.g.


define(`SMART_HOST', `exchange.my.domain')dnl
define(`LOCAL_RELAY', `modrelay:exchange.my.domain')dnl


It's a little bit messy but works for me.


No comments:

Post a Comment