Annotation of doc/install/linux/install.pl, revision 1.65

1.1       raeburn     1: #!/usr/bin/perl
                      2: # The LearningOnline Network 
                      3: # Pre-installation script for LON-CAPA
                      4: #
                      5: # Copyright Michigan State University Board of Trustees
                      6: #
                      7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      8: #
                      9: # LON-CAPA is free software; you can redistribute it and/or modify
                     10: # it under the terms of the GNU General Public License as published by
                     11: # the Free Software Foundation; either version 2 of the License, or
                     12: # (at your option) any later version.
                     13: #
                     14: # LON-CAPA is distributed in the hope that it will be useful,
                     15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     17: # GNU General Public License for more details.
                     18: #
                     19: # You should have received a copy of the GNU General Public License
                     20: # along with LON-CAPA; if not, write to the Free Software
                     21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     22: #
                     23: # http://www.lon-capa.org/
                     24: #
                     25: 
                     26: use strict;
                     27: use File::Copy;
                     28: use Term::ReadKey;
1.47      raeburn    29: use Socket;
1.46      raeburn    30: use Sys::Hostname::FQDN();
1.1       raeburn    31: use DBI;
1.43      raeburn    32: use Cwd();
                     33: use File::Basename();
                     34: use lib File::Basename::dirname(Cwd::abs_path($0));
1.1       raeburn    35: use LCLocalization::localize;
                     36: 
                     37: # ========================================================= The language handle
                     38: 
                     39: my %languages = (
                     40:                   ar => 'Arabic', 
                     41:                   de => 'German',
                     42:                   en => 'English',
                     43:                   es => 'Spanish (Castellan)',
                     44:                   fa => 'Persian',
                     45:                   fr => 'French', 
                     46:                   he => 'Hebrew', 
                     47:                   ja => 'Japanese',
                     48:                   pt => 'Portuguese',
                     49:                   ru => 'Russian',
                     50:                   tr => 'Turkish',
                     51:                   zh => 'Chinese Simplified'
                     52:                ); 
                     53: 
                     54: use vars qw($lh $lang);
                     55: $lang = 'en';
                     56: if (@ARGV > 0) {
                     57:     foreach my $poss (keys(%languages)) {
                     58:         if ($ARGV[0] eq $poss) {
                     59:             $lang = $ARGV[0]; 
                     60:         }
                     61:     }
                     62: }
                     63: 
                     64: &get_language_handle($lang);
                     65: 
                     66: # Check user has root privs
                     67: if (0 != $<) {
                     68:     print &mt('This script must be run as root.')."\n".
                     69:           &mt('Stopping execution.')."\n";
                     70:     exit;
                     71: }
                     72: 
                     73: 
                     74: # Globals: filehandle LOG is global.
                     75: if (!open(LOG,">>loncapa_install.log")) {
                     76:     print &mt('Unable to open log file.')."\n".
                     77:           &mt('Stopping execution.')."\n";
                     78:     exit;
                     79: } else {
1.65    ! raeburn    80:     print LOG '$Id: install.pl,v 1.64 2020/05/01 18:29:56 raeburn Exp $'."\n";
1.1       raeburn    81: }
                     82: 
                     83: #
1.2       raeburn    84: # Helper routines and routines to establish recommended actions
1.1       raeburn    85: #
                     86: 
                     87: sub get_language_handle {
                     88:     my @languages = @_;
                     89:     $lh=LCLocalization::localize->get_handle(@languages);
                     90: }
                     91: 
                     92: sub mt (@) {
                     93:     if ($lh) {
                     94:         if ($_[0] eq '') {
                     95:             if (wantarray) {
                     96:                 return @_;
                     97:             } else {
                     98:                 return $_[0];
                     99:             }
                    100:         } else {
                    101:             return $lh->maketext(@_);
                    102:         }
                    103:     } else {
                    104:         if (wantarray) {
                    105:             return @_;
                    106:         } else {
                    107:             return $_[0];
                    108:         }
                    109:     }
                    110: }
                    111: 
                    112: sub texthash {
                    113:     my (%hash) = @_;
                    114:     foreach (keys(%hash)) {
                    115:         $hash{$_}=&mt($hash{$_});
                    116:     }
                    117:     return %hash;
                    118: }
                    119: 
                    120: 
                    121: sub skip_if_nonempty {
                    122:     my ($string,$error)=@_;
                    123:     return if (! defined($error));
                    124:     chomp($string);chomp($error);
                    125:     if ($string ne '') {
                    126:         print_and_log("$error\n".&mt('Stopping execution.')."\n");
                    127:         return 1;
                    128:     }
                    129:     return;
                    130: }
                    131: 
                    132: sub writelog {
                    133:     while ($_ = shift) {
                    134:         chomp();
                    135:         print LOG "$_\n";
                    136:     }
                    137: }
                    138: 
                    139: sub print_and_log {
                    140:     while ($_=shift) {
                    141:         chomp();
                    142:         print "$_\n";
                    143:         print LOG "$_\n";
                    144:     }
                    145: }
                    146: 
                    147: sub get_user_selection {
                    148:     my ($defaultrun) = @_;
                    149:     my $do_action = 0;
                    150:     my $choice = <STDIN>;
                    151:     chomp($choice);
                    152:     $choice =~ s/(^\s+|\s+$)//g;
                    153:     my $yes = &mt('y');
                    154:     if ($defaultrun) {
                    155:         if (($choice eq '') || ($choice =~ /^\Q$yes\E/i)) {
                    156:             $do_action = 1;
                    157:         }
                    158:     } else {
                    159:         if ($choice =~ /^\Q$yes\E/i) {
                    160:             $do_action = 1;
                    161:         }
                    162:     }
                    163:     return $do_action;
                    164: }
                    165: 
                    166: sub get_distro {
1.49      raeburn   167:     my ($distro,$gotprereqs,$updatecmd,$packagecmd,$installnow,$unknown);
1.1       raeburn   168:     $packagecmd = '/bin/rpm -q LONCAPA-prerequisites ';
1.55      raeburn   169:     if (-e '/etc/oracle-release') {
                    170:         open(IN,'</etc/oracle-release');
                    171:         my $versionstring=<IN>;
                    172:         chomp($versionstring);
                    173:         close(IN);
                    174:         if ($versionstring =~ /^Oracle Linux Server release (\d+)/) {
                    175:             my $version = $1;
                    176:             $distro = 'oracle'.$1;
                    177:             $updatecmd = 'yum install LONCAPA-prerequisites';
                    178:             $installnow = 'yum -y install LONCAPA-prerequisites';
                    179:         }
                    180:     } elsif (-e '/etc/redhat-release') {
1.1       raeburn   181:         open(IN,'</etc/redhat-release');
                    182:         my $versionstring=<IN>;
                    183:         chomp($versionstring);
                    184:         close(IN);
                    185:         if ($versionstring =~ /^Red Hat Linux release ([\d\.]+) /) {
                    186:             my $version = $1;
                    187:             if ($version=~/^7\./) {
                    188:                 $distro='redhat7';
                    189:             } elsif ($version=~/^8\./) {
                    190:                 $distro='redhat8';
                    191:             } elsif ($version=~/^9/) {
                    192:                 $distro='redhat9';
                    193:             }
                    194:         } elsif ($versionstring =~ /Fedora( Core)? release ([\d\.]+) /) {
                    195:             my $version=$2;
                    196:             if ($version - int($version) > .9) {
                    197:                 $distro = 'fedora'.(int($version)+1);
                    198:             } else {
                    199:                 $distro = 'fedora'.int($version);
                    200:             }
                    201:             $updatecmd = 'yum install LONCAPA-prerequisites';
                    202:             $installnow = 'yum -y install LONCAPA-prerequisites';
                    203:         } elsif ($versionstring =~ /Red Hat Enterprise Linux [AE]S release ([\d\.]+) /) {
                    204:             $distro = 'rhes'.$1;
                    205:             $updatecmd = 'up2date -i LONCAPA-prerequisites';
                    206:         } elsif ($versionstring =~ /Red Hat Enterprise Linux Server release (\d+)/) {
                    207:             $distro = 'rhes'.$1;
                    208:             $updatecmd = 'yum install LONCAPA-prerequisites';
                    209:             $installnow = 'yum -y install LONCAPA-prerequisites';
1.54      raeburn   210:         } elsif ($versionstring =~ /Red Hat Enterprise Linux release (\d+)/) {
                    211:             $distro = 'rhes'.$1;
                    212:             $updatecmd = 'dnf install LONCAPA-prerequisites';
                    213:             $installnow = 'dnf -y install LONCAPA-prerequisites';
1.21      raeburn   214:         } elsif ($versionstring =~ /CentOS(?:| Linux) release (\d+)/) {
1.1       raeburn   215:             $distro = 'centos'.$1;
                    216:             $updatecmd = 'yum install LONCAPA-prerequisites';
                    217:             $installnow = 'yum -y install LONCAPA-prerequisites';
1.22      raeburn   218:         } elsif ($versionstring =~ /Scientific Linux (?:SL )?release ([\d.]+) /) {
1.1       raeburn   219:             my $ver = $1;
                    220:             $ver =~ s/\.\d+$//;
                    221:             $distro = 'scientific'.$ver;
                    222:             $updatecmd = 'yum install LONCAPA-prerequisites';
                    223:             $installnow = 'yum -y install LONCAPA-prerequisites';
                    224:         } else {
                    225:             print &mt('Unable to interpret [_1] to determine system type.',
                    226:                       '/etc/redhat-release')."\n";
1.49      raeburn   227:             $unknown = 1;
1.1       raeburn   228:         }
                    229:     } elsif (-e '/etc/SuSE-release') {
                    230:         open(IN,'</etc/SuSE-release');
                    231:         my $versionstring=<IN>;
                    232:         chomp($versionstring);
                    233:         close(IN);
                    234:         if ($versionstring =~ /^SUSE LINUX Enterprise Server ([\d\.]+) /i) {
                    235:             $distro='sles'.$1;
                    236:             if ($1 >= 10) {
                    237:                 $updatecmd = 'zypper install LONCAPA-prerequisites';
                    238:             } else {
                    239:                 $updatecmd = 'yast -i LONCAPA-prerequisites';
                    240:             }
                    241:         } elsif ($versionstring =~ /^SuSE Linux ([\d\.]+) /i) {
                    242:             $distro = 'suse'.$1;
1.12      raeburn   243:             $updatecmd = 'yast -i LONCAPA-prerequisites';
1.1       raeburn   244:         } elsif ($versionstring =~ /^openSUSE ([\d\.]+) /i) {
                    245:             $distro = 'suse'.$1;
                    246:             if ($1 >= 10.3 ) {
                    247:                 $updatecmd = 'zypper install LONCAPA-prerequisites';
                    248:             } else {
                    249:                 $updatecmd = 'yast -i LONCAPA-prerequisites';
                    250:             }
                    251:         } else {
                    252:             print &mt('Unable to interpret [_1] to determine system type.',
                    253:                       '/etc/SuSE-release')."\n";
1.49      raeburn   254:             $unknown = 1;
1.1       raeburn   255:         }
                    256:     } elsif (-e '/etc/issue') {
                    257:         open(IN,'</etc/issue');
                    258:         my $versionstring=<IN>;
                    259:         chomp($versionstring);
                    260:         close(IN);
                    261:         if ($versionstring =~ /^Ubuntu (\d+)\.\d+/i) {
                    262:             $distro = 'ubuntu'.$1;
                    263:             $updatecmd = 'sudo apt-get install loncapa-prerequisites';
                    264:         } elsif ($versionstring =~ /^Debian\s+GNU\/Linux\s+(\d+)\.\d+/i) {
                    265:             $distro = 'debian'.$1;
1.49      raeburn   266:             $updatecmd = 'apt-get install loncapa-prerequisites';
1.1       raeburn   267:         } elsif (-e '/etc/debian_version') {
                    268:             open(IN,'</etc/debian_version');
                    269:             my $version=<IN>;
                    270:             chomp($version);
                    271:             close(IN);
                    272:             if ($version =~ /^(\d+)\.\d+\.?\d*/) {
                    273:                 $distro='debian'.$1;
1.49      raeburn   274:                 $updatecmd = 'apt-get install loncapa-prerequisites';
1.1       raeburn   275:             } else {
                    276:                 print &mt('Unable to interpret [_1] to determine system type.',
                    277:                           '/etc/debian_version')."\n";
1.49      raeburn   278:                 $unknown = 1;
1.1       raeburn   279:             }
1.49      raeburn   280:         }
                    281:         if ($distro ne '') {
                    282:             $packagecmd = '/usr/bin/dpkg -l loncapa-prerequisites ';
1.1       raeburn   283:         }
                    284:     } elsif (-e '/etc/debian_version') {
                    285:         open(IN,'</etc/debian_version');
                    286:         my $version=<IN>;
                    287:         chomp($version);
                    288:         close(IN);
                    289:         if ($version =~  /^(\d+)\.\d+\.?\d*/) {
                    290:             $distro='debian'.$1;
                    291:             $packagecmd = '/usr/bin/dpkg -l loncapa-prerequisites ';
                    292:             $updatecmd = 'apt-get install loncapa-prerequisites';
                    293:         } else {
                    294:             print &mt('Unable to interpret [_1] to determine system type.',
                    295:                       '/etc/debian_version')."\n";
1.49      raeburn   296:             $unknown = 1;
                    297:         }
                    298:     }
                    299:     if (($distro eq '') && (!$unknown)) {
                    300:         if (-e '/etc/os-release') {
                    301:             if (open(IN,'<','/etc/os-release')) {
                    302:                 my ($id,$version);
                    303:                 while(<IN>) {
                    304:                     chomp();
                    305:                     if (/^ID="(\w+)"/) {
                    306:                         $id=$1;
                    307:                     } elsif (/^VERSION_ID="([\d\.]+)"/) {
                    308:                         $version=$1;
                    309:                     }
                    310:                 }
                    311:                 close(IN);
                    312:                 if ($id eq 'sles') {
                    313:                     my ($major,$minor) = split(/\./,$version);
                    314:                     if ($major =~ /^\d+$/) {
                    315:                         $distro = $id.$major;
                    316:                         $updatecmd = 'zypper install LONCAPA-prerequisites';
                    317:                     }
                    318:                 }
                    319:             }
                    320:             if ($distro eq '') {
                    321:                 print &mt('Unable to interpret [_1] to determine system type.',
                    322:                           '/etc/os-release')."\n";
                    323:                 $unknown = 1;
                    324:             }
                    325:         } else {
1.55      raeburn   326:             print &mt('Unknown installation: expecting a debian, ubuntu, suse, sles, redhat, fedora, scientific linux, or oracle linux system.')."\n";
1.1       raeburn   327:         }
                    328:     }
                    329:     return ($distro,$packagecmd,$updatecmd,$installnow);
                    330: }
                    331: 
1.47      raeburn   332: #
                    333: # get_hostname() prompts the user to provide the server's hostname.
                    334: #
                    335: # If invalid input is provided, the routine is called recursively 
                    336: # until, a valid hostname is provided.
                    337: # 
                    338: 
1.46      raeburn   339: sub get_hostname {
                    340:     my $hostname;
                    341:     print &mt('Enter the hostname of this server, e.g., loncapa.somewhere.edu'."\n");
                    342:     my $choice = <STDIN>;
                    343:     chomp($choice);
                    344:     $choice =~ s/(^\s+|\s+$)//g;
                    345:     if ($choice eq '') {
                    346:         print &mt("Hostname you entered was either blank or contanied only white space.\n");
                    347:     } elsif ($choice =~ /^[\w\.\-]+$/) {
                    348:         $hostname = $choice;
                    349:     } else {
                    350:         print &mt("Hostname you entered was invalid --  a hostname may only contain letters, numbers, - and .\n");
                    351:     }
                    352:     while ($hostname eq '') {
                    353:         $hostname = &get_hostname();
                    354:     }
                    355:     print "\n";
                    356:     return $hostname;
                    357: }
                    358: 
1.47      raeburn   359: #
1.62      raeburn   360: # get_hostip() prompts the user to provide the server's IPv4 IP address
1.47      raeburn   361: #
                    362: # If invalid input is provided, the routine is called recursively 
                    363: # until, a valid IPv4 address is provided.
                    364: #
                    365: 
                    366: sub get_hostip {
                    367:     my $hostip;
                    368:     print &mt('Enter the IP address of this server, e.g., 192.168.10.24'."\n");
                    369:     my $choice = <STDIN>;
                    370:     chomp($choice);
                    371:     $choice =~ s/(^\s+|\s+$)//g;
                    372:     my $badformat = 1;
                    373:     if ($choice eq '') {
                    374:         print &mt("IP address you entered was either blank or contained only white space.\n"); 
                    375:     } else {
                    376:         if ($choice =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/) {
                    377:             if (($1<=255) && ($2<=255) && ($3<=255) && ($4<=255)) {
                    378:                 $badformat = 0; 
                    379:             }
                    380:         }
                    381:         if ($badformat) { 
                    382:              print &mt('Host IP you entered was invalid -- a host IP has the format d.d.d.d where each d is an integer between 0 and 255')."\n";
                    383:         } else {
                    384:             $hostip = $choice;
                    385:         }
                    386:     }
                    387:     while ($hostip eq '') {
                    388:         $hostip = &get_hostip();
                    389:     }
                    390:     print "\n";
                    391:     return $hostip;
                    392: }
                    393: 
1.1       raeburn   394: sub check_prerequisites {
                    395:     my ($packagecmd,$distro) = @_;
                    396:     my $gotprereqs;
                    397:     if ($packagecmd ne '') {
                    398:         if (open(PIPE,"$packagecmd|")) {
                    399:             if ($distro =~ /^(debian|ubuntu)/) {
                    400:                 my @lines = <PIPE>;
                    401:                 chomp(@lines);
                    402:                 foreach my $line (@lines) {
                    403:                     if ($line =~ /^ii\s+loncapa-prerequisites\s+([\w\.]+)/) {
                    404:                         $gotprereqs = $1;
                    405:                     }
                    406:                 }
                    407:             } else {
                    408:                 my $line = <PIPE>;
                    409:                 chomp($line);
1.8       raeburn   410:                 if ($line =~ /^LONCAPA\-prerequisites\-([\d\-]+)\.(?:[.\w]+)$/) {
1.1       raeburn   411:                     $gotprereqs = $1;
                    412:                 }
                    413:             }
                    414:             close(PIPE);
                    415:         } else {
                    416:             print &mt('Error: could not determine if LONCAPA-prerequisites package is installed')."\n";
                    417:         }
                    418:     }
                    419:     return $gotprereqs;
                    420: }
                    421: 
1.6       raeburn   422: sub check_locale {
                    423:     my ($distro) = @_;
1.63      raeburn   424:     my ($fh,$langvar,$command,$earlyout);
1.8       raeburn   425:     $langvar = 'LANG';
1.6       raeburn   426:     if ($distro =~ /^(ubuntu|debian)/) {
                    427:         if (!open($fh,"</etc/default/locale")) {
                    428:             print &mt('Failed to open: [_1], default locale not checked.',
                    429:                       '/etc/default/locale');
1.63      raeburn   430:             $earlyout = 1;
1.6       raeburn   431:         }
1.49      raeburn   432:     } elsif ($distro =~ /^(suse|sles)(\d+)/) {
                    433:         if (($1 eq 'sles') && ($2 >= 15)) {
                    434:             if (!open($fh,"</etc/locale.conf")) {
                    435:                 print &mt('Failed to open: [_1], default locale not checked.',
                    436:                           '/etc/locale.conf');
1.63      raeburn   437:                 $earlyout = 1;
1.49      raeburn   438:             }
                    439:         } else {
                    440:             if (!open($fh,"</etc/sysconfig/language")) {
                    441:                 print &mt('Failed to open: [_1], default locale not checked.',
                    442:                           '/etc/sysconfig/language');
1.63      raeburn   443:                 $earlyout = 1;
1.49      raeburn   444:             }
                    445:             $langvar = 'RC_LANG';
1.8       raeburn   446:         }
1.24      raeburn   447:     } elsif ($distro =~ /^fedora(\d+)/) {
                    448:         if ($1 >= 18) {
                    449:             if (!open($fh,"</etc/locale.conf")) {
                    450:                 print &mt('Failed to open: [_1], default locale not checked.',
                    451:                           '/etc/locale.conf');
1.63      raeburn   452:                 $earlyout = 1;
1.24      raeburn   453:             }
                    454:         } elsif (!open($fh,"</etc/sysconfig/i18n")) {
                    455:             print &mt('Failed to open: [_1], default locale not checked.',
                    456:                       '/etc/sysconfig/i18n');
                    457:         }
1.56      raeburn   458:     } elsif ($distro =~ /^(?:rhes|centos|scientific|oracle)(\d+)/) {
1.29      raeburn   459:         if ($1 >= 7) {
                    460:             if (!open($fh,"</etc/locale.conf")) {
                    461:                 print &mt('Failed to open: [_1], default locale not checked.',
                    462:                           '/etc/locale.conf');
1.63      raeburn   463:                 $earlyout = 1;
1.29      raeburn   464:             }
                    465:         } elsif (!open($fh,"</etc/sysconfig/i18n")) {
                    466:             print &mt('Failed to open: [_1], default locale not checked.',
                    467:                       '/etc/sysconfig/i18n');
1.63      raeburn   468:             $earlyout = 1;
1.29      raeburn   469:         }
1.6       raeburn   470:     } else {
                    471:         if (!open($fh,"</etc/sysconfig/i18n")) {
                    472:             print &mt('Failed to open: [_1], default locale not checked.',
                    473:                       '/etc/sysconfig/i18n');
1.63      raeburn   474:             $earlyout = 1;
1.6       raeburn   475:         }
                    476:     }
1.63      raeburn   477:     return if ($earlyout);
1.6       raeburn   478:     my @data = <$fh>;
                    479:     chomp(@data);
                    480:     foreach my $item (@data) {
1.25      raeburn   481:         if ($item =~ /^\Q$langvar\E=\"?([^\"]*)\"?/) {
1.6       raeburn   482:             my $default = $1;
                    483:             if ($default ne 'en_US.UTF-8') {
                    484:                 if ($distro =~ /^debian/) {
1.25      raeburn   485:                     $command = 'locale-gen en_US.UTF-8'."\n".
                    486:                                'update-locale LANG=en_US.UTF-8';
1.6       raeburn   487:                 } elsif ($distro =~ /^ubuntu/) {
1.25      raeburn   488:                     $command = 'sudo locale-gen en_US.UTF-8'."\n".
                    489:                                'sudo update-locale LANG=en_US.UTF-8';
1.7       raeburn   490:                 } elsif ($distro =~ /^(suse|sles)/) {
1.57      raeburn   491:                     $command = 'yast language';
1.54      raeburn   492:                 } elsif (-e '/usr/bin/system-config-language') {
                    493:                     $command = 'system-config-language';
                    494:                 } elsif (-e '/usr/bin/localectl') {
1.58      raeburn   495:                     $command = '/usr/bin/localectl set-locale LANG=en_US.UTF-8';
1.6       raeburn   496:                 } else {
1.54      raeburn   497:                     $command = 'No standard command found';
1.6       raeburn   498:                 }
                    499:             }
                    500:             last;
                    501:         }
                    502:     }
                    503:     close($fh);
                    504:     return $command;
                    505: }
                    506: 
