wwsympa.fcgi.in 838 KB
Newer Older
1
#!--PERL--
2
3
4
5
# -*- indent-tabs-mode: nil; -*-
# vim:ft=perl:et:sw=4
# $Id$

6
# Sympa - SYsteme de Multi-Postage Automatique
7
8
9
10
11
#
# 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 GIP RENATER
12
13
14
15
16
17
18
19
20
21
22
23
#
# 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
24
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
25

26
## Copyright 1999 Comité Réseaux des Universités
root's avatar
root committed
27
## web interface to Sympa mailing lists manager
salaun's avatar
salaun committed
28
## Sympa: http://www.sympa.org/
root's avatar
root committed
29
## Authors :
salaun's avatar
   
salaun committed
30
##           Serge Aumont <sa AT cru.fr>
31
##           Olivier Salaün <os AT cru.fr>
32

33
=encoding utf-8
34

35
36
37
=head1 NAME 

wwsympa, wwsympa.fcgi - Sympa web interface 
38
39
40
41
42
43

=head1 DESCRIPTION 

This fcgi script completely handles all aspects of the Sympa web interface

=cut 
root's avatar
root committed
44

45
46
use strict;
##use warnings;
47
use lib split(/:/, $ENV{SYMPALIB} || ''), '--modulesdir--';
olivier.salaun's avatar
olivier.salaun committed
48

49
use Archive::Zip qw();
50
use DateTime;
51
use DateTime::Format::Mail;
sikeda's avatar
sikeda committed
52
use Encode qw();
53
use English;    # FIXME: drop $MATCH usage
sikeda's avatar
sikeda committed
54
use MIME::EncWords;
55
use MIME::Lite::HTML;
56
use MIME::Parser;
sikeda's avatar
sikeda committed
57
use POSIX qw();
58
use Time::Local qw();
59
use Data::Dumper;    # tentative
60
BEGIN { eval 'use Crypt::OpenSSL::X509'; }
61

62
use Sympa::Admin;
63
use Sympa::Alarm;
sikeda's avatar
sikeda committed
64
use Sympa::Archive;
65
use Sympa::Auth;
root's avatar
root committed
66
use Conf;
67
use Sympa::ConfDef;
68
use Sympa::Constants;
69
use Sympa::CookieLib;
70
use Sympa::Crash Hook => \&_crash_handler;    # Show traceback.
sikeda's avatar
sikeda committed
71
use Sympa::Family;
72
use Sympa::Language;
73
use Sympa::LDAPSource;
74
use Sympa::List;
sikeda's avatar
sikeda committed
75
use Log;
76
77
78
use Sympa::Mail;
use Sympa::Marc::Search;
use Sympa::Message;
sikeda's avatar
sikeda committed
79
use Sympa::Regexps;
80
81
82
use Sympa::Report;
use Sympa::Robot;
use Sympa::Scenario;
83
use SDM;
84
85
use Sympa::SharedDocument;
use Sympa::Session;
86
use tools;
87
use Sympa::Tracking;
sikeda's avatar
sikeda committed
88
89
use tt2;
use Sympa::User;
90
use wwslib;
root's avatar
root committed
91
92

## WWSympa librairies
93
my %options;
root's avatar
root committed
94

95
my $sympa_conf_file = Sympa::Constants::CONFIG;
root's avatar
root committed
96

97
98
99
100
101
102
# Used via the Sympa::Plugin interface
our $list;
our $param = {};
our $robot_id;
our $session;
our $plugins;
103

salaun's avatar
salaun committed
104
my $loop = 0;
105
my ($robot, $robot_object);
106
my $pinfo;
107
my $ip;
108
my $rss;
109
my $ajax;
salaun's avatar
salaun committed
110

111
my $allow_absolute_path;    #FIXME: to be removed in the future.
112

root's avatar
root committed
113
## Load sympa config
114
unless (Conf::load()) {
115
    printf STDERR
116
117
        "Unable to load sympa configuration, file %s or one of the vhost robot.conf files contain errors. Exiting.\n",
        Conf::get_sympa_conf();
118
    exit 1;
root's avatar
root committed
119
120
}

121
Log::set_log_level($Conf::Conf{'log_level'});
122

sikeda's avatar
sikeda committed
123
my $daemon_name = Log::set_daemon($0);
124

125
126
127
## Open log
my $log_facility = $Conf::Conf{'log_facility'} || $Conf::Conf{'syslog'};
Log::do_openlog($log_facility, $Conf::Conf{'log_socket_type'}, 'wwsympa');
128

129
## Start plugins
130
if (eval "require Sympa::Plugin::Manager") {
131
132
133
    $plugins = Sympa::Plugin::Manager->new(application => 'website');
}

sikeda's avatar
sikeda committed
134
$Sympa::Mail::always_use_bulk = 1;    #FIXME: Should instantiate Sympa::Bulk.
root's avatar
root committed
135

136
if ($Conf::Conf{'use_fast_cgi'}) {
root's avatar
root committed
137
    require CGI::Fast;
138
} else {
root's avatar
root committed
139
140
    require CGI;
}
sikeda's avatar
sikeda committed
141
my $mime_types = wwslib::load_mime_types();
142

root's avatar
root committed
143
144
145
146
147
148
149
150
151
# hash of all the description files already loaded
# format :
#     $desc_files{pathfile}{'date'} : date of the last load
#     $desc_files{pathfile}{'desc_hash'} : hash which describes
#                         the description file

#%desc_files_map; NOT USED ANYMORE

# hash of the icons linked with a type of file
152
my %icon_table;
root's avatar
root committed
153

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# application file
$icon_table{'unknown'} =
    $Conf::Conf{'static_content_url'} . '/icons/unknown.png';
$icon_table{'folder'} =
    $Conf::Conf{'static_content_url'} . '/icons/folder.png';
$icon_table{'current_folder'} =
    $Conf::Conf{'static_content_url'} . '/icons/folder.open.png';
$icon_table{'application'} =
    $Conf::Conf{'static_content_url'} . '/icons/unknown.png';
$icon_table{'octet-stream'} =
    $Conf::Conf{'static_content_url'} . '/icons/binary.png';
$icon_table{'audio'} =
    $Conf::Conf{'static_content_url'} . '/icons/sound1.png';
$icon_table{'image'} =
    $Conf::Conf{'static_content_url'} . '/icons/image2.png';
$icon_table{'text'}  = $Conf::Conf{'static_content_url'} . '/icons/text.png';
$icon_table{'video'} = $Conf::Conf{'static_content_url'} . '/icons/movie.png';
$icon_table{'father'} = $Conf::Conf{'static_content_url'} . '/icons/back.png';
$icon_table{'sort'}   = $Conf::Conf{'static_content_url'} . '/icons/down.png';
$icon_table{'url'}    = $Conf::Conf{'static_content_url'} . '/icons/link.png';
$icon_table{'left'}   = $Conf::Conf{'static_content_url'} . '/icons/left.png';
$icon_table{'right'} = $Conf::Conf{'static_content_url'} . '/icons/right.png';
root's avatar
root committed
176
177
## Shared directory and description file

178
179
#$shared = 'shared';
#$desc = '.desc';
root's avatar
root committed
180
181

