File: check_earlytalker

package info (click to toggle)
qpsmtpd 0.84-9
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,376 kB
  • sloc: perl: 8,012; sh: 382; makefile: 61
file content (213 lines) | stat: -rw-r--r-- 6,930 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
=head1 NAME

check_earlytalker - Check that the client doesn't talk before we send the SMTP banner

=head1 DESCRIPTION

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.

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.

=head1 CONFIGURATION

=over 4

=item wait [integer]

The number of seconds to delay the initial greeting to see if the connecting
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.

=item action [string: deny, denysoft, log]

What to do when matching an early-talker -- the options are I<deny>,
I<denysoft> or I<log>.

If I<log> is specified, the connection will be allowed to proceed as normal,
and only a warning will be logged.

The default is I<denysoft>.

=item defer-reject [boolean]

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
issued a deny or denysoft (depending on the value of I<action>).  The default
is to react at the SMTP greeting stage by issuing the apropriate response code
and terminating the SMTP connection.

=item check-at [ CONNECT | DATA ]

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.

=back

=cut

use IO::Select;

use warnings;
use strict;

sub register {
  my ($self, $qp, @args) = @_;

  if (@args % 2) {
        $self->log(LOGERROR, "Unrecognized/mismatched arguments");
        return undef;
  }
  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;
  }
  $self->{_args} = {
        'wait' => 1,
        'action' => 'denysoft',
        'defer-reject' => 0,
        @args,
        'check-at' => \%check_at,
  };
  if ( $qp->{conn} && $qp->{conn}->isa('Apache2::Connection')) {
      require APR::Const;
      APR::Const->import(qw(POLLIN SUCCESS));
      $self->register_hook('connect', 'apr_connect_handler');
      $self->register_hook('data', 'apr_data_handler');
  }
  else {
      $self->register_hook('connect', 'connect_handler');
      $self->register_hook('data', 'data_handler');
  }
  $self->register_hook('mail', 'mail_handler')
    if $self->{_args}->{'defer-reject'};
  1;
}

sub apr_connect_handler {
    my ($self, $transaction) = @_;

    return DECLINED unless $self->{_args}{'check-at'}{CONNECT};
    return DECLINED if ($self->qp->connection->notes('whitelisthost'));
    my $ip = $self->qp->connection->remote_ip;

    my $c = $self->qp->{conn};
    my $socket = $c->client_socket;
    my $timeout = $self->{_args}->{'wait'} * 1_000_000;

    my $rc = $socket->poll($c->pool, $timeout, APR::Const::POLLIN());
    if ($rc == APR::Const::SUCCESS()) {
        $self->log(LOGNOTICE, "remote host started talking before we said hello [$ip]");
        if ($self->{_args}->{'defer-reject'}) {
            $self->qp->connection->notes('earlytalker', 1);
        }
        else {
            my $msg = 'Connecting host started transmitting before SMTP greeting';
            return (DENY,$msg) if $self->{_args}->{'action'} eq 'deny';
            return (DENYSOFT,$msg) if $self->{_args}->{'action'} eq 'denysoft';
        }
    }
    else {
        $self->log(LOGINFO, "remote host said nothing spontaneous, proceeding");
    }
}

sub apr_data_handler {
    my ($self, $transaction) = @_;

    return DECLINED unless $self->{_args}{'check-at'}{DATA};
    return DECLINED if ($self->qp->connection->notes('whitelisthost'));
    my $ip = $self->qp->connection->remote_ip;

    my $c = $self->qp->{conn};
    my $socket = $c->client_socket;
    my $timeout = $self->{_args}->{'wait'} * 1_000_000;

    my $rc = $socket->poll($c->pool, $timeout, APR::Const::POLLIN());
    if ($rc == APR::Const::SUCCESS()) {
        $self->log(LOGNOTICE, "remote host started talking before we said hello [$ip]");
        my $msg = 'Connecting host started transmitting before SMTP greeting';
        return (DENY,$msg) if $self->{_args}->{'action'} eq 'deny';
        return (DENYSOFT,$msg) if $self->{_args}->{'action'} eq 'denysoft';
    }
    else {
        $self->log(LOGINFO, "remote host said nothing spontaneous, proceeding");
    }
}

sub connect_handler {
  my ($self, $transaction) = @_;
  my $in = new IO::Select;
  my $ip = $self->qp->connection->remote_ip;

  return DECLINED unless $self->{_args}{'check-at'}{CONNECT};
  return DECLINED
      if ($self->qp->connection->notes('whitelisthost'));

  $in->add(\*STDIN) || return DECLINED;
  if ($in->can_read($self->{_args}->{'wait'})) {
    $self->log(LOGNOTICE, "remote host started talking before we said hello [$ip]");
    if ($self->{_args}->{'defer-reject'}) {
        $self->qp->connection->notes('earlytalker', 1);
    } else {
      my $msg = 'Connecting host started transmitting before SMTP greeting';
      return (DENY,$msg) if $self->{_args}->{'action'} eq 'deny';
      return (DENYSOFT,$msg) if $self->{_args}->{'action'} eq 'denysoft';
    }
  } else {
    $self->log(LOGINFO, 'remote host said nothing spontaneous, proceeding');
  }
  return DECLINED;
}

sub data_handler {
  my ($self, $transaction) = @_;
  my $in = new IO::Select;
  my $ip = $self->qp->connection->remote_ip;

  return DECLINED unless $self->{_args}{'check-at'}{DATA};
  return DECLINED
      if ($self->qp->connection->notes('whitelisthost'));

  $in->add(\*STDIN) || return DECLINED;
  if ($in->can_read($self->{_args}->{'wait'})) {
    $self->log(LOGNOTICE, "remote host started talking before we said hello [$ip]");
    my $msg = 'Connecting host started transmitting before SMTP greeting';
    return (DENY,$msg) if $self->{_args}->{'action'} eq 'deny';
    return (DENYSOFT,$msg) if $self->{_args}->{'action'} eq 'denysoft';
  }
  else {
    $self->log(LOGINFO, 'remote host said nothing spontaneous, proceeding');
  }
  return DECLINED;
}

sub mail_handler {
  my ($self, $transaction) = @_;
  my $msg = 'Connecting host started transmitting before SMTP greeting';

  return DECLINED unless $self->qp->connection->notes('earlytalker');
  return (DENY,$msg) if $self->{_args}->{'action'} eq 'deny';
  return (DENYSOFT,$msg) if $self->{_args}->{'action'} eq 'denysoft';
  return DECLINED;
}


1;