Conf.pm 87.8 KB
Newer Older
1
2
3
4
# -*- indent-tabs-mode: nil; -*-
# vim:ft=perl:et:sw=4
# $Id$

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

root's avatar
root committed
25
26
27
28
## This module handles the configuration file for Sympa.

package Conf;

29
30
use strict;
use warnings;
31
use English qw(-no_match_vars);
32
use Storable;
33

34
use Sympa;
35
use Sympa::ConfDef;
36
37
use Sympa::Constants;
use Sympa::Language;
38
use Sympa::LockedFile;
39
use Sympa::Log;
40
use SDM;
41
use tools;
42
43
use Sympa::Tools::Data;
use Sympa::Tools::File;
root's avatar
root committed
44

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

47
=encoding utf-8
48

49
50
51
#=head1 NAME
#
#Conf - Sympa configuration
52
53
54
55
56
57
58

=head1 DESCRIPTION

=head2 CONSTANTS AND EXPORTED VARIABLES

=cut

59
## Database and SQL statement handlers
60
my $sth;
61
# parameters hash, keyed by parameter name
62
our %params =
63
    map { $_->{name} => $_ }
64
    grep { $_->{name} } @Sympa::ConfDef::params;
65
66
67

# valid virtual host parameters, keyed by parameter name
my %valid_robot_key_words;
68
my %db_storable_parameters;
69
my %optional_key_words;
70
foreach my $hash (@Sympa::ConfDef::params) {
71
72
73
74
    $valid_robot_key_words{$hash->{'name'}} = 1 if ($hash->{'vhost'});
    $db_storable_parameters{$hash->{'name'}} = 1
        if (defined($hash->{'db'}) and $hash->{'db'} ne 'none');
    $optional_key_words{$hash->{'name'}} = 1 if ($hash->{'optional'});
75
76
}

sikeda's avatar
sikeda committed
77
our $params_by_categories = _get_parameters_names_by_category();
78

79
my %old_params = (
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    trusted_ca_options               => 'capath,cafile',
    'msgcat'                         => '',
    queueexpire                      => '',
    clean_delay_queueother           => '',
    web_recode_to                    => 'filesystem_encoding', # ??? - 5.2
    'localedir'                      => '',
    'ldap_export_connection_timeout' => '',                    # 3.3b3 - 4.1?
    'ldap_export_dnmanager'          => '',                    # ,,
    'ldap_export_host'               => '',                    # ,,
    'ldap_export_name'               => '',                    # ,,
    'ldap_export_password'           => '',                    # ,,
    'ldap_export_suffix'             => '',                    # ,,
    'tri'                            => 'sort',                # ??? - 1.3.4-1
    'sort'                           => '',                    # 1.4.0 - ???
    'pidfile'                        => '',                    # ??? - 6.1.17
    'pidfile_distribute'             => '',                    # ,,
    'pidfile_creation'               => '',                    # ,,
    'pidfile_bulk'                   => '',                    # ,,
    'archived_pidfile'               => '',                    # ,,
    'bounced_pidfile'                => '',                    # ,,
    'task_manager_pidfile'           => '',                    # ,,
101
102
103
104
105
106
    'email_gecos'       => 'gecos',              # 6.2a.?? - 6.2a.33
    'lock_method'       => '',                   # 5.3b.3 - 6.2a.33
    'html_editor_file'  => 'html_editor_url',    # 6.2a
    'openssl'           => '',                   # ?? - 6.2a.40
    'distribution_mode' => '',                   # 5.0a.1 - 6.2a.40
    'queuedistribute'   => '',                   # ,,
107
108
109
110

    # These are not yet implemented
    'crl_dir'          => '',
    'dkim_header_list' => '',
root's avatar
root committed
111
);
112

113
114
## These parameters now have a hard-coded value
## Customized value can be accessed though as %Ignored_Conf
olivier.salaun's avatar
olivier.salaun committed
115
my %Ignored_Conf;
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
my %hardcoded_params = (filesystem_encoding => 'utf8');

