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. */ require_once('config.inc'); require_once('pfsense-utils.inc'); require_once('util.inc'); $openvpn_prots = array( "UDP", "TCP"); $openvpn_auth_methods = array( 'pki' => "Public Key Infrastructure", 'shared_key' => "Pre Shared Key"); function openvpn_vpnid_used($vpnid) { global $config; if (is_array($config['openvpn']['openvpn-server'])) foreach ($config['openvpn']['openvpn-server'] as $id => & $settings) if( $vpnid == $settings['vpnid'] ) return true; if (is_array($config['openvpn']['openvpn-client'])) foreach ($config['openvpn']['openvpn-client'] as $id => & $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_get_cipherlist() { $ciphers = array(); $cipher_out = shell_exec('openvpn --show-ciphers | grep "default key" | 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]}"; } return $ciphers; } function openvpn_validate_host($value, $name) { $value = trim($value); if (empty($value) || !(is_domain($value) && is_ipaddr($value))) return "The field '$name' must contain a valid IP address or domain name."; return false; } function openvpn_validate_port($value, $name) { $value = trim($value); if (empty($value) || !(is_numeric($value) && ($value > 0) && ($value < 65535))) return "The field '$name' must contain a valid port, ranging from 0 to 65535."; 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 "The field '$name' must contain a valid CIDR range."; } 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['dhcp_ntp']}\"\n"; if (!empty($settings['ntp_server2'])) $conf .= "push \"dhcp-option NTP {$settings['dhcp_ntp']}\"\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) { global $g; $fpath = $g['varetc_path']."/openvpn/{$mode_id}.{$directive}"; file_put_contents($fpath, base64_decode($data)); chown($fpath, 'nobody'); chgrp($fpath, 'nobody'); $conf .= "{$directive} {$fpath}\n"; } function openvpn_reconfigure($mode, $id) { global $g, $config; $settings = $config['openvpn']["openvpn-$mode"][$id]; if (empty($settings)) return; if ($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; $tunname = "tun{$vpnid}"; if ($mode == "server") $devname = "ovpns{$vpnid}"; else $devname = "ovpnc{$vpnid}"; if (!file_exists("/dev/{$tunname}")) $tunname = exec("/sbin/ifconfig {$tunname} create"); mwexec("/sbin/ifconfig {$tunname} name {$devname}"); mwexec("/sbin/ifconfig {$devname} group openvpn"); $pidfile = $g['varrun_path'] . "/openvpn_{$mode_id}.pid"; $proto = ($settings['protocol'] == 'UDP' ? 'udp' : "tcp-{$mode}"); $cipher = $settings['crypto']; $interface = $settings['interface']; if (!$interface) $interface = 'WAN'; $iface = convert_friendly_interface_to_real_interface_name($interface); $lines = explode(' ', trim(shell_exec("ifconfig $iface | grep inet | grep -v inet6"))); $iface_ip = $lines[1]; $conf .= << & $settings) openvpn_resync('server', $id); if (is_array($config['openvpn']['openvpn-client'])) foreach ($config['openvpn']['openvpn-client'] as $id => & $settings) openvpn_resync('client', $id); if (is_array($config['openvpn']['openvpn-csc'])) foreach ($config['openvpn']['openvpn-csc'] as $id => & $settings) openvpn_resync_csc($id); /* give speedy machines time to settle */ sleep(5); /* reload the filter policy */ filter_configure(); } ?>