vpn_ipsec_phase2.php 40.9 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1 2
<?php
/*
3
	Copyright (C) 2014 Deciso B.V.
Ad Schellevis's avatar
Ad Schellevis committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
	Copyright (C) 2008 Shrew Soft Inc
	Copyright (C) 2003-2005 Manuel Kasper <mk@neon1.net>.
	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.
*/

30 31
require_once("functions.inc");
require_once("guiconfig.inc");
Ad Schellevis's avatar
Ad Schellevis committed
32 33 34
require_once("ipsec.inc");
require_once("vpn.inc");

35 36 37 38 39
$p2_modes = array(
	'tunnel' => 'Tunnel IPv4',
	'tunnel6' => 'Tunnel IPv6',
	'transport' => 'Transport');

40 41 42
if (!is_array($config['ipsec'])) {
        $config['ipsec'] = array();
}
43

44
if (!is_array($config['ipsec']['client'])) {
45
    $config['ipsec']['client'] = array();
46
}
Ad Schellevis's avatar
Ad Schellevis committed
47

48
if (!is_array($config['ipsec']['phase2'])) {
49
    $config['ipsec']['phase2'] = array();
50
}
Ad Schellevis's avatar
Ad Schellevis committed
51

52
$a_client = &$config['ipsec']['client'];
Ad Schellevis's avatar
Ad Schellevis committed
53 54
$a_phase2 = &$config['ipsec']['phase2'];

55 56 57 58 59 60
if (!empty($_GET['p2index'])) {
    $uindex = $_GET['p2index'];
}
if (!empty($_POST['uniqid'])) {
    $uindex = $_POST['uniqid'];
}
Ad Schellevis's avatar
Ad Schellevis committed
61

62 63 64
if (!empty($_GET['dup'])) {
    $uindex = $_GET['dup'];
}
Ad Schellevis's avatar
Ad Schellevis committed
65 66 67

$ph2found = false;
if (isset($uindex)) {
68 69 70 71 72 73
    foreach ($a_phase2 as $p2index => $ph2) {
        if ($ph2['uniqid'] == $uindex) {
            $ph2found = true;
            break;
        }
    }
Ad Schellevis's avatar
Ad Schellevis committed
74 75
}

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
if ($ph2found === true) {
    $pconfig['ikeid'] = $ph2['ikeid'];
    $pconfig['disabled'] = isset($ph2['disabled']);
    $pconfig['mode'] = $ph2['mode'];
    $pconfig['descr'] = $ph2['descr'];
    $pconfig['uniqid'] = $ph2['uniqid'];

    if (!empty($ph2['natlocalid'])) {
        idinfo_to_pconfig("natlocal", $ph2['natlocalid'], $pconfig);
    }
    idinfo_to_pconfig("local", $ph2['localid'], $pconfig);
    idinfo_to_pconfig("remote", $ph2['remoteid'], $pconfig);

    $pconfig['proto'] = $ph2['protocol'];
    ealgos_to_pconfig($ph2['encryption-algorithm-option'], $pconfig);
    $pconfig['halgos'] = $ph2['hash-algorithm-option'];
    $pconfig['pfsgroup'] = $ph2['pfsgroup'];
    $pconfig['lifetime'] = $ph2['lifetime'];
    $pconfig['pinghost'] = $ph2['pinghost'];

    if (isset($ph2['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";
    $pconfig['uniqid'] = uniqid();

    /* mobile client */
    if ($_GET['mobile']) {
        $pconfig['mobile']=true;
    }
Ad Schellevis's avatar
Ad Schellevis committed
116 117 118 119
}

unset($ph2);
if (!empty($_GET['dup'])) {
120 121 122
    unset($uindex);
    unset($p2index);
    $pconfig['uniqid'] = uniqid();
Ad Schellevis's avatar
Ad Schellevis committed
123 124 125
}

if ($_POST) {
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    unset($input_errors);
    $pconfig = $_POST;

    if (!isset( $_POST['ikeid'])) {
        $input_errors[] = gettext("A valid ikeid must be specified.");
    }

    /* input validation */
    $reqdfields = explode(" ", "localid_type uniqid");
    $reqdfieldsn = array(gettext("Local network type"), gettext("Unique Identifier"));
    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.");
            }
Ad Schellevis's avatar
Ad Schellevis committed
167 168
        }

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
        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.");
                }
            }
        }
