SSLGlue/SMTP.pm

filter: Statementstmt 43.3% 39 / 90 Branchbran 10.0% 3 / 30 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 56.2% 9 / 16 Podpod n/a totaltotal 36.6% 51 / 139 SCAR 42.4
CC
14
Cov
34%
__ANON__
30.0
__ANON__
20.0
__ANON__
12.0
CRAP 69.3
« SSLGlue/Socket.pm ↑ Summary SSLGlue/POP3.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Net::SSLGlue::SMTP;
5 1 ▶use IO::Socket::SSL 1.19;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Net::SMTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 our $VERSION = 1.001;
8  
9 1 my $DONT;
10 BEGIN {
11 1 ▶ if (defined &Net::SMTP::starttls) {
Subroutine BEGIN: called CC=2 SCAR=14.1
Branch: 1 branch
BranchTrueFalseTotal
if (defined &Net::SMTP::starttls) 1 0 1
12 1 warn "using SSL support of Net::SMTP $Net::SMTP::VERSION instead of SSLGlue";
13 1 $DONT = 1;
14 1 goto DONE;
15 }
16  
17 ##############################################################################
18 # mix starttls method into Net::SMTP which on SSL handshake success
19 # upgrades the class to Net::SSLGlue::SMTP::_SSLified
20 ##############################################################################
21 *Net::SMTP::starttls = sub {
22 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
23 0 ▶ $self->_STARTTLS or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_STARTTLS or return 0 0 0
24 0 my $host = $self->host;
25 # for name verification strip port from domain:port, ipv4:port, [ipv6]:port
26 0 $host =~s{(?<!:):\d+$}{};
27  
28 0 ▶ Net::SSLGlue::SMTP::_SSLified->start_SSL( $self,
Branch: 1 branch
BranchTrueFalseTotal
"Net::SSLGlue::SMTP::_SSLified"->start_SSL($self, "SSL_verify_mode", 1, "SSL_verifycn_scheme", "smtp", "SSL_verifycn_name", $host, @_) or return 0 0 0
29 SSL_verify_mode => 1,
30 SSL_verifycn_scheme => 'smtp',
31 SSL_verifycn_name => $host,
32 @_
33 ) or return;
34  
35 # another hello after starttls to read new ESMTP capabilities
36 0 return $self->hello(${*$self}{net_smtp_hello_domain});
37 0 };
38  
39 *Net::SMTP::_STARTTLS = sub {
40 0 ▶ shift->command("STARTTLS")->response() == Net::SMTP::CMD_OK
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 0 };
42  
43 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
44 0 my $old_new = \&Net::SMTP::new;
45 *Net::SMTP::new = sub {
46 0 ▶ my $class = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
47 0 ▶ my %arg = @_ % 2 == 0 ? @_ : ( Host => shift,@_ );
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 == 0 ? : 0 0 0
48 0 ▶ if ( delete $arg{SSL} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $arg{'SSL'}) { } 0 0 0
49 0 ▶ $arg{Port} ||= 465;
Condition0%0/2
$arg{"Port"} ||= 465
l!l
00
MC/DC0%0/1
$arg{"Port"} ||= 465
$arg{"Port"}
Truth table0%0/2
A
A= $arg{"Port"}
Aresult
11
00
50 0 return Net::SSLGlue::SMTP::_SSLified->new(%arg);
51 } else {
52 0 return $old_new->($class,%arg);
53 }
54 0 };
55  
56 0 my $old_hello = \&Net::SMTP::hello;
57 *Net::SMTP::hello = sub {
58 0 ▶ my ($self,$domain) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
59 0 ▶ ${*$self}{net_smtp_hello_domain} = $domain if $domain;
Branch: 1 branch
BranchTrueFalseTotal
if $domain 0 0 0
60 0 goto &$old_hello;
61 0 };
62  
63 1 DONE:
64 1;
65 }
66  
67 ##############################################################################
68 # Socket class derived from IO::Socket::SSL
69 # strict certificate verification per default
70 ##############################################################################
71 1 our %SSLopts;
72 {
73 package Net::SSLGlue::SMTP::_SSL_Socket;
74 1 ▶ goto DONE if $DONT;
Branch: 1 branch
BranchTrueFalseTotal
if $DONT 1 0 1
75 0 our @ISA = 'IO::Socket::SSL';
76 *configure_SSL = sub {
77 0 ▶ my ($self,$arg_hash) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
78  
79 # set per default strict certificate verification
80 $arg_hash->{SSL_verify_mode} = 1
81 0 ▶ if ! exists $arg_hash->{SSL_verify_mode};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $arg_hash->{"SSL_verify_mode"} 0 0 0
82 $arg_hash->{SSL_verifycn_scheme} = 'smtp'
83 0 ▶ if ! exists $arg_hash->{SSL_verifycn_scheme};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $arg_hash->{"SSL_verifycn_scheme"} 0 0 0
84 $arg_hash->{SSL_verifycn_name} = $self->host
85 0 ▶ if ! exists $arg_hash->{SSL_verifycn_name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $arg_hash->{"SSL_verifycn_name"} 0 0 0
86  
87 # force keys from %SSLopts
88 0 ▶ while ( my ($k,$v) = each %SSLopts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %Net::SSLGlue::SMTP::SSLopts) 0 0 0
89 0 $arg_hash->{$k} = $v;
90 }
91 0 return $self->SUPER::configure_SSL($arg_hash)
92 0 };
93  
94 1 DONE:
95 1;
96 }
97  
98  
99 ##############################################################################
100 # Net::SMTP derived from Net::SSLGlue::SMTP::_SSL_Socket instead of IO::Socket::INET
101 # this talks SSL to the peer
102 ##############################################################################
103 {
104 1 package Net::SSLGlue::SMTP::_SSLified;
105 1 ▶ use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
106 1 ▶ goto DONE if $DONT;
Branch: 1 branch
BranchTrueFalseTotal
if $DONT 1 0 1
107  
108 # deriving does not work because we need to replace a superclass
109 # from Net::SMTP, so just copy the class into the new one and then
110 # change it
111  
112 # copy subs
113 0 for ( keys %{Net::SMTP::} ) {
114 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
115 0 ▶ *{$_} = \&{ "Net::SMTP::$_" } if defined &{ "Net::SMTP::$_" };
Branch: 1 branch
BranchTrueFalseTotal
if defined &{"Net::SMTP::$_";} 0 0 0
116 }
117  
118 # copy + fix @ISA
119 0 our @ISA = @Net::SMTP::ISA;
120 0 ▶ grep { s{^IO::Socket::INET$}{Net::SSLGlue::SMTP::_SSL_Socket} } @ISA
Branch: 1 branch
BranchTrueFalseTotal
grep {s/^IO::Socket::INET$/Net::SSLGlue::SMTP::_SSL_Socket/;} @Net::SSLGlue::SMTP::_SSLified::ISA or die "cannot find and replace IO::Socket::INET superclass" 0 0 0
121 or die "cannot find and replace IO::Socket::INET superclass";
122  
123 # we are already sslified
124 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
125 0 ▶ *starttls = sub { croak "have already TLS\n" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
126  
127 0 my $old_new = \&new;
128 *new = sub {
129 0 ▶ my $class = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
130 0 ▶ my %arg = @_ % 2 == 0 ? @_ : ( Host => shift,@_ );
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 == 0 ? : 0 0 0
131 0 local %SSLopts;
132 0 $SSLopts{$_} = delete $arg{$_} for ( grep { /^SSL_/ } keys %arg );
133 0 return $old_new->($class,%arg);
134 0 };
135  
136 1 DONE:
137 1;
138 }
139  
140 1 1;
141  
142 =head1 NAME
143  
144 Net::SSLGlue::SMTP - make Net::SMTP able to use SSL
145  
146 =head1 SYNOPSIS
147  
148 use Net::SSLGlue::SMTP;
149 my $smtp_ssl = Net::SMTP->new( $host,
150 SSL => 1,
151 SSL_ca_path => ...
152 );
153  
154 my $smtp_plain = Net::SMTP->new( $host );
155 $smtp_plain->starttls( SSL_ca_path => ... );
156  
157 =head1 DESCRIPTION
158  
159 L<Net::SSLGlue::SMTP> extends L<Net::SMTP> so one can either start directly with SSL
160 or switch later to SSL using the STARTTLS command.
161  
162 By default it will take care to verify the certificate according to the rules
163 for SMTP implemented in L<IO::Socket::SSL>.
164  
165 =head1 METHODS
166  
167 =over 4
168  
169 =item new
170  
171 The method C<new> of L<Net::SMTP> is now able to start directly with SSL when
172 the argument C<<SSL => 1>> is given. In this case it will not create an
173 L<IO::Socket::INET> object but an L<IO::Socket::SSL> object. One can give the
174 usual C<SSL_*> parameter of L<IO::Socket::SSL> to C<Net::SMTP::new>.
175  
176 =item starttls
177  
178 If the connection is not yet SSLified it will issue the STARTTLS command and
179 change the object, so that SSL will now be used. The usual C<SSL_*> parameter of
180 L<IO::Socket::SSL> will be given.
181  
182 =item peer_certificate ...
183  
184 Once the SSL connection is established the object is derived from
185 L<IO::Socket::SSL> so that you can use this method to get information about the
186 certificate. See the L<IO::Socket::SSL> documentation.
187  
188 =back
189  
190 All of these methods can take the C<SSL_*> parameter from L<IO::Socket::SSL> to
191 change the behavior of the SSL connection. The following parameters are
192 especially useful:
193  
194 =over 4
195  
196 =item SSL_ca_path, SSL_ca_file
197  
198 Specifies the path or a file where the CAs used for checking the certificates
199 are located. This is typically L</etc/ssl/certs> on UNIX systems.
200  
201 =item SSL_verify_mode
202  
203 If set to 0, verification of the certificate will be disabled. By default
204 it is set to 1 which means that the peer certificate is checked.
205  
206 =item SSL_verifycn_name
207  
208 Usually the name given as the hostname in the constructor is used to verify the
209 identity of the certificate. If you want to check the certificate against
210 another name you can specify it with this parameter.
211  
212 =back
213  
214 =head1 SEE ALSO
215  
216 IO::Socket::SSL, Net::SMTP
217  
218 =head1 COPYRIGHT
219  
220 This module is copyright (c) 2008, Steffen Ullrich.
221 All Rights Reserved.
222 This module is free software. It may be used, redistributed and/or modified
223 under the same terms as Perl itself.
224  
« SSLGlue/Socket.pm ↑ Summary SSLGlue/POP3.pm »