The Rewriting Rules Section

The Rewriting Rules section defines the rules used to parse e-mail addresses from user mail programs and rewrite them into the format required by the mail delivery programs. Rewrite rules match the input address against a pattern, and if a match is found, rewrite the address into a new format using the rules defined in the command.

The left side of a rewrite rule contains a pattern defined by macro and literal values and by special symbols. The right side of a rewrite rule defines the template used to rewrite addresses that match the pattern. The template is also defined with literals, macro values, and special symbols. Literals are simply literal string values. Macros are the macro and class values defined in the Local Info section of the file. The special symbols vary, depending on whether they are used in the left-side pattern or the right-side template. Table 5.1 lists the pattern matching symbols and Table 5.2 lists the template symbols.

Table 5.1: Pattern Matching Symbols



[email protected]

Match exactly zero tokens.


Match zero or more tokens.


Match exactly one token.


Match one or more tokens.


Match all tokens in macro variable x.


Match any token in class variable x.


Match any token not in class variable x.

Table 5.2: Rewrite Template Symbols




Insert the value from indefinite token n.


Terminate this rewrite rule.

[email protected]

Terminate the entire ruleset.


Call the ruleset identified as name.


Convert hostname to DNS canonical form.


Get the value from a database.

Rewrite rules divide e-mail addresses into tokens for processing. A token is a string of characters delimited by an operator defined in the OperatorChars option. The operators also count as tokens. Based on this, the address [email protected] contains three tokens: the string kathy, the operator @, and the string parrot.

The tokens from the input address are matched against the pattern. The macro and literal values are directly matched against values in the input address, and the special symbols match the remaining tokens. The tokens that match the special symbols in a pattern are identified numerically, according to their relative position in the pattern that they match. Thus, the first group of tokens to match a special symbol is called $1, the second is $2, the third is $3, and so on. These tokens can then be used to create the rewritten address.

An example will clarify how addresses are processed by rewrite rules. Assume the input address is [email protected]

Assume the current rewrite rule is

[email protected]$- $1<@$2.$D> [email protected] -> user<@host.domain>

The R is the rewrite command, [email protected]$- is the pattern against which the address is matched, and $1<@$2.$D> is the template used to rewrite the address. The remainder of the command line is a comment that is intended to clarify what the rule does.

The input address matches the pattern because

• It contains one or more tokens before the literal @, which is what the special symbol $+ requires. The token that matches the special symbol is the string kathy. This token can now be referred to as $1 because it matched the first special symbol.

• It contains an @ that matches the pattern's literal @.

• It contains exactly one token after the @, which is what the $- requires. The token that matches this special symbol is the string parrot, which can now be referenced as $2 because it matched the second special symbol.

The template that rewrites the address contains the token $1, a literal string <@, the token $2, a literal dot (.), the value stored in macro D, and the literal >. You know that $1 contains kathy, and $2 contains parrot. Assume that the macro D was defined elsewhere in the file as Given these values, the input address is rewritten as kathy<>

A rewrite rule may process the same address several times because after being rewritten, the address is again compared against the pattern. If it still matches, it is rewritten again. The cycle of pattern matching and rewriting continues until the address no longer matches the pattern. In our example, when the address is again compared to the pattern after rewriting, it fails to match the pattern a second time because it no longer contains exactly one token after the literal @. In fact, it now has six tokens after the @: parrot, ., foobirds, ., org, and >. So no further processing is done by this rewrite rule, and the address is passed to the next rule in line.


Individual rewrite rules are grouped together in rulesets, so that related rewrite rules can be referenced by a single name or number. The S command marks the beginning of a ruleset; and identifies it with a name, number, or both. Therefore, the command S4 marks the beginning of ruleset 4, SLocal_check_mail marks the beginning of the Local_check_mail ruleset, and Scanonify=3 defines the beginning of the canonify ruleset, which is also known as ruleset 3.

Five rulesets are called directly by sendmail to handle normal mail processing:

• Ruleset canonify, also known as ruleset 3, is called first to prepare all addresses for processing by the other rulesets.

• Ruleset parse, also known as ruleset 0, is applied to the mail delivery address to convert it to the (mailer, host, user) triple, which contains the name of the mailer that will deliver the mail, the recipient hostname, and the recipient username.

• Ruleset sender, also known as ruleset 1, is applied to all sender addresses.

• Ruleset recipient, also known as ruleset 2, is applied to all recipient addresses.

• Ruleset final, also known as ruleset 4, is called last to convert all addresses from internal address formats into external address formats.

There are three basic types of addresses: delivery addresses, sender addresses, and recipient addresses. A recipient address and a delivery address sound like the same thing, but there is a difference. As the mailing list alias illustrated, there can be many recipients for a piece of mail, but mail is delivered to only one person at a time. The recipient address of the one person to which the current piece of mail is being delivered is the delivery address. Different rulesets are used to process the different types of addresses.

Figure 5.1 shows the rulesets that handle each address type. The S and R symbols in Figure 5.1 represent rulesets that have names, just like the other rulesets, but the specific rulesets used are identified by the S and R fields of the mailer definition. Each mailer specifies its own S and R rulesets to process sender and recipient addresses in a manner required by the mailer.

Figure 5.1: sendmail rulesets The Mailer Definitions Section

The Mailer Definitions section defines the instructions used by sendmail to invoke the mail delivery programs. The specific rewrite rules associated with each individual mailer are also defined in this section. Mailer definitions begin with the mailer command (M). Searching through the Mailer Definitions section of the Red Hat configuration file for lines that begin with M produces the mailer definitions in Listing 5.5.

Listing 5.5: Sample mailer Definitions

Mlocal, P=/usr/bin/procmail, F=lsDFMAw5:/|@qSPfhn9,

S=EnvFromL/HdrFromL, R=EnvToL/HdrToL, T=DNS/RFC822/X-Unix,

Mprog, P=/usr/sbin/smrsh, F=lsDFMoqeu9,

S=EnvFromL/HdrFromL, R=EnvToL/HdrToL, D=$z:/, T=X-Unix/X-Unix/X-Unix, A=smrsh -c $u

S=EnvFromSMTP/HdrFromSMTP, R=EnvToSMTP, E=\r\n, L=990, T=DNS/RFC822/SMTP, A=TCP $h

S=EnvFromSMTP/HdrFromSMTP, R=EnvToSMTP, E=\r\n, L=990, T=DNS/RFC822/SMTP, A=TCP $h

S=EnvFromSMTP/HdrFromSMTP, R=EnvToSMTP, E=\r\n, L=990, T=DNS/RFC822/SMTP, A=TCP $h

S=EnvFromSMTP/HdrFromSMTP, R=EnvToSMTP, E=\r\n, L=990, T=DNS/RFC822/SMTP, A=TCP $h

S=EnvFromSMTP/HdrFromSMTP, R=MasqSMTP, E=\r\n, L=2040, T=DNS/RFC822/SMTP, A=TCP $h

Mprocmail, P=/usr/bin/procmail, F=DFMSPhnu9,

S=EnvFromSMTP/HdrFromSMTP, R=EnvToSMTP/HdrFromSMTP, T=DNS/RFC822/X-Unix, A=procmail -Y -m $h $f $u

The first two mailer definitions in Listing 5.5 are required by sendmail. The first of these defines a mailer for local mail delivery. This mailer must always be called "local." The P argument defines the path to the local mailer. In this configuration, procmail is used as the local mailer. The second definition is for a mailer that delivers mail to programs, which is always called "prog". The P argument points to a program named smrsh, which is the Sendmail Restricted Shell—a special shell program specifically for handling mail. sendmail expects to find both of these mailers in the configuration, and requires that they be given the names "local" and "prog". All other mailers can be named anything the system administrator wishes. However, in practice, that is not the case. Because the files on Linux systems are built from the same m4 macros, they use the same mailer names

