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