File: check_zone

package info (click to toggle)
libnet-dns-perl 0.12-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 452 kB
  • ctags: 266
  • sloc: perl: 4,047; makefile: 52; sh: 17
file content (727 lines) | stat: -rwxr-xr-x 17,082 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
#!/usr/local/bin/perl -w
# $Id: check_zone,v 1.4 1997/06/29 04:48:45 mfuhr Exp $

=head1 NAME

check_zone - Check a DNS zone for errors

=head1 SYNOPSIS

C<check_zone> [ C<-r> ][ C<-v> ] I<domain> [ I<class> ]

=head1 DESCRIPTION

Checks a DNS zone for errors.  Current checks are:

=over 4

=item *

Checks the domain's SOA from each of the domain's name servers. The SOA serial numbers should match. This program's output cannot be trusted if they do not.

=item *

Tries to perform an AXFR from each of the domain's name servers. This test helps to detect whether the name server is blocking AXFR.

=item *

Checks that all A records have corresponding PTR records. For each A record its PTR's name is match checked.

=item *

Checks that all PTR records match an A record (sometimes they match a CNAME). Check the PTR's name against the A record.

=item *

Checks that hosts listed in NS, MX, and CNAME records have
A records. Checks for NS and CNAME records not pointing to another CNAME (i.e., they must directly resolve to an A record). That test may be somewhat controversial because, in many cases, a MX to a CNAME or a CNAME to another CNAME will resolve; however, in DNS circles it isn't a recommended practise.

=item *

Check each record processed for being with the class requested. This is an internal integrity check.

=back

=head1 OPTIONS

=over 4

=back

=item C<-r>

Perform a recursive check on subdomains.

=item C<-v>

Verbose.

=head1 AUTHORS

Originally developed by Michael Fuhr (mfuhr@dimensional.com) and hacked--with furor--by Dennis Glatting (dennis.glatting@software-munitions.com).

=head1 COPYRIGHT

=head1 SEE ALSO

L<perl(1)>, L<axfr>, L<check_soa>, L<mx>, L<perldig>, L<Net::DNS>

=head1 BUGS

A query for an A RR against a name that is a CNAME may not follow the CNAME to an A RR.

There isn't a mechanism to insure records are returned from an authoritative source.

There appears to be a bug in the resolver AXFR routine where, if one server cannot be contacted, the routine doesn't try another in its list.

=cut


require 'assert.pl';

use strict;
use vars qw($opt_r);
use vars qw($opt_v);

use Getopt::Std;
use File::Basename;
use IO::Socket;
use Net::DNS;

getopts("rv");

die "Usage: ", basename($0), " [ -r -v ] domain [ class ]\n"
    unless (@ARGV >= 1) && (@ARGV <= 2);


$opt_r = 1;

check_domain(@ARGV);
exit;


sub check_domain {

    my ( $domain, $class ) = @_;
    my $ns;
    my @zone;

    $class ||= "IN";

    print "-" x 70, "\n";
    print "$domain (class $class)\n";
    print "\n";

    my $res = new Net::DNS::Resolver;
    $res->defnames( 0 );
    $res->retry( 2 );


    my( $nspack, $ns_rr, @nsl );

    # Get a list of name servers for the domain.
    # Error-out if the query isn't satisfied.
    #
    
    $nspack = $res->query( $domain, 'NS', $class );
    unless( defined( $nspack )) {
    
        warn "Couldn't find nameservers for $domain: ",
             $res->errorstring, "\n";
        return;
    }

    printf( "List of name servers returned from '%s'\n", $res->answerfrom );
    foreach $ns_rr ( $nspack->answer ) {
        
        $ns_rr->print if( $opt_v );

        assert( $class eq $ns_rr->class );
        assert( 'NS' eq $ns_rr->type );

        if( $ns_rr->name eq $domain ) {
        
            print "\t", $ns_rr->rdatastr, "\n";
            push @nsl, $ns_rr->rdatastr;
        } else {
        
            warn( "asked for '$domain', got '%s'\n", $ns_rr->rdatastr );
        }
    }
    print "\n";

    warn( "\tZone has no NS records\n" ) if( scalar( @nsl ) == 0 );


    # Transfer the zone from each of the name servers. 
    # The zone is transferred for several reasons. 
    # First, so the check routines won't (an efficiency 
    # issue) and second, to see if we can.
    #
    
    $res->nameservers( @nsl );

    foreach $ns ( @nsl ) {
    
        $res->nameservers( $ns );
        
        my @local_zone = $res->axfr( $domain, $class );
        unless( defined( @local_zone ) && @local_zone ) {
    
            warn "Zone transfer from '", $ns, "' failed: ", 
                    $res->errorstring, "\n";
        }
        @zone = @local_zone if( ! defined( @zone ) && ! @zone );
    }

    # Query each name server for the zone
    # and check the zone's SOA serial number.
    # 
    
    print "checking SOA records\n";
    check_soa( $domain, $class, \@nsl );
    print "\n";

    
    # Check specific record types.
    #

    print "checking NS records\n";
    check_ns( $domain, $class, \@nsl, \@zone );
    print "\n";

    print "checking A records\n";
    check_a( $domain, $class, \@nsl, \@zone );
    print "\n";

    print "checking PTR records\n";
    check_ptr( $domain, $class, \@nsl, \@zone );
    print "\n";

    print "checking MX records\n";
    check_mx( $domain, $class, \@nsl, \@zone );
    print "\n";

    print "checking CNAME records\n";
    check_cname( $domain, $class, \@nsl, \@zone );
    print "\n";


    # Recurse?
    #

    if( $opt_r ) {
   
        my %subdomains;

        print "checking subdomains\n\n";

        # Get a list of NS records from the zone that 
        # are not for the zone (i.e., they're subdomains).
        #
        
        foreach ( grep { $_->type eq 'NS' and $_->name ne $domain } @zone ) {

            $subdomains{$_->name} = 1;
        }

        # For each subdomain, check it.
        #

        foreach ( sort keys %subdomains ) {

            check_domain($_, $class);
        }
    }
}

