10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
30Sstevel@tonic-gate * Use is subject to license terms.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate
6*11038SRao.Shoaib@Sun.COM
70Sstevel@tonic-gate /*
80Sstevel@tonic-gate * Copyright (c) 1985, 1993
90Sstevel@tonic-gate * The Regents of the University of California. All rights reserved.
100Sstevel@tonic-gate *
110Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
120Sstevel@tonic-gate * modification, are permitted provided that the following conditions
130Sstevel@tonic-gate * are met:
140Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
150Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
160Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
170Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
180Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
190Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
200Sstevel@tonic-gate * must display the following acknowledgement:
210Sstevel@tonic-gate * This product includes software developed by the University of
220Sstevel@tonic-gate * California, Berkeley and its contributors.
230Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors
240Sstevel@tonic-gate * may be used to endorse or promote products derived from this software
250Sstevel@tonic-gate * without specific prior written permission.
260Sstevel@tonic-gate *
270Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
280Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
290Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
300Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
310Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
320Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
330Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
340Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
350Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
360Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
370Sstevel@tonic-gate * SUCH DAMAGE.
380Sstevel@tonic-gate */
390Sstevel@tonic-gate
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate * Portions Copyright (c) 1993 by Digital Equipment Corporation.
420Sstevel@tonic-gate *
430Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
440Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
450Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies, and that
460Sstevel@tonic-gate * the name of Digital Equipment Corporation not be used in advertising or
470Sstevel@tonic-gate * publicity pertaining to distribution of the document or software without
480Sstevel@tonic-gate * specific, written prior permission.
490Sstevel@tonic-gate *
500Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
510Sstevel@tonic-gate * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
520Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
530Sstevel@tonic-gate * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
540Sstevel@tonic-gate * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
550Sstevel@tonic-gate * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
560Sstevel@tonic-gate * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
570Sstevel@tonic-gate * SOFTWARE.
580Sstevel@tonic-gate */
590Sstevel@tonic-gate
600Sstevel@tonic-gate /*
61*11038SRao.Shoaib@Sun.COM * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
620Sstevel@tonic-gate * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
630Sstevel@tonic-gate *
640Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
650Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
660Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
670Sstevel@tonic-gate *
68*11038SRao.Shoaib@Sun.COM * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
69*11038SRao.Shoaib@Sun.COM * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
70*11038SRao.Shoaib@Sun.COM * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
71*11038SRao.Shoaib@Sun.COM * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
72*11038SRao.Shoaib@Sun.COM * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
73*11038SRao.Shoaib@Sun.COM * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
74*11038SRao.Shoaib@Sun.COM * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
750Sstevel@tonic-gate */
760Sstevel@tonic-gate
770Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
780Sstevel@tonic-gate static const char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93";
79*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: res_comp.c,v 1.5 2005/07/28 06:51:50 marka Exp $";
800Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
810Sstevel@tonic-gate
820Sstevel@tonic-gate #include "port_before.h"
830Sstevel@tonic-gate #include <sys/types.h>
840Sstevel@tonic-gate #include <sys/param.h>
850Sstevel@tonic-gate #include <netinet/in.h>
860Sstevel@tonic-gate #include <arpa/nameser.h>
870Sstevel@tonic-gate #include <ctype.h>
880Sstevel@tonic-gate #include <resolv.h>
890Sstevel@tonic-gate #include <stdio.h>
900Sstevel@tonic-gate #include <string.h>
910Sstevel@tonic-gate #include <unistd.h>
920Sstevel@tonic-gate #include "port_after.h"
930Sstevel@tonic-gate
94*11038SRao.Shoaib@Sun.COM #ifndef ORIGINAL_ISC_CODE
950Sstevel@tonic-gate #pragma weak __dn_skipname = dn_skipname
960Sstevel@tonic-gate #pragma weak __res_dnok = res_dnok
970Sstevel@tonic-gate #pragma weak __res_hnok = res_hnok
980Sstevel@tonic-gate #pragma weak __res_mailok = res_mailok
990Sstevel@tonic-gate #pragma weak __res_ownok = res_ownok
1000Sstevel@tonic-gate #endif /* ORIGINAL_ISC_CODE */
101*11038SRao.Shoaib@Sun.COM
102*11038SRao.Shoaib@Sun.COM /*%
1030Sstevel@tonic-gate * Expand compressed domain name 'src' to full domain name.
104*11038SRao.Shoaib@Sun.COM *
105*11038SRao.Shoaib@Sun.COM * \li 'msg' is a pointer to the begining of the message,
106*11038SRao.Shoaib@Sun.COM * \li 'eom' points to the first location after the message,
107*11038SRao.Shoaib@Sun.COM * \li 'dst' is a pointer to a buffer of size 'dstsiz' for the result.
108*11038SRao.Shoaib@Sun.COM * \li Return size of compressed name or -1 if there was an error.
1090Sstevel@tonic-gate */
1100Sstevel@tonic-gate int
dn_expand(const u_char * msg,const u_char * eom,const u_char * src,char * dst,int dstsiz)1110Sstevel@tonic-gate dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
1120Sstevel@tonic-gate char *dst, int dstsiz)
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate if (n > 0 && dst[0] == '.')
1170Sstevel@tonic-gate dst[0] = '\0';
1180Sstevel@tonic-gate return (n);
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate
121*11038SRao.Shoaib@Sun.COM /*%
1220Sstevel@tonic-gate * Pack domain name 'exp_dn' in presentation form into 'comp_dn'.
123*11038SRao.Shoaib@Sun.COM *
124*11038SRao.Shoaib@Sun.COM * \li Return the size of the compressed name or -1.
125*11038SRao.Shoaib@Sun.COM * \li 'length' is the size of the array pointed to by 'comp_dn'.
1260Sstevel@tonic-gate */
1270Sstevel@tonic-gate int
dn_comp(const char * src,u_char * dst,int dstsiz,u_char ** dnptrs,u_char ** lastdnptr)1280Sstevel@tonic-gate dn_comp(const char *src, u_char *dst, int dstsiz,
1290Sstevel@tonic-gate u_char **dnptrs, u_char **lastdnptr)
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate return (ns_name_compress(src, dst, (size_t)dstsiz,
1320Sstevel@tonic-gate (const u_char **)dnptrs,
1330Sstevel@tonic-gate (const u_char **)lastdnptr));
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate
136*11038SRao.Shoaib@Sun.COM
137*11038SRao.Shoaib@Sun.COM /*%
1380Sstevel@tonic-gate * Skip over a compressed domain name. Return the size or -1.
1390Sstevel@tonic-gate */
1400Sstevel@tonic-gate int
dn_skipname(const u_char * ptr,const u_char * eom)1410Sstevel@tonic-gate dn_skipname(const u_char *ptr, const u_char *eom) {
1420Sstevel@tonic-gate const u_char *saveptr = ptr;
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate if (ns_name_skip(&ptr, eom) == -1)
1450Sstevel@tonic-gate return (-1);
1460Sstevel@tonic-gate return (ptr - saveptr);
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate
149*11038SRao.Shoaib@Sun.COM /*%
1500Sstevel@tonic-gate * Verify that a domain name uses an acceptable character set.
151*11038SRao.Shoaib@Sun.COM *
1520Sstevel@tonic-gate * Note the conspicuous absence of ctype macros in these definitions. On
1530Sstevel@tonic-gate * non-ASCII hosts, we can't depend on string literals or ctype macros to
1540Sstevel@tonic-gate * tell us anything about network-format data. The rest of the BIND system
1550Sstevel@tonic-gate * is not careful about this, but for some reason, we're doing it right here.
1560Sstevel@tonic-gate */
1570Sstevel@tonic-gate #define PERIOD 0x2e
1580Sstevel@tonic-gate #define hyphenchar(c) ((c) == 0x2d)
1590Sstevel@tonic-gate #define bslashchar(c) ((c) == 0x5c)
1600Sstevel@tonic-gate #ifdef SUNW_HNOK_UNDERSCORE
1610Sstevel@tonic-gate #define underscorechar(c) ((c) == 0x5f)
1620Sstevel@tonic-gate #endif /* SUNW_HNOK_UNDERSCORE */
1630Sstevel@tonic-gate #define periodchar(c) ((c) == PERIOD)
1640Sstevel@tonic-gate #define asterchar(c) ((c) == 0x2a)
1650Sstevel@tonic-gate #define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \
1660Sstevel@tonic-gate || ((c) >= 0x61 && (c) <= 0x7a))
1670Sstevel@tonic-gate #define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate #define borderchar(c) (alphachar(c) || digitchar(c))
1700Sstevel@tonic-gate #ifdef SUNW_HNOK_UNDERSCORE
1710Sstevel@tonic-gate #define middlechar(c) (borderchar(c) || hyphenchar(c) || underscorechar(c))
1720Sstevel@tonic-gate #else
1730Sstevel@tonic-gate #define middlechar(c) (borderchar(c) || hyphenchar(c))
1740Sstevel@tonic-gate #endif /* SUNW_HNOK_UNDERSCORE */
1750Sstevel@tonic-gate #define domainchar(c) ((c) > 0x20 && (c) < 0x7f)
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate int
res_hnok(const char * dn)1780Sstevel@tonic-gate res_hnok(const char *dn) {
179*11038SRao.Shoaib@Sun.COM int pch = PERIOD, ch = *dn++;
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate while (ch != '\0') {
1820Sstevel@tonic-gate int nch = *dn++;
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate if (periodchar(ch)) {
1850Sstevel@tonic-gate (void)NULL;
1860Sstevel@tonic-gate } else if (periodchar(pch)) {
1870Sstevel@tonic-gate if (!borderchar(ch))
1880Sstevel@tonic-gate return (0);
1890Sstevel@tonic-gate } else if (periodchar(nch) || nch == '\0') {
1900Sstevel@tonic-gate if (!borderchar(ch))
1910Sstevel@tonic-gate return (0);
1920Sstevel@tonic-gate } else {
1930Sstevel@tonic-gate if (!middlechar(ch))
1940Sstevel@tonic-gate return (0);
1950Sstevel@tonic-gate }
196*11038SRao.Shoaib@Sun.COM pch = ch, ch = nch;
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate return (1);
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate
201*11038SRao.Shoaib@Sun.COM /*%
2020Sstevel@tonic-gate * hostname-like (A, MX, WKS) owners can have "*" as their first label
2030Sstevel@tonic-gate * but must otherwise be as a host name.
2040Sstevel@tonic-gate */
2050Sstevel@tonic-gate int
res_ownok(const char * dn)2060Sstevel@tonic-gate res_ownok(const char *dn) {
2070Sstevel@tonic-gate if (asterchar(dn[0])) {
2080Sstevel@tonic-gate if (periodchar(dn[1]))
2090Sstevel@tonic-gate return (res_hnok(dn+2));
2100Sstevel@tonic-gate if (dn[1] == '\0')
2110Sstevel@tonic-gate return (1);
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate return (res_hnok(dn));
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate
216*11038SRao.Shoaib@Sun.COM /*%
2170Sstevel@tonic-gate * SOA RNAMEs and RP RNAMEs can have any printable character in their first
2180Sstevel@tonic-gate * label, but the rest of the name has to look like a host name.
2190Sstevel@tonic-gate */
2200Sstevel@tonic-gate int
res_mailok(const char * dn)2210Sstevel@tonic-gate res_mailok(const char *dn) {
2220Sstevel@tonic-gate int ch, escaped = 0;
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate /* "." is a valid missing representation */
2250Sstevel@tonic-gate if (*dn == '\0')
2260Sstevel@tonic-gate return (1);
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate /* otherwise <label>.<hostname> */
2290Sstevel@tonic-gate while ((ch = *dn++) != '\0') {
2300Sstevel@tonic-gate if (!domainchar(ch))
2310Sstevel@tonic-gate return (0);
2320Sstevel@tonic-gate if (!escaped && periodchar(ch))
2330Sstevel@tonic-gate break;
2340Sstevel@tonic-gate if (escaped)
2350Sstevel@tonic-gate escaped = 0;
2360Sstevel@tonic-gate else if (bslashchar(ch))
2370Sstevel@tonic-gate escaped = 1;
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate if (periodchar(ch))
2400Sstevel@tonic-gate return (res_hnok(dn));
2410Sstevel@tonic-gate return (0);
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate
244*11038SRao.Shoaib@Sun.COM /*%
245*11038SRao.Shoaib@Sun.COM * This function is quite liberal, since RFC1034's character sets are only
2460Sstevel@tonic-gate * recommendations.
2470Sstevel@tonic-gate */
2480Sstevel@tonic-gate int
res_dnok(const char * dn)2490Sstevel@tonic-gate res_dnok(const char *dn) {
2500Sstevel@tonic-gate int ch;
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate while ((ch = *dn++) != '\0')
2530Sstevel@tonic-gate if (!domainchar(ch))
2540Sstevel@tonic-gate return (0);
2550Sstevel@tonic-gate return (1);
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate
2580Sstevel@tonic-gate #ifdef BIND_4_COMPAT
259*11038SRao.Shoaib@Sun.COM /*%
2600Sstevel@tonic-gate * This module must export the following externally-visible symbols:
2610Sstevel@tonic-gate * ___putlong
2620Sstevel@tonic-gate * ___putshort
2630Sstevel@tonic-gate * __getlong
2640Sstevel@tonic-gate * __getshort
2650Sstevel@tonic-gate * Note that one _ comes from C and the others come from us.
2660Sstevel@tonic-gate */
267*11038SRao.Shoaib@Sun.COM
268*11038SRao.Shoaib@Sun.COM #ifdef SOLARIS2
269*11038SRao.Shoaib@Sun.COM #ifdef __putlong
270*11038SRao.Shoaib@Sun.COM #undef __putlong
2710Sstevel@tonic-gate #endif
272*11038SRao.Shoaib@Sun.COM #ifdef __putshort
273*11038SRao.Shoaib@Sun.COM #undef __putshort
2740Sstevel@tonic-gate #endif
275*11038SRao.Shoaib@Sun.COM #pragma weak putlong = __putlong
276*11038SRao.Shoaib@Sun.COM #pragma weak putshort = __putshort
277*11038SRao.Shoaib@Sun.COM #endif /* SOLARIS2 */
278*11038SRao.Shoaib@Sun.COM
__putlong(u_int32_t src,u_char * dst)2790Sstevel@tonic-gate void __putlong(u_int32_t src, u_char *dst) { ns_put32(src, dst); }
__putshort(u_int16_t src,u_char * dst)2800Sstevel@tonic-gate void __putshort(u_int16_t src, u_char *dst) { ns_put16(src, dst); }
2810Sstevel@tonic-gate #ifndef __ultrix__
_getlong(const u_char * src)2820Sstevel@tonic-gate u_int32_t _getlong(const u_char *src) { return (ns_get32(src)); }
_getshort(const u_char * src)2830Sstevel@tonic-gate u_int16_t _getshort(const u_char *src) { return (ns_get16(src)); }
2840Sstevel@tonic-gate #endif /*__ultrix__*/
2850Sstevel@tonic-gate #endif /*BIND_4_COMPAT*/
286*11038SRao.Shoaib@Sun.COM
287*11038SRao.Shoaib@Sun.COM /*! \file */
288