xref: /illumos-gate/usr/src/lib/libresolv2/common/resolv/res_comp.c (revision f22cbd2db87ae3945ed6a9166f8b9d61b65c6ab9)
1 /*
2  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 
7 /*
8  * Copyright (c) 1985, 1993
9  *    The Regents of the University of California.  All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  * 	This product includes software developed by the University of
22  * 	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  */
39 
40 /*
41  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
42  *
43  * Permission to use, copy, modify, and distribute this software for any
44  * purpose with or without fee is hereby granted, provided that the above
45  * copyright notice and this permission notice appear in all copies, and that
46  * the name of Digital Equipment Corporation not be used in advertising or
47  * publicity pertaining to distribution of the document or software without
48  * specific, written prior permission.
49  *
50  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
51  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
52  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
53  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
54  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
55  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
56  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
57  * SOFTWARE.
58  */
59 
60 /*
61  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
62  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
63  *
64  * Permission to use, copy, modify, and distribute this software for any
65  * purpose with or without fee is hereby granted, provided that the above
66  * copyright notice and this permission notice appear in all copies.
67  *
68  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
69  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
70  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
71  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
72  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
73  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
74  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
75  */
76 
77 #include "port_before.h"
78 #include <sys/types.h>
79 #include <sys/param.h>
80 #include <netinet/in.h>
81 #include <arpa/nameser.h>
82 #include <ctype.h>
83 #include <resolv.h>
84 #include <stdio.h>
85 #include <string.h>
86 #include <unistd.h>
87 #include "port_after.h"
88 
89 #ifndef	ORIGINAL_ISC_CODE
90 #pragma weak	__dn_skipname	=	dn_skipname
91 #pragma weak	__res_dnok	=	res_dnok
92 #pragma weak	__res_hnok	=	res_hnok
93 #pragma weak	__res_mailok	=	res_mailok
94 #pragma weak	__res_ownok	=	res_ownok
95 #endif	/* ORIGINAL_ISC_CODE */
96 
97 /*%
98  * Expand compressed domain name 'src' to full domain name.
99  *
100  * \li 'msg' is a pointer to the begining of the message,
101  * \li 'eom' points to the first location after the message,
102  * \li 'dst' is a pointer to a buffer of size 'dstsiz' for the result.
103  * \li Return size of compressed name or -1 if there was an error.
104  */
105 int
106 dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
107 	  char *dst, int dstsiz)
108 {
109 	int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
110 
111 	if (n > 0 && dst[0] == '.')
112 		dst[0] = '\0';
113 	return (n);
114 }
115 
116 /*%
117  * Pack domain name 'exp_dn' in presentation form into 'comp_dn'.
118  *
119  * \li Return the size of the compressed name or -1.
120  * \li 'length' is the size of the array pointed to by 'comp_dn'.
121  */
122 int
123 dn_comp(const char *src, u_char *dst, int dstsiz,
124 	u_char **dnptrs, u_char **lastdnptr)
125 {
126 	return (ns_name_compress(src, dst, (size_t)dstsiz,
127 				 (const u_char **)dnptrs,
128 				 (const u_char **)lastdnptr));
129 }
130 
131 
132 /*%
133  * Skip over a compressed domain name. Return the size or -1.
134  */
135 int
136 dn_skipname(const u_char *ptr, const u_char *eom) {
137 	const u_char *saveptr = ptr;
138 
139 	if (ns_name_skip(&ptr, eom) == -1)
140 		return (-1);
141 	return (ptr - saveptr);
142 }
143 
144 /*%
145  * Verify that a domain name uses an acceptable character set.
146  *
147  * Note the conspicuous absence of ctype macros in these definitions.  On
148  * non-ASCII hosts, we can't depend on string literals or ctype macros to
149  * tell us anything about network-format data.  The rest of the BIND system
150  * is not careful about this, but for some reason, we're doing it right here.
151  */
152 #define PERIOD 0x2e
153 #define	hyphenchar(c) ((c) == 0x2d)
154 #define bslashchar(c) ((c) == 0x5c)
155 #ifdef	SUNW_HNOK_UNDERSCORE
156 #define	underscorechar(c)	((c) == 0x5f)
157 #endif	/* SUNW_HNOK_UNDERSCORE */
158 #define periodchar(c) ((c) == PERIOD)
159 #define asterchar(c) ((c) == 0x2a)
160 #define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \
161 		   || ((c) >= 0x61 && (c) <= 0x7a))
162 #define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)
163 
164 #define borderchar(c) (alphachar(c) || digitchar(c))
165 #ifdef	SUNW_HNOK_UNDERSCORE
166 #define middlechar(c) (borderchar(c) || hyphenchar(c) || underscorechar(c))
167 #else
168 #define middlechar(c) (borderchar(c) || hyphenchar(c))
169 #endif	/* SUNW_HNOK_UNDERSCORE */
170 #define	domainchar(c) ((c) > 0x20 && (c) < 0x7f)
171 
172 int
173 res_hnok(const char *dn) {
174 	int pch = PERIOD, ch = *dn++;
175 
176 	while (ch != '\0') {
177 		int nch = *dn++;
178 
179 		if (periodchar(ch)) {
180 			(void)NULL;
181 		} else if (periodchar(pch)) {
182 			if (!borderchar(ch))
183 				return (0);
184 		} else if (periodchar(nch) || nch == '\0') {
185 			if (!borderchar(ch))
186 				return (0);
187 		} else {
188 			if (!middlechar(ch))
189 				return (0);
190 		}
191 		pch = ch, ch = nch;
192 	}
193 	return (1);
194 }
195 
196 /*%
197  * hostname-like (A, MX, WKS) owners can have "*" as their first label
198  * but must otherwise be as a host name.
199  */
200 int
201 res_ownok(const char *dn) {
202 	if (asterchar(dn[0])) {
203 		if (periodchar(dn[1]))
204 			return (res_hnok(dn+2));
205 		if (dn[1] == '\0')
206 			return (1);
207 	}
208 	return (res_hnok(dn));
209 }
210 
211 /*%
212  * SOA RNAMEs and RP RNAMEs can have any printable character in their first
213  * label, but the rest of the name has to look like a host name.
214  */
215 int
216 res_mailok(const char *dn) {
217 	int ch, escaped = 0;
218 
219 	/* "." is a valid missing representation */
220 	if (*dn == '\0')
221 		return (1);
222 
223 	/* otherwise <label>.<hostname> */
224 	while ((ch = *dn++) != '\0') {
225 		if (!domainchar(ch))
226 			return (0);
227 		if (!escaped && periodchar(ch))
228 			break;
229 		if (escaped)
230 			escaped = 0;
231 		else if (bslashchar(ch))
232 			escaped = 1;
233 	}
234 	if (periodchar(ch))
235 		return (res_hnok(dn));
236 	return (0);
237 }
238 
239 /*%
240  * This function is quite liberal, since RFC1034's character sets are only
241  * recommendations.
242  */
243 int
244 res_dnok(const char *dn) {
245 	int ch;
246 
247 	while ((ch = *dn++) != '\0')
248 		if (!domainchar(ch))
249 			return (0);
250 	return (1);
251 }
252 
253 #ifdef BIND_4_COMPAT
254 /*%
255  * This module must export the following externally-visible symbols:
256  *	___putlong
257  *	___putshort
258  *	__getlong
259  *	__getshort
260  * Note that one _ comes from C and the others come from us.
261  */
262 
263 #ifdef SOLARIS2
264 #ifdef  __putlong
265 #undef  __putlong
266 #endif
267 #ifdef  __putshort
268 #undef  __putshort
269 #endif
270 #pragma weak    putlong         =       __putlong
271 #pragma weak    putshort        =       __putshort
272 #endif /* SOLARIS2 */
273 
274 void __putlong(u_int32_t src, u_char *dst) { ns_put32(src, dst); }
275 void __putshort(u_int16_t src, u_char *dst) { ns_put16(src, dst); }
276 #ifndef __ultrix__
277 u_int32_t _getlong(const u_char *src) { return (ns_get32(src)); }
278 u_int16_t _getshort(const u_char *src) { return (ns_get16(src)); }
279 #endif /*__ultrix__*/
280 #endif /*BIND_4_COMPAT*/
281 
282 /*! \file */
283