sub check_soa {

    my( $domain, $class, $nsl ) = @_;
    my( $soa_sn, $soa_diff ) = ( 0, 0 );
    my( $ns, $soa_rr );
    my $rr_count = 0;

    my $res = new Net::DNS::Resolver;

    $res->defnames( 0 );
    $res->retry( 2 );
    $res->recurse( 0 );

    # Contact each name server and get the
    #   SOA for the somain.
    #
    
    foreach $ns ( @$nsl ) {
    
        my $soa = 0;
        my $nspack;
        
        # Query the name server and test
        # for a result.
        #
        
        $res->nameservers( $ns );

        $nspack = $res->query( $domain, "SOA", $class );
        unless( defined( $nspack )) {
    
            warn "Couldn't get SOA from '$ns'\n";
            next;
        }

        # Look at each SOA for the domain from the
        # name server. Specifically, look to see if
        # its serial number is different across
        # the name servers.
        #
        
        foreach $soa_rr ( $nspack->answer ) {

            $soa_rr->print if( $opt_v );

            assert( $class eq $soa_rr->class );
            assert( 'SOA' eq $soa_rr->type );
            
            print "\t$ns:\t", $soa_rr->serial, "\n";

            # If soa_sn is zero then an SOA serial number
            # hasn't been recorded. In that case record
            # the serial number. If the serial number 
            # doesn't match a previously recorded one then
            # indicate they are different.
            #
            # If the serial numbers are different then you
            # cannot really trust the remainder of the test.
            #
            
            if( $soa_sn ) {
            
                $soa_diff = 1 if ( $soa_sn != $soa_rr->serial );
            } else {
            
                $soa_sn = $soa_rr->serial;
            }
        }
        
        ++$rr_count;
    }
    
    print "\t*** SOAs are different!\n" if( $soa_diff );
    print "$rr_count SOA RRs checked.\n";
}

sub check_ptr {

    my( $domain, $class, $nsl, $zone ) = @_;

    my $res = new Net::DNS::Resolver;
    my $ptr_rr;
    my $rr_count = 0;

    $res->defnames( 0 );
    $res->retry( 2 );
    $res->nameservers( @$nsl );

    foreach $ptr_rr ( grep { $_->type eq 'PTR' } @$zone ) {

        my @types;
    
        $ptr_rr->print if( $opt_v );

        assert( $class eq $ptr_rr->class );
        assert( 'PTR' eq $ptr_rr->type );

        print "\tchecking PTR rr '$ptr_rr' to PTR\n" if( $opt_v );

        @types = types4name( $ptr_rr->ptrdname, $domain, $class, $nsl );
        if( grep { $_ eq 'A' } @types ) {

            xcheck_ptr2a( $ptr_rr, $domain, $class, $nsl );
        } else {
        
            warn "\t'", $ptr_rr->ptrdname, 
                    "' doesn't resolve to an A RR (RRs are '",
                    join( ', ', @types ), "')\n";

        }

        
        ++$rr_count;
    }

    print "$rr_count PTR RRs checked.\n";
}

