#!/usr/local/bin/php . 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("guiconfig.inc"); if ($ipv6rules = ($_GET['type'] == 'ipv6')) { $configname = 'rule6'; $typelink = '&type=ipv6'; $maxnetmask = 128; } else { $configname = 'rule'; $typelink = ''; $maxnetmask = 32; } $pgtitle = array("Firewall", "Rules", "Edit"); /* make group manager happy */ $pgtitle = array("Firewall", ipv6enabled() ? ($ipv6rules ? 'IPv6 Rules' : 'IPv4 Rules') : 'Rules', "Edit"); $specialsrcdst = explode(" ", "any wanip lan pptp"); if (!is_array($config['filter'][$configname])) { $config['filter'][$configname] = array(); } filter_rules_sort(); $a_filter = &$config['filter'][$configname]; $id = $_GET['id']; if (is_numeric($_POST['id'])) $id = $_POST['id']; $after = $_GET['after']; if (isset($_POST['after'])) $after = $_POST['after']; if (isset($_GET['dup'])) { $id = $_GET['dup']; $after = $_GET['dup']; } function is_specialnet($net) { global $specialsrcdst; if (in_array($net, $specialsrcdst) || (strstr($net, "opt") && !is_alias($net))) return true; else return false; } function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) { global $maxnetmask; if (isset($adr['any'])) $padr = "any"; else if ($adr['network']) $padr = $adr['network']; else if ($adr['address']) { list($padr, $pmask) = explode("/", $adr['address']); if (!$pmask) $pmask = $maxnetmask; } if (isset($adr['not'])) $pnot = 1; else $pnot = 0; if ($adr['port']) { list($pbeginport, $pendport) = explode("-", $adr['port']); if (!$pendport) $pendport = $pbeginport; } else { $pbeginport = "any"; $pendport = "any"; } } function pconfig_to_address(&$adr, $padr, $pmask, $pnot, $pbeginport, $pendport) { global $maxnetmask; $adr = array(); if ($padr == "any") $adr['any'] = true; else if (is_specialnet($padr)) $adr['network'] = $padr; else { $adr['address'] = $padr; if ($pmask != $maxnetmask) $adr['address'] .= "/" . $pmask; } $adr['not'] = $pnot ? true : false; if (($pbeginport != 0) && ($pbeginport != "any")) { if ($pbeginport != $pendport) $adr['port'] = $pbeginport . "-" . $pendport; else $adr['port'] = $pbeginport; } } if (isset($id) && $a_filter[$id]) { $pconfig['interface'] = $a_filter[$id]['interface']; if (!isset($a_filter[$id]['type'])) $pconfig['type'] = "pass"; else $pconfig['type'] = $a_filter[$id]['type']; if (isset($a_filter[$id]['protocol'])) $pconfig['proto'] = $a_filter[$id]['protocol']; else $pconfig['proto'] = "any"; if ($a_filter[$id]['protocol'] == "icmp" || $a_filter[$id]['protocol'] == "ipv6-icmp") $pconfig['icmptype'] = $a_filter[$id]['icmptype']; address_to_pconfig($a_filter[$id]['source'], $pconfig['src'], $pconfig['srcmask'], $pconfig['srcnot'], $pconfig['srcbeginport'], $pconfig['srcendport']); address_to_pconfig($a_filter[$id]['destination'], $pconfig['dst'], $pconfig['dstmask'], $pconfig['dstnot'], $pconfig['dstbeginport'], $pconfig['dstendport']); $pconfig['disabled'] = isset($a_filter[$id]['disabled']); $pconfig['log'] = isset($a_filter[$id]['log']); $pconfig['frags'] = isset($a_filter[$id]['frags']); $pconfig['descr'] = $a_filter[$id]['descr']; } else { /* defaults */ if ($_GET['if']) $pconfig['interface'] = $_GET['if']; $pconfig['type'] = "pass"; $pconfig['src'] = "any"; $pconfig['dst'] = "any"; } if (isset($_GET['dup'])) unset($id); if ($_POST) { if (($_POST['proto'] != "tcp") && ($_POST['proto'] != "udp") && ($_POST['proto'] != "tcp/udp")) { $_POST['srcbeginport'] = 0; $_POST['srcendport'] = 0; $_POST['dstbeginport'] = 0; $_POST['dstendport'] = 0; } else { if ($_POST['srcbeginport_cust'] && !$_POST['srcbeginport']) $_POST['srcbeginport'] = $_POST['srcbeginport_cust']; if ($_POST['srcendport_cust'] && !$_POST['srcendport']) $_POST['srcendport'] = $_POST['srcendport_cust']; if ($_POST['srcbeginport'] == "any") { $_POST['srcbeginport'] = 0; $_POST['srcendport'] = 0; } else { if (!$_POST['srcendport']) $_POST['srcendport'] = $_POST['srcbeginport']; } if ($_POST['srcendport'] == "any") $_POST['srcendport'] = $_POST['srcbeginport']; if ($_POST['dstbeginport_cust'] && !$_POST['dstbeginport']) $_POST['dstbeginport'] = $_POST['dstbeginport_cust']; if ($_POST['dstendport_cust'] && !$_POST['dstendport']) $_POST['dstendport'] = $_POST['dstendport_cust']; if ($_POST['dstbeginport'] == "any") { $_POST['dstbeginport'] = 0; $_POST['dstendport'] = 0; } else { if (!$_POST['dstendport']) $_POST['dstendport'] = $_POST['dstbeginport']; } if ($_POST['dstendport'] == "any") $_POST['dstendport'] = $_POST['dstbeginport']; } if (is_specialnet($_POST['srctype'])) { $_POST['src'] = $_POST['srctype']; $_POST['srcmask'] = 0; } else if ($_POST['srctype'] == "single") { $_POST['srcmask'] = $maxnetmask; } if (is_specialnet($_POST['dsttype'])) { $_POST['dst'] = $_POST['dsttype']; $_POST['dstmask'] = 0; } else if ($_POST['dsttype'] == "single") { $_POST['dstmask'] = $maxnetmask; } unset($input_errors); $pconfig = $_POST; /* input validation */ $reqdfields = explode(" ", "type interface proto src dst"); $reqdfieldsn = explode(",", "Type,Interface,Protocol,Source,Destination"); if (!(is_specialnet($_POST['srctype']) || ($_POST['srctype'] == "single"))) { $reqdfields[] = "srcmask"; $reqdfieldsn[] = "Source bit count"; } if (!(is_specialnet($_POST['dsttype']) || ($_POST['dsttype'] == "single"))) { $reqdfields[] = "dstmask"; $reqdfieldsn[] = "Destination bit count"; } do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors); if (!$_POST['srcbeginport']) { $_POST['srcbeginport'] = 0; $_POST['srcendport'] = 0; } if (!$_POST['dstbeginport']) { $_POST['dstbeginport'] = 0; $_POST['dstendport'] = 0; } if (($_POST['type'] == "reject") && ($_POST['proto'] != "tcp") && ($_POST['proto'] != "udp")) { $input_errors[] = "Reject only works when the protocol is set to either TCP or UDP."; } if (($_POST['srcbeginport'] && !is_port($_POST['srcbeginport']))) { $input_errors[] = "The start source port must be an integer between 1 and 65535."; } if (($_POST['srcendport'] && !is_port($_POST['srcendport']))) { $input_errors[] = "The end source port must be an integer between 1 and 65535."; } if (($_POST['dstbeginport'] && !is_port($_POST['dstbeginport']))) { $input_errors[] = "The start destination port must be an integer between 1 and 65535."; } if (($_POST['dstendport'] && !is_port($_POST['dstendport']))) { $input_errors[] = "The end destination port must be an integer between 1 and 65535."; } if (!is_specialnet($_POST['srctype'])) { if ($_POST['src'] && !($ipv6rules ? is_ipaddr6($_POST['src']) : is_ipaddroranyalias($_POST['src']))) { $input_errors[] = "A valid source IP address or alias must be specified."; } if (($_POST['srcmask'] && !is_numericint($_POST['srcmask']))) { $input_errors[] = "A valid source bit count must be specified."; } } if (!is_specialnet($_POST['dsttype'])) { if ($_POST['dst'] && !($ipv6rules ? is_ipaddr6($_POST['dst']) : is_ipaddroranyalias($_POST['dst']))) { $input_errors[] = "A valid destination IP address or alias must be specified."; } if (($_POST['dstmask'] && !is_numericint($_POST['dstmask']))) { $input_errors[] = "A valid destination bit count must be specified."; } } if ($_POST['srcbeginport'] > $_POST['srcendport']) { /* swap */ $tmp = $_POST['srcendport']; $_POST['srcendport'] = $_POST['srcbeginport']; $_POST['srcbeginport'] = $tmp; } if ($_POST['dstbeginport'] > $_POST['dstendport']) { /* swap */ $tmp = $_POST['dstendport']; $_POST['dstendport'] = $_POST['dstbeginport']; $_POST['dstbeginport'] = $tmp; } if (!$input_errors) { $filterent = array(); $filterent['type'] = $_POST['type']; $filterent['interface'] = $_POST['interface']; if ($_POST['proto'] != "any") $filterent['protocol'] = $_POST['proto']; else unset($filterent['protocol']); if (($_POST['proto'] == "icmp" || $_POST['proto'] == "ipv6-icmp") && $_POST['icmptype']) $filterent['icmptype'] = $_POST['icmptype']; else unset($filterent['icmptype']); pconfig_to_address($filterent['source'], $_POST['src'], $_POST['srcmask'], $_POST['srcnot'], $_POST['srcbeginport'], $_POST['srcendport']); pconfig_to_address($filterent['destination'], $_POST['dst'], $_POST['dstmask'], $_POST['dstnot'], $_POST['dstbeginport'], $_POST['dstendport']); $filterent['disabled'] = $_POST['disabled'] ? true : false; $filterent['log'] = $_POST['log'] ? true : false; $filterent['frags'] = $_POST['frags'] ? true : false; $filterent['descr'] = $_POST['descr']; if (isset($id) && $a_filter[$id]) $a_filter[$id] = $filterent; else { if (is_numeric($after)) array_splice($a_filter, $after+1, 0, array($filterent)); else $a_filter[] = $filterent; } write_config(); touch($d_filterconfdirty_path); header("Location: firewall_rules.php?if=" . $_POST['interface'] . $typelink); exit; } } ?>