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

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

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