Refactor for multiple emails per session (#305)

* Refactor for multiple emails per session

* Add close_callback to milter
This commit is contained in:
Shawn Iverson 2018-11-21 16:13:38 -05:00 committed by GitHub
parent e9773ffe44
commit 5521a030ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -165,6 +165,45 @@ sub envrcpt_callback
# Watch for second callback # Watch for second callback
if ( ${$message_ref} !~ /MailScanner Milter/ ) { if ( ${$message_ref} !~ /MailScanner Milter/ ) {
# Is this a subsequent message? Grab previous message id to reconstruct header
if ( ${$message_ref} =~ /^[0-9A-F]{7,20}|[0-9B-DF-HJ-NP-TV-Zb-df-hj-np-tv-z]{8,20}$/ ) {
# Read envelope
my $queuehandle = new FileHandle;
my $incoming = MailScanner::Config::Value('inqueuedir');
MailScanner::Log::DebugLog("eoh_callback: incoming = " . @{$incoming}[0]);
if ($incoming eq '') {
MailScanner::Log::WarnLog("Unable to determine incoming queue!");
Sendmail::PMilter::SMFIS_TEMPFAIL;
return;
}
my $file = @{$incoming}[0] . '/temp-' . ${$message_ref};
my $file2 = @{$incoming}[0] . '/' . ${$message_ref};
MailScanner::Log::DebugLog("envrcpt_callback: file = $file");
my $ret;
$ret = MailScanner::Lock::openlock($queuehandle,'+<' . $file, 'w');
if ($ret != 1) {
MailScanner::Log::WarnLog("Unable to to open temp queue file for reading!");
Sendmail::PMilter::SMFIS_TEMPFAIL;
return;
}
my $data;
my $pos;
# Locate predata header
while($data = readline $queuehandle) {
last if $data !~ /^(O<|S<)/;
}
${$message_ref} = $data;
MailScanner::Lock::unlockclose($queuehandle);
# Done with previous message, make ready for mailscanner
move($file, $file2);
}
# Capture the Mail From address for further processing # Capture the Mail From address for further processing
if (defined($symbols->{'M'}) && defined($symbols->{'M'}->{'{mail_addr}'})) { if (defined($symbols->{'M'}) && defined($symbols->{'M'}->{'{mail_addr}'})) {
$mailfrom=$symbols->{'M'}->{'{mail_addr}'}; $mailfrom=$symbols->{'M'}->{'{mail_addr}'};
@ -388,6 +427,20 @@ sub eom_callback
my $ctx = shift; my $ctx = shift;
my $message_ref = $ctx->getpriv(); my $message_ref = $ctx->getpriv();
# Store reference for subsequent messages in connection or for connection close
$ctx->setpriv($message_ref);
# Send DISCARD signal to accept message and drop from postfix
# for mailscanner processing
Sendmail::PMilter::SMFIS_DISCARD;
}
sub close_callback
{
my $ctx = shift;
my $message_ref = $ctx->getpriv();
my $incoming = MailScanner::Config::Value('inqueuedir'); my $incoming = MailScanner::Config::Value('inqueuedir');
MailScanner::Log::DebugLog("eom_callback: incoming = " . @{$incoming}[0]); MailScanner::Log::DebugLog("eom_callback: incoming = " . @{$incoming}[0]);
if ($incoming eq '') { if ($incoming eq '') {
@ -402,9 +455,7 @@ sub eom_callback
$ctx->setpriv(undef); $ctx->setpriv(undef);
# Send DISCARD signal to accept message and drop from postfix Sendmail::PMilter::SMFIS_CONTINUE;
# for mailscanner processing
Sendmail::PMilter::SMFIS_DISCARD;
} }
# #
@ -512,6 +563,7 @@ BEGIN {
'eoh' => \&eoh_callback, 'eoh' => \&eoh_callback,
'body' => \&body_callback, 'body' => \&body_callback,
'eom' => \&eom_callback, 'eom' => \&eom_callback,
'close' => \&close_callback,
); );
my $conn = 'inet:'. $port . '@' . $bind; my $conn = 'inet:'. $port . '@' . $bind;