]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/services.cgi
services.cgi: add link to addon config if ui exists for it
[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;
2b9b31b7 23use feature "switch";
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";
5cc18f9c 32require "/opt/pakfire/lib/functions.pl";
ac1cfefa 33
2032b6f9
CS
34my %color = ();
35my %mainsettings = ();
4e481c3a 36my %netsettings=();
2032b6f9 37&General::readhash("${General::swroot}/main/settings", \%mainsettings);
8186b372 38&General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
2032b6f9 39&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
ac1cfefa 40
4e481c3a
CS
41#workaround to suppress a warning when a variable is used only once
42my @dummy = ( ${Header::colourred} );
43undef (@dummy);
44
773362c5 45
ac1cfefa 46my %cgiparams=();
2032b6f9
CS
47# Maps a nice printable name to the changing part of the pid file, which
48# is also the name of the program
4e481c3a 49my %servicenames =(
2032b6f9
CS
50 $Lang::tr{'dhcp server'} => 'dhcpd',
51 $Lang::tr{'web server'} => 'httpd',
52 $Lang::tr{'cron server'} => 'fcron',
9625be6f 53 $Lang::tr{'dns proxy server'} => 'unbound',
2032b6f9
CS
54 $Lang::tr{'logging server'} => 'syslogd',
55 $Lang::tr{'kernel logging server'} => 'klogd',
56 $Lang::tr{'ntp server'} => 'ntpd',
57 $Lang::tr{'secure shell server'} => 'sshd',
70e8a248 58 $Lang::tr{'vpn'} => 'charon',
2032b6f9 59 $Lang::tr{'web proxy'} => 'squid',
0d8cc90f 60 $Lang::tr{'intrusion detection system'} => 'suricata',
2032b6f9
CS
61 'OpenVPN' => 'openvpn'
62);
63
533f1e11
JPT
64my %link =(
65 $Lang::tr{'dhcp server'} => "<a href=\'dhcp.cgi\'>$Lang::tr{'dhcp server'}</a>",
66 $Lang::tr{'web server'} => $Lang::tr{'web server'},
67 $Lang::tr{'cron server'} => $Lang::tr{'cron server'},
8aab72f0 68 $Lang::tr{'dns proxy server'} => "<a href=\'dns.cgi\'>$Lang::tr{'dns proxy server'}</a>",
533f1e11
JPT
69 $Lang::tr{'logging server'} => $Lang::tr{'logging server'},
70 $Lang::tr{'kernel logging server'} => $Lang::tr{'kernel logging server'},
71 $Lang::tr{'ntp server'} => "<a href=\'time.cgi\'>$Lang::tr{'ntp server'}</a>",
72 $Lang::tr{'secure shell server'} => "<a href=\'remote.cgi\'>$Lang::tr{'secure shell server'}</a>",
73 $Lang::tr{'vpn'} => "<a href=\'vpnmain.cgi\'>$Lang::tr{'vpn'}</a>",
74 $Lang::tr{'web proxy'} => "<a href=\'proxy.cgi\'>$Lang::tr{'web proxy'}</a>",
75 'OpenVPN' => "<a href=\'ovpnmain.cgi\'>OpenVPN</a>",
0d8cc90f 76 "$Lang::tr{'intrusion detection system'}" => "<a href=\'ids.cgi\'>$Lang::tr{'intrusion detection system'}</a>",
533f1e11
JPT
77);
78
0d8cc90f
SS
79# Hash to overwrite the process name of a process if it differs fromt the launch command.
80my %overwrite_exename_hash = (
81 "suricata" => "Suricata-Main"
82);
4e481c3a 83
0d8cc90f 84my $lines=0; # Used to count the outputlines to make different bgcolor
ac1cfefa 85
4e481c3a
CS
86my @querry = split(/\?/,$ENV{'QUERY_STRING'});
87$querry[0] = '' unless defined $querry[0];
88$querry[1] = 'hour' unless defined $querry[1];
89
90if ( $querry[0] =~ "processescpu"){
91 print "Content-type: image/png\n\n";
92 binmode(STDOUT);
93 &Graphs::updateprocessescpugraph($querry[1]);
94}elsif ( $querry[0] =~ "processesmemory"){
95 print "Content-type: image/png\n\n";
96 binmode(STDOUT);
97 &Graphs::updateprocessesmemorygraph($querry[1]);
98}else{
99 &Header::showhttpheaders();
100 &Header::openpage($Lang::tr{'status information'}, 1, '');
101 &Header::openbigbox('100%', 'left');
102
103 &Header::openbox('100%', 'left', $Lang::tr{'services'});
104 print <<END
2032b6f9 105<div align='center'>
91a81810 106<table width='80%' cellspacing='1' class='tbl'>
a9d40c03 107<tr>
207ca114 108 <th align='left'><b>$Lang::tr{'service'}</b></th>
91a81810
AM
109 <th align='center' ><b>$Lang::tr{'status'}</b></th>
110 <th align='center'><b>PID</b></th>
111 <th align='center'><b>$Lang::tr{'memory'}</b></th>
112</tr>
2032b6f9
CS
113END
114;
4e481c3a 115 my $key = '';
91a81810 116 my $col="";
4e481c3a
CS
117 foreach $key (sort keys %servicenames){
118 $lines++;
119 if ($lines % 2){
91a81810
AM
120 $col="bgcolor='$color{'color22'}'";
121 print "<tr><td align='left' $col>";
62a13aec 122 print $link{$key};
91a81810 123 print "</td>";
4e481c3a 124 }else{
91a81810
AM
125 $col="bgcolor='$color{'color20'}'";
126 print "<tr><td align='left' $col>";
62a13aec 127 print $link{$key};
91a81810 128 print "</td>";
4e481c3a 129 }
ac1cfefa 130
4e481c3a 131 my $shortname = $servicenames{$key};
91a81810 132 my $status = &isrunning($shortname,$col);
ac1cfefa 133
4e481c3a
CS
134 print "$status\n";
135 print "</tr>\n";
136 }
ac1cfefa 137
4e481c3a
CS
138 print "</table></div>\n";
139 &Header::closebox();
ac1cfefa 140
4e481c3a
CS
141 &Header::openbox('100%', 'left', "Addon - $Lang::tr{services}");
142 my $paramstr=$ENV{QUERY_STRING};
143 my @param=split(/!/, $paramstr);
2b9b31b7
RR
144 # Make sure action parameter is actually one of the allowed service actions
145 given ($param[1]) {
4f205b54 146 when ( ['start', 'stop', 'restart', 'enable', 'disable'] ) {
2b9b31b7
RR
147 # Make sure pak-name and service name don't contain any illegal character
148 if ( $param[0] !~ /[^a-zA-Z_0-9\-]/ &&
149 $param[2] !~ /[^a-zA-Z_0-9\-]/ ) {
150 &General::system("/usr/local/bin/addonctrl", "$param[0]", "$param[1]", "$param[2]");
151 }
152 }
4e481c3a 153 }
ac1cfefa 154
4e481c3a 155 print <<END
2032b6f9 156<div align='center'>
91a81810 157<table width='80%' cellspacing='1' class='tbl'>
a9d40c03 158<tr>
2b9b31b7 159 <th align='left'><b>Addon $Lang::tr{service}</b></th>
91a81810
AM
160 <th align='center'><b>Boot</b></th>
161 <th align='center' colspan=2><b>$Lang::tr{'action'}</b></th>
162 <th align='center'><b>$Lang::tr{'status'}</b></th>
163 <th align='center'><b>PID</b></th>
164 <th align='center'><b>$Lang::tr{'memory'}</b></th>
2032b6f9 165</tr>
ac1cfefa
MT
166END
167;
ac1cfefa 168
4e481c3a
CS
169 my $lines=0; # Used to count the outputlines to make different bgcolor
170
5cc18f9c
RR
171 my @paks;
172 my @addon_services;
173
174 # Generate list of installed addon pak services
175 my %paklist = &Pakfire::dblist("installed");
176
207ca114 177 foreach my $pak (sort keys %paklist) {
5cc18f9c
RR
178 my %metadata = &Pakfire::getmetadata($pak, "installed");
179
2b9b31b7
RR
180 my $service;
181
5cc18f9c 182 if ("$metadata{'Services'}") {
2b9b31b7
RR
183 foreach $service (split(/ /, "$metadata{'Services'}")) {
184 $lines++;
185 if ($lines % 2) {
186 print "<tr>";
187 $col="bgcolor='$color{'color22'}'";
188 } else {
189 print "<tr>";
190 $col="bgcolor='$color{'color20'}'";
191 }
ac1cfefa 192
2b9b31b7
RR
193 # Add addon name to displayname of service if servicename differs from addon
194 my $displayname = ($pak ne $service) ? "$service ($pak)" : $service;
8ed99710
RR
195 if ( -e "/srv/web/ipfire/cgi-bin/$pak.cgi" ) {
196 $displayname = ($pak ne $service) ? "$service (<a href=\'$pak.cgi\'>$pak</a>)" : "<a href=\'$pak.cgi\'>$service</a>";
197 }
198
2b9b31b7
RR
199 print "<td align='left' $col width='31%'>$displayname</td> ";
200
201 my $status = isautorun($pak,$service,$col);
202 print "$status ";
2b9b31b7
RR
203 my $status = isrunningaddon($pak,$service,$col);
204 $status =~ s/\\e\[[0-1]\;[0-9]+m//g;
205
206 chomp($status);
207 print "$status";
208 print "</tr>";
209 }
5cc18f9c 210 }
5cc18f9c
RR
211 }
212
4e481c3a
CS
213 print "</table></div>\n";
214 &Header::closebox();
ac1cfefa 215
4e481c3a
CS
216 &Header::openbox('100%', 'center', "$Lang::tr{'processes'} $Lang::tr{'graph'}");
217 &Graphs::makegraphbox("services.cgi","processescpu","day");
218 &Header::closebox();
773362c5 219
4e481c3a
CS
220 &Header::openbox('100%', 'center', "$Lang::tr{'processes'} $Lang::tr{'memory'} $Lang::tr{'graph'}");
221 &Graphs::makegraphbox("services.cgi","processesmemory","day");
222 &Header::closebox();
223
224 &Header::closebigbox();
225 &Header::closepage();
773362c5 226}
ac1cfefa 227
f6340997 228sub isautorun (@) {
2b9b31b7
RR
229 my ($pak, $service, $col) = @_;
230 my @testcmd = &General::system_output("/usr/local/bin/addonctrl", "$pak", "boot-status", "$service");
231 my $testcmd = @testcmd[0];
232 my $status = "<td align='center' $col><img alt='$Lang::tr{'service boot setting unavailable'}' title='$Lang::tr{'service boot setting unavailable'}' src='/images/dialog-warning.png' border='0' width='16' height='16' /></td>";
f6340997 233
2b9b31b7
RR
234 # Check if autorun for the given service is enabled.
235 if ( $testcmd =~ /enabled\ on\ boot/ ) {
f6340997 236 # Adjust status.
2b9b31b7
RR
237 $status = "<td align='center' $col><a href='services.cgi?$pak!disable!$service'><img alt='$Lang::tr{'deactivate'}' title='$Lang::tr{'deactivate'}' src='/images/on.gif' border='0' width='16' height='16' /></a></td>";
238 } elsif ( $testcmd =~ /disabled\ on\ boot/ ) {
f6340997 239 # Adjust status.
2b9b31b7 240 $status = "<td align='center' $col><a href='services.cgi?$pak!enable!$service'><img alt='$Lang::tr{'activate'}' title='$Lang::tr{'activate'}' src='/images/off.gif' border='0' width='16' height='16' /></a></td>";
ac1cfefa 241 }
773362c5 242
f6340997 243 # Return the status.
4e481c3a 244 return $status;
2032b6f9
CS
245}
246
f6340997
SS
247sub isrunning (@) {
248 my ($cmd, $col) = @_;
91a81810 249 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
250 my $pid = '';
251 my $testcmd = '';
252 my $exename;
24c82ed2 253 my $memory;
2032b6f9
CS
254
255 $cmd =~ /(^[a-z]+)/;
0d8cc90f
SS
256
257 # Check if the exename needs to be overwritten.
258 # This happens if the expected process name string
259 # differs from the real one. This may happened if
260 # a service uses multiple processes or threads.
261 if (exists($overwrite_exename_hash{$1})) {
262 # Grab the string which will be reported by
263 # the process from the corresponding hash.
264 $exename = $overwrite_exename_hash{$1};
265 } else {
266 # Directly expect the launched command as
267 # process name.
268 $exename = $1;
269 }
2032b6f9
CS
270
271 if (open(FILE, "/var/run/${cmd}.pid")){
272 $pid = <FILE>; chomp $pid;
273 close FILE;
274 if (open(FILE, "/proc/${pid}/status")){
275 while (<FILE>){
4e481c3a
CS
276 if (/^Name:\W+(.*)/) {
277 $testcmd = $1;
278 }
ac1cfefa 279 }
2032b6f9 280 close FILE;
ac1cfefa 281 }
24c82ed2
MT
282 if (open(FILE, "/proc/${pid}/status")) {
283 while (<FILE>) {
284 my ($key, $val) = split(":", $_, 2);
285 if ($key eq 'VmRSS') {
286 $memory = $val;
287 last;
288 }
289 }
290 close(FILE);
ac1cfefa 291 }
4e481c3a 292 if ($testcmd =~ /$exename/){
91a81810 293 $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 294 }
4e481c3a
CS
295 }
296 return $status;
ac1cfefa
MT
297}
298
f6340997 299sub isrunningaddon (@) {
2b9b31b7 300 my ($pak, $service, $col) = @_;
f6340997 301
91a81810 302 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
303 my $pid = '';
304 my $testcmd = '';
305 my $exename;
306 my @memory;
773362c5 307
2b9b31b7 308 my @testcmd = &General::system_output("/usr/local/bin/addonctrl", "$pak", "status", "$service");
31c46c07 309 my $testcmd = @testcmd[0];
ac1cfefa 310
2032b6f9 311 if ( $testcmd =~ /is\ running/ && $testcmd !~ /is\ not\ running/){
4f205b54
RR
312 $status = "<td align='center' $col width='8%'><a href='services.cgi?$pak!stop!$service'><img alt='$Lang::tr{'stop'}' title='$Lang::tr{'stop'}' src='/images/go-down.png' border='0' /></a></td> ";
313 $status .= "<td align='center' $col width='8%'><a href='services.cgi?$pak!restart!$service'><img alt='$Lang::tr{'restart'}' title='$Lang::tr{'restart'}' src='/images/reload.gif' border='0' /></a></td> ";
314 $status .= "<td align='center' bgcolor='${Header::colourgreen}'><font color='white'><b>$Lang::tr{'running'}</b></font></td>";
7fb1b9f6 315 $testcmd =~ s/.* //gi;
4e481c3a
CS
316 $testcmd =~ s/[a-z_]//gi;
317 $testcmd =~ s/\[[0-1]\;[0-9]+//gi;
318 $testcmd =~ s/[\(\)\.]//gi;
319 $testcmd =~ s/ //gi;
320 $testcmd =~ s/\e//gi;
2032b6f9 321
4e481c3a 322 my @pid = split(/\s/,$testcmd);
91a81810 323 $status .="<td align='center' $col>$pid[0]</td>";
773362c5 324
4e481c3a 325 my $memory = 0;
773362c5 326
4e481c3a
CS
327 foreach (@pid){
328 chomp($_);
329 if (open(FILE, "/proc/$_/statm")){
2032b6f9 330 my $temp = <FILE>;
4e481c3a
CS
331 @memory = split(/ /,$temp);
332 }
333 $memory+=$memory[0];
ac1cfefa 334 }
91a81810 335 $status .="<td align='center' $col>$memory KB</td>";
4e481c3a 336 }else{
4f205b54
RR
337 $status = "<td align='center' $col width='16%' colspan=2><a href='services.cgi?$pak!start!$service'><img alt='$Lang::tr{'start'}' title='$Lang::tr{'start'}' src='/images/go-up.png' border='0' /></a></td>";
338 $status .= "<td align='center' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan='2' $col></td>";
ac1cfefa 339 }
4e481c3a 340 return $status;
ac1cfefa 341}