From 2ebb10b3b237f915ffda550ac16da5d6c197df4c Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Thu, 26 Nov 2026 22:29:27 +0100 Subject: [PATCH] build(linux): leave libcurl and its TLS stack out of the bundled libraries (#439) * build(linux): leave libcurl and its TLS stack out of the bundled libraries (refs #320) * fix(editor): libcurl is loaded at run time, so the game starts without it on Linux The game was linked with libcurl, so with libcurl left out of the bundle it needed the system's libcurl.so.4 to start, although only the editor's downloads use it. Now only the headers are used at build time, and the editor opens the system's libcurl with dlopen() when it first needs it: libcurl.so.4, libcurl-gnutls.so.4, then libcurl.so. It looks up the eight functions it calls and checks each one. Without libcurl, or with one missing a function, the game runs as usual and only the editor's downloads are off, as in a build without libcurl; one log line says why. A libcurl older than the headers (before 7.85.0) gets the protocols as bit masks. Windows is unchanged: the editor uses WinHTTP there. --- CMakeLists.txt | 7 +- CMake_modules/bundle_runtime_libs.cmake | 16 ++- editor/editorHttp.cpp | 156 ++++++++++++++++++------ editor/editorOrthophoto.cpp | 7 +- tests/unit/test_editor_http.cpp | 15 +++ 5 files changed, 158 insertions(+), 43 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 93d374b94..faa6ef86d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -777,13 +777,16 @@ if (WIN32) endif() # http client of the editor, for the orthophoto, the terrain model and the topo map of geoportal.gov.pl: WinHTTP on Windows, -# libcurl elsewhere when available +# libcurl elsewhere when available. libcurl isn't linked: only its headers are used, and editor/editorHttp.cpp loads the +# system's libcurl at run time (dlopen), so the game starts on a system without it and only the editor's downloads are off +# there. the package doesn't bundle it either (CMake_modules/bundle_runtime_libs.cmake) if (WIN32) target_link_libraries(eu07_settings INTERFACE winhttp) else() find_package(CURL QUIET) if (CURL_FOUND) - target_link_libraries(eu07_settings INTERFACE CURL::libcurl) + target_include_directories(eu07_settings INTERFACE $) + target_link_libraries(eu07_settings INTERFACE ${CMAKE_DL_LIBS}) target_compile_definitions(eu07_settings INTERFACE EU07_WITH_CURL) else() message(STATUS "libcurl not found: the editor's orthophoto and terrain model will only show what's cached, its topo map nothing") diff --git a/CMake_modules/bundle_runtime_libs.cmake b/CMake_modules/bundle_runtime_libs.cmake index a0f71543e..5b95c50e8 100644 --- a/CMake_modules/bundle_runtime_libs.cmake +++ b/CMake_modules/bundle_runtime_libs.cmake @@ -4,6 +4,12 @@ # the libraries of the system itself stay out: the C library and the loader, the GPU driver stack (libGL and its # kin come with the driver), X11 / Wayland, the sound servers, libstdc++ (the GPU drivers bring their own need of a # newer one) and a few more every distribution has. what a library left out depends on isn't bundled either +# +# libcurl and everything it pulls in (TLS, Kerberos, LDAP, HTTP/2, compression, name handling) stay out too: the +# build machine's copy knows the CA bundle path of the build machine's distribution (Debian), so shipped next to +# the game it breaks the certificate checks on Fedora or openSUSE, and the security updates of the TLS stack come +# from the user's system, not from our package. the game isn't linked with libcurl: the editor loads the system's one +# at run time (editor/editorHttp.cpp), and without it only the editor's downloads are off get_filename_component(game_folder "${GAME}" DIRECTORY) set(manifest "${game_folder}/bundled_libs.txt") @@ -43,7 +49,15 @@ set(system_libraries "^lib(asound|pulse|pulse-simple|jack|pipewire-[0-9.]+)\\.so" "^lib(udev|systemd|dbus-1)\\.so" "^libz\\.so" - "^lib(fontconfig|freetype|harfbuzz|expat|uuid)\\.so") + "^lib(fontconfig|freetype|harfbuzz|expat|uuid)\\.so" + "^libcurl" + "^lib(ssl|crypto)\\.so" + "^lib(gssapi_krb5|krb5|k5crypto|krb5support|com_err|keyutils)\\.so" + "^lib(ldap|lber|sasl2)" + "^lib(nghttp2|nghttp3|ngtcp2|rtmp|ssh2?|psl|idn2|unistring)\\.so" + "^libbrotli(dec|enc|common)\\.so" + "^libzstd\\.so" + "^lib(gnutls|hogweed|nettle|gmp|p11-kit|tasn1|ffi)\\.so") file(GET_RUNTIME_DEPENDENCIES EXECUTABLES "${GAME}" diff --git a/editor/editorHttp.cpp b/editor/editorHttp.cpp index cbda581f0..2a8c90c00 100644 --- a/editor/editorHttp.cpp +++ b/editor/editorHttp.cpp @@ -13,12 +13,20 @@ http://mozilla.org/MPL/2.0/. #if defined(_WIN32) #include #elif defined(EU07_WITH_CURL) +// of libcurl only the types and the constants: its functions are looked up at run time (libcurl() below), so the game +// starts on a system without libcurl and only the editor's downloads are off there. no deprecation warnings for the +// options kept for a libcurl older than these headers +#ifndef CURL_DISABLE_DEPRECATION +#define CURL_DISABLE_DEPRECATION +#endif #include +#include +#include +#include "utilities/Logs.h" #endif #include #include -#include #include namespace editor_http @@ -308,9 +316,10 @@ bool client::get(std::string const &Url, std::vector &Out, std::st namespace { -// the file of the certificates https is checked with. the libcurl bundled with the game knows only the one of the system it -// was built on (/etc/ssl/certs/ca-certificates.crt of Debian and Ubuntu), which Fedora, RHEL and openSUSE don't have: there -// every download failed. the file named by the user first, then the ones the distributions keep; empty leaves libcurl's own +// the file of the certificates https is checked with. a libcurl built elsewhere (as the one once bundled with the game) knows +// only the one of the system it was built on (/etc/ssl/certs/ca-certificates.crt of Debian and Ubuntu), which Fedora, RHEL +// and openSUSE don't have: there every download failed. the file named by the user first, then the ones the distributions +// keep; empty leaves libcurl's own std::string const &certificates_file() { static std::string const file{[]() { @@ -335,30 +344,99 @@ std::string const &certificates_file() }()}; return file; } + +// the functions of libcurl the client uses, looked up in the system's libcurl. their types are those the headers declare +// them with, so a call through them is the call of the function itself +struct curl_library +{ + decltype(&::curl_global_init) global_init{nullptr}; + decltype(&::curl_easy_init) easy_init{nullptr}; + decltype(&::curl_easy_cleanup) easy_cleanup{nullptr}; + decltype(&::curl_easy_reset) easy_reset{nullptr}; + decltype(&::curl_easy_setopt) easy_setopt{nullptr}; + decltype(&::curl_easy_perform) easy_perform{nullptr}; + decltype(&::curl_easy_getinfo) easy_getinfo{nullptr}; + decltype(&::curl_easy_strerror) easy_strerror{nullptr}; + bool loaded{false}; // all of them found +}; + +// the system's libcurl: the name of its runtime package (libcurl4 of Debian and Ubuntu, libcurl of Fedora and Arch), that of +// the GnuTLS build of Debian and Ubuntu, then that of its development files. null when there's none +void *open_curl() +{ + for (char const *const name : {"libcurl.so.4", "libcurl-gnutls.so.4", "libcurl.so"}) + { + if (void *const library{::dlopen(name, RTLD_NOW | RTLD_LOCAL)}; library != nullptr) + return library; + } + return nullptr; +} + +// the function Name of the library into Function, false when the library has none +template +bool resolve(void *const Library, char const *const Name, Function &Into) +{ + // dlsym() gives a function's address as void *, which POSIX lets be converted back to the function's type + Into = reinterpret_cast(::dlsym(Library, Name)); + if (Into == nullptr) + WriteLog(std::format("Editor: the libcurl of the system has no {}, the downloads of the editor are off", Name)); + return Into != nullptr; +} + +// the system's libcurl, opened and initialised once, when the editor first needs it, and kept for the rest of the run +curl_library load_curl() +{ + curl_library curl; + void *const library{open_curl()}; + if (library == nullptr) + { + WriteLog("Editor: no libcurl (libcurl.so.4) on this system, the downloads of the editor (orthophoto, terrain model, topo map) are off"); + return curl; + } + curl.loaded = resolve(library, "curl_global_init", curl.global_init) && resolve(library, "curl_easy_init", curl.easy_init) && + resolve(library, "curl_easy_cleanup", curl.easy_cleanup) && resolve(library, "curl_easy_reset", curl.easy_reset) && + resolve(library, "curl_easy_setopt", curl.easy_setopt) && resolve(library, "curl_easy_perform", curl.easy_perform) && + resolve(library, "curl_easy_getinfo", curl.easy_getinfo) && resolve(library, "curl_easy_strerror", curl.easy_strerror); + if (false == curl.loaded) + { + ::dlclose(library); + return curl_library{}; + } + curl.global_init(CURL_GLOBAL_DEFAULT); + return curl; +} + +curl_library const &libcurl() +{ + static curl_library const curl{load_curl()}; + return curl; +} } // namespace client::client() : m_platform(std::make_unique()) { - static std::once_flag initialized; - std::call_once(initialized, []() { ::curl_global_init(CURL_GLOBAL_DEFAULT); }); - m_platform->curl = ::curl_easy_init(); + if (auto const &curl{libcurl()}; curl.loaded) + m_platform->curl = curl.easy_init(); } client::~client() { + // set only with libcurl loaded if (m_platform->curl != nullptr) - ::curl_easy_cleanup(m_platform->curl); + libcurl().easy_cleanup(m_platform->curl); } bool client::get(std::string const &Url, std::vector &Out, std::string &Error, stop_check const &Stop) { Out.clear(); + auto const &curl{libcurl()}; if (m_platform->curl == nullptr) { - Error = "curl_easy_init failed"; + Error = (curl.loaded ? "curl_easy_init failed" : "no libcurl on this system"); return false; } + CURL *const handle{m_platform->curl}; struct sink { @@ -378,47 +456,52 @@ bool client::get(std::string const &Url, std::vector &Out, std::st }; auto const progress = +[](void *User, curl_off_t, curl_off_t, curl_off_t, curl_off_t) -> int { return (*static_cast(User))() ? 1 : 0; }; - ::curl_easy_reset(m_platform->curl); - ::curl_easy_setopt(m_platform->curl, CURLOPT_URL, Url.c_str()); - ::curl_easy_setopt(m_platform->curl, CURLOPT_USERAGENT, "MaSzyna-editor"); + curl.easy_reset(handle); + curl.easy_setopt(handle, CURLOPT_URL, Url.c_str()); + curl.easy_setopt(handle, CURLOPT_USERAGENT, "MaSzyna-editor"); if (auto const &certificates{certificates_file()}; false == certificates.empty()) { - ::curl_easy_setopt(m_platform->curl, CURLOPT_CAINFO, certificates.c_str()); + curl.easy_setopt(handle, CURLOPT_CAINFO, certificates.c_str()); #if LIBCURL_VERSION_NUM >= 0x073400 // an https proxy is checked with the same file - ::curl_easy_setopt(m_platform->curl, CURLOPT_PROXY_CAINFO, certificates.c_str()); + curl.easy_setopt(handle, CURLOPT_PROXY_CAINFO, certificates.c_str()); #endif } - ::curl_easy_setopt(m_platform->curl, CURLOPT_FOLLOWLOCATION, 1L); - ::curl_easy_setopt(m_platform->curl, CURLOPT_MAXREDIRS, 10L); - // http kept for the services the user types; nothing else, also where a redirect leads + curl.easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L); + curl.easy_setopt(handle, CURLOPT_MAXREDIRS, 10L); + // http kept for the services the user types; nothing else, also where a redirect leads. the system's libcurl can be older + // than the headers: one without the options by name (before 7.85.0) refuses them, and takes the bit masks #if LIBCURL_VERSION_NUM >= 0x075500 - ::curl_easy_setopt(m_platform->curl, CURLOPT_PROTOCOLS_STR, "https,http"); - ::curl_easy_setopt(m_platform->curl, CURLOPT_REDIR_PROTOCOLS_STR, "https,http"); + bool const named{(curl.easy_setopt(handle, CURLOPT_PROTOCOLS_STR, "https,http") == CURLE_OK) && + (curl.easy_setopt(handle, CURLOPT_REDIR_PROTOCOLS_STR, "https,http") == CURLE_OK)}; #else - ::curl_easy_setopt(m_platform->curl, CURLOPT_PROTOCOLS, static_cast(CURLPROTO_HTTPS | CURLPROTO_HTTP)); - ::curl_easy_setopt(m_platform->curl, CURLOPT_REDIR_PROTOCOLS, static_cast(CURLPROTO_HTTPS | CURLPROTO_HTTP)); + bool const named{false}; #endif - ::curl_easy_setopt(m_platform->curl, CURLOPT_MAXFILESIZE_LARGE, static_cast(kLargest)); - ::curl_easy_setopt(m_platform->curl, CURLOPT_NOSIGNAL, 1L); - ::curl_easy_setopt(m_platform->curl, CURLOPT_CONNECTTIMEOUT, 10L); - ::curl_easy_setopt(m_platform->curl, CURLOPT_LOW_SPEED_LIMIT, 1L); - ::curl_easy_setopt(m_platform->curl, CURLOPT_LOW_SPEED_TIME, 20L); - ::curl_easy_setopt(m_platform->curl, CURLOPT_WRITEFUNCTION, write); - ::curl_easy_setopt(m_platform->curl, CURLOPT_WRITEDATA, &received); - ::curl_easy_setopt(m_platform->curl, CURLOPT_NOPROGRESS, 0L); - ::curl_easy_setopt(m_platform->curl, CURLOPT_XFERINFOFUNCTION, progress); - ::curl_easy_setopt(m_platform->curl, CURLOPT_XFERINFODATA, const_cast(&Stop)); + if (false == named) + { + curl.easy_setopt(handle, CURLOPT_PROTOCOLS, static_cast(CURLPROTO_HTTPS | CURLPROTO_HTTP)); + curl.easy_setopt(handle, CURLOPT_REDIR_PROTOCOLS, static_cast(CURLPROTO_HTTPS | CURLPROTO_HTTP)); + } + curl.easy_setopt(handle, CURLOPT_MAXFILESIZE_LARGE, static_cast(kLargest)); + curl.easy_setopt(handle, CURLOPT_NOSIGNAL, 1L); + curl.easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, 10L); + curl.easy_setopt(handle, CURLOPT_LOW_SPEED_LIMIT, 1L); + curl.easy_setopt(handle, CURLOPT_LOW_SPEED_TIME, 20L); + curl.easy_setopt(handle, CURLOPT_WRITEFUNCTION, write); + curl.easy_setopt(handle, CURLOPT_WRITEDATA, &received); + curl.easy_setopt(handle, CURLOPT_NOPROGRESS, 0L); + curl.easy_setopt(handle, CURLOPT_XFERINFOFUNCTION, progress); + curl.easy_setopt(handle, CURLOPT_XFERINFODATA, const_cast(&Stop)); - CURLcode const code = ::curl_easy_perform(m_platform->curl); + CURLcode const code = curl.easy_perform(handle); if (code != CURLE_OK) { - Error = (received.over || code == CURLE_FILESIZE_EXCEEDED ? kTooLarge : ::curl_easy_strerror(code)); + Error = (received.over || code == CURLE_FILESIZE_EXCEEDED ? kTooLarge : curl.easy_strerror(code)); Out.clear(); return false; } long status{0}; - ::curl_easy_getinfo(m_platform->curl, CURLINFO_RESPONSE_CODE, &status); + curl.easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &status); if (status != 200) { Error = "HTTP " + std::to_string(status); @@ -443,8 +526,11 @@ bool client::get(std::string const &, std::vector &Out, std::strin bool available() { -#if defined(_WIN32) || defined(EU07_WITH_CURL) +#if defined(_WIN32) return true; +#elif defined(EU07_WITH_CURL) + // libcurl opened here when nothing needed it yet + return libcurl().loaded; #else return false; #endif diff --git a/editor/editorOrthophoto.cpp b/editor/editorOrthophoto.cpp index d3254a2c1..7ea68acae 100644 --- a/editor/editorOrthophoto.cpp +++ b/editor/editorOrthophoto.cpp @@ -739,11 +739,8 @@ editor_orthophoto::service_info editor_orthophoto::describe(std::string const &S bool editor_orthophoto::can_download() { -#if defined(_WIN32) || defined(EU07_WITH_CURL) - return true; -#else - return false; -#endif + // on Linux, whether the system has libcurl as well + return editor_http::available(); } std::string editor_orthophoto::cache_directory() diff --git a/tests/unit/test_editor_http.cpp b/tests/unit/test_editor_http.cpp index ffdfe9959..a3abfc6b7 100644 --- a/tests/unit/test_editor_http.cpp +++ b/tests/unit/test_editor_http.cpp @@ -91,6 +91,21 @@ bool proxied() { } // namespace +TEST_CASE("editor http: a build with an http client has it at run time") { +#if defined(_WIN32) || defined(EU07_WITH_CURL) + // WinHTTP comes with Windows. libcurl isn't linked, it's loaded when first needed: a build made with its headers runs + // on a system that has it, so the tests below don't quietly skip a libcurl that failed to load + CHECK(editor_http::available()); +#else + CHECK_FALSE(editor_http::available()); + editor_http::client client; + std::vector data; + std::string error; + CHECK_FALSE(client.get("http://127.0.0.1/", data, error, []() { return false; })); + CHECK_FALSE(error.empty()); +#endif +} + TEST_CASE("editor http: a request a service doesn't answer is given up as soon as a stop is asked for") { if (false == editor_http::available() || proxied()) return; // a build with no http client, or a proxy in the way