res_comp.c revision 156956
1156952Sume/*
2156952Sume * Copyright (c) 1985, 1993
3156952Sume *    The Regents of the University of California.  All rights reserved.
4156952Sume *
5156952Sume * Redistribution and use in source and binary forms, with or without
6156952Sume * modification, are permitted provided that the following conditions
7156952Sume * are met:
8156952Sume * 1. Redistributions of source code must retain the above copyright
9156952Sume *    notice, this list of conditions and the following disclaimer.
10156952Sume * 2. Redistributions in binary form must reproduce the above copyright
11156952Sume *    notice, this list of conditions and the following disclaimer in the
12156952Sume *    documentation and/or other materials provided with the distribution.
13156952Sume * 3. All advertising materials mentioning features or use of this software
14156952Sume *    must display the following acknowledgement:
15156952Sume * 	This product includes software developed by the University of
16156952Sume * 	California, Berkeley and its contributors.
17156952Sume * 4. Neither the name of the University nor the names of its contributors
18156952Sume *    may be used to endorse or promote products derived from this software
19156952Sume *    without specific prior written permission.
20156952Sume *
21156952Sume * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22156952Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23156952Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24156952Sume * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25156952Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26156952Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27156952Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28156952Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29156952Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30156952Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31156952Sume * SUCH DAMAGE.
32156952Sume */
33156952Sume
34156952Sume/*
35156952Sume * Portions Copyright (c) 1993 by Digital Equipment Corporation.
36156952Sume *
37156952Sume * Permission to use, copy, modify, and distribute this software for any
38156952Sume * purpose with or without fee is hereby granted, provided that the above
39156952Sume * copyright notice and this permission notice appear in all copies, and that
40156952Sume * the name of Digital Equipment Corporation not be used in advertising or
41156952Sume * publicity pertaining to distribution of the document or software without
42156952Sume * specific, written prior permission.
43156952Sume *
44156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
45156952Sume * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
46156952Sume * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
47156952Sume * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
48156952Sume * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
49156952Sume * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
50156952Sume * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
51156952Sume * SOFTWARE.
52156952Sume */
53156952Sume
54156952Sume/*
55156952Sume * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
56156952Sume * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
57156952Sume *
58156952Sume * Permission to use, copy, modify, and distribute this software for any
59156952Sume * purpose with or without fee is hereby granted, provided that the above
60156952Sume * copyright notice and this permission notice appear in all copies.
61156952Sume *
62156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
63156952Sume * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
64156952Sume * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
65156952Sume * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
66156952Sume * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
67156952Sume * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
68156952Sume * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
69156952Sume */
70156952Sume
71156952Sume#if defined(LIBC_SCCS) && !defined(lint)
72156952Sumestatic const char sccsid[] = "@(#)res_comp.c	8.1 (Berkeley) 6/4/93";
73156952Sumestatic const char rcsid[] = "$Id: res_comp.c,v 1.1.2.1.4.2 2005/07/28 07:43:22 marka Exp $";
74156952Sume#endif /* LIBC_SCCS and not lint */
75156956Sume#include <sys/cdefs.h>
76156956Sume__FBSDID("$FreeBSD: head/lib/libc/resolv/res_comp.c 156956 2006-03-21 15:37:16Z ume $");
77156952Sume
78156952Sume#include "port_before.h"
79156952Sume#include <sys/types.h>
80156952Sume#include <sys/param.h>
81156952Sume#include <netinet/in.h>
82156952Sume#include <arpa/nameser.h>
83156952Sume#include <ctype.h>
84156952Sume#include <resolv.h>
85156952Sume#include <stdio.h>
86156952Sume#include <string.h>
87156952Sume#include <unistd.h>
88156952Sume#include "port_after.h"
89156952Sume
90156952Sume/*
91156952Sume * Expand compressed domain name 'src' to full domain name.
92156952Sume * 'msg' is a pointer to the begining of the message,
93156952Sume * 'eom' points to the first location after the message,
94156952Sume * 'dst' is a pointer to a buffer of size 'dstsiz' for the result.
95156952Sume * Return size of compressed name or -1 if there was an error.
96156952Sume */
97156952Sumeint
98156952Sumedn_expand(const u_char *msg, const u_char *eom, const u_char *src,
99156952Sume	  char *dst, int dstsiz)
100156952Sume{
101156952Sume	int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
102156952Sume
103156952Sume	if (n > 0 && dst[0] == '.')
104156952Sume		dst[0] = '\0';
105156952Sume	return (n);
106156952Sume}
107156952Sume
108156952Sume/*
109156952Sume * Pack domain name 'exp_dn' in presentation form into 'comp_dn'.
110156952Sume * Return the size of the compressed name or -1.
111156952Sume * 'length' is the size of the array pointed to by 'comp_dn'.
112156952Sume */
113156952Sumeint
114156952Sumedn_comp(const char *src, u_char *dst, int dstsiz,
115156952Sume	u_char **dnptrs, u_char **lastdnptr)
116156952Sume{
117156952Sume	return (ns_name_compress(src, dst, (size_t)dstsiz,
118156952Sume				 (const u_char **)dnptrs,
119156952Sume				 (const u_char **)lastdnptr));
120156952Sume}
121156952Sume
122156952Sume/*
123156952Sume * Skip over a compressed domain name. Return the size or -1.
124156952Sume */
125156952Sumeint
126156952Sumedn_skipname(const u_char *ptr, const u_char *eom) {
127156952Sume	const u_char *saveptr = ptr;
128156952Sume
129156952Sume	if (ns_name_skip(&ptr, eom) == -1)
130156952Sume		return (-1);
131156952Sume	return (ptr - saveptr);
132156952Sume}
133156952Sume
134156952Sume/*
135156952Sume * Verify that a domain name uses an acceptable character set.
136156952Sume */
137156952Sume
138156952Sume/*
139156952Sume * Note the conspicuous absence of ctype macros in these definitions.  On
140156952Sume * non-ASCII hosts, we can't depend on string literals or ctype macros to
141156952Sume * tell us anything about network-format data.  The rest of the BIND system
142156952Sume * is not careful about this, but for some reason, we're doing it right here.
143156952Sume */
144156952Sume#define PERIOD 0x2e
145156952Sume#define	hyphenchar(c) ((c) == 0x2d)
146156952Sume#define bslashchar(c) ((c) == 0x5c)
147156952Sume#define periodchar(c) ((c) == PERIOD)
148156952Sume#define asterchar(c) ((c) == 0x2a)
149156952Sume#define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \
150156952Sume		   || ((c) >= 0x61 && (c) <= 0x7a))
151156952Sume#define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)
152156952Sume
153156952Sume#define borderchar(c) (alphachar(c) || digitchar(c))
154156952Sume#define middlechar(c) (borderchar(c) || hyphenchar(c))
155156952Sume#define	domainchar(c) ((c) > 0x20 && (c) < 0x7f)
156156952Sume
157156952Sumeint
158156952Sumeres_hnok(const char *dn) {
159156952Sume	int pch = PERIOD, ch = *dn++;
160156952Sume
161156952Sume	while (ch != '\0') {
162156952Sume		int nch = *dn++;
163156952Sume
164156952Sume		if (periodchar(ch)) {
165156952Sume			(void)NULL;
166156952Sume		} else if (periodchar(pch)) {
167156952Sume			if (!borderchar(ch))
168156952Sume				return (0);
169156952Sume		} else if (periodchar(nch) || nch == '\0') {
170156952Sume			if (!borderchar(ch))
171156952Sume				return (0);
172156952Sume		} else {
173156952Sume			if (!middlechar(ch))
174156952Sume				return (0);
175156952Sume		}
176156952Sume		pch = ch, ch = nch;
177156952Sume	}
178156952Sume	return (1);
179156952Sume}
180156952Sume
181156952Sume/*
182156952Sume * hostname-like (A, MX, WKS) owners can have "*" as their first label
183156952Sume * but must otherwise be as a host name.
184156952Sume */
185156952Sumeint
186156952Sumeres_ownok(const char *dn) {
187156952Sume	if (asterchar(dn[0])) {
188156952Sume		if (periodchar(dn[1]))
189156952Sume			return (res_hnok(dn+2));
190156952Sume		if (dn[1] == '\0')
191156952Sume			return (1);
192156952Sume	}
193156952Sume	return (res_hnok(dn));
194156952Sume}
195156952Sume
196156952Sume/*
197156952Sume * SOA RNAMEs and RP RNAMEs can have any printable character in their first
198156952Sume * label, but the rest of the name has to look like a host name.
199156952Sume */
200156952Sumeint
201156952Sumeres_mailok(const char *dn) {
202156952Sume	int ch, escaped = 0;
203156952Sume
204156952Sume	/* "." is a valid missing representation */
205156952Sume	if (*dn == '\0')
206156952Sume		return (1);
207156952Sume
208156952Sume	/* otherwise <label>.<hostname> */
209156952Sume	while ((ch = *dn++) != '\0') {
210156952Sume		if (!domainchar(ch))
211156952Sume			return (0);
212156952Sume		if (!escaped && periodchar(ch))
213156952Sume			break;
214156952Sume		if (escaped)
215156952Sume			escaped = 0;
216156952Sume		else if (bslashchar(ch))
217156952Sume			escaped = 1;
218156952Sume	}
219156952Sume	if (periodchar(ch))
220156952Sume		return (res_hnok(dn));
221156952Sume	return (0);
222156952Sume}
223156952Sume
224156952Sume/*
225156952Sume * This function is quite liberal, since RFC 1034's character sets are only
226156952Sume * recommendations.
227156952Sume */
228156952Sumeint
229156952Sumeres_dnok(const char *dn) {
230156952Sume	int ch;
231156952Sume
232156952Sume	while ((ch = *dn++) != '\0')
233156952Sume		if (!domainchar(ch))
234156952Sume			return (0);
235156952Sume	return (1);
236156952Sume}
237156952Sume
238156952Sume#ifdef BIND_4_COMPAT
239156952Sume/*
240156952Sume * This module must export the following externally-visible symbols:
241156952Sume *	___putlong
242156952Sume *	___putshort
243156952Sume *	__getlong
244156952Sume *	__getshort
245156952Sume * Note that one _ comes from C and the others come from us.
246156952Sume */
247156952Sume
248156952Sume#ifdef SOLARIS2
249156952Sume#ifdef  __putlong
250156952Sume#undef  __putlong
251156952Sume#endif
252156952Sume#ifdef  __putshort
253156952Sume#undef  __putshort
254156952Sume#endif
255156952Sume#pragma weak    putlong         =       __putlong
256156952Sume#pragma weak    putshort        =       __putshort
257156952Sume#endif /* SOLARIS2 */
258156952Sume
259156952Sumevoid __putlong(u_int32_t src, u_char *dst) { ns_put32(src, dst); }
260156952Sumevoid __putshort(u_int16_t src, u_char *dst) { ns_put16(src, dst); }
261156952Sume#ifndef __ultrix__
262156952Sumeu_int32_t _getlong(const u_char *src) { return (ns_get32(src)); }
263156952Sumeu_int16_t _getshort(const u_char *src) { return (ns_get16(src)); }
264156952Sume#endif /*__ultrix__*/
265156952Sume#endif /*BIND_4_COMPAT*/
266156956Sume
267156956Sume/*
268156956Sume * Weak aliases for applications that use certain private entry points,
269156956Sume * and fail to include <resolv.h>.
270156956Sume */
271156956Sume#undef dn_comp
272156956Sume__weak_reference(__dn_comp, dn_comp);
273156956Sume#undef dn_expand
274156956Sume__weak_reference(__dn_expand, dn_expand);
275