1.1       raeburn   507: sub check_required {
                    508:     my ($instdir,$dsn) = @_;
                    509:     my ($distro,$packagecmd,$updatecmd,$installnow) = &get_distro();
                    510:     if ($distro eq '') {
                    511:         return;
                    512:     }
                    513:     my $gotprereqs = &check_prerequisites($packagecmd,$distro); 
                    514:     if ($gotprereqs eq '') {
1.22      raeburn   515:         return ($distro,$gotprereqs,'',$packagecmd,$updatecmd);
1.6       raeburn   516:     }
                    517:     my $localecmd = &check_locale($distro);
                    518:     unless ($localecmd eq '') {
                    519:         return ($distro,$gotprereqs,$localecmd);
1.1       raeburn   520:     }
1.34      raeburn   521:     my ($mysqlon,$mysqlsetup,$mysqlrestart,$dbh,$has_pass,$has_lcdb,%recommended,
1.46      raeburn   522:         $downloadstatus,$filetouse,$production,$testing,$apachefw,$tostop,
1.47      raeburn   523:         $uses_systemctl,$hostname,$hostip);
1.1       raeburn   524:     my $wwwuid = &uid_of_www();
                    525:     my $wwwgid = getgrnam('www');
                    526:     if (($wwwuid eq '') || ($wwwgid eq '')) {
                    527:         $recommended{'wwwuser'} = 1;
                    528:     }
                    529:     unless( -e "/usr/local/sbin/pwauth") {
                    530:         $recommended{'pwauth'} = 1;
                    531:     }
1.47      raeburn   532:     $hostname = Sys::Hostname::FQDN::fqdn();
1.46      raeburn   533:     if ($hostname eq '') {
                    534:         $hostname =&get_hostname();
                    535:     } else {
                    536:         print &mt("Hostname detected: $hostname. Is that correct? ~[Y/n~]");
                    537:         if (!&get_user_selection(1)) {
                    538:             $hostname =&get_hostname();
                    539:         }
                    540:     }
1.47      raeburn   541:     $hostip = Socket::inet_ntoa(scalar(gethostbyname($hostname)) || 'localhost');
                    542:     if ($hostip eq '') {
                    543:         $hostip=&get_hostip();
                    544:     } else {
                    545:         print &mt("Host IP address detected: $hostip. Is that correct? ~[Y/n~]");
                    546:         if (!&get_user_selection(1)) {
                    547:             $hostip=&get_hostip();
                    548:         }
                    549:     }
                    550:     print_and_log("\n".&mt('Hostname is [_1] and IP address is [_2]',$hostname,$hostip)."\n");
1.1       raeburn   551:     $mysqlon = &check_mysql_running($distro);
                    552:     if ($mysqlon) {
                    553:         my $mysql_has_wwwuser = &check_mysql_wwwuser();
1.34      raeburn   554:         ($mysqlsetup,$has_pass,$dbh,$mysql_has_wwwuser) = 
                    555:             &check_mysql_setup($instdir,$dsn,$distro,$mysql_has_wwwuser);
                    556:         if ($mysqlsetup eq 'needsrestart') {
                    557:             $mysqlrestart = '';
                    558:             if ($distro eq 'ubuntu') {
                    559:                 $mysqlrestart = 'sudo '; 
                    560:             }
                    561:             $mysqlrestart .= 'service mysql restart';
                    562:             return ($distro,$gotprereqs,$localecmd,$packagecmd,$updatecmd,$installnow,$mysqlrestart);
1.1       raeburn   563:         } else {
1.34      raeburn   564:             if ($mysqlsetup eq 'noroot') {
1.1       raeburn   565:                 $recommended{'mysqlperms'} = 1;
1.34      raeburn   566:             } else {
                    567:                 unless ($mysql_has_wwwuser) {
                    568:                     $recommended{'mysqlperms'} = 1;
                    569:                 }
                    570:             }
                    571:             if ($dbh) {
                    572:                 $has_lcdb = &check_loncapa_mysqldb($dbh);
                    573:             }
                    574:             unless ($has_lcdb) {
                    575:                 $recommended{'mysql'} = 1;
1.1       raeburn   576:             }
                    577:         }
                    578:     }
1.47      raeburn   579:     my ($sslhostsfilesref,$has_std,$has_int,$rewritenum,$nochgstd,$nochgint);
1.5       raeburn   580:     ($recommended{'firewall'},$apachefw) = &chkfirewall($distro);
1.35      raeburn   581:     ($recommended{'runlevels'},$tostop,$uses_systemctl) = &chkconfig($distro,$instdir);
1.1       raeburn   582:     $recommended{'apache'} = &chkapache($distro,$instdir);
1.47      raeburn   583:     ($recommended{'apachessl'},$sslhostsfilesref,$has_std,$has_int,$rewritenum,
                    584:      $nochgstd,$nochgint) = &chkapachessl($distro,$instdir,$hostname,$hostip);
1.1       raeburn   585:     $recommended{'stopsrvcs'} = &chksrvcs($distro,$tostop);
                    586:     ($recommended{'download'},$downloadstatus,$filetouse,$production,$testing) 
                    587:         = &need_download();
1.6       raeburn   588:     return ($distro,$gotprereqs,$localecmd,$packagecmd,$updatecmd,$installnow,
1.34      raeburn   589:             $mysqlrestart,\%recommended,$dbh,$has_pass,$has_lcdb,$downloadstatus,
1.47      raeburn   590:             $filetouse,$production,$testing,$apachefw,$uses_systemctl,$hostname,
                    591:             $hostip,$sslhostsfilesref,$has_std,$has_int,$rewritenum,$nochgstd,
                    592:             $nochgint);
1.1       raeburn   593: }
                    594: 
                    595: sub check_mysql_running {
                    596:     my ($distro) = @_;
1.23      raeburn   597:     my $use_systemctl;
1.1       raeburn   598:     my $mysqldaemon ='mysqld';
                    599:     if ($distro =~ /^(suse|sles|debian|ubuntu)/) {
                    600:         $mysqldaemon = 'mysql';
                    601:     }
1.6       raeburn   602:     my $process = 'mysqld_safe';
                    603:     my $proc_owner = 'root';
                    604:     if ($distro =~ /^ubuntu(\w+)/) {
                    605:         if ($1 >= 10) {
                    606:             $process = 'mysqld';
                    607:             $proc_owner = 'mysql';
                    608:         }
1.35      raeburn   609:     } elsif ($distro =~ /^fedora(\d+)/) {
1.23      raeburn   610:         if ($1 >= 16) {
                    611:             $process = 'mysqld';
                    612:             $proc_owner = 'mysql';
                    613:             $use_systemctl = 1;
                    614:         }
1.39      raeburn   615:         if ($1 >= 19) {
1.37      raeburn   616:             $mysqldaemon ='mariadb';
                    617:         }
1.56      raeburn   618:     } elsif ($distro =~ /^(?:centos|rhes|scientific|oracle)(\d+)/) {
1.29      raeburn   619:         if ($1 >= 7) {
                    620:             $mysqldaemon ='mariadb';
                    621:             $process = 'mysqld';
                    622:             $proc_owner = 'mysql';
                    623:             $use_systemctl = 1;
                    624:         }
1.35      raeburn   625:     } elsif ($distro =~ /^sles(\d+)/) {
                    626:         if ($1 >= 12) {
                    627:             $use_systemctl = 1;
1.42      raeburn   628:             $proc_owner = 'mysql';
                    629:             $process = 'mysqld';
1.35      raeburn   630:         }
1.49      raeburn   631:         if ($1 >= 15) {
                    632:             $mysqldaemon ='mariadb';
                    633:         }
1.35      raeburn   634:     } elsif ($distro =~ /^suse(\d+)/) {
                    635:         if ($1 >= 13) {
                    636:             $use_systemctl = 1;
                    637:         }
1.29      raeburn   638:     }
1.35      raeburn   639:     if (open(PIPE,"ps -ef |grep $process |grep ^$proc_owner |grep -v grep 2>&1 |")) {
1.1       raeburn   640:         my $status = <PIPE>;
                    641:         close(PIPE);
                    642:         chomp($status);
1.6       raeburn   643:         if ($status =~ /^\Q$proc_owner\E\s+\d+\s+/) {
1.1       raeburn   644:             print_and_log(&mt('MySQL is running.')."\n");
                    645:             return 1;
                    646:         } else {
1.23      raeburn   647:             if ($use_systemctl) {
                    648:                 system("/bin/systemctl start $mysqldaemon.service >/dev/null 2>&1 ");
                    649:             } else {
                    650:                 system("/etc/init.d/$mysqldaemon start >/dev/null 2>&1 ");
                    651:             }
1.1       raeburn   652:             print_and_log(&mt('Waiting for MySQL to start.')."\n");
                    653:             sleep 5;
1.12      raeburn   654:             if (open(PIPE,"ps -ef |grep $process |grep -v grep 2>&1 |")) {
                    655:                 $status = <PIPE>;
1.1       raeburn   656:                 close(PIPE);
                    657:                 chomp($status);
1.12      raeburn   658:                 if ($status =~ /^\Q$proc_owner\E\s+\d+\s+/) {
1.1       raeburn   659:                     print_and_log(&mt('MySQL is running.')."\n");
                    660:                     return 1;
                    661:                 } else {
1.12      raeburn   662:                     print_and_log(&mt('Still waiting for MySQL to start.')."\n");
                    663:                     sleep 5;
                    664:                     if (open(PIPE,"ps -ef |grep $process |grep -v grep 2>&1 |")) {
                    665:                         $status = <PIPE>;
                    666:                         close(PIPE);
                    667:                         chomp($status);
                    668:                         if ($status =~ /^\Q$proc_owner\E\s+\d+\s+/) {
                    669:                             print_and_log(&mt('MySQL is running.')."\n");
                    670:                             return 1;
                    671:                         } else {
                    672:                             print_and_log(&mt('Given up waiting for MySQL to start.')."\n"); 
                    673:                         }
                    674:                     }
1.1       raeburn   675:                 }
                    676:             }
                    677:         }
                    678:     } else {
                    679:         print &mt('Could not determine if MySQL is running.')."\n";
                    680:     }
                    681:     return;
                    682: }
                    683: 
                    684: sub chkconfig {
1.8       raeburn   685:     my ($distro,$instdir) = @_;
1.23      raeburn   686:     my (%needfix,%tostop,%uses_systemctl);
1.1       raeburn   687:     my $checker_bin = '/sbin/chkconfig';
1.23      raeburn   688:     my $sysctl_bin = '/bin/systemctl';
1.6       raeburn   689:     my %daemon = (
                    690:                   mysql     => 'mysqld',
                    691:                   apache    => 'httpd',
                    692:                   cups      => 'cups',
                    693:                   ntp       => 'ntpd',
                    694:                   memcached => 'memcached',
                    695:     );
1.1       raeburn   696:     my @runlevels = qw/3 4 5/;
                    697:     my @norunlevels = qw/0 1 6/;
                    698:     if ($distro =~ /^(suse|sles)/) {
                    699:         @runlevels = qw/3 5/;
                    700:         @norunlevels = qw/0 2 1 6/;
1.6       raeburn   701:         $daemon{'mysql'} = 'mysql';
                    702:         $daemon{'apache'} = 'apache2';
1.8       raeburn   703:         $daemon{'ntp'}    = 'ntp';
1.1       raeburn   704:         if ($distro =~ /^(suse|sles)9/) {
1.6       raeburn   705:             $daemon{'apache'} = 'apache';
1.1       raeburn   706:         }
1.35      raeburn   707:         if ($distro =~ /^(suse|sles)([\d\.]+)/) {
                    708:             my $name = $1;
                    709:             my $num = $2;
                    710:             if ($num > 11) {
1.26      raeburn   711:                 $uses_systemctl{'apache'} = 1;
1.35      raeburn   712:                 if (($name eq 'sles') || ($name eq 'suse' && $num >= 13.2)) {
                    713:                     $uses_systemctl{'mysql'} = 1;
                    714:                     $uses_systemctl{'ntp'} = 1;
                    715:                     $uses_systemctl{'cups'} = 1;
                    716:                     $uses_systemctl{'memcached'} = 1;
1.49      raeburn   717:                     if (($name eq 'sles') && ($num >= 15)) {
                    718:                         $daemon{'ntp'} = 'chronyd';
                    719:                         $daemon{'mysql'} = 'mariadb';
                    720:                     } else {
                    721:                         $daemon{'ntp'} = 'ntpd';
                    722:                     }
1.35      raeburn   723:                 }
1.26      raeburn   724:             }
                    725:         }
1.6       raeburn   726:     } elsif ($distro =~ /^(?:debian|ubuntu)(\d+)/) {
                    727:         my $version = $1;
1.1       raeburn   728:         @runlevels = qw/2 3 4 5/;
                    729:         @norunlevels = qw/0 1 6/;
1.44      raeburn   730:         if (($distro =~ /^ubuntu/) && ($version <= 16)) {
                    731:             $checker_bin = '/usr/sbin/sysv-rc-conf';
                    732:         } else {
                    733:             $uses_systemctl{'ntp'} = 1;
                    734:             $uses_systemctl{'mysql'} = 1;
                    735:             $uses_systemctl{'apache'} = 1;
                    736:             $uses_systemctl{'memcached'} = 1;
                    737:             $uses_systemctl{'cups'} = 1;
                    738:         }
1.6       raeburn   739:         $daemon{'mysql'}  = 'mysql';
                    740:         $daemon{'apache'} = 'apache2';
                    741:         $daemon{'ntp'}    = 'ntp';
                    742:         if (($distro =~ /^ubuntu/) && ($version <= 8)) {
                    743:             $daemon{'cups'} = 'cupsys';
                    744:         }
1.61      raeburn   745:         if (($distro =~ /^ubuntu/) && ($version >= 18)) {
                    746:             $daemon{'ntp'}    = 'chrony';
                    747:         }
1.26      raeburn   748:     } elsif ($distro =~ /^fedora(\d+)/) {
1.23      raeburn   749:         my $version = $1;
                    750:         if ($version >= 15) {
                    751:             $uses_systemctl{'ntp'} = 1;
                    752:         }
                    753:         if ($version >= 16) {
                    754:             $uses_systemctl{'mysql'} = 1;
                    755:             $uses_systemctl{'apache'} = 1;
1.35      raeburn   756:             $uses_systemctl{'memcached'} = 1;
                    757:             $uses_systemctl{'cups'} = 1;
1.23      raeburn   758:         }
1.39      raeburn   759:         if ($version >= 19) {
1.37      raeburn   760:             $daemon{'mysql'} = 'mariadb';
                    761:         }
1.59      raeburn   762:         if ($version >= 26) {
                    763:             $daemon{'ntp'} = 'chronyd';
                    764:         }
1.56      raeburn   765:     } elsif ($distro =~ /^(?:centos|rhes|scientific|oracle)(\d+)/) {
1.29      raeburn   766:         my $version = $1;
                    767:         if ($version >= 7) {
                    768:             $uses_systemctl{'ntp'} = 1;
                    769:             $uses_systemctl{'mysql'} = 1;
                    770:             $uses_systemctl{'apache'} = 1;
1.35      raeburn   771:             $uses_systemctl{'memcached'} = 1;
                    772:             $uses_systemctl{'cups'} = 1;
1.30      raeburn   773:             $daemon{'mysql'} = 'mariadb';
1.29      raeburn   774:         }
1.56      raeburn   775:         if (($version >= 8) || ($distro eq 'oracle7')) {
1.54      raeburn   776:             $daemon{'ntp'} = 'chronyd';
                    777:         }
1.1       raeburn   778:     }
1.23      raeburn   779:     my $nocheck;
1.1       raeburn   780:     if (! -x $checker_bin) {
1.23      raeburn   781:         if ($uses_systemctl{'mysql'} && $uses_systemctl{'apache'}) {
                    782:             if (! -x $sysctl_bin) {
                    783:                 $nocheck = 1;       
                    784:             }
                    785:         } else {
                    786:             $nocheck = 1;
                    787:         }
                    788:     }
                    789:     if ($nocheck) {
1.6       raeburn   790:         print &mt('Could not check runlevel status for MySQL or Apache')."\n";
1.1       raeburn   791:         return;
                    792:     }
                    793:     my $rlstr = join('',@runlevels);
                    794:     my $nrlstr = join('',@norunlevels);
1.23      raeburn   795: 
1.6       raeburn   796:     foreach my $type ('apache','mysql','ntp','cups','memcached') {
                    797:         my $service = $daemon{$type};
1.23      raeburn   798:         if ($uses_systemctl{$type}) {
1.35      raeburn   799:             if (($type eq 'memcached') || ($type eq 'cups')) {
                    800:                 if (-l "/etc/systemd/system/multi-user.target.wants/$service.service") {
                    801:                     $tostop{$type} = 1;
                    802:                 }
                    803:             } else {
                    804:                 if (!-l "/etc/systemd/system/multi-user.target.wants/$service.service") {
                    805:                     $needfix{$type} = "systemctl enable $service.service";
                    806:                 }
1.23      raeburn   807:             }
                    808:         } else {
                    809:             my $command = $checker_bin.' --list '.$service.' 2>/dev/null';
1.35      raeburn   810:             if ($type eq 'cups') {
1.23      raeburn   811:                 if ($distro =~ /^(?:debian|ubuntu)(\d+)/) {
                    812:                     my $version = $1;
                    813:                     if (($distro =~ /^ubuntu/) && ($version <= 8)) {
                    814:                         $command = $checker_bin.' --list cupsys 2>/dev/null';
1.19      raeburn   815:                     }
                    816:                 }
                    817:             }
1.23      raeburn   818:             my $results = `$command`;
                    819:             my $tofix;
                    820:             if ($results eq '') {
                    821:                 if (($type eq 'apache') || ($type eq 'mysql') || ($type eq 'ntp')) {
                    822:                     if ($distro  =~ /^(debian|ubuntu)/) {
                    823:                         $tofix = "update-rc.d $type defaults";
                    824:                     } else {
                    825:                         $tofix = "$checker_bin --add $service\n";
                    826:                     }
1.6       raeburn   827:                 }
1.23      raeburn   828:             } else {
                    829:                 my %curr_runlevels;
                    830:                 for (my $rl=0; $rl<=6; $rl++) {
                    831:                     if ($results =~ /$rl:on/) { $curr_runlevels{$rl}++; }
                    832:                 }
                    833:                 if (($type eq 'apache') || ($type eq 'mysql') || ($type eq 'ntp')) {
                    834:                     my $warning;
                    835:                     foreach my $rl (@runlevels) {
                    836:                         if (!exists($curr_runlevels{$rl})) {
                    837:                             $warning = 1;
                    838:                         }
                    839:                     }
                    840:                     if ($warning) {
                    841:                         $tofix = "$checker_bin --level $rlstr $service on\n";
                    842:                     }
                    843:                 } elsif (keys(%curr_runlevels) > 0) {
                    844:                     $tostop{$type} = 1;
1.1       raeburn   845:                 }
                    846:             }
1.23      raeburn   847:             if ($tofix) {
                    848:                 $needfix{$type} = $tofix;
1.1       raeburn   849:             }
1.5       raeburn   850:         }
1.1       raeburn   851:     }
                    852:     if ($distro =~ /^(suse|sles)([\d\.]+)$/) {
                    853:         my $name = $1;
                    854:         my $version = $2;
                    855:         my ($major,$minor);
                    856:         if ($name eq 'suse') {
                    857:             ($major,$minor) = split(/\./,$version);
                    858:         } else {
                    859:             $major = $version;
                    860:         }
1.49      raeburn   861:         if (($major > 10) && ($major <= 13)) {
1.8       raeburn   862:             if (&check_SuSEfirewall2_setup($instdir)) {
                    863:                 $needfix{'insserv'} = 1;
                    864:             }
1.1       raeburn   865:         }
                    866:     }
1.35      raeburn   867:     return (\%needfix,\%tostop,\%uses_systemctl);
1.1       raeburn   868: }
                    869: 
1.49      raeburn   870: sub uses_firewalld {
                    871:     my ($distro) = @_;
1.53      raeburn   872:     my ($inuse,$checkfirewalld,$zone);
1.49      raeburn   873:     if ($distro =~ /^(suse|sles)([\d\.]+)$/) {
                    874:         if (($1 eq 'sles') && ($2 >= 15)) {
                    875:             $checkfirewalld = 1;
                    876:         }
                    877:     } elsif ($distro =~ /^fedora(\d+)$/) {
                    878:         if ($1 >= 18) {
                    879:             $checkfirewalld = 1;
                    880:         }
1.56      raeburn   881:     } elsif ($distro =~ /^(?:centos|rhes|scientific|oracle)(\d+)/) {
1.49      raeburn   882:         if ($1 >= 7) {
                    883:             $checkfirewalld = 1;
                    884:         }
                    885:     }
                    886:     if ($checkfirewalld) {
                    887:         my ($loaded,$active);
                    888:         if (open(PIPE,"systemctl status firewalld |")) {
                    889:             while (<PIPE>) {
                    890:                 chomp();
                    891:                 if (/^\s*Loaded:\s+(\w+)/) {
                    892:                     $loaded = $1;
                    893:                 }
                    894:                 if (/^\s*Active\s+(\w+)/) {
                    895:                     $active = $1;
                    896:                 }
                    897:             }
                    898:             close(PIPE);
                    899:         }
                    900:         if (($loaded eq 'loaded') || ($active eq 'active')) {
                    901:             $inuse = 1;
1.53      raeburn   902:             my $cmd = 'firewall-cmd --get-default-zone';
                    903:             if (open(PIPE,"$cmd |")) {
                    904:                 my $result = <PIPE>;
                    905:                 chomp($result);
                    906:                 close(PIPE);
                    907:                 if ($result =~ /^\w+$/) {
                    908:                     $zone = $result;
                    909:                 }
                    910:             }
1.49      raeburn   911:         }
                    912:     }
1.53      raeburn   913:     return ($inuse,$zone);
1.49      raeburn   914: }
                    915: 
