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