my %trusted_applications = (
    'trusted_application' => {
        'occurrence' => '0-n',
        'format'     => {
            'name' => {
                'format'     => '\S*',
                'occurrence' => '1',
                'case'       => 'insensitive',
            },
            'ip' => {
                'format'     => '\d+\.\d+\.\d+\.\d+',
                'occurrence' => '0-1'
            },
            'md5password' => {
                'format'     => '.*',
                'occurrence' => '0-1'
            },
            'proxy_for_variables' => {
                'format'     => '.*',
                'occurrence' => '0-n',
                'split_char' => ','
139
140
141
142
143
144
145
146
147
148
149
            },
            'set_variables' => {
                'format'     => '\S+=.*',
                'occurrence' => '0-n',
                'split_char' => ',',
            },
            'allow_commands' => {
                'format'     => '\S+',
                'occurrence' => '0-n',
                'split_char' => ',',
            },
150
151
        }
    }
152
);
153
my $binary_file_extension = ".bin";
154

155
our $wwsconf;
olivier.salaun's avatar
olivier.salaun committed
156
our %Conf = ();
root's avatar
root committed
157

158
159
160
161
162
163
164
165
166
167
168
=head2 FUNCTIONS

=over 4

=item load ( [ CONFIG_FILE ], [ NO_DB ], [ RETURN_RESULT ] )

Loads and parses the configuration file.  Reports errors if any.

do not try to load database values if NO_DB is set;
do not change gloval hash %Conf if RETURN_RESULT is set;

169
170
## we known that's dirty, this proc should be rewritten without this global
## var %Conf
171
172
173
174
175

=back

=cut

root's avatar
root committed
176
sub load {
177
178
    my $config_file   = shift || get_sympa_conf();
    my $no_db         = shift;
179
    my $return_result = shift;
180
    my $force_reload;
181

root's avatar
root committed
182
    my $config_err = 0;
183
184
    my %line_numbered_config;

185
    if (_source_has_not_changed($config_file) and !$return_result) {
186
187
188
189
        if (my $tmp_conf = _load_binary_cache(
                {'config_file' => $config_file . $binary_file_extension}
            )
            ) {
190
            %Conf = %{$tmp_conf};
191
192
193
            # Will force the robot.conf reloading, as sympa.conf is the
            # default.
            $force_reload = 1;
194
        }
195
    } else {
196
        $log->syslog('debug3',
197
198
            'File %s has changed since the last cache. Loading file',
            $config_file);
199
200
        # Will force the robot.conf reloading, as sympa.conf is the default.
        $force_reload = 1;
201
        ## Loading the Sympa main config file.
202
203
204
205
206
207
208
        if (my $config_loading_result = _load_config_file_to_hash(
                {'path_to_config_file' => $config_file}
            )
            ) {
            %line_numbered_config =
                %{$config_loading_result->{'numbered_config'}};
            %Conf       = %{$config_loading_result->{'config'}};
209
            $config_err = $config_loading_result->{'errors'};
210
        } else {
211
212
213
214
215
            return undef;
        }
        # Returning the config file content if this is what has been asked.
        return (\%line_numbered_config) if ($return_result);

216
217
        # Users may define parameters with a typo or other errors. Check that
        # the parameters
218
        # we found in the config file are all well defined Sympa parameters.
sikeda's avatar
sikeda committed
219
        $config_err += _detect_unknown_parameters_in_config(
220
221
222
223
224
225
            {   'config_hash' => \%Conf,
                'config_file_line_numbering_reference' =>
                    \%line_numbered_config,
            }
        );

226
227
        # Some parameter values are hardcoded. In that case, ignore what was
        #  set in the config file and simply use the hardcoded value.
228
229
        %Ignored_Conf =
            %{_set_hardcoded_parameter_values({'config_hash' => \%Conf,})};
root's avatar
root committed
230

sikeda's avatar
sikeda committed
231
        _set_listmasters_entry({'config_hash' => \%Conf, 'main_config' => 1});
232
233
234
235
236

        ## Some parameters must have a value specifically defined in the
        ## config. If not, it is an error.
        $config_err += _detect_missing_mandatory_parameters(
            {'config_hash' => \%Conf, 'file_to_check' => $config_file});
237

238
        # Some parameters need special treatments to get their final values.
sikeda's avatar
sikeda committed
239
        _infer_server_specific_parameter_values({'config_hash' => \%Conf,});
240

sikeda's avatar
sikeda committed
241
        _infer_robot_parameter_values({'config_hash' => \%Conf});
root's avatar
root committed
242

243
        if ($config_err) {
244
            $log->syslog('err', 'Errors while parsing main config file %s',
sikeda's avatar
sikeda committed
245
                $config_file);
246
247
            return undef;
        }
248

249
250
        _store_source_file_name(
            {'config_hash' => \%Conf, 'config_file' => $config_file});
sikeda's avatar
sikeda committed
251
        _save_config_hash_to_binary({'config_hash' => \%Conf,});
252
    }
root's avatar
root committed
253

254
255
    if (my $missing_modules_count =
        _check_cpan_modules_required_by_config({'config_hash' => \%Conf,})) {
256
        $log->syslog('err', 'Warning: %d required modules are missing',
sikeda's avatar
sikeda committed
257
            $missing_modules_count);
258
259
    }

260
261
    _replace_file_value_by_db_value({'config_hash' => \%Conf})
        unless ($no_db);
sikeda's avatar
sikeda committed
262
263
    _load_server_specific_secondary_config_files({'config_hash' => \%Conf,});
    _load_robot_secondary_config_files({'config_hash' => \%Conf});
264

265
    ## Load robot.conf files
266
267
268
269
270
271
272
273
    unless (
        load_robots(
            {   'config_hash'  => \%Conf,
                'no_db'        => $no_db,
                'force_reload' => $force_reload
            }
        )
        ) {
274
275
        return undef;
    }
sikeda's avatar
sikeda committed
276
    ##_create_robot_like_config_for_main_robot();
root's avatar
root committed
277
    return 1;
278
}
279

