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.3.2.1.4.7 2004/09/16 07:01:12 marka 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);
79 * ns_sprintrr(handle, rr, name_ctx, origin, buf, buflen)
80 * Convert an RR to presentation format.
82 * Number of characters written to buf, or -1 (check errno).
85 ns_sprintrr(const ns_msg *handle, const ns_rr *rr,
86 const char *name_ctx, const char *origin,
87 char *buf, size_t buflen)
91 n = ns_sprintrrf(ns_msg_base(*handle), ns_msg_size(*handle),
92 ns_rr_name(*rr), ns_rr_class(*rr), ns_rr_type(*rr),
93 ns_rr_ttl(*rr), ns_rr_rdata(*rr), ns_rr_rdlen(*rr),
94 name_ctx, origin, buf, buflen);
100 * ns_sprintrrf(msg, msglen, name, class, type, ttl, rdata, rdlen,
101 * name_ctx, origin, buf, buflen)
102 * Convert the fields of an RR into presentation format.
104 * Number of characters written to buf, or -1 (check errno).
107 ns_sprintrrf(const u_char *msg, size_t msglen,
108 const char *name, ns_class class, ns_type type,
109 u_long ttl, const u_char *rdata, size_t rdlen,
110 const char *name_ctx, const char *origin,
111 char *buf, size_t buflen)
113 const char *obuf = buf;
114 const u_char *edata = rdata + rdlen;
124 if (name_ctx != NULL && ns_samename(name_ctx, name) == 1) {
125 T(addstr("\t\t\t", 3, &buf, &buflen));
127 len = prune_origin(name, origin);
130 } else if (len == 0) {
131 T(addstr("@\t\t\t", 4, &buf, &buflen));
133 T(addstr(name, len, &buf, &buflen));
134 /* Origin not used or not root, and no trailing dot? */
135 if (((origin == NULL || origin[0] == '\0') ||
136 (origin[0] != '.' && origin[1] != '\0' &&
137 name[len] == '\0')) && name[len - 1] != '.') {
139 T(addstr(".", 1, &buf, &buflen));
142 T(spaced = addtab(len, 24, spaced, &buf, &buflen));
149 T(x = ns_format_ttl(ttl, buf, buflen));
150 addlen(x, &buf, &buflen);
151 len = SPRINTF((tmp, " %s %s", p_class(class), p_type(type)));
152 T(addstr(tmp, len, &buf, &buflen));
153 T(spaced = addtab(x + len, 16, spaced, &buf, &buflen));
160 if (rdlen != (size_t)NS_INADDRSZ)
162 (void) inet_ntop(AF_INET, rdata, buf, buflen);
163 addlen(strlen(buf), &buf, &buflen);
173 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
179 T(len = charstr(rdata, edata, &buf, &buflen));
183 T(addstr(" ", 1, &buf, &buflen));
186 /* Second word, optional in ISDN records. */
187 if (type == ns_t_isdn && rdata == edata)
190 T(len = charstr(rdata, edata, &buf, &buflen));
200 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
201 T(addstr(" ", 1, &buf, &buflen));
203 /* Administrator name. */
204 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
205 T(addstr(" (\n", 3, &buf, &buflen));
208 if ((edata - rdata) != 5*NS_INT32SZ)
212 t = ns_get32(rdata); rdata += NS_INT32SZ;
213 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
214 len = SPRINTF((tmp, "%lu", t));
215 T(addstr(tmp, len, &buf, &buflen));
216 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
217 T(addstr("; serial\n", 9, &buf, &buflen));
220 /* Refresh interval. */
221 t = ns_get32(rdata); rdata += NS_INT32SZ;
222 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
223 T(len = ns_format_ttl(t, buf, buflen));
224 addlen(len, &buf, &buflen);
225 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
226 T(addstr("; refresh\n", 10, &buf, &buflen));
229 /* Retry interval. */
230 t = ns_get32(rdata); rdata += NS_INT32SZ;
231 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
232 T(len = ns_format_ttl(t, buf, buflen));
233 addlen(len, &buf, &buflen);
234 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
235 T(addstr("; retry\n", 8, &buf, &buflen));
239 t = ns_get32(rdata); rdata += NS_INT32SZ;
240 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
241 T(len = ns_format_ttl(t, buf, buflen));
242 addlen(len, &buf, &buflen);
243 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
244 T(addstr("; expiry\n", 9, &buf, &buflen));
248 t = ns_get32(rdata); rdata += NS_INT32SZ;
249 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
250 T(len = ns_format_ttl(t, buf, buflen));
251 addlen(len, &buf, &buflen);
252 T(addstr(" )", 2, &buf, &buflen));
253 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
254 T(addstr("; minimum\n", 10, &buf, &buflen));
264 if (rdlen < (size_t)NS_INT16SZ)
270 len = SPRINTF((tmp, "%u ", t));
271 T(addstr(tmp, len, &buf, &buflen));
274 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
282 if (rdlen < (size_t)NS_INT16SZ)
288 len = SPRINTF((tmp, "%u ", t));
289 T(addstr(tmp, len, &buf, &buflen));
292 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
293 T(addstr(" ", 1, &buf, &buflen));
296 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
302 T(len = charstr(rdata, edata, &buf, &buflen));
309 while (rdata < edata) {
310 T(len = charstr(rdata, edata, &buf, &buflen));
315 T(addstr(" ", 1, &buf, &buflen));
322 (void) inet_nsap_ntoa(rdlen, rdata, t);
323 T(addstr(t, strlen(t), &buf, &buflen));
328 if (rdlen != (size_t)NS_IN6ADDRSZ)
330 (void) inet_ntop(AF_INET6, rdata, buf, buflen);
331 addlen(strlen(buf), &buf, &buflen);
337 /* XXX protocol format checking? */
338 (void) loc_ntoa(rdata, t);
339 T(addstr(t, strlen(t), &buf, &buflen));
344 u_int order, preference;
347 if (rdlen < 2U*NS_INT16SZ)
350 /* Order, Precedence. */
351 order = ns_get16(rdata); rdata += NS_INT16SZ;
352 preference = ns_get16(rdata); rdata += NS_INT16SZ;
353 len = SPRINTF((t, "%u %u ", order, preference));
354 T(addstr(t, len, &buf, &buflen));
357 T(len = charstr(rdata, edata, &buf, &buflen));
361 T(addstr(" ", 1, &buf, &buflen));
364 T(len = charstr(rdata, edata, &buf, &buflen));
368 T(addstr(" ", 1, &buf, &buflen));
371 T(len = charstr(rdata, edata, &buf, &buflen));
377 T(addstr(" ", 1, &buf, &buflen));
380 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
385 u_int priority, weight, port;
388 if (rdlen < 3U*NS_INT16SZ)
391 /* Priority, Weight, Port. */
392 priority = ns_get16(rdata); rdata += NS_INT16SZ;
393 weight = ns_get16(rdata); rdata += NS_INT16SZ;
394 port = ns_get16(rdata); rdata += NS_INT16SZ;
395 len = SPRINTF((t, "%u %u %u ", priority, weight, port));
396 T(addstr(t, len, &buf, &buflen));
399 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
406 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
407 T(addstr(" ", 1, &buf, &buflen));
410 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
417 if (rdlen < 1U + NS_INT32SZ)
421 (void) inet_ntop(AF_INET, rdata, buf, buflen);
422 addlen(strlen(buf), &buf, &buflen);
423 rdata += NS_INADDRSZ;
426 len = SPRINTF((tmp, " %u ( ", *rdata));
427 T(addstr(tmp, len, &buf, &buflen));
433 while (rdata < edata) {
438 T(addstr("\n\t\t\t\t", 5,
443 len = SPRINTF((tmp, "%d ", n));
444 T(addstr(tmp, len, &buf, &buflen));
450 T(addstr(")", 1, &buf, &buflen));
456 char base64_key[NS_MD5RSA_MAX_BASE64];
457 u_int keyflags, protocol, algorithm, key_id;
461 if (rdlen < 0U + NS_INT16SZ + NS_INT8SZ + NS_INT8SZ)
464 /* Key flags, Protocol, Algorithm. */
466 key_id = dst_s_dns_key_id(rdata, edata-rdata);
470 keyflags = ns_get16(rdata); rdata += NS_INT16SZ;
472 algorithm = *rdata++;
473 len = SPRINTF((tmp, "0x%04x %u %u",
474 keyflags, protocol, algorithm));
475 T(addstr(tmp, len, &buf, &buflen));
477 /* Public key data. */
478 len = b64_ntop(rdata, edata - rdata,
479 base64_key, sizeof base64_key);
483 T(addstr(" (", 2, &buf, &buflen));
488 for (n = 0; n < len; n += 48) {
489 T(addstr(leader, strlen(leader), &buf, &buflen));
490 T(addstr(base64_key + n, MIN(len - n, 48),
494 T(addstr(" )", 2, &buf, &buflen));
495 n = SPRINTF((tmp, " ; key_tag= %u", key_id));
496 T(addstr(tmp, n, &buf, &buflen));
502 char base64_key[NS_MD5RSA_MAX_BASE64];
503 u_int type, algorithm, labels, footprint;
511 /* Type covered, Algorithm, Label count, Original TTL. */
512 type = ns_get16(rdata); rdata += NS_INT16SZ;
513 algorithm = *rdata++;
515 t = ns_get32(rdata); rdata += NS_INT32SZ;
516 len = SPRINTF((tmp, "%s %d %d %lu ",
517 p_type(type), algorithm, labels, t));
518 T(addstr(tmp, len, &buf, &buflen));
519 if (labels > (u_int)dn_count_labels(name))
522 /* Signature expiry. */
523 t = ns_get32(rdata); rdata += NS_INT32SZ;
524 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
525 T(addstr(tmp, len, &buf, &buflen));
528 t = ns_get32(rdata); rdata += NS_INT32SZ;
529 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
530 T(addstr(tmp, len, &buf, &buflen));
532 /* Signature Footprint. */
533 footprint = ns_get16(rdata); rdata += NS_INT16SZ;
534 len = SPRINTF((tmp, "%u ", footprint));
535 T(addstr(tmp, len, &buf, &buflen));
538 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
541 len = b64_ntop(rdata, edata - rdata,
542 base64_key, sizeof base64_key);
544 T(addstr(" (", 2, &buf, &buflen));
551 for (n = 0; n < len; n += 48) {
552 T(addstr(leader, strlen(leader), &buf, &buflen));
553 T(addstr(base64_key + n, MIN(len - n, 48),
557 T(addstr(" )", 2, &buf, &buflen));
564 /* Next domain name. */
565 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
569 for (c = 0; c < n*8; c++)
570 if (NS_NXT_BIT_ISSET(c, rdata)) {
571 len = SPRINTF((tmp, " %s", p_type(c)));
572 T(addstr(tmp, len, &buf, &buflen));
578 u_int c_type, key_tag, alg;
581 char base64_cert[8192], tmp[40];
584 c_type = ns_get16(rdata); rdata += NS_INT16SZ;
585 key_tag = ns_get16(rdata); rdata += NS_INT16SZ;
586 alg = (u_int) *rdata++;
588 len = SPRINTF((tmp, "%d %d %d ", c_type, key_tag, alg));
589 T(addstr(tmp, len, &buf, &buflen));
590 siz = (edata-rdata)*4/3 + 4; /* "+4" accounts for trailing \0 */
591 if (siz > sizeof(base64_cert) * 3/4) {
592 const char *str = "record too long to print";
593 T(addstr(str, strlen(str), &buf, &buflen));
596 len = b64_ntop(rdata, edata-rdata, base64_cert, siz);
601 T(addstr(" (", 2, &buf, &buflen));
608 for (n = 0; n < len; n += 48) {
609 T(addstr(leader, strlen(leader),
611 T(addstr(base64_cert + n, MIN(len - n, 48),
615 T(addstr(" )", 2, &buf, &buflen));
621 /* KJD - need to complete this */
623 int mode, err, keysize;
625 /* Algorithm name. */
626 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
627 T(addstr(" ", 1, &buf, &buflen));
630 t = ns_get32(rdata); rdata += NS_INT32SZ;
631 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
632 T(addstr(tmp, len, &buf, &buflen));
635 t = ns_get32(rdata); rdata += NS_INT32SZ;
636 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
637 T(addstr(tmp, len, &buf, &buflen));
639 /* Mode , Error, Key Size. */
640 /* Priority, Weight, Port. */
641 mode = ns_get16(rdata); rdata += NS_INT16SZ;
642 err = ns_get16(rdata); rdata += NS_INT16SZ;
643 keysize = ns_get16(rdata); rdata += NS_INT16SZ;
644 len = SPRINTF((tmp, "%u %u %u ", mode, err, keysize));
645 T(addstr(tmp, len, &buf, &buflen));
647 /* XXX need to dump key, print otherdata length & other data */
652 /* BEW - need to complete this */
655 T(len = addname(msg, msglen, &rdata, origin, &buf, &buflen));
656 T(addstr(" ", 1, &buf, &buflen));
657 rdata += 8; /* time */
658 n = ns_get16(rdata); rdata += INT16SZ;
659 rdata += n; /* sig */
660 n = ns_get16(rdata); rdata += INT16SZ; /* original id */
661 sprintf(buf, "%d", ns_get16(rdata));
663 addlen(strlen(buf), &buf, &buflen);
672 if (rdlen == 0U) goto formerr;
673 len = SPRINTF((tmp, "%d ", *rdata));
674 T(addstr(tmp, len, &buf, &buflen));
676 if (pbit > 128) goto formerr;
677 pbyte = (pbit & ~7) / 8;
680 /* address suffix: provided only when prefix len != 128 */
682 if (rdata + pbyte >= edata) goto formerr;
683 memset(&a, 0, sizeof(a));
684 memcpy(&a.s6_addr[pbyte], rdata, sizeof(a) - pbyte);
685 (void) inet_ntop(AF_INET6, &a, buf, buflen);
686 addlen(strlen(buf), &buf, &buflen);
687 rdata += sizeof(a) - pbyte;
690 /* prefix name: provided only when prefix len > 0 */
693 if (rdata >= edata) goto formerr;
694 T(addstr(" ", 1, &buf, &buflen));
695 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
701 len = SPRINTF((tmp, "%u bytes", class));
702 T(addstr(tmp, len, &buf, &buflen));
707 comment = "unknown RR type";
712 comment = "RR format error";
717 len = SPRINTF((tmp, "\\# %u%s\t; %s", (unsigned)(edata - rdata),
718 rdlen != 0U ? " (" : "", comment));
719 T(addstr(tmp, len, &buf, &buflen));
720 while (rdata < edata) {
722 p += SPRINTF((p, "\n\t"));
724 n = MIN(16, edata - rdata);
725 for (m = 0; m < n; m++)
726 p += SPRINTF((p, "%02x ", rdata[m]));
727 T(addstr(tmp, p - tmp, &buf, &buflen));
729 T(addstr(")", 1, &buf, &buflen));
730 T(addtab(p - tmp + 1, 48, spaced, &buf, &buflen));
733 p += SPRINTF((p, "; "));
734 for (m = 0; m < n; m++)
735 *p++ = (isascii(rdata[m]) && isprint(rdata[m]))
738 T(addstr(tmp, p - tmp, &buf, &buflen));
749 * prune_origin(name, origin)
750 * Find out if the name is at or under the current origin.
752 * Number of characters in name before start of origin,
753 * or length of name if origin does not match.
755 * This function should share code with samedomain().
758 prune_origin(const char *name, const char *origin) {
759 const char *oname = name;
761 while (*name != '\0') {
762 if (origin != NULL && ns_samename(name, origin) == 1)
763 return (name - oname - (name > oname));
764 while (*name != '\0') {
767 /* XXX need to handle \nnn form. */
770 } else if (*name == '.') {
777 return (name - oname);
782 * charstr(rdata, edata, buf, buflen)
783 * Format a <character-string> into the presentation buffer.
785 * Number of rdata octets consumed
786 * 0 for protocol format error
787 * -1 for output buffer error
789 * buffer is advanced on success.
792 charstr(const u_char *rdata, const u_char *edata, char **buf, size_t *buflen) {
793 const u_char *odata = rdata;
794 size_t save_buflen = *buflen;
795 char *save_buf = *buf;
797 if (addstr("\"", 1, buf, buflen) < 0)
802 if (rdata + 1 + n <= edata) {
805 if (strchr("\n\"\\", *rdata) != NULL)
806 if (addstr("\\", 1, buf, buflen) < 0)
808 if (addstr((const char *)rdata, 1,
815 if (addstr("\"", 1, buf, buflen) < 0)
817 return (rdata - odata);
821 *buflen = save_buflen;
826 addname(const u_char *msg, size_t msglen,
827 const u_char **pp, const char *origin,
828 char **buf, size_t *buflen)
830 size_t newlen, save_buflen = *buflen;
831 char *save_buf = *buf;
834 n = dn_expand(msg, msg + msglen, *pp, *buf, *buflen);
836 goto enospc; /* Guess. */
837 newlen = prune_origin(*buf, origin);
840 } else if (newlen == 0U) {
841 /* Use "@" instead of name. */
842 if (newlen + 2 > *buflen)
843 goto enospc; /* No room for "@\0". */
844 (*buf)[newlen++] = '@';
845 (*buf)[newlen] = '\0';
847 if (((origin == NULL || origin[0] == '\0') ||
848 (origin[0] != '.' && origin[1] != '\0' &&
849 (*buf)[newlen] == '\0')) && (*buf)[newlen - 1] != '.') {
850 /* No trailing dot. */
852 if (newlen + 2 > *buflen)
853 goto enospc; /* No room for ".\0". */
854 (*buf)[newlen++] = '.';
855 (*buf)[newlen] = '\0';
859 addlen(newlen, buf, buflen);
865 *buflen = save_buflen;
870 addlen(size_t len, char **buf, size_t *buflen) {
871 INSIST(len <= *buflen);
877 addstr(const char *src, size_t len, char **buf, size_t *buflen) {
878 if (len >= *buflen) {
882 memcpy(*buf, src, len);
883 addlen(len, buf, buflen);
889 addtab(size_t len, size_t target, int spaced, char **buf, size_t *buflen) {
890 size_t save_buflen = *buflen;
891 char *save_buf = *buf;
894 if (spaced || len >= target - 1) {
895 T(addstr(" ", 2, buf, buflen));
898 for (t = (target - len - 1) / 8; t >= 0; t--)
899 if (addstr("\t", 1, buf, buflen) < 0) {
900 *buflen = save_buflen;