]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - html/cgi-bin/logs.cgi/showrequestfromip.dat
core81: set need reboot flag and restart apache.
[people/teissler/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 {
157 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
158 if($_ =~ /SRC\=([\d\.]+)/){
159 if($1 eq $ip){
160 $log[$lines] = $_;
161 $lines++;
162 }
163 }
164 }
165 }
166 close (FILE);
167 }
168
169 $skip=0;
170 if ($multifile) {
171 $datediff=$datediff-1;
172 if ($datediff==0) {
173 $filestr="/var/log/messages";
174 } else {
175 $filestr="/var/log/messages.$datediff";
176 $filestr = "$filestr.gz" if -f "$filestr.gz";
177 }
178 if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
179 $errormessage="$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
180 $skip=1;
181 }
182 if (!$skip) {
183 while (<FILE>) {
184 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
185 if($_ =~ /SRC\=([\d\.]+)/){
186 if($1 eq $ip){
187 $log[$lines] = $_;
188 $lines++;
189 }
190 }
191 }
192 }
193 close (FILE);
194 }
195 }
196
197 &Header::showhttpheaders();
198 &Header::openpage($Lang::tr{'firewall log'}, 1, '');
199 &Header::openbigbox('100%', 'left', '', $errormessage);
200
201 if ($errormessage) {
202 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
203 print "<font class='base'>$errormessage&nbsp;</font>\n";
204 &Header::closebox();
205 }
206
207 &Header::openbox('100%', 'left', "$Lang::tr{'settings'}:");
208
209 print <<END
210 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
211 <table width='100%'>
212 <tr>
213 <td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
214 <td width='10%'>
215 <select name='MONTH'>
216 END
217 ;
218 my $month;
219 for ($month = 0; $month < 12; $month++)
220 {
221 print "\t<option ";
222 if ($month == $cgiparams{'MONTH'}) {
223 print "selected='selected' "; }
224 print "value='$month'>$longmonths[$month]</option>\n";
225 }
226 print <<END
227 </select>
228 </td>
229 <td width='10%' class='base' align='right'>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
230 <td width='40%'>
231 <select name='DAY'>
232 END
233 ;
234 for ($day = 1; $day <= 31; $day++)
235 {
236 print "\t<option ";
237 if ($day == $cgiparams{'DAY'}) {
238 print "selected='selected' "; }
239 print "value='$day'>$day</option>\n";
240 }
241 print <<END
242 </select>
243 </td>
244 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
245 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
246 <td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
247 <tr><td width='15%'>$Lang::tr{'source ip'}</td><td><input type='text' name='ip' value='$cgiparams{ip}'size='15'></td></tr>
248 </tr>
249 </table>
250 </form>
251 END
252 ;
253
254 &Header::closebox();
255
256 &Header::openbox('100%', 'left', $Lang::tr{'firewall log'});
257 print "<p><b>$Lang::tr{'firewall hits'} $longmonthstr $daystr: $lines</b></p>";
258
259 if ($start == -1) {
260 $start = $lines - ${Header::viewsize}; }
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 {
295 $a = $_;
296 if($_ =~ /SRC\=([\d\.]+)/){
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 $_ =~ /(^.* ..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/;
305 my $timestamp = $1; my $chain = $2; my $packet = $3;
306 $timestamp =~ /(...) (..) (..:..:..)/;
307 my $month = $1; my $day = $2; my $time = $3;
308
309 if ($a =~ /IN\=(\w+)/) { $iface = $1; }
310 if ($a =~ /OUT\=(\w+)/) { $out = $1; }
311 if ($a =~ /SRC\=([\d\.]+)/) { $srcaddr = $1; }
312 if ($a =~ /DST\=([\d\.]+)/) { $dstaddr = $1; }
313 if ($a =~ /PROTO\=(\w+)/) { $protostr = $1; }
314 my $protostrlc = lc($protostr);
315 if ($a =~ /SPT\=([\d\.]+)/){ $srcport = $1; }
316 if ($a =~ /DPT\=([\d\.]+)/){ $dstport = $1; }
317
318 if ($lines % 2) {
319 print "<tr bgcolor='${Header::table1colour}'>\n"; }
320 else {
321 print "<tr bgcolor='${Header::table2colour}'>\n"; }
322 print <<END
323 <td align='center'>$time</td>
324 <td align='center'>$chain</td>
325 <td align='center'>$iface</td>
326 <td align='center'>$protostr</td>
327 <td align='center'>
328 <table width='100%' cellpadding='0' cellspacing='0'><tr>
329 <td align='center'><a href='/cgi-bin/ipinfo.cgi?ip=$srcaddr'>$srcaddr</a></td>
330 </tr></table>
331 </td>
332 <td align='center'>$srcport</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=$dstaddr'>$dstaddr</a></td>
336 </tr></table>
337 </td>
338 <td align='center'>$dstport</td>
339 </tr>
340 END
341 ;
342 $lines++;
343 }
344 }
345 }
346
347 print <<END
348 </table>
349 END
350 ;
351
352 &oldernewer();
353
354 &Header::closebox();
355
356 &Header::closebigbox();
357
358 &Header::closepage();
359
360 sub oldernewer
361 {
362 print <<END
363 <table width='100%'>
364 <tr>
365 END
366 ;
367
368 print "<td align='center' width='50%'>";
369 if ($prev != -1) {
370 print "<a href='/cgi-bin/logs.cgi/showrequestfromip.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{ip}'>$Lang::tr{'older'}</a>"; }
371 else {
372 print "$Lang::tr{'older'}"; }
373 print "</td>\n";
374
375 print "<td align='center' width='50%'>";
376 if ($next != -1) {
377 print "<a href='/cgi-bin/logs.cgi/showrequestfromip.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{ip}'>$Lang::tr{'newer'}</a>"; }
378 else {
379 print "$Lang::tr{'newer'}"; }
380 print "</td>\n";
381
382 print <<END
383 </tr>
384 </table>
385 END
386 ;
387 }