]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/logs.cgi/proxylog.dat
suricata: Change midstream policy to "pass-flow"
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / logs.cgi / proxylog.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#
cd1a2927
MT
9
10use strict;
11
12# enable only the following on debugging purpose
13#use warnings;
14#use CGI::Carp 'fatalsToBrowser';
15
986e08d9 16require '/var/ipfire/general-functions.pl';
cd1a2927
MT
17require "${General::swroot}/lang.pl";
18require "${General::swroot}/header.pl";
19
20use POSIX();
21
22#workaround to suppress a warning when a variable is used only once
23my @dummy = ( ${Header::table2colour} );
24undef (@dummy);
25
26my %cgiparams=();
27my %logsettings=();
28my %ips=();
363fb6af 29my %users=();
cd1a2927
MT
30my %selected=();
31my %checked=();
32my @log=();
33my $errormessage = '';
34
f2fdd0c1
CS
35my %color = ();
36my %mainsettings = ();
37&General::readhash("${General::swroot}/main/settings", \%mainsettings);
8186b372 38&General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
f2fdd0c1 39
cd1a2927
MT
40my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
41 'Sep', 'Oct', 'Nov', 'Dec' );
42my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
43 $Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'},
44 $Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'},
45 $Lang::tr{'december'} );
46
47my @now = localtime();
48my $dow = $now[6]; # day of week
49my $doy = $now[7]; # day of year (0..364)
50my $tdoy = $now[7];
51my $year = $now[5]+1900;
52
66c36198 53$cgiparams{'DAY'} = $now[3];
cd1a2927
MT
54$cgiparams{'MONTH'} = $now[4];
55$cgiparams{'SOURCE_IP'} = 'ALL';
56$cgiparams{'FILTER'} = "[.](gif|jpeg|jpg|png|css|js)\$";
57$cgiparams{'ENABLE_FILTER'} = 'off';
58$cgiparams{'ACTION'} = '';
59
60&Header::getcgihash(\%cgiparams);
61$logsettings{'LOGVIEW_REVERSE'} = 'off';
62&General::readhash("${General::swroot}/logging/settings", \%logsettings);
63${Header::viewsize} = defined ($logsettings{'LOGVIEW_VIEWSIZE'}) ? $logsettings{'LOGVIEW_VIEWSIZE'} : 150;
64
65if ($cgiparams{'ACTION'} eq '')
66{
67 $cgiparams{'ENABLE_FILTER'} = 'on';
68}
69
70if ($cgiparams{'ACTION'} eq $Lang::tr{'restore defaults'})
71{
72 $cgiparams{'FILTER'} = "[.](gif|jpeg|jpg|png|css|js)\$";
66c36198 73 $cgiparams{'ENABLE_FILTER'} = 'off';
cd1a2927
MT
74}
75
76{
77 my %save=();
78 $save{'FILTER'} = $cgiparams{'FILTER'};
79 $save{'ENABLE_FILTER'} = $cgiparams{'ENABLE_FILTER'};
80 &General::writehash("${General::swroot}/proxy/viewersettings", \%save);
81 &General::readhash("${General::swroot}/proxy/viewersettings", \%save);
82}
83
84my $start = ($logsettings{'LOGVIEW_REVERSE'} eq 'on') ? 0x7FFFF000 : 0; #index of first line number to display
85
86if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
87{
88 my @temp = split(',',$ENV{'QUERY_STRING'});
89 $start = $temp[0];
90 $cgiparams{'MONTH'} = $temp[1];
66c36198 91 $cgiparams{'DAY'} = $temp[2];
cd1a2927 92 $cgiparams{'SOURCE_IP'} = $temp[3];
5ba9e660 93 $cgiparams{'USERNAME'} = &Header::escape($temp[4]);
cd1a2927
MT
94}
95
96if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
97 !($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)$/))
98{
99 $cgiparams{'DAY'} = $now[3];
100 $cgiparams{'MONTH'} = $now[4];
101}
102elsif($cgiparams{'ACTION'} eq '>>')
103{
104 my @temp_then=();
105 my @temp_now = localtime(time);
106 $temp_now[4] = $cgiparams{'MONTH'};
107 $temp_now[3] = $cgiparams{'DAY'};
108 if ($cgiparams{'DAY'}) {
109 @temp_then = localtime(POSIX::mktime(@temp_now) + 86400);
110 ## Retrieve the same time on the next day +
111 ## 86400 seconds in a day
112 $cgiparams{'MONTH'} = $temp_then[4];
113 $cgiparams{'DAY'} = $temp_then[3];
114 } else {
115 $temp_now[3] = 1;
116 $temp_now[4] = ($temp_now[4]+1) %12;
117 @temp_then = localtime(POSIX::mktime(@temp_now) );
118 @temp_then[3] = 0;
119 }
120 $cgiparams{'MONTH'} = $temp_then[4];
121 $cgiparams{'DAY'} = $temp_then[3];
122}
123elsif($cgiparams{'ACTION'} eq '<<')
124{
125 my @temp_then=();
126 my @temp_now = localtime(time);
127 $temp_now[4] = $cgiparams{'MONTH'};
128 $temp_now[3] = $cgiparams{'DAY'};
129 if ($cgiparams{'DAY'}) {
130 @temp_then = localtime(POSIX::mktime(@temp_now) - 86400);
131 ## Retrieve the same time on the next day -
132 ## 86400 seconds in a day
133 $cgiparams{'MONTH'} = $temp_then[4];
134 $cgiparams{'DAY'} = $temp_then[3];
135 } else {
136 $temp_now[3] = 1;
137 $temp_now[4] = ($temp_now[4]-1) %12;
138 @temp_then = localtime(POSIX::mktime(@temp_now) );
139 @temp_then[3] = 0;
140 }
141 $cgiparams{'MONTH'} = $temp_then[4];
142 $cgiparams{'DAY'} = $temp_then[3];
143}
144
145# Find in which file.gz is the log. Can be calculated because WEEKLY ROTATING of *.log
146my $gzindex;
147my $daystr = $cgiparams{'DAY'} == 0 ? '' :$cgiparams{'DAY'} <= 9 ? "0$cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
148
149{
150 my $xday;
151
152 # Calculate time. If future date, calculate for past year !!!
153 if (( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
66c36198 154 ( $cgiparams{'MONTH'} > $now[4] ) ) {
cd1a2927
MT
155 $xday = POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 );
156 $daystr = "$longmonths[$cgiparams{'MONTH'}] $daystr, ". int($year-1);
157 } else {
158 $xday = POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 );
159 $daystr = "$longmonths[$cgiparams{'MONTH'}] $daystr, $year";
160 }
161
162 # calculate end of active week (saturday 23H59)
163 my @then = ();
164 @then = localtime(time());
165 my $sunday = POSIX::mktime( 0, 0, 0, @then[3], @then[4], @then[5]);
166 $sunday += (6-$then[6]) * 86400;
167
168 # Convert delta in second to full weeks
169 $gzindex = int (($sunday-$xday)/604800 );
170}
171
172
173my $filter = $cgiparams{'ENABLE_FILTER'} eq 'on' ? $cgiparams{'FILTER'} : '';
174my $sourceip = $cgiparams{'SOURCE_IP'};
175my $sourceall = $cgiparams{'SOURCE_IP'} eq 'ALL' ? 1 : 0;
363fb6af
MT
176my $username = $cgiparams{'USERNAME'};
177my $usersall = $cgiparams{'USERNAME'} eq 'ALL' ? 1 : 0;
cd1a2927
MT
178
179my $lines = 0;
180my $temp = ();
181my $thiscode = '$temp =~ /$filter/;';
182eval($thiscode);
183if ($@ ne '')
184{
b740cdd7 185 $errormessage = "$Lang::tr{'bad ignore filter'}.$@<p>";
cd1a2927
MT
186 $filter = '';
187} else {
188 my $loop = 1;
189 my $filestr = 0;
190 my $lastdatetime; # for debug
191
192 while ($gzindex >=0 && $loop) {
193 # calculate file name
194 if ($gzindex == 0) {
195 $filestr = "/var/log/squid/access.log";
196 } else {
197 $filestr = "/var/log/squid/access.log.$gzindex";
198 $filestr = "$filestr.gz" if -f "$filestr.gz";
199 }
200 # now read file if existing
201 if (open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr))) {
202 #&General::log("reading $filestr");
203 my @temp_now = localtime(time);
204 $temp_now[4] = $cgiparams{'MONTH'};
205 $temp_now[3] = $cgiparams{'DAY'};
206 if ( ( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
207 ( $cgiparams{'MONTH'} > $now[4] ) ) {
208 $temp_now[5]--; # past year
209 }
210
211 $temp_now[2] = $temp_now[1] = $temp_now[0] = 0; # start at 00:00:00
212 $temp_now[3] = 1 if ($cgiparams{'DAY'}==0); # All days selected, start at '1'
66c36198 213 my $mintime = POSIX::mktime(@temp_now);
cd1a2927
MT
214 my $maxtime;
215 if ($cgiparams{'DAY'}==0) { # full month
216 if ($temp_now[4]++ == 12){
217 $temp_now[4] = 0;
218 $temp_now[5]++;
219 };
66c36198 220 $maxtime = POSIX::mktime(@temp_now);
cd1a2927
MT
221 } else {
222 $maxtime = $mintime + 86400; # full day
223 }
224 READ:while (<FILE>) {
225 my ($datetime,$do,$ip,$ray,$me,$far,$url,$so) = split;
226 $ips{$ip}++;
363fb6af 227 $users{$so}++;
cd1a2927
MT
228 # for debug
229 #$lastdatetime = $datetime;
230
231 # collect lines between date && filter
232 if (( ($datetime>$mintime)&&($datetime<$maxtime)) && !($url =~ /$filter/) &&
363fb6af
MT
233 ((($ip eq $sourceip) || $sourceall)) &&
234 ((($so eq $username) || $usersall)))
cd1a2927
MT
235 {
236 # when standart viewing, just keep in memory the correct slices
237 # it starts a '$start' and size is $viewport
238 # If export, then keep all lines...
239 if ($cgiparams{'ACTION'} eq $Lang::tr{'export'}){
363fb6af 240 $log[$lines++] = "$datetime $ip $so $url";
cd1a2927
MT
241 } else {
242 if ($lines++ < ($start + $Header::viewsize)) {
363fb6af 243 push(@log,"$datetime $ip $so $url");
cd1a2927
MT
244 if (@log > $Header::viewsize) {
245 shift (@log);
246 }
247 #} else { dont do this optimisation, need to count lines !
248 # $datetime = $maxtime; # we have read viewsize lines, stop main loop
249 # last READ; # exit read file
250 }
66c36198 251 }
cd1a2927
MT
252 }
253
254 # finish loop when date of lines are past maxtime
255 $loop = ($datetime < $maxtime);
256 }
257 close (FILE);
258 }
363fb6af 259 $gzindex--; # will try next gz file eg 40,39,38,.... because it may have holes when ipfire stopped
cd1a2927
MT
260 # for a long time
261
262 }
263
264 #$errormessage="$errormessage$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
265 if (0) { # print last date record read
266 my ($SECdt, $MINdt, $HOURdt, $DAYdt, $MONTHdt, $YEARdt) = localtime($lastdatetime);
267 $SECdt = sprintf ("%.02d",$SECdt);
268 $MINdt = sprintf ("%.02d",$MINdt);
269 $HOURdt = sprintf ("%.02d",$HOURdt);
270 $DAYdt = sprintf ("%.02d",$DAYdt);
271 $MONTHdt = sprintf ("%.02d",$MONTHdt+1);
272 $YEARdt = sprintf ("%.04d",$YEARdt+1900);
273 &General::log ("$HOURdt:$MINdt:$SECdt, $DAYdt/$MONTHdt/$YEARdt--");
274 }
275}
276
277if ($cgiparams{'ACTION'} eq $Lang::tr{'export'})
278{
279 print "Content-type: text/plain\n\n";
78331e30 280 print "IPFire proxy log\r\n";
66c36198 281 print "$Lang::tr{'date'}: $daystr\r\n";
cd1a2927 282 print "Source IP: $cgiparams{'SOURCE_IP'}\r\n";
363fb6af 283 print "Username: $cgiparams{'USERNAME'}\r\n";
cd1a2927
MT
284 if ($cgiparams{'ENABLE_FILTER'} eq 'on') {
285 print "Ignore filter: $cgiparams{'FILTER'}\r\n"; }
286 print "\r\n";
287
288 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
289
290 foreach $_ (@log) {
363fb6af 291 my ($datetime,$ip,$so,$url) = split;
cd1a2927
MT
292 my ($SECdt, $MINdt, $HOURdt, $DAYdt, $MONTHdt, $YEARdt) = localtime($datetime);
293 $SECdt = sprintf ("%.02d",$SECdt);
294 $MINdt = sprintf ("%.02d",$MINdt);
295 $HOURdt = sprintf ("%.02d",$HOURdt);
296 if ($cgiparams{'DAY'}==0) { # full month
297 $DAYdt = sprintf ("%.02d",$DAYdt);
363fb6af 298 print "$DAYdt/$HOURdt:$MINdt:$SECdt $ip $so $url\n";
cd1a2927 299 } else {
363fb6af 300 print "$HOURdt:$MINdt:$SECdt $ip $so $url\n";
cd1a2927
MT
301 }
302 }
303 exit;
304}
305
306$selected{'SOURCE_IP'}{$cgiparams{'SOURCE_IP'}} = "selected='selected'";
363fb6af 307$selected{'USERNAME'}{$cgiparams{'USERNAME'}} = "selected='selected'";
cd1a2927
MT
308
309$checked{'ENABLE_FILTER'}{'off'} = '';
310$checked{'ENABLE_FILTER'}{'on'} = '';
311$checked{'ENABLE_FILTER'}{$cgiparams{'ENABLE_FILTER'}} = "checked='checked'";
312
313&Header::showhttpheaders();
314
315&Header::openpage($Lang::tr{'proxy log viewer'}, 1, '');
316
317&Header::openbigbox('100%', 'left', '', $errormessage);
318
319if ($errormessage) {
320 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
321 print "<font class='base'>$errormessage&nbsp;</font>\n";
322 &Header::closebox();
323}
324
325&Header::openbox('100%', 'left', "$Lang::tr{'settings'}:");
326
327print <<END
328<form method='post' action='$ENV{'SCRIPT_NAME'}'>
329<table width='100%'>
330<tr>
331 <td width='10%' class='base'>$Lang::tr{'month'}:</td>
332 <td width='20%'>
333 <select name='MONTH'>
334END
335;
336
337for (my $month = 0; $month < 12; $month++)
338{
339 print "\t<option ";
340 if ($month == $cgiparams{'MONTH'}) {
341 print "selected='selected' "; }
342 print "value='$month'>$longmonths[$month]</option>\n";
343}
344print <<END
345 </select>
346 </td>
347 <td width='10%' class='base' align='right'>$Lang::tr{'day'}:&nbsp;</td>
348 <td width='10%'>
349 <select name='DAY'>
350END
351;
352print "<option value='0'>$Lang::tr{'all'}</option>";
66c36198 353for (my $day = 1; $day <= 31; $day++)
cd1a2927
MT
354{
355 print "\t<option ";
356 if ($day == $cgiparams{'DAY'}) {
357 print "selected='selected' "; }
358 print "value='$day'>$day</option>\n";
359}
360print <<END
361 </select>
362 </td>
363 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
364 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
365 <td width='25%' class='base'>$Lang::tr{'source ip'}:</td>
366 <td width='15%'>
367 <select name='SOURCE_IP'>
368 <option value='ALL' $selected{'SOURCE_IP'}{'ALL'}>$Lang::tr{'caps all'}</option>
369END
370;
363fb6af 371foreach my $ip (sort keys %ips) {
cd1a2927
MT
372 print "<option value='$ip' $selected{'SOURCE_IP'}{$ip}>$ip</option>\n"; }
373print <<END
374 </select>
375 </td>
376</tr>
363fb6af
MT
377<tr>
378 <td width='60%' colspan='6'>&nbsp;</td>
379 <td width='25%' class='base'>$Lang::tr{'advproxy NCSA username'}:</td>
380 <td width='15%'>
381 <select name='USERNAME'>
382 <option value='ALL' $selected{'USERNAME'}{'ALL'}>$Lang::tr{'caps all'}</option>
383END
384;
385foreach my $so (sort keys %users) {
5ba9e660 386 $so = &Header::escape($so);
363fb6af
MT
387 print "<option value='$so' $selected{'USERNAME'}{$so}>$so</option>\n"; }
388print <<END
389 </select>
390 </td>
391</tr>
cd1a2927
MT
392<tr>
393 <td class='base'>$Lang::tr{'ignore filter'}:</td>
394 <td colspan='5'><input type='text' name='FILTER' value='$cgiparams{'FILTER'}' size='40' /></td>
395 <td class='base'>$Lang::tr{'enable ignore filter'}:</td>
396 <td><input type='checkbox' name='ENABLE_FILTER' value='on' $checked{'ENABLE_FILTER'}{'on'} /></td>
397</tr>
398</table>
399<div align='center'>
400<table width='50%'>
401<tr>
402 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'restore defaults'}' /></td>
403 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
404 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'export'}' /></td>
405</tr>
406</table>
407</div>
408</form>
409END
410;
411
412&Header::closebox();
413&Header::openbox('100%', 'left', $Lang::tr{'log'});
414
415
416$start = $lines - ${Header::viewsize} if ($start >= $lines - ${Header::viewsize});
417$start = 0 if ($start < 0);
418
419my $prev;
420 if ($start == 0) {
421 $prev = -1;
422 } else {
423 $prev = $start - ${Header::viewsize};
424 $prev = 0 if ( $prev < 0);
425 }
426
427my $next;
428 if ($start == $lines - ${Header::viewsize}) {
429 $next = -1;
430 } else {
431 $next = $start + ${Header::viewsize};
432 $next = $lines - ${Header::viewsize} if ($next >= $lines - ${Header::viewsize});
433 }
434
435if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
436
437print "<p><b>$Lang::tr{'web hits'} $daystr: $lines</b></p>";
438if ($lines != 0) { &oldernewer(); }
439print <<END
fda1f52f 440<table width='100%' class='tbl'>
cd1a2927 441<tr>
fda1f52f
AM
442<th width='10%' align='center' class='boldbase'><b>$Lang::tr{'time'}</b></th>
443<th width='15%' align='center' class='boldbase'><b>$Lang::tr{'source ip'}</b></th>
444<th width='12%' align='center' class='boldbase'><b>$Lang::tr{'advproxy NCSA username'}</b></th>
445<th width='63%' align='center' class='boldbase'><b>$Lang::tr{'website'}</b></th>
cd1a2927
MT
446</tr>
447END
448;
449my $ll = 0;
fda1f52f 450my $col='';
cd1a2927
MT
451foreach $_ (@log)
452{
453 if ($ll % 2) {
fda1f52f
AM
454 print "<tr>";
455 $col="bgcolor='$color{'color20'}'"; }
cd1a2927 456 else {
fda1f52f
AM
457 print "<tr>";
458 $col="bgcolor='$color{'color22'}'"; }
363fb6af 459 my ($datetime,$ip,$so,$url) = split;
cd1a2927
MT
460 my ($SECdt, $MINdt, $HOURdt, $DAYdt, $MONTHdt, $YEARdt) = localtime($datetime);
461 $SECdt = sprintf ("%.02d",$SECdt);
462 $MINdt = sprintf ("%.02d",$MINdt);
463 $HOURdt = sprintf ("%.02d",$HOURdt);
464
465 $url =~ /(^.{0,60})/;
466 my $part = $1;
66c36198 467 unless (length($part) < 60) { $part = "${part}..."; }
cd1a2927
MT
468 $url = &Header::cleanhtml($url,"y");
469 $part = &Header::cleanhtml($part,"y");
470 if ($cgiparams{'DAY'}==0) { # full month
471 $DAYdt = sprintf ("%.02d/",$DAYdt);
472 } else {
473 $DAYdt='';
474 }
475 print <<END
fda1f52f
AM
476 <td align='center' $col>$DAYdt$HOURdt:$MINdt:$SECdt</td>
477 <td align='center' $col>$ip</td>
478 <td align='center' $col>$so</td>
479 <td align='left' $col><a href='$url' title='$url' target='_new'>$part</a></td>
cd1a2927
MT
480</tr>
481END
482 ;
483 $ll++;
484}
485
486print "</table>";
487
488&oldernewer();
489
490&Header::closebox();
491
492&Header::closebigbox();
493
494&Header::closepage();
495
496
497sub oldernewer
498{
499print <<END
500<table width='100%'>
501<tr>
502END
503;
504
505print "<td align='center' width='50%'>";
506if ($prev != -1) {
363fb6af 507 print "<a href='/cgi-bin/logs.cgi/proxylog.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SOURCE_IP'},$cgiparams{'USERNAME'}'>$Lang::tr{'older'}</a>"; }
cd1a2927
MT
508else {
509 print "$Lang::tr{'older'}"; }
510print "</td>\n";
511
512print "<td align='center' width='50%'>";
513if ($next >= 0 ) {
363fb6af 514 print "<a href='/cgi-bin/logs.cgi/proxylog.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SOURCE_IP'},$cgiparams{'USERNAME'}'>$Lang::tr{'newer'}</a>"; }
cd1a2927
MT
515else {
516 print "$Lang::tr{'newer'}"; }
517print "</td>\n";
518
519print <<END
520</tr>
521</table>
522END
523;
524}
525