Localconfig.pm 11.5 KB
Newer Older
1 2 3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
#
5 6
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
7 8 9 10 11 12 13 14 15 16 17

package Bugzilla::Install::Localconfig;

# NOTE: This package may "use" any modules that it likes. However,
# all functions in this package should assume that:
#
# * The data/ directory does not exist.
# * Templates are not available.
# * Files do not have the correct permissions
# * The database is not up to date

18
use 5.10.1;
19 20 21
use strict;

use Bugzilla::Constants;
22
use Bugzilla::Install::Util qw(bin_loc install_string);
23
use Bugzilla::Util qw(generate_random_password wrap_hard);
24 25

use Data::Dumper;
26
use File::Basename qw(dirname);
27
use Safe;
28
use Term::ANSIColor;
29

30
use parent qw(Exporter);
31 32

our @EXPORT_OK = qw(
33
    read_localconfig
34 35 36
    update_localconfig
);

37
use constant LOCALCONFIG_VARS => (
38 39 40 41 42 43 44
    {
        name    => 'create_htaccess',
        default => 1,
    },
    {
        name    => 'webservergroup',
        default => ON_WINDOWS ? '' : 'apache',
45 46 47 48
    },
    {
        name    => 'use_suexec',
        default => 0,
49 50 51 52 53 54 55
    },
    {
        name    => 'db_driver',
        default => 'mysql',
    },
    {
        name    => 'db_host',
56
        default => 'localhost',           
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    },
    {
        name    => 'db_name',
        default => 'bugs',
    },
    {
        name    => 'db_user',
        default => 'bugs',
    },
    {
        name    => 'db_pass',
        default => '',
    },
    {
        name    => 'db_port',
        default => 0,
    },
    {
        name    => 'db_sock',
        default => '',
    },
    {
        name    => 'db_check',
        default => 1,
    },
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    {
        name    => 'db_mysql_ssl_ca_file',
        default => '',
    },
    {
        name    => 'db_mysql_ssl_ca_path',
        default => '',
    },
    {
        name    => 'db_mysql_ssl_client_cert',
        default => '',
    },
    {
        name    => 'db_mysql_ssl_client_key',
        default => '',
    },
98 99 100 101 102 103
    {
        name    => 'index_html',
        default => 0,
    },
    {
        name    => 'cvsbin',
104
        default => sub { bin_loc('cvs') },
105 106 107
    },
    {
        name    => 'interdiffbin',
108
        default => sub { bin_loc('interdiff') },
109 110 111
    },
    {
        name    => 'diffpath',
112
        default => sub { dirname(bin_loc('diff')) },
113 114 115
    },
    {
        name    => 'site_wide_secret',
116 117 118
        # 64 characters is roughly the equivalent of a 384-bit key, which
        # is larger than anybody would ever be able to brute-force.
        default => sub { generate_random_password(64) },
119
    },
120
);
121 122 123 124 125 126 127 128

