Project

General

Profile

Download (20.6 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) 2008 Shrew Soft Inc
7
	Copyright (C) 2003-2005 Manuel Kasper <mk@neon1.net>.
8
	All rights reserved.
9

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

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

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

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

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

    
39

    
40
require("guiconfig.inc");
41

    
42
if (!is_array($config['ipsec']['client']))
43
	$config['ipsec']['client'] = array();
44

    
45
$a_client = &$config['ipsec']['client'];
46

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

    
50
$a_phase2 = &$config['ipsec']['phase2'];
51

    
52
if($config['interfaces']['lan'])
53
	$specialsrcdst = explode(" ", "lan");
54

    
55
$p2index = $_GET['p2index'];
56
if (isset($_POST['p2index']))
57
	$p2index = $_POST['p2index'];
58

    
59
if (isset($_GET['dup']))
60
	$p2index = $_GET['dup'];
61

    
62
if (isset($p2index) && $a_phase2[$p2index])
63
{
64
	$pconfig['ikeid'] = $a_phase2[$p2index]['ikeid'];
65
	$pconfig['disabled'] = isset($a_phase2[$p2index]['disabled']);
66
	$pconfig['descr'] = $a_phase2[$p2index]['descr'];
67

    
68
	idinfo_to_pconfig("local",$a_phase2[$p2index]['localid'],$pconfig);
69
	idinfo_to_pconfig("remote",$a_phase2[$p2index]['remoteid'],$pconfig);
70

    
71
	$pconfig['proto'] = $a_phase2[$p2index]['protocol'];
72
	ealgos_to_pconfig($a_phase2[$p2index]['encryption-algorithm-option'],$pconfig);
73
	$pconfig['halgos'] = $a_phase2[$p2index]['hash-algorithm-option'];
74
	$pconfig['pfsgroup'] = $a_phase2[$p2index]['pfsgroup'];
75
	$pconfig['lifetime'] = $a_phase2[$p2index]['lifetime'];
76

    
77
	if (isset($a_phase2[$p2index]['mobile']))
78
		$pconfig['mobile'] = true;
79
}
80
else
81
{
82
	$pconfig['ikeid'] = $_GET['ikeid'];
83

    
84
	/* defaults */
85
	$pconfig['localid_type'] = "lan";
86
	$pconfig['remoteid_type'] = "network";
87
	$pconfig['proto'] = "esp";
88
	$pconfig['ealgos'] = explode(",", "3des,blowfish,cast128,aes");
89
	$pconfig['halgos'] = explode(",", "hmac_sha1,hmac_md5");
90
	$pconfig['pfsgroup'] = "0";
91
	$pconfig['lifetime'] = "3600";
92

    
93
    /* mobile client */
94
    if($_GET['mobile'])
95
        $pconfig['mobile']=true;
96
}
97

    
98
if (isset($_GET['dup']))
99
	unset($p2index);
