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

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

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