]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/services.cgi
suricata: Change midstream policy to "pass-flow"
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / services.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2005-2021 IPFire Team #
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 ###############################################################################
21
22 use strict;
23
24 # enable only the following on debugging purpose
25 #use warnings;
26 #use CGI::Carp 'fatalsToBrowser';
27
28 require '/var/ipfire/general-functions.pl';
29 require "${General::swroot}/lang.pl";
30 require "${General::swroot}/header.pl";
31 require "${General::swroot}/graphs.pl";
32
33 my %color = ();
34 my %mainsettings = ();
35 my %netsettings=();
36 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
37 &General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
38 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
39
40 #workaround to suppress a warning when a variable is used only once
41 my @dummy = ( ${Header::colourred} );
42 undef (@dummy);
43
44
45 my %cgiparams=();
46 # Maps a nice printable name to the changing part of the pid file, which
47 # is also the name of the program
48 my %servicenames =(
49 $Lang::tr{'dhcp server'} => 'dhcpd',
50 $Lang::tr{'web server'} => 'httpd',
51 $Lang::tr{'cron server'} => 'fcron',
52 $Lang::tr{'dns proxy server'} => 'unbound',
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',
57 $Lang::tr{'vpn'} => 'charon',
58 $Lang::tr{'web proxy'} => 'squid',
59 $Lang::tr{'intrusion detection system'} => 'suricata',
60 'OpenVPN' => 'openvpn'
61 );
62
63 my %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>",
75 "$Lang::tr{'intrusion detection system'}" => "<a href=\'ids.cgi\'>$Lang::tr{'intrusion detection system'}</a>",
76 );
77
78 # Hash to overwrite the process name of a process if it differs fromt the launch command.
79 my %overwrite_exename_hash = (
80 "suricata" => "Suricata-Main"
81 );
82
83 my $lines=0; # Used to count the outputlines to make different bgcolor
84
85 my @querry = split(/\?/,$ENV{'QUERY_STRING'});
86 $querry[0] = '' unless defined $querry[0];
87 $querry[1] = 'hour' unless defined $querry[1];
88
89 if ( $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
104 <div align='center'>
105 <table width='80%' cellspacing='1' class='tbl'>
106 <tr>
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>
112 END
113 ;
114 my $key = '';
115 my $col="";
116 foreach $key (sort keys %servicenames){
117 $lines++;
118 if ($lines % 2){
119 $col="bgcolor='$color{'color22'}'";
120 print "<tr><td align='left' $col>";
121 print $link{$key};
122 print "</td>";
123 }else{
124 $col="bgcolor='$color{'color20'}'";
125 print "<tr><td align='left' $col>";
126 print $link{$key};
127 print "</td>";
128 }
129
130 my $shortname = $servicenames{$key};
131 my $status = &isrunning($shortname,$col);
132
133 print "$status\n";
134 print "</tr>\n";
135 }
136
137 print "</table></div>\n";
138 &Header::closebox();
139
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 ''){
144 &General::system("/usr/local/bin/addonctrl", "$param[0]", "$param[1]");
145 }
146
147 print <<END
148 <div align='center'>
149 <table width='80%' cellspacing='1' class='tbl'>
150 <tr>
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>
157 </tr>
158 END
159 ;
160
161 my $lines=0; # Used to count the outputlines to make different bgcolor
162
163 # Generate list of installed addon pak's
164 opendir (DIR, "/opt/pakfire/db/installed") || die "Cannot opendir /opt/pakfire/db/installed/: $!";
165 my @pak = sort readdir DIR;
166 closedir(DIR);
167
168 foreach (@pak){
169 chomp($_);
170 next unless (m/^meta-/);
171 s/^meta-//;
172
173 # Check which of the paks are services
174 if (-e "/etc/init.d/$_") {
175 # blacklist some packages
176 #
177 # alsa has trouble with the volume saving and was not really stopped
178 # mdadm should not stopped with webif because this could crash the system
179 #
180 if ( $_ eq 'squid' ) {
181 next;
182 }
183 if ( ($_ ne "alsa") && ($_ ne "mdadm") ) {
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 }
192
193 print "<td align='left' $col width='31%'>$_</td> ";
194 my $status = isautorun($_,$col);
195 print "$status ";
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> ";
198 my $status = &isrunningaddon($_,$col);
199 $status =~ s/\\e\[[0-1]\;[0-9]+m//g;
200
201 chomp($status);
202 print "$status";
203 print "</tr>";
204 }
205 }
206 }
207
208 print "</table></div>\n";
209 &Header::closebox();
210
211 &Header::openbox('100%', 'center', "$Lang::tr{'processes'} $Lang::tr{'graph'}");
212 &Graphs::makegraphbox("services.cgi","processescpu","day");
213 &Header::closebox();
214
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();
221 }
222
223 sub isautorun (@) {
224 my ($cmd, $col) = @_;
225
226 # Init directory.
227 my $initdir = "/etc/rc.d/rc3.d/";
228
229 my $status = "<td align='center' $col></td>";
230
231 # Check if autorun for the given cmd is enabled.
232 if ( &find_init("$cmd", "$initdir") ) {
233 # Adjust status.
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>";
235 } else {
236 # Adjust status.
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>";
238 }
239
240 # Return the status.
241 return $status;
242 }
243
244 sub 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
269 sub isrunning (@) {
270 my ($cmd, $col) = @_;
271 my $status = "<td align='center' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan='2' $col></td>";
272 my $pid = '';
273 my $testcmd = '';
274 my $exename;
275 my $memory;
276
277 $cmd =~ /(^[a-z]+)/;
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 }
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>){
298 if (/^Name:\W+(.*)/) {
299 $testcmd = $1;
300 }
301 }
302 close FILE;
303 }
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);
313 }
314 if ($testcmd =~ /$exename/){
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>";
316 }
317 }
318 return $status;
319 }
320
321 sub isrunningaddon (@) {
322 my ($cmd, $col) = @_;
323
324 my $status = "<td align='center' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan='2' $col></td>";
325 my $pid = '';
326 my $testcmd = '';
327 my $exename;
328 my @memory;
329
330 my @testcmd = &General::system_output("/usr/local/bin/addonctrl", "$cmd", "status");
331 my $testcmd = @testcmd[0];
332
333 if ( $testcmd =~ /is\ running/ && $testcmd !~ /is\ not\ running/){
334 $status = "<td align='center' bgcolor='${Header::colourgreen}'><font color='white'><b>$Lang::tr{'running'}</b></font></td>";
335 $testcmd =~ s/.* //gi;
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;
341
342 my @pid = split(/\s/,$testcmd);
343 $status .="<td align='center' $col>$pid[0]</td>";
344
345 my $memory = 0;
346
347 foreach (@pid){
348 chomp($_);
349 if (open(FILE, "/proc/$_/statm")){
350 my $temp = <FILE>;
351 @memory = split(/ /,$temp);
352 }
353 $memory+=$memory[0];
354 }
355 $status .="<td align='center' $col>$memory KB</td>";
356 }else{
357 $status = "<td align='center' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan='2' $col></td>";
358 }
359 return $status;
360 }