1package Sisimai::Lhost::Exim; 2use parent 'Sisimai::Lhost'; 3use feature ':5.10'; 4use strict; 5use warnings; 6 7sub description { 'Exim' } 8sub make { 9 # Detect an error from Exim 10 # @param [Hash] mhead Message headers of a bounce email 11 # @param [String] mbody Message body of a bounce email 12 # @return [Hash] Bounce data list and message/rfc822 part 13 # @return [Undef] failed to parse or the arguments are missing 14 # @since v4.0.0 15 my $class = shift; 16 my $mhead = shift // return undef; 17 my $mbody = shift // return undef; 18 my $match = 0; 19 return undef if $mhead->{'from'} =~/[@].+[.]mail[.]ru[>]?/; 20 21 # Message-Id: <E1P1YNN-0003AD-Ga@example.org> 22 # X-Failed-Recipients: kijitora@example.ed.jp 23 $match++ if index($mhead->{'from'}, 'Mail Delivery System') == 0; 24 $match++ if defined $mhead->{'message-id'} && 25 $mhead->{'message-id'} =~ /\A[<]\w{7}[-]\w{6}[-]\w{2}[@]/; 26 $match++ if $mhead->{'subject'} =~ qr{(?: 27 Mail[ ]delivery[ ]failed(:[ ]returning[ ]message[ ]to[ ]sender)? 28 |Warning:[ ]message[ ][^ ]+[ ]delayed[ ]+ 29 |Delivery[ ]Status[ ]Notification 30 |Mail[ ]failure 31 |Message[ ]frozen 32 |error[(]s[)][ ]in[ ]forwarding[ ]or[ ]filtering 33 ) 34 }x; 35 return undef if $match < 2; 36 37 state $indicators = __PACKAGE__->INDICATORS; 38 state $rebackbone = qr{^(?: 39 # deliver.c:6423| if (bounce_return_body) fprintf(f, 40 # deliver.c:6424|"------ This is a copy of the message, including all the headers. ------\n"); 41 # deliver.c:6425| else fprintf(f, 42 # deliver.c:6426|"------ This is a copy of the message's headers. ------\n"); 43 [-]+[ ]This[ ]is[ ]a[ ]copy[ ]of[ ](?:the|your)[ ]message,[ ]including[ ]all[ ]the[ ]headers[.][ ][-]+ 44 |Content-Type:[ ]*message/rfc822\n(?:[\s\t]+.*?\n\n)? 45 ) 46 }msx; 47 state $startingof = { 'deliverystatus' => ['Content-type: message/delivery-status'] }; 48 state $markingsof = { 49 # Error text regular expressions which defined in exim/src/deliver.c 50 # 51 # deliver.c:6292| fprintf(f, 52 # deliver.c:6293|"This message was created automatically by mail delivery software.\n"); 53 # deliver.c:6294| if (to_sender) 54 # deliver.c:6295| { 55 # deliver.c:6296| fprintf(f, 56 # deliver.c:6297|"\nA message that you sent could not be delivered to one or more of its\n" 57 # deliver.c:6298|"recipients. This is a permanent error. The following address(es) failed:\n"); 58 # deliver.c:6299| } 59 # deliver.c:6300| else 60 # deliver.c:6301| { 61 # deliver.c:6302| fprintf(f, 62 # deliver.c:6303|"\nA message sent by\n\n <%s>\n\n" 63 # deliver.c:6304|"could not be delivered to one or more of its recipients. The following\n" 64 # deliver.c:6305|"address(es) failed:\n", sender_address); 65 # deliver.c:6306| } 66 'alias' => qr/\A([ ]+an undisclosed address)\z/, 67 'message' => qr{\A(?> 68 This[ ]message[ ]was[ ]created[ ]automatically[ ]by[ ]mail[ ]delivery[ ]software[.] 69 |A[ ]message[ ]that[ ]you[ ]sent[ ]was[ ]rejected[ ]by[ ]the[ ]local[ ]scannning[ ]code 70 |A[ ]message[ ]that[ ]you[ ]sent[ ]contained[ ]one[ ]or[ ]more[ ]recipient[ ]addresses[ ] 71 |A[ ]message[ ]that[ ]you[ ]sent[ ]could[ ]not[ ]be[ ]delivered[ ]to[ ]all[ ]of[ ]its[ ]recipients 72 |Message[ ][^ ]+[ ](?:has[ ]been[ ]frozen|was[ ]frozen[ ]on[ ]arrival) 73 |The[ ][^ ]+[ ]router[ ]encountered[ ]the[ ]following[ ]error[(]s[)]: 74 ) 75 }x, 76 'frozen' => qr/\AMessage [^ ]+ (?:has been frozen|was frozen on arrival)/, 77 }; 78 state $recommands = [ 79 # transports/smtp.c:564| *message = US string_sprintf("SMTP error from remote mail server after %s%s: " 80 # transports/smtp.c:837| string_sprintf("SMTP error from remote mail server after RCPT TO:<%s>: " 81 qr/SMTP error from remote (?:mail server|mailer) after ([A-Za-z]{4})/, 82 qr/SMTP error from remote (?:mail server|mailer) after end of ([A-Za-z]{4})/, 83 qr/LMTP error after ([A-Za-z]{4})/, 84 qr/LMTP error after end of ([A-Za-z]{4})/, 85 ]; 86 state $messagesof = { 87 # find exim/ -type f -exec grep 'message = US' {} /dev/null \; 88 # route.c:1158| DEBUG(D_uid) debug_printf("getpwnam() returned NULL (user not found)\n"); 89 'userunknown' => ['user not found'], 90 # transports/smtp.c:3524| addr->message = US"all host address lookups failed permanently"; 91 # routers/dnslookup.c:331| addr->message = US"all relevant MX records point to non-existent hosts"; 92 # route.c:1826| uschar *message = US"Unrouteable address"; 93 'hostunknown' => [ 94 'all host address lookups failed permanently', 95 'all relevant MX records point to non-existent hosts', 96 'Unrouteable address', 97 ], 98 # transports/appendfile.c:2567| addr->user_message = US"mailbox is full"; 99 # transports/appendfile.c:3049| addr->message = string_sprintf("mailbox is full " 100 # transports/appendfile.c:3050| "(quota exceeded while writing to file %s)", filename); 101 'mailboxfull' => [ 102 'mailbox is full', 103 'error: quota exceed', 104 ], 105 # routers/dnslookup.c:328| addr->message = US"an MX or SRV record indicated no SMTP service"; 106 # transports/smtp.c:3502| addr->message = US"no host found for existing SMTP connection"; 107 'notaccept' => [ 108 'an MX or SRV record indicated no SMTP service', 109 'no host found for existing SMTP connection', 110 ], 111 # parser.c:666| *errorptr = string_sprintf("%s (expected word or \"<\")", *errorptr); 112 # parser.c:701| if(bracket_count++ > 5) FAILED(US"angle-brackets nested too deep"); 113 # parser.c:738| FAILED(US"domain missing in source-routed address"); 114 # parser.c:747| : string_sprintf("malformed address: %.32s may not follow %.*s", 115 'syntaxerror' => [ 116 'angle-brackets nested too deep', 117 'expected word or "<"', 118 'domain missing in source-routed address', 119 'malformed address:', 120 ], 121 # deliver.c:5614| addr->message = US"delivery to file forbidden"; 122 # deliver.c:5624| addr->message = US"delivery to pipe forbidden"; 123 # transports/pipe.c:1156| addr->user_message = US"local delivery failed"; 124 'systemerror' => [ 125 'delivery to file forbidden', 126 'delivery to pipe forbidden', 127 'local delivery failed', 128 'LMTP error after ', 129 ], 130 # deliver.c:5425| new->message = US"Too many \"Received\" headers - suspected mail loop"; 131 'contenterror' => ['Too many "Received" headers'], 132 }; 133 state $delayedfor = [ 134 # retry.c:902| addr->message = (addr->message == NULL)? US"retry timeout exceeded" : 135 # deliver.c:7475| "No action is required on your part. Delivery attempts will continue for\n" 136 # smtp.c:3508| US"retry time not reached for any host after a long failure period" : 137 # smtp.c:3508| US"all hosts have been failing for a long time and were last tried " 138 # "after this message arrived"; 139 # deliver.c:7459| print_address_error(addr, f, US"Delay reason: "); 140 # deliver.c:7586| "Message %s has been frozen%s.\nThe sender is <%s>.\n", message_id, 141 # receive.c:4021| moan_tell_someone(freeze_tell, NULL, US"Message frozen on arrival", 142 # receive.c:4022| "Message %s was frozen on arrival by %s.\nThe sender is <%s>.\n", 143 'retry timeout exceeded', 144 'No action is required on your part', 145 'retry time not reached for any host after a long failure period', 146 'all hosts have been failing for a long time and were last tried', 147 'Delay reason: ', 148 'has been frozen', 149 'was frozen on arrival by ', 150 ]; 151 152 require Sisimai::RFC1894; 153 my $fieldtable = Sisimai::RFC1894->FIELDTABLE; 154 my $dscontents = [__PACKAGE__->DELIVERYSTATUS]; 155 my $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone); 156 my $readcursor = 0; # (Integer) Points the current cursor position 157 my $nextcursor = 0; 158 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header 159 my $localhost0 = ''; # (String) Local MTA 160 my $boundary00 = ''; # (String) Boundary string 161 my $v = undef; 162 163 if( $mhead->{'content-type'} ) { 164 # Get the boundary string and set regular expression for matching with 165 # the boundary string. 166 $boundary00 = Sisimai::MIME->boundary($mhead->{'content-type'}); 167 } 168 169 for my $e ( split("\n", $emailsteak->[0]) ) { 170 # Read error messages and delivery status lines from the head of the email 171 # to the previous line of the beginning of the original message. 172 unless( $readcursor ) { 173 # Beginning of the bounce message or message/delivery-status part 174 if( $e =~ $markingsof->{'message'} ) { 175 $readcursor |= $indicators->{'deliverystatus'}; 176 next unless $e =~ $markingsof->{'frozen'}; 177 } 178 } 179 next unless $readcursor & $indicators->{'deliverystatus'}; 180 next unless length $e; 181 182 # This message was created automatically by mail delivery software. 183 # 184 # A message that you sent could not be delivered to one or more of its 185 # recipients. This is a permanent error. The following address(es) failed: 186 # 187 # kijitora@example.jp 188 # SMTP error from remote mail server after RCPT TO:<kijitora@example.jp>: 189 # host neko.example.jp [192.0.2.222]: 550 5.1.1 <kijitora@example.jp>... User Unknown 190 $v = $dscontents->[-1]; 191 192 if( $e =~ /\A[ \t]{2}([^ \t]+[@][^ \t]+[.]?[a-zA-Z]+)(:.+)?\z/ || 193 $e =~ /\A[ \t]{2}[^ \t]+[@][^ \t]+[.][a-zA-Z]+[ ]<(.+?[@].+?)>:.+\z/ || 194 $e =~ $markingsof->{'alias'} ) { 195 # kijitora@example.jp 196 # sabineko@example.jp: forced freeze 197 # mikeneko@example.jp <nekochan@example.org>: ... 198 # 199 # deliver.c:4549| printed = US"an undisclosed address"; 200 # an undisclosed address 201 # (generated from kijitora@example.jp) 202 my $r = $1; 203 204 if( $v->{'recipient'} ) { 205 # There are multiple recipient addresses in the message body. 206 push @$dscontents, __PACKAGE__->DELIVERYSTATUS; 207 $v = $dscontents->[-1]; 208 } 209 210 if( $e =~ /\A[ \t]+[^ \t]+[@][^ \t]+[.][a-zA-Z]+[ ]<(.+?[@].+?)>:.+\z/ ) { 211 # parser.c:743| while (bracket_count-- > 0) if (*s++ != '>') 212 # parser.c:744| { 213 # parser.c:745| *errorptr = s[-1] == 0 214 # parser.c:746| ? US"'>' missing at end of address" 215 # parser.c:747| : string_sprintf("malformed address: %.32s may not follow %.*s", 216 # parser.c:748| s-1, (int)(s - US mailbox - 1), mailbox); 217 # parser.c:749| goto PARSE_FAILED; 218 # parser.c:750| } 219 $r = $1; 220 $v->{'diagnosis'} = $e; 221 } 222 $v->{'recipient'} = $r; 223 $recipients++; 224 225 } elsif( $e =~ /\A[ ]+[(]generated[ ]from[ ](.+)[)]\z/ || 226 $e =~ /\A[ ]+generated[ ]by[ ]([^ \t]+[@][^ \t]+)/ ) { 227 # (generated from kijitora@example.jp) 228 # pipe to |/bin/echo "Some pipe output" 229 # generated by userx@myhost.test.ex 230 $v->{'alias'} = $1; 231 232 } else { 233 next unless length $e; 234 235 if( $e =~ $markingsof->{'frozen'} ) { 236 # Message *** has been frozen by the system filter. 237 # Message *** was frozen on arrival by ACL. 238 $v->{'alterrors'} .= $e.' '; 239 240 } else { 241 if( $boundary00 ) { 242 # --NNNNNNNNNN-eximdsn-MMMMMMMMMM 243 # Content-type: message/delivery-status 244 # ... 245 if( Sisimai::RFC1894->match($e) ) { 246 # $e matched with any field defined in RFC3464 247 next unless my $o = Sisimai::RFC1894->field($e); 248 249 if( $o->[-1] eq 'addr' ) { 250 # Final-Recipient: rfc822;|/bin/echo "Some pipe output" 251 next unless $o->[0] eq 'final-recipient'; 252 $v->{'spec'} ||= rindex($o->[2], '@') > -1 ? 'SMTP' : 'X-UNIX'; 253 254 } elsif( $o->[-1] eq 'code' ) { 255 # Diagnostic-Code: SMTP; 550 5.1.1 <userunknown@example.jp>... User Unknown 256 $v->{'spec'} = uc $o->[1]; 257 $v->{'diagnosis'} = $o->[2]; 258 259 } else { 260 # Other DSN fields defined in RFC3464 261 next unless exists $fieldtable->{ $o->[0] }; 262 $v->{ $fieldtable->{ $o->[0] } } = $o->[2]; 263 } 264 } else { 265 # Error message ? 266 next if $nextcursor; 267 # Content-type: message/delivery-status 268 $nextcursor = 1 if index($e, $startingof->{'deliverystatus'}) == 0; 269 $v->{'alterrors'} .= $e.' ' if index($e, ' ') == 0; 270 } 271 } else { 272 if( scalar @$dscontents == $recipients ) { 273 # Error message 274 next unless length $e; 275 $v->{'diagnosis'} .= $e.' '; 276 277 } else { 278 # Error message when email address above does not include '@' 279 # and domain part. 280 if( $e =~ m<\A[ ]+pipe[ ]to[ ][|]/[^ ]+> ) { 281 # pipe to |/path/to/prog ... 282 # generated by kijitora@example.com 283 $v->{'diagnosis'} = $e; 284 285 } else { 286 next unless index($e, ' ') == 0; 287 $v->{'alterrors'} .= $e.' '; 288 } 289 } 290 } 291 } 292 } 293 } 294 295 if( $recipients ) { 296 # Check "an undisclosed address", "unroutable address" 297 for my $q ( @$dscontents ) { 298 # Replace the recipient address with the value of "alias" 299 next unless $q->{'alias'}; 300 if( ! $q->{'recipient'} || rindex($q->{'recipient'}, '@') == -1 ) { 301 # The value of "recipient" is empty or does not include "@" 302 $q->{'recipient'} = $q->{'alias'}; 303 } 304 } 305 } else { 306 # Fallback for getting recipient addresses 307 if( defined $mhead->{'x-failed-recipients'} ) { 308 # X-Failed-Recipients: kijitora@example.jp 309 my @rcptinhead = split(',', $mhead->{'x-failed-recipients'}); 310 for my $e ( @rcptinhead ) { $e =~ s/\A[ ]+//; $e =~ s/[ ]+\z// } 311 $recipients = scalar @rcptinhead; 312 313 for my $e ( @rcptinhead ) { 314 # Insert each recipient address into @$dscontents 315 $dscontents->[-1]->{'recipient'} = $e; 316 next if scalar @$dscontents == $recipients; 317 push @$dscontents, __PACKAGE__->DELIVERYSTATUS; 318 } 319 } 320 } 321 return undef unless $recipients; 322 323 if( scalar @{ $mhead->{'received'} } ) { 324 # Get the name of local MTA 325 # Received: from marutamachi.example.org (c192128.example.net [192.0.2.128]) 326 $localhost0 = $1 if $mhead->{'received'}->[-1] =~ /from[ \t]([^ ]+) /; 327 } 328 329 for my $e ( @$dscontents ) { 330 # Set default values if each value is empty. 331 $e->{'lhost'} ||= $localhost0; 332 333 if( ! $e->{'diagnosis'} && length($boundary00) > 0 ) { 334 # Empty Diagnostic-Code: or error message 335 # --NNNNNNNNNN-eximdsn-MMMMMMMMMM 336 # Content-type: message/delivery-status 337 # 338 # Reporting-MTA: dns; the.local.host.name 339 # 340 # Action: failed 341 # Final-Recipient: rfc822;/a/b/c 342 # Status: 5.0.0 343 # 344 # Action: failed 345 # Final-Recipient: rfc822;|/p/q/r 346 # Status: 5.0.0 347 $e->{'diagnosis'} = $dscontents->[0]->{'diagnosis'} || ''; 348 $e->{'spec'} ||= $dscontents->[0]->{'spec'}; 349 350 if( $dscontents->[0]->{'alterrors'} ) { 351 # The value of "alterrors" is also copied 352 $e->{'alterrors'} = $dscontents->[0]->{'alterrors'}; 353 } 354 } 355 356 if( exists $e->{'alterrors'} && $e->{'alterrors'} ) { 357 # Copy alternative error message 358 $e->{'diagnosis'} ||= $e->{'alterrors'}; 359 360 if( index($e->{'diagnosis'}, '-') == 0 || substr($e->{'diagnosis'}, -2, 2) eq '__' ) { 361 # Override the value of diagnostic code message 362 $e->{'diagnosis'} = $e->{'alterrors'} if $e->{'alterrors'}; 363 364 } else { 365 # Check the both value and try to match 366 if( length($e->{'diagnosis'}) < length($e->{'alterrors'}) ) { 367 # Check the value of alterrors 368 my $rxdiagnosis = qr/\Q$e->{'diagnosis'}\E/i; 369 if( $e->{'alterrors'} =~ $rxdiagnosis ) { 370 # Override the value of diagnostic code message because 371 # the value of alterrors includes the value of diagnosis. 372 $e->{'diagnosis'} = $e->{'alterrors'}; 373 } 374 } 375 } 376 delete $e->{'alterrors'}; 377 } 378 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'}); 379 $e->{'diagnosis'} =~ s/\b__.+\z//; 380 381 unless( $e->{'rhost'} ) { 382 # Get the remote host name 383 # host neko.example.jp [192.0.2.222]: 550 5.1.1 <kijitora@example.jp>... User Unknown 384 $e->{'rhost'} = $1 if $e->{'diagnosis'} =~ /host[ \t]+([^ \t]+)[ \t]\[.+\]:[ \t]/; 385 386 if( ! $e->{'rhost'} && scalar @{ $mhead->{'received'} } ) { 387 # Get localhost and remote host name from Received header. 388 my $r0 = $mhead->{'received'}; 389 $e->{'rhost'} = pop @{ Sisimai::RFC5322->received($r0->[-1]) }; 390 } 391 } 392 393 unless( $e->{'command'} ) { 394 # Get the SMTP command name for the session 395 SMTP: for my $r ( @$recommands ) { 396 # Verify each regular expression of SMTP commands 397 next unless $e->{'diagnosis'} =~ $r; 398 $e->{'command'} = uc $1; 399 last; 400 } 401 402 # Detect the reason of bounce 403 if( $e->{'command'} eq 'HELO' || $e->{'command'} eq 'EHLO' ) { 404 # HELO | Connected to 192.0.2.135 but my name was rejected. 405 $e->{'reason'} = 'blocked'; 406 407 } elsif( $e->{'command'} eq 'MAIL' ) { 408 # MAIL | Connected to 192.0.2.135 but sender was rejected. 409 # $e->{'reason'} = 'rejected'; 410 $e->{'reason'} = 'onhold'; 411 412 } else { 413 # Verify each regular expression of session errors 414 SESSION: for my $r ( keys %$messagesof ) { 415 # Check each regular expression 416 next unless grep { index($e->{'diagnosis'}, $_) > -1 } @{ $messagesof->{ $r } }; 417 $e->{'reason'} = $r; 418 last; 419 } 420 421 unless( $e->{'reason'} ) { 422 # The reason "expired" 423 $e->{'reason'} = 'expired' if grep { index($e->{'diagnosis'}, $_) > -1 } @$delayedfor; 424 } 425 } 426 } 427 428 STATUS: { 429 # Prefer the value of smtp reply code in Diagnostic-Code: 430 # See set-of-emails/maildir/bsd/exim-20.eml 431 # Action: failed 432 # Final-Recipient: rfc822;userx@test.ex 433 # Status: 5.0.0 434 # Remote-MTA: dns; 127.0.0.1 435 # Diagnostic-Code: smtp; 450 TEMPERROR: retry timeout exceeded 436 # The value of "Status:" indicates permanent error but the value 437 # of SMTP reply code in Diagnostic-Code: field is "TEMPERROR"!!!! 438 my $sv = Sisimai::SMTP::Status->find($e->{'diagnosis'}) || ''; 439 my $rv = Sisimai::SMTP::Reply->find($e->{'diagnosis'}) || ''; 440 my $s1 = 0; # First character of Status as integer 441 my $r1 = 0; # First character of SMTP reply code as integer 442 my $v1 = 0; 443 444 FIND_CODE: while(1) { 445 # "Status:" field did not exist in the bounce message 446 last if $sv; 447 last unless $rv; 448 449 # Check SMTP reply code 450 # Generate pseudo DSN code from SMTP reply code 451 $r1 = substr($rv, 0, 1); 452 if( $r1 == 4 ) { 453 # Get the internal DSN(temporary error) 454 $sv = Sisimai::SMTP::Status->code($e->{'reason'}, 1) || ''; 455 456 } elsif( $r1 == 5 ) { 457 # Get the internal DSN(permanent error) 458 $sv = Sisimai::SMTP::Status->code($e->{'reason'}, 0) || ''; 459 } 460 last; 461 } 462 463 $s1 = substr($sv, 0, 1) if $sv; 464 $v1 = $s1 + $r1; 465 $v1 += substr($e->{'status'}, 0, 1) if $e->{'status'}; 466 467 if( $v1 > 0 ) { 468 # Status or SMTP reply code exists 469 # Set pseudo DSN into the value of "status" accessor 470 $e->{'status'} = $sv if $r1 > 0; 471 472 } else { 473 # Neither Status nor SMTP reply code exist 474 if( $e->{'reason'} eq 'expired' || $e->{'reason'} eq 'mailboxfull' ) { 475 # Set pseudo DSN (temporary error) 476 $sv = Sisimai::SMTP::Status->code($e->{'reason'}, 1) || ''; 477 478 } else { 479 # Set pseudo DSN (permanent error) 480 $sv = Sisimai::SMTP::Status->code($e->{'reason'}, 0) || ''; 481 } 482 } 483 $e->{'status'} ||= $sv; 484 } 485 $e->{'command'} ||= ''; 486 } 487 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] }; 488} 489 4901; 491__END__ 492 493=encoding utf-8 494 495=head1 NAME 496 497Sisimai::Lhost::Exim - bounce mail parser class for C<Exim>. 498 499=head1 SYNOPSIS 500 501 use Sisimai::Lhost::Exim; 502 503=head1 DESCRIPTION 504 505Sisimai::Lhost::Exim parses a bounce email which created by C<Exim>. 506Methods in the module are called from only Sisimai::Message. 507 508=head1 CLASS METHODS 509 510=head2 C<B<description()>> 511 512C<description()> returns description string of this module. 513 514 print Sisimai::Lhost::Exim->description; 515 516=head2 C<B<make(I<header data>, I<reference to body string>)>> 517 518C<make()> method parses a bounced email and return results as a array reference. 519See Sisimai::Message for more details. 520 521=head1 AUTHOR 522 523azumakuniyuki 524 525=head1 COPYRIGHT 526 527Copyright (C) 2014-2020 azumakuniyuki, All rights reserved. 528 529=head1 LICENSE 530 531This software is distributed under The BSD 2-Clause License. 532 533=cut 534