sendmail

sendmailSearch this book
Previous: 24.5 The Aliases DatabaseChapter 24
Aliases
Next: 24.7 Pitfalls
 

24.6 Prevent Aliasing with -n

At times it is desirable to run sendmail so that it does not perform aliasing. When aliasing is disabled, sendmail uses the recipient address as is. No addresses are ever looked up in the aliases file, even if they are local.

The -n command-line switch tells sendmail not to perform aliasing of recipient addresses. This switch is rarely used but can be handy in a couple of situations.

24.6.1 Is an Alias Bad?

In tracking down delivery problems, it can be difficult to determine where the problem lies. If you suspect a bad alias, you can force aliasing to be skipped and see whether that causes the problem to go away:

% /usr/lib/sendmail -n user < /dev/null

This tells sendmail to send an empty mail message (one containing mandatory headers only) to the recipient named user. The -n prevents sendmail from looking up user either in the aliases database or in ~/.forward. If user resolves to the local delivery agent, the message will be delivered, and you should therefore suspect an aliasing problem.

Other switches, such as -v (verbose) and -d (debugging), can be combined with -n to view the delivery process in more detail.

24.6.2 Filtering Recipients with a Shell Script

The -n command-line switch can also be used to suppress aliasing when delivering to a list of recipients that has already been aliased. For example, consider the following script, which attempts to restrict delivery to users who have mail delivered locally and to skip users who have mail forwarded offsite:

#!/bin/sh
EX_OK=0                   # From <sysexits.h>
EX_NOUSER=67              # From <sysexits.h>
EX_SOFTWARE=70            # From <sysexits.h>
if [ ${#} -ne 2 ]; then
        echo Usage: $0 list-name
        exit $EX_USAGE
fi
trap "exit 70" 1 2 13 15
LIST= "`/usr/lib/sendmail -bv $1 \  
        | grep "mailer local" 2>&1`" \ 
        | sed 's/\\.\\.\\..*$//'
if [ -z "$LIST" ]
        echo "$1 expanded to an empty list"
        exit $EX_NOUSER
fi
if /usr/lib/sendmail -n $LIST >/dev/null 2>&1
then
        exit $EX_OK
fi
exit $EX_SOFTWARE

The sendmail program is called twice inside this script. First, it is given the -bv switch, which causes it to expand the list of recipients in $1. That expansion includes aliasing (and ~/.forward aliasing) for each name in the list. The output produced looks like this:

user1... deliverable: mailer local, user user1
user2@otherhost... deliverable: mailer smtp, host otherhost, user user2@otherhost

The grep(1) program selects only those lines that contain the expression "mailer local", thus indicating a local user. The sed(1) program then discards from the ... to the end of each selected line. The result, a list of local recipients only, is saved in the shell variable LIST.

The sendmail program is called with the -n switch, which prevents it from re-aliasing the list of names in $LIST (they have already been aliased once).

Note that this script should not be used as is because it checks only for the delivery agent named local, rather than for any delivery agent that can perform final delivery.


Previous: 24.5 The Aliases DatabasesendmailNext: 24.7 Pitfalls
24.5 The Aliases DatabaseBook Index24.7 Pitfalls