The SSL and TLS code for SMTP is non-trivial, so refactor it into a separate function for ease of use. Handle both files and directories as sources for CA certificates. Also add handling for older version of IO::Socket::SSL that do not support the SSL_VERIFY_PEER and SSL_VERIFY_NONE constants; in this case, print a warning and inform the user of this fact. Signed-off-by: brian m. carlson <sandals@xxxxxxxxxxxxxxxxxxxx> --- This is completely untested. I used perl -c, but that's it. git-send-email.perl | 50 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/git-send-email.perl b/git-send-email.perl index 095b6fb..11fb2d0 100755 --- a/git-send-email.perl +++ b/git-send-email.perl @@ -1083,6 +1083,37 @@ sub smtp_auth_maybe { return $auth; } +sub ssl_verify_params { + require IO::Socket::SSL; + eval { + IO::Socket::SSL->import(qw/SSL_VERIFY_PEER SSL_VERIFY_NONE/); + }; + if ($@) { + print STDERR "Not using SSL_VERIFY_PEER due to out-of-date IO::Socket::SSL.\n"; + return; + } + + if (!defined $smtp_ssl_cert_path) { + $smtp_ssl_cert_path ||= "/etc/ssl/certs"; + } + + if (!$smtp_ssl_cert_path) { + return (SSL_verify_mode => SSL_VERIFY_NONE()); + } + elsif (-d $smtp_ssl_cert_path) { + return (SSL_verify_mode => SSL_VERIFY_PEER(), + SSL_ca_path => $smtp_ssl_cert_path); + } + elsif (-f $smtp_ssl_cert_path) { + return (SSL_verify_mode => SSL_VERIFY_PEER(), + SSL_ca_file => $smtp_ssl_cert_path); + } + else { + print STDERR "Not using SSL_VERIFY_PEER because the CA path does not exist.\n"; + return (SSL_verify_mode => SSL_VERIFY_NONE()); + } +} + # Returns 1 if the message was sent, and 0 otherwise. # In actuality, the whole program dies when there # is an error sending a message. @@ -1187,7 +1218,8 @@ X-Mailer: git-send-email $gitversion $smtp_domain ||= maildomain(); $smtp ||= Net::SMTP::SSL->new($smtp_server, Hello => $smtp_domain, - Port => $smtp_server_port); + Port => $smtp_server_port, + ssl_verify_params()); } else { require Net::SMTP; @@ -1203,19 +1235,9 @@ X-Mailer: git-send-email $gitversion $smtp->command('STARTTLS'); $smtp->response(); if ($smtp->code == 220) { - # Attempt to use a ca-certificate by default - $smtp_ssl_cert_path |= "/etc/ssl/certs"; - if (-d $smtp_ssl_cert_path) { - $smtp = Net::SMTP::SSL->start_SSL($smtp, - SSL_verify_mode => SSL_VERIFY_PEER, - SSL_ca_path => $smtp_ssl_cert_path) - or die "STARTTLS failed! ".$smtp->message; - } else { - print STDERR "warning: Using SSL_VERIFY_NONE. See sendemail.smtpsslcertpath.\n"; - $smtp = Net::SMTP::SSL->start_SSL($smtp, - SSL_verify_mode => SSL_VERIFY_NONE) - or die "STARTTLS failed! ".$smtp->message; - } + $smtp = Net::SMTP::SSL->start_SSL($smtp, + ssl_verify_params()) + or die "STARTTLS failed! ".$smtp->message; $smtp_encryption = ''; # Send EHLO again to receive fresh # supported commands -- 1.8.3.2.923.g2a18ff8.dirty -- brian m. carlson / brian with sandals: Houston, Texas, US +1 832 623 2791 | http://www.crustytoothpaste.net/~bmc | My opinion only OpenPGP: RSA v4 4096b: 88AC E9B2 9196 305B A994 7552 F1BA 225C 0223 B187 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html