2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996-1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 static const char rcsid[] = "$Id: ns_print.c,v 1.10 2005/04/27 04:56:40 sra Exp $";
24 #include "port_before.h"
26 #include <sys/types.h>
27 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/nameser.h>
31 #include <arpa/inet.h>
33 #include <isc/assertions.h>
40 #include "port_after.h"
43 # define SPRINTF(x) strlen(sprintf/**/x)
45 # define SPRINTF(x) ((size_t)sprintf x)
50 static size_t prune_origin(const char *name, const char *origin);
51 static int charstr(const u_char *rdata, const u_char *edata,
52 char **buf, size_t *buflen);
53 static int addname(const u_char *msg, size_t msglen,
54 const u_char **p, const char *origin,
55 char **buf, size_t *buflen);
56 static void addlen(size_t len, char **buf, size_t *buflen);
57 static int addstr(const char *src, size_t len,
58 char **buf, size_t *buflen);
59 static int addtab(size_t len, size_t target, int spaced,
60 char **buf, size_t *buflen);
73 * Convert an RR to presentation format.
76 *\li Number of characters written to buf, or -1 (check errno).
79 ns_sprintrr(const ns_msg *handle, const ns_rr *rr,
80 const char *name_ctx, const char *origin,
81 char *buf, size_t buflen)
85 n = ns_sprintrrf(ns_msg_base(*handle), ns_msg_size(*handle),
86 ns_rr_name(*rr), ns_rr_class(*rr), ns_rr_type(*rr),
87 ns_rr_ttl(*rr), ns_rr_rdata(*rr), ns_rr_rdlen(*rr),
88 name_ctx, origin, buf, buflen);
93 * Convert the fields of an RR into presentation format.
96 *\li Number of characters written to buf, or -1 (check errno).
99 ns_sprintrrf(const u_char *msg, size_t msglen,
100 const char *name, ns_class class, ns_type type,
101 u_long ttl, const u_char *rdata, size_t rdlen,
102 const char *name_ctx, const char *origin,
103 char *buf, size_t buflen)
105 const char *obuf = buf;
106 const u_char *edata = rdata + rdlen;
116 if (name_ctx != NULL && ns_samename(name_ctx, name) == 1) {
117 T(addstr("\t\t\t", 3, &buf, &buflen));
119 len = prune_origin(name, origin);
122 } else if (len == 0) {
123 T(addstr("@\t\t\t", 4, &buf, &buflen));
125 T(addstr(name, len, &buf, &buflen));
126 /* Origin not used or not root, and no trailing dot? */
127 if (((origin == NULL || origin[0] == '\0') ||
128 (origin[0] != '.' && origin[1] != '\0' &&
129 name[len] == '\0')) && name[len - 1] != '.') {
131 T(addstr(".", 1, &buf, &buflen));
134 T(spaced = addtab(len, 24, spaced, &buf, &buflen));
141 T(x = ns_format_ttl(ttl, buf, buflen));
142 addlen(x, &buf, &buflen);
143 len = SPRINTF((tmp, " %s %s", p_class(class), p_type(type)));
144 T(addstr(tmp, len, &buf, &buflen));
145 T(spaced = addtab(x + len, 16, spaced, &buf, &buflen));
152 if (rdlen != (size_t)NS_INADDRSZ)
154 (void) inet_ntop(AF_INET, rdata, buf, buflen);
155 addlen(strlen(buf), &buf, &buflen);
165 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
171 T(len = charstr(rdata, edata, &buf, &buflen));
175 T(addstr(" ", 1, &buf, &buflen));
178 /* Second word, optional in ISDN records. */
179 if (type == ns_t_isdn && rdata == edata)
182 T(len = charstr(rdata, edata, &buf, &buflen));
192 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
193 T(addstr(" ", 1, &buf, &buflen));
195 /* Administrator name. */
196 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
197 T(addstr(" (\n", 3, &buf, &buflen));
200 if ((edata - rdata) != 5*NS_INT32SZ)
204 t = ns_get32(rdata); rdata += NS_INT32SZ;
205 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
206 len = SPRINTF((tmp, "%lu", t));
207 T(addstr(tmp, len, &buf, &buflen));
208 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
209 T(addstr("; serial\n", 9, &buf, &buflen));
212 /* Refresh interval. */
213 t = ns_get32(rdata); rdata += NS_INT32SZ;
214 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
215 T(len = ns_format_ttl(t, buf, buflen));
216 addlen(len, &buf, &buflen);
217 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
218 T(addstr("; refresh\n", 10, &buf, &buflen));
221 /* Retry interval. */
222 t = ns_get32(rdata); rdata += NS_INT32SZ;
223 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
224 T(len = ns_format_ttl(t, buf, buflen));
225 addlen(len, &buf, &buflen);
226 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
227 T(addstr("; retry\n", 8, &buf, &buflen));
231 t = ns_get32(rdata); rdata += NS_INT32SZ;
232 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
233 T(len = ns_format_ttl(t, buf, buflen));
234 addlen(len, &buf, &buflen);
235 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
236 T(addstr("; expiry\n", 9, &buf, &buflen));
240 t = ns_get32(rdata); rdata += NS_INT32SZ;
241 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
242 T(len = ns_format_ttl(t, buf, buflen));
243 addlen(len, &buf, &buflen);
244 T(addstr(" )", 2, &buf, &buflen));
245 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
246 T(addstr("; minimum\n", 10, &buf, &buflen));
256 if (rdlen < (size_t)NS_INT16SZ)
262 len = SPRINTF((tmp, "%u ", t));
263 T(addstr(tmp, len, &buf, &buflen));
266 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
274 if (rdlen < (size_t)NS_INT16SZ)
280 len = SPRINTF((tmp, "%u ", t));
281 T(addstr(tmp, len, &buf, &buflen));
284 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
285 T(addstr(" ", 1, &buf, &buflen));
288 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
294 T(len = charstr(rdata, edata, &buf, &buflen));
301 while (rdata < edata) {
302 T(len = charstr(rdata, edata, &buf, &buflen));
307 T(addstr(" ", 1, &buf, &buflen));
314 (void) inet_nsap_ntoa(rdlen, rdata, t);
315 T(addstr(t, strlen(t), &buf, &buflen));
320 if (rdlen != (size_t)NS_IN6ADDRSZ)
322 (void) inet_ntop(AF_INET6, rdata, buf, buflen);
323 addlen(strlen(buf), &buf, &buflen);
329 /* XXX protocol format checking? */
330 (void) loc_ntoa(rdata, t);
331 T(addstr(t, strlen(t), &buf, &buflen));
336 u_int order, preference;
339 if (rdlen < 2U*NS_INT16SZ)
342 /* Order, Precedence. */
343 order = ns_get16(rdata); rdata += NS_INT16SZ;
344 preference = ns_get16(rdata); rdata += NS_INT16SZ;
345 len = SPRINTF((t, "%u %u ", order, preference));
346 T(addstr(t, len, &buf, &buflen));
349 T(len = charstr(rdata, edata, &buf, &buflen));
353 T(addstr(" ", 1, &buf, &buflen));
356 T(len = charstr(rdata, edata, &buf, &buflen));
360 T(addstr(" ", 1, &buf, &buflen));
363 T(len = charstr(rdata, edata, &buf, &buflen));
369 T(addstr(" ", 1, &buf, &buflen));
372 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
377 u_int priority, weight, port;
380 if (rdlen < 3U*NS_INT16SZ)
383 /* Priority, Weight, Port. */
384 priority = ns_get16(rdata); rdata += NS_INT16SZ;
385 weight = ns_get16(rdata); rdata += NS_INT16SZ;
386 port = ns_get16(rdata); rdata += NS_INT16SZ;
387 len = SPRINTF((t, "%u %u %u ", priority, weight, port));
388 T(addstr(t, len, &buf, &buflen));
391 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
398 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
399 T(addstr(" ", 1, &buf, &buflen));
402 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
409 if (rdlen < 1U + NS_INT32SZ)
413 (void) inet_ntop(AF_INET, rdata, buf, buflen);
414 addlen(strlen(buf), &buf, &buflen);
415 rdata += NS_INADDRSZ;
418 len = SPRINTF((tmp, " %u ( ", *rdata));
419 T(addstr(tmp, len, &buf, &buflen));
425 while (rdata < edata) {
430 T(addstr("\n\t\t\t\t", 5,
435 len = SPRINTF((tmp, "%d ", n));
436 T(addstr(tmp, len, &buf, &buflen));
442 T(addstr(")", 1, &buf, &buflen));
448 char base64_key[NS_MD5RSA_MAX_BASE64];
449 u_int keyflags, protocol, algorithm, key_id;
453 if (rdlen < 0U + NS_INT16SZ + NS_INT8SZ + NS_INT8SZ)
456 /* Key flags, Protocol, Algorithm. */
457 key_id = dst_s_dns_key_id(rdata, edata-rdata);
458 keyflags = ns_get16(rdata); rdata += NS_INT16SZ;
460 algorithm = *rdata++;
461 len = SPRINTF((tmp, "0x%04x %u %u",
462 keyflags, protocol, algorithm));
463 T(addstr(tmp, len, &buf, &buflen));
465 /* Public key data. */
466 len = b64_ntop(rdata, edata - rdata,
467 base64_key, sizeof base64_key);
471 T(addstr(" (", 2, &buf, &buflen));
476 for (n = 0; n < len; n += 48) {
477 T(addstr(leader, strlen(leader), &buf, &buflen));
478 T(addstr(base64_key + n, MIN(len - n, 48),
482 T(addstr(" )", 2, &buf, &buflen));
483 n = SPRINTF((tmp, " ; key_tag= %u", key_id));
484 T(addstr(tmp, n, &buf, &buflen));
490 char base64_key[NS_MD5RSA_MAX_BASE64];
491 u_int type, algorithm, labels, footprint;
499 /* Type covered, Algorithm, Label count, Original TTL. */
500 type = ns_get16(rdata); rdata += NS_INT16SZ;
501 algorithm = *rdata++;
503 t = ns_get32(rdata); rdata += NS_INT32SZ;
504 len = SPRINTF((tmp, "%s %d %d %lu ",
505 p_type(type), algorithm, labels, t));
506 T(addstr(tmp, len, &buf, &buflen));
507 if (labels > (u_int)dn_count_labels(name))
510 /* Signature expiry. */
511 t = ns_get32(rdata); rdata += NS_INT32SZ;
512 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
513 T(addstr(tmp, len, &buf, &buflen));
516 t = ns_get32(rdata); rdata += NS_INT32SZ;
517 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
518 T(addstr(tmp, len, &buf, &buflen));
520 /* Signature Footprint. */
521 footprint = ns_get16(rdata); rdata += NS_INT16SZ;
522 len = SPRINTF((tmp, "%u ", footprint));
523 T(addstr(tmp, len, &buf, &buflen));
526 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
529 len = b64_ntop(rdata, edata - rdata,
530 base64_key, sizeof base64_key);
532 T(addstr(" (", 2, &buf, &buflen));
539 for (n = 0; n < len; n += 48) {
540 T(addstr(leader, strlen(leader), &buf, &buflen));
541 T(addstr(base64_key + n, MIN(len - n, 48),
545 T(addstr(" )", 2, &buf, &buflen));
552 /* Next domain name. */
553 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
557 for (c = 0; c < n*8; c++)
558 if (NS_NXT_BIT_ISSET(c, rdata)) {
559 len = SPRINTF((tmp, " %s", p_type(c)));
560 T(addstr(tmp, len, &buf, &buflen));
566 u_int c_type, key_tag, alg;
569 char base64_cert[8192], tmp[40];
572 c_type = ns_get16(rdata); rdata += NS_INT16SZ;
573 key_tag = ns_get16(rdata); rdata += NS_INT16SZ;
574 alg = (u_int) *rdata++;
576 len = SPRINTF((tmp, "%d %d %d ", c_type, key_tag, alg));
577 T(addstr(tmp, len, &buf, &buflen));
578 siz = (edata-rdata)*4/3 + 4; /* "+4" accounts for trailing \0 */
579 if (siz > sizeof(base64_cert) * 3/4) {
580 const char *str = "record too long to print";
581 T(addstr(str, strlen(str), &buf, &buflen));
584 len = b64_ntop(rdata, edata-rdata, base64_cert, siz);
589 T(addstr(" (", 2, &buf, &buflen));
596 for (n = 0; n < len; n += 48) {
597 T(addstr(leader, strlen(leader),
599 T(addstr(base64_cert + n, MIN(len - n, 48),
603 T(addstr(" )", 2, &buf, &buflen));
609 /* KJD - need to complete this */
611 int mode, err, keysize;
613 /* Algorithm name. */
614 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
615 T(addstr(" ", 1, &buf, &buflen));
618 t = ns_get32(rdata); rdata += NS_INT32SZ;
619 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
620 T(addstr(tmp, len, &buf, &buflen));
623 t = ns_get32(rdata); rdata += NS_INT32SZ;
624 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
625 T(addstr(tmp, len, &buf, &buflen));
627 /* Mode , Error, Key Size. */
628 /* Priority, Weight, Port. */
629 mode = ns_get16(rdata); rdata += NS_INT16SZ;
630 err = ns_get16(rdata); rdata += NS_INT16SZ;
631 keysize = ns_get16(rdata); rdata += NS_INT16SZ;
632 len = SPRINTF((tmp, "%u %u %u ", mode, err, keysize));
633 T(addstr(tmp, len, &buf, &buflen));
635 /* XXX need to dump key, print otherdata length & other data */
640 /* BEW - need to complete this */
643 T(len = addname(msg, msglen, &rdata, origin, &buf, &buflen));
644 T(addstr(" ", 1, &buf, &buflen));
645 rdata += 8; /*%< time */
646 n = ns_get16(rdata); rdata += INT16SZ;
647 rdata += n; /*%< sig */
648 n = ns_get16(rdata); rdata += INT16SZ; /*%< original id */
649 sprintf(buf, "%d", ns_get16(rdata));
651 addlen(strlen(buf), &buf, &buflen);
660 if (rdlen == 0U) goto formerr;
661 len = SPRINTF((tmp, "%d ", *rdata));
662 T(addstr(tmp, len, &buf, &buflen));
664 if (pbit > 128) goto formerr;
665 pbyte = (pbit & ~7) / 8;
668 /* address suffix: provided only when prefix len != 128 */
670 if (rdata + pbyte >= edata) goto formerr;
671 memset(&a, 0, sizeof(a));
672 memcpy(&a.s6_addr[pbyte], rdata, sizeof(a) - pbyte);
673 (void) inet_ntop(AF_INET6, &a, buf, buflen);
674 addlen(strlen(buf), &buf, &buflen);
675 rdata += sizeof(a) - pbyte;
678 /* prefix name: provided only when prefix len > 0 */
681 if (rdata >= edata) goto formerr;
682 T(addstr(" ", 1, &buf, &buflen));
683 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
689 len = SPRINTF((tmp, "%u bytes", class));
690 T(addstr(tmp, len, &buf, &buflen));
695 comment = "unknown RR type";
700 comment = "RR format error";
705 len = SPRINTF((tmp, "\\# %u%s\t; %s", (unsigned)(edata - rdata),
706 rdlen != 0U ? " (" : "", comment));
707 T(addstr(tmp, len, &buf, &buflen));
708 while (rdata < edata) {
710 p += SPRINTF((p, "\n\t"));
712 n = MIN(16, edata - rdata);
713 for (m = 0; m < n; m++)
714 p += SPRINTF((p, "%02x ", rdata[m]));
715 T(addstr(tmp, p - tmp, &buf, &buflen));
717 T(addstr(")", 1, &buf, &buflen));
718 T(addtab(p - tmp + 1, 48, spaced, &buf, &buflen));
721 p += SPRINTF((p, "; "));
722 for (m = 0; m < n; m++)
723 *p++ = (isascii(rdata[m]) && isprint(rdata[m]))
726 T(addstr(tmp, p - tmp, &buf, &buflen));
737 * prune_origin(name, origin)
738 * Find out if the name is at or under the current origin.
740 * Number of characters in name before start of origin,
741 * or length of name if origin does not match.
743 * This function should share code with samedomain().
746 prune_origin(const char *name, const char *origin) {
747 const char *oname = name;
749 while (*name != '\0') {
750 if (origin != NULL && ns_samename(name, origin) == 1)
751 return (name - oname - (name > oname));
752 while (*name != '\0') {
755 /* XXX need to handle \nnn form. */
758 } else if (*name == '.') {
765 return (name - oname);
770 * charstr(rdata, edata, buf, buflen)
771 * Format a <character-string> into the presentation buffer.
773 * Number of rdata octets consumed
774 * 0 for protocol format error
775 * -1 for output buffer error
777 * buffer is advanced on success.
780 charstr(const u_char *rdata, const u_char *edata, char **buf, size_t *buflen) {
781 const u_char *odata = rdata;
782 size_t save_buflen = *buflen;
783 char *save_buf = *buf;
785 if (addstr("\"", 1, buf, buflen) < 0)
790 if (rdata + 1 + n <= edata) {
793 if (strchr("\n\"\\", *rdata) != NULL)
794 if (addstr("\\", 1, buf, buflen) < 0)
796 if (addstr((const char *)rdata, 1,
803 if (addstr("\"", 1, buf, buflen) < 0)
805 return (rdata - odata);
809 *buflen = save_buflen;
814 addname(const u_char *msg, size_t msglen,
815 const u_char **pp, const char *origin,
816 char **buf, size_t *buflen)
818 size_t newlen, save_buflen = *buflen;
819 char *save_buf = *buf;
822 n = dn_expand(msg, msg + msglen, *pp, *buf, *buflen);
824 goto enospc; /*%< Guess. */
825 newlen = prune_origin(*buf, origin);
828 } else if (newlen == 0U) {
829 /* Use "@" instead of name. */
830 if (newlen + 2 > *buflen)
831 goto enospc; /* No room for "@\0". */
832 (*buf)[newlen++] = '@';
833 (*buf)[newlen] = '\0';
835 if (((origin == NULL || origin[0] == '\0') ||
836 (origin[0] != '.' && origin[1] != '\0' &&
837 (*buf)[newlen] == '\0')) && (*buf)[newlen - 1] != '.') {
838 /* No trailing dot. */
840 if (newlen + 2 > *buflen)
841 goto enospc; /* No room for ".\0". */
842 (*buf)[newlen++] = '.';
843 (*buf)[newlen] = '\0';
847 addlen(newlen, buf, buflen);
853 *buflen = save_buflen;
858 addlen(size_t len, char **buf, size_t *buflen) {
859 INSIST(len <= *buflen);
865 addstr(const char *src, size_t len, char **buf, size_t *buflen) {
866 if (len >= *buflen) {
870 memcpy(*buf, src, len);
871 addlen(len, buf, buflen);
877 addtab(size_t len, size_t target, int spaced, char **buf, size_t *buflen) {
878 size_t save_buflen = *buflen;
879 char *save_buf = *buf;
882 if (spaced || len >= target - 1) {
883 T(addstr(" ", 2, buf, buflen));
886 for (t = (target - len - 1) / 8; t >= 0; t--)
887 if (addstr("\t", 1, buf, buflen) < 0) {
888 *buflen = save_buflen;