Skip to content

Commit

Permalink
Add support for encode()/decode() methods in Net::Cmd
Browse files Browse the repository at this point in the history
Some mechanisms in Authen::SASL offer additional security layers and
encode()/decode() methods for them.  The I/O routines in Net::Cmd now use
it if they are available.  Currently only Net::SMTP defines
protocol-specific methods.

Note that Authen::SASL::Perl::DIGEST_MD5::encode() does not work in
this patch for some reason.  It still needs more investigations.
  • Loading branch information
hrs-allbsd committed Oct 16, 2023
1 parent 808bb35 commit 18f0342
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
21 changes: 19 additions & 2 deletions lib/Net/Cmd.pm
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,26 @@ sub set_status {
1;
}

# The default encode/decode methods
sub encode {
my ($cmd, $text, $len) = @_;

$text;
}


sub decode {
my ($cmd, $text, $len) = @_;

$text;
}


sub _syswrite_with_timeout {
my $cmd = shift;
my $line = shift;
my $line = $cmd->encode($_[0], $_[1]);
my $len = length($line);

my $len = length($line);
my $offset = 0;
my $win = "";
vec($win, fileno($cmd), 1) = 1;
Expand Down Expand Up @@ -352,6 +367,8 @@ sub getline {

substr($buf, 0, 0) = $partial; ## prepend from last sysread

$buf = $cmd->decode($buf, length($buf)); ## decode it

my @buf = split(/\015?\012/, $buf, -1); ## break into lines

$partial = pop @buf;
Expand Down
36 changes: 29 additions & 7 deletions lib/Net/SMTP.pm
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,28 @@ sub etrn {
}


# Overload encode method when Authen::SASL is available
sub encode {
my ($self, $text, $len) = @_;
my $sasl = ${*$self}{'net_smtp_sasl'};

return ($sasl)
? $sasl->encode($text, $len)
: $self->SUPER::encode($text, $len);
}


# Overload decode method when Authen::SASL is available
sub decode {
my ($self, $text, $len) = @_;
my $sasl = ${*$self}{'net_smtp_sasl'};

return ($sasl)
? $sasl->decode($text, $len)
: $self->SUPER::decode($text, $len);
}


sub auth {
my ($self, $username, $password) = @_;

Expand Down Expand Up @@ -216,13 +238,6 @@ sub auth {
# todo that we would really need to change the ISA hierarchy
# so we don't inherit from IO::Socket, but instead hold it in an attribute

# DIGEST-MD5 can support integrity and/or confidentiality protection
# over the socket traffic (auth-int and auth-conf) which we do not
# support here for now. To disable them, set maxssf=minssf=0.

$client->property('maxssf' => 0, 'minssf' => 0)
if ($client->mechanism eq 'DIGEST-MD5');

my @cmd = ("AUTH", $client->mechanism);
my $code;

Expand All @@ -241,6 +256,13 @@ sub auth {
$self->debug_print(1, "(decoded) " . $str . "\n") if $self->debug;
}

# Some mechanisms in Authen::SASL offer additional security layers
# for integrity and/or confidentiality and define encode() and
# decode() methods. To support them, store # the Authen::SASL
# object in {net_smtp_sasl}.
#
${*$self}{'net_smtp_sasl'} = $sasl->{conn};

$code == CMD_OK;
}

Expand Down

0 comments on commit 18f0342

Please sign in to comment.