Filesystem.pm 37 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::Filesystem;

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

18
use 5.10.1;
19
use strict;
20
use warnings;
21 22

use Bugzilla::Constants;
23
use Bugzilla::Error;
24
use Bugzilla::Install::Localconfig;
25
use Bugzilla::Install::Util qw(install_string);
26
use Bugzilla::Util;
27
use Bugzilla::Hook;
28

29
use File::Find;
30
use File::Path;
31
use File::Basename;
32
use File::Copy qw(move);
33
use File::Spec;
34
use File::Slurp;
35
use IO::File;
36
use POSIX ();
37

38
use parent qw(Exporter);
39 40 41
our @EXPORT = qw(
    update_filesystem
    create_htaccess
42
    fix_all_file_permissions
43
    fix_dir_permissions
44
    fix_file_permissions
45 46
);

47 48 49
use constant HT_DEFAULT_DENY => <<EOT;
# nothing in this directory is retrievable unless overridden by an .htaccess
# in a subdirectory
50
<IfModule mod_version.c>
51
  <IfVersion < 2.4>
52 53
    Deny from all
  </IfVersion>
54
  <IfVersion >= 2.4>
55
    Require all denied
56 57 58 59 60
  </IfVersion>
</IfModule>
<IfModule !mod_version.c>
  Deny from all
</IfModule>
61 62
EOT

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
###############
# Permissions #
###############

# Used by the permissions "constants" below.
sub _suexec { Bugzilla->localconfig->{'use_suexec'}     };
sub _group  { Bugzilla->localconfig->{'webservergroup'} };

# Writeable by the owner only.
use constant OWNER_WRITE => 0600;
# Executable by the owner only.
use constant OWNER_EXECUTE => 0700;
# A directory which is only writeable by the owner.
use constant DIR_OWNER_WRITE => 0700;

# A cgi script that the webserver can execute.
sub WS_EXECUTE { _group() ? 0750 : 0755 };
# A file that is read by cgi scripts, but is not ever read
# directly by the webserver.
sub CGI_READ { _group() ? 0640 : 0644 };
# A file that is written to by cgi scripts, but is not ever
# read or written directly by the webserver.
sub CGI_WRITE { _group() ? 0660 : 0666 };
# A file that is served directly by the web server.
sub WS_SERVE { (_group() and !_suexec()) ? 0640 : 0644 };

# A directory whose contents can be read or served by the
# webserver (so even directories containing cgi scripts
# would have this permission).
sub DIR_WS_SERVE { (_group() and !_suexec()) ? 0750 : 0755 };
# A directory that is read by cgi scripts, but is never accessed
# directly by the webserver
sub DIR_CGI_READ { _group() ? 0750 : 0755 };
# A directory that is written to by cgi scripts, but where the
# scripts never needs to overwrite files created by other
# users.
sub DIR_CGI_WRITE { _group() ? 0770 : 01777 };
# A directory that is written to by cgi scripts, where the
# scripts need to overwrite files created by other users.
sub DIR_CGI_OVERWRITE { _group() ? 0770 : 0777 };

# This can be combined (using "|") with other permissions for 
# directories that, in addition to their normal permissions (such
# as DIR_CGI_WRITE) also have content served directly from them
# (or their subdirectories) to the user, via the webserver.
sub DIR_ALSO_WS_SERVE { _suexec() ? 0001 : 0 };

110 111 112 113 114
# This looks like a constant because it effectively is, but
# it has to call other subroutines and read the current filesystem,
# so it's defined as a sub. This is not exported, so it doesn't have
# a perldoc. However, look at the various hashes defined inside this 
# function to understand what it returns. (There are comments throughout.)
115
#
116 117 118 119
# The rationale for the file permissions is that there is a group the
# web server executes the scripts as, so the cgi scripts should not be writable
# by this group. Otherwise someone may find it possible to change the cgis
# when exploiting some security flaw somewhere (not necessarily in Bugzilla!)
120 121 122 123 124 125 126
sub FILESYSTEM {
    my $datadir       = bz_locations()->{'datadir'};
    my $attachdir     = bz_locations()->{'attachdir'};
    my $extensionsdir = bz_locations()->{'extensionsdir'};
    my $webdotdir     = bz_locations()->{'webdotdir'};
    my $templatedir   = bz_locations()->{'templatedir'};
    my $libdir        = bz_locations()->{'libpath'};
127
    my $extlib        = bz_locations()->{'ext_libpath'};
128
    my $skinsdir      = bz_locations()->{'skinsdir'};
129
    my $localconfig   = bz_locations()->{'localconfig'};
130
    my $template_cache = bz_locations()->{'template_cache'};
131
    my $graphsdir     = bz_locations()->{'graphsdir'};
132
    my $assetsdir     = bz_locations()->{'assetsdir'};
133

134 135 136 137 138 139 140
    # We want to set the permissions the same for all localconfig files
    # across all PROJECTs, so we do something special with $localconfig,
    # lower down in the permissions section.
    if ($ENV{PROJECT}) {
        $localconfig =~ s/\.\Q$ENV{PROJECT}\E$//;
    }

141 142 143 144 145 146 147 148 149 150
    # Note: When being processed by checksetup, these have their permissions
    # set in this order: %all_dirs, %recurse_dirs, %all_files.
    #
    # Each is processed in alphabetical order of keys, so shorter keys
    # will have their permissions set before longer keys (thus setting
    # the permissions on parent directories before setting permissions
    # on their children).

    # --- FILE PERMISSIONS (Non-created files) --- #
    my %files = (
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
        '*'               => { perms => OWNER_WRITE },
        # Some .pl files are WS_EXECUTE because we want
        # users to be able to cron them or otherwise run
        # them as a secure user, like the webserver owner.
        '*.cgi'           => { perms => WS_EXECUTE },
        'whineatnews.pl'  => { perms => WS_EXECUTE },
        'collectstats.pl' => { perms => WS_EXECUTE },
        'importxml.pl'    => { perms => WS_EXECUTE },
        'testserver.pl'   => { perms => WS_EXECUTE },
        'whine.pl'        => { perms => WS_EXECUTE },
        'email_in.pl'     => { perms => WS_EXECUTE },
        'sanitycheck.pl'  => { perms => WS_EXECUTE },
        'checksetup.pl'   => { perms => OWNER_EXECUTE },
        'runtests.pl'     => { perms => OWNER_EXECUTE },
        'jobqueue.pl'     => { perms => OWNER_EXECUTE },
        'migrate.pl'      => { perms => OWNER_EXECUTE },
        'install-module.pl' => { perms => OWNER_EXECUTE },
168
        'clean-bug-user-last-visit.pl' => { perms => WS_EXECUTE },
169 170 171 172 173 174

        'Bugzilla.pm'   => { perms => CGI_READ },
        "$localconfig*" => { perms => CGI_READ },
        'bugzilla.dtd'  => { perms => WS_SERVE },
        'mod_perl.pl'   => { perms => WS_SERVE },
        'robots.txt'    => { perms => WS_SERVE },
175
        '.htaccess'     => { perms => WS_SERVE },
176 177 178

        'contrib/README'       => { perms => OWNER_WRITE },
        'contrib/*/README'     => { perms => OWNER_WRITE },
Frédéric Buclin's avatar
Frédéric Buclin committed
179
        'contrib/Bugzilla.pm'  => { perms => OWNER_WRITE },
180 181 182 183 184
        'docs/bugzilla.ent'    => { perms => OWNER_WRITE },
        'docs/makedocs.pl'     => { perms => OWNER_EXECUTE },
        'docs/style.css'       => { perms => WS_SERVE },
        'docs/*/rel_notes.txt' => { perms => WS_SERVE },
        'docs/*/README.docs'   => { perms => OWNER_WRITE },
185
        "$datadir/params.json" => { perms => CGI_WRITE },
186 187
        "$datadir/old-params.txt"  => { perms => OWNER_WRITE },
        "$extensionsdir/create.pl" => { perms => OWNER_EXECUTE },
188
        "$extensionsdir/*/*.pl"    => { perms => WS_EXECUTE },
189 190 191 192 193 194
    );

    # Directories that we want to set the perms on, but not
    # recurse through. These are directories we didn't create
    # in checkesetup.pl.
    my %non_recurse_dirs = (
195 196
        '.'  => DIR_WS_SERVE,
        docs => DIR_WS_SERVE,
197 198 199 200 201 202 203 204
    );

    # This sets the permissions for each item inside each of these 
    # directories, including the directory itself. 
    # 'CVS' directories are special, though, and are never readable by 
    # the webserver.
    my %recurse_dirs = (
        # Writeable directories
205
         $template_cache    => { files => CGI_READ,
206 207 208 209 210
                                  dirs => DIR_CGI_OVERWRITE },
         $attachdir         => { files => CGI_WRITE,
                                  dirs => DIR_CGI_WRITE },
         $webdotdir         => { files => WS_SERVE,
                                  dirs => DIR_CGI_WRITE | DIR_ALSO_WS_SERVE },
211
         $graphsdir         => { files => WS_SERVE,
212
                                  dirs => DIR_CGI_WRITE | DIR_ALSO_WS_SERVE },
213 214
         "$datadir/db"      => { files => CGI_WRITE,
                                  dirs => DIR_CGI_WRITE },
215
         $assetsdir         => { files => WS_SERVE,
216
                                  dirs => DIR_CGI_OVERWRITE | DIR_ALSO_WS_SERVE },
217 218

         # Readable directories
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
         "$datadir/mining"     => { files => CGI_READ,
                                     dirs => DIR_CGI_READ },
         "$libdir/Bugzilla"    => { files => CGI_READ,
                                     dirs => DIR_CGI_READ },
         $extlib               => { files => CGI_READ,
                                     dirs => DIR_CGI_READ },
         $templatedir          => { files => CGI_READ,
                                     dirs => DIR_CGI_READ },
         # Directories in the extensions/ dir are WS_SERVE so that
         # the web/ directories can be served by the web server.
         # But, for extra security, we deny direct webserver access to
         # the lib/ and template/ directories of extensions.
         $extensionsdir        => { files => CGI_READ,
                                     dirs => DIR_WS_SERVE },
         "$extensionsdir/*/lib" => { files => CGI_READ,
                                      dirs => DIR_CGI_READ },
         "$extensionsdir/*/template" => { files => CGI_READ,
                                           dirs => DIR_CGI_READ },

         # Content served directly by the webserver
         images                => { files => WS_SERVE,
                                     dirs => DIR_WS_SERVE },
         js                    => { files => WS_SERVE,
                                     dirs => DIR_WS_SERVE },
         $skinsdir             => { files => WS_SERVE,
                                     dirs => DIR_WS_SERVE },
         'docs/*/html'         => { files => WS_SERVE,
                                     dirs => DIR_WS_SERVE },
         'docs/*/pdf'          => { files => WS_SERVE,
                                     dirs => DIR_WS_SERVE },
         'docs/*/txt'          => { files => WS_SERVE,
                                     dirs => DIR_WS_SERVE },
         'docs/*/images'       => { files => WS_SERVE,
                                     dirs => DIR_WS_SERVE },
         "$extensionsdir/*/web" => { files => WS_SERVE,
                                     dirs => DIR_WS_SERVE },

         # Directories only for the owner, not for the webserver.
         '.bzr'                => { files => OWNER_WRITE,
                                    dirs  => DIR_OWNER_WRITE },
         t                     => { files => OWNER_WRITE,
                                     dirs => DIR_OWNER_WRITE },
261 262
         xt                    => { files => OWNER_WRITE,
                                     dirs => DIR_OWNER_WRITE },
263 264 265 266 267 268
         'docs/lib'            => { files => OWNER_WRITE,
                                     dirs => DIR_OWNER_WRITE },
         'docs/*/xml'          => { files => OWNER_WRITE,
                                     dirs => DIR_OWNER_WRITE },
         'contrib'             => { files => OWNER_EXECUTE,
                                     dirs => DIR_OWNER_WRITE, },
269 270 271 272 273 274 275
    );

    # --- FILES TO CREATE --- #

    # The name of each directory that we should actually *create*,
    # pointing at its default permissions.
    my %create_dirs = (
276 277
        # This is DIR_ALSO_WS_SERVE because it contains $webdotdir and
        # $assetsdir.
278 279 280 281 282 283
        $datadir                => DIR_CGI_OVERWRITE | DIR_ALSO_WS_SERVE,
        # Directories that are read-only for cgi scripts
        "$datadir/mining"       => DIR_CGI_READ,
        "$datadir/extensions"   => DIR_CGI_READ,
        $extensionsdir          => DIR_CGI_READ,
        # Directories that cgi scripts can write to.
284
        "$datadir/db"           => DIR_CGI_WRITE,
285
        $attachdir              => DIR_CGI_WRITE,
286
        $graphsdir              => DIR_CGI_WRITE | DIR_ALSO_WS_SERVE,
287
        $webdotdir              => DIR_CGI_WRITE | DIR_ALSO_WS_SERVE,
288
        $assetsdir              => DIR_CGI_WRITE | DIR_ALSO_WS_SERVE,
289 290 291
        # Directories that contain content served directly by the web server.
        "$skinsdir/custom"      => DIR_WS_SERVE,
        "$skinsdir/contrib"     => DIR_WS_SERVE,
292 293 294 295
    );

    # The name of each file, pointing at its default permissions and
    # default contents.
296
    my %create_files = (
297
        "$datadir/extensions/additional" => { perms    => CGI_READ, 
298
                                              contents => '' },
299 300 301 302
        # We create this file so that it always has the right owner
        # and permissions. Otherwise, the webserver creates it as
        # owned by itself, which can cause problems if jobqueue.pl
        # or something else is not running as the webserver or root.
303
        "$datadir/mailer.testfile" => { perms    => CGI_WRITE,
304
                                        contents => '' },
305
    );
306 307 308 309

    # Because checksetup controls the creation of index.html separately
    # from all other files, it gets its very own hash.
    my %index_html = (
310
        'index.html' => { perms => WS_SERVE, contents => <<EOT
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta http-equiv="Refresh" content="0; URL=index.cgi">
</head>
<body>
  <h1>I think you are looking for <a href="index.cgi">index.cgi</a></h1>
</body>
</html>
EOT
        }
    );

    # Because checksetup controls the .htaccess creation separately
    # by a localconfig variable, these go in a separate variable from
326
    # %create_files.
327 328 329 330
    #
    # Note that these get WS_SERVE as their permission
    # because they're *read* by the webserver, even though they're not
    # actually, themselves, served.
331
    my %htaccess = (
332
        "$attachdir/.htaccess"       => { perms    => WS_SERVE,
333
                                          contents => HT_DEFAULT_DENY },
334
        "$libdir/Bugzilla/.htaccess" => { perms    => WS_SERVE,
335
                                          contents => HT_DEFAULT_DENY },
336
        "$extlib/.htaccess"          => { perms    => WS_SERVE,
337
                                          contents => HT_DEFAULT_DENY },
338
        "$templatedir/.htaccess"     => { perms    => WS_SERVE,
339
                                          contents => HT_DEFAULT_DENY },
340
        'contrib/.htaccess'          => { perms    => WS_SERVE,
341
                                          contents => HT_DEFAULT_DENY },
342
        't/.htaccess'                => { perms    => WS_SERVE,
343
                                          contents => HT_DEFAULT_DENY },
344 345
        'xt/.htaccess'               => { perms    => WS_SERVE,
                                          contents => HT_DEFAULT_DENY },
346
        "$datadir/.htaccess"         => { perms    => WS_SERVE,
347
                                          contents => HT_DEFAULT_DENY },
348

349 350 351
        "$graphsdir/.htaccess" => { perms => WS_SERVE, contents => <<EOT
# Allow access to .png and .gif files.
<FilesMatch (\\.gif|\\.png)\$>
352
  <IfModule mod_version.c>
353
    <IfVersion < 2.4>
354 355
      Allow from all
    </IfVersion>
356
    <IfVersion >= 2.4>
357
      Require all granted
358 359 360 361 362
    </IfVersion>
  </IfModule>
  <IfModule !mod_version.c>
    Allow from all
  </IfModule>
363 364 365
</FilesMatch>

# And no directory listings, either.
366
<IfModule mod_version.c>
367
  <IfVersion < 2.4>
368 369
    Deny from all
  </IfVersion>
370
  <IfVersion >= 2.4>
371
    Require all denied
372 373 374 375 376
  </IfVersion>
</IfModule>
<IfModule !mod_version.c>
  Deny from all
</IfModule>
377 378 379
EOT
        },

380
        "$webdotdir/.htaccess" => { perms => WS_SERVE, contents => <<EOT
381 382 383 384
# Restrict access to .dot files to the public webdot server at research.att.com
# if research.att.com ever changes their IP, or if you use a different
# webdot server, you'll need to edit this
<FilesMatch \\.dot\$>
385
  <IfModule mod_version.c>
386
    <IfVersion < 2.4>
387 388 389
      Allow from 192.20.225.0/24
      Deny from all
    </IfVersion>
390
    <IfVersion >= 2.4>
391 392
      Require ip 192.20.225.0/24
      Require all denied
393
    </IfVersion>
394 395 396 397 398
  </IfModule>
  <IfModule !mod_version.c>
    Allow from 192.20.225.0/24
    Deny from all
  </IfModule>
399 400
</FilesMatch>

401 402 403
 # Allow access to .png files created by a local copy of 'dot'
 <FilesMatch \\.png\$>
  <IfModule mod_version.c>
404
    <IfVersion < 2.4>
405 406
      Allow from all
    </IfVersion>
407
    <IfVersion >= 2.4>
408
      Require all granted
409 410 411 412 413
    </IfVersion>
  </IfModule>
  <IfModule !mod_version.c>
    Allow from all
  </IfModule>
414 415 416
</FilesMatch>

# And no directory listings, either.
417
<IfModule mod_version.c>
418
  <IfVersion < 2.4>
419 420
    Deny from all
  </IfVersion>
421
  <IfVersion >= 2.4>
422
    Require all denied
423 424 425 426 427
  </IfVersion>
</IfModule>
<IfModule !mod_version.c>
  Deny from all
</IfModule>
428 429
EOT
        },
430 431 432

        "$assetsdir/.htaccess" => { perms => WS_SERVE, contents => <<EOT
# Allow access to .css files
433
<FilesMatch \\.(css|js)\$>
434
  <IfModule mod_version.c>
435
    <IfVersion < 2.4>
436 437
      Allow from all
    </IfVersion>
438
    <IfVersion >= 2.4>
439 440 441 442 443 444
      <IfModule mod_perl.c>
        Allow from all
      </IfModule>
      <IfModule !mod_perl.c>
        Require all granted
      </IfModule>
445 446 447 448 449
    </IfVersion>
  </IfModule>
  <IfModule !mod_version.c>
     Allow from all
  </IfModule>
450 451 452
</FilesMatch>

# And no directory listings, either.
453
<IfModule mod_version.c>
454
  <IfVersion < 2.4>
455 456
    Deny from all
  </IfVersion>
457
  <IfVersion >= 2.4>
458 459 460 461 462 463
    <IfModule mod_perl.c>
      Deny from all
    </IfModule>
    <IfModule !mod_perl.c>
      Require all denied
    </IfModule>
464 465 466 467 468
  </IfVersion>
</IfModule>
<IfModule !mod_version.c>
  Deny from all
</IfModule>
469 470 471
EOT
        },

472 473
    );

474 475 476 477 478 479 480 481 482
    Bugzilla::Hook::process('install_filesystem', {
        files            => \%files,
        create_dirs      => \%create_dirs,
        non_recurse_dirs => \%non_recurse_dirs,
        recurse_dirs     => \%recurse_dirs,
        create_files     => \%create_files,
        htaccess         => \%htaccess,
    });

483 484 485
    my %all_files = (%create_files, %htaccess, %index_html, %files);
    my %all_dirs  = (%create_dirs, %non_recurse_dirs);

486
    return {
487 488 489 490 491 492 493 494
        create_dirs  => \%create_dirs,
        recurse_dirs => \%recurse_dirs,
        all_dirs     => \%all_dirs,

        create_files => \%create_files,
        htaccess     => \%htaccess,
        index_html   => \%index_html,
        all_files    => \%all_files,
495 496 497 498 499 500
    };
}

sub update_filesystem {
    my ($params) = @_;
    my $fs = FILESYSTEM();
501 502
    my %dirs  = %{$fs->{create_dirs}};
    my %files = %{$fs->{create_files}};
503 504

    my $datadir = bz_locations->{'datadir'};
505
    my $graphsdir = bz_locations->{'graphsdir'};
506
    my $assetsdir = bz_locations->{'assetsdir'};
507 508 509
    # If the graphs/ directory doesn't exist, we're upgrading from
    # a version old enough that we need to update the $datadir/mining 
    # format.
510
    if (-d "$datadir/mining" && !-d $graphsdir) {
511 512 513
        _update_old_charts($datadir);
    }

514 515 516 517 518 519 520
    # If there is a file named '-All-' in $datadir/mining, then we're still
    # having mining files named by product name, and we need to convert them to
    # files named by product ID.
    if (-e File::Spec->catfile($datadir, 'mining', '-All-')) {
        _update_old_mining_filenames(File::Spec->catdir($datadir, 'mining'));
    }

521 522 523 524 525 526
    # By sorting the dirs, we assure that shorter-named directories
    # (meaning parent directories) are always created before their
    # child directories.
    foreach my $dir (sort keys %dirs) {
        unless (-d $dir) {
            print "Creating $dir directory...\n";
527
            mkdir $dir or die "mkdir $dir failed: $!";
528 529
            # For some reason, passing in the permissions to "mkdir"
            # doesn't work right, but doing a "chmod" does.
530
            chmod $dirs{$dir}, $dir or warn "Cannot chmod $dir: $!";
531 532 533
        }
    }

534 535
    # Move the testfile if we can't write to it, so that we can re-create
    # it with the correct permissions below.
536 537 538
    my $testfile = "$datadir/mailer.testfile";
    if (-e $testfile and !-w $testfile) {
        _rename_file($testfile, "$testfile.old");
539 540
    }

541 542 543 544 545 546
    # If old-params.txt exists in the root directory, move it to datadir.
    my $oldparamsfile = "old_params.txt";
    if (-e $oldparamsfile) {
        _rename_file($oldparamsfile, "$datadir/$oldparamsfile");
    }

547 548 549 550 551 552 553
    # Remove old assets htaccess file to force recreation with correct values.
    if (-e "$assetsdir/.htaccess") {
        if (read_file("$assetsdir/.htaccess") =~ /<FilesMatch \\\.css\$>/) {
            unlink("$assetsdir/.htaccess");
        }
    }

554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
    _create_files(%files);
    if ($params->{index_html}) {
        _create_files(%{$fs->{index_html}});
    }
    elsif (-e 'index.html') {
        my $templatedir = bz_locations()->{'templatedir'};
        print <<EOT;

*** It appears that you still have an old index.html hanging around.
    Either the contents of this file should be moved into a template and 
    placed in the '$templatedir/en/custom' directory, or you should delete 
    the file.

EOT
    }
569 570 571 572 573 574 575 576 577 578

    # Delete old files that no longer need to exist

    # 2001-04-29 jake@bugzilla.org - Remove oldemailtech
    #   http://bugzilla.mozilla.org/show_bugs.cgi?id=71552
    if (-d 'shadow') {
        print "Removing shadow directory...\n";
        rmtree("shadow");
    }

579 580 581 582 583
    if (-e "$datadir/versioncache") {
        print "Removing versioncache...\n";
        unlink "$datadir/versioncache";
    }

584 585 586 587 588
    if (-e "$datadir/duplicates.rdf") {
        print "Removing duplicates.rdf...\n";
        unlink "$datadir/duplicates.rdf";
        unlink "$datadir/duplicates-old.rdf";
    }
589 590 591 592 593

    if (-e "$datadir/duplicates") {
        print "Removing duplicates directory...\n";
        rmtree("$datadir/duplicates");
    }
594 595 596

    _remove_empty_css_files();
    _convert_single_file_skins();
597
    _remove_dynamic_assets();
598 599 600 601 602 603 604 605 606
}

sub _remove_empty_css_files {
    my $skinsdir = bz_locations()->{'skinsdir'};
    foreach my $css_file (glob("$skinsdir/custom/*.css"),
                          glob("$skinsdir/contrib/*/*.css"))
    {
        _remove_empty_css($css_file);
    }
607 608
}

609 610 611 612 613
# A simple helper for the update code that removes "empty" CSS files.
sub _remove_empty_css {
    my ($file) = @_;
    my $basename = basename($file);
    my $empty_contents = <<EOT;
614
/*
615
 * Custom rules for $basename.
616 617 618
 * The rules you put here override rules in that stylesheet.
 */
EOT
619 620 621 622 623 624 625 626
    if (length($empty_contents) == -s $file) {
        open(my $fh, '<', $file) or warn "$file: $!";
        my $file_contents;
        { local $/; $file_contents = <$fh>; }
        if ($file_contents eq $empty_contents) {
            print install_string('file_remove', { name => $file }), "\n";
            unlink $file or warn "$file: $!";
        }
627 628 629
    };
}

630 631 632 633 634 635 636 637 638 639 640 641
# We used to allow a single css file in the skins/contrib/ directory
# to be a whole skin.
sub _convert_single_file_skins {
    my $skinsdir = bz_locations()->{'skinsdir'};
    foreach my $skin_file (glob "$skinsdir/contrib/*.css") {
        my $dir_name = $skin_file;
        $dir_name =~ s/\.css$//;
        mkdir $dir_name or warn "$dir_name: $!";
        _rename_file($skin_file, "$dir_name/global.css");
    }
}