## subroutines
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
our %comm = (
    'home'                   => 'do_home',
    'logout'                 => 'do_logout',
    'loginrequest'           => 'do_loginrequest',
    'login'                  => 'do_login',
    'sso_login'              => 'do_sso_login',
    'sso_login_succeeded'    => 'do_sso_login_succeeded',
    'subscribe'              => 'do_subscribe',
    'multiple_subscribe'     => 'do_multiple_subscribe',
    'subrequest'             => 'do_subrequest',
    'subindex'               => 'do_subindex',
    'suboptions'             => 'do_suboptions',
    'signoff'                => 'do_signoff',
    'auto_signoff'           => 'do_auto_signoff',
    'family_signoff'         => 'do_family_signoff',
    'family_signoff_request' => 'do_family_signoff_request',
    #XXX'multiple_signoff' => 'do_multiple_signoff',
    'sigrequest'       => 'do_sigrequest',
    'ignoresub'        => 'do_ignoresub',
    'which'            => 'do_which',
    'lists'            => 'do_lists',
    'latest_lists'     => 'do_latest_lists',
    'active_lists'     => 'do_active_lists',
    'info'             => 'do_info',
    'subscriber_count' => 'do_subscriber_count',
    'review'           => 'do_review',
    'search'           => 'do_search',
    'pref',            => 'do_pref',
    'setpref'          => 'do_setpref',
    'setpasswd'        => 'do_setpasswd',
    'renewpasswd'      => 'do_renewpasswd',
    'firstpasswd'      => 'do_firstpasswd',
    'requestpasswd'    => 'do_requestpasswd',
    'choosepasswd'     => 'do_choosepasswd',
    'viewfile'         => 'do_viewfile',
    'set'              => 'do_set',
    'admin'            => 'do_admin',
    'add_request'      => 'do_add_request',
    'add'              => 'do_add',
    'del'              => 'do_del',
    'modindex'         => 'do_modindex',
    'reject'           => 'do_reject',
    #XXX'reject_notify' => 'do_reject_notify',
    'distribute'      => 'do_distribute',
    'add_frommod'     => 'do_add_frommod',
    'viewmod'         => 'do_viewmod',
    'd_reject_shared' => 'do_d_reject_shared',
    #XXX'reject_notify_shared' => 'do_reject_notify_shared',
    'd_install_shared'         => 'do_d_install_shared',
    'editfile'                 => 'do_editfile',
    'savefile'                 => 'do_savefile',
    'arc'                      => 'do_arc',
    'latest_arc'               => 'do_latest_arc',
    'latest_d_read'            => 'do_latest_d_read',
    'arc_manage'               => 'do_arc_manage',
    'remove_arc'               => 'do_remove_arc',
    'send_me'                  => 'do_send_me',
    'view_source'              => 'do_view_source',
    'tracking'                 => 'do_tracking',
    'arcsearch_form'           => 'do_arcsearch_form',
    'arcsearch_id'             => 'do_arcsearch_id',
    'arcsearch'                => 'do_arcsearch',
    'rebuildarc'               => 'do_rebuildarc',
    'rebuildallarc'            => 'do_rebuildallarc',
    'arc_download'             => 'do_arc_download',
    'arc_delete'               => 'do_arc_delete',
    'serveradmin'              => 'do_serveradmin',
    'set_loglevel'             => 'do_set_loglevel',
    'set_dumpvars'             => 'do_set_dumpvars',
    'show_sessions'            => 'do_show_sessions',
    'unset_dumpvars'           => 'do_unset_dumpvars',
    'set_session_email'        => 'do_set_session_email',
    'restore_email'            => 'do_restore_email',
    'skinsedit'                => 'do_skinsedit',
    'css'                      => 'do_css',
    'help'                     => 'do_help',
    'edit_list_request'        => 'do_edit_list_request',
    'edit_list'                => 'do_edit_list',
    'create_list_request'      => 'do_create_list_request',
    'create_list'              => 'do_create_list',
    'get_pending_lists'        => 'do_get_pending_lists',
    'get_closed_lists'         => 'do_get_closed_lists',
    'get_latest_lists'         => 'do_get_latest_lists',
    'get_inactive_lists'       => 'do_get_inactive_lists',
    'get_biggest_lists'        => 'do_get_biggest_lists',
    'set_pending_list_request' => 'do_set_pending_list_request',
    'install_pending_list'     => 'do_install_pending_list',
    'edit_config'              => 'do_edit_config',
    #XXX'submit_list' => 'do_submit_list',
    'editsubscriber'       => 'do_editsubscriber',
    'viewbounce'           => 'do_viewbounce',
    'redirect'             => 'do_redirect',
    'rename_list_request'  => 'do_rename_list_request',
    'rename_list'          => 'do_rename_list',
    'copy_list'            => 'do_copy_list',
    'reviewbouncing'       => 'do_reviewbouncing',
    'resetbounce'          => 'do_resetbounce',
    'scenario_test'        => 'do_scenario_test',
    'search_list'          => 'do_search_list',
    'show_cert'            => 'do_show_cert',
    'close_list_request'   => 'do_close_list_request',
    'close_list'           => 'do_close_list',
    'purge_list'           => 'do_purge_list',
    'restore_list'         => 'do_restore_list',
    'upload_pictures'      => 'do_upload_pictures',
    'delete_pictures'      => 'do_delete_pictures',
    'd_read'               => 'do_d_read',
    'd_create_dir'         => 'do_d_create_dir',
    'd_upload'             => 'do_d_upload',
    'd_unzip'              => 'do_d_unzip',
    'd_editfile'           => 'do_d_editfile',
    'd_properties'         => 'do_d_properties',
    'd_overwrite'          => 'do_d_overwrite',
    'd_savefile'           => 'do_d_savefile',
    'd_describe'           => 'do_d_describe',
    'd_delete'             => 'do_d_delete',
    'd_rename'             => 'do_d_rename',
    'd_control'            => 'do_d_control',
    'd_change_access'      => 'do_d_change_access',
    'd_set_owner'          => 'do_d_set_owner',
    'd_admin'              => 'do_d_admin',
    'dump_scenario'        => 'do_dump_scenario',
    'dump'                 => 'do_dump',
    'arc_protect'          => 'do_arc_protect',
    'remind'               => 'do_remind',
    'change_email'         => 'do_change_email',
    'change_email_request' => 'do_change_email_request',
    'load_cert'            => 'do_load_cert',
    'compose_mail'         => 'do_compose_mail',
    'send_mail'            => 'do_send_mail',
    'request_topic'        => 'do_request_topic',
    'tag_topic_by_sender'  => 'do_tag_topic_by_sender',
    'search_user'          => 'do_search_user',
    'set_lang'             => 'do_set_lang',
    'attach'               => 'do_attach',
    'stats'                => 'do_stats',
    'viewlogs'             => 'do_viewlogs',
    'wsdl'                 => 'do_wsdl',
    'sync_include'         => 'do_sync_include',
    'review_family'        => 'do_review_family',
    'ls_templates'         => 'do_ls_templates',
    'remove_template'      => 'do_remove_template',
    'copy_template'        => 'do_copy_template',
    'view_template'        => 'do_view_template',
    'edit_template'        => 'do_edit_template',
    #'rss' => 'do_rss', #FIXME:Currently processed in differenct way.
    'rss_request'     => 'do_rss_request',
    'maintenance'     => 'do_maintenance',
    'blacklist'       => 'do_blacklist',
    'edit_attributes' => 'do_edit_attributes',
    'ticket'          => 'do_ticket',
    'manage_template' => 'do_manage_template',
    #XXX'send_newsletter' => 'do_send_newsletter',
    'suspend_request'        => 'do_suspend_request',
    'suspend_request_action' => 'do_suspend_request_action',
    'show_exclude'           => 'do_show_exclude',
    # 'ca' stands for 'custom_action'. I used a short name to make it discrete
    # in a URL.
    'ca' => 'do_ca',
    # 'lca' stands for 'list_custom_action'. I used a short name to make it
    # discrete in a URL.
    'lca' => 'do_lca',
    'automatic_lists_management_request' =>
        'do_automatic_lists_management_request',
    'automatic_lists_management' => 'do_automatic_lists_management',
    'automatic_lists_request'    => 'do_automatic_lists_request',
    'automatic_lists'            => 'do_automatic_lists',
);

my %auth_action = (
    'logout'              => 1,
    'loginrequest'        => 1,
    'login'               => 1,
    'sso_login'           => 1,
    'sso_login_succeeded' => 1,
    'renewpasswd'         => 1,
    'firstpasswd'         => 1,
    'choosepasswd'        => 1,
    'sendssopasswd'       => 1,    #FIXME: currently not used
    'ticket'              => 1,
);

## Arguments awaited in the PATH_INFO, depending on the action
our %action_args = (
    'default'       => ['list'],
    'editfile'      => ['list', 'file', 'previous_action'],
    'requestpasswd' => ['email'],
    'choosepasswd' => ['email', 'passwd'],
    'lists'        => ['topic', 'subtopic'],
    'latest_lists' => ['topic', 'subtopic'],
    'active_lists' => ['topic', 'subtopic'],
    'login'        => ['email', 'passwd', 'previous_action', 'previous_list'],
    'sso_login' => ['auth_service_name', 'subaction', 'email', 'ticket'],
    'sso_login_succeeded' =>
        ['auth_service_name', 'previous_action', 'previous_list'],
    'loginrequest'     => ['previous_action', 'previous_list'],
    'logout'           => ['previous_action', 'previous_list'],
    'renewpasswd'      => ['previous_action', 'previous_list'],
    'firstpasswd'      => ['previous_action', 'previous_list'],
    'css'              => ['file'],
    'pref'             => ['previous_action', 'previous_list'],
    'reject'           => ['list',            'id'],
    'distribute'       => ['list',            'id'],
    'add_frommod'      => ['list',            'id'],
    'dump_scenario'    => ['list',            'pname'],
    'd_reject_shared'  => ['list',            'id'],
    'd_install_shared' => ['list',            'id'],
    'modindex'         => ['list'],
    'viewmod'     => ['list', 'id', '@file'],
    'viewfile'    => ['list', 'file'],
    'add'         => ['list', 'email'],
    'add_request' => ['list'],
    'del' => ['list', 'email'],
    'editsubscriber' =>
        ['list', 'email', 'previous_action', 'custom_attribute'],
397
#		'editsubscriber' => ['list','email','previous_action'],
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
    'viewbounce'  => ['list', 'email'],
    'resetbounce' => ['list', 'email'],
    'review'      => ['list', 'page', 'size', 'sortby'],
    'reviewbouncing' => ['list', 'page',  'size'],
    'arc'            => ['list', 'month', '@arc_file'],
    'latest_arc'     => ['list'],
    'arc_manage'     => ['list'],
    'arcsearch_form' => ['list', 'archive_name'],
    'arcsearch_id'   => ['list', 'archive_name', 'msgid'],
    'rebuildarc'     => ['list', 'month'],
    'rebuildallarc' => [],
    'arc_download'  => ['list'],
    'arc_delete'    => ['list', 'zip'],
    'home'          => [],
    'help'          => ['help_topic'],
    'show_cert'     => [],
    'subscribe'     => ['list', 'email', 'passwd'],
    #'subrequest' => ['list','email'],
    'subrequest' => ['list'],
    'subindex'   => ['list'],
    'ignoresub'  => ['list', '@email', '@gecos'],
    'signoff'    => ['list', 'email', 'passwd'],
    'auto_signoff'           => ['list',   'email'],
    'family_signoff'         => ['family', 'email'],
    'family_signoff_request' => ['family', 'email'],
    'sigrequest'             => ['list',   'email'],
    'set'                    => ['list',   'email', 'reception', 'gecos'],
    'serveradmin'        => ['subaction'],
    'set_session_email'  => ['email'],
    'skinsedit'          => [],
    'get_pending_lists'  => [],
    'get_closed_lists'   => [],
    'get_latest_lists'   => [],
    'get_inactive_lists' => [],
    'get_biggest_lists'  => [],
    'search_list'        => ['filter'],
    'shared'             => ['list', '@path'],       #FIXME: no such function.
    'd_read'             => ['list', '@path'],
    'latest_d_read'      => ['list'],
    'd_admin'            => ['list', 'd_admin'],
    'd_delete'           => ['list', '@path'],
    'd_rename'           => ['list', '@path'],
    'd_create_dir'       => ['list', '@path'],
    'd_overwrite'        => ['list', '@path'],
    'd_savefile'         => ['list', '@path'],
    'd_describe'         => ['list', '@path'],
    'd_editfile'         => ['list', '@path'],
    'd_properties'       => ['list', '@path'],
    'd_control'          => ['list', '@path'],
    'd_change_access'    => ['list', '@path'],
    'd_set_owner'        => ['list', '@path'],
    'dump'               => ['list', 'format'],
    'search'             => ['list', 'filter'],
    'search_user'        => ['email'],
    'set_lang'           => ['lang'],
    'attach'             => ['list', 'dir', 'file'],
    'stats'              => ['list'],
    'edit_list_request' => ['list', 'group'],
    'rename_list'       => ['list', 'new_list', 'new_robot'],
    'copy_list'         => ['list', 'new_list', 'new_robot'],
    'redirect'        => [],
    'viewlogs'        => ['list', 'page', 'size', 'sortby'],
    'wsdl'            => [],
    'sync_include'    => ['list'],
    'review_family'   => ['family_name'],
    'ls_templates'    => ['list'],
    'view_template'   => [],
    'remove_template' => [],
    'copy_template'   => ['list'],
    'edit_template'   => ['list'],
    'rss_request'     => ['list'],
    'request_topic'       => ['list', 'authkey'],
    'tag_topic_by_sender' => ['list'],
    'multiple_subscribe'  => ['lists'],
    'multiple_signoff'    => ['lists'],
    'ticket'              => ['ticket'],
    'change_email'        => ['email'],
    'manage_template' => ['subaction', 'list', 'message_template'],
    'send_newsletter' => [],
    'compose_mail'    => ['list',          'subaction'],
    'suspend_request' => ['subaction'],
    'show_exclude'    => ['list'],
    'ca'              => ['custom_action', '@cap'],
    'lca'                                => ['custom_action', 'list', '@cap'],
    'automatic_lists_management_request' => [],
    'automatic_lists_management'         => [],
    'automatic_lists_request'            => ['family'],
    'automatic_lists'                    => [],
);
root's avatar
root committed
487

