2 # Copyright (c) 2023 The Exim Maintainers
3 # SPDX-License-Identifier: GPL-2.0-or-later
4 # See the file NOTICE for conditions of use and distribution.
6 # Utility for one-time upgrage/downgrade between exim message-id formats,
7 # around the 4.97 transition
10 # This variables should be set by the building process
11 my $spool = 'SPOOL_DIRECTORY'; # may be overridden later
26 my $b62 = '[0-9A-Za-z]';
28 if ( !getopts('hud', \%opt)
30 || !$opt{u} && !$opt{d}
35 $spool = $ARGV[0] if ($ARGV[0]);
36 $mode_upgrade = $opt{u};
40 Utility for one-time down/upgrade of Exim message-id formats
41 in spool files. Only the filenames is first-line ID tag values
42 are affected; not message content such as Message-ID fields.
43 Only -H, -D and -J files are handled.
45 Syntax: exim_id_update [-d | -u | -h] [spooldir]
51 Exactly one of -d or -u must be given.
52 The spool directory defaults to the build-time value,
53 or can be given as a command-line argument.
58 # - Check exim not running
59 # - Wipe any wait-hints DBs, buy just removing the files.
60 # For all queue (main and named), with split-spool if needed, for each file identifiable
61 # as a spoolfile (name starts with an ID, ends with -H -D -J -K)
62 # XXX are there only subsets we can handle - eg. a -H + a -D ?
63 # mainline code sequence is -D (locks msg) -H ?-J
64 # mainline locking sequence (spool_open_datafile()) is
66 # - fnctl F_LOCK (amount = first line of file)
68 # The -H and -D files contain the ID as their initial line.
70 # - records successful deliveries, as insurance vs. crashes
71 # - has lines with mail addresses
73 # - is a temp for DKIM'd delivery when a transport-filter is in use
74 # - contains the message that would have been put on the wire (except for encryption)
75 # - the transport, with tpt-filter, writes the file - and then reads it
76 # so as to generate the DKIM signature. Then it sends the message, with
77 # generated headers and reading the file again, down the wire.
78 # And then it deletes it.
79 # - unclear if we really want to rewrite these files, if we do see then
82 # - if old-format name:
84 # - generate new files, in safe sequence
85 # - remove old files (do we need to archive?)
88 # loop for default Q, named Qs
89 # loop for plain, split-spool
91 # if is -H, and -D exists
93 # create new ID string from old
100 # rename old -J to new -J
107 chdir $spool or die "failed cd to $spool";
110 if ($_ =~ ($mode_upgrade ? "${b62}{6}-${b62}{6}-${b62}{2}-D" : "${b62}{6}-${b62}{11}-${b62}{4}-D") );
117 my $old_dfile = shift;
118 my $old_prefix = $old_dfile;
119 my ($old_hfile , $new_prefix);
123 $old_prefix =~ s/-D$//;
124 $old_hfile = $old_prefix . '-H';
126 # The -H file must also exist
127 return if (! -e $old_hfile);
129 $new_prefix = $old_prefix;
131 $new_prefix =~ s/^([^-]*)-([^-]*)-(.*)$/$1-00000$2-${3}00/;
133 $new_prefix =~ s/^([^-]*)-.....([^-]*)-(..)..$/$1-$2-${3}/;
136 ####### create the new -D file
138 open $d_old, '+<', $old_dfile
139 or die "Can't open file: $!\n";
141 # lock the old -D file
142 dfile_lock($d_old, $mode_upgrade ? 16 : 23);
143 # seek past the first line
146 # create the new -D file
147 $d_new = f_create($new_prefix . '-D');
149 # lock the new -D file
150 dfile_lock($d_new, $mode_upgrade ? 23 : 16);
152 # write the new message-id to the first line
153 print $d_new "$new_prefix-D\n";
155 # copy the rest of the -D file
156 while ($line = <$d_old>) {
160 ####### create the new -H file
162 open my $h_old, '<', $old_hfile
163 or die "Can't open file: $!\n";
166 my $h_new = f_create($new_prefix . '-H');
167 print $h_new "$new_prefix-H\n";
168 while ($line = <$h_old>) {
172 ###### rename a journal file if it exists
174 rename $old_prefix . '-J', $new_prefix . '-J' if (-e $old_prefix . '-J');
179 unlink $old_hfile or die "failed to remove $old_hfile";
181 unlink $old_dfile or die "failed to remove $old_dfile";
183 dfile_unlock($d_new, $mode_upgrade ? 23 : 16);
192 my $fs = new File::FcntlLock;
194 $fs->l_type( F_WRLCK );
195 $fs->l_whence( SEEK_CUR );
197 $fs->l_len( $nbytes );
199 $fs->lock( $fh, F_SETLK )
200 or die "Locking failed: " . $fs->error . "\n";
206 my $fs = new File::FcntlLock;
208 $fs->l_type( F_UNLCK );
209 $fs->l_whence( SEEK_CUR );
211 $fs->l_len( $nbytes );
212 $fs->lock( $fh, F_SETLK )
213 or die "Unlocking failed: " . $fs->error . "\n";
217 my $filename = shift;
218 sysopen(my $fh, $filename, O_RDWR|O_CREAT|O_EXCL)
219 or die "Can't create $filename: $!";
222 # TODO: chown, chgrp exim; chmod 0640