diag_ipsec.php 14.6 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 32 33 34 35 36

	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.
*/

global $g;

$pgtitle = array(gettext("Status"),gettext("IPsec"));
$shortcut_section = "ipsec";

37
require_once("guiconfig.inc");
38
require_once("vpn.inc");
39
require_once("services.inc");
40
require_once("interfaces.inc");
41
include("head.inc");
Ad Schellevis's avatar
Ad Schellevis committed
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
function ipsec_get_descr($ikeid) {
	global $config;

	if (!isset($config['ipsec']['phase1']) ||
	    !is_array($config['ipsec']['phase1']))
		return "";

	$descr = '';
	$a_phase1 = $config['ipsec']['phase1'];
	foreach ($a_phase1 as $p1) {
		if ($p1['ikeid'] == $ikeid) {
			$descr = $p1['descr'];
			break;
		}
	}
	unset($a_phase1);

	return $descr;
}

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;
}


Ad Schellevis's avatar
Ad Schellevis committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
if ($_GET['act'] == 'connect') {
	if (ctype_digit($_GET['ikeid'])) {
		mwexec("/usr/local/sbin/ipsec down con" . escapeshellarg($_GET['ikeid']));
		mwexec("/usr/local/sbin/ipsec up con" . escapeshellarg($_GET['ikeid']));
	}
} else if ($_GET['act'] == 'ikedisconnect') {
	if (ctype_digit($_GET['ikeid'])) {
		if (!empty($_GET['ikesaid']) && ctype_digit($_GET['ikesaid']))
			mwexec("/usr/local/sbin/ipsec down con" . escapeshellarg($_GET['ikeid']) . "[" . escapeshellarg($_GET['ikesaid']) . "]");
		else
			mwexec("/usr/local/sbin/ipsec down con" . escapeshellarg($_GET['ikeid']));
	}
} else if ($_GET['act'] == 'childdisconnect') {
	if (ctype_digit($_GET['ikeid'])) {
		if (!empty($_GET['ikesaid']) && ctype_digit($_GET['ikesaid']))
			mwexec("/usr/local/sbin/ipsec down con" . escapeshellarg($_GET['ikeid']) . "{" . escapeshellarg($_GET['ikesaid']) . "}");
	}
}

98 99 100 101 102
if (!is_array($config['ipsec'])) {
    $config['ipsec'] = array();
}

if (!is_array($config['ipsec']['phase1'])) {
Ad Schellevis's avatar
Ad Schellevis committed
103
    $config['ipsec']['phase1'] = array();
104
}
Ad Schellevis's avatar
Ad Schellevis committed
105 106 107 108 109 110 111 112

$a_phase1 = &$config['ipsec']['phase1'];

$status = ipsec_smp_dump_status();

?>


113
<body>
Ad Schellevis's avatar
Ad Schellevis committed
114

115
<?php include("fbegin.inc"); ?>
Ad Schellevis's avatar
Ad Schellevis committed
116

117
	<section class="page-content-main">
118
		<div class="container-fluid">
119
			<div class="row">
120

121
				<?php if (isset($input_errors) && count($input_errors) > 0) print_input_errors($input_errors); ?>
122

123
			    <section class="col-xs-12">
124

125
				<? $active_tab = "/diag_ipsec.php"; include('diag_ipsec_tabs.inc'); ?>
126 127 128

					<div class="tab-content content-box col-xs-12">

129
							<div class="table-responsive">
130
								<table class="table table-striped table-sort">
131
									<thead>
132 133 134 135 136 137 138 139
									<tr>
										<th class="listhdrr nowrap"><?php echo gettext("Description");?></th>
										<th class="listhdrr nowrap"><?php echo gettext("Local ID");?></th>
										<th class="listhdrr nowrap"><?php echo gettext("Local IP");?></th>
										<th class="listhdrr nowrap"><?php echo gettext("Remote ID");?></th>
										<th class="listhdrr nowrap"><?php echo gettext("Remote IP");?></th>
										<th class="listhdrr nowrap"><?php echo gettext("Role");?></th>
										<th class="listhdrr nowrap"><?php echo gettext("Status");?></th>
140 141 142 143 144 145 146 147 148
									</tr>
									</thead>
									<tbody>
									<?php
										$ipsecconnected = array();
										if (is_array($status['query']) && is_array($status['query']['ikesalist']) && is_array($status['query']['ikesalist']['ikesa'])) {
											foreach ($status['query']['ikesalist']['ikesa'] as $ikeid => $ikesa) {
												$con_id = substr($ikesa['peerconfig'], 3);
												$ipsecconnected[$con_id] = $con_id;
149

150
												if (ipsec_phase1_status($status['query']['ikesalist']['ikesa'], $ikesa['id'])) {
151
													$icon = "glyphicon glyphicon-play text-success";
152
												} elseif(!isset($config['ipsec']['enable'])) {
153
													$icon = "glyphicon glyphicon-remove text-danger";
154
												} else {
155
													$icon = "glyphicon glyphicon-remove text-warning";
156 157 158
												}
									?>
												<tr>
159
													<td class="listr"><?php echo htmlspecialchars(ipsec_get_descr($con_id));?></td>
160
													<td class="listr">
161 162 163 164 165 166 167 168 169 170 171 172 173
													<?php   if (!is_array($ikesa['local'])) {
																	echo "Unknown";
														} else {
															if (!empty($ikesa['local']['identification'])) {
																if ($ikesa['local']['identification'] == '%any')
																	echo 'Any identifier';
																else
																	echo htmlspecialchars($ikesa['local']['identification']);
															} else {
																echo 'Unknown';
															}
														}
													?>
174 175
													</td>
													<td class="listr">
176 177 178 179 180 181 182 183 184 185 186 187 188
													<?php   if (!is_array($ikesa['local'])) {
															echo "Unknown";
														} else {
															if (!empty($ikesa['local']['address'])) {
																echo htmlspecialchars($ikesa['local']['address']) . '<br/>Port:' . htmlspecialchars($ikesa['local']['port']);
															} else {
																echo 'Unknown';
															}
															if ($ikesa['local']['nat'] != 'false') {
																echo " NAT-T";
															}
														}
													?>
189 190
													</td>
													<td class="listr">
191 192
													<?php   if (!is_array($ikesa['remote'])) {
															echo "Unknown";
193
														}
194 195 196 197 198 199 200 201 202
														else {
															$identity = "";
															if (!empty($ikesa['remote']['identification'])) {
																if ($ikesa['remote']['identification'] == '%any') {
																	$identity = 'Any identifier';
																} else {
																	$identity = htmlspecialchars($ikesa['remote']['identification']);
																}
															}
203

204 205 206 207 208 209 210 211 212 213
															if (is_array($ikesa['remote']['auth']) && !empty($ikesa['remote']['auth'][0]['identity'])) {
																echo htmlspecialchars($ikesa['remote']['auth'][0]['identity']);
																echo "<br/>{$identity}";
															} else {
																if (empty($identity)) {
																	echo "Unknown";
																} else {
																	echo $identity;
																}
															}
214
														}
215
													?>
216 217
													</td>
													<td class="listr">
218 219 220 221 222 223 224 225 226 227 228 229 230
													<?php   if (!is_array($ikesa['remote'])) {
															echo "Unknown";
														} else {
															if (!empty($ikesa['remote']['address'])) {
																echo htmlspecialchars($ikesa['remote']['address']) . '<br/>Port:' . htmlspecialchars($ikesa['remote']['port']);
															} else {
																echo 'Unknown';
															}
															if ($ikesa['remote']['nat'] != 'false') {
																echo " NAT-T";
															}
														}
													?>
231 232 233
													</td>
													<td class="listr">
														<?php echo htmlspecialchars($ikesa['role']);?>
234

235 236
													</td>
													<td class="listr">
237
															<span class="<?php echo $icon; ?>" title="<?php echo $ikesa['status']; ?>" alt=""></span>
238
															<small><?php echo htmlspecialchars($ikesa['status']);?></small>
239 240
													</td>
													<td >
241
													<?php if ($icon != "glyphicon glyphicon-play text-success"): ?>
242 243 244
														<a href="diag_ipsec.php?act=connect&amp;ikeid=<?php echo $con_id; ?>">
														<span class="glyphicon glyphicon-play text-default" alt="Connect VPN" title="Connect VPN"></span>
														</a>
245
													<?php else: ?>
246 247 248 249 250 251
														<a href="diag_ipsec.php?act=ikedisconnect&amp;ikeid=<?php echo $con_id; ?>">
														<span class="glyphicon glyphicon-stop text-default" alt="Disconnect VPN" title="Disconnect VPN"></span>
														</a>
														<a href="diag_ipsec.php?act=ikedisconnect&amp;ikeid=<?php echo $con_id; ?>&amp;ikesaid=<?php echo $ikesa['id']; ?>">
														<span class="glyphicon glyphicon-remove text-default" alt="Disconnect VPN Connection" title="Disconnect VPN Connection" border="0"></span>
														</a>
252 253 254 255 256
													<?php endif; ?>
													</td>
												</tr>
												<?php if (is_array($ikesa['childsalist'])): ?>
												<tr>
257
													<td class="listrborder" colspan="10">
258 259 260
													<div id="btnchildsa-<?=$ikeid;?>">
														<input  type="button" onclick="show_childsa('childsa-<?=$ikeid;?>','btnchildsa-<?=$ikeid;?>');" value="+" /> - Show child SA entries
													</div>
261
													<table class="table table-sort" id="childsa-<?=$ikeid;?>" style="display:none">
262 263
													<thead>
														<tr>
264
															<th> </th>
265 266 267 268 269 270 271 272 273 274 275
															<th class="listhdrr nowrap"><?php echo gettext("Local subnets");?></th>
															<th class="listhdrr nowrap"><?php echo gettext("Local SPI(s)");?></th>
															<th class="listhdrr nowrap"><?php echo gettext("Remote subnets");?></th>
														</tr>
													</thead>
													<tbody>
													<?php
														if (is_array($ikesa['childsalist']['childsa'])) {
															foreach ($ikesa['childsalist']['childsa'] as $childsa) {
													?>
														<tr valign="top">
276 277 278 279 280
															<td>
																<a href="diag_ipsec.php?act=childdisconnect&amp;ikeid=<?php echo $con_id; ?>&amp;ikesaid=<?php echo $childsa['reqid']; ?>">
																<span class="glyphicon glyphicon-remove text-default" alt="Disconnect Child SA" title="Disconnect Child SA"></span>
																</a>
															</td>
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
															<td class="listlr nowrap">
													<?php	if (is_array($childsa['local']) && is_array($childsa['local']['networks']) && is_array($childsa['local']['networks']['network'])) {
															foreach ($childsa['local']['networks']['network'] as $lnets) {
																echo htmlspecialchars(ipsec_fixup_network($lnets)) . "<br />";
															}
														} else
															echo "Unknown";
													?>
															</td>
															<td class="listr nowrap">
													<?php	if (is_array($childsa['local']))
															echo "Local: " . htmlspecialchars($childsa['local']['spi']);
													?>
													<?php	if (is_array($childsa['remote']))
															echo "<br/>Remote: " . htmlspecialchars($childsa['remote']['spi']);
													?>
															</td>
															<td class="listr nowrap">
													<?php	if (is_array($childsa['remote']) && is_array($childsa['remote']['networks']) && is_array($childsa['remote']['networks']['network'])) {
															foreach ($childsa['remote']['networks']['network'] as $rnets) {
																echo htmlspecialchars(ipsec_fixup_network($rnets)) . "<br />";
															}
														} else
															echo "Unknown";
													?>
															</td>
														</tr>
													<?php } } ?>
														<tr style="display:none;"><td></td></tr>
													</tbody>
													</table>
													</td>
												</tr>
314 315
												<?php endif;

316 317 318
												unset($con_id);
											}
										}
