Format update for dsa-check-dnssec-delegation 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 $SIG{'__DIE__'} = sub { print @_; exit 4; };
32
33 my $RES = Net::DNS::Resolver->new;
34 my $DLV = 'dlv.isc.org';
35 my $params;
36
37 sub get_tag_generic {
38         my $zone = shift;
39         my $type = shift;
40
41         my @result;
42         my @zsks;
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));
50
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;
54                         next;
55                 }
56
57                 push @result, $rr->keytag;
58         };
59         if ($type eq 'DNSKEY' && (scalar @result) == 0) {
60                 # use remaining keys if no keys with the SEP bit are present
61                 @result = @zsks;
62         }
63         my %unique = ();
64         @result = sort {$a <=> $b} grep {!$unique{$_}++} @result;
65         return @result
66 };
67
68 sub get_dnskeytags {
69         my $zone = shift;
70         return get_tag_generic($zone, 'DNSKEY');
71 };
72 sub get_dstags {
73         my $zone = shift;
74         return get_tag_generic($zone, 'DS');
75 };
76 sub get_dlvtags {
77         my $zone = shift;
78         $zone .= ".".$DLV;
79         return get_tag_generic($zone, 'DLV');
80 };
81 sub has_dnskey_parent {
82         my $zone = shift;
83
84         my $potential_parent;
85         if ($zone =~ m/\./) {
86                 $potential_parent = $zone;
87                 $potential_parent =~ s/^[^.]+\.//;
88         } else {
89                 $potential_parent = '.';
90         }
91
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;
96
97         unless ($pkt->answer) {
98                 return undef unless $pkt->authority;
99                 for my $rr ($pkt->authority) {
100                         next unless ($rr->type eq 'SOA');
101
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;
106                         last;
107                 };
108         };
109
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);
114         };
115 }
116 sub get_parent_dnssec_status {
117         my $zone = shift;
118         my @result;
119
120         while (1) {
121                 my ($status, $parent) = has_dnskey_parent($zone);
122                 last unless defined $status;
123                 push @result, ($status ? "yes" : "no") . ("($parent)");
124                 $zone = $parent;
125                 last if $zone eq "" || $zone eq '.';
126         };
127
128         return join(', ', @result);
129 };
130
131 sub usage {
132         my $fd = shift;
133         my $exit = shift;
134
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";
138         exit $exit;
139 }
140
141 sub what_to_check {
142         my $zone = shift;
143         my $zonefile = shift;
144
145         my $do_dlv = 0;
146         my $do_ds = 0;
147
148         open(F, "<", $zonefile) or die ("Cannot open zonefile $zonefile for $zone: $!\n");
149         while (<F>) {
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; }
152         }
153         close(F);
154
155         return { 'dlv' => $do_dlv,
156                  'ds' => $do_ds };
157 }
158
159 Getopt::Long::config('bundling');
160 GetOptions (
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'});
167
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'});
171
172 my %zones;
173 if (scalar @ARGV) {
174         if (defined $params->{'dir'} && $mode ne 'check-header') {
175                 warn "--dir option ignored"
176         }
177         %zones = map { $_ => $_} @ARGV;
178 } else {
179         my $dirs = $params->{'dir'};
180         usage(\*STDOUT, 0) unless (defined $dirs);
181
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)-/;
189
190                         my $zone = $file;
191                         if ($file =~ /\.zone$/) { # it's one of our yaml things
192                                 $zone = basename($file, '.zone');
193                         };
194                         $zones{$zone} = "$dir/$file";
195                 }
196                 closedir(DIR);
197         };
198 };
199
200 $DLV = $params->{'dlv'} if $params->{'dlv'};
201
202
203 if ($mode eq 'overview') {
204         my %data;
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) };
210         }
211
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'};
221         }
222         exit(0);
223 } elsif ($mode eq 'check-dlv' || $mode eq 'check-ds' || $mode eq 'check-header') {
224         my @to_check;
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';
227
228         my @warn;
229         my @ok;
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})
234                 }
235
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)) || '-';
239
240                         if ($dnskey ne $target) {
241                                 if ($require->{$thiskey} || $target ne '-') {
242                                         push @warn, "$zone ([$dnskey] != [$target])";
243                                 }
244                         } else  {
245                                 if ($require->{$thiskey}) {
246                                         push @ok, "$zone ($dnskey)";
247                                 }
248                         };
249                 }
250         }
251         print "WARNING: ", join(", ", @warn), "\n" if (scalar @warn);
252         print "OK: ", join(", ", @ok), "\n" if (scalar @ok);
253         exit (1) if (scalar @warn);
254         exit (0);
255 } else {
256         die ("Invalid mode '$mode'\n");
257 };
258