]>
git.ipfire.org Git - people/pmueller/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}/geoip-functions.pl";
19 require "${General::swroot}/lang.pl";
20 require "${General::swroot}/header.pl";
22 # Libloc database handle.
23 my $libloc_db_handle = &GeoIP
::init
();
27 #workaround to suppress a warning when a variable is used only once
28 my @dummy = ( ${Header
::table2colour
} );
33 my $errormessage = '';
35 my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
36 'Sep', 'Oct', 'Nov', 'Dec' );
37 my @longmonths = ( $Lang::tr
{'january'}, $Lang::tr
{'february'}, $Lang::tr
{'march'},
38 $Lang::tr
{'april'}, $Lang::tr
{'may'}, $Lang::tr
{'june'}, $Lang::tr
{'july'}, $Lang::tr
{'august'},
39 $Lang::tr
{'september'}, $Lang::tr
{'october'}, $Lang::tr
{'november'},
40 $Lang::tr
{'december'} );
42 my @now = localtime();
46 my $year = $now[5]+1900;
48 $cgiparams{'DAY'} = $now[3];
49 $cgiparams{'MONTH'} = $now[4];
50 $cgiparams{'ACTION'} = '';
52 &Header
::getcgihash
(\
%cgiparams);
54 $logsettings{'LOGVIEW_REVERSE'} = 'off';
55 &General
::readhash
("${General::swroot}/logging/settings", \
%logsettings);
58 if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr
{'update'})
60 my @temp = split(',',$ENV{'QUERY_STRING'});
62 $cgiparams{'MONTH'} = $temp[1];
63 $cgiparams{'DAY'} = $temp[2];
64 $cgiparams{country
} = $temp[3];
67 if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
68 !($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)$/))
70 $cgiparams{'DAY'} = $now[3];
71 $cgiparams{'MONTH'} = $now[4];
73 elsif($cgiparams{'ACTION'} eq '>>')
76 my @temp_now = localtime(time);
77 $temp_now[4] = $cgiparams{'MONTH'};
78 $temp_now[3] = $cgiparams{'DAY'};
79 @temp_then = localtime(POSIX
::mktime
(@temp_now) + 86400);
80 ## Retrieve the same time on the next day -
81 ## 86400 seconds in a day
82 $cgiparams{'MONTH'} = $temp_then[4];
83 $cgiparams{'DAY'} = $temp_then[3];
85 elsif($cgiparams{'ACTION'} eq '<<')
88 my @temp_now = localtime(time);
89 $temp_now[4] = $cgiparams{'MONTH'};
90 $temp_now[3] = $cgiparams{'DAY'};
91 @temp_then = localtime(POSIX
::mktime
(@temp_now) - 86400);
92 ## Retrieve the same time on the previous day -
93 ## 86400 seconds in a day
94 $cgiparams{'MONTH'} = $temp_then[4];
95 $cgiparams{'DAY'} = $temp_then[3];
98 if (($cgiparams{'DAY'} ne $now[3]) || ($cgiparams{'MONTH'} ne $now[4]))
101 if ( ( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
102 ( $cgiparams{'MONTH'} > $now[4] ) ) {
103 @then = localtime(POSIX
::mktime
( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 ));
105 @then = localtime(POSIX
::mktime
( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 ));
108 my $lastleap=($year-1)%4;
110 if ($lastleap == 0 && $tdoy < 60) {
121 $datediff=int(($doy-$tdoy)/7);
122 $dowd=($doy-$tdoy)%7;
123 if (($dow-$dowd)<1) {
124 $datediff=$datediff+1;
126 if (($dow-$dowd)==0) {
131 my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
132 my $longmonthstr = $longmonths[$cgiparams{'MONTH'}];
133 my $day = $cgiparams{'DAY'};
144 $filestr="/var/log/messages";
146 $filestr="/var/log/messages.$datediff";
147 $filestr = "$filestr.gz" if -f
"$filestr.gz";
150 if (!(open (FILE
,($filestr =~ /.gz$/ ?
"gzip -dc $filestr |" : $filestr)))) {
151 $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
153 # Note: This is in case the log does not exist for that date
157 my $country = $cgiparams{country
};
162 # First check whether valid log line (date, day)
163 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
164 # If ipv6 uses bridge, then use PHYSIN otherwise use IN
165 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(PHYSIN=.*)$/) {}
166 elsif (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {}
170 # If ipv6 uses bridge, use PHYSIN otherwise IN
171 if ($packet =~ /PHYSIN=(\w+)/) { $iface = $1; } elsif ($packet =~ /IN=(\w+)/) { $iface = $1; }
172 # Extract ipv4 and ipv6 addresses
173 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})/)) {
177 if($iface eq $country) {
178 # iface matches country code
182 elsif($srcaddr ne '') {
183 # or srcaddr matches country code
184 my $ccode = &GeoIP
::lookup_country_code
($libloc_db_handle, $srcaddr);
185 if($ccode eq uc($country)){
197 $datediff=$datediff-1;
199 $filestr="/var/log/messages";
201 $filestr="/var/log/messages.$datediff";
202 $filestr = "$filestr.gz" if -f
"$filestr.gz";
204 if (!(open (FILE
,($filestr =~ /.gz$/ ?
"gzip -dc $filestr |" : $filestr)))) {
205 $errormessage="$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
210 # Check if valid log line (date, day)
211 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
213 # If ipv6 uses bridge, then use PHYSIN otherwise IN
214 if ($_ =~ /PHYSIN=(\w+)/) { $iface = $1; } elsif ($_ =~ /IN=(\w+)/) { $iface = $1; }
216 if($iface eq $country) {
217 # iface matches country code
221 # extract ipv4 and ipv6 address
222 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})/)) {
224 my $ccode = $gi->country_code_by_name($srcaddr);
225 if($ccode eq uc($country)){
226 # or srcaddr matches country code
237 &Header
::showhttpheaders
();
238 &Header
::openpage
($Lang::tr
{'firewall log country'}, 1, '');
239 &Header
::openbigbox
('100%', 'left', '', $errormessage);
242 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
243 print "<font class='base'>$errormessage </font>\n";
247 &Header
::openbox
('100%', 'left', "$Lang::tr{'settings'}:");
250 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
253 <td width='10%' class='base'>$Lang::tr{'month'}: </td>
255 <select name='MONTH'>
259 for ($month = 0; $month < 12; $month++)
262 if ($month == $cgiparams{'MONTH'}) {
263 print "selected='selected' ";
265 print "value='$month'>$longmonths[$month]</option>\n";
270 <td width='10%' class='base' align='right'> $Lang::tr{'day'}: </td>
275 for ($day = 1; $day <= 31; $day++)
278 if ($day == $cgiparams{'DAY'}) {
279 print "selected='selected' "; }
280 print "value='$day'>$day</option>\n";
285 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='<<' /></td>
286 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='>>' /></td>
287 <td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
288 <tr><td width='15%'>$Lang::tr{'source ip country'}</td><td><input type='text' name='country' value='$cgiparams{country}'size='15'></td></tr>
297 &Header
::openbox
('100%', 'left', $Lang::tr
{'firewall log country'});
298 print "<p><b>$Lang::tr{'firewall hits'} $longmonthstr $daystr: $lines</b></p>";
301 $start = $lines - ${Header
::viewsize
};
303 if ($start >= $lines - ${Header
::viewsize
}) { $start = $lines - ${Header
::viewsize
}; };
304 if ($start < 0) { $start = 0; }
306 my $prev = $start - ${Header
::viewsize
};
307 my $next = $start + ${Header
::viewsize
};
309 if ($prev < 0) { $prev = 0; }
310 if ($next >= $lines) { $next = -1 }
311 if ($start == 0) { $prev = -1; }
313 if ($lines != 0) { &oldernewer
(); }
318 <td width='10%' align='center' class='boldbase'><b>$Lang::tr{'time'}</b></td>
319 <td width='13%' align='center' class='boldbase'><b>$Lang::tr{'chain'}</b></td>
320 <td width='5%' align='center' class='boldbase'><b>$Lang::tr{'iface'}</b></td>
321 <td width='5%' align='center' class='boldbase'><b>$Lang::tr{'proto'}</b></td>
322 <td width='16%' align='center' class='boldbase'><b>$Lang::tr{'source'}</b></td>
323 <td width='10%' align='center' class='boldbase'><b>$Lang::tr{'src port'}</b></td>
324 <td width='16%' align='center' class='boldbase'><b>$Lang::tr{'destination'}</b></td>
325 <td width='16%' align='center' class='boldbase'><b>$Lang::tr{'dst port'}</b></td>
330 my @slice = splice(@log, $start, ${Header
::viewsize
});
332 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @slice = reverse @slice; }
338 # If ipv6 uses bridge, use PHYSIN otherwise use IN
339 if (/^... (..) (..:..:..) [\w\-]+ kernel:(.*)(PHYSIN=.*)$/) {}
340 elsif (/^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/) {};
343 # If ipv6 uses bridge, use PHYSIN otherwise use IN
344 if ($packet =~ /PHYSIN=(\w+)/) { $iface = $1; } elsif ($packet =~ /IN=(\w+)/) { $iface = $1; }
345 if ( $1 =~ /2./ ){ $iface=""; }
347 # Extract ipv4 and ipv6 addresses
348 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})/)) {
352 if($iface eq $country || $srcaddr ne '') {
354 if($iface ne $country) {
355 $ccode = &GeoIP
::lookup_country_code
($libloc_db_handle, $srcaddr);
357 if($iface eq $country || $ccode eq uc($country)) {
359 my $in = '-'; my $out = '-';
360 my $srcaddr = ''; my $dstaddr = '';
362 my $srcport = ''; my $dstport = '';
364 # If ipv6 uses bridge, the use PHYSIN otherwise use IN
365 if ($_ =~ /(^.* ..:..:..) [\w\-]+ kernel:(.*)(IN=.*)(PHYSIN=.*)$/) {}
366 elsif ($_ =~ /(^.* ..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/) {}
367 my $timestamp = $1; my $chain = $2; my $packet = $3;
368 $timestamp =~ /(...) (..) (..:..:..)/;
369 my $month = $1; my $day = $2; my $time = $3;
371 # If ipv6 uses bridge, use PHYSIN and PHYSOUT, otherwise use IN and OUT
372 if ($a =~ /PHYSIN=(\w+)/) { $iface = $1; } elsif ($a =~ /IN=(\w+)/) { $iface = $1; }
373 if ($a =~ /PHYSOUT=(\w+)/) { $out = $1; } elsif ($a =~ /OUT=(\w+)/) { $out = $1; }
374 # Extract ipv4 and ipv6 addresses
375 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; }
376 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; }
377 if ($a =~ /PROTO\=(\w+)/) { $protostr = $1; }
378 my $protostrlc = lc($protostr);
379 if ($a =~ /SPT\=([\d\.]+)/){ $srcport = $1; }
380 if ($a =~ /DPT\=([\d\.]+)/){ $dstport = $1; }
383 print "<tr bgcolor='${Header::table1colour}'>\n";
386 print "<tr bgcolor='${Header::table2colour}'>\n";
389 <td align='center'>$time</td>
390 <td align='center'>$chain</td>
391 <td align='center'>$iface</td>
392 <td align='center'>$protostr</td>
394 <table width='100%' cellpadding='0' cellspacing='0'><tr>
395 <td align='center'><a href='/cgi-bin/ipinfo.cgi?ip=$srcaddr'>$srcaddr</a></td>
398 <td align='center'>$srcport</td>
400 <table width='100%' cellpadding='0' cellspacing='0'><tr>
401 <td align='center'><a href='/cgi-bin/ipinfo.cgi?ip=$dstaddr'>$dstaddr</a></td>
404 <td align='center'>$dstport</td>
420 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>";
424 &Header
::closebigbox
();
426 &Header
::closepage
();
436 print "<td align='center' width='50%'>";
438 print "<a href='/cgi-bin/logs.cgi/showrequestfromcountry.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{country}'>$Lang::tr{'older'}</a>";
441 print "$Lang::tr{'older'}";
445 print "<td align='center' width='50%'>";
447 print "<a href='/cgi-bin/logs.cgi/showrequestfromcountry.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{country}'>$Lang::tr{'newer'}</a>";
450 print "$Lang::tr{'newer'}";