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

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

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