salaun's avatar
salaun committed
280
281
## load each virtual robots configuration files
sub load_robots {
282
    my $param = shift;
283
284
    my @robots;

sikeda's avatar
sikeda committed
285
    my $robots_list_ref = get_robots_list();
286
    unless (defined $robots_list_ref) {
287
        $log->syslog('err', 'Robots config loading failed');
288
        return undef;
289
    } else {
290
291
292
293
294
        @robots = @{$robots_list_ref};
    }
    unless ($#robots > -1) {
        return 1;
    }
295
    my $exiting = 0;
296
    foreach my $robot (@robots) {
297
        my $robot_config_file = "$Conf{'etc'}/$robot/robot.conf";
298
299
300
301
302
303
304
305
306
        my $robot_conf        = undef;
        unless (
            $robot_conf = _load_single_robot_config(
                {   'robot'        => $robot,
                    'no_db'        => $param->{'no_db'},
                    'force_reload' => $param->{'force_reload'}
                }
            )
            ) {
307
            $log->syslog(
sikeda's avatar
sikeda committed
308
309
310
311
                'err',
                'The config for robot %s contain errors: it could not be correctly loaded',
                $robot
            );
312
            $exiting = 1;
313
        } else {
314
315
            $param->{'config_hash'}{'robots'}{$robot} = $robot_conf;
        }
316
317
        _check_double_url_usage(
            {'config_hash' => $param->{'config_hash'}{'robots'}{$robot}});
318
    }
319
    return undef if ($exiting);
320
    return 1;
321
322
323
324
325
326
327
}

## returns a robot conf parameter
sub get_robot_conf {
    my ($robot, $param) = @_;

    if (defined $robot && $robot ne '*') {
328
329
        if (   defined $Conf{'robots'}{$robot}
            && defined $Conf{'robots'}{$robot}{$param}) {
330
331
            return $Conf{'robots'}{$robot}{$param};
        }
332
    }
333
    ## default
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
    return $Conf{$param};
}

=over 4

=item get_sympa_conf

Gets path name of main config file.
Path name is taken from:

=over 4

=item 1

C<--config> command line option

=item 2

C<SYMPA_CONFIG> environment variable

=item 3

built-in default

=back

=back

=cut

sub get_sympa_conf {
    return $main::options{'config'}
366
        if %main::options and defined $main::options{'config'};
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
    return $ENV{'SYMPA_CONFIG'} || Sympa::Constants::CONFIG;
}

=over 4

=item get_wwsympa_conf

Gets path name of wwsympa.conf file.
Path name is taken from:

=over 4

=item 1

C<SYMPA_WWSCONFIG> environment variable

=item 2

built-in default

=back

=back

=cut

sub get_wwsympa_conf {
    return $ENV{'SYMPA_WWSCONFIG'} || Sympa::Constants::WWSCONFIG;
salaun's avatar
salaun committed
395
396
}

