libzypp 17.37.13
proxyinfolibproxy.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12
14#include <iostream>
15#include <optional>
16#include <cstdlib>
17
20#include <zypp-core/fs/WatchFile>
21#include <zypp-core/Pathname.h>
22#include <zypp/base/Env.h>
23
24#include <zypp-curl/proxyinfo/ProxyInfoLibproxy>
25
26#include <dlfcn.h> // for dlload, dlsym, dlclose
27#include <glib.h> // g_clear_pointer and g_strfreev
28
29using std::endl;
30using namespace zypp::base;
31
32namespace zypp {
33 namespace env {
34 inline bool inYAST()
35 {
36 static const bool _inYAST { ::getenv("YAST_IS_RUNNING") };
37 return _inYAST;
38 }
39
40 inline bool PX_DEBUG()
41 {
42 static const bool _pxdebug { env::getenvBool("PX_DEBUG") };
43 return _pxdebug;
44 }
45 }
46
47 namespace media {
48
49 namespace {
50
51 using CreateFactoryCb = CreateFactorySig<pxProxyFactoryType>;
52 using DelFactoryCb = DelFactorySig<pxProxyFactoryType>;
53 using GetProxiesCb = GetProxiesSig<pxProxyFactoryType>;
54
55 struct LibProxyAPI
56 {
57 zypp::AutoDispose<void *> libProxyLibHandle;
58 CreateFactoryCb createProxyFactory = nullptr;
59 DelFactoryCb deleteProxyFactory = nullptr;
60 GetProxiesCb getProxies = nullptr;
61 FreeProxiesCb freeProxies = nullptr;
62
68 static void fallbackFreeProxies( char **proxies ) {
69 g_clear_pointer (&proxies, g_strfreev);
70 }
71
72 static std::unique_ptr<LibProxyAPI> create() {
73 MIL << "Detecting libproxy availability" << std::endl;
74 zypp::AutoDispose<void *> handle( dlopen("libproxy.so.1", RTLD_LAZY ), []( void *ptr ){ if ( ptr ) ::dlclose(ptr); });
75 if ( !handle ) {
76 MIL << "No libproxy support detected (could not load library): " << dlerror() << std::endl;
77 return nullptr;
78 }
79
80 std::unique_ptr<LibProxyAPI> apiInstance = std::make_unique<LibProxyAPI>();
81 apiInstance->libProxyLibHandle = std::move(handle);
82 apiInstance->createProxyFactory = (CreateFactoryCb)::dlsym ( apiInstance->libProxyLibHandle, "px_proxy_factory_new" );
83 if ( !apiInstance->createProxyFactory ){
84 ERR << "Incompatible libproxy detected (could not resolve px_proxy_factory_new): " << dlerror() << std::endl;
85 return nullptr;
86 }
87 apiInstance->deleteProxyFactory = (DelFactoryCb)::dlsym ( apiInstance->libProxyLibHandle, "px_proxy_factory_free" );
88 if ( !apiInstance->deleteProxyFactory ){
89 ERR << "Incompatible libproxy detected (could not resolve px_proxy_factory_free): " << dlerror() << std::endl;
90 return nullptr;
91 }
92 apiInstance->getProxies = (GetProxiesCb)::dlsym ( apiInstance->libProxyLibHandle, "px_proxy_factory_get_proxies" );
93 if ( !apiInstance->getProxies ){
94 ERR << "Incompatible libproxy detected (could not resolve px_proxy_factory_get_proxies): " << dlerror() << std::endl;
95 return nullptr;
96 }
97 apiInstance->freeProxies = (FreeProxiesCb)::dlsym ( apiInstance->libProxyLibHandle, "px_proxy_factory_free_proxies" );
98 if ( !apiInstance->freeProxies ){
99 MIL << "Older version of libproxy detected, using fallback function to free the proxy list (could not resolve px_proxy_factory_free_proxies): " << dlerror() << std::endl;
100 apiInstance->freeProxies = &fallbackFreeProxies;
101 }
102
103 MIL << "Libproxy is available" << std::endl;
104 return apiInstance;
105 }
106 };
107
108 LibProxyAPI *proxyApi() {
109 static std::unique_ptr<LibProxyAPI> api = LibProxyAPI::create();
110 return api.get();
111 }
112
113 LibProxyAPI &assertProxyApi() {
114 auto api = proxyApi();
115 if ( !api )
116 ZYPP_THROW( zypp::Exception("LibProxyAPI is not available.") );
117 return *api;
118 }
119 }
120
122 {
123 TmpUnsetEnv(const char *var_r) : _set(false), _var(var_r) {
124 const char * val = getenv( _var.c_str() );
125 if ( val )
126 {
127 _set = true;
128 _val = val;
129 ::unsetenv( _var.c_str() );
130 }
131 }
132
133 TmpUnsetEnv(const TmpUnsetEnv &) = delete;
137
139 {
140 if ( _set )
141 {
142 setenv( _var.c_str(), _val.c_str(), 1 );
143 }
144 }
145
146 bool _set;
147 std::string _var;
148 std::string _val;
149 };
150
152 {
153 static pxProxyFactoryType * proxyFactory = 0;
154
155 // Force libproxy into using "/etc/sysconfig/proxy"
156 // if it exists.
157 static WatchFile sysconfigProxy( "/etc/sysconfig/proxy", WatchFile::NO_INIT );
158 if ( sysconfigProxy.hasChanged() )
159 {
160 MIL << "Build Libproxy Factory from /etc/sysconfig/proxy" << endl;
161 if ( proxyFactory )
162 assertProxyApi().deleteProxyFactory( proxyFactory );
163
164 TmpUnsetEnv envguard[] __attribute__ ((__unused__)) = { "KDE_FULL_SESSION", "GNOME_DESKTOP_SESSION_ID", "DESKTOP_SESSION" };
165 proxyFactory = assertProxyApi().createProxyFactory();
166 }
167 else if ( ! proxyFactory )
168 {
169 MIL << "Build Libproxy Factory" << endl;
170 proxyFactory = assertProxyApi().createProxyFactory();
171 }
172
173 return proxyFactory;
174 }
175
182
185
187 {
188 return ( proxyApi () != nullptr );
189 }
190
191 std::string ProxyInfoLibproxy::proxy(const Url & url_r) const
192 {
193 if (!_enabled)
194 return "";
195
196 const url::ViewOption vopt =
197 url::ViewOption::WITH_SCHEME
198 + url::ViewOption::WITH_HOST
199 + url::ViewOption::WITH_PORT
200 + url::ViewOption::WITH_PATH_NAME;
201
202 auto &api = assertProxyApi ();
203
205 api.getProxies(_factory, url_r.asString(vopt).c_str())
206 , api.freeProxies
207 );
208 if ( !proxies.value() )
209 return "";
210
211 // bsc#1244710: libproxy appears to return /etc/sysconfig/proxy
212 // values after $*_proxy environment variables.
213 // In YAST, e.g after changing the proxy settings, we'd like
214 // /etc/sysconfig/proxy changes to take effect immediately.
215 // So we pick the last one matching our schema.
216 const std::string myschema { url_r.getScheme()+":" };
217 std::optional<std::string> result;
218 for ( int i = 0; proxies[i]; ++i ) {
219 if ( str::hasPrefix( proxies[i], myschema ) ) {
220 result = str::asString( proxies[i] );
221 if ( not env::inYAST() )
222 break;
223 }
224 }
225
226 if ( env::PX_DEBUG() ) {
227 L_DBG("PX_DEBUG") << "Url " << url_r << endl;
228 for ( int i = 0; proxies[i]; ++i ) {
229 L_DBG("PX_DEBUG") << "got " << proxies[i] << endl;
230 }
231 L_DBG("PX_DEBUG") << "--> " << result.value_or( "" ) << endl;
232 }
233
234 return result.value_or( "" );
235 }
236
239
242
243 } // namespace media
244} // namespace zypp
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition AutoDispose.h:95
reference value() const
Reference to the Tp object.
Url manipulation class.
Definition Url.h:93
std::string getScheme() const
Returns the scheme name of the URL.
Definition Url.cc:551
std::string asString() const
Returns a default string representation of the Url object.
Definition Url.cc:515
Remember a files attributes to detect content changes.
Definition watchfile.h:50
bool hasChanged()
Definition watchfile.h:80
DefaultIntegral< bool, false > _enabled
ProxyInfo::NoProxyIterator noProxyBegin() const override
std::string proxy(const Url &url_r) const override
ProxyInfo::NoProxyIterator noProxyEnd() const override
ProxyInfo::NoProxyList _no_proxy
std::list< std::string >::const_iterator NoProxyIterator
Definition proxyinfo.h:35
Namespace intended to collect all environment variables we use.
Definition Env.h:25
TriBool getenvBool(const C_Str &var_r)
If the environment variable var_r is set to a legal true or false string return bool,...
Definition Env.h:32
char **(*)(ProxyFactoryType *self, const char *url) GetProxiesSig
void(*)(ProxyFactoryType *self) DelFactorySig
void(*)(char **proxies) FreeProxiesCb
ProxyFactoryType *(*)(void) CreateFactorySig
static pxProxyFactoryType * getProxyFactory()
const std::string & asString(const std::string &t)
Global asString() that works with std::string too.
Definition String.h:140
bool hasPrefix(const C_Str &str_r, const C_Str &prefix_r)
Return whether str_r has prefix prefix_r.
Definition String.h:1097
Easy-to use interface to the ZYPP dependency resolver.
struct _pxProxyFactory pxProxyFactoryType
TmpUnsetEnv(TmpUnsetEnv &&)=delete
TmpUnsetEnv(const char *var_r)
TmpUnsetEnv & operator=(const TmpUnsetEnv &)=delete
TmpUnsetEnv & operator=(TmpUnsetEnv &&)=delete
TmpUnsetEnv(const TmpUnsetEnv &)=delete
Url::asString() view options.
Definition UrlBase.h:41
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:459
#define MIL
Definition Logger.h:100
#define ERR
Definition Logger.h:102
#define L_DBG(GROUP)
Definition Logger.h:108