1 #include "CurlHelper.h"
3 #include <zypp/PathInfo.h>
4 #include <zypp/Pathname.h>
5 #include <zypp/Target.h>
6 #include <zypp/base/Logger.h>
7 #include <zypp/base/String.h>
8 #include <zypp/media/ProxyInfo.h>
9 #include <zypp/media/MediaUserAuth.h>
10 #include <zypp/media/MediaException.h>
19 void globalInitCurlOnce()
21 // function-level static <=> std::call_once
22 static bool once __attribute__ ((__unused__)) = ( [] {
23 if ( curl_global_init( CURL_GLOBAL_ALL ) != 0 )
24 WAR << "curl global init failed" << std::endl;
28 int log_curl(CURL *curl, curl_infotype info,
29 char *ptr, size_t len, void *max_lvl)
35 case CURLINFO_TEXT: lvl = 1; pfx = "*"; break;
36 case CURLINFO_HEADER_IN: lvl = 2; pfx = "<"; break;
37 case CURLINFO_HEADER_OUT: lvl = 2; pfx = ">"; break;
40 if( lvl > 0 && max_lvl != NULL && lvl <= *((long *)max_lvl))
42 std::string msg(ptr, len);
43 std::list<std::string> lines;
44 std::list<std::string>::const_iterator line;
45 zypp::str::split(msg, std::back_inserter(lines), "\r\n");
46 for(line = lines.begin(); line != lines.end(); ++line)
48 DBG << pfx << " " << *line << std::endl;
54 size_t log_redirects_curl( char *ptr, size_t size, size_t nmemb, void *userdata)
56 // INT << "got header: " << string(ptr, ptr + size*nmemb) << endl;
58 char * lstart = ptr, * lend = ptr;
60 size_t max = size * nmemb;
64 for (lstart = lend; *lend != '\n' && pos < max; ++lend, ++pos);
66 // look for "Location"
77 std::string line { lstart, *(lend-1)=='\r' ? lend-1 : lend };
78 DBG << "redirecting to " << line << std::endl;
80 *reinterpret_cast<std::string *>( userdata ) = line;
85 // continue with the next line
99 * Fills the settings structure using options passed on the url
100 * for example ?timeout=x&proxy=foo
102 void fillSettingsFromUrl( const Url &url, media::TransferSettings &s )
105 const std::string & param { url.getQueryParam("timeout") };
106 if( ! param.empty() )
108 long num = str::strtonum<long>(param);
109 if( num >= 0 && num <= TRANSFER_TIMEOUT_MAX )
114 std::string param { url.getUsername() };
115 if ( ! param.empty() )
117 s.setUsername( std::move(param) );
118 param = url.getPassword();
119 if ( ! param.empty() )
120 s.setPassword( std::move(param) );
124 // if there is no username, set anonymous auth
125 if ( ( url.getScheme() == "ftp" || url.getScheme() == "tftp" ) && s.username().empty() )
126 s.setAnonymousAuth();
129 if ( url.getScheme() == "https" )
131 s.setVerifyPeerEnabled( false );
132 s.setVerifyHostEnabled( false );
134 const std::string & verify { url.getQueryParam("ssl_verify") };
135 if( verify.empty() || verify == "yes" )
137 s.setVerifyPeerEnabled( true );
138 s.setVerifyHostEnabled( true );
140 else if ( verify == "no" )
142 s.setVerifyPeerEnabled( false );
143 s.setVerifyHostEnabled( false );
147 std::vector<std::string> flags;
148 str::split( verify, std::back_inserter(flags), "," );
149 for ( const auto & flag : flags )
151 if ( flag == "host" )
152 s.setVerifyHostEnabled( true );
153 else if ( flag == "peer" )
154 s.setVerifyPeerEnabled( true );
156 ZYPP_THROW( media::MediaBadUrlException(url, "Unknown ssl_verify flag "+flag) );
161 Pathname ca_path { url.getQueryParam("ssl_capath") };
162 if( ! ca_path.empty() )
164 if( ! PathInfo(ca_path).isDir() || ! ca_path.absolute() )
165 ZYPP_THROW(media::MediaBadUrlException(url, "Invalid ssl_capath path"));
167 s.setCertificateAuthoritiesPath( std::move(ca_path) );
171 Pathname client_cert { url.getQueryParam("ssl_clientcert") };
172 if( ! client_cert.empty() )
174 if( ! PathInfo(client_cert).isFile() || ! client_cert.absolute() )
175 ZYPP_THROW(media::MediaBadUrlException(url, "Invalid ssl_clientcert file"));
177 s.setClientCertificatePath( std::move(client_cert) );
181 Pathname client_key { url.getQueryParam("ssl_clientkey") };
182 if( ! client_key.empty() )
184 if( ! PathInfo(client_key).isFile() || ! client_key.absolute() )
185 ZYPP_THROW(media::MediaBadUrlException(url, "Invalid ssl_clientkey file"));
187 s.setClientKeyPath( std::move(client_key) );
191 std::string param { url.getQueryParam( "proxy" ) };
192 if ( ! param.empty() )
194 if ( param == EXPLICITLY_NO_PROXY ) {
195 // Workaround TransferSettings shortcoming: With an
196 // empty proxy string, code will continue to look for
197 // valid proxy settings. So set proxy to some non-empty
198 // string, to indicate it has been explicitly disabled.
199 s.setProxy(EXPLICITLY_NO_PROXY);
200 s.setProxyEnabled(false);
203 const std::string & proxyport { url.getQueryParam( "proxyport" ) };
204 if ( ! proxyport.empty() ) {
208 s.setProxy( std::move(param) );
209 s.setProxyEnabled( true );
214 std::string param { url.getQueryParam( "proxyuser" ) };
215 if ( ! param.empty() )
217 s.setProxyUsername( std::move(param) );
218 s.setProxyPassword( url.getQueryParam( "proxypass" ) );
222 // HTTP authentication type
223 std::string param { url.getQueryParam("auth") };
224 if ( ! param.empty() && (url.getScheme() == "http" || url.getScheme() == "https") )
228 media::CurlAuthData::auth_type_str2long (param ); // check if we know it
230 catch ( const media::MediaException & ex_r )
232 DBG << "Rethrowing as MediaUnauthorizedException.";
233 ZYPP_THROW(media::MediaUnauthorizedException(url, ex_r.msg(), "", ""));
235 s.setAuthType( std::move(param) );
240 const std::string & param { url.getQueryParam("head_requests") };
241 if( ! param.empty() && param == "no" )
242 s.setHeadRequestsAllowed( false );
247 * Reads the system proxy configuration and fills the settings
248 * structure proxy information
250 void fillSettingsSystemProxy( const Url& url, media::TransferSettings &s )
252 media::ProxyInfo proxy_info;
253 if ( proxy_info.useProxyFor( url ) )
255 // We must extract any 'user:pass' from the proxy url
256 // otherwise they won't make it into curl (.curlrc wins).
258 Url u( proxy_info.proxy( url ) );
259 s.setProxy( u.asString( url::ViewOption::WITH_SCHEME + url::ViewOption::WITH_HOST + url::ViewOption::WITH_PORT ) );
260 // don't overwrite explicit auth settings
261 if ( s.proxyUsername().empty() )
263 s.setProxyUsername( u.getUsername( url::E_ENCODED ) );
264 s.setProxyPassword( u.getPassword( url::E_ENCODED ) );
266 s.setProxyEnabled( true );
268 catch (...) {} // no proxy if URL is malformed
273 int env::getZYPP_MEDIA_CURL_IPRESOLVE()
276 if ( const char * envp = getenv( "ZYPP_MEDIA_CURL_IPRESOLVE" ) )
278 WAR << "env set: $ZYPP_MEDIA_CURL_IPRESOLVE='" << envp << "'" << std::endl;
279 if ( strcmp( envp, "4" ) == 0 ) ret = 4;
280 else if ( strcmp( envp, "6" ) == 0 ) ret = 6;
286 const char * anonymousIdHeader()
288 // we need to add the release and identifier to the
290 // The target could be not initialized, and then this information
292 static const std::string _value(
293 str::trim( str::form(
294 "X-ZYpp-AnonymousId: %s",
295 Target::anonymousUniqueId( Pathname()/*guess root*/ ).c_str() ) )
297 return _value.c_str();
300 const char * distributionFlavorHeader()
302 // we need to add the release and identifier to the
304 // The target could be not initialized, and then this information
306 static const std::string _value(
307 str::trim( str::form(
308 "X-ZYpp-DistributionFlavor: %s",
309 Target::distributionFlavor( Pathname()/*guess root*/ ).c_str() ) )
311 return _value.c_str();
314 const char * agentString()
316 // we need to add the release and identifier to the
318 // The target could be not initialized, and then this information
320 static const std::string _value(
322 "ZYpp " LIBZYPP_VERSION_STRING " (curl %s) %s"
323 , curl_version_info(CURLVERSION_NOW)->version
324 , Target::targetDistribution( Pathname()/*guess root*/ ).c_str()
327 return _value.c_str();
330 void curlEscape( std::string & str_r,
331 const char char_r, const std::string & escaped_r ) {
332 for ( std::string::size_type pos = str_r.find( char_r );
333 pos != std::string::npos; pos = str_r.find( char_r, pos ) ) {
334 str_r.replace( pos, 1, escaped_r );
338 std::string curlEscapedPath( std::string path_r ) {
339 curlEscape( path_r, ' ', "%20" );
343 std::string curlUnEscape( std::string text_r ) {
344 char * tmp = curl_unescape( text_r.c_str(), 0 );
345 std::string ret( tmp );
350 Url clearQueryString(const Url &url)
353 curlUrl.setUsername( "" );
354 curlUrl.setPassword( "" );
355 curlUrl.setPathParams( "" );
356 curlUrl.setFragment( "" );
357 curlUrl.delQueryParam("cookies");
358 curlUrl.delQueryParam("proxy");
359 curlUrl.delQueryParam("proxyport");
360 curlUrl.delQueryParam("proxyuser");
361 curlUrl.delQueryParam("proxypass");
362 curlUrl.delQueryParam("ssl_capath");
363 curlUrl.delQueryParam("ssl_verify");
364 curlUrl.delQueryParam("ssl_clientcert");
365 curlUrl.delQueryParam("timeout");
366 curlUrl.delQueryParam("auth");
367 curlUrl.delQueryParam("username");
368 curlUrl.delQueryParam("password");
369 curlUrl.delQueryParam("mediahandler");
370 curlUrl.delQueryParam("credentials");
371 curlUrl.delQueryParam("head_requests");
375 // bsc#933839: propagate proxy settings passed in the repo URL
376 zypp::Url propagateQueryParams( zypp::Url url_r, const zypp::Url & template_r )
378 for ( std::string param : { "proxy", "proxyport", "proxyuser", "proxypass"} )
380 const std::string & value( template_r.getQueryParam( param ) );
381 if ( ! value.empty() )
382 url_r.setQueryParam( param, value );