https.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. if (getenv("http_proxy") == 0)
  53. {
  54. string DefProxy = _config->Find("Acquire::http::Proxy");
  55. string SpecificProxy = _config->Find("Acquire::http::Proxy::" + ServerName.Host);
  56. if (SpecificProxy.empty() == false)
  57. {
  58. if (SpecificProxy == "DIRECT")
  59. Proxy = "";
  60. else
  61. Proxy = SpecificProxy;
  62. }
  63. else
  64. Proxy = DefProxy;
  65. }
  66. // Parse no_proxy, a , separated list of domains
  67. if (getenv("no_proxy") != 0)
  68. {
  69. if (CheckDomainList(ServerName.Host,getenv("no_proxy")) == true)
  70. Proxy = "";
  71. }
  72. // Determine what host and port to use based on the proxy settings
  73. string Host;
  74. if (Proxy.empty() == true || Proxy.Host.empty() == true)
  75. {
  76. }
  77. else
  78. {
  79. if (Proxy.Port != 0)
  80. curl_easy_setopt(curl, CURLOPT_PROXYPORT, Proxy.Port);
  81. curl_easy_setopt(curl, CURLOPT_PROXY, Proxy.Host.c_str());
  82. }
  83. }
  84. // HttpsMethod::Fetch - Fetch an item /*{{{*/
  85. // ---------------------------------------------------------------------
  86. /* This adds an item to the pipeline. We keep the pipeline at a fixed
  87. depth. */
  88. bool HttpsMethod::Fetch(FetchItem *Itm)
  89. {
  90. stringstream ss;
  91. struct stat SBuf;
  92. struct curl_slist *headers=NULL;
  93. char curl_errorstr[CURL_ERROR_SIZE];
  94. long curl_responsecode;
  95. URI Uri = Itm->Uri;
  96. string remotehost = Uri.Host;
  97. // TODO:
  98. // - http::Pipeline-Depth
  99. // - error checking/reporting
  100. // - more debug options? (CURLOPT_DEBUGFUNCTION?)
  101. curl_easy_reset(curl);
  102. SetupProxy();
  103. // callbacks
  104. curl_easy_setopt(curl, CURLOPT_URL, Itm->Uri.c_str());
  105. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
  106. curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
  107. curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
  108. curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, this);
  109. curl_easy_setopt(curl, CURLOPT_NOPROGRESS, false);
  110. curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);
  111. curl_easy_setopt(curl, CURLOPT_FILETIME, true);
  112. // SSL parameters are set by default to the common (non mirror-specific) value
  113. // if available (or a default one) and gets overload by mirror-specific ones.
  114. // File containing the list of trusted CA.
  115. string cainfo = _config->Find("Acquire::https::CaInfo","");
  116. string knob = "Acquire::https::"+remotehost+"::CaInfo";
  117. cainfo = _config->Find(knob.c_str(),cainfo.c_str());
  118. if(cainfo != "")
  119. curl_easy_setopt(curl, CURLOPT_CAINFO,cainfo.c_str());
  120. // Check server certificate against previous CA list ...
  121. bool peer_verify = _config->FindB("Acquire::https::Verify-Peer",true);
  122. knob = "Acquire::https::" + remotehost + "::Verify-Peer";
  123. peer_verify = _config->FindB(knob.c_str(), peer_verify);
  124. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, peer_verify);
  125. // ... and hostname against cert CN or subjectAltName
  126. int default_verify = 2;
  127. bool verify = _config->FindB("Acquire::https::Verify-Host",true);
  128. knob = "Acquire::https::"+remotehost+"::Verify-Host";
  129. verify = _config->FindB(knob.c_str(),verify);
  130. if (!verify)
  131. default_verify = 0;
  132. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, verify);
  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 != "")
  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 != "")
  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. // cache-control
  157. if(_config->FindB("Acquire::http::No-Cache",false) == false)
  158. {
  159. // cache enabled
  160. if (_config->FindB("Acquire::http::No-Store",false) == true)
  161. headers = curl_slist_append(headers,"Cache-Control: no-store");
  162. ioprintf(ss, "Cache-Control: max-age=%u", _config->FindI("Acquire::http::Max-Age",0));
  163. headers = curl_slist_append(headers, ss.str().c_str());
  164. } else {
  165. // cache disabled by user
  166. headers = curl_slist_append(headers, "Cache-Control: no-cache");
  167. headers = curl_slist_append(headers, "Pragma: no-cache");
  168. }
  169. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  170. // speed limit
  171. int dlLimit = _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,"Debian APT-CURL/1.0 ("VERSION")");
  176. // set timeout
  177. int timeout = _config->FindI("Acquire::http::Timeout",120);
  178. curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
  179. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout);
  180. // debug
  181. if(_config->FindB("Debug::Acquire::https", false))
  182. curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
  183. // error handling
  184. curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
  185. // if we have the file send an if-range query with a range header
  186. if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
  187. {
  188. char Buf[1000];
  189. sprintf(Buf,"Range: bytes=%li-\r\nIf-Range: %s\r\n",
  190. (long)SBuf.st_size - 1,
  191. TimeRFC1123(SBuf.st_mtime).c_str());
  192. headers = curl_slist_append(headers, Buf);
  193. }
  194. else if(Itm->LastModified > 0)
  195. {
  196. curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
  197. curl_easy_setopt(curl, CURLOPT_TIMEVALUE, Itm->LastModified);
  198. }
  199. // go for it - if the file exists, append on it
  200. File = new FileFd(Itm->DestFile, FileFd::WriteAny);
  201. if (File->Size() > 0)
  202. File->Seek(File->Size() - 1);
  203. // keep apt updated
  204. Res.Filename = Itm->DestFile;
  205. // get it!
  206. CURLcode success = curl_easy_perform(curl);
  207. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &curl_responsecode);
  208. long curl_servdate;
  209. curl_easy_getinfo(curl, CURLINFO_FILETIME, &curl_servdate);
  210. // cleanup
  211. if(success != 0)
  212. {
  213. unlink(File->Name().c_str());
  214. _error->Error(curl_errorstr);
  215. Fail();
  216. return true;
  217. }
  218. File->Close();
  219. // Timestamp
  220. struct utimbuf UBuf;
  221. if (curl_servdate != -1) {
  222. UBuf.actime = curl_servdate;
  223. UBuf.modtime = curl_servdate;
  224. utime(File->Name().c_str(),&UBuf);
  225. }
  226. // check the downloaded result
  227. struct stat Buf;
  228. if (stat(File->Name().c_str(),&Buf) == 0)
  229. {
  230. Res.Filename = File->Name();
  231. Res.LastModified = Buf.st_mtime;
  232. Res.IMSHit = false;
  233. if (curl_responsecode == 304)
  234. {
  235. unlink(File->Name().c_str());
  236. Res.IMSHit = true;
  237. Res.LastModified = Itm->LastModified;
  238. Res.Size = 0;
  239. URIDone(Res);
  240. return true;
  241. }
  242. Res.Size = Buf.st_size;
  243. }
  244. // take hashes
  245. Hashes Hash;
  246. FileFd Fd(Res.Filename, FileFd::ReadOnly);
  247. Hash.AddFD(Fd.Fd(), Fd.Size());
  248. Res.TakeHashes(Hash);
  249. // keep apt updated
  250. URIDone(Res);
  251. // cleanup
  252. Res.Size = 0;
  253. delete File;
  254. curl_slist_free_all(headers);
  255. return true;
  256. };
  257. int main()
  258. {
  259. setlocale(LC_ALL, "");
  260. HttpsMethod Mth;
  261. curl_global_init(CURL_GLOBAL_SSL) ;
  262. return Mth.Run();
  263. }