]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/services.cgi
services.cgi: Redesign isautorun() because shell globbing cannot used anymore.
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / services.cgi
CommitLineData
ac1cfefa 1#!/usr/bin/perl
70df8302
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
31c46c07 5# Copyright (C) 2005-2021 IPFire Team #
70df8302
MT
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
ac1cfefa
MT
21
22use strict;
23
2032b6f9 24# enable only the following on debugging purpose
2d281532
CS
25#use warnings;
26#use CGI::Carp 'fatalsToBrowser';
2032b6f9 27
f2fdd0c1 28require '/var/ipfire/general-functions.pl';
ac1cfefa
MT
29require "${General::swroot}/lang.pl";
30require "${General::swroot}/header.pl";
773362c5 31require "${General::swroot}/graphs.pl";
ac1cfefa 32
2032b6f9
CS
33my %color = ();
34my %mainsettings = ();
4e481c3a 35my %netsettings=();
2032b6f9 36&General::readhash("${General::swroot}/main/settings", \%mainsettings);
8186b372 37&General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
2032b6f9 38&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
ac1cfefa 39
4e481c3a
CS
40#workaround to suppress a warning when a variable is used only once
41my @dummy = ( ${Header::colourred} );
42undef (@dummy);
43
773362c5 44
ac1cfefa 45my %cgiparams=();
2032b6f9
CS
46# Maps a nice printable name to the changing part of the pid file, which
47# is also the name of the program
4e481c3a 48my %servicenames =(
2032b6f9
CS
49 $Lang::tr{'dhcp server'} => 'dhcpd',
50 $Lang::tr{'web server'} => 'httpd',
51 $Lang::tr{'cron server'} => 'fcron',
9625be6f 52 $Lang::tr{'dns proxy server'} => 'unbound',
2032b6f9
CS
53 $Lang::tr{'logging server'} => 'syslogd',
54 $Lang::tr{'kernel logging server'} => 'klogd',
55 $Lang::tr{'ntp server'} => 'ntpd',
56 $Lang::tr{'secure shell server'} => 'sshd',
70e8a248 57 $Lang::tr{'vpn'} => 'charon',
2032b6f9 58 $Lang::tr{'web proxy'} => 'squid',
0d8cc90f 59 $Lang::tr{'intrusion detection system'} => 'suricata',
2032b6f9
CS
60 'OpenVPN' => 'openvpn'
61);
62
533f1e11
JPT
63my %link =(
64 $Lang::tr{'dhcp server'} => "<a href=\'dhcp.cgi\'>$Lang::tr{'dhcp server'}</a>",
65 $Lang::tr{'web server'} => $Lang::tr{'web server'},
66 $Lang::tr{'cron server'} => $Lang::tr{'cron server'},
67 $Lang::tr{'dns proxy server'} => $Lang::tr{'dns proxy server'},
68 $Lang::tr{'logging server'} => $Lang::tr{'logging server'},
69 $Lang::tr{'kernel logging server'} => $Lang::tr{'kernel logging server'},
70 $Lang::tr{'ntp server'} => "<a href=\'time.cgi\'>$Lang::tr{'ntp server'}</a>",
71 $Lang::tr{'secure shell server'} => "<a href=\'remote.cgi\'>$Lang::tr{'secure shell server'}</a>",
72 $Lang::tr{'vpn'} => "<a href=\'vpnmain.cgi\'>$Lang::tr{'vpn'}</a>",
73 $Lang::tr{'web proxy'} => "<a href=\'proxy.cgi\'>$Lang::tr{'web proxy'}</a>",
74 'OpenVPN' => "<a href=\'ovpnmain.cgi\'>OpenVPN</a>",
0d8cc90f 75 "$Lang::tr{'intrusion detection system'}" => "<a href=\'ids.cgi\'>$Lang::tr{'intrusion detection system'}</a>",
533f1e11
JPT
76);
77
0d8cc90f
SS
78# Hash to overwrite the process name of a process if it differs fromt the launch command.
79my %overwrite_exename_hash = (
80 "suricata" => "Suricata-Main"
81);
4e481c3a 82
0d8cc90f 83my $lines=0; # Used to count the outputlines to make different bgcolor
ac1cfefa 84
4e481c3a
CS
85my @querry = split(/\?/,$ENV{'QUERY_STRING'});
86$querry[0] = '' unless defined $querry[0];
87$querry[1] = 'hour' unless defined $querry[1];
88
89if ( $querry[0] =~ "processescpu"){
90 print "Content-type: image/png\n\n";
91 binmode(STDOUT);
92 &Graphs::updateprocessescpugraph($querry[1]);
93}elsif ( $querry[0] =~ "processesmemory"){
94 print "Content-type: image/png\n\n";
95 binmode(STDOUT);
96 &Graphs::updateprocessesmemorygraph($querry[1]);
97}else{
98 &Header::showhttpheaders();
99 &Header::openpage($Lang::tr{'status information'}, 1, '');
100 &Header::openbigbox('100%', 'left');
101
102 &Header::openbox('100%', 'left', $Lang::tr{'services'});
103 print <<END
2032b6f9 104<div align='center'>
91a81810 105<table width='80%' cellspacing='1' class='tbl'>
a9d40c03 106<tr>
91a81810
AM
107 <th align='left'><b>$Lang::tr{'services'}</b></th>
108 <th align='center' ><b>$Lang::tr{'status'}</b></th>
109 <th align='center'><b>PID</b></th>
110 <th align='center'><b>$Lang::tr{'memory'}</b></th>
111</tr>
2032b6f9
CS
112END
113;
4e481c3a 114 my $key = '';
91a81810 115 my $col="";
4e481c3a
CS
116 foreach $key (sort keys %servicenames){
117 $lines++;
118 if ($lines % 2){
91a81810
AM
119 $col="bgcolor='$color{'color22'}'";
120 print "<tr><td align='left' $col>";
62a13aec 121 print $link{$key};
91a81810 122 print "</td>";
4e481c3a 123 }else{
91a81810
AM
124 $col="bgcolor='$color{'color20'}'";
125 print "<tr><td align='left' $col>";
62a13aec 126 print $link{$key};
91a81810 127 print "</td>";
4e481c3a 128 }
ac1cfefa 129
4e481c3a 130 my $shortname = $servicenames{$key};
91a81810 131 my $status = &isrunning($shortname,$col);
ac1cfefa 132
4e481c3a
CS
133 print "$status\n";
134 print "</tr>\n";
135 }
ac1cfefa 136
4e481c3a
CS
137 print "</table></div>\n";
138 &Header::closebox();
ac1cfefa 139
4e481c3a
CS
140 &Header::openbox('100%', 'left', "Addon - $Lang::tr{services}");
141 my $paramstr=$ENV{QUERY_STRING};
142 my @param=split(/!/, $paramstr);
143 if ($param[1] ne ''){
150fadab 144 &General::system("/usr/local/bin/addonctrl", "$param[0]", "$param[1]");
4e481c3a 145 }
ac1cfefa 146
4e481c3a 147 print <<END
2032b6f9 148<div align='center'>
91a81810 149<table width='80%' cellspacing='1' class='tbl'>
a9d40c03 150<tr>
91a81810
AM
151 <th align='center'><b>Addon</b></th>
152 <th align='center'><b>Boot</b></th>
153 <th align='center' colspan=2><b>$Lang::tr{'action'}</b></th>
154 <th align='center'><b>$Lang::tr{'status'}</b></th>
155 <th align='center'><b>PID</b></th>
156 <th align='center'><b>$Lang::tr{'memory'}</b></th>
2032b6f9 157</tr>
ac1cfefa
MT
158END
159;
ac1cfefa 160
4e481c3a
CS
161 my $lines=0; # Used to count the outputlines to make different bgcolor
162
163 # Generate list of installed addon pak's
d9785588
AB
164 opendir (DIR, "/opt/pakfire/db/installed") || die "Cannot opendir /opt/pakfire/db/installed/: $!";
165 my @pak = sort readdir DIR;
f6340997
SS
166 closedir(DIR);
167
4e481c3a
CS
168 foreach (@pak){
169 chomp($_);
d9785588
AB
170 next unless (m/^meta-/);
171 s/^meta-//;
4e481c3a
CS
172
173 # Check which of the paks are services
d9785588 174 if (-e "/etc/init.d/$_") {
4e481c3a
CS
175 # blacklist some packages
176 #
177 # alsa has trouble with the volume saving and was not really stopped
bbd544ea 178 # mdadm should not stopped with webif because this could crash the system
4e481c3a 179 #
39d43569
AM
180 if ( $_ eq 'squid' ) {
181 next;
182 }
bbd544ea 183 if ( ($_ ne "alsa") && ($_ ne "mdadm") ) {
4e481c3a
CS
184 $lines++;
185 if ($lines % 2){
91a81810
AM
186 print "<tr>";
187 $col="bgcolor='$color{'color22'}'";
4e481c3a 188 }else{
91a81810
AM
189 print "<tr>";
190 $col="bgcolor='$color{'color20'}'";
4e481c3a 191 }
f6340997 192
a9d40c03 193 print "<td align='left' $col width='31%'>$_</td> ";
91a81810 194 my $status = isautorun($_,$col);
4e481c3a 195 print "$status ";
df261521
AH
196 print "<td align='center' $col width='8%'><a href='services.cgi?$_!start'><img alt='$Lang::tr{'start'}' title='$Lang::tr{'start'}' src='/images/go-up.png' border='0' /></a></td>";
197 print "<td align='center' $col width='8%'><a href='services.cgi?$_!stop'><img alt='$Lang::tr{'stop'}' title='$Lang::tr{'stop'}' src='/images/go-down.png' border='0' /></a></td> ";
91a81810 198 my $status = &isrunningaddon($_,$col);
4e481c3a
CS
199 $status =~ s/\\e\[[0-1]\;[0-9]+m//g;
200
201 chomp($status);
202 print "$status";
203 print "</tr>";
204 }
ac1cfefa 205 }
ac1cfefa 206 }
ac1cfefa 207
4e481c3a
CS
208 print "</table></div>\n";
209 &Header::closebox();
ac1cfefa 210
4e481c3a
CS
211 &Header::openbox('100%', 'center', "$Lang::tr{'processes'} $Lang::tr{'graph'}");
212 &Graphs::makegraphbox("services.cgi","processescpu","day");
213 &Header::closebox();
773362c5 214
4e481c3a
CS
215 &Header::openbox('100%', 'center', "$Lang::tr{'processes'} $Lang::tr{'memory'} $Lang::tr{'graph'}");
216 &Graphs::makegraphbox("services.cgi","processesmemory","day");
217 &Header::closebox();
218
219 &Header::closebigbox();
220 &Header::closepage();
773362c5 221}
ac1cfefa 222
f6340997
SS
223sub isautorun (@) {
224 my ($cmd, $col) = @_;
225
226 # Init directory.
227 my $initdir = "/etc/rc.d/rc3.d/";
228
91a81810 229 my $status = "<td align='center' $col></td>";
f6340997
SS
230
231 # Check if autorun for the given cmd is enabled.
232 if ( &find_init("$cmd", "$initdir") ) {
233 # Adjust status.
df261521 234 $status = "<td align='center' $col><a href='services.cgi?$_!disable'><img alt='$Lang::tr{'deactivate'}' title='$Lang::tr{'deactivate'}' src='/images/on.gif' border='0' width='16' height='16' /></a></td>";
f6340997
SS
235 } else {
236 # Adjust status.
df261521 237 $status = "<td align='center' $col><a href='services.cgi?$_!enable'><img alt='$Lang::tr{'activate'}' title='$Lang::tr{'activate'}' src='/images/off.gif' border='0' width='16' height='16' /></a></td>";
ac1cfefa 238 }
773362c5 239
f6340997 240 # Return the status.
4e481c3a 241 return $status;
2032b6f9
CS
242}
243
f6340997
SS
244sub find_init (@) {
245 my ($cmd, $dir) = @_;
246
247 # Open given init directory.
248 opendir (INITDIR, "$dir") || die "Cannot opendir $dir: $!";
249
250 # Read-in init files from directory.
251 my @inits = readdir(INITDIR);
252
253 # Close directory handle.
254 closedir(INITDIR);
255
256 # Loop through the directory.
257 foreach my $init (@inits) {
258 # Check if the current processed file belongs to the given command.
259 if ($init =~ /S\d+\d+$cmd\z/) {
260 # Found, return "1" - True.
261 return "1";
262 }
263 }
264
265 # Nothing found, return nothing.
266 return;
267}
268
269sub isrunning (@) {
270 my ($cmd, $col) = @_;
91a81810 271 my $status = "<td align='center' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan='2' $col></td>";
2032b6f9
CS
272 my $pid = '';
273 my $testcmd = '';
274 my $exename;
24c82ed2 275 my $memory;
2032b6f9
CS
276
277 $cmd =~ /(^[a-z]+)/;
0d8cc90f
SS
278
279 # Check if the exename needs to be overwritten.
280 # This happens if the expected process name string
281 # differs from the real one. This may happened if
282 # a service uses multiple processes or threads.
283 if (exists($overwrite_exename_hash{$1})) {
284 # Grab the string which will be reported by
285 # the process from the corresponding hash.
286 $exename = $overwrite_exename_hash{$1};
287 } else {
288 # Directly expect the launched command as
289 # process name.
290 $exename = $1;
291 }
2032b6f9
CS
292
293 if (open(FILE, "/var/run/${cmd}.pid")){
294 $pid = <FILE>; chomp $pid;
295 close FILE;
296 if (open(FILE, "/proc/${pid}/status")){
297 while (<FILE>){
4e481c3a
CS
298 if (/^Name:\W+(.*)/) {
299 $testcmd = $1;
300 }
ac1cfefa 301 }
2032b6f9 302 close FILE;
ac1cfefa 303 }
24c82ed2
MT
304 if (open(FILE, "/proc/${pid}/status")) {
305 while (<FILE>) {
306 my ($key, $val) = split(":", $_, 2);
307 if ($key eq 'VmRSS') {
308 $memory = $val;
309 last;
310 }
311 }
312 close(FILE);
ac1cfefa 313 }
4e481c3a 314 if ($testcmd =~ /$exename/){
91a81810 315 $status = "<td align='center' bgcolor='${Header::colourgreen}'><font color='white'><b>$Lang::tr{'running'}</b></font></td><td align='center' $col>$pid</td><td align='center' $col>$memory</td>";
2032b6f9 316 }
4e481c3a
CS
317 }
318 return $status;
ac1cfefa
MT
319}
320
f6340997
SS
321sub isrunningaddon (@) {
322 my ($cmd, $col) = @_;
323
91a81810 324 my $status = "<td align='center' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan='2' $col></td>";
2032b6f9
CS
325 my $pid = '';
326 my $testcmd = '';
327 my $exename;
328 my @memory;
773362c5 329
f6340997 330 my @testcmd = &General::system_output("/usr/local/bin/addonctrl", "$cmd", "status");
31c46c07 331 my $testcmd = @testcmd[0];
ac1cfefa 332
2032b6f9 333 if ( $testcmd =~ /is\ running/ && $testcmd !~ /is\ not\ running/){
4e481c3a 334 $status = "<td align='center' bgcolor='${Header::colourgreen}'><font color='white'><b>$Lang::tr{'running'}</b></font></td>";
7fb1b9f6 335 $testcmd =~ s/.* //gi;
4e481c3a
CS
336 $testcmd =~ s/[a-z_]//gi;
337 $testcmd =~ s/\[[0-1]\;[0-9]+//gi;
338 $testcmd =~ s/[\(\)\.]//gi;
339 $testcmd =~ s/ //gi;
340 $testcmd =~ s/\e//gi;
2032b6f9 341
4e481c3a 342 my @pid = split(/\s/,$testcmd);
91a81810 343 $status .="<td align='center' $col>$pid[0]</td>";
773362c5 344
4e481c3a 345 my $memory = 0;
773362c5 346
4e481c3a
CS
347 foreach (@pid){
348 chomp($_);
349 if (open(FILE, "/proc/$_/statm")){
2032b6f9 350 my $temp = <FILE>;
4e481c3a
CS
351 @memory = split(/ /,$temp);
352 }
353 $memory+=$memory[0];
ac1cfefa 354 }
91a81810 355 $status .="<td align='center' $col>$memory KB</td>";
4e481c3a 356 }else{
91a81810 357 $status = "<td align='center' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan='2' $col></td>";
ac1cfefa 358 }
4e481c3a 359 return $status;
ac1cfefa 360}