397
398
# deletes all the *.conf.bin files.
sub delete_binaries {
399
    $log->syslog('debug2', '');
400
    my @files = (get_sympa_conf(), get_wwsympa_conf());
sikeda's avatar
sikeda committed
401
    foreach my $robot (@{get_robots_list()}) {
402
403
404
        push @files, "$Conf{'etc'}/$robot/robot.conf";
    }
    foreach my $c_file (@files) {
405
406
        my $binary_file = $c_file . ".bin";
        if (-f $binary_file) {
407
408
            if (-w $binary_file) {
                unlink $binary_file;
409
            } else {
410
                $log->syslog(
411
                    'err',
412
                    'Could not remove file %s. You should remove it manually to ensure the configuration used is valid',
413
414
                    $binary_file
                );
415
416
417
418
419
            }
        }
    }
}

420
421
# Return a reference to an array containing the names of the robots on the
# server.
422
sub get_robots_list {
423
    $log->syslog('debug2', "Retrieving the list of robots on the server");
424
    my @robots_list;
425
    unless (opendir DIR, $Conf{'etc'}) {
426
        $log->syslog('err',
sikeda's avatar
sikeda committed
427
428
            'Unable to open directory %s for virtual robots config',
            $Conf{'etc'});
429
430
431
432
433
434
435
436
437
438
439
440
        return undef;
    }
    foreach my $robot (readdir DIR) {
        my $robot_config_file = "$Conf{'etc'}/$robot/robot.conf";
        next unless (-d "$Conf{'etc'}/$robot");
        next unless (-f $robot_config_file);
        push @robots_list, $robot;
    }
    closedir(DIR);
    return \@robots_list;
}

441
## Returns a hash containing the values of all the parameters of the group
442
443
## (as defined in Sympa::ConfDef) whose name is given as argument, in the
## context of the robot given as argument.
444
445
sub get_parameters_group {
    my ($robot, $group) = @_;
446
    $log->syslog('debug3', 'Getting parameters for group "%s"', $group);
447
448
    my $param_hash;
    foreach my $param_name (keys %{$params_by_categories->{$group}}) {
449
        $param_hash->{$param_name} = get_robot_conf($robot, $param_name);
450
451
452
    }
    return $param_hash;
}
453
454

## fetch the value from parameter $label of robot $robot from conf_table
455
sub get_db_conf {
456
457
458
459

    my $robot = shift;
    my $label = shift;

460
461
462
463
464
465
466
467
468
469
470
471
    # if the value is related to a robot that is not explicitly defined, apply
    # it to the default robot.
    $robot = '*' unless (-f $Conf{'etc'} . '/' . $robot . '/robot.conf');
    unless ($robot) { $robot = '*' }

    unless (
        $sth = SDM::do_query(
            "SELECT value_conf AS value FROM conf_table WHERE (robot_conf =%s AND label_conf =%s)",
            SDM::quote($robot),
            SDM::quote($label)
        )
        ) {
472
        $log->syslog(
473
474
475
476
477
            'err',
            'Unable retrieve value of parameter %s for robot %s from the database',
            $label,
            $robot
        );
478
        return undef;
479
    }
480

481
    my $value = $sth->fetchrow;
482

483
    $sth->finish();
484
    return $value;
485
486
487
}

