Annotation of loncom/configuration/Firewall.pm, revision 1.10
1.1 raeburn 1: # The LearningOnline Network with CAPA
2: # Firewall configuration to allow internal LON-CAPA communication between servers
3: #
1.10 ! raeburn 4: # $Id: Firewall.pm,v 1.9 2011/04/26 03:43:17 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.9 raeburn 372: my $distro;
1.10 ! raeburn 373: if (open(PIPE,"/home/httpd/perl/distprobe|")) {
! 374: $distro = <PIPE>;
! 375: close(PIPE);
1.9 raeburn 376: }
1.6 raeburn 377: my @fw_chains;
1.1 raeburn 378: my $suse_config = "/etc/sysconfig/SuSEfirewall2";
1.8 raeburn 379: my $ubuntu_config = "/etc/ufw/ufw.conf";
1.1 raeburn 380: if (-e $suse_config) {
1.6 raeburn 381: push(@fw_chains,'input_ext');
1.1 raeburn 382: } else {
1.8 raeburn 383: my @posschains;
384: if (-e $ubuntu_config) {
385: @posschains = ('ufw-user-input','INPUT');
386: } else {
1.9 raeburn 387: if ($distro =~ /^(debian|ubuntu|suse|sles)/) {
388: @posschains = ('INPUT');
389: } else {
390: @posschains = ('RH-Firewall-1-INPUT','INPUT');
391: }
1.8 raeburn 392: if (!-e '/etc/sysconfig/iptables') {
393: if (!-e '/var/lib/iptables') {
1.9 raeburn 394: unless ($distro =~ /^(debian|ubuntu)/) {
395: print("Unable to find iptables file containing static definitions\n");
396: }
397: }
398: if ($distro =~ /^(fedora|rhes|centos|scientific)/) {
399: push(@fw_chains,'RH-Firewall-1-INPUT');
1.8 raeburn 400: }
1.5 raeburn 401: }
1.1 raeburn 402: }
1.4 raeburn 403: if ($iptables eq '') {
404: $iptables = &get_pathto_iptables();
405: }
1.6 raeburn 406: my %counts;
407: if (open(PIPE,"$iptables -L -n |")) {
408: while(<PIPE>) {
409: foreach my $chain (@posschains) {
410: if (/(\Q$chain\E)/) {
411: $counts{$1} ++;
412: }
413: }
414: }
415: close(PIPE);
416: }
417: foreach my $fw_chain (@posschains) {
418: if ($counts{$fw_chain}) {
1.8 raeburn 419: unless(grep(/^\Q$fw_chain\E$/,@fw_chains)) {
420: push(@fw_chains,$fw_chain);
421: }
1.6 raeburn 422: }
1.3 raeburn 423: }
1.1 raeburn 424: }
1.6 raeburn 425: return @fw_chains;
1.1 raeburn 426: }
427:
428: sub get_pathto_iptables {
429: my $iptables;
430: if (-e '/sbin/iptables') {
431: $iptables = '/sbin/iptables';
432: } elsif (-e '/usr/sbin/iptables') {
433: $iptables = '/usr/sbin/iptables';
434: } else {
435: print("Unable to find iptables command\n");
436: }
437: return $iptables;
438: }
439:
440: 1;
441: __END__
442:
443: =pod
444:
445: =head1 NAME
446:
447: B<LONCAPA::Firewall> - dynamic opening/closing of firewall ports
448:
449: =head1 SYNOPSIS
450:
451: use lib '/home/httpd/lib/perl/';
452: use LONCAPA::Firewall;
453:
454: LONCAPA::Firewall::firewall_open_port();
455: LONCAPA::Firewall::firewall_close_port();
456: LONCAPA::Firewall::firewall_is_port_open();
457: LONCAPA::Firewall::firewall_is_active();
458: LONCAPA::Firewall::firewall_close_anywhere();
459:
460: =head1 DESCRIPTION
461:
462: The scripts: /etc/init.d/loncontrol, used to stop or start LON-CAPA services,
463: as well as the setuid script /home/httpd/perl/lciptables, called by loncron
464: for housekeeping tasks, make use of the methods provided by this module to
465: open and close firewall ports (currently the default port: 5663), used
466: for socket-based communication between LON-CAPA servers in the cluster
467: of networked servers to which the server belongs.
468:
469: The following methods are available:
470:
471: =over 4
472:
1.6 raeburn 473: =item LONCAPA::Firewall::firewall_open_port( $iptables,$fw_chains,$lond_port,$iphost,$port );
1.1 raeburn 474:
475: =back
476:
477: =over 4
478:
1.7 raeburn 479: =item LONCAPA::Firewall::firewall_close_port( $iptables,$fw_chains,$lond_port,$iphost,$ports );
1.1 raeburn 480:
481: =back
482:
483: =over 4
484:
1.6 raeburn 485: =item LONCAPA::Firewall::firewall_is_port_open( $iptables,$fw_chain,$port,$lond_port,$iphost,$curropen );
1.1 raeburn 486:
487: =back
488:
489: =over 4
490:
491: =item LONCAPA::Firewall::firewall_is_active();
492:
493: =back
494:
495: =over 4
496:
497: =item LONCAPA::Firewall::firewall_close_anywhere( $iptables,$fw_chain,$port );
498:
499: =back
500:
501: =over 4
502:
503: =item LONCAPA::Firewall::get_lond_port();
504:
505: =back
506:
507: =over 4
508:
1.6 raeburn 509: =item LONCAPA::Firewall::get_fw_chains();
1.1 raeburn 510:
511: =back
512:
513: =over 4
514:
515: =item LONCAPA::Firewall::get_pathto_iptables();
516:
517:
518: =head1 AUTHORS
519:
520: This library is free software; you can redistribute it and/or
521: modify it under the same terms as LON-CAPA itself.
522:
523: =cut
524:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>