34 #include <sys/types.h>
36 #include <sys/mount.h>
41 #define DETECT_DIR_INDEX 0
42 #define CONNECT_TIMEOUT 60
43 #define TRANSFER_TIMEOUT_MAX 60 * 60
45 #define EXPLICITLY_NO_PROXY "_none_"
47 #undef CURLVERSION_AT_LEAST
48 #define CURLVERSION_AT_LEAST(M,N,O) LIBCURL_VERSION_NUM >= ((((M)<<8)+(N))<<8)+(O)
51 using namespace zypp::base;
58 extern "C" void _do_free_once()
60 curl_global_cleanup();
63 extern "C" void globalFreeOnce()
68 extern "C" void _do_init_once()
70 CURLcode ret = curl_global_init( CURL_GLOBAL_ALL );
73 WAR <<
"curl global init failed" << endl;
86 inline void globalInitOnce()
91 int log_curl(CURL *
curl, curl_infotype info,
92 char *ptr,
size_t len,
void *max_lvl)
94 if ( max_lvl ==
nullptr )
97 long maxlvl = *((
long *)max_lvl);
102 case CURLINFO_TEXT:
if ( maxlvl < 1 )
return 0; pfx =
'*';
break;
103 case CURLINFO_HEADER_IN:
if ( maxlvl < 2 )
return 0; pfx =
'<';
break;
104 case CURLINFO_HEADER_OUT:
if ( maxlvl < 2 )
return 0; pfx =
'>';
break;
109 std::vector<std::string> lines;
110 zypp::str::split( std::string(ptr,len), std::back_inserter(lines),
"\r\n" );
111 for(
const auto & line : lines )
115 if ( pos == std::string::npos )
117 DBG << pfx <<
" " << line.substr( 0, pos ) <<
" <credentials removed>" << std::endl;
120 DBG << pfx <<
" " << line << std::endl;
127 void *ptr,
size_t size,
size_t nmemb,
void *stream)
131 char * lstart = (
char *)ptr, * lend = (
char *)ptr;
133 size_t max = size * nmemb;
134 while (pos + 1 < max)
137 for (lstart = lend; *lend !=
'\n' && pos < max; ++lend, ++pos);
140 string line(lstart, lend);
141 if (line.find(
"Location") != string::npos)
143 DBG <<
"redirecting to " << line << endl;
167 ProgressData( CURL *_curl, time_t _timeout = 0,
const Url & _url = Url(),
168 ByteCount expectedFileSize_r = 0,
169 callback::SendReport<DownloadProgressReport> *_report =
nullptr )
184 callback::SendReport<DownloadProgressReport> *
report;
201 void updateStats(
double dltotal = 0.0,
double dlnow = 0.0 )
209 if ( dlnow && dlnow !=
_dnlNow )
250 int reportProgress()
const
280 inline void escape(
string & str_r,
281 const char char_r,
const string & escaped_r ) {
283 pos != string::npos; pos = str_r.find( char_r, pos ) ) {
284 str_r.replace( pos, 1, escaped_r );
288 inline string escapedPath(
string path_r ) {
289 escape( path_r,
' ',
"%20" );
293 inline string unEscape(
string text_r ) {
294 char * tmp = curl_unescape( text_r.c_str(), 0 );
311 long num = str::strtonum<long>(param);
335 if( verify.empty() ||
341 else if( verify ==
"no")
348 std::vector<std::string> flags;
349 std::vector<std::string>::const_iterator flag;
350 str::split( verify, std::back_inserter(flags),
",");
351 for(flag = flags.begin(); flag != flags.end(); ++flag)
355 else if( *flag ==
"peer")
364 if( ! ca_path.empty())
366 if( !PathInfo(ca_path).isDir() || ! ca_path.absolute())
373 if( ! client_cert.empty())
375 if( !PathInfo(client_cert).isFile() || !client_cert.absolute())
381 if( ! client_key.empty())
383 if( !PathInfo(client_key).isFile() || !client_key.absolute())
390 if ( ! param.empty() )
402 if ( ! proxyport.empty() ) {
403 param +=
":" + proxyport;
411 if ( ! param.empty() )
423 CurlAuthData::auth_type_str2long(param);
427 DBG <<
"Rethrowing as MediaUnauthorizedException.";
435 if( !param.empty() && param ==
"no" )
452 s.
setProxy( u.asString( url::ViewOption::WITH_SCHEME + url::ViewOption::WITH_HOST + url::ViewOption::WITH_PORT ) );
465 Pathname MediaCurl::_cookieFile =
"/var/lib/YaST2/cookies";
477 static const std::string
_value(
479 "X-ZYpp-AnonymousId: %s",
480 Target::anonymousUniqueId( Pathname() ).c_str() ) )
482 return _value.c_str();
495 static const std::string
_value(
497 "X-ZYpp-DistributionFlavor: %s",
498 Target::distributionFlavor( Pathname() ).c_str() ) )
500 return _value.c_str();
513 static const std::string
_value(
515 "ZYpp %s (curl %s) %s"
517 , curl_version_info(CURLVERSION_NOW)->version
518 , Target::targetDistribution( Pathname() ).c_str()
521 return _value.c_str();
527 #define SET_OPTION(opt,val) do { \
528 ret = curl_easy_setopt ( _curl, opt, val ); \
530 ZYPP_THROW(MediaCurlSetOptException(_url, _curlError)); \
534 #define SET_OPTION_OFFT(opt,val) SET_OPTION(opt,(curl_off_t)val)
535 #define SET_OPTION_LONG(opt,val) SET_OPTION(opt,(long)val)
536 #define SET_OPTION_VOID(opt,val) SET_OPTION(opt,(void*)val)
538 MediaCurl::MediaCurl(
const Url & url_r,
539 const Pathname & attach_point_hint_r )
549 MIL <<
"MediaCurl::MediaCurl(" << url_r <<
", " << attach_point_hint_r <<
")" << endl;
557 char *atemp = ::strdup( apath.asString().c_str());
559 if( !ainfo.isDir() || !ainfo.userMayRWX() ||
560 atemp == NULL || (atest=::mkdtemp(atemp)) == NULL)
562 WAR <<
"attach point " << ainfo.path()
563 <<
" is not useable for " << url_r.
getScheme() << endl;
566 else if( atest != NULL)
614 curl_version_info_data *curl_info = NULL;
615 curl_info = curl_version_info(CURLVERSION_NOW);
617 if (curl_info->protocols)
619 const char *
const *proto;
622 for(proto=curl_info->protocols; !found && *proto; ++proto)
624 if( scheme == std::string((
const char *)*proto))
629 std::string msg(
"Unsupported protocol '");
640 char *ptr = getenv(
"ZYPP_MEDIA_CURL_DEBUG");
641 _curlDebug = (ptr && *ptr) ? str::strtonum<long>( ptr) : 0L;
644 curl_easy_setopt( _curl, CURLOPT_VERBOSE, 1L);
645 curl_easy_setopt( _curl, CURLOPT_DEBUGFUNCTION, log_curl);
646 curl_easy_setopt( _curl, CURLOPT_DEBUGDATA, &
_curlDebug);
650 curl_easy_setopt(_curl, CURLOPT_HEADERFUNCTION, log_redirects_curl);
651 CURLcode ret = curl_easy_setopt( _curl, CURLOPT_ERRORBUFFER,
_curlError );
713 #if CURLVERSION_AT_LEAST(7,19,4)
715 SET_OPTION( CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS );
733 #ifdef CURLSSLOPT_ALLOW_BEAST
735 ret = curl_easy_setopt( _curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_ALLOW_BEAST );
744 SET_OPTION(CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
760 if (use_auth.empty())
761 use_auth =
"digest,basic";
763 if( auth != CURLAUTH_NONE)
765 DBG <<
"Enabling HTTP authentication methods: " << use_auth
766 <<
" (CURLOPT_HTTPAUTH=" << auth <<
")" << std::endl;
775 SET_OPTION(CURLOPT_PROXYAUTH, CURLAUTH_BASIC|CURLAUTH_DIGEST|CURLAUTH_NTLM );
785 if ( proxyuserpwd.empty() )
790 DBG <<
"Proxy: ~/.curlrc does not contain the proxy-user option" << endl;
794 DBG <<
"Proxy: using proxy-user from ~/.curlrc" << endl;
802 if ( ! proxyuserpwd.empty() )
804 SET_OPTION(CURLOPT_PROXYUSERPWD, unEscape( proxyuserpwd ).c_str());
807 #if CURLVERSION_AT_LEAST(7,19,4)
812 DBG <<
"Proxy: explicitly NOPROXY" << endl;
818 DBG <<
"Proxy: not explicitly set" << endl;
819 DBG <<
"Proxy: libcurl may look into the environment" << endl;
830 #if CURLVERSION_AT_LEAST(7,15,5)
843 MIL <<
"No cookies requested" << endl;
848 #if CURLVERSION_AT_LEAST(7,18,0)
853 for ( TransferSettings::Headers::const_iterator it = vol_settings.
headersBegin();
883 if( mountpoint.empty())
890 _curl = curl_easy_init();
927 curl_easy_cleanup( _curl );
1038 bool timeout_reached)
const
1043 if (filename.empty())
1053 case CURLE_UNSUPPORTED_PROTOCOL:
1054 case CURLE_URL_MALFORMAT:
1055 case CURLE_URL_MALFORMAT_USER:
1058 case CURLE_LOGIN_DENIED:
1062 case CURLE_HTTP_RETURNED_ERROR:
1064 long httpReturnCode = 0;
1065 CURLcode infoRet = curl_easy_getinfo( _curl,
1066 CURLINFO_RESPONSE_CODE,
1068 if ( infoRet == CURLE_OK )
1070 string msg =
"HTTP response: " +
str::numstring( httpReturnCode );
1071 switch ( httpReturnCode )
1077 DBG << msg <<
" Login failed (URL: " << url.
asString() <<
")" << std::endl;
1078 DBG <<
"MediaUnauthorizedException auth hint: '" << auth_hint <<
"'" << std::endl;
1093 if (url.
asString().find(
"novell.com") != string::npos)
1094 msg403 =
_(
"Visit the Novell Customer Center to check whether your registration is valid and has not expired.");
1102 DBG << msg <<
" (URL: " << url.
asString() <<
")" << std::endl;
1107 string msg =
"Unable to retrieve HTTP response:";
1108 DBG << msg <<
" (URL: " << url.
asString() <<
")" << std::endl;
1113 case CURLE_FTP_COULDNT_RETR_FILE:
1114 #if CURLVERSION_AT_LEAST(7,16,0)
1115 case CURLE_REMOTE_FILE_NOT_FOUND:
1117 case CURLE_FTP_ACCESS_DENIED:
1118 case CURLE_TFTP_NOTFOUND:
1119 err =
"File not found";
1122 case CURLE_BAD_PASSWORD_ENTERED:
1123 case CURLE_FTP_USER_PASSWORD_INCORRECT:
1124 err =
"Login failed";
1126 case CURLE_COULDNT_RESOLVE_PROXY:
1127 case CURLE_COULDNT_RESOLVE_HOST:
1128 case CURLE_COULDNT_CONNECT:
1129 case CURLE_FTP_CANT_GET_HOST:
1130 err =
"Connection failed";
1132 case CURLE_WRITE_ERROR:
1133 err =
"Write error";
1135 case CURLE_PARTIAL_FILE:
1136 case CURLE_OPERATION_TIMEDOUT:
1137 timeout_reached =
true;
1139 case CURLE_ABORTED_BY_CALLBACK:
1140 if( timeout_reached )
1142 err =
"Timeout reached";
1150 case CURLE_SSL_PEER_CERTIFICATE:
1174 DBG << filename.asString() << endl;
1199 string urlBuffer( curlUrl.
asString());
1200 CURLcode ret = curl_easy_setopt( _curl, CURLOPT_URL,
1201 urlBuffer.c_str() );
1213 ret = curl_easy_setopt( _curl, CURLOPT_NOBODY, 1L );
1215 ret = curl_easy_setopt( _curl, CURLOPT_RANGE,
"0-1" );
1218 curl_easy_setopt( _curl, CURLOPT_NOBODY, 0L);
1219 curl_easy_setopt( _curl, CURLOPT_RANGE, NULL );
1225 curl_easy_setopt( _curl, CURLOPT_HTTPGET, 1L );
1229 AutoFILE file { ::fopen(
"/dev/null",
"w" ) };
1231 ERR <<
"fopen failed for /dev/null" << endl;
1232 curl_easy_setopt( _curl, CURLOPT_NOBODY, 0L);
1233 curl_easy_setopt( _curl, CURLOPT_RANGE, NULL );
1239 curl_easy_setopt( _curl, CURLOPT_HTTPGET, 1L );
1246 ret = curl_easy_setopt( _curl, CURLOPT_WRITEDATA, (*file) );
1249 curl_easy_setopt( _curl, CURLOPT_RANGE, NULL );
1250 curl_easy_setopt( _curl, CURLOPT_NOBODY, 0L);
1256 curl_easy_setopt( _curl, CURLOPT_HTTPGET, 1L );
1263 CURLcode ok = curl_easy_perform( _curl );
1264 MIL <<
"perform code: " << ok <<
" [ " << curl_easy_strerror(ok) <<
" ]" << endl;
1269 curl_easy_setopt( _curl, CURLOPT_NOBODY, 0L);
1279 curl_easy_setopt( _curl, CURLOPT_HTTPGET, 1L);
1288 curl_easy_setopt( _curl, CURLOPT_RANGE, NULL);
1309 return ( ok == CURLE_OK );
1315 #if DETECT_DIR_INDEX
1328 bool not_a_file =
false;
1330 CURLcode ret = curl_easy_getinfo( _curl,
1331 CURLINFO_EFFECTIVE_URL,
1333 if ( ret == CURLE_OK && ptr != NULL)
1338 std::string path( eurl.getPathName());
1339 if( !path.empty() && path !=
"/" && *path.rbegin() ==
'/')
1341 DBG <<
"Effective url ("
1343 <<
") seems to provide the index of a directory"
1359 Pathname dest = target.absolutename();
1362 DBG <<
"assert_dir " << dest.dirname() <<
" failed" << endl;
1366 ManagedFile destNew { target.extend(
".new.zypp.XXXXXX" ) };
1372 ERR <<
"out of memory for temp file name" << endl;
1376 AutoFD tmp_fd { ::mkostemp( buf, O_CLOEXEC ) };
1379 ERR <<
"mkstemp failed for file '" << destNew <<
"'" << endl;
1384 file = ::fdopen( tmp_fd,
"we" );
1387 ERR <<
"fopen failed for file '" << destNew <<
"'" << endl;
1390 tmp_fd.resetDispose();
1393 DBG <<
"dest: " << dest << endl;
1394 DBG <<
"temp: " << destNew << endl;
1399 curl_easy_setopt(_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
1400 curl_easy_setopt(_curl, CURLOPT_TIMEVALUE, (
long)PathInfo(target).mtime());
1404 curl_easy_setopt(_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1405 curl_easy_setopt(_curl, CURLOPT_TIMEVALUE, 0L);
1413 curl_easy_setopt(_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1414 curl_easy_setopt(_curl, CURLOPT_TIMEVALUE, 0L);
1418 long httpReturnCode = 0;
1419 CURLcode infoRet = curl_easy_getinfo(_curl,
1420 CURLINFO_RESPONSE_CODE,
1422 bool modified =
true;
1423 if (infoRet == CURLE_OK)
1426 if ( httpReturnCode == 304
1429 DBG <<
" Not modified.";
1436 WAR <<
"Could not get the reponse code." << endl;
1439 if (modified || infoRet != CURLE_OK)
1444 ERR <<
"Failed to chmod file " << destNew << endl;
1448 if ( ::fclose( file ) )
1450 ERR <<
"Fclose failed for file '" << destNew <<
"'" << endl;
1455 if (
rename( destNew, dest ) != 0 ) {
1456 ERR <<
"Rename failed" << endl;
1459 destNew.resetDispose();
1462 DBG <<
"done: " << PathInfo(dest) << endl;
1469 DBG << filename.asString() << endl;
1494 string urlBuffer( curlUrl.
asString());
1495 CURLcode ret = curl_easy_setopt( _curl, CURLOPT_URL,
1496 urlBuffer.c_str() );
1501 ret = curl_easy_setopt( _curl, CURLOPT_WRITEDATA, file );
1509 report->start(url, dest);
1510 if ( curl_easy_setopt( _curl, CURLOPT_PROGRESSDATA, &progressData ) != 0 ) {
1511 WAR <<
"Can't set CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
1514 ret = curl_easy_perform( _curl );
1515 #if CURLVERSION_AT_LEAST(7,19,4)
1520 if ( ftell(file) == 0 && ret == 0 )
1522 long httpReturnCode = 33;
1523 if ( curl_easy_getinfo( _curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) == CURLE_OK && httpReturnCode == 200 )
1525 long conditionUnmet = 33;
1526 if ( curl_easy_getinfo( _curl, CURLINFO_CONDITION_UNMET, &conditionUnmet ) == CURLE_OK && conditionUnmet )
1528 WAR <<
"TIMECONDITION unmet - retry without." << endl;
1529 curl_easy_setopt(_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1530 curl_easy_setopt(_curl, CURLOPT_TIMEVALUE, 0L);
1531 ret = curl_easy_perform( _curl );
1537 if ( curl_easy_setopt( _curl, CURLOPT_PROGRESSDATA, NULL ) != 0 ) {
1538 WAR <<
"Can't unset CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
1544 <<
", temp file size " << ftell(file)
1545 <<
" bytes." << endl;
1552 if ( progressData.fileSizeExceeded )
1563 #if DETECT_DIR_INDEX
1568 #endif // DETECT_DIR_INDEX
1578 for ( filesystem::DirContent::const_iterator it = content.begin(); it != content.end(); ++it ) {
1579 Pathname filename = dirname + it->name;
1582 switch ( it->type ) {
1589 getDir( filename, recurse_r );
1593 WAR <<
"Ignore error (" << res <<
") on creating local directory '" <<
localPath( filename ) <<
"'" << endl;
1607 const Pathname & dirname,
bool dots )
const
1615 const Pathname & dirname,
bool dots )
const
1624 ProgressData *pdata =
reinterpret_cast<ProgressData *
>( clientp );
1630 pdata->updateStats( 0.0, dlnow );
1631 return pdata->reportProgress();
1638 ProgressData *pdata =
reinterpret_cast<ProgressData *
>( clientp );
1642 long httpReturnCode = 0;
1643 if ( curl_easy_getinfo( pdata->curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) != CURLE_OK || httpReturnCode == 0 )
1644 return aliveCallback( clientp, dltotal, dlnow, ultotal, ulnow );
1646 pdata->updateStats( dltotal, dlnow );
1647 return pdata->reportProgress();
1654 ProgressData *pdata =
reinterpret_cast<ProgressData *
>(clientp);
1655 return pdata ? pdata->curl : 0;
1662 long auth_info = CURLAUTH_NONE;
1665 curl_easy_getinfo(_curl, CURLINFO_HTTPAUTH_AVAIL, &auth_info);
1667 if(infoRet == CURLE_OK)
1681 ProgressData *data =
reinterpret_cast<ProgressData *
>(clientp);
1683 data->_expectedFileSize = expectedFileSize;
1692 Target_Ptr target = zypp::getZYpp()->getTarget();
1699 if (cmcred && firstTry)
1702 DBG <<
"got stored credentials:" << endl << *credentials << endl;
1717 curlcred->setUsername(cmcred->username());
1726 curlcred->setAuthType(availAuthTypes);
1729 if (auth_report->prompt(
_url, prompt_msg, *curlcred))
1731 DBG <<
"callback answer: retry" << endl
1732 <<
"CurlAuthData: " << *curlcred << endl;
1734 if (curlcred->valid())
1736 credentials = curlcred;
1750 DBG <<
"callback answer: cancel" << endl;
1766 if (credentials->authType() == CURLAUTH_NONE)
1767 credentials->setAuthType(availAuthTypes);
1770 if (credentials->authType() != CURLAUTH_NONE)
1774 ret = curl_easy_setopt(_curl, CURLOPT_HTTPAUTH, credentials->authType());
1780 credentials->setUrl(
_url);
void setPassword(const std::string &pass, EEncoding eflag=zypp::url::E_DECODED)
Set the password in the URL authority.
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
static ZConfig & instance()
Singleton ctor.
const std::string & msg() const
Return the message string provided to the ctor.
Flag to request encoded string(s).
Store and operate with byte count.
std::string getPathName(EEncoding eflag=zypp::url::E_DECODED) const
Returns the path name from the URL.
std::string getHost(EEncoding eflag=zypp::url::E_DECODED) const
Returns the hostname or IP from the URL authority.
std::string escape(const C_Str &str_r, const char sep_r)
Escape desired character c using a backslash.
void setPathParams(const std::string ¶ms)
Set the path parameters.
pthread_once_t OnceFlag
The OnceFlag variable type.
std::string getUsername(EEncoding eflag=zypp::url::E_DECODED) const
Returns the username from the URL authority.
AutoDispose< const Pathname > ManagedFile
A Pathname plus associated cleanup code to be executed when path is no longer needed.
void setUsername(const std::string &user, EEncoding eflag=zypp::url::E_DECODED)
Set the username in the URL authority.
bool isValid() const
Verifies the Url.
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
AutoDispose<int> calling ::close
void setFragment(const std::string &fragment, EEncoding eflag=zypp::url::E_DECODED)
Set the fragment string in the URL.
std::string asString() const
Returns a default string representation of the Url object.
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
void setPathName(const std::string &path, EEncoding eflag=zypp::url::E_DECODED)
Set the path name.
int assert_file_mode(const Pathname &path, unsigned mode)
Like assert_file but enforce mode even if the file already exists.
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \t")
Split line_r into words.
void callOnce(OnceFlag &flag, void(*func)())
Call once function.
std::string trim(const std::string &s, const Trim trim_r)
int unlink(const Pathname &path)
Like 'unlink'.
int rename(const Pathname &oldpath, const Pathname &newpath)
Like 'rename'.
bool startsWith(const C_Str &str_r, const C_Str &prefix_r)
alias for hasPrefix
std::list< DirEntry > DirContent
Returned by readdir.
std::string getQueryParam(const std::string ¶m, EEncoding eflag=zypp::url::E_DECODED) const
Return the value for the specified query parameter.
std::string numstring(char n, int w=0)
void resetDispose()
Set no dispose function.
int rmdir(const Pathname &path)
Like 'rmdir'.
Base class for Exception.
bool strToBool(const C_Str &str, bool default_r)
Parse str into a bool depending on the default value.
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
std::string asUserHistory() const
A single (multiline) string composed of asUserString and historyAsString.
AutoDispose<FILE*> calling ::fclose
mode_t applyUmaskTo(mode_t mode_r)
Modify mode_r according to the current umask ( mode_r & ~getUmask() ).
std::string getScheme() const
Returns the scheme name of the URL.
std::string getPassword(EEncoding eflag=zypp::url::E_DECODED) const
Returns the password from the URL authority.
void delQueryParam(const std::string ¶m)
remove the specified query parameter.