Ad Schellevis's avatar
Ad Schellevis committed
198

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
        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'])) {
        /* User is adding phase 2 for mobile phase1 */
        foreach ($a_phase2 as $key => $name) {
            if (isset($name['mobile']) && $name['uniqid'] != $pconfig['uniqid']) {
                /* 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) {
                    /* adding new p2 entry */
                    $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'] && $pconfig['uniqid'] != $name['uniqid']) {
                /* 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) {
                    /* adding new p2 entry */
                    $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.");
        } else {
            if (empty($pconfig['halgos'])) {
                foreach ($ealgos as $ealgo) {
                    if (!strpos($ealgo['name'], "gcm")) {
                        $input_errors[] = gettext("At least one hashing algorithm needs to be selected.");
                        break;
                    }
                }
            }
        }
Ad Schellevis's avatar
Ad Schellevis committed
288

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
    }
    if (($_POST['lifetime'] && !is_numeric($_POST['lifetime']))) {
        $input_errors[] = gettext("The P2 lifetime must be an integer.");
    }

    if (!$input_errors) {
        $ph2ent = array();
        $ph2ent['ikeid'] = $pconfig['ikeid'];
        $ph2ent['uniqid'] = $pconfig['uniqid'];
        $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);
        }
Ad Schellevis's avatar
Ad Schellevis committed
308

309 310 311 312 313 314 315 316 317 318 319
        $ph2ent['protocol'] = $pconfig['proto'];
        $ph2ent['encryption-algorithm-option'] = $ealgos;
        if (!empty($pconfig['halgos'])) {
            $ph2ent['hash-algorithm-option'] = $pconfig['halgos'];
        } else {
            unset($ph2ent['hash-algorithm-option']);
        }
        $ph2ent['pfsgroup'] = $pconfig['pfsgroup'];
        $ph2ent['lifetime'] = $pconfig['lifetime'];
        $ph2ent['pinghost'] = $pconfig['pinghost'];
        $ph2ent['descr'] = $pconfig['descr'];
Ad Schellevis's avatar
Ad Schellevis committed
320

321 322 323
        if (isset($pconfig['mobile'])) {
            $ph2ent['mobile'] = true;
        }
Ad Schellevis's avatar
Ad Schellevis committed
324

325 326 327 328 329
        if ($ph2found === true && $a_phase2[$p2index]) {
            $a_phase2[$p2index] = $ph2ent;
        } else {
            $a_phase2[] = $ph2ent;
        }
Ad Schellevis's avatar
Ad Schellevis committed
330 331


332 333
        write_config();
        mark_subsystem_dirty('ipsec');
Ad Schellevis's avatar
Ad Schellevis committed
334

335 336 337
        header("Location: vpn_ipsec.php");
        exit;
    }
Ad Schellevis's avatar
Ad Schellevis committed
338 339
}

340
if ($pconfig['mobile']) {
Ad Schellevis's avatar
Ad Schellevis committed
341
    $pgtitle = array(gettext("VPN"),gettext("IPsec"),gettext("Edit Phase 2"), gettext("Mobile Client"));
342
} else {
Ad Schellevis's avatar
Ad Schellevis committed
343
    $pgtitle = array(gettext("VPN"),gettext("IPsec"),gettext("Edit Phase 2"));
344
}
Ad Schellevis's avatar
Ad Schellevis committed
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
$shortcut_section = "ipsec";


include("head.inc");

?>

<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
<?php include("fbegin.inc"); ?>
<script type="text/javascript" src="/javascript/jquery.ipv4v6ify.js"></script>
<script type="text/javascript">
//<![CDATA[

