retire da-backup checks
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / dsa-check-dnssec-delegation
1 #!/usr/bin/perl
2
3 # Copyright (c) 2010, 2014, 2015, 2017 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
74                 if ($type eq 'DNSKEY' && ($rr->{'flags'} & (1<<(15-8)))) {
75                         # key is revoked
76                         next;
77                 }
78
79                 # for now only handle KSKs, i.e. keys with the SEP flag set
80                 if ($type eq 'DNSKEY' && !($rr->sep)) {
81                         push @zsks, $tag;
82                         next;
83                 }
84
85                 push @result, $tag;
86         };
87         if ($type eq 'DNSKEY' && (scalar @result) == 0) {
88                 # use remaining keys if no keys with the SEP bit are present
89                 @result = @zsks;
90         }
91         my %unique = ();
92         @result = sort {$a cmp $b} grep {!$unique{$_}++} @result;
93         return @result
94 };
95
96 sub get_dnskeytags {
97         my $zone = shift;
98         my %options = @_;
99         return get_tag_generic($zone, 'DNSKEY', %options);
100 };
101 sub get_dstags {
102         my $zone = shift;
103         my %options = @_;
104         return get_tag_generic($zone, 'DS', %options);
105 };
106 sub get_dlvtags {
107         my $zone = shift;
108         my %options = @_;
109         $zone .= ".".$DLV;
110         return get_tag_generic($zone, 'DLV', %options);
111 };
112 sub has_dnskey_parent {
113         my $zone = shift;
114
115         my $potential_parent;
116         if ($zone =~ m/\./) {
117                 $potential_parent = $zone;
118                 $potential_parent =~ s/^[^.]+\.//;
119         } else {
120                 $potential_parent = '.';
121         }
122
123         print "Querying DNSKEY $potential_parent\n" if $params->{'verbose'};
124         my $pkt = $RES->send($potential_parent, 'DNSKEY');
125         return undef unless $pkt;
126         return undef unless $pkt->header;
127
128         unless ($pkt->answer) {
129                 return undef unless $pkt->authority;
130                 for my $rr ($pkt->authority) {
131                         next unless ($rr->type eq 'SOA');
132
133                         $potential_parent = $rr->name;
134                         print "Querying DNSKEY $potential_parent\n" if $params->{'verbose'};
135                         $pkt = $RES->send($potential_parent, 'DNSKEY');
136                         return undef unless $pkt;
137                         last;
138                 };
139         };
140
141         return (0, $potential_parent) unless $pkt->answer;
142         for my $rr ($pkt->answer) {
143                 next unless ($rr->type eq 'DNSKEY');
144                 return (1, $potential_parent);
145         };
146 }
147 sub get_parent_dnssec_status {
148         my $zone = shift;
149         my @result;
150
151         while (1) {
152                 my ($status, $parent) = has_dnskey_parent($zone);
153                 last unless defined $status;
154                 push @result, ($status ? "yes" : "no") . ("($parent)");
155                 $zone = $parent;
156                 last if $zone eq "" || $zone eq '.';
157         };
158
159         return join(', ', @result);
160 };
161
162 sub usage {
163         my $fd = shift;
164         my $exit = shift;
165
166         print $fd "Usage: $PROGRAM_NAME [--dir <dir>] overview|check-dlv|check-ds|check-header zone [zone...]\n";
167         print $fd "       $PROGRAM_NAME --dir <dir> overview|check-dlv|check-ds|check-header\n";
168         print $fd "       $PROGRAM_NAME --help\n";
169         exit $exit;
170 }
171
172 sub what_to_check {
173         my $zone = shift;
174         my $zonefile = shift;
175
176         my $do_dlv = 0;
177         my $do_ds = 0;
178
179         open(F, "<", $zonefile) or die ("Cannot open zonefile $zonefile for $zone: $!\n");
180         while (<F>) {
181                 if (/^[#;]\s*dlv-submit\s*=\s*yes\s*$/) { $do_dlv = 1; }
182                 if (/^[#;]\s*ds-in-parent\s*=\s*yes\s*$/) { $do_ds = 1; }
183         }
184         close(F);
185
186         return { 'dlv' => $do_dlv,
187                  'ds' => $do_ds };
188 }
189 sub diff_spec {
190         my $a = shift;
191         my $b = shift;
192
193         my @elems = intersect(@$a, @$b);
194         push @elems, map { '-'.$_ } array_minus(@$a, @$b);
195         push @elems, map { '+'.$_ } array_minus(@$b, @$a);
196         return join(',', @elems);
197 }
198
199 Getopt::Long::config('bundling');
200 GetOptions (
201         '--help' => \$params->{'help'},
202         '--dir=s@' => \$params->{'dir'},
203         '--dlv=s' => \$params->{'dlv'},
204         '--verbose' => \$params->{'verbose'},
205 ) or usage(\*STDERR, 1);
206 usage(\*STDOUT, 0) if ($params->{'help'});
207
208 my $mode = shift @ARGV;
209 usage(\*STDOUT, 0) unless (defined $mode && $mode =~ /^(overview|check-dlv|check-ds|check-header)$/);
210 die ("check-header needs --dir") if ($mode eq 'check-header' && !defined $params->{'dir'});
211
212 my %zones;
213 if (scalar @ARGV) {
214         if (defined $params->{'dir'} && $mode ne 'check-header') {
215                 warn "--dir option ignored"
216         }
217         %zones = map { $_ => $_} @ARGV;
218 } else {
219         my $dirs = $params->{'dir'};
220         usage(\*STDOUT, 0) unless (defined $dirs);
221
222         for my $dir (@$dirs) {
223                 chdir $dir or die "chdir $dir failed? $!\n";
224                 opendir DIR, '.' or die ("Cannot opendir $dir\n");
225                 for my $file (readdir DIR) {
226                         next if ( -l "$file" );
227                         next unless ( -f "$file" );
228                         next if $file =~ /^(dsset|keyset)-/;
229
230                         my $zone = $file;
231                         if ($file =~ /\.zone$/) { # it's one of our yaml things
232                                 $zone = basename($file, '.zone');
233                         };
234                         $zones{$zone} = "$dir/$file";
235                 }
236                 closedir(DIR);
237         };
238 };
239
240 $DLV = $params->{'dlv'} if $params->{'dlv'};
241
242
243 if ($mode eq 'overview') {
244         my %data;
245         for my $zone (keys %zones) {
246                 $data{$zone} = { 'dnskey' => join(', ', get_dnskeytags($zone, pretty=>1)),
247                                  'ds'     => join(', ', get_dstags($zone, pretty=>1)),
248                                  'dlv'    => join(', ', get_dlvtags($zone, pretty=>1)),
249                                  'parent_dnssec' => get_parent_dnssec_status($zone) };
250         }
251
252         my $format = "%60s %-20s %-15s %-3s %-10s\n";
253         printf $format, "zone", "DNSKEY", "DS\@parent", "DLV", "dnssec\@parent";
254         printf $format, "-"x 60,  "-"x 20,  "-"x 15,  "-"x 3, "-"x 10;
255         for my $zone (sort {$a cmp $b} keys %data) {
256                 printf $format, $zone,
257                         $data{$zone}->{'dnskey'},
258                         $data{$zone}->{'ds'},
259                         $data{$zone}->{'dlv'},
260                         $data{$zone}->{'parent_dnssec'};
261         }
262         exit(0);
263 } elsif ($mode eq 'check-dlv' || $mode eq 'check-ds' || $mode eq 'check-header') {
264         my @to_check;
265         push @to_check, 'dlv' if $mode eq 'check-header' ||  $mode eq 'check-dlv';
266         push @to_check, 'ds'  if $mode eq 'check-header' ||  $mode eq 'check-ds';
267
268         my @warn;
269         my @ok;
270         for my $zone (sort {$a cmp $b} keys %zones) {
271                 my $require = { map { $_ => 1 } @to_check };
272                 if ($mode eq 'check-header') {
273                         $require = what_to_check($zone, $zones{$zone})
274                 }
275
276                 my @dnskey = get_dnskeytags($zone);
277                 for my $thiskey (@to_check) {
278                         my @target = $thiskey eq 'ds' ? get_dstags($zone) : get_dlvtags($zone);
279
280                         my $spec = diff_spec(\@target, \@dnskey);
281                         # if the intersection between DS and KEY is empty,
282                         # or if there are DS records for keys we do not have, that's an issue.
283                         if (intersect(@dnskey, @target) == 0 || array_minus(@target, @dnskey)) {
284                                 if ($require->{$thiskey} || scalar @target > 0) {
285                                         push @warn, "$zone ($spec)";
286                                 }
287                         } else  {
288                                 if ($require->{$thiskey}) {
289                                         push @ok, "$zone ($spec)";
290                                 }
291                         };
292                 }
293         }
294         print "WARNING: ", join(", ", @warn), "\n" if (scalar @warn);
295         print "OK: ", join(", ", @ok), "\n" if (scalar @ok);
296         exit (1) if (scalar @warn);
297         exit (0);
298 } else {
299         die ("Invalid mode '$mode'\n");
300 };
301