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>
35 #define INSIST(cond) assert(cond)
37 #include <isc/assertions.h>
45 #include "port_after.h"
48 # define SPRINTF(x) strlen(sprintf/**/x)
50 # define SPRINTF(x) ((size_t)sprintf x)
55 static size_t prune_origin(const char *name, const char *origin);
56 static int charstr(const u_char *rdata, const u_char *edata,
57 char **buf, size_t *buflen);
58 static int addname(const u_char *msg, size_t msglen,
59 const u_char **p, const char *origin,
60 char **buf, size_t *buflen);
61 static void addlen(size_t len, char **buf, size_t *buflen);
62 static int addstr(const char *src, size_t len,
63 char **buf, size_t *buflen);
64 static int addtab(size_t len, size_t target, int spaced,
65 char **buf, size_t *buflen);
78 * Convert an RR to presentation format.
81 *\li Number of characters written to buf, or -1 (check errno).
84 ns_sprintrr(const ns_msg *handle, const ns_rr *rr,
85 const char *name_ctx, const char *origin,
86 char *buf, size_t buflen)
90 n = ns_sprintrrf(ns_msg_base(*handle), ns_msg_size(*handle),
91 ns_rr_name(*rr), ns_rr_class(*rr), ns_rr_type(*rr),
92 ns_rr_ttl(*rr), ns_rr_rdata(*rr), ns_rr_rdlen(*rr),
93 name_ctx, origin, buf, buflen);
98 * Convert the fields of an RR into presentation format.
101 *\li Number of characters written to buf, or -1 (check errno).
104 ns_sprintrrf(const u_char *msg, size_t msglen,
105 const char *name, ns_class class, ns_type type,
106 u_long ttl, const u_char *rdata, size_t rdlen,
107 const char *name_ctx, const char *origin,
108 char *buf, size_t buflen)
110 const char *obuf = buf;
111 const u_char *edata = rdata + rdlen;
121 if (name_ctx != NULL && ns_samename(name_ctx, name) == 1) {
122 T(addstr("\t\t\t", 3, &buf, &buflen));
124 len = prune_origin(name, origin);
127 } else if (len == 0) {
128 T(addstr("@\t\t\t", 4, &buf, &buflen));
130 T(addstr(name, len, &buf, &buflen));
131 /* Origin not used or not root, and no trailing dot? */
132 if (((origin == NULL || origin[0] == '\0') ||
133 (origin[0] != '.' && origin[1] != '\0' &&
134 name[len] == '\0')) && name[len - 1] != '.') {
136 T(addstr(".", 1, &buf, &buflen));
139 T(spaced = addtab(len, 24, spaced, &buf, &buflen));
146 T(x = ns_format_ttl(ttl, buf, buflen));
147 addlen(x, &buf, &buflen);
148 len = SPRINTF((tmp, " %s %s", p_class(class), p_type(type)));
149 T(addstr(tmp, len, &buf, &buflen));
150 T(spaced = addtab(x + len, 16, spaced, &buf, &buflen));
157 if (rdlen != (size_t)NS_INADDRSZ)
159 (void) inet_ntop(AF_INET, rdata, buf, buflen);
160 addlen(strlen(buf), &buf, &buflen);
170 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
176 T(len = charstr(rdata, edata, &buf, &buflen));
180 T(addstr(" ", 1, &buf, &buflen));
183 /* Second word, optional in ISDN records. */
184 if (type == ns_t_isdn && rdata == edata)
187 T(len = charstr(rdata, edata, &buf, &buflen));
197 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
198 T(addstr(" ", 1, &buf, &buflen));
200 /* Administrator name. */
201 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
202 T(addstr(" (\n", 3, &buf, &buflen));
205 if ((edata - rdata) != 5*NS_INT32SZ)
209 t = ns_get32(rdata); rdata += NS_INT32SZ;
210 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
211 len = SPRINTF((tmp, "%lu", t));
212 T(addstr(tmp, len, &buf, &buflen));
213 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
214 T(addstr("; serial\n", 9, &buf, &buflen));
217 /* Refresh interval. */
218 t = ns_get32(rdata); rdata += NS_INT32SZ;
219 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
220 T(len = ns_format_ttl(t, buf, buflen));
221 addlen(len, &buf, &buflen);
222 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
223 T(addstr("; refresh\n", 10, &buf, &buflen));
226 /* Retry interval. */
227 t = ns_get32(rdata); rdata += NS_INT32SZ;
228 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
229 T(len = ns_format_ttl(t, buf, buflen));
230 addlen(len, &buf, &buflen);
231 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
232 T(addstr("; retry\n", 8, &buf, &buflen));
236 t = ns_get32(rdata); rdata += NS_INT32SZ;
237 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
238 T(len = ns_format_ttl(t, buf, buflen));
239 addlen(len, &buf, &buflen);
240 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
241 T(addstr("; expiry\n", 9, &buf, &buflen));
245 t = ns_get32(rdata); rdata += NS_INT32SZ;
246 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
247 T(len = ns_format_ttl(t, buf, buflen));
248 addlen(len, &buf, &buflen);
249 T(addstr(" )", 2, &buf, &buflen));
250 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
251 T(addstr("; minimum\n", 10, &buf, &buflen));
261 if (rdlen < (size_t)NS_INT16SZ)
267 len = SPRINTF((tmp, "%u ", t));
268 T(addstr(tmp, len, &buf, &buflen));
271 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
279 if (rdlen < (size_t)NS_INT16SZ)
285 len = SPRINTF((tmp, "%u ", t));
286 T(addstr(tmp, len, &buf, &buflen));
289 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
290 T(addstr(" ", 1, &buf, &buflen));
293 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
299 T(len = charstr(rdata, edata, &buf, &buflen));
306 while (rdata < edata) {
307 T(len = charstr(rdata, edata, &buf, &buflen));
312 T(addstr(" ", 1, &buf, &buflen));
319 (void) inet_nsap_ntoa(rdlen, rdata, t);
320 T(addstr(t, strlen(t), &buf, &buflen));
325 if (rdlen != (size_t)NS_IN6ADDRSZ)
327 (void) inet_ntop(AF_INET6, rdata, buf, buflen);
328 addlen(strlen(buf), &buf, &buflen);
334 /* XXX protocol format checking? */
335 (void) loc_ntoa(rdata, t);
336 T(addstr(t, strlen(t), &buf, &buflen));
341 u_int order, preference;
344 if (rdlen < 2U*NS_INT16SZ)
347 /* Order, Precedence. */
348 order = ns_get16(rdata); rdata += NS_INT16SZ;
349 preference = ns_get16(rdata); rdata += NS_INT16SZ;
350 len = SPRINTF((t, "%u %u ", order, preference));
351 T(addstr(t, len, &buf, &buflen));
354 T(len = charstr(rdata, edata, &buf, &buflen));
358 T(addstr(" ", 1, &buf, &buflen));
361 T(len = charstr(rdata, edata, &buf, &buflen));
365 T(addstr(" ", 1, &buf, &buflen));
368 T(len = charstr(rdata, edata, &buf, &buflen));
374 T(addstr(" ", 1, &buf, &buflen));
377 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
382 u_int priority, weight, port;
385 if (rdlen < 3U*NS_INT16SZ)
388 /* Priority, Weight, Port. */
389 priority = ns_get16(rdata); rdata += NS_INT16SZ;
390 weight = ns_get16(rdata); rdata += NS_INT16SZ;
391 port = ns_get16(rdata); rdata += NS_INT16SZ;
392 len = SPRINTF((t, "%u %u %u ", priority, weight, port));
393 T(addstr(t, len, &buf, &buflen));
396 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
403 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
404 T(addstr(" ", 1, &buf, &buflen));
407 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
414 if (rdlen < 1U + NS_INT32SZ)
418 (void) inet_ntop(AF_INET, rdata, buf, buflen);
419 addlen(strlen(buf), &buf, &buflen);
420 rdata += NS_INADDRSZ;
423 len = SPRINTF((tmp, " %u ( ", *rdata));
424 T(addstr(tmp, len, &buf, &buflen));
430 while (rdata < edata) {
435 T(addstr("\n\t\t\t\t", 5,
440 len = SPRINTF((tmp, "%d ", n));
441 T(addstr(tmp, len, &buf, &buflen));
447 T(addstr(")", 1, &buf, &buflen));
453 char base64_key[NS_MD5RSA_MAX_BASE64];
454 u_int keyflags, protocol, algorithm, key_id;
458 if (rdlen < 0U + NS_INT16SZ + NS_INT8SZ + NS_INT8SZ)
461 /* Key flags, Protocol, Algorithm. */
463 key_id = dst_s_dns_key_id(rdata, edata-rdata);
467 keyflags = ns_get16(rdata); rdata += NS_INT16SZ;
469 algorithm = *rdata++;
470 len = SPRINTF((tmp, "0x%04x %u %u",
471 keyflags, protocol, algorithm));
472 T(addstr(tmp, len, &buf, &buflen));
474 /* Public key data. */
475 len = b64_ntop(rdata, edata - rdata,
476 base64_key, sizeof base64_key);
480 T(addstr(" (", 2, &buf, &buflen));
485 for (n = 0; n < len; n += 48) {
486 T(addstr(leader, strlen(leader), &buf, &buflen));
487 T(addstr(base64_key + n, MIN(len - n, 48),
491 T(addstr(" )", 2, &buf, &buflen));
492 n = SPRINTF((tmp, " ; key_tag= %u", key_id));
493 T(addstr(tmp, n, &buf, &buflen));
499 char base64_key[NS_MD5RSA_MAX_BASE64];
500 u_int type, algorithm, labels, footprint;
508 /* Type covered, Algorithm, Label count, Original TTL. */
509 type = ns_get16(rdata); rdata += NS_INT16SZ;
510 algorithm = *rdata++;
512 t = ns_get32(rdata); rdata += NS_INT32SZ;
513 len = SPRINTF((tmp, "%s %d %d %lu ",
514 p_type(type), algorithm, labels, t));
515 T(addstr(tmp, len, &buf, &buflen));
516 if (labels > (u_int)dn_count_labels(name))
519 /* Signature expiry. */
520 t = ns_get32(rdata); rdata += NS_INT32SZ;
521 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
522 T(addstr(tmp, len, &buf, &buflen));
525 t = ns_get32(rdata); rdata += NS_INT32SZ;
526 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
527 T(addstr(tmp, len, &buf, &buflen));
529 /* Signature Footprint. */
530 footprint = ns_get16(rdata); rdata += NS_INT16SZ;
531 len = SPRINTF((tmp, "%u ", footprint));
532 T(addstr(tmp, len, &buf, &buflen));
535 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
538 len = b64_ntop(rdata, edata - rdata,
539 base64_key, sizeof base64_key);
541 T(addstr(" (", 2, &buf, &buflen));
548 for (n = 0; n < len; n += 48) {
549 T(addstr(leader, strlen(leader), &buf, &buflen));
550 T(addstr(base64_key + n, MIN(len - n, 48),
554 T(addstr(" )", 2, &buf, &buflen));
561 /* Next domain name. */
562 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
566 for (c = 0; c < n*8; c++)
567 if (NS_NXT_BIT_ISSET(c, rdata)) {
568 len = SPRINTF((tmp, " %s", p_type(c)));
569 T(addstr(tmp, len, &buf, &buflen));
575 u_int c_type, key_tag, alg;
578 char base64_cert[8192], tmp[40];
581 c_type = ns_get16(rdata); rdata += NS_INT16SZ;
582 key_tag = ns_get16(rdata); rdata += NS_INT16SZ;
583 alg = (u_int) *rdata++;
585 len = SPRINTF((tmp, "%d %d %d ", c_type, key_tag, alg));
586 T(addstr(tmp, len, &buf, &buflen));
587 siz = (edata-rdata)*4/3 + 4; /* "+4" accounts for trailing \0 */
588 if (siz > sizeof(base64_cert) * 3/4) {
589 const char *str = "record too long to print";
590 T(addstr(str, strlen(str), &buf, &buflen));
593 len = b64_ntop(rdata, edata-rdata, base64_cert, siz);
598 T(addstr(" (", 2, &buf, &buflen));
605 for (n = 0; n < len; n += 48) {
606 T(addstr(leader, strlen(leader),
608 T(addstr(base64_cert + n, MIN(len - n, 48),
612 T(addstr(" )", 2, &buf, &buflen));
618 /* KJD - need to complete this */
620 int mode, err, keysize;
622 /* Algorithm name. */
623 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
624 T(addstr(" ", 1, &buf, &buflen));
627 t = ns_get32(rdata); rdata += NS_INT32SZ;
628 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
629 T(addstr(tmp, len, &buf, &buflen));
632 t = ns_get32(rdata); rdata += NS_INT32SZ;
633 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
634 T(addstr(tmp, len, &buf, &buflen));
636 /* Mode , Error, Key Size. */
637 /* Priority, Weight, Port. */
638 mode = ns_get16(rdata); rdata += NS_INT16SZ;
639 err = ns_get16(rdata); rdata += NS_INT16SZ;
640 keysize = ns_get16(rdata); rdata += NS_INT16SZ;
641 len = SPRINTF((tmp, "%u %u %u ", mode, err, keysize));
642 T(addstr(tmp, len, &buf, &buflen));
644 /* XXX need to dump key, print otherdata length & other data */
649 /* BEW - need to complete this */
652 T(len = addname(msg, msglen, &rdata, origin, &buf, &buflen));
653 T(addstr(" ", 1, &buf, &buflen));
654 rdata += 8; /*%< time */
655 n = ns_get16(rdata); rdata += INT16SZ;
656 rdata += n; /*%< sig */
657 n = ns_get16(rdata); rdata += INT16SZ; /*%< original id */
658 sprintf(buf, "%d", ns_get16(rdata));
660 addlen(strlen(buf), &buf, &buflen);
669 if (rdlen == 0U) goto formerr;
670 len = SPRINTF((tmp, "%d ", *rdata));
671 T(addstr(tmp, len, &buf, &buflen));
673 if (pbit > 128) goto formerr;
674 pbyte = (pbit & ~7) / 8;
677 /* address suffix: provided only when prefix len != 128 */
679 if (rdata + pbyte >= edata) goto formerr;
680 memset(&a, 0, sizeof(a));
681 memcpy(&a.s6_addr[pbyte], rdata, sizeof(a) - pbyte);
682 (void) inet_ntop(AF_INET6, &a, buf, buflen);
683 addlen(strlen(buf), &buf, &buflen);
684 rdata += sizeof(a) - pbyte;
687 /* prefix name: provided only when prefix len > 0 */
690 if (rdata >= edata) goto formerr;
691 T(addstr(" ", 1, &buf, &buflen));
692 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
698 len = SPRINTF((tmp, "%u bytes", class));
699 T(addstr(tmp, len, &buf, &buflen));
704 comment = "unknown RR type";
709 comment = "RR format error";
714 len = SPRINTF((tmp, "\\# %u%s\t; %s", (unsigned)(edata - rdata),
715 rdlen != 0U ? " (" : "", comment));
716 T(addstr(tmp, len, &buf, &buflen));
717 while (rdata < edata) {
719 p += SPRINTF((p, "\n\t"));
721 n = MIN(16, edata - rdata);
722 for (m = 0; m < n; m++)
723 p += SPRINTF((p, "%02x ", rdata[m]));
724 T(addstr(tmp, p - tmp, &buf, &buflen));
726 T(addstr(")", 1, &buf, &buflen));
727 T(addtab(p - tmp + 1, 48, spaced, &buf, &buflen));
730 p += SPRINTF((p, "; "));
731 for (m = 0; m < n; m++)
732 *p++ = (isascii(rdata[m]) && isprint(rdata[m]))
735 T(addstr(tmp, p - tmp, &buf, &buflen));
746 * prune_origin(name, origin)
747 * Find out if the name is at or under the current origin.
749 * Number of characters in name before start of origin,
750 * or length of name if origin does not match.
752 * This function should share code with samedomain().
755 prune_origin(const char *name, const char *origin) {
756 const char *oname = name;
758 while (*name != '\0') {
759 if (origin != NULL && ns_samename(name, origin) == 1)
760 return (name - oname - (name > oname));
761 while (*name != '\0') {
764 /* XXX need to handle \nnn form. */
767 } else if (*name == '.') {
774 return (name - oname);
779 * charstr(rdata, edata, buf, buflen)
780 * Format a <character-string> into the presentation buffer.
782 * Number of rdata octets consumed
783 * 0 for protocol format error
784 * -1 for output buffer error
786 * buffer is advanced on success.
789 charstr(const u_char *rdata, const u_char *edata, char **buf, size_t *buflen) {
790 const u_char *odata = rdata;
791 size_t save_buflen = *buflen;
792 char *save_buf = *buf;
794 if (addstr("\"", 1, buf, buflen) < 0)
799 if (rdata + 1 + n <= edata) {
802 if (strchr("\n\"\\", *rdata) != NULL)
803 if (addstr("\\", 1, buf, buflen) < 0)
805 if (addstr((const char *)rdata, 1,
812 if (addstr("\"", 1, buf, buflen) < 0)
814 return (rdata - odata);
818 *buflen = save_buflen;
823 addname(const u_char *msg, size_t msglen,
824 const u_char **pp, const char *origin,
825 char **buf, size_t *buflen)
827 size_t newlen, save_buflen = *buflen;
828 char *save_buf = *buf;
831 n = dn_expand(msg, msg + msglen, *pp, *buf, *buflen);
833 goto enospc; /*%< Guess. */
834 newlen = prune_origin(*buf, origin);
837 } else if (newlen == 0U) {
838 /* Use "@" instead of name. */
839 if (newlen + 2 > *buflen)
840 goto enospc; /* No room for "@\0". */
841 (*buf)[newlen++] = '@';
842 (*buf)[newlen] = '\0';
844 if (((origin == NULL || origin[0] == '\0') ||
845 (origin[0] != '.' && origin[1] != '\0' &&
846 (*buf)[newlen] == '\0')) && (*buf)[newlen - 1] != '.') {
847 /* No trailing dot. */
849 if (newlen + 2 > *buflen)
850 goto enospc; /* No room for ".\0". */
851 (*buf)[newlen++] = '.';
852 (*buf)[newlen] = '\0';
856 addlen(newlen, buf, buflen);
862 *buflen = save_buflen;
867 addlen(size_t len, char **buf, size_t *buflen) {
868 INSIST(len <= *buflen);
874 addstr(const char *src, size_t len, char **buf, size_t *buflen) {
875 if (len >= *buflen) {
879 memcpy(*buf, src, len);
880 addlen(len, buf, buflen);
886 addtab(size_t len, size_t target, int spaced, char **buf, size_t *buflen) {
887 size_t save_buflen = *buflen;
888 char *save_buf = *buf;
891 if (spaced || len >= target - 1) {
892 T(addstr(" ", 2, buf, buflen));
895 for (t = (target - len - 1) / 8; t >= 0; t--)
896 if (addstr("\t", 1, buf, buflen) < 0) {
897 *buflen = save_buflen;