diag_ipsec.php 14.3 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1
<?php
2

Ad Schellevis's avatar
Ad Schellevis committed
3
/*
4
	Copyright (C) 2014 Deciso B.V.
Ad Schellevis's avatar
Ad Schellevis committed
5 6 7
	Copyright (C) 2004-2009 Scott Ullrich
	Copyright (C) 2008 Shrew Soft Inc <mgrooms@shrew.net>.
	Copyright (C) 2003-2004 Manuel Kasper
8
	All rights reserved.
Ad Schellevis's avatar
Ad Schellevis committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

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

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

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

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

32
require_once("guiconfig.inc");
33
require_once("vpn.inc");
34
require_once("services.inc");
35
require_once("interfaces.inc");
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52


function ipsec_fixup_network($network) {
	if (substr($network, -3) == '|/0')
		$result = substr($network, 0, -3);
	else {
		$tmp = explode('|', $network);
		if (isset($tmp[1]))
			$result = $tmp[1];
		else
			$result = $tmp[0];
		unset($tmp);
	}

	return $result;
}

53
if (!isset($config['ipsec']) || !is_array($config['ipsec'])) {
54 55 56
    $config['ipsec'] = array();
}

57
if (!isset($config['ipsec']['phase1'])) {
Ad Schellevis's avatar
Ad Schellevis committed
58
    $config['ipsec']['phase1'] = array();
59
}
Ad Schellevis's avatar
Ad Schellevis committed
60

61 62
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
		// check if post can be valid
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
		if (!empty($_POST['ikeid']) && ctype_digit(str_replace('-','',$_POST['ikeid'])) && !empty($_POST['action'])) {
			// temporary solution to find related IKEv1 connections
			// eventually we need to look at VICI (https://wiki.strongswan.org/projects/strongswan/wiki/VICI)
			$ipsec_connlist = array();
			exec("/usr/local/sbin/ipsec statusall", $ipsec_status_all);
			foreach ($ipsec_status_all as $line ) {
					if (strpos($line, "child:") !== false) {
							$connId = trim(explode(":",$line)[0]);
							if (strpos($line, "-") !== false) {
									// IKEv1 items with more then one phase 2 entry
									if (!isset($ipsec_connlist[explode("-",$connId)[0]])) {
											$ipsec_connlist[trim(explode("-",$connId)[0])] = array();
									}
									$ipsec_connlist[trim(explode("-",$connId)[0])][] = $connId;
							} else {
									$ipsec_connlist[$connId] = array($connId);
							}
					}
			}

83
			$act = $_POST['action'];
84
			$ikeid = 'con'.$_POST['ikeid'];
85 86
			// check if a valid ikesaid is provided
			if (!empty($_POST['ikesaid']) && ctype_digit($_POST['ikesaid'])) {
87
					$ikesaid = $_POST['ikesaid'];
88
			} else {
89
					$ikesaid = null;
90 91 92 93
			}
			// todo: move to configctl calls
			switch ($act) {
				case 'connect':
94 95 96 97 98 99 100 101 102 103
					if (isset($ipsec_connlist[$ikeid])) {
							foreach ($ipsec_connlist[$ikeid] as $client) {
									mwexec("/usr/local/sbin/ipsec down " . $client);
									mwexec("/usr/local/sbin/ipsec up " . $client);
							}
					} else {
							// ipsec statusall should have found this connection
							mwexec("/usr/local/sbin/ipsec down " . $ikeid);
							mwexec("/usr/local/sbin/ipsec up " . $ikeid);
					}
104 105 106
					break;
				case 'ikedisconnect':
					if ($ikesaid !== null) {
107
						mwexec("/usr/local/sbin/ipsec down " . $ikeid . "[" . $ikesaid . "]");
108
					} else {
109
						mwexec("/usr/local/sbin/ipsec down " . $ikeid);
110
					}
111
					break;
112
				case 'childdisconnect':
113
					mwexec("/usr/local/sbin/ipsec down " . $ikeid . "{" . $ikesaid . "}");
114 115 116 117 118
					break;

			}
		}
}
Ad Schellevis's avatar
Ad Schellevis committed
119 120

$status = ipsec_smp_dump_status();
121 122 123 124 125
$pconfig = $config['ipsec']['phase1'];
legacy_html_escape_form_data($pconfig);
legacy_html_escape_form_data($status);
$pgtitle = array(gettext("Status"),gettext("IPsec"));
$shortcut_section = "ipsec";
Ad Schellevis's avatar
Ad Schellevis committed
126

127
include("head.inc");
Ad Schellevis's avatar
Ad Schellevis committed
128
?>
129 130 131 132 133 134 135 136 137
<script type="text/javascript">
//<![CDATA[
	function show_childsa(id, buttonid) {
		document.getElementById(buttonid).innerHTML='';
		aodiv = document.getElementById(id);
		aodiv.style.display = "";
	}
//]]>
</script>
138
<body>
Ad Schellevis's avatar
Ad Schellevis committed
139

140 141
<?php include("fbegin.inc"); ?>
	<section class="page-content-main">
142
		<div class="container-fluid">
143
			<div class="row">
144
				<?php if (isset($input_errors) && count($input_errors) > 0) print_input_errors($input_errors); ?>
145
			    <section class="col-xs-12">
146
<?				$active_tab = "/diag_ipsec.php";
147 148 149
					include('diag_ipsec_tabs.inc');
?>
						<div class="tab-content content-box col-xs-12">
150
							<div class="table-responsive">
151
								<table class="table table-striped">
152
									<thead>
153
									<tr>
154 155 156 157 158 159 160 161
										<th><?= gettext("Description");?></th>
										<th><?= gettext("Local ID");?></th>
										<th><?= gettext("Local IP");?></th>
										<th><?= gettext("Remote ID");?></th>
										<th><?= gettext("Remote IP");?></th>
										<th><?= gettext("Role");?></th>
										<th><?= gettext("Status");?></th>
										<th></th>
162 163 164 165 166
									</tr>
									</thead>
									<tbody>
									<?php
										$ipsecconnected = array();
167 168 169
										if (isset($status['query']['ikesalist']['ikesa'])):
											foreach ($status['query']['ikesalist']['ikesa'] as $ikeid => $ikesa):
												// first do formatting
170
												$con_id = substr($ikesa['peerconfig'], 3);
171
												$ipsecconnected[trim(explode("-",$con_id)[0])] = $con_id;
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
												$ipsec_get_descr = '';
												foreach ($pconfig as $p1) {
														if ($p1['ikeid'] == $con_id) {
																$ipsec_get_descr = $p1['descr'];
																break;
														}
												}
												$ipsec_local_identification = 'Unknown';
												if (!empty($ikesa['local']['identification'])) {
													if ($ikesa['local']['identification'] = "%any" ){
															$ipsec_local_identification = 'Any identifier';
													} else {
															$ipsec_local_identification = $ikesa['local']['identification'];
													}
												}
												$ipsec_local_address = 'Unknown';
												if (!empty($ikesa['local']['address'])) {
														$ipsec_local_address = $ikesa['local']['address'] . '<br/>Port:' . $ikesa['local']['port'];
												}
												if (isset($ikesa['local']['nat']) && $ikesa['local']['nat'] != 'false') {
														$ipsec_local_address .= ' NAT-T';
												}
194

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
												$ipsec_remote_identification = 'Unknown';
												if (!empty($ikesa['remote']['identification'])) {
														if ($ikesa['remote']['identification'] == '%any') {
																$ipsec_remote_identification = 'Any identifier';
														} else {
																$ipsec_remote_identification = $ikesa['remote']['identification'];
														}
												}
												if (!empty($ikesa['remote']['auth'][0]['identity'])) {
													$ipsec_remote_identification = $ikesa['remote']['auth'][0]['identity'] . '<br/>'. $ipsec_remote_identification;
												}

												$ipsec_remote_address = 'Unknown';
												if (!empty($ikesa['remote']['address'])) {
														$ipsec_remote_address = $ikesa['remote']['address'] . '<br/>Port:';
												}
												if (isset($ikesa['remote']['nat']) && $ikesa['remote']['nat'] != 'false') {
														$ipsec_remote_address .= ' NAT-T';
												}

												$connected = false;
216
												if (ipsec_phase1_status($status['query']['ikesalist']['ikesa'], $ikesa['id'])) {
217
													$icon = "glyphicon glyphicon-play text-success";
218
													$connected = true;
219
												} elseif(!isset($config['ipsec']['enable'])) {
220
													$icon = "glyphicon glyphicon-remove text-danger";
221
												} else {
222
													$icon = "glyphicon glyphicon-remove text-warning";
223 224 225
												}
									?>
												<tr>
226 227 228 229 230 231 232 233 234
													<td><?= $ipsec_get_descr;?></td>
													<td><?= $ipsec_local_identification ?></td>
													<td><?= $ipsec_local_address ?> </td>
													<td><?= $ipsec_remote_identification ?> </td>
													<td><?= $ipsec_remote_address ?> </td>
													<td><?= $ikesa['role'];?></td>
													<td>
															<span class="<?= $icon; ?>" title="<?= $ikesa['status']; ?>" alt=""></span>
															<small><?= $ikesa['status'];?></small>
235
													</td>
236 237 238 239
													<td>
														<form method="post">
															<input type="hidden" value="<?=$con_id?>" name="ikeid"/>
															<input type="hidden" value="<?=isset($ikesa['id']) ? $ikesa['id'] :""?>" name="ikesaid" />
240
<?php												if (!$connected): ?>
241 242 243
															<button type="submit" class="btn btn-xs" name="action" value="connect"  title="<?=gettext("Connect VPN");?>">
																<span class="glyphicon glyphicon-play"/>
															</button>
244
<?php												else: ?>
245 246 247
															<button type="submit" class="btn btn-xs" name="action" value="ikedisconnect" title="<?=gettext("Disconnect VPN");?>">
																<span class="glyphicon glyphicon-stop"/>
															</button>
248
<?php												endif; ?>
249
														</form>
250 251
													</td>
												</tr>
252
<?php									if (isset($ikesa['childsalist']) && is_array($ikesa['childsalist']) ): ?>
253
												<tr>
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
													<td colspan="8">
														<div id="btnchildsa-<?=$ikeid;?>">
															<button class="btn btn-xs" type="button" onclick="show_childsa('childsa-<?=$ikeid;?>','btnchildsa-<?=$ikeid;?>');" >
																<i class="fa fa-plus"></i> - <?=gettext("Show child SA entries");?>
															</button>
														</div>
														<table class="table table-condensed" id="childsa-<?=$ikeid;?>" style="display:none">
															<thead>
																<tr>
																	<th> </th>
																	<th><?php echo gettext("Local subnets");?></th>
																	<th><?php echo gettext("Local SPI(s)");?></th>
																	<th><?php echo gettext("Remote subnets");?></th>
																</tr>
															</thead>
															<tbody>
<?php
271 272
														if (is_array($ikesa['childsalist']['childsa'])) {
															foreach ($ikesa['childsalist']['childsa'] as $childsa) {
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
?>
																<tr>
																	<td>
																		<form method="post">
																			<input type="hidden" value="<?=$con_id?>" name="ikeid"/>
																			<input type="hidden" value="<?=$childsa['reqid'];?>" name="ikesaid"/>
																			<button type="submit" class="btn btn-xs" name="action" value="childdisconnect">
																				<span class="glyphicon glyphicon-remove text-default"/>
																			</button>
																		</form>
																	</td>
																	<td>
<?php																if (isset($childsa['local']['networks']['network'])):
																			foreach ($childsa['local']['networks']['network'] as $lnets):
?>
																			<?=htmlspecialchars(ipsec_fixup_network($lnets));?> <br/>
289 290
<?php																endforeach;
																		else:
291 292 293 294 295 296 297 298 299
?>
																			Unknown <br/>
<?php																endif;
?>
																	</td>
																	<td>
<?php																if (isset($childsa['local']['spi'])):
?>
																			Local : <?=htmlspecialchars($childsa['local']['spi']);?>
300
<?php															endif;
301 302 303 304
?>
<?php																if (isset($childsa['remote']['spi'])):
?>
																			<br/>Remote : <?=htmlspecialchars($childsa['remote']['spi']);?>
305
<?php															endif;
306 307 308 309 310 311 312
?>
																	</td>
																	<td>
<?php																if (isset($childsa['remote']['networks']['network'])):
																			foreach ($childsa['remote']['networks']['network'] as $rnets):
?>
																			<?=htmlspecialchars(ipsec_fixup_network($rnets));?> <br/>
313
<?php																endforeach;
314 315 316 317 318 319
																		else:
?>
																			Unknown <br/>
<?php																endif;
?>
																</td>
320 321 322 323 324 325 326
														</tr>
													<?php } } ?>
														<tr style="display:none;"><td></td></tr>
													</tbody>
													</table>
													</td>
												</tr>
327
<?php								endif;
328 329 330 331 332 333 334 335 336 337 338 339 340
											unset($con_id);
												// close outer loop {foreach ($status['query']['ikesalist']['ikesa'] as $ikeid => $ikesa)}
												endforeach;
											endif;

											$rgmap = array();
											foreach ($pconfig as $ph1ent):
												if (isset($ph1ent['remote-gateway'])) {
														$rgmap[$ph1ent['remote-gateway']] = $ph1ent['remote-gateway'];
												}
												if (isset($ipsecconnected[$ph1ent['ikeid']])) {
														continue;
												}
341
												$ph1src = ipsec_get_phase1_src($ph1ent);
342 343
												$ph1dst = ipsec_get_phase1_dst($ph1ent);
												list ($myid_type, $myid_data) = ipsec_find_id($ph1ent, "local");
344
												list ($peerid_type, $peerid_data) = ipsec_find_id($ph1ent, "peer", $rgmap);
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365

?>
												<tr>
													<td><?php echo htmlspecialchars($ph1ent['descr']);?></td>
													<td><?=!empty($myid_data) ? htmlspecialchars($myid_data) : "Unknown"?></td>
													<td><?=!empty($ph1src) ? htmlspecialchars($ph1src) : "Unknown"?></td>
													<td><?=!empty($peerid_data) ? htmlspecialchars($peerid_data) : "Unknown"?></td>
													<td><?=!empty($ph1dst) ? htmlspecialchars($ph1dst) : "Unknown"?></td>
													<td></td>
													<td>
														<span class="glyphicon glyphicon-remove text-warning" title="Disconnected" alt=""></span>
														<small>Disconnected</small>
													</td>
													<td >
														<form method="post">
															<input type="hidden" value="<?=$ph1ent['ikeid']?>" name="ikeid"/>
															<button type="submit" class="btn btn-xs" name="action" value="connect">
																<span class="glyphicon glyphicon-play"/>
															</button>
														</form>
													</td>
366
											</tr>
367
<?php
368
										endforeach;
369 370 371 372 373 374 375 376 377 378 379 380
?>
											<tr>
												<td colspan="8">
													<span class="text-danger">
														<strong><?php echo gettext("Note:");?><br /></strong>
													</span>
													<?php echo gettext("You can configure IPsec");?>
													<a href="vpn_ipsec.php">here</a>.</p>
												</td>
											</tr>
										</tbody>
									</table>
381
						</div>
382
							</div>
383
				</section>
384
					</div>
385 386
				</div>
			</section>
387 388


389 390


391 392
<?php
include("foot.inc"); ?>