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

filter: Statementstmt 95.3% 82 / 86 Branchbran 87.5% 42 / 48 Conditioncond 79.3% 23 / 29 MC/DCmcdc 61.1% 11 / 18 Subroutinesub 100% 10 / 10 Podpod 100% 6 / 6 totaltotal 88.3% 174 / 197 CC 40 SCAR 25.6
CC
40
Cov
90%
check
20.0
try
11.0
validate
10.0
CRAP 12.9
« blib/lib/Monitoring/Sneck/Checks/fail2ban.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/mailq.pm »
1 package Monitoring::Sneck::Checks::tls_protocols;
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::tls_protocols - checks what TLS versions a server takes
11  
12 =head1 VERSION
13  
14 Version 0.0.1
15  
16 =cut
17  
18 4 our $VERSION = '0.0.1';
19  
20 # The versions, by the names IO::Socket::SSL takes, and how they are said.
21 4 our @VERSIONS = (
22 [ 'SSLv3', 'SSLv3' ],
23 [ 'TLSv1', 'TLSv1.0' ],
24 [ 'TLSv1_1', 'TLSv1.1' ],
25 [ 'TLSv1_2', 'TLSv1.2' ],
26 [ 'TLSv1_3', 'TLSv1.3' ]
27 );
28 4 our %SAID = map { $_->[0] => $_->[1] } @VERSIONS;
29  
30 =head1 SYNOPSIS
31  
32 www_tls|perl:tls_protocols -H www.example.org
33 mail_tls|perl:tls_protocols -H mail.example.org -s smtp -a TLSv1_2 -a TLSv1_3
34 ldaps_tls|perl:tls_protocols -H ldap.example.org -p 636 -r TLSv1 -r TLSv1_1 -r SSLv3
35  
36 =head1 DESCRIPTION
37  
38 Tries each TLS version given on its own, through L<IO::Socket::SSL>. A
39 version in --refuse that the server takes, or one in --accept that it
40 does not, is CRITICAL.
41  
42 Every version is tried with OpenSSL's security level set to 0, as
43 otherwise this side would refuse the old ones, not the server. A version this
44 host's OpenSSL can not do at all, as is common for SSLv3, is UNKNOWN,
45 as it can not be told if the server takes it. So is not being able
46 to connect, or STARTTLS failing, as nothing could be tried.
47  
48 The certificate is not checked, see L<Monitoring::Sneck::Checks::https_cert>
49 for that. --starttls works the same as there.
50  
51 =head1 OPTIONS
52  
53 - -H, --hostname :: The host. Required.
54  
55 - -p, --port :: The port.
56 Default :: 443, or for --starttls, the protocol's
57  
58 - -n, --sni :: The name to ask for.
59 Default :: --hostname
60  
61 - -s, --starttls :: Ask for TLS through smtp, imap, pop3, ftp, or ldap.
62  
63 - --helo-name :: The name to give in EHLO, for --starttls smtp.
64 Default :: the host name of this host
65  
66 - -r, --refuse :: A version it must not take, SSLv3, TLSv1, TLSv1_1,
67 TLSv1_2, or TLSv1_3. May be given more than once.
68 Default :: TLSv1 and TLSv1_1, unless --accept is given
69  
70 - -a, --accept :: A version it must take. May be given more than once.
71  
72 =cut
73  
74 10 ▶sub option_spec {
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
75 10 return ( 'hostname|H=s', 'port|p=i', 'sni|n=s', 'starttls|s=s', 'helo-name=s', 'refuse|r=s@', 'accept|a=s@' );
76 }
77  
78 10 ▶sub required { return ('hostname') }
Subroutine required: called CC=1 SCAR=0.0
Pod documented
79  
80 10 ▶sub validate {
Subroutine validate: called CC=10 SCAR=23.1
Pod documented
81 10 my ( $self, $options ) = @_;
82  
83 10 require Monitoring::Sneck::Checks::https_cert;
84 10 my @problems;
85 10 foreach my $name ( 'hostname', 'sni', 'helo-name' ) {
86 push( @problems, '--' . $name . ' "' . $options->{$name} . '" is not a IP or hostname' )
87 30 ▶ if defined( $options->{$name} ) && !$self->is_host( $options->{$name} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{$name} and not $self->is_host($options->{$name}) 0 30 30
Condition66%2/3
defined $options->{$name} and not $self->is_host($options->{$name})
!ll&&!rl&&r
19110
MC/DC0%0/2
defined $options->{$name} and not $self->is_host($options->{$name})
defined $options->{$name} not $self->is_host($options->{$name})
Truth table66%2/3
A and not(B)
A= defined $options->{$name}B= not $self->is_host($options->{$name})
ABresult
0X0
100
111
88 }
89 10 foreach my $name ( 'refuse', 'accept' ) {
90 20 ▶ foreach my $version ( @{ $options->{$name} // [] } ) {
Condition100%2/2
$options->{$name} // []
l!l
713
MC/DC100%1/1
$options->{$name} // []
$options->{$name}
Truth table100%2/2
A
A= $options->{$name}
Aresult
11
00
91 5 push( @problems, '--' . $name . ' "' . $version . '" must be ' . join( ', ', map { $_->[0] } @VERSIONS ) )
92 9 ▶ if !$SAID{$version};
Branch: 1 branch
BranchTrueFalseTotal
unless $Monitoring::Sneck::Checks::tls_protocols::SAID{$version} 1 8 9
93 }
94 }
95 push( @problems,
96 '--starttls must be ' . join( ', ', sort( keys(%Monitoring::Sneck::Checks::https_cert::STARTTLS_PORTS) ) ) )
97 if defined( $options->{starttls} )
98 10 ▶ && !$Monitoring::Sneck::Checks::https_cert::STARTTLS_PORTS{ $options->{starttls} };
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"starttls"} and not $Monitoring::Sneck::Checks::https_cert::STARTTLS_PORTS{$options->{"starttls"}} 1 9 10
Condition100%3/3
defined $options->{"starttls"} and not $Monitoring::Sneck::Checks::https_cert::STARTTLS_PORTS{$options->{"starttls"}}
!ll&&!rl&&r
811
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
99  
100 10 return @problems;
101 } ## end sub validate
102  
103 =head1 METHODS
104  
105 =head2 check
106  
107 Does the check.
108  
109 =cut
110  
111 8 ▶sub check {
Subroutine check: called CC=20 SCAR=30.0
Pod documented
112 8 my ($self) = @_;
113  
114 8 ▶ my @refuse = @{ $self->opt('refuse') // ( defined( $self->opt('accept') ) ? [] : [ 'TLSv1', 'TLSv1_1' ] ) };
Branch: 1 branch
BranchTrueFalseTotal
defined $self->opt('accept') ? : 3 3 6
Condition100%2/2
$self->opt("refuse") // (defined $self->opt("accept") ? [] : ["TLSv1", "TLSv1_1"])
l!l
26
MC/DC100%1/1
$self->opt("refuse") // (defined $self->opt("accept") ? [] : ["TLSv1", "TLSv1_1"])
$self->opt("refuse")
Truth table100%2/2
A
A= $self->opt("refuse")
Aresult
11
00
115 8 ▶ my @accept = @{ $self->opt('accept') // [] };
Condition100%2/2
$self->opt("accept") // []
l!l
44
MC/DC100%1/1
$self->opt("accept") // []
$self->opt("accept")
Truth table100%2/2
A
A= $self->opt("accept")
Aresult
11
00
116 8 my ( @taken, @refused, @should_refuse, @should_take, @untried );
117 8 foreach my $version ( map { $_->[0] } @VERSIONS ) {
118 40 ▶ next if !grep { $_ eq $version } @refuse, @accept;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $version;} @refuse, @accept 26 14 40
119 14 my ( $taken, $why ) = $self->try($version);
120 14 ▶ if ( !defined($taken) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $taken) 3 11 14
121 3 push( @untried, $SAID{$version} . ', ' . $why );
122 3 next;
123 }
124 11 ▶ push( @{ $taken ? \@taken : \@refused }, $SAID{$version} );
Branch: 1 branch
BranchTrueFalseTotal
$taken ? : 6 5 11
125 11 ▶ push( @should_refuse, $SAID{$version} ) if $taken && grep { $_ eq $version } @refuse;
Branch: 1 branch
BranchTrueFalseTotal
if $taken and grep {$_ eq $version;} @refuse 2 9 11
Condition100%3/3
$taken and grep {$_ eq $version;} @refuse
!ll&&!rl&&r
542
MC/DC100%2/2
$taken and grep {$_ eq $version;} @refuse
$taken grep {$_ eq $version;} @refuse
Truth table100%3/3
A and B
A= $takenB= grep {$_ eq $version;} @refuse
ABresult
0X0
100
111
126 11 ▶ push( @should_take, $SAID{$version} ) if !$taken && grep { $_ eq $version } @accept;
Branch: 1 branch
BranchTrueFalseTotal
if not $taken and grep {$_ eq $version;} @accept 1 10 11
Condition100%3/3
not $taken and grep {$_ eq $version;} @accept
!ll&&!rl&&r
641
MC/DC100%2/2
not $taken and grep {$_ eq $version;} @accept
not $taken grep {$_ eq $version;} @accept
Truth table100%3/3
not(A) and B
A= not $takenB= grep {$_ eq $version;} @accept
ABresult
0X0
100
111
127 } ## end foreach my $version ( map { $_->[0] } @VERSIONS)
128  
129 8 my @codes = ( $self->OK );
130 8 ▶ push( @codes, $self->CRITICAL ) if defined( $should_refuse[0] ) || defined( $should_take[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $should_refuse[0] or defined $should_take[0] 2 6 8
Condition100%3/3
defined $should_refuse[0] or defined $should_take[0]
l!l&&r!l&&!r
116
MC/DC100%2/2
defined $should_refuse[0] or defined $should_take[0]
defined $should_refuse[0] defined $should_take[0]
Truth table100%3/3
A or B
A= defined $should_refuse[0]B= defined $should_take[0]
ABresult
1X1
011
000
131 8 ▶ push( @codes, $self->UNKNOWN ) if defined( $untried[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $untried[0] 2 6 8
132 8 my @parts;
133 8 ▶ push( @parts, 'takes ' . join( ', ', @taken ) ) if defined( $taken[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $taken[0] 4 4 8
134 8 ▶ push( @parts, 'refuses ' . join( ', ', @refused ) ) if defined( $refused[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $refused[0] 3 5 8
135 8 ▶ push( @parts, 'should refuse ' . join( ', ', @should_refuse ) ) if defined( $should_refuse[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $should_refuse[0] 1 7 8
136 8 ▶ push( @parts, 'should take ' . join( ', ', @should_take ) ) if defined( $should_take[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $should_take[0] 1 7 8
137 8 ▶ push( @parts, 'could not try ' . join( '; ', @untried ) ) if defined( $untried[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $untried[0] 2 6 8
138  
139 8 return $self->finish( $self->worst(@codes),
140 $self->opt('hostname') . ':' . $self->port . ' ' . join( ', ', @parts ) );
141 } ## end sub check
142  
143 =head2 port
144  
145 Returns the port, --port, or the default for --starttls, or 443.
146  
147 =cut
148  
149 22 ▶sub port {
Subroutine port: called CC=3 SCAR=16.5
Pod documented
150 22 my ($self) = @_;
151  
152 22 ▶ return $self->opt('port') if defined( $self->opt('port') );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->opt("port") 22 0 22
153 0 ▶ return $Monitoring::Sneck::Checks::https_cert::STARTTLS_PORTS{ $self->opt('starttls') }
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->opt("starttls") 0 0 0
154 if defined( $self->opt('starttls') );
155  
156 0 return 443;
157 }
158  
159 =head2 try
160  
161 Tries a version.
162  
163 Returns 1 if the server takes it, 0 and why if not, or undef and why if
164 it could not be tried, as for this host's OpenSSL not doing it, or not
165 being able to connect at all.
166  
167 =cut
168  
169 14 ▶sub try {
Subroutine try: called CC=10 SCAR=24.0
Pod documented
170 14 my ( $self, $version ) = @_;
171  
172 14 require IO::Socket::SSL;
173 14 require IO::Socket::IP;
174 14 require Data::Validate::IP;
175 14 ▶ my $name = $self->opt('sni') // $self->opt('hostname');
Condition33%1/3
$self->opt("sni") // $self->opt("hostname")
l!l&&r!l&&!r
0140
MC/DC0%0/2
$self->opt("sni") // $self->opt("hostname")
$self->opt("sni") $self->opt("hostname")
Truth table33%1/3
A // B
A= $self->opt("sni")B= $self->opt("hostname")
ABresult
1X1
011
000
176 14 ▶ my %ssl = (
Branch: 1 branch
BranchTrueFalseTotal
Data::Validate::IP::is_ip($name) ? : 14 0 14
177 SSL_version => $version,
178 SSL_verify_mode => 0,
179 SSL_cipher_list => 'ALL:@SECLEVEL=0',
180 SSL_hostname => Data::Validate::IP::is_ip($name) ? '' : $name,
181 Timeout => $self->timeout,
182 );
183  
184 14 my $plain = IO::Socket::IP->new(
185 PeerHost => $self->opt('hostname'),
186 PeerPort => $self->port,
187 Proto => 'tcp',
188 Timeout => $self->timeout
189 );
190 14 ▶ return ( undef, 'can not connect, ' . ( $@ || $! ) ) if !$plain;
Branch: 1 branch
BranchTrueFalseTotal
unless $plain 2 12 14
Condition33%1/3
$@ || $!
l!l&&r!l&&!r
200
MC/DC0%0/2
$@ || $!
$@ $!
Truth table33%1/3
A || B
A= $@B= $!
ABresult
1X1
011
000
191 12 ▶ if ( defined( $self->opt('starttls') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->opt("starttls")) 1 11 12
192 1 $plain->autoflush(1);
193 1 my $problem = Monitoring::Sneck::Checks::https_cert::starttls( $self, $plain, $self->opt('starttls') );
194 1 ▶ if ( defined($problem) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $problem) 0 1 1
195 0 close($plain);
196 0 return ( undef, $self->opt('starttls') . ' ' . $problem );
197 }
198 }
199 12 my $socket = IO::Socket::SSL->start_SSL( $plain, %ssl );
200 12 ▶ if ($socket) {
Branch: 1 branch
BranchTrueFalseTotal
if ($socket) 6 6 12
201 6 close($socket);
202 6 return (1);
203 }
204 6 ▶ my $why = IO::Socket::SSL::errstr() // 'unknown error';
Condition50%1/2
IO::Socket::SSL::errstr() // "unknown error"
l!l
60
MC/DC0%0/1
IO::Socket::SSL::errstr() // "unknown error"
IO::Socket::SSL::errstr()
Truth table50%1/2
A
A= IO::Socket::SSL::errstr()
Aresult
11
00
205 6 close($plain);
206  
207 # this side not being able to do it at all, not the server saying no
208 6 ▶ return ( undef, 'this host can not do it, ' . $why )
Branch: 1 branch
BranchTrueFalseTotal
if $why =~ /no protocols available|null ssl method|unknown option|invalid SSL_version|SSL Version \S+ not supported/iu 1 5 6
209 if $why
210 =~ /no protocols available|null ssl method|unknown option|invalid SSL_version|SSL Version \S+ not supported/i;
211  
212 5 return ( 0, $why );
213 } ## end sub try
214  
215 4 1;
« blib/lib/Monitoring/Sneck/Checks/fail2ban.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/mailq.pm »