| 1 | package Mail::Decency::LogParser::Core::PostfixParser; | ||
| 2 | |||
| 3 | use Moose::Role; | ||
| 4 | |||
| 5 | use version 0.74; our $VERSION = qv( "v0.1.6" ); | ||
| 6 | |||
| 7 | use Data::Dumper; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Mail::Decency::LogParser::Core::LogParser | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | Parse logs in postfix style. Also can be used as background for another log parser module | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | our $RX_HOST_AND_IP = qr/([^\]]*?)\[([^\]]+?)\]/; | ||
| 20 | |||
| 21 | =head1 METHODS | ||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | =head2 parse_line | ||
| 26 | |||
| 27 | Parses a single line and returns a parsed hashref which will passed to the handling modules. | ||
| 28 | |||
| 29 | Returns parsed line as hashref: | ||
| 30 | |||
| 31 | { | ||
| 32 | # wheter mail was rejected | ||
| 33 | reject => [0|1], | ||
| 34 | |||
| 35 | # wheter the log processing of this mail delivery is finished | ||
| 36 | final => [0|1], | ||
| 37 | |||
| 38 | # the hostname sending/injecting the mail | ||
| 39 | host => [string], | ||
| 40 | |||
| 41 | # the ip sending/injecting the mail | ||
| 42 | ip => [ipv4 or ipv6 string], | ||
| 43 | |||
| 44 | # the reverse hostname of the sending/injecting mails seerver | ||
| 45 | rdns => [ipv4 or ipv6 string], | ||
| 46 | |||
| 47 | # the helo name of the sending/injecting mail server | ||
| 48 | helo => [string], | ||
| 49 | |||
| 50 | # the senders/recipients address or domain | ||
| 51 | (from|to)_(address|domain) => [string] | ||
| 52 | |||
| 53 | |||
| 54 | # | ||
| 55 | # REJECTED | ||
| 56 | # | ||
| 57 | |||
| 58 | # final deliver/reject code (200, 554, ..) | ||
| 59 | code => [string], | ||
| 60 | |||
| 61 | # for rejections: the reason why the mail is rejected | ||
| 62 | message => [string], | ||
| 63 | |||
| 64 | # | ||
| 65 | # NOT REJECTED | ||
| 66 | # | ||
| 67 | |||
| 68 | # wheter the mail has been queued (rejected mails will not) | ||
| 69 | queued => [0|1], | ||
| 70 | |||
| 71 | # the current queue id in the log | ||
| 72 | queue_id => [string], | ||
| 73 | |||
| 74 | # depending on the MTA, sub division which handled thisi particula log line | ||
| 75 | prog => [string, eg "bounce" for postfix..], | ||
| 76 | |||
| 77 | # the relay target (hostname, ip), if any | ||
| 78 | relay_(host|ip) => [string], | ||
| 79 | |||
| 80 | # the size of the mail in bytes | ||
| 81 | size => [integer], | ||
| 82 | |||
| 83 | # wheter the mail has been removed (can be happen before final) | ||
| 84 | removed => [0|1], | ||
| 85 | |||
| 86 | # what the final handling/delivery was | ||
| 87 | (bounced|sent|deferred) => [0|1], | ||
| 88 | |||
| 89 | # the next queue id, eg if the mail is to be bounced | ||
| 90 | next_id => [string], | ||
| 91 | |||
| 92 | # the previous queue id, eg if this is a bounce mail | ||
| 93 | prev_id => [string], | ||
| 94 | |||
| 95 | # wheter the current mail is a bounce (eg this is the bounce mail or the mail to be bounced) | ||
| 96 | is_bounce => [0|1], | ||
| 97 | |||
| 98 | # the original from (a bounce mail will be delivered from "") | ||
| 99 | orig_from => [string], | ||
| 100 | } | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | sub parse_line { | ||
| 105 | my ( $self, $line ) = @_; | ||
| 106 | |||
| 107 | #$self->logger->debug3( "Got line '$line'" ); | ||
| 108 | return if index( $line, 'postfix/' ) == -1 || index( $line, ' warning:' ) > -1; | ||
| 109 | |||
| 110 | return if $line =~ / (dis)?connect from/; | ||
| 111 | |||
| 112 | my $ref = {}; | ||
| 113 | my $queue_id; | ||
| 114 | |||
| 115 | # found REJECT | ||
| 116 | if ( index( $line, ' NOQUEUE:' ) > -1 ) { | ||
| 117 | if ( $line =~ / reject: RCPT from $RX_HOST_AND_IP: (\d\d\d) [^:]+?: ([^;]*?);/ ) { | ||
| 118 | $ref->{ reject }++; | ||
| 119 | $ref->{ final }++; | ||
| 120 | $ref->{ host } = $1; | ||
| 121 | $ref->{ ip } = $2; | ||
| 122 | $ref->{ code } = $3; | ||
| 123 | $ref->{ message } = $4; | ||
| 124 | } | ||
| 125 | else { | ||
| 126 | return; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | # found QUEUED message | ||
| 131 | else { | ||
| 132 | |||
| 133 | # parse .. | ||
| 134 | my ( $prog, $id, $msg ) = $line =~ / | ||
| 135 | postfix \/ ([^\[]+) # cleanup, bounce, .. | ||
| 136 | \[\d+\]:\s+ # some process id | ||
| 137 | ([A-Z0-9]+):\s+ # the queue id | ||
| 138 | (.+) # rest of the message | ||
| 139 | /x; | ||
| 140 | |||
| 141 | # mark as queued | ||
| 142 | $ref->{ queued } ++; | ||
| 143 | |||
| 144 | # remember id and prog | ||
| 145 | $queue_id = $ref->{ id } = $id; | ||
| 146 | $ref->{ prog } = $prog; | ||
| 147 | } | ||
| 148 | |||
| 149 | # got sender or recupuent | ||
| 150 | my $found_from = 0; | ||
| 151 | while ( $line =~ /\b(from|to)=<([^>]*)/g ) { | ||
| 152 | my ( $type, $value ) = ( $1, $2 ); | ||
| 153 | $ref->{ "${type}_address" } = $value; | ||
| 154 | if ( $value =~ /^[^@]+@(.+?)$/ ) { | ||
| 155 | $ref->{ "${type}_domain" } = $1; | ||
| 156 | } | ||
| 157 | $found_from ++ if $type eq 'from'; | ||
| 158 | } | ||
| 159 | |||
| 160 | # got helo | ||
| 161 | if ( $line =~ /helo=<([^>]*)/ ) { | ||
| 162 | $ref->{ helo } = $1; | ||
| 163 | } | ||
| 164 | |||
| 165 | # got sender | ||
| 166 | if ( $line =~ /client=$RX_HOST_AND_IP/ ) { | ||
| 167 | $ref->{ rdns } = $1; | ||
| 168 | $ref->{ ip } = $2; | ||
| 169 | } | ||
| 170 | |||
| 171 | # workign on queue id (not no-queue) | ||
| 172 | if ( $queue_id ) { | ||
| 173 | |||
| 174 | # got relay target | ||
| 175 | if ( $line =~ /\brelay=([^\[,]*)(?:\[([^\]]*)\])?/ ) { | ||
| 176 | $ref->{ relay_host } = $1; | ||
| 177 | $ref->{ relay_ip } = $2 || ''; | ||
| 178 | } | ||
| 179 | |||
| 180 | # got suze | ||
| 181 | if ( $line =~ /\bsize=(\d+)/ ) { | ||
| 182 | $ref->{ size } = $1; | ||
| 183 | } | ||
| 184 | |||
| 185 | # got final status | ||
| 186 | if ( $line =~ /\bstatus=(bounced|sent|deferred)\b/ ) { | ||
| 187 | $ref->{ $1 }++; | ||
| 188 | $ref->{ final }++; | ||
| 189 | } | ||
| 190 | |||
| 191 | # got final remove | ||
| 192 | elsif ( $line =~ / removed$/ ) { | ||
| 193 | $ref->{ removed }++; | ||
| 194 | } | ||
| 195 | |||
| 196 | # try read current from cache | ||
| 197 | my $cached = $self->cache->get( "QUEUE-$queue_id" ); | ||
| 198 | if ( $cached ) { | ||
| 199 | |||
| 200 | # not final if has next | ||
| 201 | if ( $cached->{ next_id } ) { | ||
| 202 | delete $ref->{ final }; | ||
| 203 | } | ||
| 204 | |||
| 205 | # update self | ||
| 206 | $ref = { %$cached, %$ref }; | ||
| 207 | delete $ref->{ final } if $found_from; | ||
| 208 | delete $ref->{ deferred } if $ref->{ sent }; | ||
| 209 | } | ||
| 210 | |||
| 211 | |||
| 212 | # non delivery | ||
| 213 | if ( $line =~ /sender non-delivery notification: ([A-Z0-9]+)/ ) { | ||
| 214 | my $next_id = $1; | ||
| 215 | |||
| 216 | # create new cache entry | ||
| 217 | my %next = %$ref; | ||
| 218 | push @{ $next{ prev } ||= [] }, $ref; | ||
| 219 | $next{ orig_from } = $ref->{ from } if $ref->{ from }; | ||
| 220 | $next{ prev_id } = $queue_id; | ||
| 221 | $next{ queue_id } = $next_id; | ||
| 222 | $next{ is_bounce } = 1; | ||
| 223 | delete $next{ next_id }; | ||
| 224 | |||
| 225 | # save next instance to cache | ||
| 226 | $self->cache->set( "QUEUE-$next_id", \%next, time() + 600 ); | ||
| 227 | |||
| 228 | # current is not final anymore | ||
| 229 | $ref->{ next_id } = $next_id; | ||
| 230 | delete $ref->{ final }; | ||
| 231 | } | ||
| 232 | |||
| 233 | # update current to cache | ||
| 234 | $self->cache->set( "QUEUE-$queue_id", $ref, time() + 600 ); | ||
| 235 | } | ||
| 236 | |||
| 237 | $queue_id ||= "NOQUEUE"; | ||
| 238 | $self->logger->debug3( Dumper( { | ||
| 239 | $queue_id => $ref, | ||
| 240 | LINE => $line | ||
| 241 | } ) ) if 0; | ||
| 242 | |||
| 243 | return $ref; | ||
| 244 | } | ||
| 245 | |||
| 246 | =head1 AUTHOR | ||
| 247 | |||
| 248 | Ulrich Kautz <uk@fortrabbit.de> | ||
| 249 | |||
| 250 | =head1 COPYRIGHT | ||
| 251 | |||
| 252 | Copyright (c) 2010 the L</AUTHOR> as listed above | ||
| 253 | |||
| 254 | =head1 LICENCSE | ||
| 255 | |||
| 256 | This library is free software and may be distributed under the same terms as perl itself. | ||
| 257 | |||
| 258 | =cut | ||
| 259 | |||
| 260 | 1; |