sub read_localconfig {
    my ($include_deprecated) = @_;
    my $filename = bz_locations()->{'localconfig'};

    my %localconfig;
    if (-e $filename) {
        my $s = new Safe;
129 130 131
        # Some people like to store their database password in another file.
        $s->permit('dofile');

132 133 134
        $s->rdo($filename);
        if ($@ || $!) {
            my $err_msg = $@ ? $@ : $!;
135 136
            die install_string('error_localconfig_read',
                    { error => $err_msg, localconfig => $filename }), "\n";
137 138
        }

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
        my @read_symbols;
        if ($include_deprecated) {
            # First we have to get the whole symbol table
            my $safe_root = $s->root;
            my %safe_package;
            { no strict 'refs'; %safe_package = %{$safe_root . "::"}; }
            # And now we read the contents of every var in the symbol table.
            # However:
            # * We only include symbols that start with an alphanumeric
            #   character. This excludes symbols like "_<./localconfig"
            #   that show up in some perls.
            # * We ignore the INC symbol, which exists in every package.
            # * Perl 5.10 imports a lot of random symbols that all
            #   contain "::", and we want to ignore those.
            @read_symbols = grep { /^[A-Za-z0-1]/ and !/^INC$/ and !/::/ }
                                 (keys %safe_package);
        }
        else {
            @read_symbols = map($_->{name}, LOCALCONFIG_VARS);
        }
        foreach my $var (@read_symbols) {
160
            my $glob = $s->varglob($var);
161 162 163 164 165 166 167 168 169
            # We can't get the type of a variable out of a Safe automatically.
            # We can only get the glob itself. So we figure out its type this
            # way, by trying first a scalar, then an array, then a hash.
            #
            # The interesting thing is that this converts all deprecated 
            # array or hash vars into hashrefs or arrayrefs, but that's 
            # fine since as I write this all modern localconfig vars are 
            # actually scalars.
            if (defined $$glob) {
170
                $localconfig{$var} = $$glob;
171
            }
172
            elsif (@$glob) {
173
                $localconfig{$var} = \@$glob;
174
            }
175
            elsif (%$glob) {
176
                $localconfig{$var} = \%$glob;
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
            }
        }
    }

    return \%localconfig;
}


#
# This is quite tricky. But fun!
#
# First we read the file 'localconfig'. Then we check if the variables we
# need are defined. If not, we will append the new settings to
# localconfig, instruct the user to check them, and stop.
#
# Why do it this way?
#
# Assume we will enhance Bugzilla and eventually more local configuration
# stuff arises on the horizon.
#
# But the file 'localconfig' is not in the Bugzilla CVS or tarfile. You
# know, we never want to overwrite your own version of 'localconfig', so
# we can't put it into the CVS/tarfile, can we?
#
# Now, when we need a new variable, we simply add the necessary stuff to
# LOCALCONFIG_VARS. When the user gets the new version of Bugzilla from CVS and
# runs checksetup, it finds out "Oh, there is something new". Then it adds
# some default value to the user's local setup and informs the user to
# check that to see if it is what the user wants.
#
# Cute, ey?
#
sub update_localconfig {
    my ($params) = @_;

    my $output      = $params->{output} || 0;
213
    my $answer      = Bugzilla->installation_answers;
214 215 216 217 218
    my $localconfig = read_localconfig('include deprecated');

    my @new_vars;
    foreach my $var (LOCALCONFIG_VARS) {
        my $name = $var->{name};
219 220 221 222 223 224 225 226
        my $value = $localconfig->{$name};
        # Regenerate site_wide_secret if it was made by our old, weak
        # generate_random_password. Previously we used to generate
        # a 256-character string for site_wide_secret.
        $value = undef if ($name eq 'site_wide_secret' and defined $value
                           and length($value) == 256);
        
        if (!defined $value) {
227
            push(@new_vars, $name);
228
            $var->{default} = &{$var->{default}} if ref($var->{default}) eq 'CODE';
229 230 231 232 233 234
            if (exists $answer->{$name}) {
                $localconfig->{$name} = $answer->{$name};
            }
            else {
                $localconfig->{$name} = $var->{default};
            }
235 236 237 238
        }
    }

    if (!$localconfig->{'interdiffbin'} && $output) {
239
        print "\n", install_string('patchutils_missing'), "\n";
240 241
    }

242 243 244 245 246
    my @old_vars;
    foreach my $var (keys %$localconfig) {
        push(@old_vars, $var) if !grep($_->{name} eq $var, LOCALCONFIG_VARS);
    }

247 248
    my $filename = bz_locations->{'localconfig'};

249
    # Move any custom or old variables into a separate file.
250
    if (scalar @old_vars) {
251
        my $filename_old = "$filename.old";
252 253
        open(my $old_file, ">>:utf8", $filename_old) 
            or die "$filename_old: $!";
254 255 256 257 258 259
        local $Data::Dumper::Purity = 1;
        foreach my $var (@old_vars) {
            print $old_file Data::Dumper->Dump([$localconfig->{$var}], 
                                               ["*$var"]) . "\n\n";
        }
        close $old_file;
260
        my $oldstuff = join(', ', @old_vars);
261 262 263
        print install_string('lc_old_vars',
            { localconfig => $filename, old_file => $filename_old,
              vars => $oldstuff }), "\n";
264 265
    }

266
    # Re-write localconfig
267
    open(my $fh, ">:utf8", $filename) or die "$filename: $!";
268
    foreach my $var (LOCALCONFIG_VARS) {
269 270 271 272 273 274 275 276
        my $name = $var->{name};
        my $desc = install_string("localconfig_$name", { root => ROOT_USER });
        chomp($desc);
        # Make the description into a comment.
        $desc =~ s/^/# /mg;
        print $fh $desc, "\n",
                  Data::Dumper->Dump([$localconfig->{$name}],
                                     ["*$name"]), "\n";
277
   }
278

279
    if (@new_vars) {
280
        my $newstuff = join(', ', @new_vars);
281 282 283 284
        print "\n";
        print colored(install_string('lc_new_vars', { localconfig => $filename,
                                                      new_vars => wrap_hard($newstuff, 70) }),
                      COLOR_ERROR), "\n";
285 286 287
        exit;
    }

288 289 290
    # Reset the cache for Bugzilla->localconfig so that it will be re-read
    delete Bugzilla->request_cache->{localconfig};

291 292 293 294 295 296 297 298 299 300 301 302 303 304
    return { old_vars => \@old_vars, new_vars => \@new_vars };
}

