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

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;
                     29: use DBI;
1.43      raeburn    30: use Cwd();
                     31: use File::Basename();
                     32: use lib File::Basename::dirname(Cwd::abs_path($0));
1.1       raeburn    33: use LCLocalization::localize;
                     34: 
                     35: # ========================================================= The language handle
                     36: 
                     37: my %languages = (
                     38:                   ar => 'Arabic', 
                     39:                   de => 'German',
                     40:                   en => 'English',
                     41:                   es => 'Spanish (Castellan)',
                     42:                   fa => 'Persian',
                     43:                   fr => 'French', 
                     44:                   he => 'Hebrew', 
                     45:                   ja => 'Japanese',
                     46:                   pt => 'Portuguese',
                     47:                   ru => 'Russian',
                     48:                   tr => 'Turkish',
                     49:                   zh => 'Chinese Simplified'
                     50:                ); 
                     51: 
                     52: use vars qw($lh $lang);
                     53: $lang = 'en';
                     54: if (@ARGV > 0) {
                     55:     foreach my $poss (keys(%languages)) {
                     56:         if ($ARGV[0] eq $poss) {
                     57:             $lang = $ARGV[0]; 
                     58:         }
                     59:     }
                     60: }
                     61: 
                     62: &get_language_handle($lang);
                     63: 
                     64: # Check user has root privs
                     65: if (0 != $<) {
                     66:     print &mt('This script must be run as root.')."\n".
                     67:           &mt('Stopping execution.')."\n";
                     68:     exit;
                     69: }
                     70: 
                     71: 
                     72: # Globals: filehandle LOG is global.
                     73: if (!open(LOG,">>loncapa_install.log")) {
                     74:     print &mt('Unable to open log file.')."\n".
                     75:           &mt('Stopping execution.')."\n";
                     76:     exit;
                     77: } else {
1.45.2.9! raeburn    78:     print LOG '$Id: install.pl,v 1.45.2.8 2020/07/08 17:40:18 raeburn Exp $'."\n";
1.1       raeburn    79: }
                     80: 
                     81: #
1.2       raeburn    82: # Helper routines and routines to establish recommended actions
1.1       raeburn    83: #
                     84: 
                     85: sub get_language_handle {
                     86:     my @languages = @_;
                     87:     $lh=LCLocalization::localize->get_handle(@languages);
                     88: }
                     89: 
                     90: sub mt (@) {
                     91:     if ($lh) {
                     92:         if ($_[0] eq '') {
                     93:             if (wantarray) {
                     94:                 return @_;
                     95:             } else {
                     96:                 return $_[0];
                     97:             }
                     98:         } else {
                     99:             return $lh->maketext(@_);
                    100:         }
                    101:     } else {
                    102:         if (wantarray) {
                    103:             return @_;
                    104:         } else {
                    105:             return $_[0];
                    106:         }
                    107:     }
                    108: }
                    109: 
                    110: sub texthash {
                    111:     my (%hash) = @_;
                    112:     foreach (keys(%hash)) {
                    113:         $hash{$_}=&mt($hash{$_});
                    114:     }
                    115:     return %hash;
                    116: }
                    117: 
                    118: 
                    119: sub skip_if_nonempty {
                    120:     my ($string,$error)=@_;
                    121:     return if (! defined($error));
                    122:     chomp($string);chomp($error);
                    123:     if ($string ne '') {
                    124:         print_and_log("$error\n".&mt('Stopping execution.')."\n");
                    125:         return 1;
                    126:     }
                    127:     return;
                    128: }
                    129: 
                    130: sub writelog {
                    131:     while ($_ = shift) {
                    132:         chomp();
                    133:         print LOG "$_\n";
                    134:     }
                    135: }
                    136: 
                    137: sub print_and_log {
                    138:     while ($_=shift) {
                    139:         chomp();
                    140:         print "$_\n";
                    141:         print LOG "$_\n";
                    142:     }
                    143: }
                    144: 
                    145: sub get_user_selection {
                    146:     my ($defaultrun) = @_;
                    147:     my $do_action = 0;
                    148:     my $choice = <STDIN>;
                    149:     chomp($choice);
                    150:     $choice =~ s/(^\s+|\s+$)//g;
                    151:     my $yes = &mt('y');
                    152:     if ($defaultrun) {
                    153:         if (($choice eq '') || ($choice =~ /^\Q$yes\E/i)) {
                    154:             $do_action = 1;
                    155:         }
                    156:     } else {
                    157:         if ($choice =~ /^\Q$yes\E/i) {
                    158:             $do_action = 1;
                    159:         }
                    160:     }
                    161:     return $do_action;
                    162: }
                    163: 
                    164: sub get_distro {
1.45.2.1  raeburn   165:     my ($distro,$gotprereqs,$updatecmd,$packagecmd,$installnow,$unknown);
1.1       raeburn   166:     $packagecmd = '/bin/rpm -q LONCAPA-prerequisites ';
1.45.2.3  raeburn   167:     if (-e '/etc/oracle-release') {
                    168:         open(IN,'</etc/oracle-release');
                    169:         my $versionstring=<IN>;
                    170:         chomp($versionstring);
                    171:         close(IN);
                    172:         if ($versionstring =~ /^Oracle Linux Server release (\d+)/) {
                    173:             my $version = $1;
                    174:             $distro = 'oracle'.$1;
                    175:             $updatecmd = 'yum install LONCAPA-prerequisites';
                    176:             $installnow = 'yum -y install LONCAPA-prerequisites';
                    177:         }
                    178:     } elsif (-e '/etc/redhat-release') {
1.1       raeburn   179:         open(IN,'</etc/redhat-release');
                    180:         my $versionstring=<IN>;
                    181:         chomp($versionstring);
                    182:         close(IN);
                    183:         if ($versionstring =~ /^Red Hat Linux release ([\d\.]+) /) {
                    184:             my $version = $1;
                    185:             if ($version=~/^7\./) {
                    186:                 $distro='redhat7';
                    187:             } elsif ($version=~/^8\./) {
                    188:                 $distro='redhat8';
                    189:             } elsif ($version=~/^9/) {
                    190:                 $distro='redhat9';
                    191:             }
                    192:         } elsif ($versionstring =~ /Fedora( Core)? release ([\d\.]+) /) {
                    193:             my $version=$2;
                    194:             if ($version - int($version) > .9) {
                    195:                 $distro = 'fedora'.(int($version)+1);
                    196:             } else {
                    197:                 $distro = 'fedora'.int($version);
                    198:             }
                    199:             $updatecmd = 'yum install LONCAPA-prerequisites';
                    200:             $installnow = 'yum -y install LONCAPA-prerequisites';
                    201:         } elsif ($versionstring =~ /Red Hat Enterprise Linux [AE]S release ([\d\.]+) /) {
                    202:             $distro = 'rhes'.$1;
                    203:             $updatecmd = 'up2date -i LONCAPA-prerequisites';
                    204:         } elsif ($versionstring =~ /Red Hat Enterprise Linux Server release (\d+)/) {
                    205:             $distro = 'rhes'.$1;
                    206:             $updatecmd = 'yum install LONCAPA-prerequisites';
                    207:             $installnow = 'yum -y install LONCAPA-prerequisites';
1.45.2.3  raeburn   208:         } elsif ($versionstring =~ /Red Hat Enterprise Linux release (\d+)/) {
                    209:             $distro = 'rhes'.$1;
                    210:             $updatecmd = 'dnf install LONCAPA-prerequisites';
                    211:             $installnow = 'dnf -y install LONCAPA-prerequisites';
1.21      raeburn   212:         } elsif ($versionstring =~ /CentOS(?:| Linux) release (\d+)/) {
1.1       raeburn   213:             $distro = 'centos'.$1;
                    214:             $updatecmd = 'yum install LONCAPA-prerequisites';
                    215:             $installnow = 'yum -y install LONCAPA-prerequisites';
1.22      raeburn   216:         } elsif ($versionstring =~ /Scientific Linux (?:SL )?release ([\d.]+) /) {
1.1       raeburn   217:             my $ver = $1;
                    218:             $ver =~ s/\.\d+$//;
                    219:             $distro = 'scientific'.$ver;
                    220:             $updatecmd = 'yum install LONCAPA-prerequisites';
                    221:             $installnow = 'yum -y install LONCAPA-prerequisites';
                    222:         } else {
                    223:             print &mt('Unable to interpret [_1] to determine system type.',
                    224:                       '/etc/redhat-release')."\n";
1.45.2.1  raeburn   225:             $unknown = 1;
1.1       raeburn   226:         }
                    227:     } elsif (-e '/etc/SuSE-release') {
                    228:         open(IN,'</etc/SuSE-release');
                    229:         my $versionstring=<IN>;
                    230:         chomp($versionstring);
                    231:         close(IN);
                    232:         if ($versionstring =~ /^SUSE LINUX Enterprise Server ([\d\.]+) /i) {
                    233:             $distro='sles'.$1;
                    234:             if ($1 >= 10) {
                    235:                 $updatecmd = 'zypper install LONCAPA-prerequisites';
                    236:             } else {
                    237:                 $updatecmd = 'yast -i LONCAPA-prerequisites';
                    238:             }
                    239:         } elsif ($versionstring =~ /^SuSE Linux ([\d\.]+) /i) {
                    240:             $distro = 'suse'.$1;
1.12      raeburn   241:             $updatecmd = 'yast -i LONCAPA-prerequisites';
1.1       raeburn   242:         } elsif ($versionstring =~ /^openSUSE ([\d\.]+) /i) {
                    243:             $distro = 'suse'.$1;
                    244:             if ($1 >= 10.3 ) {
                    245:                 $updatecmd = 'zypper install LONCAPA-prerequisites';
                    246:             } else {
                    247:                 $updatecmd = 'yast -i LONCAPA-prerequisites';
                    248:             }
                    249:         } else {
                    250:             print &mt('Unable to interpret [_1] to determine system type.',
                    251:                       '/etc/SuSE-release')."\n";
1.45.2.1  raeburn   252:             $unknown = 1;
1.1       raeburn   253:         }
                    254:     } elsif (-e '/etc/issue') {
                    255:         open(IN,'</etc/issue');
                    256:         my $versionstring=<IN>;
                    257:         chomp($versionstring);
                    258:         close(IN);
                    259:         if ($versionstring =~ /^Ubuntu (\d+)\.\d+/i) {
                    260:             $distro = 'ubuntu'.$1;
                    261:             $updatecmd = 'sudo apt-get install loncapa-prerequisites';
                    262:         } elsif ($versionstring =~ /^Debian\s+GNU\/Linux\s+(\d+)\.\d+/i) {
                    263:             $distro = 'debian'.$1;
1.45.2.1  raeburn   264:             $updatecmd = 'apt-get install loncapa-prerequisites';
1.1       raeburn   265:         } elsif (-e '/etc/debian_version') {
                    266:             open(IN,'</etc/debian_version');
                    267:             my $version=<IN>;
                    268:             chomp($version);
                    269:             close(IN);
                    270:             if ($version =~ /^(\d+)\.\d+\.?\d*/) {
                    271:                 $distro='debian'.$1;
1.45.2.1  raeburn   272:                 $updatecmd = 'apt-get install loncapa-prerequisites';
1.1       raeburn   273:             } else {
                    274:                 print &mt('Unable to interpret [_1] to determine system type.',
                    275:                           '/etc/debian_version')."\n";
1.45.2.1  raeburn   276:                 $unknown = 1;
1.1       raeburn   277:             }
1.45.2.1  raeburn   278:         }
                    279:         if ($distro ne '') {
                    280:             $packagecmd = '/usr/bin/dpkg -l loncapa-prerequisites ';
1.1       raeburn   281:         }
                    282:     } elsif (-e '/etc/debian_version') {
                    283:         open(IN,'</etc/debian_version');
                    284:         my $version=<IN>;
                    285:         chomp($version);
                    286:         close(IN);
                    287:         if ($version =~  /^(\d+)\.\d+\.?\d*/) {
                    288:             $distro='debian'.$1;
                    289:             $packagecmd = '/usr/bin/dpkg -l loncapa-prerequisites ';
                    290:             $updatecmd = 'apt-get install loncapa-prerequisites';
                    291:         } else {
                    292:             print &mt('Unable to interpret [_1] to determine system type.',
                    293:                       '/etc/debian_version')."\n";
1.45.2.1  raeburn   294:             $unknown = 1;
                    295:         }
                    296:     }
                    297:     if (($distro eq '') && (!$unknown)) {
                    298:         if (-e '/etc/os-release') {
                    299:             if (open(IN,'<','/etc/os-release')) {
                    300:                 my ($id,$version);
                    301:                 while(<IN>) {
                    302:                     chomp();
                    303:                     if (/^ID="(\w+)"/) {
                    304:                         $id=$1;
                    305:                     } elsif (/^VERSION_ID="([\d\.]+)"/) {
                    306:                         $version=$1;
                    307:                     }
                    308:                 }
                    309:                 close(IN);
                    310:                 if ($id eq 'sles') {
                    311:                     my ($major,$minor) = split(/\./,$version);
                    312:                     if ($major =~ /^\d+$/) {
                    313:                         $distro = $id.$major;
                    314:                         $updatecmd = 'zypper install LONCAPA-prerequisites';
                    315:                     }
                    316:                 }
                    317:             }
                    318:             if ($distro eq '') {
                    319:                 print &mt('Unable to interpret [_1] to determine system type.',
                    320:                           '/etc/os-release')."\n";
                    321:                 $unknown = 1;
                    322:             }
                    323:         } else {
1.45.2.3  raeburn   324:             print &mt('Unknown installation: expecting a debian, ubuntu, suse, sles, redhat, fedora, scientific linux, or oracle linux system.')."\n";
1.1       raeburn   325:         }
                    326:     }
                    327:     return ($distro,$packagecmd,$updatecmd,$installnow);
                    328: }
                    329: 
                    330: sub check_prerequisites {
                    331:     my ($packagecmd,$distro) = @_;
                    332:     my $gotprereqs;
                    333:     if ($packagecmd ne '') {
                    334:         if (open(PIPE,"$packagecmd|")) {
                    335:             if ($distro =~ /^(debian|ubuntu)/) {
                    336:                 my @lines = <PIPE>;
                    337:                 chomp(@lines);
                    338:                 foreach my $line (@lines) {
                    339:                     if ($line =~ /^ii\s+loncapa-prerequisites\s+([\w\.]+)/) {
                    340:                         $gotprereqs = $1;
                    341:                     }
                    342:                 }
                    343:             } else {
                    344:                 my $line = <PIPE>;
                    345:                 chomp($line);
1.8       raeburn   346:                 if ($line =~ /^LONCAPA\-prerequisites\-([\d\-]+)\.(?:[.\w]+)$/) {
1.1       raeburn   347:                     $gotprereqs = $1;
                    348:                 }
                    349:             }
                    350:             close(PIPE);
                    351:         } else {
                    352:             print &mt('Error: could not determine if LONCAPA-prerequisites package is installed')."\n";
                    353:         }
                    354:     }
                    355:     return $gotprereqs;
                    356: }
                    357: 
1.6       raeburn   358: sub check_locale {
                    359:     my ($distro) = @_;
1.45.2.7  raeburn   360:     my ($fh,$langvar,$command,$earlyout);
1.8       raeburn   361:     $langvar = 'LANG';
1.6       raeburn   362:     if ($distro =~ /^(ubuntu|debian)/) {
                    363:         if (!open($fh,"</etc/default/locale")) {
                    364:             print &mt('Failed to open: [_1], default locale not checked.',
                    365:                       '/etc/default/locale');
1.45.2.7  raeburn   366:             $earlyout = 1;
1.6       raeburn   367:         }
1.45.2.1  raeburn   368:     } elsif ($distro =~ /^(suse|sles)(\d+)/) {
                    369:         if (($1 eq 'sles') && ($2 >= 15)) {
                    370:             if (!open($fh,"</etc/locale.conf")) {
                    371:                 print &mt('Failed to open: [_1], default locale not checked.',
                    372:                           '/etc/locale.conf');
1.45.2.7  raeburn   373:                 $earlyout = 1;
1.45.2.1  raeburn   374:             }
                    375:         } else {
                    376:             if (!open($fh,"</etc/sysconfig/language")) {
                    377:                 print &mt('Failed to open: [_1], default locale not checked.',
                    378:                           '/etc/sysconfig/language');
1.45.2.7  raeburn   379:                 $earlyout = 1;
1.45.2.1  raeburn   380:             }
                    381:             $langvar = 'RC_LANG';
1.8       raeburn   382:         }
1.24      raeburn   383:     } elsif ($distro =~ /^fedora(\d+)/) {
                    384:         if ($1 >= 18) {
                    385:             if (!open($fh,"</etc/locale.conf")) {
                    386:                 print &mt('Failed to open: [_1], default locale not checked.',
                    387:                           '/etc/locale.conf');
1.45.2.7  raeburn   388:                 $earlyout = 1;
1.24      raeburn   389:             }
                    390:         } elsif (!open($fh,"</etc/sysconfig/i18n")) {
                    391:             print &mt('Failed to open: [_1], default locale not checked.',
                    392:                       '/etc/sysconfig/i18n');
1.45.2.7  raeburn   393:             $earlyout = 1;
1.24      raeburn   394:         }
1.45.2.3  raeburn   395:     } elsif ($distro =~ /^(?:rhes|centos|scientific|oracle)(\d+)/) {
1.29      raeburn   396:         if ($1 >= 7) {
                    397:             if (!open($fh,"</etc/locale.conf")) {
                    398:                 print &mt('Failed to open: [_1], default locale not checked.',
                    399:                           '/etc/locale.conf');
1.45.2.7  raeburn   400:                 $earlyout = 1;
1.29      raeburn   401:             }
                    402:         } elsif (!open($fh,"</etc/sysconfig/i18n")) {
                    403:             print &mt('Failed to open: [_1], default locale not checked.',
                    404:                       '/etc/sysconfig/i18n');
1.45.2.7  raeburn   405:             $earlyout = 1;
1.29      raeburn   406:         }
1.6       raeburn   407:     } else {
                    408:         if (!open($fh,"</etc/sysconfig/i18n")) {
                    409:             print &mt('Failed to open: [_1], default locale not checked.',
                    410:                       '/etc/sysconfig/i18n');
1.45.2.7  raeburn   411:             $earlyout = 1;
1.6       raeburn   412:         }
                    413:     }
1.45.2.7  raeburn   414:     return if ($earlyout);
1.6       raeburn   415:     my @data = <$fh>;
                    416:     chomp(@data);
                    417:     foreach my $item (@data) {
1.25      raeburn   418:         if ($item =~ /^\Q$langvar\E=\"?([^\"]*)\"?/) {
1.6       raeburn   419:             my $default = $1;
                    420:             if ($default ne 'en_US.UTF-8') {
                    421:                 if ($distro =~ /^debian/) {
1.25      raeburn   422:                     $command = 'locale-gen en_US.UTF-8'."\n".
                    423:                                'update-locale LANG=en_US.UTF-8';
1.6       raeburn   424:                 } elsif ($distro =~ /^ubuntu/) {
1.25      raeburn   425:                     $command = 'sudo locale-gen en_US.UTF-8'."\n".
                    426:                                'sudo update-locale LANG=en_US.UTF-8';
1.7       raeburn   427:                 } elsif ($distro =~ /^(suse|sles)/) {
1.45.2.3  raeburn   428:                     $command = 'yast language';
                    429:                 } elsif (-e '/usr/bin/system-config-language') {
1.6       raeburn   430:                     $command = 'system-config-language';
1.45.2.3  raeburn   431:                 } elsif (-e '/usr/bin/localectl') {
1.45.2.4  raeburn   432:                     $command = '/usr/bin/localectl set-locale LANG=en_US.UTF-8';
1.45.2.3  raeburn   433:                 } else {
                    434:                     $command = 'No standard command found';
1.6       raeburn   435:                 }
                    436:             }
                    437:             last;
                    438:         }
                    439:     }
                    440:     close($fh);
                    441:     return $command;
                    442: }
                    443: 
1.1       raeburn   444: sub check_required {
                    445:     my ($instdir,$dsn) = @_;
                    446:     my ($distro,$packagecmd,$updatecmd,$installnow) = &get_distro();
                    447:     if ($distro eq '') {
                    448:         return;
                    449:     }
                    450:     my $gotprereqs = &check_prerequisites($packagecmd,$distro); 
                    451:     if ($gotprereqs eq '') {
1.22      raeburn   452:         return ($distro,$gotprereqs,'',$packagecmd,$updatecmd);
1.6       raeburn   453:     }
                    454:     my $localecmd = &check_locale($distro);
                    455:     unless ($localecmd eq '') {
                    456:         return ($distro,$gotprereqs,$localecmd);
1.1       raeburn   457:     }
1.45.2.9! raeburn   458:     my ($mysqlon,$mysqlsetup,$mysqlrestart,$dbh,$has_pass,$mysql_unix_socket,$has_lcdb,
        !           459:         %recommended,$downloadstatus,$filetouse,$production,$testing,$apachefw,
        !           460:         $tostop,$uses_systemctl);
1.1       raeburn   461:     my $wwwuid = &uid_of_www();
                    462:     my $wwwgid = getgrnam('www');
                    463:     if (($wwwuid eq '') || ($wwwgid eq '')) {
                    464:         $recommended{'wwwuser'} = 1;
                    465:     }
                    466:     unless( -e "/usr/local/sbin/pwauth") {
                    467:         $recommended{'pwauth'} = 1;
                    468:     }
                    469:     $mysqlon = &check_mysql_running($distro);
                    470:     if ($mysqlon) {
                    471:         my $mysql_has_wwwuser = &check_mysql_wwwuser();
1.45.2.9! raeburn   472:         ($mysqlsetup,$has_pass,$dbh,$mysql_has_wwwuser,$mysql_unix_socket) = 
1.34      raeburn   473:             &check_mysql_setup($instdir,$dsn,$distro,$mysql_has_wwwuser);
                    474:         if ($mysqlsetup eq 'needsrestart') {
                    475:             $mysqlrestart = '';
                    476:             if ($distro eq 'ubuntu') {
                    477:                 $mysqlrestart = 'sudo '; 
                    478:             }
                    479:             $mysqlrestart .= 'service mysql restart';
                    480:             return ($distro,$gotprereqs,$localecmd,$packagecmd,$updatecmd,$installnow,$mysqlrestart);
1.1       raeburn   481:         } else {
1.34      raeburn   482:             if ($mysqlsetup eq 'noroot') {
1.1       raeburn   483:                 $recommended{'mysqlperms'} = 1;
1.34      raeburn   484:             } else {
                    485:                 unless ($mysql_has_wwwuser) {
                    486:                     $recommended{'mysqlperms'} = 1;
                    487:                 }
                    488:             }
                    489:             if ($dbh) {
                    490:                 $has_lcdb = &check_loncapa_mysqldb($dbh);
                    491:             }
                    492:             unless ($has_lcdb) {
                    493:                 $recommended{'mysql'} = 1;
1.1       raeburn   494:             }
                    495:         }
                    496:     }
1.5       raeburn   497:     ($recommended{'firewall'},$apachefw) = &chkfirewall($distro);
1.35      raeburn   498:     ($recommended{'runlevels'},$tostop,$uses_systemctl) = &chkconfig($distro,$instdir);
1.1       raeburn   499:     $recommended{'apache'} = &chkapache($distro,$instdir);
                    500:     $recommended{'stopsrvcs'} = &chksrvcs($distro,$tostop);
                    501:     ($recommended{'download'},$downloadstatus,$filetouse,$production,$testing) 
                    502:         = &need_download();
1.6       raeburn   503:     return ($distro,$gotprereqs,$localecmd,$packagecmd,$updatecmd,$installnow,
1.45.2.9! raeburn   504:             $mysqlrestart,\%recommended,$dbh,$has_pass,$mysql_unix_socket,
        !           505:             $has_lcdb,$downloadstatus,$filetouse,$production,$testing,$apachefw,
        !           506:             $uses_systemctl);
1.1       raeburn   507: }
                    508: 
                    509: sub check_mysql_running {
                    510:     my ($distro) = @_;
1.23      raeburn   511:     my $use_systemctl;
1.1       raeburn   512:     my $mysqldaemon ='mysqld';
                    513:     if ($distro =~ /^(suse|sles|debian|ubuntu)/) {
                    514:         $mysqldaemon = 'mysql';
                    515:     }
1.6       raeburn   516:     my $process = 'mysqld_safe';
                    517:     my $proc_owner = 'root';
                    518:     if ($distro =~ /^ubuntu(\w+)/) {
                    519:         if ($1 >= 10) {
                    520:             $process = 'mysqld';
                    521:             $proc_owner = 'mysql';
                    522:         }
1.35      raeburn   523:     } elsif ($distro =~ /^fedora(\d+)/) {
1.23      raeburn   524:         if ($1 >= 16) {
                    525:             $process = 'mysqld';
                    526:             $proc_owner = 'mysql';
                    527:             $use_systemctl = 1;
                    528:         }
1.39      raeburn   529:         if ($1 >= 19) {
1.37      raeburn   530:             $mysqldaemon ='mariadb';
                    531:         }
1.45.2.3  raeburn   532:     } elsif ($distro =~ /^(?:centos|rhes|scientific|oracle)(\d+)/) {
1.29      raeburn   533:         if ($1 >= 7) {
                    534:             $mysqldaemon ='mariadb';
                    535:             $process = 'mysqld';
                    536:             $proc_owner = 'mysql';
                    537:             $use_systemctl = 1;
                    538:         }
1.35      raeburn   539:     } elsif ($distro =~ /^sles(\d+)/) {
                    540:         if ($1 >= 12) {
                    541:             $use_systemctl = 1;
1.42      raeburn   542:             $proc_owner = 'mysql';
                    543:             $process = 'mysqld';
1.35      raeburn   544:         }
1.45.2.1  raeburn   545:         if ($1 >= 15) {
                    546:             $mysqldaemon ='mariadb';
                    547:         }
1.35      raeburn   548:     } elsif ($distro =~ /^suse(\d+)/) {
                    549:         if ($1 >= 13) {
                    550:             $use_systemctl = 1;
                    551:         }
1.29      raeburn   552:     }
1.35      raeburn   553:     if (open(PIPE,"ps -ef |grep $process |grep ^$proc_owner |grep -v grep 2>&1 |")) {
1.1       raeburn   554:         my $status = <PIPE>;
                    555:         close(PIPE);
                    556:         chomp($status);
1.6       raeburn   557:         if ($status =~ /^\Q$proc_owner\E\s+\d+\s+/) {
1.1       raeburn   558:             print_and_log(&mt('MySQL is running.')."\n");
                    559:             return 1;
                    560:         } else {
1.23      raeburn   561:             if ($use_systemctl) {
                    562:                 system("/bin/systemctl start $mysqldaemon.service >/dev/null 2>&1 ");
                    563:             } else {
                    564:                 system("/etc/init.d/$mysqldaemon start >/dev/null 2>&1 ");
                    565:             }
1.1       raeburn   566:             print_and_log(&mt('Waiting for MySQL to start.')."\n");
                    567:             sleep 5;
1.12      raeburn   568:             if (open(PIPE,"ps -ef |grep $process |grep -v grep 2>&1 |")) {
                    569:                 $status = <PIPE>;
1.1       raeburn   570:                 close(PIPE);
                    571:                 chomp($status);
1.12      raeburn   572:                 if ($status =~ /^\Q$proc_owner\E\s+\d+\s+/) {
1.1       raeburn   573:                     print_and_log(&mt('MySQL is running.')."\n");
                    574:                     return 1;
                    575:                 } else {
1.12      raeburn   576:                     print_and_log(&mt('Still waiting for MySQL to start.')."\n");
                    577:                     sleep 5;
                    578:                     if (open(PIPE,"ps -ef |grep $process |grep -v grep 2>&1 |")) {
                    579:                         $status = <PIPE>;
                    580:                         close(PIPE);
                    581:                         chomp($status);
                    582:                         if ($status =~ /^\Q$proc_owner\E\s+\d+\s+/) {
                    583:                             print_and_log(&mt('MySQL is running.')."\n");
                    584:                             return 1;
                    585:                         } else {
                    586:                             print_and_log(&mt('Given up waiting for MySQL to start.')."\n"); 
                    587:                         }
                    588:                     }
1.1       raeburn   589:                 }
                    590:             }
                    591:         }
                    592:     } else {
                    593:         print &mt('Could not determine if MySQL is running.')."\n";
                    594:     }
                    595:     return;
                    596: }
                    597: 
                    598: sub chkconfig {
1.8       raeburn   599:     my ($distro,$instdir) = @_;
1.23      raeburn   600:     my (%needfix,%tostop,%uses_systemctl);
1.1       raeburn   601:     my $checker_bin = '/sbin/chkconfig';
1.23      raeburn   602:     my $sysctl_bin = '/bin/systemctl';
1.6       raeburn   603:     my %daemon = (
                    604:                   mysql     => 'mysqld',
                    605:                   apache    => 'httpd',
                    606:                   cups      => 'cups',
                    607:                   ntp       => 'ntpd',
                    608:                   memcached => 'memcached',
                    609:     );
1.1       raeburn   610:     my @runlevels = qw/3 4 5/;
                    611:     my @norunlevels = qw/0 1 6/;
                    612:     if ($distro =~ /^(suse|sles)/) {
                    613:         @runlevels = qw/3 5/;
                    614:         @norunlevels = qw/0 2 1 6/;
1.6       raeburn   615:         $daemon{'mysql'} = 'mysql';
                    616:         $daemon{'apache'} = 'apache2';
1.8       raeburn   617:         $daemon{'ntp'}    = 'ntp';
1.1       raeburn   618:         if ($distro =~ /^(suse|sles)9/) {
1.6       raeburn   619:             $daemon{'apache'} = 'apache';
1.1       raeburn   620:         }
1.35      raeburn   621:         if ($distro =~ /^(suse|sles)([\d\.]+)/) {
                    622:             my $name = $1;
                    623:             my $num = $2;
                    624:             if ($num > 11) {
1.26      raeburn   625:                 $uses_systemctl{'apache'} = 1;
1.35      raeburn   626:                 if (($name eq 'sles') || ($name eq 'suse' && $num >= 13.2)) {
                    627:                     $uses_systemctl{'mysql'} = 1;
                    628:                     $uses_systemctl{'ntp'} = 1;
                    629:                     $uses_systemctl{'cups'} = 1;
                    630:                     $uses_systemctl{'memcached'} = 1;
1.45.2.1  raeburn   631:                     if (($name eq 'sles') && ($num >= 15)) {
                    632:                         $daemon{'ntp'} = 'chronyd';
                    633:                         $daemon{'mysql'} = 'mariadb';
                    634:                     } else {
                    635:                         $daemon{'ntp'} = 'ntpd';
                    636:                     }
1.35      raeburn   637:                 }
1.26      raeburn   638:             }
                    639:         }
1.6       raeburn   640:     } elsif ($distro =~ /^(?:debian|ubuntu)(\d+)/) {
                    641:         my $version = $1;
1.1       raeburn   642:         @runlevels = qw/2 3 4 5/;
                    643:         @norunlevels = qw/0 1 6/;
1.44      raeburn   644:         if (($distro =~ /^ubuntu/) && ($version <= 16)) {
                    645:             $checker_bin = '/usr/sbin/sysv-rc-conf';
                    646:         } else {
                    647:             $uses_systemctl{'ntp'} = 1;
                    648:             $uses_systemctl{'mysql'} = 1;
                    649:             $uses_systemctl{'apache'} = 1;
                    650:             $uses_systemctl{'memcached'} = 1;
                    651:             $uses_systemctl{'cups'} = 1;
                    652:         }
1.6       raeburn   653:         $daemon{'mysql'}  = 'mysql';
                    654:         $daemon{'apache'} = 'apache2';
                    655:         $daemon{'ntp'}    = 'ntp';
                    656:         if (($distro =~ /^ubuntu/) && ($version <= 8)) {
                    657:             $daemon{'cups'} = 'cupsys';
                    658:         }
1.45.2.6  raeburn   659:         if (($distro =~ /^ubuntu/) && ($version >= 18)) {
                    660:             $daemon{'ntp'}    = 'chrony';
                    661:         }
1.26      raeburn   662:     } elsif ($distro =~ /^fedora(\d+)/) {
1.23      raeburn   663:         my $version = $1;
                    664:         if ($version >= 15) {
                    665:             $uses_systemctl{'ntp'} = 1;
                    666:         }
                    667:         if ($version >= 16) {
                    668:             $uses_systemctl{'mysql'} = 1;
                    669:             $uses_systemctl{'apache'} = 1;
1.35      raeburn   670:             $uses_systemctl{'memcached'} = 1;
                    671:             $uses_systemctl{'cups'} = 1;
1.23      raeburn   672:         }
1.39      raeburn   673:         if ($version >= 19) {
1.37      raeburn   674:             $daemon{'mysql'} = 'mariadb';
                    675:         }
1.45.2.5  raeburn   676:         if ($version >= 26) {
                    677:             $daemon{'ntp'} = 'chronyd';
                    678:         }
1.45.2.3  raeburn   679:     } elsif ($distro =~ /^(?:centos|rhes|scientific|oracle)(\d+)/) {
1.29      raeburn   680:         my $version = $1;
                    681:         if ($version >= 7) {
                    682:             $uses_systemctl{'ntp'} = 1;
                    683:             $uses_systemctl{'mysql'} = 1;
                    684:             $uses_systemctl{'apache'} = 1;
1.35      raeburn   685:             $uses_systemctl{'memcached'} = 1;
                    686:             $uses_systemctl{'cups'} = 1;
1.30      raeburn   687:             $daemon{'mysql'} = 'mariadb';
1.29      raeburn   688:         }
1.45.2.3  raeburn   689:         if (($version >= 8) || ($distro eq 'oracle7')) {
                    690:             $daemon{'ntp'} = 'chronyd';
                    691:         }
1.1       raeburn   692:     }
1.23      raeburn   693:     my $nocheck;
1.1       raeburn   694:     if (! -x $checker_bin) {
1.23      raeburn   695:         if ($uses_systemctl{'mysql'} && $uses_systemctl{'apache'}) {
                    696:             if (! -x $sysctl_bin) {
                    697:                 $nocheck = 1;       
                    698:             }
                    699:         } else {
                    700:             $nocheck = 1;
                    701:         }
                    702:     }
                    703:     if ($nocheck) {
1.6       raeburn   704:         print &mt('Could not check runlevel status for MySQL or Apache')."\n";
1.1       raeburn   705:         return;
                    706:     }
                    707:     my $rlstr = join('',@runlevels);
                    708:     my $nrlstr = join('',@norunlevels);
1.23      raeburn   709: 
1.6       raeburn   710:     foreach my $type ('apache','mysql','ntp','cups','memcached') {
                    711:         my $service = $daemon{$type};
1.23      raeburn   712:         if ($uses_systemctl{$type}) {
1.35      raeburn   713:             if (($type eq 'memcached') || ($type eq 'cups')) {
                    714:                 if (-l "/etc/systemd/system/multi-user.target.wants/$service.service") {
                    715:                     $tostop{$type} = 1;
                    716:                 }
                    717:             } else {
                    718:                 if (!-l "/etc/systemd/system/multi-user.target.wants/$service.service") {
                    719:                     $needfix{$type} = "systemctl enable $service.service";
                    720:                 }
1.23      raeburn   721:             }
                    722:         } else {
                    723:             my $command = $checker_bin.' --list '.$service.' 2>/dev/null';
1.35      raeburn   724:             if ($type eq 'cups') {
1.23      raeburn   725:                 if ($distro =~ /^(?:debian|ubuntu)(\d+)/) {
                    726:                     my $version = $1;
                    727:                     if (($distro =~ /^ubuntu/) && ($version <= 8)) {
                    728:                         $command = $checker_bin.' --list cupsys 2>/dev/null';
1.19      raeburn   729:                     }
                    730:                 }
                    731:             }
1.23      raeburn   732:             my $results = `$command`;
                    733:             my $tofix;
                    734:             if ($results eq '') {
                    735:                 if (($type eq 'apache') || ($type eq 'mysql') || ($type eq 'ntp')) {
                    736:                     if ($distro  =~ /^(debian|ubuntu)/) {
                    737:                         $tofix = "update-rc.d $type defaults";
                    738:                     } else {
                    739:                         $tofix = "$checker_bin --add $service\n";
                    740:                     }
1.6       raeburn   741:                 }
1.23      raeburn   742:             } else {
                    743:                 my %curr_runlevels;
                    744:                 for (my $rl=0; $rl<=6; $rl++) {
                    745:                     if ($results =~ /$rl:on/) { $curr_runlevels{$rl}++; }
                    746:                 }
                    747:                 if (($type eq 'apache') || ($type eq 'mysql') || ($type eq 'ntp')) {
                    748:                     my $warning;
                    749:                     foreach my $rl (@runlevels) {
                    750:                         if (!exists($curr_runlevels{$rl})) {
                    751:                             $warning = 1;
                    752:                         }
                    753:                     }
                    754:                     if ($warning) {
                    755:                         $tofix = "$checker_bin --level $rlstr $service on\n";
                    756:                     }
                    757:                 } elsif (keys(%curr_runlevels) > 0) {
                    758:                     $tostop{$type} = 1;
1.1       raeburn   759:                 }
                    760:             }
1.23      raeburn   761:             if ($tofix) {
                    762:                 $needfix{$type} = $tofix;
1.1       raeburn   763:             }
1.5       raeburn   764:         }
1.1       raeburn   765:     }
                    766:     if ($distro =~ /^(suse|sles)([\d\.]+)$/) {
                    767:         my $name = $1;
                    768:         my $version = $2;
                    769:         my ($major,$minor);
                    770:         if ($name eq 'suse') {
                    771:             ($major,$minor) = split(/\./,$version);
                    772:         } else {
                    773:             $major = $version;
                    774:         }
1.45.2.1  raeburn   775:         if (($major > 10) && ($major <= 13)) {
1.8       raeburn   776:             if (&check_SuSEfirewall2_setup($instdir)) {
                    777:                 $needfix{'insserv'} = 1;
                    778:             }
1.1       raeburn   779:         }
                    780:     }
1.35      raeburn   781:     return (\%needfix,\%tostop,\%uses_systemctl);
1.1       raeburn   782: }
                    783: 
