Debian Codegraph @

plugins/async/earlytalker

raw
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 =over 4
21
22 =item wait [integer]
23
24 The number of seconds to delay the initial greeting to see if the connecting
25 host speaks first. The default is 1. Do not select a value that is too high,
26 or you may be unable to receive mail from MTAs with short SMTP connect or
27 greeting timeouts -- these are known to range as low as 30 seconds, and may
28 in some cases be configured lower by mailserver admins. Network transit time
29 must also be allowed for.
30
31 =item action [string: deny, denysoft, log]
32
33 What to do when matching an early-talker -- the options are I<deny>,
34 I<denysoft> or I<log>.
35
36 If I<log> is specified, the connection will be allowed to proceed as normal,
37 and only a warning will be logged.
38
39 The default is I<denysoft>.
40
41 =item defer-reject [boolean]
42
43 When an early-talker is detected, if this option is set to a true value, the
44 SMTP greeting will be issued as usual, but all RCPT/MAIL commands will be
45 issued a deny or denysoft (depending on the value of I<action>). The default
46 is to react at the SMTP greeting stage by issuing the apropriate response code
47 and terminating the SMTP connection.
48
49 =item check-at [string: connect, data]
50
51 Defines when to check for early talkers, either at connect time (pre-greet pause)
52 or at DATA time (pause before sending "354 go ahead").
53
54 The default is I<connect>.
55
56 Note that defer-reject has no meaning if check-at is I<data>.
57
58 =back
59
60 =cut
61
62 my $MSG = 'Connecting host started transmitting before SMTP greeting';
63
64 sub register {
65 my ($self, $qp, @args) = @_;
66
67 if (@args % 2) {
68 $self->log(LOGERROR, "Unrecognized/mismatched arguments");
69 return undef;
70 }
71 $self->{_args} = {
72 'wait' => 1,
73 'action' => 'denysoft',
74 'defer-reject' => 0,
75 'check-at' => 'connect',
76 @args,
77 };
78 print STDERR "Check at: ", $self->{_args}{'check-at'}, "\n";
79 $self->register_hook($self->{_args}->{'check-at'}, 'check_talker_poll');
80 $self->register_hook($self->{_args}->{'check-at'}, 'check_talker_post');
81 if ($self->{_args}{'check-at'} eq 'connect') {
82 $self->register_hook('mail', 'hook_mail')
83 if $self->{_args}->{'defer-reject'};
84 }
85 1;
86 }
87
88 sub check_talker_poll {
89 my ($self, $transaction) = @_;
90
91 my $qp = $self->qp;
92 my $conn = $qp->connection;
93 my $check_until = time + $self->{_args}{'wait'};
94 $qp->AddTimer(
95 1,
96 sub {
97 read_now($qp, $conn, $check_until, $self->{_args}{'check-at'});
98 }
99 );
100 return YIELD;
101 }
102
103 sub read_now {
104 my ($qp, $conn, $until, $phase) = @_;
105
106 if ($qp->has_data) {
107 $qp->log(LOGNOTICE,
108 'remote host started talking after $phase before we responded');
109 $qp->clear_data if $phase eq 'data';
110 $conn->notes('earlytalker', 1);
111 $qp->run_continuation;
112 }
113 elsif (time >= $until) {
114
115 # no early talking
116 $qp->run_continuation;
117 }
118 else {
119 $qp->AddTimer(1, sub { read_now($qp, $conn, $until, $phase) });
120 }
121 }
122
123 sub check_talker_post {
124 my ($self, $transaction) = @_;
125
126 return DECLINED unless $self->connection->notes('earlytalker');
127 return DECLINED if $self->{'defer-reject'};
128 return (DENY, $MSG) if $self->{_args}->{'action'} eq 'deny';
129 return (DENYSOFT, $MSG) if $self->{_args}->{'action'} eq 'denysoft';
130 return DECLINED; # assume action eq 'log'
131 }
132
133 sub hook_mail {
134 my ($self, $transaction) = @_;
135
136 return DECLINED unless $self->connection->notes('earlytalker');
137 return (DENY, $MSG) if $self->{_args}->{'action'} eq 'deny';
138 return (DENYSOFT, $MSG) if $self->{_args}->{'action'} eq 'denysoft';
139 return DECLINED;
140 }
141