Home Menu

Email Services


Contents


Introduction

Linux mail is generally based on the Simple Mail Transfer Protocol (SMTP rfc 821). Setting up your system for mail involves several steps: installing and configuring an SMTP server; setting up mail aliases; installing mail clients for users; setting up POP (Post Office Protocol) if necessary and managing security and convenience issues.

We will concentrate on the sendmail SMTP mail server. It is probably the most common server, and it has been thoroughly debugged, hacked, cracked and hopefully fixed.

The sendmail daemon waits for either internal or external mail activity. When you mail something internally, the mail client attempts to get sendmail to deliver it immediately. If it can't for some reason, it is spooled to /var/spool/mqueue. Sendmail wakes up occassionally and attempts to resend anything in this queue up to some maximum number of tries or amount of time and then it sends a message back to the sender. When something is mailed to a user on your host, it contacts sendmail on (typically) port 25 and negotiates the transfer of the mail. It is spooled in /var/spool/mail/username. The mail client (pine, mush, mail, kmail, ad infinitum) looks in this file to get mail that has been sent for a given user. Some clients (e.g. kmail, evolution) move the mail from that location to a location under the users directory, others don't.

An email message has three parts:

The envelope determines where a message will be delivered and who sent it. The sender address in the envelope determines where a reply will be sent. The headers contain information about when the message was sent and what transport agents handled the message. Typically, the headers also contain the sender and receiver addresses, and they do not necessarily have to agree with the envelope. Finally, the body contains the message.

SMTP is an ASCII-based protocol with the following command set which is described in RFC 821 and RFC 822:

EMSTP is the Extended version of SMTP that handles non-text types, like audio, images and video. The characteristics of ESMTP are described in RFC 1869.


Installation

Sendmail may be automatically installed when you install Linux, and if installed, may run when you boot your machine. If you haven't been using it, you should have disabled the daemon. If you don't have it, locate the sendmail rpms on your install CD set or on the web and install them, or use apt or up2date. sendmail requires multiple rpms, so make sure that you get them all.

These are the required rpms, but you should also install

and there is a development rpm as well which you don't need unless you are going to make contributions to the sendmail development branch. If you want to know more about sendmail, go to sendmail.org.

Note, make sure you uninstall any previous versions of these packages if you are going to upgrade. rpm -q sendmail will tell you if you already have installations. rpm -e package-name will uninstall them. Or you can do an upgrade with rpm -Uvh package-name

Running sendmail

If you have sendmail, put a "ps ax | grep sendmail" shows no process, try chkconfig --list sendmail to see the current status. If it isn't running, you can go to /etc/rc.d and make the appropriate changes, or try:

There are options that you can add to the script in /etc/rc.d/init.d where it runs sendmail with the -bd option. For example, you can add a number to determine how often the mail queue should be processed. Typical times are 15 to 60 minutes.


sendmail Configuration

sendmail is configured with with the files in the directory /etc/mail/sendmail.cf, but it references a number of other configuration files. This file is widely reported to be the most complicated configuration file of all time, but that's probably just legend. However, several books have been written about it. Fortunately, the need to modify it comes along very seldom. For your information, the sendmail.cf format came from Eric Allman who wanted to practice with production systems (as in context-free languages). He intended to simplify it, but it has turned out to be a very powerful, albeit difficult to understand, configuration tool.

When in doubt about anything, go to sendmail.org for help.

The sendmail.cf file is typically around 1200 lines long, but a sample of the file structure is shown in the file of fragments.

Normally you wouldn't modify this file directly, but the basics of the syntax are included here.

sendmail-cf

So how do you get sendmail configured? There are a set of tools and files that are normally in /usr/share/sendmail-cf (or possibly /usr/lib/sendmail-cf on some systems). A typical directory structure is:

The configuration files are located in the cf directory as files with a .mc suffix. The other directories contain specific information regarding configuration which are processed with the ".mc" files to produce the sendmail.cf files. The m4 macro preprocessor is used to do the processing. The redhat.mc file specifies the configuration that you might expect on a Red Hat Linux system.

The m4 macros that are commonly used in the mc files are:

There are also some important macros that are used to include other files. The more important of these are:

FEATURES


Feature m4 files are found under the feature subdirectory. These are typically simple files specifying defines for sendmail.cf or file locations. An example is the acceptable_unqualified_senders.m4 file.

Here is a partial list of features and most will be discussed later.

Configuration Options


There are quite a number of configuration options which are normally included as defines or undefines, depending on the desired configuration. They are normally included as definitions like this:

Note that these need to be quoted as they are m4 variables and arguments with commas have to be double-quoted (``''). These seldom need to be changed, but they may need to be modified if your configuration is different from the default template.

For example,

To create your own .cf file,

  1. rename the cf file under /usr/share/sendmail-cf to something else, such as cf-examples.
  2. Create your own cf directory
  3. Copy the Makefile and Build files from your cf-examples file to your own cf directory.
  4. If you want to start with a template, copy one of the .mc templates from the examples directory.
  5. Make any modifications that you desire in the .mc file,
  6. Build a new .cf file with:

  7. Make a copy of /etc/sendmail.cf
  8. Copy the resulting new file - yourfile.cf. to /etc/sendmail.cf
  9. Restart sendmail and try it out.

It has become common on Red Hat systems to copy the .mc used to /etc/mail/sendmail.mc, apparently so that there is not doubt about which one to use. In the event that you need to create a new .cf file, it can be done in the /etc/mail directory using m4.

To have complete knowledge of the sendmail system requires a signficant amount of study and practice, more than we have time for. You can do quite a bit just with the configuration files below, and the FEATURE options.


/etc/mail Files

The files in /etc/mail are of two types. Those created by system administrators to control the mail system, and the database files created by makemap and newaliases when sendmail is started (in the rc script that starts sendmail, there are calls to makemap and newaliases).

/etc/aliases

