Project

General

Profile

Download (35.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	vpn_ipsec_phase2.php
4
	part of m0n0wall (http://m0n0.ch/wall)
5

    
6
	Copyright (C) 2003-2005 Manuel Kasper <mk@neon1.net>.
7
	Copyright (C) 2008 Shrew Soft Inc
8
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
9
	All rights reserved.
10

    
11
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13

    
14
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16

    
17
	2. Redistributions in binary form must reproduce the above copyright
18
	   notice, this list of conditions and the following disclaimer in the
19
	   documentation and/or other materials provided with the distribution.
20

    
21
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
	POSSIBILITY OF SUCH DAMAGE.
31
*/
32

    
33
##|+PRIV
34
##|*IDENT=page-vpn-ipsec-editphase2
35
##|*NAME=VPN: IPsec: Edit Phase 2 page
36
##|*DESCR=Allow access to the 'VPN: IPsec: Edit Phase 2' page.
37
##|*MATCH=vpn_ipsec_phase2.php*
38
##|-PRIV
39

    
40
require("functions.inc");
41
require("guiconfig.inc");
42
require_once("ipsec.inc");
43
require_once("vpn.inc");
44

    
45
if (!is_array($config['ipsec']['client']))
46
	$config['ipsec']['client'] = array();
47

    
48
$a_client = &$config['ipsec']['client'];
49

    
50
if (!is_array($config['ipsec']['phase1']))
51
	$config['ipsec']['phase1'] = array();
52

    
53
if (!is_array($config['ipsec']['phase2']))
54
	$config['ipsec']['phase2'] = array();
55

    
56
$a_phase1 = &$config['ipsec']['phase1'];
57
$a_phase2 = &$config['ipsec']['phase2'];
58

    
59
if (!empty($_GET['p2index']))
60
	$uindex = $_GET['p2index'];
61
if (!empty($_POST['uniqid']))
62
	$uindex = $_POST['uniqid'];
63

    
64
if (!empty($_GET['dup']))
65
	$uindex = $_GET['dup'];
66

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

    
77
if ($ph2found === true)
78
{
79
	$pconfig['ikeid'] = $ph2['ikeid'];
80
	$pconfig['disabled'] = isset($ph2['disabled']);
81
	$pconfig['mode'] = $ph2['mode'];
82
	$pconfig['descr'] = $ph2['descr'];
83
	$pconfig['uniqid'] = $ph2['uniqid'];
84

    
85
	if (!empty($ph2['natlocalid']))
86
		idinfo_to_pconfig("natlocal",$ph2['natlocalid'],$pconfig);
87
	idinfo_to_pconfig("local",$ph2['localid'],$pconfig);
88
	idinfo_to_pconfig("remote",$ph2['remoteid'],$pconfig);
89

    
90
	$pconfig['proto'] = $ph2['protocol'];
91
	ealgos_to_pconfig($ph2['encryption-algorithm-option'],$pconfig);
92
	$pconfig['halgos'] = $ph2['hash-algorithm-option'];
93
	$pconfig['pfsgroup'] = $ph2['pfsgroup'];
94
	$pconfig['lifetime'] = $ph2['lifetime'];
95
	$pconfig['pinghost'] = $ph2['pinghost'];
96
	$pconfig['reqid'] = $ph2['reqid'];
97

    
98
	if (isset($ph2['mobile']))
99
		$pconfig['mobile'] = true;
100
}
101
else
102
{
103
	$pconfig['ikeid'] = $_GET['ikeid'];
104

    
105
	/* defaults */
106
	$pconfig['localid_type'] = "lan";
107
	$pconfig['remoteid_type'] = "network";
108
	$pconfig['proto'] = "esp";
109
	$pconfig['ealgos'] = explode(",", "aes");
110
	$pconfig['halgos'] = explode(",", "hmac_sha1");
111
	$pconfig['pfsgroup'] = "0";
112
	$pconfig['lifetime'] = "3600";
113
	$pconfig['uniqid'] = uniqid();
114

    
115
	/* mobile client */
116
	if($_GET['mobile'])
117
		$pconfig['mobile']=true;
118
}
119

    
120
unset($ph2);
121
if (!empty($_GET['dup'])) {
122
	unset($uindex);
123
	unset($p2index);
124
	$pconfig['uniqid'] = uniqid();
125
	$pconfig['reqid'] = ipsec_new_reqid();
126
}
127

    
128
if ($_POST) {
129

    
130
	unset($input_errors);
131
	$pconfig = $_POST;
132

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

    
136
	/* input validation */
137
	$reqdfields = explode(" ", "localid_type uniqid");
138
	$reqdfieldsn = array(gettext("Local network type"), gettext("Unique Identifier"));
139
	if (!isset($pconfig['mobile'])){
140
		$reqdfields[] = "remoteid_type";
141
		$reqdfieldsn[] = gettext("Remote network type");
142
	}
143

    
144
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
145

    
146
	if(($pconfig['mode'] == "tunnel") || ($pconfig['mode'] == "tunnel6")) 
147
	{
148
		switch ($pconfig['localid_type']) {
149
			case "network":
150
				if (($pconfig['localid_netbits'] != 0 && !$pconfig['localid_netbits']) || !is_numeric($pconfig['localid_netbits']))
151
					$input_errors[] = gettext("A valid local network bit count must be specified.");
152
			case "address":
153
				if (!$pconfig['localid_address'] || !is_ipaddr($pconfig['localid_address']))
154
					$input_errors[] = gettext("A valid local network IP address must be specified.");
155
				elseif (is_ipaddrv4($pconfig['localid_address']) && ($pconfig['mode'] != "tunnel"))
156
					$input_errors[] = gettext("A valid local network IPv4 address must be specified or you need to change Mode to IPv6");
157
				elseif (is_ipaddrv6($pconfig['localid_address']) && ($pconfig['mode'] != "tunnel6"))
158
					$input_errors[] = gettext("A valid local network IPv6 address must be specified or you need to change Mode to IPv4");
159
				break;
160
		}
161
		/* Check if the localid_type is an interface, to confirm if it has a valid subnet. */
162
		if (is_array($config['interfaces'][$pconfig['localid_type']])) {
163
			// Don't let an empty subnet into racoon.conf, it can cause parse errors. Ticket #2201.
164
			$address = get_interface_ip($pconfig['localid_type']);
165
			$netbits = get_interface_subnet($pconfig['localid_type']);
166

    
167
			if (empty($address) || empty($netbits))
168
				$input_errors[] = gettext("Invalid Local Network.") . " " . convert_friendly_interface_to_friendly_descr($pconfig['localid_type']) . " " . gettext("has no subnet.");
169
		}
170

    
171
		if (!empty($pconfig['natlocalid_address'])) {
172
			switch ($pconfig['natlocalid_type']) {
173
				case "network":
174
					if (($pconfig['natlocalid_netbits'] != 0 && !$pconfig['natlocalid_netbits']) || !is_numeric($pconfig['natlocalid_netbits']))
175
						$input_errors[] = gettext("A valid NAT local network bit count must be specified.");
176
					if ($pconfig['localid_type'] == "address")
177
						$input_errors[] = gettext("You cannot configure a network type address for NAT while only an address type is selected for local source."); 
178
				case "address":
179
					if (!empty($pconfig['natlocalid_address']) && !is_ipaddr($pconfig['natlocalid_address']))
180
						$input_errors[] = gettext("A valid NAT local network IP address must be specified.");
181
					elseif (is_ipaddrv4($pconfig['natlocalid_address']) && ($pconfig['mode'] != "tunnel"))
182
						$input_errors[] = gettext("A valid NAT local network IPv4 address must be specified or you need to change Mode to IPv6");
183
					elseif (is_ipaddrv6($pconfig['natlocalid_address']) && ($pconfig['mode'] != "tunnel6"))
184
						$input_errors[] = gettext("A valid NAT local network IPv6 address must be specified or you need to change Mode to IPv4");
185
					break;
186
			}
187

    
188
			if (is_array($config['interfaces'][$pconfig['natlocalid_type']])) {
189
				// Don't let an empty subnet into racoon.conf, it can cause parse errors. Ticket #2201.
190
				$address = get_interface_ip($pconfig['natlocalid_type']);
191
				$netbits = get_interface_subnet($pconfig['natlocalid_type']);
192

    
193
				if (empty($address) || empty($netbits))
194
					$input_errors[] = gettext("Invalid Local Network.") . " " . convert_friendly_interface_to_friendly_descr($pconfig['natlocalid_type']) . " " . gettext("has no subnet.");
195
			}
196
		}
197

    
198
		switch ($pconfig['remoteid_type']) {
199
			case "network":
200
				if (($pconfig['remoteid_netbits'] != 0 && !$pconfig['remoteid_netbits']) || !is_numeric($pconfig['remoteid_netbits']))
201
					$input_errors[] = gettext("A valid remote network bit count must be specified.");
202
			case "address":
203
				if (!$pconfig['remoteid_address'] || !is_ipaddr($pconfig['remoteid_address']))
204
					$input_errors[] = gettext("A valid remote network IP address must be specified.");
205
				elseif (is_ipaddrv4($pconfig['remoteid_address']) && ($pconfig['mode'] != "tunnel"))
206
					$input_errors[] = gettext("A valid remote network IPv4 address must be specified or you need to change Mode to IPv6");
207
				elseif (is_ipaddrv6($pconfig['remoteid_address']) && ($pconfig['mode'] != "tunnel6"))
208
					$input_errors[] = gettext("A valid remote network IPv6 address must be specified or you need to change Mode to IPv4");
209
				break;
210
		}
211
	}
212
	/* Validate enabled phase2's are not duplicates */
213
	if (isset($pconfig['mobile'])){
214
		/* User is adding phase 2 for mobile phase1 */
215
		foreach($a_phase2 as $key => $name){
216
			if (isset($name['mobile']) && $name['uniqid'] != $pconfig['uniqid']) {
217
				/* check duplicate localids only for mobile clents */
218
				$localid_data = ipsec_idinfo_to_cidr($name['localid'], false, $name['mode']);
219
				$entered = array();
220
				$entered['type'] = $pconfig['localid_type'];
221
				if (isset($pconfig['localid_address'])) $entered['address'] = $pconfig['localid_address'];
222
				if (isset($pconfig['localid_netbits'])) $entered['netbits'] = $pconfig['localid_netbits'];
223
				$entered_localid_data = ipsec_idinfo_to_cidr($entered, false, $pconfig['mode']);
224
				if ($localid_data == $entered_localid_data){
225
					/* adding new p2 entry */
226
					$input_errors[] = gettext("Phase2 with this Local Network is already defined for mobile clients.");
227
					break;
228
				}
229
			}
230
		}
231
	}else{
232
		/* User is adding phase 2 for site-to-site phase1 */
233
		$input_error = 0;
234
		foreach($a_phase2 as $key => $name){
235
			if (!isset($name['mobile']) && $pconfig['ikeid'] == $name['ikeid'] && $pconfig['uniqid'] != $name['uniqid']) {
236
				/* check duplicate subnets only for given phase1 */
237
				$localid_data = ipsec_idinfo_to_cidr($name['localid'], false, $name['mode']);
238
				$remoteid_data = ipsec_idinfo_to_cidr($name['remoteid'], false, $name['mode']);
239
				$entered_local = array();
240
				$entered_local['type'] = $pconfig['localid_type'];
241
				if (isset($pconfig['localid_address'])) $entered_local['address'] = $pconfig['localid_address'];
242
				if (isset($pconfig['localid_netbits'])) $entered_local['netbits'] = $pconfig['localid_netbits'];
243
				$entered_localid_data = ipsec_idinfo_to_cidr($entered_local, false, $pconfig['mode']);
244
				$entered_remote = array();
245
				$entered_remote['type'] = $pconfig['remoteid_type'];
246
				if (isset($pconfig['remoteid_address'])) $entered_remote['address'] = $pconfig['remoteid_address'];
247
				if (isset($pconfig['remoteid_netbits'])) $entered_remote['netbits'] = $pconfig['remoteid_netbits'];
248
				$entered_remoteid_data = ipsec_idinfo_to_cidr($entered_remote, false, $pconfig['mode']);
249
				if ($localid_data == $entered_localid_data && $remoteid_data == $entered_remoteid_data) { 
250
					/* adding new p2 entry */
251
					$input_errors[] = gettext("Phase2 with this Local/Remote networks combination is already defined for this Phase1.");
252
					break;
253
				}
254
			}
255
		}
256
		foreach ($a_phase1 as $phase1) {
257
			if($phase1['ikeid'] == $pconfig['ikeid']) {
258
				/* This is the P1 for this entry, validate its remote-gateway and local interface isn't within tunnel */
259
				$entered_local = array();
260
				$entered_local['type'] = $pconfig['localid_type'];
261
				if (isset($pconfig['localid_address'])) $entered_local['address'] = $pconfig['localid_address'];
262
				if (isset($pconfig['localid_netbits'])) $entered_local['netbits'] = $pconfig['localid_netbits'];
263
				$entered_localid_data = ipsec_idinfo_to_cidr($entered_local, false, $pconfig['mode']);
264
				list($entered_local_network, $entered_local_mask) = explode('/', $entered_localid_data);
265
				$entered_remote = array();
266
				$entered_remote['type'] = $pconfig['remoteid_type'];
267
				if (isset($pconfig['remoteid_address'])) $entered_remote['address'] = $pconfig['remoteid_address'];
268
				if (isset($pconfig['remoteid_netbits'])) $entered_remote['netbits'] = $pconfig['remoteid_netbits'];
269
				$entered_remoteid_data = ipsec_idinfo_to_cidr($entered_remote, false, $pconfig['mode']);
270
				list($entered_remote_network, $entered_remote_mask) = explode('/', $entered_remoteid_data);
271
				if ($phase1['protocol'] == "inet6") { 
272
					$if = get_failover_interface($phase1['interface'], "inet6");
273
					$interfaceip = get_interface_ipv6($if);
274
				} else {
275
					$if = get_failover_interface($phase1['interface']);
276
					$interfaceip = get_interface_ip($if);
277
				}
278
				/* skip validation for hostnames, they're subject to change anyway */
279
				if (is_ipaddr($phase1['remote-gateway'])) {
280
					if ($pconfig['mode'] == "tunnel") {
281
						if(check_subnets_overlap($interfaceip, 32, $entered_local_network, $entered_local_mask) && check_subnets_overlap($phase1['remote-gateway'], 32, $entered_remote_network, $entered_remote_mask)) {
282
							$input_errors[] = gettext("The local and remote networks of a phase 2 entry cannot overlap the outside of the tunnel (interface and remote gateway) configured in its phase 1.");
283
							break;
284
						}
285
					} else if ($pconfig['mode'] == "tunnel6") {
286
						if(check_subnetsv6_overlap($interfaceip, 128, $entered_local_network, $entered_local_mask) && check_subnets_overlap($phase1['remote-gateway'], 128, $entered_remote_network, $entered_remote_mask)) {
287
							$input_errors[] = gettext("The local and remote networks of a phase 2 entry cannot overlap the outside of the tunnel (interface and remote gateway) configured in its phase 1.");
288
							break;
289
						}							
290
					}				
291
				}
292
			}
293
		}
294
        }
295

    
296
	/* For ESP protocol, handle encryption algorithms */
297
	if ( $pconfig['proto'] == "esp") {
298
		$ealgos = pconfig_to_ealgos($pconfig);
299

    
300
		if (!count($ealgos)) {
301
			$input_errors[] = gettext("At least one encryption algorithm must be selected.");
302
		} else {
303
			if (empty($pconfig['halgos'])) {
304
				foreach ($ealgos as $ealgo) {
305
					if (!strpos($ealgo['name'], "gcm")) {
306
						$input_errors[] = gettext("At least one hashing algorithm needs to be selected.");
307
						break;
308
					}
309
				}
310
			}
311
		}
312
		
313
	}
314
	if (($_POST['lifetime'] && !is_numeric($_POST['lifetime']))) {
315
		$input_errors[] = gettext("The P2 lifetime must be an integer.");
316
	}
317

    
318
	if (!$input_errors) {
319

    
320
		$ph2ent = array();
321
		$ph2ent['ikeid'] = $pconfig['ikeid'];
322
		$ph2ent['uniqid'] = $pconfig['uniqid'];
323
		$ph2ent['mode'] = $pconfig['mode'];
324
		$ph2ent['disabled'] = $pconfig['disabled'] ? true : false;
325
		if (!isset($pconfig['reqid']))
326
			$ph2ent['reqid'] = ipsec_new_reqid();
327
		else
328
			$ph2ent['reqid'] = $pconfig['reqid'];
329

    
330
		if(($ph2ent['mode'] == "tunnel") || ($ph2ent['mode'] == "tunnel6")){
331
			if (!empty($pconfig['natlocalid_address']))
332
				$ph2ent['natlocalid'] = pconfig_to_idinfo("natlocal",$pconfig);
333
			$ph2ent['localid'] = pconfig_to_idinfo("local",$pconfig);
334
			$ph2ent['remoteid'] = pconfig_to_idinfo("remote",$pconfig);
335
		}
336

    
337
		$ph2ent['protocol'] = $pconfig['proto'];
338
		$ph2ent['encryption-algorithm-option'] = $ealgos;
339
		if (!empty($pconfig['halgos']))
340
			$ph2ent['hash-algorithm-option'] = $pconfig['halgos'];
341
		else
342
			unset($ph2ent['hash-algorithm-option']);
343
		$ph2ent['pfsgroup'] = $pconfig['pfsgroup'];
344
		$ph2ent['lifetime'] = $pconfig['lifetime'];
345
		$ph2ent['pinghost'] = $pconfig['pinghost'];
346
		$ph2ent['descr'] = $pconfig['descr'];
347

    
348
		if (isset($pconfig['mobile']))
349
			$ph2ent['mobile'] = true;
350

    
351
		if ($ph2found === true && $a_phase2[$p2index])
352
			$a_phase2[$p2index] = $ph2ent;
353
		else
354
			$a_phase2[] = $ph2ent;
355

    
356

    
357
		write_config();
358
		mark_subsystem_dirty('ipsec');
359

    
360
		header("Location: vpn_ipsec.php");
361
		exit;
362
	}
363
}
364

    
365
if ($pconfig['mobile'])
366
    $pgtitle = array(gettext("VPN"),gettext("IPsec"),gettext("Edit Phase 2"), gettext("Mobile Client"));
367
else
368
    $pgtitle = array(gettext("VPN"),gettext("IPsec"),gettext("Edit Phase 2"));
369
$shortcut_section = "ipsec";
370

    
371

    
372
include("head.inc");
373

    
374
?>
375

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

    
382
function change_mode() {
383
	index = document.iform.mode.selectedIndex;
384
	value = document.iform.mode.options[index].value;
385
	if ((value == 'tunnel') || (value == 'tunnel6')) {
386
		document.getElementById('opt_localid').style.display = '';
387
<?php if (!isset($pconfig['mobile'])): ?>
388
		document.getElementById('opt_remoteid').style.display = '';
389
<?php endif; ?>
390
	} else {
391
		document.getElementById('opt_localid').style.display = 'none';
392
<?php if (!isset($pconfig['mobile'])): ?>
393
		document.getElementById('opt_remoteid').style.display = 'none';
394
<?php endif; ?>
395
	}
396
}
397

    
398
function typesel_change_natlocal(bits) {
399
	var value = document.iform.mode.options[index].value;
400
	if (typeof(bits) === "undefined") {
401
		if (value === "tunnel") {
402
			bits = 24;
403
		}
404
		else if (value === "tunnel6") {
405
			bits = 64;
406
		}
407
	}
408
	var address_is_blank = !/\S/.test(document.iform.natlocalid_address.value);
409
	switch (document.iform.natlocalid_type.selectedIndex) {
410
		case 0:	/* single */
411
			document.iform.natlocalid_address.disabled = 0;
412
			if (address_is_blank) {
413
				document.iform.natlocalid_netbits.value = 0;
414
			}
415
			document.iform.natlocalid_netbits.disabled = 1;
416
			break;
417
		case 1:	/* network */
418
			document.iform.natlocalid_address.disabled = 0;
419
			if (address_is_blank) {
420
				document.iform.natlocalid_netbits.value = bits;
421
			}
422
			document.iform.natlocalid_netbits.disabled = 0;
423
			break;
424
		case 3:	/* none */
425
			document.iform.natlocalid_address.disabled = 1;
426
			document.iform.natlocalid_netbits.disabled = 1;
427
			break;
428
		default:
429
			document.iform.natlocalid_address.value = "";
430
			document.iform.natlocalid_address.disabled = 1;
431
			if (address_is_blank) {
432
				document.iform.natlocalid_netbits.value = 0;
433
			}
434
			document.iform.natlocalid_netbits.disabled = 1;
435
			break;
436
	}
437
}
438

    
439
function typesel_change_local(bits) {
440
	var value = document.iform.mode.options[index].value;
441
	if (typeof(bits) === "undefined") {
442
		if (value === "tunnel") {
443
			bits = 24;
444
		}
445
		else if (value === "tunnel6") {
446
			bits = 64;
447
		}
448
	}
449
	var address_is_blank = !/\S/.test(document.iform.localid_address.value);
450
	switch (document.iform.localid_type.selectedIndex) {
451
		case 0:	/* single */
452
			document.iform.localid_address.disabled = 0;
453
			if (address_is_blank) {
454
				document.iform.localid_netbits.value = 0;
455
			}
456
			document.iform.localid_netbits.disabled = 1;
457
			break;
458
		case 1:	/* network */
459
			document.iform.localid_address.disabled = 0;
460
			if (address_is_blank) {
461
				document.iform.localid_netbits.value = bits;
462
			}
463
			document.iform.localid_netbits.disabled = 0;
464
			break;
465
		case 3:	/* none */
466
			document.iform.localid_address.disabled = 1;
467
			document.iform.localid_netbits.disabled = 1;
468
			break;
469
		default:
470
			document.iform.localid_address.value = "";
471
			document.iform.localid_address.disabled = 1;
472
			if (address_is_blank) {
473
				document.iform.localid_netbits.value = 0;
474
			}
475
			document.iform.localid_netbits.disabled = 1;
476
			break;
477
	}
478
}
479

    
480
<?php if (!isset($pconfig['mobile'])): ?>
481

    
482
function typesel_change_remote(bits) {
483
	var value = document.iform.mode.options[index].value;
484
	if (typeof(bits) === "undefined") {
485
		if (value === "tunnel") {
486
			bits = 24;
487
		}
488
		else if (value === "tunnel6") {
489
			bits = 64;
490
		}
491
	}
492
	var address_is_blank = !/\S/.test(document.iform.remoteid_address.value);
493
	switch (document.iform.remoteid_type.selectedIndex) {
494
		case 0:	/* single */
495
			document.iform.remoteid_address.disabled = 0;
496
			if (address_is_blank) {
497
				document.iform.remoteid_netbits.value = 0;
498
			}
499
			document.iform.remoteid_netbits.disabled = 1;
500
			break;
501
		case 1:	/* network */
502
			document.iform.remoteid_address.disabled = 0;
503
			if (address_is_blank) {
504
				document.iform.remoteid_netbits.value = bits;
505
			}
506
			document.iform.remoteid_netbits.disabled = 0;
507
			break;
508
		default:
509
			document.iform.remoteid_address.value = "";
510
			document.iform.remoteid_address.disabled = 1;
511
			if (address_is_blank) {
512
				document.iform.remoteid_netbits.value = 0;
513
			}
514
			document.iform.remoteid_netbits.disabled = 1;
515
			break;
516
	}
517
}
518

    
519
<?php endif; ?>
520

    
521
function change_protocol() {
522
	index = document.iform.proto.selectedIndex;
523
	value = document.iform.proto.options[index].value;
524
	if (value == 'esp')
525
		document.getElementById('opt_enc').style.display = '';
526
	else
527
		document.getElementById('opt_enc').style.display = 'none';
528
}
529

    
530
//]]>
531
</script>
532

    
533
<form action="vpn_ipsec_phase2.php" method="post" name="iform" id="iform">
534

    
535
<?php
536
	if ($input_errors)
537
		print_input_errors($input_errors);
538
?>
539

    
540
<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="vpn ipsec phase-2">
541
	<tr class="tabnavtbl">
542
		<td id="tabnav">
543
			<?php
544
				$tab_array = array();
545
				$tab_array[0] = array(gettext("Tunnels"), true, "vpn_ipsec.php");
546
				$tab_array[1] = array(gettext("Mobile clients"), false, "vpn_ipsec_mobile.php");
547
				$tab_array[2] = array(gettext("Pre-Shared Keys"), false, "vpn_ipsec_keys.php");
548
				$tab_array[3] = array(gettext("Advanced Settings"), false, "vpn_ipsec_settings.php");
549
				display_top_tabs($tab_array);
550
			?>
551
		</td>
552
	</tr>
553
	<tr>
554
		<td id="mainarea">
555
			<div class="tabcont">
556
				<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="main area">
557
					<tr>
558
						<td width="22%" valign="top" class="vncellreq"><?=gettext("Disabled"); ?></td>
559
						<td width="78%" class="vtable">
560
							<input name="disabled" type="checkbox" id="disabled" value="yes" <?php if ($pconfig['disabled']) echo "checked=\"checked\""; ?> />
561
							<strong><?=gettext("Disable this phase2 entry"); ?></strong>
562
							<br />
563
							<span class="vexpl"><?=gettext("Set this option to disable this phase2 entry without " .
564
							  "removing it from the list"); ?>.
565
							</span>
566
						</td>
567
					</tr>
568
					<tr>
569
						<td width="22%" valign="top" class="vncellreq"><?=gettext("Mode"); ?></td>
570
						<td width="78%" class="vtable">
571
							<select name="mode" class="formselect" onchange="change_mode()">
572
								<?php
573
									foreach($p2_modes as $name => $value):
574
										$selected = "";
575
										if ($name == $pconfig['mode'])
576
											$selected = "selected=\"selected\"";
577
								?>
578
								<option value="<?=$name;?>" <?=$selected;?>><?=$value;?></option>
579
								<?php endforeach; ?>
580
							</select>
581
						</td>
582
					</tr>
583
					<tr id="opt_localid">
584
						<td width="22%" valign="top" class="vncellreq"><?=gettext("Local Network"); ?></td>
585
						<td width="78%" class="vtable">
586
							<table border="0" cellspacing="0" cellpadding="0" summary="local network">
587
								<tr>
588
									<td><?=gettext("Type"); ?>:&nbsp;&nbsp;</td>
589
									<td></td>
590
									<td>
591
										<select name="localid_type" class="formselect" onchange="typesel_change_local()">
592
											<option value="address" <?php if ($pconfig['localid_type'] == "address") echo "selected=\"selected\"";?>><?=gettext("Address"); ?></option>
593
											<option value="network" <?php if ($pconfig['localid_type'] == "network") echo "selected=\"selected\"";?>><?=gettext("Network"); ?></option>
594
											<?php
595
												$iflist = get_configured_interface_with_descr();
596
												foreach ($iflist as $ifname => $ifdescr):
597
											?>
598
											<option value="<?=$ifname; ?>" <?php if ($pconfig['localid_type'] == $ifname ) echo "selected=\"selected\"";?>><?=sprintf(gettext("%s subnet"), $ifdescr); ?></option>
599
											<?php endforeach; ?>
600
										</select>
601
									</td>
602
								</tr>
603
								<tr>
604
									<td><?=gettext("Address:");?>&nbsp;&nbsp;</td>
605
									<td><?=$mandfldhtmlspc;?></td>
606
									<td>
607
										<input name="localid_address" type="text" class="formfld unknown ipv4v6" id="localid_address" size="28" value="<?=htmlspecialchars($pconfig['localid_address']);?>" />
608
										/
609
										<select name="localid_netbits" class="formselect ipv4v6" id="localid_netbits">
610
										<?php for ($i = 128; $i >= 0; $i--): ?>
611
											<option value="<?=$i;?>" <?php if (isset($pconfig['localid_netbits']) && $i == $pconfig['localid_netbits']) echo "selected=\"selected\""; ?>>
612
												<?=$i;?>
613
											</option>
614
										<?php endfor; ?>
615
										</select>
616
									</td>
617
								</tr>
618
								<tr> <td colspan="3">
619
								<br />
620
								<?php echo gettext("In case you need NAT/BINAT on this network specify the address to be translated"); ?>
621
								</td></tr>
622
								<tr>
623
									<td><?=gettext("Type"); ?>:&nbsp;&nbsp;</td>
624
									<td></td>
625
									<td>
626
										<select name="natlocalid_type" class="formselect" onchange="typesel_change_natlocal()">
627
											<option value="address" <?php if ($pconfig['natlocalid_type'] == "address") echo "selected=\"selected\"";?>><?=gettext("Address"); ?></option>
628
											<option value="network" <?php if ($pconfig['natlocalid_type'] == "network") echo "selected=\"selected\"";?>><?=gettext("Network"); ?></option>
629
											<?php
630
												$iflist = get_configured_interface_with_descr();
631
												foreach ($iflist as $ifname => $ifdescr):
632
											?>
633
											<option value="<?=$ifname; ?>" <?php if ($pconfig['natlocalid_type'] == $ifname ) echo "selected=\"selected\"";?>><?=sprintf(gettext("%s subnet"), $ifdescr); ?></option>
634
											<?php endforeach; ?>
635
											<option value="none" <?php if (empty($pconfig['natlocalid_type']) || $pconfig['natlocalid_type'] == "none" ) echo "selected=\"selected\"";?>><?=gettext("None"); ?></option>
636
										</select>
637
									</td>
638
								</tr>
639
								<tr>
640
									<td><?=gettext("Address:");?>&nbsp;&nbsp;</td>
641
									<td><?=$mandfldhtmlspc;?></td>
642
									<td>
643
										<input name="natlocalid_address" type="text" class="formfld unknown ipv4v6" id="natlocalid_address" size="28" value="<?=htmlspecialchars($pconfig['natlocalid_address']);?>" />
644
										/
645
										<select name="natlocalid_netbits" class="formselect ipv4v6" id="natlocalid_netbits">
646
										<?php for ($i = 128; $i >= 0; $i--): ?>
647
											<option value="<?=$i;?>" <?php if (isset($pconfig['natlocalid_netbits']) && $i == $pconfig['natlocalid_netbits']) echo "selected=\"selected\""; ?>>
648
												<?=$i;?>
649
											</option>
650
										<?php endfor; ?>
651
										</select>
652
									</td>
653
								</tr>
654
							</table>
655
						</td>
656
					</tr>
657

    
658
					<?php if (!isset($pconfig['mobile'])): ?>
659
					
660
					<tr id="opt_remoteid">
661
						<td width="22%" valign="top" class="vncellreq"><?=gettext("Remote Network"); ?></td>
662
						<td width="78%" class="vtable">
663
							<table border="0" cellspacing="0" cellpadding="0" summary="remote network">
664
								<tr>
665
									<td><?=gettext("Type"); ?>:&nbsp;&nbsp;</td>
666
									<td></td>
667
									<td>
668
										<select name="remoteid_type" class="formselect" onchange="typesel_change_remote()">
669
											<option value="address" <?php if ($pconfig['remoteid_type'] == "address") echo "selected=\"selected\""; ?>><?=gettext("Address"); ?></option>
670
											<option value="network" <?php if ($pconfig['remoteid_type'] == "network") echo "selected=\"selected\""; ?>><?=gettext("Network"); ?></option>
671
										</select>
672
									</td>
673
								</tr>
674
								<tr>
675
									<td><?=gettext("Address"); ?>:&nbsp;&nbsp;</td>
676
									<td><?=$mandfldhtmlspc;?></td>
677
									<td>
678
										<input name="remoteid_address" type="text" class="formfld unknown ipv4v6" id="remoteid_address" size="28" value="<?=htmlspecialchars($pconfig['remoteid_address']);?>" />
679
										/
680
										<select name="remoteid_netbits" class="formselect ipv4v6" id="remoteid_netbits">
681
										<?php for ($i = 128; $i >= 0; $i--) { 
682
											
683
											echo "<option value=\"{$i}\"";
684
											if (isset($pconfig['remoteid_netbits']) && $i == $pconfig['remoteid_netbits']) echo " selected=\"selected\"";
685
											echo ">{$i}</option>\n";
686
											} ?>
687
										</select>
688
									</td>
689
								</tr>
690
							</table>
691
						</td>
692
					</tr>
693
					
694
					<?php endif; ?>
695
					
696
					<tr>
697
						<td width="22%" valign="top" class="vncell"><?=gettext("Description"); ?></td>
698
						<td width="78%" class="vtable">
699
							<input name="descr" type="text" class="formfld unknown" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>" />
700
							<br />
701
							<span class="vexpl">
702
								<?=gettext("You may enter a description here " .
703
								"for your reference (not parsed)"); ?>.
704
							</span>
705
						</td>
706
					</tr>
707
					<tr>
708
						<td colspan="2" class="list" height="12"></td>
709
					</tr>
710
					<tr>
711
						<td colspan="2" valign="top" class="listtopic">
712
							<?=gettext("Phase 2 proposal (SA/Key Exchange)"); ?>
713
						</td>
714
					</tr>
715
					<tr>
716
						<td width="22%" valign="top" class="vncellreq"><?=gettext("Protocol"); ?></td>
717
						<td width="78%" class="vtable">
718
							<select name="proto" class="formselect" onchange="change_protocol()">
719
							<?php foreach ($p2_protos as $proto => $protoname): ?>
720
								<option value="<?=$proto;?>" <?php if ($proto == $pconfig['proto']) echo "selected=\"selected\""; ?>>
721
									<?=htmlspecialchars($protoname);?>
722
								</option>
723
							<?php endforeach; ?>
724
							</select>
725
							<br />
726
							<span class="vexpl">
727
								<?=gettext("ESP is encryption, AH is authentication only"); ?>
728
							</span>
729
						</td>
730
					</tr>
731
					<tr id="opt_enc">
732
						<td width="22%" valign="top" class="vncellreq"><?=gettext("Encryption algorithms"); ?></td>
733
						<td width="78%" class="vtable">
734
							<table border="0" cellspacing="0" cellpadding="0" summary="encryption">
735
							<?php
736
								foreach ($p2_ealgos as $algo => $algodata):
737
									$checked = '';
738
									if (is_array($pconfig['ealgos']) && in_array($algo,$pconfig['ealgos']))
739
										$checked = " checked=\"checked\"";
740
								?>
741
								<tr>
742
									<td>
743
										<input type="checkbox" name="ealgos[]" value="<?=$algo;?>"<?=$checked?> />
744
									</td>
745
									<td>
746
										<?=htmlspecialchars($algodata['name']);?>
747
									</td>
748
									<td>
749
										<?php if(is_array($algodata['keysel'])): ?>
750
										&nbsp;&nbsp;
751
										<select name="keylen_<?=$algo;?>" class="formselect">
752
											<option value="auto"><?=gettext("auto"); ?></option>
753
											<?php
754
												$key_hi = $algodata['keysel']['hi'];
755
												$key_lo = $algodata['keysel']['lo'];
756
												$key_step = $algodata['keysel']['step'];
757
												for ($keylen = $key_hi; $keylen >= $key_lo; $keylen -= $key_step):
758
													$selected = "";
759
				//									if ($checked && in_array("keylen_".$algo,$pconfig))
760
													if ($keylen == $pconfig["keylen_".$algo])
761
														$selected = " selected=\"selected\"";
762
											?>
763
											<option value="<?=$keylen;?>"<?=$selected;?>><?=$keylen;?> <?=gettext("bits"); ?></option>
764
											<?php endfor; ?>
765
										</select>
766
										<?php endif; ?>
767
									</td>
768
								</tr>
769
								
770
								<?php endforeach; ?>
771
								
772
							</table>
773
							<br />
774
							<?=gettext("Hint: use 3DES for best compatibility or if you have a hardware " . 
775
							"crypto accelerator card. Blowfish is usually the fastest in " .
776
							"software encryption"); ?>.
777
						</td>
778
					</tr>
779
					<tr>
780
						<td width="22%" valign="top" class="vncellreq"><?=gettext("Hash algorithms"); ?></td>
781
						<td width="78%" class="vtable">
782
						<?php foreach ($p2_halgos as $algo => $algoname): ?>
783
							<input type="checkbox" name="halgos[]" value="<?=$algo;?>" <?php if (in_array($algo, $pconfig['halgos'])) echo "checked=\"checked\""; ?> />
784
							<?=htmlspecialchars($algoname);?>
785
							<br />
786
						<?php endforeach; ?>
787
						</td>
788
					</tr>
789
					<tr>
790
						<td width="22%" valign="top" class="vncellreq"><?=gettext("PFS key group"); ?></td>
791
						<td width="78%" class="vtable">
792
						<?php if (!isset($pconfig['mobile']) || !isset($a_client['pfs_group'])): ?>
793
							<select name="pfsgroup" class="formselect">
794
							<?php foreach ($p2_pfskeygroups as $keygroup => $keygroupname): ?>
795
								<option value="<?=$keygroup;?>" <?php if ($keygroup == $pconfig['pfsgroup']) echo "selected=\"selected\""; ?>>
796
									<?=htmlspecialchars($keygroupname);?>
797
								</option>
798
							<?php endforeach; ?>
799
							</select>
800
							<br />
801
							<?php else: ?>
802

    
803
							<select class="formselect" disabled="disabled">
804
								<option selected="selected"><?=$p2_pfskeygroups[$a_client['pfs_group']];?></option>
805
							</select>
806
							<input name="pfsgroup" type="hidden" value="<?=htmlspecialchars($pconfig['pfsgroup']);?>" />
807
							<br />
808
							<span class="vexpl"><em><?=gettext("Set globally in mobile client options"); ?></em></span>
809
						<?php endif; ?>
810
						</td>
811
					</tr>
812
					<tr>
813
						<td width="22%" valign="top" class="vncell"><?=gettext("Lifetime"); ?></td>
814
						<td width="78%" class="vtable">
815
							<input name="lifetime" type="text" class="formfld unknown" id="lifetime" size="20" value="<?=htmlspecialchars($pconfig['lifetime']);?>" />
816
							<?=gettext("seconds"); ?>
817
						</td>
818
					</tr>
819
					<tr>
820
						<td colspan="2" class="list" height="12"></td>
821
					</tr>
822
					<tr>
823
						<td colspan="2" valign="top" class="listtopic"><?=gettext("Advanced Options"); ?></td>
824
					</tr>
825
					<tr>
826
						<td width="22%" valign="top" class="vncell"><?=gettext("Automatically ping host"); ?></td>
827
						<td width="78%" class="vtable">
828
							<input name="pinghost" type="text" class="formfld unknown" id="pinghost" size="28" value="<?=htmlspecialchars($pconfig['pinghost']);?>" />
829
							<?=gettext("IP address"); ?>
830
						</td>
831
					</tr>
832
					<tr>
833
						<td width="22%" valign="top">&nbsp;</td>
834
						<td width="78%">
835
						<?php if ($pconfig['mobile']): ?>
836
							<input name="mobile" type="hidden" value="true" />
837
							<input name="remoteid_type" type="hidden" value="mobile" />
838
						<?php endif; ?>
839
							<input name="Submit" type="submit" class="formbtn" value="<?=gettext("Save"); ?>" />
840
							<input name="ikeid" type="hidden" value="<?=htmlspecialchars($pconfig['ikeid']);?>" />
841
						<?php if (!empty($pconfig['reqid'])): ?>
842
							<input name="reqid" type="hidden" value="<?=htmlspecialchars($pconfig['reqid']);?>" />
843
						<?php endif; ?>
844
							<input name="uniqid" type="hidden" value="<?=htmlspecialchars($pconfig['uniqid']);?>" />
845
						</td>
846
					</tr>
847
				</table>
848
			</div>
849
		</td>
850
	</tr>
851
</table>
852
</form>
853
<script type="text/javascript">
854
//<![CDATA[
855
change_mode('<?=htmlspecialchars($pconfig['mode'])?>');
856
change_protocol('<?=htmlspecialchars($pconfig['proto'])?>');
857
typesel_change_local(<?=htmlspecialchars($pconfig['localid_netbits'])?>);
858
typesel_change_natlocal(<?=htmlspecialchars($pconfig['natlocalid_netbits'])?>);
859
<?php if (!isset($pconfig['mobile'])): ?>
860
typesel_change_remote(<?=htmlspecialchars($pconfig['remoteid_netbits'])?>);
861
<?php endif; ?>
862
//]]>
863
</script>
864
<?php include("fend.inc"); ?>
865
</body>
866
</html>
867

    
868
<?php
869

    
870
/* local utility functions */
871

    
872
function pconfig_to_ealgos(& $pconfig) {
873
	global $p2_ealgos;
874

    
875
	$ealgos = array();
876
	if (is_array($pconfig['ealgos'])) {
877
		foreach ($p2_ealgos as $algo_name => $algo_data) {
878
			if (in_array($algo_name,$pconfig['ealgos'])) {
879
				$ealg = array();
880
				$ealg['name'] = $algo_name;
881
				if (is_array($algo_data['keysel']))
882
					$ealg['keylen'] = $_POST["keylen_".$algo_name];
883
				$ealgos[] = $ealg;
884
			}
885
		}
886
	}
887

    
888
	return $ealgos;
889
}
890

    
891
function ealgos_to_pconfig(& $ealgos,& $pconfig) {
892

    
893
	$pconfig['ealgos'] = array();
894
	foreach ($ealgos as $algo_data) {
895
		$pconfig['ealgos'][] = $algo_data['name'];
896
		if (isset($algo_data['keylen']))
897
			$pconfig["keylen_".$algo_data['name']] = $algo_data['keylen'];
898
	}
899

    
900
	return $ealgos;
901
}
902

    
903
function pconfig_to_idinfo($prefix,& $pconfig) {
904

    
905
	$type = $pconfig[$prefix."id_type"];
906
	$address = $pconfig[$prefix."id_address"];
907
	$netbits = $pconfig[$prefix."id_netbits"];
908

    
909
	switch( $type )
910
	{
911
		case "address":
912
			return array('type' => $type, 'address' => $address);
913
		case "network":
914
			return array('type' => $type, 'address' => $address, 'netbits' => $netbits);
915
		default:
916
			return array('type' => $type );
917
	}
918
}
919

    
920
function idinfo_to_pconfig($prefix,& $idinfo,& $pconfig) {
921

    
922
	switch( $idinfo['type'] )
923
	{
924
		case "address":
925
			$pconfig[$prefix."id_type"] = $idinfo['type'];
926
			$pconfig[$prefix."id_address"] = $idinfo['address'];
927
			break;
928
		case "network":
929
			$pconfig[$prefix."id_type"] = $idinfo['type'];
930
			$pconfig[$prefix."id_address"] = $idinfo['address'];
931
			$pconfig[$prefix."id_netbits"] = $idinfo['netbits'];
932
			break;
933
		default:
934
			$pconfig[$prefix."id_type"] = $idinfo['type'];
935
			break;
936
	}
937
}
938

    
939
?>
(242-242/256)