https.cc 9.5 KB

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