-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exim4.pm
245 lines (215 loc) · 5.9 KB
/
Exim4.pm
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# ---- class Reporter::Exim4 ----
# Exim4 log reporter
package DDgrey::Reporter::Exim4;
use strict;
use integer;
use Data::Dumper; # DEBUG
use Date::Manip;
use Digest::MD5 qw(md5_hex);
use DDgrey::Perl6::Parameters;
use parent qw(DDgrey::TailReporter);
# my $ip_re='[\d[a-f]\.\:]+'; # when IPV6 is supported
my $ip_re='[\d\.]+';
my $host_re='(?:\S+\s+\(\S+\)|\S+)\s+\[('.$ip_re.')\]';
# ---- constructor ----
sub new($class){
# return: new reporter of class from config
# effect: registers with select
# starts subprocess
# may raise exception
my $file=($main::config->{exim4_mainlog} // '/var/log/exim4/mainlog');
my $self=$class->SUPER::new($file);
$self->{current}={};
main::lm("following exim4 mainlog $file","exim4");
return $self;
};
# ---- methods -----
sub service($self){
# return: name of subsystem (for logging)
return "exim4";
};
sub receive_line($self,$line){
# effect: receives line, possibly makes report
chomp $line;
# configuration
my $unknown = $main::config->{exim4_unknown} // 'verify';
# extract date
$line=~s/^(\d+\-\d+\-\d+ \d+\:\d+\:\d+)\s*//;
my $date=$1 ? ParseDate($1) : undef;
if(!$date){
main::lm("unknown date in exim4 log ($line)","exim4","warning");
return;
};
my $time=UnixDate($date,"%s");
# unexcepted disconnect from MTA
if($line=~/^unexpected disconnection while reading SMTP command from $host_re \(error: Connection reset by peer\)/){
my $report=DDgrey::Report->new({
event=>'disconnect',
ip=>$1,
time=>$time,
reporter=>"exim4"
});
$self->report($report);
return;
};
# no MAIL command received
if($line=~/^no MAIL in SMTP connection from $host_re D=/){
my $report=DDgrey::Report->new({
event=>'smtp_no_mail',
ip=>$1,
time=>$time,
reporter=>"exim4"
});
$self->report($report);
return;
};
# no waitng from greeting
if($line=~/^SMTP protocol synchronization error \(input sent without waiting for greeting\)\: rejected connection from H=$host_re/){
my $report=DDgrey::Report->new({
event=>'smtp_no_wait',
ip=>$1,
time=>$time,
reporter=>"exim4"
});
$self->report($report);
return;
};
# lost connection in middle of transaction
if($line=~/^H=$host_re incomplete transaction \(connection lost\)/){
my $report=DDgrey::Report->new({
event=>'smtp_connection_lost',
ip=>$1,
time=>$time,
reporter=>"exim4"
});
$self->report($report);
return;
};
# unknown recipient (from special early logging clauses in ACL)
if($line=~/^H=$host_re Warning: unknown recipient (\S+) from (\S+)/){
if($unknown eq 'verify'){
my $report=DDgrey::Report->new({
event=>'unknown',
ip=>$1,
e_from=>$3,
e_to=>$2,
time=>$time,
reporter=>"exim4"
});
$self->report($report);
};
return;
};
# spam
if($line=~/^(\S+) F=(\S+) H=$host_re P=esmtp rejected by local_scan\(\)\: Rejected\b/){
my $report=DDgrey::Report->new({
event=>'spam',
mta_id=>$1,
e_from=>$2,
ip=>$3,
time=>$time,
reporter=>"exim4"
});
$self->report($report);
return;
};
# relay
if($line=~/^H=$host_re F=<(\S+)> rejected RCPT <(\S+)>\: relay not permitted\b/){
my $report=DDgrey::Report->new({
event=>'relay',
ip=>$1,
e_from=>$2,
e_to=>$3,
time=>$time,
reporter=>"exim4"
});
$self->report($report);
return;
};
# unknown recipient
if($line=~/^H=$host_re F=<(\S+)> rejected RCPT <(\S+)>\: Unrouteable address\b/){
if($unknown eq 'delivery'){
my $report=DDgrey::Report->new({
event=>'unknown',
ip=>$1,
e_from=>$2,
e_to=>$3,
time=>$time,
reporter=>"exim4"
});
$self->report($report);
};
return;
};
# external message accepted
if($line=~/^(\S+) \<\= (\S+) H=$host_re/){
$self->{current}->{$1}={
exim_id=>$1,
from=>$2,
ip=>$3
};
return;
};
if($line=~/^(\S+) \<\= (\S+) U=\S+ P=local\b/){
$self->{current}->{$1}={
exim_id=>$1,
from=>$2,
};
return;
};
if($line=~/^(\S+)\s+(.*)$/ and defined($self->{current}->{$1})){
my $exim_id=$1;
my $rest=$2;
# to-lines
if($rest=~/^\=\> (\S+?\s+)?\<(\S+)\>/){
$self->{current}->{$exim_id}->{to}->{$2}=1;
return;
};
if($rest=~/^\=\> (\S+)/){
$self->{current}->{$exim_id}->{to}->{$1}=1;
return;
};
# message done, make report
if($rest=~/^Completed\b/){
# skip local IP:s
if(!defined($self->{current}->{$exim_id}->{ip})){
return;
};
for my $to (keys %{$self->{current}->{$exim_id}->{to}}){
my $report=DDgrey::Report->new({
event=>'ok',
mta_id=>$exim_id,
e_from=>$self->{current}->{$exim_id}->{from},
e_to=>$to,
ip=>$self->{current}->{$exim_id}->{ip},
time=>$time,
reporter=>"exim4"
});
$self->report($report);
};
delete $self->{current}->{$exim_id};
return;
};
};
# other to skip, for debug
# $line=~/^Start queue run:/ and return;
# $line=~/^End queue run:/ and return;
# $line=~/^\S+ Message is frozen\b/ and return;
# $line=~/^\S+ Unfrozen by errmsg timer\b/ and return;
# $line=~/^unexpected disconnection while reading SMTP command\b/ and return;
# $line=~/^SMTP command timeout on connection\b/ and return;
# $line=~/^\S+ SA\:/ and return;
# $line=~/^\S+ DKIM\:/ and return;
# $line=~/^no IP address found for host\b/ and return;
# $line=~/^no host name found for IP address\b/ and return;
# $line=~/\S+ $host_re Network is unreachable/ and return;
# $line=~/H=$host_re F=<\S+> temporarily rejected RCPT <\S+>\: greylisted\b/ and return;
# warn "$line\n";
};
sub report($self,$report){
# effekt: send report to dispatcher
$main::debug > 1 and main::lm("sending report ".$report->unicode(),"exim4");
$main::dispatcher->report($report);
};
# ---- package init ----
return 1;