Project

General

Profile

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

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

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

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

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

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

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

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

    
47
if (!is_array($config['ipsec']['phase1']))
48
	$config['ipsec']['phase1'] = array();
49

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

    
53
$a_phase1 = &$config['ipsec']['phase1'];
54
$a_phase2 = &$config['ipsec']['phase2'];
55

    
56
if (is_numericint($_GET['p1index']))
57
	$p1index = $_GET['p1index'];
58
if (isset($_POST['p1index']) && is_numericint($_POST['p1index']))
59
	$p1index = $_POST['p1index'];
60

    
61
if (isset($_GET['dup']) && is_numericint($_GET['dup']))
62
	$p1index = $_GET['dup'];
63

    
64
if (isset($p1index) && $a_phase1[$p1index]) {
65
	// don't copy the ikeid on dup
66
	if (!isset($_GET['dup']) || !is_numericint($_GET['dup']))
67
		$pconfig['ikeid'] = $a_phase1[$p1index]['ikeid'];
68

    
69
	$old_ph1ent = $a_phase1[$p1index];
70

    
71
	$pconfig['disabled'] = isset($a_phase1[$p1index]['disabled']);
72

    
73
	if ($a_phase1[$p1index]['interface'])
74
		$pconfig['interface'] = $a_phase1[$p1index]['interface'];
75
	else
76
		$pconfig['interface'] = "wan";
77

    
78
	list($pconfig['remotenet'],$pconfig['remotebits']) = explode("/", $a_phase1[$p1index]['remote-subnet']);
79

    
80
	if (isset($a_phase1[$p1index]['mobile']))
81
		$pconfig['mobile'] = 'true';
82
	else
83
		$pconfig['remotegw'] = $a_phase1[$p1index]['remote-gateway'];
84

    
85
	if (empty($a_phase1[$p1index]['iketype']))
86
		$pconfig['iketype'] = "ikev1";
87
	else
88
		$pconfig['iketype'] = $a_phase1[$p1index]['iketype'];
89
	$pconfig['mode'] = $a_phase1[$p1index]['mode'];
90
	$pconfig['protocol'] = $a_phase1[$p1index]['protocol'];
91
	$pconfig['myid_type'] = $a_phase1[$p1index]['myid_type'];
92
	$pconfig['myid_data'] = $a_phase1[$p1index]['myid_data'];
93
	$pconfig['peerid_type'] = $a_phase1[$p1index]['peerid_type'];
94
	$pconfig['peerid_data'] = $a_phase1[$p1index]['peerid_data'];
95
	$pconfig['ealgo'] = $a_phase1[$p1index]['encryption-algorithm'];
96
	$pconfig['halgo'] = $a_phase1[$p1index]['hash-algorithm'];
97
	$pconfig['dhgroup'] = $a_phase1[$p1index]['dhgroup'];
98
	$pconfig['lifetime'] = $a_phase1[$p1index]['lifetime'];
99
	$pconfig['authentication_method'] = $a_phase1[$p1index]['authentication_method'];
100

    
101
	if (($pconfig['authentication_method'] == "pre_shared_key") ||
102
		($pconfig['authentication_method'] == "xauth_psk_server")) {
103
		$pconfig['pskey'] = $a_phase1[$p1index]['pre-shared-key'];
104
	} else {
105
		$pconfig['certref'] = $a_phase1[$p1index]['certref'];
106
		$pconfig['caref'] = $a_phase1[$p1index]['caref'];
107
	}
108

    
109
	$pconfig['descr'] = $a_phase1[$p1index]['descr'];
110
	$pconfig['nat_traversal'] = $a_phase1[$p1index]['nat_traversal'];
111
		$pconfig['mobike'] = $a_phase1[$p1index]['mobike'];
112

    
113
	if (isset($a_phase1[$p1index]['reauth_enable']))
114
		$pconfig['reauth_enable'] = true;
115
	if (isset($a_phase1[$p1index]['rekey_enable']))
116
		$pconfig['rekey_enable'] = true;
117
	if (isset($a_phase1[$p1index]['responderonly']))
118
		$pconfig['responderonly'] = true;
119

    
120
	if ($a_phase1[$p1index]['dpd_delay'] && $a_phase1[$p1index]['dpd_maxfail']) {
121
		$pconfig['dpd_enable'] = true;
122
		$pconfig['dpd_delay'] = $a_phase1[$p1index]['dpd_delay'];
123
		$pconfig['dpd_maxfail'] = $a_phase1[$p1index]['dpd_maxfail'];
124
	}
125
} else {
126
	/* defaults */
127
	$pconfig['interface'] = "wan";
128
	if($config['interfaces']['lan'])
129
		$pconfig['localnet'] = "lan";
130
	$pconfig['mode'] = "main";
131
	$pconfig['protocol'] = "inet";
132
	$pconfig['myid_type'] = "myaddress";
133
	$pconfig['peerid_type'] = "peeraddress";
134
	$pconfig['authentication_method'] = "pre_shared_key";
135
	$pconfig['ealgo'] = array( name => "aes" );
136
	$pconfig['halgo'] = "sha1";
137
	$pconfig['dhgroup'] = "2";
138
	$pconfig['lifetime'] = "28800";
139
	$pconfig['nat_traversal'] = 'on';
140
		$pconfig['mobike'] = 'off';
141
	$pconfig['dpd_enable'] = true;
142
	$pconfig['iketype'] = "ikev1";
143

    
144
	/* mobile client */
145
	if($_GET['mobile']) {
146
		$pconfig['mobile']=true;
147
				$pconfig['mode'] = "aggressive";
148
	}
149
}
150

    
151
if (isset($_GET['dup']) && is_numericint($_GET['dup']))
152
	unset($p1index);
