]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/logs.cgi/log.dat
Menu: Add ipblocklist entry to firewall menu.
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / logs.cgi / log.dat
CommitLineData
cd1a2927
MT
1#!/usr/bin/perl
2#
3# SmoothWall CGIs
4#
5# This code is distributed under the terms of the GPL
6#
7# (c) The SmoothWall Team
8#
9# $Id: log.dat,v 1.6.2.22 2006/01/03 12:47:17 franck78 Exp $
10#
11
12use strict;
13
14# enable only the following on debugging purpose
5595bc03
CS
15use warnings;
16use CGI::Carp 'fatalsToBrowser';
cd1a2927 17
986e08d9 18require '/var/ipfire/general-functions.pl';
cd1a2927
MT
19require "${General::swroot}/lang.pl";
20require "${General::swroot}/header.pl";
21
f2fdd0c1
CS
22my %color = ();
23my %mainsettings = ();
24&General::readhash("${General::swroot}/main/settings", \%mainsettings);
8186b372 25&General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
f2fdd0c1 26
cd1a2927
MT
27use POSIX();
28
cd1a2927
MT
29my %cgiparams=();
30my %logsettings=();
31my $errormessage = '';
32
33my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
34 'Sep', 'Oct', 'Nov', 'Dec' );
35my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
36 $Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'},
37 $Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'},
38 $Lang::tr{'december'} );
39
40my @now = localtime();
41my $dow = $now[6];
42my $doy = $now[7];
43my $tdoy = $now[7];
44my $year = $now[5]+1900;
45
46$cgiparams{'DAY'} = $now[3];
47$cgiparams{'MONTH'} = $now[4];
48$cgiparams{'ACTION'} = '';
97fe1741 49$cgiparams{'SECTION'} = 'ipfire';
cd1a2927
MT
50
51my %sections = (
5e818d6a 52 'auth' => '(\w+\(pam_unix\)\[.*\]: )',
4f19781d 53 'wio' => '(wio:|wio\[.*\])',
6d31cfdd 54 'captive' => '(Captive:)',
4f57d698 55 'clamav' => '(clamd\[.*\]: |freshclam\[.*\]: )',
5362088b 56 'collectd' => '(collectd\[.*\]: )',
5595bc03 57 'cron' => '(fcron\[.*\]: )',
5e818d6a
MF
58 'ddns' => '(ddns\[\d+\]:)',
59 'dhcp' => '(dhcpd: )',
11121641 60 'dma' => '(dma: |dma\[.*\]: |postfix/\w*\[\d*\]: )',
5e818d6a
MF
61 'guardian' => '(guardian\[.*\]: )',
62 'ipfire' => '(ipfire: )',
0e14ef8a 63 'ipsec' => '(ipsec_[\w_]+: |pluto\[.*\]: |charon: |vpnwatch: )',
5e818d6a 64 'kernel' => '(kernel: (?!DROP_))',
8f58e661 65 'monit' => '(monit\[.*?\]: )',
5e818d6a 66 'ntp' => '(ntpd(?:ate)?\[.*\]: )',
5508f18c 67 'oinkmaster' => '(oinkmaster\[.*\]: )',
0f0db884 68 'openvpn' => '(openvpnserver\[.*\]: |.*n2n\[.*\]: )',
5e818d6a
MF
69 'pakfire' => '(pakfire:)',
70 'red' => '(red:|pppd\[.*\]: |chat\[.*\]|pppoe\[.*\]|pptp\[.*\]|pppoa\[.*\]|pppoa3\[.*\]|pppoeci\[.*\]|ipppd|ipppd\[.*\]|kernel: ippp\d|kernel: isdn.*|ibod\[.*\]|dhcpcd\[.*\]|modem_run\[.*\])',
13e455ae 71 'samba' => '(nmbd|smbd|winbind)\[\d+\]:',
830dfc97 72 'suricata' => '(suricata: )',
5e818d6a
MF
73 'squid' => '(squid\[.*\]: |squid: )',
74 'ssh' => '(sshd(?:\(.*\))?\[.*\]: )',
d659de88 75 'unbound' => '(unbound: \[.*:.*\])(.*:.*$)',
5e818d6a
MF
76 'urlfilter bl' => '(installpackage\[urlfilter\]: )',
77 'wireless' => '(hostapd:|kernel: ath.*:|kernel: wifi[0-9]:)'
cd1a2927
MT
78 );
79
80# Translations for the %sections array.
81my %trsections = (
5e818d6a 82 'auth' => "$Lang::tr{'loginlogout'}",
dec52693 83 'wio' => 'Who Is Online?',
e26a5c48 84 'captive' => $Lang::tr{'Captive'},
5e818d6a
MF
85 'clamav' => 'ClamAV',
86 'collectd' => 'Collectd',
87 'cron' => 'Cron',
9494c611 88 'ddns' => "$Lang::tr{'dynamic dns'}",
cd1a2927 89 'dhcp' => "$Lang::tr{'dhcp server'}",
5e818d6a 90 'dma' => 'Mail',
5e818d6a
MF
91 'guardian' => "$Lang::tr{'guardian'}",
92 'ipfire' => 'IPFire',
cd1a2927 93 'ipsec' => 'IPSec',
5e818d6a 94 'kernel' => "$Lang::tr{'kernel'}",
0b2aa517 95 'monit' => 'Monit',
5e818d6a 96 'ntp' => 'NTP',
c77bd492 97 'oinkmaster' => 'Oinkmaster',
6e13d0a5 98 'openvpn' => 'OpenVPN',
c506cad0 99 'pakfire' => 'Pakfire',
5e818d6a 100 'red' => 'RED',
13e455ae 101 'samba' => $Lang::tr{'samba'},
9f9651e0 102 'suricata' => "$Lang::tr{'intrusion detection'}",
5e818d6a
MF
103 'squid' => "$Lang::tr{'web proxy'}",
104 'ssh' => 'SSH',
d659de88 105 'unbound' => 'DNS: Unbound',
5e818d6a
MF
106 'urlfilter bl' => 'URLFilter Blacklist',
107 'wireless' => 'Wireless'
cd1a2927
MT
108 );
109
110
111&Header::getcgihash(\%cgiparams);
112$logsettings{'LOGVIEW_REVERSE'} = 'off';
113&General::readhash("${General::swroot}/logging/settings", \%logsettings);
114${Header::viewsize} = defined ($logsettings{'LOGVIEW_VIEWSIZE'}) ? $logsettings{'LOGVIEW_VIEWSIZE'} : 150;
115
116my $start = ($logsettings{'LOGVIEW_REVERSE'} eq 'on') ? 0x7FFFF000 : 0; #index of firts line number to display
117
118if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
119{
120 my @temp = split(',',$ENV{'QUERY_STRING'});
121 $start = $temp[0];
122 $cgiparams{'MONTH'} = $temp[1];
123 $cgiparams{'DAY'} = $temp[2];
124 $cgiparams{'SECTION'} = $temp[3];
125}
126
127if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
128 !($cgiparams{'DAY'} =~ /^(0|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)$/))
129{
130 $cgiparams{'DAY'} = $now[3];
131 $cgiparams{'MONTH'} = $now[4];
132}
133elsif($cgiparams{'ACTION'} eq '>>')
134{
135 my @temp_then=();
136 my @temp_now = localtime(time);
137 $temp_now[4] = $cgiparams{'MONTH'};
138 $temp_now[3] = $cgiparams{'DAY'};
139 if ($cgiparams{'DAY'}) {
140 @temp_then = localtime(POSIX::mktime(@temp_now) + 86400);
141 ## Retrieve the same time on the next day +
142 ## 86400 seconds in a day
143 } else {
144 $temp_now[3] = 1;
145 $temp_now[4] = ($temp_now[4]+1) %12;
146 @temp_then = localtime(POSIX::mktime(@temp_now) );
147 $temp_then[3] = 0;
148 }
149 $cgiparams{'MONTH'} = $temp_then[4];
150 $cgiparams{'DAY'} = $temp_then[3];
151}
152elsif($cgiparams{'ACTION'} eq '<<')
153{
154 my @temp_then=();
155 my @temp_now = localtime(time);
156 $temp_now[4] = $cgiparams{'MONTH'};
157 $temp_now[3] = $cgiparams{'DAY'};
158 if ($cgiparams{'DAY'}) {
159 @temp_then = localtime(POSIX::mktime(@temp_now) - 86400);
160 ## Retrieve the same time on the next day -
161 ## 86400 seconds in a day
162 } else {
163 $temp_now[3] = 1;
164 $temp_now[4] = ($temp_now[4]-1) %12;
165 @temp_then = localtime(POSIX::mktime(@temp_now) );
166 $temp_then[3] = 0;
167 }
168 $cgiparams{'MONTH'} = $temp_then[4];
169 $cgiparams{'DAY'} = $temp_then[3];
170}
171
172# Find in which file.gz is the log. Can be calculated because WEEKLY ROTATING of access.log
173my $gzindex;
174my $date = $cgiparams{'DAY'} == 0 ? '' : $cgiparams{'DAY'} <= 9 ? "0$cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
175
176{
177 my $xday;
9f663e66 178
cd1a2927
MT
179 # Calculate time. If future date, calculate for past year !!!
180 if (( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
181 ( $cgiparams{'MONTH'} > $now[4] ) ) {
182 $xday = POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 );
183 $date = "$longmonths[$cgiparams{'MONTH'}] $date, ". int($year-1);
184 } else {
185 $xday = POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 );
186 $date = "$longmonths[$cgiparams{'MONTH'}] $date, $year";
187 }
188
189 # calculate end of active week (saturday 23H59)
190 my @then = ();
191 @then = localtime(time());
0d08de33 192 my $sunday = POSIX::mktime( 0, 0, 0, $then[3], $then[4], $then[5]);
cd1a2927
MT
193 $sunday += (6-$then[6]) * 86400;
194
195 # Convert delta in second to full weeks
196 $gzindex = int (($sunday-$xday)/604800 );
197}
9f663e66 198
cd1a2927
MT
199my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
200my $daystr = $cgiparams{'DAY'} == 0 ? '..' : $cgiparams{'DAY'} <= 9 ? " $cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
201my $section = $sections{$cgiparams{'SECTION'}};
202
203my $lines = 0;
204my @log=();
205
206my $loop = 1;
207my $filestr = 0;
208my $lastdatetime; # for debug
209my $search_for_end = 0;
9f663e66 210
cd1a2927
MT
211while ($gzindex >=0 && $loop) {
212 # calculate file name
213 if ($gzindex == 0) {
214 $filestr = "/var/log/messages";
1986cc88 215 $filestr = "/var/log/mail" if (${section} =~ 'dma');
cd1a2927
MT
216 } else {
217 $filestr = "/var/log/messages.$gzindex";
1986cc88 218 $filestr = "/var/log/mail.$gzindex" if (${section} =~ 'dma');
cd1a2927
MT
219 $filestr = "$filestr.gz" if -f "$filestr.gz";
220 }
221 # now read file if existing
222 if (open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr))) {
223 #&General::log("reading $filestr");
224 READ:while (<FILE>) {
225 my $line = $_;
5595bc03 226 if ($line =~ /^${monthstr} ${daystr} ..:..:.. [\w\-]+ ${section}(.*)/) {
5e818d6a 227 # when standard viewing, just keep in memory the correct slice
cd1a2927
MT
228 # it starts a '$start' and size is $viewport
229 # If export, then keep all lines...
230 if ($cgiparams{'ACTION'} eq $Lang::tr{'export'}){
231 $log[$lines++] = "$line";
232 } else {
233 if ($lines++ < ($start + $Header::viewsize)) {
234 push(@log,"$line");
235 if (@log > $Header::viewsize) {
236 shift (@log);
237 }
5e818d6a 238 #} else { don't do this optimisation, need to count lines !
cd1a2927
MT
239 # $datetime = $maxtime; # we have read viewsize lines, stop main loop
240 # last READ; # exit read file
241 }
242 }
243 $search_for_end = 1; # we find the start of slice, can look for end now
244 } else {
245 if ($search_for_end == 1) {
246 #finish read files when date is over (test month equality only)
247 $line =~ /^(...) (..) ..:..:..*$/;
248 $loop = 0 if ( ($1 ne $monthstr) || ( ($daystr ne '..') && ($daystr ne $2) ) );
249 }
250 }
251 }
252 close (FILE);
253 }
254 $gzindex--; # will try next gz file eg 40,39,38,.... because it may have holes when ipcop stopped
255 # for a long time
256}# while
257
258# $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
259
260if ($cgiparams{'ACTION'} eq $Lang::tr{'export'})
261{
262 print "Content-type: text/plain\n\n";
78331e30 263 print "IPFire diagnostics\r\n";
cd1a2927
MT
264 print "$Lang::tr{'section'}: $cgiparams{'SECTION'}\n";
265 print "$Lang::tr{'date'}: $date\r\n\r\n";
266
267 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
268
269 foreach $_ (@log)
270 {
5595bc03 271 /^... (..) (..:..:..) [\w\-]+ ${section}(.*)$/;
cd1a2927
MT
272 my $day = $1;
273 $day =~ tr / /0/;
274 my $time = $cgiparams{'DAY'} ? "$2" : "$day/$2" ;
5595bc03 275 print "$time $3 $4\r\n";
cd1a2927
MT
276 }
277 exit 0;
278}
279
280&Header::showhttpheaders();
281
986e08d9 282&Header::openpage($Lang::tr{'system logs'}, 1, '');
cd1a2927
MT
283
284&Header::openbigbox('100%', 'left', '', $errormessage);
285
286if ($errormessage) {
287 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
288 print "<font class='base'>$errormessage&nbsp;</font>\n";
289 &Header::closebox();
290}
291
292&Header::openbox('100%', 'left', "$Lang::tr{'settings'}:");
293
294print <<END
295<form method='post' action='$ENV{'SCRIPT_NAME'}'>
296<table width='100%'>
297<tr>
298 <td width='10%' class='base'>$Lang::tr{'section'}:&nbsp;</td>
299 <td width='20%'>
300 <select name='SECTION'>
301END
302;
5e818d6a 303foreach $section (sort {$trsections{$a} cmp $trsections{$b}} keys %sections) {
cd1a2927
MT
304 print "\t<option ";
305 if ($section eq $cgiparams{'SECTION'}) {
306 print "selected='selected' "; }
307 print "value='$section'>$trsections{$section}</option>\n";
308}
309print <<END
310 </select>
311 </td>
312 <td width='10%' class='base' align='right'>$Lang::tr{'month'}:&nbsp;</td>
313 <td width='10%'>
314 <select name='MONTH'>
315END
316;
317for (my $month = 0; $month < 12; $month++)
318{
319 print "\t<option ";
320 if ($month == $cgiparams{'MONTH'}) {
321 print "selected='selected' "; }
322 print "value='$month'>$longmonths[$month]</option>\n";
323}
324print <<END
325 </select>
326 </td>
327 <td width='10%' class='base' align='right'>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
328 <td width='40%'>
329 <select name='DAY'>
330END
331;
332print "<option value='0'>$Lang::tr{'all'}</option>\n";
9f663e66 333for (my $day = 1; $day <= 31; $day++)
cd1a2927
MT
334{
335 print "\t<option ";
336 if ($day == $cgiparams{'DAY'}) {
337 print "selected='selected' "; }
338 print "value='$day'>$day</option>\n";
339}
340print <<END
341</select>
342</td>
343<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
344<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
345<td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
346<td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'export'}' /></td>
347</tr>
348</table>
349</form>
350END
351;
352
353&Header::closebox();
354
355&Header::openbox('100%', 'left', $Lang::tr{'log'});
356print "<p><b>$Lang::tr{'total hits for log section'} $cgiparams{'SECTION'} $date: $lines</b></p>";
357
358$start = $lines - ${Header::viewsize} if ($start >= $lines - ${Header::viewsize});
359$start = 0 if ($start < 0);
360
361my $prev;
362 if ($start == 0) {
363 $prev = -1;
364 } else {
365 $prev = $start - ${Header::viewsize};
366 $prev = 0 if ( $prev < 0);
367 }
9f663e66 368
cd1a2927
MT
369my $next;
370 if ($start == $lines - ${Header::viewsize}) {
371 $next = -1;
372 } else {
373 $next = $start + ${Header::viewsize};
374 $next = $lines - ${Header::viewsize} if ($next >= $lines - ${Header::viewsize});
375 }
376
377if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
378if ($lines != 0) { &oldernewer(); }
379
380print <<END
fda1f52f 381<table width='100%' class='tbl'>
cd1a2927 382<tr>
fda1f52f 383 <th width='10%' align='center' class='boldbase'><b>$Lang::tr{'time'}</b></th>
5e818d6a
MF
384 <th width='15%' align='center' class='boldbase'><b>$Lang::tr{'section'}</b></th>
385 <th width='75%'>&nbsp;</th>
cd1a2927
MT
386</tr>
387END
388;
389
390$lines = 0;
391#print '<tt>';
392foreach $_ (@log)
393{
5595bc03 394 /^... (..) (..:..:..) [\w\-]+ ${section}(.*)$/;
cd1a2927
MT
395 my $day = $1;
396 $day =~ tr / /0/;
397 my $time = $cgiparams{'DAY'} ? "$2" : "$day/$2" ;
398 my $sec = $3;
399 my $data = $4;
400 # correct the cut position, just when section=RED
401 if (($cgiparams{'SECTION'} eq 'red' ) && ($sec =~ /(kernel:)(.*)/)) {
402 $sec = 'kernel';
403 $data = $2.': '.$data;
404 }
405 my $d = substr ($data,0,80);
406 while (length($data)>80){ #very basic breaking of lines...
407 $data = substr ($data,80); #permit correct display in table cell
408 $d .= ' ' . substr ($data,0,80);
409 }
fda1f52f 410 my $col="";
cd1a2927
MT
411
412 if ($lines % 2) {
fda1f52f
AM
413 print "<tr>";
414 $col="bgcolor='$color{'color20'}'"; }
cd1a2927 415 else {
fda1f52f
AM
416 print "<tr>";
417 $col="bgcolor='$color{'color22'}'"; }
418 print "<td $col>$time<td $col>$sec<td $col>" .&Header::cleanhtml ("$d", 'y') . "</td></tr>\n";
cd1a2927
MT
419 $lines++;
420}
421#print '</tt>';
422print "</table>";
423
424&oldernewer();
425
426&Header::closebox();
427
428&Header::closebigbox();
429
430&Header::closepage();
431
432sub oldernewer
433{
434print <<END
435<table width='100%'>
436<tr>
437END
438;
439
440print "<td align='center' width='50%'>";
441if ($prev != -1) {
442 print "<a href='/cgi-bin/logs.cgi/log.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SECTION'}'>$Lang::tr{'older'}</a>"; }
443else {
444 print "$Lang::tr{'older'}"; }
445print "</td>\n";
446
447print "<td align='center' width='50%'>";
448if ($next >= 0) {
449 print "<a href='/cgi-bin/logs.cgi/log.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SECTION'}'>$Lang::tr{'newer'}</a>"; }
450else {
451 print "$Lang::tr{'newer'}"; }
452print "</td>\n";
453
454print <<END
455</tr>
456</table>
457END
458;
459}