. 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. */ ##|+PRIV ##|*IDENT=page-vpn-ipsec-editphase2 ##|*NAME=VPN: IPsec: Edit Phase 2 page ##|*DESCR=Allow access to the 'VPN: IPsec: Edit Phase 2' page. ##|*MATCH=vpn_ipsec_phase2.php* ##|-PRIV require("functions.inc"); require("guiconfig.inc"); require_once("ipsec.inc"); require_once("vpn.inc"); if (!is_array($config['ipsec']['client'])) $config['ipsec']['client'] = array(); $a_client = &$config['ipsec']['client']; if (!is_array($config['ipsec']['phase2'])) $config['ipsec']['phase2'] = array(); $a_phase2 = &$config['ipsec']['phase2']; $p2index = $_GET['p2index']; if (isset($_POST['p2index'])) $p2index = $_POST['p2index']; if (isset($_GET['dup'])) $p2index = $_GET['dup']; if (isset($p2index) && $a_phase2[$p2index]) { $pconfig['ikeid'] = $a_phase2[$p2index]['ikeid']; $pconfig['disabled'] = isset($a_phase2[$p2index]['disabled']); $pconfig['mode'] = $a_phase2[$p2index]['mode']; $pconfig['descr'] = $a_phase2[$p2index]['descr']; $old_ph2ent = $a_phase2[$p2index]; if (!empty($a_phase2[$p2index]['natlocalid'])) idinfo_to_pconfig("natlocal",$a_phase2[$p2index]['natlocalid'],$pconfig); idinfo_to_pconfig("local",$a_phase2[$p2index]['localid'],$pconfig); idinfo_to_pconfig("remote",$a_phase2[$p2index]['remoteid'],$pconfig); $pconfig['proto'] = $a_phase2[$p2index]['protocol']; ealgos_to_pconfig($a_phase2[$p2index]['encryption-algorithm-option'],$pconfig); $pconfig['halgos'] = $a_phase2[$p2index]['hash-algorithm-option']; $pconfig['pfsgroup'] = $a_phase2[$p2index]['pfsgroup']; $pconfig['lifetime'] = $a_phase2[$p2index]['lifetime']; $pconfig['pinghost'] = $a_phase2[$p2index]['pinghost']; if (isset($a_phase2[$p2index]['mobile'])) $pconfig['mobile'] = true; } else { $pconfig['ikeid'] = $_GET['ikeid']; /* defaults */ $pconfig['localid_type'] = "lan"; $pconfig['remoteid_type'] = "network"; $pconfig['proto'] = "esp"; $pconfig['ealgos'] = explode(",", "3des,blowfish,cast128,aes"); $pconfig['halgos'] = explode(",", "hmac_sha1,hmac_md5"); $pconfig['pfsgroup'] = "0"; $pconfig['lifetime'] = "3600"; /* mobile client */ if($_GET['mobile']) $pconfig['mobile']=true; } if (isset($_GET['dup'])) unset($p2index); if ($_POST) { unset($input_errors); $pconfig = $_POST; if (!isset( $_POST['ikeid'])) $input_errors[] = gettext("A valid ikeid must be specified."); /* input validation */ $reqdfields = explode(" ", "localid_type halgos"); $reqdfieldsn = array(gettext("Local network type"),gettext("P2 Hash Algorithms")); if (!isset($pconfig['mobile'])){ $reqdfields[] = "remoteid_type"; $reqdfieldsn[] = gettext("Remote network type"); } do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors); if(($pconfig['mode'] == "tunnel") || ($pconfig['mode'] == "tunnel6")) { switch ($pconfig['localid_type']) { case "network": if (($pconfig['localid_netbits'] != 0 && !$pconfig['localid_netbits']) || !is_numeric($pconfig['localid_netbits'])) $input_errors[] = gettext("A valid local network bit count must be specified."); case "address": if (!$pconfig['localid_address'] || !is_ipaddr($pconfig['localid_address'])) $input_errors[] = gettext("A valid local network IP address must be specified."); elseif (is_ipaddrv4($pconfig['localid_address']) && ($pconfig['mode'] != "tunnel")) $input_errors[] = gettext("A valid local network IPv4 address must be specified or you need to change Mode to IPv6"); elseif (is_ipaddrv6($pconfig['localid_address']) && ($pconfig['mode'] != "tunnel6")) $input_errors[] = gettext("A valid local network IPv6 address must be specified or you need to change Mode to IPv4"); break; } /* Check if the localid_type is an interface, to confirm if it has a valid subnet. */ if (is_array($config['interfaces'][$pconfig['localid_type']])) { // Don't let an empty subnet into racoon.conf, it can cause parse errors. Ticket #2201. $address = get_interface_ip($pconfig['localid_type']); $netbits = get_interface_subnet($pconfig['localid_type']); if (empty($address) || empty($netbits)) $input_errors[] = gettext("Invalid Local Network.") . " " . convert_friendly_interface_to_friendly_descr($pconfig['localid_type']) . " " . gettext("has no subnet."); } if (!empty($pconfig['natlocalid_address'])) { switch ($pconfig['natlocalid_type']) { case "network": if (($pconfig['natlocalid_netbits'] != 0 && !$pconfig['natlocalid_netbits']) || !is_numeric($pconfig['natlocalid_netbits'])) $input_errors[] = gettext("A valid nat local network bit count must be specified."); if ($pconfig['localid_type'] == "address") $input_errors[] = gettext("You cannot configure a network type address for NAT while only an address type is selected for local source."); case "address": if (!empty($pconfig['natlocalid_address']) && !is_ipaddr($pconfig['natlocalid_address'])) $input_errors[] = gettext("A valid nat local network IP address must be specified."); elseif (is_ipaddrv4($pconfig['natlocalid_address']) && ($pconfig['mode'] != "tunnel")) $input_errors[] = gettext("A valid nat local network IPv4 address must be specified or you need to change Mode to IPv6"); elseif (is_ipaddrv6($pconfig['natlocalid_address']) && ($pconfig['mode'] != "tunnel6")) $input_errors[] = gettext("A valid nat local network IPv6 address must be specified or you need to change Mode to IPv4"); break; } if (is_array($config['interfaces'][$pconfig['natlocalid_type']])) { // Don't let an empty subnet into racoon.conf, it can cause parse errors. Ticket #2201. $address = get_interface_ip($pconfig['natlocalid_type']); $netbits = get_interface_subnet($pconfig['natlocalid_type']); if (empty($address) || empty($netbits)) $input_errors[] = gettext("Invalid Local Network.") . " " . convert_friendly_interface_to_friendly_descr($pconfig['natlocalid_type']) . " " . gettext("has no subnet."); } } switch ($pconfig['remoteid_type']) { case "network": if (($pconfig['remoteid_netbits'] != 0 && !$pconfig['remoteid_netbits']) || !is_numeric($pconfig['remoteid_netbits'])) $input_errors[] = gettext("A valid remote network bit count must be specified."); case "address": if (!$pconfig['remoteid_address'] || !is_ipaddr($pconfig['remoteid_address'])) $input_errors[] = gettext("A valid remote network IP address must be specified."); elseif (is_ipaddrv4($pconfig['remoteid_address']) && ($pconfig['mode'] != "tunnel")) $input_errors[] = gettext("A valid remote network IPv4 address must be specified or you need to change Mode to IPv6"); elseif (is_ipaddrv6($pconfig['remoteid_address']) && ($pconfig['mode'] != "tunnel6")) $input_errors[] = gettext("A valid remote network IPv6 address must be specified or you need to change Mode to IPv4"); break; } } /* Validate enabled phase2's are not duplicates */ if (isset($pconfig['mobile'])){ if (substr($pconfig['mode'], 0, 6) != "tunnel") $input_errors[] = gettext("Mobile IPsec only supports Tunnel mode."); /* User is adding phase 2 for mobile phase1 */ foreach($a_phase2 as $key => $name){ if (isset($name['mobile'])){ /* check duplicate localids only for mobile clents */ $localid_data = ipsec_idinfo_to_cidr($name['localid'], false, $name['mode']); $entered = array(); $entered['type'] = $pconfig['localid_type']; if (isset($pconfig['localid_address'])) $entered['address'] = $pconfig['localid_address']; if (isset($pconfig['localid_netbits'])) $entered['netbits'] = $pconfig['localid_netbits']; $entered_localid_data = ipsec_idinfo_to_cidr($entered, false, $pconfig['mode']); if ($localid_data == $entered_localid_data){ if (!isset($pconfig['p2index'])){ /* adding new p2 entry */ $input_errors[] = gettext("Phase2 with this Local Network is already defined for mobile clients."); break; }else if ($pconfig['p2index'] != $key){ /* editing p2 and entered p2 networks match with different p2 for given p1 */ $input_errors[] = gettext("Phase2 with this Local Network is already defined for mobile clients."); break; } } } } }else{ /* User is adding phase 2 for site-to-site phase1 */ $input_error = 0; foreach($a_phase2 as $key => $name){ if (!isset($name['mobile']) && $pconfig['ikeid'] == $name['ikeid']){ /* check duplicate subnets only for given phase1 */ $localid_data = ipsec_idinfo_to_cidr($name['localid'], false, $name['mode']); $remoteid_data = ipsec_idinfo_to_cidr($name['remoteid'], false, $name['mode']); $entered_local = array(); $entered_local['type'] = $pconfig['localid_type']; if (isset($pconfig['localid_address'])) $entered_local['address'] = $pconfig['localid_address']; if (isset($pconfig['localid_netbits'])) $entered_local['netbits'] = $pconfig['localid_netbits']; $entered_localid_data = ipsec_idinfo_to_cidr($entered_local, false, $pconfig['mode']); $entered_remote = array(); $entered_remote['type'] = $pconfig['remoteid_type']; if (isset($pconfig['remoteid_address'])) $entered_remote['address'] = $pconfig['remoteid_address']; if (isset($pconfig['remoteid_netbits'])) $entered_remote['netbits'] = $pconfig['remoteid_netbits']; $entered_remoteid_data = ipsec_idinfo_to_cidr($entered_remote, false, $pconfig['mode']); if ($localid_data == $entered_localid_data && $remoteid_data == $entered_remoteid_data) { if (!isset($pconfig['p2index'])){ /* adding new p2 entry */ $input_errors[] = gettext("Phase2 with this Local/Remote networks combination is already defined for this Phase1."); break; }else if ($pconfig['p2index'] != $key){ /* editing p2 and entered p2 networks match with different p2 for given p1 */ $input_errors[] = gettext("Phase2 with this Local/Remote networks combination is already defined for this Phase1."); break; } } } } } /* For ESP protocol, handle encryption algorithms */ if ( $pconfig['proto'] == "esp") { $ealgos = pconfig_to_ealgos($pconfig); if (!count($ealgos)) { $input_errors[] = gettext("At least one encryption algorithm must be selected."); } } if (($_POST['lifetime'] && !is_numeric($_POST['lifetime']))) { $input_errors[] = gettext("The P2 lifetime must be an integer."); } if (!$input_errors) { $ph2ent['ikeid'] = $pconfig['ikeid']; $ph2ent['mode'] = $pconfig['mode']; $ph2ent['disabled'] = $pconfig['disabled'] ? true : false; if(($ph2ent['mode'] == "tunnel") || ($ph2ent['mode'] == "tunnel6")){ if (!empty($pconfig['natlocalid_address'])) $ph2ent['natlocalid'] = pconfig_to_idinfo("natlocal",$pconfig); $ph2ent['localid'] = pconfig_to_idinfo("local",$pconfig); $ph2ent['remoteid'] = pconfig_to_idinfo("remote",$pconfig); } $ph2ent['protocol'] = $pconfig['proto']; $ph2ent['encryption-algorithm-option'] = $ealgos; $ph2ent['hash-algorithm-option'] = $pconfig['halgos']; $ph2ent['pfsgroup'] = $pconfig['pfsgroup']; $ph2ent['lifetime'] = $pconfig['lifetime']; $ph2ent['pinghost'] = $pconfig['pinghost']; $ph2ent['descr'] = $pconfig['descr']; if (isset($pconfig['mobile'])) $ph2ent['mobile'] = true; ipsec_lookup_phase1($ph2ent, $ph1ent); if (($ph1ent['protocol'] == "inet") && ($ph2ent['mode'] == "tunnel6")) $input_errors[] = gettext("Phase 1 is using IPv4. You cannot use Tunnel IPv6 on Phase 2."); if (($ph1ent['protocol'] == "inet6") && ($ph2ent['mode'] == "tunnel")) $input_errors[] = gettext("Phase 1 is using IPv6. You cannot use Tunnel IPv4 on Phase 2."); } if (!$input_errors) { if (isset($p2index) && $a_phase2[$p2index]) $a_phase2[$p2index] = $ph2ent; else $a_phase2[] = $ph2ent; /* now we need to find all phase2 entries for this host */ if(is_array($ph2ent)) { ipsec_lookup_phase1($ph2ent, $ph1ent); $old_ph1ent = $ph1ent; $old_ph1ent['remote-gateway'] = resolve_retry($old_ph1ent['remote-gateway']); reload_tunnel_spd_policy ($ph1ent, $ph2ent, $old_ph1ent, $old_ph2ent); } write_config(); mark_subsystem_dirty('ipsec'); header("Location: vpn_ipsec.php"); exit; } } if ($pconfig['mobile']) $pgtitle = array(gettext("VPN"),gettext("IPsec"),gettext("Edit Phase 2"), gettext("Mobile Client")); else $pgtitle = array(gettext("VPN"),gettext("IPsec"),gettext("Edit Phase 2")); $shortcut_section = "ipsec"; include("head.inc"); ?>
>
.
:  
   /

