Wojciech Dyżewski
Wojciech Dyżewski

Reputation: 33

Perl Net::SMTP force auth method

I'm trying to send mail using Perl Net::SMTP with auth method other than default picked GSSAPI (e.g. force PLAIN).

I have tried:

my $smtp;
$smtp = Net::SMTP::SSL->new($host, Port => $port);
$smtp->auth($user, $passwd);

and replacing last line with:

$smtp->auth('PLAIN', $user, $passwd);

or passing Authen::SASL object with selected mechanism to $smtp->auth(). None of the above work - debug (and mail server logs) says it still tries AUTH GSSAPI.

Does anyone know how to correctly force auth method in Net::SMTP?

My Perl version is 5.20.2-3+deb8u8 from Debian 8, packages version:

Upvotes: 1

Views: 2938

Answers (2)

AnFi
AnFi

Reputation: 10913

Net::SMTP version below 3.00

Net::SMTP version 2.31_1 (newest pre 3.00 in libnet) overwrites mechanism in Authen::SASL with mechanism listed in EHLO reply. Below please find my UGLY fix.

use Net::SMTP::SSL;
use Authen::SASL;

my ( $host, $port, $user, $pass ) = ( '...', '...', '...', '...' );    # fill correct data

my $smtp = Net::SMTP::SSL->new( $host, Port => $port, Debug => 1 );
my $auth = Authen::SASL->new(
  mechanism => 'PLAIN LOGIN',
  callback  => { user => $user, pass => $passwd }
);
{
  no warnings 'redefine';
  my $count;
  local *Authen::SASL::mechanism = sub {
    my $self = shift;

    # Fix Begin
    # ignore first setting of mechanism
    if ( !$count++ && @_ && $Net::SMTP::VERSION =~ /^2\./ ) {
      return;
    }

    # Fix End
    @_
      ? $self->{mechanism} = shift
      : $self->{mechanism};
  };
  $smtp->auth($auth);
}

Code requiring fixing - Net::SMTP 2.31_1 from libnet 1.22_01

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

  ...

  my $mechanisms = $self->supports('AUTH', 500, ["Command unknown: 'AUTH'"]);
  return unless defined $mechanisms;

  my $sasl;

  if (ref($username) and UNIVERSAL::isa($username, 'Authen::SASL')) {
    $sasl = $username;
    $sasl->mechanism($mechanisms); # <= HERE Authen::SASL mechanism overwrite 
  }

Upvotes: 0

AnFi
AnFi

Reputation: 10913

Net::SMTP version above 3.00

Net::SMTP above version 3:
* does not overwrite mechanism in Authen::SASL parameter of auth method
* supports STARTTLS and smtps

use Net::SMTP;
use Authen::SASL;

my($host, $user, $pass) = ('...','...','...'); # fill correct data

my $smtp = Net::SMTP->new( $host, SSL=>1, Debug => 1 ); # SSL=>1 - use smtps
$smtp->auth(
  Authen::SASL->new(
    mechanism => 'PLAIN LOGIN',
    callback  => { user => $user, pass => $passwd }
  )
);

Upvotes: 2

Related Questions