16 #include <zypp/base/Logger.h> 17 #include <zypp/ExternalProgram.h> 18 #include <zypp/base/String.h> 19 #include <zypp/base/Gettext.h> 20 #include <zypp-core/parser/Sysconfig> 21 #include <zypp/base/Gettext.h> 24 #include <zypp-curl/ProxyInfo> 25 #include <zypp-curl/auth/CurlAuthData> 26 #include <zypp-media/auth/CredentialManager> 27 #include <zypp-curl/CurlConfig> 28 #include <zypp-curl/private/curlhelper_p.h> 34 #include <sys/types.h> 36 #include <sys/mount.h> 59 time_t _timeStart = 0;
64 double _dnlTotal = 0.0;
65 double _dnlLast = 0.0;
70 double _drateTotal= 0.0;
71 double _drateLast = 0.0;
73 void updateStats(
double dltotal = 0.0,
double dlnow = 0.0 );
75 int reportProgress()
const;
101 , fileSizeExceeded ( false )
103 , _expectedFileSize( expectedFileSize_r )
114 if ( dlnow && dlnow !=
_dnlNow )
166 static const std::string
_value(
168 "X-ZYpp-AnonymousId: %s",
180 static const std::string
_value(
182 "X-ZYpp-DistributionFlavor: %s",
194 static const std::string
_value(
196 "ZYpp " LIBZYPP_VERSION_STRING
" (curl %s) %s" 197 , curl_version_info(CURLVERSION_NOW)->version
213 Pathname MediaCurl::_cookieFile =
"/var/lib/YaST2/cookies";
218 #define SET_OPTION(opt,val) do { \ 219 ret = curl_easy_setopt ( _curl, opt, val ); \ 221 ZYPP_THROW(MediaCurlSetOptException(_url, _curlError)); \ 225 #define SET_OPTION_OFFT(opt,val) SET_OPTION(opt,(curl_off_t)val) 226 #define SET_OPTION_LONG(opt,val) SET_OPTION(opt,(long)val) 227 #define SET_OPTION_VOID(opt,val) SET_OPTION(opt,(void*)val) 229 MediaCurl::MediaCurl(
const Url & url_r,
230 const Pathname & attach_point_hint_r )
240 MIL <<
"MediaCurl::MediaCurl(" << url_r <<
", " << attach_point_hint_r <<
")" << endl;
242 globalInitCurlOnce();
248 char *atemp = ::strdup( apath.
asString().c_str());
251 atemp == NULL || (atest=::mkdtemp(atemp)) == NULL)
253 WAR <<
"attach point " << ainfo.
path()
254 <<
" is not useable for " << url_r.
getScheme() << endl;
257 else if( atest != NULL)
267 return internal::clearQueryString(
url);
279 curl_version_info_data *curl_info = NULL;
280 curl_info = curl_version_info(CURLVERSION_NOW);
282 if (curl_info->protocols)
284 const char *
const *proto;
287 for(proto=curl_info->protocols; !found && *proto; ++proto)
289 if( scheme == std::string((
const char *)*proto))
294 std::string msg(
"Unsupported protocol '");
308 curl_easy_setopt(
_curl, CURLOPT_VERBOSE, 1L);
309 curl_easy_setopt(
_curl, CURLOPT_DEBUGFUNCTION, log_curl);
314 curl_easy_setopt(
_curl, CURLOPT_HEADERFUNCTION, log_redirects_curl);
316 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_ERRORBUFFER,
_curlError );
318 ZYPP_THROW(MediaCurlSetOptException(
_url,
"Error setting error buffer"));
326 TransferSettings vol_settings(
_settings);
334 vol_settings.addHeader(
"Pragma:");
337 _settings.setConnectTimeout(CONNECT_TIMEOUT);
346 catch (
const MediaException &e )
359 switch ( env::ZYPP_MEDIA_CURL_IPRESOLVE() )
361 case 4:
SET_OPTION(CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
break;
362 case 6:
SET_OPTION(CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
break;
385 #if CURLVERSION_AT_LEAST(7,19,4) 388 SET_OPTION( CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS );
390 SET_OPTION( CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS );
399 if( !
_settings.clientCertificatePath().empty() )
403 if( !
_settings.clientKeyPath().empty() )
408 #ifdef CURLSSLOPT_ALLOW_BEAST 410 ret = curl_easy_setopt(
_curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_ALLOW_BEAST );
419 SET_OPTION(CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
436 const auto cred = cm.getCred(
_url );
437 if ( cred && cred->valid() ) {
454 std::string use_auth =
_settings.authType();
455 if (use_auth.empty())
456 use_auth =
"digest,basic";
457 long auth = CurlAuthData::auth_type_str2long(use_auth);
458 if( auth != CURLAUTH_NONE)
460 DBG <<
"Enabling HTTP authentication methods: " << use_auth
461 <<
" (CURLOPT_HTTPAUTH=" << auth <<
")" << std::endl;
470 SET_OPTION(CURLOPT_PROXYAUTH, CURLAUTH_BASIC|CURLAUTH_DIGEST|CURLAUTH_NTLM );
478 std::string proxyuserpwd =
_settings.proxyUserPassword();
480 if ( proxyuserpwd.empty() )
483 CurlConfig::parseConfig(curlconf);
484 if ( curlconf.proxyuserpwd.empty() )
485 DBG <<
"Proxy: ~/.curlrc does not contain the proxy-user option" << endl;
488 proxyuserpwd = curlconf.proxyuserpwd;
489 DBG <<
"Proxy: using proxy-user from ~/.curlrc" << endl;
494 DBG <<
"Proxy: using provided proxy-user '" <<
_settings.proxyUsername() <<
"'" << endl;
497 if ( ! proxyuserpwd.empty() )
499 SET_OPTION(CURLOPT_PROXYUSERPWD, curlUnEscape( proxyuserpwd ).c_str());
502 #if CURLVERSION_AT_LEAST(7,19,4) 503 else if (
_settings.proxy() == EXPLICITLY_NO_PROXY )
507 DBG <<
"Proxy: explicitly NOPROXY" << endl;
513 DBG <<
"Proxy: not explicitly set" << endl;
514 DBG <<
"Proxy: libcurl may look into the environment" << endl;
525 #if CURLVERSION_AT_LEAST(7,15,5) 538 MIL <<
"No cookies requested" << endl;
543 #if CURLVERSION_AT_LEAST(7,18,0) 548 for (
const auto &header : vol_settings.headers() )
578 _curl = curl_easy_init();
616 try { curl_easy_cleanup(
_curl ); }
657 const auto &filename = srcFile.
filename();
673 catch (MediaUnauthorizedException & ex_r)
684 catch (MediaException & excpt_r)
687 if(
typeid(excpt_r) ==
typeid( media::MediaFileNotFoundException ) ||
688 typeid(excpt_r) ==
typeid( media::MediaNotAFileException ) )
692 report->finish(fileurl, reason, excpt_r.asUserHistory());
714 catch (MediaUnauthorizedException & ex_r)
722 catch (MediaException & excpt_r)
736 bool timeout_reached)
const 741 if (filename.
empty())
750 case CURLE_UNSUPPORTED_PROTOCOL:
751 err =
" Unsupported protocol";
754 err +=
" or redirect (";
759 case CURLE_URL_MALFORMAT:
760 case CURLE_URL_MALFORMAT_USER:
763 case CURLE_LOGIN_DENIED:
765 MediaUnauthorizedException(
url,
"Login failed.",
_curlError,
""));
767 case CURLE_HTTP_RETURNED_ERROR:
769 long httpReturnCode = 0;
770 CURLcode infoRet = curl_easy_getinfo(
_curl,
771 CURLINFO_RESPONSE_CODE,
773 if ( infoRet == CURLE_OK )
775 std::string msg =
"HTTP response: " +
str::numstring( httpReturnCode );
776 switch ( httpReturnCode )
782 DBG << msg <<
" Login failed (URL: " <<
url.
asString() <<
")" << std::endl;
783 DBG <<
"MediaUnauthorizedException auth hint: '" << auth_hint <<
"'" << std::endl;
798 if (
url.
getHost().find(
".suse.com") != std::string::npos )
799 msg403 =
_(
"Visit the SUSE Customer Center to check whether your registration is valid and has not expired.");
800 else if (
url.
asString().find(
"novell.com") != std::string::npos)
801 msg403 =
_(
"Visit the Novell Customer Center to check whether your registration is valid and has not expired.");
814 std::string msg =
"Unable to retrieve HTTP response:";
820 case CURLE_FTP_COULDNT_RETR_FILE:
821 #if CURLVERSION_AT_LEAST(7,16,0) 822 case CURLE_REMOTE_FILE_NOT_FOUND:
824 case CURLE_FTP_ACCESS_DENIED:
825 case CURLE_TFTP_NOTFOUND:
826 err =
"File not found";
829 case CURLE_BAD_PASSWORD_ENTERED:
830 case CURLE_FTP_USER_PASSWORD_INCORRECT:
831 err =
"Login failed";
833 case CURLE_COULDNT_RESOLVE_PROXY:
834 case CURLE_COULDNT_RESOLVE_HOST:
835 case CURLE_COULDNT_CONNECT:
836 case CURLE_FTP_CANT_GET_HOST:
837 err =
"Connection failed";
839 case CURLE_WRITE_ERROR:
842 case CURLE_PARTIAL_FILE:
843 case CURLE_OPERATION_TIMEDOUT:
844 timeout_reached =
true;
846 case CURLE_ABORTED_BY_CALLBACK:
847 if( timeout_reached )
849 err =
"Timeout reached";
857 case CURLE_SSL_PEER_CERTIFICATE:
903 std::string urlBuffer( curlUrl.
asString());
904 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_URL,
917 ret = curl_easy_setopt(
_curl, CURLOPT_NOBODY, 1L );
919 ret = curl_easy_setopt(
_curl, CURLOPT_RANGE,
"0-1" );
922 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
923 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
929 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
933 AutoFILE file { ::fopen(
"/dev/null",
"w" ) };
935 ERR <<
"fopen failed for /dev/null" << endl;
936 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
937 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
943 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
950 ret = curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, (*file) );
953 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
954 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
960 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
967 CURLcode ok = curl_easy_perform(
_curl );
968 MIL <<
"perform code: " << ok <<
" [ " << curl_easy_strerror(ok) <<
" ]" << endl;
973 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
983 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L);
992 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL);
1004 catch (
const MediaFileNotFoundException &e ) {
1008 catch (
const MediaException &e ) {
1013 return ( ok == CURLE_OK );
1019 #if DETECT_DIR_INDEX 1032 bool not_a_file =
false;
1034 CURLcode ret = curl_easy_getinfo(
_curl,
1035 CURLINFO_EFFECTIVE_URL,
1037 if ( ret == CURLE_OK && ptr != NULL)
1042 std::string path( eurl.getPathName());
1043 if( !path.empty() && path !=
"/" && *path.rbegin() ==
'/')
1045 DBG <<
"Effective url (" 1047 <<
") seems to provide the index of a directory" 1066 DBG <<
"assert_dir " << dest.
dirname() <<
" failed" << endl;
1076 ERR <<
"out of memory for temp file name" << endl;
1080 AutoFD tmp_fd { ::mkostemp( buf, O_CLOEXEC ) };
1083 ERR <<
"mkstemp failed for file '" << destNew <<
"'" << endl;
1088 file = ::fdopen( tmp_fd,
"we" );
1091 ERR <<
"fopen failed for file '" << destNew <<
"'" << endl;
1094 tmp_fd.resetDispose();
1097 DBG <<
"dest: " << dest << endl;
1098 DBG <<
"temp: " << destNew << endl;
1103 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
1104 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, (
long)
PathInfo(target).mtime());
1108 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1109 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1117 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1118 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1122 long httpReturnCode = 0;
1123 CURLcode infoRet = curl_easy_getinfo(
_curl,
1124 CURLINFO_RESPONSE_CODE,
1126 bool modified =
true;
1127 if (infoRet == CURLE_OK)
1130 if ( httpReturnCode == 304
1133 DBG <<
" Not modified.";
1140 WAR <<
"Could not get the response code." << endl;
1143 if (modified || infoRet != CURLE_OK)
1148 ERR <<
"Failed to chmod file " << destNew << endl;
1152 if ( ::fclose( file ) )
1154 ERR <<
"Fclose failed for file '" << destNew <<
"'" << endl;
1159 if (
rename( destNew, dest ) != 0 ) {
1160 ERR <<
"Rename failed" << endl;
1163 destNew.resetDispose();
1199 std::string urlBuffer( curlUrl.
asString());
1200 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_URL,
1201 urlBuffer.c_str() );
1206 ret = curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, file );
1214 report->start(
url, dest);
1215 if ( curl_easy_setopt(
_curl, CURLOPT_PROGRESSDATA, &progressData ) != 0 ) {
1216 WAR <<
"Can't set CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
1219 ret = curl_easy_perform(
_curl );
1220 #if CURLVERSION_AT_LEAST(7,19,4) 1225 if ( ftell(file) == 0 && ret == 0 )
1227 long httpReturnCode = 33;
1228 if ( curl_easy_getinfo(
_curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) == CURLE_OK && httpReturnCode == 200 )
1230 long conditionUnmet = 33;
1231 if ( curl_easy_getinfo(
_curl, CURLINFO_CONDITION_UNMET, &conditionUnmet ) == CURLE_OK && conditionUnmet )
1233 WAR <<
"TIMECONDITION unmet - retry without." << endl;
1234 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1235 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1236 ret = curl_easy_perform(
_curl );
1242 if ( curl_easy_setopt(
_curl, CURLOPT_PROGRESSDATA, NULL ) != 0 ) {
1243 WAR <<
"Can't unset CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
1249 <<
", temp file size " << ftell(file)
1250 <<
" bytes." << endl;
1257 if ( progressData.fileSizeExceeded )
1258 ZYPP_THROW(MediaFileSizeExceededException(
url, progressData._expectedFileSize));
1262 catch (
const MediaException &e ) {
1268 #if DETECT_DIR_INDEX 1273 #endif // DETECT_DIR_INDEX 1283 for ( filesystem::DirContent::const_iterator it = content.begin(); it != content.end(); ++it ) {
1284 Pathname filename = dirname + it->name;
1287 switch ( it->type ) {
1294 getDir( filename, recurse_r );
1298 WAR <<
"Ignore error (" << res <<
") on creating local directory '" <<
localPath( filename ) <<
"'" << endl;
1312 const Pathname & dirname,
bool dots )
const 1320 const Pathname & dirname,
bool dots )
const 1347 long httpReturnCode = 0;
1348 if ( curl_easy_getinfo( pdata->
curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) != CURLE_OK || httpReturnCode == 0 )
1349 return aliveCallback( clientp, dltotal, dlnow, ultotal, ulnow );
1360 return pdata ? pdata->
curl : 0;
1367 long auth_info = CURLAUTH_NONE;
1370 curl_easy_getinfo(
_curl, CURLINFO_HTTPAUTH_AVAIL, &auth_info);
1372 if(infoRet == CURLE_OK)
1374 return CurlAuthData::auth_type_long2str(auth_info);
1397 CredentialManager cm(CredManagerOptions(
ZConfig::instance().repoManagerRoot()));
1398 CurlAuthData_Ptr credentials;
1401 AuthData_Ptr cmcred = cm.getCred(
_url);
1403 if (cmcred && firstTry)
1405 credentials.reset(
new CurlAuthData(*cmcred));
1406 DBG <<
"got stored credentials:" << endl << *credentials << endl;
1412 CurlAuthData_Ptr curlcred;
1413 curlcred.reset(
new CurlAuthData());
1421 curlcred->setUsername(cmcred->username());
1430 curlcred->setAuthType(availAuthTypes);
1433 if (auth_report->prompt(
_url, prompt_msg, *curlcred))
1435 DBG <<
"callback answer: retry" << endl
1436 <<
"CurlAuthData: " << *curlcred << endl;
1438 if (curlcred->valid())
1440 credentials = curlcred;
1454 DBG <<
"callback answer: cancel" << endl;
1466 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_USERPWD,
_settings.userPassword().c_str());
1470 if (credentials->authType() == CURLAUTH_NONE)
1471 credentials->setAuthType(availAuthTypes);
1474 if (credentials->authType() != CURLAUTH_NONE)
1477 const_cast<MediaCurl*
>(
this)->
_settings.setAuthType(credentials->authTypeAsString());
1478 ret = curl_easy_setopt(
_curl, CURLOPT_HTTPAUTH, credentials->authType());
1484 credentials->setUrl(
_url);
1485 cm.addCred(*credentials);
std::string getScheme() const
Returns the scheme name of the URL.
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
std::string targetDistribution() const
This is register.target attribute of the installed base product.
ProgressData()
Ctor no range [0,0](0).
const Pathname & path() const
Return current Pathname.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
static ZConfig & instance()
Singleton ctor.
const char * distributionFlavorHeader()
Store and operate with byte count.
Pathname extend(const std::string &r) const
Append string r to the last component of the path.
int reportProgress() const
const char * anonymousIdHeader()
AutoDispose< const Pathname > ManagedFile
A Pathname plus associated cleanup code to be executed when path is no longer needed.
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
AutoDispose<int> calling ::close
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.
int assert_file_mode(const Pathname &path, unsigned mode)
Like assert_file but enforce mode even if the file already exists.
double _dnlTotal
Bytes to download or 0 if unknown.
std::string asString() const
Returns a default string representation of the Url object.
std::string getQueryParam(const std::string ¶m, EEncoding eflag=zypp::url::E_DECODED) const
Return the value for the specified query parameter.
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'.
Pathname dirname() const
Return all but the last component od this path.
double _dnlLast
Bytes downloaded at period start.
std::list< DirEntry > DirContent
Returned by readdir.
bool isValid() const
Verifies the Url.
std::string numstring(char n, int w=0)
const char * agentString()
void resetDispose()
Set no dispose function.
zypp::ByteCount _expectedFileSize
int _dnlPercent
Percent completed or 0 if _dnlTotal is unknown.
int rmdir(const Pathname &path)
Like 'rmdir'.
Pathname absolutename() const
Return this path, adding a leading '/' if relative.
Base class for Exception.
double _drateLast
Download rate in last period.
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.
time_t _timeStart
Start total stats.
bool strToBool(const C_Str &str, bool default_r)
Parse str into a bool depending on the default value.
double _dnlNow
Bytes downloaded now.
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Wrapper class for ::stat/::lstat.
AutoDispose<FILE*> calling ::fclose
void updateStats(double dltotal=0.0, double dlnow=0.0)
double _drateTotal
Download rate so far.
std::string anonymousUniqueId() const
anonymous unique id
mode_t applyUmaskTo(mode_t mode_r)
Modify mode_r according to the current umask ( mode_r & ~getUmask() ).
time_t _timeRcv
Start of no-data timeout.
Easy-to use interface to the ZYPP dependency resolver.
std::string distributionFlavor() const
This is flavor attribute of the installed base product but does not require the target to be loaded a...
time_t _timeLast
Start last period(~1sec)
const char * c_str() const
zypp::callback::SendReport< zypp::media::DownloadProgressReport > * report
std::string getUsername(EEncoding eflag=zypp::url::E_DECODED) const
Returns the username from the URL authority.