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