File:  [LON-CAPA] / loncom / init.d / loncontrol
Revision 1.34.2.1: download - view: text, annotated - select for diffs
Wed Jan 13 19:26:54 2010 UTC (14 years, 5 months ago) by raeburn
Branches: version_2_8_X
CVS tags: version_2_8_2
Diff to branchpoint 1.34: preferred, unified
- Backport part of Firewall.pm rev 1.3 to support cases where loncontrol is used
  on a system where chain is INPUT instead of RH-Firewall-1-INPUT.

    1: #!/usr/bin/perl
    2: #
    3: # $Id: loncontrol,v 1.34.2.1 2010/01/13 19:26:54 raeburn Exp $
    4: #
    5: # The LearningOnline Network with CAPA
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: #
   29: # Startup script for the LON-CAPA network processes
   30: #
   31: 
   32: # chkconfig: 345 95 5
   33: # description: LON-CAPA is a "network of knowledge".  It is used to \
   34: # distribute knowledge resources and instructional management.
   35: # processnames: lonc, lond, lonsql, lonmaxima
   36: # pidfiles: /home/httpd/perl/logs/lon*.pid
   37: # config: /etc/httpd/conf/loncapa.conf
   38: # config: /home/httpd/lonTabs/hosts.tab
   39: # config: /home/httpd/lonTabs/spare.tab
   40: # SuSE chkconfig/insserv info
   41: ### BEGIN INIT INFO
   42: # Provides:       loncapa
   43: # Required-Start: mysql apache2 $network $remote_fs
   44: # Required-Stop:
   45: # Default-Start:  3 4 5
   46: # Default-Stop:
   47: # Description:    Starts the LON-CAPA services
   48: ### END INIT INFO
   49: 
   50: use strict;
   51: use lib '/home/httpd/lib/perl/';
   52: use LONCAPA::Configuration;
   53: 
   54: my $command=$ARGV[0]; $command=~s/[^a-z]//g;
   55: 
   56: $ENV{'PATH'}="/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin";
   57: $ENV{'BASH_ENV'}="";
   58: 
   59: { # Firewall variable scoping
   60:     # Firewall code is based on the code in FC2 /etc/init.d/ntpd
   61:     my $fw_chain = 'RH-Firewall-1-INPUT';
   62:     my $iptables = '/sbin/iptables';
   63:     if (! -e $iptables) {
   64: 	$iptables = '/usr/sbin/iptables';
   65: 	if (!-e $iptables) {
   66: 	    print("Unable to find iptables command\n");
   67: 	}
   68:     }
   69:     my $suse_config = "/etc/sysconfig/SuSEfirewall2";
   70:     if (-e $suse_config) {
   71:         $fw_chain = 'input_ext';
   72:     } else {
   73:         if (!-e '/etc/sysconfig/iptables') {
   74:             print("Unable to find iptables file containing static definitions\n");
   75:         }
   76:     }
   77:     if (-e $iptables) {
   78:         my $count = `$iptables -L -n 2>/dev/null |grep $fw_chain |wc -l`;
   79:         chomp($count);
   80:         if (!$count) {
   81:             $fw_chain ='INPUT';
   82:         }
   83:     }
   84:     my $lond_port = &get_lond_port();
   85:     if (!$lond_port) {
   86:         print("Unable to determine lond port number from LON-CAPA configuration.\n");
   87:     }
   88: 
   89: sub firewall_open_port {
   90:     return 'inactive firewall' if (! &firewall_is_active);
   91:     return 'port number unknown' if !$lond_port;
   92:     my @opened;
   93:     my $suse_config = "/etc/sysconfig/SuSEfirewall2";
   94:     if (-e $suse_config) {
   95:         if (open(my $fh,"<$suse_config")) {
   96:             while(<$fh>) {
   97:                 chomp();
   98:                 if (/^FW_SERVICES_EXT_TCP="([^"]+)"\s*$/) {
   99:                     my $portstr = $1;
  100:                     my @suseports = split(/\s+/,$portstr);
  101:                     foreach my $port ($lond_port) {
  102:                         if (grep/^\Q$port\E$/,@suseports) {
  103:                             push(@opened,$port);
  104:                         }
  105:                     }
  106:                 }
  107:             }
  108:         }
  109:     } else {
  110:         if (! `$iptables -L -n 2>/dev/null | grep $fw_chain | wc -l`) { 
  111:             return 'chain error';
  112:         }
  113:         # iptables is running with our chain
  114:         #
  115:         # We could restrict the servers allowed to attempt to communicate
  116:         # here, but the logistics of updating the /home/httpd/lonTabs/host.tab
  117:         # file are likely to be a problem
  118:         foreach my $port ($lond_port) {
  119:             print "Opening firewall access on port $port.\n";
  120:             my $result;
  121:             my $firewall_command = 
  122:                 "$iptables -I $fw_chain -p tcp -d 0/0 --dport $port -j ACCEPT";
  123:             system($firewall_command);
  124:             my $return_status = $?>>8;
  125:             if ($return_status == 1) {
  126:                 # Error
  127:                 print "Error opening port.\n";
  128:             } elsif ($return_status == 2) {
  129:                 # Bad command
  130:                 print "Bad command error opening port.  Command was\n".
  131:                       "  ".$firewall_command."\n";
  132:             } elsif ($return_status == 0) {
  133:                 push(@opened,$port);
  134:             }
  135:         }
  136:     }
  137:     foreach my $port ($lond_port) {
  138:         if (!grep(/^\Q$port\E$/,@opened)) {
  139:             return 'Required port not open: '.$port."\n";  
  140:         }
  141:     }
  142:     return 'ok';
  143: }
  144: 
  145: sub firewall_is_port_open {
  146:     my ($port) = @_;
  147:     # returns 1 if the firewall port is open, 0 if not.
  148:     #
  149:     # check if firewall is active or installed
  150:     return if (! &firewall_is_active);
  151:     if (`$iptables -L -n 2>/dev/null | grep "tcp dpt:$port"`) { 
  152:         return 1;
  153:     } else {
  154:         return 0;
  155:     }
  156: }
  157: 
  158: sub firewall_is_active {
  159:     if (-e '/proc/net/ip_tables_names') {
  160:         return 1;
  161:     } else {
  162:         return 0;
  163:     }
  164: }
  165: 
  166: sub firewall_close_port {
  167:     return 'inactive firewall' if (! &firewall_is_active);
  168:     return 'port number unknown' if !$lond_port;
  169:     my $suse_config = "/etc/sysconfig/SuSEfirewall2";
  170:     return if (-e $suse_config);
  171:     foreach my $port ($lond_port) {
  172:         print "Closing firewall access on port $port\n";
  173:         my $firewall_command = 
  174:             "$iptables -D $fw_chain -p tcp -d 0/0 --dport $port -j ACCEPT";
  175:         system($firewall_command);
  176:         my $return_status = $?>>8;
  177:         if ($return_status == 1) {
  178:             # Error
  179:             print "Error closing port.\n";
  180:         } elsif ($return_status == 2) {
  181:             # Bad command
  182:             print "Bad command error closing port.  Command was\n".
  183:                 "  ".$firewall_command."\n";
  184:         } else {
  185:             print "Port closed.\n";
  186:         }
  187:     }
  188:     return;
  189: }
  190: 
  191: sub get_lond_port {
  192:     my $perlvarref=&LONCAPA::Configuration::read_conf();
  193:     my $lond_port;
  194:     if (ref($perlvarref) eq 'HASH') {
  195:         if (defined($perlvarref->{'londPort'})) {
  196:             $lond_port = $perlvarref->{'londPort'};
  197:         }
  198:     }
  199:     return $lond_port;
  200: }
  201: 
  202: } # End firewall variable scope
  203: 
  204: sub stop_daemon {
  205:     my ($daemon,$killallname)=@_;
  206:     my $pidfile="/home/httpd/perl/logs/$daemon.pid";
  207:     
  208:     printf("%-15s ",$daemon);
  209:     if (-e $pidfile) {
  210: 	open(PIDFILE,$pidfile);
  211: 	my $daemonpid=<PIDFILE>;
  212: 	chomp($daemonpid);
  213: 	kill TERM => $daemonpid;
  214: 	my $count=0;
  215: 	while ($count++ < 5 && kill(0 => $daemonpid)) {
  216: 	    sleep 1;
  217: 	}
  218: 	if (kill 0 => $daemonpid) {
  219: 	    kill KILL => $daemonpid;
  220: 	    sleep 1;
  221: 	    if (kill 0 => $daemonpid) {
  222: 		print("failed to kill");
  223: 	    } else {
  224: 		print("killed");
  225: 	    }
  226: 	} else {
  227: 	    print("stopped");
  228: 	}
  229:     } else {
  230: 	print("not running");
  231:     }
  232:     system("killall -q -0 $killallname");
  233:     if ($? == 0) {
  234: 	system("killall -q $killallname");
  235: 	print(", killed off extraneous processes");
  236:     }
  237:     unlink($pidfile);
  238:     print("\n");
  239: }
  240: 
  241: sub clean_sockets {
  242:     opendir(SOCKETS,"/home/httpd/sockets/");
  243:     my $perlvarref=&LONCAPA::Configuration::read_conf();
  244:     return if (ref($perlvarref) ne 'HASH');
  245:     while (my $fname=readdir(SOCKETS)) {
  246: 	next if (-d $fname
  247: 		 || $fname=~/(mysqlsock|maximasock|\Q$perlvarref->{'lonSockDir'}\E)/);
  248: 	unlink("/home/httpd/sockets/$fname");
  249:     }
  250: }
  251: 
  252: if ($command eq "restart") {
  253:     print 'Restarting LON-CAPA'."\n";
  254:     print 'Ending LON-CAPA client and daemon processes'."\n";
  255:     foreach my $daemon ('lonsql','lond','lonc','lonmemcached','lonmaxima') {
  256: 	my $killallname=$daemon;
  257: 	if ($daemon eq 'lonc') { $killallname='loncnew'; }
  258: 	&stop_daemon($daemon,$killallname);
  259:     }
  260:     print 'Starting LON-CAPA client and daemon processes (please be patient)'.
  261: 	"\n";
  262:     system("su www -c '/home/httpd/perl/loncron --justcheckdaemons'");
  263: } elsif ($command eq "stop") {
  264:     print 'Stopping LON-CAPA'."\n";
  265:     foreach my $daemon ('lonsql','lond','lonc','lonmemcached','lonmaxima') {
  266: 	my $killallname=$daemon;
  267: 	if ($daemon eq 'lonc') { $killallname='loncnew'; }
  268: 	&stop_daemon($daemon,$killallname);
  269:     }
  270:     my $firewall_result = &firewall_close_port();
  271:     if ($firewall_result) {
  272:         print "$firewall_result\n";
  273:     }
  274:     &clean_sockets();
  275: } elsif ($command eq "start") {
  276:     my $firewall_result = &firewall_open_port();
  277:     if (($firewall_result eq 'ok') || ($firewall_result eq 'inactive firewall')) {
  278:         if ($firewall_result eq 'inactive firewall') {
  279:             print "WARNING: iptables firewall is currently inactive\n";
  280:         }
  281:         print 'Starting LON-CAPA'."\n";
  282:         print 'Starting LON-CAPA client and daemon processes (please be patient)'.
  283: 	      "\n";
  284:         system("su www -c '/home/httpd/perl/loncron --justcheckdaemons'");
  285:     } else {
  286:         print "Not starting LON-CAPA\n";
  287:         if ($firewall_result eq 'port number unknown') {
  288:             print "Could not check for status of LON-CAPA port in running firewall - port number unknown.  \n";
  289:         } elsif ($firewall_result) {
  290:             print "$firewall_result\n";
  291:         }
  292:     }
  293: } elsif ($command eq "reload") {
  294:     print 'Reload LON-CAPA config files'."\n";
  295:     system("su www -c '/home/httpd/perl/loncron --justreload'");
  296: } elsif ($command eq "status") {
  297:     my $lond_port = &get_lond_port();
  298:     my $response=`/bin/cat /home/httpd/perl/logs/*.pid 2>&1`;
  299:     if ($response=~/No such file or directory/) {
  300: 	print 'LON-CAPA is not running.'."\n";
  301:     } else {
  302: 	print 'LON-CAPA is running.'."\n";
  303: 	system("su www -c '/home/httpd/perl/loncron --justcheckconnections'");
  304:     }
  305:     if (! &firewall_is_active) {
  306:         print 'The iptables firewall is not active'."\n";
  307:     }
  308:     my $lond_port = &get_lond_port();
  309:     if ($lond_port) {
  310:         if (&firewall_is_port_open($lond_port)) {
  311:             print "The LON-CAPA port ($lond_port) is open in firewall.\n";
  312:         } elsif (&firewall_is_active) {
  313:             print "The LON-CAPA port ($lond_port) is NOT open in running firewall!\n";
  314:         }
  315:     } else {
  316:         if (&firewall_is_active) {
  317:             print "Could not check for status of LON-CAPA port in running firewall - port number unknown.\n";
  318:         } else {
  319:             print "LON-CAPA port number is unknown, and firewall is not running.\n";
  320:         }
  321:     }
  322: } else {
  323:     print "You need to specify one of restart|stop|start|status on the command line.\n";
  324: }

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