#!/usr/local/bin/perl $debug=0; $domainname="mns.net.au"; $hostname ="mail.".$domainname; $state=0; $address=""; $offender=""; $message=""; $mode=1; while () { print $_ if ($debug>1); if ($state==0) { # looking for content type or end of headers if (/^Content-Type: /) { ($junk, $boundary, $junk2)=split(/\"/); $boundary="--".$boundary; printf "boundary=%s\n",$boundary; } elsif (/^Subject: /) { if (/ (.*)/) { $subject=$1; chomp($subject); if ($subject =~ /^To: /i) { $subject =~ s/^To: /To: /i; $mode=2; } } } if (/^$/) { $state=1; printf "State=1\n" if $debug; } } elsif ($state >= 1) { # looking for boundary if (/^$boundary$/m) { $state=2; printf "state=2\n" if $debug; } } if ($state==2) { if (/^Content-Type: message/) { $state=3; printf "state=3\n" if $debug; } } elsif ($state==3) { if (/^$/) { $state=4; printf "state=4\n" if $debug; } } elsif ($state==4) { if (/^$/) { $state=5; printf "state=5\n" if $debug; } elsif (/^To: /) { if ($mode==1) { if (/<(.*)>/) { $address=$1; } elsif (/ (.*)/) { $address=$1; chomp($address); } } elsif ($mode==2) { $_ = $subject."\n"; } } elsif (/^From: /) { if (/<(.*)>/) { $offender=$1; } elsif (/ (.*)/) { $offender=$1; chomp($offender); } } $message .= $_; } elsif ($state==5) { $message .= $_; } } # while STDIN $time=localtime(); if ($mode==2) { $offender=substr($subject, 3); } if ($debug) { printf "Offender: %s\nAddress: %s\n",$offender, $address; open (MAIL, ">/dev/tty"); } else { open (MAIL, "|-") || exec ("/usr/bin/sendmail", "-t", "-oi") or die("Can't access sendmail"); } if ($mode==1) { printf MAIL "From MAILER-DAEMON\@%s %s\n", $hostname, $time; print MAIL "Return-Path: MAILER-DAEMON\n", "From: Mail Delivery Subsystem \n", "Subject: Returned mail: User unknown\n"; printf MAIL "To: %s\n", $offender; print MAIL "MIME-Version: 1.0\n", "Content-Type: multipart/report; report-type=delivery-status;\n", " boundary=\"MAA21237.910759472/$domainname\"\n", "Auto-Submitted: auto-generated (failure)\n", "\n", "This is a MIME-encapsulated message\n", "\n", "--MAA21237.910759472/$domainname\n", "\n", " ----- The following addresses have delivery notifications -----\n"; printf MAIL "%s (unrecoverable error)\n\n", $address; print MAIL " ----- Transcript of session follows -----\n", "... while talking to $hostname.:\n\n"; printf MAIL ">>> RCPT To:<%s>\n", $address; printf MAIL "<<< 550 <%s>... User unknown\n", $address; printf MAIL "550 %s... User unknown\n", $address; print MAIL "\n", "--MAA21237.910759472/$domainname\n", "Content-Type: message/delivery-status\n", "\n", "Reporting-MTA: dns; $hostname\n", "\n"; printf MAIL "Final-Recipient: RFC822; %s\n", $address; print MAIL "Action: failed\n", "Status: 5.2.0\n", "Remote-MTA: DNS; $hostname\n"; printf MAIL "Diagnostic-Code: SMTP; 550 <%s>... User unknown\n", $address; print MAIL "\n", "--MAA21237.910759472/$domainname\n", "Content-Type: message/rfc822\n\n"; } print MAIL $message; if ($mode==1) { print MAIL "\n", "--MAA21237.910759472/$domainname--\n"; } close(MAIL);