connect.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: connect.cc,v 1.10.2.1 2004/01/16 18:58:50 mdz Exp $
  4. /* ######################################################################
  5. Connect - Replacement connect call
  6. This was originally authored by Jason Gunthorpe <jgg@debian.org>
  7. and is placed in the Public Domain, do with it what you will.
  8. ##################################################################### */
  9. /*}}}*/
  10. // Include Files /*{{{*/
  11. #include <config.h>
  12. #include <apt-pkg/error.h>
  13. #include <apt-pkg/fileutl.h>
  14. #include <apt-pkg/strutl.h>
  15. #include <apt-pkg/acquire-method.h>
  16. #include <apt-pkg/configuration.h>
  17. #include <apt-pkg/srvrec.h>
  18. #include <stdio.h>
  19. #include <errno.h>
  20. #include <unistd.h>
  21. #include <sstream>
  22. #include <string.h>
  23. #include<set>
  24. #include<string>
  25. // Internet stuff
  26. #include <netinet/in.h>
  27. #include <sys/socket.h>
  28. #include <arpa/inet.h>
  29. #include <netdb.h>
  30. #include "connect.h"
  31. #include "rfc2553emu.h"
  32. #include <apti18n.h>
  33. /*}}}*/
  34. static std::string LastHost;
  35. static int LastPort = 0;
  36. static struct addrinfo *LastHostAddr = 0;
  37. static struct addrinfo *LastUsed = 0;
  38. static std::vector<SrvRec> SrvRecords;
  39. // Set of IP/hostnames that we timed out before or couldn't resolve
  40. static std::set<std::string> bad_addr;
  41. // RotateDNS - Select a new server from a DNS rotation /*{{{*/
  42. // ---------------------------------------------------------------------
  43. /* This is called during certain errors in order to recover by selecting a
  44. new server */
  45. void RotateDNS()
  46. {
  47. if (LastUsed != 0 && LastUsed->ai_next != 0)
  48. LastUsed = LastUsed->ai_next;
  49. else
  50. LastUsed = LastHostAddr;
  51. }
  52. /*}}}*/
  53. static bool ConnectionAllowed(char const * const Service, std::string const &Host)/*{{{*/
  54. {
  55. if (unlikely(Host.empty())) // the only legal empty host (RFC2782 '.' target) is detected by caller
  56. return false;
  57. if (APT::String::Endswith(Host, ".onion") && _config->FindB("Acquire::BlockDotOnion", true))
  58. {
  59. // TRANSLATOR: %s is e.g. Tor's ".onion" which would likely fail or leak info (RFC7686)
  60. _error->Error(_("Direct connection to %s domains is blocked by default."), ".onion");
  61. if (strcmp(Service, "http") == 0)
  62. _error->Error(_("If you meant to use Tor remember to use %s instead of %s."), "tor+http", "http");
  63. return false;
  64. }
  65. return true;
  66. }
  67. /*}}}*/
  68. // DoConnect - Attempt a connect operation /*{{{*/
  69. // ---------------------------------------------------------------------
  70. /* This helper function attempts a connection to a single address. */
  71. static bool DoConnect(struct addrinfo *Addr,std::string const &Host,
  72. unsigned long TimeOut,int &Fd,pkgAcqMethod *Owner)
  73. {
  74. // Show a status indicator
  75. char Name[NI_MAXHOST];
  76. char Service[NI_MAXSERV];
  77. Name[0] = 0;
  78. Service[0] = 0;
  79. getnameinfo(Addr->ai_addr,Addr->ai_addrlen,
  80. Name,sizeof(Name),Service,sizeof(Service),
  81. NI_NUMERICHOST|NI_NUMERICSERV);
  82. Owner->Status(_("Connecting to %s (%s)"),Host.c_str(),Name);
  83. // if that addr did timeout before, we do not try it again
  84. if(bad_addr.find(std::string(Name)) != bad_addr.end())
  85. return false;
  86. /* If this is an IP rotation store the IP we are using.. If something goes
  87. wrong this will get tacked onto the end of the error message */
  88. if (LastHostAddr->ai_next != 0)
  89. {
  90. std::stringstream ss;
  91. ioprintf(ss, _("[IP: %s %s]"),Name,Service);
  92. Owner->SetIP(ss.str());
  93. }
  94. // Get a socket
  95. if ((Fd = socket(Addr->ai_family,Addr->ai_socktype,
  96. Addr->ai_protocol)) < 0)
  97. return _error->Errno("socket",_("Could not create a socket for %s (f=%u t=%u p=%u)"),
  98. Name,Addr->ai_family,Addr->ai_socktype,Addr->ai_protocol);
  99. SetNonBlock(Fd,true);
  100. if (connect(Fd,Addr->ai_addr,Addr->ai_addrlen) < 0 &&
  101. errno != EINPROGRESS)
  102. return _error->Errno("connect",_("Cannot initiate the connection "
  103. "to %s:%s (%s)."),Host.c_str(),Service,Name);
  104. /* This implements a timeout for connect by opening the connection
  105. nonblocking */
  106. if (WaitFd(Fd,true,TimeOut) == false) {
  107. bad_addr.insert(bad_addr.begin(), std::string(Name));
  108. Owner->SetFailReason("Timeout");
  109. return _error->Error(_("Could not connect to %s:%s (%s), "
  110. "connection timed out"),Host.c_str(),Service,Name);
  111. }
  112. // Check the socket for an error condition
  113. unsigned int Err;
  114. unsigned int Len = sizeof(Err);
  115. if (getsockopt(Fd,SOL_SOCKET,SO_ERROR,&Err,&Len) != 0)
  116. return _error->Errno("getsockopt",_("Failed"));
  117. if (Err != 0)
  118. {
  119. errno = Err;
  120. if(errno == ECONNREFUSED)
  121. Owner->SetFailReason("ConnectionRefused");
  122. else if (errno == ETIMEDOUT)
  123. Owner->SetFailReason("ConnectionTimedOut");
  124. bad_addr.insert(bad_addr.begin(), std::string(Name));
  125. return _error->Errno("connect",_("Could not connect to %s:%s (%s)."),Host.c_str(),
  126. Service,Name);
  127. }
  128. return true;
  129. }
  130. /*}}}*/
  131. // Connect to a given Hostname /*{{{*/
  132. static bool ConnectToHostname(std::string const &Host, int const Port,
  133. const char * const Service, int DefPort, int &Fd,
  134. unsigned long const TimeOut, pkgAcqMethod * const Owner)
  135. {
  136. if (ConnectionAllowed(Service, Host) == false)
  137. return false;
  138. // Convert the port name/number
  139. char ServStr[300];
  140. if (Port != 0)
  141. snprintf(ServStr,sizeof(ServStr),"%i", Port);
  142. else
  143. snprintf(ServStr,sizeof(ServStr),"%s", Service);
  144. /* We used a cached address record.. Yes this is against the spec but
  145. the way we have setup our rotating dns suggests that this is more
  146. sensible */
  147. if (LastHost != Host || LastPort != Port)
  148. {
  149. Owner->Status(_("Connecting to %s"),Host.c_str());
  150. // Free the old address structure
  151. if (LastHostAddr != 0)
  152. {
  153. freeaddrinfo(LastHostAddr);
  154. LastHostAddr = 0;
  155. LastUsed = 0;
  156. }
  157. // We only understand SOCK_STREAM sockets.
  158. struct addrinfo Hints;
  159. memset(&Hints,0,sizeof(Hints));
  160. Hints.ai_socktype = SOCK_STREAM;
  161. Hints.ai_flags = 0;
  162. #ifdef AI_IDN
  163. if (_config->FindB("Acquire::Connect::IDN", true) == true)
  164. Hints.ai_flags |= AI_IDN;
  165. #endif
  166. // see getaddrinfo(3): only return address if system has such a address configured
  167. // useful if system is ipv4 only, to not get ipv6, but that fails if the system has
  168. // no address configured: e.g. offline and trying to connect to localhost.
  169. if (_config->FindB("Acquire::Connect::AddrConfig", true) == true)
  170. Hints.ai_flags |= AI_ADDRCONFIG;
  171. Hints.ai_protocol = 0;
  172. if(_config->FindB("Acquire::ForceIPv4", false) == true)
  173. Hints.ai_family = AF_INET;
  174. else if(_config->FindB("Acquire::ForceIPv6", false) == true)
  175. Hints.ai_family = AF_INET6;
  176. else
  177. Hints.ai_family = AF_UNSPEC;
  178. // if we couldn't resolve the host before, we don't try now
  179. if(bad_addr.find(Host) != bad_addr.end())
  180. return _error->Error(_("Could not resolve '%s'"),Host.c_str());
  181. // Resolve both the host and service simultaneously
  182. while (1)
  183. {
  184. int Res;
  185. if ((Res = getaddrinfo(Host.c_str(),ServStr,&Hints,&LastHostAddr)) != 0 ||
  186. LastHostAddr == 0)
  187. {
  188. if (Res == EAI_NONAME || Res == EAI_SERVICE)
  189. {
  190. if (DefPort != 0)
  191. {
  192. snprintf(ServStr, sizeof(ServStr), "%i", DefPort);
  193. DefPort = 0;
  194. continue;
  195. }
  196. bad_addr.insert(bad_addr.begin(), Host);
  197. Owner->SetFailReason("ResolveFailure");
  198. return _error->Error(_("Could not resolve '%s'"),Host.c_str());
  199. }
  200. if (Res == EAI_AGAIN)
  201. {
  202. Owner->SetFailReason("TmpResolveFailure");
  203. return _error->Error(_("Temporary failure resolving '%s'"),
  204. Host.c_str());
  205. }
  206. if (Res == EAI_SYSTEM)
  207. return _error->Errno("getaddrinfo", _("System error resolving '%s:%s'"),
  208. Host.c_str(),ServStr);
  209. return _error->Error(_("Something wicked happened resolving '%s:%s' (%i - %s)"),
  210. Host.c_str(),ServStr,Res,gai_strerror(Res));
  211. }
  212. break;
  213. }
  214. LastHost = Host;
  215. LastPort = Port;
  216. }
  217. // When we have an IP rotation stay with the last IP.
  218. struct addrinfo *CurHost = LastHostAddr;
  219. if (LastUsed != 0)
  220. CurHost = LastUsed;
  221. while (CurHost != 0)
  222. {
  223. if (DoConnect(CurHost,Host,TimeOut,Fd,Owner) == true)
  224. {
  225. LastUsed = CurHost;
  226. return true;
  227. }
  228. close(Fd);
  229. Fd = -1;
  230. // Ignore UNIX domain sockets
  231. do
  232. {
  233. CurHost = CurHost->ai_next;
  234. }
  235. while (CurHost != 0 && CurHost->ai_family == AF_UNIX);
  236. /* If we reached the end of the search list then wrap around to the
  237. start */
  238. if (CurHost == 0 && LastUsed != 0)
  239. CurHost = LastHostAddr;
  240. // Reached the end of the search cycle
  241. if (CurHost == LastUsed)
  242. break;
  243. if (CurHost != 0)
  244. _error->Discard();
  245. }
  246. if (_error->PendingError() == true)
  247. return false;
  248. return _error->Error(_("Unable to connect to %s:%s:"),Host.c_str(),ServStr);
  249. }
  250. /*}}}*/
  251. // Connect - Connect to a server /*{{{*/
  252. // ---------------------------------------------------------------------
  253. /* Performs a connection to the server (including SRV record lookup) */
  254. bool Connect(std::string Host,int Port,const char *Service,
  255. int DefPort,int &Fd,
  256. unsigned long TimeOut,pkgAcqMethod *Owner)
  257. {
  258. if (_error->PendingError() == true)
  259. return false;
  260. if (ConnectionAllowed(Service, Host) == false)
  261. return false;
  262. if(LastHost != Host || LastPort != Port)
  263. {
  264. SrvRecords.clear();
  265. if (_config->FindB("Acquire::EnableSrvRecords", true) == true)
  266. {
  267. GetSrvRecords(Host, DefPort, SrvRecords);
  268. // RFC2782 defines that a lonely '.' target is an abort reason
  269. if (SrvRecords.size() == 1 && SrvRecords[0].target.empty())
  270. return _error->Error("SRV records for %s indicate that "
  271. "%s service is not available at this domain", Host.c_str(), Service);
  272. }
  273. }
  274. size_t stackSize = 0;
  275. // try to connect in the priority order of the srv records
  276. std::string initialHost{std::move(Host)};
  277. while(SrvRecords.empty() == false)
  278. {
  279. _error->PushToStack();
  280. ++stackSize;
  281. // PopFromSrvRecs will also remove the server
  282. Host = PopFromSrvRecs(SrvRecords).target;
  283. auto const ret = ConnectToHostname(Host, Port, Service, DefPort, Fd, TimeOut, Owner);
  284. if (ret)
  285. {
  286. while(stackSize--)
  287. _error->RevertToStack();
  288. return true;
  289. }
  290. }
  291. Host = std::move(initialHost);
  292. // we have no (good) SrvRecords for this host, connect right away
  293. _error->PushToStack();
  294. ++stackSize;
  295. auto const ret = ConnectToHostname(Host, Port, Service, DefPort, Fd,
  296. TimeOut, Owner);
  297. while(stackSize--)
  298. if (ret)
  299. _error->RevertToStack();
  300. else
  301. _error->MergeWithStack();
  302. return ret;
  303. }