]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/logs.cgi/ipblocklists.dat
suricata: Change midstream policy to "pass-flow"
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / logs.cgi / ipblocklists.dat
CommitLineData
09a8d612
TF
1#!/usr/bin/perl
2#
3# SmoothWall CGIs
4#
5# This code is distributed under the terms of the GPL
6#
7# JC HERITIER
8# page inspired from the initial firewalllog.dat
9#
10# Modified for IPFire by Christian Schmidt
11# and Michael Tremer (www.ipfire.org)
12
13use strict;
09a8d612
TF
14
15# enable only the following on debugging purpose
16#use warnings;
17#use CGI::Carp 'fatalsToBrowser';
18
19require '/var/ipfire/general-functions.pl';
09a8d612
TF
20require "${General::swroot}/lang.pl";
21require "${General::swroot}/header.pl";
22
23require "${General::swroot}/ipblocklist-functions.pl";
24require "${General::swroot}/ipblocklist/sources";
25
26use POSIX();
27
28my %cgiparams=();
29my $errormessage = '';
30
31my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
32 'Sep', 'Oct', 'Nov', 'Dec' );
33my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
34 $Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'},
35 $Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'},
36 $Lang::tr{'december'} );
37
38my @now = localtime();
39my $dow = $now[6];
40my $doy = $now[7];
41my $tdoy = $now[7];
42my $year = $now[5]+1900;
43
44$cgiparams{'DAY'} = $now[3];
45$cgiparams{'MONTH'} = $now[4];
46$cgiparams{'ACTION'} = '';
47
48&Header::getcgihash(\%cgiparams);
49
50my $start = -1;
51if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
52{
53 my @temp = split(',',$ENV{'QUERY_STRING'});
54 $start = $temp[0];
55 $cgiparams{'MONTH'} = $temp[1];
56 $cgiparams{'DAY'} = $temp[2];
57}
58
59if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
60 !($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)$/))
61{
62 $cgiparams{'DAY'} = $now[3];
63 $cgiparams{'MONTH'} = $now[4];
64}
65elsif($cgiparams{'ACTION'} eq '>>')
66{
67 my @temp_then=();
68 my @temp_now = localtime(time);
69 $temp_now[4] = $cgiparams{'MONTH'};
70 $temp_now[3] = $cgiparams{'DAY'};
71 @temp_then = localtime(POSIX::mktime(@temp_now) + 86400);
72 ## Retrieve the same time on the next day -
73 ## 86400 seconds in a day
74 $cgiparams{'MONTH'} = $temp_then[4];
75 $cgiparams{'DAY'} = $temp_then[3];
76}
77elsif($cgiparams{'ACTION'} eq '<<')
78{
79 my @temp_then=();
80 my @temp_now = localtime(time);
81 $temp_now[4] = $cgiparams{'MONTH'};
82 $temp_now[3] = $cgiparams{'DAY'};
83 @temp_then = localtime(POSIX::mktime(@temp_now) - 86400);
84 ## Retrieve the same time on the previous day -
85 ## 86400 seconds in a day
86 $cgiparams{'MONTH'} = $temp_then[4];
87 $cgiparams{'DAY'} = $temp_then[3];
88}
89
90if (($cgiparams{'DAY'} ne $now[3]) || ($cgiparams{'MONTH'} ne $now[4]))
91{
92 my @then = ();
93 if ( ( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
94 ( $cgiparams{'MONTH'} > $now[4] ) ) {
95 @then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 ));
96 } else {
97 @then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 ));
98 }
99 $tdoy = $then[7];
100 my $lastleap=($year-1)%4;
101 if ($tdoy>$doy) {
102 if ($lastleap == 0 && $tdoy < 60) {
103 $doy=$tdoy+366;
104 } else {
105 $doy=$doy+365;
106 }
107 }
108}
109
110my $datediff=0;
111my $dowd=0;
112my $multifile=0;
113if ($tdoy ne $doy) {
114 $datediff=int(($doy-$tdoy)/7);
115 $dowd=($doy-$tdoy)%7;
116 if (($dow-$dowd)<1) {
117 $datediff=$datediff+1;
118 }
119 if (($dow-$dowd)==0) {
120 $multifile=1;
121 }
122}
123
124my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
125my $longmonthstr = $longmonths[$cgiparams{'MONTH'}];
126my $day = $cgiparams{'DAY'};
127my $daystr='';
128if ($day <= 9) {
129 $daystr = " $day"; }
130else {
131 $daystr = $day;
132}
133
134my %lists;
135my %directions;
136my %sources = ();
137my %settings = ();
138&General::readhash("${General::swroot}/ipblocklist/settings", \%settings);
139
140# Get all available blocklists.
141my @blocklists = &IPblocklist::get_blocklists();
142
143foreach my $blocklist (@blocklists)
144{
145 $lists{$blocklist} = {} if ($settings{$blocklist} eq 'on');
146}
147
148my $skip=0;
149my $filestr='';
150if ($datediff==0) {
151 $filestr="/var/log/messages";
152} else {
153 $filestr="/var/log/messages.$datediff";
154 $filestr = "$filestr.gz" if -f "$filestr.gz";
155}
156
157if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
158 $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
159 $skip=1;
160 # Note: This is in case the log does not exist for that date
161}
162
163my $lines = 0;
164my $directions = 0;
165
166if (!$skip)
167{
168 while (<FILE>)
169 {
170 if (/^${monthstr} ${daystr} ..:..:.. [\w\-]+ kernel:.*BLKLST_(\w+)\s*IN=(\w*)/)
171 {
172 my $list = $1;
173
174 if ($2 =~ m/ppp|red/)
175 {
176 $lists{$list}{in}++;
177 $directions{in}++;
178 }
179 else
180 {
181 $lists{$list}{out}++;
182 $directions{out}++;
183 }
184
185 $lines++;
186 }
187
188 }
189 close (FILE);
190}
191
192if ($multifile) {
193 $datediff=$datediff-1;
194 if ($datediff==0) {
195 $filestr="/var/log/messages";
196 } else {
197 $filestr="/var/log/messages.$datediff";
198 $filestr = "$filestr.gz" if -f "$filestr.gz";
199 }
200 if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
201 $errormessage="$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
202 $skip=1;
203 }
204 if (!$skip) {
205 while (<FILE>) {
206 if (/^${monthstr} ${daystr} ..:..:.. [\w\-]+ kernel:.*BLKLST_(\w+)\s*IN=(\w+)/)
207 {
208 my $list = $1;
209
210 if ($2 =~ m/ppp|red/)
211 {
212 $lists{$list}{in}++;
213 $directions{in}++;
214 }
215 else
216 {
217 $lists{$list}{out}++;
218 $directions{out}++;
219 }
220
221 $lines++;
222 }
223 }
224 close (FILE);
225 }
226}
227
228my $MODNAME="fwlogs";
229
230&Header::showhttpheaders();
231&Header::openpage($Lang::tr{'ipblocklist logs'}, 1, '');
232&Header::openbigbox('100%', 'left', '', $errormessage);
233
234
235if ($errormessage) {
236 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
237 print "<font class='base'>$errormessage&nbsp;</font>\n";
238 &Header::closebox();
239}
240
241&Header::openbox('100%', 'left', "$Lang::tr{'settings'}");
242
243print <<END
244<form method='post' action='$ENV{'SCRIPT_NAME'}'>
245<table width='100%'>
246<tr>
247 <td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
248 <td width='10%'>
249 <select name='MONTH'>
250END
251;
252my $month;
253for ($month = 0; $month < 12; $month++)
254{
255 print "\t<option ";
256 if ($month == $cgiparams{'MONTH'}) {
257 print "selected='selected' ";
258 }
259 print "value='$month'>$longmonths[$month]</option>\n";
260}
261print <<END
262 </select>
263 </td>
264 <td width='10%' class='base' align='right'>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
265 <td width='40%'>
266 <select name='DAY'>
267END
268;
269for ($day = 1; $day <= 31; $day++)
270{
271 print "\t<option ";
272 if ($day == $cgiparams{'DAY'}) {
273 print "selected='selected' ";
274 }
275 print "value='$day'>$day</option>\n";
276}
277
278print <<END
279</select>
280</td>
281<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
282<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
283<td width='20%' align='right'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
284</tr>
285</table>
286</form>
287END
288;
289
290&Header::closebox();
291
292&Header::openbox('100%', 'left', $Lang::tr{'firewall log'});
293print "<p><b>$Lang::tr{'ipblocklist hits'} $longmonthstr $daystr: $lines</b></p>";
294
295my %color = ();
296my %mainsettings = ();
297&General::readhash("${General::swroot}/main/settings", \%mainsettings);
98d50c03 298&General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
09a8d612
TF
299
300my @lists = sort keys (%lists);
301
302print <<END
303<table width='100%' class='tbl'>
304<tr>
305<th align='center' class='boldbase' rowspan='2'></th>
306<th align='left' class='boldbase' rowspan='2'><b>$Lang::tr{'ipblocklist id'}</b></th>
307<th align='left' class='boldbase' rowspan='2'><b>$Lang::tr{'ipblocklist category'}</b></th>
308<th align='center' class='boldbase' colspan='2'><b>$Lang::tr{'ipblocklist input'}</b></th>
309<th align='center' class='boldbase' colspan='2'><b>$Lang::tr{'ipblocklist output'}</b></th>
310</tr>
311<tr>
312<th align='center' class='boldbase'>$Lang::tr{'count'}</th>
313<th align='center' class='boldbase'>$Lang::tr{'percentage'}</th>
314<th align='center' class='boldbase'>$Lang::tr{'count'}</th>
315<th align='center' class='boldbase'>$Lang::tr{'percentage'}</th>
316</tr>
317END
318;
319
320$lines = 0;
321my $lists = join ',', @lists;
322
323foreach my $list (@lists)
324{
325 my $col = ($lines++ % 2) ? "bgcolor='$color{'color20'}'" : "bgcolor='$color{'color22'}'";
326 my $category = exists( $IPblocklist::List::sources{$list}) ? $Lang::tr{"ipblocklist category $IPblocklist::List::sources{$list}{'category'}"} : '&nbsp;';
327
328 print "<tr>";
329
330 print "<td align='center' $col><form method='post' action='showrequestfromblocklist.dat'><input type='hidden' name='MONTH' value='$cgiparams{'MONTH'}'> <input type='hidden' name='DAY' value='$cgiparams{'DAY'}'> <input type='hidden' name='blocklist' value='$list'><input type='hidden' name='blocklists' value='$lists'> <input type='submit' value='$Lang::tr{'details'}'></form></td>";
331
332 if (exists($IPblocklist::List::sources{$list}) and $IPblocklist::List::sources{$list}{'info'})
333 {
334 print "<td $col><a href='$IPblocklist::List::sources{$list}{info}' target='_blank'>$list</a></td>";
335 }
336 else
337 {
338 print "<td $col>$list</td>";
339 }
340
341 print "<td $col>$category</td>";
342
343 foreach my $direction ('in', 'out')
344 {
345 my $count = $lists{$list}{$direction} || 0;
346 my $percent = $directions{$direction} > 0 ? $count * 100 / $directions{$direction} : 0;
347 $percent = sprintf("%.f", $percent);
348 print "<td align='center' class='boldbase' $col>$count</th>";
349 print "<td align='center' class='boldbase' $col>$percent%</th>";
350 }
351
352 print "</tr>";
353}
354print <<END
355</table>
356END
357;
358
359&Header::closebox();
360&Header::closebigbox();
361&Header::closepage();
362
363sub checkversion {
364 #Automatic Updates is disabled
365 return "0","0";
366}