diff --git a/apps/gpclient/src/cli.rs b/apps/gpclient/src/cli.rs index 1b38dd1..ad44c09 100644 --- a/apps/gpclient/src/cli.rs +++ b/apps/gpclient/src/cli.rs @@ -154,11 +154,11 @@ fn init_logger(cli: &Cli) { // Output the log messages to a file if the command is the auth callback if let CliCommand::LaunchGui(args) = &cli.command { let auth_data = args.auth_data.as_deref().unwrap_or_default(); - if !auth_data.is_empty() { - if let Ok(log_file) = File::create(temp_dir().join("gpcallback.log")) { - let target = Box::new(log_file); - builder.target(env_logger::Target::Pipe(target)); - } + if !auth_data.is_empty() + && let Ok(log_file) = File::create(temp_dir().join("gpcallback.log")) + { + let target = Box::new(log_file); + builder.target(env_logger::Target::Pipe(target)); } } diff --git a/apps/gpclient/src/connect.rs b/apps/gpclient/src/connect.rs index 887f9e5..f9243fb 100644 --- a/apps/gpclient/src/connect.rs +++ b/apps/gpclient/src/connect.rs @@ -2,6 +2,7 @@ use std::{ borrow::Cow, cell::RefCell, fs, + str::FromStr, sync::{Arc, Mutex}, }; @@ -41,6 +42,36 @@ struct GatewayLoginSession { extension_auth: SessionExtensionAuth, } +#[derive(Clone, Debug, PartialEq, Eq)] +struct CscPreferenceArg { + domain: String, + key: String, + value: String, +} + +impl FromStr for CscPreferenceArg { + type Err = String; + + fn from_str(input: &str) -> Result { + let (name, value) = input + .split_once('=') + .ok_or_else(|| "expected :=".to_string())?; + let (domain, key) = name + .split_once(':') + .ok_or_else(|| "expected :=".to_string())?; + + if domain.is_empty() || key.is_empty() || value.is_empty() { + return Err("domain, key, and value must be non-empty".to_string()); + } + + Ok(Self { + domain: domain.to_string(), + key: key.to_string(), + value: value.to_string(), + }) + } +} + #[derive(Args)] pub(crate) struct ConnectArgs { #[arg(help = "The portal server to connect to")] @@ -133,6 +164,13 @@ pub(crate) struct ConnectArgs { )] local_hostname: Option, + #[arg( + long = "csc-preference", + help = "Override a Mac CSC plist preference value, in the form :=", + value_name = "DOMAIN:KEY=VALUE" + )] + csc_preferences: Vec, + #[arg( long = "force-dpd", help = "Same as the '--force-dpd' option in the openconnect command" @@ -237,6 +275,10 @@ impl<'a> ConnectHandler<'a> { builder.computer(local_hostname); } + for preference in &self.args.csc_preferences { + builder.csc_preference_override(&preference.domain, &preference.key, &preference.value); + } + builder.build() } @@ -583,7 +625,7 @@ impl<'a> ConnectHandler<'a> { .saml_request(prelogin.saml_request()) .user_agent(&user_agent) .os(self.args.os.as_str()) - .os_version(Some(&os_version)) + .os_version(Some(os_version)) .fix_openssl(self.shared_args.fix_openssl) .ignore_tls_errors(self.shared_args.ignore_tls_errors) .browser(browser) @@ -705,3 +747,31 @@ fn get_uid(user: &Option) -> anyhow::Result { get_non_root_user().map_or_else(|_| Ok(0), |user| Ok(user.uid())) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parses_csc_preference_override() { + let preference = CscPreferenceArg::from_str("com.example.settings:ExampleKey=ExampleValue").unwrap(); + + assert_eq!( + preference, + CscPreferenceArg { + domain: "com.example.settings".to_string(), + key: "ExampleKey".to_string(), + value: "ExampleValue".to_string(), + } + ); + } + + #[test] + fn rejects_invalid_csc_preference_override() { + assert!(CscPreferenceArg::from_str("com.example.settings:ExampleKey").is_err()); + assert!(CscPreferenceArg::from_str("com.example.settings=ExampleValue").is_err()); + assert!(CscPreferenceArg::from_str(":ExampleKey=ExampleValue").is_err()); + assert!(CscPreferenceArg::from_str("com.example.settings:=ExampleValue").is_err()); + assert!(CscPreferenceArg::from_str("com.example.settings:ExampleKey=").is_err()); + } +} diff --git a/crates/gpapi/src/credential.rs b/crates/gpapi/src/credential.rs index b057af2..8791f28 100644 --- a/crates/gpapi/src/credential.rs +++ b/crates/gpapi/src/credential.rs @@ -262,6 +262,18 @@ impl From for Credential { } } +impl From<&AuthCookieCredential> for Credential { + fn from(value: &AuthCookieCredential) -> Self { + Self::AuthCookie(value.clone()) + } +} + +impl From<&CachedCredential> for Credential { + fn from(value: &CachedCredential) -> Self { + Self::Cached(value.clone()) + } +} + #[cfg(test)] mod tests { use super::*; @@ -300,15 +312,3 @@ mod tests { assert_eq!(params.get("token"), Some(&"cas-token")); } } - -impl From<&AuthCookieCredential> for Credential { - fn from(value: &AuthCookieCredential) -> Self { - Self::AuthCookie(value.clone()) - } -} - -impl From<&CachedCredential> for Credential { - fn from(value: &CachedCredential) -> Self { - Self::Cached(value.clone()) - } -} diff --git a/crates/gpapi/src/gateway/parse_gateways.rs b/crates/gpapi/src/gateway/parse_gateways.rs index fc27078..6d08d11 100644 --- a/crates/gpapi/src/gateway/parse_gateways.rs +++ b/crates/gpapi/src/gateway/parse_gateways.rs @@ -35,7 +35,7 @@ pub(crate) fn parse_gateways(element: &Element, prefer_internal: bool) -> Option let priority = parse_priority(gateway_item); let priority_rules = gateway_item .child("priority-rule") - .map(|n| parse_priority_rules(n)) + .map(parse_priority_rules) .unwrap_or_default(); Gateway { diff --git a/crates/gpapi/src/gp_params.rs b/crates/gpapi/src/gp_params.rs index d3d3ce2..7bef8e6 100644 --- a/crates/gpapi/src/gp_params.rs +++ b/crates/gpapi/src/gp_params.rs @@ -58,6 +58,7 @@ pub struct GpParams { certificate: Option, sslkey: Option, key_password: Option, + csc_preference_overrides: HashMap, // Used for MFA input_str: Option, otp: Option, @@ -100,6 +101,13 @@ impl GpParams { self.client_version.as_deref() } + pub fn csc_preference_override(&self, domain: &str, key: &str) -> Option<&str> { + self + .csc_preference_overrides + .get(&csc_preference_key(domain, key)) + .map(String::as_str) + } + pub fn set_input_str(&mut self, input_str: &str) { self.input_str = Some(input_str.to_string()); } @@ -152,6 +160,7 @@ pub struct GpParamsBuilder { certificate: Option, sslkey: Option, key_password: Option, + csc_preference_overrides: HashMap, } impl GpParamsBuilder { @@ -169,6 +178,7 @@ impl GpParamsBuilder { certificate: Default::default(), sslkey: Default::default(), key_password: Default::default(), + csc_preference_overrides: Default::default(), } } @@ -222,6 +232,13 @@ impl GpParamsBuilder { self } + pub fn csc_preference_override(&mut self, domain: &str, key: &str, value: &str) -> &mut Self { + self + .csc_preference_overrides + .insert(csc_preference_key(domain, key), value.to_string()); + self + } + pub fn build(&self) -> GpParams { GpParams { is_gateway: self.is_gateway, @@ -234,12 +251,17 @@ impl GpParamsBuilder { certificate: self.certificate.clone(), sslkey: self.sslkey.clone(), key_password: self.key_password.clone(), + csc_preference_overrides: self.csc_preference_overrides.clone(), input_str: Default::default(), otp: Default::default(), } } } +fn csc_preference_key(domain: &str, key: &str) -> String { + format!("{domain}:{key}") +} + impl Default for GpParamsBuilder { fn default() -> Self { Self::new() diff --git a/crates/gpapi/src/portal/config.rs b/crates/gpapi/src/portal/config.rs index 203a74f..60fda8a 100644 --- a/crates/gpapi/src/portal/config.rs +++ b/crates/gpapi/src/portal/config.rs @@ -203,6 +203,7 @@ async fn retrieve_csc_config( let params = csc::csc_params(&csc_req, username, gp_params, &swg_nonce); let url = format!("{}/global-protect/getconfig_csc.esp", 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| { @@ -252,7 +253,7 @@ fn parse_portal_config(server: &str, cred: &Credential, root: Element) -> anyhow let mut prefer_internal = false; if let Some(ihd_node) = root.descendant("internal-host-detection") { ihd_enabled = true; - prefer_internal = internal_host_detect(&ihd_node) + prefer_internal = internal_host_detect(ihd_node) } let mut gateways = parse_gateways(&root, prefer_internal).unwrap_or_else(|| { @@ -313,23 +314,24 @@ fn internal_host_detect(element: &Element) -> bool { info!("Found internal-host-detection, performing DNS lookup"); for (ip_address, host) in ip_info.iter() { - if let (Some(ip_address), Some(host)) = (ip_address.as_deref(), host.as_deref()) { - if !ip_address.is_empty() && !host.is_empty() { - match ip_address.parse::() { - Ok(ip) => match lookup_addr(&ip) { - Ok(host_lookup) if host_lookup.to_lowercase() == host.to_lowercase() => { - return true; - } - Ok(host_lookup) => { - info!( - "rDNS lookup for {} returned {}, expected {}", - ip_address, host_lookup, host - ); - } - Err(err) => warn!("rDNS lookup failed for {}: {}", ip_address, err), - }, - Err(err) => warn!("Invalid IP address {}: {}", ip_address, err), - } + if let (Some(ip_address), Some(host)) = (ip_address.as_deref(), host.as_deref()) + && !ip_address.is_empty() + && !host.is_empty() + { + match ip_address.parse::() { + Ok(ip) => match lookup_addr(&ip) { + Ok(host_lookup) if host_lookup.to_lowercase() == host.to_lowercase() => { + return true; + } + Ok(host_lookup) => { + info!( + "rDNS lookup for {} returned {}, expected {}", + ip_address, host_lookup, host + ); + } + Err(err) => warn!("rDNS lookup failed for {}: {}", ip_address, err), + }, + Err(err) => warn!("Invalid IP address {}: {}", ip_address, err), } } } diff --git a/crates/gpapi/src/portal/csc.rs b/crates/gpapi/src/portal/csc.rs index 84b323d..b2189e6 100644 --- a/crates/gpapi/src/portal/csc.rs +++ b/crates/gpapi/src/portal/csc.rs @@ -1,5 +1,6 @@ use std::{ collections::HashMap, + fmt::Write, fs, process::Command, time::{SystemTime, UNIX_EPOCH}, @@ -19,20 +20,62 @@ pub(super) struct CscRequest { pub(super) config_digest: String, pub(super) csc_digest: String, pub(super) csc_data: String, + summary: CscBuildSummary, host_id: String, serial_number: String, } -pub(super) trait PreferenceReader { - fn read_preference(&self, domain: &str, key: &str) -> Option; +impl CscRequest { + pub(super) fn summary(&self) -> String { + format!( + "digest_len={}, xml_len={}, plist_entries={}, plist_present={}, plist_missing={}, process_entries={}, process_present={}, process_missing={}", + self.csc_digest.len(), + self.csc_data.len(), + self.summary.plist_entries, + self.summary.plist_present, + self.summary.plist_missing, + self.summary.process_entries, + self.summary.process_present, + self.summary.process_missing, + ) + } } -struct SystemPreferenceReader; +#[derive(Default)] +struct CscBuildSummary { + plist_entries: usize, + plist_present: usize, + plist_missing: usize, + process_entries: usize, + process_present: usize, + process_missing: usize, +} -impl PreferenceReader for SystemPreferenceReader { +struct CscDataInput<'a> { + criteria: &'a Element, + auth_cookie: &'a str, + username: &'a str, + gp_params: &'a GpParams, + host_id: &'a str, + serial_number: &'a str, + csc_digest: &'a str, +} + +pub(super) trait CscDataReader { + fn read_preference(&self, domain: &str, key: &str) -> Option; + fn process_exists(&self, name: &str) -> bool; +} + +struct SystemCscDataReader; + +impl CscDataReader for SystemCscDataReader { fn read_preference(&self, domain: &str, key: &str) -> Option { read_macos_preference(domain, key) } + + fn process_exists(&self, name: &str) -> bool { + linux_process_names().iter().any(|process_name| process_name == name) + } } pub(super) fn swg_nonce() -> String { @@ -49,7 +92,7 @@ pub(super) fn is_config_criteria(root: &Element) -> bool { } pub(super) fn build_csc_request(root: &Element, username: &str, gp_params: &GpParams) -> anyhow::Result { - let reader = SystemPreferenceReader; + let reader = SystemCscDataReader; build_csc_request_with_reader(root, username, gp_params, &reader) } @@ -57,7 +100,7 @@ pub(super) fn build_csc_request_with_reader( root: &Element, username: &str, gp_params: &GpParams, - reader: &dyn PreferenceReader, + reader: &dyn CscDataReader, ) -> anyhow::Result { let auth_cookie = root .descendant_text("portal-csc-auth-cookie") @@ -67,17 +110,21 @@ pub(super) fn build_csc_request_with_reader( let host_id = collect_host_id(gp_params); let serial_number = collect_serial_number(); + let mut summary = CscBuildSummary::default(); let csc_data = build_csc_data( - root, - &auth_cookie, - username, - gp_params, - &host_id, - &serial_number, - DIGEST_PLACEHOLDER, + CscDataInput { + criteria: root, + auth_cookie: &auth_cookie, + username, + gp_params, + host_id: &host_id, + serial_number: &serial_number, + csc_digest: DIGEST_PLACEHOLDER, + }, reader, + &mut summary, )?; - let csc_digest = format!("{:x}", md5::compute(csc_data.as_bytes())); + let csc_digest = pan_md5_hex(csc_data.as_bytes()); let csc_data = csc_data.replace(DIGEST_PLACEHOLDER, &csc_digest); Ok(CscRequest { @@ -85,6 +132,7 @@ pub(super) fn build_csc_request_with_reader( config_digest, csc_digest, csc_data, + summary, host_id, serial_number, }) @@ -115,26 +163,24 @@ pub(super) fn csc_params<'a>( } fn build_csc_data( - criteria: &Element, - auth_cookie: &str, - username: &str, - gp_params: &GpParams, - host_id: &str, - serial_number: &str, - csc_digest: &str, - reader: &dyn PreferenceReader, + input: CscDataInput<'_>, + reader: &dyn CscDataReader, + summary: &mut CscBuildSummary, ) -> anyhow::Result { let mut root = Element::new("hip-report"); - push_text(&mut root, "portal-csc-auth-cookie", auth_cookie); - push_text(&mut root, "user-name", &urlencoding::encode(username)); - push_text(&mut root, "client-os", &client_os_report_value(gp_params)); - push_text(&mut root, "host-id", host_id); - push_text(&mut root, "device-serial-number", serial_number); - push_text(&mut root, "csc-digest", csc_digest); + push_text(&mut root, "portal-csc-auth-cookie", input.auth_cookie); + push_text(&mut root, "user-name", &urlencoding::encode(input.username)); + push_text(&mut root, "client-os", &client_os_report_value(input.gp_params)); + push_text(&mut root, "host-id", input.host_id); + push_text(&mut root, "device-serial-number", input.serial_number); + push_text(&mut root, "csc-digest", input.csc_digest); root.children.push(XMLNode::Element(Element::new("categories"))); - root - .children - .push(XMLNode::Element(build_custom_checks(criteria, gp_params, reader))); + root.children.push(XMLNode::Element(build_custom_checks( + input.criteria, + input.gp_params, + reader, + summary, + ))); let mut bytes = Vec::new(); root.write_with_config( @@ -147,26 +193,49 @@ fn build_csc_data( String::from_utf8(bytes).map_err(Into::into) } -fn build_custom_checks(criteria: &Element, gp_params: &GpParams, reader: &dyn PreferenceReader) -> Element { +fn build_custom_checks( + criteria: &Element, + gp_params: &GpParams, + reader: &dyn CscDataReader, + summary: &mut CscBuildSummary, +) -> Element { let mut custom_checks = Element::new("custom-checks"); let Some(requested_checks) = criteria.descendant("custom-checks") else { return custom_checks; }; - if let Some(mac_os) = requested_checks.child("mac-os") { - if let Some(plist) = mac_os.child("plist") { - custom_checks.children.push(XMLNode::Element(build_plist_checks( - plist, - gp_params.client_os() == "Mac", - reader, - ))); - } + if gp_params.client_os() == "Mac" + && let Some(plist) = requested_checks + .child("mac-os") + .and_then(|mac_os| mac_os.child("plist")) + { + custom_checks + .children + .push(XMLNode::Element(build_plist_checks(plist, gp_params, reader, summary))); + } + + if gp_params.client_os() == "Linux" + && let Some(process_list) = requested_checks + .child("linux") + .and_then(|linux| linux.child("process-list")) + .or_else(|| requested_checks.child("process-list")) + { + custom_checks.children.push(XMLNode::Element(build_process_list_checks( + process_list, + reader, + summary, + ))); } custom_checks } -fn build_plist_checks(plist: &Element, can_read: bool, reader: &dyn PreferenceReader) -> Element { +fn build_plist_checks( + plist: &Element, + gp_params: &GpParams, + reader: &dyn CscDataReader, + summary: &mut CscBuildSummary, +) -> Element { let mut out = Element::new("plist"); for entry in plist.children("entry") { @@ -178,15 +247,17 @@ fn build_plist_checks(plist: &Element, can_read: bool, reader: &dyn PreferenceRe let values: Vec<_> = keys .iter() .map(|key| { - let value = if can_read { - reader.read_preference(domain, key) - } else { - None - }; + let value = gp_params + .csc_preference_override(domain, key) + .map(str::to_string) + .or_else(|| reader.read_preference(domain, key)); (key.as_str(), value) }) .collect(); let plist_exists = values.iter().any(|(_, value)| value.is_some()); + summary.plist_entries += keys.len(); + summary.plist_present += values.iter().filter(|(_, value)| value.is_some()).count(); + summary.plist_missing += values.iter().filter(|(_, value)| value.is_none()).count(); push_text(&mut out_entry, "exist", yes_no(plist_exists)); push_text(&mut out_entry, "value", ""); @@ -207,6 +278,32 @@ fn build_plist_checks(plist: &Element, can_read: bool, reader: &dyn PreferenceRe out } +fn build_process_list_checks( + process_list: &Element, + reader: &dyn CscDataReader, + summary: &mut CscBuildSummary, +) -> Element { + let mut out = Element::new("process-list"); + + for process_name in process_list_names(process_list) { + let exists = reader.process_exists(&process_name); + summary.process_entries += 1; + if exists { + summary.process_present += 1; + } else { + summary.process_missing += 1; + } + + let mut entry = Element::new("entry"); + entry.attributes.insert("name".to_string(), process_name.clone()); + push_text(&mut entry, "exist", yes_no(exists)); + push_text(&mut entry, "value", if exists { &process_name } else { "" }); + out.children.push(XMLNode::Element(entry)); + } + + out +} + fn plist_entry_keys(entry: &Element) -> Vec { let keys: Vec = entry .descendants("member") @@ -225,6 +322,37 @@ fn plist_entry_keys(entry: &Element) -> Vec { } } +fn process_list_names(process_list: &Element) -> Vec { + let mut names: Vec = process_list + .descendants("member") + .into_iter() + .filter_map(|element| element.get_text()) + .map(|text| text.trim().to_string()) + .filter(|text| !text.is_empty()) + .collect(); + + names.extend( + process_list + .children("entry") + .into_iter() + .filter_map(|entry| entry.attr("name")) + .map(str::to_string), + ); + + names.sort(); + names.dedup(); + names +} + +fn pan_md5_hex(value: &[u8]) -> String { + let digest = md5::compute(value); + let mut out = String::new(); + for byte in digest.0 { + write!(&mut out, "{:x}", byte).expect("writing to String should not fail"); + } + out +} + fn collect_host_id(gp_params: &GpParams) -> String { match gp_params.client_os() { "Mac" => netdev::get_default_interface() @@ -298,6 +426,30 @@ fn read_macos_preference(domain: &str, key: &str) -> Option { .filter(|value| !value.is_empty()) } +fn linux_process_names() -> Vec { + let Ok(entries) = fs::read_dir("/proc") else { + return Vec::new(); + }; + + entries + .filter_map(Result::ok) + .filter_map(|entry| { + entry + .file_name() + .to_str() + .filter(|name| name.chars().all(|ch| ch.is_ascii_digit())) + .map(|_| entry.path().join("stat")) + }) + .filter_map(|path| fs::read_to_string(path).ok()) + .filter_map(|stat| { + let start = stat.find('(')?; + let end = stat.rfind(')')?; + stat.get(start + 1..end).map(str::to_string) + }) + .filter(|name| !name.is_empty()) + .collect() +} + fn client_os_report_value(gp_params: &GpParams) -> String { gp_params .os_version() @@ -320,14 +472,19 @@ mod tests { use super::*; use crate::gp_params::{ClientOs, GpParams}; - struct TestPreferenceReader { + struct TestCscDataReader { values: HashMap, + processes: Vec, } - impl PreferenceReader for TestPreferenceReader { + impl CscDataReader for TestCscDataReader { fn read_preference(&self, domain: &str, key: &str) -> Option { self.values.get(&format!("{domain}:{key}")).cloned() } + + fn process_exists(&self, name: &str) -> bool { + self.processes.iter().any(|process| process == name) + } } fn parse_xml(xml: &str) -> Element { @@ -354,8 +511,8 @@ mod tests { - - HipID + + ExampleKey @@ -363,11 +520,12 @@ mod tests { cookie "#, ); - let reader = TestPreferenceReader { + let reader = TestCscDataReader { values: HashMap::from([( - "com.paloaltonetworks.GlobalProtect.settings:HipID".to_string(), - "MicroStrategyGP".to_string(), + "com.example.settings:ExampleKey".to_string(), + "ExampleValue".to_string(), )]), + processes: Vec::new(), }; let req = build_csc_request_with_reader(&root, "alice@example.com", &gp_params(ClientOs::Mac), &reader).unwrap(); @@ -376,7 +534,7 @@ mod tests { assert!(req.csc_data.contains("alice%40example.com")); assert!(req.csc_data.contains("Apple+Mac+OS+X+26.4.1")); assert!(req.csc_data.contains("yes")); - assert!(req.csc_data.contains("MicroStrategyGP")); + assert!(req.csc_data.contains("ExampleValue")); assert!( req .csc_data @@ -399,7 +557,10 @@ mod tests { cookie "#, ); - let reader = TestPreferenceReader { values: HashMap::new() }; + let reader = TestCscDataReader { + values: HashMap::new(), + processes: Vec::new(), + }; let req = build_csc_request_with_reader(&root, "alice", &gp_params(ClientOs::Mac), &reader).unwrap(); @@ -408,7 +569,37 @@ mod tests { } #[test] - fn unsupported_client_os_reports_macos_plist_missing() { + fn macos_plist_override_works_without_native_preference() { + let root = parse_xml( + r#" + + + + ExampleKey + + + + cookie + "#, + ); + let reader = TestCscDataReader { + values: HashMap::new(), + processes: Vec::new(), + }; + let gp_params = GpParams::builder() + .client_os(ClientOs::Mac) + .os_version(Some("Apple Mac OS X 26.4.1".to_string())) + .csc_preference_override("com.example.settings", "ExampleKey", "ExampleValue") + .build(); + + let req = build_csc_request_with_reader(&root, "alice", &gp_params, &reader).unwrap(); + + assert!(req.csc_data.contains("yes")); + assert!(req.csc_data.contains("ExampleValue")); + } + + #[test] + fn unsupported_client_os_ignores_macos_plist_branch() { let root = parse_xml( r#" @@ -421,16 +612,54 @@ mod tests { cookie "#, ); - let reader = TestPreferenceReader { + let reader = TestCscDataReader { values: HashMap::from([("com.example:HipID".to_string(), "present".to_string())]), + processes: Vec::new(), }; let req = build_csc_request_with_reader(&root, "alice", &gp_params(ClientOs::Linux), &reader).unwrap(); - assert!(req.csc_data.contains("no")); + assert!(!req.csc_data.contains("")); + assert!(!req.csc_data.contains("no")); assert!(!req.csc_data.contains("present")); } + #[test] + fn builds_linux_process_list_report() { + let root = parse_xml( + r#" + + + + + + + + + cookie + "#, + ); + let reader = TestCscDataReader { + values: HashMap::new(), + processes: vec!["present-process".to_string()], + }; + + let req = build_csc_request_with_reader(&root, "alice", &gp_params(ClientOs::Linux), &reader).unwrap(); + + assert!(req.csc_data.contains(r#""#)); + assert!(req.csc_data.contains("present-process")); + assert!(req.csc_data.contains(r#""#)); + assert!(req.summary().contains("process_entries=2")); + assert!(req.summary().contains("process_present=1")); + assert!(req.summary().contains("process_missing=1")); + } + + #[test] + fn uses_pan_non_padded_md5_digest() { + assert_eq!(pan_md5_hex(b""), "d41d8cd98f0b24e980998ecf8427e"); + assert_ne!(pan_md5_hex(b""), format!("{:x}", md5::compute(b""))); + } + #[test] fn builds_getconfig_csc_params() { let req = CscRequest { @@ -439,6 +668,7 @@ mod tests { csc_digest: "csc-digest".to_string(), csc_data: "hostserial" .to_string(), + summary: CscBuildSummary::default(), host_id: "host".to_string(), serial_number: "serial".to_string(), }; @@ -456,4 +686,30 @@ mod tests { assert_eq!(params.get("swg-auth-token"), Some(&"0")); assert_eq!(params.get("swg-nonce"), Some(&"123")); } + + #[test] + fn generated_getconfig_csc_params_include_final_digest_and_data() { + let root = parse_xml( + r#" + + cookie + "#, + ); + let reader = TestCscDataReader { + values: HashMap::new(), + processes: Vec::new(), + }; + let gp_params = gp_params(ClientOs::Linux); + let req = build_csc_request_with_reader(&root, "alice", &gp_params, &reader).unwrap(); + let params = csc_params(&req, "alice", &gp_params, "123"); + + assert_eq!(params.get("csc-digest"), Some(&req.csc_digest.as_str())); + assert_eq!(params.get("csc-data"), Some(&req.csc_data.as_str())); + assert!( + req + .csc_data + .contains(&format!("{}", req.csc_digest)) + ); + assert!(!req.csc_data.contains(DIGEST_PLACEHOLDER)); + } } diff --git a/crates/gpapi/src/utils/xml.rs b/crates/gpapi/src/utils/xml.rs index 5da8e7a..0e13849 100644 --- a/crates/gpapi/src/utils/xml.rs +++ b/crates/gpapi/src/utils/xml.rs @@ -46,10 +46,10 @@ impl ElementExt for Element { } for child in &self.children { - if let Some(element) = child.as_element() { - if let Some(found) = element.descendant(name) { - return Some(found); - } + if let Some(element) = child.as_element() + && let Some(found) = element.descendant(name) + { + return Some(found); } } diff --git a/crates/openconnect/build.rs b/crates/openconnect/build.rs index 15ad7a2..5f11201 100644 --- a/crates/openconnect/build.rs +++ b/crates/openconnect/build.rs @@ -1,6 +1,10 @@ -use std::{env, path::PathBuf, process::Command}; +use std::{ + env, + path::{Path, PathBuf}, + process::Command, +}; -fn apply_patches(patches_dir: &PathBuf, build_src: &PathBuf) { +fn apply_patches(patches_dir: &Path, build_src: &Path) { let patches = std::fs::read_dir(patches_dir) .expect("Failed to read patches directory") .filter_map(|entry| entry.ok()) @@ -30,7 +34,7 @@ fn apply_patches(patches_dir: &PathBuf, build_src: &PathBuf) { } } -fn build_libxml2(deps_dir: &PathBuf, out_dir: &PathBuf) -> PathBuf { +fn build_libxml2(deps_dir: &Path, out_dir: &Path) -> PathBuf { let libxml2_dir = deps_dir.join("libxml2"); // The temporary location where we will build @@ -89,7 +93,7 @@ fn build_libxml2(deps_dir: &PathBuf, out_dir: &PathBuf) -> PathBuf { dst } -fn build_openconnect(deps_dir: &PathBuf, out_dir: &PathBuf) -> PathBuf { +fn build_openconnect(deps_dir: &Path, out_dir: &Path) -> PathBuf { let openconnect_dir = deps_dir.join("openconnect"); let patches_dir = deps_dir.join("patches"); @@ -200,6 +204,6 @@ fn main() { cc::Build::new() .file("src/ffi/vpn.c") .include("src/ffi") - .include(&oc_dst.join("include")) + .include(oc_dst.join("include")) .compile("vpn"); } diff --git a/crates/openconnect/src/vpn_utils.rs b/crates/openconnect/src/vpn_utils.rs index e5f81c2..51a9292 100644 --- a/crates/openconnect/src/vpn_utils.rs +++ b/crates/openconnect/src/vpn_utils.rs @@ -44,11 +44,11 @@ fn find_executable(locations: &[&'static str]) -> Option<&'static str> { } pub fn find_vpnc_script() -> Option<&'static str> { - find_executable(&VPNC_SCRIPT_LOCATIONS) + find_executable(VPNC_SCRIPT_LOCATIONS) } pub fn find_csd_wrapper() -> Option<&'static str> { - find_executable(&CSD_WRAPPER_LOCATIONS) + find_executable(CSD_WRAPPER_LOCATIONS) } /// If file exists, check if it is executable