File:  [LON-CAPA] / loncom / configuration / Firewall.pm
Revision 1.28: download - view: text, annotated - select for diffs
Fri Sep 13 03:52:03 2024 UTC (5 months, 1 week ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, version_2_11_X, version_2_11_6_msu, version_2_11_6, HEAD
- Bug 6993. For LON-CAPA servers/VMs using firewalld: firewall rules added
  by lciptables made "permanent" so they persist after firewall-cmd --reload

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>