100

    
101
if ($_POST) {
102

    
103
	unset($input_errors);
104
	$pconfig = $_POST;
105

    
106
	if (!isset( $_POST['ikeid']))
107
		$input_errors[] = "A valid ikeid must be specified.";
108

    
109
	/* input validation */
110
	$reqdfields = explode(" ", "localid_type halgos");
111
	$reqdfieldsn = explode(",", "Local network type,P2 Hash Algorithms");
112
	if (!isset($pconfig['mobile'])){
113
		$reqdfields[] = "remoteid_type";
114
		$reqdfieldsn[] = "Remote network type";
115
	}
116

    
117
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
118

    
119
	switch ($pconfig['localid_type']) {
120
		case "network":
121
			if (!$pconfig['localid_netbits'] || !is_numeric($pconfig['localid_netbits']))
122
				$input_errors[] = "A valid local network bit count must be specified..";
123
		case "address":
124
			if (!$pconfig['localid_address'] || !is_ipaddr($pconfig['localid_address']))
125
				$input_errors[] = "A valid local network IP address must be specified.";
126
			break;
127
	}
128

    
129
	switch ($pconfig['remoteid_type']) {
130
		case "network":
131
			if (!$pconfig['remoteid_netbits'] || !is_numeric($pconfig['remoteid_netbits']))
132
				$input_errors[] = "A valid remote network bit count must be specified..";
133
		case "address":
134
			if (!$pconfig['remoteid_address'] || !is_ipaddr($pconfig['remoteid_address']))
135
				$input_errors[] = "A valid remote network IP address must be specified.";
136
			break;
137
	}
138

    
139
/* TODO : Validate enabled phase2's are not duplicates */
140

    
141
	$ealgos = pconfig_to_ealgos($pconfig);
142

    
143
	if (!count($ealgos)) {
144
		$input_errors[] = "At least one encryption algorithm must be selected.";
145
	}
146
	if (($_POST['lifetime'] && !is_numeric($_POST['lifetime']))) {
147
		$input_errors[] = "The P2 lifetime must be an integer.";
148
	}
149

    
150
	if (!$input_errors) {
151

    
152
		$ph2ent['ikeid'] = $pconfig['ikeid'];
153
		$ph2ent['disabled'] = $pconfig['disabled'] ? true : false;
154

    
155
		$ph2ent['localid'] = pconfig_to_idinfo("local",$pconfig);
156
		$ph2ent['remoteid'] = pconfig_to_idinfo("remote",$pconfig);
157

    
158
		$ph2ent['protocol'] = $pconfig['proto'];
159
		$ph2ent['encryption-algorithm-option'] = $ealgos;
160
		$ph2ent['hash-algorithm-option'] = $pconfig['halgos'];
161
		$ph2ent['pfsgroup'] = $pconfig['pfsgroup'];
162
		$ph2ent['lifetime'] = $pconfig['lifetime'];
163
		$ph2ent['descr'] = $pconfig['descr'];
164

    
165
		if (isset($pconfig['mobile']))
166
			$ph2ent['mobile'] = true;
167

    
168
		if (isset($p2index) && $a_phase2[$p2index])
169
			$a_phase2[$p2index] = $ph2ent;
170
		else
171
			$a_phase2[] = $ph2ent;
172

    
173
		write_config();
174
		touch($d_ipsecconfdirty_path);
175

    
176
		header("Location: vpn_ipsec.php");
177
		exit;
178
	}
179
}
180

    
181
if ($pconfig['mobile'])
182
    $pgtitle = array("VPN","IPsec","Edit Phase 2", "Mobile Client");
183
else
184
    $pgtitle = array("VPN","IPsec","Edit Phase 2");
185

    
186
include("head.inc");
187

    
188
?>
189

    
190
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
191
<?php include("fbegin.inc"); ?>
192
<script language="JavaScript">
193
<!--
194
function typesel_change_local(bits) {
195

    
196
	if (!bits)
197
		bits = 24;
198

    
199
	switch (document.iform.localid_type.selectedIndex) {
200
		case 0:	/* single */
201
			document.iform.localid_address.disabled = 0;
202
			document.iform.localid_netbits.value = 0;
203
			document.iform.localid_netbits.disabled = 1;
204
			break;
205
		case 1:	/* network */
206
			document.iform.localid_address.disabled = 0;
207
			document.iform.localid_netbits.value = bits;
208
			document.iform.localid_netbits.disabled = 0;
209
			break;
210
		default:
211
			document.iform.localid_address.value = "";
212
			document.iform.localid_address.disabled = 1;
213
			document.iform.localid_netbits.value = 0;
214
			document.iform.localid_netbits.disabled = 1;
215
			break;
216
	}
217
}
218

    
219
<?php if (isset($pconfig['mobile'])): ?>
220

    
221
function typesel_change_remote(bits) {
222

    
223
	document.iform.remoteid_address.disabled = 1;
224
	document.iform.remoteid_netbits.disabled = 1;
225
}
226

    
227
<?php else: ?>
228

    
229
function typesel_change_remote(bits) {
230

    
231
	if (!bits)
232
		bits = 24;
233

    
234
	switch (document.iform.remoteid_type.selectedIndex) {
235
		case 0:	/* single */
236
			document.iform.remoteid_address.disabled = 0;
237
			document.iform.remoteid_netbits.value = 0;
238
			document.iform.remoteid_netbits.disabled = 1;
239
			break;
240
		case 1:	/* network */
241
			document.iform.remoteid_address.disabled = 0;
242
			document.iform.remoteid_netbits.value = bits;
243
			document.iform.remoteid_netbits.disabled = 0;
244
			break;
245
		default:
246
			document.iform.remoteid_address.value = "";
247
			document.iform.remoteid_address.disabled = 1;
248
			document.iform.remoteid_netbits.value = 0;
249
			document.iform.remoteid_netbits.disabled = 1;
250
			break;
251
	}
252
}
253

    
254
<?php endif; ?>
255

    
256
//-->
257

    
258
</script>
259
<?php if ($input_errors) print_input_errors($input_errors); ?>
260
            <form action="vpn_ipsec_phase2.php" method="post" name="iform" id="iform">
