Backport ipsec_dump_mobile() from 2.3 to make it work witn new strongswan port (without stroke_list.c patch)

This commit is contained in:
Renato Botelho 2016-02-17 20:02:17 -02:00
parent e4449c1977
commit eda5cb845f

View File

@ -550,32 +550,52 @@ function ipsec_dump_sad() {
* Return dump of mobile user list
*/
function ipsec_dump_mobile() {
global $g, $custom_listtags;
global $g;
$_gb = exec("/usr/local/sbin/ipsec stroke leases > {$g['tmp_path']}/strongswan_leases.xml");
$_gb = exec("/usr/local/sbin/ipsec leases 2>/dev/null", $output, $rc);
if (!file_exists("{$g['tmp_path']}/strongswan_leases.xml")) {
if ($rc != 0) {
log_error(gettext("Unable to find IPsec daemon leases file. Could not display mobile user stats!"));
return array();
}
if (filesize("{$g['tmp_path']}/strongswan_leases.xml") == 0) {
return array();
$response = array();
$id = -1;
/* Leases in pool '10.7.200.0/24', usage: 1/254, 1 online */
$lease_regex='/^Leases *in *pool *\'(?P<name>.+)\', *usage: *(?P<usage>\d+)\/(?P<size>\d+), *(?P<online>\d+) *online/';
/* 10.7.200.1 online 'jimp' */
$pool_regex='/\s*(?P<host>[\d\.]+)\s+(?P<status>online|offline)\s+\'(?P<id>.*)\'/';
/* no matching leases found */
$nopool_regex='/no *matching *leases *found/';
$lease=false;
foreach ($output as $line) {
if (preg_match($lease_regex, $line, $matches)) {
$id++;
$response['pool'][$id] = array(
'name' => $matches['name'],
'usage' => $matches['usage'],
'size' => $matches['size'],
'online' => $matches['online'],
);
$lease=true;
} else if ($lease) {
if (preg_match($nopool_regex, $line)) {
$response['pool'][$id]['lease'][] = array();
$lease=false;
} else if (preg_match($pool_regex, $line, $matches)) {
$response['pool'][$id]['lease'][] = array(
'host' => $matches['host'],
'status' => $matches['status'],
'id' => $matches['id']
);
}
}
}
if (!function_exists('parse_xml_config')) {
require_once('xmlparse.inc');
}
$custom_listtags = array('lease', 'pool');
$response = parse_xml_config("{$g['tmp_path']}/strongswan_leases.xml", "leases");
if ($response == -1) {
return array();
}
@unlink("{$g['tmp_path']}/strongswan_leases.xml");
unset($custom_listtags, $_gb);
unset($_gb, $lease, $output, $rc, $id, $lease_regex, $pool_regex,
$nopool_regex);
return $response;
}