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;
31 $SIG{'__DIE__'} = sub { print @_; exit 4; };
33 my $RES = Net::DNS::Resolver->new;
34 my $DLV = 'dlv.isc.org';
43 print "Querying $type $zone\n" if $params->{'verbose'};
44 my $pkt = $RES->send($zone, $type);
45 return () unless $pkt;
46 return () unless $pkt->answer;
47 for my $rr ($pkt->answer) {
48 next unless ($rr->type eq $type);
49 next unless (lc($rr->name) eq lc($zone));
51 # for now only handle KSKs, i.e. keys with the SEP flag set
52 if ($type eq 'DNSKEY' && !($rr->is_sep)) {
53 push @zsks, $rr->keytag;
57 push @result, $rr->keytag;
59 if ($type eq 'DNSKEY' && (scalar @result) == 0) {
60 # use remaining keys if no keys with the SEP bit are present
64 @result = sort {$a <=> $b} grep {!$unique{$_}++} @result;
70 return get_tag_generic($zone, 'DNSKEY');
74 return get_tag_generic($zone, 'DS');
79 return get_tag_generic($zone, 'DLV');
81 sub has_dnskey_parent {
86 $potential_parent = $zone;
87 $potential_parent =~ s/^[^.]+\.//;
89 $potential_parent = '.';
92 print "Querying DNSKEY $potential_parent\n" if $params->{'verbose'};
93 my $pkt = $RES->send($potential_parent, 'DNSKEY');
94 return undef unless $pkt;
95 return undef unless $pkt->header;
97 unless ($pkt->answer) {
98 return undef unless $pkt->authority;
99 for my $rr ($pkt->authority) {
100 next unless ($rr->type eq 'SOA');
102 $potential_parent = $rr->name;
103 print "Querying DNSKEY $potential_parent\n" if $params->{'verbose'};
104 $pkt = $RES->send($potential_parent, 'DNSKEY');
105 return undef unless $pkt;
110 return (0, $potential_parent) unless $pkt->answer;
111 for my $rr ($pkt->answer) {
112 next unless ($rr->type eq 'DNSKEY');
113 return (1, $potential_parent);
116 sub get_parent_dnssec_status {
121 my ($status, $parent) = has_dnskey_parent($zone);
122 last unless defined $status;
123 push @result, ($status ? "yes" : "no") . ("($parent)");
125 last if $zone eq "" || $zone eq '.';
128 return join(', ', @result);
135 print $fd "Usage: $PROGRAM_NAME [--dir <dir>] overview|check-dlv|check-ds|check-header zone [zone...]\n";
136 print $fd " $PROGRAM_NAME --dir <dir> overview|check-dlv|check-ds|check-header\n";
137 print $fd " $PROGRAM_NAME --help\n";
143 my $zonefile = shift;
148 open(F, "<", $zonefile) or die ("Cannot open zonefile $zonefile for $zone: $!\n");
150 if (/^[#;]\s*dlv-submit\s*=\s*yes\s*$/) { $do_dlv = 1; }
151 if (/^[#;]\s*ds-in-parent\s*=\s*yes\s*$/) { $do_ds = 1; }
155 return { 'dlv' => $do_dlv,
159 Getopt::Long::config('bundling');
161 '--help' => \$params->{'help'},
162 '--dir=s@' => \$params->{'dir'},
163 '--dlv=s' => \$params->{'dlv'},
164 '--verbose' => \$params->{'verbose'},
165 ) or usage(\*STDERR, 1);
166 usage(\*STDOUT, 0) if ($params->{'help'});
168 my $mode = shift @ARGV;
169 usage(\*STDOUT, 0) unless (defined $mode && $mode =~ /^(overview|check-dlv|check-ds|check-header)$/);
170 die ("check-header needs --dir") if ($mode eq 'check-header' && !defined $params->{'dir'});
174 if (defined $params->{'dir'} && $mode ne 'check-header') {
175 warn "--dir option ignored"
177 %zones = map { $_ => $_} @ARGV;
179 my $dirs = $params->{'dir'};
180 usage(\*STDOUT, 0) unless (defined $dirs);
182 for my $dir (@$dirs) {
183 chdir $dir or die "chdir $dir failed? $!\n";
184 opendir DIR, '.' or die ("Cannot opendir $dir\n");
185 for my $file (readdir DIR) {
186 next if ( -l "$file" );
187 next unless ( -f "$file" );
188 next if $file =~ /^(dsset|keyset)-/;
191 if ($file =~ /\.zone$/) { # it's one of our yaml things
192 $zone = basename($file, '.zone');
194 $zones{$zone} = "$dir/$file";
200 $DLV = $params->{'dlv'} if $params->{'dlv'};
203 if ($mode eq 'overview') {
205 for my $zone (keys %zones) {
206 $data{$zone} = { 'dnskey' => join(', ', get_dnskeytags($zone)),
207 'ds' => join(', ', get_dstags($zone)),
208 'dlv' => join(', ', get_dlvtags($zone)),
209 'parent_dnssec' => get_parent_dnssec_status($zone) };
212 my $format = "%60s %-15s %-15s %-3s %-10s\n";
213 printf $format, "zone", "DNSKEY", "DS\@parent", "DLV", "dnssec\@parent";
214 printf $format, "-"x 60, "-"x 15, "-"x 15, "-"x 3, "-"x 10;
215 for my $zone (sort {$a cmp $b} keys %data) {
216 printf $format, $zone,
217 $data{$zone}->{'dnskey'},
218 $data{$zone}->{'ds'},
219 $data{$zone}->{'dlv'},
220 $data{$zone}->{'parent_dnssec'};
223 } elsif ($mode eq 'check-dlv' || $mode eq 'check-ds' || $mode eq 'check-header') {
225 push @to_check, 'dlv' if $mode eq 'check-header' || $mode eq 'check-dlv';
226 push @to_check, 'ds' if $mode eq 'check-header' || $mode eq 'check-ds';
230 for my $zone (sort {$a cmp $b} keys %zones) {
231 my $require = { map { $_ => 1 } @to_check };
232 if ($mode eq 'check-header') {
233 $require = what_to_check($zone, $zones{$zone})
236 my $dnskey = join(', ', get_dnskeytags($zone)) || '-';
237 for my $thiskey (@to_check) {
238 my $target = join(', ', $thiskey eq 'ds' ? get_dstags($zone) : get_dlvtags($zone)) || '-';
240 if ($dnskey ne $target) {
241 if ($require->{$thiskey} || $target ne '-') {
242 push @warn, "$zone ([$dnskey] != [$target])";
245 if ($require->{$thiskey}) {
246 push @ok, "$zone ($dnskey)";
251 print "WARNING: ", join(", ", @warn), "\n" if (scalar @warn);
252 print "OK: ", join(", ", @ok), "\n" if (scalar @ok);
253 exit (1) if (scalar @warn);
256 die ("Invalid mode '$mode'\n");