function change_mode() {
	index = document.iform.mode.selectedIndex;
	value = document.iform.mode.options[index].value;
	if ((value == 'tunnel') || (value == 'tunnel6')) {
		document.getElementById('opt_localid').style.display = '';
363 364
<?php if (!isset($pconfig['mobile'])) :
?>
Ad Schellevis's avatar
Ad Schellevis committed
365
		document.getElementById('opt_remoteid').style.display = '';
366 367
<?php
endif; ?>
Ad Schellevis's avatar
Ad Schellevis committed
368 369
	} else {
		document.getElementById('opt_localid').style.display = 'none';
370 371
<?php if (!isset($pconfig['mobile'])) :
?>
Ad Schellevis's avatar
Ad Schellevis committed
372
		document.getElementById('opt_remoteid').style.display = 'none';
373 374
<?php
endif; ?>
Ad Schellevis's avatar
Ad Schellevis committed
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
	}
}

function typesel_change_natlocal(bits) {
	var value = document.iform.mode.options[index].value;
	if (typeof(bits) === "undefined") {
		if (value === "tunnel") {
			bits = 24;
		}
		else if (value === "tunnel6") {
			bits = 64;
		}
	}
	var address_is_blank = !/\S/.test(document.iform.natlocalid_address.value);
	switch (document.iform.natlocalid_type.selectedIndex) {
		case 0:	/* single */
			document.iform.natlocalid_address.disabled = 0;
			if (address_is_blank) {
				document.iform.natlocalid_netbits.value = 0;
			}
			document.iform.natlocalid_netbits.disabled = 1;
			break;
		case 1:	/* network */
			document.iform.natlocalid_address.disabled = 0;
			if (address_is_blank) {
				document.iform.natlocalid_netbits.value = bits;
			}
			document.iform.natlocalid_netbits.disabled = 0;
			break;
		case 3:	/* none */
			document.iform.natlocalid_address.disabled = 1;
			document.iform.natlocalid_netbits.disabled = 1;
			break;
		default:
			document.iform.natlocalid_address.value = "";
			document.iform.natlocalid_address.disabled = 1;
			if (address_is_blank) {
				document.iform.natlocalid_netbits.value = 0;
			}
			document.iform.natlocalid_netbits.disabled = 1;
			break;
	}
}

function typesel_change_local(bits) {
	var value = document.iform.mode.options[index].value;
	if (typeof(bits) === "undefined") {
		if (value === "tunnel") {
			bits = 24;
		}
		else if (value === "tunnel6") {
			bits = 64;
		}
	}
	var address_is_blank = !/\S/.test(document.iform.localid_address.value);
	switch (document.iform.localid_type.selectedIndex) {
		case 0:	/* single */
			document.iform.localid_address.disabled = 0;
			if (address_is_blank) {
				document.iform.localid_netbits.value = 0;
			}
			document.iform.localid_netbits.disabled = 1;
			break;
		case 1:	/* network */
			document.iform.localid_address.disabled = 0;
			if (address_is_blank) {
				document.iform.localid_netbits.value = bits;
			}
			document.iform.localid_netbits.disabled = 0;
			break;
		case 3:	/* none */
			document.iform.localid_address.disabled = 1;
			document.iform.localid_netbits.disabled = 1;
			break;
		default:
			document.iform.localid_address.value = "";
			document.iform.localid_address.disabled = 1;
			if (address_is_blank) {
				document.iform.localid_netbits.value = 0;
			}
			document.iform.localid_netbits.disabled = 1;
			break;
	}
}

460 461
<?php if (!isset($pconfig['mobile'])) :
?>
Ad Schellevis's avatar
Ad Schellevis committed
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499

function typesel_change_remote(bits) {
	var value = document.iform.mode.options[index].value;
	if (typeof(bits) === "undefined") {
		if (value === "tunnel") {
			bits = 24;
		}
		else if (value === "tunnel6") {
			bits = 64;
		}
	}
	var address_is_blank = !/\S/.test(document.iform.remoteid_address.value);
	switch (document.iform.remoteid_type.selectedIndex) {
		case 0:	/* single */
			document.iform.remoteid_address.disabled = 0;
			if (address_is_blank) {
				document.iform.remoteid_netbits.value = 0;
			}
			document.iform.remoteid_netbits.disabled = 1;
			break;
		case 1:	/* network */
			document.iform.remoteid_address.disabled = 0;
			if (address_is_blank) {
				document.iform.remoteid_netbits.value = bits;
			}
			document.iform.remoteid_netbits.disabled = 0;
			break;
		default:
			document.iform.remoteid_address.value = "";
			document.iform.remoteid_address.disabled = 1;
			if (address_is_blank) {
				document.iform.remoteid_netbits.value = 0;
			}
			document.iform.remoteid_netbits.disabled = 1;
			break;
	}
}

