]>
Commit | Line | Data |
---|---|---|
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 | /*}}}*/ |
37 | using namespace std; | |
38 | ||
39 | size_t | |
40 | HttpsMethod::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 | ||
50 | int | |
51 | HttpsMethod::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 | 62 | void HttpsMethod::SetupProxy() /*{{{*/ |
d546f98d MV |
63 | { |
64 | URI ServerName = Queue->Uri; | |
65 | ||
5b63d2a9 MV |
66 | // Curl should never read proxy settings from the environment, as |
67 | // we determine which proxy to use. Do this for consistency among | |
68 | // methods and prevent an environment variable overriding a | |
69 | // no-proxy ("DIRECT") setting in apt.conf. | |
70 | curl_easy_setopt(curl, CURLOPT_PROXY, ""); | |
71 | ||
4407a02f MV |
72 | // Determine the proxy setting - try https first, fallback to http and use env at last |
73 | string UseProxy = _config->Find("Acquire::https::Proxy::" + ServerName.Host, | |
74 | _config->Find("Acquire::http::Proxy::" + ServerName.Host).c_str()); | |
75 | ||
76 | if (UseProxy.empty() == true) | |
77 | UseProxy = _config->Find("Acquire::https::Proxy", _config->Find("Acquire::http::Proxy").c_str()); | |
78 | ||
79 | // User want to use NO proxy, so nothing to setup | |
80 | if (UseProxy == "DIRECT") | |
81 | return; | |
82 | ||
83 | if (UseProxy.empty() == false) | |
d546f98d | 84 | { |
4407a02f MV |
85 | // Parse no_proxy, a comma (,) separated list of domains we don't want to use |
86 | // a proxy for so we stop right here if it is in the list | |
87 | if (getenv("no_proxy") != 0 && CheckDomainList(ServerName.Host,getenv("no_proxy")) == true) | |
88 | return; | |
89 | } else { | |
5b63d2a9 MV |
90 | const char* result = getenv("https_proxy"); |
91 | // FIXME: Fall back to http_proxy is to remain compatible with | |
92 | // existing setups and behaviour of apt.conf. This should be | |
93 | // deprecated in the future (including apt.conf). Most other | |
94 | // programs do not fall back to http proxy settings and neither | |
95 | // should Apt. | |
96 | if (result == NULL) | |
97 | result = getenv("http_proxy"); | |
4407a02f | 98 | UseProxy = result == NULL ? "" : result; |
d546f98d | 99 | } |
4407a02f | 100 | |
d546f98d | 101 | // Determine what host and port to use based on the proxy settings |
4407a02f | 102 | if (UseProxy.empty() == false) |
d546f98d | 103 | { |
4407a02f MV |
104 | Proxy = UseProxy; |
105 | if (Proxy.Port != 1) | |
d546f98d MV |
106 | curl_easy_setopt(curl, CURLOPT_PROXYPORT, Proxy.Port); |
107 | curl_easy_setopt(curl, CURLOPT_PROXY, Proxy.Host.c_str()); | |
5b63d2a9 MV |
108 | if (Proxy.User.empty() == false || Proxy.Password.empty() == false) |
109 | { | |
110 | curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME, Proxy.User.c_str()); | |
111 | curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD, Proxy.Password.c_str()); | |
112 | } | |
d546f98d | 113 | } |
b9e9a44b | 114 | } /*}}}*/ |
d546f98d MV |
115 | // HttpsMethod::Fetch - Fetch an item /*{{{*/ |
116 | // --------------------------------------------------------------------- | |
117 | /* This adds an item to the pipeline. We keep the pipeline at a fixed | |
118 | depth. */ | |
119 | bool HttpsMethod::Fetch(FetchItem *Itm) | |
120 | { | |
d546f98d MV |
121 | struct stat SBuf; |
122 | struct curl_slist *headers=NULL; | |
714ee06c | 123 | char curl_errorstr[CURL_ERROR_SIZE]; |
4c499611 | 124 | long curl_responsecode; |
c769cd6f MV |
125 | URI Uri = Itm->Uri; |
126 | string remotehost = Uri.Host; | |
d546f98d MV |
127 | |
128 | // TODO: | |
d546f98d MV |
129 | // - http::Pipeline-Depth |
130 | // - error checking/reporting | |
131 | // - more debug options? (CURLOPT_DEBUGFUNCTION?) | |
132 | ||
5820530d | 133 | curl_easy_reset(curl); |
d546f98d MV |
134 | SetupProxy(); |
135 | ||
1de1f703 | 136 | maybe_add_auth (Uri, _config->FindFile("Dir::Etc::netrc")); |
592b7800 | 137 | |
d546f98d | 138 | // callbacks |
01fc8930 | 139 | curl_easy_setopt(curl, CURLOPT_URL, static_cast<string>(Uri).c_str()); |
d546f98d MV |
140 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); |
141 | curl_easy_setopt(curl, CURLOPT_WRITEDATA, this); | |
142 | curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback); | |
143 | curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, this); | |
144 | curl_easy_setopt(curl, CURLOPT_NOPROGRESS, false); | |
5820530d | 145 | curl_easy_setopt(curl, CURLOPT_FILETIME, true); |
d546f98d | 146 | |
c769cd6f MV |
147 | // SSL parameters are set by default to the common (non mirror-specific) value |
148 | // if available (or a default one) and gets overload by mirror-specific ones. | |
149 | ||
150 | // File containing the list of trusted CA. | |
151 | string cainfo = _config->Find("Acquire::https::CaInfo",""); | |
152 | string knob = "Acquire::https::"+remotehost+"::CaInfo"; | |
153 | cainfo = _config->Find(knob.c_str(),cainfo.c_str()); | |
46e39c8e | 154 | if(cainfo.empty() == false) |
c769cd6f MV |
155 | curl_easy_setopt(curl, CURLOPT_CAINFO,cainfo.c_str()); |
156 | ||
157 | // Check server certificate against previous CA list ... | |
158 | bool peer_verify = _config->FindB("Acquire::https::Verify-Peer",true); | |
159 | knob = "Acquire::https::" + remotehost + "::Verify-Peer"; | |
160 | peer_verify = _config->FindB(knob.c_str(), peer_verify); | |
714ee06c MV |
161 | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, peer_verify); |
162 | ||
c769cd6f | 163 | // ... and hostname against cert CN or subjectAltName |
c769cd6f MV |
164 | bool verify = _config->FindB("Acquire::https::Verify-Host",true); |
165 | knob = "Acquire::https::"+remotehost+"::Verify-Host"; | |
166 | verify = _config->FindB(knob.c_str(),verify); | |
52b22cea DK |
167 | int const default_verify = (verify == true) ? 2 : 0; |
168 | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, default_verify); | |
c769cd6f | 169 | |
46e39c8e MV |
170 | // Also enforce issuer of server certificate using its cert |
171 | string issuercert = _config->Find("Acquire::https::IssuerCert",""); | |
172 | knob = "Acquire::https::"+remotehost+"::IssuerCert"; | |
173 | issuercert = _config->Find(knob.c_str(),issuercert.c_str()); | |
174 | if(issuercert.empty() == false) | |
175 | curl_easy_setopt(curl, CURLOPT_ISSUERCERT,issuercert.c_str()); | |
176 | ||
c769cd6f | 177 | // For client authentication, certificate file ... |
714ee06c | 178 | string pem = _config->Find("Acquire::https::SslCert",""); |
c769cd6f MV |
179 | knob = "Acquire::https::"+remotehost+"::SslCert"; |
180 | pem = _config->Find(knob.c_str(),pem.c_str()); | |
46e39c8e | 181 | if(pem.empty() == false) |
714ee06c | 182 | curl_easy_setopt(curl, CURLOPT_SSLCERT, pem.c_str()); |
c769cd6f MV |
183 | |
184 | // ... and associated key. | |
185 | string key = _config->Find("Acquire::https::SslKey",""); | |
186 | knob = "Acquire::https::"+remotehost+"::SslKey"; | |
187 | key = _config->Find(knob.c_str(),key.c_str()); | |
46e39c8e | 188 | if(key.empty() == false) |
c769cd6f MV |
189 | curl_easy_setopt(curl, CURLOPT_SSLKEY, key.c_str()); |
190 | ||
191 | // Allow forcing SSL version to SSLv3 or TLSv1 (SSLv2 is not | |
192 | // supported by GnuTLS). | |
193 | long final_version = CURL_SSLVERSION_DEFAULT; | |
194 | string sslversion = _config->Find("Acquire::https::SslForceVersion",""); | |
195 | knob = "Acquire::https::"+remotehost+"::SslForceVersion"; | |
196 | sslversion = _config->Find(knob.c_str(),sslversion.c_str()); | |
197 | if(sslversion == "TLSv1") | |
198 | final_version = CURL_SSLVERSION_TLSv1; | |
199 | else if(sslversion == "SSLv3") | |
200 | final_version = CURL_SSLVERSION_SSLv3; | |
201 | curl_easy_setopt(curl, CURLOPT_SSLVERSION, final_version); | |
d546f98d | 202 | |
46e39c8e MV |
203 | // CRL file |
204 | string crlfile = _config->Find("Acquire::https::CrlFile",""); | |
205 | knob = "Acquire::https::"+remotehost+"::CrlFile"; | |
206 | crlfile = _config->Find(knob.c_str(),crlfile.c_str()); | |
207 | if(crlfile.empty() == false) | |
208 | curl_easy_setopt(curl, CURLOPT_CRLFILE, crlfile.c_str()); | |
209 | ||
d546f98d | 210 | // cache-control |
b9e9a44b DK |
211 | if(_config->FindB("Acquire::https::No-Cache", |
212 | _config->FindB("Acquire::http::No-Cache",false)) == false) | |
d546f98d MV |
213 | { |
214 | // cache enabled | |
b9e9a44b DK |
215 | if (_config->FindB("Acquire::https::No-Store", |
216 | _config->FindB("Acquire::http::No-Store",false)) == true) | |
d546f98d | 217 | headers = curl_slist_append(headers,"Cache-Control: no-store"); |
8654fae9 | 218 | stringstream ss; |
b9e9a44b DK |
219 | ioprintf(ss, "Cache-Control: max-age=%u", _config->FindI("Acquire::https::Max-Age", |
220 | _config->FindI("Acquire::http::Max-Age",0))); | |
d546f98d MV |
221 | headers = curl_slist_append(headers, ss.str().c_str()); |
222 | } else { | |
223 | // cache disabled by user | |
224 | headers = curl_slist_append(headers, "Cache-Control: no-cache"); | |
225 | headers = curl_slist_append(headers, "Pragma: no-cache"); | |
226 | } | |
227 | curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); | |
228 | ||
d546f98d | 229 | // speed limit |
46e39c8e | 230 | int const dlLimit = _config->FindI("Acquire::https::Dl-Limit", |
b9e9a44b | 231 | _config->FindI("Acquire::http::Dl-Limit",0))*1024; |
d546f98d MV |
232 | if (dlLimit > 0) |
233 | curl_easy_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE, dlLimit); | |
234 | ||
235 | // set header | |
9f542bae DK |
236 | curl_easy_setopt(curl, CURLOPT_USERAGENT, |
237 | _config->Find("Acquire::https::User-Agent", | |
238 | _config->Find("Acquire::http::User-Agent", | |
335e2c82 | 239 | "Debian APT-CURL/1.0 (" PACKAGE_VERSION ")").c_str()).c_str()); |
d546f98d | 240 | |
cc615257 | 241 | // set timeout |
46e39c8e | 242 | int const timeout = _config->FindI("Acquire::https::Timeout", |
b9e9a44b | 243 | _config->FindI("Acquire::http::Timeout",120)); |
cc615257 | 244 | curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout); |
43cf55db | 245 | //set really low lowspeed timeout (see #497983) |
5085e660 | 246 | curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, DL_MIN_SPEED); |
43cf55db | 247 | curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, timeout); |
cc615257 | 248 | |
668ce84d | 249 | // set redirect options and default to 10 redirects |
46e39c8e | 250 | bool const AllowRedirect = _config->FindB("Acquire::https::AllowRedirect", |
b9e9a44b | 251 | _config->FindB("Acquire::http::AllowRedirect",true)); |
668ce84d MV |
252 | curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, AllowRedirect); |
253 | curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 10); | |
254 | ||
d546f98d | 255 | // debug |
714ee06c | 256 | if(_config->FindB("Debug::Acquire::https", false)) |
d546f98d MV |
257 | curl_easy_setopt(curl, CURLOPT_VERBOSE, true); |
258 | ||
714ee06c | 259 | // error handling |
cc418115 | 260 | curl_errorstr[0] = '\0'; |
714ee06c MV |
261 | curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr); |
262 | ||
6f4501f9 DK |
263 | // If we ask for uncompressed files servers might respond with content- |
264 | // negotation which lets us end up with compressed files we do not support, | |
265 | // see 657029, 657560 and co, so if we have no extension on the request | |
266 | // ask for text only. As a sidenote: If there is nothing to negotate servers | |
267 | // seem to be nice and ignore it. | |
268 | if (_config->FindB("Acquire::https::SendAccept", _config->FindB("Acquire::http::SendAccept", true)) == true) | |
269 | { | |
270 | size_t const filepos = Itm->Uri.find_last_of('/'); | |
271 | string const file = Itm->Uri.substr(filepos + 1); | |
272 | if (flExtension(file) == file) | |
273 | headers = curl_slist_append(headers, "Accept: text/*"); | |
274 | } | |
275 | ||
d6039f9e | 276 | // if we have the file send an if-range query with a range header |
4c499611 MV |
277 | if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0) |
278 | { | |
279 | char Buf[1000]; | |
8654fae9 | 280 | sprintf(Buf, "Range: bytes=%li-", (long) SBuf.st_size - 1); |
4c499611 | 281 | headers = curl_slist_append(headers, Buf); |
8654fae9 DK |
282 | sprintf(Buf, "If-Range: %s", TimeRFC1123(SBuf.st_mtime).c_str()); |
283 | headers = curl_slist_append(headers, Buf); | |
284 | } | |
d6039f9e MV |
285 | else if(Itm->LastModified > 0) |
286 | { | |
287 | curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE); | |
288 | curl_easy_setopt(curl, CURLOPT_TIMEVALUE, Itm->LastModified); | |
4c499611 | 289 | } |
d546f98d MV |
290 | |
291 | // go for it - if the file exists, append on it | |
292 | File = new FileFd(Itm->DestFile, FileFd::WriteAny); | |
d6039f9e MV |
293 | if (File->Size() > 0) |
294 | File->Seek(File->Size() - 1); | |
d546f98d MV |
295 | |
296 | // keep apt updated | |
297 | Res.Filename = Itm->DestFile; | |
298 | ||
299 | // get it! | |
300 | CURLcode success = curl_easy_perform(curl); | |
4c499611 | 301 | curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &curl_responsecode); |
d546f98d | 302 | |
5820530d OS |
303 | long curl_servdate; |
304 | curl_easy_getinfo(curl, CURLINFO_FILETIME, &curl_servdate); | |
305 | ||
1dea08eb MV |
306 | // If the server returns 200 OK but the If-Modified-Since condition is not |
307 | // met, CURLINFO_CONDITION_UNMET will be set to 1 | |
308 | long curl_condition_unmet = 0; | |
309 | curl_easy_getinfo(curl, CURLINFO_CONDITION_UNMET, &curl_condition_unmet); | |
310 | ||
db1f1469 MV |
311 | File->Close(); |
312 | ||
d546f98d | 313 | // cleanup |
cc418115 | 314 | if(success != 0 || (curl_responsecode != 200 && curl_responsecode != 304)) |
4c499611 | 315 | { |
9b5d79ec | 316 | _error->Error("%s", curl_errorstr); |
db1f1469 MV |
317 | // unlink, no need keep 401/404 page content in partial/ |
318 | unlink(File->Name().c_str()); | |
d546f98d MV |
319 | Fail(); |
320 | return true; | |
321 | } | |
322 | ||
5820530d OS |
323 | // Timestamp |
324 | struct utimbuf UBuf; | |
325 | if (curl_servdate != -1) { | |
326 | UBuf.actime = curl_servdate; | |
327 | UBuf.modtime = curl_servdate; | |
328 | utime(File->Name().c_str(),&UBuf); | |
329 | } | |
330 | ||
d546f98d MV |
331 | // check the downloaded result |
332 | struct stat Buf; | |
333 | if (stat(File->Name().c_str(),&Buf) == 0) | |
334 | { | |
d546f98d MV |
335 | Res.Filename = File->Name(); |
336 | Res.LastModified = Buf.st_mtime; | |
337 | Res.IMSHit = false; | |
1dea08eb | 338 | if (curl_responsecode == 304 || curl_condition_unmet) |
714ee06c | 339 | { |
d6039f9e | 340 | unlink(File->Name().c_str()); |
d546f98d | 341 | Res.IMSHit = true; |
714ee06c | 342 | Res.LastModified = Itm->LastModified; |
d6039f9e MV |
343 | Res.Size = 0; |
344 | URIDone(Res); | |
345 | return true; | |
714ee06c | 346 | } |
d6039f9e | 347 | Res.Size = Buf.st_size; |
d546f98d MV |
348 | } |
349 | ||
350 | // take hashes | |
351 | Hashes Hash; | |
352 | FileFd Fd(Res.Filename, FileFd::ReadOnly); | |
109eb151 | 353 | Hash.AddFD(Fd); |
d546f98d MV |
354 | Res.TakeHashes(Hash); |
355 | ||
356 | // keep apt updated | |
357 | URIDone(Res); | |
358 | ||
359 | // cleanup | |
d546f98d MV |
360 | Res.Size = 0; |
361 | delete File; | |
362 | curl_slist_free_all(headers); | |
363 | ||
364 | return true; | |
365 | }; | |
366 | ||
367 | int main() | |
368 | { | |
369 | setlocale(LC_ALL, ""); | |
370 | ||
371 | HttpsMethod Mth; | |
372 | curl_global_init(CURL_GLOBAL_SSL) ; | |
373 | ||
374 | return Mth.Run(); | |
375 | } | |
376 | ||
377 |