Home | History | Annotate | Line # | Download | only in nameser
ns_name.c revision 1.6.14.1
      1  1.6.14.1       riz /*	$NetBSD: ns_name.c,v 1.6.14.1 2011/01/09 20:43:28 riz Exp $	*/
      2       1.1  christos 
      3       1.1  christos /*
      4       1.1  christos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
      5       1.1  christos  * Copyright (c) 1996,1999 by Internet Software Consortium.
      6       1.1  christos  *
      7       1.1  christos  * Permission to use, copy, modify, and distribute this software for any
      8       1.1  christos  * purpose with or without fee is hereby granted, provided that the above
      9       1.1  christos  * copyright notice and this permission notice appear in all copies.
     10       1.1  christos  *
     11       1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     12       1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13       1.1  christos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     14       1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15       1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16       1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     17       1.1  christos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18       1.1  christos  */
     19       1.1  christos 
     20       1.2  christos #include <sys/cdefs.h>
     21       1.1  christos #ifndef lint
     22       1.2  christos #ifdef notdef
     23  1.6.14.1       riz static const char rcsid[] = "Id: ns_name.c,v 1.11 2009/01/23 19:59:16 each Exp";
     24       1.2  christos #else
     25  1.6.14.1       riz __RCSID("$NetBSD: ns_name.c,v 1.6.14.1 2011/01/09 20:43:28 riz Exp $");
     26       1.2  christos #endif
     27       1.1  christos #endif
     28       1.1  christos 
     29       1.1  christos #include "port_before.h"
     30       1.1  christos 
     31       1.1  christos #include <sys/types.h>
     32       1.1  christos 
     33       1.1  christos #include <netinet/in.h>
     34       1.1  christos #include <arpa/nameser.h>
     35       1.1  christos 
     36       1.1  christos #include <errno.h>
     37       1.1  christos #include <resolv.h>
     38       1.1  christos #include <string.h>
     39       1.1  christos #include <ctype.h>
     40       1.1  christos #include <stdlib.h>
     41       1.1  christos #include <limits.h>
     42       1.1  christos 
     43       1.1  christos #include "port_after.h"
     44       1.1  christos 
     45       1.1  christos #ifdef SPRINTF_CHAR
     46       1.1  christos # define SPRINTF(x) strlen(sprintf/**/x)
     47       1.1  christos #else
     48       1.1  christos # define SPRINTF(x) ((size_t)sprintf x)
     49       1.1  christos #endif
     50       1.1  christos 
     51       1.4  christos #define NS_TYPE_ELT			0x40 /*%< EDNS0 extended label type */
     52       1.1  christos #define DNS_LABELTYPE_BITSTRING		0x41
     53       1.1  christos 
     54       1.1  christos /* Data. */
     55       1.1  christos 
     56       1.1  christos static const char	digits[] = "0123456789";
     57       1.1  christos 
     58       1.1  christos static const char digitvalue[256] = {
     59       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,	/*16*/
     60       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*32*/
     61       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*48*/
     62       1.1  christos 	 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1, /*64*/
     63       1.1  christos 	-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*80*/
     64       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*96*/
     65       1.1  christos 	-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*112*/
     66       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*128*/
     67       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     68       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     69       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     70       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     71       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     72       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     73       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     74       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*256*/
     75       1.1  christos };
     76       1.1  christos 
     77       1.1  christos /* Forward. */
     78       1.1  christos 
     79       1.1  christos static int		special(int);
     80       1.1  christos static int		printable(int);
     81       1.1  christos static int		dn_find(const u_char *, const u_char *,
     82       1.1  christos 				const u_char * const *,
     83       1.1  christos 				const u_char * const *);
     84       1.1  christos static int		encode_bitsring(const char **, const char *,
     85       1.3  christos 					unsigned char **, unsigned char **,
     86       1.3  christos 					unsigned const char *);
     87       1.1  christos static int		labellen(const u_char *);
     88       1.3  christos static int		decode_bitstring(const unsigned char **,
     89       1.3  christos 					 char *, const char *);
     90       1.1  christos 
     91       1.1  christos /* Public. */
     92       1.1  christos 
     93       1.4  christos /*%
     94       1.1  christos  *	Convert an encoded domain name to printable ascii as per RFC1035.
     95       1.4  christos 
     96       1.1  christos  * return:
     97       1.4  christos  *\li	Number of bytes written to buffer, or -1 (with errno set)
     98       1.4  christos  *
     99       1.1  christos  * notes:
    100       1.4  christos  *\li	The root is returned as "."
    101       1.4  christos  *\li	All other domains are returned in non absolute form
    102       1.1  christos  */
    103       1.1  christos int
    104       1.1  christos ns_name_ntop(const u_char *src, char *dst, size_t dstsiz)
    105       1.1  christos {
    106       1.1  christos 	const u_char *cp;
    107       1.1  christos 	char *dn, *eom;
    108       1.1  christos 	u_char c;
    109       1.1  christos 	u_int n;
    110       1.1  christos 	int l;
    111       1.1  christos 
    112       1.1  christos 	cp = src;
    113       1.1  christos 	dn = dst;
    114       1.1  christos 	eom = dst + dstsiz;
    115       1.1  christos 
    116       1.1  christos 	while ((n = *cp++) != 0) {
    117       1.1  christos 		if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
    118       1.1  christos 			/* Some kind of compression pointer. */
    119       1.1  christos 			errno = EMSGSIZE;
    120       1.1  christos 			return (-1);
    121       1.1  christos 		}
    122       1.1  christos 		if (dn != dst) {
    123       1.1  christos 			if (dn >= eom) {
    124       1.1  christos 				errno = EMSGSIZE;
    125       1.1  christos 				return (-1);
    126       1.1  christos 			}
    127       1.1  christos 			*dn++ = '.';
    128       1.1  christos 		}
    129       1.1  christos 		if ((l = labellen(cp - 1)) < 0) {
    130       1.4  christos 			errno = EMSGSIZE; /*%< XXX */
    131  1.6.14.1       riz 			return (-1);
    132       1.1  christos 		}
    133       1.1  christos 		if (dn + l >= eom) {
    134       1.1  christos 			errno = EMSGSIZE;
    135       1.1  christos 			return (-1);
    136       1.1  christos 		}
    137       1.1  christos 		if ((n & NS_CMPRSFLGS) == NS_TYPE_ELT) {
    138       1.1  christos 			int m;
    139       1.1  christos 
    140       1.1  christos 			if (n != DNS_LABELTYPE_BITSTRING) {
    141       1.1  christos 				/* XXX: labellen should reject this case */
    142       1.1  christos 				errno = EINVAL;
    143  1.6.14.1       riz 				return (-1);
    144       1.1  christos 			}
    145       1.3  christos 			if ((m = decode_bitstring(&cp, dn, eom)) < 0)
    146       1.1  christos 			{
    147       1.1  christos 				errno = EMSGSIZE;
    148  1.6.14.1       riz 				return (-1);
    149       1.1  christos 			}
    150       1.1  christos 			dn += m;
    151       1.1  christos 			continue;
    152       1.1  christos 		}
    153       1.2  christos 		for (; l > 0; l--) {
    154       1.1  christos 			c = *cp++;
    155       1.1  christos 			if (special(c)) {
    156       1.1  christos 				if (dn + 1 >= eom) {
    157       1.1  christos 					errno = EMSGSIZE;
    158       1.1  christos 					return (-1);
    159       1.1  christos 				}
    160       1.1  christos 				*dn++ = '\\';
    161       1.1  christos 				*dn++ = (char)c;
    162       1.1  christos 			} else if (!printable(c)) {
    163       1.1  christos 				if (dn + 3 >= eom) {
    164       1.1  christos 					errno = EMSGSIZE;
    165       1.1  christos 					return (-1);
    166       1.1  christos 				}
    167       1.1  christos 				*dn++ = '\\';
    168       1.1  christos 				*dn++ = digits[c / 100];
    169       1.1  christos 				*dn++ = digits[(c % 100) / 10];
    170       1.1  christos 				*dn++ = digits[c % 10];
    171       1.1  christos 			} else {
    172       1.1  christos 				if (dn >= eom) {
    173       1.1  christos 					errno = EMSGSIZE;
    174       1.1  christos 					return (-1);
    175       1.1  christos 				}
    176       1.1  christos 				*dn++ = (char)c;
    177       1.1  christos 			}
    178       1.1  christos 		}
    179       1.1  christos 	}
    180       1.1  christos 	if (dn == dst) {
    181       1.1  christos 		if (dn >= eom) {
    182       1.1  christos 			errno = EMSGSIZE;
    183       1.1  christos 			return (-1);
    184       1.1  christos 		}
    185       1.1  christos 		*dn++ = '.';
    186       1.1  christos 	}
    187       1.1  christos 	if (dn >= eom) {
    188       1.1  christos 		errno = EMSGSIZE;
    189       1.1  christos 		return (-1);
    190       1.1  christos 	}
    191       1.1  christos 	*dn++ = '\0';
    192       1.1  christos 	return (dn - dst);
    193       1.1  christos }
    194       1.1  christos 
    195       1.4  christos /*%
    196       1.1  christos  *	Convert a ascii string into an encoded domain name as per RFC1035.
    197       1.4  christos  *
    198       1.1  christos  * return:
    199       1.4  christos  *
    200       1.4  christos  *\li	-1 if it fails
    201       1.4  christos  *\li	1 if string was fully qualified
    202       1.4  christos  *\li	0 is string was not fully qualified
    203       1.4  christos  *
    204       1.1  christos  * notes:
    205       1.4  christos  *\li	Enforces label and domain length limits.
    206       1.1  christos  */
    207  1.6.14.1       riz int
    208  1.6.14.1       riz ns_name_pton(const char *src, u_char *dst, size_t dstsiz) {
    209  1.6.14.1       riz 	return (ns_name_pton2(src, dst, dstsiz, NULL));
    210  1.6.14.1       riz }
    211       1.1  christos 
    212  1.6.14.1       riz /*
    213  1.6.14.1       riz  * ns_name_pton2(src, dst, dstsiz, *dstlen)
    214  1.6.14.1       riz  *	Convert a ascii string into an encoded domain name as per RFC1035.
    215  1.6.14.1       riz  * return:
    216  1.6.14.1       riz  *	-1 if it fails
    217  1.6.14.1       riz  *	1 if string was fully qualified
    218  1.6.14.1       riz  *	0 is string was not fully qualified
    219  1.6.14.1       riz  * side effects:
    220  1.6.14.1       riz  *	fills in *dstlen (if non-NULL)
    221  1.6.14.1       riz  * notes:
    222  1.6.14.1       riz  *	Enforces label and domain length limits.
    223  1.6.14.1       riz  */
    224       1.1  christos int
    225  1.6.14.1       riz ns_name_pton2(const char *src, u_char *dst, size_t dstsiz, size_t *dstlen) {
    226       1.1  christos 	u_char *label, *bp, *eom;
    227       1.1  christos 	int c, n, escaped, e = 0;
    228       1.1  christos 	char *cp;
    229       1.1  christos 
    230       1.1  christos 	escaped = 0;
    231       1.1  christos 	bp = dst;
    232       1.1  christos 	eom = dst + dstsiz;
    233       1.1  christos 	label = bp++;
    234       1.1  christos 
    235       1.1  christos 	while ((c = *src++) != 0) {
    236       1.1  christos 		if (escaped) {
    237       1.4  christos 			if (c == '[') { /*%< start a bit string label */
    238       1.1  christos 				if ((cp = strchr(src, ']')) == NULL) {
    239       1.4  christos 					errno = EINVAL; /*%< ??? */
    240  1.6.14.1       riz 					return (-1);
    241       1.1  christos 				}
    242       1.3  christos 				if ((e = encode_bitsring(&src, cp + 2,
    243       1.3  christos 							 &label, &bp, eom))
    244       1.1  christos 				    != 0) {
    245       1.1  christos 					errno = e;
    246  1.6.14.1       riz 					return (-1);
    247       1.1  christos 				}
    248       1.1  christos 				escaped = 0;
    249       1.1  christos 				label = bp++;
    250       1.1  christos 				if ((c = *src++) == 0)
    251       1.1  christos 					goto done;
    252       1.1  christos 				else if (c != '.') {
    253       1.1  christos 					errno = EINVAL;
    254  1.6.14.1       riz 					return	(-1);
    255       1.1  christos 				}
    256       1.1  christos 				continue;
    257       1.1  christos 			}
    258       1.1  christos 			else if ((cp = strchr(digits, c)) != NULL) {
    259       1.1  christos 				n = (cp - digits) * 100;
    260       1.1  christos 				if ((c = *src++) == 0 ||
    261       1.1  christos 				    (cp = strchr(digits, c)) == NULL) {
    262       1.1  christos 					errno = EMSGSIZE;
    263       1.1  christos 					return (-1);
    264       1.1  christos 				}
    265       1.1  christos 				n += (cp - digits) * 10;
    266       1.1  christos 				if ((c = *src++) == 0 ||
    267       1.1  christos 				    (cp = strchr(digits, c)) == NULL) {
    268       1.1  christos 					errno = EMSGSIZE;
    269       1.1  christos 					return (-1);
    270       1.1  christos 				}
    271       1.1  christos 				n += (cp - digits);
    272       1.1  christos 				if (n > 255) {
    273       1.1  christos 					errno = EMSGSIZE;
    274       1.1  christos 					return (-1);
    275       1.1  christos 				}
    276       1.1  christos 				c = n;
    277       1.1  christos 			}
    278       1.1  christos 			escaped = 0;
    279       1.1  christos 		} else if (c == '\\') {
    280       1.1  christos 			escaped = 1;
    281       1.1  christos 			continue;
    282       1.1  christos 		} else if (c == '.') {
    283       1.1  christos 			c = (bp - label - 1);
    284       1.4  christos 			if ((c & NS_CMPRSFLGS) != 0) {	/*%< Label too big. */
    285       1.1  christos 				errno = EMSGSIZE;
    286       1.1  christos 				return (-1);
    287       1.1  christos 			}
    288       1.1  christos 			if (label >= eom) {
    289       1.1  christos 				errno = EMSGSIZE;
    290       1.1  christos 				return (-1);
    291       1.1  christos 			}
    292       1.1  christos 			*label = c;
    293       1.1  christos 			/* Fully qualified ? */
    294       1.1  christos 			if (*src == '\0') {
    295       1.1  christos 				if (c != 0) {
    296       1.1  christos 					if (bp >= eom) {
    297       1.1  christos 						errno = EMSGSIZE;
    298       1.1  christos 						return (-1);
    299       1.1  christos 					}
    300       1.1  christos 					*bp++ = '\0';
    301       1.1  christos 				}
    302       1.1  christos 				if ((bp - dst) > MAXCDNAME) {
    303       1.1  christos 					errno = EMSGSIZE;
    304       1.1  christos 					return (-1);
    305       1.1  christos 				}
    306  1.6.14.1       riz 				if (dstlen != NULL)
    307  1.6.14.1       riz 					*dstlen = (bp - dst);
    308       1.1  christos 				return (1);
    309       1.1  christos 			}
    310       1.1  christos 			if (c == 0 || *src == '.') {
    311       1.1  christos 				errno = EMSGSIZE;
    312       1.1  christos 				return (-1);
    313       1.1  christos 			}
    314       1.1  christos 			label = bp++;
    315       1.1  christos 			continue;
    316       1.1  christos 		}
    317       1.1  christos 		if (bp >= eom) {
    318       1.1  christos 			errno = EMSGSIZE;
    319       1.1  christos 			return (-1);
    320       1.1  christos 		}
    321       1.1  christos 		*bp++ = (u_char)c;
    322       1.1  christos 	}
    323       1.1  christos 	c = (bp - label - 1);
    324       1.4  christos 	if ((c & NS_CMPRSFLGS) != 0) {		/*%< Label too big. */
    325       1.1  christos 		errno = EMSGSIZE;
    326       1.1  christos 		return (-1);
    327       1.1  christos 	}
    328       1.1  christos   done:
    329       1.1  christos 	if (label >= eom) {
    330       1.1  christos 		errno = EMSGSIZE;
    331       1.1  christos 		return (-1);
    332       1.1  christos 	}
    333       1.1  christos 	*label = c;
    334       1.1  christos 	if (c != 0) {
    335       1.1  christos 		if (bp >= eom) {
    336       1.1  christos 			errno = EMSGSIZE;
    337       1.1  christos 			return (-1);
    338       1.1  christos 		}
    339       1.1  christos 		*bp++ = 0;
    340       1.1  christos 	}
    341       1.4  christos 	if ((bp - dst) > MAXCDNAME) {	/*%< src too big */
    342       1.1  christos 		errno = EMSGSIZE;
    343       1.1  christos 		return (-1);
    344       1.1  christos 	}
    345  1.6.14.1       riz 	if (dstlen != NULL)
    346  1.6.14.1       riz 		*dstlen = (bp - dst);
    347       1.1  christos 	return (0);
    348       1.1  christos }
    349       1.1  christos 
    350       1.4  christos /*%
    351       1.1  christos  *	Convert a network strings labels into all lowercase.
    352       1.4  christos  *
    353       1.1  christos  * return:
    354       1.4  christos  *\li	Number of bytes written to buffer, or -1 (with errno set)
    355       1.4  christos  *
    356       1.1  christos  * notes:
    357       1.4  christos  *\li	Enforces label and domain length limits.
    358       1.1  christos  */
    359       1.1  christos 
    360       1.1  christos int
    361       1.1  christos ns_name_ntol(const u_char *src, u_char *dst, size_t dstsiz)
    362       1.1  christos {
    363       1.1  christos 	const u_char *cp;
    364       1.1  christos 	u_char *dn, *eom;
    365       1.1  christos 	u_char c;
    366       1.1  christos 	u_int n;
    367       1.1  christos 	int l;
    368       1.1  christos 
    369       1.1  christos 	cp = src;
    370       1.1  christos 	dn = dst;
    371       1.1  christos 	eom = dst + dstsiz;
    372       1.1  christos 
    373       1.1  christos 	if (dn >= eom) {
    374       1.1  christos 		errno = EMSGSIZE;
    375       1.1  christos 		return (-1);
    376       1.1  christos 	}
    377       1.1  christos 	while ((n = *cp++) != 0) {
    378       1.1  christos 		if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
    379       1.1  christos 			/* Some kind of compression pointer. */
    380       1.1  christos 			errno = EMSGSIZE;
    381       1.1  christos 			return (-1);
    382       1.1  christos 		}
    383       1.1  christos 		*dn++ = n;
    384       1.1  christos 		if ((l = labellen(cp - 1)) < 0) {
    385       1.1  christos 			errno = EMSGSIZE;
    386       1.1  christos 			return (-1);
    387       1.1  christos 		}
    388       1.1  christos 		if (dn + l >= eom) {
    389       1.1  christos 			errno = EMSGSIZE;
    390       1.1  christos 			return (-1);
    391       1.1  christos 		}
    392       1.2  christos 		for (; l > 0; l--) {
    393       1.1  christos 			c = *cp++;
    394  1.6.14.1       riz 			if (isascii(c) && isupper(c))
    395       1.1  christos 				*dn++ = tolower(c);
    396       1.1  christos 			else
    397       1.1  christos 				*dn++ = c;
    398       1.1  christos 		}
    399       1.1  christos 	}
    400       1.1  christos 	*dn++ = '\0';
    401       1.1  christos 	return (dn - dst);
    402       1.1  christos }
    403       1.1  christos 
    404       1.4  christos /*%
    405       1.1  christos  *	Unpack a domain name from a message, source may be compressed.
    406       1.4  christos  *
    407       1.1  christos  * return:
    408       1.4  christos  *\li	-1 if it fails, or consumed octets if it succeeds.
    409       1.1  christos  */
    410       1.1  christos int
    411       1.1  christos ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
    412       1.1  christos 	       u_char *dst, size_t dstsiz)
    413       1.1  christos {
    414  1.6.14.1       riz 	return (ns_name_unpack2(msg, eom, src, dst, dstsiz, NULL));
    415  1.6.14.1       riz }
    416  1.6.14.1       riz 
    417  1.6.14.1       riz /*
    418  1.6.14.1       riz  * ns_name_unpack2(msg, eom, src, dst, dstsiz, *dstlen)
    419  1.6.14.1       riz  *	Unpack a domain name from a message, source may be compressed.
    420  1.6.14.1       riz  * return:
    421  1.6.14.1       riz  *	-1 if it fails, or consumed octets if it succeeds.
    422  1.6.14.1       riz  * side effect:
    423  1.6.14.1       riz  *	fills in *dstlen (if non-NULL).
    424  1.6.14.1       riz  */
    425  1.6.14.1       riz int
    426  1.6.14.1       riz ns_name_unpack2(const u_char *msg, const u_char *eom, const u_char *src,
    427  1.6.14.1       riz 		u_char *dst, size_t dstsiz, size_t *dstlen)
    428  1.6.14.1       riz {
    429       1.1  christos 	const u_char *srcp, *dstlim;
    430       1.1  christos 	u_char *dstp;
    431       1.1  christos 	int n, len, checked, l;
    432       1.1  christos 
    433       1.1  christos 	len = -1;
    434       1.1  christos 	checked = 0;
    435       1.1  christos 	dstp = dst;
    436       1.1  christos 	srcp = src;
    437       1.1  christos 	dstlim = dst + dstsiz;
    438       1.1  christos 	if (srcp < msg || srcp >= eom) {
    439       1.1  christos 		errno = EMSGSIZE;
    440       1.1  christos 		return (-1);
    441       1.1  christos 	}
    442       1.1  christos 	/* Fetch next label in domain name. */
    443       1.1  christos 	while ((n = *srcp++) != 0) {
    444       1.1  christos 		/* Check for indirection. */
    445       1.1  christos 		switch (n & NS_CMPRSFLGS) {
    446       1.1  christos 		case 0:
    447       1.1  christos 		case NS_TYPE_ELT:
    448       1.1  christos 			/* Limit checks. */
    449       1.1  christos 			if ((l = labellen(srcp - 1)) < 0) {
    450       1.1  christos 				errno = EMSGSIZE;
    451  1.6.14.1       riz 				return (-1);
    452       1.1  christos 			}
    453       1.1  christos 			if (dstp + l + 1 >= dstlim || srcp + l >= eom) {
    454       1.1  christos 				errno = EMSGSIZE;
    455       1.1  christos 				return (-1);
    456       1.1  christos 			}
    457       1.1  christos 			checked += l + 1;
    458       1.1  christos 			*dstp++ = n;
    459       1.2  christos 			memcpy(dstp, srcp, (size_t)l);
    460       1.1  christos 			dstp += l;
    461       1.1  christos 			srcp += l;
    462       1.1  christos 			break;
    463       1.1  christos 
    464       1.1  christos 		case NS_CMPRSFLGS:
    465       1.1  christos 			if (srcp >= eom) {
    466       1.1  christos 				errno = EMSGSIZE;
    467       1.1  christos 				return (-1);
    468       1.1  christos 			}
    469       1.1  christos 			if (len < 0)
    470       1.1  christos 				len = srcp - src + 1;
    471       1.1  christos 			srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
    472       1.4  christos 			if (srcp < msg || srcp >= eom) {  /*%< Out of range. */
    473       1.1  christos 				errno = EMSGSIZE;
    474       1.1  christos 				return (-1);
    475       1.1  christos 			}
    476       1.1  christos 			checked += 2;
    477       1.1  christos 			/*
    478       1.1  christos 			 * Check for loops in the compressed name;
    479       1.1  christos 			 * if we've looked at the whole message,
    480       1.1  christos 			 * there must be a loop.
    481       1.1  christos 			 */
    482       1.1  christos 			if (checked >= eom - msg) {
    483       1.1  christos 				errno = EMSGSIZE;
    484       1.1  christos 				return (-1);
    485       1.1  christos 			}
    486       1.1  christos 			break;
    487       1.1  christos 
    488       1.1  christos 		default:
    489       1.1  christos 			errno = EMSGSIZE;
    490       1.4  christos 			return (-1);			/*%< flag error */
    491       1.1  christos 		}
    492       1.1  christos 	}
    493  1.6.14.1       riz 	*dstp++ = 0;
    494  1.6.14.1       riz 	if (dstlen != NULL)
    495  1.6.14.1       riz 		*dstlen = dstp - dst;
    496       1.1  christos 	if (len < 0)
    497       1.1  christos 		len = srcp - src;
    498       1.1  christos 	return (len);
    499       1.1  christos }
    500       1.1  christos 
    501       1.4  christos /*%
    502       1.1  christos  *	Pack domain name 'domain' into 'comp_dn'.
    503       1.4  christos  *
    504       1.1  christos  * return:
    505       1.4  christos  *\li	Size of the compressed name, or -1.
    506       1.4  christos  *
    507       1.1  christos  * notes:
    508       1.4  christos  *\li	'dnptrs' is an array of pointers to previous compressed names.
    509       1.4  christos  *\li	dnptrs[0] is a pointer to the beginning of the message. The array
    510       1.1  christos  *	ends with NULL.
    511       1.4  christos  *\li	'lastdnptr' is a pointer to the end of the array pointed to
    512       1.1  christos  *	by 'dnptrs'.
    513       1.4  christos  *
    514       1.1  christos  * Side effects:
    515       1.4  christos  *\li	The list of pointers in dnptrs is updated for labels inserted into
    516       1.1  christos  *	the message as we compress the name.  If 'dnptr' is NULL, we don't
    517       1.1  christos  *	try to compress names. If 'lastdnptr' is NULL, we don't update the
    518       1.1  christos  *	list.
    519       1.1  christos  */
    520       1.1  christos int
    521       1.1  christos ns_name_pack(const u_char *src, u_char *dst, int dstsiz,
    522       1.1  christos 	     const u_char **dnptrs, const u_char **lastdnptr)
    523       1.1  christos {
    524       1.1  christos 	u_char *dstp;
    525       1.1  christos 	const u_char **cpp, **lpp, *eob, *msg;
    526       1.1  christos 	const u_char *srcp;
    527       1.1  christos 	int n, l, first = 1;
    528       1.1  christos 
    529       1.1  christos 	srcp = src;
    530       1.1  christos 	dstp = dst;
    531       1.1  christos 	eob = dstp + dstsiz;
    532       1.1  christos 	lpp = cpp = NULL;
    533       1.1  christos 	if (dnptrs != NULL) {
    534       1.1  christos 		if ((msg = *dnptrs++) != NULL) {
    535       1.1  christos 			for (cpp = dnptrs; *cpp != NULL; cpp++)
    536       1.4  christos 				continue;
    537       1.4  christos 			lpp = cpp;	/*%< end of list to search */
    538       1.1  christos 		}
    539       1.1  christos 	} else
    540       1.1  christos 		msg = NULL;
    541       1.1  christos 
    542       1.1  christos 	/* make sure the domain we are about to add is legal */
    543       1.1  christos 	l = 0;
    544       1.1  christos 	do {
    545       1.1  christos 		int l0;
    546       1.1  christos 
    547       1.1  christos 		n = *srcp;
    548       1.1  christos 		if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
    549       1.1  christos 			errno = EMSGSIZE;
    550       1.1  christos 			return (-1);
    551       1.1  christos 		}
    552       1.1  christos 		if ((l0 = labellen(srcp)) < 0) {
    553       1.1  christos 			errno = EINVAL;
    554  1.6.14.1       riz 			return (-1);
    555       1.1  christos 		}
    556       1.1  christos 		l += l0 + 1;
    557       1.1  christos 		if (l > MAXCDNAME) {
    558       1.1  christos 			errno = EMSGSIZE;
    559       1.1  christos 			return (-1);
    560       1.1  christos 		}
    561       1.1  christos 		srcp += l0 + 1;
    562       1.1  christos 	} while (n != 0);
    563       1.1  christos 
    564       1.1  christos 	/* from here on we need to reset compression pointer array on error */
    565       1.1  christos 	srcp = src;
    566       1.1  christos 	do {
    567       1.1  christos 		/* Look to see if we can use pointers. */
    568       1.1  christos 		n = *srcp;
    569       1.1  christos 		if (n != 0 && msg != NULL) {
    570       1.1  christos 			l = dn_find(srcp, msg, (const u_char * const *)dnptrs,
    571       1.1  christos 				    (const u_char * const *)lpp);
    572       1.1  christos 			if (l >= 0) {
    573       1.1  christos 				if (dstp + 1 >= eob) {
    574       1.1  christos 					goto cleanup;
    575       1.1  christos 				}
    576       1.2  christos 				*dstp++ = ((u_int32_t)l >> 8) | NS_CMPRSFLGS;
    577       1.1  christos 				*dstp++ = l % 256;
    578       1.1  christos 				return (dstp - dst);
    579       1.1  christos 			}
    580       1.1  christos 			/* Not found, save it. */
    581       1.1  christos 			if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
    582       1.1  christos 			    (dstp - msg) < 0x4000 && first) {
    583       1.1  christos 				*cpp++ = dstp;
    584       1.1  christos 				*cpp = NULL;
    585       1.1  christos 				first = 0;
    586       1.1  christos 			}
    587       1.1  christos 		}
    588       1.1  christos 		/* copy label to buffer */
    589       1.1  christos 		if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
    590       1.1  christos 			/* Should not happen. */
    591       1.1  christos 			goto cleanup;
    592       1.1  christos 		}
    593       1.1  christos 		n = labellen(srcp);
    594       1.1  christos 		if (dstp + 1 + n >= eob) {
    595       1.1  christos 			goto cleanup;
    596       1.1  christos 		}
    597       1.2  christos 		memcpy(dstp, srcp, (size_t)(n + 1));
    598       1.1  christos 		srcp += n + 1;
    599       1.1  christos 		dstp += n + 1;
    600       1.1  christos 	} while (n != 0);
    601       1.1  christos 
    602       1.1  christos 	if (dstp > eob) {
    603       1.1  christos cleanup:
    604       1.1  christos 		if (msg != NULL)
    605       1.1  christos 			*lpp = NULL;
    606       1.1  christos 		errno = EMSGSIZE;
    607       1.1  christos 		return (-1);
    608       1.1  christos 	}
    609       1.1  christos 	return (dstp - dst);
    610       1.1  christos }
    611       1.1  christos 
    612       1.4  christos /*%
    613       1.1  christos  *	Expand compressed domain name to presentation format.
    614       1.4  christos  *
    615       1.1  christos  * return:
    616       1.4  christos  *\li	Number of bytes read out of `src', or -1 (with errno set).
    617       1.4  christos  *
    618       1.1  christos  * note:
    619       1.4  christos  *\li	Root domain returns as "." not "".
    620       1.1  christos  */
    621       1.1  christos int
    622       1.1  christos ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src,
    623       1.1  christos 		   char *dst, size_t dstsiz)
    624       1.1  christos {
    625       1.1  christos 	u_char tmp[NS_MAXCDNAME];
    626       1.1  christos 	int n;
    627       1.1  christos 
    628       1.1  christos 	if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
    629       1.1  christos 		return (-1);
    630       1.1  christos 	if (ns_name_ntop(tmp, dst, dstsiz) == -1)
    631       1.1  christos 		return (-1);
    632       1.1  christos 	return (n);
    633       1.1  christos }
    634       1.1  christos 
    635       1.4  christos /*%
    636       1.1  christos  *	Compress a domain name into wire format, using compression pointers.
    637       1.4  christos  *
    638       1.1  christos  * return:
    639       1.4  christos  *\li	Number of bytes consumed in `dst' or -1 (with errno set).
    640       1.4  christos  *
    641       1.1  christos  * notes:
    642       1.4  christos  *\li	'dnptrs' is an array of pointers to previous compressed names.
    643       1.4  christos  *\li	dnptrs[0] is a pointer to the beginning of the message.
    644       1.4  christos  *\li	The list ends with NULL.  'lastdnptr' is a pointer to the end of the
    645       1.1  christos  *	array pointed to by 'dnptrs'. Side effect is to update the list of
    646       1.1  christos  *	pointers for labels inserted into the message as we compress the name.
    647       1.4  christos  *\li	If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
    648       1.1  christos  *	is NULL, we don't update the list.
    649       1.1  christos  */
    650       1.1  christos int
    651       1.1  christos ns_name_compress(const char *src, u_char *dst, size_t dstsiz,
    652       1.1  christos 		 const u_char **dnptrs, const u_char **lastdnptr)
    653       1.1  christos {
    654       1.1  christos 	u_char tmp[NS_MAXCDNAME];
    655       1.1  christos 
    656       1.1  christos 	if (ns_name_pton(src, tmp, sizeof tmp) == -1)
    657       1.1  christos 		return (-1);
    658       1.2  christos 	return (ns_name_pack(tmp, dst, (int)dstsiz, dnptrs, lastdnptr));
    659       1.1  christos }
    660       1.1  christos 
    661       1.4  christos /*%
    662       1.1  christos  * Reset dnptrs so that there are no active references to pointers at or
    663       1.1  christos  * after src.
    664       1.1  christos  */
    665       1.1  christos void
    666       1.1  christos ns_name_rollback(const u_char *src, const u_char **dnptrs,
    667       1.1  christos 		 const u_char **lastdnptr)
    668       1.1  christos {
    669       1.1  christos 	while (dnptrs < lastdnptr && *dnptrs != NULL) {
    670       1.1  christos 		if (*dnptrs >= src) {
    671       1.1  christos 			*dnptrs = NULL;
    672       1.1  christos 			break;
    673       1.1  christos 		}
    674       1.1  christos 		dnptrs++;
    675       1.1  christos 	}
    676       1.1  christos }
    677       1.1  christos 
    678       1.4  christos /*%
    679       1.1  christos  *	Advance *ptrptr to skip over the compressed name it points at.
    680       1.4  christos  *
    681       1.1  christos  * return:
    682       1.4  christos  *\li	0 on success, -1 (with errno set) on failure.
    683       1.1  christos  */
    684       1.1  christos int
    685       1.1  christos ns_name_skip(const u_char **ptrptr, const u_char *eom)
    686       1.1  christos {
    687       1.1  christos 	const u_char *cp;
    688       1.1  christos 	u_int n;
    689       1.1  christos 	int l;
    690       1.1  christos 
    691       1.1  christos 	cp = *ptrptr;
    692       1.1  christos 	while (cp < eom && (n = *cp++) != 0) {
    693       1.1  christos 		/* Check for indirection. */
    694       1.1  christos 		switch (n & NS_CMPRSFLGS) {
    695       1.4  christos 		case 0:			/*%< normal case, n == len */
    696       1.1  christos 			cp += n;
    697       1.1  christos 			continue;
    698       1.4  christos 		case NS_TYPE_ELT: /*%< EDNS0 extended label */
    699       1.1  christos 			if ((l = labellen(cp - 1)) < 0) {
    700       1.4  christos 				errno = EMSGSIZE; /*%< XXX */
    701  1.6.14.1       riz 				return (-1);
    702       1.1  christos 			}
    703       1.1  christos 			cp += l;
    704       1.1  christos 			continue;
    705       1.4  christos 		case NS_CMPRSFLGS:	/*%< indirection */
    706       1.1  christos 			cp++;
    707       1.1  christos 			break;
    708       1.4  christos 		default:		/*%< illegal type */
    709       1.1  christos 			errno = EMSGSIZE;
    710       1.1  christos 			return (-1);
    711       1.1  christos 		}
    712       1.1  christos 		break;
    713       1.1  christos 	}
    714       1.1  christos 	if (cp > eom) {
    715       1.1  christos 		errno = EMSGSIZE;
    716       1.1  christos 		return (-1);
    717       1.1  christos 	}
    718       1.1  christos 	*ptrptr = cp;
    719       1.1  christos 	return (0);
    720       1.1  christos }
    721       1.1  christos 
    722  1.6.14.1       riz /* Find the number of octets an nname takes up, including the root label.
    723  1.6.14.1       riz  * (This is basically ns_name_skip() without compression-pointer support.)
    724  1.6.14.1       riz  * ((NOTE: can only return zero if passed-in namesiz argument is zero.))
    725  1.6.14.1       riz  */
    726  1.6.14.1       riz ssize_t
    727  1.6.14.1       riz ns_name_length(ns_nname_ct nname, size_t namesiz) {
    728  1.6.14.1       riz 	ns_nname_ct orig = nname;
    729  1.6.14.1       riz 	u_int n;
    730  1.6.14.1       riz 
    731  1.6.14.1       riz 	while (namesiz-- > 0 && (n = *nname++) != 0) {
    732  1.6.14.1       riz 		if ((n & NS_CMPRSFLGS) != 0) {
    733  1.6.14.1       riz 			errno = EISDIR;
    734  1.6.14.1       riz 			return (-1);
    735  1.6.14.1       riz 		}
    736  1.6.14.1       riz 		if (n > namesiz) {
    737  1.6.14.1       riz 			errno = EMSGSIZE;
    738  1.6.14.1       riz 			return (-1);
    739  1.6.14.1       riz 		}
    740  1.6.14.1       riz 		nname += n;
    741  1.6.14.1       riz 		namesiz -= n;
    742  1.6.14.1       riz 	}
    743  1.6.14.1       riz 	return (nname - orig);
    744  1.6.14.1       riz }
    745  1.6.14.1       riz 
    746  1.6.14.1       riz /* Compare two nname's for equality.  Return -1 on error (setting errno).
    747  1.6.14.1       riz  */
    748  1.6.14.1       riz int
    749  1.6.14.1       riz ns_name_eq(ns_nname_ct a, size_t as, ns_nname_ct b, size_t bs) {
    750  1.6.14.1       riz 	ns_nname_ct ae = a + as, be = b + bs;
    751  1.6.14.1       riz 	int ac, bc;
    752  1.6.14.1       riz 
    753  1.6.14.1       riz 	while (ac = *a, bc = *b, ac != 0 && bc != 0) {
    754  1.6.14.1       riz 		if ((ac & NS_CMPRSFLGS) != 0 || (bc & NS_CMPRSFLGS) != 0) {
    755  1.6.14.1       riz 			errno = EISDIR;
    756  1.6.14.1       riz 			return (-1);
    757  1.6.14.1       riz 		}
    758  1.6.14.1       riz 		if (a + ac >= ae || b + bc >= be) {
    759  1.6.14.1       riz 			errno = EMSGSIZE;
    760  1.6.14.1       riz 			return (-1);
    761  1.6.14.1       riz 		}
    762  1.6.14.1       riz 		if (ac != bc || strncasecmp((const char *) ++a,
    763  1.6.14.1       riz 					    (const char *) ++b,
    764  1.6.14.1       riz 					    (size_t)ac) != 0)
    765  1.6.14.1       riz 			return (0);
    766  1.6.14.1       riz 		a += ac, b += bc;
    767  1.6.14.1       riz 	}
    768  1.6.14.1       riz 	return (ac == 0 && bc == 0);
    769  1.6.14.1       riz }
    770  1.6.14.1       riz 
    771  1.6.14.1       riz /* Is domain "A" owned by (at or below) domain "B"?
    772  1.6.14.1       riz  */
    773  1.6.14.1       riz int
    774  1.6.14.1       riz ns_name_owned(ns_namemap_ct a, int an, ns_namemap_ct b, int bn) {
    775  1.6.14.1       riz 	/* If A is shorter, it cannot be owned by B. */
    776  1.6.14.1       riz 	if (an < bn)
    777  1.6.14.1       riz 		return (0);
    778  1.6.14.1       riz 
    779  1.6.14.1       riz 	/* If they are unequal before the length of the shorter, A cannot... */
    780  1.6.14.1       riz 	while (bn > 0) {
    781  1.6.14.1       riz 		if (a->len != b->len ||
    782  1.6.14.1       riz 		    strncasecmp((const char *) a->base,
    783  1.6.14.1       riz 				(const char *) b->base, (size_t)a->len) != 0)
    784  1.6.14.1       riz 			return (0);
    785  1.6.14.1       riz 		a++, an--;
    786  1.6.14.1       riz 		b++, bn--;
    787  1.6.14.1       riz 	}
    788  1.6.14.1       riz 
    789  1.6.14.1       riz 	/* A might be longer or not, but either way, B owns it. */
    790  1.6.14.1       riz 	return (1);
    791  1.6.14.1       riz }
    792  1.6.14.1       riz 
    793  1.6.14.1       riz /* Build an array of <base,len> tuples from an nname, top-down order.
    794  1.6.14.1       riz  * Return the number of tuples (labels) thus discovered.
    795  1.6.14.1       riz  */
    796  1.6.14.1       riz int
    797  1.6.14.1       riz ns_name_map(ns_nname_ct nname, size_t namelen, ns_namemap_t map, int mapsize) {
    798  1.6.14.1       riz 	u_int n;
    799  1.6.14.1       riz 	int l;
    800  1.6.14.1       riz 
    801  1.6.14.1       riz 	n = *nname++;
    802  1.6.14.1       riz 	namelen--;
    803  1.6.14.1       riz 
    804  1.6.14.1       riz 	/* Root zone? */
    805  1.6.14.1       riz 	if (n == 0) {
    806  1.6.14.1       riz 		/* Extra data follows name? */
    807  1.6.14.1       riz 		if (namelen > 0) {
    808  1.6.14.1       riz 			errno = EMSGSIZE;
    809  1.6.14.1       riz 			return (-1);
    810  1.6.14.1       riz 		}
    811  1.6.14.1       riz 		return (0);
    812  1.6.14.1       riz 	}
    813  1.6.14.1       riz 
    814  1.6.14.1       riz 	/* Compression pointer? */
    815  1.6.14.1       riz 	if ((n & NS_CMPRSFLGS) != 0) {
    816  1.6.14.1       riz 		errno = EISDIR;
    817  1.6.14.1       riz 		return (-1);
    818  1.6.14.1       riz 	}
    819  1.6.14.1       riz 
    820  1.6.14.1       riz 	/* Label too long? */
    821  1.6.14.1       riz 	if (n > namelen) {
    822  1.6.14.1       riz 		errno = EMSGSIZE;
    823  1.6.14.1       riz 		return (-1);
    824  1.6.14.1       riz 	}
    825  1.6.14.1       riz 
    826  1.6.14.1       riz 	/* Recurse to get rest of name done first. */
    827  1.6.14.1       riz 	l = ns_name_map(nname + n, namelen - n, map, mapsize);
    828  1.6.14.1       riz 	if (l < 0)
    829  1.6.14.1       riz 		return (-1);
    830  1.6.14.1       riz 
    831  1.6.14.1       riz 	/* Too many labels? */
    832  1.6.14.1       riz 	if (l >= mapsize) {
    833  1.6.14.1       riz 		errno = ENAMETOOLONG;
    834  1.6.14.1       riz 		return (-1);
    835  1.6.14.1       riz 	}
    836  1.6.14.1       riz 
    837  1.6.14.1       riz 	/* We're on our way back up-stack, store current map data. */
    838  1.6.14.1       riz 	map[l].base = nname;
    839  1.6.14.1       riz 	map[l].len = n;
    840  1.6.14.1       riz 	return (l + 1);
    841  1.6.14.1       riz }
    842  1.6.14.1       riz 
    843  1.6.14.1       riz /* Count the labels in a domain name.  Root counts, so COM. has two.  This
    844  1.6.14.1       riz  * is to make the result comparable to the result of ns_name_map().
    845  1.6.14.1       riz  */
    846  1.6.14.1       riz int
    847  1.6.14.1       riz ns_name_labels(ns_nname_ct nname, size_t namesiz) {
    848  1.6.14.1       riz 	int ret = 0;
    849  1.6.14.1       riz 	u_int n;
    850  1.6.14.1       riz 
    851  1.6.14.1       riz 	while (namesiz-- > 0 && (n = *nname++) != 0) {
    852  1.6.14.1       riz 		if ((n & NS_CMPRSFLGS) != 0) {
    853  1.6.14.1       riz 			errno = EISDIR;
    854  1.6.14.1       riz 			return (-1);
    855  1.6.14.1       riz 		}
    856  1.6.14.1       riz 		if (n > namesiz) {
    857  1.6.14.1       riz 			errno = EMSGSIZE;
    858  1.6.14.1       riz 			return (-1);
    859  1.6.14.1       riz 		}
    860  1.6.14.1       riz 		nname += n;
    861  1.6.14.1       riz 		namesiz -= n;
    862  1.6.14.1       riz 		ret++;
    863  1.6.14.1       riz 	}
    864  1.6.14.1       riz 	return (ret + 1);
    865  1.6.14.1       riz }
    866  1.6.14.1       riz 
    867       1.1  christos /* Private. */
    868       1.1  christos 
    869       1.4  christos /*%
    870       1.1  christos  *	Thinking in noninternationalized USASCII (per the DNS spec),
    871       1.1  christos  *	is this characted special ("in need of quoting") ?
    872       1.4  christos  *
    873       1.1  christos  * return:
    874       1.4  christos  *\li	boolean.
    875       1.1  christos  */
    876       1.1  christos static int
    877       1.1  christos special(int ch) {
    878       1.1  christos 	switch (ch) {
    879       1.4  christos 	case 0x22: /*%< '"' */
    880       1.4  christos 	case 0x2E: /*%< '.' */
    881       1.4  christos 	case 0x3B: /*%< ';' */
    882       1.4  christos 	case 0x5C: /*%< '\\' */
    883       1.4  christos 	case 0x28: /*%< '(' */
    884       1.4  christos 	case 0x29: /*%< ')' */
    885       1.1  christos 	/* Special modifiers in zone files. */
    886       1.4  christos 	case 0x40: /*%< '@' */
    887       1.4  christos 	case 0x24: /*%< '$' */
    888       1.1  christos 		return (1);
    889       1.1  christos 	default:
    890       1.1  christos 		return (0);
    891       1.1  christos 	}
    892       1.1  christos }
    893       1.1  christos 
    894       1.4  christos /*%
    895       1.1  christos  *	Thinking in noninternationalized USASCII (per the DNS spec),
    896       1.1  christos  *	is this character visible and not a space when printed ?
    897       1.4  christos  *
    898       1.1  christos  * return:
    899       1.4  christos  *\li	boolean.
    900       1.1  christos  */
    901       1.1  christos static int
    902       1.1  christos printable(int ch) {
    903       1.1  christos 	return (ch > 0x20 && ch < 0x7f);
    904       1.1  christos }
    905       1.1  christos 
    906       1.4  christos /*%
    907       1.1  christos  *	Thinking in noninternationalized USASCII (per the DNS spec),
    908       1.1  christos  *	convert this character to lower case if it's upper case.
    909       1.1  christos  */
    910       1.1  christos static int
    911       1.1  christos mklower(int ch) {
    912       1.1  christos 	if (ch >= 0x41 && ch <= 0x5A)
    913       1.1  christos 		return (ch + 0x20);
    914       1.1  christos 	return (ch);
    915       1.1  christos }
    916       1.1  christos 
    917       1.4  christos /*%
    918       1.1  christos  *	Search for the counted-label name in an array of compressed names.
    919       1.4  christos  *
    920       1.1  christos  * return:
    921       1.4  christos  *\li	offset from msg if found, or -1.
    922       1.4  christos  *
    923       1.1  christos  * notes:
    924       1.4  christos  *\li	dnptrs is the pointer to the first name on the list,
    925       1.4  christos  *\li	not the pointer to the start of the message.
    926       1.1  christos  */
    927       1.1  christos static int
    928       1.1  christos dn_find(const u_char *domain, const u_char *msg,
    929       1.1  christos 	const u_char * const *dnptrs,
    930       1.1  christos 	const u_char * const *lastdnptr)
    931       1.1  christos {
    932       1.1  christos 	const u_char *dn, *cp, *sp;
    933       1.1  christos 	const u_char * const *cpp;
    934       1.1  christos 	u_int n;
    935       1.1  christos 
    936       1.1  christos 	for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
    937       1.1  christos 		sp = *cpp;
    938       1.1  christos 		/*
    939       1.1  christos 		 * terminate search on:
    940       1.1  christos 		 * root label
    941       1.1  christos 		 * compression pointer
    942       1.1  christos 		 * unusable offset
    943       1.1  christos 		 */
    944       1.1  christos 		while (*sp != 0 && (*sp & NS_CMPRSFLGS) == 0 &&
    945       1.1  christos 		       (sp - msg) < 0x4000) {
    946       1.1  christos 			dn = domain;
    947       1.1  christos 			cp = sp;
    948       1.1  christos 			while ((n = *cp++) != 0) {
    949       1.1  christos 				/*
    950       1.1  christos 				 * check for indirection
    951       1.1  christos 				 */
    952       1.1  christos 				switch (n & NS_CMPRSFLGS) {
    953       1.4  christos 				case 0:		/*%< normal case, n == len */
    954       1.4  christos 					n = labellen(cp - 1); /*%< XXX */
    955       1.1  christos 					if (n != *dn++)
    956       1.1  christos 						goto next;
    957       1.1  christos 
    958       1.2  christos 					for (; n > 0; n--)
    959       1.1  christos 						if (mklower(*dn++) !=
    960       1.1  christos 						    mklower(*cp++))
    961       1.1  christos 							goto next;
    962       1.1  christos 					/* Is next root for both ? */
    963       1.1  christos 					if (*dn == '\0' && *cp == '\0')
    964       1.1  christos 						return (sp - msg);
    965       1.1  christos 					if (*dn)
    966       1.1  christos 						continue;
    967       1.1  christos 					goto next;
    968       1.4  christos 				case NS_CMPRSFLGS:	/*%< indirection */
    969       1.1  christos 					cp = msg + (((n & 0x3f) << 8) | *cp);
    970       1.1  christos 					break;
    971       1.1  christos 
    972       1.4  christos 				default:	/*%< illegal type */
    973       1.1  christos 					errno = EMSGSIZE;
    974       1.1  christos 					return (-1);
    975       1.1  christos 				}
    976       1.1  christos 			}
    977       1.1  christos  next: ;
    978       1.1  christos 			sp += *sp + 1;
    979       1.1  christos 		}
    980       1.1  christos 	}
    981       1.1  christos 	errno = ENOENT;
    982       1.1  christos 	return (-1);
    983       1.1  christos }
    984       1.1  christos 
    985       1.1  christos static int
    986       1.3  christos decode_bitstring(const unsigned char **cpp, char *dn, const char *eom)
    987       1.1  christos {
    988       1.3  christos 	const unsigned char *cp = *cpp;
    989       1.1  christos 	char *beg = dn, tc;
    990       1.1  christos 	int b, blen, plen, i;
    991       1.1  christos 
    992       1.1  christos 	if ((blen = (*cp & 0xff)) == 0)
    993       1.1  christos 		blen = 256;
    994       1.1  christos 	plen = (blen + 3) / 4;
    995       1.1  christos 	plen += sizeof("\\[x/]") + (blen > 99 ? 3 : (blen > 9) ? 2 : 1);
    996       1.1  christos 	if (dn + plen >= eom)
    997  1.6.14.1       riz 		return (-1);
    998       1.1  christos 
    999       1.1  christos 	cp++;
   1000       1.1  christos 	i = SPRINTF((dn, "\\[x"));
   1001       1.1  christos 	if (i < 0)
   1002       1.1  christos 		return (-1);
   1003       1.1  christos 	dn += i;
   1004       1.1  christos 	for (b = blen; b > 7; b -= 8, cp++) {
   1005       1.1  christos 		i = SPRINTF((dn, "%02x", *cp & 0xff));
   1006       1.1  christos 		if (i < 0)
   1007       1.1  christos 			return (-1);
   1008       1.1  christos 		dn += i;
   1009       1.1  christos 	}
   1010       1.1  christos 	if (b > 4) {
   1011       1.1  christos 		tc = *cp++;
   1012       1.1  christos 		i = SPRINTF((dn, "%02x", tc & (0xff << (8 - b))));
   1013       1.1  christos 		if (i < 0)
   1014       1.1  christos 			return (-1);
   1015       1.1  christos 		dn += i;
   1016       1.1  christos 	} else if (b > 0) {
   1017       1.1  christos 		tc = *cp++;
   1018       1.1  christos 		i = SPRINTF((dn, "%1x",
   1019       1.2  christos 			       (((u_int32_t)tc >> 4) & 0x0f) & (0x0f << (4 - b))));
   1020       1.1  christos 		if (i < 0)
   1021       1.1  christos 			return (-1);
   1022       1.1  christos 		dn += i;
   1023       1.1  christos 	}
   1024       1.1  christos 	i = SPRINTF((dn, "/%d]", blen));
   1025       1.1  christos 	if (i < 0)
   1026       1.1  christos 		return (-1);
   1027       1.1  christos 	dn += i;
   1028       1.1  christos 
   1029       1.1  christos 	*cpp = cp;
   1030  1.6.14.1       riz 	return (dn - beg);
   1031       1.1  christos }
   1032       1.1  christos 
   1033       1.1  christos static int
   1034       1.3  christos encode_bitsring(const char **bp, const char *end, unsigned char **labelp,
   1035  1.6.14.1       riz 		unsigned char ** dst, unsigned const char *eom)
   1036       1.1  christos {
   1037       1.1  christos 	int afterslash = 0;
   1038       1.1  christos 	const char *cp = *bp;
   1039       1.3  christos 	unsigned char *tp;
   1040       1.3  christos 	char c;
   1041       1.1  christos 	const char *beg_blen;
   1042       1.1  christos 	char *end_blen = NULL;
   1043       1.1  christos 	int value = 0, count = 0, tbcount = 0, blen = 0;
   1044       1.1  christos 
   1045       1.1  christos 	beg_blen = end_blen = NULL;
   1046       1.1  christos 
   1047       1.1  christos 	/* a bitstring must contain at least 2 characters */
   1048       1.1  christos 	if (end - cp < 2)
   1049  1.6.14.1       riz 		return (EINVAL);
   1050       1.1  christos 
   1051       1.1  christos 	/* XXX: currently, only hex strings are supported */
   1052       1.1  christos 	if (*cp++ != 'x')
   1053  1.6.14.1       riz 		return (EINVAL);
   1054       1.4  christos 	if (!isxdigit((*cp) & 0xff)) /*%< reject '\[x/BLEN]' */
   1055  1.6.14.1       riz 		return (EINVAL);
   1056       1.1  christos 
   1057       1.1  christos 	for (tp = *dst + 1; cp < end && tp < eom; cp++) {
   1058       1.1  christos 		switch((c = *cp)) {
   1059       1.4  christos 		case ']':	/*%< end of the bitstring */
   1060       1.1  christos 			if (afterslash) {
   1061       1.1  christos 				if (beg_blen == NULL)
   1062  1.6.14.1       riz 					return (EINVAL);
   1063       1.1  christos 				blen = (int)strtol(beg_blen, &end_blen, 10);
   1064       1.1  christos 				if (*end_blen != ']')
   1065  1.6.14.1       riz 					return (EINVAL);
   1066       1.1  christos 			}
   1067       1.1  christos 			if (count)
   1068       1.1  christos 				*tp++ = ((value << 4) & 0xff);
   1069       1.4  christos 			cp++;	/*%< skip ']' */
   1070       1.1  christos 			goto done;
   1071       1.1  christos 		case '/':
   1072       1.1  christos 			afterslash = 1;
   1073       1.1  christos 			break;
   1074       1.1  christos 		default:
   1075       1.1  christos 			if (afterslash) {
   1076       1.1  christos 				if (!isdigit(c&0xff))
   1077  1.6.14.1       riz 					return (EINVAL);
   1078       1.1  christos 				if (beg_blen == NULL) {
   1079       1.1  christos 
   1080       1.1  christos 					if (c == '0') {
   1081       1.1  christos 						/* blen never begings with 0 */
   1082  1.6.14.1       riz 						return (EINVAL);
   1083       1.1  christos 					}
   1084       1.1  christos 					beg_blen = cp;
   1085       1.1  christos 				}
   1086       1.1  christos 			} else {
   1087       1.1  christos 				if (!isxdigit(c&0xff))
   1088  1.6.14.1       riz 					return (EINVAL);
   1089       1.1  christos 				value <<= 4;
   1090       1.1  christos 				value += digitvalue[(int)c];
   1091       1.1  christos 				count += 4;
   1092       1.1  christos 				tbcount += 4;
   1093       1.1  christos 				if (tbcount > 256)
   1094  1.6.14.1       riz 					return (EINVAL);
   1095       1.1  christos 				if (count == 8) {
   1096       1.1  christos 					*tp++ = value;
   1097       1.1  christos 					count = 0;
   1098       1.1  christos 				}
   1099       1.1  christos 			}
   1100       1.1  christos 			break;
   1101       1.1  christos 		}
   1102       1.1  christos 	}
   1103       1.1  christos   done:
   1104       1.1  christos 	if (cp >= end || tp >= eom)
   1105  1.6.14.1       riz 		return (EMSGSIZE);
   1106       1.1  christos 
   1107       1.1  christos 	/*
   1108       1.1  christos 	 * bit length validation:
   1109       1.1  christos 	 * If a <length> is present, the number of digits in the <bit-data>
   1110       1.1  christos 	 * MUST be just sufficient to contain the number of bits specified
   1111       1.1  christos 	 * by the <length>. If there are insignificant bits in a final
   1112       1.1  christos 	 * hexadecimal or octal digit, they MUST be zero.
   1113       1.4  christos 	 * RFC2673, Section 3.2.
   1114       1.1  christos 	 */
   1115       1.1  christos 	if (blen > 0) {
   1116       1.1  christos 		int traillen;
   1117       1.1  christos 
   1118       1.1  christos 		if (((blen + 3) & ~3) != tbcount)
   1119  1.6.14.1       riz 			return (EINVAL);
   1120       1.4  christos 		traillen = tbcount - blen; /*%< between 0 and 3 */
   1121       1.1  christos 		if (((value << (8 - traillen)) & 0xff) != 0)
   1122  1.6.14.1       riz 			return (EINVAL);
   1123       1.1  christos 	}
   1124       1.1  christos 	else
   1125       1.1  christos 		blen = tbcount;
   1126       1.1  christos 	if (blen == 256)
   1127       1.1  christos 		blen = 0;
   1128       1.1  christos 
   1129       1.1  christos 	/* encode the type and the significant bit fields */
   1130       1.1  christos 	**labelp = DNS_LABELTYPE_BITSTRING;
   1131       1.1  christos 	**dst = blen;
   1132       1.1  christos 
   1133       1.1  christos 	*bp = cp;
   1134       1.1  christos 	*dst = tp;
   1135       1.1  christos 
   1136  1.6.14.1       riz 	return (0);
   1137       1.1  christos }
   1138       1.1  christos 
   1139       1.1  christos static int
   1140       1.1  christos labellen(const u_char *lp)
   1141       1.1  christos {
   1142       1.1  christos 	int bitlen;
   1143       1.1  christos 	u_char l = *lp;
   1144       1.1  christos 
   1145       1.1  christos 	if ((l & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
   1146       1.1  christos 		/* should be avoided by the caller */
   1147  1.6.14.1       riz 		return (-1);
   1148       1.1  christos 	}
   1149       1.1  christos 
   1150       1.1  christos 	if ((l & NS_CMPRSFLGS) == NS_TYPE_ELT) {
   1151       1.1  christos 		if (l == DNS_LABELTYPE_BITSTRING) {
   1152       1.1  christos 			if ((bitlen = *(lp + 1)) == 0)
   1153       1.1  christos 				bitlen = 256;
   1154  1.6.14.1       riz 			return ((bitlen + 7 ) / 8 + 1);
   1155       1.1  christos 		}
   1156  1.6.14.1       riz 		return (-1);	/*%< unknwon ELT */
   1157       1.1  christos 	}
   1158  1.6.14.1       riz 	return (l);
   1159       1.1  christos }
   1160       1.4  christos 
   1161       1.4  christos /*! \file */
   1162