2 * Copyright (c) 1996-1999 by Internet Software Consortium.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
18 #if !defined(_LIBC) && !defined(lint)
19 static const char rcsid
[] = "$BINDId: ns_print.c,v 8.18 2000/02/29 05:48:12 vixie Exp $";
24 #include <sys/types.h>
25 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/nameser.h>
29 #include <arpa/inet.h>
38 # define SPRINTF(x) strlen(sprintf/**/x)
40 # define SPRINTF(x) ((size_t)sprintf x)
45 static size_t prune_origin(const char *name
, const char *origin
);
46 static int charstr(const u_char
*rdata
, const u_char
*edata
,
47 char **buf
, size_t *buflen
);
48 static int addname(const u_char
*msg
, size_t msglen
,
49 const u_char
**p
, const char *origin
,
50 char **buf
, size_t *buflen
);
51 static void addlen(size_t len
, char **buf
, size_t *buflen
);
52 static int addstr(const char *src
, size_t len
,
53 char **buf
, size_t *buflen
);
54 static int addtab(size_t len
, size_t target
, int spaced
,
55 char **buf
, size_t *buflen
);
60 u_int16_t
dst_s_dns_key_id(const u_char
*, const int);
75 * ns_sprintrr(handle, rr, name_ctx, origin, buf, buflen)
76 * Convert an RR to presentation format.
78 * Number of characters written to buf, or -1 (check errno).
81 ns_sprintrr(const ns_msg
*handle
, const ns_rr
*rr
,
82 const char *name_ctx
, const char *origin
,
83 char *buf
, size_t buflen
)
87 n
= ns_sprintrrf(ns_msg_base(*handle
), ns_msg_size(*handle
),
88 ns_rr_name(*rr
), ns_rr_class(*rr
), ns_rr_type(*rr
),
89 ns_rr_ttl(*rr
), ns_rr_rdata(*rr
), ns_rr_rdlen(*rr
),
90 name_ctx
, origin
, buf
, buflen
);
96 * ns_sprintrrf(msg, msglen, name, class, type, ttl, rdata, rdlen,
97 * name_ctx, origin, buf, buflen)
98 * Convert the fields of an RR into presentation format.
100 * Number of characters written to buf, or -1 (check errno).
103 ns_sprintrrf(const u_char
*msg
, size_t msglen
,
104 const char *name
, ns_class
class, ns_type type
,
105 u_long ttl
, const u_char
*rdata
, size_t rdlen
,
106 const char *name_ctx
, const char *origin
,
107 char *buf
, size_t buflen
)
109 const char *obuf
= buf
;
110 const u_char
*edata
= rdata
+ rdlen
;
120 if (name_ctx
!= NULL
&& ns_samename(name_ctx
, name
) == 1) {
121 T(addstr("\t\t\t", 3, &buf
, &buflen
));
123 len
= prune_origin(name
, origin
);
125 T(addstr("@\t\t\t", 4, &buf
, &buflen
));
127 T(addstr(name
, len
, &buf
, &buflen
));
128 /* Origin not used or not root, and no trailing dot? */
129 if (((origin
== NULL
|| origin
[0] == '\0') ||
130 (origin
[0] != '.' && origin
[1] != '\0' &&
131 name
[len
] == '\0')) && name
[len
- 1] != '.') {
132 T(addstr(".", 1, &buf
, &buflen
));
135 T(spaced
= addtab(len
, 24, spaced
, &buf
, &buflen
));
142 T(x
= ns_format_ttl(ttl
, buf
, buflen
));
143 addlen(x
, &buf
, &buflen
);
144 len
= SPRINTF((tmp
, " %s %s", p_class(class), p_type(type
)));
145 T(addstr(tmp
, len
, &buf
, &buflen
));
146 T(spaced
= addtab(x
+ len
, 16, spaced
, &buf
, &buflen
));
153 if (rdlen
!= NS_INADDRSZ
)
155 (void) inet_ntop(AF_INET
, rdata
, buf
, buflen
);
156 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
< 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
< 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
!= 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
< 2*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
< NS_INT16SZ
*3)
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
< NS_INT32SZ
+ 1)
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
));
449 char base64_key
[NS_MD5RSA_MAX_BASE64
];
450 u_int keyflags
, protocol
, algorithm
, key_id
;
454 if (rdlen
< NS_INT16SZ
+ NS_INT8SZ
+ NS_INT8SZ
)
457 /* Key flags, Protocol, Algorithm. */
458 key_id
= dst_s_dns_key_id(rdata
, edata
-rdata
);
459 keyflags
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
461 algorithm
= *rdata
++;
462 len
= SPRINTF((tmp
, "0x%04x %u %u",
463 keyflags
, protocol
, algorithm
));
464 T(addstr(tmp
, len
, &buf
, &buflen
));
466 /* Public key data. */
467 len
= b64_ntop(rdata
, edata
- rdata
,
468 base64_key
, sizeof base64_key
);
472 T(addstr(" (", 2, &buf
, &buflen
));
477 for (n
= 0; n
< len
; n
+= 48) {
478 T(addstr(leader
, strlen(leader
), &buf
, &buflen
));
479 T(addstr(base64_key
+ n
, MIN(len
- n
, 48),
483 T(addstr(" )", 2, &buf
, &buflen
));
484 n
= SPRINTF((tmp
, " ; key_tag= %u", key_id
));
485 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
));
556 /* Next domain name. */
557 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
561 for (c
= 0; c
< n
*8; c
++)
562 if (NS_NXT_BIT_ISSET(c
, rdata
)) {
563 len
= SPRINTF((tmp
, " %s", p_type(c
)));
564 T(addstr(tmp
, len
, &buf
, &buflen
));
570 u_int c_type
, key_tag
, alg
;
572 char base64_cert
[8192], *leader
, tmp
[40];
574 c_type
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
575 key_tag
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
576 alg
= (u_int
) *rdata
++;
578 len
= SPRINTF((tmp
, "%d %d %d ", c_type
, key_tag
, alg
));
579 T(addstr(tmp
, len
, &buf
, &buflen
));
580 siz
= (edata
-rdata
)*4/3 + 4; /* "+4" accounts for trailing \0 */
581 if (siz
> sizeof(base64_cert
) * 3/4) {
582 char *str
= "record too long to print";
583 T(addstr(str
, strlen(str
), &buf
, &buflen
));
586 len
= b64_ntop(rdata
, edata
-rdata
, base64_cert
, siz
);
591 T(addstr(" (", 2, &buf
, &buflen
));
598 for (n
= 0; n
< len
; n
+= 48) {
599 T(addstr(leader
, strlen(leader
),
601 T(addstr(base64_cert
+ n
, MIN(len
- n
, 48),
605 T(addstr(" )", 2, &buf
, &buflen
));
611 /* BEW - need to complete this */
614 T(len
= addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
615 T(addstr(" ", 1, &buf
, &buflen
));
616 rdata
+= 8; /* time */
617 n
= ns_get16(rdata
); rdata
+= INT16SZ
;
618 rdata
+= n
; /* sig */
619 n
= ns_get16(rdata
); rdata
+= INT16SZ
; /* original id */
620 sprintf(buf
, "%d", ns_get16(rdata
));
622 addlen(strlen(buf
), &buf
, &buflen
);
627 comment
= "unknown RR type";
632 comment
= "RR format error";
637 len
= SPRINTF((tmp
, "\\#(\t\t; %s", comment
));
638 T(addstr(tmp
, len
, &buf
, &buflen
));
639 while (rdata
< edata
) {
641 p
+= SPRINTF((p
, "\n\t"));
643 n
= MIN(16, edata
- rdata
);
644 for (m
= 0; m
< n
; m
++)
645 p
+= SPRINTF((p
, "%02x ", rdata
[m
]));
646 T(addstr(tmp
, p
- tmp
, &buf
, &buflen
));
648 T(addstr(")", 1, &buf
, &buflen
));
649 T(addtab(p
- tmp
+ 1, 48, spaced
, &buf
, &buflen
));
652 p
+= SPRINTF((p
, "; "));
653 for (m
= 0; m
< n
; m
++)
654 *p
++ = (isascii(rdata
[m
]) && isprint(rdata
[m
]))
657 T(addstr(tmp
, p
- tmp
, &buf
, &buflen
));
668 * prune_origin(name, origin)
669 * Find out if the name is at or under the current origin.
671 * Number of characters in name before start of origin,
672 * or length of name if origin does not match.
674 * This function should share code with samedomain().
677 prune_origin(const char *name
, const char *origin
) {
678 const char *oname
= name
;
680 while (*name
!= '\0') {
681 if (origin
!= NULL
&& ns_samename(name
, origin
) == 1)
682 return (name
- oname
- (name
> oname
));
683 while (*name
!= '\0') {
686 /* XXX need to handle \nnn form. */
689 } else if (*name
== '.') {
696 return (name
- oname
);
701 * charstr(rdata, edata, buf, buflen)
702 * Format a <character-string> into the presentation buffer.
704 * Number of rdata octets consumed
705 * 0 for protocol format error
706 * -1 for output buffer error
708 * buffer is advanced on success.
711 charstr(const u_char
*rdata
, const u_char
*edata
, char **buf
, size_t *buflen
) {
712 const u_char
*odata
= rdata
;
713 size_t save_buflen
= *buflen
;
714 char *save_buf
= *buf
;
716 if (addstr("\"", 1, buf
, buflen
) < 0)
721 if (rdata
+ 1 + n
<= edata
) {
724 if (strchr("\n\"\\", *rdata
) != NULL
)
725 if (addstr("\\", 1, buf
, buflen
) < 0)
727 if (addstr((const char *)rdata
, 1,
734 if (addstr("\"", 1, buf
, buflen
) < 0)
736 return (rdata
- odata
);
738 __set_errno (ENOSPC
);
740 *buflen
= save_buflen
;
745 addname(const u_char
*msg
, size_t msglen
,
746 const u_char
**pp
, const char *origin
,
747 char **buf
, size_t *buflen
)
749 size_t newlen
, save_buflen
= *buflen
;
750 char *save_buf
= *buf
;
753 n
= dn_expand(msg
, msg
+ msglen
, *pp
, *buf
, *buflen
);
755 goto enospc
; /* Guess. */
756 newlen
= prune_origin(*buf
, origin
);
758 /* Use "@" instead of name. */
759 if (newlen
+ 2 > *buflen
)
760 goto enospc
; /* No room for "@\0". */
761 (*buf
)[newlen
++] = '@';
762 (*buf
)[newlen
] = '\0';
764 if (((origin
== NULL
|| origin
[0] == '\0') ||
765 (origin
[0] != '.' && origin
[1] != '\0' &&
766 (*buf
)[newlen
] == '\0')) && (*buf
)[newlen
- 1] != '.') {
767 /* No trailing dot. */
768 if (newlen
+ 2 > *buflen
)
769 goto enospc
; /* No room for ".\0". */
770 (*buf
)[newlen
++] = '.';
771 (*buf
)[newlen
] = '\0';
775 addlen(newlen
, buf
, buflen
);
779 __set_errno (ENOSPC
);
781 *buflen
= save_buflen
;
786 addlen(size_t len
, char **buf
, size_t *buflen
) {
787 assert(len
<= *buflen
);
793 addstr(const char *src
, size_t len
, char **buf
, size_t *buflen
) {
794 if (len
>= *buflen
) {
795 __set_errno (ENOSPC
);
798 memcpy(*buf
, src
, len
);
799 addlen(len
, buf
, buflen
);
805 addtab(size_t len
, size_t target
, int spaced
, char **buf
, size_t *buflen
) {
806 size_t save_buflen
= *buflen
;
807 char *save_buf
= *buf
;
810 if (spaced
|| len
>= target
- 1) {
811 T(addstr(" ", 2, buf
, buflen
));
814 for (t
= (target
- len
- 1) / 8; t
>= 0; t
--)
815 if (addstr("\t", 1, buf
, buflen
) < 0) {
816 *buflen
= save_buflen
;