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)
176 callback::SendReport<DownloadProgressReport> *
report;
196 inline void escape(
string & str_r,
197 const char char_r,
const string & escaped_r ) {
199 pos != string::npos; pos = str_r.find( char_r, pos ) ) {
200 str_r.replace( pos, 1, escaped_r );
204 inline string escapedPath(
string path_r ) {
205 escape( path_r,
' ',
"%20" );
209 inline string unEscape(
string text_r ) {
210 char * tmp = curl_unescape( text_r.c_str(), 0 );
227 long num = str::strtonum<long>(param);
251 if( verify.empty() ||
257 else if( verify ==
"no")
264 std::vector<std::string> flags;
265 std::vector<std::string>::const_iterator flag;
266 str::split( verify, std::back_inserter(flags),
",");
267 for(flag = flags.begin(); flag != flags.end(); ++flag)
271 else if( *flag ==
"peer")
280 if( ! ca_path.empty())
282 if( !PathInfo(ca_path).isDir() || ! ca_path.absolute())
289 if ( ! param.empty() )
301 if ( ! proxyport.empty() ) {
302 param +=
":" + proxyport;
310 if ( ! param.empty() )
322 CurlAuthData::auth_type_str2long(param);
326 DBG <<
"Rethrowing as MediaUnauthorizedException.";
334 if( !param.empty() && param ==
"no" )
351 s.
setProxy( u.asString( url::ViewOption::WITH_SCHEME + url::ViewOption::WITH_HOST + url::ViewOption::WITH_PORT ) );
364 Pathname MediaCurl::_cookieFile =
"/var/lib/YaST2/cookies";
376 static const std::string
_value(
378 "X-ZYpp-AnonymousId: %s",
379 Target::anonymousUniqueId( Pathname() ).c_str() ) )
381 return _value.c_str();
394 static const std::string
_value(
396 "X-ZYpp-DistributionFlavor: %s",
397 Target::distributionFlavor( Pathname() ).c_str() ) )
399 return _value.c_str();
412 static const std::string
_value(
414 "ZYpp %s (curl %s) %s"
416 , curl_version_info(CURLVERSION_NOW)->version
417 , Target::targetDistribution( Pathname() ).c_str()
420 return _value.c_str();
426 #define SET_OPTION(opt,val) do { \
427 ret = curl_easy_setopt ( _curl, opt, val ); \
429 ZYPP_THROW(MediaCurlSetOptException(_url, _curlError)); \
433 #define SET_OPTION_OFFT(opt,val) SET_OPTION(opt,(curl_off_t)val)
434 #define SET_OPTION_LONG(opt,val) SET_OPTION(opt,(long)val)
435 #define SET_OPTION_VOID(opt,val) SET_OPTION(opt,(void*)val)
437 MediaCurl::MediaCurl(
const Url & url_r,
438 const Pathname & attach_point_hint_r )
448 MIL <<
"MediaCurl::MediaCurl(" << url_r <<
", " << attach_point_hint_r <<
")" << endl;
456 char *atemp = ::strdup( apath.asString().c_str());
458 if( !ainfo.isDir() || !ainfo.userMayRWX() ||
459 atemp == NULL || (atest=::mkdtemp(atemp)) == NULL)
461 WAR <<
"attach point " << ainfo.path()
462 <<
" is not useable for " << url_r.
getScheme() << endl;
465 else if( atest != NULL)
510 curl_version_info_data *curl_info = NULL;
511 curl_info = curl_version_info(CURLVERSION_NOW);
513 if (curl_info->protocols)
515 const char *
const *proto;
518 for(proto=curl_info->protocols; !found && *proto; ++proto)
520 if( scheme == std::string((
const char *)*proto))
525 std::string msg(
"Unsupported protocol '");
536 char *ptr = getenv(
"ZYPP_MEDIA_CURL_DEBUG");
537 _curlDebug = (ptr && *ptr) ? str::strtonum<long>( ptr) : 0L;
540 curl_easy_setopt(
_curl, CURLOPT_VERBOSE, 1L);
541 curl_easy_setopt(
_curl, CURLOPT_DEBUGFUNCTION, log_curl);
546 curl_easy_setopt(
_curl, CURLOPT_HEADERFUNCTION, log_redirects_curl);
547 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_ERRORBUFFER,
_curlError );
599 #if CURLVERSION_AT_LEAST(7,19,4)
601 SET_OPTION( CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS );
627 if (use_auth.empty())
628 use_auth =
"digest,basic";
630 if( auth != CURLAUTH_NONE)
632 DBG <<
"Enabling HTTP authentication methods: " << use_auth
633 <<
" (CURLOPT_HTTPAUTH=" << auth <<
")" << std::endl;
642 SET_OPTION(CURLOPT_PROXYAUTH, CURLAUTH_BASIC|CURLAUTH_DIGEST|CURLAUTH_NTLM );
652 if ( proxyuserpwd.empty() )
657 DBG <<
"Proxy: ~/.curlrc does not contain the proxy-user option" << endl;
661 DBG <<
"Proxy: using proxy-user from ~/.curlrc" << endl;
669 if ( ! proxyuserpwd.empty() )
671 SET_OPTION(CURLOPT_PROXYUSERPWD, unEscape( proxyuserpwd ).c_str());
674 #if CURLVERSION_AT_LEAST(7,19,4)
679 DBG <<
"Proxy: explicitly NOPROXY" << endl;
686 DBG <<
"Proxy: not explicitly set" << endl;
697 #if CURLVERSION_AT_LEAST(7,15,5)
709 MIL <<
"No cookies requested" << endl;
714 #if CURLVERSION_AT_LEAST(7,18,0)
719 for ( TransferSettings::Headers::const_iterator it = vol_settings.
headersBegin();
723 MIL <<
"HEADER " << *it << std::endl;
749 if( mountpoint.empty())
756 _curl = curl_easy_init();
793 curl_easy_cleanup(
_curl );
809 if ( !path.empty() && path !=
"/" && *path.rbegin() ==
'/' &&
810 filename.absolute() )
814 path += filename.asString().substr( 1, filename.asString().size() - 1 );
816 else if ( filename.relative() )
819 if (path.empty()) path =
"/";
820 else if (*path.rbegin() !=
'/' ) path +=
"/";
822 path += filename.asString().substr( 2, filename.asString().size() - 2 );
826 path += filename.asString();
918 bool timeout_reached )
const
923 if (filename.empty())
932 case CURLE_UNSUPPORTED_PROTOCOL:
933 case CURLE_URL_MALFORMAT:
934 case CURLE_URL_MALFORMAT_USER:
937 case CURLE_LOGIN_DENIED:
941 case CURLE_HTTP_RETURNED_ERROR:
943 long httpReturnCode = 0;
944 CURLcode infoRet = curl_easy_getinfo(
_curl,
945 CURLINFO_RESPONSE_CODE,
947 if ( infoRet == CURLE_OK )
949 string msg =
"HTTP response: " +
str::numstring( httpReturnCode );
950 switch ( httpReturnCode )
956 DBG << msg <<
" Login failed (URL: " << url.
asString() <<
")" << std::endl;
957 DBG <<
"MediaUnauthorizedException auth hint: '" << auth_hint <<
"'" << std::endl;
971 if (url.
asString().find(
"novell.com") != string::npos)
972 msg403 =
_(
"Visit the Novell Customer Center to check whether your registration is valid and has not expired.");
979 DBG << msg <<
" (URL: " << url.
asString() <<
")" << std::endl;
984 string msg =
"Unable to retrieve HTTP response:";
985 DBG << msg <<
" (URL: " << url.
asString() <<
")" << std::endl;
990 case CURLE_FTP_COULDNT_RETR_FILE:
991 #if CURLVERSION_AT_LEAST(7,16,0)
992 case CURLE_REMOTE_FILE_NOT_FOUND:
994 case CURLE_FTP_ACCESS_DENIED:
995 case CURLE_TFTP_NOTFOUND:
996 err =
"File not found";
999 case CURLE_BAD_PASSWORD_ENTERED:
1000 case CURLE_FTP_USER_PASSWORD_INCORRECT:
1001 err =
"Login failed";
1003 case CURLE_COULDNT_RESOLVE_PROXY:
1004 case CURLE_COULDNT_RESOLVE_HOST:
1005 case CURLE_COULDNT_CONNECT:
1006 case CURLE_FTP_CANT_GET_HOST:
1007 err =
"Connection failed";
1009 case CURLE_WRITE_ERROR:
1010 err =
"Write error";
1012 case CURLE_PARTIAL_FILE:
1013 case CURLE_OPERATION_TIMEDOUT:
1014 timeout_reached =
true;
1016 case CURLE_ABORTED_BY_CALLBACK:
1017 if( timeout_reached )
1019 err =
"Timeout reached";
1027 case CURLE_SSL_PEER_CERTIFICATE:
1029 err =
"Unrecognized error";
1051 DBG << filename.asString() << endl;
1076 string urlBuffer( curlUrl.
asString());
1077 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_URL,
1078 urlBuffer.c_str() );
1090 ret = curl_easy_setopt(
_curl, CURLOPT_NOBODY, 1L );
1092 ret = curl_easy_setopt(
_curl, CURLOPT_RANGE,
"0-1" );
1095 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
1096 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
1102 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
1106 FILE *file = ::fopen(
"/dev/null",
"w" );
1108 ERR <<
"fopen failed for /dev/null" << endl;
1109 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
1110 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
1116 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
1123 ret = curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, file );
1127 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
1128 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
1134 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
1141 CURLcode ok = curl_easy_perform(
_curl );
1142 MIL <<
"perform code: " << ok <<
" [ " << curl_easy_strerror(ok) <<
" ]" << endl;
1147 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
1157 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L);
1166 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL);
1191 return ( ok == CURLE_OK );
1197 #if DETECT_DIR_INDEX
1210 bool not_a_file =
false;
1212 CURLcode ret = curl_easy_getinfo(
_curl,
1213 CURLINFO_EFFECTIVE_URL,
1215 if ( ret == CURLE_OK && ptr != NULL)
1220 std::string path( eurl.getPathName());
1221 if( !path.empty() && path !=
"/" && *path.rbegin() ==
'/')
1223 DBG <<
"Effective url ("
1225 <<
") seems to provide the index of a directory"
1241 Pathname dest = target.absolutename();
1244 DBG <<
"assert_dir " << dest.dirname() <<
" failed" << endl;
1248 string destNew = target.asString() +
".new.zypp.XXXXXX";
1249 char *buf = ::strdup( destNew.c_str());
1252 ERR <<
"out of memory for temp file name" << endl;
1257 int tmp_fd = ::mkostemp( buf, O_CLOEXEC );
1261 ERR <<
"mkstemp failed for file '" << destNew <<
"'" << endl;
1267 FILE *file = ::fdopen( tmp_fd,
"we" );
1271 ERR <<
"fopen failed for file '" << destNew <<
"'" << endl;
1275 DBG <<
"dest: " << dest << endl;
1276 DBG <<
"temp: " << destNew << endl;
1281 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
1282 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, (
long)PathInfo(target).mtime());
1286 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1287 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1297 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1298 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1302 long httpReturnCode = 0;
1303 CURLcode infoRet = curl_easy_getinfo(
_curl,
1304 CURLINFO_RESPONSE_CODE,
1306 bool modified =
true;
1307 if (infoRet == CURLE_OK)
1310 if ( httpReturnCode == 304
1313 DBG <<
" Not modified.";
1320 WAR <<
"Could not get the reponse code." << endl;
1323 if (modified || infoRet != CURLE_OK)
1328 ERR <<
"Failed to chmod file " << destNew << endl;
1330 if (::fclose( file ))
1332 ERR <<
"Fclose failed for file '" << destNew <<
"'" << endl;
1336 if (
rename( destNew, dest ) != 0 ) {
1337 ERR <<
"Rename failed" << endl;
1348 DBG <<
"done: " << PathInfo(dest) << endl;
1355 DBG << filename.asString() << endl;
1380 string urlBuffer( curlUrl.
asString());
1381 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_URL,
1382 urlBuffer.c_str() );
1387 ret = curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, file );
1395 report->start(url, dest);
1396 if ( curl_easy_setopt(
_curl, CURLOPT_PROGRESSDATA, &progressData ) != 0 ) {
1397 WAR <<
"Can't set CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
1400 ret = curl_easy_perform(
_curl );
1401 #if CURLVERSION_AT_LEAST(7,19,4)
1406 if ( ftell(file) == 0 && ret == 0 )
1408 long httpReturnCode = 33;
1409 if ( curl_easy_getinfo(
_curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) == CURLE_OK && httpReturnCode == 200 )
1411 long conditionUnmet = 33;
1412 if ( curl_easy_getinfo(
_curl, CURLINFO_CONDITION_UNMET, &conditionUnmet ) == CURLE_OK && conditionUnmet )
1414 WAR <<
"TIMECONDITION unmet - retry without." << endl;
1415 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1416 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1417 ret = curl_easy_perform(
_curl );
1423 if ( curl_easy_setopt(
_curl, CURLOPT_PROGRESSDATA, NULL ) != 0 ) {
1424 WAR <<
"Can't unset CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
1430 <<
", temp file size " << ftell(file)
1431 <<
" bytes." << endl;
1445 #if DETECT_DIR_INDEX
1450 #endif // DETECT_DIR_INDEX
1460 for ( filesystem::DirContent::const_iterator it = content.begin(); it != content.end(); ++it ) {
1461 Pathname filename = dirname + it->name;
1464 switch ( it->type ) {
1471 getDir( filename, recurse_r );
1475 WAR <<
"Ignore error (" << res <<
") on creating local directory '" <<
localPath( filename ) <<
"'" << endl;
1489 const Pathname & dirname,
bool dots )
const
1497 const Pathname & dirname,
bool dots )
const
1505 double dltotal,
double dlnow,
1506 double ultotal,
double ulnow)
1508 ProgressData *pdata =
reinterpret_cast<ProgressData *
>(clientp);
1511 time_t now = time(NULL);
1516 if( pdata->ltime <= 0 || pdata->ltime > now)
1524 if (dlnow > 0 || ulnow > 0)
1526 dif = (now - pdata->ltime);
1527 dif = dif > 0 ? dif : 0;
1538 if ( pdata->secs > 1 && (dif > 0 || dlnow == dltotal ))
1539 pdata->drate_avg = (dlnow / pdata->secs);
1543 pdata->drate_period = ((dlnow - pdata->dload_period) / dif);
1544 pdata->dload_period = dlnow;
1551 if (!(*(pdata->report))->progress(
int( dltotal ? dlnow * 100 / dltotal : 0 ),
1554 pdata->drate_period))
1561 if( pdata->timeout > 0)
1565 bool progress =
false;
1568 if( dlnow != pdata->dload)
1571 pdata->dload = dlnow;
1575 if( ulnow != pdata->uload)
1578 pdata->uload = ulnow;
1582 if( !progress && (now >= (pdata->ltime + pdata->timeout)))
1584 pdata->reached =
true;
1597 long auth_info = CURLAUTH_NONE;
1600 curl_easy_getinfo(
_curl, CURLINFO_HTTPAUTH_AVAIL, &auth_info);
1602 if(infoRet == CURLE_OK)
1615 Target_Ptr target = zypp::getZYpp()->getTarget();
1622 if (cmcred && firstTry)
1625 DBG <<
"got stored credentials:" << endl << *credentials << endl;
1640 curlcred->setUsername(cmcred->username());
1645 string prompt_msg = boost::str(boost::format(
1651 curlcred->setAuthType(availAuthTypes);
1654 if (auth_report->prompt(
_url, prompt_msg, *curlcred))
1656 DBG <<
"callback answer: retry" << endl
1657 <<
"CurlAuthData: " << *curlcred << endl;
1659 if (curlcred->valid())
1661 credentials = curlcred;
1675 DBG <<
"callback answer: cancel" << endl;
1691 if (credentials->authType() == CURLAUTH_NONE)
1692 credentials->setAuthType(availAuthTypes);
1695 if (credentials->authType() != CURLAUTH_NONE)
1699 ret = curl_easy_setopt(
_curl, CURLOPT_HTTPAUTH, credentials->authType());
1705 credentials->setUrl(
_url);