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