xref: /csrg-svn/usr.sbin/sendmail/src/domain.c (revision 51324)
1 /*
2  * Copyright (c) 1986 Eric P. Allman
3  * Copyright (c) 1988 Regents of the University of California.
4  * All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #include "sendmail.h"
10 
11 #ifndef lint
12 #ifdef NAMED_BIND
13 static char sccsid[] = "@(#)domain.c	5.30 (Berkeley) 10/07/91 (with name server)";
14 #else
15 static char sccsid[] = "@(#)domain.c	5.30 (Berkeley) 10/07/91 (without name server)";
16 #endif
17 #endif /* not lint */
18 
19 #ifdef NAMED_BIND
20 
21 #include <sys/param.h>
22 #include <errno.h>
23 #include <arpa/nameser.h>
24 #include <resolv.h>
25 #include <netdb.h>
26 
27 typedef union {
28 	HEADER qb1;
29 	char qb2[PACKETSZ];
30 } querybuf;
31 
32 static char hostbuf[MAXMXHOSTS*PACKETSZ];
33 
34 getmxrr(host, mxhosts, localhost, rcode)
35 	char *host, **mxhosts, *localhost;
36 	int *rcode;
37 {
38 	extern int h_errno;
39 	register u_char *eom, *cp;
40 	register int i, j, n, nmx;
41 	register char *bp;
42 	HEADER *hp;
43 	querybuf answer;
44 	int ancount, qdcount, buflen, seenlocal;
45 	u_short pref, localpref, type, prefer[MAXMXHOSTS];
46 
47 	errno = 0;
48 	n = res_search(host, C_IN, T_MX, (char *)&answer, sizeof(answer));
49 	if (n < 0)
50 	{
51 		if (tTd(8, 1))
52 			printf("getmxrr: res_search failed (errno=%d, h_errno=%d)\n",
53 			    errno, h_errno);
54 		switch (h_errno)
55 		{
56 		  case NO_DATA:
57 		  case NO_RECOVERY:
58 			/* no MX data on this host */
59 			goto punt;
60 
61 		  case HOST_NOT_FOUND:
62 			/* the host just doesn't exist */
63 			*rcode = EX_NOHOST;
64 			break;
65 
66 		  case TRY_AGAIN:
67 			/* couldn't connect to the name server */
68 			if (!UseNameServer && errno == ECONNREFUSED)
69 				goto punt;
70 
71 			/* it might come up later; better queue it up */
72 			*rcode = EX_TEMPFAIL;
73 			break;
74 		}
75 
76 		/* irreconcilable differences */
77 		return (-1);
78 	}
79 
80 	/* find first satisfactory answer */
81 	hp = (HEADER *)&answer;
82 	cp = (u_char *)&answer + sizeof(HEADER);
83 	eom = (u_char *)&answer + n;
84 	for (qdcount = ntohs(hp->qdcount); qdcount--; cp += n + QFIXEDSZ)
85 		if ((n = dn_skipname(cp, eom)) < 0)
86 			goto punt;
87 	nmx = 0;
88 	seenlocal = 0;
89 	buflen = sizeof(hostbuf);
90 	bp = hostbuf;
91 	ancount = ntohs(hp->ancount);
92 	while (--ancount >= 0 && cp < eom && nmx < MAXMXHOSTS) {
93 		if ((n = dn_expand((u_char *)&answer,
94 		    eom, cp, (u_char *)bp, buflen)) < 0)
95 			break;
96 		cp += n;
97 		GETSHORT(type, cp);
98  		cp += sizeof(u_short) + sizeof(u_long);
99 		GETSHORT(n, cp);
100 		if (type != T_MX)  {
101 			if (tTd(8, 1) || _res.options & RES_DEBUG)
102 				printf("unexpected answer type %d, size %d\n",
103 				    type, n);
104 			cp += n;
105 			continue;
106 		}
107 		GETSHORT(pref, cp);
108 		if ((n = dn_expand((u_char *)&answer,
109 		    eom, cp, (u_char *)bp, buflen)) < 0)
110 			break;
111 		cp += n;
112 		if (!strcasecmp(bp, localhost)) {
113 			if (seenlocal == 0 || pref < localpref)
114 				localpref = pref;
115 			seenlocal = 1;
116 			continue;
117 		}
118 		prefer[nmx] = pref;
119 		mxhosts[nmx++] = bp;
120 		n = strlen(bp) + 1;
121 		bp += n;
122 		buflen -= n;
123 	}
124 	if (nmx == 0) {
125 punt:		mxhosts[0] = strcpy(hostbuf, host);
126 		return(1);
127 	}
128 
129 	/* sort the records */
130 	for (i = 0; i < nmx; i++) {
131 		for (j = i + 1; j < nmx; j++) {
132 			if (prefer[i] > prefer[j] ||
133 			    (prefer[i] == prefer[j] && rand() % 1 == 0)) {
134 				register int temp;
135 				register char *temp1;
136 
137 				temp = prefer[i];
138 				prefer[i] = prefer[j];
139 				prefer[j] = temp;
140 				temp1 = mxhosts[i];
141 				mxhosts[i] = mxhosts[j];
142 				mxhosts[j] = temp1;
143 			}
144 		}
145 		if (seenlocal && prefer[i] >= localpref) {
146 			/*
147 			 * truncate higher pref part of list; if we're
148 			 * the best choice left, we should have realized
149 			 * awhile ago that this was a local delivery.
150 			 */
151 			if (i == 0) {
152 				*rcode = EX_CONFIG;
153 				return(-1);
154 			}
155 			nmx = i;
156 			break;
157 		}
158 	}
159 	return(nmx);
160 }
161 
162 /*
163  * Use query type of ANY if possible (NoWildcardMX), which will
164  * find types CNAME, A, and MX, and will cause all existing records
165  * to be cached by our local server.  If there is (might be) a
166  * wildcard MX record in the local domain or its parents that are
167  * searched, we can't use ANY; it would cause fully-qualified names
168  * to match as names in a local domain.
169  */
170 
171 bool
172 getcanonname(host, hbsize)
173 	char *host;
174 	int hbsize;
175 {
176 	extern int h_errno;
177 	register u_char *eom, *ap;
178 	register char *cp;
179 	register int n;
180 	HEADER *hp;
181 	querybuf answer;
182 	int first, ancount, qdcount, loopcnt;
183 	int ret;
184 	int qtype = NoWildcardMX ? T_ANY : T_CNAME;
185 	char **domain;
186 	bool rval;
187 	int type;
188 	char nbuf[PACKETSZ];
189 	extern char *__hostalias();
190 
191 	if (tTd(8, 2))
192 		printf("getcanonname(%s)\n", host);
193 
194 	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
195 		return (FALSE);
196 
197 	loopcnt = 0;
198 	rval = FALSE;
199 loop:
200 	for (cp = host, n = 0; *cp; cp++)
201 		if (*cp == '.')
202 			n++;
203 	if (n > 0 && *--cp == '.')
204 	{
205 		cp = host;
206 		n = -1;
207 	}
208 	else if (n == 0 && (cp = __hostalias(host)))
209 		n = -1;
210 
211 	/*
212 	 * We do at least one level of search if
213 	 *	- there is no dot and RES_DEFNAME is set, or
214 	 *	- there is at least one dot, there is no trailing dot,
215 	 *	  and RES_DNSRCH is set.
216 	 */
217 	ret = -1;
218 	if ((n == 0 && _res.options & RES_DEFNAMES) ||
219 	   (n > 0 && *--cp != '.' && _res.options & RES_DNSRCH))
220 	{
221 		for (domain = _res.dnsrch; *domain; domain++)
222 		{
223 			(void) sprintf(nbuf, "%.*s.%.*s",
224 				MAXDNAME, host, MAXDNAME, *domain);
225 			if (tTd(8, 5))
226 				printf("getcanonname: trying %s\n", nbuf);
227 			ret = res_query(nbuf, C_IN, qtype, &answer, sizeof(answer));
228 			if (ret > 0)
229 			{
230 				if (tTd(8, 8))
231 					printf("\tYES\n");
232 				cp = nbuf;
233 				break;
234 			}
235 			else if (tTd(8, 8))
236 				printf("\tNO: h_errno=%d\n", h_errno);
237 
238 			/*
239 			 * If no server present, give up.
240 			 * If name isn't found in this domain,
241 			 * keep trying higher domains in the search list
242 			 * (if that's enabled).
243 			 * On a NO_DATA error, keep trying, otherwise
244 			 * a wildcard entry of another type could keep us
245 			 * from finding this entry higher in the domain.
246 			 * If we get some other error (negative answer or
247 			 * server failure), then stop searching up,
248 			 * but try the input name below in case it's fully-qualified.
249 			 */
250 			if (errno == ECONNREFUSED) {
251 				h_errno = TRY_AGAIN;
252 				return rval;
253 			}
254 			if (h_errno == NO_DATA)
255 			{
256 				ret = 0;
257 				cp = nbuf;
258 				break;
259 			}
260 			if ((h_errno != HOST_NOT_FOUND) ||
261 			    (_res.options & RES_DNSRCH) == 0)
262 				return rval;
263 		}
264 	}
265 	if (ret < 0)
266 	{
267 		cp = host;
268 		if (tTd(8, 5))
269 			printf("getcanonname: trying %s\n", cp);
270 		ret = res_query(cp, C_IN, qtype, &answer, sizeof(answer));
271 		if (ret > 0)
272 		{
273 			if (tTd(8, 8))
274 				printf("\tYES\n");
275 		}
276 		else
277 		{
278 			if (tTd(8, 8))
279 				printf("\tNO: h_errno=%d\n", h_errno);
280 			return rval;
281 		}
282 	}
283 
284 	/* find first satisfactory answer */
285 	hp = (HEADER *)&answer;
286 	ancount = ntohs(hp->ancount);
287 	if (tTd(8, 3))
288 		printf("rcode = %d, ancount=%d, qdcount=%d\n",
289 			hp->rcode, ancount, ntohs(hp->qdcount));
290 
291 	/* we don't care about errors here, only if we got an answer */
292 	if (ancount == 0)
293 	{
294 		strncpy(host, cp, hbsize);
295 		host[hbsize - 1] = '\0';
296 		return (TRUE);
297 	}
298 	ap = (u_char *)&answer + sizeof(HEADER);
299 	eom = (u_char *)&answer + ret;
300 	for (qdcount = ntohs(hp->qdcount); qdcount--; ap += ret + QFIXEDSZ)
301 	{
302 		if ((ret = dn_skipname(ap, eom)) < 0)
303 		{
304 			if (tTd(8, 20))
305 				printf("qdcount failure (%d)\n",
306 					ntohs(hp->qdcount));
307 			return rval;		/* ???XXX??? */
308 		}
309 	}
310 
311 	/*
312 	* just in case someone puts a CNAME record after another record,
313 	* check all records for CNAME; otherwise, just take the first
314 	* name found.
315 	*/
316 	for (first = 1; --ancount >= 0 && ap < eom; ap += ret)
317 	{
318 		if ((ret = dn_expand((u_char *)&answer,
319 		    eom, ap, (u_char *)nbuf, sizeof(nbuf))) < 0)
320 			break;
321 		if (first) {			/* XXX */
322 			(void)strncpy(host, nbuf, hbsize);
323 			host[hbsize - 1] = '\0';
324 			first = 0;
325 			rval = TRUE;
326 		}
327 		ap += ret;
328 		GETSHORT(type, ap);
329 		ap += sizeof(u_short) + sizeof(u_long);
330 		GETSHORT(ret, ap);
331 		if (type == T_CNAME)  {
332 			/*
333 			 * assume that only one cname will be found.  More
334 			 * than one is undefined.  Copy so that if dn_expand
335 			 * fails, `host' is still okay.
336 			 */
337 			if ((ret = dn_expand((u_char *)&answer,
338 			    eom, ap, (u_char *)nbuf, sizeof(nbuf))) < 0)
339 				break;
340 			(void)strncpy(host, nbuf, hbsize); /* XXX */
341 			host[hbsize - 1] = '\0';
342 			if (++loopcnt > 8)	/* never be more than 1 */
343 				return FALSE;
344 			rval = TRUE;
345 			goto loop;
346 		}
347 	}
348 	return rval;		/* ???XXX??? */
349 }
350 
351 #else /* not NAMED_BIND */
352 
353 #include <netdb.h>
354 
355 bool
356 getcanonname(host, hbsize)
357 	char *host;
358 	int hbsize;
359 {
360 	struct hostent *hp;
361 
362 	hp = gethostbyname(host);
363 	if (hp == NULL)
364 		return (FALSE);
365 
366 	if (strlen(hp->h_name) >= hbsize)
367 		return (FALSE);
368 
369 	(void) strcpy(host, hp->h_name);
370 	return (TRUE);
371 }
372 
373 #endif /* not NAMED_BIND */
374