153

    
154
if ($_POST) {
155
	unset($input_errors);
156
	$pconfig = $_POST;
157

    
158
	/* input validation */
159

    
160
	$method = $pconfig['authentication_method'];
161
	// Unset ca and cert if not required to avaoid storing in config
162
	if ($method == "pre_shared_key" || $method == "xauth_psk_server"){
163
		unset($pconfig['caref']);
164
		unset($pconfig['certref']);
165
	}
166

    
167
	// Only require PSK here for normal PSK tunnels (not mobile) or xauth.
168
	// For RSA methods, require the CA/Cert.
169
	switch ($method) {
170
		case 'eap-mschapv2':
171
			if ($pconfig['iketype'] != 'ikev2')
172
				$input_errors[] = gettext("EAP-MSChapv2 can only be used with IKEv2 type VPNs.");
173
			break;
174
		case "eap-tls":
175
			if ($pconfig['iketype'] != 'ikev2')
176
				$input_errors[] = gettext("EAP-TLS can only be used with IKEv2 type VPNs.");
177
			break;
178
		case "pre_shared_key":
179
			// If this is a mobile PSK tunnel the user PSKs go on
180
			//	the PSK tab, not here, so skip the check.
181
			if ($pconfig['mobile'])
182
				break;
183
		case "xauth_psk_server":
184
			$reqdfields = explode(" ", "pskey");
185
			$reqdfieldsn = array(gettext("Pre-Shared Key"));
186
			$validate_pskey = true;
187
			break;
188
		case "hybrid_rsa_server":
189
		case "xauth_rsa_server":
190
		case "rsasig":
191
			$reqdfields = explode(" ", "caref certref");
192
			$reqdfieldsn = array(gettext("Certificate Authority"),gettext("Certificate"));
193
			break;
194
	}
195
	if (!$pconfig['mobile']) {
196
		$reqdfields[] = "remotegw";
197
		$reqdfieldsn[] = gettext("Remote gateway");
198
	}
199

    
200
	do_input_validation($pconfig, $reqdfields, $reqdfieldsn, $input_errors);
201

    
202
	if (isset($validate_pskey) && isset($pconfig['pskey']) && !preg_match('/^[[:ascii:]]*$/', $pconfig['pskey'])) {
203
		unset($validate_pskey);
204
		$input_errors[] = gettext("Pre-Shared Key contains invalid characters.");
205
	}
206

    
207
	if (($pconfig['lifetime'] && !is_numeric($pconfig['lifetime'])))
208
		$input_errors[] = gettext("The P1 lifetime must be an integer.");
209

    
210
	if ($pconfig['remotegw']) {
211
		if (!is_ipaddr($pconfig['remotegw']) && !is_domain($pconfig['remotegw']))
212
			$input_errors[] = gettext("A valid remote gateway address or host name must be specified.");
213
		elseif (is_ipaddrv4($pconfig['remotegw']) && ($pconfig['protocol'] != "inet"))
214
			$input_errors[] = gettext("A valid remote gateway IPv4 address must be specified or you need to change protocol to IPv6");
215
		elseif (is_ipaddrv6($pconfig['remotegw']) && ($pconfig['protocol'] != "inet6"))
216
			$input_errors[] = gettext("A valid remote gateway IPv6 address must be specified or you need to change protocol to IPv4");
217
	}
218

    
219
	if (($pconfig['remotegw'] && is_ipaddr($pconfig['remotegw']) && !isset($pconfig['disabled']) )) {
220
		$t = 0;
221
		foreach ($a_phase1 as $ph1tmp) {
222
			if ($p1index != $t) {
223
				$tremotegw = $pconfig['remotegw'];
224
				if (($ph1tmp['remote-gateway'] == $tremotegw) && !isset($ph1tmp['disabled'])) {
225
					$input_errors[] = sprintf(gettext('The remote gateway "%1$s" is already used by phase1 "%2$s".'), $tremotegw, $ph1tmp['descr']);
226
				}
227
			}
228
			$t++;
229
		}
230
	}
231

    
232
	if (is_array($a_phase2) && (count($a_phase2))) {
233
		foreach ($a_phase2 as $phase2) {
234
			if($phase2['ikeid'] == $pconfig['ikeid']) {
235
				if (($pconfig['protocol'] == "inet") && ($phase2['mode'] == "tunnel6")) {
236
					$input_errors[] = gettext("There is a Phase 2 using IPv6, you cannot use IPv4.");
237
					break;
238
				}
239
				if (($pconfig['protocol'] == "inet6") && ($phase2['mode'] == "tunnel")) {
240
					$input_errors[] = gettext("There is a Phase 2 using IPv4, you cannot use IPv6.");
241
					break;
242
				}
243
			}
244
		}
245
	}
246

    
247
	/* My identity */
248

    
249
	if ($pconfig['myid_type'] == "myaddress")
250
		$pconfig['myid_data'] = "";
251

    
252
	if ($pconfig['myid_type'] == "address" and $pconfig['myid_data'] == "")
253
		$input_errors[] = gettext("Please enter an address for 'My Identifier'");
254

    
255
	if ($pconfig['myid_type'] == "keyid tag" and $pconfig['myid_data'] == "")
256
		$input_errors[] = gettext("Please enter a keyid tag for 'My Identifier'");
257

    
258
	if ($pconfig['myid_type'] == "fqdn" and $pconfig['myid_data'] == "")
259
		$input_errors[] = gettext("Please enter a fully qualified domain name for 'My Identifier'");
260

    
261
	if ($pconfig['myid_type'] == "user_fqdn" and $pconfig['myid_data'] == "")
262
		$input_errors[] = gettext("Please enter a user and fully qualified domain name for 'My Identifier'");
263

    
264
	if ($pconfig['myid_type'] == "dyn_dns" and $pconfig['myid_data'] == "")
265
		$input_errors[] = gettext("Please enter a dynamic domain name for 'My Identifier'");
266

    
267
	if ((($pconfig['myid_type'] == "address") && !is_ipaddr($pconfig['myid_data'])))
268
		$input_errors[] = gettext("A valid IP address for 'My identifier' must be specified.");
269

    
270
	if ((($pconfig['myid_type'] == "fqdn") && !is_domain($pconfig['myid_data'])))
271
		$input_errors[] = gettext("A valid domain name for 'My identifier' must be specified.");
272

    
273
	if ($pconfig['myid_type'] == "fqdn")
274
		if (is_domain($pconfig['myid_data']) == false)
275
			$input_errors[] = gettext("A valid FQDN for 'My identifier' must be specified.");
276

    
277
	if ($pconfig['myid_type'] == "user_fqdn") {
278
		$user_fqdn = explode("@",$pconfig['myid_data']);
279
		if (is_domain($user_fqdn[1]) == false)
280
			$input_errors[] = gettext("A valid User FQDN in the form of user@my.domain.com for 'My identifier' must be specified.");
281
	}
282

    
283
	if ($pconfig['myid_type'] == "dyn_dns")
284
		if (is_domain($pconfig['myid_data']) == false)
285
			$input_errors[] = gettext("A valid Dynamic DNS address for 'My identifier' must be specified.");
286

    
287
	/* Peer identity */
288

    
289
	if ($pconfig['myid_type'] == "peeraddress")
290
		$pconfig['peerid_data'] = "";
291

    
292
	// Only enforce peer ID if we are not dealing with a pure-psk mobile config.
293
	if (!(($pconfig['authentication_method'] == "pre_shared_key") && ($pconfig['mobile']))) {
294
		if ($pconfig['peerid_type'] == "address" and $pconfig['peerid_data'] == "")
295
			$input_errors[] = gettext("Please enter an address for 'Peer Identifier'");
296

    
297
		if ($pconfig['peerid_type'] == "keyid tag" and $pconfig['peerid_data'] == "")
298
			$input_errors[] = gettext("Please enter a keyid tag for 'Peer Identifier'");
299

    
300
		if ($pconfig['peerid_type'] == "fqdn" and $pconfig['peerid_data'] == "")
301
			$input_errors[] = gettext("Please enter a fully qualified domain name for 'Peer Identifier'");
302

    
303
		if ($pconfig['peerid_type'] == "user_fqdn" and $pconfig['peerid_data'] == "")
304
			$input_errors[] = gettext("Please enter a user and fully qualified domain name for 'Peer Identifier'");
305

    
306
		if ((($pconfig['peerid_type'] == "address") && !is_ipaddr($pconfig['peerid_data'])))
307
			$input_errors[] = gettext("A valid IP address for 'Peer identifier' must be specified.");
308

    
309
		if ((($pconfig['peerid_type'] == "fqdn") && !is_domain($pconfig['peerid_data'])))
310
			$input_errors[] = gettext("A valid domain name for 'Peer identifier' must be specified.");
311

    
312
		if ($pconfig['peerid_type'] == "fqdn")
313
			if (is_domain($pconfig['peerid_data']) == false)
314
				$input_errors[] = gettext("A valid FQDN for 'Peer identifier' must be specified.");
315

    
316
		if ($pconfig['peerid_type'] == "user_fqdn") {
317
			$user_fqdn = explode("@",$pconfig['peerid_data']);
318
			if (is_domain($user_fqdn[1]) == false)
319
				$input_errors[] = gettext("A valid User FQDN in the form of user@my.domain.com for 'Peer identifier' must be specified.");
320
		}
321
	}
322

    
323
	if ($pconfig['dpd_enable']) {
324
		if (!is_numeric($pconfig['dpd_delay']))
325
			$input_errors[] = gettext("A numeric value must be specified for DPD delay.");
326

    
327
		if (!is_numeric($pconfig['dpd_maxfail']))
328
			$input_errors[] = gettext("A numeric value must be specified for DPD retries.");
329
	}
330

    
331
	if (!empty($pconfig['iketype']) && $pconfig['iketype'] != "ikev1" && $pconfig['iketype'] != "ikev2" && $pconfig['iketype'] != "auto")
332
		$input_errors[] = gettext("Valid arguments for IKE type is v1 or v2 or auto");
333

    
334
		if (!empty($_POST['ealgo']) && isset($config['system']['crypto_hardware'])) {
335
			if ($config['system']['crypto_hardware'] == "glxsb") {
336
				if ($_POST['ealgo'] == "aes" && $_POST['ealgo_keylen'] != "128")
337
					$input_errors[] = gettext("Only 128 bit AES can be used where the glxsb crypto accelerator is enabled.");
338
			}
339
		}
340

    
341
	/* build our encryption algorithms array */
342
	$pconfig['ealgo'] = array();
343
	$pconfig['ealgo']['name'] = $_POST['ealgo'];
344
	if($pconfig['ealgo_keylen'])
345
		$pconfig['ealgo']['keylen'] = $_POST['ealgo_keylen'];
346

    
347
	if (!$input_errors) {
348
		$ph1ent['ikeid'] = $pconfig['ikeid'];
349
		$ph1ent['iketype'] = $pconfig['iketype'];
350
		if ($pconfig['iketype'] != 'ikev1')
351
			unset($ph1ent['mode']);
352
		else
353
			$ph1ent['mode'] = $pconfig['mode'];
354
		$ph1ent['disabled'] = $pconfig['disabled'] ? true : false;
355
		$ph1ent['interface'] = $pconfig['interface'];
356
		/* if the remote gateway changed and the interface is not WAN then remove route */
357
		/* the vpn_ipsec_configure() handles adding the route */
358
		if ($pconfig['interface'] != "wan") {
359
			if($old_ph1ent['remote-gateway'] != $pconfig['remotegw']) {
360
				mwexec("/sbin/route delete -host {$old_ph1ent['remote-gateway']}");
361
			}
362
		}
363

    
364
		if ($pconfig['mobile'])
365
			$ph1ent['mobile'] = true;
366
		else
367
			$ph1ent['remote-gateway'] = $pconfig['remotegw'];
368

    
369
		$ph1ent['protocol'] = $pconfig['protocol'];
370

    
371
		$ph1ent['myid_type'] = $pconfig['myid_type'];
372
		$ph1ent['myid_data'] = $pconfig['myid_data'];
373
		$ph1ent['peerid_type'] = $pconfig['peerid_type'];
374
		$ph1ent['peerid_data'] = $pconfig['peerid_data'];
375

    
376
		$ph1ent['encryption-algorithm'] = $pconfig['ealgo'];
377
		$ph1ent['hash-algorithm'] = $pconfig['halgo'];
378
		$ph1ent['dhgroup'] = $pconfig['dhgroup'];
379
		$ph1ent['lifetime'] = $pconfig['lifetime'];
380
		$ph1ent['pre-shared-key'] = $pconfig['pskey'];
381
		$ph1ent['private-key'] = base64_encode($pconfig['privatekey']);
382
		$ph1ent['certref'] = $pconfig['certref'];
383
		$ph1ent['caref'] = $pconfig['caref'];
384
		$ph1ent['authentication_method'] = $pconfig['authentication_method'];
385
		$ph1ent['descr'] = $pconfig['descr'];
386
		$ph1ent['nat_traversal'] = $pconfig['nat_traversal'];
387
				$ph1ent['mobike'] = $pconfig['mobike'];
388

    
389
		if (isset($pconfig['reauth_enable']))
390
			$ph1ent['reauth_enable'] = true;
391
		else
392
			unset($ph1ent['reauth_enable']);
393
		if (isset($pconfig['rekey_enable']))
394
			$ph1ent['rekey_enable'] = true;
395
		else
396
			unset($ph1ent['rekey_enable']);
397

    
398
		if (isset($pconfig['responderonly']))
399
			$ph1ent['responderonly'] = true;
400
		else
401
			unset($ph1ent['responderonly']);
402

    
403
		if (isset($pconfig['dpd_enable'])) {
404
			$ph1ent['dpd_delay'] = $pconfig['dpd_delay'];
405
			$ph1ent['dpd_maxfail'] = $pconfig['dpd_maxfail'];
406
		}
407

    
408
		/* generate unique phase1 ikeid */
409
		if ($ph1ent['ikeid'] == 0)
410
			$ph1ent['ikeid'] = ipsec_ikeid_next();
411

    
412
		if (isset($p1index) && $a_phase1[$p1index])
413
			$a_phase1[$p1index] = $ph1ent;
414
		else
415
			$a_phase1[] = $ph1ent;
416

    
417
		write_config();
418
		mark_subsystem_dirty('ipsec');
419

    
420
		header("Location: vpn_ipsec.php");
421
		exit;
422
	}
423
}
424

    
425
function build_interface_list() {
426
	$interfaces = get_configured_interface_with_descr();
427

    
428
	$carplist = get_configured_carp_interface_list();
429

    
430
	foreach ($carplist as $cif => $carpip)
431
		$interfaces[$cif] = $carpip." (".get_vip_descr($carpip).")";
432

    
433
	$aliaslist = get_configured_ip_aliases_list();
434

    
435
	foreach ($aliaslist as $aliasip => $aliasif)
436
		$interfaces[$aliasip] = $aliasip." (".get_vip_descr($aliasip).")";
437

    
438
	$grouplist = return_gateway_groups_array();
439

    
440
	foreach ($grouplist as $name => $group) {
441
		if($group[0]['vip'] != "")
442
			$vipif = $group[0]['vip'];
443
		else
444
			$vipif = $group[0]['int'];
445

    
446
		$interfaces[$name] = "GW Group {$name}";
447
	}
448

    
449
	return($interfaces);
450

    
451
}
452

    
453
function build_auth_method_list() {
454
	global $p1_authentication_methods;
455

    
456
	$list = array();
457

    
458
	foreach ($p1_authentication_methods as $method_type => $method_params){
459
		if (!$pconfig['mobile'] && $method_params['mobile'])
460
			continue;
461

    
462
		$list[$method_type] = htmlspecialchars($method_params['name']);
463
	}
464

    
465
	return($list);
466
}
467

    
468
function build_myid_list() {
469
	global $my_identifier_list;
470

    
471
	$list = array();
472

    
473
	foreach ($my_identifier_list as $id_type => $id_params)
474
		$list[$id_type] = htmlspecialchars($id_params['desc']);
475

    
476
	return($list);
477
}
478

    
479
function build_peerid_list() {
480
	global $peer_identifier_list;
481

    
482
	$list = array();
483

    
484
	foreach ($peer_identifier_list as $id_type => $id_params)
485
		$list[$id_type] = htmlspecialchars($id_params['desc']);
486

    
487
	return($list);
488
}
489

    
490
function build_cert_list() {
491
	global $config;
492

    
493
	$list = array();
494

    
495
	foreach ($config['cert'] as $cert)
496
		$list[$cert['refid']] = $cert['descr'];
497

    
498
	return($list);
499
}
500

    
501
function build_ca_list() {
502
	global $config;
503

    
504
	$list = array();
505

    
506
	foreach ($config['ca'] as $ca)
507
		$list[$ca['refid']] =  $ca['descr'];
508

    
509
	return($list);
510
}
511

    
512
function build_eal_list() {
513
	global $p1_ealgos;
514

    
515
	$list = array();
516

    
517
	foreach ($p1_ealgos as $algo => $algodata)
518
		$list[$algo] = htmlspecialchars($algodata['name']);
519

    
520
	return($list);
521
}
522

    
523
if ($pconfig['mobile'])
524
	$pgtitle = array(gettext("VPN"),gettext("IPsec"),gettext("Edit Phase 1"), gettext("Mobile Client"));
