https.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 != "")
  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. int default_verify = 2;
  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. if (!verify)
  127. default_verify = 0;
  128. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, verify);
  129. // For client authentication, certificate file ...
  130. string pem = _config->Find("Acquire::https::SslCert","");
  131. knob = "Acquire::https::"+remotehost+"::SslCert";
  132. pem = _config->Find(knob.c_str(),pem.c_str());
  133. if(pem != "")
  134. curl_easy_setopt(curl, CURLOPT_SSLCERT, pem.c_str());
  135. // ... and associated key.
  136. string key = _config->Find("Acquire::https::SslKey","");
  137. knob = "Acquire::https::"+remotehost+"::SslKey";
  138. key = _config->Find(knob.c_str(),key.c_str());
  139. if(key != "")
  140. curl_easy_setopt(curl, CURLOPT_SSLKEY, key.c_str());
  141. // Allow forcing SSL version to SSLv3 or TLSv1 (SSLv2 is not
  142. // supported by GnuTLS).
  143. long final_version = CURL_SSLVERSION_DEFAULT;
  144. string sslversion = _config->Find("Acquire::https::SslForceVersion","");
  145. knob = "Acquire::https::"+remotehost+"::SslForceVersion";
  146. sslversion = _config->Find(knob.c_str(),sslversion.c_str());
  147. if(sslversion == "TLSv1")
  148. final_version = CURL_SSLVERSION_TLSv1;
  149. else if(sslversion == "SSLv3")
  150. final_version = CURL_SSLVERSION_SSLv3;
  151. curl_easy_setopt(curl, CURLOPT_SSLVERSION, final_version);
  152. // cache-control
  153. if(_config->FindB("Acquire::https::No-Cache",
  154. _config->FindB("Acquire::http::No-Cache",false)) == false)
  155. {
  156. // cache enabled
  157. if (_config->FindB("Acquire::https::No-Store",
  158. _config->FindB("Acquire::http::No-Store",false)) == true)
  159. headers = curl_slist_append(headers,"Cache-Control: no-store");
  160. ioprintf(ss, "Cache-Control: max-age=%u", _config->FindI("Acquire::https::Max-Age",
  161. _config->FindI("Acquire::http::Max-Age",0)));
  162. headers = curl_slist_append(headers, ss.str().c_str());
  163. } else {
  164. // cache disabled by user
  165. headers = curl_slist_append(headers, "Cache-Control: no-cache");
  166. headers = curl_slist_append(headers, "Pragma: no-cache");
  167. }
  168. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  169. // speed limit
  170. int dlLimit = _config->FindI("Acquire::https::Dl-Limit",
  171. _config->FindI("Acquire::http::Dl-Limit",0))*1024;
  172. if (dlLimit > 0)
  173. curl_easy_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE, dlLimit);
  174. // set header
  175. curl_easy_setopt(curl, CURLOPT_USERAGENT,
  176. _config->Find("Acquire::https::User-Agent",
  177. _config->Find("Acquire::http::User-Agent",
  178. "Debian APT-CURL/1.0 ("VERSION")").c_str()).c_str());
  179. // set timeout
  180. int timeout = _config->FindI("Acquire::https::Timeout",
  181. _config->FindI("Acquire::http::Timeout",120));
  182. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout);
  183. //set really low lowspeed timeout (see #497983)
  184. curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, DL_MIN_SPEED);
  185. curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, timeout);
  186. // set redirect options and default to 10 redirects
  187. bool AllowRedirect = _config->FindB("Acquire::https::AllowRedirect",
  188. _config->FindB("Acquire::http::AllowRedirect",true));
  189. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, AllowRedirect);
  190. curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 10);
  191. // debug
  192. if(_config->FindB("Debug::Acquire::https", false))
  193. curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
  194. // error handling
  195. curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
  196. // if we have the file send an if-range query with a range header
  197. if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
  198. {
  199. char Buf[1000];
  200. sprintf(Buf,"Range: bytes=%li-\r\nIf-Range: %s\r\n",
  201. (long)SBuf.st_size - 1,
  202. TimeRFC1123(SBuf.st_mtime).c_str());
  203. headers = curl_slist_append(headers, Buf);
  204. }
  205. else if(Itm->LastModified > 0)
  206. {
  207. curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
  208. curl_easy_setopt(curl, CURLOPT_TIMEVALUE, Itm->LastModified);
  209. }
  210. // go for it - if the file exists, append on it
  211. File = new FileFd(Itm->DestFile, FileFd::WriteAny);
  212. if (File->Size() > 0)
  213. File->Seek(File->Size() - 1);
  214. // keep apt updated
  215. Res.Filename = Itm->DestFile;
  216. // get it!
  217. CURLcode success = curl_easy_perform(curl);
  218. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &curl_responsecode);
  219. long curl_servdate;
  220. curl_easy_getinfo(curl, CURLINFO_FILETIME, &curl_servdate);
  221. // cleanup
  222. if(success != 0)
  223. {
  224. _error->Error("%s", curl_errorstr);
  225. Fail();
  226. return true;
  227. }
  228. File->Close();
  229. // Timestamp
  230. struct utimbuf UBuf;
  231. if (curl_servdate != -1) {
  232. UBuf.actime = curl_servdate;
  233. UBuf.modtime = curl_servdate;
  234. utime(File->Name().c_str(),&UBuf);
  235. }
  236. // check the downloaded result
  237. struct stat Buf;
  238. if (stat(File->Name().c_str(),&Buf) == 0)
  239. {
  240. Res.Filename = File->Name();
  241. Res.LastModified = Buf.st_mtime;
  242. Res.IMSHit = false;
  243. if (curl_responsecode == 304)
  244. {
  245. unlink(File->Name().c_str());
  246. Res.IMSHit = true;
  247. Res.LastModified = Itm->LastModified;
  248. Res.Size = 0;
  249. URIDone(Res);
  250. return true;
  251. }
  252. Res.Size = Buf.st_size;
  253. }
  254. // take hashes
  255. Hashes Hash;
  256. FileFd Fd(Res.Filename, FileFd::ReadOnly);
  257. Hash.AddFD(Fd.Fd(), Fd.Size());
  258. Res.TakeHashes(Hash);
  259. // keep apt updated
  260. URIDone(Res);
  261. // cleanup
  262. Res.Size = 0;
  263. delete File;
  264. curl_slist_free_all(headers);
  265. return true;
  266. };
  267. int main()
  268. {
  269. setlocale(LC_ALL, "");
  270. HttpsMethod Mth;
  271. curl_global_init(CURL_GLOBAL_SSL) ;
  272. return Mth.Run();
  273. }