use std::collections::HashMap; use anyhow::bail; use log::{debug, info, warn}; use reqwest::{Client, StatusCode}; use serde::Serialize; use specta::Type; use tokio_util::sync::CancellationToken; use xmltree::Element; use crate::{ credential::{AuthCookieCredential, Credential}, error::{PortalError, ServerTlsError}, gateway::{Gateway, parse_gateways}, gp_params::GpParams, params, session::{InternalHostDetection, InternalSessionPolicy}, utils::{normalize_server, parse_gp_response, redact::redact_form_params, remove_url_scheme, xml::ElementExt}, }; use super::csc; #[derive(Debug, Serialize, Type)] #[serde(rename_all = "camelCase")] pub struct PortalConfig { portal: String, auth_cookie: AuthCookieCredential, config_cred: Credential, gateways: Vec, connect_method: Option, config_digest: Option, /** * Variants: * - None: Internal host detection is not supported * - Some(false): Internal host detection is supported but the user is not connected to the internal network * - Some(true): Internal host detection is supported and the user is connected to the internal network */ internal_host_detection: Option, internal_detection: Option, /** * The version returned by the portal config, if any */ version: Option, /** * Whether the portal policy allows extending the gateway session. */ allow_extend_session: Option, /** * Whether the portal policy enables default-browser authentication. */ default_browser: Option, internal_session_policy: Option, } /// Parsed portal policy before a caller chooses network-location candidates. pub struct PortalPolicy { config: PortalConfig, internal_gateways: Vec, external_gateways: Vec, } impl PortalPolicy { pub fn external_gateways(self) -> PortalConfig { self.select_gateways(false, None) } pub(super) fn detection(&self) -> Option<&InternalHostDetection> { self.config.internal_detection.as_ref() } pub(super) fn select_gateways(mut self, prefer_internal: bool, detected: Option) -> PortalConfig { let mut gateways = if prefer_internal { self.internal_gateways } else { self.external_gateways }; if gateways.is_empty() { gateways.push(Gateway::new(self.config.portal.clone(), self.config.portal.clone())); } self.config.gateways = gateways; self.config.internal_host_detection = detected; self.config } } impl PortalConfig { pub fn portal(&self) -> &str { &self.portal } pub fn gateways(&self) -> Vec<&Gateway> { self.gateways.iter().collect() } pub fn auth_cookie(&self) -> &AuthCookieCredential { &self.auth_cookie } pub fn config_cred(&self) -> &Credential { &self.config_cred } pub fn internal_host_detection(&self) -> Option { self.internal_host_detection } pub fn internal_detection(&self) -> Option<&InternalHostDetection> { self.internal_detection.as_ref() } pub fn connect_method(&self) -> Option<&str> { self.connect_method.as_deref() } pub fn version(&self) -> Option<&str> { self.version.as_deref() } pub fn allow_extend_session(&self) -> Option { self.allow_extend_session } pub fn default_browser(&self) -> Option { self.default_browser } pub fn internal_session_policy(&self) -> Option { self.internal_session_policy } /// In-place sort the gateways by region pub fn sort_gateways(&mut self, region: &str) { let preferred_gateway = self.find_preferred_gateway(region); let preferred_gateway_index = self .gateways() .iter() .position(|gateway| gateway.name == preferred_gateway.name) .unwrap(); // Move the preferred gateway to the front of the list self.gateways.swap(0, preferred_gateway_index); } /// Find a gateway by name or address pub fn find_gateway(&self, name_or_address: &str) -> Option<&Gateway> { self .gateways .iter() .find(|gateway| gateway.name == name_or_address || gateway.address == name_or_address) } /// Find the preferred gateway for the given region /// Iterates over the gateways and find the first one that /// has the lowest priority for the given region. /// If no gateway is found, returns the gateway with the lowest priority. pub fn find_preferred_gateway(&self, region: &str) -> &Gateway { let mut preferred_gateway: Option<&Gateway> = None; let mut lowest_region_priority = u32::MAX; for gateway in &self.gateways { for rule in &gateway.priority_rules { if (rule.name == region || rule.name == "Any") && rule.priority < lowest_region_priority { preferred_gateway = Some(gateway); lowest_region_priority = rule.priority; } } } // If no gateway is found, return the gateway with the lowest priority preferred_gateway.unwrap_or_else(|| self.gateways.iter().min_by_key(|gateway| gateway.priority).unwrap()) } } /// Retrieve server policy without inspecting local network location. pub async fn retrieve_config( portal: &str, cred: &Credential, gp_params: &GpParams, cancellation: &CancellationToken, ) -> anyhow::Result { let portal = normalize_server(portal)?; let server_url = url::Url::parse(&portal)?; let server = remove_url_scheme(&portal); let url = format!("{}/global-protect/getconfig.esp", portal); let client = Client::try_from(gp_params)?; let request_params = params::portal_getconfig::build(cred, gp_params, &server); let body_pairs: Vec<(&str, &str)> = request_params .body .iter() .map(|(k, v)| (k.as_str(), v.as_str())) .collect(); info!("Retrieve the portal config, user_agent: {}", gp_params.user_agent()); info!("Portal config request params: {}", redact_form_params(&body_pairs)); let res_xml = tokio::select! { biased; _ = cancellation.cancelled() => bail!("Portal configuration cancelled"), result = async { let res = client.post(&url).form(&request_params.body).send().await.map_err(|e| { warn!("Network error: {:?}", e); match ServerTlsError::from_request(&server_url, &e) { Some(tls) => anyhow::anyhow!(tls), None => anyhow::anyhow!(PortalError::NetworkError(e)), } })?; parse_gp_response(res).await.or_else(|err| { if let Some(tls) = ServerTlsError::from_request(&server_url, &err) { return Err(anyhow::anyhow!(tls)); } if gp_params.os_profile().client_os() == crate::os_profile::ClientOs::Ios && err.is_authentication_rejection() { return Err(anyhow::anyhow!(crate::error::AuthenticationRejected)); } if err.status == StatusCode::NOT_FOUND { bail!(PortalError::ConfigError("Config endpoint not found".to_string())); } if err.is_status_error() { warn!("{err}"); bail!("Portal config error: {}", err.reason); } Err(anyhow::anyhow!(PortalError::ConfigError(err.reason))) }) } => result?, }; if res_xml.is_empty() { bail!(PortalError::ConfigError("Empty portal config response".to_string())) } debug!("Portal config response received: {} bytes", res_xml.len()); let root = Element::parse(res_xml.as_bytes()).map_err(|e| PortalError::ConfigError(e.to_string()))?; if csc::is_config_criteria(&root) { info!("Portal returned CSC criteria: {}", csc_criteria_summary(&root)); if !gp_params.effective_csc_support() { bail!(PortalError::ConfigError( "Portal returned CSC criteria but CSC support is disabled".to_string() )); } let csc_xml = tokio::select! { biased; _ = cancellation.cancelled() => bail!("Portal configuration cancelled"), result = retrieve_csc_config(&client, &portal, &root, cred.username(), gp_params) => result?, }; debug!("Portal CSC config response received: {} bytes", csc_xml.len()); let root = Element::parse(csc_xml.as_bytes()).map_err(|e| PortalError::ConfigError(e.to_string()))?; return parse_portal_policy(&server, cred, root, cancellation).await; } info!("Portal did not return CSC criteria"); parse_portal_policy(&server, cred, root, cancellation).await } async fn retrieve_csc_config( client: &Client, portal: &str, root: &Element, username: &str, gp_params: &GpParams, ) -> anyhow::Result { let csc_req = csc::build_csc_request(root, username, gp_params)?; let swg_nonce = csc::swg_nonce(); let params = csc::csc_params(&csc_req, username, gp_params, &swg_nonce); let url = format!("{}/global-protect/getconfig_csc.esp", portal); let server_url = url::Url::parse(portal)?; info!("Portal CSC config request summary: {}", csc_req.summary()); info!("Portal CSC config request params: {}", redact_params(¶ms)); let res = client.post(&url).form(¶ms).send().await.map_err(|e| { warn!("Network error: {:?}", e); match ServerTlsError::from_request(&server_url, &e) { Some(tls) => anyhow::anyhow!(tls), None => anyhow::anyhow!(PortalError::NetworkError(e)), } })?; parse_gp_response(res).await.or_else(|err| { if let Some(tls) = ServerTlsError::from_request(&server_url, &err) { return Err(anyhow::anyhow!(tls)); } if err.status == StatusCode::NOT_FOUND { bail!(PortalError::ConfigError("CSC config endpoint not found".to_string())); } if err.is_status_error() { warn!("{err}"); bail!("Portal CSC config error: {}", err.reason); } Err(anyhow::anyhow!(PortalError::ConfigError(err.reason))) }) } fn redact_params(params: &HashMap<&str, &str>) -> String { let params = params.iter().map(|(key, value)| (*key, *value)).collect::>(); redact_form_params(¶ms) } fn csc_criteria_summary(root: &Element) -> String { let auth_cookie = present(root.descendant_text("portal-csc-auth-cookie").as_deref()); let config_digest = present(root.descendant_text("config-digest").as_deref()); let custom_check_entries = root .descendant("custom-checks") .map(|custom_checks| custom_checks.descendants("entry").len()) .unwrap_or_default(); format!("auth_cookie={auth_cookie}, config_digest={config_digest}, custom_check_entries={custom_check_entries}") } fn present(value: Option<&str>) -> &'static str { match value { Some(value) if !value.is_empty() => "present", _ => "empty", } } async fn parse_portal_policy( server: &str, cred: &Credential, root: Element, cancellation: &CancellationToken, ) -> anyhow::Result { let internal_detection = root .descendant("internal-host-detection") .map(parse_internal_detection) .transpose() .unwrap_or_else(|error| { warn!("Internal host detection policy is unavailable: {error}"); None }) .flatten(); if cancellation.is_cancelled() { bail!("Portal configuration cancelled"); } let internal_gateways = parse_gateways(&root, true).unwrap_or_default(); let external_gateways = parse_gateways(&root, false).unwrap_or_default(); let user_auth_cookie = root.descendant_text("portal-userauthcookie").unwrap_or_default(); let prelogon_user_auth_cookie = root .descendant_text("portal-prelogonuserauthcookie") .unwrap_or_default(); let config_digest = root.descendant_text("config-digest"); let connect_method = parse_connect_method(&root); let version = root.descendant_text("version").map(|s| s.to_string()); info!("Detected portal version: {:?}", version); let allow_extend_session = parse_allow_extend_session(&root); let default_browser = parse_default_browser(&root); let internal_session_policy = parse_internal_session_policy(&root).ok(); Ok(PortalPolicy { internal_gateways, external_gateways, config: PortalConfig { portal: server.to_string(), auth_cookie: AuthCookieCredential::new(cred.username(), &user_auth_cookie, &prelogon_user_auth_cookie), config_cred: cred.clone(), gateways: Vec::new(), connect_method, config_digest: config_digest.map(|s| s.to_string()), internal_host_detection: None, internal_detection, version, allow_extend_session, default_browser, internal_session_policy, }, }) } fn parse_internal_session_policy(root: &Element) -> anyhow::Result { let hip = root.descendant("hip-collection"); let interval = hip .and_then(|hip| hip.child("hip-report-interval")) .map(|element| element.get_text().unwrap_or_default().into_owned()); let collect = hip .and_then(|hip| hip.child("collect-hip-data")) .map(|element| element.get_text().unwrap_or_default().into_owned()); let interval = parse_policy_number(interval, 3600, "hip-report-interval")?; let collect = match collect.as_deref().map(str::trim) { None | Some("yes") => true, Some("no") => false, Some(_) => bail!("Invalid portal collect-hip-data policy"), }; InternalSessionPolicy::new(interval, collect) } fn parse_policy_number(value: Option, default: u32, field: &str) -> anyhow::Result { match value { Some(value) => value .trim() .parse() .map_err(|_| anyhow::anyhow!("Invalid portal {field} policy")), None => Ok(default), } } fn parse_default_browser(root: &Element) -> Option { match root.descendant_text("default-browser")?.trim() { "yes" => Some(true), "no" => Some(false), _ => None, } } fn parse_allow_extend_session(root: &Element) -> Option { match root.descendant_text("allow-extend-session")?.trim() { "yes" => Some(true), "no" => Some(false), _ => None, } } fn parse_connect_method(root: &Element) -> Option { root .descendant_text("connect-method") .filter(|s| !s.is_empty()) .map(|s| s.to_string()) } fn parse_internal_detection(element: &Element) -> anyhow::Result> { let ip_info = [ (element.child_text("ip-address"), element.child_text("host")), (element.child_text("ipv6-address"), element.child_text("ipv6-host")), ]; let mut targets = Vec::new(); for (ip_address, host) in ip_info.iter() { let address = ip_address.as_deref().filter(|value| !value.is_empty()); let host = host.as_deref().filter(|value| !value.is_empty()); match (address, host) { (None, None) => {} (Some(address), Some(host)) => targets.push((address.parse()?, host.to_owned())), _ => bail!("Incomplete internal host detection policy"), } } if targets.is_empty() { Ok(None) } else { Ok(Some(InternalHostDetection::new(targets)?)) } } #[cfg(test)] mod tests { use super::*; #[tokio::test] async fn parsed_policy_keeps_location_selection_explicit() { let xml = "192.0.2.1inside.example"; let credential = crate::credential::PasswordCredential::new("alice", "password").into(); let policy = parse_portal_policy("portal.example", &credential, parse_xml(xml), &CancellationToken::new()) .await .unwrap(); assert!(policy.detection().is_some()); let external = policy.external_gateways(); assert_eq!(external.gateways()[0].server(), "external.example"); assert_eq!(external.internal_host_detection(), None); let policy = parse_portal_policy("portal.example", &credential, parse_xml(xml), &CancellationToken::new()) .await .unwrap(); let internal = policy.select_gateways(true, Some(true)); assert_eq!(internal.gateways()[0].server(), "internal.example"); assert_eq!(internal.internal_host_detection(), Some(true)); } #[tokio::test] async fn empty_candidates_preserve_portal_gateway_fallback() { let credential = crate::credential::PasswordCredential::new("alice", "password").into(); for detected in [None, Some(false), Some(true)] { let policy = parse_portal_policy( "portal.example", &credential, parse_xml(""), &CancellationToken::new(), ) .await .unwrap(); let config = policy.select_gateways(detected.unwrap_or(false), detected); assert_eq!(config.gateways().len(), 1); assert_eq!(config.gateways()[0].server(), "portal.example"); assert_eq!(config.internal_host_detection(), detected); } } #[tokio::test] async fn invalid_non_tunnel_policy_does_not_reject_tunnel_candidates() { let root = parse_xml( "invalidinvalidinternal.example", ); let credential = crate::credential::PasswordCredential::new("alice", "password").into(); let config = parse_portal_policy("portal.example", &credential, root, &CancellationToken::new()) .await .unwrap() .external_gateways(); assert!(config.internal_session_policy().is_none()); assert_eq!(config.gateways()[0].server(), "vpn.example"); assert_eq!(config.gateways()[0].kind(), crate::gateway::GatewayKind::External); } fn parse_xml(xml: &str) -> Element { Element::parse(xml.as_bytes()).unwrap() } #[test] fn parses_internal_maintenance_policy_with_explicit_units() { let root = parse_xml( "60no3", ); let policy = parse_internal_session_policy(&root).unwrap(); assert_eq!(policy.hip_interval(), std::time::Duration::from_secs(60)); assert!(!policy.collect_hip_data()); assert_eq!( parse_internal_session_policy(&parse_xml("")).unwrap(), InternalSessionPolicy::default() ); } #[test] fn rejects_invalid_supplied_internal_policy() { for interval in ["0", "-1", "", "4294967295", "not-a-number"] { let root = parse_xml(&format!( "{interval}" )); assert!(parse_internal_session_policy(&root).is_err()); } let root = parse_xml("true"); assert!(parse_internal_session_policy(&root).is_err()); } #[tokio::test] async fn accepts_existing_full_portal_fixture() { let root = parse_xml(include_str!("../../tests/files/portal_config.xml")); let cred = Credential::from(crate::credential::PasswordCredential::new( "fixture-user", "fixture-password", )); let config = parse_portal_policy("vpn.example.com", &cred, root, &CancellationToken::new()) .await .unwrap() .external_gateways(); assert_eq!( config.internal_session_policy().unwrap().hip_interval(), std::time::Duration::from_secs(3600) ); assert!(!config.gateways().is_empty()); } #[test] fn parses_allow_extend_session_yes() { let root = parse_xml("yes"); assert_eq!(parse_allow_extend_session(&root), Some(true)); } #[test] fn parses_allow_extend_session_no() { let root = parse_xml("no"); assert_eq!(parse_allow_extend_session(&root), Some(false)); } #[test] fn leaves_absent_allow_extend_session_unknown() { let root = parse_xml(""); assert_eq!(parse_allow_extend_session(&root), None); } #[test] fn ignores_non_official_allow_extend_session_values() { let root = parse_xml("true"); assert_eq!(parse_allow_extend_session(&root), None); } #[test] fn parses_default_browser_yes() { let root = parse_xml("yes"); assert_eq!(parse_default_browser(&root), Some(true)); } #[test] fn parses_default_browser_no() { let root = parse_xml("no"); assert_eq!(parse_default_browser(&root), Some(false)); } #[test] fn leaves_absent_default_browser_unknown() { let root = parse_xml(""); assert_eq!(parse_default_browser(&root), None); } #[test] fn parses_connect_method() { let root = parse_xml("on-demand"); assert_eq!(parse_connect_method(&root).as_deref(), Some("on-demand")); } #[tokio::test] async fn parses_csc_policy_response_as_portal_config() { let root = parse_xml( r#" user-cookie prelogon-cookie us1.vpn.example.com "#, ); let cred = Credential::from(crate::credential::PasswordCredential::new("alice", "secret")); let config = parse_portal_policy("vpn.example.com", &cred, root, &CancellationToken::new()) .await .unwrap() .external_gateways(); assert_eq!(config.auth_cookie().user_auth_cookie(), "user-cookie"); assert_eq!(config.auth_cookie().prelogon_user_auth_cookie(), "prelogon-cookie"); assert_eq!(config.gateways().len(), 1); } }