2012-04-29 10:35:59 +02:00
|
|
|
#!perl -w
|
2012-05-21 23:13:16 +02:00
|
|
|
|
2003-04-15 19:39:27 +02:00
|
|
|
=head1 NAME
|
|
|
|
|
2012-06-28 01:17:01 +02:00
|
|
|
earlytalker - Check that the client doesn't talk before we send the SMTP banner
|
2003-04-15 19:39:27 +02:00
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
2004-08-01 09:08:07 +02:00
|
|
|
Checks to see if the remote host starts talking before we've issued a 2xx
|
|
|
|
greeting. If so, we're likely looking at a direct-to-MX spam agent which
|
|
|
|
pipelines its entire SMTP conversation, and will happily dump an entire spam
|
|
|
|
into our mail log even if later tests deny acceptance.
|
2003-04-15 19:39:27 +02:00
|
|
|
|
2004-08-01 09:08:07 +02:00
|
|
|
Depending on configuration, clients which behave in this way are either
|
|
|
|
immediately disconnected with a deny or denysoft code, or else are issued this
|
|
|
|
on all mail/rcpt commands in the transaction.
|
2003-04-15 19:39:27 +02:00
|
|
|
|
2004-08-01 09:08:07 +02:00
|
|
|
=head1 CONFIGURATION
|
2003-04-15 19:39:27 +02:00
|
|
|
|
2012-05-21 23:13:16 +02:00
|
|
|
=head2 wait [integer]
|
2004-08-01 09:08:07 +02:00
|
|
|
|
|
|
|
The number of seconds to delay the initial greeting to see if the connecting
|
2005-07-30 09:22:13 +02:00
|
|
|
host speaks first. The default is 1. Do not select a value that is too high,
|
|
|
|
or you may be unable to receive mail from MTAs with short SMTP connect or
|
|
|
|
greeting timeouts -- these are known to range as low as 30 seconds, and may
|
|
|
|
in some cases be configured lower by mailserver admins. Network transit time
|
|
|
|
must also be allowed for.
|
2004-08-01 09:08:07 +02:00
|
|
|
|
2012-05-21 23:13:16 +02:00
|
|
|
=head2 reject <boolean>
|
|
|
|
|
|
|
|
Do we reject/deny connections to early talkers?
|
|
|
|
|
2012-06-28 01:17:01 +02:00
|
|
|
earlytalker reject [ 0 | 1 ]
|
2012-05-21 23:13:16 +02:00
|
|
|
|
|
|
|
Default: I<reject 1>
|
2004-08-01 09:08:07 +02:00
|
|
|
|
2012-05-21 23:13:16 +02:00
|
|
|
=head2 reject_type [ temp | perm ]
|
2004-08-01 09:08:07 +02:00
|
|
|
|
2012-05-21 23:13:16 +02:00
|
|
|
What type of rejection to send. A temporary rejection tells the remote server to try again later. A permanent error tells it to give up permanently.
|
2004-08-01 09:08:07 +02:00
|
|
|
|
2012-05-21 23:13:16 +02:00
|
|
|
Default I<reject_type temp>.
|
2004-08-01 09:08:07 +02:00
|
|
|
|
2012-05-21 23:13:16 +02:00
|
|
|
=head2 defer-reject [boolean]
|
2004-08-01 09:08:07 +02:00
|
|
|
|
|
|
|
When an early-talker is detected, if this option is set to a true value, the
|
|
|
|
SMTP greeting will be issued as usual, but all RCPT/MAIL commands will be
|
2012-05-21 23:13:16 +02:00
|
|
|
issued a deny or denysoft (depending on the value of I<reject_type>). The default
|
2004-08-01 09:08:07 +02:00
|
|
|
is to react at the SMTP greeting stage by issuing the apropriate response code
|
|
|
|
and terminating the SMTP connection.
|
|
|
|
|
2012-06-28 01:17:01 +02:00
|
|
|
earlytalker defer-reject [ 0 | 1 ]
|
2012-05-21 23:13:16 +02:00
|
|
|
|
|
|
|
=head2 check-at [ CONNECT | DATA ]
|
2007-07-12 19:14:36 +02:00
|
|
|
|
|
|
|
Specifies when to check for early talkers. You can specify this option
|
|
|
|
multiple times to check more than once.
|
|
|
|
|
|
|
|
The default is I<check-at CONNECT> only.
|
|
|
|
|
2012-05-21 23:13:16 +02:00
|
|
|
=head2 loglevel
|
|
|
|
|
|
|
|
Adjust the quantity of logging for this plugin. See docs/logging.pod
|
2003-04-15 19:39:27 +02:00
|
|
|
|
|
|
|
=cut
|
|
|
|
|
2004-08-01 09:08:07 +02:00
|
|
|
use strict;
|
2012-04-08 02:11:16 +02:00
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use IO::Select;
|
|
|
|
use Qpsmtpd::Constants;
|
2004-08-01 09:08:07 +02:00
|
|
|
|
2003-04-15 19:39:27 +02:00
|
|
|
sub register {
|
2004-08-01 09:08:07 +02:00
|
|
|
my ($self, $qp, @args) = @_;
|
|
|
|
|
|
|
|
if (@args % 2) {
|
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
|
|
|
$self->log(LOGERROR, "Unrecognized/mismatched arguments");
|
2012-05-21 23:13:16 +02:00
|
|
|
return;
|
2004-08-01 09:08:07 +02:00
|
|
|
}
|
2007-07-12 19:14:36 +02:00
|
|
|
my %check_at;
|
|
|
|
for (0..$#args) {
|
|
|
|
next if $_ % 2;
|
|
|
|
if (lc($args[$_]) eq 'check-at') {
|
|
|
|
my $val = $args[$_ + 1];
|
|
|
|
$check_at{uc($val)}++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!%check_at) {
|
|
|
|
$check_at{CONNECT} = 1;
|
|
|
|
}
|
2004-08-01 09:08:07 +02:00
|
|
|
$self->{_args} = {
|
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
|
|
|
'wait' => 1,
|
|
|
|
@args,
|
2007-07-12 19:14:36 +02:00
|
|
|
'check-at' => \%check_at,
|
2004-08-01 09:08:07 +02:00
|
|
|
};
|
2012-05-21 23:13:16 +02:00
|
|
|
# backwards compat with old 'action' argument
|
|
|
|
if ( defined $self->{_args}{action} && ! defined $self->{_args}{reject} ) {
|
|
|
|
$self->{_args}{reject} = $self->{_args}{action} =~ /^deny/i ? 1 : 0;
|
|
|
|
};
|
|
|
|
if ( defined $self->{_args}{'defer-reject'} && ! defined $self->{_args}{reject_type} ) {
|
|
|
|
$self->{_args}{reject_type} = $self->{_args}{action} == 'denysoft' ? 'temp' : 'perm';
|
|
|
|
};
|
2012-06-04 03:28:54 +02:00
|
|
|
if ( ! defined $self->{_args}{reject_type} ) {
|
|
|
|
$self->{_args}{reject_type} = 'perm';
|
|
|
|
};
|
2012-05-21 23:13:16 +02:00
|
|
|
# /end compat
|
2007-02-07 23:25:44 +01:00
|
|
|
if ( $qp->{conn} && $qp->{conn}->isa('Apache2::Connection')) {
|
2005-06-20 20:46:38 +02:00
|
|
|
require APR::Const;
|
|
|
|
APR::Const->import(qw(POLLIN SUCCESS));
|
|
|
|
$self->register_hook('connect', 'apr_connect_handler');
|
2007-07-12 19:14:36 +02:00
|
|
|
$self->register_hook('data', 'apr_data_handler');
|
2005-06-20 20:46:38 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$self->register_hook('connect', 'connect_handler');
|
2007-07-12 19:14:36 +02:00
|
|
|
$self->register_hook('data', 'data_handler');
|
2005-06-20 20:46:38 +02:00
|
|
|
}
|
2004-08-01 09:08:07 +02:00
|
|
|
$self->register_hook('mail', 'mail_handler')
|
2012-05-21 23:13:16 +02:00
|
|
|
if $self->{_args}{'defer-reject'};
|
|
|
|
$self->{_args}{reject} = 1 if ! defined $self->{_args}{reject};
|
2003-04-15 19:39:27 +02:00
|
|
|
}
|
|
|
|
|
2005-06-20 20:46:38 +02:00
|
|
|
sub apr_connect_handler {
|
|
|
|
my ($self, $transaction) = @_;
|
2008-05-05 19:05:38 +02:00
|
|
|
|
2007-07-12 19:14:36 +02:00
|
|
|
return DECLINED unless $self->{_args}{'check-at'}{CONNECT};
|
2012-06-03 03:44:46 +02:00
|
|
|
return DECLINED if $self->is_immune();
|
2005-06-20 20:46:38 +02:00
|
|
|
|
2012-05-21 23:13:16 +02:00
|
|
|
my $c = $self->qp->{conn} or return DECLINED;
|
|
|
|
my $socket = $c->client_socket or return DECLINED;
|
|
|
|
my $timeout = $self->{_args}{'wait'} * 1_000_000;
|
2008-05-05 19:05:38 +02:00
|
|
|
|
2005-06-20 20:46:38 +02:00
|
|
|
my $rc = $socket->poll($c->pool, $timeout, APR::Const::POLLIN());
|
|
|
|
if ($rc == APR::Const::SUCCESS()) {
|
2012-05-21 23:13:16 +02:00
|
|
|
if ($self->{_args}{'defer-reject'}) {
|
2012-06-23 06:37:54 +02:00
|
|
|
$self->connection->notes('earlytalker', 1);
|
2012-05-21 23:13:16 +02:00
|
|
|
return DECLINED;
|
|
|
|
};
|
|
|
|
return $self->log_and_deny();
|
|
|
|
};
|
|
|
|
return $self->log_and_pass();
|
2005-06-20 20:46:38 +02:00
|
|
|
}
|
|
|
|
|
2007-07-12 19:14:36 +02:00
|
|
|
sub apr_data_handler {
|
|
|
|
my ($self, $transaction) = @_;
|
2008-05-05 19:05:38 +02:00
|
|
|
|
2007-07-12 19:14:36 +02:00
|
|
|
return DECLINED unless $self->{_args}{'check-at'}{DATA};
|
2012-06-03 03:44:46 +02:00
|
|
|
return DECLINED if $self->is_immune();
|
2007-07-12 19:14:36 +02:00
|
|
|
|
2012-05-21 23:13:16 +02:00
|
|
|
my $c = $self->qp->{conn} or return DECLINED;
|
|
|
|
my $socket = $c->client_socket or return DECLINED;
|
|
|
|
my $timeout = $self->{_args}{'wait'} * 1_000_000;
|
2008-05-05 19:05:38 +02:00
|
|
|
|
2007-07-12 19:14:36 +02:00
|
|
|
my $rc = $socket->poll($c->pool, $timeout, APR::Const::POLLIN());
|
|
|
|
if ($rc == APR::Const::SUCCESS()) {
|
2012-05-21 23:13:16 +02:00
|
|
|
return $self->log_and_deny();
|
|
|
|
};
|
|
|
|
return $self->log_and_pass();
|
2007-07-12 19:14:36 +02:00
|
|
|
}
|
|
|
|
|
2003-04-15 19:39:27 +02:00
|
|
|
sub connect_handler {
|
2012-05-21 23:13:16 +02:00
|
|
|
my ($self, $transaction) = @_;
|
|
|
|
my $in = new IO::Select;
|
|
|
|
|
|
|
|
return DECLINED unless $self->{_args}{'check-at'}{CONNECT};
|
2012-06-04 03:28:54 +02:00
|
|
|
return DECLINED if $self->is_immune();
|
2012-05-21 23:13:16 +02:00
|
|
|
|
|
|
|
$in->add(\*STDIN) or return DECLINED;
|
|
|
|
if (! $in->can_read($self->{_args}{'wait'})) {
|
|
|
|
return $self->log_and_pass();
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( ! $self->{_args}{'defer-reject'}) {
|
|
|
|
return $self->log_and_deny();
|
|
|
|
};
|
|
|
|
|
2012-06-23 06:37:54 +02:00
|
|
|
$self->connection->notes('earlytalker', 1);
|
2012-06-30 21:37:25 +02:00
|
|
|
$self->adjust_karma( -1 );
|
2012-05-21 23:13:16 +02:00
|
|
|
return DECLINED;
|
2003-04-15 19:39:27 +02:00
|
|
|
}
|
2004-08-01 09:08:07 +02:00
|
|
|
|
2007-07-12 19:14:36 +02:00
|
|
|
sub data_handler {
|
2012-05-21 23:13:16 +02:00
|
|
|
my ($self, $transaction) = @_;
|
|
|
|
my $in = new IO::Select;
|
|
|
|
|
|
|
|
return DECLINED unless $self->{_args}{'check-at'}{DATA};
|
2012-06-04 03:28:54 +02:00
|
|
|
return DECLINED if $self->is_immune();
|
2012-05-21 23:13:16 +02:00
|
|
|
|
|
|
|
$in->add(\*STDIN) or return DECLINED;
|
|
|
|
if ( ! $in->can_read($self->{_args}{'wait'})) {
|
|
|
|
return $self->log_and_pass();
|
|
|
|
};
|
2007-07-12 19:14:36 +02:00
|
|
|
|
2012-05-21 23:13:16 +02:00
|
|
|
return $self->log_and_deny();
|
|
|
|
};
|
2007-07-12 19:14:36 +02:00
|
|
|
|
2012-05-21 23:13:16 +02:00
|
|
|
sub log_and_pass {
|
|
|
|
my $self = shift;
|
|
|
|
my $ip = $self->qp->connection->remote_ip || 'remote host';
|
2012-06-23 06:37:54 +02:00
|
|
|
$self->log(LOGINFO, "pass, $ip said nothing spontaneous");
|
2012-05-21 23:13:16 +02:00
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub log_and_deny {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $ip = $self->qp->connection->remote_ip || 'remote host';
|
|
|
|
|
2012-06-23 06:37:54 +02:00
|
|
|
$self->connection->notes('earlytalker', 1);
|
2012-05-21 23:13:16 +02:00
|
|
|
|
2012-06-23 09:06:24 +02:00
|
|
|
my $log_mess = "$ip started talking before we said hello";
|
2012-06-23 06:37:54 +02:00
|
|
|
my $smtp_msg = 'Connecting host started transmitting before SMTP greeting';
|
|
|
|
|
|
|
|
return $self->get_reject( $smtp_msg, $log_mess );
|
2007-07-12 19:14:36 +02:00
|
|
|
}
|
|
|
|
|
2004-08-01 09:08:07 +02:00
|
|
|
sub mail_handler {
|
2008-05-05 19:05:38 +02:00
|
|
|
my ($self, $transaction) = @_;
|
2004-08-01 09:08:07 +02:00
|
|
|
|
2012-06-23 06:37:54 +02:00
|
|
|
return DECLINED unless $self->connection->notes('earlytalker');
|
2012-05-21 23:13:16 +02:00
|
|
|
return $self->log_and_deny();
|
2004-08-01 09:08:07 +02:00
|
|
|
}
|
|
|
|
|