upgrade_send_spool.pl.in 5.75 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#! --PERL--
# -*- indent-tabs-mode: nil; -*-
# vim:ft=perl:et:sw=4
# $Id$

# Sympa - SYsteme de Multi-Postage Automatique
#
# Copyright (c) 1997, 1998, 1999 Institut Pasteur & Christophe Wolfhugel
# Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
# 2006, 2007, 2008, 2009, 2010, 2011 Comite Reseau des Universites
11
# Copyright (c) 2011, 2012, 2013, 2014, 2015 GIP RENATER
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

use lib split(/:/, $ENV{SYMPALIB} || ''), '--modulesdir--';
use strict;
use warnings;
use Digest::MD5;
use English qw(-no_match_vars);
use Getopt::Long;
use Pod::Usage;

use Sympa::Bulk;
use Sympa::Constants;
use Conf;
37
use Sympa::LockedFile;
38
39
40
41
42
43
use Log;

my %options;
unless (GetOptions(\%options, 'help|h', 'dry_run', 'version|v')) {
    pod2usage(-exitval => 1, -output => \*STDERR);
}
44
if ($options{'help'}) {
45
    pod2usage(0);
46
} elsif ($options{'version'}) {
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    printf "Sympa %s\n", Sympa::Constants::VERSION;
    exit 0;
}

unless (Conf::load(Conf::get_sympa_conf(), 'no_db')) {
    die sprintf 'Configuration file %s has errors.\n', Conf::get_sympa_conf();
}

# Set the User ID & Group ID for the process
$GID = $EGID = (getgrnam(Sympa::Constants::GROUP))[2];
$UID = $EUID = (getpwnam(Sympa::Constants::USER))[2];
# Required on FreeBSD to change ALL IDs (effective UID + real UID + saved UID)
POSIX::setuid((getpwnam(Sympa::Constants::USER))[2]);
POSIX::setgid((getgrnam(Sympa::Constants::GROUP))[2]);
# Check if the UID has correctly been set (useful on OS X)
unless (($GID == (getgrnam(Sympa::Constants::GROUP))[2])
    && ($UID == (getpwnam(Sympa::Constants::USER))[2])) {
    die
        "Failed to change process user ID and group ID. Note that on some OS Perl scripts can't change their real UID. In such circumstances Sympa should be run via sudo.";
}
# Sets the UMASK
68
umask oct $Conf::Conf{'umask'};
69

70
my $bulk      = Sympa::Bulk->new;
71
72
my $spool_dir = $Conf::Conf{'queue'};

73
74
75
mkdir "$spool_dir/bad",   0755 unless -d "$spool_dir/bad";
mkdir "$spool_dir/moved", 0755 unless -d "$spool_dir/moved";

76
my ($dh, $filename);
77
unless (opendir $dh, $spool_dir) {
78
79
80
81
82
83
84
85
86
87
    printf STDERR "Cannot open directory %s.\n", $spool_dir;
    exit 1;
}
while ($filename = readdir $dh) {
    next unless -f "$spool_dir/$filename";
    next if $filename =~ /,lock/;    # Skip lock.
    next unless /\AT\./;
    next unless /\ABAD-/;
    next unless /\A\.+/;

88
89
90
91
    my $lock_fh =
        Sympa::LockedFile->new($spool_dir . '/' . $filename, -1, '+<');
    next unless $lock_fh;

92
93
94
95
96
    my $metadata = tools::unmarshal_metadata(
        $spool_dir, $filename,
        qr{\A([^\s\@]+)(?:\@([\w\.\-]+))?\.(\d+)\.(\w+)(?:,.*)?\z},
        [qw(localpart domainpart date pid)]
    );
97
98
99
100
    unless ($metadata) {
        $lock_fh->close;
        next;
    }
101

102
    my $msg_string = do { local $RS; <$lock_fh> };
103
    my $message = Sympa::Message->new($msg_string, %$metadata);
104
105
106
107
    unless ($message and $message->{checksum}) {
        $lock_fh->close;
        next;
    }
108
109

    unless (process($message)) {
110
        $lock_fh->rename($spool_dir . '/bad/' . $filename)
111
112
            unless $options{dry_run};
    } else {
113
        $lock_fh->rename($spool_dir . '/moved/' . $filename)
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
            unless $options{dry_run};
    }
}
closedir $dh;

sub process {
    my $message = shift;

    ## valid X-Sympa-Checksum prove the message comes from web interface with
    ## authenticated sender
    unless ($message->{'checksum'} eq sympa_checksum($message->{'rcpt'})) {
        Log::do_log('err', '%s: Incorrect X-Sympa-Checksum header', $message);
        return undef;
    }

    if (ref $message->{context} eq 'Sympa::List') {
        $message->{'md5_check'} = 1;
        delete $message->{checksum};

        tools::store_spool($spool_dir, $message, '%s@%s.%ld.%ld,%d',
            [qw(localpart domainpart date pid RAND)])
            unless $options{dry_run};
        Log::do_log('info', '%s: Moved to msg spool', $message);
    } else {
138
        $bulk->store($message, [split /\s*,\s*/, $message->{rcpt}])
139
            unless $options{dry_run};
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
        Log::do_log('info', '%s: Moved to bulk spool', $message);
    }
    return 1;
}

sub sympa_checksum {
    my $rcpt = shift;
    return (
        substr(
            Digest::MD5::md5_hex(join('/', $Conf::Conf{'cookie'}, $rcpt)), -10
        )
    );
}

__END__

=encoding utf-8

=head1 NAME

upgrade_send_spool, upgrade_send_spool.pl - Upgrade messages in incoming spool

=head1 SYNOMSIS

  upgrade_send_spool.pl [ --dry_run ]

=head1 DESCRIPTION

On Sympa earlier than 6.2, messages sent from WWSympa were injected in
msg spool with special checksum.
Recent release of Sympa and WWSympa injects outbound messages in outgoing
spool or sends them by Mailer directly.
This program migrates messages with old format in appropriate spools.

=head1 OPTIONS

=over

=item --dry_run

Shows what will be done but won't really perform upgrade process.

=back

=head1 RETURN VALUE

This program exits with status 0 if processing secceeded.
Otherwise exits with non-zero status.

=head1 CONFIGURATION OPTIONS

Following site configuration parameters in F<--CONFIG--> are referred.

=over

=item cookie

=item queue

=item umask

=back

=head1 SEE ALSO

L<sympa.conf(5)>, L<Sympa::Message>.

=head1 HISTORY

upgrade_send_spool.pl appeared on Sympa 6.2.

=cut