261
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
262
                <tr>
263
                  <td width="22%" valign="top" class="vncellreq">Mode</td>
264
                  <td width="78%" class="vtable"> Tunnel</td>
265
                </tr>
266
                <tr>
267
                  <td width="22%" valign="top" class="vncellreq">Disabled</td>
268
                  <td width="78%" class="vtable">
269
                    <input name="disabled" type="checkbox" id="disabled" value="yes" <?php if ($pconfig['disabled']) echo "checked"; ?>>
270
                    <strong>Disable this phase2 entry</strong><br>
271
                    <span class="vexpl">Set this option to disable this phase2 entry without
272
                      removing it from the list.
273
                    </span>
274
                  </td>
275
                </tr>
276
                <tr>
277
                  <td width="22%" valign="top" class="vncellreq">Local Network</td>
278
                  <td width="78%" class="vtable">
279
                    <table border="0" cellspacing="0" cellpadding="0">
280
                      <tr>
281
                        <td>Type:&nbsp;&nbsp;</td>
282
                        <td></td>
283
                        <td>
284
                          <select name="localid_type" class="formselect" onChange="typesel_change_local()">
285
                            <option value="address" <?php if ($pconfig['localid_type'] == "address") echo "selected";?>>Address</option>
286
                            <option value="network" <?php if ($pconfig['localid_type'] == "network") echo "selected";?>>Network</option>
287
                            <option value="lan" <?php if ($pconfig['localid_type'] == "lan" ) echo "selected";?>>LAN subnet</option>
288
                          </select>
289
                        </td>
290
                      </tr>
291
                      <tr>
292
                        <td>Address:&nbsp;&nbsp;</td>
293
                        <td><?=$mandfldhtmlspc;?></td>
294
                        <td>
295
                          <input name="localid_address" type="text" class="formfld unknown" id="localid_address" size="20" value="<?=$pconfig['localid_address'];?>">
296
                          /
297
                          <select name="localid_netbits" class="formselect" id="localid_netbits">
298
                            <?php for ($i = 32; $i >= 0; $i--): ?>
299
                            <option value="<?=$i;?>" <?php if ($i == $pconfig['localid_netbits']) echo "selected"; ?>>
300
                              <?=$i;?>
301
                            </option>
302
                            <?php endfor; ?>
303
                          </select>
304
                        </td>
305
                      </tr>
306
                    </table>
307
                  </td>
308
                </tr>
309
                <?php if (!isset($pconfig['mobile'])): ?>
310
				<tr>
311
                  <td width="22%" valign="top" class="vncellreq">Remote Network</td>
312
                  <td width="78%" class="vtable">
313
                    <table border="0" cellspacing="0" cellpadding="0">
314
                      <tr>
315
                        <td>Type:&nbsp;&nbsp;</td>
316
                        <td></td>
317
                        <td>
318
                          <select name="remoteid_type" class="formselect" onChange="typesel_change_remote()">
319
                            <option value="address" <?php if ($pconfig['remoteid_type'] == "address") echo "selected"; ?>>Address</option>
320
                            <option value="network" <?php if ($pconfig['remoteid_type'] == "network") echo "selected"; ?>>Network</option>
321
                          </select>
322
                        </td>
323
                      </tr>
324
                      <tr>
325
                        <td>Address:&nbsp;&nbsp;</td>
326
                        <td><?=$mandfldhtmlspc;?></td>
327
                        <td>
328
                          <input name="remoteid_address" type="text" class="formfld unknown" id="remoteid_address" size="20" value="<?=$pconfig['remoteid_address'];?>">
329
                          /
330
                          <select name="remoteid_netbits" class="formselect" id="remoteid_netbits">
331
                            <?php for ($i = 32; $i >= 0; $i--): ?>
332
                            <option value="<?=$i;?>" <?php if ($i == $pconfig['remoteid_netbits']) echo "selected"; ?>>
333
                              <?=$i;?>
334
                            </option>
335
                            <?php endfor; ?>
336
                          </select>
337
                        </td>
338
                      </tr>
339
                    </table>
340
                  </td>
341
                </tr>
342
                <?php endif; ?>
343
                <tr>
344
                  <td width="22%" valign="top" class="vncell">Description</td>
345
                  <td width="78%" class="vtable">
346
                    <input name="descr" type="text" class="formfld unknown" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>">
347
                    <br> <span class="vexpl">You may enter a description here
