| 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 acquire method for APT. |
| 7 | |
| 8 | It uses libcurl |
| 9 | |
| 10 | ##################################################################### */ |
| 11 | /*}}}*/ |
| 12 | // Include Files /*{{{*/ |
| 13 | #include <config.h> |
| 14 | |
| 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> |
| 21 | #include <apt-pkg/macros.h> |
| 22 | #include <apt-pkg/strutl.h> |
| 23 | #include <apt-pkg/proxy.h> |
| 24 | |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/time.h> |
| 27 | #include <unistd.h> |
| 28 | #include <stdio.h> |
| 29 | #include <iostream> |
| 30 | #include <sstream> |
| 31 | #include <ctype.h> |
| 32 | #include <stdlib.h> |
| 33 | |
| 34 | #include "https.h" |
| 35 | |
| 36 | #include <apti18n.h> |
| 37 | /*}}}*/ |
| 38 | using namespace std; |
| 39 | |
| 40 | size_t |
| 41 | HttpsMethod::parse_header(void *buffer, size_t size, size_t nmemb, void *userp) |
| 42 | { |
| 43 | size_t len = size * nmemb; |
| 44 | HttpsMethod *me = (HttpsMethod *)userp; |
| 45 | std::string line((char*) buffer, len); |
| 46 | for (--len; len > 0; --len) |
| 47 | if (isspace(line[len]) == 0) |
| 48 | { |
| 49 | ++len; |
| 50 | break; |
| 51 | } |
| 52 | line.erase(len); |
| 53 | |
| 54 | if (line.empty() == true) |
| 55 | { |
| 56 | if (me->Server->Result != 416 && me->Server->StartPos != 0) |
| 57 | ; |
| 58 | else if (me->Server->Result == 416 && me->Server->Size == me->File->FileSize()) |
| 59 | { |
| 60 | me->Server->Result = 200; |
| 61 | me->Server->StartPos = me->Server->Size; |
| 62 | } |
| 63 | else |
| 64 | me->Server->StartPos = 0; |
| 65 | |
| 66 | me->File->Truncate(me->Server->StartPos); |
| 67 | me->File->Seek(me->Server->StartPos); |
| 68 | } |
| 69 | else if (me->Server->HeaderLine(line) == false) |
| 70 | return 0; |
| 71 | |
| 72 | return size*nmemb; |
| 73 | } |
| 74 | |
| 75 | size_t |
| 76 | HttpsMethod::write_data(void *buffer, size_t size, size_t nmemb, void *userp) |
| 77 | { |
| 78 | HttpsMethod *me = (HttpsMethod *)userp; |
| 79 | |
| 80 | if (me->Res.Size == 0) |
| 81 | me->URIStart(me->Res); |
| 82 | if(me->File->Write(buffer, size*nmemb) != true) |
| 83 | return false; |
| 84 | |
| 85 | return size*nmemb; |
| 86 | } |
| 87 | |
| 88 | int |
| 89 | HttpsMethod::progress_callback(void *clientp, double dltotal, double /*dlnow*/, |
| 90 | double /*ultotal*/, double /*ulnow*/) |
| 91 | { |
| 92 | HttpsMethod *me = (HttpsMethod *)clientp; |
| 93 | if(dltotal > 0 && me->Res.Size == 0) { |
| 94 | me->Res.Size = (unsigned long long)dltotal; |
| 95 | } |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | // HttpsServerState::HttpsServerState - Constructor /*{{{*/ |
| 100 | HttpsServerState::HttpsServerState(URI Srv,HttpsMethod * /*Owner*/) : ServerState(Srv, NULL) |
| 101 | { |
| 102 | TimeOut = _config->FindI("Acquire::https::Timeout",TimeOut); |
| 103 | Reset(); |
| 104 | } |
| 105 | /*}}}*/ |
| 106 | |
| 107 | void HttpsMethod::SetupProxy() /*{{{*/ |
| 108 | { |
| 109 | URI ServerName = Queue->Uri; |
| 110 | |
| 111 | // Determine the proxy setting |
| 112 | AutoDetectProxy(ServerName); |
| 113 | |
| 114 | // Curl should never read proxy settings from the environment, as |
| 115 | // we determine which proxy to use. Do this for consistency among |
| 116 | // methods and prevent an environment variable overriding a |
| 117 | // no-proxy ("DIRECT") setting in apt.conf. |
| 118 | curl_easy_setopt(curl, CURLOPT_PROXY, ""); |
| 119 | |
| 120 | // Determine the proxy setting - try https first, fallback to http and use env at last |
| 121 | string UseProxy = _config->Find("Acquire::https::Proxy::" + ServerName.Host, |
| 122 | _config->Find("Acquire::http::Proxy::" + ServerName.Host).c_str()); |
| 123 | |
| 124 | if (UseProxy.empty() == true) |
| 125 | UseProxy = _config->Find("Acquire::https::Proxy", _config->Find("Acquire::http::Proxy").c_str()); |
| 126 | |
| 127 | // User want to use NO proxy, so nothing to setup |
| 128 | if (UseProxy == "DIRECT") |
| 129 | return; |
| 130 | |
| 131 | if (UseProxy.empty() == false) |
| 132 | { |
| 133 | // Parse no_proxy, a comma (,) separated list of domains we don't want to use |
| 134 | // a proxy for so we stop right here if it is in the list |
| 135 | if (getenv("no_proxy") != 0 && CheckDomainList(ServerName.Host,getenv("no_proxy")) == true) |
| 136 | return; |
| 137 | } else { |
| 138 | const char* result = getenv("https_proxy"); |
| 139 | // FIXME: Fall back to http_proxy is to remain compatible with |
| 140 | // existing setups and behaviour of apt.conf. This should be |
| 141 | // deprecated in the future (including apt.conf). Most other |
| 142 | // programs do not fall back to http proxy settings and neither |
| 143 | // should Apt. |
| 144 | if (result == NULL) |
| 145 | result = getenv("http_proxy"); |
| 146 | UseProxy = result == NULL ? "" : result; |
| 147 | } |
| 148 | |
| 149 | // Determine what host and port to use based on the proxy settings |
| 150 | if (UseProxy.empty() == false) |
| 151 | { |
| 152 | Proxy = UseProxy; |
| 153 | if (Proxy.Port != 1) |
| 154 | curl_easy_setopt(curl, CURLOPT_PROXYPORT, Proxy.Port); |
| 155 | curl_easy_setopt(curl, CURLOPT_PROXY, Proxy.Host.c_str()); |
| 156 | if (Proxy.User.empty() == false || Proxy.Password.empty() == false) |
| 157 | { |
| 158 | curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME, Proxy.User.c_str()); |
| 159 | curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD, Proxy.Password.c_str()); |
| 160 | } |
| 161 | } |
| 162 | } /*}}}*/ |
| 163 | // HttpsMethod::Fetch - Fetch an item /*{{{*/ |
| 164 | // --------------------------------------------------------------------- |
| 165 | /* This adds an item to the pipeline. We keep the pipeline at a fixed |
| 166 | depth. */ |
| 167 | bool HttpsMethod::Fetch(FetchItem *Itm) |
| 168 | { |
| 169 | struct stat SBuf; |
| 170 | struct curl_slist *headers=NULL; |
| 171 | char curl_errorstr[CURL_ERROR_SIZE]; |
| 172 | URI Uri = Itm->Uri; |
| 173 | string remotehost = Uri.Host; |
| 174 | |
| 175 | // TODO: |
| 176 | // - http::Pipeline-Depth |
| 177 | // - error checking/reporting |
| 178 | // - more debug options? (CURLOPT_DEBUGFUNCTION?) |
| 179 | |
| 180 | curl_easy_reset(curl); |
| 181 | SetupProxy(); |
| 182 | |
| 183 | maybe_add_auth (Uri, _config->FindFile("Dir::Etc::netrc")); |
| 184 | |
| 185 | // callbacks |
| 186 | curl_easy_setopt(curl, CURLOPT_URL, static_cast<string>(Uri).c_str()); |
| 187 | curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, parse_header); |
| 188 | curl_easy_setopt(curl, CURLOPT_WRITEHEADER, this); |
| 189 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); |
| 190 | curl_easy_setopt(curl, CURLOPT_WRITEDATA, this); |
| 191 | curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback); |
| 192 | curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, this); |
| 193 | // options |
| 194 | curl_easy_setopt(curl, CURLOPT_NOPROGRESS, false); |
| 195 | curl_easy_setopt(curl, CURLOPT_FILETIME, true); |
| 196 | // only allow curl to handle https, not the other stuff it supports |
| 197 | curl_easy_setopt(curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS); |
| 198 | curl_easy_setopt(curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS); |
| 199 | |
| 200 | // SSL parameters are set by default to the common (non mirror-specific) value |
| 201 | // if available (or a default one) and gets overload by mirror-specific ones. |
| 202 | |
| 203 | // File containing the list of trusted CA. |
| 204 | string cainfo = _config->Find("Acquire::https::CaInfo",""); |
| 205 | string knob = "Acquire::https::"+remotehost+"::CaInfo"; |
| 206 | cainfo = _config->Find(knob.c_str(),cainfo.c_str()); |
| 207 | if(cainfo.empty() == false) |
| 208 | curl_easy_setopt(curl, CURLOPT_CAINFO,cainfo.c_str()); |
| 209 | |
| 210 | // Check server certificate against previous CA list ... |
| 211 | bool peer_verify = _config->FindB("Acquire::https::Verify-Peer",true); |
| 212 | knob = "Acquire::https::" + remotehost + "::Verify-Peer"; |
| 213 | peer_verify = _config->FindB(knob.c_str(), peer_verify); |
| 214 | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, peer_verify); |
| 215 | |
| 216 | // ... and hostname against cert CN or subjectAltName |
| 217 | bool verify = _config->FindB("Acquire::https::Verify-Host",true); |
| 218 | knob = "Acquire::https::"+remotehost+"::Verify-Host"; |
| 219 | verify = _config->FindB(knob.c_str(),verify); |
| 220 | int const default_verify = (verify == true) ? 2 : 0; |
| 221 | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, default_verify); |
| 222 | |
| 223 | // Also enforce issuer of server certificate using its cert |
| 224 | string issuercert = _config->Find("Acquire::https::IssuerCert",""); |
| 225 | knob = "Acquire::https::"+remotehost+"::IssuerCert"; |
| 226 | issuercert = _config->Find(knob.c_str(),issuercert.c_str()); |
| 227 | if(issuercert.empty() == false) |
| 228 | curl_easy_setopt(curl, CURLOPT_ISSUERCERT,issuercert.c_str()); |
| 229 | |
| 230 | // For client authentication, certificate file ... |
| 231 | string pem = _config->Find("Acquire::https::SslCert",""); |
| 232 | knob = "Acquire::https::"+remotehost+"::SslCert"; |
| 233 | pem = _config->Find(knob.c_str(),pem.c_str()); |
| 234 | if(pem.empty() == false) |
| 235 | curl_easy_setopt(curl, CURLOPT_SSLCERT, pem.c_str()); |
| 236 | |
| 237 | // ... and associated key. |
| 238 | string key = _config->Find("Acquire::https::SslKey",""); |
| 239 | knob = "Acquire::https::"+remotehost+"::SslKey"; |
| 240 | key = _config->Find(knob.c_str(),key.c_str()); |
| 241 | if(key.empty() == false) |
| 242 | curl_easy_setopt(curl, CURLOPT_SSLKEY, key.c_str()); |
| 243 | |
| 244 | // Allow forcing SSL version to SSLv3 or TLSv1 (SSLv2 is not |
| 245 | // supported by GnuTLS). |
| 246 | long final_version = CURL_SSLVERSION_DEFAULT; |
| 247 | string sslversion = _config->Find("Acquire::https::SslForceVersion",""); |
| 248 | knob = "Acquire::https::"+remotehost+"::SslForceVersion"; |
| 249 | sslversion = _config->Find(knob.c_str(),sslversion.c_str()); |
| 250 | if(sslversion == "TLSv1") |
| 251 | final_version = CURL_SSLVERSION_TLSv1; |
| 252 | else if(sslversion == "SSLv3") |
| 253 | final_version = CURL_SSLVERSION_SSLv3; |
| 254 | curl_easy_setopt(curl, CURLOPT_SSLVERSION, final_version); |
| 255 | |
| 256 | // CRL file |
| 257 | string crlfile = _config->Find("Acquire::https::CrlFile",""); |
| 258 | knob = "Acquire::https::"+remotehost+"::CrlFile"; |
| 259 | crlfile = _config->Find(knob.c_str(),crlfile.c_str()); |
| 260 | if(crlfile.empty() == false) |
| 261 | curl_easy_setopt(curl, CURLOPT_CRLFILE, crlfile.c_str()); |
| 262 | |
| 263 | // cache-control |
| 264 | if(_config->FindB("Acquire::https::No-Cache", |
| 265 | _config->FindB("Acquire::http::No-Cache",false)) == false) |
| 266 | { |
| 267 | // cache enabled |
| 268 | if (_config->FindB("Acquire::https::No-Store", |
| 269 | _config->FindB("Acquire::http::No-Store",false)) == true) |
| 270 | headers = curl_slist_append(headers,"Cache-Control: no-store"); |
| 271 | stringstream ss; |
| 272 | ioprintf(ss, "Cache-Control: max-age=%u", _config->FindI("Acquire::https::Max-Age", |
| 273 | _config->FindI("Acquire::http::Max-Age",0))); |
| 274 | headers = curl_slist_append(headers, ss.str().c_str()); |
| 275 | } else { |
| 276 | // cache disabled by user |
| 277 | headers = curl_slist_append(headers, "Cache-Control: no-cache"); |
| 278 | headers = curl_slist_append(headers, "Pragma: no-cache"); |
| 279 | } |
| 280 | curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); |
| 281 | |
| 282 | // speed limit |
| 283 | int const dlLimit = _config->FindI("Acquire::https::Dl-Limit", |
| 284 | _config->FindI("Acquire::http::Dl-Limit",0))*1024; |
| 285 | if (dlLimit > 0) |
| 286 | curl_easy_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE, dlLimit); |
| 287 | |
| 288 | // set header |
| 289 | curl_easy_setopt(curl, CURLOPT_USERAGENT, |
| 290 | _config->Find("Acquire::https::User-Agent", |
| 291 | _config->Find("Acquire::http::User-Agent", |
| 292 | "Debian APT-CURL/1.0 (" PACKAGE_VERSION ")").c_str()).c_str()); |
| 293 | |
| 294 | // set timeout |
| 295 | int const timeout = _config->FindI("Acquire::https::Timeout", |
| 296 | _config->FindI("Acquire::http::Timeout",120)); |
| 297 | curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout); |
| 298 | //set really low lowspeed timeout (see #497983) |
| 299 | curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, DL_MIN_SPEED); |
| 300 | curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, timeout); |
| 301 | |
| 302 | // set redirect options and default to 10 redirects |
| 303 | bool const AllowRedirect = _config->FindB("Acquire::https::AllowRedirect", |
| 304 | _config->FindB("Acquire::http::AllowRedirect",true)); |
| 305 | curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, AllowRedirect); |
| 306 | curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 10); |
| 307 | |
| 308 | // debug |
| 309 | if(_config->FindB("Debug::Acquire::https", false)) |
| 310 | curl_easy_setopt(curl, CURLOPT_VERBOSE, true); |
| 311 | |
| 312 | // error handling |
| 313 | curl_errorstr[0] = '\0'; |
| 314 | curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr); |
| 315 | |
| 316 | // If we ask for uncompressed files servers might respond with content- |
| 317 | // negotiation which lets us end up with compressed files we do not support, |
| 318 | // see 657029, 657560 and co, so if we have no extension on the request |
| 319 | // ask for text only. As a sidenote: If there is nothing to negotate servers |
| 320 | // seem to be nice and ignore it. |
| 321 | if (_config->FindB("Acquire::https::SendAccept", _config->FindB("Acquire::http::SendAccept", true)) == true) |
| 322 | { |
| 323 | size_t const filepos = Itm->Uri.find_last_of('/'); |
| 324 | string const file = Itm->Uri.substr(filepos + 1); |
| 325 | if (flExtension(file) == file) |
| 326 | headers = curl_slist_append(headers, "Accept: text/*"); |
| 327 | } |
| 328 | |
| 329 | // if we have the file send an if-range query with a range header |
| 330 | if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0) |
| 331 | { |
| 332 | std::string Buf; |
| 333 | strprintf(Buf, "Range: bytes=%lli-", (long long) SBuf.st_size); |
| 334 | headers = curl_slist_append(headers, Buf.c_str()); |
| 335 | strprintf(Buf, "If-Range: %s", TimeRFC1123(SBuf.st_mtime).c_str()); |
| 336 | headers = curl_slist_append(headers, Buf.c_str()); |
| 337 | } |
| 338 | else if(Itm->LastModified > 0) |
| 339 | { |
| 340 | curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE); |
| 341 | curl_easy_setopt(curl, CURLOPT_TIMEVALUE, Itm->LastModified); |
| 342 | } |
| 343 | |
| 344 | // go for it - if the file exists, append on it |
| 345 | File = new FileFd(Itm->DestFile, FileFd::WriteAny); |
| 346 | Server = new HttpsServerState(Itm->Uri, this); |
| 347 | |
| 348 | // keep apt updated |
| 349 | Res.Filename = Itm->DestFile; |
| 350 | |
| 351 | // get it! |
| 352 | CURLcode success = curl_easy_perform(curl); |
| 353 | |
| 354 | // If the server returns 200 OK but the If-Modified-Since condition is not |
| 355 | // met, CURLINFO_CONDITION_UNMET will be set to 1 |
| 356 | long curl_condition_unmet = 0; |
| 357 | curl_easy_getinfo(curl, CURLINFO_CONDITION_UNMET, &curl_condition_unmet); |
| 358 | |
| 359 | File->Close(); |
| 360 | curl_slist_free_all(headers); |
| 361 | |
| 362 | // cleanup |
| 363 | if (success != 0) |
| 364 | { |
| 365 | _error->Error("%s", curl_errorstr); |
| 366 | unlink(File->Name().c_str()); |
| 367 | return false; |
| 368 | } |
| 369 | |
| 370 | // server says file not modified |
| 371 | if (Server->Result == 304 || curl_condition_unmet == 1) |
| 372 | { |
| 373 | unlink(File->Name().c_str()); |
| 374 | Res.IMSHit = true; |
| 375 | Res.LastModified = Itm->LastModified; |
| 376 | Res.Size = 0; |
| 377 | URIDone(Res); |
| 378 | return true; |
| 379 | } |
| 380 | Res.IMSHit = false; |
| 381 | |
| 382 | if (Server->Result != 200 && // OK |
| 383 | Server->Result != 206 && // Partial |
| 384 | Server->Result != 416) // invalid Range |
| 385 | { |
| 386 | char err[255]; |
| 387 | snprintf(err, sizeof(err) - 1, "HttpError%i", Server->Result); |
| 388 | SetFailReason(err); |
| 389 | _error->Error("%s", err); |
| 390 | // unlink, no need keep 401/404 page content in partial/ |
| 391 | unlink(File->Name().c_str()); |
| 392 | return false; |
| 393 | } |
| 394 | |
| 395 | struct stat resultStat; |
| 396 | if (unlikely(stat(File->Name().c_str(), &resultStat) != 0)) |
| 397 | { |
| 398 | _error->Errno("stat", "Unable to access file %s", File->Name().c_str()); |
| 399 | return false; |
| 400 | } |
| 401 | Res.Size = resultStat.st_size; |
| 402 | |
| 403 | // invalid range-request |
| 404 | if (Server->Result == 416) |
| 405 | { |
| 406 | unlink(File->Name().c_str()); |
| 407 | Res.Size = 0; |
| 408 | delete File; |
| 409 | Redirect(Itm->Uri); |
| 410 | return true; |
| 411 | } |
| 412 | |
| 413 | // Timestamp |
| 414 | curl_easy_getinfo(curl, CURLINFO_FILETIME, &Res.LastModified); |
| 415 | if (Res.LastModified != -1) |
| 416 | { |
| 417 | struct timeval times[2]; |
| 418 | times[0].tv_sec = Res.LastModified; |
| 419 | times[1].tv_sec = Res.LastModified; |
| 420 | times[0].tv_usec = times[1].tv_usec = 0; |
| 421 | utimes(File->Name().c_str(), times); |
| 422 | } |
| 423 | else |
| 424 | Res.LastModified = resultStat.st_mtime; |
| 425 | |
| 426 | // take hashes |
| 427 | Hashes Hash; |
| 428 | FileFd Fd(Res.Filename, FileFd::ReadOnly); |
| 429 | Hash.AddFD(Fd); |
| 430 | Res.TakeHashes(Hash); |
| 431 | |
| 432 | // keep apt updated |
| 433 | URIDone(Res); |
| 434 | |
| 435 | // cleanup |
| 436 | Res.Size = 0; |
| 437 | delete File; |
| 438 | |
| 439 | return true; |
| 440 | } |
| 441 | |
| 442 | int main() |
| 443 | { |
| 444 | setlocale(LC_ALL, ""); |
| 445 | |
| 446 | HttpsMethod Mth; |
| 447 | curl_global_init(CURL_GLOBAL_SSL) ; |
| 448 | |
| 449 | return Mth.Run(); |
| 450 | } |
| 451 | |