#!/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;
$query_name = clean_input($query->param("query_name"));
$id = clean_input($query->param("id"));
$log_resource = clean_input($query->param("log_resource"));
$log_message = clean_input($query->param("log_message"));
$log_action = clean_input($query->param("log_action"));
$log_protocol = clean_input($query->param("log_protocol"));
$log_src_ip = clean_input($query->param("src_ip"));
$log_src_pt = clean_input($query->param("src_pt"));
$log_dst_ip = clean_input($query->param("dst_ip"));
$log_dst_pt = clean_input($query->param("dst_pt"));
$type = clean_input($query->param("type"));
$query_detail = clean_input($query->param("query_detail"));
$date=`date +%Y"-"%m"-"%d" "%H":"%M":"%S`;
$date=~s/\n//g;

if ($log_action eq "any") {
    $log_action="\%";
}
 
if (length($log_resource) < "1") {
    $log_resource="\%";
}

if (length($log_src_ip) < "1") {
    $log_src_ip="\%";
}

if (length($log_src_pt) < "1") {
    $log_src_pt="\%";
}

if (length($log_dst_ip) < "1") {
    $log_dst_ip="\%";
}

if (length($log_dst_pt) < "1") {
    $log_dst_pt="\%";
}

if (length($log_message) < "1") {
    $log_message="\%";
}

if (length($log_protocol) < "1") {
    $log_protocol="\%";
}

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



#
# DB Query Parameters
#
 
$addQuery_query=$db_handle->prepare("update userdef_query set userdef_update_time=\'$date\', log_action=\'$log_action\', log_type=\'$type\', log_src_ip=\'$log_src_ip\', log_src_pt=\'$log_src_pt\', log_dst_ip=\'$log_dst_ip\', log_dst_pt=\'$log_dst_pt\', log_protocol=\'$log_protocol\', log_message=\'$log_message\', log_resource=\'$log_resource\', userdef_name=\'$query_name\', userdef_detail=\'$query_detail\' where userdef_id like \'$id\'");
$rc=$addQuery_query->execute();

