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