1 //-*- mode: cpp; mode: fold -*-
3 // $Id: http.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $
4 /* ######################################################################
6 HTTPS Acquire Method - This is the HTTPS acquire method for APT.
10 ##################################################################### */
12 // Include Files /*{{{*/
15 #include <apt-pkg/fileutl.h>
16 #include <apt-pkg/error.h>
17 #include <apt-pkg/hashes.h>
18 #include <apt-pkg/netrc.h>
19 #include <apt-pkg/configuration.h>
20 #include <apt-pkg/macros.h>
21 #include <apt-pkg/strutl.h>
22 #include <apt-pkg/proxy.h>
42 struct APT_HIDDEN CURLUserPointer
{
43 HttpsMethod
* const https
;
44 HttpsMethod::FetchResult
* const Res
;
45 HttpsMethod::FetchItem
const * const Itm
;
46 CURLUserPointer(HttpsMethod
* const https
, HttpsMethod::FetchResult
* const Res
,
47 HttpsMethod::FetchItem
const * const Itm
) : https(https
), Res(Res
), Itm(Itm
) {}
51 HttpsMethod::parse_header(void *buffer
, size_t size
, size_t nmemb
, void *userp
)
53 size_t len
= size
* nmemb
;
54 CURLUserPointer
*me
= static_cast<CURLUserPointer
*>(userp
);
55 std::string
line((char*) buffer
, len
);
56 for (--len
; len
> 0; --len
)
57 if (isspace_ascii(line
[len
]) == 0)
64 if (line
.empty() == true)
66 me
->https
->Server
->JunkSize
= 0;
67 if (me
->https
->Server
->Result
!= 416 && me
->https
->Server
->StartPos
!= 0)
69 else if (me
->https
->Server
->Result
== 416)
71 bool partialHit
= false;
72 if (me
->Itm
->ExpectedHashes
.usable() == true)
74 Hashes
resultHashes(me
->Itm
->ExpectedHashes
);
75 FileFd
file(me
->Itm
->DestFile
, FileFd::ReadOnly
);
76 me
->https
->Server
->TotalFileSize
= file
.FileSize();
77 me
->https
->Server
->Date
= file
.ModificationTime();
78 resultHashes
.AddFD(file
);
79 HashStringList
const hashList
= resultHashes
.GetHashStringList();
80 partialHit
= (me
->Itm
->ExpectedHashes
== hashList
);
82 else if (me
->https
->Server
->Result
== 416 && me
->https
->Server
->TotalFileSize
== me
->https
->File
->FileSize())
85 if (partialHit
== true)
87 me
->https
->Server
->Result
= 200;
88 me
->https
->Server
->StartPos
= me
->https
->Server
->TotalFileSize
;
89 // the actual size is not important for https as curl will deal with it
90 // by itself and e.g. doesn't bother us with transport-encoding…
91 me
->https
->Server
->JunkSize
= std::numeric_limits
<unsigned long long>::max();
94 me
->https
->Server
->StartPos
= 0;
97 me
->https
->Server
->StartPos
= 0;
99 me
->Res
->LastModified
= me
->https
->Server
->Date
;
100 me
->Res
->Size
= me
->https
->Server
->TotalFileSize
;
101 me
->Res
->ResumePoint
= me
->https
->Server
->StartPos
;
103 // we expect valid data, so tell our caller we get the file now
104 if (me
->https
->Server
->Result
>= 200 && me
->https
->Server
->Result
< 300)
106 if (me
->Res
->Size
!= 0 && me
->Res
->Size
> me
->Res
->ResumePoint
)
107 me
->https
->URIStart(*me
->Res
);
108 if (me
->https
->Server
->AddPartialFileToHashes(*(me
->https
->File
)) == false)
112 me
->https
->Server
->JunkSize
= std::numeric_limits
<decltype(me
->https
->Server
->JunkSize
)>::max();
114 else if (me
->https
->Server
->HeaderLine(line
) == false)
121 HttpsMethod::write_data(void *buffer
, size_t size
, size_t nmemb
, void *userp
)
123 HttpsMethod
*me
= static_cast<HttpsMethod
*>(userp
);
124 size_t buffer_size
= size
* nmemb
;
125 // we don't need to count the junk here, just drop anything we get as
126 // we don't always know how long it would be, e.g. in chunked encoding.
127 if (me
->Server
->JunkSize
!= 0)
130 if(me
->File
->Write(buffer
, buffer_size
) != true)
133 if(me
->Queue
->MaximumSize
> 0)
135 unsigned long long const TotalWritten
= me
->File
->Tell();
136 if (TotalWritten
> me
->Queue
->MaximumSize
)
138 me
->SetFailReason("MaximumSizeExceeded");
139 _error
->Error("Writing more data than expected (%llu > %llu)",
140 TotalWritten
, me
->Queue
->MaximumSize
);
145 if (me
->Server
->GetHashes()->Add((unsigned char const * const)buffer
, buffer_size
) == false)
151 // HttpsServerState::HttpsServerState - Constructor /*{{{*/
152 HttpsServerState::HttpsServerState(URI Srv
,HttpsMethod
* Owner
) : ServerState(Srv
, Owner
), Hash(NULL
)
154 TimeOut
= Owner
->ConfigFindI("Timeout", TimeOut
);
158 bool HttpsServerState::InitHashes(HashStringList
const &ExpectedHashes
) /*{{{*/
161 Hash
= new Hashes(ExpectedHashes
);
165 APT_PURE Hashes
* HttpsServerState::GetHashes() /*{{{*/
171 bool HttpsMethod::SetupProxy() /*{{{*/
173 URI ServerName
= Queue
->Uri
;
175 // Determine the proxy setting
176 AutoDetectProxy(ServerName
);
178 // Curl should never read proxy settings from the environment, as
179 // we determine which proxy to use. Do this for consistency among
180 // methods and prevent an environment variable overriding a
181 // no-proxy ("DIRECT") setting in apt.conf.
182 curl_easy_setopt(curl
, CURLOPT_PROXY
, "");
184 // Determine the proxy setting - try https first, fallback to http and use env at last
185 string UseProxy
= ConfigFind("Proxy::" + ServerName
.Host
, "");
186 if (UseProxy
.empty() == true)
187 UseProxy
= ConfigFind("Proxy", "");
188 // User wants to use NO proxy, so nothing to setup
189 if (UseProxy
== "DIRECT")
192 // Parse no_proxy, a comma (,) separated list of domains we don't want to use
193 // a proxy for so we stop right here if it is in the list
194 if (getenv("no_proxy") != 0 && CheckDomainList(ServerName
.Host
,getenv("no_proxy")) == true)
197 if (UseProxy
.empty() == true)
199 const char* result
= nullptr;
200 if (std::find(methodNames
.begin(), methodNames
.end(), "https") != methodNames
.end())
201 result
= getenv("https_proxy");
202 // FIXME: Fall back to http_proxy is to remain compatible with
203 // existing setups and behaviour of apt.conf. This should be
204 // deprecated in the future (including apt.conf). Most other
205 // programs do not fall back to http proxy settings and neither
207 if (result
== nullptr && std::find(methodNames
.begin(), methodNames
.end(), "http") != methodNames
.end())
208 result
= getenv("http_proxy");
209 UseProxy
= result
== nullptr ? "" : result
;
212 // Determine what host and port to use based on the proxy settings
213 if (UseProxy
.empty() == false)
216 AddProxyAuth(Proxy
, ServerName
);
218 if (Proxy
.Access
== "socks5h")
219 curl_easy_setopt(curl
, CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS5_HOSTNAME
);
220 else if (Proxy
.Access
== "socks5")
221 curl_easy_setopt(curl
, CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS5
);
222 else if (Proxy
.Access
== "socks4a")
223 curl_easy_setopt(curl
, CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS4A
);
224 else if (Proxy
.Access
== "socks")
225 curl_easy_setopt(curl
, CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS4
);
226 else if (Proxy
.Access
== "http" || Proxy
.Access
== "https")
227 curl_easy_setopt(curl
, CURLOPT_PROXYTYPE
, CURLPROXY_HTTP
);
232 curl_easy_setopt(curl
, CURLOPT_PROXYPORT
, Proxy
.Port
);
233 curl_easy_setopt(curl
, CURLOPT_PROXY
, Proxy
.Host
.c_str());
234 if (Proxy
.User
.empty() == false || Proxy
.Password
.empty() == false)
236 curl_easy_setopt(curl
, CURLOPT_PROXYUSERNAME
, Proxy
.User
.c_str());
237 curl_easy_setopt(curl
, CURLOPT_PROXYPASSWORD
, Proxy
.Password
.c_str());
242 // HttpsMethod::Fetch - Fetch an item /*{{{*/
243 // ---------------------------------------------------------------------
244 /* This adds an item to the pipeline. We keep the pipeline at a fixed
246 bool HttpsMethod::Fetch(FetchItem
*Itm
)
249 struct curl_slist
*headers
=NULL
;
250 char curl_errorstr
[CURL_ERROR_SIZE
];
252 setPostfixForMethodNames(Uri
.Host
.c_str());
253 AllowRedirect
= ConfigFindB("AllowRedirect", true);
254 Debug
= DebugEnabled();
257 // - http::Pipeline-Depth
258 // - error checking/reporting
259 // - more debug options? (CURLOPT_DEBUGFUNCTION?)
261 auto const plus
= Binary
.find('+');
262 if (plus
!= std::string::npos
)
263 Uri
.Access
= Binary
.substr(plus
+ 1);
266 curl_easy_reset(curl
);
267 if (SetupProxy() == false)
268 return _error
->Error("Unsupported proxy configured: %s", URI::SiteOnly(Proxy
).c_str());
270 maybe_add_auth (Uri
, _config
->FindFile("Dir::Etc::netrc"));
272 // The "+" is encoded as a workaround for a amazon S3 bug
273 // see LP bugs #1003633 and #1086997. (taken from http method)
274 Uri
.Path
= QuoteString(Uri
.Path
, "+~ ");
277 CURLUserPointer
userp(this, &Res
, Itm
);
279 curl_easy_setopt(curl
, CURLOPT_URL
, static_cast<string
>(Uri
).c_str());
280 curl_easy_setopt(curl
, CURLOPT_HEADERFUNCTION
, parse_header
);
281 curl_easy_setopt(curl
, CURLOPT_WRITEHEADER
, &userp
);
282 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, write_data
);
283 curl_easy_setopt(curl
, CURLOPT_WRITEDATA
, this);
285 curl_easy_setopt(curl
, CURLOPT_NOPROGRESS
, true);
286 curl_easy_setopt(curl
, CURLOPT_FILETIME
, true);
287 curl_easy_setopt(curl
, CURLOPT_FOLLOWLOCATION
, 0);
289 if (std::find(methodNames
.begin(), methodNames
.end(), "https") != methodNames
.end())
291 curl_easy_setopt(curl
, CURLOPT_PROTOCOLS
, CURLPROTO_HTTPS
);
292 curl_easy_setopt(curl
, CURLOPT_REDIR_PROTOCOLS
, CURLPROTO_HTTPS
);
294 // File containing the list of trusted CA.
295 std::string
const cainfo
= ConfigFind("CaInfo", "");
296 if(cainfo
.empty() == false)
297 curl_easy_setopt(curl
, CURLOPT_CAINFO
, cainfo
.c_str());
298 // Check server certificate against previous CA list ...
299 curl_easy_setopt(curl
, CURLOPT_SSL_VERIFYPEER
, ConfigFindB("Verify-Peer", true) ? 1 : 0);
300 // ... and hostname against cert CN or subjectAltName
301 curl_easy_setopt(curl
, CURLOPT_SSL_VERIFYHOST
, ConfigFindB("Verify-Host", true) ? 2 : 0);
302 // Also enforce issuer of server certificate using its cert
303 std::string
const issuercert
= ConfigFind("IssuerCert", "");
304 if(issuercert
.empty() == false)
305 curl_easy_setopt(curl
, CURLOPT_ISSUERCERT
, issuercert
.c_str());
306 // For client authentication, certificate file ...
307 std::string
const pem
= ConfigFind("SslCert", "");
308 if(pem
.empty() == false)
309 curl_easy_setopt(curl
, CURLOPT_SSLCERT
, pem
.c_str());
310 // ... and associated key.
311 std::string
const key
= ConfigFind("SslKey", "");
312 if(key
.empty() == false)
313 curl_easy_setopt(curl
, CURLOPT_SSLKEY
, key
.c_str());
314 // Allow forcing SSL version to SSLv3 or TLSv1
315 long final_version
= CURL_SSLVERSION_DEFAULT
;
316 std::string
const sslversion
= ConfigFind("SslForceVersion", "");
317 if(sslversion
== "TLSv1")
318 final_version
= CURL_SSLVERSION_TLSv1
;
319 else if(sslversion
== "TLSv1.0")
320 final_version
= CURL_SSLVERSION_TLSv1_0
;
321 else if(sslversion
== "TLSv1.1")
322 final_version
= CURL_SSLVERSION_TLSv1_1
;
323 else if(sslversion
== "TLSv1.2")
324 final_version
= CURL_SSLVERSION_TLSv1_2
;
325 else if(sslversion
== "SSLv3")
326 final_version
= CURL_SSLVERSION_SSLv3
;
327 curl_easy_setopt(curl
, CURLOPT_SSLVERSION
, final_version
);
329 std::string
const crlfile
= ConfigFind("CrlFile", "");
330 if(crlfile
.empty() == false)
331 curl_easy_setopt(curl
, CURLOPT_CRLFILE
, crlfile
.c_str());
335 curl_easy_setopt(curl
, CURLOPT_PROTOCOLS
, CURLPROTO_HTTP
);
336 curl_easy_setopt(curl
, CURLOPT_REDIR_PROTOCOLS
, CURLPROTO_HTTP
);
339 if(ConfigFindB("No-Cache", false) == false)
342 if (ConfigFindB("No-Store", false) == true)
343 headers
= curl_slist_append(headers
,"Cache-Control: no-store");
345 strprintf(ss
, "Cache-Control: max-age=%u", ConfigFindI("Max-Age", 0));
346 headers
= curl_slist_append(headers
, ss
.c_str());
348 // cache disabled by user
349 headers
= curl_slist_append(headers
, "Cache-Control: no-cache");
350 headers
= curl_slist_append(headers
, "Pragma: no-cache");
352 curl_easy_setopt(curl
, CURLOPT_HTTPHEADER
, headers
);
354 int const dlLimit
= ConfigFindI("Dl-Limit", 0) * 1024;
356 curl_easy_setopt(curl
, CURLOPT_MAX_RECV_SPEED_LARGE
, dlLimit
);
359 curl_easy_setopt(curl
, CURLOPT_USERAGENT
, ConfigFind("User-Agent", "Debian APT-CURL/1.0 (" PACKAGE_VERSION
")").c_str());
362 int const timeout
= ConfigFindI("Timeout", 120);
363 curl_easy_setopt(curl
, CURLOPT_CONNECTTIMEOUT
, timeout
);
364 //set really low lowspeed timeout (see #497983)
365 curl_easy_setopt(curl
, CURLOPT_LOW_SPEED_LIMIT
, DL_MIN_SPEED
);
366 curl_easy_setopt(curl
, CURLOPT_LOW_SPEED_TIME
, timeout
);
370 curl_easy_setopt(curl
, CURLOPT_VERBOSE
, true);
373 curl_errorstr
[0] = '\0';
374 curl_easy_setopt(curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
376 // If we ask for uncompressed files servers might respond with content-
377 // negotiation which lets us end up with compressed files we do not support,
378 // see 657029, 657560 and co, so if we have no extension on the request
379 // ask for text only. As a sidenote: If there is nothing to negotate servers
380 // seem to be nice and ignore it.
381 if (ConfigFindB("SendAccept", true))
383 size_t const filepos
= Itm
->Uri
.find_last_of('/');
384 string
const file
= Itm
->Uri
.substr(filepos
+ 1);
385 if (flExtension(file
) == file
)
386 headers
= curl_slist_append(headers
, "Accept: text/*");
389 // go for it - if the file exists, append on it
390 File
= new FileFd(Itm
->DestFile
, FileFd::WriteAny
);
391 if (Server
== nullptr || Server
->Comp(Itm
->Uri
) == false)
392 Server
= CreateServerState(Itm
->Uri
);
394 Server
->Reset(false);
396 // if we have the file send an if-range query with a range header
397 if (Server
->RangesAllowed
&& stat(Itm
->DestFile
.c_str(),&SBuf
) >= 0 && SBuf
.st_size
> 0)
400 strprintf(Buf
, "Range: bytes=%lli-", (long long) SBuf
.st_size
);
401 headers
= curl_slist_append(headers
, Buf
.c_str());
402 strprintf(Buf
, "If-Range: %s", TimeRFC1123(SBuf
.st_mtime
, false).c_str());
403 headers
= curl_slist_append(headers
, Buf
.c_str());
405 else if(Itm
->LastModified
> 0)
407 curl_easy_setopt(curl
, CURLOPT_TIMECONDITION
, CURL_TIMECOND_IFMODSINCE
);
408 curl_easy_setopt(curl
, CURLOPT_TIMEVALUE
, Itm
->LastModified
);
411 if (Server
->InitHashes(Itm
->ExpectedHashes
) == false)
415 Res
.Filename
= Itm
->DestFile
;
418 CURLcode success
= curl_easy_perform(curl
);
420 // If the server returns 200 OK but the If-Modified-Since condition is not
421 // met, CURLINFO_CONDITION_UNMET will be set to 1
422 long curl_condition_unmet
= 0;
423 curl_easy_getinfo(curl
, CURLINFO_CONDITION_UNMET
, &curl_condition_unmet
);
424 if (curl_condition_unmet
== 1)
425 Server
->Result
= 304;
428 curl_slist_free_all(headers
);
431 if (success
!= CURLE_OK
)
433 #pragma GCC diagnostic push
434 #pragma GCC diagnostic ignored "-Wswitch"
437 case CURLE_COULDNT_RESOLVE_PROXY
:
438 case CURLE_COULDNT_RESOLVE_HOST
:
439 SetFailReason("ResolveFailure");
441 case CURLE_COULDNT_CONNECT
:
442 SetFailReason("ConnectionRefused");
444 case CURLE_OPERATION_TIMEDOUT
:
445 SetFailReason("Timeout");
448 #pragma GCC diagnostic pop
449 // only take curls technical errors if we haven't our own
450 // (e.g. for the maximum size limit we have and curls can be confusing)
451 if (_error
->PendingError() == false)
452 _error
->Error("%s", curl_errorstr
);
454 _error
->Warning("curl: %s", curl_errorstr
);
458 switch (DealWithHeaders(Res
))
460 case ServerMethod::IMS_HIT
:
464 case ServerMethod::ERROR_WITH_CONTENT_PAGE
:
465 // unlink, no need keep 401/404 page content in partial/
466 RemoveFile(Binary
.c_str(), File
->Name());
467 case ServerMethod::ERROR_UNRECOVERABLE
:
468 case ServerMethod::ERROR_NOT_FROM_SERVER
:
471 case ServerMethod::TRY_AGAIN_OR_REDIRECT
:
475 case ServerMethod::FILE_IS_OPEN
:
476 struct stat resultStat
;
477 if (unlikely(stat(File
->Name().c_str(), &resultStat
) != 0))
479 _error
->Errno("stat", "Unable to access file %s", File
->Name().c_str());
482 Res
.Size
= resultStat
.st_size
;
485 curl_easy_getinfo(curl
, CURLINFO_FILETIME
, &Res
.LastModified
);
486 if (Res
.LastModified
!= -1)
488 struct timeval times
[2];
489 times
[0].tv_sec
= Res
.LastModified
;
490 times
[1].tv_sec
= Res
.LastModified
;
491 times
[0].tv_usec
= times
[1].tv_usec
= 0;
492 utimes(File
->Name().c_str(), times
);
495 Res
.LastModified
= resultStat
.st_mtime
;
498 Res
.TakeHashes(*(Server
->GetHashes()));
509 std::unique_ptr
<ServerState
> HttpsMethod::CreateServerState(URI
const &uri
)/*{{{*/
511 return std::unique_ptr
<ServerState
>(new HttpsServerState(uri
, this));
514 HttpsMethod::HttpsMethod(std::string
&&pProg
) : ServerMethod(std::move(pProg
),"1.2",Pipeline
| SendConfig
)/*{{{*/
516 auto addName
= std::inserter(methodNames
, methodNames
.begin());
518 auto const plus
= Binary
.find('+');
519 if (plus
!= std::string::npos
)
521 addName
= Binary
.substr(plus
+ 1);
522 auto base
= Binary
.substr(0, plus
);
526 if (std::find(methodNames
.begin(), methodNames
.end(), "https") != methodNames
.end())
527 curl_global_init(CURL_GLOBAL_SSL
);
529 curl_global_init(CURL_GLOBAL_NOTHING
);
530 curl
= curl_easy_init();
533 HttpsMethod::~HttpsMethod() /*{{{*/
535 curl_easy_cleanup(curl
);
538 int main(int, const char *argv
[]) /*{{{*/
540 std::string Binary
= flNotDir(argv
[0]);
541 if (Binary
.find('+') == std::string::npos
&& Binary
!= "https")
542 Binary
.append("+https");
543 return HttpsMethod(std::move(Binary
)).Run();