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