res_comp.c revision 298830
1178786Skmacy/*
2178786Skmacy * Copyright (c) 1985, 1993
3178786Skmacy *    The Regents of the University of California.  All rights reserved.
4178786Skmacy *
5178786Skmacy * Redistribution and use in source and binary forms, with or without
6178786Skmacy * modification, are permitted provided that the following conditions
7178786Skmacy * are met:
8178786Skmacy * 1. Redistributions of source code must retain the above copyright
9178786Skmacy *    notice, this list of conditions and the following disclaimer.
10178786Skmacy * 2. Redistributions in binary form must reproduce the above copyright
11178786Skmacy *    notice, this list of conditions and the following disclaimer in the
12178786Skmacy *    documentation and/or other materials provided with the distribution.
13178786Skmacy * 4. Neither the name of the University nor the names of its contributors
14178786Skmacy *    may be used to endorse or promote products derived from this software
15178786Skmacy *    without specific prior written permission.
16178786Skmacy *
17178786Skmacy * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18178786Skmacy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19178786Skmacy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20178786Skmacy * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21178786Skmacy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22178786Skmacy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23178786Skmacy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24178786Skmacy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25178786Skmacy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26178786Skmacy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27178786Skmacy * SUCH DAMAGE.
28178786Skmacy */
29178786Skmacy
30178786Skmacy/*
31178786Skmacy * Portions Copyright (c) 1993 by Digital Equipment Corporation.
32237263Snp *
33237263Snp * Permission to use, copy, modify, and distribute this software for any
34237263Snp * purpose with or without fee is hereby granted, provided that the above
35178786Skmacy * copyright notice and this permission notice appear in all copies, and that
36178786Skmacy * the name of Digital Equipment Corporation not be used in advertising or
37178786Skmacy * publicity pertaining to distribution of the document or software without
38178786Skmacy * specific, written prior permission.
39178786Skmacy *
40178786Skmacy * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
41178786Skmacy * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
42178786Skmacy * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
43178786Skmacy * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
44178786Skmacy * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
45178786Skmacy * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
46178786Skmacy * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
47178786Skmacy * SOFTWARE.
48178786Skmacy */
49178786Skmacy
50178786Skmacy/*
51178786Skmacy * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
52178786Skmacy * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
53178786Skmacy *
54178786Skmacy * Permission to use, copy, modify, and distribute this software for any
55178786Skmacy * purpose with or without fee is hereby granted, provided that the above
56178786Skmacy * copyright notice and this permission notice appear in all copies.
57178786Skmacy *
58178786Skmacy * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
59178786Skmacy * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
60178786Skmacy * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
61178786Skmacy * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
62178786Skmacy * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
63178786Skmacy * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
64178786Skmacy * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
65178786Skmacy */
66178786Skmacy
67237263Snp#if defined(LIBC_SCCS) && !defined(lint)
68237263Snpstatic const char sccsid[] = "@(#)res_comp.c	8.1 (Berkeley) 6/4/93";
69237263Snpstatic const char rcsid[] = "$Id: res_comp.c,v 1.5 2005/07/28 06:51:50 marka Exp $";
70237263Snp#endif /* LIBC_SCCS and not lint */
71237263Snp#include <sys/cdefs.h>
72178786Skmacy__FBSDID("$FreeBSD: head/lib/libc/resolv/res_comp.c 298830 2016-04-30 01:24:24Z pfg $");
73237263Snp
74178786Skmacy#include "port_before.h"
75178786Skmacy#include <sys/param.h>
76178786Skmacy#include <netinet/in.h>
77178786Skmacy#include <arpa/nameser.h>
78178786Skmacy#include <ctype.h>
79178786Skmacy#include <resolv.h>
80178786Skmacy#include <stdio.h>
81178786Skmacy#include <string.h>
82178786Skmacy#include <unistd.h>
83178786Skmacy#include "port_after.h"
84178786Skmacy
85178786Skmacy/*%
86178786Skmacy * Expand compressed domain name 'src' to full domain name.
87178786Skmacy *
88178786Skmacy * \li 'msg' is a pointer to the beginning of the message,
89178786Skmacy * \li 'eom' points to the first location after the message,
90178786Skmacy * \li 'dst' is a pointer to a buffer of size 'dstsiz' for the result.
91178786Skmacy * \li Return size of compressed name or -1 if there was an error.
92178786Skmacy */
93178786Skmacyint
94178786Skmacydn_expand(const u_char *msg, const u_char *eom, const u_char *src,
95178786Skmacy	  char *dst, int dstsiz)
96178786Skmacy{
97178786Skmacy	int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
98178786Skmacy
99178786Skmacy	if (n > 0 && dst[0] == '.')
100178786Skmacy		dst[0] = '\0';
101178786Skmacy	return (n);
102178786Skmacy}
103178786Skmacy
104178786Skmacy/*%
105178786Skmacy * Pack domain name 'exp_dn' in presentation form into 'comp_dn'.
106178786Skmacy *
107178786Skmacy * \li Return the size of the compressed name or -1.
108178786Skmacy * \li 'length' is the size of the array pointed to by 'comp_dn'.
109178786Skmacy */
110178786Skmacyint
111178786Skmacydn_comp(const char *src, u_char *dst, int dstsiz,
112178786Skmacy	u_char **dnptrs, u_char **lastdnptr)
113178786Skmacy{
114178786Skmacy	return (ns_name_compress(src, dst, (size_t)dstsiz,
115178786Skmacy				 (const u_char **)dnptrs,
116178786Skmacy				 (const u_char **)lastdnptr));
117178786Skmacy}
118178786Skmacy
119178786Skmacy/*%
120178786Skmacy * Skip over a compressed domain name. Return the size or -1.
121178786Skmacy */
122178786Skmacyint
123178786Skmacydn_skipname(const u_char *ptr, const u_char *eom) {
124178786Skmacy	const u_char *saveptr = ptr;
125178786Skmacy
126178786Skmacy	if (ns_name_skip(&ptr, eom) == -1)
127178786Skmacy		return (-1);
128178786Skmacy	return (ptr - saveptr);
129178786Skmacy}
130178786Skmacy
131178786Skmacy/*%
132178786Skmacy * Verify that a domain name uses an acceptable character set.
133178786Skmacy *
134178786Skmacy * Note the conspicuous absence of ctype macros in these definitions.  On
135178786Skmacy * non-ASCII hosts, we can't depend on string literals or ctype macros to
136178786Skmacy * tell us anything about network-format data.  The rest of the BIND system
137178786Skmacy * is not careful about this, but for some reason, we're doing it right here.
138178786Skmacy */
139178786Skmacy#define PERIOD 0x2e
140178786Skmacy#define	hyphenchar(c) ((c) == 0x2d)
141178786Skmacy#define bslashchar(c) ((c) == 0x5c)
142178786Skmacy#define underscorechar(c) ((c) == 0x5f)
143178786Skmacy#define periodchar(c) ((c) == PERIOD)
144178786Skmacy#define asterchar(c) ((c) == 0x2a)
145178786Skmacy#define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \
146178786Skmacy		   || ((c) >= 0x61 && (c) <= 0x7a))
147178786Skmacy#define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)
148178786Skmacy
149178786Skmacy#ifdef	RES_ENFORCE_RFC1034
150178786Skmacy#define borderchar(c) (alphachar(c) || digitchar(c))
151178786Skmacy#else
152178786Skmacy#define borderchar(c) (alphachar(c) || digitchar(c) || underscorechar(c))
153178786Skmacy#endif
154178786Skmacy#define middlechar(c) (borderchar(c) || hyphenchar(c))
155178786Skmacy#define	domainchar(c) ((c) > 0x20 && (c) < 0x7f)
156178786Skmacy
157178786Skmacyint
158178786Skmacyres_hnok(const char *dn) {
159178786Skmacy	int pch = PERIOD, ch = *dn++;
160178786Skmacy
161178786Skmacy	while (ch != '\0') {
162178786Skmacy		int nch = *dn++;
163178786Skmacy
164178786Skmacy		if (periodchar(ch)) {
165178786Skmacy			(void)NULL;
166178786Skmacy		} else if (periodchar(pch)) {
167178786Skmacy			if (!borderchar(ch))
168178786Skmacy				return (0);
169178786Skmacy		} else if (periodchar(nch) || nch == '\0') {
170178786Skmacy			if (!borderchar(ch))
171178786Skmacy				return (0);
172178786Skmacy		} else {
173178786Skmacy			if (!middlechar(ch))
174178786Skmacy				return (0);
175178786Skmacy		}
176178786Skmacy		pch = ch, ch = nch;
177178786Skmacy	}
178178786Skmacy	return (1);
179278886Shselasky}
180178786Skmacy
181178786Skmacy/*%
182178786Skmacy * hostname-like (A, MX, WKS) owners can have "*" as their first label
183178786Skmacy * but must otherwise be as a host name.
184178786Skmacy */
185178786Skmacyint
186178786Skmacyres_ownok(const char *dn) {
187178786Skmacy	if (asterchar(dn[0])) {
188237263Snp		if (periodchar(dn[1]))
189237263Snp			return (res_hnok(dn+2));
190278886Shselasky		if (dn[1] == '\0')
191178786Skmacy			return (1);
192178786Skmacy	}
193178786Skmacy	return (res_hnok(dn));
194178786Skmacy}
195178786Skmacy
196178786Skmacy/*%
197178786Skmacy * SOA RNAMEs and RP RNAMEs can have any printable character in their first
198178786Skmacy * label, but the rest of the name has to look like a host name.
199178786Skmacy */
200178786Skmacyint
201178786Skmacyres_mailok(const char *dn) {
202178786Skmacy	int ch, escaped = 0;
203178786Skmacy
204178786Skmacy	/* "." is a valid missing representation */
205178786Skmacy	if (*dn == '\0')
206178786Skmacy		return (1);
207178786Skmacy
208178786Skmacy	/* otherwise <label>.<hostname> */
209178786Skmacy	while ((ch = *dn++) != '\0') {
210178786Skmacy		if (!domainchar(ch))
211178786Skmacy			return (0);
212178786Skmacy		if (!escaped && periodchar(ch))
213178786Skmacy			break;
214178786Skmacy		if (escaped)
215178786Skmacy			escaped = 0;
216178786Skmacy		else if (bslashchar(ch))
217178786Skmacy			escaped = 1;
218178786Skmacy	}
219178786Skmacy	if (periodchar(ch))
220178786Skmacy		return (res_hnok(dn));
221178786Skmacy	return (0);
222178786Skmacy}
223178786Skmacy
224178786Skmacy/*%
225237263Snp * This function is quite liberal, since RFC1034's character sets are only
226178786Skmacy * recommendations.
227178786Skmacy */
228178786Skmacyint
229178786Skmacyres_dnok(const char *dn) {
230178786Skmacy	int ch;
231178786Skmacy
232178786Skmacy	while ((ch = *dn++) != '\0')
233237263Snp		if (!domainchar(ch))
234237263Snp			return (0);
235237263Snp	return (1);
236237263Snp}
237237263Snp
238178786Skmacy#ifdef BIND_4_COMPAT
239178786Skmacy/*%
240178786Skmacy * This module must export the following externally-visible symbols:
241178786Skmacy *	___putlong
242178786Skmacy *	___putshort
243178786Skmacy *	__getlong
244178786Skmacy *	__getshort
245178786Skmacy * Note that one _ comes from C and the others come from us.
246178786Skmacy */
247178786Skmacy
248178786Skmacy#ifdef SOLARIS2
249178786Skmacy#ifdef  __putlong
250178786Skmacy#undef  __putlong
251178786Skmacy#endif
252178786Skmacy#ifdef  __putshort
253237263Snp#undef  __putshort
254237263Snp#endif
255237263Snp#pragma weak    putlong         =       __putlong
256237263Snp#pragma weak    putshort        =       __putshort
257237263Snp#endif /* SOLARIS2 */
258237263Snp
259237263Snpvoid __putlong(u_int32_t src, u_char *dst) { ns_put32(src, dst); }
260237263Snpvoid __putshort(u_int16_t src, u_char *dst) { ns_put16(src, dst); }
261237263Snp#ifndef __ultrix__
262237263Snpu_int32_t _getlong(const u_char *src) { return (ns_get32(src)); }
263237263Snpu_int16_t _getshort(const u_char *src) { return (ns_get16(src)); }
264237263Snp#endif /*__ultrix__*/
265237263Snp#endif /*BIND_4_COMPAT*/
266237263Snp
267237263Snp/*
268237263Snp * Weak aliases for applications that use certain private entry points,
269237263Snp * and fail to include <resolv.h>.
270178786Skmacy */
271178786Skmacy#undef dn_comp
272178786Skmacy__weak_reference(__dn_comp, dn_comp);
273178786Skmacy#undef dn_expand
274178786Skmacy__weak_reference(__dn_expand, dn_expand);
275178786Skmacy
276178786Skmacy/*! \file */
277178786Skmacy