]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/logs.cgi/firewalllogcountry.dat
geoip-functions.pl: Fix typos and formatting
[ipfire-2.x.git] / html / cgi-bin / logs.cgi / firewalllogcountry.dat
CommitLineData
e4aac473
AH
1#!/usr/bin/perl
2#
3# SmoothWall CGIs
4#
5# This code is distributed under the terms of the GPL
6#
7# JC HERITIER
8# page inspired from the initial firewalllog.dat
9#
10# Modified for IPFire by Christian Schmidt
43638be5 11# and Michael Tremer (www.ipfire.org)
e4aac473
AH
12
13use strict;
14use Geo::IP::PurePerl;
15use Getopt::Std;
16
17# enable only the following on debugging purpose
18#use warnings;
19#use CGI::Carp 'fatalsToBrowser';
20
21require '/var/ipfire/general-functions.pl';
3ea01fe0 22require "${General::swroot}/geoip-functions.pl";
e4aac473
AH
23require "${General::swroot}/lang.pl";
24require "${General::swroot}/header.pl";
25
26use POSIX();
27
28my %cgiparams=();
29my %settings=();
30my $pienumber;
31my $otherspie;
32my $showpie;
33my $sortcolumn;
34my $errormessage = '';
35
36$cgiparams{'pienumber'} = 10;
37$cgiparams{'otherspie'} = 1;
38$cgiparams{'showpie'} = 1;
39$cgiparams{'sortcolumn'} = 1;
40
41my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
43638be5 42 'Sep', 'Oct', 'Nov', 'Dec' );
e4aac473 43my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
43638be5
ME
44 $Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'},
45 $Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'},
46 $Lang::tr{'december'} );
e4aac473
AH
47
48my @now = localtime();
49my $dow = $now[6];
50my $doy = $now[7];
51my $tdoy = $now[7];
52my $year = $now[5]+1900;
53
54$cgiparams{'DAY'} = $now[3];
55$cgiparams{'MONTH'} = $now[4];
56$cgiparams{'ACTION'} = '';
57
58&General::readhash("${General::swroot}/fwlogs/ipsettings", \%settings);
59if ($settings{'pienumber'} != 0) { $cgiparams{'pienumber'} = $settings{'pienumber'} };
60if ($settings{'otherspie'} != 0) { $cgiparams{'otherspie'} = $settings{'otherspie'} };
61if ($settings{'showpie'} != 0) { $cgiparams{'showpie'} = $settings{'showpie'} };
62if ($settings{'sortcolumn'} != 0) { $cgiparams{'sortcolumn'} = $settings{'sortcolumn'} };
63
64&Header::getcgihash(\%cgiparams);
65if ($cgiparams{'pienumber'} != 0) { $settings{'pienumber'} = $cgiparams{'pienumber'} };
66if ($cgiparams{'otherspie'} != 0) { $settings{'otherspie'} = $cgiparams{'otherspie'} };
67if ($cgiparams{'showpie'} != 0) { $settings{'showpie'} = $cgiparams{'showpie'} };
68if ($cgiparams{'sortcolumn'} != 0) { $settings{'sortcolumn'} = $cgiparams{'sortcolumn'} };
69
70if ($cgiparams{'ACTION'} eq $Lang::tr{'save'})
71{
43638be5 72 &General::writehash("${General::swroot}/fwlogs/ipsettings", \%settings);
e4aac473
AH
73}
74
75my $start = -1;
76if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
77{
43638be5
ME
78 my @temp = split(',',$ENV{'QUERY_STRING'});
79 $start = $temp[0];
80 $cgiparams{'MONTH'} = $temp[1];
81 $cgiparams{'DAY'} = $temp[2];
e4aac473
AH
82}
83
84if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
85 !($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)$/))
86{
43638be5
ME
87 $cgiparams{'DAY'} = $now[3];
88 $cgiparams{'MONTH'} = $now[4];
e4aac473
AH
89}
90elsif($cgiparams{'ACTION'} eq '>>')
91{
43638be5
ME
92 my @temp_then=();
93 my @temp_now = localtime(time);
94 $temp_now[4] = $cgiparams{'MONTH'};
95 $temp_now[3] = $cgiparams{'DAY'};
96 @temp_then = localtime(POSIX::mktime(@temp_now) + 86400);
97 ## Retrieve the same time on the next day -
98 ## 86400 seconds in a day
99 $cgiparams{'MONTH'} = $temp_then[4];
100 $cgiparams{'DAY'} = $temp_then[3];
e4aac473
AH
101}
102elsif($cgiparams{'ACTION'} eq '<<')
103{
43638be5
ME
104 my @temp_then=();
105 my @temp_now = localtime(time);
106 $temp_now[4] = $cgiparams{'MONTH'};
107 $temp_now[3] = $cgiparams{'DAY'};
108 @temp_then = localtime(POSIX::mktime(@temp_now) - 86400);
109 ## Retrieve the same time on the previous day -
110 ## 86400 seconds in a day
111 $cgiparams{'MONTH'} = $temp_then[4];
112 $cgiparams{'DAY'} = $temp_then[3];
e4aac473
AH
113}
114
115if (($cgiparams{'DAY'} ne $now[3]) || ($cgiparams{'MONTH'} ne $now[4]))
116{
43638be5
ME
117 my @then = ();
118 if ( ( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
119 ( $cgiparams{'MONTH'} > $now[4] ) ) {
120 @then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 ));
121 } else {
122 @then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 ));
123 }
124 $tdoy = $then[7];
125 my $lastleap=($year-1)%4;
126 if ($tdoy>$doy) {
127 if ($lastleap == 0 && $tdoy < 60) {
128 $doy=$tdoy+366;
129 } else {
130 $doy=$doy+365;
131 }
132 }
e4aac473
AH
133}
134
135my $datediff=0;
136my $dowd=0;
137my $multifile=0;
138if ($tdoy ne $doy) {
43638be5
ME
139 $datediff=int(($doy-$tdoy)/7);
140 $dowd=($doy-$tdoy)%7;
141 if (($dow-$dowd)<1) {
142 $datediff=$datediff+1;
143 }
144 if (($dow-$dowd)==0) {
145 $multifile=1;
146 }
e4aac473
AH
147}
148
149my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
150my $longmonthstr = $longmonths[$cgiparams{'MONTH'}];
151my $day = $cgiparams{'DAY'};
152my $daystr='';
153if ($day <= 9) {
43638be5
ME
154 $daystr = " $day";
155}
e4aac473 156else {
43638be5 157 $daystr = $day;
e4aac473
AH
158}
159
160my $skip=0;
161my $filestr='';
162if ($datediff==0) {
43638be5 163 $filestr="/var/log/messages";
e4aac473 164} else {
43638be5
ME
165 $filestr="/var/log/messages.$datediff";
166 $filestr = "$filestr.gz" if -f "$filestr.gz";
e4aac473
AH
167}
168
169if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
43638be5
ME
170 $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
171 $skip=1;
172 # Note: This is in case the log does not exist for that date
e4aac473
AH
173}
174my $lines = 0;
175my @log=();
176
177if (!$skip)
178{
43638be5
ME
179 while (<FILE>)
180 {
181 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
182 $log[$lines] = $_;
183 $lines++;
184 }
185 }
186 close (FILE);
e4aac473
AH
187}
188
189$skip=0;
190if ($multifile) {
43638be5
ME
191 $datediff=$datediff-1;
192 if ($datediff==0) {
193 $filestr="/var/log/messages";
194 } else {
195 $filestr="/var/log/messages.$datediff";
196 $filestr = "$filestr.gz" if -f "$filestr.gz";
197 }
198 if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
199 $errormessage="$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
200 $skip=1;
201 }
202 if (!$skip) {
203 while (<FILE>) {
204 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
205 $log[$lines] = $_;
206 $lines++;
207 }
208 }
209 close (FILE);
210 }
e4aac473
AH
211}
212
213my $MODNAME="fwlogs";
214
215&Header::showhttpheaders();
1bea8be2 216&Header::openpage($Lang::tr{'firewall log country'}, 1, '');
e4aac473
AH
217&Header::openbigbox('100%', 'left', '', $errormessage);
218
219
220if ($errormessage) {
43638be5
ME
221 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
222 print "<font class='base'>$errormessage&nbsp;</font>\n";
223 &Header::closebox();
e4aac473
AH
224}
225
f4248975 226&Header::openbox('100%', 'left', "$Lang::tr{'settings'}");
e4aac473
AH
227
228print <<END
229<form method='post' action='$ENV{'SCRIPT_NAME'}'>
230<table width='100%'>
231<tr>
43638be5
ME
232 <td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
233 <td width='10%'>
234 <select name='MONTH'>
e4aac473
AH
235END
236;
237my $month;
238for ($month = 0; $month < 12; $month++)
239{
43638be5
ME
240 print "\t<option ";
241 if ($month == $cgiparams{'MONTH'}) {
242 print "selected='selected' "; }
243 print "value='$month'>$longmonths[$month]</option>\n";
e4aac473
AH
244}
245print <<END
43638be5
ME
246 </select>
247 </td>
248 <td width='10%' class='base' align='right'>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
249 <td width='40%'>
250 <select name='DAY'>
e4aac473
AH
251END
252;
253for ($day = 1; $day <= 31; $day++)
254{
43638be5
ME
255 print "\t<option ";
256 if ($day == $cgiparams{'DAY'}) {
257 print "selected='selected' "; }
258 print "value='$day'>$day</option>\n";
e4aac473
AH
259}
260
261if( $cgiparams{'pienumber'} != 0){$pienumber=$cgiparams{'pienumber'};}
262if( $cgiparams{'otherspie'} != 0){$otherspie=$cgiparams{'otherspie'};}
263if( $cgiparams{'showpie'} != 0){$showpie=$cgiparams{'showpie'};}
264if( $cgiparams{'sortcolumn'} != 0){$sortcolumn=$cgiparams{'sortcolumn'};}
e4aac473
AH
265print <<END
266</select>
267</td>
268<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
269<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
f4248975
AH
270<td width='20%' align='right'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
271</tr>
272<tr>
273 <td colspan='3' align='left' valign="left">$Lang::tr{'Number of Countries for the pie chart'}:</td>
274 <td colspan='3' align='left' valign="center"><input type='text' name='pienumber' value='$pienumber' size='4'></td>
275 <td align='right'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
e4aac473
AH
276</tr>
277</table>
f4248975 278</form>
e4aac473
AH
279END
280;
281
282&Header::closebox();
283
1bea8be2 284&Header::openbox('100%', 'left', $Lang::tr{'firewall graph country'});
e4aac473
AH
285print "<p><b>$Lang::tr{'firewall hits'} $longmonthstr $daystr: $lines</b></p>";
286
4fca9c99 287my $red_interface = &General::get_red_interface();
e4aac473
AH
288my $linesjc = 0;
289my %tabjc;
e4aac473
AH
290my $gi = Geo::IP::PurePerl->new();
291
292if ($pienumber == -1 || $pienumber > $lines || $sortcolumn == 2) { $pienumber = $lines; };
293$lines = 0;
4fca9c99 294
e4aac473
AH
295foreach $_ (@log)
296{
43638be5
ME
297 # If ipv6 uses bridge, use PHYSIN for iface, otherwise IN
298 if (/^... (..) (..:..:..) [\w\-]+ kernel:(.*)(PHYSIN=.*)$/) {}
299 elsif (/^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/) {}
300 my $packet = $4;
301 my $iface = '';
302 if ($packet =~ /PHYSIN=(\w+)/) { $iface = $1; } elsif ($packet =~ /IN=(\w+)/) { $iface = $1; }
303 if ( $1 =~ /2./ ) { $iface=''; }
304 my $srcaddr = '';
305 # Find ipv4 and ipv6 addresses
306 if ($packet =~ /SRC\=(([\d]{1,3})(\.([\d]{1,3})){3})/) { $srcaddr = $1; }
307 elsif ($packet =~ /SRC\=(([0-9a-fA-F]{0,4})(\:([0-9a-fA-F]{0,4})){2,7})/) { $srcaddr = $1; }
308
309 if($iface eq $red_interface) {
310 # Traffic from red
311 if($srcaddr ne '') {
312 # srcaddr is set
313 my $ccode = $gi->country_code_by_name($srcaddr);
314 if ($ccode eq '') {
315 $ccode = 'unknown';
316 }
317 $tabjc{$ccode} = $tabjc{$ccode} + 1;
318 if(($tabjc{$ccode} == 1) && ($lines < $pienumber)) { $lines = $lines + 1; }
319 $linesjc++;
320 }
321 }
322 else {
323 # Traffic not from red
324 if($iface ne '') {
325 $tabjc{$iface} = $tabjc{$iface} + 1 ;
326 if(($tabjc{$iface} == 1) && ($lines < $pienumber)) { $lines = $lines + 1; }
327 $linesjc++;
328 }
329 else {
330 # What to do with empty iface lines?
331 # This probably is traffic from ipfire itself (IN= OUT=XY)?
332 }
333 }
e4aac473
AH
334}
335
336$pienumber = $lines;
337
338my @keytabjc = keys %tabjc;
339
340my @slice;
341my $go;
342my $nblinejc;
343
344if( $cgiparams{'linejc'} eq 'all' ){ $nblinejc = $linesjc; $go=1; }
345if( ($cgiparams{'linejc'} != 0) && ($cgiparams{'linejc'} ne 'all') ){ $nblinejc = $cgiparams{'linejc'}; $go=1;}
346if( $go != 1){ $nblinejc = 1000; }
347
348my @key;
349my @value;
350my $indice=0;
351my @tabjc2;
352
43638be5
ME
353if ($sortcolumn == 1) {
354 @tabjc2 = sort { $b <=> $a } values (%tabjc);
e4aac473 355}
43638be5
ME
356else {
357 @tabjc2 = sort { $a <=> $b } keys (%tabjc);
e4aac473
AH
358}
359
360my $colour=1;
361
362##############################################
363#pie chart generation
364use GD::Graph::pie;
365use GD::Graph::colour;
366#ips sort by hits number
367my $v;
368
43638be5
ME
369if ($sortcolumn == 1) {
370 for ($v=0;$v<$pienumber;$v++){
371 findkey($tabjc2[$v]);
372 }
e4aac473 373}
43638be5
ME
374else {
375 foreach $v (@tabjc2) {
376 $key[$indice] = $v;
377 $value[$indice] = $tabjc{$v};
378 $indice++;
379 }
e4aac473
AH
380}
381
382my @ips;
383my @numb;
384
385@ips = @key;
386@numb = @value;
387
388my $o;
389
390if($cgiparams{'otherspie'} == 2 ){}
391else{
43638be5
ME
392 my $numothers;
393 for($o=0;$o<$pienumber;$o++){
394 $numothers = $numothers + $numb[$o];
395 }
396 $numothers = $linesjc - $numothers;
397 if ($numothers > 0) {
398 $ips[$pienumber]="$Lang::tr{'otherip'}";
399 $numb[$pienumber] = $numothers;
400 }
e4aac473
AH
401}
402
403my @data = (\@ips,\@numb);
404use GD::Graph::colour qw( :files );
405
406my $color=0;
407my %color = ();
408my %mainsettings = ();
409&General::readhash("${General::swroot}/main/settings", \%mainsettings);
410&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
411
412if ($showpie != 2 && $pienumber <= 50 && $pienumber != 0) {
43638be5
ME
413 my $mygraph = GD::Graph::pie->new(500, 350);
414 $mygraph->set(
415 'title' => '',
416 'pie_height' => 50,
417 'start_angle' => 89
418 ) or warn $mygraph->error;
419
420 $mygraph->set_value_font(GD::gdMediumBoldFont);
421 $mygraph->set( dclrs => [ "$color{'color1'}" , "$color{'color2'}" , "$color{'color3'}" , "$color{'color4'}" , "$color{'color5'}" , "$color{'color6'}" , "$color{'color7'}" , "$color{'color8'}" , "$color{'color9'}" , "$color{'color10'}" ] );
422 my $myimage = $mygraph->plot(\@data) or die $mygraph->error;
423
424 my @filenames = glob("/srv/web/ipfire/html/graphs/fwlog-country*.png");
425 unlink(@filenames);
426 my $imagerandom = rand(1000000);
427 my $imagename = "/srv/web/ipfire/html/graphs/fwlog-country$imagerandom.png";
428 open(FILE,">$imagename");
429 print FILE $myimage->png;
430 close(FILE);
431 #####################################################
432 print "<div style='text-align:center;'>";
433 print "<img src='/graphs/fwlog-country$imagerandom.png'>";
434 print "</div>";
e4aac473 435}
e4aac473 436print <<END
f4248975
AH
437<table width='100%' class='tbl'>
438<tr>
439<th width='10%' align='center' class='boldbase'></th>
440<th width='30%' align='center' class='boldbase'><b>$Lang::tr{'country'}</b></th>
441<th width='30%' align='center' class='boldbase'><b>Count</b></th>
442<th width='30%' align='center' class='boldbase'><b>$Lang::tr{'percentage'}</b></th>
443</tr>
e4aac473
AH
444END
445;
446
447my $total=0;
448my $show=0;
449
450my $s;
451my $percent;
f4248975 452my $col="";
e4aac473
AH
453
454for($s=0;$s<$lines;$s++)
455{
43638be5
ME
456 $show++;
457 $percent = $value[$s] * 100 / $linesjc;
458 $percent = sprintf("%.f", $percent);
459 $total = $total + $value[$s];
460 # colors are numbered 1 to 10
461 my $colorIndex = ($color % 10) + 1;
462 $col="bgcolor='$color{\"color$colorIndex\"}'";
463 $color++;
464 print "<tr>";
465
466 print "<td align='center' $col>";
467
468 # Dont show details button for "unknown" location.
469 if ($key[$s] ne 'unknown') {
470 print"<form method='post' action='showrequestfromcountry.dat'>";
471 print"<input type='hidden' name='MONTH' value='$cgiparams{'MONTH'}'>";
472 print"<input type='hidden' name='DAY' value='$cgiparams{'DAY'}'>";
473 print"<input type='hidden' name='country' value='$key[$s]'>";
1bea8be2 474 print"<input type='submit' value='$Lang::tr{'details'}'></form>";
43638be5
ME
475 }
476 elsif ($key[$s] eq 'unknown') {
477 print "unknown";
478 }
479 # Looks dangerous to use hardcoded interface names here. Probably needs fixing.
480 if ($key[$s] eq 'blue0' || $key[$s] eq 'green0' || $key[$s] eq 'orange0' ) {
3ea01fe0 481 print "<td align='center' $col>$key[$s]</td>";
43638be5
ME
482 }
483 else {
484 my $fcode = lc($key[$s]);
485
486 # Get flag icon for of the country.
487 my $flag_icon = &GeoIP::get_flag_icon($fcode);
488
489 if($flag_icon) {
490 print "<td align='center' $col><a href='/cgi-bin/country.cgi#$fcode'><img src='$flag_icon' border='0' align='absmiddle' alt='$key[$s]' title='$key[$s]'></a></td>";
491 } else {
492 print "<td align='center' $col>$key[$s]</td>";
493 }
494 }
495 print "<td align='center' $col>$value[$s]</td>";
496 print "<td align='center' $col>$percent</td>";
497 print "</tr>";
f4248975 498}
e4aac473
AH
499
500if($cgiparams{'otherspie'} == 2 ){}
501else{
43638be5
ME
502 # colors are numbered 1 to 10
503 my $colorIndex = ($color % 10) + 1;
504 $col="bgcolor='$color{\"color$colorIndex\"}'";
505 print "<tr>";
506
507 if ( $linesjc ne "0")
508 {
509 my $dif;
510 $dif = $linesjc - $total;
511 $percent = $dif * 100 / $linesjc;
512 $percent = sprintf("%.f", $percent);
513 print <<END
514 <td align='center' $col></TD>
515 <td align='center' $col>$Lang::tr{'other countries'}</td>
516 <td align='center' $col>$dif</TD>
517 <td align='center' $col>$percent</TD>
518 </tr>
e4aac473 519END
43638be5
ME
520 ;
521 }
e4aac473
AH
522}
523print <<END
524</TABLE>
525END
526;
527
528&Header::closebox();
529&Header::closebigbox();
530&Header::closepage();
531
532sub findkey {
43638be5
ME
533 my $v;
534 foreach $v (@keytabjc) {
535 if ($tabjc{$v} eq $_[0]) {
536 delete $tabjc{$v};
537 $key[$indice] = "$v";
538 $value[$indice] = $_[0];
539 $indice++;
540 last;
541 }
542 }
e4aac473
AH
543}
544sub checkversion {
43638be5
ME
545 #Automatic Updates is disabled
546 return "0","0";
e4aac473
AH
547}
548