1: #!/usr/bin/perl
2: #
3: # $Id: loncontrol,v 1.34 2009/02/02 17:06:55 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: if (!-e '/etc/sysconfig/iptables') {
72: print("Unable to find iptables file containing static definitions\n");
73: }
74: }
75: my $lond_port = &get_lond_port();
76: if (!$lond_port) {
77: print("Unable to determine lond port number from LON-CAPA configuration.\n");
78: }
79:
80: sub firewall_open_port {
81: return 'inactive firewall' if (! &firewall_is_active);
82: return 'port number unknown' if !$lond_port;
83: my @opened;
84: my $suse_config = "/etc/sysconfig/SuSEfirewall2";
85: if (-e $suse_config) {
86: if (open(my $fh,"<$suse_config")) {
87: while(<$fh>) {
88: chomp();
89: if (/^FW_SERVICES_EXT_TCP="([^"]+)"\s*$/) {
90: my $portstr = $1;
91: my @suseports = split(/\s+/,$portstr);
92: foreach my $port ($lond_port) {
93: if (grep/^\Q$port\E$/,@suseports) {
94: push(@opened,$port);
95: }
96: }
97: }
98: }
99: }
100: } else {
101: if (! `$iptables -L -n 2>/dev/null | grep $fw_chain | wc -l`) {
102: return 'chain error';
103: }
104: # iptables is running with our chain
105: #
106: # We could restrict the servers allowed to attempt to communicate
107: # here, but the logistics of updating the /home/httpd/lonTabs/host.tab
108: # file are likely to be a problem
109: foreach my $port ($lond_port) {
110: print "Opening firewall access on port $port.\n";
111: my $result;
112: my $firewall_command =
113: "$iptables -I $fw_chain -p tcp -d 0/0 --dport $port -j ACCEPT";
114: system($firewall_command);
115: my $return_status = $?>>8;
116: if ($return_status == 1) {
117: # Error
118: print "Error opening port.\n";
119: } elsif ($return_status == 2) {
120: # Bad command
121: print "Bad command error opening port. Command was\n".
122: " ".$firewall_command."\n";
123: } elsif ($return_status == 0) {
124: push(@opened,$port);
125: }
126: }
127: }
128: foreach my $port ($lond_port) {
129: if (!grep(/^\Q$port\E$/,@opened)) {
130: return 'Required port not open: '.$port."\n";
131: }
132: }
133: return 'ok';
134: }
135:
136: sub firewall_is_port_open {
137: my ($port) = @_;
138: # returns 1 if the firewall port is open, 0 if not.
139: #
140: # check if firewall is active or installed
141: return if (! &firewall_is_active);
142: if (`$iptables -L -n 2>/dev/null | grep "tcp dpt:$port"`) {
143: return 1;
144: } else {
145: return 0;
146: }
147: }
148:
149: sub firewall_is_active {
150: if (-e '/proc/net/ip_tables_names') {
151: return 1;
152: } else {
153: return 0;
154: }
155: }
156:
157: sub firewall_close_port {
158: return 'inactive firewall' if (! &firewall_is_active);
159: return 'port number unknown' if !$lond_port;
160: my $suse_config = "/etc/sysconfig/SuSEfirewall2";
161: return if (-e $suse_config);
162: foreach my $port ($lond_port) {
163: print "Closing firewall access on port $port\n";
164: my $firewall_command =
165: "$iptables -D $fw_chain -p tcp -d 0/0 --dport $port -j ACCEPT";
166: system($firewall_command);
167: my $return_status = $?>>8;
168: if ($return_status == 1) {
169: # Error
170: print "Error closing port.\n";
171: } elsif ($return_status == 2) {
172: # Bad command
173: print "Bad command error closing port. Command was\n".
174: " ".$firewall_command."\n";
175: } else {
176: print "Port closed.\n";
177: }
178: }
179: return;
180: }
181:
182: sub get_lond_port {
183: my $perlvarref=&LONCAPA::Configuration::read_conf();
184: my $lond_port;
185: if (ref($perlvarref) eq 'HASH') {
186: if (defined($perlvarref->{'londPort'})) {
187: $lond_port = $perlvarref->{'londPort'};
188: }
189: }
190: return $lond_port;
191: }
192:
193: } # End firewall variable scope
194:
195: sub stop_daemon {
196: my ($daemon,$killallname)=@_;
197: my $pidfile="/home/httpd/perl/logs/$daemon.pid";
198:
199: printf("%-15s ",$daemon);
200: if (-e $pidfile) {
201: open(PIDFILE,$pidfile);
202: my $daemonpid=<PIDFILE>;
203: chomp($daemonpid);
204: kill TERM => $daemonpid;
205: my $count=0;
206: while ($count++ < 5 && kill(0 => $daemonpid)) {
207: sleep 1;
208: }
209: if (kill 0 => $daemonpid) {
210: kill KILL => $daemonpid;
211: sleep 1;
212: if (kill 0 => $daemonpid) {
213: print("failed to kill");
214: } else {
215: print("killed");
216: }
217: } else {
218: print("stopped");
219: }
220: } else {
221: print("not running");
222: }
223: system("killall -q -0 $killallname");
224: if ($? == 0) {
225: system("killall -q $killallname");
226: print(", killed off extraneous processes");
227: }
228: unlink($pidfile);
229: print("\n");
230: }
231:
232: sub clean_sockets {
233: opendir(SOCKETS,"/home/httpd/sockets/");
234: my $perlvarref=&LONCAPA::Configuration::read_conf();
235: return if (ref($perlvarref) ne 'HASH');
236: while (my $fname=readdir(SOCKETS)) {
237: next if (-d $fname
238: || $fname=~/(mysqlsock|maximasock|\Q$perlvarref->{'lonSockDir'}\E)/);
239: unlink("/home/httpd/sockets/$fname");
240: }
241: }
242:
243: if ($command eq "restart") {
244: print 'Restarting LON-CAPA'."\n";
245: print 'Ending LON-CAPA client and daemon processes'."\n";
246: foreach my $daemon ('lonsql','lond','lonc','lonmemcached','lonmaxima') {
247: my $killallname=$daemon;
248: if ($daemon eq 'lonc') { $killallname='loncnew'; }
249: &stop_daemon($daemon,$killallname);
250: }
251: print 'Starting LON-CAPA client and daemon processes (please be patient)'.
252: "\n";
253: system("su www -c '/home/httpd/perl/loncron --justcheckdaemons'");
254: } elsif ($command eq "stop") {
255: print 'Stopping LON-CAPA'."\n";
256: foreach my $daemon ('lonsql','lond','lonc','lonmemcached','lonmaxima') {
257: my $killallname=$daemon;
258: if ($daemon eq 'lonc') { $killallname='loncnew'; }
259: &stop_daemon($daemon,$killallname);
260: }
261: my $firewall_result = &firewall_close_port();
262: if ($firewall_result) {
263: print "$firewall_result\n";
264: }
265: &clean_sockets();
266: } elsif ($command eq "start") {
267: my $firewall_result = &firewall_open_port();
268: if (($firewall_result eq 'ok') || ($firewall_result eq 'inactive firewall')) {
269: if ($firewall_result eq 'inactive firewall') {
270: print "WARNING: iptables firewall is currently inactive\n";
271: }
272: print 'Starting LON-CAPA'."\n";
273: print 'Starting LON-CAPA client and daemon processes (please be patient)'.
274: "\n";
275: system("su www -c '/home/httpd/perl/loncron --justcheckdaemons'");
276: } else {
277: print "Not starting LON-CAPA\n";
278: if ($firewall_result eq 'port number unknown') {
279: print "Could not check for status of LON-CAPA port in running firewall - port number unknown. \n";
280: } elsif ($firewall_result) {
281: print "$firewall_result\n";
282: }
283: }
284: } elsif ($command eq "reload") {
285: print 'Reload LON-CAPA config files'."\n";
286: system("su www -c '/home/httpd/perl/loncron --justreload'");
287: } elsif ($command eq "status") {
288: my $lond_port = &get_lond_port();
289: my $response=`/bin/cat /home/httpd/perl/logs/*.pid 2>&1`;
290: if ($response=~/No such file or directory/) {
291: print 'LON-CAPA is not running.'."\n";
292: } else {
293: print 'LON-CAPA is running.'."\n";
294: system("su www -c '/home/httpd/perl/loncron --justcheckconnections'");
295: }
296: if (! &firewall_is_active) {
297: print 'The iptables firewall is not active'."\n";
298: }
299: my $lond_port = &get_lond_port();
300: if ($lond_port) {
301: if (&firewall_is_port_open($lond_port)) {
302: print "The LON-CAPA port ($lond_port) is open in firewall.\n";
303: } elsif (&firewall_is_active) {
304: print "The LON-CAPA port ($lond_port) is NOT open in running firewall!\n";
305: }
306: } else {
307: if (&firewall_is_active) {
308: print "Could not check for status of LON-CAPA port in running firewall - port number unknown.\n";
309: } else {
310: print "LON-CAPA port number is unknown, and firewall is not running.\n";
311: }
312: }
313: } else {
314: print "You need to specify one of restart|stop|start|status on the command line.\n";
315: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>