https.cc 11 KB

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