## store the value from parameter $label of robot $robot from conf_table
488
sub set_robot_conf {
489
490
491
492
    my $robot = shift;
    my $label = shift;
    my $value = shift;

493
    $log->syslog('info', 'Set config for robot %s, %s="%s"',
494
        $robot, $label, $value);
495

496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
    # set the current config before to update database.
    if (-f "$Conf{'etc'}/$robot/robot.conf") {
        $Conf{'robots'}{$robot}{$label} = $value;
    } else {
        $Conf{$label} = $value;
        $robot = '*';
    }

    unless (
        $sth = SDM::do_query(
            "SELECT count(*) FROM conf_table WHERE (robot_conf=%s AND label_conf =%s)",
            SDM::quote($robot),
            SDM::quote($label)
        )
        ) {
511
        $log->syslog(
512
513
514
515
516
            'err',
            'Unable to check presence of parameter %s for robot %s in database',
            $label,
            $robot
        );
517
        return undef;
518
    }
519

520
521
    my $count = $sth->fetchrow;
    $sth->finish();
522

523
    if ($count == 0) {
524
525
526
527
528
529
530
531
        unless (
            $sth = SDM::do_query(
                "INSERT INTO conf_table (robot_conf, label_conf, value_conf) VALUES (%s,%s,%s)",
                SDM::quote($robot),
                SDM::quote($label),
                SDM::quote($value)
            )
            ) {
532
            $log->syslog(
533
534
535
536
537
538
                'err',
                'Unable add value %s for parameter %s in the robot %s DB conf',
                $value,
                $label,
                $robot
            );
539
540
            return undef;
        }
541
542
543
544
545
546
547
548
549
550
551
    } else {
        unless (
            $sth = SDM::do_query(
                "UPDATE conf_table SET robot_conf=%s, label_conf=%s, value_conf=%s WHERE ( robot_conf  =%s AND label_conf =%s)",
                SDM::quote($robot),
                SDM::quote($label),
                SDM::quote($value),
                SDM::quote($robot),
                SDM::quote($label)
            )
            ) {
552
            $log->syslog(
553
554
555
556
557
558
                'err',
                'Unable set parameter %s value to %s in the robot %s DB conf',
                $label,
                $value,
                $robot
            );
559
            return undef;
560
        }
561
562
563
    }
}

564
565
# Store configs to database
sub conf_2_db {
566
    $log->syslog('debug2', '(%s)', @_);
567

568
    my @conf_parameters = @Sympa::ConfDef::params;
569
570

    # store in database robots parameters.
571
572
573
    # load only parameters that are in a robot.conf file (do not apply
    # defaults).
    my $robots_conf = load_robots();
574

575
    unless (opendir DIR, $Conf{'etc'}) {
576
        $log->syslog('err',
sikeda's avatar
sikeda committed
577
578
            'Unable to open directory %s for virtual robots config',
            $Conf{'etc'});
579
580
581
582
583
584
        return undef;
    }

    foreach my $robot (readdir(DIR)) {
        next unless (-d "$Conf{'etc'}/$robot");
        next unless (-f "$Conf{'etc'}/$robot/robot.conf");
585

586
        my $config;
587
588
589
590
591
592
593
        if (my $result_of_config_loading = _load_config_file_to_hash(
                {         'path_to_config_file' => $Conf{'etc'} . '/' 
                        . $robot
                        . '/robot.conf'
                }
            )
            ) {
594
595
            $config = $result_of_config_loading->{'config'};
        }
sikeda's avatar
sikeda committed
596
        _remove_unvalid_robot_entry($config);
597
598
599
600
601
602
603
604
605
606
607
608
609
610

        for my $i (0 .. $#conf_parameters) {
            if ($conf_parameters[$i]->{'name'}) {
                # skip separators in conf_parameters structure
                if (($conf_parameters[$i]->{'vhost'} eq '1')
                    && #skip parameters that can't be define by robot so not to be loaded in db at that stage
                    ($config->{$conf_parameters[$i]->{'name'}})
                    ) {
                    Conf::set_robot_conf(
                        $robot,
                        $conf_parameters[$i]->{'name'},
                        $config->{$conf_parameters[$i]->{'name'}}
                    );
                }
611
612
613
            }
        }
    }
614
    closedir(DIR);
615
616

    # store in database sympa;conf and wwsympa.conf
617

618
619
    ## Load configuration file. Ignoring database config and get result
    my $global_conf;
620
621
    unless ($global_conf =
        Conf::load(Conf::get_sympa_conf(), 1, 'return_result')) {
622
        $log->syslog('err', 'Configuration file %s has errors',
623
624
            Conf::get_sympa_conf());
        return undef;
625
    }
626
627
628
629
630
631
632
633
634
635

    for my $i (0 .. $#conf_parameters) {
        if (($conf_parameters[$i]->{'edit'} eq '1')
            && $global_conf->{$conf_parameters[$i]->{'name'}}) {
            Conf::set_robot_conf(
                "*",
                $conf_parameters[$i]->{'name'},
                $global_conf->{$conf_parameters[$i]->{'name'}}[0]
            );
        }
636
637
638
    }
}

