2009-12-11 00:38:13 +01:00
|
|
|
//-*- mode: cpp; mode: fold -*-
|
2006-12-19 12:03:30 +01:00
|
|
|
// Description /*{{{*/
|
|
|
|
|
// $Id: http.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $
|
|
|
|
|
/* ######################################################################
|
|
|
|
|
|
2014-02-22 18:34:33 +01:00
|
|
|
HTTPS Acquire Method - This is the HTTPS acquire method for APT.
|
2006-12-19 12:03:30 +01:00
|
|
|
|
|
|
|
|
It uses libcurl
|
|
|
|
|
|
|
|
|
|
##################################################################### */
|
|
|
|
|
/*}}}*/
|
|
|
|
|
// Include Files /*{{{*/
|
2011-09-13 10:09:00 +02:00
|
|
|
#include <config.h>
|
|
|
|
|
|
2006-12-19 12:03:30 +01:00
|
|
|
#include <apt-pkg/fileutl.h>
|
|
|
|
|
#include <apt-pkg/error.h>
|
|
|
|
|
#include <apt-pkg/hashes.h>
|
2009-10-16 15:36:28 +02:00
|
|
|
#include <apt-pkg/netrc.h>
|
2011-09-19 19:14:19 +02:00
|
|
|
#include <apt-pkg/configuration.h>
|
2014-03-05 22:11:25 +01:00
|
|
|
#include <apt-pkg/macros.h>
|
|
|
|
|
#include <apt-pkg/strutl.h>
|
2014-09-02 15:50:19 +02:00
|
|
|
#include <apt-pkg/proxy.h>
|
2006-12-19 12:03:30 +01:00
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <stdio.h>
|
2014-03-05 22:11:25 +01:00
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <stdlib.h>
|
2006-12-19 12:03:30 +01:00
|
|
|
|
2016-07-31 18:05:56 +02:00
|
|
|
#include <array>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
|
|
|
2006-12-19 12:03:30 +01:00
|
|
|
#include "https.h"
|
2014-03-05 22:11:25 +01:00
|
|
|
|
2011-09-13 10:09:00 +02:00
|
|
|
#include <apti18n.h>
|
2006-12-19 12:03:30 +01:00
|
|
|
/*}}}*/
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
2015-03-27 15:53:43 +01:00
|
|
|
struct APT_HIDDEN CURLUserPointer {
|
|
|
|
|
HttpsMethod * const https;
|
|
|
|
|
HttpsMethod::FetchResult * const Res;
|
2015-05-12 00:30:16 +02:00
|
|
|
HttpsMethod::FetchItem const * const Itm;
|
2016-11-09 12:25:44 +01:00
|
|
|
RequestState * const Req;
|
2015-05-12 00:30:16 +02:00
|
|
|
CURLUserPointer(HttpsMethod * const https, HttpsMethod::FetchResult * const Res,
|
2016-11-09 12:25:44 +01:00
|
|
|
HttpsMethod::FetchItem const * const Itm, RequestState * const Req) : https(https), Res(Res), Itm(Itm), Req(Req) {}
|
2015-03-27 15:53:43 +01:00
|
|
|
};
|
|
|
|
|
|
2013-09-30 16:41:16 +02:00
|
|
|
size_t
|
|
|
|
|
HttpsMethod::parse_header(void *buffer, size_t size, size_t nmemb, void *userp)
|
|
|
|
|
{
|
|
|
|
|
size_t len = size * nmemb;
|
2015-11-04 21:08:55 +01:00
|
|
|
CURLUserPointer *me = static_cast<CURLUserPointer *>(userp);
|
2013-09-30 16:41:16 +02:00
|
|
|
std::string line((char*) buffer, len);
|
|
|
|
|
for (--len; len > 0; --len)
|
2015-12-27 00:51:59 +01:00
|
|
|
if (isspace_ascii(line[len]) == 0)
|
2013-09-30 16:41:16 +02:00
|
|
|
{
|
|
|
|
|
++len;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
line.erase(len);
|
|
|
|
|
|
|
|
|
|
if (line.empty() == true)
|
|
|
|
|
{
|
2016-11-09 12:25:44 +01:00
|
|
|
if (me->Req->File.Open(me->Itm->DestFile, FileFd::WriteAny) == false)
|
|
|
|
|
return ERROR_NOT_FROM_SERVER;
|
|
|
|
|
|
|
|
|
|
me->Req->JunkSize = 0;
|
|
|
|
|
if (me->Req->Result != 416 && me->Req->StartPos != 0)
|
2013-09-30 16:41:16 +02:00
|
|
|
;
|
2016-11-09 12:25:44 +01:00
|
|
|
else if (me->Req->Result == 416)
|
2013-09-30 16:41:16 +02:00
|
|
|
{
|
2015-05-12 00:30:16 +02:00
|
|
|
bool partialHit = false;
|
|
|
|
|
if (me->Itm->ExpectedHashes.usable() == true)
|
|
|
|
|
{
|
|
|
|
|
Hashes resultHashes(me->Itm->ExpectedHashes);
|
|
|
|
|
FileFd file(me->Itm->DestFile, FileFd::ReadOnly);
|
2016-11-09 12:25:44 +01:00
|
|
|
me->Req->TotalFileSize = file.FileSize();
|
|
|
|
|
me->Req->Date = file.ModificationTime();
|
2015-05-12 00:30:16 +02:00
|
|
|
resultHashes.AddFD(file);
|
|
|
|
|
HashStringList const hashList = resultHashes.GetHashStringList();
|
|
|
|
|
partialHit = (me->Itm->ExpectedHashes == hashList);
|
|
|
|
|
}
|
2016-11-09 12:25:44 +01:00
|
|
|
else if (me->Req->Result == 416 && me->Req->TotalFileSize == me->Req->File.FileSize())
|
2015-05-12 00:30:16 +02:00
|
|
|
partialHit = true;
|
|
|
|
|
|
|
|
|
|
if (partialHit == true)
|
|
|
|
|
{
|
2016-11-09 12:25:44 +01:00
|
|
|
me->Req->Result = 200;
|
|
|
|
|
me->Req->StartPos = me->Req->TotalFileSize;
|
2015-05-12 00:30:16 +02:00
|
|
|
// the actual size is not important for https as curl will deal with it
|
|
|
|
|
// by itself and e.g. doesn't bother us with transport-encoding…
|
2016-11-09 12:25:44 +01:00
|
|
|
me->Req->JunkSize = std::numeric_limits<unsigned long long>::max();
|
2015-05-12 00:30:16 +02:00
|
|
|
}
|
|
|
|
|
else
|
2016-11-09 12:25:44 +01:00
|
|
|
me->Req->StartPos = 0;
|
2013-09-30 16:41:16 +02:00
|
|
|
}
|
|
|
|
|
else
|
2016-11-09 12:25:44 +01:00
|
|
|
me->Req->StartPos = 0;
|
2013-09-30 16:41:16 +02:00
|
|
|
|
2016-11-09 12:25:44 +01:00
|
|
|
me->Res->LastModified = me->Req->Date;
|
|
|
|
|
me->Res->Size = me->Req->TotalFileSize;
|
|
|
|
|
me->Res->ResumePoint = me->Req->StartPos;
|
2015-03-27 15:53:43 +01:00
|
|
|
|
|
|
|
|
// we expect valid data, so tell our caller we get the file now
|
2016-11-09 12:25:44 +01:00
|
|
|
if (me->Req->Result >= 200 && me->Req->Result < 300)
|
2015-04-11 10:23:52 +02:00
|
|
|
{
|
2016-08-02 14:49:58 +02:00
|
|
|
if (me->Res->Size != 0 && me->Res->Size > me->Res->ResumePoint)
|
2015-04-11 10:23:52 +02:00
|
|
|
me->https->URIStart(*me->Res);
|
2016-11-09 12:25:44 +01:00
|
|
|
if (me->Req->AddPartialFileToHashes(me->Req->File) == false)
|
2015-04-11 10:23:52 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
2016-08-02 14:49:58 +02:00
|
|
|
else
|
2016-11-09 12:25:44 +01:00
|
|
|
me->Req->JunkSize = std::numeric_limits<decltype(me->Req->JunkSize)>::max();
|
2013-09-30 16:41:16 +02:00
|
|
|
}
|
2016-11-09 12:25:44 +01:00
|
|
|
else if (me->Req->HeaderLine(line) == false)
|
2013-09-30 16:41:16 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return size*nmemb;
|
|
|
|
|
}
|
|
|
|
|
|
2006-12-19 12:03:30 +01:00
|
|
|
size_t
|
|
|
|
|
HttpsMethod::write_data(void *buffer, size_t size, size_t nmemb, void *userp)
|
|
|
|
|
{
|
2016-11-09 12:25:44 +01:00
|
|
|
CURLUserPointer *me = static_cast<CURLUserPointer *>(userp);
|
2014-11-29 17:59:52 +01:00
|
|
|
size_t buffer_size = size * nmemb;
|
|
|
|
|
// we don't need to count the junk here, just drop anything we get as
|
|
|
|
|
// we don't always know how long it would be, e.g. in chunked encoding.
|
2016-11-09 12:25:44 +01:00
|
|
|
if (me->Req->JunkSize != 0)
|
2014-11-29 17:59:52 +01:00
|
|
|
return buffer_size;
|
2006-12-19 12:03:30 +01:00
|
|
|
|
2016-11-09 12:25:44 +01:00
|
|
|
if(me->Req->File.Write(buffer, buffer_size) != true)
|
2015-03-09 01:34:10 +01:00
|
|
|
return 0;
|
|
|
|
|
|
2016-11-09 12:25:44 +01:00
|
|
|
if(me->https->Queue->MaximumSize > 0)
|
2015-03-09 01:34:10 +01:00
|
|
|
{
|
2016-11-09 12:25:44 +01:00
|
|
|
unsigned long long const TotalWritten = me->Req->File.Tell();
|
|
|
|
|
if (TotalWritten > me->https->Queue->MaximumSize)
|
2015-03-09 01:34:10 +01:00
|
|
|
{
|
2016-11-09 12:25:44 +01:00
|
|
|
me->https->SetFailReason("MaximumSizeExceeded");
|
2015-03-09 01:34:10 +01:00
|
|
|
_error->Error("Writing more data than expected (%llu > %llu)",
|
2016-11-09 12:25:44 +01:00
|
|
|
TotalWritten, me->https->Queue->MaximumSize);
|
2015-03-09 01:34:10 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-12-19 12:03:30 +01:00
|
|
|
|
2016-11-09 12:25:44 +01:00
|
|
|
if (me->https->Server->GetHashes()->Add((unsigned char const * const)buffer, buffer_size) == false)
|
2015-04-11 10:23:52 +02:00
|
|
|
return 0;
|
|
|
|
|
|
2014-11-29 17:59:52 +01:00
|
|
|
return buffer_size;
|
2006-12-19 12:03:30 +01:00
|
|
|
}
|
|
|
|
|
|
2013-09-30 16:41:16 +02:00
|
|
|
// HttpsServerState::HttpsServerState - Constructor /*{{{*/
|
2015-04-11 10:23:52 +02:00
|
|
|
HttpsServerState::HttpsServerState(URI Srv,HttpsMethod * Owner) : ServerState(Srv, Owner), Hash(NULL)
|
2013-09-30 16:41:16 +02:00
|
|
|
{
|
2016-07-31 18:05:56 +02:00
|
|
|
TimeOut = Owner->ConfigFindI("Timeout", TimeOut);
|
2013-09-30 16:41:16 +02:00
|
|
|
Reset();
|
|
|
|
|
}
|
|
|
|
|
/*}}}*/
|
2015-04-11 10:23:52 +02:00
|
|
|
bool HttpsServerState::InitHashes(HashStringList const &ExpectedHashes) /*{{{*/
|
|
|
|
|
{
|
|
|
|
|
delete Hash;
|
|
|
|
|
Hash = new Hashes(ExpectedHashes);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
/*}}}*/
|
|
|
|
|
APT_PURE Hashes * HttpsServerState::GetHashes() /*{{{*/
|
|
|
|
|
{
|
|
|
|
|
return Hash;
|
|
|
|
|
}
|
|
|
|
|
/*}}}*/
|
2013-09-30 16:41:16 +02:00
|
|
|
|
2016-08-03 21:17:26 +02:00
|
|
|
bool HttpsMethod::SetupProxy() /*{{{*/
|
2006-12-19 12:03:30 +01:00
|
|
|
{
|
|
|
|
|
URI ServerName = Queue->Uri;
|
|
|
|
|
|
2014-09-02 15:50:19 +02:00
|
|
|
// Determine the proxy setting
|
|
|
|
|
AutoDetectProxy(ServerName);
|
|
|
|
|
|
2013-05-08 17:50:15 +02:00
|
|
|
// Curl should never read proxy settings from the environment, as
|
|
|
|
|
// we determine which proxy to use. Do this for consistency among
|
|
|
|
|
// methods and prevent an environment variable overriding a
|
|
|
|
|
// no-proxy ("DIRECT") setting in apt.conf.
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_PROXY, "");
|
|
|
|
|
|
2009-12-14 09:22:29 +01:00
|
|
|
// Determine the proxy setting - try https first, fallback to http and use env at last
|
2016-07-31 18:05:56 +02:00
|
|
|
string UseProxy = ConfigFind("Proxy::" + ServerName.Host, "");
|
2009-12-14 09:22:29 +01:00
|
|
|
if (UseProxy.empty() == true)
|
2016-07-31 18:05:56 +02:00
|
|
|
UseProxy = ConfigFind("Proxy", "");
|
|
|
|
|
// User wants to use NO proxy, so nothing to setup
|
2009-12-14 09:22:29 +01:00
|
|
|
if (UseProxy == "DIRECT")
|
2016-08-03 21:17:26 +02:00
|
|
|
return true;
|
2009-12-11 00:38:13 +01:00
|
|
|
|
2016-07-31 18:05:56 +02:00
|
|
|
// Parse no_proxy, a comma (,) separated list of domains we don't want to use
|
2016-04-27 16:55:55 -04:00
|
|
|
// a proxy for so we stop right here if it is in the list
|
|
|
|
|
if (getenv("no_proxy") != 0 && CheckDomainList(ServerName.Host,getenv("no_proxy")) == true)
|
2016-08-03 21:17:26 +02:00
|
|
|
return true;
|
2016-04-27 16:55:55 -04:00
|
|
|
|
|
|
|
|
if (UseProxy.empty() == true)
|
2006-12-19 12:03:30 +01:00
|
|
|
{
|
2016-07-31 18:05:56 +02:00
|
|
|
const char* result = nullptr;
|
|
|
|
|
if (std::find(methodNames.begin(), methodNames.end(), "https") != methodNames.end())
|
|
|
|
|
result = getenv("https_proxy");
|
2013-05-08 17:50:15 +02:00
|
|
|
// FIXME: Fall back to http_proxy is to remain compatible with
|
|
|
|
|
// existing setups and behaviour of apt.conf. This should be
|
|
|
|
|
// deprecated in the future (including apt.conf). Most other
|
|
|
|
|
// programs do not fall back to http proxy settings and neither
|
|
|
|
|
// should Apt.
|
2016-07-31 18:05:56 +02:00
|
|
|
if (result == nullptr && std::find(methodNames.begin(), methodNames.end(), "http") != methodNames.end())
|
|
|
|
|
result = getenv("http_proxy");
|
|
|
|
|
UseProxy = result == nullptr ? "" : result;
|
2008-12-15 21:17:39 +02:00
|
|
|
}
|
2009-12-11 00:38:13 +01:00
|
|
|
|
2006-12-19 12:03:30 +01:00
|
|
|
// Determine what host and port to use based on the proxy settings
|
2016-07-31 18:46:34 +02:00
|
|
|
if (UseProxy.empty() == false)
|
2006-12-19 12:03:30 +01:00
|
|
|
{
|
2009-12-14 09:22:29 +01:00
|
|
|
Proxy = UseProxy;
|
2016-08-06 22:54:31 +02:00
|
|
|
AddProxyAuth(Proxy, ServerName);
|
|
|
|
|
|
2016-07-31 18:46:34 +02:00
|
|
|
if (Proxy.Access == "socks5h")
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
|
|
|
|
|
else if (Proxy.Access == "socks5")
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
|
|
|
|
|
else if (Proxy.Access == "socks4a")
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4A);
|
|
|
|
|
else if (Proxy.Access == "socks")
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
|
2016-08-03 21:17:26 +02:00
|
|
|
else if (Proxy.Access == "http" || Proxy.Access == "https")
|
2016-07-31 18:46:34 +02:00
|
|
|
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
|
2016-08-03 21:17:26 +02:00
|
|
|
else
|
|
|
|
|
return false;
|
2016-07-31 18:46:34 +02:00
|
|
|
|
2009-12-14 09:22:29 +01:00
|
|
|
if (Proxy.Port != 1)
|
2006-12-19 12:03:30 +01:00
|
|
|
curl_easy_setopt(curl, CURLOPT_PROXYPORT, Proxy.Port);
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_PROXY, Proxy.Host.c_str());
|
2013-05-08 17:50:15 +02:00
|
|
|
if (Proxy.User.empty() == false || Proxy.Password.empty() == false)
|
|
|
|
|
{
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME, Proxy.User.c_str());
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD, Proxy.Password.c_str());
|
|
|
|
|
}
|
2006-12-19 12:03:30 +01:00
|
|
|
}
|
2016-08-03 21:17:26 +02:00
|
|
|
return true;
|
2009-12-11 00:38:13 +01:00
|
|
|
} /*}}}*/
|
2006-12-19 12:03:30 +01:00
|
|
|
// HttpsMethod::Fetch - Fetch an item /*{{{*/
|
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
|
/* This adds an item to the pipeline. We keep the pipeline at a fixed
|
|
|
|
|
depth. */
|
|
|
|
|
bool HttpsMethod::Fetch(FetchItem *Itm)
|
|
|
|
|
{
|
|
|
|
|
struct stat SBuf;
|
2015-03-09 01:34:10 +01:00
|
|
|
struct curl_slist *headers=NULL;
|
2006-12-19 13:11:26 +01:00
|
|
|
char curl_errorstr[CURL_ERROR_SIZE];
|
2008-07-25 19:37:19 +02:00
|
|
|
URI Uri = Itm->Uri;
|
2016-07-31 18:05:56 +02:00
|
|
|
setPostfixForMethodNames(Uri.Host.c_str());
|
|
|
|
|
AllowRedirect = ConfigFindB("AllowRedirect", true);
|
|
|
|
|
Debug = DebugEnabled();
|
2006-12-19 12:03:30 +01:00
|
|
|
|
|
|
|
|
// TODO:
|
|
|
|
|
// - http::Pipeline-Depth
|
|
|
|
|
// - error checking/reporting
|
|
|
|
|
// - more debug options? (CURLOPT_DEBUGFUNCTION?)
|
2016-07-31 18:05:56 +02:00
|
|
|
{
|
|
|
|
|
auto const plus = Binary.find('+');
|
|
|
|
|
if (plus != std::string::npos)
|
|
|
|
|
Uri.Access = Binary.substr(plus + 1);
|
|
|
|
|
}
|
2006-12-19 12:03:30 +01:00
|
|
|
|
2007-08-10 18:05:05 -03:00
|
|
|
curl_easy_reset(curl);
|
2016-08-03 21:17:26 +02:00
|
|
|
if (SetupProxy() == false)
|
|
|
|
|
return _error->Error("Unsupported proxy configured: %s", URI::SiteOnly(Proxy).c_str());
|
2006-12-19 12:03:30 +01:00
|
|
|
|
2009-10-16 15:42:05 +02:00
|
|
|
maybe_add_auth (Uri, _config->FindFile("Dir::Etc::netrc"));
|
2016-11-09 12:25:44 +01:00
|
|
|
if (Server == nullptr || Server->Comp(Itm->Uri) == false)
|
|
|
|
|
Server = CreateServerState(Itm->Uri);
|
2009-10-16 15:36:28 +02:00
|
|
|
|
2016-12-29 14:16:07 +01:00
|
|
|
// The "+" is encoded as a workaround for a amazon S3 bug
|
|
|
|
|
// see LP bugs #1003633 and #1086997. (taken from http method)
|
|
|
|
|
Uri.Path = QuoteString(Uri.Path, "+~ ");
|
|
|
|
|
|
2015-03-27 15:53:43 +01:00
|
|
|
FetchResult Res;
|
2016-11-09 12:25:44 +01:00
|
|
|
RequestState Req(this, Server.get());
|
|
|
|
|
CURLUserPointer userp(this, &Res, Itm, &Req);
|
2006-12-19 12:03:30 +01:00
|
|
|
// callbacks
|
2009-12-04 10:22:56 +01:00
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, static_cast<string>(Uri).c_str());
|
2013-09-30 16:41:16 +02:00
|
|
|
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, parse_header);
|
2015-03-27 15:53:43 +01:00
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &userp);
|
2006-12-19 12:03:30 +01:00
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
|
2016-11-09 12:25:44 +01:00
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &userp);
|
2014-02-14 17:11:07 +01:00
|
|
|
// options
|
2015-03-27 15:53:43 +01:00
|
|
|
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, true);
|
2007-08-10 18:05:05 -03:00
|
|
|
curl_easy_setopt(curl, CURLOPT_FILETIME, true);
|
2016-08-02 14:49:58 +02:00
|
|
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0);
|
2006-12-19 12:03:30 +01:00
|
|
|
|
2016-07-31 18:05:56 +02:00
|
|
|
if (std::find(methodNames.begin(), methodNames.end(), "https") != methodNames.end())
|
|
|
|
|
{
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
|
2010-01-08 22:28:49 +01:00
|
|
|
|
2016-07-31 18:05:56 +02:00
|
|
|
// File containing the list of trusted CA.
|
|
|
|
|
std::string const cainfo = ConfigFind("CaInfo", "");
|
|
|
|
|
if(cainfo.empty() == false)
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_CAINFO, cainfo.c_str());
|
|
|
|
|
// Check server certificate against previous CA list ...
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, ConfigFindB("Verify-Peer", true) ? 1 : 0);
|
|
|
|
|
// ... and hostname against cert CN or subjectAltName
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, ConfigFindB("Verify-Host", true) ? 2 : 0);
|
|
|
|
|
// Also enforce issuer of server certificate using its cert
|
|
|
|
|
std::string const issuercert = ConfigFind("IssuerCert", "");
|
|
|
|
|
if(issuercert.empty() == false)
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_ISSUERCERT, issuercert.c_str());
|
|
|
|
|
// For client authentication, certificate file ...
|
|
|
|
|
std::string const pem = ConfigFind("SslCert", "");
|
|
|
|
|
if(pem.empty() == false)
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSLCERT, pem.c_str());
|
|
|
|
|
// ... and associated key.
|
|
|
|
|
std::string const key = ConfigFind("SslKey", "");
|
|
|
|
|
if(key.empty() == false)
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSLKEY, key.c_str());
|
|
|
|
|
// Allow forcing SSL version to SSLv3 or TLSv1
|
|
|
|
|
long final_version = CURL_SSLVERSION_DEFAULT;
|
|
|
|
|
std::string const sslversion = ConfigFind("SslForceVersion", "");
|
|
|
|
|
if(sslversion == "TLSv1")
|
|
|
|
|
final_version = CURL_SSLVERSION_TLSv1;
|
|
|
|
|
else if(sslversion == "TLSv1.0")
|
|
|
|
|
final_version = CURL_SSLVERSION_TLSv1_0;
|
|
|
|
|
else if(sslversion == "TLSv1.1")
|
|
|
|
|
final_version = CURL_SSLVERSION_TLSv1_1;
|
|
|
|
|
else if(sslversion == "TLSv1.2")
|
|
|
|
|
final_version = CURL_SSLVERSION_TLSv1_2;
|
|
|
|
|
else if(sslversion == "SSLv3")
|
|
|
|
|
final_version = CURL_SSLVERSION_SSLv3;
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSLVERSION, final_version);
|
|
|
|
|
// CRL file
|
|
|
|
|
std::string const crlfile = ConfigFind("CrlFile", "");
|
|
|
|
|
if(crlfile.empty() == false)
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_CRLFILE, crlfile.c_str());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
|
|
|
|
|
}
|
2006-12-19 12:03:30 +01:00
|
|
|
// cache-control
|
2016-07-31 18:05:56 +02:00
|
|
|
if(ConfigFindB("No-Cache", false) == false)
|
2006-12-19 12:03:30 +01:00
|
|
|
{
|
|
|
|
|
// cache enabled
|
2016-07-31 18:05:56 +02:00
|
|
|
if (ConfigFindB("No-Store", false) == true)
|
2006-12-19 12:03:30 +01:00
|
|
|
headers = curl_slist_append(headers,"Cache-Control: no-store");
|
2016-07-31 18:05:56 +02:00
|
|
|
std::string ss;
|
|
|
|
|
strprintf(ss, "Cache-Control: max-age=%u", ConfigFindI("Max-Age", 0));
|
|
|
|
|
headers = curl_slist_append(headers, ss.c_str());
|
2006-12-19 12:03:30 +01:00
|
|
|
} else {
|
|
|
|
|
// cache disabled by user
|
|
|
|
|
headers = curl_slist_append(headers, "Cache-Control: no-cache");
|
|
|
|
|
headers = curl_slist_append(headers, "Pragma: no-cache");
|
|
|
|
|
}
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
|
|
|
|
// speed limit
|
2016-07-31 18:05:56 +02:00
|
|
|
int const dlLimit = ConfigFindI("Dl-Limit", 0) * 1024;
|
2006-12-19 12:03:30 +01:00
|
|
|
if (dlLimit > 0)
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE, dlLimit);
|
|
|
|
|
|
|
|
|
|
// set header
|
2016-07-31 18:05:56 +02:00
|
|
|
curl_easy_setopt(curl, CURLOPT_USERAGENT, ConfigFind("User-Agent", "Debian APT-CURL/1.0 (" PACKAGE_VERSION ")").c_str());
|
2006-12-19 12:03:30 +01:00
|
|
|
|
2008-05-02 12:26:17 -03:00
|
|
|
// set timeout
|
2016-07-31 18:05:56 +02:00
|
|
|
int const timeout = ConfigFindI("Timeout", 120);
|
2008-05-02 12:26:17 -03:00
|
|
|
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout);
|
2009-11-17 09:51:31 -06:00
|
|
|
//set really low lowspeed timeout (see #497983)
|
2009-11-17 09:55:22 -06:00
|
|
|
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, DL_MIN_SPEED);
|
2009-11-17 09:51:31 -06:00
|
|
|
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, timeout);
|
2008-05-02 12:26:17 -03:00
|
|
|
|
2016-12-08 13:48:12 +00:00
|
|
|
if(_config->FindB("Acquire::ForceIPv4", false) == true)
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
|
|
|
|
|
else if(_config->FindB("Acquire::ForceIPv6", false) == true)
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
|
|
|
|
|
|
2006-12-19 12:03:30 +01:00
|
|
|
// debug
|
2015-03-09 01:54:46 +01:00
|
|
|
if (Debug == true)
|
2006-12-19 12:03:30 +01:00
|
|
|
curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
|
|
|
|
|
|
2006-12-19 13:11:26 +01:00
|
|
|
// error handling
|
2013-01-09 11:00:13 +01:00
|
|
|
curl_errorstr[0] = '\0';
|
2006-12-19 13:11:26 +01:00
|
|
|
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
|
|
|
|
|
|
2012-01-30 16:26:10 +01:00
|
|
|
// If we ask for uncompressed files servers might respond with content-
|
2014-02-22 18:34:33 +01:00
|
|
|
// negotiation which lets us end up with compressed files we do not support,
|
2012-01-30 16:26:10 +01:00
|
|
|
// see 657029, 657560 and co, so if we have no extension on the request
|
|
|
|
|
// ask for text only. As a sidenote: If there is nothing to negotate servers
|
|
|
|
|
// seem to be nice and ignore it.
|
2016-07-31 18:05:56 +02:00
|
|
|
if (ConfigFindB("SendAccept", true))
|
2012-01-30 16:26:10 +01:00
|
|
|
{
|
|
|
|
|
size_t const filepos = Itm->Uri.find_last_of('/');
|
|
|
|
|
string const file = Itm->Uri.substr(filepos + 1);
|
|
|
|
|
if (flExtension(file) == file)
|
|
|
|
|
headers = curl_slist_append(headers, "Accept: text/*");
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-05 07:48:13 +02:00
|
|
|
// if we have the file send an if-range query with a range header
|
2016-08-11 18:24:35 +02:00
|
|
|
if (Server->RangesAllowed && stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
|
2007-08-02 19:23:41 +02:00
|
|
|
{
|
2014-04-24 10:49:41 +02:00
|
|
|
std::string Buf;
|
|
|
|
|
strprintf(Buf, "Range: bytes=%lli-", (long long) SBuf.st_size);
|
|
|
|
|
headers = curl_slist_append(headers, Buf.c_str());
|
2016-07-02 11:28:42 +02:00
|
|
|
strprintf(Buf, "If-Range: %s", TimeRFC1123(SBuf.st_mtime, false).c_str());
|
2014-04-24 10:49:41 +02:00
|
|
|
headers = curl_slist_append(headers, Buf.c_str());
|
2012-01-19 23:51:00 +01:00
|
|
|
}
|
2007-10-05 07:48:13 +02:00
|
|
|
else if(Itm->LastModified > 0)
|
|
|
|
|
{
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_TIMEVALUE, Itm->LastModified);
|
2007-08-02 19:23:41 +02:00
|
|
|
}
|
2006-12-19 12:03:30 +01:00
|
|
|
|
2015-04-11 10:23:52 +02:00
|
|
|
if (Server->InitHashes(Itm->ExpectedHashes) == false)
|
|
|
|
|
return false;
|
2013-09-30 13:42:33 +02:00
|
|
|
|
2006-12-19 12:03:30 +01:00
|
|
|
// keep apt updated
|
|
|
|
|
Res.Filename = Itm->DestFile;
|
|
|
|
|
|
|
|
|
|
// get it!
|
|
|
|
|
CURLcode success = curl_easy_perform(curl);
|
|
|
|
|
|
2013-05-08 17:45:17 +02:00
|
|
|
// If the server returns 200 OK but the If-Modified-Since condition is not
|
|
|
|
|
// met, CURLINFO_CONDITION_UNMET will be set to 1
|
|
|
|
|
long curl_condition_unmet = 0;
|
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_CONDITION_UNMET, &curl_condition_unmet);
|
2016-08-02 14:49:58 +02:00
|
|
|
if (curl_condition_unmet == 1)
|
2016-11-09 12:25:44 +01:00
|
|
|
Req.Result = 304;
|
2013-05-08 17:45:17 +02:00
|
|
|
|
2016-11-09 12:25:44 +01:00
|
|
|
Req.File.Close();
|
2013-09-30 13:42:33 +02:00
|
|
|
curl_slist_free_all(headers);
|
2011-09-20 18:21:21 +02:00
|
|
|
|
2006-12-19 12:03:30 +01:00
|
|
|
// cleanup
|
2015-10-12 22:26:36 +02:00
|
|
|
if (success != CURLE_OK)
|
2007-08-02 19:23:41 +02:00
|
|
|
{
|
2015-10-12 22:26:36 +02:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
|
#pragma GCC diagnostic ignored "-Wswitch"
|
|
|
|
|
switch (success)
|
|
|
|
|
{
|
|
|
|
|
case CURLE_COULDNT_RESOLVE_PROXY:
|
|
|
|
|
case CURLE_COULDNT_RESOLVE_HOST:
|
|
|
|
|
SetFailReason("ResolveFailure");
|
|
|
|
|
break;
|
|
|
|
|
case CURLE_COULDNT_CONNECT:
|
|
|
|
|
SetFailReason("ConnectionRefused");
|
|
|
|
|
break;
|
|
|
|
|
case CURLE_OPERATION_TIMEDOUT:
|
|
|
|
|
SetFailReason("Timeout");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
#pragma GCC diagnostic pop
|
2016-07-06 15:10:52 +02:00
|
|
|
// only take curls technical errors if we haven't our own
|
|
|
|
|
// (e.g. for the maximum size limit we have and curls can be confusing)
|
|
|
|
|
if (_error->PendingError() == false)
|
|
|
|
|
_error->Error("%s", curl_errorstr);
|
|
|
|
|
else
|
|
|
|
|
_error->Warning("curl: %s", curl_errorstr);
|
|
|
|
|
return false;
|
2013-09-30 13:42:33 +02:00
|
|
|
}
|
|
|
|
|
|
2016-11-09 12:25:44 +01:00
|
|
|
switch (DealWithHeaders(Res, Req))
|
2013-09-30 13:42:33 +02:00
|
|
|
{
|
2016-12-09 15:13:09 +01:00
|
|
|
case BaseHttpMethod::IMS_HIT:
|
2016-08-02 14:49:58 +02:00
|
|
|
URIDone(Res);
|
|
|
|
|
break;
|
2006-12-19 12:03:30 +01:00
|
|
|
|
2016-12-09 15:13:09 +01:00
|
|
|
case BaseHttpMethod::ERROR_WITH_CONTENT_PAGE:
|
2016-08-02 14:49:58 +02:00
|
|
|
// unlink, no need keep 401/404 page content in partial/
|
2016-11-09 12:25:44 +01:00
|
|
|
RemoveFile(Binary.c_str(), Req.File.Name());
|
2016-12-09 15:13:09 +01:00
|
|
|
case BaseHttpMethod::ERROR_UNRECOVERABLE:
|
|
|
|
|
case BaseHttpMethod::ERROR_NOT_FROM_SERVER:
|
2016-08-02 14:49:58 +02:00
|
|
|
return false;
|
|
|
|
|
|
2016-12-09 15:13:09 +01:00
|
|
|
case BaseHttpMethod::TRY_AGAIN_OR_REDIRECT:
|
2016-08-02 14:49:58 +02:00
|
|
|
Redirect(NextURI);
|
|
|
|
|
break;
|
|
|
|
|
|
2016-12-09 15:13:09 +01:00
|
|
|
case BaseHttpMethod::FILE_IS_OPEN:
|
2016-08-02 14:49:58 +02:00
|
|
|
struct stat resultStat;
|
2016-11-09 12:25:44 +01:00
|
|
|
if (unlikely(stat(Req.File.Name().c_str(), &resultStat) != 0))
|
2016-08-02 14:49:58 +02:00
|
|
|
{
|
2016-11-09 12:25:44 +01:00
|
|
|
_error->Errno("stat", "Unable to access file %s", Req.File.Name().c_str());
|
2016-08-02 14:49:58 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
Res.Size = resultStat.st_size;
|
|
|
|
|
|
|
|
|
|
// Timestamp
|
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_FILETIME, &Res.LastModified);
|
|
|
|
|
if (Res.LastModified != -1)
|
|
|
|
|
{
|
|
|
|
|
struct timeval times[2];
|
|
|
|
|
times[0].tv_sec = Res.LastModified;
|
|
|
|
|
times[1].tv_sec = Res.LastModified;
|
|
|
|
|
times[0].tv_usec = times[1].tv_usec = 0;
|
2016-11-09 12:25:44 +01:00
|
|
|
utimes(Req.File.Name().c_str(), times);
|
2016-08-02 14:49:58 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
Res.LastModified = resultStat.st_mtime;
|
|
|
|
|
|
|
|
|
|
// take hashes
|
|
|
|
|
Res.TakeHashes(*(Server->GetHashes()));
|
|
|
|
|
|
|
|
|
|
// keep apt updated
|
|
|
|
|
URIDone(Res);
|
|
|
|
|
break;
|
2013-09-30 13:42:33 +02:00
|
|
|
}
|
2015-03-09 01:54:46 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
/*}}}*/
|
2015-09-11 21:02:19 +02:00
|
|
|
std::unique_ptr<ServerState> HttpsMethod::CreateServerState(URI const &uri)/*{{{*/
|
2015-03-09 01:54:46 +01:00
|
|
|
{
|
2015-09-11 21:02:19 +02:00
|
|
|
return std::unique_ptr<ServerState>(new HttpsServerState(uri, this));
|
2015-03-09 01:54:46 +01:00
|
|
|
}
|
|
|
|
|
/*}}}*/
|
2016-12-09 15:13:09 +01:00
|
|
|
HttpsMethod::HttpsMethod(std::string &&pProg) : BaseHttpMethod(std::move(pProg),"1.2",Pipeline | SendConfig)/*{{{*/
|
2006-12-19 12:03:30 +01:00
|
|
|
{
|
2016-07-31 18:05:56 +02:00
|
|
|
auto addName = std::inserter(methodNames, methodNames.begin());
|
|
|
|
|
addName = "http";
|
|
|
|
|
auto const plus = Binary.find('+');
|
|
|
|
|
if (plus != std::string::npos)
|
|
|
|
|
{
|
|
|
|
|
addName = Binary.substr(plus + 1);
|
|
|
|
|
auto base = Binary.substr(0, plus);
|
|
|
|
|
if (base != "https")
|
|
|
|
|
addName = base;
|
|
|
|
|
}
|
|
|
|
|
if (std::find(methodNames.begin(), methodNames.end(), "https") != methodNames.end())
|
|
|
|
|
curl_global_init(CURL_GLOBAL_SSL);
|
|
|
|
|
else
|
|
|
|
|
curl_global_init(CURL_GLOBAL_NOTHING);
|
|
|
|
|
curl = curl_easy_init();
|
2006-12-19 12:03:30 +01:00
|
|
|
}
|
2016-07-31 18:05:56 +02:00
|
|
|
/*}}}*/
|
|
|
|
|
HttpsMethod::~HttpsMethod() /*{{{*/
|
|
|
|
|
{
|
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
}
|
|
|
|
|
/*}}}*/
|
|
|
|
|
int main(int, const char *argv[]) /*{{{*/
|
|
|
|
|
{
|
|
|
|
|
std::string Binary = flNotDir(argv[0]);
|
|
|
|
|
if (Binary.find('+') == std::string::npos && Binary != "https")
|
|
|
|
|
Binary.append("+https");
|
|
|
|
|
return HttpsMethod(std::move(Binary)).Run();
|
|
|
|
|
}
|
|
|
|
|
/*}}}*/
|