642 643 644 645 646 647 648 649
# delete all automatically generated css/js files to force recreation at the
# next request.
sub _remove_dynamic_assets {
    my @files = (
        glob(bz_locations()->{assetsdir} . '/*.css'),
        glob(bz_locations()->{assetsdir} . '/*.js'),
    );
    foreach my $file (@files) {
650 651
        unlink($file);
    }
652 653 654 655 656 657 658 659 660

    # remove old skins/assets directory
    my $old_path = bz_locations()->{skinsdir} . '/assets';
    if (-d $old_path) {
        foreach my $file (glob("$old_path/*.css")) {
            unlink($file);
        }
        rmdir($old_path);
    }
661 662
}

663 664 665 666
sub create_htaccess {
    _create_files(%{FILESYSTEM()->{htaccess}});

    # Repair old .htaccess files
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681

    my $webdot_dir = bz_locations()->{'webdotdir'};
    # The public webdot IP address changed.
    my $webdot = new IO::File("$webdot_dir/.htaccess", 'r')
        || die "$webdot_dir/.htaccess: $!";
    my $webdot_data;
    { local $/; $webdot_data = <$webdot>; }
    $webdot->close;
    if ($webdot_data =~ /192\.20\.225\.10/) {
        print "Repairing $webdot_dir/.htaccess...\n";
        $webdot_data =~ s/192\.20\.225\.10/192.20.225.0\/24/g;
        $webdot = new IO::File("$webdot_dir/.htaccess", 'w') || die $!;
        print $webdot $webdot_data;
        $webdot->close;
    }
682 683
}

