]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/logs.cgi/showrequestfromblocklist.dat
showrequestfromblocklist.dat: Fix silly typos.
[ipfire-2.x.git] / html / cgi-bin / logs.cgi / showrequestfromblocklist.dat
CommitLineData
cd07e6c6
TF
1#!/usr/bin/perl
2# SmoothWall CGIs
3#
4# This code is distributed under the terms of the GPL
5#
6# JC HERITIER
7# page inspired from the initial firewalllog.dat
8#
9# Modified for IPFire by Christian Schmidt (www.ipfire.org)
10
11# enable only the following on debugging purpose
12#use warnings;
13#use CGI::Carp 'fatalsToBrowser';
14
15require '/var/ipfire/general-functions.pl';
16require "${General::swroot}/lang.pl";
17require "${General::swroot}/header.pl";
18
19use POSIX();
20
21#workaround to suppress a warning when a variable is used only once
22my @dummy = ( ${Header::table2colour} );
23undef (@dummy);
24
25my %cgiparams=();
26my %logsettings=();
27my $errormessage = '';
28
29my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
30 'Sep', 'Oct', 'Nov', 'Dec' );
31my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
32 $Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'},
33 $Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'},
34 $Lang::tr{'december'} );
35
36my @now = localtime();
37my $dow = $now[6];
38my $doy = $now[7];
39my $tdoy = $now[7];
40my $year = $now[5]+1900;
41
42$cgiparams{'DAY'} = $now[3];
43$cgiparams{'MONTH'} = $now[4];
44$cgiparams{'ACTION'} = '';
45
46&Header::getcgihash(\%cgiparams);
47
48$logsettings{'LOGVIEW_REVERSE'} = 'off';
49&General::readhash("${General::swroot}/logging/settings", \%logsettings);
50
51my $start = -1;
52my @blocklists;
53if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
54{
55 my @temp = split(',',$ENV{'QUERY_STRING'}, 5);
56 $start = shift @temp;
57 $cgiparams{'MONTH'} = shift @temp;
58 $cgiparams{'DAY'} = shift @temp;
59 $cgiparams{'blocklist'} = shift @temp;
60 $cgiparams{'blocklists'} = shift @temp;
61}
62
63if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
64 !($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)$/))
65{
66 $cgiparams{'DAY'} = $now[3];
67 $cgiparams{'MONTH'} = $now[4];
68}
69elsif($cgiparams{'ACTION'} eq '>>')
70{
71 my @temp_then=();
72 my @temp_now = localtime(time);
73 $temp_now[4] = $cgiparams{'MONTH'};
74 $temp_now[3] = $cgiparams{'DAY'};
75 @temp_then = localtime(POSIX::mktime(@temp_now) + 86400);
76 ## Retrieve the same time on the next day -
77 ## 86400 seconds in a day
78 $cgiparams{'MONTH'} = $temp_then[4];
79 $cgiparams{'DAY'} = $temp_then[3];
80}
81elsif($cgiparams{'ACTION'} eq '<<')
82{
83 my @temp_then=();
84 my @temp_now = localtime(time);
85 $temp_now[4] = $cgiparams{'MONTH'};
86 $temp_now[3] = $cgiparams{'DAY'};
87 @temp_then = localtime(POSIX::mktime(@temp_now) - 86400);
88 ## Retrieve the same time on the previous day -
89 ## 86400 seconds in a day
90 $cgiparams{'MONTH'} = $temp_then[4];
91 $cgiparams{'DAY'} = $temp_then[3];
92}
93
94if (($cgiparams{'DAY'} ne $now[3]) || ($cgiparams{'MONTH'} ne $now[4]))
95{
96 my @then = ();
97 if ( ( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
98 ( $cgiparams{'MONTH'} > $now[4] ) ) {
99 @then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 ));
100 } else {
101 @then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 ));
102 }
103 $tdoy = $then[7];
104 my $lastleap=($year-1)%4;
105 if ($tdoy>$doy) {
106 if ($lastleap == 0 && $tdoy < 60) {
107 $doy=$tdoy+366;
108 } else {
109 $doy=$doy+365;
110 }
111 }
112}
113
114if ($cgiparams{'blocklists'})
115{
116 @blocklists = split ',', $cgiparams{'blocklists'};
117}
118
119my $datediff=0;
120my $dowd=0;
121my $multifile=0;
122if ($tdoy ne $doy) {
123 $datediff=int(($doy-$tdoy)/7);
124 $dowd=($doy-$tdoy)%7;
125 if (($dow-$dowd)<1) {
126 $datediff=$datediff+1;
127 }
128 if (($dow-$dowd)==0) {
129 $multifile=1;
130 }
131}
132
133my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
134my $longmonthstr = $longmonths[$cgiparams{'MONTH'}];
135my $day = $cgiparams{'DAY'};
136my $daystr='';
137if ($day <= 9) {
138 $daystr = " $day"; }
139else {
140 $daystr = $day;
141}
142
143my $skip=0;
144my $filestr='';
145if ($datediff==0) {
146 $filestr="/var/log/messages";
147} else {
148 $filestr="/var/log/messages.$datediff";
149 $filestr = "$filestr.gz" if -f "$filestr.gz";
150}
151
152if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
153 $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
154 $skip=1;
155 # Note: This is in case the log does not exist for that date
156}
157my $lines = 0;
158my @log=();
6223e2b4 159my $blocklist = $cgiparams{blocklist};
cd07e6c6
TF
160
161if (!$skip)
162{
163 while (<FILE>) {
3e0cb28f 164 if (/^${monthstr} ${daystr} ..:..:.. [\w\-]+ kernel:.*BLKLST_(\w+)\s?IN=.*/) {
6223e2b4 165 if($1 eq $blocklist){
cd07e6c6
TF
166 $log[$lines] = $_;
167 $lines++;
168 }
169 }
170 }
171 close (FILE);
172}
173
174$skip=0;
175if ($multifile) {
176 $datediff=$datediff-1;
177 if ($datediff==0) {
178 $filestr="/var/log/messages";
179 } else {
180 $filestr="/var/log/messages.$datediff";
181 $filestr = "$filestr.gz" if -f "$filestr.gz";
182 }
183 if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
184 $errormessage="$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
185 $skip=1;
186 }
187 if (!$skip) {
188 while (<FILE>) {
3e0cb28f 189 if (/^${monthstr} ${daystr} ..:..:.. [\w\-]+ kernel:.*BLKLST_(\w+)\s?IN=.*/) {
6223e2b4 190 if($1 eq $blocklist){
cd07e6c6
TF
191 $log[$lines] = $_;
192 $lines++;
193 }
194 }
195 }
196 close (FILE);
197 }
198}
199
200&Header::showhttpheaders();
201&Header::openpage($Lang::tr{'ipblocklist log list'}, 1, '');
202&Header::openbigbox('100%', 'left', '', $errormessage);
203
204if ($errormessage) {
205 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
206 print "<font class='base'>$errormessage&nbsp;</font>\n";
207 &Header::closebox();
208}
209
210&Header::openbox('100%', 'left', "$Lang::tr{'settings'}:");
211
212print <<END
213<form method='post' action='$ENV{'SCRIPT_NAME'}'>
214<input type='hidden' name='blocklists' value='$cgiparams{blocklists}'>
215<table width='100%'>
216<tr>
217 <td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
218 <td width='10%'>
219 <select name='MONTH'>
220END
221;
222my $month;
223for ($month = 0; $month < 12; $month++)
224{
225 print "\t<option ";
226 if ($month == $cgiparams{'MONTH'}) {
227 print "selected='selected' "; }
228 print "value='$month'>$longmonths[$month]</option>\n";
229}
230print <<END
231 </select>
232 </td>
233 <td width='10%' class='base' align='right'>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
234 <td width='40%'>
235 <select name='DAY'>
236END
237;
238for ($day = 1; $day <= 31; $day++)
239{
240 print "\t<option ";
241 if ($day == $cgiparams{'DAY'}) {
242 print "selected='selected' "; }
243 print "value='$day'>$day</option>\n";
244}
245print <<END
246</select>
247</td>
248<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
249<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
250<td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
251<tr><td width='15%'>$Lang::tr{'ipblocklist id'}</td><td><select name='blocklist'>
252END
253;
254
255foreach my $option (@blocklists)
256{
257 my $selected = $option eq $cgiparams{blocklist} ? ' selected' : '';
258 print "<option value='$option'$selected>$option</option>";
259}
260
261print <<END
262</select></td></tr>
263</tr>
264</table>
265</form>
266END
267;
268
269&Header::closebox();
270
271&Header::openbox('100%', 'left', $Lang::tr{'ipblocklist log list'});
272print "<p><b>$Lang::tr{'firewall hits'} $longmonthstr $daystr: $lines</b></p>";
273
274if ($start == -1) {
275 $start = $lines - ${Header::viewsize};
276}
277if ($start >= $lines - ${Header::viewsize}) { $start = $lines - ${Header::viewsize}; };
278if ($start < 0) { $start = 0; }
279
280my $prev = $start - ${Header::viewsize};
281my $next = $start + ${Header::viewsize};
282
283if ($prev < 0) { $prev = 0; }
284if ($next >= $lines) { $next = -1 }
285if ($start == 0) { $prev = -1; }
286
287if ($lines != 0) { &oldernewer(); }
288
289print <<END
290<table width='100%'>
291<tr>
292<td width='12%' align='center' class='boldbase'><b>$Lang::tr{'time'}</b></td>
293<td width='6%' align='center' class='boldbase'><b>$Lang::tr{'iface'}</b></td>
294<td width='6%' align='center' class='boldbase'><b>$Lang::tr{'proto'}</b></td>
295<td width='18%' align='center' class='boldbase'><b>$Lang::tr{'source'}</b></td>
296<td width='15%' align='center' class='boldbase'><b>$Lang::tr{'src port'}</b></td>
297<td width='18%' align='center' class='boldbase'><b>$Lang::tr{'destination'}</b></td>
298<td width='15%' align='center' class='boldbase'><b>$Lang::tr{'dst port'}</b></td>
299</tr>
300END
301;
302
303my @slice = splice(@log, $start, ${Header::viewsize});
304
305if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @slice = reverse @slice; }
306
307$lines = 0;
308foreach $_ (@slice) {
309 $a = $_;
310 # Check whether valid ipv4 or ipv6 address
3e0cb28f 311 if (($_ =~ /BLKLST_(\w+)\s?IN=/)) {
6223e2b4 312 if($1 eq $blocklist) {
cd07e6c6
TF
313 my $in = '-'; my $out = '-';
314 my $srcaddr = ''; my $dstaddr = '';
315 my $protostr = '';
316 my $srcport = ''; my $dstport = '';
317
318 # If ipv6 uses bridge, the use PHYSIN, otherwise use IN
319 if ($_ =~ /(^.* ..:..:..) [\w\-]+ kernel:.*(IN=.*)(PHYSIN=.*)$/) {}
320 elsif ($_ =~ /(^.* ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {}
321 my $timestamp = $1; my $packet = $2;
322 $timestamp =~ /(...) (..) (..:..:..)/;
323 my $month = $1; my $day = $2; my $time = $3;
324
325 # If ipv6 uses bridge, the use PHYSIN and PHYSOUT, otherwise use IN and OUT
326 if ($a =~ /PHYSIN=(\w+)/) { $iface = $1; } elsif ($a =~ /IN=(\w+)/) { $iface = $1; }
327 if ($a =~ /PHYSOUT=(\w+)/) { $out = $1; } elsif ($a =~ /OUT=(\w+)/) { $out = $1; }
328 # Detect ipv4 and ipv6 addresses
329 if (($a =~ /SRC\=(([\d]{1,3})(\.([\d]{1,3})){3})/) or ($a =~ /SRC\=(([0-9a-fA-F]{0,4})(\:([0-9a-fA-F]{0,4})){2,7})/)) { $srcaddr = $1; }
330 if (($a =~ /DST\=(([\d]{1,3})(\.([\d]{1,3})){3})/) or ($a =~ /DST\=(([0-9a-fA-F]{0,4})(\:([0-9a-fA-F]{0,4})){2,7})/)) { $dstaddr = $1; }
331 if ($a =~ /PROTO\=(\w+)/) { $protostr = $1; }
332 my $protostrlc = lc($protostr);
333 if ($a =~ /SPT\=([\d\.]+)/){ $srcport = $1; }
334 if ($a =~ /DPT\=([\d\.]+)/){ $dstport = $1; }
335
336 if ($lines % 2) {
337 print "<tr bgcolor='${Header::table1colour}'>\n";
338 }
339 else {
340 print "<tr bgcolor='${Header::table2colour}'>\n";
341 }
342 print <<END
343 <td align='center'>$time</td>
344 <td align='center'>$iface</td>
345 <td align='center'>$protostr</td>
346 <td align='center'>
347 <table width='100%' cellpadding='0' cellspacing='0'><tr>
348 <td align='center'><a href='/cgi-bin/ipinfo.cgi?ip=$srcaddr'>$srcaddr</a></td>
349 </tr></table>
350 </td>
351 <td align='center'>$srcport</td>
352 <td align='center'>
353 <table width='100%' cellpadding='0' cellspacing='0'><tr>
354 <td align='center'><a href='/cgi-bin/ipinfo.cgi?ip=$dstaddr'>$dstaddr</a></td>
355 </tr></table>
356 </td>
357 <td align='center'>$dstport</td>
358 </tr>
359END
360 ;
361 $lines++;
362 }
363 }
364}
365
366print <<END
367</table>
368END
369;
370
371&oldernewer();
372
373 print"<table width='100%'><tr><td align='center'><a href='/cgi-bin/logs.cgi/ipblocklists.dat'><img src='/images/back.png' alt='$Lang::tr{'back'}' title='$Lang::tr{'back'}' /></a></td></tr></table>";
374
375&Header::closebox();
376
377&Header::closebigbox();
378
379&Header::closepage();
380
381sub oldernewer
382{
383 print <<END
384 <table width='100%'>
385 <tr>
386END
387 ;
388
389 my $blocklists = join ',', @blocklists;
390
391 print "<td align='center' width='50%'>";
392 if ($prev != -1) {
393 print "<a href='/cgi-bin/logs.cgi/showrequestfromblocklist.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{blocklist},$blocklists'>$Lang::tr{'older'}</a>";
394 }
395 else {
396 print "$Lang::tr{'older'}";
397 }
398 print "</td>\n";
399
400 print "<td align='center' width='50%'>";
401 if ($next != -1) {
402 print "<a href='/cgi-bin/logs.cgi/showrequestfromblocklist.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{blocklist},$blocklists'>$Lang::tr{'newer'}</a>";
403 }
404 else {
405 print "$Lang::tr{'newer'}";
406 }
407 print "</td>\n";
408
409 print <<END
410 </tr>
411 </table>
412END
413 ;
414}