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

filter: Statementstmt 93.2% 97 / 104 Branchbran 80.3% 45 / 56 Conditioncond 68.1% 15 / 22 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 100% 14 / 14 Podpod 100% 7 / 7 totaltotal 84.7% 184 / 217 CC 39 SCAR 29.2
CC
39
Cov
86%
check
31.1
validate
7.7
reply
5.0
CRAP 18.5
« blib/lib/Monitoring/Sneck/Checks/apparmor.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/jails.pm »
1 package Monitoring::Sneck::Checks::smtp;
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 4 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Monitoring::Sneck::Checks::smtp - checks a SMTP server
12  
13 =head1 VERSION
14  
15 Version 0.0.1
16  
17 =cut
18  
19 4 our $VERSION = '0.0.1';
20  
21 =head1 SYNOPSIS
22  
23 smtp|perl:smtp -H mail.example.org
24 smtp_tls|perl:smtp -H mail.example.org -s -E SIZE -E PIPELINING -w 1 -c 3
25 submissions|perl:smtp -H mail.example.org -p 465 -S -E AUTH
26 smtp_banner|perl:smtp -H 192.0.2.25 -e 'ESMTP Postfix'
27  
28 =head1 DESCRIPTION
29  
30 Connects, reads the greeting, says EHLO, and QUITs, timing it all. A
31 greeting other than 220, such as 554 for refusing, a EHLO not answered
32 with 250, or a extension asked for not being offered, is CRITICAL, as
33 is not being able to connect.
34  
35 With --starttls, STARTTLS must be offered, and is done, with EHLO said
36 again over TLS. With --ssl, it connects over TLS from the start, as for
37 port 465. The certificate is checked unless --insecure is given. See
38 L<Monitoring::Sneck::Checks::https_cert> with --starttls smtp for when
39 it expires.
40  
41 =head1 OPTIONS
42  
43 - -H, --hostname :: The host, and the name asked for through SNI,
44 unless it is a IP. Required.
45  
46 - -p, --port :: The port.
47 Default :: 25, or 465 with --ssl
48  
49 - -S, --ssl :: Connect over TLS. Can not be used with --starttls.
50  
51 - -s, --starttls :: STARTTLS must be offered, and is done. Can not be
52 used with --ssl.
53  
54 - -e, --expect :: Text the greeting must have.
55  
56 - -E, --extension :: A extension EHLO must offer, such as SIZE, AUTH,
57 or PIPELINING. May be given more than once.
58  
59 - -n, --helo-name :: The name to give in EHLO.
60 Default :: the host name of this host
61  
62 - -w, --warning :: Range of seconds for the whole of it.
63  
64 - -c, --critical :: Range of seconds for the whole of it.
65  
66 - --insecure :: Do not check the certificate.
67  
68 - --ca-file :: The CA file to check the certificate against.
69  
70 =cut
71  
72 9 ▶sub option_spec {
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
73 return (
74 9 'hostname|H=s', 'port|p=i', 'ssl|S', 'starttls|s', 'expect|e=s', 'extension|E=s@',
75 'helo-name|n=s', 'warning|w=s', 'critical|c=s', 'insecure', 'ca-file=s',
76 );
77 }
78  
79 9 ▶sub required { return ('hostname') }
Subroutine required: called CC=1 SCAR=0.0
Pod documented
80  
81 9 ▶sub ranges { return ( 'warning', 'critical' ) }
Subroutine ranges: called CC=1 SCAR=0.0
Pod documented
82  
83 9 ▶sub validate {
Subroutine validate: called CC=7 SCAR=20.4
Pod documented
84 9 my ( $self, $options ) = @_;
85  
86 9 my @problems;
87 push( @problems, '--hostname "' . $options->{hostname} . '" is not a IP or hostname' )
88 9 ▶ if defined( $options->{hostname} ) && !$self->is_host( $options->{hostname} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"hostname"} and not $self->is_host($options->{"hostname"}) 0 9 9
Condition33%1/3
defined $options->{"hostname"} and not $self->is_host($options->{"hostname"})
!ll&&!rl&&r
090
MC/DC0%0/2
defined $options->{"hostname"} and not $self->is_host($options->{"hostname"})
defined $options->{"hostname"} not $self->is_host($options->{"hostname"})
Truth table33%1/3
A and not(B)
A= defined $options->{"hostname"}B= not $self->is_host($options->{"hostname"})
ABresult
0X0
100
111
89 9 ▶ push( @problems, '--ssl and --starttls can not both be given' ) if $options->{ssl} && $options->{starttls};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"ssl"} and $options->{"starttls"} 1 8 9
Condition100%3/3
$options->{"ssl"} and $options->{"starttls"}
!ll&&!rl&&r
531
MC/DC100%2/2
$options->{"ssl"} and $options->{"starttls"}
$options->{"ssl"} $options->{"starttls"}
Truth table100%3/3
A and B
A= $options->{"ssl"}B= $options->{"starttls"}
ABresult
0X0
100
111
90 push( @problems, '--helo-name "' . $options->{'helo-name'} . '" is not a hostname' )
91 9 ▶ if defined( $options->{'helo-name'} ) && !$self->is_host( $options->{'helo-name'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"helo-name"} and not $self->is_host($options->{"helo-name"}) 0 9 9
Condition66%2/3
defined $options->{"helo-name"} and not $self->is_host($options->{"helo-name"})
!ll&&!rl&&r
630
MC/DC0%0/2
defined $options->{"helo-name"} and not $self->is_host($options->{"helo-name"})
defined $options->{"helo-name"} not $self->is_host($options->{"helo-name"})
Truth table66%2/3
A and not(B)
A= defined $options->{"helo-name"}B= not $self->is_host($options->{"helo-name"})
ABresult
0X0
100
111
92  
93 9 return @problems;
94 } ## end sub validate
95  
96 =head1 METHODS
97  
98 =head2 check
99  
100 Does the check.
101  
102 =cut
103  
104 8 ▶sub check {
Subroutine check: called CC=28 SCAR=34.4
Pod documented
105 8 my ($self) = @_;
106  
107 8 require IO::Socket::SSL;
108 8 require IO::Socket::IP;
109 8 require Sys::Hostname;
110 8 require Data::Validate::IP;
111 8 my $host = $self->opt('hostname');
112 8 ▶ my $port = $self->opt('port') // ( $self->opt('ssl') ? 465 : 25 );
Branch: 1 branch
BranchTrueFalseTotal
$self->opt('ssl') ? : 1 0 1
Condition100%2/2
$self->opt("port") // ($self->opt("ssl") ? 465 : 25)
l!l
71
MC/DC100%1/1
$self->opt("port") // ($self->opt("ssl") ? 465 : 25)
$self->opt("port")
Truth table100%2/2
A
A= $self->opt("port")
Aresult
11
00
113 8 my $where = $host . ':' . $port;
114 8 ▶ my %ssl = ( SSL_verify_mode => $self->opt('insecure') ? 0 : 1, Timeout => $self->timeout );
Branch: 1 branch
BranchTrueFalseTotal
$self->opt('insecure') ? : 1 7 8
115  
116 # SNI does not take a IP, and is set either way, as start_SSL has no PeerHost to take it from
117 8 ▶ $ssl{SSL_hostname} = Data::Validate::IP::is_ip($host) ? '' : $host;
Branch: 1 branch
BranchTrueFalseTotal
Data::Validate::IP::is_ip($host) ? : 7 1 8
118 8 ▶ if ( !$self->opt('insecure') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->opt("insecure")) 7 1 8
119 7 $ssl{SSL_verifycn_name} = $host;
120 7 $ssl{SSL_verifycn_scheme} = 'smtp';
121 }
122 8 ▶ $ssl{SSL_ca_file} = $self->opt('ca-file') if defined( $self->opt('ca-file') );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->opt("ca-file") 2 6 8
123  
124 8 my $start = Time::HiRes::time;
125 8 ▶ my $socket
Branch: 1 branch
BranchTrueFalseTotal
$self->opt('ssl') ? : 3 5 8
126 = $self->opt('ssl')
127 ? IO::Socket::SSL->new( PeerHost => $self->opt('hostname'), PeerPort => $port, %ssl )
128 : IO::Socket::IP->new(
129 PeerHost => $self->opt('hostname'),
130 PeerPort => $port,
131 Proto => 'tcp',
132 Timeout => $self->timeout
133 );
134 8 ▶ if ( !$socket ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($socket) 2 6 8
135 2 ▶ return $self->finish( $self->CRITICAL,
Branch: 1 branch
BranchTrueFalseTotal
$self->opt('ssl') ? : 2 0 2
Condition0%0/3
$@ or $!
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or $!
$@ $!
Truth table0%0/3
A or B
A= $@B= $!
ABresult
1X1
011
000
136 $where . ', ' . ( $self->opt('ssl') ? IO::Socket::SSL::errstr() : ( $@ || $! ) ) );
137 }
138 6 $socket->autoflush(1);
139  
140 6 my $greeting = $self->reply($socket);
141 6 ▶ if ( $greeting->{code} != 220 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($greeting->{"code"} != 220) 1 5 6
142 1 close($socket);
143 1 return $self->finish( $self->CRITICAL, $where . ' greeted with ' . _first($greeting) );
144 }
145 5 my @problems;
146 5 ▶ if ( defined( $self->opt('expect') ) && index( $greeting->{text}, $self->opt('expect') ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->opt("expect") and index($greeting->{"text"}, $self->opt("expect")) == -1) 1 4 5
Condition100%3/3
defined $self->opt("expect") and index($greeting->{"text"}, $self->opt("expect")) == -1
!ll&&!rl&&r
311
MC/DC100%2/2
defined $self->opt("expect") and index($greeting->{"text"}, $self->opt("expect")) == -1
defined $self->opt("expect") index($greeting->{"text"}, $self->opt("expect")) == -1
Truth table100%3/3
A and B
A= defined $self->opt("expect")B= index($greeting->{"text"}, $self->opt("expect")) == -1
ABresult
0X0
100
111
147 1 push( @problems, 'greeting does not have "' . $self->opt('expect') . '"' );
148 }
149  
150 5 ▶ my $helo = $self->opt('helo-name') // Sys::Hostname::hostname();
Condition66%2/3
$self->opt("helo-name") // Sys::Hostname::hostname()
l!l&&r!l&&!r
320
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
151 5 my $ehlo = $self->command( $socket, 'EHLO ' . $helo );
152 5 ▶ if ( $ehlo->{code} != 250 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ehlo->{"code"} != 250) 0 5 5
153 0 close($socket);
154 0 return $self->finish( $self->CRITICAL, $where . ' answered EHLO with ' . _first($ehlo) );
155 }
156 5 ▶ if ( $self->opt('starttls') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opt("starttls")) 2 3 5
157 2 ▶ if ( !$ehlo->{extensions}{STARTTLS} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ehlo->{"extensions"}{"STARTTLS"}) 1 1 2
158 1 close($socket);
159 1 return $self->finish( $self->CRITICAL, $where . ' does not offer STARTTLS' );
160 }
161 1 my $go = $self->command( $socket, 'STARTTLS' );
162 1 ▶ if ( $go->{code} != 220 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($go->{"code"} != 220) 0 1 1
163 0 close($socket);
164 0 return $self->finish( $self->CRITICAL, $where . ' answered STARTTLS with ' . _first($go) );
165 }
166 1 ▶ if ( !IO::Socket::SSL->start_SSL( $socket, %ssl ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("IO::Socket::SSL"->start_SSL($socket, %ssl)) 0 1 1
167 0 return $self->finish( $self->CRITICAL, $where . ' STARTTLS failed, ' . IO::Socket::SSL::errstr() );
168 }
169 1 $ehlo = $self->command( $socket, 'EHLO ' . $helo );
170 1 ▶ if ( $ehlo->{code} != 250 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ehlo->{"code"} != 250) 0 1 1
171 0 close($socket);
172 0 return $self->finish( $self->CRITICAL, $where . ' answered EHLO over TLS with ' . _first($ehlo) );
173 }
174 } ## end if ( $self->opt('starttls') )
175 4 ▶ foreach my $extension ( @{ $self->opt('extension') // [] } ) {
Condition100%2/2
$self->opt("extension") // []
l!l
22
MC/DC100%1/1
$self->opt("extension") // []
$self->opt("extension")
Truth table100%2/2
A
A= $self->opt("extension")
Aresult
11
00
176 3 ▶ push( @problems, 'does not offer ' . uc($extension) ) if !$ehlo->{extensions}{ uc($extension) };
Branch: 1 branch
BranchTrueFalseTotal
unless $ehlo->{"extensions"}{uc $extension} 1 2 3
177 }
178 4 $self->command( $socket, 'QUIT' );
179 4 close($socket);
180 4 my $seconds = sprintf( '%.3f', Time::HiRes::time - $start );
181  
182 4 ▶ my @codes = ( defined( $problems[0] ) ? $self->CRITICAL : $self->OK );
Branch: 1 branch
BranchTrueFalseTotal
defined $problems[0] ? : 1 3 4
183 4 my $time_code = $self->threshold_status( $seconds, $self->opt('warning'), $self->opt('critical') );
184 4 push( @codes, $time_code );
185 4 ▶ push( @problems, 'slow' ) if $time_code != $self->OK;
Branch: 1 branch
BranchTrueFalseTotal
if $time_code != $self->OK 0 4 4
186 4 $self->perfdata(
187 label => 'time',
188 value => $seconds,
189 uom => 's',
190 warning => $self->opt('warning'),
191 critical => $self->opt('critical'),
192 min => 0
193 );
194 4 ▶ my $message
Branch: 1 branch
BranchTrueFalseTotal
$self->opt('starttls') ? : 1 3 4
195 = $where . ' '
196 . _first($greeting) . ', in '
197 . $seconds
198 . ' seconds'
199 . ( $self->opt('starttls') ? ', over STARTTLS' : '' );
200 4 ▶ $message = $message . ', ' . join( ', ', @problems ) if defined( $problems[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $problems[0] 1 3 4
201  
202 4 return $self->finish( $self->worst(@codes), $message );
203 } ## end sub check
204  
205 =head2 command
206  
207 Sends a SMTP command and reads the reply.
208  
209 - socket :: The socket.
210  
211 - command :: The command, without the line ending.
212  
213 Returns the reply, as from L</reply>.
214  
215 =cut
216  
217 11 ▶sub command {
Subroutine command: called CC=1 SCAR=0.0
Pod documented
218 11 my ( $self, $socket, $command ) = @_;
219  
220 11 print $socket $command . "\r\n";
221  
222 11 return $self->reply($socket);
223 }
224  
225 =head2 reply
226  
227 Reads a SMTP reply, all of its lines.
228  
229 Returns a hash ref of code, 0 if none came, text, the lines without
230 their codes joined by newlines, and extensions, a hash ref of the
231 upper cased first word of each line after the first, for EHLO.
232  
233 =cut
234  
235 17 ▶sub reply {
Subroutine reply: called CC=5 SCAR=16.1
Pod documented
236 17 my ( $self, $socket ) = @_;
237  
238 # a full reply, the last line of a multi line one having a space or nothing after the code
239 17 ▶ my $read = $self->read_socket( $socket, sub { return $_[0] =~ /^\d{3}(?: [^\n]*)?\r?\n/m } );
Subroutine __ANON__: called CC=1 SCAR=0.0
240 17 my @lines = $read =~ /^(\d{3})[ -]?([^\r\n]*)/mg;
241 17 my %reply = ( code => 0, text => '', extensions => {} );
242 17 ▶ return \%reply if !@lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @lines 2 15 17
243  
244 15 my @texts;
245 15 ▶ while ( my ( $code, $text ) = splice( @lines, 0, 2 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@lines, 0, 2)) 33 15 48
246 33 $reply{code} = $code;
247 33 push( @texts, $text );
248 }
249 15 $reply{text} = join( "\n", @texts );
250 15 foreach my $text ( @texts[ 1 .. $#texts ] ) {
251 18 ▶ $reply{extensions}{ uc($1) } = 1 if $text =~ /\A(\S+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\A(\S+)/u 18 0 18
252 }
253  
254 15 return \%reply;
255 } ## end sub reply
256  
257 # Describes a reply for messages, its code and first line.
258 #
259 # Args...
260 #
261 # - reply :: From reply.
262 #
263 # Returns the description, such as '220 mail.example.org ESMTP', or
264 # 'nothing' if no reply came.
265 #
266 # Example...
267 #
268 # _first( { code => 554, text => "no thanks\nbye" } ); # '554 no thanks'
269 5 ▶sub _first {
Subroutine _first: called CC=2 SCAR=7.1
270 5 my ($reply) = @_;
271  
272 5 ▶ return 'nothing' if !$reply->{code};
Branch: 1 branch
BranchTrueFalseTotal
unless $reply->{"code"} 0 5 5
273  
274 5 return $reply->{code} . ' ' . ( split( /\n/, $reply->{text} ) )[0];
275 }
276  
277 4 1;
« blib/lib/Monitoring/Sneck/Checks/apparmor.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/jails.pm »