From fc7531131a7ccb7176f80ead865eee12b97a164c Mon Sep 17 00:00:00 2001
From: Jonathan McDowell <noodles@earth.li>
Date: Tue, 24 Apr 2012 21:15:38 -0700
Subject: [PATCH] Avoid race condition when receiving incoming mails

  There's a race condition between us starting to accept a new incoming
  mail and taking the lock to start processing it; a second copy of
  onak-mail may come in and start to process the incomplete mail we're
  in the process of receiving. Receive to a tmp file and rename to .onak
  after we've received everything.

  Fixes Debian bug #650557. Thanks to Helmut Grohne <helmut@subdivi.de>
---
 onak-mail.pl.in | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/onak-mail.pl.in b/onak-mail.pl.in
index 77a3d36..96fce71 100644
--- a/onak-mail.pl.in
+++ b/onak-mail.pl.in
@@ -212,11 +212,12 @@ my $tmpfile = sprintf "%s/%04d%02d%02d-%02d%02d%02d-%d.onak",
 			$time[1],
 			$time[0],
 			$$;
-open(MAILFILE, '>'.$tmpfile);
+open(MAILFILE, '>'.$tmpfile.'.tmp');
 while (<>) {
 	print MAILFILE $_;
 }
 close(MAILFILE);
+rename $tmpfile.".tmp", $tmpfile;
 
 #
 # Lock here to ensure that only one copy of us is processing the incoming
-- 
2.39.5