1.45.2.1  raeburn   784: sub uses_firewalld {
                    785:     my ($distro) = @_;
1.45.2.3  raeburn   786:     my ($inuse,$checkfirewalld,$zone);
1.45.2.1  raeburn   787:     if ($distro =~ /^(suse|sles)([\d\.]+)$/) {
                    788:         if (($1 eq 'sles') && ($2 >= 15)) {
                    789:             $checkfirewalld = 1;
                    790:         }
                    791:     } elsif ($distro =~ /^fedora(\d+)$/) {
                    792:         if ($1 >= 18) {
                    793:             $checkfirewalld = 1;
                    794:         }
1.45.2.3  raeburn   795:     } elsif ($distro =~ /^(?:centos|rhes|scientific|oracle)(\d+)/) {
1.45.2.1  raeburn   796:         if ($1 >= 7) {
                    797:             $checkfirewalld = 1;
                    798:         }
                    799:     }
                    800:     if ($checkfirewalld) {
                    801:         my ($loaded,$active);
                    802:         if (open(PIPE,"systemctl status firewalld |")) {
                    803:             while (<PIPE>) {
                    804:                 chomp();
                    805:                 if (/^\s*Loaded:\s+(\w+)/) {
                    806:                     $loaded = $1;
                    807:                 }
                    808:                 if (/^\s*Active\s+(\w+)/) {
                    809:                     $active = $1;
                    810:                 }
                    811:             }
                    812:             close(PIPE);
                    813:         }
                    814:         if (($loaded eq 'loaded') || ($active eq 'active')) {
                    815:             $inuse = 1;
1.45.2.3  raeburn   816:             my $cmd = 'firewall-cmd --get-default-zone';
                    817:             if (open(PIPE,"$cmd |")) {
                    818:                 my $result = <PIPE>;
                    819:                 chomp($result);
                    820:                 close(PIPE);
                    821:                 if ($result =~ /^\w+$/) {
                    822:                     $zone = $result;
                    823:                 }
                    824:             }
1.45.2.1  raeburn   825:         }
                    826:     }
1.45.2.3  raeburn   827:     return ($inuse,$zone);
1.45.2.1  raeburn   828: }
                    829: 
