michael@0: /* $NetBSD: ns_name.c,v 1.3 2004/11/07 02:19:49 christos Exp $ */ michael@0: michael@0: /* michael@0: * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") michael@0: * Copyright (c) 1996,1999 by Internet Software Consortium. michael@0: * michael@0: * Permission to use, copy, modify, and distribute this software for any michael@0: * purpose with or without fee is hereby granted, provided that the above michael@0: * copyright notice and this permission notice appear in all copies. michael@0: * michael@0: * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES michael@0: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF michael@0: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR michael@0: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES michael@0: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN michael@0: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT michael@0: * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. michael@0: */ michael@0: michael@0: /* michael@0: * This version of this file is derived from Android 2.3 "Gingerbread", michael@0: * which contains uncredited changes by Android/Google developers. It has michael@0: * been modified in 2011 for use in the Android build of Mozilla Firefox by michael@0: * Mozilla contributors (including Michael Edwards , michael@0: * and Steve Workman ). michael@0: * These changes are offered under the same license as the original NetBSD michael@0: * file, whose copyright and license are unchanged above. michael@0: */ michael@0: michael@0: #define ANDROID_CHANGES 1 michael@0: #define MOZILLA_NECKO_EXCLUDE_CODE 1 michael@0: michael@0: #include michael@0: #ifndef lint michael@0: #ifdef notdef michael@0: static const char rcsid[] = "Id: ns_name.c,v 1.3.2.4.4.2 2004/05/04 03:27:47 marka Exp"; michael@0: #else michael@0: __RCSID("$NetBSD: ns_name.c,v 1.3 2004/11/07 02:19:49 christos Exp $"); michael@0: #endif michael@0: #endif michael@0: michael@0: #include michael@0: michael@0: #include michael@0: #include "arpa_nameser.h" michael@0: michael@0: #include michael@0: #ifdef ANDROID_CHANGES michael@0: #include "resolv_private.h" michael@0: #else michael@0: #include michael@0: #endif michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #ifdef SPRINTF_CHAR michael@0: # define SPRINTF(x) strlen(sprintf/**/x) michael@0: #else michael@0: # define SPRINTF(x) ((size_t)sprintf x) michael@0: #endif michael@0: michael@0: #define NS_TYPE_ELT 0x40 /* EDNS0 extended label type */ michael@0: #define DNS_LABELTYPE_BITSTRING 0x41 michael@0: michael@0: /* Data. */ michael@0: michael@0: static const char digits[] = "0123456789"; michael@0: michael@0: static const char digitvalue[256] = { michael@0: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*16*/ michael@0: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*32*/ michael@0: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*48*/ michael@0: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /*64*/ michael@0: -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*80*/ michael@0: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*96*/ michael@0: -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*112*/ michael@0: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*128*/ michael@0: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, michael@0: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, michael@0: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, michael@0: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, michael@0: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, michael@0: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, michael@0: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, michael@0: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*256*/ michael@0: }; michael@0: michael@0: /* Forward. */ michael@0: michael@0: static int special(int); michael@0: static int printable(int); michael@0: static int dn_find(const u_char *, const u_char *, michael@0: const u_char * const *, michael@0: const u_char * const *); michael@0: static int encode_bitsring(const char **, const char *, michael@0: unsigned char **, unsigned char **, michael@0: unsigned const char *); michael@0: static int labellen(const u_char *); michael@0: static int decode_bitstring(const unsigned char **, michael@0: char *, const char *); michael@0: michael@0: /* Public. */ michael@0: michael@0: /* michael@0: * ns_name_ntop(src, dst, dstsiz) michael@0: * Convert an encoded domain name to printable ascii as per RFC1035. michael@0: * return: michael@0: * Number of bytes written to buffer, or -1 (with errno set) michael@0: * notes: michael@0: * The root is returned as "." michael@0: * All other domains are returned in non absolute form michael@0: */ michael@0: int michael@0: ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) michael@0: { michael@0: const u_char *cp; michael@0: char *dn, *eom; michael@0: u_char c; michael@0: u_int n; michael@0: int l; michael@0: michael@0: cp = src; michael@0: dn = dst; michael@0: eom = dst + dstsiz; michael@0: michael@0: while ((n = *cp++) != 0) { michael@0: if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) { michael@0: /* Some kind of compression pointer. */ michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: if (dn != dst) { michael@0: if (dn >= eom) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: *dn++ = '.'; michael@0: } michael@0: if ((l = labellen(cp - 1)) < 0) { michael@0: errno = EMSGSIZE; /* XXX */ michael@0: return(-1); michael@0: } michael@0: if (dn + l >= eom) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: if ((n & NS_CMPRSFLGS) == NS_TYPE_ELT) { michael@0: int m; michael@0: michael@0: if (n != DNS_LABELTYPE_BITSTRING) { michael@0: /* XXX: labellen should reject this case */ michael@0: errno = EINVAL; michael@0: return(-1); michael@0: } michael@0: if ((m = decode_bitstring(&cp, dn, eom)) < 0) michael@0: { michael@0: errno = EMSGSIZE; michael@0: return(-1); michael@0: } michael@0: dn += m; michael@0: continue; michael@0: } michael@0: for (; l > 0; l--) { michael@0: c = *cp++; michael@0: if (special(c)) { michael@0: if (dn + 1 >= eom) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: *dn++ = '\\'; michael@0: *dn++ = (char)c; michael@0: } else if (!printable(c)) { michael@0: if (dn + 3 >= eom) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: *dn++ = '\\'; michael@0: *dn++ = digits[c / 100]; michael@0: *dn++ = digits[(c % 100) / 10]; michael@0: *dn++ = digits[c % 10]; michael@0: } else { michael@0: if (dn >= eom) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: *dn++ = (char)c; michael@0: } michael@0: } michael@0: } michael@0: if (dn == dst) { michael@0: if (dn >= eom) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: *dn++ = '.'; michael@0: } michael@0: if (dn >= eom) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: *dn++ = '\0'; michael@0: return (dn - dst); michael@0: } michael@0: michael@0: /* michael@0: * ns_name_pton(src, dst, dstsiz) michael@0: * Convert a ascii string into an encoded domain name as per RFC1035. michael@0: * return: michael@0: * -1 if it fails michael@0: * 1 if string was fully qualified michael@0: * 0 is string was not fully qualified michael@0: * notes: michael@0: * Enforces label and domain length limits. michael@0: */ michael@0: michael@0: int michael@0: ns_name_pton(const char *src, u_char *dst, size_t dstsiz) michael@0: { michael@0: u_char *label, *bp, *eom; michael@0: int c, n, escaped, e = 0; michael@0: char *cp; michael@0: michael@0: escaped = 0; michael@0: bp = dst; michael@0: eom = dst + dstsiz; michael@0: label = bp++; michael@0: michael@0: while ((c = *src++) != 0) { michael@0: if (escaped) { michael@0: if (c == '[') { /* start a bit string label */ michael@0: if ((cp = strchr(src, ']')) == NULL) { michael@0: errno = EINVAL; /* ??? */ michael@0: return(-1); michael@0: } michael@0: if ((e = encode_bitsring(&src, cp + 2, michael@0: &label, &bp, eom)) michael@0: != 0) { michael@0: errno = e; michael@0: return(-1); michael@0: } michael@0: escaped = 0; michael@0: label = bp++; michael@0: if ((c = *src++) == 0) michael@0: goto done; michael@0: else if (c != '.') { michael@0: errno = EINVAL; michael@0: return(-1); michael@0: } michael@0: continue; michael@0: } michael@0: else if ((cp = strchr(digits, c)) != NULL) { michael@0: n = (cp - digits) * 100; michael@0: if ((c = *src++) == 0 || michael@0: (cp = strchr(digits, c)) == NULL) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: n += (cp - digits) * 10; michael@0: if ((c = *src++) == 0 || michael@0: (cp = strchr(digits, c)) == NULL) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: n += (cp - digits); michael@0: if (n > 255) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: c = n; michael@0: } michael@0: escaped = 0; michael@0: } else if (c == '\\') { michael@0: escaped = 1; michael@0: continue; michael@0: } else if (c == '.') { michael@0: c = (bp - label - 1); michael@0: if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */ michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: if (label >= eom) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: *label = c; michael@0: /* Fully qualified ? */ michael@0: if (*src == '\0') { michael@0: if (c != 0) { michael@0: if (bp >= eom) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: *bp++ = '\0'; michael@0: } michael@0: if ((bp - dst) > MAXCDNAME) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: return (1); michael@0: } michael@0: if (c == 0 || *src == '.') { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: label = bp++; michael@0: continue; michael@0: } michael@0: if (bp >= eom) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: *bp++ = (u_char)c; michael@0: } michael@0: c = (bp - label - 1); michael@0: if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */ michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: done: michael@0: if (label >= eom) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: *label = c; michael@0: if (c != 0) { michael@0: if (bp >= eom) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: *bp++ = 0; michael@0: } michael@0: if ((bp - dst) > MAXCDNAME) { /* src too big */ michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: #ifndef MOZILLA_NECKO_EXCLUDE_CODE michael@0: /* michael@0: * ns_name_ntol(src, dst, dstsiz) michael@0: * Convert a network strings labels into all lowercase. michael@0: * return: michael@0: * Number of bytes written to buffer, or -1 (with errno set) michael@0: * notes: michael@0: * Enforces label and domain length limits. michael@0: */ michael@0: michael@0: int michael@0: ns_name_ntol(const u_char *src, u_char *dst, size_t dstsiz) michael@0: { michael@0: const u_char *cp; michael@0: u_char *dn, *eom; michael@0: u_char c; michael@0: u_int n; michael@0: int l; michael@0: michael@0: cp = src; michael@0: dn = dst; michael@0: eom = dst + dstsiz; michael@0: michael@0: if (dn >= eom) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: while ((n = *cp++) != 0) { michael@0: if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) { michael@0: /* Some kind of compression pointer. */ michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: *dn++ = n; michael@0: if ((l = labellen(cp - 1)) < 0) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: if (dn + l >= eom) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: for (; l > 0; l--) { michael@0: c = *cp++; michael@0: if (isupper(c)) michael@0: *dn++ = tolower(c); michael@0: else michael@0: *dn++ = c; michael@0: } michael@0: } michael@0: *dn++ = '\0'; michael@0: return (dn - dst); michael@0: } michael@0: #endif michael@0: michael@0: /* michael@0: * ns_name_unpack(msg, eom, src, dst, dstsiz) michael@0: * Unpack a domain name from a message, source may be compressed. michael@0: * return: michael@0: * -1 if it fails, or consumed octets if it succeeds. michael@0: */ michael@0: int michael@0: ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src, michael@0: u_char *dst, size_t dstsiz) michael@0: { michael@0: const u_char *srcp, *dstlim; michael@0: u_char *dstp; michael@0: int n, len, checked, l; michael@0: michael@0: len = -1; michael@0: checked = 0; michael@0: dstp = dst; michael@0: srcp = src; michael@0: dstlim = dst + dstsiz; michael@0: if (srcp < msg || srcp >= eom) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: /* Fetch next label in domain name. */ michael@0: while ((n = *srcp++) != 0) { michael@0: /* Check for indirection. */ michael@0: switch (n & NS_CMPRSFLGS) { michael@0: case 0: michael@0: case NS_TYPE_ELT: michael@0: /* Limit checks. */ michael@0: if ((l = labellen(srcp - 1)) < 0) { michael@0: errno = EMSGSIZE; michael@0: return(-1); michael@0: } michael@0: if (dstp + l + 1 >= dstlim || srcp + l >= eom) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: checked += l + 1; michael@0: *dstp++ = n; michael@0: memcpy(dstp, srcp, (size_t)l); michael@0: dstp += l; michael@0: srcp += l; michael@0: break; michael@0: michael@0: case NS_CMPRSFLGS: michael@0: if (srcp >= eom) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: if (len < 0) michael@0: len = srcp - src + 1; michael@0: srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff)); michael@0: if (srcp < msg || srcp >= eom) { /* Out of range. */ michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: checked += 2; michael@0: /* michael@0: * Check for loops in the compressed name; michael@0: * if we've looked at the whole message, michael@0: * there must be a loop. michael@0: */ michael@0: if (checked >= eom - msg) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: break; michael@0: michael@0: default: michael@0: errno = EMSGSIZE; michael@0: return (-1); /* flag error */ michael@0: } michael@0: } michael@0: *dstp = '\0'; michael@0: if (len < 0) michael@0: len = srcp - src; michael@0: return (len); michael@0: } michael@0: michael@0: /* michael@0: * ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr) michael@0: * Pack domain name 'domain' into 'comp_dn'. michael@0: * return: michael@0: * Size of the compressed name, or -1. michael@0: * notes: michael@0: * 'dnptrs' is an array of pointers to previous compressed names. michael@0: * dnptrs[0] is a pointer to the beginning of the message. The array michael@0: * ends with NULL. michael@0: * 'lastdnptr' is a pointer to the end of the array pointed to michael@0: * by 'dnptrs'. michael@0: * Side effects: michael@0: * The list of pointers in dnptrs is updated for labels inserted into michael@0: * the message as we compress the name. If 'dnptr' is NULL, we don't michael@0: * try to compress names. If 'lastdnptr' is NULL, we don't update the michael@0: * list. michael@0: */ michael@0: int michael@0: ns_name_pack(const u_char *src, u_char *dst, int dstsiz, michael@0: const u_char **dnptrs, const u_char **lastdnptr) michael@0: { michael@0: u_char *dstp; michael@0: const u_char **cpp, **lpp, *eob, *msg; michael@0: const u_char *srcp; michael@0: int n, l, first = 1; michael@0: michael@0: srcp = src; michael@0: dstp = dst; michael@0: eob = dstp + dstsiz; michael@0: lpp = cpp = NULL; michael@0: if (dnptrs != NULL) { michael@0: if ((msg = *dnptrs++) != NULL) { michael@0: for (cpp = dnptrs; *cpp != NULL; cpp++) michael@0: ; michael@0: lpp = cpp; /* end of list to search */ michael@0: } michael@0: } else michael@0: msg = NULL; michael@0: michael@0: /* make sure the domain we are about to add is legal */ michael@0: l = 0; michael@0: do { michael@0: int l0; michael@0: michael@0: n = *srcp; michael@0: if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: if ((l0 = labellen(srcp)) < 0) { michael@0: errno = EINVAL; michael@0: return(-1); michael@0: } michael@0: l += l0 + 1; michael@0: if (l > MAXCDNAME) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: srcp += l0 + 1; michael@0: } while (n != 0); michael@0: michael@0: /* from here on we need to reset compression pointer array on error */ michael@0: srcp = src; michael@0: do { michael@0: /* Look to see if we can use pointers. */ michael@0: n = *srcp; michael@0: if (n != 0 && msg != NULL) { michael@0: l = dn_find(srcp, msg, (const u_char * const *)dnptrs, michael@0: (const u_char * const *)lpp); michael@0: if (l >= 0) { michael@0: if (dstp + 1 >= eob) { michael@0: goto cleanup; michael@0: } michael@0: *dstp++ = ((u_int32_t)l >> 8) | NS_CMPRSFLGS; michael@0: *dstp++ = l % 256; michael@0: return (dstp - dst); michael@0: } michael@0: /* Not found, save it. */ michael@0: if (lastdnptr != NULL && cpp < lastdnptr - 1 && michael@0: (dstp - msg) < 0x4000 && first) { michael@0: *cpp++ = dstp; michael@0: *cpp = NULL; michael@0: first = 0; michael@0: } michael@0: } michael@0: /* copy label to buffer */ michael@0: if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) { michael@0: /* Should not happen. */ michael@0: goto cleanup; michael@0: } michael@0: n = labellen(srcp); michael@0: if (dstp + 1 + n >= eob) { michael@0: goto cleanup; michael@0: } michael@0: memcpy(dstp, srcp, (size_t)(n + 1)); michael@0: srcp += n + 1; michael@0: dstp += n + 1; michael@0: } while (n != 0); michael@0: michael@0: if (dstp > eob) { michael@0: cleanup: michael@0: if (msg != NULL) michael@0: *lpp = NULL; michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: return (dstp - dst); michael@0: } michael@0: michael@0: /* michael@0: * ns_name_uncompress(msg, eom, src, dst, dstsiz) michael@0: * Expand compressed domain name to presentation format. michael@0: * return: michael@0: * Number of bytes read out of `src', or -1 (with errno set). michael@0: * note: michael@0: * Root domain returns as "." not "". michael@0: */ michael@0: int michael@0: ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src, michael@0: char *dst, size_t dstsiz) michael@0: { michael@0: u_char tmp[NS_MAXCDNAME]; michael@0: int n; michael@0: michael@0: if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1) michael@0: return (-1); michael@0: if (ns_name_ntop(tmp, dst, dstsiz) == -1) michael@0: return (-1); michael@0: return (n); michael@0: } michael@0: michael@0: /* michael@0: * ns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr) michael@0: * Compress a domain name into wire format, using compression pointers. michael@0: * return: michael@0: * Number of bytes consumed in `dst' or -1 (with errno set). michael@0: * notes: michael@0: * 'dnptrs' is an array of pointers to previous compressed names. michael@0: * dnptrs[0] is a pointer to the beginning of the message. michael@0: * The list ends with NULL. 'lastdnptr' is a pointer to the end of the michael@0: * array pointed to by 'dnptrs'. Side effect is to update the list of michael@0: * pointers for labels inserted into the message as we compress the name. michael@0: * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr' michael@0: * is NULL, we don't update the list. michael@0: */ michael@0: int michael@0: ns_name_compress(const char *src, u_char *dst, size_t dstsiz, michael@0: const u_char **dnptrs, const u_char **lastdnptr) michael@0: { michael@0: u_char tmp[NS_MAXCDNAME]; michael@0: michael@0: if (ns_name_pton(src, tmp, sizeof tmp) == -1) michael@0: return (-1); michael@0: return (ns_name_pack(tmp, dst, (int)dstsiz, dnptrs, lastdnptr)); michael@0: } michael@0: michael@0: #ifndef MOZILLA_NECKO_EXCLUDE_CODE michael@0: /* michael@0: * Reset dnptrs so that there are no active references to pointers at or michael@0: * after src. michael@0: */ michael@0: void michael@0: ns_name_rollback(const u_char *src, const u_char **dnptrs, michael@0: const u_char **lastdnptr) michael@0: { michael@0: while (dnptrs < lastdnptr && *dnptrs != NULL) { michael@0: if (*dnptrs >= src) { michael@0: *dnptrs = NULL; michael@0: break; michael@0: } michael@0: dnptrs++; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: /* michael@0: * ns_name_skip(ptrptr, eom) michael@0: * Advance *ptrptr to skip over the compressed name it points at. michael@0: * return: michael@0: * 0 on success, -1 (with errno set) on failure. michael@0: */ michael@0: int michael@0: ns_name_skip(const u_char **ptrptr, const u_char *eom) michael@0: { michael@0: const u_char *cp; michael@0: u_int n; michael@0: int l; michael@0: michael@0: cp = *ptrptr; michael@0: while (cp < eom && (n = *cp++) != 0) { michael@0: /* Check for indirection. */ michael@0: switch (n & NS_CMPRSFLGS) { michael@0: case 0: /* normal case, n == len */ michael@0: cp += n; michael@0: continue; michael@0: case NS_TYPE_ELT: /* EDNS0 extended label */ michael@0: if ((l = labellen(cp - 1)) < 0) { michael@0: errno = EMSGSIZE; /* XXX */ michael@0: return(-1); michael@0: } michael@0: cp += l; michael@0: continue; michael@0: case NS_CMPRSFLGS: /* indirection */ michael@0: cp++; michael@0: break; michael@0: default: /* illegal type */ michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: break; michael@0: } michael@0: if (cp > eom) { michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: *ptrptr = cp; michael@0: return (0); michael@0: } michael@0: michael@0: /* Private. */ michael@0: michael@0: /* michael@0: * special(ch) michael@0: * Thinking in noninternationalized USASCII (per the DNS spec), michael@0: * is this characted special ("in need of quoting") ? michael@0: * return: michael@0: * boolean. michael@0: */ michael@0: static int michael@0: special(int ch) { michael@0: switch (ch) { michael@0: case 0x22: /* '"' */ michael@0: case 0x2E: /* '.' */ michael@0: case 0x3B: /* ';' */ michael@0: case 0x5C: /* '\\' */ michael@0: case 0x28: /* '(' */ michael@0: case 0x29: /* ')' */ michael@0: /* Special modifiers in zone files. */ michael@0: case 0x40: /* '@' */ michael@0: case 0x24: /* '$' */ michael@0: return (1); michael@0: default: michael@0: return (0); michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * printable(ch) michael@0: * Thinking in noninternationalized USASCII (per the DNS spec), michael@0: * is this character visible and not a space when printed ? michael@0: * return: michael@0: * boolean. michael@0: */ michael@0: static int michael@0: printable(int ch) { michael@0: return (ch > 0x20 && ch < 0x7f); michael@0: } michael@0: michael@0: /* michael@0: * Thinking in noninternationalized USASCII (per the DNS spec), michael@0: * convert this character to lower case if it's upper case. michael@0: */ michael@0: static int michael@0: mklower(int ch) { michael@0: if (ch >= 0x41 && ch <= 0x5A) michael@0: return (ch + 0x20); michael@0: return (ch); michael@0: } michael@0: michael@0: /* michael@0: * dn_find(domain, msg, dnptrs, lastdnptr) michael@0: * Search for the counted-label name in an array of compressed names. michael@0: * return: michael@0: * offset from msg if found, or -1. michael@0: * notes: michael@0: * dnptrs is the pointer to the first name on the list, michael@0: * not the pointer to the start of the message. michael@0: */ michael@0: static int michael@0: dn_find(const u_char *domain, const u_char *msg, michael@0: const u_char * const *dnptrs, michael@0: const u_char * const *lastdnptr) michael@0: { michael@0: const u_char *dn, *cp, *sp; michael@0: const u_char * const *cpp; michael@0: u_int n; michael@0: michael@0: for (cpp = dnptrs; cpp < lastdnptr; cpp++) { michael@0: sp = *cpp; michael@0: /* michael@0: * terminate search on: michael@0: * root label michael@0: * compression pointer michael@0: * unusable offset michael@0: */ michael@0: while (*sp != 0 && (*sp & NS_CMPRSFLGS) == 0 && michael@0: (sp - msg) < 0x4000) { michael@0: dn = domain; michael@0: cp = sp; michael@0: while ((n = *cp++) != 0) { michael@0: /* michael@0: * check for indirection michael@0: */ michael@0: switch (n & NS_CMPRSFLGS) { michael@0: case 0: /* normal case, n == len */ michael@0: n = labellen(cp - 1); /* XXX */ michael@0: michael@0: if (n != *dn++) michael@0: goto next; michael@0: michael@0: for (; n > 0; n--) michael@0: if (mklower(*dn++) != michael@0: mklower(*cp++)) michael@0: goto next; michael@0: /* Is next root for both ? */ michael@0: if (*dn == '\0' && *cp == '\0') michael@0: return (sp - msg); michael@0: if (*dn) michael@0: continue; michael@0: goto next; michael@0: case NS_CMPRSFLGS: /* indirection */ michael@0: cp = msg + (((n & 0x3f) << 8) | *cp); michael@0: break; michael@0: michael@0: default: /* illegal type */ michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: } michael@0: next: ; michael@0: sp += *sp + 1; michael@0: } michael@0: } michael@0: errno = ENOENT; michael@0: return (-1); michael@0: } michael@0: michael@0: static int michael@0: decode_bitstring(const unsigned char **cpp, char *dn, const char *eom) michael@0: { michael@0: const unsigned char *cp = *cpp; michael@0: char *beg = dn, tc; michael@0: int b, blen, plen, i; michael@0: michael@0: if ((blen = (*cp & 0xff)) == 0) michael@0: blen = 256; michael@0: plen = (blen + 3) / 4; michael@0: plen += sizeof("\\[x/]") + (blen > 99 ? 3 : (blen > 9) ? 2 : 1); michael@0: if (dn + plen >= eom) michael@0: return(-1); michael@0: michael@0: cp++; michael@0: i = SPRINTF((dn, "\\[x")); michael@0: if (i < 0) michael@0: return (-1); michael@0: dn += i; michael@0: for (b = blen; b > 7; b -= 8, cp++) { michael@0: i = SPRINTF((dn, "%02x", *cp & 0xff)); michael@0: if (i < 0) michael@0: return (-1); michael@0: dn += i; michael@0: } michael@0: if (b > 4) { michael@0: tc = *cp++; michael@0: i = SPRINTF((dn, "%02x", tc & (0xff << (8 - b)))); michael@0: if (i < 0) michael@0: return (-1); michael@0: dn += i; michael@0: } else if (b > 0) { michael@0: tc = *cp++; michael@0: i = SPRINTF((dn, "%1x", michael@0: (((u_int32_t)tc >> 4) & 0x0f) & (0x0f << (4 - b)))); michael@0: if (i < 0) michael@0: return (-1); michael@0: dn += i; michael@0: } michael@0: i = SPRINTF((dn, "/%d]", blen)); michael@0: if (i < 0) michael@0: return (-1); michael@0: dn += i; michael@0: michael@0: *cpp = cp; michael@0: return(dn - beg); michael@0: } michael@0: michael@0: static int michael@0: encode_bitsring(const char **bp, const char *end, unsigned char **labelp, michael@0: unsigned char ** dst, unsigned const char *eom) michael@0: { michael@0: int afterslash = 0; michael@0: const char *cp = *bp; michael@0: unsigned char *tp; michael@0: char c; michael@0: const char *beg_blen; michael@0: char *end_blen = NULL; michael@0: int value = 0, count = 0, tbcount = 0, blen = 0; michael@0: michael@0: beg_blen = end_blen = NULL; michael@0: michael@0: /* a bitstring must contain at least 2 characters */ michael@0: if (end - cp < 2) michael@0: return(EINVAL); michael@0: michael@0: /* XXX: currently, only hex strings are supported */ michael@0: if (*cp++ != 'x') michael@0: return(EINVAL); michael@0: if (!isxdigit((*cp) & 0xff)) /* reject '\[x/BLEN]' */ michael@0: return(EINVAL); michael@0: michael@0: for (tp = *dst + 1; cp < end && tp < eom; cp++) { michael@0: switch((c = *cp)) { michael@0: case ']': /* end of the bitstring */ michael@0: if (afterslash) { michael@0: if (beg_blen == NULL) michael@0: return(EINVAL); michael@0: blen = (int)strtol(beg_blen, &end_blen, 10); michael@0: if (*end_blen != ']') michael@0: return(EINVAL); michael@0: } michael@0: if (count) michael@0: *tp++ = ((value << 4) & 0xff); michael@0: cp++; /* skip ']' */ michael@0: goto done; michael@0: case '/': michael@0: afterslash = 1; michael@0: break; michael@0: default: michael@0: if (afterslash) { michael@0: if (!isdigit(c&0xff)) michael@0: return(EINVAL); michael@0: if (beg_blen == NULL) { michael@0: michael@0: if (c == '0') { michael@0: /* blen never begings with 0 */ michael@0: return(EINVAL); michael@0: } michael@0: beg_blen = cp; michael@0: } michael@0: } else { michael@0: if (!isxdigit(c&0xff)) michael@0: return(EINVAL); michael@0: value <<= 4; michael@0: value += digitvalue[(int)c]; michael@0: count += 4; michael@0: tbcount += 4; michael@0: if (tbcount > 256) michael@0: return(EINVAL); michael@0: if (count == 8) { michael@0: *tp++ = value; michael@0: count = 0; michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: done: michael@0: if (cp >= end || tp >= eom) michael@0: return(EMSGSIZE); michael@0: michael@0: /* michael@0: * bit length validation: michael@0: * If a is present, the number of digits in the michael@0: * MUST be just sufficient to contain the number of bits specified michael@0: * by the . If there are insignificant bits in a final michael@0: * hexadecimal or octal digit, they MUST be zero. michael@0: * RFC 2673, Section 3.2. michael@0: */ michael@0: if (blen > 0) { michael@0: int traillen; michael@0: michael@0: if (((blen + 3) & ~3) != tbcount) michael@0: return(EINVAL); michael@0: traillen = tbcount - blen; /* between 0 and 3 */ michael@0: if (((value << (8 - traillen)) & 0xff) != 0) michael@0: return(EINVAL); michael@0: } michael@0: else michael@0: blen = tbcount; michael@0: if (blen == 256) michael@0: blen = 0; michael@0: michael@0: /* encode the type and the significant bit fields */ michael@0: **labelp = DNS_LABELTYPE_BITSTRING; michael@0: **dst = blen; michael@0: michael@0: *bp = cp; michael@0: *dst = tp; michael@0: michael@0: return(0); michael@0: } michael@0: michael@0: static int michael@0: labellen(const u_char *lp) michael@0: { michael@0: int bitlen; michael@0: u_char l = *lp; michael@0: michael@0: if ((l & NS_CMPRSFLGS) == NS_CMPRSFLGS) { michael@0: /* should be avoided by the caller */ michael@0: return(-1); michael@0: } michael@0: michael@0: if ((l & NS_CMPRSFLGS) == NS_TYPE_ELT) { michael@0: if (l == DNS_LABELTYPE_BITSTRING) { michael@0: if ((bitlen = *(lp + 1)) == 0) michael@0: bitlen = 256; michael@0: return((bitlen + 7 ) / 8 + 1); michael@0: } michael@0: return(-1); /* unknwon ELT */ michael@0: } michael@0: return(l); michael@0: }