#
# HTML SECTION
#
printtitle();
                                                                                                                                                                                    
               
print <<EOF;
<span class='titlehead'><b>PLA Configuration > Log Query Details</b></span>
<br><br>
<span class="main"><font color="green"><b><u>SUCCESS:</u></b> TRAFFIC QUERY HAS BEEN UPDATED</font></span>
<br><br>
EOF
$viewQuery = "SELECT userdef_name, userdef_entry_time, userdef_update_time, log_protocol, log_src_ip, log_src_pt, log_dst_ip, log_dst_pt, log_action, log_resource, log_message, userdef_detail FROM userdef_query where userdef_id like \"$id\"";
$statement2 = $db_handle->prepare($viewQuery) or die "Couldn't prepare query '$viewQuery': $DBI::errstr\n";
$statement2->execute();
while (($userdef_name, $userdef_entry_time, $userdef_update_time, $log_protocol, $log_src_ip, $log_src_pt, $log_dst_ip, $log_dst_pt, $log_action, $log_resource, $log_message, $userdef_detail) = $statement2->fetchrow) {
if ($log_src_ip eq "\%") {
     $log_src_ip="any";
}
if ($log_src_pt eq "\%") {
     $log_src_pt="any";
}
if ($log_dst_pt eq "\%") {
     $log_dst_pt="any";
}
if ($log_dst_ip eq "\%") {
     $log_dst_ip ="any";
}
if ($log_resource eq "\%") {
     $log_resource="any";
}
if ($log_resource ne "\%") {
     $log_resource="<img src='images/pix_device.png' align='top'></img> $log_resource";
}
if ($log_action eq "\%") {
    $log_action="any";
}
if ($log_message eq "\%") {
    $log_message="any";
}
if ($log_action eq "ACCEPT") {
    $log_action="<img src='images/accept.png' align='top'></img> $log_action";
}
if ($log_action eq "DROP") {
    $log_action="<img src='images/drop.png' align='top'></img> $log_action";
}
if ($log_protocol eq "TCP") {
    $log_protocol="<img src='images/tcp_service.png' align='top'></img> $log_protocol";
}
if ($log_protocol eq "UDP") {
    $log_protocol="<img src='images/udp_service.png' align='top'></img> $log_protocol";
}
if ($log_protocol eq "ICMP") {
    $log_protocol="<img src='images/icmp_service.png' align='top'></img> $log_protocol";
}
if ($log_protocol eq "\%") {
    $log_protocol="any";
}
if (length($log_protocol) < "1") {
    $log_protocol="any";
}

print <<EOF;
<table width="600" cellpadding="0" cellspacing="0">
<td width="200" height="2" bgcolor="#000000"></td>
<td width="400" height="2" bgcolor="#000000"></td>
<tr>
<td width="200" bgcolor="#d9d9d9"><span class="main"><b>Traffic Query Config</b></span></td>
<td width="400" bgcolor="#d9d9d9"><br></td>
<tr>
<td width="200" height="2" bgcolor="#000000"></td>
<td width="400" height="2" bgcolor="#000000"></td>
<tr>
<td width="200"><br></td>
<td width="400"><br></td>
<tr>
<td width="200" bgcolor="#5479d8"><span class="button">General Details<span></td>
<td width="400"><br></td>
<tr>
<td width="200"><span class="main">Query Name:</td>
<td width="400"><span class="main"><b>$userdef_name</b></td>
<tr>
<td width="200"><span class="main">Query Entry Time:</td>
<td width="400"><span class="main"><i>$userdef_entry_time</i></td>
<tr>
<td width="200"><span class="main">Query Update Time:</td>
<td width="400"><span class="main"><i>$userdef_update_time</i></td>
<tr>
<td width="200"><br></td>
<td width="400"><br></td>
<tr>
<td width="200" bgcolor="#5479d8"><span class="button">Specific Details<span></td>
<td width="400"><br></td>
<tr>
<td width="200"><span class="main">Source IP/Source Port:</td>
<td width="400"><span class="main">$log_src_ip/$log_src_pt</td>
<tr>
<td width="200"><span class="main">Dest. IP/Dest. Port:</td>
<td width="400"><span class="main">$log_dst_ip/$log_dst_pt</td>
<tr>
<td width="200"><span class="main">Protocol:</td>
<td width="400"><span class="main">$log_protocol</td>
<tr>
<td width="200"><span class="main">Action:</td>
<td width="400"><span class="main">$log_action</td>
<tr>
<td width="200"><span class="main">Logging Resource:</td>
<td width="400"><span class="main">$log_resource</td>
<tr>
<td width="200"><span class="main">Log Message:</td>
<td width="400"><span class="main">$log_message</td>
<tr>
<td width="200"><br></td>
<td width="400"><br></td>
<tr>
<td width="200" bgcolor="#5479d8"><span class="button">Query Details<span></td>
<td width="400"><br></td>
<tr>
<td width="200" align="left" valign="top"><span class="main">Query Details:</td>
<td width="400"><span class="main">$userdef_detail</td>
<tr>
<td width="200"><br></td>
<td width="400"><br></td>
<tr>
<td width="200"><br></td>
<td width="400"><br></td>
<tr>
<td width="200" height="2" bgcolor="#000000"></td>
<td width="400" height="2" bgcolor="#000000"></td>
<tr>
<td width="200" bgcolor="#d9d9d9"><span class="main"><b>Traffic Query Options</b></span></td>
<td width="400" bgcolor="#d9d9d9"><br></td>
<tr>
<td width="200" height="2" bgcolor="#000000"></td>
<td width="400" height="2" bgcolor="#000000"></td>
<tr>
<td width="200"><br></td>
<td width="400"><br></td>
<tr>
<td width="200" bgcolor="#5479d8"><span class="button">Available Options<span></td>
<td width="400"><br></td>
<tr>
<td width="200" align="left" valign="top"><span class="main"><a href="pix_config_query_edit?id=$id&type=traffic&clone=1" class="bodylink">Clone Traffic Query</span></a></td>
<td width="400"><span class="main">Traffic Query can be cloned to create a new/similar traffic description.</td>
<tr>
<td width="200" align="left" valign="top"><span class="main"><a href="pix_config_query_edit?id=$id" class="bodylink">Edit Traffic Query</span></a></td>
<td width="400"><span class="main">Traffic Query can be edited to modify its parameters.</td>
<tr>
</table>
<br><br>
<b>>></b> <a href="pix_config_traffic_query" class="bodylink">Back to Traffic Query Configuration</a>
<br><br>
EOF

EOF
}
$db_handle->disconnect();

