1: # The LearningOnline Network with CAPA
2: # Firewall configuration to allow internal LON-CAPA communication between servers
3: #
4: # $Id: Firewall.pm,v 1.6 2010/03/25 01:47:45 raeburn Exp $
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 {
40: my ($iptables,$fw_chains,$lond_port,$iphost,$ports) = @_;
41: return 'inactive firewall' if (!&firewall_is_active());
42: return 'port number unknown' if !$lond_port;
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: }
51: }
52: if (!@okchains) {
53: return 'None of the chain names has the expected format'."\n";
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.
71: my (@port_error,%command_error,@lond_port_open,
72: @lond_port_curropen);
73: if (ref($iphost) eq 'HASH') {
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: }
80: foreach my $key (keys(%{$iphost})) {
81: my $ip = '';
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)) {
84: $ip = "$1.$2.$3.$4";
85: } else {
86: next;
87: }
88: } else {
89: next;
90: }
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: }
110: }
111: }
112: }
113: }
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: }
120: if (@lond_port_open) {
121: unless (grep(/^\Q$port\E$/,@opened)) {
122: push(@opened,$port);
123: }
124: print "Port opened for ".scalar(@lond_port_open)." IP addresses\n";
125: }
126: if (@port_error) {
127: print "Error opening port for following IP addresses: ".join(', ',@port_error)."\n";
128: }
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: }
139: }
140: } else {
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: }
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 {
179: my ($iptables,$fw_chain,$port,$lond_port,$iphost,$curropen) = @_;
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());
185: my $count = 0;
186: if (open(PIPE,"$iptables -L $fw_chain -n 2>/dev/null |")) {
187: while(<PIPE>) {
188: if ($port eq $lond_port) {
189: if (ref($iphost) eq 'HASH') {
190: if (/^ACCEPT\s+tcp\s+\-{2}\s+([\S]+)\s+/) {
191: my $ip = $1;
192: if ($iphost->{$ip}) {
193: $count ++;
194: if (ref($curropen) eq 'HASH') {
195: $curropen->{$ip} ++;
196: }
197: }
198: }
199: }
200: } else {
201: if (/tcp dpt\:\Q$port\E/) {
202: $count ++;
203: last;
204: }
205: }
206: }
207: close(PIPE);
208: }
209: return $count;
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 {
221: my ($iptables,$fw_chains,$lond_port,$ports) = @_;
222: return 'inactive firewall' if (!&firewall_is_active());
223: return 'port number unknown' if !$lond_port;
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";
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)) {
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+/) {
257: $to_close{$1} = $port;
258: }
259: }
260: close(PIPE);
261: }
262: if (keys(%to_close) > 0) {
263: foreach my $ip (keys(%to_close)) {
264: my $firewall_command =
265: "$iptables -D $fw_chain -p tcp -s $ip -d 0/0 --dport $port -j ACCEPT";
266: system($firewall_command);
267: my $return_status = $?>>8;
268: if ($return_status == 1) {
269: push (@port_error,$ip);
270: } elsif ($return_status == 2) {
271: push(@command_error,$ip);
272: } elsif ($return_status == 0) {
273: push(@lond_port_close,$ip);
274: }
275: }
276: }
277: if (@lond_port_close) {
278: print "Port closed for ".scalar(@lond_port_close)." IP addresses\n";
279: }
280: if (@port_error) {
281: print "Error closing port for following IP addresses: ".join(', ',@port_error)."\n";
282: }
283: if (@command_error) {
284: print "Bad command error opening port for following IP addresses: ".
285: join(', ',@command_error)."\n".
286: 'Command was: "'."$iptables -D $fw_chain -p tcp -s ".'$ip'." -d 0/0 --dport $port -j ACCEPT".'", where $ip is IP address'."\n";
287: }
288: }
289: } else {
290: foreach my $fw_chain (@okchains) {
291: my (@port_error,@command_error,@lond_port_close);
292: my $to_close;
293: if (open(PIPE, "$iptables -n -L $fw_chain |")) {
294: while (<PIPE>) {
295: chomp();
296: next unless (/dpt:\Q$port\E\s*$/);
297: $to_close = 1;
298: }
299: close(PIPE);
300: }
301: if ($to_close) {
302: my $firewall_command =
303: "$iptables -D $fw_chain -p tcp -d 0/0 --dport $port -j ACCEPT";
304: system($firewall_command);
305: my $return_status = $?>>8;
306: if ($return_status == 1) {
307: # Error
308: print "Error closing port for chain: $fw_chain.\n";
309: } elsif ($return_status == 2) {
310: # Bad command
311: print "Bad command error closing port. Command was\n".
312: " ".$firewall_command."\n";
313: } else {
314: print "Port closed for chain $fw_chain.\n";
315: }
316: }
317: }
318: }
319: }
320: return;
321: }
322:
323: sub firewall_close_anywhere {
324: my ($iptables,$fw_chain,$port) = @_;
325: if (open(PIPE, "$iptables --line-numbers -n -L $fw_chain |")) {
326: while (<PIPE>) {
327: next unless (/dpt:\Q$port\E/);
328: chomp();
329: if (/^(\d+)\s+ACCEPT\s+tcp\s+\-{2}\s+0\.0\.0\.0\/0\s+0\.0\.0\.0\/0/) {
330: my $firewall_command = "$iptables -D $fw_chain $1";
331: system($firewall_command);
332: my $return_status = $?>>8;
333: if ($return_status == 1) {
334: print 'Error closing port '.$port.' for source "anywhere"'."\n";
335: } elsif ($return_status == 2) {
336: print 'Bad command error closing port '.$port.' for source "anywhere". Command was'."\n".
337: ' '.$firewall_command."\n";
338: } else {
339: print 'Port '.$port.' closed for source "anywhere"'."\n";
340: }
341: }
342: }
343: close(PIPE);
344: }
345: }
346:
347: sub get_lond_port {
348: my $perlvarref=&LONCAPA::Configuration::read_conf();
349: my $lond_port;
350: if (ref($perlvarref) eq 'HASH') {
351: if (defined($perlvarref->{'londPort'})) {
352: $lond_port = $perlvarref->{'londPort'};
353: }
354: }
355: if (!$lond_port) {
356: print("Unable to determine lond port number from LON-CAPA configuration.\n");
357: }
358: return $lond_port;
359: }
360:
361: sub get_fw_chains {
362: my ($iptables) = @_;
363: my @fw_chains;
364: my $suse_config = "/etc/sysconfig/SuSEfirewall2";
365: if (-e $suse_config) {
366: push(@fw_chains,'input_ext');
367: } else {
368: if (!-e '/etc/sysconfig/iptables') {
369: if (!-e '/var/lib/iptables') {
370: print("Unable to find iptables file containing static definitions\n");
371: }
372: push(@fw_chains,'RH-Firewall-1-INPUT');
373: }
374: if ($iptables eq '') {
375: $iptables = &get_pathto_iptables();
376: }
377: my %counts;
378: my @posschains = ('RH-Firewall-1-INPUT','INPUT');
379: if (open(PIPE,"$iptables -L -n |")) {
380: while(<PIPE>) {
381: foreach my $chain (@posschains) {
382: if (/(\Q$chain\E)/) {
383: $counts{$1} ++;
384: }
385: }
386: }
387: close(PIPE);
388: }
389: foreach my $fw_chain (@posschains) {
390: if ($counts{$fw_chain}) {
391: push(@fw_chains,$fw_chain);
392: }
393: }
394: }
395: return @fw_chains;
396: }
397:
398: sub get_pathto_iptables {
399: my $iptables;
400: if (-e '/sbin/iptables') {
401: $iptables = '/sbin/iptables';
402: } elsif (-e '/usr/sbin/iptables') {
403: $iptables = '/usr/sbin/iptables';
404: } else {
405: print("Unable to find iptables command\n");
406: }
407: return $iptables;
408: }
409:
410: 1;
411: __END__
412:
413: =pod
414:
415: =head1 NAME
416:
417: B<LONCAPA::Firewall> - dynamic opening/closing of firewall ports
418:
419: =head1 SYNOPSIS
420:
421: use lib '/home/httpd/lib/perl/';
422: use LONCAPA::Firewall;
423:
424: LONCAPA::Firewall::firewall_open_port();
425: LONCAPA::Firewall::firewall_close_port();
426: LONCAPA::Firewall::firewall_is_port_open();
427: LONCAPA::Firewall::firewall_is_active();
428: LONCAPA::Firewall::firewall_close_anywhere();
429:
430: =head1 DESCRIPTION
431:
432: The scripts: /etc/init.d/loncontrol, used to stop or start LON-CAPA services,
433: as well as the setuid script /home/httpd/perl/lciptables, called by loncron
434: for housekeeping tasks, make use of the methods provided by this module to
435: open and close firewall ports (currently the default port: 5663), used
436: for socket-based communication between LON-CAPA servers in the cluster
437: of networked servers to which the server belongs.
438:
439: The following methods are available:
440:
441: =over 4
442:
443: =item LONCAPA::Firewall::firewall_open_port( $iptables,$fw_chains,$lond_port,$iphost,$port );
444:
445: =back
446:
447: =over 4
448:
449: =item LONCAPA::Firewall::firewall_close_port( $iptables,$fw_chains,$lond_port,$ports );
450:
451: =back
452:
453: =over 4
454:
455: =item LONCAPA::Firewall::firewall_is_port_open( $iptables,$fw_chain,$port,$lond_port,$iphost,$curropen );
456:
457: =back
458:
459: =over 4
460:
461: =item LONCAPA::Firewall::firewall_is_active();
462:
463: =back
464:
465: =over 4
466:
467: =item LONCAPA::Firewall::firewall_close_anywhere( $iptables,$fw_chain,$port );
468:
469: =back
470:
471: =over 4
472:
473: =item LONCAPA::Firewall::get_lond_port();
474:
475: =back
476:
477: =over 4
478:
479: =item LONCAPA::Firewall::get_fw_chains();
480:
481: =back
482:
483: =over 4
484:
485: =item LONCAPA::Firewall::get_pathto_iptables();
486:
487:
488: =head1 AUTHORS
489:
490: This library is free software; you can redistribute it and/or
491: modify it under the same terms as LON-CAPA itself.
492:
493: =cut
494:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>