The next five mailer commands define mailers for TCP/IP mail delivery. The first one, designed to deliver traditional 7-bit ASCII SMTP mail, is called smtp. The next mailer definition, which is for Extended SMTP mail, is called esmtp. The smtp8 mailer definition handles unencoded 8-bit SMTP data. The dsmtp mailer provides support for on-demand SMTP, which is special form of SMTP in which the recipient downloads mail instead of the normal case in which the sender initiates mail transfer. Finally, relay is a mailer that relays TCP/IP mail through an external mail relay host. Of these, only esmtp, which is the default mailer, and relay are actually used anywhere in the basic configuration.

The last mailer definition in Listing 5.5 is for procmail. procmail (covered in Chapter 11) is an optional mailer found on most Linux systems. The A argument in this definition invokes procmail with the -m command-line argument, which allows procmail to be used for mail filtering. Like most of the SMTP mailers, this mailer is not used anywhere in the basic file. These unused definitions provide a complete set of mailers, but they are not needed for most configurations.

Examining any one of the mailer entries, such as the entry for the smtp mailer, explains the structure of all of them:

M Beginning a line with an M indicates that the command is a mailer definition.

smtp Immediately following the M is the name of the mailer, which can be anything you wish. In this sample, the name is smtp.

P=[IPC] The P argument defines the path to the program used for this mailer. In this case, it is [IPC], which means this mail is delivered by sendmail. Other mailer definitions, such as local, have the full path of some external program in the field.

F=mDFMuX The F argument defines the sendmail flags for this mailer. Other than knowing that these are mailer flags, the meaning of each individual mailer flag is of little interest because the flags are correctly set by the m4 macro that builds the mailer entry. In this example, m says that this mailer can send to multiple recipients at once; DFM says that Date, From, and Message-ID headers are needed; u says that uppercase should be preserved in hostnames and usernames; and X says that message lines beginning with a dot should have an extra dot prepended.

S=EnvFromSMTP/HdrFromSMTP The S argument defines the S rulesets illustrated in Figure 5.1. The rulesets can be different for every mailer, allowing different mailers to process e-mail addresses differently. In this case, the sender address in the mail "envelope" is processed through ruleset EnvFromSMTP, also known as ruleset 11, and the sender address in the message is processed through ruleset HdrFromSMTP, also known as ruleset 31. (You'll see more on this later when a sendmail configuration is tested.)

R=EnvToSMTP The R argument defines the R ruleset shown in Figure 5.1. This value can be different for every mailer to allow each mailer to handle addresses differently. The sample mailer processes all recipient addresses through ruleset EnvToSMTP, also known as ruleset 21. Only one ruleset is used for the R argument with the smtp mailer; however, it is possible to specify two different rulesets, one for envelope processing and one for header processing, in exactly the same way as two rulesets were defined for the S argument.

E=\r\n The E argument defines how individual lines in a message are terminated. In this case, lines are terminated with a carriage return and a line feed.

L=990 The L argument defines the maximum line length for this mailer. This mailer can handle messages that contain individual lines up to 990 bytes long.

T=DNS/RFC822/SMTP The T argument defines the MIME types for messages handled by this mailer. This mailer uses DNS for hostnames, RFC822 e-mail addresses, and SMTP error codes.

A=TPC $h The A argument defines the command used to execute the mailer. In this case, the argument refers to an internal sendmail process. (Note that TCP and IPC can be used interchangeably.) In other cases (the local mailer is a good example), the A argument is clearly a command line.

The mailer definitions that come with your Linux system will include local, prog and the SMTP mailers. These are the correct mailer definitions to run sendmail in a TCP/IP network environment.

Was this article helpful?

0 0

Post a comment