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