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