use std::{ fs::{self, DirBuilder, OpenOptions, TryLockError}, io::ErrorKind, os::unix::fs::{DirBuilderExt, FileTypeExt, MetadataExt, OpenOptionsExt, PermissionsExt}, path::{Path, PathBuf}, }; use anyhow::{Context, bail}; use gpapi::{auth::SamlAuthData, error::AuthDataParseError}; use tokio::{ io::{AsyncReadExt, AsyncWriteExt}, net::{UnixListener, UnixStream}, time::timeout, }; mod protocol; pub use protocol::validate_auth_callback; use protocol::*; #[cfg(target_os = "macos")] mod macos; #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd", test))] mod xdg; pub struct AuthCallbackReceiver { listener: UnixListener, socket_path: PathBuf, _lock_file: fs::File, } impl AuthCallbackReceiver { pub fn bind() -> anyhow::Result { let socket_path = callback_socket_path(); prepare_socket_directory(&socket_path)?; Self::bind_at(socket_path) } fn bind_at(socket_path: PathBuf) -> anyhow::Result { let lock_file = acquire_lock(&socket_path)?; match fs::symlink_metadata(&socket_path) { Ok(_) => remove_stale_socket(&socket_path)?, Err(err) if err.kind() == ErrorKind::NotFound => {} Err(err) => return Err(err.into()), } finish_bind(UnixListener::bind(&socket_path)?, socket_path, lock_file) } pub async fn receive(&self) -> anyhow::Result { loop { let (mut stream, _) = self.listener.accept().await?; let callback = match timeout(IO_TIMEOUT, read_callback(&mut stream)).await { Ok(callback) => callback, Err(_) => continue, }; match callback { Ok(callback) => match SamlAuthData::from_gpcallback(&callback) { Ok(auth_data) => { 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; } }, Err(CallbackReadError::UnsupportedVersion) => { let _ = write_response(&mut stream, STATUS_UNSUPPORTED_VERSION).await; } Err(CallbackReadError::Invalid) => { let _ = write_response(&mut stream, STATUS_INVALID_CALLBACK).await; } Err(CallbackReadError::Io) => continue, } } } } impl Drop for AuthCallbackReceiver { fn drop(&mut self) { if fs::symlink_metadata(&self.socket_path) .is_ok_and(|metadata| metadata.file_type().is_socket() && metadata.uid() == uzers::get_current_uid()) { let _ = fs::remove_file(&self.socket_path); } } } pub async fn forward_auth_callback(callback: &str) -> anyhow::Result<()> { validate_auth_callback(callback)?; forward_auth_callback_to(callback, &callback_socket_path()).await } #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))] pub use xdg::ensure_auth_callback_handler; #[cfg(target_os = "macos")] pub fn ensure_auth_callback_handler() -> anyhow::Result<()> { macos::ensure_auth_callback_handler() } #[cfg(not(any( target_os = "linux", target_os = "freebsd", target_os = "openbsd", target_os = "macos" )))] pub fn ensure_auth_callback_handler() -> anyhow::Result<()> { Ok(()) } async fn forward_auth_callback_to(callback: &str, socket_path: &Path) -> anyhow::Result<()> { validate_auth_callback(callback)?; let (version, status) = timeout(IO_TIMEOUT, async { let mut stream = UnixStream::connect(socket_path) .await .context("No external-browser authentication is waiting for a callback")?; stream.write_all(PROTOCOL_MAGIC).await?; stream.write_u8(PROTOCOL_VERSION).await?; stream.write_u32(callback.len() as u32).await?; stream.write_all(callback.as_bytes()).await?; Ok::<_, anyhow::Error>((stream.read_u8().await?, stream.read_u8().await?)) }) .await .context("Timed out forwarding the authentication callback")??; if version != PROTOCOL_VERSION { bail!("Authentication callback protocol version mismatch"); } match status { STATUS_ACCEPTED => Ok(()), STATUS_INVALID_CALLBACK => bail!("Authentication callback was rejected"), STATUS_UNSUPPORTED_VERSION => bail!("Authentication callback protocol version is unsupported"), _ => bail!("Authentication callback handler returned an unknown status"), } } fn callback_socket_path() -> PathBuf { let uid = uzers::get_current_uid(); #[cfg(target_os = "linux")] { let runtime_dir = PathBuf::from(format!("/run/user/{uid}")); if fs::symlink_metadata(&runtime_dir).is_ok_and(|metadata| { metadata.is_dir() && !metadata.file_type().is_symlink() && metadata.uid() == uid && metadata.permissions().mode() & 0o077 == 0 }) { return runtime_dir.join("gp-connect/auth-callback.sock"); } } PathBuf::from(format!("/tmp/gp-connect-{uid}/auth-callback.sock")) } fn prepare_socket_directory(socket_path: &Path) -> anyhow::Result<()> { let directory = socket_path .parent() .ok_or_else(|| anyhow::anyhow!("Authentication callback socket has no parent directory"))?; match DirBuilder::new().mode(0o700).create(directory) { Ok(()) => {} Err(err) if err.kind() == ErrorKind::AlreadyExists => { let metadata = fs::symlink_metadata(directory)?; if !metadata.is_dir() || metadata.file_type().is_symlink() { bail!("Authentication callback runtime path is not a directory"); } if metadata.uid() != uzers::get_current_uid() { bail!("Authentication callback runtime directory has the wrong owner"); } if metadata.permissions().mode() & 0o077 != 0 { bail!("Authentication callback runtime directory permissions are too broad"); } } Err(err) => return Err(err.into()), } Ok(()) } fn finish_bind( listener: UnixListener, socket_path: PathBuf, lock_file: fs::File, ) -> anyhow::Result { fs::set_permissions(&socket_path, fs::Permissions::from_mode(0o600))?; Ok(AuthCallbackReceiver { listener, socket_path, _lock_file: lock_file, }) } fn acquire_lock(socket_path: &Path) -> anyhow::Result { let lock_path = socket_path.with_file_name("auth-callback.lock"); let lock_file = OpenOptions::new() .read(true) .write(true) .create(true) .truncate(false) .mode(0o600) .open(lock_path)?; lock_file.set_permissions(fs::Permissions::from_mode(0o600))?; match lock_file.try_lock() { Ok(()) => Ok(lock_file), Err(TryLockError::WouldBlock) => bail!("Another external-browser authentication is already in progress"), Err(TryLockError::Error(err)) => Err(err.into()), } } fn remove_stale_socket(socket_path: &Path) -> anyhow::Result<()> { let metadata = fs::symlink_metadata(socket_path)?; if !metadata.file_type().is_socket() || metadata.uid() != uzers::get_current_uid() { bail!("Authentication callback socket is not owned by the current user"); } fs::remove_file(socket_path)?; Ok(()) } #[cfg(test)] mod tests { use std::time::Duration; use tempfile::tempdir; use super::*; const VALID_CALLBACK: &str = "globalprotectcallback:cas-as=1&un=alice@example.com&token=very_long_string"; #[test] fn validates_supported_callback_scheme() { assert!(validate_auth_callback(VALID_CALLBACK).is_ok()); assert!(validate_auth_callback("https://example.com").is_err()); assert!(validate_auth_callback("globalprotectcallback:").is_err()); assert!(validate_auth_callback(&format!("globalprotectcallback:{}", "x".repeat(MAX_CALLBACK_SIZE))).is_err()); } #[tokio::test] async fn forwards_callback_to_waiting_receiver() { let directory = tempdir().unwrap(); let socket_path = directory.path().join("callback.sock"); let receiver = AuthCallbackReceiver::bind_at(socket_path.clone()).unwrap(); assert_eq!( fs::symlink_metadata(&socket_path).unwrap().permissions().mode() & 0o777, 0o600 ); let sender = tokio::spawn(async move { forward_auth_callback_to(VALID_CALLBACK, &socket_path).await }); let auth_data = receiver.receive().await.unwrap(); sender.await.unwrap().unwrap(); assert_eq!(auth_data.username(), "alice@example.com"); assert_eq!(auth_data.token(), Some("very_long_string")); let socket_path = receiver.socket_path.clone(); drop(receiver); 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(); let socket_path = directory.path().join("callback.sock"); let receiver = AuthCallbackReceiver::bind_at(socket_path.clone()).unwrap(); let sender = tokio::spawn(async move { assert_eq!( 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 ); forward_auth_callback_to(VALID_CALLBACK, &socket_path).await }); let auth_data = receiver.receive().await.unwrap(); sender.await.unwrap().unwrap(); assert_eq!(auth_data.username(), "alice@example.com"); } #[tokio::test] async fn ignores_sender_that_disconnects_before_sending() { let directory = tempdir().unwrap(); let socket_path = directory.path().join("callback.sock"); let receiver = AuthCallbackReceiver::bind_at(socket_path.clone()).unwrap(); drop(UnixStream::connect(&socket_path).await.unwrap()); let sender = tokio::spawn(async move { forward_auth_callback_to(VALID_CALLBACK, &socket_path).await }); let auth_data = receiver.receive().await.unwrap(); sender.await.unwrap().unwrap(); assert_eq!(auth_data.username(), "alice@example.com"); } #[tokio::test] async fn replaces_owned_stale_socket() { let directory = tempdir().unwrap(); let socket_path = directory.path().join("callback.sock"); drop(std::os::unix::net::UnixListener::bind(&socket_path).unwrap()); let receiver = AuthCallbackReceiver::bind_at(socket_path.clone()).unwrap(); assert!(socket_path.exists()); drop(receiver); assert!(!socket_path.exists()); } #[tokio::test] async fn reports_when_no_receiver_is_active() { let directory = tempdir().unwrap(); let socket_path = directory.path().join("missing.sock"); let err = forward_auth_callback_to(VALID_CALLBACK, &socket_path) .await .unwrap_err(); assert!(err.to_string().contains("No external-browser authentication")); } #[test] fn rejects_runtime_directory_with_broad_permissions() { let directory = tempdir().unwrap(); let runtime_dir = directory.path().join("runtime"); fs::create_dir(&runtime_dir).unwrap(); fs::set_permissions(&runtime_dir, fs::Permissions::from_mode(0o755)).unwrap(); let err = prepare_socket_directory(&runtime_dir.join("callback.sock")).unwrap_err(); assert!(err.to_string().contains("permissions are too broad")); } #[tokio::test] async fn rejects_second_active_receiver() { let directory = tempdir().unwrap(); let socket_path = directory.path().join("callback.sock"); let _receiver = AuthCallbackReceiver::bind_at(socket_path.clone()).unwrap(); let err = AuthCallbackReceiver::bind_at(socket_path).err().unwrap(); assert!(err.to_string().contains("already in progress")); } async fn send_raw_callback(socket_path: &Path, version: u8, callback: &str) -> u8 { let mut stream = UnixStream::connect(socket_path).await.unwrap(); stream.write_all(PROTOCOL_MAGIC).await.unwrap(); stream.write_u8(version).await.unwrap(); stream.write_u32(callback.len() as u32).await.unwrap(); stream.write_all(callback.as_bytes()).await.unwrap(); assert_eq!(stream.read_u8().await.unwrap(), PROTOCOL_VERSION); stream.read_u8().await.unwrap() } }