/src/CMake/Utilities/cmcurl/lib/proxy.c
Line | Count | Source |
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 | | #include "curl_setup.h" |
25 | | |
26 | | #ifndef CURL_DISABLE_PROXY |
27 | | |
28 | | #include "urldata.h" |
29 | | #include "curl_trc.h" |
30 | | #include "protocol.h" |
31 | | #include "proxy.h" |
32 | | #include "http_proxy.h" |
33 | | #include "strcase.h" |
34 | | #include "url.h" |
35 | | #include "vauth/vauth.h" |
36 | | #include "curlx/inet_pton.h" |
37 | | #include "curlx/strparse.h" |
38 | | |
39 | | #ifdef HAVE_NETINET_IN_H |
40 | | #include <netinet/in.h> |
41 | | #endif |
42 | | |
43 | | #ifdef HAVE_ARPA_INET_H |
44 | | #include <arpa/inet.h> |
45 | | #endif |
46 | | |
47 | | /* |
48 | | * cidr4_match() returns TRUE if the given IPv4 address is within the |
49 | | * specified CIDR address range. |
50 | | * |
51 | | * @unittest 1614 |
52 | | */ |
53 | | UNITTEST bool cidr4_match(const char *ipv4, /* 1.2.3.4 address */ |
54 | | const char *network, /* 1.2.3.4 address */ |
55 | | unsigned int bits); |
56 | | UNITTEST bool cidr4_match(const char *ipv4, /* 1.2.3.4 address */ |
57 | | const char *network, /* 1.2.3.4 address */ |
58 | | unsigned int bits) |
59 | 0 | { |
60 | 0 | unsigned int address = 0; |
61 | 0 | unsigned int check = 0; |
62 | |
|
63 | 0 | if(bits > 32) |
64 | | /* strange input */ |
65 | 0 | return FALSE; |
66 | | |
67 | 0 | if(curlx_inet_pton(AF_INET, ipv4, &address) != 1) |
68 | 0 | return FALSE; |
69 | 0 | if(curlx_inet_pton(AF_INET, network, &check) != 1) |
70 | 0 | return FALSE; |
71 | | |
72 | 0 | if(bits && (bits != 32)) { |
73 | 0 | unsigned int mask = 0xffffffff << (32 - bits); |
74 | 0 | unsigned int haddr = htonl(address); |
75 | 0 | unsigned int hcheck = htonl(check); |
76 | | #if 0 |
77 | | curl_mfprintf(stderr, "Host %s (%x) network %s (%x) " |
78 | | "bits %u mask %x => %x\n", |
79 | | ipv4, haddr, network, hcheck, bits, mask, |
80 | | (haddr ^ hcheck) & mask); |
81 | | #endif |
82 | 0 | if((haddr ^ hcheck) & mask) |
83 | 0 | return FALSE; |
84 | 0 | return TRUE; |
85 | 0 | } |
86 | 0 | return address == check; |
87 | 0 | } |
88 | | |
89 | | /* @unittest 1614 */ |
90 | | UNITTEST bool cidr6_match(const char *ipv6, const char *network, |
91 | | unsigned int bits); |
92 | | UNITTEST bool cidr6_match(const char *ipv6, const char *network, |
93 | | unsigned int bits) |
94 | 0 | { |
95 | 0 | #ifdef USE_IPV6 |
96 | 0 | unsigned int bytes; |
97 | 0 | unsigned int rest; |
98 | 0 | unsigned char address[16]; |
99 | 0 | unsigned char check[16]; |
100 | |
|
101 | 0 | if(!bits) |
102 | 0 | bits = 128; |
103 | |
|
104 | 0 | bytes = bits / 8; |
105 | 0 | rest = bits & 0x07; |
106 | 0 | if((bytes > 16) || ((bytes == 16) && rest)) |
107 | 0 | return FALSE; |
108 | 0 | if(curlx_inet_pton(AF_INET6, ipv6, address) != 1) |
109 | 0 | return FALSE; |
110 | 0 | if(curlx_inet_pton(AF_INET6, network, check) != 1) |
111 | 0 | return FALSE; |
112 | 0 | if(bytes && memcmp(address, check, bytes)) |
113 | 0 | return FALSE; |
114 | 0 | if(rest && ((address[bytes] ^ check[bytes]) & (0xff << (8 - rest)))) |
115 | 0 | return FALSE; |
116 | | |
117 | 0 | return TRUE; |
118 | | #else |
119 | | (void)ipv6; |
120 | | (void)network; |
121 | | (void)bits; |
122 | | return FALSE; |
123 | | #endif |
124 | 0 | } |
125 | | |
126 | | enum nametype { |
127 | | TYPE_HOST, |
128 | | TYPE_IPV4, |
129 | | TYPE_IPV6 |
130 | | }; |
131 | | |
132 | | static bool match_host(const char *token, size_t tokenlen, |
133 | | const char *name, size_t namelen) |
134 | 0 | { |
135 | 0 | bool match = FALSE; |
136 | | |
137 | | /* ignore trailing dots in the token to check */ |
138 | 0 | if(token[tokenlen - 1] == '.') |
139 | 0 | tokenlen--; |
140 | |
|
141 | 0 | if(tokenlen && (*token == '.')) { |
142 | | /* ignore leading token dot as well */ |
143 | 0 | token++; |
144 | 0 | tokenlen--; |
145 | 0 | } |
146 | | /* A: example.com matches 'example.com' |
147 | | B: www.example.com matches 'example.com' |
148 | | C: nonexample.com DOES NOT match 'example.com' |
149 | | */ |
150 | 0 | if(tokenlen == namelen) |
151 | | /* case A, exact match */ |
152 | 0 | match = curl_strnequal(token, name, namelen); |
153 | 0 | else if(tokenlen < namelen) { |
154 | | /* case B, tailmatch domain */ |
155 | 0 | match = (name[namelen - tokenlen - 1] == '.') && |
156 | 0 | curl_strnequal(token, name + (namelen - tokenlen), tokenlen); |
157 | 0 | } |
158 | | /* case C passes through, not a match */ |
159 | 0 | return match; |
160 | 0 | } |
161 | | |
162 | | static bool match_ip(int type, const char *token, size_t tokenlen, |
163 | | const char *name) |
164 | 0 | { |
165 | 0 | char *slash; |
166 | 0 | unsigned int bits = 0; |
167 | 0 | char checkip[128]; |
168 | 0 | if(tokenlen >= sizeof(checkip)) |
169 | | /* this cannot match */ |
170 | 0 | return FALSE; |
171 | | /* copy the check name to a temp buffer */ |
172 | 0 | memcpy(checkip, token, tokenlen); |
173 | 0 | checkip[tokenlen] = 0; |
174 | |
|
175 | 0 | slash = strchr(checkip, '/'); |
176 | | /* if the slash is part of this token, use it */ |
177 | 0 | if(slash) { |
178 | 0 | curl_off_t value; |
179 | 0 | const char *p = &slash[1]; |
180 | 0 | if(curlx_str_number(&p, &value, 128) || *p) |
181 | 0 | return FALSE; |
182 | | /* a too large value is rejected in the cidr function below */ |
183 | 0 | bits = (unsigned int)value; |
184 | 0 | *slash = 0; /* null-terminate there */ |
185 | 0 | } |
186 | 0 | if(type == TYPE_IPV6) |
187 | 0 | return cidr6_match(name, checkip, bits); |
188 | 0 | else |
189 | 0 | return cidr4_match(name, checkip, bits); |
190 | 0 | } |
191 | | |
192 | | /**************************************************************** |
193 | | * Checks if the host is in the noproxy list. returns TRUE if it matches and |
194 | | * therefore the proxy should NOT be used. |
195 | | ****************************************************************/ |
196 | | /* @unittest 1614 */ |
197 | | UNITTEST bool proxy_check_noproxy(const char *name, const char *no_proxy); |
198 | | UNITTEST bool proxy_check_noproxy(const char *name, const char *no_proxy) |
199 | 0 | { |
200 | | /* |
201 | | * If we do not have a hostname at all, like for example with a FILE |
202 | | * transfer, we have nothing to interrogate the noproxy list with. |
203 | | */ |
204 | 0 | if(!name || name[0] == '\0') |
205 | 0 | return FALSE; |
206 | | |
207 | | /* no_proxy=domain1.dom,host.domain2.dom |
208 | | * (a comma-separated list of hosts which should |
209 | | * not be proxied, or an asterisk to override |
210 | | * all proxy variables) |
211 | | */ |
212 | 0 | if(no_proxy && no_proxy[0]) { |
213 | 0 | const char *p = no_proxy; |
214 | 0 | size_t namelen; |
215 | 0 | char address[16]; |
216 | 0 | enum nametype type = TYPE_HOST; |
217 | 0 | if(!strcmp("*", no_proxy)) |
218 | 0 | return TRUE; |
219 | | |
220 | | /* NO_PROXY was specified and it was not only an asterisk */ |
221 | | |
222 | | /* Check if name is an IP address; if not, assume it being a hostname. */ |
223 | 0 | namelen = strlen(name); |
224 | 0 | if(curlx_inet_pton(AF_INET, name, &address) == 1) |
225 | 0 | type = TYPE_IPV4; |
226 | 0 | #ifdef USE_IPV6 |
227 | 0 | else if(curlx_inet_pton(AF_INET6, name, &address) == 1) |
228 | 0 | type = TYPE_IPV6; |
229 | 0 | #endif |
230 | 0 | else { |
231 | | /* ignore trailing dots in the hostname */ |
232 | 0 | if(name[namelen - 1] == '.') |
233 | 0 | namelen--; |
234 | 0 | } |
235 | |
|
236 | 0 | while(*p) { |
237 | 0 | const char *token; |
238 | 0 | size_t tokenlen = 0; |
239 | | |
240 | | /* pass blanks */ |
241 | 0 | curlx_str_passblanks(&p); |
242 | |
|
243 | 0 | token = p; |
244 | | /* pass over the pattern */ |
245 | 0 | while(*p && !ISBLANK(*p) && (*p != ',')) { |
246 | 0 | p++; |
247 | 0 | tokenlen++; |
248 | 0 | } |
249 | |
|
250 | 0 | if(tokenlen) { |
251 | 0 | bool match = FALSE; |
252 | 0 | if(type == TYPE_HOST) |
253 | 0 | match = match_host(token, tokenlen, name, namelen); |
254 | 0 | else |
255 | 0 | match = match_ip(type, token, tokenlen, name); |
256 | |
|
257 | 0 | if(match) |
258 | 0 | return TRUE; |
259 | 0 | } |
260 | | |
261 | | /* pass blanks after pattern */ |
262 | 0 | curlx_str_passblanks(&p); |
263 | | /* if not a comma, this ends the loop */ |
264 | 0 | if(*p != ',') |
265 | 0 | break; |
266 | | /* pass any number of commas */ |
267 | 0 | while(*p == ',') |
268 | 0 | p++; |
269 | 0 | } /* while(*p) */ |
270 | 0 | } /* NO_PROXY was specified and it was not only an asterisk */ |
271 | | |
272 | 0 | return FALSE; |
273 | 0 | } |
274 | | |
275 | | #ifndef CURL_DISABLE_HTTP |
276 | | |
277 | | /**************************************************************** |
278 | | * Detect what (if any) proxy to use. Remember that this selects a host |
279 | | * name and is not limited to HTTP proxies only. |
280 | | * The returned pointer must be freed by the caller. |
281 | | ****************************************************************/ |
282 | | static char *proxy_detect_proxy(struct Curl_easy *data, |
283 | | const struct Curl_scheme *scheme) |
284 | 0 | { |
285 | 0 | char *proxy = NULL; |
286 | | |
287 | | /* If proxy was not specified, we check for default proxy environment |
288 | | * variables, to enable i.e Lynx compliance: |
289 | | * |
290 | | * http_proxy=http://some.server.dom:port/ |
291 | | * https_proxy=http://some.server.dom:port/ |
292 | | * ftp_proxy=http://some.server.dom:port/ |
293 | | * no_proxy=domain1.dom,host.domain2.dom |
294 | | * (a comma-separated list of hosts which should |
295 | | * not be proxied, or an asterisk to override |
296 | | * all proxy variables) |
297 | | * all_proxy=http://some.server.dom:port/ |
298 | | * (seems to exist for the CERN www lib. Probably |
299 | | * the first to check for.) |
300 | | * |
301 | | * For compatibility, the all-uppercase versions of these variables are |
302 | | * checked if the lowercase versions do not exist. |
303 | | */ |
304 | 0 | char proxy_env[20]; |
305 | 0 | const char *envp; |
306 | 0 | VERBOSE(envp = proxy_env); |
307 | |
|
308 | 0 | curl_msnprintf(proxy_env, sizeof(proxy_env), "%s_proxy", scheme->name); |
309 | | |
310 | | /* read the protocol proxy: */ |
311 | 0 | proxy = curl_getenv(proxy_env); |
312 | | |
313 | | /* |
314 | | * We do not try the uppercase version of HTTP_PROXY because of |
315 | | * security reasons: |
316 | | * |
317 | | * When curl is used in a webserver application |
318 | | * environment (cgi or php), this environment variable can |
319 | | * be controlled by the web server user by setting the |
320 | | * http header 'Proxy:' to some value. |
321 | | * |
322 | | * This can cause 'internal' http/ftp requests to be |
323 | | * arbitrarily redirected by any external attacker. |
324 | | */ |
325 | 0 | if(!proxy && !curl_strequal("http_proxy", proxy_env)) { |
326 | | /* There was no lowercase variable, try the uppercase version: */ |
327 | 0 | Curl_strntoupper(proxy_env, proxy_env, sizeof(proxy_env)); |
328 | 0 | proxy = curl_getenv(proxy_env); |
329 | 0 | } |
330 | |
|
331 | 0 | if(!proxy) { |
332 | | #ifndef CURL_DISABLE_WEBSOCKETS |
333 | | /* websocket proxy fallbacks */ |
334 | | if(curl_strequal("ws_proxy", proxy_env)) { |
335 | | proxy = curl_getenv("http_proxy"); |
336 | | } |
337 | | else if(curl_strequal("wss_proxy", proxy_env)) { |
338 | | proxy = curl_getenv("https_proxy"); |
339 | | if(!proxy) |
340 | | proxy = curl_getenv("HTTPS_PROXY"); |
341 | | } |
342 | | if(!proxy) { |
343 | | #endif |
344 | 0 | envp = "all_proxy"; |
345 | 0 | proxy = curl_getenv(envp); /* default proxy to use */ |
346 | 0 | if(!proxy) { |
347 | 0 | envp = "ALL_PROXY"; |
348 | 0 | proxy = curl_getenv(envp); |
349 | 0 | } |
350 | | #ifndef CURL_DISABLE_WEBSOCKETS |
351 | | } |
352 | | #endif |
353 | 0 | } |
354 | 0 | if(proxy) |
355 | 0 | infof(data, "Uses proxy env variable %s == '%s'", envp, proxy); |
356 | |
|
357 | 0 | return proxy; |
358 | 0 | } |
359 | | #endif /* CURL_DISABLE_HTTP */ |
360 | | |
361 | | /* |
362 | | * If this is supposed to use a proxy, we need to figure out the proxy |
363 | | * hostname, so that we can reuse an existing connection |
364 | | * that may exist registered to the same proxy host. |
365 | | */ |
366 | | static CURLcode parse_proxy(struct Curl_easy *data, |
367 | | const char *proxy, |
368 | | bool for_pre_proxy, |
369 | | struct proxy_info *proxyinfo) |
370 | 0 | { |
371 | 0 | char *proxyuser = NULL; |
372 | 0 | char *proxypasswd = NULL; |
373 | 0 | char *scheme = NULL; |
374 | 0 | CURLcode result = CURLE_OK; |
375 | | /* Set the start proxy type for URL scheme guessing */ |
376 | 0 | uint8_t proxytype = for_pre_proxy ? CURLPROXY_SOCKS4 : data->set.proxytype; |
377 | 0 | CURLU *uhp = curl_url(); |
378 | 0 | CURLUcode uc; |
379 | |
|
380 | 0 | if(!uhp) { |
381 | 0 | result = CURLE_OUT_OF_MEMORY; |
382 | 0 | goto error; |
383 | 0 | } |
384 | | /* When parsing the proxy, allowing non-supported schemes since we have |
385 | | these made up ones for proxies. Guess scheme for URLs without it. */ |
386 | 0 | uc = curl_url_set(uhp, CURLUPART_URL, proxy, |
387 | 0 | CURLU_NON_SUPPORT_SCHEME | CURLU_GUESS_SCHEME); |
388 | 0 | if(!uc) { |
389 | | /* parsed okay as a URL - only update proxytype when scheme was explicit */ |
390 | 0 | uc = curl_url_get(uhp, CURLUPART_SCHEME, &scheme, CURLU_NO_GUESS_SCHEME); |
391 | 0 | if(!uc) { |
392 | 0 | result = Curl_scheme_to_proxytype(data, scheme, &proxytype, proxy); |
393 | 0 | if(result) |
394 | 0 | goto error; |
395 | 0 | } |
396 | 0 | else if(uc != CURLUE_NO_SCHEME) { |
397 | 0 | result = CURLE_OUT_OF_MEMORY; |
398 | 0 | goto error; |
399 | 0 | } |
400 | | /* else: no explicit scheme, keep the configured proxytype */ |
401 | 0 | } |
402 | 0 | else { |
403 | 0 | failf(data, "Unsupported proxy syntax in \'%s\': %s", proxy, |
404 | 0 | curl_url_strerror(uc)); |
405 | 0 | result = CURLE_COULDNT_RESOLVE_PROXY; |
406 | 0 | goto error; |
407 | 0 | } |
408 | | |
409 | 0 | result = Curl_peer_from_proxy_url(uhp, data, proxy, proxytype, |
410 | 0 | &proxyinfo->peer, &proxytype); |
411 | 0 | if(result) |
412 | 0 | goto error; |
413 | | |
414 | 0 | switch(proxytype) { |
415 | 0 | case CURLPROXY_HTTP: |
416 | 0 | case CURLPROXY_HTTP_1_0: |
417 | 0 | case CURLPROXY_HTTPS: |
418 | 0 | case CURLPROXY_HTTPS2: |
419 | 0 | case CURLPROXY_HTTPS3: |
420 | 0 | if(for_pre_proxy) { |
421 | 0 | failf(data, "Unsupported pre-proxy type for \'%s\'", proxy); |
422 | 0 | result = CURLE_COULDNT_RESOLVE_PROXY; |
423 | 0 | goto error; |
424 | 0 | } |
425 | 0 | break; |
426 | 0 | case CURLPROXY_SOCKS4: |
427 | 0 | case CURLPROXY_SOCKS4A: |
428 | 0 | case CURLPROXY_SOCKS5: |
429 | 0 | case CURLPROXY_SOCKS5_HOSTNAME: |
430 | 0 | break; |
431 | 0 | default: |
432 | 0 | failf(data, "Unsupported proxy type %u for \'%s\'", proxytype, proxy); |
433 | 0 | result = CURLE_COULDNT_RESOLVE_PROXY; |
434 | 0 | goto error; |
435 | 0 | } |
436 | | |
437 | | /* Is there a username and password given in this proxy URL? */ |
438 | 0 | uc = curl_url_get(uhp, CURLUPART_USER, &proxyuser, CURLU_URLDECODE); |
439 | 0 | if(uc && (uc != CURLUE_NO_USER)) { |
440 | 0 | result = Curl_uc_to_curlcode(uc); |
441 | 0 | goto error; |
442 | 0 | } |
443 | 0 | uc = curl_url_get(uhp, CURLUPART_PASSWORD, &proxypasswd, CURLU_URLDECODE); |
444 | 0 | if(uc && (uc != CURLUE_NO_PASSWORD)) { |
445 | 0 | result = Curl_uc_to_curlcode(uc); |
446 | 0 | goto error; |
447 | 0 | } |
448 | | |
449 | 0 | if(proxyuser || proxypasswd) { |
450 | 0 | result = Curl_creds_create(proxyuser, proxypasswd, NULL, NULL, |
451 | 0 | data->set.str[STRING_PROXY_SERVICE_NAME], |
452 | 0 | CREDS_URL, &proxyinfo->creds); |
453 | 0 | if(result) |
454 | 0 | goto error; |
455 | 0 | } |
456 | 0 | else if(!for_pre_proxy && |
457 | 0 | (data->set.str[STRING_PROXYUSERNAME] || |
458 | 0 | data->set.str[STRING_PROXYPASSWORD] || |
459 | 0 | data->set.str[STRING_PROXY_SERVICE_NAME])) { |
460 | | /* No user/passwd in URL, if this is not a pre-proxy, the |
461 | | * CURLOPT_PROXY* settings apply. */ |
462 | 0 | result = Curl_creds_create(data->set.str[STRING_PROXYUSERNAME], |
463 | 0 | data->set.str[STRING_PROXYPASSWORD], |
464 | 0 | NULL, NULL, |
465 | 0 | data->set.str[STRING_PROXY_SERVICE_NAME], |
466 | 0 | CREDS_OPTION, &proxyinfo->creds); |
467 | 0 | } |
468 | 0 | else |
469 | 0 | Curl_creds_unlink(&proxyinfo->creds); |
470 | | |
471 | 0 | proxyinfo->proxytype = proxytype; |
472 | |
|
473 | 0 | error: |
474 | 0 | curlx_free(scheme); |
475 | 0 | curlx_free(proxyuser); |
476 | 0 | curlx_free(proxypasswd); |
477 | 0 | curl_url_cleanup(uhp); |
478 | | #ifdef DEBUGBUILD |
479 | | if(!result) { |
480 | | DEBUGASSERT(proxyinfo); |
481 | | DEBUGASSERT(proxyinfo->peer); |
482 | | } |
483 | | #endif |
484 | 0 | return result; |
485 | 0 | } |
486 | | |
487 | | /* Is transfer's origin exempted from proxy use? */ |
488 | | static bool proxy_do_not_proxy(struct Curl_easy *data) |
489 | 0 | { |
490 | 0 | const char *no_proxy; |
491 | 0 | char *env_no_proxy = NULL; |
492 | 0 | bool do_not_proxy; |
493 | | |
494 | | /* no proxying if the transfer does not use the network */ |
495 | 0 | if(data->state.origin->scheme->flags & PROTOPT_NONETWORK) |
496 | 0 | return TRUE; |
497 | | |
498 | 0 | no_proxy = data->set.str[STRING_NOPROXY]; |
499 | 0 | if(!no_proxy) { |
500 | 0 | const char *p = "no_proxy"; |
501 | 0 | env_no_proxy = curl_getenv(p); |
502 | 0 | if(!env_no_proxy) { |
503 | 0 | p = "NO_PROXY"; |
504 | 0 | env_no_proxy = curl_getenv(p); |
505 | 0 | } |
506 | 0 | if(env_no_proxy) |
507 | 0 | infof(data, "Uses proxy env variable %s == '%s'", p, env_no_proxy); |
508 | 0 | no_proxy = env_no_proxy; |
509 | 0 | } |
510 | |
|
511 | 0 | do_not_proxy = proxy_check_noproxy(data->state.origin->hostname, no_proxy); |
512 | 0 | curlx_safefree(env_no_proxy); |
513 | 0 | return do_not_proxy; |
514 | 0 | } |
515 | | |
516 | | CURLcode Curl_proxy_init_conn(struct Curl_easy *data, |
517 | | struct connectdata *conn) |
518 | 0 | { |
519 | 0 | char *proxy = NULL; |
520 | 0 | char *pre_proxy = NULL; |
521 | 0 | bool do_env_detect = TRUE; |
522 | 0 | CURLcode result = CURLE_OK; |
523 | | |
524 | | /* Enforce no proxy use unless we decide to use one */ |
525 | 0 | conn->bits.origin_is_proxy = FALSE; |
526 | 0 | DEBUGASSERT(!conn->socks_proxy.peer); |
527 | 0 | DEBUGASSERT(!conn->http_proxy.peer); |
528 | |
|
529 | 0 | if(proxy_do_not_proxy(data)) |
530 | 0 | goto out; |
531 | | |
532 | | /************************************************************* |
533 | | * Detect what (if any) proxy to use |
534 | | *************************************************************/ |
535 | | /* the empty config strings disable proxy use and env detects */ |
536 | 0 | if(data->set.str[STRING_PROXY]) { |
537 | 0 | if(*data->set.str[STRING_PROXY]) { |
538 | 0 | proxy = curlx_strdup(data->set.str[STRING_PROXY]); |
539 | | /* if global proxy is set, this is it */ |
540 | 0 | if(!proxy) { |
541 | 0 | failf(data, "memory shortage"); |
542 | 0 | result = CURLE_OUT_OF_MEMORY; |
543 | 0 | goto out; |
544 | 0 | } |
545 | 0 | } |
546 | 0 | else |
547 | 0 | do_env_detect = FALSE; |
548 | 0 | } |
549 | | |
550 | 0 | if(data->set.str[STRING_PRE_PROXY]) { |
551 | 0 | if(*data->set.str[STRING_PRE_PROXY]) { |
552 | 0 | pre_proxy = curlx_strdup(data->set.str[STRING_PRE_PROXY]); |
553 | | /* if global socks proxy is set, this is it */ |
554 | 0 | if(!pre_proxy) { |
555 | 0 | failf(data, "memory shortage"); |
556 | 0 | result = CURLE_OUT_OF_MEMORY; |
557 | 0 | goto out; |
558 | 0 | } |
559 | 0 | } |
560 | 0 | else |
561 | 0 | do_env_detect = FALSE; |
562 | 0 | } |
563 | | |
564 | 0 | #ifndef CURL_DISABLE_HTTP |
565 | | /* None configured, detect possible proxy from environment. */ |
566 | 0 | if(!proxy && !pre_proxy && do_env_detect) |
567 | 0 | proxy = proxy_detect_proxy(data, conn->scheme); |
568 | | #else |
569 | | (void)do_env_detect; |
570 | | #endif /* CURL_DISABLE_HTTP */ |
571 | |
|
572 | 0 | if(!proxy && !pre_proxy) |
573 | 0 | goto out; |
574 | | |
575 | 0 | if(pre_proxy) { |
576 | 0 | result = parse_proxy(data, pre_proxy, TRUE, &conn->socks_proxy); |
577 | 0 | if(result) |
578 | 0 | goto out; |
579 | 0 | } |
580 | | |
581 | 0 | if(proxy) { |
582 | 0 | result = parse_proxy(data, proxy, FALSE, &conn->http_proxy); |
583 | 0 | if(result) |
584 | 0 | goto out; |
585 | | |
586 | 0 | switch(conn->http_proxy.proxytype) { |
587 | 0 | case CURLPROXY_SOCKS4: |
588 | 0 | case CURLPROXY_SOCKS4A: |
589 | 0 | case CURLPROXY_SOCKS5: |
590 | 0 | case CURLPROXY_SOCKS5_HOSTNAME: |
591 | | /* Whoops, it is not an HTTP proxy */ |
592 | 0 | if(pre_proxy) { |
593 | | /* and we already have a SOCKS pre-proxy. Cannot have both */ |
594 | 0 | failf(data, "Having a SOCKS pre-proxy and proxy is not " |
595 | 0 | "supported with \'%s\'", proxy); |
596 | 0 | result = CURLE_COULDNT_RESOLVE_PROXY; |
597 | 0 | goto out; |
598 | 0 | } |
599 | | /* switch */ |
600 | 0 | conn->socks_proxy = conn->http_proxy; |
601 | 0 | memset(&conn->http_proxy, 0, sizeof(conn->http_proxy)); |
602 | 0 | break; |
603 | 0 | default: |
604 | | /* all other types are HTTP */ |
605 | 0 | break; |
606 | 0 | } |
607 | 0 | } |
608 | | |
609 | 0 | if(conn->socks_proxy.peer) { |
610 | 0 | DEBUGASSERT(!CURL_PROXY_IS_ANY_HTTP(conn->socks_proxy.proxytype)); |
611 | 0 | } |
612 | |
|
613 | | #ifdef CURL_DISABLE_HTTP |
614 | | if(conn->http_proxy.peer) { |
615 | | /* asking for an HTTP proxy is a bit funny when HTTP is disabled... */ |
616 | | result = CURLE_UNSUPPORTED_PROTOCOL; |
617 | | goto out; |
618 | | } |
619 | | |
620 | | #else /* CURL_DISABLE_HTTP */ |
621 | 0 | if(conn->http_proxy.peer) { |
622 | 0 | const struct Curl_scheme *scheme = data->state.origin->scheme; |
623 | 0 | bool tunnel_proxy = (bool)data->set.tunnel_thru_httpproxy; |
624 | 0 | DEBUGASSERT(CURL_PROXY_IS_ANY_HTTP(conn->http_proxy.proxytype)); |
625 | |
|
626 | 0 | if(!tunnel_proxy) { |
627 | | /* Decide if we tunnel through proxy automatically */ |
628 | 0 | if(conn->via_peer) { |
629 | | /* With connect-to, we always tunnel */ |
630 | 0 | tunnel_proxy = TRUE; |
631 | 0 | } |
632 | 0 | else if(scheme->flags & PROTOPT_SSL) { |
633 | | /* If the transfer is supposed to be secure, we tunnel */ |
634 | 0 | tunnel_proxy = TRUE; |
635 | 0 | } |
636 | 0 | else if(scheme->flags & PROTOPT_HTTP_PROXY_TUNNEL) { |
637 | | /* transfer scheme required tunneling */ |
638 | 0 | tunnel_proxy = TRUE; |
639 | 0 | } |
640 | 0 | else if(!(scheme->protocol & PROTO_FAMILY_HTTP) && |
641 | 0 | !(scheme->flags & PROTOPT_PROXY_AS_HTTP)) { |
642 | | /* Cannot delegate transfer URL to HTTP proxy */ |
643 | 0 | tunnel_proxy = TRUE; |
644 | 0 | } |
645 | 0 | } |
646 | |
|
647 | 0 | if(!tunnel_proxy) { |
648 | | /* HTTP proxy used in forwarding mode. This means the connection |
649 | | * is really to the proxy and NOT the origin of the transfer. */ |
650 | 0 | DEBUGASSERT(!conn->via_peer); |
651 | 0 | Curl_peer_link(&conn->origin, conn->http_proxy.peer); |
652 | 0 | conn->scheme = conn->http_proxy.peer->scheme; |
653 | 0 | conn->bits.origin_is_proxy = TRUE; |
654 | 0 | } |
655 | |
|
656 | 0 | #ifndef CURL_DISABLE_DIGEST_AUTH |
657 | 0 | if(!Curl_safecmp(data->state.envproxy, proxy)) { |
658 | | /* proxy changed */ |
659 | 0 | Curl_auth_digest_cleanup(&data->state.proxydigest); |
660 | 0 | curlx_free(data->state.envproxy); |
661 | 0 | data->state.envproxy = curlx_strdup(proxy); |
662 | 0 | } |
663 | 0 | #endif |
664 | 0 | } |
665 | 0 | #endif /* !CURL_DISABLE_HTTP */ |
666 | |
|
667 | 0 | out: |
668 | 0 | curlx_free(pre_proxy); |
669 | 0 | curlx_free(proxy); |
670 | 0 | return result; |
671 | 0 | } |
672 | | |
673 | | #endif /* CURL_DISABLE_PROXY */ |