SPDX: license tags (mostly by guesswork)
[exim.git] / src / util / chunking_fixqueue_finalnewlines.pl
1 #!/usr/bin/env perl
2 # SPDX-License-Identifier: GPL-2.0-only
3
4 use warnings;
5 use strict;
6 BEGIN { pop @INC if $INC[-1] eq '.' };
7
8 use Fcntl qw(:DEFAULT :flock :seek);
9 use File::Find;
10 use File::Spec;
11
12 use constant MIN_AGE => 60; # seconds
13 my $exim = exists $ENV{'EXIM_BINARY'} ? $ENV{'EXIM_BINARY'} : 'exim';
14
15 my %known_okay = map {$_=>1} qw( linux darwin freebsd );
16 unless (exists $known_okay{$^O}) {
17   warn "for ease, this perl uses flock, not fcntl, assuming they're the same\n";
18   warn "this is not known by this author to be the case on $^O\n";
19   warn "please investigate and either add to allowed-list in script, or rewrite\n";
20   die "bailing out";
21
22   # Another approach to rewriting script: stop all exim receivers and
23   # queue-runners, prevent them from starting, then add your OS to the list and
24   # run, even though the locking type is wrong, relying upon not actually
25   # contending.
26 }
27
28 my $spool_dir = `$exim -n -bP spool_directory`;
29 chomp $spool_dir;
30
31 chdir(File::Spec->catfile($spool_dir, 'input'))
32     or die "chdir($spool_dir/input) failed: $!\n";
33
34 my $exim_msgid_r = qr/(?:[0-9A-Za-z]{6}-[0-9A-Za-z]{6}-[0-9A-Za-z]{2})/;
35 my $spool_dfile_r = qr/^(($exim_msgid_r)-D)\z/o;
36
37 sub fh_ends_newline {
38   my ($fh, $dfn, $verbose) = @_;
39   seek($fh, -1, 2) or do { warn "seek(file($dfn)) failed: $!\n"; return -1 };
40   my $count = read $fh, my $ch, 1;
41   if ($count == -1) { warn "failed to read last byte of $dfn\n"; return -1 };
42   if ($count == 0) { warn "file shrunk by one??  problem with $dfn\n"; return -1 };
43   if ($ch eq "\n") { print "okay!\n" if $verbose; return 1 }
44   print "PROBLEM: $dfn missing final newline (got $ch)\n" if $verbose;
45   return 0;
46 }
47
48
49 sub each_found_file {
50   return unless $_ =~ $spool_dfile_r;
51   my ($msgid, $dfn) = ($2, $1);
52
53   # We should have already upgraded Exim before invoking us, thus any spool
54   # files will be old and we can reduce spending time trying to lock files
55   # still being written to, etc.
56   my @st = lstat($dfn) or return;
57   if ($^T - $st[9] < MIN_AGE) { return };
58   -f "./${msgid}-H" || return;
59
60   print "consider: $dfn\n";
61   open(my $fh, '+<:raw', $dfn) or do {
62     warn "open($dfn) failed: $!\n";
63     return;
64   };
65   # return with a lexical FH in modern Perl should guarantee close, AIUI
66
67   # we do our first check without a lock, so that we can scan past messages
68   # being handled by Exim quickly, and only lock up on those which Exim is
69   # trying and failing to deliver.  However, since Exim will be hung on remote
70   # hosts, this is likely.  Thus best to kill queue-runners first.
71
72   return if fh_ends_newline($fh, $dfn, 0); # also returns on error
73   print "Problem? $msgid probably missing newline, locking to be sure ...\n";
74   flock($fh, LOCK_EX) or do { warn "flock(file($dfn)) failed: $!\n"; return };
75   return if fh_ends_newline($fh, $dfn, 1); # also returns on error
76
77   fixup_message($msgid, $dfn, $fh);
78
79   close($fh) or warn "close($dfn) failed: $!\n";
80 };
81
82 sub fixup_message {
83   my ($msgid, $dfn, $fh) = @_;
84   # we can't freeze the message, our lock stops that, which is good!
85
86   seek($fh, 0, 2) or do { warn "seek(file($dfn)) failed: $!\n"; return -1 };
87
88   my $r = inc_message_header_linecount($msgid);
89   if ($r < 0) {
90     warn "failed to fix message headers in ${msgid}-H so not editing message\n";
91     return;
92   }
93
94   print {$fh} "\n";
95
96   print "${msgid}: added newline\n";
97 };
98
99 sub inc_message_header_linecount {
100   my ($msgid) = @_;
101   my $name_in = "${msgid}-H";
102   my $name_out = "${msgid}-chunkfix";
103
104   open(my $in, '<:perlio', $name_in) or do { warn "open(${name_in}) failed: $!\n"; return -1 };
105   open(my $out, '>:perlio', $name_out) or do { warn "write-open(${name_out}) failed: $!\n"; return -1 };
106   my $seen = 0;
107   my $lc;
108   foreach (<$in>) {
109     if ($seen) {
110       print {$out} $_;
111       next;
112     }
113     if (/^(-body_linecount\s+)(\d+)(\s*)$/) {
114       $lc = $2 + 1;
115       print {$out} "${1}${lc}${3}";
116       $seen = 1;
117       next;
118     }
119     print {$out} $_;
120   }
121   close($in) or do {
122     warn "read-close(${msgid}-H) failed, assuming incomplete: $!\n";
123     close($out);
124     unlink $name_out;
125     return -1;
126   };
127   close($out) or do {
128     warn "write-close(${msgid}-chunkfix) failed, aborting: $!\n";
129     unlink $name_out;
130     return -1;
131   };
132
133   my @target = stat($name_in) or do { warn "stat($name_in) failed: $!\n"; unlink $name_out; return -1 };
134   my @created = stat($name_out) or do { warn "stat($name_out) failed: $!\n"; unlink $name_out; return -1 };
135   # 4=uid, 5=gid, 2=mode
136   if (($created[5] != $target[5]) or ($created[4] != $target[4])) {
137     chown $target[4], $target[5], $name_out or do {
138       warn "chown($name_out) failed: $!\n";
139       unlink $name_out;
140       return -1;
141     };
142   }
143   if (($created[2]&07777) != ($target[2]&0x7777)) {
144     chmod $target[2]&0x7777, $name_out or do {
145       warn "chmod($name_out) failed: $!\n";
146       unlink $name_out;
147       return -1;
148     };
149   }
150
151   rename $name_out, $name_in or do {
152     warn "rename '${msgid}-chunkfix' -> '${msgid}-H' failed: $!\n";
153     unlink $name_out;
154     return -1;
155   };
156
157   print "${msgid}: linecount set to $lc\n";
158   return 1;
159 }
160
161 find({wanted => \&each_found_file}, '.');