mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2026-11-19 06:26:14 -05:00
fix: use portal policy for session extension
This commit is contained in:
10 files changed
+164
-49
No files matched your search
@@ -33,6 +33,10 @@ pub struct PortalConfig {
|
||||
* The version returned by the portal config, if any
|
||||
*/
|
||||
version: Option<String>,
|
||||
/**
|
||||
* Whether the portal policy allows extending the gateway session.
|
||||
*/
|
||||
allow_extend_session: Option<bool>,
|
||||
}
|
||||
|
||||
impl PortalConfig {
|
||||
@@ -60,6 +64,10 @@ impl PortalConfig {
|
||||
self.version.as_deref()
|
||||
}
|
||||
|
||||
pub fn allow_extend_session(&self) -> Option<bool> {
|
||||
self.allow_extend_session
|
||||
}
|
||||
|
||||
/// In-place sort the gateways by region
|
||||
pub fn sort_gateways(&mut self, region: &str) {
|
||||
let preferred_gateway = self.find_preferred_gateway(region);
|
||||
@@ -168,6 +176,7 @@ pub async fn retrieve_config(portal: &str, cred: &Credential, gp_params: &GpPara
|
||||
|
||||
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);
|
||||
|
||||
Ok(PortalConfig {
|
||||
portal: server.to_string(),
|
||||
@@ -177,9 +186,18 @@ pub async fn retrieve_config(portal: &str, cred: &Credential, gp_params: &GpPara
|
||||
config_digest: config_digest.map(|s| s.to_string()),
|
||||
internal_host_detection: if ihd_enabled { Some(prefer_internal) } else { None },
|
||||
version,
|
||||
allow_extend_session,
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_allow_extend_session(root: &Element) -> Option<bool> {
|
||||
match root.descendant_text("allow-extend-session")?.trim() {
|
||||
"yes" => Some(true),
|
||||
"no" => Some(false),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
// Perform DNS lookup and compare the result with the expected hostname
|
||||
fn internal_host_detect(element: &Element) -> bool {
|
||||
let ip_info = [
|
||||
@@ -213,3 +231,40 @@ fn internal_host_detect(element: &Element) -> bool {
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn parse_xml(xml: &str) -> Element {
|
||||
Element::parse(xml.as_bytes()).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_allow_extend_session_yes() {
|
||||
let root = parse_xml("<response><allow-extend-session>yes</allow-extend-session></response>");
|
||||
|
||||
assert_eq!(parse_allow_extend_session(&root), Some(true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_allow_extend_session_no() {
|
||||
let root = parse_xml("<response><allow-extend-session>no</allow-extend-session></response>");
|
||||
|
||||
assert_eq!(parse_allow_extend_session(&root), Some(false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn leaves_absent_allow_extend_session_unknown() {
|
||||
let root = parse_xml("<response></response>");
|
||||
|
||||
assert_eq!(parse_allow_extend_session(&root), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ignores_non_official_allow_extend_session_values() {
|
||||
let root = parse_xml("<response><allow-extend-session>true</allow-extend-session></response>");
|
||||
|
||||
assert_eq!(parse_allow_extend_session(&root), None);
|
||||
}
|
||||
}
|
||||
@@ -52,6 +52,8 @@ pub struct ConnectArgs {
|
||||
local_hostname: Option<String>,
|
||||
force_dpd: u32,
|
||||
no_xmlpost: bool,
|
||||
#[serde(rename = "allowExtendSession")]
|
||||
allow_extend_session: bool,
|
||||
}
|
||||
|
||||
impl ConnectArgs {
|
||||
@@ -76,6 +78,7 @@ impl ConnectArgs {
|
||||
local_hostname: None,
|
||||
force_dpd: 0,
|
||||
no_xmlpost: false,
|
||||
allow_extend_session: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,6 +161,10 @@ impl ConnectArgs {
|
||||
pub fn no_xmlpost(&self) -> bool {
|
||||
self.no_xmlpost
|
||||
}
|
||||
|
||||
pub fn allow_extend_session(&self) -> bool {
|
||||
self.allow_extend_session
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Type, Clone)]
|
||||
@@ -264,6 +271,11 @@ impl ConnectRequest {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_allow_extend_session(mut self, allow_extend_session: bool) -> Self {
|
||||
self.args.allow_extend_session = allow_extend_session;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn gateway(&self) -> &Gateway {
|
||||
self.info.gateway()
|
||||
}
|
||||
@@ -296,3 +308,20 @@ pub struct UpdateGuiRequest {
|
||||
pub path: String,
|
||||
pub checksum: String,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::json;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn connect_request_serializes_allow_extend_session() {
|
||||
let gateway = Gateway::new("Gateway".to_string(), "vpn.example.com".to_string());
|
||||
let info = ConnectInfo::new("portal.example.com".to_string(), gateway.clone(), vec![gateway]);
|
||||
let req = ConnectRequest::new(info, "authcookie=AUTH".to_string()).with_allow_extend_session(true);
|
||||
let value = serde_json::to_value(req).unwrap();
|
||||
|
||||
assert_eq!(value["args"]["allowExtendSession"], json!(true));
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ diff --git a/gpst.c b/gpst.c
|
||||
index 244aaf04..78c16d1c 100644
|
||||
--- a/gpst.c
|
||||
+++ b/gpst.c
|
||||
@@ -438,8 +438,23 @@ static int gpst_parse_config_xml(struct openconnect_info *vpninfo, xmlNode *xml_
|
||||
@@ -438,8 +438,19 @@ static int gpst_parse_config_xml(struct openconnect_info *vpninfo, xmlNode *xml_
|
||||
new_ip_info.netmask = add_option_steal(&new_opts, "netmask", &s);
|
||||
} else if (!xmlnode_get_val(xml_node, "mtu", &s))
|
||||
new_ip_info.mtu = atoi(s);
|
||||
@@ -19,10 +19,6 @@ index 244aaf04..78c16d1c 100644
|
||||
+ free(vpninfo->gp_lifetime_notify_message);
|
||||
+ vpninfo->gp_lifetime_notify_message = s;
|
||||
+ s = NULL;
|
||||
+ } else if (!xmlnode_get_val(xml_node, "allow-extend-session", &s)) {
|
||||
+ vpninfo->gp_allow_extend_session = !strcasecmp(s, "yes") ||
|
||||
+ !strcasecmp(s, "true") ||
|
||||
+ !strcmp(s, "1");
|
||||
+ }
|
||||
else if (!xmlnode_get_val(xml_node, "quarantine", &s)) {
|
||||
if (strcmp(s, "no"))
|
||||
@@ -31,7 +27,7 @@ diff --git a/libopenconnect.map.in b/libopenconnect.map.in
|
||||
index f036c071..44110486 100644
|
||||
--- a/libopenconnect.map.in
|
||||
+++ b/libopenconnect.map.in
|
||||
@@ -113,6 +113,11 @@ OPENCONNECT_5_7 {
|
||||
@@ -113,6 +113,10 @@ OPENCONNECT_5_7 {
|
||||
openconnect_set_cookie;
|
||||
openconnect_set_allow_insecure_crypto;
|
||||
openconnect_get_auth_expiration;
|
||||
@@ -39,7 +35,6 @@ index f036c071..44110486 100644
|
||||
+ openconnect_get_gp_user_expires;
|
||||
+ openconnect_get_gp_lifetime_notify_prior;
|
||||
+ openconnect_get_gp_lifetime_notify_message;
|
||||
+ openconnect_get_gp_allow_extend_session;
|
||||
openconnect_disable_dtls;
|
||||
openconnect_get_connect_url;
|
||||
openconnect_set_webview_callback;
|
||||
@@ -47,7 +42,7 @@ diff --git a/libopenconnect5.symbols b/libopenconnect5.symbols
|
||||
index 7cb04d05..2c7af781 100644
|
||||
--- a/libopenconnect5.symbols
|
||||
+++ b/libopenconnect5.symbols
|
||||
@@ -94,6 +94,11 @@ libopenconnect.so.5 libopenconnect5 #MINVER#
|
||||
@@ -94,6 +94,10 @@ libopenconnect.so.5 libopenconnect5 #MINVER#
|
||||
openconnect_set_cookie@OPENCONNECT_5_7 8.20
|
||||
openconnect_set_allow_insecure_crypto@OPENCONNECT_5_7 8.20
|
||||
openconnect_get_auth_expiration@OPENCONNECT_5_7 8.20
|
||||
@@ -55,7 +50,6 @@ index 7cb04d05..2c7af781 100644
|
||||
+ openconnect_get_gp_user_expires@OPENCONNECT_5_7 8.20
|
||||
+ openconnect_get_gp_lifetime_notify_prior@OPENCONNECT_5_7 8.20
|
||||
+ openconnect_get_gp_lifetime_notify_message@OPENCONNECT_5_7 8.20
|
||||
+ openconnect_get_gp_allow_extend_session@OPENCONNECT_5_7 8.20
|
||||
openconnect_disable_dtls@OPENCONNECT_5_7 8.20
|
||||
openconnect_get_connect_url@OPENCONNECT_5_7 8.20
|
||||
openconnect_set_webview_callback@OPENCONNECT_5_7 8.20
|
||||
@@ -71,7 +65,7 @@ index b5e74e9b..03f47004 100644
|
||||
#if defined(OPENCONNECT_OPENSSL)
|
||||
free(vpninfo->cstp_cipher);
|
||||
#if defined(HAVE_BIO_METH_FREE)
|
||||
@@ -1137,6 +1138,31 @@ time_t openconnect_get_auth_expiration(struct openconnect_info *vpninfo)
|
||||
@@ -1137,6 +1138,26 @@ time_t openconnect_get_auth_expiration(struct openconnect_info *vpninfo)
|
||||
return vpninfo->auth_expiration;
|
||||
}
|
||||
|
||||
@@ -94,11 +88,6 @@ index b5e74e9b..03f47004 100644
|
||||
+{
|
||||
+ return vpninfo->gp_lifetime_notify_message;
|
||||
+}
|
||||
+
|
||||
+int openconnect_get_gp_allow_extend_session(struct openconnect_info *vpninfo)
|
||||
+{
|
||||
+ return vpninfo->gp_allow_extend_session;
|
||||
+}
|
||||
+
|
||||
int openconnect_get_ip_info(struct openconnect_info *vpninfo,
|
||||
const struct oc_ip_info **info,
|
||||
@@ -107,7 +96,7 @@ diff --git a/openconnect-internal.h b/openconnect-internal.h
|
||||
index abf45bbd..dd5507ca 100644
|
||||
--- a/openconnect-internal.h
|
||||
+++ b/openconnect-internal.h
|
||||
@@ -647,6 +647,11 @@ struct openconnect_info {
|
||||
@@ -647,6 +647,10 @@ struct openconnect_info {
|
||||
int dtls_attempt_period;
|
||||
int udp_probes_sent;
|
||||
time_t auth_expiration;
|
||||
@@ -115,7 +104,6 @@ index abf45bbd..dd5507ca 100644
|
||||
+ time_t gp_user_expires;
|
||||
+ int gp_lifetime_notify_prior;
|
||||
+ char *gp_lifetime_notify_message;
|
||||
+ int gp_allow_extend_session;
|
||||
time_t new_dtls_started;
|
||||
#if defined(OPENCONNECT_OPENSSL)
|
||||
SSL_CTX *dtls_ctx;
|
||||
@@ -123,7 +111,7 @@ diff --git a/openconnect.h b/openconnect.h
|
||||
index 136f181a..febf15cc 100644
|
||||
--- a/openconnect.h
|
||||
+++ b/openconnect.h
|
||||
@@ -655,6 +655,11 @@ void openconnect_set_dpd(struct openconnect_info *, int min_seconds);
|
||||
@@ -655,6 +655,10 @@ void openconnect_set_dpd(struct openconnect_info *, int min_seconds);
|
||||
void openconnect_set_trojan_interval(struct openconnect_info *, int seconds);
|
||||
int openconnect_get_idle_timeout(struct openconnect_info *);
|
||||
time_t openconnect_get_auth_expiration(struct openconnect_info *);
|
||||
@@ -131,7 +119,6 @@ index 136f181a..febf15cc 100644
|
||||
+time_t openconnect_get_gp_user_expires(struct openconnect_info *);
|
||||
+int openconnect_get_gp_lifetime_notify_prior(struct openconnect_info *);
|
||||
+const char *openconnect_get_gp_lifetime_notify_message(struct openconnect_info *);
|
||||
+int openconnect_get_gp_allow_extend_session(struct openconnect_info *);
|
||||
|
||||
/* The returned structures are owned by the library and may be freed/replaced
|
||||
due to rekey or reconnect. Assume that once the mainloop starts, the
|
||||
@@ -46,7 +46,6 @@ pub(crate) struct VpnSessionInfoRaw {
|
||||
pub user_expires: c_long,
|
||||
pub lifetime_warning_prior: c_int,
|
||||
pub lifetime_warning_message: *const c_char,
|
||||
pub allow_extend_session: c_int,
|
||||
}
|
||||
|
||||
#[link(name = "vpn")]
|
||||
|
||||
@@ -59,8 +59,6 @@ static void setup_tun_handler(void *_vpninfo)
|
||||
openconnect_get_gp_lifetime_notify_prior(_vpninfo),
|
||||
.lifetime_warning_message =
|
||||
openconnect_get_gp_lifetime_notify_message(_vpninfo),
|
||||
.allow_extend_session =
|
||||
openconnect_get_gp_allow_extend_session(_vpninfo),
|
||||
};
|
||||
on_vpn_connected(g_cmd_pipe_fd, &session_info, g_user_data);
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ typedef struct vpn_session_info {
|
||||
long user_expires;
|
||||
int lifetime_warning_prior;
|
||||
const char *lifetime_warning_message;
|
||||
int allow_extend_session;
|
||||
} vpn_session_info;
|
||||
|
||||
typedef void (*vpn_connected_callback)(int cmd_pipe_fd,
|
||||
|
||||
@@ -16,7 +16,6 @@ pub struct VpnSessionInfo {
|
||||
pub lifetime_secs: Option<u32>,
|
||||
pub user_expires: Option<u32>,
|
||||
pub lifetime_warning: Option<VpnSessionWarning>,
|
||||
pub allow_extend_session: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
@@ -42,7 +41,6 @@ pub(crate) fn session_info_from_raw(raw: *const ffi::VpnSessionInfoRaw) -> VpnSe
|
||||
(Some(prior_secs), Some(message)) => Some(VpnSessionWarning { prior_secs, message }),
|
||||
_ => None,
|
||||
},
|
||||
allow_extend_session: raw.allow_extend_session != 0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -424,7 +422,6 @@ mod tests {
|
||||
user_expires: 1_776_828_409,
|
||||
lifetime_warning_prior: 1_800,
|
||||
lifetime_warning_message: message.as_ptr(),
|
||||
allow_extend_session: 1,
|
||||
};
|
||||
|
||||
let info = session_info_from_raw(&raw);
|
||||
@@ -438,7 +435,6 @@ mod tests {
|
||||
message: "Session expires soon".to_string(),
|
||||
})
|
||||
);
|
||||
assert!(info.allow_extend_session);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -449,7 +445,6 @@ mod tests {
|
||||
user_expires: 0,
|
||||
lifetime_warning_prior: 0,
|
||||
lifetime_warning_message: std::ptr::null(),
|
||||
allow_extend_session: 0,
|
||||
};
|
||||
|
||||
let info = session_info_from_raw(&raw);
|
||||
@@ -457,6 +452,5 @@ mod tests {
|
||||
assert_eq!(info.user_expires, Some(1_776_828_409));
|
||||
assert_eq!(info.lifetime_secs, None);
|
||||
assert_eq!(info.lifetime_warning, None);
|
||||
assert!(!info.allow_extend_session);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user