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