1.1       raeburn   916: sub chkfirewall {
1.5       raeburn   917:     my ($distro) = @_;
1.1       raeburn   918:     my $configfirewall = 1;
                    919:     my %ports = (
                    920:                     http  =>  80,
                    921:                     https => 443,
                    922:                 );
1.5       raeburn   923:     my %activefw;
1.53      raeburn   924:     my ($firewalld,$zone) = &uses_firewalld($distro);
                    925:     if ($firewalld) {
                    926:         my %current;
                    927:         if (open(PIPE,'firewall-cmd --permanent --zone='.$zone.' --list-services |')) {
                    928:             my $svc = <PIPE>;
                    929:             close(PIPE);
                    930:             chomp($svc);
                    931:             map { $current{$_} = 1; } (split(/\s+/,$svc));
                    932:         }
                    933:         if ($current{'http'} && $current{'https'}) {
                    934:             $configfirewall = 0;
                    935:         }
                    936:     } else {
                    937:         if (&firewall_is_active()) {
1.49      raeburn   938:             my $iptables = &get_pathto_iptables();
                    939:             if ($iptables eq '') {
                    940:                 print &mt('Firewall not checked as path to iptables not determined.')."\n";
                    941:             } else {
                    942:                 my @fwchains = &get_fw_chains($iptables,$distro);
                    943:                 if (@fwchains) {
                    944:                     foreach my $service ('http','https') {
                    945:                         foreach my $fwchain (@fwchains) {
                    946:                             if (&firewall_is_port_open($iptables,$fwchain,$ports{$service})) {
                    947:                                 $activefw{$service} = 1;
                    948:                                 last;
                    949:                             }
1.1       raeburn   950:                         }
                    951:                     }
1.49      raeburn   952:                     if ($activefw{'http'}) {
                    953:                         $configfirewall = 0;
                    954:                     }
                    955:                 } else {
                    956:                     print &mt('Firewall not checked as iptables Chains not identified.')."\n";
1.1       raeburn   957:                 }
                    958:             }
1.53      raeburn   959:         } else {
                    960:             print &mt('Firewall not enabled.')."\n";
1.1       raeburn   961:         }
                    962:     }
1.5       raeburn   963:     return ($configfirewall,\%activefw);
1.1       raeburn   964: }
                    965: 
                    966: sub chkapache {
                    967:     my ($distro,$instdir) = @_;
                    968:     my $fixapache = 1;
1.28      raeburn   969:     if ($distro =~ /^(debian|ubuntu)(\d+)$/) {
                    970:         my $distname = $1;
                    971:         my $version = $2;
1.33      raeburn   972:         my ($stdconf,$stdsite);
                    973:         if (($distname eq 'ubuntu') && ($version > 12)) {
                    974:             $stdconf = "$instdir/debian-ubuntu/ubuntu14/loncapa_conf";
                    975:             $stdsite = "$instdir/debian-ubuntu/ubuntu14/loncapa_sites";
                    976:         } else {
                    977:             $stdconf = "$instdir/debian-ubuntu/loncapa"; 
                    978:         }
                    979:         if (!-e $stdconf) {
1.1       raeburn   980:             $fixapache = 0;
                    981:             print &mt('Warning: No LON-CAPA Apache configuration file found for installation check.')."\n"; 
1.28      raeburn   982:         } else {
1.33      raeburn   983:             my ($configfile,$sitefile);
1.28      raeburn   984:             if (($distname eq 'ubuntu') && ($version > 12)) {
1.33      raeburn   985:                 $sitefile = '/etc/apache2/sites-available/loncapa';
1.28      raeburn   986:                 $configfile = "/etc/apache2/conf-available/loncapa";
1.33      raeburn   987:             } else {
                    988:                 $configfile = "/etc/apache2/sites-available/loncapa";
1.28      raeburn   989:             }
1.33      raeburn   990:             if (($configfile ne '') && (-e $configfile) && (-e $stdconf))  {
                    991:                 if (open(PIPE, "diff --brief $stdconf $configfile |")) {
1.28      raeburn   992:                     my $diffres = <PIPE>;
                    993:                     close(PIPE);
                    994:                     chomp($diffres);
                    995:                     unless ($diffres) {
                    996:                         $fixapache = 0;
                    997:                     }
1.1       raeburn   998:                 }
                    999:             }
1.33      raeburn  1000:             if ((!$fixapache) && ($distname eq 'ubuntu') && ($version > 12)) {
                   1001:                 if (($sitefile ne '') && (-e $sitefile) && (-e $stdsite)) {
                   1002:                     if (open(PIPE, "diff --brief $stdsite $sitefile |")) {
                   1003:                         my $diffres = <PIPE>;
                   1004:                         close(PIPE);
                   1005:                         chomp($diffres);
                   1006:                         unless ($diffres) {
                   1007:                             $fixapache = 0;
                   1008:                         }
                   1009:                     }
                   1010:                 }
                   1011:             }
1.1       raeburn  1012:         }
1.6       raeburn  1013:         if (!$fixapache) {
                   1014:             foreach my $module ('headers.load','expires.load') {
                   1015:                 unless (-l "/etc/apache2/mods-enabled/$module") {
                   1016:                     $fixapache = 1;
                   1017:                 }
                   1018:             }
                   1019:         }
1.64      raeburn  1020:         if ((!$fixapache) && ($distname eq 'ubuntu')) {
                   1021:             my $sitestatus = "/etc/apache2/mods-available/status.conf";
                   1022:             my $stdstatus = "$instdir/debian-ubuntu/status.conf";
                   1023:             if ((-e $stdstatus) && (-e $sitestatus)) {
                   1024:                 if (open(PIPE, "diff --brief $stdstatus $sitestatus |")) {
                   1025:                     my $diffres = <PIPE>;
                   1026:                     close(PIPE);
                   1027:                     chomp($diffres);
                   1028:                     if ($diffres) {
                   1029:                         $fixapache = 1;
                   1030:                     }
                   1031:                 }
                   1032:             }
                   1033:         }
1.49      raeburn  1034:     } elsif ($distro =~ /^(suse|sles)([\d\.]+)$/) {
                   1035:         my ($name,$version) = ($1,$2);
1.1       raeburn  1036:         my $apache = 'apache';
1.57      raeburn  1037:         my $conf_file = "$instdir/sles-suse/default-server.conf";
1.49      raeburn  1038:         if ($version >= 10) {
1.8       raeburn  1039:             $apache = 'apache2';
1.1       raeburn  1040:         }
1.49      raeburn  1041:         if (($name eq 'sles') && ($version >= 12)) {
                   1042:             $conf_file = "$instdir/sles-suse/apache2.4/default-server.conf";
                   1043:         }
1.57      raeburn  1044:         if (!-e $conf_file) {
1.1       raeburn  1045:             $fixapache = 0;
                   1046:             print &mt('Warning: No LON-CAPA Apache configuration file found for installation check.')."\n";
1.49      raeburn  1047:         } elsif (-e "/etc/$apache/default-server.conf") {
                   1048:             if (open(PIPE, "diff --brief $conf_file /etc/$apache/default-server.conf |")) {
1.9       raeburn  1049:                 my $diffres = <PIPE>;
                   1050:                 close(PIPE);
                   1051:                 chomp($diffres);
                   1052:                 unless ($diffres) {
                   1053:                     $fixapache = 0;
                   1054:                 }
                   1055:             }
                   1056:         }
                   1057:     } elsif ($distro eq 'rhes4') {
                   1058:         if (!-e "$instdir/rhes4/httpd.conf") {
                   1059:             $fixapache = 0;
                   1060:             print &mt('Warning: No LON-CAPA Apache configuration file found for installation check.')."\n";
                   1061:         } elsif ((-e "/etc/httpd/conf/httpd.conf") && (-e "$instdir/rhes4/httpd.conf")) {
                   1062:             if (open(PIPE, "diff --brief $instdir/rhes4/httpd.conf /etc/httpd/conf/httpd.conf |")) {
1.1       raeburn  1063:                 my $diffres = <PIPE>;
                   1064:                 close(PIPE);
                   1065:                 chomp($diffres);
                   1066:                 unless ($diffres) {
                   1067:                     $fixapache = 0;
                   1068:                 }
                   1069:             }
                   1070:         }
                   1071:     } else {
1.14      raeburn  1072:         my $configfile = 'httpd.conf';
1.59      raeburn  1073:         my $mpmfile = 'mpm.conf';
1.56      raeburn  1074:         if ($distro =~ /^(?:centos|rhes|scientific|oracle)(\d+)$/) {
1.29      raeburn  1075:             if ($1 >= 7) {
                   1076:                 $configfile = 'apache2.4/httpd.conf';
                   1077:             } elsif ($1 > 5) {
1.14      raeburn  1078:                 $configfile = 'new/httpd.conf';
                   1079:             }
                   1080:         } elsif ($distro =~ /^fedora(\d+)$/) {
1.29      raeburn  1081:             if ($1 > 17) {
1.59      raeburn  1082:                 $configfile = 'apache2.4/httpd.conf';
1.29      raeburn  1083:             } elsif ($1 > 10) {
1.15      raeburn  1084:                 $configfile = 'new/httpd.conf';
1.14      raeburn  1085:             }
                   1086:         }
                   1087:         if (!-e "$instdir/centos-rhes-fedora-sl/$configfile") {
1.1       raeburn  1088:             $fixapache = 0;
                   1089:             print &mt('Warning: No LON-CAPA Apache configuration file found for installation check.')."\n";
1.14      raeburn  1090:         } elsif ((-e "/etc/httpd/conf/httpd.conf") && (-e "$instdir/centos-rhes-fedora-sl/$configfile")) {
                   1091:             if (open(PIPE, "diff --brief $instdir/centos-rhes-fedora-sl/$configfile /etc/httpd/conf/httpd.conf |")) {
1.1       raeburn  1092:                 my $diffres = <PIPE>;
                   1093:                 close(PIPE);
                   1094:                 chomp($diffres);
                   1095:                 unless ($diffres) {
                   1096:                     $fixapache = 0;
                   1097:                 }
                   1098:             }
                   1099:         }
1.59      raeburn  1100:         if (-e "/etc/httpd/conf.modules.d/00-mpm.conf") {
                   1101:             if (!-e "$instdir/centos-rhes-fedora-sl/$mpmfile") {
                   1102:                 print &mt('Warning: No LON-CAPA Apache MPM configuration file found for installation check.')."\n";
                   1103:             } elsif ((-e "/etc/httpd/conf.modules.d/00-mpm.conf") && (-e "$instdir/centos-rhes-fedora-sl/$mpmfile")) {
                   1104:                 if (open(PIPE, "diff --brief $instdir/centos-rhes-fedora-sl/$mpmfile /etc/httpd/conf.modules.d/00-mpm.conf |")) {
                   1105:                     my $diffres = <PIPE>;
                   1106:                     close(PIPE);
                   1107:                     chomp($diffres);
                   1108:                     if ($diffres) {
                   1109:                         $fixapache = 1;
                   1110:                     }
                   1111:                 }
                   1112:             }
                   1113:         }
1.1       raeburn  1114:     }
                   1115:     return $fixapache;
                   1116: }
                   1117: 
1.47      raeburn  1118: #
                   1119: # chkapachessl() determines whether a server's Apache SSL configuration
                   1120: # needs updating to support LON-CAPA.
                   1121: #
                   1122: # LON-CAPA uses VirtualHosts for port 443, and requires that they are 
                   1123: # defined in one Apache configuration file containing two VirtualHost
                   1124: # blocks, in order:
                   1125: #
                   1126: # (1) a block with no ServerName, or with ServerName set to the
                   1127: #     server's hostname. This block should contain:
                   1128: #
                   1129: # <IfModule mod_rewrite.c>
                   1130: # LON-CAPA rewrite rules defined in sslrewrite.conf
                   1131: # </IfModule>
                   1132: #
                   1133: # (2) a block with ServerName set to internal-$hostname
                   1134: #     (where $hostname is server's hostname).
                   1135: #    This block should contain the config and rewrite rules
                   1136: #    found in loncapassl.conf.
                   1137: #
                   1138: # chkapachessl() retrieves the names of .conf files in
                   1139: # the directory appropriate for the particular Linux distro,
                   1140: # and then checks to see which .conf file is the best candidate as
                   1141: # the single file containing VirtualHosts definitions and 
                   1142: # <IfModule mod_rewrite.c> </IfModule> rewrite blocks.
                   1143: #
                   1144: # The best candidate is the one containing a block:
                   1145: # <VirtualHost ????? :443> 
                   1146: # (where ????? might be _default_ or * or an IP address)
                   1147: # <IfModule mod_rewrite.c>
                   1148: # </IfModule>
                   1149: # </VirtualHost>
                   1150: # with the fewest differences between the contents of the 
                   1151: # IfModule block and the expected contents (from sslrewrite.conf)
                   1152: #
                   1153: # If there are no files with rewrite blocks, then a candidate file 
                   1154: # is chosen from the .conf files containing VirtualHosts definitions.
                   1155: #
                   1156: # If the user includes "Configure SSL for Apache web server" as
                   1157: # one of the actions to take to prepare the server for LON-CAPA
                   1158: # installation, then the output from &chkapachessl() will be
                   1159: # used to determined which file will contain VirtualHost configs.  
                   1160: #
                   1161: # If there are no files containing VirtualHosts definitions, then
                   1162: # <VirtualHost *:443> </VirtualHost> blocks will be appended to
                   1163: # the standard Apache SSL config for the particular distro:
                   1164: # ssl.conf for RHEL/CentOS/Scientific/Fedora, vhost-ssl.conf
                   1165: # for SuSE/SLES, and default-ssl.conf for Ubuntu.
                   1166: #
                   1167: # Once a file is selected, the contents of sslrewrite.conf and 
                   1168: # loncapassl.conf are compared with appropriate blocks in the file
                   1169: # and the user will be prompted to agree to insertion of missing
                   1170: # lines and/or deletion of surplus lines.
                   1171: #
                   1172: 
1.46      raeburn  1173: sub chkapachessl {
1.47      raeburn  1174:     my ($distro,$instdir,$hostname,$hostip) = @_;
1.46      raeburn  1175:     my $fixapachessl = 1;
1.47      raeburn  1176:     my $sslintconf = "$instdir/loncapassl.conf";
                   1177:     my $sslrewriteconf = "$instdir/sslrewrite.conf";
                   1178:     my (%sslfiles,%rewrites,%vhostonly,$has_std,$has_int,$rewritenum,$nochgint,$nochgstd);
                   1179:     $nochgstd = 0;
                   1180:     $nochgint = 0; 
                   1181:     if (!-e $sslintconf) {
                   1182:         $fixapachessl = 0;
                   1183:         print &mt('Warning: LON-CAPA SSL Apache configuration file [_1] needed for installation check.',$sslintconf)."\n";
                   1184:     } elsif (!-e $sslrewriteconf) {
1.46      raeburn  1185:         $fixapachessl = 0;
1.47      raeburn  1186:         print &mt('Warning: LON-CAPA SSL Apache configuration file [_1] needed for installation check is missing.',$sslrewriteconf)."\n";
1.46      raeburn  1187:     } else {
1.47      raeburn  1188:         my $ssldir;
1.46      raeburn  1189:         if ($distro =~ /^(debian|ubuntu)(\d+)$/) {
1.47      raeburn  1190:             $ssldir = '/etc/apache2/sites-available';
1.46      raeburn  1191:         } elsif ($distro =~ /(suse|sles)/) {
1.47      raeburn  1192:             $ssldir = '/etc/apache2/vhosts.d';
1.46      raeburn  1193:         } else {
1.47      raeburn  1194:             $ssldir = '/etc/httpd/conf.d';
                   1195:         }
                   1196:         my @rewritessl = ();
                   1197:         if (open(my $fh,'<',$sslrewriteconf)) {
                   1198:             my $skipnext = 0;
                   1199:             while (<$fh>) {
                   1200:                 chomp();
                   1201:                 s/(^\s+|\s+$)//g;
                   1202:                 next if ($_ eq '');
                   1203:                 next if ($_ eq '<IfModule mod_rewrite.c>');
                   1204:                 next if ($_ eq '</IfModule>');
                   1205:                 if ($_ eq 'RewriteCond %{REMOTE_ADDR} {[[[[HostIP]]]]}') {
                   1206:                     if (($hostip ne '') && ($hostip ne '127.0.0.1')) {
                   1207:                         push(@rewritessl,'RewriteCond %{REMOTE_ADDR} '.$hostip);
                   1208:                         next;
                   1209:                     } else {
                   1210:                         $skipnext = 1;
                   1211:                     }
                   1212:                 } elsif (($_ eq 'RewriteRule (.*) - [L]') && ($skipnext)) {
                   1213:                     $skipnext = 0;
                   1214:                     next;
                   1215:                 }
                   1216:                 push(@rewritessl,$_);
                   1217:             }
1.46      raeburn  1218:         }
1.47      raeburn  1219:         my @intssl = ();
                   1220:         if (open(my $fh,'<',$sslintconf)) {
                   1221:             while(<$fh>) {
                   1222:                 chomp();
                   1223:                 s/(^\s+|\s+$)//g;
                   1224:                 next if ($_ eq '');
                   1225:                 if ($_ eq 'ServerName internal-{[[[[Hostname]]]]}') {
                   1226:                     if ($hostname ne '') {
                   1227:                         push(@intssl,'ServerName internal-'.$hostname);
                   1228:                         next;
                   1229:                     }
                   1230:                 }
                   1231:                 next if ($_ eq '<VirtualHost *:443>');
                   1232:                 next if ($_ eq '</VirtualHost>');
                   1233:                 push(@intssl,$_);
                   1234:             }
                   1235:         }
                   1236:         if (-d $ssldir) {
                   1237:             my @actualint = ();
                   1238:             if (opendir(my $dir,$ssldir)) {
                   1239:                 my @sslconf_files;
                   1240:                 foreach my $file (grep(!/^\.+/,readdir($dir))) {
                   1241:                     next if (($distro =~ /(suse|sles)/) && ($file =~ /\.template$/));
                   1242:                     next if ($file =~ /\.rpmnew$/);
                   1243:                     if (open(my $fh,'<',"$ssldir/$file")) {
                   1244:                         while (<$fh>) {
                   1245:                             if (/^\s*<VirtualHost\s+[^:]*\:443>\s*$/) {
                   1246:                                 push(@sslconf_files,$file);
                   1247:                                 last;
                   1248:                             }
                   1249:                         }
                   1250:                         close($fh);
                   1251:                     }
                   1252:                 }
                   1253:                 closedir($dir);
                   1254:                 if (@sslconf_files) {
                   1255:                     foreach my $file (@sslconf_files) {
                   1256:                         if (open(my $fh,'<',"$ssldir/$file")) {
                   1257:                             my ($virtualhost,$rewrite,$num) = (0,0,0);
                   1258:                             my ($currname,$has_rewrite);
                   1259:                             while (<$fh>) {
                   1260:                                 chomp();
                   1261:                                 next if (/^\s*$/);
                   1262:                                 if ($virtualhost) {
                   1263:                                     if (/^\s*<\/VirtualHost>/) {
                   1264:                                         if ($currname !~ /^\Qinternal-$hostname\E/) {
                   1265:                                             if ($has_rewrite) {
                   1266:                                                 delete($vhostonly{$file});
                   1267:                                             } else {
                   1268:                                                 $vhostonly{$file} = 1;
                   1269:                                             }
                   1270:                                         }
                   1271:                                         $sslfiles{$currname}{$file} = 1;
                   1272:                                         $virtualhost = 0;
                   1273:                                         $currname = '';
                   1274:                                         $has_rewrite = '';
                   1275:                                         next;
                   1276:                                     } elsif (/^\s*ServerName\s+([^\s]+)\s*$/) {
                   1277:                                         $currname = $1;
                   1278:                                     }
                   1279:                                     if ($currname =~ /^\Qinternal-$hostname\E/) {
                   1280:                                         s/(^\s+|\s+$)//g;
                   1281:                                         push(@actualint,$_);
                   1282:                                         $has_int = $file;
                   1283:                                     } else {
                   1284:                                         if ($rewrite) {
                   1285:                                             if (/^\s*<\/IfModule>/) {
                   1286:                                                 $rewrite = 0;
                   1287:                                                 $num ++;
                   1288:                                             } else {
                   1289:                                                 s/(^\s+|\s+$)//g;
                   1290:                                                 push(@{$rewrites{$file}[$num]},$_);
                   1291:                                             }
                   1292:                                         } elsif (/^\s*<IfModule\s+mod_rewrite\.c>/) {
                   1293:                                             $rewrite = 1;
                   1294:                                             $has_rewrite = 1;
                   1295:                                             if ($currname eq '') {
                   1296:                                                 $currname = $hostname;
                   1297:                                             }
                   1298:                                             $rewrites{$file}[$num] = [];
                   1299:                                         }
                   1300:                                     }
                   1301:                                 } elsif (/^\s*<VirtualHost\s+[^:]*\:443>\s*$/) {
                   1302:                                     $virtualhost = 1;
                   1303:                                 }
                   1304:                             }
                   1305:                             close($fh);
                   1306:                         }
                   1307:                     }
                   1308:                 }
                   1309:                 if (keys(%rewrites)) {
                   1310:                     my $mindiffsall;
                   1311:                     foreach my $file (sort(keys(%rewrites))) {
                   1312:                         if (ref($rewrites{$file}) eq 'ARRAY') {
                   1313:                             my $mindiffs;
                   1314:                             for (my $i=0; $i<@{$rewrites{$file}}; $i++) {
                   1315:                                 if (ref($rewrites{$file}[$i]) eq 'ARRAY') {
                   1316:                                     my @diffs = &compare_arrays($rewrites{$file}[$i],\@rewritessl);
                   1317:                                     if (@diffs == 0) {
                   1318:                                         $fixapachessl = 0;
                   1319:                                         $mindiffs = 0;
                   1320:                                         $rewritenum = 1+$i;
                   1321:                                         last;
                   1322:                                     } else {
                   1323:                                         if ($mindiffs eq '') {
                   1324:                                             $mindiffs = scalar(@diffs);
                   1325:                                             $rewritenum = 1+$i; 
                   1326:                                         } elsif (scalar(@diffs) <= $mindiffs) {
                   1327:                                             $mindiffs = scalar(@diffs);
                   1328:                                             $rewritenum = 1+$i;
                   1329:                                         }
                   1330:                                     }
                   1331:                                 }
                   1332:                             }
                   1333:                             if ($mindiffsall eq '') {
                   1334:                                 $mindiffsall = $mindiffs;
                   1335:                                 $has_std = $file;
                   1336:                             } elsif ($mindiffs <= $mindiffsall) {
                   1337:                                 $mindiffsall = $mindiffs;
                   1338:                                 $has_std = $file;
                   1339:                             }
                   1340:                             if ($mindiffsall == 0) {
                   1341:                                 $nochgstd = 1;
                   1342:                             }
                   1343:                         }
                   1344:                     }
                   1345:                 } elsif (keys(%vhostonly) > 0) {
                   1346:                     if (($has_int ne '') && (exists($vhostonly{$has_int}))) {
                   1347:                         $has_std = $has_int;
                   1348:                     }
                   1349:                 }
                   1350:                 if (@actualint) {
                   1351:                     my @diffs = &compare_arrays(\@actualint,\@intssl);
                   1352:                     if (@diffs) {
                   1353:                         $fixapachessl = 1;
                   1354:                     } else {
                   1355:                         $nochgint = 1;
                   1356:                     }
                   1357:                 } else {
                   1358:                     $fixapachessl = 1;
1.46      raeburn  1359:                 }
                   1360:             }
                   1361:         }
                   1362:         unless ($fixapachessl) {
                   1363:             if ($distro =~ /^(debian|ubuntu)(\d+)$/) {
1.47      raeburn  1364:                 my $enabled_dir = '/etc/apache2/sites-enabled';
                   1365:                 if (keys(%sslfiles)) {
                   1366:                     foreach my $key (sort(keys(%sslfiles))) {
                   1367:                         if (ref($sslfiles{$key}) eq 'HASH') {
                   1368:                             foreach my $file (sort(keys(%{$sslfiles{$key}}))) {
                   1369:                                 unless ((-l "$enabled_dir/$file") &&
                   1370:                                         (readlink("$enabled_dir/$file") eq "$ssldir/$file")) {
                   1371:                                     print_and_log(&mt("Warning, use: 'sudo a2ensite $file' to activate LON-CAPA SSL Apache config\n"));
                   1372:                                 }
                   1373:                             }
                   1374:                         }
                   1375:                     }
1.46      raeburn  1376:                 }
                   1377:             }
                   1378:         }
                   1379:     }
1.47      raeburn  1380:     return ($fixapachessl,\%sslfiles,$has_std,$has_int,$rewritenum,$nochgstd,$nochgint);
                   1381: }
                   1382: 
                   1383: #
                   1384: # compare_arrays() expects two refs to arrays as args.
                   1385: #
                   1386: # The contents of the two arrays are compared, and if they
                   1387: # are different, and array of the differences is returned.
                   1388: #
                   1389: 
                   1390: sub compare_arrays {
                   1391:     my ($arrayref1,$arrayref2) = @_;
                   1392:     my (@difference,%count);
                   1393:     @difference = ();
                   1394:     %count = ();
                   1395:     if ((ref($arrayref1) eq 'ARRAY') && (ref($arrayref2) eq 'ARRAY')) {
                   1396:         foreach my $element (@{$arrayref1}, @{$arrayref2}) { $count{$element}++; }
                   1397:         foreach my $element (keys(%count)) {
                   1398:             if ($count{$element} == 1) {
                   1399:                 push(@difference,$element);
                   1400:             }
                   1401:         }
                   1402:     }
                   1403:     return @difference;
1.46      raeburn  1404: }
                   1405: 
