Debian Codegraph @

deps/curl/lib/socks.c

raw
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2020, Daniel Stenberg, <[email protected]>, 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.haxx.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 ***************************************************************************/
22
23 #include "curl_setup.h"
24
25 #if !defined(CURL_DISABLE_PROXY)
26
27 #ifdef HAVE_NETINET_IN_H
28 #include <netinet/in.h>
29 #endif
30 #ifdef HAVE_ARPA_INET_H
31 #include <arpa/inet.h>
32 #endif
33
34 #include "urldata.h"
35 #include "sendf.h"
36 #include "select.h"
37 #include "connect.h"
38 #include "timeval.h"
39 #include "socks.h"
40 #include "multiif.h" /* for getsock macros */
41
42 /* The last 3 #include files should be in this order */
43 #include "curl_printf.h"
44 #include "curl_memory.h"
45 #include "memdebug.h"
46
47 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
48 /*
49 * Helper read-from-socket functions. Does the same as Curl_read() but it
50 * blocks until all bytes amount of buffersize will be read. No more, no less.
51 *
52 * This is STUPID BLOCKING behavior. Only used by the SOCKS GSSAPI functions.
53 */
54 int Curl_blockread_all(struct connectdata *conn, /* connection data */
55 curl_socket_t sockfd, /* read from this socket */
56 char *buf, /* store read data here */
57 ssize_t buffersize, /* max amount to read */
58 ssize_t *n) /* amount bytes read */
59 {
60 ssize_t nread = 0;
61 ssize_t allread = 0;
62 int result;
63 *n = 0;
64 for(;;) {
65 timediff_t timeleft = Curl_timeleft(conn->data, NULL, TRUE);
66 if(timeleft < 0) {
67 /* we already got the timeout */
68 result = CURLE_OPERATION_TIMEDOUT;
69 break;
70 }
71 if(SOCKET_READABLE(sockfd, timeleft) <= 0) {
72 result = ~CURLE_OK;
73 break;
74 }
75 result = Curl_read_plain(sockfd, buf, buffersize, &nread);
76 if(CURLE_AGAIN == result)
77 continue;
78 if(result)
79 break;
80
81 if(buffersize == nread) {
82 allread += nread;
83 *n = allread;
84 result = CURLE_OK;
85 break;
86 }
87 if(!nread) {
88 result = ~CURLE_OK;
89 break;
90 }
91
92 buffersize -= nread;
93 buf += nread;
94 allread += nread;
95 }
96 return result;
97 }
98 #endif
99
100 #ifndef DEBUGBUILD
101 #define sxstate(x,y) socksstate(x,y)
102 #else
103 #define sxstate(x,y) socksstate(x,y, __LINE__)
104 #endif
105
106
107 /* always use this function to change state, to make debugging easier */
108 static void socksstate(struct connectdata *conn,
109 enum connect_t state
110 #ifdef DEBUGBUILD
111 , int lineno
112 #endif
113 )
114 {
115 enum connect_t oldstate = conn->cnnct.state;
116 #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
117 /* synced with the state list in urldata.h */
118 static const char * const statename[] = {
119 "INIT",
120 "SOCKS_INIT",
121 "SOCKS_SEND",
122 "SOCKS_READ_INIT",
123 "SOCKS_READ",
124 "GSSAPI_INIT",
125 "AUTH_INIT",
126 "AUTH_SEND",
127 "AUTH_READ",
128 "REQ_INIT",
129 "RESOLVING",
130 "RESOLVED",
131 "RESOLVE_REMOTE",
132 "REQ_SEND",
133 "REQ_SENDING",
134 "REQ_READ",
135 "REQ_READ_MORE",
136 "DONE"
137 };
138 #endif
139
140 if(oldstate == state)
141 /* don't bother when the new state is the same as the old state */
142 return;
143
144 conn->cnnct.state = state;
145
146 #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
147 infof(conn->data,
148 "SXSTATE: %s => %s conn %p; line %d\n",
149 statename[oldstate], statename[conn->cnnct.state], conn,
150 lineno);
151 #endif
152 }
153
154 int Curl_SOCKS_getsock(struct connectdata *conn, curl_socket_t *sock,
155 int sockindex)
156 {
157 int rc = 0;
158 sock[0] = conn->sock[sockindex];
159 switch(conn->cnnct.state) {
160 case CONNECT_RESOLVING:
161 case CONNECT_SOCKS_READ:
162 case CONNECT_AUTH_READ:
163 case CONNECT_REQ_READ:
164 case CONNECT_REQ_READ_MORE:
165 rc = GETSOCK_READSOCK(0);
166 break;
167 default:
168 rc = GETSOCK_WRITESOCK(0);
169 break;
170 }
171 return rc;
172 }
173
174 /*
175 * This function logs in to a SOCKS4 proxy and sends the specifics to the final
176 * destination server.
177 *
178 * Reference :
179 * https://www.openssh.com/txt/socks4.protocol
180 *
181 * Note :
182 * Set protocol4a=true for "SOCKS 4A (Simple Extension to SOCKS 4 Protocol)"
183 * Nonsupport "Identification Protocol (RFC1413)"
184 */
185 CURLcode Curl_SOCKS4(const char *proxy_user,
186 const char *hostname,
187 int remote_port,
188 int sockindex,
189 struct connectdata *conn,
190 bool *done)
191 {
192 const bool protocol4a =
193 (conn->socks_proxy.proxytype == CURLPROXY_SOCKS4A) ? TRUE : FALSE;
194 unsigned char *socksreq = &conn->cnnct.socksreq[0];
195 CURLcode result;
196 curl_socket_t sockfd = conn->sock[sockindex];
197 struct Curl_easy *data = conn->data;
198 struct connstate *sx = &conn->cnnct;
199 struct Curl_dns_entry *dns = NULL;
200 ssize_t actualread;
201 ssize_t written;
202
203 if(!SOCKS_STATE(sx->state) && !*done)
204 sxstate(conn, CONNECT_SOCKS_INIT);
205
206 switch(sx->state) {
207 case CONNECT_SOCKS_INIT:
208 if(conn->bits.httpproxy)
209 infof(conn->data, "SOCKS4%s: connecting to HTTP proxy %s port %d\n",
210 protocol4a ? "a" : "", hostname, remote_port);
211
212 infof(data, "SOCKS4 communication to %s:%d\n", hostname, remote_port);
213
214 /*
215 * Compose socks4 request
216 *
217 * Request format
218 *
219 * +----+----+----+----+----+----+----+----+----+----+....+----+
220 * | VN | CD | DSTPORT | DSTIP | USERID |NULL|
221 * +----+----+----+----+----+----+----+----+----+----+....+----+
222 * # of bytes: 1 1 2 4 variable 1
223 */
224
225 socksreq[0] = 4; /* version (SOCKS4) */
226 socksreq[1] = 1; /* connect */
227 socksreq[2] = (unsigned char)((remote_port >> 8) & 0xff); /* PORT MSB */
228 socksreq[3] = (unsigned char)(remote_port & 0xff); /* PORT LSB */
229
230 /* DNS resolve only for SOCKS4, not SOCKS4a */
231 if(!protocol4a) {
232 enum resolve_t rc =
233 Curl_resolv(conn, hostname, remote_port, FALSE, &dns);
234
235 if(rc == CURLRESOLV_ERROR)
236 return CURLE_COULDNT_RESOLVE_PROXY;
237 else if(rc == CURLRESOLV_PENDING) {
238 sxstate(conn, CONNECT_RESOLVING);
239 infof(data, "SOCKS4 non-blocking resolve of %s\n", hostname);
240 return CURLE_OK;
241 }
242 sxstate(conn, CONNECT_RESOLVED);
243 goto CONNECT_RESOLVED;
244 }
245
246 /* socks4a doesn't resolve anything locally */
247 sxstate(conn, CONNECT_REQ_INIT);
248 goto CONNECT_REQ_INIT;
249
250 case CONNECT_RESOLVING:
251 /* check if we have the name resolved by now */
252 dns = Curl_fetch_addr(conn, hostname, (int)conn->port);
253
254 if(dns) {
255 #ifdef CURLRES_ASYNCH
256 conn->async.dns = dns;
257 conn->async.done = TRUE;
258 #endif
259 infof(data, "Hostname '%s' was found\n", hostname);
260 sxstate(conn, CONNECT_RESOLVED);
261 }
262 else {
263 result = Curl_resolv_check(data->conn, &dns);
264 /* stay in the state or error out */
265 return result;
266 }
267 /* FALLTHROUGH */
268 CONNECT_RESOLVED:
269 case CONNECT_RESOLVED: {
270 Curl_addrinfo *hp = NULL;
271 char buf[64];
272 /*
273 * We cannot use 'hostent' as a struct that Curl_resolv() returns. It
274 * returns a Curl_addrinfo pointer that may not always look the same.
275 */
276 if(dns)
277 hp = dns->addr;
278 if(hp) {
279 Curl_printable_address(hp, buf, sizeof(buf));
280
281 if(hp->ai_family == AF_INET) {
282 struct sockaddr_in *saddr_in;
283
284 saddr_in = (struct sockaddr_in *)(void *)hp->ai_addr;
285 socksreq[4] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[0];
286 socksreq[5] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[1];
287 socksreq[6] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[2];
288 socksreq[7] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[3];
289
290 infof(data, "SOCKS4 connect to IPv4 %s (locally resolved)\n", buf);
291 }
292 else {
293 hp = NULL; /* fail! */
294 failf(data, "SOCKS4 connection to %s not supported\n", buf);
295 }
296
297 Curl_resolv_unlock(data, dns); /* not used anymore from now on */
298 }
299 if(!hp) {
300 failf(data, "Failed to resolve \"%s\" for SOCKS4 connect.",
301 hostname);
302 return CURLE_COULDNT_RESOLVE_HOST;
303 }
304 }
305 /* FALLTHROUGH */
306 CONNECT_REQ_INIT:
307 case CONNECT_REQ_INIT:
308 /*
309 * This is currently not supporting "Identification Protocol (RFC1413)".
310 */
311 socksreq[8] = 0; /* ensure empty userid is NUL-terminated */
312 if(proxy_user) {
313 size_t plen = strlen(proxy_user);
314 if(plen >= sizeof(sx->socksreq) - 8) {
315 failf(data, "Too long SOCKS proxy name, can't use!\n");
316 return CURLE_COULDNT_CONNECT;
317 }
318 /* copy the proxy name WITH trailing zero */
319 memcpy(socksreq + 8, proxy_user, plen + 1);
320 }
321
322 /*
323 * Make connection
324 */
325 {
326 ssize_t packetsize = 9 +
327 strlen((char *)socksreq + 8); /* size including NUL */
328
329 /* If SOCKS4a, set special invalid IP address 0.0.0.x */
330 if(protocol4a) {
331 ssize_t hostnamelen = 0;
332 socksreq[4] = 0;
333 socksreq[5] = 0;
334 socksreq[6] = 0;
335 socksreq[7] = 1;
336 /* append hostname */
337 hostnamelen = (ssize_t)strlen(hostname) + 1; /* length including NUL */
338 if(hostnamelen <= 255)
339 strcpy((char *)socksreq + packetsize, hostname);
340 else {
341 failf(data, "SOCKS4: too long host name");
342 return CURLE_COULDNT_CONNECT;
343 }
344 packetsize += hostnamelen;
345 }
346 sx->outp = socksreq;
347 sx->outstanding = packetsize;
348 sxstate(conn, CONNECT_REQ_SENDING);
349 }
350 /* FALLTHROUGH */
351 case CONNECT_REQ_SENDING:
352 /* Send request */
353 result = Curl_write_plain(conn, sockfd, (char *)sx->outp,
354 sx->outstanding, &written);
355 if(result && (CURLE_AGAIN != result)) {
356 failf(data, "Failed to send SOCKS4 connect request.");
357 return CURLE_COULDNT_CONNECT;
358 }
359 if(written != sx->outstanding) {
360 /* not done, remain in state */
361 sx->outstanding -= written;
362 sx->outp += written;
363 return CURLE_OK;
364 }
365
366 /* done sending! */
367 sx->outstanding = 8; /* receive data size */
368 sx->outp = socksreq;
369 sxstate(conn, CONNECT_SOCKS_READ);
370
371 /* FALLTHROUGH */
372 case CONNECT_SOCKS_READ:
373 /* Receive response */
374 result = Curl_read_plain(sockfd, (char *)sx->outp,
375 sx->outstanding, &actualread);
376 if(result && (CURLE_AGAIN != result)) {
377 failf(data, "SOCKS4: Failed receiving connect request ack: %s",
378 curl_easy_strerror(result));
379 return CURLE_COULDNT_CONNECT;
380 }
381 else if(actualread != sx->outstanding) {
382 /* remain in reading state */
383 sx->outstanding -= actualread;
384 sx->outp += actualread;
385 return CURLE_OK;
386 }
387 sxstate(conn, CONNECT_DONE);
388 break;
389 default: /* lots of unused states in SOCKS4 */
390 break;
391 }
392
393 /*
394 * Response format
395 *
396 * +----+----+----+----+----+----+----+----+
397 * | VN | CD | DSTPORT | DSTIP |
398 * +----+----+----+----+----+----+----+----+
399 * # of bytes: 1 1 2 4
400 *
401 * VN is the version of the reply code and should be 0. CD is the result
402 * code with one of the following values:
403 *
404 * 90: request granted
405 * 91: request rejected or failed
406 * 92: request rejected because SOCKS server cannot connect to
407 * identd on the client
408 * 93: request rejected because the client program and identd
409 * report different user-ids
410 */
411
412 /* wrong version ? */
413 if(socksreq[0] != 0) {
414 failf(data,
415 "SOCKS4 reply has wrong version, version should be 0.");
416 return CURLE_COULDNT_CONNECT;
417 }
418
419 /* Result */
420 switch(socksreq[1]) {
421 case 90:
422 infof(data, "SOCKS4%s request granted.\n", protocol4a?"a":"");
423 break;
424 case 91:
425 failf(data,
426 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
427 ", request rejected or failed.",
428 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
429 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
430 (((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
431 (unsigned char)socksreq[1]);
432 return CURLE_COULDNT_CONNECT;
433 case 92:
434 failf(data,
435 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
436 ", request rejected because SOCKS server cannot connect to "
437 "identd on the client.",
438 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
439 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
440 (((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
441 (unsigned char)socksreq[1]);
442 return CURLE_COULDNT_CONNECT;
443 case 93:
444 failf(data,
445 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
446 ", request rejected because the client program and identd "
447 "report different user-ids.",
448 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
449 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
450 (((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
451 (unsigned char)socksreq[1]);
452 return CURLE_COULDNT_CONNECT;
453 default:
454 failf(data,
455 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
456 ", Unknown.",
457 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
458 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
459 (((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
460 (unsigned char)socksreq[1]);
461 return CURLE_COULDNT_CONNECT;
462 }
463
464 *done = TRUE;
465 return CURLE_OK; /* Proxy was successful! */
466 }
467
468 /*
469 * This function logs in to a SOCKS5 proxy and sends the specifics to the final
470 * destination server.
471 */
472 CURLcode Curl_SOCKS5(const char *proxy_user,
473 const char *proxy_password,
474 const char *hostname,
475 int remote_port,
476 int sockindex,
477 struct connectdata *conn,
478 bool *done)
479 {
480 /*
481 According to the RFC1928, section "6. Replies". This is what a SOCK5
482 replies:
483
484 +----+-----+-------+------+----------+----------+
485 |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
486 +----+-----+-------+------+----------+----------+
487 | 1 | 1 | X'00' | 1 | Variable | 2 |
488 +----+-----+-------+------+----------+----------+
489
490 Where:
491
492 o VER protocol version: X'05'
493 o REP Reply field:
494 o X'00' succeeded
495 */
496 unsigned char *socksreq = &conn->cnnct.socksreq[0];
497 char dest[256] = "unknown"; /* printable hostname:port */
498 int idx;
499 ssize_t actualread;
500 ssize_t written;
501 CURLcode result;
502 curl_socket_t sockfd = conn->sock[sockindex];
503 struct Curl_easy *data = conn->data;
504 bool socks5_resolve_local =
505 (conn->socks_proxy.proxytype == CURLPROXY_SOCKS5) ? TRUE : FALSE;
506 const size_t hostname_len = strlen(hostname);
507 ssize_t len = 0;
508 const unsigned long auth = data->set.socks5auth;
509 bool allow_gssapi = FALSE;
510 struct connstate *sx = &conn->cnnct;
511 struct Curl_dns_entry *dns = NULL;
512
513 if(!SOCKS_STATE(sx->state) && !*done)
514 sxstate(conn, CONNECT_SOCKS_INIT);
515
516 switch(sx->state) {
517 case CONNECT_SOCKS_INIT:
518 if(conn->bits.httpproxy)
519 infof(conn->data, "SOCKS5: connecting to HTTP proxy %s port %d\n",
520 hostname, remote_port);
521
522 /* RFC1928 chapter 5 specifies max 255 chars for domain name in packet */
523 if(!socks5_resolve_local && hostname_len > 255) {
524 infof(conn->data, "SOCKS5: server resolving disabled for hostnames of "
525 "length > 255 [actual len=%zu]\n", hostname_len);
526 socks5_resolve_local = TRUE;
527 }
528
529 if(auth & ~(CURLAUTH_BASIC | CURLAUTH_GSSAPI))
530 infof(conn->data,
531 "warning: unsupported value passed to CURLOPT_SOCKS5_AUTH: %lu\n",
532 auth);
533 if(!(auth & CURLAUTH_BASIC))
534 /* disable username/password auth */
535 proxy_user = NULL;
536 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
537 if(auth & CURLAUTH_GSSAPI)
538 allow_gssapi = TRUE;
539 #endif
540
541 idx = 0;
542 socksreq[idx++] = 5; /* version */
543 idx++; /* number of authentication methods */
544 socksreq[idx++] = 0; /* no authentication */
545 if(allow_gssapi)
546 socksreq[idx++] = 1; /* GSS-API */
547 if(proxy_user)
548 socksreq[idx++] = 2; /* username/password */
549 /* write the number of authentication methods */
550 socksreq[1] = (unsigned char) (idx - 2);
551
552 result = Curl_write_plain(conn, sockfd, (char *)socksreq, idx, &written);
553 if(result && (CURLE_AGAIN != result)) {
554 failf(data, "Unable to send initial SOCKS5 request.");
555 return CURLE_COULDNT_CONNECT;
556 }
557 if(written != idx) {
558 sxstate(conn, CONNECT_SOCKS_SEND);
559 sx->outstanding = idx - written;
560 sx->outp = &socksreq[written];
561 return CURLE_OK;
562 }
563 sxstate(conn, CONNECT_SOCKS_READ);
564 goto CONNECT_SOCKS_READ_INIT;
565 case CONNECT_SOCKS_SEND:
566 result = Curl_write_plain(conn, sockfd, (char *)sx->outp,
567 sx->outstanding, &written);
568 if(result && (CURLE_AGAIN != result)) {
569 failf(data, "Unable to send initial SOCKS5 request.");
570 return CURLE_COULDNT_CONNECT;
571 }
572 if(written != sx->outstanding) {
573 /* not done, remain in state */
574 sx->outstanding -= written;
575 sx->outp += written;
576 return CURLE_OK;
577 }
578 /* FALLTHROUGH */
579 CONNECT_SOCKS_READ_INIT:
580 case CONNECT_SOCKS_READ_INIT:
581 sx->outstanding = 2; /* expect two bytes */
582 sx->outp = socksreq; /* store it here */
583 /* FALLTHROUGH */
584 case CONNECT_SOCKS_READ:
585 result = Curl_read_plain(sockfd, (char *)sx->outp,
586 sx->outstanding, &actualread);
587 if(result && (CURLE_AGAIN != result)) {
588 failf(data, "Unable to receive initial SOCKS5 response.");
589 return CURLE_COULDNT_CONNECT;
590 }
591 else if(actualread != sx->outstanding) {
592 /* remain in reading state */
593 sx->outstanding -= actualread;
594 sx->outp += actualread;
595 return CURLE_OK;
596 }
597 else if(socksreq[0] != 5) {
598 failf(data, "Received invalid version in initial SOCKS5 response.");
599 return CURLE_COULDNT_CONNECT;
600 }
601 else if(socksreq[1] == 0) {
602 /* DONE! No authentication needed. Send request. */
603 sxstate(conn, CONNECT_REQ_INIT);
604 goto CONNECT_REQ_INIT;
605 }
606 else if(socksreq[1] == 2) {
607 /* regular name + password authentication */
608 sxstate(conn, CONNECT_AUTH_INIT);
609 goto CONNECT_AUTH_INIT;
610 }
611 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
612 else if(allow_gssapi && (socksreq[1] == 1)) {
613 sxstate(conn, CONNECT_GSSAPI_INIT);
614 result = Curl_SOCKS5_gssapi_negotiate(sockindex, conn);
615 if(result) {
616 failf(data, "Unable to negotiate SOCKS5 GSS-API context.");
617 return CURLE_COULDNT_CONNECT;
618 }
619 }
620 #endif
621 else {
622 /* error */
623 if(!allow_gssapi && (socksreq[1] == 1)) {
624 failf(data,
625 "SOCKS5 GSSAPI per-message authentication is not supported.");
626 return CURLE_COULDNT_CONNECT;
627 }
628 else if(socksreq[1] == 255) {
629 failf(data, "No authentication method was acceptable.");
630 return CURLE_COULDNT_CONNECT;
631 }
632 failf(data,
633 "Undocumented SOCKS5 mode attempted to be used by server.");
634 return CURLE_COULDNT_CONNECT;
635 }
636 break;
637 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
638 case CONNECT_GSSAPI_INIT:
639 /* GSSAPI stuff done non-blocking */
640 break;
641 #endif
642
643 default: /* do nothing! */
644 break;
645
646 CONNECT_AUTH_INIT:
647 case CONNECT_AUTH_INIT: {
648 /* Needs user name and password */
649 size_t proxy_user_len, proxy_password_len;
650 if(proxy_user && proxy_password) {
651 proxy_user_len = strlen(proxy_user);
652 proxy_password_len = strlen(proxy_password);
653 }
654 else {
655 proxy_user_len = 0;
656 proxy_password_len = 0;
657 }
658
659 /* username/password request looks like
660 * +----+------+----------+------+----------+
661 * |VER | ULEN | UNAME | PLEN | PASSWD |
662 * +----+------+----------+------+----------+
663 * | 1 | 1 | 1 to 255 | 1 | 1 to 255 |
664 * +----+------+----------+------+----------+
665 */
666 len = 0;
667 socksreq[len++] = 1; /* username/pw subnegotiation version */
668 socksreq[len++] = (unsigned char) proxy_user_len;
669 if(proxy_user && proxy_user_len) {
670 /* the length must fit in a single byte */
671 if(proxy_user_len >= 255) {
672 failf(data, "Excessive user name length for proxy auth");
673 return CURLE_BAD_FUNCTION_ARGUMENT;
674 }
675 memcpy(socksreq + len, proxy_user, proxy_user_len);
676 }
677 len += proxy_user_len;
678 socksreq[len++] = (unsigned char) proxy_password_len;
679 if(proxy_password && proxy_password_len) {
680 /* the length must fit in a single byte */
681 if(proxy_password_len > 255) {
682 failf(data, "Excessive password length for proxy auth");
683 return CURLE_BAD_FUNCTION_ARGUMENT;
684 }
685 memcpy(socksreq + len, proxy_password, proxy_password_len);
686 }
687 len += proxy_password_len;
688 sxstate(conn, CONNECT_AUTH_SEND);
689 sx->outstanding = len;
690 sx->outp = socksreq;
691 }
692 /* FALLTHROUGH */
693 case CONNECT_AUTH_SEND:
694 result = Curl_write_plain(conn, sockfd, (char *)sx->outp,
695 sx->outstanding, &written);
696 if(result && (CURLE_AGAIN != result)) {
697 failf(data, "Failed to send SOCKS5 sub-negotiation request.");
698 return CURLE_COULDNT_CONNECT;
699 }
700 if(sx->outstanding != written) {
701 /* remain in state */
702 sx->outstanding -= written;
703 sx->outp += written;
704 return CURLE_OK;
705 }
706 sx->outp = socksreq;
707 sx->outstanding = 2;
708 sxstate(conn, CONNECT_AUTH_READ);
709 /* FALLTHROUGH */
710 case CONNECT_AUTH_READ:
711 result = Curl_read_plain(sockfd, (char *)sx->outp,
712 sx->outstanding, &actualread);
713 if(result && (CURLE_AGAIN != result)) {
714 failf(data, "Unable to receive SOCKS5 sub-negotiation response.");
715 return CURLE_COULDNT_CONNECT;
716 }
717 if(actualread != sx->outstanding) {
718 /* remain in state */
719 sx->outstanding -= actualread;
720 sx->outp += actualread;
721 return CURLE_OK;
722 }
723
724 /* ignore the first (VER) byte */
725 if(socksreq[1] != 0) { /* status */
726 failf(data, "User was rejected by the SOCKS5 server (%d %d).",
727 socksreq[0], socksreq[1]);
728 return CURLE_COULDNT_CONNECT;
729 }
730
731 /* Everything is good so far, user was authenticated! */
732 sxstate(conn, CONNECT_REQ_INIT);
733 /* FALLTHROUGH */
734 CONNECT_REQ_INIT:
735 case CONNECT_REQ_INIT:
736 if(socks5_resolve_local) {
737 enum resolve_t rc = Curl_resolv(conn, hostname, remote_port,
738 FALSE, &dns);
739
740 if(rc == CURLRESOLV_ERROR)
741 return CURLE_COULDNT_RESOLVE_HOST;
742
743 if(rc == CURLRESOLV_PENDING) {
744 sxstate(conn, CONNECT_RESOLVING);
745 return CURLE_OK;
746 }
747 sxstate(conn, CONNECT_RESOLVED);
748 goto CONNECT_RESOLVED;
749 }
750 goto CONNECT_RESOLVE_REMOTE;
751
752 case CONNECT_RESOLVING:
753 /* check if we have the name resolved by now */
754 dns = Curl_fetch_addr(conn, hostname, (int)conn->port);
755
756 if(dns) {
757 #ifdef CURLRES_ASYNCH
758 conn->async.dns = dns;
759 conn->async.done = TRUE;
760 #endif
761 infof(data, "SOCKS5: hostname '%s' found\n", hostname);
762 }
763
764 if(!dns) {
765 result = Curl_resolv_check(data->conn, &dns);
766 /* stay in the state or error out */
767 return result;
768 }
769 /* FALLTHROUGH */
770 CONNECT_RESOLVED:
771 case CONNECT_RESOLVED: {
772 Curl_addrinfo *hp = NULL;
773 if(dns)
774 hp = dns->addr;
775 if(!hp) {
776 failf(data, "Failed to resolve \"%s\" for SOCKS5 connect.",
777 hostname);
778 return CURLE_COULDNT_RESOLVE_HOST;
779 }
780
781 if(Curl_printable_address(hp, dest, sizeof(dest))) {
782 size_t destlen = strlen(dest);
783 msnprintf(dest + destlen, sizeof(dest) - destlen, ":%d", remote_port);
784 }
785 else {
786 strcpy(dest, "unknown");
787 }
788
789 len = 0;
790 socksreq[len++] = 5; /* version (SOCKS5) */
791 socksreq[len++] = 1; /* connect */
792 socksreq[len++] = 0; /* must be zero */
793 if(hp->ai_family == AF_INET) {
794 int i;
795 struct sockaddr_in *saddr_in;
796 socksreq[len++] = 1; /* ATYP: IPv4 = 1 */
797
798 saddr_in = (struct sockaddr_in *)(void *)hp->ai_addr;
799 for(i = 0; i < 4; i++) {
800 socksreq[len++] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[i];
801 }
802
803 infof(data, "SOCKS5 connect to IPv4 %s (locally resolved)\n", dest);
804 }
805 #ifdef ENABLE_IPV6
806 else if(hp->ai_family == AF_INET6) {
807 int i;
808 struct sockaddr_in6 *saddr_in6;
809 socksreq[len++] = 4; /* ATYP: IPv6 = 4 */
810
811 saddr_in6 = (struct sockaddr_in6 *)(void *)hp->ai_addr;
812 for(i = 0; i < 16; i++) {
813 socksreq[len++] =
814 ((unsigned char *)&saddr_in6->sin6_addr.s6_addr)[i];
815 }
816
817 infof(data, "SOCKS5 connect to IPv6 %s (locally resolved)\n", dest);
818 }
819 #endif
820 else {
821 hp = NULL; /* fail! */
822 failf(data, "SOCKS5 connection to %s not supported\n", dest);
823 }
824
825 Curl_resolv_unlock(data, dns); /* not used anymore from now on */
826 goto CONNECT_REQ_SEND;
827 }
828 CONNECT_RESOLVE_REMOTE:
829 case CONNECT_RESOLVE_REMOTE:
830 /* Authentication is complete, now specify destination to the proxy */
831 len = 0;
832 socksreq[len++] = 5; /* version (SOCKS5) */
833 socksreq[len++] = 1; /* connect */
834 socksreq[len++] = 0; /* must be zero */
835
836 if(!socks5_resolve_local) {
837 socksreq[len++] = 3; /* ATYP: domain name = 3 */
838 socksreq[len++] = (char) hostname_len; /* one byte address length */
839 memcpy(&socksreq[len], hostname, hostname_len); /* address w/o NULL */
840 len += hostname_len;
841 infof(data, "SOCKS5 connect to %s:%d (remotely resolved)\n",
842 hostname, remote_port);
843 }
844 /* FALLTHROUGH */
845
846 CONNECT_REQ_SEND:
847 case CONNECT_REQ_SEND:
848 /* PORT MSB */
849 socksreq[len++] = (unsigned char)((remote_port >> 8) & 0xff);
850 /* PORT LSB */
851 socksreq[len++] = (unsigned char)(remote_port & 0xff);
852
853 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
854 if(conn->socks5_gssapi_enctype) {
855 failf(data, "SOCKS5 GSS-API protection not yet implemented.");
856 return CURLE_COULDNT_CONNECT;
857 }
858 #endif
859 sx->outp = socksreq;
860 sx->outstanding = len;
861 sxstate(conn, CONNECT_REQ_SENDING);
862 /* FALLTHROUGH */
863 case CONNECT_REQ_SENDING:
864 result = Curl_write_plain(conn, sockfd, (char *)sx->outp,
865 sx->outstanding, &written);
866 if(result && (CURLE_AGAIN != result)) {
867 failf(data, "Failed to send SOCKS5 connect request.");
868 return CURLE_COULDNT_CONNECT;
869 }
870 if(sx->outstanding != written) {
871 /* remain in state */
872 sx->outstanding -= written;
873 sx->outp += written;
874 return CURLE_OK;
875 }
876 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
877 if(conn->socks5_gssapi_enctype) {
878 failf(data, "SOCKS5 GSS-API protection not yet implemented.");
879 return CURLE_COULDNT_CONNECT;
880 }
881 #endif
882 sx->outstanding = 10; /* minimum packet size is 10 */
883 sx->outp = socksreq;
884 sxstate(conn, CONNECT_REQ_READ);
885 /* FALLTHROUGH */
886 case CONNECT_REQ_READ:
887 result = Curl_read_plain(sockfd, (char *)sx->outp,
888 sx->outstanding, &actualread);
889 if(result && (CURLE_AGAIN != result)) {
890 failf(data, "Failed to receive SOCKS5 connect request ack.");
891 return CURLE_COULDNT_CONNECT;
892 }
893 else if(actualread != sx->outstanding) {
894 /* remain in state */
895 sx->outstanding -= actualread;
896 sx->outp += actualread;
897 return CURLE_OK;
898 }
899
900 if(socksreq[0] != 5) { /* version */
901 failf(data,
902 "SOCKS5 reply has wrong version, version should be 5.");
903 return CURLE_COULDNT_CONNECT;
904 }
905 else if(socksreq[1] != 0) { /* Anything besides 0 is an error */
906 failf(data, "Can't complete SOCKS5 connection to %s. (%d)",
907 hostname, (unsigned char)socksreq[1]);
908 return CURLE_COULDNT_CONNECT;
909 }
910
911 /* Fix: in general, returned BND.ADDR is variable length parameter by RFC
912 1928, so the reply packet should be read until the end to avoid errors
913 at subsequent protocol level.
914
915 +----+-----+-------+------+----------+----------+
916 |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
917 +----+-----+-------+------+----------+----------+
918 | 1 | 1 | X'00' | 1 | Variable | 2 |
919 +----+-----+-------+------+----------+----------+
920
921 ATYP:
922 o IP v4 address: X'01', BND.ADDR = 4 byte
923 o domain name: X'03', BND.ADDR = [ 1 byte length, string ]
924 o IP v6 address: X'04', BND.ADDR = 16 byte
925 */
926
927 /* Calculate real packet size */
928 if(socksreq[3] == 3) {
929 /* domain name */
930 int addrlen = (int) socksreq[4];
931 len = 5 + addrlen + 2;
932 }
933 else if(socksreq[3] == 4) {
934 /* IPv6 */
935 len = 4 + 16 + 2;
936 }
937
938 /* At this point we already read first 10 bytes */
939 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
940 if(!conn->socks5_gssapi_enctype) {
941 /* decrypt_gssapi_blockread already read the whole packet */
942 #endif
943 if(len > 10) {
944 sx->outstanding = len - 10; /* get the rest */
945 sx->outp = &socksreq[10];
946 sxstate(conn, CONNECT_REQ_READ_MORE);
947 }
948 else {
949 sxstate(conn, CONNECT_DONE);
950 break;
951 }
952 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
953 }
954 #endif
955 /* FALLTHROUGH */
956 case CONNECT_REQ_READ_MORE:
957 result = Curl_read_plain(sockfd, (char *)sx->outp,
958 sx->outstanding, &actualread);
959 if(result && (CURLE_AGAIN != result)) {
960 failf(data, "Failed to receive SOCKS5 connect request ack.");
961 return CURLE_COULDNT_CONNECT;
962 }
963 if(actualread != sx->outstanding) {
964 /* remain in state */
965 sx->outstanding -= actualread;
966 sx->outp += actualread;
967 return CURLE_OK;
968 }
969 sxstate(conn, CONNECT_DONE);
970 }
971 infof(data, "SOCKS5 request granted.\n");
972
973 *done = TRUE;
974 return CURLE_OK; /* Proxy was successful! */
975 }
976
977 #endif /* CURL_DISABLE_PROXY */