17c478bd9Sstevel@tonic-gate /*
29525b14bSRao 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  *
99525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
109525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
119525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
129525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
139525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
149525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
159525b14bSRao 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 
409525b14bSRao 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 *,
749525b14bSRao Shoaib 					unsigned char **, unsigned char **,
759525b14bSRao Shoaib 					unsigned const char *);
767c478bd9Sstevel@tonic-gate static int		labellen(const u_char *);
779525b14bSRao Shoaib static int		decode_bitstring(const unsigned char **,
789525b14bSRao Shoaib 					 char *, const char *);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /* Public. */
817c478bd9Sstevel@tonic-gate 
829525b14bSRao Shoaib /*%
837c478bd9Sstevel@tonic-gate  *	Convert an encoded domain name to printable ascii as per RFC1035.
849525b14bSRao Shoaib 
857c478bd9Sstevel@tonic-gate  * return:
869525b14bSRao Shoaib  *\li	Number of bytes written to buffer, or -1 (with errno set)
879525b14bSRao Shoaib  *
887c478bd9Sstevel@tonic-gate  * notes:
899525b14bSRao Shoaib  *\li	The root is returned as "."
909525b14bSRao 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) {
1199525b14bSRao Shoaib 			errno = EMSGSIZE; /*%< XXX */
1209525b14bSRao 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;
1329525b14bSRao Shoaib 				return (-1);
1337c478bd9Sstevel@tonic-gate 			}
1349525b14bSRao Shoaib 			if ((m = decode_bitstring(&cp, dn, eom)) < 0)
1357c478bd9Sstevel@tonic-gate 			{
1367c478bd9Sstevel@tonic-gate 				errno = EMSGSIZE;
1379525b14bSRao Shoaib 				return (-1);
1387c478bd9Sstevel@tonic-gate 			}
139*55fea89dSDan Cross 			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 
1849525b14bSRao Shoaib /*%
1859525b14bSRao Shoaib  *	Convert a ascii string into an encoded domain name as per RFC1035.
1869525b14bSRao Shoaib  *
1879525b14bSRao Shoaib  * return:
1889525b14bSRao Shoaib  *
1899525b14bSRao Shoaib  *\li	-1 if it fails
1909525b14bSRao Shoaib  *\li	1 if string was fully qualified
1919525b14bSRao Shoaib  *\li	0 is string was not fully qualified
1929525b14bSRao Shoaib  *
1939525b14bSRao Shoaib  * notes:
1949525b14bSRao Shoaib  *\li	Enforces label and domain length limits.
1959525b14bSRao Shoaib  */
1969525b14bSRao Shoaib int
ns_name_pton(const char * src,u_char * dst,size_t dstsiz)1979525b14bSRao Shoaib ns_name_pton(const char *src, u_char *dst, size_t dstsiz) {
1989525b14bSRao Shoaib 	return (ns_name_pton2(src, dst, dstsiz, NULL));
1999525b14bSRao Shoaib }
2009525b14bSRao Shoaib 
2017c478bd9Sstevel@tonic-gate /*
2029525b14bSRao 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
2089525b14bSRao Shoaib  * side effects:
2099525b14bSRao 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)2149525b14bSRao 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) {
2269525b14bSRao Shoaib 			if (c == '[') { /*%< start a bit string label */
2277c478bd9Sstevel@tonic-gate 				if ((cp = strchr(src, ']')) == NULL) {
2289525b14bSRao Shoaib 					errno = EINVAL; /*%< ??? */
2299525b14bSRao Shoaib 					return (-1);
2307c478bd9Sstevel@tonic-gate 				}
2319525b14bSRao Shoaib 				if ((e = encode_bitsring(&src, cp + 2,
2329525b14bSRao Shoaib 							 &label, &bp, eom))
2337c478bd9Sstevel@tonic-gate 				    != 0) {
2347c478bd9Sstevel@tonic-gate 					errno = e;
2359525b14bSRao 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;
2439525b14bSRao 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);
2739525b14bSRao 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 				}
2959525b14bSRao Shoaib 				if (dstlen != NULL)
2969525b14bSRao 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);
3139525b14bSRao 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 	}
3309525b14bSRao Shoaib 	if ((bp - dst) > MAXCDNAME) {	/*%< src too big */
3317c478bd9Sstevel@tonic-gate 		errno = EMSGSIZE;
3327c478bd9Sstevel@tonic-gate 		return (-1);
3337c478bd9Sstevel@tonic-gate 	}
3349525b14bSRao Shoaib 	if (dstlen != NULL)
3359525b14bSRao Shoaib 		*dstlen = (bp - dst);
3367c478bd9Sstevel@tonic-gate 	return (0);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate 
3399525b14bSRao Shoaib /*%
3407c478bd9Sstevel@tonic-gate  *	Convert a network strings labels into all lowercase.
3419525b14bSRao Shoaib  *
3427c478bd9Sstevel@tonic-gate  * return:
3439525b14bSRao Shoaib  *\li	Number of bytes written to buffer, or -1 (with errno set)
3449525b14bSRao Shoaib  *
3457c478bd9Sstevel@tonic-gate  * notes:
3469525b14bSRao 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++;
3839525b14bSRao 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 
3939525b14bSRao Shoaib /*%
3947c478bd9Sstevel@tonic-gate  *	Unpack a domain name from a message, source may be compressed.
3959525b14bSRao Shoaib  *
3967c478bd9Sstevel@tonic-gate  * return:
3979525b14bSRao 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)
4029525b14bSRao Shoaib {
4039525b14bSRao Shoaib 	return (ns_name_unpack2(msg, eom, src, dst, dstsiz, NULL));
4049525b14bSRao Shoaib }
4059525b14bSRao Shoaib 
4069525b14bSRao Shoaib /*
4079525b14bSRao Shoaib  * ns_name_unpack2(msg, eom, src, dst, dstsiz, *dstlen)
4089525b14bSRao Shoaib  *	Unpack a domain name from a message, source may be compressed.
4099525b14bSRao Shoaib  * return:
4109525b14bSRao Shoaib  *	-1 if it fails, or consumed octets if it succeeds.
4119525b14bSRao Shoaib  * side effect:
4129525b14bSRao Shoaib  *	fills in *dstlen (if non-NULL).
4139525b14bSRao Shoaib  */
4149525b14bSRao 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)4159525b14bSRao Shoaib ns_name_unpack2(const u_char *msg, const u_char *eom, const u_char *src,
4169525b14bSRao 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;
4409525b14bSRao 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));
4619525b14bSRao 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;
4799525b14bSRao Shoaib 			return (-1);			/*%< flag error */
4807c478bd9Sstevel@tonic-gate 		}
4817c478bd9Sstevel@tonic-gate 	}
4829525b14bSRao Shoaib 	*dstp++ = 0;
4839525b14bSRao Shoaib 	if (dstlen != NULL)
4849525b14bSRao 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 
4909525b14bSRao Shoaib /*%
4917c478bd9Sstevel@tonic-gate  *	Pack domain name 'domain' into 'comp_dn'.
4929525b14bSRao Shoaib  *
4937c478bd9Sstevel@tonic-gate  * return:
4949525b14bSRao Shoaib  *\li	Size of the compressed name, or -1.
4959525b14bSRao Shoaib  *
4967c478bd9Sstevel@tonic-gate  * notes:
4979525b14bSRao Shoaib  *\li	'dnptrs' is an array of pointers to previous compressed names.
4989525b14bSRao Shoaib  *\li	dnptrs[0] is a pointer to the beginning of the message. The array
4997c478bd9Sstevel@tonic-gate  *	ends with NULL.
5009525b14bSRao Shoaib  *\li	'lastdnptr' is a pointer to the end of the array pointed to
5017c478bd9Sstevel@tonic-gate  *	by 'dnptrs'.
5029525b14bSRao Shoaib  *
5037c478bd9Sstevel@tonic-gate  * Side effects:
5049525b14bSRao 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;
5269525b14bSRao 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;
5439525b14bSRao 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);
597*55fea89dSDan Cross 	}
5987c478bd9Sstevel@tonic-gate 	return (dstp - dst);
5997c478bd9Sstevel@tonic-gate }
6007c478bd9Sstevel@tonic-gate 
6019525b14bSRao Shoaib /*%
6027c478bd9Sstevel@tonic-gate  *	Expand compressed domain name to presentation format.
6039525b14bSRao Shoaib  *
6047c478bd9Sstevel@tonic-gate  * return:
6059525b14bSRao Shoaib  *\li	Number of bytes read out of `src', or -1 (with errno set).
6069525b14bSRao Shoaib  *
6077c478bd9Sstevel@tonic-gate  * note:
6089525b14bSRao 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;
616*55fea89dSDan Cross 
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 
6249525b14bSRao Shoaib /*%
6257c478bd9Sstevel@tonic-gate  *	Compress a domain name into wire format, using compression pointers.
6269525b14bSRao Shoaib  *
6277c478bd9Sstevel@tonic-gate  * return:
6289525b14bSRao Shoaib  *\li	Number of bytes consumed in `dst' or -1 (with errno set).
6299525b14bSRao Shoaib  *
6307c478bd9Sstevel@tonic-gate  * notes:
6319525b14bSRao Shoaib  *\li	'dnptrs' is an array of pointers to previous compressed names.
6329525b14bSRao Shoaib  *\li	dnptrs[0] is a pointer to the beginning of the message.
6339525b14bSRao 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.
6369525b14bSRao 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 
6509525b14bSRao 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 
6679525b14bSRao Shoaib /*%
6687c478bd9Sstevel@tonic-gate  *	Advance *ptrptr to skip over the compressed name it points at.
6699525b14bSRao Shoaib  *
6707c478bd9Sstevel@tonic-gate  * return:
6719525b14bSRao 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) {
6849525b14bSRao Shoaib 		case 0:			/*%< normal case, n == len */
6857c478bd9Sstevel@tonic-gate 			cp += n;
6867c478bd9Sstevel@tonic-gate 			continue;
6879525b14bSRao Shoaib 		case NS_TYPE_ELT: /*%< EDNS0 extended label */
6887c478bd9Sstevel@tonic-gate 			if ((l = labellen(cp - 1)) < 0) {
6899525b14bSRao Shoaib 				errno = EMSGSIZE; /*%< XXX */
6909525b14bSRao Shoaib 				return (-1);
6917c478bd9Sstevel@tonic-gate 			}
6927c478bd9Sstevel@tonic-gate 			cp += l;
6937c478bd9Sstevel@tonic-gate 			continue;
6949525b14bSRao Shoaib 		case NS_CMPRSFLGS:	/*%< indirection */
6957c478bd9Sstevel@tonic-gate 			cp++;
6967c478bd9Sstevel@tonic-gate 			break;
6979525b14bSRao 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 
7119525b14bSRao Shoaib /* Find the number of octets an nname takes up, including the root label.
7129525b14bSRao Shoaib  * (This is basically ns_name_skip() without compression-pointer support.)
7139525b14bSRao Shoaib  * ((NOTE: can only return zero if passed-in namesiz argument is zero.))
7149525b14bSRao Shoaib  */
7159525b14bSRao Shoaib ssize_t
ns_name_length(ns_nname_ct nname,size_t namesiz)7169525b14bSRao Shoaib ns_name_length(ns_nname_ct nname, size_t namesiz) {
7179525b14bSRao Shoaib 	ns_nname_ct orig = nname;
7189525b14bSRao Shoaib 	u_int n;
7199525b14bSRao Shoaib 
7209525b14bSRao Shoaib 	while (namesiz-- > 0 && (n = *nname++) != 0) {
7219525b14bSRao Shoaib 		if ((n & NS_CMPRSFLGS) != 0) {
7229525b14bSRao Shoaib 			errno = EISDIR;
7239525b14bSRao Shoaib 			return (-1);
7249525b14bSRao Shoaib 		}
7259525b14bSRao Shoaib 		if (n > namesiz) {
7269525b14bSRao Shoaib 			errno = EMSGSIZE;
7279525b14bSRao Shoaib 			return (-1);
7289525b14bSRao Shoaib 		}
7299525b14bSRao Shoaib 		nname += n;
7309525b14bSRao Shoaib 		namesiz -= n;
7319525b14bSRao Shoaib 	}
7329525b14bSRao Shoaib 	return (nname - orig);
7339525b14bSRao Shoaib }
7349525b14bSRao Shoaib 
7359525b14bSRao Shoaib /* Compare two nname's for equality.  Return -1 on error (setting errno).
7369525b14bSRao Shoaib  */
7379525b14bSRao Shoaib int
ns_name_eq(ns_nname_ct a,size_t as,ns_nname_ct b,size_t bs)7389525b14bSRao Shoaib ns_name_eq(ns_nname_ct a, size_t as, ns_nname_ct b, size_t bs) {
7399525b14bSRao Shoaib 	ns_nname_ct ae = a + as, be = b + bs;
7409525b14bSRao Shoaib 	int ac, bc;
7419525b14bSRao Shoaib 
7429525b14bSRao Shoaib 	while (ac = *a, bc = *b, ac != 0 && bc != 0) {
7439525b14bSRao Shoaib 		if ((ac & NS_CMPRSFLGS) != 0 || (bc & NS_CMPRSFLGS) != 0) {
7449525b14bSRao Shoaib 			errno = EISDIR;
7459525b14bSRao Shoaib 			return (-1);
7469525b14bSRao Shoaib 		}
7479525b14bSRao Shoaib 		if (a + ac >= ae || b + bc >= be) {
7489525b14bSRao Shoaib 			errno = EMSGSIZE;
7499525b14bSRao Shoaib 			return (-1);
7509525b14bSRao Shoaib 		}
7519525b14bSRao Shoaib 		if (ac != bc || strncasecmp((const char *) ++a,
7529525b14bSRao Shoaib 					    (const char *) ++b, ac) != 0)
7539525b14bSRao Shoaib 			return (0);
7549525b14bSRao Shoaib 		a += ac, b += bc;
7559525b14bSRao Shoaib 	}
7569525b14bSRao Shoaib 	return (ac == 0 && bc == 0);
7579525b14bSRao Shoaib }
7589525b14bSRao Shoaib 
7599525b14bSRao Shoaib /* Is domain "A" owned by (at or below) domain "B"?
7609525b14bSRao Shoaib  */
7619525b14bSRao Shoaib int
ns_name_owned(ns_namemap_ct a,int an,ns_namemap_ct b,int bn)7629525b14bSRao Shoaib ns_name_owned(ns_namemap_ct a, int an, ns_namemap_ct b, int bn) {
7639525b14bSRao Shoaib 	/* If A is shorter, it cannot be owned by B. */
7649525b14bSRao Shoaib 	if (an < bn)
7659525b14bSRao Shoaib 		return (0);
7669525b14bSRao Shoaib 
7679525b14bSRao Shoaib 	/* If they are unequal before the length of the shorter, A cannot... */
7689525b14bSRao Shoaib 	while (bn > 0) {
7699525b14bSRao Shoaib 		if (a->len != b->len ||
7709525b14bSRao Shoaib 		    strncasecmp((const char *) a->base,
7719525b14bSRao Shoaib 				(const char *) b->base, a->len) != 0)
7729525b14bSRao Shoaib 			return (0);
7739525b14bSRao Shoaib 		a++, an--;
7749525b14bSRao Shoaib 		b++, bn--;
7759525b14bSRao Shoaib 	}
7769525b14bSRao Shoaib 
7779525b14bSRao Shoaib 	/* A might be longer or not, but either way, B owns it. */
7789525b14bSRao Shoaib 	return (1);
7799525b14bSRao Shoaib }
7809525b14bSRao Shoaib 
7819525b14bSRao Shoaib /* Build an array of <base,len> tuples from an nname, top-down order.
7829525b14bSRao Shoaib  * Return the number of tuples (labels) thus discovered.
7839525b14bSRao Shoaib  */
7849525b14bSRao Shoaib int
ns_name_map(ns_nname_ct nname,size_t namelen,ns_namemap_t map,int mapsize)7859525b14bSRao Shoaib ns_name_map(ns_nname_ct nname, size_t namelen, ns_namemap_t map, int mapsize) {
7869525b14bSRao Shoaib 	u_int n;
7879525b14bSRao Shoaib 	int l;
7889525b14bSRao Shoaib 
7899525b14bSRao Shoaib 	n = *nname++;
7909525b14bSRao Shoaib 	namelen--;
7919525b14bSRao Shoaib 
7929525b14bSRao Shoaib 	/* Root zone? */
7939525b14bSRao Shoaib 	if (n == 0) {
7949525b14bSRao Shoaib 		/* Extra data follows name? */
7959525b14bSRao Shoaib 		if (namelen > 0) {
7969525b14bSRao Shoaib 			errno = EMSGSIZE;
7979525b14bSRao Shoaib 			return (-1);
7989525b14bSRao Shoaib 		}
7999525b14bSRao Shoaib 		return (0);
8009525b14bSRao Shoaib 	}
8019525b14bSRao Shoaib 
8029525b14bSRao Shoaib 	/* Compression pointer? */
8039525b14bSRao Shoaib 	if ((n & NS_CMPRSFLGS) != 0) {
8049525b14bSRao Shoaib 		errno = EISDIR;
8059525b14bSRao Shoaib 		return (-1);
8069525b14bSRao Shoaib 	}
8079525b14bSRao Shoaib 
8089525b14bSRao Shoaib 	/* Label too long? */
8099525b14bSRao Shoaib 	if (n > namelen) {
8109525b14bSRao Shoaib 		errno = EMSGSIZE;
8119525b14bSRao Shoaib 		return (-1);
8129525b14bSRao Shoaib 	}
8139525b14bSRao Shoaib 
8149525b14bSRao Shoaib 	/* Recurse to get rest of name done first. */
8159525b14bSRao Shoaib 	l = ns_name_map(nname + n, namelen - n, map, mapsize);
8169525b14bSRao Shoaib 	if (l < 0)
8179525b14bSRao Shoaib 		return (-1);
8189525b14bSRao Shoaib 
8199525b14bSRao Shoaib 	/* Too many labels? */
8209525b14bSRao Shoaib 	if (l >= mapsize) {
8219525b14bSRao Shoaib 		errno = ENAMETOOLONG;
8229525b14bSRao Shoaib 		return (-1);
8239525b14bSRao Shoaib 	}
8249525b14bSRao Shoaib 
8259525b14bSRao Shoaib 	/* We're on our way back up-stack, store current map data. */
8269525b14bSRao Shoaib 	map[l].base = nname;
8279525b14bSRao Shoaib 	map[l].len = n;
8289525b14bSRao Shoaib 	return (l + 1);
8299525b14bSRao Shoaib }
8309525b14bSRao Shoaib 
8319525b14bSRao Shoaib /* Count the labels in a domain name.  Root counts, so COM. has two.  This
8329525b14bSRao Shoaib  * is to make the result comparable to the result of ns_name_map().
8339525b14bSRao Shoaib  */
8349525b14bSRao Shoaib int
ns_name_labels(ns_nname_ct nname,size_t namesiz)8359525b14bSRao Shoaib ns_name_labels(ns_nname_ct nname, size_t namesiz) {
8369525b14bSRao Shoaib 	int ret = 0;
8379525b14bSRao Shoaib 	u_int n;
8389525b14bSRao Shoaib 
8399525b14bSRao Shoaib 	while (namesiz-- > 0 && (n = *nname++) != 0) {
8409525b14bSRao Shoaib 		if ((n & NS_CMPRSFLGS) != 0) {
8419525b14bSRao Shoaib 			errno = EISDIR;
8429525b14bSRao Shoaib 			return (-1);
8439525b14bSRao Shoaib 		}
8449525b14bSRao Shoaib 		if (n > namesiz) {
8459525b14bSRao Shoaib 			errno = EMSGSIZE;
8469525b14bSRao Shoaib 			return (-1);
8479525b14bSRao Shoaib 		}
8489525b14bSRao Shoaib 		nname += n;
8499525b14bSRao Shoaib 		namesiz -= n;
8509525b14bSRao Shoaib 		ret++;
8519525b14bSRao Shoaib 	}
8529525b14bSRao Shoaib 	return (ret + 1);
8539525b14bSRao Shoaib }
8549525b14bSRao Shoaib 
8557c478bd9Sstevel@tonic-gate /* Private. */
8567c478bd9Sstevel@tonic-gate 
8579525b14bSRao Shoaib /*%
8587c478bd9Sstevel@tonic-gate  *	Thinking in noninternationalized USASCII (per the DNS spec),
8597c478bd9Sstevel@tonic-gate  *	is this characted special ("in need of quoting") ?
8609525b14bSRao Shoaib  *
8617c478bd9Sstevel@tonic-gate  * return:
8629525b14bSRao 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) {
8679525b14bSRao Shoaib 	case 0x22: /*%< '"' */
8689525b14bSRao Shoaib 	case 0x2E: /*%< '.' */
8699525b14bSRao Shoaib 	case 0x3B: /*%< ';' */
8709525b14bSRao Shoaib 	case 0x5C: /*%< '\\' */
8719525b14bSRao Shoaib 	case 0x28: /*%< '(' */
8729525b14bSRao Shoaib 	case 0x29: /*%< ')' */
8737c478bd9Sstevel@tonic-gate 	/* Special modifiers in zone files. */
8749525b14bSRao Shoaib 	case 0x40: /*%< '@' */
8759525b14bSRao 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 
8829525b14bSRao 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 ?
8859525b14bSRao Shoaib  *
8867c478bd9Sstevel@tonic-gate  * return:
8879525b14bSRao 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 
8949525b14bSRao 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 
9059525b14bSRao Shoaib /*%
9067c478bd9Sstevel@tonic-gate  *	Search for the counted-label name in an array of compressed names.
9079525b14bSRao Shoaib  *
9087c478bd9Sstevel@tonic-gate  * return:
9099525b14bSRao Shoaib  *\li	offset from msg if found, or -1.
9109525b14bSRao Shoaib  *
9117c478bd9Sstevel@tonic-gate  * notes:
9129525b14bSRao Shoaib  *\li	dnptrs is the pointer to the first name on the list,
9139525b14bSRao 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) {
9419525b14bSRao Shoaib 				case 0:		/*%< normal case, n == len */
9429525b14bSRao 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;
9569525b14bSRao Shoaib 				case NS_CMPRSFLGS:	/*%< indirection */
9577c478bd9Sstevel@tonic-gate 					cp = msg + (((n & 0x3f) << 8) | *cp);
9587c478bd9Sstevel@tonic-gate 					break;
9597c478bd9Sstevel@tonic-gate 
9609525b14bSRao 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)9749525b14bSRao Shoaib decode_bitstring(const unsigned char **cpp, char *dn, const char *eom)
9757c478bd9Sstevel@tonic-gate {
9769525b14bSRao 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)
9859525b14bSRao 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",
1007*55fea89dSDan Cross 			       ((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;
10189525b14bSRao 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)10229525b14bSRao Shoaib encode_bitsring(const char **bp, const char *end, unsigned char **labelp,
10239525b14bSRao Shoaib 		unsigned char ** dst, unsigned const char *eom)
10247c478bd9Sstevel@tonic-gate {
10257c478bd9Sstevel@tonic-gate 	int afterslash = 0;
10267c478bd9Sstevel@tonic-gate 	const char *cp = *bp;
10279525b14bSRao Shoaib 	unsigned char *tp;
10289525b14bSRao 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)
10379525b14bSRao Shoaib 		return (EINVAL);
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	/* XXX: currently, only hex strings are supported */
10407c478bd9Sstevel@tonic-gate 	if (*cp++ != 'x')
10419525b14bSRao Shoaib 		return (EINVAL);
10429525b14bSRao Shoaib 	if (!isxdigit((*cp) & 0xff)) /*%< reject '\[x/BLEN]' */
10439525b14bSRao Shoaib 		return (EINVAL);
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	for (tp = *dst + 1; cp < end && tp < eom; cp++) {
10467c478bd9Sstevel@tonic-gate 		switch((c = *cp)) {
10479525b14bSRao Shoaib 		case ']':	/*%< end of the bitstring */
10487c478bd9Sstevel@tonic-gate 			if (afterslash) {
10497c478bd9Sstevel@tonic-gate 				if (beg_blen == NULL)
10509525b14bSRao Shoaib 					return (EINVAL);
10517c478bd9Sstevel@tonic-gate 				blen = (int)strtol(beg_blen, &end_blen, 10);
10527c478bd9Sstevel@tonic-gate 				if (*end_blen != ']')
10539525b14bSRao Shoaib 					return (EINVAL);
10547c478bd9Sstevel@tonic-gate 			}
10557c478bd9Sstevel@tonic-gate 			if (count)
10567c478bd9Sstevel@tonic-gate 				*tp++ = ((value << 4) & 0xff);
10579525b14bSRao 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))
10659525b14bSRao Shoaib 					return (EINVAL);
10667c478bd9Sstevel@tonic-gate 				if (beg_blen == NULL) {
1067*55fea89dSDan Cross 
10687c478bd9Sstevel@tonic-gate 					if (c == '0') {
10697c478bd9Sstevel@tonic-gate 						/* blen never begings with 0 */
10709525b14bSRao 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))
10769525b14bSRao 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)
10829525b14bSRao 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)
10939525b14bSRao 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.
11019525b14bSRao 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)
11079525b14bSRao Shoaib 			return (EINVAL);
11089525b14bSRao Shoaib 		traillen = tbcount - blen; /*%< between 0 and 3 */
11097c478bd9Sstevel@tonic-gate 		if (((value << (8 - traillen)) & 0xff) != 0)
11109525b14bSRao 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 
11249525b14bSRao 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 */
11359525b14bSRao 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;
11429525b14bSRao Shoaib 			return ((bitlen + 7 ) / 8 + 1);
11437c478bd9Sstevel@tonic-gate 		}
11449525b14bSRao Shoaib 		return (-1);	/*%< unknwon ELT */
11457c478bd9Sstevel@tonic-gate 	}
11469525b14bSRao Shoaib 	return (l);
11477c478bd9Sstevel@tonic-gate }
11489525b14bSRao Shoaib 
11499525b14bSRao Shoaib /*! \file */
1150