All rights reserved. Copyright (C) 2006 Fernando Lemos All rights reserved. This file was rewritten from scratch by Fernando Lemos but *MIGHT* contain code previously written by: Copyright (C) 2005 Peter Allgeyer All rights reserved. Copyright (C) 2004 Peter Curran (peter@closeconsultants.com). 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 notices, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notices, 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. DISABLE_PHP_LINT_CHECKING pfSense_BUILDER_BINARIES: /usr/local/sbin/openvpn /usr/bin/openssl /sbin/ifconfig pfSense_MODULE: openvpn */ require_once('config.inc'); require_once("certs.inc"); require_once('pfsense-utils.inc'); require_once("auth.inc"); $openvpn_prots = array("UDP", "TCP"); $openvpn_dev_mode = array("tun", "tap"); /* * The User Auth mode below is disabled because * OpenVPN erroneously requires that we provide * a CA configuration parameter. In this mode, * clients don't send a certificate so there is * no need for a CA. If we require that admins * provide one in the pfSense UI due to a bogus * requirement imposed by OpenVPN, it could be * considered very confusing ( I know I was ). * * -mgrooms */ $openvpn_dh_lengths = array( 1024, 2048, 4096 ); $openvpn_cert_depths = array( 1 => "One (Client+Server)", 2 => "Two (Client+Intermediate+Server)", 3 => "Three (Client+2xIntermediate+Server)", 4 => "Four (Client+3xIntermediate+Server)", 5 => "Five (Client+4xIntermediate+Server)" ); $openvpn_server_modes = array( 'p2p_tls' => gettext("Peer to Peer ( SSL/TLS )"), 'p2p_shared_key' => gettext("Peer to Peer ( Shared Key )"), 'server_tls' => gettext("Remote Access ( SSL/TLS )"), 'server_user' => gettext("Remote Access ( User Auth )"), 'server_tls_user' => gettext("Remote Access ( SSL/TLS + User Auth )")); $openvpn_client_modes = array( 'p2p_tls' => gettext("Peer to Peer ( SSL/TLS )"), 'p2p_shared_key' => gettext("Peer to Peer ( Shared Key )") ); function openvpn_create_key() { $fp = popen("/usr/local/sbin/openvpn --genkey --secret /dev/stdout 2>/dev/null", "r"); if (!$fp) return false; $rslt = stream_get_contents($fp); pclose($fp); return $rslt; } function openvpn_create_dhparams($bits) { $fp = popen("/usr/bin/openssl dhparam {$bits} 2>/dev/null", "r"); if (!$fp) return false; $rslt = stream_get_contents($fp); pclose($fp); return $rslt; } function openvpn_vpnid_used($vpnid) { global $config; if (is_array($config['openvpn']['openvpn-server'])) foreach ($config['openvpn']['openvpn-server'] as & $settings) if ($vpnid == $settings['vpnid']) return true; if (is_array($config['openvpn']['openvpn-client'])) foreach ($config['openvpn']['openvpn-client'] as & $settings) if ($vpnid == $settings['vpnid']) return true; return false; } function openvpn_vpnid_next() { $vpnid = 1; while(openvpn_vpnid_used($vpnid)) $vpnid++; return $vpnid; } function openvpn_port_used($prot, $port) { global $config; if (is_array($config['openvpn']['openvpn-server'])) foreach ($config['openvpn']['openvpn-server'] as & $settings) if ($port == $settings['local_port'] && $prot == $settings['protocol'] && !isset($settings['disable'])) return $settings['vpnid']; if (is_array($config['openvpn']['openvpn-client'])) foreach ($config['openvpn']['openvpn-client'] as & $settings) if ($port == $settings['local_port'] && $prot == $settings['protocol'] && !isset($settings['disable'])) return $settings['vpnid']; return 0; } function openvpn_port_next($prot) { $port = 1194; while(openvpn_port_used($prot, $port)) $port++; return $port; } function openvpn_get_cipherlist() { $ciphers = array(); $cipher_out = shell_exec('/usr/local/sbin/openvpn --show-ciphers | /usr/bin/grep "default key" | /usr/bin/awk \'{print $1, "(" $2 "-" $3 ")";}\''); $cipher_lines = explode("\n", trim($cipher_out)); sort($cipher_lines); foreach ($cipher_lines as $line) { $words = explode(' ', $line); $ciphers[$words[0]] = "{$words[0]} {$words[1]}"; } $ciphers["none"] = gettext("None (No Encryption)"); return $ciphers; } function openvpn_get_engines() { $openssl_engines = array('none' => 'No Hardware Crypto Acceleration'); exec("/usr/bin/openssl engine", $openssl_engine_output); foreach ($openssl_engine_output as $oeo) { $linematch = array(); preg_match("/\((.*)\)\s(.*)/", $oeo, $linematch); if ($linematch[1] != "dynamic") $openssl_engines[$linematch[1]] = $linematch[2]; } return $openssl_engines; } function openvpn_validate_engine($engine) { $engines = openvpn_get_engines(); return array_key_exists($engine, $engines); } function openvpn_validate_host($value, $name) { $value = trim($value); if (empty($value) || (!is_domain($value) && !is_ipaddr($value))) return sprintf(gettext("The field '%s' must contain a valid IP address or domain name."), $name); return false; } function openvpn_validate_port($value, $name) { $value = trim($value); if (empty($value) || !is_numeric($value) || $value < 0 || ($value > 65535)) return sprintf(gettext("The field '%s' must contain a valid port, ranging from 0 to 65535."), $name); return false; } function openvpn_validate_cidr($value, $name) { $value = trim($value); if (!empty($value)) { list($ip, $mask) = explode('/', $value); if (!is_ipaddr($ip) or !is_numeric($mask) or ($mask > 32) or ($mask < 0)) return sprintf(gettext("The field '%s' must contain a valid CIDR range."), $name); } return false; } function openvpn_add_dhcpopts(& $settings, & $conf) { if (!empty($settings['dns_domain'])) $conf .= "push \"dhcp-option DOMAIN {$settings['dns_domain']}\"\n"; if (!empty($settings['dns_server1'])) $conf .= "push \"dhcp-option DNS {$settings['dns_server1']}\"\n"; if (!empty($settings['dns_server2'])) $conf .= "push \"dhcp-option DNS {$settings['dns_server2']}\"\n"; if (!empty($settings['dns_server3'])) $conf .= "push \"dhcp-option DNS {$settings['dns_server3']}\"\n"; if (!empty($settings['dns_server4'])) $conf .= "push \"dhcp-option DNS {$settings['dns_server4']}\"\n"; if (!empty($settings['ntp_server1'])) $conf .= "push \"dhcp-option NTP {$settings['ntp_server1']}\"\n"; if (!empty($settings['ntp_server2'])) $conf .= "push \"dhcp-option NTP {$settings['ntp_server2']}\"\n"; if ($settings['netbios_enable']) { if (!empty($settings['dhcp_nbttype']) && ($settings['dhcp_nbttype'] != 0)) $conf .= "push \"dhcp-option NBT {$settings['dhcp_nbttype']}\"\n"; if (!empty($settings['dhcp_nbtscope'])) $conf .= "push \"dhcp-option NBS {$settings['dhcp_nbtscope']}\"\n"; if (!empty($settings['wins_server1'])) $conf .= "push \"dhcp-option WINS {$settings['wins_server1']}\"\n"; if (!empty($settings['wins_server2'])) $conf .= "push \"dhcp-option WINS {$settings['wins_server2']}\"\n"; if (!empty($settings['nbdd_server1'])) $conf .= "push \"dhcp-option NBDD {$settings['nbdd_server1']}\"\n"; } if ($settings['gwredir']) $conf .= "push \"redirect-gateway def1\"\n"; } function openvpn_add_custom(& $settings, & $conf) { if ($settings['custom_options']) { $options = explode(';', $settings['custom_options']); if (is_array($options)) { foreach ($options as $option) $conf .= "$option\n"; } else $conf .= "{$settings['custom_options']}\n"; } } function openvpn_add_keyfile(& $data, & $conf, $mode_id, $directive, $opt = "") { global $g; $fpath = $g['varetc_path']."/openvpn/{$mode_id}.{$directive}"; file_put_contents($fpath, base64_decode($data)); //chown($fpath, 'nobody'); //chgrp($fpath, 'nobody'); @chmod($fpath, 0600); $conf .= "{$directive} {$fpath} {$opt}\n"; } function openvpn_reconfigure($mode, $settings) { global $g, $config; if (empty($settings)) return; if (isset($settings['disable'])) return; /* * NOTE: Deleting tap devices causes spontaneous reboots. Instead, * we use a vpnid number which is allocated for a particular client * or server configuration. ( see openvpn_vpnid_next() ) */ $vpnid = $settings['vpnid']; $mode_id = $mode.$vpnid; if (isset($settings['dev_mode'])) $tunname = "{$settings['dev_mode']}{$vpnid}"; else { /* defaults to tun */ $tunname = "tun{$vpnid}"; $settings['dev_mode'] = "tun"; } if ($mode == "server") $devname = "ovpns{$vpnid}"; else $devname = "ovpnc{$vpnid}"; /* is our device already configured */ if (mwexec("/sbin/ifconfig {$devname}")) { /* create the tap device if required */ if (!file_exists("/dev/{$tunname}")) exec("/sbin/ifconfig {$tunname} create"); /* rename the device */ mwexec("/sbin/ifconfig {$tunname} name {$devname}"); /* add the device to the openvpn group */ mwexec("/sbin/ifconfig {$devname} group openvpn"); } $pfile = $g['varrun_path'] . "/openvpn_{$mode_id}.pid"; $proto = ($settings['protocol'] == 'UDP' ? 'udp' : "tcp-{$mode}"); $dev_mode = $settings['dev_mode']; $cipher = $settings['crypto']; $interface = $settings['interface']; $ipaddr = $settings['ipaddr']; $ipaddrv6 = $settings['ipaddrv6']; // If a specific ip address (VIP) is requested, use it. // Otherwise, if a specific interface is requested, use it // If "any" interface was selected, local directive will be ommited. if (is_ipaddrv4($ipaddr)) { $iface_ip=$ipaddr; } elseif (is_ipaddrv6($ipaddrv6)) { $iface_ipv6=$ipaddrv6; } else { if ((!empty($interface)) && (strcmp($interface, "any"))) { $iface_ip=get_interface_ip($interface); } if ((!empty($interface)) && (strcmp($interface, "any"))) { $iface_ipv6=get_interface_ipv6($interface); } } $conf = "dev {$devname}\n"; $conf .= "dev-type {$settings['dev_mode']}\n"; switch($settings['dev_mode']) { case "tun": $conf .= "tun-ipv6\n"; break; } $conf .= "dev-node /dev/{$tunname}\n"; $conf .= "writepid {$pfile}\n"; $conf .= "#user nobody\n"; $conf .= "#group nobody\n"; $conf .= "script-security 3\n"; $conf .= "daemon\n"; $conf .= "keepalive 10 60\n"; $conf .= "ping-timer-rem\n"; $conf .= "persist-tun\n"; $conf .= "persist-key\n"; $conf .= "proto {$proto}\n"; $conf .= "cipher {$cipher}\n"; $conf .= "up /usr/local/sbin/ovpn-linkup\n"; $conf .= "down /usr/local/sbin/ovpn-linkdown\n"; if (is_ipaddrv4($iface_ip)) { $conf .= "local {$iface_ip}\n"; } if (is_ipaddrv6($iface_ipv6)) { // $conf .= "local {$iface_ipv6}\n"; } if (openvpn_validate_engine($settings['engine']) && ($settings['engine'] != "none")) $conf .= "engine {$settings['engine']}\n"; // server specific settings if ($mode == 'server') { list($ip, $cidr) = explode('/', $settings['tunnel_network']); list($ipv6, $prefix) = explode('/', $settings['tunnel_networkv6']); $mask = gen_subnet_mask($cidr); // configure tls modes switch($settings['mode']) { case 'p2p_tls': case 'server_tls': case 'server_user': case 'server_tls_user': $conf .= "tls-server\n"; break; } // configure p2p/server modes switch($settings['mode']) { case 'p2p_tls': // If the CIDR is less than a /30, OpenVPN will complain if you try to // use the server directive. It works for a single client without it. // See ticket #1417 if (!empty($ip) && !empty($mask) && ($cidr < 30)) { $conf .= "server {$ip} {$mask}\n"; $conf .= "client-config-dir {$g['varetc_path']}/openvpn-csc\n"; } case 'p2p_shared_key': if (!empty($ip) && !empty($mask)) { $baselong = ip2long32($ip) & ip2long($mask); $ip1 = long2ip32($baselong + 1); $ip2 = long2ip32($baselong + 2); if ($settings['dev_mode'] == 'tun') $conf .= "ifconfig {$ip1} {$ip2}\n"; else $conf .= "ifconfig {$ip1} {$mask}\n"; } break; case 'server_tls': case 'server_user': case 'server_tls_user': if (!empty($ip) && !empty($mask)) { $conf .= "server {$ip} {$mask}\n"; if(is_ipaddr($ipv6)) $conf .= "server-ipv6 {$ipv6}/{$prefix}\n"; $conf .= "client-config-dir {$g['varetc_path']}/openvpn-csc\n"; } else { if ($settings['serverbridge_dhcp']) { if ((!empty($settings['serverbridge_interface'])) && (strcmp($settings['serverbridge_interface'], "none"))) { $biface_ip=get_interface_ip($settings['serverbridge_interface']); $biface_sm=gen_subnet_mask(get_interface_subnet($settings['serverbridge_interface'])); if (is_ipaddrv4($biface_ip) && is_ipaddrv4($settings['serverbridge_dhcp_start']) && is_ipaddrv4($settings['serverbridge_dhcp_end'])) { $conf .= "server-bridge {$biface_ip} {$biface_sm} {$settings['serverbridge_dhcp_start']} {$settings['serverbridge_dhcp_end']}\n"; } else { $conf .= "mode server\n"; } } else { $conf .= "mode server\n"; } } } break; } // configure user auth modes switch($settings['mode']) { case 'server_user': $conf .= "client-cert-not-required\n"; case 'server_tls_user': /* username-as-common-name is not compatible with server-bridge */ if (stristr($conf, "server-bridge") === false) $conf .= "username-as-common-name\n"; if (!empty($settings['authmode'])) { $authcfgs = explode(",", $settings['authmode']); $sed = "\$authmodes=array("; $firstsed = 0; foreach ($authcfgs as $authcfg) { if ($firstsed > 0) $sed .= ","; $firstsed = 1; $sed .= "\"{$authcfg}\""; } $sed .= ");\\\n"; if ($settings['strictusercn']) $sed .= "\$strictusercn = true;"; $sed .= " \$modeid = \"{$mode_id}\";"; mwexec("/bin/cat /etc/inc/openvpn.auth-user.php | /usr/bin/sed 's/\/\/