1.1       raeburn   830: sub chkfirewall {
1.5       raeburn   831:     my ($distro) = @_;
1.1       raeburn   832:     my $configfirewall = 1;
                    833:     my %ports = (
                    834:                     http  =>  80,
                    835:                     https => 443,
                    836:                 );
1.5       raeburn   837:     my %activefw;
1.45.2.3  raeburn   838:     my ($firewalld,$zone) = &uses_firewalld($distro);
                    839:     if ($firewalld) {
                    840:         my %current;
                    841:         if (open(PIPE,'firewall-cmd --permanent --zone='.$zone.' --list-services |')) {
                    842:             my $svc = <PIPE>;
                    843:             close(PIPE);
                    844:             chomp($svc);
                    845:             map { $current{$_} = 1; } (split(/\s+/,$svc));
                    846:         }
                    847:         if ($current{'http'} && $current{'https'}) {
                    848:             $configfirewall = 0;
                    849:         }
                    850:     } else {
                    851:         if (&firewall_is_active()) {
1.45.2.1  raeburn   852:             my $iptables = &get_pathto_iptables();
                    853:             if ($iptables eq '') {
                    854:                 print &mt('Firewall not checked as path to iptables not determined.')."\n";
                    855:             } else {
                    856:                 my @fwchains = &get_fw_chains($iptables,$distro);
                    857:                 if (@fwchains) {
                    858:                     foreach my $service ('http','https') {
                    859:                         foreach my $fwchain (@fwchains) {
                    860:                             if (&firewall_is_port_open($iptables,$fwchain,$ports{$service})) {
                    861:                                 $activefw{$service} = 1;
                    862:                                 last;
                    863:                             }
1.1       raeburn   864:                         }
                    865:                     }
1.45.2.1  raeburn   866:                     if ($activefw{'http'}) {
                    867:                         $configfirewall = 0;
                    868:                     }
                    869:                 } else {
                    870:                     print &mt('Firewall not checked as iptables Chains not identified.')."\n";
1.1       raeburn   871:                 }
                    872:             }
1.45.2.3  raeburn   873:         } else {
                    874:             print &mt('Firewall not enabled.')."\n";
1.1       raeburn   875:         }
                    876:     }
1.5       raeburn   877:     return ($configfirewall,\%activefw);
1.1       raeburn   878: }
                    879: 
                    880: sub chkapache {
                    881:     my ($distro,$instdir) = @_;
                    882:     my $fixapache = 1;
1.28      raeburn   883:     if ($distro =~ /^(debian|ubuntu)(\d+)$/) {
                    884:         my $distname = $1;
                    885:         my $version = $2;
1.33      raeburn   886:         my ($stdconf,$stdsite);
                    887:         if (($distname eq 'ubuntu') && ($version > 12)) {
                    888:             $stdconf = "$instdir/debian-ubuntu/ubuntu14/loncapa_conf";
                    889:             $stdsite = "$instdir/debian-ubuntu/ubuntu14/loncapa_sites";
                    890:         } else {
                    891:             $stdconf = "$instdir/debian-ubuntu/loncapa"; 
                    892:         }
                    893:         if (!-e $stdconf) {
1.1       raeburn   894:             $fixapache = 0;
                    895:             print &mt('Warning: No LON-CAPA Apache configuration file found for installation check.')."\n"; 
1.28      raeburn   896:         } else {
1.33      raeburn   897:             my ($configfile,$sitefile);
1.28      raeburn   898:             if (($distname eq 'ubuntu') && ($version > 12)) {
1.33      raeburn   899:                 $sitefile = '/etc/apache2/sites-available/loncapa';
1.28      raeburn   900:                 $configfile = "/etc/apache2/conf-available/loncapa";
1.33      raeburn   901:             } else {
                    902:                 $configfile = "/etc/apache2/sites-available/loncapa";
1.28      raeburn   903:             }
1.33      raeburn   904:             if (($configfile ne '') && (-e $configfile) && (-e $stdconf))  {
                    905:                 if (open(PIPE, "diff --brief $stdconf $configfile |")) {
1.28      raeburn   906:                     my $diffres = <PIPE>;
                    907:                     close(PIPE);
                    908:                     chomp($diffres);
                    909:                     unless ($diffres) {
                    910:                         $fixapache = 0;
                    911:                     }
1.1       raeburn   912:                 }
                    913:             }
1.33      raeburn   914:             if ((!$fixapache) && ($distname eq 'ubuntu') && ($version > 12)) {
                    915:                 if (($sitefile ne '') && (-e $sitefile) && (-e $stdsite)) {
                    916:                     if (open(PIPE, "diff --brief $stdsite $sitefile |")) {
                    917:                         my $diffres = <PIPE>;
                    918:                         close(PIPE);
                    919:                         chomp($diffres);
                    920:                         unless ($diffres) {
                    921:                             $fixapache = 0;
                    922:                         }
                    923:                     }
                    924:                 }
                    925:             }
1.1       raeburn   926:         }
1.6       raeburn   927:         if (!$fixapache) {
                    928:             foreach my $module ('headers.load','expires.load') {
                    929:                 unless (-l "/etc/apache2/mods-enabled/$module") {
                    930:                     $fixapache = 1;
                    931:                 }
                    932:             }
                    933:         }
1.45.2.7  raeburn   934:         if ((!$fixapache) && ($distname eq 'ubuntu')) {
                    935:             my $sitestatus = "/etc/apache2/mods-available/status.conf";
                    936:             my $stdstatus = "$instdir/debian-ubuntu/status.conf";
                    937:             if ((-e $stdstatus) && (-e $sitestatus)) {
                    938:                 if (open(PIPE, "diff --brief $stdstatus $sitestatus |")) {
                    939:                     my $diffres = <PIPE>;
                    940:                     close(PIPE);
                    941:                     chomp($diffres);
                    942:                     if ($diffres) {
                    943:                         $fixapache = 1;
                    944:                     }
                    945:                 }
                    946:             }
                    947:         }
1.45.2.1  raeburn   948:     } elsif ($distro =~ /^(suse|sles)([\d\.]+)$/) {
                    949:         my ($name,$version) = ($1,$2);
1.1       raeburn   950:         my $apache = 'apache';
1.45.2.1  raeburn   951:         my $conf_file = "$instdir/sles-suse/default-server.conf";
                    952:         if ($version >= 10) {
1.8       raeburn   953:             $apache = 'apache2';
1.1       raeburn   954:         }
1.45.2.1  raeburn   955:         if (($name eq 'sles') && ($version >= 12)) {
                    956:             $conf_file = "$instdir/sles-suse/apache2.4/default-server.conf";
                    957:         }
                    958:         if (!-e $conf_file) {
1.1       raeburn   959:             $fixapache = 0;
                    960:             print &mt('Warning: No LON-CAPA Apache configuration file found for installation check.')."\n";
1.45.2.1  raeburn   961:         } elsif (-e "/etc/$apache/default-server.conf") {
                    962:             if (open(PIPE, "diff --brief $conf_file /etc/$apache/default-server.conf |")) {
1.9       raeburn   963:                 my $diffres = <PIPE>;
                    964:                 close(PIPE);
                    965:                 chomp($diffres);
                    966:                 unless ($diffres) {
                    967:                     $fixapache = 0;
                    968:                 }
                    969:             }
                    970:         }
                    971:     } elsif ($distro eq 'rhes4') {
                    972:         if (!-e "$instdir/rhes4/httpd.conf") {
                    973:             $fixapache = 0;
                    974:             print &mt('Warning: No LON-CAPA Apache configuration file found for installation check.')."\n";
                    975:         } elsif ((-e "/etc/httpd/conf/httpd.conf") && (-e "$instdir/rhes4/httpd.conf")) {
                    976:             if (open(PIPE, "diff --brief $instdir/rhes4/httpd.conf /etc/httpd/conf/httpd.conf |")) {
1.1       raeburn   977:                 my $diffres = <PIPE>;
                    978:                 close(PIPE);
                    979:                 chomp($diffres);
                    980:                 unless ($diffres) {
                    981:                     $fixapache = 0;
                    982:                 }
                    983:             }
                    984:         }
                    985:     } else {
1.14      raeburn   986:         my $configfile = 'httpd.conf';
1.45.2.5  raeburn   987:         my $mpmfile = 'mpm.conf';
1.45.2.3  raeburn   988:         if ($distro =~ /^(?:centos|rhes|scientific|oracle)(\d+)$/) {
1.29      raeburn   989:             if ($1 >= 7) {
                    990:                 $configfile = 'apache2.4/httpd.conf';
                    991:             } elsif ($1 > 5) {
1.14      raeburn   992:                 $configfile = 'new/httpd.conf';
                    993:             }
                    994:         } elsif ($distro =~ /^fedora(\d+)$/) {
1.29      raeburn   995:             if ($1 > 17) {
1.45.2.5  raeburn   996:                 $configfile = 'apache2.4/httpd.conf';
1.29      raeburn   997:             } elsif ($1 > 10) {
1.15      raeburn   998:                 $configfile = 'new/httpd.conf';
1.14      raeburn   999:             }
                   1000:         }
                   1001:         if (!-e "$instdir/centos-rhes-fedora-sl/$configfile") {
1.1       raeburn  1002:             $fixapache = 0;
                   1003:             print &mt('Warning: No LON-CAPA Apache configuration file found for installation check.')."\n";
1.14      raeburn  1004:         } elsif ((-e "/etc/httpd/conf/httpd.conf") && (-e "$instdir/centos-rhes-fedora-sl/$configfile")) {
                   1005:             if (open(PIPE, "diff --brief $instdir/centos-rhes-fedora-sl/$configfile /etc/httpd/conf/httpd.conf |")) {
1.1       raeburn  1006:                 my $diffres = <PIPE>;
                   1007:                 close(PIPE);
                   1008:                 chomp($diffres);
                   1009:                 unless ($diffres) {
                   1010:                     $fixapache = 0;
                   1011:                 }
                   1012:             }
                   1013:         }
1.45.2.5  raeburn  1014:         if (-e "/etc/httpd/conf.modules.d/00-mpm.conf") {
                   1015:             if (!-e "$instdir/centos-rhes-fedora-sl/$mpmfile") {
                   1016:                 print &mt('Warning: No LON-CAPA Apache MPM configuration file found for installation check.')."\n";
                   1017:             } elsif ((-e "/etc/httpd/conf.modules.d/00-mpm.conf") && (-e "$instdir/centos-rhes-fedora-sl/$mpmfile")) {
                   1018:                 if (open(PIPE, "diff --brief $instdir/centos-rhes-fedora-sl/$mpmfile /etc/httpd/conf.modules.d/00-mpm.conf |")) {
                   1019:                     my $diffres = <PIPE>;
                   1020:                     close(PIPE);
                   1021:                     chomp($diffres);
                   1022:                     if ($diffres) {
                   1023:                         $fixapache = 1;
                   1024:                     }
                   1025:                 }
                   1026:             }
                   1027:         }
1.1       raeburn  1028:     }
                   1029:     return $fixapache;
                   1030: }
                   1031: 
                   1032: sub chksrvcs {
                   1033:     my ($distro,$tostop) = @_;
                   1034:     my %stopsrvcs;
                   1035:     if (ref($tostop) eq 'HASH') {
                   1036:         %stopsrvcs = %{$tostop};
                   1037:     }
1.6       raeburn  1038:     foreach my $service ('cups','memcached') {
1.1       raeburn  1039:         next if (exists($stopsrvcs{$service}));
                   1040:         my $daemon = $service;
                   1041:         if ($service eq 'cups') {
                   1042:             $daemon = 'cupsd';
                   1043:         }
                   1044:         my $cmd = "ps -ef |grep '$daemon' |grep -v grep";
                   1045:         if (open(PIPE,'-|',$cmd)) {
                   1046:             my $daemonrunning = <PIPE>;
                   1047:             chomp($daemonrunning);
                   1048:             close(PIPE);
                   1049:             if ($daemonrunning) {
1.12      raeburn  1050:                 if ($service eq 'memcached') {
1.16      raeburn  1051:                     my $cmd = '/usr/bin/memcached';
                   1052:                     if ($distro =~ /^(suse|sles)/) {
                   1053:                         $cmd = '/usr/sbin/memcached';
                   1054:                     }
1.12      raeburn  1055:                     unless ($daemonrunning =~ m{^www[^/]+\Q$cmd -m 400 -v\E$}) {
1.10      raeburn  1056:                         $stopsrvcs{$service} = 1;
                   1057:                     }
                   1058:                 } else {
                   1059:                     $stopsrvcs{$service} = 1;
                   1060:                 }
1.1       raeburn  1061:             }
                   1062:         }
1.10      raeburn  1063:     }
1.1       raeburn  1064:     return \%stopsrvcs;
                   1065: }
                   1066: 
                   1067: sub need_download {
                   1068:     my $needs_download = 1;
                   1069:     my ($production,$testing,$stdsizes) = &download_versionslist();
                   1070:     my ($rootdir,$localcurrent,$localtesting,%tarball,%localsize,%bymodtime,
                   1071:         %bysize,$filetouse,$downloadstatus);
                   1072:     $rootdir = '/root';
                   1073:     if (opendir(my $dir,"$rootdir")) {
                   1074:         my (@lcdownloads,$version);
                   1075:         foreach my $file (readdir($dir)) {
                   1076:             if ($file =~ /^loncapa\-([\w\-.]+)\.tar\.gz$/) {
                   1077:                 $version = $1;
                   1078:             } else {
                   1079:                 next;
                   1080:             }
                   1081:             if (ref($stdsizes) eq 'HASH') {
                   1082:                 if ($version eq 'current') {
                   1083:                     my @stats = stat("$rootdir/$file");
                   1084:                     $localcurrent = $stats[7];
1.4       raeburn  1085:                     if ($localcurrent == $stdsizes->{$production}) {
1.1       raeburn  1086:                         $needs_download = 0;
                   1087:                         $filetouse = $file;
                   1088:                     }
                   1089:                 } elsif ($version eq 'testing') {
                   1090:                     my @stats = stat("$rootdir/$file");
                   1091:                     $localtesting = $stats[7];
1.4       raeburn  1092:                     if ($localtesting == $stdsizes->{$testing}) {
1.1       raeburn  1093:                         $needs_download = 0;
                   1094:                         $filetouse = $file;
                   1095:                     }
                   1096:                 }
                   1097:             }
                   1098:             $tarball{$version} = $file;
                   1099:             push(@lcdownloads,$version);
                   1100:         }
                   1101:         if ($needs_download) {
                   1102:             if (@lcdownloads > 0) {
                   1103:                 foreach my $version (@lcdownloads) {
                   1104:                     my @stats = stat("$rootdir/$tarball{$version}");
                   1105:                     my $mtime = $stats[9];
                   1106:                     $localsize{$version} = $stats[7];
                   1107:                     if ($mtime) {
                   1108:                         push(@{$bymodtime{$mtime}},$version);
                   1109:                     }
                   1110:                     if ($localsize{$version}) {
                   1111:                         push(@{$bysize{$localsize{$version}}},$version);
                   1112:                     }
                   1113:                 }
                   1114:                 if ($testing) {
                   1115:                     if (exists($localsize{$testing})) {
                   1116:                         if ($stdsizes->{$testing} == $localsize{$testing}) {
                   1117:                             $needs_download = 0;
                   1118:                             $filetouse = 'loncapa-'.$testing.'.tar.gz';
                   1119:                         }
                   1120:                     }
                   1121:                 }
                   1122:                 if ($needs_download) { 
                   1123:                     if ($production) {
                   1124:                         if (exists($localsize{$production})) {
                   1125:                             if ($stdsizes->{$production} == $localsize{$production}) {
                   1126:                                 $needs_download = 0;
                   1127:                                 $filetouse = 'loncapa-'.$production.'.tar.gz';
                   1128:                             }
                   1129:                         }
                   1130:                     }
                   1131:                 }
                   1132:                 if ($needs_download) {
                   1133:                     my @sorted = sort { $b <=> $a } keys(%bymodtime);
                   1134:                     my $newest = $sorted[0];
                   1135:                     if (ref($bymodtime{$newest}) eq 'ARRAY') {
                   1136:                         $downloadstatus = 
                   1137:                               "Latest LON-CAPA source download in $rootdir is: ".
                   1138:                               join(',',@{$bymodtime{$newest}})." (downloaded ".
                   1139:                               localtime($newest).")\n";
                   1140:                     }
                   1141:                 } else {
                   1142:                     $downloadstatus = 
                   1143:                         "The $rootdir directory already contains the latest LON-CAPA version:".
                   1144:                         "\n".$filetouse."\n"."which can be used for installation.\n";
                   1145:                 }
                   1146:             } else {
                   1147:                 $downloadstatus = "The $rootdir directory does not appear to contain any downloaded LON-CAPA source code files which can be used for installation.\n";
                   1148:             }
                   1149:         }
                   1150:     } else {
                   1151:         $downloadstatus = "Could not open $rootdir directory to look for existing downloads of LON-CAPA source code.\n";
                   1152:     }
                   1153:     return ($needs_download,$downloadstatus,$filetouse,$production,$testing);
                   1154: }
                   1155: 
                   1156: sub check_mysql_setup {
1.34      raeburn  1157:     my ($instdir,$dsn,$distro,$mysql_has_wwwuser) = @_;
1.45.2.9! raeburn  1158:     my ($mysqlsetup,$has_pass,$mysql_unix_socket);
1.1       raeburn  1159:     my $dbh = DBI->connect($dsn,'root','',{'PrintError'=>0});
1.45.2.9! raeburn  1160:     my ($mysqlversion,$mysqlsubver,$mysqlname) = &get_mysql_version();
        !          1161:     if (($mysqlname =~ /^MariaDB/i) && ($mysqlversion >= 10.4)) {
        !          1162:         if ($dbh) {
        !          1163:             my $sth = $dbh->prepare("SELECT Priv FROM mysql.global_priv WHERE (User = 'root' AND Host ='localhost')");
        !          1164:             $sth->execute();
        !          1165:             while (my $priv = $sth->fetchrow_array) {
        !          1166:                 if ($priv =~ /unix_socket/) {        
        !          1167:                     $mysql_unix_socket = 1;
        !          1168:                     last;
        !          1169:                 }
        !          1170:             }
        !          1171:             $sth->finish();
        !          1172:             if ($mysql_unix_socket) {
        !          1173:                 print_and_log(&mt('MariaDB using unix_socket for root access from localhost.')."\n");
        !          1174:                 $mysqlsetup = 'rootok';
        !          1175:                 $mysql_unix_socket = 1;
        !          1176:                 unless ($mysql_has_wwwuser) {
        !          1177:                     $mysql_has_wwwuser = &check_mysql_wwwuser();
        !          1178:                 }
        !          1179:                 return ($mysqlsetup,$has_pass,$dbh,$mysql_has_wwwuser,$mysql_unix_socket);
        !          1180:             }
        !          1181:         }
        !          1182:     }
1.1       raeburn  1183:     if ($dbh) {
                   1184:         $mysqlsetup = 'noroot'; 
                   1185:     } elsif ($DBI::err =~ /1045/) {
                   1186:         $has_pass = 1;
1.34      raeburn  1187:     } elsif ($distro =~ /^ubuntu(\d+)$/) {
                   1188:         my $version = $1;
                   1189:         if ($1 > 12) {
                   1190:             print_and_log(&mt('Restarting mysql, please be patient')."\n");
                   1191:             if (open (PIPE, "service mysql restart 2>&1 |")) {
                   1192:                 while (<PIPE>) {
                   1193:                     print $_;
                   1194:                 }
                   1195:                 close(PIPE);
                   1196:             }
                   1197:             unless ($mysql_has_wwwuser) {
                   1198:                 $mysql_has_wwwuser = &check_mysql_wwwuser();
                   1199:             }
                   1200:             $dbh = DBI->connect($dsn,'root','',{'PrintError'=>0});
                   1201:             if ($dbh) {
                   1202:                 $mysqlsetup = 'noroot';
                   1203:             } elsif ($DBI::err =~ /1045/) {
                   1204:                 $has_pass = 1;
                   1205:             } else {
                   1206:                 $mysqlsetup = 'needsrestart';
                   1207:                 return ($mysqlsetup,$has_pass,$dbh,$mysql_has_wwwuser);
                   1208:             }
                   1209:         }
1.1       raeburn  1210:     }
                   1211:     if ($has_pass) {
                   1212:         print &mt('You have already set a root password for the MySQL database.')."\n";
                   1213:         my $currpass = &get_mysql_password(&mt('Please enter the password now'));
                   1214:         $dbh = DBI->connect($dsn,'root',$currpass,{'PrintError'=>0});
                   1215:         if ($dbh) {
                   1216:             $mysqlsetup = 'rootok';
                   1217:             print_and_log(&mt('Password accepted.')."\n");
                   1218:         } else {
                   1219:             $mysqlsetup = 'rootfail';
                   1220:             print_and_log(&mt('Problem accessing MySQL.')."\n");
                   1221:             if ($DBI::err =~ /1045/) {
                   1222:                 print_and_log(&mt('Perhaps the password was incorrect?')."\n");
                   1223:                 print &mt('Try again?').' ';
                   1224:                 $currpass = &get_mysql_password(&mt('Re-enter password now')); 
                   1225:                 $dbh = DBI->connect($dsn,'root',$currpass,{'PrintError'=>0});
                   1226:                 if ($dbh) {
                   1227:                     $mysqlsetup = 'rootok';
                   1228:                     print_and_log(&mt('Password accepted.')."\n");
                   1229:                 } else {
                   1230:                     if ($DBI::err =~ /1045/) {
                   1231:                         print_and_log(&mt('Incorrect password.')."\n");
                   1232:                     }
                   1233:                 }
                   1234:             }
                   1235:         }
1.34      raeburn  1236:     } elsif ($mysqlsetup ne 'noroot') {
1.1       raeburn  1237:         print_and_log(&mt('Problem accessing MySQL.')."\n");
                   1238:         $mysqlsetup = 'rootfail';
                   1239:     }
1.34      raeburn  1240:     return ($mysqlsetup,$has_pass,$dbh,$mysql_has_wwwuser);
1.1       raeburn  1241: }
                   1242: 
                   1243: sub check_mysql_wwwuser {
                   1244:     my $mysql_wwwuser;
1.12      raeburn  1245:     my $dbhn = DBI->connect("DBI:mysql:database=information_schema",'www','localhostkey',
                   1246:                             {PrintError => +0}) || return;
1.1       raeburn  1247:     if ($dbhn) {
                   1248:         $mysql_wwwuser = 1;
                   1249:         $dbhn->disconnect;
                   1250:     }
                   1251:     return $mysql_wwwuser;
                   1252: }
                   1253: 
                   1254: sub check_loncapa_mysqldb {
                   1255:     my ($dbh) = @_;
                   1256:     my $has_lcdb;
                   1257:     if (ref($dbh)) {
                   1258:         my $sth = $dbh->prepare("SHOW DATABASES");
                   1259:         $sth->execute();
                   1260:         while (my $dbname = $sth->fetchrow_array) {
                   1261:             if ($dbname eq 'loncapa') {
                   1262:                 $has_lcdb = 1;
                   1263:                 last;
                   1264:             }
                   1265:         }
                   1266:         $sth->finish();
                   1267:     }
                   1268:     return $has_lcdb;
                   1269: }
                   1270: 
                   1271: sub get_pathto_iptables {
                   1272:     my $iptables;
                   1273:     if (-e '/sbin/iptables') {
                   1274:         $iptables = '/sbin/iptables';
                   1275:     } elsif (-e '/usr/sbin/iptables') {
                   1276:         $iptables = '/usr/sbin/iptables';
                   1277:     } else {
                   1278:         print &mt('Unable to find iptables command.')."\n";
                   1279:     }
                   1280:     return $iptables;
                   1281: }
                   1282: 
                   1283: sub firewall_is_active {
                   1284:     if (-e '/proc/net/ip_tables_names') {
1.45.2.1  raeburn  1285:         if (open(PIPE,'cat /proc/net/ip_tables_names |grep filter |')) {
                   1286:             my $status = <PIPE>;
                   1287:             close(PIPE);
                   1288:             chomp($status);
                   1289:             if ($status eq 'filter') {
                   1290:                 return 1;
                   1291:             }
                   1292:         }
1.1       raeburn  1293:     }
1.45.2.1  raeburn  1294:     return 0;
1.1       raeburn  1295: }
                   1296: 
                   1297: sub get_fw_chains {
1.5       raeburn  1298:     my ($iptables,$distro) = @_;
1.1       raeburn  1299:     my @fw_chains;
                   1300:     my $suse_config = "/etc/sysconfig/SuSEfirewall2";
                   1301:     my $ubuntu_config = "/etc/ufw/ufw.conf";
                   1302:     if (-e $suse_config) {
                   1303:         push(@fw_chains,'input_ext');
                   1304:     } else {
                   1305:         my @posschains;
                   1306:         if (-e $ubuntu_config) {
                   1307:             @posschains = ('ufw-user-input','INPUT');
1.5       raeburn  1308:         } elsif ($distro =~ /^debian5/) {
                   1309:             @posschains = ('INPUT');
1.45.2.1  raeburn  1310:         } elsif ($distro =~ /^(suse|sles)(\d+)/) {
                   1311:             @posschains = ('IN_public');
1.1       raeburn  1312:         } else {
                   1313:             @posschains = ('RH-Firewall-1-INPUT','INPUT');
                   1314:             if (!-e '/etc/sysconfig/iptables') {
                   1315:                 if (!-e '/var/lib/iptables') {
                   1316:                     print &mt('Unable to find iptables file containing static definitions.')."\n";
                   1317:                 }
                   1318:                 push(@fw_chains,'RH-Firewall-1-INPUT');
                   1319:             }
                   1320:         }
                   1321:         if ($iptables eq '') {
                   1322:             $iptables = &get_pathto_iptables();
                   1323:         }
                   1324:         my %counts;
                   1325:         if (open(PIPE,"$iptables -L -n |")) {
                   1326:             while(<PIPE>) {
                   1327:                 foreach my $chain (@posschains) {
                   1328:                     if (/(\Q$chain\E)/) {
                   1329:                         $counts{$1} ++;
                   1330:                     }
                   1331:                 }
                   1332:             }
                   1333:             close(PIPE);
                   1334:         }
                   1335:         foreach my $fw_chain (@posschains) {
                   1336:             if ($counts{$fw_chain}) {
                   1337:                 unless(grep(/^\Q$fw_chain\E$/,@fw_chains)) {
                   1338:                     push(@fw_chains,$fw_chain);
                   1339:                 }
                   1340:             }
                   1341:         }
                   1342:     }
                   1343:     return @fw_chains;
                   1344: }
                   1345: 
                   1346: sub firewall_is_port_open {
                   1347:     my ($iptables,$fw_chain,$port) = @_;
                   1348:     # returns 1 if the firewall port is open, 0 if not.
                   1349:     #
                   1350:     # check if firewall is active or installed
                   1351:     return if (! &firewall_is_active());
                   1352:     my $count = 0;
                   1353:     if (open(PIPE,"$iptables -L $fw_chain -n |")) {
                   1354:         while(<PIPE>) {
                   1355:             if (/tcp dpt\:\Q$port\E/) {
                   1356:                 $count ++;
                   1357:                 last;
                   1358:             }
                   1359:         }
                   1360:         close(PIPE);
                   1361:     } else {
                   1362:         print &mt('Firewall status not checked: unable to run [_1].','iptables -L')."\n";
                   1363:     }
                   1364:     return $count;
                   1365: }
                   1366: 
                   1367: sub get_mysql_password {
                   1368:     my ($prompt) = @_;
                   1369:     local $| = 1;
                   1370:     print $prompt.': ';
                   1371:     my $newpasswd = '';
                   1372:     ReadMode 'raw';
                   1373:     my $key;
                   1374:     while(ord($key = ReadKey(0)) != 10) {
                   1375:         if(ord($key) == 127 || ord($key) == 8) {
                   1376:             chop($newpasswd);
                   1377:             print "\b \b";
                   1378:         } elsif(!ord($key) < 32) {
                   1379:             $newpasswd .= $key;
                   1380:             print '*';
                   1381:         }
                   1382:     }
                   1383:     ReadMode 'normal';
                   1384:     print "\n";
                   1385:     return $newpasswd;
                   1386: }
                   1387: 
                   1388: sub check_SuSEfirewall2_setup {
                   1389:     my ($instdir) = @_;
                   1390:     my $need_override = 1;
1.9       raeburn  1391:     if ((-e "/etc/insserv/overrides/SuSEfirewall2_setup") && (-e "$instdir/sles-suse/SuSEfirewall2_setup")) {
                   1392:         if (open(PIPE, "diff --brief $instdir/sles-suse/SuSEfirewall2_setup /etc/insserv/overrides/SuSEfirewall2_setup  |")) {
1.1       raeburn  1393:             my $diffres = <PIPE>;
                   1394:             close(PIPE);
                   1395:             chomp($diffres);
                   1396:             unless ($diffres) {
                   1397:                 $need_override = 0;
                   1398:             }
                   1399:         }
                   1400:     }
                   1401:     return $need_override;
                   1402: }
                   1403: 
                   1404: sub download_versionslist {
                   1405:     my ($production,$testing,%sizes);
                   1406:     if (-e "latest.txt") {
                   1407:          unlink("latest.txt");
                   1408:     }
                   1409:     my $rtncode = system("wget http://install.loncapa.org/versions/latest.txt ".
                   1410:                          "> /dev/null 2>&1");
                   1411:     if (!$rtncode) {
                   1412:         if (open(my $fh,"<latest.txt")) {
                   1413:             my @info = <$fh>;
                   1414:             close($fh);
                   1415:             foreach my $line (@info) {
                   1416:                 chomp();
                   1417:                 if ($line =~ /^\QLATEST-IS: \E([\w\-.]+):(\d+)$/) {
                   1418:                      $production = $1;
                   1419:                      $sizes{$1} = $2;
                   1420:                 } elsif ($line =~ /^LATEST-TESTING-IS: \E([\w\-.]+):(\d+)$/) {
                   1421:                      $testing = $1;
                   1422:                      $sizes{$1} = $2;
                   1423:                 }
                   1424:             }
                   1425:         }
                   1426:     }
                   1427:     return ($production,$testing,\%sizes);
                   1428: }
                   1429: 
                   1430: #
                   1431: # End helper routines.
                   1432: # Main script starts here
                   1433: #
                   1434: 
                   1435: print "
                   1436: ********************************************************************
                   1437: 
                   1438:                     ".&mt('Welcome to LON-CAPA')."
                   1439: 
                   1440: ".&mt('This script will configure your system for installation of LON-CAPA.')."
                   1441: 
                   1442: ********************************************************************
                   1443: 
                   1444: ".&mt('The following actions are available:')."
                   1445: 
