summaryrefslogtreecommitdiff
path: root/src/lib/libc/net/res_debug.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/net/res_debug.c')
-rw-r--r--src/lib/libc/net/res_debug.c1395
1 files changed, 0 insertions, 1395 deletions
diff --git a/src/lib/libc/net/res_debug.c b/src/lib/libc/net/res_debug.c
deleted file mode 100644
index 246fefef3c..0000000000
--- a/src/lib/libc/net/res_debug.c
+++ /dev/null
@@ -1,1395 +0,0 @@
1/* $OpenBSD: res_debug.c,v 1.22 2007/10/11 18:36:41 jakob Exp $ */
2
3/*
4 * ++Copyright++ 1985, 1990, 1993
5 * -
6 * Copyright (c) 1985, 1990, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 * -
33 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
34 *
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies, and that
38 * the name of Digital Equipment Corporation not be used in advertising or
39 * publicity pertaining to distribution of the document or software without
40 * specific, written prior permission.
41 *
42 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
45 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49 * SOFTWARE.
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 * --Copyright--
74 */
75
76#include <sys/param.h>
77#include <sys/types.h>
78#include <sys/socket.h>
79#include <netinet/in.h>
80#include <arpa/inet.h>
81#include <arpa/nameser.h>
82
83#include <ctype.h>
84#include <netdb.h>
85#include <resolv.h>
86#include <stdio.h>
87#include <time.h>
88
89#include <stdlib.h>
90#include <string.h>
91
92#include "thread_private.h"
93
94extern const char *_res_opcodes[];
95extern const char *_res_resultcodes[];
96
97static const char *loc_ntoal(const u_char *binary, char *ascii, int ascii_len);
98
99/* XXX: we should use getservbyport() instead. */
100static const char *
101dewks(int wks)
102{
103 static char nbuf[20];
104
105 switch (wks) {
106 case 5: return "rje";
107 case 7: return "echo";
108 case 9: return "discard";
109 case 11: return "systat";
110 case 13: return "daytime";
111 case 15: return "netstat";
112 case 17: return "qotd";
113 case 19: return "chargen";
114 case 20: return "ftp-data";
115 case 21: return "ftp";
116 case 23: return "telnet";
117 case 25: return "smtp";
118 case 37: return "time";
119 case 39: return "rlp";
120 case 42: return "name";
121 case 43: return "whois";
122 case 53: return "domain";
123 case 57: return "apts";
124 case 59: return "apfs";
125 case 67: return "bootps";
126 case 68: return "bootpc";
127 case 69: return "tftp";
128 case 77: return "rje";
129 case 79: return "finger";
130 case 87: return "link";
131 case 95: return "supdup";
132 case 100: return "newacct";
133 case 101: return "hostnames";
134 case 102: return "iso-tsap";
135 case 103: return "x400";
136 case 104: return "x400-snd";
137 case 105: return "csnet-ns";
138 case 109: return "pop-2";
139 case 111: return "sunrpc";
140 case 113: return "auth";
141 case 115: return "sftp";
142 case 117: return "uucp-path";
143 case 119: return "nntp";
144 case 121: return "erpc";
145 case 123: return "ntp";
146 case 133: return "statsrv";
147 case 136: return "profile";
148 case 144: return "NeWS";
149 case 161: return "snmp";
150 case 162: return "snmp-trap";
151 case 170: return "print-srv";
152 default:
153 (void) snprintf(nbuf, sizeof nbuf, "%d", wks);
154 return (nbuf);
155 }
156}
157
158/* XXX: we should use getprotobynumber() instead. */
159static const char *
160deproto(int protonum)
161{
162 static char nbuf[20];
163
164 switch (protonum) {
165 case 1: return "icmp";
166 case 2: return "igmp";
167 case 3: return "ggp";
168 case 5: return "st";
169 case 6: return "tcp";
170 case 7: return "ucl";
171 case 8: return "egp";
172 case 9: return "igp";
173 case 11: return "nvp-II";
174 case 12: return "pup";
175 case 16: return "chaos";
176 case 17: return "udp";
177 default:
178 (void) snprintf(nbuf, sizeof nbuf, "%d", protonum);
179 return (nbuf);
180 }
181}
182
183static const u_char *
184do_rrset(const u_char *msg, int len, const u_char *cp, int cnt, int pflag,
185 FILE *file, const char *hs)
186{
187 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
188 int n;
189 int sflag;
190
191 /*
192 * Print answer records.
193 */
194 sflag = (_resp->pfcode & pflag);
195 if ((n = ntohs(cnt))) {
196 if ((!_resp->pfcode) ||
197 ((sflag) && (_resp->pfcode & RES_PRF_HEAD1)))
198 fprintf(file, "%s", hs);
199 while (--n >= 0) {
200 if ((!_resp->pfcode) || sflag) {
201 cp = p_rr(cp, msg, file);
202 } else {
203 unsigned int dlen;
204 cp += __dn_skipname(cp, cp + MAXCDNAME);
205 cp += INT16SZ;
206 cp += INT16SZ;
207 cp += INT32SZ;
208 dlen = _getshort((u_char*)cp);
209 cp += INT16SZ;
210 cp += dlen;
211 }
212 if ((cp - msg) > len)
213 return (NULL);
214 }
215 if ((!_resp->pfcode) ||
216 ((sflag) && (_resp->pfcode & RES_PRF_HEAD1)))
217 putc('\n', file);
218 }
219 return (cp);
220}
221
222void
223__p_query(const u_char *msg)
224{
225 __fp_query(msg, stdout);
226}
227
228/*
229 * Print the current options.
230 * This is intended to be primarily a debugging routine.
231 */
232void
233__fp_resstat(struct __res_state *statp, FILE *file)
234{
235 u_long mask;
236
237 fprintf(file, ";; res options:");
238 if (!statp)
239 statp = &_res;
240 for (mask = 1; mask != 0; mask <<= 1)
241 if (statp->options & mask)
242 fprintf(file, " %s", p_option(mask));
243 putc('\n', file);
244}
245
246/*
247 * Print the contents of a query.
248 * This is intended to be primarily a debugging routine.
249 */
250void
251__fp_nquery(const u_char *msg, int len, FILE *file)
252{
253 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
254 const u_char *cp, *endMark;
255 const HEADER *hp;
256 int n;
257
258 if (_res_init(0) == -1)
259 return;
260
261#define TruncTest(x) if (x > endMark) goto trunc
262#define ErrorTest(x) if (x == NULL) goto error
263
264 /*
265 * Print header fields.
266 */
267 hp = (HEADER *)msg;
268 cp = msg + HFIXEDSZ;
269 endMark = msg + len;
270 if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_HEADX) || hp->rcode) {
271 fprintf(file, ";; ->>HEADER<<- opcode: %s, status: %s, id: %u",
272 _res_opcodes[hp->opcode],
273 _res_resultcodes[hp->rcode],
274 ntohs(hp->id));
275 putc('\n', file);
276 }
277 if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_HEADX))
278 putc(';', file);
279 if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_HEAD2)) {
280 fprintf(file, "; flags:");
281 if (hp->qr)
282 fprintf(file, " qr");
283 if (hp->aa)
284 fprintf(file, " aa");
285 if (hp->tc)
286 fprintf(file, " tc");
287 if (hp->rd)
288 fprintf(file, " rd");
289 if (hp->ra)
290 fprintf(file, " ra");
291 if (hp->unused)
292 fprintf(file, " UNUSED-BIT-ON");
293 if (hp->ad)
294 fprintf(file, " ad");
295 if (hp->cd)
296 fprintf(file, " cd");
297 }
298 if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_HEAD1)) {
299 fprintf(file, "; Ques: %u", ntohs(hp->qdcount));
300 fprintf(file, ", Ans: %u", ntohs(hp->ancount));
301 fprintf(file, ", Auth: %u", ntohs(hp->nscount));
302 fprintf(file, ", Addit: %u", ntohs(hp->arcount));
303 }
304 if ((!_resp->pfcode) || (_resp->pfcode &
305 (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
306 putc('\n',file);
307 }
308 /*
309 * Print question records.
310 */
311 if ((n = ntohs(hp->qdcount))) {
312 if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_QUES))
313 fprintf(file, ";; QUESTIONS:\n");
314 while (--n >= 0) {
315 if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_QUES))
316 fprintf(file, ";;\t");
317 TruncTest(cp);
318 if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_QUES))
319 cp = p_cdnname(cp, msg, len, file);
320 else {
321 int n;
322 char name[MAXDNAME];
323
324 if ((n = dn_expand(msg, msg+len, cp, name,
325 sizeof name)) < 0)
326 cp = NULL;
327 else
328 cp += n;
329 }
330 ErrorTest(cp);
331 TruncTest(cp);
332 if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_QUES))
333 fprintf(file, ", type = %s",
334 __p_type(_getshort((u_char*)cp)));
335 cp += INT16SZ;
336 TruncTest(cp);
337 if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_QUES))
338 fprintf(file, ", class = %s\n",
339 __p_class(_getshort((u_char*)cp)));
340 cp += INT16SZ;
341 if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_QUES))
342 putc('\n', file);
343 }
344 }
345 /*
346 * Print authoritative answer records
347 */
348 TruncTest(cp);
349 cp = do_rrset(msg, len, cp, hp->ancount, RES_PRF_ANS, file,
350 ";; ANSWERS:\n");
351 ErrorTest(cp);
352
353 /*
354 * print name server records
355 */
356 TruncTest(cp);
357 cp = do_rrset(msg, len, cp, hp->nscount, RES_PRF_AUTH, file,
358 ";; AUTHORITY RECORDS:\n");
359 ErrorTest(cp);
360
361 TruncTest(cp);
362 /*
363 * print additional records
364 */
365 cp = do_rrset(msg, len, cp, hp->arcount, RES_PRF_ADD, file,
366 ";; ADDITIONAL RECORDS:\n");
367 ErrorTest(cp);
368 return;
369 trunc:
370 fprintf(file, "\n;; ...truncated\n");
371 return;
372 error:
373 fprintf(file, "\n;; ...malformed\n");
374}
375
376void
377__fp_query(const u_char *msg, FILE *file)
378{
379 fp_nquery(msg, PACKETSZ, file);
380}
381
382const u_char *
383__p_cdnname(const u_char *cp, const u_char *msg, int len, FILE *file)
384{
385 char name[MAXDNAME];
386 int n;
387
388 if ((n = dn_expand(msg, msg + len, cp, name, sizeof name)) < 0)
389 return (NULL);
390 if (name[0] == '\0')
391 putc('.', file);
392 else
393 fputs(name, file);
394 return (cp + n);
395}
396
397const u_char *
398__p_cdname(const u_char *cp, const u_char *msg, FILE *file)
399{
400 return (p_cdnname(cp, msg, PACKETSZ, file));
401}
402
403
404/* Return a fully-qualified domain name from a compressed name (with
405 length supplied). */
406
407const u_char *
408__p_fqnname(const u_char *cp, const u_char *msg, int msglen, char *name, int namelen)
409{
410 int n, newlen;
411
412 if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0)
413 return (NULL);
414 newlen = strlen(name);
415 if (newlen == 0 || name[newlen - 1] != '.') {
416 if (newlen + 1 >= namelen) /* Lack space for final dot */
417 return (NULL);
418 else
419 strlcpy(name + newlen, ".", namelen - newlen);
420 }
421 return (cp + n);
422}
423
424/* XXX: the rest of these functions need to become length-limited, too. (vix)
425 */
426
427const u_char *
428__p_fqname(const u_char *cp, const u_char *msg, FILE *file)
429{
430 char name[MAXDNAME];
431 const u_char *n;
432
433 n = __p_fqnname(cp, msg, MAXCDNAME, name, sizeof name);
434 if (n == NULL)
435 return (NULL);
436 fputs(name, file);
437 return (n);
438}
439
440/*
441 * Print resource record fields in human readable form.
442 */
443const u_char *
444__p_rr(const u_char *cp, const u_char *msg, FILE *file)
445{
446 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
447 int type, class, dlen, n, c;
448 struct in_addr inaddr;
449 const u_char *cp1, *cp2;
450 u_int32_t tmpttl, t;
451 int lcnt;
452 u_int16_t keyflags;
453 char rrname[MAXDNAME]; /* The fqdn of this RR */
454 char base64_key[MAX_KEY_BASE64];
455
456 if (_res_init(0) == -1) {
457 h_errno = NETDB_INTERNAL;
458 return (NULL);
459 }
460 cp = __p_fqnname(cp, msg, MAXCDNAME, rrname, sizeof rrname);
461 if (!cp)
462 return (NULL); /* compression error */
463 fputs(rrname, file);
464
465 type = _getshort((u_char*)cp);
466 cp += INT16SZ;
467 class = _getshort((u_char*)cp);
468 cp += INT16SZ;
469 tmpttl = _getlong((u_char*)cp);
470 cp += INT32SZ;
471 dlen = _getshort((u_char*)cp);
472 cp += INT16SZ;
473 cp1 = cp;
474 if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_TTLID))
475 fprintf(file, "\t%lu", (u_long)tmpttl);
476 if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_CLASS))
477 fprintf(file, "\t%s", __p_class(class));
478 fprintf(file, "\t%s", __p_type(type));
479 /*
480 * Print type specific data, if appropriate
481 */
482 switch (type) {
483 case T_A:
484 switch (class) {
485 case C_IN:
486 case C_HS:
487 bcopy(cp, (char *)&inaddr, INADDRSZ);
488 if (dlen == 4) {
489 fprintf(file, "\t%s", inet_ntoa(inaddr));
490 cp += dlen;
491 } else if (dlen == 7) {
492 char *address;
493 u_char protocol;
494 in_port_t port;
495
496 address = inet_ntoa(inaddr);
497 cp += INADDRSZ;
498 protocol = *(u_char*)cp;
499 cp += sizeof (u_char);
500 port = _getshort((u_char*)cp);
501 cp += INT16SZ;
502 fprintf(file, "\t%s\t; proto %u, port %u",
503 address, protocol, port);
504 }
505 break;
506 default:
507 cp += dlen;
508 }
509 break;
510 case T_CNAME:
511 case T_MB:
512 case T_MG:
513 case T_MR:
514 case T_NS:
515 case T_PTR:
516 putc('\t', file);
517 if ((cp = p_fqname(cp, msg, file)) == NULL)
518 return (NULL);
519 break;
520
521 case T_HINFO:
522 case T_ISDN:
523 cp2 = cp + dlen;
524 (void) fputs("\t\"", file);
525 if ((n = (unsigned char) *cp++) != 0) {
526 for (c = n; c > 0 && cp < cp2; c--) {
527 if (strchr("\n\"\\", *cp))
528 (void) putc('\\', file);
529 (void) putc(*cp++, file);
530 }
531 }
532 putc('"', file);
533 if (cp < cp2 && (n = (unsigned char) *cp++) != 0) {
534 (void) fputs ("\t\"", file);
535 for (c = n; c > 0 && cp < cp2; c--) {
536 if (strchr("\n\"\\", *cp))
537 (void) putc('\\', file);
538 (void) putc(*cp++, file);
539 }
540 putc('"', file);
541 } else if (type == T_HINFO) {
542 (void) fputs("\"?\"", file);
543 fprintf(file, "\n;; *** Warning *** OS-type missing");
544 }
545 break;
546
547 case T_SOA:
548 putc('\t', file);
549 if ((cp = p_fqname(cp, msg, file)) == NULL)
550 return (NULL);
551 putc(' ', file);
552 if ((cp = p_fqname(cp, msg, file)) == NULL)
553 return (NULL);
554 fputs(" (\n", file);
555 t = _getlong((u_char*)cp); cp += INT32SZ;
556 fprintf(file, "\t\t\t%lu\t; serial\n", (u_long)t);
557 t = _getlong((u_char*)cp); cp += INT32SZ;
558 fprintf(file, "\t\t\t%lu\t; refresh (%s)\n",
559 (u_long)t, __p_time(t));
560 t = _getlong((u_char*)cp); cp += INT32SZ;
561 fprintf(file, "\t\t\t%lu\t; retry (%s)\n",
562 (u_long)t, __p_time(t));
563 t = _getlong((u_char*)cp); cp += INT32SZ;
564 fprintf(file, "\t\t\t%lu\t; expire (%s)\n",
565 (u_long)t, __p_time(t));
566 t = _getlong((u_char*)cp); cp += INT32SZ;
567 fprintf(file, "\t\t\t%lu )\t; minimum (%s)",
568 (u_long)t, __p_time(t));
569 break;
570
571 case T_MX:
572 case T_AFSDB:
573 case T_RT:
574 fprintf(file, "\t%u ", _getshort((u_char*)cp));
575 cp += INT16SZ;
576 if ((cp = p_fqname(cp, msg, file)) == NULL)
577 return (NULL);
578 break;
579
580 case T_PX:
581 fprintf(file, "\t%u ", _getshort((u_char*)cp));
582 cp += INT16SZ;
583 if ((cp = p_fqname(cp, msg, file)) == NULL)
584 return (NULL);
585 putc(' ', file);
586 if ((cp = p_fqname(cp, msg, file)) == NULL)
587 return (NULL);
588 break;
589
590 case T_X25:
591 cp2 = cp + dlen;
592 (void) fputs("\t\"", file);
593 if ((n = (unsigned char) *cp++) != 0) {
594 for (c = n; c > 0 && cp < cp2; c--) {
595 if (strchr("\n\"\\", *cp))
596 (void) putc('\\', file);
597 (void) putc(*cp++, file);
598 }
599 }
600 putc('"', file);
601 break;
602
603 case T_TXT:
604 (void) putc('\t', file);
605 cp2 = cp1 + dlen;
606 while (cp < cp2) {
607 putc('"', file);
608 if ((n = (unsigned char) *cp++)) {
609 for (c = n; c > 0 && cp < cp2; c--) {
610 if (strchr("\n\"\\", *cp))
611 (void) putc('\\', file);
612 (void) putc(*cp++, file);
613 }
614 }
615 putc('"', file);
616 if (cp < cp2)
617 putc(' ', file);
618 }
619 break;
620
621 case T_NSAP:
622 (void) fprintf(file, "\t%s", inet_nsap_ntoa(dlen, cp, NULL));
623 cp += dlen;
624 break;
625
626 case T_AAAA: {
627 char t[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
628
629 fprintf(file, "\t%s", inet_ntop(AF_INET6, cp, t, sizeof t));
630 cp += dlen;
631 break;
632 }
633
634 case T_LOC: {
635 char t[255];
636
637 fprintf(file, "\t%s", loc_ntoal(cp, t, sizeof t));
638 cp += dlen;
639 break;
640 }
641
642 case T_NAPTR: {
643 u_int order, preference;
644
645 order = _getshort(cp); cp += INT16SZ;
646 preference = _getshort(cp); cp += INT16SZ;
647 fprintf(file, "\t%u %u ",order, preference);
648 /* Flags */
649 n = *cp++;
650 fprintf(file,"\"%.*s\" ", (int)n, cp);
651 cp += n;
652 /* Service */
653 n = *cp++;
654 fprintf(file,"\"%.*s\" ", (int)n, cp);
655 cp += n;
656 /* Regexp */
657 n = *cp++;
658 fprintf(file,"\"%.*s\" ", (int)n, cp);
659 cp += n;
660 if ((cp = p_fqname(cp, msg, file)) == NULL)
661 return (NULL);
662 break;
663 }
664
665 case T_SRV: {
666 u_int priority, weight, port;
667
668 priority = _getshort(cp); cp += INT16SZ;
669 weight = _getshort(cp); cp += INT16SZ;
670 port = _getshort(cp); cp += INT16SZ;
671 fprintf(file, "\t%u %u %u ", priority, weight, port);
672 if ((cp = p_fqname(cp, msg, file)) == NULL)
673 return (NULL);
674 break;
675 }
676
677 case T_MINFO:
678 case T_RP:
679 putc('\t', file);
680 if ((cp = p_fqname(cp, msg, file)) == NULL)
681 return (NULL);
682 putc(' ', file);
683 if ((cp = p_fqname(cp, msg, file)) == NULL)
684 return (NULL);
685 break;
686
687 case T_UINFO:
688 putc('\t', file);
689 fputs((char *)cp, file);
690 cp += dlen;
691 break;
692
693 case T_UID:
694 case T_GID:
695 if (dlen == 4) {
696 fprintf(file, "\t%u", _getlong((u_char*)cp));
697 cp += INT32SZ;
698 }
699 break;
700
701 case T_WKS:
702 if (dlen < INT32SZ + 1)
703 break;
704 bcopy(cp, (char *)&inaddr, INADDRSZ);
705 cp += INT32SZ;
706 fprintf(file, "\t%s %s ( ",
707 inet_ntoa(inaddr),
708 deproto((int) *cp));
709 cp += sizeof (u_char);
710 n = 0;
711 lcnt = 0;
712 while (cp < cp1 + dlen) {
713 c = *cp++;
714 do {
715 if (c & 0200) {
716 if (lcnt == 0) {
717 fputs("\n\t\t\t", file);
718 lcnt = 5;
719 }
720 fputs(dewks(n), file);
721 putc(' ', file);
722 lcnt--;
723 }
724 c <<= 1;
725 } while (++n & 07);
726 }
727 putc(')', file);
728 break;
729
730 case T_KEY:
731 putc('\t', file);
732 keyflags = _getshort(cp);
733 cp += 2;
734 fprintf(file,"0x%04x", keyflags ); /* flags */
735 fprintf(file," %u", *cp++); /* protocol */
736 fprintf(file," %u (", *cp++); /* algorithm */
737
738 n = b64_ntop(cp, (cp1 + dlen) - cp,
739 base64_key, sizeof base64_key);
740 for (c = 0; c < n; ++c) {
741 if (0 == (c & 0x3F))
742 fprintf(file, "\n\t");
743 putc(base64_key[c], file); /* public key data */
744 }
745
746 fprintf(file, " )");
747 if (n < 0)
748 fprintf(file, "\t; BAD BASE64");
749 fflush(file);
750 cp = cp1 + dlen;
751 break;
752
753 case T_SIG:
754 case T_RRSIG:
755 type = _getshort((u_char*)cp);
756 cp += INT16SZ;
757 fprintf(file, " %s", p_type(type));
758 fprintf(file, "\t%u", *cp++); /* algorithm */
759 /* Check label value and print error if wrong. */
760 n = *cp++;
761 c = dn_count_labels (rrname);
762 if (n != c)
763 fprintf(file, "\t; LABELS WRONG (%d should be %d)\n\t",
764 n, c);
765 /* orig ttl */
766 n = _getlong((u_char*)cp);
767 if (n != tmpttl)
768 fprintf(file, " %u", n);
769 cp += INT32SZ;
770 /* sig expire */
771 fprintf(file, " (\n\t%s",
772 __p_secstodate(_getlong((u_char*)cp)));
773 cp += INT32SZ;
774 /* time signed */
775 fprintf(file, " %s", __p_secstodate(_getlong((u_char*)cp)));
776 cp += INT32SZ;
777 /* sig footprint */
778 fprintf(file," %u ", _getshort((u_char*)cp));
779 cp += INT16SZ;
780 /* signer's name */
781 cp = p_fqname(cp, msg, file);
782 n = b64_ntop(cp, (cp1 + dlen) - cp,
783 base64_key, sizeof base64_key);
784 for (c = 0; c < n; c++) {
785 if (0 == (c & 0x3F))
786 fprintf (file, "\n\t");
787 putc(base64_key[c], file); /* signature */
788 }
789 /* Clean up... */
790 fprintf(file, " )");
791 if (n < 0)
792 fprintf(file, "\t; BAD BASE64");
793 fflush(file);
794 cp = cp1+dlen;
795 break;
796
797#ifdef ALLOW_T_UNSPEC
798 case T_UNSPEC:
799 {
800 int NumBytes = 8;
801 u_char *DataPtr;
802 int i;
803
804 if (dlen < NumBytes) NumBytes = dlen;
805 fprintf(file, "\tFirst %d bytes of hex data:",
806 NumBytes);
807 for (i = 0, DataPtr = cp; i < NumBytes; i++, DataPtr++)
808 fprintf(file, " %x", *DataPtr);
809 cp += dlen;
810 }
811 break;
812#endif /* ALLOW_T_UNSPEC */
813
814 default:
815 fprintf(file, "\t?%d?", type);
816 cp += dlen;
817 }
818#if 0
819 fprintf(file, "\t; dlen=%d, ttl %s\n", dlen, __p_time(tmpttl));
820#else
821 putc('\n', file);
822#endif
823 if (cp - cp1 != dlen) {
824 fprintf(file, ";; packet size error (found %ld, dlen was %d)\n",
825 (long)(cp - cp1), dlen);
826 cp = NULL;
827 }
828 return (cp);
829}
830
831int
832__sym_ston(const struct res_sym *syms, char *name, int *success)
833{
834 for (; syms->name != 0; syms++) {
835 if (strcasecmp (name, syms->name) == 0) {
836 if (success)
837 *success = 1;
838 return (syms->number);
839 }
840 }
841 if (success)
842 *success = 0;
843 return (syms->number); /* The default value. */
844}
845
846
847const char *
848__sym_ntop(const struct res_sym *syms, int number, int *success)
849{
850 static char unname[20];
851
852 for (; syms->name != 0; syms++) {
853 if (number == syms->number) {
854 if (success)
855 *success = 1;
856 return (syms->humanname);
857 }
858 }
859 snprintf(unname, sizeof unname, "%d", number);
860 if (success)
861 *success = 0;
862 return (unname);
863}
864
865/*
866 * Return a mnemonic for an option
867 */
868const char *
869__p_option(u_long option)
870{
871 static char nbuf[40];
872
873 switch (option) {
874 case RES_INIT: return "init";
875 case RES_DEBUG: return "debug";
876 case RES_AAONLY: return "aaonly(unimpl)";
877 case RES_USEVC: return "usevc";
878 case RES_PRIMARY: return "primry(unimpl)";
879 case RES_IGNTC: return "igntc";
880 case RES_RECURSE: return "recurs";
881 case RES_DEFNAMES: return "defnam";
882 case RES_STAYOPEN: return "styopn";
883 case RES_DNSRCH: return "dnsrch";
884 case RES_INSECURE1: return "insecure1";
885 case RES_INSECURE2: return "insecure2";
886 case RES_USE_INET6: return "inet6";
887 case RES_USE_EDNS0: return "edns0";
888 default:
889 snprintf(nbuf, sizeof nbuf, "?0x%lx?", (u_long)option);
890 return (nbuf);
891 }
892}
893
894/*
895 * Return a mnemonic for a time to live
896 */
897const char *
898p_time(u_int32_t value)
899{
900 static char nbuf[40];
901 char *ebuf;
902 int secs, mins, hours, days;
903 char *p;
904 int tmp;
905
906 if (value == 0) {
907 strlcpy(nbuf, "0 secs", sizeof nbuf);
908 return (nbuf);
909 }
910
911 secs = value % 60;
912 value /= 60;
913 mins = value % 60;
914 value /= 60;
915 hours = value % 24;
916 value /= 24;
917 days = value;
918 value = 0;
919
920#define PLURALIZE(x) x, (x == 1) ? "" : "s"
921 p = nbuf;
922 ebuf = nbuf + sizeof(nbuf);
923 if (days) {
924 if ((tmp = snprintf(p, ebuf - p, "%d day%s",
925 PLURALIZE(days))) >= ebuf - p || tmp < 0)
926 goto full;
927 p += tmp;
928 }
929 if (hours) {
930 if (days)
931 *p++ = ' ';
932 if (p >= ebuf)
933 goto full;
934 if ((tmp = snprintf(p, ebuf - p, "%d hour%s",
935 PLURALIZE(hours))) >= ebuf - p || tmp < 0)
936 goto full;
937 p += tmp;
938 }
939 if (mins) {
940 if (days || hours)
941 *p++ = ' ';
942 if (p >= ebuf)
943 goto full;
944 if ((tmp = snprintf(p, ebuf - p, "%d min%s",
945 PLURALIZE(mins))) >= ebuf - p || tmp < 0)
946 goto full;
947 p += tmp;
948 }
949 if (secs || ! (days || hours || mins)) {
950 if (days || hours || mins)
951 *p++ = ' ';
952 if (p >= ebuf)
953 goto full;
954 if ((tmp = snprintf(p, ebuf - p, "%d sec%s",
955 PLURALIZE(secs))) >= ebuf - p || tmp < 0)
956 goto full;
957 }
958 return (nbuf);
959full:
960 p = nbuf + sizeof(nbuf) - 4;
961 *p++ = '.';
962 *p++ = '.';
963 *p++ = '.';
964 *p++ = '\0';
965 return (nbuf);
966}
967
968/*
969 * routines to convert between on-the-wire RR format and zone file format.
970 * Does not contain conversion to/from decimal degrees; divide or multiply
971 * by 60*60*1000 for that.
972 */
973
974static unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000,
975 1000000,10000000,100000000,1000000000};
976
977/* takes an XeY precision/size value, returns a string representation. */
978static const char *
979precsize_ntoa(u_int8_t prec)
980{
981 static char retbuf[sizeof "90000000.00"];
982 unsigned long val;
983 int mantissa, exponent;
984
985 mantissa = (int)((prec >> 4) & 0x0f) % 10;
986 exponent = (int)((prec >> 0) & 0x0f) % 10;
987
988 val = mantissa * poweroften[exponent];
989
990 (void) snprintf(retbuf, sizeof retbuf, "%ld.%.2ld", val/100, val%100);
991 return (retbuf);
992}
993
994/* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */
995static u_int8_t
996precsize_aton(char **strptr)
997{
998 unsigned int mval = 0, cmval = 0;
999 u_int8_t retval = 0;
1000 char *cp;
1001 int exponent;
1002 int mantissa;
1003
1004 cp = *strptr;
1005
1006 while (isdigit(*cp))
1007 mval = mval * 10 + (*cp++ - '0');
1008
1009 if (*cp == '.') { /* centimeters */
1010 cp++;
1011 if (isdigit(*cp)) {
1012 cmval = (*cp++ - '0') * 10;
1013 if (isdigit(*cp)) {
1014 cmval += (*cp++ - '0');
1015 }
1016 }
1017 }
1018 cmval = (mval * 100) + cmval;
1019
1020 for (exponent = 0; exponent < 9; exponent++)
1021 if (cmval < poweroften[exponent+1])
1022 break;
1023
1024 mantissa = cmval / poweroften[exponent];
1025 if (mantissa > 9)
1026 mantissa = 9;
1027
1028 retval = (mantissa << 4) | exponent;
1029
1030 *strptr = cp;
1031
1032 return (retval);
1033}
1034
1035/* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */
1036static u_int32_t
1037latlon2ul(char **latlonstrptr, int *which)
1038{
1039 char *cp;
1040 u_int32_t retval;
1041 int deg = 0, min = 0, secs = 0, secsfrac = 0;
1042
1043 cp = *latlonstrptr;
1044
1045 while (isdigit(*cp))
1046 deg = deg * 10 + (*cp++ - '0');
1047
1048 while (isspace(*cp))
1049 cp++;
1050
1051 if (!(isdigit(*cp)))
1052 goto fndhemi;
1053
1054 while (isdigit(*cp))
1055 min = min * 10 + (*cp++ - '0');
1056
1057 while (isspace(*cp))
1058 cp++;
1059
1060 if (!(isdigit(*cp)))
1061 goto fndhemi;
1062
1063 while (isdigit(*cp))
1064 secs = secs * 10 + (*cp++ - '0');
1065
1066 if (*cp == '.') { /* decimal seconds */
1067 cp++;
1068 if (isdigit(*cp)) {
1069 secsfrac = (*cp++ - '0') * 100;
1070 if (isdigit(*cp)) {
1071 secsfrac += (*cp++ - '0') * 10;
1072 if (isdigit(*cp)) {
1073 secsfrac += (*cp++ - '0');
1074 }
1075 }
1076 }
1077 }
1078
1079 while (!isspace(*cp)) /* if any trailing garbage */
1080 cp++;
1081
1082 while (isspace(*cp))
1083 cp++;
1084
1085 fndhemi:
1086 switch (*cp) {
1087 case 'N': case 'n':
1088 case 'E': case 'e':
1089 retval = ((unsigned)1<<31)
1090 + (((((deg * 60) + min) * 60) + secs) * 1000)
1091 + secsfrac;
1092 break;
1093 case 'S': case 's':
1094 case 'W': case 'w':
1095 retval = ((unsigned)1<<31)
1096 - (((((deg * 60) + min) * 60) + secs) * 1000)
1097 - secsfrac;
1098 break;
1099 default:
1100 retval = 0; /* invalid value -- indicates error */
1101 break;
1102 }
1103
1104 switch (*cp) {
1105 case 'N': case 'n':
1106 case 'S': case 's':
1107 *which = 1; /* latitude */
1108 break;
1109 case 'E': case 'e':
1110 case 'W': case 'w':
1111 *which = 2; /* longitude */
1112 break;
1113 default:
1114 *which = 0; /* error */
1115 break;
1116 }
1117
1118 cp++; /* skip the hemisphere */
1119
1120 while (!isspace(*cp)) /* if any trailing garbage */
1121 cp++;
1122
1123 while (isspace(*cp)) /* move to next field */
1124 cp++;
1125
1126 *latlonstrptr = cp;
1127
1128 return (retval);
1129}
1130
1131/* converts a zone file representation in a string to an RDATA on-the-wire
1132 * representation. */
1133int
1134loc_aton(const char *ascii, u_char *binary)
1135{
1136 const char *maxcp;
1137 u_char *bcp;
1138 char *cp;
1139
1140 u_int32_t latit = 0, longit = 0, alt = 0;
1141 u_int32_t lltemp1 = 0, lltemp2 = 0;
1142 int altmeters = 0, altfrac = 0, altsign = 1;
1143 u_int8_t hp = 0x16; /* default = 1e6 cm = 10000.00m = 10km */
1144 u_int8_t vp = 0x13; /* default = 1e3 cm = 10.00m */
1145 u_int8_t siz = 0x12; /* default = 1e2 cm = 1.00m */
1146 int which1 = 0, which2 = 0;
1147
1148 cp = (char *)ascii;
1149 maxcp = cp + strlen(ascii);
1150
1151 lltemp1 = latlon2ul(&cp, &which1);
1152
1153 lltemp2 = latlon2ul(&cp, &which2);
1154
1155 switch (which1 + which2) {
1156 case 3: /* 1 + 2, the only valid combination */
1157 if ((which1 == 1) && (which2 == 2)) { /* normal case */
1158 latit = lltemp1;
1159 longit = lltemp2;
1160 } else if ((which1 == 2) && (which2 == 1)) { /* reversed */
1161 longit = lltemp1;
1162 latit = lltemp2;
1163 } else { /* some kind of brokenness */
1164 return (0);
1165 }
1166 break;
1167 default: /* we didn't get one of each */
1168 return (0);
1169 }
1170
1171 /* altitude */
1172 if (*cp == '-') {
1173 altsign = -1;
1174 cp++;
1175 }
1176
1177 if (*cp == '+')
1178 cp++;
1179
1180 while (isdigit(*cp))
1181 altmeters = altmeters * 10 + (*cp++ - '0');
1182
1183 if (*cp == '.') { /* decimal meters */
1184 cp++;
1185 if (isdigit(*cp)) {
1186 altfrac = (*cp++ - '0') * 10;
1187 if (isdigit(*cp)) {
1188 altfrac += (*cp++ - '0');
1189 }
1190 }
1191 }
1192
1193 alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));
1194
1195 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
1196 cp++;
1197
1198 while (isspace(*cp) && (cp < maxcp))
1199 cp++;
1200
1201 if (cp >= maxcp)
1202 goto defaults;
1203
1204 siz = precsize_aton(&cp);
1205
1206 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
1207 cp++;
1208
1209 while (isspace(*cp) && (cp < maxcp))
1210 cp++;
1211
1212 if (cp >= maxcp)
1213 goto defaults;
1214
1215 hp = precsize_aton(&cp);
1216
1217 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
1218 cp++;
1219
1220 while (isspace(*cp) && (cp < maxcp))
1221 cp++;
1222
1223 if (cp >= maxcp)
1224 goto defaults;
1225
1226 vp = precsize_aton(&cp);
1227
1228 defaults:
1229
1230 bcp = binary;
1231 *bcp++ = (u_int8_t) 0; /* version byte */
1232 *bcp++ = siz;
1233 *bcp++ = hp;
1234 *bcp++ = vp;
1235 PUTLONG(latit,bcp);
1236 PUTLONG(longit,bcp);
1237 PUTLONG(alt,bcp);
1238
1239 return (16); /* size of RR in octets */
1240}
1241
1242const char *
1243loc_ntoa(const u_char *binary, char *ascii)
1244{
1245 return loc_ntoal(binary, ascii, 255);
1246}
1247
1248/* takes an on-the-wire LOC RR and formats it in a human readable format. */
1249static const char *
1250loc_ntoal(const u_char *binary, char *ascii, int ascii_len)
1251{
1252 static char *error = "?";
1253 const u_char *cp = binary;
1254
1255 int latdeg, latmin, latsec, latsecfrac;
1256 int longdeg, longmin, longsec, longsecfrac;
1257 char northsouth, eastwest;
1258 int altmeters, altfrac, altsign;
1259
1260 const int referencealt = 100000 * 100;
1261
1262 int32_t latval, longval, altval;
1263 u_int32_t templ;
1264 u_int8_t sizeval, hpval, vpval, versionval;
1265
1266 char *sizestr, *hpstr, *vpstr;
1267
1268 versionval = *cp++;
1269
1270 if (versionval) {
1271 snprintf(ascii, ascii_len, "; error: unknown LOC RR version");
1272 return (ascii);
1273 }
1274
1275 sizeval = *cp++;
1276
1277 hpval = *cp++;
1278 vpval = *cp++;
1279
1280 GETLONG(templ, cp);
1281 latval = (templ - ((unsigned)1<<31));
1282
1283 GETLONG(templ, cp);
1284 longval = (templ - ((unsigned)1<<31));
1285
1286 GETLONG(templ, cp);
1287 if (templ < referencealt) { /* below WGS 84 spheroid */
1288 altval = referencealt - templ;
1289 altsign = -1;
1290 } else {
1291 altval = templ - referencealt;
1292 altsign = 1;
1293 }
1294
1295 if (latval < 0) {
1296 northsouth = 'S';
1297 latval = -latval;
1298 } else
1299 northsouth = 'N';
1300
1301 latsecfrac = latval % 1000;
1302 latval = latval / 1000;
1303 latsec = latval % 60;
1304 latval = latval / 60;
1305 latmin = latval % 60;
1306 latval = latval / 60;
1307 latdeg = latval;
1308
1309 if (longval < 0) {
1310 eastwest = 'W';
1311 longval = -longval;
1312 } else
1313 eastwest = 'E';
1314
1315 longsecfrac = longval % 1000;
1316 longval = longval / 1000;
1317 longsec = longval % 60;
1318 longval = longval / 60;
1319 longmin = longval % 60;
1320 longval = longval / 60;
1321 longdeg = longval;
1322
1323 altfrac = altval % 100;
1324 altmeters = (altval / 100) * altsign;
1325
1326 if ((sizestr = strdup(precsize_ntoa(sizeval))) == NULL)
1327 sizestr = error;
1328 if ((hpstr = strdup(precsize_ntoa(hpval))) == NULL)
1329 hpstr = error;
1330 if ((vpstr = strdup(precsize_ntoa(vpval))) == NULL)
1331 vpstr = error;
1332
1333 snprintf(ascii, ascii_len,
1334 "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %d.%.2dm %sm %sm %sm",
1335 latdeg, latmin, latsec, latsecfrac, northsouth,
1336 longdeg, longmin, longsec, longsecfrac, eastwest,
1337 altmeters, altfrac, sizestr, hpstr, vpstr);
1338
1339 if (sizestr != error)
1340 free(sizestr);
1341 if (hpstr != error)
1342 free(hpstr);
1343 if (vpstr != error)
1344 free(vpstr);
1345
1346 return (ascii);
1347}
1348
1349
1350/* Return the number of DNS hierarchy levels in the name. */
1351int
1352__dn_count_labels(char *name)
1353{
1354 int i, len, count;
1355
1356 len = strlen(name);
1357
1358 for(i = 0, count = 0; i < len; i++) {
1359 if (name[i] == '.')
1360 count++;
1361 }
1362
1363 /* don't count initial wildcard */
1364 if (name[0] == '*')
1365 if (count)
1366 count--;
1367
1368 /* don't count the null label for root. */
1369 /* if terminating '.' not found, must adjust */
1370 /* count to include last label */
1371 if (len > 0 && name[len-1] != '.')
1372 count++;
1373 return (count);
1374}
1375
1376
1377/*
1378 * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
1379 * SIG records are required to be printed like this, by the Secure DNS RFC.
1380 */
1381char *
1382__p_secstodate (long unsigned int secs)
1383{
1384 static char output[15]; /* YYYYMMDDHHMMSS and null */
1385 time_t clock = secs;
1386 struct tm *time;
1387
1388 time = gmtime(&clock);
1389 time->tm_year += 1900;
1390 time->tm_mon += 1;
1391 snprintf(output, sizeof output, "%04d%02d%02d%02d%02d%02d",
1392 time->tm_year, time->tm_mon, time->tm_mday,
1393 time->tm_hour, time->tm_min, time->tm_sec);
1394 return (output);
1395}