Debian Codegraph @

log/summarize

raw
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Cwd;
7 use Data::Dumper;
8 use File::Tail;
9 use Getopt::Std;
10
11 $|++;
12 $Data::Dumper::Sortkeys = 1;
13
14 our $opt_l = 0;
15 getopts('l');
16
17 my (%plugins, %plugin_aliases, %seen_plugins, %pids);
18 my %hide_plugins = map { $_ => 1 } qw/ hostname /;
19
20 my $qpdir = get_qp_dir();
21 my $file = "$qpdir/log/main/current";
22 populate_plugins_from_registry();
23 my @sorted_plugins =
24 sort { $plugins{$a}{id} <=> $plugins{$b}{id} } keys %plugins;
25
26 my $fh = File::Tail->new(
27 name => $file,
28 interval => 1,
29 maxinterval => 1,
30 debug => 1,
31 tail => 1000
32 );
33 my $printed = 0;
34 my $has_cleanup;
35
36 my %formats = get_default_field_widths();
37 my %formats3 = ( %formats, map { $_ => "%-3.3s" } qw/ badrcptto check_badrcptto
38 qmail_deliverable rcpt_ok check_basicheaders headers uribl bogus_bounce
39 check_bogus_bounce domainkeys dkim dmarc spamassassin dspam
40 virus::clamdscan / );
41
42 while (defined(my $line = $fh->read)) {
43 chomp $line;
44 $line =~ s/[^[ -~]]//g; # strip out binary/unprintable
45 next if !$line;
46 my ($type, $pid, $hook, $plugin, $message) = parse_line($line);
47 next if !$type;
48 next if $type =~ /^(?:info|unknown|response|tcpserver)$/;
49 next if $type eq 'init'; # doesn't occur in all deployment models
50
51 if (!$pids{$pid}) { # haven't seen this pid
52 next if $type ne 'connect'; # ignore unless connect
53 my ($host, $ip) = split /\s/, $message;
54 $ip = substr $ip, 1, -1;
55 foreach (keys %seen_plugins, qw/ helo_host from to /) {
56 $pids{$pid}{$_} = ''; # define them
57 }
58 $pids{$pid}{ip} = $ip;
59 $pids{$pid}{hostname} = $host if $host ne 'Unknown';
60 }
61
62 if ($type eq 'close') {
63 next if $has_cleanup; # it'll get handled later
64 print_auto_format($pid, $line);
65 delete $pids{$pid};
66 }
67 elsif ($type eq 'cleanup') {
68 print_auto_format($pid, $line);
69 delete $pids{$pid};
70 }
71 elsif ($type eq 'plugin') {
72 handle_plugin($message,$plugin,$pid,$line);
73 }
74 elsif ($type eq 'reject') { }
75 elsif ($type eq 'connect') { }
76 elsif ($type eq 'dispatch') {
77 handle_dispatch($message,$pid,$line);
78 }
79 else {
80 print "$type $pid $hook $plugin $message\n";
81 }
82 }
83
84 sub get_default_field_widths {
85 my %widths = (
86 ip => "%-15.15s",
87 hostname => "%-20.20s",
88 'ident::geoip' => $opt_l ? "%-20.20s" : "%-6.6s",
89 'ident::p0f' => "%-10.10s",
90 distance => "%5.5s",
91 count_unrecognized_commands => "%-5.5s",
92 unrecognized_commands => "%-5.5s",
93 connection_time => "%-4.4s",
94 map { $_ => "%-3.3s" }
95 qw/ dnsbl rhsbl relay karma fcrdns earlytalker check_earlytalker helo
96 tls auth::auth_vpopmail auth::auth_vpopmaild auth::auth_vpopmail_sql
97 auth::auth_checkpassword badmailfrom check_badmailfrom
98 sender_permitted_from resolvable_fromhost dont_require_anglebrackets
99 queue::qmail-queue queue::smtp-forward /
100 );
101
102 return %widths;
103 };
104
105 sub handle_plugin {
106 my ($message, $plugin, $pid, $line) = @_;
107 return if $plugin eq 'naughty'; # housekeeping only
108 if (!$pids{$pid}{$plugin}) { # first entry for this plugin
109 $pids{$pid}{$plugin} = $message;
110 }
111 else { # subsequent log entry for this plugin
112 if ($pids{$pid}{$plugin} !~ /^(?:pass|fail|skip)/i) {
113 $pids{$pid}{$plugin} = $message; # overwrite 1st
114 }
115 else {
116 #print "ignoring subsequent hit on $plugin: $message\n";
117 }
118 }
119
120 if ($plugin eq 'ident::geoip') {
121 if (length $message < 3) {
122 $formats{'ident::geoip'} = "%-3.3s";
123 $formats3{'ident::geoip'} = "%-3.3s";
124 }
125 else {
126 my ($gip, $distance) = $message =~ /(.*?),\s+([\d]+)\skm/;
127 if ($distance) {
128 $pids{$pid}{$plugin} = $gip;
129 $pids{$pid}{distance} = $distance;
130 }
131 }
132 }
133 }
134
135 sub handle_dispatch {
136 my ($message, $pid, $line) = @_;
137 if ($message =~ /^dispatching MAIL FROM/i) {
138 my ($from) = $message =~ /<(.*?)>/;
139 $pids{$pid}{from} = $from || '';
140 }
141 elsif ($message =~ /^dispatching RCPT TO/i) {
142 my ($to) = $message =~ /<(.*?)>/;
143 $pids{$pid}{to} = $to || '';
144 }
145 elsif ($message =~ m/dispatching (EHLO|HELO) (.*)/) {
146 $pids{$pid}{helo_host} = $2 || '';
147 }
148 elsif ($message eq 'dispatching DATA') { }
149 elsif ($message eq 'dispatching QUIT') { }
150 elsif ($message eq 'dispatching STARTTLS') { }
151 elsif ($message eq 'dispatching RSET') {
152 print_auto_format($pid, $line);
153 }
154 else {
155 # anything here is likely an unrecognized command
156 #print "$message\n";
157 }
158 }
159
160 sub parse_line {
161 my $line = shift;
162 my ($tai, $pid, $message) = split /\s+/, $line, 3;
163 return if !$message; # garbage in the log file
164
165 # lines seen many times per connection
166 return parse_line_plugin($line) if substr($message, 0, 1) eq '(';
167 return ('dispatch', $pid, undef, undef, $message)
168 if substr($message, 0, 12) eq 'dispatching ';
169 return ('response', $pid, undef, undef, $message)
170 if $message =~ /^[2|3]\d\d/;
171 return ('tcpserver', $pid, undef, undef, undef)
172 if substr($pid, 0, 10) eq 'tcpserver:';
173
174 # lines seen about once per connection
175 return ('init', $pid, undef, undef, $message)
176 if substr($message, 0, 19) eq 'Accepted connection';
177 return ('connect', $pid, undef, undef, substr($message, 16))
178 if substr($message, 0, 15) eq 'Connection from';
179 return ('close', $pid, undef, undef, $message)
180 if substr($message, 0, 6) eq 'close ';
181 return ('close', $pid, undef, undef, $message)
182 if substr($message, 0, 20) eq 'click, disconnecting';
183 return parse_line_cleanup($line)
184 if substr($message, 0, 11) eq 'cleaning up';
185
186 # lines seen less than once per connection
187 return ('info', $pid, undef, undef, $message)
188 if $message eq 'spooling message to disk';
189 return ('reject', $pid, undef, undef, $message) if $message =~ /^[4|5]\d\d/;
190 return ('reject', $pid, undef, undef, $message)
191 if substr($message, 0, 14) eq 'deny mail from';
192 return ('reject', $pid, undef, undef, $message)
193 if substr($message, 0, 18) eq 'denysoft mail from';
194 return ('info', $pid, undef, undef, $message)
195 if substr($message, 0, 15) eq 'Lost connection';
196 return ('info', $pid, undef, undef, $message)
197 if $message eq 'auth success cleared naughty';
198 return ('info', $pid, undef, undef, $message)
199 if substr($message, 0, 15) eq 'Running as user';
200 return ('info', $pid, undef, undef, $message)
201 if substr($message, 0, 16) eq 'Loaded Qpsmtpd::';
202 return ('info', $pid, undef, undef, $message)
203 if substr($message, 0, 24) eq 'Permissions on spool_dir';
204 return ('info', $pid, undef, undef, $message)
205 if substr($message, 0, 13) eq 'Listening on ';
206
207 return ('err', $pid, undef, undef, $message)
208 if $line =~ /at [\S]+ line \d/; # generic perl error
209 print "UNKNOWN LINE: $line\n";
210 return ('unknown', $pid, undef, undef, $message);
211 }
212
213 sub parse_line_plugin {
214 my ($line) = @_;
215
216 # @tai 13486 (connect) ident::p0f: Windows (XP/2000 (RFC1323+, w, tstamp-))
217 # @tai 13681 (connect) dnsbl: fail, NAUGHTY
218 # @tai 15787 (connect) karma: pass, no penalty (0 naughty, 3 nice, 3 connects)
219 # @tai 77603 (queue) queue::qmail_2dqueue: (for 77590) Queuing to /var/qmail/bin/qmail-queue
220 my ($tai, $pid, $hook, $plugin, $message) = split /\s/, $line, 5;
221 $plugin =~ s/:$//;
222 if ($plugin =~ /_3a/) {
223 ($plugin) = split /_3a/, $plugin; # trim :N off the plugin log entry
224 }
225 $plugin =~ s/_2d/-/g;
226
227 $plugin = $plugin_aliases{$plugin}
228 if $plugin_aliases{$plugin}; # map alias to master
229 if ($hook eq '(queue)') {
230 ($pid) = $message =~ /\(for ([\d]+)\)\s/;
231 $message = 'pass';
232 }
233
234 return ('plugin', $pid, $hook, $plugin, $message);
235 }
236
237 sub parse_line_cleanup {
238 my ($line) = @_;
239
240 # @tai 85931 cleaning up after 3210
241 my $pid = (split /\s+/, $line)[-1];
242 $has_cleanup++;
243 return ('cleanup', $pid, undef, undef, $line);
244 }
245
246 sub print_auto_format {
247 my ($pid, $line) = @_;
248
249 my $format;
250 my @headers;
251 my @values;
252
253 foreach my $plugin (qw/ ip hostname distance /, @sorted_plugins) {
254 if (defined $pids{$pid}{$plugin}) {
255 if (!$seen_plugins{$plugin}) { # first time seeing this plugin
256 $printed = 0; # force header print
257 }
258 $seen_plugins{$plugin}++;
259 }
260
261 next if !$seen_plugins{$plugin}; # hide unused plugins
262 if ($hide_plugins{$plugin}) { # user doesn't want to see
263 delete $pids{$pid}{$plugin};
264 next;
265 }
266
267 my $wide = $opt_l ? 20 : 8;
268
269 if (defined $pids{$pid}{helo_host} && $plugin =~ /helo/) {
270 $format .= " %-$wide.${wide}s";
271 push @values, substr(delete $pids{$pid}{helo_host}, -$wide, $wide);
272 push @headers, 'HELO';
273 }
274 elsif (defined $pids{$pid}{from} && $plugin =~ /from/) {
275 $format .= " %-$wide.${wide}s";
276 push @values, substr(delete $pids{$pid}{from}, -$wide, $wide);
277 push @headers, 'MAIL FROM';
278 }
279 elsif (defined $pids{$pid}{to} && $plugin =~ /to|rcpt|recipient/) {
280 $format .= " %-$wide.${wide}s";
281 push @values, delete $pids{$pid}{to};
282 push @headers, 'RCPT TO';
283 }
284
285 $format .= $formats3{$plugin} ? " $formats3{$plugin}" : " %-10.10s";
286
287 if (defined $pids{$pid}{$plugin}) {
288 push @values, show_symbol(delete $pids{$pid}{$plugin});
289 }
290 else {
291 push @values, '';
292 }
293 push @headers,
294 ($plugins{$plugin}{abb3} ? $plugins{$plugin}{abb3} : $plugin);
295 }
296 $format .= "\n";
297 printf("\n$format", @headers) if (!$printed || $printed % 20 == 0);
298 printf($format, @values);
299 #print Data::Dumper::Dumper($pids{$pid}) if keys %{$pids{$pid}};
300 $printed++;
301 }
302
303 sub show_symbol {
304 my $mess = shift;
305 return ' o' if $mess eq 'TLS setup returning';
306 return ' o' if $mess eq 'pass';
307 return ' -' if $mess eq 'skip';
308 return ' x' if 'fail, tolerated' eq substr($mess, 0, 15);
309 return ' X' if $mess eq 'fail';
310 return ' -' if $mess =~ /^skip[,:\s]/i;
311 return ' o' if $mess =~ /^pass[,:\s]/i;
312 return ' X' if $mess =~ /^fail[,:\s]/i;
313 return ' x' if $mess =~ /^negative[,:\s]/i;
314 return ' o' if $mess =~ /^positive[,:\s]/i;
315 return ' !' if $mess =~ /^error[,:\s]/i;
316 $mess =~ s/\s\s/ /g;
317 return $mess;
318 }
319
320 sub get_qp_dir {
321 foreach my $user (qw/ qpsmtpd smtpd /) {
322 my ($homedir) = (getpwnam($user))[7] or next;
323
324 if (-d "$homedir/plugins") {
325 return "$homedir";
326 }
327 foreach my $s (qw/ smtpd qpsmtpd qpsmtpd-dev /) {
328 if (-d "$homedir/$s/plugins") {
329 return "$homedir/$s";
330 }
331 }
332 }
333 if (-d "./plugins") {
334 return Cwd::getcwd();
335 }
336 }
337
338 sub populate_plugins_from_registry {
339
340 my $file = "$qpdir/plugins/registry.txt";
341 if (!-f $file) {
342 die "unable to find plugin registry\n";
343 }
344
345 open my $F, '<', $file;
346 while (defined(my $line = <$F>)) {
347 next if $line =~ /^#/; # discard comments
348 chomp $line;
349 next if ! $line;
350 my ($id, $name, $abb3, $abb5, $aliases) = split /\s+/, $line;
351 next if !defined $name;
352 $plugins{$name} = {id => $id, abb3 => $abb3, abb5 => $abb5};
353
354 next if !$aliases;
355 $aliases =~ s/\s+//g;
356 $plugins{$name}{aliases} = $aliases;
357 foreach my $a (split /,/, $aliases) {
358 $plugin_aliases{$a} = $name;
359 }
360 }
361 }
362
363 __END__
364
365 =head1 NAME
366
367 Summarize
368
369 =head2 SYNOPSIS
370
371 Parse the qpsmtpd logs and display a one line summary of each connection
372
373 =head2 EXAMPLES
374
375 ip dista geo p0f krm dbl rly dns ear HELO hlo tls MAIL FRO bmf rbl rfh spf RCPT TO bto qmd rok tim
376 192.48.85.146 2705 NA, US FreeBSD 9. o o - o - tnpi.net o o 0.55
377 190.194.22.35 7925 SA, AR Windows 7 X X - X o a.net.ar x ogle.com o o o x *o*g@sim o o o 2.72
378 192.48.85.146 2705 NA, US - o o - o - tnpi.net o 0.41
379 181.164.160.98 8493 SA, AR Windows 7 X X - X o l.com.ar x ogle.com o o o x trapped@ o o o 2.61
380 188.79.146.22 8381 EU, ES Windows 7 o X - o o zztel.es o ogle.com o o o x *o**an@s o o o 3.02
381 188.79.146.22 8381 EU, ES Windows 7 o X - o o zztel.es o ogle.com o o o x *o**an@s o o o 2.58
382 188.79.146.22 8381 EU, ES Windows 7 o X - o o zztel.es o ogle.com o o o x *o**an@s o o o 2.70
383 190.194.22.35 7925 SA, AR Windows 7 X X - X o a.net.ar x ogle.com o o o x do*g@s*m o o o 2.60
384
385 ip dista geo p0f krm dbl rly dns ear HELO hlo tls MAIL FRO bmf rbl rfh spf RCPT TO bto qmd rok bog hdr dky dkm dmc spm dsp clm qqm tim
386 192.48.85.146 2705 NA, US FreeBSD 9. o o - o - tnpi.net o o 1.36
387 192.48.85.146 2705 NA, US - o o - o - tnpi.net o 0.36
388 66.175.56.179 2313 NA, US Linux 2.6. o o - o - zone.com o o chem.com o o o - d**n@the o o o o o - o - - - - o 2.86
389 190.237.55.32 5411 SA, PE Windows 7 o X - X o gtsgnvnu x ryrk.net o o x - *an@s*rl o o o 3.54
390 192.48.85.146 2705 NA, US - o o - o - tnpi.net o 0.20
391 207.171.174.77 2700 NA, US o o - o - azon.com o azon.com o o o o *a*e@s*r o o o o o - o o o o o o 7.27
392 201.141.78.4 1487 NA, MX Windows XP o X - X o fmhufhjo x fdvx.net o o x - d**@si*e o o o 2.95
393 201.141.78.4 1487 NA, MX Windows XP X X - X o fmhufhjo x fdvx.net o o x - d**@s*rl o o o 2.42
394
395 The display autosizes to display disposition results for as many plugins as are emitting logs. The 3 char abbreviations are listed with their full plugin names in plugins/registry.txt. The GeoIP, p0f, HELO, FROM, and RCPT fields are compressed to fit on a typical display. If you have a wider display, use the -l option to display longer lines and more detail.
396
397 Starting from left to right, in the first block, the results are interpreted as follows:
398
399 geo - We see 2 connections from N. America, 3 from S. America, and 3 from Europe.
400 p0f - One system is running FreeBSD and the rest are running Windows 7.
401 krm - 3 of the connections will be rejected because of bad karma (sender history)
402 dbl - 7 are from IPs on DNS blacklists, an offense worth rejecting for.
403 rly - None of the IPs have relay permission.
404 dns - Only three senders have Forward Confirmed Reverse DNS
405 ear - two connections skipped testing (good karma), and the rest passed
406 hlo - three of the senders failed to present valid HELO hostnames
407 tls - one sender negotiated TLS
408 bmf - none of the senders presented a from address in our badmailfrom list
409 rbl - none of the sender domains are in a RHS blocking list
410 rfh - resolvable_from_host: all the sender domains resolve
411 spf - all but two connections fail SPF, meaning they are forging the envelope sender identity
412 bto - badmailto: none of the recipients are in our badmailto list
413 qmd - qmail_deliverable: the recipients are valid addresses on our system
414 rok - the recipient domain is on our system
415 tim - the number of seconds the connection was active
416
417 In the second block, we have two messages that were ultimately delivered.
418
419 bog - no messages were bogus bounces
420 hdr - the messages had valid headers
421 dky - the messages were not DomainKeys signed
422 dkm - two messages were DKIM signed and passed validation
423 dmc - the message from amazon.com passed DMARC validation
424 spm - spamassassin, one skipped processing, one passed
425 dsp - dspam, one skipped, one passed
426 clm - clamav, one skipped, one passed
427 qqm - qmail queue, two messages were delivered
428
429 In the first block of entries, not a single connection made it past the DATA phase of the SMTP conversation, where the content tests kick in. Other interesting observations are that many connections purport to be from Google. Ah, you say, but does Google have Windows mail servers in Estonia? If we look over to the SPF column, the lower case x is telling us that it failed SPF tests, meaning Google has explicitely told us that IP is not theirs. Instead of rejecting immediately, the SPF plugin deferred the rejection to B<naughty> to disconnect later.
430
431 =head1 AUTHOR
432
433 Matt Simerson <[email protected]>
434
435 =cut
436