| 1 |
#!perl -w |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
earlytalker - Check that the client doesn't talk before we send the SMTP banner |
| 6 |
|
| 7 |
=head1 DESCRIPTION |
| 8 |
|
| 9 |
Checks to see if the remote host starts talking before we've issued a 2xx |
| 10 |
greeting. If so, we're likely looking at a direct-to-MX spam agent which |
| 11 |
pipelines its entire SMTP conversation, and will happily dump an entire spam |
| 12 |
into our mail log even if later tests deny acceptance. |
| 13 |
|
| 14 |
Depending on configuration, clients which behave in this way are either |
| 15 |
immediately disconnected with a deny or denysoft code, or else are issued this |
| 16 |
on all mail/rcpt commands in the transaction. |
| 17 |
|
| 18 |
=head1 CONFIGURATION |
| 19 |
|
| 20 |
=head2 wait [integer] |
| 21 |
|
| 22 |
The number of seconds to delay the initial greeting to see if the connecting |
| 23 |
host speaks first. The default is 1. Do not select a value that is too high, |
| 24 |
or you may be unable to receive mail from MTAs with short SMTP connect or |
| 25 |
greeting timeouts -- these are known to range as low as 30 seconds, and may |
| 26 |
in some cases be configured lower by mailserver admins. Network transit time |
| 27 |
must also be allowed for. |
| 28 |
|
| 29 |
=head2 reject <boolean> |
| 30 |
|
| 31 |
Do we reject/deny connections to early talkers? |
| 32 |
|
| 33 |
earlytalker reject [ 0 | 1 ] |
| 34 |
|
| 35 |
Default: I<reject 1> |
| 36 |
|
| 37 |
=head2 reject_type [ temp | perm ] |
| 38 |
|
| 39 |
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. |
| 40 |
|
| 41 |
Default I<reject_type temp>. |
| 42 |
|
| 43 |
=head2 defer-reject [boolean] |
| 44 |
|
| 45 |
When an early-talker is detected, if this option is set to a true value, the |
| 46 |
SMTP greeting will be issued as usual, but all RCPT/MAIL commands will be |
| 47 |
issued a deny or denysoft (depending on the value of I<reject_type>). The default |
| 48 |
is to react at the SMTP greeting stage by issuing the apropriate response code |
| 49 |
and terminating the SMTP connection. |
| 50 |
|
| 51 |
earlytalker defer-reject [ 0 | 1 ] |
| 52 |
|
| 53 |
=head2 check-at [ CONNECT | DATA ] |
| 54 |
|
| 55 |
Specifies when to check for early talkers. You can specify this option |
| 56 |
multiple times to check more than once. |
| 57 |
|
| 58 |
The default is I<check-at CONNECT> only. |
| 59 |
|
| 60 |
=head2 loglevel |
| 61 |
|
| 62 |
Adjust the quantity of logging for this plugin. See docs/logging.pod |
| 63 |
|
| 64 |
=cut |
| 65 |
|
| 66 |
use strict; |
| 67 |
use warnings; |
| 68 |
|
| 69 |
use IO::Select; |
| 70 |
use Qpsmtpd::Constants; |
| 71 |
|
| 72 |
sub register { |
| 73 |
my ($self, $qp, @args) = @_; |
| 74 |
|
| 75 |
if (@args % 2) { |
| 76 |
$self->log(LOGERROR, "Unrecognized/mismatched arguments"); |
| 77 |
return; |
| 78 |
} |
| 79 |
my %check_at; |
| 80 |
for (0 .. $#args) { |
| 81 |
next if $_ % 2; |
| 82 |
if (lc($args[$_]) eq 'check-at') { |
| 83 |
my $val = $args[$_ + 1]; |
| 84 |
$check_at{uc($val)}++; |
| 85 |
} |
| 86 |
} |
| 87 |
if (!%check_at) { |
| 88 |
$check_at{CONNECT} = 1; |
| 89 |
} |
| 90 |
$self->{_args} = { |
| 91 |
'wait' => 1, |
| 92 |
@args, |
| 93 |
'check-at' => \%check_at, |
| 94 |
}; |
| 95 |
|
| 96 |
# backwards compat with old 'action' argument |
| 97 |
if (defined $self->{_args}{action} && !defined $self->{_args}{reject}) { |
| 98 |
$self->{_args}{reject} = $self->{_args}{action} =~ /^deny/i ? 1 : 0; |
| 99 |
} |
| 100 |
if (defined $self->{_args}{'defer-reject'} |
| 101 |
&& !defined $self->{_args}{reject_type}) |
| 102 |
{ |
| 103 |
$self->{_args}{reject_type} = |
| 104 |
$self->{_args}{action} == 'denysoft' ? 'temp' : 'perm'; |
| 105 |
} |
| 106 |
if (!defined $self->{_args}{reject_type}) { |
| 107 |
$self->{_args}{reject_type} = 'perm'; |
| 108 |
} |
| 109 |
|
| 110 |
# /end compat |
| 111 |
if ($qp->{conn} && $qp->{conn}->isa('Apache2::Connection')) { |
| 112 |
require APR::Const; |
| 113 |
APR::Const->import(qw(POLLIN SUCCESS)); |
| 114 |
$self->register_hook('connect', 'apr_connect_handler'); |
| 115 |
$self->register_hook('data', 'apr_data_handler'); |
| 116 |
} |
| 117 |
else { |
| 118 |
$self->register_hook('connect', 'connect_handler'); |
| 119 |
$self->register_hook('data', 'data_handler'); |
| 120 |
} |
| 121 |
$self->register_hook('mail', 'mail_handler') |
| 122 |
if $self->{_args}{'defer-reject'}; |
| 123 |
$self->{_args}{reject} = 1 if !defined $self->{_args}{reject}; |
| 124 |
} |
| 125 |
|
| 126 |
sub apr_connect_handler { |
| 127 |
my ($self, $transaction) = @_; |
| 128 |
|
| 129 |
return DECLINED unless $self->{_args}{'check-at'}{CONNECT}; |
| 130 |
return DECLINED if $self->is_immune(); |
| 131 |
|
| 132 |
my $c = $self->qp->{conn} or return DECLINED; |
| 133 |
my $socket = $c->client_socket or return DECLINED; |
| 134 |
my $timeout = $self->{_args}{'wait'} * 1_000_000; |
| 135 |
|
| 136 |
my $rc = $socket->poll($c->pool, $timeout, APR::Const::POLLIN()); |
| 137 |
if ($rc == APR::Const::SUCCESS()) { |
| 138 |
if ($self->{_args}{'defer-reject'}) { |
| 139 |
$self->connection->notes('earlytalker', 1); |
| 140 |
return DECLINED; |
| 141 |
} |
| 142 |
return $self->log_and_deny(); |
| 143 |
} |
| 144 |
return $self->log_and_pass(); |
| 145 |
} |
| 146 |
|
| 147 |
sub apr_data_handler { |
| 148 |
my ($self, $transaction) = @_; |
| 149 |
|
| 150 |
return DECLINED unless $self->{_args}{'check-at'}{DATA}; |
| 151 |
return DECLINED if $self->is_immune(); |
| 152 |
|
| 153 |
my $c = $self->qp->{conn} or return DECLINED; |
| 154 |
my $socket = $c->client_socket or return DECLINED; |
| 155 |
my $timeout = $self->{_args}{'wait'} * 1_000_000; |
| 156 |
|
| 157 |
my $rc = $socket->poll($c->pool, $timeout, APR::Const::POLLIN()); |
| 158 |
if ($rc == APR::Const::SUCCESS()) { |
| 159 |
return $self->log_and_deny(); |
| 160 |
} |
| 161 |
return $self->log_and_pass(); |
| 162 |
} |
| 163 |
|
| 164 |
sub connect_handler { |
| 165 |
my ($self, $transaction) = @_; |
| 166 |
my $in = new IO::Select; |
| 167 |
|
| 168 |
return DECLINED unless $self->{_args}{'check-at'}{CONNECT}; |
| 169 |
return DECLINED if $self->is_immune(); |
| 170 |
|
| 171 |
# senders with good karma skip the delay |
| 172 |
my $karma = $self->connection->notes('karma_history'); |
| 173 |
if (defined $karma && $karma > 5) { |
| 174 |
$self->log(LOGINFO, "skip, karma $karma"); |
| 175 |
return DECLINED; |
| 176 |
} |
| 177 |
|
| 178 |
$in->add(\*STDIN) or return DECLINED; |
| 179 |
if (!$in->can_read($self->{_args}{'wait'})) { |
| 180 |
return $self->log_and_pass(); |
| 181 |
} |
| 182 |
|
| 183 |
if (!$self->{_args}{'defer-reject'}) { |
| 184 |
return $self->log_and_deny(); |
| 185 |
} |
| 186 |
|
| 187 |
$self->connection->notes('earlytalker', 1); |
| 188 |
$self->adjust_karma(-1); |
| 189 |
return DECLINED; |
| 190 |
} |
| 191 |
|
| 192 |
sub data_handler { |
| 193 |
my ($self, $transaction) = @_; |
| 194 |
my $in = new IO::Select; |
| 195 |
|
| 196 |
return DECLINED unless $self->{_args}{'check-at'}{DATA}; |
| 197 |
return DECLINED if $self->is_immune(); |
| 198 |
|
| 199 |
$in->add(\*STDIN) or return DECLINED; |
| 200 |
if (!$in->can_read($self->{_args}{'wait'})) { |
| 201 |
return $self->log_and_pass(); |
| 202 |
} |
| 203 |
|
| 204 |
return $self->log_and_deny(); |
| 205 |
} |
| 206 |
|
| 207 |
sub log_and_pass { |
| 208 |
my $self = shift; |
| 209 |
my $ip = $self->qp->connection->remote_ip || 'remote host'; |
| 210 |
$self->log(LOGINFO, "pass, not spontaneous"); |
| 211 |
return DECLINED; |
| 212 |
} |
| 213 |
|
| 214 |
sub log_and_deny { |
| 215 |
my $self = shift; |
| 216 |
|
| 217 |
my $ip = $self->qp->connection->remote_ip || 'remote host'; |
| 218 |
|
| 219 |
$self->connection->notes('earlytalker', 1); |
| 220 |
$self->adjust_karma(-1); |
| 221 |
|
| 222 |
my $log_mess = "remote started talking before we said hello"; |
| 223 |
my $smtp_msg = 'Connecting host started transmitting before SMTP greeting'; |
| 224 |
|
| 225 |
return $self->get_reject($smtp_msg, $log_mess); |
| 226 |
} |
| 227 |
|
| 228 |
sub mail_handler { |
| 229 |
my ($self, $transaction) = @_; |
| 230 |
|
| 231 |
return DECLINED unless $self->connection->notes('earlytalker'); |
| 232 |
return $self->log_and_deny(); |
| 233 |
} |
| 234 |
|