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) 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)
98 case CURLINFO_TEXT: lvl = 1; pfx =
"*";
break;
99 case CURLINFO_HEADER_IN: lvl = 2; pfx =
"<";
break;
100 case CURLINFO_HEADER_OUT: lvl = 2; pfx =
">";
break;
103 if( lvl > 0 && max_lvl != NULL && lvl <= *((
long *)max_lvl))
105 std::string msg(ptr, len);
106 std::list<std::string> lines;
107 std::list<std::string>::const_iterator line;
109 for(line = lines.begin(); line != lines.end(); ++line)
111 DBG << pfx <<
" " << *line << endl;
119 void *ptr,
size_t size,
size_t nmemb,
void *stream)
123 char * lstart = (
char *)ptr, * lend = (
char *)ptr;
125 size_t max = size * nmemb;
126 while (pos + 1 < max)
129 for (lstart = lend; *lend !=
'\n' && pos < max; ++lend, ++pos);
132 string line(lstart, lend);
133 if (line.find(
"Location") != string::npos)
135 DBG <<
"redirecting to " << line << endl;
160 inline int getZYPP_MEDIA_CURL_IPRESOLVE()
163 if (
const char * envp = getenv(
"ZYPP_MEDIA_CURL_IPRESOLVE" ) )
165 WAR <<
"env set: $ZYPP_MEDIA_CURL_IPRESOLVE='" << envp <<
"'" << endl;
166 if ( strcmp( envp,
"4" ) == 0 ) ret = 4;
167 else if ( strcmp( envp,
"6" ) == 0 ) ret = 6;
175 static int _v = getZYPP_MEDIA_CURL_IPRESOLVE();
186 ProgressData( CURL *_curl, time_t _timeout = 0,
const Url & _url = Url(),
187 ByteCount expectedFileSize_r = 0,
188 callback::SendReport<DownloadProgressReport> *_report =
nullptr )
203 callback::SendReport<DownloadProgressReport> *
report;
220 void updateStats(
double dltotal = 0.0,
double dlnow = 0.0 )
228 if ( dlnow && dlnow !=
_dnlNow )
269 int reportProgress()
const 299 inline void escape(
string & str_r,
300 const char char_r,
const string & escaped_r ) {
302 pos != string::npos; pos = str_r.find( char_r, pos ) ) {
303 str_r.replace( pos, 1, escaped_r );
307 inline string escapedPath(
string path_r ) {
308 escape( path_r,
' ',
"%20" );
312 inline string unEscape(
string text_r ) {
313 char * tmp = curl_unescape( text_r.c_str(), 0 );
327 std::string param(
url.getQueryParam(
"timeout"));
330 long num = str::strtonum<long>(param);
335 if ( !
url.getUsername().empty() )
338 if (
url.getPassword().size() )
344 if ( (
url.getScheme() ==
"ftp" ||
url.getScheme() ==
"tftp" ) && s.
username().empty() )
348 if (
url.getScheme() ==
"https" )
353 std::string verify(
url.getQueryParam(
"ssl_verify"));
354 if( verify.empty() ||
360 else if( verify ==
"no")
367 std::vector<std::string> flags;
368 std::vector<std::string>::const_iterator flag;
369 str::split( verify, std::back_inserter(flags),
",");
370 for(flag = flags.begin(); flag != flags.end(); ++flag)
374 else if( *flag ==
"peer")
382 Pathname ca_path(
url.getQueryParam(
"ssl_capath") );
383 if( ! ca_path.empty())
391 Pathname client_cert(
url.getQueryParam(
"ssl_clientcert") );
392 if( ! client_cert.empty())
399 Pathname client_key(
url.getQueryParam(
"ssl_clientkey") );
400 if( ! client_key.empty())
408 param =
url.getQueryParam(
"proxy" );
409 if ( ! param.empty() )
420 string proxyport(
url.getQueryParam(
"proxyport" ) );
421 if ( ! proxyport.empty() ) {
422 param +=
":" + proxyport;
429 param =
url.getQueryParam(
"proxyuser" );
430 if ( ! param.empty() )
437 param =
url.getQueryParam(
"auth");
438 if (!param.empty() && (
url.getScheme() ==
"http" ||
url.getScheme() ==
"https"))
442 CurlAuthData::auth_type_str2long(param);
446 DBG <<
"Rethrowing as MediaUnauthorizedException.";
453 param =
url.getQueryParam(
"head_requests");
454 if( !param.empty() && param ==
"no" )
471 s.
setProxy( u.asString( url::ViewOption::WITH_SCHEME + url::ViewOption::WITH_HOST + url::ViewOption::WITH_PORT ) );
484 Pathname MediaCurl::_cookieFile =
"/var/lib/YaST2/cookies";
496 static const std::string
_value(
498 "X-ZYpp-AnonymousId: %s",
499 Target::anonymousUniqueId(
Pathname() ).c_str() ) )
514 static const std::string
_value(
516 "X-ZYpp-DistributionFlavor: %s",
517 Target::distributionFlavor(
Pathname() ).c_str() ) )
532 static const std::string
_value(
534 "ZYpp %s (curl %s) %s" 536 , curl_version_info(CURLVERSION_NOW)->version
537 , Target::targetDistribution(
Pathname() ).c_str()
546 #define SET_OPTION(opt,val) do { \ 547 ret = curl_easy_setopt ( _curl, opt, val ); \ 549 ZYPP_THROW(MediaCurlSetOptException(_url, _curlError)); \ 553 #define SET_OPTION_OFFT(opt,val) SET_OPTION(opt,(curl_off_t)val) 554 #define SET_OPTION_LONG(opt,val) SET_OPTION(opt,(long)val) 555 #define SET_OPTION_VOID(opt,val) SET_OPTION(opt,(void*)val) 557 MediaCurl::MediaCurl(
const Url & url_r,
558 const Pathname & attach_point_hint_r )
568 MIL <<
"MediaCurl::MediaCurl(" << url_r <<
", " << attach_point_hint_r <<
")" << endl;
576 char *atemp = ::strdup( apath.
asString().c_str());
579 atemp == NULL || (atest=::mkdtemp(atemp)) == NULL)
581 WAR <<
"attach point " << ainfo.
path()
582 <<
" is not useable for " << url_r.
getScheme() << endl;
585 else if( atest != NULL)
633 curl_version_info_data *curl_info = NULL;
634 curl_info = curl_version_info(CURLVERSION_NOW);
636 if (curl_info->protocols)
638 const char *
const *proto;
641 for(proto=curl_info->protocols; !found && *proto; ++proto)
643 if( scheme == std::string((
const char *)*proto))
648 std::string msg(
"Unsupported protocol '");
659 char *ptr = getenv(
"ZYPP_MEDIA_CURL_DEBUG");
660 _curlDebug = (ptr && *ptr) ? str::strtonum<long>( ptr) : 0L;
663 curl_easy_setopt(
_curl, CURLOPT_VERBOSE, 1L);
664 curl_easy_setopt(
_curl, CURLOPT_DEBUGFUNCTION, log_curl);
669 curl_easy_setopt(
_curl, CURLOPT_HEADERFUNCTION, log_redirects_curl);
670 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_ERRORBUFFER,
_curlError );
717 case 4:
SET_OPTION(CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
break;
718 case 6:
SET_OPTION(CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
break;
742 #if CURLVERSION_AT_LEAST(7,19,4) 744 SET_OPTION( CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS );
762 #ifdef CURLSSLOPT_ALLOW_BEAST 764 ret = curl_easy_setopt(
_curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_ALLOW_BEAST );
773 SET_OPTION(CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
789 if (use_auth.empty())
790 use_auth =
"digest,basic";
792 if( auth != CURLAUTH_NONE)
794 DBG <<
"Enabling HTTP authentication methods: " << use_auth
795 <<
" (CURLOPT_HTTPAUTH=" << auth <<
")" << std::endl;
804 SET_OPTION(CURLOPT_PROXYAUTH, CURLAUTH_BASIC|CURLAUTH_DIGEST|CURLAUTH_NTLM );
814 if ( proxyuserpwd.empty() )
819 DBG <<
"Proxy: ~/.curlrc does not contain the proxy-user option" << endl;
823 DBG <<
"Proxy: using proxy-user from ~/.curlrc" << endl;
831 if ( ! proxyuserpwd.empty() )
833 SET_OPTION(CURLOPT_PROXYUSERPWD, unEscape( proxyuserpwd ).c_str());
836 #if CURLVERSION_AT_LEAST(7,19,4) 841 DBG <<
"Proxy: explicitly NOPROXY" << endl;
847 DBG <<
"Proxy: not explicitly set" << endl;
848 DBG <<
"Proxy: libcurl may look into the environment" << endl;
859 #if CURLVERSION_AT_LEAST(7,15,5) 871 MIL <<
"No cookies requested" << endl;
876 #if CURLVERSION_AT_LEAST(7,18,0) 881 for ( TransferSettings::Headers::const_iterator it = vol_settings.
headersBegin();
913 _curl = curl_easy_init();
950 curl_easy_cleanup(
_curl );
1066 bool timeout_reached)
const 1071 if (filename.
empty())
1080 case CURLE_UNSUPPORTED_PROTOCOL:
1081 case CURLE_URL_MALFORMAT:
1082 case CURLE_URL_MALFORMAT_USER:
1085 case CURLE_LOGIN_DENIED:
1089 case CURLE_HTTP_RETURNED_ERROR:
1091 long httpReturnCode = 0;
1092 CURLcode infoRet = curl_easy_getinfo(
_curl,
1093 CURLINFO_RESPONSE_CODE,
1095 if ( infoRet == CURLE_OK )
1097 string msg =
"HTTP response: " +
str::numstring( httpReturnCode );
1098 switch ( httpReturnCode )
1104 DBG << msg <<
" Login failed (URL: " <<
url.
asString() <<
")" << std::endl;
1105 DBG <<
"MediaUnauthorizedException auth hint: '" << auth_hint <<
"'" << std::endl;
1120 if (
url.
asString().find(
"novell.com") != string::npos)
1121 msg403 =
_(
"Visit the Novell Customer Center to check whether your registration is valid and has not expired.");
1134 string msg =
"Unable to retrieve HTTP response:";
1140 case CURLE_FTP_COULDNT_RETR_FILE:
1141 #if CURLVERSION_AT_LEAST(7,16,0) 1142 case CURLE_REMOTE_FILE_NOT_FOUND:
1144 case CURLE_FTP_ACCESS_DENIED:
1145 case CURLE_TFTP_NOTFOUND:
1146 err =
"File not found";
1149 case CURLE_BAD_PASSWORD_ENTERED:
1150 case CURLE_FTP_USER_PASSWORD_INCORRECT:
1151 err =
"Login failed";
1153 case CURLE_COULDNT_RESOLVE_PROXY:
1154 case CURLE_COULDNT_RESOLVE_HOST:
1155 case CURLE_COULDNT_CONNECT:
1156 case CURLE_FTP_CANT_GET_HOST:
1157 err =
"Connection failed";
1159 case CURLE_WRITE_ERROR:
1160 err =
"Write error";
1162 case CURLE_PARTIAL_FILE:
1163 case CURLE_OPERATION_TIMEDOUT:
1164 timeout_reached =
true;
1166 case CURLE_ABORTED_BY_CALLBACK:
1167 if( timeout_reached )
1169 err =
"Timeout reached";
1177 case CURLE_SSL_PEER_CERTIFICATE:
1222 string urlBuffer( curlUrl.
asString());
1223 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_URL,
1224 urlBuffer.c_str() );
1236 ret = curl_easy_setopt(
_curl, CURLOPT_NOBODY, 1L );
1238 ret = curl_easy_setopt(
_curl, CURLOPT_RANGE,
"0-1" );
1241 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
1242 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
1248 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
1252 FILE *file = ::fopen(
"/dev/null",
"w" );
1254 ERR <<
"fopen failed for /dev/null" << endl;
1255 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
1256 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
1262 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
1269 ret = curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, file );
1273 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
1274 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
1280 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
1287 CURLcode ok = curl_easy_perform(
_curl );
1288 MIL <<
"perform code: " << ok <<
" [ " << curl_easy_strerror(ok) <<
" ]" << endl;
1293 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
1303 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L);
1312 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL);
1337 return ( ok == CURLE_OK );
1343 #if DETECT_DIR_INDEX 1356 bool not_a_file =
false;
1358 CURLcode ret = curl_easy_getinfo(
_curl,
1359 CURLINFO_EFFECTIVE_URL,
1361 if ( ret == CURLE_OK && ptr != NULL)
1366 std::string path( eurl.getPathName());
1367 if( !path.empty() && path !=
"/" && *path.rbegin() ==
'/')
1369 DBG <<
"Effective url (" 1371 <<
") seems to provide the index of a directory" 1390 DBG <<
"assert_dir " << dest.
dirname() <<
" failed" << endl;
1394 string destNew = target.
asString() +
".new.zypp.XXXXXX";
1395 char *buf = ::strdup( destNew.c_str());
1398 ERR <<
"out of memory for temp file name" << endl;
1403 int tmp_fd = ::mkostemp( buf, O_CLOEXEC );
1407 ERR <<
"mkstemp failed for file '" << destNew <<
"'" << endl;
1413 FILE *file = ::fdopen( tmp_fd,
"we" );
1417 ERR <<
"fopen failed for file '" << destNew <<
"'" << endl;
1421 DBG <<
"dest: " << dest << endl;
1422 DBG <<
"temp: " << destNew << endl;
1427 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
1428 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, (
long)
PathInfo(target).mtime());
1432 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1433 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1443 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1444 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1448 long httpReturnCode = 0;
1449 CURLcode infoRet = curl_easy_getinfo(
_curl,
1450 CURLINFO_RESPONSE_CODE,
1452 bool modified =
true;
1453 if (infoRet == CURLE_OK)
1456 if ( httpReturnCode == 304
1459 DBG <<
" Not modified.";
1466 WAR <<
"Could not get the reponse code." << endl;
1469 if (modified || infoRet != CURLE_OK)
1474 ERR <<
"Failed to chmod file " << destNew << endl;
1476 if (::fclose( file ))
1478 ERR <<
"Fclose failed for file '" << destNew <<
"'" << endl;
1482 if (
rename( destNew, dest ) != 0 ) {
1483 ERR <<
"Rename failed" << endl;
1526 string urlBuffer( curlUrl.
asString());
1527 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_URL,
1528 urlBuffer.c_str() );
1533 ret = curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, file );
1542 if ( curl_easy_setopt(
_curl, CURLOPT_PROGRESSDATA, &progressData ) != 0 ) {
1543 WAR <<
"Can't set CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
1546 ret = curl_easy_perform(
_curl );
1547 #if CURLVERSION_AT_LEAST(7,19,4) 1552 if ( ftell(file) == 0 && ret == 0 )
1554 long httpReturnCode = 33;
1555 if ( curl_easy_getinfo(
_curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) == CURLE_OK && httpReturnCode == 200 )
1557 long conditionUnmet = 33;
1558 if ( curl_easy_getinfo(
_curl, CURLINFO_CONDITION_UNMET, &conditionUnmet ) == CURLE_OK && conditionUnmet )
1560 WAR <<
"TIMECONDITION unmet - retry without." << endl;
1561 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1562 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1563 ret = curl_easy_perform(
_curl );
1569 if ( curl_easy_setopt(
_curl, CURLOPT_PROGRESSDATA, NULL ) != 0 ) {
1570 WAR <<
"Can't unset CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
1576 <<
", temp file size " << ftell(file)
1577 <<
" bytes." << endl;
1584 if ( progressData.fileSizeExceeded )
1595 #if DETECT_DIR_INDEX 1600 #endif // DETECT_DIR_INDEX 1610 for ( filesystem::DirContent::const_iterator it = content.begin(); it != content.end(); ++it ) {
1611 Pathname filename = dirname + it->name;
1614 switch ( it->type ) {
1621 getDir( filename, recurse_r );
1625 WAR <<
"Ignore error (" << res <<
") on creating local directory '" <<
localPath( filename ) <<
"'" << endl;
1639 const Pathname & dirname,
bool dots )
const 1647 const Pathname & dirname,
bool dots )
const 1656 ProgressData *pdata =
reinterpret_cast<ProgressData *
>( clientp );
1662 pdata->updateStats( 0.0, dlnow );
1663 return pdata->reportProgress();
1670 ProgressData *pdata =
reinterpret_cast<ProgressData *
>( clientp );
1674 long httpReturnCode = 0;
1675 if ( curl_easy_getinfo( pdata->curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) != CURLE_OK || httpReturnCode == 0 )
1676 return aliveCallback( clientp, dltotal, dlnow, ultotal, ulnow );
1678 pdata->updateStats( dltotal, dlnow );
1679 return pdata->reportProgress();
1686 ProgressData *pdata =
reinterpret_cast<ProgressData *
>(clientp);
1687 return pdata ? pdata->curl : 0;
1694 long auth_info = CURLAUTH_NONE;
1697 curl_easy_getinfo(
_curl, CURLINFO_HTTPAUTH_AVAIL, &auth_info);
1699 if(infoRet == CURLE_OK)
1713 ProgressData *data =
reinterpret_cast<ProgressData *
>(clientp);
1715 data->_expectedFileSize = expectedFileSize;
1730 if (cmcred && firstTry)
1733 DBG <<
"got stored credentials:" << endl << *credentials << endl;
1748 curlcred->setUsername(cmcred->username());
1757 curlcred->setAuthType(availAuthTypes);
1760 if (auth_report->prompt(
_url, prompt_msg, *curlcred))
1762 DBG <<
"callback answer: retry" << endl
1763 <<
"CurlAuthData: " << *curlcred << endl;
1765 if (curlcred->valid())
1767 credentials = curlcred;
1781 DBG <<
"callback answer: cancel" << endl;
1797 if (credentials->authType() == CURLAUTH_NONE)
1798 credentials->setAuthType(availAuthTypes);
1801 if (credentials->authType() != CURLAUTH_NONE)
1805 ret = curl_easy_setopt(
_curl, CURLOPT_HTTPAUTH, credentials->authType());
1811 credentials->setUrl(
_url);
std::string getScheme() const
Returns the scheme name of the 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'.
const Pathname & path() const
Return current Pathname.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
static ZConfig & instance()
Singleton ctor.
Flag to request encoded string(s).
Store and operate with byte count.
const char * c_str() const
String representation.
void setPathParams(const std::string ¶ms)
Set the path parameters.
pthread_once_t OnceFlag
The OnceFlag variable type.
void setUsername(const std::string &user, EEncoding eflag=zypp::url::E_DECODED)
Set the username in the URL authority.
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \)
Split line_r into words.
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
void setFragment(const std::string &fragment, EEncoding eflag=zypp::url::E_DECODED)
Set the fragment string in the URL.
int ZYPP_MEDIA_CURL_IPRESOLVE()
bool empty() const
Test for an empty path.
#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.
std::string asString() const
Returns a default string representation of the Url object.
std::string escape(const C_Str &str_r, const char sep_r)
Escape desired character c using a backslash.
std::string getQueryParam(const std::string ¶m, EEncoding eflag=zypp::url::E_DECODED) const
Return the value for the specified query parameter.
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'.
const std::string & asString() const
String representation.
int rename(const Pathname &oldpath, const Pathname &newpath)
Like 'rename'.
std::string asUserHistory() const
A single (multiline) string composed of asUserString and historyAsString.
Pathname dirname() const
Return all but the last component od this path.
std::list< DirEntry > DirContent
Returned by readdir.
bool isValid() const
Verifies the Url.
std::string numstring(char n, int w=0)
int rmdir(const Pathname &path)
Like 'rmdir'.
Pathname absolutename() const
Return this path, adding a leading '/' if relative.
Base class for Exception.
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.
bool strToBool(const C_Str &str, bool default_r)
Parse str into a bool depending on the default value.
Wrapper class for ::stat/::lstat.
mode_t applyUmaskTo(mode_t mode_r)
Modify mode_r according to the current umask ( mode_r & ~getUmask() ).
Easy-to use interface to the ZYPP dependency resolver.
void delQueryParam(const std::string ¶m)
remove the specified query parameter.
std::string getUsername(EEncoding eflag=zypp::url::E_DECODED) const
Returns the username from the URL authority.
const std::string & msg() const
Return the message string provided to the ctor.