(tinc) add standard plugin glue + rc script

This commit is contained in:
Ad Schellevis
2016-11-11 18:24:31 +01:00
parent 034c1621e9
commit 5a4dce5914
11 changed files with 239 additions and 4 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
PLUGIN_NAME= tinc
PLUGIN_VERSION= 0.1
PLUGIN_VERSION= 0.2
PLUGIN_COMMENT= Tinc VPN
PLUGIN_DEPENDS= tinc
PLUGIN_MAINTAINER= ad@opnsense.org
@@ -0,0 +1,105 @@
<?php
/*
Copyright (C) 2016 Deciso B.V.
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.
*/
function if_tinc_syslog()
{
$logfacilities = array();
$logfacilities['tinc'] = array(
'facility' => array('tinc'),
'remote' => 'vpn',
);
return $logfacilities;
}
function if_tinc_services()
{
global $config;
$services = array();
$is_enabled = false;
if (isset($config['OPNsense']['proxy']['general']['enabled']) && $config['OPNsense']['proxy']['general']['enabled'] == 1) {
if (isset($config['OPNsense']['Tinc']['networks']['network'])) {
if (!isset($config['OPNsense']['Tinc']['networks']['network'][0])) {
$networks = array($config['OPNsense']['Tinc']['networks']['network']);
} else {
$networks = $config['OPNsense']['Tinc']['networks']['network'];
}
foreach ($networks as $network) {
if (!empty($network['enabled'])) {
$is_enabled = true;
break;
}
}
}
}
if ($is_enabled) {
$services[] = array(
'description' => gettext('Tinc server'),
'configd' => array(
'restart' => array('tinc restart'),
'start' => array('tinc start'),
'stop' => array('tinc stop'),
),
'name' => 'tincd',
);
}
return $services;
}
function if_tinc_interfaces()
{
global $config;
$interfaces = array();
if (isset($config['OPNsense']['Tinc']['networks']['network'])) {
if (!isset($config['OPNsense']['Tinc']['networks']['network'][0])) {
$networks = array($config['OPNsense']['Tinc']['networks']['network']);
} else {
$networks = $config['OPNsense']['Tinc']['networks']['network'];
}
foreach ($networks as $network) {
if (!empty($network['enabled'])) {
$oic = array('enable' => true);
$oic['if'] = 'tinc';
$oic['descr'] = 'TincVPN';
$oic['type'] = 'none';
$oic['virtual'] = true;
$oic['networks'] = array();
$interfaces['tinc'] = $oic;
break;
}
}
}
return $interfaces;
}
+31
View File
@@ -0,0 +1,31 @@
#!/bin/sh
#
# $FreeBSD$
#
# PROVIDE: tincd
# REQUIRE: SERVERS
# KEYWORD: shutdown
#
. /etc/rc.subr
name=opnsense-tincd
stop_cmd=tincd_stop
start_cmd=tincd_start
# stop tincd
tincd_stop()
{
echo "stopping tincd"
/usr/local/opnsense/scripts/OPNsense/Tinc/tincd.py stop
}
# start tincd
tincd_start()
{
echo "starting tincd"
/usr/local/opnsense/scripts/OPNsense/Tinc/tincd.py start
}
run_rc_command $1
@@ -37,7 +37,6 @@ use \OPNsense\Core\Backend;
*/
class ServiceController extends ApiControllerBase
{
/**
* reconfigure captive portal
*/
@@ -53,4 +52,49 @@ class ServiceController extends ApiControllerBase
return array("status" => "failed");
}
}
/**
* start tinc service (in background)
* @return array
*/
public function startAction()
{
if ($this->request->isPost()) {
$backend = new Backend();
$response = $backend->configdRun("tinc start", true);
return array("response" => $response);
} else {
return array("response" => array());
}
}
/**
* stop tinc service
* @return array
*/
public function stopAction()
{
if ($this->request->isPost()) {
$backend = new Backend();
$response = $backend->configdRun("tinc stop");
return array("response" => $response);
} else {
return array("response" => array());
}
}
/**
* restart tinc service
* @return array
*/
public function restartAction()
{
if ($this->request->isPost()) {
$backend = new Backend();
$response = $backend->configdRun("tinc restart");
return array("response" => $response);
} else {
return array("response" => array());
}
}
}
@@ -0,0 +1,11 @@
<acl>
<!-- unique acl key, must be globally unique for all acl's -->
<page-tinc-config>
<name>WebCfg - VPN: Tinc configuration</name>
<description>Allow access to the 'VPN: Tinc' page.</description>
<patterns>
<pattern>ui/tinc/*</pattern>
<pattern>api/tinc/*</pattern>
</patterns>
</page-tinc-config>
</acl>
@@ -0,0 +1,7 @@
<menu>
<VPN>
<Tinc cssClass="fa fa-lock fa-fw" order="100">
<config VisibleName="Configuration" order="10" url="/ui/tinc/"/>
</Tinc>
</VPN>
</menu>
@@ -74,6 +74,8 @@ POSSIBILITY OF SUCH DAMAGE.
message: data['status'],
draggable: true
});
} else {
ajaxCall(url="/api/tinc/service/restart", sendData={});
}
});
});
@@ -76,11 +76,11 @@ def deploy(config_filename):
tmp = network.privkey()
write_file(tmp['filename'], tmp['content'])
# write if-up file
# write tinc-up file
if_up = list()
if_up.append("#!/bin/sh")
if_up.append("ifconfig %s %s " % (interface_name, pipes.quote(network.get_local_address())))
write_file("%s/if-up" % network.get_basepath(), '\n'.join(if_up) + "\n", 0o700)
write_file("%s/tinc-up" % network.get_basepath(), '\n'.join(if_up) + "\n", 0o700)
# configure and rename new tun device, place all in group "tinc" symlink associated tun device
if interface_name not in interfaces:
@@ -0,0 +1,23 @@
[gen-key]
command:/usr/local/opnsense/scripts/OPNsense/Tinc/generate_keypair.py
parameters:
type:script_output
message:generate new keypair
[stop]
command:/usr/local/etc/rc.d/opnsense-tincd stop
parameters:
type:script
message:stop tincd
[start]
command:/usr/local/etc/rc.d/opnsense-tincd start
parameters:
type:script
message:start tincd
[restart]
command:/usr/local/etc/rc.d/opnsense-tincd restart
parameters:
type:script
message:restart tincd
@@ -1 +1,2 @@
tinc_deploy.xml:/usr/local/etc/tinc_deploy.xml
rc.conf.d:/etc/rc.conf.d/opnsense-tincd
@@ -0,0 +1,11 @@
{% set networks = [] %}
{% for network in helpers.toList('OPNsense.Tinc.networks.network') %}
{% if network.enabled == '1' %}
{% do networks.append(network) %}
{% endif %}
{% endfor %}
{% if networks|length > 0 %}
OPNtincd_enable=YES
{% else %}
OPNtincd_enable=NO
{% endif %}