684 685
sub _rename_file {
    my ($from, $to) = @_;
686
    print install_string('file_rename', { from => $from, to => $to }), "\n";
687 688 689 690 691 692 693 694
    if (-e $to) {
        warn "$to already exists, not moving\n";
    }
    else {
        move($from, $to) or warn $!;
    }
}

695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
# A helper for the above functions.
sub _create_files {
    my (%files) = @_;

    # It's not necessary to sort these, but it does make the
    # output of checksetup.pl look a bit nicer.
    foreach my $file (sort keys %files) {
        unless (-e $file) {
            print "Creating $file...\n";
            my $info = $files{$file};
            my $fh = new IO::File($file, O_WRONLY | O_CREAT, $info->{perms})
                || die $!;
            print $fh $info->{contents} if $info->{contents};
            $fh->close;
        }
    }
}

# If you ran a REALLY old version of Bugzilla, your chart files are in the
# wrong format. This code is a little messy, because it's very old, and
# when moving it into this module, I couldn't test it so I left it almost 
# completely alone.
sub _update_old_charts {
    my ($datadir) = @_;
    print "Updating old chart storage format...\n";
    foreach my $in_file (glob("$datadir/mining/*")) {
        # Don't try and upgrade image or db files!
        next if (($in_file =~ /\.gif$/i) ||
                 ($in_file =~ /\.png$/i) ||
                 ($in_file =~ /\.db$/i) ||
                 ($in_file =~ /\.orig$/i));

        rename("$in_file", "$in_file.orig") or next;
728
        open(IN, "<", "$in_file.orig") or next;
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 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
        open(OUT, '>', $in_file) or next;

        # Fields in the header
        my @declared_fields;

        # Fields we changed to half way through by mistake
        # This list comes from an old version of collectstats.pl
        # This part is only for people who ran later versions of 2.11 (devel)
        my @intermediate_fields = qw(DATE UNCONFIRMED NEW ASSIGNED REOPENED
                                     RESOLVED VERIFIED CLOSED);

        # Fields we actually want (matches the current collectstats.pl)
        my @out_fields = qw(DATE NEW ASSIGNED REOPENED UNCONFIRMED RESOLVED
                            VERIFIED CLOSED FIXED INVALID WONTFIX LATER REMIND
                            DUPLICATE WORKSFORME MOVED);

         while (<IN>) {
            if (/^# fields?: (.*)\s$/) {
                @declared_fields = map uc, (split /\||\r/, $1);
                print OUT "# fields: ", join('|', @out_fields), "\n";
            }
            elsif (/^(\d+\|.*)/) {
                my @data = split(/\||\r/, $1);
                my %data;
                if (@data == @declared_fields) {
                    # old format
                    for my $i (0 .. $#declared_fields) {
                        $data{$declared_fields[$i]} = $data[$i];
                    }
                }
                elsif (@data == @intermediate_fields) {
                    # Must have changed over at this point
                    for my $i (0 .. $#intermediate_fields) {
                        $data{$intermediate_fields[$i]} = $data[$i];
                    }
                }
                elsif (@data == @out_fields) {
                    # This line's fine - it has the right number of entries
                    for my $i (0 .. $#out_fields) {
                        $data{$out_fields[$i]} = $data[$i];
                    }
                }
                else {
                    print "Oh dear, input line $. of $in_file had " .
                          scalar(@data) . " fields\nThis was unexpected.",
                          " You may want to check your data files.\n";
                }

                print OUT join('|', 
                    map { defined ($data{$_}) ? ($data{$_}) : "" } @out_fields),
                    "\n";
            }
            else {
                print OUT;
            }
        }

        close(IN);
        close(OUT);
    } 
}

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
# The old naming scheme has product names as mining file names; we rename them
# to product IDs.
sub _update_old_mining_filenames {
    my ($miningdir) = @_;
    my @conversion_errors;

    require Bugzilla::Product;

    # We use a dummy product instance with ID 0, representing all products
    my $product_all = {id => 0, name => '-All-'};
    bless($product_all, 'Bugzilla::Product');

    print "Updating old charting data file names...";
    my @products = Bugzilla::Product->get_all();
    push(@products, $product_all);
    foreach my $product (@products) {
        if (-e File::Spec->catfile($miningdir, $product->id)) {
            push(@conversion_errors,
                 { product => $product,
                   message => 'A file named "' . $product->id .
                              '" already exists.' });
        }
    }

    if (! @conversion_errors) {
        # Renaming mining files should work now without a hitch.
        foreach my $product (@products) {
            if (! rename(File::Spec->catfile($miningdir, $product->name),
                         File::Spec->catfile($miningdir, $product->id))) {
                push(@conversion_errors,
                     { product => $product,
                       message => $! });
            }
        }
    }

    # Error reporting
    if (! @conversion_errors) {
        print " done.\n";
    }
    else {
        print " FAILED:\n";
        foreach my $error (@conversion_errors) {
            printf "Cannot rename charting data file for product %d (%s): %s\n",
                   $error->{product}->id, $error->{product}->name,
                   $error->{message};
        }
        print "You need to empty the \"$miningdir\" directory, then run\n",
              "   collectstats.pl --regenerate\n",
              "in order to clean this up.\n";
    }
}

