AuthorizeRequest.pm 9.55 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# -*- 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
# Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016 GIP RENATER
#
# 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/>.

package Sympa::Spindle::AuthorizeRequest;

use strict;
use warnings;
use Time::HiRes qw();

use Sympa;
use Sympa::CommandDef;
use Sympa::Log;
use Sympa::Report;
use Sympa::Request;
use Sympa::Scenario;
use Sympa::Spool::Request;

use base qw(Sympa::Spindle);

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

sub _twist {
    my $self    = shift;
    my $request = shift;

    # Skip authorization unless specific scenario is defined.
    if (   $request->{error}
        or not $Sympa::CommandDef::comms{$request->{action}}
        or not $Sympa::CommandDef::comms{$request->{action}}->{scenario}) {
        return ['Sympa::Spindle::DispatchRequest'];
    }

    my $scenario = $Sympa::CommandDef::comms{$request->{action}}->{scenario};
    my $action_regexp =
        $Sympa::CommandDef::comms{$request->{action}}->{action_regexp}
        or die 'bug in logic. Ask developer';

    my $message = $request->{message};
    my $sender  = $request->{sender};

    # Check if required list argument is known.
    if ($request->{localpart} and ref $request->{context} ne 'Sympa::List') {
        $request->{error} = 'unknown_list';
        return ['Sympa::Spindle::DispatchRequest'];
    }
    my $that = $request->{context};

    my $context = {
        sender  => $sender,
        message => $message,
    };

    # Authorize requests.

    my $action;
    my $result;

    my $auth_method = _get_auth_method($request);
    return 'wrong_auth'
        unless defined $auth_method;

    $result = Sympa::Scenario::request_action($that, $scenario, $auth_method,
        $context);
    $action = $result->{'action'} if ref $result eq 'HASH';

    unless (defined $action and $action =~ /\A(?:$action_regexp)\b/) {
        $log->syslog(
            'info',
            '%s for %s from %s aborted, unknown requested action "%s" in scenario "%s"',
            uc $request->{action},
            $that,
            $sender,
            $action,
            $scenario
        );
        my $error = sprintf 'Unknown requested action in scenario: %s',
            ($action || '');
        Sympa::Report::reject_report_cmd($request, 'intern', $error);
        return undef;
    }

    # Special cases for subscribe & signoff: If membership is unsatisfactory,
    # force execute request and let it be rejected.
    unless ($action =~ /\Areject\b/i) {
        if ($request->{action} eq 'subscribe'
            and defined $that->get_list_member($request->{email})) {
            $action =~ s/\A\w+/do_it/;
        } elsif ($request->{action} eq 'signoff'
            and not defined $that->get_list_member($request->{email})) {
            $action =~ s/\A\w+/do_it/;
        }
    }

    if ($action =~ /\Ado_it\b/i) {
        $request->{quiet} ||= ($action =~ /,\s*quiet\b/i);    # Overwrite.
        $request->{notify} = ($action =~ /,\s*notify\b/i);
        return ['Sympa::Spindle::DispatchRequest'];
    } elsif ($action =~ /\Arequest_auth\b(?:\s*[[]\s*(\S+)\s*[]])?/i) {
        my $to = $1;
        if ($to and $to eq 'email') {
            $to = $request->{email} || $sender;
        } else {
            $to = $sender;
        }

        $log->syslog('debug2', 'Auth requested from %s', $sender);
        unless (Sympa::request_auth(%$request, sender => $to)) {
            my $error = sprintf
                'Unable to request authentication for command "%s"',
                $request->{action};
            Sympa::Report::reject_report_cmd($request, 'intern', $error);
            return undef;
        }
        $log->syslog(
            'info',
            '%s for %s from %s, auth requested (%.2f seconds)',
            uc $request->{action},
            $that,
            $sender,
            Time::HiRes::time() - $self->{start_time}
        );
        return 1;
    } elsif ($action =~ /\Aowner\b/i and ref $that eq 'Sympa::List') {
        Sympa::Report::notice_report_cmd($request, 'req_forward')
            unless $action =~ /,\s*quiet\b/i;

        my $tpl =
            {subscribe => 'subrequest', signoff => 'sigrequest'}
            ->{$request->{action}};
        my $owner_action =
            {subscribe => 'add', signoff => 'del'}->{$request->{action}};

        # Send a notice to the owners.
        unless (
            $that->send_notify_to_owner(
                $tpl,
                {   'who'     => $sender,
                    'keyauth' => Sympa::compute_auth(
                        context => $that,
                        email   => $request->{email},
                        action  => $owner_action,
                    ),
                    'replyto' => Sympa::get_address($that, 'sympa'),
                    'gecos'   => $request->{gecos},
                }
            )
            ) {
            #FIXME: Why is error reported only in this case?
            $log->syslog('info',
                'Unable to send notify "%s" to %s list owner',
                $tpl, $that);
            Sympa::Report::reject_report_cmd(
                $request, 'intern',
                sprintf('Unable to send subrequest to %s list owner',
                    $that->get_id)
            );
        }

        my $spool_req   = Sympa::Spool::Request->new;
        my $add_request = Sympa::Request->new_from_tuples(
            %$request,
            action => $owner_action,
            date   => $message->{date},    # Keep date of message.
        );
        if ($spool_req->store($add_request)) {
            $log->syslog(
                'info',
                '%s for %s from %s forwarded to the owners of the list (%.2f seconds)',
                uc $request->{action},
                $that,
                $sender,
                Time::HiRes::time() - $self->{start_time}
            );
        }
        return 1;
    } elsif ($action =~ /\Areject\b/i) {
        if (defined $result->{'tt2'}) {
            unless (
                Sympa::send_file(
                    $that, $result->{'tt2'},
                    $sender, {'auto_submitted' => 'auto-replied'}
                )
                ) {
                $log->syslog('notice', 'Unable to send template "%s" to %s',
                    $result->{'tt2'}, $sender);
                Sympa::Report::reject_report_cmd($request, 'auth',
                    $result->{'reason'});
            }
        } else {
            Sympa::Report::reject_report_cmd($request, 'auth',
                $result->{'reason'});
        }
        $log->syslog(
            'info',
            '%s for %s from %s refused (not allowed)',
            uc $request->{action},
            $that, $sender
        );
        return 'not_allowed';
    } else {
        #NOTREACHED
        die 'bug in logic. Ask developer';
    }
}

