Annotation of doc/loncapafiles/webserver.piml, revision 1.56
1.8 harris41 1: <!DOCTYPE piml PUBLIC "-//TUX/DTD piml 1.0 Final//EN"
2: "http://lpml.sourceforge.net/DTD/piml.dtd">
1.1 harris41 3: <!-- webserver.piml -->
4:
1.56 ! raeburn 5: <!-- $Id: webserver.piml,v 1.55 2021/03/11 20:38:05 raeburn Exp $ -->
1.1 harris41 6:
7: <!--
8:
9: Copyright Michigan State University Board of Trustees
10:
11: This file is part of the LearningOnline Network with CAPA (LON-CAPA).
12:
13: LON-CAPA is free software; you can redistribute it and/or modify
14: it under the terms of the GNU General Public License as published by
15: the Free Software Foundation; either version 2 of the License, or
16: (at your option) any later version.
17:
18: LON-CAPA is distributed in the hope that it will be useful,
19: but WITHOUT ANY WARRANTY; without even the implied warranty of
20: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21: GNU General Public License for more details.
22:
23: You should have received a copy of the GNU General Public License
24: along with LON-CAPA; if not, write to the Free Software
25: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26:
27: /home/httpd/html/adm/gpl.txt
28:
29: http://www.lon-capa.org/
30:
31: -->
32:
33: <!-- Default values must be defined before specific values. -->
34: <!-- If no 'dist' attribute is specified, then it is always installed. -->
35: <!-- If 'dist' attribute is set to 'default', then the specification. -->
36: <!-- is accepted if an alternative distribution is not requested or not -->
37: <!-- defined. -->
38:
39: <piml>
1.6 harris41 40: <targetroot>/</targetroot>
1.2 harris41 41: <specialnotices>
42: <specialnotice>
43: </specialnotice>
44: </specialnotices>
1.1 harris41 45: <files>
46: <file>
47: <target dist='default'>/etc/httpd/conf/httpd.conf</target>
1.20 raeburn 48: <target dist='suse9.2 suse9.3 sles9'>/etc/httpd/httpd.conf</target>
1.45 raeburn 49: <target dist='sles10 sles11 sles12 sles15 suse10.1 suse10.2 suse10.3 suse11.1 suse11.2 suse11.3 suse11.4 suse12.1 suse12.2 suse12.3 suse13.1 suse13.2'>/etc/apache2/default-server.conf</target>
1.39 raeburn 50: <target dist='debian5 debian6 ubuntu6 ubuntu8 ubuntu10 ubuntu12'>/etc/apache2/sites-available/loncapa</target>
1.55 raeburn 51: <target dist='ubuntu14 ubuntu16 ubuntu18 ubuntu20'>/etc/apache2/conf-available/loncapa.conf</target>
1.47 raeburn 52: <note>This is for Apache 1.X for Red Hat 4ES, Fedora 2, 3 and 4, SusSE 9.2 and 9.3, and SLES 9 distributions. This is for Apache 2.X for Fedora 5, Red Hat 5, CentOS 5, Scientific Linux 5, Oracle Linux 5, SuSE 10.1, SLES 10, Debian 5, Ubuntu LTS 8 and later distributions</note>
1.1 harris41 53: <dependencies dist='default'>
54: /etc/httpd/conf/httpd.conf
55: </dependencies>
1.20 raeburn 56: <dependencies dist='suse9.2 suse9.3 sles9'>
57: /etc/httpd/httpd.conf
58: </dependencies>
1.39 raeburn 59: <dependencies dist='debian5 debian6 ubuntu6 ubuntu8 ubuntu10 ubuntu12'>
1.25 raeburn 60: /etc/apache2/sites-available/loncapa
61: </dependencies>
1.48 raeburn 62: <dependencies dist='ubuntu14 ubuntu16 ubuntu18 ubuntu20'>
1.55 raeburn 63: /etc/apache2/conf-available/loncapa.conf
1.39 raeburn 64: </dependencies>
1.45 raeburn 65: <dependencies dist='sles10 sles11 sles12 sles15 suse10.1 suse10.2 suse10.3 suse11.1 suse11.2 suse11.3 suse11.4 suse12.1 suse12.2 suse12.3 suse13.1 suse13.2'>
1.20 raeburn 66: /etc/apache2/default-server.conf
67: </dependencies>
1.19 raeburn 68: <perlscript mode='fg' dist="default">
1.11 harris41 69: # Generated from doc/loncapafiles/webserver.piml
1.44 raeburn 70: use Socket;
71: use Sys::Hostname::FQDN();
1.56 ! raeburn 72: use File::Spec;
! 73: use Cwd();
1.55 raeburn 74:
75: # For ubuntu 14 and later check for loncapa.conf in sites-available,
76: # and conf-available, and for symlinks in sites-enabled, and conf-enabled
77: if ('<DIST />' =~ /^ubuntu(\d+)$/) {
78: my $version = $1;
79: if ($version > 12) {
80: if (-l '/etc/apache2/conf-enabled/loncapa.conf') {
81: my $linkfname = readlink('/etc/apache2/conf-enabled/loncapa.conf');
1.56 ! raeburn 82: if ($linkfname ne '') {
! 83: $linkfname = Cwd::abs_path(File::Spec->rel2abs($linkfname,'/etc/apache2/conf-enabled'));
! 84: }
1.55 raeburn 85: unless ($linkfname eq '/etc/apache2/conf-available/loncapa.conf') {
86: unlink('/etc/apache2/conf-enabled/loncapa.conf');
87: }
88: }
89: if (-e '/etc/apache2/conf-available/loncapa') {
90: system('mv /etc/apache2/conf-available/loncapa /etc/apache2/conf-available/loncapa.conf');
91: }
92: unless (-l '/etc/apache2/conf-enabled/loncapa.conf') {
93: if (-e '/etc/apache2/conf-available/loncapa.conf') {
1.56 ! raeburn 94: my $currdir = Cwd::getcwd();
! 95: if ($currdir ne '') {
! 96: chdir('/etc/apache2/conf-enabled');
! 97: symlink('../conf-available/loncapa.conf','loncapa.conf');
! 98: chdir($currdir);
! 99: }
1.55 raeburn 100: }
101: }
102: if (-l '/etc/apache2/sites-enabled/000-default.conf') {
103: my $linkfname = readlink('/etc/apache2/sites-enabled/000-default.conf');
1.56 ! raeburn 104: if ($linkfname ne '') {
! 105: $linkfname = Cwd::abs_path(File::Spec->rel2abs($linkfname,'/etc/apache2/sites-enabled'));
! 106: }
! 107: if (($linkfname eq '/etc/apache2/sites-available/loncapa') ||
! 108: ($linkname eq '/etc/apache2/sites-available/000-default.conf')) {
1.55 raeburn 109: unlink('/etc/apache2/sites-enabled/000-default.conf');
110: }
111: }
112: if (-e '/etc/apache2/sites-available/loncapa') {
113: system('mv /etc/apache2/sites-available/loncapa /etc/apache2/sites-available/loncapa.conf');
114: }
115: if (-l '/etc/apache2/sites-enabled/loncapa.conf') {
116: my $linkfname = readlink('/etc/apache2/sites-enabled/loncapa.conf');
1.56 ! raeburn 117: if ($linkfname ne '') {
! 118: $linkfname = Cwd::abs_path(File::Spec->rel2abs($linkfname,'/etc/apache2/sites-enabled'));
! 119: }
1.55 raeburn 120: unless ($linkfname eq '/etc/apache2/sites-available/loncapa.conf') {
121: unlink('/etc/apache2/sites-enabled/loncapa.conf');
122: }
123: }
124: unless (-l '/etc/apache2/sites-enabled/loncapa.conf') {
125: if (-e '/etc/apache2/sites-available/loncapa.conf') {
1.56 ! raeburn 126: my $currdir = Cwd::getcwd();
! 127: if ($currdir ne '') {
! 128: chdir('/etc/apache2/sites-enabled');
! 129: symlink('../sites-available/loncapa.conf','loncapa.conf');
! 130: chdir($currdir);
! 131: }
1.55 raeburn 132: }
133: }
134: }
135: }
136:
1.1 harris41 137: unless (-e "<TARGET />") {
1.20 raeburn 138: print '**** ERROR! <TARGET /> should exist! Are you missing the Apache '.
1.13 harris41 139: 'software package?';
1.11 harris41 140: exit(1);
1.1 harris41 141: }
142: else {
1.32 raeburn 143: # Append loncapa_apache.conf inclusion to httpd.conf
1.55 raeburn 144: # (or sites-available/loncapa or conf-available/loncapa.conf) if not present.
1.9 harris41 145: $flag=0;
1.11 harris41 146: open(IN,'<<TARGET />');
1.9 harris41 147: while (<IN>) {
148: if (/^\s*Include\s+conf\/loncapa_apache.conf/) {
149: $flag=1;
150: }
151: }
1.11 harris41 152: close(IN);
1.9 harris41 153: unless ($flag==1) {
1.11 harris41 154: open(OUT,'>><TARGET />');
155: print(OUT 'Include conf/loncapa_apache.conf'."\n");
156: close(OUT);
1.9 harris41 157: }
1.39 raeburn 158: # Remove loncapa.conf inclusion from httpd.conf
1.55 raeburn 159: # (or sites-available/loncapa or conf-available/loncapa.conf) if present.
1.11 harris41 160: $flag=0;
161: open(IN,'<<TARGET />');
162: while (<IN>) {
163: if (/^\s*Include\s+conf\/loncapa.conf/) {
164: $flag=1;
165: }
166: }
167: close(IN);
168: $in='';
169: if ($flag==1) {
170: open(IN,'<<TARGET />');
171: while(<IN>) {
172: $in.=$_ unless /^\s*Include\s+conf\/loncapa.conf/;
173: }
174: close(IN);
175: open(OUT,'><TARGET />');
176: print(OUT $in."\n");
177: close(OUT);
1.13 harris41 178: }
1.18 raeburn 179:
180: # Checking for overlapping ScriptAlias and DocumentRoot definitions.
181: $scriptalias_flag=0;
182: $documentroot_flag=0;
183: my $scriptalias;
184: my $documentroot;
185: open(IN,'<<TARGET />');
186: while (<IN>) {
187: if (m!^\s*ScriptAlias\s+/cgi-bin/\s+(.*)$!) {
188: $scriptalias = $1;
189: if ($scriptalias !~ m!home/httpd/cgi-bin!) {
190: $scriptalias_flag = 1;
191: }
192: }
193: if (m!^\s*DocumentRoot\s+(.*)$!) {
194: $documentroot = $1;
195: if ($documentroot !~ m!home/httpd/html!) {
196: $documentroot_flag = 1;
197: }
198: }
199: }
200: close(IN);
201: if ($scriptalias_flag==1) {
1.32 raeburn 202: my $conffile = '/etc/httpd/conf/httpd.conf';
203: if ('<DIST />' eq 'suse9.2' || '<DIST />' eq 'suse9.3'
204: || '<DIST />' eq 'sles9') {
205: $conffile = '/etc/httpd/httpd.conf';
206: } elsif ('<DIST />' =~ /^(suse|sles)/) {
207: $conffile = '/etc/apache2/default-server.conf';
208: } elsif ('<DIST />' =~ /^(debian|ubuntu)/) {
209: $conffile = '/etc/apache2/sites-available/loncapa';
1.55 raeburn 210: if ('<DIST />' =~ /^ubuntu(\d+)$/) {
211: my $version = $1;
212: if ($version > 12) {
213: $conffile = '/etc/apache2/conf-available/loncapa.conf';
214: }
215: }
1.32 raeburn 216: }
217: print('**** ERROR **** '.$conffile.' has an overlapping definition of '.
218: 'ScriptAlias (it is incorrectly set to '.$scriptalias.').'."\n".
219: 'This conflicts with loncapa_apache.conf.'."\n");
1.18 raeburn 220: }
221: if ($documentroot_flag==1) {
1.32 raeburn 222: print('**** ERROR **** '.$conffile.' has an overlapping definition of '.
223: 'DocumentRoot (it is incorrectly set to '.$documentroot.').'."\n".
224: 'This conflicts with loncapa_apache.conf.'."\n");
1.18 raeburn 225: }
1.32 raeburn 226:
227: # Checking for rewrites of http:// to https://
228: my $rewrite_dir = '/etc/httpd/conf/rewrites';
229: my $curr_rewrite = '/etc/httpd/conf/loncapa_rewrite.conf';
230: if ('<DIST />' eq 'suse9.2' || '<DIST />' eq 'suse9.3'
231: || '<DIST />' eq 'sles9') {
232: $rewrite_dir = '/etc/httpd/rewrites/';
233: $curr_rewrite = '/etc/httpd/loncapa_rewrite.conf';
234: } elsif ('<DIST />' =~ /^(suse|sles|debian|ubuntu)/) {
235: $rewrite_dir = '/etc/apache2/rewrites';
236: $curr_rewrite = '/etc/apache2/loncapa_rewrite.conf';
237: }
238: my $rewrite_off = $rewrite_dir.'/loncapa_rewrite_off.conf';
239: my $rewrite_on = $rewrite_dir.'/loncapa_rewrite_on.conf';
240: if (!-e $curr_rewrite) {
241: system("cp $rewrite_off $curr_rewrite");
242: chmod(0644, $curr_rewrite);
243: } else {
1.44 raeburn 244: my ($not_rewrite_on,$not_rewrite_off,$rewrite_state);
1.32 raeburn 245: if (open(PIPE, "diff --brief $rewrite_off $curr_rewrite |")) {
246: my $diffres = <PIPE> ;
247: close(PIPE);
248: chomp($diffres);
249: if ($diffres) {
250: $not_rewrite_off = 1;
1.44 raeburn 251: } else {
252: $rewrite_state = 'off';
1.32 raeburn 253: }
254: }
255: if (open(PIPE, "diff --brief $rewrite_on $curr_rewrite |")) {
256: my $diffres = <PIPE> ;
257: close(PIPE);
258: chomp($diffres);
259: if ($diffres) {
260: $not_rewrite_on = 1;
1.44 raeburn 261: } else {
262: $rewrite_state = 'on';
1.32 raeburn 263: }
264: }
1.44 raeburn 265: if ($not_rewrite_off && $not_rewrite_on) {
266: print('**** WARNING **** '."\n".$curr_rewrite.' does not match '.
267: 'either:'."\n".$rewrite_on.' - the file used to enable rewriting '.
268: 'of requests for http:// to https:// '."\n".'or:'."\n".$rewrite_off.
1.32 raeburn 269: ' - the file used to disable such rewriting'."\n\n".
270: 'This may be because '. $curr_rewrite.' has been '.
1.49 raeburn 271: 'previously customized,'."\n".'or it may be because of a change '.
1.32 raeburn 272: 'to the files in '.$rewrite_dir."\n");
1.44 raeburn 273: if (open(my $fh,'<',$curr_rewrite)) {
274: while(<$fh>) {
275: if (/^\s*RewriteEngine\s+(on|off)\s*$/i) {
276: if ($1 eq 'on') {
277: $rewrite_state = 'on';
278: } else {
279: $rewrite_state = 'off';
280: }
281: last;
282: }
283: }
284: }
285: }
286: if ($rewrite_state eq 'on') {
287: # Checking for rewrites of https:// to http://
288: my ($gotrules,$rulestr,$ssldir);
289: if ('<DIST />' eq 'suse9.2' || '<DIST />' eq 'suse9.3'
290: || '<DIST />' eq 'sles9') {
291: $ssldir = '/etc/apache/vhosts.d';
292: } elsif ('<DIST />' =~ /^(suse|sles)/) {
1.50 raeburn 293: $ssldir = '/etc/apache2/vhosts.d';
1.44 raeburn 294: } elsif ('<DIST />' =~ /^(debian|ubuntu)/) {
295: $ssldir = '/etc/apache2/sites-available';
296: } else {
297: $ssldir = '/etc/httpd/conf.d';
298: }
299: my $hostname = Sys::Hostname::FQDN::fqdn();
300: my $hostip = Socket::inet_ntoa(scalar(gethostbyname($hostname)) || 'localhost');
1.53 raeburn 301: my @expected = ('RewriteEngine on',
302: 'RewriteCond %{HTTPS} =on',
303: 'RewriteCond %{REQUEST_URI} ^/adm/wrapper/ext/(?!https:)',
1.46 raeburn 304: 'RewriteCond %{QUERY_STRING} (^|&(|amp;))usehttp=1($|&)',
1.52 raeburn 305: 'RewriteRule ^/adm/wrapper/ext/(?!https:) http://%{HTTP_HOST}%{REQUEST_URI} [R,L,NE]',
1.44 raeburn 306: 'RewriteCond %{REMOTE_ADDR} 127.0.0.1',
307: 'RewriteRule (.*) - [L]');
308: if (($hostip ne '') && ($hostip ne '127.0.0.1')) {
309: push(@expected,('RewriteCond %{REMOTE_ADDR} '.$hostip,
310: 'RewriteRule (.*) - [L]'));
311: }
312: push(@expected,('RewriteCond %{REQUEST_URI} ^/public/.*/syllabus$',
1.46 raeburn 313: 'RewriteCond %{QUERY_STRING} (^|&(|amp;))usehttp=1($|&)',
1.44 raeburn 314: 'RewriteRule ^/public/.*/syllabus$ http://%{HTTP_HOST}%{REQUEST_URI} [R,L,NE]'));
315: if (-d $ssldir) {
316: my @rewrites;
317: if (opendir(my $dir,$ssldir)) {
318: my @sslconf_files;
1.54 raeburn 319: foreach my $file (grep(!/^\./,readdir($dir))) {
320: next if ($file =~ /\.rpmnew$/);
1.44 raeburn 321: if (open(my $fh,'<',"$ssldir/$file")) {
322: while (<$fh>) {
323: if (/^\s*<VirtualHost\s+[^:]*\:443>\s*$/) {
324: push(@sslconf_files,$file);
325: last;
326: }
327: }
328: close($fh);
329: }
330: }
331: if (@sslconf_files) {
332: foreach my $file (@sslconf_files) {
333: if (open(my $fh,'<',"$ssldir/$file")) {
334: my ($rewrite,$num) = (0,0);
335: while (<$fh>) {
336: if ($rewrite) {
1.54 raeburn 337: if (/^\s*<\/IfModule>/) {
1.44 raeburn 338: $rewrite = 0;
339: $num ++;
340: } else {
341: chomp();
1.54 raeburn 342: s/^\s+|\s+$//g;
1.50 raeburn 343: push(@{$rewrites[$num]},$_);
1.44 raeburn 344: }
345: } elsif (/^\s*<IfModule\s+mod_rewrite.c>/) {
346: $rewrite = 1;
347: }
348: }
349: close($fh);
350: }
351: }
352: }
353: closedir($dir);
354: }
355: if (@rewrites) {
356: foreach my $item (@rewrites) {
357: if (ref($item) eq 'ARRAY') {
358: my $found = 0;
1.53 raeburn 359: foreach my $line (@{$item}) {
1.44 raeburn 360: foreach my $match (@expected) {
1.53 raeburn 361: if ($match eq $line) {
1.44 raeburn 362: $found ++;
363: last;
364: }
365: }
366: }
1.54 raeburn 367: if ($found >= scalar(@expected)) {
1.50 raeburn 368: $gotrules = 1;
1.53 raeburn 369: last;
1.44 raeburn 370: }
371: }
372: }
373: }
374: }
375: unless ($gotrules) {
1.49 raeburn 376: print('**** WARNING **** '."\n".$curr_rewrite.' is currently set so rewrites '.
1.44 raeburn 377: 'of http to https are enabled for most URLs.'."\n".
1.49 raeburn 378: 'Unless your Apache configuration includes Strict-Transport-Security '.
379: '(with max-age > 0), it is recommended to also set rewrites from https to http '.
380: 'for specific URLs in a file in '.$ssldir.' by including the following:'."\n".
1.44 raeburn 381: "<IfModule mod_rewrite.c>\n".' '.
382: join("\n ",@expected)."\n".
383: "</IfModule>\n");
384: }
1.32 raeburn 385: }
386: }
1.13 harris41 387: }
388: </perlscript>
1.1 harris41 389: </file>
390: </files>
391: </piml>
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>