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.6.18.4 2005/04/27 05:01:09 sra Exp $";
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
26 #include "port_before.h"
28 #include <sys/types.h>
29 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/nameser.h>
33 #include <arpa/inet.h>
37 #define INSIST(cond) assert(cond)
39 #include <isc/assertions.h>
47 #include "port_after.h"
50 # define SPRINTF(x) strlen(sprintf/**/x)
52 # define SPRINTF(x) ((size_t)sprintf x)
57 static size_t prune_origin(const char *name, const char *origin);
58 static int charstr(const u_char *rdata, const u_char *edata,
59 char **buf, size_t *buflen);
60 static int addname(const u_char *msg, size_t msglen,
61 const u_char **p, const char *origin,
62 char **buf, size_t *buflen);
63 static void addlen(size_t len, char **buf, size_t *buflen);
64 static int addstr(const char *src, size_t len,
65 char **buf, size_t *buflen);
66 static int addtab(size_t len, size_t target, int spaced,
67 char **buf, size_t *buflen);
80 * Convert an RR to presentation format.
83 *\li Number of characters written to buf, or -1 (check errno).
86 ns_sprintrr(const ns_msg *handle, const ns_rr *rr,
87 const char *name_ctx, const char *origin,
88 char *buf, size_t buflen)
92 n = ns_sprintrrf(ns_msg_base(*handle), ns_msg_size(*handle),
93 ns_rr_name(*rr), ns_rr_class(*rr), ns_rr_type(*rr),
94 ns_rr_ttl(*rr), ns_rr_rdata(*rr), ns_rr_rdlen(*rr),
95 name_ctx, origin, buf, buflen);
100 * Convert the fields of an RR into presentation format.
103 *\li Number of characters written to buf, or -1 (check errno).
106 ns_sprintrrf(const u_char *msg, size_t msglen,
107 const char *name, ns_class class, ns_type type,
108 u_long ttl, const u_char *rdata, size_t rdlen,
109 const char *name_ctx, const char *origin,
110 char *buf, size_t buflen)
112 const char *obuf = buf;
113 const u_char *edata = rdata + rdlen;
123 if (name_ctx != NULL && ns_samename(name_ctx, name) == 1) {
124 T(addstr("\t\t\t", 3, &buf, &buflen));
126 len = prune_origin(name, origin);
129 } else if (len == 0) {
130 T(addstr("@\t\t\t", 4, &buf, &buflen));
132 T(addstr(name, len, &buf, &buflen));
133 /* Origin not used or not root, and no trailing dot? */
134 if (((origin == NULL || origin[0] == '\0') ||
135 (origin[0] != '.' && origin[1] != '\0' &&
136 name[len] == '\0')) && name[len - 1] != '.') {
138 T(addstr(".", 1, &buf, &buflen));
141 T(spaced = addtab(len, 24, spaced, &buf, &buflen));
148 T(x = ns_format_ttl(ttl, buf, buflen));
149 addlen(x, &buf, &buflen);
150 len = SPRINTF((tmp, " %s %s", p_class(class), p_type(type)));
151 T(addstr(tmp, len, &buf, &buflen));
152 T(spaced = addtab(x + len, 16, spaced, &buf, &buflen));
159 if (rdlen != (size_t)NS_INADDRSZ)
161 (void) inet_ntop(AF_INET, rdata, buf, buflen);
162 addlen(strlen(buf), &buf, &buflen);
172 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
178 T(len = charstr(rdata, edata, &buf, &buflen));
182 T(addstr(" ", 1, &buf, &buflen));
185 /* Second word, optional in ISDN records. */
186 if (type == ns_t_isdn && rdata == edata)
189 T(len = charstr(rdata, edata, &buf, &buflen));
199 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
200 T(addstr(" ", 1, &buf, &buflen));
202 /* Administrator name. */
203 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
204 T(addstr(" (\n", 3, &buf, &buflen));
207 if ((edata - rdata) != 5*NS_INT32SZ)
211 t = ns_get32(rdata); rdata += NS_INT32SZ;
212 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
213 len = SPRINTF((tmp, "%lu", t));
214 T(addstr(tmp, len, &buf, &buflen));
215 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
216 T(addstr("; serial\n", 9, &buf, &buflen));
219 /* Refresh interval. */
220 t = ns_get32(rdata); rdata += NS_INT32SZ;
221 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
222 T(len = ns_format_ttl(t, buf, buflen));
223 addlen(len, &buf, &buflen);
224 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
225 T(addstr("; refresh\n", 10, &buf, &buflen));
228 /* Retry interval. */
229 t = ns_get32(rdata); rdata += NS_INT32SZ;
230 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
231 T(len = ns_format_ttl(t, buf, buflen));
232 addlen(len, &buf, &buflen);
233 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
234 T(addstr("; retry\n", 8, &buf, &buflen));
238 t = ns_get32(rdata); rdata += NS_INT32SZ;
239 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
240 T(len = ns_format_ttl(t, buf, buflen));
241 addlen(len, &buf, &buflen);
242 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
243 T(addstr("; expiry\n", 9, &buf, &buflen));
247 t = ns_get32(rdata); rdata += NS_INT32SZ;
248 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
249 T(len = ns_format_ttl(t, buf, buflen));
250 addlen(len, &buf, &buflen);
251 T(addstr(" )", 2, &buf, &buflen));
252 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
253 T(addstr("; minimum\n", 10, &buf, &buflen));
263 if (rdlen < (size_t)NS_INT16SZ)
269 len = SPRINTF((tmp, "%u ", t));
270 T(addstr(tmp, len, &buf, &buflen));
273 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
281 if (rdlen < (size_t)NS_INT16SZ)
287 len = SPRINTF((tmp, "%u ", t));
288 T(addstr(tmp, len, &buf, &buflen));
291 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
292 T(addstr(" ", 1, &buf, &buflen));
295 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
301 T(len = charstr(rdata, edata, &buf, &buflen));
308 while (rdata < edata) {
309 T(len = charstr(rdata, edata, &buf, &buflen));
314 T(addstr(" ", 1, &buf, &buflen));
321 (void) inet_nsap_ntoa(rdlen, rdata, t);
322 T(addstr(t, strlen(t), &buf, &buflen));
327 if (rdlen != (size_t)NS_IN6ADDRSZ)
329 (void) inet_ntop(AF_INET6, rdata, buf, buflen);
330 addlen(strlen(buf), &buf, &buflen);
336 /* XXX protocol format checking? */
337 (void) loc_ntoa(rdata, t);
338 T(addstr(t, strlen(t), &buf, &buflen));
343 u_int order, preference;
346 if (rdlen < 2U*NS_INT16SZ)
349 /* Order, Precedence. */
350 order = ns_get16(rdata); rdata += NS_INT16SZ;
351 preference = ns_get16(rdata); rdata += NS_INT16SZ;
352 len = SPRINTF((t, "%u %u ", order, preference));
353 T(addstr(t, len, &buf, &buflen));
356 T(len = charstr(rdata, edata, &buf, &buflen));
360 T(addstr(" ", 1, &buf, &buflen));
363 T(len = charstr(rdata, edata, &buf, &buflen));
367 T(addstr(" ", 1, &buf, &buflen));
370 T(len = charstr(rdata, edata, &buf, &buflen));
376 T(addstr(" ", 1, &buf, &buflen));
379 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
384 u_int priority, weight, port;
387 if (rdlen < 3U*NS_INT16SZ)
390 /* Priority, Weight, Port. */
391 priority = ns_get16(rdata); rdata += NS_INT16SZ;
392 weight = ns_get16(rdata); rdata += NS_INT16SZ;
393 port = ns_get16(rdata); rdata += NS_INT16SZ;
394 len = SPRINTF((t, "%u %u %u ", priority, weight, port));
395 T(addstr(t, len, &buf, &buflen));
398 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
405 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
406 T(addstr(" ", 1, &buf, &buflen));
409 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
416 if (rdlen < 1U + NS_INT32SZ)
420 (void) inet_ntop(AF_INET, rdata, buf, buflen);
421 addlen(strlen(buf), &buf, &buflen);
422 rdata += NS_INADDRSZ;
425 len = SPRINTF((tmp, " %u ( ", *rdata));
426 T(addstr(tmp, len, &buf, &buflen));
432 while (rdata < edata) {
437 T(addstr("\n\t\t\t\t", 5,
442 len = SPRINTF((tmp, "%d ", n));
443 T(addstr(tmp, len, &buf, &buflen));
449 T(addstr(")", 1, &buf, &buflen));
455 char base64_key[NS_MD5RSA_MAX_BASE64];
456 u_int keyflags, protocol, algorithm, key_id;
460 if (rdlen < 0U + NS_INT16SZ + NS_INT8SZ + NS_INT8SZ)
463 /* Key flags, Protocol, Algorithm. */
465 key_id = dst_s_dns_key_id(rdata, edata-rdata);
469 keyflags = ns_get16(rdata); rdata += NS_INT16SZ;
471 algorithm = *rdata++;
472 len = SPRINTF((tmp, "0x%04x %u %u",
473 keyflags, protocol, algorithm));
474 T(addstr(tmp, len, &buf, &buflen));
476 /* Public key data. */
477 len = b64_ntop(rdata, edata - rdata,
478 base64_key, sizeof base64_key);
482 T(addstr(" (", 2, &buf, &buflen));
487 for (n = 0; n < len; n += 48) {
488 T(addstr(leader, strlen(leader), &buf, &buflen));
489 T(addstr(base64_key + n, MIN(len - n, 48),
493 T(addstr(" )", 2, &buf, &buflen));
494 n = SPRINTF((tmp, " ; key_tag= %u", key_id));
495 T(addstr(tmp, n, &buf, &buflen));
501 char base64_key[NS_MD5RSA_MAX_BASE64];
502 u_int type, algorithm, labels, footprint;
510 /* Type covered, Algorithm, Label count, Original TTL. */
511 type = ns_get16(rdata); rdata += NS_INT16SZ;
512 algorithm = *rdata++;
514 t = ns_get32(rdata); rdata += NS_INT32SZ;
515 len = SPRINTF((tmp, "%s %d %d %lu ",
516 p_type(type), algorithm, labels, t));
517 T(addstr(tmp, len, &buf, &buflen));
518 if (labels > (u_int)dn_count_labels(name))
521 /* Signature expiry. */
522 t = ns_get32(rdata); rdata += NS_INT32SZ;
523 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
524 T(addstr(tmp, len, &buf, &buflen));
527 t = ns_get32(rdata); rdata += NS_INT32SZ;
528 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
529 T(addstr(tmp, len, &buf, &buflen));
531 /* Signature Footprint. */
532 footprint = ns_get16(rdata); rdata += NS_INT16SZ;
533 len = SPRINTF((tmp, "%u ", footprint));
534 T(addstr(tmp, len, &buf, &buflen));
537 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
540 len = b64_ntop(rdata, edata - rdata,
541 base64_key, sizeof base64_key);
543 T(addstr(" (", 2, &buf, &buflen));
550 for (n = 0; n < len; n += 48) {
551 T(addstr(leader, strlen(leader), &buf, &buflen));
552 T(addstr(base64_key + n, MIN(len - n, 48),
556 T(addstr(" )", 2, &buf, &buflen));
563 /* Next domain name. */
564 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
568 for (c = 0; c < n*8; c++)
569 if (NS_NXT_BIT_ISSET(c, rdata)) {
570 len = SPRINTF((tmp, " %s", p_type(c)));
571 T(addstr(tmp, len, &buf, &buflen));
577 u_int c_type, key_tag, alg;
580 char base64_cert[8192], tmp[40];
583 c_type = ns_get16(rdata); rdata += NS_INT16SZ;
584 key_tag = ns_get16(rdata); rdata += NS_INT16SZ;
585 alg = (u_int) *rdata++;
587 len = SPRINTF((tmp, "%d %d %d ", c_type, key_tag, alg));
588 T(addstr(tmp, len, &buf, &buflen));
589 siz = (edata-rdata)*4/3 + 4; /* "+4" accounts for trailing \0 */
590 if (siz > sizeof(base64_cert) * 3/4) {
591 const char *str = "record too long to print";
592 T(addstr(str, strlen(str), &buf, &buflen));
595 len = b64_ntop(rdata, edata-rdata, base64_cert, siz);
600 T(addstr(" (", 2, &buf, &buflen));
607 for (n = 0; n < len; n += 48) {
608 T(addstr(leader, strlen(leader),
610 T(addstr(base64_cert + n, MIN(len - n, 48),
614 T(addstr(" )", 2, &buf, &buflen));
620 /* KJD - need to complete this */
622 int mode, err, keysize;
624 /* Algorithm name. */
625 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
626 T(addstr(" ", 1, &buf, &buflen));
629 t = ns_get32(rdata); rdata += NS_INT32SZ;
630 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
631 T(addstr(tmp, len, &buf, &buflen));
634 t = ns_get32(rdata); rdata += NS_INT32SZ;
635 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
636 T(addstr(tmp, len, &buf, &buflen));
638 /* Mode , Error, Key Size. */
639 /* Priority, Weight, Port. */
640 mode = ns_get16(rdata); rdata += NS_INT16SZ;
641 err = ns_get16(rdata); rdata += NS_INT16SZ;
642 keysize = ns_get16(rdata); rdata += NS_INT16SZ;
643 len = SPRINTF((tmp, "%u %u %u ", mode, err, keysize));
644 T(addstr(tmp, len, &buf, &buflen));
646 /* XXX need to dump key, print otherdata length & other data */
651 /* BEW - need to complete this */
654 T(len = addname(msg, msglen, &rdata, origin, &buf, &buflen));
655 T(addstr(" ", 1, &buf, &buflen));
656 rdata += 8; /*%< time */
657 n = ns_get16(rdata); rdata += INT16SZ;
658 rdata += n; /*%< sig */
659 n = ns_get16(rdata); rdata += INT16SZ; /*%< original id */
660 sprintf(buf, "%d", ns_get16(rdata));
662 addlen(strlen(buf), &buf, &buflen);
671 if (rdlen == 0U) goto formerr;
672 len = SPRINTF((tmp, "%d ", *rdata));
673 T(addstr(tmp, len, &buf, &buflen));
675 if (pbit > 128) goto formerr;
676 pbyte = (pbit & ~7) / 8;
679 /* address suffix: provided only when prefix len != 128 */
681 if (rdata + pbyte >= edata) goto formerr;
682 memset(&a, 0, sizeof(a));
683 memcpy(&a.s6_addr[pbyte], rdata, sizeof(a) - pbyte);
684 (void) inet_ntop(AF_INET6, &a, buf, buflen);
685 addlen(strlen(buf), &buf, &buflen);
686 rdata += sizeof(a) - pbyte;
689 /* prefix name: provided only when prefix len > 0 */
692 if (rdata >= edata) goto formerr;
693 T(addstr(" ", 1, &buf, &buflen));
694 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
700 len = SPRINTF((tmp, "%u bytes", class));
701 T(addstr(tmp, len, &buf, &buflen));
706 comment = "unknown RR type";
711 comment = "RR format error";
716 len = SPRINTF((tmp, "\\# %u%s\t; %s", (unsigned)(edata - rdata),
717 rdlen != 0U ? " (" : "", comment));
718 T(addstr(tmp, len, &buf, &buflen));
719 while (rdata < edata) {
721 p += SPRINTF((p, "\n\t"));
723 n = MIN(16, edata - rdata);
724 for (m = 0; m < n; m++)
725 p += SPRINTF((p, "%02x ", rdata[m]));
726 T(addstr(tmp, p - tmp, &buf, &buflen));
728 T(addstr(")", 1, &buf, &buflen));
729 T(addtab(p - tmp + 1, 48, spaced, &buf, &buflen));
732 p += SPRINTF((p, "; "));
733 for (m = 0; m < n; m++)
734 *p++ = (isascii(rdata[m]) && isprint(rdata[m]))
737 T(addstr(tmp, p - tmp, &buf, &buflen));
748 * prune_origin(name, origin)
749 * Find out if the name is at or under the current origin.
751 * Number of characters in name before start of origin,
752 * or length of name if origin does not match.
754 * This function should share code with samedomain().
757 prune_origin(const char *name, const char *origin) {
758 const char *oname = name;
760 while (*name != '\0') {
761 if (origin != NULL && ns_samename(name, origin) == 1)
762 return (name - oname - (name > oname));
763 while (*name != '\0') {
766 /* XXX need to handle \nnn form. */
769 } else if (*name == '.') {
776 return (name - oname);
781 * charstr(rdata, edata, buf, buflen)
782 * Format a <character-string> into the presentation buffer.
784 * Number of rdata octets consumed
785 * 0 for protocol format error
786 * -1 for output buffer error
788 * buffer is advanced on success.
791 charstr(const u_char *rdata, const u_char *edata, char **buf, size_t *buflen) {
792 const u_char *odata = rdata;
793 size_t save_buflen = *buflen;
794 char *save_buf = *buf;
796 if (addstr("\"", 1, buf, buflen) < 0)
801 if (rdata + 1 + n <= edata) {
804 if (strchr("\n\"\\", *rdata) != NULL)
805 if (addstr("\\", 1, buf, buflen) < 0)
807 if (addstr((const char *)rdata, 1,
814 if (addstr("\"", 1, buf, buflen) < 0)
816 return (rdata - odata);
820 *buflen = save_buflen;
825 addname(const u_char *msg, size_t msglen,
826 const u_char **pp, const char *origin,
827 char **buf, size_t *buflen)
829 size_t newlen, save_buflen = *buflen;
830 char *save_buf = *buf;
833 n = dn_expand(msg, msg + msglen, *pp, *buf, *buflen);
835 goto enospc; /*%< Guess. */
836 newlen = prune_origin(*buf, origin);
839 } else if (newlen == 0U) {
840 /* Use "@" instead of name. */
841 if (newlen + 2 > *buflen)
842 goto enospc; /* No room for "@\0". */
843 (*buf)[newlen++] = '@';
844 (*buf)[newlen] = '\0';
846 if (((origin == NULL || origin[0] == '\0') ||
847 (origin[0] != '.' && origin[1] != '\0' &&
848 (*buf)[newlen] == '\0')) && (*buf)[newlen - 1] != '.') {
849 /* No trailing dot. */
851 if (newlen + 2 > *buflen)
852 goto enospc; /* No room for ".\0". */
853 (*buf)[newlen++] = '.';
854 (*buf)[newlen] = '\0';
858 addlen(newlen, buf, buflen);
864 *buflen = save_buflen;
869 addlen(size_t len, char **buf, size_t *buflen) {
870 INSIST(len <= *buflen);
876 addstr(const char *src, size_t len, char **buf, size_t *buflen) {
877 if (len >= *buflen) {
881 memcpy(*buf, src, len);
882 addlen(len, buf, buflen);
888 addtab(size_t len, size_t target, int spaced, char **buf, size_t *buflen) {
889 size_t save_buflen = *buflen;
890 char *save_buf = *buf;
893 if (spaced || len >= target - 1) {
894 T(addstr(" ", 2, buf, buflen));
897 for (t = (target - len - 1) / 8; t >= 0; t--)
898 if (addstr("\t", 1, buf, buflen) < 0) {
899 *buflen = save_buflen;