500 501
<?php
endif; ?>
Ad Schellevis's avatar
Ad Schellevis committed
502 503 504 505 506 507 508 509 510 511 512 513 514

function change_protocol() {
	index = document.iform.proto.selectedIndex;
	value = document.iform.proto.options[index].value;
	if (value == 'esp')
		document.getElementById('opt_enc').style.display = '';
	else
		document.getElementById('opt_enc').style.display = 'none';
}

//]]>
</script>

515

Ad Schellevis's avatar
Ad Schellevis committed
516 517

<?php
518
if (isset($input_errors) && count($input_errors) > 0) {
519 520
    print_input_errors($input_errors);
}
Ad Schellevis's avatar
Ad Schellevis committed
521
?>
522 523 524 525 526
<section class="page-content-main">
	<div class="container-fluid">
		<div class="row">
		    <section class="col-xs-12">
				<?php
527 528 529 530 531 532 533
                    $tab_array = array();
                    $tab_array[0] = array(gettext("Tunnels"), true, "vpn_ipsec.php");
                    $tab_array[1] = array(gettext("Mobile clients"), false, "vpn_ipsec_mobile.php");
                    $tab_array[2] = array(gettext("Pre-Shared Keys"), false, "vpn_ipsec_keys.php");
                    $tab_array[3] = array(gettext("Advanced Settings"), false, "vpn_ipsec_settings.php");
                    display_top_tabs($tab_array);
                ?>
534 535 536 537 538 539


				<div class="tab-content content-box col-xs-12">
					<form action="vpn_ipsec_phase2.php" method="post" name="iform" id="iform">
						<div class="table-responsive">
							<table class="table table-striped table-sort">
Ad Schellevis's avatar
Ad Schellevis committed
540
								<tr>
541 542 543 544 545 546
									<td id="mainarea">
										<div class="tabcont">
											<table class="table table-striped" width="100%" border="0" cellpadding="6" cellspacing="0" summary="main area">
												<tr>
													<td width="22%" valign="top" class="vncellreq"><?=gettext("Disabled"); ?></td>
													<td width="78%" class="vtable">
547 548 549
														<input name="disabled" type="checkbox" id="disabled" value="yes" <?php if ($pconfig['disabled']) {
                                                            echo "checked=\"checked\"";
} ?> />
550 551 552
														<strong><?=gettext("Disable this phase2 entry"); ?></strong>
														<br />
														<span class="vexpl"><?=gettext("Set this option to disable this phase2 entry without " .
553
                                                          "removing it from the list"); ?>.
554 555 556 557 558 559 560 561
														</span>
													</td>
												</tr>
												<tr>
													<td width="22%" valign="top" class="vncellreq"><?=gettext("Mode"); ?></td>
													<td width="78%" class="vtable">
														<select name="mode" class="formselect" onchange="change_mode()">
															<?php
562 563 564 565 566 567 568 569 570 571 572
                                                            foreach ($p2_modes as $name => $value) :
                                                                $selected = "";
                                                                if ($name == $pconfig['mode']) {
                                                                    $selected = "selected=\"selected\"";
                                                                }
                                                            ?>
															<option value="<?=$name;
?>" <?=$selected;
?>><?=$value;?></option>
															<?php
                                                            endforeach; ?>
573 574 575 576 577 578 579 580 581 582 583 584
														</select>
													</td>
												</tr>
												<tr id="opt_localid">
													<td width="22%" valign="top" class="vncellreq"><?=gettext("Local Network"); ?></td>
													<td width="78%" class="vtable">
														<table class="table table-striped" border="0" cellspacing="0" cellpadding="0" summary="local network">
															<tr>
																<td><?=gettext("Type"); ?>:&nbsp;&nbsp;</td>
																<td></td>
																<td>
																	<select name="localid_type" class="formselect" onchange="typesel_change_local()">