844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
sub fix_dir_permissions {
    my ($dir) = @_;
    return if ON_WINDOWS;
    # Note that _get_owner_and_group is always silent here.
    my ($owner_id, $group_id) = _get_owner_and_group();

    my $perms;
    my $fs = FILESYSTEM();
    if ($perms = $fs->{recurse_dirs}->{$dir}) {
        _fix_perms_recursively($dir, $owner_id, $group_id, $perms);
    }
    elsif ($perms = $fs->{all_dirs}->{$dir}) {
        _fix_perms($dir, $owner_id, $group_id, $perms);
    }
    else {
        # Do nothing. We know nothing about this directory.
        warn "Unknown directory $dir";
    }
}

864 865 866 867 868 869 870 871
sub fix_file_permissions {
    my ($file) = @_;
    return if ON_WINDOWS;
    my $perms = FILESYSTEM()->{all_files}->{$file}->{perms};
    # Note that _get_owner_and_group is always silent here.
    my ($owner_id, $group_id) = _get_owner_and_group();
    _fix_perms($file, $owner_id, $group_id, $perms);
}
872 873 874 875

sub fix_all_file_permissions {
    my ($output) = @_;

876 877
    # _get_owner_and_group also checks that the webservergroup is valid.
    my ($owner_id, $group_id) = _get_owner_and_group($output);
878

879 880 881 882 883 884 885
    return if ON_WINDOWS;

    my $fs = FILESYSTEM();
    my %files = %{$fs->{all_files}};
    my %dirs  = %{$fs->{all_dirs}};
    my %recurse_dirs = %{$fs->{recurse_dirs}};

886
    print get_text('install_file_perms_fix') . "\n" if $output;
887 888 889 890 891 892

    foreach my $dir (sort keys %dirs) {
        next unless -d $dir;
        _fix_perms($dir, $owner_id, $group_id, $dirs{$dir});
    }

893 894 895 896 897 898 899
    foreach my $pattern (sort keys %recurse_dirs) {
        my $perms = $recurse_dirs{$pattern};
        # %recurse_dirs supports globs
        foreach my $dir (glob $pattern) {
            next unless -d $dir;
            _fix_perms_recursively($dir, $owner_id, $group_id, $perms);
        }
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
    }

    foreach my $file (sort keys %files) {
        # %files supports globs
        foreach my $filename (glob $file) {
            # Don't touch directories.
            next if -d $filename || !-e $filename;
            _fix_perms($filename, $owner_id, $group_id, 
                       $files{$file}->{perms});
        }
    }

    _fix_cvs_dirs($owner_id, '.');
}

915 916 917 918 919 920 921 922 923 924
sub _get_owner_and_group {
    my ($output) = @_;
    my $group_id = _check_web_server_group($output);
    return () if ON_WINDOWS;

    my $owner_id = POSIX::getuid();
    $group_id = POSIX::getgid() unless defined $group_id;
    return ($owner_id, $group_id);
}

