<?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>Amar Kulo &#187; perl</title>
	<atom:link href="http://blog.amarkulo.com/tag/perl/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.amarkulo.com</link>
	<description>System administration, photography and DIY projects</description>
	<lastBuildDate>Wed, 08 Sep 2010 10:43:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>How to build antispam SMTP gateway for Exchange on Ubuntu 8.04</title>
		<link>http://blog.amarkulo.com/how-to-build-spam-free-smtp-gateway-for-exchange-on-ubuntu-804</link>
		<comments>http://blog.amarkulo.com/how-to-build-spam-free-smtp-gateway-for-exchange-on-ubuntu-804#comments</comments>
		<pubDate>Sat, 13 Sep 2008 16:10:51 +0000</pubDate>
		<dc:creator>Amar Kulo</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tips and tricks]]></category>
		<category><![CDATA[8.04]]></category>
		<category><![CDATA[antivirus]]></category>
		<category><![CDATA[apt]]></category>
		<category><![CDATA[aptitude]]></category>
		<category><![CDATA[clam]]></category>
		<category><![CDATA[clamav]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[exchange]]></category>
		<category><![CDATA[gateway]]></category>
		<category><![CDATA[mail]]></category>
		<category><![CDATA[mailscanner]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[postfix]]></category>
		<category><![CDATA[relay]]></category>
		<category><![CDATA[scanner]]></category>
		<category><![CDATA[smtp]]></category>
		<category><![CDATA[spam]]></category>
		<category><![CDATA[spamassassin]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[virus]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://dzo.blogsite.org/?p=141</guid>
		<description><![CDATA[Because previous solution that I have done on Windows didn&#8217;t worked in way I wanted, I have decided to make dedicated Linux SMTP filtering gateway. Note: You can copy/paste all commands written here although they are not inline, they will be when you paste them to shell or file. For this I needed to have: [...]]]></description>
			<content:encoded><![CDATA[<p>Because previous solution that I have done on Windows didn&#8217;t worked in way I wanted, I have decided to make dedicated Linux SMTP filtering gateway.</p>
<p><strong>Note:</strong> You can copy/paste all commands written here although they are not inline, they will be when you paste them to shell or file.</p>
<p>For this I needed to have:</p>
<ul>
<li>Fully functional Linux machine ( I had already installed Ubuntu 8.04 server )</li>
<li>Postfix &#8211; default installation will do</li>
<li>Mail Scanner &#8211; free package that you can get on <a title="Mail scanner" href="http://mailscanner.info/" target="_blank">MailScanner home page</a></li>
<li>SpamAssassin &#8211; free antispam solution</li>
<li>Clam AV &#8211; free antivirus</li>
</ul>
<p>Before of installing anything we will fetch the latest list of packages from Ubuntu repositories with command:</p>
<blockquote><p><strong>aptitude update</strong></p></blockquote>
<p>Now before installation of a Mail Scanner we need to install some packages with command:<strong> </strong><strong></strong></p>
<blockquote><p><strong>aptitude </strong><strong>install libconvert-tnef-perl libdbd-sqlite3-perl libfilesys-df-perl libmailtools-perl libmime-tools-perl libmime-perl libnet-cidr-perl libsys-syslog-perl libio-stringy-perl libfile-temp-perl</strong> <strong>libole-storage-lite-perl libarchive-zip-perl postfix clamav clamav-daemon libsys-hostname-long-perl libnet-cidr-lite-perl spamassassin<br />
</strong></p></blockquote>
<p>Answer with Y when asked to install dependencies for these packages. During the installation of postfix you will get simple menu in which you need to select Internet site and write your full host name with domain.</p>
<p>Because whole configuration of Postfix is being done via main.cf and master.cf files we need to change some values in them. Open <strong>main.cf</strong> and add/edit following lines to look like this:</p>
<blockquote><p><strong> myhostname = ubuntu.sp.local<br />
mydomain = sp.local<br />
myorgin = $mydomain<br />
inet_interfaces = all<br />
mydestination = $myhostname, localhost.$mydomain $mydomain<br />
mynetwork_style = host<br />
relay_domains = lab.net<br />
transport_maps = hash:/etc/postfix/transport<br />
append_at_myorigin = no<br />
local_recipient_maps =<br />
header_checks = regexp:/etc/postfix/header_checks<br />
</strong></p></blockquote>
<p>Now open <strong>/etc/postfix/transport </strong>and add your forwarding domains in it. For multiple domains go to new row by same principle.</p>
<blockquote><p><strong>sp.local    smtp:[192.168.1.5]</strong></p></blockquote>
<p>This ip <strong>192.168.1.5</strong> is IP of my Exchange server,  not of my Linux machine and <strong>sp.local</strong> is the name of my domain, replace IP and domain name with yours. After this editing we need to create hash of the transport file and to restart Postfix.</p>
<blockquote><p><strong>postmap /etc/postfix/transport<br />
/etc/init.d/postfix restart</strong></p></blockquote>
<p>Now you can control if Postfix is working like it should and if it transports mail. If everything works stop it so that we can configure other options in it and because it needs to be started after MailScanner.</p>
<blockquote><p><strong>/etc/init.d/postfix stop<br />
</strong></p></blockquote>
<p>Next step would be to unpack and install the latest version of Mail Scanner because Ubuntu 8.04 still doesn&#8217;t have the latest version in the repositories.  You can get the latest version of Mail Scanner <a title="Mail scanner" href="http://mailscanner.info/downloads.html" target="_blank">here</a>. Because we will compile it we need .tar.gz version.</p>
<blockquote><p><strong>wget <a class="linkification-ext" title="Linkification: http://mailscanner.info/files/4/tar/MailScanner-install-4.71.10-1.tar.gz" href="http://mailscanner.info/files/4/tar/MailScanner-install-4.71.10-1.tar.gz">http://mailscanner.info/files/4/tar/MailScanner-install-4.71.10-1.tar.gz</a><br />
tar zxvf MailScanner-install-4.71.10-1.tar.gz<br />
cd MailScanner-install-4.71.10<br />
./install.sh</strong></p></blockquote>
<p>You will see lot of output when you run <strong>./install.sh</strong> because it will check which Perl modules do you have installed and install those that are missing, but as installation says:</p>
<blockquote><p><em>Do not worry too much about errors from the next command.<br />
It is quite likely that some of the Perl modules are<br />
already installed on your system.</em></p></blockquote>
<p>When <strong>install.sh </strong>is done you will get message to enter some lines in crontab to automatize jobs. Type <strong>crontab -e</strong> and enter following lines in crontab (:w to write and exit if you are using Vi editor ctrl+x for Pico).</p>
<blockquote><p><strong>37      5 * * * /opt/MailScanner/bin/update_phishing_sites<br />
07      * * * * /opt/MailScanner/bin/update_bad_phishing_sites<br />
58     23 * * * /opt/MailScanner/bin/clean.quarantine<br />
42      * * * * /opt/MailScanner/bin/update_virus_scanners<br />
3,23,43 * * * * /opt/MailScanner/bin/check_mailscanner</strong></p></blockquote>
<blockquote><p><strong></strong></p></blockquote>
<p>Now we need to edit <strong>/etc/default/spamassassin</strong> file and to enable SpamAssassin daemon. Open this file with editor and change these lines:</p>
<blockquote><p><strong>ENABLED=0 to ENABLED=1<br />
CRON=0 to CRON=1</strong></p></blockquote>
<p>Now we have enabled SpamAssassin as a daemon and enabled it to update daily via cron. It&#8217;s time to start it for the first time.</p>
<blockquote><p><strong>/etc/init.d/spamassassin start</strong></p></blockquote>
<p>Next step would be to edit MailScanner.conf. Find following lines and edit  them to look like this:</p>
<blockquote><p><strong>Run As User = postfix<br />
Run As Group = postfix<br />
MTA = postfix<br />
Incoming Queue Dir = /var/spool/postfix/hold<br />
Outgoing Queue Dir = /var/spool/postfix/incoming<br />
Virus Scanners = clamav<br />
Spam List = SBL+XBL<br />
SpamAssassin User State Dir = /var/spool/MailScanner/spamassassin<br />
</strong></p></blockquote>
<p>Now we will run MailScanner with command:</p>
<blockquote><p><strong>/opt/MailScanner/bin/check_mailscanner</strong></p></blockquote>
<p>It will automatically go to background. Now we need to tell Postfix to wait with forwarding of mails while they are checked against spam and antivirus definitions. We will need to edit file <strong>/etc/postfix/header_checks</strong> and to add following line because without it nothing will work.</p>
<blockquote><p><strong>/^Received:/ HOLD</strong></p></blockquote>
<p>Now we need to restart Postfix with command:</p>
<blockquote><p><strong>/etc/init.d/postfix start</strong></p></blockquote>
<p>I have got interesting output when I once killed MailScanner process that really make me laugh.</p>
<blockquote><p><strong>11818 ?        Ss     0:00 MailScanner: killing children, bwahaha!<br />
</strong>11819 ?        Z      0:01 [MailScanner] &lt;defunct&gt;<br />
11862 ?        Z      0:01 [MailScanner] &lt;defunct&gt;<br />
11905 ?        Z      0:01 [MailScanner] &lt;defunct&gt;<br />
11948 ?        Z      0:01 [MailScanner] &lt;defunct&gt;<br />
11991 ?        Z      0:01 [MailScanner] &lt;defunct&gt;<strong><br />
12038 ?        Ss     0:00 MailScanner: killing children, bwahaha!<br />
</strong>12039 ?        Z      0:01 [MailScanner] &lt;defunct&gt;<br />
12083 ?        Z      0:01 [MailScanner] &lt;defunct&gt;<br />
12126 ?        Z      0:01 [MailScanner] &lt;defunct&gt;<br />
12169 ?        Z      0:01 [MailScanner] &lt;defunct&gt;<br />
12212 ?        Z      0:01 [MailScanner] &lt;defunct&gt;</p></blockquote>
<p>LOL, they have a good sense of humor.</p>
<p>So if everything is working correctly you should receive now mails with small footer text:</p>
<blockquote><p><span style="color: #888888;">&#8211;<br />
This message has been scanned for viruses and<br />
dangerous content by <a href="http://www.mailscanner.info/" target="_blank"><strong>MailScanner</strong></a>, and is<br />
believed to be clean. </span></p></blockquote>
<p>You can control message delivery with command:</p>
<blockquote><p><strong>tail -f /var/log/syslog</strong></p></blockquote>
<p>There you will see scrolling text like:</p>
<blockquote><p>Sep 11 10:14:14 ubuntu postfix/smtpd[13220]: connect from web54408.mail.yahoo.com[206.190.49.138]<br />
Sep 11 10:14:14 ubuntu postfix/smtpd[13220]: CE433E643: client=web54408.mail.yahoo.com[206.190.49.138]<br />
Sep 11 10:14:15 ubuntu postfix/cleanup[13222]: CE433E643: hold: header Received: from web54408.mail.yahoo.com (web54408.mail.yahoo.com [206.190.49.138])??by ubuntu.sp.local (Postfix) with SMTP id CE433E643??for &lt;<strong>mymail@mydomain</strong>&gt;; Thu, 11 Sep 2008 10:14:14 -0400 (EDT) from web54408.mail.yahoo.com[206.190.49.138]; from=&lt;<strong>my_yahoo_mail</strong>@yahoo.com&gt; to=&lt;<strong><a class="linkification-ext" title="Linkification: mailto:mymail@mydomain.com" href="mailto:mymail@mydomain.com">mymail@mydomain.com</a></strong>&gt; proto=SMTP helo=&lt;web54408.mail.yahoo.com&gt;<br />
Sep 11 10:14:15 ubuntu postfix/cleanup[13222]: CE433E643: message-id=&lt;<a class="linkification-ext" title="Linkification: mailto:225121.37413.qm@web54408.mail.yahoo.com" href="mailto:225121.37413.qm@web54408.mail.yahoo.com">225121.37413.qm@web54408.mail.yahoo.com</a>&gt;<br />
Sep 11 10:14:15 ubuntu postfix/smtpd[13220]: disconnect from web54408.mail.yahoo.com[206.190.49.138]<br />
Sep 11 10:14:15 ubuntu MailScanner[13211]: New Batch: Scanning 1 messages, 2392 bytes<br />
Sep 11 10:14:16 ubuntu MailScanner[13211]: Virus and Content Scanning: Starting<br />
Sep 11 10:14:20 ubuntu MailScanner[13211]: Requeue: CE433E643.352A5 to 424A5E650<br />
Sep 11 10:14:20 ubuntu postfix/qmgr[13003]: 424A5E650: from=&lt;<strong>my_yahoo_mail</strong>@yahoo.com&gt;, size=1712, nrcpt=1 (queue active)<br />
Sep 11 10:14:20 ubuntu postfix/smtp[13245]: 424A5E650: to=&lt;<strong><a class="linkification-ext" title="Linkification: mailto:mymail@mydomain.com" href="mailto:mymail@mydomain.com">mymail@mydomain.com</a></strong>&gt;, relay=192.168.1.5[192.168.1.5]:25, delay=5.6, delays=5.5/0.01/0/0.15, dsn=2.6.0, status=sent (250 2.6.0  &lt;<a class="linkification-ext" title="Linkification: mailto:225121.37413.qm@web54408.mail.yahoo.com" href="mailto:225121.37413.qm@web54408.mail.yahoo.com">225121.37413.qm@web54408.mail.yahoo.com</a>&gt; Queued mail for delivery)<br />
Sep 11 10:14:20 ubuntu postfix/qmgr[13003]: 424A5E650: removed<br />
Sep 11 10:14:20 ubuntu <strong>MailScanner[13211]:</strong> <strong>Uninfected: Delivered 1 messages</strong></p></blockquote>
<p>This means that MailScanner is scanning messages against spam and viruses.</p>
<p>Now the latest step would be to create RC script to start MailScanner on boot and before Postfix. <a title="Mailscanner bash script" href="http://blog.amarkulo.com/wp-content/uploads/2008/09/mailscanner.txt" target="_blank">Here</a> you can read/download file <a title="Mailscanner bash script" href="http://blog.amarkulo.com/wp-content/uploads/2008/09/mailscanner.txt" target="_blank"><strong>mailscanner.txt</strong></a> file which you will use to create new <strong>/etc/init.d/mailscanner</strong> file.</p>
<p>This script is slightly modified original init script for MailScanner from Ubuntu. Next step would be to change permissions to the file and to create symlinks with commands:</p>
<blockquote><p><strong>chmod 755 /etc/init.d/mailscanner<br />
cd /etc/rc0.d/<br />
ln -s ../init.d/mailscanner K20mailscanner</strong></p>
<p><strong>cd /etc/rc1.d/<br />
ln -s ../init.d/mailscanner K20mailscanner</strong></p>
<p><strong>cd /etc/rc2.d/<br />
ln -s ../init.d/mailscanner S20mailscanner</strong></p>
<p><strong>cd /etc/rc3.d/<br />
ln -s ../init.d/mailscanner S20mailscanner</strong></p>
<p><strong>cd /etc/rc4.d/<br />
ln -s ../init.d/mailscanner S20mailscanner</strong></p>
<p><strong>cd /etc/rc5.d/<br />
ln -s ../init.d/mailscanner S20mailscanner</strong></p>
<p><strong>cd /etc/rc6.d/<br />
ln -s ../init.d/mailscanner K20mailscanner<br />
</strong></p></blockquote>
<p>So now we need to test everything with commands:</p>
<blockquote><p><strong>/etc/init.d/postfix stop<br />
/etc/init.d/mailscanner start<br />
/etc/init.d/postfix start</strong></p></blockquote>
<p><strong>Notes</strong>:</p>
<p>If SpamAssassin doesn&#8217;t detect some of your messages as spam although they are spam you can easy manually learn it to detect that and other similar messages as spam. Follow next few steps:</p>
<ol>
<li>Find that message and save it to text file including headers and content</li>
<li>Transfer file to your linux machine</li>
<li>From the directory where you have transfered file run command: <strong>spamassassin &#8211;spam &lt; your_text_file</strong></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://blog.amarkulo.com/how-to-build-spam-free-smtp-gateway-for-exchange-on-ubuntu-804/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Defending Exchange server against spam with SpamAssassin</title>
		<link>http://blog.amarkulo.com/defending-exchange-server-against-spam-with-spamassassin</link>
		<comments>http://blog.amarkulo.com/defending-exchange-server-against-spam-with-spamassassin#comments</comments>
		<pubDate>Wed, 10 Sep 2008 17:28:22 +0000</pubDate>
		<dc:creator>Amar Kulo</dc:creator>
				<category><![CDATA[Tips and tricks]]></category>
		<category><![CDATA[esa]]></category>
		<category><![CDATA[esa sink]]></category>
		<category><![CDATA[exchange]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[mail]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[serial]]></category>
		<category><![CDATA[sink]]></category>
		<category><![CDATA[smtp]]></category>
		<category><![CDATA[spam]]></category>
		<category><![CDATA[spamassassin]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://dzo.blogsite.org/?p=139</guid>
		<description><![CDATA[This short howto is written and done on Windows 2003 Server and Exchange 2003 Server. Because lately I (among other users) have begun to receive large number of spam on e-mail server of the company where I work, I have decided to implement SpamAssassin which is primary made for Linux. I have searched little bit [...]]]></description>
			<content:encoded><![CDATA[<p>This short howto is written and done on Windows 2003 Server and Exchange 2003 Server.</p>
<p>Because lately I (among other users) have begun to receive large number of spam on e-mail server of the company where I work, I have decided to implement SpamAssassin which is primary made for Linux.</p>
<p>I have searched little bit on the net how to do it and found <a title="ESA Sink" href="http://www.christopherlewis.com/ESA/ExchangeSpamAssassin.htm" target="_blank">ESA Sink</a> written by Christopher G. Lewis. It works on the principle that SpamAssasin is working in serial mode (it scans one message at a time) and ESA Sink take incoming message from Exchange, outputs it to file, run that file through SpamAssasin and returns it to Exchange if it is OK.</p>
<p>Installation and configuration of everything is not that much complicated. It can be divided in following steps:</p>
<ol>
<li>Download and install the latest version of Active Perl from <a title="Active State" href="http://www.activestate.com/" target="_blank">Active State</a></li>
<li>Download <a title="NMake" href="http://download.microsoft.com/download/vc15/Patch/1.52/W95/EN-US/Nmake15.exe" target="_blank">NMake</a> from Microsoft and after extracting move files to c:\perl\bin</li>
<li>Download <a title="ESA Sink" href="http://www.christopherlewis.com/ESA/ExchangeSpamAssassin.htm" target="_blank">ESA Sink</a> from <a title="Christopher G. Lewis" href="http://www.christopherlewis.com/" target="_blank">Chris&#8217;s site</a></li>
<li>After installation of ESA Sink you will get <strong>ExchangeSpamassassin.ini </strong>file opened</li>
<li>Check config file values, change if necessary, I have changed MaxMsgSizeKB from -1 to 2048 = 2MB and leave it opened for later as well as installation</li>
<li>Now start command prompt, Start-&gt;Run-&gt;Cmd and enter ppm to start Perl Package Manager</li>
<li>If you receive message that ppm is not recognizable type <strong>set path=%path%;c:\perl\bin\</strong> and then <strong>ppm</strong></li>
<li>Now in PPM press<strong> Ctrl+1</strong> to see list of available perl modules for installation</li>
<li>Type <strong>Net-Dns</strong> in search field and when you get this package in list click with right mouse button on it and select install</li>
<li>Now we will do same thing for <strong>IO-Socket-INET6, IP-Country</strong> and <strong>Mail-SPF</strong></li>
<li>Now press <strong>Ctrl+Enter</strong> to install these packages and when it is done you can close <strong>PPM</strong></li>
<li>Download the latest version of <a title="SpamAssassin" href="http://spamassassin.apache.org/" target="_blank">SpamAssassin</a> and unzip it to c:\Mail-SpamAssassin-3.2.5 (3.2.5=current version that you are downloading, in my case 3.2.5)</li>
<li>In the command prompt now type cd c:\Mail-SpamAssassin-3.2.5</li>
<li>Type following command: perl makefile.pl and answer questions before compiling</li>
<li>Now we need to run nmake, so just type <strong>nmake /i </strong>(/i means that we want nmake to compile application ignoring errors)</li>
<li>After compiling we need to run <strong>nmake /i install</strong> to install compiled files</li>
<li>Unset read-only attribute from <strong>c:\perl\site\bin\spamassassin.bat </strong>file</li>
<li>Now to test SpamAssassin we will enter in command prompt to directory <strong>C:\Mail-SpamAssassin-3.2.5\t\data\spam </strong>and execute command <strong>spamassassin -D &lt; 001</strong></li>
<li>If you see lot of text on your command prompt it means that SpamAssassin works like it should and on the end of output you should find scoring for content of file <strong>001</strong></li>
<li>Now we will return to <strong>.ini</strong> file from ESA setup and change line <strong>SpamAssassin_Batch_File </strong>to points to the location of our <strong>spamassassin.bat</strong> file which is <strong>c:\perl\site\bin\spamassassin.bat</strong></li>
<li>Save and close file and finish the ESA installation</li>
<li>At the end of the installation you will leave checkbox to run ESA install script and click Finish button</li>
<li>Small command prompt window will come and leave as soon as ESA is installed</li>
<li>Now to see if it is working properly you can check <strong>C:\ESA\Ham</strong> or <strong>C:\ESA\Spam</strong> directories for files and you can send e-mail to yourself as well, if everything works you should find SpamAssassin information in header of the e-mail</li>
<li>To be sure that you don&#8217;t have any errors you can check C:\ESA\Log directory in which you should find log file with errors</li>
</ol>
<p>This configuration works OK on servers that don&#8217;t receive more than 1500-2000 e-mail per day, but I wouldn&#8217;t recommend it for higher number of e-mails without very strong machine, because in this configuration SpamAssassin works in serial mode, it process one object at a time and this includes downloading of e-mail to file, control against filters, control against URBL lists and then if everything is ok returning it to server for delivery.</p>
<p>For me it was simple test to see if it will work for us, it didn&#8217;t worked because of large number of e-mails that we receive (10 000+ per day) so I will go on solution to create Linux SpamAssassin gateway.</p>
<p>Note: I would recomend creating of small .bat script that will delete .out files older than 7 days to avoid running out of disk on your system disk</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.amarkulo.com/defending-exchange-server-against-spam-with-spamassassin/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
