1 /* 2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 3 * Copyright (c) 1996-1999 by Internet Software Consortium. 4 * 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. 8 * 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. 16 */ 17 18 #include "port_before.h" 19 20 #include <sys/types.h> 21 #include <sys/socket.h> 22 23 #include <netinet/in.h> 24 #include <arpa/nameser.h> 25 #include <arpa/inet.h> 26 27 #include <isc/assertions.h> 28 #include <isc/dst.h> 29 #include <errno.h> 30 #include <resolv.h> 31 #include <string.h> 32 #include <ctype.h> 33 34 #include "port_after.h" 35 36 #ifdef SPRINTF_CHAR 37 # define SPRINTF(x) strlen(sprintf/**/x) 38 #else 39 # define SPRINTF(x) ((size_t)sprintf x) 40 #endif 41 42 /* Forward. */ 43 44 static size_t prune_origin(const char *name, const char *origin); 45 static int charstr(const u_char *rdata, const u_char *edata, 46 char **buf, size_t *buflen); 47 static int addname(const u_char *msg, size_t msglen, 48 const u_char **p, const char *origin, 49 char **buf, size_t *buflen); 50 static void addlen(size_t len, char **buf, size_t *buflen); 51 static int addstr(const char *src, size_t len, 52 char **buf, size_t *buflen); 53 static int addtab(size_t len, size_t target, int spaced, 54 char **buf, size_t *buflen); 55 56 /* Macros. */ 57 58 #define T(x) \ 59 do { \ 60 if ((x) < 0) \ 61 return (-1); \ 62 } while (0) 63 64 static const char base32hex[] = 65 "0123456789ABCDEFGHIJKLMNOPQRSTUV=0123456789abcdefghijklmnopqrstuv"; 66 67 /* Public. */ 68 69 /*% 70 * Convert an RR to presentation format. 71 * 72 * return: 73 *\li Number of characters written to buf, or -1 (check errno). 74 */ 75 int 76 ns_sprintrr(const ns_msg *handle, const ns_rr *rr, 77 const char *name_ctx, const char *origin, 78 char *buf, size_t buflen) 79 { 80 int n; 81 82 n = ns_sprintrrf(ns_msg_base(*handle), ns_msg_size(*handle), 83 ns_rr_name(*rr), ns_rr_class(*rr), ns_rr_type(*rr), 84 ns_rr_ttl(*rr), ns_rr_rdata(*rr), ns_rr_rdlen(*rr), 85 name_ctx, origin, buf, buflen); 86 return (n); 87 } 88 89 /*% 90 * Convert the fields of an RR into presentation format. 91 * 92 * return: 93 *\li Number of characters written to buf, or -1 (check errno). 94 */ 95 int 96 ns_sprintrrf(const u_char *msg, size_t msglen, 97 const char *name, ns_class class, ns_type type, 98 u_long ttl, const u_char *rdata, size_t rdlen, 99 const char *name_ctx, const char *origin, 100 char *buf, size_t buflen) 101 { 102 const char *obuf = buf; 103 const u_char *edata = rdata + rdlen; 104 int spaced = 0; 105 106 const char *comment; 107 char tmp[100]; 108 int len, x; 109 110 /* 111 * Owner. 112 */ 113 if (name_ctx != NULL && ns_samename(name_ctx, name) == 1) { 114 T(addstr("\t\t\t", 3, &buf, &buflen)); 115 } else { 116 len = prune_origin(name, origin); 117 if (*name == '\0') { 118 goto root; 119 } else if (len == 0) { 120 T(addstr("@\t\t\t", 4, &buf, &buflen)); 121 } else { 122 T(addstr(name, len, &buf, &buflen)); 123 /* Origin not used or not root, and no trailing dot? */ 124 if (((origin == NULL || origin[0] == '\0') || 125 (origin[0] != '.' && origin[1] != '\0' && 126 name[len] == '\0')) && name[len - 1] != '.') { 127 root: 128 T(addstr(".", 1, &buf, &buflen)); 129 len++; 130 } 131 T(spaced = addtab(len, 24, spaced, &buf, &buflen)); 132 } 133 } 134 135 /* 136 * TTL, Class, Type. 137 */ 138 T(x = ns_format_ttl(ttl, buf, buflen)); 139 addlen(x, &buf, &buflen); 140 len = SPRINTF((tmp, " %s %s", p_class(class), p_type(type))); 141 T(addstr(tmp, len, &buf, &buflen)); 142 T(spaced = addtab(x + len, 16, spaced, &buf, &buflen)); 143 144 /* 145 * RData. 146 */ 147 switch (type) { 148 case ns_t_a: 149 if (rdlen != (size_t)NS_INADDRSZ) 150 goto formerr; 151 (void) inet_ntop(AF_INET, rdata, buf, buflen); 152 addlen(strlen(buf), &buf, &buflen); 153 break; 154 155 case ns_t_cname: 156 case ns_t_mb: 157 case ns_t_mg: 158 case ns_t_mr: 159 case ns_t_ns: 160 case ns_t_ptr: 161 case ns_t_dname: 162 T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); 163 break; 164 165 case ns_t_hinfo: 166 case ns_t_isdn: 167 /* First word. */ 168 T(len = charstr(rdata, edata, &buf, &buflen)); 169 if (len == 0) 170 goto formerr; 171 rdata += len; 172 T(addstr(" ", 1, &buf, &buflen)); 173 174 175 /* Second word, optional in ISDN records. */ 176 if (type == ns_t_isdn && rdata == edata) 177 break; 178 179 T(len = charstr(rdata, edata, &buf, &buflen)); 180 if (len == 0) 181 goto formerr; 182 rdata += len; 183 break; 184 185 case ns_t_soa: { 186 u_long t; 187 188 /* Server name. */ 189 T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); 190 T(addstr(" ", 1, &buf, &buflen)); 191 192 /* Administrator name. */ 193 T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); 194 T(addstr(" (\n", 3, &buf, &buflen)); 195 spaced = 0; 196 197 if ((edata - rdata) != 5*NS_INT32SZ) 198 goto formerr; 199 200 /* Serial number. */ 201 t = ns_get32(rdata); rdata += NS_INT32SZ; 202 T(addstr("\t\t\t\t\t", 5, &buf, &buflen)); 203 len = SPRINTF((tmp, "%lu", t)); 204 T(addstr(tmp, len, &buf, &buflen)); 205 T(spaced = addtab(len, 16, spaced, &buf, &buflen)); 206 T(addstr("; serial\n", 9, &buf, &buflen)); 207 spaced = 0; 208 209 /* Refresh interval. */ 210 t = ns_get32(rdata); rdata += NS_INT32SZ; 211 T(addstr("\t\t\t\t\t", 5, &buf, &buflen)); 212 T(len = ns_format_ttl(t, buf, buflen)); 213 addlen(len, &buf, &buflen); 214 T(spaced = addtab(len, 16, spaced, &buf, &buflen)); 215 T(addstr("; refresh\n", 10, &buf, &buflen)); 216 spaced = 0; 217 218 /* Retry interval. */ 219 t = ns_get32(rdata); rdata += NS_INT32SZ; 220 T(addstr("\t\t\t\t\t", 5, &buf, &buflen)); 221 T(len = ns_format_ttl(t, buf, buflen)); 222 addlen(len, &buf, &buflen); 223 T(spaced = addtab(len, 16, spaced, &buf, &buflen)); 224 T(addstr("; retry\n", 8, &buf, &buflen)); 225 spaced = 0; 226 227 /* Expiry. */ 228 t = ns_get32(rdata); rdata += NS_INT32SZ; 229 T(addstr("\t\t\t\t\t", 5, &buf, &buflen)); 230 T(len = ns_format_ttl(t, buf, buflen)); 231 addlen(len, &buf, &buflen); 232 T(spaced = addtab(len, 16, spaced, &buf, &buflen)); 233 T(addstr("; expiry\n", 9, &buf, &buflen)); 234 spaced = 0; 235 236 /* Minimum TTL. */ 237 t = ns_get32(rdata); rdata += NS_INT32SZ; 238 T(addstr("\t\t\t\t\t", 5, &buf, &buflen)); 239 T(len = ns_format_ttl(t, buf, buflen)); 240 addlen(len, &buf, &buflen); 241 T(addstr(" )", 2, &buf, &buflen)); 242 T(spaced = addtab(len, 16, spaced, &buf, &buflen)); 243 T(addstr("; minimum\n", 10, &buf, &buflen)); 244 245 break; 246 } 247 248 case ns_t_mx: 249 case ns_t_afsdb: 250 case ns_t_rt: 251 case ns_t_kx: { 252 u_int t; 253 254 if (rdlen < (size_t)NS_INT16SZ) 255 goto formerr; 256 257 /* Priority. */ 258 t = ns_get16(rdata); 259 rdata += NS_INT16SZ; 260 len = SPRINTF((tmp, "%u ", t)); 261 T(addstr(tmp, len, &buf, &buflen)); 262 263 /* Target. */ 264 T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); 265 266 break; 267 } 268 269 case ns_t_px: { 270 u_int t; 271 272 if (rdlen < (size_t)NS_INT16SZ) 273 goto formerr; 274 275 /* Priority. */ 276 t = ns_get16(rdata); 277 rdata += NS_INT16SZ; 278 len = SPRINTF((tmp, "%u ", t)); 279 T(addstr(tmp, len, &buf, &buflen)); 280 281 /* Name1. */ 282 T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); 283 T(addstr(" ", 1, &buf, &buflen)); 284 285 /* Name2. */ 286 T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); 287 288 break; 289 } 290 291 case ns_t_x25: 292 T(len = charstr(rdata, edata, &buf, &buflen)); 293 if (len == 0) 294 goto formerr; 295 rdata += len; 296 break; 297 298 case ns_t_txt: 299 case ns_t_spf: 300 while (rdata < edata) { 301 T(len = charstr(rdata, edata, &buf, &buflen)); 302 if (len == 0) 303 goto formerr; 304 rdata += len; 305 if (rdata < edata) 306 T(addstr(" ", 1, &buf, &buflen)); 307 } 308 break; 309 310 case ns_t_nsap: { 311 char t[2+255*3]; 312 313 (void) inet_nsap_ntoa(rdlen, rdata, t); 314 T(addstr(t, strlen(t), &buf, &buflen)); 315 break; 316 } 317 318 case ns_t_aaaa: 319 if (rdlen != (size_t)NS_IN6ADDRSZ) 320 goto formerr; 321 (void) inet_ntop(AF_INET6, rdata, buf, buflen); 322 addlen(strlen(buf), &buf, &buflen); 323 break; 324 325 case ns_t_loc: { 326 char t[255]; 327 328 /* XXX protocol format checking? */ 329 (void) loc_ntoa(rdata, t); 330 T(addstr(t, strlen(t), &buf, &buflen)); 331 break; 332 } 333 334 case ns_t_naptr: { 335 u_int order, preference; 336 char t[50]; 337 338 if (rdlen < 2U*NS_INT16SZ) 339 goto formerr; 340 341 /* Order, Precedence. */ 342 order = ns_get16(rdata); rdata += NS_INT16SZ; 343 preference = ns_get16(rdata); rdata += NS_INT16SZ; 344 len = SPRINTF((t, "%u %u ", order, preference)); 345 T(addstr(t, len, &buf, &buflen)); 346 347 /* Flags. */ 348 T(len = charstr(rdata, edata, &buf, &buflen)); 349 if (len == 0) 350 goto formerr; 351 rdata += len; 352 T(addstr(" ", 1, &buf, &buflen)); 353 354 /* Service. */ 355 T(len = charstr(rdata, edata, &buf, &buflen)); 356 if (len == 0) 357 goto formerr; 358 rdata += len; 359 T(addstr(" ", 1, &buf, &buflen)); 360 361 /* Regexp. */ 362 T(len = charstr(rdata, edata, &buf, &buflen)); 363 if (len < 0) 364 return (-1); 365 if (len == 0) 366 goto formerr; 367 rdata += len; 368 T(addstr(" ", 1, &buf, &buflen)); 369 370 /* Server. */ 371 T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); 372 break; 373 } 374 375 case ns_t_srv: { 376 u_int priority, weight, port; 377 char t[50]; 378 379 if (rdlen < 3U*NS_INT16SZ) 380 goto formerr; 381 382 /* Priority, Weight, Port. */ 383 priority = ns_get16(rdata); rdata += NS_INT16SZ; 384 weight = ns_get16(rdata); rdata += NS_INT16SZ; 385 port = ns_get16(rdata); rdata += NS_INT16SZ; 386 len = SPRINTF((t, "%u %u %u ", priority, weight, port)); 387 T(addstr(t, len, &buf, &buflen)); 388 389 /* Server. */ 390 T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); 391 break; 392 } 393 394 case ns_t_minfo: 395 case ns_t_rp: 396 /* Name1. */ 397 T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); 398 T(addstr(" ", 1, &buf, &buflen)); 399 400 /* Name2. */ 401 T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); 402 403 break; 404 405 case ns_t_wks: { 406 int n, lcnt; 407 408 if (rdlen < 1U + NS_INT32SZ) 409 goto formerr; 410 411 /* Address. */ 412 (void) inet_ntop(AF_INET, rdata, buf, buflen); 413 addlen(strlen(buf), &buf, &buflen); 414 rdata += NS_INADDRSZ; 415 416 /* Protocol. */ 417 len = SPRINTF((tmp, " %u ( ", *rdata)); 418 T(addstr(tmp, len, &buf, &buflen)); 419 rdata += NS_INT8SZ; 420 421 /* Bit map. */ 422 n = 0; 423 lcnt = 0; 424 while (rdata < edata) { 425 u_int c = *rdata++; 426 do { 427 if (c & 0200) { 428 if (lcnt == 0) { 429 T(addstr("\n\t\t\t\t", 5, 430 &buf, &buflen)); 431 lcnt = 10; 432 spaced = 0; 433 } 434 len = SPRINTF((tmp, "%d ", n)); 435 T(addstr(tmp, len, &buf, &buflen)); 436 lcnt--; 437 } 438 c <<= 1; 439 } while (++n & 07); 440 } 441 T(addstr(")", 1, &buf, &buflen)); 442 443 break; 444 } 445 446 case ns_t_key: 447 case ns_t_dnskey: { 448 char base64_key[NS_MD5RSA_MAX_BASE64]; 449 u_int keyflags, protocol, algorithm, key_id; 450 const char *leader; 451 int n; 452 453 if (rdlen < 0U + NS_INT16SZ + NS_INT8SZ + NS_INT8SZ) 454 goto formerr; 455 456 /* Key flags, Protocol, Algorithm. */ 457 key_id = dst_s_dns_key_id(rdata, edata-rdata); 458 keyflags = ns_get16(rdata); rdata += NS_INT16SZ; 459 protocol = *rdata++; 460 algorithm = *rdata++; 461 len = SPRINTF((tmp, "0x%04x %u %u", 462 keyflags, protocol, algorithm)); 463 T(addstr(tmp, len, &buf, &buflen)); 464 465 /* Public key data. */ 466 len = b64_ntop(rdata, edata - rdata, 467 base64_key, sizeof base64_key); 468 if (len < 0) 469 goto formerr; 470 if (len > 15) { 471 T(addstr(" (", 2, &buf, &buflen)); 472 leader = "\n\t\t"; 473 spaced = 0; 474 } else 475 leader = " "; 476 for (n = 0; n < len; n += 48) { 477 T(addstr(leader, strlen(leader), &buf, &buflen)); 478 T(addstr(base64_key + n, MIN(len - n, 48), 479 &buf, &buflen)); 480 } 481 if (len > 15) 482 T(addstr(" )", 2, &buf, &buflen)); 483 n = SPRINTF((tmp, " ; key_tag= %u", key_id)); 484 T(addstr(tmp, n, &buf, &buflen)); 485 486 break; 487 } 488 489 case ns_t_sig: 490 case ns_t_rrsig: { 491 char base64_key[NS_MD5RSA_MAX_BASE64]; 492 u_int type, algorithm, labels, footprint; 493 const char *leader; 494 u_long t; 495 int n; 496 497 if (rdlen < 22U) 498 goto formerr; 499 500 /* Type covered, Algorithm, Label count, Original TTL. */ 501 type = ns_get16(rdata); rdata += NS_INT16SZ; 502 algorithm = *rdata++; 503 labels = *rdata++; 504 t = ns_get32(rdata); rdata += NS_INT32SZ; 505 len = SPRINTF((tmp, "%s %d %d %lu ", 506 p_type(type), algorithm, labels, t)); 507 T(addstr(tmp, len, &buf, &buflen)); 508 if (labels > (u_int)dn_count_labels(name)) 509 goto formerr; 510 511 /* Signature expiry. */ 512 t = ns_get32(rdata); rdata += NS_INT32SZ; 513 len = SPRINTF((tmp, "%s ", p_secstodate(t))); 514 T(addstr(tmp, len, &buf, &buflen)); 515 516 /* Time signed. */ 517 t = ns_get32(rdata); rdata += NS_INT32SZ; 518 len = SPRINTF((tmp, "%s ", p_secstodate(t))); 519 T(addstr(tmp, len, &buf, &buflen)); 520 521 /* Signature Footprint. */ 522 footprint = ns_get16(rdata); rdata += NS_INT16SZ; 523 len = SPRINTF((tmp, "%u ", footprint)); 524 T(addstr(tmp, len, &buf, &buflen)); 525 526 /* Signer's name. */ 527 T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); 528 529 /* Signature. */ 530 len = b64_ntop(rdata, edata - rdata, 531 base64_key, sizeof base64_key); 532 if (len > 15) { 533 T(addstr(" (", 2, &buf, &buflen)); 534 leader = "\n\t\t"; 535 spaced = 0; 536 } else 537 leader = " "; 538 if (len < 0) 539 goto formerr; 540 for (n = 0; n < len; n += 48) { 541 T(addstr(leader, strlen(leader), &buf, &buflen)); 542 T(addstr(base64_key + n, MIN(len - n, 48), 543 &buf, &buflen)); 544 } 545 if (len > 15) 546 T(addstr(" )", 2, &buf, &buflen)); 547 break; 548 } 549 550 case ns_t_nxt: { 551 int n, c; 552 553 /* Next domain name. */ 554 T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); 555 556 /* Type bit map. */ 557 n = edata - rdata; 558 for (c = 0; c < n*8; c++) 559 if (NS_NXT_BIT_ISSET(c, rdata)) { 560 len = SPRINTF((tmp, " %s", p_type(c))); 561 T(addstr(tmp, len, &buf, &buflen)); 562 } 563 break; 564 } 565 566 case ns_t_cert: { 567 u_int c_type, key_tag, alg; 568 int n; 569 unsigned int siz; 570 char base64_cert[8192], tmp[40]; 571 const char *leader; 572 573 c_type = ns_get16(rdata); rdata += NS_INT16SZ; 574 key_tag = ns_get16(rdata); rdata += NS_INT16SZ; 575 alg = (u_int) *rdata++; 576 577 len = SPRINTF((tmp, "%d %d %d ", c_type, key_tag, alg)); 578 T(addstr(tmp, len, &buf, &buflen)); 579 siz = (edata-rdata)*4/3 + 4; /* "+4" accounts for trailing \0 */ 580 if (siz > sizeof(base64_cert) * 3/4) { 581 const char *str = "record too long to print"; 582 T(addstr(str, strlen(str), &buf, &buflen)); 583 } 584 else { 585 len = b64_ntop(rdata, edata-rdata, base64_cert, siz); 586 587 if (len < 0) 588 goto formerr; 589 else if (len > 15) { 590 T(addstr(" (", 2, &buf, &buflen)); 591 leader = "\n\t\t"; 592 spaced = 0; 593 } 594 else 595 leader = " "; 596 597 for (n = 0; n < len; n += 48) { 598 T(addstr(leader, strlen(leader), 599 &buf, &buflen)); 600 T(addstr(base64_cert + n, MIN(len - n, 48), 601 &buf, &buflen)); 602 } 603 if (len > 15) 604 T(addstr(" )", 2, &buf, &buflen)); 605 } 606 break; 607 } 608 609 case ns_t_tkey: { 610 /* KJD - need to complete this */ 611 u_long t; 612 int mode, err, keysize; 613 614 /* Algorithm name. */ 615 T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); 616 T(addstr(" ", 1, &buf, &buflen)); 617 618 /* Inception. */ 619 t = ns_get32(rdata); rdata += NS_INT32SZ; 620 len = SPRINTF((tmp, "%s ", p_secstodate(t))); 621 T(addstr(tmp, len, &buf, &buflen)); 622 623 /* Experation. */ 624 t = ns_get32(rdata); rdata += NS_INT32SZ; 625 len = SPRINTF((tmp, "%s ", p_secstodate(t))); 626 T(addstr(tmp, len, &buf, &buflen)); 627 628 /* Mode , Error, Key Size. */ 629 /* Priority, Weight, Port. */ 630 mode = ns_get16(rdata); rdata += NS_INT16SZ; 631 err = ns_get16(rdata); rdata += NS_INT16SZ; 632 keysize = ns_get16(rdata); rdata += NS_INT16SZ; 633 len = SPRINTF((tmp, "%u %u %u ", mode, err, keysize)); 634 T(addstr(tmp, len, &buf, &buflen)); 635 636 /* XXX need to dump key, print otherdata length & other data */ 637 break; 638 } 639 640 case ns_t_tsig: { 641 /* BEW - need to complete this */ 642 int n; 643 644 T(len = addname(msg, msglen, &rdata, origin, &buf, &buflen)); 645 T(addstr(" ", 1, &buf, &buflen)); 646 rdata += 8; /*%< time */ 647 n = ns_get16(rdata); rdata += INT16SZ; 648 rdata += n; /*%< sig */ 649 n = ns_get16(rdata); rdata += INT16SZ; /*%< original id */ 650 sprintf(buf, "%d", ns_get16(rdata)); 651 rdata += INT16SZ; 652 addlen(strlen(buf), &buf, &buflen); 653 break; 654 } 655 656 case ns_t_a6: { 657 struct in6_addr a; 658 int pbyte, pbit; 659 660 /* prefix length */ 661 if (rdlen == 0U) goto formerr; 662 len = SPRINTF((tmp, "%d ", *rdata)); 663 T(addstr(tmp, len, &buf, &buflen)); 664 pbit = *rdata; 665 if (pbit > 128) goto formerr; 666 pbyte = (pbit & ~7) / 8; 667 rdata++; 668 669 /* address suffix: provided only when prefix len != 128 */ 670 if (pbit < 128) { 671 if (rdata + pbyte >= edata) goto formerr; 672 memset(&a, 0, sizeof(a)); 673 memcpy(&a.s6_addr[pbyte], rdata, sizeof(a) - pbyte); 674 (void) inet_ntop(AF_INET6, &a, buf, buflen); 675 addlen(strlen(buf), &buf, &buflen); 676 rdata += sizeof(a) - pbyte; 677 } 678 679 /* prefix name: provided only when prefix len > 0 */ 680 if (pbit == 0) 681 break; 682 if (rdata >= edata) goto formerr; 683 T(addstr(" ", 1, &buf, &buflen)); 684 T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); 685 686 break; 687 } 688 689 case ns_t_opt: { 690 len = SPRINTF((tmp, "%u bytes", class)); 691 T(addstr(tmp, len, &buf, &buflen)); 692 break; 693 } 694 695 case ns_t_ds: 696 case ns_t_dlv: 697 case ns_t_sshfp: { 698 u_int t; 699 700 if (type == ns_t_ds || type == ns_t_dlv) { 701 if (rdlen < 4U) goto formerr; 702 t = ns_get16(rdata); 703 rdata += NS_INT16SZ; 704 len = SPRINTF((tmp, "%u ", t)); 705 T(addstr(tmp, len, &buf, &buflen)); 706 } else 707 if (rdlen < 2U) goto formerr; 708 709 len = SPRINTF((tmp, "%u ", *rdata)); 710 T(addstr(tmp, len, &buf, &buflen)); 711 rdata++; 712 713 len = SPRINTF((tmp, "%u ", *rdata)); 714 T(addstr(tmp, len, &buf, &buflen)); 715 rdata++; 716 717 while (rdata < edata) { 718 len = SPRINTF((tmp, "%02X", *rdata)); 719 T(addstr(tmp, len, &buf, &buflen)); 720 rdata++; 721 } 722 break; 723 } 724 725 case ns_t_nsec3: 726 case ns_t_nsec3param: { 727 u_int t, w, l, j, k, c; 728 729 len = SPRINTF((tmp, "%u ", *rdata)); 730 T(addstr(tmp, len, &buf, &buflen)); 731 rdata++; 732 733 len = SPRINTF((tmp, "%u ", *rdata)); 734 T(addstr(tmp, len, &buf, &buflen)); 735 rdata++; 736 737 t = ns_get16(rdata); 738 rdata += NS_INT16SZ; 739 len = SPRINTF((tmp, "%u ", t)); 740 T(addstr(tmp, len, &buf, &buflen)); 741 742 t = *rdata++; 743 if (t == 0) { 744 T(addstr("-", 1, &buf, &buflen)); 745 } else { 746 while (t-- > 0) { 747 len = SPRINTF((tmp, "%02X", *rdata)); 748 T(addstr(tmp, len, &buf, &buflen)); 749 rdata++; 750 } 751 } 752 if (type == ns_t_nsec3param) 753 break; 754 T(addstr(" ", 1, &buf, &buflen)); 755 756 t = *rdata++; 757 while (t > 0) { 758 switch (t) { 759 case 1: 760 tmp[0] = base32hex[((rdata[0]>>3)&0x1f)]; 761 tmp[1] = base32hex[((rdata[0]<<2)&0x1c)]; 762 tmp[2] = tmp[3] = tmp[4] = '='; 763 tmp[5] = tmp[6] = tmp[7] = '='; 764 break; 765 case 2: 766 tmp[0] = base32hex[((rdata[0]>>3)&0x1f)]; 767 tmp[1] = base32hex[((rdata[0]<<2)&0x1c)| 768 ((rdata[1]>>6)&0x03)]; 769 tmp[2] = base32hex[((rdata[1]>>1)&0x1f)]; 770 tmp[3] = base32hex[((rdata[1]<<4)&0x10)]; 771 tmp[4] = tmp[5] = tmp[6] = tmp[7] = '='; 772 break; 773 case 3: 774 tmp[0] = base32hex[((rdata[0]>>3)&0x1f)]; 775 tmp[1] = base32hex[((rdata[0]<<2)&0x1c)| 776 ((rdata[1]>>6)&0x03)]; 777 tmp[2] = base32hex[((rdata[1]>>1)&0x1f)]; 778 tmp[3] = base32hex[((rdata[1]<<4)&0x10)| 779 ((rdata[2]>>4)&0x0f)]; 780 tmp[4] = base32hex[((rdata[2]<<1)&0x1e)]; 781 tmp[5] = tmp[6] = tmp[7] = '='; 782 break; 783 case 4: 784 tmp[0] = base32hex[((rdata[0]>>3)&0x1f)]; 785 tmp[1] = base32hex[((rdata[0]<<2)&0x1c)| 786 ((rdata[1]>>6)&0x03)]; 787 tmp[2] = base32hex[((rdata[1]>>1)&0x1f)]; 788 tmp[3] = base32hex[((rdata[1]<<4)&0x10)| 789 ((rdata[2]>>4)&0x0f)]; 790 tmp[4] = base32hex[((rdata[2]<<1)&0x1e)| 791 ((rdata[3]>>7)&0x01)]; 792 tmp[5] = base32hex[((rdata[3]>>2)&0x1f)]; 793 tmp[6] = base32hex[(rdata[3]<<3)&0x18]; 794 tmp[7] = '='; 795 break; 796 default: 797 tmp[0] = base32hex[((rdata[0]>>3)&0x1f)]; 798 tmp[1] = base32hex[((rdata[0]<<2)&0x1c)| 799 ((rdata[1]>>6)&0x03)]; 800 tmp[2] = base32hex[((rdata[1]>>1)&0x1f)]; 801 tmp[3] = base32hex[((rdata[1]<<4)&0x10)| 802 ((rdata[2]>>4)&0x0f)]; 803 tmp[4] = base32hex[((rdata[2]<<1)&0x1e)| 804 ((rdata[3]>>7)&0x01)]; 805 tmp[5] = base32hex[((rdata[3]>>2)&0x1f)]; 806 tmp[6] = base32hex[((rdata[3]<<3)&0x18)| 807 ((rdata[4]>>5)&0x07)]; 808 tmp[7] = base32hex[(rdata[4]&0x1f)]; 809 break; 810 } 811 T(addstr(tmp, 8, &buf, &buflen)); 812 if (t >= 5) { 813 rdata += 5; 814 t -= 5; 815 } else { 816 rdata += t; 817 t -= t; 818 } 819 } 820 821 while (rdata < edata) { 822 w = *rdata++; 823 l = *rdata++; 824 for (j = 0; j < l; j++) { 825 if (rdata[j] == 0) 826 continue; 827 for (k = 0; k < 8; k++) { 828 if ((rdata[j] & (0x80 >> k)) == 0) 829 continue; 830 c = w * 256 + j * 8 + k; 831 len = SPRINTF((tmp, " %s", p_type(c))); 832 T(addstr(tmp, len, &buf, &buflen)); 833 } 834 } 835 rdata += l; 836 } 837 break; 838 } 839 840 case ns_t_nsec: { 841 u_int w, l, j, k, c; 842 843 T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); 844 845 while (rdata < edata) { 846 w = *rdata++; 847 l = *rdata++; 848 for (j = 0; j < l; j++) { 849 if (rdata[j] == 0) 850 continue; 851 for (k = 0; k < 8; k++) { 852 if ((rdata[j] & (0x80 >> k)) == 0) 853 continue; 854 c = w * 256 + j * 8 + k; 855 len = SPRINTF((tmp, " %s", p_type(c))); 856 T(addstr(tmp, len, &buf, &buflen)); 857 } 858 } 859 rdata += l; 860 } 861 break; 862 } 863 864 case ns_t_dhcid: { 865 int n; 866 unsigned int siz; 867 char base64_dhcid[8192]; 868 const char *leader; 869 870 siz = (edata-rdata)*4/3 + 4; /* "+4" accounts for trailing \0 */ 871 if (siz > sizeof(base64_dhcid) * 3/4) { 872 const char *str = "record too long to print"; 873 T(addstr(str, strlen(str), &buf, &buflen)); 874 } else { 875 len = b64_ntop(rdata, edata-rdata, base64_dhcid, siz); 876 877 if (len < 0) 878 goto formerr; 879 880 else if (len > 15) { 881 T(addstr(" (", 2, &buf, &buflen)); 882 leader = "\n\t\t"; 883 spaced = 0; 884 } 885 else 886 leader = " "; 887 888 for (n = 0; n < len; n += 48) { 889 T(addstr(leader, strlen(leader), 890 &buf, &buflen)); 891 T(addstr(base64_dhcid + n, MIN(len - n, 48), 892 &buf, &buflen)); 893 } 894 if (len > 15) 895 T(addstr(" )", 2, &buf, &buflen)); 896 } 897 } 898 899 case ns_t_ipseckey: { 900 int n; 901 unsigned int siz; 902 char base64_key[8192]; 903 const char *leader; 904 905 if (rdlen < 2) 906 goto formerr; 907 908 switch (rdata[1]) { 909 case 0: 910 case 3: 911 if (rdlen < 3) 912 goto formerr; 913 break; 914 case 1: 915 if (rdlen < 7) 916 goto formerr; 917 break; 918 case 2: 919 if (rdlen < 19) 920 goto formerr; 921 break; 922 default: 923 comment = "unknown IPSECKEY gateway type"; 924 goto hexify; 925 } 926 927 len = SPRINTF((tmp, "%u ", *rdata)); 928 T(addstr(tmp, len, &buf, &buflen)); 929 rdata++; 930 931 len = SPRINTF((tmp, "%u ", *rdata)); 932 T(addstr(tmp, len, &buf, &buflen)); 933 rdata++; 934 935 len = SPRINTF((tmp, "%u ", *rdata)); 936 T(addstr(tmp, len, &buf, &buflen)); 937 rdata++; 938 939 switch (rdata[-2]) { 940 case 0: 941 T(addstr(".", 1, &buf, &buflen)); 942 break; 943 case 1: 944 (void) inet_ntop(AF_INET, rdata, buf, buflen); 945 addlen(strlen(buf), &buf, &buflen); 946 rdata += 4; 947 break; 948 case 2: 949 (void) inet_ntop(AF_INET6, rdata, buf, buflen); 950 addlen(strlen(buf), &buf, &buflen); 951 rdata += 16; 952 break; 953 case 3: 954 T(addname(msg, msglen, &rdata, origin, &buf, &buflen)); 955 break; 956 } 957 958 if (rdata >= edata) 959 break; 960 961 siz = (edata-rdata)*4/3 + 4; /* "+4" accounts for trailing \0 */ 962 if (siz > sizeof(base64_key) * 3/4) { 963 const char *str = "record too long to print"; 964 T(addstr(str, strlen(str), &buf, &buflen)); 965 } else { 966 len = b64_ntop(rdata, edata-rdata, base64_key, siz); 967 968 if (len < 0) 969 goto formerr; 970 971 else if (len > 15) { 972 T(addstr(" (", 2, &buf, &buflen)); 973 leader = "\n\t\t"; 974 spaced = 0; 975 } 976 else 977 leader = " "; 978 979 for (n = 0; n < len; n += 48) { 980 T(addstr(leader, strlen(leader), 981 &buf, &buflen)); 982 T(addstr(base64_key + n, MIN(len - n, 48), 983 &buf, &buflen)); 984 } 985 if (len > 15) 986 T(addstr(" )", 2, &buf, &buflen)); 987 } 988 } 989 990 case ns_t_hip: { 991 unsigned int i, hip_len, algorithm, key_len; 992 char base64_key[NS_MD5RSA_MAX_BASE64]; 993 unsigned int siz; 994 const char *leader = "\n\t\t\t\t\t"; 995 996 hip_len = *rdata++; 997 algorithm = *rdata++; 998 key_len = ns_get16(rdata); 999 rdata += NS_INT16SZ; 1000 1001 siz = key_len*4/3 + 4; /* "+4" accounts for trailing \0 */ 1002 if (siz > sizeof(base64_key) * 3/4) { 1003 const char *str = "record too long to print"; 1004 T(addstr(str, strlen(str), &buf, &buflen)); 1005 } else { 1006 len = sprintf(tmp, "( %u ", algorithm); 1007 T(addstr(tmp, len, &buf, &buflen)); 1008 1009 for (i = 0; i < hip_len; i++) { 1010 len = sprintf(tmp, "%02X", *rdata); 1011 T(addstr(tmp, len, &buf, &buflen)); 1012 rdata++; 1013 } 1014 T(addstr(leader, strlen(leader), &buf, &buflen)); 1015 1016 len = b64_ntop(rdata, key_len, base64_key, siz); 1017 if (len < 0) 1018 goto formerr; 1019 1020 T(addstr(base64_key, len, &buf, &buflen)); 1021 1022 rdata += key_len; 1023 while (rdata < edata) { 1024 T(addstr(leader, strlen(leader), &buf, &buflen)); 1025 T(addname(msg, msglen, &rdata, origin, 1026 &buf, &buflen)); 1027 } 1028 T(addstr(" )", 2, &buf, &buflen)); 1029 } 1030 break; 1031 } 1032 1033 default: 1034 comment = "unknown RR type"; 1035 goto hexify; 1036 } 1037 return (buf - obuf); 1038 formerr: 1039 comment = "RR format error"; 1040 hexify: { 1041 int n, m; 1042 char *p; 1043 1044 len = SPRINTF((tmp, "\\# %u%s\t; %s", (unsigned)(edata - rdata), 1045 rdlen != 0U ? " (" : "", comment)); 1046 T(addstr(tmp, len, &buf, &buflen)); 1047 while (rdata < edata) { 1048 p = tmp; 1049 p += SPRINTF((p, "\n\t")); 1050 spaced = 0; 1051 n = MIN(16, edata - rdata); 1052 for (m = 0; m < n; m++) 1053 p += SPRINTF((p, "%02x ", rdata[m])); 1054 T(addstr(tmp, p - tmp, &buf, &buflen)); 1055 if (n < 16) { 1056 T(addstr(")", 1, &buf, &buflen)); 1057 T(addtab(p - tmp + 1, 48, spaced, &buf, &buflen)); 1058 } 1059 p = tmp; 1060 p += SPRINTF((p, "; ")); 1061 for (m = 0; m < n; m++) 1062 *p++ = (isascii(rdata[m]) && isprint(rdata[m])) 1063 ? rdata[m] 1064 : '.'; 1065 T(addstr(tmp, p - tmp, &buf, &buflen)); 1066 rdata += n; 1067 } 1068 return (buf - obuf); 1069 } 1070 } 1071 1072 /* Private. */ 1073 1074 /*% 1075 * size_t 1076 * prune_origin(name, origin) 1077 * Find out if the name is at or under the current origin. 1078 * return: 1079 * Number of characters in name before start of origin, 1080 * or length of name if origin does not match. 1081 * notes: 1082 * This function should share code with samedomain(). 1083 */ 1084 static size_t 1085 prune_origin(const char *name, const char *origin) { 1086 const char *oname = name; 1087 1088 while (*name != '\0') { 1089 if (origin != NULL && ns_samename(name, origin) == 1) 1090 return (name - oname - (name > oname)); 1091 while (*name != '\0') { 1092 if (*name == '\\') { 1093 name++; 1094 /* XXX need to handle \nnn form. */ 1095 if (*name == '\0') 1096 break; 1097 } else if (*name == '.') { 1098 name++; 1099 break; 1100 } 1101 name++; 1102 } 1103 } 1104 return (name - oname); 1105 } 1106 1107 /*% 1108 * int 1109 * charstr(rdata, edata, buf, buflen) 1110 * Format a <character-string> into the presentation buffer. 1111 * return: 1112 * Number of rdata octets consumed 1113 * 0 for protocol format error 1114 * -1 for output buffer error 1115 * side effects: 1116 * buffer is advanced on success. 1117 */ 1118 static int 1119 charstr(const u_char *rdata, const u_char *edata, char **buf, size_t *buflen) { 1120 const u_char *odata = rdata; 1121 size_t save_buflen = *buflen; 1122 char *save_buf = *buf; 1123 1124 if (addstr("\"", 1, buf, buflen) < 0) 1125 goto enospc; 1126 if (rdata < edata) { 1127 int n = *rdata; 1128 1129 if (rdata + 1 + n <= edata) { 1130 rdata++; 1131 while (n-- > 0) { 1132 if (strchr("\n\"\\", *rdata) != NULL) 1133 if (addstr("\\", 1, buf, buflen) < 0) 1134 goto enospc; 1135 if (addstr((const char *)rdata, 1, 1136 buf, buflen) < 0) 1137 goto enospc; 1138 rdata++; 1139 } 1140 } 1141 } 1142 if (addstr("\"", 1, buf, buflen) < 0) 1143 goto enospc; 1144 return (rdata - odata); 1145 enospc: 1146 errno = ENOSPC; 1147 *buf = save_buf; 1148 *buflen = save_buflen; 1149 return (-1); 1150 } 1151 1152 static int 1153 addname(const u_char *msg, size_t msglen, 1154 const u_char **pp, const char *origin, 1155 char **buf, size_t *buflen) 1156 { 1157 size_t newlen, save_buflen = *buflen; 1158 char *save_buf = *buf; 1159 int n; 1160 1161 n = dn_expand(msg, msg + msglen, *pp, *buf, *buflen); 1162 if (n < 0) 1163 goto enospc; /*%< Guess. */ 1164 newlen = prune_origin(*buf, origin); 1165 if (**buf == '\0') { 1166 goto root; 1167 } else if (newlen == 0U) { 1168 /* Use "@" instead of name. */ 1169 if (newlen + 2 > *buflen) 1170 goto enospc; /* No room for "@\0". */ 1171 (*buf)[newlen++] = '@'; 1172 (*buf)[newlen] = '\0'; 1173 } else { 1174 if (((origin == NULL || origin[0] == '\0') || 1175 (origin[0] != '.' && origin[1] != '\0' && 1176 (*buf)[newlen] == '\0')) && (*buf)[newlen - 1] != '.') { 1177 /* No trailing dot. */ 1178 root: 1179 if (newlen + 2 > *buflen) 1180 goto enospc; /* No room for ".\0". */ 1181 (*buf)[newlen++] = '.'; 1182 (*buf)[newlen] = '\0'; 1183 } 1184 } 1185 *pp += n; 1186 addlen(newlen, buf, buflen); 1187 **buf = '\0'; 1188 return (newlen); 1189 enospc: 1190 errno = ENOSPC; 1191 *buf = save_buf; 1192 *buflen = save_buflen; 1193 return (-1); 1194 } 1195 1196 static void 1197 addlen(size_t len, char **buf, size_t *buflen) { 1198 INSIST(len <= *buflen); 1199 *buf += len; 1200 *buflen -= len; 1201 } 1202 1203 static int 1204 addstr(const char *src, size_t len, char **buf, size_t *buflen) { 1205 if (len >= *buflen) { 1206 errno = ENOSPC; 1207 return (-1); 1208 } 1209 memcpy(*buf, src, len); 1210 addlen(len, buf, buflen); 1211 **buf = '\0'; 1212 return (0); 1213 } 1214 1215 static int 1216 addtab(size_t len, size_t target, int spaced, char **buf, size_t *buflen) { 1217 size_t save_buflen = *buflen; 1218 char *save_buf = *buf; 1219 int t; 1220 1221 if (spaced || len >= target - 1) { 1222 T(addstr(" ", 2, buf, buflen)); 1223 spaced = 1; 1224 } else { 1225 for (t = (target - len - 1) / 8; t >= 0; t--) 1226 if (addstr("\t", 1, buf, buflen) < 0) { 1227 *buflen = save_buflen; 1228 *buf = save_buf; 1229 return (-1); 1230 } 1231 spaced = 0; 1232 } 1233 return (spaced); 1234 } 1235 1236 /*! \file */ 1237