2 * Copyright (c) 1998-2003 Sendmail, Inc. and its suppliers.
4 * Copyright (c) 1986, 1995-1997 Eric P. Allman. All rights reserved.
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * By using this file, you agree to the terms and conditions set
9 * forth in the LICENSE file which can be found at the top level of
10 * the sendmail distribution.
17 SM_RCSID("@(#)$Id: domain.c,v 8.181.2.6 2003/01/15 19:17:15 ca Exp $ (with name server)")
18 #else /* NAMED_BIND */
19 SM_RCSID("@(#)$Id: domain.c,v 8.181.2.6 2003/01/15 19:17:15 ca Exp $ (without name server)")
20 #endif /* NAMED_BIND */
24 # include <arpa/inet.h>
28 ** The standard udp packet size PACKETSZ (512) is not sufficient for some
29 ** nameserver answers containing very many resource records. The resolver
30 ** may switch to tcp and retry if it detects udp packet overflow.
31 ** Also note that the resolver routines res_query and res_search return
32 ** the size of the *un*truncated answer in case the supplied answer buffer
33 ** it not big enough to accommodate the entire answer.
37 # define MAXPACKET 8192 /* max packet size used internally by BIND */
38 # endif /* ! MAXPACKET */
43 unsigned char qb2[MAXPACKET];
46 # ifndef MXHOSTBUFSIZE
47 # define MXHOSTBUFSIZE (128 * MAXMXHOSTS)
48 # endif /* ! MXHOSTBUFSIZE */
50 static char MXHostBuf[MXHOSTBUFSIZE];
51 #if (MXHOSTBUFSIZE < 2) || (MXHOSTBUFSIZE >= INT_MAX/2)
52 ERROR: _MXHOSTBUFSIZE is out of range
53 #endif /* (MXHOSTBUFSIZE < 2) || (MXHOSTBUFSIZE >= INT_MAX/2) */
56 # define MAXDNSRCH 6 /* number of possible domains to search */
57 # endif /* ! MAXDNSRCH */
59 # ifndef RES_DNSRCH_VARIABLE
60 # define RES_DNSRCH_VARIABLE _res.dnsrch
61 # endif /* ! RES_DNSRCH_VARIABLE */
64 # define NO_DATA NO_ADDRESS
65 # endif /* ! NO_DATA */
68 # define HFIXEDSZ 12 /* sizeof(HEADER) */
69 # endif /* ! HFIXEDSZ */
71 # define MAXCNAMEDEPTH 10 /* maximum depth of CNAME recursion */
73 # if defined(__RES) && (__RES >= 19940415)
74 # define RES_UNC_T char *
75 # else /* defined(__RES) && (__RES >= 19940415) */
76 # define RES_UNC_T unsigned char *
77 # endif /* defined(__RES) && (__RES >= 19940415) */
79 static char *gethostalias __P((char *));
80 static int mxrand __P((char *));
81 static int fallbackmxrr __P((int, unsigned short *, char **));
84 ** GETFALLBACKMXRR -- get MX resource records for fallback MX host.
86 ** We have to initialize this once before doing anything else.
87 ** Moreover, we have to repeat this from time to time to avoid
88 ** stale data, e.g., in persistent queue runners.
89 ** This should be done in a parent process so the child
90 ** processes have the right data.
93 ** host -- the name of the fallback MX host.
96 ** number of MX records.
99 ** Populates NumFallBackMXHosts and fbhosts.
100 ** Sets renewal time (based on TTL).
103 int NumFallBackMXHosts = 0; /* Number of fallback MX hosts (after MX expansion) */
104 static char *fbhosts[MAXMXHOSTS + 1];
107 getfallbackmxrr(host)
112 static time_t renew = 0;
115 /* This is currently done before this function is called. */
116 if (host == NULL || *host == '\0')
119 if (NumFallBackMXHosts > 0 && renew > curtime())
120 return NumFallBackMXHosts;
124 NumFallBackMXHosts = 1;
129 for (i = 0; i < NumFallBackMXHosts; i++)
133 NumFallBackMXHosts = getmxrr(host, fbhosts, NULL, false,
134 &rcode, false, &ttl);
135 renew = curtime() + ttl;
136 for (i = 0; i < NumFallBackMXHosts; i++)
137 fbhosts[i] = newstr(fbhosts[i]);
139 return NumFallBackMXHosts;
143 ** FALLBACKMXRR -- add MX resource records for fallback MX host to list.
146 ** nmx -- current number of MX records.
147 ** prefs -- array of preferences.
148 ** mxhosts -- array of MX hosts (maximum size: MAXMXHOSTS)
151 ** new number of MX records.
154 ** If FallBackMX was set, it appends the MX records for
155 ** that host to mxhosts (and modifies prefs accordingly).
159 fallbackmxrr(nmx, prefs, mxhosts)
161 unsigned short *prefs;
166 for (i = 0; i < NumFallBackMXHosts && nmx < MAXMXHOSTS; i++)
169 prefs[nmx] = prefs[nmx - 1] + 1;
172 mxhosts[nmx++] = fbhosts[i];
178 ** GETMXRR -- get MX resource records for a domain
181 ** host -- the name of the host to MX.
182 ** mxhosts -- a pointer to a return buffer of MX records.
183 ** mxprefs -- a pointer to a return buffer of MX preferences.
184 ** If NULL, don't try to populate.
185 ** droplocalhost -- If true, all MX records less preferred
186 ** than the local host (as determined by $=w) will
188 ** rcode -- a pointer to an EX_ status code.
189 ** tryfallback -- add also fallback MX host?
190 ** pttl -- pointer to return TTL (can be NULL).
193 ** The number of MX records found.
194 ** -1 if there is an internal failure.
195 ** If no MX records are found, mxhosts[0] is set to host
196 ** and 1 is returned.
199 ** The entries made for mxhosts point to a static array
200 ** MXHostBuf[MXHOSTBUFSIZE], so the data needs to be copied,
201 ** if it must be preserved across calls to this function.
205 getmxrr(host, mxhosts, mxprefs, droplocalhost, rcode, tryfallback, pttl)
208 unsigned short *mxprefs;
214 register unsigned char *eom, *cp;
215 register int i, j, n;
220 int ancount, qdcount, buflen;
221 bool seenlocal = false;
222 unsigned short pref, type;
223 unsigned short localpref = 256;
224 char *fallbackMX = FallBackMX;
225 bool trycanon = false;
226 unsigned short *prefs;
228 unsigned short prefer[MAXMXHOSTS];
229 int weight[MAXMXHOSTS];
231 extern int res_query(), res_search();
234 sm_dprintf("getmxrr(%s, droplocalhost=%d)\n",
235 host, droplocalhost);
237 if ((fallbackMX != NULL && droplocalhost &&
238 wordinclass(fallbackMX, 'w')) || !tryfallback)
240 /* don't use fallback for this pass */
251 /* efficiency hack -- numeric or non-MX lookups */
256 ** If we don't have MX records in our host switch, don't
257 ** try for MX records. Note that this really isn't "right",
258 ** since we might be set up to try NIS first and then DNS;
259 ** if the host is found in NIS we really shouldn't be doing
260 ** MX lookups. However, that should be a degenerate case.
265 if (HasWildcardMX && ConfigLevel >= 6)
268 resfunc = res_search;
271 n = (*resfunc)(host, C_IN, T_MX, (unsigned char *) &answer,
276 sm_dprintf("getmxrr: res_search(%s) failed (errno=%d, h_errno=%d)\n",
277 host == NULL ? "<NULL>" : host, errno, h_errno);
285 /* no MX data on this host */
289 # if BROKEN_RES_SEARCH
290 case 0: /* Ultrix resolver retns failure w/ h_errno=0 */
291 # endif /* BROKEN_RES_SEARCH */
292 /* host doesn't exist in DNS; might be in /etc/hosts */
299 /* couldn't connect to the name server */
300 if (fallbackMX != NULL)
302 /* name server is hosed -- push to fallback */
303 return fallbackmxrr(nmx, prefs, mxhosts);
305 /* it might come up later; better queue it up */
306 *rcode = EX_TEMPFAIL;
310 syserr("getmxrr: res_search (%s) failed with impossible h_errno (%d)",
316 /* irreconcilable differences */
320 /* avoid problems after truncation in tcp packets */
321 if (n > sizeof(answer))
324 /* find first satisfactory answer */
325 hp = (HEADER *)&answer;
326 cp = (unsigned char *)&answer + HFIXEDSZ;
327 eom = (unsigned char *)&answer + n;
328 for (qdcount = ntohs((unsigned short) hp->qdcount);
332 if ((n = dn_skipname(cp, eom)) < 0)
336 /* NOTE: see definition of MXHostBuf! */
337 buflen = sizeof(MXHostBuf) - 1;
338 SM_ASSERT(buflen > 0);
340 ancount = ntohs((unsigned short) hp->ancount);
342 /* See RFC 1035 for layout of RRs. */
343 /* XXX leave room for FallBackMX ? */
344 while (--ancount >= 0 && cp < eom && nmx < MAXMXHOSTS - 1)
346 if ((n = dn_expand((unsigned char *)&answer, eom, cp,
347 (RES_UNC_T) bp, buflen)) < 0)
351 cp += INT16SZ; /* skip over class */
353 GETSHORT(n, cp); /* rdlength */
356 if (tTd(8, 8) || _res.options & RES_DEBUG)
357 sm_dprintf("unexpected answer type %d, size %d\n",
363 if ((n = dn_expand((unsigned char *)&answer, eom, cp,
364 (RES_UNC_T) bp, buflen)) < 0)
369 /* Can this happen? */
373 sm_syslog(LOG_ERR, NOQID,
374 "MX records for %s contain empty string",
379 if (wordinclass(bp, 'w'))
382 sm_dprintf("found localhost (%s) in MX list, pref=%d\n",
386 if (!seenlocal || pref < localpref)
394 weight[nmx] = mxrand(bp);
406 /* don't want to wrap buflen */
412 /* return only one TTL entry, that should be sufficient */
413 if (ttl > 0 && pttl != NULL)
416 /* sort the records */
417 for (i = 0; i < nmx; i++)
419 for (j = i + 1; j < nmx; j++)
421 if (prefs[i] > prefs[j] ||
422 (prefs[i] == prefs[j] && weight[i] > weight[j]))
425 register char *temp1;
431 mxhosts[i] = mxhosts[j];
434 weight[i] = weight[j];
438 if (seenlocal && prefs[i] >= localpref)
440 /* truncate higher preference part of list */
445 /* delete duplicates from list (yes, some bozos have duplicates) */
446 for (i = 0; i < nmx - 1; )
448 if (sm_strcasecmp(mxhosts[i], mxhosts[i + 1]) != 0)
452 /* compress out duplicate */
453 for (j = i + 1; j < nmx; j++)
455 mxhosts[j] = mxhosts[j + 1];
456 prefs[j] = prefs[j + 1];
467 struct hostent *h = NULL;
470 ** If we have deleted all MX entries, this is
471 ** an error -- we should NEVER send to a host that
472 ** has an MX, and this should have been caught
473 ** earlier in the config file.
475 ** Some sites prefer to go ahead and try the
476 ** A record anyway; that case is handled by
477 ** setting TryNullMXList. I believe this is a
478 ** bad idea, but it's up to you....
485 h = sm_gethostbyname(host, AF_INET);
488 if (errno == ETIMEDOUT ||
489 h_errno == TRY_AGAIN ||
490 (errno == ECONNREFUSED &&
493 *rcode = EX_TEMPFAIL;
499 h = sm_gethostbyname(host, AF_INET6);
501 (errno == ETIMEDOUT ||
502 h_errno == TRY_AGAIN ||
503 (errno == ECONNREFUSED &&
506 *rcode = EX_TEMPFAIL;
509 # endif /* NETINET6 */
516 syserr("MX list for %s points back to %s",
523 # endif /* NETINET6 */
525 if (strlen(host) >= sizeof MXHostBuf)
528 syserr("Host name %s too long",
529 shortenstring(host, MAXSHORTSTR));
532 (void) sm_strlcpy(MXHostBuf, host, sizeof MXHostBuf);
533 mxhosts[0] = MXHostBuf;
539 struct sockaddr_in6 tmp6;
540 # endif /* NETINET6 */
542 /* this may be an MX suppression-style address */
543 p = strchr(MXHostBuf, ']');
548 if (inet_addr(&MXHostBuf[1]) != INADDR_NONE)
554 else if (anynet_pton(AF_INET6, &MXHostBuf[1],
555 &tmp6.sin6_addr) == 1)
560 # endif /* NETINET6 */
569 getcanonname(mxhosts[0], sizeof MXHostBuf - 2, false, pttl))
571 /* XXX MXHostBuf == "" ? is that possible? */
572 bp = &MXHostBuf[strlen(MXHostBuf)];
582 /* if we have a default lowest preference, include that */
583 if (fallbackMX != NULL && !seenlocal)
585 nmx = fallbackmxrr(nmx, prefs, mxhosts);
590 ** MXRAND -- create a randomizer for equal MX preferences
592 ** If two MX hosts have equal preferences we want to randomize
593 ** the selection. But in order for signatures to be the same,
594 ** we need to randomize the same way each time. This function
595 ** computes a pseudo-random hash function from the host name.
598 ** host -- the name of the host.
601 ** A random but repeatable value based on the host name.
609 static unsigned int seed;
613 seed = (int) curtime() & 0xffff;
619 sm_dprintf("mxrand(%s)", host);
622 while (*host != '\0')
626 if (isascii(c) && isupper(c))
628 hfunc = ((hfunc << 1) ^ c) % 2003;
635 sm_dprintf(" = %d\n", hfunc);
639 ** BESTMX -- find the best MX for a name
641 ** This is really a hack, but I don't see any obvious way
642 ** to generalize it at the moment.
647 bestmx_map_lookup(map, name, av, statp)
654 int saveopts = _res.options;
658 char *mxhosts[MAXMXHOSTS + 1];
659 #if _FFR_BESTMX_BETTER_TRUNCATION
661 #else /* _FFR_BESTMX_BETTER_TRUNCATION */
663 char buf[PSBUFSIZE / 2];
664 #endif /* _FFR_BESTMX_BETTER_TRUNCATION */
666 _res.options &= ~(RES_DNSRCH|RES_DEFNAMES);
667 nmx = getmxrr(name, mxhosts, NULL, false, statp, false, NULL);
668 _res.options = saveopts;
671 if (bitset(MF_MATCHONLY, map->map_mflags))
672 return map_rewrite(map, name, strlen(name), NULL);
673 if ((map->map_coldelim == '\0') || (nmx == 1))
674 return map_rewrite(map, mxhosts[0], strlen(mxhosts[0]), av);
677 ** We were given a -z flag (return all MXs) and there are multiple
678 ** ones. We need to build them all into a list.
681 #if _FFR_BESTMX_BETTER_TRUNCATION
682 for (i = 0; i < nmx; i++)
684 if (strchr(mxhosts[i], map->map_coldelim) != NULL)
686 syserr("bestmx_map_lookup: MX host %.64s includes map delimiter character 0x%02X",
687 mxhosts[i], map->map_coldelim);
690 len += strlen(mxhosts[i]) + 1;
693 len -= strlen(mxhosts[i]) + 1;
697 buf = (char *) sm_malloc(len);
700 *statp = EX_UNAVAILABLE;
704 for (i = 0; i < nmx; i++)
708 end = sm_strlcat(buf, mxhosts[i], len);
709 if (i != nmx && end + 1 < len)
711 buf[end] = map->map_coldelim;
716 /* Cleanly truncate for rulesets */
717 truncate_at_delim(buf, PSBUFSIZE / 2, map->map_coldelim);
718 #else /* _FFR_BESTMX_BETTER_TRUNCATION */
720 for (i = 0; i < nmx; i++)
724 if (strchr(mxhosts[i], map->map_coldelim) != NULL)
726 syserr("bestmx_map_lookup: MX host %.64s includes map delimiter character 0x%02X",
727 mxhosts[i], map->map_coldelim);
730 slen = strlen(mxhosts[i]);
731 if (len + slen + 2 > sizeof buf)
735 *p++ = map->map_coldelim;
738 (void) sm_strlcpy(p, mxhosts[i], sizeof buf - len);
742 #endif /* _FFR_BESTMX_BETTER_TRUNCATION */
744 result = map_rewrite(map, buf, len, av);
745 #if _FFR_BESTMX_BETTER_TRUNCATION
747 #endif /* _FFR_BESTMX_BETTER_TRUNCATION */
751 ** DNS_GETCANONNAME -- get the canonical name for named host using DNS
753 ** This algorithm tries to be smart about wildcard MX records.
754 ** This is hard to do because DNS doesn't tell is if we matched
755 ** against a wildcard or a specific MX.
757 ** We always prefer A & CNAME records, since these are presumed
760 ** If we match an MX in one pass and lose it in the next, we use
761 ** the old one. For example, consider an MX matching *.FOO.BAR.COM.
762 ** A hostname bletch.foo.bar.com will match against this MX, but
763 ** will stop matching when we try bletch.bar.com -- so we know
764 ** that bletch.foo.bar.com must have been right. This fails if
765 ** there was also an MX record matching *.BAR.COM, but there are
766 ** some things that just can't be fixed.
769 ** host -- a buffer containing the name of the host.
770 ** This is a value-result parameter.
771 ** hbsize -- the size of the host buffer.
772 ** trymx -- if set, try MX records as well as A and CNAME.
773 ** statp -- pointer to place to store status.
774 ** pttl -- pointer to return TTL (can be NULL).
777 ** true -- if the host matched.
778 ** false -- otherwise.
782 # define SM_T_INITIAL T_AAAA
783 # else /* NETINET6 */
784 # define SM_T_INITIAL T_A
785 # endif /* NETINET6 */
788 dns_getcanonname(host, hbsize, trymx, statp, pttl)
795 register unsigned char *eom, *ap;
800 int ancount, qdcount;
812 char nbuf[SM_MAX(MAXPACKET, MAXDNAME*2+2)];
813 char *searchlist[MAXDNSRCH + 2];
816 sm_dprintf("dns_getcanonname(%s, trymx=%d)\n", host, trymx);
818 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
820 *statp = EX_UNAVAILABLE;
827 ** Initialize domain search list. If there is at least one
828 ** dot in the name, search the unmodified name first so we
829 ** find "vse.CS" in Czechoslovakia instead of in the local
830 ** domain (e.g., vse.CS.Berkeley.EDU). Note that there is no
831 ** longer a country named Czechoslovakia but this type of problem
834 ** Older versions of the resolver could create this
835 ** list by tearing apart the host name.
840 /* Check for dots in the name */
841 for (cp = host, n = 0; *cp != '\0'; cp++)
846 ** If this is a simple name, determine whether it matches an
847 ** alias in the file defined by the environment variable HOSTALIASES.
850 if (n == 0 && (xp = gethostalias(host)) != NULL)
852 if (loopcnt++ > MAXCNAMEDEPTH)
854 syserr("loop in ${HOSTALIASES} file");
858 (void) sm_strlcpy(host, xp, hbsize);
864 ** Build the search list.
865 ** If there is at least one dot in name, start with a null
866 ** domain to search the unmodified name first.
867 ** If name does not end with a dot and search up local domain
868 ** tree desired, append each local domain component to the
869 ** search list; if name contains no dots and default domain
870 ** name is desired, append default domain name to search list;
871 ** else if name ends in a dot, remove that dot.
877 if (n >= 0 && *--cp != '.' && bitset(RES_DNSRCH, _res.options))
879 /* make sure there are less than MAXDNSRCH domains */
880 for (domain = RES_DNSRCH_VARIABLE, ret = 0;
881 *domain != NULL && ret < MAXDNSRCH;
885 else if (n == 0 && bitset(RES_DEFNAMES, _res.options))
887 *dp++ = _res.defdname;
896 ** Now loop through the search list, appending each domain in turn
897 ** name and searching for a match.
901 qtype = SM_T_INITIAL;
903 for (dp = searchlist; *dp != NULL; )
905 if (qtype == SM_T_INITIAL)
908 sm_dprintf("dns_getcanonname: trying %s.%s (%s)\n",
911 qtype == T_AAAA ? "AAAA" :
912 # endif /* NETINET6 */
914 qtype == T_MX ? "MX" :
917 ret = res_querydomain(host, *dp, C_IN, qtype,
918 answer.qb2, sizeof(answer.qb2));
921 int save_errno = errno;
924 sm_dprintf("\tNO: errno=%d, h_errno=%d\n",
925 save_errno, h_errno);
927 if (save_errno == ECONNREFUSED || h_errno == TRY_AGAIN)
930 ** the name server seems to be down or broken.
933 SM_SET_H_ERRNO(TRY_AGAIN);
934 # if _FFR_DONT_STOP_LOOKING
938 *statp = EX_TEMPFAIL;
941 # endif /* _FFR_DONT_STOP_LOOKING */
942 *statp = EX_TEMPFAIL;
944 if (WorkAroundBrokenAAAA)
947 ** Only return if not TRY_AGAIN as an
948 ** attempt with a different qtype may
949 ** succeed (res_querydomain() calls
950 ** res_query() calls res_send() which
951 ** sets errno to ETIMEDOUT if the
952 ** nameservers could be contacted but
953 ** didn't give an answer).
956 if (save_errno != ETIMEDOUT)
963 # if _FFR_DONT_STOP_LOOKING
965 # endif /* _FFR_DONT_STOP_LOOKING */
966 if (h_errno != HOST_NOT_FOUND)
968 /* might have another type of interest */
976 # endif /* NETINET6 */
977 if (qtype == T_A && !gotmx &&
978 (trymx || **dp == '\0'))
985 /* definite no -- try the next domain */
987 qtype = SM_T_INITIAL;
991 sm_dprintf("\tYES\n");
993 /* avoid problems after truncation in tcp packets */
994 if (ret > sizeof(answer))
995 ret = sizeof(answer);
998 *statp = EX_SOFTWARE;
1003 ** Appear to have a match. Confirm it by searching for A or
1004 ** CNAME records. If we don't have a local domain
1005 ** wild card MX record, we will accept MX as well.
1008 hp = (HEADER *) &answer;
1009 ap = (unsigned char *) &answer + HFIXEDSZ;
1010 eom = (unsigned char *) &answer + ret;
1012 /* skip question part of response -- we know what we asked */
1013 for (qdcount = ntohs((unsigned short) hp->qdcount);
1015 ap += ret + QFIXEDSZ)
1017 if ((ret = dn_skipname(ap, eom)) < 0)
1020 sm_dprintf("qdcount failure (%d)\n",
1021 ntohs((unsigned short) hp->qdcount));
1022 *statp = EX_SOFTWARE;
1023 return false; /* ???XXX??? */
1028 for (ancount = ntohs((unsigned short) hp->ancount);
1029 --ancount >= 0 && ap < eom;
1032 n = dn_expand((unsigned char *) &answer, eom, ap,
1033 (RES_UNC_T) nbuf, sizeof nbuf);
1038 ap += INT16SZ; /* skip over class */
1040 GETSHORT(n, ap); /* rdlength */
1045 if (**dp != '\0' && HasWildcardMX)
1048 ** If we are using MX matches and have
1049 ** not yet gotten one, save this one
1050 ** but keep searching for an A or
1054 if (trymx && mxmatch == NULL)
1060 ** If we did not append a domain name, this
1061 ** must have been a canonical name to start
1062 ** with. Even if we did append a domain name,
1063 ** in the absence of a wildcard MX this must
1064 ** still be a real MX match.
1065 ** Such MX matches are as good as an A match,
1072 /* Flag that a good match was found */
1075 /* continue in case a CNAME also exists */
1077 # endif /* NETINET6 */
1080 /* Flag that a good match was found */
1083 /* continue in case a CNAME also exists */
1087 if (DontExpandCnames)
1089 /* got CNAME -- guaranteed canonical */
1094 if (loopcnt++ > MAXCNAMEDEPTH)
1096 /*XXX should notify postmaster XXX*/
1097 message("DNS failure: CNAME loop for %s",
1099 if (CurEnv->e_message == NULL)
1103 (void) sm_snprintf(ebuf,
1105 "Deferred: DNS failure: CNAME loop for %.100s",
1109 CurEnv->e_rpool, ebuf);
1111 SM_SET_H_ERRNO(NO_RECOVERY);
1116 /* value points at name */
1117 if ((ret = dn_expand((unsigned char *)&answer,
1118 eom, ap, (RES_UNC_T) nbuf,
1121 (void) sm_strlcpy(host, nbuf, hbsize);
1124 ** RFC 1034 section 3.6 specifies that CNAME
1125 ** should point at the canonical name -- but
1126 ** urges software to try again anyway.
1132 /* not a record of interest */
1140 ** Got a good match -- either an A, CNAME, or an
1141 ** exact MX record. Save it and get out of here.
1149 ** Nothing definitive yet.
1150 ** If this was a T_A query and we haven't yet found a MX
1151 ** match, try T_MX if allowed to do so.
1152 ** Otherwise, try the next domain.
1156 if (qtype == T_AAAA)
1159 # endif /* NETINET6 */
1160 if (qtype == T_A && !gotmx && (trymx || **dp == '\0'))
1164 qtype = SM_T_INITIAL;
1169 /* if nothing was found, we are done */
1170 if (mxmatch == NULL)
1172 if (*statp == EX_OK)
1178 ** Create canonical name and return.
1179 ** If saved domain name is null, name was already canonical.
1180 ** Otherwise append the saved domain name.
1183 (void) sm_snprintf(nbuf, sizeof nbuf, "%.*s%s%.*s", MAXDNAME, host,
1184 *mxmatch == '\0' ? "" : ".",
1186 (void) sm_strlcpy(host, nbuf, hbsize);
1188 sm_dprintf("dns_getcanonname: %s\n", host);
1191 /* return only one TTL entry, that should be sufficient */
1192 if (ttl > 0 && pttl != NULL)
1203 register char *p = NULL;
1204 long sff = SFF_REGONLY;
1206 static char hbuf[MAXDNAME];
1210 if (DontLockReadFiles)
1212 fname = getenv("HOSTALIASES");
1213 if (fname == NULL ||
1214 (fp = safefopen(fname, O_RDONLY, 0, sff)) == NULL)
1216 while (sm_io_fgets(fp, SM_TIME_DEFAULT, buf, sizeof buf) != NULL)
1218 for (p = buf; p != '\0' && !(isascii(*p) && isspace(*p)); p++)
1226 if (sm_strcasecmp(buf, host) == 0)
1233 (void) sm_io_close(fp, SM_TIME_DEFAULT);
1236 (void) sm_io_close(fp, SM_TIME_DEFAULT);
1238 /* got a match; extract the equivalent name */
1239 while (*p != '\0' && isascii(*p) && isspace(*p))
1242 while (*p != '\0' && !(isascii(*p) && isspace(*p)))
1245 (void) sm_strlcpy(hbuf, host, sizeof hbuf);
1248 #endif /* NAMED_BIND */