* Copyright (C) 2003-2004 Manuel Kasper * 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. */ require_once("guiconfig.inc"); require_once("filter.inc"); require_once("system.inc"); /*********************************************************************************************************** * format functions for this page ***********************************************************************************************************/ function firewall_rule_item_proto($filterent) { // construct line ipprotocol if (isset($filterent['ipprotocol'])) { switch($filterent['ipprotocol']) { case "inet": $record_ipprotocol = "IPv4 "; break; case "inet6": $record_ipprotocol = "IPv6 "; break; case "inet46": $record_ipprotocol = "IPv4+6 "; break; } } else { // when ipprotocol is not set, pf would normally figure out the ip proto itself. // reconstruct ipproto depending on source/destination address. if (!empty($filterent['from']) && is_ipaddr(explode("/", $filterent['from'])[0])) { $record_ipprotocol = strpos($filterent['from'], ":") === false ? "IPv4 " : "IPv6 "; } elseif (!empty($filterent['to']) && is_ipaddr(explode("/", $filterent['to'])[0])) { $record_ipprotocol = strpos($filterent['to'], ":") === false ? "IPv4 " : "IPv6 "; } elseif (isset($filterent['source']['address']) && is_ipaddr(explode("/", $filterent['source']['address'])[0])) { $record_ipprotocol = strpos($filterent['source']['address'], ":") === false ? "IPv4 " : "IPv6 "; } elseif (isset($filterent['destination']['address']) && is_ipaddr(explode("/", $filterent['destination']['address'])[0])) { $record_ipprotocol = strpos($filterent['destination']['address'], ":") === false ? "IPv4 " : "IPv6 "; } else { $record_ipprotocol = "IPv4+6 "; } } $icmptypes = array( "" => gettext("any"), "echoreq" => gettext("Echo Request"), "echorep" => gettext("Echo Reply"), "unreach" => gettext("Destination Unreachable"), "squench" => gettext("Source Quench (Deprecated)"), "redir" => gettext("Redirect"), "althost" => gettext("Alternate Host Address (Deprecated)"), "routeradv" => gettext("Router Advertisement"), "routersol" => gettext("Router Solicitation"), "timex" => gettext("Time Exceeded"), "paramprob" => gettext("Parameter Problem"), "timereq" => gettext("Timestamp"), "timerep" => gettext("Timestamp Reply"), "inforeq" => gettext("Information Request (Deprecated)"), "inforep" => gettext("Information Reply (Deprecated)"), "maskreq" => gettext("Address Mask Request (Deprecated)"), "maskrep" => gettext("Address Mask Reply (Deprecated)") ); $icmp6types = array( "" => gettext("any"), "unreach" => gettext("Destination unreachable"), "toobig" => gettext("Packet too big"), "timex" => gettext("Time exceeded"), "paramprob" => gettext("Invalid IPv6 header"), "echoreq" => gettext("Echo service request"), "echorep" => gettext("Echo service reply"), "groupqry" => gettext("Group membership query"), "listqry" => gettext("Multicast listener query"), "grouprep" => gettext("Group membership report"), "listenrep" => gettext("Multicast listener report"), "groupterm" => gettext("Group membership termination"), "listendone" => gettext("Multicast listener done"), "routersol" => gettext("Router solicitation"), "routeradv" => gettext("Router advertisement"), "neighbrsol" => gettext("Neighbor solicitation"), "neighbradv" => gettext("Neighbor advertisement"), "redir" => gettext("Shorter route exists"), "routrrenum" => gettext("Route renumbering"), "fqdnreq" => gettext("FQDN query"), "niqry" => gettext("Node information query"), "wrureq" => gettext("Who-are-you request"), "fqdnrep" => gettext("FQDN reply"), "nirep" => gettext("Node information reply"), "wrurep" => gettext("Who-are-you reply"), "mtraceresp" => gettext("mtrace response"), "mtrace" => gettext("mtrace messages") ); if (isset($filterent['protocol']) && $filterent['protocol'] == "icmp" && !empty($filterent['icmptype'])) { $result = $record_ipprotocol; $result .= sprintf( " %s ", html_safe($icmptypes[$filterent['icmptype']]), isset($filterent['protocol']) ? strtoupper($filterent['protocol']) : "*" ); return $result; } elseif (isset($filterent['protocol']) && !empty($filterent['icmp6-type'])) { $result = $record_ipprotocol; $result .= sprintf( " %s ", html_safe($icmp6types[$filterent['icmp6-type']]), isset($filterent['protocol']) ? strtoupper($filterent['protocol']) : "*" ); return $result; } else { return $record_ipprotocol . (isset($filterent['protocol']) ? strtoupper($filterent['protocol']) : "*"); } } function firewall_rule_item_icons($filterent) { $result = ""; if (empty($filterent['direction']) || $filterent['direction'] == "in") { $result .= sprintf( "", gettext("in") ); } elseif (!empty($filterent['direction']) && $filterent['direction'] == "out") { $result .= sprintf( "", gettext("out") ); } else { $result .= sprintf( "", gettext("any") ); } if (empty($filterent['floating']) && $filterent['quick'] === null){ $is_quick = true; } elseif (!empty($filterent['floating']) && $filterent['quick'] === null) { $is_quick = false; } else { $is_quick = $filterent['quick']; } if ($is_quick) { $result .= sprintf( "", gettext('first match') ); } else { $result .= sprintf( "", gettext('last match') ); } return $result; } function firewall_rule_item_action($filterent) { if ($filterent['type'] == "block" && empty($filterent['disabled'])) { return "fa fa-times fa-fw text-danger"; } elseif ($filterent['type'] == "block" && !empty($filterent['disabled'])) { return "fa fa-times fa-fw text-muted"; } elseif ($filterent['type'] == "reject" && empty($filterent['disabled'])) { return "fa fa-times-circle fa-fw text-danger"; } elseif ($filterent['type'] == "reject" && !empty($filterent['disabled'])) { return "fa fa-times-circle fa-fw text-muted"; } elseif (empty($filterent['disabled'])) { return "fa fa-play fa-fw text-success"; } else { return "fa fa-play fa-fw text-muted"; } } function firewall_rule_item_log($filterent) { if ($filterent['log'] == true) { return "fa fa-info-circle fa-fw text-info"; } else { return "fa fa-info-circle fa-fw text-muted"; } } /*********************************************************************************************************** * ***********************************************************************************************************/ $a_filter = &config_read_array('filter', 'rule'); if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (isset($_GET['if'])) { $current_if = htmlspecialchars($_GET['if']); } else { $current_if = "FloatingRules"; } $pconfig = $_POST; if (isset($pconfig['id']) && isset($a_filter[$pconfig['id']])) { // id found and valid $id = $pconfig['id']; } if (isset($pconfig['act']) && $pconfig['act'] == "apply") { system_cron_configure(); filter_configure(); clear_subsystem_dirty('filter'); $savemsg = get_std_save_message(); } elseif (isset($pconfig['act']) && $pconfig['act'] == 'del' && isset($id)) { // delete single item if (!empty($a_filter[$id]['associated-rule-id'])) { // unlink nat entry if (isset($config['nat']['rule'])) { $a_nat = &config_read_array('nat', 'rule'); foreach ($a_nat as &$natent) { if ($natent['associated-rule-id'] == $a_filter[$id]['associated-rule-id']) { $natent['associated-rule-id'] = ''; } } } } unset($a_filter[$id]); write_config(); mark_subsystem_dirty('filter'); header(url_safe('Location: /firewall_rules.php?if=%s', array($current_if))); exit; } elseif (isset($pconfig['act']) && $pconfig['act'] == 'del_x' && isset($pconfig['rule']) && count($pconfig['rule']) > 0) { // delete selected rules foreach ($pconfig['rule'] as $rulei) { // unlink nat entry if (isset($config['nat']['rule'])) { $a_nat = &config_read_array('nat', 'rule'); foreach ($a_nat as &$natent) { if ($natent['associated-rule-id'] == $a_filter[$rulei]['associated-rule-id']) { $natent['associated-rule-id'] = ''; } } } unset($a_filter[$rulei]); } write_config(); mark_subsystem_dirty('filter'); header(url_safe('Location: /firewall_rules.php?if=%s', array($current_if))); exit; } elseif (isset($pconfig['act']) && in_array($pconfig['act'], array('toggle_enable', 'toggle_disable')) && isset($pconfig['rule']) && count($pconfig['rule']) > 0) { foreach ($pconfig['rule'] as $rulei) { $a_filter[$rulei]['disabled'] = $pconfig['act'] == 'toggle_disable'; } write_config(); mark_subsystem_dirty('filter'); header(url_safe('Location: /firewall_rules.php?if=%s', array($current_if))); exit; } elseif ( isset($pconfig['act']) && $pconfig['act'] == 'move' && isset($pconfig['rule']) && count($pconfig['rule']) > 0) { // move selected rules if (!isset($id)) { // if rule not set/found, move to end $id = count($a_filter); } $a_filter = legacy_move_config_list_items($a_filter, $id, $pconfig['rule']); write_config(); mark_subsystem_dirty('filter'); header(url_safe('Location: /firewall_rules.php?if=%s', array($current_if))); exit; } elseif (isset($pconfig['act']) && $pconfig['act'] == 'toggle' && isset($id)) { // toggle item if(isset($a_filter[$id]['disabled'])) { unset($a_filter[$id]['disabled']); } else { $a_filter[$id]['disabled'] = true; } write_config(); mark_subsystem_dirty('filter'); $response = array("id" => $id); $response["new_label"] = !isset($a_filter[$id]['disabled']) ? gettext("Disable Rule") : gettext("Enable Rule"); $response["new_state"] = !isset($a_filter[$id]['disabled']) ; echo json_encode($response); exit; } elseif (isset($pconfig['act']) && $pconfig['act'] == 'log' && isset($id)) { // toggle logging if(isset($a_filter[$id]['log'])) { unset($a_filter[$id]['log']); } else { $a_filter[$id]['log'] = true; } write_config(); mark_subsystem_dirty('filter'); //header(url_safe('Location: /firewall_rules.php?if=%s', array($current_if))); $response = array("id" => $id); $response["new_label"] = isset($a_filter[$id]['log']) ? gettext("Disable Log") : gettext("Enable Log"); $response["new_state"] = isset($a_filter[$id]['log']) ; echo json_encode($response); exit; } } $selected_if = 'FloatingRules'; if (isset($_GET['if'])) { $selected_if = htmlspecialchars($_GET['if']); } $selected_category = []; if (isset($_GET['category'])) { $selected_category = !is_array($_GET['category']) ? array($_GET['category']) : $_GET['category']; } include("head.inc"); $main_buttons = array( array('label' => gettext('Add'), 'href' => 'firewall_rules_edit.php?if=' . $selected_if), ); legacy_html_escape_form_data($a_filter); $all_rule_stats = json_decode(configd_run("filter rule stats"), true); ?>
">
$filterent) { if ((!isset($filterent['floating']) && $selected_if == $filterent['interface']) || ((isset($filterent['floating']) || empty($filterent['interface'])) && $selected_if == 'FloatingRules')) { $interface_has_rules = true; break; } } ?>
iterateFilterRules() as $rule): $is_selected = $rule->getInterface() == $selected_if || ( $rule->getInterface() == "" && $selected_if == "FloatingRules" ); if ($rule->isEnabled() && $is_selected): $filterent = $rule->getRawRule(); $filterent['quick'] = !isset($filterent['quick']) || $filterent['quick']; legacy_html_escape_form_data($filterent); $rule_stats = !empty($rule->getLabel()) ? $all_rule_stats[$rule->getLabel()] : array();?> $filterent): if ( (!isset($filterent['floating']) && $selected_if == $filterent['interface']) || ( (isset($filterent['floating']) || empty($filterent['interface'])) && $selected_if == 'FloatingRules' ) ): // calculate a hash so we can track these records in the ruleset, new style (mvc) code will // automatically provide us with a uuid, this is a workaround to provide some help with tracking issues. $rule_hash = OPNsense\Firewall\Util::calcRuleHash($filterent); ?> " data-category="">
 
"> ">   " data-toggle="tooltip">