1.4       raeburn  1446: ".&mt('1.')." ".&mt('Create the www user/group.')."
1.1       raeburn  1447:    ".&mt('This is the user/group ownership under which Apache child processes run.')."
                   1448:    ".&mt('It also owns most directories within the /home/httpd directory.')." 
                   1449:    ".&mt('This directory is where most LON-CAPA files and directories are stored.')."
1.4       raeburn  1450: ".&mt('2.')." ".&mt('Install the package LON-CAPA uses to authenticate users.')."
                   1451: ".&mt('3.')." ".&mt('Set-up the MySQL database.')."
                   1452: ".&mt('4.')." ".&mt('Set-up MySQL permissions.')."
                   1453: ".&mt('5.')." ".&mt('Configure Apache web server.')."
                   1454: ".&mt('6.')." ".&mt('Configure start-up of services.')."
                   1455: ".&mt('7.')." ".&mt('Check firewall settings.')."
                   1456: ".&mt('8.')." ".&mt('Stop services not used by LON-CAPA,')."
1.1       raeburn  1457:    ".&mt('i.e., services for a print server: [_1] daemon.',"'cups'")."
1.4       raeburn  1458: ".&mt('9.')." ".&mt('Download LON-CAPA source code in readiness for installation.')."
1.1       raeburn  1459: 
                   1460: ".&mt('Typically, you will run this script only once, when you first install LON-CAPA.')." 
                   1461: 
                   1462: ".&mt('The script will analyze your system to determine which actions are recommended.')."
                   1463: ".&mt('The script will then prompt you to choose the actions you would like taken.')."
                   1464: 
                   1465: ".&mt('For each the recommended action will be selected if you hit Enter/Return.')."
                   1466: ".&mt('To override the default, type the lower case option from the two options listed.')."
                   1467: ".&mt('So, if the default is "yes", ~[Y/n~] will be shown -- type n to override.')."
                   1468: ".&mt('Whereas if the default is "no", ~[y/N~] will be shown -- type y to override.')." 
                   1469: 
                   1470: ".&mt('To accept the default, simply hit Enter/Return on your keyboard.')."
                   1471: ".&mt('Otherwise type: y or n then hit the Enter/Return key.')."
                   1472: 
                   1473: ".&mt('Once a choice has been entered for all nine actions, required changes will be made.')."
                   1474: ".&mt('Feedback will be displayed on screen, and also stored in: [_1].','loncapa_install.log')."
                   1475: 
                   1476: ".&mt('Continue? ~[Y/n~] ');
                   1477: 
                   1478: my $go_on = &get_user_selection(1);
                   1479: if (!$go_on) {
                   1480:     exit;
                   1481: }
                   1482: 
                   1483: my $instdir = `pwd`;
                   1484: chomp($instdir);
                   1485: 
                   1486: my %callsub;
                   1487: my @actions = ('wwwuser','pwauth','mysql','mysqlperms','apache',
                   1488:                'runlevels','firewall','stopsrvcs','download');
                   1489: my %prompts = &texthash( 
                   1490:     wwwuser    => "Create the 'www' user?",
                   1491:     pwauth     => 'Install the package LON-CAPA uses to authenticate users?',
                   1492:     mysql      => 'Set-up the MySQL database?',
                   1493:     mysqlperms => 'Set-up MySQL permissions?',
                   1494:     apache     => 'Configure Apache web server?',
                   1495:     runlevels  => 'Set overrides for start-up order of services?',
                   1496:     firewall   => 'Configure firewall settings for Apache',
                   1497:     stopsrvcs  => 'Stop extra services not required on a LON-CAPA server?',
                   1498:     download   => 'Download LON-CAPA source code in readiness for installation?',
                   1499: );
                   1500: 
                   1501: print "\n".&mt('Checking system status ...')."\n";
                   1502: 
                   1503: my $dsn = "DBI:mysql:database=mysql";
1.34      raeburn  1504: my ($distro,$gotprereqs,$localecmd,$packagecmd,$updatecmd,$installnow,$mysqlrestart,
1.45.2.9! raeburn  1505:     $recommended,$dbh,$has_pass,$mysql_unix_socket,$has_lcdb,$downloadstatus,
        !          1506:     $filetouse,$production,$testing,$apachefw,$uses_systemctl) = &check_required($instdir,$dsn);
1.1       raeburn  1507: if ($distro eq '') {
                   1508:     print "\n".&mt('Linux distribution could not be verified as a supported distribution.')."\n".
                   1509:           &mt('The following are supported: [_1].',
                   1510:               'CentOS, RedHat Enterprise, Fedora, Scientific Linux, '.
1.45.2.3  raeburn  1511:               'Oracle Linux, openSuSE, SLES, Ubuntu LTS, Debian')."\n\n".
1.1       raeburn  1512:           &mt('Stopping execution.')."\n";
                   1513:     exit;
                   1514: }
1.34      raeburn  1515: if ($mysqlrestart) {
                   1516:     print "\n".&mt('The mysql daemon needs to be restarted using the following command:')."\n".
                   1517:           $mysqlrestart."\n\n".
                   1518:           &mt('Stopping execution of install.pl script.')."\n".
                   1519:           &mt('Please run the install.pl script again, once you have restarted mysql.')."\n";
                   1520:     exit;
                   1521: }
1.6       raeburn  1522: if ($localecmd ne '') {
                   1523:     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";
                   1524:     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".
                   1525:     $localecmd."\n\n".
                   1526:     &mt('Stopping execution.')."\n";
                   1527:     exit;
                   1528: }
