Configuring ClamAV + Postfix

I will give an example of installing and configuring the free ClamAV antivirus for checking email messages for viruses.

In Ubuntu/Debian, switch to the root user and complete the installation:

apt install clamav clamsmtp

In the /etc/clamsmtpd.conf file, by default there were the following options, in fact they are enough (you can not change):

OutAddress: 10025
Listen: 127.0.0.1:10026
ClamAddress: /var/run/clamav/clamd.ctl
#Header: X-AV-Checked: ClamAV using ClamSMTP
TempDirectory: /var/spool/clamsmtp
PidFile: /var/run/clamsmtp/clamsmtpd.pid
#Quarantine: off
User: clamsmtp

Unless we uncomment a couple of lines and specify a script that will notify about viruses:

Header: X-AV-Checked: ClamAV using ClamSMTP
Quarantine: on
VirusAction: /etc/clamav/script.sh

Example script:

#!/bin/sh
DOMAIN=example.com
ADMIN=admin@example.com
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
LINE="-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"
if [ X`echo $SENDER | egrep $DOMAIN` != "X" ];
then MAILTO=$SENDER,$ADMIN
else MAILTO=`echo "$RECIPIENTS" | egrep $DOMAIN | tr '\n' ','`$ADMIN
fi

(echo "Virus name: $VIRUS"
echo "Sender: $SENDER"
echo "Recipient(s): $RECIPIENTS"
echo
if [ "x$EMAIL" != "x" ] && [ -f $EMAIL ]
then
echo "Quarantined to: $EMAIL"
fi
) | cat -v | mail -s "$VIRUS found on mailserver" $MAILTO 

Let’s make the script executable:

chmod +x /etc/clamav/script.sh

If you changed the configuration, then restart clamsmtp to apply the changes:

service clamsmtp restart
service clamsmtp status
ps -ef|grep clamsmtp

Make sure that ClamAV is running (otherwise letters will not go with the error “CLAMAV: couldn’t connect to: /var/run/clamav/clamd.ctl: No such file or directory”):

service clamav-daemon status
systemctl is-enabled clamav-daemon.service
systemctl enable clamav-daemon.service
service clamav-freshclam status
systemctl is-enabled clamav-freshclam.service
systemctl enable clamav-freshclam.service
service clamav-freshclam restart
service clamav-daemon restart
ps -ef|grep clamav

At the end of the /etc/postfix/main.cf file, add:

content_filter = scan:[127.0.0.1]:10026
receive_override_options = no_address_mappings

In the file /etc/postfix/master.cf add:

scan unix - - n - 16 smtp
  -o smtp_send_xforward_command=yes

127.0.0.1:10025 inet n - n - 16 smtpd
  -o content_filter=
  -o receive_override_options=no_unknown_recipient_checks,no_header_body_checks
  -o smtpd_helo_restrictions=
  -o smtpd_client_restrictions=
  -o smtpd_sender_restrictions=
  -o smtpd_recipient_restrictions=permit_mynetworks,reject
  -o mynetworks_style=host
  -o smtpd_authorized_xforward_hosts=127.0.0.0/8

Check the configuration of Postfix and restart it to apply the changes:

postfix check
service postfix restart

See also my articles:
Installing and using ClamAV antivirus
Installing and Configuring Postfix

Leave a comment

Leave a Reply