fb6d68
From 1d3aa3540da1a751c047e84fbc5dca793e411b2e Mon Sep 17 00:00:00 2001
fb6d68
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
fb6d68
Date: Wed, 21 Mar 2018 13:48:53 +0100
fb6d68
Subject: [PATCH 2/4] added tests for Net::SMTP SSL, save arguments in
fb6d68
 Net::SMTP to apply them on starttls
fb6d68
MIME-Version: 1.0
fb6d68
Content-Type: text/plain; charset=UTF-8
fb6d68
Content-Transfer-Encoding: 8bit
fb6d68
fb6d68
Ported to Net::SMTP 2.31 as found in Perl 5.16.3 from libnet upstream:
fb6d68
fb6d68
commit 34436780081aa8793b16f51864bce42a7cdd7e8b
fb6d68
Author: Steffen Ullrich <Steffen_Ullrich@genua.de>
fb6d68
Date:   Fri May 16 21:55:20 2014 +0200
fb6d68
fb6d68
    added tests for Net::SMTP SSL and IPv6, save arguments in Net::SMTP to apply them on starttls
fb6d68
fb6d68
IPv6 support excluded.
fb6d68
fb6d68
Check for IO::Socket::SSL version removed because we have default sane
fb6d68
default CA list since perl-IO-Socket-SSL-1.94-7.el7.
fb6d68
fb6d68
Signed-off-by: Petr Písař <ppisar@redhat.com>
fb6d68
---
fb6d68
 MANIFEST                 |   1 +
fb6d68
 cpan/libnet/Net/SMTP.pm  |   6 ++-
fb6d68
 cpan/libnet/t/smtp_ssl.t | 120 +++++++++++++++++++++++++++++++++++++++++++++++
fb6d68
 3 files changed, 126 insertions(+), 1 deletion(-)
fb6d68
 create mode 100644 cpan/libnet/t/smtp_ssl.t
fb6d68
fb6d68
diff --git a/MANIFEST b/MANIFEST
fb6d68
index b649ddb..aa117d3 100644
fb6d68
--- a/MANIFEST
fb6d68
+++ b/MANIFEST
fb6d68
@@ -1456,6 +1456,7 @@ cpan/libnet/t/libnet_t.pl	libnet
fb6d68
 cpan/libnet/t/netrc.t		libnet
fb6d68
 cpan/libnet/t/nntp.t		libnet
fb6d68
 cpan/libnet/t/require.t		libnet
fb6d68
+cpan/libnet/t/smtp_ssl.t	libnet
fb6d68
 cpan/libnet/t/smtp.t		libnet
fb6d68
 cpan/libnet/t/time.t		libnet
fb6d68
 cpan/List-Util/Changes			Util extension
