https.cc 10 KB

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