Commit 330e1577 authored by Ad Schellevis's avatar Ad Schellevis

(legacy) spaces and curly braces in openvpn-client-export.inc

parent d7dfa801
<?php <?php
/* /*
Copyright (C) 2009 Scott Ullrich <sullrich@gmail.com> Copyright (C) 2009 Scott Ullrich <sullrich@gmail.com>
Copyright (C) 2008 Shrew Soft Inc Copyright (C) 2008 Shrew Soft Inc
Copyright (C) 2010 Ermal Luci Copyright (C) 2010 Ermal Luci
Copyright (C) 2003-2004 Manuel Kasper Copyright (C) 2003-2004 Manuel Kasper
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met: modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, 1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer. this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright 2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution. documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE. POSSIBILITY OF SUCH DAMAGE.
*/ */
global $current_openvpn_version, $current_openvpn_version_rev; global $current_openvpn_version, $current_openvpn_version_rev;
$current_openvpn_version = "2.3.6"; $current_openvpn_version = "2.3.6";
$current_openvpn_version_rev = "01"; $current_openvpn_version_rev = "01";
function openvpn_client_export_prefix($srvid, $usrid = null, $crtid = null) { function openvpn_client_export_prefix($srvid, $usrid = null, $crtid = null)
global $config; {
global $config;
// lookup server settings
$settings = $config['openvpn']['openvpn-server'][$srvid]; // lookup server settings
if (empty($settings)) $settings = $config['openvpn']['openvpn-server'][$srvid];
return false; if (empty($settings)) {
if (!empty($settings['disable'])) { return false;
return false; }
} if (!empty($settings['disable'])) {
return false;
$host = empty($config['system']['hostname']) ? "openvpn" : $config['system']['hostname']; }
$prot = ($settings['protocol'] == 'UDP' ? 'udp' : $settings['protocol']);
$port = $settings['local_port']; $host = empty($config['system']['hostname']) ? "openvpn" : $config['system']['hostname'];
$prot = ($settings['protocol'] == 'UDP' ? 'udp' : $settings['protocol']);
$filename_addition = ""; $port = $settings['local_port'];
if ($usrid && is_numeric($usrid)) {
$filename_addition = "-".$config['system']['user'][$usrid]['name']; $filename_addition = "";
} elseif ($crtid && is_numeric($crtid)) { if ($usrid && is_numeric($usrid)) {
$filename_addition = "-" . str_replace(' ', '_', cert_get_cn($config['cert'][$crtid]['crt'])); $filename_addition = "-".$config['system']['user'][$usrid]['name'];
} } elseif ($crtid && is_numeric($crtid)) {
$filename_addition = "-" . str_replace(' ', '_', cert_get_cn($config['cert'][$crtid]['crt']));
return "{$host}-{$prot}-{$port}{$filename_addition}"; }
return "{$host}-{$prot}-{$port}{$filename_addition}";
} }
function openvpn_client_pem_to_pk12($outpath, $outpass, $crtpath, $keypath, $capath = false) { function openvpn_client_pem_to_pk12($outpath, $outpass, $crtpath, $keypath, $capath = false)
$eoutpath = escapeshellarg($outpath); {
$eoutpass = escapeshellarg($outpass); $eoutpath = escapeshellarg($outpath);
$ecrtpath = escapeshellarg($crtpath); $eoutpass = escapeshellarg($outpass);
$ekeypath = escapeshellarg($keypath); $ecrtpath = escapeshellarg($crtpath);
if ($capath) { $ekeypath = escapeshellarg($keypath);
$ecapath = escapeshellarg($capath); if ($capath) {
exec("/usr/local/bin/openssl pkcs12 -export -in {$ecrtpath} -inkey {$ekeypath} -certfile {$ecapath} -out {$eoutpath} -passout pass:{$eoutpass}"); $ecapath = escapeshellarg($capath);
} else exec("/usr/local/bin/openssl pkcs12 -export -in {$ecrtpath} -inkey {$ekeypath} -certfile {$ecapath} -out {$eoutpath} -passout pass:{$eoutpass}");
exec("/usr/local/bin/openssl pkcs12 -export -in {$ecrtpath} -inkey {$ekeypath} -out {$eoutpath} -passout pass:{$eoutpass}"); } else {
exec("/usr/local/bin/openssl pkcs12 -export -in {$ecrtpath} -inkey {$ekeypath} -out {$eoutpath} -passout pass:{$eoutpass}");
unlink($crtpath); }
unlink($keypath);
if ($capath) unlink($crtpath);
unlink($capath); unlink($keypath);
if ($capath) {
unlink($capath);
}
} }
function openvpn_client_export_validate_config($srvid, $usrid, $crtid) function openvpn_client_export_validate_config($srvid, $usrid, $crtid)
{ {
global $config, $input_errors; global $config, $input_errors;
$nokeys = false; $nokeys = false;
// lookup server settings // lookup server settings
$settings = $config['openvpn']['openvpn-server'][$srvid]; $settings = $config['openvpn']['openvpn-server'][$srvid];
if (empty($settings)) { if (empty($settings)) {
$input_errors[] = gettext("Could not locate server configuration."); $input_errors[] = gettext("Could not locate server configuration.");
return false; return false;
} }
if (!empty($settings['disable'])) { if (!empty($settings['disable'])) {
$input_errors[] = gettext("You cannot export for disabled servers."); $input_errors[] = gettext("You cannot export for disabled servers.");
return false; return false;
} }
// lookup server certificate info // lookup server certificate info
$server_cert = lookup_cert($settings['certref']); $server_cert = lookup_cert($settings['certref']);
if (!$server_cert) if (!$server_cert) {
{ $input_errors[] = gettext("Could not locate server certificate.");
$input_errors[] = gettext("Could not locate server certificate."); } else {
} else { $server_ca = ca_chain($server_cert);
$server_ca = ca_chain($server_cert); if (empty($server_ca)) {
if (empty($server_ca)) { $input_errors[] = gettext("Could not locate the CA reference for the server certificate.");
$input_errors[] = gettext("Could not locate the CA reference for the server certificate."); }
} $servercn = cert_get_cn($server_cert['crt']);
$servercn = cert_get_cn($server_cert['crt']); }
}
// lookup user info
// lookup user info if (is_numeric($usrid)) {
if (is_numeric($usrid)) { $user = $config['system']['user'][$usrid];
$user = $config['system']['user'][$usrid]; if (!$user) {
if (!$user) { $input_errors[] = gettext("Could not find user settings.");
$input_errors[] = gettext("Could not find user settings."); }
} }
}
// lookup user certificate info
// lookup user certificate info if ($settings['mode'] == "server_tls_user") {
if ($settings['mode'] == "server_tls_user") { if ($settings['authmode'] == "Local Database") {
if ($settings['authmode'] == "Local Database") { $cert = $user['cert'][$crtid];
$cert = $user['cert'][$crtid]; } else {
} else { $cert = $config['cert'][$crtid];
$cert = $config['cert'][$crtid]; }
} if (!$cert) {
if (!$cert) { $input_errors[] = gettext("Could not find client certificate.");
$input_errors[] = gettext("Could not find client certificate."); } else {
} else { // If $cert is not an array, it's a certref not a cert.
// If $cert is not an array, it's a certref not a cert. if (!is_array($cert)) {
if (!is_array($cert)) $cert = lookup_cert($cert);
$cert = lookup_cert($cert); }
} }
} elseif (($settings['mode'] == "server_tls") || (($settings['mode'] == "server_tls_user") && ($settings['authmode'] != "Local Database"))) { } elseif (($settings['mode'] == "server_tls") || (($settings['mode'] == "server_tls_user") && ($settings['authmode'] != "Local Database"))) {
$cert = $config['cert'][$crtid]; $cert = $config['cert'][$crtid];
if (!$cert) if (!$cert) {
$input_errors[] = gettext("Could not find client certificate."); $input_errors[] = gettext("Could not find client certificate.");
} else }
$nokeys = true; } else {
$nokeys = true;
if ($input_errors) }
return false;
if ($input_errors) {
return array($settings, $server_cert, $server_ca, $servercn, $user, $cert, $nokeys); return false;
}
return array($settings, $server_cert, $server_ca, $servercn, $user, $cert, $nokeys);
} }
function openvpn_client_export_config($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $randomlocalport, $usetoken, $nokeys = false, $proxy, $expformat = "baseconf", $outpass = "", $skiptls=false, $doslines=false, $openvpnmanager, $advancedoptions = "") function openvpn_client_export_config($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $randomlocalport, $usetoken, $nokeys = false, $proxy, $expformat = "baseconf", $outpass = "", $skiptls=false, $doslines=false, $openvpnmanager, $advancedoptions = "")
{ {
global $config, $input_errors; global $config, $input_errors;
$nl = ($doslines) ? "\r\n" : "\n"; $nl = ($doslines) ? "\r\n" : "\n";
$conf = ""; $conf = "";
$validconfig = openvpn_client_export_validate_config($srvid, $usrid, $crtid); $validconfig = openvpn_client_export_validate_config($srvid, $usrid, $crtid);
if (!$validconfig) { if (!$validconfig) {
return false; return false;
} }
list($settings, $server_cert, $server_ca, $servercn, $user, $cert, $nokeys) = $validconfig; list($settings, $server_cert, $server_ca, $servercn, $user, $cert, $nokeys) = $validconfig;
// determine basic variables // determine basic variables
$remotes = openvpn_client_export_build_remote_lines($settings, $useaddr, $interface, $expformat, $nl); $remotes = openvpn_client_export_build_remote_lines($settings, $useaddr, $interface, $expformat, $nl);
$server_port = $settings['local_port']; $server_port = $settings['local_port'];
$cipher = $settings['crypto']; $cipher = $settings['crypto'];
$digest = !empty($settings['digest']) ? $settings['digest'] : "SHA1"; $digest = !empty($settings['digest']) ? $settings['digest'] : "SHA1";
// add basic settings // add basic settings
$devmode = empty($settings['dev_mode']) ? "tun" : $settings['dev_mode']; $devmode = empty($settings['dev_mode']) ? "tun" : $settings['dev_mode'];
if (($expformat != "inlinedroid") && ($expformat != "inlineios")) if (($expformat != "inlinedroid") && ($expformat != "inlineios")) {
$conf .= "dev {$devmode}{$nl}"; $conf .= "dev {$devmode}{$nl}";
if(!empty($settings['tunnel_networkv6']) && ($expformat != "inlinedroid") && ($expformat != "inlineios")) { }
$conf .= "tun-ipv6{$nl}"; if (!empty($settings['tunnel_networkv6']) && ($expformat != "inlinedroid") && ($expformat != "inlineios")) {
} $conf .= "tun-ipv6{$nl}";
$conf .= "persist-tun{$nl}"; }
$conf .= "persist-key{$nl}"; $conf .= "persist-tun{$nl}";
$conf .= "persist-key{$nl}";
// if ((($expformat != "inlinedroid") && ($expformat != "inlineios")) && ($proto == "tcp"))
// $conf .= "proto tcp-client{$nl}"; // if ((($expformat != "inlinedroid") && ($expformat != "inlineios")) && ($proto == "tcp"))
$conf .= "cipher {$cipher}{$nl}"; // $conf .= "proto tcp-client{$nl}";
$conf .= "auth {$digest}{$nl}"; $conf .= "cipher {$cipher}{$nl}";
$conf .= "tls-client{$nl}"; $conf .= "auth {$digest}{$nl}";
$conf .= "client{$nl}"; $conf .= "tls-client{$nl}";
if (($expformat != "inlinedroid") && ($expformat != "inlineios")) $conf .= "client{$nl}";
$conf .= "resolv-retry infinite{$nl}"; if (($expformat != "inlinedroid") && ($expformat != "inlineios")) {
$conf .= "$remotes{$nl}"; $conf .= "resolv-retry infinite{$nl}";
}
/* Use a random local port, otherwise two clients will conflict if they run at the same time. $conf .= "$remotes{$nl}";
May not be supported on older clients (Released before May 2010) */
if (($randomlocalport != 0) && (substr($expformat, 0, 7) != "yealink") && ($expformat != "snom")) /* Use a random local port, otherwise two clients will conflict if they run at the same time.
$conf .= "lport 0{$nl}"; May not be supported on older clients (Released before May 2010) */
if (($randomlocalport != 0) && (substr($expformat, 0, 7) != "yealink") && ($expformat != "snom")) {
/* This line can cause problems with auth-only setups and also with Yealink/Snom phones $conf .= "lport 0{$nl}";
since they are stuck on an older OpenVPN version that does not support this feature. */ }
if (!empty($servercn) && !$nokeys) {
switch ($verifyservercn) { /* This line can cause problems with auth-only setups and also with Yealink/Snom phones
case "none": since they are stuck on an older OpenVPN version that does not support this feature. */
break; if (!empty($servercn) && !$nokeys) {
case "tls-remote": switch ($verifyservercn) {
$conf .= "tls-remote {$servercn}{$nl}"; case "none":
break; break;
case "tls-remote-quote": case "tls-remote":
$conf .= "tls-remote \"{$servercn}\"{$nl}"; $conf .= "tls-remote {$servercn}{$nl}";
break; break;
default: case "tls-remote-quote":
if ((substr($expformat, 0, 7) != "yealink") && ($expformat != "snom")) { $conf .= "tls-remote \"{$servercn}\"{$nl}";
$conf .= "verify-x509-name \"{$servercn}\" name{$nl}"; break;
} default:
} if ((substr($expformat, 0, 7) != "yealink") && ($expformat != "snom")) {
} $conf .= "verify-x509-name \"{$servercn}\" name{$nl}";
}
if (!empty($proxy)) { }
if ($proxy['proxy_type'] == "http") { }
if (strtoupper(substr($settings['protocol'], 0, 3)) == "UDP") { if (!empty($proxy)) {
$input_errors[] = gettext("This server uses UDP protocol and cannot communicate with HTTP proxy."); if ($proxy['proxy_type'] == "http") {
return; if (strtoupper(substr($settings['protocol'], 0, 3)) == "UDP") {
} $input_errors[] = gettext("This server uses UDP protocol and cannot communicate with HTTP proxy.");
$conf .= "http-proxy {$proxy['ip']} {$proxy['port']} "; return;
} }
if ($proxy['proxy_type'] == "socks") $conf .= "http-proxy {$proxy['ip']} {$proxy['port']} ";
$conf .= "socks-proxy {$proxy['ip']} {$proxy['port']} "; }
if ($proxy['proxy_authtype'] != "none") { if ($proxy['proxy_type'] == "socks") {
if (!isset($proxy['passwdfile'])) $conf .= "socks-proxy {$proxy['ip']} {$proxy['port']} ";
$proxy['passwdfile'] = openvpn_client_export_prefix($srvid, $usrid, $crtid) . "-proxy"; }
$conf .= " {$proxy['passwdfile']} {$proxy['proxy_authtype']}"; if ($proxy['proxy_authtype'] != "none") {
} if (!isset($proxy['passwdfile'])) {
$conf .= "{$nl}"; $proxy['passwdfile'] = openvpn_client_export_prefix($srvid, $usrid, $crtid) . "-proxy";
} }
$conf .= " {$proxy['passwdfile']} {$proxy['proxy_authtype']}";
// add user auth settings }
switch($settings['mode']) { $conf .= "{$nl}";
case 'server_user': }
case 'server_tls_user':
$conf .= "auth-user-pass{$nl}"; // add user auth settings
break; switch($settings['mode']) {
} case 'server_user':
case 'server_tls_user':
// add key settings $conf .= "auth-user-pass{$nl}";
$prefix = openvpn_client_export_prefix($srvid, $usrid, $crtid); break;
$cafile = "{$prefix}-ca.crt"; }
if($nokeys == false) {
if ($expformat == "yealink_t28") { // add key settings
$conf .= "ca /yealink/config/openvpn/keys/ca.crt{$nl}"; $prefix = openvpn_client_export_prefix($srvid, $usrid, $crtid);
$conf .= "cert /yealink/config/openvpn/keys/client1.crt{$nl}"; $cafile = "{$prefix}-ca.crt";
$conf .= "key /yealink/config/openvpn/keys/client1.key{$nl}"; if ($nokeys == false) {
} elseif ($expformat == "yealink_t38g") { if ($expformat == "yealink_t28") {
$conf .= "ca /phone/config/openvpn/keys/ca.crt{$nl}"; $conf .= "ca /yealink/config/openvpn/keys/ca.crt{$nl}";
$conf .= "cert /phone/config/openvpn/keys/client1.crt{$nl}"; $conf .= "cert /yealink/config/openvpn/keys/client1.crt{$nl}";
$conf .= "key /phone/config/openvpn/keys/client1.key{$nl}"; $conf .= "key /yealink/config/openvpn/keys/client1.key{$nl}";
} elseif ($expformat == "yealink_t38g2") { } elseif ($expformat == "yealink_t38g") {
$conf .= "ca /config/openvpn/keys/ca.crt{$nl}"; $conf .= "ca /phone/config/openvpn/keys/ca.crt{$nl}";
$conf .= "cert /config/openvpn/keys/client1.crt{$nl}"; $conf .= "cert /phone/config/openvpn/keys/client1.crt{$nl}";
$conf .= "key /config/openvpn/keys/client1.key{$nl}"; $conf .= "key /phone/config/openvpn/keys/client1.key{$nl}";
} elseif ($expformat == "snom") { } elseif ($expformat == "yealink_t38g2") {
$conf .= "ca /openvpn/ca.crt{$nl}"; $conf .= "ca /config/openvpn/keys/ca.crt{$nl}";
$conf .= "cert /openvpn/phone1.crt{$nl}"; $conf .= "cert /config/openvpn/keys/client1.crt{$nl}";
$conf .= "key /openvpn/phone1.key{$nl}"; $conf .= "key /config/openvpn/keys/client1.key{$nl}";
} elseif ($usetoken) { } elseif ($expformat == "snom") {
$conf .= "ca {$cafile}{$nl}"; $conf .= "ca /openvpn/ca.crt{$nl}";
$conf .= "cryptoapicert \"SUBJ:{$user['name']}\"{$nl}"; $conf .= "cert /openvpn/phone1.crt{$nl}";
} elseif (substr($expformat, 0, 6) != "inline") { $conf .= "key /openvpn/phone1.key{$nl}";
$conf .= "pkcs12 {$prefix}.p12{$nl}"; } elseif ($usetoken) {
} $conf .= "ca {$cafile}{$nl}";
} else if ($settings['mode'] == "server_user") { $conf .= "cryptoapicert \"SUBJ:{$user['name']}\"{$nl}";
if (substr($expformat, 0, 6) != "inline") } elseif (substr($expformat, 0, 6) != "inline") {
$conf .= "ca {$cafile}{$nl}"; $conf .= "pkcs12 {$prefix}.p12{$nl}";
} }
} else if ($settings['mode'] == "server_user") {
if ($settings['tls'] && !$skiptls) { if (substr($expformat, 0, 6) != "inline") {
if ($expformat == "yealink_t28") $conf .= "ca {$cafile}{$nl}";
$conf .= "tls-auth /yealink/config/openvpn/keys/ta.key 1{$nl}"; }
elseif ($expformat == "yealink_t38g") }
$conf .= "tls-auth /phone/config/openvpn/keys/ta.key 1{$nl}";
elseif ($expformat == "yealink_t38g2") if ($settings['tls'] && !$skiptls) {
$conf .= "tls-auth /config/openvpn/keys/ta.key 1{$nl}"; if ($expformat == "yealink_t28") {
elseif ($expformat == "snom") $conf .= "tls-auth /yealink/config/openvpn/keys/ta.key 1{$nl}";
$conf .= "tls-auth /openvpn/ta.key 1{$nl}"; } elseif ($expformat == "yealink_t38g") {
elseif (substr($expformat, 0, 6) != "inline") $conf .= "tls-auth /phone/config/openvpn/keys/ta.key 1{$nl}";
$conf .= "tls-auth {$prefix}-tls.key 1{$nl}"; } elseif ($expformat == "yealink_t38g2") {
} $conf .= "tls-auth /config/openvpn/keys/ta.key 1{$nl}";
} elseif ($expformat == "snom") {
// Prevent MITM attacks by verifying the server certificate. $conf .= "tls-auth /openvpn/ta.key 1{$nl}";
// - Disable for now, it requires the server cert to include special options } elseif (substr($expformat, 0, 6) != "inline") {
//$conf .= "remote-cert-tls server{$nl}"; $conf .= "tls-auth {$prefix}-tls.key 1{$nl}";
}
if (is_array($server_cert) && ($server_cert['crt'])) { }
$purpose = cert_get_purpose($server_cert['crt'], true);
if ($purpose['server'] == 'Yes') { // Prevent MITM attacks by verifying the server certificate.
$conf .= "ns-cert-type server{$nl}"; // - Disable for now, it requires the server cert to include special options
} //$conf .= "remote-cert-tls server{$nl}";
}
if (is_array($server_cert) && ($server_cert['crt'])) {
// add optional settings $purpose = cert_get_purpose($server_cert['crt'], true);
if (!empty($settings['compression'])) { if ($purpose['server'] == 'Yes') {
$conf .= "comp-lzo {$settings['compression']}{$nl}"; $conf .= "ns-cert-type server{$nl}";
} }
}
if ($settings['passtos']) {
$conf .= "passtos{$nl}"; // add optional settings
} if (!empty($settings['compression'])) {
$conf .= "comp-lzo {$settings['compression']}{$nl}";
if ($openvpnmanager) { }
if (!empty($settings['client_mgmt_port'])) {
$client_mgmt_port = $settings['client_mgmt_port']; if ($settings['passtos']) {
} else { $conf .= "passtos{$nl}";
$client_mgmt_port = 166; }
}
$conf .= $nl; if ($openvpnmanager) {
$conf .= "# dont terminate service process on wrong password, ask again{$nl}"; if (!empty($settings['client_mgmt_port'])) {
$conf .= "auth-retry interact{$nl}"; $client_mgmt_port = $settings['client_mgmt_port'];
$conf .= "# open management channel{$nl}"; } else {
$conf .= "management 127.0.0.1 {$client_mgmt_port}{$nl}"; $client_mgmt_port = 166;
$conf .= "# wait for management to explicitly start connection{$nl}"; }
$conf .= "management-hold{$nl}"; $conf .= $nl;
$conf .= "# query management channel for user/pass{$nl}"; $conf .= "# dont terminate service process on wrong password, ask again{$nl}";
$conf .= "management-query-passwords{$nl}"; $conf .= "auth-retry interact{$nl}";
$conf .= "# disconnect VPN when management program connection is closed{$nl}"; $conf .= "# open management channel{$nl}";
$conf .= "management-signal{$nl}"; $conf .= "management 127.0.0.1 {$client_mgmt_port}{$nl}";
$conf .= "# forget password when management disconnects{$nl}"; $conf .= "# wait for management to explicitly start connection{$nl}";
$conf .= "management-forget-disconnect{$nl}"; $conf .= "management-hold{$nl}";
$conf .= $nl; $conf .= "# query management channel for user/pass{$nl}";
}; $conf .= "management-query-passwords{$nl}";
$conf .= "# disconnect VPN when management program connection is closed{$nl}";
// add advanced options $conf .= "management-signal{$nl}";
$advancedoptions = str_replace("\r\n", "\n", $advancedoptions); $conf .= "# forget password when management disconnects{$nl}";
$advancedoptions = str_replace("\n", $nl, $advancedoptions); $conf .= "management-forget-disconnect{$nl}";
$advancedoptions = str_replace(";", $nl, $advancedoptions); $conf .= $nl;
$conf .= $advancedoptions; };
$conf .= $nl;
// add advanced options
switch ($expformat) { $advancedoptions = str_replace("\r\n", "\n", $advancedoptions);
case "zip": $advancedoptions = str_replace("\n", $nl, $advancedoptions);
// create template directory $advancedoptions = str_replace(";", $nl, $advancedoptions);
$tempdir = "/tmp/{$prefix}"; $conf .= $advancedoptions;
@mkdir($tempdir, 0700, true); $conf .= $nl;
file_put_contents("{$tempdir}/{$prefix}.ovpn", $conf); switch ($expformat) {
case "zip":
$cafile = "{$tempdir}/{$cafile}"; // create template directory
file_put_contents("{$cafile}", $server_ca); $tempdir = "/tmp/{$prefix}";
if ($settings['tls']) { @mkdir($tempdir, 0700, true);
$tlsfile = "{$tempdir}/{$prefix}-tls.key";
file_put_contents($tlsfile, base64_decode($settings['tls'])); file_put_contents("{$tempdir}/{$prefix}.ovpn", $conf);
}
$cafile = "{$tempdir}/{$cafile}";
// write key files file_put_contents("{$cafile}", $server_ca);
if ($settings['mode'] != "server_user") { if ($settings['tls']) {
$crtfile = "{$tempdir}/{$prefix}-cert.crt"; $tlsfile = "{$tempdir}/{$prefix}-tls.key";
file_put_contents($crtfile, base64_decode($cert['crt'])); file_put_contents($tlsfile, base64_decode($settings['tls']));
$keyfile = "{$tempdir}/{$prefix}.key"; }
file_put_contents($keyfile, base64_decode($cert['prv']));
// write key files
// convert to pkcs12 format if ($settings['mode'] != "server_user") {
$p12file = "{$tempdir}/{$prefix}.p12"; $crtfile = "{$tempdir}/{$prefix}-cert.crt";
if ($usetoken) file_put_contents($crtfile, base64_decode($cert['crt']));
openvpn_client_pem_to_pk12($p12file, $outpass, $crtfile, $keyfile); $keyfile = "{$tempdir}/{$prefix}.key";
else file_put_contents($keyfile, base64_decode($cert['prv']));
openvpn_client_pem_to_pk12($p12file, $outpass, $crtfile, $keyfile, $cafile);
} // convert to pkcs12 format
$command = "cd " . escapeshellarg("{$tempdir}/..") $p12file = "{$tempdir}/{$prefix}.p12";
. " && /usr/local/bin/zip -r " if ($usetoken) {
. escapeshellarg("/tmp/{$prefix}-config.zip") openvpn_client_pem_to_pk12($p12file, $outpass, $crtfile, $keyfile);
. " " . escapeshellarg($prefix); } else {
exec($command); openvpn_client_pem_to_pk12($p12file, $outpass, $crtfile, $keyfile, $cafile);
// Remove temporary directory }
exec("rm -rf " . escapeshellarg($tempdir)); }
return "/tmp/{$prefix}-config.zip"; $command = "cd " . escapeshellarg("{$tempdir}/..")
break; . " && /usr/local/bin/zip -r "
case "inline": . escapeshellarg("/tmp/{$prefix}-config.zip")
case "inlinedroid": . " " . escapeshellarg($prefix);
case "inlineios": exec($command);
// Inline CA // Remove temporary directory
$conf .= "<ca>{$nl}" . trim($server_ca) . "{$nl}</ca>{$nl}"; exec("rm -rf " . escapeshellarg($tempdir));
if ($settings['mode'] != "server_user") { return "/tmp/{$prefix}-config.zip";
// Inline Cert break;
$conf .= "<cert>{$nl}" . trim(base64_decode($cert['crt'])) . "{$nl}</cert>{$nl}"; case "inline":
// Inline Key case "inlinedroid":
$conf .= "<key>{$nl}" . trim(base64_decode($cert['prv'])) . "{$nl}</key>{$nl}"; case "inlineios":
} else { // Inline CA
// Work around OpenVPN Connect assuming you have a client cert even when you don't need one $conf .= "<ca>{$nl}" . trim($server_ca) . "{$nl}</ca>{$nl}";
$conf .= "setenv CLIENT_CERT 0{$nl}"; if ($settings['mode'] != "server_user") {
} // Inline Cert
// Inline TLS $conf .= "<cert>{$nl}" . trim(base64_decode($cert['crt'])) . "{$nl}</cert>{$nl}";
if ($settings['tls']) { // Inline Key
$conf .= "<tls-auth>{$nl}" . trim(base64_decode($settings['tls'])) . "{$nl}</tls-auth>{$nl} key-direction 1{$nl}"; $conf .= "<key>{$nl}" . trim(base64_decode($cert['prv'])) . "{$nl}</key>{$nl}";
} } else {
return $conf; // Work around OpenVPN Connect assuming you have a client cert even when you don't need one
break; $conf .= "setenv CLIENT_CERT 0{$nl}";
case "yealink_t28": }
case "yealink_t38g": // Inline TLS
case "yealink_t38g2": if ($settings['tls']) {
// create template directory $conf .= "<tls-auth>{$nl}" . trim(base64_decode($settings['tls'])) . "{$nl}</tls-auth>{$nl} key-direction 1{$nl}";
$tempdir = "/tmp/{$prefix}"; }
$keydir = "{$tempdir}/keys"; return $conf;
mkdir($tempdir, 0700, true); break;
mkdir($keydir, 0700, true); case "yealink_t28":
case "yealink_t38g":
file_put_contents("{$tempdir}/vpn.cnf", $conf); case "yealink_t38g2":
// create template directory
$cafile = "{$keydir}/ca.crt"; $tempdir = "/tmp/{$prefix}";
file_put_contents("{$cafile}", $server_ca); $keydir = "{$tempdir}/keys";
if ($settings['tls']) { mkdir($tempdir, 0700, true);
$tlsfile = "{$keydir}/ta.key"; mkdir($keydir, 0700, true);
file_put_contents($tlsfile, base64_decode($settings['tls']));
} file_put_contents("{$tempdir}/vpn.cnf", $conf);
// write key files $cafile = "{$keydir}/ca.crt";
if ($settings['mode'] != "server_user") { file_put_contents("{$cafile}", $server_ca);
$crtfile = "{$keydir}/client1.crt"; if ($settings['tls']) {
file_put_contents($crtfile, base64_decode($cert['crt'])); $tlsfile = "{$keydir}/ta.key";
$keyfile = "{$keydir}/client1.key"; file_put_contents($tlsfile, base64_decode($settings['tls']));
file_put_contents($keyfile, base64_decode($cert['prv'])); }
}
exec("tar -C {$tempdir} -cf /tmp/client.tar ./keys ./vpn.cnf"); // write key files
// Remove temporary directory if ($settings['mode'] != "server_user") {
exec("rm -rf {$tempdir}"); $crtfile = "{$keydir}/client1.crt";
return '/tmp/client.tar'; file_put_contents($crtfile, base64_decode($cert['crt']));
case "snom": $keyfile = "{$keydir}/client1.key";
// create template directory file_put_contents($keyfile, base64_decode($cert['prv']));
$tempdir = "/tmp/{$prefix}"; }
mkdir($tempdir, 0700, true); exec("tar -C {$tempdir} -cf /tmp/client.tar ./keys ./vpn.cnf");
// Remove temporary directory
file_put_contents("{$tempdir}/vpn.cnf", $conf); exec("rm -rf {$tempdir}");
return '/tmp/client.tar';
$cafile = "{$tempdir}/ca.crt"; case "snom":
file_put_contents("{$cafile}", $server_ca); // create template directory
if ($settings['tls']) { $tempdir = "/tmp/{$prefix}";
$tlsfile = "{$tempdir}/ta.key"; mkdir($tempdir, 0700, true);
file_put_contents($tlsfile, base64_decode($settings['tls']));
} file_put_contents("{$tempdir}/vpn.cnf", $conf);
// write key files $cafile = "{$tempdir}/ca.crt";
if ($settings['mode'] != "server_user") { file_put_contents("{$cafile}", $server_ca);
$crtfile = "{$tempdir}/phone1.crt"; if ($settings['tls']) {
file_put_contents($crtfile, base64_decode($cert['crt'])); $tlsfile = "{$tempdir}/ta.key";
$keyfile = "{$tempdir}/phone1.key"; file_put_contents($tlsfile, base64_decode($settings['tls']));
file_put_contents($keyfile, base64_decode($cert['prv'])); }
}
exec("cd {$tempdir}/ && tar -cf /tmp/vpnclient.tar *"); // write key files
// Remove temporary directory if ($settings['mode'] != "server_user") {
exec("rm -rf {$tempdir}"); $crtfile = "{$tempdir}/phone1.crt";
return '/tmp/vpnclient.tar'; file_put_contents($crtfile, base64_decode($cert['crt']));
default: $keyfile = "{$tempdir}/phone1.key";
return $conf; file_put_contents($keyfile, base64_decode($cert['prv']));
} }
exec("cd {$tempdir}/ && tar -cf /tmp/vpnclient.tar *");
// Remove temporary directory
exec("rm -rf {$tempdir}");
return '/tmp/vpnclient.tar';
default:
return $conf;
}
} }
function openvpn_client_export_installer($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $randomlocalport, $usetoken, $outpass, $proxy, $openvpnmanager, $advancedoptions, $openvpn_version = "x86-xp") function openvpn_client_export_installer($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $randomlocalport, $usetoken, $outpass, $proxy, $openvpnmanager, $advancedoptions, $openvpn_version = "x86-xp")
{ {
global $config, $input_errors, $current_openvpn_version, $current_openvpn_version_rev; global $config, $input_errors, $current_openvpn_version, $current_openvpn_version_rev;
$uname_p = trim(exec("uname -p")); $uname_p = trim(exec("uname -p"));
switch ($openvpn_version) { switch ($openvpn_version) {
case "x86-xp": case "x86-xp":
$client_install_exe = "openvpn-install-{$current_openvpn_version}-I0{$current_openvpn_version_rev}-i686.exe"; $client_install_exe = "openvpn-install-{$current_openvpn_version}-I0{$current_openvpn_version_rev}-i686.exe";
break; break;
case "x64-xp": case "x64-xp":
$client_install_exe = "openvpn-install-{$current_openvpn_version}-I0{$current_openvpn_version_rev}-x86_64.exe"; $client_install_exe = "openvpn-install-{$current_openvpn_version}-I0{$current_openvpn_version_rev}-x86_64.exe";
break; break;
case "x86-win6": case "x86-win6":
$client_install_exe = "openvpn-install-{$current_openvpn_version}-I6{$current_openvpn_version_rev}-i686.exe"; $client_install_exe = "openvpn-install-{$current_openvpn_version}-I6{$current_openvpn_version_rev}-i686.exe";
break; break;
case "x64-win6": case "x64-win6":
$client_install_exe = "openvpn-install-{$current_openvpn_version}-I6{$current_openvpn_version_rev}-x86_64.exe"; $client_install_exe = "openvpn-install-{$current_openvpn_version}-I6{$current_openvpn_version_rev}-x86_64.exe";
break; break;
default: default:
$client_install_exe = "openvpn-install-{$current_openvpn_version}-I0{$current_openvpn_version_rev}-i686.exe"; $client_install_exe = "openvpn-install-{$current_openvpn_version}-I0{$current_openvpn_version_rev}-i686.exe";
} }
$ovpndir = "/usr/local/opnsense/contrib/openvpn"; $ovpndir = "/usr/local/opnsense/contrib/openvpn";
$workdir = "{$ovpndir}/client-export"; $workdir = "{$ovpndir}/client-export";
$validconfig = openvpn_client_export_validate_config($srvid, $usrid, $crtid); $validconfig = openvpn_client_export_validate_config($srvid, $usrid, $crtid);
if (!$validconfig) { if (!$validconfig) {
return false; return false;
} }
list($settings, $server_cert, $server_ca, $servercn, $user, $cert, $nokeys) = $validconfig; list($settings, $server_cert, $server_ca, $servercn, $user, $cert, $nokeys) = $validconfig;
// create template directory // create template directory
$tempdir = "/tmp//openvpn-export-".uniqid(); $tempdir = "/tmp//openvpn-export-".uniqid();
mkdir($tempdir, 0700, true); mkdir($tempdir, 0700, true);
// create config directory // create config directory
$confdir = "{$tempdir}/config"; $confdir = "{$tempdir}/config";
if (!is_dir($conf_dir)) if (!is_dir($conf_dir)) {
mkdir($confdir, 0700, true); mkdir($confdir, 0700, true);
}
// copy the template directory
exec("cp -r {$workdir}/template/* {$tempdir}"); // copy the template directory
// and put the required installer exe in place exec("cp -r {$workdir}/template/* {$tempdir}");
exec("/bin/cp {$tempdir}/{$client_install_exe} {$tempdir}/openvpn-install.exe"); // and put the required installer exe in place
if (stristr($openvpn_version, "x64")) exec("/bin/cp {$tempdir}/{$client_install_exe} {$tempdir}/openvpn-install.exe");
rename("{$tempdir}/openvpn-postinstall64.exe", "{$tempdir}/openvpn-postinstall.exe"); if (stristr($openvpn_version, "x64")) {
rename("{$tempdir}/openvpn-postinstall64.exe", "{$tempdir}/openvpn-postinstall.exe");
// write configuration file }
$prefix = openvpn_client_export_prefix($srvid, $usrid, $crtid);
$cfgfile = "{$confdir}/{$prefix}-config.ovpn"; // write configuration file
if (!empty($proxy) && $proxy['proxy_authtype'] != "none") { $prefix = openvpn_client_export_prefix($srvid, $usrid, $crtid);
$proxy['passwdfile'] = "{$prefix}-password"; $cfgfile = "{$confdir}/{$prefix}-config.ovpn";
$pwdfle = "{$proxy['user']}\r\n"; if (!empty($proxy) && $proxy['proxy_authtype'] != "none") {
$pwdfle .= "{$proxy['password']}\r\n"; $proxy['passwdfile'] = "{$prefix}-password";
file_put_contents("{$confdir}/{$proxy['passwdfile']}", $pwdfle); $pwdfle = "{$proxy['user']}\r\n";
} $pwdfle .= "{$proxy['password']}\r\n";
$conf = openvpn_client_export_config($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $randomlocalport, $usetoken, $nokeys, $proxy, "", "baseconf", false, true, $openvpnmanager, $advancedoptions); file_put_contents("{$confdir}/{$proxy['passwdfile']}", $pwdfle);
if (!$conf) { }
$input_errors[] = gettext("Could not create a config to export."); $conf = openvpn_client_export_config($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $randomlocalport, $usetoken, $nokeys, $proxy, "", "baseconf", false, true, $openvpnmanager, $advancedoptions);
return false; if (!$conf) {
} $input_errors[] = gettext("Could not create a config to export.");
return false;
file_put_contents($cfgfile, $conf); }
$cafile = "{$tempdir}/config/{$prefix}-ca.crt"; file_put_contents($cfgfile, $conf);
file_put_contents($cafile, $server_ca);
if ($settings['tls']) { $cafile = "{$tempdir}/config/{$prefix}-ca.crt";
$tlsfile = "{$tempdir}/config/{$prefix}-tls.key"; file_put_contents($cafile, $server_ca);
file_put_contents($tlsfile, base64_decode($settings['tls'])); if ($settings['tls']) {
} $tlsfile = "{$tempdir}/config/{$prefix}-tls.key";
file_put_contents($tlsfile, base64_decode($settings['tls']));
// write key files }
if ($settings['mode'] != "server_user") {
$crtfile = "{$tempdir}/config/{$prefix}-{$user['name']}.crt"; // write key files
file_put_contents($crtfile, base64_decode($cert['crt'])); if ($settings['mode'] != "server_user") {
$keyfile = "{$tempdir}/config/{$prefix}-{$user['name']}.key"; $crtfile = "{$tempdir}/config/{$prefix}-{$user['name']}.crt";
file_put_contents($keyfile, base64_decode($cert['prv'])); file_put_contents($crtfile, base64_decode($cert['crt']));
// convert to pkcs12 format $keyfile = "{$tempdir}/config/{$prefix}-{$user['name']}.key";
$p12file = "{$tempdir}/config/{$prefix}.p12"; file_put_contents($keyfile, base64_decode($cert['prv']));
if ($usetoken) // convert to pkcs12 format
openvpn_client_pem_to_pk12($p12file, $outpass, $crtfile, $keyfile); $p12file = "{$tempdir}/config/{$prefix}.p12";
else if ($usetoken) {
openvpn_client_pem_to_pk12($p12file, $outpass, $crtfile, $keyfile, $cafile); openvpn_client_pem_to_pk12($p12file, $outpass, $crtfile, $keyfile);
} } else {
openvpn_client_pem_to_pk12($p12file, $outpass, $crtfile, $keyfile, $cafile);
// 7zip the configuration data }
chdir($tempdir); }
$files = "config ";
if ($openvpnmanager) // 7zip the configuration data
$files .= "openvpnmanager "; chdir($tempdir);
$files = "config ";
$files .= "openvpn-install.exe "; if ($openvpnmanager) {
$files .= "openvpn-postinstall.exe "; $files .= "openvpnmanager ";
if ($usetoken) }
$procchain = ';!@Install@!UTF-8!
$files .= "openvpn-install.exe ";
$files .= "openvpn-postinstall.exe ";
if ($usetoken) {
$procchain = ';!@Install@!UTF-8!
RunProgram="openvpn-postinstall.exe /Import" RunProgram="openvpn-postinstall.exe /Import"
;!@InstallEnd@!' ;!@InstallEnd@!'
; ;
else } else {
$procchain = ';!@Install@!UTF-8! $procchain = ';!@Install@!UTF-8!
RunProgram="openvpn-postinstall.exe" RunProgram="openvpn-postinstall.exe"
;!@InstallEnd@!' ;!@InstallEnd@!';
; }
file_put_contents("{$tempdir}/7zipConfig",$procchain); file_put_contents("{$tempdir}/7zipConfig",$procchain);
if(file_exists("/usr/pbi/p7zip-{$uname_p}/bin/7z")) if (file_exists("/usr/pbi/p7zip-{$uname_p}/bin/7z")) {
exec("/usr/pbi/p7zip-{$uname_p}/bin/7z -y a archive.7z {$files}"); exec("/usr/pbi/p7zip-{$uname_p}/bin/7z -y a archive.7z {$files}");
else } else {
exec("/usr/local/libexec/p7zip/7z -y a archive.7z {$files}"); exec("/usr/local/libexec/p7zip/7z -y a archive.7z {$files}");
}
// create the final installer // create the final installer
$outfile = "{$tempdir}-install.exe"; $outfile = "{$tempdir}-install.exe";
chdir('/tmp'); chdir('/tmp');
exec("/bin/cat {$tempdir}/7zS.sfx {$tempdir}/7zipConfig {$tempdir}/archive.7z > {$outfile}"); exec("/bin/cat {$tempdir}/7zS.sfx {$tempdir}/7zipConfig {$tempdir}/archive.7z > {$outfile}");
// cleanup // cleanup
exec("/bin/rm -r {$tempdir}"); exec("/bin/rm -r {$tempdir}");
return $outfile; return $outfile;
} }
function viscosity_openvpn_client_config_exporter($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $randomlocalport, $usetoken, $outpass, $proxy, $openvpnmanager, $advancedoptions) function viscosity_openvpn_client_config_exporter($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $randomlocalport, $usetoken, $outpass, $proxy, $openvpnmanager, $advancedoptions)
{ {
global $config; global $config;
$uname_p = trim(exec("uname -p")); $uname_p = trim(exec("uname -p"));
$ovpndir = "/usr/local/opnsense/contrib/openvpn/"; $ovpndir = "/usr/local/opnsense/contrib/openvpn/";
$uniq = uniqid(); $uniq = uniqid();
$tempdir = "/tmp/openvpn-export-{$uniq}"; $tempdir = "/tmp/openvpn-export-{$uniq}";
$zipfile = "/tmp/{$uniq}-Viscosity.visc.zip"; $zipfile = "/tmp/{$uniq}-Viscosity.visc.zip";
$validconfig = openvpn_client_export_validate_config($srvid, $usrid, $crtid); $validconfig = openvpn_client_export_validate_config($srvid, $usrid, $crtid);
if (!$validconfig) { if (!$validconfig) {
return false; return false;
} }
list($settings, $server_cert, $server_ca, $servercn, $user, $cert, $nokeys) = $validconfig; list($settings, $server_cert, $server_ca, $servercn, $user, $cert, $nokeys) = $validconfig;
// create template directory // create template directory
mkdir($tempdir, 0700, true); mkdir($tempdir, 0700, true);
mkdir($tempdir . "/Viscosity.visc", 0700, true); mkdir($tempdir . "/Viscosity.visc", 0700, true);
// Append new Viscosity.visc directory on top // Append new Viscosity.visc directory on top
$tempdir = $tempdir . "/Viscosity.visc/"; $tempdir = $tempdir . "/Viscosity.visc/";
// write cofiguration file // write cofiguration file
$prefix = openvpn_client_export_prefix($srvid, $usrid, $crtid); $prefix = openvpn_client_export_prefix($srvid, $usrid, $crtid);
if (!empty($proxy) && $proxy['proxy_authtype'] != "none") { if (!empty($proxy) && $proxy['proxy_authtype'] != "none") {
$proxy['passwdfile'] = "config-password"; $proxy['passwdfile'] = "config-password";
$pwdfle = "{$proxy['user']}\n"; $pwdfle = "{$proxy['user']}\n";
$pwdfle .= "{$proxy['password']}\n"; $pwdfle .= "{$proxy['password']}\n";
file_put_contents("{$tempdir}/{$proxy['passwdfile']}", $pwdfle); file_put_contents("{$tempdir}/{$proxy['passwdfile']}", $pwdfle);
} }
$conf = openvpn_client_export_config($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $randomlocalport, $usetoken, true, $proxy, "baseconf", $outpass, true, true, $openvpnmanager, $advancedoptions); $conf = openvpn_client_export_config($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $randomlocalport, $usetoken, true, $proxy, "baseconf", $outpass, true, true, $openvpnmanager, $advancedoptions);
if (!$conf) if (!$conf) {
return false; return false;
}
// We need to nuke the ca line from the above config if it exists.
$conf = explode("\n", $conf); // We need to nuke the ca line from the above config if it exists.
for ($i=0; $i < count($conf); $i++) { $conf = explode("\n", $conf);
if ((substr($conf[$i], 0, 3) == "ca ") || (substr($conf[$i], 0, 7) == "pkcs12 ")) for ($i=0; $i < count($conf); $i++) {
unset($conf[$i]); if ((substr($conf[$i], 0, 3) == "ca ") || (substr($conf[$i], 0, 7) == "pkcs12 ")) {
} unset($conf[$i]);
$conf = implode("\n", $conf); }
}
$friendly_name = $settings['description']; $conf = implode("\n", $conf);
$visc_settings = <<<EOF
$friendly_name = $settings['description'];
$visc_settings = <<<EOF
#-- Config Auto Generated for Viscosity --# #-- Config Auto Generated for Viscosity --#
#viscosity startonopen false #viscosity startonopen false
...@@ -619,308 +641,322 @@ function viscosity_openvpn_client_config_exporter($srvid, $usrid, $crtid, $usead ...@@ -619,308 +641,322 @@ function viscosity_openvpn_client_config_exporter($srvid, $usrid, $crtid, $usead
EOF; EOF;
$configfile = "{$tempdir}/config.conf"; $configfile = "{$tempdir}/config.conf";
$conf .= "ca ca.crt\n"; $conf .= "ca ca.crt\n";
$conf .= "tls-auth ta.key 1\n"; $conf .= "tls-auth ta.key 1\n";
if ($settings['mode'] != "server_user") { if ($settings['mode'] != "server_user") {
$conf .= <<<EOF $conf .= <<<EOF
cert cert.crt cert cert.crt
key key.key key key.key
EOF; EOF;
} }
file_put_contents($configfile, $visc_settings . "\n" . $conf); file_put_contents($configfile, $visc_settings . "\n" . $conf);
// ca.crt cert.crt config.conf key.key ta.key // ca.crt cert.crt config.conf key.key ta.key
// write ca // write ca
$cafile = "{$tempdir}/ca.crt"; $cafile = "{$tempdir}/ca.crt";
file_put_contents($cafile, $server_ca); file_put_contents($cafile, $server_ca);
if ($settings['mode'] != "server_user") { if ($settings['mode'] != "server_user") {
// write user .crt
// write user .crt $crtfile = "{$tempdir}/cert.crt";
$crtfile = "{$tempdir}/cert.crt"; file_put_contents($crtfile, base64_decode($cert['crt']));
file_put_contents($crtfile, base64_decode($cert['crt']));
// write user .key
// write user .key if (!empty($outpass)) {
if (!empty($outpass)) { $keyfile = "{$tempdir}/key.key";
$keyfile = "{$tempdir}/key.key"; $clearkeyfile = "{$tempdir}/key-clear.key";
$clearkeyfile = "{$tempdir}/key-clear.key"; file_put_contents($clearkeyfile, base64_decode($cert['prv']));
file_put_contents($clearkeyfile, base64_decode($cert['prv'])); $eoutpass = escapeshellarg($outpass);
$eoutpass = escapeshellarg($outpass); $ekeyfile = escapeshellarg($keyfile);
$ekeyfile = escapeshellarg($keyfile); $eclearkeyfile = escapeshellarg($clearkeyfile);
$eclearkeyfile = escapeshellarg($clearkeyfile); exec("/usr/local/bin/openssl rsa -in ${eclearkeyfile} -out ${ekeyfile} -des3 -passout pass:${eoutpass}");
exec("/usr/local/bin/openssl rsa -in ${eclearkeyfile} -out ${ekeyfile} -des3 -passout pass:${eoutpass}"); unlink($clearkeyfile);
unlink($clearkeyfile); } else {
} else { $keyfile = "{$tempdir}/key.key";
$keyfile = "{$tempdir}/key.key"; file_put_contents($keyfile, base64_decode($cert['prv']));
file_put_contents($keyfile, base64_decode($cert['prv'])); }
} }
}
// TLS support?
// TLS support? if ($settings['tls']) {
if ($settings['tls']) { $tlsfile = "{$tempdir}/ta.key";
$tlsfile = "{$tempdir}/ta.key"; file_put_contents($tlsfile, base64_decode($settings['tls']));
file_put_contents($tlsfile, base64_decode($settings['tls'])); }
}
// Zip Viscosity file
// Zip Viscosity file if (file_exists("/usr/pbi/zip-{$uname_p}/bin/zip")) {
if(file_exists("/usr/pbi/zip-{$uname_p}/bin/zip")) exec("cd {$tempdir}/.. && /usr/pbi/zip-{$uname_p}/bin/zip -r {$zipfile} Viscosity.visc");
exec("cd {$tempdir}/.. && /usr/pbi/zip-{$uname_p}/bin/zip -r {$zipfile} Viscosity.visc"); } else {
else exec("cd {$tempdir}/.. && /usr/local/bin/zip -r {$zipfile} Viscosity.visc");
exec("cd {$tempdir}/.. && /usr/local/bin/zip -r {$zipfile} Viscosity.visc"); }
// Remove temporary directory // Remove temporary directory
exec("rm -rf {$tempdir}"); exec("rm -rf {$tempdir}");
return $zipfile; return $zipfile;
} }
function openvpn_client_export_sharedkey_config($srvid, $useaddr, $proxy, $zipconf = false) function openvpn_client_export_sharedkey_config($srvid, $useaddr, $proxy, $zipconf = false)
{ {
global $config, $input_errors; global $config, $input_errors;
// lookup server settings // lookup server settings
$settings = $config['openvpn']['openvpn-server'][$srvid]; $settings = $config['openvpn']['openvpn-server'][$srvid];
if (empty($settings)) { if (empty($settings)) {
$input_errors[] = gettext("Could not locate server configuration."); $input_errors[] = gettext("Could not locate server configuration.");
return false; return false;
} }
if ($settings['disable']) { if ($settings['disable']) {
$input_errors[] = gettext("You cannot export for disabled servers."); $input_errors[] = gettext("You cannot export for disabled servers.");
return false; return false;
} }
// determine basic variables // determine basic variables
if ($useaddr == "serveraddr") { if ($useaddr == "serveraddr") {
$interface = $settings['interface']; $interface = $settings['interface'];
if (!empty($settings['ipaddr']) && is_ipaddr($settings['ipaddr'])) { if (!empty($settings['ipaddr']) && is_ipaddr($settings['ipaddr'])) {
$server_host = $settings['ipaddr']; $server_host = $settings['ipaddr'];
} else { } else {
if (!$interface) if (!$interface) {
$interface = "wan"; $interface = "wan";
if (in_array(strtolower($settings['protocol']), array("udp6", "tcp6"))) }
$server_host = get_interface_ipv6($interface); if (in_array(strtolower($settings['protocol']), array("udp6", "tcp6"))) {
else $server_host = get_interface_ipv6($interface);
$server_host = get_interface_ip($interface); } else {
} $server_host = get_interface_ip($interface);
} else if ($useaddr == "serverhostname" || empty($useaddr)) { }
$server_host = empty($config['system']['hostname']) ? "" : "{$config['system']['hostname']}."; }
$server_host .= "{$config['system']['domain']}"; } elseif ($useaddr == "serverhostname" || empty($useaddr)) {
} else $server_host = empty($config['system']['hostname']) ? "" : "{$config['system']['hostname']}.";
$server_host = $useaddr; $server_host .= "{$config['system']['domain']}";
} else {
$server_port = $settings['local_port']; $server_host = $useaddr;
}
$proto = strtolower($settings['protocol']);
if (strtolower(substr($settings['protocol'], 0, 3)) == "tcp") $server_port = $settings['local_port'];
$proto .= "-client";
$proto = strtolower($settings['protocol']);
$cipher = $settings['crypto']; if (strtolower(substr($settings['protocol'], 0, 3)) == "tcp") {
$digest = !empty($settings['digest']) ? $settings['digest'] : "SHA1"; $proto .= "-client";
}
// add basic settings
$conf = "dev tun\n"; $cipher = $settings['crypto'];
if(! empty($settings['tunnel_networkv6'])) { $digest = !empty($settings['digest']) ? $settings['digest'] : "SHA1";
$conf .= "tun-ipv6\n";
} // add basic settings
$conf .= "persist-tun\n"; $conf = "dev tun\n";
$conf .= "persist-key\n"; if (! empty($settings['tunnel_networkv6'])) {
$conf .= "proto {$proto}\n"; $conf .= "tun-ipv6\n";
$conf .= "cipher {$cipher}\n"; }
$conf .= "auth {$digest}\n"; $conf .= "persist-tun\n";
$conf .= "pull\n"; $conf .= "persist-key\n";
$conf .= "resolv-retry infinite\n"; $conf .= "proto {$proto}\n";
$conf .= "remote {$server_host} {$server_port}\n"; $conf .= "cipher {$cipher}\n";
if ($settings['local_network']) { $conf .= "auth {$digest}\n";
list($ip, $mask) = explode('/', $settings['local_network']); $conf .= "pull\n";
$mask = gen_subnet_mask($mask); $conf .= "resolv-retry infinite\n";
$conf .= "route $ip $mask\n"; $conf .= "remote {$server_host} {$server_port}\n";
} if ($settings['local_network']) {
if (!empty($settings['tunnel_network'])) { list($ip, $mask) = explode('/', $settings['local_network']);
list($ip, $mask) = explode('/', $settings['tunnel_network']); $mask = gen_subnet_mask($mask);
$mask = gen_subnet_mask($mask); $conf .= "route $ip $mask\n";
$baselong = ip2long32($ip) & ip2long($mask); }
$ip1 = long2ip32($baselong + 1); if (!empty($settings['tunnel_network'])) {
$ip2 = long2ip32($baselong + 2); list($ip, $mask) = explode('/', $settings['tunnel_network']);
$conf .= "ifconfig $ip2 $ip1\n"; $mask = gen_subnet_mask($mask);
} $baselong = ip2long32($ip) & ip2long($mask);
$conf .= "keepalive 10 60\n"; $ip1 = long2ip32($baselong + 1);
$conf .= "ping-timer-rem\n"; $ip2 = long2ip32($baselong + 2);
$conf .= "ifconfig $ip2 $ip1\n";
if (!empty($proxy)) { }
if ($proxy['proxy_type'] == "http") { $conf .= "keepalive 10 60\n";
if ($proto == "udp") { $conf .= "ping-timer-rem\n";
$input_errors[] = gettext("This server uses UDP protocol and cannot communicate with HTTP proxy.");
return; if (!empty($proxy)) {
} if ($proxy['proxy_type'] == "http") {
$conf .= "http-proxy {$proxy['ip']} {$proxy['port']} "; if ($proto == "udp") {
} $input_errors[] = gettext("This server uses UDP protocol and cannot communicate with HTTP proxy.");
if ($proxy['proxy_type'] == "socks") return;
$conf .= "socks-proxy {$proxy['ip']} {$proxy['port']} "; }
if ($proxy['proxy_authtype'] != "none") { $conf .= "http-proxy {$proxy['ip']} {$proxy['port']} ";
if (!isset($proxy['passwdfile'])) }
$proxy['passwdfile'] = openvpn_client_export_prefix($srvid) . "-proxy"; if ($proxy['proxy_type'] == "socks") {
$conf .= " {$proxy['passwdfile']} {$proxy['proxy_authtype']}"; $conf .= "socks-proxy {$proxy['ip']} {$proxy['port']} ";
} }
$conf .= "\n"; if ($proxy['proxy_authtype'] != "none") {
} if (!isset($proxy['passwdfile'])) {
$proxy['passwdfile'] = openvpn_client_export_prefix($srvid) . "-proxy";
// add key settings }
$prefix = openvpn_client_export_prefix($srvid); $conf .= " {$proxy['passwdfile']} {$proxy['proxy_authtype']}";
$shkeyfile = "{$prefix}.secret"; }
$conf .= "secret {$shkeyfile}\n"; $conf .= "\n";
}
// add optional settings
if ($settings['compression']) // add key settings
$conf .= "comp-lzo\n"; $prefix = openvpn_client_export_prefix($srvid);
if ($settings['passtos']) $shkeyfile = "{$prefix}.secret";
$conf .= "passtos\n"; $conf .= "secret {$shkeyfile}\n";
if ($zipconf == true) { // add optional settings
// create template directory if ($settings['compression']) {
$tempdir = "/tmp/{$prefix}"; $conf .= "comp-lzo\n";
mkdir($tempdir, 0700, true); }
if ($settings['passtos']) {
file_put_contents("{$tempdir}/{$prefix}.ovpn", $conf); $conf .= "passtos\n";
}
$shkeyfile = "{$tempdir}/{$shkeyfile}";
file_put_contents("{$shkeyfile}", base64_decode($settings['shared_key'])); if ($zipconf == true) {
// create template directory
if(file_exists("/usr/pbi/zip-{$uname_p}/bin/zip")) $tempdir = "/tmp/{$prefix}";
exec("cd {$tempdir}/.. && /usr/pbi/zip-{$uname_p}/bin/zip -r /tmp/{$prefix}-config.zip {$prefix}"); mkdir($tempdir, 0700, true);
else
exec("cd {$tempdir}/.. && /usr/local/bin/zip -r /tmp/{$prefix}-config.zip {$prefix}"); file_put_contents("{$tempdir}/{$prefix}.ovpn", $conf);
// Remove temporary directory $shkeyfile = "{$tempdir}/{$shkeyfile}";
exec("rm -rf {$tempdir}"); file_put_contents("{$shkeyfile}", base64_decode($settings['shared_key']));
return "{$prefix}-config.zip";
} else if (file_exists("/usr/pbi/zip-{$uname_p}/bin/zip")) {
return $conf; exec("cd {$tempdir}/.. && /usr/pbi/zip-{$uname_p}/bin/zip -r /tmp/{$prefix}-config.zip {$prefix}");
} else {
exec("cd {$tempdir}/.. && /usr/local/bin/zip -r /tmp/{$prefix}-config.zip {$prefix}");
}
// Remove temporary directory
exec("rm -rf {$tempdir}");
return "{$prefix}-config.zip";
} else {
return $conf;
}
} }
function openvpn_client_export_build_remote_lines($settings, $useaddr, $interface, $expformat, $nl) { function openvpn_client_export_build_remote_lines($settings, $useaddr, $interface, $expformat, $nl) {
global $config; global $config;
$remotes = array(); $remotes = array();
if (($useaddr == "serveraddr") || ($useaddr == "servermagic") || ($useaddr == "servermagichost")) { if (($useaddr == "serveraddr") || ($useaddr == "servermagic") || ($useaddr == "servermagichost")) {
$interface = $settings['interface']; $interface = $settings['interface'];
if (!empty($settings['ipaddr']) && is_ipaddr($settings['ipaddr'])) { if (!empty($settings['ipaddr']) && is_ipaddr($settings['ipaddr'])) {
$server_host = $settings['ipaddr']; $server_host = $settings['ipaddr'];
} else { } else {
if (!$interface || ($interface == "any")) if (!$interface || ($interface == "any")) {
$interface = "wan"; $interface = "wan";
if (in_array(strtolower($settings['protocol']), array("udp6", "tcp6"))) }
$server_host = get_interface_ipv6($interface); if (in_array(strtolower($settings['protocol']), array("udp6", "tcp6"))) {
else $server_host = get_interface_ipv6($interface);
$server_host = get_interface_ip($interface); } else {
} $server_host = get_interface_ip($interface);
} else if ($useaddr == "serverhostname" || empty($useaddr)) { }
$server_host = empty($config['system']['hostname']) ? "" : "{$config['system']['hostname']}."; }
$server_host .= "{$config['system']['domain']}"; } else if ($useaddr == "serverhostname" || empty($useaddr)) {
} else $server_host = empty($config['system']['hostname']) ? "" : "{$config['system']['hostname']}.";
$server_host = $useaddr; $server_host .= "{$config['system']['domain']}";
} else {
$proto = strtolower($settings['protocol']); $server_host = $useaddr;
if (strtolower(substr($settings['protocol'], 0, 3)) == "tcp") }
$proto .= "-client";
$proto = strtolower($settings['protocol']);
if (($expformat == "inlineios") && ($proto == "tcp-client")) if (strtolower(substr($settings['protocol'], 0, 3)) == "tcp") {
$proto = "tcp"; $proto .= "-client";
}
if (($useaddr == "servermagic") || ($useaddr == "servermagichost")) {
$destinations = openvpn_client_export_find_port_forwards($server_host, $settings['local_port'], $proto, true, ($useaddr == "servermagichost")); if (($expformat == "inlineios") && ($proto == "tcp-client")) {
foreach ($destinations as $dest) { $proto = "tcp";
$remotes[] = "remote {$dest['host']} {$dest['port']} {$dest['proto']}"; }
}
} else { if (($useaddr == "servermagic") || ($useaddr == "servermagichost")) {
$remotes[] = "remote {$server_host} {$settings['local_port']} {$proto}"; $destinations = openvpn_client_export_find_port_forwards($server_host, $settings['local_port'], $proto, true, ($useaddr == "servermagichost"));
} foreach ($destinations as $dest) {
$remotes[] = "remote {$dest['host']} {$dest['port']} {$dest['proto']}";
return implode($nl, $remotes); }
} else {
$remotes[] = "remote {$server_host} {$settings['local_port']} {$proto}";
}
return implode($nl, $remotes);
} }
function openvpn_client_export_find_port_forwards($targetip, $targetport, $targetproto, $skipprivate, $findhostname = false) function openvpn_client_export_find_port_forwards($targetip, $targetport, $targetproto, $skipprivate, $findhostname = false)
{ {
global $config; global $config;
$FilterIflist = filter_generate_optcfg_array(); $FilterIflist = filter_generate_optcfg_array();
$destinations = array(); $destinations = array();
if (!isset($config['nat']['rule'])) { if (!isset($config['nat']['rule'])) {
return $destinations; return $destinations;
} }
foreach ($config['nat']['rule'] as $natent) { foreach ($config['nat']['rule'] as $natent) {
$dest = array(); $dest = array();
if (!isset($natent['disabled']) if (!isset($natent['disabled'])
&& ($natent['target'] == $targetip) && ($natent['target'] == $targetip)
&& ($natent['local-port'] == $targetport) && ($natent['local-port'] == $targetport)
&& ($natent['protocol'] == $targetproto)) { && ($natent['protocol'] == $targetproto)) {
$dest['proto'] = $natent['protocol']; $dest['proto'] = $natent['protocol'];
// Could be multiple ports... But we can only use one. // Could be multiple ports... But we can only use one.
$dports = is_port($natent['destination']['port']) ? array($natent['destination']['port']) : filter_expand_alias_array($natent['destination']['port']); $dports = is_port($natent['destination']['port']) ? array($natent['destination']['port']) : filter_expand_alias_array($natent['destination']['port']);
$dest['port'] = $dports[0]; $dest['port'] = $dports[0];
// Could be network or address ... // Could be network or address ...
$natif = (!$natent['interface']) ? "wan" : $natent['interface']; $natif = (!$natent['interface']) ? "wan" : $natent['interface'];
if (!isset($FilterIflist[$natif])) { if (!isset($FilterIflist[$natif])) {
continue; // Skip if there is no interface continue; // Skip if there is no interface
} }
$dstaddr = trim(filter_generate_address($FilterIflist, $natent, 'destination', true)); $dstaddr = trim(filter_generate_address($FilterIflist, $natent, 'destination', true));
if (!$dstaddr) { if (!$dstaddr) {
$dstaddr = $FilterIflist[$natif]['ip']; $dstaddr = $FilterIflist[$natif]['ip'];
} }
$dstaddr_port = explode(" ", $dstaddr); $dstaddr_port = explode(" ", $dstaddr);
if(empty($dstaddr_port[0]) || strtolower(trim($dstaddr_port[0])) == "port") if (empty($dstaddr_port[0]) || strtolower(trim($dstaddr_port[0])) == "port") {
continue; // Skip port forward if no destination address found continue; // Skip port forward if no destination address found
}
if (!is_ipaddr($dstaddr_port[0])) {
if (!is_ipaddr($dstaddr_port[0])) continue; // We can only work with single IPs, not subnets!
continue; // We can only work with single IPs, not subnets! }
if ($skipprivate && is_private_ip($dstaddr_port[0])) {
continue; // Skipping a private IP destination!
if ($skipprivate && is_private_ip($dstaddr_port[0])) }
continue; // Skipping a private IP destination!
$dest['host'] = $dstaddr_port[0];
$dest['host'] = $dstaddr_port[0];
if ($findhostname) {
if ($findhostname) { $hostname = openvpn_client_export_find_hostname($natif);
$hostname = openvpn_client_export_find_hostname($natif); if (!empty($hostname)) {
if (!empty($hostname)) $dest['host'] = $hostname;
$dest['host'] = $hostname; }
} }
$destinations[] = $dest;
$destinations[] = $dest; }
} }
}
return $destinations;
return $destinations;
} }
function openvpn_client_export_find_hostname($interface) function openvpn_client_export_find_hostname($interface)
{ {
global $config; global $config;
$hostname = ''; $hostname = '';
if (isset($config['dyndnses']['dyndns'])) { if (isset($config['dyndnses']['dyndns'])) {
foreach ($config['dyndnses']['dyndns'] as $ddns) { foreach ($config['dyndnses']['dyndns'] as $ddns) {
if (($ddns['interface'] == $interface) && isset($ddns['enable']) && !empty($ddns['host']) && !is_numeric($ddns['host']) && is_hostname($ddns['host'])) if (($ddns['interface'] == $interface) && isset($ddns['enable']) && !empty($ddns['host']) && !is_numeric($ddns['host']) && is_hostname($ddns['host'])) {
return $ddns['host']; return $ddns['host'];
} }
} }
if (isset($config['dnsupdates']['dnsupdate'])) { }
foreach ($config['dnsupdates']['dnsupdate'] as $ddns) { if (isset($config['dnsupdates']['dnsupdate'])) {
if (($ddns['interface'] == $interface) && isset($ddns['enable']) && !empty($ddns['host']) && !is_numeric($ddns['host']) && is_hostname($ddns['host'])) foreach ($config['dnsupdates']['dnsupdate'] as $ddns) {
return $ddns['host']; if (($ddns['interface'] == $interface) && isset($ddns['enable']) && !empty($ddns['host']) && !is_numeric($ddns['host']) && is_hostname($ddns['host'])) {
} return $ddns['host'];
} }
}
}
} }
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