488
## Define the required parameters for each action
489
490
## Parameter names refer to the %in structure of to $param if mentionned as
## 'param.x'
491
492
## This structure is used to determine if any parameter is missing
## The list of parameters is not ordered
493
494
495
## Some keywords are reserved: param.list and param.user.email
## Alternate parameters can be defined with the '|' character
## Limits of this structure: it does not define optional parameters (a or b)
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
## Limit: it does not allow to have a specific error message and redirect to a
## given page if the parameter is missing
our %required_args = (
    'active_lists'            => ['for|count'],
    'admin'                   => ['param.list', 'param.user.email'],
    'add'                     => ['param.list', 'param.user.email'],
    'add_request'             => ['param.list', 'param.user.email'],
    'arc'                     => ['param.list'],
    'arc_delete'              => ['param.user.email', 'param.list'],
    'arc_download'            => ['param.user.email', 'param.list'],
    'arc_manage'              => ['param.list'],
    'arc_protect'             => ['param.list'],
    'arcsearch'               => ['param.list'],
    'arcsearch_form'          => ['param.list'],
    'arcsearch_id'            => ['param.list'],
    'automatic_lists_request' => ['family'],
    'automatic_lists'         => ['family'],
    'attach'                  => ['param.list'],
    'blacklist'               => ['param.list'],
    'change_email'            => ['param.user.email'],
    'change_email_request'    => ['param.user.email', 'new_email'],
    'close_list'              => ['param.user.email', 'param.list'],
    'close_list_request'      => ['param.user.email', 'param.list'],
    'compose_mail'            => ['param.user.email', 'param.list'],
    'copy_template'           => ['webormail'],
    ## other required parameters are checked in the subroutine
    'create_list'         => ['param.user.email'],
    'create_list_request' => ['param.user.email'],
    'css'                 => [],
    'd_admin'             => ['param.list', 'param.user.email'],
    'd_change_access'     => ['param.list', 'param.user.email'],
    'd_control'           => ['param.list', 'param.user.email'],
    'd_create_dir'        => ['param.list', 'param.user.email', 'name_doc'],
    'd_delete'         => ['param.list', 'param.user.email'],
    'd_describe'       => ['param.list', 'param.user.email', 'content'],
    'd_editfile'       => ['param.list', 'param.user.email'],
    'd_install_shared' => ['param.list', 'param.user.email', 'id'],
    'd_overwrite'      => ['param.list', 'param.user.email'],
    'd_properties'     => ['param.list', 'param.user.email'],
    'd_read'          => ['param.list'],
    'd_reject_shared' => ['param.list', 'param.user.email', 'id'],
    'd_rename'        => ['param.list', 'param.user.email', 'new_name'],
    'd_savefile'      => ['param.list', 'param.user.email', 'content|url'],
    'd_set_owner'     => ['param.list', 'param.user.email'],
    'd_unzip'         => ['param.list', 'param.user.email'],
    'd_upload'        => ['param.list', 'param.user.email'],
    'del'             => ['param.list', 'param.user.email', 'email'],
    'delete_pictures' => ['param.list', 'param.user.email'],
    'distribute'      => ['param.list', 'param.user.email', 'id|idspam'],
    'add_frommod'     => ['param.list', 'param.user.email', 'id'],
    'dump'               => ['param.list'],
    'dump_scenario'      => ['param.list', 'pname'],
    'edit_list'          => ['param.user.email', 'param.list'],
    'edit_list_request'  => ['param.user.email', 'param.list'],
    'edit_template'      => ['webormail'],
    'editfile'           => ['param.user.email'],
    'editsubscriber'     => ['param.list', 'param.user.email', 'email'],
    'get_closed_lists'   => ['param.user.email'],
    'get_inactive_lists' => ['param.user.email'],
    'get_latest_lists'   => ['param.user.email'],
    'get_biggest_lists'  => ['param.user.email'],
    'get_pending_lists'  => ['param.user.email'],
    'ignoresub'            => ['param.list', 'param.user.email'],
    'info'                 => ['param.list'],
    'install_pending_list' => ['param.user.email'],
    'edit_config'          => ['param.user.email'],
    'latest_arc'           => ['param.list', 'for|count'],
    'latest_d_read' => ['param.list', 'for', 'count'],
    'latest_lists'  => ['for|count'],
    'load_cert'     => ['param.list'],
    'logout'        => ['param.user.email'],
    'manage_template'    => ['param.list',       'param.user.email'],
    'modindex'           => ['param.list',       'param.user.email'],
    'multiple_subscribe' => ['param.list'],
    'pref'               => ['param.user.email'],
    'purge_list'         => ['param.user.email', 'selected_lists'],
    'rebuildallarc'      => ['param.user.email'],
    'rebuildarc'         => ['param.user.email', 'param.list'],
    'reject'          => ['param.list', 'param.user.email', 'id|idspam'],
    'remind'          => ['param.list', 'param.user.email'],
    'remove_arc'      => ['param.list'],
    'remove_template' => ['webormail'],
    'rename_list' =>
        ['param.user.email', 'param.list', 'new_listname', 'new_robot'],
    'copy_list' =>
        ['param.user.email', 'param.list', 'new_listname', 'new_robot'],
    'rename_list_request' => ['param.user.email', 'param.list'],
    'request_topic'       => ['param.list',       'authkey'],
    'resetbounce'  => ['param.list',       'param.user.email', 'email'],
    'restore_list' => ['param.user.email', 'param.list'],
    'review'          => ['param.list'],
    'review_family'   => ['param.user.email', 'family_name'],
    'reviewbouncing'  => ['param.list'],
    'rss_request'     => [],
    'savefile'        => ['param.user.email', 'file'],
    'search'          => ['param.list', 'filter'],
    'search_user'     => ['param.user.email', 'email'],
    'send_mail'       => ['param.user.email'],
    'send_newsletter' => ['param.list', 'param.user.email', 'url'],
    'send_me'         => ['param.list'],
    'view_source'     => ['param.list'],
    'tracking'        => ['param.list'],
    'requestpasswd'   => ['email'],
    'serveradmin'     => ['param.user.email'],
    'set'      => ['param.list', 'reception|visibility'],
    'set_lang' => [],
    'set_pending_list_request' => ['param.user.email'],
    'setpasswd'  => ['param.user.email', 'newpasswd1', 'newpasswd2'],
    'setpref'    => ['param.user.email'],
    'signoff'    => ['param.list'],
    'sigrequest' => ['param.list'],
    'skinsedit'  => ['param.user.email'],
    'sso_login'  => ['auth_service_name'],
    'stats'      => ['param.list'],
    'subindex'               => ['param.list',       'param.user.email'],
    'suboptions'             => ['param.list',       'param.user.email'],
    'subrequest'             => ['param.list'],
    'subscribe'              => ['param.list'],
    'subscriber_count'       => ['param.list'],
    'suspend_request'        => [],
    'suspend_request_action' => [],
    'show_exclude'           => ['param.list'],
    'sync_include'           => ['param.list',       'param.user.email'],
    'tag_topic_by_sender'    => ['param.list'],
    'upload_pictures'        => ['param.user.email', 'param.list'],
    'view_template'          => ['webormail'],
    'viewbounce'             => ['param.list',       'email'],
    'viewfile'               => ['file',             'param.list'],
    'viewlogs'               => ['param.list'],
    'viewmod' => ['param.list', 'param.user.email', 'id|idspam'],
    'wsdl'    => [],
    'which'   => ['param.user.email'],
);
629
630
631

## Defines the required privileges to access privileged actions
## You can define a set ofequiivalent privileges in the ARRAYREF
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
our %required_privileges = (
    'admin'              => ['owner',  'editor'],
    'arc_delete'         => ['owner'],
    'arc_download'       => ['owner'],
    'arc_manage'         => ['owner'],
    'blacklist'          => ['owner',  'editor'],
    'close_list'         => ['privileged_owner'],
    'close_list_request' => ['privileged_owner'],
    'copy_template'      => ['listmaster'],
    'd_install_shared'   => ['editor', 'owner'],
    'd_reject_shared'    => ['editor', 'owner'],
    'distribute'        => ['editor', 'owner', 'listmaster'],
    'add_frommod'       => ['editor', 'owner'],
    'dump_scenario'     => ['listmaster'],
    'edit_list'         => ['owner'],
    'edit_list_request' => ['owner'],
    'edit_template'     => ['listmaster'],
    'editsubscriber'       => ['owner', 'editor'],
    'get_closed_lists'     => ['listmaster'],
    'get_inactive_lists'   => ['listmaster'],
    'get_latest_lists'     => ['listmaster'],
    'get_biggest_lists'    => ['listmaster'],
    'get_pending_lists'    => ['listmaster'],
    'ignoresub'            => ['owner', 'editor'],
    'install_pending_list' => ['listmaster'],
    'edit_config'          => ['listmaster'],
    'ls_templates'         => ['listmaster'],
    'manage_template'      => ['owner'],
    'modindex'        => ['editor',           'owner', 'listmaster'],
    'purge_list'      => ['privileged_owner', 'listmaster'],
    'rebuildallarc'   => ['listmaster'],
    'rebuildarc'      => ['listmaster'],
    'reject'          => ['editor',           'owner', 'listmaster'],
    'remove_template' => ['listmaster'],
    'rename_list'     => ['privileged_owner'],
    'copy_list'                => ['owner', 'listmaster'],
    'rename_list_request'      => ['privileged_owner'],
    'resetbounce'              => ['owner', 'editor'],
    'restore_list'             => ['listmaster'],
    'review_family'            => ['listmaster'],
    'reviewbouncing'           => ['owner', 'editor'],
    'search_user'              => ['listmaster'],
    'serveradmin'              => ['listmaster'],
    'set_dumpvars'             => ['listmaster'],
    'set_loglevel'             => ['listmaster'],
    'set_pending_list_request' => ['listmaster'],
    'set_session_email'        => ['listmaster'],
    'show_sessions'            => ['listmaster'],
    'stats'                    => ['owner'],
    'subindex'                 => ['owner', 'editor'],
    'sync_include'             => ['owner', 'editor'],
    'skinsedit'                => ['listmaster'],
    'view_template'            => ['listmaster'],
    'viewbounce'               => ['owner', 'editor'],
    'viewlogs'                 => ['owner', 'editor'],
    'viewmod' => ['editor', 'owner', 'listmaster'],
    'automatic_lists_management_request' => ['listmaster'],
    'automatic_lists_management'         => ['listmaster'],
);

# this definition is used to choose the left side menu type (admin ->
# listowner admin menu | serveradmin -> server_admin menu | none list or
# your_list menu)
695
my %action_type = (
696
697
698
699
700
701
702
    'review'      => 'admin',
    'search'      => 'admin',
    'viewfile'    => 'admin',
    'admin'       => 'admin',
    'add_request' => 'admin',
    'add'         => 'admin',
    'del'         => 'admin',
sympa-authors's avatar
   
sympa-authors committed
703
#		'modindex' =>'admin',
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
    'reject'             => 'admin',
    'reject_notify'      => 'admin',
    'distribute'         => 'admin',
    'add_frommod'        => 'admin',
    'viewmod'            => 'admin',
    'savefile'           => 'admin',
    'rebuildallarc'      => 'admin',    #FIXME: serveradmin?
    'reviewbouncing'     => 'admin',
    'edit_list_request'  => 'admin',
    'edit_list'          => 'admin',
    'editsubscriber'     => 'admin',
    'viewbounce'         => 'admin',
    'resetbounce'        => 'admin',
    'scenario_test'      => 'admin',
    'close_list_request' => 'admin',
    'close_list'         => 'admin',
    'restore_list'       => 'admin',
    'd_admin'            => 'admin',
    'd_reject_shared'    => 'admin',
    'd_install_shared'   => 'admin',
    'dump_scenario'      => 'admin',
    'dump'               => 'admin',
    'remind'             => 'admin',
sympa-authors's avatar
   
sympa-authors committed
727
#		'subindex' => 'admin',
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
    'stats'                              => 'admin',
    'ignoresub'                          => 'admin',
    'rename_list'                        => 'admin',
    'copy_list'                          => 'admin',
    'rename_list_request'                => 'admin',
    'arc_manage'                         => 'admin',
    'sync_include'                       => 'admin',
    'view_template'                      => 'admin',
    'remove_template'                    => 'admin',
    'copy_template'                      => 'admin',
    'edit_template'                      => 'admin',
    'blacklist'                          => 'admin',
    'viewlogs'                           => 'admin',
    'serveradmin'                        => 'serveradmin',
    'get_pending_lists'                  => 'serveradmin',
    'get_closed_lists'                   => 'serveradmin',
    'get_inactive_lists'                 => 'serveradmin',
    'get_latest_lists'                   => 'serveradmin',
    'get_biggest_lists'                  => 'serveradmin',
    'ls_templates'                       => 'serveradmin',
    'skinsedit'                          => 'serveradmin',
    'review_family'                      => 'serveradmin',
    'search_user'                        => 'serveradmin',
    'show_sessions'                      => 'serveradmin',
    'show_exclude'                       => 'admin',
    'rebuildarc'                         => 'serveradmin',
    'set_session_email'                  => 'serveradmin',
    'set_loglevel'                       => 'serveradmin',
    'editfile'                           => 'serveradmin',    #FIXME: admin?
    'unset_dumpvars'                     => 'serveradmin',
    'set_dumpvars'                       => 'serveradmin',
    'automatic_lists_management_request' => 'serveradmin',
    'automatic_lists_management'         => 'serveradmin',
761
);
root's avatar
root committed
762

763
## actions tthat are not used in return of login,
764
765
766
767
768
769
770
771
772
773
774
775
776
my %temporary_actions = (
    'logout'              => 1,
    'loginrequest'        => 1,
    'login'               => 1,
    'sso_login'           => 1,
    'sso_login_succeeded' => 1,
    'ticket'              => 1,
    'css'                 => 1,
    'rss'                 => 1,    # FIXME:currently not used.
    'ajax'                => 1,
    'wsdl'                => 1,
    'redirect'            => 1,
);
777

778
779
780
## Regexp applied on incoming parameters (%in)
## The aim is not a strict definition of parameter format
## but rather a security check
781
our %in_regexp = (
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
    ## Default regexp
    '*' => '[\w\-\.]+',

    ## List config parameters
    'single_param'   => '.+',
    'multiple_param' => '.+',

    ## Textarea content
    'template_content'     => '.+',
    'content'              => '.+',
    'body'                 => '.+',
    'info'                 => '.+',
    'new_scenario_content' => '.+',
    'blacklist'            => '.*',

    ## Integer
    'page' => '\d+',
    'size' => '\d+',

    ## Free data
    'subject'          => '.*',
    'gecos'            => '[^<>\\\*\$\n]+',
    'fromname'         => '[^<>\\\*\$\n]+',
    'additional_field' => '[^<>\\\*\$\n]+',
    'dump'             => '[^<>\\\*\$]+',     # contents email + gecos

    ## Search
    'filter'   => '[^<>\\\[\]\(\)\$\n]+',     # search list
    'key_word' => '.*',
    'format'   => '[^<>\\\$\n]+',             # dump format/filter string

    ## File names
    'file'          => '[^<>\*\$\n]+',
    'template_path' => '[\w\-\.\/_]+',
    'arc_file'      => '[^<>\\\*\$\n]+',
    'path'          => '[^<>\\\*\$\n]+',
    'uploaded_file' =>
        '(.*[\/\\\\])?[^<>\*\$\n]+',          # Could be precised (use of "'")
    'unzipped_file'     => '(.*[\/\\\\])?[^<>\*\$\n]+',
    'dir'               => '[^<>\\\*\$\n]+',
    'name_doc'          => '[^<>\\\*\$\[\]\/\n]+',
    'shortname'         => '[^<>\\\*\$\n]+',
    'new_name'          => '[^<>\\\*\$\n]+',
    'id'                => '[^<>\\\*\$\n]+',
    'template_name'     => Sympa::Regexps::template_name(),
    'new_template_name' => Sympa::Regexps::template_name(),
    'message_template'  => Sympa::Regexps::template_name(),
    'new_default'       => Sympa::Regexps::template_name(),

    ## Archives
    ## format is yyyy-mm for 'arc' and mm for 'send_me'
    'month' => '\d{2}|\d{4}\-\d{2}',

    ## URL
    'referer'         => '[^\\\$\*\"\'\`\^\|\<\>\n]+',
    'failure_referer' => '[^\\\$\*\"\'\`\^\|\<\>\n]+',
    'url'             => '[^\\\$\*\"\'\`\^\|\<\>\n]+',

    ## Msg ID
    'msgid'       => '[^\\\*\"\'\`\^\|\n]+',
    'in_reply_to' => '[^\\\*\"\'\`\^\|\n]+',
    'message_id'  => '[^\\\*\"\'\`\^\|\n]+',

    ## Password
    'passwd'       => '.+',
    'password'     => '.+',
    'newpasswd1'   => '.+',
    'newpasswd2'   => '.+',
    'new_password' => '.+',

    ## Topics
853
    'topic'    => '\@?[\-\w\/]+',
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
    'topics'   => '[\-\w\/]+',
    'subtopic' => '[\-\w\/]+',

    ## List names
    'list' => '[\w\-\.\+]*',    ## Sympa::Regexps::listname() + uppercase
    'previous_list'  => '[\w\-\.\+]*',
    'new_list'       => '[\w\-\.\+]*',
    'listname'       => '[\w\-\.\+]*',
    'new_listname'   => '[\w\-\.\+]*',
    'selected_lists' => '[\w\-\.\+]*',

    ## Family names
    'family_name' => Sympa::Regexps::family_name(),
    'family'      => Sympa::Regexps::family_name(),

    ## Email addresses
    'email'         => Sympa::Regexps::email() . '|' . Sympa::Regexps::uid(),
    'init_email'    => Sympa::Regexps::email(),
    'old_email'     => Sympa::Regexps::email(),
    'new_email'     => Sympa::Regexps::email(),
    'pending_email' => Sympa::Regexps::email()
        . ',.*',    # Email address is followed by ',' + gecos data
    'sender'   => Sympa::Regexps::email(),
    'fromaddr' => Sympa::Regexps::email(),
    'to'       => '(([\w\-\_\.\/\+\=\']+|\".*\")\s[\w\-]+(\.[\w\-]+)+(,?))*',
    'automatic_list_part_*' => '[\w\-\.\+]*',

    ## Host
    'new_robot'   => Sympa::Regexps::host(),
    'remote_host' => Sympa::Regexps::host(),
    'remote_addr' => Sympa::Regexps::host(),

    ## Scenario name
    'scenario'    => Sympa::Regexps::scenario(),
    'read_access' => Sympa::Regexps::scenario(),
    'edit_access' => Sympa::Regexps::scenario(),
    ## RSS URL or blank
    'active_lists'  => '.*',
    'latest_lists'  => '.*',
    'latest_arc'    => '.*',
    'latest_d_read' => '.*',

    ##Logs
    'target_type' => '[\w\-\.\:]*',
    'target'      => Sympa::Regexps::email(),
    'date_from'   => '[\d\/\-]+',
    'date_to'     => '[\d\/\-]+',
    'ip'          => Sympa::Regexps::host(),

    ## colors
    'subaction_test'      => '.*',
    'subaction_reset'     => '.*',
    'subaction_install'   => '.*',
    'custom_color_value'  => '\#[0-9a-fA-F]+',
    'custom_color_number' => 'color_\w+',

    ## Custom attribute
    'custom_attribute' => '.*',

    ## Templates
    'scope' => 'distrib|robot|family|list|site',

    ## Custom Inputs from create_list_request.tt2
    'custom_input' => '.*',

    ## conf parameters
    'conf_new_value' => '.*',

    ## custom actions
    'cap'  => '.*',
    'lcap' => '.*',

    'plugin' => '.*',
);
928

