1 /* $NetBSD: res_debug.c,v 1.13 2012/06/25 22:32:45 abs Exp $ */ 2 3 /* 4 * Portions Copyright (C) 2004, 2005, 2008, 2009 Internet Systems Consortium, Inc. ("ISC") 5 * Portions Copyright (C) 1996-2003 Internet Software Consortium. 6 * 7 * Permission to use, copy, modify, and/or distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 /* 21 * Copyright (c) 1985 22 * The Regents of the University of California. All rights reserved. 23 * 24 * Redistribution and use in source and binary forms, with or without 25 * modification, are permitted provided that the following conditions 26 * are met: 27 * 1. Redistributions of source code must retain the above copyright 28 * notice, this list of conditions and the following disclaimer. 29 * 2. Redistributions in binary form must reproduce the above copyright 30 * notice, this list of conditions and the following disclaimer in the 31 * documentation and/or other materials provided with the distribution. 32 * 3. All advertising materials mentioning features or use of this software 33 * must display the following acknowledgement: 34 * This product includes software developed by the University of 35 * California, Berkeley and its contributors. 36 * 4. Neither the name of the University nor the names of its contributors 37 * may be used to endorse or promote products derived from this software 38 * without specific prior written permission. 39 * 40 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 50 * SUCH DAMAGE. 51 */ 52 53 /* 54 * Portions Copyright (c) 1993 by Digital Equipment Corporation. 55 * 56 * Permission to use, copy, modify, and distribute this software for any 57 * purpose with or without fee is hereby granted, provided that the above 58 * copyright notice and this permission notice appear in all copies, and that 59 * the name of Digital Equipment Corporation not be used in advertising or 60 * publicity pertaining to distribution of the document or software without 61 * specific, written prior permission. 62 * 63 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 64 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 65 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 66 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 67 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 68 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 69 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 70 * SOFTWARE. 71 */ 72 73 /* 74 * Portions Copyright (c) 1995 by International Business Machines, Inc. 75 * 76 * International Business Machines, Inc. (hereinafter called IBM) grants 77 * permission under its copyrights to use, copy, modify, and distribute this 78 * Software with or without fee, provided that the above copyright notice and 79 * all paragraphs of this notice appear in all copies, and that the name of IBM 80 * not be used in connection with the marketing of any product incorporating 81 * the Software or modifications thereof, without specific, written prior 82 * permission. 83 * 84 * To the extent it has a right to do so, IBM grants an immunity from suit 85 * under its patents, if any, for the use, sale or manufacture of products to 86 * the extent that such products are used for performing Domain Name System 87 * dynamic updates in TCP/IP networks by means of the Software. No immunity is 88 * granted for any product per se or for any other function of any product. 89 * 90 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES, 91 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 92 * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, 93 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING 94 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN 95 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES. 96 */ 97 98 #include <sys/cdefs.h> 99 #if defined(LIBC_SCCS) && !defined(lint) 100 #ifdef notdef 101 static const char sccsid[] = "@(#)res_debug.c 8.1 (Berkeley) 6/4/93"; 102 static const char rcsid[] = "Id: res_debug.c,v 1.19 2009/02/26 11:20:20 tbox Exp"; 103 #else 104 __RCSID("$NetBSD: res_debug.c,v 1.13 2012/06/25 22:32:45 abs Exp $"); 105 #endif 106 #endif /* LIBC_SCCS and not lint */ 107 108 109 110 #include <sys/types.h> 111 #include <sys/param.h> 112 #include <sys/socket.h> 113 114 #include <netinet/in.h> 115 #include <arpa/inet.h> 116 #include <arpa/nameser.h> 117 118 #include <ctype.h> 119 #include <errno.h> 120 #include <math.h> 121 #include <netdb.h> 122 #include "resolv_private.h" 123 #include <stdio.h> 124 #include <stdlib.h> 125 #include <string.h> 126 #include <strings.h> 127 #include <time.h> 128 129 130 131 #ifdef SPRINTF_CHAR 132 # define SPRINTF(x) strlen(sprintf/**/x) 133 #else 134 # define SPRINTF(x) sprintf x 135 #endif 136 137 extern const char * const _res_opcodes[]; 138 extern const char * const _res_sectioncodes[]; 139 140 #ifndef _LIBC 141 /* 142 * Print the current options. 143 */ 144 void 145 fp_resstat(const res_state statp, FILE *file) { 146 u_long mask; 147 148 fprintf(file, ";; res options:"); 149 for (mask = 1; mask != 0U; mask <<= 1) 150 if (statp->options & mask) 151 fprintf(file, " %s", p_option(mask)); 152 putc('\n', file); 153 } 154 #endif 155 156 static void 157 do_section(const res_state statp, 158 ns_msg *handle, ns_sect section, 159 int pflag, FILE *file) 160 { 161 int n, sflag, rrnum; 162 int buflen = 2048; 163 char *buf; 164 ns_opcode opcode; 165 ns_rr rr; 166 167 /* 168 * Print answer records. 169 */ 170 sflag = (int)(statp->pfcode & pflag); 171 if (statp->pfcode && !sflag) 172 return; 173 174 buf = malloc((size_t)buflen); 175 if (buf == NULL) { 176 fprintf(file, ";; memory allocation failure\n"); 177 return; 178 } 179 180 opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode); 181 rrnum = 0; 182 for (;;) { 183 if (ns_parserr(handle, section, rrnum, &rr)) { 184 if (errno != ENODEV) 185 fprintf(file, ";; ns_parserr: %s\n", 186 strerror(errno)); 187 else if (rrnum > 0 && sflag != 0 && 188 (statp->pfcode & RES_PRF_HEAD1)) 189 putc('\n', file); 190 goto cleanup; 191 } 192 if (rrnum == 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1)) 193 fprintf(file, ";; %s SECTION:\n", 194 p_section(section, opcode)); 195 if (section == ns_s_qd) 196 fprintf(file, ";;\t%s, type = %s, class = %s\n", 197 ns_rr_name(rr), 198 p_type(ns_rr_type(rr)), 199 p_class(ns_rr_class(rr))); 200 else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) { 201 size_t rdatalen, ttl; 202 uint16_t optcode, optlen; 203 204 rdatalen = ns_rr_rdlen(rr); 205 ttl = ns_rr_ttl(rr); 206 fprintf(file, 207 "; EDNS: version: %zu, udp=%u, flags=%04zx\n", 208 (ttl>>16)&0xff, ns_rr_class(rr), ttl&0xffff); 209 while (rdatalen >= 4) { 210 const u_char *cp = ns_rr_rdata(rr); 211 int i; 212 213 GETSHORT(optcode, cp); 214 GETSHORT(optlen, cp); 215 216 if (optcode == NS_OPT_NSID) { 217 fputs("; NSID: ", file); 218 if (optlen == 0) { 219 fputs("; NSID\n", file); 220 } else { 221 fputs("; NSID: ", file); 222 for (i = 0; i < optlen; i++) 223 fprintf(file, "%02x ", 224 cp[i]); 225 fputs(" (",file); 226 for (i = 0; i < optlen; i++) 227 fprintf(file, "%c", 228 isprint(cp[i])? 229 cp[i] : '.'); 230 fputs(")\n", file); 231 } 232 } else { 233 if (optlen == 0) { 234 fprintf(file, "; OPT=%u\n", 235 optcode); 236 } else { 237 fprintf(file, "; OPT=%u: ", 238 optcode); 239 for (i = 0; i < optlen; i++) 240 fprintf(file, "%02x ", 241 cp[i]); 242 fputs(" (",file); 243 for (i = 0; i < optlen; i++) 244 fprintf(file, "%c", 245 isprint(cp[i]) ? 246 cp[i] : '.'); 247 fputs(")\n", file); 248 } 249 } 250 rdatalen -= 4 + optlen; 251 } 252 } else { 253 n = ns_sprintrr(handle, &rr, NULL, NULL, 254 buf, (u_int)buflen); 255 if (n < 0) { 256 if (errno == ENOSPC) { 257 free(buf); 258 buf = NULL; 259 if (buflen < 131072) 260 buf = malloc((size_t)(buflen += 1024)); 261 if (buf == NULL) { 262 fprintf(file, 263 ";; memory allocation failure\n"); 264 return; 265 } 266 continue; 267 } 268 fprintf(file, ";; ns_sprintrr: %s\n", 269 strerror(errno)); 270 goto cleanup; 271 } 272 fputs(buf, file); 273 fputc('\n', file); 274 } 275 rrnum++; 276 } 277 cleanup: 278 if (buf != NULL) 279 free(buf); 280 } 281 282 /* 283 * Print the contents of a query. 284 * This is intended to be primarily a debugging routine. 285 */ 286 void 287 res_pquery(const res_state statp, const u_char *msg, int len, FILE *file) { 288 ns_msg handle; 289 int qdcount, ancount, nscount, arcount; 290 u_int opcode, rcode, id; 291 292 if (ns_initparse(msg, len, &handle) < 0) { 293 fprintf(file, ";; ns_initparse: %s\n", strerror(errno)); 294 return; 295 } 296 opcode = ns_msg_getflag(handle, ns_f_opcode); 297 rcode = ns_msg_getflag(handle, ns_f_rcode); 298 id = ns_msg_id(handle); 299 qdcount = ns_msg_count(handle, ns_s_qd); 300 ancount = ns_msg_count(handle, ns_s_an); 301 nscount = ns_msg_count(handle, ns_s_ns); 302 arcount = ns_msg_count(handle, ns_s_ar); 303 304 /* 305 * Print header fields. 306 */ 307 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX) || rcode) 308 fprintf(file, 309 ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n", 310 _res_opcodes[opcode], p_rcode((int)rcode), id); 311 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX)) 312 putc(';', file); 313 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD2)) { 314 fprintf(file, "; flags:"); 315 if (ns_msg_getflag(handle, ns_f_qr)) 316 fprintf(file, " qr"); 317 if (ns_msg_getflag(handle, ns_f_aa)) 318 fprintf(file, " aa"); 319 if (ns_msg_getflag(handle, ns_f_tc)) 320 fprintf(file, " tc"); 321 if (ns_msg_getflag(handle, ns_f_rd)) 322 fprintf(file, " rd"); 323 if (ns_msg_getflag(handle, ns_f_ra)) 324 fprintf(file, " ra"); 325 if (ns_msg_getflag(handle, ns_f_z)) 326 fprintf(file, " ??"); 327 if (ns_msg_getflag(handle, ns_f_ad)) 328 fprintf(file, " ad"); 329 if (ns_msg_getflag(handle, ns_f_cd)) 330 fprintf(file, " cd"); 331 } 332 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD1)) { 333 fprintf(file, "; %s: %d", 334 p_section(ns_s_qd, (int)opcode), qdcount); 335 fprintf(file, ", %s: %d", 336 p_section(ns_s_an, (int)opcode), ancount); 337 fprintf(file, ", %s: %d", 338 p_section(ns_s_ns, (int)opcode), nscount); 339 fprintf(file, ", %s: %d", 340 p_section(ns_s_ar, (int)opcode), arcount); 341 } 342 if ((!statp->pfcode) || (statp->pfcode & 343 (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) { 344 putc('\n',file); 345 } 346 /* 347 * Print the various sections. 348 */ 349 do_section(statp, &handle, ns_s_qd, RES_PRF_QUES, file); 350 do_section(statp, &handle, ns_s_an, RES_PRF_ANS, file); 351 do_section(statp, &handle, ns_s_ns, RES_PRF_AUTH, file); 352 do_section(statp, &handle, ns_s_ar, RES_PRF_ADD, file); 353 if (qdcount == 0 && ancount == 0 && 354 nscount == 0 && arcount == 0) 355 putc('\n', file); 356 } 357 358 const u_char * 359 p_cdnname(const u_char *cp, const u_char *msg, int len, FILE *file) { 360 char name[MAXDNAME]; 361 int n; 362 363 if ((n = dn_expand(msg, msg + len, cp, name, (int)sizeof name)) < 0) 364 return (NULL); 365 if (name[0] == '\0') 366 putc('.', file); 367 else 368 fputs(name, file); 369 return (cp + n); 370 } 371 372 const u_char * 373 p_cdname(const u_char *cp, const u_char *msg, FILE *file) { 374 return (p_cdnname(cp, msg, PACKETSZ, file)); 375 } 376 377 /* Return a fully-qualified domain name from a compressed name (with 378 length supplied). */ 379 380 const u_char * 381 p_fqnname(const u_char *cp, const u_char *msg, int msglen, char *name, 382 int namelen) 383 { 384 int n; 385 size_t newlen; 386 387 if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0) 388 return (NULL); 389 newlen = strlen(name); 390 if (newlen == 0 || name[newlen - 1] != '.') { 391 if ((int)newlen + 1 >= namelen) /* Lack space for final dot */ 392 return (NULL); 393 else 394 strcpy(name + newlen, "."); 395 } 396 return (cp + n); 397 } 398 399 /* XXX: the rest of these functions need to become length-limited, too. */ 400 401 const u_char * 402 p_fqname(const u_char *cp, const u_char *msg, FILE *file) { 403 char name[MAXDNAME]; 404 const u_char *n; 405 406 n = p_fqnname(cp, msg, MAXCDNAME, name, (int)sizeof name); 407 if (n == NULL) 408 return (NULL); 409 fputs(name, file); 410 return (n); 411 } 412 413 /* 414 * Names of RR classes and qclasses. Classes and qclasses are the same, except 415 * that C_ANY is a qclass but not a class. (You can ask for records of class 416 * C_ANY, but you can't have any records of that class in the database.) 417 */ 418 const struct res_sym __p_class_syms[] = { 419 {C_IN, "IN", (char *)0}, 420 {C_CHAOS, "CH", (char *)0}, 421 {C_CHAOS, "CHAOS", (char *)0}, 422 {C_HS, "HS", (char *)0}, 423 {C_HS, "HESIOD", (char *)0}, 424 {C_ANY, "ANY", (char *)0}, 425 {C_NONE, "NONE", (char *)0}, 426 {C_IN, (char *)0, (char *)0} 427 }; 428 429 /* 430 * Names of message sections. 431 */ 432 static const struct res_sym __p_default_section_syms[] = { 433 {ns_s_qd, "QUERY", (char *)0}, 434 {ns_s_an, "ANSWER", (char *)0}, 435 {ns_s_ns, "AUTHORITY", (char *)0}, 436 {ns_s_ar, "ADDITIONAL", (char *)0}, 437 {0, (char *)0, (char *)0} 438 }; 439 440 static const struct res_sym __p_update_section_syms[] = { 441 {S_ZONE, "ZONE", (char *)0}, 442 {S_PREREQ, "PREREQUISITE", (char *)0}, 443 {S_UPDATE, "UPDATE", (char *)0}, 444 {S_ADDT, "ADDITIONAL", (char *)0}, 445 {0, (char *)0, (char *)0} 446 }; 447 448 const struct res_sym __p_key_syms[] = { 449 {NS_ALG_MD5RSA, "RSA", "RSA KEY with MD5 hash"}, 450 {NS_ALG_DH, "DH", "Diffie Hellman"}, 451 {NS_ALG_DSA, "DSA", "Digital Signature Algorithm"}, 452 {NS_ALG_EXPIRE_ONLY, "EXPIREONLY", "No algorithm"}, 453 {NS_ALG_PRIVATE_OID, "PRIVATE", "Algorithm obtained from OID"}, 454 {0, NULL, NULL} 455 }; 456 457 const struct res_sym __p_cert_syms[] = { 458 {cert_t_pkix, "PKIX", "PKIX (X.509v3) Certificate"}, 459 {cert_t_spki, "SPKI", "SPKI certificate"}, 460 {cert_t_pgp, "PGP", "PGP certificate"}, 461 {cert_t_url, "URL", "URL Private"}, 462 {cert_t_oid, "OID", "OID Private"}, 463 {0, NULL, NULL} 464 }; 465 466 /* 467 * Names of RR types and qtypes. Types and qtypes are the same, except 468 * that T_ANY is a qtype but not a type. (You can ask for records of type 469 * T_ANY, but you can't have any records of that type in the database.) 470 */ 471 const struct res_sym __p_type_syms[] = { 472 {ns_t_a, "A", "address"}, 473 {ns_t_ns, "NS", "name server"}, 474 {ns_t_md, "MD", "mail destination (deprecated)"}, 475 {ns_t_mf, "MF", "mail forwarder (deprecated)"}, 476 {ns_t_cname, "CNAME", "canonical name"}, 477 {ns_t_soa, "SOA", "start of authority"}, 478 {ns_t_mb, "MB", "mailbox"}, 479 {ns_t_mg, "MG", "mail group member"}, 480 {ns_t_mr, "MR", "mail rename"}, 481 {ns_t_null, "NULL", "null"}, 482 {ns_t_wks, "WKS", "well-known service (deprecated)"}, 483 {ns_t_ptr, "PTR", "domain name pointer"}, 484 {ns_t_hinfo, "HINFO", "host information"}, 485 {ns_t_minfo, "MINFO", "mailbox information"}, 486 {ns_t_mx, "MX", "mail exchanger"}, 487 {ns_t_txt, "TXT", "text"}, 488 {ns_t_rp, "RP", "responsible person"}, 489 {ns_t_afsdb, "AFSDB", "DCE or AFS server"}, 490 {ns_t_x25, "X25", "X25 address"}, 491 {ns_t_isdn, "ISDN", "ISDN address"}, 492 {ns_t_rt, "RT", "router"}, 493 {ns_t_nsap, "NSAP", "nsap address"}, 494 {ns_t_nsap_ptr, "NSAP_PTR", "domain name pointer"}, 495 {ns_t_sig, "SIG", "signature"}, 496 {ns_t_key, "KEY", "key"}, 497 {ns_t_px, "PX", "mapping information"}, 498 {ns_t_gpos, "GPOS", "geographical position (withdrawn)"}, 499 {ns_t_aaaa, "AAAA", "IPv6 address"}, 500 {ns_t_loc, "LOC", "location"}, 501 {ns_t_nxt, "NXT", "next valid name (unimplemented)"}, 502 {ns_t_eid, "EID", "endpoint identifier (unimplemented)"}, 503 {ns_t_nimloc, "NIMLOC", "NIMROD locator (unimplemented)"}, 504 {ns_t_srv, "SRV", "server selection"}, 505 {ns_t_atma, "ATMA", "ATM address (unimplemented)"}, 506 {ns_t_naptr, "NAPTR", "naptr"}, 507 {ns_t_kx, "KX", "key exchange"}, 508 {ns_t_cert, "CERT", "certificate"}, 509 {ns_t_a6, "A", "IPv6 address (experminental)"}, 510 {ns_t_dname, "DNAME", "non-terminal redirection"}, 511 {ns_t_opt, "OPT", "opt"}, 512 {ns_t_apl, "apl", "apl"}, 513 {ns_t_ds, "DS", "delegation signer"}, 514 {ns_t_sshfp, "SSFP", "SSH fingerprint"}, 515 {ns_t_ipseckey, "IPSECKEY", "IPSEC key"}, 516 {ns_t_rrsig, "RRSIG", "rrsig"}, 517 {ns_t_nsec, "NSEC", "nsec"}, 518 {ns_t_dnskey, "DNSKEY", "DNS key"}, 519 {ns_t_dhcid, "DHCID", "dynamic host configuration identifier"}, 520 {ns_t_nsec3, "NSEC3", "nsec3"}, 521 {ns_t_nsec3param, "NSEC3PARAM", "NSEC3 parameters"}, 522 {ns_t_hip, "HIP", "host identity protocol"}, 523 {ns_t_spf, "SPF", "sender policy framework"}, 524 {ns_t_tkey, "TKEY", "tkey"}, 525 {ns_t_tsig, "TSIG", "transaction signature"}, 526 {ns_t_ixfr, "IXFR", "incremental zone transfer"}, 527 {ns_t_axfr, "AXFR", "zone transfer"}, 528 {ns_t_zxfr, "ZXFR", "compressed zone transfer"}, 529 {ns_t_mailb, "MAILB", "mailbox-related data (deprecated)"}, 530 {ns_t_maila, "MAILA", "mail agent (deprecated)"}, 531 {ns_t_naptr, "NAPTR", "URN Naming Authority"}, 532 {ns_t_kx, "KX", "Key Exchange"}, 533 {ns_t_cert, "CERT", "Certificate"}, 534 {ns_t_a6, "A6", "IPv6 Address"}, 535 {ns_t_dname, "DNAME", "dname"}, 536 {ns_t_sink, "SINK", "Kitchen Sink (experimental)"}, 537 {ns_t_opt, "OPT", "EDNS Options"}, 538 {ns_t_any, "ANY", "\"any\""}, 539 {ns_t_dlv, "DLV", "DNSSEC look-aside validation"}, 540 {0, NULL, NULL} 541 }; 542 543 /* 544 * Names of DNS rcodes. 545 */ 546 const struct res_sym __p_rcode_syms[] = { 547 {ns_r_noerror, "NOERROR", "no error"}, 548 {ns_r_formerr, "FORMERR", "format error"}, 549 {ns_r_servfail, "SERVFAIL", "server failed"}, 550 {ns_r_nxdomain, "NXDOMAIN", "no such domain name"}, 551 {ns_r_notimpl, "NOTIMP", "not implemented"}, 552 {ns_r_refused, "REFUSED", "refused"}, 553 {ns_r_yxdomain, "YXDOMAIN", "domain name exists"}, 554 {ns_r_yxrrset, "YXRRSET", "rrset exists"}, 555 {ns_r_nxrrset, "NXRRSET", "rrset doesn't exist"}, 556 {ns_r_notauth, "NOTAUTH", "not authoritative"}, 557 {ns_r_notzone, "NOTZONE", "Not in zone"}, 558 {ns_r_max, "", ""}, 559 {ns_r_badsig, "BADSIG", "bad signature"}, 560 {ns_r_badkey, "BADKEY", "bad key"}, 561 {ns_r_badtime, "BADTIME", "bad time"}, 562 {0, NULL, NULL} 563 }; 564 565 int 566 sym_ston(const struct res_sym *syms, const char *name, int *success) { 567 for (; syms->name != 0; syms++) { 568 if (strcasecmp (name, syms->name) == 0) { 569 if (success) 570 *success = 1; 571 return (syms->number); 572 } 573 } 574 if (success) 575 *success = 0; 576 return (syms->number); /* The default value. */ 577 } 578 579 const char * 580 sym_ntos(const struct res_sym *syms, int number, int *success) { 581 static char unname[20]; 582 583 for (; syms->name != 0; syms++) { 584 if (number == syms->number) { 585 if (success) 586 *success = 1; 587 return (syms->name); 588 } 589 } 590 591 sprintf(unname, "%d", number); /* XXX nonreentrant */ 592 if (success) 593 *success = 0; 594 return (unname); 595 } 596 597 const char * 598 sym_ntop(const struct res_sym *syms, int number, int *success) { 599 static char unname[20]; 600 601 for (; syms->name != 0; syms++) { 602 if (number == syms->number) { 603 if (success) 604 *success = 1; 605 return (syms->humanname); 606 } 607 } 608 sprintf(unname, "%d", number); /* XXX nonreentrant */ 609 if (success) 610 *success = 0; 611 return (unname); 612 } 613 614 /* 615 * Return a string for the type. 616 */ 617 const char * 618 p_type(int type) { 619 int success; 620 const char *result; 621 static char typebuf[20]; 622 623 result = sym_ntos(__p_type_syms, type, &success); 624 if (success) 625 return (result); 626 if (type < 0 || type > 0xffff) 627 return ("BADTYPE"); 628 sprintf(typebuf, "TYPE%d", type); 629 return (typebuf); 630 } 631 632 /* 633 * Return a string for the type. 634 */ 635 const char * 636 p_section(int section, int opcode) { 637 const struct res_sym *symbols; 638 639 switch (opcode) { 640 case ns_o_update: 641 symbols = __p_update_section_syms; 642 break; 643 default: 644 symbols = __p_default_section_syms; 645 break; 646 } 647 return (sym_ntos(symbols, section, (int *)0)); 648 } 649 650 /* 651 * Return a mnemonic for class. 652 */ 653 const char * 654 p_class(int class) { 655 int success; 656 const char *result; 657 static char classbuf[20]; 658 659 result = sym_ntos(__p_class_syms, class, &success); 660 if (success) 661 return (result); 662 if (class < 0 || class > 0xffff) 663 return ("BADCLASS"); 664 sprintf(classbuf, "CLASS%d", class); 665 return (classbuf); 666 } 667 668 /* 669 * Return a mnemonic for an option 670 */ 671 const char * 672 p_option(u_long option) { 673 static char nbuf[40]; 674 675 switch (option) { 676 case RES_INIT: return "init"; 677 case RES_DEBUG: return "debug"; 678 case RES_AAONLY: return "aaonly(unimpl)"; 679 case RES_USEVC: return "usevc"; 680 case RES_PRIMARY: return "primry(unimpl)"; 681 case RES_IGNTC: return "igntc"; 682 case RES_RECURSE: return "recurs"; 683 case RES_DEFNAMES: return "defnam"; 684 case RES_STAYOPEN: return "styopn"; 685 case RES_DNSRCH: return "dnsrch"; 686 case RES_INSECURE1: return "insecure1"; 687 case RES_INSECURE2: return "insecure2"; 688 case RES_NOALIASES: return "noaliases"; 689 case RES_USE_INET6: return "inet6"; 690 #ifdef RES_USE_EDNS0 /* KAME extension */ 691 case RES_USE_EDNS0: return "edns0"; 692 #endif 693 #ifdef RES_USE_DNAME 694 case RES_USE_DNAME: return "dname"; 695 #endif 696 #ifdef RES_USE_DNSSEC 697 case RES_USE_DNSSEC: return "dnssec"; 698 #endif 699 #ifdef RES_NOTLDQUERY 700 case RES_NOTLDQUERY: return "no-tld-query"; 701 #endif 702 #ifdef RES_NO_NIBBLE2 703 case RES_NO_NIBBLE2: return "no-nibble2"; 704 #endif 705 /* XXX nonreentrant */ 706 default: sprintf(nbuf, "?0x%lx?", (u_long)option); 707 return (nbuf); 708 } 709 } 710 711 /* 712 * Return a mnemonic for a time to live. 713 */ 714 const char * 715 p_time(u_int32_t value) { 716 static char nbuf[40]; /* XXX nonreentrant */ 717 718 if (ns_format_ttl((u_long)value, nbuf, sizeof nbuf) < 0) 719 sprintf(nbuf, "%u", value); 720 return (nbuf); 721 } 722 723 /* 724 * Return a string for the rcode. 725 */ 726 const char * 727 p_rcode(int rcode) { 728 return (sym_ntos(__p_rcode_syms, rcode, (int *)0)); 729 } 730 731 /* 732 * Return a string for a res_sockaddr_union. 733 */ 734 const char * 735 p_sockun(union res_sockaddr_union u, char *buf, size_t size) { 736 char ret[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:123.123.123.123"]; 737 738 switch (u.sin.sin_family) { 739 case AF_INET: 740 inet_ntop(AF_INET, &u.sin.sin_addr, ret, (socklen_t)sizeof ret); 741 break; 742 #ifdef HAS_INET6_STRUCTS 743 case AF_INET6: 744 inet_ntop(AF_INET6, &u.sin6.sin6_addr, ret, sizeof ret); 745 break; 746 #endif 747 default: 748 sprintf(ret, "[af%d]", u.sin.sin_family); 749 break; 750 } 751 if (size > 0U) { 752 strncpy(buf, ret, size - 1); 753 buf[size - 1] = '0'; 754 } 755 return (buf); 756 } 757 758 /* 759 * routines to convert between on-the-wire RR format and zone file format. 760 * Does not contain conversion to/from decimal degrees; divide or multiply 761 * by 60*60*1000 for that. 762 */ 763 764 static const unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000, 765 1000000,10000000,100000000,1000000000}; 766 767 /* takes an XeY precision/size value, returns a string representation. */ 768 static const char * 769 precsize_ntoa(u_int32_t prec) 770 { 771 static char retbuf[sizeof "90000000.00"]; /* XXX nonreentrant */ 772 unsigned long val; 773 int mantissa, exponent; 774 775 mantissa = (int)((prec >> 4) & 0x0f) % 10; 776 exponent = (int)((prec >> 0) & 0x0f) % 10; 777 778 val = mantissa * poweroften[exponent]; 779 780 (void) sprintf(retbuf, "%lu.%.2lu", val/100, val%100); 781 return (retbuf); 782 } 783 784 /* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */ 785 static u_int8_t 786 precsize_aton(const char **strptr) { 787 unsigned int mval = 0, cmval = 0; 788 u_int8_t retval = 0; 789 const char *cp; 790 int exponent; 791 int mantissa; 792 793 cp = *strptr; 794 795 while (isdigit((unsigned char)*cp)) 796 mval = mval * 10 + (*cp++ - '0'); 797 798 if (*cp == '.') { /* centimeters */ 799 cp++; 800 if (isdigit((unsigned char)*cp)) { 801 cmval = (*cp++ - '0') * 10; 802 if (isdigit((unsigned char)*cp)) { 803 cmval += (*cp++ - '0'); 804 } 805 } 806 } 807 cmval = (mval * 100) + cmval; 808 809 for (exponent = 0; exponent < 9; exponent++) 810 if (cmval < poweroften[exponent+1]) 811 break; 812 813 mantissa = cmval / poweroften[exponent]; 814 if (mantissa > 9) 815 mantissa = 9; 816 817 retval = (mantissa << 4) | exponent; 818 819 *strptr = cp; 820 821 return (retval); 822 } 823 824 /* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */ 825 static u_int32_t 826 latlon2ul(const char **latlonstrptr, int *which) { 827 const char *cp; 828 u_int32_t retval; 829 int deg = 0, min = 0, secs = 0, secsfrac = 0; 830 831 cp = *latlonstrptr; 832 833 while (isdigit((unsigned char)*cp)) 834 deg = deg * 10 + (*cp++ - '0'); 835 836 while (isspace((unsigned char)*cp)) 837 cp++; 838 839 if (!(isdigit((unsigned char)*cp))) 840 goto fndhemi; 841 842 while (isdigit((unsigned char)*cp)) 843 min = min * 10 + (*cp++ - '0'); 844 845 while (isspace((unsigned char)*cp)) 846 cp++; 847 848 if (!(isdigit((unsigned char)*cp))) 849 goto fndhemi; 850 851 while (isdigit((unsigned char)*cp)) 852 secs = secs * 10 + (*cp++ - '0'); 853 854 if (*cp == '.') { /* decimal seconds */ 855 cp++; 856 if (isdigit((unsigned char)*cp)) { 857 secsfrac = (*cp++ - '0') * 100; 858 if (isdigit((unsigned char)*cp)) { 859 secsfrac += (*cp++ - '0') * 10; 860 if (isdigit((unsigned char)*cp)) { 861 secsfrac += (*cp++ - '0'); 862 } 863 } 864 } 865 } 866 867 while (!isspace((unsigned char)*cp)) /* if any trailing garbage */ 868 cp++; 869 870 while (isspace((unsigned char)*cp)) 871 cp++; 872 873 fndhemi: 874 switch (*cp) { 875 case 'N': case 'n': 876 case 'E': case 'e': 877 retval = ((unsigned)1<<31) 878 + (((((deg * 60) + min) * 60) + secs) * 1000) 879 + secsfrac; 880 break; 881 case 'S': case 's': 882 case 'W': case 'w': 883 retval = ((unsigned)1<<31) 884 - (((((deg * 60) + min) * 60) + secs) * 1000) 885 - secsfrac; 886 break; 887 default: 888 retval = 0; /* invalid value -- indicates error */ 889 break; 890 } 891 892 switch (*cp) { 893 case 'N': case 'n': 894 case 'S': case 's': 895 *which = 1; /* latitude */ 896 break; 897 case 'E': case 'e': 898 case 'W': case 'w': 899 *which = 2; /* longitude */ 900 break; 901 default: 902 *which = 0; /* error */ 903 break; 904 } 905 906 cp++; /* skip the hemisphere */ 907 908 while (!isspace((unsigned char)*cp)) /* if any trailing garbage */ 909 cp++; 910 911 while (isspace((unsigned char)*cp)) /* move to next field */ 912 cp++; 913 914 *latlonstrptr = cp; 915 916 return (retval); 917 } 918 919 /* converts a zone file representation in a string to an RDATA on-the-wire 920 * representation. */ 921 int 922 loc_aton(const char *ascii, u_char *binary) 923 { 924 const char *cp, *maxcp; 925 u_char *bcp; 926 927 u_int32_t latit = 0, longit = 0, alt = 0; 928 u_int32_t lltemp1 = 0, lltemp2 = 0; 929 int altmeters = 0, altfrac = 0, altsign = 1; 930 u_int8_t hp = 0x16; /* default = 1e6 cm = 10000.00m = 10km */ 931 u_int8_t vp = 0x13; /* default = 1e3 cm = 10.00m */ 932 u_int8_t siz = 0x12; /* default = 1e2 cm = 1.00m */ 933 int which1 = 0, which2 = 0; 934 935 cp = ascii; 936 maxcp = cp + strlen(ascii); 937 938 lltemp1 = latlon2ul(&cp, &which1); 939 940 lltemp2 = latlon2ul(&cp, &which2); 941 942 switch (which1 + which2) { 943 case 3: /* 1 + 2, the only valid combination */ 944 if ((which1 == 1) && (which2 == 2)) { /* normal case */ 945 latit = lltemp1; 946 longit = lltemp2; 947 } else if ((which1 == 2) && (which2 == 1)) { /* reversed */ 948 longit = lltemp1; 949 latit = lltemp2; 950 } else { /* some kind of brokenness */ 951 return (0); 952 } 953 break; 954 default: /* we didn't get one of each */ 955 return (0); 956 } 957 958 /* altitude */ 959 if (*cp == '-') { 960 altsign = -1; 961 cp++; 962 } 963 964 if (*cp == '+') 965 cp++; 966 967 while (isdigit((unsigned char)*cp)) 968 altmeters = altmeters * 10 + (*cp++ - '0'); 969 970 if (*cp == '.') { /* decimal meters */ 971 cp++; 972 if (isdigit((unsigned char)*cp)) { 973 altfrac = (*cp++ - '0') * 10; 974 if (isdigit((unsigned char)*cp)) { 975 altfrac += (*cp++ - '0'); 976 } 977 } 978 } 979 980 alt = (10000000 + (altsign * (altmeters * 100 + altfrac))); 981 982 while (!isspace((unsigned char)*cp) && (cp < maxcp)) /* if trailing garbage or m */ 983 cp++; 984 985 while (isspace((unsigned char)*cp) && (cp < maxcp)) 986 cp++; 987 988 if (cp >= maxcp) 989 goto defaults; 990 991 siz = precsize_aton(&cp); 992 993 while (!isspace((unsigned char)*cp) && (cp < maxcp)) /* if trailing garbage or m */ 994 cp++; 995 996 while (isspace((unsigned char)*cp) && (cp < maxcp)) 997 cp++; 998 999 if (cp >= maxcp) 1000 goto defaults; 1001 1002 hp = precsize_aton(&cp); 1003 1004 while (!isspace((unsigned char)*cp) && (cp < maxcp)) /* if trailing garbage or m */ 1005 cp++; 1006 1007 while (isspace((unsigned char)*cp) && (cp < maxcp)) 1008 cp++; 1009 1010 if (cp >= maxcp) 1011 goto defaults; 1012 1013 vp = precsize_aton(&cp); 1014 1015 defaults: 1016 1017 bcp = binary; 1018 *bcp++ = (u_int8_t) 0; /* version byte */ 1019 *bcp++ = siz; 1020 *bcp++ = hp; 1021 *bcp++ = vp; 1022 PUTLONG(latit,bcp); 1023 PUTLONG(longit,bcp); 1024 PUTLONG(alt,bcp); 1025 1026 return (16); /* size of RR in octets */ 1027 } 1028 1029 /* takes an on-the-wire LOC RR and formats it in a human readable format. */ 1030 const char * 1031 loc_ntoa(const u_char *binary, char *ascii) 1032 { 1033 static const char *error = "?"; 1034 static char tmpbuf[sizeof 1035 "1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"]; 1036 const u_char *cp = binary; 1037 1038 int latdeg, latmin, latsec, latsecfrac; 1039 int longdeg, longmin, longsec, longsecfrac; 1040 char northsouth, eastwest; 1041 const char *altsign; 1042 int altmeters, altfrac; 1043 1044 const u_int32_t referencealt = 100000 * 100; 1045 1046 int32_t latval, longval, altval; 1047 u_int32_t templ; 1048 u_int8_t sizeval, hpval, vpval, versionval; 1049 1050 char *sizestr, *hpstr, *vpstr; 1051 1052 versionval = *cp++; 1053 1054 if (ascii == NULL) 1055 ascii = tmpbuf; 1056 1057 if (versionval) { 1058 (void) sprintf(ascii, "; error: unknown LOC RR version"); 1059 return (ascii); 1060 } 1061 1062 sizeval = *cp++; 1063 1064 hpval = *cp++; 1065 vpval = *cp++; 1066 1067 GETLONG(templ, cp); 1068 latval = (templ - ((unsigned)1<<31)); 1069 1070 GETLONG(templ, cp); 1071 longval = (templ - ((unsigned)1<<31)); 1072 1073 GETLONG(templ, cp); 1074 if (templ < referencealt) { /* below WGS 84 spheroid */ 1075 altval = referencealt - templ; 1076 altsign = "-"; 1077 } else { 1078 altval = templ - referencealt; 1079 altsign = ""; 1080 } 1081 1082 if (latval < 0) { 1083 northsouth = 'S'; 1084 latval = -latval; 1085 } else 1086 northsouth = 'N'; 1087 1088 latsecfrac = latval % 1000; 1089 latval = latval / 1000; 1090 latsec = latval % 60; 1091 latval = latval / 60; 1092 latmin = latval % 60; 1093 latval = latval / 60; 1094 latdeg = latval; 1095 1096 if (longval < 0) { 1097 eastwest = 'W'; 1098 longval = -longval; 1099 } else 1100 eastwest = 'E'; 1101 1102 longsecfrac = longval % 1000; 1103 longval = longval / 1000; 1104 longsec = longval % 60; 1105 longval = longval / 60; 1106 longmin = longval % 60; 1107 longval = longval / 60; 1108 longdeg = longval; 1109 1110 altfrac = altval % 100; 1111 altmeters = (altval / 100); 1112 1113 sizestr = strdup(precsize_ntoa((u_int32_t)sizeval)); 1114 hpstr = strdup(precsize_ntoa((u_int32_t)hpval)); 1115 vpstr = strdup(precsize_ntoa((u_int32_t)vpval)); 1116 1117 sprintf(ascii, 1118 "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %s%d.%.2dm %sm %sm %sm", 1119 latdeg, latmin, latsec, latsecfrac, northsouth, 1120 longdeg, longmin, longsec, longsecfrac, eastwest, 1121 altsign, altmeters, altfrac, 1122 (sizestr != NULL) ? sizestr : error, 1123 (hpstr != NULL) ? hpstr : error, 1124 (vpstr != NULL) ? vpstr : error); 1125 1126 if (sizestr != NULL) 1127 free(sizestr); 1128 if (hpstr != NULL) 1129 free(hpstr); 1130 if (vpstr != NULL) 1131 free(vpstr); 1132 1133 return (ascii); 1134 } 1135 1136 1137 /* Return the number of DNS hierarchy levels in the name. */ 1138 int 1139 dn_count_labels(const char *name) { 1140 size_t len, i, count; 1141 1142 len = strlen(name); 1143 for (i = 0, count = 0; i < len; i++) { 1144 /* XXX need to check for \. or use named's nlabels(). */ 1145 if (name[i] == '.') 1146 count++; 1147 } 1148 1149 /* don't count initial wildcard */ 1150 if (name[0] == '*') 1151 if (count) 1152 count--; 1153 1154 /* don't count the null label for root. */ 1155 /* if terminating '.' not found, must adjust */ 1156 /* count to include last label */ 1157 if (len > 0 && name[len-1] != '.') 1158 count++; 1159 _DIAGASSERT(__type_fit(int, count)); 1160 return (int)count; 1161 } 1162 1163 1164 /* 1165 * Make dates expressed in seconds-since-Jan-1-1970 easy to read. 1166 * SIG records are required to be printed like this, by the Secure DNS RFC. 1167 */ 1168 char * 1169 p_secstodate (u_long secs) { 1170 /* XXX nonreentrant */ 1171 static char output[15]; /* YYYYMMDDHHMMSS and null */ 1172 time_t myclock = secs; 1173 struct tm *mytime; 1174 #ifdef HAVE_TIME_R 1175 struct tm res; 1176 1177 mytime = gmtime_r(&myclock, &res); 1178 #else 1179 mytime = gmtime(&myclock); 1180 #endif 1181 mytime->tm_year += 1900; 1182 mytime->tm_mon += 1; 1183 sprintf(output, "%04d%02d%02d%02d%02d%02d", 1184 mytime->tm_year, mytime->tm_mon, mytime->tm_mday, 1185 mytime->tm_hour, mytime->tm_min, mytime->tm_sec); 1186 return (output); 1187 } 1188 1189 u_int16_t 1190 res_nametoclass(const char *buf, int *successp) { 1191 unsigned long result; 1192 char *endptr; 1193 int success; 1194 1195 result = sym_ston(__p_class_syms, buf, &success); 1196 if (success) 1197 goto done; 1198 1199 if (strncasecmp(buf, "CLASS", 5) != 0 || 1200 !isdigit((unsigned char)buf[5])) 1201 goto done; 1202 errno = 0; 1203 result = strtoul(buf + 5, &endptr, 10); 1204 if (errno == 0 && *endptr == '\0' && result <= 0xffffU) 1205 success = 1; 1206 done: 1207 if (successp) 1208 *successp = success; 1209 return (u_int16_t)(result); 1210 } 1211 1212 u_int16_t 1213 res_nametotype(const char *buf, int *successp) { 1214 unsigned long result; 1215 char *endptr; 1216 int success; 1217 1218 result = sym_ston(__p_type_syms, buf, &success); 1219 if (success) 1220 goto done; 1221 1222 if (strncasecmp(buf, "type", 4) != 0 || 1223 !isdigit((unsigned char)buf[4])) 1224 goto done; 1225 errno = 0; 1226 result = strtoul(buf + 4, &endptr, 10); 1227 if (errno == 0 && *endptr == '\0' && result <= 0xffffU) 1228 success = 1; 1229 done: 1230 if (successp) 1231 *successp = success; 1232 return (u_int16_t)(result); 1233 } 1234