<?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>gnodde.org &#187; Systeembeheer</title>
	<atom:link href="http://gnodde.org/category/systeembeheer/feed" rel="self" type="application/rss+xml" />
	<link>http://gnodde.org</link>
	<description>De zin of de onzin, dat is de vraag</description>
	<lastBuildDate>Mon, 26 Jul 2010 13:52:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Postfix Relay Recipient Maps</title>
		<link>http://gnodde.org/2009/01/13/postfix-relay-recipient-maps.html</link>
		<comments>http://gnodde.org/2009/01/13/postfix-relay-recipient-maps.html#comments</comments>
		<pubDate>Tue, 13 Jan 2009 22:51:26 +0000</pubDate>
		<dc:creator>Peter Gnodde</dc:creator>
				<category><![CDATA[Systeembeheer]]></category>

		<guid isPermaLink="false">http://gnodde.org/?p=55</guid>
		<description><![CDATA[A friend of mine, whose server acts as secondary mailserver for my domains, suggested I read http://macnugget.org/projects/postfixrelaymaps/ and proposed to implement it to reduce spam by refusing mail for non-existing users. For the specific ideas I recommend you read the article.
The solution offered did not match my situation, so I had to modify it a [...]]]></description>
			<content:encoded><![CDATA[<p>A <a title="Michiel's weblog" href="http://elfstone.nl/" target="_blank">friend</a> of mine, whose server acts as secondary mailserver for my domains, suggested I read <a title="Postfix Relay Recipients Maps by David McNett" href="http://macnugget.org/projects/postfixrelaymaps/" target="_blank">http://macnugget.org/projects/postfixrelaymaps/</a> and proposed to implement it to reduce spam by refusing mail for non-existing users. For the specific ideas I recommend you read the article.</p>
<p>The solution offered did not match my situation, so I had to modify it a bit. The first problem was that the original script parses only <code>/etc/passwd</code> and the aliasfiles of <a title="Postfix" href="http://www.postfix.org/" target="_blank">Postfix</a> itself. I also have virtual aliases and multiple domains, so I modified it to parse those too, you can download it <a title="parse_relays.pl" href="http://gnodde.org/dl/parse_relays.pl.gz">here (parse_relays.pl.gz)</a>. At that moment I was thinking about how to automate it further.</p>
<p>The original script uses <code>scp</code> to transfer a generated file and the database generated from that to the secondary mailserver. It checked if there were any changes before doing that though, this meant that if I were to automate that, it would be with a <code>crontab</code> and thus polling. Luckily there are more efficient methods in Linux: <a title="Inotify" href="http://en.wikipedia.org/wiki/Inotify" target="_blank">inotify</a>.</p>
<p>Inotify is a kernel-subsystem which can watch files for events, like opening, writing and closing and it does this without polling! I installed <a title="inotify-tools" href="http://inotify-tools.sourceforge.net/" target="_blank">inotify-tools</a> and use the <code>inotifywait</code> tool to watch a couple of files, if they are altered the shellscript continues running and will parse the files and send the result to the secondary mailserver, where a new database will be generated.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
&nbsp;
<span style="color: #007800;">FILES</span>=<span style="color: #ff0000;">&quot;/etc/passwd /etc/aliases.db /etc/postfix/aliases.db <span style="color: #000099; font-weight: bold;">\
</span>        /etc/postfix/virtual.db /etc/postfix/domains&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #000000; font-weight: bold;">in</span>
    start<span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Starting Postfix Relay updater.&quot;</span>
        <span style="color: #000000; font-weight: bold;">/</span>root<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>update_relays.sh <span style="color: #000000; font-weight: bold;">&amp;</span>
        <span style="color: #7a0874; font-weight: bold;">exit</span>
        <span style="color: #000000; font-weight: bold;">;;</span>
    stop<span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #7a0874; font-weight: bold;">exit</span>
        <span style="color: #000000; font-weight: bold;">;;</span>
<span style="color: #000000; font-weight: bold;">esac</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">true</span>; <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #666666; font-style: italic;"># Wait for filemodifications</span>
    inotifywait <span style="color: #660033;">-qqe</span> MODIFY <span style="color: #800000;">${FILES}</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Sleep half a second to prevent bashing</span>
    <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">0.5</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Send the file to the secondary mailserver</span>
    <span style="color: #000000; font-weight: bold;">/</span>root<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>parse_relays.pl <span style="color: #000000; font-weight: bold;">|</span>
        <span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #660033;">-qTi</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>postfix_relay user<span style="color: #000000; font-weight: bold;">@</span>mail2.example.org
<span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

<p>To automate the transfer to the secondary mailserver, I generated a ssh-key on the primary (<code>ssh-keyget -t dsa -f /path/to/key</code>). The public-key part of this key should be transfered to the secondary mailserver and placed in <code>~/.ssh/authorized_keys</code>, prepended by something like <code>from="1.2.3.4",command="/home/user/bin/receive_relays.sh",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty</code>.</p>
<p><code>receive_relays.sh</code> is executed at the moment an ssh-connection comes in which authenticates itself with the generated key. It places everything from <code>stdin</code> in <code>~/.relay/relay_example_org</code> and runs <code>postmap</code> on it.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
&nbsp;
<span style="color: #007800;">RELAY_FILE</span>=<span style="color: #ff0000;">&quot;/home/user/.relay/relay_example_org&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Update the file</span>
<span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #800000;">${RELAY_FILE}</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Create the Postfix database</span>
<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>postmap <span style="color: #800000;">${RELAY_FILE}</span></pre></div></div>

<p>If Postfix is configured correctly (see the part about <code>relay_recipient_maps</code> in above article) all should be well and completely automated!</p>
]]></content:encoded>
			<wfw:commentRss>http://gnodde.org/2009/01/13/postfix-relay-recipient-maps.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
