Pages

Saturday 28 December 2013

Mail Gateway with Postfix - amavisd-new - spamassassin - clamav

Domain : salongcuong.com.vn  (and more)
IP Mail server: 116.193.77.64
IP Mail Gateway : 116.193.77.201  (host gateway.salongcuong.com.vn)

<Note : remember stop 'send mail' service first before use postfix>

Step 1 : Install and Configure Postfix

#yum install postfix

==> Configure Postfix: vi /etc/postfix/main.cf

myhostname = gateway.salongcuong.com.vn
mydomain = salongcuong.com.vn
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, $mydomain
relay_domains = salongcuong.com.vn
transport_maps = hash:/etc/postfix/transport
local_recipient_maps =
local_transport = error:local mail delivery is disabled

==> Configure transport file /etc/postfix/transport

#vi /etc/postfix/transport
Add this line into the file:

salongcuong.com.vn      smtp:[116.193.77.64]

#postmap /etc/postfix/transport

==> Configure /etc/postfix/master.cf file


#vi /etc/postfix/master.cf

Put # before (comment out) :

local     unix  -       n       n       -       -       local


Step 2: Setup Amavis-new, Spamassassin and ClamAV

First setup RPM forge
(If you want to know why do we have to setup this please refer this)

#wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
(or you can choose lastest version by manually download that files)
#rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt
#rpm -K rpmforge-release-0.5.2-2.el5.rf.i386.rpm
The last line will check the file, result should be OK.

#rpm -i rpmforge-release-0.5.2-2.el5.rf.i386.rpm

===== Done

Edit /etc/yum.repos.d/CentOS-Base.repo

[base] exclude=spamassass* ... [updates] exclude=spamassass* ...


#yum --enablerepo=rpmforge,rpmforge-extras install amavisd-new clamav clamav-devel clamd spamassassin

====================== Configure ClamAV:

### /etc/clamd.conf
#
# Set the LocalSocket for clam
# Note this *MUST* match that set in /etc/amavisd.conf
#
LocalSocket /var/run/clamav/clamd.sock
#
# Comment out the TCPSocket setting:
# TCPSocket 3310


===================== Configure  Amavisd-new   /etc/amavisd.conf

$mydomain = 'example.com';                  # Edit: a convenient default for other settings
$MYHOME = '/var/amavis';                    # Uncomment: a convenient default for other settings, -H
$helpers_home = "$MYHOME/var";              # Uncomment: working directory for SpamAssassin, -S
$lock_file = "$MYHOME/var/amavisd.lock";    # Uncomment, -L
$pid_file  = "$MYHOME/var/amavisd.pid";     # Uncomment, -P
$myhostname = 'mail.example.com';           # Uncomment & Edit: must be a fully-qualified domain name!

And:


Finally, we need to uncomment the section for ClamAV like so:

### http://www.clamav.net/
['ClamAV-clamd',
  \&ask_daemon, ["CONTSCAN {}\n", "/var/run/clamav/clamd.sock"],
  qr/\bOK$/, qr/\bFOUND$/,
  qr/^.*?: (?!Infected Archive)(.*) FOUND$/ ],
# # NOTE: run clamd under the same user as amavisd, or run it under its own
# #   uid such as clamav, add user clamav to the amavis group, and then add
# #   AllowSupplementaryGroups to clamd.conf;
# # NOTE: match socket name (LocalSocket) in clamav.conf to the socket name in
# #   this entry; when running chrooted one may prefer socket "$MYHOME/clamd".


===================== Configure PostFix again to integrate with amavisd-new in master.cf
#vi /etc/postfix/master.cf 

Add below record to this file:

amavisfeed unix    -       -       n        -      2     lmtp
    -o lmtp_data_done_timeout=1200
    -o lmtp_send_xforward_command=yes
    -o disable_dns_lookups=yes
    -o max_use=20


And:

127.0.0.1:10025 inet n    -       n       -       -     smtpd
    -o content_filter=
    -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 smtpd_restriction_classes=
    -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,no_milters,no_address_mappings
    -o local_header_rewrite_clients=
    -o smtpd_milters=
    -o local_recipient_maps=
    -o relay_recipient_maps=


And

===================== Configure PostFix main.cf

Add this line into the last of the file:

content_filter = amavisfeed:[127.0.0.1]:10024


And then run:

#postfix reload


Note : If this step you would see the service amavisd can not start, when check log file (#tail - 50 /var/log/maillog) and see this error : "amavisd dead but subsys locked" or
"
Dec  8 11:26:17 test amavis[28365]: Creating db in /var/amavis/db/; BerkeleyDB 0.43, libdb 4.3
Dec  8 11:26:17 test amavis[28365]: (!!)TROUBLE in pre_loop_hook: config: no rules were found!  Do you need to run 'sa-update'?
Dec  8 11:26:17 test amavis[28365]: (!)_DIE: Suicide () TROUBLE in pre_loop_hook: config: no rules were found!  Do you need to run 'sa-update'?
"

Just run:
#sa-update

This will be OK.



============================== If you want to trust relay server (to allow which server can relay email via this gateway)

#vi /etc/postfix/main.cf

add more record to this file like this:
mynetworks = 127.0.0.0/8, 116.193.77.64/32  //......... add more server IP that you want to trust relay from the gateway (postfix) server

#postfix reload

=====================================================================

================================ Or if you want Spam assassin mark "Spam" label in front of the email subject, edit :

#vi /etc/amavisd.conf

@local_domains_maps = ( [".$mydomain", ".salongcuong.com.vn", ".domain.what.you.want.to.show.net"] );



================================ And after change information in /etc/postfix/transport file 
Have to recall

#postmap /etc/postfix/transport

No comments:

Post a Comment