Commit 3b64eaf8 authored by Ad Schellevis's avatar Ad Schellevis

(legacy) move single used code, fix csrf issue, and further cleanups in firewall log code

parent 6af1ccbc
<?php
/*
Copyright (C) 2009-2010 Jim Pingle (jpingle@gmail.com)
Originally Sponsored By Anathematic @ pfSense Forums
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.
*/
$blockaliasname = 'EasyRuleBlockHosts';
$protocols_with_ports = array('tcp', 'udp');
require_once("functions.inc");
require_once("util.inc");
require_once("config.inc");
function easyrule_find_rule_interface($int) {
global $config;
/* Borrowed from firewall_rules.php */
$iflist = get_configured_interface_with_descr(false, true);
if ($config['pptpd']['mode'] == "server")
$iflist['pptp'] = "PPTP VPN";
if ($config['pppoe']['mode'] == "server")
$iflist['pppoe'] = "PPPoE VPN";
if ($config['l2tp']['mode'] == "server")
$iflist['l2tp'] = "L2TP VPN";
/* add ipsec interfaces */
if (isset($config['ipsec']['enable']) || isset($config['ipsec']['client']['enable'])){
$iflist["enc0"] = "IPSEC";
}
if (isset($iflist[$int]))
return $int;
foreach ($iflist as $if => $ifd) {
if (strtolower($int) == strtolower($ifd))
return $if;
}
if (substr($int, 0, 4) == "ovpn")
return "openvpn";
return false;
}
function easyrule_block_rule_exists($int = 'wan', $ipproto = "inet") {
global $blockaliasname, $config;
/* No rules, we we know it doesn't exist */
if (!is_array($config['filter']['rule'])) {
return false;
}
/* Search through the rules for one referencing our alias */
foreach ($config['filter']['rule'] as $rule) {
if (!is_array($rule) || !is_array($rule['source']))
continue;
$checkproto = isset($rule['ipprotocol']) ? $rule['ipprotocol'] : "inet";
if ($rule['source']['address'] == $blockaliasname . strtoupper($int) && ($rule['interface'] == $int) && ($checkproto == $ipproto))
return true;
}
return false;
}
function easyrule_block_rule_create($int = 'wan', $ipproto = "inet") {
global $blockaliasname, $config;
/* If the alias doesn't exist, exit.
* Can't create an empty alias, and we don't know a host */
if (easyrule_block_alias_getid($int) === false)
return false;
/* If the rule already exists, no need to do it again */
if (easyrule_block_rule_exists($int, $ipproto))
return true;
/* No rules, start a new array */
if (!is_array($config['filter']['rule'])) {
$config['filter']['rule'] = array();
}
filter_rules_sort();
$a_filter = &$config['filter']['rule'];
/* Make up a new rule */
$filterent = array();
$filterent['type'] = 'block';
$filterent['interface'] = $int;
$filterent['ipprotocol'] = $ipproto;
$filterent['source']['address'] = $blockaliasname . strtoupper($int);
$filterent['destination']['any'] = '';
$filterent['descr'] = gettext("Easy Rule: Blocked from Firewall Log View");
$filterent['created'] = make_config_revision_entry(null, gettext("Easy Rule"));
array_splice($a_filter, 0, 0, array($filterent));
return true;
}
function easyrule_block_alias_getid($int = 'wan') {
global $blockaliasname, $config;
if (!is_array($config['aliases']))
return false;
/* Hunt down an alias with the name we want, return its id */
foreach ($config['aliases']['alias'] as $aliasid => $alias)
if ($alias['name'] == $blockaliasname . strtoupper($int))
return $aliasid;
return false;
}
function easyrule_block_alias_add($host, $int = 'wan') {
global $blockaliasname, $config;
/* If the host isn't a valid IP address, bail */
$host = trim($host, "[]");
if (!is_ipaddr($host) && !is_subnet($host))
return false;
/* If there are no aliases, start an array */
if (!is_array($config['aliases'])) {
$config['aliases'] = array();
}
if (!is_array($config['aliases']['alias'])) {
$config['aliases']['alias'] = array();
}
$a_aliases = &$config['aliases']['alias'];
/* Try to get the ID if the alias already exists */
$id = easyrule_block_alias_getid($int);
if ($id === false)
unset($id);
$alias = array();
if (is_subnet($host)) {
list($host, $mask) = explode("/", $host);
} elseif (is_specialnet($host)) {
$mask = 0;
} elseif (is_ipaddrv6($host)) {
$mask = 128;
} else {
$mask = 32;
}
if (isset($id) && $a_aliases[$id]) {
/* Make sure this IP isn't already in the list. */
if (in_array($host.'/'.$mask, explode(" ", $a_aliases[$id]['address'])))
return true;
/* Since the alias already exists, just add to it. */
$alias['name'] = $a_aliases[$id]['name'];
$alias['type'] = $a_aliases[$id]['type'];
$alias['descr'] = $a_aliases[$id]['descr'];
$alias['address'] = $a_aliases[$id]['address'] . ' ' . $host . '/' . $mask;
$alias['detail'] = $a_aliases[$id]['detail'] . gettext('Entry added') . ' ' . date('r') . '||';
} else {
/* Create a new alias with all the proper information */
$alias['name'] = $blockaliasname . strtoupper($int);
$alias['type'] = 'network';
$alias['descr'] = gettext("Hosts blocked from Firewall Log view");
$alias['address'] = $host . '/' . $mask;
$alias['detail'] = gettext('Entry added') . ' ' . date('r') . '||';
}
/* Replace the old alias if needed, otherwise tack it on the end */
if (isset($id) && $a_aliases[$id])
$a_aliases[$id] = $alias;
else
$a_aliases[] = $alias;
// Sort list
$a_aliases = msort($a_aliases, "name");
return true;
}
function easyrule_block_host_add($host, $int = 'wan', $ipproto = "inet") {
global $retval;
/* Bail if the supplied host is not a valid IP address */
$host = trim($host, "[]");
if (!is_ipaddr($host) && !is_subnet($host))
return false;
/* Flag whether or not we need to reload the filter */
$dirty = false;
/* Attempt to add this host to the alias */
if (easyrule_block_alias_add($host, $int)) {
$dirty = true;
} else {
/* Couldn't add the alias, or adding the host failed. */
return false;
}
/* Attempt to add the firewall rule if it doesn't exist.
* Failing to add the rule isn't necessarily an error, it may
* have been modified by the user in some way. Adding to the
* Alias is what's important.
*/
if (!easyrule_block_rule_exists($int, $ipproto)) {
if (easyrule_block_rule_create($int, $ipproto)) {
$dirty = true;
} else {
return false;
}
}
/* If needed, write the config and reload the filter */
if ($dirty) {
write_config();
$retval = filter_configure();
if (!empty($_SERVER['DOCUMENT_ROOT'])) {
header("Location: firewall_aliases.php");
exit;
} else {
return true;
}
} else {
return false;
}
}
function easyrule_pass_rule_add($int, $proto, $srchost, $dsthost, $dstport, $ipproto) {
global $config;
/* No rules, start a new array */
if (!is_array($config['filter']['rule'])) {
$config['filter']['rule'] = array();
}
filter_rules_sort();
$a_filter = &$config['filter']['rule'];
/* Make up a new rule */
$filterent = array();
$filterent['type'] = 'pass';
$filterent['interface'] = $int;
$filterent['ipprotocol'] = $ipproto;
$filterent['descr'] = gettext("Easy Rule: Passed from Firewall Log View");
if ($proto != "any")
$filterent['protocol'] = $proto;
else
unset($filterent['protocol']);
/* Default to only allow echo requests, since that's what most people want and
* it should be a safe choice. */
if ($proto == "icmp")
$filterent['icmptype'] = 'echoreq';
if ((strtolower($proto) == "icmp6") || (strtolower($proto) == "icmpv6"))
$filterent['protocol'] = "icmp";
if (is_subnet($srchost)) {
list($srchost, $srcmask) = explode("/", $srchost);
} elseif (is_specialnet($srchost)) {
$srcmask = 0;
} elseif (is_ipaddrv6($srchost)) {
$srcmask = 128;
} else {
$srcmask = 32;
}
if (is_subnet($dsthost)) {
list($dsthost, $dstmask) = explode("/", $dsthost);
} elseif (is_specialnet($dsthost)) {
$dstmask = 0;
} elseif (is_ipaddrv6($dsthost)) {
$dstmask = 128;
} else {
$dstmask = 32;
}
pconfig_to_address($filterent['source'], $srchost, $srcmask);
pconfig_to_address($filterent['destination'], $dsthost, $dstmask, '', $dstport, $dstport);
$filterent['created'] = make_config_revision_entry(null, gettext("Easy Rule"));
$a_filter[] = $filterent;
write_config($filterent['descr']);
$retval = filter_configure();
if (!empty($_SERVER['DOCUMENT_ROOT'])) {
header("Location: firewall_rules.php?if={$int}");
exit;
} else {
return true;
}
}
function easyrule_parse_block($int, $src, $ipproto = "inet") {
if (!empty($src) && !empty($int)) {
$src = trim($src, "[]");
if (!is_ipaddr($src) && !is_subnet($src)) {
return gettext("Tried to block invalid IP:") . ' ' . htmlspecialchars($src);
}
$int = easyrule_find_rule_interface($int);
if ($int === false) {
return gettext("Invalid interface for block rule:") . ' ' . htmlspecialchars($int);
}
if (easyrule_block_host_add($src, $int, $ipproto)) {
return gettext("Host added successfully");
} else {
return gettext("Failed to create block rule, alias, or add host.");
}
} else {
return gettext("Tried to block but had no host IP or interface");
}
return gettext("Unknown block error.");
}
function easyrule_parse_pass($int, $proto, $src, $dst, $dstport = 0, $ipproto = "inet") {
/* Check for valid int, srchost, dsthost, dstport, and proto */
global $protocols_with_ports;
$src = trim($src, "[]");
$dst = trim($dst, "[]");
if (!empty($int) && !empty($proto) && !empty($src) && !empty($dst)) {
$int = easyrule_find_rule_interface($int);
if ($int === false) {
return gettext("Invalid interface for pass rule:") . ' ' . htmlspecialchars($int);
}
if (getprotobyname($proto) == -1) {
return gettext("Invalid protocol for pass rule:") . ' ' . htmlspecialchars($proto);
}
if (!is_ipaddr($src) && !is_subnet($src) && !is_ipaddroralias($src) && !is_specialnet($src)) {
return gettext("Tried to pass invalid source IP:") . ' ' . htmlspecialchars($src);
}
if (!is_ipaddr($dst) && !is_subnet($dst) && !is_ipaddroralias($dst) && !is_specialnet($dst)) {
return gettext("Tried to pass invalid destination IP:") . ' ' . htmlspecialchars($dst);
}
if (in_array($proto, $protocols_with_ports)) {
if (empty($dstport)) {
return gettext("Missing destination port:") . ' ' . htmlspecialchars($dstport);
}
if (!is_port($dstport) && ($dstport != "any")) {
return gettext("Tried to pass invalid destination port:") . ' ' . htmlspecialchars($dstport);
}
} else {
$dstport = 0;
}
/* Should have valid input... */
if (easyrule_pass_rule_add($int, $proto, $src, $dst, $dstport, $ipproto)) {
return gettext("Successfully added pass rule!");
} else {
return gettext("Failed to add pass rule.");
}
} else {
return gettext("Missing parameters for pass rule.");
}
return gettext("Unknown pass error.");
}
?>
......@@ -5,6 +5,8 @@
Copyright (C) Jim Pingle jim@pingle.org
Copyright (C) 2004-2009 Scott Ullrich
Copyright (C) 2003-2009 Manuel Kasper <mk@neon1.net>,
(origin easyrule.inc/php) Copyright (C) 2009-2010 Jim Pingle (jpingle@gmail.com)
(origin easyrule.inc/php) Originally Sponsored By Anathematic @ pfSense Forums
All rights reserved.
Redistribution and use in source and binary forms, with or without
......@@ -32,6 +34,360 @@
require_once("guiconfig.inc");
require_once("filter_log.inc");
/********************************************************************************************************************
* imported from easyrule.inc/php
********************************************************************************************************************/
require_once("functions.inc");
require_once("util.inc");
function easyrule_find_rule_interface($int) {
global $config;
/* Borrowed from firewall_rules.php */
$iflist = get_configured_interface_with_descr(false, true);
if ($config['pptpd']['mode'] == "server")
$iflist['pptp'] = "PPTP VPN";
if ($config['pppoe']['mode'] == "server")
$iflist['pppoe'] = "PPPoE VPN";
if ($config['l2tp']['mode'] == "server")
$iflist['l2tp'] = "L2TP VPN";
/* add ipsec interfaces */
if (isset($config['ipsec']['enable']) || isset($config['ipsec']['client']['enable'])){
$iflist["enc0"] = "IPSEC";
}
if (isset($iflist[$int]))
return $int;
foreach ($iflist as $if => $ifd) {
if (strtolower($int) == strtolower($ifd))
return $if;
}
if (substr($int, 0, 4) == "ovpn")
return "openvpn";
return false;
}
function easyrule_block_rule_exists($int = 'wan', $ipproto = "inet") {
global $config;
$blockaliasname = 'EasyRuleBlockHosts';
/* No rules, we we know it doesn't exist */
if (!is_array($config['filter']['rule'])) {
return false;
}
/* Search through the rules for one referencing our alias */
foreach ($config['filter']['rule'] as $rule) {
if (!is_array($rule) || !is_array($rule['source']))
continue;
$checkproto = isset($rule['ipprotocol']) ? $rule['ipprotocol'] : "inet";
if ($rule['source']['address'] == $blockaliasname . strtoupper($int) && ($rule['interface'] == $int) && ($checkproto == $ipproto))
return true;
}
return false;
}
function easyrule_block_rule_create($int = 'wan', $ipproto = "inet") {
global $config;
$blockaliasname = 'EasyRuleBlockHosts';
/* If the alias doesn't exist, exit.
* Can't create an empty alias, and we don't know a host */
if (easyrule_block_alias_getid($int) === false)
return false;
/* If the rule already exists, no need to do it again */
if (easyrule_block_rule_exists($int, $ipproto))
return true;
/* No rules, start a new array */
if (!is_array($config['filter']['rule'])) {
$config['filter']['rule'] = array();
}
filter_rules_sort();
$a_filter = &$config['filter']['rule'];
/* Make up a new rule */
$filterent = array();
$filterent['type'] = 'block';
$filterent['interface'] = $int;
$filterent['ipprotocol'] = $ipproto;
$filterent['source']['address'] = $blockaliasname . strtoupper($int);
$filterent['destination']['any'] = '';
$filterent['descr'] = gettext("Easy Rule: Blocked from Firewall Log View");
$filterent['created'] = make_config_revision_entry(null, gettext("Easy Rule"));
array_splice($a_filter, 0, 0, array($filterent));
return true;
}
function easyrule_block_alias_getid($int = 'wan') {
global $config;
$blockaliasname = 'EasyRuleBlockHosts';
if (!is_array($config['aliases']))
return false;
/* Hunt down an alias with the name we want, return its id */
foreach ($config['aliases']['alias'] as $aliasid => $alias)
if ($alias['name'] == $blockaliasname . strtoupper($int))
return $aliasid;
return false;
}
function easyrule_block_alias_add($host, $int = 'wan') {
global $config;
$blockaliasname = 'EasyRuleBlockHosts';
/* If the host isn't a valid IP address, bail */
$host = trim($host, "[]");
if (!is_ipaddr($host) && !is_subnet($host))
return false;
/* If there are no aliases, start an array */
if (!is_array($config['aliases'])) {
$config['aliases'] = array();
}
if (!is_array($config['aliases']['alias'])) {
$config['aliases']['alias'] = array();
}
$a_aliases = &$config['aliases']['alias'];
/* Try to get the ID if the alias already exists */
$id = easyrule_block_alias_getid($int);
if ($id === false)
unset($id);
$alias = array();
if (is_subnet($host)) {
list($host, $mask) = explode("/", $host);
} elseif (is_specialnet($host)) {
$mask = 0;
} elseif (is_ipaddrv6($host)) {
$mask = 128;
} else {
$mask = 32;
}
if (isset($id) && $a_aliases[$id]) {
/* Make sure this IP isn't already in the list. */
if (in_array($host.'/'.$mask, explode(" ", $a_aliases[$id]['address'])))
return true;
/* Since the alias already exists, just add to it. */
$alias['name'] = $a_aliases[$id]['name'];
$alias['type'] = $a_aliases[$id]['type'];
$alias['descr'] = $a_aliases[$id]['descr'];
$alias['address'] = $a_aliases[$id]['address'] . ' ' . $host . '/' . $mask;
$alias['detail'] = $a_aliases[$id]['detail'] . gettext('Entry added') . ' ' . date('r') . '||';
} else {
/* Create a new alias with all the proper information */
$alias['name'] = $blockaliasname . strtoupper($int);
$alias['type'] = 'network';
$alias['descr'] = gettext("Hosts blocked from Firewall Log view");
$alias['address'] = $host . '/' . $mask;
$alias['detail'] = gettext('Entry added') . ' ' . date('r') . '||';
}
/* Replace the old alias if needed, otherwise tack it on the end */
if (isset($id) && $a_aliases[$id])
$a_aliases[$id] = $alias;
else
$a_aliases[] = $alias;
// Sort list
$a_aliases = msort($a_aliases, "name");
return true;
}
function easyrule_block_host_add($host, $int = 'wan', $ipproto = "inet") {
global $retval;
/* Bail if the supplied host is not a valid IP address */
$host = trim($host, "[]");
if (!is_ipaddr($host) && !is_subnet($host))
return false;
/* Flag whether or not we need to reload the filter */
$dirty = false;
/* Attempt to add this host to the alias */
if (easyrule_block_alias_add($host, $int)) {
$dirty = true;
} else {
/* Couldn't add the alias, or adding the host failed. */
return false;
}
/* Attempt to add the firewall rule if it doesn't exist.
* Failing to add the rule isn't necessarily an error, it may
* have been modified by the user in some way. Adding to the
* Alias is what's important.
*/
if (!easyrule_block_rule_exists($int, $ipproto)) {
if (easyrule_block_rule_create($int, $ipproto)) {
$dirty = true;
} else {
return false;
}
}
/* If needed, write the config and reload the filter */
if ($dirty) {
write_config();
$retval = filter_configure();
if (!empty($_SERVER['DOCUMENT_ROOT'])) {
header("Location: firewall_aliases.php");
exit;
} else {
return true;
}
} else {
return false;
}
}
function easyrule_pass_rule_add($int, $proto, $srchost, $dsthost, $dstport, $ipproto) {
global $config;
/* No rules, start a new array */
if (!is_array($config['filter']['rule'])) {
$config['filter']['rule'] = array();
}
filter_rules_sort();
$a_filter = &$config['filter']['rule'];
/* Make up a new rule */
$filterent = array();
$filterent['type'] = 'pass';
$filterent['interface'] = $int;
$filterent['ipprotocol'] = $ipproto;
$filterent['descr'] = gettext("Easy Rule: Passed from Firewall Log View");
if ($proto != "any")
$filterent['protocol'] = $proto;
else
unset($filterent['protocol']);
/* Default to only allow echo requests, since that's what most people want and
* it should be a safe choice. */
if ($proto == "icmp")
$filterent['icmptype'] = 'echoreq';
if ((strtolower($proto) == "icmp6") || (strtolower($proto) == "icmpv6"))
$filterent['protocol'] = "icmp";
if (is_subnet($srchost)) {
list($srchost, $srcmask) = explode("/", $srchost);
} elseif (is_specialnet($srchost)) {
$srcmask = 0;
} elseif (is_ipaddrv6($srchost)) {
$srcmask = 128;
} else {
$srcmask = 32;
}
if (is_subnet($dsthost)) {
list($dsthost, $dstmask) = explode("/", $dsthost);
} elseif (is_specialnet($dsthost)) {
$dstmask = 0;
} elseif (is_ipaddrv6($dsthost)) {
$dstmask = 128;
} else {
$dstmask = 32;
}
pconfig_to_address($filterent['source'], $srchost, $srcmask);
pconfig_to_address($filterent['destination'], $dsthost, $dstmask, '', $dstport, $dstport);
$filterent['created'] = make_config_revision_entry(null, gettext("Easy Rule"));
$a_filter[] = $filterent;
write_config($filterent['descr']);
$retval = filter_configure();
if (!empty($_SERVER['DOCUMENT_ROOT'])) {
header("Location: firewall_rules.php?if={$int}");
exit;
} else {
return true;
}
}
function easyrule_parse_block($int, $src, $ipproto = "inet") {
if (!empty($src) && !empty($int)) {
$src = trim($src, "[]");
if (!is_ipaddr($src) && !is_subnet($src)) {
return gettext("Tried to block invalid IP:") . ' ' . htmlspecialchars($src);
}
$int = easyrule_find_rule_interface($int);
if ($int === false) {
return gettext("Invalid interface for block rule:") . ' ' . htmlspecialchars($int);
}
if (easyrule_block_host_add($src, $int, $ipproto)) {
return gettext("Host added successfully");
} else {
return gettext("Failed to create block rule, alias, or add host.");
}
} else {
return gettext("Tried to block but had no host IP or interface");
}
return gettext("Unknown block error.");
}
function easyrule_parse_pass($int, $proto, $src, $dst, $dstport = 0, $ipproto = "inet") {
/* Check for valid int, srchost, dsthost, dstport, and proto */
$protocols_with_ports = array('tcp', 'udp');
$src = trim($src, "[]");
$dst = trim($dst, "[]");
if (!empty($int) && !empty($proto) && !empty($src) && !empty($dst)) {
$int = easyrule_find_rule_interface($int);
if ($int === false) {
return gettext("Invalid interface for pass rule:") . ' ' . htmlspecialchars($int);
}
if (getprotobyname($proto) == -1) {
return gettext("Invalid protocol for pass rule:") . ' ' . htmlspecialchars($proto);
}
if (!is_ipaddr($src) && !is_subnet($src) && !is_ipaddroralias($src) && !is_specialnet($src)) {
return gettext("Tried to pass invalid source IP:") . ' ' . htmlspecialchars($src);
}
if (!is_ipaddr($dst) && !is_subnet($dst) && !is_ipaddroralias($dst) && !is_specialnet($dst)) {
return gettext("Tried to pass invalid destination IP:") . ' ' . htmlspecialchars($dst);
}
if (in_array($proto, $protocols_with_ports)) {
if (empty($dstport)) {
return gettext("Missing destination port:") . ' ' . htmlspecialchars($dstport);
}
if (!is_port($dstport) && ($dstport != "any")) {
return gettext("Tried to pass invalid destination port:") . ' ' . htmlspecialchars($dstport);
}
} else {
$dstport = 0;
}
/* Should have valid input... */
if (easyrule_pass_rule_add($int, $proto, $src, $dst, $dstport, $ipproto)) {
return gettext("Successfully added pass rule!");
} else {
return gettext("Failed to add pass rule.");
}
} else {
return gettext("Missing parameters for pass rule.");
}
return gettext("Unknown pass error.");
}
/**********************************************************************************************************************************
* End of imported code
*********************************************************************************************************************************/
# --- AJAX RESOLVE ---
if (isset($_POST['resolve'])) {
$ip = strtolower($_POST['resolve']);
......@@ -46,12 +402,33 @@ if (isset($_POST['resolve'])) {
exit;
}
if (isset($_POST['easyrule'])) {
require_once("easyrule.inc");
require_once("filter.inc");
$response = array("status"=>"unknown") ;
switch ($_POST['easyrule']) {
case 'block':
easyrule_parse_block($_POST['intf'], $_POST['srcip'], $_POST['ipproto']);
$response["status"] = "block" ;
break;
case 'pass':
easyrule_parse_pass($_POST['intf'], $_POST['proto'], $_POST['srcip'], $_POST['dstip'], $_POST['dstport'], $_POST['ipproto']);
$response["status"] = "pass" ;
break;
}
echo json_encode(str_replace("\\","\\\\", $response));
exit;
}
function getGETPOSTsettingvalue($settingname, $default)
{
$settingvalue = $default;
if($_GET[$settingname])
if(isset($_GET[$settingname]))
$settingvalue = $_GET[$settingname];
if($_POST[$settingname])
if(isset($_POST[$settingname]))
$settingvalue = $_POST[$settingname];
return $settingvalue;
}
......@@ -64,13 +441,13 @@ if($rulenum) {
exit;
}
$filterfieldsarray = array();
$filtersubmit = getGETPOSTsettingvalue('filtersubmit', null);
if ($filtersubmit) {
$interfacefilter = getGETPOSTsettingvalue('interface', null);
$filtertext = getGETPOSTsettingvalue('filtertext', "");
$filterlogentries_qty = getGETPOSTsettingvalue('filterlogentries_qty', null);
$filterfieldsarray = array();
$actpass = getGETPOSTsettingvalue('actpass', null);
$actblock = getGETPOSTsettingvalue('actblock', null);
......@@ -86,22 +463,31 @@ if ($filtersubmit) {
$filterfieldsarray['proto'] = getGETPOSTsettingvalue('filterlogentries_protocol', null);
$filterfieldsarray['tcpflags'] = getGETPOSTsettingvalue('filterlogentries_protocolflags', null);
$filterlogentries_qty = getGETPOSTsettingvalue('filterlogentries_qty', null);
} else {
$interfacefilter = null;
$filterlogentries_qty = null ;
$filtertext = null;
foreach (array('act','time','interface','srcip','srcport','dstip','dstport','proto','tcpflags') as $tag) {
$filterfieldsarray[$tag] = null;
}
}
$filter_logfile = '/var/log/filter.log';
$nentries = $config['syslog']['nentries'];
if (isset($config['syslog']['nentries'])) {
$nentries = $config['syslog']['nentries'];
} else {
$nentries = 50;
}
# Override Display Quantity
if ($filterlogentries_qty) {
if (isset($filterlogentries_qty) && $filterlogentries_qty != null) {
$nentries = $filterlogentries_qty;
}
if (!$nentries) {
$nentries = 50;
}
if ($_POST['clear']) {
if (isset($_POST['clear'])) {
clear_log_file($filter_logfile);
}
......@@ -184,88 +570,6 @@ include("head.inc");
</div>
</div>
</section>
<!--
<section class="col-xs-12">
<div class="tab-content content-box col-xs-12">
<div class="container-fluid">
<div id="filterform_show" class="widgetconfigdiv" style="<?=(!isset($config['syslog']['rawfilter']))?"display:none":""?>">
<form id="filterform" name="filterform" action="diag_logs_filter.php" method="post">
<table width="0%" border="0" cellpadding="0" cellspacing="0" summary="firewall log">
<tr>
<td>
<div align="center" style="vertical-align:top;"><?=gettext("Interface");?></div>
<div align="center" style="vertical-align:top;">
<select name="interface" onchange="dst_change(this.value,iface_old,document.iform.dsttype.value);iface_old = document.iform.interface.value;typesel_change();">
<option value="" <?=$interfacefilter?"":"selected=\"selected\""?>>*Any interface</option>
<?php
$iflist = get_configured_interface_with_descr(false, true);
foreach ($iflist as $if => $ifdesc)
$interfaces[$if] = $ifdesc;
if ($config['l2tp']['mode'] == "server")
$interfaces['l2tp'] = "L2TP VPN";
if ($config['pptpd']['mode'] == "server")
$interfaces['pptp'] = "PPTP VPN";
if (is_pppoe_server_enabled() && have_ruleint_access("pppoe"))
$interfaces['pppoe'] = "PPPoE VPN";
/* add ipsec interfaces */
if (isset($config['ipsec']['enable']) || isset($config['ipsec']['client']['enable']))
$interfaces["enc0"] = "IPsec";
/* add openvpn/tun interfaces */
if (isset($config['openvpn']['openvpn-server']) || isset($config['openvpn']['openvpn-client'])) {
$interfaces['openvpn'] = 'OpenVPN';
}
foreach ($interfaces as $iface => $ifacename): ?>
<option value="<?=$iface;?>" <?=($iface==$interfacefilter)?"selected=\"selected\"":"";?>><?=htmlspecialchars($ifacename);?></option>
<?php endforeach; ?>
</select>
</div>
</td>
<td>
<div align="center" style="vertical-align:top;"><?=gettext("Filter expression");?></div>
<div align="center" style="vertical-align:top;"><input id="filtertext" name="filtertext" class="formfld search" style="vertical-align:top;" type="text" size="35" value="<?=$filtertext?>" /></div>
</td>
<td>
<div align="center" style="vertical-align:top;"><?=gettext("Quantity");?></div>
<div align="center" style="vertical-align:top;"><input id="filterlogentries_qty" name="filterlogentries_qty" class="" style="vertical-align:top;" type="text" size="6" value="<?= $filterlogentries_qty ?>" /></div>
</td>
<td>
<div align="center" style="vertical-align:top;">&nbsp;</div>
<div align="center" style="vertical-align:top;"><input id="filtersubmit" name="filtersubmit" type="submit" class="formbtn" style="vertical-align:top;" value="<?=gettext("Filter");?>" /></div>
</td>
</tr>
<tr>
<td></td>
<td colspan="2">
<?printf(gettext('Matches %1$s regular expression%2$s.'), '<a target="_blank" href="http://www.php.net/manual/en/book.pcre.php">', '</a>');?>&nbsp;&nbsp;
</td>
</tr>
</table>
</form>
<div style="float: right; vertical-align:middle">
<br />
<?php if (!isset($config['syslog']['rawfilter']) && (isset($config['syslog']['filterdescriptions']) && $config['syslog']['filterdescriptions'] === "2")):?>
<a href="#" onclick="toggleListDescriptions()">Show/hide rule descriptions</a>
<?php endif;?>
</div>
</div>
</div>
</div>
</section>
-->
<section class="col-xs-12">
<div class="tab-content content-box col-xs-12">
......@@ -277,16 +581,16 @@ include("head.inc");
<?php if (!isset($config['syslog']['rawfilter'])):
$iflist = get_configured_interface_with_descr(false, true);
if ($iflist[$interfacefilter])
if (isset($iflist[$interfacefilter]))
$interfacefilter = $iflist[$interfacefilter];
if ($filtersubmit)
if (isset($filtersubmit))
$filterlog = conv_log_filter($filter_logfile, $nentries, $nentries + 100, $filterfieldsarray);
else
$filterlog = conv_log_filter($filter_logfile, $nentries, $nentries + 100, $filtertext, $interfacefilter);
?>
<tr>
<td colspan="<?=$config['syslog']['filterdescriptions']==="1"?7:6?>" class="listtopic">
<td colspan="<?=isset($config['syslog']['filterdescriptions']) && $config['syslog']['filterdescriptions']==="1"?7:6?>" class="listtopic">
<?php if ( (!$filtertext) && (!$filterfieldsarray) )
printf(gettext("Last %s firewall log entries."),count($filterlog));
else
......@@ -298,7 +602,7 @@ include("head.inc");
<td width="50" class="listhdrr"><?=gettext("Act");?></td>
<td class="listhdrr"><?=gettext("Time");?></td>
<td class="listhdrr"><?=gettext("If");?></td>
<?php if ($config['syslog']['filterdescriptions'] === "1"):?>
<?php if (isset($config['syslog']['filterdescriptions']) && $config['syslog']['filterdescriptions'] === "1"):?>
<td width="10%" class="listhdrr"><?=gettext("Rule");?></td>
<?php endif;?>
<td class="listhdrr"><?=gettext("Source");?></td>
......@@ -306,7 +610,7 @@ include("head.inc");
<td class="listhdrr"><?=gettext("Proto");?></td>
</tr>
<?php
if ($config['syslog']['filterdescriptions'])
if (isset($config['syslog']['filterdescriptions']))
buffer_rules_load();
$rowIndex = 0;
foreach ($filterlog as $filterent):
......@@ -315,7 +619,7 @@ include("head.inc");
<tr class="<?=$evenRowClass?>">
<td class="listMRlr nowrap" align="center" sorttable_customkey="<?=$filterent['act']?>">
<a onclick="javascript:getURL('diag_logs_filter.php?getrulenum=<?php echo "{$filterent['rulenum']},{$filterent['act']}"; ?>', outputrule);" title="<?php echo $filterent['act'] .'/';?>"><span class="glyphicon glyphicon-remove"></span></a></td>
<?php if ($filterent['count']) echo $filterent['count'];?></a></center></td>
<?php if (isset($filterent['count'])) echo $filterent['count'];?></a></center></td>
<td class="listMRr nowrap"><?php echo htmlspecialchars($filterent['time']);?></td>
<td class="listMRr nowrap">
<?php if ($filterent['direction'] == "out"): ?>
......@@ -323,7 +627,7 @@ include("head.inc");
<?php endif; ?>
<?php echo htmlspecialchars($filterent['interface']);?></td>
<?php
if ($config['syslog']['filterdescriptions'] === "1")
if (isset($config['syslog']['filterdescriptions']) && $config['syslog']['filterdescriptions'] === "1")
echo("<td class=\"listMRr nowrap\">".find_rule_by_number_buffer($filterent['rulenum'],$filterent['act'])."</td>");
$int = strtolower($filterent['interface']);
......@@ -335,21 +639,32 @@ include("head.inc");
} else {
$ipproto = "inet";
}
if (!isset($filterent['srcport'])) $filterent['srcport'] = null ;
$srcstr = $filterent['srcip'] . get_port_with_service($filterent['srcport'], $proto);
$src_htmlclass = str_replace(array('.', ':'), '-', $filterent['srcip']);
if (!isset($filterent['dstport'])) $filterent['dstport'] = null ;
$dststr = $filterent['dstip'] . get_port_with_service($filterent['dstport'], $proto);
$dst_htmlclass = str_replace(array('.', ':'), '-', $filterent['dstip']);
?>
<td class="listMRr nowrap">
<span onclick="javascript:resolve_with_ajax('<?php echo "{$filterent['srcip']}"; ?>');" title="<?=gettext("Click to resolve");?>" class="ICON-<?= $src_htmlclass; ?>" alt="Icon Reverse Resolve with DNS"><span class="btn btn-default btn-xs glyphicon glyphicon-info-sign"></span></span>
<a class="btn btn-danger btn-xs" href="easyrule.php?<?php echo "action=block&amp;int={$int}&amp;src={$filterent['srcip']}&amp;ipproto={$ipproto}"; ?>" title="<?=gettext("Easy Rule: Add to Block List");?>" onclick="return confirm('<?=gettext("Do you really want to add this BLOCK rule?")."\n\n".gettext("Easy Rule is still experimental.")."\n".gettext("Continue at risk of your own peril.")."\n".gettext("Backups are also nice.")?>')">
<a title="<?=gettext("Easy Rule: Add to Block List");?>" href="#blockEasy" class="btn btn-danger btn-xs easy_block">
<input type="hidden" value="<?= $filterent['srcip']; ?>" id="srcip"/>
<input type="hidden" value="<?= $int;?>" id="intf"/>
<input type="hidden" value="<?= $ipproto;?>" id="ipproto"/>
<span class="glyphicon glyphicon-remove" alt="Icon Easy Rule: Add to Block List"></span></a>
<?php echo $srcstr . '<span class="RESOLVE-' . $src_htmlclass . '"></span>';?>
</td>
<td class="listMRr nowrap">
<span onclick="javascript:resolve_with_ajax('<?php echo "{$filterent['dstip']}"; ?>');" title="<?=gettext("Click to resolve");?>" class="ICON-<?= $dst_htmlclass; ?>" alt="Icon Reverse Resolve with DNS"><span class="btn btn-default btn-xs glyphicon glyphicon-info-sign"></span></span>
<a class="btn btn-success btn-xs" href="easyrule.php?<?php echo "action=pass&amp;int={$int}&amp;proto={$proto}&amp;src={$filterent['srcip']}&amp;dst={$filterent['dstip']}&amp;dstport={$filterent['dstport']}&amp;ipproto={$ipproto}"; ?>" title="<?=gettext("Easy Rule: Pass this traffic");?>" onclick="return confirm('<?=gettext("Do you really want to add this PASS rule?")."\n\n".gettext("Easy Rule is still experimental.")."\n".gettext("Continue at risk of your own peril.")."\n".gettext("Backups are also nice.");?>')">
<a title="<?=gettext("Easy Rule: Pass this traffic");?>" href="#blockEasy" class="btn btn-success btn-xs easy_pass">
<input type="hidden" value="<?= $filterent['srcip']; ?>" id="srcip"/>
<input type="hidden" value="<?= $filterent['dstip']; ?>" id="dstip"/>
<input type="hidden" value="<?= $filterent['dstport']; ?>" id="dstport"/>
<input type="hidden" value="<?= $int;?>" id="intf"/>
<input type="hidden" value="<?= $proto;?>" id="proto"/>
<input type="hidden" value="<?= $ipproto;?>" id="ipproto"/>
<span class="glyphicon glyphicon-play" alt="Icon Easy Rule: Pass this traffic"></span></a>
<?php echo $dststr . '<span class="RESOLVE-' . $dst_htmlclass . '"></span>';?>
</td>
......@@ -405,6 +720,49 @@ include("head.inc");
<!-- AJAXY STUFF -->
<script type="text/javascript">
//<![CDATA[
$( document ).ready(function() {
$(".easy_block").click(function(){
$.ajax(
"/diag_logs_filter.php",
{
type: 'post',
dataType: 'json',
data: {
easyrule:'block',
srcip:$(this).find('#srcip').val(),
ipproto:$(this).find('#ipproto').val(),
intf:$(this).find('#intf').val()
},
complete: function(data,status) {
alert("done");
},
});
});
$(".easy_pass").click(function(){
$.ajax(
"/diag_logs_filter.php",
{
type: 'post',
dataType: 'json',
data: {
easyrule:'pass',
srcip:$(this).find('#srcip').val(),
dstip:$(this).find('#dstip').val(),
dstport:$(this).find('#dstport').val(),
proto:$(this).find('#proto').val(),
ipproto:$(this).find('#ipproto').val(),
intf:$(this).find('#intf').val()
},
complete: function(data,status) {
alert("done");
},
});
});
});
function resolve_with_ajax(ip_to_resolve) {
var url = "/diag_logs_filter.php";
......
......@@ -39,7 +39,7 @@ $nentries = 50;
/* AJAX related routines */
handle_ajax($nentries, $nentries + 20);
if ($_POST['clear']) {
if (isset($_POST['clear'])) {
clear_log_file($filter_logfile);
}
......
......@@ -70,7 +70,7 @@ function stat_block($summary, $stat, $num) {
$total = 0;
$numentries = 0;
for ($i=0; $i < $num; $i++) {
if ($k[$i]) {
if (isset($k[$i])) {
$total += $summary[$stat][$k[$i]];
$numentries++;
$outstr = $k[$i];
......@@ -101,7 +101,7 @@ function pie_block($summary, $stat, $num) {
print "\n<script type=\"text/javascript\">\n";
print "//<![CDATA[\n";
for ($i=0; $i < $num; $i++) {
if ($k[$i]) {
if (isset($k[$i])) {
$total += $summary[$stat][$k[$i]];
$numentries++;
print "var d{$stat}{$i} = [];\n";
......@@ -118,7 +118,7 @@ function pie_block($summary, $stat, $num) {
print " new Proto.Chart($('piechart{$stat}'),\n";
print " [\n";
for ($i=0; $i < $num; $i++) {
if ($k[$i]) {
if (isset($k[$i])) {
print " { data: d{$stat}{$i}, label: \"{$k[$i]}\"}";
if (!(($i == ($numentries - 1)) && ($leftover <= 0)))
print ",\n";
......@@ -143,20 +143,17 @@ function pie_block($summary, $stat, $num) {
}
foreach ($filterlog as $fe) {
$specialfields = array('srcport', 'dstport');
foreach (array_keys($fields) as $field) {
if (!in_array($field, $specialfields))
if (isset($fe[$field])) {
if (!isset($summary[$field])) {
$summary[$field] = array();
}
if (!isset($summary[$field][$fe[$field]])) {
$summary[$field][$fe[$field]] = 0;
}
$summary[$field][$fe[$field]]++;
}
/* Handle some special cases */
if ($fe['srcport'])
$summary['srcport'][$fe['proto'].'/'.$fe['srcport']]++;
else
$summary['srcport'][$fe['srcport']]++;
if ($fe['dstport'])
$summary['dstport'][$fe['proto'].'/'.$fe['dstport']]++;
else
$summary['dstport'][$fe['dstport']]++;
}
}
include("head.inc"); ?>
......
<?php
/*
Copyritgh (C) 2014 Deciso B.V.
Copyright (C) 2009-2010 Jim Pingle (jpingle@gmail.com)
Originally Sponsored By Anathematic @ pfSense Forums
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.
*/
$pgtitle = gettext("Firewall: EasyRule");
require_once("guiconfig.inc");
require_once("easyrule.inc");
require_once("filter.inc");
$retval = 0;
$message = "";
$specialsrcdst = explode(" ", "any pptp pppoe l2tp openvpn");
if ($_GET && isset($_GET['action'])) {
switch ($_GET['action']) {
case 'block':
/* Check that we have a valid host */
easyrule_parse_block($_GET['int'], $_GET['src'], $_GET['ipproto']);
break;
case 'pass':
easyrule_parse_pass($_GET['int'], $_GET['proto'], $_GET['src'], $_GET['dst'], $_GET['dstport'], $_GET['ipproto']);
break;
}
}
if(stristr($retval, "error") == true)
$message = $retval;
include("head.inc"); ?>
<body link="#000000" vlink="#000000" alink="#000000">
<?php include("fbegin.inc"); ?>
<section class="page-content-main">
<div class="container-fluid col-xs-12 col-sm-10 col-md-9">
<div class="row">
<section class="col-xs-12">
<?php if (isset($input_errors) && count($input_errors) > 0) print_input_errors($input_errors); ?>
<?php if ($message) { ?>
<br />
<?=gettext("Message"); ?>: <?php echo $message; ?>
<br />
<?php } else { ?>
<?=gettext("This is the Easy Rule status page, mainly used to display errors when adding rules. " .
"If you are seeing this, there apparently was not an error, and you navigated to the " .
"page directly without telling it what to do"); ?>.<br /><br />
<?=gettext("This page is meant to be called from the block/pass buttons on the Firewall Logs page"); ?>, <a href="diag_logs_filter.php"><?=gettext("Status"); ?> &gt; <?=gettext("System Logs, " .
"Firewall Tab"); ?></a>.
<br />
<?php } ?>
</section>
</div>
</div>
</section>
<?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