Mail/ESMTP/STARTTLS.pm

filter: Statementstmt 90.4% 38 / 42 Branchbran 70.0% 7 / 10 Conditioncond 41.6% 5 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 3 totaltotal 72.2% 60 / 83 CC 10 SCAR 21.1
CC
10
Cov
78%
starttls
14.0
BEGIN
1.0
BEGIN
1.0
CRAP 8.2
« Mail/ESMTP/PIPELINING.pm ↑ Summary Mail/ESMTP.pm »
1 #
2 # Copyright 2013 Mytram <r.mytram@gmail.com>. All rights reserved.
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the same terms as Perl itself.
6 #
7  
8 package Net::Server::Mail::ESMTP::STARTTLS;
9  
10 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # IO::Socket::SSL v1.831 fixed a readline() behavioural deviation in
15 # list context on nonblocking sockets, which caused Net::Server::Mail
16 # to fail to read commands correctly
17  
18 2 ▶use IO::Socket::SSL 1.831;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Net::Server::Mail::ESMTP::Extension;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 our @ISA = qw(Net::Server::Mail::ESMTP::Extension);
21  
22 2 our $VERSION = 0.26;
23  
24 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 REPLY_READY_TO_START => 220,
26 REPLY_SYNTAX_ERROR => 502,
27 REPLY_NOT_AVAILABLE => 454,
28 2 };
29  
30 # https://tools.ietf.org/html/rfc2487
31  
32 7 ▶sub verb {
Subroutine verb: called CC=1 SCAR=0.0
Pod undocumented
33 7 my $self = shift;
34 7 return ( [ 'STARTTLS' => \&starttls ] );
35 }
36  
37 9 ▶sub keyword { 'STARTTLS' }
Subroutine keyword: called CC=1 SCAR=0.0
Pod undocumented
38  
39 # Return a non undef to signal the server to close the socket.
40 6 ▶sub starttls {
Subroutine starttls: called CC=10 SCAR=26.4
Pod undocumented
41 6 my $server = shift;
42 6 my $args = shift;
43  
44 6 ▶ if ($args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args) 1 5 6
45  
46 # No parameter verb
47 1 $server->reply( REPLY_SYNTAX_ERROR,
48 'Syntax error (no parameters allowed)' );
49 1 return;
50 }
51  
52 my $ssl_config = $server->{options}{ssl_config}
53 5 ▶ if exists $server->{options}{ssl_config};
Branch: 1 branch
BranchTrueFalseTotal
if exists $server->{"options"}{"ssl_config"} 5 0 5
54 5 ▶ if ( !$ssl_config || ref $ssl_config ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ssl_config or ref $ssl_config ne "HASH") 0 5 5
Condition33%1/3
not $ssl_config or ref $ssl_config ne "HASH"
l!l&&r!l&&!r
005
MC/DC0%0/2
not $ssl_config or ref $ssl_config ne "HASH"
not $ssl_config ref $ssl_config ne "HASH"
Truth table33%1/3
not(A) or B
A= not $ssl_configB= ref $ssl_config ne "HASH"
ABresult
1X1
011
000
55 0 $server->reply( REPLY_NOT_AVAILABLE,
56 'TLS not available due to temporary reason' );
57 0 return;
58 }
59  
60 5 $server->reply( REPLY_READY_TO_START, 'Ready to start TLS' );
61  
62 my $ssl_socket = IO::Socket::SSL->start_SSL( $server->{options}{socket},
63 5 %$ssl_config, SSL_server => 1, );
64  
65 # Use SSL_startHandshake to control nonblocking behaviour
66 # See perldoc IO::Socket::SSL for more
67  
68 5 ▶ if ( !$ssl_socket || !$ssl_socket->isa('IO::Socket::SSL') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ssl_socket and $ssl_socket->isa("IO::Socket::SSL")) 2 3 5
Condition66%2/3
$ssl_socket and $ssl_socket->isa("IO::Socket::SSL")
!ll&&!rl&&r
203
MC/DC50%1/2
$ssl_socket and $ssl_socket->isa("IO::Socket::SSL")
$ssl_socket $ssl_socket->isa("IO::Socket::SSL")
Truth table66%2/3
A and B
A= $ssl_socketB= $ssl_socket->isa("IO::Socket::SSL")
ABresult
0X0
100
111
69 2 $server->reply( REPLY_NOT_AVAILABLE,
70 'TLS not available due to temporary reason ['
71 . IO::Socket::SSL::errstr()
72 . ']' );
73 2 return 0; # to single the server to close the socket
74 }
75  
76 3 my $ref = $server->{callback}->{STARTTLS};
77 3 ▶ if ( defined $ref && ref $ref eq 'ARRAY' && ref $ref->[0] eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref and ref $ref eq "ARRAY" and ref $ref->[0] eq "CODE") 0 3 3
Condition33%2/6
defined $ref and ref $ref eq "ARRAY" and ref $ref->[0] eq "CODE"
!ll&&!rl&&r
300
defined $ref and ref $ref eq "ARRAY"
!ll&&!rl&&r
300
MC/DC0%0/3
defined $ref and ref $ref eq "ARRAY" and ref $ref->[0] eq "CODE"
defined $ref ref $ref eq "ARRAY" ref $ref->[0] eq "CODE"
Truth table25%1/4
A and B and C
A= defined $refB= ref $ref eq "ARRAY"C= ref $ref->[0] eq "CODE"
ABCresult
0XX0
10X0
1100
1111
78 0 my $code = $ref->[0];
79 0 &$code($server);
80 }
81  
82 3 return ();
83 }
84  
85 2 1;
86  
87 =head1 NAME
88  
89 Net::Server::Mail::ESMTP::STARTTLS - A module to support the STARTTLS command in Net::Server::Mail::ESMTP
90  
91 =head1 SYNOPSIS
92  
93 use strict;
94 use Net::Server::Mail::ESMTP;
95  
96 my @local_domains = qw(example.com example.org);
97 my $server = IO::Socket::INET->new( Listen => 1, LocalPort => 25 );
98  
99 my $conn;
100 while($conn = $server->accept)
101 {
102 my $esmtp = Net::Server::Mail::ESMTP->new(
103 socket => $conn,
104 SSL_config => {
105 SSL_cert_file => 'your_cert.pem',
106 SSL_key_file => 'your_key.key',
107 # Any other options taken by IO::Socket::SSL
108 }
109 );
110
111 # activate some extensions
112 $esmtp->register('Net::Server::Mail::ESMTP::STARTTLS');
113
114 # adding optional STARTTLS handler
115 $esmtp->set_callback(STARTTLS => \&tls_started);
116 $esmtp->process();
117 $conn->close();
118 }
119
120 sub tls_started {
121 my ($session) = @_;
122  
123 # Now, allow authentication
124 $session->register('Net::Server::Mail::ESMTP::AUTH');
125 }
126  
127 =head1 DESCRIPTION
128  
129 This module conducts a TLS handshake with the client upon receiving
130 the STARTTLS command. It uses IO::Socket::SSL, requiring 1.831+, to
131 perform the handshake and secure traffic.
132  
133 An additional option, SSL_config, is passed to
134 Net::Server::Mail::ESMTP's constructor. It contains options for
135 IO::Socket::SSL's constructor. Please refer to IO::Socket::SSL's
136 perldoc for details.
137  
138 =head1 SEE ALSO
139  
140 Please, see L<Net::Server::Mail>
141  
142 =head1 AUTHOR
143  
144 This module has been written by Xavier Guimard <x.guimard@free.fr> using libs
145 written by:
146  
147 =over
148  
149 =item Mytram <rmytram@gmail.com>
150  
151 =item Dan Moore C<< <dan at moore.cx> >>
152  
153 =back
154  
155 =head1 AVAILABILITY
156  
157 Available on CPAN.
158  
159 anonymous Git repository:
160  
161 git clone git://github.com/rs/net-server-mail.git
162  
163 Git repository on the web:
164  
165 L<https://github.com/rs/net-server-mail>
166  
167 =head1 BUGS
168  
169 Please use CPAN system to report a bug (http://rt.cpan.org/).
170  
171 =head1 LICENSE AND COPYRIGHT
172  
173 =over
174  
175 =item Copyright (C) 2009 - Dan Moore <dan at moore.cx>
176  
177 =item Copyright (C) 2013 - Mytram <rmytram@gmail.com>
178  
179 =item Copyright (C) 2013 - Xavier Guimard <x.guimard@free.fr>
180  
181 =back
182  
183 This program is free software; you can redistribute it and/or modify it
184 under the terms of either: the GNU General Public License as published
185 by the Free Software Foundation; or the Artistic License.
186  
187 See http://dev.perl.org/licenses/ for more information.
188  
189 =cut
« Mail/ESMTP/PIPELINING.pm ↑ Summary Mail/ESMTP.pm »