<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>108.bz &#187; Postfix</title>
	<atom:link href="http://www.108.bz/posts/tag/postfix/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.108.bz</link>
	<description>Wandering futilities...</description>
	<lastBuildDate>Fri, 27 May 2011 09:08:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>Conditional address rewriting with Postfix</title>
		<link>http://www.108.bz/posts/it/conditional-address-rewriting-with-postfix/</link>
		<comments>http://www.108.bz/posts/it/conditional-address-rewriting-with-postfix/#comments</comments>
		<pubDate>Tue, 13 Apr 2010 20:35:49 +0000</pubDate>
		<dc:creator>Giuliano</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Postfix]]></category>

		<guid isPermaLink="false">http://www.108.bz/?p=416</guid>
		<description><![CDATA[Today I had the need to (automatically) rewrite sender addresses of an email depending on the recipient domain. A way to trick Postfix into applying a sort of &#8220;conditional masquerading&#8221;. Postfix rewriting tables are just static key &#8594; value dictionaries: they&#8217;re used to lookup B given A, but there&#8217;s no available logic to cope with [...]]]></description>
			<content:encoded><![CDATA[<p>Today I had the need to (automatically) rewrite <i>sender</i> addresses of an email depending on the <i>recipient</i> domain. A way to trick Postfix into applying a sort of &#8220;conditional masquerading&#8221;. Postfix rewriting tables are just static key &rarr; value dictionaries: they&#8217;re used to lookup B given A, but there&#8217;s no available logic to cope with more complicated patterns.<br />
A little more context to help me explain: I&#8217;m talking about a monitoring system. Alert emails are generated by Nagios and handed to a local Postfix on the same server. And here are the rules to implement:</p>
<ul>
<li>A locally generated email whose destination is <i>inside</i> the company, should leave Postfix with a <i>@FQDN</i> suffix (<i>@hostname.localdomain.lan</i>) in its sender addresses. Sender addresses shouldn&#8217;t be rewritten/masqueraded at all.</li>
<li>A locally generated email whose destination is <i>outside</i> of the company, needs to be masquerated, its sender addresses rewritten as <i>@extdomain.com</i> .</li>
</ul>
<p>Moreover, but that&#8217;s a routing matter rather than a rewriting one:</p>
<ul>
<li>Emails directed to <i>@smsgw.localdomain.lan</i> have to be relayed through a different mail server. </li>
</ul>
<p>As you can see, the logic is: lookup B (rewritten sender) given A (sender) depending on C (recipient).</p>
<p>I found the right hint deeply <a href="http://thread.gmane.org/gmane.mail.postfix.user/125861/focus=125866">buried</a> in Postfix&#8217;s mailing list: check out Noel Jones post, kudos to him.</p>
<ul>
<li>First, define a new <i>smtp</i> transport in &#8220;master.cf&#8221;; just copy/paste the existing one and change the first column to whatever name you like. We are explicitly telling the new transport that it will use its own <a href="http://www.postfix.org/ADDRESS_REWRITING_README.html#generic">generic</a> <i>regexp</i> map (the <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">-o</span> command-line option).</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">[root@hostname postfix]# cd /etc/postfix<br />
[root@hostname postfix]# grep '^\(smtp\|toext\).*unix' master.cf <br />
smtp &nbsp; &nbsp; &nbsp;unix &nbsp;- &nbsp; &nbsp; &nbsp; - &nbsp; &nbsp; &nbsp; n &nbsp; &nbsp; &nbsp; - &nbsp; &nbsp; &nbsp; - &nbsp; &nbsp; &nbsp; smtp<br />
toext &nbsp; &nbsp; unix &nbsp;- &nbsp; &nbsp; &nbsp; - &nbsp; &nbsp; &nbsp; n &nbsp; &nbsp; &nbsp; - &nbsp; &nbsp; &nbsp; - &nbsp; &nbsp; &nbsp; smtp -o smtp_generic_maps=regexp:/etc/postfix/generic_toext</div></div>
</li>
<li>We also need to take control over the mail routing mechanism. This is done by enabling <a href="http://www.postfix.org/transport.5.html">transport</a> maps.
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">[root@hostname postfix]# grep ^transport main.cf<br />
transport_maps = regexp:/etc/postfix/transport</div></div>
</li>
<li>Transport maps (remember that they&#8217;re matched against <i>From</i> addresses) are configured in order to:
<ul>
<li>Route mail that should be delivered locally through the <i>local</i> transport. This will preserve <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">/etc/aliases</span> and <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">.forward</span> behaviour and make everything act like you expect on Unix.</li>
<li>Route mail to @smsgw.localdomain.lan, via its dedicated gateway, using the &#8220;standard&#8221; <i>smtp</i> transport.</li>
<li>Route mail to @localdomain.lan, through the main SMTP gateway, using the <i>smtp</i> transport.</li>
<li>Route any other message through the main SMTP gateway, <i>but</i> use our custom transport.</li>
</ul>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">[root@hostname postfix]# tail -4 transport<br />
/@hostname\.localdomain\.lan$/ &nbsp;local:hostname.localdomain.lan<br />
/@smsgw\.localdomain\.lan$/ &nbsp; &nbsp; smtp:[smsgw.localdomain.lan]<br />
/@localdomain\.lan/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; smtp:[gateway.localdomain.lan]<br />
/@.*$/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;toext:[gateway.localdomain.lan]</div></div>
</li>
<li>The custom transport&#8217;s <i>generic</i> map rewrites recipient adresses, shortening the FQDN by preserving just the domain name, and changing the address part before the @ sign. Hostname is being stripped but I still want to be able to tell, at a glance, from where the message originates. When they leave the mail system, rewritten addresses look like <i>username</i>-<i>hostname</i>@<i>extdomain</i>.<i>com</i> .
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">[root@hostname postfix]# cat generic_toext<br />
/^(.*)@hostname\.localdomain\.lan$/ $1-hostname@extdomain.com</div></div>
</li>
</ul>
 <img src="http://www.108.bz/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=416" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://www.108.bz/posts/it/conditional-address-rewriting-with-postfix/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