925 926 927 928 929 930 931
# A helper for fix_all_file_permissions
sub _fix_cvs_dirs {
    my ($owner_id, $dir) = @_;
    my $owner_gid = POSIX::getgid();
    find({ no_chdir => 1, wanted => sub {
        my $name = $File::Find::name;
        if ($File::Find::dir =~ /\/CVS/ || $_ eq '.cvsignore'
932 933 934 935 936 937 938
            || (-d $name && $_ =~ /CVS$/)) 
        {
            my $perms = 0600;
            if (-d $name) {
                $perms = 0700;
            }
            _fix_perms($name, $owner_id, $owner_gid, $perms);
939 940 941 942 943 944 945
        }
    }}, $dir);
}

sub _fix_perms {
    my ($name, $owner, $group, $perms) = @_;
    #printf ("Changing $name to %o\n", $perms);
946 947 948 949 950 951 952

    # The webserver should never try to chown files.
    if (Bugzilla->usage_mode == USAGE_MODE_CMDLINE) {
        chown $owner, $group, $name
            or warn install_string('chown_failed', { path => $name, 
                                                     error => $! }) . "\n";
    }
953
    chmod $perms, $name
954 955
        or warn install_string('chmod_failed', { path => $name, 
                                                 error => $! }) . "\n";
956 957
}

958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
sub _fix_perms_recursively {
    my ($dir, $owner_id, $group_id, $perms) = @_;
    # Set permissions on the directory itself.
    _fix_perms($dir, $owner_id, $group_id, $perms->{dirs});
    # Now recurse through the directory and set the correct permissions
    # on subdirectories and files.
    find({ no_chdir => 1, wanted => sub {
        my $name = $File::Find::name;
        if (-d $name) {
            _fix_perms($name, $owner_id, $group_id, $perms->{dirs});
        }
        else {
            _fix_perms($name, $owner_id, $group_id, $perms->{files});
        }
    }}, $dir);
}

975
sub _check_web_server_group {
976
    my ($output) = @_;
977

978
    my $group    = Bugzilla->localconfig->{'webservergroup'};
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
    my $filename = bz_locations()->{'localconfig'};
    my $group_id;

    # If we are on Windows, webservergroup does nothing
    if (ON_WINDOWS && $group && $output) {
        print "\n\n" . get_text('install_webservergroup_windows') . "\n\n";
    }

    # If we're not on Windows, make sure that webservergroup isn't
    # empty.
    elsif (!ON_WINDOWS && !$group && $output) {
        print "\n\n" . get_text('install_webservergroup_empty') . "\n\n";
    }

    # If we're not on Windows, make sure we are actually a member of
    # the webservergroup.
    elsif (!ON_WINDOWS && $group) {
        $group_id = getgrnam($group);
        ThrowCodeError('invalid_webservergroup', { group => $group }) 
            unless defined $group_id;

        # If on unix, see if we need to print a warning about a webservergroup
        # that we can't chgrp to
        if ($output && $< != 0 && !grep($_ eq $group_id, split(" ", $)))) {
            print "\n\n" . get_text('install_webservergroup_not_in') . "\n\n";
        }
    }

    return $group_id;
}

1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
1;

__END__

=head1 NAME

Bugzilla::Install::Filesystem - Fix up the filesystem during
  installation.

=head1 DESCRIPTION

This module is used primarily by L<checksetup.pl> to modify the 
filesystem during installation, including creating the data/ directory.

=head1 SUBROUTINES

=over

=item C<update_filesystem({ index_html => 0 })>

Description: Creates all the directories and files that Bugzilla
             needs to function but doesn't ship with. Also does
             any updates to these files as necessary during an
             upgrade.

Params:      C<index_html> - Whether or not we should create
               the F<index.html> file.

Returns:     nothing

=item C<create_htaccess()>

Description: Creates all of the .htaccess files for Apache,
             in the various Bugzilla directories. Also updates
             the .htaccess files if they need updating.

Params:      none

Returns:     nothing

1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
=item C<fix_all_file_permissions($output)>

Description: Sets all the file permissions on all of Bugzilla's files
             to what they should be. Note that permissions are different
             depending on whether or not C<$webservergroup> is set
             in F<localconfig>.

Params:      C<$output> - C<true> if you want this function to print
                 out information about what it's doing.

Returns:     nothing

1062 1063 1064 1065 1066 1067
=item C<fix_dir_permissions>

Given the name of a directory, its permissions will be fixed according to
how they are supposed to be set in Bugzilla's current configuration.
If it fails to set the permissions, a warning will be printed to STDERR.

1068 1069 1070 1071 1072 1073
=item C<fix_file_permissions>

Given the name of a file, its permissions will be fixed according to
how they are supposed to be set in Bugzilla's current configuration.
If it fails to set the permissions, a warning will be printed to STDERR.

1074
=back
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100

=head1 B<Methods in need of POD>

=over

=item CGI_WRITE

=item DIR_WS_SERVE

=item DIR_ALSO_WS_SERVE

=item WS_SERVE

=item FILESYSTEM

=item WS_EXECUTE

=item CGI_READ

=item DIR_CGI_READ

=item DIR_CGI_WRITE

=item DIR_CGI_OVERWRITE

=back