Installing Mailscrubber on Windows

Table of Contents

About

Todo:
  • Change sa-learn to run as "NetworkService"
  • Package up files

Install Perl

Download the most recent version of the ActivePerl Windows MSI installer from the ActiveState website: http://www.activestate.com/Products/Download/Download.plex?id=ActivePerl. At the time of writing, ActivePerl 5.8.6 is the most recent version.

Double click on the file and follow the installation instructions. Use the default options.

Install cygwin

Download and install cygwin by running http://cygwin.com/setup.exe.

When asked to select a mirror, ftp.easynet.be tends to be a fast one.

Use the default install options except when selecting packages. Mmake sure to include:gcc, automake, autoconf, make, exim, less, an editor (nano/emacs/vi/vim/etc), openssh, cron, mutt, curl-devel, openssl-devel, gd, libgd-devel

add c:\cygwin\bin to the path.
(right click on my computer, properties, advanced, environment variables, system, edit "PATH")

set "CYGWIN=ntsec tty" as a system environment variable.
(right click on my computer, properties, advanced, environment variables, system, new)

set EDITOR and VISUAL to be the name of the editor you installed above as system environment variables.

Enable SYSTEM access

Enable login as the SYSTEM user to the system so that we can access files and run programs as the SYSTEM User. Open a cygwin shell and type:
mkdir /home/system && chown SYSTEM /home/system
change the SYSTEM line in /etc/passwd to:
SYSTEM:*:18:18:Local System,U-COMPUTERNAME\Administrator,S-1-5-18:/home/system:/bin/bash

Enable the SSH server

Run the config program from a Cygwin shell by typing:
ssh-host-config
Answer yes to all of the yes/no questions.

Give a password for the sshd_server user that meets system password requirements

When prompted for what to set the CYGWIN variable to, type "ntsec tty"

Start up the service by typing:
net start sshd

Install SpamAssassin

