noproxy.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifndef CURL_DISABLE_PROXY
  26. #include <curl/curl.h> /* for curl_strnequal() */
  27. #include "curlx/inet_pton.h"
  28. #include "noproxy.h"
  29. #include "curlx/strparse.h"
  30. #ifdef HAVE_NETINET_IN_H
  31. #include <netinet/in.h>
  32. #endif
  33. #ifdef HAVE_ARPA_INET_H
  34. #include <arpa/inet.h>
  35. #endif
  36. /*
  37. * Curl_cidr4_match() returns TRUE if the given IPv4 address is within the
  38. * specified CIDR address range.
  39. */
  40. UNITTEST bool Curl_cidr4_match(const char *ipv4, /* 1.2.3.4 address */
  41. const char *network, /* 1.2.3.4 address */
  42. unsigned int bits)
  43. {
  44. unsigned int address = 0;
  45. unsigned int check = 0;
  46. if(bits > 32)
  47. /* strange input */
  48. return FALSE;
  49. if(curlx_inet_pton(AF_INET, ipv4, &address) != 1)
  50. return FALSE;
  51. if(curlx_inet_pton(AF_INET, network, &check) != 1)
  52. return FALSE;
  53. if(bits && (bits != 32)) {
  54. unsigned int mask = 0xffffffff << (32 - bits);
  55. unsigned int haddr = htonl(address);
  56. unsigned int hcheck = htonl(check);
  57. #if 0
  58. curl_mfprintf(stderr, "Host %s (%x) network %s (%x) "
  59. "bits %u mask %x => %x\n",
  60. ipv4, haddr, network, hcheck, bits, mask,
  61. (haddr ^ hcheck) & mask);
  62. #endif
  63. if((haddr ^ hcheck) & mask)
  64. return FALSE;
  65. return TRUE;
  66. }
  67. return address == check;
  68. }
  69. UNITTEST bool Curl_cidr6_match(const char *ipv6,
  70. const char *network,
  71. unsigned int bits)
  72. {
  73. #ifdef USE_IPV6
  74. unsigned int bytes;
  75. unsigned int rest;
  76. unsigned char address[16];
  77. unsigned char check[16];
  78. if(!bits)
  79. bits = 128;
  80. bytes = bits / 8;
  81. rest = bits & 0x07;
  82. if((bytes > 16) || ((bytes == 16) && rest))
  83. return FALSE;
  84. if(curlx_inet_pton(AF_INET6, ipv6, address) != 1)
  85. return FALSE;
  86. if(curlx_inet_pton(AF_INET6, network, check) != 1)
  87. return FALSE;
  88. if(bytes && memcmp(address, check, bytes))
  89. return FALSE;
  90. if(rest && ((address[bytes] ^ check[bytes]) & (0xff << (8 - rest))))
  91. return FALSE;
  92. return TRUE;
  93. #else
  94. (void)ipv6;
  95. (void)network;
  96. (void)bits;
  97. return FALSE;
  98. #endif
  99. }
  100. enum nametype {
  101. TYPE_HOST,
  102. TYPE_IPV4,
  103. TYPE_IPV6
  104. };
  105. /****************************************************************
  106. * Checks if the host is in the noproxy list. returns TRUE if it matches and
  107. * therefore the proxy should NOT be used.
  108. ****************************************************************/
  109. bool Curl_check_noproxy(const char *name, const char *no_proxy)
  110. {
  111. char hostip[128];
  112. /*
  113. * If we do not have a hostname at all, like for example with a FILE
  114. * transfer, we have nothing to interrogate the noproxy list with.
  115. */
  116. if(!name || name[0] == '\0')
  117. return FALSE;
  118. /* no_proxy=domain1.dom,host.domain2.dom
  119. * (a comma-separated list of hosts which should
  120. * not be proxied, or an asterisk to override
  121. * all proxy variables)
  122. */
  123. if(no_proxy && no_proxy[0]) {
  124. const char *p = no_proxy;
  125. size_t namelen;
  126. enum nametype type = TYPE_HOST;
  127. if(!strcmp("*", no_proxy))
  128. return TRUE;
  129. /* NO_PROXY was specified and it was not just an asterisk */
  130. if(name[0] == '[') {
  131. char *endptr;
  132. /* IPv6 numerical address */
  133. endptr = strchr(name, ']');
  134. if(!endptr)
  135. return FALSE;
  136. name++;
  137. namelen = endptr - name;
  138. if(namelen >= sizeof(hostip))
  139. return FALSE;
  140. memcpy(hostip, name, namelen);
  141. hostip[namelen] = 0;
  142. name = hostip;
  143. type = TYPE_IPV6;
  144. }
  145. else {
  146. unsigned int address;
  147. namelen = strlen(name);
  148. if(curlx_inet_pton(AF_INET, name, &address) == 1)
  149. type = TYPE_IPV4;
  150. else {
  151. /* ignore trailing dots in the hostname */
  152. if(name[namelen - 1] == '.')
  153. namelen--;
  154. }
  155. }
  156. while(*p) {
  157. const char *token;
  158. size_t tokenlen = 0;
  159. bool match = FALSE;
  160. /* pass blanks */
  161. curlx_str_passblanks(&p);
  162. token = p;
  163. /* pass over the pattern */
  164. while(*p && !ISBLANK(*p) && (*p != ',')) {
  165. p++;
  166. tokenlen++;
  167. }
  168. if(tokenlen) {
  169. switch(type) {
  170. case TYPE_HOST:
  171. /* ignore trailing dots in the token to check */
  172. if(token[tokenlen - 1] == '.')
  173. tokenlen--;
  174. if(tokenlen && (*token == '.')) {
  175. /* ignore leading token dot as well */
  176. token++;
  177. tokenlen--;
  178. }
  179. /* A: example.com matches 'example.com'
  180. B: www.example.com matches 'example.com'
  181. C: nonexample.com DOES NOT match 'example.com'
  182. */
  183. if(tokenlen == namelen)
  184. /* case A, exact match */
  185. match = curl_strnequal(token, name, namelen);
  186. else if(tokenlen < namelen) {
  187. /* case B, tailmatch domain */
  188. match = (name[namelen - tokenlen - 1] == '.') &&
  189. curl_strnequal(token, name + (namelen - tokenlen),
  190. tokenlen);
  191. }
  192. /* case C passes through, not a match */
  193. break;
  194. case TYPE_IPV4:
  195. case TYPE_IPV6: {
  196. const char *check = token;
  197. char *slash;
  198. unsigned int bits = 0;
  199. char checkip[128];
  200. if(tokenlen >= sizeof(checkip))
  201. /* this cannot match */
  202. break;
  203. /* copy the check name to a temp buffer */
  204. memcpy(checkip, check, tokenlen);
  205. checkip[tokenlen] = 0;
  206. check = checkip;
  207. slash = strchr(check, '/');
  208. /* if the slash is part of this token, use it */
  209. if(slash) {
  210. /* if the bits variable gets a crazy value here, that is fine as
  211. the value will then be rejected in the cidr function */
  212. bits = (unsigned int)atoi(slash + 1);
  213. *slash = 0; /* null-terminate there */
  214. }
  215. if(type == TYPE_IPV6)
  216. match = Curl_cidr6_match(name, check, bits);
  217. else
  218. match = Curl_cidr4_match(name, check, bits);
  219. break;
  220. }
  221. }
  222. if(match)
  223. return TRUE;
  224. } /* if(tokenlen) */
  225. /* pass blanks after pattern */
  226. curlx_str_passblanks(&p);
  227. /* if not a comma, this ends the loop */
  228. if(*p != ',')
  229. break;
  230. /* pass any number of commas */
  231. while(*p == ',')
  232. p++;
  233. } /* while(*p) */
  234. } /* NO_PROXY was specified and it was not just an asterisk */
  235. return FALSE;
  236. }
  237. #endif /* CURL_DISABLE_PROXY */