http_proxy.c 13 KB

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