#!/usr/bin/perl
#
# PIX Logging Architecture
# [ Kristof Philipsen ]
#
# This file is part of PIX Logging Architecture
#
# PIX Logging Architecture 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.
#
# PIX Logging Architecture 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.
#
# You should have received a copy of the GNU General Public License
# along with Foobar; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

                                                                                                                                                                                                  
use DBI;
use CGI;
                                                                                                                                                                                                  
#
# include configuration
#
require "conf.pl";      # General Configuration
require "subs.pl";      # Subroutines
                                                                                                                                                                                                  
#
# Make Database Connection
#
db_connect();
                                                                                                                                                                                                  
#
# Get CGI Parameters
#
$query = new CGI;
$year = $query->param("year");
$month = $query->param("month");
$day = $query->param("day");
$date="$year-$month-$day";
$firewall=$query->param("firewall");
if (($year == "") || ($month=="") || ($date=="")) {
    $date=`date +%Y"-"%m"-"%d`;
    $date=~s/\n//g;
}
#$date=`date +%Y"-"%m"-"%d`;
#$date=~s/\n//g;
$datetime=`date`;

#
# DB Parameters
#
$sql = "SELECT count(*) FROM ids_log";
$statement = $db_handle->prepare($sql)
    or die "Couldn't prepare query '$sql': $DBI::errstr\n";

#
# HTML SECTION
#
printtitle();
                                                                                                                                                                                                  
$statement->execute()
    or die "Couldn't execute query '$sql': $DBI::errstr\n";
while ($row_ref = $statement->fetchrow)
{
    print "<span class='titlehead'><b>PIX IDS Logs > $date</b></span> &nbsp;&nbsp;&nbsp;&nbsp; <span class='titlehead'>Database last queried: <b>$datetime</b> &nbsp;&nbsp;|&nbsp;&nbsp; Number of entries in ids_log_db: <b>$row_ref</b>.</span><br><br>\n";
}
if (length($firewall) >= "1") {
$getdate = "SELECT log_id, log_time, log_protocol, log_src_ip, log_dst_ip, log_signature FROM ids_log WHERE log_time LIKE \"$date\%\" and log_resource=\"$firewall\"";
} else {
$getdate = "SELECT log_id, log_time, log_protocol, log_src_ip, log_dst_ip, log_signature FROM ids_log WHERE log_time LIKE \"$date\%\"";
}
$statement2 = $db_handle->prepare($getdate) or die "Couldn't prepare query '$gettoday': $DBI::errstr\n";
print <<EOF;
<form action="/log-fws/external/pix/pix_ids_logs" method="post">
<select name="year">
<option selected>-year-
<option>
<option value="2000">2000
<option value="2001">2001
<option value="2002">2002
<option value="2003">2003
<option value="2004">2004
<option value="2005">2005
<option value="2006">2006
<option value="2007">2007
<option value="2008">2008
<option value="2009">2009
<option value="2010">2010
</select>
<select name="month">
<option selected>-month-
<option>
<option value="01">Jan
<option value="02">Feb
<option value="03">Mar
<option value="04">Apr
<option value="05">May
<option value="06">Jun
<option value="07">Jul
<option value="08">Aug
<option value="09">Sep
<option value="10">Oct
<option value="11">Nov
<option value="12">Dec
</select>
<select name="day">
<option selected>-day-
<option>
<option value="01">01
<option value="02">02
<option value="03">03
<option value="04">04
<option value="05">05
<option value="06">06
<option value="07">07
<option value="08">08
<option value="09">09
<option value="10">10
<option value="11">11
<option value="12">12
<option value="13">13
<option value="14">14
<option value="15">15
<option value="16">16
<option value="17">17
<option value="18">18
<option value="19">19
<option value="20">20
<option value="21">21
<option value="22">22
<option value="23">23
<option value="24">24
<option value="25">25
<option value="26">26
<option value="27">27
<option value="28">28
<option value="29">29
<option value="30">30
<option value="31">31
</select>
<select name="firewall">
<option selected value="">All Firewalls
<option value="">
EOF
$getAllResources="SELECT log_resource from ids_log group by log_resource";
$AllResources = $db_handle->prepare($getAllResources) or die "Couldn't prepare query '$getAllResources': $DBI::errstr\n";
$AllResources->execute();
while ($log_resource = $AllResources->fetchrow) {
print <<EOF;
<option value="$log_resource">$log_resource
EOF
}
print <<EOF;
</select>
<INPUT type="submit" value="GO">
</form>
<table width="80%" cellpadding="0" cellspacing="0" border="0">
<td bgcolor="#5479d8"><b><span class="button">Log ID</span></b></td>
<td bgcolor="#5479d8"><b><span class="button">Time</span></b></td>
<td bgcolor="#5479d8"><b><span class="button">Protocol</b></span></td>
<td bgcolor="#5479d8"><b><span class="button">SRC</b></font></td>
<td bgcolor="#5479d8"><b><span class="button">DST</b></font></td>
<td bgcolor="#5479d8"><b><span class="button">IDS Signature</b></font></td>
<tr>
EOF
$statement2->execute();
while (($log_id,$log_time,$log_protocol,$log_src_ip,$log_dst_ip,$log_signature) = $statement2->fetchrow) {
if ($log_protocol eq "Large") {
    $log_protocol="ICMP";
}
print <<EOF;
<td><a href="pix_ids_id?id=$log_id&date=$log_time"><span class="table">$log_id</span></td>
<td><span class="table">$log_time</span></td>
<td><span class="table">$log_protocol</span></font></td>
<td><span class="table">$log_src_ip</span></td>
<td><span class="table">$log_dst_ip</span></td>
<td><span class="table">$log_signature</span></a></td>
<tr>
EOF
}
print <<EOF;
</table>
EOF
$db_handle->disconnect();

