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>
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);
74 * ns_sprintrr(handle, rr, name_ctx, origin, buf, buflen)
75 * Convert an RR to presentation format.
77 * Number of characters written to buf, or -1 (check errno).
80 ns_sprintrr(const ns_msg *handle, const ns_rr *rr,
81 const char *name_ctx, const char *origin,
82 char *buf, size_t buflen)
86 n = ns_sprintrrf(ns_msg_base(*handle), ns_msg_size(*handle),
87 ns_rr_name(*rr), ns_rr_class(*rr), ns_rr_type(*rr),
88 ns_rr_ttl(*rr), ns_rr_rdata(*rr), ns_rr_rdlen(*rr),
89 name_ctx, origin, buf, buflen);
95 * ns_sprintrrf(msg, msglen, name, class, type, ttl, rdata, rdlen,
96 * name_ctx, origin, buf, buflen)
97 * Convert the fields of an RR into presentation format.
99 * Number of characters written to buf, or -1 (check errno).
102 ns_sprintrrf(const u_char *msg, size_t msglen,
103 const char *name, ns_class class, ns_type type,
104 u_long ttl, const u_char *rdata, size_t rdlen,
105 const char *name_ctx, const char *origin,
106 char *buf, size_t buflen)
108 const char *obuf = buf;
109 const u_char *edata = rdata + rdlen;
119 if (name_ctx != NULL && ns_samename(name_ctx, name) == 1) {
120 T(addstr("\t\t\t", 3, &buf, &buflen));
122 len = prune_origin(name, origin);
125 } else if (len == 0) {
126 T(addstr("@\t\t\t", 4, &buf, &buflen));
128 T(addstr(name, len, &buf, &buflen));
129 /* Origin not used or not root, and no trailing dot? */
130 if (((origin == NULL || origin[0] == '\0') ||
131 (origin[0] != '.' && origin[1] != '\0' &&
132 name[len] == '\0')) && name[len - 1] != '.') {
134 T(addstr(".", 1, &buf, &buflen));
137 T(spaced = addtab(len, 24, spaced, &buf, &buflen));
144 T(x = ns_format_ttl(ttl, buf, buflen));
145 addlen(x, &buf, &buflen);
146 len = SPRINTF((tmp, " %s %s", p_class(class), p_type(type)));
147 T(addstr(tmp, len, &buf, &buflen));
148 T(spaced = addtab(x + len, 16, spaced, &buf, &buflen));
155 if (rdlen != (size_t)NS_INADDRSZ)
157 (void) inet_ntop(AF_INET, rdata, buf, buflen);
158 addlen(strlen(buf), &buf, &buflen);
168 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
174 T(len = charstr(rdata, edata, &buf, &buflen));
178 T(addstr(" ", 1, &buf, &buflen));
181 /* Second word, optional in ISDN records. */
182 if (type == ns_t_isdn && rdata == edata)
185 T(len = charstr(rdata, edata, &buf, &buflen));
195 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
196 T(addstr(" ", 1, &buf, &buflen));
198 /* Administrator name. */
199 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
200 T(addstr(" (\n", 3, &buf, &buflen));
203 if ((edata - rdata) != 5*NS_INT32SZ)
207 t = ns_get32(rdata); rdata += NS_INT32SZ;
208 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
209 len = SPRINTF((tmp, "%lu", t));
210 T(addstr(tmp, len, &buf, &buflen));
211 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
212 T(addstr("; serial\n", 9, &buf, &buflen));
215 /* Refresh interval. */
216 t = ns_get32(rdata); rdata += NS_INT32SZ;
217 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
218 T(len = ns_format_ttl(t, buf, buflen));
219 addlen(len, &buf, &buflen);
220 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
221 T(addstr("; refresh\n", 10, &buf, &buflen));
224 /* Retry interval. */
225 t = ns_get32(rdata); rdata += NS_INT32SZ;
226 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
227 T(len = ns_format_ttl(t, buf, buflen));
228 addlen(len, &buf, &buflen);
229 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
230 T(addstr("; retry\n", 8, &buf, &buflen));
234 t = ns_get32(rdata); rdata += NS_INT32SZ;
235 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
236 T(len = ns_format_ttl(t, buf, buflen));
237 addlen(len, &buf, &buflen);
238 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
239 T(addstr("; expiry\n", 9, &buf, &buflen));
243 t = ns_get32(rdata); rdata += NS_INT32SZ;
244 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
245 T(len = ns_format_ttl(t, buf, buflen));
246 addlen(len, &buf, &buflen);
247 T(addstr(" )", 2, &buf, &buflen));
248 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
249 T(addstr("; minimum\n", 10, &buf, &buflen));
259 if (rdlen < (size_t)NS_INT16SZ)
265 len = SPRINTF((tmp, "%u ", t));
266 T(addstr(tmp, len, &buf, &buflen));
269 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
277 if (rdlen < (size_t)NS_INT16SZ)
283 len = SPRINTF((tmp, "%u ", t));
284 T(addstr(tmp, len, &buf, &buflen));
287 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
288 T(addstr(" ", 1, &buf, &buflen));
291 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
297 T(len = charstr(rdata, edata, &buf, &buflen));
304 while (rdata < edata) {
305 T(len = charstr(rdata, edata, &buf, &buflen));
310 T(addstr(" ", 1, &buf, &buflen));
317 (void) inet_nsap_ntoa(rdlen, rdata, t);
318 T(addstr(t, strlen(t), &buf, &buflen));
323 if (rdlen != (size_t)NS_IN6ADDRSZ)
325 (void) inet_ntop(AF_INET6, rdata, buf, buflen);
326 addlen(strlen(buf), &buf, &buflen);
332 /* XXX protocol format checking? */
333 (void) loc_ntoa(rdata, t);
334 T(addstr(t, strlen(t), &buf, &buflen));
339 u_int order, preference;
342 if (rdlen < 2U*NS_INT16SZ)
345 /* Order, Precedence. */
346 order = ns_get16(rdata); rdata += NS_INT16SZ;
347 preference = ns_get16(rdata); rdata += NS_INT16SZ;
348 len = SPRINTF((t, "%u %u ", order, preference));
349 T(addstr(t, len, &buf, &buflen));
352 T(len = charstr(rdata, edata, &buf, &buflen));
356 T(addstr(" ", 1, &buf, &buflen));
359 T(len = charstr(rdata, edata, &buf, &buflen));
363 T(addstr(" ", 1, &buf, &buflen));
366 T(len = charstr(rdata, edata, &buf, &buflen));
372 T(addstr(" ", 1, &buf, &buflen));
375 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
380 u_int priority, weight, port;
383 if (rdlen < 3U*NS_INT16SZ)
386 /* Priority, Weight, Port. */
387 priority = ns_get16(rdata); rdata += NS_INT16SZ;
388 weight = ns_get16(rdata); rdata += NS_INT16SZ;
389 port = ns_get16(rdata); rdata += NS_INT16SZ;
390 len = SPRINTF((t, "%u %u %u ", priority, weight, port));
391 T(addstr(t, len, &buf, &buflen));
394 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
401 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
402 T(addstr(" ", 1, &buf, &buflen));
405 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
412 if (rdlen < 1U + NS_INT32SZ)
416 (void) inet_ntop(AF_INET, rdata, buf, buflen);
417 addlen(strlen(buf), &buf, &buflen);
418 rdata += NS_INADDRSZ;
421 len = SPRINTF((tmp, " %u ( ", *rdata));
422 T(addstr(tmp, len, &buf, &buflen));
428 while (rdata < edata) {
433 T(addstr("\n\t\t\t\t", 5,
438 len = SPRINTF((tmp, "%d ", n));
439 T(addstr(tmp, len, &buf, &buflen));
445 T(addstr(")", 1, &buf, &buflen));
451 char base64_key[NS_MD5RSA_MAX_BASE64];
452 u_int keyflags, protocol, algorithm, key_id;
456 if (rdlen < 0U + NS_INT16SZ + NS_INT8SZ + NS_INT8SZ)
459 /* Key flags, Protocol, Algorithm. */
460 key_id = dst_s_dns_key_id(rdata, edata-rdata);
461 keyflags = ns_get16(rdata); rdata += NS_INT16SZ;
463 algorithm = *rdata++;
464 len = SPRINTF((tmp, "0x%04x %u %u",
465 keyflags, protocol, algorithm));
466 T(addstr(tmp, len, &buf, &buflen));
468 /* Public key data. */
469 len = b64_ntop(rdata, edata - rdata,
470 base64_key, sizeof base64_key);
474 T(addstr(" (", 2, &buf, &buflen));
479 for (n = 0; n < len; n += 48) {
480 T(addstr(leader, strlen(leader), &buf, &buflen));
481 T(addstr(base64_key + n, MIN(len - n, 48),
485 T(addstr(" )", 2, &buf, &buflen));
486 n = SPRINTF((tmp, " ; key_tag= %u", key_id));
487 T(addstr(tmp, n, &buf, &buflen));
493 char base64_key[NS_MD5RSA_MAX_BASE64];
494 u_int type, algorithm, labels, footprint;
502 /* Type covered, Algorithm, Label count, Original TTL. */
503 type = ns_get16(rdata); rdata += NS_INT16SZ;
504 algorithm = *rdata++;
506 t = ns_get32(rdata); rdata += NS_INT32SZ;
507 len = SPRINTF((tmp, "%s %d %d %lu ",
508 p_type(type), algorithm, labels, t));
509 T(addstr(tmp, len, &buf, &buflen));
510 if (labels > (u_int)dn_count_labels(name))
513 /* Signature expiry. */
514 t = ns_get32(rdata); rdata += NS_INT32SZ;
515 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
516 T(addstr(tmp, len, &buf, &buflen));
519 t = ns_get32(rdata); rdata += NS_INT32SZ;
520 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
521 T(addstr(tmp, len, &buf, &buflen));
523 /* Signature Footprint. */
524 footprint = ns_get16(rdata); rdata += NS_INT16SZ;
525 len = SPRINTF((tmp, "%u ", footprint));
526 T(addstr(tmp, len, &buf, &buflen));
529 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
532 len = b64_ntop(rdata, edata - rdata,
533 base64_key, sizeof base64_key);
535 T(addstr(" (", 2, &buf, &buflen));
542 for (n = 0; n < len; n += 48) {
543 T(addstr(leader, strlen(leader), &buf, &buflen));
544 T(addstr(base64_key + n, MIN(len - n, 48),
548 T(addstr(" )", 2, &buf, &buflen));
555 /* Next domain name. */
556 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
560 for (c = 0; c < n*8; c++)
561 if (NS_NXT_BIT_ISSET(c, rdata)) {
562 len = SPRINTF((tmp, " %s", p_type(c)));
563 T(addstr(tmp, len, &buf, &buflen));
569 u_int c_type, key_tag, alg;
572 char base64_cert[8192], tmp[40];
575 c_type = ns_get16(rdata); rdata += NS_INT16SZ;
576 key_tag = ns_get16(rdata); rdata += NS_INT16SZ;
577 alg = (u_int) *rdata++;
579 len = SPRINTF((tmp, "%d %d %d ", c_type, key_tag, alg));
580 T(addstr(tmp, len, &buf, &buflen));
581 siz = (edata-rdata)*4/3 + 4; /* "+4" accounts for trailing \0 */
582 if (siz > sizeof(base64_cert) * 3/4) {
583 const char *str = "record too long to print";
584 T(addstr(str, strlen(str), &buf, &buflen));
587 len = b64_ntop(rdata, edata-rdata, base64_cert, siz);
592 T(addstr(" (", 2, &buf, &buflen));
599 for (n = 0; n < len; n += 48) {
600 T(addstr(leader, strlen(leader),
602 T(addstr(base64_cert + n, MIN(len - n, 48),
606 T(addstr(" )", 2, &buf, &buflen));
612 /* KJD - need to complete this */
614 int mode, err, keysize;
616 /* Algorithm name. */
617 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
618 T(addstr(" ", 1, &buf, &buflen));
621 t = ns_get32(rdata); rdata += NS_INT32SZ;
622 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
623 T(addstr(tmp, len, &buf, &buflen));
626 t = ns_get32(rdata); rdata += NS_INT32SZ;
627 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
628 T(addstr(tmp, len, &buf, &buflen));
630 /* Mode , Error, Key Size. */
631 /* Priority, Weight, Port. */
632 mode = ns_get16(rdata); rdata += NS_INT16SZ;
633 err = ns_get16(rdata); rdata += NS_INT16SZ;
634 keysize = ns_get16(rdata); rdata += NS_INT16SZ;
635 len = SPRINTF((tmp, "%u %u %u ", mode, err, keysize));
636 T(addstr(tmp, len, &buf, &buflen));
638 /* XXX need to dump key, print otherdata length & other data */
643 /* BEW - need to complete this */
646 T(len = addname(msg, msglen, &rdata, origin, &buf, &buflen));
647 T(addstr(" ", 1, &buf, &buflen));
648 rdata += 8; /* time */
649 n = ns_get16(rdata); rdata += INT16SZ;
650 rdata += n; /* sig */
651 n = ns_get16(rdata); rdata += INT16SZ; /* original id */
652 sprintf(buf, "%d", ns_get16(rdata));
654 addlen(strlen(buf), &buf, &buflen);
663 if (rdlen == 0U) goto formerr;
664 len = SPRINTF((tmp, "%d ", *rdata));
665 T(addstr(tmp, len, &buf, &buflen));
667 if (pbit > 128) goto formerr;
668 pbyte = (pbit & ~7) / 8;
671 /* address suffix: provided only when prefix len != 128 */
673 if (rdata + pbyte >= edata) goto formerr;
674 memset(&a, 0, sizeof(a));
675 memcpy(&a.s6_addr[pbyte], rdata, sizeof(a) - pbyte);
676 (void) inet_ntop(AF_INET6, &a, buf, buflen);
677 addlen(strlen(buf), &buf, &buflen);
678 rdata += sizeof(a) - pbyte;
681 /* prefix name: provided only when prefix len > 0 */
684 if (rdata >= edata) goto formerr;
685 T(addstr(" ", 1, &buf, &buflen));
686 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
692 len = SPRINTF((tmp, "%u bytes", class));
693 T(addstr(tmp, len, &buf, &buflen));
698 comment = "unknown RR type";
703 comment = "RR format error";
708 len = SPRINTF((tmp, "\\# %u%s\t; %s", (unsigned)(edata - rdata),
709 rdlen != 0U ? " (" : "", comment));
710 T(addstr(tmp, len, &buf, &buflen));
711 while (rdata < edata) {
713 p += SPRINTF((p, "\n\t"));
715 n = MIN(16, edata - rdata);
716 for (m = 0; m < n; m++)
717 p += SPRINTF((p, "%02x ", rdata[m]));
718 T(addstr(tmp, p - tmp, &buf, &buflen));
720 T(addstr(")", 1, &buf, &buflen));
721 T(addtab(p - tmp + 1, 48, spaced, &buf, &buflen));
724 p += SPRINTF((p, "; "));
725 for (m = 0; m < n; m++)
726 *p++ = (isascii(rdata[m]) && isprint(rdata[m]))
729 T(addstr(tmp, p - tmp, &buf, &buflen));
740 * prune_origin(name, origin)
741 * Find out if the name is at or under the current origin.
743 * Number of characters in name before start of origin,
744 * or length of name if origin does not match.
746 * This function should share code with samedomain().
749 prune_origin(const char *name, const char *origin) {
750 const char *oname = name;
752 while (*name != '\0') {
753 if (origin != NULL && ns_samename(name, origin) == 1)
754 return (name - oname - (name > oname));
755 while (*name != '\0') {
758 /* XXX need to handle \nnn form. */
761 } else if (*name == '.') {
768 return (name - oname);
773 * charstr(rdata, edata, buf, buflen)
774 * Format a <character-string> into the presentation buffer.
776 * Number of rdata octets consumed
777 * 0 for protocol format error
778 * -1 for output buffer error
780 * buffer is advanced on success.
783 charstr(const u_char *rdata, const u_char *edata, char **buf, size_t *buflen) {
784 const u_char *odata = rdata;
785 size_t save_buflen = *buflen;
786 char *save_buf = *buf;
788 if (addstr("\"", 1, buf, buflen) < 0)
793 if (rdata + 1 + n <= edata) {
796 if (strchr("\n\"\\", *rdata) != NULL)
797 if (addstr("\\", 1, buf, buflen) < 0)
799 if (addstr((const char *)rdata, 1,
806 if (addstr("\"", 1, buf, buflen) < 0)
808 return (rdata - odata);
812 *buflen = save_buflen;
817 addname(const u_char *msg, size_t msglen,
818 const u_char **pp, const char *origin,
819 char **buf, size_t *buflen)
821 size_t newlen, save_buflen = *buflen;
822 char *save_buf = *buf;
825 n = dn_expand(msg, msg + msglen, *pp, *buf, *buflen);
827 goto enospc; /* Guess. */
828 newlen = prune_origin(*buf, origin);
831 } else if (newlen == 0U) {
832 /* Use "@" instead of name. */
833 if (newlen + 2 > *buflen)
834 goto enospc; /* No room for "@\0". */
835 (*buf)[newlen++] = '@';
836 (*buf)[newlen] = '\0';
838 if (((origin == NULL || origin[0] == '\0') ||
839 (origin[0] != '.' && origin[1] != '\0' &&
840 (*buf)[newlen] == '\0')) && (*buf)[newlen - 1] != '.') {
841 /* No trailing dot. */
843 if (newlen + 2 > *buflen)
844 goto enospc; /* No room for ".\0". */
845 (*buf)[newlen++] = '.';
846 (*buf)[newlen] = '\0';
850 addlen(newlen, buf, buflen);
856 *buflen = save_buflen;
861 addlen(size_t len, char **buf, size_t *buflen) {
862 INSIST(len <= *buflen);
868 addstr(const char *src, size_t len, char **buf, size_t *buflen) {
869 if (len >= *buflen) {
873 memcpy(*buf, src, len);
874 addlen(len, buf, buflen);
880 addtab(size_t len, size_t target, int spaced, char **buf, size_t *buflen) {
881 size_t save_buflen = *buflen;
882 char *save_buf = *buf;
885 if (spaced || len >= target - 1) {
886 T(addstr(" ", 2, buf, buflen));
889 for (t = (target - len - 1) / 8; t >= 0; t--)
890 if (addstr("\t", 1, buf, buflen) < 0) {
891 *buflen = save_buflen;