#!/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 PIX Logging Architecture; 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;
$id = clean_input($query->param("id"));
$remove_filter = clean_input($query->param("remove_filter"));
$enable_filter = clean_input($query->param("enable_filter"));
$disable_filter = clean_input($query->param("disable_filter"));
$datetime = clean_input($query->param("date"));
$showtype = clean_input($query->param("showtype"));
$searchtype = clean_input($query->param("search_type"));
$searchstring = clean_input($query->param("search_string"));
$showoptions = clean_input($query->param("showoptions"));


if ($searchtype eq "filter_id" && $searchstring eq "") {
    $searchstring="\%";
}

if (length($searchtype) < 1) {
   $search_sql = "";
} else {
   $search_sql = "WHERE $searchtype like \"$searchstring\"";
}

($date,$time) = split(" ",$datetime);


if ($remove_filter eq "true") {
    $removeFilterAll=$db_handle->prepare("delete from log_filter where filter_id=\"$id\"");
    $removeFilterAll->execute();
}

if ($enable_filter eq "true") {
    $activateFilterAll=$db_handle->prepare("update log_filter set filter_state=\"active\" where filter_id=\"$id\"");
    $activateFilterAll->execute();
}

if ($disable_filter eq "true") {
    $deactivateFilterAll=$db_handle->prepare("update log_filter set filter_state=\"inactive\" where filter_id=\"$id\"");
    $deactivateFilterAll->execute();
}

if ((length($showtype) < 1)) {
    $showtype="";
}

if (length($showoptions) < 1) {
    $showoptions="0";
}
                                                                                                                                                                                                  
#
# HTML SECTION
#
printtitle();
                                                                                                                                                                                                  
print <<EOF;
<script language="JavaScript">
<!-- Original script borrowed from http://web.uvic.ca/atrsweb/vikes/default.html -->
<!-- Implemented at Kwantlen 26 June 2000 DMH -->
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
//-->
</script>
<span class='titlehead'><b>PLA Configuration > Traffic Logs > Filtering</b></span>
<br><br>
<span class="main">
EOF


if ($showoptions eq "0") {
print <<EOF;
<a href="$pix_config_traffic_log?showoptions=1"><img src="images/pla_plus.gif" align="top"></a> <img src="images/config_filter.gif" align="top" valign="top"></img> <b>Traffic Filter Options</b> <i><font color="red">(Expand to create new filter)</font></i><br><br>
EOF
}
                                                                                                                                                                                                     
                                                                                                                                                                                                     
                                                                                                                                                                         
if ($showoptions eq "1") {

print <<EOF;
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<td align="left" valign="top">
<span class="main">
<a href="$pix_config_traffic_log?showoptions=0"><img src="images/pla_minus.gif" align="top"></a> <img src="images/config_filter.gif" align="top" valign="top"></img> <b>Traffic Filter Options</b>

</td>
<tr>
<td height="2" bgcolor="#000000"></td>
<td height="2" bgcolor="#000000"></td>
<td height="2" bgcolor="#000000"></td>
<tr>
<td height="2" bgcolor="#ffffff"></td>
<td height="2" bgcolor="#ffffff"></td>
<td height="2" bgcolor="#ffffff"></td>
<tr>
<td align="left" valign="top">
<span class="main">
<img src="images/pla_blank.gif" align="top"> <a href="pix_config_filter_create" class="bodylink"><img src="images/pla_create.gif" border="0" alt="Create Traffic Filter"></img></a>
</td>

<td align="middle" valign="top">
<span class="main">
<form action="pix_config_traffic_filter" name="" method="post">
<input type="hidden" name="showoptions" value="1">
Search Filters: 

<select name="search_type">
<option selected value="filter_id">id
<option value="filter_name">filter name
<option value="filter_description">filter description
<option value="log_src_ip">source IP
<option value="log_src_pt">source port
<option value="log_dst_ip">destination IP
<option value="log_dst_pt">destination port
<option value="log_protocol">protocol
<option value="log_action">action
<option value="log_resource">logging resource
</select>
 = 
<input type="text" name="search_string"></input>
<input type="image" value="submit" src="images/pla_search_small.gif" align="top" border="0">
</form>

</td>



<td align="right" valign="top">
<span class="main">
<form action="pix_config_traffic_filter" method="post" align="left" valign="top">
Show:
<select name="showtype" onChange="MM_jumpMenu('parent',this,0)">
<option select value="?showoptions=$showoptions">Select ... 
<option>
<option value="?showtype=&showoptions=$showoptions">All Filters
<option value="?showtype=enabled&showoptions=$showoptions">Only Enabled Filters
<option value="?showtype=disabled&showoptions=$showoptions">Only Disabled Filters
</select>
</td>
<tr>
<td height="2" bgcolor="#ffffff"></td>
<td height="2" bgcolor="#ffffff"></td>
<td height="2" bgcolor="#ffffff"></td>
<tr>
<td height="2" bgcolor="#000000"></td>
<td height="2" bgcolor="#000000"></td>
<td height="2" bgcolor="#000000"></td>
</table>
<br>
EOF

}