sub check_ns {

    my( $domain, $class, $nsl, $zone ) = @_;
    my $res = new Net::DNS::Resolver;
    my $ns_rr;
    my $rr_count = 0;

    $res->defnames( 0 );
    $res->retry( 2 );
    $res->nameservers( @$nsl );

    # Go through the zone data and process
    # all NS RRs for the zone (delegation
    # NS RRs are ignored). Specifically, 
    # check to see if the indicate name server
    # is a CNAME RR and the name resolves to an A
    # RR. Check to insure the address resolved
    # against the name has an associated PTR RR.
    #

    foreach $ns_rr ( grep { $_->type eq 'NS' } @$zone ) {

        my @types;

        $ns_rr->print if( $opt_v );

        assert( $class eq $ns_rr->class );
        assert( 'NS' eq $ns_rr->type );

        next if( $ns_rr->name ne $domain );
        
        printf( "rr nsdname:  %s\n", $ns_rr->nsdname ) if $opt_v;

        @types = types4name( $ns_rr->nsdname, $domain, $class, $nsl );
        if( grep { $_ eq 'A' } @types ) {
        
            xcheck_name( $ns_rr->nsdname, $domain, $class, $nsl );
        } else {
        
            warn "\t'", $ns_rr->nsdname, 
                    "' doesn't resolve to an A RR (RRs are '",
                    join( ', ', @types ), "')\n";
        }
        ++$rr_count;
    }
    
    print "$rr_count NS RRs checked.\n";
}

sub check_a {

    my( $domain, $class, $nsl, $zone ) = @_;

    my $res = new Net::DNS::Resolver;
    my $a_rr;
    my $rr_count = 0;

    $res->defnames( 0 );
    $res->retry( 2 );
    $res->nameservers( @$nsl );

    # Go through the zone data and process
    # all A RRs. Specifically, check to insure
    # each A RR matches a PTR RR and the PTR RR
    # matches the A RR.
    #

    foreach $a_rr ( grep { $_->type eq 'A' } @$zone ) {

        $a_rr->print if( $opt_v );

        assert( $class eq $a_rr->class );
        assert( 'A' eq $a_rr->type );

        print "\tchecking A RR '", $a_rr->address, "' to PTR\n" if( $opt_v );

        xcheck_a2ptr( $a_rr, $domain, $class, $nsl );
        
        ++$rr_count;
    }

    print "$rr_count A RRs checked.\n";
}


sub check_mx {

    my( $domain, $class, $nsl, $zone ) = @_;

    my $res = new Net::DNS::Resolver;
    my $mx_rr;
    my $rr_count = 0;

    $res->defnames( 0 );
    $res->retry( 2 );
    $res->nameservers( @$nsl );

    # Go through the zone data and process
    # all MX RRs. Specifically, check to insure
    # each MX RR resolves to an A RR and the 
    # A RR has a matching PTR RR.
    #

    foreach $mx_rr ( grep { $_->type eq 'MX' } @$zone ) {

        $mx_rr->print if( $opt_v );

        assert( $class eq $mx_rr->class );
        assert( 'MX' eq $mx_rr->type );

        print "\tchecking MX RR '", $mx_rr->exchange, "' to A\n" if( $opt_v );

        xcheck_name( $mx_rr->exchange, $domain, $class, $nsl );
        
        ++$rr_count;
    }

    print "$rr_count MX RRs checked.\n";
}

sub check_cname {

    my( $domain, $class, $nsl, $zone ) = @_;

    my $res = new Net::DNS::Resolver;
    my $cname_rr;
    my $rr_count = 0;

    $res->defnames( 0 );
    $res->retry( 2 );
    $res->nameservers( @$nsl );

    # Go through the zone data and process
    # all CNAME RRs. Specifically, check to insure
    # each CNAME RR resolves to an A RR and the 
    # A RR has a matching PTR RR.
    #

    foreach $cname_rr ( grep { $_->type eq 'CNAME' } @$zone ) {

        my @types;

        $cname_rr->print if( $opt_v );

        assert( $class eq $cname_rr->class );
        assert( 'CNAME' eq $cname_rr->type );

        print "\tchecking CNAME RR '", $cname_rr->cname, "' to A\n" 
            if( $opt_v );

        @types = types4name( $cname_rr->cname, $domain, $class, $nsl );
        if( grep { $_ eq 'A' } @types ) {
         
            xcheck_name( $cname_rr->cname, $domain, $class, $nsl );
        } else {
        
            warn "\t'", $cname_rr->cname, 
                    "' doesn't resolve to an A RR (RRs are '",
                    join( ', ', @types ), "')\n";
        }
    
        ++$rr_count;
    }

    print "$rr_count CNAME RRs checked.\n";
}


