xref: /original-bsd/usr.sbin/sendmail/src/domain.c (revision a6d8c59f)
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.36 (Berkeley) 12/15/92 (with name server)";
14 #else
15 static char sccsid[] = "@(#)domain.c	5.36 (Berkeley) 12/15/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) - 1;
90 	bp = hostbuf;
91 	ancount = ntohs(hp->ancount);
92 	while (--ancount >= 0 && cp < eom && nmx < MAXMXHOSTS)
93 	{
94 		if ((n = dn_expand((u_char *)&answer,
95 		    eom, cp, (u_char *)bp, buflen)) < 0)
96 			break;
97 		cp += n;
98 		GETSHORT(type, cp);
99  		cp += sizeof(u_short) + sizeof(u_long);
100 		GETSHORT(n, cp);
101 		if (type != T_MX)
102 		{
103 			if (tTd(8, 1) || _res.options & RES_DEBUG)
104 				printf("unexpected answer type %d, size %d\n",
105 				    type, n);
106 			cp += n;
107 			continue;
108 		}
109 		GETSHORT(pref, cp);
110 		if ((n = dn_expand((u_char *)&answer, eom, cp,
111 				   (u_char *)bp, buflen)) < 0)
112 			break;
113 		cp += n;
114 		if (!strcasecmp(bp, localhost))
115 		{
116 			if (seenlocal == 0 || pref < localpref)
117 				localpref = pref;
118 			seenlocal = 1;
119 			continue;
120 		}
121 		prefer[nmx] = pref;
122 		mxhosts[nmx++] = bp;
123 		n = strlen(bp);
124 		bp += n;
125 		if (bp[-1] != '.')
126 		{
127 			*bp++ = '.';
128 			n++;
129 		}
130 		*bp++ = '\0';
131 		buflen -= n + 1;
132 	}
133 	if (nmx == 0) {
134 punt:		mxhosts[0] = strcpy(hostbuf, host);
135 		return(1);
136 	}
137 
138 	/* sort the records */
139 	for (i = 0; i < nmx; i++) {
140 		for (j = i + 1; j < nmx; j++) {
141 			if (prefer[i] > prefer[j] ||
142 			    (prefer[i] == prefer[j] && (rand() & 0100) == 0)) {
143 				register int temp;
144 				register char *temp1;
145 
146 				temp = prefer[i];
147 				prefer[i] = prefer[j];
148 				prefer[j] = temp;
149 				temp1 = mxhosts[i];
150 				mxhosts[i] = mxhosts[j];
151 				mxhosts[j] = temp1;
152 			}
153 		}
154 		if (seenlocal && prefer[i] >= localpref) {
155 			/*
156 			 * truncate higher pref part of list; if we're
157 			 * the best choice left, we should have realized
158 			 * awhile ago that this was a local delivery.
159 			 */
160 			if (i == 0) {
161 				*rcode = EX_CONFIG;
162 				return(-1);
163 			}
164 			nmx = i;
165 			break;
166 		}
167 	}
168 	return(nmx);
169 }
170 /*
171 **  GETCANONNAME -- get the canonical name for named host
172 **
173 **	Parameters:
174 **		host -- a buffer containing the name of the host.
175 **			This is a value-result parameter.
176 **		hbsize -- the size of the host buffer.
177 **
178 **	Returns:
179 **		TRUE -- if the host matched.
180 **		FALSE -- otherwise.
181 **
182 **	Notes:
183 **		Use query type of ANY if possible (NoWildcardMX), which
184 **		will find types CNAME, A, and MX, and will cause all
185 **		existing records to be cached by our local server.  If
186 **		there is (might be) a wildcard MX record in the local
187 **		domain or its parents that are searched, we can't use
188 **		ANY; it would cause fully-qualified names to match as
189 **		names in a local domain.
190 */
191 
192 bool
193 getcanonname(host, hbsize)
194 	char *host;
195 	int hbsize;
196 {
197 	extern int h_errno;
198 	register u_char *eom, *ap;
199 	register char *cp;
200 	register int n;
201 	HEADER *hp;
202 	querybuf answer;
203 	int first, ancount, qdcount, loopcnt;
204 	int ret;
205 	int qtype = NoWildcardMX ? T_ANY : T_CNAME;
206 	char **domain;
207 	bool rval;
208 	int type;
209 	char nbuf[PACKETSZ];
210 
211 	if (tTd(8, 2))
212 		printf("getcanonname(%s)\n", host);
213 
214 	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
215 		return (FALSE);
216 
217 	loopcnt = 0;
218 	rval = FALSE;
219 loop:
220 	for (cp = host, n = 0; *cp; cp++)
221 		if (*cp == '.')
222 			n++;
223 	if (n > 0 && *--cp == '.')
224 	{
225 		cp = host;
226 		n = -1;
227 	}
228 
229 	/*
230 	 * We do at least one level of search if
231 	 *	- there is no dot and RES_DEFNAME is set, or
232 	 *	- there is at least one dot, there is no trailing dot,
233 	 *	  and RES_DNSRCH is set.
234 	 */
235 	ret = -1;
236 	if ((n == 0 && _res.options & RES_DEFNAMES) ||
237 	   (n > 0 && *--cp != '.' && _res.options & RES_DNSRCH))
238 	{
239 		for (domain = _res.dnsrch; *domain; domain++)
240 		{
241 			(void) sprintf(nbuf, "%.*s.%.*s",
242 				MAXDNAME, host, MAXDNAME, *domain);
243 			if (tTd(8, 5))
244 				printf("getcanonname: trying %s\n", nbuf);
245 			ret = res_query(nbuf, C_IN, qtype, &answer, sizeof(answer));
246 			if (ret > 0)
247 			{
248 				if (tTd(8, 8))
249 					printf("\tYES\n");
250 				cp = nbuf;
251 				break;
252 			}
253 			else if (tTd(8, 8))
254 				printf("\tNO: h_errno=%d\n", h_errno);
255 
256 			/*
257 			 * If no server present, give up.
258 			 * If name isn't found in this domain,
259 			 * keep trying higher domains in the search list
260 			 * (if that's enabled).
261 			 * On a NO_DATA error, keep trying, otherwise
262 			 * a wildcard entry of another type could keep us
263 			 * from finding this entry higher in the domain.
264 			 * If we get some other error (negative answer or
265 			 * server failure), then stop searching up,
266 			 * but try the input name below in case it's fully-qualified.
267 			 */
268 			if (errno == ECONNREFUSED) {
269 				h_errno = TRY_AGAIN;
270 				return FALSE;
271 			}
272 			if (h_errno == NO_DATA)
273 			{
274 				ret = 0;
275 				cp = nbuf;
276 				break;
277 			}
278 			if ((h_errno != HOST_NOT_FOUND) ||
279 			    (_res.options & RES_DNSRCH) == 0)
280 				return FALSE;
281 		}
282 	}
283 	if (ret < 0)
284 	{
285 		/*
286 		**  Try the unmodified name.
287 		*/
288 
289 		cp = host;
290 		if (tTd(8, 5))
291 			printf("getcanonname: trying %s\n", cp);
292 		ret = res_query(cp, C_IN, qtype, &answer, sizeof(answer));
293 		if (ret > 0)
294 		{
295 			if (tTd(8, 8))
296 				printf("\tYES\n");
297 		}
298 		else
299 		{
300 			if (tTd(8, 8))
301 				printf("\tNO: h_errno=%d\n", h_errno);
302 			if (h_errno != NO_DATA)
303 				return FALSE;
304 		}
305 	}
306 
307 	/* find first satisfactory answer */
308 	hp = (HEADER *)&answer;
309 	ancount = ntohs(hp->ancount);
310 	if (tTd(8, 3))
311 		printf("rcode = %d, ancount=%d, qdcount=%d\n",
312 			hp->rcode, ancount, ntohs(hp->qdcount));
313 
314 	/* we don't care about errors here, only if we got an answer */
315 	if (ancount == 0)
316 	{
317 		strncpy(host, cp, hbsize);
318 		host[hbsize - 1] = '\0';
319 		return (TRUE);
320 	}
321 	ap = (u_char *)&answer + sizeof(HEADER);
322 	eom = (u_char *)&answer + ret;
323 	for (qdcount = ntohs(hp->qdcount); qdcount--; ap += ret + QFIXEDSZ)
324 	{
325 		if ((ret = dn_skipname(ap, eom)) < 0)
326 		{
327 			if (tTd(8, 20))
328 				printf("qdcount failure (%d)\n",
329 					ntohs(hp->qdcount));
330 			return FALSE;		/* ???XXX??? */
331 		}
332 	}
333 
334 	/*
335 	* just in case someone puts a CNAME record after another record,
336 	* check all records for CNAME; otherwise, just take the first
337 	* name found.
338 	*/
339 	for (first = 1; --ancount >= 0 && ap < eom; ap += ret)
340 	{
341 		if ((ret = dn_expand((u_char *)&answer,
342 		    eom, ap, (u_char *)nbuf, sizeof(nbuf))) < 0)
343 			break;
344 		if (first) {			/* XXX */
345 			(void)strncpy(host, nbuf, hbsize);
346 			host[hbsize - 1] = '\0';
347 			first = 0;
348 			rval = TRUE;
349 		}
350 		ap += ret;
351 		GETSHORT(type, ap);
352 		ap += sizeof(u_short) + sizeof(u_long);
353 		GETSHORT(ret, ap);
354 		if (type == T_CNAME)  {
355 			/*
356 			 * assume that only one cname will be found.  More
357 			 * than one is undefined.  Copy so that if dn_expand
358 			 * fails, `host' is still okay.
359 			 */
360 			if ((ret = dn_expand((u_char *)&answer,
361 			    eom, ap, (u_char *)nbuf, sizeof(nbuf))) < 0)
362 				break;
363 			(void)strncpy(host, nbuf, hbsize); /* XXX */
364 			host[hbsize - 1] = '\0';
365 			if (++loopcnt > 8)	/* never be more than 1 */
366 				return FALSE;
367 			rval = TRUE;
368 			goto loop;
369 		}
370 	}
371 	return rval;		/* ???XXX??? */
372 }
373 
374 #else /* not NAMED_BIND */
375 
376 #include <netdb.h>
377 
378 bool
379 getcanonname(host, hbsize)
380 	char *host;
381 	int hbsize;
382 {
383 	struct hostent *hp;
384 
385 	hp = gethostbyname(host);
386 	if (hp == NULL)
387 		return (FALSE);
388 
389 	if (strlen(hp->h_name) >= hbsize)
390 		return (FALSE);
391 
392 	(void) strcpy(host, hp->h_name);
393 	return (TRUE);
394 }
395 
396 #endif /* not NAMED_BIND */
397