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