Module: Sisimai::Lhost::Exim

Defined in:
lib/sisimai/lhost/exim.rb

Overview

Sisimai::Lhost::Exim decodes a bounce email which created by Exim Internet Mailer www.exim.org/. Methods in the module are called from only Sisimai::Message.

Constant Summary collapse

Indicators =
Sisimai::Lhost.INDICATORS
FieldTable =
Sisimai::RFC1894.FIELDTABLE
Boundaries =
[
  # deliver.c:6423|          if (bounce_return_body) fprintf(f,
  # deliver.c:6424|"------ This is a copy of the message, including all the headers. ------\n");
  # deliver.c:6425|          else fprintf(f,
  # deliver.c:6426|"------ This is a copy of the message's headers. ------\n");
  "------ This is a copy of the message, including all the headers. ------",
  "Content-Type: message/rfc822",
  "Included is a copy of the message header:\n-----------------------------------------", # MXLogic
].freeze
EmailTitle =
[
  "Delivery Status Notification",
  "Mail delivery failed",
  "Mail failure",
  "Message frozen",
  "Warning: message ",
  "error(s) in forwarding or filtering",
].freeze
StartingOf =
{
  # Error text strings which defined in exim/src/deliver.c
  #
  # deliver.c:6292| fprintf(f,
  # deliver.c:6293|"This message was created automatically by mail delivery software.\n");
  # deliver.c:6294|        if (to_sender)
  # deliver.c:6295|          {
  # deliver.c:6296|          fprintf(f,
  # deliver.c:6297|"\nA message that you sent could not be delivered to one or more of its\n"
  # deliver.c:6298|"recipients. This is a permanent error. The following address(es) failed:\n");
  # deliver.c:6299|          }
  # deliver.c:6300|        else
  # deliver.c:6301|          {
  # deliver.c:6302|          fprintf(f,
  # deliver.c:6303|"\nA message sent by\n\n  <%s>\n\n"
  # deliver.c:6304|"could not be delivered to one or more of its recipients. The following\n"
  # deliver.c:6305|"address(es) failed:\n", sender_address);
  # deliver.c:6306|          }
  "alias"          => [" an undisclosed address"],
  "command"        => ["SMTP error from remote ", "LMTP error after "],
  "deliverystatus" => ["Content-Type: message/delivery-status"],
  "frozen"         => [" has been frozen", " was frozen on arrival"],
  "message"        => [
    "This message was created automatically by mail delivery software.",
    "A message that you sent was rejected by the local scannning code",
    "A message that you sent contained one or more recipient addresses ",
    "A message that you sent could not be delivered to all of its recipients",
    " has been frozen",
    " was frozen on arrival",
    " router encountered the following error(s):",
  ],
}.freeze
MessagesOf =
{
  # find exim/ -type f -exec grep 'message = US' {} /dev/null \;
  # route.c:1158|  DEBUG(D_uid) debug_printf("getpwnam() returned NULL (user not found)\n");
  "userunknown" => ["user not found"],
  # parser.c:666| *errorptr = string_sprintf("%s (expected word or \"<\")", *errorptr);
  # parser.c:701| if(bracket_count++ > 5) FAILED(US"angle-brackets nested too deep");
  # parser.c:738| FAILED(US"domain missing in source-routed address");
  # parser.c:747| : string_sprintf("malformed address: %.32s may not follow %.*s",
  "syntaxerror" => [
    "angle-brackets nested too deep",
    'expected word or "<"',
    "domain missing in source-routed address",
    "malformed address:",
  ],
}.freeze
DelayedFor =
[
  # deliver.c:7475| "No action is required on your part. Delivery attempts will continue for\n"
  # smtp.c:3508|    US"retry time not reached for any host after a long failure period" :
  # deliver.c:7459| print_address_error(addr, f, US"Delay reason: ");
  "No action is required on your part",
  "retry time not reached for any host after a long failure period",
  "Delay reason: ",
].freeze

Class Method Summary collapse

Class Method Details

.descriptionObject



433
# File 'lib/sisimai/lhost/exim.rb', line 433

def description; return "Exim"; end

.inquire(mhead, mbody) ⇒ Hash, Nil

This method is abstract.

Decodes the bounce message from Exim

Parameters:

  • mhead (Hash)

    Message headers of a bounce email

  • mbody (String)

    Message body of a bounce email

Returns:

  • (Hash)

    Bounce data list and message/rfc822 part

  • (Nil)

    it failed to decode or the arguments are missing



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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/sisimai/lhost/exim.rb', line 89

