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 # taken from Array::Utils
32 # http://cpansearch.perl.org/src/ZMIJ/Array-Utils-0.5/Utils.pm
33 # This module is Copyright (c) 2007 Sergei A. Fedorov.
34 # You may distribute under the terms of either the GNU General Public
35 # License or the Artistic License, as specified in the Perl README file.
38 my %e = map { $_ => undef } @{$_[0]};
39 return grep { exists( $e{$_} ) } @{$_[1]};
41 sub array_diff(\@\@) {
42 my %e = map { $_ => undef } @{$_[1]};
43 return @{[ ( grep { (exists $e{$_}) ? ( delete $e{$_} ) : ( 1 ) } @{ $_[0] } ), keys %e ] };
45 sub array_minus(\@\@) {
46 my %e = map{ $_ => undef } @{$_[1]};
47 return grep( ! exists( $e{$_} ), @{$_[0]} );
51 $SIG{'__DIE__'} = sub { print @_; exit 4; };
53 my $RES = Net::DNS::Resolver->new;
54 my $DLV = 'dlv.isc.org';
64 print "Querying $type $zone\n" if $params->{'verbose'};
65 my $pkt = $RES->send($zone, $type);
66 return () unless $pkt;
67 return () unless $pkt->answer;
68 for my $rr ($pkt->answer) {
69 next unless ($rr->type eq $type);
70 next unless (lc($rr->name) eq lc($zone));
72 my $tag = $options{'pretty'} ? sprintf("%5d(%d)", $rr->keytag, $rr->algorithm) : $rr->keytag;
73 # for now only handle KSKs, i.e. keys with the SEP flag set
74 if ($type eq 'DNSKEY' && !($rr->is_sep)) {
81 if ($type eq 'DNSKEY' && (scalar @result) == 0) {
82 # use remaining keys if no keys with the SEP bit are present
86 @result = sort {$a cmp $b} grep {!$unique{$_}++} @result;
93 return get_tag_generic($zone, 'DNSKEY', %options);
98 return get_tag_generic($zone, 'DS', %options);
104 return get_tag_generic($zone, 'DLV', %options);
106 sub has_dnskey_parent {
109 my $potential_parent;
110 if ($zone =~ m/\./) {
111 $potential_parent = $zone;
112 $potential_parent =~ s/^[^.]+\.//;
114 $potential_parent = '.';
117 print "Querying DNSKEY $potential_parent\n" if $params->{'verbose'};
118 my $pkt = $RES->send($potential_parent, 'DNSKEY');
119 return undef unless $pkt;
120 return undef unless $pkt->header;
122 unless ($pkt->answer) {
123 return undef unless $pkt->authority;
124 for my $rr ($pkt->authority) {
125 next unless ($rr->type eq 'SOA');
127 $potential_parent = $rr->name;
128 print "Querying DNSKEY $potential_parent\n" if $params->{'verbose'};
129 $pkt = $RES->send($potential_parent, 'DNSKEY');
130 return undef unless $pkt;
135 return (0, $potential_parent) unless $pkt->answer;
136 for my $rr ($pkt->answer) {
137 next unless ($rr->type eq 'DNSKEY');
138 return (1, $potential_parent);
141 sub get_parent_dnssec_status {
146 my ($status, $parent) = has_dnskey_parent($zone);
147 last unless defined $status;
148 push @result, ($status ? "yes" : "no") . ("($parent)");
150 last if $zone eq "" || $zone eq '.';
153 return join(', ', @result);
160 print $fd "Usage: $PROGRAM_NAME [--dir <dir>] overview|check-dlv|check-ds|check-header zone [zone...]\n";
161 print $fd " $PROGRAM_NAME --dir <dir> overview|check-dlv|check-ds|check-header\n";
162 print $fd " $PROGRAM_NAME --help\n";
168 my $zonefile = shift;
173 open(F, "<", $zonefile) or die ("Cannot open zonefile $zonefile for $zone: $!\n");
175 if (/^[#;]\s*dlv-submit\s*=\s*yes\s*$/) { $do_dlv = 1; }
176 if (/^[#;]\s*ds-in-parent\s*=\s*yes\s*$/) { $do_ds = 1; }
180 return { 'dlv' => $do_dlv,
187 my @elems = intersect(@$a, @$b);
188 push @elems, map { '-'.$_ } array_minus(@$a, @$b);
189 push @elems, map { '+'.$_ } array_minus(@$b, @$a);
190 return join(',', @elems);
193 Getopt::Long::config('bundling');
195 '--help' => \$params->{'help'},
196 '--dir=s@' => \$params->{'dir'},
197 '--dlv=s' => \$params->{'dlv'},
198 '--verbose' => \$params->{'verbose'},
199 ) or usage(\*STDERR, 1);
200 usage(\*STDOUT, 0) if ($params->{'help'});
202 my $mode = shift @ARGV;
203 usage(\*STDOUT, 0) unless (defined $mode && $mode =~ /^(overview|check-dlv|check-ds|check-header)$/);
204 die ("check-header needs --dir") if ($mode eq 'check-header' && !defined $params->{'dir'});
208 if (defined $params->{'dir'} && $mode ne 'check-header') {
209 warn "--dir option ignored"
211 %zones = map { $_ => $_} @ARGV;
213 my $dirs = $params->{'dir'};
214 usage(\*STDOUT, 0) unless (defined $dirs);
216 for my $dir (@$dirs) {
217 chdir $dir or die "chdir $dir failed? $!\n";
218 opendir DIR, '.' or die ("Cannot opendir $dir\n");
219 for my $file (readdir DIR) {
220 next if ( -l "$file" );
221 next unless ( -f "$file" );
222 next if $file =~ /^(dsset|keyset)-/;
225 if ($file =~ /\.zone$/) { # it's one of our yaml things
226 $zone = basename($file, '.zone');
228 $zones{$zone} = "$dir/$file";
234 $DLV = $params->{'dlv'} if $params->{'dlv'};
237 if ($mode eq 'overview') {
239 for my $zone (keys %zones) {
240 $data{$zone} = { 'dnskey' => join(', ', get_dnskeytags($zone, pretty=>1)),
241 'ds' => join(', ', get_dstags($zone, pretty=>1)),
242 'dlv' => join(', ', get_dlvtags($zone, pretty=>1)),
243 'parent_dnssec' => get_parent_dnssec_status($zone) };
246 my $format = "%60s %-20s %-15s %-3s %-10s\n";
247 printf $format, "zone", "DNSKEY", "DS\@parent", "DLV", "dnssec\@parent";
248 printf $format, "-"x 60, "-"x 20, "-"x 15, "-"x 3, "-"x 10;
249 for my $zone (sort {$a cmp $b} keys %data) {
250 printf $format, $zone,
251 $data{$zone}->{'dnskey'},
252 $data{$zone}->{'ds'},
253 $data{$zone}->{'dlv'},
254 $data{$zone}->{'parent_dnssec'};
257 } elsif ($mode eq 'check-dlv' || $mode eq 'check-ds' || $mode eq 'check-header') {
259 push @to_check, 'dlv' if $mode eq 'check-header' || $mode eq 'check-dlv';
260 push @to_check, 'ds' if $mode eq 'check-header' || $mode eq 'check-ds';
264 for my $zone (sort {$a cmp $b} keys %zones) {
265 my $require = { map { $_ => 1 } @to_check };
266 if ($mode eq 'check-header') {
267 $require = what_to_check($zone, $zones{$zone})
270 my @dnskey = get_dnskeytags($zone);
271 for my $thiskey (@to_check) {
272 my @target = $thiskey eq 'ds' ? get_dstags($zone) : get_dlvtags($zone);
274 my $spec = diff_spec(\@target, \@dnskey);
275 # if the intersection between DS and KEY is empty,
276 # or if there are DS records for keys we do not have, that's an issue.
277 if (intersect(@dnskey, @target) == 0 || array_minus(@target, @dnskey)) {
278 if ($require->{$thiskey} || scalar @target > 0) {
279 push @warn, "$zone ($spec)";
282 if ($require->{$thiskey}) {
283 push @ok, "$zone ($spec)";
288 print "WARNING: ", join(", ", @warn), "\n" if (scalar @warn);
289 print "OK: ", join(", ", @ok), "\n" if (scalar @ok);
290 exit (1) if (scalar @warn);
293 die ("Invalid mode '$mode'\n");