Project

General

Profile

Download (29.5 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
	
62
if (!empty($_POST['uniqid']))
63
	$uindex = $_POST['uniqid'];
64

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

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

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

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

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

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

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

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

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

    
129
if ($_POST) {
130

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

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

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

    
146
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
147

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

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

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

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

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

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

    
321
	/* For ESP protocol, handle encryption algorithms */
322
	if ( $pconfig['proto'] == "esp") {
323
		$ealgos = pconfig_to_ealgos($pconfig);
324

    
325
		if (!count($ealgos)) {
326
			$input_errors[] = gettext("At least one encryption algorithm must be selected.");
327
		} else {
328
			foreach ($ealgos as $ealgo) {
329
				if (isset($config['system']['crypto_hardware'])) {
330
					if ($config['system']['crypto_hardware'] == "glxsb") {
331
						if ($ealgo['name'] == "aes" && $ealgo['keylen'] != "128")
332
						$input_errors[] = gettext("Only 128 bit AES can be used where the glxsb crypto accelerator is enabled.");
333
					}
334
				}
335
				if (empty($pconfig['halgos'])) {
336
					if (!strpos($ealgo['name'], "gcm")) {
337
						$input_errors[] = gettext("At least one hashing algorithm needs to be selected.");
338
						break;
339
					}
340
				}
341
			}
342
		}
343

    
344
	}
345
	if (($_POST['lifetime'] && !is_numeric($_POST['lifetime']))) {
346
		$input_errors[] = gettext("The P2 lifetime must be an integer.");
347
	}
348

    
349
	if (!$input_errors) {
350

    
351
		$ph2ent = array();
352
		$ph2ent['ikeid'] = $pconfig['ikeid'];
353
		$ph2ent['uniqid'] = $pconfig['uniqid'];
354
		$ph2ent['mode'] = $pconfig['mode'];
355
		$ph2ent['disabled'] = $pconfig['disabled'] ? true : false;
356
		if (!isset($pconfig['reqid']))
357
			$ph2ent['reqid'] = ipsec_new_reqid();
358
		else
359
			$ph2ent['reqid'] = $pconfig['reqid'];
360

    
361
		if(($ph2ent['mode'] == "tunnel") || ($ph2ent['mode'] == "tunnel6")){
362
			if (!empty($pconfig['natlocalid_address']))
363
				$ph2ent['natlocalid'] = pconfig_to_idinfo("natlocal",$pconfig);
364
				
365
			$ph2ent['localid'] = pconfig_to_idinfo("local",$pconfig);
366
			$ph2ent['remoteid'] = pconfig_to_idinfo("remote",$pconfig);
367
		}
368

    
369
		$ph2ent['protocol'] = $pconfig['proto'];
370
		$ph2ent['encryption-algorithm-option'] = $ealgos;
371
		
372
		if (!empty($pconfig['halgos']))
373
			$ph2ent['hash-algorithm-option'] = $pconfig['halgos'];
374
		else
375
			unset($ph2ent['hash-algorithm-option']);
376
			
377
		$ph2ent['pfsgroup'] = $pconfig['pfsgroup'];
378
		$ph2ent['lifetime'] = $pconfig['lifetime'];
379
		$ph2ent['pinghost'] = $pconfig['pinghost'];
380
		$ph2ent['descr'] = $pconfig['descr'];
381

    
382
		if (isset($pconfig['mobile']))
383
			$ph2ent['mobile'] = true;
384

    
385
		if ($ph2found === true && $a_phase2[$p2index])
386
			$a_phase2[$p2index] = $ph2ent;
387
		else
388
			$a_phase2[] = $ph2ent;
389

    
390

    
391
		write_config();
392
		mark_subsystem_dirty('ipsec');
393

    
394
		header("Location: vpn_ipsec.php");
395
		exit;
396
	}
397
}
398

    
399
if ($pconfig['mobile'])
400
	$pgtitle = array(gettext("VPN"),gettext("IPsec"),gettext("Edit Phase 2"), gettext("Mobile Client"));
401
else
402
	$pgtitle = array(gettext("VPN"),gettext("IPsec"),gettext("Edit Phase 2"));
403
	
404
$shortcut_section = "ipsec";
405

    
406
include("head.inc");
407

    
408
function pconfig_to_ealgos(& $pconfig) {
409
	global $p2_ealgos;
410

    
411
	$ealgos = array();
412
	if (is_array($pconfig['ealgos'])) {
413
		foreach ($p2_ealgos as $algo_name => $algo_data) {
414
			if (in_array($algo_name,$pconfig['ealgos'])) {
415
				$ealg = array();
416
				$ealg['name'] = $algo_name;
417
				
418
				if (is_array($algo_data['keysel']))
419
					$ealg['keylen'] = $_POST["keylen_".$algo_name];
420
					
421
				$ealgos[] = $ealg;
422
			}
423
		}
424
	}
425

    
426
	return $ealgos;
427
}
428

    
429
function ealgos_to_pconfig(& $ealgos,& $pconfig) {
430

    
431
	$pconfig['ealgos'] = array();
432
	foreach ($ealgos as $algo_data) {
433
		$pconfig['ealgos'][] = $algo_data['name'];
434
		
435
		if (isset($algo_data['keylen']))
436
			$pconfig["keylen_".$algo_data['name']] = $algo_data['keylen'];
437
	}
438

    
439
	return $ealgos;
440
}
441

    
442
function pconfig_to_idinfo($prefix,& $pconfig) {
443

    
444
	$type = $pconfig[$prefix."id_type"];
445
	$address = $pconfig[$prefix."id_address"];
446
	$netbits = $pconfig[$prefix."id_netbits"];
447

    
448
	switch( $type )
449
	{
450
		case "address":
451
			return array('type' => $type, 'address' => $address);
452
		case "network":
453
			return array('type' => $type, 'address' => $address, 'netbits' => $netbits);
454
		default:
455
			return array('type' => $type );
456
	}
457
}
458

    
459
function idinfo_to_pconfig($prefix,& $idinfo,& $pconfig) {
460

    
461
	switch( $idinfo['type'] )
462
	{
463
		case "address":
464
			$pconfig[$prefix."id_type"] = $idinfo['type'];
465
			$pconfig[$prefix."id_address"] = $idinfo['address'];
466
			break;
467
		case "network":
468
			$pconfig[$prefix."id_type"] = $idinfo['type'];
469
			$pconfig[$prefix."id_address"] = $idinfo['address'];
470
			$pconfig[$prefix."id_netbits"] = $idinfo['netbits'];
471
			break;
472
		default:
473
			$pconfig[$prefix."id_type"] = $idinfo['type'];
474
			break;
475
	}
476
}
477

    
478
if ($input_errors)
479
	print_input_errors($input_errors);
480

    
481
$tab_array = array();
482
$tab_array[0] = array(gettext("Tunnels"), true, "vpn_ipsec.php");
483
$tab_array[1] = array(gettext("Mobile clients"), false, "vpn_ipsec_mobile.php");
484
$tab_array[2] = array(gettext("Pre-Shared Keys"), false, "vpn_ipsec_keys.php");
485
$tab_array[3] = array(gettext("Advanced Settings"), false, "vpn_ipsec_settings.php");
486
display_top_tabs($tab_array);
487

    
488
require('classes/Form.class.php');
489

    
490
$form = new Form();
491

    
492
$section = new Form_Section('General Information');
493

    
494
$section->addInput(new Form_Checkbox(
495
	'disabled',
496
	'Disabled',
497
	'Disable this phase 2 entry without removing it from the list. ',
498
	$pconfig['disabled']
499
));
500

    
501
$section->addInput(new Form_Select(
502
	'mode',
503
	'Mode',
504
	$pconfig['mode'],
505
	$p2_modes
506
));
507

    
508
$group = new Form_Group('Local Network');
509
$group->addClass('opt_localid');
510

    
511
$subnetarray = get_configured_interface_with_descr();
512
foreach($subnetarray as $ifname => $ifdescr)
513
	$subnetarray[$ifname] = $ifdescr . ' subnet';
514

    
515
$group->add(new Form_Select(
516
	'localid_type',
517
	null,
518
	$pconfig['localid_type'],
519
	array_merge(array('address' => 'Address', 'network' => 'Network'), $subnetarray)
520
))->setHelp('Type');
521

    
522
$group->add(new Form_IpAddress(
523
	'localid_address',
524
	null,
525
	$pconfig['localid_address']
526
))->setHelp('Address')->addMask(localid_netbits, $pconfig['localid_netbits']);
527

    
528
$section->add($group);
529

    
530
$group = new Form_Group('NAT/BINAT translation');
531
$group->addClass('opt_natid');
532

    
533
$subnetarray = get_configured_interface_with_descr();
534
foreach($subnetarray as $ifname => $ifdescr)
535
	$subnetarray[$ifname] = $ifdescr . ' subnet';
536

    
537
$group->add(new Form_Select(
538
	'natlocalid_type',
539
	null,
540
	$pconfig['natlocalid_type'],
541
	array_merge(array('address' => 'Address', 'network' => 'Network'), array_merge($subnetarray, array('none' => gettext('None'))))
542
))->setHelp('Type');
543

    
544
$group->add(new Form_IpAddress(
545
	'natlocalid_address',
546
	null,
547
	$pconfig['localid_address']
548
))->setHelp('Address')->addMask(natlocalid_netbits, $pconfig['natlocalid_netbits']);
549

    
550
$group->setHelp('If NAT/BINAT is required on this network specify the address to be translated');
551
$section->add($group);
552

    
553
$group = new Form_Group('Remote Network');
554
$group->addClass('opt_remoteid');
555

    
556
$group->add(new Form_Select(
557
	'remoteid_type',
558
	null,
559
	$pconfig['remoteid_type'],
560
	array('address' => 'Address', 'network' => 'Network')
561
))->setHelp('Type');
562

    
563
$group->add(new Form_IpAddress(
564
	'remoteid_address',
565
	null,
566
	$pconfig['remoteid_address']
567
))->setHelp('Address')->addMask(remoteid_netbits, $pconfig['remoteid_netbits']);
568

    
569
$section->add($group);
570

    
571
$section->addInput(new Form_Input(
572
	'descr',
573
	'Description',
574
	'text',
575
	$pconfig['descr']
576
))->setHelp('You may enter a description here for your reference (not parsed).');
577

    
578
$form->add($section);
579

    
580
$section = new Form_Section('Phase 2 proposal (SA/Key Exchange)');
581

    
582
$section->addInput(new Form_Select(
583
	'proto',
584
	'Protocol',
585
	$pconfig['proto'],
586
	$p2_protos
587
))->setHelp('ESP is encryption, AH is authentication only.');
588

    
589
$i = 0;
590
$rows = count($p2_ealgos) - 1;
591

    
592
foreach ($p2_ealgos as $algo => $algodata) {
593
	$group = new Form_Group($i == 0 ? 'Encryption Algorithms':'');
594
	$group->addClass('encalg');
595

    
596
	$group->add(new Form_Checkbox(
597
		'ealgos',
598
		null,
599
		$algodata['name'],
600
		(is_array($pconfig['ealgos']) && in_array($algo,$pconfig['ealgos']))
601
	));
602

    
603

    
604

    
605
	if(is_array($algodata['keysel'])) {
606
		$list = array();
607
		$key_hi = $algodata['keysel']['hi'];
608
		$key_lo = $algodata['keysel']['lo'];
609
		$key_step = $algodata['keysel']['step'];
610
		for ($keylen = $key_hi; $keylen >= $key_lo; $keylen -= $key_step) {
611
			$list[$keylen] = $keylen . ' bits';
612
		}
613

    
614
		$group->add(new Form_Select(
615
			'keylen_' . $algo,
616
			null,
617
			$keylen == $pconfig["keylen_".$algo],
618
			array_merge(array('auto' => 'Auto'), $list)
619
		));
620
	}
621

    
622

    
623
	if($i == $rows)
624
		$group->setHelp('Use 3DES for best compatibility or if you have a hardware crypto accelerator card. Blowfish is usually the fastest in software encryption.');
625

    
626
	$i++;
627
	$section->add($group);
628
}
629

    
630
$group = new Form_Group('Hash Algorithms');
631

    
632
foreach ($p2_halgos as $algo => $algoname) {
633
	$group->add(new Form_Checkbox(
634
		$algo,
635
		null,
636
		$algoname,
637
		(in_array($algo, $pconfig['halgos']))
638
	));
639
}
640

    
641
$section->add($group);
642

    
643
$sm = (!isset($pconfig['mobile']) || !isset($a_client['pfs_group']));
644

    
645
$section->addInput(new Form_Select(
646
	'pfsgroup',
647
	'PFS key group',
648
	$pconfig['psgroup'],
649
	$sm ? $p2_pfskeygroups:array()
650
))->setHelp($sm ? '':'Set globally in mobile client options');
651

    
652
$section->addInput(new Form_Input(
653
	'lifetime',
654
	'Lifetime',
655
	'number',
656
	$pconfig['lifetime']
657
))->setHelp('Seconds');
658

    
659
$form->add($section);
660

    
661
$section = new Form_Section('Advanced Configuration');
662

    
663
$section->addInput(new Form_IpAddress(
664
	'pinghost',
665
	'Automatically ping host',
666
	$pconfig['pinghost']
667
))->setHelp('IP Address');
668

    
669
// Hidden inputs
670
if ($pconfig['mobile']) {
671
	$section->addInput(new Form_Input(
672
		'mobile',
673
		null,
674
		'hidden',
675
		'true'
676
	));
677
}
678

    
679
$section->addInput(new Form_Input(
680
	'ikeid',
681
	null,
682
	'hidden',
683
	$pconfig['ikeid']
684
));
685

    
686
if (!empty($pconfig['reqid'])) {
687
	$section->addInput(new Form_Input(
688
		'reqid',
689
		null,
690
		'hidden',
691
		$pconfig['reqid']
692
	));
693
}
694

    
695
$section->addInput(new Form_Input(
696
	'uniqid',
697
	null,
698
	'hidden',
699
	$pconfig['uniqid']
700
));
701

    
702
$form->add($section);
703

    
704
print($form);
705

    
706
?>
707

    
708
<script type="text/javascript">
709
//<![CDATA[
710
events.push(function(){
711

    
712
    // ---------- On changing "Mode" ----------------------------------------------------------------------------------
713
	function change_mode() {
714

    
715
		value = $('#mode').val();
716

    
717
		if ((value == 'tunnel') || (value == 'tunnel6')) {
718
			hideClass('opt_localid', false);
719
			hideClass('opt_natid', false);
720

    
721
<?php	if (!isset($pconfig['mobile'])): ?>
722
			hideClass('opt_remoteid', false);
723
			hideClass('opt_natid', false);
724
<?php	endif; ?>
725
		} else {
726
			hideClass('opt_localid', true);
727
			hideClass('opt_natid', true);
728
<?php	if (!isset($pconfig['mobile'])): ?>
729
			hideClass('opt_remoteid', true);
730
<?php	endif; ?>
731
		}
732
	}
733

    
734
    // ---------- On changing "NAT/BINAT" -----------------------------------------------------------------------------
735
	function typesel_change_natlocal(bits) {
736
		var value = $('#mode').val();
737

    
738
		if (typeof(bits) === "undefined") {
739
			if (value === "tunnel") {
740
				bits = 24;
741
			}
742
			else if (value === "tunnel6") {
743
				bits = 64;
744
			}
745
		}
746

    
747
		var address_is_blank = !/\S/.test($('#natlocalid_address').val());
748

    
749
		switch ($("#natlocalid_type option:selected").index()) {
750
			case 0: /* single */
751
				disableInput('natlocalid_address', false);
752

    
753
				if (address_is_blank) {
754
					$('#natlocalid_netbits').val(0);
755
				}
756

    
757
				disableInput('natlocalid_netbits', true);
758
				break;
759
			case 1: /* network */
760
				disableInput('natlocalid_address', false);
761
				
762
				if (address_is_blank) {
763
					$('#natlocalid_netbits').val(bits);
764
				}
765
				
766
				disableInput('natlocalid_netbits', false);
767
				break;
768
			case 3: /* none */
769
				disableInput('natlocalid_address', true);
770
				disableInput('natlocalid_netbits', true);
771
				break;
772
			default:
773
				$('#natlocalid_address').val("");
774
				disableInput('natlocalid_address', true);
775
				
776
				if (address_is_blank) {
777
					$('#natlocalid_netbits').val(0);
778
				}
779
				
780
				disableInput('natlocalid_netbits', true);
781
				break;
782
		}
783
	}
784

    
785
    // ---------- On changing "Local Network" -------------------------------------------------------------------------
786
	function typesel_change_local(bits) {
787
		var value = $('#mode').val();
788

    
789
		if (typeof(bits) === "undefined") {
790
			if (value === "tunnel") {
791
				bits = 24;
792
			}
793
			else if (value === "tunnel6") {
794
				bits = 64;
795
			}
796
		}
797

    
798
		var address_is_blank = !/\S/.test($('#localid_address').val());
799

    
800
		switch ($("#localid_type option:selected").index()) {
801
			case 0: /* single */
802
				disableInput('localid_address', false);
803

    
804
				if (address_is_blank) {
805
					$('#localid_netbits').val(0);
806
				}
807

    
808
				disableInput('localid_netbits', true);
809
				break;
810
			case 1: /* network */
811
				disableInput('localid_address', false);
812
				
813
				if (address_is_blank) {
814
					$('#localid_netbits').val(bits);
815
				}
816
				
817
				disableInput('localid_netbits', false);
818
				break;
819
			case 3: /* none */
820
				disableInput('localid_address', true);
821
				disableInput('localid_netbits', true);
822
				break;
823
			default:
824
				$('#localid_address').val("");
825
				disableInput('localid_address', true);
826
				
827
				if (address_is_blank) {
828
					$('#localid_netbits').val(0);
829
				}
830
				
831
				disableInput('localid_netbits', true);
832
				break;
833
		}
834
	}
835

    
836
<?php
837

    
838
    // ---------- On changing "Remote Network" ------------------------------------------------------------------------
839
	if (!isset($pconfig['mobile'])): ?>
840

    
841
		function typesel_change_remote(bits) {
842

    
843
			var value = $('#mode').val();
844

    
845
			if (typeof(bits) === "undefined") {
846
				if (value === "tunnel") {
847
					bits = 24;
848
				}
849
				else if (value === "tunnel6") {
850
					bits = 64;
851
				}
852
			}
853

    
854
			var address_is_blank = !/\S/.test($('#remoteid_address').val());
855

    
856
			switch ($("#remoteid_type option:selected").index()) {
857
				case 0: /* single */
858
					disableInput('remoteid_address', false);
859

    
860
					if (address_is_blank) {
861
						$('#remoteid_netbits').val(0);
862
					}
863

    
864
					disableInput('remoteid_netbits', true);
865
					break;
866
				case 1: /* network */
867
					disableInput('remoteid_address', false);
868
					
869
					if (address_is_blank) {
870
						$('#remoteid_netbits').val(bits);
871
					}
872
					
873
					disableInput('remoteid_netbits', false);
874
					break;
875
				case 3: /* none */
876
					disableInput('remoteid_address', true);
877
					disableInput('remoteid_netbits', true);
878
					break;
879
				default:
880
					$('#remoteid_address').val("");
881
					disableInput('remoteid_address', true);
882
					
883
					if (address_is_blank) {
884
						$('#remoteid_netbits').val(0);
885
					}
886
					
887
					disableInput('remoteid_netbits', true);
888
					break;
889
			}
890
		}
891

    
892
	<?php endif; ?>
893

    
894
	function change_protocol() {
895
			hideClass('encalg', ($('#proto').val() != 'esp'));
896
	}
897

    
898
	// ---------- Library of show/hide functions ----------------------------------------------------------------------
899

    
900
	// Hides the <div> in which the specified input element lives so that the input,
901
	// its label and help text are hidden
902
	function hideInput(id, hide) {
903
		if(hide)
904
			$('#' + id).parent().parent('div').addClass('hidden');
905
		else
906
			$('#' + id).parent().parent('div').removeClass('hidden');
907
	}
908

    
909
	// Hides the <div> in which the specified group input element lives so that the input,
910
	// its label and help text are hidden
911
	function hideGroupInput(id, hide) {
912
		if(hide)
913
			$('#' + id).parent('div').addClass('hidden');
914
		else
915
			$('#' + id).parent('div').removeClass('hidden');
916
	}
917

    
918
	// Hides the <div> in which the specified checkbox lives so that the checkbox,
919
	// its label and help text are hidden
920
	function hideCheckbox(id, hide) {
921
		if(hide)
922
			$('#' + id).parent().parent().parent('div').addClass('hidden');
923
		else
924
			$('#' + id).parent().parent().parent('div').removeClass('hidden');
925
	}
926

    
927
	// Disables the specified input element
928
	function disableInput(id, disable) {
929
		$('#' + id).prop("disabled", disable);
930
	}
931

    
932
	// Hides all elements of the specified class. This will usually be a section or group
933
	function hideClass(s_class, hide) {
934
		if(hide)
935
			$('.' + s_class).hide();
936
		else
937
			$('.' + s_class).show();
938
	}
939

    
940
	// ---------- Monitor elements for change and call the appropriate display functions ------------------------------
941

    
942
	 // Protocol
943
	$('#proto').click(function () {
944
		change_protocol();
945
	});
946

    
947
	 // Localid
948
	$('#localid_type').click(function () {
949
		typesel_change_local(<?=htmlspecialchars($pconfig['localid_netbits'])?>);
950
	});
951

    
952
	 // Remoteid
953
	$('#remoteid_type').click(function () {
954
		typesel_change_remote(<?=htmlspecialchars($pconfig['remoteid_netbits'])?>);
955
	});
956

    
957
	 // NATLocalid
958
	$('#natlocalid_type').click(function () {
959
		typesel_change_natlocal(<?=htmlspecialchars($pconfig['natlocalid_netbits'])?>);
960
	});
961

    
962
	 // Mode
963
	$('#mode').click(function () {
964
		change_mode();
965
	});
966

    
967
    // ---------- Iniatial page load ----------------------------------------------------------------------------------    
968
	change_mode();
969
	change_protocol();
970
	typesel_change_local(<?=htmlspecialchars($pconfig['localid_netbits'])?>);
971
	typesel_change_natlocal(<?=htmlspecialchars($pconfig['natlocalid_netbits'])?>);
972
<?php
973
	if (!isset($pconfig['mobile'])):
974
?>
975
		typesel_change_remote(<?=htmlspecialchars($pconfig['remoteid_netbits'])?>);
976
<?php
977
endif;
978
?>
979
});
980
//]]>
981
</script>
982
<?php
983
include("foot.inc");
(223-223/237)