Secure email servers from scratch with FreeBSD 6 (Part 2)

Configuring the core components

Download the whole article as PDF

Write a full post in response to this!


In the last article we parted ways after configuring a base FreeBSD system, enabling it with upgrades via cvsup and portsupgrade, and securing it with a simple ipfw2 firewall. The previous article created a solid foundation which this article will build on, covering the configuration of Postfix, amavisd-new, ClamAV, SpamAssassin, MySQL and finally SquirrelMail for web mail. The final setup will have all the bells and whistles of a high end-mail setup: web-mail, anti-virus filtering, spam filtering, and hosting unlimited domains with virtual domains and users stored in MySQL.

Postfix is released under the IBM Public License, and not the GNU Public License

Postfix

The first and most important component is Postfix, a well known mail transfer agent developed by Wietse Venema at IBM and initially known as the “IBM Secure Mailer”. Venema, is a respected software engineer who also developed the popular security tool “S.A.T.A.N” (Security Administrator Tool for Analyzing Networks). Postfix is released under the IBM Public License, and not the GNU Public License; the “IPL” has been approved as an open source license by both the Free Software Foundation (“FSF”) and the Open Source Initiative(“OSI”); however, it has been declared incompatible with the GPL. Initially Postfix was created in reaction to a long list of security vulnerabilities in Sendmail, the then dominant “MTA”. The direct result of the “security first” mind, Postfix has a well earned reputation for being easy to setup, fast and secure.

Postfix has two central configuration files:

  • main.cf: which configures the “properties” of the mail server such as where user and domain information is stored, or which domains to accept mail for.
  • master.cf: which configures the “behavior” of the Postfix daemon, such as configuring interfaces to non Postfix programs, and other configuration settings for the Postfix daemon itself.

FreeBSD places the configuration files of packages installed from ports under /usr/local/etc; so normally you’ll find the Postfix configuration files under /usr/local/etc/postfix. First of all, you should install Postfix from the ports tree, in the same way that MySQL was installed in the first article.

Installing Postfix using ports

 
cd /usr/ports/mail/postfix

make install && make clean

You will then be presented with a dialog box: select TLS and MYSQL.

Postfix configuration dialog
Postfix configuration dialog

Note that when MySQL functionality is selected, the default action is to install the MySQL 4.1 client library. If you plan on running a newer version of MySQL, such as 5.0, simply cancel the Postfix installation, install the MySQL client library of your choice, and then re-run the installation.

Example:

 
cd /usr/ports/databases/mysql50-client
make install && make clean

During the installation of the client libraries you may be prompted for options to the gettext package. It’s not necessary to select any of the options, but feel free to do so if you wish.

After Postfix is built, you will be prompted asking if you want to activate it in the mailer.conf file: say “yes”:

 
[Prompt] Would you like to activate Postfix in /etc/mail/mailer.conf [n]? y

Configuring Postfix—main.cf

Now that Postfix is installed, it’s time to dive into the most important of the two configuration files, main.cf. In this one file there are essentially two sets of directives: one for the domains the server will be hosting, which being with the virtual prefix; and one for the mail server itself, with lines that begin with my as in myhostname. I’ll be giving in line commentary, so read the configuration file closely.

 

# These virtual_* directives configure the domains, users, 
# and aliases this Postfix instance will handle.
# Use proxy: for performance
virtual_alias_maps = proxy:mysql:/usr/local/etc/postfix/mysql_virtual_alias_maps.cf
virtual_mailbox_domains = proxy:mysql:/usr/local/etc/postfix/mysql_virtual_domains_maps.cf
virtual_mailbox_maps = proxy:mysql:/usr/local/etc/postfix/mysql_virtual_mailboxes_maps.cf

proxy_read_maps = $local_recipient_maps $mydestination $virtual_alias_maps
  $virtual_alias_domains $virtual_mailbox_maps $virtual_mailbox_domains
  $relay_recipient_maps $relay_domains $canonical_maps $sender_canonical_maps
  $recipient_canonical_maps $relocated_maps $transport_maps $mynetworks
  $virtual_mailbox_limit_maps

# Where to store the mail
virtual_mailbox_base = /usr/local/virtual

# Ownership of the mail directory
virtual_uid_maps = static:125
virtual_gid_maps = static:125


smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_security_options = noanonymous

# Secure SMTP-AUTH 
smtpd_use_tls = yes
smtpd_tls_enforce_tls = yes

# Uncomment the following line if you only want auth to happen over tsl
# smtpd_tls_auth_only = yes

# This setups the ssl certificates which I'll configure a little later
smtpd_tls_cert_file = /usr/local/etc/postfix/smtpd.crt
smtpd_tls_key_file = /usr/local/etc/postfix/smtpd.key

# Mostly for MS outlook clients
broken_sasl_auth_clients = yes

