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

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

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