xref: /original-bsd/usr.sbin/sendmail/src/domain.c (revision 502e1f47)
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.33 (Berkeley) 03/06/92 (with name server)";
14 #else
15 static char sccsid[] = "@(#)domain.c	5.33 (Berkeley) 03/06/92 (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(%s) failed (errno=%d, h_errno=%d)\n",
53 			    (host == NULL) ? "<NULL>" : host, 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 
190 	if (tTd(8, 2))
191 		printf("getcanonname(%s)\n", host);
192 
193 	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
194 		return (FALSE);
195 
196 	loopcnt = 0;
197 	rval = FALSE;
198 loop:
199 	for (cp = host, n = 0; *cp; cp++)
200 		if (*cp == '.')
201 			n++;
202 	if (n > 0 && *--cp == '.')
203 	{
204 		cp = host;
205 		n = -1;
206 	}
207 
208 	/*
209 	 * We do at least one level of search if
210 	 *	- there is no dot and RES_DEFNAME is set, or
211 	 *	- there is at least one dot, there is no trailing dot,
212 	 *	  and RES_DNSRCH is set.
213 	 */
214 	ret = -1;
215 	if ((n == 0 && _res.options & RES_DEFNAMES) ||
216 	   (n > 0 && *--cp != '.' && _res.options & RES_DNSRCH))
217 	{
218 		for (domain = _res.dnsrch; *domain; domain++)
219 		{
220 			(void) sprintf(nbuf, "%.*s.%.*s",
221 				MAXDNAME, host, MAXDNAME, *domain);
222 			if (tTd(8, 5))
223 				printf("getcanonname: trying %s\n", nbuf);
224 			ret = res_query(nbuf, C_IN, qtype, &answer, sizeof(answer));
225 			if (ret > 0)
226 			{
227 				if (tTd(8, 8))
228 					printf("\tYES\n");
229 				cp = nbuf;
230 				break;
231 			}
232 			else if (tTd(8, 8))
233 				printf("\tNO: h_errno=%d\n", h_errno);
234 
235 			/*
236 			 * If no server present, give up.
237 			 * If name isn't found in this domain,
238 			 * keep trying higher domains in the search list
239 			 * (if that's enabled).
240 			 * On a NO_DATA error, keep trying, otherwise
241 			 * a wildcard entry of another type could keep us
242 			 * from finding this entry higher in the domain.
243 			 * If we get some other error (negative answer or
244 			 * server failure), then stop searching up,
245 			 * but try the input name below in case it's fully-qualified.
246 			 */
247 			if (errno == ECONNREFUSED) {
248 				h_errno = TRY_AGAIN;
249 				return FALSE;
250 			}
251 			if (h_errno == NO_DATA)
252 			{
253 				ret = 0;
254 				cp = nbuf;
255 				break;
256 			}
257 			if ((h_errno != HOST_NOT_FOUND) ||
258 			    (_res.options & RES_DNSRCH) == 0)
259 				return FALSE;
260 		}
261 	}
262 	if (ret < 0)
263 	{
264 		/*
265 		**  Try the unmodified name.
266 		*/
267 
268 		cp = host;
269 		if (tTd(8, 5))
270 			printf("getcanonname: trying %s\n", cp);
271 		ret = res_query(cp, C_IN, qtype, &answer, sizeof(answer));
272 		if (ret > 0)
273 		{
274 			if (tTd(8, 8))
275 				printf("\tYES\n");
276 		}
277 		else
278 		{
279 			if (tTd(8, 8))
280 				printf("\tNO: h_errno=%d\n", h_errno);
281 			if (h_errno != NO_DATA)
282 				return FALSE;
283 		}
284 	}
285 
286 	/* find first satisfactory answer */
287 	hp = (HEADER *)&answer;
288 	ancount = ntohs(hp->ancount);
289 	if (tTd(8, 3))
290 		printf("rcode = %d, ancount=%d, qdcount=%d\n",
291 			hp->rcode, ancount, ntohs(hp->qdcount));
292 
293 	/* we don't care about errors here, only if we got an answer */
294 	if (ancount == 0)
295 	{
296 		strncpy(host, cp, hbsize);
297 		host[hbsize - 1] = '\0';
298 		return (TRUE);
299 	}
300 	ap = (u_char *)&answer + sizeof(HEADER);
301 	eom = (u_char *)&answer + ret;
302 	for (qdcount = ntohs(hp->qdcount); qdcount--; ap += ret + QFIXEDSZ)
303 	{
304 		if ((ret = dn_skipname(ap, eom)) < 0)
305 		{
306 			if (tTd(8, 20))
307 				printf("qdcount failure (%d)\n",
308 					ntohs(hp->qdcount));
309 			return FALSE;		/* ???XXX??? */
310 		}
311 	}
312 
313 	/*
314 	* just in case someone puts a CNAME record after another record,
315 	* check all records for CNAME; otherwise, just take the first
316 	* name found.
317 	*/
318 	for (first = 1; --ancount >= 0 && ap < eom; ap += ret)
319 	{
320 		if ((ret = dn_expand((u_char *)&answer,
321 		    eom, ap, (u_char *)nbuf, sizeof(nbuf))) < 0)
322 			break;
323 		if (first) {			/* XXX */
324 			(void)strncpy(host, nbuf, hbsize);
325 			host[hbsize - 1] = '\0';
326 			first = 0;
327 			rval = TRUE;
328 		}
329 		ap += ret;
330 		GETSHORT(type, ap);
331 		ap += sizeof(u_short) + sizeof(u_long);
332 		GETSHORT(ret, ap);
333 		if (type == T_CNAME)  {
334 			/*
335 			 * assume that only one cname will be found.  More
336 			 * than one is undefined.  Copy so that if dn_expand
337 			 * fails, `host' is still okay.
338 			 */
339 			if ((ret = dn_expand((u_char *)&answer,
340 			    eom, ap, (u_char *)nbuf, sizeof(nbuf))) < 0)
341 				break;
342 			(void)strncpy(host, nbuf, hbsize); /* XXX */
343 			host[hbsize - 1] = '\0';
344 			if (++loopcnt > 8)	/* never be more than 1 */
345 				return FALSE;
346 			rval = TRUE;
347 			goto loop;
348 		}
349 	}
350 	return rval;		/* ???XXX??? */
351 }
352 
353 #else /* not NAMED_BIND */
354 
355 #include <netdb.h>
356 
357 bool
358 getcanonname(host, hbsize)
359 	char *host;
360 	int hbsize;
361 {
362 	struct hostent *hp;
363 
364 	hp = gethostbyname(host);
365 	if (hp == NULL)
366 		return (FALSE);
367 
368 	if (strlen(hp->h_name) >= hbsize)
369 		return (FALSE);
370 
371 	(void) strcpy(host, hp->h_name);
372 	return (TRUE);
373 }
374 
375 #endif /* not NAMED_BIND */
376