/src/PROJ/curl/lib/socks.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 | | #ifndef CURL_DISABLE_PROXY |
28 | | |
29 | | #ifdef HAVE_NETINET_IN_H |
30 | | #include <netinet/in.h> |
31 | | #endif |
32 | | #ifdef HAVE_ARPA_INET_H |
33 | | #include <arpa/inet.h> |
34 | | #endif |
35 | | |
36 | | #include "urldata.h" |
37 | | #include "bufq.h" |
38 | | #include "sendf.h" |
39 | | #include "select.h" |
40 | | #include "cfilters.h" |
41 | | #include "connect.h" |
42 | | #include "curlx/timeval.h" |
43 | | #include "socks.h" |
44 | | #include "multiif.h" /* for getsock macros */ |
45 | | #include "curlx/inet_pton.h" |
46 | | #include "url.h" |
47 | | |
48 | | /* The last 2 #include files should be in this order */ |
49 | | #include "curl_memory.h" |
50 | | #include "memdebug.h" |
51 | | |
52 | | #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS) |
53 | | #define DEBUG_AND_VERBOSE |
54 | | #endif |
55 | | |
56 | | /* for the (SOCKS) connect state machine */ |
57 | | enum socks_state_t { |
58 | | SOCKS_ST_INIT, |
59 | | /* SOCKS Version 4 states */ |
60 | | SOCKS4_ST_START, |
61 | | SOCKS4_ST_RESOLVING, |
62 | | SOCKS4_ST_SEND, |
63 | | SOCKS4_ST_RECV, |
64 | | /* SOCKS Version 5 states */ |
65 | | SOCKS5_ST_START, |
66 | | SOCKS5_ST_REQ0_SEND, |
67 | | SOCKS5_ST_RESP0_RECV, /* set up read */ |
68 | | SOCKS5_ST_GSSAPI_INIT, |
69 | | SOCKS5_ST_AUTH_INIT, /* setup outgoing auth buffer */ |
70 | | SOCKS5_ST_AUTH_SEND, /* send auth */ |
71 | | SOCKS5_ST_AUTH_RECV, /* read auth response */ |
72 | | SOCKS5_ST_REQ1_INIT, /* init SOCKS "request" */ |
73 | | SOCKS5_ST_RESOLVING, |
74 | | SOCKS5_ST_REQ1_SEND, |
75 | | SOCKS5_ST_RESP1_RECV, |
76 | | /* Terminal states, all SOCKS versions */ |
77 | | SOCKS_ST_SUCCESS, |
78 | | SOCKS_ST_FAILED |
79 | | }; |
80 | | |
81 | | #ifdef DEBUG_AND_VERBOSE |
82 | | static const char * const cf_socks_statename[] = { |
83 | | "SOCKS_INIT", |
84 | | "SOCKS4_START", |
85 | | "SOCKS4_RESOLVING", |
86 | | "SOCKS4_SEND", |
87 | | "SOCKS4_RECV", |
88 | | "SOCKS5_START", |
89 | | "SOCKS5_REQ0_SEND", |
90 | | "SOCKS5_RESP0_RECV", |
91 | | "SOCKS5_GSSAPI_INIT", |
92 | | "SOCKS5_AUTH_INIT", |
93 | | "SOCKS5_AUTH_SEND", |
94 | | "SOCKS5_AUTH_RECV", |
95 | | "SOCKS5_REQ1_INIT", |
96 | | "SOCKS5_RESOLVING", |
97 | | "SOCKS5_REQ1_SEND", |
98 | | "SOCKS5_RESP1_RECV", |
99 | | "SOCKS_SUCCESS", |
100 | | "SOCKS_FAILED" |
101 | | }; |
102 | | #endif |
103 | | |
104 | 0 | #define SOCKS_CHUNK_SIZE 1024 |
105 | 0 | #define SOCKS_CHUNKS 1 |
106 | | |
107 | | |
108 | | struct socks_state { |
109 | | enum socks_state_t state; |
110 | | struct bufq iobuf; |
111 | | const char *hostname; |
112 | | int remote_port; |
113 | | const char *proxy_user; |
114 | | const char *proxy_password; |
115 | | CURLproxycode presult; |
116 | | unsigned char version; |
117 | | BIT(resolve_local); |
118 | | BIT(start_resolving); |
119 | | BIT(socks4a); |
120 | | }; |
121 | | |
122 | | #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI) |
123 | | /* |
124 | | * Helper read-from-socket functions. Does the same as Curl_read() but it |
125 | | * blocks until all bytes amount of buffersize will be read. No more, no less. |
126 | | * |
127 | | * This is STUPID BLOCKING behavior. Only used by the SOCKS GSSAPI functions. |
128 | | */ |
129 | | CURLcode Curl_blockread_all(struct Curl_cfilter *cf, |
130 | | struct Curl_easy *data, |
131 | | char *buf, /* store read data here */ |
132 | | size_t blen, /* space in buf */ |
133 | | size_t *pnread) /* amount bytes read */ |
134 | | { |
135 | | size_t nread = 0; |
136 | | CURLcode result; |
137 | | |
138 | | *pnread = 0; |
139 | | for(;;) { |
140 | | timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE); |
141 | | if(timeout_ms < 0) { |
142 | | /* we already got the timeout */ |
143 | | return CURLE_OPERATION_TIMEDOUT; |
144 | | } |
145 | | if(!timeout_ms) |
146 | | timeout_ms = TIMEDIFF_T_MAX; |
147 | | if(SOCKET_READABLE(cf->conn->sock[cf->sockindex], timeout_ms) <= 0) |
148 | | return CURLE_OPERATION_TIMEDOUT; |
149 | | result = Curl_conn_cf_recv(cf->next, data, buf, blen, &nread); |
150 | | if(CURLE_AGAIN == result) |
151 | | continue; |
152 | | else if(result) |
153 | | return result; |
154 | | |
155 | | if(blen == nread) { |
156 | | *pnread += nread; |
157 | | return CURLE_OK; |
158 | | } |
159 | | if(!nread) /* EOF */ |
160 | | return CURLE_RECV_ERROR; |
161 | | |
162 | | buf += nread; |
163 | | blen -= nread; |
164 | | *pnread += nread; |
165 | | } |
166 | | } |
167 | | #endif |
168 | | |
169 | | #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS) |
170 | | #define DEBUG_AND_VERBOSE |
171 | | #define sxstate(x,c,d,y) socksstate(x,c,d,y, __LINE__) |
172 | | #else |
173 | 0 | #define sxstate(x,c,d,y) socksstate(x,c,d,y) |
174 | | #endif |
175 | | |
176 | | /* always use this function to change state, to make debugging easier */ |
177 | | static void socksstate(struct socks_state *sx, |
178 | | struct Curl_cfilter *cf, |
179 | | struct Curl_easy *data, |
180 | | enum socks_state_t state |
181 | | #ifdef DEBUG_AND_VERBOSE |
182 | | , int lineno |
183 | | #endif |
184 | | ) |
185 | 0 | { |
186 | 0 | enum socks_state_t oldstate = sx->state; |
187 | 0 | (void)cf; |
188 | 0 | (void)data; |
189 | 0 | if(oldstate == state) |
190 | | /* do not bother when the new state is the same as the old state */ |
191 | 0 | return; |
192 | | |
193 | 0 | sx->state = state; |
194 | |
|
195 | | #ifdef DEBUG_AND_VERBOSE |
196 | | CURL_TRC_CF(data, cf, "[%s] -> [%s] (line %d)", |
197 | | cf_socks_statename[oldstate], |
198 | | cf_socks_statename[sx->state], lineno); |
199 | | #endif |
200 | 0 | } |
201 | | |
202 | | static CURLproxycode socks_failed(struct socks_state *sx, |
203 | | struct Curl_cfilter *cf, |
204 | | struct Curl_easy *data, |
205 | | CURLproxycode presult) |
206 | 0 | { |
207 | 0 | sxstate(sx, cf, data, SOCKS_ST_FAILED); |
208 | 0 | sx->presult = presult; |
209 | 0 | return presult; |
210 | 0 | } |
211 | | |
212 | | static CURLproxycode socks_flush(struct socks_state *sx, |
213 | | struct Curl_cfilter *cf, |
214 | | struct Curl_easy *data, |
215 | | bool *done) |
216 | 0 | { |
217 | 0 | CURLcode result; |
218 | 0 | size_t nwritten; |
219 | |
|
220 | 0 | *done = FALSE; |
221 | 0 | while(!Curl_bufq_is_empty(&sx->iobuf)) { |
222 | 0 | result = Curl_cf_send_bufq(cf->next, data, &sx->iobuf, NULL, 0, |
223 | 0 | &nwritten); |
224 | 0 | if(result == CURLE_AGAIN) |
225 | 0 | return CURLPX_OK; |
226 | 0 | else if(result) { |
227 | 0 | failf(data, "Failed to send SOCKS request: %s", |
228 | 0 | curl_easy_strerror(result)); |
229 | 0 | return socks_failed(sx, cf, data, CURLPX_SEND_CONNECT); |
230 | 0 | } |
231 | 0 | } |
232 | 0 | *done = TRUE; |
233 | 0 | return CURLPX_OK; |
234 | 0 | } |
235 | | |
236 | | static CURLproxycode socks_recv(struct socks_state *sx, |
237 | | struct Curl_cfilter *cf, |
238 | | struct Curl_easy *data, |
239 | | size_t min_bytes, |
240 | | bool *done) |
241 | 0 | { |
242 | 0 | CURLcode result; |
243 | 0 | size_t nread; |
244 | |
|
245 | 0 | *done = FALSE; |
246 | 0 | while(Curl_bufq_len(&sx->iobuf) < min_bytes) { |
247 | 0 | result = Curl_cf_recv_bufq(cf->next, data, &sx->iobuf, |
248 | 0 | min_bytes - Curl_bufq_len(&sx->iobuf), |
249 | 0 | &nread); |
250 | 0 | if(result == CURLE_AGAIN) |
251 | 0 | return CURLPX_OK; |
252 | 0 | else if(result) { |
253 | 0 | failf(data, "Failed to receive SOCKS response: %s", |
254 | 0 | curl_easy_strerror(result)); |
255 | 0 | return CURLPX_RECV_CONNECT; |
256 | 0 | } |
257 | 0 | else if(!nread) { /* EOF */ |
258 | 0 | if(Curl_bufq_len(&sx->iobuf) < min_bytes) { |
259 | 0 | failf(data, "Failed to receive SOCKS response, " |
260 | 0 | "proxy closed connection"); |
261 | 0 | return CURLPX_RECV_CONNECT; |
262 | 0 | } |
263 | 0 | break; |
264 | 0 | } |
265 | 0 | } |
266 | 0 | *done = TRUE; |
267 | 0 | return CURLPX_OK; |
268 | 0 | } |
269 | | |
270 | | static CURLproxycode socks4_req_add_hd(struct socks_state *sx, |
271 | | struct Curl_easy *data) |
272 | 0 | { |
273 | 0 | unsigned char buf[4]; |
274 | 0 | size_t nwritten; |
275 | 0 | CURLcode result; |
276 | |
|
277 | 0 | (void)data; |
278 | 0 | buf[0] = 4; /* version (SOCKS4) */ |
279 | 0 | buf[1] = 1; /* connect */ |
280 | 0 | buf[2] = (unsigned char)((sx->remote_port >> 8) & 0xffu); /* MSB */ |
281 | 0 | buf[3] = (unsigned char)(sx->remote_port & 0xffu); /* LSB */ |
282 | |
|
283 | 0 | result = Curl_bufq_write(&sx->iobuf, buf, 4, &nwritten); |
284 | 0 | if(result || (nwritten != 4)) |
285 | 0 | return CURLPX_SEND_REQUEST; |
286 | 0 | return CURLPX_OK; |
287 | 0 | } |
288 | | |
289 | | static CURLproxycode socks4_req_add_user(struct socks_state *sx, |
290 | | struct Curl_easy *data) |
291 | 0 | { |
292 | 0 | CURLcode result; |
293 | 0 | size_t nwritten; |
294 | |
|
295 | 0 | if(sx->proxy_user) { |
296 | 0 | size_t plen = strlen(sx->proxy_user); |
297 | 0 | if(plen > 255) { |
298 | | /* there is no real size limit to this field in the protocol, but |
299 | | SOCKS5 limits the proxy user field to 255 bytes and it seems likely |
300 | | that a longer field is either a mistake or malicious input */ |
301 | 0 | failf(data, "Too long SOCKS proxy username"); |
302 | 0 | return CURLPX_LONG_USER; |
303 | 0 | } |
304 | | /* add proxy name WITH trailing zero */ |
305 | 0 | result = Curl_bufq_cwrite(&sx->iobuf, sx->proxy_user, plen + 1, |
306 | 0 | &nwritten); |
307 | 0 | if(result || (nwritten != (plen + 1))) |
308 | 0 | return CURLPX_SEND_REQUEST; |
309 | 0 | } |
310 | 0 | else { |
311 | | /* empty user name */ |
312 | 0 | unsigned char b = 0; |
313 | 0 | result = Curl_bufq_write(&sx->iobuf, &b, 1, &nwritten); |
314 | 0 | if(result || (nwritten != 1)) |
315 | 0 | return CURLPX_SEND_REQUEST; |
316 | 0 | } |
317 | 0 | return CURLPX_OK; |
318 | 0 | } |
319 | | |
320 | | static CURLproxycode socks4_resolving(struct socks_state *sx, |
321 | | struct Curl_cfilter *cf, |
322 | | struct Curl_easy *data, |
323 | | bool *done) |
324 | 0 | { |
325 | 0 | struct Curl_dns_entry *dns = NULL; |
326 | 0 | struct Curl_addrinfo *hp = NULL; |
327 | 0 | CURLcode result; |
328 | 0 | size_t nwritten; |
329 | |
|
330 | 0 | *done = FALSE; |
331 | 0 | if(sx->start_resolving) { |
332 | | /* need to resolve hostname to add destination address */ |
333 | 0 | sx->start_resolving = FALSE; |
334 | |
|
335 | 0 | result = Curl_resolv(data, sx->hostname, sx->remote_port, |
336 | 0 | cf->conn->ip_version, TRUE, &dns); |
337 | 0 | if(result == CURLE_AGAIN) { |
338 | 0 | CURL_TRC_CF(data, cf, "SOCKS4 non-blocking resolve of %s", |
339 | 0 | sx->hostname); |
340 | 0 | return CURLPX_OK; |
341 | 0 | } |
342 | 0 | else if(result) |
343 | 0 | return CURLPX_RESOLVE_HOST; |
344 | 0 | } |
345 | 0 | else { |
346 | | /* check if we have the name resolved by now */ |
347 | 0 | result = Curl_resolv_check(data, &dns); |
348 | 0 | if(!result && !dns) |
349 | 0 | return CURLPX_OK; |
350 | 0 | } |
351 | | |
352 | 0 | if(result || !dns) { |
353 | 0 | failf(data, "Failed to resolve \"%s\" for SOCKS4 connect.", |
354 | 0 | sx->hostname); |
355 | 0 | return CURLPX_RESOLVE_HOST; |
356 | 0 | } |
357 | | |
358 | | /* |
359 | | * We cannot use 'hostent' as a struct that Curl_resolv() returns. It |
360 | | * returns a Curl_addrinfo pointer that may not always look the same. |
361 | | */ |
362 | | /* scan for the first IPv4 address */ |
363 | 0 | hp = dns->addr; |
364 | 0 | while(hp && (hp->ai_family != AF_INET)) |
365 | 0 | hp = hp->ai_next; |
366 | |
|
367 | 0 | if(hp) { |
368 | 0 | struct sockaddr_in *saddr_in; |
369 | 0 | char ipbuf[64]; |
370 | |
|
371 | 0 | Curl_printable_address(hp, ipbuf, sizeof(ipbuf)); |
372 | 0 | CURL_TRC_CF(data, cf, "SOCKS4 connect to IPv4 %s (locally resolved)", |
373 | 0 | ipbuf); |
374 | |
|
375 | 0 | saddr_in = (struct sockaddr_in *)(void *)hp->ai_addr; |
376 | 0 | result = Curl_bufq_write(&sx->iobuf, |
377 | 0 | (unsigned char *)&saddr_in->sin_addr.s_addr, 4, |
378 | 0 | &nwritten); |
379 | |
|
380 | 0 | Curl_resolv_unlink(data, &dns); /* not used anymore from now on */ |
381 | 0 | if(result || (nwritten != 4)) |
382 | 0 | return CURLPX_SEND_REQUEST; |
383 | 0 | } |
384 | 0 | else { |
385 | 0 | failf(data, "SOCKS4 connection to %s not supported", sx->hostname); |
386 | 0 | return CURLPX_RESOLVE_HOST; |
387 | 0 | } |
388 | | |
389 | 0 | *done = TRUE; |
390 | 0 | return CURLPX_OK; |
391 | 0 | } |
392 | | |
393 | | static CURLproxycode socks4_check_resp(struct socks_state *sx, |
394 | | struct Curl_cfilter *cf, |
395 | | struct Curl_easy *data) |
396 | 0 | { |
397 | 0 | const unsigned char *resp; |
398 | 0 | size_t rlen; |
399 | |
|
400 | 0 | if(!Curl_bufq_peek(&sx->iobuf, &resp, &rlen) || rlen < 8) { |
401 | 0 | failf(data, "SOCKS4 reply is incomplete."); |
402 | 0 | return CURLPX_RECV_CONNECT; |
403 | 0 | } |
404 | | |
405 | 0 | DEBUGASSERT(rlen == 8); |
406 | | /* |
407 | | * Response format |
408 | | * |
409 | | * +----+----+----+----+----+----+----+----+ |
410 | | * | VN | CD | DSTPORT | DSTIP | |
411 | | * +----+----+----+----+----+----+----+----+ |
412 | | * # of bytes: 1 1 2 4 |
413 | | * |
414 | | * VN is the version of the reply code and should be 0. CD is the result |
415 | | * code with one of the following values: |
416 | | * |
417 | | * 90: request granted |
418 | | * 91: request rejected or failed |
419 | | * 92: request rejected because SOCKS server cannot connect to |
420 | | * identd on the client |
421 | | * 93: request rejected because the client program and identd |
422 | | * report different user-ids |
423 | | */ |
424 | | |
425 | | /* wrong version ? */ |
426 | 0 | if(resp[0]) { |
427 | 0 | failf(data, "SOCKS4 reply has wrong version, version should be 0."); |
428 | 0 | return CURLPX_BAD_VERSION; |
429 | 0 | } |
430 | | |
431 | | /* Result */ |
432 | 0 | switch(resp[1]) { |
433 | 0 | case 90: |
434 | 0 | CURL_TRC_CF(data, cf, "SOCKS4%s request granted.", sx->socks4a ? "a" : ""); |
435 | 0 | Curl_bufq_reset(&sx->iobuf); |
436 | 0 | return CURLPX_OK; |
437 | 0 | case 91: |
438 | 0 | failf(data, |
439 | 0 | "[SOCKS] cannot complete SOCKS4 connection to %u.%u.%u.%u:%u. (%u)" |
440 | 0 | ", request rejected or failed.", |
441 | 0 | resp[4], resp[5], resp[6], resp[7], |
442 | 0 | ((resp[2] << 8) | resp[3]), resp[1]); |
443 | 0 | return CURLPX_REQUEST_FAILED; |
444 | 0 | case 92: |
445 | 0 | failf(data, |
446 | 0 | "[SOCKS] cannot complete SOCKS4 connection to %u.%u.%u.%u:%u. (%u)" |
447 | 0 | ", request rejected because SOCKS server cannot connect to " |
448 | 0 | "identd on the client.", |
449 | 0 | resp[4], resp[5], resp[6], resp[7], |
450 | 0 | ((resp[2] << 8) | resp[3]), resp[1]); |
451 | 0 | return CURLPX_IDENTD; |
452 | 0 | case 93: |
453 | 0 | failf(data, |
454 | 0 | "[SOCKS] cannot complete SOCKS4 connection to %u.%u.%u.%u:%u. (%u)" |
455 | 0 | ", request rejected because the client program and identd " |
456 | 0 | "report different user-ids.", |
457 | 0 | resp[4], resp[5], resp[6], resp[7], |
458 | 0 | ((resp[2] << 8) | resp[3]), resp[1]); |
459 | 0 | return CURLPX_IDENTD_DIFFER; |
460 | 0 | default: |
461 | 0 | failf(data, |
462 | 0 | "[SOCKS] cannot complete SOCKS4 connection to %u.%u.%u.%u:%u. (%u)" |
463 | 0 | ", Unknown.", |
464 | 0 | resp[4], resp[5], resp[6], resp[7], |
465 | 0 | ((resp[2] << 8) | resp[3]), resp[1]); |
466 | 0 | return CURLPX_UNKNOWN_FAIL; |
467 | 0 | } |
468 | 0 | } |
469 | | |
470 | | /* |
471 | | * This function logs in to a SOCKS4 proxy and sends the specifics to the final |
472 | | * destination server. |
473 | | * |
474 | | * Reference : |
475 | | * https://www.openssh.com/txt/socks4.protocol |
476 | | * |
477 | | * Note : |
478 | | * Set protocol4a=true for "SOCKS 4A (Simple Extension to SOCKS 4 Protocol)" |
479 | | * Nonsupport "Identification Protocol (RFC1413)" |
480 | | */ |
481 | | static CURLproxycode socks4_connect(struct Curl_cfilter *cf, |
482 | | struct socks_state *sx, |
483 | | struct Curl_easy *data) |
484 | 0 | { |
485 | 0 | size_t nwritten; |
486 | 0 | CURLproxycode presult; |
487 | 0 | CURLcode result; |
488 | 0 | bool done; |
489 | |
|
490 | 0 | process_state: |
491 | 0 | switch(sx->state) { |
492 | 0 | case SOCKS_ST_INIT: |
493 | 0 | sx->version = 4; |
494 | 0 | sxstate(sx, cf, data, SOCKS4_ST_START); |
495 | 0 | FALLTHROUGH(); |
496 | |
|
497 | 0 | case SOCKS4_ST_START: |
498 | 0 | Curl_bufq_reset(&sx->iobuf); |
499 | 0 | sx->start_resolving = FALSE; |
500 | 0 | sx->socks4a = (cf->conn->socks_proxy.proxytype == CURLPROXY_SOCKS4A); |
501 | 0 | sx->resolve_local = !sx->socks4a; |
502 | 0 | sx->presult = CURLPX_OK; |
503 | | |
504 | | /* SOCKS4 can only do IPv4, insist! */ |
505 | 0 | cf->conn->ip_version = CURL_IPRESOLVE_V4; |
506 | 0 | CURL_TRC_CF(data, cf, "SOCKS4%s communication to%s %s:%d", |
507 | 0 | sx->socks4a ? "a" : "", |
508 | 0 | cf->conn->bits.httpproxy ? " HTTP proxy" : "", |
509 | 0 | sx->hostname, sx->remote_port); |
510 | | |
511 | | /* |
512 | | * Compose socks4 request |
513 | | * |
514 | | * Request format |
515 | | * |
516 | | * +----+----+----+----+----+----+----+----+----+----+....+----+ |
517 | | * | VN | CD | DSTPORT | DSTIP | USERID |NULL| |
518 | | * +----+----+----+----+----+----+----+----+----+----+....+----+ |
519 | | * # of bytes: 1 1 2 4 variable 1 |
520 | | */ |
521 | 0 | presult = socks4_req_add_hd(sx, data); |
522 | 0 | if(presult) |
523 | 0 | return socks_failed(sx, cf, data, presult); |
524 | | |
525 | | /* DNS resolve only for SOCKS4, not SOCKS4a */ |
526 | 0 | if(!sx->resolve_local) { |
527 | | /* socks4a, not resolving locally, sends the hostname. |
528 | | * add an invalid address + user + hostname */ |
529 | 0 | unsigned char buf[4] = { 0, 0, 0, 1 }; |
530 | 0 | size_t hlen = strlen(sx->hostname) + 1; /* including NUL */ |
531 | |
|
532 | 0 | if(hlen > 255) { |
533 | 0 | failf(data, "SOCKS4: too long hostname"); |
534 | 0 | return socks_failed(sx, cf, data, CURLPX_LONG_HOSTNAME); |
535 | 0 | } |
536 | 0 | result = Curl_bufq_write(&sx->iobuf, buf, 4, &nwritten); |
537 | 0 | if(result || (nwritten != 4)) |
538 | 0 | return socks_failed(sx, cf, data, CURLPX_SEND_REQUEST); |
539 | 0 | presult = socks4_req_add_user(sx, data); |
540 | 0 | if(presult) |
541 | 0 | return socks_failed(sx, cf, data, presult); |
542 | 0 | result = Curl_bufq_cwrite(&sx->iobuf, sx->hostname, hlen, &nwritten); |
543 | 0 | if(result || (nwritten != hlen)) |
544 | 0 | return socks_failed(sx, cf, data, CURLPX_SEND_REQUEST); |
545 | | /* request complete */ |
546 | 0 | sxstate(sx, cf, data, SOCKS4_ST_SEND); |
547 | 0 | goto process_state; |
548 | 0 | } |
549 | 0 | sx->start_resolving = TRUE; |
550 | 0 | sxstate(sx, cf, data, SOCKS4_ST_RESOLVING); |
551 | 0 | FALLTHROUGH(); |
552 | |
|
553 | 0 | case SOCKS4_ST_RESOLVING: |
554 | 0 | presult = socks4_resolving(sx, cf, data, &done); |
555 | 0 | if(presult) |
556 | 0 | return socks_failed(sx, cf, data, presult); |
557 | 0 | if(!done) |
558 | 0 | return CURLPX_OK; |
559 | | /* append user */ |
560 | 0 | presult = socks4_req_add_user(sx, data); |
561 | 0 | if(presult) |
562 | 0 | return socks_failed(sx, cf, data, presult); |
563 | 0 | sxstate(sx, cf, data, SOCKS4_ST_SEND); |
564 | 0 | FALLTHROUGH(); |
565 | |
|
566 | 0 | case SOCKS4_ST_SEND: |
567 | 0 | presult = socks_flush(sx, cf, data, &done); |
568 | 0 | if(presult) |
569 | 0 | return socks_failed(sx, cf, data, presult); |
570 | 0 | else if(!done) |
571 | 0 | return CURLPX_OK; |
572 | 0 | sxstate(sx, cf, data, SOCKS4_ST_RECV); |
573 | 0 | FALLTHROUGH(); |
574 | |
|
575 | 0 | case SOCKS4_ST_RECV: |
576 | | /* Receive 8 byte response */ |
577 | 0 | presult = socks_recv(sx, cf, data, 8, &done); |
578 | 0 | if(presult) |
579 | 0 | return socks_failed(sx, cf, data, presult); |
580 | 0 | else if(!done) |
581 | 0 | return CURLPX_OK; |
582 | 0 | presult = socks4_check_resp(sx, cf, data); |
583 | 0 | if(presult) |
584 | 0 | return socks_failed(sx, cf, data, presult); |
585 | 0 | sxstate(sx, cf, data, SOCKS_ST_SUCCESS); |
586 | 0 | FALLTHROUGH(); |
587 | |
|
588 | 0 | case SOCKS_ST_SUCCESS: |
589 | 0 | return CURLPX_OK; |
590 | | |
591 | 0 | case SOCKS_ST_FAILED: |
592 | 0 | DEBUGASSERT(sx->presult); |
593 | 0 | return sx->presult; |
594 | | |
595 | 0 | default: |
596 | 0 | DEBUGASSERT(0); |
597 | 0 | return socks_failed(sx, cf, data, CURLPX_SEND_REQUEST); |
598 | 0 | } |
599 | 0 | } |
600 | | |
601 | | static CURLproxycode socks5_req0_init(struct Curl_cfilter *cf, |
602 | | struct socks_state *sx, |
603 | | struct Curl_easy *data) |
604 | 0 | { |
605 | 0 | const unsigned char auth = data->set.socks5auth; |
606 | 0 | unsigned char req[5]; /* version + len + 3 possible auth methods */ |
607 | 0 | unsigned char nauths; |
608 | 0 | size_t req_len, nwritten; |
609 | 0 | CURLcode result; |
610 | |
|
611 | 0 | (void)cf; |
612 | | /* RFC1928 chapter 5 specifies max 255 chars for domain name in packet */ |
613 | 0 | if(!sx->resolve_local && strlen(sx->hostname) > 255) { |
614 | 0 | failf(data, "SOCKS5: the destination hostname is too long to be " |
615 | 0 | "resolved remotely by the proxy."); |
616 | 0 | return CURLPX_LONG_HOSTNAME; |
617 | 0 | } |
618 | | |
619 | 0 | if(auth & ~(CURLAUTH_BASIC | CURLAUTH_GSSAPI)) |
620 | 0 | infof(data, "warning: unsupported value passed to " |
621 | 0 | "CURLOPT_SOCKS5_AUTH: %u", auth); |
622 | 0 | if(!(auth & CURLAUTH_BASIC)) |
623 | | /* disable username/password auth */ |
624 | 0 | sx->proxy_user = NULL; |
625 | |
|
626 | 0 | req[0] = 5; /* version */ |
627 | 0 | nauths = 1; |
628 | 0 | req[1 + nauths] = 0; /* 1. no authentication */ |
629 | | #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI) |
630 | | if(auth & CURLAUTH_GSSAPI) { |
631 | | ++nauths; |
632 | | req[1 + nauths] = 1; /* GSS-API */ |
633 | | } |
634 | | #endif |
635 | 0 | if(sx->proxy_user) { |
636 | 0 | ++nauths; |
637 | 0 | req[1 + nauths] = 2; /* username/password */ |
638 | 0 | } |
639 | 0 | req[1] = nauths; |
640 | 0 | req_len = 2 + nauths; |
641 | |
|
642 | 0 | result = Curl_bufq_write(&sx->iobuf, req, req_len, &nwritten); |
643 | 0 | if(result || (nwritten != req_len)) |
644 | 0 | return CURLPX_SEND_REQUEST; |
645 | 0 | return CURLPX_OK; |
646 | 0 | } |
647 | | |
648 | | static CURLproxycode socks5_check_resp0(struct socks_state *sx, |
649 | | struct Curl_cfilter *cf, |
650 | | struct Curl_easy *data) |
651 | 0 | { |
652 | 0 | const unsigned char *resp; |
653 | 0 | unsigned char auth_mode; |
654 | 0 | size_t rlen; |
655 | |
|
656 | 0 | if(!Curl_bufq_peek(&sx->iobuf, &resp, &rlen) || rlen < 2) { |
657 | 0 | failf(data, "SOCKS5 initial reply is incomplete."); |
658 | 0 | return CURLPX_RECV_CONNECT; |
659 | 0 | } |
660 | | |
661 | 0 | if(resp[0] != 5) { |
662 | 0 | failf(data, "Received invalid version in initial SOCKS5 response."); |
663 | 0 | return CURLPX_BAD_VERSION; |
664 | 0 | } |
665 | | |
666 | 0 | auth_mode = resp[1]; |
667 | 0 | Curl_bufq_reset(&sx->iobuf); |
668 | |
|
669 | 0 | switch(auth_mode) { |
670 | 0 | case 0: |
671 | | /* DONE! No authentication needed. Send request. */ |
672 | 0 | sxstate(sx, cf, data, SOCKS5_ST_REQ1_INIT); |
673 | 0 | return CURLPX_OK; |
674 | 0 | case 1: |
675 | 0 | if(data->set.socks5auth & CURLAUTH_GSSAPI) { |
676 | 0 | sxstate(sx, cf, data, SOCKS5_ST_GSSAPI_INIT); |
677 | 0 | return CURLPX_OK; |
678 | 0 | } |
679 | 0 | failf(data, |
680 | 0 | "SOCKS5 GSSAPI per-message authentication is not enabled."); |
681 | 0 | return CURLPX_GSSAPI_PERMSG; |
682 | 0 | case 2: |
683 | | /* regular name + password authentication */ |
684 | 0 | if(data->set.socks5auth & CURLAUTH_BASIC) { |
685 | 0 | sxstate(sx, cf, data, SOCKS5_ST_AUTH_INIT); |
686 | 0 | return CURLPX_OK; |
687 | 0 | } |
688 | 0 | failf(data, "BASIC authentication proposed but not enabled."); |
689 | 0 | return CURLPX_NO_AUTH; |
690 | 0 | case 255: |
691 | 0 | failf(data, "No authentication method was acceptable."); |
692 | 0 | return CURLPX_NO_AUTH; |
693 | 0 | default: |
694 | 0 | failf(data, "Unknown SOCKS5 mode attempted to be used by server."); |
695 | 0 | return CURLPX_UNKNOWN_MODE; |
696 | 0 | } |
697 | 0 | } |
698 | | |
699 | | static CURLproxycode socks5_auth_init(struct Curl_cfilter *cf, |
700 | | struct socks_state *sx, |
701 | | struct Curl_easy *data) |
702 | 0 | { |
703 | | /* Needs username and password */ |
704 | 0 | size_t ulen = 0, plen = 0, nwritten; |
705 | 0 | unsigned char buf[2]; |
706 | 0 | CURLcode result; |
707 | |
|
708 | 0 | if(sx->proxy_user && sx->proxy_password) { |
709 | 0 | ulen = strlen(sx->proxy_user); |
710 | 0 | plen = strlen(sx->proxy_password); |
711 | | /* the lengths must fit in a single byte */ |
712 | 0 | if(ulen > 255) { |
713 | 0 | failf(data, "Excessive username length for proxy auth"); |
714 | 0 | return CURLPX_LONG_USER; |
715 | 0 | } |
716 | 0 | if(plen > 255) { |
717 | 0 | failf(data, "Excessive password length for proxy auth"); |
718 | 0 | return CURLPX_LONG_PASSWD; |
719 | 0 | } |
720 | 0 | } |
721 | | |
722 | | /* username/password request looks like |
723 | | * +----+------+----------+------+----------+ |
724 | | * |VER | ULEN | UNAME | PLEN | PASSWD | |
725 | | * +----+------+----------+------+----------+ |
726 | | * | 1 | 1 | 1 to 255 | 1 | 1 to 255 | |
727 | | * +----+------+----------+------+----------+ |
728 | | */ |
729 | 0 | buf[0] = 1; /* username/pw subnegotiation version */ |
730 | 0 | buf[1] = (unsigned char)ulen; |
731 | 0 | result = Curl_bufq_write(&sx->iobuf, buf, 2, &nwritten); |
732 | 0 | if(result || (nwritten != 2)) |
733 | 0 | return CURLPX_SEND_REQUEST; |
734 | 0 | if(ulen) { |
735 | 0 | result = Curl_bufq_cwrite(&sx->iobuf, sx->proxy_user, ulen, &nwritten); |
736 | 0 | if(result || (nwritten != ulen)) |
737 | 0 | return CURLPX_SEND_REQUEST; |
738 | 0 | } |
739 | 0 | buf[0] = (unsigned char) plen; |
740 | 0 | result = Curl_bufq_write(&sx->iobuf, buf, 1, &nwritten); |
741 | 0 | if(result || (nwritten != 1)) |
742 | 0 | return CURLPX_SEND_REQUEST; |
743 | 0 | if(plen) { |
744 | 0 | result = Curl_bufq_cwrite(&sx->iobuf, sx->proxy_password, plen, &nwritten); |
745 | 0 | if(result || (nwritten != plen)) |
746 | 0 | return CURLPX_SEND_REQUEST; |
747 | 0 | } |
748 | 0 | sxstate(sx, cf, data, SOCKS5_ST_AUTH_SEND); |
749 | 0 | return CURLPX_OK; |
750 | 0 | } |
751 | | |
752 | | static CURLproxycode socks5_check_auth_resp(struct socks_state *sx, |
753 | | struct Curl_cfilter *cf, |
754 | | struct Curl_easy *data) |
755 | 0 | { |
756 | 0 | const unsigned char *resp; |
757 | 0 | unsigned char auth_status; |
758 | 0 | size_t rlen; |
759 | |
|
760 | 0 | (void)cf; |
761 | 0 | if(!Curl_bufq_peek(&sx->iobuf, &resp, &rlen) || rlen < 2) { |
762 | 0 | failf(data, "SOCKS5 sub-negotiation response incomplete."); |
763 | 0 | return CURLPX_RECV_CONNECT; |
764 | 0 | } |
765 | | |
766 | | /* ignore the first (VER) byte */ |
767 | 0 | auth_status = resp[1]; |
768 | 0 | Curl_bufq_reset(&sx->iobuf); |
769 | |
|
770 | 0 | if(auth_status) { |
771 | 0 | failf(data, "User was rejected by the SOCKS5 server (%d %d).", |
772 | 0 | resp[0], resp[1]); |
773 | 0 | return CURLPX_USER_REJECTED; |
774 | 0 | } |
775 | 0 | return CURLPX_OK; |
776 | 0 | } |
777 | | |
778 | | static CURLproxycode socks5_req1_init(struct socks_state *sx, |
779 | | struct Curl_cfilter *cf, |
780 | | struct Curl_easy *data) |
781 | 0 | { |
782 | 0 | unsigned char req[5]; |
783 | 0 | unsigned char ipbuf[16]; |
784 | 0 | const unsigned char *destination; |
785 | 0 | unsigned char desttype, destlen, hdlen; |
786 | 0 | size_t nwritten; |
787 | 0 | CURLcode result; |
788 | |
|
789 | 0 | req[0] = 5; /* version (SOCKS5) */ |
790 | 0 | req[1] = 1; /* connect */ |
791 | 0 | req[2] = 0; /* must be zero */ |
792 | 0 | if(sx->resolve_local) { |
793 | | /* rest of request is added after resolving */ |
794 | 0 | result = Curl_bufq_write(&sx->iobuf, req, 3, &nwritten); |
795 | 0 | if(result || (nwritten != 3)) |
796 | 0 | return CURLPX_SEND_REQUEST; |
797 | 0 | return CURLPX_OK; |
798 | 0 | } |
799 | | |
800 | | /* remote resolving, send what type+addr/string to resolve */ |
801 | 0 | #ifdef USE_IPV6 |
802 | 0 | if(cf->conn->bits.ipv6_ip) { |
803 | 0 | desttype = 4; |
804 | 0 | destination = ipbuf; |
805 | 0 | destlen = 16; |
806 | 0 | if(curlx_inet_pton(AF_INET6, sx->hostname, ipbuf) != 1) |
807 | 0 | return CURLPX_BAD_ADDRESS_TYPE; |
808 | 0 | } |
809 | 0 | else |
810 | 0 | #endif |
811 | 0 | if(curlx_inet_pton(AF_INET, sx->hostname, ipbuf) == 1) { |
812 | 0 | desttype = 1; |
813 | 0 | destination = ipbuf; |
814 | 0 | destlen = 4; |
815 | 0 | } |
816 | 0 | else { |
817 | 0 | const size_t hostname_len = strlen(sx->hostname); |
818 | 0 | desttype = 3; |
819 | 0 | destination = (const unsigned char *)sx->hostname; |
820 | 0 | destlen = (unsigned char) hostname_len; /* one byte length */ |
821 | 0 | } |
822 | | |
823 | 0 | req[3] = desttype; |
824 | 0 | req[4] = destlen; |
825 | 0 | hdlen = (desttype == 3) ? 5 : 4; /* no length byte for ip addresses */ |
826 | 0 | result = Curl_bufq_write(&sx->iobuf, req, hdlen, &nwritten); |
827 | 0 | if(result || (nwritten != hdlen)) |
828 | 0 | return CURLPX_SEND_REQUEST; |
829 | 0 | result = Curl_bufq_write(&sx->iobuf, destination, destlen, &nwritten); |
830 | 0 | if(result || (nwritten != destlen)) |
831 | 0 | return CURLPX_SEND_REQUEST; |
832 | | /* PORT MSB+LSB */ |
833 | 0 | req[0] = (unsigned char)((sx->remote_port >> 8) & 0xff); |
834 | 0 | req[1] = (unsigned char)(sx->remote_port & 0xff); |
835 | 0 | result = Curl_bufq_write(&sx->iobuf, req, 2, &nwritten); |
836 | 0 | if(result || (nwritten != 2)) |
837 | 0 | return CURLPX_SEND_REQUEST; |
838 | 0 | CURL_TRC_CF(data, cf, "SOCKS5 connect to %s:%d (remotely resolved)", |
839 | 0 | sx->hostname, sx->remote_port); |
840 | 0 | return CURLPX_OK; |
841 | 0 | } |
842 | | |
843 | | static CURLproxycode socks5_resolving(struct socks_state *sx, |
844 | | struct Curl_cfilter *cf, |
845 | | struct Curl_easy *data, |
846 | | bool *done) |
847 | 0 | { |
848 | 0 | struct Curl_dns_entry *dns = NULL; |
849 | 0 | struct Curl_addrinfo *hp = NULL; |
850 | 0 | char dest[MAX_IPADR_LEN]; /* printable address */ |
851 | 0 | unsigned char *destination = NULL; |
852 | 0 | unsigned char desttype = 1, destlen = 4; |
853 | 0 | unsigned char req[2]; |
854 | 0 | CURLcode result; |
855 | 0 | CURLproxycode presult = CURLPX_OK; |
856 | 0 | size_t nwritten; |
857 | |
|
858 | 0 | *done = FALSE; |
859 | 0 | if(sx->start_resolving) { |
860 | | /* need to resolve hostname to add destination address */ |
861 | 0 | sx->start_resolving = FALSE; |
862 | |
|
863 | 0 | result = Curl_resolv(data, sx->hostname, sx->remote_port, |
864 | 0 | cf->conn->ip_version, TRUE, &dns); |
865 | 0 | if(result == CURLE_AGAIN) { |
866 | 0 | CURL_TRC_CF(data, cf, "SOCKS5 non-blocking resolve of %s", |
867 | 0 | sx->hostname); |
868 | 0 | return CURLPX_OK; |
869 | 0 | } |
870 | 0 | else if(result) |
871 | 0 | return CURLPX_RESOLVE_HOST; |
872 | 0 | } |
873 | 0 | else { |
874 | | /* check if we have the name resolved by now */ |
875 | 0 | result = Curl_resolv_check(data, &dns); |
876 | 0 | if(!result && !dns) |
877 | 0 | return CURLPX_OK; |
878 | 0 | } |
879 | | |
880 | 0 | if(result || !dns) { |
881 | 0 | failf(data, "Failed to resolve \"%s\" for SOCKS5 connect.", |
882 | 0 | sx->hostname); |
883 | 0 | presult = CURLPX_RESOLVE_HOST; |
884 | 0 | goto out; |
885 | 0 | } |
886 | | |
887 | 0 | if(dns) |
888 | 0 | hp = dns->addr; |
889 | 0 | #ifdef USE_IPV6 |
890 | 0 | if(data->set.ipver != CURL_IPRESOLVE_WHATEVER) { |
891 | 0 | int wanted_family = data->set.ipver == CURL_IPRESOLVE_V4 ? |
892 | 0 | AF_INET : AF_INET6; |
893 | | /* scan for the first proper address */ |
894 | 0 | while(hp && (hp->ai_family != wanted_family)) |
895 | 0 | hp = hp->ai_next; |
896 | 0 | } |
897 | 0 | #endif |
898 | 0 | if(!hp) { |
899 | 0 | failf(data, "Failed to resolve \"%s\" for SOCKS5 connect.", |
900 | 0 | sx->hostname); |
901 | 0 | presult = CURLPX_RESOLVE_HOST; |
902 | 0 | goto out; |
903 | 0 | } |
904 | | |
905 | 0 | Curl_printable_address(hp, dest, sizeof(dest)); |
906 | |
|
907 | 0 | if(hp->ai_family == AF_INET) { |
908 | 0 | struct sockaddr_in *saddr_in; |
909 | 0 | desttype = 1; /* ATYP: IPv4 = 1 */ |
910 | 0 | destlen = 4; |
911 | 0 | saddr_in = (struct sockaddr_in *)(void *)hp->ai_addr; |
912 | 0 | destination = (unsigned char *)&saddr_in->sin_addr.s_addr; |
913 | 0 | CURL_TRC_CF(data, cf, "SOCKS5 connect to %s:%d (locally resolved)", |
914 | 0 | dest, sx->remote_port); |
915 | 0 | } |
916 | 0 | #ifdef USE_IPV6 |
917 | 0 | else if(hp->ai_family == AF_INET6) { |
918 | 0 | struct sockaddr_in6 *saddr_in6; |
919 | 0 | desttype = 4; /* ATYP: IPv6 = 4 */ |
920 | 0 | destlen = 16; |
921 | 0 | saddr_in6 = (struct sockaddr_in6 *)(void *)hp->ai_addr; |
922 | 0 | destination = (unsigned char *)&saddr_in6->sin6_addr.s6_addr; |
923 | 0 | CURL_TRC_CF(data, cf, "SOCKS5 connect to [%s]:%d (locally resolved)", |
924 | 0 | dest, sx->remote_port); |
925 | 0 | } |
926 | 0 | #endif |
927 | |
|
928 | 0 | if(!destination) { |
929 | 0 | failf(data, "SOCKS5 connection to %s not supported", dest); |
930 | 0 | presult = CURLPX_RESOLVE_HOST; |
931 | 0 | goto out; |
932 | 0 | } |
933 | | |
934 | 0 | req[0] = desttype; |
935 | 0 | result = Curl_bufq_write(&sx->iobuf, req, 1, &nwritten); |
936 | 0 | if(result || (nwritten != 1)) { |
937 | 0 | presult = CURLPX_SEND_REQUEST; |
938 | 0 | goto out; |
939 | 0 | } |
940 | 0 | result = Curl_bufq_write(&sx->iobuf, destination, destlen, &nwritten); |
941 | 0 | if(result || (nwritten != destlen)) { |
942 | 0 | presult = CURLPX_SEND_REQUEST; |
943 | 0 | goto out; |
944 | 0 | } |
945 | | /* PORT MSB+LSB */ |
946 | 0 | req[0] = (unsigned char)((sx->remote_port >> 8) & 0xffu); |
947 | 0 | req[1] = (unsigned char)(sx->remote_port & 0xffu); |
948 | 0 | result = Curl_bufq_write(&sx->iobuf, req, 2, &nwritten); |
949 | 0 | if(result || (nwritten != 2)) { |
950 | 0 | presult = CURLPX_SEND_REQUEST; |
951 | 0 | goto out; |
952 | 0 | } |
953 | | |
954 | 0 | out: |
955 | 0 | if(dns) |
956 | 0 | Curl_resolv_unlink(data, &dns); |
957 | 0 | *done = (presult == CURLPX_OK); |
958 | 0 | return presult; |
959 | 0 | } |
960 | | |
961 | | static CURLproxycode socks5_recv_resp1(struct socks_state *sx, |
962 | | struct Curl_cfilter *cf, |
963 | | struct Curl_easy *data, |
964 | | bool *done) |
965 | 0 | { |
966 | 0 | const unsigned char *resp; |
967 | 0 | size_t rlen, resp_len = 8; /* minimum response length */ |
968 | 0 | CURLproxycode presult; |
969 | |
|
970 | 0 | presult = socks_recv(sx, cf, data, resp_len, done); |
971 | 0 | if(presult) |
972 | 0 | return presult; |
973 | 0 | else if(!*done) |
974 | 0 | return CURLPX_OK; |
975 | | |
976 | 0 | if(!Curl_bufq_peek(&sx->iobuf, &resp, &rlen) || rlen < resp_len) { |
977 | 0 | failf(data, "SOCKS5 response is incomplete."); |
978 | 0 | return CURLPX_RECV_CONNECT; |
979 | 0 | } |
980 | | |
981 | | /* Response packet includes BND.ADDR is variable length parameter by RFC |
982 | | 1928, so the response packet MUST be read until the end to avoid errors |
983 | | at subsequent protocol level. |
984 | | |
985 | | +----+-----+-------+------+----------+----------+ |
986 | | |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT | |
987 | | +----+-----+-------+------+----------+----------+ |
988 | | | 1 | 1 | X'00' | 1 | Variable | 2 | |
989 | | +----+-----+-------+------+----------+----------+ |
990 | | |
991 | | ATYP: |
992 | | o IP v4 address: X'01', BND.ADDR = 4 byte |
993 | | o domain name: X'03', BND.ADDR = [ 1 byte length, string ] |
994 | | o IP v6 address: X'04', BND.ADDR = 16 byte |
995 | | */ |
996 | 0 | if(resp[0] != 5) { /* version */ |
997 | 0 | failf(data, "SOCKS5 reply has wrong version, version should be 5."); |
998 | 0 | return CURLPX_BAD_VERSION; |
999 | 0 | } |
1000 | 0 | else if(resp[1]) { /* Anything besides 0 is an error */ |
1001 | 0 | CURLproxycode rc = CURLPX_REPLY_UNASSIGNED; |
1002 | 0 | int code = resp[1]; |
1003 | 0 | failf(data, "cannot complete SOCKS5 connection to %s. (%d)", |
1004 | 0 | sx->hostname, code); |
1005 | 0 | if(code < 9) { |
1006 | | /* RFC 1928 section 6 lists: */ |
1007 | 0 | static const CURLproxycode lookup[] = { |
1008 | 0 | CURLPX_OK, |
1009 | 0 | CURLPX_REPLY_GENERAL_SERVER_FAILURE, |
1010 | 0 | CURLPX_REPLY_NOT_ALLOWED, |
1011 | 0 | CURLPX_REPLY_NETWORK_UNREACHABLE, |
1012 | 0 | CURLPX_REPLY_HOST_UNREACHABLE, |
1013 | 0 | CURLPX_REPLY_CONNECTION_REFUSED, |
1014 | 0 | CURLPX_REPLY_TTL_EXPIRED, |
1015 | 0 | CURLPX_REPLY_COMMAND_NOT_SUPPORTED, |
1016 | 0 | CURLPX_REPLY_ADDRESS_TYPE_NOT_SUPPORTED, |
1017 | 0 | }; |
1018 | 0 | rc = lookup[code]; |
1019 | 0 | } |
1020 | 0 | return rc; |
1021 | 0 | } |
1022 | | |
1023 | | /* Calculate real packet size */ |
1024 | 0 | switch(resp[3]) { |
1025 | 0 | case 1: /* IPv4 */ |
1026 | 0 | resp_len = 4 + 4 + 2; |
1027 | 0 | break; |
1028 | 0 | case 3: /* domain name */ |
1029 | 0 | resp_len = 4 + 1 + resp[4] + 2; /* header, var length, var bytes, port */ |
1030 | 0 | break; |
1031 | 0 | case 4: /* IPv6 */ |
1032 | 0 | resp_len = 4 + 16 + 2; |
1033 | 0 | break; |
1034 | 0 | default: |
1035 | 0 | failf(data, "SOCKS5 reply has wrong address type."); |
1036 | 0 | return CURLPX_BAD_ADDRESS_TYPE; |
1037 | 0 | } |
1038 | | |
1039 | | /* receive the rest of the response */ |
1040 | 0 | presult = socks_recv(sx, cf, data, resp_len, done); |
1041 | 0 | if(presult) |
1042 | 0 | return presult; |
1043 | 0 | else if(!*done) |
1044 | 0 | return CURLPX_OK; |
1045 | | |
1046 | 0 | if(!Curl_bufq_peek(&sx->iobuf, &resp, &rlen) || rlen < resp_len) { |
1047 | 0 | failf(data, "SOCKS5 response is incomplete."); |
1048 | 0 | return CURLPX_RECV_CONNECT; |
1049 | 0 | } |
1050 | | /* got it all */ |
1051 | 0 | *done = TRUE; |
1052 | 0 | return CURLPX_OK; |
1053 | 0 | } |
1054 | | |
1055 | | /* |
1056 | | * This function logs in to a SOCKS5 proxy and sends the specifics to the final |
1057 | | * destination server. |
1058 | | */ |
1059 | | static CURLproxycode socks5_connect(struct Curl_cfilter *cf, |
1060 | | struct socks_state *sx, |
1061 | | struct Curl_easy *data) |
1062 | 0 | { |
1063 | 0 | CURLproxycode presult; |
1064 | 0 | bool done; |
1065 | |
|
1066 | 0 | process_state: |
1067 | 0 | switch(sx->state) { |
1068 | 0 | case SOCKS_ST_INIT: |
1069 | 0 | sx->version = 5; |
1070 | 0 | sx->resolve_local = (cf->conn->socks_proxy.proxytype == CURLPROXY_SOCKS5); |
1071 | 0 | sxstate(sx, cf, data, SOCKS5_ST_START); |
1072 | 0 | FALLTHROUGH(); |
1073 | |
|
1074 | 0 | case SOCKS5_ST_START: |
1075 | 0 | if(cf->conn->bits.httpproxy) |
1076 | 0 | CURL_TRC_CF(data, cf, "SOCKS5: connecting to HTTP proxy %s port %d", |
1077 | 0 | sx->hostname, sx->remote_port); |
1078 | 0 | presult = socks5_req0_init(cf, sx, data); |
1079 | 0 | if(presult) |
1080 | 0 | return socks_failed(sx, cf, data, presult); |
1081 | 0 | sxstate(sx, cf, data, SOCKS5_ST_REQ0_SEND); |
1082 | 0 | FALLTHROUGH(); |
1083 | |
|
1084 | 0 | case SOCKS5_ST_REQ0_SEND: |
1085 | 0 | presult = socks_flush(sx, cf, data, &done); |
1086 | 0 | if(presult) |
1087 | 0 | return socks_failed(sx, cf, data, presult); |
1088 | 0 | else if(!done) |
1089 | 0 | return CURLPX_OK; |
1090 | | /* done sending! */ |
1091 | 0 | sxstate(sx, cf, data, SOCKS5_ST_RESP0_RECV); |
1092 | 0 | FALLTHROUGH(); |
1093 | |
|
1094 | 0 | case SOCKS5_ST_RESP0_RECV: |
1095 | 0 | presult = socks_recv(sx, cf, data, 2, &done); |
1096 | 0 | if(presult) |
1097 | 0 | return socks_failed(sx, cf, data, presult); |
1098 | 0 | else if(!done) |
1099 | 0 | return CURLPX_OK; |
1100 | 0 | presult = socks5_check_resp0(sx, cf, data); |
1101 | 0 | if(presult) |
1102 | 0 | return socks_failed(sx, cf, data, presult); |
1103 | | /* socks5_check_resp0() sets next socks state */ |
1104 | 0 | goto process_state; |
1105 | | |
1106 | 0 | case SOCKS5_ST_GSSAPI_INIT: { |
1107 | | #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI) |
1108 | | /* GSSAPI stuff done non-blocking */ |
1109 | | CURLcode result = Curl_SOCKS5_gssapi_negotiate(cf, data); |
1110 | | if(result) { |
1111 | | failf(data, "Unable to negotiate SOCKS5 GSS-API context."); |
1112 | | return CURLPX_GSSAPI; |
1113 | | } |
1114 | | sxstate(sx, cf, data, SOCKS5_ST_REQ1_INIT); |
1115 | | goto process_state; |
1116 | | #else |
1117 | 0 | failf(data, |
1118 | 0 | "SOCKS5 GSSAPI per-message authentication is not supported."); |
1119 | 0 | return socks_failed(sx, cf, data, CURLPX_GSSAPI_PERMSG); |
1120 | 0 | #endif |
1121 | 0 | } |
1122 | | |
1123 | 0 | case SOCKS5_ST_AUTH_INIT: |
1124 | 0 | presult = socks5_auth_init(cf, sx, data); |
1125 | 0 | if(presult) |
1126 | 0 | return socks_failed(sx, cf, data, presult); |
1127 | 0 | sxstate(sx, cf, data, SOCKS5_ST_AUTH_SEND); |
1128 | 0 | FALLTHROUGH(); |
1129 | |
|
1130 | 0 | case SOCKS5_ST_AUTH_SEND: |
1131 | 0 | presult = socks_flush(sx, cf, data, &done); |
1132 | 0 | if(presult) |
1133 | 0 | return socks_failed(sx, cf, data, presult); |
1134 | 0 | else if(!done) |
1135 | 0 | return CURLPX_OK; |
1136 | 0 | sxstate(sx, cf, data, SOCKS5_ST_AUTH_RECV); |
1137 | 0 | FALLTHROUGH(); |
1138 | |
|
1139 | 0 | case SOCKS5_ST_AUTH_RECV: |
1140 | 0 | presult = socks_recv(sx, cf, data, 2, &done); |
1141 | 0 | if(presult) |
1142 | 0 | return socks_failed(sx, cf, data, presult); |
1143 | 0 | else if(!done) |
1144 | 0 | return CURLPX_OK; |
1145 | 0 | presult = socks5_check_auth_resp(sx, cf, data); |
1146 | 0 | if(presult) |
1147 | 0 | return socks_failed(sx, cf, data, presult); |
1148 | | /* Everything is good so far, user was authenticated! */ |
1149 | 0 | sxstate(sx, cf, data, SOCKS5_ST_REQ1_INIT); |
1150 | 0 | FALLTHROUGH(); |
1151 | |
|
1152 | 0 | case SOCKS5_ST_REQ1_INIT: |
1153 | 0 | presult = socks5_req1_init(sx, cf, data); |
1154 | 0 | if(presult) |
1155 | 0 | return socks_failed(sx, cf, data, presult); |
1156 | 0 | if(!sx->resolve_local) { |
1157 | | /* we do not resolve, request is complete */ |
1158 | 0 | sxstate(sx, cf, data, SOCKS5_ST_REQ1_SEND); |
1159 | 0 | goto process_state; |
1160 | 0 | } |
1161 | 0 | sx->start_resolving = TRUE; |
1162 | 0 | sxstate(sx, cf, data, SOCKS5_ST_RESOLVING); |
1163 | 0 | FALLTHROUGH(); |
1164 | |
|
1165 | 0 | case SOCKS5_ST_RESOLVING: |
1166 | 0 | presult = socks5_resolving(sx, cf, data, &done); |
1167 | 0 | if(presult) |
1168 | 0 | return socks_failed(sx, cf, data, presult); |
1169 | 0 | if(!done) |
1170 | 0 | return CURLPX_OK; |
1171 | 0 | sxstate(sx, cf, data, SOCKS5_ST_REQ1_SEND); |
1172 | 0 | FALLTHROUGH(); |
1173 | |
|
1174 | 0 | case SOCKS5_ST_REQ1_SEND: |
1175 | 0 | presult = socks_flush(sx, cf, data, &done); |
1176 | 0 | if(presult) |
1177 | 0 | return socks_failed(sx, cf, data, presult); |
1178 | 0 | else if(!done) |
1179 | 0 | return CURLPX_OK; |
1180 | | #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI) |
1181 | | if(cf->conn->socks5_gssapi_enctype) { |
1182 | | failf(data, "SOCKS5 GSS-API protection not yet implemented."); |
1183 | | return CURLPX_GSSAPI_PROTECTION; |
1184 | | } |
1185 | | #endif |
1186 | 0 | sxstate(sx, cf, data, SOCKS5_ST_RESP1_RECV); |
1187 | 0 | FALLTHROUGH(); |
1188 | |
|
1189 | 0 | case SOCKS5_ST_RESP1_RECV: |
1190 | 0 | presult = socks5_recv_resp1(sx, cf, data, &done); |
1191 | 0 | if(presult) |
1192 | 0 | return socks_failed(sx, cf, data, presult); |
1193 | 0 | if(!done) |
1194 | 0 | return CURLPX_OK; |
1195 | 0 | CURL_TRC_CF(data, cf, "SOCKS5 request granted."); |
1196 | 0 | sxstate(sx, cf, data, SOCKS_ST_SUCCESS); |
1197 | 0 | FALLTHROUGH(); |
1198 | |
|
1199 | 0 | case SOCKS_ST_SUCCESS: |
1200 | 0 | return CURLPX_OK; |
1201 | | |
1202 | 0 | case SOCKS_ST_FAILED: |
1203 | 0 | DEBUGASSERT(sx->presult); |
1204 | 0 | return sx->presult; |
1205 | | |
1206 | 0 | default: |
1207 | 0 | DEBUGASSERT(0); |
1208 | 0 | return socks_failed(sx, cf, data, CURLPX_SEND_REQUEST); |
1209 | 0 | } |
1210 | 0 | } |
1211 | | |
1212 | | static void socks_proxy_cf_free(struct Curl_cfilter *cf) |
1213 | 0 | { |
1214 | 0 | struct socks_state *sxstate = cf->ctx; |
1215 | 0 | if(sxstate) { |
1216 | 0 | Curl_bufq_free(&sxstate->iobuf); |
1217 | 0 | free(sxstate); |
1218 | 0 | cf->ctx = NULL; |
1219 | 0 | } |
1220 | 0 | } |
1221 | | |
1222 | | /* After a TCP connection to the proxy has been verified, this function does |
1223 | | the next magic steps. If 'done' is not set TRUE, it is not done yet and |
1224 | | must be called again. |
1225 | | |
1226 | | Note: this function's sub-functions call failf() |
1227 | | |
1228 | | */ |
1229 | | static CURLcode socks_proxy_cf_connect(struct Curl_cfilter *cf, |
1230 | | struct Curl_easy *data, |
1231 | | bool *done) |
1232 | 0 | { |
1233 | 0 | CURLcode result; |
1234 | 0 | struct connectdata *conn = cf->conn; |
1235 | 0 | int sockindex = cf->sockindex; |
1236 | 0 | struct socks_state *sx = cf->ctx; |
1237 | 0 | CURLproxycode pxresult = CURLPX_OK; |
1238 | |
|
1239 | 0 | if(cf->connected) { |
1240 | 0 | *done = TRUE; |
1241 | 0 | return CURLE_OK; |
1242 | 0 | } |
1243 | | |
1244 | 0 | result = cf->next->cft->do_connect(cf->next, data, done); |
1245 | 0 | if(result || !*done) |
1246 | 0 | return result; |
1247 | | |
1248 | 0 | if(!sx) { |
1249 | 0 | cf->ctx = sx = calloc(1, sizeof(*sx)); |
1250 | 0 | if(!sx) |
1251 | 0 | return CURLE_OUT_OF_MEMORY; |
1252 | | |
1253 | | /* for the secondary socket (FTP), use the "connect to host" |
1254 | | * but ignore the "connect to port" (use the secondary port) |
1255 | | */ |
1256 | 0 | sx->hostname = |
1257 | 0 | conn->bits.httpproxy ? |
1258 | 0 | conn->http_proxy.host.name : |
1259 | 0 | conn->bits.conn_to_host ? |
1260 | 0 | conn->conn_to_host.name : |
1261 | 0 | sockindex == SECONDARYSOCKET ? |
1262 | 0 | conn->secondaryhostname : conn->host.name; |
1263 | 0 | sx->remote_port = |
1264 | 0 | conn->bits.httpproxy ? (int)conn->http_proxy.port : |
1265 | 0 | sockindex == SECONDARYSOCKET ? conn->secondary_port : |
1266 | 0 | conn->bits.conn_to_port ? conn->conn_to_port : |
1267 | 0 | conn->remote_port; |
1268 | 0 | sx->proxy_user = conn->socks_proxy.user; |
1269 | 0 | sx->proxy_password = conn->socks_proxy.passwd; |
1270 | 0 | Curl_bufq_init2(&sx->iobuf, SOCKS_CHUNK_SIZE, SOCKS_CHUNKS, |
1271 | 0 | BUFQ_OPT_SOFT_LIMIT); |
1272 | 0 | } |
1273 | | |
1274 | 0 | switch(conn->socks_proxy.proxytype) { |
1275 | 0 | case CURLPROXY_SOCKS5: |
1276 | 0 | case CURLPROXY_SOCKS5_HOSTNAME: |
1277 | 0 | pxresult = socks5_connect(cf, sx, data); |
1278 | 0 | break; |
1279 | | |
1280 | 0 | case CURLPROXY_SOCKS4: |
1281 | 0 | case CURLPROXY_SOCKS4A: |
1282 | 0 | pxresult = socks4_connect(cf, sx, data); |
1283 | 0 | break; |
1284 | | |
1285 | 0 | default: |
1286 | 0 | failf(data, "unknown proxytype option given"); |
1287 | 0 | result = CURLE_COULDNT_CONNECT; |
1288 | 0 | goto out; |
1289 | 0 | } |
1290 | | |
1291 | 0 | if(pxresult) { |
1292 | 0 | result = CURLE_PROXY; |
1293 | 0 | data->info.pxcode = pxresult; |
1294 | 0 | goto out; |
1295 | 0 | } |
1296 | 0 | else if(sx->state != SOCKS_ST_SUCCESS) |
1297 | 0 | goto out; |
1298 | | |
1299 | 0 | #ifndef CURL_DISABLE_VERBOSE_STRINGS |
1300 | 0 | if(Curl_trc_is_verbose(data)) { |
1301 | 0 | struct ip_quadruple ipquad; |
1302 | 0 | bool is_ipv6; |
1303 | 0 | if(!Curl_conn_cf_get_ip_info(cf->next, data, &is_ipv6, &ipquad)) |
1304 | 0 | infof(data, "Opened %sSOCKS connection from %s port %u to %s port %u " |
1305 | 0 | "(via %s port %u)", |
1306 | 0 | (sockindex == SECONDARYSOCKET) ? "2nd " : "", |
1307 | 0 | ipquad.local_ip, ipquad.local_port, |
1308 | 0 | sx->hostname, sx->remote_port, |
1309 | 0 | ipquad.remote_ip, ipquad.remote_port); |
1310 | 0 | else |
1311 | 0 | infof(data, "Opened %sSOCKS connection", |
1312 | 0 | (sockindex == SECONDARYSOCKET) ? "2nd " : ""); |
1313 | 0 | } |
1314 | 0 | #endif |
1315 | 0 | socks_proxy_cf_free(cf); |
1316 | 0 | cf->connected = TRUE; |
1317 | |
|
1318 | 0 | out: |
1319 | 0 | *done = cf->connected; |
1320 | 0 | return result; |
1321 | 0 | } |
1322 | | |
1323 | | static CURLcode socks_cf_adjust_pollset(struct Curl_cfilter *cf, |
1324 | | struct Curl_easy *data, |
1325 | | struct easy_pollset *ps) |
1326 | 0 | { |
1327 | 0 | struct socks_state *sx = cf->ctx; |
1328 | 0 | CURLcode result = CURLE_OK; |
1329 | |
|
1330 | 0 | if(!cf->connected && sx) { |
1331 | | /* If we are not connected, the filter below is and has nothing |
1332 | | * to wait on, we determine what to wait for. */ |
1333 | 0 | curl_socket_t sock = Curl_conn_cf_get_socket(cf, data); |
1334 | 0 | switch(sx->state) { |
1335 | 0 | case SOCKS4_ST_SEND: |
1336 | 0 | case SOCKS5_ST_REQ0_SEND: |
1337 | 0 | case SOCKS5_ST_AUTH_SEND: |
1338 | 0 | case SOCKS5_ST_REQ1_SEND: |
1339 | 0 | CURL_TRC_CF(data, cf, "adjust pollset out (%d)", sx->state); |
1340 | 0 | result = Curl_pollset_set_out_only(data, ps, sock); |
1341 | 0 | break; |
1342 | 0 | default: |
1343 | 0 | CURL_TRC_CF(data, cf, "adjust pollset in (%d)", sx->state); |
1344 | 0 | result = Curl_pollset_set_in_only(data, ps, sock); |
1345 | 0 | break; |
1346 | 0 | } |
1347 | 0 | } |
1348 | 0 | return result; |
1349 | 0 | } |
1350 | | |
1351 | | static void socks_proxy_cf_close(struct Curl_cfilter *cf, |
1352 | | struct Curl_easy *data) |
1353 | 0 | { |
1354 | |
|
1355 | 0 | DEBUGASSERT(cf->next); |
1356 | 0 | cf->connected = FALSE; |
1357 | 0 | socks_proxy_cf_free(cf); |
1358 | 0 | cf->next->cft->do_close(cf->next, data); |
1359 | 0 | } |
1360 | | |
1361 | | static void socks_proxy_cf_destroy(struct Curl_cfilter *cf, |
1362 | | struct Curl_easy *data) |
1363 | 0 | { |
1364 | 0 | (void)data; |
1365 | 0 | socks_proxy_cf_free(cf); |
1366 | 0 | } |
1367 | | |
1368 | | static CURLcode socks_cf_query(struct Curl_cfilter *cf, |
1369 | | struct Curl_easy *data, |
1370 | | int query, int *pres1, void *pres2) |
1371 | 0 | { |
1372 | 0 | struct socks_state *sx = cf->ctx; |
1373 | |
|
1374 | 0 | switch(query) { |
1375 | 0 | case CF_QUERY_HOST_PORT: |
1376 | 0 | if(sx) { |
1377 | 0 | *pres1 = sx->remote_port; |
1378 | 0 | *((const char **)pres2) = sx->hostname; |
1379 | 0 | return CURLE_OK; |
1380 | 0 | } |
1381 | 0 | break; |
1382 | 0 | case CF_QUERY_ALPN_NEGOTIATED: { |
1383 | 0 | const char **palpn = pres2; |
1384 | 0 | DEBUGASSERT(palpn); |
1385 | 0 | *palpn = NULL; |
1386 | 0 | return CURLE_OK; |
1387 | 0 | } |
1388 | 0 | default: |
1389 | 0 | break; |
1390 | 0 | } |
1391 | 0 | return cf->next ? |
1392 | 0 | cf->next->cft->query(cf->next, data, query, pres1, pres2) : |
1393 | 0 | CURLE_UNKNOWN_OPTION; |
1394 | 0 | } |
1395 | | |
1396 | | struct Curl_cftype Curl_cft_socks_proxy = { |
1397 | | "SOCKS", |
1398 | | CF_TYPE_IP_CONNECT|CF_TYPE_PROXY, |
1399 | | 0, |
1400 | | socks_proxy_cf_destroy, |
1401 | | socks_proxy_cf_connect, |
1402 | | socks_proxy_cf_close, |
1403 | | Curl_cf_def_shutdown, |
1404 | | socks_cf_adjust_pollset, |
1405 | | Curl_cf_def_data_pending, |
1406 | | Curl_cf_def_send, |
1407 | | Curl_cf_def_recv, |
1408 | | Curl_cf_def_cntrl, |
1409 | | Curl_cf_def_conn_is_alive, |
1410 | | Curl_cf_def_conn_keep_alive, |
1411 | | socks_cf_query, |
1412 | | }; |
1413 | | |
1414 | | CURLcode Curl_cf_socks_proxy_insert_after(struct Curl_cfilter *cf_at, |
1415 | | struct Curl_easy *data) |
1416 | 0 | { |
1417 | 0 | struct Curl_cfilter *cf; |
1418 | 0 | CURLcode result; |
1419 | |
|
1420 | 0 | (void)data; |
1421 | 0 | result = Curl_cf_create(&cf, &Curl_cft_socks_proxy, NULL); |
1422 | 0 | if(!result) |
1423 | 0 | Curl_conn_cf_insert_after(cf_at, cf); |
1424 | 0 | return result; |
1425 | 0 | } |
1426 | | |
1427 | | #endif /* CURL_DISABLE_PROXY */ |