]>
git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/logs.cgi/showrequestfromcountry.dat
4 # This code is distributed under the terms of the GPL
7 # page inspired from the initial firewalllog.dat
9 # Modified for IPFire by Christian Schmidt (www.ipfire.org)
11 # enable only the following on debugging purpose
13 #use CGI::Carp 'fatalsToBrowser';
17 require '/var/ipfire/general-functions.pl';
18 require "${General::swroot}/location-functions.pl";
19 require "${General::swroot}/lang.pl";
20 require "${General::swroot}/header.pl";
24 #workaround to suppress a warning when a variable is used only once
25 my @dummy = ( ${Header
::table2colour
} );
30 my $errormessage = '';
32 my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
33 'Sep', 'Oct', 'Nov', 'Dec' );
34 my @longmonths = ( $Lang::tr
{'january'}, $Lang::tr
{'february'}, $Lang::tr
{'march'},
35 $Lang::tr
{'april'}, $Lang::tr
{'may'}, $Lang::tr
{'june'}, $Lang::tr
{'july'}, $Lang::tr
{'august'},
36 $Lang::tr
{'september'}, $Lang::tr
{'october'}, $Lang::tr
{'november'},
37 $Lang::tr
{'december'} );
39 my @now = localtime();
43 my $year = $now[5]+1900;
45 $cgiparams{'DAY'} = $now[3];
46 $cgiparams{'MONTH'} = $now[4];
47 $cgiparams{'ACTION'} = '';
49 &Header
::getcgihash
(\
%cgiparams);
51 $logsettings{'LOGVIEW_REVERSE'} = 'off';
52 &General
::readhash
("${General::swroot}/logging/settings", \
%logsettings);
55 if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr
{'update'})
57 my @temp = split(',',$ENV{'QUERY_STRING'});
59 $cgiparams{'MONTH'} = $temp[1];
60 $cgiparams{'DAY'} = $temp[2];
61 $cgiparams{country
} = $temp[3];
64 if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
65 !($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)$/))
67 $cgiparams{'DAY'} = $now[3];
68 $cgiparams{'MONTH'} = $now[4];
70 elsif($cgiparams{'ACTION'} eq '>>')
73 my @temp_now = localtime(time);
74 $temp_now[4] = $cgiparams{'MONTH'};
75 $temp_now[3] = $cgiparams{'DAY'};
76 @temp_then = localtime(POSIX
::mktime
(@temp_now) + 86400);
77 ## Retrieve the same time on the next day -
78 ## 86400 seconds in a day
79 $cgiparams{'MONTH'} = $temp_then[4];
80 $cgiparams{'DAY'} = $temp_then[3];
82 elsif($cgiparams{'ACTION'} eq '<<')
85 my @temp_now = localtime(time);
86 $temp_now[4] = $cgiparams{'MONTH'};
87 $temp_now[3] = $cgiparams{'DAY'};
88 @temp_then = localtime(POSIX
::mktime
(@temp_now) - 86400);
89 ## Retrieve the same time on the previous day -
90 ## 86400 seconds in a day
91 $cgiparams{'MONTH'} = $temp_then[4];
92 $cgiparams{'DAY'} = $temp_then[3];
95 if (($cgiparams{'DAY'} ne $now[3]) || ($cgiparams{'MONTH'} ne $now[4]))
98 if ( ( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
99 ( $cgiparams{'MONTH'} > $now[4] ) ) {
100 @then = localtime(POSIX
::mktime
( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 ));
102 @then = localtime(POSIX
::mktime
( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 ));
105 my $lastleap=($year-1)%4;
107 if ($lastleap == 0 && $tdoy < 60) {
118 $datediff=int(($doy-$tdoy)/7);
119 $dowd=($doy-$tdoy)%7;
120 if (($dow-$dowd)<1) {
121 $datediff=$datediff+1;
123 if (($dow-$dowd)==0) {
128 my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
129 my $longmonthstr = $longmonths[$cgiparams{'MONTH'}];
130 my $day = $cgiparams{'DAY'};
141 $filestr="/var/log/messages";
143 $filestr="/var/log/messages.$datediff";
144 $filestr = "$filestr.gz" if -f
"$filestr.gz";
147 if (!(open (FILE
,($filestr =~ /.gz$/ ?
"gzip -dc $filestr |" : $filestr)))) {
148 $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
150 # Note: This is in case the log does not exist for that date
154 my $country = $cgiparams{country
};
159 # First check whether valid log line (date, day)
160 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
161 # If ipv6 uses bridge, then use PHYSIN otherwise use IN
162 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(PHYSIN=.*)$/) {}
163 elsif (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {}
167 # If ipv6 uses bridge, use PHYSIN otherwise IN
168 if ($packet =~ /PHYSIN=(\w+)/) { $iface = $1; } elsif ($packet =~ /IN=(\w+)/) { $iface = $1; }
169 # Extract ipv4 and ipv6 addresses
170 if (($packet =~ /SRC\=(([\d]{1,3})(\.([\d]{1,3})){3})/) or ($packet =~ /SRC\=(([0-9a-fA-F]{0,4})(\:([0-9a-fA-F]{0,4})){2,7})/)) {
174 if($iface eq $country) {
175 # iface matches country code
179 elsif($srcaddr ne '') {
180 # or srcaddr matches country code
181 my $ccode = &Location
::Functions
::lookup_country_code
($srcaddr);
182 if($ccode eq uc($country)){
194 $datediff=$datediff-1;
196 $filestr="/var/log/messages";
198 $filestr="/var/log/messages.$datediff";
199 $filestr = "$filestr.gz" if -f
"$filestr.gz";
201 if (!(open (FILE
,($filestr =~ /.gz$/ ?
"gzip -dc $filestr |" : $filestr)))) {
202 $errormessage="$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
207 # Check if valid log line (date, day)
208 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
210 # If ipv6 uses bridge, then use PHYSIN otherwise IN
211 if ($_ =~ /PHYSIN=(\w+)/) { $iface = $1; } elsif ($_ =~ /IN=(\w+)/) { $iface = $1; }
213 if($iface eq $country) {
214 # iface matches country code
218 # extract ipv4 and ipv6 address
219 elsif (($_ =~ /SRC\=(([\d]{1,3})(\.([\d]{1,3})){3})/) or ($_ =~ /SRC\=(([0-9a-fA-F]{0,4})(\:([0-9a-fA-F]{0,4})){2,7})/)) {
221 my $ccode = &Location
::Functions
::lookup_country_code
($srcaddr);
222 if($ccode eq uc($country)){
223 # or srcaddr matches country code
234 &Header
::showhttpheaders
();
235 &Header
::openpage
($Lang::tr
{'firewall log country'}, 1, '');
236 &Header
::openbigbox
('100%', 'left', '', $errormessage);
239 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
240 print "<font class='base'>$errormessage </font>\n";
244 &Header
::openbox
('100%', 'left', "$Lang::tr{'settings'}:");
247 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
250 <td width='10%' class='base'>$Lang::tr{'month'}: </td>
252 <select name='MONTH'>
256 for ($month = 0; $month < 12; $month++)
259 if ($month == $cgiparams{'MONTH'}) {
260 print "selected='selected' ";
262 print "value='$month'>$longmonths[$month]</option>\n";
267 <td width='10%' class='base' align='right'> $Lang::tr{'day'}: </td>
272 for ($day = 1; $day <= 31; $day++)
275 if ($day == $cgiparams{'DAY'}) {
276 print "selected='selected' "; }
277 print "value='$day'>$day</option>\n";
282 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='<<' /></td>
283 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='>>' /></td>
284 <td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
285 <tr><td width='15%'>$Lang::tr{'source ip country'}</td><td><input type='text' name='country' value='$cgiparams{country}'size='15'></td></tr>
294 &Header
::openbox
('100%', 'left', $Lang::tr
{'firewall log country'});
295 print "<p><b>$Lang::tr{'firewall hits'} $longmonthstr $daystr: $lines</b></p>";
298 $start = $lines - ${Header
::viewsize
};
300 if ($start >= $lines - ${Header
::viewsize
}) { $start = $lines - ${Header
::viewsize
}; };
301 if ($start < 0) { $start = 0; }
303 my $prev = $start - ${Header
::viewsize
};
304 my $next = $start + ${Header
::viewsize
};
306 if ($prev < 0) { $prev = 0; }
307 if ($next >= $lines) { $next = -1 }
308 if ($start == 0) { $prev = -1; }
310 if ($lines != 0) { &oldernewer
(); }
315 <td width='10%' align='center' class='boldbase'><b>$Lang::tr{'time'}</b></td>
316 <td width='13%' align='center' class='boldbase'><b>$Lang::tr{'chain'}</b></td>
317 <td width='5%' align='center' class='boldbase'><b>$Lang::tr{'iface'}</b></td>
318 <td width='5%' align='center' class='boldbase'><b>$Lang::tr{'proto'}</b></td>
319 <td width='16%' align='center' class='boldbase'><b>$Lang::tr{'source'}</b></td>
320 <td width='10%' align='center' class='boldbase'><b>$Lang::tr{'src port'}</b></td>
321 <td width='16%' align='center' class='boldbase'><b>$Lang::tr{'destination'}</b></td>
322 <td width='16%' align='center' class='boldbase'><b>$Lang::tr{'dst port'}</b></td>
327 my @slice = splice(@log, $start, ${Header
::viewsize
});
329 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @slice = reverse @slice; }
335 # If ipv6 uses bridge, use PHYSIN otherwise use IN
336 if (/^... (..) (..:..:..) [\w\-]+ kernel:(.*)(PHYSIN=.*)$/) {}
337 elsif (/^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/) {};
340 # If ipv6 uses bridge, use PHYSIN otherwise use IN
341 if ($packet =~ /PHYSIN=(\w+)/) { $iface = $1; } elsif ($packet =~ /IN=(\w+)/) { $iface = $1; }
342 if ( $1 =~ /2./ ){ $iface=""; }
344 # Extract ipv4 and ipv6 addresses
345 if (($packet =~ /SRC\=(([\d]{1,3})(\.([\d]{1,3})){3})/) or ($packet =~ /SRC\=(([0-9a-fA-F]{0,4})(\:([0-9a-fA-F]{0,4})){2,7})/)) {
349 if($iface eq $country || $srcaddr ne '') {
351 if($iface ne $country) {
352 $ccode = &Location
::Functions
::lookup_country_code
($srcaddr);
354 if($iface eq $country || $ccode eq uc($country)) {
356 my $in = '-'; my $out = '-';
357 my $srcaddr = ''; my $dstaddr = '';
359 my $srcport = ''; my $dstport = '';
361 # If ipv6 uses bridge, the use PHYSIN otherwise use IN
362 if ($_ =~ /(^.* ..:..:..) [\w\-]+ kernel:(.*)(IN=.*)(PHYSIN=.*)$/) {}
363 elsif ($_ =~ /(^.* ..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/) {}
364 my $timestamp = $1; my $chain = $2; my $packet = $3;
365 $timestamp =~ /(...) (..) (..:..:..)/;
366 my $month = $1; my $day = $2; my $time = $3;
368 # If ipv6 uses bridge, use PHYSIN and PHYSOUT, otherwise use IN and OUT
369 if ($a =~ /PHYSIN=(\w+)/) { $iface = $1; } elsif ($a =~ /IN=(\w+)/) { $iface = $1; }
370 if ($a =~ /PHYSOUT=(\w+)/) { $out = $1; } elsif ($a =~ /OUT=(\w+)/) { $out = $1; }
371 # Extract ipv4 and ipv6 addresses
372 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; }
373 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; }
374 if ($a =~ /PROTO\=(\w+)/) { $protostr = $1; }
375 my $protostrlc = lc($protostr);
376 if ($a =~ /SPT\=([\d\.]+)/){ $srcport = $1; }
377 if ($a =~ /DPT\=([\d\.]+)/){ $dstport = $1; }
380 print "<tr bgcolor='${Header::table1colour}'>\n";
383 print "<tr bgcolor='${Header::table2colour}'>\n";
386 <td align='center'>$time</td>
387 <td align='center'>$chain</td>
388 <td align='center'>$iface</td>
389 <td align='center'>$protostr</td>
391 <table width='100%' cellpadding='0' cellspacing='0'><tr>
392 <td align='center'><a href='/cgi-bin/ipinfo.cgi?ip=$srcaddr'>$srcaddr</a></td>
395 <td align='center'>$srcport</td>
397 <table width='100%' cellpadding='0' cellspacing='0'><tr>
398 <td align='center'><a href='/cgi-bin/ipinfo.cgi?ip=$dstaddr'>$dstaddr</a></td>
401 <td align='center'>$dstport</td>
417 print"<table width='100%'><tr><td align='center'><a href='/cgi-bin/logs.cgi/firewalllogcountry.dat'><img src='/images/back.png' alt='$Lang::tr{'back'}' title='$Lang::tr{'back'}' /></a></td></tr></table>";
421 &Header
::closebigbox
();
423 &Header
::closepage
();
433 print "<td align='center' width='50%'>";
435 print "<a href='/cgi-bin/logs.cgi/showrequestfromcountry.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{country}'>$Lang::tr{'older'}</a>";
438 print "$Lang::tr{'older'}";
442 print "<td align='center' width='50%'>";
444 print "<a href='/cgi-bin/logs.cgi/showrequestfromcountry.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{country}'>$Lang::tr{'newer'}</a>";
447 print "$Lang::tr{'newer'}";