1.1       raeburn  1406: sub chksrvcs {
                   1407:     my ($distro,$tostop) = @_;
                   1408:     my %stopsrvcs;
                   1409:     if (ref($tostop) eq 'HASH') {
                   1410:         %stopsrvcs = %{$tostop};
                   1411:     }
1.6       raeburn  1412:     foreach my $service ('cups','memcached') {
1.1       raeburn  1413:         next if (exists($stopsrvcs{$service}));
                   1414:         my $daemon = $service;
                   1415:         if ($service eq 'cups') {
                   1416:             $daemon = 'cupsd';
                   1417:         }
                   1418:         my $cmd = "ps -ef |grep '$daemon' |grep -v grep";
                   1419:         if (open(PIPE,'-|',$cmd)) {
                   1420:             my $daemonrunning = <PIPE>;
                   1421:             chomp($daemonrunning);
                   1422:             close(PIPE);
                   1423:             if ($daemonrunning) {
1.12      raeburn  1424:                 if ($service eq 'memcached') {
1.16      raeburn  1425:                     my $cmd = '/usr/bin/memcached';
                   1426:                     if ($distro =~ /^(suse|sles)/) {
                   1427:                         $cmd = '/usr/sbin/memcached';
                   1428:                     }
1.12      raeburn  1429:                     unless ($daemonrunning =~ m{^www[^/]+\Q$cmd -m 400 -v\E$}) {
1.10      raeburn  1430:                         $stopsrvcs{$service} = 1;
                   1431:                     }
                   1432:                 } else {
                   1433:                     $stopsrvcs{$service} = 1;
                   1434:                 }
1.1       raeburn  1435:             }
                   1436:         }
1.10      raeburn  1437:     }
1.1       raeburn  1438:     return \%stopsrvcs;
                   1439: }
                   1440: 
                   1441: sub need_download {
                   1442:     my $needs_download = 1;
                   1443:     my ($production,$testing,$stdsizes) = &download_versionslist();
                   1444:     my ($rootdir,$localcurrent,$localtesting,%tarball,%localsize,%bymodtime,
                   1445:         %bysize,$filetouse,$downloadstatus);
                   1446:     $rootdir = '/root';
                   1447:     if (opendir(my $dir,"$rootdir")) {
                   1448:         my (@lcdownloads,$version);
                   1449:         foreach my $file (readdir($dir)) {
                   1450:             if ($file =~ /^loncapa\-([\w\-.]+)\.tar\.gz$/) {
                   1451:                 $version = $1;
                   1452:             } else {
                   1453:                 next;
                   1454:             }
                   1455:             if (ref($stdsizes) eq 'HASH') {
                   1456:                 if ($version eq 'current') {
                   1457:                     my @stats = stat("$rootdir/$file");
                   1458:                     $localcurrent = $stats[7];
1.4       raeburn  1459:                     if ($localcurrent == $stdsizes->{$production}) {
1.1       raeburn  1460:                         $needs_download = 0;
                   1461:                         $filetouse = $file;
                   1462:                     }
                   1463:                 } elsif ($version eq 'testing') {
                   1464:                     my @stats = stat("$rootdir/$file");
                   1465:                     $localtesting = $stats[7];
1.4       raeburn  1466:                     if ($localtesting == $stdsizes->{$testing}) {
1.1       raeburn  1467:                         $needs_download = 0;
                   1468:                         $filetouse = $file;
                   1469:                     }
                   1470:                 }
                   1471:             }
                   1472:             $tarball{$version} = $file;
                   1473:             push(@lcdownloads,$version);
                   1474:         }
                   1475:         if ($needs_download) {
                   1476:             if (@lcdownloads > 0) {
                   1477:                 foreach my $version (@lcdownloads) {
                   1478:                     my @stats = stat("$rootdir/$tarball{$version}");
                   1479:                     my $mtime = $stats[9];
                   1480:                     $localsize{$version} = $stats[7];
                   1481:                     if ($mtime) {
                   1482:                         push(@{$bymodtime{$mtime}},$version);
                   1483:                     }
                   1484:                     if ($localsize{$version}) {
                   1485:                         push(@{$bysize{$localsize{$version}}},$version);
                   1486:                     }
                   1487:                 }
                   1488:                 if ($testing) {
                   1489:                     if (exists($localsize{$testing})) {
                   1490:                         if ($stdsizes->{$testing} == $localsize{$testing}) {
                   1491:                             $needs_download = 0;
                   1492:                             $filetouse = 'loncapa-'.$testing.'.tar.gz';
                   1493:                         }
                   1494:                     }
                   1495:                 }
                   1496:                 if ($needs_download) { 
                   1497:                     if ($production) {
                   1498:                         if (exists($localsize{$production})) {
                   1499:                             if ($stdsizes->{$production} == $localsize{$production}) {
                   1500:                                 $needs_download = 0;
                   1501:                                 $filetouse = 'loncapa-'.$production.'.tar.gz';
                   1502:                             }
                   1503:                         }
                   1504:                     }
                   1505:                 }
                   1506:                 if ($needs_download) {
                   1507:                     my @sorted = sort { $b <=> $a } keys(%bymodtime);
                   1508:                     my $newest = $sorted[0];
                   1509:                     if (ref($bymodtime{$newest}) eq 'ARRAY') {
                   1510:                         $downloadstatus = 
                   1511:                               "Latest LON-CAPA source download in $rootdir is: ".
                   1512:                               join(',',@{$bymodtime{$newest}})." (downloaded ".
                   1513:                               localtime($newest).")\n";
                   1514:                     }
                   1515:                 } else {
                   1516:                     $downloadstatus = 
                   1517:                         "The $rootdir directory already contains the latest LON-CAPA version:".
                   1518:                         "\n".$filetouse."\n"."which can be used for installation.\n";
                   1519:                 }
                   1520:             } else {
                   1521:                 $downloadstatus = "The $rootdir directory does not appear to contain any downloaded LON-CAPA source code files which can be used for installation.\n";
                   1522:             }
                   1523:         }
                   1524:     } else {
                   1525:         $downloadstatus = "Could not open $rootdir directory to look for existing downloads of LON-CAPA source code.\n";
                   1526:     }
                   1527:     return ($needs_download,$downloadstatus,$filetouse,$production,$testing);
                   1528: }
                   1529: 
                   1530: sub check_mysql_setup {
1.34      raeburn  1531:     my ($instdir,$dsn,$distro,$mysql_has_wwwuser) = @_;
1.1       raeburn  1532:     my ($mysqlsetup,$has_pass);
                   1533:     my $dbh = DBI->connect($dsn,'root','',{'PrintError'=>0});
                   1534:     if ($dbh) {
                   1535:         $mysqlsetup = 'noroot'; 
                   1536:     } elsif ($DBI::err =~ /1045/) {
                   1537:         $has_pass = 1;
1.34      raeburn  1538:     } elsif ($distro =~ /^ubuntu(\d+)$/) {
                   1539:         my $version = $1;
                   1540:         if ($1 > 12) {
                   1541:             print_and_log(&mt('Restarting mysql, please be patient')."\n");
                   1542:             if (open (PIPE, "service mysql restart 2>&1 |")) {
                   1543:                 while (<PIPE>) {
                   1544:                     print $_;
                   1545:                 }
                   1546:                 close(PIPE);
                   1547:             }
                   1548:             unless ($mysql_has_wwwuser) {
                   1549:                 $mysql_has_wwwuser = &check_mysql_wwwuser();
                   1550:             }
                   1551:             $dbh = DBI->connect($dsn,'root','',{'PrintError'=>0});
                   1552:             if ($dbh) {
                   1553:                 $mysqlsetup = 'noroot';
                   1554:             } elsif ($DBI::err =~ /1045/) {
                   1555:                 $has_pass = 1;
                   1556:             } else {
                   1557:                 $mysqlsetup = 'needsrestart';
                   1558:                 return ($mysqlsetup,$has_pass,$dbh,$mysql_has_wwwuser);
                   1559:             }
                   1560:         }
1.1       raeburn  1561:     }
                   1562:     if ($has_pass) {
                   1563:         print &mt('You have already set a root password for the MySQL database.')."\n";
                   1564:         my $currpass = &get_mysql_password(&mt('Please enter the password now'));
                   1565:         $dbh = DBI->connect($dsn,'root',$currpass,{'PrintError'=>0});
                   1566:         if ($dbh) {
                   1567:             $mysqlsetup = 'rootok';
                   1568:             print_and_log(&mt('Password accepted.')."\n");
                   1569:         } else {
                   1570:             $mysqlsetup = 'rootfail';
                   1571:             print_and_log(&mt('Problem accessing MySQL.')."\n");
                   1572:             if ($DBI::err =~ /1045/) {
                   1573:                 print_and_log(&mt('Perhaps the password was incorrect?')."\n");
                   1574:                 print &mt('Try again?').' ';
                   1575:                 $currpass = &get_mysql_password(&mt('Re-enter password now')); 
                   1576:                 $dbh = DBI->connect($dsn,'root',$currpass,{'PrintError'=>0});
                   1577:                 if ($dbh) {
                   1578:                     $mysqlsetup = 'rootok';
                   1579:                     print_and_log(&mt('Password accepted.')."\n");
                   1580:                 } else {
                   1581:                     if ($DBI::err =~ /1045/) {
                   1582:                         print_and_log(&mt('Incorrect password.')."\n");
                   1583:                     }
                   1584:                 }
                   1585:             }
                   1586:         }
1.34      raeburn  1587:     } elsif ($mysqlsetup ne 'noroot') {
1.1       raeburn  1588:         print_and_log(&mt('Problem accessing MySQL.')."\n");
                   1589:         $mysqlsetup = 'rootfail';
                   1590:     }
1.34      raeburn  1591:     return ($mysqlsetup,$has_pass,$dbh,$mysql_has_wwwuser);
1.1       raeburn  1592: }
                   1593: 
                   1594: sub check_mysql_wwwuser {
                   1595:     my $mysql_wwwuser;
1.12      raeburn  1596:     my $dbhn = DBI->connect("DBI:mysql:database=information_schema",'www','localhostkey',
                   1597:                             {PrintError => +0}) || return;
1.1       raeburn  1598:     if ($dbhn) {
                   1599:         $mysql_wwwuser = 1;
                   1600:         $dbhn->disconnect;
                   1601:     }
                   1602:     return $mysql_wwwuser;
                   1603: }
                   1604: 
                   1605: sub check_loncapa_mysqldb {
                   1606:     my ($dbh) = @_;
                   1607:     my $has_lcdb;
                   1608:     if (ref($dbh)) {
                   1609:         my $sth = $dbh->prepare("SHOW DATABASES");
                   1610:         $sth->execute();
                   1611:         while (my $dbname = $sth->fetchrow_array) {
                   1612:             if ($dbname eq 'loncapa') {
                   1613:                 $has_lcdb = 1;
                   1614:                 last;
                   1615:             }
                   1616:         }
                   1617:         $sth->finish();
                   1618:     }
                   1619:     return $has_lcdb;
                   1620: }
                   1621: 
                   1622: sub get_pathto_iptables {
                   1623:     my $iptables;
                   1624:     if (-e '/sbin/iptables') {
                   1625:         $iptables = '/sbin/iptables';
                   1626:     } elsif (-e '/usr/sbin/iptables') {
                   1627:         $iptables = '/usr/sbin/iptables';
                   1628:     } else {
                   1629:         print &mt('Unable to find iptables command.')."\n";
                   1630:     }
                   1631:     return $iptables;
                   1632: }
                   1633: 
                   1634: sub firewall_is_active {
                   1635:     if (-e '/proc/net/ip_tables_names') {
1.49      raeburn  1636:         if (open(PIPE,'cat /proc/net/ip_tables_names |grep filter |')) {
                   1637:             my $status = <PIPE>;
                   1638:             close(PIPE);
                   1639:             chomp($status);
                   1640:             if ($status eq 'filter') {
                   1641:                 return 1;
                   1642:             }
                   1643:         }
1.1       raeburn  1644:     }
1.49      raeburn  1645:     return 0;
1.1       raeburn  1646: }
                   1647: 
                   1648: sub get_fw_chains {
1.5       raeburn  1649:     my ($iptables,$distro) = @_;
1.1       raeburn  1650:     my @fw_chains;
                   1651:     my $suse_config = "/etc/sysconfig/SuSEfirewall2";
                   1652:     my $ubuntu_config = "/etc/ufw/ufw.conf";
                   1653:     if (-e $suse_config) {
                   1654:         push(@fw_chains,'input_ext');
                   1655:     } else {
                   1656:         my @posschains;
                   1657:         if (-e $ubuntu_config) {
                   1658:             @posschains = ('ufw-user-input','INPUT');
1.5       raeburn  1659:         } elsif ($distro =~ /^debian5/) {
                   1660:             @posschains = ('INPUT');
1.49      raeburn  1661:         } elsif ($distro =~ /^(suse|sles)(\d+)/) {
                   1662:             @posschains = ('IN_public');
1.1       raeburn  1663:         } else {
                   1664:             @posschains = ('RH-Firewall-1-INPUT','INPUT');
                   1665:             if (!-e '/etc/sysconfig/iptables') {
                   1666:                 if (!-e '/var/lib/iptables') {
                   1667:                     print &mt('Unable to find iptables file containing static definitions.')."\n";
                   1668:                 }
                   1669:                 push(@fw_chains,'RH-Firewall-1-INPUT');
                   1670:             }
                   1671:         }
                   1672:         if ($iptables eq '') {
                   1673:             $iptables = &get_pathto_iptables();
                   1674:         }
                   1675:         my %counts;
                   1676:         if (open(PIPE,"$iptables -L -n |")) {
                   1677:             while(<PIPE>) {
                   1678:                 foreach my $chain (@posschains) {
                   1679:                     if (/(\Q$chain\E)/) {
                   1680:                         $counts{$1} ++;
                   1681:                     }
                   1682:                 }
                   1683:             }
                   1684:             close(PIPE);
                   1685:         }
                   1686:         foreach my $fw_chain (@posschains) {
                   1687:             if ($counts{$fw_chain}) {
                   1688:                 unless(grep(/^\Q$fw_chain\E$/,@fw_chains)) {
                   1689:                     push(@fw_chains,$fw_chain);
                   1690:                 }
                   1691:             }
                   1692:         }
                   1693:     }
                   1694:     return @fw_chains;
                   1695: }
                   1696: 
                   1697: sub firewall_is_port_open {
                   1698:     my ($iptables,$fw_chain,$port) = @_;
                   1699:     # returns 1 if the firewall port is open, 0 if not.
                   1700:     #
                   1701:     # check if firewall is active or installed
                   1702:     return if (! &firewall_is_active());
                   1703:     my $count = 0;
                   1704:     if (open(PIPE,"$iptables -L $fw_chain -n |")) {
                   1705:         while(<PIPE>) {
                   1706:             if (/tcp dpt\:\Q$port\E/) {
                   1707:                 $count ++;
                   1708:                 last;
                   1709:             }
                   1710:         }
                   1711:         close(PIPE);
                   1712:     } else {
                   1713:         print &mt('Firewall status not checked: unable to run [_1].','iptables -L')."\n";
                   1714:     }
                   1715:     return $count;
                   1716: }
                   1717: 
                   1718: sub get_mysql_password {
                   1719:     my ($prompt) = @_;
                   1720:     local $| = 1;
                   1721:     print $prompt.': ';
                   1722:     my $newpasswd = '';
                   1723:     ReadMode 'raw';
                   1724:     my $key;
                   1725:     while(ord($key = ReadKey(0)) != 10) {
                   1726:         if(ord($key) == 127 || ord($key) == 8) {
                   1727:             chop($newpasswd);
                   1728:             print "\b \b";
                   1729:         } elsif(!ord($key) < 32) {
                   1730:             $newpasswd .= $key;
                   1731:             print '*';
                   1732:         }
                   1733:     }
                   1734:     ReadMode 'normal';
                   1735:     print "\n";
                   1736:     return $newpasswd;
                   1737: }
                   1738: 
                   1739: sub check_SuSEfirewall2_setup {
                   1740:     my ($instdir) = @_;
                   1741:     my $need_override = 1;
1.9       raeburn  1742:     if ((-e "/etc/insserv/overrides/SuSEfirewall2_setup") && (-e "$instdir/sles-suse/SuSEfirewall2_setup")) {
                   1743:         if (open(PIPE, "diff --brief $instdir/sles-suse/SuSEfirewall2_setup /etc/insserv/overrides/SuSEfirewall2_setup  |")) {
1.1       raeburn  1744:             my $diffres = <PIPE>;
                   1745:             close(PIPE);
                   1746:             chomp($diffres);
                   1747:             unless ($diffres) {
                   1748:                 $need_override = 0;
                   1749:             }
                   1750:         }
                   1751:     }
                   1752:     return $need_override;
                   1753: }
                   1754: 
                   1755: sub download_versionslist {
                   1756:     my ($production,$testing,%sizes);
                   1757:     if (-e "latest.txt") {
                   1758:          unlink("latest.txt");
                   1759:     }
                   1760:     my $rtncode = system("wget http://install.loncapa.org/versions/latest.txt ".
                   1761:                          "> /dev/null 2>&1");
                   1762:     if (!$rtncode) {
                   1763:         if (open(my $fh,"<latest.txt")) {
                   1764:             my @info = <$fh>;
                   1765:             close($fh);
                   1766:             foreach my $line (@info) {
                   1767:                 chomp();
                   1768:                 if ($line =~ /^\QLATEST-IS: \E([\w\-.]+):(\d+)$/) {
                   1769:                      $production = $1;
                   1770:                      $sizes{$1} = $2;
                   1771:                 } elsif ($line =~ /^LATEST-TESTING-IS: \E([\w\-.]+):(\d+)$/) {
                   1772:                      $testing = $1;
                   1773:                      $sizes{$1} = $2;
                   1774:                 }
                   1775:             }
                   1776:         }
                   1777:     }
                   1778:     return ($production,$testing,\%sizes);
                   1779: }
                   1780: 
                   1781: #
                   1782: # End helper routines.
                   1783: # Main script starts here
                   1784: #
                   1785: 
                   1786: print "
                   1787: ********************************************************************
                   1788: 
                   1789:                     ".&mt('Welcome to LON-CAPA')."
                   1790: 
                   1791: ".&mt('This script will configure your system for installation of LON-CAPA.')."
                   1792: 
                   1793: ********************************************************************
                   1794: 
                   1795: ".&mt('The following actions are available:')."
                   1796: 
1.4       raeburn  1797: ".&mt('1.')." ".&mt('Create the www user/group.')."
1.1       raeburn  1798:    ".&mt('This is the user/group ownership under which Apache child processes run.')."
                   1799:    ".&mt('It also owns most directories within the /home/httpd directory.')." 
                   1800:    ".&mt('This directory is where most LON-CAPA files and directories are stored.')."
1.4       raeburn  1801: ".&mt('2.')." ".&mt('Install the package LON-CAPA uses to authenticate users.')."
                   1802: ".&mt('3.')." ".&mt('Set-up the MySQL database.')."
                   1803: ".&mt('4.')." ".&mt('Set-up MySQL permissions.')."
                   1804: ".&mt('5.')." ".&mt('Configure Apache web server.')."
1.46      raeburn  1805: ".&mt('6.')." ".&mt('Configure SSL for Apache web server.')."
                   1806: ".&mt('7.')." ".&mt('Configure start-up of services.')."
                   1807: ".&mt('8.')." ".&mt('Check firewall settings.')."
                   1808: ".&mt('9.')." ".&mt('Stop services not used by LON-CAPA,')."
1.1       raeburn  1809:    ".&mt('i.e., services for a print server: [_1] daemon.',"'cups'")."
1.46      raeburn  1810: ".&mt('10.')." ".&mt('Download LON-CAPA source code in readiness for installation.')."
1.1       raeburn  1811: 
                   1812: ".&mt('Typically, you will run this script only once, when you first install LON-CAPA.')." 
                   1813: 
                   1814: ".&mt('The script will analyze your system to determine which actions are recommended.')."
                   1815: ".&mt('The script will then prompt you to choose the actions you would like taken.')."
                   1816: 
                   1817: ".&mt('For each the recommended action will be selected if you hit Enter/Return.')."
                   1818: ".&mt('To override the default, type the lower case option from the two options listed.')."
                   1819: ".&mt('So, if the default is "yes", ~[Y/n~] will be shown -- type n to override.')."
                   1820: ".&mt('Whereas if the default is "no", ~[y/N~] will be shown -- type y to override.')." 
                   1821: 
                   1822: ".&mt('To accept the default, simply hit Enter/Return on your keyboard.')."
                   1823: ".&mt('Otherwise type: y or n then hit the Enter/Return key.')."
                   1824: 
                   1825: ".&mt('Once a choice has been entered for all nine actions, required changes will be made.')."
                   1826: ".&mt('Feedback will be displayed on screen, and also stored in: [_1].','loncapa_install.log')."
                   1827: 
                   1828: ".&mt('Continue? ~[Y/n~] ');
                   1829: 
                   1830: my $go_on = &get_user_selection(1);
                   1831: if (!$go_on) {
                   1832:     exit;
                   1833: }
                   1834: 
                   1835: my $instdir = `pwd`;
                   1836: chomp($instdir);
                   1837: 
                   1838: my %callsub;
                   1839: my @actions = ('wwwuser','pwauth','mysql','mysqlperms','apache',
1.46      raeburn  1840:                'apachessl','runlevels','firewall','stopsrvcs','download');
1.1       raeburn  1841: my %prompts = &texthash( 
                   1842:     wwwuser    => "Create the 'www' user?",
                   1843:     pwauth     => 'Install the package LON-CAPA uses to authenticate users?',
                   1844:     mysql      => 'Set-up the MySQL database?',
                   1845:     mysqlperms => 'Set-up MySQL permissions?',
                   1846:     apache     => 'Configure Apache web server?',
1.46      raeburn  1847:     apachessl  => 'Configure SSL for Apache web server?', 
1.1       raeburn  1848:     runlevels  => 'Set overrides for start-up order of services?',
                   1849:     firewall   => 'Configure firewall settings for Apache',
                   1850:     stopsrvcs  => 'Stop extra services not required on a LON-CAPA server?',
                   1851:     download   => 'Download LON-CAPA source code in readiness for installation?',
                   1852: );
                   1853: 
