Mail Server Mini-Howto

Chapter 2


Previous

 

Next

Configuring Qmail

In this section, we configure Qmail to:

  • store received messages in ~/home/mail/inbox
  • handle mail aliases
  • relay messages selectively
  1. Qmail Inbox
  2. Mail Aliases
  3. Selective Mail Relaying
  4. Starting Qmail

 

Qmail Inbox

By default, Qmail is set up to put incoming messages into $HOME/Mailbox. So a user joe would get his incoming messages stored in /home/joe/Mailbox. I don't like this default location, I want messages to go into $HOME/mail/inbox instead, so I made these changes. If you want to use the default $HOME/Mailbox file, you can ignore this section. Note that Qmail also supports a Maildir format, read the Qmail docs for more information.

/etc/qmail/dot-qmail

| dot-forward .forward
./mail/inbox

The dot-forward .forward command tells Qmail to forward emails if a .forward file exists in a user's home directory.

The ./mail/inbox line tells Qmail to store incoming messages in $HOME/mail/inbox, so if your username was joe, your incoming messages would go into /home/joe/mail/inbox.

/etc/profile.d/qmail.csh
/etc/profile.d/qmail.sh

These files define the MAIL and MAILDIR environment variables. Some programs, such as mailx and the shell, look at these variables to see where your inbox is. Change all occurrences of $HOME/Mailbox to $HOME/mail/inbox.

You may also want to check your /etc/profile file to see if it defines MAIL. If so, you can remove the declaration because they are overwritten by the declarations in /etc/profile.d/qmail.csh or /etc/profile.d/qmail.sh (depending on which shell you are using).

User Home Directories

Since messages will be stored in $HOME/mail/inbox, that directory has to exist in each user's home directory. So for the user joe, you should do this:

$ mkdir /home/joe/mail
$ touch /home/joe/mail/inbox
$ chmod 700 /home/joe/mail
$ chown joe:users /home/joe/mail

This will create the /home/joe/mail directory and the file home/joe/mail/inbox. The chmod and chown commands change the ownership and rights for the file and directory. If you are unsure of anything you see, consult their man pages.

Next, you should create the directory and file in /etc/skel since that is a template directory that gets copied into each new user's home directory. (If you don't have an /etc/skel, don't worry about this part)

$ mkdir /etc/skel/mail
$ touch /etc/skel/mail/inbox

Don't worry about changing ownership and rights for what you create in /etc/skel, a script will fix all that up when a new user is created.

 

Mail Aliases

If you installed Qmail via the RPM, there is a fastforward program that allows Qmail to use the /etc/aliases file that sendmail uses. This file is where you define your mail aliases, for example mail for root should go to your account.

Put this line into /var/qmail/alias/.qmail-default:

| /var/qmail/bin/fastforward -d /etc/aliases.cdb

Qmail doesn't deliver mail to root, so you must set up a mail alias. It is also useful to set up some other mail aliases. Click here to see a sample aliases file, it sets up some common mail aliases and will send mail destined to those aliases to a user jim.

After you create an /etc/aliases file, you must compile it into the /etc/aliases.cdb file. To do this, simply run:

$ /usr/local/bin/newaliases

You must remember to run this program everytime you make changes to your aliases file.

 

Selective Mail Relaying

Mail relaying is when your mail server receives email from an outside server that is not destined for an internal email address. Your mail server then relays the email to its final destination. This is bad because people can in effect use your mail server to send out mail.

What you want is to control this behaviour, you want to:

  • receive mail for hosts defined in /var/qmail/control/rcpthosts
  • send mail to external hosts if it originates from the localhost
  • send mail to external hosts from an allowed host
  • reject mail if none of the above criteria are satisfied

I consulted Michael Samuel's also How to Configure Qmail to be a Selective Relay page for my Qmail setup.

Create a rcpthosts file

The file /var/qmail/control/rcpthosts specifies which recipient hosts are allowed. If this file is blank, your mail server will be an open relay, and relay messages from anyone to anyone. If there are hosts in this file, then mail will only be relayed to those hosts.

This is what you would normally have (pretty much the same as in your /var/qmail/control/locals):

foo.bar
mail.foo.bar

The next step is to somehow tell Qmail to send mail to hosts other than these IF the email was sent from an allowed host. This is where we call on help from TCP Server.

Download and Build TCP Server

We will use the TCP Server to Qmail it's okay to relay messages if the message comes from an allowed host. First we have to download and build it.

Download it ucspi-tcp-0.84.tar.gz (or whatever is the latest) from ftp://koobera.math.uic.edu/pub/software/, and save it somewhere like /usr/src. Then:

$ cd /usr/src
$ tar -zxf ucspi-tcp-0.84.tar.gz
$ cd ucspi-tcp-0.84
$ make
$ make man
$ make setup

Create TCP Rules

Next, we must create a rules file that specifies what systems to allow. Create the file /etc/tcprules.smtp with the similar contents:

192.:allow,RELAYCLIENT=""
127.:allow,RELAYCLIENT=""
:allow

This allow messages originating from hosts 192.*.*.* and 127.*.*.* to be relayed anywhere. For messages from all other hosts, the defaults in rcpthosts apply.

Compile TCP Rules

Now we have to compile these rules into a file /etc/tcprules.smtpd.cdb:

$ cat /etc/tcprules.smtp | \
  tcprules /etc/tcprules.smtp.cdb ~/tcprules.smtp.tmp

 

Starting Qmail

Finally, all the configuration is done! It's time to get Qmail started. If you are using the RPM, it comes with a /etc/rc.d/init.d/qmail script. You have to modify this to make things work with the selective relaying. This is my sample qmail script, you can copy the contents overtop the existing one.

Next, look in your /etc/inetd.conf. Comment out the line starting with smtp (put a # infront of it) as we will no longer be starting Qmail from inetd. It will be started up and run as a daemon by our qmail script.

Here's for the final moment, execute this commands to start Qmail:

$ /etc/rc.d/init.d/qmail start

Now let's test if it worked:

$ telnet localhost smtp

If everything went well, you should see something like:

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 mail.foo.bar ESMTP

Press Ctrl-] and type close to quit your telnet session.

 

Previous

Next

Web Database Mini-Howto by Ying Zhang (ying @ zippydesign dot com)