#!/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_query = clean_input($query->param("remove_query"));
$searchtype = clean_input($query->param("search_type"));
$searchstring = clean_input($query->param("search_string"));
$showoptions = clean_input($query->param("showoptions"));


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

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

$datetime = $query->param("date");
($date,$time) = split(" ",$datetime);
if ($remove_query eq "true") {
    $removeQueryAll=$db_handle->prepare("delete from userdef_query where userdef_id=\"$id\"");
    $removeQueryAll->execute();
}


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


                                                                                                                                                                                                  
#
# HTML SECTION
#
printtitle();
                                                                                                                                                                                                  
print <<EOF;
<span class='titlehead'><b>PLA Configuration > Traffic Logs > Queries</b></span>
<br><br>
EOF

if ($showoptions eq "0") {
print <<EOF;
<span class="main">
<a href="$pix_config_traffic_query?showoptions=1"><img src="images/pla_plus.gif" align="top"></a> <img src="images/config_query.gif" align="top" valign="top"></img> <b>Query Options</b> <i><font color="red">(Expand to create new query)</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_query?showoptions=0"><img src="images/pla_minus.gif" align="top"></a> <img src="images/config_query.gif" align="top" valign="top"></img> <b>Query 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_query_create" class="bodylink"><img src="images/pla_create.gif" border="0" alt="Create Traffic Query"></img></a>
</td>

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

<select name="search_type">
<option selected value="userdef_id">id
<option value="userdef_name">query name
<option value="userdef_detail">query detail
<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" width="25%">
<br>
</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

}


print <<EOF;
<span class="main">
EOF
$getAllQueries = "SELECT userdef_id, userdef_entry_time, userdef_name, log_resource FROM userdef_query where log_type like \'traffic\' $search_sql order by userdef_name";
$statement2 = $db_handle->prepare($getAllQueries) 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="200" bgcolor="#5479d8"><span class="button">Query Name</td>
<td width="80" bgcolor="#5479d8"><span class="button">Logging Resource</td>
<td width="80" bgcolor="#5479d8"><span class="button">Entry Time</td>
<td width="100" bgcolor="#5479d8"><span class="button">Options</td>
EOF
while (($userdef_id, $userdef_entry_time, $userdef_name, $log_resource) = $statement2->fetchrow) {
if ($log_resource eq "\%") {
    $log_resource="any";
}
print <<EOF;
<tr OnMouseOver="this.bgColor='#d9d9d9'" OnMouseOut="this.bgColor='#ffffff'">
<td width="200"><a href="pix_config_query_view?id=$userdef_id" class="bodylink">$userdef_name</a></td>
<td width="80"><span class="main">$log_resource</td>
<td width="80"><span class="main">$userdef_entry_time</td>
<td width="100"><a href="pix_config_query_edit?id=$userdef_id&type=traffic&clone=1" class="bodylink">[ clone ]</span></a> <a href="pix_config_query_edit?id=$userdef_id" class="bodylink">[ edit ]</span></a> <a href="pix_config_traffic_query?remove_query=true&id=$userdef_id" class="bodylink">[ remove ]</span</a></td>
<tr>
EOF
}

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

