Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Utilities / cmcurl / lib / noproxy.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2022, 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 /*
38  * Curl_cidr4_match() returns TRUE if the given IPv4 address is within the
39  * specified CIDR address range.
40  */
41 UNITTEST bool Curl_cidr4_match(const char *ipv4,    /* 1.2.3.4 address */
42                                const char *network, /* 1.2.3.4 address */
43                                unsigned int bits)
44 {
45   unsigned int address = 0;
46   unsigned int check = 0;
47
48   if(bits > 32)
49     /* strange input */
50     return FALSE;
51
52   if(1 != Curl_inet_pton(AF_INET, ipv4, &address))
53     return FALSE;
54   if(1 != Curl_inet_pton(AF_INET, network, &check))
55     return FALSE;
56
57   if(bits && (bits != 32)) {
58     unsigned int mask = 0xffffffff << (32 - bits);
59     unsigned int haddr = htonl(address);
60     unsigned int hcheck = htonl(check);
61 #if 0
62     fprintf(stderr, "Host %s (%x) network %s (%x) bits %u mask %x => %x\n",
63             ipv4, haddr, network, hcheck, bits, mask,
64             (haddr ^ hcheck) & mask);
65 #endif
66     if((haddr ^ hcheck) & mask)
67       return FALSE;
68     return TRUE;
69   }
70   return (address == check);
71 }
72
73 UNITTEST bool Curl_cidr6_match(const char *ipv6,
74                                const char *network,
75                                unsigned int bits)
76 {
77 #ifdef ENABLE_IPV6
78   int bytes;
79   int rest;
80   unsigned char address[16];
81   unsigned char check[16];
82
83   if(!bits)
84     bits = 128;
85
86   bytes = bits/8;
87   rest = bits & 0x07;
88   if(1 != Curl_inet_pton(AF_INET6, ipv6, address))
89     return FALSE;
90   if(1 != Curl_inet_pton(AF_INET6, network, check))
91     return FALSE;
92   if((bytes > 16) || ((bytes == 16) && rest))
93     return FALSE;
94   if(bytes && memcmp(address, check, bytes))
95     return FALSE;
96   if(rest && !((address[bytes] ^ check[bytes]) & (0xff << (8 - rest))))
97     return FALSE;
98
99   return TRUE;
100 #else
101   (void)ipv6;
102   (void)network;
103   (void)bits;
104   return FALSE;
105 #endif
106 }
107
108 enum nametype {
109   TYPE_HOST,
110   TYPE_IPV4,
111   TYPE_IPV6
112 };
113
114 /****************************************************************
115 * Checks if the host is in the noproxy list. returns TRUE if it matches and
116 * therefore the proxy should NOT be used.
117 ****************************************************************/
118 bool Curl_check_noproxy(const char *name, const char *no_proxy)
119 {
120   /* no_proxy=domain1.dom,host.domain2.dom
121    *   (a comma-separated list of hosts which should
122    *   not be proxied, or an asterisk to override
123    *   all proxy variables)
124    */
125   if(no_proxy && no_proxy[0]) {
126     const char *p = no_proxy;
127     size_t namelen;
128     enum nametype type = TYPE_HOST;
129     char hostip[128];
130     if(!strcmp("*", no_proxy))
131       return TRUE;
132
133     /* NO_PROXY was specified and it wasn't just an asterisk */
134
135     if(name[0] == '[') {
136       char *endptr;
137       /* IPv6 numerical address */
138       endptr = strchr(name, ']');
139       if(!endptr)
140         return FALSE;
141       name++;
142       namelen = endptr - name;
143       if(namelen >= sizeof(hostip))
144         return FALSE;
145       memcpy(hostip, name, namelen);
146       hostip[namelen] = 0;
147       name = hostip;
148       type = TYPE_IPV6;
149     }
150     else {
151       unsigned int address;
152       if(1 == Curl_inet_pton(AF_INET, name, &address))
153         type = TYPE_IPV4;
154       namelen = strlen(name);
155     }
156
157     while(*p) {
158       const char *token;
159       size_t tokenlen = 0;
160       bool match = FALSE;
161
162       /* pass blanks */
163       while(*p && ISBLANK(*p))
164         p++;
165
166       token = p;
167       /* pass over the pattern */
168       while(*p && !ISBLANK(*p) && (*p != ',')) {
169         p++;
170         tokenlen++;
171       }
172
173       if(tokenlen) {
174         switch(type) {
175         case TYPE_HOST:
176           if(*token == '.') {
177             ++token;
178             --tokenlen;
179             /* tailmatch */
180             match = (tokenlen <= namelen) &&
181               strncasecompare(token, name + (namelen - tokenlen), namelen);
182           }
183           else
184             match = (tokenlen == namelen) &&
185               strncasecompare(token, name, namelen);
186           break;
187         case TYPE_IPV4:
188           /* FALLTHROUGH */
189         case TYPE_IPV6: {
190           const char *check = token;
191           char *slash = strchr(check, '/');
192           unsigned int bits = 0;
193           char checkip[128];
194           /* if the slash is part of this token, use it */
195           if(slash && (slash < &check[tokenlen])) {
196             bits = atoi(slash + 1);
197             /* copy the check name to a temp buffer */
198             if(tokenlen >= sizeof(checkip))
199               break;
200             memcpy(checkip, check, tokenlen);
201             checkip[ slash - check ] = 0;
202             check = checkip;
203           }
204           if(type == TYPE_IPV6)
205             match = Curl_cidr6_match(name, check, bits);
206           else
207             match = Curl_cidr4_match(name, check, bits);
208           break;
209         }
210         }
211         if(match)
212           return TRUE;
213       } /* if(tokenlen) */
214       while(*p == ',')
215         p++;
216     } /* while(*p) */
217   } /* NO_PROXY was specified and it wasn't just an asterisk */
218
219   return FALSE;
220 }
221
222 #endif /* CURL_DISABLE_PROXY */