if ($showtype eq "") {
$getAllEvents = "SELECT filter_id, filter_add_time, filter_name, log_resource, log_protocol, filter_state FROM log_filter $search_sql order by filter_name";
} 

if ($showtype eq "enabled") {
$getAllEvents = "SELECT filter_id, filter_add_time, filter_name, log_resource, log_protocol, filter_state FROM log_filter where filter_state=\"active\" order by filter_name";
}

if ($showtype eq "disabled") {
$getAllEvents = "SELECT filter_id, filter_add_time, filter_name, log_resource, log_protocol, filter_state FROM log_filter where filter_state=\"inactive\" order by filter_name";
}


$statement2 = $db_handle->prepare($getAllEvents) or die "Couldn't prepare query '$getAllEvents': $DBI::errstr\n";
$statement2->execute();
print <<EOF;
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<td width="300" bgcolor="#5479d8"><span class="button">Filter Name</td>
<td width="80" bgcolor="#5479d8"><span class="button">Logging Resource</td>
<td width="110" bgcolor="#5479d8"><span class="button">Entry Time</td>
<td width="180" bgcolor="#5479d8"><span class="button">Options</td>
EOF
while (($filter_id, $filter_add_time, $filter_name, $log_resource, $log_protocol, $filter_state) = $statement2->fetchrow) {
if ($log_resource eq "\%") {
    $log_resource="any";
}

print <<EOF;
<tr OnMouseOver="this.bgColor='#d9d9d9'" OnMouseOut="this.bgColor='#ffffff'">
<td width="300"><a href="pix_config_filter_view?id=$filter_id" class="bodylink">$filter_name</a></td>
<td width="80"><span class="main">$log_resource</td>
<td width="110"><span class="main">$filter_add_time</td>
EOF


if ($filter_state eq "active") {
 
print <<EOF;
<td width="180"><a href="pix_config_filter_edit?id=$filter_id&type=traffic&clone=1" class="bodylink">[ clone ]</span></a> <a href="pix_config_filter_edit?id=$filter_id&type=traffic" class="bodylink">[ edit ]</span></a> <a href="pix_config_traffic_filter?remove_filter=true&id=$filter_id" class="bodylink">[ remove ]</span></a>
<a href="pix_config_traffic_filter?disable_filter=true&id=$filter_id" class="bodylink">[ disable ]</span></a></td>
EOF

} 

if ($filter_state eq "inactive") {

print <<EOF;
<td width="130"><a href="pix_config_filter_edit?id=$filter_id&type=traffic&clone=1" class="bodylink">[ clone ]</span></a> <a href="pix_config_filter_edit?id=$filter_id&type=traffic" class="bodylink">[ edit ]</span></a> <a href="pix_config_traffic_filter?remove_filter=true&id=$filter_id" class="bodylink">[ remove ]</span></a>
<a href="pix_config_traffic_filter?enable_filter=true&id=$filter_id" class="bodylink">[ enable ]</span></a></td>
EOF

}

print <<EOF;
<tr>
EOF
}

print <<EOF;
</table>
EOF
$db_handle->disconnect();