525
else
526
	$pgtitle = array(gettext("VPN"),gettext("IPsec"),gettext("Edit Phase 1"));
527

    
528
$shortcut_section = "ipsec";
529

    
530
include("head.inc");
531

    
532
$tab_array = array();
533
$tab_array[0] = array(gettext("Tunnels"), true, "vpn_ipsec.php");
534
$tab_array[1] = array(gettext("Mobile clients"), false, "vpn_ipsec_mobile.php");
535
$tab_array[2] = array(gettext("Pre-Shared Keys"), false, "vpn_ipsec_keys.php");
536
$tab_array[3] = array(gettext("Advanced Settings"), false, "vpn_ipsec_settings.php");
537
display_top_tabs($tab_array);
538

    
539
require('classes/Form.class.php');
540

    
541
$form = new Form();
542

    
543
$section = new Form_Section('General Information');
544

    
545
$section->addInput(new Form_Checkbox(
546
	'disabled',
547
	'Disabled',
548
	'Set this option to disable this phase1 without removing it from the list. ',
549
	$pconfig['disabled']
550
));
551

    
552
$section->addInput(new Form_Select(
553
	'iketype',
554
	'Key Exchange version',
555
	$pconfig['iketype'],
556
	array("ikev1" => "V1", "ikev2" => "V2", "auto" => "Auto")
557
))->setHelp('Select the Internet Key Exchange protocol version to be used, IKEv1 or IKEv2.');
558

    
559
$section->addInput(new Form_Select(
560
	'protocol',
561
	'Internet Protocol',
562
	$pconfig['protocol'],
563
	array("inet" => "IPv4", "inet6" => "IPv6")
564
))->setHelp('Select the Internet Protocol family.');
565

    
566
$section->addInput(new Form_Select(
567
	'interface',
568
	'Interface',
569
	$pconfig['interface'],
570
	build_interface_list()
571
))->setHelp('Select the interface for the local endpoint of this phase1 entry.');
572

    
573
$section->addInput(new Form_Input(
574
	'remotegw',
575
	'Remote Gateway',
576
	'text',
577
	$pconfig['remotegw']
578
))->setHelp('Enter the public IP address or host name of the remote gateway');
579

    
580
$section->addInput(new Form_Input(
581
	'descr',
582
	'Description',
583
	'text',
584
	$pconfig['descr']
585
))->setHelp('You may enter a description here for your reference (not parsed).');
586

    
587
$form->add($section);
588

    
589
$section = new Form_Section('Phase 1 proposal (Authentication)');
590

    
591
$section->addInput(new Form_Select(
592
	'authentication_method',
593
	'Authentication Method',
594
	$pconfig['authentication_method'],
595
	build_auth_method_list()
596
))->setHelp('Must match the setting chosen on the remote side.');
597

    
598
$section->addInput(new Form_Select(
599
	'mode',
600
	'Negotiation mode',
601
	$pconfig['mode'],
602
	array("main" => "Main", "aggressive" => "Aggressive")
603
))->setHelp('Aggressive is more flexible, but less secure.');
604

    
605
$group = new Form_Group('My identifier');
606

    
607
$group->add(new Form_Select(
608
	'myid_type',
609
	null,
610
	$pconfig['myid_type'],
611
	build_myid_list()
612
));
613

    
614
$group->add(new Form_Input(
615
	'myid_data',
616
	null,
617
	'text',
618
	$pconfig['myid_data']
619
));
620

    
621
$section->add($group);
622

    
623
$group = new Form_Group('Peer identifier');
624
$group->addClass('peeridgroup');
625

    
626
$group->add(new Form_Select(
627
	'peerid_type',
628
	null,
629
	$pconfig['peerid_type'],
630
	build_peerid_list()
631
));
632

    
633
$group->add(new Form_Input(
634
	'peerid_data',
635
	null,
636
	'text',
637
	$pconfig['peerid_data']
638
));
639

    
640
if($pconfig['mobile'])
641
	$group->setHelp('This is known as the "group" setting on some VPN client implementations');
