blib/lib/Monitoring/Sneck/Checks/https_cert.pm

filter: Statementstmt 98.0% 99 / 101 Branchbran 81.4% 44 / 54 Conditioncond 66.6% 18 / 27 MC/DCmcdc 35.2% 6 / 17 Subroutinesub 100% 15 / 15 Podpod 100% 6 / 6 totaltotal 85.4% 188 / 220 CC 38 SCAR 24.4
CC
38
Cov
88%
starttls
17.3
check
14.7
validate
9.3
CRAP 11.4
« blib/lib/Monitoring/Sneck/Checks/hosts.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/swraid.pm »
1 package Monitoring::Sneck::Checks::https_cert;
2  
3 4 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Monitoring::Sneck::Checks::https_cert - checks the certificate a TLS server hands out
11  
12 =head1 VERSION
13  
14 Version 0.0.1
15  
16 =cut
17  
18 4 our $VERSION = '0.0.1';
19  
20 # The port for each protocol --starttls takes, used if --port is not given.
21 4 our %STARTTLS_PORTS = ( smtp => 25, imap => 143, pop3 => 110, ftp => 21, ldap => 389 );
22  
23 # The LDAP StartTLS extended request, message ID 1, as BER.
24 4 our $LDAP_STARTTLS = pack( 'H*', '301d02010177188016' ) . '1.3.6.1.4.1.1466.20037';
25  
26 =head1 SYNOPSIS
27  
28 www_cert|perl:https_cert -H www.example.org
29 ldaps_cert|perl:https_cert -H ldap.example.org -p 636 -w 45 -c 20
30 vhost_cert|perl:https_cert -H 192.0.2.10 -n shop.example.org
31 internal_cert|perl:https_cert -H intranet.example.org --ca-file /usr/local/etc/ssl/internal-ca.pem
32 mail_cert|perl:https_cert -H mail.example.org -s smtp
33 imap_cert|perl:https_cert -H mail.example.org -s imap -w 45
34  
35 =head1 DESCRIPTION
36  
37 Connects through L<IO::Socket::SSL> and checks the certificate the
38 server hands out. It must be signed by a trusted CA and cover the name,
39 unless --insecure is given, else it is CRITICAL, as is expired or not
40 good yet. Then it is WARNING or CRITICAL by the days left.
41  
42 Any TLS port works, not only 443. With --starttls, it connects in the
43 clear and asks for TLS the way that protocol does, which for SMTP needs
44 STARTTLS in the EHLO reply. Not being able to is CRITICAL.
45  
46 =head1 OPTIONS
47  
48 - -H, --hostname :: The host to connect to. Required.
49  
50 - -p, --port :: The port.
51 Default :: 443, or for --starttls, the protocol's
52  
53 - -s, --starttls :: Ask for TLS through smtp, imap, pop3, ftp, or ldap.
54  
55 - --helo-name :: The name to give in EHLO, for --starttls smtp.
56 Default :: the host name of this host
57  
58 - -n, --sni :: The name to ask for and check the certificate covers.
59 Default :: --hostname
60  
61 - -w, --warning :: Days left at or under which it is WARNING.
62 Default :: 30
63  
64 - -c, --critical :: Days left at or under which it is CRITICAL.
65 Default :: 14
66  
67 - --insecure :: Only check when it expires, not who signed it or the
68 name.
69  
70 - --ca-file :: The CA file to check it against.
71  
72 =cut
73  
74 21 ▶sub option_spec {
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
75 return (
76 21 'hostname|H=s', 'port|p=i', 'sni|n=s', 'starttls|s=s', 'helo-name=s',
77 'warning|w=i', 'critical|c=i', 'insecure', 'ca-file=s'
78 );
79 }
80  
81 21 ▶sub defaults { return { warning => 30, critical => 14 } }
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
82  
83 21 ▶sub required { return ('hostname') }
Subroutine required: called CC=1 SCAR=0.0
Pod documented
84  
85 21 ▶sub validate {
Subroutine validate: called CC=9 SCAR=22.3
Pod documented
86 21 my ( $self, $options ) = @_;
87  
88 21 my @problems;
89 21 foreach my $name ( 'hostname', 'sni', 'helo-name' ) {
90 push( @problems, '--' . $name . ' "' . $options->{$name} . '" is not a IP or hostname' )
91 63 ▶ if defined( $options->{$name} ) && !$self->is_host( $options->{$name} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{$name} and not $self->is_host($options->{$name}) 1 62 63
Condition100%3/3
defined $options->{$name} and not $self->is_host($options->{$name})
!ll&&!rl&&r
37251
MC/DC100%2/2
defined $options->{$name} and not $self->is_host($options->{$name})
defined $options->{$name} not $self->is_host($options->{$name})
Truth table100%3/3
A and not(B)
A= defined $options->{$name}B= not $self->is_host($options->{$name})
ABresult
0X0
100
111
92 }
93 push( @problems, '--port must be 1 to 65535' )
94 21 ▶ if defined( $options->{port} ) && ( $options->{port} < 1 || $options->{port} > 65535 );
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"port"} and $options->{"port"} < 1 || $options->{"port"} > 65535 0 21 21
Condition50%3/6
defined $options->{"port"} and $options->{"port"} < 1 || $options->{"port"} > 65535
!ll&&!rl&&r
2190
$options->{"port"} < 1 or $options->{"port"} > 65535
l!l&&r!l&&!r
0019
MC/DC0%0/3
defined $options->{"port"} and $options->{"port"} < 1 || $options->{"port"} > 65535
defined $options->{"port"} $options->{"port"} < 1 $options->{"port"} > 65535
Truth table50%2/4
A and B or C
A= defined $options->{"port"}B= $options->{"port"} < 1C= $options->{"port"} > 65535
ABCresult
0XX0
1000
11X1
1011
95 push( @problems, '--starttls must be ' . join( ', ', sort( keys(%STARTTLS_PORTS) ) ) )
96 21 ▶ if defined( $options->{starttls} ) && !$STARTTLS_PORTS{ $options->{starttls} };
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"starttls"} and not $Monitoring::Sneck::Checks::https_cert::STARTTLS_PORTS{$options->{"starttls"}} 1 20 21
Condition100%3/3
defined $options->{"starttls"} and not $Monitoring::Sneck::Checks::https_cert::STARTTLS_PORTS{$options->{"starttls"}}
!ll&&!rl&&r
8121
MC/DC100%2/2
defined $options->{"starttls"} and not $Monitoring::Sneck::Checks::https_cert::STARTTLS_PORTS{$options->{"starttls"}}
defined $options->{"starttls"} not $Monitoring::Sneck::Checks::https_cert::STARTTLS_PORTS{$options->{"starttls"}}
Truth table100%3/3
A and not(B)
A= defined $options->{"starttls"}B= not $Monitoring::Sneck::Checks::https_cert::STARTTLS_PORTS{$options->{"starttls"}}
ABresult
0X0
100
111
97  
98 21 return @problems;
99 } ## end sub validate
100  
101 =head1 METHODS
102  
103 =head2 check
104  
105 Does the check.
106  
107 =cut
108  
109 19 ▶sub check {
Subroutine check: called CC=14 SCAR=26.9
Pod documented
110 19 my ($self) = @_;
111  
112 19 require IO::Socket::SSL;
113 19 require Data::Validate::IP;
114 19 my $starttls = $self->opt('starttls');
115 19 ▶ my $port = $self->opt('port') // ( defined($starttls) ? $STARTTLS_PORTS{$starttls} : 443 );
Branch: 1 branch
BranchTrueFalseTotal
defined $starttls ? : 0 0 0
Condition33%1/3
$self->opt("port") // (defined $starttls ? $Monitoring::Sneck::Checks::https_cert::STARTTLS_PORTS{$starttls} : 443)
l!l&&r!l&&!r
1900
MC/DC0%0/2
$self->opt("port") // (defined $starttls ? $Monitoring::Sneck::Checks::https_cert::STARTTLS_PORTS{$starttls} : 443)
$self->opt("port") (defined $starttls ? $Monitoring::Sneck::Checks::https_cert::STARTTLS_PORTS{$starttls} : 443)
Truth table33%1/3
A // B
A= $self->opt("port")B= (defined $starttls ? $Monitoring::Sneck::Checks::https_cert::STARTTLS_PORTS{$starttls} : 443)
ABresult
1X1
011
000
116 19 ▶ my $name = $self->opt('sni') // $self->opt('hostname');
Condition66%2/3
$self->opt("sni") // $self->opt("hostname")
l!l&&r!l&&!r
3160
MC/DC0%0/2
$self->opt("sni") // $self->opt("hostname")
$self->opt("sni") $self->opt("hostname")
Truth table66%2/3
A // B
A= $self->opt("sni")B= $self->opt("hostname")
ABresult
1X1
011
000
117 19 my $where = $self->opt('hostname') . ':' . $port;
118 19 ▶ my %ssl = (
Branch: 1 branch
BranchTrueFalseTotal
$self->opt('insecure') ? : 1 18 19
119 Timeout => $self->timeout,
120 SSL_verify_mode => $self->opt('insecure') ? 0 : 1,
121 );
122 19 ▶ $ssl{SSL_hostname} = Data::Validate::IP::is_ip($name) ? '' : $name;
Branch: 1 branch
BranchTrueFalseTotal
Data::Validate::IP::is_ip($name) ? : 16 3 19
123  
124 19 ▶ if ( !$self->opt('insecure') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->opt("insecure")) 18 1 19
125 18 $ssl{SSL_verifycn_name} = $name;
126 18 $ssl{SSL_verifycn_scheme} = 'http';
127 }
128 19 ▶ $ssl{SSL_ca_file} = $self->opt('ca-file') if defined( $self->opt('ca-file') );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->opt("ca-file") 10 9 19
129  
130 19 my $socket;
131 19 ▶ if ( defined($starttls) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $starttls) { } 11 8 19
132 11 require IO::Socket::IP;
133 11 my $plain = IO::Socket::IP->new(
134 PeerHost => $self->opt('hostname'),
135 PeerPort => $port,
136 Proto => 'tcp',
137 Timeout => $self->timeout,
138 );
139 11 ▶ if ( !defined($plain) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $plain) 1 10 11
140 1 ▶ return $self->finish( $self->CRITICAL, $where . ', ' . ( $@ || $! ) );
Condition33%1/3
$@ || $!
l!l&&r!l&&!r
100
MC/DC0%0/2
$@ || $!
$@ $!
Truth table33%1/3
A || B
A= $@B= $!
ABresult
1X1
011
000
141 }
142 10 $plain->autoflush(1);
143 10 my $problem = $self->starttls( $plain, $starttls );
144 10 ▶ if ( defined($problem) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $problem) 4 6 10
145 4 close($plain);
146 4 return $self->finish( $self->CRITICAL, $where . ' ' . $starttls . ' ' . $problem );
147 }
148 6 $socket = IO::Socket::SSL->start_SSL( $plain, %ssl );
149 } else {
150 8 $socket = IO::Socket::SSL->new( PeerHost => $self->opt('hostname'), PeerPort => $port, %ssl );
151 }
152 14 ▶ if ( !$socket ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($socket) 4 10 14
153 4 return $self->finish( $self->CRITICAL, $where . ', ' . IO::Socket::SSL::errstr() );
154 }
155 10 my $cert = $socket->peer_certificate;
156 10 ▶ if ( !$cert ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cert) 0 10 10
157 0 close($socket);
158 0 return $self->finish( $self->CRITICAL, $where . ' handed out no certificate' );
159 }
160 10 my $details = $self->cert_details($cert);
161 10 close($socket);
162  
163 10 my ( $code, $message ) = $self->cert_status( $details, $self->opt('warning'), $self->opt('critical') );
164 $self->perfdata(
165 label => 'days',
166 value => $details->{days_left},
167 10 warning => $self->opt('warning'),
168 critical => $self->opt('critical')
169 );
170  
171 10 return $self->finish( $code, $where . ' ' . $message . ', issued by "' . $details->{issuer} . '"' );
172 } ## end sub check
173  
174 =head2 starttls
175  
176 Asks for TLS on a socket connected in the clear.
177  
178 - socket :: The socket.
179  
180 - protocol :: smtp, imap, pop3, ftp, or ldap.
181  
182 Returns undef once the server said yes, else what went wrong.
183  
184 my $problem = $check->starttls( $socket, 'smtp' );
185  
186 =cut
187  
188 11 ▶sub starttls {
Subroutine starttls: called CC=17 SCAR=28.5
Pod documented
189 11 my ( $self, $socket, $protocol ) = @_;
190  
191 # a full reply, the last line of a multi line one having a space after the code
192 11 ▶ my $coded = sub { return $_[0] =~ /^\d{3} [^\n]*\n/m };
Subroutine __ANON__: called CC=1 SCAR=0.0
193 11 ▶ my $line = sub { return $_[0] =~ /\n/ };
Subroutine __ANON__: called CC=1 SCAR=0.0
194 11 ▶ my $first = sub { my ($reply) = @_; $reply =~ s/\r?\n.*\z//s; return '"' . $reply . '"' };
Subroutine __ANON__: called CC=1 SCAR=0.0
195  
196 11 ▶ if ( $protocol eq 'smtp' || $protocol eq 'ftp' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($protocol eq 'smtp' or $protocol eq 'ftp') { } 6 5 11
elsif ($protocol eq 'imap') { } 2 3 5
elsif ($protocol eq 'pop3') { } 1 2 3
Condition100%3/3
$protocol eq "smtp" or $protocol eq "ftp"
l!l&&r!l&&!r
515
MC/DC100%2/2
$protocol eq "smtp" or $protocol eq "ftp"
$protocol eq "smtp" $protocol eq "ftp"
Truth table100%3/3
A or B
A= $protocol eq "smtp"B= $protocol eq "ftp"
ABresult
1X1
011
000
197 6 my $greeting = $self->read_socket( $socket, $coded );
198 6 ▶ return 'greeted with ' . $first->($greeting) if $greeting !~ /^220 /m;
Branch: 1 branch
BranchTrueFalseTotal
unless $greeting =~ /^220 /mu 0 6 6
199 6 ▶ if ( $protocol eq 'smtp' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($protocol eq 'smtp') { } 5 1 6
200 5 require Sys::Hostname;
201 5 ▶ print $socket 'EHLO ' . ( $self->opt('helo-name') // Sys::Hostname::hostname() ) . "\r\n";
Condition66%2/3
$self->opt("helo-name") // Sys::Hostname::hostname()
l!l&&r!l&&!r
230
MC/DC0%0/2
$self->opt("helo-name") // Sys::Hostname::hostname()
$self->opt("helo-name") Sys::Hostname::hostname()
Truth table66%2/3
A // B
A= $self->opt("helo-name")B= Sys::Hostname::hostname()
ABresult
1X1
011
000
202 5 my $ehlo = $self->read_socket( $socket, $coded );
203 5 ▶ return 'EHLO got ' . $first->($ehlo) if $ehlo !~ /^250 /m;
Branch: 1 branch
BranchTrueFalseTotal
unless $ehlo =~ /^250 /mu 1 4 5
204 4 ▶ return 'does not offer STARTTLS' if $ehlo !~ /^250[ -]STARTTLS\s*$/mi;
Branch: 1 branch
BranchTrueFalseTotal
unless $ehlo =~ /^250[ -]STARTTLS\s*$/imu 1 3 4
205 3 print $socket "STARTTLS\r\n";
206 } else {
207 1 print $socket "AUTH TLS\r\n";
208 }
209 4 my $reply = $self->read_socket( $socket, $coded );
210 4 ▶ return 'refused TLS, ' . $first->($reply) if $reply !~ /^2(?:20|34) /m;
Branch: 1 branch
BranchTrueFalseTotal
unless $reply =~ /^2(?:20|34) /mu 0 4 4
211 } elsif ( $protocol eq 'imap' ) {
212 2 my $greeting = $self->read_socket( $socket, $line );
213 2 ▶ return 'greeted with ' . $first->($greeting) if $greeting !~ /^\* OK/;
Branch: 1 branch
BranchTrueFalseTotal
unless $greeting =~ /^\* OK/u 1 1 2
214 1 print $socket "a1 STARTTLS\r\n";
215 1 ▶ my $reply = $self->read_socket( $socket, sub { return $_[0] =~ /^a1 [^\n]*\n/m } );
Subroutine __ANON__: called CC=1 SCAR=0.0
216 1 ▶ return 'refused TLS, ' . $first->( $reply =~ s/\A.*?^(a1 )/$1/msr ) if $reply !~ /^a1 OK/m;
Branch: 1 branch
BranchTrueFalseTotal
unless $reply =~ /^a1 OK/mu 0 1 1
217 } elsif ( $protocol eq 'pop3' ) {
218 1 my $greeting = $self->read_socket( $socket, $line );
219 1 ▶ return 'greeted with ' . $first->($greeting) if $greeting !~ /^\+OK/;
Branch: 1 branch
BranchTrueFalseTotal
unless $greeting =~ /^\+OK/u 0 1 1
220 1 print $socket "STLS\r\n";
221 1 my $reply = $self->read_socket( $socket, $line );
222 1 ▶ return 'refused TLS, ' . $first->($reply) if $reply !~ /^\+OK/;
Branch: 1 branch
BranchTrueFalseTotal
unless $reply =~ /^\+OK/u 0 1 1
223 } else {
224 2 print $socket $LDAP_STARTTLS;
225 2 ▶ my $reply = $self->read_socket( $socket, sub { return $_[0] =~ /\x78.*?\x0a\x01./s } );
Subroutine __ANON__: called CC=1 SCAR=0.0
226 2 ▶ return 'got no StartTLS reply' if $reply !~ /\x78.*?\x0a\x01(.)/s;
Branch: 1 branch
BranchTrueFalseTotal
unless $reply =~ /\x78.*?\x0a\x01(.)/su 0 2 2
227 2 ▶ return 'refused TLS, result code ' . ord($1) if $1 ne "\x00";
Branch: 1 branch
BranchTrueFalseTotal
if $1 ne "\0" 1 1 2
228 }
229  
230 7 return undef;
231 } ## end sub starttls
232  
233 4 1;
« blib/lib/Monitoring/Sneck/Checks/hosts.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/swraid.pm »