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>
39 struct APT_HIDDEN CURLUserPointer
{
40 HttpsMethod
* const https
;
41 HttpsMethod::FetchResult
* const Res
;
42 HttpsMethod::FetchItem
const * const Itm
;
43 CURLUserPointer(HttpsMethod
* const https
, HttpsMethod::FetchResult
* const Res
,
44 HttpsMethod::FetchItem
const * const Itm
) : https(https
), Res(Res
), Itm(Itm
) {}
48 HttpsMethod::parse_header(void *buffer
, size_t size
, size_t nmemb
, void *userp
)
50 size_t len
= size
* nmemb
;
51 CURLUserPointer
*me
= static_cast<CURLUserPointer
*>(userp
);
52 std::string
line((char*) buffer
, len
);
53 for (--len
; len
> 0; --len
)
54 if (isspace_ascii(line
[len
]) == 0)
61 if (line
.empty() == true)
63 if (me
->https
->Server
->Result
!= 416 && me
->https
->Server
->StartPos
!= 0)
65 else if (me
->https
->Server
->Result
== 416)
67 bool partialHit
= false;
68 if (me
->Itm
->ExpectedHashes
.usable() == true)
70 Hashes
resultHashes(me
->Itm
->ExpectedHashes
);
71 FileFd
file(me
->Itm
->DestFile
, FileFd::ReadOnly
);
72 me
->https
->Server
->TotalFileSize
= file
.FileSize();
73 me
->https
->Server
->Date
= file
.ModificationTime();
74 resultHashes
.AddFD(file
);
75 HashStringList
const hashList
= resultHashes
.GetHashStringList();
76 partialHit
= (me
->Itm
->ExpectedHashes
== hashList
);
78 else if (me
->https
->Server
->Result
== 416 && me
->https
->Server
->TotalFileSize
== me
->https
->File
->FileSize())
81 if (partialHit
== true)
83 me
->https
->Server
->Result
= 200;
84 me
->https
->Server
->StartPos
= me
->https
->Server
->TotalFileSize
;
85 // the actual size is not important for https as curl will deal with it
86 // by itself and e.g. doesn't bother us with transport-encoding…
87 me
->https
->Server
->JunkSize
= std::numeric_limits
<unsigned long long>::max();
90 me
->https
->Server
->StartPos
= 0;
93 me
->https
->Server
->StartPos
= 0;
95 me
->Res
->LastModified
= me
->https
->Server
->Date
;
96 me
->Res
->Size
= me
->https
->Server
->TotalFileSize
;
97 me
->Res
->ResumePoint
= me
->https
->Server
->StartPos
;
99 // we expect valid data, so tell our caller we get the file now
100 if (me
->https
->Server
->Result
>= 200 && me
->https
->Server
->Result
< 300)
102 if (me
->https
->Server
->JunkSize
== 0 && me
->Res
->Size
!= 0 && me
->Res
->Size
> me
->Res
->ResumePoint
)
103 me
->https
->URIStart(*me
->Res
);
104 if (me
->https
->Server
->AddPartialFileToHashes(*(me
->https
->File
)) == false)
108 else if (me
->https
->Server
->HeaderLine(line
) == false)
115 HttpsMethod::write_data(void *buffer
, size_t size
, size_t nmemb
, void *userp
)
117 HttpsMethod
*me
= static_cast<HttpsMethod
*>(userp
);
118 size_t buffer_size
= size
* nmemb
;
119 // we don't need to count the junk here, just drop anything we get as
120 // we don't always know how long it would be, e.g. in chunked encoding.
121 if (me
->Server
->JunkSize
!= 0)
124 if(me
->File
->Write(buffer
, buffer_size
) != true)
127 if(me
->Queue
->MaximumSize
> 0)
129 unsigned long long const TotalWritten
= me
->File
->Tell();
130 if (TotalWritten
> me
->Queue
->MaximumSize
)
132 me
->SetFailReason("MaximumSizeExceeded");
133 _error
->Error("Writing more data than expected (%llu > %llu)",
134 TotalWritten
, me
->Queue
->MaximumSize
);
139 if (me
->Server
->GetHashes()->Add((unsigned char const * const)buffer
, buffer_size
) == false)
145 // HttpsServerState::HttpsServerState - Constructor /*{{{*/
146 HttpsServerState::HttpsServerState(URI Srv
,HttpsMethod
* Owner
) : ServerState(Srv
, Owner
), Hash(NULL
)
148 TimeOut
= _config
->FindI("Acquire::https::Timeout",TimeOut
);
152 bool HttpsServerState::InitHashes(HashStringList
const &ExpectedHashes
) /*{{{*/
155 Hash
= new Hashes(ExpectedHashes
);
159 APT_PURE Hashes
* HttpsServerState::GetHashes() /*{{{*/
165 void HttpsMethod::SetupProxy() /*{{{*/
167 URI ServerName
= Queue
->Uri
;
169 // Determine the proxy setting
170 AutoDetectProxy(ServerName
);
172 // Curl should never read proxy settings from the environment, as
173 // we determine which proxy to use. Do this for consistency among
174 // methods and prevent an environment variable overriding a
175 // no-proxy ("DIRECT") setting in apt.conf.
176 curl_easy_setopt(curl
, CURLOPT_PROXY
, "");
178 // Determine the proxy setting - try https first, fallback to http and use env at last
179 string UseProxy
= _config
->Find("Acquire::https::Proxy::" + ServerName
.Host
,
180 _config
->Find("Acquire::http::Proxy::" + ServerName
.Host
).c_str());
182 if (UseProxy
.empty() == true)
183 UseProxy
= _config
->Find("Acquire::https::Proxy", _config
->Find("Acquire::http::Proxy").c_str());
185 // User want to use NO proxy, so nothing to setup
186 if (UseProxy
== "DIRECT")
189 // Parse no_proxy, a comma (,) separated list of domains we don't want to use
190 // a proxy for so we stop right here if it is in the list
191 if (getenv("no_proxy") != 0 && CheckDomainList(ServerName
.Host
,getenv("no_proxy")) == true)
194 if (UseProxy
.empty() == true)
196 const char* result
= getenv("https_proxy");
197 // FIXME: Fall back to http_proxy is to remain compatible with
198 // existing setups and behaviour of apt.conf. This should be
199 // deprecated in the future (including apt.conf). Most other
200 // programs do not fall back to http proxy settings and neither
203 result
= getenv("http_proxy");
204 UseProxy
= result
== NULL
? "" : result
;
207 // Determine what host and port to use based on the proxy settings
208 if (UseProxy
.empty() == false)
211 if (Proxy
.Access
== "socks5h")
212 curl_easy_setopt(curl
, CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS5_HOSTNAME
);
213 else if (Proxy
.Access
== "socks5")
214 curl_easy_setopt(curl
, CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS5
);
215 else if (Proxy
.Access
== "socks4a")
216 curl_easy_setopt(curl
, CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS4A
);
217 else if (Proxy
.Access
== "socks")
218 curl_easy_setopt(curl
, CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS4
);
220 curl_easy_setopt(curl
, CURLOPT_PROXYTYPE
, CURLPROXY_HTTP
);
223 curl_easy_setopt(curl
, CURLOPT_PROXYPORT
, Proxy
.Port
);
224 curl_easy_setopt(curl
, CURLOPT_PROXY
, Proxy
.Host
.c_str());
225 if (Proxy
.User
.empty() == false || Proxy
.Password
.empty() == false)
227 curl_easy_setopt(curl
, CURLOPT_PROXYUSERNAME
, Proxy
.User
.c_str());
228 curl_easy_setopt(curl
, CURLOPT_PROXYPASSWORD
, Proxy
.Password
.c_str());
232 // HttpsMethod::Fetch - Fetch an item /*{{{*/
233 // ---------------------------------------------------------------------
234 /* This adds an item to the pipeline. We keep the pipeline at a fixed
236 bool HttpsMethod::Fetch(FetchItem
*Itm
)
239 struct curl_slist
*headers
=NULL
;
240 char curl_errorstr
[CURL_ERROR_SIZE
];
242 string remotehost
= Uri
.Host
;
245 // - http::Pipeline-Depth
246 // - error checking/reporting
247 // - more debug options? (CURLOPT_DEBUGFUNCTION?)
249 curl_easy_reset(curl
);
252 maybe_add_auth (Uri
, _config
->FindFile("Dir::Etc::netrc"));
255 CURLUserPointer
userp(this, &Res
, Itm
);
257 curl_easy_setopt(curl
, CURLOPT_URL
, static_cast<string
>(Uri
).c_str());
258 curl_easy_setopt(curl
, CURLOPT_HEADERFUNCTION
, parse_header
);
259 curl_easy_setopt(curl
, CURLOPT_WRITEHEADER
, &userp
);
260 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, write_data
);
261 curl_easy_setopt(curl
, CURLOPT_WRITEDATA
, this);
263 curl_easy_setopt(curl
, CURLOPT_NOPROGRESS
, true);
264 curl_easy_setopt(curl
, CURLOPT_FILETIME
, true);
265 // only allow curl to handle https, not the other stuff it supports
266 curl_easy_setopt(curl
, CURLOPT_PROTOCOLS
, CURLPROTO_HTTPS
);
267 curl_easy_setopt(curl
, CURLOPT_REDIR_PROTOCOLS
, CURLPROTO_HTTPS
);
269 // SSL parameters are set by default to the common (non mirror-specific) value
270 // if available (or a default one) and gets overload by mirror-specific ones.
272 // File containing the list of trusted CA.
273 string cainfo
= _config
->Find("Acquire::https::CaInfo","");
274 string knob
= "Acquire::https::"+remotehost
+"::CaInfo";
275 cainfo
= _config
->Find(knob
.c_str(),cainfo
.c_str());
276 if(cainfo
.empty() == false)
277 curl_easy_setopt(curl
, CURLOPT_CAINFO
,cainfo
.c_str());
279 // Check server certificate against previous CA list ...
280 bool peer_verify
= _config
->FindB("Acquire::https::Verify-Peer",true);
281 knob
= "Acquire::https::" + remotehost
+ "::Verify-Peer";
282 peer_verify
= _config
->FindB(knob
.c_str(), peer_verify
);
283 curl_easy_setopt(curl
, CURLOPT_SSL_VERIFYPEER
, peer_verify
);
285 // ... and hostname against cert CN or subjectAltName
286 bool verify
= _config
->FindB("Acquire::https::Verify-Host",true);
287 knob
= "Acquire::https::"+remotehost
+"::Verify-Host";
288 verify
= _config
->FindB(knob
.c_str(),verify
);
289 int const default_verify
= (verify
== true) ? 2 : 0;
290 curl_easy_setopt(curl
, CURLOPT_SSL_VERIFYHOST
, default_verify
);
292 // Also enforce issuer of server certificate using its cert
293 string issuercert
= _config
->Find("Acquire::https::IssuerCert","");
294 knob
= "Acquire::https::"+remotehost
+"::IssuerCert";
295 issuercert
= _config
->Find(knob
.c_str(),issuercert
.c_str());
296 if(issuercert
.empty() == false)
297 curl_easy_setopt(curl
, CURLOPT_ISSUERCERT
,issuercert
.c_str());
299 // For client authentication, certificate file ...
300 string pem
= _config
->Find("Acquire::https::SslCert","");
301 knob
= "Acquire::https::"+remotehost
+"::SslCert";
302 pem
= _config
->Find(knob
.c_str(),pem
.c_str());
303 if(pem
.empty() == false)
304 curl_easy_setopt(curl
, CURLOPT_SSLCERT
, pem
.c_str());
306 // ... and associated key.
307 string key
= _config
->Find("Acquire::https::SslKey","");
308 knob
= "Acquire::https::"+remotehost
+"::SslKey";
309 key
= _config
->Find(knob
.c_str(),key
.c_str());
310 if(key
.empty() == false)
311 curl_easy_setopt(curl
, CURLOPT_SSLKEY
, key
.c_str());
313 // Allow forcing SSL version to SSLv3 or TLSv1 (SSLv2 is not
314 // supported by GnuTLS).
315 long final_version
= CURL_SSLVERSION_DEFAULT
;
316 string sslversion
= _config
->Find("Acquire::https::SslForceVersion","");
317 knob
= "Acquire::https::"+remotehost
+"::SslForceVersion";
318 sslversion
= _config
->Find(knob
.c_str(),sslversion
.c_str());
319 if(sslversion
== "TLSv1")
320 final_version
= CURL_SSLVERSION_TLSv1
;
321 else if(sslversion
== "SSLv3")
322 final_version
= CURL_SSLVERSION_SSLv3
;
323 curl_easy_setopt(curl
, CURLOPT_SSLVERSION
, final_version
);
326 string crlfile
= _config
->Find("Acquire::https::CrlFile","");
327 knob
= "Acquire::https::"+remotehost
+"::CrlFile";
328 crlfile
= _config
->Find(knob
.c_str(),crlfile
.c_str());
329 if(crlfile
.empty() == false)
330 curl_easy_setopt(curl
, CURLOPT_CRLFILE
, crlfile
.c_str());
333 if(_config
->FindB("Acquire::https::No-Cache",
334 _config
->FindB("Acquire::http::No-Cache",false)) == false)
337 if (_config
->FindB("Acquire::https::No-Store",
338 _config
->FindB("Acquire::http::No-Store",false)) == true)
339 headers
= curl_slist_append(headers
,"Cache-Control: no-store");
341 ioprintf(ss
, "Cache-Control: max-age=%u", _config
->FindI("Acquire::https::Max-Age",
342 _config
->FindI("Acquire::http::Max-Age",0)));
343 headers
= curl_slist_append(headers
, ss
.str().c_str());
345 // cache disabled by user
346 headers
= curl_slist_append(headers
, "Cache-Control: no-cache");
347 headers
= curl_slist_append(headers
, "Pragma: no-cache");
349 curl_easy_setopt(curl
, CURLOPT_HTTPHEADER
, headers
);
352 int const dlLimit
= _config
->FindI("Acquire::https::Dl-Limit",
353 _config
->FindI("Acquire::http::Dl-Limit",0))*1024;
355 curl_easy_setopt(curl
, CURLOPT_MAX_RECV_SPEED_LARGE
, dlLimit
);
358 curl_easy_setopt(curl
, CURLOPT_USERAGENT
,
359 _config
->Find("Acquire::https::User-Agent",
360 _config
->Find("Acquire::http::User-Agent",
361 "Debian APT-CURL/1.0 (" PACKAGE_VERSION
")").c_str()).c_str());
364 int const timeout
= _config
->FindI("Acquire::https::Timeout",
365 _config
->FindI("Acquire::http::Timeout",120));
366 curl_easy_setopt(curl
, CURLOPT_CONNECTTIMEOUT
, timeout
);
367 //set really low lowspeed timeout (see #497983)
368 curl_easy_setopt(curl
, CURLOPT_LOW_SPEED_LIMIT
, DL_MIN_SPEED
);
369 curl_easy_setopt(curl
, CURLOPT_LOW_SPEED_TIME
, timeout
);
371 // set redirect options and default to 10 redirects
372 curl_easy_setopt(curl
, CURLOPT_FOLLOWLOCATION
, AllowRedirect
);
373 curl_easy_setopt(curl
, CURLOPT_MAXREDIRS
, 10);
377 curl_easy_setopt(curl
, CURLOPT_VERBOSE
, true);
380 curl_errorstr
[0] = '\0';
381 curl_easy_setopt(curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
383 // If we ask for uncompressed files servers might respond with content-
384 // negotiation which lets us end up with compressed files we do not support,
385 // see 657029, 657560 and co, so if we have no extension on the request
386 // ask for text only. As a sidenote: If there is nothing to negotate servers
387 // seem to be nice and ignore it.
388 if (_config
->FindB("Acquire::https::SendAccept", _config
->FindB("Acquire::http::SendAccept", true)) == true)
390 size_t const filepos
= Itm
->Uri
.find_last_of('/');
391 string
const file
= Itm
->Uri
.substr(filepos
+ 1);
392 if (flExtension(file
) == file
)
393 headers
= curl_slist_append(headers
, "Accept: text/*");
396 // if we have the file send an if-range query with a range header
397 if (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 // go for it - if the file exists, append on it
412 File
= new FileFd(Itm
->DestFile
, FileFd::WriteAny
);
413 Server
= CreateServerState(Itm
->Uri
);
414 if (Server
->InitHashes(Itm
->ExpectedHashes
) == false)
418 Res
.Filename
= Itm
->DestFile
;
421 CURLcode success
= curl_easy_perform(curl
);
423 // If the server returns 200 OK but the If-Modified-Since condition is not
424 // met, CURLINFO_CONDITION_UNMET will be set to 1
425 long curl_condition_unmet
= 0;
426 curl_easy_getinfo(curl
, CURLINFO_CONDITION_UNMET
, &curl_condition_unmet
);
429 curl_slist_free_all(headers
);
432 if (success
!= CURLE_OK
)
434 #pragma GCC diagnostic push
435 #pragma GCC diagnostic ignored "-Wswitch"
438 case CURLE_COULDNT_RESOLVE_PROXY
:
439 case CURLE_COULDNT_RESOLVE_HOST
:
440 SetFailReason("ResolveFailure");
442 case CURLE_COULDNT_CONNECT
:
443 SetFailReason("ConnectionRefused");
445 case CURLE_OPERATION_TIMEDOUT
:
446 SetFailReason("Timeout");
449 #pragma GCC diagnostic pop
450 // only take curls technical errors if we haven't our own
451 // (e.g. for the maximum size limit we have and curls can be confusing)
452 if (_error
->PendingError() == false)
453 _error
->Error("%s", curl_errorstr
);
455 _error
->Warning("curl: %s", curl_errorstr
);
459 // server says file not modified
460 if (Server
->Result
== 304 || curl_condition_unmet
== 1)
462 RemoveFile("https", File
->Name());
464 Res
.LastModified
= Itm
->LastModified
;
471 if (Server
->Result
!= 200 && // OK
472 Server
->Result
!= 206 && // Partial
473 Server
->Result
!= 416) // invalid Range
476 snprintf(err
, sizeof(err
) - 1, "HttpError%i", Server
->Result
);
478 _error
->Error("%i %s", Server
->Result
, Server
->Code
);
479 // unlink, no need keep 401/404 page content in partial/
480 RemoveFile("https", File
->Name());
484 // invalid range-request
485 if (Server
->Result
== 416)
487 RemoveFile("https", File
->Name());
493 struct stat resultStat
;
494 if (unlikely(stat(File
->Name().c_str(), &resultStat
) != 0))
496 _error
->Errno("stat", "Unable to access file %s", File
->Name().c_str());
499 Res
.Size
= resultStat
.st_size
;
502 curl_easy_getinfo(curl
, CURLINFO_FILETIME
, &Res
.LastModified
);
503 if (Res
.LastModified
!= -1)
505 struct timeval times
[2];
506 times
[0].tv_sec
= Res
.LastModified
;
507 times
[1].tv_sec
= Res
.LastModified
;
508 times
[0].tv_usec
= times
[1].tv_usec
= 0;
509 utimes(File
->Name().c_str(), times
);
512 Res
.LastModified
= resultStat
.st_mtime
;
515 Res
.TakeHashes(*(Server
->GetHashes()));
526 // HttpsMethod::Configuration - Handle a configuration message /*{{{*/
527 bool HttpsMethod::Configuration(string Message
)
529 if (ServerMethod::Configuration(Message
) == false)
532 AllowRedirect
= _config
->FindB("Acquire::https::AllowRedirect",
533 _config
->FindB("Acquire::http::AllowRedirect", true));
534 Debug
= _config
->FindB("Debug::Acquire::https",false);
539 std::unique_ptr
<ServerState
> HttpsMethod::CreateServerState(URI
const &uri
)/*{{{*/
541 return std::unique_ptr
<ServerState
>(new HttpsServerState(uri
, this));
547 return HttpsMethod().Run();