]>
Commit | Line | Data |
---|---|---|
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 | #include <apt-pkg/netrc.h> | |
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) { | |
54 | me->Res.Size = (unsigned long)dltotal; | |
55 | me->URIStart(me->Res); | |
56 | } | |
57 | return 0; | |
58 | } | |
59 | ||
60 | void HttpsMethod::SetupProxy() | |
61 | { | |
62 | URI ServerName = Queue->Uri; | |
63 | ||
64 | // Determine the proxy setting | |
65 | string SpecificProxy = _config->Find("Acquire::http::Proxy::" + ServerName.Host); | |
66 | if (!SpecificProxy.empty()) | |
67 | { | |
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 | } | |
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 | |
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; | |
117 | char curl_errorstr[CURL_ERROR_SIZE]; | |
118 | long curl_responsecode; | |
119 | URI Uri = Itm->Uri; | |
120 | string remotehost = Uri.Host; | |
121 | ||
122 | // TODO: | |
123 | // - http::Pipeline-Depth | |
124 | // - error checking/reporting | |
125 | // - more debug options? (CURLOPT_DEBUGFUNCTION?) | |
126 | ||
127 | curl_easy_reset(curl); | |
128 | SetupProxy(); | |
129 | ||
130 | maybe_add_auth (Uri, _config->FindFile("Dir::Etc::netrc")); | |
131 | ||
132 | // callbacks | |
133 | curl_easy_setopt(curl, CURLOPT_URL, static_cast<string>(Uri).c_str()); | |
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); | |
140 | curl_easy_setopt(curl, CURLOPT_FILETIME, true); | |
141 | ||
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); | |
156 | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, peer_verify); | |
157 | ||
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 ... | |
168 | string pem = _config->Find("Acquire::https::SslCert",""); | |
169 | knob = "Acquire::https::"+remotehost+"::SslCert"; | |
170 | pem = _config->Find(knob.c_str(),pem.c_str()); | |
171 | if(pem != "") | |
172 | curl_easy_setopt(curl, CURLOPT_SSLCERT, pem.c_str()); | |
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); | |
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 | ||
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 | ||
216 | // set timeout | |
217 | int timeout = _config->FindI("Acquire::http::Timeout",120); | |
218 | curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout); | |
219 | //set really low lowspeed timeout (see #497983) | |
220 | curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, DL_MIN_SPEED); | |
221 | curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, timeout); | |
222 | ||
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 | ||
228 | // debug | |
229 | if(_config->FindB("Debug::Acquire::https", false)) | |
230 | curl_easy_setopt(curl, CURLOPT_VERBOSE, true); | |
231 | ||
232 | // error handling | |
233 | curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr); | |
234 | ||
235 | // if we have the file send an if-range query with a range header | |
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); | |
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); | |
248 | } | |
249 | ||
250 | // go for it - if the file exists, append on it | |
251 | File = new FileFd(Itm->DestFile, FileFd::WriteAny); | |
252 | if (File->Size() > 0) | |
253 | File->Seek(File->Size() - 1); | |
254 | ||
255 | // keep apt updated | |
256 | Res.Filename = Itm->DestFile; | |
257 | ||
258 | // get it! | |
259 | CURLcode success = curl_easy_perform(curl); | |
260 | curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &curl_responsecode); | |
261 | ||
262 | long curl_servdate; | |
263 | curl_easy_getinfo(curl, CURLINFO_FILETIME, &curl_servdate); | |
264 | ||
265 | // cleanup | |
266 | if(success != 0) | |
267 | { | |
268 | _error->Error("%s", curl_errorstr); | |
269 | Fail(); | |
270 | return true; | |
271 | } | |
272 | File->Close(); | |
273 | ||
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 | ||
282 | // check the downloaded result | |
283 | struct stat Buf; | |
284 | if (stat(File->Name().c_str(),&Buf) == 0) | |
285 | { | |
286 | Res.Filename = File->Name(); | |
287 | Res.LastModified = Buf.st_mtime; | |
288 | Res.IMSHit = false; | |
289 | if (curl_responsecode == 304) | |
290 | { | |
291 | unlink(File->Name().c_str()); | |
292 | Res.IMSHit = true; | |
293 | Res.LastModified = Itm->LastModified; | |
294 | Res.Size = 0; | |
295 | URIDone(Res); | |
296 | return true; | |
297 | } | |
298 | Res.Size = Buf.st_size; | |
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 | |
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 |