2006-12-08 20:46:18 +01:00
|
|
|
package Qpsmtpd::PollServer;
|
|
|
|
|
|
|
|
use base ('Danga::Client', 'Qpsmtpd::SMTP');
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
# use fields required to be a subclass of Danga::Client. Have to include
|
|
|
|
# all fields used by Qpsmtpd.pm here too.
|
|
|
|
use fields qw(
|
2013-04-21 06:08:43 +02:00
|
|
|
input_sock
|
|
|
|
mode
|
|
|
|
header_lines
|
|
|
|
in_header
|
|
|
|
data_size
|
|
|
|
max_size
|
|
|
|
hooks
|
|
|
|
start_time
|
|
|
|
cmd_timeout
|
|
|
|
conn
|
|
|
|
_auth
|
|
|
|
_auth_mechanism
|
|
|
|
_auth_state
|
|
|
|
_auth_ticket
|
|
|
|
_auth_user
|
|
|
|
_commands
|
|
|
|
_config_cache
|
|
|
|
_connection
|
|
|
|
_continuation
|
|
|
|
_extras
|
|
|
|
_test_mode
|
|
|
|
_transaction
|
|
|
|
);
|
2006-12-08 20:46:18 +01:00
|
|
|
use Qpsmtpd::Constants;
|
|
|
|
use Qpsmtpd::Address;
|
|
|
|
use ParaDNS;
|
|
|
|
use Mail::Header;
|
|
|
|
use POSIX qw(strftime);
|
|
|
|
use Socket qw(inet_aton AF_INET CRLF);
|
|
|
|
use Time::HiRes qw(time);
|
|
|
|
use strict;
|
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
sub max_idle_time { 60 }
|
2006-12-08 20:46:18 +01:00
|
|
|
sub max_connect_time { 1200 }
|
|
|
|
|
|
|
|
sub input_sock {
|
|
|
|
my $self = shift;
|
|
|
|
@_ and $self->{input_sock} = shift;
|
|
|
|
$self->{input_sock} || $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub new {
|
|
|
|
my Qpsmtpd::PollServer $self = shift;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
$self = fields::new($self) unless ref $self;
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->SUPER::new(@_);
|
2006-12-08 20:46:18 +01:00
|
|
|
$self->{cmd_timeout} = 5;
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->{start_time} = time;
|
|
|
|
$self->{mode} = 'connect';
|
2006-12-08 20:46:18 +01:00
|
|
|
$self->load_plugins;
|
|
|
|
$self->load_logging;
|
2008-05-12 16:42:41 +02:00
|
|
|
|
2008-05-12 19:19:31 +02:00
|
|
|
my ($rc, @msg) = $self->run_hooks_no_respond("pre-connection");
|
2008-05-12 16:42:41 +02:00
|
|
|
if ($rc == DENYSOFT || $rc == DENYSOFT_DISCONNECT) {
|
|
|
|
@msg = ("Sorry, try again later")
|
|
|
|
unless @msg;
|
|
|
|
$self->respond(451, @msg);
|
|
|
|
$self->disconnect;
|
|
|
|
}
|
|
|
|
elsif ($rc == DENY || $rc == DENY_DISCONNECT) {
|
|
|
|
@msg = ("Sorry, service not available for you")
|
|
|
|
unless @msg;
|
|
|
|
$self->respond(550, @msg);
|
|
|
|
$self->disconnect;
|
|
|
|
}
|
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub uptime {
|
|
|
|
my Qpsmtpd::PollServer $self = shift;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
return (time() - $self->{start_time});
|
|
|
|
}
|
|
|
|
|
|
|
|
sub reset_for_next_message {
|
|
|
|
my Qpsmtpd::PollServer $self = shift;
|
|
|
|
$self->SUPER::reset_for_next_message(@_);
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
$self->{_commands} = {
|
2014-05-30 08:21:08 +02:00
|
|
|
proxy => 1,
|
2013-04-21 06:08:43 +02:00
|
|
|
ehlo => 1,
|
|
|
|
helo => 1,
|
|
|
|
rset => 1,
|
|
|
|
mail => 1,
|
|
|
|
rcpt => 1,
|
|
|
|
data => 1,
|
|
|
|
help => 1,
|
|
|
|
vrfy => 1,
|
|
|
|
noop => 1,
|
|
|
|
quit => 1,
|
|
|
|
auth => 0, # disabled by default
|
|
|
|
};
|
|
|
|
$self->{mode} = 'cmd';
|
2006-12-08 20:46:18 +01:00
|
|
|
$self->{_extras} = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub respond {
|
|
|
|
my Qpsmtpd::PollServer $self = shift;
|
|
|
|
my ($code, @messages) = @_;
|
|
|
|
while (my $msg = shift @messages) {
|
|
|
|
my $line = $code . (@messages ? "-" : " ") . $msg;
|
|
|
|
$self->write("$line\r\n");
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub fault {
|
|
|
|
my Qpsmtpd::PollServer $self = shift;
|
|
|
|
$self->SUPER::fault(@_);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-02-08 22:26:25 +01:00
|
|
|
my %cmd_cache;
|
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
sub process_line {
|
|
|
|
my Qpsmtpd::PollServer $self = shift;
|
|
|
|
my $line = shift || return;
|
2013-04-21 06:08:43 +02:00
|
|
|
if ($::DEBUG > 1) { print "$$:" . ($self + 0) . "C($self->{mode}): $line"; }
|
2008-02-08 22:26:25 +01:00
|
|
|
if ($self->{mode} eq 'cmd') {
|
2008-05-09 17:27:11 +02:00
|
|
|
$line =~ s/\r?\n$//s;
|
|
|
|
$self->connection->notes('original_string', $line);
|
2008-02-08 22:26:25 +01:00
|
|
|
my ($cmd, @params) = split(/ +/, $line, 2);
|
|
|
|
my $meth = lc($cmd);
|
2013-04-21 06:08:43 +02:00
|
|
|
if (my $lookup =
|
|
|
|
$cmd_cache{$meth}
|
|
|
|
|| $self->{_commands}->{$meth} && $self->can($meth))
|
|
|
|
{
|
2008-02-08 22:26:25 +01:00
|
|
|
$cmd_cache{$meth} = $lookup;
|
2013-04-21 06:08:43 +02:00
|
|
|
eval { $lookup->($self, @params); };
|
2008-02-08 22:26:25 +01:00
|
|
|
if ($@) {
|
|
|
|
my $error = $@;
|
|
|
|
chomp($error);
|
|
|
|
$self->log(LOGERROR, "Command Error: $error");
|
|
|
|
$self->fault("command '$cmd' failed unexpectedly");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
# No such method - i.e. unrecognized command
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($rc, $msg) =
|
|
|
|
$self->run_hooks("unrecognized_command", $meth, @params);
|
2008-02-08 22:26:25 +01:00
|
|
|
}
|
2006-12-08 20:46:18 +01:00
|
|
|
}
|
2008-02-08 22:26:25 +01:00
|
|
|
elsif ($self->{mode} eq 'connect') {
|
2006-12-08 20:46:18 +01:00
|
|
|
$self->{mode} = 'cmd';
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2008-02-08 22:26:25 +01:00
|
|
|
# I've removed an eval{} from around this. It shouldn't ever die()
|
|
|
|
# but if it does we're a bit screwed... Ah well :-)
|
|
|
|
$self->start_conversation;
|
2006-12-08 20:46:18 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
die "Unknown mode";
|
|
|
|
}
|
2008-02-08 22:26:25 +01:00
|
|
|
return;
|
2006-12-08 20:46:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub disconnect {
|
|
|
|
my Qpsmtpd::PollServer $self = shift;
|
|
|
|
$self->SUPER::disconnect(@_);
|
|
|
|
$self->close;
|
|
|
|
}
|
|
|
|
|
2008-05-10 07:53:39 +02:00
|
|
|
sub close {
|
|
|
|
my Qpsmtpd::PollServer $self = shift;
|
2008-05-12 19:19:31 +02:00
|
|
|
$self->run_hooks_no_respond("post-connection");
|
2008-05-10 07:55:56 +02:00
|
|
|
$self->connection->reset;
|
2008-05-10 07:53:39 +02:00
|
|
|
$self->SUPER::close;
|
|
|
|
}
|
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
sub start_conversation {
|
|
|
|
my Qpsmtpd::PollServer $self = shift;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
my $conn = $self->connection;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
# set remote_host, remote_ip and remote_port
|
2012-11-19 18:32:21 +01:00
|
|
|
my ($ip, $port) = split(/:/, $self->peer_addr_string);
|
2007-03-21 14:42:57 +01:00
|
|
|
return $self->close() unless $ip;
|
2006-12-08 20:46:18 +01:00
|
|
|
$conn->remote_ip($ip);
|
|
|
|
$conn->remote_port($port);
|
|
|
|
$conn->remote_info("[$ip]");
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($lip, $lport) = split(/:/, $self->local_addr_string);
|
2008-12-15 21:48:42 +01:00
|
|
|
$conn->local_ip($lip);
|
|
|
|
$conn->local_port($lport);
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2007-10-08 20:24:35 +02:00
|
|
|
ParaDNS->new(
|
2013-04-21 06:08:43 +02:00
|
|
|
finished => sub { $self->continue_read(); $self->run_hooks("connect") },
|
|
|
|
|
2007-10-08 20:24:35 +02:00
|
|
|
# NB: Setting remote_info to the same as remote_host
|
2013-04-21 06:08:43 +02:00
|
|
|
callback => sub { $conn->remote_info($conn->remote_host($_[0])) },
|
|
|
|
host => $ip,
|
|
|
|
);
|
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub data {
|
|
|
|
my Qpsmtpd::PollServer $self = shift;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
my ($rc, $msg) = $self->run_hooks("data");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub data_respond {
|
|
|
|
my Qpsmtpd::PollServer $self = shift;
|
|
|
|
my ($rc, $msg) = @_;
|
|
|
|
if ($rc == DONE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
elsif ($rc == DENY) {
|
2008-05-01 08:20:45 +02:00
|
|
|
$msg->[0] ||= "Message denied";
|
|
|
|
$self->respond(554, @$msg);
|
2006-12-08 20:46:18 +01:00
|
|
|
$self->reset_transaction();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
elsif ($rc == DENYSOFT) {
|
2008-05-01 08:20:45 +02:00
|
|
|
$msg->[0] ||= "Message denied temporarily";
|
|
|
|
$self->respond(451, @$msg);
|
2006-12-08 20:46:18 +01:00
|
|
|
$self->reset_transaction();
|
|
|
|
return;
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
2006-12-08 20:46:18 +01:00
|
|
|
elsif ($rc == DENY_DISCONNECT) {
|
2008-05-01 08:20:45 +02:00
|
|
|
$msg->[0] ||= "Message denied";
|
|
|
|
$self->respond(554, @$msg);
|
2006-12-08 20:46:18 +01:00
|
|
|
$self->disconnect;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
elsif ($rc == DENYSOFT_DISCONNECT) {
|
2008-05-01 08:20:45 +02:00
|
|
|
$msg->[0] ||= "Message denied temporarily";
|
|
|
|
$self->respond(451, @$msg);
|
2006-12-08 20:46:18 +01:00
|
|
|
$self->disconnect;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return $self->respond(503, "MAIL first") unless $self->transaction->sender;
|
2013-04-21 06:08:43 +02:00
|
|
|
return $self->respond(503, "RCPT first")
|
|
|
|
unless $self->transaction->recipients;
|
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
$self->{header_lines} = '';
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->{data_size} = 0;
|
|
|
|
$self->{in_header} = 1;
|
|
|
|
$self->{max_size} = ($self->config('databytes'))[0] || 0;
|
|
|
|
|
|
|
|
$self->log(LOGDEBUG,
|
|
|
|
"max_size: $self->{max_size} / size: $self->{data_size}");
|
2006-12-08 20:46:18 +01:00
|
|
|
|
|
|
|
$self->respond(354, "go ahead");
|
|
|
|
|
|
|
|
my $max_get = $self->{max_size} || 1048576;
|
|
|
|
$self->get_chunks($max_get, sub { $self->got_data($_[0]) });
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub got_data {
|
|
|
|
my Qpsmtpd::PollServer $self = shift;
|
|
|
|
my $data = shift;
|
|
|
|
|
|
|
|
my $done = 0;
|
|
|
|
my $remainder;
|
2008-05-01 08:15:32 +02:00
|
|
|
if ($data =~ s/^\.\r\n(.*)\z//ms) {
|
2006-12-08 20:46:18 +01:00
|
|
|
$remainder = $1;
|
2013-04-21 06:08:43 +02:00
|
|
|
$done = 1;
|
2006-12-08 20:46:18 +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...
|
2006-12-08 20:46:18 +01:00
|
|
|
unless (($self->{max_size} and $self->{data_size} > $self->{max_size})) {
|
|
|
|
$data =~ s/\r\n/\n/mg;
|
|
|
|
$data =~ s/^\.\./\./mg;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2008-03-18 16:20:13 +01:00
|
|
|
if ($self->{in_header}) {
|
|
|
|
$self->{header_lines} .= $data;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2008-03-18 16:20:13 +01:00
|
|
|
if ($self->{header_lines} =~ s/\n(\n.*)\z/\n/ms) {
|
|
|
|
$data = $1;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2008-03-18 16:20:13 +01:00
|
|
|
# end of headers
|
|
|
|
$self->{in_header} = 0;
|
2013-04-21 06:08:43 +02: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.
|
2008-03-18 16:20:13 +01:00
|
|
|
my @header_lines = split(/^/m, $self->{header_lines});
|
2013-04-21 06:08:43 +02:00
|
|
|
|
|
|
|
my $header =
|
|
|
|
Mail::Header->new(
|
|
|
|
\@header_lines,
|
|
|
|
Modify => 0,
|
|
|
|
MailFrom => "COERCE"
|
|
|
|
);
|
2008-03-18 16:20:13 +01:00
|
|
|
$self->transaction->header($header);
|
2008-09-26 20:59:12 +02:00
|
|
|
$self->transaction->body_write($self->{header_lines});
|
2008-03-18 16:20:13 +01:00
|
|
|
$self->{header_lines} = '';
|
2006-12-08 20:46:18 +01:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
#$header->add("X-SMTPD", "qpsmtpd/".$self->version.", http://smtpd.develooper.com/");
|
|
|
|
|
2008-03-18 16:20:13 +01:00
|
|
|
# FIXME - call plugins to work on just the header here; can
|
|
|
|
# save us buffering the mail content.
|
2013-04-21 06:08:43 +02:00
|
|
|
|
|
|
|
# Save the start of just the body itself
|
2008-03-18 16:20:13 +01:00
|
|
|
$self->transaction->set_body_start();
|
|
|
|
}
|
2006-12-08 20:46:18 +01:00
|
|
|
}
|
2007-03-19 22:40:56 +01:00
|
|
|
|
|
|
|
$self->transaction->body_write(\$data);
|
2006-12-08 20:46:18 +01:00
|
|
|
$self->{data_size} += length $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($done) {
|
|
|
|
$self->end_of_data;
|
|
|
|
$self->end_get_chunks($remainder);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
sub end_of_data {
|
|
|
|
my Qpsmtpd::PollServer $self = shift;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
#$self->log(LOGDEBUG, "size is at $size\n") unless ($i % 300);
|
2013-04-21 06:08:43 +02:00
|
|
|
|
|
|
|
$self->log(LOGDEBUG,
|
|
|
|
"max_size: $self->{max_size} / size: $self->{data_size}");
|
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
my $header = $self->transaction->header;
|
|
|
|
if (!$header) {
|
|
|
|
$header = Mail::Header->new(Modify => 0, MailFrom => "COERCE");
|
|
|
|
$self->transaction->header($header);
|
|
|
|
}
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2007-04-09 16:45:11 +02:00
|
|
|
my $smtp = $self->connection->hello eq "ehlo" ? "ESMTP" : "SMTP";
|
2013-04-21 06:08:43 +02:00
|
|
|
my $esmtp = substr($smtp, 0, 1) eq "E";
|
2007-04-09 16:45:11 +02:00
|
|
|
my $authheader;
|
|
|
|
my $sslheader;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2007-04-09 16:45:11 +02:00
|
|
|
if (defined $self->connection->notes('tls_enabled')
|
2013-04-21 06:08:43 +02:00
|
|
|
and $self->connection->notes('tls_enabled'))
|
2007-04-09 16:45:11 +02:00
|
|
|
{
|
2013-04-21 06:08:43 +02:00
|
|
|
$smtp .= "S" if $esmtp; # RFC3848
|
|
|
|
$sslheader = "("
|
|
|
|
. $self->connection->notes('tls_socket')->get_cipher()
|
|
|
|
. " encrypted) ";
|
2007-04-09 16:45:11 +02:00
|
|
|
}
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2007-04-09 16:45:11 +02:00
|
|
|
if (defined $self->{_auth} and $self->{_auth} == OK) {
|
2013-04-21 06:08:43 +02:00
|
|
|
$smtp .= "A" if $esmtp; # RFC3848
|
|
|
|
$authheader =
|
|
|
|
"(smtp-auth username $self->{_auth_user}, mechanism $self->{_auth_mechanism})\n";
|
2006-12-08 20:46:18 +01:00
|
|
|
}
|
2013-04-21 06:08:43 +02:00
|
|
|
|
|
|
|
$header->add("Received",
|
|
|
|
$self->received_line($smtp, $authheader, $sslheader), 0);
|
|
|
|
|
|
|
|
return $self->respond(552, "Message too big!")
|
|
|
|
if $self->{max_size} and $self->{data_size} > $self->{max_size};
|
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
my ($rc, $msg) = $self->run_hooks("data_post");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|
|
|
|
|