585 586 587 588 589 590 591 592
																		<option value="address" <?php if ($pconfig['localid_type'] == "address") {
                                                                            echo "selected=\"selected\"";

}?>><?=gettext("Address"); ?></option>
																		<option value="network" <?php if ($pconfig['localid_type'] == "network") {
                                                                            echo "selected=\"selected\"";

}?>><?=gettext("Network"); ?></option>
593
																		<?php
594 595 596 597 598 599 600 601 602
                                                                            $iflist = get_configured_interface_with_descr();
                                                                        foreach ($iflist as $ifname => $ifdescr) :
                                                                        ?>
																		<option value="<?=$ifname; ?>" <?php if ($pconfig['localid_type'] == $ifname) {
                                                                            echo "selected=\"selected\"";

}?>><?=sprintf(gettext("%s subnet"), $ifdescr); ?></option>
																		<?php
                                                                        endforeach; ?>
603 604 605 606 607
																	</select>
																</td>
															</tr>
															<tr>
																<td><?=gettext("Address:");?>&nbsp;&nbsp;</td>
608
																<td></td>
609 610 611 612
																<td>
																	<input name="localid_address" type="text" class="formfld unknown ipv4v6" id="localid_address" size="28" value="<?=htmlspecialchars($pconfig['localid_address']);?>" />
																	/
																	<select name="localid_netbits" class="formselect ipv4v6" id="localid_netbits">
613 614 615 616 617
																	<?php for ($i = 128; $i >= 0; $i--) :
?>
																		<option value="<?=$i;?>" <?php if (isset($pconfig['localid_netbits']) && $i == $pconfig['localid_netbits']) {
                                                                            echo "selected=\"selected\"";
} ?>>
618 619
																			<?=$i;?>
																		</option>
620 621
																	<?php
endfor; ?>
622 623 624 625 626 627 628 629 630 631 632 633
																	</select>
																</td>
															</tr>
															<tr> <td colspan="3">
															<br />
															<?php echo gettext("In case you need NAT/BINAT on this network specify the address to be translated"); ?>
															</td></tr>
															<tr>
																<td><?=gettext("Type"); ?>:&nbsp;&nbsp;</td>
																<td></td>
																<td>
																	<select name="natlocalid_type" class="formselect" onchange="typesel_change_natlocal()">
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
																		<option value="address" <?php if ($pconfig['natlocalid_type'] == "address") {
                                                                            echo "selected=\"selected\"";

}?>><?=gettext("Address"); ?></option>
																		<option value="network" <?php if ($pconfig['natlocalid_type'] == "network") {
                                                                            echo "selected=\"selected\"";

}?>><?=gettext("Network"); ?></option>
																		<?php
                                                                            $iflist = get_configured_interface_with_descr();
                                                                        foreach ($iflist as $ifname => $ifdescr) :
                                                                        ?>
																		<option value="<?=$ifname; ?>" <?php if ($pconfig['natlocalid_type'] == $ifname) {
                                                                            echo "selected=\"selected\"";

}?>><?=sprintf(gettext("%s subnet"), $ifdescr); ?></option>
650
																		<?php
651 652 653 654 655
                                                                        endforeach; ?>
																		<option value="none" <?php if (empty($pconfig['natlocalid_type']) || $pconfig['natlocalid_type'] == "none") {
                                                                            echo "selected=\"selected\"";

}?>><?=gettext("None"); ?></option>
656 657 658 659 660
																	</select>
																</td>
															</tr>
															<tr>
																<td><?=gettext("Address:");?>&nbsp;&nbsp;</td>
661
																<td></td>
662 663 664 665
																<td>
																	<input name="natlocalid_address" type="text" class="formfld unknown ipv4v6" id="natlocalid_address" size="28" value="<?=htmlspecialchars($pconfig['natlocalid_address']);?>" />
																	/
																	<select name="natlocalid_netbits" class="formselect ipv4v6" id="natlocalid_netbits">
666 667 668 669 670
																	<?php for ($i = 128; $i >= 0; $i--) :
?>
																		<option value="<?=$i;?>" <?php if (isset($pconfig['natlocalid_netbits']) && $i == $pconfig['natlocalid_netbits']) {
                                                                            echo "selected=\"selected\"";
} ?>>
671 672
																			<?=$i;?>
																		</option>
673 674
																	<?php
