https.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 aquire 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/acquire-method.h>
  13. #include <apt-pkg/error.h>
  14. #include <apt-pkg/hashes.h>
  15. #include <apt-pkg/netrc.h>
  16. #include <apt-pkg/configuration.h>
  17. #include <sys/stat.h>
  18. #include <sys/time.h>
  19. #include <utime.h>
  20. #include <unistd.h>
  21. #include <signal.h>
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <iostream>
  26. #include <sstream>
  27. #include "config.h"
  28. #include "https.h"
  29. #include <apti18n.h>
  30. /*}}}*/
  31. using namespace std;
  32. size_t
  33. HttpsMethod::write_data(void *buffer, size_t size, size_t nmemb, void *userp)
  34. {
  35. HttpsMethod *me = (HttpsMethod *)userp;
  36. if(me->File->Write(buffer, size*nmemb) != true)
  37. return false;
  38. return size*nmemb;
  39. }
  40. int
  41. HttpsMethod::progress_callback(void *clientp, double dltotal, double dlnow,
  42. double ultotal, double ulnow)
  43. {
  44. HttpsMethod *me = (HttpsMethod *)clientp;
  45. if(dltotal > 0 && me->Res.Size == 0) {
  46. me->Res.Size = (unsigned long long)dltotal;
  47. me->URIStart(me->Res);
  48. }
  49. return 0;
  50. }
  51. void HttpsMethod::SetupProxy() /*{{{*/
  52. {
  53. URI ServerName = Queue->Uri;
  54. // Determine the proxy setting - try https first, fallback to http and use env at last
  55. string UseProxy = _config->Find("Acquire::https::Proxy::" + ServerName.Host,
  56. _config->Find("Acquire::http::Proxy::" + ServerName.Host).c_str());
  57. if (UseProxy.empty() == true)
  58. UseProxy = _config->Find("Acquire::https::Proxy", _config->Find("Acquire::http::Proxy").c_str());
  59. // User want to use NO proxy, so nothing to setup
  60. if (UseProxy == "DIRECT")
  61. return;
  62. if (UseProxy.empty() == false)
  63. {
  64. // Parse no_proxy, a comma (,) separated list of domains we don't want to use
  65. // a proxy for so we stop right here if it is in the list
  66. if (getenv("no_proxy") != 0 && CheckDomainList(ServerName.Host,getenv("no_proxy")) == true)
  67. return;
  68. } else {
  69. const char* result = getenv("http_proxy");
  70. UseProxy = result == NULL ? "" : result;
  71. }
  72. // Determine what host and port to use based on the proxy settings
  73. if (UseProxy.empty() == false)
  74. {
  75. Proxy = UseProxy;
  76. if (Proxy.Port != 1)
  77. curl_easy_setopt(curl, CURLOPT_PROXYPORT, Proxy.Port);
  78. curl_easy_setopt(curl, CURLOPT_PROXY, Proxy.Host.c_str());
  79. }
  80. } /*}}}*/
  81. // HttpsMethod::Fetch - Fetch an item /*{{{*/
  82. // ---------------------------------------------------------------------
  83. /* This adds an item to the pipeline. We keep the pipeline at a fixed
  84. depth. */
  85. bool HttpsMethod::Fetch(FetchItem *Itm)
  86. {
  87. stringstream ss;
  88. struct stat SBuf;
  89. struct curl_slist *headers=NULL;
  90. char curl_errorstr[CURL_ERROR_SIZE];
  91. long curl_responsecode;
  92. URI Uri = Itm->Uri;
  93. string remotehost = Uri.Host;
  94. // TODO:
  95. // - http::Pipeline-Depth
  96. // - error checking/reporting
  97. // - more debug options? (CURLOPT_DEBUGFUNCTION?)
  98. curl_easy_reset(curl);
  99. SetupProxy();
  100. maybe_add_auth (Uri, _config->FindFile("Dir::Etc::netrc"));
  101. // callbacks
  102. curl_easy_setopt(curl, CURLOPT_URL, static_cast<string>(Uri).c_str());
  103. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
  104. curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
  105. curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
  106. curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, this);
  107. curl_easy_setopt(curl, CURLOPT_NOPROGRESS, false);
  108. curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);
  109. curl_easy_setopt(curl, CURLOPT_FILETIME, true);
  110. // SSL parameters are set by default to the common (non mirror-specific) value
  111. // if available (or a default one) and gets overload by mirror-specific ones.
  112. // File containing the list of trusted CA.
  113. string cainfo = _config->Find("Acquire::https::CaInfo","");
  114. string knob = "Acquire::https::"+remotehost+"::CaInfo";
  115. cainfo = _config->Find(knob.c_str(),cainfo.c_str());
  116. if(cainfo.empty() == false)
  117. curl_easy_setopt(curl, CURLOPT_CAINFO,cainfo.c_str());
  118. // Check server certificate against previous CA list ...
  119. bool peer_verify = _config->FindB("Acquire::https::Verify-Peer",true);
  120. knob = "Acquire::https::" + remotehost + "::Verify-Peer";
  121. peer_verify = _config->FindB(knob.c_str(), peer_verify);
  122. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, peer_verify);
  123. // ... and hostname against cert CN or subjectAltName
  124. bool verify = _config->FindB("Acquire::https::Verify-Host",true);
  125. knob = "Acquire::https::"+remotehost+"::Verify-Host";
  126. verify = _config->FindB(knob.c_str(),verify);
  127. int const default_verify = (verify == true) ? 2 : 0;
  128. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, default_verify);
  129. // Also enforce issuer of server certificate using its cert
  130. string issuercert = _config->Find("Acquire::https::IssuerCert","");
  131. knob = "Acquire::https::"+remotehost+"::IssuerCert";
  132. issuercert = _config->Find(knob.c_str(),issuercert.c_str());
  133. if(issuercert.empty() == false)
  134. curl_easy_setopt(curl, CURLOPT_ISSUERCERT,issuercert.c_str());
  135. // For client authentication, certificate file ...
  136. string pem = _config->Find("Acquire::https::SslCert","");
  137. knob = "Acquire::https::"+remotehost+"::SslCert";
  138. pem = _config->Find(knob.c_str(),pem.c_str());
  139. if(pem.empty() == false)
  140. curl_easy_setopt(curl, CURLOPT_SSLCERT, pem.c_str());
  141. // ... and associated key.
  142. string key = _config->Find("Acquire::https::SslKey","");
  143. knob = "Acquire::https::"+remotehost+"::SslKey";
  144. key = _config->Find(knob.c_str(),key.c_str());
  145. if(key.empty() == false)
  146. curl_easy_setopt(curl, CURLOPT_SSLKEY, key.c_str());
  147. // Allow forcing SSL version to SSLv3 or TLSv1 (SSLv2 is not
  148. // supported by GnuTLS).
  149. long final_version = CURL_SSLVERSION_DEFAULT;
  150. string sslversion = _config->Find("Acquire::https::SslForceVersion","");
  151. knob = "Acquire::https::"+remotehost+"::SslForceVersion";
  152. sslversion = _config->Find(knob.c_str(),sslversion.c_str());
  153. if(sslversion == "TLSv1")
  154. final_version = CURL_SSLVERSION_TLSv1;
  155. else if(sslversion == "SSLv3")
  156. final_version = CURL_SSLVERSION_SSLv3;
  157. curl_easy_setopt(curl, CURLOPT_SSLVERSION, final_version);
  158. // CRL file
  159. string crlfile = _config->Find("Acquire::https::CrlFile","");
  160. knob = "Acquire::https::"+remotehost+"::CrlFile";
  161. crlfile = _config->Find(knob.c_str(),crlfile.c_str());
  162. if(crlfile.empty() == false)
  163. curl_easy_setopt(curl, CURLOPT_CRLFILE, crlfile.c_str());
  164. // cache-control
  165. if(_config->FindB("Acquire::https::No-Cache",
  166. _config->FindB("Acquire::http::No-Cache",false)) == false)
  167. {
  168. // cache enabled
  169. if (_config->FindB("Acquire::https::No-Store",
  170. _config->FindB("Acquire::http::No-Store",false)) == true)
  171. headers = curl_slist_append(headers,"Cache-Control: no-store");
  172. ioprintf(ss, "Cache-Control: max-age=%u", _config->FindI("Acquire::https::Max-Age",
  173. _config->FindI("Acquire::http::Max-Age",0)));
  174. headers = curl_slist_append(headers, ss.str().c_str());
  175. } else {
  176. // cache disabled by user
  177. headers = curl_slist_append(headers, "Cache-Control: no-cache");
  178. headers = curl_slist_append(headers, "Pragma: no-cache");
  179. }
  180. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  181. // speed limit
  182. int const dlLimit = _config->FindI("Acquire::https::Dl-Limit",
  183. _config->FindI("Acquire::http::Dl-Limit",0))*1024;
  184. if (dlLimit > 0)
  185. curl_easy_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE, dlLimit);
  186. // set header
  187. curl_easy_setopt(curl, CURLOPT_USERAGENT,
  188. _config->Find("Acquire::https::User-Agent",
  189. _config->Find("Acquire::http::User-Agent",
  190. "Debian APT-CURL/1.0 ("VERSION")").c_str()).c_str());
  191. // set timeout
  192. int const timeout = _config->FindI("Acquire::https::Timeout",
  193. _config->FindI("Acquire::http::Timeout",120));
  194. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout);
  195. //set really low lowspeed timeout (see #497983)
  196. curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, DL_MIN_SPEED);
  197. curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, timeout);
  198. // set redirect options and default to 10 redirects
  199. bool const AllowRedirect = _config->FindB("Acquire::https::AllowRedirect",
  200. _config->FindB("Acquire::http::AllowRedirect",true));
  201. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, AllowRedirect);
  202. curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 10);
  203. // debug
  204. if(_config->FindB("Debug::Acquire::https", false))
  205. curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
  206. // error handling
  207. curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
  208. // if we have the file send an if-range query with a range header
  209. if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
  210. {
  211. char Buf[1000];
  212. sprintf(Buf,"Range: bytes=%li-\r\nIf-Range: %s\r\n",
  213. (long)SBuf.st_size - 1,
  214. TimeRFC1123(SBuf.st_mtime).c_str());
  215. headers = curl_slist_append(headers, Buf);
  216. }
  217. else if(Itm->LastModified > 0)
  218. {
  219. curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
  220. curl_easy_setopt(curl, CURLOPT_TIMEVALUE, Itm->LastModified);
  221. }
  222. // go for it - if the file exists, append on it
  223. File = new FileFd(Itm->DestFile, FileFd::WriteAny);
  224. if (File->Size() > 0)
  225. File->Seek(File->Size() - 1);
  226. // keep apt updated
  227. Res.Filename = Itm->DestFile;
  228. // get it!
  229. CURLcode success = curl_easy_perform(curl);
  230. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &curl_responsecode);
  231. long curl_servdate;
  232. curl_easy_getinfo(curl, CURLINFO_FILETIME, &curl_servdate);
  233. File->Close();
  234. // cleanup
  235. if(success != 0)
  236. {
  237. _error->Error("%s", curl_errorstr);
  238. // unlink, no need keep 401/404 page content in partial/
  239. unlink(File->Name().c_str());
  240. Fail();
  241. return true;
  242. }
  243. // Timestamp
  244. struct utimbuf UBuf;
  245. if (curl_servdate != -1) {
  246. UBuf.actime = curl_servdate;
  247. UBuf.modtime = curl_servdate;
  248. utime(File->Name().c_str(),&UBuf);
  249. }
  250. // check the downloaded result
  251. struct stat Buf;
  252. if (stat(File->Name().c_str(),&Buf) == 0)
  253. {
  254. Res.Filename = File->Name();
  255. Res.LastModified = Buf.st_mtime;
  256. Res.IMSHit = false;
  257. if (curl_responsecode == 304)
  258. {
  259. unlink(File->Name().c_str());
  260. Res.IMSHit = true;
  261. Res.LastModified = Itm->LastModified;
  262. Res.Size = 0;
  263. URIDone(Res);
  264. return true;
  265. }
  266. Res.Size = Buf.st_size;
  267. }
  268. // take hashes
  269. Hashes Hash;
  270. FileFd Fd(Res.Filename, FileFd::ReadOnly);
  271. Hash.AddFD(Fd.Fd(), Fd.Size());
  272. Res.TakeHashes(Hash);
  273. // keep apt updated
  274. URIDone(Res);
  275. // cleanup
  276. Res.Size = 0;
  277. delete File;
  278. curl_slist_free_all(headers);
  279. return true;
  280. };
  281. int main()
  282. {
  283. setlocale(LC_ALL, "");
  284. HttpsMethod Mth;
  285. curl_global_init(CURL_GLOBAL_SSL) ;
  286. return Mth.Run();
  287. }