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