Coverage Report

Created: 2024-02-25 06:14

/src/PROJ/curl/lib/noproxy.c
Line
Count
Source (jump to first uncovered line)
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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
25
#include "curl_setup.h"
26
27
#ifndef CURL_DISABLE_PROXY
28
29
#include "inet_pton.h"
30
#include "strcase.h"
31
#include "noproxy.h"
32
33
#ifdef HAVE_NETINET_IN_H
34
#include <netinet/in.h>
35
#endif
36
37
#ifdef HAVE_ARPA_INET_H
38
#include <arpa/inet.h>
39
#endif
40
41
/*
42
 * Curl_cidr4_match() returns TRUE if the given IPv4 address is within the
43
 * specified CIDR address range.
44
 */
45
UNITTEST bool Curl_cidr4_match(const char *ipv4,    /* 1.2.3.4 address */
46
                               const char *network, /* 1.2.3.4 address */
47
                               unsigned int bits)
48
0
{
49
0
  unsigned int address = 0;
50
0
  unsigned int check = 0;
51
52
0
  if(bits > 32)
53
    /* strange input */
54
0
    return FALSE;
55
56
0
  if(1 != Curl_inet_pton(AF_INET, ipv4, &address))
57
0
    return FALSE;
58
0
  if(1 != Curl_inet_pton(AF_INET, network, &check))
59
0
    return FALSE;
60
61
0
  if(bits && (bits != 32)) {
62
0
    unsigned int mask = 0xffffffff << (32 - bits);
63
0
    unsigned int haddr = htonl(address);
64
0
    unsigned int hcheck = htonl(check);
65
#if 0
66
    fprintf(stderr, "Host %s (%x) network %s (%x) bits %u mask %x => %x\n",
67
            ipv4, haddr, network, hcheck, bits, mask,
68
            (haddr ^ hcheck) & mask);
69
#endif
70
0
    if((haddr ^ hcheck) & mask)
71
0
      return FALSE;
72
0
    return TRUE;
73
0
  }
74
0
  return (address == check);
75
0
}
76
77
UNITTEST bool Curl_cidr6_match(const char *ipv6,
78
                               const char *network,
79
                               unsigned int bits)
80
0
{
81
0
#ifdef ENABLE_IPV6
82
0
  int bytes;
83
0
  int rest;
84
0
  unsigned char address[16];
85
0
  unsigned char check[16];
86
87
0
  if(!bits)
88
0
    bits = 128;
89
90
0
  bytes = bits/8;
91
0
  rest = bits & 0x07;
92
0
  if(1 != Curl_inet_pton(AF_INET6, ipv6, address))
93
0
    return FALSE;
94
0
  if(1 != Curl_inet_pton(AF_INET6, network, check))
95
0
    return FALSE;
96
0
  if((bytes > 16) || ((bytes == 16) && rest))
97
0
    return FALSE;
98
0
  if(bytes && memcmp(address, check, bytes))
99
0
    return FALSE;
100
0
  if(rest && !((address[bytes] ^ check[bytes]) & (0xff << (8 - rest))))
101
0
    return FALSE;
102
103
0
  return TRUE;
104
#else
105
  (void)ipv6;
106
  (void)network;
107
  (void)bits;
108
  return FALSE;
109
#endif
110
0
}
111
112
enum nametype {
113
  TYPE_HOST,
114
  TYPE_IPV4,
115
  TYPE_IPV6
116
};
117
118
/****************************************************************
119
* Checks if the host is in the noproxy list. returns TRUE if it matches and
120
* therefore the proxy should NOT be used.
121
****************************************************************/
122
bool Curl_check_noproxy(const char *name, const char *no_proxy,
123
                        bool *spacesep)
124
0
{
125
0
  char hostip[128];
126
0
  *spacesep = FALSE;
127
  /*
128
   * If we don't have a hostname at all, like for example with a FILE
129
   * transfer, we have nothing to interrogate the noproxy list with.
130
   */
131
0
  if(!name || name[0] == '\0')
132
0
    return FALSE;
133
134
  /* no_proxy=domain1.dom,host.domain2.dom
135
   *   (a comma-separated list of hosts which should
136
   *   not be proxied, or an asterisk to override
137
   *   all proxy variables)
138
   */
139
0
  if(no_proxy && no_proxy[0]) {
140
0
    const char *p = no_proxy;
141
0
    size_t namelen;
142
0
    enum nametype type = TYPE_HOST;
143
0
    if(!strcmp("*", no_proxy))
144
0
      return TRUE;
145
146
    /* NO_PROXY was specified and it wasn't just an asterisk */
147
148
0
    if(name[0] == '[') {
149
0
      char *endptr;
150
      /* IPv6 numerical address */
151
0
      endptr = strchr(name, ']');
152
0
      if(!endptr)
153
0
        return FALSE;
154
0
      name++;
155
0
      namelen = endptr - name;
156
0
      if(namelen >= sizeof(hostip))
157
0
        return FALSE;
158
0
      memcpy(hostip, name, namelen);
159
0
      hostip[namelen] = 0;
160
0
      name = hostip;
161
0
      type = TYPE_IPV6;
162
0
    }
163
0
    else {
164
0
      unsigned int address;
165
0
      namelen = strlen(name);
166
0
      if(1 == Curl_inet_pton(AF_INET, name, &address))
167
0
        type = TYPE_IPV4;
168
0
      else {
169
        /* ignore trailing dots in the host name */
170
0
        if(name[namelen - 1] == '.')
171
0
          namelen--;
172
0
      }
173
0
    }
174
175
0
    while(*p) {
176
0
      const char *token;
177
0
      size_t tokenlen = 0;
178
0
      bool match = FALSE;
179
180
      /* pass blanks */
181
0
      while(*p && ISBLANK(*p))
182
0
        p++;
183
184
0
      token = p;
185
      /* pass over the pattern */
186
0
      while(*p && !ISBLANK(*p) && (*p != ',')) {
187
0
        p++;
188
0
        tokenlen++;
189
0
      }
190
191
0
      if(tokenlen) {
192
0
        switch(type) {
193
0
        case TYPE_HOST:
194
          /* ignore trailing dots in the token to check */
195
0
          if(token[tokenlen - 1] == '.')
196
0
            tokenlen--;
197
198
0
          if(tokenlen && (*token == '.')) {
199
            /* ignore leading token dot as well */
200
0
            token++;
201
0
            tokenlen--;
202
0
          }
203
          /* A: example.com matches 'example.com'
204
             B: www.example.com matches 'example.com'
205
             C: nonexample.com DOES NOT match 'example.com'
206
          */
207
0
          if(tokenlen == namelen)
208
            /* case A, exact match */
209
0
            match = strncasecompare(token, name, namelen);
210
0
          else if(tokenlen < namelen) {
211
            /* case B, tailmatch domain */
212
0
            match = (name[namelen - tokenlen - 1] == '.') &&
213
0
              strncasecompare(token, name + (namelen - tokenlen),
214
0
                              tokenlen);
215
0
          }
216
          /* case C passes through, not a match */
217
0
          break;
218
0
        case TYPE_IPV4:
219
0
        case TYPE_IPV6: {
220
0
          const char *check = token;
221
0
          char *slash;
222
0
          unsigned int bits = 0;
223
0
          char checkip[128];
224
0
          if(tokenlen >= sizeof(checkip))
225
            /* this cannot match */
226
0
            break;
227
          /* copy the check name to a temp buffer */
228
0
          memcpy(checkip, check, tokenlen);
229
0
          checkip[tokenlen] = 0;
230
0
          check = checkip;
231
232
0
          slash = strchr(check, '/');
233
          /* if the slash is part of this token, use it */
234
0
          if(slash) {
235
0
            bits = atoi(slash + 1);
236
0
            *slash = 0; /* null terminate there */
237
0
          }
238
0
          if(type == TYPE_IPV6)
239
0
            match = Curl_cidr6_match(name, check, bits);
240
0
          else
241
0
            match = Curl_cidr4_match(name, check, bits);
242
0
          break;
243
0
        }
244
0
        }
245
0
        if(match)
246
0
          return TRUE;
247
0
      } /* if(tokenlen) */
248
      /* pass blanks after pattern */
249
0
      while(ISBLANK(*p))
250
0
        p++;
251
      /* if not a comma! */
252
0
      if(*p && (*p != ',')) {
253
0
        *spacesep = TRUE;
254
0
        continue;
255
0
      }
256
      /* pass any number of commas */
257
0
      while(*p == ',')
258
0
        p++;
259
0
    } /* while(*p) */
260
0
  } /* NO_PROXY was specified and it wasn't just an asterisk */
261
262
0
  return FALSE;
263
0
}
264
265
#endif /* CURL_DISABLE_PROXY */