1.46      raeburn  1854: print "\n".&mt('Checking system status ...')."\n\n";
1.1       raeburn  1855: 
                   1856: my $dsn = "DBI:mysql:database=mysql";
1.34      raeburn  1857: my ($distro,$gotprereqs,$localecmd,$packagecmd,$updatecmd,$installnow,$mysqlrestart,
                   1858:     $recommended,$dbh,$has_pass,$has_lcdb,$downloadstatus,$filetouse,$production,
1.47      raeburn  1859:     $testing,$apachefw,$uses_systemctl,$hostname,$hostip,$sslhostsfiles,$has_std,
                   1860:     $has_int,$rewritenum,$nochgstd,$nochgint) = &check_required($instdir,$dsn);
1.1       raeburn  1861: if ($distro eq '') {
                   1862:     print "\n".&mt('Linux distribution could not be verified as a supported distribution.')."\n".
                   1863:           &mt('The following are supported: [_1].',
                   1864:               'CentOS, RedHat Enterprise, Fedora, Scientific Linux, '.
1.56      raeburn  1865:               'Oracle Linux, openSuSE, SLES, Ubuntu LTS, Debian')."\n\n".
1.1       raeburn  1866:           &mt('Stopping execution.')."\n";
                   1867:     exit;
                   1868: }
1.34      raeburn  1869: if ($mysqlrestart) {
                   1870:     print "\n".&mt('The mysql daemon needs to be restarted using the following command:')."\n".
                   1871:           $mysqlrestart."\n\n".
                   1872:           &mt('Stopping execution of install.pl script.')."\n".
                   1873:           &mt('Please run the install.pl script again, once you have restarted mysql.')."\n";
                   1874:     exit;
                   1875: }
1.6       raeburn  1876: if ($localecmd ne '') {
                   1877:     print "\n".&mt('Although the LON-CAPA application itself is localized for a number of different languages, the default locale language for the Linux OS on which it runs should be US English.')."\n";
                   1878:     print "\n".&mt('Run the following command from the command line to set the default language for your OS, and then run this LON-CAPA installation set-up script again.')."\n\n".
                   1879:     $localecmd."\n\n".
                   1880:     &mt('Stopping execution.')."\n";
                   1881:     exit;
                   1882: }
1.1       raeburn  1883: if (!$gotprereqs) {
1.12      raeburn  1884:     print "\n".&mt('The LONCAPA-prerequisites package is not installed.')."\n".
1.1       raeburn  1885:           &mt('The following command can be used to install the package (and dependencies):')."\n\n".
                   1886:           $updatecmd."\n\n";
                   1887:     if ($installnow eq '') {
                   1888:         exit;
                   1889:     } else {
                   1890:         print &mt('Run command? ~[Y/n~]');
                   1891:         my $install_prereq = &get_user_selection(1);
                   1892:         if ($install_prereq) {
                   1893:             if (open(PIPE,'|-',$installnow)) {
                   1894:                 close(PIPE);
                   1895:                 $gotprereqs = &check_prerequisites($packagecmd,$distro);
                   1896:                 if (!$gotprereqs) {
1.12      raeburn  1897:                     print &mt('The LONCAPA-prerequisites package is not installed.')."\n".
1.1       raeburn  1898:                           &mt('Stopping execution.')."\n";
                   1899:                     exit;
                   1900:                 } else {
1.6       raeburn  1901:                     ($distro,$gotprereqs,$localecmd,$packagecmd,$updatecmd,$installnow,
1.34      raeburn  1902:                      $mysqlrestart,$recommended,$dbh,$has_pass,$has_lcdb,$downloadstatus,
1.35      raeburn  1903:                      $filetouse,$production,$testing,$apachefw,$uses_systemctl) = 
1.5       raeburn  1904:                      &check_required($instdir,$dsn);
1.1       raeburn  1905:                 }
                   1906:             } else {
1.12      raeburn  1907:                 print &mt('Failed to run command to install LONCAPA-prerequisites')."\n";
1.1       raeburn  1908:                 exit;
                   1909:             }
                   1910:         } else {
                   1911:             print &mt('Stopping execution.')."\n";
                   1912:             exit;
                   1913:         }
                   1914:     }
                   1915: }
                   1916: unless (ref($recommended) eq 'HASH') {
                   1917:     print "\n".&mt('An error occurred determining which actions are recommended.')."\n\n".
                   1918:           &mt('Stopping execution.')."\n";
                   1919:     exit;
                   1920: }
                   1921: 
                   1922: print "\n";
                   1923: my $num = 0;
                   1924: foreach my $action (@actions) {
                   1925:     $num ++;
                   1926:     my ($yesno,$defaultrun);
                   1927:     if (ref($recommended) eq 'HASH') {
1.4       raeburn  1928:         if (($action eq 'runlevels') || ($action eq 'stopsrvcs')) {
1.1       raeburn  1929:             $yesno = '[y/N]';
                   1930:             if (ref($recommended->{$action}) eq 'HASH') {
                   1931:                 if (keys(%{$recommended->{$action}}) > 0) {
                   1932:                     $yesno = &mt('~[Y/n~]');
                   1933:                     $defaultrun = 1;
                   1934:                 }
                   1935:             }
                   1936:         } else {
                   1937:             if ($action eq 'download') {
                   1938:                 if ($downloadstatus) {
                   1939:                     print "\n$downloadstatus\n";
                   1940:                 }
                   1941:             }
                   1942:             if ($recommended->{$action}) {
                   1943:                 $yesno = mt('~[Y/n~]');
                   1944:                 $defaultrun = 1;
                   1945:             } else {
                   1946:                 $yesno = &mt('~[y/N~]');
                   1947:             }
                   1948:         }
                   1949:         print $num.'. '.$prompts{$action}." $yesno ";
                   1950:         $callsub{$action} = &get_user_selection($defaultrun);
                   1951:     }
                   1952: }
                   1953: 
                   1954: my $lctarball = 'loncapa-current.tar.gz';
                   1955: my $sourcetarball = $lctarball;
                   1956: if ($callsub{'download'}) {
                   1957:     my ($production,$testing,$sizes) = &download_versionslist();
                   1958:     if ($production && $testing) {
                   1959:         if ($production ne $testing) {
                   1960:             print &mt('Two recent LON-CAPA releases are available: ')."\n".
1.3       raeburn  1961:                   &mt('1.').' '.&mt('A production release - version: [_1].',$production)."\n".
                   1962:                   &mt('2.').' '.&mt('A testing release - version: [_1].',$testing)."\n\n".
1.1       raeburn  1963:                   &mt('Download the production release? ~[Y/n~]');
                   1964:             if (&get_user_selection(1)) {
1.4       raeburn  1965:                 $sourcetarball = 'loncapa-'.$production.'.tar.gz';
1.1       raeburn  1966:             } else {
                   1967:                 print "\n".&mt('Download the testing release? ~[Y/n~]');
                   1968:                 if (&get_user_selection(1)) {
1.4       raeburn  1969:                     $sourcetarball = 'loncapa-'.$testing.'.tar.gz';
1.1       raeburn  1970:                 }
                   1971:             }
                   1972:         }
                   1973:     } elsif ($production) {
                   1974:         print &mt('The most recent LON-CAPA release is version: [_1].',$production)."\n".
                   1975:               &mt('Download the production release? ~[Y/n~]');
                   1976:         if (&get_user_selection(1)) {
1.20      raeburn  1977:             $sourcetarball = 'loncapa-'.$production.'.tar.gz';
1.1       raeburn  1978:         }
                   1979:     }
                   1980: } elsif ($filetouse ne '') {
                   1981:     $sourcetarball = $filetouse;
                   1982: }
                   1983: 
                   1984: print_and_log("\n");
                   1985: 
                   1986: # Each action: report if skipping, or perform action and provide feedback. 
                   1987: if ($callsub{'wwwuser'}) {
                   1988:     &setup_www();
                   1989: } else {
                   1990:     &print_and_log(&mt('Skipping creation of user [_1].',"'www'")."\n");
                   1991: }
                   1992: 
                   1993: if ($callsub{'pwauth'}) {
1.4       raeburn  1994:     &build_and_install_mod_auth_external($instdir);
1.1       raeburn  1995: } else {
                   1996:     &print_and_log(&mt('Skipping [_1] installation.',"'pwauth'")."\n");
                   1997: }
                   1998: 
                   1999: if ($callsub{'mysql'}) {
                   2000:     if ($dbh) {
                   2001:         &setup_mysql($callsub{'mysqlperms'},$distro,$dbh,$has_pass,$has_lcdb);
                   2002:     } else {
                   2003:         print &mt('Unable to configure MySQL because access is denied.')."\n";
                   2004:     }
                   2005: } else {
                   2006:     &print_and_log(&mt('Skipping configuration of MySQL.')."\n");
                   2007:     if ($callsub{'mysqlperms'}) {
                   2008:         if ($dbh) {
                   2009:             &setup_mysql_permissions($dbh,$has_pass);
                   2010:         } else {
                   2011:             print &mt('Unable to configure MySQL because access is denied.')."\n";  
                   2012:         }
                   2013:     } else {
                   2014:         &print_and_log(&mt('Skipping MySQL permissions setup.')."\n");
                   2015:     }
                   2016: }
                   2017: 
                   2018: if ($dbh) {
                   2019:     if (!$dbh->disconnect) {
                   2020:         &print_and_log(&mt('Failed to disconnect from MySQL:')."\n".
                   2021:                        $dbh->errstr);
                   2022:     }
                   2023: }
                   2024: 
                   2025: if ($callsub{'apache'}) {
                   2026:     if ($distro =~ /^(suse|sles)/) {
1.49      raeburn  2027:         &copy_apache2_suseconf($instdir,$hostname,$distro);
1.1       raeburn  2028:     } elsif ($distro =~ /^(debian|ubuntu)/) {
1.46      raeburn  2029:         &copy_apache2_debconf($instdir,$distro,$hostname);
1.1       raeburn  2030:     } else {
1.46      raeburn  2031:         &copy_httpd_conf($instdir,$distro,$hostname);
1.59      raeburn  2032:         &copy_mpm_conf($instdir,$distro);
1.1       raeburn  2033:     }
                   2034: } else {
                   2035:     print_and_log(&mt('Skipping configuration of Apache web server.')."\n");
                   2036: }
                   2037: 
1.46      raeburn  2038: if ($callsub{'apachessl'}) {
1.47      raeburn  2039:     my $targetdir = '/etc/httpd/conf.d';
1.46      raeburn  2040:     if ($distro =~ /^(suse|sles)/) {
1.47      raeburn  2041:         $targetdir = '/etc/apache2/vhosts.d';
1.46      raeburn  2042:     } elsif ($distro =~ /^(debian|ubuntu)/) {
1.47      raeburn  2043:         $targetdir = '/etc/apache2/sites-available';
                   2044:     }
                   2045:     my ($new_rewrite,$new_int) = 
                   2046:         &copy_apache_sslconf_files($distro,$hostname,$hostip,$instdir,$targetdir,$sslhostsfiles,
                   2047:                                    $has_std,$has_int,$rewritenum,$nochgstd,$nochgint); 
                   2048:     if ($distro =~ /^(debian|ubuntu)/) {
                   2049:         my $apache2_sites_enabled_dir = '/etc/apache2/sites-enabled';
                   2050:         if (-d $apache2_sites_enabled_dir) {  
                   2051:             if ($has_std ne '') {
                   2052:                 unless ((-l "$apache2_sites_enabled_dir/$has_std") && (readlink(("$apache2_sites_enabled_dir/$has_std") eq "$targetdir/$has_std"))) {
                   2053:                     my $made_symlink =  eval { symlink("$targetdir/$has_std","$apache2_sites_enabled_dir/$has_std"); 1};
                   2054:                     if ($made_symlink) {
                   2055:                         print_and_log(&mt('Enabling "[_1]" Apache SSL configuration.',$has_std)."\n");
                   2056:                     }
                   2057:                 }
                   2058:             }
                   2059:             if (($has_int ne '') && ($has_int ne $has_std)) {
                   2060:                 unless ((-l "$apache2_sites_enabled_dir/$has_int") && (readlink("$apache2_sites_enabled_dir/$has_int") eq "$targetdir/$has_int")) {
                   2061:                     my $made_symlink =  eval { symlink("$targetdir/$has_int","$apache2_sites_enabled_dir/$has_int"); 1 };
                   2062:                     if ($made_symlink) {
                   2063:                         print_and_log(&mt('Enabling "[_1]" Apache SSL configuration.',$has_int)."\n");
                   2064:                     }
                   2065:                 }
1.46      raeburn  2066:             }
                   2067:         }
                   2068:     }
                   2069:     print_and_log("\n");
                   2070: } else {
                   2071:     print_and_log(&mt('Skipping configuration of SSL for Apache web server.')."\n");
                   2072: }
                   2073: 