:  
   /
:  
:   /

.

$algodata): $checked = ''; if (is_array($pconfig['ealgos']) && in_array($algo,$pconfig['ealgos'])) $checked = " checked"; ?>
>   

.
$algoname): ?> >


  ">
$algo_data) { if (in_array($algo_name,$pconfig['ealgos'])) { $ealg = array(); $ealg['name'] = $algo_name; if (is_array($algo_data['keysel'])) $ealg['keylen'] = $_POST["keylen_".$algo_name]; $ealgos[] = $ealg; } } } return $ealgos; } function ealgos_to_pconfig(& $ealgos,& $pconfig) { $pconfig['ealgos'] = array(); foreach ($ealgos as $algo_data) { $pconfig['ealgos'][] = $algo_data['name']; if (isset($algo_data['keylen'])) $pconfig["keylen_".$algo_data['name']] = $algo_data['keylen']; } return $ealgos; } function pconfig_to_idinfo($prefix,& $pconfig) { $type = $pconfig[$prefix."id_type"]; $address = $pconfig[$prefix."id_address"]; $netbits = $pconfig[$prefix."id_netbits"]; switch( $type ) { case "address": return array('type' => $type, 'address' => $address); case "network": return array('type' => $type, 'address' => $address, 'netbits' => $netbits); default: return array('type' => $type ); } } function idinfo_to_pconfig($prefix,& $idinfo,& $pconfig) { switch( $idinfo['type'] ) { case "address": $pconfig[$prefix."id_type"] = $idinfo['type']; $pconfig[$prefix."id_address"] = $idinfo['address']; break; case "network": $pconfig[$prefix."id_type"] = $idinfo['type']; $pconfig[$prefix."id_address"] = $idinfo['address']; $pconfig[$prefix."id_netbits"] = $idinfo['netbits']; break; default: $pconfig[$prefix."id_type"] = $idinfo['type']; break; } } ?>