929
## Regexp applied on incoming parameters (%in)
930
931
932
933
934
935
936
937
## This regular expression defines forbidden expressions applied on all
## incoming parameters
## Note that you can use the ^ and $ expressions to match beginning and ending
## of expressions
our %in_negative_regexp = ('arc_file' => '^(arctxt|\.)');

## List some required filtering of incoming parameters, depending on current
## action
938
## Paramater can be '*' or 'param*'
939
## Like Q-encoding
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
my %filtering = (
    'd_reject_shared'  => {'id'       => 'qencode'},
    'd_install_shared' => {'id'       => 'qencode'},
    'd_read'           => {'path'     => 'qencode'},
    'd_create_dir'     => {'name_doc' => 'qencode', 'path' => 'qencode'},
    'd_upload'         => {'path'     => 'qencode'},
    'd_unzip'          => {'path'     => 'qencode'},
    'd_editfile'       => {'path'     => 'qencode'},
    'd_properties'     => {'path'     => 'qencode'},
    'd_overwrite'      => {'path'     => 'qencode'},
    'd_savefile'       => {'path'     => 'qencode', 'name_doc' => 'qencode'},
    'd_describe'       => {'path'     => 'qencode'},
    'd_delete'         => {'path'     => 'qencode'},
    'd_rename'         => {'path'     => 'qencode', 'new_name' => 'qencode'},
    'd_control'        => {'path'     => 'qencode'},
    'd_change_access'  => {'path'     => 'qencode'},
    'd_set_owner'      => {'path'     => 'qencode'},
    'requestpasswd'    => {'email'    => 'fix_escape_uri'},
    'viewbounce'       => {'email'    => 'fix_escape_uri'},
    'editsubscriber'   => {'email'    => 'fix_escape_uri'},
    ## Required because outgoing parameters have been html-escaped in
    ## edit_list_request
    'edit_list' => {'*param*' => 'unescape_html'},
    ## Remove leading/trailing white spaces and lowercase
    'change_email' => {'*email' => 'normalize'},
);

## Set locale configuration
968
969
my $language = Sympa::Language->instance;
$language->set_lang($Conf::Conf{'lang'}, 'en');
salaun's avatar
salaun committed
970

971
972
## Important to leave this there because it defined defaults for
## user_data_source
sikeda's avatar
sikeda committed
973
SDM::check_db_connect();
974

975
976
977
978
# Now, load all components which are implemented as plugins.  Those will
# add fields to above configuration and may do database upgrades.
$plugins->start if $plugins;

979
980
981
## Check that the data structure is uptodate
## If not, set the web interface to maintenance mode
my $maintenance_mode;
982
unless (SDM::data_structure_uptodate()) {
983
    $maintenance_mode = 1;
984
985
986
    Log::do_log('err',
        "Web interface set to maintenance mode ; you should run sympa.pl --upgrade"
    );
987
988
}

sikeda's avatar
sikeda committed
989
tools::ciphersaber_installed();
990

991
our %changed_params;
salaun's avatar
salaun committed
992

993
994
our %in;
my $query;
root's avatar
root committed
995

996
my $birthday = time;
997

998
Log::do_log('info', 'WWSympa started, process %d', $PID);
999

1000
1001
1002
1003
# Now internal encoding is same as input/output.
#XXX## Set output encoding
#XXX## All outgoing strings will be recoded transparently using this charset
#XXXbinmode STDOUT, ":utf8";
1004

1005
1006
#XXX## Incoming data is utf8-encoded
#XXXbinmode STDIN, ":utf8";
1007