1.1       raeburn  2074: if ($callsub{'runlevels'}) {
                   2075:     my $count = 0;
                   2076:     if (ref($recommended) eq 'HASH') {
                   2077:         if (ref($recommended->{'runlevels'}) eq 'HASH') {
                   2078:             foreach my $type (keys(%{$recommended->{'runlevels'}})) {
                   2079:                 next if ($type eq 'insserv');
                   2080:                 $count ++;
                   2081:                 my $command = $recommended->{'runlevels'}{$type};
                   2082:                 if ($command ne '') {
                   2083:                     print_and_log(&mt('Runlevel update command run: [_1].',$command)."\n");
                   2084:                     system($command);
                   2085:                 }
                   2086:             }
                   2087:             if (!$count) {
                   2088:                 print_and_log(&mt('No runlevel updates required.')."\n");
                   2089:             }  
                   2090:         }
                   2091:     }
1.49      raeburn  2092:     if ($distro =~ /^(suse|sles)(\d+)/) {
                   2093:         unless(($1 eq 'sles') && ($2 >= 15)) {
                   2094:             &update_SuSEfirewall2_setup($instdir);
                   2095:         }
1.11      raeburn  2096:     }
1.1       raeburn  2097: } else {
                   2098:     &print_and_log(&mt('Skipping setting override for start-up order of services.')."\n");
                   2099: }
                   2100: 
                   2101: if ($callsub{'firewall'}) {
1.53      raeburn  2102:     my ($firewalld,$zone) = &uses_firewalld($distro);
                   2103:     if ($firewalld) {
1.49      raeburn  2104:         my (%current,%added);
1.53      raeburn  2105:         if (open(PIPE,"firewall-cmd --permanent --zone=$zone --list-services |")) {
1.49      raeburn  2106:             my $svc = <PIPE>;
                   2107:             close(PIPE);
                   2108:             chomp($svc);
                   2109:             map { $current{$_} = 1; } (split(/\s+/,$svc));
                   2110:         }
                   2111:         foreach my $service ('http','https') {
                   2112:             unless ($current{$service}) {
1.53      raeburn  2113:                 if (open(PIPE,"firewall-cmd --permanent --zone=$zone --add-service=$service |")) {
1.49      raeburn  2114:                     my $result = <PIPE>;
                   2115:                     if ($result =~ /^success/) {
                   2116:                         $added{$service} = 1;
                   2117:                     }
                   2118:                 }
                   2119:             }
                   2120:         }
                   2121:         if (keys(%added) > 0) {
                   2122:             print &mt('Firewall configured to allow access for: [_1].',
                   2123:                       join(', ',sort(keys(%added))))."\n";
                   2124:         }
                   2125:         if ($current{'http'} || $current{'https'}) {
                   2126:             print &mt('Firewall already configured to allow access for:[_1].',
                   2127:                       (($current{'http'})? ' http':'').(($current{'https'})? ' https':''))."\n";
                   2128:         }
                   2129:         unless ($current{'ssh'}) {
                   2130:             print &mt('If you would the like to allow access to ssh from outside, use the command[_1].',
1.53      raeburn  2131:                   "firewall-cmd --permanent --zone=$zone --add-service=ssh")."\n";
1.49      raeburn  2132:         }
                   2133:     } elsif ($distro =~ /^(suse|sles)/) {
1.5       raeburn  2134:         print &mt('Use [_1] to configure the firewall to allow access for [_2].',
                   2135:                   'yast -- Security and Users -> Firewall -> Interfaces',
1.49      raeburn  2136:                   'ssh, http, https')."\n";
1.5       raeburn  2137:     } elsif ($distro =~ /^(debian|ubuntu)(\d+)/) {
                   2138:         if (($1 eq 'ubuntu') || ($2 > 5)) {
                   2139:             print &mt('Use [_1] to configure the firewall to allow access for [_2].',
                   2140:                       'ufw','ssh, http, https')."\n";
                   2141:         } else {
                   2142:             my $fwadded = &get_iptables_rules($distro,$instdir,$apachefw);
                   2143:             if ($fwadded) {
                   2144:                 print &mt('Enable firewall? ~[Y/n~]');
                   2145:                 my $enable_iptables = &get_user_selection(1);
                   2146:                 if ($enable_iptables) {
                   2147:                     system('/etc/network/if-pre-up.d/iptables');
                   2148:                     print &mt('Firewall enabled using rules defined in [_1].',
                   2149:                               '/etc/iptables.loncapa.rules'); 
                   2150:                 }
                   2151:             }
                   2152:         }
1.57      raeburn  2153:     } elsif ($distro =~ /^(scientific|oracle)/) {
1.11      raeburn  2154:         print &mt('Use [_1] to configure the firewall to allow access for [_2].',
                   2155:                   'system-config-firewall-tui -- Customize',
                   2156:                   'ssh, http')."\n";
1.1       raeburn  2157:     } else {
1.53      raeburn  2158:         my $version;
                   2159:         if ($distro =~ /^(redhat|centos)(\d+)$/) {
                   2160:             $version = $1;
                   2161:         }
                   2162:         if ($version > 5) {
                   2163:             print &mt('Use [_1] to configure the firewall to allow access for [_2].',
                   2164:                   'system-config-firewall-tui -- Customize',
                   2165:                   'ssh, http')."\n";
                   2166:         } else {
                   2167:             print &mt('Use [_1] to configure the firewall to allow access for [_2].',
                   2168:                       'setup -- Firewall configuration -> Customize',
                   2169:                       'ssh, http, https')."\n";
                   2170:         }
1.1       raeburn  2171:     }
                   2172: } else {
1.5       raeburn  2173:     &print_and_log(&mt('Skipping Firewall configuration.')."\n");
1.1       raeburn  2174: }
                   2175: 
                   2176: if ($callsub{'stopsrvcs'}) {
1.45      raeburn  2177:     &kill_extra_services($distro,$recommended->{'stopsrvcs'},$uses_systemctl);
1.1       raeburn  2178: } else {
1.10      raeburn  2179:     &print_and_log(&mt('Skipping stopping unnecessary service ([_1] daemons).',"'cups','memcached'")."\n");
1.1       raeburn  2180: }
                   2181: 
                   2182: my ($have_tarball,$updateshown);
                   2183: if ($callsub{'download'}) {
                   2184:     ($have_tarball,$updateshown) = &download_loncapa($instdir,$sourcetarball);
                   2185: } else {
                   2186:     print_and_log(&mt('Skipping download of LON-CAPA tar file.')."\n\n");
                   2187:     print &mt('LON-CAPA is available for download from: [_1]',
                   2188:               'http://install.loncapa.org/')."\n";
                   2189:     if (!-e '/etc/loncapa-release') {
                   2190:         &print_and_log(&mt('LON-CAPA is not yet installed on your system.'). 
                   2191:                        "\n\n". 
                   2192:                        &mt('You may retrieve the source for LON-CAPA by executing:')."\n".
                   2193:                        "wget http://install.loncapa.org/versions/$lctarball\n");
                   2194:     } else {
                   2195:         my $currentversion;
                   2196:         if (open(my $fh,"</etc/loncapa-release")) {
                   2197:             my $version = <$fh>;
                   2198:             chomp($version);
                   2199:             if ($version =~ /^\QLON-CAPA release \E([\w\-.]+)$/) {
                   2200:                 $currentversion = $1;
                   2201:             }
                   2202:         }
                   2203:         if ($currentversion ne '') {
                   2204:             print &mt('Version of LON-CAPA currently installed on this server is: [_1].',
                   2205:                       $currentversion),"\n";
                   2206:             if ($production) {
                   2207:                 print &mt('The latest production release of LON-CAPA is [_1].',$production)."\n";
                   2208:             }
                   2209:             if ($testing) {
                   2210:                 print &mt('The latest testing release of LON-CAPA is [_1].',$testing)."\n";
                   2211:             }
                   2212:         }
                   2213:     }
                   2214:     if ($filetouse ne '') {
                   2215:         $have_tarball = 1;
                   2216:     }
                   2217: }
                   2218: 
                   2219: print "\n".&mt('Requested configuration complete.')."\n\n";
                   2220: my $apachename;
                   2221: if ($have_tarball && !$updateshown) {
                   2222:     my ($lcdir) = ($sourcetarball =~ /^([\w.\-]+)\.tar.gz$/);
                   2223:     if (!-e '/etc/loncapa-release') {
                   2224:         print &mt('If you are now ready to install LON-CAPA, enter the following commands:')."\n\n";
                   2225:     } else {
                   2226:         print &mt('If you are now ready to update LON-CAPA, enter the following commands:')."\n\n".
                   2227:                   "/etc/init.d/loncontrol stop\n";
                   2228:         if ($distro =~ /^(suse|sles|debian|ubuntu)([\d.]+)/) {
                   2229:             if (($1 eq 'suse') && ($2 < 10)) {
                   2230:                 $apachename = 'apache';
                   2231:             } else {
                   2232:                 $apachename = 'apache2';
                   2233:             }
                   2234:         } else {
                   2235:             $apachename = 'httpd';
                   2236:         }
                   2237:         print "/etc/init.d/$apachename stop\n";
                   2238:     }
                   2239:     print "cd /root\n".
                   2240:           "tar zxf $sourcetarball\n".
                   2241:           "cd $lcdir\n".
                   2242:           "./UPDATE\n";
                   2243:     if (-e '/etc/loncapa-release') {
                   2244:         print "/etc/init.d/loncontrol start\n";
                   2245:         print "/etc/init.d/$apachename start\n";
                   2246:     }
                   2247: }
                   2248: exit;
                   2249: 
                   2250: #
                   2251: # End main script
                   2252: #
                   2253: 
                   2254: #
                   2255: # Routines for the actions
                   2256: #
                   2257:  
                   2258: sub setup_www {
                   2259:     ##
                   2260:     ## Set up www
                   2261:     ##
                   2262:     print_and_log(&mt('Creating user [_1]',"'www'")."\n");
                   2263:     # -- Add group
                   2264: 
                   2265:     my $status = `/usr/sbin/groupadd www`;
                   2266:     if ($status =~ /\QGroup `www' already exists.\E/) {
                   2267:         print &mt('Group [_1] already exists.',"'www'")."\n";
                   2268:     } elsif ($status ne '') {
                   2269:         print &mt('Unable to add group [_1].',"'www'")."\n";
                   2270:     }
                   2271:    
                   2272:     my $gid = getgrnam('www');
                   2273: 
                   2274:     if (open (PIPE, "/usr/sbin/useradd -c LONCAPA -g $gid www 2>&1 |")) {
                   2275:         $status = <PIPE>;
                   2276:         close(PIPE);
                   2277:         chomp($status);
                   2278:         if ($status =~ /\QAccount `www' already exists.\E/) {
                   2279:             print &mt('Account [_1] already exists.',"'www'")."\n";
                   2280:         } elsif ($status ne '') {
                   2281:             print &mt('Unable to add user [_1].',"'www'")."\n";
                   2282:         }
                   2283:     }  else {
                   2284:         print &mt('Unable to run command to add user [_1].',"'www'")."\n";
                   2285:     }
                   2286: 
                   2287:     my $uid = &uid_of_www();
                   2288:     if (($gid ne '') && ($uid ne '')) {
                   2289:         if (!-e '/home/www') {
                   2290:             mkdir('/home/www',0755);
                   2291:             system('chown www:www /home/www');
                   2292:         }
                   2293:     }
                   2294:     writelog ($status);
                   2295: }
                   2296: 
                   2297: sub uid_of_www {
                   2298:     my ($num) = (getpwnam('www'))[2];
                   2299:     return $num;
                   2300: }
                   2301: 
                   2302: sub build_and_install_mod_auth_external {
                   2303:     my ($instdir) = @_;
                   2304:     my $num = &uid_of_www();
                   2305:     # Patch pwauth
                   2306:     print_and_log(&mt('Building authentication system for LON-CAPA users.')."\n");
                   2307:     my $patch = <<"ENDPATCH";
                   2308: 148c148
                   2309: < #define SERVER_UIDS 99		/* user "nobody" */
                   2310: ---
                   2311: > #define SERVER_UIDS $num		/* user "www" */
                   2312: ENDPATCH
                   2313: 
                   2314:     if (! -e "/usr/bin/patch") {
                   2315: 	print_and_log(&mt('You must install the software development tools package: [_1], when installing Linux.',"'patch'")."\n");
                   2316:         print_and_log(&mt('Authentication installation not completed.')."\n");
                   2317:         return;
                   2318:     }
                   2319:     if (&skip_if_nonempty(`cd /tmp; tar zxf $instdir/pwauth-2.2.8.tar.gz`,
                   2320: 		     &mt('Unable to extract pwauth')."\n")) {
                   2321:         return;
                   2322:     }
                   2323:     my $dir = "/tmp/pwauth-2.2.8";
                   2324:     if (open(PATCH,"| patch $dir/config.h")) {
                   2325:         print PATCH $patch;
                   2326:         close(PATCH);
                   2327:         print_and_log("\n");
                   2328:         ##
                   2329:         ## Compile patched pwauth
                   2330:         ##
                   2331:         print_and_log(&mt('Compiling pwauth')."\n");
1.12      raeburn  2332:         my $result = `cd $dir/; make 2>/dev/null `;
1.1       raeburn  2333:         my $expected = <<"END";
                   2334: gcc -g    -c -o pwauth.o pwauth.c
                   2335: gcc -o pwauth -g  pwauth.o -lcrypt
                   2336: END
                   2337:         if ($result eq $expected) {
                   2338:             print_and_log(&mt('Apparent success compiling pwauth:').
                   2339:                           "\n".$result );
                   2340:             # Install patched pwauth
                   2341:             print_and_log(&mt('Copying pwauth to [_1]',' /usr/local/sbin')."\n");
                   2342:             if (copy "$dir/pwauth","/usr/local/sbin/pwauth") {
1.5       raeburn  2343:                 if (chmod(06755, "/usr/local/sbin/pwauth")) {
1.1       raeburn  2344:                     print_and_log(&mt('[_1] copied successfully',"'pwauth'").
                   2345:                                   "\n");
                   2346:                 } else {
                   2347:                     print &mt('Unable to set permissions on [_1].'.
                   2348:                               "/usr/local/sbin/pwauth")."\n";
                   2349:                 }
                   2350:             } else {
                   2351:                 print &mt('Unable to copy [_1] to [_2]',
                   2352:                           "'$dir/pwauth'","/usr/local/sbin/pwauth")."\n$!\n";
                   2353:             }
                   2354:         } else {
                   2355:             print &mt('Unable to compile patched [_1].'."'pwauth'")."\n";
                   2356:         }
                   2357:     } else {
                   2358:         print &mt('Unable to start patch for [_1]',"'pwauth'")."\n";
                   2359:     }
                   2360:     print_and_log("\n");
                   2361: }
                   2362: 
                   2363: sub kill_extra_services {
1.45      raeburn  2364:     my ($distro,$stopsrvcs,$uses_systemctl) = @_;
1.1       raeburn  2365:     if (ref($stopsrvcs) eq 'HASH') {
                   2366:         my @stopping = sort(keys(%{$stopsrvcs}));
                   2367:         if (@stopping) {
1.6       raeburn  2368:             my $kill_list = join("', '",@stopping);
1.1       raeburn  2369:             if ($kill_list) {
                   2370:                 $kill_list = "'".$kill_list."'";
1.6       raeburn  2371:                 &print_and_log("\n".&mt('Killing unnecessary services ([_1] daemon(s)).',$kill_list)."\n");
                   2372:                 foreach my $service (@stopping) {
                   2373:                     my $daemon = $service;
                   2374:                     if ($service eq 'cups') {
                   2375:                         $daemon = 'cupsd';
                   2376:                         if ($distro =~ /^(?:debian|ubuntu)(\d+)/) {
                   2377:                             my $version = $1;
                   2378:                             if (($distro =~ /^ubuntu/) && ($version <= 8)) {
                   2379:                                 $daemon = 'cupsys';
                   2380:                             }
1.12      raeburn  2381:                         } else {
1.8       raeburn  2382:                             $daemon = 'cups';
1.6       raeburn  2383:                         }
                   2384:                     }
1.12      raeburn  2385:                     my $cmd = "ps -ef |grep '$daemon' |grep -v grep";
                   2386:                     if (open(PIPE,'-|',$cmd)) {
                   2387:                         my $daemonrunning = <PIPE>;
                   2388:                         chomp($daemonrunning);
                   2389:                         close(PIPE);
                   2390:                         if ($daemonrunning) {
                   2391:                             &print_and_log(`/etc/init.d/$daemon stop`);
                   2392:                         }
                   2393:                     }
1.1       raeburn  2394: 	            &print_and_log(&mt('Removing [_1] from startup.',$service)."\n");
1.45      raeburn  2395:                     if ($distro =~ /^(?:debian|ubuntu)(\d+)/) {
                   2396:                         my $version = $1;
                   2397:                         if (($distro =~ /^ubuntu/) && ($version > 16)) {
                   2398:                             if (ref($uses_systemctl) eq 'HASH') {
                   2399:                                 if ($uses_systemctl->{$service}) {
                   2400:                                     if (`systemctl is-enabled $service`) {
                   2401:                                         &print_and_log(`systemctl disable $service`);
                   2402:                                     }
                   2403:                                 }
                   2404:                             }
                   2405:                         } else {
                   2406:                             &print_and_log(`update-rc.d -f $daemon remove`);
                   2407:                         }
1.1       raeburn  2408:                     } else {
1.35      raeburn  2409:                         if (ref($uses_systemctl) eq 'HASH') {
                   2410:                             if ($uses_systemctl->{$service}) {
                   2411:                                 if (`systemctl is-enabled $service`) {                          
                   2412:                                     &print_and_log(`systemctl disable $service`);
                   2413:                                 }
                   2414:                             } else {
                   2415:                                 &print_and_log(`/sbin/chkconfig --del $service`);
                   2416:                             }
                   2417:                         } else {
                   2418: 	                    &print_and_log(`/sbin/chkconfig --del $service`);
                   2419:                         } 
1.1       raeburn  2420:                     }
                   2421:                 }
                   2422:             }
                   2423:         }
                   2424:     }
                   2425:     return;
                   2426: }
                   2427: 
                   2428: sub setup_mysql {
                   2429:     my ($setup_mysql_permissions,$distro,$dbh,$has_pass,$has_lcdb) = @_;
1.4       raeburn  2430:     my @mysql_lc_commands;
1.1       raeburn  2431:     unless ($has_lcdb) {
1.4       raeburn  2432:         push(@mysql_lc_commands,"CREATE DATABASE loncapa");
1.1       raeburn  2433:     }
1.4       raeburn  2434:     push(@mysql_lc_commands,"USE loncapa");
                   2435:     push(@mysql_lc_commands,qq{
1.18      raeburn  2436: CREATE TABLE IF NOT EXISTS metadata (title TEXT, author TEXT, subject TEXT, url TEXT, keywords TEXT, version TEXT, notes TEXT, abstract TEXT, mime TEXT, language TEXT, creationdate DATETIME, lastrevisiondate DATETIME, owner TEXT, copyright TEXT, domain TEXT, dependencies TEXT, modifyinguser TEXT, authorspace TEXT, lowestgradelevel TEXT, highestgradelevel TEXT, standards TEXT, count INT, course INT, course_list TEXT, goto INT, goto_list TEXT, comefrom INT, comefrom_list TEXT, sequsage INT, sequsage_list TEXT, stdno INT, stdno_list TEXT, avetries FLOAT, avetries_list TEXT, difficulty FLOAT, difficulty_list TEXT, disc FLOAT, disc_list TEXT, clear FLOAT, technical FLOAT, correct FLOAT, helpful FLOAT, depth FLOAT, hostname TEXT, FULLTEXT idx_title (title), FULLTEXT idx_author (author), FULLTEXT idx_subject (subject), FULLTEXT idx_url (url), FULLTEXT idx_keywords (keywords), FULLTEXT idx_version (version), FULLTEXT idx_notes (notes), FULLTEXT idx_abstract (abstract), FULLTEXT idx_mime (mime), FULLTEXT idx_language (language), FULLTEXT idx_owner (owner), FULLTEXT idx_copyright (copyright)) ENGINE=MYISAM
1.4       raeburn  2437: });
1.1       raeburn  2438:     if ($setup_mysql_permissions) {
1.4       raeburn  2439:         &setup_mysql_permissions($dbh,$has_pass,@mysql_lc_commands);
1.1       raeburn  2440:     } else {
                   2441:         print_and_log(&mt('Skipping MySQL permissions setup.')."\n");
                   2442:         if ($dbh) {
1.4       raeburn  2443:             if (@mysql_lc_commands) {
                   2444:                 foreach my $lccmd (@mysql_lc_commands) { 
                   2445:                     $dbh->do($lccmd) || print $dbh->errstr."\n";
                   2446:                 }
                   2447:             }
1.1       raeburn  2448:             print_and_log(&mt('MySQL database set up complete.')."\n");
                   2449:         } else {
                   2450:             print_and_log(&mt('Problem accessing MySQL.')."\n");
                   2451:         }
                   2452:     }
                   2453: }
                   2454: 
                   2455: sub setup_mysql_permissions {
1.4       raeburn  2456:     my ($dbh,$has_pass,@mysql_lc_commands) = @_;
1.38      raeburn  2457:     my ($mysqlversion,$mysqlsubver,$mysqlname) = &get_mysql_version();
1.50      raeburn  2458:     my ($usesauth,$is_mariadb,$hasauthcol,@mysql_commands);
1.38      raeburn  2459:     if ($mysqlname =~ /^MariaDB/i) {
1.50      raeburn  2460:         $is_mariadb = 1;
1.38      raeburn  2461:         if ($mysqlversion >= 10.2) {
                   2462:             $usesauth = 1;
1.42      raeburn  2463:         } elsif ($mysqlversion >= 5.5) {
                   2464:             $hasauthcol = 1;
1.38      raeburn  2465:         }
                   2466:     } else {
                   2467:         if (($mysqlversion > 5.7) || (($mysqlversion == 5.7) && ($mysqlsubver > 5))) {
                   2468:             $usesauth = 1;
1.42      raeburn  2469:         } elsif (($mysqlversion >= 5.6) || (($mysqlversion == 5.5) && ($mysqlsubver >= 7))) {
                   2470:             $hasauthcol = 1;
1.38      raeburn  2471:         }
                   2472:     }
                   2473:     if ($usesauth) {
1.50      raeburn  2474:         @mysql_commands = ("INSERT user (Host, User, ssl_cipher, x509_issuer, x509_subject, authentication_string) VALUES('localhost','www','','','','')");
                   2475:         if ($is_mariadb) {
1.52      raeburn  2476:             push(@mysql_commands,"ALTER USER 'www'\@'localhost' IDENTIFIED BY 'localhostkey'");
1.50      raeburn  2477:         } else {
1.52      raeburn  2478:             push(@mysql_commands,"ALTER USER 'www'\@'localhost' IDENTIFIED WITH mysql_native_password BY 'localhostkey'");
1.50      raeburn  2479:         }
1.42      raeburn  2480:     } elsif ($hasauthcol) {
                   2481:         @mysql_commands = ("INSERT user (Host, User, Password, ssl_cipher, x509_issuer, x509_subject, authentication_string) VALUES('localhost','www',password('localhostkey'),'','','','');");
1.36      raeburn  2482:     } else {
1.42      raeburn  2483:         @mysql_commands = ("INSERT user (Host, User, Password, ssl_cipher, x509_issuer, x509_subject) VALUES('localhost','www',password('localhostkey'),'','','');");
1.36      raeburn  2484:     }
1.1       raeburn  2485:     if ($mysqlversion < 4) {
1.4       raeburn  2486:         push (@mysql_commands,"
                   2487: INSERT db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv,Grant_priv,References_priv,Index_priv,Alter_priv) VALUES('localhost','loncapa','www','Y','Y','Y','Y','Y','Y','N','Y','Y','Y')");
                   2488:     } else {
                   2489:         push (@mysql_commands,"
                   2490: INSERT db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv,Grant_priv,References_priv,Index_priv,Alter_priv,Create_tmp_table_priv,Lock_tables_priv) VALUES('localhost','loncapa','www','Y','Y','Y','Y','Y','Y','N','Y','Y','Y','Y','Y')");
1.1       raeburn  2491:     }
1.4       raeburn  2492:     push(@mysql_commands,"DELETE FROM user WHERE host<>'localhost'");
1.1       raeburn  2493:     if ($has_pass) {
                   2494:         if ($dbh) {
1.4       raeburn  2495:             push(@mysql_commands,"FLUSH PRIVILEGES");
                   2496:             if (@mysql_commands) {
                   2497:                 foreach my $cmd (@mysql_commands) {
                   2498:                     $dbh->do($cmd) || print $dbh->errstr."\n";
                   2499:                 }
                   2500:             }
                   2501:             if (@mysql_lc_commands) {
                   2502:                 foreach my $lccmd (@mysql_lc_commands) {
                   2503:                     $dbh->do($lccmd) || print $dbh->errstr."\n";
                   2504:                 }
                   2505:             }
1.1       raeburn  2506:             print_and_log(&mt('Permissions set for LON-CAPA MySQL user: [_1]',"'www'")."\n");
                   2507:         } else {
                   2508:             print_and_log(&mt('Problem accessing MySQL.')."\n".
                   2509:                           &mt('Permissions not set.')."\n");
                   2510:         }
                   2511:     } else {
                   2512:         my ($firstpass,$secondpass,$got_passwd,$newmysqlpass);
                   2513:         print &mt('Please enter a root password for the mysql database.')."\n".
                   2514:               &mt('It does not have to match your root account password, but you will need to remember it.')."\n";
                   2515:         my $maxtries = 10;
                   2516:         my $trial = 0;
                   2517:         while ((!$got_passwd) && ($trial < $maxtries)) {
                   2518:             $firstpass = &get_mysql_password(&mt('Enter password'));
                   2519:             if (length($firstpass) > 5) { 
                   2520:                 $secondpass = &get_mysql_password(&mt('Enter password a second time'));
                   2521:                 if ($firstpass eq $secondpass) {
                   2522:                     $got_passwd = 1;
                   2523:                     $newmysqlpass = $firstpass;
                   2524:                 } else {
                   2525:                     print(&mt('Passwords did not match. Please try again.')."\n");
                   2526:                 }
                   2527:                 $trial ++;
                   2528:             } else {
                   2529:                 print(&mt('Password too short.')."\n".
                   2530:                       &mt('Please choose a password with at least six characters.')."\n");
                   2531:             }
                   2532:         }
                   2533:         if ($got_passwd) {
1.50      raeburn  2534:             my (@newpass_cmds) = &new_mysql_rootpasswd($newmysqlpass,$usesauth,$is_mariadb);
1.4       raeburn  2535:             push(@mysql_commands,@newpass_cmds);
1.1       raeburn  2536:         } else {
                   2537:             print_and_log(&mt('Failed to get MySQL root password from user input.')."\n");
                   2538:         }
                   2539:         if ($dbh) {
1.4       raeburn  2540:             if (@mysql_commands) {
                   2541:                 foreach my $cmd (@mysql_commands) {
                   2542:                     $dbh->do($cmd) || print $dbh->errstr."\n";
                   2543:                 }
                   2544:             }
                   2545:             if (@mysql_lc_commands) {
                   2546:                 foreach my $lccmd (@mysql_lc_commands) {
                   2547:                     $dbh->do($lccmd) || print $dbh->errstr."\n";
                   2548:                 }
                   2549:             }
1.1       raeburn  2550:             if ($got_passwd) {
                   2551:                 print_and_log(&mt('MySQL root password stored.')."\n".
                   2552:                               &mt('Permissions set for LON-CAPA MySQL user: [_1].',"'www'")."\n");
                   2553:             } else {
                   2554:                 print_and_log(&mt('Permissions set for LON-CAPA MySQL user: [_1].',"'www'")."\n");
                   2555:             }
                   2556:         } else {
                   2557:             print_and_log(&mt('Problem accessing MySQL.')."\n".
                   2558:                           &mt('Permissions not set.')."\n");
                   2559:         }
                   2560:     }
                   2561: }
                   2562: 
                   2563: sub new_mysql_rootpasswd {
1.50      raeburn  2564:     my ($currmysqlpass,$usesauth,$is_mariadb) = @_;
1.36      raeburn  2565:     if ($usesauth) {
1.50      raeburn  2566:         if ($is_mariadb) {
1.51      raeburn  2567:             return ("ALTER USER 'root'\@'localhost' IDENTIFIED BY '$currmysqlpass'",
1.50      raeburn  2568:                     "FLUSH PRIVILEGES;");
                   2569:         } else {
1.51      raeburn  2570:             return ("ALTER USER 'root'\@'localhost' IDENTIFIED WITH mysql_native_password BY '$currmysqlpass'",
1.50      raeburn  2571:                     "FLUSH PRIVILEGES;");
                   2572:         }
1.36      raeburn  2573:     } else {
                   2574:         return ("SET PASSWORD FOR 'root'\@'localhost'=PASSWORD('$currmysqlpass')",
                   2575:                 "FLUSH PRIVILEGES;");
                   2576:     }
1.1       raeburn  2577: }
                   2578: 
                   2579: sub get_mysql_version {
1.38      raeburn  2580:     my ($version,$subversion,$name);
1.1       raeburn  2581:     if (open(PIPE," mysql -V |")) {
                   2582:         my $info = <PIPE>;
                   2583:         chomp($info);
                   2584:         close(PIPE);
1.65    ! raeburn  2585:         ($version,$subversion,$name) = ($info =~ /(\d+\.\d+)\.(\d+)(?:\-?(\w*),|)/);
1.1       raeburn  2586:     } else {
                   2587:         print &mt('Could not determine which version of MySQL is installed.').
                   2588:               "\n";
                   2589:     }
1.38      raeburn  2590:     return ($version,$subversion,$name);
1.1       raeburn  2591: }
                   2592: 
                   2593: ###########################################################
                   2594: ##
                   2595: ## RHEL/CentOS/Fedora/Scientific Linux
                   2596: ## Copy LON-CAPA httpd.conf to /etc/httpd/conf
                   2597: ##
                   2598: ###########################################################
                   2599: 
                   2600: sub copy_httpd_conf {
1.46      raeburn  2601:     my ($instdir,$distro,$hostname) = @_;
1.14      raeburn  2602:     my $configfile = 'httpd.conf';
1.56      raeburn  2603:     if ($distro =~ /^(?:centos|rhes|scientific|oracle)(\d+)$/) {
1.29      raeburn  2604:         if ($1 >= 7) {
                   2605:             $configfile = 'apache2.4/httpd.conf';
                   2606:         } elsif ($1 > 5) {
1.14      raeburn  2607:             $configfile = 'new/httpd.conf';
                   2608:         }
                   2609:     } elsif ($distro =~ /^fedora(\d+)$/) {
1.29      raeburn  2610:         if ($1 > 17) {
                   2611:             $configfile = 'apache2.4/httpd.conf';
                   2612:         } elsif ($1 > 10) {
1.14      raeburn  2613:             $configfile = 'new/httpd.conf';
                   2614:         }
                   2615:     }
1.1       raeburn  2616:     print_and_log(&mt('Copying the LON-CAPA [_1] to [_2].',"'httpd.conf'",
                   2617:                   "'/etc/httpd/conf/httpd.conf'")."\n");
                   2618:     copy "/etc/httpd/conf/httpd.conf","/etc/httpd/conf/httpd.conf.original";
1.14      raeburn  2619:     copy "$instdir/centos-rhes-fedora-sl/$configfile","/etc/httpd/conf/httpd.conf";
1.5       raeburn  2620:     chmod(0444,"/etc/httpd/conf/httpd.conf");
1.1       raeburn  2621:     print_and_log("\n");
                   2622: }
                   2623: 
1.59      raeburn  2624: ###########################################################
                   2625: ##
                   2626: ## RHEL/CentOS/Fedora/Scientific Linux
                   2627: ## Copy LON-CAPA mpm.conf to /etc/httpd/conf.modules.d/00-mpm.conf
                   2628: ##
                   2629: ## The LON-CAPA mpm.conf enables the prefork MPM module in
                   2630: ## Apache. This is also the default for RHEL/CentOS/Oracle
1.60      raeburn  2631: ## Linux 7 and earlier, and Fedora 26 and earlier. For more
1.59      raeburn  2632: ## recent versions of those distros, the event MPM is enabled
                   2633: ## by default. After &copy_mpm_conf() is run, the prefork MPM
                   2634: ## module will be enabled instead of the event MPM module.
                   2635: ##
                   2636: ###########################################################
                   2637: 
                   2638: sub copy_mpm_conf {
                   2639:     my ($instdir,$distro) = @_;
                   2640:     my $mpmfile = 'mpm.conf';
                   2641:     if ((-e "/etc/httpd/conf.modules.d/00-mpm.conf") &&
                   2642:         (-e "$instdir/centos-rhes-fedora-sl/$mpmfile")) {
                   2643:         print_and_log(&mt('Copying the LON-CAPA [_1] to [_2].',"'mpm.conf'",
                   2644:                       "'/etc/httpd/conf.modules.d/00-mpm.conf'")."\n");
                   2645:         copy "$instdir/centos-rhes-fedora-sl/$mpmfile","/etc/httpd/conf.modules.d/00-mpm.conf";
                   2646:         chmod(0644,"/etc/httpd/conf.modules.d/00-mpm.conf");
                   2647:         print_and_log("\n");
                   2648:     } else {
                   2649:         my $logfail;
                   2650:         if ($distro =~ /^(?:centos|rhes|scientific|oracle)(\d+)$/) {
                   2651:             if ($1 > 7) {
                   2652:                 $logfail = 1;
                   2653:             }
                   2654:         } elsif ($distro =~ /^fedora(\d+)$/) {
                   2655:             if ($1 > 26) {
                   2656:                 $logfail = 1;
                   2657:             }
                   2658:         }
                   2659:         if ($logfail) {
                   2660:             print_and_log(&mt('Warning: copying the LON-CAPA [_1] failed because [_2] and/or [_3] are missing.',
                   2661:                               $mpmfile,"'$instdir/centos-rhes-fedora-sl/$mpmfile'",
                   2662:                               "'/etc/httpd/conf.modules.d/00-mpm.conf'"));
                   2663:             print_and_log("\n");
                   2664:         }
                   2665:     }
                   2666: }
                   2667: 