sub xcheck_a2ptr {

    my( $a_rr, $domain, $class, $nsl ) = @_;

    my $res = new Net::DNS::Resolver;

    $res->defnames( 0 );
    $res->retry( 2 );
    $res->nameservers( @$nsl );

    assert( $class eq $a_rr->class );
    assert( 'A' eq $a_rr->type );

    # Request a PTR RR against the A RR.
    # A missing PTR RR is an error.
    #

    my $ans = $res->query( $a_rr->address, 'PTR', $class );
    if( defined( $ans )) {

        my $ptr_rr;
        foreach $ptr_rr ( $ans->answer ) {

            $ptr_rr->print if( $opt_v );

            assert( $class eq $ptr_rr->class );
            assert( 'PTR' eq $ptr_rr->type );

            warn( "\t'", $a_rr->name, "' has address '", 
                    $a_rr->address, "' but PTR is '",  
                    $ptr_rr->ptrdname, "'\n" )
                if( $a_rr->name ne $ptr_rr->ptrdname );

            warn( "\t'", $a_rr->name, "' has address '", 
                    $a_rr->address, "' but PTR is '", 
                    ip_ptr2a_str( $ptr_rr->name ), "'\n" )
                if( $a_rr->address ne ip_ptr2a_str( $ptr_rr->name ));
        }
    } else {
    
        warn( "\tNO PTR RR for '", $a_rr->name, 
                "' at address '", $a_rr->address,"'\n" );
    }
}


sub xcheck_ptr2a {

    my( $ptr_rr, $domain, $class, $nsl ) = @_;

    my $res = new Net::DNS::Resolver;

    $res->defnames( 0 );
    $res->retry( 2 );
    $res->nameservers( @$nsl );

    assert( $class eq $ptr_rr->class );
    assert( 'PTR' eq $ptr_rr->type );

    # Request an A RR against the PTR RR.
    # A missing A RR is an error.
    #

    my $ans = $res->query( $ptr_rr->ptrdname, 'A', $class );
    if( defined( $ans )) {
            
        my $a_rr;
        foreach $a_rr ( $ans->answer ) {

            $a_rr->print if( $opt_v );

            assert( $class eq $a_rr->class );
            assert( 'A' eq $a_rr->type );

            warn( "\tPTR RR '", $ptr_rr->name, "' has name '", 
                    $ptr_rr->ptrdname, "' but A query returned '", 
                    $a_rr->name, "'\n" )
                if( $ptr_rr->ptrdname ne $a_rr->name );

            warn( "\tPTR RR '", $ptr_rr->name, "' has address '", 
                    ip_ptr2a_str( $ptr_rr->name ), 
                    "' but A query returned '", $a_rr->address, "'\n" )
                if( ip_ptr2a_str( $ptr_rr->name ) ne $a_rr->address );
        }
    } else {
    
        warn( "\tNO A RR for '", $ptr_rr->ptrdname, 
                "' at address '", ip_ptr2a_str( $ptr_rr->address ), "'\n" );
    }
}


sub xcheck_name {

    my( $name, $domain, $class, $nsl ) = @_;

    my $res = new Net::DNS::Resolver;

    $res->defnames( 0 );
    $res->retry( 2 );
    $res->nameservers( @$nsl );

    # Get the A RR for the name.
    #

    my $ans = $res->query( $name, 'A', $class );
    if( defined( $ans )) {
        
        # There is one or more A RRs.
        # For each A RR do a reverse look-up
        # and verify the PTR matches the A.
        #

        my $a_rr;
        foreach $a_rr ( $ans->answer ) {

            $a_rr->print if( $opt_v );

            assert( $class eq $a_rr->class );
            assert( 'A' eq $a_rr->type );

            warn( "\tQuery for '$name' returned A RR name '", 
                    $a_rr->name, "'\n" ) 
                if( $name ne $a_rr->name );

            xcheck_a2ptr( $a_rr, $domain, $class, $nsl );
        }
    } else {
        
        warn( "\t", $name, " has no A RR\n" );
    }

}


sub types4name {

    my( $name, $domain, $class, $nsl ) = @_;

    my $res = new Net::DNS::Resolver;
    my @rr_types;

    $res->defnames( 0 );
    $res->retry( 2 );
    $res->nameservers( @$nsl );

    # Get the RRs for the name.
    #

    my $ans = $res->query( $name, 'ANY', $class );
    if( defined( $ans )) {
        
        my $any_rr;
        foreach $any_rr ( $ans->answer ) {

            $any_rr->print if( $opt_v );

            assert( $class eq $any_rr->class );
            
            push @rr_types, ( $any_rr->type );
        }
    } else {
        
        warn( "\t'", $name, "' doesn't resolve.\n" );
    }
    
    # If there were no RRs for the name then
    # return the RR types of ???
    #
    
    push @rr_types, ( '???' ) if( ! defined( @rr_types ) || ! @rr_types );

    return @rr_types;
}


sub ip_ptr2a_str {

    my( $d, $c, $b, $a ) = ip_parts( $_[0]);

    return "$a.$b.$c.$d";
}



sub ip_parts {

    my $ip = $_[0];
    assert( $ip ne '' );

    if( $ip =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/oi ) {

        return ( $1, $2, $3, $4 );
    } else {

        warn "Unable to parse '$ip'\n";
    }

    assert( 0 );
}