endfor; ?>
675 676 677 678 679 680 681
																	</select>
																</td>
															</tr>
														</table>
													</td>
												</tr>

682 683
												<?php if (!isset($pconfig['mobile'])) :
?>
684 685 686 687 688 689 690 691 692 693

												<tr id="opt_remoteid">
													<td width="22%" valign="top" class="vncellreq"><?=gettext("Remote Network"); ?></td>
													<td width="78%" class="vtable">
														<table class="table table-striped" border="0" cellspacing="0" cellpadding="0" summary="remote network">
															<tr>
																<td><?=gettext("Type"); ?>:&nbsp;&nbsp;</td>
																<td></td>
																<td>
																	<select name="remoteid_type" class="formselect" onchange="typesel_change_remote()">
694 695 696 697 698 699 700 701
																		<option value="address" <?php if ($pconfig['remoteid_type'] == "address") {
                                                                            echo "selected=\"selected\"";

} ?>><?=gettext("Address"); ?></option>
																		<option value="network" <?php if ($pconfig['remoteid_type'] == "network") {
                                                                            echo "selected=\"selected\"";

} ?>><?=gettext("Network"); ?></option>
702 703 704 705 706
																	</select>
																</td>
															</tr>
															<tr>
																<td><?=gettext("Address"); ?>:&nbsp;&nbsp;</td>
707
																<td></td>
708 709 710 711 712
																<td>
																	<input name="remoteid_address" type="text" class="formfld unknown ipv4v6" id="remoteid_address" size="28" value="<?=htmlspecialchars($pconfig['remoteid_address']);?>" />
																	/
																	<select name="remoteid_netbits" class="formselect ipv4v6" id="remoteid_netbits">
																	<?php for ($i = 128; $i >= 0; $i--) {
713 714 715 716 717 718
                                                                        echo "<option value=\"{$i}\"";
                                                                        if (isset($pconfig['remoteid_netbits']) && $i == $pconfig['remoteid_netbits']) {
                                                                            echo " selected=\"selected\"";
                                                                        }
                                                                        echo ">{$i}</option>\n";
} ?>
719 720 721 722 723 724 725
																	</select>
																</td>
															</tr>
														</table>
													</td>
												</tr>

726 727
												<?php
endif; ?>
728 729 730 731 732 733 734 735

												<tr>
													<td width="22%" valign="top" class="vncell"><?=gettext("Description"); ?></td>
													<td width="78%" class="vtable">
														<input name="descr" type="text" class="formfld unknown" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>" />
														<br />
														<span class="vexpl">
															<?=gettext("You may enter a description here " .
736
                                                            "for your reference (not parsed)"); ?>.
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
														</span>
													</td>
												</tr>
												<tr>
													<td colspan="2" class="list" height="12"></td>
												</tr>
												<tr>
													<td colspan="2" valign="top" class="listtopic">
														<?=gettext("Phase 2 proposal (SA/Key Exchange)"); ?>
													</td>
												</tr>
												<tr>
													<td width="22%" valign="top" class="vncellreq"><?=gettext("Protocol"); ?></td>
													<td width="78%" class="vtable">
														<select name="proto" class="formselect" onchange="change_protocol()">
752 753 754 755 756
														<?php foreach ($p2_protos as $proto => $protoname) :
?>
															<option value="<?=$proto;?>" <?php if ($proto == $pconfig['proto']) {
                                                                echo "selected=\"selected\"";
} ?>>
757 758
																<?=htmlspecialchars($protoname);?>
															</option>
759 760
														<?php
endforeach; ?>
761 762 763 764 765 766 767 768 769 770 771 772
														</select>
														<br />
														<span class="vexpl">
															<?=gettext("ESP is encryption, AH is authentication only"); ?>
														</span>
													</td>
												</tr>
												<tr id="opt_enc">
													<td width="22%" valign="top" class="vncellreq"><?=gettext("Encryption algorithms"); ?></td>
													<td width="78%" class="vtable">
														<table class="table table-striped" border="0" cellspacing="0" cellpadding="0" summary="encryption">
														<?php
773 774 775 776 777 778
                                                        foreach ($p2_ealgos as $algo => $algodata) :
                                                            $checked = '';
                                                            if (is_array($pconfig['ealgos']) && in_array($algo, $pconfig['ealgos'])) {
                                                                $checked = " checked=\"checked\"";
                                                            }
                                                            ?>
