Files
GlobalProtect-openconnect/crates/openconnect/src/vpn.rs
T

1143 lines
35 KiB
Rust

#[cfg(unix)]
use std::os::fd::{AsRawFd, OwnedFd};
#[cfg(windows)]
use std::os::windows::io::{AsRawHandle, OwnedHandle};
use std::{
ffi::{CStr, CString, c_char},
fmt, io,
sync::{Arc, Mutex, RwLock},
};
use log::{info, warn};
use crate::vpn_utils::{check_executable, find_vpnc_script};
use crate::{HipSource, NetworkConfiguration, NetworkConfigurationHandler, ffi};
type OnConnectedCallback = Arc<RwLock<Option<Box<dyn FnOnce(VpnSessionInfo) + 'static + Send + Sync>>>>;
type OnHipReportCallback = RwLock<Option<Arc<dyn Fn(&str) + Send + Sync>>>;
type OnTunnelCallback = RwLock<Option<Arc<dyn Fn(crate::tunnel::Event) + Send + Sync>>>;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct VpnSessionInfo {
pub lifetime_secs: Option<u32>,
pub user_expires: Option<u32>,
pub lifetime_warning: Option<VpnSessionWarning>,
pub nlb_enabled: bool,
pub nlb_connected_gw_ip: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VpnSessionWarning {
pub prior_secs: u32,
pub message: String,
}
pub(crate) fn session_info_from_raw(raw: *const ffi::VpnSessionInfoRaw) -> VpnSessionInfo {
if raw.is_null() {
return VpnSessionInfo::default();
}
let raw = unsafe { &*raw };
let warning_message =
unsafe { optional_c_string(raw.lifetime_warning_message) }.filter(|message| !message.is_empty());
let user_expires =
positive_i64_to_u32(raw.user_expires.into()).or_else(|| positive_i64_to_u32(raw.auth_expiration.into()));
VpnSessionInfo {
lifetime_secs: positive_i64_to_u32(raw.lifetime_secs as i64),
user_expires,
lifetime_warning: match (positive_i64_to_u32(raw.lifetime_warning_prior as i64), warning_message) {
(Some(prior_secs), Some(message)) => Some(VpnSessionWarning { prior_secs, message }),
_ => None,
},
nlb_enabled: raw.nlb_enabled != 0,
nlb_connected_gw_ip: unsafe { optional_c_string(raw.nlb_connected_gw_ip) },
}
}
unsafe fn optional_c_string(value: *const c_char) -> Option<String> {
if value.is_null() {
return None;
}
unsafe { CStr::from_ptr(value) }.to_str().ok().map(ToOwned::to_owned)
}
fn positive_i64_to_u32(value: i64) -> Option<u32> {
u32::try_from(value).ok().filter(|value| *value > 0)
}
// The descriptor is borrowed only between attach and detach. Serialize writes
// against teardown and retain cancellation before the pipe exists.
#[derive(Default)]
struct Cancellation {
state: Mutex<CancellationState>,
}
#[derive(Default)]
struct CancellationState {
requested: bool,
command_fd: Option<ffi::CommandHandle>,
statistics_pending: bool,
}
impl Cancellation {
fn attach(&self, fd: ffi::CommandHandle) -> bool {
let mut state = self.state.lock().unwrap();
state.command_fd = Some(fd);
state.requested
}
fn detach(&self) {
let mut state = self.state.lock().unwrap();
state.command_fd = None;
state.statistics_pending = false;
}
fn request_statistics(&self) -> io::Result<bool> {
let mut state = self.state.lock().unwrap();
if state.requested || state.statistics_pending {
return Ok(false);
}
let Some(fd) = state.command_fd else {
return Ok(false);
};
match ffi::write_statistics(fd) {
Ok(()) => {
state.statistics_pending = true;
Ok(true)
}
Err(error) if error.kind() == io::ErrorKind::WouldBlock => Ok(false),
Err(error) => Err(error),
}
}
fn statistics_received(&self) {
self.state.lock().unwrap().statistics_pending = false;
}
fn cancel(&self) -> io::Result<()> {
let mut state = self.state.lock().unwrap();
if state.requested {
return Ok(());
}
state.requested = true;
if let Some(fd) = state.command_fd {
ffi::write_cancel(fd)?;
}
Ok(())
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum GatewayIpPreference {
#[default]
Auto = 0,
Ipv4 = 4,
Ipv6 = 6,
}
pub struct Vpn {
cancellation: Cancellation,
server: CString,
cookie: CString,
user_agent: CString,
os: CString,
os_version: Option<CString>,
client_version: Option<CString>,
host_id: Option<CString>,
local_hostname: Option<CString>,
script: Option<CString>,
pub(crate) network_configuration_handler: Option<Arc<NetworkConfigurationHandler>>,
interface: Option<CString>,
script_tun: bool,
#[cfg(unix)]
script_tun_launch: Option<ScriptTunLaunch>,
#[cfg(windows)]
script_tun_pipe: Option<OwnedHandle>,
certificate: Option<CString>,
sslkey: Option<CString>,
key_password: Option<CString>,
servercert: Option<CString>,
pub(crate) hip_source: Arc<HipSource>,
reconnect_timeout: u32,
mtu: u32,
disable_ipv6: bool,
gateway_ip_preference: GatewayIpPreference,
no_dtls: bool,
dpd_interval: u32,
no_xmlpost: bool,
callback: OnConnectedCallback,
hip_report_callback: OnHipReportCallback,
tunnel_callback: OnTunnelCallback,
observed_transport: Mutex<Option<crate::tunnel::Transport>>,
}
impl Vpn {
/// Install before `connect`. The observer runs synchronously on the native
/// connection thread and must return promptly. Clear it after joining connect.
pub fn set_tunnel_observer(&self, callback: impl Fn(crate::tunnel::Event) + Send + Sync + 'static) {
*self.tunnel_callback.write().unwrap() = Some(Arc::new(callback));
}
pub fn clear_tunnel_observer(&self) {
*self.tunnel_callback.write().unwrap() = None;
}
/// Signal a nonblocking statistics request. False means no command was issued
/// because collection was coalesced, unavailable, cancelled, or the pipe full.
pub fn request_statistics(&self) -> io::Result<bool> {
self.cancellation.request_statistics()
}
pub(crate) fn statistics_received(&self) {
self.cancellation.statistics_received();
}
pub(crate) fn on_tunnel_event(&self, event: crate::tunnel::Event) {
if let crate::tunnel::Event::Transport(transport) = &event {
let mut previous = self.observed_transport.lock().unwrap();
if previous.as_ref() == Some(transport) {
return;
}
*previous = Some(*transport);
}
let callback = self.tunnel_callback.read().unwrap().clone();
if let Some(callback) = callback {
callback(event);
}
}
pub fn builder(server: &str, cookie: &str) -> VpnBuilder {
VpnBuilder::new(server, cookie)
}
pub fn connect(&self, on_connected: impl FnOnce(VpnSessionInfo) + 'static + Send + Sync) -> i32 {
match self.hip_source.as_ref() {
HipSource::Disabled => info!("HIP reporting: disabled"),
HipSource::Generator(_) => info!("HIP reporting: enabled, source=callback"),
HipSource::Script(script) => {
#[cfg(unix)]
{
let uid = script.user.unwrap_or_else(|| unsafe { libc::geteuid() });
info!("HIP reporting: enabled, source=script, uid={uid}");
}
#[cfg(not(unix))]
match script.user {
Some(uid) => info!("HIP reporting: enabled, source=script, uid={uid}"),
None => info!("HIP reporting: enabled, source=script, user=process-user"),
}
}
}
self.callback.write().unwrap().replace(Box::new(on_connected));
let mut options = self.build_connect_options();
#[cfg(unix)]
let mut argv = Vec::new();
#[cfg(unix)]
let launch = self.script_tun_launch.as_ref().map(|launch| {
argv.push(launch.program.as_ptr());
argv.extend(launch.arguments.iter().map(|s| s.as_ptr()));
argv.push(std::ptr::null());
ffi::ScriptTunLaunchRaw {
program: launch.program.as_ptr(),
argv: argv.as_ptr(),
uid: launch.uid,
control_fd: launch.control.lock().unwrap().as_ref().map_or(-1, AsRawFd::as_raw_fd),
}
});
// Both the borrowed launch record and argv remain alive through native connect.
#[cfg(unix)]
{
options.script_tun_launch = launch.as_ref().map_or(std::ptr::null(), |launch| launch);
}
let environment = match self.hip_source.as_ref() {
HipSource::Script(script) => ffi::script_environment(script),
_ => Vec::new(),
};
if !environment.is_empty() {
options.hip_script.environment = environment.as_ptr();
}
ffi::connect(&options)
}
/// Receives a borrowed report after the gateway accepts its HIP submission.
/// The callback must copy the XML if it needs to retain it.
pub fn set_hip_report_callback(&self, callback: impl Fn(&str) + Send + Sync + 'static) {
*self.hip_report_callback.write().unwrap() = Some(Arc::new(callback));
}
pub(crate) fn on_hip_report_submitted(&self, report: &str) {
let callback = self.hip_report_callback.read().unwrap().clone();
if let Some(callback) = callback {
callback(report);
}
}
pub(crate) fn on_connected(&self, pipe_fd: ffi::CommandHandle, session_info: VpnSessionInfo) {
info!("Connected to VPN, pipe_fd: {}", pipe_fd);
if let Some(callback) = self.callback.write().unwrap().take() {
callback(session_info);
}
}
pub(crate) fn attach_command_pipe(&self, fd: ffi::CommandHandle) -> bool {
self.cancellation.attach(fd)
}
pub(crate) fn detach_command_pipe(&self) {
self.cancellation.detach();
}
pub fn disconnect(&self) {
if let Err(error) = self.cancellation.cancel() {
warn!("Failed to signal VPN cancellation: {error}");
}
}
fn build_connect_options(&self) -> ffi::ConnectOptions {
ffi::ConnectOptions {
user_data: self as *const _ as *mut _,
on_hip_report_submitted: Some(ffi::on_hip_report_submitted),
server: self.server.as_ptr(),
cookie: self.cookie.as_ptr(),
user_agent: self.user_agent.as_ptr(),
os: self.os.as_ptr(),
os_version: Self::option_to_ptr(&self.os_version),
client_version: Self::option_to_ptr(&self.client_version),
host_id: Self::option_to_ptr(&self.host_id),
local_hostname: Self::option_to_ptr(&self.local_hostname),
script: Self::option_to_ptr(&self.script),
configure_network: self
.network_configuration_handler
.as_ref()
.map(|_| ffi::configure_network as _),
interface_name: Self::option_to_ptr(&self.interface),
script_tun: self.script_tun as u32,
script_tun_launch: std::ptr::null(),
#[cfg(windows)]
script_tun_pipe: self
.script_tun_pipe
.as_ref()
.map(|handle| handle.as_raw_handle() as isize)
.unwrap_or(-1),
certificate: Self::option_to_ptr(&self.certificate),
sslkey: Self::option_to_ptr(&self.sslkey),
key_password: Self::option_to_ptr(&self.key_password),
servercert: Self::option_to_ptr(&self.servercert),
hip_script: match self.hip_source.as_ref() {
HipSource::Script(script) => ffi::HipScriptRaw::from_script(script),
_ => Default::default(),
},
generate_hip: match self.hip_source.as_ref() {
HipSource::Generator(_) => Some(ffi::generate_hip_report),
_ => None,
},
reconnect_timeout: self.reconnect_timeout,
mtu: self.mtu,
disable_ipv6: self.disable_ipv6 as u32,
gateway_ip_preference: self.gateway_ip_preference as u32,
no_dtls: self.no_dtls as u32,
dpd_interval: self.dpd_interval,
no_xmlpost: self.no_xmlpost as u32,
}
}
fn option_to_ptr(option: &Option<CString>) -> *const c_char {
match option {
Some(value) => value.as_ptr(),
None => std::ptr::null(),
}
}
}
#[derive(Debug)]
pub struct VpnError {
message: String,
}
impl VpnError {
fn new(message: String) -> Self {
Self { message }
}
}
impl fmt::Display for VpnError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.message)
}
}
impl std::error::Error for VpnError {}
#[cfg(unix)]
struct ScriptTunLaunch {
program: CString,
arguments: Vec<CString>,
uid: u32,
control: Mutex<Option<OwnedFd>>,
}
pub struct VpnBuilder {
server: String,
cookie: String,
script: Option<String>,
network_configuration_handler: Option<Arc<NetworkConfigurationHandler>>,
script_is_path: bool,
interface: Option<String>,
script_tun: bool,
#[cfg(unix)]
script_tun_launch: Option<ScriptTunLaunch>,
#[cfg(windows)]
script_tun_pipe: Option<OwnedHandle>,
user_agent: Option<String>,
os: Option<String>,
os_version: Option<String>,
client_version: Option<String>,
host_id: Option<String>,
local_hostname: Option<String>,
certificate: Option<String>,
sslkey: Option<String>,
key_password: Option<String>,
hip_source: Arc<HipSource>,
reconnect_timeout: u32,
mtu: u32,
disable_ipv6: bool,
gateway_ip_preference: GatewayIpPreference,
no_dtls: bool,
dpd_interval: u32,
no_xmlpost: bool,
}
impl VpnBuilder {
fn new(server: &str, cookie: &str) -> Self {
Self {
server: server.to_string(),
cookie: cookie.to_string(),
script: None,
network_configuration_handler: None,
script_is_path: false,
interface: None,
script_tun: false,
#[cfg(unix)]
script_tun_launch: None,
#[cfg(windows)]
script_tun_pipe: None,
user_agent: None,
os: None,
os_version: None,
client_version: None,
host_id: None,
local_hostname: None,
certificate: None,
sslkey: None,
key_password: None,
hip_source: Arc::new(HipSource::Disabled),
reconnect_timeout: 300,
mtu: 0,
disable_ipv6: false,
gateway_ip_preference: GatewayIpPreference::Auto,
no_dtls: false,
dpd_interval: 0,
no_xmlpost: false,
}
}
pub fn script<T: Into<Option<String>>>(mut self, script: T) -> Self {
self.script = script.into();
self.script_is_path = false;
self
}
pub fn script_path<T: Into<Option<String>>>(mut self, script: T) -> Self {
self.script = script.into();
self.script_is_path = true;
self
}
/// Handle network configuration synchronously on OpenConnect's connection thread.
/// Replaces the external configuration script, not the tunnel device or packet I/O.
/// The handler receives a snapshot; it must not mutate the process environment.
pub fn network_configuration_handler(
mut self,
handler: impl Fn(&NetworkConfiguration) -> io::Result<()> + Send + Sync + 'static,
) -> Self {
self.network_configuration_handler = Some(Arc::new(handler));
self
}
pub fn interface<T: Into<Option<String>>>(mut self, interface: T) -> Self {
self.interface = interface.into();
self
}
#[cfg(unix)]
pub fn script_tun_program(
mut self,
program: &str,
arguments: &[String],
uid: u32,
inherited: OwnedFd,
) -> Result<Self, VpnError> {
if !std::path::Path::new(program).is_absolute() || program.contains('\0') {
return Err(VpnError::new("Script-tun requires an absolute executable path".into()));
}
let arguments = arguments
.iter()
.map(|arg| CString::new(arg.as_str()).map_err(|_| VpnError::new("Script-tun argument contains NUL".into())))
.collect::<Result<_, _>>()?;
self.script = None;
self.script_tun = true;
self.script_is_path = false;
self.script_tun_launch = Some(ScriptTunLaunch {
program: CString::new(program).expect("program was checked for NUL"),
arguments,
uid,
control: Mutex::new(Some(inherited)),
});
Ok(self)
}
/// Deliver packets through a connected Windows message-mode pipe.
///
/// # Safety
/// The handle must use overlapped I/O and must not be associated with an IOCP.
/// OpenConnect duplicates it and owns its own completion events; attaching a
/// Tokio/Mio handle would send native OVERLAPPED completions into that reactor.
#[cfg(windows)]
pub unsafe fn script_tun_pipe(mut self, pipe: OwnedHandle) -> Self {
self.script_tun = true;
self.script_tun_pipe = Some(pipe);
self
}
pub fn script_tun(mut self, script_tun: bool) -> Self {
self.script_tun = script_tun;
self
}
pub fn user_agent<T: Into<Option<String>>>(mut self, user_agent: T) -> Self {
self.user_agent = user_agent.into();
self
}
pub fn os<T: Into<Option<String>>>(mut self, os: T) -> Self {
self.os = os.into();
self
}
pub fn os_version<T: Into<Option<String>>>(mut self, os_version: T) -> Self {
self.os_version = os_version.into();
self
}
pub fn client_version<T: Into<Option<String>>>(mut self, client_version: T) -> Self {
self.client_version = client_version.into();
self
}
pub fn host_id<T: Into<Option<String>>>(mut self, host_id: T) -> Self {
self.host_id = host_id.into();
self
}
pub fn local_hostname<T: Into<Option<String>>>(mut self, local_hostname: T) -> Self {
self.local_hostname = local_hostname.into();
self
}
pub fn certificate<T: Into<Option<String>>>(mut self, certificate: T) -> Self {
self.certificate = certificate.into();
self
}
pub fn sslkey<T: Into<Option<String>>>(mut self, sslkey: T) -> Self {
self.sslkey = sslkey.into();
self
}
pub fn key_password<T: Into<Option<String>>>(mut self, key_password: T) -> Self {
self.key_password = key_password.into();
self
}
pub fn hip_source(mut self, hip_source: impl Into<Arc<HipSource>>) -> Self {
self.hip_source = hip_source.into();
self
}
pub fn reconnect_timeout(mut self, reconnect_timeout: u32) -> Self {
self.reconnect_timeout = reconnect_timeout;
self
}
pub fn mtu(mut self, mtu: u32) -> Self {
self.mtu = mtu;
self
}
/// Request IPv4-only connectivity inside the VPN tunnel.
pub fn disable_ipv6(mut self, disable_ipv6: bool) -> Self {
self.disable_ipv6 = disable_ipv6;
self
}
/// Order gateway addresses without excluding either IP family.
pub fn gateway_ip_preference(mut self, preference: GatewayIpPreference) -> Self {
self.gateway_ip_preference = preference;
self
}
pub fn no_dtls(mut self, no_dtls: bool) -> Self {
self.no_dtls = no_dtls;
self
}
pub fn dpd_interval(mut self, dpd_interval: u32) -> Self {
self.dpd_interval = dpd_interval;
self
}
pub fn no_xmlpost(mut self, no_xmlpost: bool) -> Self {
self.no_xmlpost = no_xmlpost;
self
}
fn resolve_script(&self) -> Result<Option<CString>, VpnError> {
if self.network_configuration_handler.is_some() {
if self.script.is_some() || self.script_tun {
return Err(VpnError::new(
"Network configuration handler conflicts with script options".into(),
));
}
return Ok(None);
}
#[cfg(unix)]
if self.script_tun_launch.is_some() {
return Ok(None);
}
#[cfg(windows)]
if self.script_tun_pipe.is_some() {
if self.script.is_some() || !self.script_tun {
return Err(VpnError::new("Packet pipe conflicts with script options".into()));
}
return Ok(None);
}
let script = match &self.script {
Some(script) => {
if self.script_is_path && !std::path::Path::new(script).exists() {
return Err(VpnError::new(format!("VPN script does not exist: {script}")));
}
check_executable(script).map_err(|e| VpnError::new(e.to_string()))?;
Ok(script.clone())
}
None => find_vpnc_script().ok_or_else(|| VpnError::new(String::from("Failed to find vpnc-script"))),
}?;
let script = if self.script_is_path || self.script.is_none() {
shell_quote_path(&script)
} else {
script
};
Ok(Some(Self::to_cstring(&script)))
}
pub fn build(self) -> Result<Vpn, VpnError> {
#[cfg(unix)]
if self.script_tun_launch.is_some() && !self.script_tun {
return Err(VpnError::new("Child identity requires script-tun".into()));
}
let script = self.resolve_script()?;
let user_agent = self.user_agent.unwrap_or_default();
let os = self.os.unwrap_or("linux".to_string());
Ok(Vpn {
cancellation: Cancellation::default(),
server: Self::to_cstring(&self.server),
cookie: Self::to_cstring(&self.cookie),
user_agent: Self::to_cstring(&user_agent),
os: Self::to_cstring(&os),
os_version: self.os_version.as_deref().map(Self::to_cstring),
client_version: self.client_version.as_deref().map(Self::to_cstring),
host_id: self.host_id.as_deref().map(Self::to_cstring),
local_hostname: self.local_hostname.as_deref().map(Self::to_cstring),
script,
network_configuration_handler: self.network_configuration_handler,
interface: self.interface.as_deref().map(Self::to_cstring),
script_tun: self.script_tun,
#[cfg(unix)]
script_tun_launch: self.script_tun_launch,
#[cfg(windows)]
script_tun_pipe: self.script_tun_pipe,
certificate: self.certificate.as_deref().map(Self::to_cstring),
sslkey: self.sslkey.as_deref().map(Self::to_cstring),
key_password: self.key_password.as_deref().map(Self::to_cstring),
servercert: None,
hip_source: self.hip_source,
reconnect_timeout: self.reconnect_timeout,
mtu: self.mtu,
disable_ipv6: self.disable_ipv6,
gateway_ip_preference: self.gateway_ip_preference,
no_dtls: self.no_dtls,
dpd_interval: self.dpd_interval,
no_xmlpost: self.no_xmlpost,
callback: Default::default(),
hip_report_callback: Default::default(),
tunnel_callback: Default::default(),
observed_transport: Default::default(),
})
}
fn to_cstring(value: &str) -> CString {
CString::new(value.to_string()).expect("Failed to convert to CString")
}
}
fn shell_quote_path(path: &str) -> String {
format!("'{}'", path.replace('\'', "'\\''"))
}
#[cfg(test)]
mod tests {
use super::*;
use std::ffi::CString;
#[test]
fn quotes_vpnc_script_path_for_the_shell() {
assert_eq!(
shell_quote_path("/Applications/GP Connect.app/Contents/Helpers/vpnc-script"),
"'/Applications/GP Connect.app/Contents/Helpers/vpnc-script'"
);
assert_eq!(shell_quote_path("/tmp/user's script"), "'/tmp/user'\\''s script'");
}
#[test]
fn preserves_vpnc_script_commands() {
let command = "\"/tmp/script with spaces\" --option";
let vpn = Vpn::builder("vpn.example.com", "cookie")
.script(command.to_string())
.build()
.unwrap();
assert_eq!(vpn.script.as_ref().unwrap().to_str().unwrap(), command);
}
#[test]
fn quotes_explicit_vpnc_script_paths() {
let vpn = Vpn::builder("vpn.example.com", "cookie")
.script_path("/bin/sh".to_string())
.build()
.unwrap();
assert_eq!(vpn.script.as_ref().unwrap().to_str().unwrap(), "'/bin/sh'");
}
#[test]
fn maps_session_info_from_callback_payload() {
let message = CString::new("Session expires soon").unwrap();
let connected_gw = CString::new("10.1.2.3").unwrap();
let raw = ffi::VpnSessionInfoRaw {
auth_expiration: 0,
lifetime_secs: 43_200,
user_expires: 1_776_828_409,
lifetime_warning_prior: 1_800,
lifetime_warning_message: message.as_ptr(),
nlb_enabled: 1,
nlb_connected_gw_ip: connected_gw.as_ptr(),
};
let info = session_info_from_raw(&raw);
assert_eq!(info.lifetime_secs, Some(43_200));
assert_eq!(info.user_expires, Some(1_776_828_409));
assert_eq!(
info.lifetime_warning,
Some(VpnSessionWarning {
prior_secs: 1_800,
message: "Session expires soon".to_string(),
})
);
assert!(info.nlb_enabled);
assert_eq!(info.nlb_connected_gw_ip.as_deref(), Some("10.1.2.3"));
}
#[test]
fn falls_back_to_auth_expiration_when_user_expires_is_absent() {
let raw = ffi::VpnSessionInfoRaw {
auth_expiration: 1_776_828_409,
lifetime_secs: 0,
user_expires: 0,
lifetime_warning_prior: 0,
lifetime_warning_message: std::ptr::null(),
nlb_enabled: 0,
nlb_connected_gw_ip: std::ptr::null(),
};
let info = session_info_from_raw(&raw);
assert_eq!(info.user_expires, Some(1_776_828_409));
assert_eq!(info.lifetime_secs, None);
assert_eq!(info.lifetime_warning, None);
}
#[test]
fn connect_options_include_host_id() {
let vpn = Vpn {
cancellation: Cancellation::default(),
server: CString::new("gateway.example.com").unwrap(),
cookie: CString::new("cookie").unwrap(),
user_agent: CString::new("agent").unwrap(),
os: CString::new("linux").unwrap(),
os_version: None,
client_version: None,
host_id: Some(CString::new("profile-host-id").unwrap()),
local_hostname: None,
script: Some(CString::new("/bin/true").unwrap()),
network_configuration_handler: None,
interface: None,
script_tun: false,
#[cfg(unix)]
script_tun_launch: None,
#[cfg(windows)]
script_tun_pipe: None,
certificate: None,
sslkey: None,
key_password: None,
servercert: None,
hip_source: Arc::new(HipSource::Disabled),
reconnect_timeout: 300,
mtu: 0,
disable_ipv6: false,
gateway_ip_preference: GatewayIpPreference::Auto,
no_dtls: false,
dpd_interval: 0,
no_xmlpost: false,
callback: Default::default(),
hip_report_callback: Default::default(),
tunnel_callback: Default::default(),
observed_transport: Default::default(),
};
let options = vpn.build_connect_options();
let host_id = unsafe { CStr::from_ptr(options.host_id) }.to_str().unwrap();
assert_eq!(host_id, "profile-host-id");
}
#[test]
fn gateway_preference_and_tunnel_ipv6_are_independent_in_native_options() {
for preference in [
GatewayIpPreference::Auto,
GatewayIpPreference::Ipv4,
GatewayIpPreference::Ipv6,
] {
for disable_ipv6 in [false, true] {
let vpn = Vpn::builder("https://gateway.example", "fixture")
.script_path("/bin/sh".to_string())
.disable_ipv6(disable_ipv6)
.gateway_ip_preference(preference)
.build()
.unwrap();
let options = vpn.build_connect_options();
assert_eq!(options.gateway_ip_preference, preference as u32);
assert_eq!(options.disable_ipv6, disable_ipv6 as u32);
}
}
}
#[test]
fn hip_report_callback_copies_only_bounded_utf8() {
let vpn = Vpn::builder("vpn.example.com", "cookie")
.script_path("/bin/sh".to_string())
.build()
.unwrap();
let reports = Arc::new(Mutex::new(Vec::new()));
let received = Arc::clone(&reports);
vpn.set_hip_report_callback(move |report| received.lock().unwrap().push(report.to_owned()));
let options = vpn.build_connect_options();
let callback = options.on_hip_report_submitted.unwrap();
callback(options.user_data, b"<report/>".as_ptr().cast(), 9);
callback(options.user_data, b"\xff".as_ptr().cast(), 1);
callback(options.user_data, b"x".as_ptr().cast(), 1024 * 1024 + 1);
assert_eq!(*reports.lock().unwrap(), ["<report/>"]);
}
#[test]
fn disconnect_before_connect_is_retained_without_contacting_server() {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
listener.set_nonblocking(true).unwrap();
let vpn = Vpn::builder(&format!("https://{}", listener.local_addr().unwrap()), "test-cookie")
.script_path("/bin/sh".to_string())
.build()
.unwrap();
vpn.disconnect();
vpn.disconnect();
assert_ne!(vpn.connect(|_| panic!("Canceled attempt connected")), 0);
assert_eq!(listener.accept().unwrap_err().kind(), io::ErrorKind::WouldBlock);
}
#[test]
fn command_pipe_failure_preserves_stdin() {
use std::{
ffi::{c_int, c_void},
fs::File,
os::fd::AsFd,
os::unix::process::CommandExt,
process::{Command, Stdio},
};
const CHILD: &str = "OPENCONNECT_TEST_PIPE_FAILURE";
if std::env::var_os(CHILD).is_none() {
// Limit descriptors only in a subprocess, leaving the test runner untouched.
let mut child = Command::new(std::env::current_exe().unwrap());
child
.args([
"--exact",
"vpn::tests::command_pipe_failure_preserves_stdin",
"--nocapture",
])
.env(CHILD, "1")
.stdin(Stdio::null());
// Launch directly: system shells can strip the dynamic-library search
// environment required by the replaceable macOS OpenConnect library.
unsafe {
child.pre_exec(|| {
let limit = libc::rlimit {
rlim_cur: 64,
rlim_max: 64,
};
if libc::setrlimit(libc::RLIMIT_NOFILE, &limit) == 0 {
Ok(())
} else {
Err(io::Error::last_os_error())
}
});
}
let output = child.output().unwrap();
assert!(
output.status.success(),
"{}\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
return;
}
unsafe extern "C" {
fn openconnect_vpninfo_new(
agent: *const c_char,
validate: Option<unsafe extern "C" fn(*mut c_void, *const c_char) -> c_int>,
config: Option<unsafe extern "C" fn(*mut c_void, *const c_char, c_int) -> c_int>,
auth: Option<unsafe extern "C" fn(*mut c_void, *mut c_void) -> c_int>,
progress: Option<unsafe extern "C" fn(*mut c_void, c_int, *const c_char, ...)>,
data: *mut c_void,
) -> *mut c_void;
fn openconnect_setup_cmd_pipe(info: *mut c_void) -> c_int;
fn openconnect_vpninfo_free(info: *mut c_void);
}
// Construct first so resource exhaustion targets pipe creation specifically.
let info = unsafe { openconnect_vpninfo_new(c"test".as_ptr(), None, None, None, None, std::ptr::null_mut()) };
assert!(!info.is_null());
let mut descriptors = Vec::new();
loop {
match File::open("/dev/null") {
Ok(file) => descriptors.push(file),
Err(error) => {
assert_eq!(error.raw_os_error(), Some(24)); // EMFILE on supported Unix platforms.
break;
}
}
}
assert!(unsafe { openconnect_setup_cmd_pipe(info) } < 0);
unsafe { openconnect_vpninfo_free(info) };
// Constructor resource acquisition may also fail before selecting a protocol.
let info = unsafe { openconnect_vpninfo_new(c"test".as_ptr(), None, None, None, None, std::ptr::null_mut()) };
if !info.is_null() {
unsafe { openconnect_vpninfo_free(info) };
}
drop(descriptors);
std::io::stdin()
.as_fd()
.try_clone_to_owned()
.expect("Cleanup closed stdin");
}
#[test]
fn disconnect_interrupts_in_progress_tls_and_cannot_cancel_another_attempt() {
use std::{io::Read, sync::mpsc, thread, time::Duration};
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
listener.set_nonblocking(true).unwrap();
let vpn = Arc::new(
Vpn::builder(&format!("https://{}", listener.local_addr().unwrap()), "test-cookie")
.script_path("/bin/sh".to_string())
.build()
.unwrap(),
);
let (tx, rx) = mpsc::channel();
let worker_vpn = vpn.clone();
let worker = thread::spawn(move || {
tx.send(worker_vpn.connect(|_| panic!("Test peer never completes TLS")))
.unwrap();
});
let deadline = std::time::Instant::now() + Duration::from_secs(3);
let peer = loop {
match listener.accept() {
Ok((peer, _)) => break peer,
Err(error) if error.kind() == io::ErrorKind::WouldBlock && std::time::Instant::now() < deadline => {
thread::sleep(Duration::from_millis(5));
}
other => {
vpn.disconnect();
worker.join().unwrap();
panic!("Connection did not reach test peer: {other:?}");
}
}
};
vpn.disconnect();
let result = rx.recv_timeout(Duration::from_secs(2));
drop(peer); // Release the peer even if cancellation regresses.
worker.join().unwrap();
assert_ne!(result.expect("Cancellation did not interrupt TLS"), 0);
use std::os::{fd::AsRawFd, unix::net::UnixStream};
let next = Cancellation::default();
let (mut reader, writer) = UnixStream::pair().unwrap();
reader.set_nonblocking(true).unwrap();
assert!(!next.attach(writer.as_raw_fd()));
vpn.disconnect(); // A late stop of the old attempt cannot affect the new one.
assert_eq!(reader.read(&mut [0]).unwrap_err().kind(), io::ErrorKind::WouldBlock);
next.detach();
}
#[test]
fn cancellation_racing_pipe_attachment_is_never_lost() {
use std::{
io::Read,
os::{fd::AsRawFd, unix::net::UnixStream},
thread,
};
for _ in 0..100 {
let cancellation = Cancellation::default();
let (mut reader, writer) = UnixStream::pair().unwrap();
reader.set_nonblocking(true).unwrap();
let pending = thread::scope(|scope| {
let cancel = scope.spawn(|| cancellation.cancel().unwrap());
let pending = cancellation.attach(writer.as_raw_fd());
cancel.join().unwrap();
pending
});
if !pending {
let mut command = [0];
assert_eq!(reader.read(&mut command).unwrap(), 1);
assert_eq!(command[0], b'x');
}
cancellation.detach();
}
}
#[test]
fn disconnect_after_detach_never_writes_to_the_old_descriptor() {
use std::{
io::Read,
os::{fd::AsRawFd, unix::net::UnixStream},
};
let cancellation = Cancellation::default();
let (mut reader, writer) = UnixStream::pair().unwrap();
reader.set_nonblocking(true).unwrap();
assert!(!cancellation.attach(writer.as_raw_fd()));
cancellation.detach();
cancellation.cancel().unwrap();
assert_eq!(reader.read(&mut [0]).unwrap_err().kind(), io::ErrorKind::WouldBlock);
}
#[test]
fn statistics_are_coalesced_and_fenced_by_cancellation_and_detach() {
use std::{
io::Read,
os::{fd::AsRawFd, unix::net::UnixStream},
};
let cancellation = Cancellation::default();
let (mut reader, writer) = UnixStream::pair().unwrap();
reader.set_nonblocking(true).unwrap();
assert!(!cancellation.request_statistics().unwrap());
assert!(!cancellation.attach(writer.as_raw_fd()));
assert!(cancellation.request_statistics().unwrap());
assert!(!cancellation.request_statistics().unwrap());
let mut command = [0];
reader.read_exact(&mut command).unwrap();
assert_eq!(command, [b's']);
cancellation.statistics_received();
assert!(cancellation.request_statistics().unwrap());
reader.read_exact(&mut command).unwrap();
cancellation.cancel().unwrap();
assert!(!cancellation.request_statistics().unwrap());
reader.read_exact(&mut command).unwrap();
assert_eq!(command, [b'x']);
cancellation.detach();
assert!(!cancellation.state.lock().unwrap().statistics_pending);
assert!(!cancellation.request_statistics().unwrap());
assert_eq!(reader.read(&mut command).unwrap_err().kind(), io::ErrorKind::WouldBlock);
}
#[test]
fn full_command_pipe_does_not_latch_a_statistics_request() {
use std::{
io::{Read, Write},
os::{fd::AsRawFd, unix::net::UnixStream},
};
let cancellation = Cancellation::default();
let (mut reader, mut writer) = UnixStream::pair().unwrap();
reader.set_nonblocking(true).unwrap();
writer.set_nonblocking(true).unwrap();
while writer.write(&[0; 1024]).is_ok() {}
while writer.write(&[0]).is_ok() {}
assert!(!cancellation.attach(writer.as_raw_fd()));
assert!(!cancellation.request_statistics().unwrap());
assert!(!cancellation.state.lock().unwrap().statistics_pending);
let mut bytes = [0; 4096];
while reader.read(&mut bytes).is_ok() {}
assert!(cancellation.request_statistics().unwrap());
cancellation.detach();
}
}
#[unsafe(no_mangle)]
#[cfg(unix)]
extern "C" fn vpn_script_started(data: *mut std::ffi::c_void) {
// Native setup has forked or failed; only the child may retain this endpoint.
let vpn = unsafe { &*(data as *const Vpn) };
if let Some(launch) = &vpn.script_tun_launch {
launch.control.lock().unwrap().take();
}
}