2012-05-04 21:20:48 +02:00
|
|
|
#!/usr/bin/perl -w
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use Data::Dumper;
|
|
|
|
use Digest::HMAC_MD5 qw(hmac_md5_hex);
|
|
|
|
use English qw/ -no_match_vars /;
|
|
|
|
use File::Path;
|
|
|
|
use Scalar::Util qw( openhandle );
|
|
|
|
use Test::More qw(no_plan);
|
|
|
|
|
2014-09-18 07:22:17 +02:00
|
|
|
use lib 't';
|
|
|
|
use lib 'lib';
|
|
|
|
use Qpsmtpd::Constants;
|
2012-05-04 21:20:48 +02:00
|
|
|
use_ok('Test::Qpsmtpd');
|
|
|
|
use_ok('Qpsmtpd::Auth');
|
|
|
|
|
|
|
|
my ($smtpd, $conn) = Test::Qpsmtpd->new_conn();
|
|
|
|
|
2013-04-21 06:52:07 +02:00
|
|
|
ok($smtpd, "get new connection ($smtpd)");
|
|
|
|
isa_ok($conn, 'Qpsmtpd::Connection', "get new connection");
|
2012-05-04 21:20:48 +02:00
|
|
|
|
|
|
|
my $r;
|
2013-04-21 06:52:07 +02:00
|
|
|
my $user = 'good@example.com';
|
|
|
|
my $pass = 'good_pass';
|
|
|
|
my $enc_plain = Qpsmtpd::Auth::e64(join("\0", '', $user, $pass));
|
2012-05-04 21:20:48 +02:00
|
|
|
|
|
|
|
# get_auth_details_plain: plain auth method handles credentials properly
|
2013-04-21 06:52:07 +02:00
|
|
|
my ($loginas, $ruser, $passClear) =
|
|
|
|
Qpsmtpd::Auth::get_auth_details_plain($smtpd, $enc_plain);
|
|
|
|
cmp_ok($user, 'eq', $user, "get_auth_details_plain, user");
|
|
|
|
cmp_ok($passClear, 'eq', $pass, "get_auth_details_plain, password");
|
|
|
|
|
|
|
|
my $bad_auth = Qpsmtpd::Auth::e64(join("\0", 'loginas', 'user@foo', 'passer'));
|
|
|
|
($loginas, $ruser, $passClear) =
|
|
|
|
Qpsmtpd::Auth::get_auth_details_plain($smtpd, $bad_auth);
|
|
|
|
ok(!$loginas, "get_auth_details_plain, loginas -");
|
|
|
|
ok(!$ruser, "get_auth_details_plain, user -");
|
|
|
|
ok(!$passClear, "get_auth_details_plain, pass -");
|
2012-05-04 21:20:48 +02:00
|
|
|
|
|
|
|
# these plugins test against whicever loaded plugin provides their selected
|
|
|
|
# auth type. Right now, they end up testing against auth_flat_file.
|
|
|
|
|
|
|
|
# PLAIN
|
|
|
|
$r = Qpsmtpd::Auth::SASL($smtpd, 'plain', $enc_plain);
|
2014-12-15 21:19:23 +01:00
|
|
|
is( return_code($r), 'OK', 'plain auth' );
|
2013-04-21 06:52:07 +02:00
|
|
|
|
|
|
|
if ($ENV{QPSMTPD_DEVELOPER} && is_interactive()) {
|
2012-05-04 21:20:48 +02:00
|
|
|
|
2013-04-21 06:52:07 +02:00
|
|
|
# same thing, but must be entered interactively
|
2012-05-04 21:20:48 +02:00
|
|
|
print "answer: $enc_plain\n";
|
|
|
|
$r = Qpsmtpd::Auth::SASL($smtpd, 'plain', '');
|
2014-12-15 21:19:23 +01:00
|
|
|
is( return_code($r), 'OK', 'SASL, plain' );
|
2013-04-21 06:52:07 +02:00
|
|
|
}
|
2012-05-04 21:20:48 +02:00
|
|
|
|
|
|
|
# LOGIN
|
|
|
|
|
2013-04-21 06:52:07 +02:00
|
|
|
if ($ENV{QPSMTPD_DEVELOPER} && is_interactive()) {
|
2012-05-04 21:20:48 +02:00
|
|
|
|
2013-04-21 06:52:07 +02:00
|
|
|
my $enc_user = Qpsmtpd::Auth::e64($user);
|
|
|
|
my $enc_pass = Qpsmtpd::Auth::e64($pass);
|
2012-05-04 21:20:48 +02:00
|
|
|
|
2013-04-21 06:52:07 +02:00
|
|
|
# get_base64_response
|
2012-05-04 21:20:48 +02:00
|
|
|
print "answer: $enc_user\n";
|
2013-04-21 06:52:07 +02:00
|
|
|
$r = Qpsmtpd::Auth::get_base64_response($smtpd, 'Username');
|
|
|
|
cmp_ok($r, 'eq', $user, "get_base64_response +");
|
2012-05-04 21:20:48 +02:00
|
|
|
|
2013-04-21 06:52:07 +02:00
|
|
|
# get_auth_details_login
|
2012-05-04 21:20:48 +02:00
|
|
|
print "answer: $enc_pass\n";
|
2013-04-21 06:52:07 +02:00
|
|
|
($ruser, $passClear) =
|
|
|
|
Qpsmtpd::Auth::get_auth_details_login($smtpd, $enc_user);
|
|
|
|
cmp_ok($ruser, 'eq', $user, "get_auth_details_login, user +");
|
|
|
|
cmp_ok($passClear, 'eq', $pass, "get_auth_details_login, pass +");
|
2012-05-04 21:20:48 +02:00
|
|
|
|
|
|
|
print "encoded pass: $enc_pass\n";
|
|
|
|
$r = Qpsmtpd::Auth::SASL($smtpd, 'login', $enc_user);
|
2014-12-15 21:19:23 +01:00
|
|
|
is( return_code($r), 'OK', 'SASL, login' );
|
2013-04-21 06:52:07 +02:00
|
|
|
}
|
2012-05-04 21:20:48 +02:00
|
|
|
|
|
|
|
# CRAM-MD5
|
|
|
|
|
2013-04-21 06:52:07 +02:00
|
|
|
if ($ENV{QPSMTPD_DEVELOPER} && is_interactive()) {
|
2012-05-04 21:20:48 +02:00
|
|
|
print "starting SASL\n";
|
|
|
|
|
2013-04-21 06:52:07 +02:00
|
|
|
# since we don't have bidirection communication here, we pre-generate a ticket
|
|
|
|
my $ticket =
|
|
|
|
sprintf('<%x.%x@%s>', rand(1000000), time(), $smtpd->config('me'));
|
|
|
|
my $hash_pass = hmac_md5_hex($ticket, $pass);
|
|
|
|
my $enc_answer = Qpsmtpd::Auth::e64(join(' ', $user, $hash_pass));
|
2012-05-04 21:20:48 +02:00
|
|
|
print "answer: $enc_answer\n";
|
2013-04-21 06:52:07 +02:00
|
|
|
my (@r) = Qpsmtpd::Auth::get_auth_details_cram_md5($smtpd, $ticket);
|
|
|
|
cmp_ok($r[0], 'eq', $ticket, "get_auth_details_cram_md5, ticket");
|
|
|
|
cmp_ok($r[1], 'eq', $user, "get_auth_details_cram_md5, user");
|
|
|
|
cmp_ok($r[2], 'eq', $hash_pass, "get_auth_details_cram_md5, passHash");
|
2012-05-04 21:20:48 +02:00
|
|
|
|
2013-04-21 06:52:07 +02:00
|
|
|
#warn Data::Dumper::Dumper(\@r);
|
2012-05-04 21:20:48 +02:00
|
|
|
|
2013-04-21 06:52:07 +02:00
|
|
|
# this isn't going to work without bidirection communication to get the ticket
|
|
|
|
#$r = Qpsmtpd::Auth::SASL($smtpd, 'cram-md5' );
|
2014-12-15 21:19:23 +01:00
|
|
|
#is( return_code($r), 'OK', 'login auth' );
|
2013-04-21 06:52:07 +02:00
|
|
|
}
|
2012-05-04 21:20:48 +02:00
|
|
|
|
|
|
|
sub is_interactive {
|
|
|
|
|
|
|
|
## no critic
|
2013-04-21 06:52:07 +02:00
|
|
|
# borrowed from IO::Interactive
|
|
|
|
my ($out_handle) = (@_, select); # Default to default output handle
|
2012-05-04 21:20:48 +02:00
|
|
|
|
2013-04-21 06:52:07 +02:00
|
|
|
# Not interactive if output is not to terminal...
|
2012-05-04 21:20:48 +02:00
|
|
|
return if not -t $out_handle;
|
|
|
|
|
2013-04-21 06:52:07 +02:00
|
|
|
# If *ARGV is opened, we're interactive if...
|
|
|
|
if (openhandle * ARGV) {
|
2012-05-04 21:20:48 +02:00
|
|
|
|
2013-04-21 06:52:07 +02:00
|
|
|
# ...it's currently opened to the magic '-' file
|
2012-05-04 21:20:48 +02:00
|
|
|
return -t *STDIN if defined $ARGV && $ARGV eq '-';
|
|
|
|
|
2013-04-21 06:52:07 +02:00
|
|
|
# ...it's at end-of-file and the next file is the magic '-' file
|
2012-05-04 21:20:48 +02:00
|
|
|
return @ARGV > 0 && $ARGV[0] eq '-' && -t *STDIN if eof *ARGV;
|
|
|
|
|
2013-04-21 06:52:07 +02:00
|
|
|
# ...it's directly attached to the terminal
|
2012-05-04 21:20:48 +02:00
|
|
|
return -t *ARGV;
|
2013-04-21 06:52:07 +02:00
|
|
|
}
|
2012-05-04 21:20:48 +02:00
|
|
|
|
2013-04-21 06:52:07 +02:00
|
|
|
# If *ARGV isn't opened, it will be interactive if *STDIN is attached
|
|
|
|
# to a terminal and either there are no files specified on the command line
|
|
|
|
# or if there are files and the first is the magic '-' file
|
|
|
|
return -t *STDIN && (@ARGV == 0 || $ARGV[0] eq '-');
|
2012-05-04 21:20:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
__END__
|
|
|
|
|
|
|
|
if ( ref $r ) {
|
|
|
|
} else {
|
|
|
|
warn $r;
|
|
|
|
}
|
|
|
|
#print Data::Dumper::Dumper($conn);
|
|
|
|
#print Data::Dumper::Dumper($smtpd);
|
|
|
|
|