(Based on instructions from: http://www.openhandhome.com/howtosa260.html)

First download and install the components needed by SpamAssassin. Get nmake from ftp://ftp.microsoft.com/Softlib/MSLFILES/nmake15.exe

Double click on this file and extract the file to C:\perl\bin

Then install the necessary perl modules from ppm by typing:
ppm3 install Net::DNS DB_File Time::HiRes
Now go to http://www.spamassassin.org, choose Download, and get the ZIP file distribution.

Unzip the file to C:\
Open a windows shell and type:
cd c:\spamassassin-*\
nmake
nmake install
Tell spamassasisn about your DNS server by adding "SET RES_NAMESERVERS=mydnsserver to
c:\perl\bin\spamassassin.bat
c:\perl\bin\sa-learn.bat

Then disable razor, pyzor, and dcc in c:\cygwin\etc\mail\spamassassin\local.cf because they don't work on windows yet:
use_razor2 0
use_dcc 0
use_pyzor 0

Configure the environment

Open up a cygwin shell and type in the following commands to make the folder for SpamAssasin to learn.
mkdir -p /home/is_spam/.maildir
mkdir -p /home/not_spam/.maildir
chmod -R 777 /home/is_spam/
chmod -R 777 /home/not_spam/

Install custom scripts

in /home/system/headermangle.pl:
#!/usr/bin/perl -w
#headermangle.pl

# This is supposed to extract the header bits we don't want and toss them,
# and save the relevant ones for some future purpose.

use strict;
local ($/); #sets the record separator built-in variable to null rather than '\n';

my $debug = '0';
my ($subject, $from, $replyto, $to, $date) = "";

my $message = <>; # now we slurp the entire file into the scalar $message

# then we look to see if it's been forwarded or not,
# next rip out all the X- whatnot headers,
# then pull out the From: and Reply to: if they're there
# Last just spew the mail out after the original message delimiter


$message =~ /----- Original Message -----.*\n|Begin forwarded .*\n/;

if ($') { my $newmessage = $';                  # grab everything after the "Original..."
                $newmessage =~ s/X-.*\n//g;     # rip out all the previous X-header crap
                 # get other relevant headers in here.
                 # Do we need to account for ">" in some forwarded messages,
                 # is ">" ever in front of the headers we want? (probably not?)

                getbasicheaders ();

                print "$newmessage";
                if ($debug) {   print "\n(New message)\n";
                                print "It was sent to $to, on $date,\nallegedly from $from,\nabout \"$subject\"\n";
                }
} else { $message =~ s/X-.*\n//g;

                getbasicheaders ();
                print "$message";
                if ($debug) {   print "\n(Original message)\n";
                }
}

sub getbasicheaders {
                if ($message =~ /Subject: (.*)/ ) { $subject = $1; }
                if ($message =~ /From: (.*)/ ) { $from = $1; }
                if ($message =~ /Reply to: (.*)/ ) { $replyto = $1; }
                if ($message =~ /To: (.*)/ ) { $to = $1; }
                if ($message =~ /Sent: (.*)/ ) { $date = $1; }
}
        
in /home/system/sa_learner.pl:
#!C:\perl\bin\perl -w
#sa_learner.pl

use strict;
use CWD;

my $spamdir    = "C:/cygwin/home/is_spam/.maildir/new";
my $hamdir     = "C:/cygwin/home/not_spam/.maildir/new";
my $storedir      = "C:/cygwin/home/system/mail";
my $bindir = "C:/cygwin/home/system/";

chdir($spamdir);
foreach my $file (<*>) {
    if (-f $file) {
        system("perl $bindir/headermangle.pl < $file > $storedir/spam/$file");
        system("del $file");
    }
}

chdir($hamdir);
foreach my $file (<*>) {
    if (-f $file) {
        system("perl $bindir/headermangle.pl < $file > $storedir/ham/$file");
        system("del $file");
    }
}

system("sa-learn --spam $storedir\\spam");
system("sa-learn --ham $storedir\\ham");
        

Configure Exim

edit /etc/exim.conf
        local_interfaces = <; ip_address
        domainlist local_domains = mailscrubber.example.com
        domainlist relay_to_domains = example.com
        #comment out verify = recipitent in +relay_to_domains acl
        #add after domain_literal router:
        spamcheck_router:
                no_verify
                check_local_user
                condition = "${if and { {!def:h_X-Spam-Flag:} {!eq {$received_protocol}{spam-scanned}}} {1}{0}}"
                driver = accept
                transport = spamcheck
        #add after remote_smtp transport:
        spamcheck:
                debug_print = "T: spamassassin_pope for $local_part@$domain"
                driver = pipe
                command = /usr/bin/exim -oMr spam-scanned -bS
                use_bsmtp
                headers_add = "X-Spam-Scanned: pumpaudio mailscrubber"
                transport_filter = C:/Perl/bin/spamassassin.bat
                home_directory = "/tmp"
                current_directory = "/tmp"
                user = Administrator
                group = Administrators
                # return_fail_output
                message_prefix =
                message_suffix =

        #edit the localPdelivery transport to look like:
        local_delivery:
                driver = appendfile
                # group = mail
                mode = 0660
                mode_fail_narrower = false
                envelope_to_add = true
                return_path_add = true
                directory = /home/${local_part}/.maildir/
                maildir_format
        

Schedule the task

Schedule task with windows to run perl.exe sa_learner.pl at a desired interval.

Start->Programs->Accessories->System Tools->Scheduled Tasks
Double Click on "Add Scheduled Task"
Click on "Browse" and browse to Perl->bin->Perl.exe, then click "Next"
Name the task "SpamAssassin Learn"
Choose to run it "Daily" and click "Next"
Perform the task every day, set a time for this, click "Next"
Enter in your user information so it can run as you, click "Next"
Check the box to "Open advanced properties" and click "Next"
In the box labeled "run", change the text to: "C:\Perl\bin\perl.exe C:\cygwin\home\system\sa_learner.pl" and click "Apply" then "Ok".

Configure the admin site in IIS

Make a new folder: C:\Mailscrubber\ and C:\Mailscrubber\admin\
Start -> programs -> administrative tools -> internet information systems (IIS) manager
Select the name of your server in the pane on the left and click the + next to it
Click on Web Service Extensions
You should see Perl ISAPI Extension and Perl CGI Extension on the right.
Right click on each one of them and click "allowed"
Then right click on "Web Sites" on the left pane and click on New -> Website.
Go through the wizard answering questions with the following information:
-Description: mailscrubber.example.com
-Host header for this website: mailscrubber.example.com
-Path: C:\Mailscrubber\admin\
-Uncheck Allow anonymous access
-Check the box next to "Execute"
The wizard should be done and a new site should show up on the left.
Right click on "mailscrubber.example.com" and click on properties.
On the "Directory Security" Tab, under "Authentication and access control", click "Edit"
Uncheck "Enable anonymous access"
Enable other kinds of authenticated access as needed
Click ok
Under the "Documents" tab, click the add button and add "index.pl" to the list
Click on index.pl in the list and click "Move up" until it is at the top.
Click Apply, and then ok. Your site is now ready

Install the admin GUI

Change the permissions on C:\Perl\site\etc\mail\spamassassin\local.cf so that apache has write access to it.

Edit C:\Perl\site\etc\mail\spamassassin\local.cf to:
report Mailscrubber has identifed the attached message as spam.
report If this is not spam, please forward it to
report not_spam@mailscrubber.example.com. If you have any questions,
report see _CONTACTADDRESS_ for details.
header Checker-Version : SpamAssassin _VERSION_ (_SUBVERSION_) on _HOSTNAME_ : all
header Flag : _YESNOCAPS_ : spam
header Level : _STARS(*)_ : all
header Status : "_YESNO_, score=_SCORE_ required=_REQD_ tests=_TESTS_ autolearn=_AUTOLEARN_ version=_VERSION_" : all
rewrite_header Subject ****SPAM****
required_score 5
Then install the provided files into C:\Mailscrubber\admin\ and access them through your web browser at http://mailscrubber.example.com/.

comments powered by Disqus