113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci
2513498266Sopenharmony_ci#include "curl_setup.h"
2613498266Sopenharmony_ci
2713498266Sopenharmony_ci#ifndef CURL_DISABLE_PROXY
2813498266Sopenharmony_ci
2913498266Sopenharmony_ci#include "inet_pton.h"
3013498266Sopenharmony_ci#include "strcase.h"
3113498266Sopenharmony_ci#include "noproxy.h"
3213498266Sopenharmony_ci
3313498266Sopenharmony_ci#ifdef HAVE_NETINET_IN_H
3413498266Sopenharmony_ci#include <netinet/in.h>
3513498266Sopenharmony_ci#endif
3613498266Sopenharmony_ci
3713498266Sopenharmony_ci#ifdef HAVE_ARPA_INET_H
3813498266Sopenharmony_ci#include <arpa/inet.h>
3913498266Sopenharmony_ci#endif
4013498266Sopenharmony_ci
4113498266Sopenharmony_ci/*
4213498266Sopenharmony_ci * Curl_cidr4_match() returns TRUE if the given IPv4 address is within the
4313498266Sopenharmony_ci * specified CIDR address range.
4413498266Sopenharmony_ci */
4513498266Sopenharmony_ciUNITTEST bool Curl_cidr4_match(const char *ipv4,    /* 1.2.3.4 address */
4613498266Sopenharmony_ci                               const char *network, /* 1.2.3.4 address */
4713498266Sopenharmony_ci                               unsigned int bits)
4813498266Sopenharmony_ci{
4913498266Sopenharmony_ci  unsigned int address = 0;
5013498266Sopenharmony_ci  unsigned int check = 0;
5113498266Sopenharmony_ci
5213498266Sopenharmony_ci  if(bits > 32)
5313498266Sopenharmony_ci    /* strange input */
5413498266Sopenharmony_ci    return FALSE;
5513498266Sopenharmony_ci
5613498266Sopenharmony_ci  if(1 != Curl_inet_pton(AF_INET, ipv4, &address))
5713498266Sopenharmony_ci    return FALSE;
5813498266Sopenharmony_ci  if(1 != Curl_inet_pton(AF_INET, network, &check))
5913498266Sopenharmony_ci    return FALSE;
6013498266Sopenharmony_ci
6113498266Sopenharmony_ci  if(bits && (bits != 32)) {
6213498266Sopenharmony_ci    unsigned int mask = 0xffffffff << (32 - bits);
6313498266Sopenharmony_ci    unsigned int haddr = htonl(address);
6413498266Sopenharmony_ci    unsigned int hcheck = htonl(check);
6513498266Sopenharmony_ci#if 0
6613498266Sopenharmony_ci    fprintf(stderr, "Host %s (%x) network %s (%x) bits %u mask %x => %x\n",
6713498266Sopenharmony_ci            ipv4, haddr, network, hcheck, bits, mask,
6813498266Sopenharmony_ci            (haddr ^ hcheck) & mask);
6913498266Sopenharmony_ci#endif
7013498266Sopenharmony_ci    if((haddr ^ hcheck) & mask)
7113498266Sopenharmony_ci      return FALSE;
7213498266Sopenharmony_ci    return TRUE;
7313498266Sopenharmony_ci  }
7413498266Sopenharmony_ci  return (address == check);
7513498266Sopenharmony_ci}
7613498266Sopenharmony_ci
7713498266Sopenharmony_ciUNITTEST bool Curl_cidr6_match(const char *ipv6,
7813498266Sopenharmony_ci                               const char *network,
7913498266Sopenharmony_ci                               unsigned int bits)
8013498266Sopenharmony_ci{
8113498266Sopenharmony_ci#ifdef ENABLE_IPV6
8213498266Sopenharmony_ci  int bytes;
8313498266Sopenharmony_ci  int rest;
8413498266Sopenharmony_ci  unsigned char address[16];
8513498266Sopenharmony_ci  unsigned char check[16];
8613498266Sopenharmony_ci
8713498266Sopenharmony_ci  if(!bits)
8813498266Sopenharmony_ci    bits = 128;
8913498266Sopenharmony_ci
9013498266Sopenharmony_ci  bytes = bits/8;
9113498266Sopenharmony_ci  rest = bits & 0x07;
9213498266Sopenharmony_ci  if(1 != Curl_inet_pton(AF_INET6, ipv6, address))
9313498266Sopenharmony_ci    return FALSE;
9413498266Sopenharmony_ci  if(1 != Curl_inet_pton(AF_INET6, network, check))
9513498266Sopenharmony_ci    return FALSE;
9613498266Sopenharmony_ci  if((bytes > 16) || ((bytes == 16) && rest))
9713498266Sopenharmony_ci    return FALSE;
9813498266Sopenharmony_ci  if(bytes && memcmp(address, check, bytes))
9913498266Sopenharmony_ci    return FALSE;
10013498266Sopenharmony_ci  if(rest && !((address[bytes] ^ check[bytes]) & (0xff << (8 - rest))))
10113498266Sopenharmony_ci    return FALSE;
10213498266Sopenharmony_ci
10313498266Sopenharmony_ci  return TRUE;
10413498266Sopenharmony_ci#else
10513498266Sopenharmony_ci  (void)ipv6;
10613498266Sopenharmony_ci  (void)network;
10713498266Sopenharmony_ci  (void)bits;
10813498266Sopenharmony_ci  return FALSE;
10913498266Sopenharmony_ci#endif
11013498266Sopenharmony_ci}
11113498266Sopenharmony_ci
11213498266Sopenharmony_cienum nametype {
11313498266Sopenharmony_ci  TYPE_HOST,
11413498266Sopenharmony_ci  TYPE_IPV4,
11513498266Sopenharmony_ci  TYPE_IPV6
11613498266Sopenharmony_ci};
11713498266Sopenharmony_ci
11813498266Sopenharmony_ci/****************************************************************
11913498266Sopenharmony_ci* Checks if the host is in the noproxy list. returns TRUE if it matches and
12013498266Sopenharmony_ci* therefore the proxy should NOT be used.
12113498266Sopenharmony_ci****************************************************************/
12213498266Sopenharmony_cibool Curl_check_noproxy(const char *name, const char *no_proxy,
12313498266Sopenharmony_ci                        bool *spacesep)
12413498266Sopenharmony_ci{
12513498266Sopenharmony_ci  char hostip[128];
12613498266Sopenharmony_ci  *spacesep = FALSE;
12713498266Sopenharmony_ci  /*
12813498266Sopenharmony_ci   * If we don't have a hostname at all, like for example with a FILE
12913498266Sopenharmony_ci   * transfer, we have nothing to interrogate the noproxy list with.
13013498266Sopenharmony_ci   */
13113498266Sopenharmony_ci  if(!name || name[0] == '\0')
13213498266Sopenharmony_ci    return FALSE;
13313498266Sopenharmony_ci
13413498266Sopenharmony_ci  /* no_proxy=domain1.dom,host.domain2.dom
13513498266Sopenharmony_ci   *   (a comma-separated list of hosts which should
13613498266Sopenharmony_ci   *   not be proxied, or an asterisk to override
13713498266Sopenharmony_ci   *   all proxy variables)
13813498266Sopenharmony_ci   */
13913498266Sopenharmony_ci  if(no_proxy && no_proxy[0]) {
14013498266Sopenharmony_ci    const char *p = no_proxy;
14113498266Sopenharmony_ci    size_t namelen;
14213498266Sopenharmony_ci    enum nametype type = TYPE_HOST;
14313498266Sopenharmony_ci    if(!strcmp("*", no_proxy))
14413498266Sopenharmony_ci      return TRUE;
14513498266Sopenharmony_ci
14613498266Sopenharmony_ci    /* NO_PROXY was specified and it wasn't just an asterisk */
14713498266Sopenharmony_ci
14813498266Sopenharmony_ci    if(name[0] == '[') {
14913498266Sopenharmony_ci      char *endptr;
15013498266Sopenharmony_ci      /* IPv6 numerical address */
15113498266Sopenharmony_ci      endptr = strchr(name, ']');
15213498266Sopenharmony_ci      if(!endptr)
15313498266Sopenharmony_ci        return FALSE;
15413498266Sopenharmony_ci      name++;
15513498266Sopenharmony_ci      namelen = endptr - name;
15613498266Sopenharmony_ci      if(namelen >= sizeof(hostip))
15713498266Sopenharmony_ci        return FALSE;
15813498266Sopenharmony_ci      memcpy(hostip, name, namelen);
15913498266Sopenharmony_ci      hostip[namelen] = 0;
16013498266Sopenharmony_ci      name = hostip;
16113498266Sopenharmony_ci      type = TYPE_IPV6;
16213498266Sopenharmony_ci    }
16313498266Sopenharmony_ci    else {
16413498266Sopenharmony_ci      unsigned int address;
16513498266Sopenharmony_ci      namelen = strlen(name);
16613498266Sopenharmony_ci      if(1 == Curl_inet_pton(AF_INET, name, &address))
16713498266Sopenharmony_ci        type = TYPE_IPV4;
16813498266Sopenharmony_ci      else {
16913498266Sopenharmony_ci        /* ignore trailing dots in the host name */
17013498266Sopenharmony_ci        if(name[namelen - 1] == '.')
17113498266Sopenharmony_ci          namelen--;
17213498266Sopenharmony_ci      }
17313498266Sopenharmony_ci    }
17413498266Sopenharmony_ci
17513498266Sopenharmony_ci    while(*p) {
17613498266Sopenharmony_ci      const char *token;
17713498266Sopenharmony_ci      size_t tokenlen = 0;
17813498266Sopenharmony_ci      bool match = FALSE;
17913498266Sopenharmony_ci
18013498266Sopenharmony_ci      /* pass blanks */
18113498266Sopenharmony_ci      while(*p && ISBLANK(*p))
18213498266Sopenharmony_ci        p++;
18313498266Sopenharmony_ci
18413498266Sopenharmony_ci      token = p;
18513498266Sopenharmony_ci      /* pass over the pattern */
18613498266Sopenharmony_ci      while(*p && !ISBLANK(*p) && (*p != ',')) {
18713498266Sopenharmony_ci        p++;
18813498266Sopenharmony_ci        tokenlen++;
18913498266Sopenharmony_ci      }
19013498266Sopenharmony_ci
19113498266Sopenharmony_ci      if(tokenlen) {
19213498266Sopenharmony_ci        switch(type) {
19313498266Sopenharmony_ci        case TYPE_HOST:
19413498266Sopenharmony_ci          /* ignore trailing dots in the token to check */
19513498266Sopenharmony_ci          if(token[tokenlen - 1] == '.')
19613498266Sopenharmony_ci            tokenlen--;
19713498266Sopenharmony_ci
19813498266Sopenharmony_ci          if(tokenlen && (*token == '.')) {
19913498266Sopenharmony_ci            /* ignore leading token dot as well */
20013498266Sopenharmony_ci            token++;
20113498266Sopenharmony_ci            tokenlen--;
20213498266Sopenharmony_ci          }
20313498266Sopenharmony_ci          /* A: example.com matches 'example.com'
20413498266Sopenharmony_ci             B: www.example.com matches 'example.com'
20513498266Sopenharmony_ci             C: nonexample.com DOES NOT match 'example.com'
20613498266Sopenharmony_ci          */
20713498266Sopenharmony_ci          if(tokenlen == namelen)
20813498266Sopenharmony_ci            /* case A, exact match */
20913498266Sopenharmony_ci            match = strncasecompare(token, name, namelen);
21013498266Sopenharmony_ci          else if(tokenlen < namelen) {
21113498266Sopenharmony_ci            /* case B, tailmatch domain */
21213498266Sopenharmony_ci            match = (name[namelen - tokenlen - 1] == '.') &&
21313498266Sopenharmony_ci              strncasecompare(token, name + (namelen - tokenlen),
21413498266Sopenharmony_ci                              tokenlen);
21513498266Sopenharmony_ci          }
21613498266Sopenharmony_ci          /* case C passes through, not a match */
21713498266Sopenharmony_ci          break;
21813498266Sopenharmony_ci        case TYPE_IPV4:
21913498266Sopenharmony_ci        case TYPE_IPV6: {
22013498266Sopenharmony_ci          const char *check = token;
22113498266Sopenharmony_ci          char *slash;
22213498266Sopenharmony_ci          unsigned int bits = 0;
22313498266Sopenharmony_ci          char checkip[128];
22413498266Sopenharmony_ci          if(tokenlen >= sizeof(checkip))
22513498266Sopenharmony_ci            /* this cannot match */
22613498266Sopenharmony_ci            break;
22713498266Sopenharmony_ci          /* copy the check name to a temp buffer */
22813498266Sopenharmony_ci          memcpy(checkip, check, tokenlen);
22913498266Sopenharmony_ci          checkip[tokenlen] = 0;
23013498266Sopenharmony_ci          check = checkip;
23113498266Sopenharmony_ci
23213498266Sopenharmony_ci          slash = strchr(check, '/');
23313498266Sopenharmony_ci          /* if the slash is part of this token, use it */
23413498266Sopenharmony_ci          if(slash) {
23513498266Sopenharmony_ci            bits = atoi(slash + 1);
23613498266Sopenharmony_ci            *slash = 0; /* null terminate there */
23713498266Sopenharmony_ci          }
23813498266Sopenharmony_ci          if(type == TYPE_IPV6)
23913498266Sopenharmony_ci            match = Curl_cidr6_match(name, check, bits);
24013498266Sopenharmony_ci          else
24113498266Sopenharmony_ci            match = Curl_cidr4_match(name, check, bits);
24213498266Sopenharmony_ci          break;
24313498266Sopenharmony_ci        }
24413498266Sopenharmony_ci        }
24513498266Sopenharmony_ci        if(match)
24613498266Sopenharmony_ci          return TRUE;
24713498266Sopenharmony_ci      } /* if(tokenlen) */
24813498266Sopenharmony_ci      /* pass blanks after pattern */
24913498266Sopenharmony_ci      while(ISBLANK(*p))
25013498266Sopenharmony_ci        p++;
25113498266Sopenharmony_ci      /* if not a comma! */
25213498266Sopenharmony_ci      if(*p && (*p != ',')) {
25313498266Sopenharmony_ci        *spacesep = TRUE;
25413498266Sopenharmony_ci        continue;
25513498266Sopenharmony_ci      }
25613498266Sopenharmony_ci      /* pass any number of commas */
25713498266Sopenharmony_ci      while(*p == ',')
25813498266Sopenharmony_ci        p++;
25913498266Sopenharmony_ci    } /* while(*p) */
26013498266Sopenharmony_ci  } /* NO_PROXY was specified and it wasn't just an asterisk */
26113498266Sopenharmony_ci
26213498266Sopenharmony_ci  return FALSE;
26313498266Sopenharmony_ci}
26413498266Sopenharmony_ci
26513498266Sopenharmony_ci#endif /* CURL_DISABLE_PROXY */
266