779
															<tr>
780 781 782 783 784 785 786 787 788
                                                            <td>
                                                                <input type="checkbox" name="ealgos[]" value="<?=$algo;?>"<?=$checked?> />
                                                            </td>
                                                            <td>
                                                                <?=htmlspecialchars($algodata['name']);?>
                                                            </td>
                                                            <td>
                                                                <?php if (is_array($algodata['keysel'])) :
?>
789 790 791 792
																	&nbsp;&nbsp;
																	<select name="keylen_<?=$algo;?>" class="formselect">
																		<option value="auto"><?=gettext("auto"); ?></option>
																		<?php
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
                                                                            $key_hi = $algodata['keysel']['hi'];
                                                                            $key_lo = $algodata['keysel']['lo'];
                                                                            $key_step = $algodata['keysel']['step'];
                                                                        for ($keylen = $key_hi; $keylen >= $key_lo; $keylen -= $key_step) :
                                                                            $selected = "";
                                            //									if ($checked && in_array("keylen_".$algo,$pconfig))
                                                                            if ($keylen == $pconfig["keylen_".$algo]) {
                                                                                $selected = " selected=\"selected\"";
                                                                            }
                                                                        ?>
																		<option value="<?=$keylen;
?>"<?=$selected;
?>><?=$keylen;
?> <?=gettext("bits"); ?></option>
																		<?php
                                                                        endfor; ?>
809
																	</select>
810 811
																	<?php
endif; ?>
812 813 814
																</td>
															</tr>

815 816
															<?php
                                                        endforeach; ?>
817 818 819 820

														</table>
														<br />
														<?=gettext("Hint: use 3DES for best compatibility or if you have a hardware " .
821 822
                                                        "crypto accelerator card. Blowfish is usually the fastest in " .
                                                        "software encryption"); ?>.
823 824 825 826 827
													</td>
												</tr>
												<tr>
													<td width="22%" valign="top" class="vncellreq"><?=gettext("Hash algorithms"); ?></td>
													<td width="78%" class="vtable">
828 829 830 831 832
													<?php foreach ($p2_halgos as $algo => $algoname) :
?>
														<input type="checkbox" name="halgos[]" value="<?=$algo;?>" <?php if (in_array($algo, $pconfig['halgos'])) {
                                                            echo "checked=\"checked\"";
} ?> />
833 834
														<?=htmlspecialchars($algoname);?>
														<br />
835 836
													<?php
endforeach; ?>
837 838 839 840 841
													</td>
												</tr>
												<tr>
													<td width="22%" valign="top" class="vncellreq"><?=gettext("PFS key group"); ?></td>
													<td width="78%" class="vtable">
842 843
													<?php if (!isset($pconfig['mobile']) || !isset($a_client['pfs_group'])) :
?>
844
														<select name="pfsgroup" class="formselect">
845 846 847 848 849
														<?php foreach ($p2_pfskeygroups as $keygroup => $keygroupname) :
?>
															<option value="<?=$keygroup;?>" <?php if ($keygroup == $pconfig['pfsgroup']) {
                                                                echo "selected=\"selected\"";
} ?>>
850 851
																<?=htmlspecialchars($keygroupname);?>
															</option>
852 853
														<?php
endforeach; ?>
854 855
														</select>
														<br />
856 857 858
														<?php
else :
?>
859 860 861 862 863 864 865

														<select class="formselect" disabled="disabled">
															<option selected="selected"><?=$p2_pfskeygroups[$a_client['pfs_group']];?></option>
														</select>
														<input name="pfsgroup" type="hidden" value="<?=htmlspecialchars($pconfig['pfsgroup']);?>" />
														<br />
														<span class="vexpl"><em><?=gettext("Set globally in mobile client options"); ?></em></span>
866 867
													<?php
