Debian Codegraph @

dns_tree

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #!/home/johnh/BIN/perl5 -w
2
3 #
4 # dns_tree
5 # Copyright (C) 1997 by John Heidemann
6 # $Id: dns_tree,v 1.22 2002/05/13 16:33:34 johnh Exp $
7 #
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #
22
23
24 sub usage {
25 print STDERR <<END;
26 usage: $0
27
28 display a tree-structured view of the dns
29
30 Options:
31 -f override warnings (force)
32 -t TYPE show only records of these TYPE (repeat for multiple types)
33 (the ``all'' type does everything I know about)
34 -m MATCH show only records whos first component matches the
35 perl regexp MATCH
36 -v verbose (show all DNS requests)
37
38 (Only -f is currently implemented)
39
40 Data is cached in ~/.DNS_BROWSE with an approximation of the usual DNS
41 caching rules. Remove all files in that directory to prematurely
42 flush the cache.
43
44 Some information is returned in ``fake records'':
45 z head of a zone
46 iz branch point internal to a zone
47 i information
48 e errors
49
50 Bugs and desired features:
51 Currently requires that you start it at the root of a zone.
52 END
53 exit 1;
54 }
55
56 use strict;
57
58 use Carp;
59 use Getopt::Long;
60 my(%opts);
61 my($SORT, $A, $TTL, $TYPE, $B) = (0..20);
62 my($SOA_HOST, $SOA_E_MAIL, $SOA_SERIAL, $SOA_REFRESH, $SOA_RETRY, $SOA_EXPIRE, $SOA_MINIMUM) = ($B..20);
63 my(@default_types) = qw(NS A SOA);
64 my(@maximal_types) = qw(A CNAME HINFO LOC MX NS PTR TXT SOA);
65 # Temp creation race found by the Debian folks.
66 # http://bugs.debian.org/146591
67 # Fix: make the tmp file in the users home directory.
68 my($cache_dir) = $ENV{'HOME'} . "/.DNS_TREE";
69 my(@dangereous_names) = qw(com. edu. org. in-addr.arpa. co.uk.); # all lc
70 my(@messages) = ();
71
72 sub parse_time_spec {
73 my($spec) = @_;
74 $spec = lc($spec);
75 my($secs) = 0;
76 while ($spec ne '') {
77 # seconds, minutes, hours, days, weeks
78 $secs += $1, next if ($spec =~ s/^([.0-9]+)s[a-z]*//);
79 $secs += $1 * 60, next if ($spec =~ s/^([.0-9]+)m[a-z]*//);
80 $secs += $1 * 60*60, next if ($spec =~ s/^([.0-9]+)h[a-z]*//);
81 $secs += $1 * 60*60*24, next if ($spec =~ s/^([.0-9]+)d[a-z]*//);
82 $secs += $1 * 60*60*24*7, next if ($spec =~ s/^([.0-9]+)w[a-z]*//);
83 # no units, assume seconds
84 $secs += $1, next if ($spec =~ s/^([.0-9]+)$//);
85 die "$0: unknown time specification: $spec\n";
86 };
87 return $secs;
88 }
89
90 sub dns_make_absolute {
91 my($name) = @_;
92 return $name if ($name =~ /\.$/);
93 return "$name.";
94 }
95
96 sub dns_to_sort {
97 my($name) = lc($_[0]);
98 my(@f) = reverse split(/\./, $name);
99 return join('.', @f);
100 }
101
102 sub rec_to_sort {
103 my($rec_ref) = @_;
104 my($sort, $a, $ttl, $type, $b) = @$rec_ref;
105 return dns_to_sort($a);
106 }
107
108 sub sort_level {
109 my($res_ref) = @_;
110 my($count) = $#$res_ref;
111
112 # generate sorting keys
113 foreach (0..$count) {
114 $res_ref->[$_][$SORT] = rec_to_sort(\@{$res_ref->[$_]});
115 # print $res_ref->[$_][$A] . " => " . $res_ref->[$_][$SORT] . "\n";
116 };
117
118 # do it
119 my(@keys) = sort {
120 $res_ref->[$a][$SORT] cmp $res_ref->[$b][$SORT]
121 } (0..$count);
122
123 return \@keys;
124 }
125
126 sub display_rec {
127 my($name, $rec_aref, $stats_href, $depth) = @_;
128 my($sort, $a, $ttl, $type, $b) = @$rec_aref;
129
130 return if ($a !~ /$name$/i); # skip stuff not in our domain
131
132 my($suba) = $a;
133 $suba =~ s/$name$//i;
134 # return if ($suba eq ''); # skip records for our level
135 return if ($suba eq '' && !($type eq 'TXT' || $type eq 'MX')); # skip some records for our level
136 $suba =~ s/\.$//; # trim . between sub and old name
137
138 # only show one NS for each subthing
139 if ($type eq 'NS' || $type eq 'iz') {
140 my($lc_suba) = lc($suba);
141 return if (defined($stats_href->{subns_shown_href}{$lc_suba}));
142 $stats_href->{subns_shown_href}{$lc_suba} = 1;
143 $stats_href->{subns_shown_count}++;
144 };
145
146 # only show A/CNAMES records pointing to web servers
147 if (!($type eq 'NS' || $type eq 'iz') && defined($opts{'match'})) {
148 return if ($suba !~ /$opts{'match'}/i);
149 };
150
151 # getting subdomain without NSes up to it? fake them?
152 if ($suba =~ /\./) {
153 my($car, $cdr) = ($a =~ /^([^.]+)\.(.+)$/);
154 my(@fake_rec) = (0, $cdr, 1, 'iz');
155 display_rec($name, \@fake_rec, $stats_href, $depth);
156 };
157
158 my($dots) = $suba =~ tr/././;
159 my($tabs) = "\t" x ($dots + $depth);
160 my($subsuba) = ($dots > 0) ? ($suba =~ /^([^.]+)\./): ($suba);
161
162 my($rest) = "";
163 $rest = $b if ($type eq 'TXT');
164
165 print "$tabs$type\t$subsuba$rest\n";
166 }
167
168 sub display_level {
169 my($name, $res_ref, $ns) = @_;
170
171 my($count) = $#$res_ref;
172 print "z\t$name\n";
173 foreach (@messages) {
174 print "\ti\t$_\n";
175 };
176 print "\ti\t($count records, from $ns)\n";
177
178 my($keys_ref) = sort_level($res_ref);
179
180 my(%stats);
181 $stats{'subnses_shown_count'} = 0;
182 $stats{'subnses_shown_href'} = { };
183 my($i);
184 foreach $i (@$keys_ref) {
185 display_rec($name, \@{$res_ref->[$i]}, \%stats, 1);
186 };
187 # print "\ti\t" . $stats{'subnses_shown_count'} . "\tsub-domains\n";
188 }
189
190 sub dig {
191 my($ah_ref) = @_;
192 my($cmd, $name, $server, $cache_duration) = ($ah_ref->{cmd}, $ah_ref->{name}, $ah_ref->{server}, $ah_ref->{cache_duration});
193 my(@msgs);
194
195 # What should we keep from the dig output?
196 my($typesa_ref) = $ah_ref->{types};
197 $typesa_ref = \@default_types if (!defined($typesa_ref));
198 my(%types) = ();
199 foreach (@$typesa_ref) {
200 $types{$_} = 1;
201 };
202
203 croak "bad cmd" if (!defined($cmd));
204 croak "bad name" if (!defined($name));
205 if (!defined($server)) {
206 $server = "";
207 } else {
208 $server = '@' . $server;
209 };
210 $cache_duration = 60 * 60 * 12 if (!defined($cache_duration)); # 12 hours
211
212 # Normalize case (important for cache files).
213 ($cmd, $name, $server) = (lc($cmd), lc($name), lc($server));
214
215 my(@res);
216 my($ok) = 0;
217
218 # Implement caching (of the raw dig output).
219 # check cache
220 my($from_cache) = 0;
221 my($fn) = "$cache_dir/" . lc("$cmd:$name:$server");
222 my($done) = 0;
223 # check cache validity
224 if (-f $fn) {
225 my($age) = (-M $fn) * 24 * 60 * 60;
226 if ($age > $cache_duration) {
227 # print STDERR "$fn is out of date\n";
228 unlink($fn);
229 };
230 };
231 if (-f $fn) {
232 open(DIG, "< $fn") || croak "cached dig";
233 $from_cache = 1;
234 } else {
235 open(DIG, "dig $cmd $name $server |") || croak "dig";
236 open(CACHE, "> $fn") || croak "dig to cache $fn";
237 };
238
239 # Parse dig output line-by-line.
240 # Currently we implement a simple two-state machine
241 # to handle SOA records.
242 my($in_soa) = 0;
243 my($old_dig) = 1; # default to pre-bind-8.0 dig
244 my(@soa);
245 my($origin) = ".";
246 my($lasta) = undef;
247 while (<DIG>) {
248 print CACHE $_ if (!$from_cache);
249 if ($in_soa) {
250 my($dummy, $n) = split(/\s+/);
251 push(@soa, $n);
252 $in_soa++;
253 if ($in_soa > $SOA_MINIMUM) {
254 $in_soa = 0;
255 push(@res, [@soa]);
256 @soa = ();
257 };
258 next;
259 };
260 next if (/^\s*$/);
261 next if (/^\s+/);
262 $done = 1 if (/^;; ADDITIONAL RECORDS/);
263 if (/^;\s*\<\<\>\> DiG\s+([0-9]+)\s*.*\<\<\>\>\s*(.*)$/) {
264 $old_dig = 0 if ($1 >= 8);
265 # record signatures
266 push(@msgs, $2);
267 };
268 if (/^;; (Received 0 answers.*)$/) {
269 push(@msgs, $1);
270 };
271 next if ($done);
272 next if (/^;/);
273 if (/^\$ORIGIN\s+(\S+)$/) {
274 $origin = $1;
275 next;
276 };
277 $ok++;
278 #
279 # parse it.
280 my($a, $ttl, $type, $b, $c, @rest);
281 if ($old_dig) {
282 ($a, $ttl, $type, $b, $c, @rest) = split(/\s+/);
283 } else {
284 my($in);
285 ($a, $ttl, $in, $type, $b, $c, @rest) = split(/\s+/);
286 };
287 #
288 # handle default names and the origin
289 if ($a eq '') {
290 die "$0: undef lasta.\n" if (!defined($lasta));
291 $a = $lasta;
292 } else {
293 $lasta = $a;
294 };
295 $a .= $origin if ($a !~ /\.$/);
296 #
297 next if (!defined($type));
298 if ($type eq 'SOA') {
299 # special case: multi-line SOAs
300 @soa = (0, $a, $ttl, $type, $b, $c);
301 $in_soa = $SOA_SERIAL;
302 next;
303 };
304 if ($type eq 'TXT') { # hack
305 $c = '' if (!defined($c));
306 @rest = ("") if ($#rest == -1);
307 $b = "$b $c " . join(" ", @rest);
308 $c = undef; @rest = ();
309 };
310 if (defined($types{$type})) {
311 push(@res, [0, $a, $ttl, $type, $b]);
312 };
313 };
314 close DIG;
315 close CACHE if (!$from_cache);
316 return (undef, \@msgs) if (!$ok);
317
318 return (\@res, \@msgs);
319 }
320
321 sub push_msgs {
322 my($msgs_ref) = @_;
323 if (defined($msgs_ref)) {
324 push(@messages, @$msgs_ref);
325 };
326 }
327
328 sub fetch_nses {
329 my($name, $cache_duration) = @_;
330
331 my($res_ref, $msgs_ref) = dig({cmd => 'ns', name => $name, types => [qw(NS)], cache_duration => $cache_duration});
332 die "fetch_nses: dig ns failed\n" if (!defined($res_ref));
333 push_msgs($msgs_ref) if (defined($opts{'verbose'}));
334
335 my(@nses);
336 foreach (@$res_ref) {
337 push(@nses, $_->[$B]);
338 };
339 return \@nses;
340 }
341
342 sub fetch_level_from_ns {
343 my($name, $ns, $types_aref, $cache_duration) = @_;
344
345 my($res_ref, $msgs_ref) = dig({cmd => 'axfr', name => $name, server => $ns, types => $types_aref}, cache_duration => $cache_duration);
346 push_msgs($msgs_ref);
347 return $res_ref;
348 }
349
350 sub fetch_level_from_nses {
351 my($name, $nses_ref, $types_aref, $cache_duration) = @_;
352
353 my($ns);
354 foreach $ns (@$nses_ref) {
355 my($res_ref) = fetch_level_from_ns($name, $ns, $types_aref, $cache_duration);
356 return ($res_ref, $ns) if defined($res_ref);
357 };
358 return undef;
359 }
360
361 sub fetch_soa {
362 my($name) = @_;
363
364 my($soa_ref, $msgs_ref) = dig({cmd => 'soa', name => $name, types => [qw(SOA)]});
365 die "fetch_soa: dig soa failed\n" if (!defined($soa_ref));
366 push_msgs($msgs_ref) if (defined($opts{'verbose'}));
367 return $soa_ref->[0];
368 }
369
370 sub fetch_level {
371 my($name, $types_aref) = @_;
372 my($soa_ref) = fetch_soa($name);
373 my($cache_duration) = parse_time_spec($soa_ref->[$SOA_MINIMUM]);
374 my($nses_ref) = fetch_nses($name, $cache_duration);
375 return undef if (!defined($nses_ref));
376 my(@f) = fetch_level_from_nses($name, $nses_ref, $types_aref, $cache_duration);
377 return @f;
378 }
379
380 sub dangereous_name {
381 my($name) = @_;
382
383 return undef if (defined($opts{'force'}));
384
385 $name = lc($name);
386 foreach (@dangereous_names) {
387 return 1 if ($name eq $_);
388 };
389 return 0;
390 }
391
392 sub main {
393 &usage if ($#ARGV >= 0 && $ARGV[0] eq '-?');
394 &GetOptions(\%opts, qw(f m=s t=s@ v));
395 &usage if ($#ARGV != 0);
396
397 $opts{'force'} = $opts{'f'};
398 $opts{'match'} = $opts{'m'};
399 $opts{'types'} = $opts{'t'};
400 $opts{'verbose'} = $opts{'v'};
401 my($types_aref) = $opts{'types'};
402 if (defined($types_aref) && $#$types_aref == 0 && $types_aref->[0] eq 'all') {
403 # special case "all"
404 $types_aref = $opts{'types'} = \@maximal_types;
405 };
406
407 # Be careful about perms on the cache dir (thanks, Debian).
408 if (! -d $cache_dir) {
409 mkdir($cache_dir,0700) or die "$0: cannot mkdir $cache_dir.\n";
410 };
411
412 my($name) = $ARGV[0];
413 $name = dns_make_absolute($name);
414
415 if (dangereous_name($name)) {
416 print "z\t$name\n\te\tdangerously-large-zone\n";
417 return;
418 };
419
420 $types_aref = (defined($opts{'types'}) ? $opts{'types'} : [qw(NS A CNAME)]);
421 push (@$types_aref, qw(i e m));
422 my($res_ref, $ns) = fetch_level($name, $types_aref);
423 if (defined($res_ref)) {
424 display_level($name, $res_ref, $ns);
425 } else {
426 foreach (@messages) {
427 print "\ti\t$_\n";
428 };
429 print "\te\terror looking up $name.\n";
430 exit 1;
431 };
432 }
433
434 main;
435 exit 0;
436
437
438