1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #if !defined(CURL_DISABLE_PROXY)
27 #ifdef HAVE_NETINET_IN_H
28 #include <netinet/in.h>
30 #ifdef HAVE_ARPA_INET_H
31 #include <arpa/inet.h>
42 /* The last #include file should be: */
46 * Helper read-from-socket functions. Does the same as Curl_read() but it
47 * blocks until all bytes amount of buffersize will be read. No more, no less.
49 * This is STUPID BLOCKING behaviour which we frown upon, but right now this
52 int Curl_blockread_all(struct connectdata *conn, /* connection data */
53 curl_socket_t sockfd, /* read from this socket */
54 char *buf, /* store read data here */
55 ssize_t buffersize, /* max amount to read */
56 ssize_t *n) /* amount bytes read */
64 timeleft = Curl_timeleft(conn->data, NULL, TRUE);
66 /* we already got the timeout */
67 result = CURLE_OPERATION_TIMEDOUT;
70 if(Curl_socket_ready(sockfd, CURL_SOCKET_BAD, timeleft) <= 0) {
74 result = Curl_read_plain(sockfd, buf, buffersize, &nread);
75 if(CURLE_AGAIN == result)
80 if(buffersize == nread) {
99 * This function logs in to a SOCKS4 proxy and sends the specifics to the final
100 * destination server.
103 * http://socks.permeo.com/protocol/socks4.protocol
106 * Set protocol4a=true for "SOCKS 4A (Simple Extension to SOCKS 4 Protocol)"
107 * Nonsupport "Identification Protocol (RFC1413)"
109 CURLcode Curl_SOCKS4(const char *proxy_name,
110 const char *hostname,
113 struct connectdata *conn,
116 #define SOCKS4REQLEN 262
117 unsigned char socksreq[SOCKS4REQLEN]; /* room for SOCKS4 request incl. user
121 curl_socket_t sock = conn->sock[sockindex];
122 struct SessionHandle *data = conn->data;
124 if(Curl_timeleft(data, NULL, TRUE) < 0) {
125 /* time-out, bail out, go home */
126 failf(data, "Connection time-out");
127 return CURLE_OPERATION_TIMEDOUT;
130 curlx_nonblock(sock, FALSE);
133 * Compose socks4 request
137 * +----+----+----+----+----+----+----+----+----+----+....+----+
138 * | VN | CD | DSTPORT | DSTIP | USERID |NULL|
139 * +----+----+----+----+----+----+----+----+----+----+....+----+
140 * # of bytes: 1 1 2 4 variable 1
143 socksreq[0] = 4; /* version (SOCKS4) */
144 socksreq[1] = 1; /* connect */
145 socksreq[2] = (unsigned char)((remote_port >> 8) & 0xff); /* PORT MSB */
146 socksreq[3] = (unsigned char)(remote_port & 0xff); /* PORT LSB */
148 /* DNS resolve only for SOCKS4, not SOCKS4a */
150 struct Curl_dns_entry *dns;
151 Curl_addrinfo *hp=NULL;
154 rc = Curl_resolv(conn, hostname, remote_port, &dns);
156 if(rc == CURLRESOLV_ERROR)
157 return CURLE_COULDNT_RESOLVE_PROXY;
159 if(rc == CURLRESOLV_PENDING)
160 /* ignores the return code, but 'dns' remains NULL on failure */
161 (void)Curl_resolver_wait_resolv(conn, &dns);
164 * We cannot use 'hostent' as a struct that Curl_resolv() returns. It
165 * returns a Curl_addrinfo pointer that may not always look the same.
171 unsigned short ip[4];
172 Curl_printable_address(hp, buf, sizeof(buf));
174 if(4 == sscanf( buf, "%hu.%hu.%hu.%hu",
175 &ip[0], &ip[1], &ip[2], &ip[3])) {
177 socksreq[4] = (unsigned char)ip[0];
178 socksreq[5] = (unsigned char)ip[1];
179 socksreq[6] = (unsigned char)ip[2];
180 socksreq[7] = (unsigned char)ip[3];
183 hp = NULL; /* fail! */
185 Curl_resolv_unlock(data, dns); /* not used anymore from now on */
189 failf(data, "Failed to resolve \"%s\" for SOCKS4 connect.",
191 return CURLE_COULDNT_RESOLVE_HOST;
196 * This is currently not supporting "Identification Protocol (RFC1413)".
198 socksreq[8] = 0; /* ensure empty userid is NUL-terminated */
200 size_t plen = strlen(proxy_name);
201 if(plen >= sizeof(socksreq) - 8) {
202 failf(data, "Too long SOCKS proxy name, can't use!\n");
203 return CURLE_COULDNT_CONNECT;
205 /* copy the proxy name WITH trailing zero */
206 memcpy(socksreq + 8, proxy_name, plen+1);
215 ssize_t hostnamelen = 0;
217 (int)strlen((char*)socksreq + 8); /* size including NUL */
219 /* If SOCKS4a, set special invalid IP address 0.0.0.x */
225 /* If still enough room in buffer, also append hostname */
226 hostnamelen = (ssize_t)strlen(hostname) + 1; /* length including NUL */
227 if(packetsize + hostnamelen <= SOCKS4REQLEN)
228 strcpy((char*)socksreq + packetsize, hostname);
230 hostnamelen = 0; /* Flag: hostname did not fit in buffer */
234 code = Curl_write_plain(conn, sock, (char *)socksreq,
235 packetsize + hostnamelen,
237 if((code != CURLE_OK) || (written != packetsize + hostnamelen)) {
238 failf(data, "Failed to send SOCKS4 connect request.");
239 return CURLE_COULDNT_CONNECT;
241 if(protocol4a && hostnamelen == 0) {
242 /* SOCKS4a with very long hostname - send that name separately */
243 hostnamelen = (ssize_t)strlen(hostname) + 1;
244 code = Curl_write_plain(conn, sock, (char *)hostname, hostnamelen,
246 if((code != CURLE_OK) || (written != hostnamelen)) {
247 failf(data, "Failed to send SOCKS4 connect request.");
248 return CURLE_COULDNT_CONNECT;
252 packetsize = 8; /* receive data size */
254 /* Receive response */
255 result = Curl_blockread_all(conn, sock, (char *)socksreq, packetsize,
257 if((result != CURLE_OK) || (actualread != packetsize)) {
258 failf(data, "Failed to receive SOCKS4 connect request ack.");
259 return CURLE_COULDNT_CONNECT;
265 * +----+----+----+----+----+----+----+----+
266 * | VN | CD | DSTPORT | DSTIP |
267 * +----+----+----+----+----+----+----+----+
268 * # of bytes: 1 1 2 4
270 * VN is the version of the reply code and should be 0. CD is the result
271 * code with one of the following values:
273 * 90: request granted
274 * 91: request rejected or failed
275 * 92: request rejected because SOCKS server cannot connect to
276 * identd on the client
277 * 93: request rejected because the client program and identd
278 * report different user-ids
281 /* wrong version ? */
282 if(socksreq[0] != 0) {
284 "SOCKS4 reply has wrong version, version should be 4.");
285 return CURLE_COULDNT_CONNECT;
289 switch(socksreq[1]) {
291 infof(data, "SOCKS4%s request granted.\n", protocol4a?"a":"");
295 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
296 ", request rejected or failed.",
297 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
298 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
299 ((socksreq[8] << 8) | socksreq[9]),
301 return CURLE_COULDNT_CONNECT;
304 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
305 ", request rejected because SOCKS server cannot connect to "
306 "identd on the client.",
307 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
308 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
309 ((socksreq[8] << 8) | socksreq[9]),
311 return CURLE_COULDNT_CONNECT;
314 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
315 ", request rejected because the client program and identd "
316 "report different user-ids.",
317 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
318 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
319 ((socksreq[8] << 8) | socksreq[9]),
321 return CURLE_COULDNT_CONNECT;
324 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
326 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
327 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
328 ((socksreq[8] << 8) | socksreq[9]),
330 return CURLE_COULDNT_CONNECT;
334 curlx_nonblock(sock, TRUE);
336 return CURLE_OK; /* Proxy was successful! */
340 * This function logs in to a SOCKS5 proxy and sends the specifics to the final
341 * destination server.
343 CURLcode Curl_SOCKS5(const char *proxy_name,
344 const char *proxy_password,
345 const char *hostname,
348 struct connectdata *conn)
351 According to the RFC1928, section "6. Replies". This is what a SOCK5
354 +----+-----+-------+------+----------+----------+
355 |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
356 +----+-----+-------+------+----------+----------+
357 | 1 | 1 | X'00' | 1 | Variable | 2 |
358 +----+-----+-------+------+----------+----------+
362 o VER protocol version: X'05'
367 unsigned char socksreq[600]; /* room for large user/pw (255 max each) */
372 curl_socket_t sock = conn->sock[sockindex];
373 struct SessionHandle *data = conn->data;
375 bool socks5_resolve_local = (conn->proxytype == CURLPROXY_SOCKS5)?TRUE:FALSE;
376 const size_t hostname_len = strlen(hostname);
379 /* RFC1928 chapter 5 specifies max 255 chars for domain name in packet */
380 if(!socks5_resolve_local && hostname_len > 255) {
381 infof(conn->data,"SOCKS5: server resolving disabled for hostnames of "
382 "length > 255 [actual len=%zu]\n", hostname_len);
383 socks5_resolve_local = TRUE;
387 timeout = Curl_timeleft(data, NULL, TRUE);
390 /* time-out, bail out, go home */
391 failf(data, "Connection time-out");
392 return CURLE_OPERATION_TIMEDOUT;
395 curlx_nonblock(sock, TRUE);
397 /* wait until socket gets connected */
398 result = Curl_socket_ready(CURL_SOCKET_BAD, sock, timeout);
401 failf(conn->data, "SOCKS5: no connection here");
402 return CURLE_COULDNT_CONNECT;
404 else if(0 == result) {
405 failf(conn->data, "SOCKS5: connection timeout");
406 return CURLE_OPERATION_TIMEDOUT;
409 if(result & CURL_CSELECT_ERR) {
410 failf(conn->data, "SOCKS5: error occurred during connection");
411 return CURLE_COULDNT_CONNECT;
414 socksreq[0] = 5; /* version */
415 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
416 socksreq[1] = (char)(proxy_name ? 3 : 2); /* number of methods (below) */
417 socksreq[2] = 0; /* no authentication */
418 socksreq[3] = 1; /* gssapi */
419 socksreq[4] = 2; /* username/password */
421 socksreq[1] = (char)(proxy_name ? 2 : 1); /* number of methods (below) */
422 socksreq[2] = 0; /* no authentication */
423 socksreq[3] = 2; /* username/password */
426 curlx_nonblock(sock, FALSE);
428 code = Curl_write_plain(conn, sock, (char *)socksreq, (2 + (int)socksreq[1]),
430 if((code != CURLE_OK) || (written != (2 + (int)socksreq[1]))) {
431 failf(data, "Unable to send initial SOCKS5 request.");
432 return CURLE_COULDNT_CONNECT;
435 curlx_nonblock(sock, TRUE);
437 result = Curl_socket_ready(sock, CURL_SOCKET_BAD, timeout);
440 failf(conn->data, "SOCKS5 nothing to read");
441 return CURLE_COULDNT_CONNECT;
443 else if(0 == result) {
444 failf(conn->data, "SOCKS5 read timeout");
445 return CURLE_OPERATION_TIMEDOUT;
448 if(result & CURL_CSELECT_ERR) {
449 failf(conn->data, "SOCKS5 read error occurred");
450 return CURLE_RECV_ERROR;
453 curlx_nonblock(sock, FALSE);
455 result=Curl_blockread_all(conn, sock, (char *)socksreq, 2, &actualread);
456 if((result != CURLE_OK) || (actualread != 2)) {
457 failf(data, "Unable to receive initial SOCKS5 response.");
458 return CURLE_COULDNT_CONNECT;
461 if(socksreq[0] != 5) {
462 failf(data, "Received invalid version in initial SOCKS5 response.");
463 return CURLE_COULDNT_CONNECT;
465 if(socksreq[1] == 0) {
466 /* Nothing to do, no authentication needed */
469 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
470 else if(socksreq[1] == 1) {
471 code = Curl_SOCKS5_gssapi_negotiate(sockindex, conn);
472 if(code != CURLE_OK) {
473 failf(data, "Unable to negotiate SOCKS5 gssapi context.");
474 return CURLE_COULDNT_CONNECT;
478 else if(socksreq[1] == 2) {
479 /* Needs user name and password */
480 size_t proxy_name_len, proxy_password_len;
481 if(proxy_name && proxy_password) {
482 proxy_name_len = strlen(proxy_name);
483 proxy_password_len = strlen(proxy_password);
487 proxy_password_len = 0;
490 /* username/password request looks like
491 * +----+------+----------+------+----------+
492 * |VER | ULEN | UNAME | PLEN | PASSWD |
493 * +----+------+----------+------+----------+
494 * | 1 | 1 | 1 to 255 | 1 | 1 to 255 |
495 * +----+------+----------+------+----------+
498 socksreq[len++] = 1; /* username/pw subnegotiation version */
499 socksreq[len++] = (unsigned char) proxy_name_len;
500 if(proxy_name && proxy_name_len)
501 memcpy(socksreq + len, proxy_name, proxy_name_len);
502 len += proxy_name_len;
503 socksreq[len++] = (unsigned char) proxy_password_len;
504 if(proxy_password && proxy_password_len)
505 memcpy(socksreq + len, proxy_password, proxy_password_len);
506 len += proxy_password_len;
508 code = Curl_write_plain(conn, sock, (char *)socksreq, len, &written);
509 if((code != CURLE_OK) || (len != written)) {
510 failf(data, "Failed to send SOCKS5 sub-negotiation request.");
511 return CURLE_COULDNT_CONNECT;
514 result=Curl_blockread_all(conn, sock, (char *)socksreq, 2, &actualread);
515 if((result != CURLE_OK) || (actualread != 2)) {
516 failf(data, "Unable to receive SOCKS5 sub-negotiation response.");
517 return CURLE_COULDNT_CONNECT;
520 /* ignore the first (VER) byte */
521 if(socksreq[1] != 0) { /* status */
522 failf(data, "User was rejected by the SOCKS5 server (%d %d).",
523 socksreq[0], socksreq[1]);
524 return CURLE_COULDNT_CONNECT;
527 /* Everything is good so far, user was authenticated! */
531 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
532 if(socksreq[1] == 255) {
534 if(socksreq[1] == 1) {
536 "SOCKS5 GSSAPI per-message authentication is not supported.");
537 return CURLE_COULDNT_CONNECT;
539 else if(socksreq[1] == 255) {
541 if(!proxy_name || !*proxy_name) {
543 "No authentication method was acceptable. (It is quite likely"
544 " that the SOCKS5 server wanted a username/password, since none"
545 " was supplied to the server on this connection.)");
548 failf(data, "No authentication method was acceptable.");
550 return CURLE_COULDNT_CONNECT;
554 "Undocumented SOCKS5 mode attempted to be used by server.");
555 return CURLE_COULDNT_CONNECT;
559 /* Authentication is complete, now specify destination to the proxy */
561 socksreq[len++] = 5; /* version (SOCKS5) */
562 socksreq[len++] = 1; /* connect */
563 socksreq[len++] = 0; /* must be zero */
565 if(!socks5_resolve_local) {
566 socksreq[len++] = 3; /* ATYP: domain name = 3 */
567 socksreq[len++] = (char) hostname_len; /* address length */
568 memcpy(&socksreq[len], hostname, hostname_len); /* address str w/o NULL */
572 struct Curl_dns_entry *dns;
573 Curl_addrinfo *hp = NULL;
574 int rc = Curl_resolv(conn, hostname, remote_port, &dns);
576 if(rc == CURLRESOLV_ERROR)
577 return CURLE_COULDNT_RESOLVE_HOST;
579 if(rc == CURLRESOLV_PENDING) {
580 /* this requires that we're in "wait for resolve" state */
581 code = Curl_resolver_wait_resolv(conn, &dns);
587 * We cannot use 'hostent' as a struct that Curl_resolv() returns. It
588 * returns a Curl_addrinfo pointer that may not always look the same.
593 struct sockaddr_in *saddr_in;
595 struct sockaddr_in6 *saddr_in6;
599 if(hp->ai_family == AF_INET) {
600 socksreq[len++] = 1; /* ATYP: IPv4 = 1 */
602 saddr_in = (struct sockaddr_in*)hp->ai_addr;
603 for(i = 0; i < 4; i++) {
604 socksreq[len++] = ((unsigned char*)&saddr_in->sin_addr.s_addr)[i];
605 infof(data, "%d\n", socksreq[len-1]);
609 else if(hp->ai_family == AF_INET6) {
610 socksreq[len++] = 4; /* ATYP: IPv6 = 4 */
612 saddr_in6 = (struct sockaddr_in6*)hp->ai_addr;
613 for(i = 0; i < 16; i++) {
614 socksreq[len++] = ((unsigned char*)&saddr_in6->sin6_addr.s6_addr)[i];
619 hp = NULL; /* fail! */
621 Curl_resolv_unlock(data, dns); /* not used anymore from now on */
624 failf(data, "Failed to resolve \"%s\" for SOCKS5 connect.",
626 return CURLE_COULDNT_RESOLVE_HOST;
630 socksreq[len++] = (unsigned char)((remote_port >> 8) & 0xff); /* PORT MSB */
631 socksreq[len++] = (unsigned char)(remote_port & 0xff); /* PORT LSB */
633 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
634 if(conn->socks5_gssapi_enctype) {
635 failf(data, "SOCKS5 gssapi protection not yet implemented.");
639 code = Curl_write_plain(conn, sock, (char *)socksreq, len, &written);
641 if((code != CURLE_OK) || (len != written)) {
642 failf(data, "Failed to send SOCKS5 connect request.");
643 return CURLE_COULDNT_CONNECT;
646 len = 10; /* minimum packet size is 10 */
648 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
649 if(conn->socks5_gssapi_enctype) {
650 failf(data, "SOCKS5 gssapi protection not yet implemented.");
654 result = Curl_blockread_all(conn, sock, (char *)socksreq,
657 if((result != CURLE_OK) || (len != actualread)) {
658 failf(data, "Failed to receive SOCKS5 connect request ack.");
659 return CURLE_COULDNT_CONNECT;
662 if(socksreq[0] != 5) { /* version */
664 "SOCKS5 reply has wrong version, version should be 5.");
665 return CURLE_COULDNT_CONNECT;
667 if(socksreq[1] != 0) { /* Anything besides 0 is an error */
668 if(socksreq[3] == 1) {
670 "Can't complete SOCKS5 connection to %d.%d.%d.%d:%d. (%d)",
671 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
672 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
673 ((socksreq[8] << 8) | socksreq[9]),
676 else if(socksreq[3] == 3) {
678 "Can't complete SOCKS5 connection to %s:%d. (%d)",
680 ((socksreq[8] << 8) | socksreq[9]),
683 else if(socksreq[3] == 4) {
685 "Can't complete SOCKS5 connection to %02x%02x:%02x%02x:"
686 "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%d. (%d)",
687 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
688 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
689 (unsigned char)socksreq[8], (unsigned char)socksreq[9],
690 (unsigned char)socksreq[10], (unsigned char)socksreq[11],
691 (unsigned char)socksreq[12], (unsigned char)socksreq[13],
692 (unsigned char)socksreq[14], (unsigned char)socksreq[15],
693 (unsigned char)socksreq[16], (unsigned char)socksreq[17],
694 (unsigned char)socksreq[18], (unsigned char)socksreq[19],
695 ((socksreq[8] << 8) | socksreq[9]),
698 return CURLE_COULDNT_CONNECT;
701 /* Fix: in general, returned BND.ADDR is variable length parameter by RFC
702 1928, so the reply packet should be read until the end to avoid errors at
703 subsequent protocol level.
705 +----+-----+-------+------+----------+----------+
706 |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
707 +----+-----+-------+------+----------+----------+
708 | 1 | 1 | X'00' | 1 | Variable | 2 |
709 +----+-----+-------+------+----------+----------+
712 o IP v4 address: X'01', BND.ADDR = 4 byte
713 o domain name: X'03', BND.ADDR = [ 1 byte length, string ]
714 o IP v6 address: X'04', BND.ADDR = 16 byte
717 /* Calculate real packet size */
718 if(socksreq[3] == 3) {
720 int addrlen = (int) socksreq[4];
721 len = 5 + addrlen + 2;
723 else if(socksreq[3] == 4) {
728 /* At this point we already read first 10 bytes */
729 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
730 if(!conn->socks5_gssapi_enctype) {
731 /* decrypt_gssapi_blockread already read the whole packet */
735 result = Curl_blockread_all(conn, sock, (char *)&socksreq[10],
737 if((result != CURLE_OK) || (len != actualread)) {
738 failf(data, "Failed to receive SOCKS5 connect request ack.");
739 return CURLE_COULDNT_CONNECT;
742 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
746 curlx_nonblock(sock, TRUE);
747 return CURLE_OK; /* Proxy was successful! */
750 #endif /* CURL_DISABLE_PROXY */