Hurricane Electric

Understanding sendmail.cf in one page

That's a program!


Addresses Are Tokenized

The fundamental concept in sendmail is that addresses are tokenized and kept in a workspace, on which all rules work. For example:

sales@mailtest.he.net

is tokenized to:

"sales" "@" "mailtest" "." "he" "." "net"

(The quotes are merely there to indicate tokens)

Rulesets

Rules are colled into rulesets. Rulesets start with a line starting with S which is then followed by lines starting with R. Example:
S4
R$* <@>                 $@                          handle <> and list:;
R$* < @ $+ . > $*       $1 < @ $2 > $3
R$* < @ *LOCAL* > $*    $1 < @ $j > $2
R$* < $+ > $*           $1 $2 $3                    defocus
R@ $+ : @ $+ : $+       @ $1 , @ $2 : $3             canonical
R@ $*                   $@ @ $1                     ... and exit
R$+ % $=w @ $=w         $1 @ $j                     u%host@host => u@host
Rules in a ruleset are invoked sequentially until either the end of the ruleset or a special operator is used to cause the ruleset to terminate.

Rules

Rules specify a pattern to match against the workspace and a substitution to replace the workspace with if the match succeeds.

Rpattern   tab  substitution  tab  comment
The pattern, substitution, and comment must be separated by a tab. They may contain spaces.

Rules are invoked repeatedly until it is either false or a special operator is used to cause the rule to terminate.

Given the workspace:

"sales" "@" "mailtest" "." "he" "." "net"

the rule:

R$+@$+        orders @ $2

will rewrite the workspace to:

"orders" "@" "mailtest" "." "he" "." "net"

If you are familiar with perl you might think of a rule being analogous to the following:

$line =~ s/pattern/substituion/;
The only difference being is that perl pattern matching operates at the character level and sendmail pattern matching operates at the token level.

Patterns

abc			match the string literal abc
@ < > .			more literals (no special meanings)
$*			match zero or more tokens
$+			match one or more tokens
$=w			pattern match one token against class
			(defined with Cw or Fw)
$~w			pattern match one token against class
			(defined with Cw or Fw)

Substitutions

abc			insert string literal abc
$1			insert first matched symbol
$2			insert second matched symbol, and so on.
$(domaintable $2 $)	a lookup using a database file.
$[ $1 $]		canoncalize (lookup using DNS) token $1
$>96 $1			call ruleset 96 with workspace $1

Only valid at the start of the substitution.

$:			check this rule once
$@			check this rule once
			and exit ruleset if true.
$#			set mailer information.
			warning: changes meaning of $: and $@
			when encountered later in same rule.

Macros

The line
DMxyz
defines the macro $M to be equal to "xyz" which can be used in substituions or patterns.

Classes

A class is a list of tokens. The line
Cwlocahost
sets the class $=w to "localhost". The line
Fw/etc/sendmail.cw
loads the class $=w from a file.

Delivery Agents

The local delivery agent is defined by the line Mlocal. A delivery agent may have special rules which are invoked for rewriting the sender (specified with S=) and recipient (specified with R=). For example
Mlocal,         P=/usr/bin/procmail, F=lsDFMAw5:/|@qShP, S=10/30, R=20/40,
                T=DNS/RFC822/X-Unix,
                A=procmail -a $h -d $u
The 10/30 specifies rules for rewritting the envelope/header. Rule S10 is to be invoked for the envelope and S30 for the header.

Standard Rules

Ruleset 3 is allways invoked by sendmail first. You can think of it a rule to generally clean up email addresses. This rule results in an email address.

Ruleset 0 is invoked to determine the delivery agent. This rule results in what is called a triple. A triple is just the information needed to decide on how the message is to be delivered.

Ruleset 2 is invoked next.

The ruleset specified by the R= parameter of the local delivery agent is inoked next.

Ruleset 4 is invoked last.

Testing

Rules can be tested by invoking sendmail like so:
sendmail -bt
You can then run your rules on an email address by typing them like so:
3 sales@mailtest.he.net
or

3,0,2,10,4 sales@mailtest.he.net

Back To Hurricane Electric's Home Page.