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