17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
6*9525b14bSRao Shoaib 
77c478bd9Sstevel@tonic-gate /*
87c478bd9Sstevel@tonic-gate  * Copyright (c) 1985, 1993
97c478bd9Sstevel@tonic-gate  *    The Regents of the University of California.  All rights reserved.
107c478bd9Sstevel@tonic-gate  *
117c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
127c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
137c478bd9Sstevel@tonic-gate  * are met:
147c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
157c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
167c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
177c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
187c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
197c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
207c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
217c478bd9Sstevel@tonic-gate  * 	This product includes software developed by the University of
227c478bd9Sstevel@tonic-gate  * 	California, Berkeley and its contributors.
237c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
247c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
257c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
287c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
297c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
307c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
317c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
327c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
337c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
347c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
357c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
367c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
377c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
447c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
457c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies, and that
467c478bd9Sstevel@tonic-gate  * the name of Digital Equipment Corporation not be used in advertising or
477c478bd9Sstevel@tonic-gate  * publicity pertaining to distribution of the document or software without
487c478bd9Sstevel@tonic-gate  * specific, written prior permission.
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
517c478bd9Sstevel@tonic-gate  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
527c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
537c478bd9Sstevel@tonic-gate  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
547c478bd9Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
557c478bd9Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
567c478bd9Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
577c478bd9Sstevel@tonic-gate  * SOFTWARE.
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
61*9525b14bSRao Shoaib  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
627c478bd9Sstevel@tonic-gate  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
657c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
667c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
677c478bd9Sstevel@tonic-gate  *
68*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
69*9525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
70*9525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
71*9525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
72*9525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
73*9525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
74*9525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
787c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)res_comp.c	8.1 (Berkeley) 6/4/93";
79*9525b14bSRao Shoaib static const char rcsid[] = "$Id: res_comp.c,v 1.5 2005/07/28 06:51:50 marka Exp $";
807c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #include "port_before.h"
837c478bd9Sstevel@tonic-gate #include <sys/types.h>
847c478bd9Sstevel@tonic-gate #include <sys/param.h>
857c478bd9Sstevel@tonic-gate #include <netinet/in.h>
867c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
877c478bd9Sstevel@tonic-gate #include <ctype.h>
887c478bd9Sstevel@tonic-gate #include <resolv.h>
897c478bd9Sstevel@tonic-gate #include <stdio.h>
907c478bd9Sstevel@tonic-gate #include <string.h>
917c478bd9Sstevel@tonic-gate #include <unistd.h>
927c478bd9Sstevel@tonic-gate #include "port_after.h"
937c478bd9Sstevel@tonic-gate 
94*9525b14bSRao Shoaib #ifndef	ORIGINAL_ISC_CODE
957c478bd9Sstevel@tonic-gate #pragma weak	__dn_skipname	=	dn_skipname
967c478bd9Sstevel@tonic-gate #pragma weak	__res_dnok	=	res_dnok
977c478bd9Sstevel@tonic-gate #pragma weak	__res_hnok	=	res_hnok
987c478bd9Sstevel@tonic-gate #pragma weak	__res_mailok	=	res_mailok
997c478bd9Sstevel@tonic-gate #pragma weak	__res_ownok	=	res_ownok
1007c478bd9Sstevel@tonic-gate #endif	/* ORIGINAL_ISC_CODE */
101*9525b14bSRao Shoaib 
102*9525b14bSRao Shoaib /*%
1037c478bd9Sstevel@tonic-gate  * Expand compressed domain name 'src' to full domain name.
104*9525b14bSRao Shoaib  *
105*9525b14bSRao Shoaib  * \li 'msg' is a pointer to the begining of the message,
106*9525b14bSRao Shoaib  * \li 'eom' points to the first location after the message,
107*9525b14bSRao Shoaib  * \li 'dst' is a pointer to a buffer of size 'dstsiz' for the result.
108*9525b14bSRao Shoaib  * \li Return size of compressed name or -1 if there was an error.
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate int
1117c478bd9Sstevel@tonic-gate dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
1127c478bd9Sstevel@tonic-gate 	  char *dst, int dstsiz)
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate 	int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	if (n > 0 && dst[0] == '.')
1177c478bd9Sstevel@tonic-gate 		dst[0] = '\0';
1187c478bd9Sstevel@tonic-gate 	return (n);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
121*9525b14bSRao Shoaib /*%
1227c478bd9Sstevel@tonic-gate  * Pack domain name 'exp_dn' in presentation form into 'comp_dn'.
123*9525b14bSRao Shoaib  *
124*9525b14bSRao Shoaib  * \li Return the size of the compressed name or -1.
125*9525b14bSRao Shoaib  * \li 'length' is the size of the array pointed to by 'comp_dn'.
1267c478bd9Sstevel@tonic-gate  */
1277c478bd9Sstevel@tonic-gate int
1287c478bd9Sstevel@tonic-gate dn_comp(const char *src, u_char *dst, int dstsiz,
1297c478bd9Sstevel@tonic-gate 	u_char **dnptrs, u_char **lastdnptr)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	return (ns_name_compress(src, dst, (size_t)dstsiz,
1327c478bd9Sstevel@tonic-gate 				 (const u_char **)dnptrs,
1337c478bd9Sstevel@tonic-gate 				 (const u_char **)lastdnptr));
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
136*9525b14bSRao Shoaib 
137*9525b14bSRao Shoaib /*%
1387c478bd9Sstevel@tonic-gate  * Skip over a compressed domain name. Return the size or -1.
1397c478bd9Sstevel@tonic-gate  */
1407c478bd9Sstevel@tonic-gate int
1417c478bd9Sstevel@tonic-gate dn_skipname(const u_char *ptr, const u_char *eom) {
1427c478bd9Sstevel@tonic-gate 	const u_char *saveptr = ptr;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if (ns_name_skip(&ptr, eom) == -1)
1457c478bd9Sstevel@tonic-gate 		return (-1);
1467c478bd9Sstevel@tonic-gate 	return (ptr - saveptr);
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate 
149*9525b14bSRao Shoaib /*%
1507c478bd9Sstevel@tonic-gate  * Verify that a domain name uses an acceptable character set.
151*9525b14bSRao Shoaib  *
1527c478bd9Sstevel@tonic-gate  * Note the conspicuous absence of ctype macros in these definitions.  On
1537c478bd9Sstevel@tonic-gate  * non-ASCII hosts, we can't depend on string literals or ctype macros to
1547c478bd9Sstevel@tonic-gate  * tell us anything about network-format data.  The rest of the BIND system
1557c478bd9Sstevel@tonic-gate  * is not careful about this, but for some reason, we're doing it right here.
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate #define PERIOD 0x2e
1587c478bd9Sstevel@tonic-gate #define	hyphenchar(c) ((c) == 0x2d)
1597c478bd9Sstevel@tonic-gate #define bslashchar(c) ((c) == 0x5c)
1607c478bd9Sstevel@tonic-gate #ifdef	SUNW_HNOK_UNDERSCORE
1617c478bd9Sstevel@tonic-gate #define	underscorechar(c)	((c) == 0x5f)
1627c478bd9Sstevel@tonic-gate #endif	/* SUNW_HNOK_UNDERSCORE */
1637c478bd9Sstevel@tonic-gate #define periodchar(c) ((c) == PERIOD)
1647c478bd9Sstevel@tonic-gate #define asterchar(c) ((c) == 0x2a)
1657c478bd9Sstevel@tonic-gate #define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \
1667c478bd9Sstevel@tonic-gate 		   || ((c) >= 0x61 && (c) <= 0x7a))
1677c478bd9Sstevel@tonic-gate #define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate #define borderchar(c) (alphachar(c) || digitchar(c))
1707c478bd9Sstevel@tonic-gate #ifdef	SUNW_HNOK_UNDERSCORE
1717c478bd9Sstevel@tonic-gate #define middlechar(c) (borderchar(c) || hyphenchar(c) || underscorechar(c))
1727c478bd9Sstevel@tonic-gate #else
1737c478bd9Sstevel@tonic-gate #define middlechar(c) (borderchar(c) || hyphenchar(c))
1747c478bd9Sstevel@tonic-gate #endif	/* SUNW_HNOK_UNDERSCORE */
1757c478bd9Sstevel@tonic-gate #define	domainchar(c) ((c) > 0x20 && (c) < 0x7f)
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate int
1787c478bd9Sstevel@tonic-gate res_hnok(const char *dn) {
179*9525b14bSRao Shoaib 	int pch = PERIOD, ch = *dn++;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	while (ch != '\0') {
1827c478bd9Sstevel@tonic-gate 		int nch = *dn++;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 		if (periodchar(ch)) {
1857c478bd9Sstevel@tonic-gate 			(void)NULL;
1867c478bd9Sstevel@tonic-gate 		} else if (periodchar(pch)) {
1877c478bd9Sstevel@tonic-gate 			if (!borderchar(ch))
1887c478bd9Sstevel@tonic-gate 				return (0);
1897c478bd9Sstevel@tonic-gate 		} else if (periodchar(nch) || nch == '\0') {
1907c478bd9Sstevel@tonic-gate 			if (!borderchar(ch))
1917c478bd9Sstevel@tonic-gate 				return (0);
1927c478bd9Sstevel@tonic-gate 		} else {
1937c478bd9Sstevel@tonic-gate 			if (!middlechar(ch))
1947c478bd9Sstevel@tonic-gate 				return (0);
1957c478bd9Sstevel@tonic-gate 		}
196*9525b14bSRao Shoaib 		pch = ch, ch = nch;
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 	return (1);
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
201*9525b14bSRao Shoaib /*%
2027c478bd9Sstevel@tonic-gate  * hostname-like (A, MX, WKS) owners can have "*" as their first label
2037c478bd9Sstevel@tonic-gate  * but must otherwise be as a host name.
2047c478bd9Sstevel@tonic-gate  */
2057c478bd9Sstevel@tonic-gate int
2067c478bd9Sstevel@tonic-gate res_ownok(const char *dn) {
2077c478bd9Sstevel@tonic-gate 	if (asterchar(dn[0])) {
2087c478bd9Sstevel@tonic-gate 		if (periodchar(dn[1]))
2097c478bd9Sstevel@tonic-gate 			return (res_hnok(dn+2));
2107c478bd9Sstevel@tonic-gate 		if (dn[1] == '\0')
2117c478bd9Sstevel@tonic-gate 			return (1);
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 	return (res_hnok(dn));
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
216*9525b14bSRao Shoaib /*%
2177c478bd9Sstevel@tonic-gate  * SOA RNAMEs and RP RNAMEs can have any printable character in their first
2187c478bd9Sstevel@tonic-gate  * label, but the rest of the name has to look like a host name.
2197c478bd9Sstevel@tonic-gate  */
2207c478bd9Sstevel@tonic-gate int
2217c478bd9Sstevel@tonic-gate res_mailok(const char *dn) {
2227c478bd9Sstevel@tonic-gate 	int ch, escaped = 0;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	/* "." is a valid missing representation */
2257c478bd9Sstevel@tonic-gate 	if (*dn == '\0')
2267c478bd9Sstevel@tonic-gate 		return (1);
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	/* otherwise <label>.<hostname> */
2297c478bd9Sstevel@tonic-gate 	while ((ch = *dn++) != '\0') {
2307c478bd9Sstevel@tonic-gate 		if (!domainchar(ch))
2317c478bd9Sstevel@tonic-gate 			return (0);
2327c478bd9Sstevel@tonic-gate 		if (!escaped && periodchar(ch))
2337c478bd9Sstevel@tonic-gate 			break;
2347c478bd9Sstevel@tonic-gate 		if (escaped)
2357c478bd9Sstevel@tonic-gate 			escaped = 0;
2367c478bd9Sstevel@tonic-gate 		else if (bslashchar(ch))
2377c478bd9Sstevel@tonic-gate 			escaped = 1;
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 	if (periodchar(ch))
2407c478bd9Sstevel@tonic-gate 		return (res_hnok(dn));
2417c478bd9Sstevel@tonic-gate 	return (0);
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate 
244*9525b14bSRao Shoaib /*%
245*9525b14bSRao Shoaib  * This function is quite liberal, since RFC1034's character sets are only
2467c478bd9Sstevel@tonic-gate  * recommendations.
2477c478bd9Sstevel@tonic-gate  */
2487c478bd9Sstevel@tonic-gate int
2497c478bd9Sstevel@tonic-gate res_dnok(const char *dn) {
2507c478bd9Sstevel@tonic-gate 	int ch;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	while ((ch = *dn++) != '\0')
2537c478bd9Sstevel@tonic-gate 		if (!domainchar(ch))
2547c478bd9Sstevel@tonic-gate 			return (0);
2557c478bd9Sstevel@tonic-gate 	return (1);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate #ifdef BIND_4_COMPAT
259*9525b14bSRao Shoaib /*%
2607c478bd9Sstevel@tonic-gate  * This module must export the following externally-visible symbols:
2617c478bd9Sstevel@tonic-gate  *	___putlong
2627c478bd9Sstevel@tonic-gate  *	___putshort
2637c478bd9Sstevel@tonic-gate  *	__getlong
2647c478bd9Sstevel@tonic-gate  *	__getshort
2657c478bd9Sstevel@tonic-gate  * Note that one _ comes from C and the others come from us.
2667c478bd9Sstevel@tonic-gate  */
267*9525b14bSRao Shoaib 
268*9525b14bSRao Shoaib #ifdef SOLARIS2
269*9525b14bSRao Shoaib #ifdef  __putlong
270*9525b14bSRao Shoaib #undef  __putlong
2717c478bd9Sstevel@tonic-gate #endif
272*9525b14bSRao Shoaib #ifdef  __putshort
273*9525b14bSRao Shoaib #undef  __putshort
2747c478bd9Sstevel@tonic-gate #endif
275*9525b14bSRao Shoaib #pragma weak    putlong         =       __putlong
276*9525b14bSRao Shoaib #pragma weak    putshort        =       __putshort
277*9525b14bSRao Shoaib #endif /* SOLARIS2 */
278*9525b14bSRao Shoaib 
2797c478bd9Sstevel@tonic-gate void __putlong(u_int32_t src, u_char *dst) { ns_put32(src, dst); }
2807c478bd9Sstevel@tonic-gate void __putshort(u_int16_t src, u_char *dst) { ns_put16(src, dst); }
2817c478bd9Sstevel@tonic-gate #ifndef __ultrix__
2827c478bd9Sstevel@tonic-gate u_int32_t _getlong(const u_char *src) { return (ns_get32(src)); }
2837c478bd9Sstevel@tonic-gate u_int16_t _getshort(const u_char *src) { return (ns_get16(src)); }
2847c478bd9Sstevel@tonic-gate #endif /*__ultrix__*/
2857c478bd9Sstevel@tonic-gate #endif /*BIND_4_COMPAT*/
286*9525b14bSRao Shoaib 
287*9525b14bSRao Shoaib /*! \file */
288