upgrade_send_spool.pl.in 5.62 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, 2016, 2017 GIP RENATER
12
13
# Copyright 2017, 2019, 2021 The Sympa Community. See the
# AUTHORS.md file at the top-level directory of this distribution and at
14
# <https://github.com/sympa-community/sympa.git>.
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#
# 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::Constants;
use Conf;
39
use Sympa::Log;
40
use Sympa::Spool;
41
use Sympa::Spool::Incoming;
42
use Sympa::Spool::Outgoing;
43
44
45
46
47

my %options;
unless (GetOptions(\%options, 'help|h', 'dry_run', 'version|v')) {
    pod2usage(-exitval => 1, -output => \*STDERR);
}
48
if ($options{'help'}) {
49
    pod2usage(0);
50
} elsif ($options{'version'}) {
51
52
53
54
    printf "Sympa %s\n", Sympa::Constants::VERSION;
    exit 0;
}

55
56
my $log = Sympa::Log->instance;

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

61
62
63
64
65
66
# Get obsoleted parameter.
open my $fh, '<', Conf::get_sympa_conf() or die $ERRNO;
my ($cookie) =
    grep {defined} map { /\A\s*cookie\s+(\S+)/s ? $1 : undef } <$fh>;
close $fh;

67
68
69
70
71
72
73
74
75
76
77
78
79
# 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
80
umask oct $Conf::Conf{'umask'};
81

82
my $bulk      = Sympa::Spool::Outgoing->new;
83
84
my $spool     = Sympa::Spool::Incoming->new;
my $spool_dir = $spool->{directory};
85

86
87
mkdir "$spool_dir/moved", 0755 unless -d "$spool_dir/moved";

88
while (1) {
89
    my ($message, $handle) = $spool->next(no_filter => 1);
90
91
92
93
94
95
96
97
98
99
100
101

    if ($message and $handle) {
        my $status = process($message);
        unless (defined $status) {
            $spool->quarantine($handle) unless $options{dry_run};
        } elsif ($status) {
            $handle->rename($spool_dir . '/moved/' . $handle->basename)
                unless $options{dry_run};
        } else {
            next;
        }
    } elsif ($handle) {
102
        next;
103
    } else {
104
        last;
105
106
107
108
109
110
    }
}

sub process {
    my $message = shift;

111
112
    return 0 unless $message->{checksum};

113
114
115
    ## valid X-Sympa-Checksum prove the message comes from web interface with
    ## authenticated sender
    unless ($message->{'checksum'} eq sympa_checksum($message->{'rcpt'})) {
116
117
        $log->syslog('err', '%s: Incorrect X-Sympa-Checksum header',
            $message);
118
119
120
121
122
123
124
        return undef;
    }

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

125
        # Don't use method of incoming spool to preserve original PID.
126
        Sympa::Spool::store_spool($spool_dir, $message, '%s@%s.%ld.%ld,%d',
127
128
            [qw(localpart domainpart date pid RAND)])
            unless $options{dry_run};
129
        $log->syslog('info', '%s: Moved to msg spool', $message);
130
    } else {
131
        $bulk->store($message, [split /\s*,\s*/, $message->{rcpt}])
132
            unless $options{dry_run};
133
        $log->syslog('info', '%s: Moved to bulk spool', $message);
134
135
136
137
138
139
    }
    return 1;
}

sub sympa_checksum {
    my $rcpt = shift;
140
    return substr Digest::MD5::md5_hex(join '/', $cookie, $rcpt), -10;
141
142
143
144
145
146
147
148
149
150
}

__END__

=encoding utf-8

=head1 NAME

upgrade_send_spool, upgrade_send_spool.pl - Upgrade messages in incoming spool

151
=head1 SYNOPSIS
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174

  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

175
This program exits with status 0 if processing succeeded.
176
177
178
179
180
181
182
183
184
185
Otherwise exits with non-zero status.

=head1 CONFIGURATION OPTIONS

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

=over

=item cookie

186
187
(obsoleted by Sympa 6.2.61b)

188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
=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