| 1 | package eris::log::context::postfix; | ||||||||||||||
| 2 | # ABSTRACT: Parses postfix messages into structured data | ||||||||||||||
| 3 | |||||||||||||||
| 4 | 1 | ▶ | use Const::Fast; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 1 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 1 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 1 | with qw( | |||||||||||||
| 9 | eris::role::context | ||||||||||||||
| 10 | ); | ||||||||||||||
| 11 | |||||||||||||||
| 12 | 1 | our $VERSION = '0.008'; # VERSION | |||||||||||||
| 13 | |||||||||||||||
| 14 | |||||||||||||||
| 15 | 0 | ▶ | sub _build_matcher { qr/^postfix/ } | ||||||||||||
|
Subroutine
_build_matcher: not called CC=1 SCAR=6.9
| |||||||||||||||
| 16 | |||||||||||||||
| 17 | |||||||||||||||
| 18 | sub sample_messages { | ||||||||||||||
| 19 | 0 | ▶ | my @msgs = split /\r?\n/, <<'EOF'; | ||||||||||||
|
Subroutine
sample_messages: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 20 | Nov 20 04:22:13 janus postfix/smtpd[19324]: connect from mailer28.promotebusiness.gr[62.38.238.232] | ||||||||||||||
| 21 | Nov 20 04:22:15 janus postfix/smtpd[19324]: NOQUEUE: reject: RCPT from mailer28.promotebusiness.gr[62.38.238.232]: 450 4.2.0 <brad@divisionbyzero.net>: Recipient address rejected: Greylisted, see http://postgrey.schweikert.ch/help/divisionbyzero.net.html; from=<newsletter@e-telekat.net> to=<brad@divisionbyzero.net> proto=ESMTP helo=<mailer28.promotebusiness.gr> | ||||||||||||||
| 22 | Nov 20 04:22:15 janus postfix/smtpd[19324]: disconnect from mailer28.promotebusiness.gr[62.38.238.232] | ||||||||||||||
| 23 | Nov 20 04:25:35 janus postfix/anvil[18199]: statistics: max connection rate 2/60s for (smtp:2600:3c03::f03c:91ff:fe93:c5e8) at Nov 20 04:19:15 | ||||||||||||||
| 24 | Nov 20 04:25:35 janus postfix/anvil[18199]: statistics: max connection count 1 for (smtp:2600:3c03::f03c:91ff:fe93:c5e8) at Nov 20 04:19:14 | ||||||||||||||
| 25 | Nov 20 04:25:35 janus postfix/anvil[18199]: statistics: max cache size 1 at Nov 20 04:19:14 | ||||||||||||||
| 26 | Nov 20 04:28:22 janus postfix/smtpd[20365]: connect from mail.astelecom.ru[83.142.9.162] | ||||||||||||||
| 27 | Nov 20 04:28:22 janus postfix/smtpd[20365]: NOQUEUE: reject: RCPT from mail.astelecom.ru[83.142.9.162]: 450 4.1.8 <payments@ustreasury.gov>: Sender address rejected: Domain not found; from=<payments@ustreasury.gov> to=<brad@divisionbyzero.net> proto=ESMTP helo=<mail.astelecom.ru> | ||||||||||||||
| 28 | Nov 20 04:28:22 janus postfix/smtpd[20365]: disconnect from mail.astelecom.ru[83.142.9.162] | ||||||||||||||
| 29 | Nov 20 04:29:10 janus postfix/smtpd[20365]: connect from unknown[198.52.131.89] | ||||||||||||||
| 30 | Nov 20 04:29:11 janus postfix/smtpd[20365]: NOQUEUE: reject_warning: RCPT from unknown[198.52.131.89]: 450 4.7.1 Client host rejected: cannot find your reverse hostname, [198.52.131.89]; from=<Christie.Brinkley.Skincare@grieving.thoweam.top> to=<brad@divisionbyzero.net> proto=ESMTP helo=<grieving.thoweam.top> | ||||||||||||||
| 31 | Nov 20 04:29:12 janus postfix/smtpd[20365]: NOQUEUE: reject: RCPT from unknown[198.52.131.89]: 554 5.7.1 Service unavailable; Client host [198.52.131.89] blocked using zen.spamhaus.org; https://www.spamhaus.org/sbl/query/SBLCSS; from=<Christie.Brinkley.Skincare@grieving.thoweam.top> to=<brad@divisionbyzero.net> proto=ESMTP helo=<grieving.thoweam.top> | ||||||||||||||
| 32 | Nov 20 04:29:12 janus postfix/smtpd[20365]: disconnect from unknown[198.52.131.89] | ||||||||||||||
| 33 | Nov 20 06:44:57 janus postfix/smtpd[15590]: connect from localhost[127.0.0.1] | ||||||||||||||
| 34 | Nov 20 06:44:57 janus postfix/smtpd[15590]: NOQUEUE: reject_warning: RCPT from localhost[127.0.0.1]: 450 4.7.1 <notify.ossec.net>: Helo command rejected: Host not found; from=<ossec@divisionbyzero.net> to=<security@db0.us> proto=SMTP helo=<notify.ossec.net> | ||||||||||||||
| 35 | Nov 20 06:44:57 janus postfix/smtpd[15590]: A2FE829C1076: client=localhost[127.0.0.1] | ||||||||||||||
| 36 | Nov 20 06:44:57 janus postfix/cleanup[15611]: A2FE829C1076: message-id=<20161120054457.A2FE829C1076@janus.divisionbyzero.net> | ||||||||||||||
| 37 | Nov 20 06:44:57 janus opendkim[1390]: A2FE829C1076: DKIM-Signature field added (s=default, d=divisionbyzero.net) | ||||||||||||||
| 38 | Nov 20 06:44:57 janus postfix/qmgr[13857]: A2FE829C1076: from=<ossec@divisionbyzero.net>, size=1584, nrcpt=1 (queue active) | ||||||||||||||
| 39 | Nov 20 06:44:57 janus postfix/smtpd[15590]: disconnect from localhost[127.0.0.1] | ||||||||||||||
| 40 | EOF | ||||||||||||||
| 41 | 0 | return @msgs; | |||||||||||||
| 42 | } | ||||||||||||||
| 43 | |||||||||||||||
| 44 | |||||||||||||||
| 45 | 1 | const my %MAP => qw( | |||||||||||||
| 46 | from src_user | ||||||||||||||
| 47 | to dst_user | ||||||||||||||
| 48 | proto proto_app | ||||||||||||||
| 49 | size in_bytes | ||||||||||||||
| 50 | helo src | ||||||||||||||
| 51 | ); | ||||||||||||||
| 52 | |||||||||||||||
| 53 | sub contextualize_message { | ||||||||||||||
| 54 | 0 | ▶ | my ($self,$log) = @_; | ||||||||||||
|
Subroutine
contextualize_message: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||
| 55 | 0 | my $c = $log->context; | |||||||||||||
| 56 | 0 | my $str = $c->{message}; | |||||||||||||
| 57 | |||||||||||||||
| 58 | 0 | $log->add_tags(qw(mail)); | |||||||||||||
| 59 | 0 | my %ctxt = (); | |||||||||||||
| 60 | |||||||||||||||
| 61 | 0 | ▶ | if( my @connection = ($str =~ /^((?:dis)?connect) from ([^\[]+)\[([^\]]+)\]/) ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 62 | 0 | @ctxt{qw(action src src_ip)} = @connection; | |||||||||||||
| 63 | } | ||||||||||||||
| 64 | elsif( my @details = ($str =~ /^([A-F0-9]{8,16}): (.*)$/) ) { | ||||||||||||||
| 65 | 0 | $ctxt{rec_id} = $details[0]; | |||||||||||||
| 66 | 0 | foreach my $kv ( split /, /, $details[1] ) { | |||||||||||||
| 67 | 0 | ▶ | if( my ($k,$v) = ($kv =~ /(\w+)=<?([^>]+)>?/) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 68 | 0 | ▶ | $ctxt{$MAP{$k}} = $v if exists $MAP{$k}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 69 | } | ||||||||||||||
| 70 | } | ||||||||||||||
| 71 | } | ||||||||||||||
| 72 | else { | ||||||||||||||
| 73 | # Last ditch effort to grab information | ||||||||||||||
| 74 | 0 | ▶ | if( my @conn = ($str =~ /(?>\b(from|to) ([^\[]+)\[([^\]]+)\])/) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 75 | 0 | ▶ | my @fields = shift @conn eq 'from' ? qw(src src_ip) : qw(dst dst_ip); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 76 | 0 | @ctxt{@fields} = @conn; | |||||||||||||
| 77 | 0 | ▶ | if ( $str =~ /NOQUEUE: ([^:]+):/ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 78 | 0 | $ctxt{status} = $1; | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | } | ||||||||||||||
| 81 | } | ||||||||||||||
| 82 | # install the /v pairs | ||||||||||||||
| 83 | 0 | ▶ | if ( $c->{sdata} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 84 | 0 | foreach my $k (sort keys %MAP) { | |||||||||||||
| 85 | 0 | ▶ | next unless exists $c->{sdata}{$k}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 86 | 0 | $ctxt{$MAP{$k}} = $c->{sdata}{$k}; | |||||||||||||
| 87 | } | ||||||||||||||
| 88 | } | ||||||||||||||
| 89 | |||||||||||||||
| 90 | 0 | ▶ | $log->add_context($self->name,\%ctxt) if keys %ctxt; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 91 | } | ||||||||||||||
| 92 | |||||||||||||||
| 93 | |||||||||||||||
| 94 | 1 | 1; | |||||||||||||
| 95 | |||||||||||||||
| 96 | __END__ | ||||||||||||||
| 97 | |||||||||||||||
| 98 | =pod | ||||||||||||||
| 99 | |||||||||||||||
| 100 | =encoding UTF-8 | ||||||||||||||
| 101 | |||||||||||||||
| 102 | =head1 NAME | ||||||||||||||
| 103 | |||||||||||||||
| 104 | eris::log::context::postfix - Parses postfix messages into structured data | ||||||||||||||
| 105 | |||||||||||||||
| 106 | =head1 VERSION | ||||||||||||||
| 107 | |||||||||||||||
| 108 | version 0.008 | ||||||||||||||
| 109 | |||||||||||||||
| 110 | =head1 SYNOPSIS | ||||||||||||||
| 111 | |||||||||||||||
| 112 | Parses postfix messages to extract relevant and interesting data. | ||||||||||||||
| 113 | |||||||||||||||
| 114 | =head1 ATTRIBUTES | ||||||||||||||
| 115 | |||||||||||||||
| 116 | =head2 matcher | ||||||||||||||
| 117 | |||||||||||||||
| 118 | A regex starting with the word 'postfix' | ||||||||||||||
| 119 | |||||||||||||||
| 120 | =head1 METHODS | ||||||||||||||
| 121 | |||||||||||||||
| 122 | =head2 contextualize_message | ||||||||||||||
| 123 | |||||||||||||||
| 124 | Parses a postfix messages into a structured document, extracting status | ||||||||||||||
| 125 | information, IP addresses, and email addresses. | ||||||||||||||
| 126 | |||||||||||||||
| 127 | action => Connect/Disconnect, etc. | ||||||||||||||
| 128 | dst_ip => Receiver IP | ||||||||||||||
| 129 | dst_user => "To" email address | ||||||||||||||
| 130 | proc => Postfix daemon issuing the message, e.g.: smtpd, anvil | ||||||||||||||
| 131 | program => Set to 'postfix' for simplicity | ||||||||||||||
| 132 | proto_app => ESMTP, SMTP, LMTP, etc. | ||||||||||||||
| 133 | src => Advertised server name taking action | ||||||||||||||
| 134 | src_ip => Sender IP | ||||||||||||||
| 135 | src_user => "From" email address | ||||||||||||||
| 136 | status => Whether the operation was successful | ||||||||||||||
| 137 | |||||||||||||||
| 138 | Tags messages with 'mail' | ||||||||||||||
| 139 | |||||||||||||||
| 140 | =for Pod::Coverage sample_messages | ||||||||||||||
| 141 | |||||||||||||||
| 142 | =head1 SEE ALSO | ||||||||||||||
| 143 | |||||||||||||||
| 144 | L<eris::log::contextualizer>, L<eris::role::context> | ||||||||||||||
| 145 | |||||||||||||||
| 146 | =head1 AUTHOR | ||||||||||||||
| 147 | |||||||||||||||
| 148 | Brad Lhotsky <brad@divisionbyzero.net> | ||||||||||||||
| 149 | |||||||||||||||
| 150 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 151 | |||||||||||||||
| 152 | This software is Copyright (c) 2015 by Brad Lhotsky. | ||||||||||||||
| 153 | |||||||||||||||
| 154 | This is free software, licensed under: | ||||||||||||||
| 155 | |||||||||||||||
| 156 | The (three-clause) BSD License | ||||||||||||||
| 157 | |||||||||||||||
| 158 | =cut | ||||||||||||||