How can I completely replace the headers in a milter? The replaced header data contains both added and changed headers. Here is the code im working with:
sub eom_callback {
$ctx = shift;
$senderunsafe = $ctx->getsymval('{mail_addr}');
($suser, $sdomain) = split("\@", $senderunsafe);
$suser =~ s/[^A-Z0-9._%+-]*//sgi;
$sdomain =~ s/[^A-Z0-9.-]*//sgi;
if ((length($suser) > 0)&&($sdomain eq "sebbe.eu")) { #Ensure we only
touch mail that are outgoing (relayed) by authorized users.
$mimedata = $ctx->getpriv;
# Some MUAs does put line breaks and other nonpermitted
whitespace in their boundary strings while they appear in the message body.
# That is incompatible with RFC 2046. But still, we need to fix
that because MIME::Parser cannot parse messages containing boundaries with
whitespace.
for ($mimedata =~ /Content-Type: .*?boundary=\"(.*?)\"/sg) {
$cbound = $_;
$fixed = $cbound;
$fixed =~ s/\t//sgi;
$fixed =~ s/\n//sgi;
$fixed =~ s/\r//sgi;
# $fixed now contains a fixed boundary. Fix the email
message by removing nonpermitted whitespace from the message boundaries.
$mimedata =~ s/\-\-$cbound/\-\-$fixed/sgi;
}
$parser = new MIME::Parser;
$parser->output_under("/tmp");
$parser->ignore_errors(1); # If MIME parser has trouble parsing
a badly formatted message, try hard.
$message = eval{ $parser->parse($mimedata) }; # If parser
crashes due to bad MIME data, the whole milter wont die.
eval{ $message->sync_headers(Length=>'COMPUTE') }; #Some MUA's
cant count bytes correctly. Fix that.
$fixedmsgheader = eval{ $message->header_as_string };
$fixedmsgbody = eval{ $message->body_as_string };
if ((length($fixedmsgheader) > 0)&&(length($fixedmsgbody) > 0))
{
}
else
{
#Parsing failed completely. This MIME message was really broken.
#Lets build a completely new message from scratch and then put
the erronous MIME data as a text/plain body.
#Note that $senderunsafe is safe in this context because we
dont call system functions with $senderunsafe as parameter.
#Note that we will only use the last recipient specified during
the SMTP transaction. Thats for durability,
#If the MIME data is unparseable, theres a high propability the
client can't talk proper SMTP either.
$scratch = MIME::Entity->build(Type => "text/plain",
Encoding => "quoted-printable",
Data => $mimedata,
From => $senderunsafe,
To => $ctx->getsymval('{rcpt_addr}'),
Subject => "Unparseable MIME message from
".$senderunsafe);
$fixedmsgheader = $scratch->header_as_string;
$fuxedmsgbody = $scratch->body_as_string;
}
#Here, I want to replace the complete header with
$fixedmsgheader. Note that $fixedmsgheader can contain BOTH added and changed
headers.
#Change the body with fixed content:
$ctx->replacebody($fixedmsgbody);
}
return(SMFIS_CONTINUE);
}
smime.p7s
Description: S/MIME cryptographic signature
