#!/usr/bin/perl
###########################################################################
#
# Program : Log Analyzer for DansGuardian
# Author : Jimmy Myrick (jmyrick@tiger1.tiger.org)
# Version : .1.1
# Released : July 6, 2002
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# Heck, if you like it though and want to send me something, that's
# ok too.
#
# Change history is available here for now:
# http://www.tiger.org/technology/dg
#
###########################################################################
###########################################################################
#
# Change to point to your DansGuardian log directory
# NOTE: The trailing / IS REQUIRED!!
#
###########################################################################
$logdir = '/var/log/dansguardian/';
###########################################################################
#
# Log filename. Change this to match the prefix of your log files
# This defaults to access.log and should not have to be modified.
#
# Any logfiles in $logdir that match the prefix $logfile and are gzip'ed
# with a .gz extension will also be read. The results will be printed in
# reverse chronological filename order.
#
# Example:
# If you have the files: access.log access.log.0.gz access.log.1.gz
# where they are newest to oldest, then any matches in
# access.log.1.gz will be printed first, followed by access.log.0.gz
# and then access.log
#
# No sorting is done by the program and the results are displayed in logfile
# order. If your results are out of sequence, check the filename/dates
# to be sure they are compressed and rotated properly. If you use
# the FreeBSD newsyslog.conf to rotate your logs, this will not be a
# problem.
#
###########################################################################
$logfile = 'access.log';
###########################################################################
#
# If you need the perl modules below, download and untar them to a directory.
# Then cd to the directory and enter the commands:
# perl Makefile.PL; make; make test; make install
#
# If you need more instructions,
# go here: http://www.cpan.org/modules/INSTALL.html
#
# Get it here: http://www.cpan.org/authors/id/LDS/CGI.pm-2.81.tar.gz
#
###########################################################################
use CGI;
###########################################################################
#
# This is needed to do gzip'ed log files on the fly
# Get it here: http://www.cpan.org/authors/id/PMQS/Compress-Zlib-1.16.tar.gz
#
###########################################################################
use Compress::Zlib;
###########################################################################
#
# This should determine where the program is called from automagically.
# If not, uncomment the first lane, change to your server name/path and
# comment the second line. You can use Apache restrictions to block
# access to this file if desired.
#
###########################################################################
#$cgipath = 'http://your.server.com/cgi-bin/dglog/dglog.pl';
$cgipath = $ENV{SCRIPT_NAME};
###########################################################################
#
# SHOULDN'T HAVE TO MODIFY ANYTHING BELOW THIS LINE
#
###########################################################################
$q = new CGI;
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$mon = $mon + 1; # mon starts at 0
$year = $year + 1900; # year needs 1900 added
$pagename = 'Log Analyzer for DansGuardian';
$a = $q->param('a');
if ($a eq 'i') { # Inquiry into logs
# These are the values that can be sent by the user through the browser
$sIP = "ALL"; # IP address
$sUN = "ALL"; # Username
$sURL = "ALL"; # URL to show or trace a denied site - this is the URL to trace
$sSD = "ALL"; # Complete start date
$sSDY = "ALL"; # Start date year
$sSDM = "ALL"; # Start date month
$sSDD = "ALL"; # Start date day
$sED = "ALL"; # Complete end date
$sEDY = "ALL"; # End date year
$sEDM = "ALL"; # End date month
$sEDD = "ALL"; # End date day
$sA = "ALL"; # Action
$sSumCnt = "20"; # Number of summary sites to show
$sSumDen = "off"; # Show denied summary? on/off
$sSumAlw = "off"; # Show allowed summary? on/off
$sL = "off"; # Turn URL's into links? on/off
$sZ = "off"; # Examine gziped files? on/off
$sIP = &validateIP($q->param('sIP')) if $q->param('sIP') ne "";
$sUN = $q->param('sUN') if $q->param('sUN') ne "";
$sURL = $q->param('sURL') if $q->param('sURL') ne "";
if ($q->param('sSDY') ne "" &&
$q->param('sSDM') ne "" &&
$q->param('sSDD') ne "" &&
$q->param('sEDY') ne "" &&
$q->param('sEDM') ne "" &&
$q->param('sEDD') ne "") {
$sSDY = $q->param('sSDY');
$sSDM = $q->param('sSDM');
$sSDD = $q->param('sSDD');
$sEDY = $q->param('sEDY');
$sEDM = $q->param('sEDM');
$sEDD = $q->param('sEDD');
$sSD = $sSDY.'.'.$sSDM.'.'.$sSDD;
$sSD = convertDate($sSD);
$sED = $sEDY.'.'.$sEDM.'.'.$sEDD;
$sED = convertDate($sED);
if ($sSD > $sED) {
$msg = "End Date is greather than Start Date";
&printMenu;
}
}
$sA = &validateAction($q->param('sA')) if $q->param('sA') ne ""; # Action
$sSumCnt = &validateSummary($q->param('sSumCnt'))
if $q->param('sSumCnt') ne "";
$sSumDen = $q->param('sSumDen') if $q->param('sSumDen') eq 'on';
$sSumAlw = $q->param('sSumAlw') if $q->param('sSumAlw') eq 'on';
$sL = $q->param('sL') if $q->param('sL') eq 'on';
$sZ = $q->param('sZ') if $q->param('sZ') eq 'on';
# Need a few global variables to keep from passing back and forth a bunch
$linesRead, $allowTotal, $blockTotal, $grandTotal = 0;
&searchLog;
}
elsif ($a eq 'h') {
&displayHelp;
}
else {
&printMenu;
}
#############
sub searchLog
#############
{
my $first = 0;
&printHeader;
print "";
print "Report information for:
Start Date: $sSD | End Date: $sED |
Username : $sUN | IP: $sIP |
Action: $sA | URL: $sURL\n";
print "";
opendir(D, $logdir);
@files = grep {/^$logfile/} readdir(D);
@files = sort {$b cmp $a} @files;
closedir(D);
foreach $file (@files) {
if ($file =~ /\.gz/) {
if ($sZ eq 'on') {
if ($first == 0) {
print "Ignoring gzip logfile(s) in $logdir: ";
$first = 1;
}
print "$file | ";
next;
}
$gz = gzopen($logdir.$file,r);
if (!$gz) {
$msg = "Cannot open $logdir$file. Check Permissions.";
&printMenu;
}
while ($gz->gzreadline($line)) {
&checkLine($line);
}
$gz->gzclose;
}
else {
print "
Total $whatToShow Requests (only top $topNum sites shown) : $subTotal
";
print "
";
}
###################
sub validateSummary
###################
{
my ($count) = @_;
if ($count < 0 || $count > 100) {
$count = 20;
}
return($count);
}
##############
sub validateIP
##############
{
my ($checkIP) = @_;
if ($checkIP eq 'ALL') {
return('ALL');
}
elsif ($checkIP =~ /^((2([0-4]\d|5[0-5])|1?\d{1,2})(\.|$)){4}/) {
return ($checkIP);
}
else {
$msg = "Invalid IP address entered.";
&printMenu;
}
}
##################
sub validateAction {
##################
my ($action) = @_;
# Need to make the actions a hash and reference them that way
# Make it easier to add/modify and can validate that way too
# Maybe later.
if ($action eq "none") { return ("ALL"); }
return ($action);
}
###############
sub convertDate {
###############
my ($workDate) = @_;
($year, $mon, $day) = split(/\./,$workDate);
if (length($mon) == 1) { $mon = '0'.$mon; }
if (length($day) == 1) { $day = '0'.$day; }
if (($mon ge "01" && $mon le "12") && ($day ge "01" && $day le "31") &&
($year ge "2000" && $year le "2035")) {
$goodDate = $year.$mon.$day;
return ($goodDate);
} else {
$msg = "Invalid Date Detected";
&printMenu;
}
}
###############
sub buildSelect
###############
{
my ($start, $end, $type) = @_;
my $x = 0;
## print "