]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/logs.cgi/showrequestfromip.dat
Merge branch 'core110'
[ipfire-2.x.git] / html / cgi-bin / logs.cgi / showrequestfromip.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 require '/var/ipfire/general-functions.pl';
16 require "${General::swroot}/lang.pl";
17 require "${General::swroot}/header.pl";
18
19 use POSIX();
20
21 #workaround to suppress a warning when a variable is used only once
22 my @dummy = ( ${Header::table2colour} );
23 undef (@dummy);
24
25 my %cgiparams=();
26 my %logsettings=();
27 my $errormessage = '';
28
29 my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
30 'Sep', 'Oct', 'Nov', 'Dec' );
31 my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
32 $Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'},
33 $Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'},
34 $Lang::tr{'december'} );
35
36 my @now = localtime();
37 my $dow = $now[6];
38 my $doy = $now[7];
39 my $tdoy = $now[7];
40 my $year = $now[5]+1900;
41
42 $cgiparams{'DAY'} = $now[3];
43 $cgiparams{'MONTH'} = $now[4];
44 $cgiparams{'ACTION'} = '';
45
46 &Header::getcgihash(\%cgiparams);
47
48 $logsettings{'LOGVIEW_REVERSE'} = 'off';
49 &General::readhash("${General::swroot}/logging/settings", \%logsettings);
50
51 my $start = -1;
52 if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
53 {
54 my @temp = split(',',$ENV{'QUERY_STRING'});
55 $start = $temp[0];
56 $cgiparams{'MONTH'} = $temp[1];
57 $cgiparams{'DAY'} = $temp[2];
58 $cgiparams{ip} = $temp[3];
59 }
60
61 if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
62 !($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)$/))
63 {
64 $cgiparams{'DAY'} = $now[3];
65 $cgiparams{'MONTH'} = $now[4];
66 }
67 elsif($cgiparams{'ACTION'} eq '>>')
68 {
69 my @temp_then=();
70 my @temp_now = localtime(time);
71 $temp_now[4] = $cgiparams{'MONTH'};
72 $temp_now[3] = $cgiparams{'DAY'};
73 @temp_then = localtime(POSIX::mktime(@temp_now) + 86400);
74 ## Retrieve the same time on the next day -
75 ## 86400 seconds in a day
76 $cgiparams{'MONTH'} = $temp_then[4];
77 $cgiparams{'DAY'} = $temp_then[3];
78 }
79 elsif($cgiparams{'ACTION'} eq '<<')
80 {
81 my @temp_then=();
82 my @temp_now = localtime(time);
83 $temp_now[4] = $cgiparams{'MONTH'};
84 $temp_now[3] = $cgiparams{'DAY'};
85 @temp_then = localtime(POSIX::mktime(@temp_now) - 86400);
86 ## Retrieve the same time on the previous day -
87 ## 86400 seconds in a day
88 $cgiparams{'MONTH'} = $temp_then[4];
89 $cgiparams{'DAY'} = $temp_then[3];
90 }
91
92 if (($cgiparams{'DAY'} ne $now[3]) || ($cgiparams{'MONTH'} ne $now[4]))
93 {
94 my @then = ();
95 if ( ( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
96 ( $cgiparams{'MONTH'} > $now[4] ) ) {
97 @then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 ));
98 } else {
99 @then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 ));
100 }
101 $tdoy = $then[7];
102 my $lastleap=($year-1)%4;
103 if ($tdoy>$doy) {
104 if ($lastleap == 0 && $tdoy < 60) {
105 $doy=$tdoy+366;
106 } else {
107 $doy=$doy+365;
108 }
109 }
110 }
111 my $datediff=0;
112 my $dowd=0;
113 my $multifile=0;
114 if ($tdoy ne $doy) {
115 $datediff=int(($doy-$tdoy)/7);
116 $dowd=($doy-$tdoy)%7;
117 if (($dow-$dowd)<1) {
118 $datediff=$datediff+1;
119 }
120 if (($dow-$dowd)==0) {
121 $multifile=1;
122 }
123 }
124
125 my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
126 my $longmonthstr = $longmonths[$cgiparams{'MONTH'}];
127 my $day = $cgiparams{'DAY'};
128 my $daystr='';
129 if ($day <= 9) {
130 $daystr = " $day"; }
131 else {
132 $daystr = $day;
133 }
134
135 my $skip=0;
136 my $filestr='';
137 if ($datediff==0) {
138 $filestr="/var/log/messages";
139 } else {
140 $filestr="/var/log/messages.$datediff";
141 $filestr = "$filestr.gz" if -f "$filestr.gz";
142 }
143
144 if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
145 $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
146 $skip=1;
147 # Note: This is in case the log does not exist for that date
148 }
149 my $lines = 0;
150 my @log=();
151 my $ip = $cgiparams{ip};
152
153 if (!$skip)
154 {
155 while (<FILE>) {
156 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
157 if (($_ =~ /SRC\=(([\d]{1,3})(\.([\d]{1,3})){3})/) or ($_ =~ /SRC\=(([0-9a-fA-F]{0,4})(\:([0-9a-fA-F]{0,4})){2,7})/)) {
158 if($1 eq $ip){
159 $log[$lines] = $_;
160 $lines++;
161 }
162 }
163 }
164 }
165 close (FILE);
166 }
167
168 $skip=0;
169 if ($multifile) {
170 $datediff=$datediff-1;
171 if ($datediff==0) {
172 $filestr="/var/log/messages";
173 } else {
174 $filestr="/var/log/messages.$datediff";
175 $filestr = "$filestr.gz" if -f "$filestr.gz";
176 }
177 if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
178 $errormessage="$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
179 $skip=1;
180 }
181 if (!$skip) {
182 while (<FILE>) {
183 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
184 if (($_ =~ /SRC\=(([\d]{1,3})(\.([\d]{1,3})){3})/) or ($_ =~ /SRC\=(([0-9a-fA-F]{0,4})(\:([0-9a-fA-F]{0,4})){2,7})/)) {
185 if($1 eq $ip){
186 $log[$lines] = $_;
187 $lines++;
188 }
189 }
190 }
191 }
192 close (FILE);
193 }
194 }
195
196 &Header::showhttpheaders();
197 &Header::openpage($Lang::tr{'firewall log ip'}, 1, '');
198 &Header::openbigbox('100%', 'left', '', $errormessage);
199
200 if ($errormessage) {
201 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
202 print "<font class='base'>$errormessage&nbsp;</font>\n";
203 &Header::closebox();
204 }
205
206 &Header::openbox('100%', 'left', "$Lang::tr{'settings'}:");
207
208 print <<END
209 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
210 <table width='100%'>
211 <tr>
212 <td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
213 <td width='10%'>
214 <select name='MONTH'>
215 END
216 ;
217 my $month;
218 for ($month = 0; $month < 12; $month++)
219 {
220 print "\t<option ";
221 if ($month == $cgiparams{'MONTH'}) {
222 print "selected='selected' "; }
223 print "value='$month'>$longmonths[$month]</option>\n";
224 }
225 print <<END
226 </select>
227 </td>
228 <td width='10%' class='base' align='right'>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
229 <td width='40%'>
230 <select name='DAY'>
231 END
232 ;
233 for ($day = 1; $day <= 31; $day++)
234 {
235 print "\t<option ";
236 if ($day == $cgiparams{'DAY'}) {
237 print "selected='selected' "; }
238 print "value='$day'>$day</option>\n";
239 }
240 print <<END
241 </select>
242 </td>
243 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
244 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
245 <td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
246 <tr><td width='15%'>$Lang::tr{'source ip'}</td><td><input type='text' name='ip' value='$cgiparams{ip}'size='15'></td></tr>
247 </tr>
248 </table>
249 </form>
250 END
251 ;
252
253 &Header::closebox();
254
255 &Header::openbox('100%', 'left', $Lang::tr{'firewall log ip'});
256 print "<p><b>$Lang::tr{'firewall hits'} $longmonthstr $daystr: $lines</b></p>";
257
258 if ($start == -1) {
259 $start = $lines - ${Header::viewsize};
260 }
261 if ($start >= $lines - ${Header::viewsize}) { $start = $lines - ${Header::viewsize}; };
262 if ($start < 0) { $start = 0; }
263
264 my $prev = $start - ${Header::viewsize};
265 my $next = $start + ${Header::viewsize};
266
267 if ($prev < 0) { $prev = 0; }
268 if ($next >= $lines) { $next = -1 }
269 if ($start == 0) { $prev = -1; }
270
271 if ($lines != 0) { &oldernewer(); }
272
273 print <<END
274 <table width='100%'>
275 <tr>
276 <td width='10%' align='center' class='boldbase'><b>$Lang::tr{'time'}</b></td>
277 <td width='13%' align='center' class='boldbase'><b>$Lang::tr{'chain'}</b></td>
278 <td width='5%' align='center' class='boldbase'><b>$Lang::tr{'iface'}</b></td>
279 <td width='5%' align='center' class='boldbase'><b>$Lang::tr{'proto'}</b></td>
280 <td width='16%' align='center' class='boldbase'><b>$Lang::tr{'source'}</b></td>
281 <td width='10%' align='center' class='boldbase'><b>$Lang::tr{'src port'}</b></td>
282 <td width='16%' align='center' class='boldbase'><b>$Lang::tr{'destination'}</b></td>
283 <td width='16%' align='center' class='boldbase'><b>$Lang::tr{'dst port'}</b></td>
284 </tr>
285 END
286 ;
287
288 my @slice = splice(@log, $start, ${Header::viewsize});
289
290 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @slice = reverse @slice; }
291
292 $lines = 0;
293 foreach $_ (@slice) {
294 $a = $_;
295 # Check whether valid ipv4 or ipv6 address
296 if (($_ =~ /SRC\=(([\d]{1,3})(\.([\d]{1,3})){3})/) or ($_ =~ /SRC\=(([0-9a-fA-F]{0,4})(\:([0-9a-fA-F]{0,4})){2,7})/)) {
297 if($1 eq $ip) {
298 my $chain = '';
299 my $in = '-'; my $out = '-';
300 my $srcaddr = ''; my $dstaddr = '';
301 my $protostr = '';
302 my $srcport = ''; my $dstport = '';
303
304 # If ipv6 uses bridge, the use PHYSIN, otherwise use IN
305 if ($_ =~ /(^.* ..:..:..) [\w\-]+ kernel:(.*)(IN=.*)(PHYSIN=.*)$/) {}
306 elsif ($_ =~ /(^.* ..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/) {}
307 my $timestamp = $1; my $chain = $2; my $packet = $3;
308 $timestamp =~ /(...) (..) (..:..:..)/;
309 my $month = $1; my $day = $2; my $time = $3;
310
311 # If ipv6 uses bridge, the use PHYSIN and PHYSOUT, otherwise use IN and OUT
312 if ($a =~ /PHYSIN=(\w+)/) { $iface = $1; } elsif ($a =~ /IN=(\w+)/) { $iface = $1; }
313 if ($a =~ /PHYSOUT=(\w+)/) { $out = $1; } elsif ($a =~ /OUT=(\w+)/) { $out = $1; }
314 # Detect ipv4 and ipv6 addresses
315 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; }
316 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; }
317 if ($a =~ /PROTO\=(\w+)/) { $protostr = $1; }
318 my $protostrlc = lc($protostr);
319 if ($a =~ /SPT\=([\d\.]+)/){ $srcport = $1; }
320 if ($a =~ /DPT\=([\d\.]+)/){ $dstport = $1; }
321
322 if ($lines % 2) {
323 print "<tr bgcolor='${Header::table1colour}'>\n";
324 }
325 else {
326 print "<tr bgcolor='${Header::table2colour}'>\n";
327 }
328 print <<END
329 <td align='center'>$time</td>
330 <td align='center'>$chain</td>
331 <td align='center'>$iface</td>
332 <td align='center'>$protostr</td>
333 <td align='center'>
334 <table width='100%' cellpadding='0' cellspacing='0'><tr>
335 <td align='center'><a href='/cgi-bin/ipinfo.cgi?ip=$srcaddr'>$srcaddr</a></td>
336 </tr></table>
337 </td>
338 <td align='center'>$srcport</td>
339 <td align='center'>
340 <table width='100%' cellpadding='0' cellspacing='0'><tr>
341 <td align='center'><a href='/cgi-bin/ipinfo.cgi?ip=$dstaddr'>$dstaddr</a></td>
342 </tr></table>
343 </td>
344 <td align='center'>$dstport</td>
345 </tr>
346 END
347 ;
348 $lines++;
349 }
350 }
351 }
352
353 print <<END
354 </table>
355 END
356 ;
357
358 &oldernewer();
359
360 print"<table width='100%'><tr><td align='center'><a href='/cgi-bin/logs.cgi/firewalllogip.dat'><img src='/images/back.png' alt='$Lang::tr{'back'}' title='$Lang::tr{'back'}' /></a></td></tr></table>";
361
362 &Header::closebox();
363
364 &Header::closebigbox();
365
366 &Header::closepage();
367
368 sub oldernewer
369 {
370 print <<END
371 <table width='100%'>
372 <tr>
373 END
374 ;
375
376 print "<td align='center' width='50%'>";
377 if ($prev != -1) {
378 print "<a href='/cgi-bin/logs.cgi/showrequestfromip.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{ip}'>$Lang::tr{'older'}</a>";
379 }
380 else {
381 print "$Lang::tr{'older'}";
382 }
383 print "</td>\n";
384
385 print "<td align='center' width='50%'>";
386 if ($next != -1) {
387 print "<a href='/cgi-bin/logs.cgi/showrequestfromip.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{ip}'>$Lang::tr{'newer'}</a>";
388 }
389 else {
390 print "$Lang::tr{'newer'}";
391 }
392 print "</td>\n";
393
394 print <<END
395 </tr>
396 </table>
397 END
398 ;
399 }