348
                    for your reference (not parsed).</span>
349
                  </td>
350
                </tr>
351
                <tr>
352
                  <td colspan="2" class="list" height="12"></td>
353
                </tr>
354
                <tr>
355
                  <td colspan="2" valign="top" class="listtopic">Phase 2 proposal
356
                    (SA/Key Exchange)
357
                  </td>
358
                </tr>
359
                <tr>
360
                  <td width="22%" valign="top" class="vncellreq">Protocol</td>
361
                  <td width="78%" class="vtable">
362
                    <select name="proto" class="formselect">
363
                      <?php foreach ($p2_protos as $proto => $protoname): ?>
364
                      <option value="<?=$proto;?>" <?php if ($proto == $pconfig['proto']) echo "selected"; ?>>
365
                        <?=htmlspecialchars($protoname);?>
366
                      </option>
367
                      <?php endforeach; ?>
368
                    </select>
369
                    <br>
370
                    <span class="vexpl">ESP is encryption, AH is authentication only </span>
371
                  </td>
372
                </tr>
373
                <tr>
374
                  <td width="22%" valign="top" class="vncellreq">Encryption algorithms</td>
375
                  <td width="78%" class="vtable">
376
                    <table border="0" cellspacing="0" cellpadding="0">
377
                      <?php
378
                        foreach ($p2_ealgos as $algo => $algodata):
379
                        $checked = '';
380
                        if (in_array($algo,$pconfig['ealgos']))
381
                          $checked = " checked";
382
                      ?>
383
                      <tr>
384
                        <td>
385
                          <input type="checkbox" name="ealgos[]" value="<?=$algo;?>"<?=$checked?>>
386
						</td>
387
                        <td>
388
                          <?=htmlspecialchars($algodata['name']);?>
389
                        </td>
390
                        <td>
391
                          <?php if(is_array($algodata['keysel'])): ?>
392
                          &nbsp;&nbsp;
393
                          <select name="keylen_<?=$algo;?>" class="formselect">
394
                            <option value="auto">auto</option>
395
                            <?php
396
                              $key_hi = $algodata['keysel']['hi'];
397
                              $key_lo = $algodata['keysel']['lo'];
398
                              $key_step = $algodata['keysel']['step'];
399
                              for ($keylen = $key_hi; $keylen >= $key_lo; $keylen -= $key_step):
400
                                $selected = '';
401
//                                if ($checked && in_array("keylen_".$algo,$pconfig))
402
                                  if ($keylen == $pconfig["keylen_".$algo])
403
                                    $selected = " selected";
404
                             ?>
405
                            <option value="<?=$keylen;?>"<?=$selected;?>><?=$keylen;?> bits</option>
406
                            <?php endfor; ?>
407
                          </select>
408
                          <?php endif; ?>
409
                        </td>
410
                      </tr>
411
                      <?php endforeach; ?>
412
                    </table>
413
                    <br>
414
                    Hint: use 3DES for best compatibility or if you have a hardware
415
                    crypto accelerator card. Blowfish is usually the fastest in
416
                    software encryption.
417
                  </td>
418
                </tr>
419
                <tr>
420
                  <td width="22%" valign="top" class="vncellreq">Hash algorithms</td>
421
                  <td width="78%" class="vtable">
422
                    <?php foreach ($p2_halgos as $algo => $algoname): ?>
423
                    <input type="checkbox" name="halgos[]" value="<?=$algo;?>" <?php if (in_array($algo, $pconfig['halgos'])) echo "checked"; ?>>
424
                    <?=htmlspecialchars($algoname);?>
425
                    <br>
426
                    <?php endforeach; ?>
427
                  </td>
428
                </tr>
429
                <tr>
430
                  <td width="22%" valign="top" class="vncellreq">PFS key group</td>
431
                  <td width="78%" class="vtable">
432
					<?php if (!isset($pconfig['mobile']) || !isset($a_client['pfs_group'])): ?>
433
                    <select name="pfsgroup" class="formselect">
434
                      <?php foreach ($p2_pfskeygroups as $keygroup => $keygroupname): ?>
435
                      <option value="<?=$keygroup;?>" <?php if ($keygroup == $pconfig['pfsgroup']) echo "selected"; ?>>
436
                        <?=htmlspecialchars($keygroupname);?>
437
                      </option>
438
                      <?php endforeach; ?>
439
                    </select>
440
                    <br>
441
                    <span class="vexpl"><em>1 = 768 bit, 2 = 1024 bit, 5 = 1536 bit</em></span>
