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 #include "port_before.h"
787c478bd9Sstevel@tonic-gate #include <sys/types.h>
797c478bd9Sstevel@tonic-gate #include <sys/param.h>
807c478bd9Sstevel@tonic-gate #include <netinet/in.h>
817c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
827c478bd9Sstevel@tonic-gate #include <ctype.h>
837c478bd9Sstevel@tonic-gate #include <resolv.h>
847c478bd9Sstevel@tonic-gate #include <stdio.h>
857c478bd9Sstevel@tonic-gate #include <string.h>
867c478bd9Sstevel@tonic-gate #include <unistd.h>
877c478bd9Sstevel@tonic-gate #include "port_after.h"
887c478bd9Sstevel@tonic-gate 
89*9525b14bSRao Shoaib #ifndef	ORIGINAL_ISC_CODE
907c478bd9Sstevel@tonic-gate #pragma weak	__dn_skipname	=	dn_skipname
917c478bd9Sstevel@tonic-gate #pragma weak	__res_dnok	=	res_dnok
927c478bd9Sstevel@tonic-gate #pragma weak	__res_hnok	=	res_hnok
937c478bd9Sstevel@tonic-gate #pragma weak	__res_mailok	=	res_mailok
947c478bd9Sstevel@tonic-gate #pragma weak	__res_ownok	=	res_ownok
957c478bd9Sstevel@tonic-gate #endif	/* ORIGINAL_ISC_CODE */
96*9525b14bSRao Shoaib 
97*9525b14bSRao Shoaib /*%
987c478bd9Sstevel@tonic-gate  * Expand compressed domain name 'src' to full domain name.
99*9525b14bSRao Shoaib  *
100*9525b14bSRao Shoaib  * \li 'msg' is a pointer to the begining of the message,
101*9525b14bSRao Shoaib  * \li 'eom' points to the first location after the message,
102*9525b14bSRao Shoaib  * \li 'dst' is a pointer to a buffer of size 'dstsiz' for the result.
103*9525b14bSRao Shoaib  * \li Return size of compressed name or -1 if there was an error.
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate int
dn_expand(const u_char * msg,const u_char * eom,const u_char * src,char * dst,int dstsiz)1067c478bd9Sstevel@tonic-gate dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
1077c478bd9Sstevel@tonic-gate 	  char *dst, int dstsiz)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if (n > 0 && dst[0] == '.')
1127c478bd9Sstevel@tonic-gate 		dst[0] = '\0';
1137c478bd9Sstevel@tonic-gate 	return (n);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
116*9525b14bSRao Shoaib /*%
1177c478bd9Sstevel@tonic-gate  * Pack domain name 'exp_dn' in presentation form into 'comp_dn'.
118*9525b14bSRao Shoaib  *
119*9525b14bSRao Shoaib  * \li Return the size of the compressed name or -1.
120*9525b14bSRao Shoaib  * \li 'length' is the size of the array pointed to by 'comp_dn'.
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate int
dn_comp(const char * src,u_char * dst,int dstsiz,u_char ** dnptrs,u_char ** lastdnptr)1237c478bd9Sstevel@tonic-gate dn_comp(const char *src, u_char *dst, int dstsiz,
1247c478bd9Sstevel@tonic-gate 	u_char **dnptrs, u_char **lastdnptr)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	return (ns_name_compress(src, dst, (size_t)dstsiz,
1277c478bd9Sstevel@tonic-gate 				 (const u_char **)dnptrs,
1287c478bd9Sstevel@tonic-gate 				 (const u_char **)lastdnptr));
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
131*9525b14bSRao Shoaib 
132*9525b14bSRao Shoaib /*%
1337c478bd9Sstevel@tonic-gate  * Skip over a compressed domain name. Return the size or -1.
1347c478bd9Sstevel@tonic-gate  */
1357c478bd9Sstevel@tonic-gate int
dn_skipname(const u_char * ptr,const u_char * eom)1367c478bd9Sstevel@tonic-gate dn_skipname(const u_char *ptr, const u_char *eom) {
1377c478bd9Sstevel@tonic-gate 	const u_char *saveptr = ptr;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	if (ns_name_skip(&ptr, eom) == -1)
1407c478bd9Sstevel@tonic-gate 		return (-1);
1417c478bd9Sstevel@tonic-gate 	return (ptr - saveptr);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate 
144*9525b14bSRao Shoaib /*%
1457c478bd9Sstevel@tonic-gate  * Verify that a domain name uses an acceptable character set.
146*9525b14bSRao Shoaib  *
1477c478bd9Sstevel@tonic-gate  * Note the conspicuous absence of ctype macros in these definitions.  On
1487c478bd9Sstevel@tonic-gate  * non-ASCII hosts, we can't depend on string literals or ctype macros to
1497c478bd9Sstevel@tonic-gate  * tell us anything about network-format data.  The rest of the BIND system
1507c478bd9Sstevel@tonic-gate  * is not careful about this, but for some reason, we're doing it right here.
1517c478bd9Sstevel@tonic-gate  */
1527c478bd9Sstevel@tonic-gate #define PERIOD 0x2e
1537c478bd9Sstevel@tonic-gate #define	hyphenchar(c) ((c) == 0x2d)
1547c478bd9Sstevel@tonic-gate #define bslashchar(c) ((c) == 0x5c)
1557c478bd9Sstevel@tonic-gate #ifdef	SUNW_HNOK_UNDERSCORE
1567c478bd9Sstevel@tonic-gate #define	underscorechar(c)	((c) == 0x5f)
1577c478bd9Sstevel@tonic-gate #endif	/* SUNW_HNOK_UNDERSCORE */
1587c478bd9Sstevel@tonic-gate #define periodchar(c) ((c) == PERIOD)
1597c478bd9Sstevel@tonic-gate #define asterchar(c) ((c) == 0x2a)
1607c478bd9Sstevel@tonic-gate #define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \
1617c478bd9Sstevel@tonic-gate 		   || ((c) >= 0x61 && (c) <= 0x7a))
1627c478bd9Sstevel@tonic-gate #define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate #define borderchar(c) (alphachar(c) || digitchar(c))
1657c478bd9Sstevel@tonic-gate #ifdef	SUNW_HNOK_UNDERSCORE
1667c478bd9Sstevel@tonic-gate #define middlechar(c) (borderchar(c) || hyphenchar(c) || underscorechar(c))
1677c478bd9Sstevel@tonic-gate #else
1687c478bd9Sstevel@tonic-gate #define middlechar(c) (borderchar(c) || hyphenchar(c))
1697c478bd9Sstevel@tonic-gate #endif	/* SUNW_HNOK_UNDERSCORE */
1707c478bd9Sstevel@tonic-gate #define	domainchar(c) ((c) > 0x20 && (c) < 0x7f)
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate int
res_hnok(const char * dn)1737c478bd9Sstevel@tonic-gate res_hnok(const char *dn) {
174*9525b14bSRao Shoaib 	int pch = PERIOD, ch = *dn++;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	while (ch != '\0') {
1777c478bd9Sstevel@tonic-gate 		int nch = *dn++;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 		if (periodchar(ch)) {
1807c478bd9Sstevel@tonic-gate 			(void)NULL;
1817c478bd9Sstevel@tonic-gate 		} else if (periodchar(pch)) {
1827c478bd9Sstevel@tonic-gate 			if (!borderchar(ch))
1837c478bd9Sstevel@tonic-gate 				return (0);
1847c478bd9Sstevel@tonic-gate 		} else if (periodchar(nch) || nch == '\0') {
1857c478bd9Sstevel@tonic-gate 			if (!borderchar(ch))
1867c478bd9Sstevel@tonic-gate 				return (0);
1877c478bd9Sstevel@tonic-gate 		} else {
1887c478bd9Sstevel@tonic-gate 			if (!middlechar(ch))
1897c478bd9Sstevel@tonic-gate 				return (0);
1907c478bd9Sstevel@tonic-gate 		}
191*9525b14bSRao Shoaib 		pch = ch, ch = nch;
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 	return (1);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
196*9525b14bSRao Shoaib /*%
1977c478bd9Sstevel@tonic-gate  * hostname-like (A, MX, WKS) owners can have "*" as their first label
1987c478bd9Sstevel@tonic-gate  * but must otherwise be as a host name.
1997c478bd9Sstevel@tonic-gate  */
2007c478bd9Sstevel@tonic-gate int
res_ownok(const char * dn)2017c478bd9Sstevel@tonic-gate res_ownok(const char *dn) {
2027c478bd9Sstevel@tonic-gate 	if (asterchar(dn[0])) {
2037c478bd9Sstevel@tonic-gate 		if (periodchar(dn[1]))
2047c478bd9Sstevel@tonic-gate 			return (res_hnok(dn+2));
2057c478bd9Sstevel@tonic-gate 		if (dn[1] == '\0')
2067c478bd9Sstevel@tonic-gate 			return (1);
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 	return (res_hnok(dn));
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
211*9525b14bSRao Shoaib /*%
2127c478bd9Sstevel@tonic-gate  * SOA RNAMEs and RP RNAMEs can have any printable character in their first
2137c478bd9Sstevel@tonic-gate  * label, but the rest of the name has to look like a host name.
2147c478bd9Sstevel@tonic-gate  */
2157c478bd9Sstevel@tonic-gate int
res_mailok(const char * dn)2167c478bd9Sstevel@tonic-gate res_mailok(const char *dn) {
2177c478bd9Sstevel@tonic-gate 	int ch, escaped = 0;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	/* "." is a valid missing representation */
2207c478bd9Sstevel@tonic-gate 	if (*dn == '\0')
2217c478bd9Sstevel@tonic-gate 		return (1);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	/* otherwise <label>.<hostname> */
2247c478bd9Sstevel@tonic-gate 	while ((ch = *dn++) != '\0') {
2257c478bd9Sstevel@tonic-gate 		if (!domainchar(ch))
2267c478bd9Sstevel@tonic-gate 			return (0);
2277c478bd9Sstevel@tonic-gate 		if (!escaped && periodchar(ch))
2287c478bd9Sstevel@tonic-gate 			break;
2297c478bd9Sstevel@tonic-gate 		if (escaped)
2307c478bd9Sstevel@tonic-gate 			escaped = 0;
2317c478bd9Sstevel@tonic-gate 		else if (bslashchar(ch))
2327c478bd9Sstevel@tonic-gate 			escaped = 1;
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 	if (periodchar(ch))
2357c478bd9Sstevel@tonic-gate 		return (res_hnok(dn));
2367c478bd9Sstevel@tonic-gate 	return (0);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
239*9525b14bSRao Shoaib /*%
240*9525b14bSRao Shoaib  * This function is quite liberal, since RFC1034's character sets are only
2417c478bd9Sstevel@tonic-gate  * recommendations.
2427c478bd9Sstevel@tonic-gate  */
2437c478bd9Sstevel@tonic-gate int
res_dnok(const char * dn)2447c478bd9Sstevel@tonic-gate res_dnok(const char *dn) {
2457c478bd9Sstevel@tonic-gate 	int ch;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	while ((ch = *dn++) != '\0')
2487c478bd9Sstevel@tonic-gate 		if (!domainchar(ch))
2497c478bd9Sstevel@tonic-gate 			return (0);
2507c478bd9Sstevel@tonic-gate 	return (1);
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate #ifdef BIND_4_COMPAT
254*9525b14bSRao Shoaib /*%
2557c478bd9Sstevel@tonic-gate  * This module must export the following externally-visible symbols:
2567c478bd9Sstevel@tonic-gate  *	___putlong
2577c478bd9Sstevel@tonic-gate  *	___putshort
2587c478bd9Sstevel@tonic-gate  *	__getlong
2597c478bd9Sstevel@tonic-gate  *	__getshort
2607c478bd9Sstevel@tonic-gate  * Note that one _ comes from C and the others come from us.
2617c478bd9Sstevel@tonic-gate  */
262*9525b14bSRao Shoaib 
263*9525b14bSRao Shoaib #ifdef SOLARIS2
264*9525b14bSRao Shoaib #ifdef  __putlong
265*9525b14bSRao Shoaib #undef  __putlong
2667c478bd9Sstevel@tonic-gate #endif
267*9525b14bSRao Shoaib #ifdef  __putshort
268*9525b14bSRao Shoaib #undef  __putshort
2697c478bd9Sstevel@tonic-gate #endif
270*9525b14bSRao Shoaib #pragma weak    putlong         =       __putlong
271*9525b14bSRao Shoaib #pragma weak    putshort        =       __putshort
272*9525b14bSRao Shoaib #endif /* SOLARIS2 */
273*9525b14bSRao Shoaib 
__putlong(u_int32_t src,u_char * dst)2747c478bd9Sstevel@tonic-gate void __putlong(u_int32_t src, u_char *dst) { ns_put32(src, dst); }
__putshort(u_int16_t src,u_char * dst)2757c478bd9Sstevel@tonic-gate void __putshort(u_int16_t src, u_char *dst) { ns_put16(src, dst); }
2767c478bd9Sstevel@tonic-gate #ifndef __ultrix__
_getlong(const u_char * src)2777c478bd9Sstevel@tonic-gate u_int32_t _getlong(const u_char *src) { return (ns_get32(src)); }
_getshort(const u_char * src)2787c478bd9Sstevel@tonic-gate u_int16_t _getshort(const u_char *src) { return (ns_get16(src)); }
2797c478bd9Sstevel@tonic-gate #endif /*__ultrix__*/
2807c478bd9Sstevel@tonic-gate #endif /*BIND_4_COMPAT*/
281*9525b14bSRao Shoaib 
282*9525b14bSRao Shoaib /*! \file */
283