13dfe3
From d3ad0a08dbd9e32a498a5f3335d521d6c38f4f1f Mon Sep 17 00:00:00 2001
13dfe3
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
13dfe3
Date: Wed, 21 Mar 2018 13:21:42 +0100
13dfe3
Subject: [PATCH 1/4] SSL support for Net::SMTP
13dfe3
MIME-Version: 1.0
13dfe3
Content-Type: text/plain; charset=UTF-8
13dfe3
Content-Transfer-Encoding: 8bit
13dfe3
13dfe3
Ported to Net::SMTP 2.31 as found in Perl 5.16.3 from libnet upstream:
13dfe3
13dfe3
commit 34436780081aa8793b16f51864bce42a7cdd7e8b
13dfe3
Author: Steffen Ullrich <Steffen_Ullrich@genua.de>
13dfe3
Date:   Fri May 16 21:55:20 2014 +0200
13dfe3
13dfe3
    added tests for Net::SMTP SSL and IPv6, save arguments in Net::SMTP to apply them on starttls
13dfe3
13dfe3
IPv6 support exluded.
13dfe3
13dfe3
Check for IO::Socket::SSL version removed because we have default sane
13dfe3
default CA list since perl-IO-Socket-SSL-1.94-7.el7. We also set
13dfe3
SSL_verify_mode => SSL_VERIFY_PEER by default.
13dfe3
13dfe3
Signed-off-by: Petr Písař <ppisar@redhat.com>
13dfe3
---
13dfe3
 cpan/libnet/Net/Cmd.pm  |  1 -
13dfe3
 cpan/libnet/Net/SMTP.pm | 72 +++++++++++++++++++++++++++++++++++++++++++++++++
13dfe3
 2 files changed, 72 insertions(+), 1 deletion(-)
13dfe3
13dfe3
diff --git a/cpan/libnet/Net/Cmd.pm b/cpan/libnet/Net/Cmd.pm
13dfe3
index 4f0e444..29eeb14 100644
13dfe3
--- a/cpan/libnet/Net/Cmd.pm
13dfe3
+++ b/cpan/libnet/Net/Cmd.pm
13dfe3
@@ -53,7 +53,6 @@ my %debug = ();
13dfe3
 
13dfe3
 my $tr = $^O eq 'os390' ? Convert::EBCDIC->new() : undef;
13dfe3
 
