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