File:  [LON-CAPA] / loncom / configuration / Firewall.pm
Revision 1.21: download - view: text, annotated - select for diffs
Thu Jul 9 13:42:37 2020 UTC (3 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Warn if iphost information is missing.

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

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