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