Commit cbed5e4e authored by Ad Schellevis's avatar Ad Schellevis Committed by Franco Fichtner

(legacy) refactor diag_dump_states.php

(cherry picked from commit 56f3f564)
parent 5ea4c31d
<?php <?php
/* /*
Copyright (C) 2014 Deciso B.V. Copyright (C) 2014-2016 Deciso B.V.
Copyright (C) 2005-2009 Scott Ullrich Copyright (C) 2005-2009 Scott Ullrich
Copyright (C) 2005 Colin Smith Copyright (C) 2005 Colin Smith
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met: modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, 1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer. this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright 2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution. documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE. POSSIBILITY OF SUCH DAMAGE.
*/ */
require_once("guiconfig.inc"); require_once("guiconfig.inc");
...@@ -33,229 +33,164 @@ require_once("interfaces.inc"); ...@@ -33,229 +33,164 @@ require_once("interfaces.inc");
/* handle AJAX operations */ /* handle AJAX operations */
if(isset($_POST['action']) && $_POST['action'] == "remove") { if(isset($_POST['action']) && $_POST['action'] == "remove") {
if (isset($_POST['srcip']) && isset($_POST['dstip']) && is_ipaddr($_POST['srcip']) && is_ipaddr($_POST['dstip'])) { if (isset($_POST['srcip']) && isset($_POST['dstip']) && is_ipaddr($_POST['srcip']) && is_ipaddr($_POST['dstip'])) {
$retval = mwexec("/sbin/pfctl -k " . escapeshellarg($_POST['srcip']) . " -k " . escapeshellarg($_POST['dstip'])); $retval = mwexec("/sbin/pfctl -k " . escapeshellarg($_POST['srcip']) . " -k " . escapeshellarg($_POST['dstip']));
echo htmlentities("|{$_POST['srcip']}|{$_POST['dstip']}|{$retval}|"); echo htmlentities("|{$_POST['srcip']}|{$_POST['dstip']}|{$retval}|");
} else { } else {
echo gettext("invalid input"); echo gettext("invalid input");
} }
return; exit;
} }
if (isset($_POST['filter']) && isset($_POST['killfilter'])) { if (isset($_POST['filter']) && isset($_POST['killfilter'])) {
if (is_ipaddr($_POST['filter'])) { if (is_ipaddr($_POST['filter'])) {
$tokill = escapeshellarg($_POST['filter'] . "/32"); $tokill = escapeshellarg($_POST['filter'] . "/32");
} elseif (is_subnet($_POST['filter'])) { } elseif (is_subnet($_POST['filter'])) {
$tokill = escapeshellarg($_POST['filter']); $tokill = escapeshellarg($_POST['filter']);
} else { } else {
// Invalid filter // Invalid filter
$tokill = ""; $tokill = "";
} }
if (!empty($tokill)) { if (!empty($tokill)) {
$retval = mwexec("/sbin/pfctl -k {$tokill} -k 0/0"); mwexec("/sbin/pfctl -k {$tokill} -k 0/0");
$retval = mwexec("/sbin/pfctl -k 0.0.0.0/0 -k {$tokill}"); mwexec("/sbin/pfctl -k 0.0.0.0/0 -k {$tokill}");
} }
} }
include("head.inc"); include("head.inc");
?> ?>
<body> <body>
<?php include("fbegin.inc"); ?> <?php include("fbegin.inc"); ?>
<script type="text/javascript">
<script type="text/javascript"> $( document ).ready(function() {
//<![CDATA[ // delete state
function removeState(srcip, dstip) { $(".act_del").click(function(event){
var busy = function(index,icon) { event.preventDefault();
jQuery(icon).bind("onclick",""); var srcip = $(this).data('srcip');
jQuery(icon).attr('src',jQuery(icon).attr('src').replace("\.gif", "_d.gif")); var dstip = $(this).data('dstip');
jQuery(icon).css("cursor","wait"); var rowid = $(this).data('rowid');
}
$.post(window.location, {action: 'remove', srcip: srcip, dstip: dstip}, function(data) {
jQuery('span[name="i:' + srcip + ":" + dstip + '"]').each(busy); $("."+rowid).hide();
});
jQuery.ajax( });
"<?=$_SERVER['SCRIPT_NAME'];?>", });
{
type: "post",
data: {
action: "remove",
srcip: srcip,
dstip: dstip
},
complete: removeComplete
}
);
}
function removeComplete(req) {
var values = req.responseText.split("|");
if(values[3] != "0") {
alert('<?=gettext("An error occurred.");?>');
return;
}
jQuery('tr[id="r:' + values[1] + ":" + values[2] + '"]').each(
function(index,row) { jQuery(row).fadeOut(1000); }
);
}
//]]>
</script> </script>
<section class="page-content-main">
<div class="container-fluid">
<div class="row">
<section class="col-xs-12">
<section class="page-content-main"> <div class="content-box">
<div class="container-fluid"> <form method="post" name="iform">
<div class="row"> <?php
<section class="col-xs-12"> $current_statecount=`pfctl -si | grep "current entries" | awk '{ print $3 }'`;?>
<div class="tab-content content-box col-xs-12"> <table class="table table-striped">
<form action="<?=$_SERVER['SCRIPT_NAME'];?>" method="post" name="iform"> <thead>
<?php <tr>
$current_statecount=`pfctl -si | grep "current entries" | awk '{ print $3 }'`; <th><?=gettext("Current total state count");?></th>
?> <th><?=gettext("Filter expression:");?></th>
<th></th>
<table class="table table-striped"> </tr>
<tbody> </thead>
<tr> <tbody>
<td><?=gettext("Current total state count");?>: <?= $current_statecount ?></td> <tr>
<td><?=gettext("Filter expression:");?></td> <td><?=$current_statecount?></td>
<td><input type="text" name="filter" class="form-control search" value="<?=htmlspecialchars($_POST['filter']);?>" size="30" /></td> <td>
<td> <input type="submit" class="btn btn-primary" value="<?=gettext("Filter");?>" /> <input type="text" name="filter" value="<?=!empty($_POST['filter']) ? htmlspecialchars($_POST['filter']) : "";?>"/>
<?php if (isset($_POST['filter']) && (is_ipaddr($_POST['filter']) || is_subnet($_POST['filter']))): ?> </td>
<input type="submit" class="btn" name="killfilter" value="<?=gettext("Kill");?>" /> <td>
<?php endif; ?> <input type="submit" class="btn btn-primary" value="<?=gettext("Filter");?>" />
</td> <?php if (isset($_POST['filter']) && (is_ipaddr($_POST['filter']) || is_subnet($_POST['filter']))): ?>
</tr> <input type="submit" class="btn btn-primary" name="killfilter" value="<?=gettext("Kill");?>" />
<?php endif; ?>
</tbody> </td>
</table> </tr>
</tbody>
</table>
</form> </form>
</div>
<div class="container-fluid tab-content"> </section>
<section class="col-xs-12">
<div class="tab-pane active" id="system"> <div class="content-box">
<div class="content-box-main">
<div class="content-box"> <div class="table-responsive">
<table id="state_table" class="table table-condensed table-hover table-striped">
<div class="table-responsive"> <thead>
<tr>
<table class="table table-striped table-sort sortable __nomb"> <th data-column-id="int"><?=gettext("Int");?></th>
<tr class="content-box-head"> <th data-column-id="proto"><?=gettext("Proto");?></th>
<th> <th data-column-id="path"><?=gettext("Source -> Router -> Destination");?></th>
<table> <th data-column-id="state"><?=gettext("State");?></th>
<tr> </tr>
<td><?=gettext("Int");?></td> </thead>
<td> <tbody>
<span class="table-sort-icon glyphicon glyphicon-sort"></span> <?php
</td> $row = 0;
</tr> /* get our states */
</table> $grepline = (isset($_POST['filter'])) ? "| /usr/bin/egrep " . escapeshellarg(htmlspecialchars($_POST['filter'])) : "";
</th> $fd = popen("/sbin/pfctl -s state {$grepline}", "r" );
<th> while ($line = chop(fgets($fd))):
<table> if ($row >= 10000) {
<tr> break;
<td><?=gettext("Proto");?></td> }
<td>
<span class="table-sort-icon glyphicon glyphicon-sort"></span> $line_split = preg_split("/\s+/", $line);
</td>
</tr> $iface = array_shift($line_split);
</table> $proto = array_shift($line_split);
</th> $state = array_pop($line_split);
<th> $info = htmlspecialchars(implode(" ", $line_split));
<table>
<tr> // We may want to make this optional, with a large state table, this could get to be expensive.
<td><?=gettext("Source -> Router -> Destination");?></td> $iface = convert_real_interface_to_friendly_descr($iface);
<td>
<span class="table-sort-icon glyphicon glyphicon-sort"></span> /* break up info and extract $srcip and $dstip */
</td> $ends = preg_split("/\<?-\>?/", $info);
</tr> $parts = explode(":", $ends[0]);
</table> $srcip = trim($parts[0]);
</th> $parts = explode(":", $ends[count($ends) - 1]);
<th> $dstip = trim($parts[0]);
<table> // states can be deleted by source / dest combination, all matching records use the same class.
<tr> $rowid = str_replace(array('.', ':'), '_', $srcip.$dstip);
<td><?=gettext("State");?></td> ?>
<td> <tr class="r<?=$rowid;?>">
<span class="table-sort-icon glyphicon glyphicon-sort"></span> <td><?= $iface ?></td>
</td> <td><?= $proto ?></td>
</tr> <td><?= $info ?></td>
</table> <td><?= $state ?></td>
</th> <td>
<th></th> <a href="#" data-rowid="r<?=$rowid?>" data-srcip="<?=$srcip?>" data-dstip="<?=$dstip;?>" class="act_del btn btn-default" title="<?= gettext('Remove all state entries from') ?> <?= $srcip ?> <?= gettext('to') ?> <?= $dstip ?>"><span class="glyphicon glyphicon-remove"></span></a>
</tr> </td>
<?php </tr>
$row = 0; <?php
/* get our states */ $row++;
$grepline = (isset($_POST['filter'])) ? "| /usr/bin/egrep " . escapeshellarg(htmlspecialchars($_POST['filter'])) : ""; endwhile;
$fd = popen("/sbin/pfctl -s state {$grepline}", "r" ); if ($row == 0): ?>
while ($line = chop(fgets($fd))) { <tr>
if($row >= 10000) <td colspan="5"><?= gettext("No states were found.") ?></td>
break; </tr>
<?php
$line_split = preg_split("/\s+/", $line); endif;
pclose($fd);?>
$iface = array_shift($line_split); </tbody>
$proto = array_shift($line_split); <tfoot>
$state = array_pop($line_split); <?php
$info = implode(" ", $line_split); if (!empty($_POST['filter'])): ?>
<tr>
// We may want to make this optional, with a large state table, this could get to be expensive. <td colspan="5"><?=gettext("States matching current filter")?>: <?= $row ?></td>
$iface = convert_real_interface_to_friendly_descr($iface); </tr>
<?php
/* break up info and extract $srcip and $dstip */ endif;?>
$ends = preg_split("/\<?-\>?/", $info); </tfoot>
$parts = explode(":", $ends[0]); </table>
$srcip = trim($parts[0]); </div>
$parts = explode(":", $ends[count($ends) - 1]); </div>
$dstip = trim($parts[0]); </div>
</section>
?> </div>
<tr id="r:<?= $srcip ?>:<?= $dstip ?>"> </div>
<td class="listlr"><?= $iface ?></td> </section>
<td class="listr"><?= $proto ?></td>
<td class="listr"><?= $info ?></td>
<td class="listr"><?= $state ?></td>
<td class="list">
<a href="#" onclick="removeState('<?= $srcip ?>', '<?= $dstip ?>');" name="i:<?= $srcip ?>:<?= $dstip ?>" class="btn btn-default" title="<?= gettext('Remove all state entries from') ?> <?= $srcip ?> <?= gettext('to') ?> <?= $dstip ?>"><span class="glyphicon glyphicon-remove"></span></a>
</td>
</tr>
<?php
$row++;
ob_flush();
}
if ($row == 0): ?>
<tr>
<td class="list" colspan="5" align="center" valign="top">
<?= gettext("No states were found.") ?>
</td>
</tr>
<?php endif;
pclose($fd);
?>
</table>
<?php if (isset($_POST['filter']) && !empty($_POST['filter'])): ?>
<div class="col-xs-12"><p><?=gettext("States matching current filter")?>: <?= $row ?></p></div>
<?php endif; ?>
</div>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
</section>
<?php include('foot.inc');?> <?php include('foot.inc');?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment