4 # Return a doubly-bounced e-mail to postmaster. Specific to sendmail,
5 # updated to work on sendmail 8.12.6.
7 # Based on the original doublebounce.pl code by jr@terra.net, 12/4/97.
8 # Updated by bicknell@ufp.org, 12/4/2002 to understand new sendmail DSN
9 # bounces. Code cleanup also performed, mainly making things more
12 # Original intro included below, lines with ##
13 ## attempt to return a doubly-bounced email to a postmaster
14 ## jr@terra.net, 12/4/97
16 ## invoke by creating an mail alias such as:
17 ## doublebounce: "|/usr/local/sbin/doublebounce"
18 ## then adding this line to your sendmail.cf:
19 ## O DoubleBounceAddress=doublebounce
21 ## optionally, add a "-d" flag in the aliases file, to send a
22 ## debug trace to your own postmaster showing what is going on
24 ## this allows the "postmaster" address to still go to a human being,
25 ## while bounce messages can go to this script, which will bounce them
26 ## back to the postmaster at the sending site.
28 ## the algorithm is to scan the double-bounce error report generated
29 ## by sendmail on stdin, for the original message (it starts after the
30 ## second "Orignal message follows" marker), look for From, Sender, and
31 ## Received headers from the point closest to the sender back to the point
32 ## closest to us, and try to deliver a double-bounce report back to a
33 ## postmaster at one of these sites in the hope that they can
34 ## return the message to the original sender, or do something about
35 ## the fact that that sender's return address is not valid.
40 use Sys::Syslog qw(:DEFAULT setlogsock);
42 use vars qw( $opt_d $tmpfile);
45 # parse hostname from From: header
52 ($addr) = $hdr =~ m/<(.*)>/;
56 if ($addr =~ /\s*\(/) {
57 ($addr) = $hdr =~ m/\s*(.*)\s*\(/;
61 ($addr) = $hdr =~ m/\s*(.*)\s*/;
67 # send bounce to postmaster
69 # this re-invokes sendmail in immediate and quiet mode to try
70 # to deliver to a postmaster. sendmail's exit status tells us
71 # whether the delivery attempt really was successful.
74 my($addr, $from) = @_;
78 my($dest) = "postmaster\@" . parseaddr($addr);
81 syslog ('info', "Attempting to send to user $dest");
83 open(MAIL, "| /usr/sbin/sendmail -oeq $dest");
85 From: Mail Delivery Subsystem <mail-router>
86 Subject: Postmaster notify: double bounce
90 Auto-Submitted: auto-generated (postmaster notification)
92 The following message was received for an invalid recipient. The
93 sender's address was also invalid. Since the message originated
94 at or transited through your mailer, this notification is being
95 sent to you in the hope that you will determine the real originator
96 and have them correct their From or Sender address.
98 The from header on the original e-mail was: $from.
100 ----- The following is a double bounce -----
104 open(MSG, "<$tmpfile");
107 $result = close(MAIL);
109 syslog('info', 'doublebounce successfully sent to %s', $dest);
115 # Get our command line options
120 openlog('doublebounce', 'pid', 'mail');
123 syslog('info', 'Processing a doublebounce.');
126 # The bounced e-mail may be large, so we'd better not try to buffer
127 # it in memory, get a temporary file.
130 if (!open(MSG, ">$tmpfile")) {
131 syslog('err', "Unable to open temporary file $tmpfile");
132 exit(75); # 75 is a temporary failure, sendmail should retry
136 if (!open(MSG, "<$tmpfile")) {
137 syslog('err', "Unable to reopen temporary file $tmpfile");
138 exit(74); # 74 is an IO error
141 # Ok, now we can get down to business, find the original message
142 my($skip_lines, $in_header, $headers_found, @addresses);
147 if ($skip_lines > 0) {
152 # Starting message depends on your version of sendmail
153 if (/^ ----- Original message follows -----$/ ||
154 /^ ----Unsent message follows----$/ ||
155 /^Content-Type: message\/rfc822$/) {
156 # Found the original message
163 if ($headers_found >= 2) {
164 # We only process two deep, even if there are more
168 # We've found the end of a header, scan for the next one
176 if (/^(received): (.*)/i ||
177 /^(reply-to): (.*)/i ||
178 /^(sender): (.*)/i ||
180 $addresses[$headers_found]{$1} = $2;
184 # continuation header
185 # we should really process these, but we don't yet
189 # Nothing to do if we're not in a header
195 # Start with the original (inner) sender
197 foreach $addr (keys %{$addresses[2]}) {
199 syslog('info', "Trying to send to $addresses[2]{$addr} - $addresses[2]{\"From\"}");
201 $sent = send_bounce($addresses[2]{$addr}, $addresses[2]{"From"});
204 if (!$sent && $opt_d) {
206 syslog('info', 'Unable to find original sender, falling back.');
208 foreach $addr (keys %{$addresses[1]}) {
210 syslog('info', "Trying to send to $addresses[2]{$addr} - $addresses[2]{\"From\"}");
212 $sent = send_bounce($addresses[1]{$addr}, $addresses[2]{"From"});
216 syslog('info', 'Unable to find anyone to send a doublebounce notification');