1.1       raeburn  1529: if (!$gotprereqs) {
1.12      raeburn  1530:     print "\n".&mt('The LONCAPA-prerequisites package is not installed.')."\n".
1.1       raeburn  1531:           &mt('The following command can be used to install the package (and dependencies):')."\n\n".
                   1532:           $updatecmd."\n\n";
                   1533:     if ($installnow eq '') {
                   1534:         print &mt('Stopping execution.')."\n";
                   1535:         exit;
                   1536:     } else {
                   1537:         print &mt('Run command? ~[Y/n~]');
                   1538:         my $install_prereq = &get_user_selection(1);
                   1539:         if ($install_prereq) {
                   1540:             if (open(PIPE,'|-',$installnow)) {
                   1541:                 close(PIPE);
                   1542:                 $gotprereqs = &check_prerequisites($packagecmd,$distro);
                   1543:                 if (!$gotprereqs) {
1.12      raeburn  1544:                     print &mt('The LONCAPA-prerequisites package is not installed.')."\n".
1.1       raeburn  1545:                           &mt('Stopping execution.')."\n";
                   1546:                     exit;
                   1547:                 } else {
1.6       raeburn  1548:                     ($distro,$gotprereqs,$localecmd,$packagecmd,$updatecmd,$installnow,
1.45.2.9! raeburn  1549:                      $mysqlrestart,$recommended,$dbh,$has_pass,$mysql_unix_socket,
        !          1550:                      $has_lcdb,$downloadstatus,$filetouse,$production,$testing,$apachefw,
        !          1551:                      $uses_systemctl) = &check_required($instdir,$dsn);
1.1       raeburn  1552:                 }
                   1553:             } else {
1.12      raeburn  1554:                 print &mt('Failed to run command to install LONCAPA-prerequisites')."\n";
1.1       raeburn  1555:                 exit;
                   1556:             }
                   1557:         } else {
                   1558:             print &mt('Stopping execution.')."\n";
                   1559:             exit;
                   1560:         }
                   1561:     }
                   1562: }
                   1563: unless (ref($recommended) eq 'HASH') {
                   1564:     print "\n".&mt('An error occurred determining which actions are recommended.')."\n\n".
                   1565:           &mt('Stopping execution.')."\n";
                   1566:     exit;
                   1567: }
                   1568: 
                   1569: print "\n";
                   1570: my $num = 0;
                   1571: foreach my $action (@actions) {
                   1572:     $num ++;
                   1573:     my ($yesno,$defaultrun);
                   1574:     if (ref($recommended) eq 'HASH') {
1.4       raeburn  1575:         if (($action eq 'runlevels') || ($action eq 'stopsrvcs')) {
1.1       raeburn  1576:             $yesno = '[y/N]';
                   1577:             if (ref($recommended->{$action}) eq 'HASH') {
                   1578:                 if (keys(%{$recommended->{$action}}) > 0) {
                   1579:                     $yesno = &mt('~[Y/n~]');
                   1580:                     $defaultrun = 1;
                   1581:                 }
                   1582:             }
                   1583:         } else {
                   1584:             if ($action eq 'download') {
                   1585:                 if ($downloadstatus) {
                   1586:                     print "\n$downloadstatus\n";
                   1587:                 }
                   1588:             }
                   1589:             if ($recommended->{$action}) {
                   1590:                 $yesno = mt('~[Y/n~]');
                   1591:                 $defaultrun = 1;
                   1592:             } else {
                   1593:                 $yesno = &mt('~[y/N~]');
                   1594:             }
                   1595:         }
                   1596:         print $num.'. '.$prompts{$action}." $yesno ";
                   1597:         $callsub{$action} = &get_user_selection($defaultrun);
                   1598:     }
                   1599: }
                   1600: 
                   1601: my $lctarball = 'loncapa-current.tar.gz';
                   1602: my $sourcetarball = $lctarball;
                   1603: if ($callsub{'download'}) {
                   1604:     my ($production,$testing,$sizes) = &download_versionslist();
                   1605:     if ($production && $testing) {
                   1606:         if ($production ne $testing) {
                   1607:             print &mt('Two recent LON-CAPA releases are available: ')."\n".
1.3       raeburn  1608:                   &mt('1.').' '.&mt('A production release - version: [_1].',$production)."\n".
                   1609:                   &mt('2.').' '.&mt('A testing release - version: [_1].',$testing)."\n\n".
1.1       raeburn  1610:                   &mt('Download the production release? ~[Y/n~]');
                   1611:             if (&get_user_selection(1)) {
1.4       raeburn  1612:                 $sourcetarball = 'loncapa-'.$production.'.tar.gz';
1.1       raeburn  1613:             } else {
                   1614:                 print "\n".&mt('Download the testing release? ~[Y/n~]');
                   1615:                 if (&get_user_selection(1)) {
1.4       raeburn  1616:                     $sourcetarball = 'loncapa-'.$testing.'.tar.gz';
1.1       raeburn  1617:                 }
                   1618:             }
                   1619:         }
                   1620:     } elsif ($production) {
                   1621:         print &mt('The most recent LON-CAPA release is version: [_1].',$production)."\n".
                   1622:               &mt('Download the production release? ~[Y/n~]');
                   1623:         if (&get_user_selection(1)) {
1.20      raeburn  1624:             $sourcetarball = 'loncapa-'.$production.'.tar.gz';
1.1       raeburn  1625:         }
                   1626:     }
                   1627: } elsif ($filetouse ne '') {
                   1628:     $sourcetarball = $filetouse;
                   1629: }
                   1630: 
                   1631: print_and_log("\n");
                   1632: 
                   1633: # Each action: report if skipping, or perform action and provide feedback. 
                   1634: if ($callsub{'wwwuser'}) {
                   1635:     &setup_www();
                   1636: } else {
                   1637:     &print_and_log(&mt('Skipping creation of user [_1].',"'www'")."\n");
                   1638: }
                   1639: 
                   1640: if ($callsub{'pwauth'}) {
1.4       raeburn  1641:     &build_and_install_mod_auth_external($instdir);
1.1       raeburn  1642: } else {
                   1643:     &print_and_log(&mt('Skipping [_1] installation.',"'pwauth'")."\n");
                   1644: }
                   1645: 
                   1646: if ($callsub{'mysql'}) {
                   1647:     if ($dbh) {
1.45.2.9! raeburn  1648:         &setup_mysql($callsub{'mysqlperms'},$dbh,$has_pass,
        !          1649:                      $mysql_unix_socket,$has_lcdb);
1.1       raeburn  1650:     } else {
                   1651:         print &mt('Unable to configure MySQL because access is denied.')."\n";
                   1652:     }
                   1653: } else {
                   1654:     &print_and_log(&mt('Skipping configuration of MySQL.')."\n");
                   1655:     if ($callsub{'mysqlperms'}) {
                   1656:         if ($dbh) {
1.45.2.9! raeburn  1657:             &setup_mysql_permissions($dbh,$has_pass,$mysql_unix_socket);
1.1       raeburn  1658:         } else {
                   1659:             print &mt('Unable to configure MySQL because access is denied.')."\n";  
                   1660:         }
                   1661:     } else {
                   1662:         &print_and_log(&mt('Skipping MySQL permissions setup.')."\n");
                   1663:     }
                   1664: }
                   1665: 
                   1666: if ($dbh) {
                   1667:     if (!$dbh->disconnect) {
                   1668:         &print_and_log(&mt('Failed to disconnect from MySQL:')."\n".
                   1669:                        $dbh->errstr);
                   1670:     }
                   1671: }
                   1672: 
                   1673: if ($callsub{'apache'}) {
                   1674:     if ($distro =~ /^(suse|sles)/) {
1.45.2.1  raeburn  1675:         &copy_apache2_suseconf($instdir,$distro);
1.1       raeburn  1676:     } elsif ($distro =~ /^(debian|ubuntu)/) {
1.28      raeburn  1677:         &copy_apache2_debconf($instdir,$distro);
1.1       raeburn  1678:     } else {
1.14      raeburn  1679:         &copy_httpd_conf($instdir,$distro);
1.45.2.5  raeburn  1680:         &copy_mpm_conf($instdir,$distro);
1.1       raeburn  1681:     }
                   1682: } else {
                   1683:     print_and_log(&mt('Skipping configuration of Apache web server.')."\n");
                   1684: }
                   1685: 
                   1686: if ($callsub{'runlevels'}) {
                   1687:     my $count = 0;
                   1688:     if (ref($recommended) eq 'HASH') {
                   1689:         if (ref($recommended->{'runlevels'}) eq 'HASH') {
                   1690:             foreach my $type (keys(%{$recommended->{'runlevels'}})) {
                   1691:                 next if ($type eq 'insserv');
                   1692:                 $count ++;
                   1693:                 my $command = $recommended->{'runlevels'}{$type};
                   1694:                 if ($command ne '') {
                   1695:                     print_and_log(&mt('Runlevel update command run: [_1].',$command)."\n");
                   1696:                     system($command);
                   1697:                 }
                   1698:             }
                   1699:             if (!$count) {
                   1700:                 print_and_log(&mt('No runlevel updates required.')."\n");
                   1701:             }  
                   1702:         }
                   1703:     }
1.45.2.1  raeburn  1704:     if ($distro =~ /^(suse|sles)(\d+)/) {
                   1705:         unless(($1 eq 'sles') && ($2 >= 15)) {
                   1706:             &update_SuSEfirewall2_setup($instdir);
                   1707:         }
1.11      raeburn  1708:     }
1.1       raeburn  1709: } else {
                   1710:     &print_and_log(&mt('Skipping setting override for start-up order of services.')."\n");
                   1711: }
                   1712: 
                   1713: if ($callsub{'firewall'}) {
1.45.2.3  raeburn  1714:     my ($firewalld,$zone) = &uses_firewalld($distro);
                   1715:     if ($firewalld) {
1.45.2.1  raeburn  1716:         my (%current,%added);
1.45.2.3  raeburn  1717:         if (open(PIPE,"firewall-cmd --permanent --zone=$zone --list-services |")) {
1.45.2.1  raeburn  1718:             my $svc = <PIPE>;
                   1719:             close(PIPE);
                   1720:             chomp($svc);
                   1721:             map { $current{$_} = 1; } (split(/\s+/,$svc));
                   1722:         }
                   1723:         foreach my $service ('http','https') {
                   1724:             unless ($current{$service}) {
1.45.2.3  raeburn  1725:                 if (open(PIPE,"firewall-cmd --permanent --zone=$zone --add-service=$service |")) {
1.45.2.1  raeburn  1726:                     my $result = <PIPE>;
                   1727:                     if ($result =~ /^success/) {
                   1728:                         $added{$service} = 1;
                   1729:                     }
                   1730:                 }
                   1731:             }
                   1732:         }
                   1733:         if (keys(%added) > 0) {
                   1734:             print &mt('Firewall configured to allow access for: [_1].',
                   1735:                       join(', ',sort(keys(%added))))."\n";
                   1736:         }
                   1737:         if ($current{'http'} || $current{'https'}) {
                   1738:             print &mt('Firewall already configured to allow access for:[_1].',
                   1739:                       (($current{'http'})? ' http':'').(($current{'https'})? ' https':''))."\n";
                   1740:         }
                   1741:         unless ($current{'ssh'}) {
                   1742:             print &mt('If you would the like to allow access to ssh from outside, use the command[_1].',
1.45.2.3  raeburn  1743:                   "firewall-cmd --permanent --zone=$zone --add-service=ssh")."\n";
1.45.2.1  raeburn  1744:         }
                   1745:     } elsif ($distro =~ /^(suse|sles)/) {
1.5       raeburn  1746:         print &mt('Use [_1] to configure the firewall to allow access for [_2].',
                   1747:                   'yast -- Security and Users -> Firewall -> Interfaces',
1.45.2.1  raeburn  1748:                   'ssh, http, https')."\n";
1.5       raeburn  1749:     } elsif ($distro =~ /^(debian|ubuntu)(\d+)/) {
                   1750:         if (($1 eq 'ubuntu') || ($2 > 5)) {
                   1751:             print &mt('Use [_1] to configure the firewall to allow access for [_2].',
                   1752:                       'ufw','ssh, http, https')."\n";
                   1753:         } else {
                   1754:             my $fwadded = &get_iptables_rules($distro,$instdir,$apachefw);
                   1755:             if ($fwadded) {
                   1756:                 print &mt('Enable firewall? ~[Y/n~]');
                   1757:                 my $enable_iptables = &get_user_selection(1);
                   1758:                 if ($enable_iptables) {
                   1759:                     system('/etc/network/if-pre-up.d/iptables');
                   1760:                     print &mt('Firewall enabled using rules defined in [_1].',
                   1761:                               '/etc/iptables.loncapa.rules'); 
                   1762:                 }
                   1763:             }
                   1764:         }
1.45.2.3  raeburn  1765:     } elsif ($distro =~ /^(scientific|oracle)/) {
1.11      raeburn  1766:         print &mt('Use [_1] to configure the firewall to allow access for [_2].',
                   1767:                   'system-config-firewall-tui -- Customize',
                   1768:                   'ssh, http')."\n";
1.1       raeburn  1769:     } else {
1.45.2.3  raeburn  1770:         my $version;
                   1771:         if ($distro =~ /^(redhat|centos)(\d+)$/) {
                   1772:             $version = $1;
                   1773:         }
                   1774:         if ($version > 5) {
                   1775:             print &mt('Use [_1] to configure the firewall to allow access for [_2].',
                   1776:                   'system-config-firewall-tui -- Customize',
                   1777:                   'ssh, http')."\n";
                   1778:         } else {
                   1779:             print &mt('Use [_1] to configure the firewall to allow access for [_2].',
                   1780:                       'setup -- Firewall configuration -> Customize',
                   1781:                       'ssh, http, https')."\n";
                   1782:         }
1.1       raeburn  1783:     }
                   1784: } else {
1.5       raeburn  1785:     &print_and_log(&mt('Skipping Firewall configuration.')."\n");
1.1       raeburn  1786: }
                   1787: 
                   1788: if ($callsub{'stopsrvcs'}) {
1.45      raeburn  1789:     &kill_extra_services($distro,$recommended->{'stopsrvcs'},$uses_systemctl);
1.1       raeburn  1790: } else {
1.10      raeburn  1791:     &print_and_log(&mt('Skipping stopping unnecessary service ([_1] daemons).',"'cups','memcached'")."\n");
1.1       raeburn  1792: }
                   1793: 
                   1794: my ($have_tarball,$updateshown);
                   1795: if ($callsub{'download'}) {
                   1796:     ($have_tarball,$updateshown) = &download_loncapa($instdir,$sourcetarball);
                   1797: } else {
                   1798:     print_and_log(&mt('Skipping download of LON-CAPA tar file.')."\n\n");
                   1799:     print &mt('LON-CAPA is available for download from: [_1]',
                   1800:               'http://install.loncapa.org/')."\n";
                   1801:     if (!-e '/etc/loncapa-release') {
                   1802:         &print_and_log(&mt('LON-CAPA is not yet installed on your system.'). 
                   1803:                        "\n\n". 
                   1804:                        &mt('You may retrieve the source for LON-CAPA by executing:')."\n".
                   1805:                        "wget http://install.loncapa.org/versions/$lctarball\n");
                   1806:     } else {
                   1807:         my $currentversion;
                   1808:         if (open(my $fh,"</etc/loncapa-release")) {
                   1809:             my $version = <$fh>;
                   1810:             chomp($version);
                   1811:             if ($version =~ /^\QLON-CAPA release \E([\w\-.]+)$/) {
                   1812:                 $currentversion = $1;
                   1813:             }
                   1814:         }
                   1815:         if ($currentversion ne '') {
                   1816:             print &mt('Version of LON-CAPA currently installed on this server is: [_1].',
                   1817:                       $currentversion),"\n";
                   1818:             if ($production) {
                   1819:                 print &mt('The latest production release of LON-CAPA is [_1].',$production)."\n";
                   1820:             }
                   1821:             if ($testing) {
                   1822:                 print &mt('The latest testing release of LON-CAPA is [_1].',$testing)."\n";
                   1823:             }
                   1824:         }
                   1825:     }
                   1826:     if ($filetouse ne '') {
                   1827:         $have_tarball = 1;
                   1828:     }
                   1829: }
                   1830: 
                   1831: print "\n".&mt('Requested configuration complete.')."\n\n";
                   1832: my $apachename;
                   1833: if ($have_tarball && !$updateshown) {
                   1834:     my ($lcdir) = ($sourcetarball =~ /^([\w.\-]+)\.tar.gz$/);
                   1835:     if (!-e '/etc/loncapa-release') {
                   1836:         print &mt('If you are now ready to install LON-CAPA, enter the following commands:')."\n\n";
                   1837:     } else {
                   1838:         print &mt('If you are now ready to update LON-CAPA, enter the following commands:')."\n\n".
                   1839:                   "/etc/init.d/loncontrol stop\n";
                   1840:         if ($distro =~ /^(suse|sles|debian|ubuntu)([\d.]+)/) {
                   1841:             if (($1 eq 'suse') && ($2 < 10)) {
                   1842:                 $apachename = 'apache';
                   1843:             } else {
                   1844:                 $apachename = 'apache2';
                   1845:             }
                   1846:         } else {
                   1847:             $apachename = 'httpd';
                   1848:         }
                   1849:         print "/etc/init.d/$apachename stop\n";
                   1850:     }
                   1851:     print "cd /root\n".
                   1852:           "tar zxf $sourcetarball\n".
                   1853:           "cd $lcdir\n".
                   1854:           "./UPDATE\n";
                   1855:     if (-e '/etc/loncapa-release') {
                   1856:         print "/etc/init.d/loncontrol start\n";
                   1857:         print "/etc/init.d/$apachename start\n";
                   1858:     }
                   1859: }
                   1860: exit;
                   1861: 
                   1862: #
                   1863: # End main script
                   1864: #
                   1865: 
                   1866: #
                   1867: # Routines for the actions
                   1868: #
                   1869:  
                   1870: sub setup_www {
                   1871:     ##
                   1872:     ## Set up www
                   1873:     ##
                   1874:     print_and_log(&mt('Creating user [_1]',"'www'")."\n");
                   1875:     # -- Add group
                   1876: 
                   1877:     my $status = `/usr/sbin/groupadd www`;
                   1878:     if ($status =~ /\QGroup `www' already exists.\E/) {
                   1879:         print &mt('Group [_1] already exists.',"'www'")."\n";
                   1880:     } elsif ($status ne '') {
                   1881:         print &mt('Unable to add group [_1].',"'www'")."\n";
                   1882:     }
                   1883:    
                   1884:     my $gid = getgrnam('www');
                   1885: 
                   1886:     if (open (PIPE, "/usr/sbin/useradd -c LONCAPA -g $gid www 2>&1 |")) {
                   1887:         $status = <PIPE>;
                   1888:         close(PIPE);
                   1889:         chomp($status);
                   1890:         if ($status =~ /\QAccount `www' already exists.\E/) {
                   1891:             print &mt('Account [_1] already exists.',"'www'")."\n";
                   1892:         } elsif ($status ne '') {
                   1893:             print &mt('Unable to add user [_1].',"'www'")."\n";
                   1894:         }
                   1895:     }  else {
                   1896:         print &mt('Unable to run command to add user [_1].',"'www'")."\n";
                   1897:     }
                   1898: 
                   1899:     my $uid = &uid_of_www();
                   1900:     if (($gid ne '') && ($uid ne '')) {
                   1901:         if (!-e '/home/www') {
                   1902:             mkdir('/home/www',0755);
                   1903:             system('chown www:www /home/www');
                   1904:         }
                   1905:     }
                   1906:     writelog ($status);
                   1907: }
                   1908: 
                   1909: sub uid_of_www {
                   1910:     my ($num) = (getpwnam('www'))[2];
                   1911:     return $num;
                   1912: }
                   1913: 
                   1914: sub build_and_install_mod_auth_external {
                   1915:     my ($instdir) = @_;
                   1916:     my $num = &uid_of_www();
                   1917:     # Patch pwauth
                   1918:     print_and_log(&mt('Building authentication system for LON-CAPA users.')."\n");
                   1919:     my $patch = <<"ENDPATCH";
                   1920: 148c148
                   1921: < #define SERVER_UIDS 99		/* user "nobody" */
                   1922: ---
                   1923: > #define SERVER_UIDS $num		/* user "www" */
                   1924: ENDPATCH
                   1925: 
                   1926:     if (! -e "/usr/bin/patch") {
                   1927: 	print_and_log(&mt('You must install the software development tools package: [_1], when installing Linux.',"'patch'")."\n");
                   1928:         print_and_log(&mt('Authentication installation not completed.')."\n");
                   1929:         return;
                   1930:     }
                   1931:     if (&skip_if_nonempty(`cd /tmp; tar zxf $instdir/pwauth-2.2.8.tar.gz`,
                   1932: 		     &mt('Unable to extract pwauth')."\n")) {
                   1933:         return;
                   1934:     }
                   1935:     my $dir = "/tmp/pwauth-2.2.8";
                   1936:     if (open(PATCH,"| patch $dir/config.h")) {
                   1937:         print PATCH $patch;
                   1938:         close(PATCH);
                   1939:         print_and_log("\n");
                   1940:         ##
                   1941:         ## Compile patched pwauth
                   1942:         ##
                   1943:         print_and_log(&mt('Compiling pwauth')."\n");
1.12      raeburn  1944:         my $result = `cd $dir/; make 2>/dev/null `;
1.1       raeburn  1945:         my $expected = <<"END";
                   1946: gcc -g    -c -o pwauth.o pwauth.c
                   1947: gcc -o pwauth -g  pwauth.o -lcrypt
                   1948: END
                   1949:         if ($result eq $expected) {
                   1950:             print_and_log(&mt('Apparent success compiling pwauth:').
                   1951:                           "\n".$result );
                   1952:             # Install patched pwauth
                   1953:             print_and_log(&mt('Copying pwauth to [_1]',' /usr/local/sbin')."\n");
                   1954:             if (copy "$dir/pwauth","/usr/local/sbin/pwauth") {
1.5       raeburn  1955:                 if (chmod(06755, "/usr/local/sbin/pwauth")) {
1.1       raeburn  1956:                     print_and_log(&mt('[_1] copied successfully',"'pwauth'").
                   1957:                                   "\n");
                   1958:                 } else {
                   1959:                     print &mt('Unable to set permissions on [_1].'.
                   1960:                               "/usr/local/sbin/pwauth")."\n";
                   1961:                 }
                   1962:             } else {
                   1963:                 print &mt('Unable to copy [_1] to [_2]',
                   1964:                           "'$dir/pwauth'","/usr/local/sbin/pwauth")."\n$!\n";
                   1965:             }
                   1966:         } else {
                   1967:             print &mt('Unable to compile patched [_1].'."'pwauth'")."\n";
                   1968:         }
                   1969:     } else {
                   1970:         print &mt('Unable to start patch for [_1]',"'pwauth'")."\n";
                   1971:     }
                   1972:     print_and_log("\n");
                   1973: }
                   1974: 
                   1975: sub kill_extra_services {
1.45      raeburn  1976:     my ($distro,$stopsrvcs,$uses_systemctl) = @_;
1.1       raeburn  1977:     if (ref($stopsrvcs) eq 'HASH') {
                   1978:         my @stopping = sort(keys(%{$stopsrvcs}));
                   1979:         if (@stopping) {
1.6       raeburn  1980:             my $kill_list = join("', '",@stopping);
1.1       raeburn  1981:             if ($kill_list) {
                   1982:                 $kill_list = "'".$kill_list."'";
1.6       raeburn  1983:                 &print_and_log("\n".&mt('Killing unnecessary services ([_1] daemon(s)).',$kill_list)."\n");
                   1984:                 foreach my $service (@stopping) {
                   1985:                     my $daemon = $service;
                   1986:                     if ($service eq 'cups') {
                   1987:                         $daemon = 'cupsd';
                   1988:                         if ($distro =~ /^(?:debian|ubuntu)(\d+)/) {
                   1989:                             my $version = $1;
                   1990:                             if (($distro =~ /^ubuntu/) && ($version <= 8)) {
                   1991:                                 $daemon = 'cupsys';
                   1992:                             }
1.12      raeburn  1993:                         } else {
1.8       raeburn  1994:                             $daemon = 'cups';
1.6       raeburn  1995:                         }
                   1996:                     }
1.12      raeburn  1997:                     my $cmd = "ps -ef |grep '$daemon' |grep -v grep";
                   1998:                     if (open(PIPE,'-|',$cmd)) {
                   1999:                         my $daemonrunning = <PIPE>;
                   2000:                         chomp($daemonrunning);
                   2001:                         close(PIPE);
                   2002:                         if ($daemonrunning) {
                   2003:                             &print_and_log(`/etc/init.d/$daemon stop`);
                   2004:                         }
                   2005:                     }
1.1       raeburn  2006: 	            &print_and_log(&mt('Removing [_1] from startup.',$service)."\n");
1.45      raeburn  2007:                     if ($distro =~ /^(?:debian|ubuntu)(\d+)/) {
                   2008:                         my $version = $1;
                   2009:                         if (($distro =~ /^ubuntu/) && ($version > 16)) {
                   2010:                             if (ref($uses_systemctl) eq 'HASH') {
                   2011:                                 if ($uses_systemctl->{$service}) {
                   2012:                                     if (`systemctl is-enabled $service`) {
                   2013:                                         &print_and_log(`systemctl disable $service`);
                   2014:                                     }
                   2015:                                 }
                   2016:                             }
                   2017:                         } else {
                   2018:                             &print_and_log(`update-rc.d -f $daemon remove`);
                   2019:                         }
1.1       raeburn  2020:                     } else {
1.35      raeburn  2021:                         if (ref($uses_systemctl) eq 'HASH') {
                   2022:                             if ($uses_systemctl->{$service}) {
                   2023:                                 if (`systemctl is-enabled $service`) {                          
                   2024:                                     &print_and_log(`systemctl disable $service`);
                   2025:                                 }
                   2026:                             } else {
                   2027:                                 &print_and_log(`/sbin/chkconfig --del $service`);
                   2028:                             }
                   2029:                         } else {
                   2030: 	                    &print_and_log(`/sbin/chkconfig --del $service`);
                   2031:                         } 
1.1       raeburn  2032:                     }
                   2033:                 }
                   2034:             }
                   2035:         }
                   2036:     }
                   2037:     return;
                   2038: }
                   2039: 
                   2040: sub setup_mysql {
1.45.2.9! raeburn  2041:     my ($setup_mysql_permissions,$dbh,$has_pass,$mysql_unix_socket,$has_lcdb) = @_;
1.4       raeburn  2042:     my @mysql_lc_commands;
1.1       raeburn  2043:     unless ($has_lcdb) {
1.4       raeburn  2044:         push(@mysql_lc_commands,"CREATE DATABASE loncapa");
1.1       raeburn  2045:     }
1.4       raeburn  2046:     push(@mysql_lc_commands,"USE loncapa");
                   2047:     push(@mysql_lc_commands,qq{
1.18      raeburn  2048: 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  2049: });
1.1       raeburn  2050:     if ($setup_mysql_permissions) {
1.45.2.9! raeburn  2051:         &setup_mysql_permissions($dbh,$has_pass,$mysql_unix_socket,@mysql_lc_commands);
1.1       raeburn  2052:     } else {
                   2053:         print_and_log(&mt('Skipping MySQL permissions setup.')."\n");
                   2054:         if ($dbh) {
1.4       raeburn  2055:             if (@mysql_lc_commands) {
                   2056:                 foreach my $lccmd (@mysql_lc_commands) { 
                   2057:                     $dbh->do($lccmd) || print $dbh->errstr."\n";
                   2058:                 }
                   2059:             }
1.1       raeburn  2060:             print_and_log(&mt('MySQL database set up complete.')."\n");
                   2061:         } else {
                   2062:             print_and_log(&mt('Problem accessing MySQL.')."\n");
                   2063:         }
                   2064:     }
                   2065: }
                   2066: 
                   2067: sub setup_mysql_permissions {
1.45.2.9! raeburn  2068:     my ($dbh,$has_pass,$mysql_unix_socket,@mysql_lc_commands) = @_;
1.38      raeburn  2069:     my ($mysqlversion,$mysqlsubver,$mysqlname) = &get_mysql_version();
1.45.2.9! raeburn  2070:     my ($usescreate,$usesauth,$is_mariadb,$hasauthcol,@mysql_commands);
1.38      raeburn  2071:     if ($mysqlname =~ /^MariaDB/i) {
1.45.2.2  raeburn  2072:         $is_mariadb = 1;
1.45.2.9! raeburn  2073:         if ($mysqlversion >= 10.4) {
        !          2074:             $usescreate = 1;
        !          2075:         } elsif ($mysqlversion >= 10.2) {
1.38      raeburn  2076:             $usesauth = 1;
1.42      raeburn  2077:         } elsif ($mysqlversion >= 5.5) {
                   2078:             $hasauthcol = 1;
1.38      raeburn  2079:         }
                   2080:     } else {
                   2081:         if (($mysqlversion > 5.7) || (($mysqlversion == 5.7) && ($mysqlsubver > 5))) {
                   2082:             $usesauth = 1;
1.42      raeburn  2083:         } elsif (($mysqlversion >= 5.6) || (($mysqlversion == 5.5) && ($mysqlsubver >= 7))) {
                   2084:             $hasauthcol = 1;
1.38      raeburn  2085:         }
                   2086:     }
1.45.2.9! raeburn  2087:     if ($usescreate) {
        !          2088:         @mysql_commands = ("CREATE USER 'www'\@'localhost' IDENTIFIED BY 'localhostkey'");
        !          2089:     } elsif ($usesauth) {
1.45.2.2  raeburn  2090:         @mysql_commands = ("INSERT user (Host, User, ssl_cipher, x509_issuer, x509_subject, authentication_string) VALUES('localhost','www','','','','')");
                   2091:         if ($is_mariadb) {
                   2092:             push(@mysql_commands,"ALTER USER 'www'\@'localhost' IDENTIFIED BY 'localhostkey'");
                   2093:         } else {
                   2094:             push(@mysql_commands,"ALTER USER 'www'\@'localhost' IDENTIFIED WITH mysql_native_password BY 'localhostkey'");
                   2095:         }
1.42      raeburn  2096:     } elsif ($hasauthcol) {
                   2097:         @mysql_commands = ("INSERT user (Host, User, Password, ssl_cipher, x509_issuer, x509_subject, authentication_string) VALUES('localhost','www',password('localhostkey'),'','','','');");
1.36      raeburn  2098:     } else {
1.42      raeburn  2099:         @mysql_commands = ("INSERT user (Host, User, Password, ssl_cipher, x509_issuer, x509_subject) VALUES('localhost','www',password('localhostkey'),'','','');");
1.36      raeburn  2100:     }
1.1       raeburn  2101:     if ($mysqlversion < 4) {
1.4       raeburn  2102:         push (@mysql_commands,"
                   2103: 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')");
                   2104:     } else {
                   2105:         push (@mysql_commands,"
                   2106: 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  2107:     }
1.4       raeburn  2108:     push(@mysql_commands,"DELETE FROM user WHERE host<>'localhost'");
1.45.2.9! raeburn  2109:     if (($has_pass) || ($mysql_unix_socket)) {
1.1       raeburn  2110:         if ($dbh) {
1.4       raeburn  2111:             push(@mysql_commands,"FLUSH PRIVILEGES");
                   2112:             if (@mysql_commands) {
                   2113:                 foreach my $cmd (@mysql_commands) {
                   2114:                     $dbh->do($cmd) || print $dbh->errstr."\n";
                   2115:                 }
                   2116:             }
                   2117:             if (@mysql_lc_commands) {
                   2118:                 foreach my $lccmd (@mysql_lc_commands) {
                   2119:                     $dbh->do($lccmd) || print $dbh->errstr."\n";
                   2120:                 }
                   2121:             }
1.1       raeburn  2122:             print_and_log(&mt('Permissions set for LON-CAPA MySQL user: [_1]',"'www'")."\n");
                   2123:         } else {
                   2124:             print_and_log(&mt('Problem accessing MySQL.')."\n".
                   2125:                           &mt('Permissions not set.')."\n");
                   2126:         }
                   2127:     } else {
                   2128:         my ($firstpass,$secondpass,$got_passwd,$newmysqlpass);
                   2129:         print &mt('Please enter a root password for the mysql database.')."\n".
                   2130:               &mt('It does not have to match your root account password, but you will need to remember it.')."\n";
                   2131:         my $maxtries = 10;
                   2132:         my $trial = 0;
                   2133:         while ((!$got_passwd) && ($trial < $maxtries)) {
                   2134:             $firstpass = &get_mysql_password(&mt('Enter password'));
                   2135:             if (length($firstpass) > 5) { 
                   2136:                 $secondpass = &get_mysql_password(&mt('Enter password a second time'));
                   2137:                 if ($firstpass eq $secondpass) {
                   2138:                     $got_passwd = 1;
                   2139:                     $newmysqlpass = $firstpass;
                   2140:                 } else {
                   2141:                     print(&mt('Passwords did not match. Please try again.')."\n");
                   2142:                 }
                   2143:                 $trial ++;
                   2144:             } else {
                   2145:                 print(&mt('Password too short.')."\n".
                   2146:                       &mt('Please choose a password with at least six characters.')."\n");
                   2147:             }
                   2148:         }
                   2149:         if ($got_passwd) {
1.45.2.2  raeburn  2150:             my (@newpass_cmds) = &new_mysql_rootpasswd($newmysqlpass,$usesauth,$is_mariadb);
1.4       raeburn  2151:             push(@mysql_commands,@newpass_cmds);
1.1       raeburn  2152:         } else {
                   2153:             print_and_log(&mt('Failed to get MySQL root password from user input.')."\n");
                   2154:         }
                   2155:         if ($dbh) {
1.4       raeburn  2156:             if (@mysql_commands) {
                   2157:                 foreach my $cmd (@mysql_commands) {
                   2158:                     $dbh->do($cmd) || print $dbh->errstr."\n";
                   2159:                 }
                   2160:             }
                   2161:             if (@mysql_lc_commands) {
                   2162:                 foreach my $lccmd (@mysql_lc_commands) {
                   2163:                     $dbh->do($lccmd) || print $dbh->errstr."\n";
                   2164:                 }
                   2165:             }
1.1       raeburn  2166:             if ($got_passwd) {
                   2167:                 print_and_log(&mt('MySQL root password stored.')."\n".
                   2168:                               &mt('Permissions set for LON-CAPA MySQL user: [_1].',"'www'")."\n");
                   2169:             } else {
                   2170:                 print_and_log(&mt('Permissions set for LON-CAPA MySQL user: [_1].',"'www'")."\n");
                   2171:             }
                   2172:         } else {
                   2173:             print_and_log(&mt('Problem accessing MySQL.')."\n".
                   2174:                           &mt('Permissions not set.')."\n");
                   2175:         }
                   2176:     }
                   2177: }
                   2178: 
                   2179: sub new_mysql_rootpasswd {
1.45.2.2  raeburn  2180:     my ($currmysqlpass,$usesauth,$is_mariadb) = @_;
1.36      raeburn  2181:     if ($usesauth) {
1.45.2.2  raeburn  2182:         if ($is_mariadb) {
                   2183:             return ("ALTER USER 'root'\@'localhost' IDENTIFIED BY '$currmysqlpass'",
                   2184:                     "FLUSH PRIVILEGES;");
                   2185:         } else {
                   2186:             return ("ALTER USER 'root'\@'localhost' IDENTIFIED WITH mysql_native_password BY '$currmysqlpass'",
                   2187:                     "FLUSH PRIVILEGES;");
                   2188:         }
1.36      raeburn  2189:     } else {
                   2190:         return ("SET PASSWORD FOR 'root'\@'localhost'=PASSWORD('$currmysqlpass')",
                   2191:                 "FLUSH PRIVILEGES;");
                   2192:     }
1.1       raeburn  2193: }
                   2194: 
                   2195: sub get_mysql_version {
1.38      raeburn  2196:     my ($version,$subversion,$name);
1.1       raeburn  2197:     if (open(PIPE," mysql -V |")) {
                   2198:         my $info = <PIPE>;
                   2199:         chomp($info);
                   2200:         close(PIPE);
1.45.2.7  raeburn  2201:         ($version,$subversion,$name) = ($info =~ /(\d+\.\d+)\.(\d+)(?:\-?(\w*),|)/);
1.1       raeburn  2202:     } else {
                   2203:         print &mt('Could not determine which version of MySQL is installed.').
                   2204:               "\n";
                   2205:     }
1.38      raeburn  2206:     return ($version,$subversion,$name);
1.1       raeburn  2207: }
                   2208: 
                   2209: ###########################################################
                   2210: ##
                   2211: ## RHEL/CentOS/Fedora/Scientific Linux
                   2212: ## Copy LON-CAPA httpd.conf to /etc/httpd/conf
                   2213: ##
                   2214: ###########################################################
                   2215: 
                   2216: sub copy_httpd_conf {
1.14      raeburn  2217:     my ($instdir,$distro) = @_;
                   2218:     my $configfile = 'httpd.conf';
1.45.2.3  raeburn  2219:     if ($distro =~ /^(?:centos|rhes|scientific|oracle)(\d+)$/) {
1.29      raeburn  2220:         if ($1 >= 7) {
                   2221:             $configfile = 'apache2.4/httpd.conf';
                   2222:         } elsif ($1 > 5) {
1.14      raeburn  2223:             $configfile = 'new/httpd.conf';
                   2224:         }
                   2225:     } elsif ($distro =~ /^fedora(\d+)$/) {
1.29      raeburn  2226:         if ($1 > 17) {
                   2227:             $configfile = 'apache2.4/httpd.conf';
                   2228:         } elsif ($1 > 10) {
1.14      raeburn  2229:             $configfile = 'new/httpd.conf';
                   2230:         }
                   2231:     }
1.1       raeburn  2232:     print_and_log(&mt('Copying the LON-CAPA [_1] to [_2].',"'httpd.conf'",
                   2233:                   "'/etc/httpd/conf/httpd.conf'")."\n");
                   2234:     copy "/etc/httpd/conf/httpd.conf","/etc/httpd/conf/httpd.conf.original";
1.14      raeburn  2235:     copy "$instdir/centos-rhes-fedora-sl/$configfile","/etc/httpd/conf/httpd.conf";
1.5       raeburn  2236:     chmod(0444,"/etc/httpd/conf/httpd.conf");
1.1       raeburn  2237:     print_and_log("\n");
                   2238: }
                   2239: 
1.45.2.5  raeburn  2240: ###########################################################
                   2241: ##
                   2242: ## RHEL/CentOS/Fedora/Scientific Linux
                   2243: ## Copy LON-CAPA mpm.conf to /etc/httpd/conf.modules.d/00-mpm.conf
                   2244: ##
                   2245: ## The LON-CAPA mpm.conf enables the prefork MPM module in
                   2246: ## Apache. This is also the default for RHEL/CentOS/Oracle
                   2247: ## Linux 7 and earlier, and Fedora 26 and earlier. For more
                   2248: ## recent versions of those distros, the event MPM is enabled
                   2249: ## by default. After &copy_mpm_conf() is run, the prefork MPM
                   2250: ## module will be enabled instead of the event MPM module.
                   2251: ##
                   2252: ###########################################################
                   2253: 
                   2254: sub copy_mpm_conf {
                   2255:     my ($instdir,$distro) = @_;
                   2256:     my $mpmfile = 'mpm.conf';
                   2257:     if ((-e "/etc/httpd/conf.modules.d/00-mpm.conf") &&
                   2258:         (-e "$instdir/centos-rhes-fedora-sl/$mpmfile")) {
                   2259:         print_and_log(&mt('Copying the LON-CAPA [_1] to [_2].',"'mpm.conf'",
                   2260:                       "'/etc/httpd/conf.modules.d/00-mpm.conf'")."\n");
                   2261:         copy "$instdir/centos-rhes-fedora-sl/$mpmfile","/etc/httpd/conf.modules.d/00-mpm.conf";
                   2262:         chmod(0644,"/etc/httpd/conf.modules.d/00-mpm.conf");
                   2263:         print_and_log("\n");
                   2264:     } else {
                   2265:         my $logfail;
                   2266:         if ($distro =~ /^(?:centos|rhes|scientific|oracle)(\d+)$/) {
                   2267:             if ($1 > 7) {
                   2268:                 $logfail = 1;
                   2269:             }
                   2270:         } elsif ($distro =~ /^fedora(\d+)$/) {
                   2271:             if ($1 > 26) {
                   2272:                 $logfail = 1;
                   2273:             }
                   2274:         }
                   2275:         if ($logfail) {
                   2276:             print_and_log(&mt('Warning: copying the LON-CAPA [_1] failed because [_2] and/or [_3] are missing.',
                   2277:                               $mpmfile,"'$instdir/centos-rhes-fedora-sl/$mpmfile'",
                   2278:                               "'/etc/httpd/conf.modules.d/00-mpm.conf'"));
                   2279:             print_and_log("\n");
                   2280:         }
                   2281:     }
                   2282: }
                   2283: 
1.1       raeburn  2284: #########################################################
                   2285: ##
1.17      raeburn  2286: ## Ubuntu/Debian -- copy our loncapa configuration file to
1.1       raeburn  2287: ## sites-available and set the symlink from sites-enabled.
                   2288: ##
                   2289: #########################################################
                   2290: 
                   2291: sub copy_apache2_debconf {
1.28      raeburn  2292:     my ($instdir,$distro) = @_;
1.6       raeburn  2293:     my $apache2_mods_enabled_dir = '/etc/apache2/mods-enabled';
                   2294:     my $apache2_mods_available_dir = '/etc/apache2/mods-available';
                   2295:     foreach my $module ('headers.load','expires.load') {
                   2296:         unless (-l "$apache2_mods_enabled_dir/$module") {
                   2297:             symlink("$apache2_mods_available_dir/$module","$apache2_mods_enabled_dir/$module");
                   2298:             print_and_log(&mt('Enabling "[_1]" Apache module.',$module)."\n");
                   2299:         }
                   2300:     }
1.28      raeburn  2301:     my $apache2_sites_enabled_dir = '/etc/apache2/sites-enabled';
                   2302:     my $apache2_sites_available_dir = '/etc/apache2/sites-available';
                   2303:     my $defaultconfig = "$apache2_sites_enabled_dir/000-default";
                   2304:     my ($distname,$version);
                   2305:     if ($distro =~ /^(debian|ubuntu)(\d+)$/) {
                   2306:         $distname = $1;
                   2307:         $version = $2;
                   2308:     }
                   2309:     if (($distname eq 'ubuntu') && ($version > 12)) {
                   2310:         $defaultconfig = "$apache2_sites_enabled_dir/000-default.conf";
                   2311:     }
1.45.2.7  raeburn  2312:     my ($skipconf,$skipsite,$skipstatus);
1.28      raeburn  2313:     if (($distname eq 'ubuntu') && ($version > 12)) {
                   2314:         my $apache2_conf_enabled_dir = '/etc/apache2/conf-enabled';
                   2315:         my $apache2_conf_available_dir = '/etc/apache2/conf-available';
1.33      raeburn  2316:         my $defaultconf = $apache2_conf_enabled_dir.'/loncapa.conf';
1.45.2.7  raeburn  2317:         if ((-e "$apache2_conf_available_dir/loncapa") && (-e "$instdir/debian-ubuntu/ubuntu14/loncapa_conf")) {
1.45.2.8  raeburn  2318:             if (open(PIPE, "diff --brief $apache2_conf_available_dir/loncapa $instdir/debian-ubuntu/ubuntu14/loncapa_conf |")) {
1.45.2.7  raeburn  2319:                 my $diffres = <PIPE>;
                   2320:                 close(PIPE);
                   2321:                 chomp($diffres);
                   2322:                 if ($diffres) {
                   2323:                     copy("$apache2_conf_available_dir/loncapa","$apache2_conf_available_dir/loncapa.original");
                   2324:                 }
                   2325:                 if (-l $defaultconf) {
                   2326:                     my $linkfname = readlink($defaultconf);
                   2327:                     if ($linkfname eq "$apache2_conf_available_dir/loncapa") {
                   2328:                         unless ($diffres) {
                   2329:                             $skipconf = 1;
                   2330:                         }
                   2331:                     }
                   2332:                 }
                   2333:             }
                   2334:         }
                   2335:         unless ($skipconf) {
                   2336:             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");
                   2337:             copy("$instdir/debian-ubuntu/ubuntu14/loncapa_conf","$apache2_conf_available_dir/loncapa");
                   2338:             chmod(0444,"$apache2_conf_available_dir/loncapa");
                   2339:             if (-l $defaultconf) {
                   2340:                 unlink($defaultconf);
                   2341:             }
                   2342:             symlink("$apache2_conf_available_dir/loncapa","$defaultconf");
                   2343:         }
                   2344:         my $stdsite = "$instdir/debian-ubuntu/ubuntu14/loncapa_site";
                   2345:         if ((-e $stdsite) && (-e "$apache2_sites_available_dir/loncapa")) {
                   2346:             if (open(PIPE, "diff --brief $stdsite $apache2_sites_available_dir/loncapa |")) {
                   2347:                 my $diffres = <PIPE>;
                   2348:                 close(PIPE);
                   2349:                 chomp($diffres);
                   2350:                 if ($diffres) {
                   2351:                     copy("$apache2_sites_available_dir/loncapa","$apache2_sites_available_dir/loncapa.original");
                   2352:                 }
                   2353:                 if (-l $defaultconfig) {
                   2354:                     my $linkfname = readlink($defaultconfig);
                   2355:                     if ($linkfname eq "$apache2_sites_available_dir/loncapa") {
                   2356:                         unless ($diffres) {
                   2357:                             $skipsite = 1;
                   2358:                         }
                   2359:                     }
                   2360:                 }
                   2361:             }
                   2362:         }
                   2363:         unless ($skipsite) {
                   2364:             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");
                   2365:             copy("$instdir/debian-ubuntu/ubuntu14/loncapa_site","$apache2_sites_available_dir/loncapa");
                   2366:             chmod(0444,"$apache2_sites_available_dir/loncapa");
                   2367:             symlink("$apache2_sites_available_dir/loncapa","$defaultconfig");
                   2368:         }
                   2369:     } else {
                   2370:         if ((-e "$instdir/debian-ubuntu/loncapa") && (-e "$apache2_sites_available_dir/loncapa")) {
                   2371:             if (open(PIPE, "diff --brief $instdir/debian-ubuntu/loncapa $apache2_sites_available_dir/loncapa |")) {
                   2372:                 my $diffres = <PIPE>;
                   2373:                 close(PIPE);
                   2374:                 chomp($diffres);
                   2375:                 if ($diffres) {
                   2376:                     copy("$apache2_sites_available_dir/loncapa","$apache2_sites_available_dir/loncapa.original");
                   2377:                 }
                   2378:                 if (-l $defaultconfig) {
                   2379:                     my $linkfname = readlink($defaultconfig);
                   2380:                     if ($linkfname eq "$apache2_sites_available_dir/loncapa") {
                   2381:                         unless ($diffres) {
                   2382:                             $skipsite = 1;
                   2383:                         }
                   2384:                     }
                   2385:                 }
                   2386:             }
                   2387:         }
                   2388:         unless ($skipsite) {
                   2389:             if (-l $defaultconfig) {
                   2390:                 unlink($defaultconfig);
                   2391:             }
                   2392:             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");
                   2393:             if (-e "$instdir/debian-ubuntu/loncapa") {
                   2394:                 copy("$instdir/debian-ubuntu/loncapa","$apache2_sites_available_dir/loncapa");
                   2395:                 chmod(0444,"$apache2_sites_available_dir/loncapa");
                   2396:                 symlink("$apache2_sites_available_dir/loncapa","$apache2_sites_enabled_dir/000-default");
                   2397:             }
                   2398:         }
                   2399:     }
                   2400:     if ($distname eq 'ubuntu') {
                   2401:         my $sitestatus = "$apache2_mods_available_dir/status.conf";
                   2402:         my $stdstatus = "$instdir/debian-ubuntu/status.conf";
                   2403:         if ((-e $sitestatus) && (-e $stdstatus)) {
                   2404:             if (open(PIPE, "diff --brief $stdstatus $sitestatus |")) {
                   2405:                 my $diffres = <PIPE>;
                   2406:                 close(PIPE);
                   2407:                 chomp($diffres);
                   2408:                 if ($diffres) {
                   2409:                     copy("$apache2_mods_available_dir/status.conf","$apache2_mods_available_dir/status.conf.original");
                   2410:                 } else {
                   2411:                     $skipstatus = 1;
                   2412:                 }
                   2413:             }
                   2414:         }
                   2415:         unless ($skipstatus) {
                   2416:             if (-e $stdstatus) {
                   2417:                 print_and_log(&mt('Copying loncapa [_1] file to [_2],',"'status.conf'","'/etc/apache2/mods-available/status.conf'")."\n");
                   2418:                 copy($stdstatus,$sitestatus);
                   2419:                 chmod(0644,$sitestatus);
                   2420:             }
                   2421:         }
1.28      raeburn  2422:     }
1.1       raeburn  2423:     print_and_log("\n");
                   2424: }
                   2425: 
                   2426: ###########################################################
                   2427: ##
                   2428: ## openSuSE/SLES Copy apache2 config files:
                   2429: ##   default-server.conf, uid.conf, /etc/sysconfig/apache2 
                   2430: ##   and create symlink from /srv/www/conf to /etc/apache2 
                   2431: ##
                   2432: ###########################################################
                   2433: 
                   2434: sub copy_apache2_suseconf {
1.45.2.1  raeburn  2435:     my ($instdir,$distro) = @_;
                   2436:     my ($name,$version) = ($distro =~ /^(suse|sles)([\d\.]+)$/);
                   2437:     my $conf_file = "$instdir/sles-suse/default-server.conf";
                   2438:     if (($name eq 'sles') && ($version >= 12)) {
                   2439:         $conf_file = "$instdir/sles-suse/apache2.4/default-server.conf";
                   2440:     }
1.1       raeburn  2441:     print_and_log(&mt('Copying the LON-CAPA [_1] to [_2].',
                   2442:                   "'default-server.conf'",
                   2443:                   "'/etc/apache2/default-server.conf'")."\n");
                   2444:     if (!-e "/etc/apache2/default-server.conf.original") {
                   2445:         copy "/etc/apache2/default-server.conf","/etc/apache2/default-server.conf.original";
                   2446:     }
1.45.2.1  raeburn  2447:     copy $conf_file,"/etc/apache2/default-server.conf";
1.5       raeburn  2448:     chmod(0444,"/etc/apache2/default-server.conf");
1.1       raeburn  2449:     # Make symlink for conf directory (included in loncapa_apache.conf)
                   2450:     my $can_symlink = (eval { symlink('/etc/apache2','/srv/www/conf'); }, $@ eq '');
                   2451:     if ($can_symlink) {
                   2452:         &print_and_log(&mt('Symlink created for [_1] to [_2].',
                   2453:                        "'/srv/www/conf'","'/etc/apache2'")."\n");
                   2454:     } else {
                   2455:         &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");
                   2456:     }
                   2457:     &copy_apache2_conf_files($instdir);
1.45.2.1  raeburn  2458:     &copy_sysconfig_apache2_file($instdir,$name,$version); 
1.1       raeburn  2459:     print_and_log("\n");
                   2460: }
                   2461: 
                   2462: ###############################################
                   2463: ##
                   2464: ## Modify uid.conf
                   2465: ##
                   2466: ###############################################
                   2467: sub copy_apache2_conf_files {
                   2468:     my ($instdir) = @_;
                   2469:     print_and_log(&mt('Copying the LON-CAPA [_1] to [_2].',
                   2470:                   "'uid.conf'","'/etc/apache2/uid.conf'")."\n");
                   2471:     if (!-e "/etc/apache2/uid.conf.original") {
                   2472:         copy "/etc/apache2/uid.conf","/etc/apache2/uid.conf.original";
                   2473:     }
1.9       raeburn  2474:     copy "$instdir/sles-suse/uid.conf","/etc/apache2/uid.conf";
1.5       raeburn  2475:     chmod(0444,"/etc/apache2/uid.conf");
1.1       raeburn  2476: }
                   2477: 
                   2478: ###############################################
                   2479: ##
                   2480: ## Modify /etc/sysconfig/apache2  
                   2481: ##
                   2482: ###############################################
                   2483: sub copy_sysconfig_apache2_file {
1.45.2.1  raeburn  2484:     my ($instdir,$name,$version) = @_;
1.1       raeburn  2485:     print_and_log(&mt('Copying the LON-CAPA [_1] to [_2].',"'sysconfig/apache2'","'/etc/sysconfig/apache2'")."\n");
                   2486:     if (!-e "/etc/sysconfig/apache2.original") {
                   2487:         copy "/etc/sysconfig/apache2","/etc/sysconfig/apache2.original";
                   2488:     }
1.45.2.1  raeburn  2489:     my $sysconf_file = "$instdir/sles-suse/sysconfig_apache2";
                   2490:     if (($name eq 'sles') && ($version >= 12)) {
                   2491:        $sysconf_file = "$instdir/sles-suse/apache2.4/sysconfig_apache2";
                   2492:     }
                   2493:     copy $sysconf_file,"/etc/sysconfig/apache2";
1.5       raeburn  2494:     chmod(0444,"/etc/sysconfig/apache2");
1.1       raeburn  2495: }
                   2496: 
                   2497: ###############################################
                   2498: ##
                   2499: ## Add/Modify /etc/insserv/overrides
                   2500: ##
                   2501: ###############################################
                   2502: 
                   2503: sub update_SuSEfirewall2_setup {
                   2504:     my ($instdir) = @_;
                   2505:     print_and_log(&mt('Copying the LON-CAPA [_1] to [_2].',"'SuSEfirewall2_setup'","'/etc/insserv/overrides/SuSEfirewall2_setup'")."\n");
                   2506:     if (!-e "/etc/insserv/overrides/SuSEfirewall2_setup") {
                   2507:         if (!-d "/etc/insserv") {
                   2508:             mkdir("/etc/insserv",0755); 
                   2509:         }
                   2510:         if (!-d "/etc/insserv/overrides") {
                   2511:             mkdir("/etc/insserv/overrides",0755);
                   2512:         }
                   2513:     } elsif (!-e  "/etc/insserv/overrides/SuSEfirewall2_setup.original") {
                   2514:         copy "/etc/insserv/overrides/SuSEfirewall2_setup","/etc/insserv/overrides/SuSEfirewall2_setup.original"
                   2515:     }
1.9       raeburn  2516:     copy "$instdir/sles-suse/SuSEfirewall2_setup","/etc/insserv/overrides/SuSEfirewall2_setup";
1.5       raeburn  2517:     chmod(0444,"/etc/insserv/overrides/SuSEfirewall2_setup");
                   2518: }
                   2519: 
                   2520: sub get_iptables_rules {
                   2521:     my ($distro,$instdir,$apachefw) = @_;
                   2522:     my (@fwchains,@ports);
                   2523:     if (&firewall_is_active()) {
                   2524:         my $iptables = &get_pathto_iptables();
                   2525:         if ($iptables ne '') {
                   2526:             @fwchains = &get_fw_chains($iptables,$distro);
                   2527:         }
                   2528:     }
                   2529:     if (ref($apachefw) eq 'HASH') {
                   2530:         foreach my $service ('http','https') {
                   2531:             unless ($apachefw->{$service}) {
                   2532:                 push (@ports,$service); 
                   2533:             }
                   2534:         }
                   2535:     } else {
                   2536:         @ports = ('http','https');
                   2537:     }
                   2538:     if (@ports == 0) {
                   2539:         return;
                   2540:     }
                   2541:     my $ask_to_enable;
                   2542:     if (-e "/etc/iptables.loncapa.rules") {
1.9       raeburn  2543:         if (open(PIPE, "diff --brief $instdir/debian-ubuntu/iptables.loncapa.rules /etc/iptables.loncapa.rules |")) {
1.5       raeburn  2544:             my $diffres = <PIPE>;
                   2545:             close(PIPE);
                   2546:             chomp($diffres);
                   2547:             if ($diffres) {
                   2548:                 print &mt('Warning: [_1] exists but differs from LON-CAPA supplied file.','/etc/iptables.loncapa.rules')."\n";
                   2549:             }
                   2550:         } else {
                   2551:             print &mt('Error: unable to open [_1] to compare contents with LON-CAPA supplied file.','/etc/iptables.loncapa.rules')."\n";
                   2552:         }
                   2553:     } else {
1.9       raeburn  2554:         if (-e "$instdir/debian-ubuntu/iptables.loncapa.rules") {
                   2555:             copy "$instdir/debian-ubuntu/iptables.loncapa.rules","/etc/iptables.loncapa.rules";
1.5       raeburn  2556:             chmod(0600,"/etc/iptables.loncapa.rules");
                   2557:         }
                   2558:     }
                   2559:     if (-e "/etc/iptables.loncapa.rules") {
                   2560:         if (-e "/etc/network/if-pre-up.d/iptables") {
1.9       raeburn  2561:             if (open(PIPE, "diff --brief $instdir/debian-ubuntu/iptables /etc/network/if-pre-up/iptables |")) {
1.5       raeburn  2562:                 my $diffres = <PIPE>;
                   2563:                 close(PIPE);
                   2564:                 chomp($diffres);
                   2565:                 if ($diffres) {
                   2566:                     print &mt('Warning: [_1] exists but differs from LON-CAPA supplied file.','/etc/network/if-pre-up.d/iptables')."\n";
                   2567:                 }
                   2568:             } else {
                   2569:                 print &mt('Error: unable to open [_1] to compare contents with LON-CAPA supplied file.','/etc/network/if-pre-up.d/iptables')."\n";
                   2570:             }
                   2571:         } else {
1.9       raeburn  2572:             copy "$instdir/debian-ubuntu/iptables","/etc/network/if-pre-up.d/iptables";
1.5       raeburn  2573:             chmod(0755,"/etc/network/if-pre-up.d/iptables");
                   2574:             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'));
                   2575:             $ask_to_enable = 1;
                   2576:         }
                   2577:     }
                   2578:     return $ask_to_enable;
1.1       raeburn  2579: }
                   2580: 
                   2581: sub download_loncapa {
                   2582:     my ($instdir,$lctarball) = @_;
                   2583:     my ($have_tarball,$updateshown);
                   2584:     if (! -e "$instdir/$lctarball") {
                   2585: 	print_and_log(&mt('Retrieving LON-CAPA source files from: [_1]',
                   2586: 		      'http://install.loncapa.org')."\n");
                   2587: 	system("wget http://install.loncapa.org/versions/$lctarball ".
                   2588: 	       "2>/dev/null 1>/dev/null");
                   2589: 	if (! -e "./$lctarball") {
                   2590: 	    print &mt('Unable to retrieve LON-CAPA source files from: [_1].', 
                   2591: 		     "http://install.loncapa.org/versions/$lctarball")."\n";
                   2592: 	} else {
                   2593:             $have_tarball = 1;
                   2594:         }
                   2595: 	print_and_log("\n");
                   2596:     } else {
                   2597:         $have_tarball = 1;
                   2598: 	print_and_log("
                   2599: ------------------------------------------------------------------------
                   2600: 
                   2601: ".&mt('You seem to have a version of loncapa-current.tar.gz in [_1]',$instdir)."\n".
                   2602: &mt('This copy will be used and a new version will NOT be downloaded.')."\n".
                   2603: &mt('If you wish, you may download a new version by executing:')."
                   2604: 
1.2       raeburn  2605: wget http://install.loncapa.org/versions/loncapa-current.tar.gz
1.1       raeburn  2606: 
                   2607: ------------------------------------------------------------------------
                   2608: ");
                   2609:     }
                   2610: 
                   2611:     ##
                   2612:     ## untar loncapa.tar.gz
                   2613:     ##
                   2614:     if ($have_tarball) {
                   2615:         print_and_log(&mt('Extracting LON-CAPA source files')."\n");
                   2616:         writelog(`cd ~root; tar zxf $instdir/$lctarball`);
                   2617:         print_and_log("\n");
                   2618:         print &mt('LON-CAPA source files extracted.')."\n".
                   2619:               &mt('It remains for you to execute the following commands:')."
                   2620: 
1.45.2.7  raeburn  2621: cd /root/loncapa-X.Y.Z     (X.Y.Z should correspond to a version number like '2.11.3')
1.1       raeburn  2622: ./UPDATE
                   2623: 
                   2624: ".&mt('If you have any trouble, please see [_1] and [_2]',
                   2625:       'http://install.loncapa.org/','http://help.loncapa.org/')."\n";
                   2626:         $updateshown = 1;
                   2627:     }
                   2628:     return ($have_tarball,$updateshown);
                   2629: }
                   2630: 
                   2631: close LOG;

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