sendmailSearch this book
Previous: 11.5 Testing So FarChapter 11
Rule Sets 1 and S=
Next: 11.7 Rule Set 1

11.6 Handling user@thishost

The Hubset rule set next needs a rule that will take an address consisting of a username and a short hostname and will change only the host part. Consider an address of the form:


Here, the user part is what you already dealt with: a user's login name such as you. It is followed by an @ character and then the local host's name. One method of matching this form of address in the LHS would be to match the user, the @ character, and any hostname (recall that the $+ matches one or more tokens in the workspace):


This form is easy to use because the $+ would match any hostname at all and wouldn't require that you know all the possible names for the local host ahead of time. Unfortunately, you shouldn't use this approach, because you want to rewrite the sender's hostname only if it is that of the local machine. Using $+ would cause the sender's hostname to be rewritten whether or not it is the name of the local machine.

But how could the sender's address not be from the local machine? Recall that sendmail gets the sender address from one of four places: from the envelope, from the From: header, from the -f switch (see below), or from the uid of the process that ran sendmail.

Consider, for example, a Usenet news-posting program that posts news by sending mail. It may be desirable to have all posted news messages appear to be from the news program on the news server machine. One way to achieve this is by running sendmail with the -f switch:

-f news@news.server

Here, the -f switch causes sendmail to use the address specified on the command line as the address of the sender.

But in this example you would not want to change the address of the sender to appear as though it were from the hub. That would undo what news tries to do with the -f switch.

A better approach is to match the user, the @ character, and the specific local hostname:


Recall that $w was defined to be the short hostname of the local host. This LHS matches only a workspace (sender's address) that begins with a single user's login name ($-), followed by @, and then by the name of the local host ($w).

Add this new rule to the file:

SHubset # Rewrite the sender for the hub
R$-             $@ $1@${HUB}            user -> user@hub
R$-@$w          $@ $1@${HUB}            user@local -> user@hub      <- new

Notice that, other than their comments, the two rules differ only in their LHS. The flow through these rules is that the first tries to match a lone username in the workspace. If that match fails, the second rule tries to match the workspace. It matches only a workspace that contains the name of a user at the local machine. To observe this rule in action, run sendmail in rule-testing mode again:

% ./sendmail -d0.1 -bt

This time, we added the -d0.1 debugging command-line switch, which tells sendmail to print the identity of the local machine:

Version 8.8.4

============ SYSTEM IDENTITY (after readcf) ============
      (short domain name) $w = here
  (canonical domain name) $j =
         (subdomain name) $m =
              (node name) $k = here

ADDRESS TEST MODE (ruleset 3 NOT automatically invoked)
Enter <ruleset> <address>

Note that sendmail, in this example, defines $w as here, the name of the local machine. Your local machine name will, of course, differ. Fortunately, you don't have to know what that name is to design a new rule. Simply use $w, and let sendmail do all the work.

Now give sendmail rule sets 3 and Hubset as you did before, but this time specify a sender's address that contains a user and a host part:

> 3,Hubset you@here
               the same as appeared in $w

The user part can be any login name. The host part must be the text in the $w macro (displayed when you just ran sendmail with the -d0.1 debugging command-line switch above).

rewrite: ruleset  3   input: you @ here
rewrite: ruleset  3 returns: you @ here
rewrite: ruleset 199   input: you @ here
rewrite: ruleset 199 returns: you @ mail . us . edu

As intended, Hubset, the custom rule set for rewriting the sender's address for the hub delivery agent, made the local address appear to be from the hub.

Note, however, that you cannot specify macros when testing addresses; that is, the following does not work:

> 3,Hubset you@$w

The sendmail program does not recognize macros in addresses. They aren't recognized because macros in rule sets are expanded when the configuration file is read, not when sendmail reads addresses. The command above results in the following erroneous output:

rewrite: ruleset  3   input: you @ $w
rewrite: ruleset  3 returns: you @ $w
rewrite: ruleset 199   input: you @ $w
rewrite: ruleset 199 returns: you @ $w

Also note that the value in $w is the short hostname, here, instead of the fully qualified name Instead of adding an additional rule to test for the fully qualified name, we defer this problem to the next chapter.

Previous: 11.5 Testing So FarsendmailNext: 11.7 Rule Set 1
11.5 Testing So FarBook Index11.7 Rule Set 1