https.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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
  53. string SpecificProxy = _config->Find("Acquire::http::Proxy::" + ServerName.Host);
  54. if (!SpecificProxy.empty())
  55. {
  56. if (SpecificProxy == "DIRECT")
  57. Proxy = "";
  58. else
  59. Proxy = SpecificProxy;
  60. }
  61. else
  62. {
  63. string DefProxy = _config->Find("Acquire::http::Proxy");
  64. if (!DefProxy.empty())
  65. {
  66. Proxy = DefProxy;
  67. }
  68. else
  69. {
  70. char* result = getenv("http_proxy");
  71. Proxy = result ? result : "";
  72. }
  73. }
  74. // Parse no_proxy, a , separated list of domains
  75. if (getenv("no_proxy") != 0)
  76. {
  77. if (CheckDomainList(ServerName.Host,getenv("no_proxy")) == true)
  78. Proxy = "";
  79. }
  80. // Determine what host and port to use based on the proxy settings
  81. string Host;
  82. if (Proxy.empty() == true || Proxy.Host.empty() == true)
  83. {
  84. }
  85. else
  86. {
  87. if (Proxy.Port != 0)
  88. curl_easy_setopt(curl, CURLOPT_PROXYPORT, Proxy.Port);
  89. curl_easy_setopt(curl, CURLOPT_PROXY, Proxy.Host.c_str());
  90. }
  91. }
  92. // HttpsMethod::Fetch - Fetch an item /*{{{*/
  93. // ---------------------------------------------------------------------
  94. /* This adds an item to the pipeline. We keep the pipeline at a fixed
  95. depth. */
  96. bool HttpsMethod::Fetch(FetchItem *Itm)
  97. {
  98. stringstream ss;
  99. struct stat SBuf;
  100. struct curl_slist *headers=NULL;
  101. char curl_errorstr[CURL_ERROR_SIZE];
  102. long curl_responsecode;
  103. URI Uri = Itm->Uri;
  104. string remotehost = Uri.Host;
  105. // TODO:
  106. // - http::Pipeline-Depth
  107. // - error checking/reporting
  108. // - more debug options? (CURLOPT_DEBUGFUNCTION?)
  109. curl_easy_reset(curl);
  110. SetupProxy();
  111. maybe_add_auth (Uri, _config->FindFile("Dir::Etc::netrc"));
  112. // callbacks
  113. curl_easy_setopt(curl, CURLOPT_URL, static_cast<string>(Uri).c_str());
  114. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
  115. curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
  116. curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
  117. curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, this);
  118. curl_easy_setopt(curl, CURLOPT_NOPROGRESS, false);
  119. curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);
  120. curl_easy_setopt(curl, CURLOPT_FILETIME, true);
  121. // SSL parameters are set by default to the common (non mirror-specific) value
  122. // if available (or a default one) and gets overload by mirror-specific ones.
  123. // File containing the list of trusted CA.
  124. string cainfo = _config->Find("Acquire::https::CaInfo","");
  125. string knob = "Acquire::https::"+remotehost+"::CaInfo";
  126. cainfo = _config->Find(knob.c_str(),cainfo.c_str());
  127. if(cainfo != "")
  128. curl_easy_setopt(curl, CURLOPT_CAINFO,cainfo.c_str());
  129. // Check server certificate against previous CA list ...
  130. bool peer_verify = _config->FindB("Acquire::https::Verify-Peer",true);
  131. knob = "Acquire::https::" + remotehost + "::Verify-Peer";
  132. peer_verify = _config->FindB(knob.c_str(), peer_verify);
  133. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, peer_verify);
  134. // ... and hostname against cert CN or subjectAltName
  135. int default_verify = 2;
  136. bool verify = _config->FindB("Acquire::https::Verify-Host",true);
  137. knob = "Acquire::https::"+remotehost+"::Verify-Host";
  138. verify = _config->FindB(knob.c_str(),verify);
  139. if (!verify)
  140. default_verify = 0;
  141. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, verify);
  142. // For client authentication, certificate file ...
  143. string pem = _config->Find("Acquire::https::SslCert","");
  144. knob = "Acquire::https::"+remotehost+"::SslCert";
  145. pem = _config->Find(knob.c_str(),pem.c_str());
  146. if(pem != "")
  147. curl_easy_setopt(curl, CURLOPT_SSLCERT, pem.c_str());
  148. // ... and associated key.
  149. string key = _config->Find("Acquire::https::SslKey","");
  150. knob = "Acquire::https::"+remotehost+"::SslKey";
  151. key = _config->Find(knob.c_str(),key.c_str());
  152. if(key != "")
  153. curl_easy_setopt(curl, CURLOPT_SSLKEY, key.c_str());
  154. // Allow forcing SSL version to SSLv3 or TLSv1 (SSLv2 is not
  155. // supported by GnuTLS).
  156. long final_version = CURL_SSLVERSION_DEFAULT;
  157. string sslversion = _config->Find("Acquire::https::SslForceVersion","");
  158. knob = "Acquire::https::"+remotehost+"::SslForceVersion";
  159. sslversion = _config->Find(knob.c_str(),sslversion.c_str());
  160. if(sslversion == "TLSv1")
  161. final_version = CURL_SSLVERSION_TLSv1;
  162. else if(sslversion == "SSLv3")
  163. final_version = CURL_SSLVERSION_SSLv3;
  164. curl_easy_setopt(curl, CURLOPT_SSLVERSION, final_version);
  165. // cache-control
  166. if(_config->FindB("Acquire::http::No-Cache",false) == false)
  167. {
  168. // cache enabled
  169. if (_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::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 dlLimit = _config->FindI("Acquire::http::Dl-Limit",0)*1024;
  181. if (dlLimit > 0)
  182. curl_easy_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE, dlLimit);
  183. // set header
  184. curl_easy_setopt(curl, CURLOPT_USERAGENT,"Debian APT-CURL/1.0 ("VERSION")");
  185. // set timeout
  186. int timeout = _config->FindI("Acquire::http::Timeout",120);
  187. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout);
  188. //set really low lowspeed timeout (see #497983)
  189. curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, DL_MIN_SPEED);
  190. curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, timeout);
  191. // set redirect options and default to 10 redirects
  192. bool AllowRedirect = _config->FindI("Acquire::https::AllowRedirect", true);
  193. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, AllowRedirect);
  194. curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 10);
  195. // debug
  196. if(_config->FindB("Debug::Acquire::https", false))
  197. curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
  198. // error handling
  199. curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
  200. // if we have the file send an if-range query with a range header
  201. if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
  202. {
  203. char Buf[1000];
  204. sprintf(Buf,"Range: bytes=%li-\r\nIf-Range: %s\r\n",
  205. (long)SBuf.st_size - 1,
  206. TimeRFC1123(SBuf.st_mtime).c_str());
  207. headers = curl_slist_append(headers, Buf);
  208. }
  209. else if(Itm->LastModified > 0)
  210. {
  211. curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
  212. curl_easy_setopt(curl, CURLOPT_TIMEVALUE, Itm->LastModified);
  213. }
  214. // go for it - if the file exists, append on it
  215. File = new FileFd(Itm->DestFile, FileFd::WriteAny);
  216. if (File->Size() > 0)
  217. File->Seek(File->Size() - 1);
  218. // keep apt updated
  219. Res.Filename = Itm->DestFile;
  220. // get it!
  221. CURLcode success = curl_easy_perform(curl);
  222. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &curl_responsecode);
  223. long curl_servdate;
  224. curl_easy_getinfo(curl, CURLINFO_FILETIME, &curl_servdate);
  225. // cleanup
  226. if(success != 0)
  227. {
  228. _error->Error("%s", curl_errorstr);
  229. Fail();
  230. return true;
  231. }
  232. File->Close();
  233. // Timestamp
  234. struct utimbuf UBuf;
  235. if (curl_servdate != -1) {
  236. UBuf.actime = curl_servdate;
  237. UBuf.modtime = curl_servdate;
  238. utime(File->Name().c_str(),&UBuf);
  239. }
  240. // check the downloaded result
  241. struct stat Buf;
  242. if (stat(File->Name().c_str(),&Buf) == 0)
  243. {
  244. Res.Filename = File->Name();
  245. Res.LastModified = Buf.st_mtime;
  246. Res.IMSHit = false;
  247. if (curl_responsecode == 304)
  248. {
  249. unlink(File->Name().c_str());
  250. Res.IMSHit = true;
  251. Res.LastModified = Itm->LastModified;
  252. Res.Size = 0;
  253. URIDone(Res);
  254. return true;
  255. }
  256. Res.Size = Buf.st_size;
  257. }
  258. // take hashes
  259. Hashes Hash;
  260. FileFd Fd(Res.Filename, FileFd::ReadOnly);
  261. Hash.AddFD(Fd.Fd(), Fd.Size());
  262. Res.TakeHashes(Hash);
  263. // keep apt updated
  264. URIDone(Res);
  265. // cleanup
  266. Res.Size = 0;
  267. delete File;
  268. curl_slist_free_all(headers);
  269. return true;
  270. };
  271. int main()
  272. {
  273. setlocale(LC_ALL, "");
  274. HttpsMethod Mth;
  275. curl_global_init(CURL_GLOBAL_SSL) ;
  276. return Mth.Run();
  277. }