xref: /original-bsd/usr.sbin/sendmail/src/domain.c (revision d272e02a)
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.23 (Berkeley) 03/02/91 (with name server)";
14 #else
15 static char sccsid[] = "@(#)domain.c	5.23 (Berkeley) 03/02/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 getcanonname(host, hbsize)
163 	char *host;
164 	int hbsize;
165 {
166 	extern int h_errno;
167 	register u_char *eom, *cp;
168 	register int n;
169 	HEADER *hp;
170 	querybuf answer;
171 	u_short type;
172 	int first, ancount, qdcount, loopcnt;
173 	char nbuf[PACKETSZ];
174 
175 	loopcnt = 0;
176 loop:
177 	/*
178 	 * Use query type of ANY if possible (NO_WILDCARD_MX), which will
179 	 * find types CNAME, A, and MX, and will cause all existing records
180 	 * to be cached by our local server.  If there is (might be) a
181 	 * wildcard MX record in the local domain or its parents that are
182 	 * searched, we can't use ANY; it would cause fully-qualified names
183 	 * to match as names in a local domain.
184 	 */
185 # ifdef NO_WILDCARD_MX
186 	n = res_search(host, C_IN, T_ANY, (char *)&answer, sizeof(answer));
187 # else
188 	n = res_search(host, C_IN, T_CNAME, (char *)&answer, sizeof(answer));
189 # endif
190 	if (n < 0) {
191 		if (tTd(8, 1))
192 			printf("getcanonname:  res_search failed (errno=%d, h_errno=%d)\n",
193 			    errno, h_errno);
194 		return;
195 	}
196 
197 	/* find first satisfactory answer */
198 	hp = (HEADER *)&answer;
199 	ancount = ntohs(hp->ancount);
200 
201 	/* we don't care about errors here, only if we got an answer */
202 	if (ancount == 0) {
203 		if (tTd(8, 1))
204 			printf("rcode = %d, ancount=%d\n", hp->rcode, ancount);
205 		return;
206 	}
207 	cp = (u_char *)&answer + sizeof(HEADER);
208 	eom = (u_char *)&answer + n;
209 	for (qdcount = ntohs(hp->qdcount); qdcount--; cp += n + QFIXEDSZ)
210 		if ((n = __dn_skipname(cp, eom)) < 0)
211 			return;
212 
213 	/*
214 	 * just in case someone puts a CNAME record after another record,
215 	 * check all records for CNAME; otherwise, just take the first
216 	 * name found.
217 	 */
218 	for (first = 1; --ancount >= 0 && cp < eom; cp += n) {
219 		if ((n = dn_expand((u_char *)&answer,
220 		    eom, cp, (u_char *)nbuf, sizeof(nbuf))) < 0)
221 			break;
222 		if (first) {			/* XXX */
223 			(void)strncpy(host, nbuf, hbsize);
224 			host[hbsize - 1] = '\0';
225 			first = 0;
226 		}
227 		cp += n;
228 		GETSHORT(type, cp);
229  		cp += sizeof(u_short) + sizeof(u_long);
230 		GETSHORT(n, cp);
231 		if (type == T_CNAME)  {
232 			/*
233 			 * assume that only one cname will be found.  More
234 			 * than one is undefined.  Copy so that if dn_expand
235 			 * fails, `host' is still okay.
236 			 */
237 			if ((n = dn_expand((u_char *)&answer,
238 			    eom, cp, (u_char *)nbuf, sizeof(nbuf))) < 0)
239 				break;
240 			(void)strncpy(host, nbuf, hbsize); /* XXX */
241 			host[hbsize - 1] = '\0';
242 			if (++loopcnt > 8)	/* never be more than 1 */
243 				return;
244 			goto loop;
245 		}
246 	}
247 }
248 
249 #else /* not NAMED_BIND */
250 
251 #include <netdb.h>
252 
253 getcanonname(host, hbsize)
254 	char *host;
255 	int hbsize;
256 {
257 	struct hostent *hp;
258 
259 	hp = gethostbyname(host);
260 	if (hp == NULL)
261 		return;
262 
263 	if (strlen(hp->h_name) >= hbsize)
264 		return;
265 
266 	(void) strcpy(host, hp->h_name);
267 }
268 
269 #endif /* not NAMED_BIND */
270