fb6d68
diff --git a/cpan/libnet/Net/SMTP.pm b/cpan/libnet/Net/SMTP.pm
fb6d68
index 6143990..7dbf3df 100644
fb6d68
--- a/cpan/libnet/Net/SMTP.pm
fb6d68
+++ b/cpan/libnet/Net/SMTP.pm
fb6d68
@@ -70,6 +70,7 @@ sub new {
fb6d68
   return undef
fb6d68
     unless defined $obj;
fb6d68
 
fb6d68
+  ${*$obj}{'net_smtp_arg'} = \%arg;
fb6d68
   if ($arg{SSL}) {
fb6d68
     Net::SMTP::_SSLified->start_SSL($obj,SSL_verifycn_name => $host,%arg)
fb6d68
       or return;
fb6d68
@@ -216,7 +217,10 @@ sub starttls {
fb6d68
   my $self = shift;
fb6d68
   $ssl_class or die $nossl_warn;
fb6d68
   $self->_STARTTLS or return;
fb6d68
-  Net::SMTP::_SSLified->start_SSL($self,@_) or return;
fb6d68
+  Net::SMTP::_SSLified->start_SSL($self,
fb6d68
+    %{ ${*$self}{'net_smtp_arg'} }, # (ssl) args given in new
fb6d68
+    @_   # more (ssl) args
fb6d68
+  ) or return;
fb6d68
 
fb6d68
   # another hello after starttls to read new ESMTP capabilities
fb6d68
   return $self->hello(${*$self}{net_smtp_hello_domain});
fb6d68
diff --git a/cpan/libnet/t/smtp_ssl.t b/cpan/libnet/t/smtp_ssl.t
fb6d68
new file mode 100644
fb6d68
index 0000000..e817d00
fb6d68
--- /dev/null
fb6d68
+++ b/cpan/libnet/t/smtp_ssl.t
fb6d68
@@ -0,0 +1,120 @@
fb6d68
+use strict;
fb6d68
+use warnings;
fb6d68
+use Test::More;
fb6d68
+use File::Temp 'tempfile';
fb6d68
+use Net::SMTP;
fb6d68
+
fb6d68
+my $debug = 0; # Net::SMTP Debug => ..
fb6d68
+
fb6d68
+plan skip_all => "no SSL support found in Net::SMTP" if ! Net::SMTP->can_ssl;
fb6d68
+
fb6d68
+plan skip_all => "fork not supported on this platform"
fb6d68
+  if grep { $^O =~m{$_} } qw(MacOS VOS vmesa riscos amigaos);
fb6d68
+
fb6d68
+plan skip_all => "incomplete or to old version of IO::Socket::SSL" if ! eval {
fb6d68
+  require IO::Socket::SSL
fb6d68
+    && require IO::Socket::SSL::Utils
fb6d68
+    && defined &IO::Socket::SSL::Utils::CERT_create;
fb6d68
+};
fb6d68
+
fb6d68
+my $srv = IO::Socket::INET->new(
fb6d68
+  LocalAddr => '127.0.0.1',
fb6d68
+  Listen => 10
fb6d68
+);
fb6d68
+plan skip_all => "cannot create listener on localhost: $!" if ! $srv;
fb6d68
+my $saddr = $srv->sockhost.':'.$srv->sockport;
fb6d68
+
fb6d68
+plan tests => 2;
fb6d68
+
fb6d68
+my ($ca,$key) = IO::Socket::SSL::Utils::CERT_create( CA => 1 );
fb6d68
+my ($fh,$cafile) = tempfile();
fb6d68
+print $fh IO::Socket::SSL::Utils::PEM_cert2string($ca);
fb6d68
+close($fh);
fb6d68
+
fb6d68
+my $parent = $$;
fb6d68
+END { unlink($cafile) if $$ == $parent }
fb6d68
+
fb6d68
+my ($cert) = IO::Socket::SSL::Utils::CERT_create(
fb6d68
+  subject => { CN => 'smtp.example.com' },
fb6d68
+  issuer_cert => $ca, issuer_key => $key,
fb6d68
+  key => $key
fb6d68
+);
fb6d68
+
fb6d68
+test(1); # direct ssl
fb6d68
+test(0); # starttls
fb6d68
+
fb6d68
+
fb6d68
+sub test {
fb6d68
+  my $ssl = shift;
fb6d68
+  defined( my $pid = fork()) or die "fork failed: $!";
fb6d68
+  exit(smtp_server($ssl)) if ! $pid;
fb6d68
+  smtp_client($ssl);
fb6d68
+  wait;
fb6d68
+}
fb6d68
+
fb6d68
+
fb6d68
+sub smtp_client {
fb6d68
+  my $ssl = shift;
fb6d68
+  my %sslopt = (
fb6d68
+    SSL_verifycn_name => 'smtp.example.com',
fb6d68
+    SSL_ca_file => $cafile
fb6d68
+  );
fb6d68
+  $sslopt{SSL} = 1 if $ssl;
fb6d68
+  my $cl = Net::SMTP->new($saddr, %sslopt, Debug => $debug);
fb6d68
+  diag("created Net::SMTP object");
fb6d68
+  if (!$cl) {
fb6d68
+    fail( ($ssl ? "SSL ":"" )."SMTP connect failed");
fb6d68
+  } elsif ($ssl) {
fb6d68
+    $cl->quit;
fb6d68
+    pass("SSL SMTP connect success");
fb6d68
+  } elsif ( ! $cl->starttls ) {
fb6d68
+    fail("starttls failed: $IO::Socket::SSL::SSL_ERROR");
fb6d68
+  } else {
fb6d68
+    $cl->quit;
fb6d68
+    pass("starttls success");
fb6d68
+  }
fb6d68
+}
fb6d68
+
fb6d68
+sub smtp_server {
fb6d68
+  my $ssl = shift;
fb6d68
+  my $cl = $srv->accept or die "accept failed: $!";
fb6d68
+  my %sslargs = (
fb6d68
+    SSL_server => 1,
fb6d68
+    SSL_cert => $cert,
fb6d68
+    SSL_key => $key,
fb6d68
+  );
fb6d68
+  if ( $ssl ) {
fb6d68
+    if ( ! IO::Socket::SSL->start_SSL($cl, %sslargs)) {
fb6d68
+      diag("initial ssl handshake with client failed");
fb6d68
+      return;
fb6d68
+    }
fb6d68
+  }
fb6d68
+
fb6d68
+  print $cl "220 welcome\r\n";
fb6d68
+  while (<$cl>) {
fb6d68
+    my ($cmd,$arg) = m{^(\S+)(?: +(.*))?\r\n} or die $_;
fb6d68
+    $cmd = uc($cmd);
fb6d68
+    if ($cmd eq 'QUIT' ) {
fb6d68
+      print $cl "250 bye\r\n";
fb6d68
+      last;
fb6d68
+    } elsif ( $cmd eq 'HELO' ) {
fb6d68
+      print $cl "250 localhost\r\n";
fb6d68
+    } elsif ( $cmd eq 'EHLO' ) {
fb6d68
+      print $cl "250-localhost\r\n".
fb6d68
+	( $ssl ? "" : "250-STARTTLS\r\n" ).
fb6d68
+	"250 HELP\r\n";
fb6d68
+    } elsif ( ! $ssl and $cmd eq 'STARTTLS' ) {
fb6d68
+      print $cl "250 starting ssl\r\n";
fb6d68
+      if ( ! IO::Socket::SSL->start_SSL($cl, %sslargs)) {
fb6d68
+	diag("initial ssl handshake with client failed");
fb6d68
+	return;
fb6d68
+      }
fb6d68
+      $ssl = 1;
fb6d68
+    } else {
fb6d68
+      diag("received unknown command: $cmd");
fb6d68
+      print "500 unknown cmd\r\n";
fb6d68
+    }
fb6d68
+  }
fb6d68
+
fb6d68
+  diag("SMTP dialog done");
fb6d68
+}
fb6d68
-- 
fb6d68
2.14.3
fb6d68