Print all parents 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
30 $SIG{'__DIE__'} = sub { print @_; exit 4; };
31
32 my $RES = Net::DNS::Resolver->new;
33 my $DLV = 'dlv.isc.org';
34
35 sub get_tag_generic {
36         my $zone = shift;
37         my $type = shift;
38
39         my @result;
40         my $pkt = $RES->send($zone, $type);
41         return () unless $pkt;
42         return () unless $pkt->answer;
43         for my $rr ($pkt->answer) {
44                 next unless ($rr->type eq $type);
45                 next unless (lc($rr->name) eq lc($zone));
46
47                 # only handle KSKs, i.e. keys with the SEP flag set
48                 next if ($type eq 'DNSKEY' && !($rr->is_sep));
49
50                 push @result, $rr->keytag;
51         };
52         my %unique = ();
53         @result = sort {$a <=> $b} grep {!$unique{$_}++} @result;
54         return @result
55 };
56
57 sub get_dnskeytags {
58         my $zone = shift;
59         return get_tag_generic($zone, 'DNSKEY');
60 };
61 sub get_dstags {
62         my $zone = shift;
63         return get_tag_generic($zone, 'DS');
64 };
65 sub get_dlvtags {
66         my $zone = shift;
67         $zone .= ".".$DLV;
68         return get_tag_generic($zone, 'DLV');
69 };
70 sub has_dnskey_parent {
71         my $zone = shift;
72
73         my $potential_parent;
74         if ($zone =~ m/\./) {
75                 $potential_parent = $zone;
76                 $potential_parent =~ s/^[^.]+\.//;
77         } else {
78                 $potential_parent = '.';
79         }
80
81         my $pkt = $RES->send($potential_parent, 'DNSKEY');
82         return undef unless $pkt;
83         return undef unless $pkt->header;
84
85         # try to find the zone start
86         unless ($pkt->answer) {
87                 #print "Looking for zone apex\n";
88                 return undef unless $pkt->authority;
89                 for my $rr ($pkt->authority) {
90                         next unless ($rr->type eq 'SOA');
91
92                         $potential_parent = $rr->name;
93                         #print "Found it at $potential_parent\n";
94                         $pkt = $RES->send($potential_parent, 'DNSKEY');
95                         return undef unless $pkt;
96                         last;
97                 };
98         };
99
100         return (0, $potential_parent) unless $pkt->answer;
101         for my $rr ($pkt->answer) {
102                 next unless ($rr->type eq 'DNSKEY');
103                 return (1, $potential_parent);
104         };
105 }
106 sub get_parent_dnssec_status {
107         my $zone = shift;
108         my @result;
109
110         while (1) {
111                 my ($status, $parent) = has_dnskey_parent($zone);
112                 last unless defined $status;
113                 push @result, ($status ? "yes" : "no") . ("($parent)");
114                 $zone = $parent;
115                 last if $zone eq "";
116         };
117
118         return join(', ', @result);
119 };
120
121 sub usage {
122         my $fd = shift;
123         my $exit = shift;
124
125         print $fd "Usage: $PROGRAM_NAME [--dir <dir>] overview|check-dlv|check-ds|check-header zone [zone...]\n";
126         print $fd "       $PROGRAM_NAME --dir <dir> overview|check-dlv|check-ds|check-header\n";
127         print $fd "       $PROGRAM_NAME --help\n";
128         exit $exit;
129 }
130
131 sub what_to_check {
132         my $zone = shift;
133         my $indir = shift;
134
135         my $do_dlv = 0;
136         my $do_ds = 0;
137
138         open(F, "<", $indir."/".$zone) or die ("Cannot open zonefile for $zone: $!\n");
139         while (<F>) {
140                 if (/^;\s*dlv-submit\s*=\s*yes\s*$/) { $do_dlv = 1; }
141                 if (/^;\s*ds-in-parent\s*=\s*yes\s*$/) { $do_ds = 1; }
142         }
143         close(F);
144
145         my @keys = ();
146         push @keys, 'dlv' if $do_dlv;
147         push @keys, 'ds' if $do_ds;
148         return @keys;
149 }
150
151 my $params;
152 Getopt::Long::config('bundling');
153 GetOptions (
154         '--help' => \$params->{'help'},
155         '--dir=s' => \$params->{'dir'},
156         '--dlv=s' => \$params->{'dlv'},
157 ) or usage(\*STDERR, 1);
158 usage(\*STDOUT, 0) if ($params->{'help'});
159
160 my $mode = shift @ARGV;
161 usage(\*STDOUT, 0) unless (defined $mode && $mode =~ /^(overview|check-dlv|check-ds|check-header)$/);
162 die ("check-header needs --dir") if ($mode eq 'check-header' && !defined $params->{'dir'});
163
164 my @zones;
165 if (scalar @ARGV) {
166         if (defined $params->{'dir'} && $mode ne 'check-header') {
167                 warn "--dir option ignored"
168         }
169         @zones = @ARGV;
170 } else {
171         my $dir = $params->{'dir'};
172         usage(\*STDOUT, 0) unless (defined $dir);
173
174         chdir $dir or die "chdir $dir failed? $!\n";
175         opendir DIR, '.' or die ("Cannot opendir $dir\n");
176         for my $file (readdir DIR) {
177                 next if ( -l "$file" );
178                 next unless ( -f "$file" );
179                 next if $file =~ /^(dsset|keyset)-/;
180
181                 push @zones, $file;
182         }
183         closedir(DIR);
184 };
185
186 $DLV = $params->{'dlv'} if $params->{'dlv'};
187
188 my %data;
189 for my $zone (@zones) {
190         $data{$zone} = { 'dnskey' => join(', ', get_dnskeytags($zone)),
191                          'ds'     => join(', ', get_dstags($zone)),
192                          'dlv'    => join(', ', get_dlvtags($zone)),
193                          'parent_dnssec' => get_parent_dnssec_status($zone) };
194 }
195
196 if ($mode eq 'overview') {
197         my $format = "%60s %-10s %-10s %-10s %-10s\n";
198         printf $format, "zone", "DNSKEY", "DS\@parent", "DLV", "dnssec\@parent";
199         printf $format, "-"x 60,  "-"x 10,  "-"x 10,  "-"x 10, "-"x 10;
200         for my $zone (sort {$a cmp $b} keys %data) {
201                 printf $format, $zone,
202                         $data{$zone}->{'dnskey'},
203                         $data{$zone}->{'ds'},
204                         $data{$zone}->{'dlv'},
205                         $data{$zone}->{'parent_dnssec'};
206         }
207         exit(0);
208 } elsif ($mode eq 'check-dlv' || $mode eq 'check-ds' || $mode eq 'check-header') {
209         my $key;
210         $key = 'dlv' if $mode eq 'check-dlv';
211         $key = 'ds' if $mode eq 'check-ds';
212         $key = 'per-zone' if $mode eq 'check-header';
213         die ("key undefined") unless $key;
214
215         my @warn;
216         my @ok;
217         for my $zone (sort {$a cmp $b} keys %data) {
218                 my @thiskeys = $key eq 'per-zone' ? what_to_check($zone, $params->{'dir'}) : ($key);
219
220                 for my $thiskey (@thiskeys) {
221                         my $dnskey = $data{$zone}->{'dnskey'} || '-';
222                         my $target = $data{$zone}->{$thiskey} || '-';
223
224                         if ($dnskey ne $target) {
225                                 push @warn, "$zone ($dnskey != $target)";
226                         } else  {
227                                 push @ok, "$zone ($dnskey)";
228                         };
229                 }
230         }
231         print "WARNING: ", join(", ", @warn), "\n" if (scalar @warn);
232         print "OK: ", join(", ", @ok), "\n" if (scalar @ok);
233         exit (1) if (scalar @warn);
234         exit (0);
235 } else {
236         die ("Invalid mode '$mode'\n");
237 };
238