endif; ?>
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
													</td>
												</tr>
												<tr>
													<td width="22%" valign="top" class="vncell"><?=gettext("Lifetime"); ?></td>
													<td width="78%" class="vtable">
														<input name="lifetime" type="text" class="formfld unknown" id="lifetime" size="20" value="<?=htmlspecialchars($pconfig['lifetime']);?>" />
														<?=gettext("seconds"); ?>
													</td>
												</tr>
												<tr>
													<td colspan="2" class="list" height="12"></td>
												</tr>
												<tr>
													<td colspan="2" valign="top" class="listtopic"><?=gettext("Advanced Options"); ?></td>
												</tr>
												<tr>
													<td width="22%" valign="top" class="vncell"><?=gettext("Automatically ping host"); ?></td>
													<td width="78%" class="vtable">
														<input name="pinghost" type="text" class="formfld unknown" id="pinghost" size="28" value="<?=htmlspecialchars($pconfig['pinghost']);?>" />
														<?=gettext("IP address"); ?>
													</td>
												</tr>
												<tr>
													<td width="22%" valign="top">&nbsp;</td>
													<td width="78%">
893 894
													<?php if ($pconfig['mobile']) :
?>
895 896
														<input name="mobile" type="hidden" value="true" />
														<input name="remoteid_type" type="hidden" value="mobile" />
897 898
													<?php
endif; ?>
899 900 901 902 903 904 905
														<input name="Submit" type="submit" class="btn btn-primary" value="<?=gettext("Save"); ?>" />
														<input name="ikeid" type="hidden" value="<?=htmlspecialchars($pconfig['ikeid']);?>" />
														<input name="uniqid" type="hidden" value="<?=htmlspecialchars($pconfig['uniqid']);?>" />
													</td>
												</tr>
											</table>
										</div>
Ad Schellevis's avatar
Ad Schellevis committed
906 907 908
									</td>
								</tr>
							</table>
909 910 911 912 913 914 915
						</div>
					</form>
				</div>
			</section>
		</div>
	</div>
</section>
Ad Schellevis's avatar
Ad Schellevis committed
916 917 918 919 920 921
<script type="text/javascript">
//<![CDATA[
change_mode('<?=htmlspecialchars($pconfig['mode'])?>');
change_protocol('<?=htmlspecialchars($pconfig['proto'])?>');
typesel_change_local(<?=htmlspecialchars($pconfig['localid_netbits'])?>);
typesel_change_natlocal(<?=htmlspecialchars($pconfig['natlocalid_netbits'])?>);
922 923
<?php if (!isset($pconfig['mobile'])) :
?>
Ad Schellevis's avatar
Ad Schellevis committed
924
typesel_change_remote(<?=htmlspecialchars($pconfig['remoteid_netbits'])?>);
925 926
<?php
endif; ?>
Ad Schellevis's avatar
Ad Schellevis committed
927 928
//]]>
</script>
929
<?php include("foot.inc"); ?>
Ad Schellevis's avatar
Ad Schellevis committed
930 931 932 933 934 935 936
</body>
</html>

<?php

/* local utility functions */

937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
function pconfig_to_ealgos(& $pconfig)
{
    global $p2_ealgos;

    $ealgos = array();
    if (is_array($pconfig['ealgos'])) {
        foreach ($p2_ealgos as $algo_name => $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;
            }
        }
    }
Ad Schellevis's avatar
Ad Schellevis committed
954

955
    return $ealgos;
Ad Schellevis's avatar
Ad Schellevis committed
956 957
}

958 959
function ealgos_to_pconfig(& $ealgos, & $pconfig)
{
Ad Schellevis's avatar
Ad Schellevis committed
960

961 962 963 964 965 966 967
    $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'];
        }
    }
Ad Schellevis's avatar
Ad Schellevis committed
968

969
    return $ealgos;
Ad Schellevis's avatar
Ad Schellevis committed
970 971
}

972 973
function pconfig_to_idinfo($prefix, & $pconfig)
{
Ad Schellevis's avatar
Ad Schellevis committed
974

975 976 977 978 979 980 981 982 983 984 985 986 987
    $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 );
    }
Ad Schellevis's avatar
Ad Schellevis committed
988 989
}

990 991
function idinfo_to_pconfig($prefix, & $idinfo, & $pconfig)
{
Ad Schellevis's avatar
Ad Schellevis committed
992

993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
    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;
    }
Ad Schellevis's avatar
Ad Schellevis committed
1008
}