# Built in restrictions
smtpd_recipient_restrictions =
  permit_mynetworks,
  permit_sasl_authenticated
  reject_non_fqdn_hostname,
  reject_non_fqdn_sender,
  reject_non_fqdn_recipient,
  reject_unauth_destination,
  reject_unauth_pipelining,
  reject_invalid_hostname,
  reject_rbl_client opm.blitzed.org,
  reject_rbl_client list.dsbl.org,
  reject_rbl_client bl.spamcop.net,
  reject_rbl_client sbl-xbl.spamhaus.org


# Enables virtual hosting
virtual_transport = virtual

# Filter with amavis-new which uses clam-av for
content_filter=smtp-amavis:[127.0.0.1]:10024
Don't miss out on the other pages!
123456next ›last »

Write a full post in response to this!

Similar articles

0

Do you like this post?
Vote for it!

Copyright information

Verbatim copying and distribution of this entire article is permitted in any medium without royalty provided this notice is preserved.

Biography

Yousef Ourabi: Yousef Ourabi is a developer in the San Francisco bay area. He is currently working at the startup he recently founded, Zero-Analog. Zero-Analog is currently developing an enterprise application, however, one of its stated goals is "to increase the rate of open source adoption in companies of all sizes, across all industries". Zero-Analog also offers consulting services, all based around open source tools, frameworks and applications.

Anonymous visitor's picture

i thing there is an error

Submitted by Anonymous visitor on Sat, 2006-12-02 14:00.

Vote!
0

i thing there is an error in the manual.
actualy i was expecting part two with great impatience
now i am testing it and so far i am at the mysql query part
here i am:

Grant privileges
###
Run the following command:

GRANT SELECT on mail.* to mail_admin identified by password ('mail_admin_password');
###
(it is from inside of mysql)
but i get syntax error on this step
"ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('mail_admin_password')' at line 1"

i am not a mysql specialist, but i tried some variations of that command, but still i got errors

i thing that there is something missing, i dont see where in mysql the user mail_admin is created, and may be this is the reason this command to fail.
forgive me if i am wrong, but please fix it :))

Anonymous visitor's picture

GRANT SELECT on mail.* to

Submitted by Anonymous visitor on Sat, 2006-12-02 14:13.

Vote!
0

GRANT SELECT on mail.* to mail_admin identified by password ('mail_admin_password');

should be
GRANT SELECT on mail.* to mail_admin identified by 'mail_admin_password';

and there will be no errors :))

Anonymous visitor's picture

.pdf or printable version?

Submitted by Anonymous visitor on Tue, 2007-01-02 22:30.

Vote!
0

Can we please have a .pdf or at least a printable version of this article?

Thanks a lot :)

Anonymous visitor's picture

printable version!!!

Submitted by Anonymous visitor on Mon, 2007-01-08 13:10.

Vote!
0

+1 for the printable version of this article.

Anonymous visitor's picture

Suggestion & printable version

Submitted by Anonymous visitor on Tue, 2007-01-16 10:45.

Vote!
0

+1 for the printable version also (metoo... Feh)

Also, given the FreeBSD project themselves now recommending using portsnap rather than cvsup to keep your ports tree up-to-date (and speaking personally, it's a whole lot easier to work than cvsup) might it be worth mentioning it?

Yousef Ourabi's picture

viz Portsnap

Submitted by Yousef Ourabi on Thu, 2007-01-18 01:26.

Vote!
0

The problem as I see it with portsnap is that you can't upgrade your src tree -- and seamless upgrading from release to release via make buildworld && make buildkernel is one of FreeBSD's strongest points -- and with one extra line to the cvsup config file it (cvsup) can pull down source and documents -- so it's still more attractive in my eyes

Though there are strong advantages to portsnap: faster update, no portsdb -Uu after pulling down the ports tree, more secure (signed with keys...etc)

-Yousef Ourabi

Anonymous visitor's picture

Nice Article

Submitted by Anonymous visitor on Fri, 2007-01-19 13:54.

Vote!
0

... but what makes this mailserver secure now?

Anonymous visitor's picture

A lot of things are left out in this doc

Submitted by Anonymous visitor on Sun, 2007-01-21 07:16.

Vote!
0

This doc is by no means even close to a beginners document. there are all kinds of steps left out and missing parts that a newbie would totally screw a system up... So tread lightly and research everything first by looking at other sources especially with the Kernel Make. Its a hopeless mess....

Draco

Anonymous visitor's picture

Anonymous

Submitted by Anonymous visitor on Sun, 2007-01-21 18:27.

Vote!
0

Not that it's of weighing importance, but one should take notice of the error in the filename of the certificate (`main.cf', page #1 of the tutorial). It says `smtpd.cert', whilst the certificate created is named `smtpd.crt'. Naturally, one would stumble across this even at first go, but then again, it might serve well changing it if one wanted a spotless and correct version for print.

Good day to all, and thank you Yousef for your great article.:)

admin's picture

Fixed

Submitted by admin on Tue, 2007-01-23 02:00.

Vote!
0

Good pick up.

Thanks

Anonymous visitor's picture

SASL daemon

Submitted by Anonymous visitor on Tue, 2007-01-23 09:43.

Vote!
0

In a configuration like that is the saslauthd need to be started also, or it is not necessary.
I am confused because I found many posts starting the saslauthd and others not.
Is the following variables needed smtpd_sasl_application_name, smtpd_sasl_type.

Anonymous visitor's picture

maildir ... directory or file

Submitted by Anonymous visitor on Fri, 2007-01-26 05:30.

Vote!
0

If the maildir field in the MySQL data base is a directory Postfix complains:

Jan 26 00:13:35 web postfix/virtual[1190]: 24A635C9: to=, relay=virtual, delay=0.41, delays=0.12/0.18/0/0.11, dsn=4.2.0, status=deferred (delivery failed to mailbox /usr/local/virtual/jennifer: cannot open file: Is a directory)

If the maildir field in the data base is a file IMAP complains:

Jan 26 00:17:51 web imapd-ssl: chdir jennifer: Not a directory

Any thoughts?

Yousef Ourabi's picture

re: maildir

Submitted by Yousef Ourabi on Mon, 2007-01-29 10:49.

Vote!
0

Hey:
if you do a mysql "select maildir from mailbox", do the directories have a trailing / -- if not that's probably the cause.

-Yousef

Anonymous visitor's picture

Thanks ... one more

Submitted by Anonymous visitor on Sat, 2007-02-03 23:32.

Vote!
0

Thanks for the pointer on the maildir above. I am having some issues with squirrelmail I keep getting the following error when trying to navigate to the web page:

/libexec/ld-elf.so.1: /usr/local/lib/php/20020429/gettext.so: Undefined symbol "php_realpath"

Note I am running PHP 4, I don't know if this is related.

Thanks again,
Aaron

Anonymous visitor's picture

Looks like squirrelmail does reuire PHP5

Submitted by Anonymous visitor on Sun, 2007-02-04 18:07.

Vote!
0

Since my web server was already running PHP4 and I didn't feel up to trying to load both modules into apache I ended up using sqwebmail. Very intuitive and simple in its configuration.

Thanks again,
Aaron

Anonymous visitor's picture

what about rc.conf?

Submitted by Anonymous visitor on Mon, 2007-02-19 17:29.

Vote!
0

what to add in rc.conf? i mean how to start all these programs?

Anonymous visitor's picture

what about rc.conf?

Submitted by Anonymous visitor on Mon, 2007-02-19 17:22.

Vote!
0

i was just wondering what do i have to add to rc.conf to start all of this apps... I've installed them exactly as it's guided...
thx

Anonymous visitor's picture

Help Needed

Submitted by Anonymous visitor on Sat, 2007-03-10 14:57.

Vote!
0

Hello all - I am a complete newbie to FreeBSD and any Unix/Linux based systems. My company has been a Windows only shop until a change in management. I followed this article to the "T" and am having problems. When I start Postfix, I get an error that it cannot perform a lookup using the mysql_virtual_aliases_maps.cf file. I was thinking that maybe the SELECT statement within that file is causing the problem? I do know that I had to insert my own mail_admin password, which has been done.

I am fairly certain that the other components are working. Squirrelmail's "configtest.php" comes back stating that the system is working fine. I can also telnet to amavisd and issue an EHLO command, which returns data, making it seem as though it is working.

One last item - can you give some examples of what data actually goes into the database "mail"? I have added users, a virtual domain and aliases. But some examples would give me a starting point of what the syntax actaully is. Not the INSERT string to add data to the database, but the actaul way the data is setup to be read. For example: if I have a user that has a username of "jdoe1" what would his alias be? And, if the server is currently "server1.mydomain.com" which IS the domain that I am looking to receive mail on, what is the virtual domain?

The ultimate goal of this server is to make it an external smarthost for an internal Microsoft Exchange box. I would appreciate anyone willing to give me some pointers there too once the mail system is actually up and running. I've seen that there are ways to actually pull the AD information using OpenLDAP.

Anonymous visitor's picture

error :(

Submitted by Anonymous visitor (not verified) on Fri, 2007-08-24 07:31.

Vote!
0

Hi.. I have error:
host 127.0.0.1[127.0.0.1] said: 451 4.5.0 Error in processing, id=71084-02, parts_decode_ext FAILED: Unix utility file(1) not available, but is needed at (eval 70) line 113. (in reply to end of DATA command))

Help?