Protecting your email server with Alpine

From Alpine Linux
Revision as of 14:19, 6 June 2008 by Clandmeter (talk | contribs) (Setting up Gross greylisting server)
Jump to: navigation, search


This document will outline how you can setup a spam/virus gateway with Alpine Linux. I want to say first that this is not the only way of setting up such a system. There are many ways of achieving this goal but this is the way I take. I do not issue any guarantee that this will work for you!

First thing I want to mention is, it is probably not a good way to setup Postfix on a disk less system (having the mailer spool in memory). If you would ever suffer from power failure you would loose the contents of your Postfix spool. That said, in our organization we are using a UPS device to supply our servers with backup power, so the chances that our server would shutdown because of power failure is minimal (and we are prepared to take this risk).

For this particular setup we are going to use the following:

  • Mailer daemon: Postfix
  • Virus scanner: Clamav
  • SMTP filter: Clamsmtp
  • Greylisting server: Gross
  • Extra definitions: SaneSecurity & MSRBL
  • Exchange 2003 users/groups in relay_recipient_maps
  • Alpine Linux 1.7.19 (some packages are not available before this version)

Setting up Postfix

The first thing we are going to install is our mailer daemon:

apkl_add postfix

This will install Postfix with a default configuration in /etc/postfix. Lets first take a look at, this is the (as the name implies) main configuration file for Postfix. I will show you my configuration file which you can use (I've commented out some options which we enable later on):

mynetworks = lan-net/24,
transport_maps = hash:/etc/postfix/transport
relay_domains = $transport_maps
smtpd_helo_required = yes
disable_vrfy_command = yes
#relay_recipient_maps = hash:/etc/postfix/exchange_receipients
smtpd_recipient_restrictions =
    #check_policy_service inet:,
smtpd_data_restrictions =
#content_filter = scan:[]:10025

These are the minimal settings I use to setup a postfix mail gateway. If you are looking for other settings please issue the following command:

postconf |more

This will display your current default configuration. If you want to change any of these settings you can add them to and reload postfix.

Looking at my file you will see the setting "transport_maps". This setting refers to a file inside the postfix config directory which will hold information for postfix to which server it should forward email to. It should look similar like this:

domain-a.tld   smtp:[]
domain-b.tld   smtp:[]

When ever an email enters our mail gateway for a domain specified in our "transport_maps" file it will forward this email after processing to the IP address assigned. For complete documentation please refer to the postfix docs. When are ready editing this file, issue the following command:

postmap /etc/postfix/transport

This will create a hash db of this file which will be easier/faster for postfix to read.

The second setting we will look at is 'relay_domains". This setting will tell postfix for which domains it will relay emails. Because this setting will most probably be the same as the domains we mention in "transport_maps" we can just link to it.

Now your basic email gateway is ready and you can start it but remember there will be no virus or spam filtering.

/etc/init.d/postfix start

We can start it at boot:

rc_add -k postfix

Setting up Clamav

To be able to filter out viruses from our emails we need a virus scanner. The only real open-source solution available is Clamav. Lets install it:

apk_add clamav

We will be using the daemonized version of Clamav "clamd". There is nothing we need to change for Clamav, we can use the default settings. The virus definitions are automatically updated with freshclam. Lets start it:

/etc/init.d/clamd start

Lets start it at boot:

rc_add -k clamd

Setting up Clamsmtp

Ok so now we got a mail daemon and a virus daemon installed and setup ready. Now we need the two daemons to talk to each other. The most popular tool to do so is amavisd-new but it is based on Perl and I don't like it because Perl can be a resource hog and I'm not planning to install it on my Alpine install. Another lighter C-based solution is Clamsmtp. It is a SMTP filter which listens for incoming connections and scans the emails with clamd and forwards it back again to the MTA. It doesn't come with a lot of features like amavisd-new does but its enough for me. Lets install it:

apk_add clamsmtp

Here is my clamsmtp.conf configuration file:

ClamAddress: /var/run/clamav/clamd.sock
TempDirectory: /tmp
Action: drop
Quarantine: on
User: clamav
VirusAction: /etc/postfix/scripts/

Clamsmtp has support for a virus action script which will be run each time clamd returns and Positive detection. I have linked my virus action script here but it has not been tested enough so use it at your own risk!

NOTE: Here in our organization we are running Exchange 2003. Exchange has support for public folders which is a good way of storing the files we filter with Clamsmtp. Make sure you have proper permissions and size limitations for the public folder so it doesn't get to big and other people cannot access the folder, remember it will contain viruses!

Setting up Gross greylisting server

I have used greylisting for several months now and while it has it positive affects it also has its negative. One of the positive affects is that you will get almost no spam/virus emails into your system anymore but it will introduce a delay to a part of you email traffic. If your organization is big enough you will start to notice people complain about delayed emails... This is where Gross will jump in, it still uses greylisting but it will not do so for all hosts but only the ones that are matched to the specified DNSBL databases. If you want to find out more regarding gross please go to their website:

Lets install gross:

apk_add gross

Here is my grossd.conf file:

protocol = postfix
statefile = /var/db/gross/state
check = dnsbl
check = rhsbl
dnsbl =
dnsbl =
dnsbl =
dnsbl =
dnsbl =
dnsbl =
rhsbl =

Lets start grossd:

/etc/init.d/grossd start

Please note: the init file for gross will automatically generate the grossd state file in the directory specified in its config file. Because we are running Alpine from memory the state file is not saved to disk so we need to add it to our backup with lbu_commit. The safest way to do this is the first stop grossd before committing the changes to our backup.

lbu_include /var/db/gross/state

/etc/init.d/grossd stop


/etc/init.d/grossd start

Setting up SaneSecurity & MSRBL extra definitions

apk_add curl rsync

To Be Continued....