1;

__END__

=head1 NAME

Bugzilla::Install::Localconfig - Functions and variables dealing
  with the manipulation and creation of the F<localconfig> file.

=head1 SYNOPSIS

305
 use Bugzilla::Install::Requirements qw(update_localconfig);
306
 update_localconfig({ output => 1 });
307 308 309 310

=head1 DESCRIPTION

This module is used primarily by L<checksetup.pl> to create and
311 312
modify the localconfig file. Most scripts should use L<Bugzilla/localconfig>
to access localconfig variables.
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

=head1 CONSTANTS

=over

=item C<LOCALCONFIG_VARS>

An array of hashrefs. These hashrefs contain three keys:

 name    - The name of the variable.
 default - The default value for the variable. Should always be
           something that can fit in a scalar.
 desc    - Additional text to put in localconfig before the variable
           definition. Must end in a newline. Each line should start
           with "#" unless you have some REALLY good reason not
           to do that.

=item C<OLD_LOCALCONFIG_VARS>

An array of names of variables. If C<update_localconfig> finds these
variables defined in localconfig, it will print out a warning.

=back

=head1 SUBROUTINES

=over

341
=item C<read_localconfig>
342

343 344 345 346 347 348 349 350 351 352 353
=over

=item B<Description>

Reads the localconfig file and returns all valid values in a hashref.

=item B<Params>

=over

=item C<$include_deprecated> 
354

355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
C<true> if you want the returned hashref to include *any* variable
currently defined in localconfig, even if it doesn't exist in 
C<LOCALCONFIG_VARS>. Generally this is is only for use 
by L</update_localconfig>.

=back

=item B<Returns>

A hashref of the localconfig variables. If an array is defined in
localconfig, it will be an arrayref in the returned hash. If a
hash is defined, it will be a hashref in the returned hash.
Only includes variables specified in C<LOCALCONFIG_VARS>, unless
C<$include_deprecated> is true.

=back
371 372


373
=item C<update_localconfig>
374 375 376 377 378 379

Description: Adds any new variables to localconfig that aren't
             currently defined there. Also optionally prints out
             a message about vars that *should* be there and aren't.
             Exits the program if it adds any new vars.

380
Params:      C<$output> - C<true> if the function should display informational
381 382 383 384 385 386 387 388
                 output and warnings. It will always display errors or
                 any message which would cause program execution to halt.

Returns:     A hashref, with C<old_vals> being an array of names of variables
             that were removed, and C<new_vals> being an array of names
             of variables that were added to localconfig.

=back