[v2,3/8] ipblacklist: WUI Log page

Message ID 20200427143123.6378-4-ipfr@tfitzgeorge.me.uk
State Superseded
Headers
Series ipblacklist: IP Address Blacklists |

Commit Message

Tim FitzGeorge April 27, 2020, 2:31 p.m. UTC
  Signed-off-by: Tim FitzGeorge <ipfr@tfitzgeorge.me.uk>
---
 html/cgi-bin/logs.cgi/ipblacklists.dat | 363 +++++++++++++++++++++++++++++++++
 1 file changed, 363 insertions(+)
 create mode 100755 html/cgi-bin/logs.cgi/ipblacklists.dat
  

Patch

diff --git a/html/cgi-bin/logs.cgi/ipblacklists.dat b/html/cgi-bin/logs.cgi/ipblacklists.dat
new file mode 100755
index 000000000..9b8db7062
--- /dev/null
+++ b/html/cgi-bin/logs.cgi/ipblacklists.dat
@@ -0,0 +1,363 @@ 
+#!/usr/bin/perl
+#
+# SmoothWall CGIs
+#
+# This code is distributed under the terms of the GPL
+#
+# JC HERITIER
+# page inspired from the initial firewalllog.dat
+#
+# Modified for IPFire by Christian Schmidt
+#	                    and Michael Tremer (www.ipfire.org)
+
+use strict;
+use Getopt::Std;
+
+# enable only the following on debugging purpose
+#use warnings;
+#use CGI::Carp 'fatalsToBrowser';
+
+require '/var/ipfire/general-functions.pl';
+require "${General::swroot}/geoip-functions.pl";
+require "${General::swroot}/lang.pl";
+require "${General::swroot}/header.pl";
+
+use POSIX();
+
+my %cgiparams=();
+my $errormessage = '';
+
+my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
+	'Sep', 'Oct', 'Nov', 'Dec' );
+my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
+	$Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'},
+	$Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'},
+	$Lang::tr{'december'} );
+
+my @now = localtime();
+my $dow = $now[6];
+my $doy = $now[7];
+my $tdoy = $now[7];
+my $year = $now[5]+1900;
+
+$cgiparams{'DAY'} = $now[3];
+$cgiparams{'MONTH'} = $now[4];
+$cgiparams{'ACTION'} = '';
+
+&Header::getcgihash(\%cgiparams);
+
+my $start = -1;
+if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
+{
+	my @temp = split(',',$ENV{'QUERY_STRING'});
+	$start = $temp[0];
+	$cgiparams{'MONTH'} = $temp[1];
+	$cgiparams{'DAY'} = $temp[2];
+}
+
+if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
+    !($cgiparams{'DAY'} =~ /^(1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)$/))
+{
+	$cgiparams{'DAY'} = $now[3];
+	$cgiparams{'MONTH'} = $now[4];
+}
+elsif($cgiparams{'ACTION'} eq '>>')
+{
+	my @temp_then=();
+	my @temp_now = localtime(time);
+	$temp_now[4] = $cgiparams{'MONTH'};
+	$temp_now[3] = $cgiparams{'DAY'};
+	@temp_then = localtime(POSIX::mktime(@temp_now) + 86400);
+	## Retrieve the same time on the next day -
+	## 86400 seconds in a day
+	$cgiparams{'MONTH'} = $temp_then[4];
+	$cgiparams{'DAY'} = $temp_then[3];
+}
+elsif($cgiparams{'ACTION'} eq '<<')
+{
+	my @temp_then=();
+	my @temp_now = localtime(time);
+	$temp_now[4] = $cgiparams{'MONTH'};
+	$temp_now[3] = $cgiparams{'DAY'};
+	@temp_then = localtime(POSIX::mktime(@temp_now) - 86400);
+	## Retrieve the same time on the previous day -
+	## 86400 seconds in a day
+	$cgiparams{'MONTH'} = $temp_then[4];
+	$cgiparams{'DAY'} = $temp_then[3];
+}
+
+if (($cgiparams{'DAY'} ne $now[3]) || ($cgiparams{'MONTH'} ne $now[4]))
+{
+	my @then = ();
+	if ( (  $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
+	     ( $cgiparams{'MONTH'} > $now[4] ) ) {
+		@then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 ));
+	} else {
+		@then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 ));
+	}
+	$tdoy = $then[7];
+	my $lastleap=($year-1)%4;
+	if ($tdoy>$doy) {
+		if ($lastleap == 0 && $tdoy < 60) {
+			$doy=$tdoy+366;
+		} else {
+			$doy=$doy+365;
+		}
+	}
+}
+
+my $datediff=0;
+my $dowd=0;
+my $multifile=0;
+if ($tdoy ne $doy) {
+	$datediff=int(($doy-$tdoy)/7);
+	$dowd=($doy-$tdoy)%7;
+	if (($dow-$dowd)<1) {
+		$datediff=$datediff+1;
+	}
+	if (($dow-$dowd)==0) {
+		$multifile=1;
+	}
+}
+
+my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
+my $longmonthstr = $longmonths[$cgiparams{'MONTH'}];
+my $day = $cgiparams{'DAY'};
+my $daystr='';
+if ($day <= 9) {
+	$daystr = " $day"; }
+else {
+	$daystr = $day;
+}
+
+my %lists;
+my %directions;
+my %sources = ();
+my %settings = ();
+&General::readhash("${General::swroot}/ipblacklist/settings", \%settings);
+eval qx|/bin/cat /var/ipfire/ipblacklist/sources|;
+
+foreach my $blacklist (keys %sources)
+{
+	$lists{$blacklist} = {} if ($settings{$blacklist} eq 'on');
+}
+
+my $skip=0;
+my $filestr='';
+if ($datediff==0) {
+	$filestr="/var/log/messages";
+} else {
+	$filestr="/var/log/messages.$datediff";
+	$filestr = "$filestr.gz" if -f "$filestr.gz";
+}
+
+if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
+	$errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
+	$skip=1;
+	# Note: This is in case the log does not exist for that date
+}
+
+my $lines = 0;
+my $directions = 0;
+
+if (!$skip)
+{
+	while (<FILE>)
+	{
+		if (/^${monthstr} ${daystr} ..:..:.. [\w\-]+ kernel:.*BLKLST_(\w+)\s*IN=(\w*)/)
+		{
+			my $list = $1;
+
+			if ($2 =~ m/ppp|red/)
+			{
+				$lists{$list}{in}++;
+				$directions{in}++;
+			}
+			else
+			{
+				$lists{$list}{out}++;
+				$directions{out}++;
+			}
+
+			$lines++;
+		}
+
+	}
+	close (FILE);
+}
+
+if ($multifile) {
+	$datediff=$datediff-1;
+	if ($datediff==0) {
+		$filestr="/var/log/messages";
+	} else {
+		$filestr="/var/log/messages.$datediff";
+		$filestr = "$filestr.gz" if -f "$filestr.gz";
+	}
+	if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
+		$errormessage="$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
+		$skip=1;
+	}
+	if (!$skip) {
+		while (<FILE>) {
+			if (/^${monthstr} ${daystr} ..:..:.. [\w\-]+ kernel:.*BLKLST_(\w+)\s*IN=(\w+)/)
+			{
+				my $list = $1;
+
+				if ($2 =~ m/ppp|red/)
+				{
+					$lists{$list}{in}++;
+					$directions{in}++;
+				}
+				else
+				{
+					$lists{$list}{out}++;
+					$directions{out}++;
+				}
+
+				$lines++;
+			}
+		}
+		close (FILE);
+	}
+}
+
+my $MODNAME="fwlogs";
+
+&Header::showhttpheaders();
+&Header::openpage($Lang::tr{'ipblacklist logs'}, 1, '');
+&Header::openbigbox('100%', 'left', '', $errormessage);
+
+
+if ($errormessage) {
+	&Header::openbox('100%', 'left', $Lang::tr{'error messages'});
+	print "<font class='base'>$errormessage&nbsp;</font>\n";
+	&Header::closebox();
+}
+
+&Header::openbox('100%', 'left', "$Lang::tr{'settings'}");
+
+print <<END
+<form method='post' action='$ENV{'SCRIPT_NAME'}'>
+<table width='100%'>
+<tr>
+	<td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
+	<td width='10%'>
+	<select name='MONTH'>
+END
+;
+my $month;
+for ($month = 0; $month < 12; $month++)
+{
+	print "\t<option ";
+	if ($month == $cgiparams{'MONTH'}) {
+		print "selected='selected' ";
+	}
+	print "value='$month'>$longmonths[$month]</option>\n";
+}
+print <<END
+	</select>
+	</td>
+	<td width='10%' class='base' align='right'>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
+	<td width='40%'>
+	<select name='DAY'>
+END
+;
+for ($day = 1; $day <= 31; $day++)
+{
+	print "\t<option ";
+	if ($day == $cgiparams{'DAY'}) {
+		print "selected='selected' ";
+	}
+	print "value='$day'>$day</option>\n";
+}
+
+print <<END
+</select>
+</td>
+<td width='5%'  align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
+<td width='5%'  align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
+<td width='20%' align='right'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
+</tr>
+</table>
+</form>
+END
+;
+
+&Header::closebox();
+
+&Header::openbox('100%', 'left', $Lang::tr{'firewall log'});
+print "<p><b>$Lang::tr{'ipblacklist hits'} $longmonthstr $daystr: $lines</b></p>";
+
+my %color = ();
+my %mainsettings = ();
+&General::readhash("${General::swroot}/main/settings", \%mainsettings);
+&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
+
+my @lists = sort keys (%lists);
+
+print <<END
+<table width='100%' class='tbl'>
+<tr>
+<th align='center' class='boldbase' rowspan='2'></th>
+<th align='left'   class='boldbase' rowspan='2'><b>$Lang::tr{'ipblacklist id'}</b></th>
+<th align='left'   class='boldbase' rowspan='2'><b>$Lang::tr{'ipblacklist category'}</b></th>
+<th align='center' class='boldbase' colspan='2'><b>$Lang::tr{'ipblacklist input'}</b></th>
+<th align='center' class='boldbase' colspan='2'><b>$Lang::tr{'ipblacklist output'}</b></th>
+</tr>
+<tr>
+<th align='center' class='boldbase'>$Lang::tr{'count'}</th>
+<th align='center' class='boldbase'>$Lang::tr{'percentage'}</th>
+<th align='center' class='boldbase'>$Lang::tr{'count'}</th>
+<th align='center' class='boldbase'>$Lang::tr{'percentage'}</th>
+</tr>
+END
+;
+
+$lines = 0;
+my $lists = join ',', @lists;
+
+foreach my $list (@lists)
+{
+	my $col = ($lines++ % 2) ? "bgcolor='$color{'color20'}'" : "bgcolor='$color{'color22'}'";
+	my $category = exists( $sources{$list}) ? $Lang::tr{"ipblacklist category $sources{$list}{'category'}"} : '&nbsp;';
+
+	print "<tr>";
+
+	print "<td align='center' $col><form method='post' action='showrequestfromblacklist.dat'><input type='hidden' name='MONTH' value='$cgiparams{'MONTH'}'> <input type='hidden' name='DAY' value='$cgiparams{'DAY'}'> <input type='hidden' name='blacklist' value='$list'><input type='hidden' name='blacklists' value='$lists'> <input type='submit' value='$Lang::tr{'details'}'></form></td>";
+
+	if (exists($sources{$list}) and $sources{$list}{'info'})
+	{
+		print "<td $col><a href='$sources{$list}{info}' target='_blank'>$list</a></td>";
+	}
+	else
+	{
+		print "<td $col>$list</td>";
+	}
+
+	print "<td $col>$category</td>";
+
+	foreach my $direction ('in', 'out')
+	{
+		my $count   = $lists{$list}{$direction} || 0;
+		my $percent = $directions{$direction} > 0 ? $count * 100 / $directions{$direction} : 0;
+		$percent = sprintf("%.f", $percent);
+		print "<td align='center' class='boldbase' $col>$count</th>";
+		print "<td align='center' class='boldbase' $col>$percent%</th>";
+	}
+
+	print "</tr>";
+}
+print <<END
+</table>
+END
+;
+
+&Header::closebox();
+&Header::closebigbox();
+&Header::closepage();
+
+sub checkversion {
+	#Automatic Updates is disabled
+	return "0","0";
+}