dsa-check-dnssec-delegation: accept any ds/dnskey combination whose intersection...
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / dsa-check-dnssec-delegation
1 #!/usr/bin/perl
2
3 # Copyright (c) 2010 Peter Palfrader <peter@palfrader.org>
4 #
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:
12 #
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
15 #
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.
23
24 use strict;
25 use warnings;
26 use English;
27 use Net::DNS::Resolver;
28 use Getopt::Long;
29 use File::Basename;
30
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.
36 #
37 sub intersect(\@\@) {
38         my %e = map { $_ => undef } @{$_[0]};
39         return grep { exists( $e{$_} ) } @{$_[1]};
40 }
41 sub array_diff(\@\@) {
42         my %e = map { $_ => undef } @{$_[1]};
43         return @{[ ( grep { (exists $e{$_}) ? ( delete $e{$_} ) : ( 1 ) } @{ $_[0] } ), keys %e ] };
44 }
45 sub array_minus(\@\@) {
46         my %e = map{ $_ => undef } @{$_[1]};
47         return grep( ! exists( $e{$_} ), @{$_[0]} );
48 }
49
50
51 $SIG{'__DIE__'} = sub { print @_; exit 4; };
52
53 my $RES = Net::DNS::Resolver->new;
54 my $DLV = 'dlv.isc.org';
55 my $params;
56
57 sub get_tag_generic {
58         my $zone = shift;
59         my $type = shift;
60
61         my @result;
62         my @zsks;
63         print "Querying $type $zone\n" if $params->{'verbose'};
64         my $pkt = $RES->send($zone, $type);
65         return () unless $pkt;
66         return () unless $pkt->answer;
67         for my $rr ($pkt->answer) {
68                 next unless ($rr->type eq $type);
69                 next unless (lc($rr->name) eq lc($zone));
70
71                 # for now only handle KSKs, i.e. keys with the SEP flag set
72                 if ($type eq 'DNSKEY' && !($rr->is_sep)) {
73                         push @zsks, $rr->keytag;
74                         next;
75                 }
76
77                 push @result, $rr->keytag;
78         };
79         if ($type eq 'DNSKEY' && (scalar @result) == 0) {
80                 # use remaining keys if no keys with the SEP bit are present
81                 @result = @zsks;
82         }
83         my %unique = ();
84         @result = sort {$a <=> $b} grep {!$unique{$_}++} @result;
85         return @result
86 };
87
88 sub get_dnskeytags {
89         my $zone = shift;
90         return get_tag_generic($zone, 'DNSKEY');
91 };
92 sub get_dstags {
93         my $zone = shift;
94         return get_tag_generic($zone, 'DS');
95 };
96 sub get_dlvtags {
97         my $zone = shift;
98         $zone .= ".".$DLV;
99         return get_tag_generic($zone, 'DLV');
100 };
101 sub has_dnskey_parent {
102         my $zone = shift;
103
104         my $potential_parent;
105         if ($zone =~ m/\./) {
106                 $potential_parent = $zone;
107                 $potential_parent =~ s/^[^.]+\.//;
108         } else {
109                 $potential_parent = '.';
110         }
111
112         print "Querying DNSKEY $potential_parent\n" if $params->{'verbose'};
113         my $pkt = $RES->send($potential_parent, 'DNSKEY');
114         return undef unless $pkt;
115         return undef unless $pkt->header;
116
117         unless ($pkt->answer) {
118                 return undef unless $pkt->authority;
119                 for my $rr ($pkt->authority) {
120                         next unless ($rr->type eq 'SOA');
121
122                         $potential_parent = $rr->name;
123                         print "Querying DNSKEY $potential_parent\n" if $params->{'verbose'};
124                         $pkt = $RES->send($potential_parent, 'DNSKEY');
125                         return undef unless $pkt;
126                         last;
127                 };
128         };
129
130         return (0, $potential_parent) unless $pkt->answer;
131         for my $rr ($pkt->answer) {
132                 next unless ($rr->type eq 'DNSKEY');
133                 return (1, $potential_parent);
134         };
135 }
136 sub get_parent_dnssec_status {
137         my $zone = shift;
138         my @result;
139
140         while (1) {
141                 my ($status, $parent) = has_dnskey_parent($zone);
142                 last unless defined $status;
143                 push @result, ($status ? "yes" : "no") . ("($parent)");
144                 $zone = $parent;
145                 last if $zone eq "" || $zone eq '.';
146         };
147
148         return join(', ', @result);
149 };
150
151 sub usage {
152         my $fd = shift;
153         my $exit = shift;
154
155         print $fd "Usage: $PROGRAM_NAME [--dir <dir>] overview|check-dlv|check-ds|check-header zone [zone...]\n";
156         print $fd "       $PROGRAM_NAME --dir <dir> overview|check-dlv|check-ds|check-header\n";
157         print $fd "       $PROGRAM_NAME --help\n";
158         exit $exit;
159 }
160
161 sub what_to_check {
162         my $zone = shift;
163         my $zonefile = shift;
164
165         my $do_dlv = 0;
166         my $do_ds = 0;
167
168         open(F, "<", $zonefile) or die ("Cannot open zonefile $zonefile for $zone: $!\n");
169         while (<F>) {
170                 if (/^[#;]\s*dlv-submit\s*=\s*yes\s*$/) { $do_dlv = 1; }
171                 if (/^[#;]\s*ds-in-parent\s*=\s*yes\s*$/) { $do_ds = 1; }
172         }
173         close(F);
174
175         return { 'dlv' => $do_dlv,
176                  'ds' => $do_ds };
177 }
178
179 Getopt::Long::config('bundling');
180 GetOptions (
181         '--help' => \$params->{'help'},
182         '--dir=s@' => \$params->{'dir'},
183         '--dlv=s' => \$params->{'dlv'},
184         '--verbose' => \$params->{'verbose'},
185 ) or usage(\*STDERR, 1);
186 usage(\*STDOUT, 0) if ($params->{'help'});
187
188 my $mode = shift @ARGV;
189 usage(\*STDOUT, 0) unless (defined $mode && $mode =~ /^(overview|check-dlv|check-ds|check-header)$/);
190 die ("check-header needs --dir") if ($mode eq 'check-header' && !defined $params->{'dir'});
191
192 my %zones;
193 if (scalar @ARGV) {
194         if (defined $params->{'dir'} && $mode ne 'check-header') {
195                 warn "--dir option ignored"
196         }
197         %zones = map { $_ => $_} @ARGV;
198 } else {
199         my $dirs = $params->{'dir'};
200         usage(\*STDOUT, 0) unless (defined $dirs);
201
202         for my $dir (@$dirs) {
203                 chdir $dir or die "chdir $dir failed? $!\n";
204                 opendir DIR, '.' or die ("Cannot opendir $dir\n");
205                 for my $file (readdir DIR) {
206                         next if ( -l "$file" );
207                         next unless ( -f "$file" );
208                         next if $file =~ /^(dsset|keyset)-/;
209
210                         my $zone = $file;
211                         if ($file =~ /\.zone$/) { # it's one of our yaml things
212                                 $zone = basename($file, '.zone');
213                         };
214                         $zones{$zone} = "$dir/$file";
215                 }
216                 closedir(DIR);
217         };
218 };
219
220 $DLV = $params->{'dlv'} if $params->{'dlv'};
221
222
223 if ($mode eq 'overview') {
224         my %data;
225         for my $zone (keys %zones) {
226                 $data{$zone} = { 'dnskey' => join(', ', get_dnskeytags($zone)),
227                                  'ds'     => join(', ', get_dstags($zone)),
228                                  'dlv'    => join(', ', get_dlvtags($zone)),
229                                  'parent_dnssec' => get_parent_dnssec_status($zone) };
230         }
231
232         my $format = "%60s %-15s %-15s %-3s %-10s\n";
233         printf $format, "zone", "DNSKEY", "DS\@parent", "DLV", "dnssec\@parent";
234         printf $format, "-"x 60,  "-"x 15,  "-"x 15,  "-"x 3, "-"x 10;
235         for my $zone (sort {$a cmp $b} keys %data) {
236                 printf $format, $zone,
237                         $data{$zone}->{'dnskey'},
238                         $data{$zone}->{'ds'},
239                         $data{$zone}->{'dlv'},
240                         $data{$zone}->{'parent_dnssec'};
241         }
242         exit(0);
243 } elsif ($mode eq 'check-dlv' || $mode eq 'check-ds' || $mode eq 'check-header') {
244         my @to_check;
245         push @to_check, 'dlv' if $mode eq 'check-header' ||  $mode eq 'check-dlv';
246         push @to_check, 'ds'  if $mode eq 'check-header' ||  $mode eq 'check-ds';
247
248         my @warn;
249         my @ok;
250         for my $zone (sort {$a cmp $b} keys %zones) {
251                 my $require = { map { $_ => 1 } @to_check };
252                 if ($mode eq 'check-header') {
253                         $require = what_to_check($zone, $zones{$zone})
254                 }
255
256                 my @dnskey = get_dnskeytags($zone);
257                 my $dnskey = join(",", @dnskey) || '-';
258                 for my $thiskey (@to_check) {
259                         my @target = $thiskey eq 'ds' ? get_dstags($zone) : get_dlvtags($zone);
260                         my $target = join(",", @target) || '-';
261
262                         my @isect = intersect(@dnskey, @target);
263                         if (scalar @isect == 0) {
264                                 if ($require->{$thiskey} || scalar @target > 0) {
265                                         push @warn, "$zone ([$dnskey] ~ [$target])";
266                                 }
267                         } else  {
268                                 if ($require->{$thiskey}) {
269                                         my $spec;
270                                         if (!array_diff(@dnskey, @target)) {
271                                                 $spec = $dnskey;
272                                         } else {
273                                                 my @elems = intersect(@dnskey, @target);
274                                                 push @elems, map { '-'.$_ } array_minus(@target, @dnskey);
275                                                 push @elems, map { '+'.$_ } array_minus(@dnskey, @target);
276                                                 $spec = join ',', @elems;
277                                         }
278                                         push @ok, "$zone ($spec)";
279                                 }
280                         };
281                 }
282         }
283         print "WARNING: ", join(", ", @warn), "\n" if (scalar @warn);
284         print "OK: ", join(", ", @ok), "\n" if (scalar @ok);
285         exit (1) if (scalar @warn);
286         exit (0);
287 } else {
288         die ("Invalid mode '$mode'\n");
289 };
290