639
640
641
## Check required files and create them if required
sub checkfiles_as_root {

642
    my $config_err = 0;
sympa-authors's avatar
   
sympa-authors committed
643

644
    ## Check aliases file
645
646
647
    unless (-f $Conf{'sendmail_aliases'}
        || ($Conf{'sendmail_aliases'} =~ /^none$/i)) {
        unless (open ALIASES, ">$Conf{'sendmail_aliases'}") {
648
            $log->syslog(
649
650
651
652
653
654
                'err',
                "Failed to create aliases file %s",
                $Conf{'sendmail_aliases'}
            );
            return undef;
        }
655

656
657
658
659
660
        print ALIASES
            "## This aliases file is dedicated to Sympa Mailing List Manager\n";
        print ALIASES
            "## You should edit your sendmail.mc or sendmail.cf file to declare it\n";
        close ALIASES;
661
        $log->syslog(
662
663
664
665
666
            'notice',
            "Created missing file %s",
            $Conf{'sendmail_aliases'}
        );
        unless (
667
            Sympa::Tools::File::set_file_rights(
668
669
670
671
672
673
                file  => $Conf{'sendmail_aliases'},
                user  => Sympa::Constants::USER,
                group => Sympa::Constants::GROUP,
                mode  => 0644,
            )
            ) {
674
            $log->syslog('err', 'Unable to set rights on %s',
675
676
677
                $Conf{'db_name'});
            return undef;
        }
sympa-authors's avatar
   
sympa-authors committed
678
679
    }

680
    foreach my $robot (keys %{$Conf{'robots'}}) {
sympa-authors's avatar
sympa-authors committed
681

682
683
684
685
        # create static content directory
        my $dir = get_robot_conf($robot, 'static_content_path');
        if ($dir ne '' && !-d $dir) {
            unless (mkdir($dir, 0775)) {
686
687
                $log->syslog('err', 'Unable to create directory %s: %m',
                    $dir);
688
689
                $config_err++;
            }
690

691
            unless (
692
                Sympa::Tools::File::set_file_rights(
693
694
695
696
697
                    file  => $dir,
                    user  => Sympa::Constants::USER,
                    group => Sympa::Constants::GROUP,
                )
                ) {
698
                $log->syslog('err', 'Unable to set rights on %s',
699
700
701
                    $Conf{'db_name'});
                return undef;
            }
702
        }
703
704
    }

705
    return 1;
706
707
}

sikeda's avatar
sikeda committed
708
709
710
711
712
713
714
715
716
717
## Check if data structures are uptodate
## If not, no operation should be performed before the upgrade process is run
sub data_structure_uptodate {
    my $version_file =
        Conf::get_robot_conf('*', 'etc') . '/data_structure.version';
    my $data_structure_version;

    if (-f $version_file) {
        my $fh;
        unless (open $fh, '<', $version_file) {
718
            $log->syslog('err', 'Unable to open %s: %m', $version_file);
sikeda's avatar
sikeda committed
719
720
721
722
723
724
725
726
727
728
729
730
731
732
            return undef;
        }
        while (<$fh>) {
            next if /^\s*$/;
            next if /^\s*\#/;
            chomp;
            $data_structure_version = $_;
            last;
        }
        close $fh;
    }

    if (defined $data_structure_version
        and $data_structure_version ne Sympa::Constants::VERSION) {
733
        $log->syslog('err',
sikeda's avatar
sikeda committed
734
735
736
737
738
739
740
741
            "Data structure (%s) is not uptodate for current release (%s)",
            $data_structure_version, Sympa::Constants::VERSION);
        return 0;
    }

    return 1;
}

742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
# Check if cookie parameter was changed.
# Old name: tools::cookie_changed().
sub cookie_changed {
    my $current = $Conf::Conf{'cookie'};
    $current = '' unless defined $current;

    my $changed = 1;
    if (-f "$Conf::Conf{'etc'}/cookies.history") {
        my $fh;
        unless (open $fh, "$Conf::Conf{'etc'}/cookies.history") {
            $log->syslog('err', 'Unable to read %s/cookies.history',
                $Conf::Conf{'etc'});
            return undef;
        }
        my $oldcook = <$fh>;
        close $fh;
        ($oldcook) = reverse split /\s+/, $oldcook;
        $oldcook = '' unless defined $oldcook;

        if ($oldcook eq $current) {
            $log->syslog('debug2', 'Cookie is stable');
            $changed = 0;
        }
        return $changed;
    } else {
        my $umask = umask 037;
        unless (open COOK, ">$Conf::Conf{'etc'}/cookies.history") {
            umask $umask;
            $log->syslog('err', 'Unable to create %s/cookies.history',
                $Conf::Conf{'etc'});
            return undef;
        }
        umask $umask;
        chown [getpwnam(Sympa::Constants::USER)]->[2],
            [getgrnam(Sympa::Constants::GROUP)]->[2],
            "$Conf::Conf{'etc'}/cookies.history";
        print COOK "$current ";
        close COOK;
        return (0);
    }
}

