| 1 |
package Qpsmtpd::Auth; |
| 2 |
|
| 3 |
# See the documentation in 'perldoc docs/authentication.pod' |
| 4 |
|
| 5 |
use strict; |
| 6 |
use warnings; |
| 7 |
|
| 8 |
use Qpsmtpd::Constants; |
| 9 |
|
| 10 |
use Digest::HMAC_MD5 qw(hmac_md5_hex); |
| 11 |
use MIME::Base64; |
| 12 |
|
| 13 |
sub e64 { |
| 14 |
my ($arg) = @_; |
| 15 |
my $res = encode_base64($arg); |
| 16 |
chomp($res); |
| 17 |
return ($res); |
| 18 |
} |
| 19 |
|
| 20 |
sub SASL { |
| 21 |
|
| 22 |
# $DB::single = 1; |
| 23 |
my ($session, $mechanism, $prekey) = @_; |
| 24 |
my ($user, $passClear, $passHash, $ticket, $loginas); |
| 25 |
|
| 26 |
if ($mechanism eq 'plain') { |
| 27 |
($loginas, $user, $passClear) = |
| 28 |
get_auth_details_plain($session, $prekey); |
| 29 |
return DECLINED if !$user || !$passClear; |
| 30 |
} |
| 31 |
elsif ($mechanism eq 'login') { |
| 32 |
($user, $passClear) = get_auth_details_login($session, $prekey); |
| 33 |
return DECLINED if !$user || !$passClear; |
| 34 |
} |
| 35 |
elsif ($mechanism eq 'cram-md5') { |
| 36 |
($ticket, $user, $passHash) = get_auth_details_cram_md5($session); |
| 37 |
return DECLINED if !$user || !$passHash; |
| 38 |
} |
| 39 |
else { |
| 40 |
#this error is now caught in SMTP.pm's sub auth |
| 41 |
$session->respond(500, "Internal server error"); |
| 42 |
return DECLINED; |
| 43 |
} |
| 44 |
|
| 45 |
# try running the specific hooks first |
| 46 |
my ($rc, $msg) = |
| 47 |
$session->run_hooks("auth-$mechanism", $mechanism, $user, $passClear, |
| 48 |
$passHash, $ticket); |
| 49 |
|
| 50 |
# try running the polymorphous hooks next |
| 51 |
if (!$rc || $rc == DECLINED) { |
| 52 |
($rc, $msg) = |
| 53 |
$session->run_hooks("auth", $mechanism, $user, |
| 54 |
$passClear, $passHash, $ticket); |
| 55 |
} |
| 56 |
|
| 57 |
if ($rc == OK) { |
| 58 |
$msg = |
| 59 |
uc($mechanism) |
| 60 |
. " authentication successful for $user" |
| 61 |
. ($msg ? " - $msg" : ''); |
| 62 |
$session->respond(235, $msg); |
| 63 |
$session->connection->relay_client(1); |
| 64 |
if ($session->connection->notes('naughty')) { |
| 65 |
$session->log(LOGINFO, "auth success cleared naughty"); |
| 66 |
$session->connection->notes('naughty', 0); |
| 67 |
} |
| 68 |
$session->log(LOGDEBUG, $msg); # already logged by $session->respond |
| 69 |
|
| 70 |
$session->{_auth_user} = $user; |
| 71 |
$session->{_auth_mechanism} = $mechanism; |
| 72 |
s/[\r\n].*//s for ($session->{_auth_user}, $session->{_auth_mechanism}); |
| 73 |
|
| 74 |
return OK; |
| 75 |
} |
| 76 |
else { |
| 77 |
$msg = |
| 78 |
uc($mechanism) |
| 79 |
. " authentication failed for $user" |
| 80 |
. ($msg ? " - $msg" : ''); |
| 81 |
$session->respond(535, $msg); |
| 82 |
$session->log(LOGDEBUG, $msg); # already logged by $session->respond |
| 83 |
return DENY; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
sub get_auth_details_plain { |
| 88 |
my ($session, $prekey) = @_; |
| 89 |
|
| 90 |
if (!$prekey) { |
| 91 |
$session->respond(334, ' '); |
| 92 |
$prekey = <STDIN>; |
| 93 |
} |
| 94 |
|
| 95 |
my ($loginas, $user, $passClear) = split /\x0/, decode_base64($prekey); |
| 96 |
|
| 97 |
if (!$user) { |
| 98 |
if ($loginas) { |
| 99 |
$session->respond(535, "Authentication invalid ($loginas)"); |
| 100 |
} |
| 101 |
else { |
| 102 |
$session->respond(535, "Authentication invalid"); |
| 103 |
} |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
# Authorization ID must not be different from Authentication ID |
| 108 |
if ($loginas ne '' && $loginas ne $user) { |
| 109 |
$session->respond(535, "Authentication invalid for $user"); |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
return ($loginas, $user, $passClear); |
| 114 |
} |
| 115 |
|
| 116 |
sub get_auth_details_login { |
| 117 |
my ($session, $prekey) = @_; |
| 118 |
|
| 119 |
my $user; |
| 120 |
|
| 121 |
if ($prekey) { |
| 122 |
$user = decode_base64($prekey); |
| 123 |
} |
| 124 |
else { |
| 125 |
$user = get_base64_response($session, 'Username:') or return; |
| 126 |
} |
| 127 |
|
| 128 |
my $passClear = get_base64_response($session, 'Password:') or return; |
| 129 |
|
| 130 |
return ($user, $passClear); |
| 131 |
} |
| 132 |
|
| 133 |
sub get_auth_details_cram_md5 { |
| 134 |
my ($session, $ticket) = @_; |
| 135 |
|
| 136 |
if (!$ticket) { # ticket is only passed in during testing |
| 137 |
# rand() is not cryptographic, but we only need to generate a globally |
| 138 |
# unique number. The rand() is there in case the user logs in more than |
| 139 |
# once in the same second, or if the clock is skewed. |
| 140 |
$ticket = |
| 141 |
sprintf('<%x.%x@%s>', rand(1000000), time(), $session->config('me')); |
| 142 |
} |
| 143 |
|
| 144 |
# send the base64 encoded ticket |
| 145 |
$session->respond(334, encode_base64($ticket, '')); |
| 146 |
my $line = <STDIN>; |
| 147 |
|
| 148 |
if ($line eq '*') { |
| 149 |
$session->respond(501, "Authentication canceled"); |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
my ($user, $passHash) = split(/ /, decode_base64($line)); |
| 154 |
unless ($user && $passHash) { |
| 155 |
$session->respond(504, "Invalid authentication string"); |
| 156 |
return; |
| 157 |
} |
| 158 |
|
| 159 |
$session->{auth}{ticket} = $ticket; |
| 160 |
return ($ticket, $user, $passHash); |
| 161 |
} |
| 162 |
|
| 163 |
sub get_base64_response { |
| 164 |
my ($session, $question) = @_; |
| 165 |
|
| 166 |
$session->respond(334, e64($question)); |
| 167 |
my $answer = decode_base64(<STDIN>); |
| 168 |
if ($answer eq '*') { |
| 169 |
$session->respond(501, "Authentication canceled"); |
| 170 |
return; |
| 171 |
} |
| 172 |
return $answer; |
| 173 |
} |
| 174 |
|
| 175 |
sub validate_password { |
| 176 |
my ($self, %a) = @_; |
| 177 |
|
| 178 |
my ($pkg, $file, $line) = caller(); |
| 179 |
$file = (split /\//, $file)[-1]; # strip off the path |
| 180 |
|
| 181 |
my $src_clear = $a{src_clear}; |
| 182 |
my $src_crypt = $a{src_crypt}; |
| 183 |
my $attempt_clear = $a{attempt_clear}; |
| 184 |
my $attempt_hash = $a{attempt_hash}; |
| 185 |
my $method = $a{method} or die "missing method"; |
| 186 |
my $ticket = $a{ticket} || $self->{auth}{ticket}; |
| 187 |
my $deny = $a{deny} || DENY; |
| 188 |
|
| 189 |
if (!$src_crypt && !$src_clear) { |
| 190 |
$self->log(LOGINFO, "fail: missing password"); |
| 191 |
return ($deny, "$file - no such user"); |
| 192 |
} |
| 193 |
|
| 194 |
if (!$src_clear && $method =~ /CRAM-MD5/i) { |
| 195 |
$self->log(LOGINFO, "skip: cram-md5 not supported w/o clear pass"); |
| 196 |
return (DECLINED, $file); |
| 197 |
} |
| 198 |
|
| 199 |
if (defined $attempt_clear) { |
| 200 |
if ($src_clear && $src_clear eq $attempt_clear) { |
| 201 |
$self->log(LOGINFO, "pass: clear match"); |
| 202 |
return (OK, $file); |
| 203 |
} |
| 204 |
|
| 205 |
if ($src_crypt && $src_crypt eq crypt($attempt_clear, $src_crypt)) { |
| 206 |
$self->log(LOGINFO, "pass: crypt match"); |
| 207 |
return (OK, $file); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
if (defined $attempt_hash && $src_clear) { |
| 212 |
if (!$ticket) { |
| 213 |
$self->log(LOGERROR, "skip: missing ticket"); |
| 214 |
return (DECLINED, $file); |
| 215 |
} |
| 216 |
|
| 217 |
if ($attempt_hash eq hmac_md5_hex($ticket, $src_clear)) { |
| 218 |
$self->log(LOGINFO, "pass: hash match"); |
| 219 |
return (OK, $file); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
$self->log(LOGINFO, "fail: wrong password"); |
| 224 |
return ($deny, "$file - wrong password"); |
| 225 |
} |
| 226 |
|
| 227 |
# tag: qpsmtpd plugin that sets RELAYCLIENT when the user authenticates |
| 228 |
|
| 229 |
1; |