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