<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">use strict;
use warnings;

package Net::SSLGlue::POP3;
use IO::Socket::SSL 1.19;
use Net::POP3;
our $VERSION = 0.91;

##############################################################################
# mix starttls method into Net::POP3 which on SSL handshake success 
# upgrades the class to Net::POP3::_SSLified
##############################################################################
sub Net::POP3::starttls {
	my $self = shift;
	$self-&gt;_STLS or return;
	my $host = $self-&gt;host;
	# for name verification strip port from domain:port, ipv4:port, [ipv6]:port
	$host =~s{(?&lt;!:):\d+$}{};

	Net::POP3::_SSLified-&gt;start_SSL( $self,
		SSL_verify_mode =&gt; 1,
		SSL_verifycn_scheme =&gt; 'pop3',
		SSL_verifycn_name =&gt; $host,
		@_ 
	) or return;
}
sub Net::POP3::_STLS { 
	shift-&gt;command("STLS")-&gt;response() == Net::POP3::CMD_OK
}

no warnings 'redefine';
my $old_new = \&amp;Net::POP3::new;
*Net::POP3::new = sub {
	my $class = shift;
	my %arg = @_ % 2 == 0 ? @_ : ( Host =&gt; shift,@_ );
	if ( delete $arg{SSL} ) {
		$arg{Port} ||= 995;
		return Net::POP3::_SSLified-&gt;new(%arg);
	} else {
		return $old_new-&gt;($class,%arg);
	}
};

##############################################################################
# Socket class derived from IO::Socket::SSL
# strict certificate verification per default
##############################################################################
our %SSLopts;
{
	package Net::POP3::_SSL_Socket;
	our @ISA = 'IO::Socket::SSL';
	sub configure_SSL {
		my ($self,$arg_hash) = @_;

		# set per default strict certificate verification
		$arg_hash-&gt;{SSL_verify_mode} = 1 
			if ! exists $arg_hash-&gt;{SSL_verify_mode};
		$arg_hash-&gt;{SSL_verifycn_scheme} = 'pop3'
			if ! exists $arg_hash-&gt;{SSL_verifycn_scheme};
		$arg_hash-&gt;{SSL_verifycn_name} = $self-&gt;host
			if ! exists $arg_hash-&gt;{SSL_verifycn_name};

		# force keys from %SSLopts
		while ( my ($k,$v) = each %SSLopts ) {
			$arg_hash-&gt;{$k} = $v;
		}
		return $self-&gt;SUPER::configure_SSL($arg_hash)
	}
}


##############################################################################
# Net::POP3 derived from Net::POP3::_SSL_Socket instead of IO::Socket::INET
# this talks SSL to the peer
##############################################################################
{
	package Net::POP3::_SSLified;
	use Carp 'croak';

	# deriving does not work because we need to replace a superclass
	# from Net::POP3, so just copy the class into the new one and then
	# change it

	# copy subs
	for ( keys %{Net::POP3::} ) {
		no strict 'refs';
		eval { *{$Net::POP3::{$_}} &amp;&amp; *{$Net::POP3::{$_}}{CODE} } or next;
		*{$_} = \&amp;{ "Net::POP3::$_" };
	}

	# copy + fix @ISA
	our @ISA = @Net::POP3::ISA;
	grep { s{^IO::Socket::INET$}{Net::POP3::_SSL_Socket} } @ISA
		or die "cannot find and replace IO::Socket::INET superclass";

	# we are already sslified
	no warnings 'redefine';
	sub starttls { croak "have already TLS\n" }

	my $old_new = \&amp;new;
	*Net::POP3::_SSLified::new = sub {
		my $class = shift;
		my %arg = @_ % 2 == 0 ? @_ : ( Host =&gt; shift,@_ );
		local %SSLopts;
		$SSLopts{$_} = delete $arg{$_} for ( grep { /^SSL_/ } keys %arg );
		return $old_new-&gt;($class,%arg);
	};

	# Net::Cmd getline uses select, but this is not sufficient with SSL
	# note that this does no EBCDIC etc conversions
	*Net::POP3::_SSLified::getline = sub {
		my $self = shift;
		# skip Net::POP3 getline and go directly to IO::Socket::SSL
		return $self-&gt;IO::Socket::SSL::getline(@_);
	};
}

1;

=head1 NAME

Net::SSLGlue::POP3 - make Net::POP3 able to use SSL

=head1 SYNOPSIS

  	use Net::SSLGlue::POP3;
  	my $pop3s = Net::POP3-&gt;new( $host, 
  		SSL =&gt; 1,
		SSL_ca_path =&gt; ...
	);

	my $pop3 = Net::POP3-&gt;new( $host );
	$pop3-&gt;starttls( SSL_ca_path =&gt; ... );

=head1 DESCRIPTION

L&lt;Net::SSLGlue::POP3&gt; extends L&lt;Net::POP3&gt; so one can either start directly with SSL
or switch later to SSL using the STLS command.

By default it will take care to verify the certificate according to the rules
for POP3 implemented in L&lt;IO::Socket::SSL&gt;.

=head1 METHODS

=over 4

=item new

The method C&lt;new&gt; of L&lt;Net::POP3&gt; is now able to start directly with SSL when
the argument C&lt;&lt;SSL =&gt; 1&gt;&gt; is given. In this case it will not create an
L&lt;IO::Socket::INET&gt; object but an L&lt;IO::Socket::SSL&gt; object. One can give the
usual C&lt;SSL_*&gt; parameter of L&lt;IO::Socket::SSL&gt; to C&lt;Net::POP3::new&gt;.

=item starttls

If the connection is not yet SSLified it will issue the STLS command and
change the object, so that SSL will now be used. The usual C&lt;SSL_*&gt; parameter of
L&lt;IO::Socket::SSL&gt; will be given.

=item peer_certificate ...

Once the SSL connection is established the object is derived from
L&lt;IO::Socket::SSL&gt; so that you can use this method to get information about the
certificate. See the L&lt;IO::Socket::SSL&gt; documentation.

=back

All of these methods can take the C&lt;SSL_*&gt; parameter from L&lt;IO::Socket::SSL&gt; to
change the behavior of the SSL connection. The following parameters are
especially useful:

=over 4

=item SSL_ca_path, SSL_ca_file

Specifies the path or a file where the CAs used for checking the certificates
are located. This is typically L&lt;/etc/ssl/certs&gt; on UNIX systems.

=item SSL_verify_mode

If set to 0, verification of the certificate will be disabled. By default
it is set to 1 which means that the peer certificate is checked.

=item SSL_verifycn_name

Usually the name given as the hostname in the constructor is used to verify the
identity of the certificate. If you want to check the certificate against
another name you can specify it with this parameter.

=back

=head1 SEE ALSO

IO::Socket::SSL, Net::POP3

=head1 COPYRIGHT

This module is copyright (c) 2013, Steffen Ullrich.
All Rights Reserved.
This module is free software. It may be used, redistributed and/or modified
under the same terms as Perl itself.

</pre></body></html>