root's avatar
root committed
784
785
786
## Check a few files
sub checkfiles {
    my $config_err = 0;
787
788
789
790
791

    foreach my $p ('sendmail', 'openssl', 'antivirus_path') {
        next unless $Conf{$p};

        unless (-x $Conf{$p}) {
792
            $log->syslog('err', "File %s does not exist or is not executable",
793
794
                $Conf{$p});
            $config_err++;
795
        }
796
797
798
    }

    foreach my $qdir (
799
800
801
        'spool',          'queue',      'queueautomatic', 'queuedigest',
        'queuemod',       'queuetopic', 'queueauth',      'queuebounce',
        'queuesubscribe', 'queuetask',  'tmpdir'
802
803
        ) {
        unless (-d $Conf{$qdir}) {
804
            $log->syslog('info', 'Creating spool %s', $Conf{$qdir});
805
            unless (mkdir($Conf{$qdir}, 0775)) {
806
807
                $log->syslog('err', 'Unable to create spool %s',
                    $Conf{$qdir});
808
809
810
                $config_err++;
            }
            unless (
811
                Sympa::Tools::File::set_file_rights(
812
813
814
                    file  => $Conf{$qdir},
                    user  => Sympa::Constants::USER,
                    group => Sympa::Constants::GROUP,
815
816
                )
                ) {
817
                $log->syslog('err', 'Unable to set rights on %s',
818
819
                    $Conf{$qdir});
                $config_err++;
820
            }
821
        }
root's avatar
root committed
822
    }
823

824
    ## Also create associated bad/ spools
825
    foreach my $qdir ('queue', 'queueautomatic', 'queuebounce') {
826
827
        my $subdir = $Conf{$qdir} . '/bad';
        unless (-d $subdir) {
828
            $log->syslog('info', 'Creating spool %s', $subdir);
829
            unless (mkdir($subdir, 0775)) {
830
                $log->syslog('err', 'Unable to create spool %s', $subdir);
831
832
833
                $config_err++;
            }
            unless (
834
                Sympa::Tools::File::set_file_rights(
835
836
837
                    file  => $subdir,
                    user  => Sympa::Constants::USER,
                    group => Sympa::Constants::GROUP,
838
839
                )
                ) {
840
                $log->syslog('err', 'Unable to set rights on %s', $subdir);
841
                $config_err++;
842
            }
843
        }
844
    }
salaun's avatar
salaun committed
845

846
847
    ## Check cafile and capath access
    if (defined $Conf{'cafile'} && $Conf{'cafile'}) {
848
        unless (-f $Conf{'cafile'} && -r $Conf{'cafile'}) {
849
            $log->syslog('err', 'Cannot access cafile %s', $Conf{'cafile'});
850
            Sympa::send_notify_to_listmaster('*', 'cannot_access_cafile',
851
                [$Conf{'cafile'}]);
852
            $config_err++;
853
        }
854
855
856
    }

    if (defined $Conf{'capath'} && $Conf{'capath'}) {
857
        unless (-d $Conf{'capath'} && -x $Conf{'capath'}) {
858
            $log->syslog('err', 'Cannot access capath %s', $Conf{'capath'});
859
            Sympa::send_notify_to_listmaster('*', 'cannot_access_capath',
860
                [$Conf{'capath'}]);
861
            $config_err++;
862
        }
863
864
    }

865
    ## queuebounce and bounce_path pointing to the same directory
866
    if ($Conf{'queuebounce'} eq $Conf{'bounce_path'}) {
867
        $log->syslog(
868
869
870
871
            'err',
            'Error in config: queuebounce and bounce_path parameters pointing to the same directory (%s)',
            $Conf{'queuebounce'}
        );
872
        Sympa::send_notify_to_listmaster(
873
            '*',
874
            'queuebounce_and_bounce_path_are_the_same',
875
876
            [$Conf{'queuebounce'}]
        );
877
        $config_err++;
878
    }
879

880
    ## automatic_list_creation enabled but queueautomatic pointing to queue
881
882
    if (($Conf{automatic_list_feature} eq 'on')
        && $Conf{'queue'} eq $Conf{'queueautomatic'}) {
883
        $log->syslog(
884
885
886
887
            'err',
            'Error in config: queue and queueautomatic parameters pointing to the same directory (%s)',
            $Conf{'queue'}
        );
888
        Sympa::send_notify_to_listmaster('*',
889
            'queue_and_queueautomatic_are_the_same',
890
            [$Conf{'queue'}]);
891
892
893
        $config_err++;
    }

894
    # Create pictures dir if useful for each robot.
sympa-authors's avatar
   
sympa-authors committed
895
    foreach my $robot (keys %{$Conf{'robots'}}) {
896
897
898
899
        my $dir = get_robot_conf($robot, 'static_content_path');
        if ($dir ne '' && -d $dir) {
            unless (-f $dir . '/index.html') {
                unless (open(FF, ">$dir" . '/index.html')) {
900
                    $log->syslog(
901
                        'err',
902
903
                        'Unable to create %s/index.html as an empty file to protect directory: %m',
                        $dir
904
905
906
                    );
                }
                close FF;
907
908
            }

909
910
            # create picture dir
            if (get_robot_conf($robot, 'pictures_feature') eq 'on') {
911
912
913
                my $pictures_dir =
                    get_robot_conf($robot, 'static_content_path')
                    . '/pictures';
914
915
                unless (-d $pictures_dir) {
                    unless (mkdir($pictures_dir, 0775)) {
916
                        $log->syslog('err', 'Unable to create directory %s',
917
918
919
920
921
922
923
924
                            $pictures_dir);
                        $config_err++;
                    }
                    chmod 0775, $pictures_dir;

                    my $index_path = $pictures_dir . '/index.html';
                    unless (-f $index_path) {
                        unless (open(FF, ">$index_path")) {
925
                            $log->syslog(
926
927
928
929
930
931
932
933
                                'err',
                                'Unable to create %s as an empty file to protect directory',
                                $index_path
                            );
                        }
                        close FF;
                    }
                }
934
935
936
            }
        }
    }
937

938
939
    #update_css();

root's avatar
root committed
940
941
942
943
    return undef if ($config_err);
    return 1;
}

