]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/logs.cgi/showrequestfromcountry.dat
Merge branch 'switch-to-libloc' into next-switch-to-libloc
[people/pmueller/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
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";
21
22 # Libloc database handle.
23 my $libloc_db_handle = &GeoIP::init();
24
25 use POSIX();
26
27 #workaround to suppress a warning when a variable is used only once
28 my @dummy = ( ${Header::table2colour} );
29 undef (@dummy);
30
31 my %cgiparams=();
32 my %logsettings=();
33 my $errormessage = '';
34
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'} );
41
42 my @now = localtime();
43 my $dow = $now[6];
44 my $doy = $now[7];
45 my $tdoy = $now[7];
46 my $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
57 my $start = -1;
58 if ($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];
64 $cgiparams{country} = $temp[3];
65 }
66
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)$/))
69 {
70 $cgiparams{'DAY'} = $now[3];
71 $cgiparams{'MONTH'} = $now[4];
72 }
73 elsif($cgiparams{'ACTION'} eq '>>')
74 {
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];
84 }
85 elsif($cgiparams{'ACTION'} eq '<<')
86 {
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];
96 }
97
98 if (($cgiparams{'DAY'} ne $now[3]) || ($cgiparams{'MONTH'} ne $now[4]))
99 {
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 }
116 }
117 my $datediff=0;
118 my $dowd=0;
119 my $multifile=0;
120 if ($tdoy ne $doy) {
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 }
129 }
130
131 my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
132 my $longmonthstr = $longmonths[$cgiparams{'MONTH'}];
133 my $day = $cgiparams{'DAY'};
134 my $daystr='';
135 if ($day <= 9) {
136 $daystr = " $day"; }
137 else {
138 $daystr = $day;
139 }
140
141 my $skip=0;
142 my $filestr='';
143 if ($datediff==0) {
144 $filestr="/var/log/messages";
145 } else {
146 $filestr="/var/log/messages.$datediff";
147 $filestr = "$filestr.gz" if -f "$filestr.gz";
148 }
149
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'}";
152 $skip=1;
153 # Note: This is in case the log does not exist for that date
154 }
155 my $lines = 0;
156 my @log=();
157 my $country = $cgiparams{country};
158
159 if (!$skip)
160 {
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
184 my $ccode = &GeoIP::lookup_country_code($libloc_db_handle, $srcaddr);
185 if($ccode eq uc($country)){
186 $log[$lines] = $_;
187 $lines++;
188 }
189 }
190 }
191 }
192 close (FILE);
193 }
194
195 $skip=0;
196 if ($multifile) {
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) {
209 while (<FILE>) {
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 }
232 }
233 close (FILE);
234 }
235 }
236
237 &Header::showhttpheaders();
238 &Header::openpage($Lang::tr{'firewall log country'}, 1, '');
239 &Header::openbigbox('100%', 'left', '', $errormessage);
240
241 if ($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
249 print <<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'>
256 END
257 ;
258 my $month;
259 for ($month = 0; $month < 12; $month++)
260 {
261 print "\t<option ";
262 if ($month == $cgiparams{'MONTH'}) {
263 print "selected='selected' ";
264 }
265 print "value='$month'>$longmonths[$month]</option>\n";
266 }
267 print <<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'>
273 END
274 ;
275 for ($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 }
282 print <<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>
288 <tr><td width='15%'>$Lang::tr{'source ip country'}</td><td><input type='text' name='country' value='$cgiparams{country}'size='15'></td></tr>
289 </tr>
290 </table>
291 </form>
292 END
293 ;
294
295 &Header::closebox();
296
297 &Header::openbox('100%', 'left', $Lang::tr{'firewall log country'});
298 print "<p><b>$Lang::tr{'firewall hits'} $longmonthstr $daystr: $lines</b></p>";
299
300 if ($start == -1) {
301 $start = $lines - ${Header::viewsize};
302 }
303 if ($start >= $lines - ${Header::viewsize}) { $start = $lines - ${Header::viewsize}; };
304 if ($start < 0) { $start = 0; }
305
306 my $prev = $start - ${Header::viewsize};
307 my $next = $start + ${Header::viewsize};
308
309 if ($prev < 0) { $prev = 0; }
310 if ($next >= $lines) { $next = -1 }
311 if ($start == 0) { $prev = -1; }
312
313 if ($lines != 0) { &oldernewer(); }
314
315 print <<END
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>
327 END
328 ;
329
330 my @slice = splice(@log, $start, ${Header::viewsize});
331
332 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @slice = reverse @slice; }
333
334 $lines = 0;
335 foreach $_ (@slice)
336 {
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) {
355 $ccode = &GeoIP::lookup_country_code($libloc_db_handle, $srcaddr);
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>
406 END
407 ;
408 $lines++;
409 }
410 }
411 }
412
413 print <<END
414 </table>
415 END
416 ;
417
418 &oldernewer();
419
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
422 &Header::closebox();
423
424 &Header::closebigbox();
425
426 &Header::closepage();
427
428 sub oldernewer
429 {
430 print <<END
431 <table width='100%'>
432 <tr>
433 END
434 ;
435
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";
444
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 }
452
453 print "</td>\n";
454 print <<END
455 </tr>
456 </table>
457 END
458 ;
459 }
460