13dfe3
-
13dfe3
 sub toebcdic {
13dfe3
   my $cmd = shift;
13dfe3
 
13dfe3
diff --git a/cpan/libnet/Net/SMTP.pm b/cpan/libnet/Net/SMTP.pm
13dfe3
index a28496d..6143990 100644
13dfe3
--- a/cpan/libnet/Net/SMTP.pm
13dfe3
+++ b/cpan/libnet/Net/SMTP.pm
13dfe3
@@ -18,6 +18,15 @@ use Net::Config;
13dfe3
 
13dfe3
 $VERSION = "2.31";
13dfe3
 
13dfe3
+# Code for detecting if we can use SSL
13dfe3
+my $ssl_class = eval {
13dfe3
+  require IO::Socket::SSL;
13dfe3
+} && 'IO::Socket::SSL';
13dfe3
+my $nossl_warn = !$ssl_class &&
13dfe3
+  'To use SSL please install IO::Socket::SSL';
13dfe3
+
13dfe3
+sub can_ssl   { $ssl_class };
13dfe3
+
13dfe3
 @ISA = qw(Net::Cmd IO::Socket::INET);
13dfe3
 
13dfe3
 
13dfe3
@@ -33,6 +42,13 @@ sub new {
13dfe3
     %arg  = @_;
13dfe3
     $host = delete $arg{Host};
13dfe3
   }
13dfe3
+
13dfe3
+  if ($arg{SSL}) {
13dfe3
+    # SSL from start
13dfe3
+    die $nossl_warn if !$ssl_class;
13dfe3
+    $arg{Port} ||= 465;
13dfe3
+  }
13dfe3
+
13dfe3
   my $hosts = defined $host ? $host : $NetConfig{smtp_hosts};
13dfe3
   my $obj;
13dfe3
 
13dfe3
@@ -54,6 +70,11 @@ sub new {
13dfe3
   return undef
13dfe3
     unless defined $obj;
13dfe3
 
13dfe3
+  if ($arg{SSL}) {
13dfe3
+    Net::SMTP::_SSLified->start_SSL($obj,SSL_verifycn_name => $host,%arg)
13dfe3
+      or return;
13dfe3
+  }
13dfe3
+
13dfe3
   $obj->autoflush(1);
13dfe3
 
13dfe3
   $obj->debug(exists $arg{Debug} ? $arg{Debug} : undef);
13dfe3
@@ -185,11 +206,22 @@ sub hello {
13dfe3
   }
13dfe3
 
13dfe3
   return undef unless $ok;
13dfe3
+  ${*$me}{net_smtp_hello_domain} = $domain;
13dfe3
 
13dfe3
   $msg[0] =~ /\A\s*(\S+)/;
13dfe3
   return ($1 || " ");
13dfe3
 }
13dfe3
 
13dfe3
+sub starttls {
13dfe3
+  my $self = shift;
13dfe3
+  $ssl_class or die $nossl_warn;
13dfe3
+  $self->_STARTTLS or return;
13dfe3
+  Net::SMTP::_SSLified->start_SSL($self,@_) or return;
13dfe3
+
13dfe3
+  # another hello after starttls to read new ESMTP capabilities
13dfe3
+  return $self->hello(${*$self}{net_smtp_hello_domain});
13dfe3
+}
13dfe3
+
13dfe3
 
13dfe3
 sub supports {
13dfe3
   my $self = shift;
13dfe3
@@ -527,6 +559,27 @@ sub _BDAT { shift->command("BDAT", @_) }
13dfe3
 sub _TURN { shift->unsupported(@_); }
13dfe3
 sub _ETRN { shift->command("ETRN", @_)->response() == CMD_OK }
13dfe3
 sub _AUTH { shift->command("AUTH", @_)->response() == CMD_OK }
13dfe3
+sub _STARTTLS { shift->command("STARTTLS", @_)->response() == CMD_OK }
13dfe3
+
13dfe3
+
13dfe3
+{
13dfe3
+  package Net::SMTP::_SSLified;
13dfe3
+  our @ISA = ( $ssl_class ? ($ssl_class):(), 'Net::SMTP' );
13dfe3
+  sub starttls { die "SMTP connection is already in SSL mode" }
13dfe3
+  sub start_SSL {
13dfe3
+    my ($class,$smtp,%arg) = @_;
13dfe3
+    delete @arg{ grep { !m{^SSL_} } keys %arg };
13dfe3
+    ( $arg{SSL_verifycn_name} ||= $smtp->host )
13dfe3
+	=~s{(?
13dfe3
+    $arg{SSL_verifycn_scheme} ||= 'smtp';
13dfe3
+    $arg{SSL_verify_mode} ||= IO::Socket::SSL::SSL_VERIFY_PEER();
13dfe3
+    my $ok = $class->SUPER::start_SSL($smtp,%arg);
13dfe3
+    $@ = $ssl_class->errstr if !$ok;
13dfe3
+    return $ok;
13dfe3
+  }
13dfe3
+}
13dfe3
+
13dfe3
+
13dfe3
 
13dfe3
 1;
13dfe3
 
13dfe3
@@ -613,6 +666,11 @@ the C<PeerAddr> option in L<IO::Socket::INET>, or a reference to
13dfe3
 an array with hosts to try in turn. The L</host> method will return the value
13dfe3
 which was used to connect to the host.
13dfe3
 
13dfe3
+B<SSL> - If the connection should be done from start with SSL, contrary to later
13dfe3
+upgrade with C<starttls>.
13dfe3
+You can use SSL arguments as documented in L<IO::Socket::SSL>, but it will
13dfe3
+usually use the right arguments already.
13dfe3
+
13dfe3
 B<LocalAddr> and B<LocalPort> - These parameters are passed directly
13dfe3
 to IO::Socket to allow binding the socket to a local port.
13dfe3
 
13dfe3
@@ -643,6 +701,14 @@ Example:
13dfe3
                            Debug   => 1,
13dfe3
 			  );
13dfe3
 
13dfe3
+    # the same with direct SSL
13dfe3
+    $smtp = Net::SMTP->new('mailhost',
13dfe3
+			   Hello => 'my.mail.domain',
13dfe3
+			   Timeout => 30,
13dfe3
+			   Debug   => 1,
13dfe3
+			   SSL     => 1,
13dfe3
+			  );
13dfe3
+
13dfe3
     # Connect to the default server from Net::config
13dfe3
     $smtp = Net::SMTP->new(
13dfe3
 			   Hello => 'my.mail.domain',
13dfe3
@@ -686,6 +752,12 @@ to connect to the host.
13dfe3
 
13dfe3
 Request a queue run for the DOMAIN given.
13dfe3
 
13dfe3
+=item starttls ( SSLARGS )
13dfe3
+
13dfe3
+Upgrade existing plain connection to SSL.
13dfe3
+You can use SSL arguments as documented in L<IO::Socket::SSL>, but it will
13dfe3
+usually use the right arguments already.
13dfe3
+
13dfe3
 =item auth ( USERNAME, PASSWORD )
13dfe3
 
13dfe3
 Attempt SASL authentication.
13dfe3
-- 
13dfe3
2.14.3
13dfe3