/src/PROJ/curl/lib/http_proxy.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 | | #include "http_proxy.h" |
28 | | |
29 | | #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_PROXY) |
30 | | |
31 | | #include <curl/curl.h> |
32 | | #include "sendf.h" |
33 | | #include "http.h" |
34 | | #include "url.h" |
35 | | #include "select.h" |
36 | | #include "progress.h" |
37 | | #include "cfilters.h" |
38 | | #include "cf-h1-proxy.h" |
39 | | #include "cf-h2-proxy.h" |
40 | | #include "connect.h" |
41 | | #include "vtls/vtls.h" |
42 | | #include "transfer.h" |
43 | | #include "multiif.h" |
44 | | #include "vauth/vauth.h" |
45 | | #include "curlx/strparse.h" |
46 | | |
47 | | /* The last 3 #include files should be in this order */ |
48 | | #include "curl_printf.h" |
49 | | #include "curl_memory.h" |
50 | | #include "memdebug.h" |
51 | | |
52 | | static bool hd_name_eq(const char *n1, size_t n1len, |
53 | | const char *n2, size_t n2len) |
54 | 0 | { |
55 | 0 | return (n1len == n2len) ? curl_strnequal(n1, n2, n1len) : FALSE; |
56 | 0 | } |
57 | | |
58 | | static CURLcode dynhds_add_custom(struct Curl_easy *data, |
59 | | bool is_connect, int httpversion, |
60 | | struct dynhds *hds) |
61 | 0 | { |
62 | 0 | struct connectdata *conn = data->conn; |
63 | 0 | const char *ptr; |
64 | 0 | struct curl_slist *h[2]; |
65 | 0 | struct curl_slist *headers; |
66 | 0 | int numlists = 1; /* by default */ |
67 | 0 | int i; |
68 | |
|
69 | 0 | enum Curl_proxy_use proxy; |
70 | |
|
71 | 0 | if(is_connect) |
72 | 0 | proxy = HEADER_CONNECT; |
73 | 0 | else |
74 | 0 | proxy = conn->bits.httpproxy && !conn->bits.tunnel_proxy ? |
75 | 0 | HEADER_PROXY : HEADER_SERVER; |
76 | |
|
77 | 0 | switch(proxy) { |
78 | 0 | case HEADER_SERVER: |
79 | 0 | h[0] = data->set.headers; |
80 | 0 | break; |
81 | 0 | case HEADER_PROXY: |
82 | 0 | h[0] = data->set.headers; |
83 | 0 | if(data->set.sep_headers) { |
84 | 0 | h[1] = data->set.proxyheaders; |
85 | 0 | numlists++; |
86 | 0 | } |
87 | 0 | break; |
88 | 0 | case HEADER_CONNECT: |
89 | 0 | if(data->set.sep_headers) |
90 | 0 | h[0] = data->set.proxyheaders; |
91 | 0 | else |
92 | 0 | h[0] = data->set.headers; |
93 | 0 | break; |
94 | 0 | } |
95 | | |
96 | | /* loop through one or two lists */ |
97 | 0 | for(i = 0; i < numlists; i++) { |
98 | 0 | for(headers = h[i]; headers; headers = headers->next) { |
99 | 0 | const char *name, *value; |
100 | 0 | size_t namelen, valuelen; |
101 | | |
102 | | /* There are 2 quirks in place for custom headers: |
103 | | * 1. setting only 'name:' to suppress a header from being sent |
104 | | * 2. setting only 'name;' to send an empty (illegal) header |
105 | | */ |
106 | 0 | ptr = strchr(headers->data, ':'); |
107 | 0 | if(ptr) { |
108 | 0 | name = headers->data; |
109 | 0 | namelen = ptr - headers->data; |
110 | 0 | ptr++; /* pass the colon */ |
111 | 0 | curlx_str_passblanks(&ptr); |
112 | 0 | if(*ptr) { |
113 | 0 | value = ptr; |
114 | 0 | valuelen = strlen(value); |
115 | 0 | } |
116 | 0 | else { |
117 | | /* quirk #1, suppress this header */ |
118 | 0 | continue; |
119 | 0 | } |
120 | 0 | } |
121 | 0 | else { |
122 | 0 | ptr = strchr(headers->data, ';'); |
123 | |
|
124 | 0 | if(!ptr) { |
125 | | /* neither : nor ; in provided header value. We seem |
126 | | * to ignore this silently */ |
127 | 0 | continue; |
128 | 0 | } |
129 | | |
130 | 0 | name = headers->data; |
131 | 0 | namelen = ptr - headers->data; |
132 | 0 | ptr++; /* pass the semicolon */ |
133 | 0 | curlx_str_passblanks(&ptr); |
134 | 0 | if(!*ptr) { |
135 | | /* quirk #2, send an empty header */ |
136 | 0 | value = ""; |
137 | 0 | valuelen = 0; |
138 | 0 | } |
139 | 0 | else { |
140 | | /* this may be used for something else in the future, |
141 | | * ignore this for now */ |
142 | 0 | continue; |
143 | 0 | } |
144 | 0 | } |
145 | | |
146 | 0 | DEBUGASSERT(name && value); |
147 | 0 | if(data->state.aptr.host && |
148 | | /* a Host: header was sent already, do not pass on any custom Host: |
149 | | header as that will produce *two* in the same request! */ |
150 | 0 | hd_name_eq(name, namelen, STRCONST("Host:"))) |
151 | 0 | ; |
152 | 0 | else if(data->state.httpreq == HTTPREQ_POST_FORM && |
153 | | /* this header (extended by formdata.c) is sent later */ |
154 | 0 | hd_name_eq(name, namelen, STRCONST("Content-Type:"))) |
155 | 0 | ; |
156 | 0 | else if(data->state.httpreq == HTTPREQ_POST_MIME && |
157 | | /* this header is sent later */ |
158 | 0 | hd_name_eq(name, namelen, STRCONST("Content-Type:"))) |
159 | 0 | ; |
160 | 0 | else if(data->req.authneg && |
161 | | /* while doing auth neg, do not allow the custom length since |
162 | | we will force length zero then */ |
163 | 0 | hd_name_eq(name, namelen, STRCONST("Content-Length:"))) |
164 | 0 | ; |
165 | 0 | else if(data->state.aptr.te && |
166 | | /* when asking for Transfer-Encoding, do not pass on a custom |
167 | | Connection: */ |
168 | 0 | hd_name_eq(name, namelen, STRCONST("Connection:"))) |
169 | 0 | ; |
170 | 0 | else if((httpversion >= 20) && |
171 | 0 | hd_name_eq(name, namelen, STRCONST("Transfer-Encoding:"))) |
172 | | /* HTTP/2 and HTTP/3 do not support chunked requests */ |
173 | 0 | ; |
174 | 0 | else if((hd_name_eq(name, namelen, STRCONST("Authorization:")) || |
175 | 0 | hd_name_eq(name, namelen, STRCONST("Cookie:"))) && |
176 | | /* be careful of sending this potentially sensitive header to |
177 | | other hosts */ |
178 | 0 | !Curl_auth_allowed_to_host(data)) |
179 | 0 | ; |
180 | 0 | else { |
181 | 0 | CURLcode result; |
182 | |
|
183 | 0 | result = Curl_dynhds_add(hds, name, namelen, value, valuelen); |
184 | 0 | if(result) |
185 | 0 | return result; |
186 | 0 | } |
187 | 0 | } |
188 | 0 | } |
189 | | |
190 | 0 | return CURLE_OK; |
191 | 0 | } |
192 | | |
193 | | CURLcode Curl_http_proxy_get_destination(struct Curl_cfilter *cf, |
194 | | const char **phostname, |
195 | | int *pport, bool *pipv6_ip) |
196 | 0 | { |
197 | 0 | DEBUGASSERT(cf); |
198 | 0 | DEBUGASSERT(cf->conn); |
199 | |
|
200 | 0 | if(cf->conn->bits.conn_to_host) |
201 | 0 | *phostname = cf->conn->conn_to_host.name; |
202 | 0 | else if(cf->sockindex == SECONDARYSOCKET) |
203 | 0 | *phostname = cf->conn->secondaryhostname; |
204 | 0 | else |
205 | 0 | *phostname = cf->conn->host.name; |
206 | |
|
207 | 0 | if(cf->sockindex == SECONDARYSOCKET) |
208 | 0 | *pport = cf->conn->secondary_port; |
209 | 0 | else if(cf->conn->bits.conn_to_port) |
210 | 0 | *pport = cf->conn->conn_to_port; |
211 | 0 | else |
212 | 0 | *pport = cf->conn->remote_port; |
213 | |
|
214 | 0 | if(*phostname != cf->conn->host.name) |
215 | 0 | *pipv6_ip = (strchr(*phostname, ':') != NULL); |
216 | 0 | else |
217 | 0 | *pipv6_ip = cf->conn->bits.ipv6_ip; |
218 | |
|
219 | 0 | return CURLE_OK; |
220 | 0 | } |
221 | | |
222 | | struct cf_proxy_ctx { |
223 | | /* the protocol specific sub-filter we install during connect */ |
224 | | struct Curl_cfilter *cf_protocol; |
225 | | int httpversion; /* HTTP version used to CONNECT */ |
226 | | }; |
227 | | |
228 | | CURLcode Curl_http_proxy_create_CONNECT(struct httpreq **preq, |
229 | | struct Curl_cfilter *cf, |
230 | | struct Curl_easy *data, |
231 | | int http_version_major) |
232 | 0 | { |
233 | 0 | struct cf_proxy_ctx *ctx = cf->ctx; |
234 | 0 | const char *hostname = NULL; |
235 | 0 | char *authority = NULL; |
236 | 0 | int port; |
237 | 0 | bool ipv6_ip; |
238 | 0 | CURLcode result; |
239 | 0 | struct httpreq *req = NULL; |
240 | |
|
241 | 0 | result = Curl_http_proxy_get_destination(cf, &hostname, &port, &ipv6_ip); |
242 | 0 | if(result) |
243 | 0 | goto out; |
244 | | |
245 | 0 | authority = aprintf("%s%s%s:%d", ipv6_ip ? "[" : "", hostname, |
246 | 0 | ipv6_ip ?"]" : "", port); |
247 | 0 | if(!authority) { |
248 | 0 | result = CURLE_OUT_OF_MEMORY; |
249 | 0 | goto out; |
250 | 0 | } |
251 | | |
252 | 0 | result = Curl_http_req_make(&req, "CONNECT", sizeof("CONNECT")-1, |
253 | 0 | NULL, 0, authority, strlen(authority), |
254 | 0 | NULL, 0); |
255 | 0 | if(result) |
256 | 0 | goto out; |
257 | | |
258 | | /* Setup the proxy-authorization header, if any */ |
259 | 0 | result = Curl_http_output_auth(data, cf->conn, req->method, HTTPREQ_GET, |
260 | 0 | req->authority, TRUE); |
261 | 0 | if(result) |
262 | 0 | goto out; |
263 | | |
264 | | /* If user is not overriding Host: header, we add for HTTP/1.x */ |
265 | 0 | if(http_version_major == 1 && |
266 | 0 | !Curl_checkProxyheaders(data, cf->conn, STRCONST("Host"))) { |
267 | 0 | result = Curl_dynhds_cadd(&req->headers, "Host", authority); |
268 | 0 | if(result) |
269 | 0 | goto out; |
270 | 0 | } |
271 | | |
272 | 0 | if(data->state.aptr.proxyuserpwd) { |
273 | 0 | result = Curl_dynhds_h1_cadd_line(&req->headers, |
274 | 0 | data->state.aptr.proxyuserpwd); |
275 | 0 | if(result) |
276 | 0 | goto out; |
277 | 0 | } |
278 | | |
279 | 0 | if(!Curl_checkProxyheaders(data, cf->conn, STRCONST("User-Agent")) && |
280 | 0 | data->set.str[STRING_USERAGENT] && *data->set.str[STRING_USERAGENT]) { |
281 | 0 | result = Curl_dynhds_cadd(&req->headers, "User-Agent", |
282 | 0 | data->set.str[STRING_USERAGENT]); |
283 | 0 | if(result) |
284 | 0 | goto out; |
285 | 0 | } |
286 | | |
287 | 0 | if(http_version_major == 1 && |
288 | 0 | !Curl_checkProxyheaders(data, cf->conn, STRCONST("Proxy-Connection"))) { |
289 | 0 | result = Curl_dynhds_cadd(&req->headers, "Proxy-Connection", "Keep-Alive"); |
290 | 0 | if(result) |
291 | 0 | goto out; |
292 | 0 | } |
293 | | |
294 | 0 | result = dynhds_add_custom(data, TRUE, ctx->httpversion, &req->headers); |
295 | |
|
296 | 0 | out: |
297 | 0 | if(result && req) { |
298 | 0 | Curl_http_req_free(req); |
299 | 0 | req = NULL; |
300 | 0 | } |
301 | 0 | free(authority); |
302 | 0 | *preq = req; |
303 | 0 | return result; |
304 | 0 | } |
305 | | |
306 | | static CURLcode http_proxy_cf_connect(struct Curl_cfilter *cf, |
307 | | struct Curl_easy *data, |
308 | | bool *done) |
309 | 0 | { |
310 | 0 | struct cf_proxy_ctx *ctx = cf->ctx; |
311 | 0 | CURLcode result; |
312 | |
|
313 | 0 | if(cf->connected) { |
314 | 0 | *done = TRUE; |
315 | 0 | return CURLE_OK; |
316 | 0 | } |
317 | | |
318 | 0 | CURL_TRC_CF(data, cf, "connect"); |
319 | 0 | connect_sub: |
320 | 0 | result = cf->next->cft->do_connect(cf->next, data, done); |
321 | 0 | if(result || !*done) |
322 | 0 | return result; |
323 | | |
324 | 0 | *done = FALSE; |
325 | 0 | if(!ctx->cf_protocol) { |
326 | 0 | struct Curl_cfilter *cf_protocol = NULL; |
327 | 0 | int httpversion = 0; |
328 | 0 | const char *alpn = Curl_conn_cf_get_alpn_negotiated(cf->next, data); |
329 | |
|
330 | 0 | if(alpn) |
331 | 0 | infof(data, "CONNECT: '%s' negotiated", alpn); |
332 | 0 | else |
333 | 0 | infof(data, "CONNECT: no ALPN negotiated"); |
334 | |
|
335 | 0 | if(alpn && !strcmp(alpn, "http/1.0")) { |
336 | 0 | CURL_TRC_CF(data, cf, "installing subfilter for HTTP/1.0"); |
337 | 0 | result = Curl_cf_h1_proxy_insert_after(cf, data); |
338 | 0 | if(result) |
339 | 0 | goto out; |
340 | 0 | cf_protocol = cf->next; |
341 | 0 | httpversion = 10; |
342 | 0 | } |
343 | 0 | else if(!alpn || !strcmp(alpn, "http/1.1")) { |
344 | 0 | CURL_TRC_CF(data, cf, "installing subfilter for HTTP/1.1"); |
345 | 0 | result = Curl_cf_h1_proxy_insert_after(cf, data); |
346 | 0 | if(result) |
347 | 0 | goto out; |
348 | 0 | cf_protocol = cf->next; |
349 | | /* Assume that without an ALPN, we are talking to an ancient one */ |
350 | 0 | httpversion = 11; |
351 | 0 | } |
352 | | #ifdef USE_NGHTTP2 |
353 | | else if(!strcmp(alpn, "h2")) { |
354 | | CURL_TRC_CF(data, cf, "installing subfilter for HTTP/2"); |
355 | | result = Curl_cf_h2_proxy_insert_after(cf, data); |
356 | | if(result) |
357 | | goto out; |
358 | | cf_protocol = cf->next; |
359 | | httpversion = 20; |
360 | | } |
361 | | #endif |
362 | 0 | else { |
363 | 0 | failf(data, "CONNECT: negotiated ALPN '%s' not supported", alpn); |
364 | 0 | result = CURLE_COULDNT_CONNECT; |
365 | 0 | goto out; |
366 | 0 | } |
367 | | |
368 | 0 | ctx->cf_protocol = cf_protocol; |
369 | 0 | ctx->httpversion = httpversion; |
370 | | /* after we installed the filter "below" us, we call connect |
371 | | * on out sub-chain again. |
372 | | */ |
373 | 0 | goto connect_sub; |
374 | 0 | } |
375 | 0 | else { |
376 | | /* subchain connected and we had already installed the protocol filter. |
377 | | * This means the protocol tunnel is established, we are done. |
378 | | */ |
379 | 0 | DEBUGASSERT(ctx->cf_protocol); |
380 | 0 | result = CURLE_OK; |
381 | 0 | } |
382 | | |
383 | 0 | out: |
384 | 0 | if(!result) { |
385 | 0 | cf->connected = TRUE; |
386 | 0 | *done = TRUE; |
387 | 0 | } |
388 | 0 | return result; |
389 | 0 | } |
390 | | |
391 | | CURLcode Curl_cf_http_proxy_query(struct Curl_cfilter *cf, |
392 | | struct Curl_easy *data, |
393 | | int query, int *pres1, void *pres2) |
394 | 0 | { |
395 | 0 | switch(query) { |
396 | 0 | case CF_QUERY_HOST_PORT: |
397 | 0 | *pres1 = (int)cf->conn->http_proxy.port; |
398 | 0 | *((const char **)pres2) = cf->conn->http_proxy.host.name; |
399 | 0 | return CURLE_OK; |
400 | 0 | case CF_QUERY_ALPN_NEGOTIATED: { |
401 | 0 | const char **palpn = pres2; |
402 | 0 | DEBUGASSERT(palpn); |
403 | 0 | *palpn = NULL; |
404 | 0 | return CURLE_OK; |
405 | 0 | } |
406 | 0 | default: |
407 | 0 | break; |
408 | 0 | } |
409 | 0 | return cf->next ? |
410 | 0 | cf->next->cft->query(cf->next, data, query, pres1, pres2) : |
411 | 0 | CURLE_UNKNOWN_OPTION; |
412 | 0 | } |
413 | | |
414 | | static void http_proxy_cf_destroy(struct Curl_cfilter *cf, |
415 | | struct Curl_easy *data) |
416 | 0 | { |
417 | 0 | struct cf_proxy_ctx *ctx = cf->ctx; |
418 | |
|
419 | 0 | (void)data; |
420 | 0 | CURL_TRC_CF(data, cf, "destroy"); |
421 | 0 | free(ctx); |
422 | 0 | } |
423 | | |
424 | | static void http_proxy_cf_close(struct Curl_cfilter *cf, |
425 | | struct Curl_easy *data) |
426 | 0 | { |
427 | 0 | struct cf_proxy_ctx *ctx = cf->ctx; |
428 | |
|
429 | 0 | CURL_TRC_CF(data, cf, "close"); |
430 | 0 | cf->connected = FALSE; |
431 | 0 | if(ctx->cf_protocol) { |
432 | 0 | struct Curl_cfilter *f; |
433 | | /* if someone already removed it, we assume he also |
434 | | * took care of destroying it. */ |
435 | 0 | for(f = cf->next; f; f = f->next) { |
436 | 0 | if(f == ctx->cf_protocol) { |
437 | | /* still in our sub-chain */ |
438 | 0 | Curl_conn_cf_discard_sub(cf, ctx->cf_protocol, data, FALSE); |
439 | 0 | break; |
440 | 0 | } |
441 | 0 | } |
442 | 0 | ctx->cf_protocol = NULL; |
443 | 0 | } |
444 | 0 | if(cf->next) |
445 | 0 | cf->next->cft->do_close(cf->next, data); |
446 | 0 | } |
447 | | |
448 | | |
449 | | struct Curl_cftype Curl_cft_http_proxy = { |
450 | | "HTTP-PROXY", |
451 | | CF_TYPE_IP_CONNECT|CF_TYPE_PROXY, |
452 | | 0, |
453 | | http_proxy_cf_destroy, |
454 | | http_proxy_cf_connect, |
455 | | http_proxy_cf_close, |
456 | | Curl_cf_def_shutdown, |
457 | | Curl_cf_def_adjust_pollset, |
458 | | Curl_cf_def_data_pending, |
459 | | Curl_cf_def_send, |
460 | | Curl_cf_def_recv, |
461 | | Curl_cf_def_cntrl, |
462 | | Curl_cf_def_conn_is_alive, |
463 | | Curl_cf_def_conn_keep_alive, |
464 | | Curl_cf_http_proxy_query, |
465 | | }; |
466 | | |
467 | | CURLcode Curl_cf_http_proxy_insert_after(struct Curl_cfilter *cf_at, |
468 | | struct Curl_easy *data) |
469 | 0 | { |
470 | 0 | struct Curl_cfilter *cf; |
471 | 0 | struct cf_proxy_ctx *ctx = NULL; |
472 | 0 | CURLcode result; |
473 | |
|
474 | 0 | (void)data; |
475 | 0 | ctx = calloc(1, sizeof(*ctx)); |
476 | 0 | if(!ctx) { |
477 | 0 | result = CURLE_OUT_OF_MEMORY; |
478 | 0 | goto out; |
479 | 0 | } |
480 | 0 | result = Curl_cf_create(&cf, &Curl_cft_http_proxy, ctx); |
481 | 0 | if(result) |
482 | 0 | goto out; |
483 | 0 | ctx = NULL; |
484 | 0 | Curl_conn_cf_insert_after(cf_at, cf); |
485 | |
|
486 | 0 | out: |
487 | 0 | free(ctx); |
488 | 0 | return result; |
489 | 0 | } |
490 | | |
491 | | #endif /* ! CURL_DISABLE_HTTP && !CURL_DISABLE_PROXY */ |