diff options
author | Steffen Ullrich <Steffen_Ullrich@genua.de> | 2014-05-16 22:18:48 +0200 |
---|---|---|
committer | Steffen Ullrich <Steffen_Ullrich@genua.de> | 2014-05-16 22:18:48 +0200 |
commit | c7d5842b0010f9f2ff01394ffac75df8256eda08 (patch) | |
tree | a87e8d5291012ee07ce9a7f8a9c952fc6b683cbf | |
parent | 34436780081aa8793b16f51864bce42a7cdd7e8b (diff) | |
download | perl-libnet-c7d5842b0010f9f2ff01394ffac75df8256eda08.tar.gz |
SSL and IPv6 support for POP3 incl. tests
-rw-r--r-- | MANIFEST | 2 | ||||
-rw-r--r-- | Net/POP3.pm | 81 | ||||
-rw-r--r-- | Net/SMTP.pm | 11 | ||||
-rw-r--r-- | t/pop3_ipv6.pl | 57 | ||||
-rw-r--r-- | t/pop3_ssl.t | 119 | ||||
-rw-r--r-- | t/smtp_ipv6.pl | 2 |
6 files changed, 263 insertions, 9 deletions
@@ -39,5 +39,7 @@ t/require.t t/smtp.t t/smtp_ssl.t t/smtp_ipv6.t +t/pop3_ssl.t +t/pop3_ipv6.t t/time.t t/external/smtp-ssl.t diff --git a/Net/POP3.pm b/Net/POP3.pm index 4b94a11..22c5f84 100644 --- a/Net/POP3.pm +++ b/Net/POP3.pm @@ -16,6 +16,31 @@ use Net::Config; $VERSION = "2.31"; @ISA = qw(Net::Cmd IO::Socket::INET); +# Code for detecting if we can use SSL +my $ssl_class = eval { + require IO::Socket::SSL; + # first version with default CA on most platforms + IO::Socket::SSL->VERSION(1.968); +} && 'IO::Socket::SSL'; +my $nossl_warn = !$ssl_class && + 'To use SSL please install IO::Socket::SSL with version>=1.968'; + +# Code for detecting if we can use IPv6 +my $inet6_class = + eval { + require IO::Socket::IP; + IO::Socket::IP->VERSION(0.20); + } && 'IO::Socket::IP' || + eval { + require IO::Socket::INET6; + IO::Socket::INET6->VERSION(2.62); + } && 'IO::Socket::INET6'; + +sub can_ssl { $ssl_class }; +sub can_inet6 { $inet6_class }; + + +@ISA = ( 'Net::Cmd', $inet6_class || 'IO::Socket::INET' ); sub new { @@ -34,6 +59,14 @@ sub new { my $obj; my @localport = exists $arg{ResvPort} ? (LocalPort => $arg{ResvPort}) : (); + if ($arg{SSL}) { + # SSL from start + die $nossl_warn if !$ssl_class; + $arg{Port} ||= 995; + } + + $arg{Timeout} = 120 if ! defined $arg{Timeout}; + my $h; foreach $h (@{$hosts}) { $obj = $type->SUPER::new( @@ -41,9 +74,7 @@ sub new { PeerPort => $arg{Port} || 'pop3(110)', Proto => 'tcp', @localport, - Timeout => defined $arg{Timeout} - ? $arg{Timeout} - : 120 + Timeout => $arg{Timeout}, ) and last; } @@ -51,6 +82,13 @@ sub new { return undef unless defined $obj; + ${*$obj}{'net_pop3_arg'} = \%arg; + if ($arg{SSL}) { + Net::POP3::_SSLified->start_SSL($obj, + SSL_verifycn_name => $host,%arg + ) or return; + } + ${*$obj}{'net_pop3_host'} = $host; $obj->autoflush(1); @@ -93,6 +131,16 @@ sub login { and $me->pass($pass); } +sub starttls { + my $self = shift; + $ssl_class or die $nossl_warn; + $self->_STLS or return; + Net::POP3::_SSLified->start_SSL($self, + %{ ${*$self}{'net_pop3_arg'} }, # (ssl) args given in new + @_ # more (ssl) args + ) or return; + return 1; +} sub apop { @_ >= 1 && @_ <= 3 or croak 'usage: $pop3->apop( USER, PASS )'; @@ -323,6 +371,7 @@ sub _PING { shift->command('PING', $_[0])->response() == CMD_OK } sub _RPOP { shift->command('RPOP', $_[0])->response() == CMD_OK } sub _LAST { shift->command('LAST' )->response() == CMD_OK } sub _CAPA { shift->command('CAPA' )->response() == CMD_OK } +sub _STLS { shift->command("STLS", )->response() == CMD_OK } sub quit { @@ -520,6 +569,24 @@ sub banner { return ${*$this}{'net_pop3_banner'}; } +{ + package Net::POP3::_SSLified; + our @ISA = ( $ssl_class ? ($ssl_class):(), 'Net::POP3' ); + sub starttls { die "POP3 connection is already in SSL mode" } + sub start_SSL { + my ($class,$pop3,%arg) = @_; + delete @arg{ grep { !m{^SSL_} } keys %arg }; + ( $arg{SSL_verifycn_name} ||= $pop3->host ) + =~s{(?<!:):[\w()]+$}{}; # strip port + $arg{SSL_verifycn_scheme} ||= 'pop3'; + my $ok = $class->SUPER::start_SSL($pop3,%arg); + $@ = $ssl_class->errstr if !$ok; + return $ok; + } +} + + + 1; __END__ @@ -580,6 +647,14 @@ the C<PeerAddr> option in L<IO::Socket::INET>, or a reference to an array with hosts to try in turn. The L</host> method will return the value which was used to connect to the host. +B<Port> - port to connect to. +Default - 110 for plain POP3 and 995 for POP3s (direct SSL). + +B<SSL> - If the connection should be done from start with SSL, contrary to later +upgrade with C<starttls>. +You can use SSL arguments as documented in L<IO::Socket::SSL>, but it will +usually use the right arguments already. + B<ResvPort> - If given then the socket for the C<Net::POP3> object will be bound to the local port given using C<bind> when the socket is created. diff --git a/Net/SMTP.pm b/Net/SMTP.pm index 358afa6..6be2952 100644 --- a/Net/SMTP.pm +++ b/Net/SMTP.pm @@ -67,6 +67,8 @@ sub new { my $hosts = defined $host ? $host : $NetConfig{smtp_hosts}; my $obj; + $arg{Timeout} = 120 if ! defined $arg{Timeout}; + my $h; foreach $h (@{ref($hosts) ? $hosts : [$hosts]}) { $obj = $type->SUPER::new( @@ -75,9 +77,7 @@ sub new { LocalAddr => $arg{LocalAddr}, LocalPort => $arg{LocalPort}, Proto => 'tcp', - Timeout => defined $arg{Timeout} - ? $arg{Timeout} - : 120 + Timeout => $arg{Timeout} ) and last; } @@ -582,7 +582,7 @@ sub _BDAT { shift->command("BDAT", @_) } sub _TURN { shift->unsupported(@_); } sub _ETRN { shift->command("ETRN", @_)->response() == CMD_OK } sub _AUTH { shift->command("AUTH", @_)->response() == CMD_OK } -sub _STARTTLS { shift->command("STARTTLS", @_)->response() == CMD_OK } +sub _STARTTLS { shift->command("STARTTLS")->response() == CMD_OK } { @@ -692,8 +692,9 @@ B<Host> - SMTP host to connect to. It may be a single scalar (hostname[:port]), as defined for the C<PeerAddr> option in L<IO::Socket::INET>, or a reference to an array with hosts to try in turn. The L</host> method will return the value which was used to connect to the host. +Format - C<PeerHost> from L<IO::Socket::INET> new method. -B<Port> - port to connect to. Format - C<PeerHost> from L<IO::Socket::INET> new method. +B<Port> - port to connect to. Default - 25 for plain SMTP and 465 for immediate SSL. B<SSL> - If the connection should be done from start with SSL, contrary to later diff --git a/t/pop3_ipv6.pl b/t/pop3_ipv6.pl new file mode 100644 index 0000000..2f073ab --- /dev/null +++ b/t/pop3_ipv6.pl @@ -0,0 +1,57 @@ +use strict; +use warnings; +use Test::More; +use File::Temp 'tempfile'; +use Net::POP3; + +my $debug = 0; # Net::POP3->new( Debug => .. ) + +my $inet6class = Net::POP3->can_inet6; +plan skip_all => "no IPv6 support found in Net::POP3" if ! $inet6class; + +plan skip_all => "fork not supported on this platform" + if grep { $^O =~m{$_} } qw(MacOS VOS vmesa riscos amigaos); + +my $srv = $inet6class->new( + LocalAddr => '::1', + Listen => 10 +); +plan skip_all => "cannot create listener on ::1: $!" if ! $srv; +my $saddr = "[".$srv->sockhost."]".':'.$srv->sockport; +diag("server on $saddr"); + +plan tests => 1; + +defined( my $pid = fork()) or die "fork failed: $!"; +exit(pop3_server()) if ! $pid; + +my $cl = Net::POP3->new($saddr, Debug => $debug); +diag("created Net::POP3 object"); +if (!$cl) { + fail("IPv6 POP3 connect failed"); +} else { + $cl->quit; + pass("IPv6 success"); +} +wait; + +sub pop3_server { + my $cl = $srv->accept or die "accept failed: $!"; + print $cl "+OK localhost ready\r\n"; + while (<$cl>) { + my ($cmd,$arg) = m{^(\S+)(?: +(.*))?\r\n} or die $_; + $cmd = uc($cmd); + if ($cmd eq 'QUIT' ) { + print $cl "+OK bye\r\n"; + last; + } elsif ( $cmd eq 'CAPA' ) { + print $cl "+OK\r\n". + ".\r\n"; + } else { + diag("received unknown command: $cmd"); + print "-ERR unknown cmd\r\n"; + } + } + + diag("POP3 dialog done"); +} diff --git a/t/pop3_ssl.t b/t/pop3_ssl.t new file mode 100644 index 0000000..f1090af --- /dev/null +++ b/t/pop3_ssl.t @@ -0,0 +1,119 @@ +use strict; +use warnings; +use Test::More; +use File::Temp 'tempfile'; +use Net::POP3; + +my $debug = 0; # Net::POP3 Debug => .. + +plan skip_all => "no SSL support found in Net::POP3" if ! Net::POP3->can_ssl; + +plan skip_all => "fork not supported on this platform" + if grep { $^O =~m{$_} } qw(MacOS VOS vmesa riscos amigaos); + +plan skip_all => "incomplete or to old version of IO::Socket::SSL" if ! eval { + require IO::Socket::SSL + && IO::Socket::SSL->VERSION(1.968) + && require IO::Socket::SSL::Utils + && defined &IO::Socket::SSL::Utils::CERT_create; +}; + +my $srv = IO::Socket::INET->new( + LocalAddr => '127.0.0.1', + Listen => 10 +); +plan skip_all => "cannot create listener on localhost: $!" if ! $srv; +my $saddr = $srv->sockhost.':'.$srv->sockport; + +plan tests => 2; + +my ($ca,$key) = IO::Socket::SSL::Utils::CERT_create( CA => 1 ); +my ($fh,$cafile) = tempfile(); +print $fh IO::Socket::SSL::Utils::PEM_cert2string($ca); +close($fh); + +my $parent = $$; +END { unlink($cafile) if $$ == $parent } + +my ($cert) = IO::Socket::SSL::Utils::CERT_create( + subject => { CN => 'pop3.example.com' }, + issuer_cert => $ca, issuer_key => $key, + key => $key +); + +test(1); # direct ssl +test(0); # starttls + + +sub test { + my $ssl = shift; + defined( my $pid = fork()) or die "fork failed: $!"; + exit(pop3_server($ssl)) if ! $pid; + pop3_client($ssl); + wait; +} + + +sub pop3_client { + my $ssl = shift; + my %sslopt = ( + SSL_verifycn_name => 'pop3.example.com', + SSL_ca_file => $cafile + ); + $sslopt{SSL} = 1 if $ssl; + my $cl = Net::POP3->new($saddr, %sslopt, Debug => $debug); + diag("created Net::POP3 object"); + if (!$cl) { + fail( ($ssl ? "SSL ":"" )."POP3 connect failed"); + } elsif ($ssl) { + $cl->quit; + pass("SSL POP3 connect success"); + } elsif ( ! $cl->starttls ) { + fail("starttls failed: $IO::Socket::SSL::SSL_ERROR"); + } else { + $cl->quit; + pass("starttls success"); + } +} + +sub pop3_server { + my $ssl = shift; + my $cl = $srv->accept or die "accept failed: $!"; + my %sslargs = ( + SSL_server => 1, + SSL_cert => $cert, + SSL_key => $key, + ); + if ( $ssl ) { + if ( ! IO::Socket::SSL->start_SSL($cl, %sslargs)) { + diag("initial ssl handshake with client failed"); + return; + } + } + + print $cl "+OK localhost ready\r\n"; + while (<$cl>) { + my ($cmd,$arg) = m{^(\S+)(?: +(.*))?\r\n} or die $_; + $cmd = uc($cmd); + if ($cmd eq 'QUIT' ) { + print $cl "+OK bye\r\n"; + last; + } elsif ( $cmd eq 'CAPA' ) { + print $cl "+OK\r\n". + ( $ssl ? "" : "STLS\r\n" ). + ".\r\n"; + } elsif ( ! $ssl and $cmd eq 'STLS' ) { + print $cl "+OK starting ssl\r\n"; + if ( ! IO::Socket::SSL->start_SSL($cl, %sslargs)) { + diag("initial ssl handshake with client failed"); + return; + } + $ssl = 1; + } else { + diag("received unknown command: $cmd"); + print "-ERR unknown cmd\r\n"; + } + } + + diag("POP3 dialog done"); +} diff --git a/t/smtp_ipv6.pl b/t/smtp_ipv6.pl index b34b84d..6a01520 100644 --- a/t/smtp_ipv6.pl +++ b/t/smtp_ipv6.pl @@ -4,7 +4,7 @@ use Test::More; use File::Temp 'tempfile'; use Net::SMTP; -my $debug = 1; # Net::SMTP->new( Debug => .. ) +my $debug = 0; # Net::SMTP->new( Debug => .. ) my $inet6class = Net::SMTP->can_inet6; plan skip_all => "no IPv6 support found in Net::SMTP" if ! $inet6class; |