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