mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2026-11-18 22:16:18 -05:00
refactor: align GP profile and packaging flow
Main changes: - Refactor connect/auth flows around a single OS profile and host identity model, including gateway fallback and cookie-on-stdin handling. - Align request parameters, HIP host-id propagation, SAML webview user-agent behavior, and portal default-browser handling with observed official client behavior. - Add opt-in cookie cache support and Microsoft Defender HIP reporting while keeping host-id validation explicit. - Add BSD build, packaging, release artifact, and provenance support with FreeBSD/OpenBSD path and dependency cleanup. - Update NixOS install documentation and dependency compatibility fixes.
This commit is contained in:
92 files changed
+9847
-2868
No files matched your search
@@ -4,7 +4,7 @@ use anyhow::bail;
|
||||
use common::constants::GP_AUTH_BINARY;
|
||||
use tokio::process::Command;
|
||||
|
||||
use crate::{auth::SamlAuthResult, credential::Credential};
|
||||
use crate::{auth::SamlAuthResult, credential::Credential, os_profile::OsProfile};
|
||||
|
||||
use super::command_traits::CommandExt;
|
||||
|
||||
@@ -13,9 +13,9 @@ pub struct SamlAuthLauncher<'a> {
|
||||
auth_executable: Option<&'a str>,
|
||||
gateway: bool,
|
||||
saml_request: Option<&'a str>,
|
||||
user_agent: Option<&'a str>,
|
||||
os: Option<&'a str>,
|
||||
os_version: Option<&'a str>,
|
||||
host_id: Option<&'a str>,
|
||||
client_version: Option<&'a str>,
|
||||
fix_openssl: bool,
|
||||
ignore_tls_errors: bool,
|
||||
#[cfg(feature = "webview-auth")]
|
||||
@@ -35,9 +35,9 @@ impl<'a> SamlAuthLauncher<'a> {
|
||||
auth_executable: None,
|
||||
gateway: false,
|
||||
saml_request: None,
|
||||
user_agent: None,
|
||||
os: None,
|
||||
os_version: None,
|
||||
host_id: None,
|
||||
client_version: None,
|
||||
fix_openssl: false,
|
||||
ignore_tls_errors: false,
|
||||
#[cfg(feature = "webview-auth")]
|
||||
@@ -66,18 +66,10 @@ impl<'a> SamlAuthLauncher<'a> {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn user_agent(mut self, user_agent: &'a str) -> Self {
|
||||
self.user_agent = Some(user_agent);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn os(mut self, os: &'a str) -> Self {
|
||||
self.os = Some(os);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn os_version(mut self, os_version: Option<&'a str>) -> Self {
|
||||
self.os_version = os_version;
|
||||
pub fn os_profile(mut self, profile: &'a OsProfile) -> Self {
|
||||
self.os = Some(profile.client_os().as_str());
|
||||
self.host_id = Some(profile.host_identity().host_id());
|
||||
self.client_version = Some(profile.client_version());
|
||||
self
|
||||
}
|
||||
|
||||
@@ -133,16 +125,16 @@ impl<'a> SamlAuthLauncher<'a> {
|
||||
auth_cmd.arg("--saml-request").arg(saml_request);
|
||||
}
|
||||
|
||||
if let Some(user_agent) = self.user_agent {
|
||||
auth_cmd.arg("--user-agent").arg(user_agent);
|
||||
}
|
||||
|
||||
if let Some(os) = self.os {
|
||||
auth_cmd.arg("--os").arg(os);
|
||||
}
|
||||
|
||||
if let Some(os_version) = self.os_version {
|
||||
auth_cmd.arg("--os-version").arg(os_version);
|
||||
if let Some(host_id) = self.host_id {
|
||||
auth_cmd.arg("--host-id").arg(host_id);
|
||||
}
|
||||
|
||||
if let Some(client_version) = self.client_version {
|
||||
auth_cmd.arg("--client-version").arg(client_version);
|
||||
}
|
||||
|
||||
if self.fix_openssl {
|
||||
@@ -195,3 +187,36 @@ impl<'a> SamlAuthLauncher<'a> {
|
||||
Credential::try_from(auth_result)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::os_profile::{ClientOs, OsProfileBuilder};
|
||||
|
||||
#[test]
|
||||
fn os_profile_sets_os_from_profile() {
|
||||
let profile = OsProfileBuilder::new(ClientOs::Linux).client_version("6.0.0").build();
|
||||
|
||||
let launcher = SamlAuthLauncher::new("portal.example.com").os_profile(&profile);
|
||||
|
||||
assert_eq!(launcher.os, Some("Linux"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn os_profile_sets_host_id_from_profile_runtime_identity() {
|
||||
let profile = OsProfileBuilder::new(ClientOs::Linux).client_version("6.0.0").build();
|
||||
|
||||
let launcher = SamlAuthLauncher::new("portal.example.com").os_profile(&profile);
|
||||
|
||||
assert_eq!(launcher.host_id, Some(profile.host_identity().host_id()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn os_profile_sets_client_version_from_profile() {
|
||||
let profile = OsProfileBuilder::new(ClientOs::Linux).client_version("6.0.0").build();
|
||||
|
||||
let launcher = SamlAuthLauncher::new("portal.example.com").os_profile(&profile);
|
||||
|
||||
assert_eq!(launcher.client_version, Some("6.0.0"));
|
||||
}
|
||||
}
|
||||
@@ -3,12 +3,18 @@ use std::env;
|
||||
use anyhow::bail;
|
||||
use uzers::User;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
enum RealUserLookup {
|
||||
Uid(u32),
|
||||
Name(String),
|
||||
}
|
||||
|
||||
pub fn get_user_by_name(username: &str) -> anyhow::Result<User> {
|
||||
uzers::get_user_by_name(username).ok_or_else(|| anyhow::anyhow!("User ({}) not found", username))
|
||||
}
|
||||
|
||||
pub fn get_non_root_user() -> anyhow::Result<User> {
|
||||
let current_user = whoami::username();
|
||||
let current_user = current_username()?;
|
||||
|
||||
let user = if current_user == "root" {
|
||||
get_real_user()?
|
||||
@@ -24,16 +30,87 @@ pub fn get_non_root_user() -> anyhow::Result<User> {
|
||||
}
|
||||
|
||||
pub fn get_current_user() -> anyhow::Result<User> {
|
||||
let current_user = whoami::username();
|
||||
let current_user = current_username()?;
|
||||
get_user_by_name(¤t_user)
|
||||
}
|
||||
|
||||
fn current_username() -> anyhow::Result<String> {
|
||||
whoami::username().map_err(|err| anyhow::anyhow!("Failed to resolve current user: {err}"))
|
||||
}
|
||||
|
||||
fn get_real_user() -> anyhow::Result<User> {
|
||||
// Read the UID from SUDO_UID or PKEXEC_UID environment variable if available.
|
||||
let uid = match env::var("SUDO_UID") {
|
||||
Ok(uid) => uid.parse::<u32>()?,
|
||||
_ => env::var("PKEXEC_UID")?.parse::<u32>()?,
|
||||
};
|
||||
match real_user_lookup(|key| env::var(key).ok())? {
|
||||
RealUserLookup::Uid(uid) => uzers::get_user_by_uid(uid).ok_or_else(|| anyhow::anyhow!("User not found")),
|
||||
RealUserLookup::Name(name) => get_user_by_name(&name),
|
||||
}
|
||||
}
|
||||
|
||||
fn real_user_lookup(env_var: impl Fn(&str) -> Option<String>) -> anyhow::Result<RealUserLookup> {
|
||||
if let Some(uid) = env_var("SUDO_UID") {
|
||||
return Ok(RealUserLookup::Uid(uid.parse::<u32>()?));
|
||||
}
|
||||
|
||||
if let Some(uid) = env_var("PKEXEC_UID") {
|
||||
return Ok(RealUserLookup::Uid(uid.parse::<u32>()?));
|
||||
}
|
||||
|
||||
if let Some(user) = env_var("DOAS_USER").filter(|user| !user.trim().is_empty()) {
|
||||
return Ok(RealUserLookup::Name(user));
|
||||
}
|
||||
|
||||
bail!("User not found")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn lookup(vars: &[(&str, &str)]) -> anyhow::Result<RealUserLookup> {
|
||||
real_user_lookup(|key| {
|
||||
vars
|
||||
.iter()
|
||||
.find(|(name, _)| *name == key)
|
||||
.map(|(_, value)| value.to_string())
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn real_user_lookup_uses_sudo_uid() {
|
||||
assert_eq!(lookup(&[("SUDO_UID", "1000")]).unwrap(), RealUserLookup::Uid(1000));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn real_user_lookup_uses_pkexec_uid() {
|
||||
assert_eq!(lookup(&[("PKEXEC_UID", "1001")]).unwrap(), RealUserLookup::Uid(1001));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn real_user_lookup_uses_doas_user() {
|
||||
assert_eq!(
|
||||
lookup(&[("DOAS_USER", "alice")]).unwrap(),
|
||||
RealUserLookup::Name("alice".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn real_user_lookup_prefers_uid_over_doas_user() {
|
||||
assert_eq!(
|
||||
lookup(&[("SUDO_UID", "1000"), ("PKEXEC_UID", "1001"), ("DOAS_USER", "alice")]).unwrap(),
|
||||
RealUserLookup::Uid(1000)
|
||||
);
|
||||
assert_eq!(
|
||||
lookup(&[("PKEXEC_UID", "1001"), ("DOAS_USER", "alice")]).unwrap(),
|
||||
RealUserLookup::Uid(1001)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn real_user_lookup_ignores_blank_doas_user() {
|
||||
assert!(lookup(&[("DOAS_USER", " ")]).is_err());
|
||||
}
|
||||
|
||||
uzers::get_user_by_uid(uid).ok_or_else(|| anyhow::anyhow!("User not found"))
|
||||
#[test]
|
||||
fn real_user_lookup_errors_without_root_origin_env() {
|
||||
assert!(lookup(&[]).is_err());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user