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