442
					<?php else: ?>
443
                    <select class="formselect" disabled>
444
                      <option selected><?=$p2_pfskeygroups[$a_client['pfs_group']];?></option>
445
                    </select>
446
                    <input name="pfsgroup" type="hidden" value="<?=$pconfig['pfsgroup'];?>">
447
                    <br>
448
                    <span class="vexpl"><em>Set globally in mobile client options</em></span>
449
					<?php endif; ?>
450
                  </td>
451
                </tr>
452
                <tr>
453
                  <td width="22%" valign="top" class="vncell">Lifetime</td>
454
                  <td width="78%" class="vtable">
455
                    <input name="lifetime" type="text" class="formfld unknown" id="lifetime" size="20" value="<?=$pconfig['lifetime'];?>">
456
                    seconds
457
                  </td>
458
                </tr>
459
                <tr>
460
                  <td width="22%" valign="top">&nbsp;</td>
461
                  <td width="78%">
462
                    <?php if (isset($p2index) && $a_phase2[$p2index]): ?>
463
                    <input name="p2index" type="hidden" value="<?=$p2index;?>">
464
                    <?php endif; ?>
465
                    <?php if ($pconfig['mobile']): ?>
466
                    <input name="mobile" type="hidden" value="true">
467
                    <input name="remoteid_type" type="hidden" value="mobile">
468
                    <?php endif; ?>
469
                    <input name="Submit" type="submit" class="formbtn" value="Save">
470
                    <input name="ikeid" type="hidden" value="<?=$pconfig['ikeid'];?>">
471
                  </td>
472
                </tr>
473
              </table>
474
</form>
475
<script lannguage="JavaScript">
476
<!--
477
typesel_change_local(<?=$pconfig['localid_netbits']?>);
478
typesel_change_remote(<?=$pconfig['remoteid_netbits']?>);
479
//-->
480
</script>
481
<?php include("fend.inc"); ?>
482
</body>
483
</html>
484

    
485
<?php
486

    
487
/* local utility functions */
488

    
489
function pconfig_to_ealgos(& $pconfig) {
490

    
491
	global $p2_ealgos;
492

    
493
	$ealgos = array();
494
	foreach ($p2_ealgos as $algo_name => $algo_data) {
495
		if (in_array($algo_name,$pconfig['ealgos'])) {
496
			$ealg = array();
497
			$ealg['name'] = $algo_name;
498
			if (is_array($algo_data['keysel']))
499
				$ealg['keylen'] = $_POST["keylen_".$algo_name];
500
			$ealgos[] = $ealg;
501
		}
502
	}
503

    
504
	return $ealgos;
505
}
506

    
507
function ealgos_to_pconfig(& $ealgos,& $pconfig) {
508

    
509
	$pconfig['ealgos'] = array();
510
	foreach ($ealgos as $algo_data) {
511
		$pconfig['ealgos'][] = $algo_data['name'];
512
		if (isset($algo_data['keylen']))
513
			$pconfig["keylen_".$algo_data['name']] = $algo_data['keylen'];
514
	}
515

    
516
	return $ealgos;
517
}
518

    
519
function pconfig_to_idinfo($prefix,& $pconfig) {
520

    
521
	$type = $pconfig[$prefix."id_type"];
522
	$address = $pconfig[$prefix."id_address"];
523
	$netbits = $pconfig[$prefix."id_netbits"];
524

    
525
	switch( $type )
526
	{
527
		case "address":
528
			return array('type' => $type, 'address' => $address);
529
		case "network":
530
			return array('type' => $type, 'address' => $address, 'netbits' => $netbits);
531
		default:
532
			return array('type' => $type );
533
	}
534
}
535

    
536
function idinfo_to_pconfig($prefix,& $idinfo,& $pconfig) {
537

    
538
	switch( $idinfo['type'] )
539
	{
540
		case "address":
541
			$pconfig[$prefix."id_type"] = $idinfo['type'];
542
			$pconfig[$prefix."id_address"] = $idinfo['address'];
543
			break;
544
		case "network":
545
			$pconfig[$prefix."id_type"] = $idinfo['type'];
546
			$pconfig[$prefix."id_address"] = $idinfo['address'];
547
			$pconfig[$prefix."id_netbits"] = $idinfo['netbits'];
548
			break;
549
		default:
550
			$pconfig[$prefix."id_type"] = $idinfo['type'];
551
			break;
552
	}
553
}
554

    
555
?>
556

    
(203-203/214)