3 # Copyright (c) 2010 Peter Palfrader <peter@palfrader.org>
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 use Net::DNS::Resolver;
30 $SIG{'__DIE__'} = sub { print @_; exit 4; };
32 my $RES = Net::DNS::Resolver->new;
33 my $DLV = 'dlv.isc.org';
40 my $pkt = $RES->send($zone, $type);
41 return () unless $pkt;
42 return () unless $pkt->answer;
43 for my $rr ($pkt->answer) {
44 next unless ($rr->type eq $type);
45 next unless (lc($rr->name) eq lc($zone));
47 # only handle KSKs, i.e. keys with the SEP flag set
48 next if ($type eq 'DNSKEY' && !($rr->is_sep));
50 push @result, $rr->keytag;
53 @result = sort {$a <=> $b} grep {!$unique{$_}++} @result;
59 return get_tag_generic($zone, 'DNSKEY');
63 return get_tag_generic($zone, 'DS');
68 return get_tag_generic($zone, 'DLV');
70 sub has_dnskey_parent {
73 my $potential_parent = $zone;
74 $potential_parent =~ s/^[^.]*\.//;
76 my $pkt = $RES->send($potential_parent, 'DNSKEY');
77 return undef unless $pkt;
78 return undef unless $pkt->header;
80 # try to find the zone start
81 unless ($pkt->answer) {
82 #print "Looking for zone apex\n";
83 return undef unless $pkt->authority;
84 for my $rr ($pkt->authority) {
85 next unless ($rr->type eq 'SOA');
87 $potential_parent = $rr->name;
88 #print "Found it at $potential_parent\n";
89 $pkt = $RES->send($potential_parent, 'DNSKEY');
90 return undef unless $pkt;
95 return 0 unless $pkt->answer;
96 for my $rr ($pkt->answer) {
97 next unless ($rr->type eq 'DNSKEY');
106 print $fd "Usage: $PROGRAM_NAME [--dir <dir>] overview|check-dlv|check-ds|check-header zone [zone...]\n";
107 print $fd " $PROGRAM_NAME --dir <dir> overview|check-dlv|check-ds|check-header\n";
108 print $fd " $PROGRAM_NAME --help\n";
119 open(F, "<", $indir."/".$zone) or die ("Cannot open zonefile for $zone: $!\n");
121 if (/^;\s*dlv-submit\s*=\s*yes\s*$/) { $do_dlv = 1; }
122 if (/^;\s*ds-in-parent\s*=\s*yes\s*$/) { $do_ds = 1; }
127 push @keys, 'dlv' if $do_dlv;
128 push @keys, 'ds' if $do_ds;
133 Getopt::Long::config('bundling');
135 '--help' => \$params->{'help'},
136 '--dir=s' => \$params->{'dir'},
137 '--dlv=s' => \$params->{'dlv'},
138 ) or usage(\*STDERR, 1);
139 usage(\*STDOUT, 0) if ($params->{'help'});
141 my $mode = shift @ARGV;
142 usage(\*STDOUT, 0) unless (defined $mode && $mode =~ /^(overview|check-dlv|check-ds|check-header)$/);
143 die ("check-header needs --dir") if ($mode eq 'check-header' && !defined $params->{'dir'});
147 if (defined $params->{'dir'} && $mode ne 'check-header') {
148 warn "--dir option ignored"
152 my $dir = $params->{'dir'};
153 usage(\*STDOUT, 0) unless (defined $dir);
155 chdir $dir or die "chdir $dir failed? $!\n";
156 opendir DIR, '.' or die ("Cannot opendir $dir\n");
157 for my $file (readdir DIR) {
158 next if ( -l "$file" );
159 next unless ( -f "$file" );
160 next if $file =~ /^(dsset|keyset)-/;
167 $DLV = $params->{'dlv'} if $params->{'dlv'};
170 for my $zone (@zones) {
171 $data{$zone} = { 'dnskey' => join(', ', get_dnskeytags($zone)),
172 'ds' => join(', ', get_dstags($zone)),
173 'dlv' => join(', ', get_dlvtags($zone)),
174 'parent_dnssec' => has_dnskey_parent($zone) };
177 if ($mode eq 'overview') {
178 my $format = "%60s %-10s %-10s %-10s %-10s\n";
179 printf $format, "zone", "DNSKEY", "DS\@parent", "DLV", "dnssec\@parent";
180 printf $format, "-"x 60, "-"x 10, "-"x 10, "-"x 10, "-"x 10;
181 for my $zone (sort {$a cmp $b} keys %data) {
182 printf $format, $zone,
183 $data{$zone}->{'dnskey'},
184 $data{$zone}->{'ds'},
185 $data{$zone}->{'dlv'},
186 $data{$zone}->{'parent_dnssec'} ? 'yes' : '-';
189 } elsif ($mode eq 'check-dlv' || $mode eq 'check-ds' || $mode eq 'check-header') {
191 $key = 'dlv' if $mode eq 'check-dlv';
192 $key = 'ds' if $mode eq 'check-ds';
193 $key = 'per-zone' if $mode eq 'check-header';
194 die ("key undefined") unless $key;
198 for my $zone (sort {$a cmp $b} keys %data) {
199 my @thiskeys = $key eq 'per-zone' ? what_to_check($zone, $params->{'dir'}) : ($key);
201 for my $thiskey (@thiskeys) {
202 my $dnskey = $data{$zone}->{'dnskey'} || '-';
203 my $target = $data{$zone}->{$thiskey} || '-';
205 if ($dnskey ne $target) {
206 push @warn, "$zone ($dnskey != $target)";
208 push @ok, "$zone ($dnskey)";
212 print "WARNING: ", join(", ", @warn), "\n" if (scalar @warn);
213 print "OK: ", join(", ", @ok), "\n" if (scalar @ok);
214 exit (1) if (scalar @warn);
217 die ("Invalid mode '$mode'\n");