diag_dump_states.php 7.84 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1
<?php
2

Ad Schellevis's avatar
Ad Schellevis committed
3
/*
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
    Copyright (C) 2014-2016 Deciso B.V.
    Copyright (C) 2005-2009 Scott Ullrich
    Copyright (C) 2005 Colin Smith
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.

    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    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
    POSSIBILITY OF SUCH DAMAGE.
Ad Schellevis's avatar
Ad Schellevis committed
29 30 31 32 33 34 35
*/

require_once("guiconfig.inc");
require_once("interfaces.inc");

/* handle AJAX operations */
if(isset($_POST['action']) && $_POST['action'] == "remove") {
36 37 38 39 40 41 42
    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']));
        echo htmlentities("|{$_POST['srcip']}|{$_POST['dstip']}|{$retval}|");
    } else {
        echo gettext("invalid input");
    }
    exit;
Ad Schellevis's avatar
Ad Schellevis committed
43 44 45
}

if (isset($_POST['filter']) && isset($_POST['killfilter'])) {
46 47 48 49 50 51 52 53 54 55 56 57
    if (is_ipaddr($_POST['filter'])) {
        $tokill = escapeshellarg($_POST['filter'] . "/32");
    } elseif (is_subnet($_POST['filter'])) {
        $tokill = escapeshellarg($_POST['filter']);
    } else {
        // Invalid filter
        $tokill = "";
    }
    if (!empty($tokill)) {
        mwexec("/sbin/pfctl -k {$tokill} -k 0/0");
        mwexec("/sbin/pfctl -k 0.0.0.0/0 -k {$tokill}");
    }
Ad Schellevis's avatar
Ad Schellevis committed
58 59 60 61 62
}

include("head.inc");
?>

63
<body>
Ad Schellevis's avatar
Ad Schellevis committed
64
<?php include("fbegin.inc"); ?>
65 66 67 68 69 70 71 72 73 74 75 76 77 78
  <script type="text/javascript">
  $( document ).ready(function() {
    // delete state
    $(".act_del").click(function(event){
        event.preventDefault();
        var srcip = $(this).data('srcip');
        var dstip = $(this).data('dstip');
        var rowid = $(this).data('rowid');

        $.post(window.location, {action: 'remove', srcip: srcip, dstip: dstip}, function(data) {
            $("."+rowid).hide();
        });
    });
  });
Ad Schellevis's avatar
Ad Schellevis committed
79
</script>
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
  <section class="page-content-main">
    <div class="container-fluid">
      <div class="row">
        <section class="col-xs-12">
          <div class="content-box">
            <form  method="post" name="iform">
<?php
              $current_statecount=`pfctl -si | grep "current entries" | awk '{ print $3 }'`;?>
              <table class="table table-striped">
                <thead>
                  <tr>
                    <th><?=gettext("Current total state count");?></th>
                    <th><?=gettext("Filter expression:");?></th>
                    <th></th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td><?=$current_statecount?></td>
                    <td>
                      <input type="text" name="filter" value="<?=!empty($_POST['filter']) ? htmlspecialchars($_POST['filter']) : "";?>"/>
                    </td>
                    <td>
                      <input type="submit" class="btn btn-primary" value="<?=gettext("Filter");?>" />
                      <?php if (isset($_POST['filter']) && (is_ipaddr($_POST['filter']) || is_subnet($_POST['filter']))): ?>
                      <input type="submit" class="btn btn-primary" name="killfilter" value="<?=gettext("Kill");?>" />
                      <?php endif; ?>
                    </td>
                  </tr>
                </tbody>
              </table>
            </form>
          </div>
        </section>
        <section class="col-xs-12">
          <div class="content-box">
            <div class="content-box-main">
              <div class="table-responsive">
                <table id="state_table" class="table table-condensed table-hover table-striped">
                  <thead>
                    <tr>
                      <th data-column-id="int"><?=gettext("Int");?></th>
                      <th data-column-id="proto"><?=gettext("Proto");?></th>
                      <th data-column-id="path"><?=gettext("Source -> Router -> Destination");?></th>
                      <th data-column-id="state"><?=gettext("State");?></th>
                    </tr>
                  </thead>
                  <tbody>
<?php
                  $row = 0;
                  /* get our states */
                  $grepline = (isset($_POST['filter'])) ? "| /usr/bin/egrep " . escapeshellarg(htmlspecialchars($_POST['filter'])) : "";
                  $fd = popen("/sbin/pfctl -s state {$grepline}", "r" );
                  while ($line = chop(fgets($fd))):
                    if ($row >= 10000) {
                        break;
                    }

                    $line_split = preg_split("/\s+/", $line);

                    $iface  = array_shift($line_split);
                    $proto = array_shift($line_split);
                    $state = array_pop($line_split);
                    $info  = htmlspecialchars(implode(" ", $line_split));

                    // We may want to make this optional, with a large state table, this could get to be expensive.
                    $iface = convert_real_interface_to_friendly_descr($iface);

                    /* break up info and extract $srcip and $dstip */
                    $ends = preg_split("/\<?-\>?/", $info);
                    $parts = explode(":", $ends[0]);
                    $srcip = trim($parts[0]);
                    $parts = explode(":", $ends[count($ends) - 1]);
                    $dstip = trim($parts[0]);
                    // states can be deleted by source / dest combination, all matching records use the same class.
                    $rowid = str_replace(array('.', ':'), '_', $srcip.$dstip);
                  ?>
                    <tr class="r<?=$rowid;?>">
                      <td><?= $iface ?></td>
                      <td><?= $proto ?></td>
                      <td><?= $info ?></td>
                      <td><?= $state ?></td>
                      <td>
                        <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>
                      </td>
                    </tr>
<?php
                    $row++;
                  endwhile;
                  if ($row == 0): ?>
                    <tr>
                      <td colspan="5"><?= gettext("No states were found.") ?></td>
                    </tr>
<?php
                  endif;
                  pclose($fd);?>
                  </tbody>
                  <tfoot>
<?php
                    if (!empty($_POST['filter'])): ?>
                    <tr>
                      <td colspan="5"><?=gettext("States matching current filter")?>: <?= $row ?></td>
                    </tr>
<?php
                    endif;?>
                  </tfoot>
                </table>
              </div>
            </div>
          </div>
        </section>
      </div>
    </div>
  </section>
194

Ad Schellevis's avatar
Ad Schellevis committed
195

196
<?php include('foot.inc');?>