319

320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
										$rgmap = array();
										foreach ($a_phase1 as $ph1ent):
											$rgmap[$ph1ent['remote-gateway']] = $ph1ent['remote-gateway'];
											if ($ipsecconnected[$ph1ent['ikeid']])
												continue;
									?>
											<tr>
												<td class="listlr">
													<?php echo htmlspecialchars($ph1ent['descr']);?>
												</td>
												<td class="listr">
											<?php
												list ($myid_type, $myid_data) = ipsec_find_id($ph1ent, "local");
												if (empty($myid_data))
													echo "Unknown";
												else
													echo htmlspecialchars($myid_data);
											?>
												</td>
												<td class="listr">
											<?php
												$ph1src = ipsec_get_phase1_src($ph1ent);
												if (empty($ph1src))
													echo "Unknown";
												else
													echo htmlspecialchars($ph1src);
											?>
												</td>
												<td class="listr">
											<?php
												list ($peerid_type, $peerid_data) = ipsec_find_id($ph1ent, "peer", $rgmap);
												if (empty($peerid_data))
													echo "Unknown";
												else
													echo htmlspecialchars($peerid_data);
											?>
												</td>
												<td class="listr">
											<?php
												$ph1src = ipsec_get_phase1_dst($ph1ent);
												if (empty($ph1src))
													echo "Unknown";
												else
													echo htmlspecialchars($ph1src);
											?>
												</td>
												<td class="listr" >
												</td>
												<td class="listr">
369 370
													<span class="glyphicon glyphicon-remove text-warning" title="Disconnected" alt=""></span>
													<small>Disconnected</small>
371 372
												</td>
												<td >
373 374 375
													<a href="diag_ipsec.php?act=connect&amp;ikeid=<?php echo $ph1ent['ikeid']; ?>">
													<span class="glyphicon glyphicon-play text-default" alt="Connect VPN" title="Connect VPN"></span>
													</a>
376 377 378 379 380 381 382 383 384
												</td>
											</tr>
									<?php
										endforeach;
										unset($ipsecconnected, $phase1, $rgmap);
									?>
												<tr style="display:none;"><td></td></tr>
											</tbody>
											</table>
385

Ad Schellevis's avatar
Ad Schellevis committed
386
											<div class="container-fluid">
387 388
											<p class="vexpl">
	<span class="text-danger">
Ad Schellevis's avatar
Ad Schellevis committed
389 390 391 392 393
		<strong><?php echo gettext("Note:");?><br /></strong>
	</span>
	<?php echo gettext("You can configure IPsec");?>
	<a href="vpn_ipsec.php">here</a>.
</p>
394 395 396
											</div>

				    </div>
397 398 399 400 401
					</div>
			    </section>
			</div>
		</div>
	</section>
402 403


404 405


Ad Schellevis's avatar
Ad Schellevis committed
406 407 408 409 410
<script type="text/javascript">
//<![CDATA[
function show_childsa(id, buttonid) {
	document.getElementById(buttonid).innerHTML='';
	aodiv = document.getElementById(id);
411
	aodiv.style.display = "";
Ad Schellevis's avatar
Ad Schellevis committed
412 413 414
}
//]]>
</script>
415
<?php unset($status); include("foot.inc"); ?>