2002-09-24 12:56:35 +02:00
|
|
|
package Qpsmtpd::SMTP;
|
|
|
|
use strict;
|
2014-09-11 01:53:28 +02:00
|
|
|
|
|
|
|
use base 'Qpsmtpd';
|
|
|
|
|
2002-09-24 12:56:35 +02:00
|
|
|
use Carp;
|
2014-09-11 01:53:28 +02:00
|
|
|
#use Data::Dumper;
|
|
|
|
use POSIX qw(strftime);
|
|
|
|
use Mail::Header;
|
|
|
|
use Net::DNS;
|
2002-09-24 12:56:35 +02:00
|
|
|
|
2014-09-11 01:53:28 +02:00
|
|
|
use Qpsmtpd;
|
2002-09-24 12:56:35 +02:00
|
|
|
use Qpsmtpd::Connection;
|
|
|
|
use Qpsmtpd::Transaction;
|
|
|
|
use Qpsmtpd::Plugin;
|
|
|
|
use Qpsmtpd::Constants;
|
2004-06-29 23:45:35 +02:00
|
|
|
use Qpsmtpd::Auth;
|
2004-07-15 01:58:47 +02:00
|
|
|
use Qpsmtpd::Address ();
|
2006-04-07 20:58:02 +02:00
|
|
|
use Qpsmtpd::Command;
|
2002-09-24 12:56:35 +02:00
|
|
|
|
2014-09-11 01:53:28 +02:00
|
|
|
my %auth_mechanisms = ();
|
2002-09-24 12:56:35 +02:00
|
|
|
|
2004-11-27 07:38:32 +01:00
|
|
|
# this is only good for forkserver
|
|
|
|
# can't set these here, cause forkserver resets them
|
2013-12-17 23:13:51 +01:00
|
|
|
#$SIG{ALRM} = sub { respond(421, "timeout; I can't wait that long..."); exit };
|
2004-11-27 07:38:32 +01:00
|
|
|
#$SIG{ALRM} = sub { warn "Connection Timed Out\n"; exit; };
|
2002-09-24 12:56:35 +02:00
|
|
|
|
|
|
|
sub new {
|
2014-09-11 01:53:28 +02:00
|
|
|
my ($proto, %args) = @_;
|
2013-04-21 06:08:43 +02:00
|
|
|
my $class = ref($proto) || $proto;
|
|
|
|
|
|
|
|
my $self = bless({args => \%args}, $class);
|
2002-09-24 12:56:35 +02:00
|
|
|
|
2014-09-11 01:53:28 +02:00
|
|
|
# this list of valid commands should probably be a method or a set of methods
|
|
|
|
$self->{_commands} = {
|
|
|
|
map { $_ => '' } qw(ehlo helo rset mail rcpt data help vrfy noop quit)
|
|
|
|
};
|
2002-09-24 12:56:35 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->SUPER::_restart(%args) if $args{restart}; # calls Qpsmtpd::_restart()
|
|
|
|
$self;
|
2002-09-24 12:56:35 +02:00
|
|
|
}
|
|
|
|
|
2003-04-21 11:42:01 +02:00
|
|
|
sub command_counter {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
|
|
|
$self->{_counter} || 0;
|
2003-04-21 11:42:01 +02:00
|
|
|
}
|
2002-09-24 12:56:35 +02:00
|
|
|
|
|
|
|
sub dispatch {
|
2012-06-23 06:18:40 +02:00
|
|
|
my $self = shift;
|
|
|
|
my ($cmd) = shift;
|
2013-04-21 06:08:43 +02:00
|
|
|
if (!$cmd) {
|
2012-06-23 06:18:40 +02:00
|
|
|
$self->run_hooks("unrecognized_command", '', @_);
|
|
|
|
return 1;
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
2012-06-23 06:18:40 +02:00
|
|
|
$cmd = lc $cmd;
|
2002-09-24 12:56:35 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->{_counter}++;
|
2003-04-21 11:42:01 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
if ($cmd !~ /^(\w{1,12})$/ or !exists $self->{_commands}->{$1}) {
|
|
|
|
$self->run_hooks("unrecognized_command", $cmd, @_);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
$cmd = $1;
|
2002-09-24 12:56:35 +02:00
|
|
|
|
|
|
|
my ($result) = eval { $self->$cmd(@_) };
|
2004-03-05 13:46:24 +01:00
|
|
|
$self->log(LOGERROR, "XX: $@") if $@;
|
2002-09-24 12:56:35 +02:00
|
|
|
return $result if defined $result;
|
|
|
|
return $self->fault("command '$cmd' failed unexpectedly");
|
|
|
|
}
|
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
sub unrecognized_command_respond {
|
|
|
|
my ($self, $rc, $msg) = @_;
|
|
|
|
if ($rc == DENY_DISCONNECT) {
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->respond(521, @$msg);
|
|
|
|
$self->disconnect;
|
2006-12-08 20:46:18 +01:00
|
|
|
}
|
|
|
|
elsif ($rc == DENY) {
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->respond(500, @$msg);
|
2006-12-08 20:46:18 +01:00
|
|
|
}
|
|
|
|
elsif ($rc != DONE) {
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->respond(500, "Unrecognized command");
|
2006-12-08 20:46:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-24 12:56:35 +02:00
|
|
|
sub fault {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
|
|
|
my ($msg) = shift || "program fault - command not performed";
|
|
|
|
my ($name) = split /\s+/, $0, 2;
|
2014-09-11 18:58:31 +02:00
|
|
|
print STDERR $name, "[$$]: $msg\n";
|
|
|
|
print STDERR $name, "[$$]: Last system error: $!"
|
|
|
|
." (Likely irelevant--debug the crashed plugin to ensure it handles \$! properly)";
|
2013-04-21 06:08:43 +02:00
|
|
|
return $self->respond(451, "Internal error - try again later - " . $msg);
|
2002-09-24 12:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub start_conversation {
|
|
|
|
my $self = shift;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2002-09-24 12:56:35 +02:00
|
|
|
# this should maybe be called something else than "connect", see
|
|
|
|
# lib/Qpsmtpd/TcpServer.pm for more confusion.
|
2006-12-08 20:46:18 +01:00
|
|
|
$self->run_hooks("connect");
|
|
|
|
return DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub connect_respond {
|
|
|
|
my ($self, $rc, $msg) = @_;
|
2006-12-25 11:12:54 +01:00
|
|
|
if ($rc == DENY || $rc == DENY_DISCONNECT) {
|
2013-04-21 06:08:43 +02:00
|
|
|
$msg->[0] ||= 'Connection from you denied, bye bye.';
|
|
|
|
$self->respond(550, @$msg);
|
|
|
|
$self->disconnect;
|
2003-04-15 19:01:43 +02:00
|
|
|
}
|
2006-12-25 11:12:54 +01:00
|
|
|
elsif ($rc == DENYSOFT || $rc == DENYSOFT_DISCONNECT) {
|
2013-04-21 06:08:43 +02:00
|
|
|
$msg->[0] ||= 'Connection from you temporarily denied, bye bye.';
|
|
|
|
$self->respond(450, @$msg);
|
|
|
|
$self->disconnect;
|
2003-04-15 19:01:43 +02:00
|
|
|
}
|
|
|
|
elsif ($rc != DONE) {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $greets = $self->config('smtpgreeting');
|
|
|
|
if ($greets) {
|
|
|
|
$greets .= " ESMTP" unless $greets =~ /(^|\W)ESMTP(\W|$)/;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$greets =
|
|
|
|
$self->config('me')
|
|
|
|
. " ESMTP qpsmtpd "
|
|
|
|
. $self->version
|
|
|
|
. " ready; send us your mail, but not your spam.";
|
|
|
|
}
|
2005-07-19 16:20:05 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->respond(220, $greets);
|
2002-09-24 12:56:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub transaction {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
|
|
|
return $self->{_transaction} || $self->reset_transaction();
|
2002-09-24 12:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub reset_transaction {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
|
|
|
$self->run_hooks("reset_transaction") if $self->{_transaction};
|
|
|
|
return $self->{_transaction} = Qpsmtpd::Transaction->new();
|
2002-09-24 12:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub connection {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
|
|
|
@_ and $self->{_connection} = shift;
|
|
|
|
return $self->{_connection}
|
|
|
|
|| ($self->{_connection} = Qpsmtpd::Connection->new());
|
2007-09-02 12:50:23 +02:00
|
|
|
}
|
2002-09-24 12:56:35 +02:00
|
|
|
|
|
|
|
sub helo {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $line) = @_;
|
|
|
|
my ($rc, @msg) = $self->run_hooks('helo_parse');
|
|
|
|
my ($ok, $hello_host, @stuff) =
|
|
|
|
Qpsmtpd::Command->parse('helo', $line, $msg[0]);
|
2006-04-07 20:58:02 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
return $self->respond(501,
|
|
|
|
"helo requires domain/address - see RFC-2821 4.1.1.1")
|
|
|
|
unless $hello_host;
|
|
|
|
my $conn = $self->connection;
|
|
|
|
return $self->respond(503, "but you already said HELO ...") if $conn->hello;
|
2002-09-24 12:56:35 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->run_hooks("helo", $hello_host, @stuff);
|
2006-12-08 20:46:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub helo_respond {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $rc, $msg, $args) = @_;
|
|
|
|
my ($hello_host) = @$args;
|
|
|
|
if ($rc == DONE) {
|
|
|
|
|
|
|
|
# do nothing:
|
|
|
|
1;
|
|
|
|
}
|
|
|
|
elsif ($rc == DENY) {
|
|
|
|
$self->respond(550, @$msg);
|
|
|
|
}
|
|
|
|
elsif ($rc == DENYSOFT) {
|
|
|
|
$self->respond(450, @$msg);
|
|
|
|
}
|
|
|
|
elsif ($rc == DENY_DISCONNECT) {
|
|
|
|
$self->respond(550, @$msg);
|
|
|
|
$self->disconnect;
|
|
|
|
}
|
|
|
|
elsif ($rc == DENYSOFT_DISCONNECT) {
|
|
|
|
$self->respond(450, @$msg);
|
|
|
|
$self->disconnect;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
my $conn = $self->connection;
|
|
|
|
$conn->hello("helo");
|
|
|
|
$conn->hello_host($hello_host);
|
|
|
|
$self->transaction;
|
|
|
|
$self->respond(
|
|
|
|
250,
|
|
|
|
$self->config('me') . " Hi "
|
|
|
|
. $conn->remote_info . " ["
|
|
|
|
. $conn->remote_ip
|
|
|
|
. "]; I am so happy to meet you."
|
|
|
|
);
|
|
|
|
}
|
2002-09-24 12:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub ehlo {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $line) = @_;
|
|
|
|
my ($rc, @msg) = $self->run_hooks('ehlo_parse');
|
|
|
|
my ($ok, $hello_host, @stuff) =
|
|
|
|
Qpsmtpd::Command->parse('ehlo', $line, $msg[0]);
|
|
|
|
return $self->respond(501,
|
|
|
|
"ehlo requires domain/address - see RFC-2821 4.1.1.1")
|
|
|
|
unless $hello_host;
|
|
|
|
my $conn = $self->connection;
|
|
|
|
return $self->respond(503, "but you already said HELO ...") if $conn->hello;
|
2002-09-24 12:56:35 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->run_hooks("ehlo", $hello_host, @stuff);
|
2006-12-08 20:46:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub ehlo_respond {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $rc, $msg, $args) = @_;
|
|
|
|
my ($hello_host) = @$args;
|
|
|
|
if ($rc == DONE) {
|
|
|
|
|
|
|
|
# do nothing:
|
|
|
|
1;
|
|
|
|
}
|
|
|
|
elsif ($rc == DENY) {
|
|
|
|
$self->respond(550, @$msg);
|
|
|
|
}
|
|
|
|
elsif ($rc == DENYSOFT) {
|
|
|
|
$self->respond(450, @$msg);
|
|
|
|
}
|
|
|
|
elsif ($rc == DENY_DISCONNECT) {
|
|
|
|
$self->respond(550, @$msg);
|
|
|
|
$self->disconnect;
|
|
|
|
}
|
|
|
|
elsif ($rc == DENYSOFT_DISCONNECT) {
|
|
|
|
$self->respond(450, @$msg);
|
|
|
|
$self->disconnect;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
my $conn = $self->connection;
|
|
|
|
$conn->hello("ehlo");
|
|
|
|
$conn->hello_host($hello_host);
|
|
|
|
$self->transaction;
|
|
|
|
|
|
|
|
my @capabilities =
|
|
|
|
$self->transaction->notes('capabilities')
|
|
|
|
? @{$self->transaction->notes('capabilities')}
|
|
|
|
: ();
|
|
|
|
|
|
|
|
# Check for possible AUTH mechanisms
|
|
|
|
HOOK: foreach my $hook (keys %{$self->hooks}) {
|
|
|
|
if ($hook =~ m/^auth-?(.+)?$/) {
|
|
|
|
if (defined $1) {
|
|
|
|
$auth_mechanisms{uc($1)} = 1;
|
|
|
|
}
|
|
|
|
else { # at least one polymorphous auth provider
|
|
|
|
%auth_mechanisms = map { $_, 1 } qw(PLAIN CRAM-MD5 LOGIN);
|
|
|
|
last HOOK;
|
|
|
|
}
|
2004-06-29 23:45:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
# Check if we should only offer AUTH after TLS is completed
|
|
|
|
my $tls_before_auth =
|
|
|
|
($self->config('tls_before_auth')
|
|
|
|
? ($self->config('tls_before_auth'))[0]
|
|
|
|
&& $self->transaction->notes('tls_enabled')
|
|
|
|
: 0);
|
|
|
|
if (%auth_mechanisms && !$tls_before_auth) {
|
|
|
|
push @capabilities, 'AUTH ' . join(" ", keys(%auth_mechanisms));
|
|
|
|
$self->{_commands}->{'auth'} = "";
|
|
|
|
}
|
2004-06-29 23:45:35 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->respond(
|
|
|
|
250,
|
|
|
|
$self->config("me") . " Hi "
|
|
|
|
. $conn->remote_info . " ["
|
|
|
|
. $conn->remote_ip . "]",
|
|
|
|
"PIPELINING",
|
|
|
|
"8BITMIME",
|
|
|
|
(
|
|
|
|
$self->config('databytes')
|
|
|
|
? "SIZE " . ($self->config('databytes'))[0]
|
|
|
|
: ()
|
|
|
|
),
|
|
|
|
@capabilities,
|
|
|
|
);
|
|
|
|
}
|
2002-09-24 12:56:35 +02:00
|
|
|
}
|
|
|
|
|
2005-10-31 18:12:37 +01:00
|
|
|
sub auth {
|
2006-04-07 20:58:02 +02:00
|
|
|
my ($self, $line) = @_;
|
2006-12-08 20:46:18 +01:00
|
|
|
$self->run_hooks('auth_parse', $line);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub auth_parse_respond {
|
|
|
|
my ($self, $rc, $msg, $args) = @_;
|
|
|
|
my ($line) = @$args;
|
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($ok, $mechanism, @stuff) =
|
|
|
|
Qpsmtpd::Command->parse('auth', $line, $msg->[0]);
|
|
|
|
return $self->respond(501, $mechanism || "Syntax error in command")
|
2006-04-07 20:58:02 +02:00
|
|
|
unless ($ok == OK);
|
2006-09-22 17:31:28 +02:00
|
|
|
|
2006-09-24 16:55:48 +02:00
|
|
|
$mechanism = lc($mechanism);
|
2005-10-31 18:12:37 +01:00
|
|
|
|
|
|
|
#they AUTH'd once already
|
2013-04-21 06:08:43 +02:00
|
|
|
return $self->respond(503, "but you already said AUTH ...")
|
|
|
|
if (defined $self->{_auth} && $self->{_auth} == OK);
|
2012-05-04 20:51:35 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
return $self->respond(503, "AUTH not defined for HELO")
|
|
|
|
if ($self->connection->hello eq "helo");
|
2012-05-04 20:51:35 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
return $self->respond(503, "SSL/TLS required before AUTH")
|
|
|
|
if (($self->config('tls_before_auth'))[0]
|
|
|
|
&& $self->transaction->notes('tls_enabled'));
|
2005-10-31 18:12:37 +01:00
|
|
|
|
2012-05-04 20:51:35 +02:00
|
|
|
# we don't have a plugin implementing this auth mechanism, 504
|
2013-04-21 06:08:43 +02:00
|
|
|
if (exists $auth_mechanisms{uc($mechanism)}) {
|
|
|
|
return $self->{_auth} = Qpsmtpd::Auth::SASL($self, $mechanism, @stuff);
|
|
|
|
}
|
2006-09-22 17:31:28 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->respond(504, "Unimplemented authentification mechanism: $mechanism");
|
2012-05-04 20:51:35 +02:00
|
|
|
return DENY;
|
2005-10-31 18:12:37 +01:00
|
|
|
}
|
|
|
|
|
2002-09-24 12:56:35 +02:00
|
|
|
sub mail {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $line) = @_;
|
|
|
|
|
|
|
|
# -> from RFC2821
|
|
|
|
# The MAIL command (or the obsolete SEND, SOML, or SAML commands)
|
|
|
|
# begins a mail transaction. Once started, a mail transaction
|
|
|
|
# consists of a transaction beginning command, one or more RCPT
|
|
|
|
# commands, and a DATA command, in that order. A mail transaction
|
|
|
|
# may be aborted by the RSET (or a new EHLO) command. There may be
|
|
|
|
# zero or more transactions in a session. MAIL (or SEND, SOML, or
|
|
|
|
# SAML) MUST NOT be sent if a mail transaction is already open,
|
|
|
|
# i.e., it should be sent only if no mail transaction had been
|
|
|
|
# started in the session, or it the previous one successfully
|
|
|
|
# concluded with a successful DATA command, or if the previous one
|
|
|
|
# was aborted with a RSET.
|
|
|
|
|
|
|
|
# sendmail (8.11) rejects a second MAIL command.
|
|
|
|
|
|
|
|
# qmail-smtpd (1.03) accepts it and just starts a new transaction.
|
|
|
|
# Since we are a qmail-smtpd thing we will do the same.
|
|
|
|
|
|
|
|
$self->reset_transaction;
|
|
|
|
|
|
|
|
if (!$self->connection->hello) {
|
|
|
|
return $self->respond(503, "please say hello first ...");
|
|
|
|
}
|
2012-06-23 06:18:40 +02:00
|
|
|
|
2012-05-04 20:55:14 +02:00
|
|
|
$self->log(LOGDEBUG, "full from_parameter: $line");
|
2013-04-30 05:50:18 +02:00
|
|
|
$self->connection->notes('envelope_from', $line);
|
2006-12-08 20:46:18 +01:00
|
|
|
$self->run_hooks("mail_parse", $line);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub mail_parse_respond {
|
|
|
|
my ($self, $rc, $msg, $args) = @_;
|
|
|
|
my ($line) = @$args;
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($ok, $from, @params) =
|
|
|
|
Qpsmtpd::Command->parse('mail', $line, $msg->[0]);
|
|
|
|
return $self->respond(501, $from || "Syntax error in command")
|
|
|
|
unless ($ok == OK);
|
2006-04-07 20:58:02 +02:00
|
|
|
my %param;
|
|
|
|
foreach (@params) {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($k, $v) = split /=/, $_, 2;
|
2006-04-07 20:58:02 +02:00
|
|
|
$param{lc $k} = $v;
|
|
|
|
}
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2006-04-07 20:58:02 +02:00
|
|
|
# to support addresses without <> we now require a plugin
|
2013-04-21 06:08:43 +02:00
|
|
|
# hooking "mail_pre" to
|
|
|
|
# return (OK, "<$from>");
|
2006-04-07 20:58:02 +02:00
|
|
|
# (...or anything else parseable by Qpsmtpd::Address ;-))
|
|
|
|
# see also comment in sub rcpt()
|
2006-12-08 20:46:18 +01:00
|
|
|
$self->run_hooks("mail_pre", $from, \%param);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub mail_pre_respond {
|
|
|
|
my ($self, $rc, $msg, $args) = @_;
|
|
|
|
my ($from, $param) = @$args;
|
2006-04-07 20:58:02 +02:00
|
|
|
if ($rc == OK) {
|
2013-04-21 06:08:43 +02:00
|
|
|
$from = shift @$msg;
|
2006-04-07 20:58:02 +02:00
|
|
|
}
|
2004-07-16 04:22:11 +02:00
|
|
|
|
2008-09-30 05:22:34 +02:00
|
|
|
$self->log(LOGDEBUG, "from email address : [$from]");
|
2013-04-21 06:08:43 +02:00
|
|
|
return $self->respond(501, "could not parse your mail from command")
|
2006-04-07 20:58:02 +02:00
|
|
|
unless $from =~ /^<.*>$/;
|
2004-11-22 21:50:57 +01:00
|
|
|
|
Changes by jpeacock@cpan.org (John Peacock)
o plugins/check_badmailfromto
- New plugin in the style of check_badmailfrom, which matches a pair
of FROM/TO and makes it seem like the recipient's address no longer
exists (but only from the matching sender's point of view). Useful
for stalkers and other harassment cases.
o plugins/dns_whitelist_soft
- New plugin to provide a DNS-based whitelist (good for distributed
sites).
o various files
- Replaced tab character with 8 spaces and adjusted line breaks for
better readability.
Changes by mct@toren.net (Michael C. Toren)
o lib/Qpsmtpd/SMTP.pm
- Assumes a MAIL FROM value of "<#@[]>" (utilized by qmail to
indicate a null sender when generating a doublebounce message)
is equivalent to "<>". Previously qpsmtpd complained that the
value could not be parsed.
- Adds LOGIN to the default list of supported auth mechanisms.
The documentation in Auth.pm indicated that auth-login was not
currently supported due to lack of functionality, however I can
confirm that LOGIN appears to work fine as tested by using msmtp
(http://msmtp.sourceforge.net/). Are there any indications that
LOGIN support is actually broken in the current implementation?
- Removes the "X-Qpsmtpd-Auth: True" header appended when a message
has been sent by an authenticated user. One problem with such a
header is that it's impossible to say which SMTP hop added it,
and it provides no information which could be used to backtrack
the transaction. I grepped through my mail archives a bit
looking for how other MTAs handled the problem, and decided it
would be best to place this information in the Received: header:
Received: from remotehost (HELO remotehost) (192.168.42.42)
(smtp-auth username foo, mechanism cram-md5)
by mail.netisland.net (qpsmtpd/0.28) with ESMTP; <date>
o lib/Qpsmtpd/Auth.pm:
- Documentation update for the arguments passed to an auth
handler; previously the $mechanism argument was not mentioned,
which threw off the argument offsets.
- Documentation update for auth-login removing the warning
that auth-login is not currently supported due to lack of
functionality.
- Fix to execute a generic auth hook when a more specific
auth-$mechanism hook does not exist. (Previously posted
to the list last week.)
- Upon authentication, sets $session->{_auth_user} and
$session->{_auth_mechanism} so that SMTP.pm can include them
in the Received: header.
o plugins/queue/qmail-queue
- Added a timestamp and the qmail-queue qp identifier to the
"Queued!" 250 message, for compatibility with qmail-smtpd, which
can be very useful for tracking message delivery from machine to
machine. For example, the new 250 message might be:
250 Queued! 1105927468 qp 3210 <1105927457@netisland.net>
qmail-smtpd returns:
250 ok 1106546213 qp 7129
Additionally, for consistency angle brackets are placed around
the Message-ID displayed in the 250 if they were missing in the
message header.
o plugins/check_badmailfrom:
- Changed the error message from "Mail from $bad not accepted
here" to "sorry, your envelope sender is in my badmailfrom
list", for compatibility with qmail-smtpd. I didn't see any
reason to share with the sender the value of $bad, especially
for situations where the sender was rejected resulting from a
wildcard.
o plugins/check_earlytalker:
o plugins/require_resolvable_fromhost:
- No longer checks for earlytalkers or resolvable senders if the
connection note "whitelistclient" is set, which is nice for
helping backup MX hosts empty their queue faster.
o plugins/count_unrecognized_commands:
- Return code changed from DENY_DISCONNECT, which isn't valid in
an unrecognized_command hook, to DENY, which in this context
drops the connection anyway. (Previously posted to the list
last week.)
git-svn-id: https://svn.perl.org/qpsmtpd/trunk@356 958fd67b-6ff1-0310-b445-bb7760255be9
2005-01-28 04:30:50 +01:00
|
|
|
if ($from eq "<>" or $from =~ m/\[undefined\]/ or $from eq "<#@[]>") {
|
2013-04-21 06:08:43 +02:00
|
|
|
$from = Qpsmtpd::Address->new("<>");
|
|
|
|
}
|
2002-09-24 12:56:35 +02:00
|
|
|
else {
|
2013-04-21 06:08:43 +02:00
|
|
|
$from = (Qpsmtpd::Address->parse($from))[0];
|
2002-09-24 12:56:35 +02:00
|
|
|
}
|
2013-04-21 06:08:43 +02:00
|
|
|
return $self->respond(501, "could not parse your mail from command")
|
|
|
|
unless $from;
|
2002-09-24 12:56:35 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
$self->run_hooks("mail", $from, %$param);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub mail_respond {
|
|
|
|
my ($self, $rc, $msg, $args) = @_;
|
|
|
|
my ($from, $param) = @$args;
|
2002-09-24 12:56:35 +02:00
|
|
|
if ($rc == DONE) {
|
2013-04-21 06:08:43 +02:00
|
|
|
return 1;
|
2002-09-24 12:56:35 +02:00
|
|
|
}
|
|
|
|
elsif ($rc == DENY) {
|
2013-04-21 06:08:43 +02:00
|
|
|
$msg->[0] ||= $from->format . ', denied';
|
|
|
|
$self->log(LOGINFO, "deny mail from " . $from->format . " (@$msg)");
|
|
|
|
$self->respond(550, @$msg);
|
2002-09-24 12:56:35 +02:00
|
|
|
}
|
|
|
|
elsif ($rc == DENYSOFT) {
|
2013-04-21 06:08:43 +02:00
|
|
|
$msg->[0] ||= $from->format . ', temporarily denied';
|
|
|
|
$self->log(LOGINFO, "denysoft mail from " . $from->format . " (@$msg)");
|
|
|
|
$self->respond(450, @$msg);
|
2002-09-24 12:56:35 +02:00
|
|
|
}
|
2004-06-15 00:25:52 +02:00
|
|
|
elsif ($rc == DENY_DISCONNECT) {
|
2013-04-21 06:08:43 +02:00
|
|
|
$msg->[0] ||= $from->format . ', denied';
|
|
|
|
$self->log(LOGINFO, "deny mail from " . $from->format . " (@$msg)");
|
|
|
|
$self->respond(550, @$msg);
|
|
|
|
$self->disconnect;
|
2003-12-03 09:07:36 +01:00
|
|
|
}
|
2004-06-15 00:25:52 +02:00
|
|
|
elsif ($rc == DENYSOFT_DISCONNECT) {
|
2013-04-21 06:08:43 +02:00
|
|
|
$msg->[0] ||= $from->format . ', temporarily denied';
|
|
|
|
$self->log(LOGINFO, "denysoft mail from " . $from->format . " (@$msg)");
|
|
|
|
$self->respond(421, @$msg);
|
|
|
|
$self->disconnect;
|
2004-06-11 08:06:30 +02:00
|
|
|
}
|
2013-04-21 06:08:43 +02:00
|
|
|
else { # includes OK
|
|
|
|
$self->log(LOGDEBUG, "getting mail from " . $from->format);
|
|
|
|
$self->respond(
|
|
|
|
250,
|
|
|
|
$from->format
|
|
|
|
. ", sender OK - how exciting to get mail from you!"
|
|
|
|
);
|
|
|
|
$self->transaction->sender($from);
|
2002-09-24 12:56:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub rcpt {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $line) = @_;
|
2013-04-30 05:50:18 +02:00
|
|
|
$self->connection->notes('envelope_rcpt', $line);
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->run_hooks("rcpt_parse", $line);
|
2006-12-08 20:46:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub rcpt_parse_respond {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $rc, $msg, $args) = @_;
|
|
|
|
my ($line) = @$args;
|
|
|
|
my ($ok, $rcpt, @param) = Qpsmtpd::Command->parse("rcpt", $line, $msg->[0]);
|
|
|
|
return $self->respond(501, $rcpt || "Syntax error in command")
|
|
|
|
unless ($ok == OK);
|
|
|
|
return $self->respond(503, "Use MAIL before RCPT")
|
|
|
|
unless $self->transaction->sender;
|
|
|
|
|
|
|
|
my %param;
|
|
|
|
foreach (@param) {
|
|
|
|
my ($k, $v) = split /=/, $_, 2;
|
|
|
|
$param{lc $k} = $v;
|
|
|
|
}
|
|
|
|
|
|
|
|
# to support addresses without <> we now require a plugin
|
|
|
|
# hooking "rcpt_pre" to
|
|
|
|
# return (OK, "<$rcpt>");
|
|
|
|
# (... or anything else parseable by Qpsmtpd::Address ;-))
|
|
|
|
# this means, a plugin can decide to (pre-)accept
|
|
|
|
# addresses like <user@example.com.> or <user@example.com >
|
2013-04-30 05:50:18 +02:00
|
|
|
# by removing the trailing dot or space from this example.
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->run_hooks("rcpt_pre", $rcpt, \%param);
|
2006-12-08 20:46:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub rcpt_pre_respond {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $rc, $msg, $args) = @_;
|
|
|
|
my ($rcpt, $param) = @$args;
|
|
|
|
if ($rc == OK) {
|
|
|
|
$rcpt = shift @$msg;
|
|
|
|
}
|
|
|
|
$self->log(LOGDEBUG, "to email address : [$rcpt]");
|
|
|
|
return $self->respond(501, "could not parse recipient")
|
|
|
|
unless $rcpt =~ /^<.*>$/;
|
2006-04-07 20:58:02 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
$rcpt = (Qpsmtpd::Address->parse($rcpt))[0];
|
2002-09-24 12:56:35 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
return $self->respond(501, "could not parse recipient")
|
|
|
|
if (!$rcpt or ($rcpt->format eq '<>'));
|
2002-09-24 12:56:35 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->run_hooks("rcpt", $rcpt, %$param);
|
2006-12-08 20:46:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub rcpt_respond {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $rc, $msg, $args) = @_;
|
|
|
|
my ($rcpt, $param) = @$args;
|
|
|
|
if ($rc == DONE) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
elsif ($rc == DENY) {
|
|
|
|
$msg->[0] ||= 'relaying denied';
|
|
|
|
$self->respond(550, @$msg);
|
|
|
|
}
|
|
|
|
elsif ($rc == DENYSOFT) {
|
|
|
|
$msg->[0] ||= 'relaying denied';
|
|
|
|
return $self->respond(450, @$msg);
|
|
|
|
}
|
|
|
|
elsif ($rc == DENY_DISCONNECT) {
|
|
|
|
$msg->[0] ||= 'delivery denied';
|
|
|
|
$self->log(LOGDEBUG, "delivery denied (@$msg)");
|
|
|
|
$self->respond(550, @$msg);
|
|
|
|
$self->disconnect;
|
|
|
|
}
|
|
|
|
elsif ($rc == DENYSOFT_DISCONNECT) {
|
|
|
|
$msg->[0] ||= 'relaying denied';
|
|
|
|
$self->log(LOGDEBUG, "delivery denied (@$msg)");
|
|
|
|
$self->respond(421, @$msg);
|
|
|
|
$self->disconnect;
|
|
|
|
}
|
|
|
|
elsif ($rc == OK) {
|
|
|
|
$self->respond(250, $rcpt->format . ", recipient ok");
|
|
|
|
return $self->transaction->add_recipient($rcpt);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $self->respond(450, "No plugin decided if relaying is allowed");
|
|
|
|
}
|
|
|
|
return 0;
|
2002-09-24 12:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub help {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, @args) = @_;
|
|
|
|
$self->run_hooks("help", @args);
|
2007-12-07 10:12:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub help_respond {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $rc, $msg, $args) = @_;
|
2007-12-07 10:12:15 +01:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
return 1
|
|
|
|
if $rc == DONE;
|
2007-12-07 10:12:15 +01:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
if ($rc == DENY) {
|
|
|
|
$msg->[0] ||= "Syntax error, command not recognized";
|
|
|
|
$self->respond(500, @$msg);
|
2007-12-07 10:12:15 +01:00
|
|
|
}
|
2013-04-21 06:08:43 +02:00
|
|
|
else {
|
|
|
|
unless ($msg->[0]) {
|
|
|
|
@$msg = (
|
|
|
|
"This is qpsmtpd "
|
|
|
|
. ($self->config('smtpgreeting') ? '' : $self->version),
|
|
|
|
"See http://smtpd.develooper.com/",
|
|
|
|
'To report bugs or send comments, mail to <ask@develooper.com>.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$self->respond(214, @$msg);
|
|
|
|
}
|
|
|
|
return 1;
|
2002-09-24 12:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub noop {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
|
|
|
$self->run_hooks("noop");
|
2008-01-24 19:43:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub noop_respond {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $rc, $msg, $args) = @_;
|
|
|
|
return 1 if $rc == DONE;
|
2008-01-24 19:43:34 +01:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
if ($rc == DENY || $rc == DENY_DISCONNECT) {
|
|
|
|
$msg->[0] ||= "Stop wasting my time."; # FIXME: better default message?
|
|
|
|
$self->respond(500, @$msg);
|
|
|
|
$self->disconnect if $rc == DENY_DISCONNECT;
|
|
|
|
return 1;
|
|
|
|
}
|
2008-01-24 19:43:34 +01:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->respond(250, "OK");
|
|
|
|
return 1;
|
2002-09-24 12:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub vrfy {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
2004-09-05 06:30:21 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
# Note, this doesn't support the multiple ambiguous results
|
|
|
|
# documented in RFC2821#3.5.1
|
|
|
|
# I also don't think it provides all the proper result codes.
|
2004-09-05 06:30:21 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->run_hooks("vrfy");
|
2006-12-08 20:46:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub vrfy_respond {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $rc, $msg, $args) = @_;
|
|
|
|
if ($rc == DONE) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
elsif ($rc == DENY) {
|
|
|
|
$msg->[0] ||= "Access Denied";
|
|
|
|
$self->respond(554, @$msg);
|
|
|
|
$self->reset_transaction();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
elsif ($rc == OK) {
|
|
|
|
$msg->[0] ||= "User OK";
|
|
|
|
$self->respond(250, @$msg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else { # $rc == DECLINED or anything else
|
|
|
|
$self->respond(252,
|
|
|
|
"Just try sending a mail and we'll see how it turns out ...");
|
|
|
|
return 1;
|
|
|
|
}
|
2002-09-24 12:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub rset {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
|
|
|
$self->reset_transaction;
|
|
|
|
$self->respond(250, "OK");
|
2002-09-24 12:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub quit {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
|
|
|
$self->run_hooks("quit");
|
2006-12-08 20:46:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub quit_respond {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $rc, $msg, $args) = @_;
|
|
|
|
if ($rc != DONE) {
|
|
|
|
$msg->[0] ||=
|
|
|
|
$self->config('me') . " closing connection. Have a wonderful day.";
|
|
|
|
$self->respond(221, @$msg);
|
|
|
|
}
|
|
|
|
$self->disconnect();
|
2002-09-24 12:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub disconnect {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
|
|
|
$self->run_hooks("disconnect");
|
|
|
|
$self->connection->notes(disconnected => 1);
|
|
|
|
$self->reset_transaction;
|
2002-09-24 12:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub data {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
|
|
|
$self->run_hooks("data");
|
2006-12-08 20:46:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub data_respond {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $rc, $msg, $args) = @_;
|
|
|
|
if ($rc == DONE) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
elsif ($rc == DENY) {
|
|
|
|
$msg->[0] ||= "Message denied";
|
|
|
|
$self->respond(554, @$msg);
|
|
|
|
$self->reset_transaction();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
elsif ($rc == DENYSOFT) {
|
|
|
|
$msg->[0] ||= "Message denied temporarily";
|
|
|
|
$self->respond(451, @$msg);
|
|
|
|
$self->reset_transaction();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
elsif ($rc == DENY_DISCONNECT) {
|
|
|
|
$msg->[0] ||= "Message denied";
|
|
|
|
$self->respond(554, @$msg);
|
|
|
|
$self->disconnect;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
elsif ($rc == DENYSOFT_DISCONNECT) {
|
|
|
|
$msg->[0] ||= "Message denied temporarily";
|
|
|
|
$self->respond(421, @$msg);
|
|
|
|
$self->disconnect;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
$self->respond(503, "MAIL first"), return 1
|
|
|
|
unless $self->transaction->sender;
|
|
|
|
$self->respond(503, "RCPT first"), return 1
|
|
|
|
unless $self->transaction->recipients;
|
|
|
|
$self->respond(354, "go ahead");
|
|
|
|
|
|
|
|
my $buffer = '';
|
|
|
|
my $size = 0;
|
|
|
|
my $i = 0;
|
|
|
|
my $max_size =
|
|
|
|
($self->config('databytes'))[0] || 0; # this should work in scalar context
|
|
|
|
my $blocked = "";
|
|
|
|
my %matches;
|
|
|
|
my $in_header = 1;
|
|
|
|
my $complete = 0;
|
|
|
|
|
|
|
|
$self->log(LOGDEBUG, "max_size: $max_size / size: $size");
|
|
|
|
|
|
|
|
my $header = Mail::Header->new(Modify => 0, MailFrom => "COERCE");
|
|
|
|
|
|
|
|
my $timeout = $self->config('timeout');
|
|
|
|
while (defined($_ = $self->getline($timeout))) {
|
|
|
|
if ($_ eq ".\r\n") {
|
|
|
|
$complete++;
|
|
|
|
$_ = '';
|
|
|
|
}
|
|
|
|
$i++;
|
2004-03-04 05:30:02 +01:00
|
|
|
|
|
|
|
# should probably use \012 and \015 in these checks instead of \r and \n ...
|
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
# Reject messages that have either bare LF or CR. rjkaes noticed a
|
|
|
|
# lot of spam that is malformed in the header.
|
2004-03-04 05:30:02 +01:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
($_ eq ".\n" or $_ eq ".\r")
|
|
|
|
and $self->respond(421, "See http://smtpd.develooper.com/barelf.html")
|
|
|
|
and return $self->disconnect;
|
2004-03-04 05:30:02 +01:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
# add a transaction->blocked check back here when we have line by line plugin access...
|
|
|
|
unless (($max_size and $size > $max_size)) {
|
|
|
|
s/\r\n$/\n/;
|
|
|
|
s/^\.\./\./;
|
|
|
|
if ($in_header && (m/^$/ || $complete > 0)) {
|
|
|
|
$in_header = 0;
|
|
|
|
my @headers = split /^/m, $buffer;
|
2002-09-24 12:56:35 +02:00
|
|
|
|
Changes by jpeacock@cpan.org (John Peacock)
o plugins/check_badmailfromto
- New plugin in the style of check_badmailfrom, which matches a pair
of FROM/TO and makes it seem like the recipient's address no longer
exists (but only from the matching sender's point of view). Useful
for stalkers and other harassment cases.
o plugins/dns_whitelist_soft
- New plugin to provide a DNS-based whitelist (good for distributed
sites).
o various files
- Replaced tab character with 8 spaces and adjusted line breaks for
better readability.
Changes by mct@toren.net (Michael C. Toren)
o lib/Qpsmtpd/SMTP.pm
- Assumes a MAIL FROM value of "<#@[]>" (utilized by qmail to
indicate a null sender when generating a doublebounce message)
is equivalent to "<>". Previously qpsmtpd complained that the
value could not be parsed.
- Adds LOGIN to the default list of supported auth mechanisms.
The documentation in Auth.pm indicated that auth-login was not
currently supported due to lack of functionality, however I can
confirm that LOGIN appears to work fine as tested by using msmtp
(http://msmtp.sourceforge.net/). Are there any indications that
LOGIN support is actually broken in the current implementation?
- Removes the "X-Qpsmtpd-Auth: True" header appended when a message
has been sent by an authenticated user. One problem with such a
header is that it's impossible to say which SMTP hop added it,
and it provides no information which could be used to backtrack
the transaction. I grepped through my mail archives a bit
looking for how other MTAs handled the problem, and decided it
would be best to place this information in the Received: header:
Received: from remotehost (HELO remotehost) (192.168.42.42)
(smtp-auth username foo, mechanism cram-md5)
by mail.netisland.net (qpsmtpd/0.28) with ESMTP; <date>
o lib/Qpsmtpd/Auth.pm:
- Documentation update for the arguments passed to an auth
handler; previously the $mechanism argument was not mentioned,
which threw off the argument offsets.
- Documentation update for auth-login removing the warning
that auth-login is not currently supported due to lack of
functionality.
- Fix to execute a generic auth hook when a more specific
auth-$mechanism hook does not exist. (Previously posted
to the list last week.)
- Upon authentication, sets $session->{_auth_user} and
$session->{_auth_mechanism} so that SMTP.pm can include them
in the Received: header.
o plugins/queue/qmail-queue
- Added a timestamp and the qmail-queue qp identifier to the
"Queued!" 250 message, for compatibility with qmail-smtpd, which
can be very useful for tracking message delivery from machine to
machine. For example, the new 250 message might be:
250 Queued! 1105927468 qp 3210 <1105927457@netisland.net>
qmail-smtpd returns:
250 ok 1106546213 qp 7129
Additionally, for consistency angle brackets are placed around
the Message-ID displayed in the 250 if they were missing in the
message header.
o plugins/check_badmailfrom:
- Changed the error message from "Mail from $bad not accepted
here" to "sorry, your envelope sender is in my badmailfrom
list", for compatibility with qmail-smtpd. I didn't see any
reason to share with the sender the value of $bad, especially
for situations where the sender was rejected resulting from a
wildcard.
o plugins/check_earlytalker:
o plugins/require_resolvable_fromhost:
- No longer checks for earlytalkers or resolvable senders if the
connection note "whitelistclient" is set, which is nice for
helping backup MX hosts empty their queue faster.
o plugins/count_unrecognized_commands:
- Return code changed from DENY_DISCONNECT, which isn't valid in
an unrecognized_command hook, to DENY, which in this context
drops the connection anyway. (Previously posted to the list
last week.)
git-svn-id: https://svn.perl.org/qpsmtpd/trunk@356 958fd67b-6ff1-0310-b445-bb7760255be9
2005-01-28 04:30:50 +01:00
|
|
|
# ... need to check that we don't reformat any of the received lines.
|
|
|
|
#
|
|
|
|
# 3.8.2 Received Lines in Gatewaying
|
|
|
|
# When forwarding a message into or out of the Internet environment, a
|
|
|
|
# gateway MUST prepend a Received: line, but it MUST NOT alter in any
|
|
|
|
# way a Received: line that is already in the header.
|
2002-09-24 12:56:35 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
$header->extract(\@headers);
|
2002-10-10 03:49:34 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
#$header->add("X-SMTPD", "qpsmtpd/".$self->version.", http://smtpd.develooper.com/");
|
2002-09-24 12:56:35 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
$buffer = "";
|
2009-02-09 22:25:51 +01:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->transaction->header($header);
|
2002-09-24 12:56:35 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
# NOTE: This will not work properly under async. A
|
|
|
|
# data_headers_end_respond needs to be created.
|
|
|
|
my ($rc, $msg) = $self->run_hooks('data_headers_end');
|
|
|
|
if ($rc == DENY_DISCONNECT) {
|
|
|
|
$self->respond(554, $msg || "Message denied");
|
|
|
|
$self->disconnect;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
elsif ($rc == DENYSOFT_DISCONNECT) {
|
|
|
|
$self->respond(421, $msg || "Message denied temporarily");
|
|
|
|
$self->disconnect;
|
|
|
|
return 1;
|
|
|
|
}
|
2005-03-01 15:31:25 +01:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
# Save the start of just the body itself
|
|
|
|
$self->transaction->set_body_start();
|
2002-09-24 12:56:35 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
2002-09-24 12:56:35 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
# grab a copy of all of the header lines
|
|
|
|
if ($in_header) {
|
|
|
|
$buffer .= $_;
|
|
|
|
}
|
|
|
|
|
|
|
|
# copy all lines into the spool file, including the headers
|
|
|
|
# we will create a new header later before sending onwards
|
|
|
|
$self->transaction->body_write($_) if !$complete;
|
|
|
|
$size += length $_;
|
|
|
|
}
|
|
|
|
last if $complete > 0;
|
|
|
|
|
|
|
|
#$self->log(LOGDEBUG, "size is at $size\n") unless ($i % 300);
|
2002-09-24 12:56:35 +02:00
|
|
|
}
|
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->log(LOGDEBUG, "max_size: $max_size / size: $size");
|
2002-09-24 12:56:35 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
# if we get here without seeing a terminator, the connection is
|
|
|
|
# probably dead.
|
|
|
|
unless ($complete) {
|
|
|
|
$self->respond(451, "Incomplete DATA");
|
|
|
|
$self->reset_transaction; # clean up after ourselves
|
|
|
|
return 1;
|
|
|
|
}
|
2002-09-24 12:56:35 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
#$self->respond(550, $self->transaction->blocked),return 1 if ($self->transaction->blocked);
|
|
|
|
if ($max_size and $size > $max_size) {
|
|
|
|
$self->log(LOGALERT,
|
|
|
|
"Message too big: size: $size (max size: $max_size)");
|
|
|
|
$self->respond(552, "Message too big!");
|
|
|
|
$self->reset_transaction; # clean up after ourselves
|
|
|
|
return 1;
|
|
|
|
}
|
2002-09-24 12:56:35 +02:00
|
|
|
|
2014-06-04 05:59:52 +02:00
|
|
|
$self->authentication_results();
|
|
|
|
$self->received_line();
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->run_hooks("data_post");
|
2006-12-08 20:46:18 +01:00
|
|
|
}
|
|
|
|
|
2013-05-02 09:30:48 +02:00
|
|
|
sub authentication_results {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
my @auth_list = $self->config('me');
|
|
|
|
# $self->clean_authentication_results();
|
|
|
|
|
|
|
|
if ( ! defined $self->{_auth} ) {
|
|
|
|
push @auth_list, 'auth=none';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
my $mechanism = "(" . $self->{_auth_mechanism} . ")";
|
|
|
|
my $user = "smtp.auth=" . $self->{_auth_user};
|
|
|
|
if ( $self->{_auth} == OK) {
|
|
|
|
push @auth_list, "auth=pass $mechanism $user";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
push @auth_list, "auth=fail $mechanism $user";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
# RFC 5451: used in AUTH, DKIM, DOMAINKEYS, SENDERID, SPF
|
|
|
|
if ( $self->connection->notes('authentication_results') ) {
|
|
|
|
push @auth_list, $self->connection->notes('authentication_results');
|
|
|
|
};
|
|
|
|
|
|
|
|
$self->log(LOGDEBUG, "adding auth results header" );
|
2013-05-02 09:53:21 +02:00
|
|
|
$self->transaction->header->add('Authentication-Results', join('; ', @auth_list), 0);
|
2013-05-02 09:30:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
sub clean_authentication_results {
|
|
|
|
my $self = shift;
|
|
|
|
|
2013-12-17 23:13:51 +01:00
|
|
|
# http://tools.ietf.org/html/draft-kucherawy-original-authres-00.html
|
2013-05-02 09:30:48 +02:00
|
|
|
|
2013-12-17 23:13:51 +01:00
|
|
|
# On messages received from the internet, move Authentication-Results headers
|
|
|
|
# to Original-AR, so our downstream can trust the A-R header we insert.
|
|
|
|
|
|
|
|
# TODO: Do not invalidate DKIM signatures.
|
|
|
|
# if $self->transaction->header->get('DKIM-Signature')
|
|
|
|
# Parse the DKIM signature(s)
|
|
|
|
# return if A-R header is signed;
|
|
|
|
# }
|
|
|
|
|
|
|
|
my @ar_headers = $self->transaction->header->get('Authentication-Results');
|
|
|
|
for ( my $i = 0; $i < scalar @ar_headers; $i++ ) {
|
2013-05-02 09:30:48 +02:00
|
|
|
$self->transaction->header->delete('Authentication-Results', $i);
|
2013-12-17 23:13:51 +01:00
|
|
|
$self->transaction->header->add('Original-Authentication-Results', $ar_headers[$i]);
|
2013-05-02 09:30:48 +02:00
|
|
|
}
|
2013-12-17 23:13:51 +01:00
|
|
|
|
|
|
|
$self->log(LOGDEBUG, "Authentication-Results moved to Original-Authentication-Results" );
|
2013-05-02 09:30:48 +02:00
|
|
|
};
|
|
|
|
|
2007-04-09 16:45:11 +02:00
|
|
|
sub received_line {
|
2013-05-02 09:30:48 +02:00
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
my $smtp = $self->connection->hello eq "ehlo" ? "ESMTP" : "SMTP";
|
|
|
|
my $esmtp = substr($smtp, 0, 1) eq "E";
|
|
|
|
my $authheader = '';
|
|
|
|
my $sslheader = '';
|
|
|
|
|
|
|
|
if (defined $self->connection->notes('tls_enabled')
|
|
|
|
and $self->connection->notes('tls_enabled'))
|
|
|
|
{
|
|
|
|
$smtp .= "S" if $esmtp; # RFC3848
|
|
|
|
$sslheader = "("
|
|
|
|
. $self->connection->notes('tls_socket')->get_cipher()
|
|
|
|
. " encrypted) ";
|
|
|
|
}
|
|
|
|
if (defined $self->{_auth} && $self->{_auth} == OK) {
|
|
|
|
my $mech = $self->{_auth_mechanism};
|
|
|
|
my $user = $self->{_auth_user};
|
|
|
|
$smtp .= "A" if $esmtp; # RFC3848
|
|
|
|
$authheader = "(smtp-auth username $user, mechanism $mech)\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
my $header_str;
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($rc, @received) =
|
|
|
|
$self->run_hooks("received_line", $smtp, $authheader, $sslheader);
|
|
|
|
if ($rc == YIELD) {
|
|
|
|
die "YIELD not supported for received_line hook";
|
|
|
|
}
|
|
|
|
elsif ($rc == OK) {
|
|
|
|
return join("\n", @received);
|
|
|
|
}
|
|
|
|
else { # assume $rc == DECLINED
|
2013-05-02 09:30:48 +02:00
|
|
|
$header_str =
|
2013-04-21 06:08:43 +02:00
|
|
|
"from "
|
|
|
|
. $self->connection->remote_info
|
|
|
|
. " (HELO "
|
|
|
|
. $self->connection->hello_host . ") ("
|
|
|
|
. $self->connection->remote_ip
|
2013-04-26 09:35:02 +02:00
|
|
|
. ")\n by "
|
2013-04-21 06:08:43 +02:00
|
|
|
. $self->config('me')
|
|
|
|
. " (qpsmtpd/"
|
|
|
|
. $self->version
|
|
|
|
. ") with $sslheader$smtp; "
|
|
|
|
. (strftime('%a, %d %b %Y %H:%M:%S %z', localtime));
|
|
|
|
}
|
2013-05-02 09:30:48 +02:00
|
|
|
$self->transaction->header->add('Received', $header_str, 0 );
|
2007-04-09 16:45:11 +02:00
|
|
|
}
|
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
sub data_post_respond {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $rc, $msg, $args) = @_;
|
|
|
|
if ($rc == DONE) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
elsif ($rc == DENY) {
|
|
|
|
$msg->[0] ||= "Message denied";
|
|
|
|
$self->respond(552, @$msg);
|
|
|
|
|
|
|
|
# DATA is always the end of a "transaction"
|
|
|
|
return $self->reset_transaction;
|
|
|
|
}
|
|
|
|
elsif ($rc == DENYSOFT) {
|
|
|
|
$msg->[0] ||= "Message denied temporarily";
|
|
|
|
$self->respond(452, @$msg);
|
|
|
|
|
|
|
|
# DATA is always the end of a "transaction"
|
|
|
|
return $self->reset_transaction;
|
|
|
|
}
|
|
|
|
elsif ($rc == DENY_DISCONNECT) {
|
|
|
|
$msg->[0] ||= "Message denied";
|
|
|
|
$self->respond(552, @$msg);
|
|
|
|
$self->disconnect;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
elsif ($rc == DENYSOFT_DISCONNECT) {
|
|
|
|
$msg->[0] ||= "Message denied temporarily";
|
|
|
|
$self->respond(452, @$msg);
|
|
|
|
$self->disconnect;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$self->queue($self->transaction);
|
|
|
|
}
|
2002-09-24 12:56:35 +02:00
|
|
|
}
|
|
|
|
|
2004-09-14 07:48:39 +02:00
|
|
|
sub getline {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $timeout) = @_;
|
|
|
|
|
|
|
|
alarm $timeout;
|
|
|
|
my $line = <STDIN>; # default implementation
|
|
|
|
alarm 0;
|
|
|
|
return $line;
|
2004-09-14 07:48:39 +02:00
|
|
|
}
|
|
|
|
|
2002-09-24 12:56:35 +02:00
|
|
|
sub queue {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $transaction) = @_;
|
2002-09-24 12:56:35 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
# First fire any queue_pre hooks
|
|
|
|
$self->run_hooks("queue_pre");
|
2006-12-08 20:46:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub queue_pre_respond {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $rc, $msg, $args) = @_;
|
|
|
|
if ($rc == DONE) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
elsif ($rc != OK and $rc != DECLINED and $rc != 0) {
|
|
|
|
return $self->log(LOGERROR, "pre plugin returned illegal value");
|
|
|
|
return 0;
|
|
|
|
}
|
2006-03-01 17:46:55 +01:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
# If we got this far, run the queue hooks
|
|
|
|
$self->run_hooks("queue");
|
2006-12-08 20:46:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub queue_respond {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $rc, $msg, $args) = @_;
|
|
|
|
|
|
|
|
# reset transaction if we queued the mail
|
|
|
|
$self->reset_transaction;
|
|
|
|
|
|
|
|
if ($rc == DONE) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
elsif ($rc == OK) {
|
|
|
|
$msg->[0] ||= 'Queued';
|
|
|
|
$self->respond(250, @$msg);
|
|
|
|
}
|
|
|
|
elsif ($rc == DENY) {
|
|
|
|
$msg->[0] ||= 'Message denied';
|
|
|
|
$self->respond(552, @$msg);
|
|
|
|
}
|
|
|
|
elsif ($rc == DENYSOFT) {
|
|
|
|
$msg->[0] ||= 'Message denied temporarily';
|
|
|
|
$self->respond(452, @$msg);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$msg->[0] ||= 'Queuing declined or disabled; try again later';
|
|
|
|
$self->respond(451, @$msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
# And finally run any queue_post hooks
|
|
|
|
$self->run_hooks("queue_post");
|
2006-12-08 20:46:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub queue_post_respond {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $rc, $msg, $args) = @_;
|
|
|
|
$self->log(LOGERROR, @$msg) unless ($rc == OK or $rc == 0);
|
2002-09-24 12:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|