Friday, October 3, 2014

Configure spamassassin with postfix and dovecot

1- configure mail server, follow the following link

http://theamdara.blogspot.com/2014/09/how-to-configure-email-with-virtual.html

2- Add spam filter to mail server using spamassassin
 2.1 install necessary packages
root@emailserver# apt-get install spamassassin spamc dovecot-sieve
 2.2 add spamassassin rule to /etc/postfix/master.cf

smtp      inet  n       -       -       -       -       smtpd
   -o content_filter=spamassassin

submission inet n       -       -       -       -       smtpd
  -o syslog_name=postfix/submission
  -o smtpd_sasl_auth_enable=yes
pickup    fifo  n       -       -       60      1       pickup
cleanup   unix  n       -       -       -       0       cleanup
qmgr      fifo  n       -       n       300     1       qmgr
tlsmgr    unix  -       -       -       1000?   1       tlsmgr
rewrite   unix  -       -       -       -       -       trivial-rewrite
bounce    unix  -       -       -       -       0       bounce
defer     unix  -       -       -       -       0       bounce
trace     unix  -       -       -       -       0       bounce
verify    unix  -       -       -       -       1       verify
flush     unix  n       -       -       1000?   0       flush
proxymap  unix  -       -       n       -       -       proxymap
proxywrite unix -       -       n       -       1       proxymap
smtp      unix  -       -       -       -       -       smtp
relay     unix  -       -       -       -       -       smtp
showq     unix  n       -       -       -       -       showq
error     unix  -       -       -       -       -       error
retry     unix  -       -       -       -       -       error
discard   unix  -       -       -       -       -       discard
local     unix  -       n       n       -       -       local
virtual   unix  -       n       n       -       -       virtual
lmtp      unix  -       -       -       -       -       lmtp
anvil     unix  -       -       -       -       1       anvil
scache    unix  -       -       -       -       1       scache
maildrop  unix  -       n       n       -       -       pipe
  flags=DRhu user=vmail argv=/usr/bin/maildrop -d ${recipient}
uucp      unix  -       n       n       -       -       pipe
  flags=Fqhu user=uucp argv=uux -r -n -z -a$sender - $nexthop!rmail ($recipient)
ifmail    unix  -       n       n       -       -       pipe
  flags=F user=ftn argv=/usr/lib/ifmail/ifmail -r $nexthop ($recipient)
bsmtp     unix  -       n       n       -       -       pipe
  flags=Fq. user=bsmtp argv=/usr/lib/bsmtp/bsmtp -t$nexthop -f$sender $recipient
scalemail-backend unix  -       n       n       -       2       pipe
  flags=R user=scalemail argv=/usr/lib/scalemail/bin/scalemail-store ${nexthop} ${user} ${extension}
mailman   unix  -       n       n       -       -       pipe
  flags=FR user=list argv=/usr/lib/mailman/bin/postfix-to-mailman.py
  ${nexthop} ${user}
spamassassin
        unix - n        n - - pipe
      user=spamd argv=/usr/bin/spamc -f -e /usr/sbin/sendmail -oi -f ${sender} ${recipient}



2.3 edit /etc/default/spamassassin to the following
ENABLED=1
SPAMD_HOME="/home/spamd/"
OPTIONS="--create-prefs --max-children 5 --username spamd --helper-home-dir ${SPAMD_HOME} -s ${SPAMD_HOME}spamd.log"
PIDFILE="${SPAMD_HOME}spamd.pid"
CRON=1


2.4 edit /etc/spamassassin/local.cf to the following
 rewrite_header Subject *****SPAM*****
 report_safe 0
 required_score 5.0
 use_bayes 1
 bayes_auto_learn 1
skip_rbl_checks 0
use_razor2 0
use_dcc 0
use_pyzor 0



2.5 allow plugins in dovecot lmtp by editing /etc/dovecot/conf.d/20-lmtp.conf to
protocol lmtp {
  mail_plugins = $mail_plugins sieve
}

2.6 configure dovecot sieve by editing /etc/dovecot/conf.d/20-lmtp.conf
plugin {
  sieve = ~/.dovecot.sieve
  sieve_dir = ~/sieve
  sieve_global_path = /var/lib/dovecot/sieve/default.sieve
  sieve_global_dir = /var/lib/dovecot/sieve/
}

2.7 create sieve rule to move spam male to Junk folder and apply the rule
if /var/lib/dovecot/sieve/ create it and create a file /var/lib/dovecot/sieve/default.sieve with the following content
require ["fileinto", "envelope", "subaddress"];
if header :contains "X-Spam-Flag" "YES" {
   fileinto "Junk";
}


then run the following comment
root@emailserver#sievec /var/lib/dovecot/sieve/default.sieve

2.8 auto create Junk directory, edit/etc/dovecot/conf.d/10-mail.conf to

mail_location = maildir:/var/mail/vhosts/%d/%n
namespace inbox {
  inbox = yes

 mailbox Junk {
    auto = subscribe
    special_use = \Junk
 }
}

mail_privileged_group = mail


2.9 create user and assign permission

root@mailserver# adduser spamd --disabled-login
root@mailserver# chown -R vmail:vmail /var/lib/dovecot

restart service
root@mailserver# service spamassassin restart
root@mailserver# service dovecot restart
root@mailserver# service postfix restart 


No comments:

Post a Comment