1.46      raeburn  2668: ###############################################
                   2669: ##
1.47      raeburn  2670: ## Copy loncapassl.conf and sslrewrite.conf
1.46      raeburn  2671: ##
                   2672: ###############################################
                   2673: 
1.47      raeburn  2674: #
                   2675: # The Apache SSL configuration used by LON-CAPA is contained in
                   2676: # two files: sslrewrite.conf and loncapassl.conf.
                   2677: #
                   2678: # Starting with LON-CAPA 2.12, name-based virtual hosts are used
                   2679: # with port 443. The default virtual host (i.e., the one listed
                   2680: # first) is for the server's standard hostname, and that is the one 
                   2681: # which will respond to client browser requests for https:// pages. 
                   2682: #
                   2683: # Accordingly, a system administrator will need to edit the config
                   2684: # config file to include paths to a signed SSL certificate (public), 
                   2685: # chain (public) and key (private) pem files. The certificate should
                   2686: # have been signed by a recognized certificate authority ((e.g., 
                   2687: # InCommon or Let's Encrypt).
                   2688: #  
                   2689: # The sslrewrite.conf file contains the rewrite configuration for
                   2690: # the default virtual host. The rewrite rules defined are used to 
                   2691: # allow internal HEAD requests to /cgi-bin/mimetex.cgi to be served
                   2692: # http://, in order to support vertical alignment of mimetex images
                   2693: # (one of the options for rendering Math content); (b) allow requests
                   2694: # for certain URLs (external resource, and syllabus, if external URL
                   2695: # used) to be served http:// to accommodate the use of iframes which 
                   2696: # would otherwise result in browser blocking of mixed active content.
                   2697: #
                   2698: # The loncapassl.conf file contains the configuration for the  
                   2699: # "internal" virtual host, which will respond to requests for https://
                   2700: # pages from other LON-CAPA servers in the network to which the node
                   2701: # belongs. The ServerName is internal-<hostname> where <hostname>
                   2702: # is the server's hostname. There is no need to create a DNS entry 
                   2703: # for internal-<hostname>, as LON-CAPA 2.12 automatically performs
                   2704: # the required hostname to IP mapping.
                   2705: # 
                   2706: # Requests to /raw on the "internal" virtual host require a valid
                   2707: # SSL client certificate, signed by the certificate authority
                   2708: # for the LON-CAPA network to which the node belongs.
                   2709: # 
                   2710: # The configuration file to which the contents of sslrewrite.conf
                   2711: # and loncapassl.conf will be written will have either been identified
                   2712: # when &chkapachessl() was run, or if no files were found with
                   2713: # existing rewrite blocks, then a candidate file will be chosen
                   2714: # from the .conf files containing VirtualHosts definitions.
                   2715: # If there is more than one suitable candidate file, the system
                   2716: # administrator will be prompted to select from the available files.
                   2717: #
                   2718: # If there are no files containing VirtualHosts definitions, then
                   2719: # <VirtualHost *:443> </VirtualHost> blocks will be appended to
                   2720: # the standard Apache SSL config for the particular distro:
                   2721: # ssl.conf for RHEL/CentOS/Scientific/Fedora, vhost-ssl.conf
                   2722: # for SuSE/SLES, and default-ssl.conf for Ubuntu.
                   2723: #
                   2724: # Once a file is selected, the contents of sslrewrite.conf and
                   2725: # loncapassl.conf are compared with appropriate blocks in the file
                   2726: # and the user will be prompted to agree to insertion of missing lines
                   2727: # and/or deletion of surplus lines.
                   2728: #
                   2729: 
                   2730: sub copy_apache_sslconf_files {
                   2731:     my ($distro,$hostname,$hostip,$instdir,$targetdir,$targetfilesref,
                   2732:         $has_std,$has_int,$rewritenum,$nochgstd,$nochgint) = @_;
                   2733:     my ($new_std,$new_int);
                   2734:     my (@internal,@standard,%int_by_linenum,%int_by_linetext,
                   2735:         %rule_by_linenum,%rule_by_linetext,%foundint);
1.46      raeburn  2736:     if (-e "$instdir/loncapassl.conf") {
                   2737:         if (open(my $fh,'<',"$instdir/loncapassl.conf")) {
1.47      raeburn  2738:             my $num = 1;
                   2739:             while (<$fh>) {
                   2740:                 chomp();
                   2741:                 if (/^ServerName/) {
                   2742:                     s/(\Qinternal-{[[[[Hostname]]]]}\E)/internal-$hostname/;
                   2743:                 }
                   2744:                 push(@internal,$_);
                   2745:                 $int_by_linenum{$num} = $_;
                   2746:                 s/(^\s+|\s+$)//g;
                   2747:                 push(@{$int_by_linetext{$_}},$num);
                   2748:                 $num ++;
                   2749:             }
                   2750:             close($fh);
                   2751:         }
                   2752:     }
                   2753:     if (-e "$instdir/sslrewrite.conf") {
                   2754:         if (open(my $fh,'<',"$instdir/sslrewrite.conf")) {
                   2755:             my $num = 1;
                   2756:             while (<$fh>) {
                   2757:                 chomp();
                   2758:                 if (/\Q{[[[[HostIP]]]]}\E/) {
                   2759:                     s/(\QRewriteCond %{REMOTE_ADDR} {[[[[HostIP]]]]}\E)/RewriteCond %{REMOTE_ADDR} $hostip/;
                   2760:                 }
                   2761:                 push(@standard,$_);
                   2762:                 $rule_by_linenum{$num} = $_;
                   2763:                 s/(^\s+|\s+$)//g;
                   2764:                 push(@{$rule_by_linetext{$_}},$num);
                   2765:                 $num ++;
                   2766:             }
                   2767:             close($fh);
                   2768:         }
                   2769:     }
                   2770:     if (!$nochgstd) {
                   2771:         if ($has_std eq '') {
                   2772:             my $file;
                   2773:             if ($has_int ne '') {
                   2774:                 if (open(my $fh,'<',"$targetdir/$has_int")) {
                   2775:                     my @saved = <$fh>;
                   2776:                     close($fh);
                   2777:                     if (open(my $fhout, '>',"$targetdir/$has_int")) {
                   2778:                         print $fhout "<VirtualHost *:443>\n".
                   2779:                                      "ServerName $hostname\n".
                   2780:                                      join("\n",@standard)."\n".
                   2781:                                      "</VirtualHost>\n\n".
                   2782:                                      join('',@saved);
                   2783:                         close($fhout);
                   2784:                         $new_int = $has_int; 
                   2785:                     }
                   2786:                 }
                   2787:             }
                   2788:         } else {
                   2789:             if ($rewritenum eq '') {
                   2790:                 &append_to_vhost($targetdir,$has_std,$hostname,\%rule_by_linenum,'std');
                   2791:                 $new_std = $has_std;
                   2792:             } else {
                   2793:                 $new_std = &modify_ssl_config($targetdir,$has_std,$hostname,$rewritenum,
                   2794:                                               \%rule_by_linetext,\%rule_by_linenum,'std');
                   2795:             }
                   2796:         }
                   2797:     }
                   2798:     if (!$nochgint) {
                   2799:         if ($has_int eq '') {
                   2800:             if ($has_std ne '') {
                   2801:                 if (open(my $fhout,'>>',"$targetdir/$has_std")) {
                   2802:                     print $fhout "\n".join("\n",@internal)."\n";
                   2803:                     close($fhout);
                   2804:                     $new_int = $has_std;
                   2805:                 }
                   2806:             }
                   2807:         } else {
                   2808:             $new_int = &modify_ssl_config($targetdir,$has_int,$hostname,$rewritenum,\%int_by_linetext,\%int_by_linenum,'int');
                   2809:         }
                   2810:     }
                   2811:     if (($has_std eq '') && ($has_int eq '')) {
                   2812:         my ($file,$numfiles) = &get_sslconf_filename($distro,$targetdir,$targetfilesref);
                   2813:         if ($numfiles == 0) { 
                   2814:             if (open(my $fhout, '>>', "$targetdir/$file")) {
                   2815:                 print $fhout "<VirtualHost *:443>\n".
                   2816:                              "ServerName $hostname\n".
                   2817:                              join("\n",@standard)."\n".
                   2818:                              "</VirtualHost>\n\n".
                   2819:                              join("\n",@internal)."\n";
                   2820:                 close($fhout);
                   2821:                 $new_std = $file;
                   2822:                 $new_int = $file;
                   2823:             }
                   2824:         } elsif ($numfiles == 1) {
                   2825:             &append_to_vhost($targetdir,$file,$hostname,\%rule_by_linenum,'std');
                   2826:             if (open(my $fhout, '>>', "$targetdir/$file")) {
                   2827:                 print $fhout "\n".join("\n",@internal)."\n";
                   2828:                 close($fhout);
                   2829:                 $new_std = $file;
                   2830:                 $new_int = $file;
                   2831:             }
                   2832:         } elsif ($numfiles == -1) {
                   2833:             print_and_log(&mt('Failed to copy contents of [_1] or [_2] to a file in [_3]',
                   2834:                               "'loncapassl.conf'","'sslrewrite.conf'","'$targetdir'")."\n");
                   2835:         }
                   2836:     }
                   2837:     if ($nochgstd) {
                   2838:         print_and_log(&mt('No change required to file: [_1] in [_2], (no difference between [_3] and rewrite block.)',
                   2839:                           "'$has_std'","'$targetdir'","'sslrewrite.conf'"));
                   2840:     }
                   2841:     if ($nochgint) {
                   2842:         print_and_log(&mt('No change required to file: [_1] in [_2], (no difference between [_3] and virtualhost block.)',
                   2843:                           "'$has_int'","'$targetdir'","'loncapassl.conf'"));
                   2844:     }
                   2845:     if ($new_int) {
                   2846:         print_and_log(&mt('Successfully copied contents of [_1] to [_2].',"'loncapassl.conf'","'$targetdir/$new_int'")."\n");
                   2847:         chmod(0444,"$targetdir/loncapassl.conf");
                   2848:     }
                   2849:     if ($new_std) {
                   2850:         print_and_log(&mt('Successfully copied contents of [_1] to [_2].',"'sslrewrite.conf'","'$targetdir/$new_std'")."\n");
                   2851:         chmod(0444,"$targetdir/loncapassl.conf");
                   2852:     }
                   2853:     return ($new_int,$new_std);
                   2854: }
                   2855: 
                   2856: #
                   2857: # append_to_vhost() is called to add rewrite rules (in a 
                   2858: # <IfModule +mod_rewrite.c> </IfModule> block), provided
                   2859: # in the sslrewrite.conf configuration file, to an Apache
                   2860: # SSL configuration file within a VirtualHost for port 443
                   2861: # (for server's public-facing hostname).
                   2862: #
                   2863: sub append_to_vhost {
                   2864:     my ($targetdir,$filename,$hostname,$by_linenum,$type) = @_;
                   2865:     return unless (ref($by_linenum) eq 'HASH');
                   2866:     my ($startvhost,$endvhost);
                   2867:     if (-e "$targetdir/$filename") {
                   2868:         my (@lines,$currname,$virtualhost,$hasname);
                   2869:         if (open(my $fh,'<',"$targetdir/$filename")) {
                   2870:             my $currline = 0;
                   2871:             while (<$fh>) {
                   2872:                 $currline ++;
                   2873:                 push(@lines,$_);
                   2874:                 chomp();
                   2875:                 s/(^\s+|\s+$)//g;
                   2876:                 if (/^<VirtualHost\s+[^:]*\:443>/) {
                   2877:                     $virtualhost = 1;
                   2878:                     unless ($endvhost) {
                   2879:                         $startvhost = $currline;
1.46      raeburn  2880:                     }
                   2881:                 }
1.47      raeburn  2882:                 if ($virtualhost) {
                   2883:                     if (/^ServerName\s+([^\s]+)\s*$/) {
                   2884:                         $currname = $1;
                   2885:                         unless ($endvhost) {
                   2886:                             if ((($currname eq '') || ($currname eq $hostname)) && ($type eq 'std')) {
                   2887:                                 $hasname = 1;
                   2888:                             }
                   2889:                         }
                   2890:                     }
                   2891:                     if (/^<\/VirtualHost>/) {
                   2892:                         $virtualhost = 0;
                   2893:                         unless ($endvhost) {
                   2894:                             if (((($currname eq '') || ($currname eq $hostname)) && ($type eq 'std')) ||
                   2895:                                 (($currname eq 'internal-'.$hostname) && ($type eq 'int'))) { 
                   2896:                                 $endvhost = $currline;
                   2897:                             } else {
                   2898:                                 undef($startvhost);  
                   2899:                             }
                   2900:                         }
                   2901:                     }
                   2902:                 }
                   2903:             }
                   2904:             close($fh);
                   2905:         }
                   2906:         if ($endvhost) {
                   2907:             if (open(my $fout,'>',"$targetdir/$filename")) {
                   2908:                 for (my $i=0; $i<@lines; $i++) {
                   2909:                     if ($i == $startvhost) {
                   2910:                         unless (($hasname) && ($type eq 'std')) {
                   2911:                             print $fout "ServerName $hostname\n";
                   2912:                         }
                   2913:                     }
                   2914:                     if ($i == $endvhost-1) {
                   2915:                         foreach my $item (sort { $a <=> $b } keys(%{$by_linenum})) {
                   2916:                             print $fout $by_linenum->{$item}."\n";
                   2917:                         }
                   2918:                     }
                   2919:                     print $fout $lines[$i];
                   2920:                 }
                   2921:                 close($fout);
                   2922:             }
                   2923:         }
                   2924:     }
                   2925:     return $endvhost;
                   2926: }
                   2927: 
                   2928: #
                   2929: # get_sslconf_filename() is called when the Apache SSL configuration
                   2930: # option has been selected and there are no files containing
                   2931: # VirtualHost definitions containing rewrite blocks,
                   2932: # 
                   2933: # In this case get_sslconf_filename() is used to chose from the 
                   2934: # available .conf files containing VirtualHosts definitions. If
                   2935: # there is ambiguity about which file to use, &apacheconf_choice()
                   2936: # will be called to prompt the user to choose one of the possible
                   2937: # files.
                   2938: #
                   2939: 
                   2940: sub get_sslconf_filename {
                   2941:     my ($distro,$targetdir,$targetfilesref) = @_;
                   2942:     my ($configfile,$numfiles,@possfiles);
                   2943:     if (ref($targetfilesref) eq 'HASH') {
                   2944:         if (keys(%{$targetfilesref}) > 0) {
                   2945:             foreach my $name (sort(keys(%{$targetfilesref}))) {
                   2946:                 if (ref($targetfilesref->{$name}) eq 'HASH') {
                   2947:                     foreach my $file (sort(keys(%{$targetfilesref->{$name}}))) {
                   2948:                         next if ($file eq '');
                   2949:                         next if (!-e "$targetdir/$file");
                   2950:                         unless (grep(/^\Q$file\E$/,@possfiles)) {
                   2951:                             push(@possfiles,$file);
                   2952:                         }
                   2953:                     }
                   2954:                 }
                   2955:             }
                   2956:         }
                   2957:         if (@possfiles == 0) {
                   2958:             $configfile = 'ssl.conf';
                   2959:             if ($distro =~ /^(suse|sles)/) {
                   2960:                 $configfile = 'vhost-ssl.conf';
                   2961:             } elsif ($distro =~ /^(debian|ubuntu)/) {
                   2962:                 $configfile = 'default-ssl.conf';
                   2963:             }
                   2964:             $numfiles = 0;
                   2965:             print &mt('No configuration files in [_1] contain a <VirtualHost *:443> </VirtualHost> block which can be used to house Apache rewrite rules from https to http.',$targetdir)."\n\n".
                   2966:                   &mt('Accordingly, the contents of sslrewrite.conf will be included in a <VirtualHost *:443> </VirtualHost> block which will be added to a file named: [_1].',$configfile)."\n\n";
                   2967:         } elsif (@possfiles == 1) {
                   2968:             $configfile = $possfiles[0];
                   2969:             $numfiles = 1;
                   2970:             print &mt('A single configuration file in [_1] contains a <VirtualHost *:443> </VirtualHost> block.',$targetdir)."\n".
                   2971:                   &mt('The contents of sslrewrite.conf will be added to this block.')."\n\n";
                   2972:         } else {
                   2973:             print &mt('More than one Apache config file contains a <VirtualHost *:443> </VirtualHost> block.')."\n\n".&mt('The possible files are:')."\n";
                   2974:             my $counter = 1;
                   2975:             my $max = scalar(@possfiles);
                   2976:             foreach my $file (@possfiles) {
                   2977:                 print "$counter. $file\n";
                   2978:                 $counter ++;
                   2979:             }
                   2980:             print "\n".&mt('Enter a number between 1 and [_1] to indicate which file should be modified to include the contents of sslrewrite.conf.',$max)."\n";
                   2981:             my $choice = &apacheconf_choice($max);
                   2982:             if (($choice =~ /^\d+$/) && ($choice >= 1) && ($choice <= $max)) {
                   2983:                 $configfile = $possfiles[$choice-1];
                   2984:                 $numfiles = 1; 
1.46      raeburn  2985:             } else {
1.47      raeburn  2986:                 $numfiles = -1;
1.46      raeburn  2987:             }
                   2988:         }
                   2989:     }
1.47      raeburn  2990:     return ($configfile,$numfiles);
                   2991: }
                   2992: 
                   2993: #
                   2994: # &apacheconf_choice() prompts a user to choose an integer between 1 and the  
                   2995: # maximum number of available of possible Apache SSL config files found
                   2996: # at the distros standard location for Apache config files containing
                   2997: # VirtualHost definitions.
                   2998: #
                   2999: # This routine is called recursively until the user enters a valid integer.
                   3000: #
                   3001: 
                   3002: sub apacheconf_choice {
                   3003:     my ($max) = @_;
                   3004:     my $choice = <STDIN>;
                   3005:     chomp($choice);
                   3006:     $choice =~ s/(^\s+|\s+$)//g;
                   3007:     my $configfile;
                   3008:     if (($choice =~ /^\d+$/) && ($choice >= 1) && ($choice <= $max)) {
                   3009:         $configfile = $choice;
                   3010:     } 
                   3011:     while ($configfile eq '') {
                   3012:         print &mt('Invalid choice.  Please enter a number between 1 and [_1].',$max)."\n";
                   3013:         $configfile = &apacheconf_choice($max);
                   3014:     }
                   3015:     print "\n";
                   3016:     return $configfile;
                   3017: }
                   3018: 
                   3019: #
                   3020: # &modify_ssl_config() is called to modify the contents of an Apache SSL config
                   3021: # file so that it has two <VirtualHost *:443> </VirtualHost> blocks containing
                   3022: # (a) the default VirtualHost with the <IfModule mod_rewrite.c> </IfModule> block 
                   3023: # provided in sslrewrites.conf, and (b) an "internal" VirtualHost with the 
                   3024: # content provided in loncapassl.conf.
                   3025: #
                   3026: # This routine will prompted you to agree to insertion of lines present in the
                   3027: # shipped conf file, but missing from the local config file, and also for
                   3028: # deletion of lines present in the local config file, but not required in
                   3029: # the shipped conf file.
                   3030: # 
                   3031:  
                   3032: sub modify_ssl_config {
                   3033:     my ($targetdir,$filename,$hostname,$rewritenum,$by_linetext,$by_linenum,$type) = @_;
                   3034:     return unless ((ref($by_linetext) eq 'HASH') && (ref($by_linenum) eq 'HASH'));
                   3035:     if (-e "$targetdir/$filename") {
                   3036:         my (@lines,$virtualhost,$currname,$rewrite);
                   3037:         if (open(my $fh,'<',"$targetdir/$filename")) {
                   3038:             my %found;
                   3039:             my %possible;
                   3040:             my $currline = 0;
                   3041:             my $rewritecount = 0;
                   3042:             while (<$fh>) {
                   3043:                 $currline ++;
                   3044:                 push(@lines,$_);
                   3045:                 chomp();
                   3046:                 s/(^\s+|\s+$)//g;
                   3047:                 if (/^\s*<VirtualHost\s+[^:]*\:443>\s*$/) {
                   3048:                     $virtualhost = 1;
                   3049:                 }
                   3050:                 if ($virtualhost) {
                   3051:                     if ((exists($by_linetext->{$_})) && (ref($by_linetext->{$_}) eq 'ARRAY') &&
                   3052:                         (@{$by_linetext->{$_}} > 0)) {
                   3053:                         $possible{$currline} = shift(@{$by_linetext->{$_}});
                   3054:                     } 
                   3055:                     if (/^\s*<\/VirtualHost>/) {
                   3056:                         if ((($currname eq 'internal-'.$hostname) && ($type eq 'int')) ||
                   3057:                             ((($currname eq $hostname) || ($currname eq '')) && ($type eq 'std') &&
                   3058:                               ($rewritecount == $rewritenum))) {
                   3059:                             %found = (%found,%possible);
                   3060:                         } else {
                   3061:                             foreach my $line (sort {$b <=> $a } keys(%possible)) {
                   3062:                                 my $num = $possible{$line};
                   3063:                                 if (ref($by_linetext->{$by_linenum->{$num}}) eq 'ARRAY') { 
                   3064:                                     unshift(@{$by_linetext->{$by_linenum->{$num}}},$num);
                   3065:                                 }
                   3066:                             } 
                   3067:                         }
                   3068:                         undef(%possible);
                   3069:                         $virtualhost = 0;
                   3070:                         $currname = '';
                   3071:                     } elsif (/^\s*ServerName\s+([^\s]+)\s*$/) {
                   3072:                         $currname = $1;
                   3073:                     } elsif (/^\s*<IfModule\s+mod_rewrite\.c>/) {
                   3074:                         $rewrite = 1;
                   3075:                     } elsif (/^\s*<\/IfModule>/) {
                   3076:                         $rewritecount ++;
                   3077:                         $rewrite = 0;
                   3078:                     }
                   3079:                 }
                   3080:             }
                   3081:             close($fh);
                   3082:             if (open(my $fout,'>',"$targetdir/$filename")) {
                   3083:                 my $currline = 0;
                   3084:                 my ($lastfound,$done);
                   3085:                 my $numfound = 0;
                   3086:                 foreach my $line (@lines) {
                   3087:                     $currline ++;
                   3088:                     if ($done) {
                   3089:                         print $fout $line;
                   3090:                     } elsif ($lastfound) {
                   3091:                         if ($found{$currline}) {
                   3092:                             for (my $i=$lastfound+1; $i<$found{$currline}; $i++) {
                   3093:                                 print &mt('The following line is missing from the current <VirtualHost *:443> </VirtualHost> block:')."\n".
                   3094:                                       $by_linenum->{$i}."\n".
                   3095:                                       &mt('Add this line? ~[Y/n~]');
                   3096:                                 if (&get_user_selection(1)) {
                   3097:                                     print $fout $by_linenum->{$i}."\n";
                   3098:                                 }
                   3099:                             }
                   3100:                             $numfound ++;
                   3101:                             $lastfound = $found{$currline};
                   3102:                             print $fout $line;
                   3103:                             if ($numfound == scalar(keys(%found))) {
                   3104:                                 $done = 1;
                   3105:                                 for (my $i=$found{$currline}+1; $i<=scalar(keys(%{$by_linenum})); $i++) {
                   3106:                                     print &mt('The following line is missing from the current <VirtualHost *:443> </VirtualHost> block:')."\n".
                   3107:                                           $by_linenum->{$i}."\n".
                   3108:                                           &mt('Add this line? ~[Y/n~]');
                   3109:                                     if (&get_user_selection(1)) {
                   3110:                                         print $fout $by_linenum->{$i}."\n";
                   3111:                                     }
                   3112:                                 }
                   3113:                             }
                   3114:                         } else {
                   3115:                             print &mt('The following line found within a <VirtualHost *:443> </VirtualHost> block does not match that expected by LON-CAPA:')."\n".
                   3116:                                   $line.
                   3117:                                   &mt('Delete this line? ~[Y/n~]');
                   3118:                             if (!&get_user_selection(1)) {
                   3119:                                 print $fout $line;
                   3120:                             }
                   3121:                         }
                   3122:                     } elsif ($found{$currline}) {
                   3123:                         $numfound ++;
                   3124:                         $lastfound = $found{$currline};
                   3125:                         for (my $i=1; $i<$found{$currline}; $i++) {
                   3126:                             print &mt('The following line is missing from the current <VirtualHost *:443> </VirtualHost> block:')."\n".
                   3127:                                   $by_linenum->{$i}."\n".
                   3128:                                   &mt('Add this line? ~[Y/n~]');
                   3129:                             if (&get_user_selection(1)) {
                   3130:                                 print $fout $by_linenum->{$i}."\n";
                   3131:                             }
                   3132:                         }
                   3133:                         print $fout $line;
                   3134:                     } else {
                   3135:                         print $fout $line;
                   3136:                     }
                   3137:                 }
                   3138:                 close($fout);
                   3139:             }
1.46      raeburn  3140:         }
                   3141:     }
1.47      raeburn  3142:     return $filename;
1.46      raeburn  3143: }
                   3144: 
