]>
Commit | Line | Data |
---|---|---|
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 | /*}}}*/ | |
35 | using namespace std; | |
36 | ||
37 | size_t | |
38 | HttpsMethod::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 | ||
48 | int | |
49 | HttpsMethod::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 | 60 | void 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. */ | |
112 | bool 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); |
d546f98d | 141 | |
c769cd6f MV |
142 | // SSL parameters are set by default to the common (non mirror-specific) value |
143 | // if available (or a default one) and gets overload by mirror-specific ones. | |
144 | ||
145 | // File containing the list of trusted CA. | |
146 | string cainfo = _config->Find("Acquire::https::CaInfo",""); | |
147 | string knob = "Acquire::https::"+remotehost+"::CaInfo"; | |
148 | cainfo = _config->Find(knob.c_str(),cainfo.c_str()); | |
149 | if(cainfo != "") | |
150 | curl_easy_setopt(curl, CURLOPT_CAINFO,cainfo.c_str()); | |
151 | ||
152 | // Check server certificate against previous CA list ... | |
153 | bool peer_verify = _config->FindB("Acquire::https::Verify-Peer",true); | |
154 | knob = "Acquire::https::" + remotehost + "::Verify-Peer"; | |
155 | peer_verify = _config->FindB(knob.c_str(), peer_verify); | |
714ee06c MV |
156 | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, peer_verify); |
157 | ||
c769cd6f MV |
158 | // ... and hostname against cert CN or subjectAltName |
159 | int default_verify = 2; | |
160 | bool verify = _config->FindB("Acquire::https::Verify-Host",true); | |
161 | knob = "Acquire::https::"+remotehost+"::Verify-Host"; | |
162 | verify = _config->FindB(knob.c_str(),verify); | |
163 | if (!verify) | |
164 | default_verify = 0; | |
165 | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, verify); | |
166 | ||
167 | // For client authentication, certificate file ... | |
714ee06c | 168 | string pem = _config->Find("Acquire::https::SslCert",""); |
c769cd6f MV |
169 | knob = "Acquire::https::"+remotehost+"::SslCert"; |
170 | pem = _config->Find(knob.c_str(),pem.c_str()); | |
714ee06c MV |
171 | if(pem != "") |
172 | curl_easy_setopt(curl, CURLOPT_SSLCERT, pem.c_str()); | |
c769cd6f MV |
173 | |
174 | // ... and associated key. | |
175 | string key = _config->Find("Acquire::https::SslKey",""); | |
176 | knob = "Acquire::https::"+remotehost+"::SslKey"; | |
177 | key = _config->Find(knob.c_str(),key.c_str()); | |
178 | if(key != "") | |
179 | curl_easy_setopt(curl, CURLOPT_SSLKEY, key.c_str()); | |
180 | ||
181 | // Allow forcing SSL version to SSLv3 or TLSv1 (SSLv2 is not | |
182 | // supported by GnuTLS). | |
183 | long final_version = CURL_SSLVERSION_DEFAULT; | |
184 | string sslversion = _config->Find("Acquire::https::SslForceVersion",""); | |
185 | knob = "Acquire::https::"+remotehost+"::SslForceVersion"; | |
186 | sslversion = _config->Find(knob.c_str(),sslversion.c_str()); | |
187 | if(sslversion == "TLSv1") | |
188 | final_version = CURL_SSLVERSION_TLSv1; | |
189 | else if(sslversion == "SSLv3") | |
190 | final_version = CURL_SSLVERSION_SSLv3; | |
191 | curl_easy_setopt(curl, CURLOPT_SSLVERSION, final_version); | |
d546f98d MV |
192 | |
193 | // cache-control | |
194 | if(_config->FindB("Acquire::http::No-Cache",false) == false) | |
195 | { | |
196 | // cache enabled | |
197 | if (_config->FindB("Acquire::http::No-Store",false) == true) | |
198 | headers = curl_slist_append(headers,"Cache-Control: no-store"); | |
199 | ioprintf(ss, "Cache-Control: max-age=%u", _config->FindI("Acquire::http::Max-Age",0)); | |
200 | headers = curl_slist_append(headers, ss.str().c_str()); | |
201 | } else { | |
202 | // cache disabled by user | |
203 | headers = curl_slist_append(headers, "Cache-Control: no-cache"); | |
204 | headers = curl_slist_append(headers, "Pragma: no-cache"); | |
205 | } | |
206 | curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); | |
207 | ||
d546f98d MV |
208 | // speed limit |
209 | int dlLimit = _config->FindI("Acquire::http::Dl-Limit",0)*1024; | |
210 | if (dlLimit > 0) | |
211 | curl_easy_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE, dlLimit); | |
212 | ||
213 | // set header | |
214 | curl_easy_setopt(curl, CURLOPT_USERAGENT,"Debian APT-CURL/1.0 ("VERSION")"); | |
215 | ||
cc615257 OS |
216 | // set timeout |
217 | int timeout = _config->FindI("Acquire::http::Timeout",120); | |
cc615257 | 218 | curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout); |
43cf55db | 219 | //set really low lowspeed timeout (see #497983) |
5085e660 | 220 | curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, DL_MIN_SPEED); |
43cf55db | 221 | curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, timeout); |
cc615257 | 222 | |
668ce84d MV |
223 | // set redirect options and default to 10 redirects |
224 | bool AllowRedirect = _config->FindI("Acquire::https::AllowRedirect", true); | |
225 | curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, AllowRedirect); | |
226 | curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 10); | |
227 | ||
d546f98d | 228 | // debug |
714ee06c | 229 | if(_config->FindB("Debug::Acquire::https", false)) |
d546f98d MV |
230 | curl_easy_setopt(curl, CURLOPT_VERBOSE, true); |
231 | ||
714ee06c MV |
232 | // error handling |
233 | curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr); | |
234 | ||
d6039f9e | 235 | // if we have the file send an if-range query with a range header |
4c499611 MV |
236 | if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0) |
237 | { | |
238 | char Buf[1000]; | |
239 | sprintf(Buf,"Range: bytes=%li-\r\nIf-Range: %s\r\n", | |
240 | (long)SBuf.st_size - 1, | |
241 | TimeRFC1123(SBuf.st_mtime).c_str()); | |
242 | headers = curl_slist_append(headers, Buf); | |
d6039f9e MV |
243 | } |
244 | else if(Itm->LastModified > 0) | |
245 | { | |
246 | curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE); | |
247 | curl_easy_setopt(curl, CURLOPT_TIMEVALUE, Itm->LastModified); | |
4c499611 | 248 | } |
d546f98d MV |
249 | |
250 | // go for it - if the file exists, append on it | |
251 | File = new FileFd(Itm->DestFile, FileFd::WriteAny); | |
d6039f9e MV |
252 | if (File->Size() > 0) |
253 | File->Seek(File->Size() - 1); | |
d546f98d MV |
254 | |
255 | // keep apt updated | |
256 | Res.Filename = Itm->DestFile; | |
257 | ||
258 | // get it! | |
259 | CURLcode success = curl_easy_perform(curl); | |
4c499611 | 260 | curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &curl_responsecode); |
d546f98d | 261 | |
5820530d OS |
262 | long curl_servdate; |
263 | curl_easy_getinfo(curl, CURLINFO_FILETIME, &curl_servdate); | |
264 | ||
d546f98d | 265 | // cleanup |
4c499611 MV |
266 | if(success != 0) |
267 | { | |
9b5d79ec | 268 | _error->Error("%s", curl_errorstr); |
d546f98d MV |
269 | Fail(); |
270 | return true; | |
271 | } | |
4c499611 | 272 | File->Close(); |
d546f98d | 273 | |
5820530d OS |
274 | // Timestamp |
275 | struct utimbuf UBuf; | |
276 | if (curl_servdate != -1) { | |
277 | UBuf.actime = curl_servdate; | |
278 | UBuf.modtime = curl_servdate; | |
279 | utime(File->Name().c_str(),&UBuf); | |
280 | } | |
281 | ||
d546f98d MV |
282 | // check the downloaded result |
283 | struct stat Buf; | |
284 | if (stat(File->Name().c_str(),&Buf) == 0) | |
285 | { | |
d546f98d MV |
286 | Res.Filename = File->Name(); |
287 | Res.LastModified = Buf.st_mtime; | |
288 | Res.IMSHit = false; | |
4c499611 | 289 | if (curl_responsecode == 304) |
714ee06c | 290 | { |
d6039f9e | 291 | unlink(File->Name().c_str()); |
d546f98d | 292 | Res.IMSHit = true; |
714ee06c | 293 | Res.LastModified = Itm->LastModified; |
d6039f9e MV |
294 | Res.Size = 0; |
295 | URIDone(Res); | |
296 | return true; | |
714ee06c | 297 | } |
d6039f9e | 298 | Res.Size = Buf.st_size; |
d546f98d MV |
299 | } |
300 | ||
301 | // take hashes | |
302 | Hashes Hash; | |
303 | FileFd Fd(Res.Filename, FileFd::ReadOnly); | |
304 | Hash.AddFD(Fd.Fd(), Fd.Size()); | |
305 | Res.TakeHashes(Hash); | |
306 | ||
307 | // keep apt updated | |
308 | URIDone(Res); | |
309 | ||
310 | // cleanup | |
d546f98d MV |
311 | Res.Size = 0; |
312 | delete File; | |
313 | curl_slist_free_all(headers); | |
314 | ||
315 | return true; | |
316 | }; | |
317 | ||
318 | int main() | |
319 | { | |
320 | setlocale(LC_ALL, ""); | |
321 | ||
322 | HttpsMethod Mth; | |
323 | curl_global_init(CURL_GLOBAL_SSL) ; | |
324 | ||
325 | return Mth.Run(); | |
326 | } | |
327 | ||
328 |