diff --git a/Makefile b/Makefile index 1a542a7..9e40441 100644 --- a/Makefile +++ b/Makefile @@ -164,7 +164,6 @@ install: install -Dm755 packaging/files/usr/lib/NetworkManager/dispatcher.d/gpclient-nm-hook $(DESTDIR)/usr/lib/NetworkManager/dispatcher.d/gpclient-nm-hook install -Dm644 packaging/files/usr/share/applications/gpgui.desktop $(DESTDIR)/usr/share/applications/gpgui.desktop - install -Dm644 packaging/files/usr/share/applications/gpauth.desktop $(DESTDIR)/usr/share/applications/gpauth.desktop install -Dm644 packaging/files/usr/share/icons/hicolor/scalable/apps/gpgui.svg $(DESTDIR)/usr/share/icons/hicolor/scalable/apps/gpgui.svg install -Dm644 packaging/files/usr/share/icons/hicolor/32x32/apps/gpgui.png $(DESTDIR)/usr/share/icons/hicolor/32x32/apps/gpgui.png install -Dm644 packaging/files/usr/share/icons/hicolor/128x128/apps/gpgui.png $(DESTDIR)/usr/share/icons/hicolor/128x128/apps/gpgui.png @@ -195,7 +194,6 @@ install-bsd: install -d $(DESTDIR)$(PREFIX)/share/applications install -m 644 packaging/bsd/gpgui.desktop $(DESTDIR)$(PREFIX)/share/applications/gpgui.desktop - install -m 644 packaging/bsd/gpauth.desktop $(DESTDIR)$(PREFIX)/share/applications/gpauth.desktop install -d $(DESTDIR)$(PREFIX)/share/icons/hicolor/scalable/apps install -d $(DESTDIR)$(PREFIX)/share/icons/hicolor/32x32/apps install -d $(DESTDIR)$(PREFIX)/share/icons/hicolor/128x128/apps @@ -294,7 +292,6 @@ uninstall: rm -f $(DESTDIR)/usr/lib/NetworkManager/dispatcher.d/gpclient-nm-hook rm -f $(DESTDIR)/usr/share/applications/gpgui.desktop - rm -f $(DESTDIR)/usr/share/applications/gpauth.desktop rm -f $(DESTDIR)/usr/share/icons/hicolor/scalable/apps/gpgui.svg rm -f $(DESTDIR)/usr/share/icons/hicolor/32x32/apps/gpgui.png rm -f $(DESTDIR)/usr/share/icons/hicolor/128x128/apps/gpgui.png diff --git a/apps/gpclient/src/cli.rs b/apps/gpclient/src/cli.rs index d8c26c0..31b0ba7 100644 --- a/apps/gpclient/src/cli.rs +++ b/apps/gpclient/src/cli.rs @@ -102,6 +102,14 @@ impl Args for Cli { } impl Cli { + fn can_run_alongside_client(&self) -> bool { + match &self.command { + CliCommand::Disconnect(_) => true, + CliCommand::LaunchGui(args) => args.is_auth_callback(), + _ => false, + } + } + async fn is_running(&self) -> bool { let Ok(c) = fs::read_to_string(&self.lock_file).await else { return false; @@ -132,7 +140,7 @@ impl Cli { async fn run(&self) -> anyhow::Result<()> { // check if an instance is running - if !matches!(self.command, CliCommand::Disconnect(_)) && self.is_running().await { + if !self.can_run_alongside_client() && self.is_running().await { bail!("Another instance of the client is already running"); } @@ -203,4 +211,33 @@ mod tests { assert_eq!(cli.lock_file, PathBuf::from("/tmp/gpclient-portal.lock")); } + + #[test] + #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))] + fn launch_gui_accepts_auth_callback() { + let cli = Cli::try_parse_from([ + "gpclient", + "launch-gui", + "globalprotectcallback:cas-as=1&un=alice@example.com&token=token", + ]) + .expect("authentication callback should parse"); + + assert!(matches!( + &cli.command, + CliCommand::LaunchGui(args) if args.is_auth_callback() + )); + assert!(cli.can_run_alongside_client()); + } + + #[test] + #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))] + fn launch_gui_rejects_callback_with_minimized() { + assert!(Cli::try_parse_from(["gpclient", "launch-gui", "globalprotectcallback:token", "--minimized",]).is_err()); + } + + #[test] + #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))] + fn launch_gui_rejects_non_callback_positional() { + assert!(Cli::try_parse_from(["gpclient", "launch-gui", "https://example.com/callback"]).is_err()); + } } diff --git a/apps/gpclient/src/launch_gui.rs b/apps/gpclient/src/launch_gui.rs index 58bdf40..8f13112 100644 --- a/apps/gpclient/src/launch_gui.rs +++ b/apps/gpclient/src/launch_gui.rs @@ -1,6 +1,15 @@ use std::{collections::HashMap, fs, path::PathBuf}; +#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd", test))] +use std::process::{Command, ExitStatus}; + +#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))] +use anyhow::Context; +#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd", test))] +use anyhow::bail; use clap::Args; +#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd", test))] +use common::binary_paths; use directories::ProjectDirs; use gpapi::{ process::service_launcher::ServiceLauncher, @@ -10,10 +19,30 @@ use log::info; #[derive(Args)] pub(crate) struct LaunchGuiArgs { + #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))] + #[arg( + required = false, + conflicts_with = "minimized", + value_parser = parse_auth_callback, + help = "The external-browser authentication callback URL" + )] + callback: Option, #[arg(long, help = "Launch the GUI minimized")] minimized: bool, } +impl LaunchGuiArgs { + pub(crate) fn is_auth_callback(&self) -> bool { + #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))] + { + return self.callback.is_some(); + } + + #[cfg(not(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd")))] + false + } +} + pub(crate) struct LaunchGuiHandler<'a> { args: &'a LaunchGuiArgs, } @@ -30,6 +59,11 @@ impl<'a> LaunchGuiHandler<'a> { anyhow::bail!("`launch-gui` cannot be run as root"); } + #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))] + if let Some(callback) = &self.args.callback { + return forward_auth_callback(callback); + } + if try_active_gui().await.is_ok() { info!("The GUI is already running"); return Ok(()); @@ -66,6 +100,38 @@ impl<'a> LaunchGuiHandler<'a> { } } +#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))] +fn forward_auth_callback(callback: &str) -> anyhow::Result<()> { + let status = auth_callback_command(callback) + .status() + .context("Failed to start the authentication callback handler")?; + ensure_auth_callback_succeeded(status) +} + +#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd", test))] +fn auth_callback_command(callback: &str) -> Command { + let mut command = Command::new(binary_paths::gpauth()); + command.args(["auth-callback", callback]); + command +} + +#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd", test))] +fn ensure_auth_callback_succeeded(status: ExitStatus) -> anyhow::Result<()> { + if !status.success() { + bail!("Authentication callback handler exited with {status}"); + } + + Ok(()) +} + +#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd", test))] +fn parse_auth_callback(callback: &str) -> Result { + callback + .starts_with("globalprotectcallback:") + .then(|| callback.to_owned()) + .ok_or_else(|| "callback URL must use the globalprotectcallback scheme".to_owned()) +} + async fn try_active_gui() -> anyhow::Result<()> { let service_endpoint = http_endpoint().await?; @@ -86,3 +152,34 @@ fn get_log_file() -> anyhow::Result { Ok(dirs.data_dir().join("gpclient.log")) } + +#[cfg(all(test, unix))] +mod tests { + use std::os::unix::process::ExitStatusExt; + + use super::*; + + const CALLBACK: &str = "globalprotectcallback:cas-as=1&un=alice@example.com&token=token"; + + #[test] + fn callback_command_preserves_callback_argument() { + let command = auth_callback_command(CALLBACK); + let args = command + .get_args() + .map(|argument| argument.to_str().unwrap()) + .collect::>(); + + assert_eq!(args, ["auth-callback", CALLBACK]); + } + + #[test] + fn callback_command_status_is_propagated() { + assert!(ensure_auth_callback_succeeded(ExitStatus::from_raw(0)).is_ok()); + assert!(ensure_auth_callback_succeeded(ExitStatus::from_raw(1 << 8)).is_err()); + } + + #[test] + fn rejects_non_callback_url() { + assert!(parse_auth_callback("https://example.com/callback").is_err()); + } +} diff --git a/crates/auth/src/callback.rs b/crates/auth/src/callback.rs index e900dfa..829f82a 100644 --- a/crates/auth/src/callback.rs +++ b/crates/auth/src/callback.rs @@ -97,7 +97,7 @@ pub async fn forward_auth_callback(callback: &str) -> anyhow::Result<()> { #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))] pub fn ensure_auth_callback_handler() -> anyhow::Result<()> { const MIME_TYPE: &str = "x-scheme-handler/globalprotectcallback"; - const HANDLER: &str = "gpauth.desktop"; + const HANDLER: &str = "gpgui.desktop"; let current_handler = query_callback_handler(MIME_TYPE)?; if !should_register_handler(¤t_handler) { @@ -147,7 +147,7 @@ pub fn ensure_auth_callback_handler() -> anyhow::Result<()> { #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd", test))] fn should_register_handler(current_handler: &str) -> bool { - current_handler != "gpauth.desktop" + current_handler != "gpgui.desktop" } async fn forward_auth_callback_to(callback: &str, socket_path: &Path) -> anyhow::Result<()> { @@ -334,9 +334,9 @@ mod tests { #[test] fn replaces_any_other_callback_handler() { - assert!(!should_register_handler("gpauth.desktop")); + assert!(!should_register_handler("gpgui.desktop")); assert!(should_register_handler("")); - assert!(should_register_handler("gpgui.desktop")); + assert!(should_register_handler("gpauth.desktop")); assert!(should_register_handler("other-vpn.desktop")); } diff --git a/flake.nix b/flake.nix index 4d1fe57..1914671 100644 --- a/flake.nix +++ b/flake.nix @@ -98,9 +98,6 @@ substituteInPlace $out/share/applications/gpgui.desktop \ --replace-fail /usr/bin/gpclient $out/bin/gpclient - substituteInPlace $out/share/applications/gpauth.desktop \ - --replace-fail /usr/bin/gpauth $out/bin/gpauth - substituteInPlace $out/share/polkit-1/actions/com.yuezk.gpgui.policy \ --replace-fail /usr/bin/gpservice $out/bin/gpservice \ --replace-fail /usr/libexec/gpclient/gp-vpnc-script-installer $out/bin/gp-vpnc-script-installer @@ -115,9 +112,6 @@ substituteInPlace $out/share/applications/gpgui.desktop \ --replace-fail /usr/bin/gpclient $out/bin/gpclient - substituteInPlace $out/share/applications/gpauth.desktop \ - --replace-fail /usr/bin/gpauth $out/bin/gpauth - substituteInPlace $out/share/polkit-1/actions/com.yuezk.gpgui.policy \ --replace-fail /usr/bin/gpservice $out/bin/gpservice \ --replace-fail /usr/libexec/gpclient/gp-vpnc-script-installer ${prebuiltFiles}/libexec/gpclient/gp-vpnc-script-installer @@ -342,16 +336,25 @@ gpclient_fhs='${prebuiltCommands.gpclient}/bin/gpclient' gpservice_public='@gpservice_public@' + gpauth_public='@gpauth_public@' if [ "''${1:-}" = "launch-gui" ]; then shift minimized= + callback= for arg in "$@"; do case "$arg" in --minimized) minimized=--minimized ;; + globalprotectcallback:*) + if [ -n "$callback" ]; then + echo "Multiple authentication callback URLs were provided" >&2 + exit 2 + fi + callback=$arg + ;; *) echo "Unsupported launch-gui argument: $arg" >&2 exit 2 @@ -359,6 +362,14 @@ esac done + if [ -n "$callback" ]; then + if [ -n "$minimized" ]; then + echo "An authentication callback cannot be combined with --minimized" >&2 + exit 2 + fi + exec "$gpauth_public" auth-callback "$callback" + fi + if [ -n "''${XDG_DATA_HOME:-}" ]; then data_home=$XDG_DATA_HOME elif [ -n "''${HOME:-}" ]; then @@ -395,7 +406,8 @@ exec "$gpclient_fhs" "$@" EOF substituteInPlace $out/bin/gpclient \ - --replace-fail '@gpservice_public@' "$out/bin/gpservice" + --replace-fail '@gpservice_public@' "$out/bin/gpservice" \ + --replace-fail '@gpauth_public@' "$out/bin/gpauth" chmod +x $out/bin/gpclient cat > $out/bin/gpservice <<'EOF' diff --git a/packaging/binary/Makefile.in b/packaging/binary/Makefile.in index d7d752e..f969699 100644 --- a/packaging/binary/Makefile.in +++ b/packaging/binary/Makefile.in @@ -19,7 +19,6 @@ install: install -Dm755 artifacts/usr/lib/NetworkManager/dispatcher.d/gpclient-nm-hook $(DESTDIR)/usr/lib/NetworkManager/dispatcher.d/gpclient-nm-hook install -Dm644 artifacts/usr/share/applications/gpgui.desktop $(DESTDIR)/usr/share/applications/gpgui.desktop - install -Dm644 artifacts/usr/share/applications/gpauth.desktop $(DESTDIR)/usr/share/applications/gpauth.desktop install -Dm644 artifacts/usr/share/icons/hicolor/scalable/apps/gpgui.svg $(DESTDIR)/usr/share/icons/hicolor/scalable/apps/gpgui.svg install -Dm644 artifacts/usr/share/icons/hicolor/32x32/apps/gpgui.png $(DESTDIR)/usr/share/icons/hicolor/32x32/apps/gpgui.png install -Dm644 artifacts/usr/share/icons/hicolor/128x128/apps/gpgui.png $(DESTDIR)/usr/share/icons/hicolor/128x128/apps/gpgui.png @@ -44,7 +43,6 @@ uninstall: rm -f $(DESTDIR)/usr/lib/NetworkManager/dispatcher.d/gpclient-nm-hook rm -f $(DESTDIR)/usr/share/applications/gpgui.desktop - rm -f $(DESTDIR)/usr/share/applications/gpauth.desktop rm -f $(DESTDIR)/usr/share/icons/hicolor/scalable/apps/gpgui.svg rm -f $(DESTDIR)/usr/share/icons/hicolor/32x32/apps/gpgui.png rm -f $(DESTDIR)/usr/share/icons/hicolor/128x128/apps/gpgui.png diff --git a/packaging/bsd/gpauth.desktop b/packaging/bsd/gpauth.desktop deleted file mode 100644 index 70e0062..0000000 --- a/packaging/bsd/gpauth.desktop +++ /dev/null @@ -1,11 +0,0 @@ -[Desktop Entry] -Type=Application -Name=GP Connect Login -Comment=Authenticate GP Connect VPN connections -NoDisplay=true -Terminal=false -TryExec=/usr/local/bin/gpauth -Exec=/usr/local/bin/gpauth auth-callback %u -MimeType=x-scheme-handler/globalprotectcallback; -Icon=gpgui -Categories=Network; diff --git a/packaging/bsd/gpgui.desktop b/packaging/bsd/gpgui.desktop index 0ffb1c6..5e18b01 100644 --- a/packaging/bsd/gpgui.desktop +++ b/packaging/bsd/gpgui.desktop @@ -4,6 +4,7 @@ Name=GP Connect Comment=A GUI client for GlobalProtect VPN GenericName=GlobalProtect VPN Client Categories=Network;Dialup; -Exec=/usr/local/bin/gpclient launch-gui +Exec=/usr/local/bin/gpclient launch-gui %u +MimeType=x-scheme-handler/globalprotectcallback; Icon=gpgui Keywords=GlobalProtect;Openconnect;SAML;connection;VPN; diff --git a/packaging/files/usr/share/applications/gpauth.desktop b/packaging/files/usr/share/applications/gpauth.desktop deleted file mode 100644 index 22669c7..0000000 --- a/packaging/files/usr/share/applications/gpauth.desktop +++ /dev/null @@ -1,11 +0,0 @@ -[Desktop Entry] -Type=Application -Name=GP Connect Login -Comment=Authenticate GP Connect VPN connections -NoDisplay=true -Terminal=false -TryExec=/usr/bin/gpauth -Exec=/usr/bin/gpauth auth-callback %u -MimeType=x-scheme-handler/globalprotectcallback; -Icon=gpgui -Categories=Network; diff --git a/packaging/files/usr/share/applications/gpgui.desktop b/packaging/files/usr/share/applications/gpgui.desktop index 19447cc..40bf13c 100644 --- a/packaging/files/usr/share/applications/gpgui.desktop +++ b/packaging/files/usr/share/applications/gpgui.desktop @@ -5,6 +5,7 @@ Name=GP Connect Comment=A GUI client for GlobalProtect VPN GenericName=GlobalProtect VPN Client Categories=Network;Dialup; -Exec=/usr/bin/gpclient launch-gui +Exec=/usr/bin/gpclient launch-gui %u +MimeType=x-scheme-handler/globalprotectcallback; Icon=gpgui Keywords=GlobalProtect;Openconnect;SAML;connection;VPN; diff --git a/packaging/rpm/globalprotect-openconnect.spec.in b/packaging/rpm/globalprotect-openconnect.spec.in index ea92874..752b9e2 100644 --- a/packaging/rpm/globalprotect-openconnect.spec.in +++ b/packaging/rpm/globalprotect-openconnect.spec.in @@ -75,7 +75,6 @@ make build %{_libexecdir}/gpclient/hipreport.sh %{_libexecdir}/gpclient/gp-vpnc-script-installer %{_datadir}/applications/gpgui.desktop -%{_datadir}/applications/gpauth.desktop %{_datadir}/icons/hicolor/32x32/apps/gpgui.png %{_datadir}/icons/hicolor/128x128/apps/gpgui.png %{_datadir}/icons/hicolor/256x256/apps/gpgui.png diff --git a/scripts/verify-linux-desktop-assets.sh b/scripts/verify-linux-desktop-assets.sh index cfb4b72..bb0d77e 100755 --- a/scripts/verify-linux-desktop-assets.sh +++ b/scripts/verify-linux-desktop-assets.sh @@ -22,6 +22,25 @@ verify_same_file() { fi } +verify_contains() { + local file="$1" + local expected="$2" + + if ! grep -Fqx "$expected" "$file"; then + echo "Linux desktop asset is missing '$expected': $file" >&2 + exit 1 + fi +} + +verify_absent() { + local file="$1" + + if [[ -e "$file" ]]; then + echo "Obsolete Linux desktop asset is still present: $file" >&2 + exit 1 + fi +} + verify_same_file \ "$GPGUI_DIR/app/src-tauri/icons/32x32.png" \ "$GP_DIR/packaging/files/usr/share/icons/hicolor/32x32/apps/gpgui.png" @@ -73,8 +92,26 @@ verify_same_file \ verify_same_file \ "$GPGUI_DIR/app/src-tauri/icons/icon-small.svg" \ "$GP_DIR/apps/gpgui-helper/src/assets/icon-small.svg" -verify_same_file \ - "$GPGUI_DIR/app/gpauth.desktop" \ - "$GP_DIR/packaging/files/usr/share/applications/gpauth.desktop" +verify_contains \ + "$GPGUI_DIR/app/gpgui.desktop.in" \ + "Exec=/usr/bin/gpclient launch-gui %u" +verify_contains \ + "$GPGUI_DIR/app/gpgui.desktop.in" \ + "MimeType=x-scheme-handler/globalprotectcallback;" +verify_contains \ + "$GP_DIR/packaging/files/usr/share/applications/gpgui.desktop" \ + "Exec=/usr/bin/gpclient launch-gui %u" +verify_contains \ + "$GP_DIR/packaging/files/usr/share/applications/gpgui.desktop" \ + "MimeType=x-scheme-handler/globalprotectcallback;" +verify_contains \ + "$GP_DIR/packaging/bsd/gpgui.desktop" \ + "Exec=/usr/local/bin/gpclient launch-gui %u" +verify_contains \ + "$GP_DIR/packaging/bsd/gpgui.desktop" \ + "MimeType=x-scheme-handler/globalprotectcallback;" +verify_absent "$GPGUI_DIR/app/gpauth.desktop" +verify_absent "$GP_DIR/packaging/files/usr/share/applications/gpauth.desktop" +verify_absent "$GP_DIR/packaging/bsd/gpauth.desktop" echo "Linux desktop assets are synchronized"