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 | | #if !defined(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 "sendf.h" |
38 | | #include "select.h" |
39 | | #include "cfilters.h" |
40 | | #include "connect.h" |
41 | | #include "curlx/timeval.h" |
42 | | #include "socks.h" |
43 | | #include "multiif.h" /* for getsock macros */ |
44 | | #include "curlx/inet_pton.h" |
45 | | #include "url.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 | | /* for the (SOCKS) connect state machine */ |
53 | | enum connect_t { |
54 | | CONNECT_INIT, |
55 | | CONNECT_SOCKS_INIT, /* 1 */ |
56 | | CONNECT_SOCKS_SEND, /* 2 waiting to send more first data */ |
57 | | CONNECT_SOCKS_READ_INIT, /* 3 set up read */ |
58 | | CONNECT_SOCKS_READ, /* 4 read server response */ |
59 | | CONNECT_GSSAPI_INIT, /* 5 */ |
60 | | CONNECT_AUTH_INIT, /* 6 setup outgoing auth buffer */ |
61 | | CONNECT_AUTH_SEND, /* 7 send auth */ |
62 | | CONNECT_AUTH_READ, /* 8 read auth response */ |
63 | | CONNECT_REQ_INIT, /* 9 init SOCKS "request" */ |
64 | | CONNECT_RESOLVING, /* 10 */ |
65 | | CONNECT_RESOLVED, /* 11 */ |
66 | | CONNECT_RESOLVE_REMOTE, /* 12 */ |
67 | | CONNECT_REQ_SEND, /* 13 */ |
68 | | CONNECT_REQ_SENDING, /* 14 */ |
69 | | CONNECT_REQ_READ, /* 15 */ |
70 | | CONNECT_REQ_READ_MORE, /* 16 */ |
71 | | CONNECT_DONE /* 17 connected fine to the remote or the SOCKS proxy */ |
72 | | }; |
73 | | |
74 | | #define CURL_SOCKS_BUF_SIZE 600 |
75 | | |
76 | | /* make sure we configure it not too low */ |
77 | | #if CURL_SOCKS_BUF_SIZE < 600 |
78 | | #error CURL_SOCKS_BUF_SIZE must be at least 600 |
79 | | #endif |
80 | | |
81 | | |
82 | | struct socks_state { |
83 | | enum connect_t state; |
84 | | size_t outstanding; /* send this many bytes more */ |
85 | | unsigned char buffer[CURL_SOCKS_BUF_SIZE]; |
86 | | unsigned char *outp; /* send from this pointer */ |
87 | | |
88 | | const char *hostname; |
89 | | int remote_port; |
90 | | const char *proxy_user; |
91 | | const char *proxy_password; |
92 | | }; |
93 | | |
94 | | #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI) |
95 | | /* |
96 | | * Helper read-from-socket functions. Does the same as Curl_read() but it |
97 | | * blocks until all bytes amount of buffersize will be read. No more, no less. |
98 | | * |
99 | | * This is STUPID BLOCKING behavior. Only used by the SOCKS GSSAPI functions. |
100 | | */ |
101 | | int Curl_blockread_all(struct Curl_cfilter *cf, |
102 | | struct Curl_easy *data, /* transfer */ |
103 | | char *buf, /* store read data here */ |
104 | | size_t blen, /* space in buf */ |
105 | | size_t *pnread) /* amount bytes read */ |
106 | | { |
107 | | size_t nread = 0; |
108 | | CURLcode err; |
109 | | |
110 | | *pnread = 0; |
111 | | for(;;) { |
112 | | timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE); |
113 | | if(timeout_ms < 0) { |
114 | | /* we already got the timeout */ |
115 | | return CURLE_OPERATION_TIMEDOUT; |
116 | | } |
117 | | if(!timeout_ms) |
118 | | timeout_ms = TIMEDIFF_T_MAX; |
119 | | if(SOCKET_READABLE(cf->conn->sock[cf->sockindex], timeout_ms) <= 0) { |
120 | | return ~CURLE_OK; |
121 | | } |
122 | | err = Curl_conn_cf_recv(cf->next, data, buf, blen, &nread); |
123 | | if(CURLE_AGAIN == err) |
124 | | continue; |
125 | | else if(err) |
126 | | return (int)err; |
127 | | |
128 | | if(blen == nread) { |
129 | | *pnread += nread; |
130 | | return CURLE_OK; |
131 | | } |
132 | | if(!nread) /* EOF */ |
133 | | return ~CURLE_OK; |
134 | | |
135 | | buf += nread; |
136 | | blen -= nread; |
137 | | *pnread += nread; |
138 | | } |
139 | | } |
140 | | #endif |
141 | | |
142 | | #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS) |
143 | | #define DEBUG_AND_VERBOSE |
144 | 4.74k | #define sxstate(x,d,y) socksstate(x,d,y, __LINE__) |
145 | | #else |
146 | | #define sxstate(x,d,y) socksstate(x,d,y) |
147 | | #endif |
148 | | |
149 | | /* always use this function to change state, to make debugging easier */ |
150 | | static void socksstate(struct socks_state *sx, struct Curl_easy *data, |
151 | | enum connect_t state |
152 | | #ifdef DEBUG_AND_VERBOSE |
153 | | , int lineno |
154 | | #endif |
155 | | ) |
156 | 4.74k | { |
157 | 4.74k | enum connect_t oldstate = sx->state; |
158 | 4.74k | #ifdef DEBUG_AND_VERBOSE |
159 | | /* synced with the state list in urldata.h */ |
160 | 4.74k | static const char * const socks_statename[] = { |
161 | 4.74k | "INIT", |
162 | 4.74k | "SOCKS_INIT", |
163 | 4.74k | "SOCKS_SEND", |
164 | 4.74k | "SOCKS_READ_INIT", |
165 | 4.74k | "SOCKS_READ", |
166 | 4.74k | "GSSAPI_INIT", |
167 | 4.74k | "AUTH_INIT", |
168 | 4.74k | "AUTH_SEND", |
169 | 4.74k | "AUTH_READ", |
170 | 4.74k | "REQ_INIT", |
171 | 4.74k | "RESOLVING", |
172 | 4.74k | "RESOLVED", |
173 | 4.74k | "RESOLVE_REMOTE", |
174 | 4.74k | "REQ_SEND", |
175 | 4.74k | "REQ_SENDING", |
176 | 4.74k | "REQ_READ", |
177 | 4.74k | "REQ_READ_MORE", |
178 | 4.74k | "DONE" |
179 | 4.74k | }; |
180 | 4.74k | #endif |
181 | | |
182 | 4.74k | (void)data; |
183 | 4.74k | if(oldstate == state) |
184 | | /* do not bother when the new state is the same as the old state */ |
185 | 0 | return; |
186 | | |
187 | 4.74k | sx->state = state; |
188 | | |
189 | 4.74k | #ifdef DEBUG_AND_VERBOSE |
190 | 4.74k | infof(data, |
191 | 4.74k | "SXSTATE: %s => %s; line %d", |
192 | 4.74k | socks_statename[oldstate], socks_statename[sx->state], |
193 | 4.74k | lineno); |
194 | 4.74k | #endif |
195 | 4.74k | } |
196 | | |
197 | | static CURLproxycode socks_state_send(struct Curl_cfilter *cf, |
198 | | struct socks_state *sx, |
199 | | struct Curl_easy *data, |
200 | | CURLproxycode failcode, |
201 | | const char *description) |
202 | 1.47k | { |
203 | 1.47k | size_t nwritten; |
204 | 1.47k | CURLcode result; |
205 | | |
206 | 1.47k | result = Curl_conn_cf_send(cf->next, data, (char *)sx->outp, |
207 | 1.47k | sx->outstanding, FALSE, &nwritten); |
208 | 1.47k | if(result) { |
209 | 0 | if(CURLE_AGAIN == result) |
210 | 0 | return CURLPX_OK; |
211 | | |
212 | 0 | failf(data, "Failed to send %s: %s", description, |
213 | 0 | curl_easy_strerror(result)); |
214 | 0 | return failcode; |
215 | 0 | } |
216 | 1.47k | else if(!nwritten) { |
217 | | /* connection closed */ |
218 | 0 | failf(data, "connection to proxy closed"); |
219 | 0 | return CURLPX_CLOSED; |
220 | 0 | } |
221 | | |
222 | 1.47k | DEBUGASSERT(sx->outstanding >= nwritten); |
223 | | /* not done, remain in state */ |
224 | 1.47k | sx->outstanding -= nwritten; |
225 | 1.47k | sx->outp += nwritten; |
226 | 1.47k | return CURLPX_OK; |
227 | 1.47k | } |
228 | | |
229 | | static CURLproxycode socks_state_recv(struct Curl_cfilter *cf, |
230 | | struct socks_state *sx, |
231 | | struct Curl_easy *data, |
232 | | CURLproxycode failcode, |
233 | | const char *description) |
234 | 2.51k | { |
235 | 2.51k | size_t nread; |
236 | 2.51k | CURLcode result; |
237 | | |
238 | 2.51k | result = Curl_conn_cf_recv(cf->next, data, (char *)sx->outp, |
239 | 2.51k | sx->outstanding, &nread); |
240 | 2.51k | if(result) { |
241 | 753 | if(CURLE_AGAIN == result) |
242 | 753 | return CURLPX_OK; |
243 | | |
244 | 0 | failf(data, "SOCKS: Failed receiving %s: %s", description, |
245 | 0 | curl_easy_strerror(result)); |
246 | 0 | return failcode; |
247 | 753 | } |
248 | 1.76k | else if(!nread) { |
249 | | /* connection closed */ |
250 | 367 | failf(data, "connection to proxy closed"); |
251 | 367 | return CURLPX_CLOSED; |
252 | 367 | } |
253 | | /* remain in reading state */ |
254 | 1.39k | DEBUGASSERT(sx->outstanding >= nread); |
255 | 1.39k | sx->outstanding -= nread; |
256 | 1.39k | sx->outp += nread; |
257 | 1.39k | return CURLPX_OK; |
258 | 1.39k | } |
259 | | |
260 | | /* |
261 | | * This function logs in to a SOCKS4 proxy and sends the specifics to the final |
262 | | * destination server. |
263 | | * |
264 | | * Reference : |
265 | | * https://www.openssh.com/txt/socks4.protocol |
266 | | * |
267 | | * Note : |
268 | | * Set protocol4a=true for "SOCKS 4A (Simple Extension to SOCKS 4 Protocol)" |
269 | | * Nonsupport "Identification Protocol (RFC1413)" |
270 | | */ |
271 | | static CURLproxycode do_SOCKS4(struct Curl_cfilter *cf, |
272 | | struct socks_state *sx, |
273 | | struct Curl_easy *data) |
274 | 321k | { |
275 | 321k | struct connectdata *conn = cf->conn; |
276 | 321k | const bool protocol4a = |
277 | 321k | (conn->socks_proxy.proxytype == CURLPROXY_SOCKS4A); |
278 | 321k | unsigned char *socksreq = sx->buffer; |
279 | 321k | CURLcode result; |
280 | 321k | CURLproxycode presult; |
281 | 321k | struct Curl_dns_entry *dns = NULL; |
282 | | |
283 | 321k | switch(sx->state) { |
284 | 355 | case CONNECT_SOCKS_INIT: |
285 | | /* SOCKS4 can only do IPv4, insist! */ |
286 | 355 | conn->ip_version = CURL_IPRESOLVE_V4; |
287 | 355 | if(conn->bits.httpproxy) |
288 | 62 | infof(data, "SOCKS4%s: connecting to HTTP proxy %s port %d", |
289 | 355 | protocol4a ? "a" : "", sx->hostname, sx->remote_port); |
290 | | |
291 | 355 | infof(data, "SOCKS4 communication to %s:%d", |
292 | 355 | sx->hostname, sx->remote_port); |
293 | | |
294 | | /* |
295 | | * Compose socks4 request |
296 | | * |
297 | | * Request format |
298 | | * |
299 | | * +----+----+----+----+----+----+----+----+----+----+....+----+ |
300 | | * | VN | CD | DSTPORT | DSTIP | USERID |NULL| |
301 | | * +----+----+----+----+----+----+----+----+----+----+....+----+ |
302 | | * # of bytes: 1 1 2 4 variable 1 |
303 | | */ |
304 | | |
305 | 355 | socksreq[0] = 4; /* version (SOCKS4) */ |
306 | 355 | socksreq[1] = 1; /* connect */ |
307 | 355 | socksreq[2] = (unsigned char)((sx->remote_port >> 8) & 0xff); /* MSB */ |
308 | 355 | socksreq[3] = (unsigned char)(sx->remote_port & 0xff); /* LSB */ |
309 | | |
310 | | /* DNS resolve only for SOCKS4, not SOCKS4a */ |
311 | 355 | if(!protocol4a) { |
312 | 190 | result = Curl_resolv(data, sx->hostname, sx->remote_port, |
313 | 190 | cf->conn->ip_version, TRUE, &dns); |
314 | | |
315 | 190 | if(result == CURLE_AGAIN) { |
316 | 59 | sxstate(sx, data, CONNECT_RESOLVING); |
317 | 59 | infof(data, "SOCKS4 non-blocking resolve of %s", sx->hostname); |
318 | 59 | return CURLPX_OK; |
319 | 59 | } |
320 | 131 | else if(result) |
321 | 2 | return CURLPX_RESOLVE_HOST; |
322 | 129 | sxstate(sx, data, CONNECT_RESOLVED); |
323 | 129 | goto CONNECT_RESOLVED; |
324 | 190 | } |
325 | | |
326 | | /* socks4a does not resolve anything locally */ |
327 | 165 | sxstate(sx, data, CONNECT_REQ_INIT); |
328 | 165 | goto CONNECT_REQ_INIT; |
329 | | |
330 | 320k | case CONNECT_RESOLVING: |
331 | | /* check if we have the name resolved by now */ |
332 | 320k | result = Curl_resolv_check(data, &dns); |
333 | 320k | if(!dns) { |
334 | 320k | if(result) |
335 | 43 | return CURLPX_RESOLVE_HOST; |
336 | 320k | return CURLPX_OK; |
337 | 320k | } |
338 | 1 | FALLTHROUGH(); |
339 | 1 | case CONNECT_RESOLVED: |
340 | 130 | CONNECT_RESOLVED: |
341 | 130 | { |
342 | 130 | struct Curl_addrinfo *hp = NULL; |
343 | | /* |
344 | | * We cannot use 'hostent' as a struct that Curl_resolv() returns. It |
345 | | * returns a Curl_addrinfo pointer that may not always look the same. |
346 | | */ |
347 | 130 | if(dns) { |
348 | 130 | hp = dns->addr; |
349 | | |
350 | | /* scan for the first IPv4 address */ |
351 | 131 | while(hp && (hp->ai_family != AF_INET)) |
352 | 1 | hp = hp->ai_next; |
353 | | |
354 | 130 | if(hp) { |
355 | 129 | struct sockaddr_in *saddr_in; |
356 | 129 | char buf[64]; |
357 | 129 | Curl_printable_address(hp, buf, sizeof(buf)); |
358 | | |
359 | 129 | saddr_in = (struct sockaddr_in *)(void *)hp->ai_addr; |
360 | 129 | socksreq[4] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[0]; |
361 | 129 | socksreq[5] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[1]; |
362 | 129 | socksreq[6] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[2]; |
363 | 129 | socksreq[7] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[3]; |
364 | | |
365 | 129 | infof(data, "SOCKS4 connect to IPv4 %s (locally resolved)", buf); |
366 | | |
367 | 129 | Curl_resolv_unlink(data, &dns); /* not used anymore from now on */ |
368 | 129 | } |
369 | 1 | else |
370 | 1 | failf(data, "SOCKS4 connection to %s not supported", sx->hostname); |
371 | 130 | } |
372 | 0 | else |
373 | 0 | failf(data, "Failed to resolve \"%s\" for SOCKS4 connect.", |
374 | 0 | sx->hostname); |
375 | | |
376 | 130 | if(!hp) |
377 | 1 | return CURLPX_RESOLVE_HOST; |
378 | 130 | } |
379 | 129 | FALLTHROUGH(); |
380 | 129 | case CONNECT_REQ_INIT: |
381 | 294 | CONNECT_REQ_INIT: |
382 | | /* |
383 | | * This is currently not supporting "Identification Protocol (RFC1413)". |
384 | | */ |
385 | 294 | socksreq[8] = 0; /* ensure empty userid is null-terminated */ |
386 | 294 | if(sx->proxy_user) { |
387 | 126 | size_t plen = strlen(sx->proxy_user); |
388 | 126 | if(plen > 255) { |
389 | | /* there is no real size limit to this field in the protocol, but |
390 | | SOCKS5 limits the proxy user field to 255 bytes and it seems likely |
391 | | that a longer field is either a mistake or malicious input */ |
392 | 11 | failf(data, "Too long SOCKS proxy username"); |
393 | 11 | return CURLPX_LONG_USER; |
394 | 11 | } |
395 | | /* copy the proxy name WITH trailing zero */ |
396 | 115 | memcpy(socksreq + 8, sx->proxy_user, plen + 1); |
397 | 115 | } |
398 | | |
399 | | /* |
400 | | * Make connection |
401 | | */ |
402 | 283 | { |
403 | 283 | size_t packetsize = 9 + |
404 | 283 | strlen((char *)socksreq + 8); /* size including NUL */ |
405 | | |
406 | | /* If SOCKS4a, set special invalid IP address 0.0.0.x */ |
407 | 283 | if(protocol4a) { |
408 | 156 | size_t hostnamelen = 0; |
409 | 156 | socksreq[4] = 0; |
410 | 156 | socksreq[5] = 0; |
411 | 156 | socksreq[6] = 0; |
412 | 156 | socksreq[7] = 1; |
413 | | /* append hostname */ |
414 | 156 | hostnamelen = strlen(sx->hostname) + 1; /* length including NUL */ |
415 | 156 | if((hostnamelen <= 255) && |
416 | 156 | (packetsize + hostnamelen < sizeof(sx->buffer))) |
417 | 156 | strcpy((char *)socksreq + packetsize, sx->hostname); |
418 | 0 | else { |
419 | 0 | failf(data, "SOCKS4: too long hostname"); |
420 | 0 | return CURLPX_LONG_HOSTNAME; |
421 | 0 | } |
422 | 156 | packetsize += hostnamelen; |
423 | 156 | } |
424 | 283 | sx->outp = socksreq; |
425 | 283 | DEBUGASSERT(packetsize <= sizeof(sx->buffer)); |
426 | 283 | sx->outstanding = packetsize; |
427 | 283 | sxstate(sx, data, CONNECT_REQ_SENDING); |
428 | 283 | } |
429 | 283 | FALLTHROUGH(); |
430 | 283 | case CONNECT_REQ_SENDING: |
431 | | /* Send request */ |
432 | 283 | presult = socks_state_send(cf, sx, data, CURLPX_SEND_CONNECT, |
433 | 283 | "SOCKS4 connect request"); |
434 | 283 | if(CURLPX_OK != presult) |
435 | 0 | return presult; |
436 | 283 | else if(sx->outstanding) { |
437 | | /* remain in sending state */ |
438 | 0 | return CURLPX_OK; |
439 | 0 | } |
440 | | /* done sending! */ |
441 | 283 | sx->outstanding = 8; /* receive data size */ |
442 | 283 | sx->outp = socksreq; |
443 | 283 | sxstate(sx, data, CONNECT_SOCKS_READ); |
444 | | |
445 | 283 | FALLTHROUGH(); |
446 | 518 | case CONNECT_SOCKS_READ: |
447 | | /* Receive response */ |
448 | 518 | presult = socks_state_recv(cf, sx, data, CURLPX_RECV_CONNECT, |
449 | 518 | "connect request ack"); |
450 | 518 | if(CURLPX_OK != presult) |
451 | 133 | return presult; |
452 | 385 | else if(sx->outstanding) { |
453 | | /* remain in reading state */ |
454 | 252 | return CURLPX_OK; |
455 | 252 | } |
456 | 133 | sxstate(sx, data, CONNECT_DONE); |
457 | 133 | break; |
458 | 0 | default: /* lots of unused states in SOCKS4 */ |
459 | 0 | break; |
460 | 321k | } |
461 | | |
462 | | /* |
463 | | * Response format |
464 | | * |
465 | | * +----+----+----+----+----+----+----+----+ |
466 | | * | VN | CD | DSTPORT | DSTIP | |
467 | | * +----+----+----+----+----+----+----+----+ |
468 | | * # of bytes: 1 1 2 4 |
469 | | * |
470 | | * VN is the version of the reply code and should be 0. CD is the result |
471 | | * code with one of the following values: |
472 | | * |
473 | | * 90: request granted |
474 | | * 91: request rejected or failed |
475 | | * 92: request rejected because SOCKS server cannot connect to |
476 | | * identd on the client |
477 | | * 93: request rejected because the client program and identd |
478 | | * report different user-ids |
479 | | */ |
480 | | |
481 | | /* wrong version ? */ |
482 | 133 | if(socksreq[0]) { |
483 | 17 | failf(data, |
484 | 17 | "SOCKS4 reply has wrong version, version should be 0."); |
485 | 17 | return CURLPX_BAD_VERSION; |
486 | 17 | } |
487 | | |
488 | | /* Result */ |
489 | 116 | switch(socksreq[1]) { |
490 | 67 | case 90: |
491 | 67 | infof(data, "SOCKS4%s request granted.", protocol4a ? "a" : ""); |
492 | 67 | break; |
493 | 12 | case 91: |
494 | 12 | failf(data, |
495 | 12 | "cannot complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)" |
496 | 12 | ", request rejected or failed.", |
497 | 12 | socksreq[4], socksreq[5], socksreq[6], socksreq[7], |
498 | 12 | (((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]), |
499 | 12 | (unsigned char)socksreq[1]); |
500 | 12 | return CURLPX_REQUEST_FAILED; |
501 | 12 | case 92: |
502 | 12 | failf(data, |
503 | 12 | "cannot complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)" |
504 | 12 | ", request rejected because SOCKS server cannot connect to " |
505 | 12 | "identd on the client.", |
506 | 12 | socksreq[4], socksreq[5], socksreq[6], socksreq[7], |
507 | 12 | (((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]), |
508 | 12 | (unsigned char)socksreq[1]); |
509 | 12 | return CURLPX_IDENTD; |
510 | 12 | case 93: |
511 | 12 | failf(data, |
512 | 12 | "cannot complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)" |
513 | 12 | ", request rejected because the client program and identd " |
514 | 12 | "report different user-ids.", |
515 | 12 | socksreq[4], socksreq[5], socksreq[6], socksreq[7], |
516 | 12 | (((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]), |
517 | 12 | (unsigned char)socksreq[1]); |
518 | 12 | return CURLPX_IDENTD_DIFFER; |
519 | 13 | default: |
520 | 13 | failf(data, |
521 | 13 | "cannot complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)" |
522 | 13 | ", Unknown.", |
523 | 13 | socksreq[4], socksreq[5], socksreq[6], socksreq[7], |
524 | 13 | (((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]), |
525 | 13 | (unsigned char)socksreq[1]); |
526 | 13 | return CURLPX_UNKNOWN_FAIL; |
527 | 116 | } |
528 | | |
529 | 67 | return CURLPX_OK; /* Proxy was successful! */ |
530 | 116 | } |
531 | | |
532 | | /* |
533 | | * This function logs in to a SOCKS5 proxy and sends the specifics to the final |
534 | | * destination server. |
535 | | */ |
536 | | static CURLproxycode do_SOCKS5(struct Curl_cfilter *cf, |
537 | | struct socks_state *sx, |
538 | | struct Curl_easy *data) |
539 | 772k | { |
540 | | /* |
541 | | According to the RFC1928, section "6. Replies". This is what a SOCK5 |
542 | | replies: |
543 | | |
544 | | +----+-----+-------+------+----------+----------+ |
545 | | |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT | |
546 | | +----+-----+-------+------+----------+----------+ |
547 | | | 1 | 1 | X'00' | 1 | Variable | 2 | |
548 | | +----+-----+-------+------+----------+----------+ |
549 | | |
550 | | Where: |
551 | | |
552 | | o VER protocol version: X'05' |
553 | | o REP Reply field: |
554 | | o X'00' succeeded |
555 | | */ |
556 | 772k | struct connectdata *conn = cf->conn; |
557 | 772k | unsigned char *socksreq = sx->buffer; |
558 | 772k | size_t idx; |
559 | 772k | CURLcode result; |
560 | 772k | CURLproxycode presult; |
561 | 772k | bool socks5_resolve_local = |
562 | 772k | (conn->socks_proxy.proxytype == CURLPROXY_SOCKS5); |
563 | 772k | const size_t hostname_len = strlen(sx->hostname); |
564 | 772k | size_t len = 0; |
565 | 772k | const unsigned char auth = data->set.socks5auth; |
566 | 772k | bool allow_gssapi = FALSE; |
567 | 772k | struct Curl_dns_entry *dns = NULL; |
568 | | |
569 | 772k | switch(sx->state) { |
570 | 691 | case CONNECT_SOCKS_INIT: |
571 | 691 | if(conn->bits.httpproxy) |
572 | 62 | infof(data, "SOCKS5: connecting to HTTP proxy %s port %d", |
573 | 691 | sx->hostname, sx->remote_port); |
574 | | |
575 | | /* RFC1928 chapter 5 specifies max 255 chars for domain name in packet */ |
576 | 691 | if(!socks5_resolve_local && hostname_len > 255) { |
577 | 0 | failf(data, "SOCKS5: the destination hostname is too long to be " |
578 | 0 | "resolved remotely by the proxy."); |
579 | 0 | return CURLPX_LONG_HOSTNAME; |
580 | 0 | } |
581 | | |
582 | 691 | if(auth & ~(CURLAUTH_BASIC | CURLAUTH_GSSAPI)) |
583 | 0 | infof(data, |
584 | 691 | "warning: unsupported value passed to CURLOPT_SOCKS5_AUTH: %u", |
585 | 691 | auth); |
586 | 691 | if(!(auth & CURLAUTH_BASIC)) |
587 | | /* disable username/password auth */ |
588 | 12 | sx->proxy_user = NULL; |
589 | | #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI) |
590 | | if(auth & CURLAUTH_GSSAPI) |
591 | | allow_gssapi = TRUE; |
592 | | #endif |
593 | | |
594 | 691 | idx = 0; |
595 | 691 | socksreq[idx++] = 5; /* version */ |
596 | 691 | idx++; /* number of authentication methods */ |
597 | 691 | socksreq[idx++] = 0; /* no authentication */ |
598 | 691 | if(allow_gssapi) |
599 | 0 | socksreq[idx++] = 1; /* GSS-API */ |
600 | 691 | if(sx->proxy_user) |
601 | 291 | socksreq[idx++] = 2; /* username/password */ |
602 | | /* write the number of authentication methods */ |
603 | 691 | socksreq[1] = (unsigned char) (idx - 2); |
604 | | |
605 | 691 | sx->outp = socksreq; |
606 | 691 | DEBUGASSERT(idx <= sizeof(sx->buffer)); |
607 | 691 | sx->outstanding = idx; |
608 | 691 | presult = socks_state_send(cf, sx, data, CURLPX_SEND_CONNECT, |
609 | 691 | "initial SOCKS5 request"); |
610 | 691 | if(CURLPX_OK != presult) |
611 | 0 | return presult; |
612 | 691 | else if(sx->outstanding) { |
613 | | /* remain in sending state */ |
614 | 0 | return CURLPX_OK; |
615 | 0 | } |
616 | 691 | sxstate(sx, data, CONNECT_SOCKS_READ); |
617 | 691 | goto CONNECT_SOCKS_READ_INIT; |
618 | 0 | case CONNECT_SOCKS_SEND: |
619 | 0 | presult = socks_state_send(cf, sx, data, CURLPX_SEND_CONNECT, |
620 | 0 | "initial SOCKS5 request"); |
621 | 0 | if(CURLPX_OK != presult) |
622 | 0 | return presult; |
623 | 0 | else if(sx->outstanding) { |
624 | | /* remain in sending state */ |
625 | 0 | return CURLPX_OK; |
626 | 0 | } |
627 | 0 | FALLTHROUGH(); |
628 | 0 | case CONNECT_SOCKS_READ_INIT: |
629 | 691 | CONNECT_SOCKS_READ_INIT: |
630 | 691 | sx->outstanding = 2; /* expect two bytes */ |
631 | 691 | sx->outp = socksreq; /* store it here */ |
632 | 691 | FALLTHROUGH(); |
633 | 1.21k | case CONNECT_SOCKS_READ: |
634 | 1.21k | presult = socks_state_recv(cf, sx, data, CURLPX_RECV_CONNECT, |
635 | 1.21k | "initial SOCKS5 response"); |
636 | 1.21k | if(CURLPX_OK != presult) |
637 | 107 | return presult; |
638 | 1.11k | else if(sx->outstanding) { |
639 | | /* remain in reading state */ |
640 | 540 | return CURLPX_OK; |
641 | 540 | } |
642 | 572 | else if(socksreq[0] != 5) { |
643 | 58 | failf(data, "Received invalid version in initial SOCKS5 response."); |
644 | 58 | return CURLPX_BAD_VERSION; |
645 | 58 | } |
646 | 514 | else if(socksreq[1] == 0) { |
647 | | /* DONE! No authentication needed. Send request. */ |
648 | 212 | sxstate(sx, data, CONNECT_REQ_INIT); |
649 | 212 | goto CONNECT_REQ_INIT; |
650 | 212 | } |
651 | 302 | else if(socksreq[1] == 2) { |
652 | | /* regular name + password authentication */ |
653 | 245 | sxstate(sx, data, CONNECT_AUTH_INIT); |
654 | 245 | goto CONNECT_AUTH_INIT; |
655 | 245 | } |
656 | | #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI) |
657 | | else if(allow_gssapi && (socksreq[1] == 1)) { |
658 | | sxstate(sx, data, CONNECT_GSSAPI_INIT); |
659 | | result = Curl_SOCKS5_gssapi_negotiate(cf, data); |
660 | | if(result) { |
661 | | failf(data, "Unable to negotiate SOCKS5 GSS-API context."); |
662 | | return CURLPX_GSSAPI; |
663 | | } |
664 | | } |
665 | | #endif |
666 | 57 | else { |
667 | | /* error */ |
668 | 57 | if(!allow_gssapi && (socksreq[1] == 1)) { |
669 | 11 | failf(data, |
670 | 11 | "SOCKS5 GSSAPI per-message authentication is not supported."); |
671 | 11 | return CURLPX_GSSAPI_PERMSG; |
672 | 11 | } |
673 | 46 | else if(socksreq[1] == 255) { |
674 | 12 | failf(data, "No authentication method was acceptable."); |
675 | 12 | return CURLPX_NO_AUTH; |
676 | 12 | } |
677 | 57 | } |
678 | 34 | failf(data, |
679 | 34 | "Undocumented SOCKS5 mode attempted to be used by server."); |
680 | 34 | return CURLPX_UNKNOWN_MODE; |
681 | | #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI) |
682 | | case CONNECT_GSSAPI_INIT: |
683 | | /* GSSAPI stuff done non-blocking */ |
684 | | break; |
685 | | #endif |
686 | | |
687 | 0 | default: /* do nothing! */ |
688 | 0 | break; |
689 | | |
690 | 245 | CONNECT_AUTH_INIT: |
691 | 245 | case CONNECT_AUTH_INIT: { |
692 | | /* Needs username and password */ |
693 | 245 | size_t proxy_user_len, proxy_password_len; |
694 | 245 | if(sx->proxy_user && sx->proxy_password) { |
695 | 175 | proxy_user_len = strlen(sx->proxy_user); |
696 | 175 | proxy_password_len = strlen(sx->proxy_password); |
697 | 175 | } |
698 | 70 | else { |
699 | 70 | proxy_user_len = 0; |
700 | 70 | proxy_password_len = 0; |
701 | 70 | } |
702 | | |
703 | | /* username/password request looks like |
704 | | * +----+------+----------+------+----------+ |
705 | | * |VER | ULEN | UNAME | PLEN | PASSWD | |
706 | | * +----+------+----------+------+----------+ |
707 | | * | 1 | 1 | 1 to 255 | 1 | 1 to 255 | |
708 | | * +----+------+----------+------+----------+ |
709 | | */ |
710 | 245 | len = 0; |
711 | 245 | socksreq[len++] = 1; /* username/pw subnegotiation version */ |
712 | 245 | socksreq[len++] = (unsigned char) proxy_user_len; |
713 | 245 | if(sx->proxy_user && proxy_user_len) { |
714 | | /* the length must fit in a single byte */ |
715 | 148 | if(proxy_user_len > 255) { |
716 | 20 | failf(data, "Excessive username length for proxy auth"); |
717 | 20 | return CURLPX_LONG_USER; |
718 | 20 | } |
719 | 128 | memcpy(socksreq + len, sx->proxy_user, proxy_user_len); |
720 | 128 | } |
721 | 225 | len += proxy_user_len; |
722 | 225 | socksreq[len++] = (unsigned char) proxy_password_len; |
723 | 225 | if(sx->proxy_password && proxy_password_len) { |
724 | | /* the length must fit in a single byte */ |
725 | 79 | if(proxy_password_len > 255) { |
726 | 14 | failf(data, "Excessive password length for proxy auth"); |
727 | 14 | return CURLPX_LONG_PASSWD; |
728 | 14 | } |
729 | 65 | memcpy(socksreq + len, sx->proxy_password, proxy_password_len); |
730 | 65 | } |
731 | 211 | len += proxy_password_len; |
732 | 211 | sxstate(sx, data, CONNECT_AUTH_SEND); |
733 | 211 | DEBUGASSERT(len <= sizeof(sx->buffer)); |
734 | 211 | sx->outstanding = len; |
735 | 211 | sx->outp = socksreq; |
736 | 211 | } |
737 | 211 | FALLTHROUGH(); |
738 | 211 | case CONNECT_AUTH_SEND: |
739 | 211 | presult = socks_state_send(cf, sx, data, CURLPX_SEND_AUTH, |
740 | 211 | "SOCKS5 sub-negotiation request"); |
741 | 211 | if(CURLPX_OK != presult) |
742 | 0 | return presult; |
743 | 211 | else if(sx->outstanding) { |
744 | | /* remain in sending state */ |
745 | 0 | return CURLPX_OK; |
746 | 0 | } |
747 | 211 | sx->outp = socksreq; |
748 | 211 | sx->outstanding = 2; |
749 | 211 | sxstate(sx, data, CONNECT_AUTH_READ); |
750 | 211 | FALLTHROUGH(); |
751 | 245 | case CONNECT_AUTH_READ: |
752 | 245 | presult = socks_state_recv(cf, sx, data, CURLPX_RECV_AUTH, |
753 | 245 | "SOCKS5 sub-negotiation response"); |
754 | 245 | if(CURLPX_OK != presult) |
755 | 23 | return presult; |
756 | 222 | else if(sx->outstanding) { |
757 | | /* remain in reading state */ |
758 | 38 | return CURLPX_OK; |
759 | 38 | } |
760 | | /* ignore the first (VER) byte */ |
761 | 184 | else if(socksreq[1]) { /* status */ |
762 | 49 | failf(data, "User was rejected by the SOCKS5 server (%d %d).", |
763 | 49 | socksreq[0], socksreq[1]); |
764 | 49 | return CURLPX_USER_REJECTED; |
765 | 49 | } |
766 | | |
767 | | /* Everything is good so far, user was authenticated! */ |
768 | 135 | sxstate(sx, data, CONNECT_REQ_INIT); |
769 | 135 | FALLTHROUGH(); |
770 | 135 | case CONNECT_REQ_INIT: |
771 | 347 | CONNECT_REQ_INIT: |
772 | 347 | if(socks5_resolve_local) { |
773 | 217 | result = Curl_resolv(data, sx->hostname, sx->remote_port, |
774 | 217 | cf->conn->ip_version, TRUE, &dns); |
775 | | |
776 | 217 | if(result == CURLE_AGAIN) { |
777 | 52 | sxstate(sx, data, CONNECT_RESOLVING); |
778 | 52 | return CURLPX_OK; |
779 | 52 | } |
780 | 165 | else if(result) |
781 | 2 | return CURLPX_RESOLVE_HOST; |
782 | 163 | sxstate(sx, data, CONNECT_RESOLVED); |
783 | 163 | goto CONNECT_RESOLVED; |
784 | 217 | } |
785 | 130 | goto CONNECT_RESOLVE_REMOTE; |
786 | | |
787 | 770k | case CONNECT_RESOLVING: |
788 | | /* check if we have the name resolved by now */ |
789 | 770k | result = Curl_resolv_check(data, &dns); |
790 | 770k | if(!dns) { |
791 | 770k | if(result) |
792 | 20 | return CURLPX_RESOLVE_HOST; |
793 | 770k | return CURLPX_OK; |
794 | 770k | } |
795 | 2 | FALLTHROUGH(); |
796 | 2 | case CONNECT_RESOLVED: |
797 | 165 | CONNECT_RESOLVED: |
798 | 165 | { |
799 | 165 | char dest[MAX_IPADR_LEN]; /* printable address */ |
800 | 165 | struct Curl_addrinfo *hp = NULL; |
801 | 165 | if(dns) |
802 | 165 | hp = dns->addr; |
803 | 165 | #ifdef USE_IPV6 |
804 | 165 | if(data->set.ipver != CURL_IPRESOLVE_WHATEVER) { |
805 | 5 | int wanted_family = data->set.ipver == CURL_IPRESOLVE_V4 ? |
806 | 4 | AF_INET : AF_INET6; |
807 | | /* scan for the first proper address */ |
808 | 6 | while(hp && (hp->ai_family != wanted_family)) |
809 | 1 | hp = hp->ai_next; |
810 | 5 | } |
811 | 165 | #endif |
812 | 165 | if(!hp) { |
813 | 1 | failf(data, "Failed to resolve \"%s\" for SOCKS5 connect.", |
814 | 1 | sx->hostname); |
815 | 1 | return CURLPX_RESOLVE_HOST; |
816 | 1 | } |
817 | | |
818 | 164 | Curl_printable_address(hp, dest, sizeof(dest)); |
819 | | |
820 | 164 | len = 0; |
821 | 164 | socksreq[len++] = 5; /* version (SOCKS5) */ |
822 | 164 | socksreq[len++] = 1; /* connect */ |
823 | 164 | socksreq[len++] = 0; /* must be zero */ |
824 | 164 | if(hp->ai_family == AF_INET) { |
825 | 164 | int i; |
826 | 164 | struct sockaddr_in *saddr_in; |
827 | 164 | socksreq[len++] = 1; /* ATYP: IPv4 = 1 */ |
828 | | |
829 | 164 | saddr_in = (struct sockaddr_in *)(void *)hp->ai_addr; |
830 | 820 | for(i = 0; i < 4; i++) { |
831 | 656 | socksreq[len++] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[i]; |
832 | 656 | } |
833 | | |
834 | 164 | infof(data, "SOCKS5 connect to %s:%d (locally resolved)", dest, |
835 | 164 | sx->remote_port); |
836 | 164 | } |
837 | 0 | #ifdef USE_IPV6 |
838 | 0 | else if(hp->ai_family == AF_INET6) { |
839 | 0 | int i; |
840 | 0 | struct sockaddr_in6 *saddr_in6; |
841 | 0 | socksreq[len++] = 4; /* ATYP: IPv6 = 4 */ |
842 | |
|
843 | 0 | saddr_in6 = (struct sockaddr_in6 *)(void *)hp->ai_addr; |
844 | 0 | for(i = 0; i < 16; i++) { |
845 | 0 | socksreq[len++] = |
846 | 0 | ((unsigned char *)&saddr_in6->sin6_addr.s6_addr)[i]; |
847 | 0 | } |
848 | |
|
849 | 0 | infof(data, "SOCKS5 connect to [%s]:%d (locally resolved)", dest, |
850 | 0 | sx->remote_port); |
851 | 0 | } |
852 | 0 | #endif |
853 | 0 | else { |
854 | 0 | hp = NULL; /* fail! */ |
855 | 0 | failf(data, "SOCKS5 connection to %s not supported", dest); |
856 | 0 | } |
857 | | |
858 | 164 | Curl_resolv_unlink(data, &dns); /* not used anymore from now on */ |
859 | 164 | goto CONNECT_REQ_SEND; |
860 | 165 | } |
861 | 130 | CONNECT_RESOLVE_REMOTE: |
862 | 130 | case CONNECT_RESOLVE_REMOTE: |
863 | | /* Authentication is complete, now specify destination to the proxy */ |
864 | 130 | len = 0; |
865 | 130 | socksreq[len++] = 5; /* version (SOCKS5) */ |
866 | 130 | socksreq[len++] = 1; /* connect */ |
867 | 130 | socksreq[len++] = 0; /* must be zero */ |
868 | | |
869 | 130 | if(!socks5_resolve_local) { |
870 | | /* ATYP: domain name = 3, |
871 | | IPv6 == 4, |
872 | | IPv4 == 1 */ |
873 | 130 | unsigned char ip4[4]; |
874 | 130 | #ifdef USE_IPV6 |
875 | 130 | if(conn->bits.ipv6_ip) { |
876 | 2 | char ip6[16]; |
877 | 2 | if(1 != curlx_inet_pton(AF_INET6, sx->hostname, ip6)) |
878 | 2 | return CURLPX_BAD_ADDRESS_TYPE; |
879 | 0 | socksreq[len++] = 4; |
880 | 0 | memcpy(&socksreq[len], ip6, sizeof(ip6)); |
881 | 0 | len += sizeof(ip6); |
882 | 0 | } |
883 | 128 | else |
884 | 128 | #endif |
885 | 128 | if(1 == curlx_inet_pton(AF_INET, sx->hostname, ip4)) { |
886 | 126 | socksreq[len++] = 1; |
887 | 126 | memcpy(&socksreq[len], ip4, sizeof(ip4)); |
888 | 126 | len += sizeof(ip4); |
889 | 126 | } |
890 | 2 | else { |
891 | 2 | socksreq[len++] = 3; |
892 | 2 | socksreq[len++] = (unsigned char) hostname_len; /* one byte length */ |
893 | 2 | memcpy(&socksreq[len], sx->hostname, hostname_len); /* w/o NULL */ |
894 | 2 | len += hostname_len; |
895 | 2 | } |
896 | 128 | infof(data, "SOCKS5 connect to %s:%d (remotely resolved)", |
897 | 128 | sx->hostname, sx->remote_port); |
898 | 128 | } |
899 | 128 | FALLTHROUGH(); |
900 | | |
901 | 128 | case CONNECT_REQ_SEND: |
902 | 292 | CONNECT_REQ_SEND: |
903 | | /* PORT MSB */ |
904 | 292 | socksreq[len++] = (unsigned char)((sx->remote_port >> 8) & 0xff); |
905 | | /* PORT LSB */ |
906 | 292 | socksreq[len++] = (unsigned char)(sx->remote_port & 0xff); |
907 | | |
908 | | #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI) |
909 | | if(conn->socks5_gssapi_enctype) { |
910 | | failf(data, "SOCKS5 GSS-API protection not yet implemented."); |
911 | | return CURLPX_GSSAPI_PROTECTION; |
912 | | } |
913 | | #endif |
914 | 292 | sx->outp = socksreq; |
915 | 292 | DEBUGASSERT(len <= sizeof(sx->buffer)); |
916 | 292 | sx->outstanding = len; |
917 | 292 | sxstate(sx, data, CONNECT_REQ_SENDING); |
918 | 292 | FALLTHROUGH(); |
919 | 292 | case CONNECT_REQ_SENDING: |
920 | 292 | presult = socks_state_send(cf, sx, data, CURLPX_SEND_REQUEST, |
921 | 292 | "SOCKS5 connect request"); |
922 | 292 | if(CURLPX_OK != presult) |
923 | 0 | return presult; |
924 | 292 | else if(sx->outstanding) { |
925 | | /* remain in send state */ |
926 | 0 | return CURLPX_OK; |
927 | 0 | } |
928 | | #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI) |
929 | | if(conn->socks5_gssapi_enctype) { |
930 | | failf(data, "SOCKS5 GSS-API protection not yet implemented."); |
931 | | return CURLPX_GSSAPI_PROTECTION; |
932 | | } |
933 | | #endif |
934 | 292 | sx->outstanding = 10; /* minimum packet size is 10 */ |
935 | 292 | sx->outp = socksreq; |
936 | 292 | sxstate(sx, data, CONNECT_REQ_READ); |
937 | 292 | FALLTHROUGH(); |
938 | 395 | case CONNECT_REQ_READ: |
939 | 395 | presult = socks_state_recv(cf, sx, data, CURLPX_RECV_REQACK, |
940 | 395 | "SOCKS5 connect request ack"); |
941 | 395 | if(CURLPX_OK != presult) |
942 | 73 | return presult; |
943 | 322 | else if(sx->outstanding) { |
944 | | /* remain in reading state */ |
945 | 112 | return CURLPX_OK; |
946 | 112 | } |
947 | 210 | else if(socksreq[0] != 5) { /* version */ |
948 | 39 | failf(data, |
949 | 39 | "SOCKS5 reply has wrong version, version should be 5."); |
950 | 39 | return CURLPX_BAD_VERSION; |
951 | 39 | } |
952 | 171 | else if(socksreq[1]) { /* Anything besides 0 is an error */ |
953 | 34 | CURLproxycode rc = CURLPX_REPLY_UNASSIGNED; |
954 | 34 | int code = socksreq[1]; |
955 | 34 | failf(data, "cannot complete SOCKS5 connection to %s. (%d)", |
956 | 34 | sx->hostname, (unsigned char)socksreq[1]); |
957 | 34 | if(code < 9) { |
958 | | /* RFC 1928 section 6 lists: */ |
959 | 15 | static const CURLproxycode lookup[] = { |
960 | 15 | CURLPX_OK, |
961 | 15 | CURLPX_REPLY_GENERAL_SERVER_FAILURE, |
962 | 15 | CURLPX_REPLY_NOT_ALLOWED, |
963 | 15 | CURLPX_REPLY_NETWORK_UNREACHABLE, |
964 | 15 | CURLPX_REPLY_HOST_UNREACHABLE, |
965 | 15 | CURLPX_REPLY_CONNECTION_REFUSED, |
966 | 15 | CURLPX_REPLY_TTL_EXPIRED, |
967 | 15 | CURLPX_REPLY_COMMAND_NOT_SUPPORTED, |
968 | 15 | CURLPX_REPLY_ADDRESS_TYPE_NOT_SUPPORTED, |
969 | 15 | }; |
970 | 15 | rc = lookup[code]; |
971 | 15 | } |
972 | 34 | return rc; |
973 | 34 | } |
974 | | |
975 | | /* Fix: in general, returned BND.ADDR is variable length parameter by RFC |
976 | | 1928, so the reply packet should be read until the end to avoid errors |
977 | | at subsequent protocol level. |
978 | | |
979 | | +----+-----+-------+------+----------+----------+ |
980 | | |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT | |
981 | | +----+-----+-------+------+----------+----------+ |
982 | | | 1 | 1 | X'00' | 1 | Variable | 2 | |
983 | | +----+-----+-------+------+----------+----------+ |
984 | | |
985 | | ATYP: |
986 | | o IP v4 address: X'01', BND.ADDR = 4 byte |
987 | | o domain name: X'03', BND.ADDR = [ 1 byte length, string ] |
988 | | o IP v6 address: X'04', BND.ADDR = 16 byte |
989 | | */ |
990 | | |
991 | | /* Calculate real packet size */ |
992 | 137 | if(socksreq[3] == 3) { |
993 | | /* domain name */ |
994 | 56 | int addrlen = (int) socksreq[4]; |
995 | 56 | len = 5 + addrlen + 2; |
996 | 56 | } |
997 | 81 | else if(socksreq[3] == 4) { |
998 | | /* IPv6 */ |
999 | 26 | len = 4 + 16 + 2; |
1000 | 26 | } |
1001 | 55 | else if(socksreq[3] == 1) { |
1002 | 26 | len = 4 + 4 + 2; |
1003 | 26 | } |
1004 | 29 | else { |
1005 | 29 | failf(data, "SOCKS5 reply has wrong address type."); |
1006 | 29 | return CURLPX_BAD_ADDRESS_TYPE; |
1007 | 29 | } |
1008 | | |
1009 | | /* At this point we already read first 10 bytes */ |
1010 | | #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI) |
1011 | | if(!conn->socks5_gssapi_enctype) { |
1012 | | /* decrypt_gssapi_blockread already read the whole packet */ |
1013 | | #endif |
1014 | 108 | if(len > 10) { |
1015 | 76 | DEBUGASSERT(len <= sizeof(sx->buffer)); |
1016 | 76 | sx->outstanding = len - 10; /* get the rest */ |
1017 | 76 | sx->outp = &socksreq[10]; |
1018 | 76 | sxstate(sx, data, CONNECT_REQ_READ_MORE); |
1019 | 76 | } |
1020 | 32 | else { |
1021 | 32 | sxstate(sx, data, CONNECT_DONE); |
1022 | 32 | break; |
1023 | 32 | } |
1024 | | #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI) |
1025 | | } |
1026 | | #endif |
1027 | 76 | FALLTHROUGH(); |
1028 | 141 | case CONNECT_REQ_READ_MORE: |
1029 | 141 | presult = socks_state_recv(cf, sx, data, CURLPX_RECV_ADDRESS, |
1030 | 141 | "SOCKS5 connect request address"); |
1031 | 141 | if(CURLPX_OK != presult) |
1032 | 31 | return presult; |
1033 | 110 | else if(sx->outstanding) { |
1034 | | /* remain in reading state */ |
1035 | 72 | return CURLPX_OK; |
1036 | 72 | } |
1037 | 772k | sxstate(sx, data, CONNECT_DONE); |
1038 | 772k | } |
1039 | 70 | infof(data, "SOCKS5 request granted."); |
1040 | | |
1041 | 70 | return CURLPX_OK; /* Proxy was successful! */ |
1042 | 772k | } |
1043 | | |
1044 | | static CURLcode connect_SOCKS(struct Curl_cfilter *cf, |
1045 | | struct socks_state *sxstate, |
1046 | | struct Curl_easy *data) |
1047 | 1.09M | { |
1048 | 1.09M | CURLcode result = CURLE_OK; |
1049 | 1.09M | CURLproxycode pxresult = CURLPX_OK; |
1050 | 1.09M | struct connectdata *conn = cf->conn; |
1051 | | |
1052 | 1.09M | switch(conn->socks_proxy.proxytype) { |
1053 | 771k | case CURLPROXY_SOCKS5: |
1054 | 772k | case CURLPROXY_SOCKS5_HOSTNAME: |
1055 | 772k | pxresult = do_SOCKS5(cf, sxstate, data); |
1056 | 772k | break; |
1057 | | |
1058 | 320k | case CURLPROXY_SOCKS4: |
1059 | 321k | case CURLPROXY_SOCKS4A: |
1060 | 321k | pxresult = do_SOCKS4(cf, sxstate, data); |
1061 | 321k | break; |
1062 | | |
1063 | 0 | default: |
1064 | 0 | failf(data, "unknown proxytype option given"); |
1065 | 0 | result = CURLE_COULDNT_CONNECT; |
1066 | 1.09M | } /* switch proxytype */ |
1067 | 1.09M | if(pxresult) { |
1068 | 815 | result = CURLE_PROXY; |
1069 | 815 | data->info.pxcode = pxresult; |
1070 | 815 | } |
1071 | | |
1072 | 1.09M | return result; |
1073 | 1.09M | } |
1074 | | |
1075 | | static void socks_proxy_cf_free(struct Curl_cfilter *cf) |
1076 | 2.22k | { |
1077 | 2.22k | struct socks_state *sxstate = cf->ctx; |
1078 | 2.22k | if(sxstate) { |
1079 | 1.04k | free(sxstate); |
1080 | 1.04k | cf->ctx = NULL; |
1081 | 1.04k | } |
1082 | 2.22k | } |
1083 | | |
1084 | | /* After a TCP connection to the proxy has been verified, this function does |
1085 | | the next magic steps. If 'done' is not set TRUE, it is not done yet and |
1086 | | must be called again. |
1087 | | |
1088 | | Note: this function's sub-functions call failf() |
1089 | | |
1090 | | */ |
1091 | | static CURLcode socks_proxy_cf_connect(struct Curl_cfilter *cf, |
1092 | | struct Curl_easy *data, |
1093 | | bool *done) |
1094 | 1.09M | { |
1095 | 1.09M | CURLcode result; |
1096 | 1.09M | struct connectdata *conn = cf->conn; |
1097 | 1.09M | int sockindex = cf->sockindex; |
1098 | 1.09M | struct socks_state *sx = cf->ctx; |
1099 | | |
1100 | 1.09M | if(cf->connected) { |
1101 | 19 | *done = TRUE; |
1102 | 19 | return CURLE_OK; |
1103 | 19 | } |
1104 | | |
1105 | 1.09M | result = cf->next->cft->do_connect(cf->next, data, done); |
1106 | 1.09M | if(result || !*done) |
1107 | 0 | return result; |
1108 | | |
1109 | 1.09M | if(!sx) { |
1110 | 1.04k | sx = calloc(1, sizeof(*sx)); |
1111 | 1.04k | if(!sx) |
1112 | 0 | return CURLE_OUT_OF_MEMORY; |
1113 | 1.04k | cf->ctx = sx; |
1114 | 1.04k | } |
1115 | | |
1116 | 1.09M | if(sx->state == CONNECT_INIT) { |
1117 | | /* for the secondary socket (FTP), use the "connect to host" |
1118 | | * but ignore the "connect to port" (use the secondary port) |
1119 | | */ |
1120 | 1.04k | sxstate(sx, data, CONNECT_SOCKS_INIT); |
1121 | 1.04k | sx->hostname = |
1122 | 1.04k | conn->bits.httpproxy ? |
1123 | 124 | conn->http_proxy.host.name : |
1124 | 1.04k | conn->bits.conn_to_host ? |
1125 | 916 | conn->conn_to_host.name : |
1126 | 922 | sockindex == SECONDARYSOCKET ? |
1127 | 6 | conn->secondaryhostname : conn->host.name; |
1128 | 1.04k | sx->remote_port = |
1129 | 1.04k | conn->bits.httpproxy ? (int)conn->http_proxy.port : |
1130 | 1.04k | sockindex == SECONDARYSOCKET ? conn->secondary_port : |
1131 | 922 | conn->bits.conn_to_port ? conn->conn_to_port : |
1132 | 912 | conn->remote_port; |
1133 | 1.04k | sx->proxy_user = conn->socks_proxy.user; |
1134 | 1.04k | sx->proxy_password = conn->socks_proxy.passwd; |
1135 | 1.04k | } |
1136 | | |
1137 | 1.09M | result = connect_SOCKS(cf, sx, data); |
1138 | 1.09M | if(!result && sx->state == CONNECT_DONE) { |
1139 | 137 | cf->connected = TRUE; |
1140 | 137 | Curl_verboseconnect(data, conn, cf->sockindex); |
1141 | 137 | socks_proxy_cf_free(cf); |
1142 | 137 | } |
1143 | | |
1144 | 1.09M | *done = cf->connected; |
1145 | 1.09M | return result; |
1146 | 1.09M | } |
1147 | | |
1148 | | static void socks_cf_adjust_pollset(struct Curl_cfilter *cf, |
1149 | | struct Curl_easy *data, |
1150 | | struct easy_pollset *ps) |
1151 | 1.65M | { |
1152 | 1.65M | struct socks_state *sx = cf->ctx; |
1153 | | |
1154 | 1.65M | if(!cf->connected && sx) { |
1155 | | /* If we are not connected, the filter below is and has nothing |
1156 | | * to wait on, we determine what to wait for. */ |
1157 | 1.09M | curl_socket_t sock = Curl_conn_cf_get_socket(cf, data); |
1158 | 1.09M | switch(sx->state) { |
1159 | 1.09M | case CONNECT_RESOLVING: |
1160 | 1.09M | case CONNECT_SOCKS_READ: |
1161 | 1.09M | case CONNECT_AUTH_READ: |
1162 | 1.09M | case CONNECT_REQ_READ: |
1163 | 1.09M | case CONNECT_REQ_READ_MORE: |
1164 | 1.09M | Curl_pollset_set_in_only(data, ps, sock); |
1165 | 1.09M | break; |
1166 | 0 | default: |
1167 | 0 | Curl_pollset_set_out_only(data, ps, sock); |
1168 | 0 | break; |
1169 | 1.09M | } |
1170 | 1.09M | } |
1171 | 1.65M | } |
1172 | | |
1173 | | static void socks_proxy_cf_close(struct Curl_cfilter *cf, |
1174 | | struct Curl_easy *data) |
1175 | 1.04k | { |
1176 | | |
1177 | 1.04k | DEBUGASSERT(cf->next); |
1178 | 1.04k | cf->connected = FALSE; |
1179 | 1.04k | socks_proxy_cf_free(cf); |
1180 | 1.04k | cf->next->cft->do_close(cf->next, data); |
1181 | 1.04k | } |
1182 | | |
1183 | | static void socks_proxy_cf_destroy(struct Curl_cfilter *cf, |
1184 | | struct Curl_easy *data) |
1185 | 1.04k | { |
1186 | 1.04k | (void)data; |
1187 | 1.04k | socks_proxy_cf_free(cf); |
1188 | 1.04k | } |
1189 | | |
1190 | | static CURLcode socks_cf_query(struct Curl_cfilter *cf, |
1191 | | struct Curl_easy *data, |
1192 | | int query, int *pres1, void *pres2) |
1193 | 2.78M | { |
1194 | 2.78M | struct socks_state *sx = cf->ctx; |
1195 | | |
1196 | 2.78M | if(sx) { |
1197 | 2.78M | switch(query) { |
1198 | 0 | case CF_QUERY_HOST_PORT: |
1199 | 0 | *pres1 = sx->remote_port; |
1200 | 0 | *((const char **)pres2) = sx->hostname; |
1201 | 0 | return CURLE_OK; |
1202 | 2.78M | default: |
1203 | 2.78M | break; |
1204 | 2.78M | } |
1205 | 2.78M | } |
1206 | 2.78M | return cf->next ? |
1207 | 2.78M | cf->next->cft->query(cf->next, data, query, pres1, pres2) : |
1208 | 2.78M | CURLE_UNKNOWN_OPTION; |
1209 | 2.78M | } |
1210 | | |
1211 | | struct Curl_cftype Curl_cft_socks_proxy = { |
1212 | | "SOCKS-PROXYY", |
1213 | | CF_TYPE_IP_CONNECT|CF_TYPE_PROXY, |
1214 | | 0, |
1215 | | socks_proxy_cf_destroy, |
1216 | | socks_proxy_cf_connect, |
1217 | | socks_proxy_cf_close, |
1218 | | Curl_cf_def_shutdown, |
1219 | | socks_cf_adjust_pollset, |
1220 | | Curl_cf_def_data_pending, |
1221 | | Curl_cf_def_send, |
1222 | | Curl_cf_def_recv, |
1223 | | Curl_cf_def_cntrl, |
1224 | | Curl_cf_def_conn_is_alive, |
1225 | | Curl_cf_def_conn_keep_alive, |
1226 | | socks_cf_query, |
1227 | | }; |
1228 | | |
1229 | | CURLcode Curl_cf_socks_proxy_insert_after(struct Curl_cfilter *cf_at, |
1230 | | struct Curl_easy *data) |
1231 | 1.04k | { |
1232 | 1.04k | struct Curl_cfilter *cf; |
1233 | 1.04k | CURLcode result; |
1234 | | |
1235 | 1.04k | (void)data; |
1236 | 1.04k | result = Curl_cf_create(&cf, &Curl_cft_socks_proxy, NULL); |
1237 | 1.04k | if(!result) |
1238 | 1.04k | Curl_conn_cf_insert_after(cf_at, cf); |
1239 | 1.04k | return result; |
1240 | 1.04k | } |
1241 | | |
1242 | | #endif /* CURL_DISABLE_PROXY */ |