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

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

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