File:  [LON-CAPA] / loncom / configuration / Firewall.pm
Revision 1.17: download - view: text, annotated - select for diffs
Tue May 7 21:18:24 2019 UTC (5 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Dynamic management of LON-CAPA port 5663 compatible with firewalld
  - &uses_firewalld() gets default zone when firewalld is in use.

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

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