fix: use portal policy for session extension

This commit is contained in:
Kevin Yue committed 2026-05-06 23:20:57 +08:00
1 parent 731a0d895c
commit fb6db0dc12
10 files changed
+164 -49

No files matched your search

+24 -9
View File
@@ -294,7 +294,9 @@ impl<'a> ConnectHandler<'a> {
if as_gateway {
info!("Treating the server as a gateway");
return self.connect_gateway_with_prelogin(server).await;
return self
.connect_gateway_with_prelogin(server, server, self.args.client_version.as_deref(), false)
.await;
}
let Err(err) = self.connect_portal_with_prelogin(server).await else {
@@ -304,7 +306,9 @@ impl<'a> ConnectHandler<'a> {
warn!("Failed to connect portal with prelogin: {}", err);
if err.root_cause().downcast_ref::<PortalError>().is_some() {
info!("Trying the gateway authentication workflow...");
self.connect_gateway_with_prelogin(server).await?;
self
.connect_gateway_with_prelogin(server, server, self.args.client_version.as_deref(), false)
.await?;
eprintln!("\nNOTE: the server may be a gateway, not a portal.");
eprintln!("NOTE: try to use the `--as-gateway` option if you were authenticated twice.");
@@ -351,13 +355,18 @@ impl<'a> ConnectHandler<'a> {
Ok(login_session) => login_session,
Err(err) => {
info!("Gateway login failed: {}", err);
return self.connect_gateway_with_prelogin(gateway).await;
let client_version = self.args.client_version.as_deref().or_else(|| portal_config.version());
let allow_extend_session = portal_config.allow_extend_session().unwrap_or(false);
return self
.connect_gateway_with_prelogin(portal, gateway, client_version, allow_extend_session)
.await;
}
};
// Use the client version from the command line argument if specified, otherwise
// use the version from the portal config if available
let client_version = self.args.client_version.as_deref().or_else(|| portal_config.version());
let allow_extend_session = portal_config.allow_extend_session().unwrap_or(false);
self
.connect_gateway(
@@ -365,12 +374,19 @@ impl<'a> ConnectHandler<'a> {
gateway,
&login_session.cookie,
client_version,
allow_extend_session,
login_session.extension_auth,
)
.await
}
async fn connect_gateway_with_prelogin(&self, gateway: &str) -> anyhow::Result<()> {
async fn connect_gateway_with_prelogin(
&self,
portal: &str,
gateway: &str,
client_version: Option<&str>,
allow_extend_session: bool,
) -> anyhow::Result<()> {
info!("Performing the gateway authentication...");
let mut gp_params = self.build_gp_params();
@@ -381,15 +397,13 @@ impl<'a> ConnectHandler<'a> {
let login_session = self.login_gateway(gateway, &cred, &gp_params).await?;
// When logging in to a gateway directly, there is no portal config to get the client version from
let client_version = self.args.client_version.as_deref();
self
.connect_gateway(
gateway,
portal,
gateway,
&login_session.cookie,
client_version,
allow_extend_session,
login_session.extension_auth,
)
.await
@@ -428,6 +442,7 @@ impl<'a> ConnectHandler<'a> {
gateway: &str,
cookie: &str,
client_version: Option<&str>,
allow_extend_session: bool,
extension_auth: SessionExtensionAuth,
) -> anyhow::Result<()> {
let mtu = self.args.mtu.unwrap_or(0);
@@ -496,7 +511,7 @@ impl<'a> ConnectHandler<'a> {
let Some(session_ctx) = session_ctx_on_connect.lock().unwrap().take() else {
return;
};
let session_info = session_info_from_vpn(vpn_session_info);
let session_info = session_info_from_vpn(vpn_session_info, allow_extend_session);
info!("VPN session info: {}", session_info.log_summary());
let task = spawn_session_runtime_with_info(&runtime_handle, session_ctx, session_info);
+31 -9
View File
@@ -52,7 +52,7 @@ pub(crate) fn build_session_context(input: SessionContextInput) -> SessionContex
}
}
pub(crate) fn session_info_from_vpn(vpn_session_info: VpnSessionInfo) -> SessionInfo {
pub(crate) fn session_info_from_vpn(vpn_session_info: VpnSessionInfo, allow_extend_session: bool) -> SessionInfo {
SessionInfo::from_vpn_session_fields(
vpn_session_info.lifetime_secs,
vpn_session_info.user_expires,
@@ -60,7 +60,7 @@ pub(crate) fn session_info_from_vpn(vpn_session_info: VpnSessionInfo) -> Session
prior_secs: warning.prior_secs,
message: warning.message,
}),
vpn_session_info.allow_extend_session,
allow_extend_session,
)
}
@@ -212,13 +212,15 @@ mod tests {
#[test]
fn maps_openconnect_session_metadata_to_runtime_session_info() {
let info = SessionInfo::from_vpn_session_fields(
Some(43_200),
Some(1_776_828_409),
Some(SessionWarning {
prior_secs: 1_800,
message: "Session expires soon".to_string(),
}),
let info = session_info_from_vpn(
VpnSessionInfo {
lifetime_secs: Some(43_200),
user_expires: Some(1_776_828_409),
lifetime_warning: Some(openconnect::VpnSessionWarning {
prior_secs: 1_800,
message: "Session expires soon".to_string(),
}),
},
true,
);
@@ -227,4 +229,24 @@ mod tests {
assert_eq!(info.lifetime_warning.unwrap().prior_secs, 1_800);
assert!(info.allow_extend_session);
}
#[test]
fn direct_gateway_session_info_does_not_allow_extension_without_portal_policy() {
let info = session_info_from_vpn(
VpnSessionInfo {
lifetime_secs: Some(43_200),
user_expires: Some(1_776_828_409),
lifetime_warning: Some(openconnect::VpnSessionWarning {
prior_secs: 1_800,
message: "Session expires soon".to_string(),
}),
},
false,
);
assert_eq!(info.lifetime_secs, Some(43_200));
assert_eq!(info.user_expires, Some(1_776_828_409));
assert_eq!(info.lifetime_warning.unwrap().prior_secs, 1_800);
assert!(!info.allow_extend_session);
}
}
+19 -2
View File
@@ -4,9 +4,9 @@ use gpapi::{
logger,
service::{
request::{ConnectRequest, UpdateLogLevelRequest, WsRequest},
session::{SessionInfo, SessionWarning},
vpn_state::{ConnectedInfo, VpnState},
},
session::{SessionInfo, SessionWarning},
};
use log::{info, warn};
use openconnect::Vpn;
@@ -39,6 +39,7 @@ impl VpnTaskContext {
let info = req.info().clone();
let vpn_handle = Arc::clone(&self.vpn_handle);
let args = req.args();
let allow_extend_session = args.allow_extend_session();
let vpn = match Vpn::builder(req.gateway().server(), args.cookie())
.script(args.vpnc_script())
.user_agent(args.user_agent())
@@ -90,7 +91,7 @@ impl VpnTaskContext {
prior_secs: warning.prior_secs,
message: warning.message,
}),
vpn_session_info.allow_extend_session,
allow_extend_session,
);
info!("VPN session info: {}", session_info.log_summary());
let connected_info = Box::new(ConnectedInfo::new(info.clone(), Some(session_info)));
@@ -216,4 +217,20 @@ mod tests {
assert_eq!(info.lifetime_warning.unwrap().prior_secs, 1_800);
assert!(info.allow_extend_session);
}
#[test]
fn direct_request_session_metadata_keeps_extension_disabled() {
let info = SessionInfo::from_vpn_session_fields(
Some(43_200),
None,
Some(SessionWarning {
prior_secs: 1_800,
message: "Session expires soon".to_string(),
}),
false,
);
assert_eq!(info.lifetime_secs, Some(43_200));
assert!(!info.allow_extend_session);
}
}
+55
View File
@@ -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);
}
}
+29
View File
@@ -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
-1
View File
@@ -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")]
-2
View File
@@ -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);
}
-1
View File
@@ -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,
-6
View File
@@ -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);
}
}