1008
1009
1010
1011
## Main loop
my $loop_count;
my $start_time = time;
while ($query = new_loop()) {
1012

1013
    undef %::changed_params;
root's avatar
root committed
1014

1015
1016
1017
1018
1019
1020
1021
1022
1023
    undef $param;
    undef $list;
    undef $robot;
    undef $robot_object;
    undef $pinfo;
    undef $ip;
    undef $rss;
    undef $ajax;
    undef $session;
1024

1025
1026
    Log::set_log_level($Conf::Conf{'log_level'});
    $language->set_lang($Conf::Conf{'lang'}, 'en');
1027

1028
    ## Empty cache of the List.pm module
1029
1030
    Sympa::List::init_list_cache();

1031
    # Process grouped notifications
1032
    Sympa::Alarm::flush();
1033
1034

    ## Check effective ID
1035
    unless ($EUID eq (getpwnam(Sympa::Constants::USER))[2]) {
1036
        $maintenance_mode = 1;
sikeda's avatar
sikeda committed
1037
1038
        Sympa::Report::reject_report_web('intern_quiet',
            'incorrect_server_config', {}, '', '');
1039
1040
        wwslog(
            'err',
1041
            'Config error: WWSympa should run with UID %s (instead of %s). *** Switching to maintenance mode. ***',
1042
            (getpwnam(Sympa::Constants::USER))[2],
1043
            $EUID
1044
1045
        );
    }
1046

1047
1048
1049
    ## We set the real UID with the effective UID value
    ## It is usefull to allow execution of scripts like alias_manager
    ## that otherwise might loose the benefit of SetUID
1050
1051
    $UID = $EUID;    ## UID
    $GID = $EGID;    ## GID
1052

sikeda's avatar
sikeda committed
1053
    unless (SDM::check_db_connect()) {
sikeda's avatar
sikeda committed
1054
1055
        Sympa::Report::reject_report_web('system_quiet', 'no_database', {},
            '', '');
1056
1057
        Log::do_log('info', 'WWSympa requires a RDBMS to run');
    }
1058

1059
1060
1061
    ## If in maintenance mode, check if the data structure is now uptodate
    if ($maintenance_mode
        && (SDM::data_structure_uptodate()
1062
            && ($EUID eq (getpwnam(Sympa::Constants::USER))[2]))
1063
1064
1065
1066
1067
        ) {
        $maintenance_mode = undef;
        Log::do_log('notice',
            "Data structure seem updated, setting OFF maintenance mode");
    }
1068

1069
    ## Generate traceback if crashed.
1070
1071
    ## Though I don't know why, __DIE__ handler is cleared after INIT.
    Sympa::Crash::register_handler();
1072

1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
    ## Get params in a hash
    #    foreach ($query->param) {
    #      $in{$_} = $query->param($_);
    #    }
    %in = $query->Vars;

    foreach my $k (keys %::changed_params) {
        Log::do_log('debug3', 'Changed Param: %s', $k);
    }

    ## Free terminated sendmail processes
1084
    #    Sympa::Mail::reaper();
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113

    ## Parse CGI parameters
    #    &CGI::ReadParse();

    if (defined $Conf::Conf{'robot_by_http_host'}
        {get_header_field('SERVER_NAME')}) {
        my ($selected_robot, $selected_path);
        my ($k,              $v);
        while (
            ($k, $v) = each %{
                $Conf::Conf{'robot_by_http_host'}
                    {get_header_field('SERVER_NAME')}
            }
            ) {
            if ($ENV{'REQUEST_URI'} =~ /^$k/) {
                ## Longer path wins
                if (length($k) > length($selected_path)) {
                    ($selected_robot, $selected_path) = ($v, $k);
                }
            }
        }
        $robot = $selected_robot;
    }

    $robot = $Conf::Conf{'host'} unless $robot;

    # Not yet implemented
    ### Create Robot object
    #$robot_object = new Robot $robot;
1114
    $pinfo = tools::get_list_params($robot);
1115

1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
    ## Default robot
    if ($robot eq $Conf::Conf{'host'}) {
        $param->{'default_robot'} = 1;
    }

    $param->{'cookie_domain'} = Conf::get_robot_conf($robot, 'cookie_domain');

    $ip = $ENV{'REMOTE_HOST'};
    $ip = $ENV{'REMOTE_ADDR'} unless ($ip);
    $ip = 'undef' unless ($ip);
    ## In case HTTP_HOST does not match cookie_domain
sikeda's avatar
sikeda committed
1127
    my $http_host = get_header_field('HTTP_HOST');
1128
1129
1130
1131
1132
    $http_host =~ s/:\d+$//;    ## suppress port
    unless (($http_host =~ /$param->{'cookie_domain'}$/)
        || ($param->{'cookie_domain'} eq 'localhost')) {
        wwslog(
            'notice',
1133
1134
            '(%s) Does NOT match HTTP_HOST; setting cookie_domain to %s',
            $param->{'cookie_domain'}, $http_host
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
        );
        $param->{'cookie_domain'} = $http_host;
    }
    Log::set_log_level(Conf::get_robot_conf($robot, 'log_level'));

    ## Sympa parameters in $param->{'conf'}
    $param->{'conf'} = {};
    foreach my $p (
        'email',
        'host',
        'sympa',
        'request',
        'soap_url',
        'wwsympa_url',
        'listmaster_email',
        'logo_html_definition',
        'favicon_url',
        'main_menu_custom_button_1_url',
        'main_menu_custom_button_1_title',
        'main_menu_custom_button_1_target',
        'main_menu_custom_button_2_url',
        'main_menu_custom_button_2_title',
        'main_menu_custom_button_2_target',
        'main_menu_custom_button_3_url',
        'main_menu_custom_button_3_title',
        'main_menu_custom_button_3_target',
        'dark_color',
        'light_color',
        'text_color',
        'bg_color',
        'error_color',
        'use_blacklist',
        'antispam_feature',
        'custom_robot_parameter',
        'selected_color',
        'shaded_color',
        'color_0',
        'color_1',
        'color_2',
        'color_3',
        'color_4',
        'color_5',
        'color_6',
        'color_7',
        'color_8',
        'color_9',
        'color_10',
        'color_11',
        'color_12',
        'color_13',
        'color_14',
        'color_15',
        'reporting_spam_script_path',
        'automatic_list_families',
1189
        'your_lists_size',
1190
1191
1192
1193
1194
1195
1196
1197
        ) {

        $param->{'conf'}{$p} = Conf::get_robot_conf($robot, $p);
        $param->{$p} = Conf::get_robot_conf($robot, $p)
            if (($p =~ /_color$/) || ($p =~ /color_/));
    }

    foreach my $auth (keys %{$Conf::Conf{'cas_id'}{$robot}}) {
1198
        Log::do_log('debug2', 'CAS authentication service %s', $auth);
1199
1200
1201
1202
1203
1204
        $param->{'sso'}{$auth} =
            $Conf::Conf{'cas_id'}{$robot}{$auth}
            {'auth_service_friendly_name'};
    }

    foreach my $auth (keys %{$Conf::Conf{'generic_sso_id'}{$robot}}) {
1205
        Log::do_log('debug', 'Generic SSO authentication service %s', $auth);
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
        $param->{'sso'}{$auth} =
            $Conf::Conf{'auth_services'}{$robot}
            [$Conf::Conf{'generic_sso_id'}{$robot}{$auth}]{'service_name'};
    }

    $param->{'sso_number'} =
        $Conf::Conf{'cas_number'}{$robot} +
        $Conf::Conf{'generic_sso_number'}{$robot};
    $param->{'use_passwd'} = $Conf::Conf{'use_passwd'}{$robot};
    $param->{'use_sso'} = 1 if ($param->{'sso_number'});
    $param->{'authentication_info_url'} =
        $Conf::Conf{'authentication_info_url'}{$robot};
    $param->{'wwsconf'} = Conf::_load_wwsconf;    #FXIME: no longer used?

    $param->{'path_cgi'} = $ENV{'SCRIPT_NAME'};
    ## Replace '//' with '/' because it would break navigation
    $param->{'path_cgi'} =~ s/\/\//\//g;
    $param->{'version'} = Sympa::Constants::VERSION;
    $param->{'date'} =
        $language->gettext_strftime("%d %b %Y at %H:%M:%S", localtime time);
    $param->{'time'} =
        $language->gettext_strftime("%H:%M:%S", localtime time);

    ## Hash defining the parameters where no control is performed (because
    ## they are supposed to contain html and/or javascript).
    $param->{'htmlAllowedParam'} = {
        'title'                => 1,
        'hidden_head'          => 1,
        'hidden_end'           => 1,
        'hidden_at'            => 1,
        'list_protected_email' => 1,
        'selected'             => 1,
        'author_mailto'        => 1,
        'mailto'               => 1,
        'logo_html_definition' => 1,
        'template_content'     => 1,
        'html_dumpvars'        => 1,
        'html_editor_init'     => 1,
    };
    ## Hash defining the parameters where HTML must be filtered.
    $param->{'htmlToFilter'} = {
        'homepage_content' => 1,
        'info_content'     => 1,
    };

    ## Change to list root
1252
    unless (chdir $Conf::Conf{'home'}) {
sikeda's avatar
sikeda committed
1253
1254
        Sympa::Report::reject_report_web('intern', 'chdir_error', {}, '', '',
            '', $robot);
1255
        wwslog('info', 'Unable to change directory');
1256
        exit -1;
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
    }

    ## Sets the UMASK
    umask(oct($Conf::Conf{'umask'}));

    ## Authentication
    ## use https client certificat information if define.

    ## Default auth method (for scenarios)
    $param->{'auth_method'} = 'md5';

1268
    Sympa::Report::init_report_web();
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295

    ## Get PATH_INFO parameters
    get_parameters();

    # Propagate plugins parameters
    $param->{'plugin'} = $in{'plugin'};

    # Static content
    $param->{'static_content_url'} =
        Conf::get_robot_conf($robot, 'static_content_url');

    ## CSS related
    $param->{'css_path'} = Conf::get_robot_conf($robot, 'css_path');
    $param->{'css_url'}  = Conf::get_robot_conf($robot, 'css_url');
    ## If CSS file not found, let Sympa do the job...
    unless (-f $param->{'css_path'} . '/style.css') {
        wwslog(
            'err',
            'Could not find CSS file %s, using default CSS',
            $param->{'css_path'} . '/style.css'
        ) if ($param->{'css_path'});    ## Notice only if path was defined
        $param->{'css_url'} =
            $param->{'base_url'} . $param->{'path_cgi'} . '/css';
    }

    wwslog(
        'info',
1296
        'Parameter css_url "%s" seems strange, it must be the url of a directory not a css file',
1297
1298
1299
        $param->{'css_url'}
    ) if ($param->{'css_url'} =~ /\.css$/);

1300
    $session = Sympa::Session->new(
1301
        $robot,
sikeda's avatar
sikeda committed
1302
1303
        {   'cookie' =>
                Sympa::Session::get_session_cookie($ENV{'HTTP_COOKIE'}),
1304
1305
1306
1307
1308
1309
            'action' => $in{'action'},
            'rss'    => $rss,
            'ajax'   => $ajax
        }
    );

sikeda's avatar
sikeda committed
1310
1311
1312
    # Getting rid of the environment variable to make sure it won't be
    # affected to another anonymous session.
    undef $ENV{'HTTP_COOKIE'};
1313
    unless (defined $session) {
1314
1315
        tools::send_notify_to_listmaster($robot,
            'failed_to_create_web_session', {});
1316
        wwslog('info', 'Failed to create session');
1317
        $session = Sympa::Session->new($robot, {});
1318
1319
1320
1321
1322
    }

    # Retreive oauth_token from session if it exists and if we have a (newly)
    # identified user
    my $oauth_token = $session->{'oauth_token'} || '';
1323
1324
    $in{'oauth_token'} = delete $session->{'oauth_token'}
        if $oauth_token ne '' && $session->{'email'} ne 'nobody';
1325

1326
    $session->{'is_family_owner'} = undef;
1327
1328
    my $automatic_list_families =
        Conf::get_robot_conf($robot, 'automatic_list_families');
1329
    if (defined $automatic_list_families) {
1330
1331
        foreach my $key (keys %{$automatic_list_families}) {
            my $family;
1332
            if ($family = Sympa::Family->new($key, $robot)) {
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
                if ($family->is_allowed_to_create_automatic_lists(
                        (   'auth_level' => 'md5',
                            'sender'     => $session->{'email'},
                            'message'    => undef,
                            'listname'   => ''
                        )
                    )
                    ) {
                    $session->{'is_family_owner'}{$key} = 1;
                } else {
                    $session->{'is_family_owner'}{$key} = undef;
                }
            }
        }
1347
1348
    }

1349
    $param->{'session'} = $session->as_hashref();
1350

1351
1352
1353
1354
    Log::set_log_level($session->{'log_level'}) if ($session->{'log_level'});
    $param->{'restore_email'}         = $session->{'restore_email'};
    $param->{'dumpvars'}              = $session->{'dumpvars'};
    $param->{'unauthenticated_email'} = $session->{'unauthenticated_email'};
1355

1356
    ## testing custom CSS
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
    if ($session->{'custom_css'}) {
        foreach my $i (0 .. 15) {
            $param->{'color_' . $i} = $session->{'color_' . $i}
                if $session->{'color_' . $i};
        }
    }

    ## RSS does not require user authentication
    unless ($rss) {

        if (   ($ENV{'SSL_CLIENT_VERIFY'} eq 'SUCCESS')
            && ($in{'action'} ne 'sso_login'))
        {    ## Do not check client certificate automatically if in sso_login

            Log::do_log(
                'debug2',
1373
                'SSL verified, S_EMAIL = %s, " . " S_DN_Email = %s',
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
                $ENV{'SSL_CLIENT_S_EMAIL'},
                $ENV{'SSL_CLIENT_S_DN_Email'}
            );
            if (($ENV{'SSL_CLIENT_S_EMAIL'})) {
                ## this is the X509v3 SubjectAlternativeName, and requires
                ## a patch to mod_ssl -- cm@coretec.at
                $param->{'user'}{'email'} = lc($ENV{'SSL_CLIENT_S_EMAIL'});
            } elsif ($ENV{SSL_CLIENT_S_DN_Email}) {
                $param->{'user'}{'email'} = lc($ENV{'SSL_CLIENT_S_DN_Email'});
            } elsif ($ENV{'SSL_CLIENT_S_DN'} =~ /\+MAIL=([^\+\/]+)$/) {
                ## Compatibility issue with old a-sign.at certs
                $param->{'user'}{'email'} = lc($1);
1386
1387
            } elsif ($Crypt::OpenSSL::X509::VERSION
                and exists($ENV{SSL_CLIENT_CERT})) {
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
                ## this is the X509v3 SubjectAlternativeName, and does only
                ## require "SSLOptions +ExportCertData" without patching
                ## mod_ssl -- massar@unix-ag.uni-kl.de
                $param->{'user'}{'email'} = lc(
                    Crypt::OpenSSL::X509->new_from_string(
                        $ENV{SSL_CLIENT_CERT}
                        )->email()
                );
            }

            if ($param->{user}{email}) {
                $session->{'email'}          = $param->{user}{email};
                $param->{'auth_method'}      = 'smime';
                $session->{'auth'}           = 'x509';
                $param->{'ssl_client_s_dn'}  = $ENV{'SSL_CLIENT_S_DN'};
                $param->{'ssl_client_v_end'} = $ENV{'SSL_CLIENT_V_END'};
                $param->{'ssl_client_i_dn'}  = $ENV{'SSL_CLIENT_I_DN'};
                $param->{'ssl_cipher_usekeysize'} =
                    $ENV{'SSL_CIPHER_USEKEYSIZE'};
            }

        } elsif (($session->{'email'}) && ($session->{'email'} ne 'nobody')) {
            $param->{'user'}{'email'} = $session->{'email'};
        } elsif ($in{'ticket'} =~ /(S|P)T\-/)
        {  # the request contain a CAS named ticket that use CAS ticket format
            delete $session->{'do_not_use_cas'
                };  #reset do_not_use_cas because this client probably use CAS
            # select the cas server that redirect the user to sympa and check
            # the ticket
            Log::do_log('notice',
                "CAS ticket is detected. in{'ticket'}=$in{'ticket'} checked_cas=$session->{'checked_cas'}"
            );

            my $cas_id = '';
            if ($in{'checked_cas'} =~ /^(\d+)\,?/) {
                $cas_id = $1;
            } elsif ($session->{'checked_cas'} =~ /^(\d+)\,?/) {
                $cas_id = $1;
            }
            if ($cas_id ne '') {

                my $ticket = $in{'ticket'};
                my $cas_server =
                    $Conf::Conf{'auth_services'}{$robot}[$cas_id]
                    {'cas_server'};

                my $service_url = wwslib::get_my_url();
                $service_url =~ s/(\?|&)ticket\=.+$//;

                my $net_id = $cas_server->validateST($service_url, $ticket);

                if (defined $net_id) {    # the ticket is valid net-id
1440
1441
                    Log::do_log('notice', 'Login CAS OK server netid=%s',
                        $net_id);
1442
                    $param->{'user'}{'email'} = lc(
1443
                        Sympa::Auth::get_email_by_net_id(
1444
1445
1446
1447
1448
1449
1450
1451
1452
                            $robot, $cas_id, {'uid' => $net_id}
                        )
                    );
                    $session->{'auth'}  = 'cas';
                    $session->{'email'} = $param->{user}{email};

                    $session->{'cas_server'} = $cas_id;

                } else {
1453
                    Log::do_log('err', 'CAS ticket validation failed: %s',
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
                        AuthCAS::get_errors());
                }
            } else {
                Log::do_log('notice',
                    "Internal error while receiving a CAS ticket $session->{'checked_cas'} "
                );
            }
        } elsif (($Conf::Conf{'cas_number'}{$robot} > 0)
            && ($in{'action'} !~ /^(login|sso_login|wsdl)$/))
        {    # some cas server are defined but no CAS ticket detected
            unless ($session->{'do_not_use_cas'}) {
                # user not taggued as not using cas
                foreach
                    my $auth_service (@{$Conf::Conf{'auth_services'}{$robot}})
                {
                    # skip auth services not related to cas
                    next
                        unless ($auth_service->{'auth_type'} eq 'cas');
                    next
                        unless (
                        $auth_service->{'non_blocking_redirection'} eq 'on');

                    ## skip cas server where client as been already redirect
                    ## to the list of cas servers already checked is stored in
                    ## the session
                    ## the check below works fine as long as we
                    ## don't have more then 10 CAS servers (because we don't
                    ## properly split the list of values)
                    Log::do_log('debug',
                        "check_cas checker_cas : $session->{'checked_cas'} current cas_id $Conf::Conf{'cas_id'}{$robot}{$auth_service->{'auth_service_name'}}"
                    );
                    next
                        if ($session->{'checked_cas'} =~
                        /$Conf::Conf{'cas_id'}{$robot}{$auth_service->{'auth_service_name'}}/
                        );

                    # before redirect update the list of already checked cas
                    # server to prevent loop
                    my $cas_server = $auth_service->{'cas_server'};
                    my $return_url = wwslib::get_my_url();

                    ## Append the current CAS server ID to the list of checked
                    ## CAS servers
                    $session->{'checked_cas'} .=
                        $Conf::Conf{'cas_id'}{$robot}
                        {$auth_service->{'auth_service_name'}};

                    my $redirect_url =
                        $cas_server->getServerLoginGatewayURL($return_url);

                    if ($redirect_url =~ /http(s)+\:\//i) {
                        $in{'action'} = 'redirect';
                        $param->{'redirect_to'} = $redirect_url;

                        last;
                    } elsif ($redirect_url == -1) {    # CAS server auth error
                        Log::do_log('notice',
                            "CAS server auth error $auth_service->{'auth_service_name'}"
                        );
                    } else {
                        Log::do_log('notice',
                            "Strange CAS ticket detected and validated check sympa code !"
                        );
                    }
                }
                # set do_not_use_cas because all cas servers have been checked
                # without success
                $session->{'do_not_use_cas'} = 1
                    unless ($param->{'redirect_to'} =~ /http(s)+\:\//i);
            }
        }

        ##Cookie extern : sympa_altemails
        ## !!
        $param->{'alt_emails'} =
1529
            Sympa::CookieLib::check_cookie_extern($ENV{'HTTP_COOKIE'},
1530
1531
1532
            $Conf::Conf{'cookie'}, $param->{'user'}{'email'});

        if ($param->{'user'}{'email'}) {
1533
1534
1535
            #$param->{'auth'} =
            #    $param->{'alt_emails'}{$param->{'user'}{'email'}}
            #    || 'classic';
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550

            if (Sympa::User::is_global_user($param->{'user'}{'email'})) {
                $param->{'user'} =
                    Sympa::User::get_global_user($param->{'user'}{'email'});
            }

            ## For the parser to display an empty field instead of [xxx]
            $param->{'user'}{'gecos'} ||= '';
            unless (defined $param->{'user'}{'cookie_delay'}) {
                $param->{'user'}{'cookie_delay'} =
                    $Conf::Conf{'cookie_expire'};
            }

            ## Skip get_which if either in a list context or accessing the CSS
            unless ($in{'action'} eq 'css' || defined $in{'list'}) {
1551
1552
1553
1554
1555
1556
1557
                ## get_which will be limited at most (your_lists_size + 1).
                my $your_lists_size =
                    Conf::get_robot_conf($robot, 'your_lists_size');
                my %limit_which = ();
                %limit_which = ('limit' => $your_lists_size + 1)
                    if 0 <= $your_lists_size;
                foreach my $role ('member', 'owner', 'editor') {
1558
                    $param->{"get_which_$role"} = Sympa::List::get_lists(
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
                        $robot,
                        'filter' => [
                            $role      => $param->{'user'}{'email'},
                            '! status' => 'closed|family_closed'
                        ],
                        %limit_which
                    );
                }
                ## for compatibility to <= 6.2a
                $param->{'get_which'} = $param->{'get_which_member'};
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
            }
        }
    }    ## END if RSS

    ## Action
    my $action = $in{'action'};

    ## Store current action in the session in order to redirect after a logi
    ## or other temporary actions.
    ## We should not memorize ULRs that are transitory actions
    ## POST is not handled
    ## A lot of other methods where used in the past (before session was
    ## introduced in Sympa). We must clean all.
    unless ($temporary_actions{$action} || $ENV{'REQUEST_METHOD'} ne 'GET') {
        $session->{'redirect_url'} =
            $param->{'base_url'} . $param->{'path_cgi'} . $ENV{'PATH_INFO'};
    }

    $action ||= Conf::get_robot_conf($robot, 'default_home');
    $param->{'remote_addr'}     = $ENV{'REMOTE_ADDR'};
    $param->{'remote_host'}     = $ENV{'REMOTE_HOST'};
    $param->{'http_user_agent'} = $ENV{'HTTP_USER_AGENT'};
    $param->{'htmlarea_url'}    = $Conf::Conf{'htmlarea_url'};
    # if ($Conf::Conf{'export_topics'} =~ /all/i);

    if ($in{'action'} eq 'css') {
        do_css();
        $param->{'action'} = 'css';
    } elsif ($maintenance_mode) {
        do_maintenance();
        $param->{'action'} = 'maintenance';
    } else {
        ## Session loop
        while ($action) {
            unless (check_param_in()) {
sikeda's avatar
sikeda committed
1604
1605
                Sympa::Report::reject_report_web('user', 'wrong_param', {},
                    $action, $list);
1606
1607
1608
1609
1610
                wwslog('info', 'Wrong parameters');
                last;
            }

            $param->{'host'} = $list->{'admin'}{'host'}
1611
                if (ref($list) eq 'Sympa::List');
1612
            $param->{'host'} ||= $robot;
sikeda's avatar
sikeda committed
1613
1614
            $param->{'domain'} = $list->{'domain'}
                if (ref($list) eq 'Sympa::List');
1615
1616
1617

            # Set content language
            # FIXME: $ENV{'HTTP_ACCEPT_LANGUAGE'} not used !
sikeda's avatar
sikeda committed
1618
1619
            my $list_lang = $list->{'admin'}{'lang'}
                if ref $list eq 'Sympa::List';
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
            my $user_lang = $param->{'user'}{'lang'} if $param->{'user'};
            $param->{'lang'} =
                $language->set_lang($session->{'lang'}, $user_lang,
                $list_lang, Conf::get_robot_conf($robot, 'lang'),
                $Conf::Conf{'lang'}, 'en');
            # compatibility concern: old-style locale.
            $param->{'locale'} =
                Sympa::Language::lang2oldlocale($param->{'lang'});
            # compatibility concern: for 6.1.
            $param->{'lang_tag'} = $param->{'lang'};

            export_topics($robot);

            unless ($comm{$action}) {
1634
                if (Sympa::List->new($action, $robot)) {
1635
1636
1637
1638
1639
1640
                    do_redirect($param->{'base_url'}
                            . $param->{'path_cgi'}
                            . '/info/'
                            . $action);
                    last;
                }
1641
                Sympa::Report::reject_report_web('user', 'unknown_action', {},
1642
                    $action, $list);
1643
                wwslog('info', 'Unknown action %s', $action);
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
                unless ($action = prevent_visibility_bypass()) {
                    last;
                }
            }

            $param->{'action'} = $action;

            my $old_action    = $action;
            my $old_subaction = $in{'subaction'};

            ## Check required action parameters
            my $check_output = check_action_parameters($action);

            if (!defined $check_output) {
1658
1659
                wwslog('err', 'Missing required parameters for action "%s"',
                    $action);
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
                delete($param->{'action'});
                last;

            } elsif ($check_output != 1) {
                ## The output of the check may indicate another action to run
                ## first
                ## Example : running loginrequest if user is not authenticated
                $action = $param->{'action'} = $check_output;
            }

            ## Execute the action ##
            if (defined $action) {
1672
                no strict 'refs';
1673
1674
1675
1676
1677
1678
1679
1680
                $action = $comm{$action}->();
            }

            unless (defined $action) {
                unless ($action = prevent_visibility_bypass()) {
                    delete($param->{'action'});
                    last;
                } else {
sikeda's avatar
sikeda committed
1681
1682
                    Sympa::Report::reject_report_web('user',
                        'authorization_reject', {}, $param->{'action'}, '');
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
                }
            }

            # after redirect do not send anything, it will crash fcgi lib
            last
                if ($action =~ /redirect/);

            if ($action eq $old_action) {
                # if a subaction is define and change, then it is not a loop
                if (!defined($in{'subaction'})
                    || ($in{'subaction'} eq $old_subaction)) {
                    wwslog('info', 'Stopping loop with %s action', $action);
                    #undef $action;
                    $action = 'home';
                }
            }

            undef $action if ($action == 1);
        }
    }

    ## Prepare outgoing params
    check_param_out();

    ## Params
    $param->{'refparam'}    = ref($param);
    $param->{'action_type'} = $action_type{$param->{'action'}};

    $param->{'action_type'} = 'none'
        unless (($param->{'is_priv'})
        || ($param->{'action_type'} eq 'serveradmin'));
1714
1715
1716

    #FIXME: is this block neccessary?
    unless ($param->{'lang'}) {
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
        my $user_lang = $param->{'user'}{'lang'} if $param->{'user'};
        $param->{'lang'} =
            $language->set_lang($user_lang,
            Conf::get_robot_conf($robot, 'lang'),
            $Conf::Conf{'lang'}, 'en');
        # compatibility: 6.1.
        $param->{'lang_tag'} = $param->{'lang'};
    }

    if ($param->{'list'}) {
        $param->{'list_title'} = $list->{'admin'}{'subject'};
        $param->{'list_protected_email'} =
            get_protected_email_address($param->{'list'},
            $list->{'admin'}{'host'});
        $param->{'title'} =
            get_protected_email_address($param->{'list'},
            $list->{'admin'}{'host'});
        $param->{'title_clear_txt'} = "$param->{'list'}";

        if ($param->{'subtitle'}) {
            $param->{'main_title'} =
                "$param->{'list'} - $param->{'subtitle'}";
        }

    } else {
        $param->{'main_title'} = $param->{'title'} =
            Conf::get_robot_conf($robot, 'title');
        $param->{'title_clear_txt'} = $param->{'title'};
    }

    # Store oauth_token in session if present and no user so that it
    # will not be lost during login process
    my $oauth_token = $in{'oauth_token'} || '';
    $session->{'oauth_token'} = $oauth_token
        if $oauth_token ne '' && $session->{'email'} eq 'nobody';

    ## store in session table this session contexte
    $session->store();

    ## Do not manage cookies at this level if content was already sent
    unless ($param->{'bypass'} eq 'extreme'
        || $param->{'action'} eq 'css'
        || $maintenance_mode
        || $rss
        || $ajax) {

        my $delay = $param->{'user'}{'cookie_delay'};
        unless (defined $delay) {
            $delay = $Conf::Conf{'cookie_expire'};
        }

        if ($delay == 0) {
            $delay = 'session';
        }
        $session->renew() unless $param->{'use_ssl'};

        unless (
            $session->set_cookie(
                $param->{'cookie_domain'},
                $delay, $param->{'use_ssl'}
            )
            ) {
            wwslog('notice', 'Could not set HTTP cookie');
        }

        ## Set cookies "your_subscribtions" unless in one list page
1783
        if ($param->{'user'}{'email'} && ref($list) ne 'Sympa::List') {
1784
1785
1786
1787
1788
1789
1790
1791
            # In case get_which was not set.
            # The get_which will be limited at most (your_lists_size + 1).
            my $your_lists_size =
                Conf::get_robot_conf($robot, 'your_lists_size');
            my %limit_which = ();
            %limit_which = ('limit' => $your_lists_size + 1)
                if 0 <= $your_lists_size;
            foreach my $role ('member', 'owner', 'editor') {
1792
                $param->{"get_which_$role"} = Sympa::List::get_lists(
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
                    $robot,
                    'filter' => [
                        $role      => $param->{'user'}{'email'},
                        '! status' => 'closed|family_closed'
                    ],
                    %limit_which
                ) unless defined $param->{"get_which_$role"};
            }
            ## for compatibility to <= 6.2a.
            $param->{'get_which'} = $param->{'get_which_member'};
1803
1804

            ## Add lists information to 'which_info'
1805
            foreach my $list (@{$param->{'get_which_member'}}) {
1806
                ## Evaluate AuthZ scenario first
1807
                my $result = Sympa::Scenario::request_action(
1808
                    $list,
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
                    'visibility',
                    $param->{'auth_method'},
                    {   'sender'      => $param->{'user'}{'email'},
                        'remote_host' => $param->{'remote_host'},
                        'remote_addr' => $param->{'remote_addr'}
                    }
                );
                next
                    unless (ref($result) eq 'HASH'
                    && $result->{'action'} eq 'do_it');

                my $l = $list->{'name'};
                $param->{'which_info'}{$l}{'subject'} =
                    $list->{'admin'}{'subject'};
                $param->{'which_info'}{$l}{'host'} = $list->{'admin'}{'host'};
                $param->{'which_info'}{$l}{'info'} = 1;
            }
            foreach my $list (@{$param->{'get_which_owner'}}) {
                my $l = $list->{'name'};
                $param->{'which_info'}{$l}{'subject'} =
                    $list->{'admin'}{'subject'};
                $param->{'which_info'}{$l}{'host'} = $list->{'admin'}{'host'};
                $param->{'which_info'}{$l}{'info'} = 1;
                $param->{'which_info'}{$l}{'admin'} = 1;
            }
            foreach my $list (@{$param->{'get_which_editor'}}) {
                my $l = $list->{'name'};

                $param->{'which_info'}{$l}{'subject'} =
                    $list->{'admin'}{'subject'};
                $param->{'which_info'}{$l}{'host'} = $list->{'admin'}{'host'};
                $param->{'which_info'}{$l}{'info'} = 1;
                $param->{'which_info'}{$l}{'admin'} = 1;
            }
        }
        ## Set cookies unless client use https authentication
        if ($param->{'user'}{'email'}) {
1846
            if ($param->{'user'}{'email'} ne 'x509') {    #FIXME FIXME FIXME
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
                $session->{'auth'} ||= 'classic';
                $param->{'cookie_set'} = 1;

                ###Cookie extern : sympa_altemails
                my $number = 0;
                foreach my $element (keys %{$param->{'alt_emails'}}) {
                    $number++ if ($element);
                }

                unless ($number == 0) {
                    unless (
1858
                        Sympa::CookieLib::set_cookie_extern(
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
                            $Conf::Conf{'cookie'},
                            $param->{'cookie_domain'},
                            %{$param->{'alt_emails'}}
                        )
                        ) {
                        wwslog('notice',
                            'Could not set HTTP cookie for external_auth');
                    }
                }
            }
1869
1870
1871
1872
1873
        }
        # elsif ($ENV{'HTTP_COOKIE'} =~ /sympauser\=/) {
        #     Sympa::CookieLib::set_cookie('unknown', $Conf::Conf{'cookie'},
        #         $param->{'cookie_domain'}, 'now');
        # }
1874
1875
    }

1876
1877
1878
1879
    ## Available languages
    $param->{'languages'} = {};
    $language->push_lang();
    foreach my $lang (tools::get_supported_languages($robot)) {
1880
1881
1882
1883
1884
        next unless $lang = $language->set_lang($lang);
        $param->{'languages'}{$lang}{'complete'} = $language->native_name
            || $lang;
        # compatibility: 6.1.
        $param->{'languages'}{$lang}{'lang_tag'} = $lang;
1885
    }
1886
1887
1888
1889
1890
    if (my $lang = $language->set_lang($param->{'lang'})) {    #current lang
        $param->{'languages'}{$lang}{'complete'} = $language->native_name
            || $lang;
        # compatibility: 6.1.
        $param->{'languages'}{$lang}{'lang_tag'} = $lang;
1891

1892
        $param->{'languages'}{$lang}{'selected'} = 'selected="selected"';
1893
1894
    }
    $language->pop_lang;
1895

1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
    $param->{'html_dumpvars'} = tools::dump_html_var($param)
        if ($session->{'dumpvars'});

    # if bypass is defined select the content-type from various vars
    if ($param->{'bypass'}) {

        ## if bypass = 'extreme' leave the action send the content-type and
        ## the content itself
        unless ($param->{'bypass'} eq 'extreme') {

            ## if bypass = 'asis', file content-type is in the file itself as is define by the action in $param->{'content_type'};
            unless ($param->{'bypass'} eq 'asis') {
                my $type =
                       $param->{'content_type'}
                    || $mime_types->{$param->{'file_extension'}}
                    || 'application/octet-stream';
                printf "Content-Type: %s\n\n", $type;
            }

            #  $param->{'file'} or $param->{'error'} must be define in this case.

            if (open(FILE, $param->{'file'})) {
                print <FILE>;
                close FILE;
1920
            } elsif (Sympa::Report::is_there_any_reject_report_web()) {
1921
                ## for compatibility : it could be better
1922
1923
1924
1925
                my $intern = Sympa::Report::get_intern_error_web();
                my $system = Sympa::Report::get_system_error_web();
                my $user   = Sympa::Report::get_user_error_web();
                my $auth   = Sympa::Report::get_auth_reject_web();
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972

                if (ref($intern) eq 'ARRAY') {
                    print "INTERNAL SERVER ERROR\n";
                }
                if (ref($system) eq 'ARRAY') {
                    print "SYSTEM ERROR\n";
                }
                if (ref($user) eq 'ARRAY') {
                    foreach my $err (@$user) {
                        printf "ERROR : %s\n", $err;
                    }
                }
                if (ref($auth) eq 'ARRAY') {
                    foreach my $err (@$auth) {
                        printf "AUTHORISATION FAILED : %s\n", $err;
                    }
                }

            } else {
                print "Internal error content-type nor file defined\n";
                Log::do_log('err',
                    'Internal error content-type nor file defined');
            }
        }

    } elsif ($rss) {
        ## Send RSS
        print "Cache-control: no-cache\n";
        print "Content-Type: application/rss+xml; charset=utf-8\n\n";

        ## Icons
        $param->{'icons_url'} =
            Conf::get_robot_conf($robot, 'static_content_url') . '/icons';

        ## Retro compatibility concerns
        $param->{'active'} = 1;

        if (defined $list) {
            $param->{'list_conf'} = $list->{'admin'};
        }

        my $tt2_include_path = tools::get_search_path(
            $list || $robot,
            subdir => 'web_tt2',
            lang   => $param->{'lang'}
        );

1973
1974
1975
        unshift @$tt2_include_path, $plugins->tt2Paths
            if $plugins;

1976
1977
1978
1979
1980
1981
1982
        unless (
            tt2::parse_tt2(
                $param, 'rss.tt2',