August 1st, 2010 by jde
Postfix Tutorial – Step 7
Spam and virus detection
AMaViS (A Mail Virus Scanner) gives you protection against spam through Spamassassin and virus protection with Clamav (optional):
Let’s install AMaViS:
# cd /usr/ports/security/amavisd-new/
# make install clean
Options for amavisd-new 2.6.4_7,1
[X] BDB Use BerkeleyDB for nanny/cache/snmp
[ ] SNMP Install amavisd snmp subagent
[ ] SQLITE Use SQLite for lookups
[X] MYSQL Use MySQL for lookups/logging/quarantine
[ ] PGSQL Use PgSQL for lookups/logging/quarantine
[ ] LDAP Use LDAP for lookups
[ ] SASL Use SASL authentication
[X] MILTER Sendmail milter support
[X] SPAMASSASSIN Use mail/p5-Mail-SpamAssassin
[ ] P0F Passive operating system fingerprinting
[ ] ALTERMIME Use AlterMime for defanging/disclaimers
[X] FILE Use newer file(1) utility from port
[ ] RAR RAR support with archivers/rar
[X] UNRAR RAR support with archivers/unrar
[X] ARJ ARJ support with archivers/arj
[ ] UNARJ ARJ support with archivers/unarj
[X] LHA LHA support with archivers/lha
[X] ARC ARC support with archivers/arc
[ ] NOMARCH ARC support with archivers/nomarch
[X] CAB CAB support with archivers/cabextract
[X] RPM RPM support with archivers/rpm2cpio
[X] ZOO ZOO support with archivers/zoo
[ ] UNZOO ZOO support with archivers/unzoo
[X] LZOP LZOP support with archivers/lzop
[X] FREEZE FREEZE support with archivers/freeze
[X] P7ZIP P7ZIP support with archivers/p7zip
[X] MSWORD Ms Word support with textproc/ripole
[ ] TNEF Add external tnef decoder converters/tnef
Some time during the installation, you will also be prompted for SpamAssassin options:
Options for p5-Mail-SpamAssassin 3.3.1
[X] AS_ROOT Run spamd as root (recommended)
[X] SPAMC Build spamd/spamc (not for amavisd)
[ ] SACOMPILE sa-compile
[X] DKIM DKIM/DomainKeys Identified Mail
[X] SSL Build with SSL support for spamd/spamc
[X] GNUPG Install GnuPG (for sa-update)
[X] MYSQL Add MySQL support
[ ] PGSQL Add PostreSQL support
[X] RAZOR Add Vipul's Razor support
[ ] SPF_QUERY Add SPF query support
[ ] RELAY_COUNTRY Relay country support
[ ] DCC Add DCC support (see LICENSE)
Install Clamav:
# cd /usr/ports/clamav/
# make install clean
Options for clamav 0.96.1
[X] ARC Enable arch archives support
[X] ARJ Enable arj archives support
[X] LHA Enable lha archives support
[X] UNZOO Enable zoo archives support
[X] UNRAR Enable rar archives support
[X] LLVM Enable JIT Bytecode compiler
[ ] MILTER Compile the milter interface
[ ] LDAP libmilter was built with LDAP
[ ] ICONV Enable ICONV support
[ ] STDERR Print logs to stderr instead of stdout
[ ] EXPERIMENTAL Build experimental code
In /etc/rc.conf:
amavis_milter_enable="YES"
amavisd_enable="YES"
clamav_clamd_enable="YES"
clamav_freshclam_enable="YES"
spamd_enable="YES"
Run sa-update to download configuration files (rules, scores, etc,) for SpamAssassin (Read more…), then start AMaViS and make sure it starts up correctly:
# rehash
# sa-update
# /usr/local/etc/rc.d/amavisd start
Starting amavisd.
# ps -ax | grep amavis
55743 ?? Ss 0:02.77 amavisd (master) (perl)
55744 ?? I 0:00.01 amavisd (virgin child) (perl)
55745 ?? I 0:00.01 amavisd (virgin child) (perl)
# netstat -p tcp -a | grep 10024
tcp4 0 0 localhost.10024 *.* LISTEN
Note that the startup command for AMaViS may fail silently, so if you don’t see it listening consult /var/log/maillog.
In /usr/local/etc/postfix/main.cf add these lines:
# Amavis content filtering
content_filter=smtp-amavis:[127.0.0.1]:10024
receive_override_options=no_address_mappings
In /usr/local/etc/postfix/master.cf add these lines:
smtp-amavis unix - - n - 2 smtp
-o smtp_data_done_timeout=1200
-o smtp_send_xforward_command=yes
-o disable_dns_lookups=yes
-o max_use=20
127.0.0.1:10025 inet n - - - - smtpd
-o content_filter=
-o local_recipient_maps=
-o relay_recipient_maps=
-o smtpd_restriction_classes=
-o smtpd_delay_reject=no
-o smtpd_client_restrictions=permit_mynetworks,reject
-o smtpd_helo_restrictions=
-o smtpd_sender_restrictions=
-o smtpd_recipient_restrictions=permit_mynetworks,reject
-o smtpd_data_restrictions=reject_unauth_pipelining
-o smtpd_end_of_data_restrictions=
-o mynetworks=127.0.0.0/8
-o smtpd_error_sleep_time=0
-o smtpd_soft_error_limit=1001
-o smtpd_hard_error_limit=1000
-o smtpd_client_connection_count_limit=0
-o smtpd_client_connection_rate_limit=0
-o receive_override_options=no_header_body_checks,no_unknown_recipient_checks
-o local_header_rewrite_clients=
Reload Postfix:
# postfix reload
postfix/postfix-script: refreshing the Postfix mail system
Add clamav user to the vscan group in /etc/group:
Find these lines:
vscan:*:110:
clamav:*:106:
Change them to:
vscan:*:110:clamav
clamav:*:106:vscan
Start clamav (be patient, it may take a while before you get the prompt back):
# /usr/local/etc/rc.d/clamav-clamd start
Starting clamav_clamd.
LibClamAV Warning: **************************************************
LibClamAV Warning: *** The virus database is older than 7 days! ***
LibClamAV Warning: *** Please update it as soon as possible. ***
LibClamAV Warning: **************************************************
You can ignore this warning since clamav-freshclam will take care of that from now on. Let’s start it:
# /usr/local/etc/rc.d/clamav-freshclam start
Open /usr/local/etc/amavisd.conf and locate the @lookup_sql_dsn section. Edit it so that it looks like this:
@lookup_sql_dsn = (
['DBI:mysql:database=postfix;host=127.0.0.1;port=3306',
'postfix',
'password']);
$sql_select_policy = 'SELECT domain as name FROM domain WHERE CONCAT("@",domain) IN (%k)';
Also locate the @av_scanners section, and make sure clamav is uncommented:
### http://www.clamav.net/
['ClamAV-clamd',
\&ask_daemon, ["CONTSCAN {}\n", "/var/run/clamav/clamd.sock"],
qr/\bOK$/m, qr/\bFOUND$/m,
qr/^.*?: (?!Infected Archive)(.*) FOUND$/m ],
Also tell AMaViS to let spam pass … It will be deliveret to the users inbox and then each user can set their own “Sieve” options.
Whatever you do, do not set $final_spam_destiny to D_BOUNCE. Spam is never sent from the spammers own email address, to you’ll just end up annoying (innocent) people.
$final_spam_destiny = D_PASS;
$spam_quarantine_to = undef;
Restart AMaViS:
# /usr/local/etc/rc.d/amavisd restart
Stopping amavisd.
Waiting for PIDS: 55743 55744 55745, 55743.
Starting amavisd.
Test AMaViS spam detection with this message: spamassassin.apache.org/gtube/gtube.txt
Replace my@examplemail.com with an existing mail account on your server.
# fetch http://spamassassin.apache.org/gtube/gtube.txt
# sendmail my@examplemail.com < gtube.txt
Your maillog should look something like this:
postfix/pickup[59535]: 13CF27CD: uid=0 from=<root>
postfix/cleanup[59586]: 13CF27CD: message-id=<GTUBE1.1010101@example.net>
postfix/qmgr[59173]: 13CF27CD: from=<root@examplemail.com>, size=938, nrcpt=1 (queue active)
postfix/smtpd[59590]: connect from unknown[127.0.0.1]
postfix/smtpd[59590]: 28DC17C8: client=unknown[127.0.0.1]
postfix/cleanup[59586]: 28DC17C8: message-id=<GTUBE1.1010101@example.net>
postfix/qmgr[59173]: 28DC17C8: from=<root@examplemail.com>, size=1673, nrcpt=1 (queue active)
amavis[59204]: (59204-01) Passed SPAM, <root@examplemail.com> -> <my@examplemail.com>, Message-ID: <GTUBE1.1010101@example.net>, mail_id: x1U2T7du28CF, Hits: 1005.069, size: 938, queued_as: 28DC17C8, 42885 ms
postfix/smtp[59588]: 13CF27CD: to=<my@examplemail.com>, relay=127.0.0.1[127.0.0.1]:10024, delay=50, delays=0.62/0.26/11/38, dsn=2.0.0, status=sent (250 2.0.0 from MTA([127.0.0.1]:10025): 250 2.0.0 Ok: queued as 28DC17C8)
postfix/qmgr[59173]: 13CF27CD: removed
postfix/pipe[59592]: 28DC17C8: to=<my@examplemail.com>, relay=dovecot, delay=3, delays=0.17/0.27/0/2.6, dsn=2.0.0, status=sent (delivered via dovecot service)
postfix/qmgr[59173]: 28DC17C8: removed