#!/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"));
$type = clean_input($query->param("type")); 
$clone = clean_input($query->param("clone"));
$type="traffic";
#
# HTML SECTION
#
printtitle();

$getAllFilters= "SELECT parse_name, parse_add_time, parse_update_time, log_protocol, log_src_ip, log_src_pt, log_dst_ip, log_dst_pt, log_action, log_resource, parse_description FROM parse_filter where parse_id like \"$id\"";
$statement2 = $db_handle->prepare($getAllFilters) or die "Couldn't prepare query '$getAllFilters': $DBI::errstr\n";
$statement2->execute();

while (($filter_name, $filter_add_time, $filter_update_time, $log_protocol, $log_src_ip, $log_src_pt, $log_dst_ip, $log_dst_pt, $log_action, $log_resource2, $filter_description) = $statement2->fetchrow) {

if ($log_src_ip eq "any") {
    $log_src_ip="";
} 

if ($log_src_pt eq "any") {
    $log_src_pt="";
}

if ($log_dst_ip eq "any") {
    $log_dst_ip="";
}

if ($log_dst_pt eq "any") {
    $log_dst_pt="";
}

if ($log_resource2 eq "any") {
     $log_resource2="";
}

if ($log_action eq "any") {
    $log_action="any";
}

$filter_description=~s/\<a href\=\"(.*)\" class\=\"bodylink\"\>(.*)\<\/a\>/\<url\>$1\<\/url\>/g;
$filter_description=~s/\\'/'/g;
$filter_description=~s/\\"/"/g;


if ($clone eq "1") {
    $filter_name = "CLONED: $filter_name";
    $filter_description = "CLONED: $filter_description";
}

print <<EOF;
<span class='titlehead'><b>PLA Configuration > Edit Parse Filter</b></span>
EOF

if ($clone eq "1") {
print <<EOF;
<br><br><br>
<form action="./pix_config_parse_filter_process" method="get" name="filter">
EOF
} else {
print <<EOF;
<br><br><br>
<form action="./pix_config_parse_filter_edit_process" method="get" name="filter">
EOF
}

print <<EOF;
<input type="hidden" name="id" value="$id">
 <table width="500" cellpadding="0" cellspacing="0" border="0">
    <td bgcolor="#5479d8"><span class="button"> >> General Criteria</span</td>
    </table>
    <table width="500" cellpadding="0" cellspacing="0" border="0" bgcolor="#d9d9d9">
    <td><br>
    <input type="hidden" name="type" value="$type">
    <span class="main">Filter Name:</span> <input type="text" name="filter_name" size="60" width="50" value="$filter_name"><span class="main"><br>
    Logging Resource:
    <select name="log_resource">
EOF
if ($log_resource2 eq "") {
print <<EOF;
    <option value="" selected>All Firewalls
    <option value="">
EOF
} else {
print <<EOF;
    <option value="">All Firewalls
    <option value="">
EOF
}

$getAllResources="SELECT log_resource from traffic_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) {
if ($log_resource2 eq $log_resource) {
print <<EOF;
<option value="$log_resource" selected>$log_resource
EOF
} else {
print <<EOF;
<option value="$log_resource">$log_resource
EOF
}

}

print <<EOF;
</select><br>
EOF

if ($type == "traffic") {
print <<EOF;
Logging Action:
<select name="log_action">
EOF

if ($log_action eq "any") {

print <<EOF;

<option value="any" selected>Any
<option>
<option value="ACCEPT">Accept
<option value="DROP">Drop
&nbsp;&nbsp;

EOF

}

if ($log_action eq "ACCEPT") {

print <<EOF;

<option value="any">Any
<option>
<option value="ACCEPT" selected>Accept
<option value="DROP">Drop
&nbsp;&nbsp;

EOF

}

if ($log_action eq "DROP") {

print <<EOF;
<option value="any">Any
<option>
<option value="ACCEPT">Accept
<option value="DROP" selected>Drop
&nbsp;&nbsp;
EOF
}

print <<EOF;
</select>
EOF

}

print <<EOF;
Protocol:
<select name="log_protocol">
EOF

if ($log_protocol eq "TCP") {
print <<EOF;
<option selected value="TCP">TCP
<option value="UDP">UDP
<option value="ICMP">ICMP
EOF
}

if ($log_protocol eq "UDP") {
print <<EOF;
<option value="TCP">TCP
<option selected value="UDP">UDP
<option value="ICMP">ICMP
EOF
}

if ($log_protocol eq "ICMP") {
print <<EOF;
<option value="TCP">TCP
<option value="UDP">UDP
<option selected alue="ICMP">ICMP
EOF
}


if ($type eq "ids") {
if ($log_protocol eq "IP") {
print <<EOF;
<option selected value="IP">IP
EOF
} else {
print <<EOF;
<option value="IP">IP
EOF
}
}

print <<EOF;
</select>
    </td>
    </table>
    <table width="500" cellpadding="0" cellspacing="0" border="0">
    <td bgcolor="#5479d8"><span class="button"> >> TCP/IP Criteria</span</td>
    </table>
    <table width="500" cellpadding="0" cellspacing="0" border="0" bgcolor="#d9d9d9">
    <td><br>
    <span class="main">Src IP:</span> <input type="text" name="src_ip" border="0" value="$log_src_ip"></input>
EOF
    if ($type eq "traffic") {
print <<EOF;
    <span class="main">Src Port:</span> <input type="text" name="src_pt" size="5" value="$log_src_pt"></input>
EOF
    }
print <<EOF;
    <br>
    <span class="main">Dst IP:</span> <input type="text" name="dst_ip" border="0" value="$log_dst_ip"></input>
EOF
    if ($type eq "traffic") {
print <<EOF;
    <span class="main">Dst Port:</span> <input type="text" name="dst_pt" size="5" value="$log_dst_pt"></input>
EOF
    }
print <<EOF;
    <br><br>
    </td></table>
  <table width="500" cellpadding="0" cellspacing="0" border="0">
    <td bgcolor="#5479d8"><span class="button"> >> Filter Criteria</span</td>
    </table>
    <table width="500" cellpadding="0" cellspacing="0" border="0" bgcolor="#d9d9d9">
    <td><br>
<span class="main">Filter Description:</span><br>
<textarea name="filter_description" rows="5" cols="60" scrollable="no" multiline="true">$filter_description</textarea><span class="main">
<br><br>
</td>
<tr>
<td bgcolor="#000000" align="left" valign="top" height="3"></td>
<tr>
<td bgcolor="#a7a7a7" align="left" valign="top">
EOF
if ($clone eq "1") {
print <<EOF;
<input type="image" src="images/pla_create.gif" border="0" value="Create">
EOF
} else {
print <<EOF;
<input type="image" src="images/pla_update.gif" border="0" value="Edit">
EOF
}
print <<EOF;
<a href="javascript:window.document.filter.reset();"><img src="images/pla_reset.gif" border="0"></a>
</form>
</td></table>
EOF
}
$db_handle->disconnect();

