. 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. */ /* pfSense_MODULE: nat */ ##|+PRIV ##|*IDENT=page-firewall-nat-portforward-edit ##|*NAME=Firewall: NAT: Port Forward: Edit page ##|*DESCR=Allow access to the 'Firewall: NAT: Port Forward: Edit' page. ##|*MATCH=firewall_nat_edit.php* ##|-PRIV require("guiconfig.inc"); require_once("itemid.inc"); require("filter.inc"); require("shaper.inc"); if (!is_array($config['nat']['rule'])) { $config['nat']['rule'] = array(); } $a_nat = &$config['nat']['rule']; $id = $_GET['id']; if (isset($_POST['id'])) $id = $_POST['id']; if (isset($_GET['dup'])) { $id = $_GET['dup']; $after = $_GET['dup']; } if (isset($id) && $a_nat[$id]) { $pconfig['extaddr'] = $a_nat[$id]['external-address']; $pconfig['proto'] = $a_nat[$id]['protocol']; list($pconfig['beginport'],$pconfig['endport']) = explode("-", $a_nat[$id]['external-port']); if(!$pconfig['endport']) $pconfig['endport'] = $pconfig['beginport']; $pconfig['localip'] = $a_nat[$id]['target']; $pconfig['localbeginport'] = $a_nat[$id]['local-port']; $pconfig['descr'] = $a_nat[$id]['descr']; $pconfig['interface'] = $a_nat[$id]['interface']; $pconfig['associated-filter-rule-id'] = $a_nat[$id]['associated-filter-rule-id']; $pconfig['nosync'] = isset($a_nat[$id]['nosync']); if (!$pconfig['interface']) $pconfig['interface'] = "wan"; } else { $pconfig['interface'] = "wan"; } if (isset($_GET['dup'])) unset($id); /* run through $_POST items encoding HTML entties so that the user * cannot think he is slick and perform a XSS attack on the unwilling */ foreach ($_POST as $key => $value) { $temp = $value; $newpost = htmlentities($temp); if($newpost <> $temp) $input_errors[] = "Invalid characters detected ($temp). Please remove invalid characters and save again."; } if ($_POST) { if ($_POST['beginport_cust'] && !$_POST['beginport']) $_POST['beginport'] = $_POST['beginport_cust']; if ($_POST['endport_cust'] && !$_POST['endport']) $_POST['endport'] = $_POST['endport_cust']; if ($_POST['localbeginport_cust'] && !$_POST['localbeginport']) $_POST['localbeginport'] = $_POST['localbeginport_cust']; if (!$_POST['endport']) $_POST['endport'] = $_POST['beginport']; /* Make beginning port end port if not defined and endport is */ if (!$_POST['beginport'] && $_POST['endport']) $_POST['beginport'] = $_POST['endport']; unset($input_errors); $pconfig = $_POST; /* input validation */ if(strtoupper($_POST['proto']) == "TCP" or strtoupper($_POST['proto']) == "UDP" or strtoupper($_POST['proto']) == "TCP/UDP") { $reqdfields = explode(" ", "interface proto beginport endport localip localbeginport"); $reqdfieldsn = explode(",", "Interface,Protocol,External port from,External port to,NAT IP,Local port"); } else { $reqdfields = explode(" ", "interface proto localip"); $reqdfieldsn = explode(",", "Interface,Protocol,NAT IP"); } do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors); if (($_POST['localip'] && !is_ipaddroralias($_POST['localip']))) { $input_errors[] = "\"{$_POST['localip']}\" is not valid NAT IP address or host alias."; } /* only validate the ports if the protocol is TCP, UDP or TCP/UDP */ if(strtoupper($_POST['proto']) == "TCP" or strtoupper($_POST['proto']) == "UDP" or strtoupper($_POST['proto']) == "TCP/UDP") { if ($_POST['beginport'] && !is_portoralias($_POST['beginport'])) { $input_errors[] = "The start port must be an integer between 1 and 65535."; } if ($_POST['endport'] && !is_portoralias($_POST['endport'])) { $input_errors[] = "The end port must be an integer between 1 and 65535."; } if ($_POST['localbeginport'] && !is_portoralias($_POST['localbeginport'])) { $input_errors[] = "The local port must be an integer between 1 and 65535."; } if ($_POST['beginport'] > $_POST['endport']) { /* swap */ $tmp = $_POST['endport']; $_POST['endport'] = $_POST['beginport']; $_POST['beginport'] = $tmp; } if (!$input_errors) { if (($_POST['endport'] - $_POST['beginport'] + $_POST['localbeginport']) > 65535) $input_errors[] = "The target port range must be an integer between 1 and 65535."; } } /* check for overlaps */ foreach ($a_nat as $natent) { if (isset($id) && ($a_nat[$id]) && ($a_nat[$id] === $natent)) continue; if ($natent['interface'] != $_POST['interface']) continue; if ($natent['external-address'] != $_POST['extaddr']) continue; if (($natent['proto'] != $_POST['proto']) && ($natent['proto'] != "tcp/udp") && ($_POST['proto'] != "tcp/udp")) continue; list($begp,$endp) = explode("-", $natent['external-port']); if (!$endp) $endp = $begp; if (!( (($_POST['beginport'] < $begp) && ($_POST['endport'] < $begp)) || (($_POST['beginport'] > $endp) && ($_POST['endport'] > $endp)))) { $input_errors[] = "The external port range overlaps with an existing entry."; break; } } if (!$input_errors) { $natent = array(); if ($_POST['extaddr']) $natent['external-address'] = $_POST['extaddr']; $natent['protocol'] = $_POST['proto']; if ($_POST['beginport'] == $_POST['endport']) $natent['external-port'] = $_POST['beginport']; else $natent['external-port'] = $_POST['beginport'] . "-" . $_POST['endport']; $natent['target'] = $_POST['localip']; $natent['local-port'] = $_POST['localbeginport']; $natent['interface'] = $_POST['interface']; $natent['descr'] = $_POST['descr']; $natent['associated-filter-rule-id'] = $_POST['associated-filter-rule-id']; if($_POST['filter-rule-association'] == "pass") $natent['associated-filter-rule-id'] = "pass"; if($_POST['nosync'] == "yes") $natent['nosync'] = true; else unset($natent['nosync']); // If we used to have an associated filter rule, but no-longer should have one if( $a_nat[$id]>0 && ($natent['associated-filter-rule-id']>0)===false ) { // Delete the previous rule delete_id($a_nat[$id]['associated-filter-rule-id'], $config['filter']['rule']); mark_subsystem_dirty('filter'); } $need_filter_rule = false; // Updating a rule with a filter rule associated if( $natent['associated-filter-rule-id']>0 ) $need_filter_rule = true; // Create a rule or if we want to create a new one if( $natent['associated-filter-rule-id']=='new' ) { $need_filter_rule = true; unset( $natent['associated-filter-rule-id'] ); $_POST['filter-rule-association']='add-associated'; } // If creating a new rule, where we want to add the filter rule, associated or not else if( isset($_POST['filter-rule-association']) && ($_POST['filter-rule-association']=='add-associated' || $_POST['filter-rule-association']=='add-unassociated') ) $need_filter_rule = true; // Determine NAT entry ID now, we need it for the firewall rule if (isset($id) && $a_nat[$id]) $a_nat[$id] = $natent; else { if (is_numeric($after)) $id = $after + 1; else $id = count($a_nat); } if ($need_filter_rule) { /* auto-generate a matching firewall rule */ $filterent = array(); // If a rule already exists, load it if( $natent['associated-filter-rule-id'] > 0 ) $filterent = &get_id($natent['associated-filter-rule-id'], $config['filter']['rule']); else // Create the default source entry for new filter entries $filterent['source']['any'] = ""; // Update associated nat rule ID $filterent['associated-nat-rule-id'] = $id; // Update interface, protocol and destination $filterent['interface'] = $_POST['interface']; $filterent['protocol'] = $_POST['proto']; $filterent['destination']['address'] = $_POST['localip']; $dstpfrom = $_POST['localbeginport']; $dstpto = $dstpfrom + $_POST['endport'] - $_POST['beginport']; if ($dstpfrom == $dstpto) $filterent['destination']['port'] = $dstpfrom; else $filterent['destination']['port'] = $dstpfrom . "-" . $dstpto; $filterent['descr'] = "NAT " . $_POST['descr']; /* * Our firewall filter description may be no longer than * 63 characters, so don't let it be. */ $filterent['descr'] = substr("NAT " . $_POST['descr'], 0, 59); // If this is a new rule, create an ID and add the rule if( $_POST['filter-rule-association']=='add-associated' ) { $natent['associated-filter-rule-id'] = $filterent['id'] = get_next_id($config['filter']['rule']); $config['filter']['rule'][] = $filterent; } mark_subsystem_dirty('filter'); } // Update the NAT entry now if (isset($id) && $a_nat[$id]) $a_nat[$id] = $natent; else { if (is_numeric($after)) array_splice($a_nat, $after+1, 0, array($natent)); else $a_nat[] = $natent; } mark_subsystem_dirty('natconf'); write_config(); header("Location: firewall_nat.php"); exit; } } $pgtitle = array("Firewall","NAT","Port Forward: Edit"); include("head.inc"); ?>
"") foreach($config['aliases']['alias'] as $alias_name) { switch ($alias_name['type']) { case "port": if($isfirst == 1) $portaliases .= ","; $portaliases .= "'" . $alias_name['name'] . "'"; $isfirst = 1; break; case "host": case "network": case "openvpn": if($addrisfirst == 1) $aliasesaddr .= ","; $aliasesaddr .= "'" . $alias_name['name'] . "'"; $addrisfirst = 1; break; default: break; } } ?>