. 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_BUILDER_BINARIES: /bin/rm /bin/mkdir /usr/bin/fetch pfSense_MODULE: aliases */ ##|+PRIV ##|*IDENT=page-firewall-alias-edit ##|*NAME=Firewall: Alias: Edit page ##|*DESCR=Allow access to the 'Firewall: Alias: Edit' page. ##|*MATCH=firewall_aliases_edit.php* ##|-PRIV // Keywords not allowed in names $reserved_keywords = array("pass", "out", "queue", "max", "min", "pptp", "pppoe", "L2TP", "OpenVPN", "IPsec"); require("guiconfig.inc"); require_once("functions.inc"); require_once("filter.inc"); require_once("shaper.inc"); $pgtitle = array(gettext("Firewall"),gettext("Aliases"),gettext("Edit")); $reserved_ifs = get_configured_interface_list(false, true); $reserved_keywords = array_merge($reserved_keywords, $reserved_ifs); if (!is_array($config['aliases']['alias'])) $config['aliases']['alias'] = array(); $a_aliases = &$config['aliases']['alias']; if($_POST) $origname = $_POST['origname']; // Debugging if($debug) exec("rm -f {$g['tmp_path']}/alias_rename_log.txt"); function alias_same_type($name, $type) { global $config; foreach ($config['aliases']['alias'] as $alias) { if ($name == $alias['name']) { if (in_array($type, array("host", "network")) && in_array($alias['type'], array("host", "network"))) return true; if ($type == $alias['type']) return true; else return false; } } return true; } $id = $_GET['id']; if (isset($_POST['id'])) $id = $_POST['id']; if (isset($id) && $a_aliases[$id]) { $original_alias_name = $a_aliases[$id]['name']; $pconfig['name'] = $a_aliases[$id]['name']; $pconfig['detail'] = $a_aliases[$id]['detail']; $pconfig['address'] = $a_aliases[$id]['address']; $pconfig['type'] = $a_aliases[$id]['type']; $pconfig['descr'] = html_entity_decode($a_aliases[$id]['descr']); /* interface list */ $iflist = get_configured_interface_with_descr(false, true); foreach ($iflist as $if => $ifdesc) if($ifdesc == $pconfig['descr']) $input_errors[] = sprintf(gettext("Sorry, an interface is already named %s."), $pconfig['descr']); if($a_aliases[$id]['type'] == "urltable") { $pconfig['address'] = $a_aliases[$id]['url']; $pconfig['updatefreq'] = $a_aliases[$id]['updatefreq']; } if($a_aliases[$id]['aliasurl'] <> "") { $pconfig['type'] = "url"; if(is_array($a_aliases[$id]['aliasurl'])) { $isfirst = 0; $pconfig['address'] = ""; foreach($a_aliases[$id]['aliasurl'] as $aa) { if($isfirst == 1) $pconfig['address'] .= " "; $isfirst = 1; $pconfig['address'] .= $aa; } } else { $pconfig['address'] = $a_aliases[$id]['aliasurl']; } } } if ($_POST) { unset($input_errors); /* input validation */ $reqdfields = explode(" ", "name"); $reqdfieldsn = array(gettext("Name")); do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors); $x = is_validaliasname($_POST['name']); if (!isset($x)) { $input_errors[] = gettext("Reserved word used for alias name."); } else if ($_POST['type'] == "port" && (getservbyname($_POST['name'], "tcp") || getservbyname($_POST['name'], "udp"))) { $input_errors[] = gettext("Reserved word used for alias name."); } else { if (is_validaliasname($_POST['name']) == false) $input_errors[] = gettext("The alias name may only consist of the characters") . " a-z, A-Z, 0-9, _."; } /* check for name conflicts */ if (empty($a_aliases[$id])) { foreach ($a_aliases as $alias) { if ($alias['name'] == $_POST['name']) { $input_errors[] = gettext("An alias with this name already exists."); break; } } } /* Check for reserved keyword names */ foreach($reserved_keywords as $rk) if($rk == $_POST['name']) $input_errors[] = sprintf(gettext("Cannot use a reserved keyword as alias name %s"), $rk); /* check for name interface description conflicts */ foreach($config['interfaces'] as $interface) { if($interface['descr'] == $_POST['name']) { $input_errors[] = gettext("An interface description with this name already exists."); break; } } $alias = array(); $address = array(); $final_address_details = array(); $alias['name'] = $_POST['name']; if ($_POST['type'] == "urltable") { $address = ""; $isfirst = 0; /* item is a url type */ if ($_POST['address0']) { /* fetch down and add in */ $isfirst = 0; $address = ""; $alias['url'] = $_POST['address0']; $alias['updatefreq'] = $_POST['address_subnet0'] ? $_POST['address_subnet0'] : 7; if (!is_URL($alias['url']) || empty($alias['url'])) { $input_errors[] = gettext("You must provide a valid URL."); $dont_update = true; } elseif (! process_alias_urltable($alias['name'], $alias['url'], 0, true)) { $input_errors[] = gettext("Unable to fetch usable data."); $dont_update = true; } } } elseif($_POST['type'] == "url") { $isfirst = 0; $address_count = 2; /* item is a url type */ for($x=0; isset($_POST['address'. $x]); $x++) { if($_POST['address' . $x]) { /* fetch down and add in */ $isfirst = 0; $temp_filename = tempnam("{$g['tmp_path']}/", "alias_import"); unlink($temp_filename); $fda = fopen("{$g['tmp_path']}/tmpfetch","w"); fwrite($fda, "/usr/bin/fetch -q -o \"{$temp_filename}/aliases\" \"" . $_POST['address' . $x] . "\""); fclose($fda); mwexec("/bin/mkdir -p {$temp_filename}"); mwexec("/usr/bin/fetch -q -o \"{$temp_filename}/aliases\" \"" . $_POST['address' . $x] . "\""); /* if the item is tar gzipped then extract */ if(stristr($_POST['address' . $x], ".tgz")) process_alias_tgz($temp_filename); if(file_exists("{$temp_filename}/aliases")) { $file_contents = file_get_contents("{$temp_filename}/aliases"); $file_contents = str_replace("#", "\n#", $file_contents); $file_contents_split = split("\n", $file_contents); foreach($file_contents_split as $fc) { // Stop at 3000 items, aliases larger than that tend to break both pf and the WebGUI. if ($address_count >= 3000) break; $tmp = trim($fc); if(stristr($fc, "#")) { $tmp_split = split("#", $tmp); $tmp = trim($tmp_split[0]); } $tmp = trim($tmp); if(!empty($tmp) && (is_ipaddr($tmp) || is_subnet($tmp))) { $address[] = $tmp; $isfirst = 1; $address_count++; } } if($isfirst == 0) { /* nothing was found */ $input_errors[] = gettext("You must provide a valid URL. Could not fetch usable data."); $dont_update = true; break; } $alias['aliasurl'][] = $_POST['address' . $x]; mwexec("/bin/rm -rf {$temp_filename}"); } else { $input_errors[] = gettext("You must provide a valid URL."); $dont_update = true; break; } } } } else { /* item is a normal alias type */ $wrongaliases = ""; for($x=0; $x<4999; $x++) { if($_POST["address{$x}"] <> "") { if (is_alias($_POST["address{$x}"])) { if (!alias_same_type($_POST["address{$x}"], $_POST['type'])) $wrongaliases .= " " . $_POST["address{$x}"]; } else if ($_POST['type'] == "port") { if (!is_port($_POST["address{$x}"])) $input_errors[] = $_POST["address{$x}"] . " " . gettext("is not a valid port or alias."); } else if ($_POST['type'] == "host" || $_POST['type'] == "network") { if (!is_ipaddr($_POST["address{$x}"]) && !is_hostname($_POST["address{$x}"]) && !is_iprange($_POST["address{$x}"])) $input_errors[] = sprintf(gettext('%1$s is not a valid %2$s alias.'), $_POST["address{$x}"], $_POST['type']); } if (is_iprange($_POST["address{$x}"])) { list($startip, $endip) = explode('-', $_POST["address{$x}"]); $rangesubnets = ip_range_to_subnet_array($startip, $endip); $address = array_merge($address, $rangesubnets); } else { $tmpaddress = $_POST["address{$x}"]; if(is_ipaddr($_POST["address{$x}"]) && $_POST["address_subnet{$x}"] <> "") $tmpaddress .= "/" . $_POST["address_subnet{$x}"]; $address[] = $tmpaddress; } if ($_POST["detail{$x}"] <> "") $final_address_details[] = $_POST["detail{$x}"]; else $final_address_details[] = sprintf(gettext("Entry added %s"), date('r')); } } if ($wrongaliases <> "") $input_errors[] = sprintf(gettext('The alias(es): %s cannot be nested because they are not of the same type.'), $wrongaliases); } if (!$input_errors) { $alias['address'] = is_array($address) ? implode(" ", $address) : $address; $alias['descr'] = $_POST['descr']; $alias['type'] = $_POST['type']; $alias['detail'] = implode("||", $final_address_details); /* Check to see if alias name needs to be * renamed on referenced rules and such */ if ($_POST['name'] <> $_POST['origname']) { // Firewall rules update_alias_names_upon_change(array('filter', 'rule'), array('source', 'address'), $_POST['name'], $origname); update_alias_names_upon_change(array('filter', 'rule'), array('destination', 'address'), $_POST['name'], $origname); update_alias_names_upon_change(array('filter', 'rule'), array('source', 'port'), $_POST['name'], $origname); update_alias_names_upon_change(array('filter', 'rule'), array('destination', 'port'), $_POST['name'], $origname); // NAT Rules update_alias_names_upon_change(array('nat', 'rule'), array('source', 'address'), $_POST['name'], $origname); update_alias_names_upon_change(array('nat', 'rule'), array('source', 'port'), $_POST['name'], $origname); update_alias_names_upon_change(array('nat', 'rule'), array('destination', 'address'), $_POST['name'], $origname); update_alias_names_upon_change(array('nat', 'rule'), array('destination', 'port'), $_POST['name'], $origname); update_alias_names_upon_change(array('nat', 'rule'), array('target'), $_POST['name'], $origname); update_alias_names_upon_change(array('nat', 'rule'), array('local-port'), $_POST['name'], $origname); // NAT 1:1 Rules //update_alias_names_upon_change(array('nat', 'onetoone'), array('external'), $_POST['name'], $origname); //update_alias_names_upon_change(array('nat', 'onetoone'), array('source', 'address'), $_POST['name'], $origname); update_alias_names_upon_change(array('nat', 'onetoone'), array('destination', 'address'), $_POST['name'], $origname); // NAT Outbound Rules update_alias_names_upon_change(array('nat', 'advancedoutbound', 'rule'), array('source', 'network'), $_POST['name'], $origname); update_alias_names_upon_change(array('nat', 'advancedoutbound', 'rule'), array('sourceport'), $_POST['name'], $origname); update_alias_names_upon_change(array('nat', 'advancedoutbound', 'rule'), array('destination', 'address'), $_POST['name'], $origname); update_alias_names_upon_change(array('nat', 'advancedoutbound', 'rule'), array('dstport'), $_POST['name'], $origname); update_alias_names_upon_change(array('nat', 'advancedoutbound', 'rule'), array('target'), $_POST['name'], $origname); // Alias in an alias update_alias_names_upon_change(array('aliases', 'alias'), array('address'), $_POST['name'], $origname); } if (isset($id) && $a_aliases[$id]) { if ($a_aliases[$id]['name'] <> $alias['name']) { foreach ($a_aliases as $aliasid => $aliasd) { if ($aliasd['address'] <> "") { $tmpdirty = false; $tmpaddr = explode(" ", $aliasd['address']); foreach ($tmpaddr as $tmpidx => $tmpalias) { if ($tmpalias == $a_aliases[$id]['name']) { $tmpaddr[$tmpidx] = $alias['name']; $tmpdirty = true; } } if ($tmpdirty == true) $a_aliases[$aliasid]['address'] = implode(" ", $tmpaddr); } } } $a_aliases[$id] = $alias; } else $a_aliases[] = $alias; mark_subsystem_dirty('aliases'); // Sort list $a_aliases = msort($a_aliases, "name"); write_config(); header("Location: firewall_aliases.php"); exit; } //we received input errors, copy data to prevent retype else { $pconfig['name'] = $_POST['name']; $pconfig['descr'] = $_POST['descr']; $pconfig['address'] = implode(" ", $address); $pconfig['type'] = $_POST['type']; $pconfig['detail'] = implode("||", $final_address_details); } } include("head.inc"); $jscriptstr = << var objAlias = new Array(4999); function typesel_change() { switch (document.iform.type.selectedIndex) { case 0: /* host */ var cmd; newrows = totalrows; for(i=0; i EOD; ?> ">


"") { $item = explode(" ", $address); $item3 = explode("||", $pconfig['detail']); foreach($item as $ww) { $address = $item[$counter]; $address_subnet = ""; $item2 = explode("/", $address); foreach($item2 as $current) { if($item2[1] <> "") { $address = $item2[0]; $address_subnet = $item2[1]; } } $item4 = $item3[$counter]; $tracker = $counter; ?>
CIDR
" />
  " /> " />