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