642

    
643
$section->add($group);
644

    
645
$section->addInput(new Form_Input(
646
	'pskey',
647
	'Pre-Shared Key',
648
	'text',
649
	$pconfig['pskey']
650
))->setHelp('Enter your Pre-Shared Key string.');
651

    
652
$section->addInput(new Form_Select(
653
	'certref',
654
	'My Certificate',
655
	$pconfig['certref'],
656
	build_cert_list()
657
))->setHelp('Select a certificate previously configured in the Certificate Manager.');
658

    
659
$section->addInput(new Form_Select(
660
	'caref',
661
	'My Certificate Authority',
662
	$pconfig['caref'],
663
	build_ca_list()
664
))->setHelp('Select a certificate authority previously configured in the Certificate Manager.');
665

    
666
$form->add($section);
667

    
668
$section = new Form_Section('Phase 1 proposal (Algorithms)');
669

    
670
$group = new Form_Group('Encryption Algorithm');
671

    
672
$group->add(new Form_Select(
673
	'ealgo',
674
	null,
675
	$pconfig['ealgo']['name'],
676
	build_eal_list()
677
));
678

    
679
$group->add(new Form_Select(
680
	'ealgo_keylen',
681
	null,
682
	$pconfig['ealgo_keylen'],
683
	array()
684
));
685

    
686
$section->add($group);
687

    
688
$section->addInput(new Form_Select(
689
	'halgo',
690
	'Hash Algorithm',
691
	$pconfig['halgo'],
692
	$p1_halgos
693
))->setHelp('Must match the setting chosen on the remote side.');
694

    
695
$section->addInput(new Form_Select(
696
	'dhgroup',
697
	'DH Group',
698
	$pconfig['dhgroup'],
699
	$p1_dhgroups
700
))->setHelp('Must match the setting chosen on the remote side.');
701

    
702
$section->addInput(new Form_Input(
703
	'lifetime',
704
	'Lifetime (Seconds)',
705
	'number',
706
	$pconfig['lifetime']
707
));
708

    
709
$form->add($section);
710

    
711
$section = new Form_Section('Advanced Options');
712

    
713
$section->addInput(new Form_Checkbox(
714
	'rekey_enable',
715
	'Disable rekey',
716
	'Disables renegotiation when a connection is about to expire.',
717
	$pconfig['rekey_enable']
718
));
719

    
720
$section->addInput(new Form_Checkbox(
721
	'reauth_enable',
722
	'Disable Reauth',
723
	'Whether rekeying of an IKE_SA should also reauthenticate the peer. In IKEv1, reauthentication is always done.',
724
	$pconfig['reauth_enable']
725
));
726

    
727
$section->addInput(new Form_Checkbox(
728
	'responderonly',
729
	'Responder Only',
730
	'Enable this option to never initiate this connection from this side, only respond to incoming requests.',
731
	$pconfig['responderonly']
732
));
733

    
734
$section->addInput(new Form_Select(
735
	'nat_traversal',
736
	'NAT Traversal',
737
	$pconfig['nat_traversal'],
738
	array('on' => 'Auto', 'force' => 'Force')
739
))->setHelp('Set this option to enable the use of NAT-T (i.e. the encapsulation of ESP in UDP packets) if needed, ' .
740
			'which can help with clients that are behind restrictive firewalls.');
