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