Import sendmail 8.13.4 into a new contrib directory as the first step
[dragonfly.git] / contrib / sendmail-8.13.4 / contrib / doublebounce.pl
blobdc26ab84f1a780b8baee4b74a15bdbd3f62190c1
1 #!/usr/bin/perl
2 # doublebounce.pl
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
10 # robust.
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.
37 use Socket;
38 use Getopt::Std;
39 use File::Temp;
40 use Sys::Syslog qw(:DEFAULT setlogsock);
41 use strict;
42 use vars qw( $opt_d $tmpfile);
44 # parseaddr()
45 # parse hostname from From: header
47 sub parseaddr {
48 my($hdr) = @_;
49 my($addr);
51 if ($hdr =~ /<.*>/) {
52 ($addr) = $hdr =~ m/<(.*)>/;
53 $addr =~ s/.*\@//;
54 return $addr;
56 if ($addr =~ /\s*\(/) {
57 ($addr) = $hdr =~ m/\s*(.*)\s*\(/;
58 $addr =~ s/.*\@//;
59 return $addr;
61 ($addr) = $hdr =~ m/\s*(.*)\s*/;
62 $addr =~ s/.*\@//;
63 return $addr;
66 # sendbounce()
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.
73 sub send_bounce {
74 my($addr, $from) = @_;
75 my($st);
76 my($result);
78 my($dest) = "postmaster\@" . parseaddr($addr);
80 if ($opt_d) {
81 syslog ('info', "Attempting to send to user $dest");
83 open(MAIL, "| /usr/sbin/sendmail -oeq $dest");
84 print MAIL <<EOT;
85 From: Mail Delivery Subsystem <mail-router>
86 Subject: Postmaster notify: double bounce
87 Reply-To: nobody
88 Errors-To: nobody
89 Precedence: junk
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");
105 print MAIL <MSG>;
106 close(MSG);
107 $result = close(MAIL);
108 if ($result) {
109 syslog('info', 'doublebounce successfully sent to %s', $dest);
111 return $result;
114 sub main {
115 # Get our command line options
116 getopts('d');
118 # Set up syslog
119 setlogsock('unix');
120 openlog('doublebounce', 'pid', 'mail');
122 if ($opt_d) {
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.
128 $tmpfile = tmpnam();
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
134 print(MSG <STDIN>);
135 close(MSG);
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);
143 $skip_lines = 0;
144 $in_header = 0;
145 $headers_found = 0;
146 while (<MSG>) {
147 if ($skip_lines > 0) {
148 $skip_lines--;
149 next;
151 chomp;
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
157 $skip_lines++;
158 $in_header = 1;
159 $headers_found++;
160 next;
162 if (/^$/) {
163 if ($headers_found >= 2) {
164 # We only process two deep, even if there are more
165 last;
167 if ($in_header) {
168 # We've found the end of a header, scan for the next one
169 $in_header = 0;
171 next;
173 if ($in_header) {
174 if (! /^[ \t]/) {
175 # New Header
176 if (/^(received): (.*)/i ||
177 /^(reply-to): (.*)/i ||
178 /^(sender): (.*)/i ||
179 /^(from): (.*)/i ) {
180 $addresses[$headers_found]{$1} = $2;
182 next;
183 } else {
184 # continuation header
185 # we should really process these, but we don't yet
186 next;
188 } else {
189 # Nothing to do if we're not in a header
190 next;
193 close(MSG);
195 # Start with the original (inner) sender
196 my($addr, $sent);
197 foreach $addr (keys %{$addresses[2]}) {
198 if ($opt_d) {
199 syslog('info', "Trying to send to $addresses[2]{$addr} - $addresses[2]{\"From\"}");
201 $sent = send_bounce($addresses[2]{$addr}, $addresses[2]{"From"});
202 last if $sent;
204 if (!$sent && $opt_d) {
205 if ($opt_d) {
206 syslog('info', 'Unable to find original sender, falling back.');
208 foreach $addr (keys %{$addresses[1]}) {
209 if ($opt_d) {
210 syslog('info', "Trying to send to $addresses[2]{$addr} - $addresses[2]{\"From\"}");
212 $sent = send_bounce($addresses[1]{$addr}, $addresses[2]{"From"});
213 last if $sent;
215 if (!$sent) {
216 syslog('info', 'Unable to find anyone to send a doublebounce notification');
220 unlink($tmpfile);
223 main();
224 exit(0);