diff --git a/Cargo.lock b/Cargo.lock index 205fe22..1f5ced2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -720,6 +720,9 @@ dependencies = [ [[package]] name = "common" version = "3.0.0" +dependencies = [ + "tempfile", +] [[package]] name = "compile-time" diff --git a/README.md b/README.md index 82931fe..598de36 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # GlobalProtect-openconnect -A GlobalProtect VPN client built on OpenConnect with support for SSO authentication. It provides command-line tools for Linux, FreeBSD, and OpenBSD, and the GP Connect desktop application for macOS, Linux, FreeBSD, and OpenBSD. +A GlobalProtect VPN client built on OpenConnect with support for SSO authentication. It provides command-line tools and the GP Connect desktop application for macOS, Linux, FreeBSD, and OpenBSD.

@@ -134,7 +134,23 @@ gpclient launch-gui ### macOS -Download the GP Connect `.dmg` from the [releases](https://github.com/yuezk/GlobalProtect-openconnect/releases) page, open it, and drag GP Connect into Applications. The macOS build requires Apple silicon and macOS 13 or later. +GP Connect requires Apple silicon and macOS 13 or later. The macOS app includes both the desktop application and the free `gpclient` CLI. The CLI does not require a GUI license. + +#### Option 1: Install with Homebrew + +```bash +brew install --cask yuezk/gp-connect/gp-connect +``` + +Open GP Connect from Applications, or connect from the terminal: + +```bash +sudo gpclient connect --browser +``` + +#### Option 2: Install from DMG + +Download the GP Connect `.dmg` from the [releases](https://github.com/yuezk/GlobalProtect-openconnect/releases) page, open it, and drag GP Connect into Applications. ### Debian / Ubuntu diff --git a/apps/gpclient/src/connect/gateway.rs b/apps/gpclient/src/connect/gateway.rs index 1c06afe..f379a33 100644 --- a/apps/gpclient/src/connect/gateway.rs +++ b/apps/gpclient/src/connect/gateway.rs @@ -451,6 +451,15 @@ impl ConnectHandler<'_> { .local_hostname(self.args.local_hostname.clone()) .dpd_interval(self.args.dpd_interval.unwrap_or(0)) .no_xmlpost(self.args.no_xmlpost); + #[cfg(target_os = "macos")] + let vpn_builder = if self.args.script.is_none() + && std::env::var_os("GP_VPNC_SCRIPT").is_none_or(|value| value.is_empty()) + && let Some(script) = common::binary_paths::bundled_vpnc_script() + { + vpn_builder.script_path(script.to_string_lossy().into_owned()) + } else { + vpn_builder + }; let vpn = apply_os_profile(vpn_builder, &os_profile) .build() .map_err(|err| GatewayConnectError::before_establishment(err.into()))?; diff --git a/crates/auth/src/callback.rs b/crates/auth/src/callback.rs index 3cda767..a231a22 100644 --- a/crates/auth/src/callback.rs +++ b/crates/auth/src/callback.rs @@ -7,7 +7,7 @@ use std::{ }; use anyhow::{Context, bail}; -use gpapi::auth::SamlAuthData; +use gpapi::{auth::SamlAuthData, error::AuthDataParseError}; use tokio::{ io::{AsyncReadExt, AsyncWriteExt}, net::{UnixListener, UnixStream}, @@ -27,6 +27,7 @@ const PROTOCOL_VERSION: u8 = 1; const MAX_CALLBACK_SIZE: usize = 1024 * 1024; const IO_TIMEOUT: Duration = Duration::from_secs(5); +// Acceptance acknowledges delivery, including an authentication failure. const STATUS_ACCEPTED: u8 = 0; const STATUS_INVALID_CALLBACK: u8 = 1; const STATUS_UNSUPPORTED_VERSION: u8 = 2; @@ -70,6 +71,10 @@ impl AuthCallbackReceiver { let _ = write_response(&mut stream, STATUS_ACCEPTED).await; return Ok(auth_data); } + Err(error @ AuthDataParseError::AuthenticationFailed(_)) => { + let _ = write_response(&mut stream, STATUS_ACCEPTED).await; + return Err(error.into()); + } Err(_) => { let _ = write_response(&mut stream, STATUS_INVALID_CALLBACK).await; } @@ -419,6 +424,24 @@ mod tests { assert!(!socket_path.exists()); } + #[tokio::test] + async fn reports_authentication_failure_to_waiting_receiver() { + let directory = tempdir().unwrap(); + let socket_path = directory.path().join("callback.sock"); + let receiver = AuthCallbackReceiver::bind_at(socket_path.clone()).unwrap(); + let failure = gpapi::utils::base64::encode(b"0"); + let callback = format!("globalprotectcallback:{failure}"); + let sender = tokio::spawn(async move { forward_auth_callback_to(&callback, &socket_path).await }); + + let error = timeout(Duration::from_millis(500), receiver.receive()) + .await + .expect("Failure callback must finish the waiting authentication") + .unwrap_err(); + + assert!(error.to_string().contains("Authentication failed")); + sender.await.unwrap().expect("Failure callback was delivered"); + } + #[tokio::test] async fn rejects_bad_requests_and_keeps_waiting() { let directory = tempdir().unwrap(); @@ -430,6 +453,10 @@ mod tests { send_raw_callback(&socket_path, PROTOCOL_VERSION, "https://example.com").await, STATUS_INVALID_CALLBACK ); + assert_eq!( + send_raw_callback(&socket_path, PROTOCOL_VERSION, "globalprotectcallback:not-base64").await, + STATUS_INVALID_CALLBACK + ); assert_eq!( send_raw_callback(&socket_path, PROTOCOL_VERSION + 1, VALID_CALLBACK).await, STATUS_UNSUPPORTED_VERSION diff --git a/crates/auth/src/webview/auth_messenger.rs b/crates/auth/src/webview/auth_messenger.rs index 85567c9..379974c 100644 --- a/crates/auth/src/webview/auth_messenger.rs +++ b/crates/auth/src/webview/auth_messenger.rs @@ -150,8 +150,8 @@ impl AuthMessenger { match auth_result { Ok(data) => self.send_auth_data(data, AuthDataLocation::Body), - Err(AuthDataParseError::Invalid(err)) => self.send_auth_error(AuthError::invalid_from_body(err)), Err(AuthDataParseError::NotFound) => self.send_auth_error(AuthError::not_found_in_body()), + Err(err) => self.send_auth_error(AuthError::invalid_from_body(err.into())), } } diff --git a/crates/common/Cargo.toml b/crates/common/Cargo.toml index 9d90f8b..44f5473 100644 --- a/crates/common/Cargo.toml +++ b/crates/common/Cargo.toml @@ -6,3 +6,6 @@ authors.workspace = true homepage.workspace = true edition.workspace = true license = "MIT" + +[dev-dependencies] +tempfile.workspace = true diff --git a/crates/common/src/binary_paths.rs b/crates/common/src/binary_paths.rs index f36dc3a..2eb237c 100644 --- a/crates/common/src/binary_paths.rs +++ b/crates/common/src/binary_paths.rs @@ -3,6 +3,9 @@ use std::{ path::{Path, PathBuf}, }; +#[cfg(target_os = "macos")] +mod macos; + #[cfg(all( not(debug_assertions), any(target_os = "linux", target_os = "freebsd", target_os = "openbsd") @@ -80,13 +83,18 @@ fn resolve(env_key: &str, binary_name: &str, default_path: &str) -> PathBuf { fn sibling_binary(binary_name: &str) -> Option { let current_exe = env::current_exe().ok()?; - let bin_dir = current_exe.parent()?; + sibling_binary_for_executable(¤t_exe, binary_name) +} + +fn sibling_binary_for_executable(executable: &Path, binary_name: &str) -> Option { + let executable = executable.canonicalize().ok()?; + let bin_dir = executable.parent()?; let binary = bin_dir.join(binary_name); if is_file(&binary) { return Some(binary); } #[cfg(target_os = "macos")] - if let Some(binary) = bundled_helper_path(¤t_exe, binary_name) + if let Some(binary) = macos::helper_path(&executable, binary_name) && is_file(&binary) { return Some(binary); @@ -95,14 +103,14 @@ fn sibling_binary(binary_name: &str) -> Option { } #[cfg(target_os = "macos")] -fn bundled_helper_path(executable: &Path, binary_name: &str) -> Option { - let bin_dir = executable.parent()?; - let contents = bin_dir.parent()?; - let bundle = contents.parent()?; - if bin_dir.file_name()? != "MacOS" || contents.file_name()? != "Contents" || bundle.extension()? != "app" { - return None; - } - Some(contents.join("Helpers").join(binary_name)) +pub fn bundled_vpnc_script() -> Option { + bundled_vpnc_script_for_executable(&env::current_exe().ok()?) +} + +#[cfg(target_os = "macos")] +fn bundled_vpnc_script_for_executable(executable: &Path) -> Option { + let executable = executable.canonicalize().ok()?; + macos::vpnc_script_path(&executable) } fn is_file(path: &Path) -> bool { @@ -117,14 +125,44 @@ mod tests { #[cfg(target_os = "macos")] fn desktop_resolves_helpers_in_the_actual_app_bundle_layout() { assert_eq!( - bundled_helper_path( + macos::helper_path( Path::new("/Applications/GP Connect.app/Contents/MacOS/gpgui"), "gpclient" ), Some(PathBuf::from("/Applications/GP Connect.app/Contents/Helpers/gpclient")) ); - assert!(bundled_helper_path(Path::new("/usr/local/bin/gpgui"), "gpclient").is_none()); - assert!(bundled_helper_path(Path::new("/tmp/Contents/MacOS/gpgui"), "gpclient").is_none()); + assert!(macos::helper_path(Path::new("/usr/local/bin/gpgui"), "gpclient").is_none()); + assert!(macos::helper_path(Path::new("/tmp/Contents/MacOS/gpgui"), "gpclient").is_none()); + } + + #[test] + #[cfg(unix)] + fn symlinked_executable_resolves_its_bundle_paths() { + let directory = tempfile::tempdir().unwrap(); + let helpers = directory.path().join("GP Connect.app/Contents/Helpers"); + let bin = directory.path().join("bin"); + std::fs::create_dir_all(&helpers).unwrap(); + std::fs::create_dir_all(&bin).unwrap(); + std::fs::write(helpers.join("gpclient"), "").unwrap(); + std::fs::write(helpers.join("gpauth"), "").unwrap(); + std::os::unix::fs::symlink(helpers.join("gpclient"), bin.join("gpclient")).unwrap(); + + assert_eq!( + sibling_binary_for_executable(&bin.join("gpclient"), "gpauth"), + Some(helpers.canonicalize().unwrap().join("gpauth")) + ); + #[cfg(target_os = "macos")] + assert_eq!( + bundled_vpnc_script_for_executable(&bin.join("gpclient")), + Some( + helpers + .canonicalize() + .unwrap() + .parent() + .unwrap() + .join("Resources/Scripts/vpnc-script") + ) + ); } #[test] diff --git a/crates/common/src/binary_paths/macos.rs b/crates/common/src/binary_paths/macos.rs new file mode 100644 index 0000000..bd0b972 --- /dev/null +++ b/crates/common/src/binary_paths/macos.rs @@ -0,0 +1,45 @@ +use std::path::{Path, PathBuf}; + +fn contents_dir(executable: &Path) -> Option<&Path> { + let bin_dir = executable.parent()?; + let contents = bin_dir.parent()?; + let bundle = contents.parent()?; + let directory_name = bin_dir.file_name()?; + if (directory_name != "MacOS" && directory_name != "Helpers") + || contents.file_name()? != "Contents" + || bundle.extension()? != "app" + { + return None; + } + Some(contents) +} + +pub(super) fn helper_path(executable: &Path, binary_name: &str) -> Option { + Some(contents_dir(executable)?.join("Helpers").join(binary_name)) +} + +pub(super) fn vpnc_script_path(executable: &Path) -> Option { + Some(contents_dir(executable)?.join("Resources/Scripts/vpnc-script")) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn bundle_entry_points_use_their_own_vpnc_script() { + for executable in [ + "/Applications/GP Connect.app/Contents/Helpers/gpclient", + "/Applications/GP Connect.app/Contents/MacOS/gpgui", + ] { + assert_eq!( + vpnc_script_path(Path::new(executable)), + Some(PathBuf::from( + "/Applications/GP Connect.app/Contents/Resources/Scripts/vpnc-script" + )) + ); + } + assert!(vpnc_script_path(Path::new("/opt/homebrew/bin/gpclient")).is_none()); + assert!(vpnc_script_path(Path::new("/tmp/Contents/Helpers/gpclient")).is_none()); + } +} diff --git a/crates/gpapi/src/auth.rs b/crates/gpapi/src/auth.rs index b889889..5250f21 100644 --- a/crates/gpapi/src/auth.rs +++ b/crates/gpapi/src/auth.rs @@ -130,10 +130,7 @@ impl SamlAuthData { Ok(auth_data) } - Some(status) => Err(AuthDataParseError::Invalid(anyhow::anyhow!( - "SAML auth status: {}", - status - ))), + Some(status) => Err(AuthDataParseError::AuthenticationFailed(status)), None => Err(AuthDataParseError::NotFound), } } diff --git a/crates/gpapi/src/error.rs b/crates/gpapi/src/error.rs index 505177c..a87ad45 100644 --- a/crates/gpapi/src/error.rs +++ b/crates/gpapi/src/error.rs @@ -29,12 +29,17 @@ impl PortalError { pub enum AuthDataParseError { #[error("No auth data found")] NotFound, + #[error("Authentication failed (SAML status: {0})")] + AuthenticationFailed(String), #[error(transparent)] Invalid(#[from] anyhow::Error), } impl AuthDataParseError { pub fn is_invalid(&self) -> bool { - matches!(self, AuthDataParseError::Invalid(_)) + matches!( + self, + AuthDataParseError::Invalid(_) | AuthDataParseError::AuthenticationFailed(_) + ) } }