Debian Codegraph @

dns_tree

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