xref: /openbsd-src/lib/libc/net/res_comp.c (revision daf88648c0e349d5c02e1504293082072c981640)
1 /*	$OpenBSD: res_comp.c,v 1.13 2005/08/06 20:30:03 espie Exp $	*/
2 
3 /*
4  * ++Copyright++ 1985, 1993
5  * -
6  * Copyright (c) 1985, 1993
7  *    The Regents of the University of California.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  * -
33  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
34  *
35  * Permission to use, copy, modify, and distribute this software for any
36  * purpose with or without fee is hereby granted, provided that the above
37  * copyright notice and this permission notice appear in all copies, and that
38  * the name of Digital Equipment Corporation not be used in advertising or
39  * publicity pertaining to distribution of the document or software without
40  * specific, written prior permission.
41  *
42  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
45  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49  * SOFTWARE.
50  * -
51  * --Copyright--
52  */
53 
54 #include <sys/types.h>
55 #include <sys/param.h>
56 #include <netinet/in.h>
57 #include <arpa/nameser.h>
58 
59 #include <stdio.h>
60 #include <resolv.h>
61 #include <ctype.h>
62 
63 #include <unistd.h>
64 #include <string.h>
65 
66 static int dn_find(u_char *, u_char *, u_char **, u_char **);
67 
68 /*
69  * Expand compressed domain name 'comp_dn' to full domain name.
70  * 'msg' is a pointer to the begining of the message,
71  * 'eomorig' points to the first location after the message,
72  * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
73  * Return size of compressed name or -1 if there was an error.
74  */
75 int
76 dn_expand(const u_char *msg, const u_char *eomorig, const u_char *comp_dn,
77     char *exp_dn, int length)
78 {
79 	const u_char *cp;
80 	char *dn;
81 	int n, c;
82 	char *eom;
83 	int len = -1, checked = 0;
84 
85 	dn = exp_dn;
86 	cp = comp_dn;
87 	if (length > MAXHOSTNAMELEN-1)
88 		length = MAXHOSTNAMELEN-1;
89 	eom = exp_dn + length;
90 	/*
91 	 * fetch next label in domain name
92 	 */
93 	while ((n = *cp++)) {
94 		/*
95 		 * Check for indirection
96 		 */
97 		switch (n & INDIR_MASK) {
98 		case 0:
99 			if (dn != exp_dn) {
100 				if (dn >= eom)
101 					return (-1);
102 				*dn++ = '.';
103 			}
104 			if (dn+n >= eom)
105 				return (-1);
106 			checked += n + 1;
107 			while (--n >= 0) {
108 				if (((c = *cp++) == '.') || (c == '\\')) {
109 					if (dn + n + 2 >= eom)
110 						return (-1);
111 					*dn++ = '\\';
112 				}
113 				*dn++ = c;
114 				if (cp >= eomorig)	/* out of range */
115 					return (-1);
116 			}
117 			break;
118 
119 		case INDIR_MASK:
120 			if (len < 0)
121 				len = cp - comp_dn + 1;
122 			cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
123 			if (cp < msg || cp >= eomorig)	/* out of range */
124 				return (-1);
125 			checked += 2;
126 			/*
127 			 * Check for loops in the compressed name;
128 			 * if we've looked at the whole message,
129 			 * there must be a loop.
130 			 */
131 			if (checked >= eomorig - msg)
132 				return (-1);
133 			break;
134 
135 		default:
136 			return (-1);			/* flag error */
137 		}
138 	}
139 	*dn = '\0';
140 	if (len < 0)
141 		len = cp - comp_dn;
142 	return (len);
143 }
144 
145 /*
146  * Compress domain name 'exp_dn' into 'comp_dn'.
147  * Return the size of the compressed name or -1.
148  * 'length' is the size of the array pointed to by 'comp_dn'.
149  * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0]
150  * is a pointer to the beginning of the message. The list ends with NULL.
151  * 'lastdnptr' is a pointer to the end of the arrary pointed to
152  * by 'dnptrs'. Side effect is to update the list of pointers for
153  * labels inserted into the message as we compress the name.
154  * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
155  * is NULL, we don't update the list.
156  */
157 int
158 dn_comp(const char *exp_dn, u_char *comp_dn, int length, u_char **dnptrs,
159     u_char **lastdnptr)
160 {
161 	u_char *cp, *dn;
162 	int c, l;
163 	u_char **cpp, **lpp, *sp, *eob;
164 	u_char *msg;
165 
166 	dn = (u_char *)exp_dn;
167 	cp = comp_dn;
168 	eob = cp + length;
169 	lpp = cpp = NULL;
170 	if (dnptrs != NULL) {
171 		if ((msg = *dnptrs++) != NULL) {
172 			for (cpp = dnptrs; *cpp != NULL; cpp++)
173 				;
174 			lpp = cpp;	/* end of list to search */
175 		}
176 	} else
177 		msg = NULL;
178 	for (c = *dn++; c != '\0'; ) {
179 		/* look to see if we can use pointers */
180 		if (msg != NULL) {
181 			if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) {
182 				if (cp+1 >= eob)
183 					return (-1);
184 				*cp++ = (l >> 8) | INDIR_MASK;
185 				*cp++ = l % 256;
186 				return (cp - comp_dn);
187 			}
188 			/* not found, save it */
189 			if (lastdnptr != NULL && cpp < lastdnptr-1) {
190 				*cpp++ = cp;
191 				*cpp = NULL;
192 			}
193 		}
194 		sp = cp++;	/* save ptr to length byte */
195 		do {
196 			if (c == '.') {
197 				c = *dn++;
198 				break;
199 			}
200 			if (c == '\\') {
201 				if ((c = *dn++) == '\0')
202 					break;
203 			}
204 			if (cp >= eob) {
205 				if (msg != NULL)
206 					*lpp = NULL;
207 				return (-1);
208 			}
209 			*cp++ = c;
210 		} while ((c = *dn++) != '\0');
211 		/* catch trailing '.'s but not '..' */
212 		if ((l = cp - sp - 1) == 0 && c == '\0') {
213 			cp--;
214 			break;
215 		}
216 		if (l <= 0 || l > MAXLABEL) {
217 			if (msg != NULL)
218 				*lpp = NULL;
219 			return (-1);
220 		}
221 		*sp = l;
222 	}
223 	if (cp >= eob) {
224 		if (msg != NULL)
225 			*lpp = NULL;
226 		return (-1);
227 	}
228 	*cp++ = '\0';
229 	return (cp - comp_dn);
230 }
231 
232 /*
233  * Skip over a compressed domain name. Return the size or -1.
234  */
235 int
236 __dn_skipname(const u_char *comp_dn, const u_char *eom)
237 {
238 	const u_char *cp;
239 	int n;
240 
241 	cp = comp_dn;
242 	while (cp < eom && (n = *cp++)) {
243 		/*
244 		 * check for indirection
245 		 */
246 		switch (n & INDIR_MASK) {
247 		case 0:			/* normal case, n == len */
248 			cp += n;
249 			continue;
250 		case INDIR_MASK:	/* indirection */
251 			cp++;
252 			break;
253 		default:		/* illegal type */
254 			return (-1);
255 		}
256 		break;
257 	}
258 	if (cp > eom)
259 		return (-1);
260 	return (cp - comp_dn);
261 }
262 
263 static int
264 mklower(int ch)
265 {
266 	if (isascii(ch) && isupper(ch))
267 		return (tolower(ch));
268 	return (ch);
269 }
270 
271 /*
272  * Search for expanded name from a list of previously compressed names.
273  * Return the offset from msg if found or -1.
274  * dnptrs is the pointer to the first name on the list,
275  * not the pointer to the start of the message.
276  */
277 static int
278 dn_find(u_char *exp_dn, u_char *msg, u_char **dnptrs, u_char **lastdnptr)
279 {
280 	u_char *dn, *cp, **cpp;
281 	int n;
282 	u_char *sp;
283 
284 	for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
285 		dn = exp_dn;
286 		sp = cp = *cpp;
287 		while ((n = *cp++)) {
288 			/*
289 			 * check for indirection
290 			 */
291 			switch (n & INDIR_MASK) {
292 			case 0:		/* normal case, n == len */
293 				while (--n >= 0) {
294 					if (*dn == '.')
295 						goto next;
296 					if (*dn == '\\')
297 						dn++;
298 					if (mklower(*dn++) != mklower(*cp++))
299 						goto next;
300 				}
301 				if ((n = *dn++) == '\0' && *cp == '\0')
302 					return (sp - msg);
303 				if (n == '.')
304 					continue;
305 				goto next;
306 
307 			case INDIR_MASK:	/* indirection */
308 				cp = msg + (((n & 0x3f) << 8) | *cp);
309 				break;
310 
311 			default:	/* illegal type */
312 				return (-1);
313 			}
314 		}
315 		if (*dn == '\0')
316 			return (sp - msg);
317 	next:	;
318 	}
319 	return (-1);
320 }
321 
322 /*
323  * Verify that a domain name uses an acceptable character set.
324  */
325 
326 /*
327  * Note the conspicuous absence of ctype macros in these definitions.  On
328  * non-ASCII hosts, we can't depend on string literals or ctype macros to
329  * tell us anything about network-format data.  The rest of the BIND system
330  * is not careful about this, but for some reason, we're doing it right here.
331  */
332 #define PERIOD 0x2e
333 #define	hyphenchar(c) ((c) == 0x2d)
334 #define bslashchar(c) ((c) == 0x5c)
335 #define periodchar(c) ((c) == PERIOD)
336 #define asterchar(c) ((c) == 0x2a)
337 #define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \
338 		   || ((c) >= 0x61 && (c) <= 0x7a))
339 #define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)
340 
341 #define borderchar(c) (alphachar(c) || digitchar(c))
342 #define middlechar(c) (borderchar(c) || hyphenchar(c))
343 #define	domainchar(c) ((c) > 0x20 && (c) < 0x7f)
344 
345 int
346 res_hnok(const char *dn)
347 {
348 	int pch = PERIOD, ch = *dn++;
349 
350 	while (ch != '\0') {
351 		int nch = *dn++;
352 
353 		if (periodchar(ch)) {
354 			;
355 		} else if (periodchar(pch)) {
356 			if (!borderchar(ch))
357 				return (0);
358 		} else if (periodchar(nch) || nch == '\0') {
359 			if (!borderchar(ch))
360 				return (0);
361 		} else {
362 			if (!middlechar(ch))
363 				return (0);
364 		}
365 		pch = ch, ch = nch;
366 	}
367 	return (1);
368 }
369 
370 /*
371  * hostname-like (A, MX, WKS) owners can have "*" as their first label
372  * but must otherwise be as a host name.
373  */
374 int
375 res_ownok(const char *dn)
376 {
377 	if (asterchar(dn[0])) {
378 		if (periodchar(dn[1]))
379 			return (res_hnok(dn+2));
380 		if (dn[1] == '\0')
381 			return (1);
382 	}
383 	return (res_hnok(dn));
384 }
385 
386 /*
387  * SOA RNAMEs and RP RNAMEs can have any printable character in their first
388  * label, but the rest of the name has to look like a host name.
389  */
390 int
391 res_mailok(const char *dn)
392 {
393 	int ch, escaped = 0;
394 
395 	/* "." is a valid missing representation */
396 	if (*dn == '\0')
397 		return(1);
398 
399 	/* otherwise <label>.<hostname> */
400 	while ((ch = *dn++) != '\0') {
401 		if (!domainchar(ch))
402 			return (0);
403 		if (!escaped && periodchar(ch))
404 			break;
405 		if (escaped)
406 			escaped = 0;
407 		else if (bslashchar(ch))
408 			escaped = 1;
409 	}
410 	if (periodchar(ch))
411 		return (res_hnok(dn));
412 	return(0);
413 }
414 
415 /*
416  * This function is quite liberal, since RFC 1034's character sets are only
417  * recommendations.
418  */
419 int
420 res_dnok(const char *dn)
421 {
422 	int ch;
423 
424 	while ((ch = *dn++) != '\0')
425 		if (!domainchar(ch))
426 			return (0);
427 	return (1);
428 }
429 
430 /*
431  * Routines to insert/extract short/long's.
432  */
433 
434 u_int16_t
435 _getshort(const u_char *msgp)
436 {
437 	u_int16_t u;
438 
439 	GETSHORT(u, msgp);
440 	return (u);
441 }
442 
443 #ifdef NeXT
444 /*
445  * nExt machines have some funky library conventions, which we must maintain.
446  */
447 u_int16_t
448 res_getshort(msgp)
449 	const u_char *msgp;
450 {
451 	return (_getshort(msgp));
452 }
453 #endif
454 
455 u_int32_t
456 _getlong(const u_char *msgp)
457 {
458 	u_int32_t u;
459 
460 	GETLONG(u, msgp);
461 	return (u);
462 }
463 
464 void
465 __putshort(u_int16_t s, u_char *msgp)
466 {
467 	PUTSHORT(s, msgp);
468 }
469 
470 void
471 __putlong(u_int32_t l, u_char *msgp)
472 {
473 	PUTLONG(l, msgp);
474 }
475