Archive for the ‘IT / Tech’ Category

SpamAssassin: Dealing with unrecognized spam

Saturday, May 3rd, 2008

Everyone hates spam, and one of the main ways that people are fighting it is through the use of SpamAssassin. I’ve been using it for a while now and have Sieve detecting spam headers and moving them to my Junk folder.

The Problem

Dealing with spam that went unrecognized has been more of a manual process. Every once in a while, I’d have to segregate all of my useful mail from the spam and run “sa-learn” on my the leftovers. This isn’t horrible, because I tend to shell into my server fairly frequently, but I really prefer to have menial tasks like this automated.

A solution

First of all, I created a folder in my mailbox called “Unrecognized Spam”. The name isn’t important, really. It just needs to be a place to file away all of those messages that SpamAssassin didn’t catch on the way in.

Once that was done, I wrote a very simple little script, which I dropped in /etc/cron.daily/:

#!/bin/bash
 
SPAM_DIR="/home/bshacklett/Maildir/Unrecognized Spam/cur"
 
cd "$SPAM_DIR"
sa-learn --spam .;
rm *

Nasty, I know, but it did the job. All I had to do when I got spam that went unnoticed by SpamAssassin was drag it into my “Unrecognized Spam folder” and it would be learned and gone within 24 hours. Of course, I was also getting mail from the cron daemon complaining when there weren’t any emails to learn from or delete.

Improvements

So, this morning I had a little spare time, so I decided to improve on the script a bit:

#!/bin/bash
 
# Constants
SPAM_PATH="Maildir/.Unrecognized Spam/cur";
 
# Find all of the directories directly under /home/
homeDirectories=(`find /home/ -maxdepth 1 -mindepth 1 -type d`);
 
# Loop through the found directories and check for spam
for homeDirectory in ${homeDirectories[*]}
do
    fullSpamPath=${homeDirectory}|>/${SPAM_PATH}|>;
 
    #Check if the spam directory exists under this home directory
    if [ -d  "${fullSpamPath}" ]; then
 
        # Check if there is mail under the spam directory
        if [ "$( ls -A "${fullSpamPath}|>" )" ]; then
            sa-learn --spam "$fullSpamPath";
            rm "${fullSpammPath}/"*;
        fi
    fi
 
done

Now I know I’m not a great shell scripter, but this is working pretty well. It basically scans all of the home directories and looks for the “Unrecognized Spam” directory under each one. If it finds it, it will test to make sure that there are emails in the folder, then learn them and remove them.

Caveats

  • This isn’t going to scale all that well. I’m guessing it would be fine for 200 users or less, as it runs at night, but it would need some tweaking for anything more.
  • As it is, this requires that your mail be stored in the Maildir format. I know that sa-learn can work with mBox stores, but I’m not sure how you’d target it effectively.

Server Upgrade

Sunday, April 27th, 2008

So, now that Hardy Heron is out, I’ve gone ahead and upgraded my server. It took a little while, but overall it went quite smoothly. Having the ability to go back and forth between the old server image and the new server image made it a much easier experience. Big thanks to Linode and virtualization.

Changes

One large change I made to my configuration is using Dovecot’s sasl authentication for Postfix rather than Cyrus. Dovecot really seems to be making headway in the mail server market. I strongly reccomend it to anyone looking for a decent IMAP server.

I’ve also upgraded to Wordpress 2.5 and a later version of Roundcube for webmail. Unfortunately, Ubuntu seems to be holding Roundcube back a bit in their package repository. I’m not sure what the holdup is, but they’re at least one version behind at the time I’m writing this. For that matter, Wordpress is a couple of releases behind as well. I guess that happens with an LTS release.

Todo

I’m still having a bit of trouble getting Wordpress’ permalinks working correctly, so you’ll probably notice that some of the links aren’t working properly on the blog. I remember having this problem in the past, but, for the life of me, I can’t remember what I did to solve it.

Update: Got the permalink issue taken care of. Apache must be told, with the “AllowOverride” parameter, to allow .htaccess files. If this isn’t done, they will be ignored.

The move to a new host

Sunday, May 6th, 2007

Well, I’ve finally done it. Last night I made the final DNS changes and I’m up and running with my very own linode. It took some time to decide on which mail servers to use and then get everything configured correctly, but it was most definitely worth it.

Hosting is costing me $20.00 a month for a full virtual colocated server running my linux distribution of choice (Ubuntu) and easy access to all of the tools that I need to manage it. I’m paying $20.00 a year for a DNS solution from DynDNS. Linode.com could do it all for me, but I have a server at my house that needs dynamic DNS updates and DynDNS was the best option for me.

So, for anyone curious, I’m running Apache2 with PHP5 and MySQL plus Apache Tomcat for Java support. On the mail side of things, I’ve got Postfix handling my SMTP needs and Dovecot serving up IMAP (POP will soon be disabled). I’m running Postgrey for spam controls which, coupled with slightly paranoid settings in Postfix, is keeping spam level at a very good level. Definitely not as low as when I was using boxtrapper, but for an automatic solution that is less vulnerable to exploits I can hardly complain. I will, however, be looking into some active blacklists soon as well. I have RoundCube installed for web access to my emails and of course WordPress which you’re reading from right now.

Please bear with me on the default WordPress theme for the time being. I’ve got some ideas for a new design, but I haven’t had the time to implement them. I’ll be working on it as soon as I’m finished with a few more pressing projects.