SSLGlue/POP3.pm untested

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