]> git.saurik.com Git - apt.git/blob - methods/https.cc
7c0c3241df95c8bc1f3b7dfabe346b182fa32af3
[apt.git] / methods / https.cc
1 //-*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: http.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $
4 /* ######################################################################
5
6 HTTPS Acquire Method - This is the HTTPS acquire method for APT.
7
8 It uses libcurl
9
10 ##################################################################### */
11 /*}}}*/
12 // Include Files /*{{{*/
13 #include <config.h>
14
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>
23
24 #include <sys/stat.h>
25 #include <sys/time.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <iostream>
29 #include <sstream>
30 #include <ctype.h>
31 #include <stdlib.h>
32
33 #include "https.h"
34
35 #include <apti18n.h>
36 /*}}}*/
37 using namespace std;
38
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) {}
45 };
46
47 size_t
48 HttpsMethod::parse_header(void *buffer, size_t size, size_t nmemb, void *userp)
49 {
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)
55 {
56 ++len;
57 break;
58 }
59 line.erase(len);
60
61 if (line.empty() == true)
62 {
63 if (me->https->Server->Result != 416 && me->https->Server->StartPos != 0)
64 ;
65 else if (me->https->Server->Result == 416)
66 {
67 bool partialHit = false;
68 if (me->Itm->ExpectedHashes.usable() == true)
69 {
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);
77 }
78 else if (me->https->Server->Result == 416 && me->https->Server->TotalFileSize == me->https->File->FileSize())
79 partialHit = true;
80
81 if (partialHit == true)
82 {
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();
88 }
89 else
90 me->https->Server->StartPos = 0;
91 }
92 else
93 me->https->Server->StartPos = 0;
94
95 me->Res->LastModified = me->https->Server->Date;
96 me->Res->Size = me->https->Server->TotalFileSize;
97 me->Res->ResumePoint = me->https->Server->StartPos;
98
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)
101 {
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)
105 return 0;
106 }
107 }
108 else if (me->https->Server->HeaderLine(line) == false)
109 return 0;
110
111 return size*nmemb;
112 }
113
114 size_t
115 HttpsMethod::write_data(void *buffer, size_t size, size_t nmemb, void *userp)
116 {
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)
122 return buffer_size;
123
124 if(me->File->Write(buffer, buffer_size) != true)
125 return 0;
126
127 if(me->Queue->MaximumSize > 0)
128 {
129 unsigned long long const TotalWritten = me->File->Tell();
130 if (TotalWritten > me->Queue->MaximumSize)
131 {
132 me->SetFailReason("MaximumSizeExceeded");
133 _error->Error("Writing more data than expected (%llu > %llu)",
134 TotalWritten, me->Queue->MaximumSize);
135 return 0;
136 }
137 }
138
139 if (me->Server->GetHashes()->Add((unsigned char const * const)buffer, buffer_size) == false)
140 return 0;
141
142 return buffer_size;
143 }
144
145 // HttpsServerState::HttpsServerState - Constructor /*{{{*/
146 HttpsServerState::HttpsServerState(URI Srv,HttpsMethod * Owner) : ServerState(Srv, Owner), Hash(NULL)
147 {
148 TimeOut = _config->FindI("Acquire::https::Timeout",TimeOut);
149 Reset();
150 }
151 /*}}}*/
152 bool HttpsServerState::InitHashes(HashStringList const &ExpectedHashes) /*{{{*/
153 {
154 delete Hash;
155 Hash = new Hashes(ExpectedHashes);
156 return true;
157 }
158 /*}}}*/
159 APT_PURE Hashes * HttpsServerState::GetHashes() /*{{{*/
160 {
161 return Hash;
162 }
163 /*}}}*/
164
165 bool HttpsMethod::SetupProxy() /*{{{*/
166 {
167 URI ServerName = Queue->Uri;
168
169 // Determine the proxy setting
170 AutoDetectProxy(ServerName);
171
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, "");
177
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());
181
182 if (UseProxy.empty() == true)
183 UseProxy = _config->Find("Acquire::https::Proxy", _config->Find("Acquire::http::Proxy").c_str());
184
185 // User want to use NO proxy, so nothing to setup
186 if (UseProxy == "DIRECT")
187 return true;
188
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)
192 return true;
193
194 if (UseProxy.empty() == true)
195 {
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
201 // should Apt.
202 if (result == NULL)
203 result = getenv("http_proxy");
204 UseProxy = result == NULL ? "" : result;
205 }
206
207 // Determine what host and port to use based on the proxy settings
208 if (UseProxy.empty() == false)
209 {
210 Proxy = UseProxy;
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);
219 else if (Proxy.Access == "http" || Proxy.Access == "https")
220 curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
221 else
222 return false;
223
224 if (Proxy.Port != 1)
225 curl_easy_setopt(curl, CURLOPT_PROXYPORT, Proxy.Port);
226 curl_easy_setopt(curl, CURLOPT_PROXY, Proxy.Host.c_str());
227 if (Proxy.User.empty() == false || Proxy.Password.empty() == false)
228 {
229 curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME, Proxy.User.c_str());
230 curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD, Proxy.Password.c_str());
231 }
232 }
233 return true;
234 } /*}}}*/
235 // HttpsMethod::Fetch - Fetch an item /*{{{*/
236 // ---------------------------------------------------------------------
237 /* This adds an item to the pipeline. We keep the pipeline at a fixed
238 depth. */
239 bool HttpsMethod::Fetch(FetchItem *Itm)
240 {
241 struct stat SBuf;
242 struct curl_slist *headers=NULL;
243 char curl_errorstr[CURL_ERROR_SIZE];
244 URI Uri = Itm->Uri;
245 string remotehost = Uri.Host;
246
247 // TODO:
248 // - http::Pipeline-Depth
249 // - error checking/reporting
250 // - more debug options? (CURLOPT_DEBUGFUNCTION?)
251
252 curl_easy_reset(curl);
253 if (SetupProxy() == false)
254 return _error->Error("Unsupported proxy configured: %s", URI::SiteOnly(Proxy).c_str());
255
256 maybe_add_auth (Uri, _config->FindFile("Dir::Etc::netrc"));
257
258 FetchResult Res;
259 CURLUserPointer userp(this, &Res, Itm);
260 // callbacks
261 curl_easy_setopt(curl, CURLOPT_URL, static_cast<string>(Uri).c_str());
262 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, parse_header);
263 curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &userp);
264 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
265 curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
266 // options
267 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, true);
268 curl_easy_setopt(curl, CURLOPT_FILETIME, true);
269 // only allow curl to handle https, not the other stuff it supports
270 curl_easy_setopt(curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
271 curl_easy_setopt(curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
272
273 // SSL parameters are set by default to the common (non mirror-specific) value
274 // if available (or a default one) and gets overload by mirror-specific ones.
275
276 // File containing the list of trusted CA.
277 string cainfo = _config->Find("Acquire::https::CaInfo","");
278 string knob = "Acquire::https::"+remotehost+"::CaInfo";
279 cainfo = _config->Find(knob.c_str(),cainfo.c_str());
280 if(cainfo.empty() == false)
281 curl_easy_setopt(curl, CURLOPT_CAINFO,cainfo.c_str());
282
283 // Check server certificate against previous CA list ...
284 bool peer_verify = _config->FindB("Acquire::https::Verify-Peer",true);
285 knob = "Acquire::https::" + remotehost + "::Verify-Peer";
286 peer_verify = _config->FindB(knob.c_str(), peer_verify);
287 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, peer_verify);
288
289 // ... and hostname against cert CN or subjectAltName
290 bool verify = _config->FindB("Acquire::https::Verify-Host",true);
291 knob = "Acquire::https::"+remotehost+"::Verify-Host";
292 verify = _config->FindB(knob.c_str(),verify);
293 int const default_verify = (verify == true) ? 2 : 0;
294 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, default_verify);
295
296 // Also enforce issuer of server certificate using its cert
297 string issuercert = _config->Find("Acquire::https::IssuerCert","");
298 knob = "Acquire::https::"+remotehost+"::IssuerCert";
299 issuercert = _config->Find(knob.c_str(),issuercert.c_str());
300 if(issuercert.empty() == false)
301 curl_easy_setopt(curl, CURLOPT_ISSUERCERT,issuercert.c_str());
302
303 // For client authentication, certificate file ...
304 string pem = _config->Find("Acquire::https::SslCert","");
305 knob = "Acquire::https::"+remotehost+"::SslCert";
306 pem = _config->Find(knob.c_str(),pem.c_str());
307 if(pem.empty() == false)
308 curl_easy_setopt(curl, CURLOPT_SSLCERT, pem.c_str());
309
310 // ... and associated key.
311 string key = _config->Find("Acquire::https::SslKey","");
312 knob = "Acquire::https::"+remotehost+"::SslKey";
313 key = _config->Find(knob.c_str(),key.c_str());
314 if(key.empty() == false)
315 curl_easy_setopt(curl, CURLOPT_SSLKEY, key.c_str());
316
317 // Allow forcing SSL version to SSLv3 or TLSv1 (SSLv2 is not
318 // supported by GnuTLS).
319 long final_version = CURL_SSLVERSION_DEFAULT;
320 string sslversion = _config->Find("Acquire::https::SslForceVersion","");
321 knob = "Acquire::https::"+remotehost+"::SslForceVersion";
322 sslversion = _config->Find(knob.c_str(),sslversion.c_str());
323 if(sslversion == "TLSv1")
324 final_version = CURL_SSLVERSION_TLSv1;
325 else if(sslversion == "SSLv3")
326 final_version = CURL_SSLVERSION_SSLv3;
327 curl_easy_setopt(curl, CURLOPT_SSLVERSION, final_version);
328
329 // CRL file
330 string crlfile = _config->Find("Acquire::https::CrlFile","");
331 knob = "Acquire::https::"+remotehost+"::CrlFile";
332 crlfile = _config->Find(knob.c_str(),crlfile.c_str());
333 if(crlfile.empty() == false)
334 curl_easy_setopt(curl, CURLOPT_CRLFILE, crlfile.c_str());
335
336 // cache-control
337 if(_config->FindB("Acquire::https::No-Cache",
338 _config->FindB("Acquire::http::No-Cache",false)) == false)
339 {
340 // cache enabled
341 if (_config->FindB("Acquire::https::No-Store",
342 _config->FindB("Acquire::http::No-Store",false)) == true)
343 headers = curl_slist_append(headers,"Cache-Control: no-store");
344 stringstream ss;
345 ioprintf(ss, "Cache-Control: max-age=%u", _config->FindI("Acquire::https::Max-Age",
346 _config->FindI("Acquire::http::Max-Age",0)));
347 headers = curl_slist_append(headers, ss.str().c_str());
348 } else {
349 // cache disabled by user
350 headers = curl_slist_append(headers, "Cache-Control: no-cache");
351 headers = curl_slist_append(headers, "Pragma: no-cache");
352 }
353 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
354
355 // speed limit
356 int const dlLimit = _config->FindI("Acquire::https::Dl-Limit",
357 _config->FindI("Acquire::http::Dl-Limit",0))*1024;
358 if (dlLimit > 0)
359 curl_easy_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE, dlLimit);
360
361 // set header
362 curl_easy_setopt(curl, CURLOPT_USERAGENT,
363 _config->Find("Acquire::https::User-Agent",
364 _config->Find("Acquire::http::User-Agent",
365 "Debian APT-CURL/1.0 (" PACKAGE_VERSION ")").c_str()).c_str());
366
367 // set timeout
368 int const timeout = _config->FindI("Acquire::https::Timeout",
369 _config->FindI("Acquire::http::Timeout",120));
370 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout);
371 //set really low lowspeed timeout (see #497983)
372 curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, DL_MIN_SPEED);
373 curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, timeout);
374
375 // set redirect options and default to 10 redirects
376 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, AllowRedirect);
377 curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 10);
378
379 // debug
380 if (Debug == true)
381 curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
382
383 // error handling
384 curl_errorstr[0] = '\0';
385 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
386
387 // If we ask for uncompressed files servers might respond with content-
388 // negotiation which lets us end up with compressed files we do not support,
389 // see 657029, 657560 and co, so if we have no extension on the request
390 // ask for text only. As a sidenote: If there is nothing to negotate servers
391 // seem to be nice and ignore it.
392 if (_config->FindB("Acquire::https::SendAccept", _config->FindB("Acquire::http::SendAccept", true)) == true)
393 {
394 size_t const filepos = Itm->Uri.find_last_of('/');
395 string const file = Itm->Uri.substr(filepos + 1);
396 if (flExtension(file) == file)
397 headers = curl_slist_append(headers, "Accept: text/*");
398 }
399
400 // if we have the file send an if-range query with a range header
401 if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
402 {
403 std::string Buf;
404 strprintf(Buf, "Range: bytes=%lli-", (long long) SBuf.st_size);
405 headers = curl_slist_append(headers, Buf.c_str());
406 strprintf(Buf, "If-Range: %s", TimeRFC1123(SBuf.st_mtime, false).c_str());
407 headers = curl_slist_append(headers, Buf.c_str());
408 }
409 else if(Itm->LastModified > 0)
410 {
411 curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
412 curl_easy_setopt(curl, CURLOPT_TIMEVALUE, Itm->LastModified);
413 }
414
415 // go for it - if the file exists, append on it
416 File = new FileFd(Itm->DestFile, FileFd::WriteAny);
417 Server = CreateServerState(Itm->Uri);
418 if (Server->InitHashes(Itm->ExpectedHashes) == false)
419 return false;
420
421 // keep apt updated
422 Res.Filename = Itm->DestFile;
423
424 // get it!
425 CURLcode success = curl_easy_perform(curl);
426
427 // If the server returns 200 OK but the If-Modified-Since condition is not
428 // met, CURLINFO_CONDITION_UNMET will be set to 1
429 long curl_condition_unmet = 0;
430 curl_easy_getinfo(curl, CURLINFO_CONDITION_UNMET, &curl_condition_unmet);
431
432 File->Close();
433 curl_slist_free_all(headers);
434
435 // cleanup
436 if (success != CURLE_OK)
437 {
438 #pragma GCC diagnostic push
439 #pragma GCC diagnostic ignored "-Wswitch"
440 switch (success)
441 {
442 case CURLE_COULDNT_RESOLVE_PROXY:
443 case CURLE_COULDNT_RESOLVE_HOST:
444 SetFailReason("ResolveFailure");
445 break;
446 case CURLE_COULDNT_CONNECT:
447 SetFailReason("ConnectionRefused");
448 break;
449 case CURLE_OPERATION_TIMEDOUT:
450 SetFailReason("Timeout");
451 break;
452 }
453 #pragma GCC diagnostic pop
454 // only take curls technical errors if we haven't our own
455 // (e.g. for the maximum size limit we have and curls can be confusing)
456 if (_error->PendingError() == false)
457 _error->Error("%s", curl_errorstr);
458 else
459 _error->Warning("curl: %s", curl_errorstr);
460 return false;
461 }
462
463 // server says file not modified
464 if (Server->Result == 304 || curl_condition_unmet == 1)
465 {
466 RemoveFile("https", File->Name());
467 Res.IMSHit = true;
468 Res.LastModified = Itm->LastModified;
469 Res.Size = 0;
470 URIDone(Res);
471 return true;
472 }
473 Res.IMSHit = false;
474
475 if (Server->Result != 200 && // OK
476 Server->Result != 206 && // Partial
477 Server->Result != 416) // invalid Range
478 {
479 char err[255];
480 snprintf(err, sizeof(err) - 1, "HttpError%i", Server->Result);
481 SetFailReason(err);
482 _error->Error("%i %s", Server->Result, Server->Code);
483 // unlink, no need keep 401/404 page content in partial/
484 RemoveFile("https", File->Name());
485 return false;
486 }
487
488 // invalid range-request
489 if (Server->Result == 416)
490 {
491 RemoveFile("https", File->Name());
492 delete File;
493 Redirect(Itm->Uri);
494 return true;
495 }
496
497 struct stat resultStat;
498 if (unlikely(stat(File->Name().c_str(), &resultStat) != 0))
499 {
500 _error->Errno("stat", "Unable to access file %s", File->Name().c_str());
501 return false;
502 }
503 Res.Size = resultStat.st_size;
504
505 // Timestamp
506 curl_easy_getinfo(curl, CURLINFO_FILETIME, &Res.LastModified);
507 if (Res.LastModified != -1)
508 {
509 struct timeval times[2];
510 times[0].tv_sec = Res.LastModified;
511 times[1].tv_sec = Res.LastModified;
512 times[0].tv_usec = times[1].tv_usec = 0;
513 utimes(File->Name().c_str(), times);
514 }
515 else
516 Res.LastModified = resultStat.st_mtime;
517
518 // take hashes
519 Res.TakeHashes(*(Server->GetHashes()));
520
521 // keep apt updated
522 URIDone(Res);
523
524 // cleanup
525 delete File;
526
527 return true;
528 }
529 /*}}}*/
530 // HttpsMethod::Configuration - Handle a configuration message /*{{{*/
531 bool HttpsMethod::Configuration(string Message)
532 {
533 if (ServerMethod::Configuration(Message) == false)
534 return false;
535
536 AllowRedirect = _config->FindB("Acquire::https::AllowRedirect",
537 _config->FindB("Acquire::http::AllowRedirect", true));
538 Debug = _config->FindB("Debug::Acquire::https",false);
539
540 return true;
541 }
542 /*}}}*/
543 std::unique_ptr<ServerState> HttpsMethod::CreateServerState(URI const &uri)/*{{{*/
544 {
545 return std::unique_ptr<ServerState>(new HttpsServerState(uri, this));
546 }
547 /*}}}*/
548
549 int main()
550 {
551 return HttpsMethod().Run();
552 }
553