Postfixadmin / PostgreSQL / Courier / Squirrelmail / SpamAssassin / ClamAV on Debian Etch (Howto/Tutorial)
What?
This is a quick guide to installing Postfixadmin on a Debian Linux (Etch/4.0) server. I've done a similar guide on this before, but it's getting dated; hence this new one.
This guide also covers installing ClamAV to scan incoming mail and viruses.
If you follow this guide through you should end up with a mail server which can support virtual domains and users, and can be administered through a web browser. Domain specific 'stuff' can be delegated to other administrators if you so wish.
The mail server in question (for a customer of mine) also has Squirrelmail installed, so I've bundled that in too. For historical reasons, the server uses PostgreSQL (rather than MySQL) for it's backend database.
PostfixAdmin is the web based front end through which users and administrators interact with the configuration of the server. Using it you can easily add domains/users/aliases etc to a mail server. It also supports vacation / autoreply support.
Commercial Plug
Pale Purple provide and support Linux mail servers based on a similar configuration. So, if you need a support contract, or more functionality....
Install Debian
No surprise there... I installed Etch via netboot, and ended up with a fairly minimal setup. You'll probably do it a different way. I told it to install as a 'mail server' and a 'web server'. The 'mail server' option was probably a mistake as it installs uw-imapd and exim, neither of which I wanted/needed.
You probably want to install openssh-server and molly-guard :)
Postfix
apt-get install postfix postfix-pgsql
(Or postfix-mysql if you're going to use that instead)
I selected the Internet Site configuration when asked to pick a configuration.
/etc/apt/sources.list
In order to have slightly more recent versions of a few packages (PHP5, ClamAV and PostgreSQL mainly), I added the following into my /etc/apt/sources.list file :
deb http://packages.dotdeb.org stable all deb http://www.mirrorservice.org/sites/backports.org/ etch-backports main contrib non-free deb http://volatile.debian.org/debian-volatile etch/volatile main contrib non-free
Install PostgreSQL
I installed PostgreSQL 8.2 from backports.org ...
apt-get install -t etch-backports postgresql-8.2
(Note: there is no requirement on using v8.2, but I'm under the impression that it's faster than previous versions). I'd suggest you use at least v8.1 (in Etch) from a maintenance point of view.
Install PHP5
I always install the suhosin extension to PHP in the hope it will provide extra security. APC (Alternative PHP Cache) is also installed in the expectation it will improve performance.
apt-get install php5 libapache2-mod-php5 php5-pgsql php5-suhosin php5-apc php-pear
(The above packages nearly all come from dotdeb.org)
Install Postfixadmin
Although I have created .deb for Postfixadmin; at the time of writing, v2.2.0 hasn't been released; so I instal Postfixadmin from SVN. Hopefully, we'll release version 2.2.0 of Postfixadmin sometime soon, and you will want to see this page to download it.
cd /var/www svn co https://postfixadmin.svn.sourceforge.net/svnroot/postfixadmin/trunk postfixadmin
If you now hit http://your.server/postfixadmin you should see a slightly useful 'welcome' screen, follow the link through to the 'setup.php' page. And you should get some sort of instant gratification that at least something works :) (although some of the checks will fail)
Setting up PostgreSQL (or MySQL)
As postfixadmin stores all of it's configuration within a database, we need to setup the database before we can do much further. You may find that phppgadmin or phpmyadmin help with this.
Basically - create a user (e.g. 'postfix') and a database (e.g. 'postfix'). The user should own the database. Ensure there's a password set on the user.
If security is a concern, you should probably have a user that is 'read-only' which is used by postfix (as it only ever queries the DB) while postfixadmin will need a read-write user account.
If you're using PostgreSQL, the following shows what I typed in from a shell (all lines containing a $ or #)on the server when logged in as root
mail:~# su - postgres
postgres@mail:~$ psql template1
Welcome to psql 8.2.4, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit
template1=# CREATE USER postfix WITH PASSWORD 'complexpassword';
CREATE ROLE
template1=# CREATE DATABASE postfix WITH OWNER postfix ENCODING 'UNICODE';
CREATE DATABASE
template1=# \q
If, like me, you are useless at picking passwords, try using pwgen
Load the Postfixadmin Database Schema into your database
As of subversion revision 328, the setup.php script should automatically create the database structure for you. If you are using an older version of PostfixAdmin you'll need to do the following :
cd /var/www/postfixadmin psql -U postfix -h localhost postfix < DATABASE_PGSQL.TXT
This may spew out a few errors about roles that don't exist, but it should work
Configuration of Postfixadmin
Edit /var/www/postfixadmin/config.inc.php in your favourite editor (vi[m]).
- Change
$CONF['configured'] = false;
to
$CONF['configured'] = true;
- Change
$CONF['postfix_admin_url'] = '';
to
$CONF['postfix_admin_url'] = 'http://your.server/postfixadmin';
- Change
$CONF['database_type'] = 'mysql';
to pgsql (assuming you want PostgreSQL!)
- Change the other database parameters to match what you used above.
You'll want to change other parameters, but they're not normally essential
Postfixadmin
Once your config.inc.php file has the right database credentials, and you refresh http://your.server/postfixadmin/setup.php you should some output indicating that the database tables have been created, and also see a dialog box to Create the superadmin account. You should treat these details a bit like the 'root' password for a Unix server. This user will be able to add/remove/edit any domains/users/aliases etc.
Anyway, choose an admin account, this could be (for example) it@your.domain
Submitting this form, successfully, should result in the page giving you a message like 'Admin has been added!'
Delete setup.php (rm setup.php or mv setup.php setup.php.blah)
Configuring Postfix
This always seems to be the bit that causes others trouble....
New configuration files
In my world, the following go in /etc/postfix/pgsql
You'll need to change the xxxxxx's to appropriate values
relay-domains.cf
(Who we relay mail for (as a backup mx))
user = postfix password = xxxxxxx dbname = postfix hosts = localhost query = SELECT domain FROM domain WHERE domain = '%s' AND backupmx = true
virtual-alias-maps.cf
(Think: /etc/aliases or similar)
user = postfix password = xxxxxxxx dbname = postfix hosts = localhost query = SELECT goto FROM alias WHERE address='%s' AND active = true
virtual-domains.cf
(Domains we accept mail for...)
user = postfix password = xxxxxxxx dbname = postfix hosts = localhost query = SELECT domain FROM domain WHERE domain='%s' AND backupmx = false AND active = true
virtual-mailbox-limit-maps.cf
(Only used if you're checking quota etc)
user = postfix password = xxxxxxx hosts = localhost dbname = postfix query = SELECT quota FROM mailbox WHERE username = '%s'
virtual-mailbox-maps.cf
(What mailboxes exist that we can deliver to)
user = postfix password = xxxxxxxx dbname = postfix hosts = localhost query = SELECT maildir FROM mailbox WHERE username='%s' AND active = true
main.cf changes
Add in the following :
# All virtual mailboxes live somewhere in here .. virtual_mailbox_base = /var/mail/vmail # The (virtual) domains we accept mail for virtual_mailbox_domains = proxy:pgsql:/etc/postfix/pgsql/virtual-domains.cf # Lookup mailbox location, uid and gid based on email address received. virtual_mailbox_maps = proxy:pgsql:/etc/postfix/pgsql/virtual-mailbox-maps.cf virtual_uid_maps = static:101 virtual_gid_maps = static:101 virtual_alias_maps = proxy:pgsql:/etc/postfix/pgsql/virtual-alias-maps.cf relay_domains = proxy:pgsql:/etc/postfix/pgsql/relay-domains.cf local_transport = virtual local_recipient_maps = $virtual_mailbox_maps
chown 101 /var/mail/vmail
Postfix SMTP Auth Support
If your users are likely to be trying to send mail through your mail server when they are not on a trusted network, you'll need to add this to /etc/postfix/main.cf
smtpd_sasl_authenticated_header = yes smtpd_sasl_auth_enable = yes smtpd_sasl_security_options = noanonymous broken_sasl_auth_clients = yes
And in /etc/postfix/sasl/smtpd.conf put the following :
pwcheck_method: saslauthd saslauthd_path: /var/run/saslauthd/mux log_level: 3 mech_list: PLAIN LOGIN
(As you can see, we'll be using SASL as a backend for authentication)
SASL
Thankfully the SASL package works a bit better under Etch than it did under Sarge/etc.
apt-get install sasl2-bin
Edit /etc/default/saslauthd so it has :
MECHANISMS="rimap" THREADS=5 OPTIONS="-r -c -O localhost -m /var/spool/postfix/var/run/saslauthd"
You'll need to mkdir -p /var/spool/postfix/var/run/saslauthd before SASL will start
(One day, I might change to use the pam_sql module; as this would remove unnecessary IMAP logins... )
Courier
apt-get install courier-authdaemon courier-imap courier-imap-ssl courier-pop courier-pop-ssl courier-authlib-postgresql
Configuring Courier's authdaemon
You'll need to edit /etc/courier/authpgsqlrc (or authmysqlrc if using MySQL)
PGSQL_HOST localhost PGSQL_PORT 5432 PGSQL_USERNAME postfix PGSQL_PASSWORD something PGSQL_DATABASE postfix PGSQL_USER_TABLE mailbox PGSQL_CRYPT_PWFIELD password PGSQL_UID_FIELD '101' PGSQL_GID_FIELD '101' PGSQL_LOGIN_FIELD username PGSQL_HOME_FIELD '/var/mail/vmail' PGSQL_NAME_FIELD name PGSQL_MAILDIR_FIELD maildir PGSQL_QUOTA_FIELD quota
And also edit /etc/courier/authdaemonrc, and set authmodulelist="authpgsql" (or authmysql if you're using MySQL)
If you now create a user in a test domain on postfixadmin, you should be able to connect to your mail server successfully, and receive mail
Basic Testing (pop3)
Assuming you've created a domain, and a user within that domain from Postfixadmin, you should be able to do something like the following :
mail:~# tail -f /var/log/mail.log & mail:~# echo 'test email' | mail test@my.domain mail:~# Dec 6 22:31:56 mail postfix/pickup[11888]: A811A2B10063: uid=0 from=<root> Dec 6 22:31:56 mail postfix/cleanup[11897]: A811A2B10063: message-id=<20071206223156.A811A2B10063@mail.my.domain> Dec 6 22:31:56 mail postfix/qmgr[11889]: A811A2B10063: from=<root>, size=297, nrcpt=1 (queue active) Dec 6 22:31:56 mail postfix/virtual[11902]: A811A2B10063: to=<test@my.domain>, relay=virtual, delay=0.11, delays=0.05/0.04/0/0.02, dsn=2.0.0, status=sent (delivered to maildir) Dec 6 22:31:56 mail postfix/qmgr[11889]: A811A2B10063: removed
Additionally, if you now look in /var/mail/vmail, you should see a folder called 'test@my.domain'. No guesses should be needed to figure out what this contains!
Squirrelmail
Squirrelmail is a mature web based mail client. It's been around for some time now, and thankfully plugins exist for a number of additional "features". As your author patched up the squirrelmail postfixadmin plugin, he's going to take a small amount of time <plug>it.</plug>
-
apt-get install squirrelmail
-
wget http://squirrelmail-postfixadmin.palepurple.co.uk/files/squirrelmail-postfixadmin_2.1.0-1_all.deb
-
dpkg -i squirrelmail-postfixadmin_2.1.0-1_all.deb
- Edit /etc/squirrelmail/plugins/postfixadmin-config.php - use the same settings from Postfixadmin
- pear install MDB2
- pear install MDB2#pgsql (or MDB2#mysql)
- Run squirrelmail-configure and enable the Postfixadmin plugin
Squirrelmail should be accessible at http://youserver/squirrelmail by default.
ClamAV
This is easiest to integrate via Amavis. You should get ClamAV from dotdeb.org, or Debian volatile via apt-get. Relying on the 'default' clamav shipped with etch is probably not a good idea (and ClamAV will also moan when it tries to update it's definitions list).
apt-get install amavisd-new clamav spamassassin
You'll want to edit
/etc/amavis/50-user
In my case, it looks a bit like :
use strict;
#
# Place your configuration directives here. They will override those in
# earlier files.
#
# See /usr/share/doc/amavisd-new/ for documentation and examples of
# the directives you can use in this file
#
chomp($myhostname = `hostname`);
$forward_method = 'smtp:127.0.0.1:10025';
# Where to submit notifications
$notify_method = $forward_method;
# Net::Server Pre-forking settings; note max_servers should match Postfix's master.cf..
$max_servers = 5;
$max_requests = 10;
$child_timeout = 5*60; # abort child if it takes longer than x seconds to complete.
# MTA specific settings...
$relayhost_is_client = 0;
$insert_received_line = 1;
$inet_socket_bind = '127.0.0.50';
@inet_acl = qw ( 127.0.0.0/8 );
# How we handle viruses and spam; options being discard, bounce or pass.
$final_virus_destiny = D_DISCARD;
$final_banned_destiny = D_BOUNCE;
$final_spam_destiny = D_PASS;
$final_bad_header_destiny = D_BOUNCE;
# Headers..
$X_HEADER_TAG = 'X-Virus-Scanned';
$X_HEADER_LINE = "by Amavis+SpamAssassin+ClamAV and more at $mydomain";
$remove_existing_x_scanned_headers = 1;
$remove_existing_spam_headers = 1;
$sa_tag_level_deflt = -99.0; # add spam info headers if at, or above that level
$sa_tag2_level_deflt = 5.0;
$sa_kill_level_deflt = 5.0; # triggers spam evasive actions
$recipient_delimiter = '+';
$replace_existing_extension = 1;
$localpart_is_case_sensitive = 0;
# SpamAssassin settings
$sa_timeout = 30;
$sa_auto_whitelist = 1;
$sa_local_tests_only = 0;
$sa_spam_modifies_subj = 1;
$sa_spam_subject_tag = '*** SPAM *** ';
$sa_spam_report_header = 1;
$first_infected_stops_scan = 1;
$sa_debug = 1;
$DO_SYSLOG = 0;
$SYSLOG_LEVEL = 'mail.info';
$LOGFILE = "/var/log/amavis.log";
@local_domains_acl = ('.');
#------------ Do not modify anything below this line -------------
1; # insure a defined return
The above configures Amavis to :
- Scan and label Spam for all mail that goes through the server (And not just for some specified domains - see @local_domains_acl)
- Forward scanned mail to 127.0.0.2:10025 for delivery by Postfix
- Listen for mail (from Postfix) on 127.0.0.50
- Discard Viruses
- Remove any existing anti-virus tags etc (as these will be from some.other.system)
- If SpamAssassin scores more than 5.0, then rewrite the subject etc
Some notes:
- SpamAssassin does not need to run via the SpamC/SpamD mechanism - Amavis handles this all internally
- Amavis appears to ignore most, if not all, settings you would otherwise set for SpamAssassin in e.g. /etc/spamassassin/local.cf
- When I initially started using Amavis it seemed to be a bit clueless when it came to listening and delivering on the same IP address. Perhaps I did something wrong, but nevertheless, this is why it listens on a different IP address (127.0.0.50) to the one it delivers to (127.0.0.1). You don't need to do anything to setup 127.0.0.50 on a Linux box.
Amavis / Postfix Integration
- Edit /etc/postfix/master.cf
amavis unix - - - - 5 smtp
-o smtp_data_done_timeout=5000
-o smtp_send_xforward_command=yes
-o receive_override_options=no_address_mappings
# change or remove the existing smtp definition.
smtp inet n - - - 12 smtpd
-o content_filter=amavis:[127.0.0.50]:10024
# where mail is re-injected back in by Amavis after it's
# done it's stuff.
127.0.0.1:10025 inet n - - - - smtpd
-o smtpd_autorized_xforward_hosts=127.0.0.0/8
-o smtpd_client_restrictions=
-o smtpd_helo_restrictions=
-o smtpd_sender_restrictions=
-o smtpd_recipient_restrictions=permit_mynetworks,reject
-o mynetworks=127.0.0.0/8
-o receive_override_options=no_unknown_recipient_checks,no_header_body_checks,no_address_mappings
Then reload Postfix, make sure Amavis and ClamAV are running (/etc/init.d/amavis start or /etc/init.d/clamav-daemon ) and it should work.
Technorati Tags:
antivirus and antispam control
Have write a complementary howto to integrate antivirus and antispam control ?
antispam/antivirus
Hi,
No I haven't; but there are many tutorials "out there" which can guide you in the right direction.
David.
updated...
Hi,
I've added in 'stuff' on how to integrate ClamAV + Amavis + SpamAssassin
thanks!
you helped me a lot!
but for the many not-so-experienced users out there (like me...) you shouldn't forget to tell them to restart apache after installation of the php modules...
thanks
there is an error regarding UID 101 / 1001 ...must be the same everywhere
UID / GID error
Maybe they are already in use on your system by another user / group? Those are not set in stone. You can set whatever UID/GID you like, so long as you alter the rest of the config files to match.
-J
Thanks for tutorial! but
Thanks for tutorial!
but some whn trying integrate amavis + clamav + spamassasin, i got this error when try receive or send email from server:
Apr 8 11:58:59 mailsrv postfix/qmgr[16246]: warning: connect to transport custom_filter: No such file or directory
ah, that's because i was chaining content filters
Hi,
I've updated the docs above, essentially I had a stray '-o content_filter=custom_filter' when it [custom_filter] wasn't defined anywhere.
Thanks
David.
amavis issues
Hi,
I've noticed that Amavis can create too many files in /var/lib/amavis/tmp and /var/lib/amavis/virusmails ....
If this is the case you'll see messages like :
Apr 8 13:24:05 youserver /usr/sbin/amavisd-new[5539]: (05539-01) TROUBLE in process_request: Can't create directory /var/lib/amavis/tmp/amavis-20080408T132405-05539: Too many links at /usr/sbin/amavisd-new line 4032, line 4.
This indicates (no surprise) that you've got a directory with too many entries in it (or you've run out of inodes (unlikely)).
The solution is easy enough...
find /var/lib/amavis/tmp -type f -name '*spamassassin*' -print | xargs rm -Rf
find /var/lib/amavis/virusmails -type f '*.gz' | xargs rm -Rf
(Or change your config files to not store the quarantined files)
(I have a feeling the tmp/.spamassassin* files are erronous, and should be cleaned up by SpamAssassin)
MySQL & Sockets
With mysql you might have problems connecting to the db via sockets if you're in a chrooted environment. Therefore you should use "hosts = 127.0.0.1" instead of "hosts = localhost" to force connection via tcp. See also http://www.postfix.org/mysql_table.5.html (Parameter "hosts").
SASL error message
if you for some reason get error message:
warning: xsasl_cyrus_server_get_mechanism_list: no applicable SASL mechanisms
run:
apt-get install libsasl2-modules
it worked for me
I'm running into problems
I'm running into problems somewhere ... I don't know really if I should change sth or not in the part about creating the files at /etc/postfix/pgsql
And I think that is the cause of all my problems right now.
There's another thing I think you should say, when editing /var/www/postfixadmin/config.inc.php users should change also the username and psw. I know this sounds stupid and I didn't have any problems with this, but an unexperienced user will have headaches with this.
There's one last thing, what if you want to have multiple domains? ... what should I change etc. I have no idea.
Thanks for this nice and complete guide.
Hi there, to begin with,
Hi there, to begin with, thanks for this guide.
I would like to say that there are some parts that are not clear enough (for me at least). The intention of this is to make this guide better, I'm not trying to steal you credit or tell you how you should make things.
---------------------
When you edit Edit /var/www/postfixadmin/config.inc.php while configuring postfix, the user should fill in the username, password and database name.
svn is not installed by default in some installations, for example, installing a network version of debian.
While creating the files at /etc/postfix/pgsql it's not clear enough (for me at least) what should be changed or not.
I finished up at the Basic Testing (pop3) part with warnings and errors.
One idea that appears in my head, is that all the things that a user should change or write or anything could be between <<>> for example, or anything else.
Another thing that appears in my head, is what if you want to add more domains, what would you need to edit, change, whatever to have more domains. I get lost at creating the files al /etc/postfix/pgsql.
-----------------------------
Despite that, it's one of the most complete guides I saw out there.
Thanks again, and sorry if I hurt your feelings. Mail me if you want to.
Great
Hi, I welcome all feedback - as long as it's not spammy or too offensive, in which case it never gets published :)
I was mistaken
Well David, I started this guide again, from zero, and ... it went fine, with almost everything (explained below), I think I was misstyping or doing sth stupid round there, maybe because of how sleepy I was.
Now, everything was working beautifull ... installed everything except the Squirrel, and rebooted the machine, checked mail trough Out.Exps using the same admin user created to test.
But, after creating more account's through the Postfix Admin interface, using or no the same domain, gives me an error.
(I'm sure it's not because of the domain)
mail courierpop3login: Connection, ip=[::ffff:192.168.1.122]
mail courierpop3login: chdir name@domain.com/: No such file or directory
Went to the /var/mail/vmail/ and find that there was only the admin folder. It seems it's not creating the folder for the user.
I think this happened after installing the Amavis/SpamAssassin/ClamAv.
Can't see where is the bug. And I'm tired, so no more for today. =)
ah - the classic postfixadmin bug report
Hi,
You need to tell 'it' to send an email to the user when creating a mailbox. Postfixadmin (itself) doesn't have permission to create files within the mail directory. But by sending a welcome message to the user, Postfix creates the mail directory, and everything works.
I am wondering if the option of sending a 'welcome' email should no longer be an option, and instead become mandatory.
About the welcome mail
Well ... after I saw that the folder wasn't created, I went to the postfix admin webpage and sent a welcome mail.
Nothing happened. I'm testing this in a VM, so, I'll tell you tomorrow (if I can) what happens before installing Amavis/ClamAv/SpamAssassin and after.
If you want me to test sth in particular, tell me. I think you can see my mail addres from my posts, mail me if you want.
check the directories
Hi,
Look in /var/log/mail.log - was an email sent to the account, or did it get bounced with an error message?
Look in /var/mail/vmail (or wherever) - does the user's mail directory exist? If not, try 'echo "hello" | mail user@the.domain' in a shell, and see if that shows up in mail.log.
thanks
David.
Checked log
Hi
well ... I've tryed two ways of the welcome mail. Through command line and through postfixadmin.
Through command line I get this
mail postfix/virtual[2368]: 18621A8531: to=, relay=virtual, delay=0.6, delays=0.3/0.12/0/0.18, dsn=2.0.0, status=sent (delivered to maildir)
And the directory is created.
Through postfixadmin I get this
mail postfix/qmgr[2188]: 3AFBBA852C: to=, relay=none, delay=0.09, delays=0.07/0.02/0/0, dsn=4.4.1, status=deferred (delivery temporarily suspended: connect to 127.0.0.50[127.0.0.50]: Connection refused)
And of course, the directory isn't created.
So, to check my thought's ... went to /etc/postfix/master.cf and commented the added parts and restored the original smtpd line.
Then went to postfixadmin and sent again a mail to the mailbox that had no directory created. The directory was created.
I think it might be an option of amavis or sth like that. =)
and thank you.
Leo.
amavis to blame!
it looks like your amavis isn't listening on 127.0.0.50....
check /etc/amavis/conf.d/50_user.cf contains "$inet_socket_bind = '127.0.0.50';" and is running.
"
Discovery
David ... I'm really sorry to say this, but in the guide, in the ClamAv section you put that we should edit /etc/amavis/50-user .... when is /etc/amavis/conf.d/50_user.cf
I edited the file, etc. But I checked if amavis whas running, to my surprise, no. So I tryed to start it, and gives me this error
Starting amavisd: The value of variable $myhostname is "mail", but should have been
a fully qualified domain name; perhaps uname(3) did not provide such.
You must explicitly assign a FQDN of this host to variable $myhostname
in amavisd.conf, or fix what uname(3) provides as a host's network name!
(failed).
What amavis says is correct, my hostname is mail and the domain is what goes after @ ...
So I changed the domain in 50_user.cf to my domain ... as 'mail.mydomain.com' and it worked. =D
I don't really know if I should name my machine in other way, what do you think ?
The definitive solution
The definitive solution is to put change this line
chomp($myhostname = '127.0.0.1');
And that's all. =D
Thanks David for everything.
Compressed files
If users want to scan for virus inside compressed files, you may want to run this
apt-get install bzip2 cabextract arj arc zoo lzop tnef pax unrar-free
It installs support for almost all compressed files.
David, thanks for this guide again.
Post new comment