xref: /minix3/lib/libc/resolv/res_comp.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: res_comp.c,v 1.13 2015/02/24 17:56:20 christos Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras  * Copyright (c) 1985, 1993
52fe8fb19SBen Gras  *    The Regents of the University of California.  All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras  * are met:
102fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc  * 3. Neither the name of the University nor the names of its contributors
162fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
172fe8fb19SBen Gras  *    without specific prior written permission.
182fe8fb19SBen Gras  *
192fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292fe8fb19SBen Gras  * SUCH DAMAGE.
302fe8fb19SBen Gras  */
312fe8fb19SBen Gras 
322fe8fb19SBen Gras /*
332fe8fb19SBen Gras  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
342fe8fb19SBen Gras  *
352fe8fb19SBen Gras  * Permission to use, copy, modify, and distribute this software for any
362fe8fb19SBen Gras  * purpose with or without fee is hereby granted, provided that the above
372fe8fb19SBen Gras  * copyright notice and this permission notice appear in all copies, and that
382fe8fb19SBen Gras  * the name of Digital Equipment Corporation not be used in advertising or
392fe8fb19SBen Gras  * publicity pertaining to distribution of the document or software without
402fe8fb19SBen Gras  * specific, written prior permission.
412fe8fb19SBen Gras  *
422fe8fb19SBen Gras  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
432fe8fb19SBen Gras  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
442fe8fb19SBen Gras  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
452fe8fb19SBen Gras  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
462fe8fb19SBen Gras  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
472fe8fb19SBen Gras  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
482fe8fb19SBen Gras  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
492fe8fb19SBen Gras  * SOFTWARE.
502fe8fb19SBen Gras  */
512fe8fb19SBen Gras 
522fe8fb19SBen Gras /*
532fe8fb19SBen Gras  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
542fe8fb19SBen Gras  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
552fe8fb19SBen Gras  *
562fe8fb19SBen Gras  * Permission to use, copy, modify, and distribute this software for any
572fe8fb19SBen Gras  * purpose with or without fee is hereby granted, provided that the above
582fe8fb19SBen Gras  * copyright notice and this permission notice appear in all copies.
592fe8fb19SBen Gras  *
602fe8fb19SBen Gras  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
612fe8fb19SBen Gras  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
622fe8fb19SBen Gras  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
632fe8fb19SBen Gras  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
642fe8fb19SBen Gras  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
652fe8fb19SBen Gras  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
662fe8fb19SBen Gras  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
672fe8fb19SBen Gras  */
682fe8fb19SBen Gras #include <sys/cdefs.h>
692fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
702fe8fb19SBen Gras #ifdef notdef
712fe8fb19SBen Gras static const char sccsid[] = "@(#)res_comp.c	8.1 (Berkeley) 6/4/93";
722fe8fb19SBen Gras static const char rcsid[] = "Id: res_comp.c,v 1.5 2005/07/28 06:51:50 marka Exp";
732fe8fb19SBen Gras #else
74*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: res_comp.c,v 1.13 2015/02/24 17:56:20 christos Exp $");
752fe8fb19SBen Gras #endif
762fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
772fe8fb19SBen Gras 
782fe8fb19SBen Gras #include "port_before.h"
792fe8fb19SBen Gras 
802fe8fb19SBen Gras #include "namespace.h"
812fe8fb19SBen Gras #include <sys/types.h>
822fe8fb19SBen Gras #include <sys/param.h>
832fe8fb19SBen Gras #include <netinet/in.h>
842fe8fb19SBen Gras #include <arpa/nameser.h>
85f14fb602SLionel Sambuc #include <assert.h>
862fe8fb19SBen Gras #include <ctype.h>
872fe8fb19SBen Gras #include <resolv.h>
882fe8fb19SBen Gras #include <stdio.h>
892fe8fb19SBen Gras #include <string.h>
902fe8fb19SBen Gras #include <unistd.h>
912fe8fb19SBen Gras #include "port_after.h"
922fe8fb19SBen Gras 
932fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(dn_expand,_dn_expand)942fe8fb19SBen Gras __weak_alias(dn_expand,_dn_expand)
952fe8fb19SBen Gras __weak_alias(dn_comp,__dn_comp)
9684d9c625SLionel Sambuc #if defined(__minix)
972fe8fb19SBen Gras __weak_alias(dn_skipname,__dn_skipname)
982fe8fb19SBen Gras __weak_alias(res_hnok,__res_hnok)
992fe8fb19SBen Gras __weak_alias(res_ownok,__res_ownok)
1002fe8fb19SBen Gras __weak_alias(res_mailok,__res_mailok)
1012fe8fb19SBen Gras __weak_alias(res_dnok,__res_dnok)
10284d9c625SLionel Sambuc #endif /* defined(__minix) */
1032fe8fb19SBen Gras #endif
1042fe8fb19SBen Gras 
1052fe8fb19SBen Gras /*%
1062fe8fb19SBen Gras  * Expand compressed domain name 'src' to full domain name.
1072fe8fb19SBen Gras  *
1082fe8fb19SBen Gras  * \li 'msg' is a pointer to the begining of the message,
1092fe8fb19SBen Gras  * \li 'eom' points to the first location after the message,
1102fe8fb19SBen Gras  * \li 'dst' is a pointer to a buffer of size 'dstsiz' for the result.
1112fe8fb19SBen Gras  * \li Return size of compressed name or -1 if there was an error.
1122fe8fb19SBen Gras  */
1132fe8fb19SBen Gras int
1142fe8fb19SBen Gras dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
1152fe8fb19SBen Gras 	  char *dst, int dstsiz)
1162fe8fb19SBen Gras {
1172fe8fb19SBen Gras 	int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
1182fe8fb19SBen Gras 
1192fe8fb19SBen Gras 	if (n > 0 && dst[0] == '.')
1202fe8fb19SBen Gras 		dst[0] = '\0';
1212fe8fb19SBen Gras 	return (n);
1222fe8fb19SBen Gras }
1232fe8fb19SBen Gras 
1242fe8fb19SBen Gras /*%
1252fe8fb19SBen Gras  * Pack domain name 'exp_dn' in presentation form into 'comp_dn'.
1262fe8fb19SBen Gras  *
1272fe8fb19SBen Gras  * \li Return the size of the compressed name or -1.
1282fe8fb19SBen Gras  * \li 'length' is the size of the array pointed to by 'comp_dn'.
1292fe8fb19SBen Gras  */
1302fe8fb19SBen Gras int
dn_comp(const char * src,u_char * dst,int dstsiz,u_char ** dnptrs,u_char ** lastdnptr)1312fe8fb19SBen Gras dn_comp(const char *src, u_char *dst, int dstsiz,
1322fe8fb19SBen Gras 	u_char **dnptrs, u_char **lastdnptr)
1332fe8fb19SBen Gras {
1342fe8fb19SBen Gras 	return (ns_name_compress(src, dst, (size_t)dstsiz,
135f14fb602SLionel Sambuc 				 (void *)dnptrs,
136f14fb602SLionel Sambuc 				 (void *)lastdnptr));
1372fe8fb19SBen Gras }
1382fe8fb19SBen Gras 
1392fe8fb19SBen Gras /*%
1402fe8fb19SBen Gras  * Skip over a compressed domain name. Return the size or -1.
1412fe8fb19SBen Gras  */
1422fe8fb19SBen Gras int
dn_skipname(const u_char * ptr,const u_char * eom)1432fe8fb19SBen Gras dn_skipname(const u_char *ptr, const u_char *eom) {
1442fe8fb19SBen Gras 	const u_char *saveptr = ptr;
1452fe8fb19SBen Gras 
1462fe8fb19SBen Gras 	if (ns_name_skip(&ptr, eom) == -1)
1472fe8fb19SBen Gras 		return (-1);
148f14fb602SLionel Sambuc 	_DIAGASSERT(__type_fit(int, ptr - saveptr));
149f14fb602SLionel Sambuc 	return (int)(ptr - saveptr);
1502fe8fb19SBen Gras }
1512fe8fb19SBen Gras 
1522fe8fb19SBen Gras /*%
1532fe8fb19SBen Gras  * Verify that a domain name uses an acceptable character set.
1542fe8fb19SBen Gras  *
1552fe8fb19SBen Gras  * Note the conspicuous absence of ctype macros in these definitions.  On
1562fe8fb19SBen Gras  * non-ASCII hosts, we can't depend on string literals or ctype macros to
1572fe8fb19SBen Gras  * tell us anything about network-format data.  The rest of the BIND system
1582fe8fb19SBen Gras  * is not careful about this, but for some reason, we're doing it right here.
1592fe8fb19SBen Gras  */
1602fe8fb19SBen Gras #define PERIOD 0x2e
1612fe8fb19SBen Gras #define	hyphenchar(c) ((c) == 0x2d)
1622fe8fb19SBen Gras #define bslashchar(c) ((c) == 0x5c)
1632fe8fb19SBen Gras #define periodchar(c) ((c) == PERIOD)
1642fe8fb19SBen Gras #define asterchar(c) ((c) == 0x2a)
1652fe8fb19SBen Gras #define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \
1662fe8fb19SBen Gras 		   || ((c) >= 0x61 && (c) <= 0x7a))
1672fe8fb19SBen Gras #define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)
1682fe8fb19SBen Gras 
1692fe8fb19SBen Gras #define borderchar(c) (alphachar(c) || digitchar(c))
1702fe8fb19SBen Gras #define middlechar(c) (borderchar(c) || hyphenchar(c))
1712fe8fb19SBen Gras #define	domainchar(c) ((c) > 0x20 && (c) < 0x7f)
1722fe8fb19SBen Gras 
1732fe8fb19SBen Gras int
res_hnok(const char * dn)1742fe8fb19SBen Gras res_hnok(const char *dn) {
1752fe8fb19SBen Gras 	int pch = PERIOD, ch = *dn++;
1762fe8fb19SBen Gras 
1772fe8fb19SBen Gras 	while (ch != '\0') {
1782fe8fb19SBen Gras 		int nch = *dn++;
1792fe8fb19SBen Gras 
1802fe8fb19SBen Gras 		if (periodchar(ch)) {
1812fe8fb19SBen Gras 			;
1822fe8fb19SBen Gras 		} else if (periodchar(pch)) {
1832fe8fb19SBen Gras 			if (!borderchar(ch))
1842fe8fb19SBen Gras 				return (0);
1852fe8fb19SBen Gras 		} else if (periodchar(nch) || nch == '\0') {
1862fe8fb19SBen Gras 			if (!borderchar(ch))
1872fe8fb19SBen Gras 				return (0);
1882fe8fb19SBen Gras 		} else {
1892fe8fb19SBen Gras 			if (!middlechar(ch))
1902fe8fb19SBen Gras 				return (0);
1912fe8fb19SBen Gras 		}
1922fe8fb19SBen Gras 		pch = ch, ch = nch;
1932fe8fb19SBen Gras 	}
1942fe8fb19SBen Gras 	return (1);
1952fe8fb19SBen Gras }
1962fe8fb19SBen Gras 
1972fe8fb19SBen Gras /*%
1982fe8fb19SBen Gras  * hostname-like (A, MX, WKS) owners can have "*" as their first label
1992fe8fb19SBen Gras  * but must otherwise be as a host name.
2002fe8fb19SBen Gras  */
2012fe8fb19SBen Gras int
res_ownok(const char * dn)2022fe8fb19SBen Gras res_ownok(const char *dn) {
2032fe8fb19SBen Gras 	if (asterchar(dn[0])) {
2042fe8fb19SBen Gras 		if (periodchar(dn[1]))
2052fe8fb19SBen Gras 			return (res_hnok(dn+2));
2062fe8fb19SBen Gras 		if (dn[1] == '\0')
2072fe8fb19SBen Gras 			return (1);
2082fe8fb19SBen Gras 	}
2092fe8fb19SBen Gras 	return (res_hnok(dn));
2102fe8fb19SBen Gras }
2112fe8fb19SBen Gras 
2122fe8fb19SBen Gras /*%
2132fe8fb19SBen Gras  * SOA RNAMEs and RP RNAMEs can have any printable character in their first
2142fe8fb19SBen Gras  * label, but the rest of the name has to look like a host name.
2152fe8fb19SBen Gras  */
2162fe8fb19SBen Gras int
res_mailok(const char * dn)2172fe8fb19SBen Gras res_mailok(const char *dn) {
2182fe8fb19SBen Gras 	int ch, escaped = 0;
2192fe8fb19SBen Gras 
2202fe8fb19SBen Gras 	/* "." is a valid missing representation */
2212fe8fb19SBen Gras 	if (*dn == '\0')
2222fe8fb19SBen Gras 		return (1);
2232fe8fb19SBen Gras 
2242fe8fb19SBen Gras 	/* otherwise <label>.<hostname> */
2252fe8fb19SBen Gras 	while ((ch = *dn++) != '\0') {
2262fe8fb19SBen Gras 		if (!domainchar(ch))
2272fe8fb19SBen Gras 			return (0);
2282fe8fb19SBen Gras 		if (!escaped && periodchar(ch))
2292fe8fb19SBen Gras 			break;
2302fe8fb19SBen Gras 		if (escaped)
2312fe8fb19SBen Gras 			escaped = 0;
2322fe8fb19SBen Gras 		else if (bslashchar(ch))
2332fe8fb19SBen Gras 			escaped = 1;
2342fe8fb19SBen Gras 	}
2352fe8fb19SBen Gras 	if (periodchar(ch))
2362fe8fb19SBen Gras 		return (res_hnok(dn));
2372fe8fb19SBen Gras 	return (0);
2382fe8fb19SBen Gras }
2392fe8fb19SBen Gras 
2402fe8fb19SBen Gras /*%
2412fe8fb19SBen Gras  * This function is quite liberal, since RFC1034's character sets are only
2422fe8fb19SBen Gras  * recommendations.
2432fe8fb19SBen Gras  */
2442fe8fb19SBen Gras int
res_dnok(const char * dn)2452fe8fb19SBen Gras res_dnok(const char *dn) {
2462fe8fb19SBen Gras 	int ch;
2472fe8fb19SBen Gras 
2482fe8fb19SBen Gras 	while ((ch = *dn++) != '\0')
2492fe8fb19SBen Gras 		if (!domainchar(ch))
2502fe8fb19SBen Gras 			return (0);
2512fe8fb19SBen Gras 	return (1);
2522fe8fb19SBen Gras }
2532fe8fb19SBen Gras 
2542fe8fb19SBen Gras #ifdef BIND_4_COMPAT
2552fe8fb19SBen Gras /*%
2562fe8fb19SBen Gras  * This module must export the following externally-visible symbols:
2572fe8fb19SBen Gras  *	___putlong
2582fe8fb19SBen Gras  *	___putshort
2592fe8fb19SBen Gras  *	__getlong
2602fe8fb19SBen Gras  *	__getshort
2612fe8fb19SBen Gras  * Note that one _ comes from C and the others come from us.
2622fe8fb19SBen Gras  */
2632fe8fb19SBen Gras 
2642fe8fb19SBen Gras #ifdef SOLARIS2
2652fe8fb19SBen Gras #ifdef  __putlong
2662fe8fb19SBen Gras #undef  __putlong
2672fe8fb19SBen Gras #endif
2682fe8fb19SBen Gras #ifdef  __putshort
2692fe8fb19SBen Gras #undef  __putshort
2702fe8fb19SBen Gras #endif
2712fe8fb19SBen Gras #pragma weak    putlong         =       __putlong
2722fe8fb19SBen Gras #pragma weak    putshort        =       __putshort
2732fe8fb19SBen Gras #endif /* SOLARIS2 */
2742fe8fb19SBen Gras 
__putlong(u_int32_t src,u_char * dst)2752fe8fb19SBen Gras void __putlong(u_int32_t src, u_char *dst) { ns_put32(src, dst); }
__putshort(u_int16_t src,u_char * dst)2762fe8fb19SBen Gras void __putshort(u_int16_t src, u_char *dst) { ns_put16(src, dst); }
2772fe8fb19SBen Gras #ifndef __ultrix__
_getlong(const u_char * src)2782fe8fb19SBen Gras u_int32_t _getlong(const u_char *src) { return (ns_get32(src)); }
_getshort(const u_char * src)2792fe8fb19SBen Gras u_int16_t _getshort(const u_char *src) { return (ns_get16(src)); }
2802fe8fb19SBen Gras #endif /*__ultrix__*/
2812fe8fb19SBen Gras #endif /*BIND_4_COMPAT*/
2822fe8fb19SBen Gras 
2832fe8fb19SBen Gras /*! \file */
284