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