<?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>johker&#039;s blog &#187; crontab</title>
	<atom:link href="http://johker.ibutho.de/tag/crontab/feed/" rel="self" type="application/rss+xml" />
	<link>http://johker.ibutho.de</link>
	<description>stories about me, my life and my trips</description>
	<lastBuildDate>Sun, 18 Dec 2011 11:53:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>fetchmail Integration in postfixadmin</title>
		<link>http://johker.ibutho.de/2009/04/07/fetchmail-integration-in-postfixadmin/</link>
		<comments>http://johker.ibutho.de/2009/04/07/fetchmail-integration-in-postfixadmin/#comments</comments>
		<pubDate>Tue, 07 Apr 2009 21:26:34 +0000</pubDate>
		<dc:creator>johker</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tipps und Tricks]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[cronjob]]></category>
		<category><![CDATA[crontab]]></category>
		<category><![CDATA[fetchmail]]></category>
		<category><![CDATA[installation]]></category>
		<category><![CDATA[konfiguration]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[postfixadmin]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://johker.ibutho.de/?p=227</guid>
		<description><![CDATA[Viele Benutzer haben auch Mailboxen bei anderen Anbietern, z.B. bei gmx oder web.de. Damit diese Benutzer ihre E-Mails aggregieren können, wird fetchmail eingerichtet. fetchmail ist bereits in postfixadmin integriert, somit können fetchmail-Einträge über postfixadmin verwaltet werden (derzeit leider nur von &#8230; <a href="http://johker.ibutho.de/2009/04/07/fetchmail-integration-in-postfixadmin/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Viele Benutzer haben auch Mailboxen bei anderen Anbietern, z.B. bei gmx oder web.de. Damit diese Benutzer ihre E-Mails aggregieren können, wird <em>fetchmail</em> eingerichtet. <em>fetchmail</em> ist bereits in <em>postfixadmin</em> integriert, somit können <em>fetchmail</em>-Einträge über <em>postfixadmin</em> verwaltet werden (derzeit leider nur von Administratoren). Über das mitgelieferte <em>fetchmail.pl</em>-Skript werden die Daten aus der Datenbank ausgelesen, <em>fetchmail</em> aufgerufen, die Mails durch <em>amavisd-new</em> überprüft und an die Mailboxen der Benutzer ausgeliefert. Das <em>perl</em>-Skript wurde etwas angepasst, damit es mit <em>postgres</em> interagieren kann. Des Weiteren wurden die Dateinamen etwas angepasst.</p>
<p>Installation:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">aptitude</span> <span style="color: #c20cb9; font-weight: bold;">install</span> fetchmail
<span style="color: #c20cb9; font-weight: bold;">aptitude</span> <span style="color: #c20cb9; font-weight: bold;">install</span> liblockfile-simple-perl</pre></div></div>

<p>Nötige Verzeichnisse für das Skript anlegen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>run<span style="color: #000000; font-weight: bold;">/</span>fetchmail
<span style="color: #c20cb9; font-weight: bold;">touch</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>run<span style="color: #000000; font-weight: bold;">/</span>fetchmail<span style="color: #000000; font-weight: bold;">/</span>fetchmail-all.lock</pre></div></div>

<p>Das Skript:</p>
<p><em>fetchmail.pl</em></p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> DBI<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> MIME<span style="color: #339933;">::</span><span style="color: #006600;">Base64</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;"># use Data::Dumper;</span>
<span style="color: #000000; font-weight: bold;">use</span> File<span style="color: #339933;">::</span><span style="color: #006600;">Temp</span> <span style="color: #009966; font-style: italic;">qw/ mkstemp /</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Sys<span style="color: #339933;">::</span><span style="color: #006600;">Syslog</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;"># require liblockfile-simple-perl</span>
<span style="color: #000000; font-weight: bold;">use</span> LockFile<span style="color: #339933;">::</span><span style="color: #006600;">Simple</span> <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span>lock trylock unlock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
openlog<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;fetchmail-all&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;pid&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;mail&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> log_and_die <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">my</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$message</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
  syslog<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;err&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">die</span> <span style="color: #0000ff;">$message</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># read options and arguments</span>
&nbsp;
<span style="color: #0000ff;">$configfile</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;/etc/fetchmail/config&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #0000ff;">@ARGS1</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #000000; font-weight: bold;">ARGV</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span> <span style="color: #0000ff;">@ARGS1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^-/</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^--config$/</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #0000ff;">$configfile</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span> <span style="color: #0000ff;">@ARGS1</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># postgres settings</span>
<span style="color: #0000ff;">$database</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;postfix&quot;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$hostname</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;127.0.0.1&quot;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$user</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;postfix&quot;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$password</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;XXXXXX&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #0000ff;">$run_dir</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;/var/run/fetchmail&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># use specified config file</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>e <span style="color: #0000ff;">$configfile</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">do</span> <span style="color: #0000ff;">$configfile</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">$dsn</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;DBI:Pg:database=$database;host=$hostname&quot;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$lock_file</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$run_dir</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">&quot;/fetchmail-all.lock&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #0000ff;">$lockmgr</span> <span style="color: #339933;">=</span> LockFile<span style="color: #339933;">::</span><span style="color: #006600;">Simple</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">make</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>autoclean <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span>max <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$lockmgr</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">lock</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$lock_file</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> log_and_die <span style="color: #ff0000;">&quot;can't lock ${lock_file}&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#postgres connect</span>
<span style="color: #0000ff;">$dbh</span> <span style="color: #339933;">=</span> DBI<span style="color: #339933;">-&gt;</span><span style="color: #006600;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dsn</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$user</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$password</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> log_and_die <span style="color: #ff0000;">&quot;cannot connect the database&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #0000ff;">$sql</span><span style="color: #339933;">=</span><span style="color: #cc0000; font-style: italic;">&lt;&lt;SQL;
SELECT id,mailbox,src_server,src_auth,src_user,src_password,src_folder,fetchall,keep,protocol,mda,extra_options,usessl 
FROM fetchmail
WHERE date_part('epoch',now())-date_part('epoch',date) &gt; poll_time*60
SQL</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">%config</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">map</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$id</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$mailbox</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$src_server</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$src_auth</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$src_user</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$src_password</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$src_folder</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$fetchall</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$keep</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$protocol</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$mda</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$extra_options</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$usessl</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=@</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">;</span>
&nbsp;
  syslog<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;info&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;fetch ${src_user}@${src_server} for ${mailbox}&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #0000ff;">$cmd</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;user '${src_user}' there with password '&quot;</span><span style="color: #339933;">.</span>decode_base64<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$src_password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot;'&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #0000ff;">$cmd</span><span style="color: #339933;">.=</span><span style="color: #ff0000;">&quot; folder '${src_folder}'&quot;</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$src_folder</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #0000ff;">$cmd</span><span style="color: #339933;">.=</span><span style="color: #ff0000;">&quot; mda &quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$mda</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$mda</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#       $cmd.=&quot; mda \&quot;/usr/local/libexec/dovecot/deliver -m ${mailbox}\&quot;&quot;;</span>
        <span style="color: #0000ff;">$cmd</span><span style="color: #339933;">.=</span><span style="color: #ff0000;">&quot; is '${mailbox}' here&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #0000ff;">$cmd</span><span style="color: #339933;">.=</span><span style="color: #ff0000;">&quot; keep&quot;</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$keep</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #0000ff;">$cmd</span><span style="color: #339933;">.=</span><span style="color: #ff0000;">&quot; fetchall&quot;</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$fetchall</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #0000ff;">$cmd</span><span style="color: #339933;">.=</span><span style="color: #ff0000;">&quot; ssl&quot;</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$usessl</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #0000ff;">$cmd</span><span style="color: #339933;">.=</span><span style="color: #ff0000;">&quot; &quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$extra_options</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$extra_options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #0000ff;">$text</span><span style="color: #339933;">=</span><span style="color: #cc0000; font-style: italic;">&lt;&lt;TXT;
set postmaster &quot;postmaster&quot;
set nobouncemail
set no spambounce
set properties &quot;&quot;
set syslog
&nbsp;
poll ${src_server} with proto ${protocol}
        $cmd
&nbsp;
TXT</span>
&nbsp;
  <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$file_handler</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$filename</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> mkstemp<span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;/tmp/fetchmail-all-XXXXX&quot;</span> <span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> log_and_die <span style="color: #ff0000;">&quot;cannot open/create fetchmail temp file&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">print</span> <span style="color: #0000ff;">$file_handler</span> <span style="color: #0000ff;">$text</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">close</span> <span style="color: #0000ff;">$file_handler</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #0000ff;">$ret</span><span style="color: #339933;">=</span><span style="color: #ff0000;">`/usr/bin/fetchmail -f $filename -i $run_dir/fetchmail.pid`</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066;">unlink</span> <span style="color: #0000ff;">$filename</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #0000ff;">$sql</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;UPDATE fetchmail SET returned_text=&quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">quote</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$ret</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot;, date=now() WHERE id=&quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$id</span><span style="color: #339933;">;</span>
  <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">do</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">@</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">selectall_arrayref</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #0000ff;">$lockmgr</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">unlock</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$lock_file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
closelog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Damit das Skript regelmäßig ausgeführt wird, legen wir einen Cronjob dafür an (als Benutzer <strong>root</strong>). In der hier gezeigten Konfiguration wird das Skript alle 5 Minuten ausgeführt.</p>
<p><em>crontab -e</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">*/</span><span style="color: #000000;">5</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">/</span>PFAD<span style="color: #000000; font-weight: bold;">/</span>ZU<span style="color: #000000; font-weight: bold;">/</span>fetchmail.pl <span style="color: #000000; font-weight: bold;">&amp;&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://johker.ibutho.de/2009/04/07/fetchmail-integration-in-postfixadmin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

