34 #include <sys/types.h>
36 #include <sys/mount.h>
40 #include <boost/format.hpp>
42 #define DETECT_DIR_INDEX 0
43 #define CONNECT_TIMEOUT 60
44 #define TRANSFER_TIMEOUT_MAX 60 * 60
46 #define EXPLICITLY_NO_PROXY "_none_"
48 #undef CURLVERSION_AT_LEAST
49 #define CURLVERSION_AT_LEAST(M,N,O) LIBCURL_VERSION_NUM >= ((((M)<<8)+(N))<<8)+(O)
52 using namespace zypp::base;
59 extern "C" void _do_free_once()
61 curl_global_cleanup();
64 extern "C" void globalFreeOnce()
69 extern "C" void _do_init_once()
71 CURLcode ret = curl_global_init( CURL_GLOBAL_ALL );
74 WAR <<
"curl global init failed" << endl;
87 inline void globalInitOnce()
92 int log_curl(CURL *
curl, curl_infotype info,
93 char *ptr,
size_t len,
void *max_lvl)
99 case CURLINFO_TEXT: lvl = 1; pfx =
"*";
break;
100 case CURLINFO_HEADER_IN: lvl = 2; pfx =
"<";
break;
101 case CURLINFO_HEADER_OUT: lvl = 2; pfx =
">";
break;
104 if( lvl > 0 && max_lvl != NULL && lvl <= *((
long *)max_lvl))
106 std::string msg(ptr, len);
107 std::list<std::string> lines;
108 std::list<std::string>::const_iterator line;
110 for(line = lines.begin(); line != lines.end(); ++line)
112 DBG << pfx <<
" " << *line << endl;
120 void *ptr,
size_t size,
size_t nmemb,
void *stream)
124 char * lstart = (
char *)ptr, * lend = (
char *)ptr;
126 size_t max = size * nmemb;
127 while (pos + 1 < max)
130 for (lstart = lend; *lend !=
'\n' && pos < max; ++lend, ++pos);
133 string line(lstart, lend);
134 if (line.find(
"Location") != string::npos)
136 DBG <<
"redirecting to " << line << endl;
161 callback::SendReport<DownloadProgressReport> *_report=NULL)
178 callback::SendReport<DownloadProgressReport> *
report;
198 inline void escape(
string & str_r,
199 const char char_r,
const string & escaped_r ) {
201 pos != string::npos; pos = str_r.find( char_r, pos ) ) {
202 str_r.replace( pos, 1, escaped_r );
206 inline string escapedPath(
string path_r ) {
207 escape( path_r,
' ',
"%20" );
211 inline string unEscape(
string text_r ) {
212 char * tmp = curl_unescape( text_r.c_str(), 0 );
229 long num = str::strtonum<long>(param);
253 if( verify.empty() ||
259 else if( verify ==
"no")
266 std::vector<std::string> flags;
267 std::vector<std::string>::const_iterator flag;
268 str::split( verify, std::back_inserter(flags),
",");
269 for(flag = flags.begin(); flag != flags.end(); ++flag)
273 else if( *flag ==
"peer")
282 if( ! ca_path.empty())
284 if( !PathInfo(ca_path).isDir() || ! ca_path.absolute())
291 if( ! client_cert.empty())
293 if( !PathInfo(client_cert).isFile() || !client_cert.absolute())
300 if ( ! param.empty() )
312 if ( ! proxyport.empty() ) {
313 param +=
":" + proxyport;
321 if ( ! param.empty() )
333 CurlAuthData::auth_type_str2long(param);
337 DBG <<
"Rethrowing as MediaUnauthorizedException.";
345 if( !param.empty() && param ==
"no" )
362 s.
setProxy( u.asString( url::ViewOption::WITH_SCHEME + url::ViewOption::WITH_HOST + url::ViewOption::WITH_PORT ) );
375 Pathname MediaCurl::_cookieFile =
"/var/lib/YaST2/cookies";
387 static const std::string
_value(
389 "X-ZYpp-AnonymousId: %s",
390 Target::anonymousUniqueId( Pathname() ).c_str() ) )
392 return _value.c_str();
405 static const std::string
_value(
407 "X-ZYpp-DistributionFlavor: %s",
408 Target::distributionFlavor( Pathname() ).c_str() ) )
410 return _value.c_str();
423 static const std::string
_value(
425 "ZYpp %s (curl %s) %s"
427 , curl_version_info(CURLVERSION_NOW)->version
428 , Target::targetDistribution( Pathname() ).c_str()
431 return _value.c_str();
437 #define SET_OPTION(opt,val) do { \
438 ret = curl_easy_setopt ( _curl, opt, val ); \
440 ZYPP_THROW(MediaCurlSetOptException(_url, _curlError)); \
444 #define SET_OPTION_OFFT(opt,val) SET_OPTION(opt,(curl_off_t)val)
445 #define SET_OPTION_LONG(opt,val) SET_OPTION(opt,(long)val)
446 #define SET_OPTION_VOID(opt,val) SET_OPTION(opt,(void*)val)
448 MediaCurl::MediaCurl(
const Url & url_r,
449 const Pathname & attach_point_hint_r )
459 MIL <<
"MediaCurl::MediaCurl(" << url_r <<
", " << attach_point_hint_r <<
")" << endl;
467 char *atemp = ::strdup( apath.asString().c_str());
469 if( !ainfo.isDir() || !ainfo.userMayRWX() ||
470 atemp == NULL || (atest=::mkdtemp(atemp)) == NULL)
472 WAR <<
"attach point " << ainfo.path()
473 <<
" is not useable for " << url_r.
getScheme() << endl;
476 else if( atest != NULL)
521 curl_version_info_data *curl_info = NULL;
522 curl_info = curl_version_info(CURLVERSION_NOW);
524 if (curl_info->protocols)
526 const char *
const *proto;
529 for(proto=curl_info->protocols; !found && *proto; ++proto)
531 if( scheme == std::string((
const char *)*proto))
536 std::string msg(
"Unsupported protocol '");
547 char *ptr = getenv(
"ZYPP_MEDIA_CURL_DEBUG");
548 _curlDebug = (ptr && *ptr) ? str::strtonum<long>( ptr) : 0L;
551 curl_easy_setopt(
_curl, CURLOPT_VERBOSE, 1L);
552 curl_easy_setopt(
_curl, CURLOPT_DEBUGFUNCTION, log_curl);
557 curl_easy_setopt(
_curl, CURLOPT_HEADERFUNCTION, log_redirects_curl);
558 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_ERRORBUFFER,
_curlError );
610 #if CURLVERSION_AT_LEAST(7,19,4)
612 SET_OPTION( CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS );
626 #ifdef CURLSSLOPT_ALLOW_BEAST
628 ret = curl_easy_setopt(
_curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_ALLOW_BEAST );
651 if (use_auth.empty())
652 use_auth =
"digest,basic";
654 if( auth != CURLAUTH_NONE)
656 DBG <<
"Enabling HTTP authentication methods: " << use_auth
657 <<
" (CURLOPT_HTTPAUTH=" << auth <<
")" << std::endl;
666 SET_OPTION(CURLOPT_PROXYAUTH, CURLAUTH_BASIC|CURLAUTH_DIGEST|CURLAUTH_NTLM );
676 if ( proxyuserpwd.empty() )
681 DBG <<
"Proxy: ~/.curlrc does not contain the proxy-user option" << endl;
685 DBG <<
"Proxy: using proxy-user from ~/.curlrc" << endl;
693 if ( ! proxyuserpwd.empty() )
695 SET_OPTION(CURLOPT_PROXYUSERPWD, unEscape( proxyuserpwd ).c_str());
698 #if CURLVERSION_AT_LEAST(7,19,4)
703 DBG <<
"Proxy: explicitly NOPROXY" << endl;
710 DBG <<
"Proxy: not explicitly set" << endl;
721 #if CURLVERSION_AT_LEAST(7,15,5)
733 MIL <<
"No cookies requested" << endl;
738 #if CURLVERSION_AT_LEAST(7,18,0)
743 for ( TransferSettings::Headers::const_iterator it = vol_settings.
headersBegin();
773 if( mountpoint.empty())
780 _curl = curl_easy_init();
817 curl_easy_cleanup(
_curl );
833 if ( !path.empty() && path !=
"/" && *path.rbegin() ==
'/' &&
834 filename.absolute() )
838 path += filename.asString().substr( 1, filename.asString().size() - 1 );
840 else if ( filename.relative() )
843 if (path.empty()) path =
"/";
844 else if (*path.rbegin() !=
'/' ) path +=
"/";
846 path += filename.asString().substr( 2, filename.asString().size() - 2 );
850 path += filename.asString();
942 bool timeout_reached )
const
947 if (filename.empty())
956 case CURLE_UNSUPPORTED_PROTOCOL:
957 case CURLE_URL_MALFORMAT:
958 case CURLE_URL_MALFORMAT_USER:
961 case CURLE_LOGIN_DENIED:
965 case CURLE_HTTP_RETURNED_ERROR:
967 long httpReturnCode = 0;
968 CURLcode infoRet = curl_easy_getinfo(
_curl,
969 CURLINFO_RESPONSE_CODE,
971 if ( infoRet == CURLE_OK )
973 string msg =
"HTTP response: " +
str::numstring( httpReturnCode );
974 switch ( httpReturnCode )
980 DBG << msg <<
" Login failed (URL: " << url.
asString() <<
")" << std::endl;
981 DBG <<
"MediaUnauthorizedException auth hint: '" << auth_hint <<
"'" << std::endl;
995 if (url.
asString().find(
"novell.com") != string::npos)
996 msg403 =
_(
"Visit the Novell Customer Center to check whether your registration is valid and has not expired.");
1003 DBG << msg <<
" (URL: " << url.
asString() <<
")" << std::endl;
1008 string msg =
"Unable to retrieve HTTP response:";
1009 DBG << msg <<
" (URL: " << url.
asString() <<
")" << std::endl;
1014 case CURLE_FTP_COULDNT_RETR_FILE:
1015 #if CURLVERSION_AT_LEAST(7,16,0)
1016 case CURLE_REMOTE_FILE_NOT_FOUND:
1018 case CURLE_FTP_ACCESS_DENIED:
1019 case CURLE_TFTP_NOTFOUND:
1020 err =
"File not found";
1023 case CURLE_BAD_PASSWORD_ENTERED:
1024 case CURLE_FTP_USER_PASSWORD_INCORRECT:
1025 err =
"Login failed";
1027 case CURLE_COULDNT_RESOLVE_PROXY:
1028 case CURLE_COULDNT_RESOLVE_HOST:
1029 case CURLE_COULDNT_CONNECT:
1030 case CURLE_FTP_CANT_GET_HOST:
1031 err =
"Connection failed";
1033 case CURLE_WRITE_ERROR:
1034 err =
"Write error";
1036 case CURLE_PARTIAL_FILE:
1037 case CURLE_OPERATION_TIMEDOUT:
1038 timeout_reached =
true;
1040 case CURLE_ABORTED_BY_CALLBACK:
1041 if( timeout_reached )
1043 err =
"Timeout reached";
1051 case CURLE_SSL_PEER_CERTIFICATE:
1053 err =
"Unrecognized error";
1075 DBG << filename.asString() << endl;
1100 string urlBuffer( curlUrl.
asString());
1101 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_URL,
1102 urlBuffer.c_str() );
1114 ret = curl_easy_setopt(
_curl, CURLOPT_NOBODY, 1L );
1116 ret = curl_easy_setopt(
_curl, CURLOPT_RANGE,
"0-1" );
1119 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
1120 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
1126 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
1130 FILE *file = ::fopen(
"/dev/null",
"w" );
1132 ERR <<
"fopen failed for /dev/null" << endl;
1133 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
1134 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
1140 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
1147 ret = curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, file );
1151 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
1152 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
1158 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
1165 CURLcode ok = curl_easy_perform(
_curl );
1166 MIL <<
"perform code: " << ok <<
" [ " << curl_easy_strerror(ok) <<
" ]" << endl;
1171 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
1181 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L);
1190 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL);
1215 return ( ok == CURLE_OK );
1221 #if DETECT_DIR_INDEX
1234 bool not_a_file =
false;
1236 CURLcode ret = curl_easy_getinfo(
_curl,
1237 CURLINFO_EFFECTIVE_URL,
1239 if ( ret == CURLE_OK && ptr != NULL)
1244 std::string path( eurl.getPathName());
1245 if( !path.empty() && path !=
"/" && *path.rbegin() ==
'/')
1247 DBG <<
"Effective url ("
1249 <<
") seems to provide the index of a directory"
1265 Pathname dest = target.absolutename();
1268 DBG <<
"assert_dir " << dest.dirname() <<
" failed" << endl;
1272 string destNew = target.asString() +
".new.zypp.XXXXXX";
1273 char *buf = ::strdup( destNew.c_str());
1276 ERR <<
"out of memory for temp file name" << endl;
1281 int tmp_fd = ::mkostemp( buf, O_CLOEXEC );
1285 ERR <<
"mkstemp failed for file '" << destNew <<
"'" << endl;
1291 FILE *file = ::fdopen( tmp_fd,
"we" );
1295 ERR <<
"fopen failed for file '" << destNew <<
"'" << endl;
1299 DBG <<
"dest: " << dest << endl;
1300 DBG <<
"temp: " << destNew << endl;
1305 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
1306 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, (
long)PathInfo(target).mtime());
1310 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1311 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1321 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1322 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1326 long httpReturnCode = 0;
1327 CURLcode infoRet = curl_easy_getinfo(
_curl,
1328 CURLINFO_RESPONSE_CODE,
1330 bool modified =
true;
1331 if (infoRet == CURLE_OK)
1334 if ( httpReturnCode == 304
1337 DBG <<
" Not modified.";
1344 WAR <<
"Could not get the reponse code." << endl;
1347 if (modified || infoRet != CURLE_OK)
1352 ERR <<
"Failed to chmod file " << destNew << endl;
1354 if (::fclose( file ))
1356 ERR <<
"Fclose failed for file '" << destNew <<
"'" << endl;
1360 if (
rename( destNew, dest ) != 0 ) {
1361 ERR <<
"Rename failed" << endl;
1372 DBG <<
"done: " << PathInfo(dest) << endl;
1379 DBG << filename.asString() << endl;
1404 string urlBuffer( curlUrl.
asString());
1405 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_URL,
1406 urlBuffer.c_str() );
1411 ret = curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, file );
1419 report->start(url, dest);
1420 if ( curl_easy_setopt(
_curl, CURLOPT_PROGRESSDATA, &progressData ) != 0 ) {
1421 WAR <<
"Can't set CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
1424 ret = curl_easy_perform(
_curl );
1425 #if CURLVERSION_AT_LEAST(7,19,4)
1430 if ( ftell(file) == 0 && ret == 0 )
1432 long httpReturnCode = 33;
1433 if ( curl_easy_getinfo(
_curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) == CURLE_OK && httpReturnCode == 200 )
1435 long conditionUnmet = 33;
1436 if ( curl_easy_getinfo(
_curl, CURLINFO_CONDITION_UNMET, &conditionUnmet ) == CURLE_OK && conditionUnmet )
1438 WAR <<
"TIMECONDITION unmet - retry without." << endl;
1439 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1440 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1441 ret = curl_easy_perform(
_curl );
1447 if ( curl_easy_setopt(
_curl, CURLOPT_PROGRESSDATA, NULL ) != 0 ) {
1448 WAR <<
"Can't unset CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
1454 <<
", temp file size " << ftell(file)
1455 <<
" bytes." << endl;
1469 #if DETECT_DIR_INDEX
1474 #endif // DETECT_DIR_INDEX
1484 for ( filesystem::DirContent::const_iterator it = content.begin(); it != content.end(); ++it ) {
1485 Pathname filename = dirname + it->name;
1488 switch ( it->type ) {
1495 getDir( filename, recurse_r );
1499 WAR <<
"Ignore error (" << res <<
") on creating local directory '" <<
localPath( filename ) <<
"'" << endl;
1513 const Pathname & dirname,
bool dots )
const
1521 const Pathname & dirname,
bool dots )
const
1529 double dltotal,
double dlnow,
1530 double ultotal,
double ulnow)
1532 ProgressData *pdata =
reinterpret_cast<ProgressData *
>(clientp);
1536 long httpReturnCode = 0;
1537 if (curl_easy_getinfo(pdata->curl, CURLINFO_RESPONSE_CODE, &httpReturnCode) != CURLE_OK || httpReturnCode == 0)
1540 time_t now = time(NULL);
1545 if( pdata->ltime <= 0 || pdata->ltime > now)
1553 if (dlnow > 0 || ulnow > 0)
1555 dif = (now - pdata->ltime);
1556 dif = dif > 0 ? dif : 0;
1567 if ( pdata->secs > 1 && (dif > 0 || dlnow == dltotal ))
1568 pdata->drate_avg = (dlnow / pdata->secs);
1572 pdata->drate_period = ((dlnow - pdata->dload_period) / dif);
1573 pdata->dload_period = dlnow;
1580 if (!(*(pdata->report))->progress(
int( dltotal ? dlnow * 100 / dltotal : 0 ),
1583 pdata->drate_period))
1590 if( pdata->timeout > 0)
1594 bool progress =
false;
1597 if( dlnow != pdata->dload)
1600 pdata->dload = dlnow;
1604 if( ulnow != pdata->uload)
1607 pdata->uload = ulnow;
1611 if( !progress && (now >= (pdata->ltime + pdata->timeout)))
1613 pdata->reached =
true;
1624 ProgressData *pdata =
reinterpret_cast<ProgressData *
>(clientp);
1625 return pdata ? pdata->curl : 0;
1632 long auth_info = CURLAUTH_NONE;
1635 curl_easy_getinfo(
_curl, CURLINFO_HTTPAUTH_AVAIL, &auth_info);
1637 if(infoRet == CURLE_OK)
1650 Target_Ptr target = zypp::getZYpp()->getTarget();
1657 if (cmcred && firstTry)
1660 DBG <<
"got stored credentials:" << endl << *credentials << endl;
1675 curlcred->setUsername(cmcred->username());
1680 string prompt_msg = boost::str(boost::format(
1686 curlcred->setAuthType(availAuthTypes);
1689 if (auth_report->prompt(
_url, prompt_msg, *curlcred))
1691 DBG <<
"callback answer: retry" << endl
1692 <<
"CurlAuthData: " << *curlcred << endl;
1694 if (curlcred->valid())
1696 credentials = curlcred;
1710 DBG <<
"callback answer: cancel" << endl;
1726 if (credentials->authType() == CURLAUTH_NONE)
1727 credentials->setAuthType(availAuthTypes);
1730 if (credentials->authType() != CURLAUTH_NONE)
1734 ret = curl_easy_setopt(
_curl, CURLOPT_HTTPAUTH, credentials->authType());
1740 credentials->setUrl(
_url);