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