944
## return 1 if the parameter is a known robot
945
## Valid options :
946
##    'just_try' : prevent error logs if robot is not valid
947
sub valid_robot {
948
    my $robot   = shift;
949
    my $options = shift;
950

951
952
    ## Main host
    return 1 if ($robot eq $Conf{'domain'});
953

954
    ## Missing etc directory
955
    unless (-d $Conf{'etc'} . '/' . $robot) {
956
        $log->syslog(
957
            'err',  'Robot %s undefined; no %s directory',
958
959
960
            $robot, $Conf{'etc'} . '/' . $robot
        ) unless ($options->{'just_try'});
        return undef;
961
    }
962

963
    ## Missing expl directory
964
    unless (-d $Conf{'home'} . '/' . $robot) {
965
        $log->syslog(
966
            'err',  'Robot %s undefined; no %s directory',
967
968
969
            $robot, $Conf{'home'} . '/' . $robot
        ) unless ($options->{'just_try'});
        return undef;
970
    }
971

972
973
    ## Robot not loaded
    unless (defined $Conf{'robots'}{$robot}) {
974
        $log->syslog('err', 'Robot %s was not loaded by this Sympa process',
975
976
977
            $robot)
            unless ($options->{'just_try'});
        return undef;
978
    }
979

980
981
982
983
984
985
986
987
988
    return 1;
}

## Returns the SSO record correponding to the provided sso_id
## return undef if none was found
sub get_sso_by_id {
    my %param = @_;

    unless (defined $param{'service_id'} && defined $param{'robot'}) {
989
        return undef;
990
991
992
    }

    foreach my $sso (@{$Conf{'auth_services'}{$param{'robot'}}}) {
993
        $log->syslog('notice', 'SSO: %s', $sso->{'service_id'});
994
        next unless ($sso->{'service_id'} eq $param{'service_id'});
995

996
        return $sso;
997
    }
998

999
1000
    return undef;
}
For faster browsing, not all history is shown. View entire blame