]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/logs.cgi/showrequestfromcountry.dat
logs.cgi/showrequestfromcountry.dat: Use location-functions.pl
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / logs.cgi / showrequestfromcountry.dat
CommitLineData
e4aac473
AH
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
15#use strict;
e4aac473
AH
16
17require '/var/ipfire/general-functions.pl';
4f6d5b3e 18require "${General::swroot}/location-functions.pl";
e4aac473
AH
19require "${General::swroot}/lang.pl";
20require "${General::swroot}/header.pl";
21
50494dfd 22# Libloc database handle.
4f6d5b3e 23my $libloc_db_handle = &Location::Functions::init();
50494dfd 24
e4aac473
AH
25use POSIX();
26
27#workaround to suppress a warning when a variable is used only once
28my @dummy = ( ${Header::table2colour} );
29undef (@dummy);
30
31my %cgiparams=();
32my %logsettings=();
33my $errormessage = '';
34
35my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
36 'Sep', 'Oct', 'Nov', 'Dec' );
37my @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'} );
41
42my @now = localtime();
43my $dow = $now[6];
44my $doy = $now[7];
45my $tdoy = $now[7];
46my $year = $now[5]+1900;
47
48$cgiparams{'DAY'} = $now[3];
49$cgiparams{'MONTH'} = $now[4];
50$cgiparams{'ACTION'} = '';
51
52&Header::getcgihash(\%cgiparams);
53
54$logsettings{'LOGVIEW_REVERSE'} = 'off';
55&General::readhash("${General::swroot}/logging/settings", \%logsettings);
56
57my $start = -1;
58if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
59{
60 my @temp = split(',',$ENV{'QUERY_STRING'});
61 $start = $temp[0];
62 $cgiparams{'MONTH'} = $temp[1];
63 $cgiparams{'DAY'} = $temp[2];
bde7a7d2 64 $cgiparams{country} = $temp[3];
e4aac473
AH
65}
66
67if (!($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)$/))
69{
70 $cgiparams{'DAY'} = $now[3];
71 $cgiparams{'MONTH'} = $now[4];
72}
73elsif($cgiparams{'ACTION'} eq '>>')
74{
43638be5
ME
75 my @temp_then=();
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];
e4aac473
AH
84}
85elsif($cgiparams{'ACTION'} eq '<<')
86{
43638be5
ME
87 my @temp_then=();
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];
e4aac473
AH
96}
97
98if (($cgiparams{'DAY'} ne $now[3]) || ($cgiparams{'MONTH'} ne $now[4]))
99{
43638be5
ME
100 my @then = ();
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 ));
104 } else {
105 @then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 ));
106 }
107 $tdoy = $then[7];
108 my $lastleap=($year-1)%4;
109 if ($tdoy>$doy) {
110 if ($lastleap == 0 && $tdoy < 60) {
111 $doy=$tdoy+366;
112 } else {
113 $doy=$doy+365;
114 }
115 }
e4aac473
AH
116}
117my $datediff=0;
118my $dowd=0;
119my $multifile=0;
120if ($tdoy ne $doy) {
43638be5
ME
121 $datediff=int(($doy-$tdoy)/7);
122 $dowd=($doy-$tdoy)%7;
123 if (($dow-$dowd)<1) {
124 $datediff=$datediff+1;
125 }
126 if (($dow-$dowd)==0) {
127 $multifile=1;
128 }
e4aac473
AH
129}
130
131my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
132my $longmonthstr = $longmonths[$cgiparams{'MONTH'}];
133my $day = $cgiparams{'DAY'};
134my $daystr='';
135if ($day <= 9) {
136 $daystr = " $day"; }
137else {
138 $daystr = $day;
139}
140
141my $skip=0;
142my $filestr='';
143if ($datediff==0) {
43638be5 144 $filestr="/var/log/messages";
e4aac473 145} else {
43638be5
ME
146 $filestr="/var/log/messages.$datediff";
147 $filestr = "$filestr.gz" if -f "$filestr.gz";
e4aac473
AH
148}
149
150if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
43638be5
ME
151 $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
152 $skip=1;
153 # Note: This is in case the log does not exist for that date
e4aac473
AH
154}
155my $lines = 0;
156my @log=();
157my $country = $cgiparams{country};
e4aac473
AH
158
159if (!$skip)
160{
43638be5
ME
161 while (<FILE>) {
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=.*)$/) {}
167 my $packet = $2;
168 my $iface = '';
169 my $srcaddr = '';
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})/)) {
174 $srcaddr = $1;
175 }
176
177 if($iface eq $country) {
178 # iface matches country code
179 $log[$lines] = $_;
180 $lines++;
181 }
182 elsif($srcaddr ne '') {
183 # or srcaddr matches country code
4f6d5b3e 184 my $ccode = &Location::Functions::lookup_country_code($libloc_db_handle, $srcaddr);
43638be5
ME
185 if($ccode eq uc($country)){
186 $log[$lines] = $_;
187 $lines++;
188 }
189 }
190 }
e4aac473
AH
191 }
192 close (FILE);
193}
194
195$skip=0;
196if ($multifile) {
43638be5
ME
197 $datediff=$datediff-1;
198 if ($datediff==0) {
199 $filestr="/var/log/messages";
200 } else {
201 $filestr="/var/log/messages.$datediff";
202 $filestr = "$filestr.gz" if -f "$filestr.gz";
203 }
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'}";
206 $skip=1;
207 }
208 if (!$skip) {
e4aac473 209 while (<FILE>) {
43638be5
ME
210 # Check if valid log line (date, day)
211 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
212 my $iface = '';
213 # If ipv6 uses bridge, then use PHYSIN otherwise IN
214 if ($_ =~ /PHYSIN=(\w+)/) { $iface = $1; } elsif ($_ =~ /IN=(\w+)/) { $iface = $1; }
215
216 if($iface eq $country) {
217 # iface matches country code
218 $log[$lines] = $_;
219 $lines++;
220 }
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})/)) {
223 my $srcaddr=$1;
224 my $ccode = $gi->country_code_by_name($srcaddr);
225 if($ccode eq uc($country)){
226 # or srcaddr matches country code
227 $log[$lines] = $_;
228 $lines++;
229 }
230 }
231 }
e4aac473
AH
232 }
233 close (FILE);
234 }
235}
236
237&Header::showhttpheaders();
415cbcec 238&Header::openpage($Lang::tr{'firewall log country'}, 1, '');
e4aac473
AH
239&Header::openbigbox('100%', 'left', '', $errormessage);
240
241if ($errormessage) {
242 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
243 print "<font class='base'>$errormessage&nbsp;</font>\n";
244 &Header::closebox();
245}
246
247&Header::openbox('100%', 'left', "$Lang::tr{'settings'}:");
248
249print <<END
250<form method='post' action='$ENV{'SCRIPT_NAME'}'>
251<table width='100%'>
252<tr>
253 <td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
254 <td width='10%'>
255 <select name='MONTH'>
256END
257;
258my $month;
259for ($month = 0; $month < 12; $month++)
260{
261 print "\t<option ";
262 if ($month == $cgiparams{'MONTH'}) {
43638be5
ME
263 print "selected='selected' ";
264 }
e4aac473
AH
265 print "value='$month'>$longmonths[$month]</option>\n";
266}
267print <<END
268 </select>
269 </td>
270 <td width='10%' class='base' align='right'>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
271 <td width='40%'>
272 <select name='DAY'>
273END
274;
275for ($day = 1; $day <= 31; $day++)
276{
277 print "\t<option ";
278 if ($day == $cgiparams{'DAY'}) {
279 print "selected='selected' "; }
280 print "value='$day'>$day</option>\n";
281}
282print <<END
283</select>
284</td>
285<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
286<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
287<td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
bde7a7d2 288<tr><td width='15%'>$Lang::tr{'source ip country'}</td><td><input type='text' name='country' value='$cgiparams{country}'size='15'></td></tr>
e4aac473
AH
289</tr>
290</table>
291</form>
292END
293;
294
295&Header::closebox();
296
415cbcec 297&Header::openbox('100%', 'left', $Lang::tr{'firewall log country'});
e4aac473
AH
298print "<p><b>$Lang::tr{'firewall hits'} $longmonthstr $daystr: $lines</b></p>";
299
300if ($start == -1) {
43638be5
ME
301 $start = $lines - ${Header::viewsize};
302}
e4aac473
AH
303if ($start >= $lines - ${Header::viewsize}) { $start = $lines - ${Header::viewsize}; };
304if ($start < 0) { $start = 0; }
305
306my $prev = $start - ${Header::viewsize};
307my $next = $start + ${Header::viewsize};
308
309if ($prev < 0) { $prev = 0; }
310if ($next >= $lines) { $next = -1 }
311if ($start == 0) { $prev = -1; }
312
313if ($lines != 0) { &oldernewer(); }
314
315print <<END
bde7a7d2
AH
316<table width='100%'>
317<tr>
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>
326</tr>
e4aac473
AH
327END
328;
329
330my @slice = splice(@log, $start, ${Header::viewsize});
331
332if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @slice = reverse @slice; }
333
334$lines = 0;
335foreach $_ (@slice)
336{
43638be5
ME
337 $a = $_;
338 # If ipv6 uses bridge, use PHYSIN otherwise use IN
339 if (/^... (..) (..:..:..) [\w\-]+ kernel:(.*)(PHYSIN=.*)$/) {}
340 elsif (/^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/) {};
341 my $packet = $4;
342 my $iface = '';
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=""; }
346 my $srcaddr = '';
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})/)) {
349 $srcaddr = $1;
350 };
351
352 if($iface eq $country || $srcaddr ne '') {
353 my $ccode='';
354 if($iface ne $country) {
4f6d5b3e 355 $ccode = &Location::Functions::lookup_country_code($libloc_db_handle, $srcaddr);
43638be5
ME
356 }
357 if($iface eq $country || $ccode eq uc($country)) {
358 my $chain = '';
359 my $in = '-'; my $out = '-';
360 my $srcaddr = ''; my $dstaddr = '';
361 my $protostr = '';
362 my $srcport = ''; my $dstport = '';
363
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;
370
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; }
381
382 if ($lines % 2) {
383 print "<tr bgcolor='${Header::table1colour}'>\n";
384 }
385 else {
386 print "<tr bgcolor='${Header::table2colour}'>\n";
387 }
388 print <<END
389 <td align='center'>$time</td>
390 <td align='center'>$chain</td>
391 <td align='center'>$iface</td>
392 <td align='center'>$protostr</td>
393 <td align='center'>
394 <table width='100%' cellpadding='0' cellspacing='0'><tr>
395 <td align='center'><a href='/cgi-bin/ipinfo.cgi?ip=$srcaddr'>$srcaddr</a></td>
396 </tr></table>
397 </td>
398 <td align='center'>$srcport</td>
399 <td align='center'>
400 <table width='100%' cellpadding='0' cellspacing='0'><tr>
401 <td align='center'><a href='/cgi-bin/ipinfo.cgi?ip=$dstaddr'>$dstaddr</a></td>
402 </tr></table>
403 </td>
404 <td align='center'>$dstport</td>
405 </tr>
e4aac473 406END
43638be5
ME
407 ;
408 $lines++;
409 }
410 }
e4aac473
AH
411}
412
413print <<END
bde7a7d2 414</table>
e4aac473
AH
415END
416;
417
418&oldernewer();
419
415cbcec
MF
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>";
421
e4aac473
AH
422&Header::closebox();
423
424&Header::closebigbox();
425
426&Header::closepage();
427
428sub oldernewer
429{
43638be5
ME
430 print <<END
431 <table width='100%'>
432 <tr>
e4aac473 433END
43638be5 434 ;
e4aac473 435
43638be5
ME
436 print "<td align='center' width='50%'>";
437 if ($prev != -1) {
438 print "<a href='/cgi-bin/logs.cgi/showrequestfromcountry.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{country}'>$Lang::tr{'older'}</a>";
439 }
440 else {
441 print "$Lang::tr{'older'}";
442 }
443 print "</td>\n";
e4aac473 444
43638be5
ME
445 print "<td align='center' width='50%'>";
446 if ($next != -1) {
447 print "<a href='/cgi-bin/logs.cgi/showrequestfromcountry.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{country}'>$Lang::tr{'newer'}</a>";
448 }
449 else {
450 print "$Lang::tr{'newer'}";
451 }
e4aac473 452
43638be5
ME
453 print "</td>\n";
454 print <<END
455 </tr>
456 </table>
e4aac473 457END
43638be5 458 ;
e4aac473 459}
bde7a7d2 460