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 | | /* If proxy was not specified, we check for default proxy environment |
286 | | * variables, to enable i.e Lynx compliance: |
287 | | * |
288 | | * http_proxy=http://some.server.dom:port/ |
289 | | * https_proxy=http://some.server.dom:port/ |
290 | | * ftp_proxy=http://some.server.dom:port/ |
291 | | * no_proxy=domain1.dom,host.domain2.dom |
292 | | * (a comma-separated list of hosts which should |
293 | | * not be proxied, or an asterisk to override |
294 | | * all proxy variables) |
295 | | * all_proxy=http://some.server.dom:port/ |
296 | | * (seems to exist for the CERN www lib. Probably |
297 | | * the first to check for.) |
298 | | * |
299 | | * For compatibility, the all-uppercase versions of these variables are |
300 | | * checked if the lowercase versions do not exist. |
301 | | */ |
302 | 0 | const char *env_name = NULL; |
303 | 0 | char *proxy = NULL; |
304 | 0 | char name_buf[20]; |
305 | | |
306 | | /* Try scheme specific env var first, unless http(s). |
307 | | * lowercase first, then uppercase. */ |
308 | 0 | if((scheme != &Curl_scheme_https) && (scheme != &Curl_scheme_http)) { |
309 | 0 | curl_msnprintf(name_buf, sizeof(name_buf), "%s_proxy", scheme->name); |
310 | 0 | env_name = name_buf; |
311 | 0 | proxy = curl_getenv(env_name); |
312 | 0 | if(!proxy) { |
313 | 0 | Curl_strntoupper(name_buf, name_buf, sizeof(name_buf)); |
314 | 0 | proxy = curl_getenv(env_name); |
315 | 0 | } |
316 | 0 | } |
317 | |
|
318 | 0 | if(!proxy && |
319 | 0 | ((scheme == &Curl_scheme_https) || (scheme == &Curl_scheme_wss))) { |
320 | | /* Not found, check 'https' env vars, also for 'wss'. |
321 | | * Again, first lowercase then uppercase. */ |
322 | 0 | env_name = "https_proxy"; |
323 | 0 | proxy = curl_getenv(env_name); |
324 | 0 | if(!proxy) { |
325 | 0 | env_name = "HTTPS_PROXY"; |
326 | 0 | proxy = curl_getenv(env_name); |
327 | 0 | } |
328 | 0 | } |
329 | 0 | else if(!proxy && |
330 | 0 | ((scheme == &Curl_scheme_http) || (scheme == &Curl_scheme_ws))) { |
331 | | /* Not found, check 'http' env vars, also for 'ws'. |
332 | | * We do NOT try the uppercase version 'HTTP_PROXY' because of |
333 | | * security reasons: |
334 | | * |
335 | | * When curl is used in a webserver application |
336 | | * environment (cgi or php), this environment variable can |
337 | | * be controlled by the web server user by setting the |
338 | | * http header 'Proxy:' to some value. |
339 | | * |
340 | | * This can cause 'internal' http/ftp requests to be |
341 | | * arbitrarily redirected by any external attacker. |
342 | | */ |
343 | 0 | env_name = "http_proxy"; |
344 | 0 | proxy = curl_getenv(env_name); |
345 | 0 | } |
346 | |
|
347 | 0 | if(!proxy) { |
348 | | /* still not found, last resort checks. */ |
349 | 0 | env_name = "all_proxy"; |
350 | 0 | proxy = curl_getenv(env_name); |
351 | 0 | if(!proxy) { |
352 | 0 | env_name = "ALL_PROXY"; |
353 | 0 | proxy = curl_getenv(env_name); |
354 | 0 | } |
355 | 0 | } |
356 | |
|
357 | 0 | if(proxy) |
358 | 0 | infof(data, "Uses proxy env variable %s == '%s'", env_name, proxy); |
359 | |
|
360 | 0 | return proxy; |
361 | 0 | } |
362 | | #endif /* CURL_DISABLE_HTTP */ |
363 | | |
364 | | /* |
365 | | * If this is supposed to use a proxy, we need to figure out the proxy |
366 | | * hostname, so that we can reuse an existing connection |
367 | | * that may exist registered to the same proxy host. |
368 | | */ |
369 | | static CURLcode parse_proxy(struct Curl_easy *data, |
370 | | const char *proxy, |
371 | | bool for_pre_proxy, |
372 | | struct proxy_info *proxyinfo) |
373 | 0 | { |
374 | 0 | char *proxyuser = NULL; |
375 | 0 | char *proxypasswd = NULL; |
376 | 0 | char *scheme = NULL; |
377 | 0 | CURLcode result = CURLE_OK; |
378 | | /* Set the start proxy type for URL scheme guessing */ |
379 | 0 | uint8_t proxytype = for_pre_proxy ? CURLPROXY_SOCKS4 : data->set.proxytype; |
380 | 0 | CURLU *uhp = curl_url(); |
381 | 0 | CURLUcode uc; |
382 | |
|
383 | 0 | if(!uhp) { |
384 | 0 | result = CURLE_OUT_OF_MEMORY; |
385 | 0 | goto error; |
386 | 0 | } |
387 | | /* When parsing the proxy, allowing non-supported schemes since we have |
388 | | these made up ones for proxies. Guess scheme for URLs without it. */ |
389 | 0 | uc = curl_url_set(uhp, CURLUPART_URL, proxy, |
390 | 0 | CURLU_NON_SUPPORT_SCHEME | CURLU_GUESS_SCHEME); |
391 | 0 | if(!uc) { |
392 | | /* parsed okay as a URL - only update proxytype when scheme was explicit */ |
393 | 0 | uc = curl_url_get(uhp, CURLUPART_SCHEME, &scheme, CURLU_NO_GUESS_SCHEME); |
394 | 0 | if(!uc) { |
395 | 0 | result = Curl_scheme_to_proxytype(data, scheme, &proxytype, proxy); |
396 | 0 | if(result) |
397 | 0 | goto error; |
398 | 0 | } |
399 | 0 | else if(uc != CURLUE_NO_SCHEME) { |
400 | 0 | result = CURLE_OUT_OF_MEMORY; |
401 | 0 | goto error; |
402 | 0 | } |
403 | | /* else: no explicit scheme, keep the configured proxytype */ |
404 | 0 | } |
405 | 0 | else { |
406 | 0 | failf(data, "Unsupported proxy syntax in \'%s\': %s", proxy, |
407 | 0 | curl_url_strerror(uc)); |
408 | 0 | result = CURLE_COULDNT_RESOLVE_PROXY; |
409 | 0 | goto error; |
410 | 0 | } |
411 | | |
412 | 0 | result = Curl_peer_from_proxy_url(uhp, data, proxy, proxytype, |
413 | 0 | &proxyinfo->peer, &proxytype); |
414 | 0 | if(result) |
415 | 0 | goto error; |
416 | | |
417 | 0 | switch(proxytype) { |
418 | 0 | case CURLPROXY_HTTP: |
419 | 0 | case CURLPROXY_HTTP_1_0: |
420 | 0 | case CURLPROXY_HTTPS: |
421 | 0 | case CURLPROXY_HTTPS2: |
422 | 0 | case CURLPROXY_HTTPS3: |
423 | 0 | if(for_pre_proxy) { |
424 | 0 | failf(data, "Unsupported pre-proxy type for \'%s\'", proxy); |
425 | 0 | result = CURLE_COULDNT_RESOLVE_PROXY; |
426 | 0 | goto error; |
427 | 0 | } |
428 | 0 | break; |
429 | 0 | case CURLPROXY_SOCKS4: |
430 | 0 | case CURLPROXY_SOCKS4A: |
431 | 0 | case CURLPROXY_SOCKS5: |
432 | 0 | case CURLPROXY_SOCKS5_HOSTNAME: |
433 | 0 | break; |
434 | 0 | default: |
435 | 0 | failf(data, "Unsupported proxy type %u for \'%s\'", proxytype, proxy); |
436 | 0 | result = CURLE_COULDNT_RESOLVE_PROXY; |
437 | 0 | goto error; |
438 | 0 | } |
439 | | |
440 | | /* Is there a username and password given in this proxy URL? */ |
441 | 0 | uc = curl_url_get(uhp, CURLUPART_USER, &proxyuser, CURLU_URLDECODE); |
442 | 0 | if(uc && (uc != CURLUE_NO_USER)) { |
443 | 0 | result = Curl_uc_to_curlcode(uc); |
444 | 0 | goto error; |
445 | 0 | } |
446 | 0 | uc = curl_url_get(uhp, CURLUPART_PASSWORD, &proxypasswd, CURLU_URLDECODE); |
447 | 0 | if(uc && (uc != CURLUE_NO_PASSWORD)) { |
448 | 0 | result = Curl_uc_to_curlcode(uc); |
449 | 0 | goto error; |
450 | 0 | } |
451 | | |
452 | 0 | if(proxyuser || proxypasswd) { |
453 | 0 | result = Curl_creds_create(proxyuser, proxypasswd, NULL, NULL, |
454 | 0 | CURL_EASY_STR(data, STRING_PROXY_SERVICE_NAME), |
455 | 0 | CREDS_URL, &proxyinfo->creds); |
456 | 0 | if(result) |
457 | 0 | goto error; |
458 | 0 | } |
459 | 0 | else if(!for_pre_proxy && |
460 | 0 | (CURL_EASY_STR(data, STRING_PROXYUSERNAME) || |
461 | 0 | CURL_EASY_STR(data, STRING_PROXYPASSWORD) || |
462 | 0 | CURL_EASY_STR(data, STRING_PROXY_SERVICE_NAME))) { |
463 | | /* No user/passwd in URL, if this is not a pre-proxy, the |
464 | | * CURLOPT_PROXY* settings apply. */ |
465 | 0 | result = Curl_creds_create(CURL_EASY_STR(data, STRING_PROXYUSERNAME), |
466 | 0 | CURL_EASY_STR(data, STRING_PROXYPASSWORD), |
467 | 0 | NULL, NULL, |
468 | 0 | CURL_EASY_STR(data, STRING_PROXY_SERVICE_NAME), |
469 | 0 | CREDS_OPTION, &proxyinfo->creds); |
470 | 0 | } |
471 | 0 | else |
472 | 0 | Curl_creds_unlink(&proxyinfo->creds); |
473 | | |
474 | 0 | proxyinfo->proxytype = proxytype; |
475 | |
|
476 | 0 | error: |
477 | 0 | curlx_free(scheme); |
478 | 0 | curlx_free(proxyuser); |
479 | 0 | curlx_free(proxypasswd); |
480 | 0 | curl_url_cleanup(uhp); |
481 | 0 | #ifdef DEBUGBUILD |
482 | 0 | if(!result) { |
483 | 0 | DEBUGASSERT(proxyinfo); |
484 | 0 | DEBUGASSERT(proxyinfo->peer); |
485 | 0 | } |
486 | 0 | #endif |
487 | 0 | return result; |
488 | 0 | } |
489 | | |
490 | | /* Is transfer's origin exempted from proxy use? */ |
491 | | static bool proxy_do_not_proxy(struct Curl_easy *data) |
492 | 0 | { |
493 | 0 | const char *no_proxy; |
494 | 0 | char *env_no_proxy = NULL; |
495 | 0 | bool do_not_proxy; |
496 | | |
497 | | /* no proxying if the transfer does not use the network */ |
498 | 0 | if(data->state.origin->scheme->flags & PROTOPT_NONETWORK) |
499 | 0 | return TRUE; |
500 | | |
501 | 0 | no_proxy = CURL_EASY_STR(data, STRING_NOPROXY); |
502 | 0 | if(!no_proxy) { |
503 | 0 | const char *p = "no_proxy"; |
504 | 0 | env_no_proxy = curl_getenv(p); |
505 | 0 | if(!env_no_proxy) { |
506 | 0 | p = "NO_PROXY"; |
507 | 0 | env_no_proxy = curl_getenv(p); |
508 | 0 | } |
509 | 0 | if(env_no_proxy) |
510 | 0 | infof(data, "Uses proxy env variable %s == '%s'", p, env_no_proxy); |
511 | 0 | no_proxy = env_no_proxy; |
512 | 0 | } |
513 | |
|
514 | 0 | do_not_proxy = proxy_check_noproxy(data->state.origin->hostname, no_proxy); |
515 | 0 | curlx_safefree(env_no_proxy); |
516 | 0 | return do_not_proxy; |
517 | 0 | } |
518 | | |
519 | | CURLcode Curl_proxy_init_conn(struct Curl_easy *data, |
520 | | struct connectdata *conn) |
521 | 0 | { |
522 | 0 | char *proxy = NULL; |
523 | 0 | char *pre_proxy = NULL; |
524 | 0 | const char *str = NULL; |
525 | 0 | bool do_env_detect = TRUE; |
526 | 0 | CURLcode result = CURLE_OK; |
527 | | |
528 | | /* Enforce no proxy use unless we decide to use one */ |
529 | 0 | conn->bits.origin_is_proxy = FALSE; |
530 | 0 | DEBUGASSERT(!conn->socks_proxy.peer); |
531 | 0 | DEBUGASSERT(!conn->http_proxy.peer); |
532 | |
|
533 | 0 | if(proxy_do_not_proxy(data)) |
534 | 0 | goto out; |
535 | | |
536 | | /************************************************************* |
537 | | * Detect what (if any) proxy to use |
538 | | *************************************************************/ |
539 | | /* the empty config strings disable proxy use and env detects */ |
540 | 0 | str = CURL_EASY_STR(data, STRING_PROXY); |
541 | 0 | if(str) { |
542 | 0 | if(*str) { |
543 | 0 | proxy = curlx_strdup(str); |
544 | | /* if global proxy is set, this is it */ |
545 | 0 | if(!proxy) { |
546 | 0 | failf(data, "memory shortage"); |
547 | 0 | result = CURLE_OUT_OF_MEMORY; |
548 | 0 | goto out; |
549 | 0 | } |
550 | 0 | } |
551 | 0 | else |
552 | 0 | do_env_detect = FALSE; |
553 | 0 | } |
554 | | |
555 | 0 | str = CURL_EASY_STR(data, STRING_PRE_PROXY); |
556 | 0 | if(str) { |
557 | 0 | if(*str) { |
558 | 0 | pre_proxy = curlx_strdup(str); |
559 | | /* if global socks proxy is set, this is it */ |
560 | 0 | if(!pre_proxy) { |
561 | 0 | failf(data, "memory shortage"); |
562 | 0 | result = CURLE_OUT_OF_MEMORY; |
563 | 0 | goto out; |
564 | 0 | } |
565 | 0 | } |
566 | 0 | else |
567 | 0 | do_env_detect = FALSE; |
568 | 0 | } |
569 | | |
570 | 0 | #ifndef CURL_DISABLE_HTTP |
571 | | /* None configured, detect possible proxy from environment. */ |
572 | 0 | if(!proxy && !pre_proxy && do_env_detect) |
573 | 0 | proxy = proxy_detect_proxy(data, conn->scheme); |
574 | | #else |
575 | | (void)do_env_detect; |
576 | | #endif /* CURL_DISABLE_HTTP */ |
577 | |
|
578 | 0 | if(!proxy && !pre_proxy) |
579 | 0 | goto out; |
580 | | |
581 | 0 | if(pre_proxy) { |
582 | 0 | result = parse_proxy(data, pre_proxy, TRUE, &conn->socks_proxy); |
583 | 0 | if(result) |
584 | 0 | goto out; |
585 | 0 | } |
586 | | |
587 | 0 | if(proxy) { |
588 | 0 | result = parse_proxy(data, proxy, FALSE, &conn->http_proxy); |
589 | 0 | if(result) |
590 | 0 | goto out; |
591 | | |
592 | 0 | switch(conn->http_proxy.proxytype) { |
593 | 0 | case CURLPROXY_SOCKS4: |
594 | 0 | case CURLPROXY_SOCKS4A: |
595 | 0 | case CURLPROXY_SOCKS5: |
596 | 0 | case CURLPROXY_SOCKS5_HOSTNAME: |
597 | | /* Whoops, it is not an HTTP proxy */ |
598 | 0 | if(pre_proxy) { |
599 | | /* and we already have a SOCKS pre-proxy. Cannot have both */ |
600 | 0 | failf(data, "Having a SOCKS pre-proxy and proxy is not " |
601 | 0 | "supported with \'%s\'", proxy); |
602 | 0 | result = CURLE_COULDNT_RESOLVE_PROXY; |
603 | 0 | goto out; |
604 | 0 | } |
605 | | /* switch */ |
606 | 0 | conn->socks_proxy = conn->http_proxy; |
607 | 0 | memset(&conn->http_proxy, 0, sizeof(conn->http_proxy)); |
608 | 0 | break; |
609 | 0 | default: |
610 | | /* all other types are HTTP */ |
611 | 0 | break; |
612 | 0 | } |
613 | 0 | } |
614 | | |
615 | 0 | if(conn->socks_proxy.peer) { |
616 | 0 | DEBUGASSERT(!CURL_PROXY_IS_ANY_HTTP(conn->socks_proxy.proxytype)); |
617 | 0 | } |
618 | |
|
619 | | #ifdef CURL_DISABLE_HTTP |
620 | | if(conn->http_proxy.peer) { |
621 | | /* asking for an HTTP proxy is a bit funny when HTTP is disabled... */ |
622 | | result = CURLE_UNSUPPORTED_PROTOCOL; |
623 | | goto out; |
624 | | } |
625 | | |
626 | | #else /* CURL_DISABLE_HTTP */ |
627 | 0 | if(conn->http_proxy.peer) { |
628 | 0 | const struct Curl_scheme *scheme = data->state.origin->scheme; |
629 | 0 | bool tunnel_proxy = (bool)data->set.tunnel_thru_httpproxy; |
630 | 0 | DEBUGASSERT(CURL_PROXY_IS_ANY_HTTP(conn->http_proxy.proxytype)); |
631 | |
|
632 | 0 | if(!tunnel_proxy) { |
633 | | /* Decide if we tunnel through proxy automatically */ |
634 | 0 | if(conn->via_peer) { |
635 | | /* With connect-to, we always tunnel */ |
636 | 0 | tunnel_proxy = TRUE; |
637 | 0 | } |
638 | 0 | else if(scheme->flags & PROTOPT_SSL) { |
639 | | /* If the transfer is supposed to be secure, we tunnel */ |
640 | 0 | tunnel_proxy = TRUE; |
641 | 0 | } |
642 | 0 | else if(scheme->flags & PROTOPT_HTTP_PROXY_TUNNEL) { |
643 | | /* transfer scheme required tunneling */ |
644 | 0 | tunnel_proxy = TRUE; |
645 | 0 | } |
646 | 0 | else if(!(scheme->protocol & PROTO_FAMILY_HTTP) && |
647 | 0 | !(scheme->flags & PROTOPT_PROXY_AS_HTTP)) { |
648 | | /* Cannot delegate transfer URL to HTTP proxy */ |
649 | 0 | tunnel_proxy = TRUE; |
650 | 0 | } |
651 | 0 | } |
652 | |
|
653 | 0 | if(!tunnel_proxy) { |
654 | | /* HTTP proxy used in forwarding mode. This means the connection |
655 | | * is really to the proxy and NOT the origin of the transfer. */ |
656 | 0 | DEBUGASSERT(!conn->via_peer); |
657 | 0 | Curl_peer_link(&conn->origin, conn->http_proxy.peer); |
658 | 0 | conn->scheme = conn->http_proxy.peer->scheme; |
659 | 0 | conn->bits.origin_is_proxy = TRUE; |
660 | 0 | } |
661 | |
|
662 | 0 | #ifndef CURL_DISABLE_DIGEST_AUTH |
663 | 0 | if(!Curl_safecmp(data->state.envproxy, proxy)) { |
664 | | /* proxy changed */ |
665 | 0 | Curl_auth_digest_cleanup(&data->state.proxydigest); |
666 | 0 | curlx_free(data->state.envproxy); |
667 | 0 | data->state.envproxy = curlx_strdup(proxy); |
668 | 0 | } |
669 | 0 | #endif |
670 | 0 | } |
671 | 0 | #endif /* !CURL_DISABLE_HTTP */ |
672 | |
|
673 | 0 | out: |
674 | 0 | curlx_free(pre_proxy); |
675 | 0 | curlx_free(proxy); |
676 | 0 | return result; |
677 | 0 | } |
678 | | |
679 | | #endif /* CURL_DISABLE_PROXY */ |