mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2026-11-19 06:26:14 -05:00
feat: add portable GlobalProtect APIs and iOS tunnel support
This commit is contained in:
112 files changed
+3864
-3011
No files matched your search
+8
-246
@@ -1,250 +1,12 @@
|
||||
use std::{env, path::PathBuf};
|
||||
|
||||
fn build_libxml2(deps_dir: &PathBuf, out_dir: &PathBuf) -> PathBuf {
|
||||
let libxml2_dir = deps_dir.join("libxml2");
|
||||
let install_dir = out_dir.join("libxml2_install");
|
||||
|
||||
// The temporary location where we will build
|
||||
let build_src = out_dir.join("libxml2_build");
|
||||
|
||||
// Track changes so cargo rebuilds if submodule changes
|
||||
println!("cargo:rerun-if-changed={}", libxml2_dir.display());
|
||||
|
||||
// Prepare the build directory
|
||||
// If the directory exists from a previous run, remove it to ensure a clean slate
|
||||
if build_src.exists() {
|
||||
std::fs::remove_dir_all(&build_src).unwrap();
|
||||
}
|
||||
if install_dir.exists() {
|
||||
std::fs::remove_dir_all(&install_dir).unwrap();
|
||||
}
|
||||
std::fs::create_dir_all(&install_dir).unwrap();
|
||||
std::fs::create_dir_all(&build_src).unwrap();
|
||||
|
||||
// Copy the source code to OUT_DIR
|
||||
// We use fs_extra options to copy contents recursively
|
||||
let mut options = fs_extra::dir::CopyOptions::new();
|
||||
options.overwrite = true;
|
||||
options.content_only = true; // Copy content of src into dest, not src folder itself
|
||||
|
||||
fs_extra::dir::copy(&libxml2_dir, &build_src, &options).expect("Failed to copy C source code to OUT_DIR");
|
||||
|
||||
// Build the libxml2 library using autotools
|
||||
let dst = {
|
||||
let mut config = autotools::Config::new(&build_src);
|
||||
config
|
||||
.out_dir(&install_dir)
|
||||
.reconf("-ivf")
|
||||
.enable_static()
|
||||
.disable_shared()
|
||||
// Disable optional features that complicate static builds
|
||||
.with("python", Some("no"))
|
||||
.with("icu", Some("no"))
|
||||
.with("http", Some("no"))
|
||||
.with("ftp", Some("no"))
|
||||
.with("catalog", Some("no"))
|
||||
.with("docbook", Some("no"))
|
||||
.with("legacy", Some("no"))
|
||||
.with("threads", Some("no"))
|
||||
.with("zlib", Some("no"))
|
||||
.with("lzma", Some("no"))
|
||||
// Ensure position-independent code (safe for static linking)
|
||||
.cflag("-fPIC");
|
||||
config.build()
|
||||
};
|
||||
|
||||
let lib_dir = dst.join("lib");
|
||||
let include_dir = dst.join("include/libxml2");
|
||||
|
||||
// Tell rustc where to find the static library
|
||||
println!("cargo:rustc-link-search=native={}", lib_dir.display());
|
||||
|
||||
// Static linking of libxml2
|
||||
println!("cargo:rustc-link-lib=static=xml2");
|
||||
|
||||
// Export include path for downstream use (bindgen / cc)
|
||||
println!("cargo:include={}", include_dir.display());
|
||||
|
||||
dst
|
||||
}
|
||||
|
||||
fn build_openconnect(deps_dir: &PathBuf, out_dir: &PathBuf) -> PathBuf {
|
||||
let openconnect_dir = deps_dir.join("openconnect");
|
||||
let install_dir = out_dir.join("openconnect_install");
|
||||
|
||||
// The temporary location where we will build
|
||||
let build_src = out_dir.join("openconnect_build");
|
||||
|
||||
// Track changes so cargo rebuilds if submodule changes
|
||||
println!("cargo:rerun-if-changed={}", openconnect_dir.display());
|
||||
|
||||
// Prepare the build directory
|
||||
// If the directory exists from a previous run, remove it to ensure a clean slate
|
||||
if build_src.exists() {
|
||||
std::fs::remove_dir_all(&build_src).unwrap();
|
||||
}
|
||||
if install_dir.exists() {
|
||||
std::fs::remove_dir_all(&install_dir).unwrap();
|
||||
}
|
||||
std::fs::create_dir_all(&install_dir).unwrap();
|
||||
std::fs::create_dir_all(&build_src).unwrap();
|
||||
|
||||
// Copy the source code to OUT_DIR
|
||||
// We use fs_extra options to copy contents recursively
|
||||
let mut options = fs_extra::dir::CopyOptions::new();
|
||||
options.overwrite = true;
|
||||
options.content_only = true; // Copy content of src into dest, not src folder itself
|
||||
|
||||
fs_extra::dir::copy(&openconnect_dir, &build_src, &options).expect("Failed to copy C source code to OUT_DIR");
|
||||
|
||||
// Build the OpenConnect library using autotools. Keep the upstream LGPL
|
||||
// library dynamically linked on macOS so it remains replaceable in the app
|
||||
// bundle. Other platforms retain the existing static-link behavior.
|
||||
let mut config = autotools::Config::new(&build_src);
|
||||
config
|
||||
.out_dir(&install_dir)
|
||||
.reconf("-ivf")
|
||||
.disable("nls", None) // disable translations to save space
|
||||
.disable("docs", None)
|
||||
.disable("dsa-tests", None)
|
||||
.without("libproxy", None)
|
||||
.without("stoken", None)
|
||||
.without("libpcsclite", None)
|
||||
.without("libpskc", None)
|
||||
.without("gssapi", None)
|
||||
.with("gnutls-tss2", Some("no"))
|
||||
.with("vpnc-script", Some("/etc/vpnc/vpnc-script")); // Specify vpnc-script, otherwise it will fail on macOS
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
config
|
||||
.disable_static()
|
||||
.enable_shared()
|
||||
.with("gnutls", None)
|
||||
.without("openssl", None);
|
||||
config.ldflag("-liconv");
|
||||
config.env("LIBS", "-liconv");
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
config.enable_static().disable_shared();
|
||||
|
||||
#[cfg(any(target_os = "freebsd", target_os = "openbsd"))]
|
||||
{
|
||||
config.ldflag("-L/usr/local/lib");
|
||||
config.ldflag("-liconv");
|
||||
config.env("LIBS", "-liconv");
|
||||
}
|
||||
|
||||
let dst = config.build();
|
||||
|
||||
let lib_dir = dst.join("lib");
|
||||
let include_dir = dst.join("include");
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
let configuration = std::fs::read_to_string(dst.join("build/config.h"))
|
||||
.expect("Failed to read the OpenConnect TLS backend configuration");
|
||||
assert!(
|
||||
configuration.lines().any(|line| line == "#define OPENCONNECT_GNUTLS 1")
|
||||
&& !configuration
|
||||
.lines()
|
||||
.any(|line| line == "#define OPENCONNECT_OPENSSL 1"),
|
||||
"macOS OpenConnect must use GnuTLS"
|
||||
);
|
||||
// Do not embed Cargo's temporary build directory in consumers of this library.
|
||||
let library = std::fs::canonicalize(lib_dir.join("libopenconnect.dylib")).unwrap();
|
||||
let library_name = library.file_name().unwrap().to_str().unwrap();
|
||||
let status = std::process::Command::new("install_name_tool")
|
||||
.arg("-id")
|
||||
.arg(format!("@rpath/{library_name}"))
|
||||
.arg(&library)
|
||||
.status()
|
||||
.expect("Failed to update the OpenConnect dylib install name");
|
||||
assert!(status.success(), "Failed to update the OpenConnect dylib install name");
|
||||
let status = std::process::Command::new("codesign")
|
||||
.args(["--force", "--sign", "-"])
|
||||
.arg(&library)
|
||||
.status()
|
||||
.expect("Failed to sign the OpenConnect dylib");
|
||||
assert!(status.success(), "Failed to sign the OpenConnect dylib");
|
||||
println!("cargo:lib_dir={}", lib_dir.display());
|
||||
}
|
||||
|
||||
println!("cargo:rustc-link-search=native={}", lib_dir.display());
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
println!("cargo:rustc-link-lib=dylib=openconnect");
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
println!("cargo:rustc-link-lib=static=openconnect");
|
||||
|
||||
// Export include path for downstream use (bindgen / cc)
|
||||
println!("cargo:include={}", include_dir.display());
|
||||
|
||||
dst
|
||||
}
|
||||
#[path = "build/desktop.rs"]
|
||||
mod desktop;
|
||||
#[path = "build/ios.rs"]
|
||||
mod ios;
|
||||
|
||||
fn main() {
|
||||
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
|
||||
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
|
||||
let deps_dir = PathBuf::from(manifest_dir).join("deps");
|
||||
|
||||
let oc_dst = build_openconnect(&deps_dir, &out_dir);
|
||||
println!(
|
||||
"cargo:rustc-env=OPENCONNECT_PROTOCOL_BUILD={}",
|
||||
oc_dst.join("build").display()
|
||||
);
|
||||
println!(
|
||||
"cargo:rustc-env=OPENCONNECT_PROTOCOL_SOURCE={}",
|
||||
out_dir.join("openconnect_build").display()
|
||||
);
|
||||
|
||||
// Only statically link libxml2 if `LIBXML2_STATIC` is set
|
||||
if env::var("LIBXML2_STATIC").is_ok() {
|
||||
let _libxml2_dst = build_libxml2(&deps_dir, &out_dir);
|
||||
} else {
|
||||
// Using pkg-config to find system libxml2
|
||||
pkg_config::probe_library("libxml-2.0").unwrap();
|
||||
match std::env::var("CARGO_CFG_TARGET_OS").as_deref() {
|
||||
Ok("ios") => ios::build(),
|
||||
Ok("macos" | "linux" | "freebsd" | "openbsd") => desktop::build(),
|
||||
target => panic!("Unsupported OpenConnect target: {target:?}"),
|
||||
}
|
||||
|
||||
// Using pkg-config to find system deps
|
||||
pkg_config::probe_library("zlib").unwrap();
|
||||
#[cfg(target_os = "macos")]
|
||||
pkg_config::probe_library("gnutls").unwrap();
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
{
|
||||
pkg_config::probe_library("liblz4").unwrap();
|
||||
pkg_config::probe_library("gnutls").unwrap();
|
||||
|
||||
// Below are required by gnutls
|
||||
pkg_config::probe_library("p11-kit-1").unwrap();
|
||||
pkg_config::probe_library("hogweed").unwrap();
|
||||
pkg_config::probe_library("nettle").unwrap();
|
||||
pkg_config::probe_library("gmp").unwrap();
|
||||
}
|
||||
|
||||
// Compile the vpn.c file
|
||||
println!("cargo:rerun-if-changed=src/ffi/vpn.c");
|
||||
println!("cargo:rerun-if-changed=src/ffi/vpn.h");
|
||||
|
||||
#[cfg(any(target_os = "freebsd", target_os = "openbsd"))]
|
||||
{
|
||||
println!("cargo:rustc-link-search=native=/usr/local/lib");
|
||||
println!("cargo:rustc-link-lib=iconv");
|
||||
}
|
||||
|
||||
// Prevent silent fallback to dynamic libraries
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
println!("cargo:rustc-link-arg=-Wl,-Bstatic");
|
||||
println!("cargo:rustc-link-arg=-Wl,-Bdynamic");
|
||||
}
|
||||
|
||||
cc::Build::new()
|
||||
.file("src/ffi/vpn.c")
|
||||
.include("src/ffi")
|
||||
.include(&oc_dst.join("include"))
|
||||
.compile("vpn");
|
||||
}
|
||||
@@ -0,0 +1,252 @@
|
||||
use std::{env, path::PathBuf};
|
||||
|
||||
fn build_libxml2(deps_dir: &PathBuf, out_dir: &PathBuf) -> PathBuf {
|
||||
let libxml2_dir = deps_dir.join("libxml2");
|
||||
let install_dir = out_dir.join("libxml2_install");
|
||||
|
||||
// The temporary location where we will build
|
||||
let build_src = out_dir.join("libxml2_build");
|
||||
|
||||
// Track changes so cargo rebuilds if submodule changes
|
||||
println!("cargo:rerun-if-changed={}", libxml2_dir.display());
|
||||
|
||||
// Prepare the build directory
|
||||
// If the directory exists from a previous run, remove it to ensure a clean slate
|
||||
if build_src.exists() {
|
||||
std::fs::remove_dir_all(&build_src).unwrap();
|
||||
}
|
||||
if install_dir.exists() {
|
||||
std::fs::remove_dir_all(&install_dir).unwrap();
|
||||
}
|
||||
std::fs::create_dir_all(&install_dir).unwrap();
|
||||
std::fs::create_dir_all(&build_src).unwrap();
|
||||
|
||||
// Copy the source code to OUT_DIR
|
||||
// We use fs_extra options to copy contents recursively
|
||||
let mut options = fs_extra::dir::CopyOptions::new();
|
||||
options.overwrite = true;
|
||||
options.content_only = true; // Copy content of src into dest, not src folder itself
|
||||
|
||||
fs_extra::dir::copy(&libxml2_dir, &build_src, &options).expect("Failed to copy C source code to OUT_DIR");
|
||||
|
||||
// Build the libxml2 library using autotools
|
||||
let dst = {
|
||||
let mut config = autotools::Config::new(&build_src);
|
||||
config
|
||||
.out_dir(&install_dir)
|
||||
.reconf("-ivf")
|
||||
.enable_static()
|
||||
.disable_shared()
|
||||
// Disable optional features that complicate static builds
|
||||
.with("python", Some("no"))
|
||||
.with("icu", Some("no"))
|
||||
.with("http", Some("no"))
|
||||
.with("ftp", Some("no"))
|
||||
.with("catalog", Some("no"))
|
||||
.with("docbook", Some("no"))
|
||||
.with("legacy", Some("no"))
|
||||
.with("threads", Some("no"))
|
||||
.with("zlib", Some("no"))
|
||||
.with("lzma", Some("no"))
|
||||
// Ensure position-independent code (safe for static linking)
|
||||
.cflag("-fPIC");
|
||||
config.build()
|
||||
};
|
||||
|
||||
let lib_dir = dst.join("lib");
|
||||
let include_dir = dst.join("include/libxml2");
|
||||
|
||||
// Tell rustc where to find the static library
|
||||
println!("cargo:rustc-link-search=native={}", lib_dir.display());
|
||||
|
||||
// Static linking of libxml2
|
||||
println!("cargo:rustc-link-lib=static=xml2");
|
||||
|
||||
// Export include path for downstream use (bindgen / cc)
|
||||
println!("cargo:include={}", include_dir.display());
|
||||
|
||||
dst
|
||||
}
|
||||
|
||||
fn build_openconnect(deps_dir: &PathBuf, out_dir: &PathBuf) -> PathBuf {
|
||||
let openconnect_dir = deps_dir.join("openconnect");
|
||||
let install_dir = out_dir.join("openconnect_install");
|
||||
|
||||
// The temporary location where we will build
|
||||
let build_src = out_dir.join("openconnect_build");
|
||||
|
||||
// Track changes so cargo rebuilds if submodule changes
|
||||
println!("cargo:rerun-if-changed={}", openconnect_dir.display());
|
||||
|
||||
// Prepare the build directory
|
||||
// If the directory exists from a previous run, remove it to ensure a clean slate
|
||||
if build_src.exists() {
|
||||
std::fs::remove_dir_all(&build_src).unwrap();
|
||||
}
|
||||
if install_dir.exists() {
|
||||
std::fs::remove_dir_all(&install_dir).unwrap();
|
||||
}
|
||||
std::fs::create_dir_all(&install_dir).unwrap();
|
||||
std::fs::create_dir_all(&build_src).unwrap();
|
||||
|
||||
// Copy the source code to OUT_DIR
|
||||
// We use fs_extra options to copy contents recursively
|
||||
let mut options = fs_extra::dir::CopyOptions::new();
|
||||
options.overwrite = true;
|
||||
options.content_only = true; // Copy content of src into dest, not src folder itself
|
||||
|
||||
fs_extra::dir::copy(&openconnect_dir, &build_src, &options).expect("Failed to copy C source code to OUT_DIR");
|
||||
|
||||
// Build the OpenConnect library using autotools. Keep the upstream LGPL
|
||||
// library dynamically linked on macOS so it remains replaceable in the app
|
||||
// bundle. Other platforms retain the existing static-link behavior.
|
||||
let mut config = autotools::Config::new(&build_src);
|
||||
config
|
||||
.out_dir(&install_dir)
|
||||
.reconf("-ivf")
|
||||
.disable("nls", None) // disable translations to save space
|
||||
.disable("docs", None)
|
||||
.disable("dsa-tests", None)
|
||||
.without("libproxy", None)
|
||||
.without("stoken", None)
|
||||
.without("libpcsclite", None)
|
||||
.without("libpskc", None)
|
||||
.without("gssapi", None)
|
||||
.with("gnutls-tss2", Some("no"))
|
||||
.with("vpnc-script", Some("/etc/vpnc/vpnc-script")); // Specify vpnc-script, otherwise it will fail on macOS
|
||||
|
||||
if target_os() == "macos" {
|
||||
config
|
||||
.disable_static()
|
||||
.enable_shared()
|
||||
.with("gnutls", None)
|
||||
.without("openssl", None);
|
||||
config.ldflag("-liconv");
|
||||
config.env("LIBS", "-liconv");
|
||||
}
|
||||
|
||||
if target_os() != "macos" {
|
||||
config.enable_static().disable_shared();
|
||||
}
|
||||
|
||||
if matches!(target_os().as_str(), "freebsd" | "openbsd") {
|
||||
config.ldflag("-L/usr/local/lib");
|
||||
config.ldflag("-liconv");
|
||||
config.env("LIBS", "-liconv");
|
||||
}
|
||||
|
||||
let dst = config.build();
|
||||
|
||||
let lib_dir = dst.join("lib");
|
||||
let include_dir = dst.join("include");
|
||||
|
||||
if target_os() == "macos" {
|
||||
let configuration = std::fs::read_to_string(dst.join("build/config.h"))
|
||||
.expect("Failed to read the OpenConnect TLS backend configuration");
|
||||
assert!(
|
||||
configuration.lines().any(|line| line == "#define OPENCONNECT_GNUTLS 1")
|
||||
&& !configuration
|
||||
.lines()
|
||||
.any(|line| line == "#define OPENCONNECT_OPENSSL 1"),
|
||||
"macOS OpenConnect must use GnuTLS"
|
||||
);
|
||||
// Do not embed Cargo's temporary build directory in consumers of this library.
|
||||
let library = std::fs::canonicalize(lib_dir.join("libopenconnect.dylib")).unwrap();
|
||||
let library_name = library.file_name().unwrap().to_str().unwrap();
|
||||
let status = std::process::Command::new("install_name_tool")
|
||||
.arg("-id")
|
||||
.arg(format!("@rpath/{library_name}"))
|
||||
.arg(&library)
|
||||
.status()
|
||||
.expect("Failed to update the OpenConnect dylib install name");
|
||||
assert!(status.success(), "Failed to update the OpenConnect dylib install name");
|
||||
let status = std::process::Command::new("codesign")
|
||||
.args(["--force", "--sign", "-"])
|
||||
.arg(&library)
|
||||
.status()
|
||||
.expect("Failed to sign the OpenConnect dylib");
|
||||
assert!(status.success(), "Failed to sign the OpenConnect dylib");
|
||||
println!("cargo:lib_dir={}", lib_dir.display());
|
||||
}
|
||||
|
||||
println!("cargo:rustc-link-search=native={}", lib_dir.display());
|
||||
|
||||
if target_os() == "macos" {
|
||||
println!("cargo:rustc-link-lib=dylib=openconnect");
|
||||
}
|
||||
|
||||
if target_os() != "macos" {
|
||||
println!("cargo:rustc-link-lib=static=openconnect");
|
||||
}
|
||||
|
||||
// Export include path for downstream use (bindgen / cc)
|
||||
println!("cargo:include={}", include_dir.display());
|
||||
|
||||
dst
|
||||
}
|
||||
|
||||
pub fn build() {
|
||||
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
|
||||
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
|
||||
let deps_dir = PathBuf::from(manifest_dir).join("deps");
|
||||
|
||||
let oc_dst = build_openconnect(&deps_dir, &out_dir);
|
||||
println!(
|
||||
"cargo:rustc-env=OPENCONNECT_PROTOCOL_BUILD={}",
|
||||
oc_dst.join("build").display()
|
||||
);
|
||||
println!(
|
||||
"cargo:rustc-env=OPENCONNECT_PROTOCOL_SOURCE={}",
|
||||
out_dir.join("openconnect_build").display()
|
||||
);
|
||||
|
||||
// Only statically link libxml2 if `LIBXML2_STATIC` is set
|
||||
if env::var("LIBXML2_STATIC").is_ok() {
|
||||
let _libxml2_dst = build_libxml2(&deps_dir, &out_dir);
|
||||
} else {
|
||||
// Using pkg-config to find system libxml2
|
||||
pkg_config::probe_library("libxml-2.0").unwrap();
|
||||
}
|
||||
|
||||
// Using pkg-config to find system deps
|
||||
pkg_config::probe_library("zlib").unwrap();
|
||||
if target_os() == "macos" {
|
||||
pkg_config::probe_library("gnutls").unwrap();
|
||||
}
|
||||
|
||||
if target_os() != "macos" {
|
||||
pkg_config::probe_library("liblz4").unwrap();
|
||||
pkg_config::probe_library("gnutls").unwrap();
|
||||
|
||||
// Below are required by gnutls
|
||||
pkg_config::probe_library("p11-kit-1").unwrap();
|
||||
pkg_config::probe_library("hogweed").unwrap();
|
||||
pkg_config::probe_library("nettle").unwrap();
|
||||
pkg_config::probe_library("gmp").unwrap();
|
||||
}
|
||||
|
||||
// Compile the vpn.c file
|
||||
println!("cargo:rerun-if-changed=src/ffi/vpn.c");
|
||||
println!("cargo:rerun-if-changed=src/ffi/vpn.h");
|
||||
|
||||
if matches!(target_os().as_str(), "freebsd" | "openbsd") {
|
||||
println!("cargo:rustc-link-search=native=/usr/local/lib");
|
||||
println!("cargo:rustc-link-lib=iconv");
|
||||
}
|
||||
|
||||
// Prevent silent fallback to dynamic libraries
|
||||
if target_os() == "linux" {
|
||||
println!("cargo:rustc-link-arg=-Wl,-Bstatic");
|
||||
println!("cargo:rustc-link-arg=-Wl,-Bdynamic");
|
||||
}
|
||||
|
||||
cc::Build::new()
|
||||
.file("src/ffi/vpn.c")
|
||||
.include("src/ffi")
|
||||
.include(&oc_dst.join("include"))
|
||||
.compile("vpn");
|
||||
}
|
||||
|
||||
fn target_os() -> String {
|
||||
env::var("CARGO_CFG_TARGET_OS").expect("Cargo target OS")
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
use std::{env, path::PathBuf};
|
||||
|
||||
pub fn build() {
|
||||
println!("cargo:rerun-if-env-changed=GP_IOS_NATIVE_PREFIX");
|
||||
let prefix = PathBuf::from(env::var_os("GP_IOS_NATIVE_PREFIX").expect(
|
||||
"Set GP_IOS_NATIVE_PREFIX to target-built OpenConnect/GnuTLS libraries with an aarch64-apple-ios target.txt receipt",
|
||||
));
|
||||
let target = std::fs::read_to_string(prefix.join("target.txt")).expect("Missing iOS native target receipt");
|
||||
assert_eq!(
|
||||
target.trim(),
|
||||
env::var("TARGET").unwrap(),
|
||||
"Native library target mismatch"
|
||||
);
|
||||
for name in ["openconnect", "gnutls", "hogweed", "nettle", "gmp"] {
|
||||
let archive = prefix.join(format!("lib/lib{name}.a"));
|
||||
assert!(archive.is_file(), "Missing {}", archive.display());
|
||||
println!("cargo:rerun-if-changed={}", archive.display());
|
||||
}
|
||||
println!("cargo:rerun-if-changed=src/ffi/vpn.c");
|
||||
println!("cargo:rerun-if-changed=src/ffi/vpn.h");
|
||||
cc::Build::new()
|
||||
.file("src/ffi/vpn.c")
|
||||
.include("src/ffi")
|
||||
.include(prefix.join("include"))
|
||||
.flag("-fapplication-extension")
|
||||
.compile("vpn");
|
||||
println!("cargo:rustc-link-search=native={}", prefix.join("lib").display());
|
||||
for name in ["openconnect", "gnutls", "hogweed", "nettle", "gmp"] {
|
||||
println!("cargo:rustc-link-lib=static={name}");
|
||||
}
|
||||
for name in ["xml2", "z", "iconv", "resolv"] {
|
||||
println!("cargo:rustc-link-lib={name}");
|
||||
}
|
||||
println!("cargo:rustc-link-lib=framework=Security");
|
||||
println!("cargo:rustc-link-lib=framework=CoreFoundation");
|
||||
}
|
||||
Submodule crates/openconnect/deps/openconnect updated: 33b3d7a01d...43b36d7293.
@@ -44,6 +44,7 @@ pub(crate) struct ConnectOptions {
|
||||
pub interface: *const c_char,
|
||||
pub script_tun: u32,
|
||||
pub script_tun_launch: *const ScriptTunLaunchRaw,
|
||||
pub packet_fd: c_int,
|
||||
|
||||
pub certificate: *const c_char,
|
||||
pub sslkey: *const c_char,
|
||||
|
||||
@@ -52,9 +52,13 @@ static void observe_statistics(void *private_data, const struct oc_stats *stats)
|
||||
}
|
||||
|
||||
/* Validate the peer certificate */
|
||||
static int validate_peer_cert(__attribute__((unused)) void *_vpninfo,
|
||||
static int validate_peer_cert(void *private_data,
|
||||
const char *reason)
|
||||
{
|
||||
struct vpn_context *context = private_data;
|
||||
if (context->options->servercert)
|
||||
return openconnect_check_peer_cert_hash(context->vpninfo,
|
||||
context->options->servercert);
|
||||
INFO("Accepting the server certificate though %s", reason);
|
||||
return 0;
|
||||
}
|
||||
@@ -82,7 +86,10 @@ static void setup_tun_handler(void *private_data)
|
||||
struct openconnect_info *_vpninfo = context->vpninfo;
|
||||
const vpn_options *options = context->options;
|
||||
int ret;
|
||||
if (options->script_tun) {
|
||||
if (options->packet_fd >= 0) {
|
||||
int fd = dup(options->packet_fd);
|
||||
ret = fd < 0 ? -errno : openconnect_setup_tun_fd_with_config(_vpninfo, fd);
|
||||
} else if (options->script_tun) {
|
||||
const struct openconnect_script_tun_launch *launch = options->script_tun_launch;
|
||||
if (launch) {
|
||||
ret = openconnect_setup_tun_script_exec(_vpninfo, launch);
|
||||
@@ -156,6 +163,9 @@ int vpn_connect(const vpn_options *options, vpn_connected_callback callback)
|
||||
}
|
||||
|
||||
context.vpninfo = vpninfo;
|
||||
/* A supplied pin is authoritative even when the system would trust a CA. */
|
||||
if (options->servercert)
|
||||
openconnect_set_system_trust(vpninfo, 0);
|
||||
openconnect_set_tunnel_handler(vpninfo, observe_tunnel);
|
||||
openconnect_set_reconnected_handler(vpninfo, observe_reconnected);
|
||||
openconnect_set_stats_handler(vpninfo, observe_statistics);
|
||||
|
||||
@@ -48,6 +48,7 @@ typedef struct vpn_options {
|
||||
const char *interface;
|
||||
const int script_tun;
|
||||
const struct openconnect_script_tun_launch *script_tun_launch;
|
||||
const int packet_fd;
|
||||
|
||||
const char *certificate;
|
||||
const char *sslkey;
|
||||
|
||||
@@ -151,6 +151,7 @@ pub struct Vpn {
|
||||
interface: Option<CString>,
|
||||
script_tun: bool,
|
||||
script_tun_launch: Option<ScriptTunLaunch>,
|
||||
packet_endpoint: Option<OwnedFd>,
|
||||
|
||||
certificate: Option<CString>,
|
||||
sslkey: Option<CString>,
|
||||
@@ -314,6 +315,7 @@ impl Vpn {
|
||||
interface: Self::option_to_ptr(&self.interface),
|
||||
script_tun: self.script_tun as u32,
|
||||
script_tun_launch: std::ptr::null(),
|
||||
packet_fd: self.packet_endpoint.as_ref().map_or(-1, AsRawFd::as_raw_fd),
|
||||
|
||||
certificate: Self::option_to_ptr(&self.certificate),
|
||||
sslkey: Self::option_to_ptr(&self.sslkey),
|
||||
@@ -382,6 +384,7 @@ pub struct VpnBuilder {
|
||||
interface: Option<String>,
|
||||
script_tun: bool,
|
||||
script_tun_launch: Option<ScriptTunLaunch>,
|
||||
packet_endpoint: Option<OwnedFd>,
|
||||
|
||||
user_agent: Option<String>,
|
||||
os: Option<String>,
|
||||
@@ -393,6 +396,7 @@ pub struct VpnBuilder {
|
||||
certificate: Option<String>,
|
||||
sslkey: Option<String>,
|
||||
key_password: Option<String>,
|
||||
servercert: Option<String>,
|
||||
|
||||
hip_source: Arc<HipSource>,
|
||||
|
||||
@@ -417,6 +421,7 @@ impl VpnBuilder {
|
||||
interface: None,
|
||||
script_tun: false,
|
||||
script_tun_launch: None,
|
||||
packet_endpoint: None,
|
||||
|
||||
user_agent: None,
|
||||
os: None,
|
||||
@@ -428,6 +433,7 @@ impl VpnBuilder {
|
||||
certificate: None,
|
||||
sslkey: None,
|
||||
key_password: None,
|
||||
servercert: None,
|
||||
|
||||
hip_source: Arc::new(HipSource::Disabled),
|
||||
|
||||
@@ -441,6 +447,16 @@ impl VpnBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
/// Use an application-owned packet descriptor. OpenConnect borrows a duplicate
|
||||
/// during connect; this owner remains alive until the Vpn is dropped. Apple/BSD
|
||||
/// endpoints use the native four-byte, network-order address-family prefix.
|
||||
/// Requires callback configuration and a SHA-256 server public-key pin.
|
||||
pub fn packet_endpoint(mut self, endpoint: OwnedFd, server_pin: String) -> Self {
|
||||
self.packet_endpoint = Some(endpoint);
|
||||
self.servercert = Some(server_pin);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn script<T: Into<Option<String>>>(mut self, script: T) -> Self {
|
||||
self.script = script.into();
|
||||
self.script_is_path = false;
|
||||
@@ -618,6 +634,24 @@ impl VpnBuilder {
|
||||
}
|
||||
|
||||
pub fn build(self) -> Result<Vpn, VpnError> {
|
||||
if self.packet_endpoint.is_some() {
|
||||
let pin = self.servercert.as_deref().unwrap_or_default();
|
||||
if self.network_configuration_handler.is_none()
|
||||
|| self.interface.is_some()
|
||||
|| self.script.is_some()
|
||||
|| self.script_tun
|
||||
|| self.script_tun_launch.is_some()
|
||||
{
|
||||
return Err(VpnError::new(
|
||||
"Packet endpoint requires callback configuration and no device/script options".into(),
|
||||
));
|
||||
}
|
||||
if !pin.starts_with("pin-sha256:") || pin.len() != 55 || pin.contains('\0') {
|
||||
return Err(VpnError::new(
|
||||
"Packet endpoint requires a pin-sha256 server public-key fingerprint".into(),
|
||||
));
|
||||
}
|
||||
}
|
||||
if self.script_tun_launch.is_some() && !self.script_tun {
|
||||
return Err(VpnError::new("Child identity requires script-tun".into()));
|
||||
}
|
||||
@@ -643,11 +677,12 @@ impl VpnBuilder {
|
||||
interface: self.interface.as_deref().map(Self::to_cstring),
|
||||
script_tun: self.script_tun,
|
||||
script_tun_launch: self.script_tun_launch,
|
||||
packet_endpoint: self.packet_endpoint,
|
||||
|
||||
certificate: self.certificate.as_deref().map(Self::to_cstring),
|
||||
sslkey: self.sslkey.as_deref().map(Self::to_cstring),
|
||||
key_password: self.key_password.as_deref().map(Self::to_cstring),
|
||||
servercert: None,
|
||||
servercert: self.servercert.as_deref().map(Self::to_cstring),
|
||||
|
||||
hip_source: self.hip_source,
|
||||
|
||||
@@ -775,6 +810,7 @@ mod tests {
|
||||
interface: None,
|
||||
script_tun: false,
|
||||
script_tun_launch: None,
|
||||
packet_endpoint: None,
|
||||
certificate: None,
|
||||
sslkey: None,
|
||||
key_password: None,
|
||||
|
||||
@@ -75,6 +75,58 @@ int inspect_esp_packet(struct openconnect_info *vpninfo, struct pkt *pkt, uint8_
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Check the real request builder without a VPN server. Desktop payloads stay exact. */
|
||||
static void getconfig_profile_parameters(void)
|
||||
{
|
||||
const char *platforms[] = { "linux", "mac-intel", "win", "apple-ios" };
|
||||
const char *names[] = { "Linux", "Mac", "Windows", "iOS" };
|
||||
int i;
|
||||
for (i = 0; i < 4; i++) {
|
||||
struct openconnect_info *vpninfo = openconnect_vpninfo_new("test", NULL, NULL, NULL, NULL, NULL);
|
||||
struct oc_text_buf *body = buf_alloc();
|
||||
char expected[2048];
|
||||
assert(vpninfo && body);
|
||||
assert(!openconnect_set_reported_os(vpninfo, platforms[i]));
|
||||
assert(!openconnect_set_gp_app_version(vpninfo, "6.3.5-28"));
|
||||
assert(!openconnect_set_gp_os_version(vpninfo, "test-os"));
|
||||
assert(!openconnect_set_gp_host_id(vpninfo, "HOSTID"));
|
||||
assert(!openconnect_set_cookie(vpninfo, "user=test&portal=portal&authcookie=fixture&computer=Phone&preferred-ip=192.0.2.5"));
|
||||
assert(!gpst_build_getconfig_request(vpninfo, body));
|
||||
if (i == 3) {
|
||||
assert(strstr(body->data, "clientos=iOS"));
|
||||
assert(strstr(body->data, "vpn-type=App%20Level%20VPN"));
|
||||
assert(strstr(body->data, "client-ip=&client-ipv6=&serialno=&joined-domain="));
|
||||
assert(strstr(body->data, "user=test") && strstr(body->data, "authcookie=fixture"));
|
||||
assert(strstr(body->data, "preferred-ip=192.0.2.5"));
|
||||
assert(!strstr(body->data, "host-id=") && !strstr(body->data, "clientgpversion=") && !strstr(body->data, "computer="));
|
||||
buf_truncate(body);
|
||||
vpninfo->ip_info.addr = strdup("192.0.2.6");
|
||||
assert(!gpst_build_getconfig_request(vpninfo, body));
|
||||
assert(strstr(body->data, "preferred-ip=192.0.2.6"));
|
||||
assert(!strstr(body->data, "192.0.2.5"));
|
||||
/* The connected HTTPS socket supplies the actual local client address. */
|
||||
int fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
struct sockaddr_in local = { .sin_family = AF_INET, .sin_addr.s_addr = htonl(INADDR_LOOPBACK) };
|
||||
assert(fd >= 0 && !bind(fd, (struct sockaddr *)&local, sizeof(local)));
|
||||
vpninfo->ssl_fd = fd;
|
||||
buf_truncate(body);
|
||||
assert(!gpst_build_getconfig_request(vpninfo, body));
|
||||
assert(strstr(body->data, "client-ip=127.0.0.1&client-ipv6="));
|
||||
vpninfo->ssl_fd = -1;
|
||||
close(fd);
|
||||
} else {
|
||||
snprintf(expected, sizeof(expected),
|
||||
"client-type=1&protocol-version=p1&internal=no&app-version=6.3.5-28&ipv6-support=yes"
|
||||
"&clientos=%s&os-version=test-os&hmac-algo=sha1%%2c&enc-algo=aes-256-gcm%%2caes-128-gcm%%2caes-128-cbc%%2c"
|
||||
"&clientgpversion=6.3.5-28&host-id=HOSTID&user=test&portal=portal&authcookie=fixture&computer=Phone&preferred-ip=192.0.2.5",
|
||||
names[i]);
|
||||
assert(!strcmp(body->data, expected));
|
||||
}
|
||||
buf_free(body);
|
||||
openconnect_vpninfo_free(vpninfo);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
static const char key[] =
|
||||
@@ -92,6 +144,7 @@ int main(int argc, char **argv)
|
||||
int sockets[2], nlb, invalid, length;
|
||||
|
||||
assert(argc == 2);
|
||||
getconfig_profile_parameters();
|
||||
nlb = !strncmp(argv[1], "nlb-", 4);
|
||||
invalid = !strcmp(argv[1], "regular-invalid") || !strcmp(argv[1], "regular-missing");
|
||||
if (!strcmp(argv[1], "regular-address") || !strcmp(argv[1], "nlb-address"))
|
||||
|
||||
@@ -160,6 +160,59 @@ static void reconnect_refusal_propagates(void)
|
||||
openconnect_vpninfo_free(vpninfo);
|
||||
}
|
||||
|
||||
static void external_packets_and_configuration(void)
|
||||
{
|
||||
struct state state = { .parent = getpid(), .reason = "connect" };
|
||||
struct openconnect_info *vpninfo = new_vpn(&state);
|
||||
int fds[2], families[] = { AF_INET, AF_INET6 };
|
||||
int creations = device_creations;
|
||||
assert(!socketpair(AF_UNIX, SOCK_DGRAM, 0, fds));
|
||||
assert(!openconnect_setup_tun_fd_with_config(vpninfo, fds[0]));
|
||||
assert(device_creations == creations);
|
||||
assert(state.calls == 1);
|
||||
for (int i = 0; i < 2; i++) {
|
||||
unsigned char frame[1504] = {0}, received[1504];
|
||||
struct pkt *packet = alloc_pkt(vpninfo, 1500);
|
||||
int prefix = 0, length = i ? 40 : 20;
|
||||
#if defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__)
|
||||
uint32_t family = htonl(families[i]);
|
||||
memcpy(frame, &family, 4);
|
||||
prefix = 4;
|
||||
#endif
|
||||
frame[prefix] = i ? 0x60 : 0x45;
|
||||
assert(write(fds[1], frame, length + prefix) == length + prefix);
|
||||
packet->len = 1500;
|
||||
assert(!os_read_tun(vpninfo, packet));
|
||||
assert(packet->len == length);
|
||||
assert(!memcmp(packet->data, frame + prefix, length));
|
||||
assert(!os_write_tun(vpninfo, packet));
|
||||
assert(read(fds[1], received, sizeof(received)) == length + prefix);
|
||||
assert(!memcmp(received, frame, length + prefix));
|
||||
free_pkt(vpninfo, packet);
|
||||
}
|
||||
state.reason = "disconnect";
|
||||
openconnect_vpninfo_free(vpninfo);
|
||||
assert(state.calls == 2);
|
||||
assert(fcntl(fds[0], F_GETFD) == -1 && errno == EBADF);
|
||||
close(fds[1]);
|
||||
|
||||
state.calls = 0;
|
||||
state.reason = "connect";
|
||||
state.failure_reason = "connect";
|
||||
state.result = -EPERM;
|
||||
const char *reasons[] = { "connect", "disconnect" };
|
||||
state.expected_reasons = reasons;
|
||||
state.expected_count = 2;
|
||||
vpninfo = new_vpn(&state);
|
||||
assert(!socketpair(AF_UNIX, SOCK_DGRAM, 0, fds));
|
||||
assert(openconnect_setup_tun_fd_with_config(vpninfo, fds[0]) == -EPERM);
|
||||
assert(vpninfo->tun_fd == -1);
|
||||
assert(fcntl(fds[0], F_GETFD) == -1 && errno == EBADF);
|
||||
openconnect_vpninfo_free(vpninfo);
|
||||
assert(state.calls == 2);
|
||||
close(fds[1]);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
const char *reasons[] = { "pre-init", "connect", "attempt-reconnect", "reconnect", "disconnect" };
|
||||
@@ -199,5 +252,6 @@ int main(void)
|
||||
openconnect_vpninfo_free(vpninfo);
|
||||
connect_failure_rolls_back();
|
||||
reconnect_refusal_propagates();
|
||||
external_packets_and_configuration();
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user