Commit 5dcae9cf authored by Ad Schellevis's avatar Ad Schellevis

(legacy, ipsec) fix ipsec diag page, connect to vici, drop smp support, add...

(legacy, ipsec) fix ipsec diag page, connect to vici, drop smp support, add privilege separation using configd
parent 89e806e4
#!/usr/local/bin/python2.7
"""
Copyright (c) 2015 Ad Schellevis
All rights reserved.
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.
--------------------------------------------------------------------------------------
connect ipsec connection
"""
import sys
import os
import subprocess
import ujson
import vici
# parse input parameter
conn_id = None
if len(sys.argv) > 1:
p_conn_id = sys.argv[1].strip()
# validate if SA is active before trying to disconnect, validates input data
# and collect child sa's to bring up.
conns_found = []
s = vici.Session()
for conns in s.list_conns():
for conn in conns:
if conn == p_conn_id:
conns_found.append(conn)
for child in conns[conn]['children']:
if child not in conns_found:
conns_found.append(child)
# setup connection if found
for conn_id in conns_found:
devnull = open(os.devnull, 'w')
subprocess.call(['/usr/local/sbin/ipsec', 'up', conn_id], stdin=devnull, stdout=devnull, stderr=devnull)
#!/usr/local/bin/python2.7
"""
Copyright (c) 2015 Ad Schellevis
All rights reserved.
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.
--------------------------------------------------------------------------------------
disconnect ipsec connection
"""
import sys
import os
import subprocess
import ujson
import vici
# parse input parameter
conn_id = None
if len(sys.argv) > 1:
conn_id = sys.argv[1].strip()
# validate if SA is active before trying to disconnect, validates input data
conn_found = False
s = vici.Session()
for sas in s.list_sas():
for sa in sas:
if sa == conn_id:
conn_found = True
# terminate connection if found
if conn_found:
devnull = open(os.devnull, 'w')
subprocess.call(['/usr/local/sbin/ipsec', 'down', conn_id], stdin=devnull, stdout=devnull, stderr=devnull)
...@@ -39,11 +39,12 @@ for conns in s.list_conns(): ...@@ -39,11 +39,12 @@ for conns in s.list_conns():
for connection_id in conns: for connection_id in conns:
result[connection_id] = dict() result[connection_id] = dict()
result[connection_id]['version'] = conns[connection_id]['version'] result[connection_id]['version'] = conns[connection_id]['version']
result[connection_id]['local_addrs'] = ','.join(conns[connection_id]['local_addrs']) result[connection_id]['local-addrs'] = ','.join(conns[connection_id]['local_addrs'])
result[connection_id]['local-id'] = '' result[connection_id]['local-id'] = ''
result[connection_id]['local-class'] = [] result[connection_id]['local-class'] = []
result[connection_id]['remote-id'] = '' result[connection_id]['remote-id'] = ''
result[connection_id]['remote-class'] = [] result[connection_id]['remote-class'] = []
result[connection_id]['remote-addrs'] = ','.join(conns[connection_id]['remote_addrs'])
result[connection_id]['children'] = conns[connection_id]['children'] result[connection_id]['children'] = conns[connection_id]['children']
result[connection_id]['sas'] = [] result[connection_id]['sas'] = []
......
...@@ -3,3 +3,15 @@ command:/usr/local/opnsense/scripts/ipsec/list_status.py ...@@ -3,3 +3,15 @@ command:/usr/local/opnsense/scripts/ipsec/list_status.py
parameters: parameters:
type:script_output type:script_output
message:IPsec list status message:IPsec list status
[connect]
command:/usr/local/opnsense/scripts/ipsec/connect.py
parameters:%s
type=script
message:IPsec connect %s
[disconnect]
command:/usr/local/opnsense/scripts/ipsec/disconnect.py
parameters:%s
type=script
message:IPsec disconnect %s
...@@ -30,110 +30,50 @@ ...@@ -30,110 +30,50 @@
*/ */
require_once("guiconfig.inc"); require_once("guiconfig.inc");
require_once("vpn.inc");
require_once("services.inc"); require_once("services.inc");
require_once("interfaces.inc");
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;
}
if (!isset($config['ipsec']) || !is_array($config['ipsec'])) {
$config['ipsec'] = array();
}
if (!isset($config['ipsec']['phase1'])) {
$config['ipsec']['phase1'] = array();
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// check if post can be valid if (isset($_POST['action'])) {
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);
}
}
}
$act = $_POST['action']; $act = $_POST['action'];
$ikeid = 'con'.$_POST['ikeid'];
// check if a valid ikesaid is provided
if (!empty($_POST['ikesaid']) && ctype_digit($_POST['ikesaid'])) {
$ikesaid = $_POST['ikesaid'];
} else { } else {
$ikesaid = null; $act = null;
} }
// todo: move to configctl calls
switch ($act) { switch ($act) {
case 'connect': case 'connect':
if (isset($ipsec_connlist[$ikeid])) { if (!empty($_POST['connid'])) {
foreach ($ipsec_connlist[$ikeid] as $client) { configd_run("ipsec connect ".$_POST['connid']);
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);
} }
break; break;
case 'ikedisconnect': case 'disconnect':
if ($ikesaid !== null) { if (!empty($_POST['connid'])) {
mwexec("/usr/local/sbin/ipsec down " . $ikeid . "[" . $ikesaid . "]"); configd_run("ipsec disconnect ".$_POST['connid']);
} else {
mwexec("/usr/local/sbin/ipsec down " . $ikeid);
} }
break; break;
case 'childdisconnect': default:
mwexec("/usr/local/sbin/ipsec down " . $ikeid . "{" . $ikesaid . "}");
break; break;
}
} }
header("Location: diag_ipsec.php");
exit(0);
} }
$status = ipsec_smp_dump_status(); $ipsec_status = json_decode(configd_run("ipsec list_status"), true);
$pconfig = $config['ipsec']['phase1']; if ($ipsec_status == null) {
legacy_html_escape_form_data($pconfig); $ipsec_status = array();
legacy_html_escape_form_data($status); }
$pgtitle = array(gettext('VPN'), gettext('IPsec'), gettext('Status Overview')); $pgtitle = array(gettext('VPN'), gettext('IPsec'), gettext('Status Overview'));
$shortcut_section = 'ipsec'; $shortcut_section = 'ipsec';
include("head.inc"); include("head.inc");
?> ?>
<script type="text/javascript"> <script type="text/javascript">
//<![CDATA[ $( document ).ready(function() {
function show_childsa(id, buttonid) { // show / hide connection details
document.getElementById(buttonid).innerHTML=''; $(".ipsec_info").click(function(event){
aodiv = document.getElementById(id); $("#" + $(this).data('target')).toggleClass('hidden visible');
aodiv.style.display = ""; event.preventDefault();
} });
//]]> });
</script> </script>
<body> <body>
...@@ -143,227 +83,109 @@ include("head.inc"); ...@@ -143,227 +83,109 @@ include("head.inc");
<div class="row"> <div class="row">
<?php if (isset($input_errors) && count($input_errors) > 0) print_input_errors($input_errors); ?> <?php if (isset($input_errors) && count($input_errors) > 0) print_input_errors($input_errors); ?>
<section class="col-xs-12"> <section class="col-xs-12">
<div class="tab-content content-box col-xs-12"> <div class="tab-content content-box">
<div class="table-responsive"> <div class="table-responsive">
<table class="table table-striped"> <table class="table table-striped">
<thead> <thead>
<tr> <tr>
<th><?= gettext("Description");?></th> <th><?= gettext("Connection");?></th>
<th><?= gettext("Local ID");?></th> <th class="hidden-xs hidden-sm"><?= gettext("Version");?></th>
<th><?= gettext("Local IP");?></th> <th class="hidden-xs"><?= gettext("Local ID");?></th>
<th><?= gettext("Remote ID");?></th> <th class="hidden-xs"><?= gettext("Local IP");?></th>
<th class="hidden-xs"><?= gettext("Remote ID");?></th>
<th><?= gettext("Remote IP");?></th> <th><?= gettext("Remote IP");?></th>
<th><?= gettext("Role");?></th> <th class="hidden-xs hidden-sm"><?= gettext("Local Auth");?></th>
<th class="hidden-xs hidden-sm"><?= gettext("Remote Auth");?></th>
<th><?= gettext("Status");?></th> <th><?= gettext("Status");?></th>
<th></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<?php <?php foreach ($ipsec_status as $ipsec_conn_key => $ipsec_conn): ?>
$ipsecconnected = array();
if (isset($status['query']['ikesalist']['ikesa'])):
foreach ($status['query']['ikesalist']['ikesa'] as $ikeid => $ikesa):
// first do formatting
$con_id = substr($ikesa['peerconfig'], 3);
$ipsecconnected[trim(explode("-",$con_id)[0])] = $con_id;
$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';
}
$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;
if (ipsec_phase1_status($status['query']['ikesalist']['ikesa'], $ikesa['id'])) {
$icon = "glyphicon glyphicon-play text-success";
$connected = true;
} elseif(!isset($config['ipsec']['enable'])) {
$icon = "glyphicon glyphicon-remove text-danger";
} else {
$icon = "glyphicon glyphicon-remove text-warning";
}
?>
<tr> <tr>
<td><?= $ipsec_get_descr;?></td> <td><?= $ipsec_conn_key;?></td>
<td><?= $ipsec_local_identification ?></td> <td class="hidden-xs hidden-sm"><?= $ipsec_conn['version'] ?></td>
<td><?= $ipsec_local_address ?> </td> <td class="hidden-xs"><?= $ipsec_conn['local-id'] ?></td>
<td><?= $ipsec_remote_identification ?> </td> <td class="hidden-xs"><?= $ipsec_conn['local-addrs'] ?></td>
<td><?= $ipsec_remote_address ?> </td> <td class="hidden-xs"><?= $ipsec_conn['remote-id'] ?></td>
<td><?= $ikesa['role'];?></td> <td><?= $ipsec_conn['remote-addrs'] ?></td>
<td> <td class="hidden-xs hidden-sm"><?= $ipsec_conn['local-class'] ?></td>
<span class="<?= $icon; ?>" title="<?= $ikesa['status']; ?>" alt=""></span> <td class="hidden-xs hidden-sm"><?= $ipsec_conn['remote-class'] ?></td>
<small><?= $ikesa['status'];?></small>
</td>
<td> <td>
<?php if (count($ipsec_conn['sas'])):
?>
<form method="post"> <form method="post">
<input type="hidden" value="<?=$con_id?>" name="ikeid"/> <input type="hidden" value="<?=$ipsec_conn_key;?>" name="connid"/>
<input type="hidden" value="<?=isset($ikesa['id']) ? $ikesa['id'] :""?>" name="ikesaid" /> <button type="submit" class="btn btn-xs" name="action" value="disconnect">
<?php if (!$connected): ?> <span class="glyphicon glyphicon-remove text-default"/>
<button type="submit" class="btn btn-xs" name="action" value="connect" title="<?=gettext("Connect VPN");?>"> </button>
<span class="glyphicon glyphicon-play"/> <button type="submit" class="btn btn-xs" name="action" value="connect">
<span class="glyphicon glyphicon-play text-success" title="" alt=""></span>
</button> </button>
<?php else: ?> <button type="none" class="btn btn-xs ipsec_info" data-target="info_<?=$ipsec_conn_key?>">
<button type="submit" class="btn btn-xs" name="action" value="ikedisconnect" title="<?=gettext("Disconnect VPN");?>"> <span class="glyphicon glyphicon-info-sign"></i>
<span class="glyphicon glyphicon-stop"/>
</button> </button>
<?php endif; ?>
</form> </form>
<?php else:
?>
<form method="post">
<input type="hidden" value="<?=$ipsec_conn_key;?>" name="connid"/>
<button type="submit" class="btn btn-xs" name="action" value="connect">
<span class="glyphicon glyphicon-play text-warning"/>
</button>
</form>
<?php endif;
?>
</td> </td>
</tr> </tr>
<?php if (isset($ikesa['childsalist']) && is_array($ikesa['childsalist']) ): ?> <?php if (count($ipsec_conn['sas'])):
?>
<tr> <tr>
<td colspan="8"> <td colspan="9" class="hidden" id="info_<?=$ipsec_conn_key?>">
<div id="btnchildsa-<?=$ikeid;?>"> <table class="table table-condensed">
<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> <thead>
<tr> <tr>
<th> </th> <th><?= gettext("Local subnets");?></th>
<th><?php echo gettext("Local subnets");?></th> <th class="hidden-xs hidden-sm"><?= gettext("SPI(s)");?></th>
<th><?php echo gettext("Local SPI(s)");?></th> <th><?= gettext("Remote subnets");?></th>
<th><?php echo gettext("Remote subnets");?></th> <th class="hidden-xs hidden-sm"><?= gettext("State");?></th>
<th><?= gettext("Stats");?></th>
</tr> </tr>
</thead> </head>
<tbody> <tbody>
<?php <?php foreach ($ipsec_conn['sas'] as $sa_key => $sa):?>
if (is_array($ikesa['childsalist']['childsa'])) { <?php foreach ($sa['child-sas'] as $child_sa_key => $child_sa):?>
foreach ($ikesa['childsalist']['childsa'] as $childsa) {
?>
<tr> <tr>
<td> <td>
<form method="post"> <?= implode('<br/>', $child_sa['local-ts'])?>
<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>
<td> <td class="hidden-xs hidden-sm">
<?php if (isset($childsa['local']['networks']['network'])): <?=gettext('in')?> : <?= $child_sa['spi-in'] ?><br/>
foreach ($childsa['local']['networks']['network'] as $lnets): <?=gettext('out')?> : <?= $child_sa['spi-out'] ?>
?>
<?=htmlspecialchars(ipsec_fixup_network($lnets));?> <br/>
<?php endforeach;
else:
?>
Unknown <br/>
<?php endif;
?>
</td> </td>
<td> <td>
<?php if (isset($childsa['local']['spi'])): <?= implode('<br/>', $child_sa['remote-ts'])?>
?> </td>
Local : <?=htmlspecialchars($childsa['local']['spi']);?> <td class="hidden-xs hidden-sm">
<?php endif; <?= $child_sa['state']?>
?>
<?php if (isset($childsa['remote']['spi'])):
?>
<br/>Remote : <?=htmlspecialchars($childsa['remote']['spi']);?>
<?php endif;
?>
</td> </td>
<td> <td>
<?php if (isset($childsa['remote']['networks']['network'])): <small>
foreach ($childsa['remote']['networks']['network'] as $rnets): <?= gettext('Time');?> : <?= $child_sa['install-time']?><br/>
?> <?= gettext('Bytes in');?> : <?= $child_sa['bytes-in']?><br/>
<?=htmlspecialchars(ipsec_fixup_network($rnets));?> <br/> <?= gettext('Bytes out');?> : <?= $child_sa['bytes-out']?>
<?php endforeach; </small>
else:
?>
Unknown <br/>
<?php endif;
?>
</td> </td>
</tr> </tr>
<?php } } ?> <?php endforeach; ?>
<tr style="display:none;"><td></td></tr> <?php endforeach; ?>
</tbody> </tbody>
</table> </table>
</td> </td>
</tr> </tr>
<?php endif; <?php endif;
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;
}
$ph1src = ipsec_get_phase1_src($ph1ent);
$ph1dst = ipsec_get_phase1_dst($ph1ent);
list ($myid_type, $myid_data) = ipsec_find_id($ph1ent, "local");
list ($peerid_type, $peerid_data) = ipsec_find_id($ph1ent, "peer", $rgmap);
?>
<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>
</tr>
<?php
endforeach;
?> ?>
<?php endforeach; ?>
</tbody> </tbody>
</table> </table>
</div> </div>
...@@ -372,9 +194,5 @@ include("head.inc"); ...@@ -372,9 +194,5 @@ include("head.inc");
</div> </div>
</div> </div>
</section> </section>
<?php <?php
include("foot.inc"); ?> include("foot.inc"); ?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment