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