17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate  * Copyright (c) 1996,1999 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate  *
9*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*9525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*9525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*9525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #include "port_before.h"
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate #include <sys/types.h>
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate #include <netinet/in.h>
237c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include <errno.h>
267c478bd9Sstevel@tonic-gate #include <resolv.h>
277c478bd9Sstevel@tonic-gate #include <string.h>
287c478bd9Sstevel@tonic-gate #include <ctype.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <limits.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include "port_after.h"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #ifdef SPRINTF_CHAR
357c478bd9Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
367c478bd9Sstevel@tonic-gate #else
377c478bd9Sstevel@tonic-gate # define SPRINTF(x) ((size_t)sprintf x)
387c478bd9Sstevel@tonic-gate #endif
397c478bd9Sstevel@tonic-gate 
40*9525b14bSRao Shoaib #define NS_TYPE_ELT			0x40 /*%< EDNS0 extended label type */
417c478bd9Sstevel@tonic-gate #define DNS_LABELTYPE_BITSTRING		0x41
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /* Data. */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static const char	digits[] = "0123456789";
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate static const char digitvalue[256] = {
487c478bd9Sstevel@tonic-gate 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,	/*16*/
497c478bd9Sstevel@tonic-gate 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*32*/
507c478bd9Sstevel@tonic-gate 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*48*/
517c478bd9Sstevel@tonic-gate 	 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1, /*64*/
527c478bd9Sstevel@tonic-gate 	-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*80*/
537c478bd9Sstevel@tonic-gate 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*96*/
547c478bd9Sstevel@tonic-gate 	-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*112*/
557c478bd9Sstevel@tonic-gate 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*128*/
567c478bd9Sstevel@tonic-gate 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
577c478bd9Sstevel@tonic-gate 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
587c478bd9Sstevel@tonic-gate 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
597c478bd9Sstevel@tonic-gate 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
607c478bd9Sstevel@tonic-gate 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
617c478bd9Sstevel@tonic-gate 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
627c478bd9Sstevel@tonic-gate 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
637c478bd9Sstevel@tonic-gate 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*256*/
647c478bd9Sstevel@tonic-gate };
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /* Forward. */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate static int		special(int);
697c478bd9Sstevel@tonic-gate static int		printable(int);
707c478bd9Sstevel@tonic-gate static int		dn_find(const u_char *, const u_char *,
717c478bd9Sstevel@tonic-gate 				const u_char * const *,
727c478bd9Sstevel@tonic-gate 				const u_char * const *);
737c478bd9Sstevel@tonic-gate static int		encode_bitsring(const char **, const char *,
74*9525b14bSRao Shoaib 					unsigned char **, unsigned char **,
75*9525b14bSRao Shoaib 					unsigned const char *);
767c478bd9Sstevel@tonic-gate static int		labellen(const u_char *);
77*9525b14bSRao Shoaib static int		decode_bitstring(const unsigned char **,
78*9525b14bSRao Shoaib 					 char *, const char *);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /* Public. */
817c478bd9Sstevel@tonic-gate 
82*9525b14bSRao Shoaib /*%
837c478bd9Sstevel@tonic-gate  *	Convert an encoded domain name to printable ascii as per RFC1035.
84*9525b14bSRao Shoaib 
857c478bd9Sstevel@tonic-gate  * return:
86*9525b14bSRao Shoaib  *\li	Number of bytes written to buffer, or -1 (with errno set)
87*9525b14bSRao Shoaib  *
887c478bd9Sstevel@tonic-gate  * notes:
89*9525b14bSRao Shoaib  *\li	The root is returned as "."
90*9525b14bSRao Shoaib  *\li	All other domains are returned in non absolute form
917c478bd9Sstevel@tonic-gate  */
927c478bd9Sstevel@tonic-gate int
ns_name_ntop(const u_char * src,char * dst,size_t dstsiz)937c478bd9Sstevel@tonic-gate ns_name_ntop(const u_char *src, char *dst, size_t dstsiz)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	const u_char *cp;
967c478bd9Sstevel@tonic-gate 	char *dn, *eom;
977c478bd9Sstevel@tonic-gate 	u_char c;
987c478bd9Sstevel@tonic-gate 	u_int n;
997c478bd9Sstevel@tonic-gate 	int l;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	cp = src;
1027c478bd9Sstevel@tonic-gate 	dn = dst;
1037c478bd9Sstevel@tonic-gate 	eom = dst + dstsiz;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	while ((n = *cp++) != 0) {
1067c478bd9Sstevel@tonic-gate 		if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
1077c478bd9Sstevel@tonic-gate 			/* Some kind of compression pointer. */
1087c478bd9Sstevel@tonic-gate 			errno = EMSGSIZE;
1097c478bd9Sstevel@tonic-gate 			return (-1);
1107c478bd9Sstevel@tonic-gate 		}
1117c478bd9Sstevel@tonic-gate 		if (dn != dst) {
1127c478bd9Sstevel@tonic-gate 			if (dn >= eom) {
1137c478bd9Sstevel@tonic-gate 				errno = EMSGSIZE;
1147c478bd9Sstevel@tonic-gate 				return (-1);
1157c478bd9Sstevel@tonic-gate 			}
1167c478bd9Sstevel@tonic-gate 			*dn++ = '.';
1177c478bd9Sstevel@tonic-gate 		}
1187c478bd9Sstevel@tonic-gate 		if ((l = labellen(cp - 1)) < 0) {
119*9525b14bSRao Shoaib 			errno = EMSGSIZE; /*%< XXX */
120*9525b14bSRao Shoaib 			return (-1);
1217c478bd9Sstevel@tonic-gate 		}
1227c478bd9Sstevel@tonic-gate 		if (dn + l >= eom) {
1237c478bd9Sstevel@tonic-gate 			errno = EMSGSIZE;
1247c478bd9Sstevel@tonic-gate 			return (-1);
1257c478bd9Sstevel@tonic-gate 		}
1267c478bd9Sstevel@tonic-gate 		if ((n & NS_CMPRSFLGS) == NS_TYPE_ELT) {
1277c478bd9Sstevel@tonic-gate 			int m;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 			if (n != DNS_LABELTYPE_BITSTRING) {
1307c478bd9Sstevel@tonic-gate 				/* XXX: labellen should reject this case */
1317c478bd9Sstevel@tonic-gate 				errno = EINVAL;
132*9525b14bSRao Shoaib 				return (-1);
1337c478bd9Sstevel@tonic-gate 			}
134*9525b14bSRao Shoaib 			if ((m = decode_bitstring(&cp, dn, eom)) < 0)
1357c478bd9Sstevel@tonic-gate 			{
1367c478bd9Sstevel@tonic-gate 				errno = EMSGSIZE;
137*9525b14bSRao Shoaib 				return (-1);
1387c478bd9Sstevel@tonic-gate 			}
1397c478bd9Sstevel@tonic-gate 			dn += m;
1407c478bd9Sstevel@tonic-gate 			continue;
1417c478bd9Sstevel@tonic-gate 		}
1427c478bd9Sstevel@tonic-gate 		for ((void)NULL; l > 0; l--) {
1437c478bd9Sstevel@tonic-gate 			c = *cp++;
1447c478bd9Sstevel@tonic-gate 			if (special(c)) {
1457c478bd9Sstevel@tonic-gate 				if (dn + 1 >= eom) {
1467c478bd9Sstevel@tonic-gate 					errno = EMSGSIZE;
1477c478bd9Sstevel@tonic-gate 					return (-1);
1487c478bd9Sstevel@tonic-gate 				}
1497c478bd9Sstevel@tonic-gate 				*dn++ = '\\';
1507c478bd9Sstevel@tonic-gate 				*dn++ = (char)c;
1517c478bd9Sstevel@tonic-gate 			} else if (!printable(c)) {
1527c478bd9Sstevel@tonic-gate 				if (dn + 3 >= eom) {
1537c478bd9Sstevel@tonic-gate 					errno = EMSGSIZE;
1547c478bd9Sstevel@tonic-gate 					return (-1);
1557c478bd9Sstevel@tonic-gate 				}
1567c478bd9Sstevel@tonic-gate 				*dn++ = '\\';
1577c478bd9Sstevel@tonic-gate 				*dn++ = digits[c / 100];
1587c478bd9Sstevel@tonic-gate 				*dn++ = digits[(c % 100) / 10];
1597c478bd9Sstevel@tonic-gate 				*dn++ = digits[c % 10];
1607c478bd9Sstevel@tonic-gate 			} else {
1617c478bd9Sstevel@tonic-gate 				if (dn >= eom) {
1627c478bd9Sstevel@tonic-gate 					errno = EMSGSIZE;
1637c478bd9Sstevel@tonic-gate 					return (-1);
1647c478bd9Sstevel@tonic-gate 				}
1657c478bd9Sstevel@tonic-gate 				*dn++ = (char)c;
1667c478bd9Sstevel@tonic-gate 			}
1677c478bd9Sstevel@tonic-gate 		}
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 	if (dn == dst) {
1707c478bd9Sstevel@tonic-gate 		if (dn >= eom) {
1717c478bd9Sstevel@tonic-gate 			errno = EMSGSIZE;
1727c478bd9Sstevel@tonic-gate 			return (-1);
1737c478bd9Sstevel@tonic-gate 		}
1747c478bd9Sstevel@tonic-gate 		*dn++ = '.';
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 	if (dn >= eom) {
1777c478bd9Sstevel@tonic-gate 		errno = EMSGSIZE;
1787c478bd9Sstevel@tonic-gate 		return (-1);
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 	*dn++ = '\0';
1817c478bd9Sstevel@tonic-gate 	return (dn - dst);
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
184*9525b14bSRao Shoaib /*%
185*9525b14bSRao Shoaib  *	Convert a ascii string into an encoded domain name as per RFC1035.
186*9525b14bSRao Shoaib  *
187*9525b14bSRao Shoaib  * return:
188*9525b14bSRao Shoaib  *
189*9525b14bSRao Shoaib  *\li	-1 if it fails
190*9525b14bSRao Shoaib  *\li	1 if string was fully qualified
191*9525b14bSRao Shoaib  *\li	0 is string was not fully qualified
192*9525b14bSRao Shoaib  *
193*9525b14bSRao Shoaib  * notes:
194*9525b14bSRao Shoaib  *\li	Enforces label and domain length limits.
195*9525b14bSRao Shoaib  */
196*9525b14bSRao Shoaib int
ns_name_pton(const char * src,u_char * dst,size_t dstsiz)197*9525b14bSRao Shoaib ns_name_pton(const char *src, u_char *dst, size_t dstsiz) {
198*9525b14bSRao Shoaib 	return (ns_name_pton2(src, dst, dstsiz, NULL));
199*9525b14bSRao Shoaib }
200*9525b14bSRao Shoaib 
2017c478bd9Sstevel@tonic-gate /*
202*9525b14bSRao Shoaib  * ns_name_pton2(src, dst, dstsiz, *dstlen)
2037c478bd9Sstevel@tonic-gate  *	Convert a ascii string into an encoded domain name as per RFC1035.
2047c478bd9Sstevel@tonic-gate  * return:
2057c478bd9Sstevel@tonic-gate  *	-1 if it fails
2067c478bd9Sstevel@tonic-gate  *	1 if string was fully qualified
2077c478bd9Sstevel@tonic-gate  *	0 is string was not fully qualified
208*9525b14bSRao Shoaib  * side effects:
209*9525b14bSRao Shoaib  *	fills in *dstlen (if non-NULL)
2107c478bd9Sstevel@tonic-gate  * notes:
2117c478bd9Sstevel@tonic-gate  *	Enforces label and domain length limits.
2127c478bd9Sstevel@tonic-gate  */
2137c478bd9Sstevel@tonic-gate int
ns_name_pton2(const char * src,u_char * dst,size_t dstsiz,size_t * dstlen)214*9525b14bSRao Shoaib ns_name_pton2(const char *src, u_char *dst, size_t dstsiz, size_t *dstlen) {
2157c478bd9Sstevel@tonic-gate 	u_char *label, *bp, *eom;
2167c478bd9Sstevel@tonic-gate 	int c, n, escaped, e = 0;
2177c478bd9Sstevel@tonic-gate 	char *cp;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	escaped = 0;
2207c478bd9Sstevel@tonic-gate 	bp = dst;
2217c478bd9Sstevel@tonic-gate 	eom = dst + dstsiz;
2227c478bd9Sstevel@tonic-gate 	label = bp++;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	while ((c = *src++) != 0) {
2257c478bd9Sstevel@tonic-gate 		if (escaped) {
226*9525b14bSRao Shoaib 			if (c == '[') { /*%< start a bit string label */
2277c478bd9Sstevel@tonic-gate 				if ((cp = strchr(src, ']')) == NULL) {
228*9525b14bSRao Shoaib 					errno = EINVAL; /*%< ??? */
229*9525b14bSRao Shoaib 					return (-1);
2307c478bd9Sstevel@tonic-gate 				}
231*9525b14bSRao Shoaib 				if ((e = encode_bitsring(&src, cp + 2,
232*9525b14bSRao Shoaib 							 &label, &bp, eom))
2337c478bd9Sstevel@tonic-gate 				    != 0) {
2347c478bd9Sstevel@tonic-gate 					errno = e;
235*9525b14bSRao Shoaib 					return (-1);
2367c478bd9Sstevel@tonic-gate 				}
2377c478bd9Sstevel@tonic-gate 				escaped = 0;
2387c478bd9Sstevel@tonic-gate 				label = bp++;
2397c478bd9Sstevel@tonic-gate 				if ((c = *src++) == 0)
2407c478bd9Sstevel@tonic-gate 					goto done;
2417c478bd9Sstevel@tonic-gate 				else if (c != '.') {
2427c478bd9Sstevel@tonic-gate 					errno = EINVAL;
243*9525b14bSRao Shoaib 					return	(-1);
2447c478bd9Sstevel@tonic-gate 				}
2457c478bd9Sstevel@tonic-gate 				continue;
2467c478bd9Sstevel@tonic-gate 			}
2477c478bd9Sstevel@tonic-gate 			else if ((cp = strchr(digits, c)) != NULL) {
2487c478bd9Sstevel@tonic-gate 				n = (cp - digits) * 100;
2497c478bd9Sstevel@tonic-gate 				if ((c = *src++) == 0 ||
2507c478bd9Sstevel@tonic-gate 				    (cp = strchr(digits, c)) == NULL) {
2517c478bd9Sstevel@tonic-gate 					errno = EMSGSIZE;
2527c478bd9Sstevel@tonic-gate 					return (-1);
2537c478bd9Sstevel@tonic-gate 				}
2547c478bd9Sstevel@tonic-gate 				n += (cp - digits) * 10;
2557c478bd9Sstevel@tonic-gate 				if ((c = *src++) == 0 ||
2567c478bd9Sstevel@tonic-gate 				    (cp = strchr(digits, c)) == NULL) {
2577c478bd9Sstevel@tonic-gate 					errno = EMSGSIZE;
2587c478bd9Sstevel@tonic-gate 					return (-1);
2597c478bd9Sstevel@tonic-gate 				}
2607c478bd9Sstevel@tonic-gate 				n += (cp - digits);
2617c478bd9Sstevel@tonic-gate 				if (n > 255) {
2627c478bd9Sstevel@tonic-gate 					errno = EMSGSIZE;
2637c478bd9Sstevel@tonic-gate 					return (-1);
2647c478bd9Sstevel@tonic-gate 				}
2657c478bd9Sstevel@tonic-gate 				c = n;
2667c478bd9Sstevel@tonic-gate 			}
2677c478bd9Sstevel@tonic-gate 			escaped = 0;
2687c478bd9Sstevel@tonic-gate 		} else if (c == '\\') {
2697c478bd9Sstevel@tonic-gate 			escaped = 1;
2707c478bd9Sstevel@tonic-gate 			continue;
2717c478bd9Sstevel@tonic-gate 		} else if (c == '.') {
2727c478bd9Sstevel@tonic-gate 			c = (bp - label - 1);
273*9525b14bSRao Shoaib 			if ((c & NS_CMPRSFLGS) != 0) {	/*%< Label too big. */
2747c478bd9Sstevel@tonic-gate 				errno = EMSGSIZE;
2757c478bd9Sstevel@tonic-gate 				return (-1);
2767c478bd9Sstevel@tonic-gate 			}
2777c478bd9Sstevel@tonic-gate 			if (label >= eom) {
2787c478bd9Sstevel@tonic-gate 				errno = EMSGSIZE;
2797c478bd9Sstevel@tonic-gate 				return (-1);
2807c478bd9Sstevel@tonic-gate 			}
2817c478bd9Sstevel@tonic-gate 			*label = c;
2827c478bd9Sstevel@tonic-gate 			/* Fully qualified ? */
2837c478bd9Sstevel@tonic-gate 			if (*src == '\0') {
2847c478bd9Sstevel@tonic-gate 				if (c != 0) {
2857c478bd9Sstevel@tonic-gate 					if (bp >= eom) {
2867c478bd9Sstevel@tonic-gate 						errno = EMSGSIZE;
2877c478bd9Sstevel@tonic-gate 						return (-1);
2887c478bd9Sstevel@tonic-gate 					}
2897c478bd9Sstevel@tonic-gate 					*bp++ = '\0';
2907c478bd9Sstevel@tonic-gate 				}
2917c478bd9Sstevel@tonic-gate 				if ((bp - dst) > MAXCDNAME) {
2927c478bd9Sstevel@tonic-gate 					errno = EMSGSIZE;
2937c478bd9Sstevel@tonic-gate 					return (-1);
2947c478bd9Sstevel@tonic-gate 				}
295*9525b14bSRao Shoaib 				if (dstlen != NULL)
296*9525b14bSRao Shoaib 					*dstlen = (bp - dst);
2977c478bd9Sstevel@tonic-gate 				return (1);
2987c478bd9Sstevel@tonic-gate 			}
2997c478bd9Sstevel@tonic-gate 			if (c == 0 || *src == '.') {
3007c478bd9Sstevel@tonic-gate 				errno = EMSGSIZE;
3017c478bd9Sstevel@tonic-gate 				return (-1);
3027c478bd9Sstevel@tonic-gate 			}
3037c478bd9Sstevel@tonic-gate 			label = bp++;
3047c478bd9Sstevel@tonic-gate 			continue;
3057c478bd9Sstevel@tonic-gate 		}
3067c478bd9Sstevel@tonic-gate 		if (bp >= eom) {
3077c478bd9Sstevel@tonic-gate 			errno = EMSGSIZE;
3087c478bd9Sstevel@tonic-gate 			return (-1);
3097c478bd9Sstevel@tonic-gate 		}
3107c478bd9Sstevel@tonic-gate 		*bp++ = (u_char)c;
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 	c = (bp - label - 1);
313*9525b14bSRao Shoaib 	if ((c & NS_CMPRSFLGS) != 0) {		/*%< Label too big. */
3147c478bd9Sstevel@tonic-gate 		errno = EMSGSIZE;
3157c478bd9Sstevel@tonic-gate 		return (-1);
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate   done:
3187c478bd9Sstevel@tonic-gate 	if (label >= eom) {
3197c478bd9Sstevel@tonic-gate 		errno = EMSGSIZE;
3207c478bd9Sstevel@tonic-gate 		return (-1);
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 	*label = c;
3237c478bd9Sstevel@tonic-gate 	if (c != 0) {
3247c478bd9Sstevel@tonic-gate 		if (bp >= eom) {
3257c478bd9Sstevel@tonic-gate 			errno = EMSGSIZE;
3267c478bd9Sstevel@tonic-gate 			return (-1);
3277c478bd9Sstevel@tonic-gate 		}
3287c478bd9Sstevel@tonic-gate 		*bp++ = 0;
3297c478bd9Sstevel@tonic-gate 	}
330*9525b14bSRao Shoaib 	if ((bp - dst) > MAXCDNAME) {	/*%< src too big */
3317c478bd9Sstevel@tonic-gate 		errno = EMSGSIZE;
3327c478bd9Sstevel@tonic-gate 		return (-1);
3337c478bd9Sstevel@tonic-gate 	}
334*9525b14bSRao Shoaib 	if (dstlen != NULL)
335*9525b14bSRao Shoaib 		*dstlen = (bp - dst);
3367c478bd9Sstevel@tonic-gate 	return (0);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate 
339*9525b14bSRao Shoaib /*%
3407c478bd9Sstevel@tonic-gate  *	Convert a network strings labels into all lowercase.
341*9525b14bSRao Shoaib  *
3427c478bd9Sstevel@tonic-gate  * return:
343*9525b14bSRao Shoaib  *\li	Number of bytes written to buffer, or -1 (with errno set)
344*9525b14bSRao Shoaib  *
3457c478bd9Sstevel@tonic-gate  * notes:
346*9525b14bSRao Shoaib  *\li	Enforces label and domain length limits.
3477c478bd9Sstevel@tonic-gate  */
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate int
ns_name_ntol(const u_char * src,u_char * dst,size_t dstsiz)3507c478bd9Sstevel@tonic-gate ns_name_ntol(const u_char *src, u_char *dst, size_t dstsiz)
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate 	const u_char *cp;
3537c478bd9Sstevel@tonic-gate 	u_char *dn, *eom;
3547c478bd9Sstevel@tonic-gate 	u_char c;
3557c478bd9Sstevel@tonic-gate 	u_int n;
3567c478bd9Sstevel@tonic-gate 	int l;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	cp = src;
3597c478bd9Sstevel@tonic-gate 	dn = dst;
3607c478bd9Sstevel@tonic-gate 	eom = dst + dstsiz;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	if (dn >= eom) {
3637c478bd9Sstevel@tonic-gate 		errno = EMSGSIZE;
3647c478bd9Sstevel@tonic-gate 		return (-1);
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 	while ((n = *cp++) != 0) {
3677c478bd9Sstevel@tonic-gate 		if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
3687c478bd9Sstevel@tonic-gate 			/* Some kind of compression pointer. */
3697c478bd9Sstevel@tonic-gate 			errno = EMSGSIZE;
3707c478bd9Sstevel@tonic-gate 			return (-1);
3717c478bd9Sstevel@tonic-gate 		}
3727c478bd9Sstevel@tonic-gate 		*dn++ = n;
3737c478bd9Sstevel@tonic-gate 		if ((l = labellen(cp - 1)) < 0) {
3747c478bd9Sstevel@tonic-gate 			errno = EMSGSIZE;
3757c478bd9Sstevel@tonic-gate 			return (-1);
3767c478bd9Sstevel@tonic-gate 		}
3777c478bd9Sstevel@tonic-gate 		if (dn + l >= eom) {
3787c478bd9Sstevel@tonic-gate 			errno = EMSGSIZE;
3797c478bd9Sstevel@tonic-gate 			return (-1);
3807c478bd9Sstevel@tonic-gate 		}
3817c478bd9Sstevel@tonic-gate 		for ((void)NULL; l > 0; l--) {
3827c478bd9Sstevel@tonic-gate 			c = *cp++;
383*9525b14bSRao Shoaib 			if (isascii(c) && isupper(c))
3847c478bd9Sstevel@tonic-gate 				*dn++ = tolower(c);
3857c478bd9Sstevel@tonic-gate 			else
3867c478bd9Sstevel@tonic-gate 				*dn++ = c;
3877c478bd9Sstevel@tonic-gate 		}
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 	*dn++ = '\0';
3907c478bd9Sstevel@tonic-gate 	return (dn - dst);
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate 
393*9525b14bSRao Shoaib /*%
3947c478bd9Sstevel@tonic-gate  *	Unpack a domain name from a message, source may be compressed.
395*9525b14bSRao Shoaib  *
3967c478bd9Sstevel@tonic-gate  * return:
397*9525b14bSRao Shoaib  *\li	-1 if it fails, or consumed octets if it succeeds.
3987c478bd9Sstevel@tonic-gate  */
3997c478bd9Sstevel@tonic-gate int
ns_name_unpack(const u_char * msg,const u_char * eom,const u_char * src,u_char * dst,size_t dstsiz)4007c478bd9Sstevel@tonic-gate ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
4017c478bd9Sstevel@tonic-gate 	       u_char *dst, size_t dstsiz)
402*9525b14bSRao Shoaib {
403*9525b14bSRao Shoaib 	return (ns_name_unpack2(msg, eom, src, dst, dstsiz, NULL));
404*9525b14bSRao Shoaib }
405*9525b14bSRao Shoaib 
406*9525b14bSRao Shoaib /*
407*9525b14bSRao Shoaib  * ns_name_unpack2(msg, eom, src, dst, dstsiz, *dstlen)
408*9525b14bSRao Shoaib  *	Unpack a domain name from a message, source may be compressed.
409*9525b14bSRao Shoaib  * return:
410*9525b14bSRao Shoaib  *	-1 if it fails, or consumed octets if it succeeds.
411*9525b14bSRao Shoaib  * side effect:
412*9525b14bSRao Shoaib  *	fills in *dstlen (if non-NULL).
413*9525b14bSRao Shoaib  */
414*9525b14bSRao Shoaib int
ns_name_unpack2(const u_char * msg,const u_char * eom,const u_char * src,u_char * dst,size_t dstsiz,size_t * dstlen)415*9525b14bSRao Shoaib ns_name_unpack2(const u_char *msg, const u_char *eom, const u_char *src,
416*9525b14bSRao Shoaib 		u_char *dst, size_t dstsiz, size_t *dstlen)
4177c478bd9Sstevel@tonic-gate {
4187c478bd9Sstevel@tonic-gate 	const u_char *srcp, *dstlim;
4197c478bd9Sstevel@tonic-gate 	u_char *dstp;
4207c478bd9Sstevel@tonic-gate 	int n, len, checked, l;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	len = -1;
4237c478bd9Sstevel@tonic-gate 	checked = 0;
4247c478bd9Sstevel@tonic-gate 	dstp = dst;
4257c478bd9Sstevel@tonic-gate 	srcp = src;
4267c478bd9Sstevel@tonic-gate 	dstlim = dst + dstsiz;
4277c478bd9Sstevel@tonic-gate 	if (srcp < msg || srcp >= eom) {
4287c478bd9Sstevel@tonic-gate 		errno = EMSGSIZE;
4297c478bd9Sstevel@tonic-gate 		return (-1);
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate 	/* Fetch next label in domain name. */
4327c478bd9Sstevel@tonic-gate 	while ((n = *srcp++) != 0) {
4337c478bd9Sstevel@tonic-gate 		/* Check for indirection. */
4347c478bd9Sstevel@tonic-gate 		switch (n & NS_CMPRSFLGS) {
4357c478bd9Sstevel@tonic-gate 		case 0:
4367c478bd9Sstevel@tonic-gate 		case NS_TYPE_ELT:
4377c478bd9Sstevel@tonic-gate 			/* Limit checks. */
4387c478bd9Sstevel@tonic-gate 			if ((l = labellen(srcp - 1)) < 0) {
4397c478bd9Sstevel@tonic-gate 				errno = EMSGSIZE;
440*9525b14bSRao Shoaib 				return (-1);
4417c478bd9Sstevel@tonic-gate 			}
4427c478bd9Sstevel@tonic-gate 			if (dstp + l + 1 >= dstlim || srcp + l >= eom) {
4437c478bd9Sstevel@tonic-gate 				errno = EMSGSIZE;
4447c478bd9Sstevel@tonic-gate 				return (-1);
4457c478bd9Sstevel@tonic-gate 			}
4467c478bd9Sstevel@tonic-gate 			checked += l + 1;
4477c478bd9Sstevel@tonic-gate 			*dstp++ = n;
4487c478bd9Sstevel@tonic-gate 			memcpy(dstp, srcp, l);
4497c478bd9Sstevel@tonic-gate 			dstp += l;
4507c478bd9Sstevel@tonic-gate 			srcp += l;
4517c478bd9Sstevel@tonic-gate 			break;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 		case NS_CMPRSFLGS:
4547c478bd9Sstevel@tonic-gate 			if (srcp >= eom) {
4557c478bd9Sstevel@tonic-gate 				errno = EMSGSIZE;
4567c478bd9Sstevel@tonic-gate 				return (-1);
4577c478bd9Sstevel@tonic-gate 			}
4587c478bd9Sstevel@tonic-gate 			if (len < 0)
4597c478bd9Sstevel@tonic-gate 				len = srcp - src + 1;
4607c478bd9Sstevel@tonic-gate 			srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
461*9525b14bSRao Shoaib 			if (srcp < msg || srcp >= eom) {  /*%< Out of range. */
4627c478bd9Sstevel@tonic-gate 				errno = EMSGSIZE;
4637c478bd9Sstevel@tonic-gate 				return (-1);
4647c478bd9Sstevel@tonic-gate 			}
4657c478bd9Sstevel@tonic-gate 			checked += 2;
4667c478bd9Sstevel@tonic-gate 			/*
4677c478bd9Sstevel@tonic-gate 			 * Check for loops in the compressed name;
4687c478bd9Sstevel@tonic-gate 			 * if we've looked at the whole message,
4697c478bd9Sstevel@tonic-gate 			 * there must be a loop.
4707c478bd9Sstevel@tonic-gate 			 */
4717c478bd9Sstevel@tonic-gate 			if (checked >= eom - msg) {
4727c478bd9Sstevel@tonic-gate 				errno = EMSGSIZE;
4737c478bd9Sstevel@tonic-gate 				return (-1);
4747c478bd9Sstevel@tonic-gate 			}
4757c478bd9Sstevel@tonic-gate 			break;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 		default:
4787c478bd9Sstevel@tonic-gate 			errno = EMSGSIZE;
479*9525b14bSRao Shoaib 			return (-1);			/*%< flag error */
4807c478bd9Sstevel@tonic-gate 		}
4817c478bd9Sstevel@tonic-gate 	}
482*9525b14bSRao Shoaib 	*dstp++ = 0;
483*9525b14bSRao Shoaib 	if (dstlen != NULL)
484*9525b14bSRao Shoaib 		*dstlen = dstp - dst;
4857c478bd9Sstevel@tonic-gate 	if (len < 0)
4867c478bd9Sstevel@tonic-gate 		len = srcp - src;
4877c478bd9Sstevel@tonic-gate 	return (len);
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate 
490*9525b14bSRao Shoaib /*%
4917c478bd9Sstevel@tonic-gate  *	Pack domain name 'domain' into 'comp_dn'.
492*9525b14bSRao Shoaib  *
4937c478bd9Sstevel@tonic-gate  * return:
494*9525b14bSRao Shoaib  *\li	Size of the compressed name, or -1.
495*9525b14bSRao Shoaib  *
4967c478bd9Sstevel@tonic-gate  * notes:
497*9525b14bSRao Shoaib  *\li	'dnptrs' is an array of pointers to previous compressed names.
498*9525b14bSRao Shoaib  *\li	dnptrs[0] is a pointer to the beginning of the message. The array
4997c478bd9Sstevel@tonic-gate  *	ends with NULL.
500*9525b14bSRao Shoaib  *\li	'lastdnptr' is a pointer to the end of the array pointed to
5017c478bd9Sstevel@tonic-gate  *	by 'dnptrs'.
502*9525b14bSRao Shoaib  *
5037c478bd9Sstevel@tonic-gate  * Side effects:
504*9525b14bSRao Shoaib  *\li	The list of pointers in dnptrs is updated for labels inserted into
5057c478bd9Sstevel@tonic-gate  *	the message as we compress the name.  If 'dnptr' is NULL, we don't
5067c478bd9Sstevel@tonic-gate  *	try to compress names. If 'lastdnptr' is NULL, we don't update the
5077c478bd9Sstevel@tonic-gate  *	list.
5087c478bd9Sstevel@tonic-gate  */
5097c478bd9Sstevel@tonic-gate int
ns_name_pack(const u_char * src,u_char * dst,int dstsiz,const u_char ** dnptrs,const u_char ** lastdnptr)5107c478bd9Sstevel@tonic-gate ns_name_pack(const u_char *src, u_char *dst, int dstsiz,
5117c478bd9Sstevel@tonic-gate 	     const u_char **dnptrs, const u_char **lastdnptr)
5127c478bd9Sstevel@tonic-gate {
5137c478bd9Sstevel@tonic-gate 	u_char *dstp;
5147c478bd9Sstevel@tonic-gate 	const u_char **cpp, **lpp, *eob, *msg;
5157c478bd9Sstevel@tonic-gate 	const u_char *srcp;
5167c478bd9Sstevel@tonic-gate 	int n, l, first = 1;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	srcp = src;
5197c478bd9Sstevel@tonic-gate 	dstp = dst;
5207c478bd9Sstevel@tonic-gate 	eob = dstp + dstsiz;
5217c478bd9Sstevel@tonic-gate 	lpp = cpp = NULL;
5227c478bd9Sstevel@tonic-gate 	if (dnptrs != NULL) {
5237c478bd9Sstevel@tonic-gate 		if ((msg = *dnptrs++) != NULL) {
5247c478bd9Sstevel@tonic-gate 			for (cpp = dnptrs; *cpp != NULL; cpp++)
5257c478bd9Sstevel@tonic-gate 				(void)NULL;
526*9525b14bSRao Shoaib 			lpp = cpp;	/*%< end of list to search */
5277c478bd9Sstevel@tonic-gate 		}
5287c478bd9Sstevel@tonic-gate 	} else
5297c478bd9Sstevel@tonic-gate 		msg = NULL;
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	/* make sure the domain we are about to add is legal */
5327c478bd9Sstevel@tonic-gate 	l = 0;
5337c478bd9Sstevel@tonic-gate 	do {
5347c478bd9Sstevel@tonic-gate 		int l0;
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 		n = *srcp;
5377c478bd9Sstevel@tonic-gate 		if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
5387c478bd9Sstevel@tonic-gate 			errno = EMSGSIZE;
5397c478bd9Sstevel@tonic-gate 			return (-1);
5407c478bd9Sstevel@tonic-gate 		}
5417c478bd9Sstevel@tonic-gate 		if ((l0 = labellen(srcp)) < 0) {
5427c478bd9Sstevel@tonic-gate 			errno = EINVAL;
543*9525b14bSRao Shoaib 			return (-1);
5447c478bd9Sstevel@tonic-gate 		}
5457c478bd9Sstevel@tonic-gate 		l += l0 + 1;
5467c478bd9Sstevel@tonic-gate 		if (l > MAXCDNAME) {
5477c478bd9Sstevel@tonic-gate 			errno = EMSGSIZE;
5487c478bd9Sstevel@tonic-gate 			return (-1);
5497c478bd9Sstevel@tonic-gate 		}
5507c478bd9Sstevel@tonic-gate 		srcp += l0 + 1;
5517c478bd9Sstevel@tonic-gate 	} while (n != 0);
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	/* from here on we need to reset compression pointer array on error */
5547c478bd9Sstevel@tonic-gate 	srcp = src;
5557c478bd9Sstevel@tonic-gate 	do {
5567c478bd9Sstevel@tonic-gate 		/* Look to see if we can use pointers. */
5577c478bd9Sstevel@tonic-gate 		n = *srcp;
5587c478bd9Sstevel@tonic-gate 		if (n != 0 && msg != NULL) {
5597c478bd9Sstevel@tonic-gate 			l = dn_find(srcp, msg, (const u_char * const *)dnptrs,
5607c478bd9Sstevel@tonic-gate 				    (const u_char * const *)lpp);
5617c478bd9Sstevel@tonic-gate 			if (l >= 0) {
5627c478bd9Sstevel@tonic-gate 				if (dstp + 1 >= eob) {
5637c478bd9Sstevel@tonic-gate 					goto cleanup;
5647c478bd9Sstevel@tonic-gate 				}
5657c478bd9Sstevel@tonic-gate 				*dstp++ = (l >> 8) | NS_CMPRSFLGS;
5667c478bd9Sstevel@tonic-gate 				*dstp++ = l % 256;
5677c478bd9Sstevel@tonic-gate 				return (dstp - dst);
5687c478bd9Sstevel@tonic-gate 			}
5697c478bd9Sstevel@tonic-gate 			/* Not found, save it. */
5707c478bd9Sstevel@tonic-gate 			if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
5717c478bd9Sstevel@tonic-gate 			    (dstp - msg) < 0x4000 && first) {
5727c478bd9Sstevel@tonic-gate 				*cpp++ = dstp;
5737c478bd9Sstevel@tonic-gate 				*cpp = NULL;
5747c478bd9Sstevel@tonic-gate 				first = 0;
5757c478bd9Sstevel@tonic-gate 			}
5767c478bd9Sstevel@tonic-gate 		}
5777c478bd9Sstevel@tonic-gate 		/* copy label to buffer */
5787c478bd9Sstevel@tonic-gate 		if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
5797c478bd9Sstevel@tonic-gate 			/* Should not happen. */
5807c478bd9Sstevel@tonic-gate 			goto cleanup;
5817c478bd9Sstevel@tonic-gate 		}
5827c478bd9Sstevel@tonic-gate 		n = labellen(srcp);
5837c478bd9Sstevel@tonic-gate 		if (dstp + 1 + n >= eob) {
5847c478bd9Sstevel@tonic-gate 			goto cleanup;
5857c478bd9Sstevel@tonic-gate 		}
5867c478bd9Sstevel@tonic-gate 		memcpy(dstp, srcp, n + 1);
5877c478bd9Sstevel@tonic-gate 		srcp += n + 1;
5887c478bd9Sstevel@tonic-gate 		dstp += n + 1;
5897c478bd9Sstevel@tonic-gate 	} while (n != 0);
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	if (dstp > eob) {
5927c478bd9Sstevel@tonic-gate cleanup:
5937c478bd9Sstevel@tonic-gate 		if (msg != NULL)
5947c478bd9Sstevel@tonic-gate 			*lpp = NULL;
5957c478bd9Sstevel@tonic-gate 		errno = EMSGSIZE;
5967c478bd9Sstevel@tonic-gate 		return (-1);
5977c478bd9Sstevel@tonic-gate 	}
5987c478bd9Sstevel@tonic-gate 	return (dstp - dst);
5997c478bd9Sstevel@tonic-gate }
6007c478bd9Sstevel@tonic-gate 
601*9525b14bSRao Shoaib /*%
6027c478bd9Sstevel@tonic-gate  *	Expand compressed domain name to presentation format.
603*9525b14bSRao Shoaib  *
6047c478bd9Sstevel@tonic-gate  * return:
605*9525b14bSRao Shoaib  *\li	Number of bytes read out of `src', or -1 (with errno set).
606*9525b14bSRao Shoaib  *
6077c478bd9Sstevel@tonic-gate  * note:
608*9525b14bSRao Shoaib  *\li	Root domain returns as "." not "".
6097c478bd9Sstevel@tonic-gate  */
6107c478bd9Sstevel@tonic-gate int
ns_name_uncompress(const u_char * msg,const u_char * eom,const u_char * src,char * dst,size_t dstsiz)6117c478bd9Sstevel@tonic-gate ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src,
6127c478bd9Sstevel@tonic-gate 		   char *dst, size_t dstsiz)
6137c478bd9Sstevel@tonic-gate {
6147c478bd9Sstevel@tonic-gate 	u_char tmp[NS_MAXCDNAME];
6157c478bd9Sstevel@tonic-gate 	int n;
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
6187c478bd9Sstevel@tonic-gate 		return (-1);
6197c478bd9Sstevel@tonic-gate 	if (ns_name_ntop(tmp, dst, dstsiz) == -1)
6207c478bd9Sstevel@tonic-gate 		return (-1);
6217c478bd9Sstevel@tonic-gate 	return (n);
6227c478bd9Sstevel@tonic-gate }
6237c478bd9Sstevel@tonic-gate 
624*9525b14bSRao Shoaib /*%
6257c478bd9Sstevel@tonic-gate  *	Compress a domain name into wire format, using compression pointers.
626*9525b14bSRao Shoaib  *
6277c478bd9Sstevel@tonic-gate  * return:
628*9525b14bSRao Shoaib  *\li	Number of bytes consumed in `dst' or -1 (with errno set).
629*9525b14bSRao Shoaib  *
6307c478bd9Sstevel@tonic-gate  * notes:
631*9525b14bSRao Shoaib  *\li	'dnptrs' is an array of pointers to previous compressed names.
632*9525b14bSRao Shoaib  *\li	dnptrs[0] is a pointer to the beginning of the message.
633*9525b14bSRao Shoaib  *\li	The list ends with NULL.  'lastdnptr' is a pointer to the end of the
6347c478bd9Sstevel@tonic-gate  *	array pointed to by 'dnptrs'. Side effect is to update the list of
6357c478bd9Sstevel@tonic-gate  *	pointers for labels inserted into the message as we compress the name.
636*9525b14bSRao Shoaib  *\li	If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
6377c478bd9Sstevel@tonic-gate  *	is NULL, we don't update the list.
6387c478bd9Sstevel@tonic-gate  */
6397c478bd9Sstevel@tonic-gate int
ns_name_compress(const char * src,u_char * dst,size_t dstsiz,const u_char ** dnptrs,const u_char ** lastdnptr)6407c478bd9Sstevel@tonic-gate ns_name_compress(const char *src, u_char *dst, size_t dstsiz,
6417c478bd9Sstevel@tonic-gate 		 const u_char **dnptrs, const u_char **lastdnptr)
6427c478bd9Sstevel@tonic-gate {
6437c478bd9Sstevel@tonic-gate 	u_char tmp[NS_MAXCDNAME];
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	if (ns_name_pton(src, tmp, sizeof tmp) == -1)
6467c478bd9Sstevel@tonic-gate 		return (-1);
6477c478bd9Sstevel@tonic-gate 	return (ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr));
6487c478bd9Sstevel@tonic-gate }
6497c478bd9Sstevel@tonic-gate 
650*9525b14bSRao Shoaib /*%
6517c478bd9Sstevel@tonic-gate  * Reset dnptrs so that there are no active references to pointers at or
6527c478bd9Sstevel@tonic-gate  * after src.
6537c478bd9Sstevel@tonic-gate  */
6547c478bd9Sstevel@tonic-gate void
ns_name_rollback(const u_char * src,const u_char ** dnptrs,const u_char ** lastdnptr)6557c478bd9Sstevel@tonic-gate ns_name_rollback(const u_char *src, const u_char **dnptrs,
6567c478bd9Sstevel@tonic-gate 		 const u_char **lastdnptr)
6577c478bd9Sstevel@tonic-gate {
6587c478bd9Sstevel@tonic-gate 	while (dnptrs < lastdnptr && *dnptrs != NULL) {
6597c478bd9Sstevel@tonic-gate 		if (*dnptrs >= src) {
6607c478bd9Sstevel@tonic-gate 			*dnptrs = NULL;
6617c478bd9Sstevel@tonic-gate 			break;
6627c478bd9Sstevel@tonic-gate 		}
6637c478bd9Sstevel@tonic-gate 		dnptrs++;
6647c478bd9Sstevel@tonic-gate 	}
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate 
667*9525b14bSRao Shoaib /*%
6687c478bd9Sstevel@tonic-gate  *	Advance *ptrptr to skip over the compressed name it points at.
669*9525b14bSRao Shoaib  *
6707c478bd9Sstevel@tonic-gate  * return:
671*9525b14bSRao Shoaib  *\li	0 on success, -1 (with errno set) on failure.
6727c478bd9Sstevel@tonic-gate  */
6737c478bd9Sstevel@tonic-gate int
ns_name_skip(const u_char ** ptrptr,const u_char * eom)6747c478bd9Sstevel@tonic-gate ns_name_skip(const u_char **ptrptr, const u_char *eom)
6757c478bd9Sstevel@tonic-gate {
6767c478bd9Sstevel@tonic-gate 	const u_char *cp;
6777c478bd9Sstevel@tonic-gate 	u_int n;
6787c478bd9Sstevel@tonic-gate 	int l;
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	cp = *ptrptr;
6817c478bd9Sstevel@tonic-gate 	while (cp < eom && (n = *cp++) != 0) {
6827c478bd9Sstevel@tonic-gate 		/* Check for indirection. */
6837c478bd9Sstevel@tonic-gate 		switch (n & NS_CMPRSFLGS) {
684*9525b14bSRao Shoaib 		case 0:			/*%< normal case, n == len */
6857c478bd9Sstevel@tonic-gate 			cp += n;
6867c478bd9Sstevel@tonic-gate 			continue;
687*9525b14bSRao Shoaib 		case NS_TYPE_ELT: /*%< EDNS0 extended label */
6887c478bd9Sstevel@tonic-gate 			if ((l = labellen(cp - 1)) < 0) {
689*9525b14bSRao Shoaib 				errno = EMSGSIZE; /*%< XXX */
690*9525b14bSRao Shoaib 				return (-1);
6917c478bd9Sstevel@tonic-gate 			}
6927c478bd9Sstevel@tonic-gate 			cp += l;
6937c478bd9Sstevel@tonic-gate 			continue;
694*9525b14bSRao Shoaib 		case NS_CMPRSFLGS:	/*%< indirection */
6957c478bd9Sstevel@tonic-gate 			cp++;
6967c478bd9Sstevel@tonic-gate 			break;
697*9525b14bSRao Shoaib 		default:		/*%< illegal type */
6987c478bd9Sstevel@tonic-gate 			errno = EMSGSIZE;
6997c478bd9Sstevel@tonic-gate 			return (-1);
7007c478bd9Sstevel@tonic-gate 		}
7017c478bd9Sstevel@tonic-gate 		break;
7027c478bd9Sstevel@tonic-gate 	}
7037c478bd9Sstevel@tonic-gate 	if (cp > eom) {
7047c478bd9Sstevel@tonic-gate 		errno = EMSGSIZE;
7057c478bd9Sstevel@tonic-gate 		return (-1);
7067c478bd9Sstevel@tonic-gate 	}
7077c478bd9Sstevel@tonic-gate 	*ptrptr = cp;
7087c478bd9Sstevel@tonic-gate 	return (0);
7097c478bd9Sstevel@tonic-gate }
7107c478bd9Sstevel@tonic-gate 
711*9525b14bSRao Shoaib /* Find the number of octets an nname takes up, including the root label.
712*9525b14bSRao Shoaib  * (This is basically ns_name_skip() without compression-pointer support.)
713*9525b14bSRao Shoaib  * ((NOTE: can only return zero if passed-in namesiz argument is zero.))
714*9525b14bSRao Shoaib  */
715*9525b14bSRao Shoaib ssize_t
ns_name_length(ns_nname_ct nname,size_t namesiz)716*9525b14bSRao Shoaib ns_name_length(ns_nname_ct nname, size_t namesiz) {
717*9525b14bSRao Shoaib 	ns_nname_ct orig = nname;
718*9525b14bSRao Shoaib 	u_int n;
719*9525b14bSRao Shoaib 
720*9525b14bSRao Shoaib 	while (namesiz-- > 0 && (n = *nname++) != 0) {
721*9525b14bSRao Shoaib 		if ((n & NS_CMPRSFLGS) != 0) {
722*9525b14bSRao Shoaib 			errno = EISDIR;
723*9525b14bSRao Shoaib 			return (-1);
724*9525b14bSRao Shoaib 		}
725*9525b14bSRao Shoaib 		if (n > namesiz) {
726*9525b14bSRao Shoaib 			errno = EMSGSIZE;
727*9525b14bSRao Shoaib 			return (-1);
728*9525b14bSRao Shoaib 		}
729*9525b14bSRao Shoaib 		nname += n;
730*9525b14bSRao Shoaib 		namesiz -= n;
731*9525b14bSRao Shoaib 	}
732*9525b14bSRao Shoaib 	return (nname - orig);
733*9525b14bSRao Shoaib }
734*9525b14bSRao Shoaib 
735*9525b14bSRao Shoaib /* Compare two nname's for equality.  Return -1 on error (setting errno).
736*9525b14bSRao Shoaib  */
737*9525b14bSRao Shoaib int
ns_name_eq(ns_nname_ct a,size_t as,ns_nname_ct b,size_t bs)738*9525b14bSRao Shoaib ns_name_eq(ns_nname_ct a, size_t as, ns_nname_ct b, size_t bs) {
739*9525b14bSRao Shoaib 	ns_nname_ct ae = a + as, be = b + bs;
740*9525b14bSRao Shoaib 	int ac, bc;
741*9525b14bSRao Shoaib 
742*9525b14bSRao Shoaib 	while (ac = *a, bc = *b, ac != 0 && bc != 0) {
743*9525b14bSRao Shoaib 		if ((ac & NS_CMPRSFLGS) != 0 || (bc & NS_CMPRSFLGS) != 0) {
744*9525b14bSRao Shoaib 			errno = EISDIR;
745*9525b14bSRao Shoaib 			return (-1);
746*9525b14bSRao Shoaib 		}
747*9525b14bSRao Shoaib 		if (a + ac >= ae || b + bc >= be) {
748*9525b14bSRao Shoaib 			errno = EMSGSIZE;
749*9525b14bSRao Shoaib 			return (-1);
750*9525b14bSRao Shoaib 		}
751*9525b14bSRao Shoaib 		if (ac != bc || strncasecmp((const char *) ++a,
752*9525b14bSRao Shoaib 					    (const char *) ++b, ac) != 0)
753*9525b14bSRao Shoaib 			return (0);
754*9525b14bSRao Shoaib 		a += ac, b += bc;
755*9525b14bSRao Shoaib 	}
756*9525b14bSRao Shoaib 	return (ac == 0 && bc == 0);
757*9525b14bSRao Shoaib }
758*9525b14bSRao Shoaib 
759*9525b14bSRao Shoaib /* Is domain "A" owned by (at or below) domain "B"?
760*9525b14bSRao Shoaib  */
761*9525b14bSRao Shoaib int
ns_name_owned(ns_namemap_ct a,int an,ns_namemap_ct b,int bn)762*9525b14bSRao Shoaib ns_name_owned(ns_namemap_ct a, int an, ns_namemap_ct b, int bn) {
763*9525b14bSRao Shoaib 	/* If A is shorter, it cannot be owned by B. */
764*9525b14bSRao Shoaib 	if (an < bn)
765*9525b14bSRao Shoaib 		return (0);
766*9525b14bSRao Shoaib 
767*9525b14bSRao Shoaib 	/* If they are unequal before the length of the shorter, A cannot... */
768*9525b14bSRao Shoaib 	while (bn > 0) {
769*9525b14bSRao Shoaib 		if (a->len != b->len ||
770*9525b14bSRao Shoaib 		    strncasecmp((const char *) a->base,
771*9525b14bSRao Shoaib 				(const char *) b->base, a->len) != 0)
772*9525b14bSRao Shoaib 			return (0);
773*9525b14bSRao Shoaib 		a++, an--;
774*9525b14bSRao Shoaib 		b++, bn--;
775*9525b14bSRao Shoaib 	}
776*9525b14bSRao Shoaib 
777*9525b14bSRao Shoaib 	/* A might be longer or not, but either way, B owns it. */
778*9525b14bSRao Shoaib 	return (1);
779*9525b14bSRao Shoaib }
780*9525b14bSRao Shoaib 
781*9525b14bSRao Shoaib /* Build an array of <base,len> tuples from an nname, top-down order.
782*9525b14bSRao Shoaib  * Return the number of tuples (labels) thus discovered.
783*9525b14bSRao Shoaib  */
784*9525b14bSRao Shoaib int
ns_name_map(ns_nname_ct nname,size_t namelen,ns_namemap_t map,int mapsize)785*9525b14bSRao Shoaib ns_name_map(ns_nname_ct nname, size_t namelen, ns_namemap_t map, int mapsize) {
786*9525b14bSRao Shoaib 	u_int n;
787*9525b14bSRao Shoaib 	int l;
788*9525b14bSRao Shoaib 
789*9525b14bSRao Shoaib 	n = *nname++;
790*9525b14bSRao Shoaib 	namelen--;
791*9525b14bSRao Shoaib 
792*9525b14bSRao Shoaib 	/* Root zone? */
793*9525b14bSRao Shoaib 	if (n == 0) {
794*9525b14bSRao Shoaib 		/* Extra data follows name? */
795*9525b14bSRao Shoaib 		if (namelen > 0) {
796*9525b14bSRao Shoaib 			errno = EMSGSIZE;
797*9525b14bSRao Shoaib 			return (-1);
798*9525b14bSRao Shoaib 		}
799*9525b14bSRao Shoaib 		return (0);
800*9525b14bSRao Shoaib 	}
801*9525b14bSRao Shoaib 
802*9525b14bSRao Shoaib 	/* Compression pointer? */
803*9525b14bSRao Shoaib 	if ((n & NS_CMPRSFLGS) != 0) {
804*9525b14bSRao Shoaib 		errno = EISDIR;
805*9525b14bSRao Shoaib 		return (-1);
806*9525b14bSRao Shoaib 	}
807*9525b14bSRao Shoaib 
808*9525b14bSRao Shoaib 	/* Label too long? */
809*9525b14bSRao Shoaib 	if (n > namelen) {
810*9525b14bSRao Shoaib 		errno = EMSGSIZE;
811*9525b14bSRao Shoaib 		return (-1);
812*9525b14bSRao Shoaib 	}
813*9525b14bSRao Shoaib 
814*9525b14bSRao Shoaib 	/* Recurse to get rest of name done first. */
815*9525b14bSRao Shoaib 	l = ns_name_map(nname + n, namelen - n, map, mapsize);
816*9525b14bSRao Shoaib 	if (l < 0)
817*9525b14bSRao Shoaib 		return (-1);
818*9525b14bSRao Shoaib 
819*9525b14bSRao Shoaib 	/* Too many labels? */
820*9525b14bSRao Shoaib 	if (l >= mapsize) {
821*9525b14bSRao Shoaib 		errno = ENAMETOOLONG;
822*9525b14bSRao Shoaib 		return (-1);
823*9525b14bSRao Shoaib 	}
824*9525b14bSRao Shoaib 
825*9525b14bSRao Shoaib 	/* We're on our way back up-stack, store current map data. */
826*9525b14bSRao Shoaib 	map[l].base = nname;
827*9525b14bSRao Shoaib 	map[l].len = n;
828*9525b14bSRao Shoaib 	return (l + 1);
829*9525b14bSRao Shoaib }
830*9525b14bSRao Shoaib 
831*9525b14bSRao Shoaib /* Count the labels in a domain name.  Root counts, so COM. has two.  This
832*9525b14bSRao Shoaib  * is to make the result comparable to the result of ns_name_map().
833*9525b14bSRao Shoaib  */
834*9525b14bSRao Shoaib int
ns_name_labels(ns_nname_ct nname,size_t namesiz)835*9525b14bSRao Shoaib ns_name_labels(ns_nname_ct nname, size_t namesiz) {
836*9525b14bSRao Shoaib 	int ret = 0;
837*9525b14bSRao Shoaib 	u_int n;
838*9525b14bSRao Shoaib 
839*9525b14bSRao Shoaib 	while (namesiz-- > 0 && (n = *nname++) != 0) {
840*9525b14bSRao Shoaib 		if ((n & NS_CMPRSFLGS) != 0) {
841*9525b14bSRao Shoaib 			errno = EISDIR;
842*9525b14bSRao Shoaib 			return (-1);
843*9525b14bSRao Shoaib 		}
844*9525b14bSRao Shoaib 		if (n > namesiz) {
845*9525b14bSRao Shoaib 			errno = EMSGSIZE;
846*9525b14bSRao Shoaib 			return (-1);
847*9525b14bSRao Shoaib 		}
848*9525b14bSRao Shoaib 		nname += n;
849*9525b14bSRao Shoaib 		namesiz -= n;
850*9525b14bSRao Shoaib 		ret++;
851*9525b14bSRao Shoaib 	}
852*9525b14bSRao Shoaib 	return (ret + 1);
853*9525b14bSRao Shoaib }
854*9525b14bSRao Shoaib 
8557c478bd9Sstevel@tonic-gate /* Private. */
8567c478bd9Sstevel@tonic-gate 
857*9525b14bSRao Shoaib /*%
8587c478bd9Sstevel@tonic-gate  *	Thinking in noninternationalized USASCII (per the DNS spec),
8597c478bd9Sstevel@tonic-gate  *	is this characted special ("in need of quoting") ?
860*9525b14bSRao Shoaib  *
8617c478bd9Sstevel@tonic-gate  * return:
862*9525b14bSRao Shoaib  *\li	boolean.
8637c478bd9Sstevel@tonic-gate  */
8647c478bd9Sstevel@tonic-gate static int
special(int ch)8657c478bd9Sstevel@tonic-gate special(int ch) {
8667c478bd9Sstevel@tonic-gate 	switch (ch) {
867*9525b14bSRao Shoaib 	case 0x22: /*%< '"' */
868*9525b14bSRao Shoaib 	case 0x2E: /*%< '.' */
869*9525b14bSRao Shoaib 	case 0x3B: /*%< ';' */
870*9525b14bSRao Shoaib 	case 0x5C: /*%< '\\' */
871*9525b14bSRao Shoaib 	case 0x28: /*%< '(' */
872*9525b14bSRao Shoaib 	case 0x29: /*%< ')' */
8737c478bd9Sstevel@tonic-gate 	/* Special modifiers in zone files. */
874*9525b14bSRao Shoaib 	case 0x40: /*%< '@' */
875*9525b14bSRao Shoaib 	case 0x24: /*%< '$' */
8767c478bd9Sstevel@tonic-gate 		return (1);
8777c478bd9Sstevel@tonic-gate 	default:
8787c478bd9Sstevel@tonic-gate 		return (0);
8797c478bd9Sstevel@tonic-gate 	}
8807c478bd9Sstevel@tonic-gate }
8817c478bd9Sstevel@tonic-gate 
882*9525b14bSRao Shoaib /*%
8837c478bd9Sstevel@tonic-gate  *	Thinking in noninternationalized USASCII (per the DNS spec),
8847c478bd9Sstevel@tonic-gate  *	is this character visible and not a space when printed ?
885*9525b14bSRao Shoaib  *
8867c478bd9Sstevel@tonic-gate  * return:
887*9525b14bSRao Shoaib  *\li	boolean.
8887c478bd9Sstevel@tonic-gate  */
8897c478bd9Sstevel@tonic-gate static int
printable(int ch)8907c478bd9Sstevel@tonic-gate printable(int ch) {
8917c478bd9Sstevel@tonic-gate 	return (ch > 0x20 && ch < 0x7f);
8927c478bd9Sstevel@tonic-gate }
8937c478bd9Sstevel@tonic-gate 
894*9525b14bSRao Shoaib /*%
8957c478bd9Sstevel@tonic-gate  *	Thinking in noninternationalized USASCII (per the DNS spec),
8967c478bd9Sstevel@tonic-gate  *	convert this character to lower case if it's upper case.
8977c478bd9Sstevel@tonic-gate  */
8987c478bd9Sstevel@tonic-gate static int
mklower(int ch)8997c478bd9Sstevel@tonic-gate mklower(int ch) {
9007c478bd9Sstevel@tonic-gate 	if (ch >= 0x41 && ch <= 0x5A)
9017c478bd9Sstevel@tonic-gate 		return (ch + 0x20);
9027c478bd9Sstevel@tonic-gate 	return (ch);
9037c478bd9Sstevel@tonic-gate }
9047c478bd9Sstevel@tonic-gate 
905*9525b14bSRao Shoaib /*%
9067c478bd9Sstevel@tonic-gate  *	Search for the counted-label name in an array of compressed names.
907*9525b14bSRao Shoaib  *
9087c478bd9Sstevel@tonic-gate  * return:
909*9525b14bSRao Shoaib  *\li	offset from msg if found, or -1.
910*9525b14bSRao Shoaib  *
9117c478bd9Sstevel@tonic-gate  * notes:
912*9525b14bSRao Shoaib  *\li	dnptrs is the pointer to the first name on the list,
913*9525b14bSRao Shoaib  *\li	not the pointer to the start of the message.
9147c478bd9Sstevel@tonic-gate  */
9157c478bd9Sstevel@tonic-gate static int
dn_find(const u_char * domain,const u_char * msg,const u_char * const * dnptrs,const u_char * const * lastdnptr)9167c478bd9Sstevel@tonic-gate dn_find(const u_char *domain, const u_char *msg,
9177c478bd9Sstevel@tonic-gate 	const u_char * const *dnptrs,
9187c478bd9Sstevel@tonic-gate 	const u_char * const *lastdnptr)
9197c478bd9Sstevel@tonic-gate {
9207c478bd9Sstevel@tonic-gate 	const u_char *dn, *cp, *sp;
9217c478bd9Sstevel@tonic-gate 	const u_char * const *cpp;
9227c478bd9Sstevel@tonic-gate 	u_int n;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
9257c478bd9Sstevel@tonic-gate 		sp = *cpp;
9267c478bd9Sstevel@tonic-gate 		/*
9277c478bd9Sstevel@tonic-gate 		 * terminate search on:
9287c478bd9Sstevel@tonic-gate 		 * root label
9297c478bd9Sstevel@tonic-gate 		 * compression pointer
9307c478bd9Sstevel@tonic-gate 		 * unusable offset
9317c478bd9Sstevel@tonic-gate 		 */
9327c478bd9Sstevel@tonic-gate 		while (*sp != 0 && (*sp & NS_CMPRSFLGS) == 0 &&
9337c478bd9Sstevel@tonic-gate 		       (sp - msg) < 0x4000) {
9347c478bd9Sstevel@tonic-gate 			dn = domain;
9357c478bd9Sstevel@tonic-gate 			cp = sp;
9367c478bd9Sstevel@tonic-gate 			while ((n = *cp++) != 0) {
9377c478bd9Sstevel@tonic-gate 				/*
9387c478bd9Sstevel@tonic-gate 				 * check for indirection
9397c478bd9Sstevel@tonic-gate 				 */
9407c478bd9Sstevel@tonic-gate 				switch (n & NS_CMPRSFLGS) {
941*9525b14bSRao Shoaib 				case 0:		/*%< normal case, n == len */
942*9525b14bSRao Shoaib 					n = labellen(cp - 1); /*%< XXX */
9437c478bd9Sstevel@tonic-gate 					if (n != *dn++)
9447c478bd9Sstevel@tonic-gate 						goto next;
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 					for ((void)NULL; n > 0; n--)
9477c478bd9Sstevel@tonic-gate 						if (mklower(*dn++) !=
9487c478bd9Sstevel@tonic-gate 						    mklower(*cp++))
9497c478bd9Sstevel@tonic-gate 							goto next;
9507c478bd9Sstevel@tonic-gate 					/* Is next root for both ? */
9517c478bd9Sstevel@tonic-gate 					if (*dn == '\0' && *cp == '\0')
9527c478bd9Sstevel@tonic-gate 						return (sp - msg);
9537c478bd9Sstevel@tonic-gate 					if (*dn)
9547c478bd9Sstevel@tonic-gate 						continue;
9557c478bd9Sstevel@tonic-gate 					goto next;
956*9525b14bSRao Shoaib 				case NS_CMPRSFLGS:	/*%< indirection */
9577c478bd9Sstevel@tonic-gate 					cp = msg + (((n & 0x3f) << 8) | *cp);
9587c478bd9Sstevel@tonic-gate 					break;
9597c478bd9Sstevel@tonic-gate 
960*9525b14bSRao Shoaib 				default:	/*%< illegal type */
9617c478bd9Sstevel@tonic-gate 					errno = EMSGSIZE;
9627c478bd9Sstevel@tonic-gate 					return (-1);
9637c478bd9Sstevel@tonic-gate 				}
9647c478bd9Sstevel@tonic-gate 			}
9657c478bd9Sstevel@tonic-gate  next: ;
9667c478bd9Sstevel@tonic-gate 			sp += *sp + 1;
9677c478bd9Sstevel@tonic-gate 		}
9687c478bd9Sstevel@tonic-gate 	}
9697c478bd9Sstevel@tonic-gate 	errno = ENOENT;
9707c478bd9Sstevel@tonic-gate 	return (-1);
9717c478bd9Sstevel@tonic-gate }
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate static int
decode_bitstring(const unsigned char ** cpp,char * dn,const char * eom)974*9525b14bSRao Shoaib decode_bitstring(const unsigned char **cpp, char *dn, const char *eom)
9757c478bd9Sstevel@tonic-gate {
976*9525b14bSRao Shoaib 	const unsigned char *cp = *cpp;
9777c478bd9Sstevel@tonic-gate 	char *beg = dn, tc;
9787c478bd9Sstevel@tonic-gate 	int b, blen, plen, i;
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	if ((blen = (*cp & 0xff)) == 0)
9817c478bd9Sstevel@tonic-gate 		blen = 256;
9827c478bd9Sstevel@tonic-gate 	plen = (blen + 3) / 4;
9837c478bd9Sstevel@tonic-gate 	plen += sizeof("\\[x/]") + (blen > 99 ? 3 : (blen > 9) ? 2 : 1);
9847c478bd9Sstevel@tonic-gate 	if (dn + plen >= eom)
985*9525b14bSRao Shoaib 		return (-1);
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 	cp++;
9887c478bd9Sstevel@tonic-gate 	i = SPRINTF((dn, "\\[x"));
9897c478bd9Sstevel@tonic-gate 	if (i < 0)
9907c478bd9Sstevel@tonic-gate 		return (-1);
9917c478bd9Sstevel@tonic-gate 	dn += i;
9927c478bd9Sstevel@tonic-gate 	for (b = blen; b > 7; b -= 8, cp++) {
9937c478bd9Sstevel@tonic-gate 		i = SPRINTF((dn, "%02x", *cp & 0xff));
9947c478bd9Sstevel@tonic-gate 		if (i < 0)
9957c478bd9Sstevel@tonic-gate 			return (-1);
9967c478bd9Sstevel@tonic-gate 		dn += i;
9977c478bd9Sstevel@tonic-gate 	}
9987c478bd9Sstevel@tonic-gate 	if (b > 4) {
9997c478bd9Sstevel@tonic-gate 		tc = *cp++;
10007c478bd9Sstevel@tonic-gate 		i = SPRINTF((dn, "%02x", tc & (0xff << (8 - b))));
10017c478bd9Sstevel@tonic-gate 		if (i < 0)
10027c478bd9Sstevel@tonic-gate 			return (-1);
10037c478bd9Sstevel@tonic-gate 		dn += i;
10047c478bd9Sstevel@tonic-gate 	} else if (b > 0) {
10057c478bd9Sstevel@tonic-gate 		tc = *cp++;
10067c478bd9Sstevel@tonic-gate 		i = SPRINTF((dn, "%1x",
10077c478bd9Sstevel@tonic-gate 			       ((tc >> 4) & 0x0f) & (0x0f << (4 - b))));
10087c478bd9Sstevel@tonic-gate 		if (i < 0)
10097c478bd9Sstevel@tonic-gate 			return (-1);
10107c478bd9Sstevel@tonic-gate 		dn += i;
10117c478bd9Sstevel@tonic-gate 	}
10127c478bd9Sstevel@tonic-gate 	i = SPRINTF((dn, "/%d]", blen));
10137c478bd9Sstevel@tonic-gate 	if (i < 0)
10147c478bd9Sstevel@tonic-gate 		return (-1);
10157c478bd9Sstevel@tonic-gate 	dn += i;
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	*cpp = cp;
1018*9525b14bSRao Shoaib 	return (dn - beg);
10197c478bd9Sstevel@tonic-gate }
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate static int
encode_bitsring(const char ** bp,const char * end,unsigned char ** labelp,unsigned char ** dst,unsigned const char * eom)1022*9525b14bSRao Shoaib encode_bitsring(const char **bp, const char *end, unsigned char **labelp,
1023*9525b14bSRao Shoaib 		unsigned char ** dst, unsigned const char *eom)
10247c478bd9Sstevel@tonic-gate {
10257c478bd9Sstevel@tonic-gate 	int afterslash = 0;
10267c478bd9Sstevel@tonic-gate 	const char *cp = *bp;
1027*9525b14bSRao Shoaib 	unsigned char *tp;
1028*9525b14bSRao Shoaib 	char c;
10297c478bd9Sstevel@tonic-gate 	const char *beg_blen;
10307c478bd9Sstevel@tonic-gate 	char *end_blen = NULL;
10317c478bd9Sstevel@tonic-gate 	int value = 0, count = 0, tbcount = 0, blen = 0;
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	beg_blen = end_blen = NULL;
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	/* a bitstring must contain at least 2 characters */
10367c478bd9Sstevel@tonic-gate 	if (end - cp < 2)
1037*9525b14bSRao Shoaib 		return (EINVAL);
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	/* XXX: currently, only hex strings are supported */
10407c478bd9Sstevel@tonic-gate 	if (*cp++ != 'x')
1041*9525b14bSRao Shoaib 		return (EINVAL);
1042*9525b14bSRao Shoaib 	if (!isxdigit((*cp) & 0xff)) /*%< reject '\[x/BLEN]' */
1043*9525b14bSRao Shoaib 		return (EINVAL);
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	for (tp = *dst + 1; cp < end && tp < eom; cp++) {
10467c478bd9Sstevel@tonic-gate 		switch((c = *cp)) {
1047*9525b14bSRao Shoaib 		case ']':	/*%< end of the bitstring */
10487c478bd9Sstevel@tonic-gate 			if (afterslash) {
10497c478bd9Sstevel@tonic-gate 				if (beg_blen == NULL)
1050*9525b14bSRao Shoaib 					return (EINVAL);
10517c478bd9Sstevel@tonic-gate 				blen = (int)strtol(beg_blen, &end_blen, 10);
10527c478bd9Sstevel@tonic-gate 				if (*end_blen != ']')
1053*9525b14bSRao Shoaib 					return (EINVAL);
10547c478bd9Sstevel@tonic-gate 			}
10557c478bd9Sstevel@tonic-gate 			if (count)
10567c478bd9Sstevel@tonic-gate 				*tp++ = ((value << 4) & 0xff);
1057*9525b14bSRao Shoaib 			cp++;	/*%< skip ']' */
10587c478bd9Sstevel@tonic-gate 			goto done;
10597c478bd9Sstevel@tonic-gate 		case '/':
10607c478bd9Sstevel@tonic-gate 			afterslash = 1;
10617c478bd9Sstevel@tonic-gate 			break;
10627c478bd9Sstevel@tonic-gate 		default:
10637c478bd9Sstevel@tonic-gate 			if (afterslash) {
10647c478bd9Sstevel@tonic-gate 				if (!isdigit(c&0xff))
1065*9525b14bSRao Shoaib 					return (EINVAL);
10667c478bd9Sstevel@tonic-gate 				if (beg_blen == NULL) {
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 					if (c == '0') {
10697c478bd9Sstevel@tonic-gate 						/* blen never begings with 0 */
1070*9525b14bSRao Shoaib 						return (EINVAL);
10717c478bd9Sstevel@tonic-gate 					}
10727c478bd9Sstevel@tonic-gate 					beg_blen = cp;
10737c478bd9Sstevel@tonic-gate 				}
10747c478bd9Sstevel@tonic-gate 			} else {
10757c478bd9Sstevel@tonic-gate 				if (!isxdigit(c&0xff))
1076*9525b14bSRao Shoaib 					return (EINVAL);
10777c478bd9Sstevel@tonic-gate 				value <<= 4;
10787c478bd9Sstevel@tonic-gate 				value += digitvalue[(int)c];
10797c478bd9Sstevel@tonic-gate 				count += 4;
10807c478bd9Sstevel@tonic-gate 				tbcount += 4;
10817c478bd9Sstevel@tonic-gate 				if (tbcount > 256)
1082*9525b14bSRao Shoaib 					return (EINVAL);
10837c478bd9Sstevel@tonic-gate 				if (count == 8) {
10847c478bd9Sstevel@tonic-gate 					*tp++ = value;
10857c478bd9Sstevel@tonic-gate 					count = 0;
10867c478bd9Sstevel@tonic-gate 				}
10877c478bd9Sstevel@tonic-gate 			}
10887c478bd9Sstevel@tonic-gate 			break;
10897c478bd9Sstevel@tonic-gate 		}
10907c478bd9Sstevel@tonic-gate 	}
10917c478bd9Sstevel@tonic-gate   done:
10927c478bd9Sstevel@tonic-gate 	if (cp >= end || tp >= eom)
1093*9525b14bSRao Shoaib 		return (EMSGSIZE);
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 	/*
10967c478bd9Sstevel@tonic-gate 	 * bit length validation:
10977c478bd9Sstevel@tonic-gate 	 * If a <length> is present, the number of digits in the <bit-data>
10987c478bd9Sstevel@tonic-gate 	 * MUST be just sufficient to contain the number of bits specified
10997c478bd9Sstevel@tonic-gate 	 * by the <length>. If there are insignificant bits in a final
11007c478bd9Sstevel@tonic-gate 	 * hexadecimal or octal digit, they MUST be zero.
1101*9525b14bSRao Shoaib 	 * RFC2673, Section 3.2.
11027c478bd9Sstevel@tonic-gate 	 */
11037c478bd9Sstevel@tonic-gate 	if (blen > 0) {
11047c478bd9Sstevel@tonic-gate 		int traillen;
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 		if (((blen + 3) & ~3) != tbcount)
1107*9525b14bSRao Shoaib 			return (EINVAL);
1108*9525b14bSRao Shoaib 		traillen = tbcount - blen; /*%< between 0 and 3 */
11097c478bd9Sstevel@tonic-gate 		if (((value << (8 - traillen)) & 0xff) != 0)
1110*9525b14bSRao Shoaib 			return (EINVAL);
11117c478bd9Sstevel@tonic-gate 	}
11127c478bd9Sstevel@tonic-gate 	else
11137c478bd9Sstevel@tonic-gate 		blen = tbcount;
11147c478bd9Sstevel@tonic-gate 	if (blen == 256)
11157c478bd9Sstevel@tonic-gate 		blen = 0;
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	/* encode the type and the significant bit fields */
11187c478bd9Sstevel@tonic-gate 	**labelp = DNS_LABELTYPE_BITSTRING;
11197c478bd9Sstevel@tonic-gate 	**dst = blen;
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	*bp = cp;
11227c478bd9Sstevel@tonic-gate 	*dst = tp;
11237c478bd9Sstevel@tonic-gate 
1124*9525b14bSRao Shoaib 	return (0);
11257c478bd9Sstevel@tonic-gate }
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate static int
labellen(const u_char * lp)11287c478bd9Sstevel@tonic-gate labellen(const u_char *lp)
11297c478bd9Sstevel@tonic-gate {
11307c478bd9Sstevel@tonic-gate 	int bitlen;
11317c478bd9Sstevel@tonic-gate 	u_char l = *lp;
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	if ((l & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
11347c478bd9Sstevel@tonic-gate 		/* should be avoided by the caller */
1135*9525b14bSRao Shoaib 		return (-1);
11367c478bd9Sstevel@tonic-gate 	}
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	if ((l & NS_CMPRSFLGS) == NS_TYPE_ELT) {
11397c478bd9Sstevel@tonic-gate 		if (l == DNS_LABELTYPE_BITSTRING) {
11407c478bd9Sstevel@tonic-gate 			if ((bitlen = *(lp + 1)) == 0)
11417c478bd9Sstevel@tonic-gate 				bitlen = 256;
1142*9525b14bSRao Shoaib 			return ((bitlen + 7 ) / 8 + 1);
11437c478bd9Sstevel@tonic-gate 		}
1144*9525b14bSRao Shoaib 		return (-1);	/*%< unknwon ELT */
11457c478bd9Sstevel@tonic-gate 	}
1146*9525b14bSRao Shoaib 	return (l);
11477c478bd9Sstevel@tonic-gate }
1148*9525b14bSRao Shoaib 
1149*9525b14bSRao Shoaib /*! \file */
1150