1 /* $NetBSD: ns_name.c,v 1.11 2014/03/07 01:07:01 christos Exp $ */
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996,1999 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/cdefs.h>
23 static const char rcsid
[] = "Id: ns_name.c,v 1.11 2009/01/23 19:59:16 each Exp";
25 __RCSID("$NetBSD: ns_name.c,v 1.11 2014/03/07 01:07:01 christos Exp $");
29 #include "port_before.h"
31 #include <sys/types.h>
33 #include <netinet/in.h>
34 #include <arpa/nameser.h>
44 #include "port_after.h"
47 # define SPRINTF(x) ((int)strlen(sprintf/**/x))
49 # define SPRINTF(x) (sprintf x)
52 #define NS_TYPE_ELT 0x40 /*%< EDNS0 extended label type */
53 #define DNS_LABELTYPE_BITSTRING 0x41
57 static const char digits
[] = "0123456789";
59 static const char digitvalue
[256] = {
60 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*16*/
61 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*32*/
62 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*48*/
63 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /*64*/
64 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*80*/
65 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*96*/
66 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*112*/
67 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*128*/
68 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
69 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
70 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
71 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
72 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
73 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
74 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
75 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*256*/
80 static int special(int);
81 static int printable(int);
82 static int dn_find(const u_char
*, const u_char
*,
83 const u_char
* const *,
84 const u_char
* const *);
85 static int encode_bitsring(const char **, const char *,
86 unsigned char **, unsigned char **,
87 unsigned const char *);
88 static int labellen(const u_char
*);
89 static int decode_bitstring(const unsigned char **,
90 char *, const char *);
95 * Convert an encoded domain name to printable ascii as per RFC1035.
98 *\li Number of bytes written to buffer, or -1 (with errno set)
101 *\li The root is returned as "."
102 *\li All other domains are returned in non absolute form
105 ns_name_ntop(const u_char
*src
, char *dst
, size_t dstsiz
)
117 while ((n
= *cp
++) != 0) {
118 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
119 /* Some kind of compression pointer. */
130 if ((l
= labellen(cp
- 1)) < 0) {
131 errno
= EMSGSIZE
; /*%< XXX */
138 if ((n
& NS_CMPRSFLGS
) == NS_TYPE_ELT
) {
141 if (n
!= DNS_LABELTYPE_BITSTRING
) {
142 /* XXX: labellen should reject this case */
146 if ((m
= decode_bitstring(&cp
, dn
, eom
)) < 0)
163 } else if (!printable(c
)) {
169 *dn
++ = digits
[c
/ 100];
170 *dn
++ = digits
[(c
% 100) / 10];
171 *dn
++ = digits
[c
% 10];
193 _DIAGASSERT(__type_fit(int, dn
- dst
));
194 return (int)(dn
- dst
);
198 * Convert a ascii string into an encoded domain name as per RFC1035.
203 *\li 1 if string was fully qualified
204 *\li 0 is string was not fully qualified
207 *\li Enforces label and domain length limits.
210 ns_name_pton(const char *src
, u_char
*dst
, size_t dstsiz
) {
211 return (ns_name_pton2(src
, dst
, dstsiz
, NULL
));
215 * ns_name_pton2(src, dst, dstsiz, *dstlen)
216 * Convert a ascii string into an encoded domain name as per RFC1035.
219 * 1 if string was fully qualified
220 * 0 is string was not fully qualified
222 * fills in *dstlen (if non-NULL)
224 * Enforces label and domain length limits.
227 ns_name_pton2(const char *src
, u_char
*dst
, size_t dstsiz
, size_t *dstlen
) {
228 u_char
*label
, *bp
, *eom
;
229 int c
, n
, escaped
, e
= 0;
237 while ((c
= *src
++) != 0) {
239 if (c
== '[') { /*%< start a bit string label */
240 if ((cp
= strchr(src
, ']')) == NULL
) {
241 errno
= EINVAL
; /*%< ??? */
244 if ((e
= encode_bitsring(&src
, cp
+ 2,
252 if ((c
= *src
++) == 0)
260 else if ((cp
= strchr(digits
, c
)) != NULL
) {
261 n
= (int)(cp
- digits
) * 100;
262 if ((c
= *src
++) == 0 ||
263 (cp
= strchr(digits
, c
)) == NULL
) {
267 n
+= (int)(cp
- digits
) * 10;
268 if ((c
= *src
++) == 0 ||
269 (cp
= strchr(digits
, c
)) == NULL
) {
273 n
+= (int)(cp
- digits
);
281 } else if (c
== '\\') {
284 } else if (c
== '.') {
285 c
= (int)(bp
- label
- 1);
286 if ((c
& NS_CMPRSFLGS
) != 0) { /*%< Label too big. */
295 /* Fully qualified ? */
304 if ((bp
- dst
) > MAXCDNAME
) {
309 *dstlen
= (bp
- dst
);
312 if (c
== 0 || *src
== '.') {
325 c
= (int)(bp
- label
- 1);
326 if ((c
& NS_CMPRSFLGS
) != 0) { /*%< Label too big. */
343 if ((bp
- dst
) > MAXCDNAME
) { /*%< src too big */
348 *dstlen
= (bp
- dst
);
353 * Convert a network strings labels into all lowercase.
356 *\li Number of bytes written to buffer, or -1 (with errno set)
359 *\li Enforces label and domain length limits.
363 ns_name_ntol(const u_char
*src
, u_char
*dst
, size_t dstsiz
)
379 while ((n
= *cp
++) != 0) {
380 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
381 /* Some kind of compression pointer. */
386 if ((l
= labellen(cp
- 1)) < 0) {
396 if (isascii(c
) && isupper(c
))
403 _DIAGASSERT(__type_fit(int, dn
- dst
));
404 return (int)(dn
- dst
);
408 * Unpack a domain name from a message, source may be compressed.
411 *\li -1 if it fails, or consumed octets if it succeeds.
414 ns_name_unpack(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
415 u_char
*dst
, size_t dstsiz
)
417 return (ns_name_unpack2(msg
, eom
, src
, dst
, dstsiz
, NULL
));
421 * ns_name_unpack2(msg, eom, src, dst, dstsiz, *dstlen)
422 * Unpack a domain name from a message, source may be compressed.
424 * -1 if it fails, or consumed octets if it succeeds.
426 * fills in *dstlen (if non-NULL).
429 ns_name_unpack2(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
430 u_char
*dst
, size_t dstsiz
, size_t *dstlen
)
432 const u_char
*srcp
, *dstlim
;
434 int n
, len
, checked
, l
;
440 dstlim
= dst
+ dstsiz
;
441 if (srcp
< msg
|| srcp
>= eom
) {
445 /* Fetch next label in domain name. */
446 while ((n
= *srcp
++) != 0) {
447 /* Check for indirection. */
448 switch (n
& NS_CMPRSFLGS
) {
452 if ((l
= labellen(srcp
- 1)) < 0) {
456 if (dstp
+ l
+ 1 >= dstlim
|| srcp
+ l
>= eom
) {
462 memcpy(dstp
, srcp
, (size_t)l
);
473 _DIAGASSERT(__type_fit(int, srcp
- src
+ 1));
474 len
= (int)(srcp
- src
+ 1);
476 n
= ((n
& 0x3f) << 8) | (*srcp
& 0xff);
477 if (n
>= eom
- msg
) { /*%< Out of range. */
484 * Check for loops in the compressed name;
485 * if we've looked at the whole message,
486 * there must be a loop.
488 if (checked
>= eom
- msg
) {
496 return (-1); /*%< flag error */
501 *dstlen
= dstp
- dst
;
503 _DIAGASSERT(__type_fit(int, srcp
- src
));
504 len
= (int)(srcp
- src
);
510 * Pack domain name 'domain' into 'comp_dn'.
513 *\li Size of the compressed name, or -1.
516 *\li 'dnptrs' is an array of pointers to previous compressed names.
517 *\li dnptrs[0] is a pointer to the beginning of the message. The array
519 *\li 'lastdnptr' is a pointer to the end of the array pointed to
523 *\li The list of pointers in dnptrs is updated for labels inserted into
524 * the message as we compress the name. If 'dnptr' is NULL, we don't
525 * try to compress names. If 'lastdnptr' is NULL, we don't update the
529 ns_name_pack(const u_char
*src
, u_char
*dst
, int dstsiz
,
530 const u_char
**dnptrs
, const u_char
**lastdnptr
)
533 const u_char
**cpp
, **lpp
, *eob
, *msg
;
541 if (dnptrs
!= NULL
) {
542 if ((msg
= *dnptrs
++) != NULL
) {
543 for (cpp
= dnptrs
; *cpp
!= NULL
; cpp
++)
545 lpp
= cpp
; /*%< end of list to search */
550 /* make sure the domain we are about to add is legal */
556 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
560 if ((l0
= labellen(srcp
)) < 0) {
572 /* from here on we need to reset compression pointer array on error */
575 /* Look to see if we can use pointers. */
577 if (n
!= 0 && msg
!= NULL
) {
578 l
= dn_find(srcp
, msg
, (const u_char
* const *)dnptrs
,
579 (const u_char
* const *)lpp
);
581 if (dstp
+ 1 >= eob
) {
584 *dstp
++ = ((u_int32_t
)l
>> 8) | NS_CMPRSFLGS
;
586 _DIAGASSERT(__type_fit(int, dstp
- dst
));
587 return (int)(dstp
- dst
);
589 /* Not found, save it. */
590 if (lastdnptr
!= NULL
&& cpp
< lastdnptr
- 1 &&
591 (dstp
- msg
) < 0x4000 && first
) {
597 /* copy label to buffer */
598 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
599 /* Should not happen. */
603 if (dstp
+ 1 + n
>= eob
) {
606 memcpy(dstp
, srcp
, (size_t)(n
+ 1));
618 _DIAGASSERT(__type_fit(int, dstp
- dst
));
619 return (int)(dstp
- dst
);
623 * Expand compressed domain name to presentation format.
626 *\li Number of bytes read out of `src', or -1 (with errno set).
629 *\li Root domain returns as "." not "".
632 ns_name_uncompress(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
633 char *dst
, size_t dstsiz
)
635 u_char tmp
[NS_MAXCDNAME
];
638 if ((n
= ns_name_unpack(msg
, eom
, src
, tmp
, sizeof tmp
)) == -1)
640 if (ns_name_ntop(tmp
, dst
, dstsiz
) == -1)
646 * Compress a domain name into wire format, using compression pointers.
649 *\li Number of bytes consumed in `dst' or -1 (with errno set).
652 *\li 'dnptrs' is an array of pointers to previous compressed names.
653 *\li dnptrs[0] is a pointer to the beginning of the message.
654 *\li The list ends with NULL. 'lastdnptr' is a pointer to the end of the
655 * array pointed to by 'dnptrs'. Side effect is to update the list of
656 * pointers for labels inserted into the message as we compress the name.
657 *\li If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
658 * is NULL, we don't update the list.
661 ns_name_compress(const char *src
, u_char
*dst
, size_t dstsiz
,
662 const u_char
**dnptrs
, const u_char
**lastdnptr
)
664 u_char tmp
[NS_MAXCDNAME
];
666 if (ns_name_pton(src
, tmp
, sizeof tmp
) == -1)
668 return (ns_name_pack(tmp
, dst
, (int)dstsiz
, dnptrs
, lastdnptr
));
672 * Reset dnptrs so that there are no active references to pointers at or
676 ns_name_rollback(const u_char
*src
, const u_char
**dnptrs
,
677 const u_char
**lastdnptr
)
679 while (dnptrs
< lastdnptr
&& *dnptrs
!= NULL
) {
680 if (*dnptrs
>= src
) {
689 * Advance *ptrptr to skip over the compressed name it points at.
692 *\li 0 on success, -1 (with errno set) on failure.
695 ns_name_skip(const u_char
**ptrptr
, const u_char
*eom
)
702 while (cp
< eom
&& (n
= *cp
++) != 0) {
703 /* Check for indirection. */
704 switch (n
& NS_CMPRSFLGS
) {
705 case 0: /*%< normal case, n == len */
708 case NS_TYPE_ELT
: /*%< EDNS0 extended label */
709 if ((l
= labellen(cp
- 1)) < 0) {
710 errno
= EMSGSIZE
; /*%< XXX */
715 case NS_CMPRSFLGS
: /*%< indirection */
718 default: /*%< illegal type */
732 /* Find the number of octets an nname takes up, including the root label.
733 * (This is basically ns_name_skip() without compression-pointer support.)
734 * ((NOTE: can only return zero if passed-in namesiz argument is zero.))
737 ns_name_length(ns_nname_ct nname
, size_t namesiz
) {
738 ns_nname_ct orig
= nname
;
741 while (namesiz
-- > 0 && (n
= *nname
++) != 0) {
742 if ((n
& NS_CMPRSFLGS
) != 0) {
753 return (nname
- orig
);
756 /* Compare two nname's for equality. Return -1 on error (setting errno).
759 ns_name_eq(ns_nname_ct a
, size_t as
, ns_nname_ct b
, size_t bs
) {
760 ns_nname_ct ae
= a
+ as
, be
= b
+ bs
;
763 while (ac
= *a
, bc
= *b
, ac
!= 0 && bc
!= 0) {
764 if ((ac
& NS_CMPRSFLGS
) != 0 || (bc
& NS_CMPRSFLGS
) != 0) {
768 if (a
+ ac
>= ae
|| b
+ bc
>= be
) {
772 if (ac
!= bc
|| strncasecmp((const char *) ++a
,
778 return (ac
== 0 && bc
== 0);
781 /* Is domain "A" owned by (at or below) domain "B"?
784 ns_name_owned(ns_namemap_ct a
, int an
, ns_namemap_ct b
, int bn
) {
785 /* If A is shorter, it cannot be owned by B. */
789 /* If they are unequal before the length of the shorter, A cannot... */
791 if (a
->len
!= b
->len
||
792 strncasecmp((const char *) a
->base
,
793 (const char *) b
->base
, (size_t)a
->len
) != 0)
799 /* A might be longer or not, but either way, B owns it. */
803 /* Build an array of <base,len> tuples from an nname, top-down order.
804 * Return the number of tuples (labels) thus discovered.
807 ns_name_map(ns_nname_ct nname
, size_t namelen
, ns_namemap_t map
, int mapsize
) {
816 /* Extra data follows name? */
824 /* Compression pointer? */
825 if ((n
& NS_CMPRSFLGS
) != 0) {
830 /* Label too long? */
836 /* Recurse to get rest of name done first. */
837 l
= ns_name_map(nname
+ n
, namelen
- n
, map
, mapsize
);
841 /* Too many labels? */
843 errno
= ENAMETOOLONG
;
847 /* We're on our way back up-stack, store current map data. */
853 /* Count the labels in a domain name. Root counts, so COM. has two. This
854 * is to make the result comparable to the result of ns_name_map().
857 ns_name_labels(ns_nname_ct nname
, size_t namesiz
) {
861 while (namesiz
-- > 0 && (n
= *nname
++) != 0) {
862 if ((n
& NS_CMPRSFLGS
) != 0) {
880 * Thinking in noninternationalized USASCII (per the DNS spec),
881 * is this characted special ("in need of quoting") ?
889 case 0x22: /*%< '"' */
890 case 0x2E: /*%< '.' */
891 case 0x3B: /*%< ';' */
892 case 0x5C: /*%< '\\' */
893 case 0x28: /*%< '(' */
894 case 0x29: /*%< ')' */
895 /* Special modifiers in zone files. */
896 case 0x40: /*%< '@' */
897 case 0x24: /*%< '$' */
905 * Thinking in noninternationalized USASCII (per the DNS spec),
906 * is this character visible and not a space when printed ?
913 return (ch
> 0x20 && ch
< 0x7f);
917 * Thinking in noninternationalized USASCII (per the DNS spec),
918 * convert this character to lower case if it's upper case.
922 if (ch
>= 0x41 && ch
<= 0x5A)
928 * Search for the counted-label name in an array of compressed names.
931 *\li offset from msg if found, or -1.
934 *\li dnptrs is the pointer to the first name on the list,
935 *\li not the pointer to the start of the message.
938 dn_find(const u_char
*domain
, const u_char
*msg
,
939 const u_char
* const *dnptrs
,
940 const u_char
* const *lastdnptr
)
942 const u_char
*dn
, *cp
, *sp
;
943 const u_char
* const *cpp
;
946 for (cpp
= dnptrs
; cpp
< lastdnptr
; cpp
++) {
949 * terminate search on:
951 * compression pointer
954 while (*sp
!= 0 && (*sp
& NS_CMPRSFLGS
) == 0 &&
955 (sp
- msg
) < 0x4000) {
958 while ((n
= *cp
++) != 0) {
960 * check for indirection
962 switch (n
& NS_CMPRSFLGS
) {
963 case 0: /*%< normal case, n == len */
964 n
= labellen(cp
- 1); /*%< XXX */
969 if (mklower(*dn
++) !=
972 /* Is next root for both ? */
973 if (*dn
== '\0' && *cp
== '\0') {
974 _DIAGASSERT(__type_fit(int,
976 return (int)(sp
- msg
);
981 case NS_CMPRSFLGS
: /*%< indirection */
982 cp
= msg
+ (((n
& 0x3f) << 8) | *cp
);
985 default: /*%< illegal type */
999 decode_bitstring(const unsigned char **cpp
, char *dn
, const char *eom
)
1001 const unsigned char *cp
= *cpp
;
1003 int b
, blen
, plen
, i
;
1005 if ((blen
= (*cp
& 0xff)) == 0)
1007 plen
= (blen
+ 3) / 4;
1008 plen
+= (int)sizeof("\\[x/]") + (blen
> 99 ? 3 : (blen
> 9) ? 2 : 1);
1009 if (dn
+ plen
>= eom
)
1013 i
= SPRINTF((dn
, "\\[x"));
1017 for (b
= blen
; b
> 7; b
-= 8, cp
++) {
1018 i
= SPRINTF((dn
, "%02x", *cp
& 0xff));
1025 i
= SPRINTF((dn
, "%02x", tc
& (0xff << (8 - b
))));
1031 i
= SPRINTF((dn
, "%1x",
1032 (((u_int32_t
)tc
>> 4) & 0x0f) & (0x0f << (4 - b
))));
1037 i
= SPRINTF((dn
, "/%d]", blen
));
1043 _DIAGASSERT(__type_fit(int, dn
- beg
));
1044 return (int)(dn
- beg
);
1048 encode_bitsring(const char **bp
, const char *end
, unsigned char **labelp
,
1049 unsigned char ** dst
, unsigned const char *eom
)
1052 const char *cp
= *bp
;
1055 const char *beg_blen
;
1056 char *end_blen
= NULL
;
1057 int value
= 0, count
= 0, tbcount
= 0, blen
= 0;
1059 beg_blen
= end_blen
= NULL
;
1061 /* a bitstring must contain at least 2 characters */
1065 /* XXX: currently, only hex strings are supported */
1068 if (!isxdigit((*cp
) & 0xff)) /*%< reject '\[x/BLEN]' */
1071 for (tp
= *dst
+ 1; cp
< end
&& tp
< eom
; cp
++) {
1073 case ']': /*%< end of the bitstring */
1075 if (beg_blen
== NULL
)
1077 blen
= (int)strtol(beg_blen
, &end_blen
, 10);
1078 if (*end_blen
!= ']')
1082 *tp
++ = ((value
<< 4) & 0xff);
1083 cp
++; /*%< skip ']' */
1090 if (!isdigit(c
&0xff))
1092 if (beg_blen
== NULL
) {
1095 /* blen never begings with 0 */
1101 if (!isxdigit(c
&0xff))
1104 value
+= digitvalue
[(int)c
];
1118 if (cp
>= end
|| tp
>= eom
)
1122 * bit length validation:
1123 * If a <length> is present, the number of digits in the <bit-data>
1124 * MUST be just sufficient to contain the number of bits specified
1125 * by the <length>. If there are insignificant bits in a final
1126 * hexadecimal or octal digit, they MUST be zero.
1127 * RFC2673, Section 3.2.
1132 if (((blen
+ 3) & ~3) != tbcount
)
1134 traillen
= tbcount
- blen
; /*%< between 0 and 3 */
1135 if (((value
<< (8 - traillen
)) & 0xff) != 0)
1143 /* encode the type and the significant bit fields */
1144 **labelp
= DNS_LABELTYPE_BITSTRING
;
1154 labellen(const u_char
*lp
)
1159 if ((l
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
1160 /* should be avoided by the caller */
1164 if ((l
& NS_CMPRSFLGS
) == NS_TYPE_ELT
) {
1165 if (l
== DNS_LABELTYPE_BITSTRING
) {
1166 if ((bitlen
= *(lp
+ 1)) == 0)
1168 return ((bitlen
+ 7 ) / 8 + 1);
1170 return (-1); /*%< unknwon ELT */