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