1.1       raeburn  3145: #########################################################
                   3146: ##
1.17      raeburn  3147: ## Ubuntu/Debian -- copy our loncapa configuration file to
1.1       raeburn  3148: ## sites-available and set the symlink from sites-enabled.
                   3149: ##
                   3150: #########################################################
                   3151: 
                   3152: sub copy_apache2_debconf {
1.46      raeburn  3153:     my ($instdir,$distro,$hostname) = @_;
1.6       raeburn  3154:     my $apache2_mods_enabled_dir = '/etc/apache2/mods-enabled';
                   3155:     my $apache2_mods_available_dir = '/etc/apache2/mods-available';
                   3156:     foreach my $module ('headers.load','expires.load') {
                   3157:         unless (-l "$apache2_mods_enabled_dir/$module") {
                   3158:             symlink("$apache2_mods_available_dir/$module","$apache2_mods_enabled_dir/$module");
                   3159:             print_and_log(&mt('Enabling "[_1]" Apache module.',$module)."\n");
                   3160:         }
                   3161:     }
1.28      raeburn  3162:     my $apache2_sites_enabled_dir = '/etc/apache2/sites-enabled';
                   3163:     my $apache2_sites_available_dir = '/etc/apache2/sites-available';
                   3164:     my $defaultconfig = "$apache2_sites_enabled_dir/000-default";
                   3165:     my ($distname,$version);
                   3166:     if ($distro =~ /^(debian|ubuntu)(\d+)$/) {
                   3167:         $distname = $1;
                   3168:         $version = $2;
                   3169:     }
                   3170:     if (($distname eq 'ubuntu') && ($version > 12)) {
                   3171:         $defaultconfig = "$apache2_sites_enabled_dir/000-default.conf";
                   3172:     }
1.64      raeburn  3173:     my ($skipconf,$skipsite,$skipstatus);
1.28      raeburn  3174:     if (($distname eq 'ubuntu') && ($version > 12)) {
                   3175:         my $apache2_conf_enabled_dir = '/etc/apache2/conf-enabled';
                   3176:         my $apache2_conf_available_dir = '/etc/apache2/conf-available';
1.64      raeburn  3177:         my $defaultconf = $apache2_conf_enabled_dir.'/loncapa.conf';
                   3178:         if ((-e "$apache2_conf_available_dir/loncapa") && (-e "$instdir/debian-ubuntu/ubuntu14/loncapa_conf")) {
                   3179:             if (open(PIPE, "diff --brief $apache2_conf_available_dir/loncapa $instdir/debian-ubuntu/ubuntu14/loncapa_conf" |")) {
                   3180:                 my $diffres = <PIPE>;
                   3181:                 close(PIPE);
                   3182:                 chomp($diffres);
                   3183:                 if ($diffres) {
                   3184:                     copy("$apache2_conf_available_dir/loncapa","$apache2_conf_available_dir/loncapa.original");
                   3185:                 }
                   3186:                 if (-l $defaultconf) {
                   3187:                     my $linkfname = readlink($defaultconf);
                   3188:                     if ($linkfname eq "$apache2_conf_available_dir/loncapa") {
                   3189:                         unless ($diffres) {
                   3190:                             $skipconf = 1;
                   3191:                         }
                   3192:                     }
                   3193:                 }
                   3194:             }
                   3195:         }
                   3196:         unless ($skipconf) {
                   3197:             print_and_log(&mt('Copying loncapa [_1] config file to [_2] and pointing [_3] to it from conf-enabled.',"'apache2'","'/etc/apache2/conf-available'","'loncapa.conf symlink'")."\n");
                   3198:             copy("$instdir/debian-ubuntu/ubuntu14/loncapa_conf","$apache2_conf_available_dir/loncapa");
                   3199:             chmod(0444,"$apache2_conf_available_dir/loncapa");
                   3200:             if (-l $defaultconf) {
                   3201:                 unlink($defaultconf);
                   3202:             }
                   3203:             symlink("$apache2_conf_available_dir/loncapa","$defaultconf");
                   3204:         }
                   3205:         my $stdsite = "$instdir/debian-ubuntu/ubuntu14/loncapa_site";
                   3206:         if ((-e $stdsite) && (-e "$apache2_sites_available_dir/loncapa")) {
                   3207:             if (open(PIPE, "diff --brief $stdsite $apache2_sites_available_dir/loncapa |")) {
                   3208:                 my $diffres = <PIPE>;
                   3209:                 close(PIPE);
                   3210:                 chomp($diffres);
                   3211:                 if ($diffres) {
                   3212:                     copy("$apache2_sites_available_dir/loncapa","$apache2_sites_available_dir/loncapa.original");
                   3213:                 }
                   3214:                 if (-l $defaultconfig) {
                   3215:                     my $linkfname = readlink($defaultconfig);
                   3216:                     if ($linkfname eq "$apache2_sites_available_dir/loncapa") {
                   3217:                         unless ($diffres) {
                   3218:                             $skipsite = 1;
                   3219:                         }
1.65    ! raeburn  3220:                     }
1.64      raeburn  3221:                 }
                   3222:             }
                   3223:         }
                   3224:         unless ($skipsite) {
                   3225:             print_and_log(&mt('Copying loncapa [_1] site file to [_2] and pointing [_3] to it from sites-enabled.',"'apache2'","'/etc/apache2/sites-available'","'000-default.conf symlink'")."\n");
                   3226:             copy("$instdir/debian-ubuntu/ubuntu14/loncapa_site","$apache2_sites_available_dir/loncapa");
                   3227:             chmod(0444,"$apache2_sites_available_dir/loncapa");
                   3228:             symlink("$apache2_sites_available_dir/loncapa","$defaultconfig");
                   3229:         }
                   3230:     } else {
                   3231:         if ((-e "$instdir/debian-ubuntu/loncapa") && (-e "$apache2_sites_available_dir/loncapa")) {
                   3232:             if (open(PIPE, "diff --brief $instdir/debian-ubuntu/loncapa $apache2_sites_available_dir/loncapa |")) {
                   3233:                 my $diffres = <PIPE>;
                   3234:                 close(PIPE);
                   3235:                 chomp($diffres);
                   3236:                 if ($diffres) {
                   3237:                     copy("$apache2_sites_available_dir/loncapa","$apache2_sites_available_dir/loncapa.original");
                   3238:                 }
                   3239:                 if (-l $defaultconfig) {
                   3240:                     my $linkfname = readlink($defaultconfig);
                   3241:                     if ($linkfname eq "$apache2_sites_available_dir/loncapa") {
                   3242:                         unless ($diffres) {
                   3243:                             $skipsite = 1;
                   3244:                         }
                   3245:                     }
                   3246:                 }
                   3247:             }
                   3248:         }
                   3249:         unless ($skipsite) {
                   3250:             if (-l $defaultconfig) {
                   3251:                 unlink($defaultconfig);
                   3252:             }
                   3253:             print_and_log(&mt('Copying loncapa [_1] config file to [_2] and pointing [_3] to it from sites-enabled.',"'apache2'","'/etc/apache2/sites-available'","'000-default symlink'")."\n");
                   3254:             if (-e "$instdir/debian-ubuntu/loncapa") {
                   3255:                 copy("$instdir/debian-ubuntu/loncapa","$apache2_sites_available_dir/loncapa");
                   3256:                 chmod(0444,"$apache2_sites_available_dir/loncapa");
                   3257:                 symlink("$apache2_sites_available_dir/loncapa","$apache2_sites_enabled_dir/000-default");
                   3258:             }
                   3259:         }
                   3260:     }
                   3261:     if ($distname eq 'ubuntu') {
                   3262:         my $sitestatus = "$apache2_mods_available_dir/status.conf";
                   3263:         my $stdstatus = "$instdir/debian-ubuntu/status.conf";
                   3264:         if ((-e $sitestatus) && (-e $stdstatus)) {
                   3265:             if (open(PIPE, "diff --brief $stdstatus $sitestatus |")) {
                   3266:                 my $diffres = <PIPE>;
                   3267:                 close(PIPE);
                   3268:                 chomp($diffres);
                   3269:                 if ($diffres) {
                   3270:                     copy("$apache2_mods_available_dir/status.conf","$apache2_mods_available_dir/status.conf.original");
                   3271:                 } else {
                   3272:                     $skipstatus = 1;
                   3273:                 }
                   3274:             }
                   3275:         }
                   3276:         unless ($skipstatus) {
                   3277:             if (-e $stdstatus) {
                   3278:                 print_and_log(&mt('Copying loncapa [_1] file to [_2],',"'status.conf'","'/etc/apache2/mods-available/status.conf'")."\n");
                   3279:                 copy($stdstatus,$sitestatus);
                   3280:                 chmod(0644,$sitestatus);
                   3281:             }
1.28      raeburn  3282:         }
                   3283:     }
1.1       raeburn  3284:     print_and_log("\n");
                   3285: }
                   3286: 
                   3287: ###########################################################
                   3288: ##
                   3289: ## openSuSE/SLES Copy apache2 config files:
                   3290: ##   default-server.conf, uid.conf, /etc/sysconfig/apache2 
                   3291: ##   and create symlink from /srv/www/conf to /etc/apache2 
                   3292: ##
                   3293: ###########################################################
                   3294: 
                   3295: sub copy_apache2_suseconf {
1.49      raeburn  3296:     my ($instdir,$hostname,$distro) = @_;
                   3297:     my ($name,$version) = ($distro =~ /^(suse|sles)([\d\.]+)$/);
                   3298:     my $conf_file = "$instdir/sles-suse/default-server.conf";
                   3299:     if (($name eq 'sles') && ($version >= 12)) {
                   3300:         $conf_file = "$instdir/sles-suse/apache2.4/default-server.conf";
                   3301:     }
1.1       raeburn  3302:     print_and_log(&mt('Copying the LON-CAPA [_1] to [_2].',
                   3303:                   "'default-server.conf'",
                   3304:                   "'/etc/apache2/default-server.conf'")."\n");
                   3305:     if (!-e "/etc/apache2/default-server.conf.original") {
                   3306:         copy "/etc/apache2/default-server.conf","/etc/apache2/default-server.conf.original";
                   3307:     }
1.49      raeburn  3308:     copy $conf_file,"/etc/apache2/default-server.conf";
1.5       raeburn  3309:     chmod(0444,"/etc/apache2/default-server.conf");
1.1       raeburn  3310:     # Make symlink for conf directory (included in loncapa_apache.conf)
                   3311:     my $can_symlink = (eval { symlink('/etc/apache2','/srv/www/conf'); }, $@ eq '');
                   3312:     if ($can_symlink) {
                   3313:         &print_and_log(&mt('Symlink created for [_1] to [_2].',
                   3314:                        "'/srv/www/conf'","'/etc/apache2'")."\n");
                   3315:     } else {
                   3316:         &print_and_log(&mt('Symlink creation failed for [_1] to [_2]. You will need to perform this action from the command line.',"'/srv/www/conf'","'/etc/apache2'")."\n");
                   3317:     }
                   3318:     &copy_apache2_conf_files($instdir);
1.49      raeburn  3319:     &copy_sysconfig_apache2_file($instdir,$name,$version); 
1.1       raeburn  3320:     print_and_log("\n");
                   3321: }
                   3322: 
                   3323: ###############################################
                   3324: ##
                   3325: ## Modify uid.conf
                   3326: ##
                   3327: ###############################################
                   3328: sub copy_apache2_conf_files {
                   3329:     my ($instdir) = @_;
                   3330:     print_and_log(&mt('Copying the LON-CAPA [_1] to [_2].',
                   3331:                   "'uid.conf'","'/etc/apache2/uid.conf'")."\n");
                   3332:     if (!-e "/etc/apache2/uid.conf.original") {
                   3333:         copy "/etc/apache2/uid.conf","/etc/apache2/uid.conf.original";
                   3334:     }
1.9       raeburn  3335:     copy "$instdir/sles-suse/uid.conf","/etc/apache2/uid.conf";
1.5       raeburn  3336:     chmod(0444,"/etc/apache2/uid.conf");
1.1       raeburn  3337: }
                   3338: 
                   3339: ###############################################
                   3340: ##
                   3341: ## Modify /etc/sysconfig/apache2  
                   3342: ##
                   3343: ###############################################
                   3344: sub copy_sysconfig_apache2_file {
1.49      raeburn  3345:     my ($instdir,$name,$version) = @_;
1.1       raeburn  3346:     print_and_log(&mt('Copying the LON-CAPA [_1] to [_2].',"'sysconfig/apache2'","'/etc/sysconfig/apache2'")."\n");
                   3347:     if (!-e "/etc/sysconfig/apache2.original") {
                   3348:         copy "/etc/sysconfig/apache2","/etc/sysconfig/apache2.original";
                   3349:     }
1.49      raeburn  3350:     my $sysconf_file = "$instdir/sles-suse/sysconfig_apache2";
                   3351:     if (($name eq 'sles') && ($version >= 12)) {
                   3352:        $sysconf_file = "$instdir/sles-suse/apache2.4/sysconfig_apache2";
                   3353:     }
1.57      raeburn  3354:     copy $sysconf_file,"/etc/sysconfig/apache2";
1.5       raeburn  3355:     chmod(0444,"/etc/sysconfig/apache2");
1.1       raeburn  3356: }
                   3357: 
                   3358: ###############################################
                   3359: ##
                   3360: ## Add/Modify /etc/insserv/overrides
                   3361: ##
                   3362: ###############################################
                   3363: 
                   3364: sub update_SuSEfirewall2_setup {
                   3365:     my ($instdir) = @_;
                   3366:     print_and_log(&mt('Copying the LON-CAPA [_1] to [_2].',"'SuSEfirewall2_setup'","'/etc/insserv/overrides/SuSEfirewall2_setup'")."\n");
                   3367:     if (!-e "/etc/insserv/overrides/SuSEfirewall2_setup") {
                   3368:         if (!-d "/etc/insserv") {
                   3369:             mkdir("/etc/insserv",0755); 
                   3370:         }
                   3371:         if (!-d "/etc/insserv/overrides") {
                   3372:             mkdir("/etc/insserv/overrides",0755);
                   3373:         }
                   3374:     } elsif (!-e  "/etc/insserv/overrides/SuSEfirewall2_setup.original") {
                   3375:         copy "/etc/insserv/overrides/SuSEfirewall2_setup","/etc/insserv/overrides/SuSEfirewall2_setup.original"
                   3376:     }
1.9       raeburn  3377:     copy "$instdir/sles-suse/SuSEfirewall2_setup","/etc/insserv/overrides/SuSEfirewall2_setup";
1.5       raeburn  3378:     chmod(0444,"/etc/insserv/overrides/SuSEfirewall2_setup");
                   3379: }
                   3380: 
                   3381: sub get_iptables_rules {
                   3382:     my ($distro,$instdir,$apachefw) = @_;
                   3383:     my (@fwchains,@ports);
                   3384:     if (&firewall_is_active()) {
                   3385:         my $iptables = &get_pathto_iptables();
                   3386:         if ($iptables ne '') {
                   3387:             @fwchains = &get_fw_chains($iptables,$distro);
                   3388:         }
                   3389:     }
                   3390:     if (ref($apachefw) eq 'HASH') {
                   3391:         foreach my $service ('http','https') {
                   3392:             unless ($apachefw->{$service}) {
                   3393:                 push (@ports,$service); 
                   3394:             }
                   3395:         }
                   3396:     } else {
                   3397:         @ports = ('http','https');
                   3398:     }
                   3399:     if (@ports == 0) {
                   3400:         return;
                   3401:     }
                   3402:     my $ask_to_enable;
                   3403:     if (-e "/etc/iptables.loncapa.rules") {
1.9       raeburn  3404:         if (open(PIPE, "diff --brief $instdir/debian-ubuntu/iptables.loncapa.rules /etc/iptables.loncapa.rules |")) {
1.5       raeburn  3405:             my $diffres = <PIPE>;
                   3406:             close(PIPE);
                   3407:             chomp($diffres);
                   3408:             if ($diffres) {
                   3409:                 print &mt('Warning: [_1] exists but differs from LON-CAPA supplied file.','/etc/iptables.loncapa.rules')."\n";
                   3410:             }
                   3411:         } else {
                   3412:             print &mt('Error: unable to open [_1] to compare contents with LON-CAPA supplied file.','/etc/iptables.loncapa.rules')."\n";
                   3413:         }
                   3414:     } else {
1.9       raeburn  3415:         if (-e "$instdir/debian-ubuntu/iptables.loncapa.rules") {
                   3416:             copy "$instdir/debian-ubuntu/iptables.loncapa.rules","/etc/iptables.loncapa.rules";
1.5       raeburn  3417:             chmod(0600,"/etc/iptables.loncapa.rules");
                   3418:         }
                   3419:     }
                   3420:     if (-e "/etc/iptables.loncapa.rules") {
                   3421:         if (-e "/etc/network/if-pre-up.d/iptables") {
1.9       raeburn  3422:             if (open(PIPE, "diff --brief $instdir/debian-ubuntu/iptables /etc/network/if-pre-up/iptables |")) {
1.5       raeburn  3423:                 my $diffres = <PIPE>;
                   3424:                 close(PIPE);
                   3425:                 chomp($diffres);
                   3426:                 if ($diffres) {
                   3427:                     print &mt('Warning: [_1] exists but differs from LON-CAPA supplied file.','/etc/network/if-pre-up.d/iptables')."\n";
                   3428:                 }
                   3429:             } else {
                   3430:                 print &mt('Error: unable to open [_1] to compare contents with LON-CAPA supplied file.','/etc/network/if-pre-up.d/iptables')."\n";
                   3431:             }
                   3432:         } else {
1.9       raeburn  3433:             copy "$instdir/debian-ubuntu/iptables","/etc/network/if-pre-up.d/iptables";
1.5       raeburn  3434:             chmod(0755,"/etc/network/if-pre-up.d/iptables");
                   3435:             print_and_log(&mt('Installed script "[_1]" to add iptables rules to block all ports except 22, 80, and 443 when network is enabled during boot.','/etc/network/if-pre-up.d/iptables'));
                   3436:             $ask_to_enable = 1;
                   3437:         }
                   3438:     }
                   3439:     return $ask_to_enable;
1.1       raeburn  3440: }
                   3441: 
                   3442: sub download_loncapa {
                   3443:     my ($instdir,$lctarball) = @_;
                   3444:     my ($have_tarball,$updateshown);
                   3445:     if (! -e "$instdir/$lctarball") {
                   3446: 	print_and_log(&mt('Retrieving LON-CAPA source files from: [_1]',
                   3447: 		      'http://install.loncapa.org')."\n");
                   3448: 	system("wget http://install.loncapa.org/versions/$lctarball ".
                   3449: 	       "2>/dev/null 1>/dev/null");
                   3450: 	if (! -e "./$lctarball") {
                   3451: 	    print &mt('Unable to retrieve LON-CAPA source files from: [_1].', 
                   3452: 		     "http://install.loncapa.org/versions/$lctarball")."\n";
                   3453: 	} else {
                   3454:             $have_tarball = 1;
                   3455:         }
                   3456: 	print_and_log("\n");
                   3457:     } else {
                   3458:         $have_tarball = 1;
                   3459: 	print_and_log("
                   3460: ------------------------------------------------------------------------
                   3461: 
                   3462: ".&mt('You seem to have a version of loncapa-current.tar.gz in [_1]',$instdir)."\n".
                   3463: &mt('This copy will be used and a new version will NOT be downloaded.')."\n".
                   3464: &mt('If you wish, you may download a new version by executing:')."
                   3465: 
1.2       raeburn  3466: wget http://install.loncapa.org/versions/loncapa-current.tar.gz
1.1       raeburn  3467: 
                   3468: ------------------------------------------------------------------------
                   3469: ");
                   3470:     }
                   3471: 
                   3472:     ##
                   3473:     ## untar loncapa.tar.gz
                   3474:     ##
                   3475:     if ($have_tarball) {
                   3476:         print_and_log(&mt('Extracting LON-CAPA source files')."\n");
                   3477:         writelog(`cd ~root; tar zxf $instdir/$lctarball`);
                   3478:         print_and_log("\n");
                   3479:         print &mt('LON-CAPA source files extracted.')."\n".
                   3480:               &mt('It remains for you to execute the following commands:')."
                   3481: 
1.63      raeburn  3482: cd /root/loncapa-X.Y.Z     (X.Y.Z should correspond to a version number like '2.11.3')
1.1       raeburn  3483: ./UPDATE
                   3484: 
                   3485: ".&mt('If you have any trouble, please see [_1] and [_2]',
                   3486:       'http://install.loncapa.org/','http://help.loncapa.org/')."\n";
                   3487:         $updateshown = 1;
                   3488:     }
                   3489:     return ($have_tarball,$updateshown);
                   3490: }
                   3491: 
                   3492: close LOG;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>