741

    
742
$section->addInput(new Form_Select(
743
	'mobike',
744
	'MOBIKE',
745
	$pconfig['mobike'],
746
	array('on' => 'Enable', 'off' => 'Disable')
747
))->setHelp('Set this option to control the use of MOBIKE');
748

    
749
$section->addInput(new Form_Checkbox(
750
	'dpd_enable',
751
	'Dead Peer Detection',
752
	'Enable DPD',
753
	$pconfig['dpd_enable']
754
));
755

    
756
$section->addInput(new Form_Input(
757
	'dpd_delay',
758
	'Delay',
759
	'number',
760
	$pconfig['dpd_delay']
761
))->setHelp('Delay between requesting peer acknowledgement.');
762

    
763
$section->addInput(new Form_Input(
764
	'dpd_maxfail',
765
	'Max failures',
766
	'number',
767
	$pconfig['dpd_maxfail']
768
))->setHelp('Number of consecutive failures allowed before disconnect. ');
769

    
770
if (isset($p1index) && $a_phase1[$p1index]) {
771
	$section->addInput(new Form_Input(
772
		'p1index',
773
		null,
774
		'hidden',
775
		$pconfig['$p1index']
776
	));
777
}
778

    
779
if ($pconfig['mobile']) {
780
	$section->addInput(new Form_Input(
781
		'mobile',
782
		null,
783
		'hidden',
784
		'true'
785
	));
786
}
787

    
788
$section->addInput(new Form_Input(
789
	'ikeid',
790
	null,
791
	'hidden',
792
	$pconfig['ikeid']
793
));
794

    
795
$form->add($section);
796

    
797
print($form);
798

    
799
/* determine if we should init the key length */
800
$keyset = '';
801
if (isset($pconfig['ealgo']['keylen']))
802
	if (is_numeric($pconfig['ealgo']['keylen']))