def inquire(mhead, mbody)
  # Message-Id: <E1P1YNN-0003AD-Ga@example.org>
  # X-Failed-Recipients: kijitora@example.ed.jp
  return nil if Sisimai::Lhost.BannerDTAG.any? { |a| mbody.include?(a) }

  thirdparty = false
  proceedsto = 0
  messageidv = mhead["message-id"] || ""

  proceedsto += 1 if mhead["from"].include?("Mail Delivery System")
  while messageidv != "" do
    # Message-Id: <E1P1YNN-0003AD-Ga@example.org>
    break if messageidv.index("<") !=  0
    break if messageidv.index("-") !=  8
    break if messageidv.index('@') != 18
    proceedsto += 1; break
  end
  EmailTitle.each do |e|
    # Subject: Mail delivery failed: returning message to sender
    # Subject: Mail delivery failed
    # Subject: Message frozen
    next if mhead["subject"].include?(e) == false
    proceedsto += 1; break
  end

  while true
    # Exim clones of the third Parties
    # 1. McAfee Saas (Formerly MXLogic)
    if mhead.has_key?("x-mx-bounce")    then thirdparty = true; break; end
    if mhead.has_key?("x-mxl-hash")     then thirdparty = true; break; end
    if mhead.has_key?("x-mxl-notehash") then thirdparty = true; break; end
    if messageidv.include?("<mxl~")     then thirdparty = true; break; end
    break
  end
  return nil if proceedsto < 2 && thirdparty == false

  require "sisimai/reason"
  require "sisimai/address"
  require "sisimai/rfc2045"
  require "sisimai/smtp/command"
  require "sisimai/smtp/failure"

  dscontents = [Sisimai::Lhost.DELIVERYSTATUS]; v = nil
  emailparts = Sisimai::RFC5322.part(mbody, Boundaries)
  bodyslices = emailparts[0].split("\n")
  readcursor = 0      # (Integer) Points the current cursor position
  nextcursor = false
  recipients = 0      # (Integer) The number of 'Final-Recipient' header
  boundary00 = ""     # (String) Boundary string

  if mhead["content-type"]
    # Get the boundary string and set regular expression for matching with the boundary string.
    boundary00 = Sisimai::RFC2045.boundary(mhead["content-type"]) || ""
  end

  p1 = -1; p2 = -1;
  while e = bodyslices.shift do
    # Read error messages and delivery status lines from the head of the email to the previous
    # line of the beginning of the original message.
    if readcursor == 0
      # Beginning of the bounce message or message/delivery-status part
      if StartingOf["message"].any? { |a| e.include?(a) }
        # Check the message defined in StartingOf["message"], ["frozen"]
        readcursor |= Indicators[:deliverystatus]
        next if StartingOf["frozen"].none? { |a| e.include?(a) }
      end
    end
    next if (readcursor & Indicators[:deliverystatus]) == 0 || e.empty?

    # This message was created automatically by mail delivery software.
    #
    # A message that you sent could not be delivered to one or more of its
    # recipients. This is a permanent error. The following address(es) failed:
    #
    #  kijitora@example.jp
    #    SMTP error from remote mail server after RCPT TO:<kijitora@example.jp>:
    #    host neko.example.jp [192.0.2.222]: 550 5.1.1 <kijitora@example.jp>... User Unknown
    v = dscontents[-1]

    cv = ''
    ce = false
    while true
      # Check if the line matche the following patterns:
      break if e.start_with?("  ") == false       # The line should start with "  " (2 spaces)
      break if e.include?('@')     == false       # "@" should be included (email)
      break if e.include?(".")     == false       # "." should be included (domain part)
      break if e.include?("pipe to |")            # Exclude "pipe to /path/to/prog" line

      cx = e[2, 1]
      break if cx == " "                          # The 3rd character is " "
      break if thirdparty == false && cx == "<"   # MXLogic returns "  <neko@example.jp>:..."
      ce = true
      break
    end

    if ce == true || e.include?(StartingOf["alias"][0])
      # The line is including an email address
      if v["recipient"] != ""
        # There are multiple recipient addresses in the message body.
        dscontents << Sisimai::Lhost.DELIVERYSTATUS
        v = dscontents[-1]
      end

      if e.include?(StartingOf["alias"][0])
        # The line does not include an email address
        # deliver.c:4549|  printed = US"an undisclosed address";
        #   an undisclosed address
        #     (generated from kijitora@example.jp)
        cv = e[2, e.size]
      else
        #   kijitora@example.jp
        #   sabineko@example.jp: forced freeze
        #   mikeneko@example.jp <nekochan@example.org>: ...
        p1 = e.index("<") || -1
        p2 = e.index(">:") || -1

        if p1 > 1 && p2 > 1
          # There are an email address and an error message in the line
          # parser.c:743| while (bracket_count-- > 0) if (*s++ != '>')
          # parser.c:744|   {
          # parser.c:745|   *errorptr = s[-1] == 0
          # parser.c:746|     ? US"'>' missing at end of address"
          # parser.c:747|     : string_sprintf("malformed address: %.32s may not follow %.*s",
          # parser.c:748|     s-1, (int)(s - US mailbox - 1), mailbox);
          # parser.c:749|   goto PARSE_FAILED;
          # parser.c:750|   }
          cv = Sisimai::Address.s3s4(e[p1, p2 - p1 - 1])
          v["diagnosis"] = e[p2 + 1, e.size]
        else
          # There is an email address only in the line
          #   kijitora@example.jp
          cv = Sisimai::Address.s3s4(e[2, e.size])
        end
      end
      v["recipient"] = cv
      recipients += 1

    elsif e.include?(" (generated from ") || e.include?(" generated by ")
      #     (generated from kijitora@example.jp)
      #  pipe to |/bin/echo "Some pipe output"
      #    generated by userx@myhost.test.ex
      e.split(" ").each do |f|
        # Find the alias address
        next if f.include?('@') == false
        v["alias"] = Sisimai::Address.s3s4(f)
      end
    else
      if StartingOf["frozen"].any? { |a| e.include?(a) }
        # Message *** has been frozen by the system filter.
        # Message *** was frozen on arrival by ACL.
        v["alterrors"] ||= ""
        v["alterrors"]  += "#{e} "
      elsif boundary00 != ""
        # --NNNNNNNNNN-eximdsn-MMMMMMMMMM
        # Content-type: message/delivery-status
        # ...
        if Sisimai::RFC1894.match(e) > 0
          # "e" matched with any field defined in RFC3464
          next unless o = Sisimai::RFC1894.field(e)

          case o[3]
          when "addr"
            # Final-Recipient: rfc822; kijitora@example.jp
            # X-Actual-Recipient: rfc822; kijitora@example.co.jp
            next if o[0] != "final-recipient"
            if v["spec"].empty?
              v["spec"] = o[2].include?('@') ? "SMTP" : "X-UNIX"
            end

          when "code"
            # Diagnostic-Code: SMTP; 550 5.1.1 <userunknown@example.jp>... User Unknown
            v["spec"] = o[1].upcase
            v["diagnosis"] = o[2]

          else
            # Other DSN fields defined in RFC3464
            next if FieldTable[o[0]].nil?
            v[FieldTable[o[0]]] = o[2]
          end
        else
          # Error message ?
          next if nextcursor

          # Content-type: message/delivery-status
          nextcursor = true if e.start_with?(StartingOf["deliverystatus"][0])
          v["alterrors"] ||= ''
          v["alterrors"]  += "#{e} " if e.start_with?(" ")
        end
      else
        # There is no boundary string in $boundary00
        if dscontents.size == recipients
          # Error message
          v["diagnosis"] += "#{e} "
        else
          # Error message when email address above does not include '@' and domain part
          # pipe to |/path/to/prog ...
          #   generated by kijitora@example.com
          next if e.start_with?("  ") == false
          v["diagnosis"] += "#{e} "
        end
      end
    end
  end

  if recipients > 0
    # Check "an undisclosed address", "unroutable address"
    dscontents.each do |q|
      # Replace the recipient address with the value of "alias"
      next if q["alias"].empty?
      if q["recipient"].empty? || q["recipient"].include?('@') == false
        # The value of "recipient" is empty or does not include "@"
        q["recipient"] = q["alias"]
      end
    end
  elsif mhead["x-failed-recipients"]
    # Fallback for getting recipient addresses
    # X-Failed-Recipients: kijitora@example.jp
    rcptinhead = mhead["x-failed-recipients"].split(",")
    rcptinhead.each do |e|
      # Remove space characters
      e.lstrip!
      e.rstrip!
    end
    recipients = rcptinhead.size

    while e = rcptinhead.shift do
      # Insert each recipient address into dscontents
      dscontents[-1]["recipient"] = e
      next if dscontents.size == recipients
      dscontents << Sisimai::Lhost.DELIVERYSTATUS
    end
  end
  return nil if recipients == 0

  # Get the name of the local MTA
  # Received: from marutamachi.example.org (c192128.example.net [192.0.2.128])
  receivedby = mhead["received"] || []
  recvdtoken = Sisimai::RFC5322.received(receivedby[-1])

  dscontents.each do |e|
    # Check the error message, the rhost, the lhost, and the smtp command.
    e["alterrors"] = "" if e["alterrors"].nil?
    if e["diagnosis"].empty? && boundary00.size > 0
      # Empty Diagnostic-Code: or error message
      # --NNNNNNNNNN-eximdsn-MMMMMMMMMM
      # Content-type: message/delivery-status
      #
      # Reporting-MTA: dns; the.local.host.name
      #
      # Action: failed
      # Final-Recipient: rfc822;/a/b/c
      # Status: 5.0.0
      #
      # Action: failed
      # Final-Recipient: rfc822;|/p/q/r
      # Status: 5.0.0
      e["diagnosis"] = dscontents[0]["diagnosis"]
      e["spec"]      = dscontents[0]["spec"]      if e["spec"].empty?
      e["alterrors"] = dscontents[0]["alterrors"] if dscontents[0]["alterrors"] != ""
    end

    if e["alterrors"]
      # Copy alternative error message
      e["diagnosis"] = e["alterrors"] if e["diagnosis"].empty?

      if e["diagnosis"].start_with?("-") || e["diagnosis"].end_with?("__")
        # Override the value of diagnostic code message
        e["diagnosis"] = e["alterrors"]

      elsif e["diagnosis"].size < e["alterrors"].size
        # Override the value of diagnostic code message with the value of alterrors because
        # the latter includes the former.
        e["alterrors"].squeeze!(" ")
        e["diagnosis"] = e["alterrors"] if e["alterrors"].downcase.include?(e["diagnosis"].downcase)
      end
      e.delete("alterrors")
    end
    p1 = e["diagnosis"].index("__") || -1
    e["diagnosis"] = e["diagnosis"][0, p1] if p1 > 1

    if e["rhost"].empty?
      # Get the remote host name
      # host neko.example.jp [192.0.2.222]: 550 5.1.1 <kijitora@example.jp>... User Unknown
      p1 = e["diagnosis"].index("host ")     || -1
      p2 = e["diagnosis"].index(" ", p1 + 5) || -1
      e["rhost"] = e["diagnosis"][p1 + 5, p2 - p1 - 5] if p1 > -1
      e["rhost"] = recvdtoken[1] if e["rhost"].empty?
    end
    e["lhost"] = recvdtoken[0] if e["lhost"].empty?

    if e["command"].empty?
      # Get the SMTP command name for the session
      StartingOf["command"].each do |r|
        # Verify each regular expression of SMTP commands
        next if e["diagnosis"].include?(r) == false
        e["command"] = Sisimai::SMTP::Command.find(e["diagnosis"]); next if e["command"].empty?
        break
      end

      # Detect the reason of bounce
      case e["command"]
        # - HELO | Connected to 192.0.2.135 but my name was rejected.
        # - MAIL | Connected to 192.0.2.135 but sender was rejected.
        when "HELO", "EHLO" then e["reason"] = "blocked"
        when "MAIL"         then e["reason"] = "onhold"
      else
        # Try to match the error message with each message pattern 
        MessagesOf.each_key do |r|
          # Check each message pattern
          next if MessagesOf[r].none? { |a| e["diagnosis"].include?(a) }
          e["reason"] = r
          break
        end

        if e["reason"].empty?
          e["reason"] = "expired" if DelayedFor.any? { |a| e["diagnosis"].include?(a) }
        end
      end
    end

    # Prefer the value of smtp reply code in Diagnostic-Code:
    #   Action: failed
    #   Final-Recipient: rfc822;userx@test.ex
    #   Status: 5.0.0
    #   Remote-MTA: dns; 127.0.0.1
    #   Diagnostic-Code: smtp; 450 TEMPERROR: retry timeout exceeded
    #
    # The value of "Status:" indicates permanent error but the value of SMTP reply code in
    # Diagnostic-Code: field is "TEMPERROR"!!!!
    cr = Sisimai::SMTP::Reply.find(e["diagnosis"], e["status"])
    cs = Sisimai::SMTP::Status.find(e["diagnosis"], cr)
    re = e["reason"]
    cv = ""

    if Sisimai::SMTP::Failure.is_temporary(cr) || re == "expired"
      # Set the pseudo status code as a temporary error
      cv = Sisimai::SMTP::Status.code(re, true) if Sisimai::Reason.is_explicit(re)
    end
    e["replycode"] = cr if e["replycode"].empty?
    e["status"]    = Sisimai::SMTP::Status.prefer(cv, cs, cr) if e["status"].empty?
  end

  return { "ds" => dscontents, "rfc822" => emailparts[1] }
end