# Checks the authentication and return method
# used if authentication not failed.
# Returns 'smime', 'md5', 'dkim' or 'smtp' if authentication OK, undef else.
# Old name: Sympa::Commands::get_auth_method().
sub _get_auth_method {
    $log->syslog('debug3', '(%s)', @_);
    my $request = shift;

    my $list     = $request->{context};
    my $sign_mod = $request->{sign_mod};
    my $sender   = $request->{sender};

    my $cmd   = $request->{action};
    my $email = $request->{email};

    my $auth = $request->{auth};

    my $that;
    my $auth_method;

    if ($sign_mod and $sign_mod eq 'smime') {
        $auth_method = 'smime';
    } elsif ($auth) {
        $log->syslog('debug', 'Auth received from %s: %s', $sender, $auth);

        my $compute;
        if (ref $list eq 'Sympa::List') {
            $compute = Sympa::compute_auth(
                context => $list,
                email   => $email,
                action  => $cmd
            );
            $that = $list->{'domain'};    # Robot
        } else {
            $compute = Sympa::compute_auth(
                context => '*',
                email   => $email,
                action  => $cmd
            );
            $that = '*';                  # Site
        }
        if ($auth eq $compute) {
            $auth_method = 'md5';
        } else {
            $log->syslog('debug2', 'Auth should be %s', $compute);
            if (grep { $cmd eq $_ } qw(add del invite signoff subscribe)) {
                Sympa::Report::reject_report_cmd($request, 'user',
                    'wrong_email_confirm', {command => $cmd});
            } else {
                Sympa::Report::reject_report_cmd($request, 'intern',
                    'The authentication process failed');
            }
            $log->syslog('info', 'Command "%s" from %s refused, auth failed',
                $request->{cmd_line}, $sender);
            return undef;
        }
    } else {
        $auth_method = 'smtp';
        $auth_method = 'dkim' if $sign_mod and $sign_mod eq 'dkim';
    }

    return $auth_method;
}

1;