803
		$keyset = $pconfig['ealgo']['keylen'];
804
?>
805

    
806

    
807
<form action="vpn_ipsec_phase1.php" method="post" name="iform" id="iform">
808

    
809
<?php
810
	if ($input_errors)
811
		print_input_errors($input_errors);
812
?>
813

    
814
<script type="text/javascript">
815
//<![CDATA[
816
events.push(function(){
817

    
818
	function myidsel_change() {
819
		hideGroupInput('myid_data', ($('#myid_type').val() == 'myaddress'));
820
	}
821

    
822
	function iketype_change() {
823

    
824
		if ($('#iketype').val() == 'ikev2') {
825
			hideInput('mode', true);
826
			hideInput('mobike', false);
827
			hideInput('nat_traversal', true);
828
			hideCheckbox('reauth_enable', false);
829
		} else {
830
			hideInput('mode', false);
831
			hideInput('mobike', true);
832
			hideInput('nat_traversal', false);
833
			hideCheckbox('reauth_enable', true);
834
		}
835
	}
836

    
837
	function peeridsel_change() {
838
		hideGroupInput('peerid_data', ($('#peerid_type').val() == 'peeraddress'));
839
	}
840

    
841
	function methodsel_change() {
842

    
843
		switch ($('#authentication_method').val()) {
844
			case 'eap-mschapv2':
845
			case 'eap-tls':
846
			case 'hybrid_rsa_server':
847
			case 'xauth_rsa_server':
848
			case 'rsasig':
849
				hideInput('pskey', true);
850
				hideClass('peeridgroup', false);
851
				hideInput('certref', false);
852
				hideInput('caref', false);
853
				disableInput('certref', false);
854
				disableInput('caref', false);
855
				break;
856

    
857
<?php if ($pconfig['mobile']) { ?>
858
				case 'pre_shared_key':
859
					hideInput('pskey', true);
860
					hideClass('peeridgroup', true);
861
					hideInput('certref', true);
862
					hideInput('caref', true);
863
					disableInput('certref', true);
864
					disableInput('caref', true);
865
					break;
866
<?php } ?>
867
			default: /* psk modes*/
868
				hideInput('pskey', false);
869
				hideClass('peeridgroup', false);
870
				hideInput('certref', true);
871
				hideInput('caref', true);
872
				disableInput('certref', true);
873
				disableInput('caref', true);
874
				break;
875
		}
876
	}
877

    
878
	/* PHP generates javascript case statements for variable length keys */
879
	function ealgosel_change(bits) {
880

    
881
		$("select[name='ealgo_keylen']").find('option').remove().end();
882

    
883
		switch ($('#ealgo').find(":selected").index().toString()) {
884
<?php
885
	$i = 0;
886
	foreach ($p1_ealgos as $algo => $algodata) {
887
		if (is_array($algodata['keysel'])) {
888
?>
889
			case '<?=$i?>':
890
				hideGroupInput('ealgo_keylen', false);
891
<?php
892
			$key_hi = $algodata['keysel']['hi'];
893
			$key_lo = $algodata['keysel']['lo'];
894
			$key_step = $algodata['keysel']['step'];
895

    
896
			for ($keylen = $key_hi; $keylen >= $key_lo; $keylen -= $key_step) {
897
?>
898
				$("select[name='ealgo_keylen']").append($('<option value="<?=$keylen?>"><?=$keylen?> bits</option>'));
899
<?php
900
			}
901
?>
902
			break;
903
<?php
904
		} else {
905
?>
906
			case '<?=$i?>':
907
				hideGroupInput('ealgo_keylen', true);
908
			break;
909
<?php
910
		}
911
		$i++;
912
	}
913
?>
914
		}
915

    
916
		if( bits )
917
			$('#ealgo_keylen').val(bits);
918
	}
919

    
920
	function dpdchkbox_change() {
921
		hide = ! $('#dpd_enable').prop('checked');
922

    
923
		hideInput('dpd_delay', hide);
924
		hideInput('dpd_maxfail', hide);
925

    
926
		if(! $('#dpd_delay').val())
927
			$('#dpd_delay').val('10')
928

    
929
		if(! $('#dpd_maxfail').val())
930
			$('#dpd_maxfail').val('5')
931
	}
932

    
933
	// ---------- Library of show/hide functions ----------------------------------------------------------------------
934

    
935
	// Hides the <div> in which the specified input element lives so that the input,
936
	// its label and help text are hidden
937
	function hideInput(id, hide) {
938
		if(hide)
939
			$('#' + id).parent().parent('div').addClass('hidden');
940
		else
941
			$('#' + id).parent().parent('div').removeClass('hidden');
942
	}
943

    
944
	// Hides the <div> in which the specified group input element lives so that the input,
945
	// its label and help text are hidden
946
	function hideGroupInput(id, hide) {
947
		if(hide)
948
			$('#' + id).parent('div').addClass('hidden');
949
		else
950
			$('#' + id).parent('div').removeClass('hidden');
951
	}
952

    
953
	// Hides the <div> in which the specified checkbox lives so that the checkbox,
954
	// its label and help text are hidden
955
	function hideCheckbox(id, hide) {
956
		if(hide)
957
			$('#' + id).parent().parent().parent('div').addClass('hidden');
958
		else
959
			$('#' + id).parent().parent().parent('div').removeClass('hidden');
960
	}
961

    
962
	// Disables the specified input element
963
	function disableInput(id, disable) {
964
		$('#' + id).prop("disabled", disable);
965
	}
966

    
967
	// Hides all elements of the specified class. This will usually be a section or group
968
	function hideClass(s_class, hide) {
969
		if(hide)
970
			$('.' + s_class).hide();
971
		else
972
			$('.' + s_class).show();
973
	}
974

    
975
	// ---------- Monitor elements for change and call the appropriate display functions ------------------------------
976

    
977
	 // Enable DPD
978
	$('#dpd_enable').click(function () {
979
		dpdchkbox_change();
980
	});
981

    
982
	 // Peer identifier
983
	$('#peerid_type').click(function () {
984
		peeridsel_change();
985
	});
986

    
987
	 // My identifier
988
	$('#myid_type').click(function () {
989
		myidsel_change();
990
	});
991

    
992
	 // ike type
993
	$('#iketype').click(function () {
994
		iketype_change();
995
	});
996

    
997
	 // authentication method
998
	$('#authentication_method').click(function () {
999
		methodsel_change();
1000
	});
1001

    
1002
	 // authentication method
1003
	$('#ealgo').click(function () {
1004
		ealgosel_change(<?=$keyset?>);
1005
	});
1006

    
1007
	// On ititial page load
1008
	myidsel_change();
1009
	peeridsel_change();
1010
	iketype_change();
1011
	methodsel_change();
1012
	ealgosel_change(<?=$keyset?>);
1013
	dpdchkbox_change();
1014
});
1015
//]]>
1016
</script>
1017
<?php
1018

    
1019
include("foot.inc");
(226-226/241)