📄 domain.c
字号:
/*
* Copyright (c) 1998-2000 Sendmail, Inc. and its suppliers.
* All rights reserved.
* Copyright (c) 1986, 1995-1997 Eric P. Allman. All rights reserved.
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the sendmail distribution.
*
*/
#include <sendmail.h>
#ifndef lint
# if NAMED_BIND
static char id[] = "@(#)$Id: domain.c,v 8.114.6.1.2.3 2000/06/13 18:00:08 gshapiro Exp $ (with name server)";
# else /* NAMED_BIND */
static char id[] = "@(#)$Id: domain.c,v 8.114.6.1.2.3 2000/06/13 18:00:08 gshapiro Exp $ (without name server)";
# endif /* NAMED_BIND */
#endif /* ! lint */
#if NAMED_BIND
# include <arpa/inet.h>
/*
** The standard udp packet size PACKETSZ (512) is not sufficient for some
** nameserver answers containing very many resource records. The resolver
** may switch to tcp and retry if it detects udp packet overflow.
** Also note that the resolver routines res_query and res_search return
** the size of the *un*truncated answer in case the supplied answer buffer
** it not big enough to accommodate the entire answer.
*/
# ifndef MAXPACKET
# define MAXPACKET 8192 /* max packet size used internally by BIND */
# endif /* ! MAXPACKET */
typedef union
{
HEADER qb1;
u_char qb2[MAXPACKET];
} querybuf;
# ifndef MXHOSTBUFSIZE
# define MXHOSTBUFSIZE (128 * MAXMXHOSTS)
# endif /* ! MXHOSTBUFSIZE */
static char MXHostBuf[MXHOSTBUFSIZE];
# ifndef MAXDNSRCH
# define MAXDNSRCH 6 /* number of possible domains to search */
# endif /* ! MAXDNSRCH */
# ifndef RES_DNSRCH_VARIABLE
# define RES_DNSRCH_VARIABLE _res.dnsrch
# endif /* ! RES_DNSRCH_VARIABLE */
# ifndef MAX
# define MAX(a, b) ((a) > (b) ? (a) : (b))
# endif /* ! MAX */
# ifndef NO_DATA
# define NO_DATA NO_ADDRESS
# endif /* ! NO_DATA */
# ifndef HFIXEDSZ
# define HFIXEDSZ 12 /* sizeof(HEADER) */
# endif /* ! HFIXEDSZ */
# define MAXCNAMEDEPTH 10 /* maximum depth of CNAME recursion */
# if defined(__RES) && (__RES >= 19940415)
# define RES_UNC_T char *
# else /* defined(__RES) && (__RES >= 19940415) */
# define RES_UNC_T u_char *
# endif /* defined(__RES) && (__RES >= 19940415) */
static char *gethostalias __P((char *));
static int mxrand __P((char *));
/*
** GETMXRR -- get MX resource records for a domain
**
** Parameters:
** host -- the name of the host to MX.
** mxhosts -- a pointer to a return buffer of MX records.
** mxprefs -- a pointer to a return buffer of MX preferences.
** If NULL, don't try to populate.
** droplocalhost -- If TRUE, all MX records less preferred
** than the local host (as determined by $=w) will
** be discarded.
** rcode -- a pointer to an EX_ status code.
**
** Returns:
** The number of MX records found.
** -1 if there is an internal failure.
** If no MX records are found, mxhosts[0] is set to host
** and 1 is returned.
*/
int
getmxrr(host, mxhosts, mxprefs, droplocalhost, rcode)
char *host;
char **mxhosts;
u_short *mxprefs;
bool droplocalhost;
int *rcode;
{
register u_char *eom, *cp;
register int i, j, n;
int nmx = 0;
register char *bp;
HEADER *hp;
querybuf answer;
int ancount, qdcount, buflen;
bool seenlocal = FALSE;
u_short pref, type;
u_short localpref = 256;
char *fallbackMX = FallBackMX;
bool trycanon = FALSE;
u_short *prefs;
int (*resfunc)();
u_short prefer[MAXMXHOSTS];
int weight[MAXMXHOSTS];
extern int res_query(), res_search();
if (tTd(8, 2))
dprintf("getmxrr(%s, droplocalhost=%d)\n",
host, droplocalhost);
if (fallbackMX != NULL && droplocalhost &&
wordinclass(fallbackMX, 'w'))
{
/* don't use fallback for this pass */
fallbackMX = NULL;
}
*rcode = EX_OK;
if (mxprefs != NULL)
prefs = mxprefs;
else
prefs = prefer;
/* efficiency hack -- numeric or non-MX lookups */
if (host[0] == '[')
goto punt;
/*
** If we don't have MX records in our host switch, don't
** try for MX records. Note that this really isn't "right",
** since we might be set up to try NIS first and then DNS;
** if the host is found in NIS we really shouldn't be doing
** MX lookups. However, that should be a degenerate case.
*/
if (!UseNameServer)
goto punt;
if (HasWildcardMX && ConfigLevel >= 6)
resfunc = res_query;
else
resfunc = res_search;
errno = 0;
n = (*resfunc)(host, C_IN, T_MX, (u_char *) &answer, sizeof(answer));
if (n < 0)
{
if (tTd(8, 1))
dprintf("getmxrr: res_search(%s) failed (errno=%d, h_errno=%d)\n",
(host == NULL) ? "<NULL>" : host, errno, h_errno);
switch (h_errno)
{
case NO_DATA:
trycanon = TRUE;
/* FALLTHROUGH */
case NO_RECOVERY:
/* no MX data on this host */
goto punt;
case HOST_NOT_FOUND:
# if BROKEN_RES_SEARCH
case 0: /* Ultrix resolver retns failure w/ h_errno=0 */
# endif /* BROKEN_RES_SEARCH */
/* host doesn't exist in DNS; might be in /etc/hosts */
trycanon = TRUE;
*rcode = EX_NOHOST;
goto punt;
case TRY_AGAIN:
case -1:
/* couldn't connect to the name server */
if (fallbackMX != NULL)
{
/* name server is hosed -- push to fallback */
if (nmx > 0)
prefs[nmx] = prefs[nmx - 1] + 1;
else
prefs[nmx] = 0;
mxhosts[nmx++] = fallbackMX;
return nmx;
}
/* it might come up later; better queue it up */
*rcode = EX_TEMPFAIL;
break;
default:
syserr("getmxrr: res_search (%s) failed with impossible h_errno (%d)\n",
host, h_errno);
*rcode = EX_OSERR;
break;
}
/* irreconcilable differences */
return -1;
}
/* avoid problems after truncation in tcp packets */
if (n > sizeof(answer))
n = sizeof(answer);
/* find first satisfactory answer */
hp = (HEADER *)&answer;
cp = (u_char *)&answer + HFIXEDSZ;
eom = (u_char *)&answer + n;
for (qdcount = ntohs((u_short)hp->qdcount);
qdcount--;
cp += n + QFIXEDSZ)
{
if ((n = dn_skipname(cp, eom)) < 0)
goto punt;
}
buflen = sizeof(MXHostBuf) - 1;
bp = MXHostBuf;
ancount = ntohs((u_short)hp->ancount);
while (--ancount >= 0 && cp < eom && nmx < MAXMXHOSTS - 1)
{
if ((n = dn_expand((u_char *)&answer,
eom, cp, (RES_UNC_T) bp, buflen)) < 0)
break;
cp += n;
GETSHORT(type, cp);
cp += INT16SZ + INT32SZ;
GETSHORT(n, cp);
if (type != T_MX)
{
if (tTd(8, 8) || _res.options & RES_DEBUG)
dprintf("unexpected answer type %d, size %d\n",
type, n);
cp += n;
continue;
}
GETSHORT(pref, cp);
if ((n = dn_expand((u_char *)&answer, eom, cp,
(RES_UNC_T) bp, buflen)) < 0)
break;
cp += n;
if (wordinclass(bp, 'w'))
{
if (tTd(8, 3))
dprintf("found localhost (%s) in MX list, pref=%d\n",
bp, pref);
if (droplocalhost)
{
if (!seenlocal || pref < localpref)
localpref = pref;
seenlocal = TRUE;
continue;
}
weight[nmx] = 0;
}
else
weight[nmx] = mxrand(bp);
prefs[nmx] = pref;
mxhosts[nmx++] = bp;
n = strlen(bp);
bp += n;
if (bp[-1] != '.')
{
*bp++ = '.';
n++;
}
*bp++ = '\0';
buflen -= n + 1;
}
/* sort the records */
for (i = 0; i < nmx; i++)
{
for (j = i + 1; j < nmx; j++)
{
if (prefs[i] > prefs[j] ||
(prefs[i] == prefs[j] && weight[i] > weight[j]))
{
register int temp;
register char *temp1;
temp = prefs[i];
prefs[i] = prefs[j];
prefs[j] = temp;
temp1 = mxhosts[i];
mxhosts[i] = mxhosts[j];
mxhosts[j] = temp1;
temp = weight[i];
weight[i] = weight[j];
weight[j] = temp;
}
}
if (seenlocal && prefs[i] >= localpref)
{
/* truncate higher preference part of list */
nmx = i;
}
}
/* delete duplicates from list (yes, some bozos have duplicates) */
for (i = 0; i < nmx - 1; )
{
if (strcasecmp(mxhosts[i], mxhosts[i + 1]) != 0)
i++;
else
{
/* compress out duplicate */
for (j = i + 1; j < nmx; j++)
{
mxhosts[j] = mxhosts[j + 1];
prefs[j] = prefs[j + 1];
}
nmx--;
}
}
if (nmx == 0)
{
punt:
if (seenlocal)
{
struct hostent *h = NULL;
/*
** If we have deleted all MX entries, this is
** an error -- we should NEVER send to a host that
** has an MX, and this should have been caught
** earlier in the config file.
**
** Some sites prefer to go ahead and try the
** A record anyway; that case is handled by
** setting TryNullMXList. I believe this is a
** bad idea, but it's up to you....
*/
if (TryNullMXList)
{
h_errno = 0;
errno = 0;
h = sm_gethostbyname(host, AF_INET);
if (h == NULL)
{
if (errno == ETIMEDOUT ||
h_errno == TRY_AGAIN ||
(errno == ECONNREFUSED &&
UseNameServer))
{
*rcode = EX_TEMPFAIL;
return -1;
}
# if NETINET6
h_errno = 0;
errno = 0;
h = sm_gethostbyname(host, AF_INET6);
if (h == NULL &&
(errno == ETIMEDOUT ||
h_errno == TRY_AGAIN ||
(errno == ECONNREFUSED &&
UseNameServer)))
{
*rcode = EX_TEMPFAIL;
return -1;
}
# endif /* NETINET6 */
}
}
if (h == NULL)
{
*rcode = EX_CONFIG;
syserr("MX list for %s points back to %s",
host, MyHostName);
return -1;
}
}
if (strlen(host) >= (SIZE_T) sizeof MXHostBuf)
{
*rcode = EX_CONFIG;
syserr("Host name %s too long",
shortenstring(host, MAXSHORTSTR));
return -1;
}
snprintf(MXHostBuf, sizeof MXHostBuf, "%s", host);
mxhosts[0] = MXHostBuf;
prefs[0] = 0;
if (host[0] == '[')
{
register char *p;
# if NETINET6
struct sockaddr_in6 tmp6;
# endif /* NETINET6 */
/* this may be an MX suppression-style address */
p = strchr(MXHostBuf, ']');
if (p != NULL)
{
*p = '\0';
if (inet_addr(&MXHostBuf[1]) != INADDR_NONE)
{
nmx++;
*p = ']';
}
# if NETINET6
else if (inet_pton(AF_INET6, &MXHostBuf[1],
&tmp6.sin6_addr) == 1)
{
nmx++;
*p = ']';
}
# endif /* NETINET6 */
else
{
trycanon = TRUE;
mxhosts[0]++;
}
}
}
if (trycanon &&
getcanonname(mxhosts[0], sizeof MXHostBuf - 2, FALSE))
{
bp = &MXHostBuf[strlen(MXHostBuf)];
if (bp[-1] != '.')
{
*bp++ = '.';
*bp = '\0';
}
nmx = 1;
}
}
/* if we have a default lowest preference, include that */
if (fallbackMX != NULL && !seenlocal)
{
if (nmx > 0)
prefs[nmx] = prefs[nmx - 1] + 1;
else
prefs[nmx] = 0;
mxhosts[nmx++] = fallbackMX;
}
return nmx;
}
/*
** MXRAND -- create a randomizer for equal MX preferences
**
** If two MX hosts have equal preferences we want to randomize
** the selection. But in order for signatures to be the same,
** we need to randomize the same way each time. This function
** computes a pseudo-random hash function from the host name.
**
** Parameters:
** host -- the name of the host.
**
** Returns:
** A random but repeatable value based on the host name.
**
** Side Effects:
** none.
*/
static int
mxrand(host)
register char *host;
{
int hfunc;
static unsigned int seed;
if (seed == 0)
{
seed = (int) curtime() & 0xffff;
if (seed == 0)
seed++;
}
if (tTd(17, 9))
dprintf("mxrand(%s)", host);
hfunc = seed;
while (*host != '\0')
{
int c = *host++;
if (isascii(c) && isupper(c))
c = tolower(c);
hfunc = ((hfunc << 1) ^ c) % 2003;
}
hfunc &= 0xff;
hfunc++;
if (tTd(17, 9))
dprintf(" = %d\n", hfunc);
return hfunc;
}
/*
** BESTMX -- find the best MX for a name
**
** This is really a hack, but I don't see any obvious way
** to generalize it at the moment.
*/
/* ARGSUSED3 */
char *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -