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