keep track of where we are in a filedescriptor so we can use it as Tell()
[ntk/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
ae58a985 6 HTTPS Acquire Method - This is the HTTPS aquire 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>
24#include <utime.h>
25#include <unistd.h>
26#include <signal.h>
27#include <stdio.h>
28#include <errno.h>
29#include <string.h>
30#include <iostream>
d546f98d
MV
31#include <sstream>
32
33#include "config.h"
34#include "https.h"
ea542140 35#include <apti18n.h>
d546f98d
MV
36 /*}}}*/
37using namespace std;
38
39size_t
40HttpsMethod::write_data(void *buffer, size_t size, size_t nmemb, void *userp)
41{
42 HttpsMethod *me = (HttpsMethod *)userp;
43
44 if(me->File->Write(buffer, size*nmemb) != true)
45 return false;
46
47 return size*nmemb;
48}
49
50int
51HttpsMethod::progress_callback(void *clientp, double dltotal, double dlnow,
52 double ultotal, double ulnow)
53{
54 HttpsMethod *me = (HttpsMethod *)clientp;
55 if(dltotal > 0 && me->Res.Size == 0) {
650faab0 56 me->Res.Size = (unsigned long long)dltotal;
d546f98d
MV
57 me->URIStart(me->Res);
58 }
59 return 0;
60}
61
4407a02f 62void HttpsMethod::SetupProxy() /*{{{*/
d546f98d
MV
63{
64 URI ServerName = Queue->Uri;
65
4407a02f
MV
66 // Determine the proxy setting - try https first, fallback to http and use env at last
67 string UseProxy = _config->Find("Acquire::https::Proxy::" + ServerName.Host,
68 _config->Find("Acquire::http::Proxy::" + ServerName.Host).c_str());
69
70 if (UseProxy.empty() == true)
71 UseProxy = _config->Find("Acquire::https::Proxy", _config->Find("Acquire::http::Proxy").c_str());
72
73 // User want to use NO proxy, so nothing to setup
74 if (UseProxy == "DIRECT")
75 return;
76
77 if (UseProxy.empty() == false)
d546f98d 78 {
4407a02f
MV
79 // Parse no_proxy, a comma (,) separated list of domains we don't want to use
80 // a proxy for so we stop right here if it is in the list
81 if (getenv("no_proxy") != 0 && CheckDomainList(ServerName.Host,getenv("no_proxy")) == true)
82 return;
83 } else {
84 const char* result = getenv("http_proxy");
85 UseProxy = result == NULL ? "" : result;
d546f98d 86 }
4407a02f 87
d546f98d 88 // Determine what host and port to use based on the proxy settings
4407a02f 89 if (UseProxy.empty() == false)
d546f98d 90 {
4407a02f
MV
91 Proxy = UseProxy;
92 if (Proxy.Port != 1)
d546f98d
MV
93 curl_easy_setopt(curl, CURLOPT_PROXYPORT, Proxy.Port);
94 curl_easy_setopt(curl, CURLOPT_PROXY, Proxy.Host.c_str());
95 }
b9e9a44b 96} /*}}}*/
d546f98d
MV
97// HttpsMethod::Fetch - Fetch an item /*{{{*/
98// ---------------------------------------------------------------------
99/* This adds an item to the pipeline. We keep the pipeline at a fixed
100 depth. */
101bool HttpsMethod::Fetch(FetchItem *Itm)
102{
103 stringstream ss;
104 struct stat SBuf;
105 struct curl_slist *headers=NULL;
714ee06c 106 char curl_errorstr[CURL_ERROR_SIZE];
4c499611 107 long curl_responsecode;
c769cd6f
MV
108 URI Uri = Itm->Uri;
109 string remotehost = Uri.Host;
d546f98d
MV
110
111 // TODO:
d546f98d
MV
112 // - http::Pipeline-Depth
113 // - error checking/reporting
114 // - more debug options? (CURLOPT_DEBUGFUNCTION?)
115
5820530d 116 curl_easy_reset(curl);
d546f98d
MV
117 SetupProxy();
118
1de1f703 119 maybe_add_auth (Uri, _config->FindFile("Dir::Etc::netrc"));
592b7800 120
d546f98d 121 // callbacks
01fc8930 122 curl_easy_setopt(curl, CURLOPT_URL, static_cast<string>(Uri).c_str());
d546f98d
MV
123 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
124 curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
125 curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
126 curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, this);
127 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, false);
128 curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);
5820530d 129 curl_easy_setopt(curl, CURLOPT_FILETIME, true);
d546f98d 130
c769cd6f
MV
131 // SSL parameters are set by default to the common (non mirror-specific) value
132 // if available (or a default one) and gets overload by mirror-specific ones.
133
134 // File containing the list of trusted CA.
135 string cainfo = _config->Find("Acquire::https::CaInfo","");
136 string knob = "Acquire::https::"+remotehost+"::CaInfo";
137 cainfo = _config->Find(knob.c_str(),cainfo.c_str());
46e39c8e 138 if(cainfo.empty() == false)
c769cd6f
MV
139 curl_easy_setopt(curl, CURLOPT_CAINFO,cainfo.c_str());
140
141 // Check server certificate against previous CA list ...
142 bool peer_verify = _config->FindB("Acquire::https::Verify-Peer",true);
143 knob = "Acquire::https::" + remotehost + "::Verify-Peer";
144 peer_verify = _config->FindB(knob.c_str(), peer_verify);
714ee06c
MV
145 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, peer_verify);
146
c769cd6f 147 // ... and hostname against cert CN or subjectAltName
c769cd6f
MV
148 bool verify = _config->FindB("Acquire::https::Verify-Host",true);
149 knob = "Acquire::https::"+remotehost+"::Verify-Host";
150 verify = _config->FindB(knob.c_str(),verify);
52b22cea
DK
151 int const default_verify = (verify == true) ? 2 : 0;
152 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, default_verify);
c769cd6f 153
46e39c8e
MV
154 // Also enforce issuer of server certificate using its cert
155 string issuercert = _config->Find("Acquire::https::IssuerCert","");
156 knob = "Acquire::https::"+remotehost+"::IssuerCert";
157 issuercert = _config->Find(knob.c_str(),issuercert.c_str());
158 if(issuercert.empty() == false)
159 curl_easy_setopt(curl, CURLOPT_ISSUERCERT,issuercert.c_str());
160
c769cd6f 161 // For client authentication, certificate file ...
714ee06c 162 string pem = _config->Find("Acquire::https::SslCert","");
c769cd6f
MV
163 knob = "Acquire::https::"+remotehost+"::SslCert";
164 pem = _config->Find(knob.c_str(),pem.c_str());
46e39c8e 165 if(pem.empty() == false)
714ee06c 166 curl_easy_setopt(curl, CURLOPT_SSLCERT, pem.c_str());
c769cd6f
MV
167
168 // ... and associated key.
169 string key = _config->Find("Acquire::https::SslKey","");
170 knob = "Acquire::https::"+remotehost+"::SslKey";
171 key = _config->Find(knob.c_str(),key.c_str());
46e39c8e 172 if(key.empty() == false)
c769cd6f
MV
173 curl_easy_setopt(curl, CURLOPT_SSLKEY, key.c_str());
174
175 // Allow forcing SSL version to SSLv3 or TLSv1 (SSLv2 is not
176 // supported by GnuTLS).
177 long final_version = CURL_SSLVERSION_DEFAULT;
178 string sslversion = _config->Find("Acquire::https::SslForceVersion","");
179 knob = "Acquire::https::"+remotehost+"::SslForceVersion";
180 sslversion = _config->Find(knob.c_str(),sslversion.c_str());
181 if(sslversion == "TLSv1")
182 final_version = CURL_SSLVERSION_TLSv1;
183 else if(sslversion == "SSLv3")
184 final_version = CURL_SSLVERSION_SSLv3;
185 curl_easy_setopt(curl, CURLOPT_SSLVERSION, final_version);
d546f98d 186
46e39c8e
MV
187 // CRL file
188 string crlfile = _config->Find("Acquire::https::CrlFile","");
189 knob = "Acquire::https::"+remotehost+"::CrlFile";
190 crlfile = _config->Find(knob.c_str(),crlfile.c_str());
191 if(crlfile.empty() == false)
192 curl_easy_setopt(curl, CURLOPT_CRLFILE, crlfile.c_str());
193
d546f98d 194 // cache-control
b9e9a44b
DK
195 if(_config->FindB("Acquire::https::No-Cache",
196 _config->FindB("Acquire::http::No-Cache",false)) == false)
d546f98d
MV
197 {
198 // cache enabled
b9e9a44b
DK
199 if (_config->FindB("Acquire::https::No-Store",
200 _config->FindB("Acquire::http::No-Store",false)) == true)
d546f98d 201 headers = curl_slist_append(headers,"Cache-Control: no-store");
b9e9a44b
DK
202 ioprintf(ss, "Cache-Control: max-age=%u", _config->FindI("Acquire::https::Max-Age",
203 _config->FindI("Acquire::http::Max-Age",0)));
d546f98d
MV
204 headers = curl_slist_append(headers, ss.str().c_str());
205 } else {
206 // cache disabled by user
207 headers = curl_slist_append(headers, "Cache-Control: no-cache");
208 headers = curl_slist_append(headers, "Pragma: no-cache");
209 }
210 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
211
d546f98d 212 // speed limit
46e39c8e 213 int const dlLimit = _config->FindI("Acquire::https::Dl-Limit",
b9e9a44b 214 _config->FindI("Acquire::http::Dl-Limit",0))*1024;
d546f98d
MV
215 if (dlLimit > 0)
216 curl_easy_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE, dlLimit);
217
218 // set header
9f542bae
DK
219 curl_easy_setopt(curl, CURLOPT_USERAGENT,
220 _config->Find("Acquire::https::User-Agent",
221 _config->Find("Acquire::http::User-Agent",
e4c2981b 222 "Debian APT-CURL/1.0 ("VERSION")").c_str()).c_str());
d546f98d 223
cc615257 224 // set timeout
46e39c8e 225 int const timeout = _config->FindI("Acquire::https::Timeout",
b9e9a44b 226 _config->FindI("Acquire::http::Timeout",120));
cc615257 227 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout);
43cf55db 228 //set really low lowspeed timeout (see #497983)
5085e660 229 curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, DL_MIN_SPEED);
43cf55db 230 curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, timeout);
cc615257 231
668ce84d 232 // set redirect options and default to 10 redirects
46e39c8e 233 bool const AllowRedirect = _config->FindB("Acquire::https::AllowRedirect",
b9e9a44b 234 _config->FindB("Acquire::http::AllowRedirect",true));
668ce84d
MV
235 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, AllowRedirect);
236 curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 10);
237
d546f98d 238 // debug
714ee06c 239 if(_config->FindB("Debug::Acquire::https", false))
d546f98d
MV
240 curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
241
714ee06c
MV
242 // error handling
243 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
244
d6039f9e 245 // if we have the file send an if-range query with a range header
4c499611
MV
246 if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
247 {
248 char Buf[1000];
249 sprintf(Buf,"Range: bytes=%li-\r\nIf-Range: %s\r\n",
250 (long)SBuf.st_size - 1,
251 TimeRFC1123(SBuf.st_mtime).c_str());
252 headers = curl_slist_append(headers, Buf);
d6039f9e
MV
253 }
254 else if(Itm->LastModified > 0)
255 {
256 curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
257 curl_easy_setopt(curl, CURLOPT_TIMEVALUE, Itm->LastModified);
4c499611 258 }
d546f98d
MV
259
260 // go for it - if the file exists, append on it
261 File = new FileFd(Itm->DestFile, FileFd::WriteAny);
d6039f9e
MV
262 if (File->Size() > 0)
263 File->Seek(File->Size() - 1);
d546f98d
MV
264
265 // keep apt updated
266 Res.Filename = Itm->DestFile;
267
268 // get it!
269 CURLcode success = curl_easy_perform(curl);
4c499611 270 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &curl_responsecode);
d546f98d 271
5820530d
OS
272 long curl_servdate;
273 curl_easy_getinfo(curl, CURLINFO_FILETIME, &curl_servdate);
274
db1f1469
MV
275 File->Close();
276
d546f98d 277 // cleanup
4c499611
MV
278 if(success != 0)
279 {
9b5d79ec 280 _error->Error("%s", curl_errorstr);
db1f1469
MV
281 // unlink, no need keep 401/404 page content in partial/
282 unlink(File->Name().c_str());
d546f98d
MV
283 Fail();
284 return true;
285 }
286
5820530d
OS
287 // Timestamp
288 struct utimbuf UBuf;
289 if (curl_servdate != -1) {
290 UBuf.actime = curl_servdate;
291 UBuf.modtime = curl_servdate;
292 utime(File->Name().c_str(),&UBuf);
293 }
294
d546f98d
MV
295 // check the downloaded result
296 struct stat Buf;
297 if (stat(File->Name().c_str(),&Buf) == 0)
298 {
d546f98d
MV
299 Res.Filename = File->Name();
300 Res.LastModified = Buf.st_mtime;
301 Res.IMSHit = false;
4c499611 302 if (curl_responsecode == 304)
714ee06c 303 {
d6039f9e 304 unlink(File->Name().c_str());
d546f98d 305 Res.IMSHit = true;
714ee06c 306 Res.LastModified = Itm->LastModified;
d6039f9e
MV
307 Res.Size = 0;
308 URIDone(Res);
309 return true;
714ee06c 310 }
d6039f9e 311 Res.Size = Buf.st_size;
d546f98d
MV
312 }
313
314 // take hashes
315 Hashes Hash;
316 FileFd Fd(Res.Filename, FileFd::ReadOnly);
317 Hash.AddFD(Fd.Fd(), Fd.Size());
318 Res.TakeHashes(Hash);
319
320 // keep apt updated
321 URIDone(Res);
322
323 // cleanup
d546f98d
MV
324 Res.Size = 0;
325 delete File;
326 curl_slist_free_all(headers);
327
328 return true;
329};
330
331int main()
332{
333 setlocale(LC_ALL, "");
334
335 HttpsMethod Mth;
336 curl_global_init(CURL_GLOBAL_SSL) ;
337
338 return Mth.Run();
339}
340
341