Annotation of loncom/configuration/Firewall.pm, revision 1.8
1.1 raeburn 1: # The LearningOnline Network with CAPA
2: # Firewall configuration to allow internal LON-CAPA communication between servers
3: #
1.8 ! raeburn 4: # $Id: Firewall.pm,v 1.7 2010/12/30 18:40:29 raeburn Exp $
1.1 raeburn 5: #
6: # The LearningOnline Network with CAPA
7: #
8: # Copyright Michigan State University Board of Trustees
9: #
10: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
11: #
12: # LON-CAPA is free software; you can redistribute it and/or modify
13: # it under the terms of the GNU General Public License as published by
14: # the Free Software Foundation; either version 2 of the License, or
15: # (at your option) any later version.
16: #
17: # LON-CAPA is distributed in the hope that it will be useful,
18: # but WITHOUT ANY WARRANTY; without even the implied warranty of
19: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20: # GNU General Public License for more details.
21: #
22: # You should have received a copy of the GNU General Public License
23: # along with LON-CAPA; if not, write to the Free Software
24: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25: #
26: # /home/httpd/html/adm/gpl.txt
27: #
28: # http://www.lon-capa.org/
29: #
30: # Startup script for the LON-CAPA network processes
31: #
32:
33: package LONCAPA::Firewall;
34:
35: use strict;
36: use lib '/home/httpd/perl/lib';
37: use LONCAPA::Configuration;
38:
39: sub firewall_open_port {
1.6 raeburn 40: my ($iptables,$fw_chains,$lond_port,$iphost,$ports) = @_;
1.1 raeburn 41: return 'inactive firewall' if (!&firewall_is_active());
42: return 'port number unknown' if !$lond_port;
1.6 raeburn 43: return 'invalid firewall chain' unless (ref($fw_chains) eq 'ARRAY');
44: my (@opened,@chains,@badchains,@okchains);
45: foreach my $chain (@{$fw_chains}) {
46: if ($chain =~ /^([\w\-]+)$/) {
47: push(@okchains,$1);
48: } else {
49: push(@badchains,$chain);
50: }
1.1 raeburn 51: }
1.6 raeburn 52: if (!@okchains) {
53: return 'None of the chain names has the expected format'."\n";
1.1 raeburn 54: }
55: if (ref($ports) ne 'ARRAY') {
56: return 'List of ports to open needed.';
57: }
58: foreach my $portnum (@{$ports}) {
59: my $port = '';
60: if ($portnum =~ /^(\d+)$/) {
61: $port = $1;
62: } else {
63: print "Skipped non-numeric port: $portnum\n";
64: next;
65: }
66: print "Opening firewall access on port $port.\n";
67: my $result;
68: if ($port eq $lond_port) {
69: # For lond port, restrict the servers allowed to attempt to communicate
70: # to include only source IPs in the LON-CAPA cluster.
1.6 raeburn 71: my (@port_error,%command_error,@lond_port_open,
72: @lond_port_curropen);
1.1 raeburn 73: if (ref($iphost) eq 'HASH') {
1.6 raeburn 74: if (keys(%{$iphost}) > 0) {
75: my %curropen;
76: foreach my $fw_chain (@okchains) {
77: &firewall_close_anywhere($iptables,$fw_chain,$port);
78: my $current = &firewall_is_port_open($iptables,$fw_chain,$port,$lond_port,$iphost,\%curropen);
79: }
1.1 raeburn 80: foreach my $key (keys(%{$iphost})) {
81: my $ip = '';
1.2 raeburn 82: if ($key =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/) {
83: if (($1<=255) && ($2<=255) && ($3<=255) && ($4<=255)) {
1.1 raeburn 84: $ip = "$1.$2.$3.$4";
85: } else {
86: next;
87: }
88: } else {
89: next;
90: }
1.6 raeburn 91: if ($curropen{$ip}) {
92: push(@lond_port_curropen,$ip);
93: } else {
94: foreach my $fw_chain (@okchains) {
95: my $firewall_command =
96: "$iptables -I $fw_chain -p tcp -s $ip -d 0/0 --dport $port -j ACCEPT";
97: system($firewall_command);
98: my $return_status = $?>>8;
99: if ($return_status == 1) {
100: unless(grep(/^\Q$ip\E$/,@port_error)) {
101: push (@port_error,$ip);
102: }
103: } elsif ($return_status == 2) {
104: push(@{$command_error{$fw_chain}},$ip);
105: } elsif ($return_status == 0) {
106: push(@lond_port_open,$ip);
107: last;
108: }
109: }
1.1 raeburn 110: }
111: }
112: }
113: }
1.6 raeburn 114: if (@lond_port_curropen) {
115: unless (grep(/^\Q$port\E$/,@opened)) {
116: push(@opened,$port);
117: }
118: print "Port already open for ".scalar(@lond_port_curropen)." IP addresses\n";
119: }
1.1 raeburn 120: if (@lond_port_open) {
1.6 raeburn 121: unless (grep(/^\Q$port\E$/,@opened)) {
122: push(@opened,$port);
123: }
124: print "Port opened for ".scalar(@lond_port_open)." IP addresses\n";
1.1 raeburn 125: }
126: if (@port_error) {
1.6 raeburn 127: print "Error opening port for following IP addresses: ".join(', ',@port_error)."\n";
1.1 raeburn 128: }
1.6 raeburn 129: if (keys(%command_error) > 0) {
130: foreach my $chain (sort(keys(%command_error))) {
131: if (ref($command_error{$chain}) eq 'ARRAY') {
132: if (@{$command_error{$chain}}) {
133: print "Bad command error opening port for following IP addresses: ".
134: join(', ',@{$command_error{$chain}})."\n".
135: 'Command was: "'."$iptables -I $chain -p tcp -s ".'$ip'." -d 0/0 --dport $port -j ACCEPT".'", where $ip is IP address'."\n";
136: }
137: }
138: }
1.1 raeburn 139: }
140: } else {
1.6 raeburn 141: my (@port_errors,%command_errors);
142: foreach my $fw_chain (@okchains) {
143: my $firewall_command =
144: "$iptables -I $fw_chain -p tcp -d 0/0 --dport $port -j ACCEPT";
145: system($firewall_command);
146: my $return_status = $?>>8;
147: if ($return_status == 1) {
148: push(@port_errors,$fw_chain);
149: } elsif ($return_status == 2) {
150: $command_errors{$fw_chain} = $firewall_command;
151: } elsif ($return_status == 0) {
152: push(@opened,$port);
153: last;
154: }
155: }
156: unless (grep(/^\Q$port\E$/,@opened)) {
157: if (@port_errors) {
158: print "Error opening port for chains: ".
159: join(', ',@port_errors).".\n";
160: }
161: if (keys(%command_errors)) {
162: foreach my $fw_chain (sort(keys(%command_errors))) {
163: print "Bad command error opening port for chain: $fw_chain. Command was\n".
164: " ".$command_errors{$fw_chain}."\n";
165: }
166: }
1.1 raeburn 167: }
168: }
169: }
170: foreach my $port (@{$ports}) {
171: if (!grep(/^\Q$port\E$/,@opened)) {
172: return 'Required port not open: '.$port."\n";
173: }
174: }
175: return 'ok';
176: }
177:
178: sub firewall_is_port_open {
1.6 raeburn 179: my ($iptables,$fw_chain,$port,$lond_port,$iphost,$curropen) = @_;
1.1 raeburn 180: # for lond port returns number of source IPs for which firewall port is open
181: # for other ports returns 1 if the firewall port is open, 0 if not.
182: #
183: # check if firewall is active or installed
184: return if (! &firewall_is_active());
1.6 raeburn 185: my $count = 0;
1.7 raeburn 186: if (open(PIPE,"$iptables -L $fw_chain -n |")) {
1.6 raeburn 187: while(<PIPE>) {
188: if ($port eq $lond_port) {
189: if (ref($iphost) eq 'HASH') {
1.7 raeburn 190: if (/^ACCEPT\s+tcp\s+\-{2}\s+(\S+)\s+\S+\s+tcp\s+dpt\:\Q$port\E/) {
1.6 raeburn 191: my $ip = $1;
192: if ($iphost->{$ip}) {
193: $count ++;
194: if (ref($curropen) eq 'HASH') {
195: $curropen->{$ip} ++;
196: }
197: }
1.1 raeburn 198: }
1.6 raeburn 199: }
200: } else {
201: if (/tcp dpt\:\Q$port\E/) {
202: $count ++;
203: last;
1.1 raeburn 204: }
205: }
206: }
1.6 raeburn 207: close(PIPE);
1.1 raeburn 208: }
1.6 raeburn 209: return $count;
1.1 raeburn 210: }
211:
212: sub firewall_is_active {
213: if (-e '/proc/net/ip_tables_names') {
214: return 1;
215: } else {
216: return 0;
217: }
218: }
219:
220: sub firewall_close_port {
1.7 raeburn 221: my ($iptables,$fw_chains,$lond_port,$iphost,$ports) = @_;
1.1 raeburn 222: return 'inactive firewall' if (!&firewall_is_active());
223: return 'port number unknown' if !$lond_port;
1.6 raeburn 224: return 'invalid firewall chain' unless (ref($fw_chains) eq 'ARRAY');
225: my (@opened,@chains,@badchains,@okchains);
226: foreach my $chain (@{$fw_chains}) {
227: if ($chain =~ /^([\w\-]+)$/) {
228: push(@okchains,$1);
229: } else {
230: push(@badchains,$chain);
231: }
232: }
233: if (!@okchains) {
234: return 'None of the chain names has the expected format'."\n";
1.1 raeburn 235: }
236: if (ref($ports) ne 'ARRAY') {
237: return 'List of ports to close needed.';
238: }
239: foreach my $portnum (@{$ports}) {
240: my $port = '';
241: if ($portnum =~ /^(\d+)$/) {
242: $port = $1;
243: } else {
244: print "Skipped non-numeric port: $portnum\n";
245: next;
246: }
247: print "Closing firewall access on port $port\n";
248: if (($port ne '') && ($port eq $lond_port)) {
1.6 raeburn 249: foreach my $fw_chain (@okchains) {
250: my (@port_error,@command_error,@lond_port_close);
251: my %to_close;
252: if (open(PIPE, "$iptables -n -L $fw_chain |")) {
253: while (<PIPE>) {
254: chomp();
255: next unless (/dpt:\Q$port\E\s*$/);
256: if (/^ACCEPT\s+tcp\s+\-{2}\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+/) {
1.7 raeburn 257: my $ip = $1;
258: my $keepopen = 0;
259: if (ref($iphost) eq 'HASH') {
260: if (exists($iphost->{$ip})) {
261: $keepopen = 1;
262: }
263: }
264: unless ($keepopen) {
265: $to_close{$ip} = $port;
266: }
1.6 raeburn 267: }
268: }
269: close(PIPE);
270: }
271: if (keys(%to_close) > 0) {
272: foreach my $ip (keys(%to_close)) {
273: my $firewall_command =
274: "$iptables -D $fw_chain -p tcp -s $ip -d 0/0 --dport $port -j ACCEPT";
275: system($firewall_command);
276: my $return_status = $?>>8;
277: if ($return_status == 1) {
278: push (@port_error,$ip);
279: } elsif ($return_status == 2) {
280: push(@command_error,$ip);
281: } elsif ($return_status == 0) {
282: push(@lond_port_close,$ip);
283: }
284: }
285: }
286: if (@lond_port_close) {
287: print "Port closed for ".scalar(@lond_port_close)." IP addresses\n";
288: }
289: if (@port_error) {
290: print "Error closing port for following IP addresses: ".join(', ',@port_error)."\n";
291: }
292: if (@command_error) {
293: print "Bad command error opening port for following IP addresses: ".
294: join(', ',@command_error)."\n".
295: 'Command was: "'."$iptables -D $fw_chain -p tcp -s ".'$ip'." -d 0/0 --dport $port -j ACCEPT".'", where $ip is IP address'."\n";
1.1 raeburn 296: }
297: }
1.6 raeburn 298: } else {
299: foreach my $fw_chain (@okchains) {
300: my (@port_error,@command_error,@lond_port_close);
301: my $to_close;
302: if (open(PIPE, "$iptables -n -L $fw_chain |")) {
303: while (<PIPE>) {
304: chomp();
305: next unless (/dpt:\Q$port\E\s*$/);
306: $to_close = 1;
307: }
308: close(PIPE);
309: }
310: if ($to_close) {
1.1 raeburn 311: my $firewall_command =
1.6 raeburn 312: "$iptables -D $fw_chain -p tcp -d 0/0 --dport $port -j ACCEPT";
1.1 raeburn 313: system($firewall_command);
314: my $return_status = $?>>8;
315: if ($return_status == 1) {
1.6 raeburn 316: # Error
317: print "Error closing port for chain: $fw_chain.\n";
1.1 raeburn 318: } elsif ($return_status == 2) {
1.6 raeburn 319: # Bad command
320: print "Bad command error closing port. Command was\n".
321: " ".$firewall_command."\n";
322: } else {
323: print "Port closed for chain $fw_chain.\n";
1.1 raeburn 324: }
325: }
326: }
327: }
328: }
329: return;
330: }
331:
332: sub firewall_close_anywhere {
333: my ($iptables,$fw_chain,$port) = @_;
1.6 raeburn 334: if (open(PIPE, "$iptables --line-numbers -n -L $fw_chain |")) {
335: while (<PIPE>) {
336: next unless (/dpt:\Q$port\E/);
337: chomp();
338: if (/^(\d+)\s+ACCEPT\s+tcp\s+\-{2}\s+0\.0\.0\.0\/0\s+0\.0\.0\.0\/0/) {
339: my $firewall_command = "$iptables -D $fw_chain $1";
340: system($firewall_command);
341: my $return_status = $?>>8;
342: if ($return_status == 1) {
343: print 'Error closing port '.$port.' for source "anywhere"'."\n";
344: } elsif ($return_status == 2) {
345: print 'Bad command error closing port '.$port.' for source "anywhere". Command was'."\n".
346: ' '.$firewall_command."\n";
347: } else {
348: print 'Port '.$port.' closed for source "anywhere"'."\n";
349: }
1.1 raeburn 350: }
351: }
1.6 raeburn 352: close(PIPE);
1.1 raeburn 353: }
354: }
355:
356: sub get_lond_port {
357: my $perlvarref=&LONCAPA::Configuration::read_conf();
358: my $lond_port;
359: if (ref($perlvarref) eq 'HASH') {
360: if (defined($perlvarref->{'londPort'})) {
361: $lond_port = $perlvarref->{'londPort'};
362: }
363: }
364: if (!$lond_port) {
365: print("Unable to determine lond port number from LON-CAPA configuration.\n");
366: }
367: return $lond_port;
368: }
369:
1.6 raeburn 370: sub get_fw_chains {
1.4 raeburn 371: my ($iptables) = @_;
1.6 raeburn 372: my @fw_chains;
1.1 raeburn 373: my $suse_config = "/etc/sysconfig/SuSEfirewall2";
1.8 ! raeburn 374: my $ubuntu_config = "/etc/ufw/ufw.conf";
1.1 raeburn 375: if (-e $suse_config) {
1.6 raeburn 376: push(@fw_chains,'input_ext');
1.1 raeburn 377: } else {
1.8 ! raeburn 378: my @posschains;
! 379: if (-e $ubuntu_config) {
! 380: @posschains = ('ufw-user-input','INPUT');
! 381: } else {
! 382: @posschains = ('RH-Firewall-1-INPUT','INPUT');
! 383: if (!-e '/etc/sysconfig/iptables') {
! 384: if (!-e '/var/lib/iptables') {
! 385: print("Unable to find iptables file containing static definitions\n");
! 386: }
! 387: push(@fw_chains,'RH-Firewall-1-INPUT');
1.5 raeburn 388: }
1.1 raeburn 389: }
1.4 raeburn 390: if ($iptables eq '') {
391: $iptables = &get_pathto_iptables();
392: }
1.6 raeburn 393: my %counts;
394: if (open(PIPE,"$iptables -L -n |")) {
395: while(<PIPE>) {
396: foreach my $chain (@posschains) {
397: if (/(\Q$chain\E)/) {
398: $counts{$1} ++;
399: }
400: }
401: }
402: close(PIPE);
403: }
404: foreach my $fw_chain (@posschains) {
405: if ($counts{$fw_chain}) {
1.8 ! raeburn 406: unless(grep(/^\Q$fw_chain\E$/,@fw_chains)) {
! 407: push(@fw_chains,$fw_chain);
! 408: }
1.6 raeburn 409: }
1.3 raeburn 410: }
1.1 raeburn 411: }
1.6 raeburn 412: return @fw_chains;
1.1 raeburn 413: }
414:
415: sub get_pathto_iptables {
416: my $iptables;
417: if (-e '/sbin/iptables') {
418: $iptables = '/sbin/iptables';
419: } elsif (-e '/usr/sbin/iptables') {
420: $iptables = '/usr/sbin/iptables';
421: } else {
422: print("Unable to find iptables command\n");
423: }
424: return $iptables;
425: }
426:
427: 1;
428: __END__
429:
430: =pod
431:
432: =head1 NAME
433:
434: B<LONCAPA::Firewall> - dynamic opening/closing of firewall ports
435:
436: =head1 SYNOPSIS
437:
438: use lib '/home/httpd/lib/perl/';
439: use LONCAPA::Firewall;
440:
441: LONCAPA::Firewall::firewall_open_port();
442: LONCAPA::Firewall::firewall_close_port();
443: LONCAPA::Firewall::firewall_is_port_open();
444: LONCAPA::Firewall::firewall_is_active();
445: LONCAPA::Firewall::firewall_close_anywhere();
446:
447: =head1 DESCRIPTION
448:
449: The scripts: /etc/init.d/loncontrol, used to stop or start LON-CAPA services,
450: as well as the setuid script /home/httpd/perl/lciptables, called by loncron
451: for housekeeping tasks, make use of the methods provided by this module to
452: open and close firewall ports (currently the default port: 5663), used
453: for socket-based communication between LON-CAPA servers in the cluster
454: of networked servers to which the server belongs.
455:
456: The following methods are available:
457:
458: =over 4
459:
1.6 raeburn 460: =item LONCAPA::Firewall::firewall_open_port( $iptables,$fw_chains,$lond_port,$iphost,$port );
1.1 raeburn 461:
462: =back
463:
464: =over 4
465:
1.7 raeburn 466: =item LONCAPA::Firewall::firewall_close_port( $iptables,$fw_chains,$lond_port,$iphost,$ports );
1.1 raeburn 467:
468: =back
469:
470: =over 4
471:
1.6 raeburn 472: =item LONCAPA::Firewall::firewall_is_port_open( $iptables,$fw_chain,$port,$lond_port,$iphost,$curropen );
1.1 raeburn 473:
474: =back
475:
476: =over 4
477:
478: =item LONCAPA::Firewall::firewall_is_active();
479:
480: =back
481:
482: =over 4
483:
484: =item LONCAPA::Firewall::firewall_close_anywhere( $iptables,$fw_chain,$port );
485:
486: =back
487:
488: =over 4
489:
490: =item LONCAPA::Firewall::get_lond_port();
491:
492: =back
493:
494: =over 4
495:
1.6 raeburn 496: =item LONCAPA::Firewall::get_fw_chains();
1.1 raeburn 497:
498: =back
499:
500: =over 4
501:
502: =item LONCAPA::Firewall::get_pathto_iptables();
503:
504:
505: =head1 AUTHORS
506:
507: This library is free software; you can redistribute it and/or
508: modify it under the same terms as LON-CAPA itself.
509:
510: =cut
511:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>