1 //-*- mode: cpp; mode: fold -*-
3 // $Id: http.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $
4 /* ######################################################################
6 HTTPS Acquire Method - This is the HTTPS aquire method for APT.
10 ##################################################################### */
12 // Include Files /*{{{*/
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>
19 #include <apt-pkg/netrc.h>
20 #include <apt-pkg/configuration.h>
40 HttpsMethod::parse_header(void *buffer
, size_t size
, size_t nmemb
, void *userp
)
42 size_t len
= size
* nmemb
;
43 HttpsMethod
*me
= (HttpsMethod
*)userp
;
44 std::string
line((char*) buffer
, len
);
45 for (--len
; len
> 0; --len
)
46 if (isspace(line
[len
]) == 0)
53 if (line
.empty() == true)
55 if (me
->Server
->Result
!= 416 && me
->Server
->StartPos
!= 0)
57 else if (me
->Server
->Result
== 416 && me
->Server
->Size
== me
->File
->FileSize())
59 me
->Server
->Result
= 200;
60 me
->Server
->StartPos
= me
->Server
->Size
;
63 me
->Server
->StartPos
= 0;
65 me
->File
->Truncate(me
->Server
->StartPos
);
66 me
->File
->Seek(me
->Server
->StartPos
);
68 else if (me
->Server
->HeaderLine(line
) == false)
75 HttpsMethod::write_data(void *buffer
, size_t size
, size_t nmemb
, void *userp
)
77 HttpsMethod
*me
= (HttpsMethod
*)userp
;
79 if(me
->File
->Write(buffer
, size
*nmemb
) != true)
86 HttpsMethod::progress_callback(void *clientp
, double dltotal
, double dlnow
,
87 double ultotal
, double ulnow
)
89 HttpsMethod
*me
= (HttpsMethod
*)clientp
;
90 if(dltotal
> 0 && me
->Res
.Size
== 0) {
91 me
->Res
.Size
= (unsigned long long)dltotal
;
92 me
->URIStart(me
->Res
);
97 // HttpsServerState::HttpsServerState - Constructor /*{{{*/
98 HttpsServerState::HttpsServerState(URI Srv
,HttpsMethod
*Owner
) : ServerState(Srv
, NULL
)
100 TimeOut
= _config
->FindI("Acquire::https::Timeout",TimeOut
);
105 void HttpsMethod::SetupProxy() /*{{{*/
107 URI ServerName
= Queue
->Uri
;
109 // Curl should never read proxy settings from the environment, as
110 // we determine which proxy to use. Do this for consistency among
111 // methods and prevent an environment variable overriding a
112 // no-proxy ("DIRECT") setting in apt.conf.
113 curl_easy_setopt(curl
, CURLOPT_PROXY
, "");
115 // Determine the proxy setting - try https first, fallback to http and use env at last
116 string UseProxy
= _config
->Find("Acquire::https::Proxy::" + ServerName
.Host
,
117 _config
->Find("Acquire::http::Proxy::" + ServerName
.Host
).c_str());
119 if (UseProxy
.empty() == true)
120 UseProxy
= _config
->Find("Acquire::https::Proxy", _config
->Find("Acquire::http::Proxy").c_str());
122 // User want to use NO proxy, so nothing to setup
123 if (UseProxy
== "DIRECT")
126 if (UseProxy
.empty() == false)
128 // Parse no_proxy, a comma (,) separated list of domains we don't want to use
129 // a proxy for so we stop right here if it is in the list
130 if (getenv("no_proxy") != 0 && CheckDomainList(ServerName
.Host
,getenv("no_proxy")) == true)
133 const char* result
= getenv("https_proxy");
134 // FIXME: Fall back to http_proxy is to remain compatible with
135 // existing setups and behaviour of apt.conf. This should be
136 // deprecated in the future (including apt.conf). Most other
137 // programs do not fall back to http proxy settings and neither
140 result
= getenv("http_proxy");
141 UseProxy
= result
== NULL
? "" : result
;
144 // Determine what host and port to use based on the proxy settings
145 if (UseProxy
.empty() == false)
149 curl_easy_setopt(curl
, CURLOPT_PROXYPORT
, Proxy
.Port
);
150 curl_easy_setopt(curl
, CURLOPT_PROXY
, Proxy
.Host
.c_str());
151 if (Proxy
.User
.empty() == false || Proxy
.Password
.empty() == false)
153 curl_easy_setopt(curl
, CURLOPT_PROXYUSERNAME
, Proxy
.User
.c_str());
154 curl_easy_setopt(curl
, CURLOPT_PROXYPASSWORD
, Proxy
.Password
.c_str());
158 // HttpsMethod::Fetch - Fetch an item /*{{{*/
159 // ---------------------------------------------------------------------
160 /* This adds an item to the pipeline. We keep the pipeline at a fixed
162 bool HttpsMethod::Fetch(FetchItem
*Itm
)
165 struct curl_slist
*headers
=NULL
;
166 char curl_errorstr
[CURL_ERROR_SIZE
];
168 string remotehost
= Uri
.Host
;
171 // - http::Pipeline-Depth
172 // - error checking/reporting
173 // - more debug options? (CURLOPT_DEBUGFUNCTION?)
175 curl_easy_reset(curl
);
178 maybe_add_auth (Uri
, _config
->FindFile("Dir::Etc::netrc"));
181 curl_easy_setopt(curl
, CURLOPT_URL
, static_cast<string
>(Uri
).c_str());
182 curl_easy_setopt(curl
, CURLOPT_HEADERFUNCTION
, parse_header
);
183 curl_easy_setopt(curl
, CURLOPT_WRITEHEADER
, this);
184 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, write_data
);
185 curl_easy_setopt(curl
, CURLOPT_WRITEDATA
, this);
186 curl_easy_setopt(curl
, CURLOPT_PROGRESSFUNCTION
, progress_callback
);
187 curl_easy_setopt(curl
, CURLOPT_PROGRESSDATA
, this);
188 curl_easy_setopt(curl
, CURLOPT_NOPROGRESS
, false);
189 curl_easy_setopt(curl
, CURLOPT_FILETIME
, true);
191 // SSL parameters are set by default to the common (non mirror-specific) value
192 // if available (or a default one) and gets overload by mirror-specific ones.
194 // File containing the list of trusted CA.
195 string cainfo
= _config
->Find("Acquire::https::CaInfo","");
196 string knob
= "Acquire::https::"+remotehost
+"::CaInfo";
197 cainfo
= _config
->Find(knob
.c_str(),cainfo
.c_str());
198 if(cainfo
.empty() == false)
199 curl_easy_setopt(curl
, CURLOPT_CAINFO
,cainfo
.c_str());
201 // Check server certificate against previous CA list ...
202 bool peer_verify
= _config
->FindB("Acquire::https::Verify-Peer",true);
203 knob
= "Acquire::https::" + remotehost
+ "::Verify-Peer";
204 peer_verify
= _config
->FindB(knob
.c_str(), peer_verify
);
205 curl_easy_setopt(curl
, CURLOPT_SSL_VERIFYPEER
, peer_verify
);
207 // ... and hostname against cert CN or subjectAltName
208 bool verify
= _config
->FindB("Acquire::https::Verify-Host",true);
209 knob
= "Acquire::https::"+remotehost
+"::Verify-Host";
210 verify
= _config
->FindB(knob
.c_str(),verify
);
211 int const default_verify
= (verify
== true) ? 2 : 0;
212 curl_easy_setopt(curl
, CURLOPT_SSL_VERIFYHOST
, default_verify
);
214 // Also enforce issuer of server certificate using its cert
215 string issuercert
= _config
->Find("Acquire::https::IssuerCert","");
216 knob
= "Acquire::https::"+remotehost
+"::IssuerCert";
217 issuercert
= _config
->Find(knob
.c_str(),issuercert
.c_str());
218 if(issuercert
.empty() == false)
219 curl_easy_setopt(curl
, CURLOPT_ISSUERCERT
,issuercert
.c_str());
221 // For client authentication, certificate file ...
222 string pem
= _config
->Find("Acquire::https::SslCert","");
223 knob
= "Acquire::https::"+remotehost
+"::SslCert";
224 pem
= _config
->Find(knob
.c_str(),pem
.c_str());
225 if(pem
.empty() == false)
226 curl_easy_setopt(curl
, CURLOPT_SSLCERT
, pem
.c_str());
228 // ... and associated key.
229 string key
= _config
->Find("Acquire::https::SslKey","");
230 knob
= "Acquire::https::"+remotehost
+"::SslKey";
231 key
= _config
->Find(knob
.c_str(),key
.c_str());
232 if(key
.empty() == false)
233 curl_easy_setopt(curl
, CURLOPT_SSLKEY
, key
.c_str());
235 // Allow forcing SSL version to SSLv3 or TLSv1 (SSLv2 is not
236 // supported by GnuTLS).
237 long final_version
= CURL_SSLVERSION_DEFAULT
;
238 string sslversion
= _config
->Find("Acquire::https::SslForceVersion","");
239 knob
= "Acquire::https::"+remotehost
+"::SslForceVersion";
240 sslversion
= _config
->Find(knob
.c_str(),sslversion
.c_str());
241 if(sslversion
== "TLSv1")
242 final_version
= CURL_SSLVERSION_TLSv1
;
243 else if(sslversion
== "SSLv3")
244 final_version
= CURL_SSLVERSION_SSLv3
;
245 curl_easy_setopt(curl
, CURLOPT_SSLVERSION
, final_version
);
248 string crlfile
= _config
->Find("Acquire::https::CrlFile","");
249 knob
= "Acquire::https::"+remotehost
+"::CrlFile";
250 crlfile
= _config
->Find(knob
.c_str(),crlfile
.c_str());
251 if(crlfile
.empty() == false)
252 curl_easy_setopt(curl
, CURLOPT_CRLFILE
, crlfile
.c_str());
255 if(_config
->FindB("Acquire::https::No-Cache",
256 _config
->FindB("Acquire::http::No-Cache",false)) == false)
259 if (_config
->FindB("Acquire::https::No-Store",
260 _config
->FindB("Acquire::http::No-Store",false)) == true)
261 headers
= curl_slist_append(headers
,"Cache-Control: no-store");
263 ioprintf(ss
, "Cache-Control: max-age=%u", _config
->FindI("Acquire::https::Max-Age",
264 _config
->FindI("Acquire::http::Max-Age",0)));
265 headers
= curl_slist_append(headers
, ss
.str().c_str());
267 // cache disabled by user
268 headers
= curl_slist_append(headers
, "Cache-Control: no-cache");
269 headers
= curl_slist_append(headers
, "Pragma: no-cache");
271 curl_easy_setopt(curl
, CURLOPT_HTTPHEADER
, headers
);
274 int const dlLimit
= _config
->FindI("Acquire::https::Dl-Limit",
275 _config
->FindI("Acquire::http::Dl-Limit",0))*1024;
277 curl_easy_setopt(curl
, CURLOPT_MAX_RECV_SPEED_LARGE
, dlLimit
);
280 curl_easy_setopt(curl
, CURLOPT_USERAGENT
,
281 _config
->Find("Acquire::https::User-Agent",
282 _config
->Find("Acquire::http::User-Agent",
283 "Debian APT-CURL/1.0 (" PACKAGE_VERSION
")").c_str()).c_str());
286 int const timeout
= _config
->FindI("Acquire::https::Timeout",
287 _config
->FindI("Acquire::http::Timeout",120));
288 curl_easy_setopt(curl
, CURLOPT_CONNECTTIMEOUT
, timeout
);
289 //set really low lowspeed timeout (see #497983)
290 curl_easy_setopt(curl
, CURLOPT_LOW_SPEED_LIMIT
, DL_MIN_SPEED
);
291 curl_easy_setopt(curl
, CURLOPT_LOW_SPEED_TIME
, timeout
);
293 // set redirect options and default to 10 redirects
294 bool const AllowRedirect
= _config
->FindB("Acquire::https::AllowRedirect",
295 _config
->FindB("Acquire::http::AllowRedirect",true));
296 curl_easy_setopt(curl
, CURLOPT_FOLLOWLOCATION
, AllowRedirect
);
297 curl_easy_setopt(curl
, CURLOPT_MAXREDIRS
, 10);
300 if(_config
->FindB("Debug::Acquire::https", false))
301 curl_easy_setopt(curl
, CURLOPT_VERBOSE
, true);
304 curl_errorstr
[0] = '\0';
305 curl_easy_setopt(curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
307 // If we ask for uncompressed files servers might respond with content-
308 // negotation which lets us end up with compressed files we do not support,
309 // see 657029, 657560 and co, so if we have no extension on the request
310 // ask for text only. As a sidenote: If there is nothing to negotate servers
311 // seem to be nice and ignore it.
312 if (_config
->FindB("Acquire::https::SendAccept", _config
->FindB("Acquire::http::SendAccept", true)) == true)
314 size_t const filepos
= Itm
->Uri
.find_last_of('/');
315 string
const file
= Itm
->Uri
.substr(filepos
+ 1);
316 if (flExtension(file
) == file
)
317 headers
= curl_slist_append(headers
, "Accept: text/*");
320 // if we have the file send an if-range query with a range header
321 if (stat(Itm
->DestFile
.c_str(),&SBuf
) >= 0 && SBuf
.st_size
> 0)
324 sprintf(Buf
, "Range: bytes=%li-", (long) SBuf
.st_size
);
325 headers
= curl_slist_append(headers
, Buf
);
326 sprintf(Buf
, "If-Range: %s", TimeRFC1123(SBuf
.st_mtime
).c_str());
327 headers
= curl_slist_append(headers
, Buf
);
329 else if(Itm
->LastModified
> 0)
331 curl_easy_setopt(curl
, CURLOPT_TIMECONDITION
, CURL_TIMECOND_IFMODSINCE
);
332 curl_easy_setopt(curl
, CURLOPT_TIMEVALUE
, Itm
->LastModified
);
335 // go for it - if the file exists, append on it
336 File
= new FileFd(Itm
->DestFile
, FileFd::WriteAny
);
337 Server
= new HttpsServerState(Itm
->Uri
, this);
340 Res
.Filename
= Itm
->DestFile
;
343 CURLcode success
= curl_easy_perform(curl
);
345 // If the server returns 200 OK but the If-Modified-Since condition is not
346 // met, CURLINFO_CONDITION_UNMET will be set to 1
347 long curl_condition_unmet
= 0;
348 curl_easy_getinfo(curl
, CURLINFO_CONDITION_UNMET
, &curl_condition_unmet
);
351 curl_slist_free_all(headers
);
356 _error
->Error("%s", curl_errorstr
);
357 unlink(File
->Name().c_str());
361 // server says file not modified
362 if (Server
->Result
== 304 || curl_condition_unmet
== 1)
364 unlink(File
->Name().c_str());
366 Res
.LastModified
= Itm
->LastModified
;
373 if (Server
->Result
!= 200 && // OK
374 Server
->Result
!= 206 && // Partial
375 Server
->Result
!= 416) // invalid Range
378 snprintf(err
, sizeof(err
) - 1, "HttpError%i", Server
->Result
);
380 _error
->Error("%s", err
);
381 // unlink, no need keep 401/404 page content in partial/
382 unlink(File
->Name().c_str());
386 struct stat resultStat
;
387 if (unlikely(stat(File
->Name().c_str(), &resultStat
) != 0))
389 _error
->Errno("stat", "Unable to access file %s", File
->Name().c_str());
392 Res
.Size
= resultStat
.st_size
;
394 // invalid range-request
395 if (Server
->Result
== 416)
397 unlink(File
->Name().c_str());
405 curl_easy_getinfo(curl
, CURLINFO_FILETIME
, &Res
.LastModified
);
406 if (Res
.LastModified
!= -1)
409 UBuf
.actime
= Res
.LastModified
;
410 UBuf
.modtime
= Res
.LastModified
;
411 utime(File
->Name().c_str(),&UBuf
);
414 Res
.LastModified
= resultStat
.st_mtime
;
418 FileFd
Fd(Res
.Filename
, FileFd::ReadOnly
);
420 Res
.TakeHashes(Hash
);
434 setlocale(LC_ALL
, "");
437 curl_global_init(CURL_GLOBAL_SSL
) ;