]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/logs.cgi/showrequestfromip.dat
Fixed detection of firewall chain when bridge is used for ipv6
[ipfire-2.x.git] / html / cgi-bin / logs.cgi / showrequestfromip.dat
CommitLineData
76dd79d1
MT
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;
52if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
53{
54 my @temp = split(',',$ENV{'QUERY_STRING'});
55 $start = $temp[0];
56 $cgiparams{'MONTH'} = $temp[1];
57 $cgiparams{'DAY'} = $temp[2];
58 $cgiparams{ip} = $temp[3];
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}
111my $datediff=0;
112my $dowd=0;
113my $multifile=0;
114if ($tdoy ne $doy) {
115 $datediff=int(($doy-$tdoy)/7);
116 $dowd=($doy-$tdoy)%7;
117 if (($dow-$dowd)<1) {
118 $datediff=$datediff+1;
119 }
120 if (($dow-$dowd)==0) {
121 $multifile=1;
122 }
123}
124
125my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
126my $longmonthstr = $longmonths[$cgiparams{'MONTH'}];
127my $day = $cgiparams{'DAY'};
128my $daystr='';
129if ($day <= 9) {
130 $daystr = " $day"; }
131else {
132 $daystr = $day;
133}
134
135my $skip=0;
136my $filestr='';
137if ($datediff==0) {
138 $filestr="/var/log/messages";
139} else {
140 $filestr="/var/log/messages.$datediff";
141 $filestr = "$filestr.gz" if -f "$filestr.gz";
142}
143
144if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
145 $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
146 $skip=1;
147 # Note: This is in case the log does not exist for that date
148}
149my $lines = 0;
150my @log=();
151my $ip = $cgiparams{ip};
152
153if (!$skip)
154{
155 while (<FILE>)
156 {
157 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
9877d28d 158 if (($_ =~ /SRC\=(([\d]{1,3})(\.([\d]{1,3})){3})/) or ($_ =~ /SRC\=(([0-9a-fA-F]{0,4})(\:([0-9a-fA-F]{0,4})){2,7})/)) {
76dd79d1
MT
159 if($1 eq $ip){
160 $log[$lines] = $_;
161 $lines++;
162 }
163 }
164 }
165 }
166 close (FILE);
167}
168
169$skip=0;
170if ($multifile) {
171 $datediff=$datediff-1;
172 if ($datediff==0) {
173 $filestr="/var/log/messages";
174 } else {
175 $filestr="/var/log/messages.$datediff";
176 $filestr = "$filestr.gz" if -f "$filestr.gz";
177 }
178 if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
179 $errormessage="$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
180 $skip=1;
181 }
182 if (!$skip) {
183 while (<FILE>) {
184 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
9877d28d
ME
185 if (($_ =~ /SRC\=(([\d]{1,3})(\.([\d]{1,3})){3})/) or ($_ =~ /SRC\=(([0-9a-fA-F]{0,4})(\:([0-9a-fA-F]{0,4})){2,7})/)) {
186 if($1 eq $ip){
76dd79d1
MT
187 $log[$lines] = $_;
188 $lines++;
9877d28d
ME
189 }
190 }
76dd79d1
MT
191 }
192 }
193 close (FILE);
194 }
195}
196
197&Header::showhttpheaders();
198&Header::openpage($Lang::tr{'firewall log'}, 1, '');
199&Header::openbigbox('100%', 'left', '', $errormessage);
200
201if ($errormessage) {
202 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
203 print "<font class='base'>$errormessage&nbsp;</font>\n";
204 &Header::closebox();
205}
206
207&Header::openbox('100%', 'left', "$Lang::tr{'settings'}:");
208
209print <<END
210<form method='post' action='$ENV{'SCRIPT_NAME'}'>
211<table width='100%'>
212<tr>
213 <td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
214 <td width='10%'>
215 <select name='MONTH'>
216END
217;
218my $month;
219for ($month = 0; $month < 12; $month++)
220{
221 print "\t<option ";
222 if ($month == $cgiparams{'MONTH'}) {
223 print "selected='selected' "; }
224 print "value='$month'>$longmonths[$month]</option>\n";
225}
226print <<END
227 </select>
228 </td>
229 <td width='10%' class='base' align='right'>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
230 <td width='40%'>
231 <select name='DAY'>
232END
233;
234for ($day = 1; $day <= 31; $day++)
235{
236 print "\t<option ";
237 if ($day == $cgiparams{'DAY'}) {
238 print "selected='selected' "; }
239 print "value='$day'>$day</option>\n";
240}
241print <<END
242</select>
243</td>
244<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
245<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
246<td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
247<tr><td width='15%'>$Lang::tr{'source ip'}</td><td><input type='text' name='ip' value='$cgiparams{ip}'size='15'></td></tr>
248</tr>
249</table>
250</form>
251END
252;
253
254&Header::closebox();
255
256&Header::openbox('100%', 'left', $Lang::tr{'firewall log'});
257print "<p><b>$Lang::tr{'firewall hits'} $longmonthstr $daystr: $lines</b></p>";
258
259if ($start == -1) {
260 $start = $lines - ${Header::viewsize}; }
261if ($start >= $lines - ${Header::viewsize}) { $start = $lines - ${Header::viewsize}; };
262if ($start < 0) { $start = 0; }
263
264my $prev = $start - ${Header::viewsize};
265my $next = $start + ${Header::viewsize};
266
267if ($prev < 0) { $prev = 0; }
268if ($next >= $lines) { $next = -1 }
269if ($start == 0) { $prev = -1; }
270
271if ($lines != 0) { &oldernewer(); }
272
273print <<END
b740cdd7
AH
274<table width='100%'>
275<tr>
276<td width='10%' align='center' class='boldbase'><b>$Lang::tr{'time'}</b></td>
277<td width='13%' align='center' class='boldbase'><b>$Lang::tr{'chain'}</b></td>
278<td width='5%' align='center' class='boldbase'><b>$Lang::tr{'iface'}</b></td>
279<td width='5%' align='center' class='boldbase'><b>$Lang::tr{'proto'}</b></td>
280<td width='16%' align='center' class='boldbase'><b>$Lang::tr{'source'}</b></td>
281<td width='10%' align='center' class='boldbase'><b>$Lang::tr{'src port'}</b></td>
282<td width='16%' align='center' class='boldbase'><b>$Lang::tr{'destination'}</b></td>
283<td width='16%' align='center' class='boldbase'><b>$Lang::tr{'dst port'}</b></td>
284</tr>
76dd79d1
MT
285END
286;
287
288my @slice = splice(@log, $start, ${Header::viewsize});
289
290if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @slice = reverse @slice; }
291
292$lines = 0;
293foreach $_ (@slice)
294{
295 $a = $_;
9877d28d
ME
296 # Check whether valid ipv4 or ipv6 address
297 if (($_ =~ /SRC\=(([\d]{1,3})(\.([\d]{1,3})){3})/) or ($_ =~ /SRC\=(([0-9a-fA-F]{0,4})(\:([0-9a-fA-F]{0,4})){2,7})/)) {
76dd79d1
MT
298 if($1 eq $ip){
299 my $chain = '';
300 my $in = '-'; my $out = '-';
301 my $srcaddr = ''; my $dstaddr = '';
302 my $protostr = '';
303 my $srcport = ''; my $dstport = '';
304
9877d28d 305 # If ipv6 uses bridge, the use PHYSIN, otherwise use IN
1b5b6c91 306 if ($_ =~ /(^.* ..:..:..) [\w\-]+ kernel:(.*)(IN=.*)(PHYSIN=.*)$/) {}
9877d28d 307 elsif ($_ =~ /(^.* ..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/) {}
76dd79d1
MT
308 my $timestamp = $1; my $chain = $2; my $packet = $3;
309 $timestamp =~ /(...) (..) (..:..:..)/;
310 my $month = $1; my $day = $2; my $time = $3;
311
9877d28d 312 # If ipv6 uses bridge, the use PHYSIN and PHYSOUT, otherwise use IN and OUT
1b5b6c91
ME
313 if ($a =~ /PHYSIN=(\w+)/) { $iface = $1; } elsif ($a =~ /IN=(\w+)/) { $iface = $1; }
314 if ($a =~ /PHYSOUT=(\w+)/) { $out = $1; } elsif ($a =~ /OUT=(\w+)/) { $out = $1; }
9877d28d
ME
315 # Detect ipv4 and ipv6 addresses
316 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; }
317 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; }
76dd79d1
MT
318 if ($a =~ /PROTO\=(\w+)/) { $protostr = $1; }
319 my $protostrlc = lc($protostr);
320 if ($a =~ /SPT\=([\d\.]+)/){ $srcport = $1; }
321 if ($a =~ /DPT\=([\d\.]+)/){ $dstport = $1; }
76dd79d1
MT
322
323 if ($lines % 2) {
324 print "<tr bgcolor='${Header::table1colour}'>\n"; }
325 else {
326 print "<tr bgcolor='${Header::table2colour}'>\n"; }
327 print <<END
b740cdd7
AH
328 <td align='center'>$time</td>
329 <td align='center'>$chain</td>
330 <td align='center'>$iface</td>
331 <td align='center'>$protostr</td>
76dd79d1
MT
332 <td align='center'>
333 <table width='100%' cellpadding='0' cellspacing='0'><tr>
334 <td align='center'><a href='/cgi-bin/ipinfo.cgi?ip=$srcaddr'>$srcaddr</a></td>
335 </tr></table>
336 </td>
b740cdd7 337 <td align='center'>$srcport</td>
76dd79d1
MT
338 <td align='center'>
339 <table width='100%' cellpadding='0' cellspacing='0'><tr>
340 <td align='center'><a href='/cgi-bin/ipinfo.cgi?ip=$dstaddr'>$dstaddr</a></td>
341 </tr></table>
342 </td>
b740cdd7
AH
343 <td align='center'>$dstport</td>
344</tr>
76dd79d1
MT
345END
346 ;
347 $lines++;
348 }
349 }
350}
351
352print <<END
b740cdd7 353</table>
76dd79d1
MT
354END
355;
356
357&oldernewer();
358
359&Header::closebox();
360
361&Header::closebigbox();
362
363&Header::closepage();
364
365sub oldernewer
366{
367print <<END
368<table width='100%'>
369<tr>
370END
371;
372
373print "<td align='center' width='50%'>";
374if ($prev != -1) {
375 print "<a href='/cgi-bin/logs.cgi/showrequestfromip.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{ip}'>$Lang::tr{'older'}</a>"; }
376else {
377 print "$Lang::tr{'older'}"; }
378print "</td>\n";
379
380print "<td align='center' width='50%'>";
381if ($next != -1) {
382 print "<a href='/cgi-bin/logs.cgi/showrequestfromip.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{ip}'>$Lang::tr{'newer'}</a>"; }
383else {
384 print "$Lang::tr{'newer'}"; }
385print "</td>\n";
386
387print <<END
388</tr>
389</table>
390END
391;
392}