The alias file can also contain redirect information. If FEATURE(`REDIRECT') is present in the configuration file, the access file can contain records like this:

The message is returned to the sender with the new address to try. The message is not forwarded. If you want to forward, you can do that locally with the .forward file.

/etc/access

/etc/domaintable

/etc/mailertable

/etc/relay-domains

/etc/sendmail.cw

/etc/virtusertable, /etc/genericstable


Setting Up DNS for sendmail

When setting up email domains, they must be entered into to appropriate DNS table with an MX record. For example, suppose you are running an ISP that provides services to small companies and your mail servers are mail1.whoopee.com and mail2.whoopee.com. A shoe company buys your service, which includes mail service; you set up a domain, smellyfeet.com, and point the DNS domain server to your DNS server. In the table for smellyfeet.com, you will need to provide an entry of the form: Two mail servers provide redundancy, and the precedence values guarantee that mail1 will always to be used if it is available.


Solving Specific Problems

Masquerading

Sometimes, it is desirable to masquerade mail as coming from a mail domain that is different than the actual sending domain. This is accomplised by adding the following line to the .mc file and rebuilding sendmail.cf Which would cause mail of the form user@localaddress to be sent as user@clowns.org. Normally, this entry is accompanied by several entries that restrict masquerading for certain local users:

Masquerading controls outgoing mail headers. If you want to extend the address changes to the local recipients as well, use:

If you want to extend the addressing to the envelope as well as the header, add:

If you want to provide virtual mail hosting, you need to add:

Which allows you to list domains that you want to masquerade. In conjuction with this, you might want to use the limited_masquerade feature, which specifies other domain that will be masqueraded.

This feature works in conjuction with the genericstable where the domains are listed.

Relaying

Normally, its not a good idea to relay mail because it allows your host to become an unwitting partner in things like email storms. sendmail-8.9 turns relaying off by default. However, if you are going to behave as a POP server, or if you have subdomains for which your server is the mail server, you will have to allow such activities. Also, if you have another host which is the mail server, then you will have to relay local mail to that server.

If you want to relay all local mail to another server, you should make the following entries in the .mc file:

If there are some users that shouldn't be forwarded, such as root, add the following:

If you want to relay all mail (local and otherwise) to a central server, use:

In general, you should never do this!!

MAIL_HUB applies to all addresses qualified with the name of the local host, while LOCAL_RELAY applies to addresses that are unqualified. If you want both to be treated the same, set the smart_host feature.

The final step in relaying is to enter all domains that are allowed to relay in /etc/mail/relay-domains or tagged with RELAY in the access database. If you want to relay everything (not a good idea), set the promiscuous_relay feature.

Redirection

If you want to forward mail to users that have changed their mail addresses to another site, you can enable redirection with:

and then put the redirection information in the access file:

Anti-spam

Spam can be a major problem, much bigger than you think. First, you could become an unwitting spammer if you allow mail relay. Spammers find such sites and forward a single message with thousands of recipients. Your machine and network pays the price.

Also, spammers probe systems for email addresses, trying to find legitimate addresses to mail to. In some cases, they have a list of common names and they use the sendmail VRFY command to test the addresses with your sendmail server. If it responds positively, they have acquired a new address to send to. This could amount to thousands of probes if they test many names and combinations of names.

Probably the first step is to enable the access database and then reject or discard mail from the domains that seem to be a problem.

Also, sendmail supports a variety of privacy options that allow you to determine how much information another site can glean from your server. Primarily, these restrict the use of the various query commands and the access to the mail spool queue.

where the options are:
publicNo privacy or security checking
needmailheloRequires identification from remote host (HELO)
noexpnDoes not allow the EXPN command (Expand Recipient List)
novrfyDoes not allow the VRFY command (Verify user name)
needexpnheloWill not execute EXPN without HELO
needvrfyheloWill not execute VRFYwithout HELO
noverbNo verbose mode for EXPN
restrictmailqOnly mqueue group allowed to see queue
restrictqrunOnly mqueue group allowed to run queue
noetrnDoes not allow asynchronous queue runs as when a dial up user wants to receive mail directly.
authwarningsAdd authentication warning (the default)
noreciptsTurns off delivery status notification
nobodyreturnNo message body in DSN (Delivery Status Notification), and no successful DSN returns.
goawayDisables all SMTP queries (VRFY, EXPN, etc.)

For example, the following define is probably reasonable:

To limit spam, you need to identify the domains that might be responsible (unless you let the mail clients take care of it) and enter them into the /etc/mail/access file. One interesting option is to use a list called the REALTIME BLACKHOLE LIST maintained by the MAPS project. If you want to use it, add feature dnsbl to the mc file.

Mail from any site in the MAPS project database will be rejected by your machine. You can add other lists by including a URL.

You can also create your own blacklist with:

which supports 550 type of entries in the access file:

ArtFern@weasel.com 550 Mail not allowed
nobody@550 This user not allowd
.spammer.com550 Not allowed

It is possible to perform header checking on email to further improve anti-spam filtering. This is beyond the scope this course, but would involve using the LOCAL_CONFIG AND LOCAL_RULESETS macros in the .mc file.

Denial of Service Attacks

Privacy

Mailing Lists


Troubleshooting

mailq

Reports information on the messages in the mail queue. This is useful in determining if your mailer is having difficulty delivering messages. The only option is -v, which will give the priority of the message and if delivery has already been attempted.

Verbose Delivery

Most mailers support a -v option that will provide information on the progress of the mail delivery. This will include the commands given by the mailer and the responses from the other end.

mailstats

sendmail can be configured to collect statistics on the number and size of messages it handles. The mailstats command can then be used to view the data. In the OSTYPE file, the confSTATUS_FILE option specifies the name of the file to use (/etc/mail/statistics is the default) and enables the function.

The output of mailstats has the following format:

sendmail -bt

The sendmail configuration can be tested by executing sendmail with the -bt option. This puts sendmail in interactive mode, and you can test various rewriting rules. If you enter an address like this: you will see the address rewriting process and the final output. You have to know the various rulesets that are involved. Typically, these are:
0address parsing
1sender rewriting
2recipient rewriting
3domaintable translation
4final output post-rewriting
5post alias expansion rewriting
10Envelope sender rewriting
20Envelope recipient rewriting
30Header sender rewriting
40Header recipient rewriting
50Add local domain
51convert pseudo-domain addresses to real domain addresses

sendmail -dx.y

This will cause sendmail to run with debugging output coming to the console (or screen). x sets the debugging level; y need not be specified. Some useful choices are:

Interactive sendmail Commands

Use Ctrl-D to terminate sendmail. Other commands are:
  
? this help message.
.Dmvalue define macro `m' to `value'.
.Ccvalue add `value' to class `c'.
=Sruleset dump the contents of the indicated ruleset.
=M display the known mailers.
-ddebug-spec equivalent to the command-line -d debug flag.
$m print the value of macro $m.
$=c print the contents of class $=c.
/mx host returns the MX records for `host'.
/parse address parse address, returning the value of crackaddr, and the parsed address (same as -bv).
/try mailer addr rewrite address into the form it will have when presented to the indicated mailer.
/tryflags flags set flags used by parsing. The flags can be `H' for Header or `E' for Envelope, and `S' for Sender or `R' for Recipient. These can be combined, `HR' sets flags for header recipients.
/canon hostname try to canonify hostname.
/map mapname key look up `key' in the indicated `mapname'.
rules addr run the indicated address through the named rules. Rules can be a comma separated list of rules.

Testing the Database Files

If you modify one of the /etc/mail database files, you can use the sendmail -bt command to test the configuration. In this case, you use the /map command like this:

Logging

sendmail logs through syslog using facility mail with levels "debug" through "crit". Option confLOG_LEVEL determines what level is reported. (debug means most everything, crit means only the critical stuff).


Security

Security for sendmail involves several issues:

Built-in Security Measures

Much of what has been discussed previously relates to security in one form or another. sendmail also has features that prevent the use of .forward or aliases files that are world writable. If you want to override these features, set the confDONT_BLAME_SENDMAIL option. The name should be a reminder that this isn't particular smart.

File Ownership and Permissions

There are three user accounts that are important to sendmail:

These parameters are set with:

All sendmails mailers run as DefaultUser unless the mailer flags specify otherwise. If a "mailnull" or "sendmail" user exists, it will be the DefaultUser, otherwise it is UID=1 and GID=1 (normally bin or daemon). "mailnull" is preferred, so it should be added to the passwd file with a "star" passwd and group "nogroup". This user should own no files or directories.

TrustedUser can own maps and files and is by default root, but it can be changed.

TRUSTED_USERS is used in determining what users can process mailing lists.

RunAsUser is the UID that sendmail runs under after opening its connection. sendmail must run as root initially, and by default, continues to run as root. If it is changed to some other username, that user must:

By default, the following ownership and permission settings are necessary:

Path Owner Perm Contents
/var/spool/mqueue RunAsUser 700 Mail queue directory
/,/var, /var/spoolroot755 Path to mqueue
/etc/mail/*TrustedUser644 Map and config files
/etc/mail/TrustedUser755 Maps directory
/etcroot755 Path to mail directory

If you run sendmail -v -bi, you can see if sendmail thinks that your file ownership and permissions are appropriate.

Secure Programs

Handling Forgeries

You can stop forged mail from eminating from your machine by installing and running identd. sendmail does a callback to identd to authenticate the user and get the real login name. This is then included in the message. identd (which should run by default) will return the name of the user that has a process running. On a single user machine, identd could be corrupted to return a bogus id, so don't rely on it.

Message Privacy


Examples

Simple

Here is a configuration file (.mc) for a small system that also provides hosting for a limited number of domains:

Simple Example

Hosting Example


procmail

procmail is a mail processor that can be invoked to perform operations on mail or mailboxes. procmail is controlled by rc files ($HOME/.procmailrc) for users and /etc/procmailrc for global use. procmail can be invoked by the mail handler (e.g. sendmail), standalone by a system administrator or by a cron job, or by the .forward file in a users home directory. The mail will be piped through procmail where it performs the desired functions. To invoke from a .forward file,

Unfortunately, your .forward file must be world-readable to do this, and as we know, that can be dangerous.

If you want to invoke procmail to run over an existing file, directly or using cron, the recommended script is:

Sendmail can be configured using the M command in the .cf file, or using the MAILER option in the .mc file. This causes sendmail to use procmail to do the actual delivery of mail to the user (to the users local mailbox).

When procmail is called to do the delivery, it looks first to /etc/procmailrc for processing rules. After this, it looks for $HOME/.procmailrc for rules and the combination of the two provide the complete ruleset.

procmail recipes


Other Servers

While sendmail can do anything you might want to do, it is also complex and susceptible to exploits (a problem that will get better with time). Other MTA's you might try are Postfix (www.postfix.org) and qmail (www.qmail.org). Both of these are more simple than sendmail and Postfix in particular is noted for being reliable. Postfix requires that you not have sendmail installed.

Both of these provide the essential services and many of the advanced features of sendmail in a different form. Both tend to follow the Unix-style of small components working together rather than a large monolithic system.


Spam and Virus Control

The ability to control spam and viruses that might arrive in email has become extremely important. Products such as spamassassin and AMiViS can be used to reduce or solve the problem, but you need to integrate these with your mail system. Sendmail has an plugin architecture that makes this relatively easy. The plugin structure is actually handled by a separate daemon named Milter. When Milter is running, it manages the plugins and sendmail communicates with the milter daemon for services.

For example, suppose you want to use spamassassin with sendmail. You need to find a Milter plugin for spamassassin, and as it turns out, there are several at this site We will look at MIMEDefang because it has been around a while and has other uses. You can download MIMEDefang at the MIMEDefang website. MIMEDefang is written in Perl, so you can install the entire thing as Perl modules from the site or from CPAN.

After the repository is downloaded, untar it in some neutral location and then look at the README file. It mentions an excellent site for documentation at this site. Once you have MIMEDefang installed, you install spamassassin by downloading from the spamassassin site and following the installation instructions. You can also find rpm packages for spamassassin.

All of this is left as an interesting exercise, and you will note that there are other ways of getting a spam filter.

Now why use MIMEDefang instead of some other solution? Because MIMEDefang has a built in virus scanner for you email. You can configure MIMEDefang to handle mail attachments in various ways that will make it much easier to avoid viruses, and turn your Linux box into a mail filter for your Windows systems.


Assignment

Due:
  1. Create a mail alias for two users on your host and test it to see that it works. Also, test it using the sendmail -bt command.
  2. Using the access database, add your esus name to the list of users that cannot send mail to your machine. Then try it and see what happens. Do this with REJECT, then with 501 or 550 to see what the difference is on esus (see what comes back).
  3. Using the access database, reject all mail from cs.montana.edu and try mailing from esus to see if it works.
  4. Modify your sendmail.cf file via the m4 tools. That is, go to /usr/share/sendmail-cf/cf and modify the .mc file. Enable the virtusertable and genericstable options.
  5. Set up your machine to masquerade as some domain and try sending mail to it. Note, this will require you to put an MX record in the zone file you created for you own special one machine domain, and you can only mail to it from your machine, or one that uses your machine to resolve addresses. Set up one or more users to receive mail for this domain and use the virtusertable and genericstable to map both incoming and outgoing mail for the user(s).


Home Menu