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.
*/
require_once("guiconfig.inc");
require_once("plugins.inc.d/openvpn.inc");
require_once("services.inc");
require_once("interfaces.inc");
$a_server = &config_read_array('openvpn', 'openvpn-server');
$act = null;
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
// fetch id if provided
if (isset($_GET['dup']) && isset($a_server[$_GET['dup']])) {
$configId = $_GET['dup'];
} elseif (isset($_GET['id']) && is_numericint($_GET['id'])) {
$id = $_GET['id'];
$configId = $id;
}
if (isset($_GET['act'])) {
$act = $_GET['act'];
}
$pconfig = array();
// defaults
$vpnid = 0;
$pconfig['verbosity_level'] = 1;
$pconfig['digest'] = "SHA1"; // OpenVPN Defaults to SHA1 if unset
$pconfig['autokey_enable'] = "yes";
$pconfig['autotls_enable'] = "yes";
if (isset($configId) && isset($a_server[$configId])) {
if ($a_server[$configId]['mode'] != "p2p_shared_key") {
$pconfig['cert_depth'] = 1;
}
// 1 on 1 copy of config attributes
$copy_fields = "mode,protocol,authmode,dev_mode,interface,local_port
,description,custom_options,crypto,engine,tunnel_network
,tunnel_networkv6,remote_network,remote_networkv6,gwredir,local_network
,local_networkv6,maxclients,compression,passtos,client2client
,dynamic_ip,pool_enable,topology_subnet,serverbridge_dhcp
,serverbridge_interface,serverbridge_dhcp_start,serverbridge_dhcp_end
,dns_server1,dns_server2,dns_server3,dns_server4,ntp_server1
,ntp_server2,netbios_enable,netbios_ntype,netbios_scope,wins_server1
,wins_server2,no_tun_ipv6,push_register_dns,dns_domain,local_group
,client_mgmt_port,verbosity_level,caref,crlref,certref,dh_length
,cert_depth,strictusercn,digest,disable,duplicate_cn,vpnid,reneg-sec,use-common-name,cso_login_matching";
foreach (explode(",", $copy_fields) as $fieldname) {
$fieldname = trim($fieldname);
if (isset($a_server[$configId][$fieldname])) {
$pconfig[$fieldname] = $a_server[$configId][$fieldname];
} elseif (!isset($pconfig[$fieldname])) {
// initialize element
$pconfig[$fieldname] = null;
}
}
// load / convert
if (!empty($a_server[$configId]['ipaddr'])) {
$pconfig['interface'] = $pconfig['interface'] . '|' . $a_server[$configId]['ipaddr'];
}
if (!empty($a_server[$configId]['shared_key'])) {
$pconfig['shared_key'] = base64_decode($a_server[$configId]['shared_key']);
} else {
$pconfig['shared_key'] = null;
}
if (!empty($a_server[$configId]['tls'])) {
$pconfig['tlsauth_enable'] = "yes";
$pconfig['tls'] = base64_decode($a_server[$configId]['tls']);
} else {
$pconfig['tls'] = null;
$pconfig['tlsauth_enable'] = null;
}
} elseif ($act == "new") {
$pconfig['tlsauth_enable'] = "yes";
$pconfig['dh_length'] = 2048;
$pconfig['dev_mode'] = "tun";
$pconfig['interface'] = 'any';
$pconfig['protocol'] = 'UDP';
$pconfig['local_port'] = openvpn_port_next($pconfig['protocol']);
$pconfig['pool_enable'] = "yes";
$pconfig['cert_depth'] = 1;
// init all fields used in the form
$init_fields = "mode,protocol,authmode,dev_mode,interface,local_port
,description,custom_options,crypto,engine,tunnel_network
,tunnel_networkv6,remote_network,remote_networkv6,gwredir,local_network
,local_networkv6,maxclients,compression,passtos,client2client
,dynamic_ip,pool_enable,topology_subnet,serverbridge_dhcp
,serverbridge_interface,serverbridge_dhcp_start,serverbridge_dhcp_end
,dns_server1,dns_server2,dns_server3,dns_server4,ntp_server1
,ntp_server2,netbios_enable,netbios_ntype,netbios_scope,wins_server1
,wins_server2,no_tun_ipv6,push_register_dns,dns_domain
,client_mgmt_port,verbosity_level,caref,crlref,certref,dh_length
,cert_depth,strictusercn,digest,disable,duplicate_cn,vpnid,shared_key,tls,reneg-sec,use-common-name
,cso_login_matching";
foreach (explode(",", $init_fields) as $fieldname) {
$fieldname = trim($fieldname);
if (!isset($pconfig[$fieldname])) {
$pconfig[$fieldname] = null;
}
}
}
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['id']) && isset($a_server[$_POST['id']])) {
$id = $_POST['id'];
}
if (isset($_POST['act'])) {
$act = $_POST['act'];
}
if ($act == "del") {
// action delete
if (isset($a_server[$id])) {
openvpn_delete('server', $a_server[$id]);
unset($a_server[$id]);
write_config();
}
header(url_safe('Location: /vpn_openvpn_server.php'));
exit;
} elseif ($act == "toggle") {
if (isset($id)) {
if (isset($a_server[$id]['disable'])) {
unset($a_server[$id]['disable']);
} else {
$a_server[$id]['disable'] = true;
}
write_config();
openvpn_configure_single($a_server[$id]['vpnid']);
}
header(url_safe('Location: /vpn_openvpn_server.php'));
exit;
} else {
// action add/update
$input_errors = array();
$pconfig = $_POST;
if (isset($id) && $a_server[$id]) {
$vpnid = $a_server[$id]['vpnid'];
} else {
$vpnid = 0;
}
if ($pconfig['mode'] != "p2p_shared_key") {
$tls_mode = true;
} else {
$tls_mode = false;
}
if (!empty($pconfig['autokey_enable'])) {
$pconfig['shared_key'] = openvpn_create_key();
}
// all input validators
if (strpos($pconfig['interface'], '|') !== false) {
list($iv_iface, $iv_ip) = explode("|", $pconfig['interface']);
} else {
$iv_iface = $pconfig['interface'];
$iv_ip = null;
}
if (is_ipaddrv4($iv_ip) && (stristr($pconfig['protocol'], "6") !== false)) {
$input_errors[] = gettext("Protocol and IP address families do not match. You cannot select an IPv6 protocol and an IPv4 IP address.");
} elseif (is_ipaddrv6($iv_ip) && (stristr($pconfig['protocol'], "6") === false)) {
$input_errors[] = gettext("Protocol and IP address families do not match. You cannot select an IPv4 protocol and an IPv6 IP address.");
} elseif ((stristr($pconfig['protocol'], "6") === false) && !get_interface_ip($iv_iface) && ($pconfig['interface'] != "any")) {
$input_errors[] = gettext("An IPv4 protocol was selected, but the selected interface has no IPv4 address.");
} elseif ((stristr($pconfig['protocol'], "6") !== false) && !get_interface_ipv6($iv_iface) && ($pconfig['interface'] != "any")) {
$input_errors[] = gettext("An IPv6 protocol was selected, but the selected interface has no IPv6 address.");
}
if (empty($pconfig['authmode']) && (($pconfig['mode'] == "server_user") || ($pconfig['mode'] == "server_tls_user"))) {
$input_errors[] = gettext("You must select a Backend for Authentication if the server mode requires User Auth.");
}
if ($result = openvpn_validate_port($pconfig['local_port'], gettext('Local port'))) {
$input_errors[] = $result;
}
if ($result = openvpn_validate_cidr($pconfig['tunnel_network'], gettext('IPv4 Tunnel Network'), false, 'ipv4')) {
$input_errors[] = $result;
}
if ($result = openvpn_validate_cidr($pconfig['tunnel_networkv6'], gettext('IPv6 Tunnel Network'), false, 'ipv6')) {
$input_errors[] = $result;
}
if ($result = openvpn_validate_cidr($pconfig['remote_network'], gettext('IPv4 Remote Network'), true, 'ipv4')) {
$input_errors[] = $result;
}
if ($result = openvpn_validate_cidr($pconfig['remote_networkv6'], gettext('IPv6 Remote Network'), true, 'ipv6')) {
$input_errors[] = $result;
}
if ($result = openvpn_validate_cidr($pconfig['local_network'], gettext('IPv4 Local Network'), true, 'ipv4')) {
$input_errors[] = $result;
}
if ($result = openvpn_validate_cidr($pconfig['local_networkv6'], gettext('IPv6 Local Network'), true, 'ipv6')) {
$input_errors[] = $result;
}
if (!empty($pconfig['local_port'])) {
$portused = openvpn_port_used($pconfig['protocol'], $pconfig['interface'], $pconfig['local_port'], $vpnid);
if ($portused != $vpnid && $portused != 0) {
$input_errors[] = gettext("The specified 'Local port' is in use. Please select another value");
}
}
if (!$tls_mode && empty($pconfig['autokey_enable'])) {
if (!strstr($pconfig['shared_key'], "-----BEGIN OpenVPN Static key V1-----") ||
!strstr($pconfig['shared_key'], "-----END OpenVPN Static key V1-----")) {
$input_errors[] = gettext("The field 'Shared Key' does not appear to be valid");
}
}
if ($tls_mode && !empty($pconfig['tlsauth_enable']) && empty($pconfig['autotls_enable'])) {
if (!strstr($pconfig['tls'], "-----BEGIN OpenVPN Static key V1-----") ||
!strstr($pconfig['tls'], "-----END OpenVPN Static key V1-----")) {
$input_errors[] = gettext("The field 'TLS Authentication Key' does not appear to be valid");
}
}
if (!empty($pconfig['dns_server1']) && !is_ipaddr(trim($pconfig['dns_server1']))) {
$input_errors[] = gettext("The field 'DNS Server #1' must contain a valid IP address");
}
if (!empty($pconfig['dns_server2']) && !is_ipaddr(trim($pconfig['dns_server2']))) {
$input_errors[] = gettext("The field 'DNS Server #2' must contain a valid IP address");
}
if (!empty($pconfig['dns_server3']) && !is_ipaddr(trim($pconfig['dns_server3']))) {
$input_errors[] = gettext("The field 'DNS Server #3' must contain a valid IP address");
}
if (!empty($pconfig['dns_server4']) && !is_ipaddr(trim($pconfig['dns_server4']))) {
$input_errors[] = gettext("The field 'DNS Server #4' must contain a valid IP address");
}
if (!empty($pconfig['ntp_server1']) && !is_ipaddr(trim($pconfig['ntp_server1']))) {
$input_errors[] = gettext("The field 'NTP Server #1' must contain a valid IP address");
}
if (!empty($pconfig['ntp_server2']) && !is_ipaddr(trim($pconfig['ntp_server2']))) {
$input_errors[] = gettext("The field 'NTP Server #2' must contain a valid IP address");
}
if (!empty($pconfig['ntp_server3']) && !is_ipaddr(trim($pconfig['ntp_server3']))) {
$input_errors[] = gettext("The field 'NTP Server #3' must contain a valid IP address");
}
if (!empty($pconfig['ntp_server4']) && !is_ipaddr(trim($pconfig['ntp_server4']))) {
$input_errors[] = gettext("The field 'NTP Server #4' must contain a valid IP address");
}
if (!empty($pconfig['wins_server_enable'])) {
if (!empty($pconfig['wins_server1']) && !is_ipaddr(trim($pconfig['wins_server1']))) {
$input_errors[] = gettext("The field 'WINS Server #1' must contain a valid IP address");
}
if (!empty($pconfig['wins_server2']) && !is_ipaddr(trim($pconfig['wins_server2']))) {
$input_errors[] = gettext("The field 'WINS Server #2' must contain a valid IP address");
}
}
if (!empty($pconfig['client_mgmt_port_enable'])) {
if ($result = openvpn_validate_port($pconfig['client_mgmt_port'], gettext('Client management port'))) {
$input_errors[] = $result;
}
}
if (!empty($pconfig['maxclients']) && !is_numeric($pconfig['maxclients'])) {
$input_errors[] = gettext("The field 'Concurrent connections' must be numeric.");
}
/* If we are not in shared key mode, then we need the CA/Cert. */
if (isset($pconfig['mode']) && $pconfig['mode'] != "p2p_shared_key") {
$reqdfields = explode(" ", "caref certref");
$reqdfieldsn = array(gettext("Certificate Authority"),gettext("Certificate"));
} elseif (empty($pconfig['autokey_enable'])) {
/* We only need the shared key filled in if we are in shared key mode and autokey is not selected. */
$reqdfields = array('shared_key');
$reqdfieldsn = array(gettext('Shared key'));
}
$reqdfields[] = 'local_port';
$reqdfieldsn[] = gettext('Local port');
if ($pconfig['dev_mode'] != "tap") {
$reqdfields[] = 'tunnel_network,tunnel_networkv6';
$reqdfieldsn[] = gettext('Tunnel Network');
} else {
if ($pconfig['serverbridge_dhcp'] && ($pconfig['tunnel_network'] || $pconfig['tunnel_networkv6'])) {
$input_errors[] = gettext("Using a tunnel network and server bridge settings together is not allowed.");
}
if (($pconfig['serverbridge_dhcp_start'] && !$pconfig['serverbridge_dhcp_end'])
|| (!$pconfig['serverbridge_dhcp_start'] && $pconfig['serverbridge_dhcp_end'])) {
$input_errors[] = gettext("Server Bridge DHCP Start and End must both be empty, or defined.");
}
if (($pconfig['serverbridge_dhcp_start'] && !is_ipaddrv4($pconfig['serverbridge_dhcp_start']))) {
$input_errors[] = gettext("Server Bridge DHCP Start must be an IPv4 address.");
}
if (($pconfig['serverbridge_dhcp_end'] && !is_ipaddrv4($pconfig['serverbridge_dhcp_end']))) {
$input_errors[] = gettext("Server Bridge DHCP End must be an IPv4 address.");
}
if (ip2ulong($pconfig['serverbridge_dhcp_start']) > ip2ulong($pconfig['serverbridge_dhcp_end'])) {
$input_errors[] = gettext("The Server Bridge DHCP range is invalid (start higher than end).");
}
}
if (isset($pconfig['reneg-sec']) && $pconfig['reneg-sec'] != "" && (string)((int)$pconfig['reneg-sec']) != $pconfig['reneg-sec']) {
$input_errors[] = gettext("Renegotiate time should contain a valid number of seconds.");
}
do_input_validation($pconfig, $reqdfields, $reqdfieldsn, $input_errors);
if (count($input_errors) == 0) {
// validation correct, save data
$server = array();
// delete(rename) old interface so a new TUN or TAP interface can be created.
if (isset($id) && $pconfig['dev_mode'] <> $a_server[$id]['dev_mode']) {
openvpn_delete('server', $a_server[$id]);
}
// 1 on 1 copy of config attributes
$copy_fields = "mode,protocol,dev_mode,local_port,description,crypto,digest,engine
,tunnel_network,tunnel_networkv6,remote_network,remote_networkv6
,gwredir,local_network,local_networkv6,maxclients,compression
,passtos,client2client,dynamic_ip,pool_enable,topology_subnet,local_group
,serverbridge_dhcp,serverbridge_interface,serverbridge_dhcp_start
,serverbridge_dhcp_end,dns_domain,dns_server1,dns_server2,dns_server3
,dns_server4,push_register_dns,ntp_server1,ntp_server2,netbios_enable
,netbios_ntype,netbios_scope,no_tun_ipv6,verbosity_level,wins_server1
,wins_server2,client_mgmt_port,strictusercn,reneg-sec,use-common-name,cso_login_matching";
foreach (explode(",", $copy_fields) as $fieldname) {
$fieldname = trim($fieldname);
if (!empty($pconfig[$fieldname]) || $pconfig[$fieldname] == '0') {
$server[$fieldname] = $pconfig[$fieldname];
}
}
// attributes containing some kind of logic
if ($vpnid != 0) {
$server['vpnid'] = $vpnid;
} else {
$server['vpnid'] = openvpn_vpnid_next();
}
if ($pconfig['disable'] == "yes") {
$server['disable'] = true;
}
if (!empty($pconfig['authmode'])) {
$server['authmode'] = implode(",", $pconfig['authmode']);
}
if (strpos($pconfig['interface'], "|") !== false) {
list($server['interface'], $server['ipaddr']) = explode("|", $pconfig['interface']);
} else {
$server['interface'] = $pconfig['interface'];
}
$server['custom_options'] = str_replace("\r\n", "\n", $pconfig['custom_options']);
if ($tls_mode) {
if ($pconfig['tlsauth_enable']) {
if (!empty($pconfig['autotls_enable'])) {
$pconfig['tls'] = openvpn_create_key();
}
$server['tls'] = base64_encode($pconfig['tls']);
}
foreach (array("caref","crlref",
"certref","dh_length","cert_depth") as $cpKey) {
if (isset($pconfig[$cpKey])) {
$server[$cpKey] = $pconfig[$cpKey];
}
}
if (isset($pconfig['mode']) && $pconfig['mode'] == "server_tls_user" && isset($server['strictusercn'])) {
$server['strictusercn'] = $pconfig['strictusercn'];
}
} else {
$server['shared_key'] = base64_encode($pconfig['shared_key']);
}
if (isset($_POST['duplicate_cn']) && $_POST['duplicate_cn'] == "yes") {
$server['duplicate_cn'] = true;
}
// update or add to config
if (isset($id) && $a_server[$id]) {
$a_server[$id] = $server;
} else {
$a_server[] = $server;
}
write_config();
openvpn_configure_single($server['vpnid']);
header(url_safe('Location: /vpn_openvpn_server.php'));
exit;
} elseif (!empty($pconfig['authmode'])) {
$pconfig['authmode'] = implode(",", $pconfig['authmode']);
}
}
}
include("head.inc");
$main_buttons = array();
if (empty($act)) {
$main_buttons[] = array('href' => 'vpn_openvpn_server.php?act=new', 'label' => gettext('Add'));
}
legacy_html_escape_form_data($pconfig);
?>
0) {
print_input_errors($input_errors);
}
if (isset($savemsg)) {
print_info_box($savemsg);
}?>
|
=gettext("Protocol / Port"); ?> |
=gettext("Tunnel Network"); ?> |
=gettext("Description"); ?> |
|
|
">
">
|
=htmlspecialchars($server['protocol']);?> / =htmlspecialchars($server['local_port']);?>
|
= htmlspecialchars($server['tunnel_network']) ?>
= !empty($server['tunnel_networkv6']) && !empty($server['tunnel_network']) ? ',' : '' ?>
= htmlspecialchars($server['tunnel_networkv6']) ?>
|
=htmlspecialchars($server['description']);?>
|
|
|
= gettext('Use a wizard to setup a new server') ?>
|