https.cc 11 KB

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