]> git.saurik.com Git - apt.git/blame - methods/https.cc
HttpsMethod::Fetch(): Zero the FetchResult object when leaving due to 404
[apt.git] / methods / https.cc
CommitLineData
b9e9a44b 1//-*- mode: cpp; mode: fold -*-
d546f98d
MV
2// Description /*{{{*/
3// $Id: http.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $
4/* ######################################################################
5
1e3f4083 6 HTTPS Acquire Method - This is the HTTPS acquire method for APT.
d546f98d
MV
7
8 It uses libcurl
9
10 ##################################################################### */
11 /*}}}*/
12// Include Files /*{{{*/
ea542140
DK
13#include <config.h>
14
d546f98d
MV
15#include <apt-pkg/fileutl.h>
16#include <apt-pkg/acquire-method.h>
17#include <apt-pkg/error.h>
18#include <apt-pkg/hashes.h>
592b7800 19#include <apt-pkg/netrc.h>
472ff00e 20#include <apt-pkg/configuration.h>
453b82a3
DK
21#include <apt-pkg/macros.h>
22#include <apt-pkg/strutl.h>
c6ee61ea 23#include <apt-pkg/proxy.h>
d546f98d
MV
24
25#include <sys/stat.h>
26#include <sys/time.h>
d546f98d 27#include <unistd.h>
d546f98d 28#include <stdio.h>
d546f98d 29#include <iostream>
d546f98d 30#include <sstream>
453b82a3
DK
31#include <ctype.h>
32#include <stdlib.h>
d546f98d 33
d546f98d 34#include "https.h"
453b82a3 35
ea542140 36#include <apti18n.h>
d546f98d
MV
37 /*}}}*/
38using namespace std;
39
fd46d305
DK
40size_t
41HttpsMethod::parse_header(void *buffer, size_t size, size_t nmemb, void *userp)
42{
43 size_t len = size * nmemb;
44 HttpsMethod *me = (HttpsMethod *)userp;
45 std::string line((char*) buffer, len);
46 for (--len; len > 0; --len)
47 if (isspace(line[len]) == 0)
48 {
49 ++len;
50 break;
51 }
52 line.erase(len);
53
54 if (line.empty() == true)
55 {
56 if (me->Server->Result != 416 && me->Server->StartPos != 0)
57 ;
58 else if (me->Server->Result == 416 && me->Server->Size == me->File->FileSize())
59 {
60 me->Server->Result = 200;
61 me->Server->StartPos = me->Server->Size;
92e8c1ff
DK
62 // the actual size is not important for https as curl will deal with it
63 // by itself and e.g. doesn't bother us with transport-encoding…
64 me->Server->JunkSize = std::numeric_limits<unsigned long long>::max();
fd46d305
DK
65 }
66 else
67 me->Server->StartPos = 0;
68
69 me->File->Truncate(me->Server->StartPos);
70 me->File->Seek(me->Server->StartPos);
71 }
72 else if (me->Server->HeaderLine(line) == false)
73 return 0;
74
75 return size*nmemb;
76}
77
d546f98d
MV
78size_t
79HttpsMethod::write_data(void *buffer, size_t size, size_t nmemb, void *userp)
80{
81 HttpsMethod *me = (HttpsMethod *)userp;
92e8c1ff
DK
82 size_t buffer_size = size * nmemb;
83 // we don't need to count the junk here, just drop anything we get as
84 // we don't always know how long it would be, e.g. in chunked encoding.
85 if (me->Server->JunkSize != 0)
86 return buffer_size;
d546f98d 87
9127d7ae
MV
88 if (me->ReceivedData == false)
89 {
f9b4f12d 90 me->URIStart(me->Res);
9127d7ae
MV
91 me->ReceivedData = true;
92 }
93
92e8c1ff 94 if(me->File->Write(buffer, buffer_size) != true)
d546f98d
MV
95 return false;
96
92e8c1ff 97 return buffer_size;
d546f98d
MV
98}
99
65512241
DK
100int
101HttpsMethod::progress_callback(void *clientp, double dltotal, double /*dlnow*/,
9127d7ae 102 double /*ultotal*/, double /*ulnow*/)
d546f98d
MV
103{
104 HttpsMethod *me = (HttpsMethod *)clientp;
105 if(dltotal > 0 && me->Res.Size == 0) {
650faab0 106 me->Res.Size = (unsigned long long)dltotal;
d546f98d
MV
107 }
108 return 0;
109}
110
fd46d305 111// HttpsServerState::HttpsServerState - Constructor /*{{{*/
65512241 112HttpsServerState::HttpsServerState(URI Srv,HttpsMethod * /*Owner*/) : ServerState(Srv, NULL)
fd46d305
DK
113{
114 TimeOut = _config->FindI("Acquire::https::Timeout",TimeOut);
115 Reset();
116}
117 /*}}}*/
118
4407a02f 119void HttpsMethod::SetupProxy() /*{{{*/
d546f98d
MV
120{
121 URI ServerName = Queue->Uri;
122
c6ee61ea
MV
123 // Determine the proxy setting
124 AutoDetectProxy(ServerName);
125
5b63d2a9
MV
126 // Curl should never read proxy settings from the environment, as
127 // we determine which proxy to use. Do this for consistency among
128 // methods and prevent an environment variable overriding a
129 // no-proxy ("DIRECT") setting in apt.conf.
130 curl_easy_setopt(curl, CURLOPT_PROXY, "");
131
4407a02f
MV
132 // Determine the proxy setting - try https first, fallback to http and use env at last
133 string UseProxy = _config->Find("Acquire::https::Proxy::" + ServerName.Host,
134 _config->Find("Acquire::http::Proxy::" + ServerName.Host).c_str());
135
136 if (UseProxy.empty() == true)
137 UseProxy = _config->Find("Acquire::https::Proxy", _config->Find("Acquire::http::Proxy").c_str());
138
139 // User want to use NO proxy, so nothing to setup
140 if (UseProxy == "DIRECT")
141 return;
142
143 if (UseProxy.empty() == false)
d546f98d 144 {
4407a02f
MV
145 // Parse no_proxy, a comma (,) separated list of domains we don't want to use
146 // a proxy for so we stop right here if it is in the list
147 if (getenv("no_proxy") != 0 && CheckDomainList(ServerName.Host,getenv("no_proxy")) == true)
148 return;
149 } else {
5b63d2a9
MV
150 const char* result = getenv("https_proxy");
151 // FIXME: Fall back to http_proxy is to remain compatible with
152 // existing setups and behaviour of apt.conf. This should be
153 // deprecated in the future (including apt.conf). Most other
154 // programs do not fall back to http proxy settings and neither
155 // should Apt.
156 if (result == NULL)
157 result = getenv("http_proxy");
4407a02f 158 UseProxy = result == NULL ? "" : result;
d546f98d 159 }
4407a02f 160
d546f98d 161 // Determine what host and port to use based on the proxy settings
4407a02f 162 if (UseProxy.empty() == false)
d546f98d 163 {
4407a02f
MV
164 Proxy = UseProxy;
165 if (Proxy.Port != 1)
d546f98d
MV
166 curl_easy_setopt(curl, CURLOPT_PROXYPORT, Proxy.Port);
167 curl_easy_setopt(curl, CURLOPT_PROXY, Proxy.Host.c_str());
5b63d2a9
MV
168 if (Proxy.User.empty() == false || Proxy.Password.empty() == false)
169 {
170 curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME, Proxy.User.c_str());
171 curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD, Proxy.Password.c_str());
172 }
d546f98d 173 }
b9e9a44b 174} /*}}}*/
d546f98d
MV
175// HttpsMethod::Fetch - Fetch an item /*{{{*/
176// ---------------------------------------------------------------------
177/* This adds an item to the pipeline. We keep the pipeline at a fixed
178 depth. */
179bool HttpsMethod::Fetch(FetchItem *Itm)
180{
d546f98d
MV
181 struct stat SBuf;
182 struct curl_slist *headers=NULL;
714ee06c 183 char curl_errorstr[CURL_ERROR_SIZE];
c769cd6f
MV
184 URI Uri = Itm->Uri;
185 string remotehost = Uri.Host;
9127d7ae 186 ReceivedData = false;
d546f98d
MV
187
188 // TODO:
d546f98d
MV
189 // - http::Pipeline-Depth
190 // - error checking/reporting
191 // - more debug options? (CURLOPT_DEBUGFUNCTION?)
192
5820530d 193 curl_easy_reset(curl);
d546f98d
MV
194 SetupProxy();
195
1de1f703 196 maybe_add_auth (Uri, _config->FindFile("Dir::Etc::netrc"));
592b7800 197
d546f98d 198 // callbacks
01fc8930 199 curl_easy_setopt(curl, CURLOPT_URL, static_cast<string>(Uri).c_str());
fd46d305
DK
200 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, parse_header);
201 curl_easy_setopt(curl, CURLOPT_WRITEHEADER, this);
d546f98d
MV
202 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
203 curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
204 curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
205 curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, this);
dc95fee1 206 // options
d546f98d 207 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, false);
5820530d 208 curl_easy_setopt(curl, CURLOPT_FILETIME, true);
889b0072
DK
209 // only allow curl to handle https, not the other stuff it supports
210 curl_easy_setopt(curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
dc95fee1 211 curl_easy_setopt(curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
d546f98d 212
c769cd6f
MV
213 // SSL parameters are set by default to the common (non mirror-specific) value
214 // if available (or a default one) and gets overload by mirror-specific ones.
215
216 // File containing the list of trusted CA.
217 string cainfo = _config->Find("Acquire::https::CaInfo","");
218 string knob = "Acquire::https::"+remotehost+"::CaInfo";
219 cainfo = _config->Find(knob.c_str(),cainfo.c_str());
46e39c8e 220 if(cainfo.empty() == false)
c769cd6f
MV
221 curl_easy_setopt(curl, CURLOPT_CAINFO,cainfo.c_str());
222
223 // Check server certificate against previous CA list ...
224 bool peer_verify = _config->FindB("Acquire::https::Verify-Peer",true);
225 knob = "Acquire::https::" + remotehost + "::Verify-Peer";
226 peer_verify = _config->FindB(knob.c_str(), peer_verify);
714ee06c
MV
227 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, peer_verify);
228
c769cd6f 229 // ... and hostname against cert CN or subjectAltName
c769cd6f
MV
230 bool verify = _config->FindB("Acquire::https::Verify-Host",true);
231 knob = "Acquire::https::"+remotehost+"::Verify-Host";
232 verify = _config->FindB(knob.c_str(),verify);
52b22cea
DK
233 int const default_verify = (verify == true) ? 2 : 0;
234 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, default_verify);
c769cd6f 235
46e39c8e
MV
236 // Also enforce issuer of server certificate using its cert
237 string issuercert = _config->Find("Acquire::https::IssuerCert","");
238 knob = "Acquire::https::"+remotehost+"::IssuerCert";
239 issuercert = _config->Find(knob.c_str(),issuercert.c_str());
240 if(issuercert.empty() == false)
241 curl_easy_setopt(curl, CURLOPT_ISSUERCERT,issuercert.c_str());
242
c769cd6f 243 // For client authentication, certificate file ...
714ee06c 244 string pem = _config->Find("Acquire::https::SslCert","");
c769cd6f
MV
245 knob = "Acquire::https::"+remotehost+"::SslCert";
246 pem = _config->Find(knob.c_str(),pem.c_str());
46e39c8e 247 if(pem.empty() == false)
714ee06c 248 curl_easy_setopt(curl, CURLOPT_SSLCERT, pem.c_str());
c769cd6f
MV
249
250 // ... and associated key.
251 string key = _config->Find("Acquire::https::SslKey","");
252 knob = "Acquire::https::"+remotehost+"::SslKey";
253 key = _config->Find(knob.c_str(),key.c_str());
46e39c8e 254 if(key.empty() == false)
c769cd6f
MV
255 curl_easy_setopt(curl, CURLOPT_SSLKEY, key.c_str());
256
257 // Allow forcing SSL version to SSLv3 or TLSv1 (SSLv2 is not
258 // supported by GnuTLS).
259 long final_version = CURL_SSLVERSION_DEFAULT;
260 string sslversion = _config->Find("Acquire::https::SslForceVersion","");
261 knob = "Acquire::https::"+remotehost+"::SslForceVersion";
262 sslversion = _config->Find(knob.c_str(),sslversion.c_str());
263 if(sslversion == "TLSv1")
264 final_version = CURL_SSLVERSION_TLSv1;
265 else if(sslversion == "SSLv3")
266 final_version = CURL_SSLVERSION_SSLv3;
267 curl_easy_setopt(curl, CURLOPT_SSLVERSION, final_version);
d546f98d 268
46e39c8e
MV
269 // CRL file
270 string crlfile = _config->Find("Acquire::https::CrlFile","");
271 knob = "Acquire::https::"+remotehost+"::CrlFile";
272 crlfile = _config->Find(knob.c_str(),crlfile.c_str());
273 if(crlfile.empty() == false)
274 curl_easy_setopt(curl, CURLOPT_CRLFILE, crlfile.c_str());
275
d546f98d 276 // cache-control
b9e9a44b
DK
277 if(_config->FindB("Acquire::https::No-Cache",
278 _config->FindB("Acquire::http::No-Cache",false)) == false)
d546f98d
MV
279 {
280 // cache enabled
b9e9a44b
DK
281 if (_config->FindB("Acquire::https::No-Store",
282 _config->FindB("Acquire::http::No-Store",false)) == true)
d546f98d 283 headers = curl_slist_append(headers,"Cache-Control: no-store");
8654fae9 284 stringstream ss;
b9e9a44b
DK
285 ioprintf(ss, "Cache-Control: max-age=%u", _config->FindI("Acquire::https::Max-Age",
286 _config->FindI("Acquire::http::Max-Age",0)));
d546f98d
MV
287 headers = curl_slist_append(headers, ss.str().c_str());
288 } else {
289 // cache disabled by user
290 headers = curl_slist_append(headers, "Cache-Control: no-cache");
291 headers = curl_slist_append(headers, "Pragma: no-cache");
292 }
293 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
294
d546f98d 295 // speed limit
46e39c8e 296 int const dlLimit = _config->FindI("Acquire::https::Dl-Limit",
b9e9a44b 297 _config->FindI("Acquire::http::Dl-Limit",0))*1024;
d546f98d
MV
298 if (dlLimit > 0)
299 curl_easy_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE, dlLimit);
300
301 // set header
9f542bae
DK
302 curl_easy_setopt(curl, CURLOPT_USERAGENT,
303 _config->Find("Acquire::https::User-Agent",
304 _config->Find("Acquire::http::User-Agent",
335e2c82 305 "Debian APT-CURL/1.0 (" PACKAGE_VERSION ")").c_str()).c_str());
d546f98d 306
cc615257 307 // set timeout
46e39c8e 308 int const timeout = _config->FindI("Acquire::https::Timeout",
b9e9a44b 309 _config->FindI("Acquire::http::Timeout",120));
cc615257 310 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout);
43cf55db 311 //set really low lowspeed timeout (see #497983)
5085e660 312 curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, DL_MIN_SPEED);
43cf55db 313 curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, timeout);
cc615257 314
668ce84d 315 // set redirect options and default to 10 redirects
46e39c8e 316 bool const AllowRedirect = _config->FindB("Acquire::https::AllowRedirect",
b9e9a44b 317 _config->FindB("Acquire::http::AllowRedirect",true));
668ce84d
MV
318 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, AllowRedirect);
319 curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 10);
320
d546f98d 321 // debug
714ee06c 322 if(_config->FindB("Debug::Acquire::https", false))
d546f98d
MV
323 curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
324
714ee06c 325 // error handling
cc418115 326 curl_errorstr[0] = '\0';
714ee06c
MV
327 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
328
6f4501f9 329 // If we ask for uncompressed files servers might respond with content-
1e3f4083 330 // negotiation which lets us end up with compressed files we do not support,
6f4501f9
DK
331 // see 657029, 657560 and co, so if we have no extension on the request
332 // ask for text only. As a sidenote: If there is nothing to negotate servers
333 // seem to be nice and ignore it.
334 if (_config->FindB("Acquire::https::SendAccept", _config->FindB("Acquire::http::SendAccept", true)) == true)
335 {
336 size_t const filepos = Itm->Uri.find_last_of('/');
337 string const file = Itm->Uri.substr(filepos + 1);
338 if (flExtension(file) == file)
339 headers = curl_slist_append(headers, "Accept: text/*");
340 }
341
d6039f9e 342 // if we have the file send an if-range query with a range header
4c499611
MV
343 if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
344 {
062074cb
DK
345 std::string Buf;
346 strprintf(Buf, "Range: bytes=%lli-", (long long) SBuf.st_size);
347 headers = curl_slist_append(headers, Buf.c_str());
348 strprintf(Buf, "If-Range: %s", TimeRFC1123(SBuf.st_mtime).c_str());
349 headers = curl_slist_append(headers, Buf.c_str());
8654fae9 350 }
d6039f9e
MV
351 else if(Itm->LastModified > 0)
352 {
353 curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
354 curl_easy_setopt(curl, CURLOPT_TIMEVALUE, Itm->LastModified);
4c499611 355 }
d546f98d
MV
356
357 // go for it - if the file exists, append on it
358 File = new FileFd(Itm->DestFile, FileFd::WriteAny);
fd46d305 359 Server = new HttpsServerState(Itm->Uri, this);
85050e76 360
d546f98d
MV
361 // keep apt updated
362 Res.Filename = Itm->DestFile;
363
364 // get it!
365 CURLcode success = curl_easy_perform(curl);
d546f98d 366
1dea08eb
MV
367 // If the server returns 200 OK but the If-Modified-Since condition is not
368 // met, CURLINFO_CONDITION_UNMET will be set to 1
369 long curl_condition_unmet = 0;
370 curl_easy_getinfo(curl, CURLINFO_CONDITION_UNMET, &curl_condition_unmet);
371
db1f1469 372 File->Close();
85050e76 373 curl_slist_free_all(headers);
db1f1469 374
d546f98d 375 // cleanup
85050e76 376 if (success != 0)
4c499611 377 {
9b5d79ec 378 _error->Error("%s", curl_errorstr);
db1f1469 379 unlink(File->Name().c_str());
85050e76
DK
380 return false;
381 }
382
383 // server says file not modified
fd46d305 384 if (Server->Result == 304 || curl_condition_unmet == 1)
85050e76
DK
385 {
386 unlink(File->Name().c_str());
387 Res.IMSHit = true;
388 Res.LastModified = Itm->LastModified;
389 Res.Size = 0;
390 URIDone(Res);
d546f98d
MV
391 return true;
392 }
fd46d305 393 Res.IMSHit = false;
d546f98d 394
fd46d305
DK
395 if (Server->Result != 200 && // OK
396 Server->Result != 206 && // Partial
397 Server->Result != 416) // invalid Range
85050e76
DK
398 {
399 char err[255];
fd46d305 400 snprintf(err, sizeof(err) - 1, "HttpError%i", Server->Result);
85050e76
DK
401 SetFailReason(err);
402 _error->Error("%s", err);
403 // unlink, no need keep 401/404 page content in partial/
404 unlink(File->Name().c_str());
1296bc7c
RE
405 Res.Size = 0;
406 Res.LastModified = 0;
85050e76
DK
407 return false;
408 }
409
410 struct stat resultStat;
411 if (unlikely(stat(File->Name().c_str(), &resultStat) != 0))
412 {
413 _error->Errno("stat", "Unable to access file %s", File->Name().c_str());
414 return false;
415 }
416 Res.Size = resultStat.st_size;
417
418 // invalid range-request
fd46d305 419 if (Server->Result == 416)
85050e76
DK
420 {
421 unlink(File->Name().c_str());
422 Res.Size = 0;
423 delete File;
424 Redirect(Itm->Uri);
425 return true;
5820530d
OS
426 }
427
85050e76
DK
428 // Timestamp
429 curl_easy_getinfo(curl, CURLINFO_FILETIME, &Res.LastModified);
430 if (Res.LastModified != -1)
431 {
246bbb61 432 struct timeval times[2];
9ce3cfc9
DK
433 times[0].tv_sec = Res.LastModified;
434 times[1].tv_sec = Res.LastModified;
246bbb61
DK
435 times[0].tv_usec = times[1].tv_usec = 0;
436 utimes(File->Name().c_str(), times);
85050e76
DK
437 }
438 else
439 Res.LastModified = resultStat.st_mtime;
d546f98d
MV
440
441 // take hashes
442 Hashes Hash;
443 FileFd Fd(Res.Filename, FileFd::ReadOnly);
109eb151 444 Hash.AddFD(Fd);
d546f98d 445 Res.TakeHashes(Hash);
85050e76 446
d546f98d
MV
447 // keep apt updated
448 URIDone(Res);
449
450 // cleanup
d546f98d
MV
451 Res.Size = 0;
452 delete File;
d546f98d
MV
453
454 return true;
d3e8fbb3 455}
d546f98d
MV
456
457int main()
458{
459 setlocale(LC_ALL, "");
460
461 HttpsMethod Mth;
462 curl_global_init(CURL_GLOBAL_SSL) ;
463
464 return Mth.Run();
465}
466