https.cc 18 KB

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