21.2 How sendmail Uses DNSThe sendmail program uses DNS in four different ways:
We discuss each of these uses individually later in this chapter. 21.2.1 Determine the Local Canonical NameAll versions of sendmail use more or less the same logical process to obtain the canonical name of the local host. As illustrated in the sample program below, sendmail first calls gethostname (3) to obtain the local host's name. That name may either be a short name or a fully qualified one depending on which comes first in the /etc/hosts file. If the call to gethostname (3) fails, the name of the local host is set to localhost :
#include <sys/types.h> #include <sys/socket.h> #include <sys/param.h> #include <netdb.h> #include <stdio.h> main() { char hostbuf[MAXHOSTNAMELEN]; struct hostent *hp; /* Get the local hostname */ if (gethostname(hostbuf, sizeof(hostbuf)) < 0) strcpy(hostbuf, "localhost"); printf("hostname = \"%s\"\n", hostbuf); /* canonicalize it and get aliases */ if((hp = gethostbyname(hostbuf)) == NULL) perror("gethostbyname"), exit(2); printf("canonical = \"%s\"\n", hp->h_name); while (*hp->h_aliases != NULL) { printf("alias: \"%s\"\n", *hp->h_aliases); ++hp->h_aliases; } } The local hostname is then given to the gethostbyname routine (see Section 37.5.186, -d61.10 ) to obtain the canonical name for the local host. That same routine also returns any aliases (other names for the local host). On some Sun and Ultrix machines that are set up to use NIS services, the canonical name is the short name, and a fully qualified name that should have been the canonical name appears as an alias. For such systems you must link with the BIND library ( libresolv.a ) when compiling this program or compiling sendmail . That library gets its information from DNS rather than from NIS. But note that V8.7 and above versions of sendmail do the intelligent thing and use the canonical name that was found as the aliases if it exists.
If a good BIND library is not available, or if it is not convenient
to compile and install a new version of
sendmail
,
you can circumvent the short name assigned to
Dm your domain here Dj$w.$m
The
canonical name
is your site's hostname with a dot and
your domain name appended. These two lines cause
The canonical name found by
gethostbyname
(3) is assigned as the
value of the
The result of all these lookups can be viewed by running
sendmail
with a 21.2.2 Look Up a Remote Host's NameWhen sendmail begins to run as a daemon, it creates a socket, binds to that socket, and listens for incoming SMTP connections. When a remote host connects to the local host, sendmail uses the accept (2) library routine to accept the connection. The accept (2) routine provides the IP address of the remote machine to sendmail . The sendmail program then calls gethostbyaddr (2) to convert that IP address to a canonical (official) hostname. The sendmail program needs the canonical hostname for four reasons:
If the 21.2.3 Look Up Addresses for DeliveryWhen sendmail prepares to connect to a remote host for transfer of mail, it first performs a series of checks that vary from version to version. All versions accept an IP address surrounded with square brackets as a literal address and use it as is. Beginning with V8.1, sendmail checks to see whether the host part of the address is surrounded with square brackets. If so, it skips looking up MX records. (We'll elaborate on MX records soon.)
Beginning with V8.8,
sendmail
first checks to see
whether the
If
sendmail
is allowed to look up MX records,
it calls the
res_search
(3) BIND library routine
[9]
to find all the MX records for the host.
If it finds any MX records, it sorts them in order of cost, selecting
the least cost first.
If V8
sendmail
finds two costs that are the same,
it randomizes the selection between the two when sorting.
[10]
After all MX records are found, or if
no MX records were found,
sendmail
adds the host
specified by the
The
sendmail
program then tries to deliver the message to each
host in the list of MX hosts,
one at a time, until one of them succeeds or until they all fail.
Beginning with V8.8
sendmail
, any host in the list that returns a
If no MX records are found,
sendmail
tries to deliver
the message to the single original host.
If all else fails,
sendmail
attempts to deliver to the host
listed with the Whether sendmail tries to connect to the original host or to a list of MX hosts, it calls gethostbyname (2) to get the network address for each. It then opens a network connection to that address and attempts to send SMTP mail. 21.2.4 The $[ and $] Operators
The Each lookup is actually composed of many lookups that occur in the form of a loop within a loop. In the outermost loop, the following logic is used:
Each lookup described above is performed by using the following steps:
Each query searches the data returned as follows:
All this apparent complexity is necessary to deal with wildcard MX records (see Section 21.3.4, "Wildcard MX Records" ) in a reasonable and successful way. |
|