Add algorithm number in overview
[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         my %options = @_;
61
62         my @result;
63         my @zsks;
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));
71
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)) {
75                         push @zsks, $tag;
76                         next;
77                 }
78
79                 push @result, $tag;
80         };
81         if ($type eq 'DNSKEY' && (scalar @result) == 0) {
82                 # use remaining keys if no keys with the SEP bit are present
83                 @result = @zsks;
84         }
85         my %unique = ();
86         @result = sort {$a cmp $b} grep {!$unique{$_}++} @result;
87         return @result
88 };
89
90 sub get_dnskeytags {
91         my $zone = shift;
92         my %options = @_;
93         return get_tag_generic($zone, 'DNSKEY', %options);
94 };
95 sub get_dstags {
96         my $zone = shift;
97         my %options = @_;
98         return get_tag_generic($zone, 'DS', %options);
99 };
100 sub get_dlvtags {
101         my $zone = shift;
102         my %options = @_;
103         $zone .= ".".$DLV;
104         return get_tag_generic($zone, 'DLV', %options);
105 };
106 sub has_dnskey_parent {
107         my $zone = shift;
108
109         my $potential_parent;
110         if ($zone =~ m/\./) {
111                 $potential_parent = $zone;
112                 $potential_parent =~ s/^[^.]+\.//;
113         } else {
114                 $potential_parent = '.';
115         }
116
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;
121
122         unless ($pkt->answer) {
123                 return undef unless $pkt->authority;
124                 for my $rr ($pkt->authority) {
125                         next unless ($rr->type eq 'SOA');
126
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;
131                         last;
132                 };
133         };
134
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);
139         };
140 }
141 sub get_parent_dnssec_status {
142         my $zone = shift;
143         my @result;
144
145         while (1) {
146                 my ($status, $parent) = has_dnskey_parent($zone);
147                 last unless defined $status;
148                 push @result, ($status ? "yes" : "no") . ("($parent)");
149                 $zone = $parent;
150                 last if $zone eq "" || $zone eq '.';
151         };
152
153         return join(', ', @result);
154 };
155
156 sub usage {
157         my $fd = shift;
158         my $exit = shift;
159
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";
163         exit $exit;
164 }
165
166 sub what_to_check {
167         my $zone = shift;
168         my $zonefile = shift;
169
170         my $do_dlv = 0;
171         my $do_ds = 0;
172
173         open(F, "<", $zonefile) or die ("Cannot open zonefile $zonefile for $zone: $!\n");
174         while (<F>) {
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; }
177         }
178         close(F);
179
180         return { 'dlv' => $do_dlv,
181                  'ds' => $do_ds };
182 }
183 sub diff_spec {
184         my $a = shift;
185         my $b = shift;
186
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);
191 }
192
193 Getopt::Long::config('bundling');
194 GetOptions (
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'});
201
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'});
205
206 my %zones;
207 if (scalar @ARGV) {
208         if (defined $params->{'dir'} && $mode ne 'check-header') {
209                 warn "--dir option ignored"
210         }
211         %zones = map { $_ => $_} @ARGV;
212 } else {
213         my $dirs = $params->{'dir'};
214         usage(\*STDOUT, 0) unless (defined $dirs);
215
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)-/;
223
224                         my $zone = $file;
225                         if ($file =~ /\.zone$/) { # it's one of our yaml things
226                                 $zone = basename($file, '.zone');
227                         };
228                         $zones{$zone} = "$dir/$file";
229                 }
230                 closedir(DIR);
231         };
232 };
233
234 $DLV = $params->{'dlv'} if $params->{'dlv'};
235
236
237 if ($mode eq 'overview') {
238         my %data;
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) };
244         }
245
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'};
255         }
256         exit(0);
257 } elsif ($mode eq 'check-dlv' || $mode eq 'check-ds' || $mode eq 'check-header') {
258         my @to_check;
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';
261
262         my @warn;
263         my @ok;
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})
268                 }
269
270                 my @dnskey = get_dnskeytags($zone);
271                 for my $thiskey (@to_check) {
272                         my @target = $thiskey eq 'ds' ? get_dstags($zone) : get_dlvtags($zone);
273
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)";
280                                 }
281                         } else  {
282                                 if ($require->{$thiskey}) {
283                                         push @ok, "$zone ($spec)";
284                                 }
285                         };
286                 }
287         }
288         print "WARNING: ", join(", ", @warn), "\n" if (scalar @warn);
289         print "OK: ", join(", ", @ok), "\n" if (scalar @ok);
290         exit (1) if (scalar @warn);
291         exit (0);
292 } else {
293         die ("Invalid mode '$mode'\n");
294 };
295