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