mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2026-11-20 06:56:17 -05:00
feat(hip): manage reports and script execution
This commit is contained in:
54 files changed
+3122
-967
No files matched your search
@@ -7,6 +7,7 @@ use std::ffi::{c_char, c_int, c_long, c_void};
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct ConnectOptions {
|
||||
pub user_data: *mut c_void,
|
||||
pub on_hip_report_submitted: Option<extern "C" fn(*mut c_void, *const c_char, usize)>,
|
||||
|
||||
pub server: *const c_char,
|
||||
pub cookie: *const c_char,
|
||||
@@ -85,6 +86,18 @@ pub(crate) fn connect(options: &ConnectOptions) -> i32 {
|
||||
unsafe { vpn_connect(options, on_vpn_connected) }
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub(crate) extern "C" fn on_hip_report_submitted(vpn: *mut c_void, report: *const c_char, length: usize) {
|
||||
let vpn = unsafe { &*(vpn as *const Vpn) };
|
||||
if report.is_null() || length > 1024 * 1024 {
|
||||
return;
|
||||
}
|
||||
let bytes = unsafe { std::slice::from_raw_parts(report.cast::<u8>(), length) };
|
||||
if let Ok(report) = std::str::from_utf8(bytes) {
|
||||
vpn.on_hip_report_submitted(report);
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn on_vpn_connected(pipe_fd: i32, session_info: *const VpnSessionInfoRaw, vpn: *mut c_void) {
|
||||
let vpn = unsafe { &*(vpn as *const Vpn) };
|
||||
|
||||
@@ -105,6 +105,8 @@ int vpn_connect(const vpn_options *options, vpn_connected_callback callback)
|
||||
}
|
||||
|
||||
context.vpninfo = vpninfo;
|
||||
openconnect_set_gp_hip_report_callback(vpninfo, options->user_data,
|
||||
options->on_hip_report_submitted);
|
||||
context.command_fd = openconnect_setup_cmd_pipe(vpninfo);
|
||||
if (context.command_fd < 0) {
|
||||
ERROR("openconnect_setup_cmd_pipe failed");
|
||||
|
||||
@@ -16,9 +16,12 @@ typedef struct vpn_session_info {
|
||||
typedef void (*vpn_connected_callback)(int cmd_pipe_fd,
|
||||
const vpn_session_info *session_info,
|
||||
void *user_data);
|
||||
typedef void (*vpn_hip_report_callback)(void *user_data, const char *report,
|
||||
size_t length);
|
||||
|
||||
typedef struct vpn_options {
|
||||
void *user_data;
|
||||
vpn_hip_report_callback on_hip_report_submitted;
|
||||
|
||||
const char *server;
|
||||
const char *cookie;
|
||||
|
||||
@@ -10,6 +10,7 @@ use crate::ffi;
|
||||
use crate::vpn_utils::{check_executable, find_csd_wrapper, find_vpnc_script};
|
||||
|
||||
type OnConnectedCallback = Arc<RwLock<Option<Box<dyn FnOnce(VpnSessionInfo) + 'static + Send + Sync>>>>;
|
||||
type OnHipReportCallback = RwLock<Option<Arc<dyn Fn(&str) + Send + Sync>>>;
|
||||
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct VpnSessionInfo {
|
||||
@@ -130,6 +131,7 @@ pub struct Vpn {
|
||||
no_xmlpost: bool,
|
||||
|
||||
callback: OnConnectedCallback,
|
||||
hip_report_callback: OnHipReportCallback,
|
||||
}
|
||||
|
||||
impl Vpn {
|
||||
@@ -144,6 +146,19 @@ impl Vpn {
|
||||
ffi::connect(&options)
|
||||
}
|
||||
|
||||
/// Receives a borrowed report after the gateway accepts its HIP submission.
|
||||
/// The callback must copy the XML if it needs to retain it.
|
||||
pub fn set_hip_report_callback(&self, callback: impl Fn(&str) + Send + Sync + 'static) {
|
||||
*self.hip_report_callback.write().unwrap() = Some(Arc::new(callback));
|
||||
}
|
||||
|
||||
pub(crate) fn on_hip_report_submitted(&self, report: &str) {
|
||||
let callback = self.hip_report_callback.read().unwrap().clone();
|
||||
if let Some(callback) = callback {
|
||||
callback(report);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn on_connected(&self, pipe_fd: i32, session_info: VpnSessionInfo) {
|
||||
info!("Connected to VPN, pipe_fd: {}", pipe_fd);
|
||||
|
||||
@@ -169,6 +184,7 @@ impl Vpn {
|
||||
fn build_connect_options(&self) -> ffi::ConnectOptions {
|
||||
ffi::ConnectOptions {
|
||||
user_data: self as *const _ as *mut _,
|
||||
on_hip_report_submitted: Some(ffi::on_hip_report_submitted),
|
||||
|
||||
server: self.server.as_ptr(),
|
||||
cookie: self.cookie.as_ptr(),
|
||||
@@ -480,6 +496,7 @@ impl VpnBuilder {
|
||||
no_xmlpost: self.no_xmlpost,
|
||||
|
||||
callback: Default::default(),
|
||||
hip_report_callback: Default::default(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -603,6 +620,7 @@ mod tests {
|
||||
dpd_interval: 0,
|
||||
no_xmlpost: false,
|
||||
callback: Default::default(),
|
||||
hip_report_callback: Default::default(),
|
||||
};
|
||||
|
||||
let options = vpn.build_connect_options();
|
||||
@@ -611,6 +629,25 @@ mod tests {
|
||||
assert_eq!(host_id, "profile-host-id");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hip_report_callback_copies_only_bounded_utf8() {
|
||||
let vpn = Vpn::builder("vpn.example.com", "cookie")
|
||||
.script_path("/bin/sh".to_string())
|
||||
.build()
|
||||
.unwrap();
|
||||
let reports = Arc::new(Mutex::new(Vec::new()));
|
||||
let received = Arc::clone(&reports);
|
||||
vpn.set_hip_report_callback(move |report| received.lock().unwrap().push(report.to_owned()));
|
||||
|
||||
let options = vpn.build_connect_options();
|
||||
let callback = options.on_hip_report_submitted.unwrap();
|
||||
callback(options.user_data, b"<report/>".as_ptr().cast(), 9);
|
||||
callback(options.user_data, b"\xff".as_ptr().cast(), 1);
|
||||
callback(options.user_data, b"x".as_ptr().cast(), 1024 * 1024 + 1);
|
||||
|
||||
assert_eq!(*reports.lock().unwrap(), ["<report/>"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn disconnect_before_connect_is_retained_without_contacting_server() {
|
||||
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
|
||||
Reference in new issue
Block a user