Annotation of loncom/configuration/Firewall.pm, revision 1.20
1.1 raeburn 1: # The LearningOnline Network with CAPA
2: # Firewall configuration to allow internal LON-CAPA communication between servers
3: #
1.20 ! raeburn 4: # $Id: Firewall.pm,v 1.19 2019/07/02 19:40:22 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;
1.12 raeburn 38: use LONCAPA;
1.1 raeburn 39:
1.15 raeburn 40: sub uses_firewalld {
41: my ($distro) = @_;
42: if ($distro eq '') {
43: $distro = &get_distro();
44: }
1.18 raeburn 45: my ($inuse,$checkfirewalld);
1.15 raeburn 46: if ($distro =~ /^(suse|sles)([\d\.]+)$/) {
47: if (($1 eq 'sles') && ($2 >= 15)) {
48: $checkfirewalld = 1;
49: }
50: } elsif ($distro =~ /^fedora(\d+)$/) {
51: if ($1 >= 18) {
52: $checkfirewalld = 1;
53: }
1.19 raeburn 54: } elsif ($distro =~ /^(?:centos|rhes|scientific|oracle)(\d+)/) {
1.15 raeburn 55: if ($1 >= 7) {
56: $checkfirewalld = 1;
57: }
58: }
59: if ($checkfirewalld) {
60: my ($loaded,$active);
1.16 raeburn 61: if (open(PIPE,"systemctl status firewalld 2>&1 |")) {
1.15 raeburn 62: while (<PIPE>) {
63: chomp();
64: if (/^\s*Loaded:\s+(\w+)/) {
65: $loaded = $1;
66: }
67: if (/^\s*Active\s+(\w+)/) {
68: $active = $1;
69: }
70: }
71: close(PIPE);
72: }
73: if (($loaded eq 'loaded') || ($active eq 'active')) {
1.17 raeburn 74: $inuse = 1;
1.15 raeburn 75: }
76: }
1.18 raeburn 77: return $inuse;
1.15 raeburn 78: }
79:
1.1 raeburn 80: sub firewall_open_port {
1.18 raeburn 81: my ($iptables,$fw_chains,$lond_port,$iphost,$ports,$firewalld) = @_;
1.1 raeburn 82: return 'inactive firewall' if (!&firewall_is_active());
83: return 'port number unknown' if !$lond_port;
1.6 raeburn 84: return 'invalid firewall chain' unless (ref($fw_chains) eq 'ARRAY');
1.18 raeburn 85: my (@opened,@okchains,$zone);
86: if ($firewalld) {
87: $zone = &get_default_zone();
88: return 'invalid zone' if ($zone eq '');
89: } else {
90: my @badchains;
91: foreach my $chain (@{$fw_chains}) {
92: if ($chain =~ /^([\w\-]+)$/) {
93: push(@okchains,$1);
94: } else {
95: push(@badchains,$chain);
96: }
97: }
98: if (!@okchains) {
99: return 'None of the chain names has the expected format.'."\n";
1.6 raeburn 100: }
1.1 raeburn 101: }
102: if (ref($ports) ne 'ARRAY') {
103: return 'List of ports to open needed.';
104: }
105: foreach my $portnum (@{$ports}) {
106: my $port = '';
107: if ($portnum =~ /^(\d+)$/) {
108: $port = $1;
109: } else {
1.14 bisitz 110: print "Skipped non-numeric port: $portnum.\n";
1.1 raeburn 111: next;
112: }
113: print "Opening firewall access on port $port.\n";
114: my $result;
115: if ($port eq $lond_port) {
116: # For lond port, restrict the servers allowed to attempt to communicate
117: # to include only source IPs in the LON-CAPA cluster.
1.6 raeburn 118: my (@port_error,%command_error,@lond_port_open,
119: @lond_port_curropen);
1.1 raeburn 120: if (ref($iphost) eq 'HASH') {
1.6 raeburn 121: if (keys(%{$iphost}) > 0) {
122: my %curropen;
1.18 raeburn 123: if ($firewalld) {
124: &firewall_close_anywhere($iptables,$zone,$port,$firewalld);
125: my $current = &firewall_is_port_open($iptables,$zone,$port,
126: $lond_port,$iphost,\%curropen,
127: $firewalld);
128: } else {
129: foreach my $fw_chain (@okchains) {
130: &firewall_close_anywhere($iptables,$fw_chain,$port);
131: my $current = &firewall_is_port_open($iptables,$fw_chain,$port,
132: $lond_port,$iphost,\%curropen);
133: }
1.6 raeburn 134: }
1.1 raeburn 135: foreach my $key (keys(%{$iphost})) {
136: my $ip = '';
1.2 raeburn 137: if ($key =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/) {
138: if (($1<=255) && ($2<=255) && ($3<=255) && ($4<=255)) {
1.1 raeburn 139: $ip = "$1.$2.$3.$4";
140: } else {
1.14 bisitz 141: print "IP address: $key does not have expected format.\n";
1.1 raeburn 142: next;
143: }
144: } else {
1.14 bisitz 145: print "IP address: $key does not have expected format.\n";
1.1 raeburn 146: next;
147: }
1.6 raeburn 148: if ($curropen{$ip}) {
149: push(@lond_port_curropen,$ip);
150: } else {
1.18 raeburn 151: if ($firewalld) {
152: my $cmd = 'firewall-cmd --zone='.$zone.' --add-rich-rule \'rule family="ipv4" source address="'.$ip.'/32" port port="'.$port.'" protocol="tcp" accept\'';
153: if (open(PIPE,"$cmd |")) {
154: my $result = <PIPE>;
155: chomp($result);
156: close(PIPE);
157: if ($result eq 'success') {
158: push(@lond_port_open,$ip);
1.15 raeburn 159: } else {
1.18 raeburn 160: push(@port_error,$ip);
1.6 raeburn 161: }
1.18 raeburn 162: }
163: } else {
164: foreach my $fw_chain (@okchains) {
1.15 raeburn 165: my $firewall_command =
166: "$iptables -I $fw_chain -p tcp -s $ip -d 0/0 --dport $port -j ACCEPT";
167: system($firewall_command);
168: my $return_status = $?>>8;
169: if ($return_status == 1) {
170: unless(grep(/^\Q$ip\E$/,@port_error)) {
1.18 raeburn 171: push(@port_error,$ip);
1.15 raeburn 172: }
173: } elsif ($return_status == 2) {
174: push(@{$command_error{$fw_chain}},$ip);
175: } elsif ($return_status == 0) {
176: push(@lond_port_open,$ip);
177: last;
178: }
1.6 raeburn 179: }
180: }
1.1 raeburn 181: }
182: }
1.13 raeburn 183: } else {
184: print "no key found in $iphost hash ref\n";
1.1 raeburn 185: }
1.13 raeburn 186: } else {
187: print "$iphost is not a reference to a hash\n";
1.1 raeburn 188: }
1.6 raeburn 189: if (@lond_port_curropen) {
190: unless (grep(/^\Q$port\E$/,@opened)) {
191: push(@opened,$port);
192: }
1.14 bisitz 193: print "Port already open for ".scalar(@lond_port_curropen)." IP addresses.\n";
1.6 raeburn 194: }
1.1 raeburn 195: if (@lond_port_open) {
1.6 raeburn 196: unless (grep(/^\Q$port\E$/,@opened)) {
197: push(@opened,$port);
198: }
1.14 bisitz 199: print "Port opened for ".scalar(@lond_port_open)." IP addresses.\n";
1.1 raeburn 200: }
201: if (@port_error) {
1.6 raeburn 202: print "Error opening port for following IP addresses: ".join(', ',@port_error)."\n";
1.1 raeburn 203: }
1.6 raeburn 204: if (keys(%command_error) > 0) {
205: foreach my $chain (sort(keys(%command_error))) {
206: if (ref($command_error{$chain}) eq 'ARRAY') {
207: if (@{$command_error{$chain}}) {
208: print "Bad command error opening port for following IP addresses: ".
209: join(', ',@{$command_error{$chain}})."\n".
210: 'Command was: "'."$iptables -I $chain -p tcp -s ".'$ip'." -d 0/0 --dport $port -j ACCEPT".'", where $ip is IP address'."\n";
211: }
212: }
213: }
1.1 raeburn 214: }
215: } else {
1.18 raeburn 216: if ($firewalld) {
217: my ($port_error);
218: my $cmd = 'firewall-cmd --zone='.$zone.' --add-rich-rule \'rule family="ipv4" port port="'.$port.'" protocol="tcp" accept\'';
219: if (open(PIPE,"$cmd |")) {
220: my $result = <PIPE>;
221: chomp($result);
222: close(PIPE);
223: if ($result eq 'success') {
224: push(@opened,$port);
1.15 raeburn 225: } else {
1.18 raeburn 226: $port_error = $port;
1.15 raeburn 227: }
228: } else {
1.18 raeburn 229: $port_error = $port;
230: }
231: if ($port_error) {
232: print "Error opening port: $port\n";
233: }
234: } else {
235: my (@port_errors,%command_errors);
236: foreach my $fw_chain (@okchains) {
1.15 raeburn 237: my $firewall_command =
238: "$iptables -I $fw_chain -p tcp -d 0/0 --dport $port -j ACCEPT";
239: system($firewall_command);
240: my $return_status = $?>>8;
241: if ($return_status == 1) {
242: push(@port_errors,$fw_chain);
243: } elsif ($return_status == 2) {
244: $command_errors{$fw_chain} = $firewall_command;
245: } elsif ($return_status == 0) {
246: push(@opened,$port);
247: last;
248: }
1.6 raeburn 249: }
1.15 raeburn 250: unless (grep(/^\Q$port\E$/,@opened)) {
251: if (@port_errors) {
252: print "Error opening port for chains: ".
253: join(', ',@port_errors).".\n";
254: }
255: if (keys(%command_errors)) {
256: foreach my $fw_chain (sort(keys(%command_errors))) {
257: print "Bad command error opening port for chain: $fw_chain. Command was\n".
258: " ".$command_errors{$fw_chain}."\n";
259: }
1.6 raeburn 260: }
261: }
1.1 raeburn 262: }
263: }
264: }
265: foreach my $port (@{$ports}) {
266: if (!grep(/^\Q$port\E$/,@opened)) {
267: return 'Required port not open: '.$port."\n";
268: }
269: }
270: return 'ok';
271: }
272:
273: sub firewall_is_port_open {
1.18 raeburn 274: my ($iptables,$fw_chain,$port,$lond_port,$iphost,$curropen,$firewalld) = @_;
1.1 raeburn 275: # for lond port returns number of source IPs for which firewall port is open
276: # for other ports returns 1 if the firewall port is open, 0 if not.
1.18 raeburn 277: # if firewalld is in use, checks for rich rules only.
1.6 raeburn 278: my $count = 0;
1.20 ! raeburn 279: # check if firewall is active or installed
1.18 raeburn 280: return $count if (! &firewall_is_active());
281: if ($firewalld) {
282: my $zone = &get_default_zone();
283: return $count if ($zone eq '');
284: if ($port eq $lond_port) {
285: if (open(PIPE,"firewall-cmd --zone=$zone --list-rich-rules |")) {
286: while(<PIPE>) {
287: chomp();
288: if (/\Qrule family="ipv4" source address="\E([\d.]+)\Q\/32" port port="$port" protocol="tcp" accept\E/) {
289: my $ip = $1;
290: if ($iphost->{$ip}) {
291: $count ++;
292: if (ref($curropen) eq 'HASH') {
293: $curropen->{$ip} ++;
294: }
295: }
296: }
297: }
298: close(PIPE);
299: }
300: } else {
301: if (open(PIPE,"firewall-cmd --zone=$zone --list-rich-rules |")) {
302: while(<PIPE>) {
303: if (/\Qrule family="ipv4" port port="$port" protocol="tcp" accept\E/) {
304: $count ++;
305: last;
306: }
307: }
308: close(PIPE);
309: }
310: }
1.20 ! raeburn 311: } elsif (($fw_chain =~ /^[\w-]+$/) && (open(PIPE,"$iptables -L $fw_chain -n |"))) {
1.6 raeburn 312: while(<PIPE>) {
313: if ($port eq $lond_port) {
314: if (ref($iphost) eq 'HASH') {
1.7 raeburn 315: if (/^ACCEPT\s+tcp\s+\-{2}\s+(\S+)\s+\S+\s+tcp\s+dpt\:\Q$port\E/) {
1.6 raeburn 316: my $ip = $1;
317: if ($iphost->{$ip}) {
318: $count ++;
319: if (ref($curropen) eq 'HASH') {
320: $curropen->{$ip} ++;
321: }
322: }
1.1 raeburn 323: }
1.6 raeburn 324: }
1.18 raeburn 325: } elsif (/tcp dpt\:\Q$port\E/) {
326: $count ++;
327: last;
1.1 raeburn 328: }
329: }
1.6 raeburn 330: close(PIPE);
1.1 raeburn 331: }
1.6 raeburn 332: return $count;
1.1 raeburn 333: }
334:
335: sub firewall_is_active {
1.15 raeburn 336: my $status = 0;
1.1 raeburn 337: if (-e '/proc/net/ip_tables_names') {
1.15 raeburn 338: if (open(PIPE,'cat /proc/net/ip_tables_names |')) {
339: while(<PIPE>) {
340: chomp();
341: if (/^filter$/) {
342: $status = 1;
343: last;
344: }
345: }
346: close(PIPE);
347: }
1.1 raeburn 348: }
1.18 raeburn 349: unless ($status) {
350: $status = &uses_firewalld();
351: }
1.15 raeburn 352: return $status;
1.1 raeburn 353: }
354:
355: sub firewall_close_port {
1.18 raeburn 356: my ($iptables,$fw_chains,$lond_port,$iphost,$ports,$firewalld) = @_;
1.1 raeburn 357: return 'inactive firewall' if (!&firewall_is_active());
358: return 'port number unknown' if !$lond_port;
1.6 raeburn 359: return 'invalid firewall chain' unless (ref($fw_chains) eq 'ARRAY');
1.18 raeburn 360: my (@okchains,$zone);
361: if ($firewalld) {
362: $zone = &get_default_zone();
363: return 'no default zone' if ($zone eq '');
364: } else {
365: my @badchains;
366: foreach my $chain (@{$fw_chains}) {
367: if ($chain =~ /^([\w\-]+)$/) {
368: push(@okchains,$1);
369: } else {
370: push(@badchains,$chain);
371: }
372: }
373: if (!@okchains) {
374: return 'None of the chain names has the expected format.'."\n";
1.6 raeburn 375: }
376: }
1.1 raeburn 377: if (ref($ports) ne 'ARRAY') {
378: return 'List of ports to close needed.';
379: }
380: foreach my $portnum (@{$ports}) {
381: my $port = '';
382: if ($portnum =~ /^(\d+)$/) {
383: $port = $1;
384: } else {
385: print "Skipped non-numeric port: $portnum\n";
386: next;
387: }
1.11 raeburn 388: print "Closing firewall access on port $port.\n";
1.1 raeburn 389: if (($port ne '') && ($port eq $lond_port)) {
1.11 raeburn 390: my $output;
1.18 raeburn 391: if ($firewalld) {
392: my (%to_close,@port_error,@lond_port_close);
393: my $cmd = 'firewall-cmd --list-rich-rules';
394: if (open(PIPE,"$cmd |")) {
395: while(<PIPE>) {
396: if (/\Qrule family="ipv4" source address="\E([\d.]+)\Q\/32" port port="$port" protocol="tcp" accept\E/) {
1.7 raeburn 397: my $ip = $1;
398: my $keepopen = 0;
399: if (ref($iphost) eq 'HASH') {
400: if (exists($iphost->{$ip})) {
1.18 raeburn 401: $keepopen = 1;
1.7 raeburn 402: }
403: }
404: unless ($keepopen) {
405: $to_close{$ip} = $port;
406: }
1.6 raeburn 407: }
408: }
409: close(PIPE);
410: }
411: if (keys(%to_close) > 0) {
1.18 raeburn 412: foreach my $ip (sort(keys(%to_close))) {
413: my $cmd = 'firewall-cmd --zone='.$zone.' --remove-rich-rule \'rule family="ipv4" source address="'.$ip.'/32" port port="'.$port.'" protocol="tcp" accept\'';
414: if (open(PIPE,"$cmd |")) {
415: my $result = <PIPE>;
416: chomp($result);
417: close(PIPE);
418: if ($result eq 'success') {
419: push(@lond_port_close,$ip);
1.15 raeburn 420: } else {
421: push(@port_error,$ip);
422: }
423: } else {
1.18 raeburn 424: push(@port_error,$ip);
425: }
426: }
427: }
428: if (@lond_port_close) {
429: $output .= "Port closed for ".scalar(@lond_port_close)." IP addresses.\n";
430: }
431: if (@port_error) {
432: $output .= "Error closing port for following IP addresses: ".join(', ',@port_error)."\n";
433: }
434: } else {
435: foreach my $fw_chain (@okchains) {
436: my (%to_close,@port_error,@command_error,@lond_port_close);
437: if (open(PIPE, "$iptables -n -L $fw_chain |")) {
438: while (<PIPE>) {
439: chomp();
440: next unless (/dpt:\Q$port\E/);
441: if (/^ACCEPT\s+tcp\s+\-{2}\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+/) {
442: my $ip = $1;
443: my $keepopen = 0;
444: if (ref($iphost) eq 'HASH') {
445: if (exists($iphost->{$ip})) {
446: $keepopen = 1;
447: }
448: }
449: unless ($keepopen) {
450: $to_close{$ip} = $port;
451: }
452: }
453: }
454: close(PIPE);
455: }
456: if (keys(%to_close) > 0) {
457: foreach my $ip (keys(%to_close)) {
1.15 raeburn 458: my $firewall_command =
459: "$iptables -D $fw_chain -p tcp -s $ip -d 0/0 --dport $port -j ACCEPT";
460: system($firewall_command);
461: my $return_status = $?>>8;
462: if ($return_status == 1) {
1.18 raeburn 463: push(@port_error,$ip);
1.15 raeburn 464: } elsif ($return_status == 2) {
465: push(@command_error,$ip);
466: } elsif ($return_status == 0) {
467: push(@lond_port_close,$ip);
468: }
1.6 raeburn 469: }
470: }
1.18 raeburn 471: if (@lond_port_close) {
472: $output .= "Port closed for ".scalar(@lond_port_close)." IP addresses.\n";
473: }
474: if (@port_error) {
475: $output .= "Error closing port for following IP addresses: ".join(', ',@port_error)."\n";
476: }
477: if (@command_error) {
478: $output .= "Bad command error opening port for following IP addresses: ".
479: join(', ',@command_error)."\n".
480: 'Command was: "'."$iptables -D $fw_chain -p tcp -s ".'$ip'." -d 0/0 --dport $port -j ACCEPT".'", where $ip is IP address'."\n";
481: }
1.1 raeburn 482: }
483: }
1.11 raeburn 484: if ($output) {
485: print $output;
486: } else {
487: print "No IP addresses required discontinuation of access.\n";
488: }
1.6 raeburn 489: } else {
1.18 raeburn 490: if ($firewalld) {
1.6 raeburn 491: my $to_close;
1.18 raeburn 492: if (open(PIPE,"firewall-cmd --list-rich-rules |")) {
493: while(<PIPE>) {
494: next unless (/\Qrule family="ipv4" port port="$port" protocol="tcp" accept\E/);
1.6 raeburn 495: $to_close = 1;
1.18 raeburn 496: last;
1.6 raeburn 497: }
498: close(PIPE);
499: }
500: if ($to_close) {
1.18 raeburn 501: my $cmd = 'firewall-cmd --zone='.$zone.' --remove-rich-rule \'rule family="ipv4" port port="'.$port.'" protocol="tcp" accept\'';
502: if (open(PIPE,"$cmd|")) {
503: my $result = <PIPE>;
504: chomp($result);
505: close(PIPE);
506: if ($result eq 'success') {
507: print "Port: $port closed in zone: $zone.\n";
1.15 raeburn 508: } else {
1.18 raeburn 509: print "Error closing port: $port in zone: $zone.\n";
1.15 raeburn 510: }
1.6 raeburn 511: } else {
1.18 raeburn 512: print "Error closing port: $port in zone: $zone.\n";
513: }
514: }
515: } else {
516: foreach my $fw_chain (@okchains) {
517: my (@port_error,@command_error,@lond_port_close);
518: my $to_close;
519: if (open(PIPE, "$iptables -n -L $fw_chain |")) {
520: while (<PIPE>) {
521: chomp();
522: next unless (/dpt:\Q$port\E/);
523: $to_close = 1;
524: last;
525: }
526: close(PIPE);
527: }
528: if ($to_close) {
1.15 raeburn 529: my $firewall_command =
530: "$iptables -D $fw_chain -p tcp -d 0/0 --dport $port -j ACCEPT";
531: system($firewall_command);
532: my $return_status = $?>>8;
533: if ($return_status == 1) {
534: # Error
1.18 raeburn 535: print "Error closing port: $port for chain: $fw_chain.\n";
1.15 raeburn 536: } elsif ($return_status == 2) {
537: # Bad command
538: print "Bad command error closing port. Command was\n".
539: " ".$firewall_command."\n";
540: } else {
541: print "Port closed for chain $fw_chain.\n";
542: }
1.1 raeburn 543: }
544: }
545: }
546: }
547: }
548: return;
549: }
550:
551: sub firewall_close_anywhere {
1.18 raeburn 552: my ($iptables,$fw_chain,$port,$firewalld) = @_;
553: my $zone;
554: if ($firewalld) {
555: $zone = &get_default_zone();
556: if ($zone eq '') {
557: print 'no default zone';
558: return;
559: }
560: } else {
561: unless ($fw_chain =~ /^([\w\-]+)$/) {
562: print 'invalid chain';
563: return;
564: }
565: }
566: if ($firewalld) {
567: my $to_close;
568: my $cmd = 'firewall-cmd --list-ports';
569: if (open(PIPE,"$cmd |")) {
570: my $currports = <PIPE>;
571: close(PIPE);
572: chomp($currports);
573: if (grep(/^\Q$port\E\/tcp/,split(/\s+/,$currports))) {
574: $to_close = 1;
575: }
576: }
577: if ($to_close) {
578: my $cmd = 'firewall-cmd --zone='.$zone.' --remove-port='.$port.'/tcp';
579: if (open(PIPE,"$cmd |")) {
580: my $result = <PIPE>;
581: chomp($result);
582: close(PIPE);
583: if ($result eq 'success') {
584: print 'Port '.$port.' closed for source "anywhere"'."\n";
585: } else {
586: print 'Error closing port '.$port.' for source "anywhere".'."\n";
587: }
588: } else {
589: print 'Error closing port '.$port.' for source "anywhere".'."\n";
590: }
591: }
592: } elsif (open(PIPE, "$iptables --line-numbers -n -L $fw_chain |")) {
1.6 raeburn 593: while (<PIPE>) {
594: next unless (/dpt:\Q$port\E/);
595: chomp();
596: if (/^(\d+)\s+ACCEPT\s+tcp\s+\-{2}\s+0\.0\.0\.0\/0\s+0\.0\.0\.0\/0/) {
1.18 raeburn 597: my $firewall_command = "$iptables -D $fw_chain $1";
598: system($firewall_command);
599: my $return_status = $?>>8;
600: if ($return_status == 1) {
601: print 'Error closing port '.$port.' for source "anywhere".'."\n";
602: } elsif ($return_status == 2) {
603: print 'Bad command error closing port '.$port.' for source "anywhere". Command was'."\n".
604: ' '.$firewall_command."\n";
1.6 raeburn 605: } else {
1.18 raeburn 606: print 'Port '.$port.' closed for source "anywhere"'."\n";
1.6 raeburn 607: }
1.1 raeburn 608: }
609: }
1.6 raeburn 610: close(PIPE);
1.1 raeburn 611: }
612: }
613:
614: sub get_lond_port {
615: my $perlvarref=&LONCAPA::Configuration::read_conf();
616: my $lond_port;
617: if (ref($perlvarref) eq 'HASH') {
618: if (defined($perlvarref->{'londPort'})) {
619: $lond_port = $perlvarref->{'londPort'};
620: }
621: }
622: if (!$lond_port) {
623: print("Unable to determine lond port number from LON-CAPA configuration.\n");
624: }
625: return $lond_port;
626: }
627:
1.6 raeburn 628: sub get_fw_chains {
1.15 raeburn 629: my ($iptables,$distro) = @_;
630: if ($distro eq '') {
631: $distro = &get_distro();
632: }
1.6 raeburn 633: my @fw_chains;
1.1 raeburn 634: my $suse_config = "/etc/sysconfig/SuSEfirewall2";
1.8 raeburn 635: my $ubuntu_config = "/etc/ufw/ufw.conf";
1.18 raeburn 636: my $firewalld = &uses_firewalld($distro);
1.17 raeburn 637: if ($firewalld) {
1.18 raeburn 638: my ($dist,$version) = ($distro =~ /^([\D]+)(\d+)$/);
1.19 raeburn 639: if (((($dist eq 'rhes') || ($dist eq 'centos')) &&
640: ($version >= 8)) || (($dist eq 'oracle') && ($version >= 7))) {
1.18 raeburn 641: push(@fw_chains,'INPUT');
1.17 raeburn 642: } else {
1.18 raeburn 643: my $zone = &get_default_zone();
644: if ($zone ne '') {
645: push(@fw_chains,'IN_'.$zone.'_allow');
646: } else {
647: push(@fw_chains,'IN_public_allow');
648: }
649: }
1.15 raeburn 650: } elsif (-e $suse_config) {
1.6 raeburn 651: push(@fw_chains,'input_ext');
1.1 raeburn 652: } else {
1.8 raeburn 653: my @posschains;
654: if (-e $ubuntu_config) {
655: @posschains = ('ufw-user-input','INPUT');
656: } else {
1.9 raeburn 657: if ($distro =~ /^(debian|ubuntu|suse|sles)/) {
658: @posschains = ('INPUT');
1.19 raeburn 659: } elsif ($distro =~ /^(fedora|rhes|centos|scientific|oracle)(\d+)$/) {
1.15 raeburn 660: if ((($1 eq 'fedora') && ($2 > 15)) || (($1 ne 'fedora') && ($2 >= 7))) {
661: @posschains = ('INPUT');
662: } else {
663: @posschains = ('RH-Firewall-1-INPUT','INPUT');
664: }
1.9 raeburn 665: }
1.8 raeburn 666: if (!-e '/etc/sysconfig/iptables') {
667: if (!-e '/var/lib/iptables') {
1.9 raeburn 668: unless ($distro =~ /^(debian|ubuntu)/) {
1.14 bisitz 669: print("Unable to find iptables file containing static definitions.\n");
1.9 raeburn 670: }
671: }
1.19 raeburn 672: if ($distro =~ /^(fedora|rhes|centos|scientific|oracle)(\d+)$/) {
1.15 raeburn 673: unless ((($1 eq 'fedora') && ($2 > 15)) || (($1 ne 'fedora') && ($2 >= 7))) {
674: push(@fw_chains,'RH-Firewall-1-INPUT');
675: }
1.8 raeburn 676: }
1.5 raeburn 677: }
1.1 raeburn 678: }
1.4 raeburn 679: if ($iptables eq '') {
680: $iptables = &get_pathto_iptables();
681: }
1.6 raeburn 682: my %counts;
683: if (open(PIPE,"$iptables -L -n |")) {
684: while(<PIPE>) {
685: foreach my $chain (@posschains) {
686: if (/(\Q$chain\E)/) {
687: $counts{$1} ++;
688: }
689: }
690: }
691: close(PIPE);
692: }
693: foreach my $fw_chain (@posschains) {
694: if ($counts{$fw_chain}) {
1.8 raeburn 695: unless(grep(/^\Q$fw_chain\E$/,@fw_chains)) {
696: push(@fw_chains,$fw_chain);
697: }
1.6 raeburn 698: }
1.3 raeburn 699: }
1.1 raeburn 700: }
1.6 raeburn 701: return @fw_chains;
1.1 raeburn 702: }
703:
1.18 raeburn 704: sub get_default_zone {
705: my $cmd = 'firewall-cmd --get-default-zone';
706: my $zone;
707: if (open(PIPE,"$cmd |")) {
708: my $result = <PIPE>;
709: chomp($result);
710: close(PIPE);
711: ($zone) = ($result =~ /^(\w+)$/);
712: }
713: return $zone;
714: }
715:
1.1 raeburn 716: sub get_pathto_iptables {
717: my $iptables;
718: if (-e '/sbin/iptables') {
719: $iptables = '/sbin/iptables';
720: } elsif (-e '/usr/sbin/iptables') {
721: $iptables = '/usr/sbin/iptables';
722: } else {
1.14 bisitz 723: print("Unable to find iptables command.\n");
1.1 raeburn 724: }
725: return $iptables;
726: }
727:
1.15 raeburn 728: sub get_distro {
729: my $distro;
730: if (open(PIPE,"/home/httpd/perl/distprobe |")) {
731: $distro = <PIPE>;
732: close(PIPE);
733: }
734: return $distro;
735: }
736:
1.1 raeburn 737: 1;
738: __END__
739:
740: =pod
741:
742: =head1 NAME
743:
744: B<LONCAPA::Firewall> - dynamic opening/closing of firewall ports
745:
746: =head1 SYNOPSIS
747:
748: use lib '/home/httpd/lib/perl/';
749: use LONCAPA::Firewall;
750:
1.15 raeburn 751: LONCAPA::Firewall::uses_firewalld();
1.1 raeburn 752: LONCAPA::Firewall::firewall_open_port();
753: LONCAPA::Firewall::firewall_close_port();
754: LONCAPA::Firewall::firewall_is_port_open();
755: LONCAPA::Firewall::firewall_is_active();
756: LONCAPA::Firewall::firewall_close_anywhere();
757:
758: =head1 DESCRIPTION
759:
760: The scripts: /etc/init.d/loncontrol, used to stop or start LON-CAPA services,
761: as well as the setuid script /home/httpd/perl/lciptables, called by loncron
762: for housekeeping tasks, make use of the methods provided by this module to
763: open and close firewall ports (currently the default port: 5663), used
764: for socket-based communication between LON-CAPA servers in the cluster
765: of networked servers to which the server belongs.
766:
767: The following methods are available:
768:
769: =over 4
770:
1.15 raeburn 771: =item LONCAPA::Firewall::uses_firewalld( $distro );
772:
773: =back
774:
775: =over 4
776:
1.18 raeburn 777: =item LONCAPA::Firewall::firewall_open_port( $iptables,$fw_chains,$lond_port,$iphost,$ports,$firewalld );
1.1 raeburn 778:
779: =back
780:
781: =over 4
782:
1.18 raeburn 783: =item LONCAPA::Firewall::firewall_close_port( $iptables,$fw_chains,$lond_port,$iphost,$ports,$firewalld );
1.1 raeburn 784:
785: =back
786:
787: =over 4
788:
1.18 raeburn 789: =item LONCAPA::Firewall::firewall_is_port_open( $iptables,$fw_chain,$port,$lond_port,$iphost,$curropen,$firewalld );
1.1 raeburn 790:
791: =back
792:
793: =over 4
794:
795: =item LONCAPA::Firewall::firewall_is_active();
796:
797: =back
798:
799: =over 4
800:
1.18 raeburn 801: =item LONCAPA::Firewall::firewall_close_anywhere( $iptables,$fw_chain,$port,$firewalld );
1.1 raeburn 802:
803: =back
804:
805: =over 4
806:
807: =item LONCAPA::Firewall::get_lond_port();
808:
809: =back
810:
811: =over 4
812:
1.15 raeburn 813: =item LONCAPA::Firewall::get_fw_chains( $iptables,$distro );
1.1 raeburn 814:
815: =back
816:
817: =over 4
818:
819: =item LONCAPA::Firewall::get_pathto_iptables();
820:
1.15 raeburn 821: =back
822:
823: =over 4
824:
825: =item LONCAPA::Firewall::get_distro();
826:
827: =back
1.1 raeburn 828:
829: =head1 AUTHORS
830:
831: This library is free software; you can redistribute it and/or
832: modify it under the same terms as LON-CAPA itself.
833:
834: =cut
835:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>