Project

General

Profile

Download (13.7 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	vpn_ipsec.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
	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
34
##|*NAME=VPN: IPsec page
35
##|*DESCR=Allow access to the 'VPN: IPsec' page.
36
##|*MATCH=vpn_ipsec.php*
37
##|-PRIV
38

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

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

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

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

    
55
$wancfg = &$config['interfaces']['wan'];
56

    
57
$pconfig['enable'] = isset($config['ipsec']['enable']);
58

    
59
if ($_POST) {
60

    
61
	if ($_POST['apply']) {
62
		$retval = 0;
63
		$retval = vpn_ipsec_refresh_policies();
64
		$retval = vpn_ipsec_configure();
65
		/* reload the filter in the background */
66
		filter_configure();
67
		$savemsg = get_std_save_message($retval);
68
		if ($retval == 0) {
69
			if (is_subsystem_dirty('ipsec'))
70
				clear_subsystem_dirty('ipsec');
71
		}
72
	} else if ($_POST['submit']) {
73
		$pconfig = $_POST;
74

    
75
		$config['ipsec']['enable'] = $_POST['enable'] ? true : false;
76

    
77
		if(!$config['ipsec']['enable']) {
78
			if(is_process_running("racoon")) 
79
				mwexec("killall racoon");
80
		} else {
81
			$retval = vpn_ipsec_configure();
82
		}
83

    
84
		write_config();
85
	}
86
}
87

    
88
if ($_GET['act'] == "delph1")
89
{
90
	if ($a_phase1[$_GET['p1index']]) {
91
		/* remove static route if interface is not WAN */
92
		if ($a_phase1[$_GET['p1index']]['interface'] <> "wan")
93
			mwexec("/sbin/route delete -host {$$a_phase1[$_GET['p1index']]['remote-gateway']}");
94

    
95
		/* remove all phase2 entries that match the ikeid */
96
		$ikeid = $a_phase1[$_GET['p1index']]['ikeid'];
97
		foreach ($a_phase2 as $p2index => $ph2tmp)
98
			if ($ph2tmp['ikeid'] == $ikeid)
99
				unset($a_phase2[$p2index]);
100

    
101
		/* remove the phase1 entry */
102
		unset($a_phase1[$_GET['p1index']]);
103
		vpn_ipsec_refresh_policies();
104
		vpn_ipsec_configure();
105
		write_config();
106
		filter_configure();
107
		header("Location: vpn_ipsec.php");
108
		exit;
109
	}
110
}
111

    
112
if ($_GET['act'] == "delph2")
113
{
114
	if ($a_phase2[$_GET['p2index']]) {
115
		/* remove the phase2 entry */
116
		unset($a_phase2[$_GET['p2index']]);
117
		vpn_ipsec_refresh_policies();
118
		vpn_ipsec_configure();
119
		filter_configure();
120
		write_config();
121
		header("Location: vpn_ipsec.php");
122
		exit;
123
	}
124
}
125

    
126
$pgtitle = array("VPN","IPsec");
127
include("head.inc");
128

    
129
?>
130

    
131
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
132
<?php include("fbegin.inc"); ?>
133
<form action="vpn_ipsec.php" method="post">
134
<?php
135
	if ($savemsg)
136
		print_info_box($savemsg);
137
	if ($pconfig['enable'] && is_subsystem_dirty('ipsec'))
138
		print_info_box_np("The IPsec tunnel configuration has been changed.<br>You must apply the changes in order for them to take effect.");
139
?>
140
<table width="100%" border="0" cellpadding="0" cellspacing="0">
141
	<tr>
142
		<td class="tabnavtbl">
143
			<?php
144
				$tab_array = array();
145
				$tab_array[0] = array("Tunnels", true, "vpn_ipsec.php");
146
				$tab_array[1] = array("Mobile clients", false, "vpn_ipsec_mobile.php");
147
				$tab_array[2] = array("Logs", false, "diag_logs_ipsec.php");
148
				display_top_tabs($tab_array);
149
			?>
150
		</td>
151
	</tr>
152
	<tr>
153
		<td>
154
			<div id="mainarea">
155
				<table border='1'>
156
				<table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
157
					<tr>
158
						<td class="vtable">
159
							<table border="0" cellspacing="2" cellpadding="0">
160
								<tr>
161
									<td>
162
										<input name="enable" type="checkbox" id="enable" value="yes" <?php if ($pconfig['enable']) echo "checked";?>>
163
									</td>
164
									<td>
165
										<strong>Enable IPsec</strong>
166
									</td>
167
								</tr>
168
							</table>
169
						</td>
170
					</tr>
171
					<tr>
172
						<td>
173
							<input name="submit" type="submit" class="formbtn" value="Save">
174
						</td>
175
					</tr>
176
				</table>
177
				<table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0">
178
					<?php
179
						$i = 0;
180
						foreach ($a_phase1 as $ph1ent) {
181
							if (isset( $ph1ent['disabled'])) {
182
								$spans = "<span class=\"gray\">";
183
								$spane = "</span>";
184
							}
185
							else
186
								$spans = $spane = "";
187
						
188
						show_ipsec_header($ph1ent);
189
						$counter++; // used to deterimine if we need to output header manually (no records exist)
190
					?>					
191
					<tr valign="top" ondblclick="document.location='vpn_ipsec_phase1.php?p1index=<?=$i;?>'">
192
						<td class="listlr">
193
							<?=$spans;?>
194
							<?php
195
								if ($ph1ent['interface']) {
196
									$iflabels = get_configured_interface_with_descr();
197
									$carplist = get_configured_carp_interface_list();
198
									foreach ($carplist as $cif => $carpip)
199
										$iflabels[$cif] = strtoupper($cif) . " ({$carpip})"; 
200
									$if = htmlspecialchars($iflabels[$ph1ent['interface']]);
201
								}
202
								else
203
									$if = "WAN";
204

    
205
								if (!isset($ph1ent['mobile']))
206
									echo $if."<br>".$ph1ent['remote-gateway'];
207
								else
208
									echo $if."<br><strong>Mobile Client</strong>";
209
							?>
210
							<?=$spane;?>
211
						</td>
212
						<td class="listr">
213
							<?=$spans;?>
214
							<?=$ph1ent['mode'];?>
215
							<?=$spane;?>
216
						</td>
217
						<td class="listr">
218
							<?=$spans;?>
219
							<?=$p1_ealgos[$ph1ent['encryption-algorithm']['name']]['name'];?>
220
							<?php
221
								if ($ph1ent['encryption-algorithm']['keylen']) {
222
									if ($ph1ent['encryption-algorithm']['keylen']=="auto")
223
										echo " (auto)";
224
									else
225
										echo " ({$ph1ent['encryption-algorithm']['keylen']} bits)";
226
								}
227
							?>
228
							<?=$spane;?>
229
						</td>
230
						<td class="listr">
231
							<?=$spans;?>
232
							<?=$p1_halgos[$ph1ent['hash-algorithm']];?>
233
							<?=$spane;?>
234
						</td>
235
						<td class="listbg">
236
							<?=$spans;?>
237
							<?=htmlspecialchars($ph1ent['descr']);?>&nbsp;
238
							<?=$spane;?>
239
						</td>
240
						<td valign="middle" nowrap class="list">
241
							<table border="0" cellspacing="0" cellpadding="1">
242
								<tr>
243
									<td>
244
										<a href="vpn_ipsec_phase1.php?p1index=<?=$i;?>">
245
											<img src="./themes/<?= $g['theme']; ?>/images/icons/icon_e.gif" title="edit phase1 entry" width="17" height="17" border="0">
246
										</a>
247
									</td>
248
									<td>
249
										<a href="vpn_ipsec.php?act=delph1&p1index=<?=$i;?>" onclick="return confirm('Do you really want to delete this phase1 and all associated phase2 entries?')">
250
											<img src="./themes/<?= $g['theme']; ?>/images/icons/icon_x.gif" title="delete phase1 entry" width="17" height="17" border="0">
251
										</a>
252
									</td>
253
								</tr>
254
								<?php if (!isset($ph1ent['mobile'])): ?>
255
								<tr>
256
									<td>
257
									</td>
258
									<td>
259
										<a href="vpn_ipsec_phase1.php?dup=<?=$i;?>">
260
											<img src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" title="copy phase1 entry" width="17" height="17" border="0">
261
										</a>
262
									</td>
263
								</tr>
264
								<?php endif; ?>
265
							</table>
266
						</td>
267
					</tr>
268
					<tr>
269
						<td class="listrborder" colspan="5">
270
							<div id="shph2but-<?=$i?>">
271
								<?php
272
									$phase2count=0;
273
									foreach ($a_phase2 as $ph2ent) {
274
										if ($ph2ent['ikeid'] != $ph1ent['ikeid']) 
275
											continue;
276
										if (isset( $ph2ent['disabled']) || isset($ph1ent['disabled'])) 
277
											continue;
278
										$phase2count++;
279
									}
280
								?>								
281
								<input  type="button" onClick="show_phase2('tdph2-<?=$i?>','shph2but-<?=$i?>')" value="+"></input> - Show <?=$phase2count?> Phase-2 entries</a>
282
							</div>
283
							<table class="tabcont" width="100%" height="100%" border="0" cellspacing="0" cellpadding="0" id="tdph2-<?=$i?>" style="display:none">
284
								<tr>
285
									<td class="listhdrr">Mode</td>
286
									<?php if($ph2ent['mode'] == "tunnel"): ?>
287
									<td class="listhdrr">Local Subnet</td>
288
									<td class="listhdrr">Remote Subnet</td>
289
									<?php endif; ?>
290
									<td class="listhdrr">P2 Protocol</td>
291
									<td class="listhdrr">P2 Transforms</td>
292
									<td class="listhdrr">P2 Auth Methods</td>
293
									<td class ="list">
294
										<a href="vpn_ipsec_phase2.php?ikeid=<?=$ph1ent['ikeid'];?><?php if (isset($ph1ent['mobile'])) echo "&mobile=true";?>">
295
											<img src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" title="add phase2 entry" width="17" height="17" border="0">
296
										</a>
297
									</td>
298
								</tr>
299
								<?php
300
									$j = 0;
301
									foreach ($a_phase2 as $ph2ent) {
302
										if ($ph2ent['ikeid'] != $ph1ent['ikeid']) {
303
											$j++;
304
											continue;
305
										}
306

    
307
										if (isset( $ph2ent['disabled']) || isset($ph1ent['disabled'])) {
308
											$spans = "<span class=\"gray\">";
309
											$spane = "</span>";
310
										}
311
										else
312
											$spans = $spane = "";
313
								?>
314
								<tr valign="top" ondblclick="document.location='vpn_ipsec_phase2.php?p2index=<?=$j;?>'">
315

    
316
									<td nowrap class="listlr">
317
										<?=$spans;?>
318
											<?=$ph2ent['mode'];?>
319
										<?=$spane;?>
320
									</td>
321
									<?php if($ph2ent['mode'] == "tunnel"): ?>
322
									<td nowrap class="listr">
323
										<?=$spans;?>
324
											<?=ipsec_idinfo_to_text($ph2ent['localid']); ?>
325
										<?=$spane;?>
326
									</td>
327
									<td nowrap class="listr">
328
										<?=$spans;?>
329
											<?=ipsec_idinfo_to_text($ph2ent['remoteid']); ?>
330
										<?=$spane;?>
331
									</td>
332
									<?php endif; ?>
333
									<td nowrap class="listr">
334
										<?=$spans;?>
335
											<?php echo $p2_protos[$ph2ent['protocol']];	?>
336
										<?=$spane;?>
337
									</td>
338
									<td class="listr">
339
										<?=$spans;?>
340
										<?php
341
											$k = 0;
342
											foreach ($ph2ent['encryption-algorithm-option'] as $ph2ea) {
343
												if ($k++)
344
													echo ", ";
345
												echo $p2_ealgos[$ph2ea['name']]['name'];
346
												if ($ph2ea['keylen']) {
347
													if ($ph2ea['keylen']=="auto")
348
														echo " (auto)";
349
													else
350
														echo " ({$ph2ea['keylen']} bits)";
351
												}
352
											}
353
										?>
354
										<?=$spane;?>
355
									</td>
356
									<td nowrap class="listr">
357
										<?=$spans;?>
358
										<?php
359
											$k = 0;
360
											foreach ($ph2ent['hash-algorithm-option'] as $ph2ha) {
361
												if ($k++)
362
													echo ", ";
363
												echo $p2_halgos[$ph2ha];
364
											}
365
										?>
366
										<?=$spane;?>
367
									</td>
368
									<td nowrap class="list">
369
										<a href="vpn_ipsec_phase2.php?p2index=<?=$j;?>">
370
											<img src="./themes/<?= $g['theme']; ?>/images/icons/icon_e.gif" title="edit phase2 entry" width="17" height="17" border="0">
371
										</a>
372
										<a href="vpn_ipsec.php?act=delph2&p2index=<?=$j;?>" onclick="return confirm('Do you really want to delete this phase2 entry?')">
373
											<img src="./themes/<?= $g['theme']; ?>/images/icons/icon_x.gif" title="delete phase2 entry" width="17" height="17" border="0">
374
										</a>
375
									</td>
376
								</tr>
377

    
378
								<?php
379
										$j++;
380
									}
381
								?>
382
							</table>
383
						</td>
384
					</tr>
385
					<tr>
386
						<td>
387
							&nbsp;
388
						</td>
389
					</tr>
390
					<?php
391
							$i++;
392
						}
393
					if(!$counter)
394
						show_ipsec_header();
395
					?>
396
					<tr>
397
						<td class="list" colspan="5"></td>
398
						<td class="list">
399
							<table border="0" cellspacing="0" cellpadding="1">
400
								<tr>
401
									<td width="17"></td>
402
									<td>
403
										<a href="vpn_ipsec_phase1.php">
404
											<img src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" title="add phase1 entry" width="17" height="17" border="0">
405
										</a>
406
									</td>
407
								</tr>
408
							</table>
409
						<td>
410
					</tr>
411
					<tr>
412
						<td colspan="4">
413
							<p>
414
								<span class="vexpl">
415
									<span class="red">
416
										<strong>Note:<br></strong>
417
									</span>
418
									You can check your IPsec status at <a href="diag_ipsec.php">Status:IPsec</a>.
419
								</span>
420
							</p>
421
						</td>
422
					</tr>
423
				</table>
424
			</div>
425
		</td>
426
	</tr>
427
</table>
428
</form>
429
<?php include("fend.inc"); ?>
430
<script type="text/javascript">
431
function show_phase2(id, buttonid) {
432
	document.getElementById(buttonid).innerHTML='';
433
	aodiv = document.getElementById(id);
434
	aodiv.style.display = "block";
435
}
436
</script>
437
</body>
438
</html>
439

    
440
<?php
441

    
442
function show_ipsec_header() {
443
	if (isset($ph1ent['mobile'])) $mobile = "&mobile=true";
444
	echo <<<EOF
445
	<tr>
446
		<td class="listhdrr">Remote Gateway</td>
447
		<td class="listhdrr">Mode</td>
448
		<td class="listhdrr">P1 Protocol</td>
449
		<td class="listhdrr">P1 Transforms</td>
450
		<td class="listhdrr">P1 Description</td>
451
		<td class ="list">
452
			<a href="vpn_ipsec_phase2.php?ikeid={$ph1ent['ikeid']}{$mobile}">
453
				<img src="./themes/{$g['theme']}/images/icons/icon_plus.gif" title="add phase2 entry" width="17" height="17" border="0">
454
			</a>
455
		</td>
456
	</tr>
457

    
458
EOF;
459
	
460
}
461

    
462
?>
(197-197/214)