2006-09-23 15:09:39 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2020-02-14 10:16:54 -05:00
|
|
|
* Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2006-09-23 15:09:39 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2006-09-23 15:09:39 -04:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
2013-01-06 13:06:49 -05:00
|
|
|
#include "curl_setup.h"
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2012-09-06 14:51:30 -04:00
|
|
|
#if !defined(CURL_DISABLE_PROXY)
|
2006-09-24 19:55:53 -04:00
|
|
|
|
2007-02-21 13:05:38 -05:00
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
2007-02-20 12:31:20 -05:00
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
2006-09-24 19:55:53 -04:00
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "select.h"
|
|
|
|
#include "connect.h"
|
|
|
|
#include "timeval.h"
|
|
|
|
#include "socks.h"
|
2020-02-14 10:16:54 -05:00
|
|
|
#include "multiif.h" /* for getsock macros */
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2019-09-21 03:39:21 -04:00
|
|
|
/* The last 3 #include files should be in this order */
|
|
|
|
#include "curl_printf.h"
|
|
|
|
#include "curl_memory.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h"
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
|
2006-09-23 15:07:20 -04:00
|
|
|
/*
|
|
|
|
* Helper read-from-socket functions. Does the same as Curl_read() but it
|
|
|
|
* blocks until all bytes amount of buffersize will be read. No more, no less.
|
|
|
|
*
|
2020-02-14 10:16:54 -05:00
|
|
|
* This is STUPID BLOCKING behavior. Only used by the SOCKS GSSAPI functions.
|
2006-09-23 15:07:20 -04:00
|
|
|
*/
|
2009-01-28 16:33:58 -05:00
|
|
|
int Curl_blockread_all(struct connectdata *conn, /* connection data */
|
|
|
|
curl_socket_t sockfd, /* read from this socket */
|
|
|
|
char *buf, /* store read data here */
|
|
|
|
ssize_t buffersize, /* max amount to read */
|
2011-08-08 05:10:17 -04:00
|
|
|
ssize_t *n) /* amount bytes read */
|
2006-09-23 15:07:20 -04:00
|
|
|
{
|
2019-03-05 03:37:53 -05:00
|
|
|
ssize_t nread = 0;
|
2006-09-23 15:07:20 -04:00
|
|
|
ssize_t allread = 0;
|
|
|
|
int result;
|
|
|
|
*n = 0;
|
2010-02-02 11:25:07 -05:00
|
|
|
for(;;) {
|
2020-04-11 23:19:55 -04:00
|
|
|
timediff_t timeout_ms = Curl_timeleft(conn->data, NULL, TRUE);
|
|
|
|
if(timeout_ms < 0) {
|
2006-09-23 15:07:20 -04:00
|
|
|
/* we already got the timeout */
|
2010-05-07 09:05:34 -04:00
|
|
|
result = CURLE_OPERATION_TIMEDOUT;
|
2006-10-26 22:18:29 -04:00
|
|
|
break;
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
2020-04-15 04:27:20 -04:00
|
|
|
if(!timeout_ms)
|
2020-06-01 02:49:20 -04:00
|
|
|
timeout_ms = TIMEDIFF_T_MAX;
|
2020-04-15 04:27:20 -04:00
|
|
|
if(SOCKET_READABLE(sockfd, timeout_ms) <= 0) {
|
2006-10-26 22:18:29 -04:00
|
|
|
result = ~CURLE_OK;
|
|
|
|
break;
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
2008-09-22 19:12:00 -04:00
|
|
|
result = Curl_read_plain(sockfd, buf, buffersize, &nread);
|
2010-05-07 09:05:34 -04:00
|
|
|
if(CURLE_AGAIN == result)
|
|
|
|
continue;
|
2017-03-10 08:28:37 -05:00
|
|
|
if(result)
|
2006-10-26 22:18:29 -04:00
|
|
|
break;
|
2006-09-23 15:07:20 -04:00
|
|
|
|
|
|
|
if(buffersize == nread) {
|
|
|
|
allread += nread;
|
|
|
|
*n = allread;
|
2006-10-26 22:18:29 -04:00
|
|
|
result = CURLE_OK;
|
|
|
|
break;
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
2007-06-05 09:42:23 -04:00
|
|
|
if(!nread) {
|
|
|
|
result = ~CURLE_OK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-09-23 15:07:20 -04:00
|
|
|
buffersize -= nread;
|
|
|
|
buf += nread;
|
|
|
|
allread += nread;
|
2010-02-02 11:25:07 -05:00
|
|
|
}
|
2006-10-26 22:18:29 -04:00
|
|
|
return result;
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
2020-02-14 10:16:54 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef DEBUGBUILD
|
|
|
|
#define sxstate(x,y) socksstate(x,y)
|
|
|
|
#else
|
|
|
|
#define sxstate(x,y) socksstate(x,y, __LINE__)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* always use this function to change state, to make debugging easier */
|
|
|
|
static void socksstate(struct connectdata *conn,
|
|
|
|
enum connect_t state
|
|
|
|
#ifdef DEBUGBUILD
|
|
|
|
, int lineno
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
|
|
|
enum connect_t oldstate = conn->cnnct.state;
|
|
|
|
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
|
|
|
|
/* synced with the state list in urldata.h */
|
|
|
|
static const char * const statename[] = {
|
|
|
|
"INIT",
|
|
|
|
"SOCKS_INIT",
|
|
|
|
"SOCKS_SEND",
|
|
|
|
"SOCKS_READ_INIT",
|
|
|
|
"SOCKS_READ",
|
|
|
|
"GSSAPI_INIT",
|
|
|
|
"AUTH_INIT",
|
|
|
|
"AUTH_SEND",
|
|
|
|
"AUTH_READ",
|
|
|
|
"REQ_INIT",
|
|
|
|
"RESOLVING",
|
|
|
|
"RESOLVED",
|
|
|
|
"RESOLVE_REMOTE",
|
|
|
|
"REQ_SEND",
|
|
|
|
"REQ_SENDING",
|
|
|
|
"REQ_READ",
|
|
|
|
"REQ_READ_MORE",
|
|
|
|
"DONE"
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(oldstate == state)
|
|
|
|
/* don't bother when the new state is the same as the old state */
|
|
|
|
return;
|
|
|
|
|
|
|
|
conn->cnnct.state = state;
|
|
|
|
|
|
|
|
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
|
|
|
|
infof(conn->data,
|
|
|
|
"SXSTATE: %s => %s conn %p; line %d\n",
|
|
|
|
statename[oldstate], statename[conn->cnnct.state], conn,
|
|
|
|
lineno);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int Curl_SOCKS_getsock(struct connectdata *conn, curl_socket_t *sock,
|
|
|
|
int sockindex)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
sock[0] = conn->sock[sockindex];
|
|
|
|
switch(conn->cnnct.state) {
|
|
|
|
case CONNECT_RESOLVING:
|
|
|
|
case CONNECT_SOCKS_READ:
|
|
|
|
case CONNECT_AUTH_READ:
|
|
|
|
case CONNECT_REQ_READ:
|
|
|
|
case CONNECT_REQ_READ_MORE:
|
|
|
|
rc = GETSOCK_READSOCK(0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rc = GETSOCK_WRITESOCK(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
2006-09-23 15:07:20 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This function logs in to a SOCKS4 proxy and sends the specifics to the final
|
|
|
|
* destination server.
|
|
|
|
*
|
|
|
|
* Reference :
|
2018-09-22 03:58:05 -04:00
|
|
|
* https://www.openssh.com/txt/socks4.protocol
|
2006-09-23 15:07:20 -04:00
|
|
|
*
|
|
|
|
* Note :
|
2008-01-02 16:40:11 -05:00
|
|
|
* Set protocol4a=true for "SOCKS 4A (Simple Extension to SOCKS 4 Protocol)"
|
2006-09-23 15:07:20 -04:00
|
|
|
* Nonsupport "Identification Protocol (RFC1413)"
|
|
|
|
*/
|
2020-08-24 02:39:29 -04:00
|
|
|
CURLproxycode Curl_SOCKS4(const char *proxy_user,
|
|
|
|
const char *hostname,
|
|
|
|
int remote_port,
|
|
|
|
int sockindex,
|
|
|
|
struct connectdata *conn,
|
|
|
|
bool *done)
|
2006-09-23 15:07:20 -04:00
|
|
|
{
|
2016-11-16 12:49:15 -05:00
|
|
|
const bool protocol4a =
|
|
|
|
(conn->socks_proxy.proxytype == CURLPROXY_SOCKS4A) ? TRUE : FALSE;
|
2020-02-14 10:16:54 -05:00
|
|
|
unsigned char *socksreq = &conn->cnnct.socksreq[0];
|
|
|
|
CURLcode result;
|
|
|
|
curl_socket_t sockfd = conn->sock[sockindex];
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2020-02-14 10:16:54 -05:00
|
|
|
struct connstate *sx = &conn->cnnct;
|
|
|
|
struct Curl_dns_entry *dns = NULL;
|
|
|
|
ssize_t actualread;
|
|
|
|
ssize_t written;
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
if(!SOCKS_STATE(sx->state) && !*done)
|
|
|
|
sxstate(conn, CONNECT_SOCKS_INIT);
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
switch(sx->state) {
|
|
|
|
case CONNECT_SOCKS_INIT:
|
2020-03-08 12:24:50 -04:00
|
|
|
/* SOCKS4 can only do IPv4, insist! */
|
|
|
|
conn->ip_version = CURL_IPRESOLVE_V4;
|
2020-02-14 10:16:54 -05:00
|
|
|
if(conn->bits.httpproxy)
|
|
|
|
infof(conn->data, "SOCKS4%s: connecting to HTTP proxy %s port %d\n",
|
|
|
|
protocol4a ? "a" : "", hostname, remote_port);
|
2013-10-26 14:19:27 -04:00
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
infof(data, "SOCKS4 communication to %s:%d\n", hostname, remote_port);
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
/*
|
|
|
|
* Compose socks4 request
|
|
|
|
*
|
|
|
|
* Request format
|
|
|
|
*
|
|
|
|
* +----+----+----+----+----+----+----+----+----+----+....+----+
|
|
|
|
* | VN | CD | DSTPORT | DSTIP | USERID |NULL|
|
|
|
|
* +----+----+----+----+----+----+----+----+----+----+....+----+
|
|
|
|
* # of bytes: 1 1 2 4 variable 1
|
|
|
|
*/
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
socksreq[0] = 4; /* version (SOCKS4) */
|
|
|
|
socksreq[1] = 1; /* connect */
|
|
|
|
socksreq[2] = (unsigned char)((remote_port >> 8) & 0xff); /* PORT MSB */
|
|
|
|
socksreq[3] = (unsigned char)(remote_port & 0xff); /* PORT LSB */
|
|
|
|
|
|
|
|
/* DNS resolve only for SOCKS4, not SOCKS4a */
|
|
|
|
if(!protocol4a) {
|
|
|
|
enum resolve_t rc =
|
|
|
|
Curl_resolv(conn, hostname, remote_port, FALSE, &dns);
|
|
|
|
|
|
|
|
if(rc == CURLRESOLV_ERROR)
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_RESOLVE_HOST;
|
2020-02-14 10:16:54 -05:00
|
|
|
else if(rc == CURLRESOLV_PENDING) {
|
|
|
|
sxstate(conn, CONNECT_RESOLVING);
|
|
|
|
infof(data, "SOCKS4 non-blocking resolve of %s\n", hostname);
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_OK;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
|
|
|
sxstate(conn, CONNECT_RESOLVED);
|
|
|
|
goto CONNECT_RESOLVED;
|
|
|
|
}
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
/* socks4a doesn't resolve anything locally */
|
|
|
|
sxstate(conn, CONNECT_REQ_INIT);
|
|
|
|
goto CONNECT_REQ_INIT;
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
case CONNECT_RESOLVING:
|
|
|
|
/* check if we have the name resolved by now */
|
|
|
|
dns = Curl_fetch_addr(conn, hostname, (int)conn->port);
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
if(dns) {
|
|
|
|
#ifdef CURLRES_ASYNCH
|
|
|
|
conn->async.dns = dns;
|
|
|
|
conn->async.done = TRUE;
|
|
|
|
#endif
|
|
|
|
infof(data, "Hostname '%s' was found\n", hostname);
|
|
|
|
sxstate(conn, CONNECT_RESOLVED);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = Curl_resolv_check(data->conn, &dns);
|
2020-08-24 02:39:29 -04:00
|
|
|
if(!dns) {
|
|
|
|
if(result)
|
|
|
|
return CURLPX_RESOLVE_HOST;
|
|
|
|
return CURLPX_OK;
|
|
|
|
}
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
CONNECT_RESOLVED:
|
|
|
|
case CONNECT_RESOLVED: {
|
2020-05-13 18:05:04 -04:00
|
|
|
struct Curl_addrinfo *hp = NULL;
|
2020-02-14 10:16:54 -05:00
|
|
|
char buf[64];
|
2006-09-23 15:07:20 -04:00
|
|
|
/*
|
|
|
|
* We cannot use 'hostent' as a struct that Curl_resolv() returns. It
|
|
|
|
* returns a Curl_addrinfo pointer that may not always look the same.
|
|
|
|
*/
|
|
|
|
if(dns)
|
2017-09-09 17:09:06 -04:00
|
|
|
hp = dns->addr;
|
2007-11-05 04:45:09 -05:00
|
|
|
if(hp) {
|
2006-09-23 15:07:20 -04:00
|
|
|
Curl_printable_address(hp, buf, sizeof(buf));
|
|
|
|
|
2016-08-20 15:12:34 -04:00
|
|
|
if(hp->ai_family == AF_INET) {
|
|
|
|
struct sockaddr_in *saddr_in;
|
|
|
|
|
2016-11-23 02:30:18 -05:00
|
|
|
saddr_in = (struct sockaddr_in *)(void *)hp->ai_addr;
|
|
|
|
socksreq[4] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[0];
|
|
|
|
socksreq[5] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[1];
|
|
|
|
socksreq[6] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[2];
|
|
|
|
socksreq[7] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[3];
|
2016-08-20 15:12:34 -04:00
|
|
|
|
|
|
|
infof(data, "SOCKS4 connect to IPv4 %s (locally resolved)\n", buf);
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
2016-08-20 15:12:34 -04:00
|
|
|
else {
|
2006-09-23 15:07:20 -04:00
|
|
|
hp = NULL; /* fail! */
|
2016-08-20 15:12:34 -04:00
|
|
|
failf(data, "SOCKS4 connection to %s not supported\n", buf);
|
|
|
|
}
|
2013-10-26 14:19:27 -04:00
|
|
|
|
2006-09-23 15:07:20 -04:00
|
|
|
Curl_resolv_unlock(data, dns); /* not used anymore from now on */
|
|
|
|
}
|
|
|
|
if(!hp) {
|
|
|
|
failf(data, "Failed to resolve \"%s\" for SOCKS4 connect.",
|
2007-02-19 06:53:54 -05:00
|
|
|
hostname);
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_RESOLVE_HOST;
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
|
|
|
}
|
2020-02-14 10:16:54 -05:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
CONNECT_REQ_INIT:
|
|
|
|
case CONNECT_REQ_INIT:
|
|
|
|
/*
|
|
|
|
* This is currently not supporting "Identification Protocol (RFC1413)".
|
|
|
|
*/
|
|
|
|
socksreq[8] = 0; /* ensure empty userid is NUL-terminated */
|
|
|
|
if(proxy_user) {
|
|
|
|
size_t plen = strlen(proxy_user);
|
|
|
|
if(plen >= sizeof(sx->socksreq) - 8) {
|
2020-08-24 02:39:29 -04:00
|
|
|
failf(data, "Too long SOCKS proxy user name, can't use!\n");
|
|
|
|
return CURLPX_LONG_USER;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
|
|
|
/* copy the proxy name WITH trailing zero */
|
|
|
|
memcpy(socksreq + 8, proxy_user, plen + 1);
|
2013-02-13 07:18:43 -05:00
|
|
|
}
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
/*
|
|
|
|
* Make connection
|
|
|
|
*/
|
|
|
|
{
|
2020-07-05 08:19:25 -04:00
|
|
|
size_t packetsize = 9 +
|
2020-02-14 10:16:54 -05:00
|
|
|
strlen((char *)socksreq + 8); /* size including NUL */
|
|
|
|
|
|
|
|
/* If SOCKS4a, set special invalid IP address 0.0.0.x */
|
|
|
|
if(protocol4a) {
|
2020-07-05 08:19:25 -04:00
|
|
|
size_t hostnamelen = 0;
|
2020-02-14 10:16:54 -05:00
|
|
|
socksreq[4] = 0;
|
|
|
|
socksreq[5] = 0;
|
|
|
|
socksreq[6] = 0;
|
|
|
|
socksreq[7] = 1;
|
|
|
|
/* append hostname */
|
2020-07-05 08:19:25 -04:00
|
|
|
hostnamelen = strlen(hostname) + 1; /* length including NUL */
|
2020-02-14 10:16:54 -05:00
|
|
|
if(hostnamelen <= 255)
|
|
|
|
strcpy((char *)socksreq + packetsize, hostname);
|
|
|
|
else {
|
|
|
|
failf(data, "SOCKS4: too long host name");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_LONG_HOSTNAME;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
|
|
|
packetsize += hostnamelen;
|
|
|
|
}
|
|
|
|
sx->outp = socksreq;
|
|
|
|
sx->outstanding = packetsize;
|
|
|
|
sxstate(conn, CONNECT_REQ_SENDING);
|
2008-01-02 16:40:11 -05:00
|
|
|
}
|
2020-02-14 10:16:54 -05:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case CONNECT_REQ_SENDING:
|
2006-09-23 15:07:20 -04:00
|
|
|
/* Send request */
|
2020-02-14 10:16:54 -05:00
|
|
|
result = Curl_write_plain(conn, sockfd, (char *)sx->outp,
|
|
|
|
sx->outstanding, &written);
|
|
|
|
if(result && (CURLE_AGAIN != result)) {
|
2006-09-23 15:07:20 -04:00
|
|
|
failf(data, "Failed to send SOCKS4 connect request.");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_SEND_CONNECT;
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
2020-02-14 10:16:54 -05:00
|
|
|
if(written != sx->outstanding) {
|
|
|
|
/* not done, remain in state */
|
|
|
|
sx->outstanding -= written;
|
|
|
|
sx->outp += written;
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_OK;
|
2008-01-02 16:40:11 -05:00
|
|
|
}
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
/* done sending! */
|
|
|
|
sx->outstanding = 8; /* receive data size */
|
|
|
|
sx->outp = socksreq;
|
|
|
|
sxstate(conn, CONNECT_SOCKS_READ);
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case CONNECT_SOCKS_READ:
|
2006-09-23 15:07:20 -04:00
|
|
|
/* Receive response */
|
2020-02-14 10:16:54 -05:00
|
|
|
result = Curl_read_plain(sockfd, (char *)sx->outp,
|
|
|
|
sx->outstanding, &actualread);
|
|
|
|
if(result && (CURLE_AGAIN != result)) {
|
|
|
|
failf(data, "SOCKS4: Failed receiving connect request ack: %s",
|
|
|
|
curl_easy_strerror(result));
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_RECV_CONNECT;
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
2020-06-08 08:05:22 -04:00
|
|
|
else if(!result && !actualread) {
|
|
|
|
/* connection closed */
|
|
|
|
failf(data, "connection to proxy closed");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_CLOSED;
|
2020-06-08 08:05:22 -04:00
|
|
|
}
|
2020-02-14 10:16:54 -05:00
|
|
|
else if(actualread != sx->outstanding) {
|
|
|
|
/* remain in reading state */
|
|
|
|
sx->outstanding -= actualread;
|
|
|
|
sx->outp += actualread;
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_OK;
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
2020-02-14 10:16:54 -05:00
|
|
|
sxstate(conn, CONNECT_DONE);
|
|
|
|
break;
|
|
|
|
default: /* lots of unused states in SOCKS4 */
|
|
|
|
break;
|
|
|
|
}
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
/*
|
|
|
|
* Response format
|
|
|
|
*
|
|
|
|
* +----+----+----+----+----+----+----+----+
|
|
|
|
* | VN | CD | DSTPORT | DSTIP |
|
|
|
|
* +----+----+----+----+----+----+----+----+
|
|
|
|
* # of bytes: 1 1 2 4
|
|
|
|
*
|
|
|
|
* VN is the version of the reply code and should be 0. CD is the result
|
|
|
|
* code with one of the following values:
|
|
|
|
*
|
|
|
|
* 90: request granted
|
|
|
|
* 91: request rejected or failed
|
|
|
|
* 92: request rejected because SOCKS server cannot connect to
|
|
|
|
* identd on the client
|
|
|
|
* 93: request rejected because the client program and identd
|
|
|
|
* report different user-ids
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* wrong version ? */
|
|
|
|
if(socksreq[0] != 0) {
|
|
|
|
failf(data,
|
|
|
|
"SOCKS4 reply has wrong version, version should be 0.");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_BAD_VERSION;
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
/* Result */
|
|
|
|
switch(socksreq[1]) {
|
|
|
|
case 90:
|
|
|
|
infof(data, "SOCKS4%s request granted.\n", protocol4a?"a":"");
|
|
|
|
break;
|
|
|
|
case 91:
|
|
|
|
failf(data,
|
|
|
|
"Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
|
|
|
|
", request rejected or failed.",
|
|
|
|
(unsigned char)socksreq[4], (unsigned char)socksreq[5],
|
|
|
|
(unsigned char)socksreq[6], (unsigned char)socksreq[7],
|
|
|
|
(((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
|
|
|
|
(unsigned char)socksreq[1]);
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_REQUEST_FAILED;
|
2020-02-14 10:16:54 -05:00
|
|
|
case 92:
|
|
|
|
failf(data,
|
|
|
|
"Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
|
|
|
|
", request rejected because SOCKS server cannot connect to "
|
|
|
|
"identd on the client.",
|
|
|
|
(unsigned char)socksreq[4], (unsigned char)socksreq[5],
|
|
|
|
(unsigned char)socksreq[6], (unsigned char)socksreq[7],
|
|
|
|
(((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
|
|
|
|
(unsigned char)socksreq[1]);
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_IDENTD;
|
2020-02-14 10:16:54 -05:00
|
|
|
case 93:
|
|
|
|
failf(data,
|
|
|
|
"Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
|
|
|
|
", request rejected because the client program and identd "
|
|
|
|
"report different user-ids.",
|
|
|
|
(unsigned char)socksreq[4], (unsigned char)socksreq[5],
|
|
|
|
(unsigned char)socksreq[6], (unsigned char)socksreq[7],
|
|
|
|
(((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
|
|
|
|
(unsigned char)socksreq[1]);
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_IDENTD_DIFFER;
|
2020-02-14 10:16:54 -05:00
|
|
|
default:
|
|
|
|
failf(data,
|
|
|
|
"Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
|
|
|
|
", Unknown.",
|
|
|
|
(unsigned char)socksreq[4], (unsigned char)socksreq[5],
|
|
|
|
(unsigned char)socksreq[6], (unsigned char)socksreq[7],
|
|
|
|
(((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
|
|
|
|
(unsigned char)socksreq[1]);
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_UNKNOWN_FAIL;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
*done = TRUE;
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_OK; /* Proxy was successful! */
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function logs in to a SOCKS5 proxy and sends the specifics to the final
|
|
|
|
* destination server.
|
|
|
|
*/
|
2020-08-24 02:39:29 -04:00
|
|
|
CURLproxycode Curl_SOCKS5(const char *proxy_user,
|
|
|
|
const char *proxy_password,
|
|
|
|
const char *hostname,
|
|
|
|
int remote_port,
|
|
|
|
int sockindex,
|
|
|
|
struct connectdata *conn,
|
|
|
|
bool *done)
|
2006-09-23 15:07:20 -04:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
According to the RFC1928, section "6. Replies". This is what a SOCK5
|
|
|
|
replies:
|
|
|
|
|
|
|
|
+----+-----+-------+------+----------+----------+
|
|
|
|
|VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
|
|
|
|
+----+-----+-------+------+----------+----------+
|
|
|
|
| 1 | 1 | X'00' | 1 | Variable | 2 |
|
|
|
|
+----+-----+-------+------+----------+----------+
|
|
|
|
|
|
|
|
Where:
|
|
|
|
|
|
|
|
o VER protocol version: X'05'
|
|
|
|
o REP Reply field:
|
|
|
|
o X'00' succeeded
|
|
|
|
*/
|
2020-02-14 10:16:54 -05:00
|
|
|
unsigned char *socksreq = &conn->cnnct.socksreq[0];
|
|
|
|
char dest[256] = "unknown"; /* printable hostname:port */
|
2017-03-08 06:16:01 -05:00
|
|
|
int idx;
|
2006-09-23 15:07:20 -04:00
|
|
|
ssize_t actualread;
|
|
|
|
ssize_t written;
|
2020-02-14 10:16:54 -05:00
|
|
|
CURLcode result;
|
|
|
|
curl_socket_t sockfd = conn->sock[sockindex];
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2016-11-16 12:49:15 -05:00
|
|
|
bool socks5_resolve_local =
|
|
|
|
(conn->socks_proxy.proxytype == CURLPROXY_SOCKS5) ? TRUE : FALSE;
|
2008-01-04 18:01:00 -05:00
|
|
|
const size_t hostname_len = strlen(hostname);
|
2012-10-04 15:27:46 -04:00
|
|
|
ssize_t len = 0;
|
2017-04-27 09:18:49 -04:00
|
|
|
const unsigned long auth = data->set.socks5auth;
|
|
|
|
bool allow_gssapi = FALSE;
|
2020-02-14 10:16:54 -05:00
|
|
|
struct connstate *sx = &conn->cnnct;
|
|
|
|
struct Curl_dns_entry *dns = NULL;
|
|
|
|
|
|
|
|
if(!SOCKS_STATE(sx->state) && !*done)
|
|
|
|
sxstate(conn, CONNECT_SOCKS_INIT);
|
|
|
|
|
|
|
|
switch(sx->state) {
|
|
|
|
case CONNECT_SOCKS_INIT:
|
|
|
|
if(conn->bits.httpproxy)
|
|
|
|
infof(conn->data, "SOCKS5: connecting to HTTP proxy %s port %d\n",
|
|
|
|
hostname, remote_port);
|
|
|
|
|
|
|
|
/* RFC1928 chapter 5 specifies max 255 chars for domain name in packet */
|
|
|
|
if(!socks5_resolve_local && hostname_len > 255) {
|
|
|
|
infof(conn->data, "SOCKS5: server resolving disabled for hostnames of "
|
|
|
|
"length > 255 [actual len=%zu]\n", hostname_len);
|
|
|
|
socks5_resolve_local = TRUE;
|
|
|
|
}
|
2008-01-04 18:01:00 -05:00
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
if(auth & ~(CURLAUTH_BASIC | CURLAUTH_GSSAPI))
|
|
|
|
infof(conn->data,
|
|
|
|
"warning: unsupported value passed to CURLOPT_SOCKS5_AUTH: %lu\n",
|
|
|
|
auth);
|
|
|
|
if(!(auth & CURLAUTH_BASIC))
|
|
|
|
/* disable username/password auth */
|
|
|
|
proxy_user = NULL;
|
2017-04-27 09:18:49 -04:00
|
|
|
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
|
2020-02-14 10:16:54 -05:00
|
|
|
if(auth & CURLAUTH_GSSAPI)
|
|
|
|
allow_gssapi = TRUE;
|
2017-04-27 09:18:49 -04:00
|
|
|
#endif
|
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
idx = 0;
|
|
|
|
socksreq[idx++] = 5; /* version */
|
|
|
|
idx++; /* number of authentication methods */
|
|
|
|
socksreq[idx++] = 0; /* no authentication */
|
|
|
|
if(allow_gssapi)
|
|
|
|
socksreq[idx++] = 1; /* GSS-API */
|
|
|
|
if(proxy_user)
|
|
|
|
socksreq[idx++] = 2; /* username/password */
|
|
|
|
/* write the number of authentication methods */
|
|
|
|
socksreq[1] = (unsigned char) (idx - 2);
|
|
|
|
|
|
|
|
result = Curl_write_plain(conn, sockfd, (char *)socksreq, idx, &written);
|
|
|
|
if(result && (CURLE_AGAIN != result)) {
|
|
|
|
failf(data, "Unable to send initial SOCKS5 request.");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_SEND_CONNECT;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
|
|
|
if(written != idx) {
|
|
|
|
sxstate(conn, CONNECT_SOCKS_SEND);
|
|
|
|
sx->outstanding = idx - written;
|
|
|
|
sx->outp = &socksreq[written];
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_OK;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
|
|
|
sxstate(conn, CONNECT_SOCKS_READ);
|
|
|
|
goto CONNECT_SOCKS_READ_INIT;
|
|
|
|
case CONNECT_SOCKS_SEND:
|
|
|
|
result = Curl_write_plain(conn, sockfd, (char *)sx->outp,
|
|
|
|
sx->outstanding, &written);
|
|
|
|
if(result && (CURLE_AGAIN != result)) {
|
|
|
|
failf(data, "Unable to send initial SOCKS5 request.");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_SEND_CONNECT;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
|
|
|
if(written != sx->outstanding) {
|
|
|
|
/* not done, remain in state */
|
|
|
|
sx->outstanding -= written;
|
|
|
|
sx->outp += written;
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_OK;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
CONNECT_SOCKS_READ_INIT:
|
|
|
|
case CONNECT_SOCKS_READ_INIT:
|
|
|
|
sx->outstanding = 2; /* expect two bytes */
|
|
|
|
sx->outp = socksreq; /* store it here */
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case CONNECT_SOCKS_READ:
|
|
|
|
result = Curl_read_plain(sockfd, (char *)sx->outp,
|
|
|
|
sx->outstanding, &actualread);
|
|
|
|
if(result && (CURLE_AGAIN != result)) {
|
|
|
|
failf(data, "Unable to receive initial SOCKS5 response.");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_RECV_CONNECT;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
2020-06-08 08:05:22 -04:00
|
|
|
else if(!result && !actualread) {
|
|
|
|
/* connection closed */
|
|
|
|
failf(data, "Connection to proxy closed");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_CLOSED;
|
2020-06-08 08:05:22 -04:00
|
|
|
}
|
2020-02-14 10:16:54 -05:00
|
|
|
else if(actualread != sx->outstanding) {
|
|
|
|
/* remain in reading state */
|
|
|
|
sx->outstanding -= actualread;
|
|
|
|
sx->outp += actualread;
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_OK;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
|
|
|
else if(socksreq[0] != 5) {
|
|
|
|
failf(data, "Received invalid version in initial SOCKS5 response.");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_BAD_VERSION;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
|
|
|
else if(socksreq[1] == 0) {
|
|
|
|
/* DONE! No authentication needed. Send request. */
|
|
|
|
sxstate(conn, CONNECT_REQ_INIT);
|
|
|
|
goto CONNECT_REQ_INIT;
|
|
|
|
}
|
|
|
|
else if(socksreq[1] == 2) {
|
|
|
|
/* regular name + password authentication */
|
|
|
|
sxstate(conn, CONNECT_AUTH_INIT);
|
|
|
|
goto CONNECT_AUTH_INIT;
|
|
|
|
}
|
2009-01-28 16:33:58 -05:00
|
|
|
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
|
2020-02-14 10:16:54 -05:00
|
|
|
else if(allow_gssapi && (socksreq[1] == 1)) {
|
|
|
|
sxstate(conn, CONNECT_GSSAPI_INIT);
|
|
|
|
result = Curl_SOCKS5_gssapi_negotiate(sockindex, conn);
|
|
|
|
if(result) {
|
|
|
|
failf(data, "Unable to negotiate SOCKS5 GSS-API context.");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_GSSAPI;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else {
|
|
|
|
/* error */
|
|
|
|
if(!allow_gssapi && (socksreq[1] == 1)) {
|
|
|
|
failf(data,
|
|
|
|
"SOCKS5 GSSAPI per-message authentication is not supported.");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_GSSAPI_PERMSG;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
|
|
|
else if(socksreq[1] == 255) {
|
|
|
|
failf(data, "No authentication method was acceptable.");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_NO_AUTH;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
2009-01-28 16:33:58 -05:00
|
|
|
}
|
2020-06-08 02:42:25 -04:00
|
|
|
failf(data,
|
|
|
|
"Undocumented SOCKS5 mode attempted to be used by server.");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_UNKNOWN_MODE;
|
2020-02-14 10:16:54 -05:00
|
|
|
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
|
|
|
|
case CONNECT_GSSAPI_INIT:
|
|
|
|
/* GSSAPI stuff done non-blocking */
|
|
|
|
break;
|
2009-01-28 16:33:58 -05:00
|
|
|
#endif
|
2020-02-14 10:16:54 -05:00
|
|
|
|
|
|
|
default: /* do nothing! */
|
|
|
|
break;
|
|
|
|
|
|
|
|
CONNECT_AUTH_INIT:
|
|
|
|
case CONNECT_AUTH_INIT: {
|
2006-09-23 15:07:20 -04:00
|
|
|
/* Needs user name and password */
|
2017-03-08 06:21:09 -05:00
|
|
|
size_t proxy_user_len, proxy_password_len;
|
|
|
|
if(proxy_user && proxy_password) {
|
|
|
|
proxy_user_len = strlen(proxy_user);
|
2012-10-04 14:17:49 -04:00
|
|
|
proxy_password_len = strlen(proxy_password);
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
|
|
|
else {
|
2017-03-08 06:21:09 -05:00
|
|
|
proxy_user_len = 0;
|
2012-10-04 14:17:49 -04:00
|
|
|
proxy_password_len = 0;
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* username/password request looks like
|
|
|
|
* +----+------+----------+------+----------+
|
|
|
|
* |VER | ULEN | UNAME | PLEN | PASSWD |
|
|
|
|
* +----+------+----------+------+----------+
|
|
|
|
* | 1 | 1 | 1 to 255 | 1 | 1 to 255 |
|
|
|
|
* +----+------+----------+------+----------+
|
|
|
|
*/
|
|
|
|
len = 0;
|
|
|
|
socksreq[len++] = 1; /* username/pw subnegotiation version */
|
2017-03-08 06:21:09 -05:00
|
|
|
socksreq[len++] = (unsigned char) proxy_user_len;
|
2019-04-05 16:50:22 -04:00
|
|
|
if(proxy_user && proxy_user_len) {
|
|
|
|
/* the length must fit in a single byte */
|
|
|
|
if(proxy_user_len >= 255) {
|
|
|
|
failf(data, "Excessive user name length for proxy auth");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_LONG_USER;
|
2019-04-05 16:50:22 -04:00
|
|
|
}
|
2017-03-08 06:21:09 -05:00
|
|
|
memcpy(socksreq + len, proxy_user, proxy_user_len);
|
2019-04-05 16:50:22 -04:00
|
|
|
}
|
2017-03-08 06:21:09 -05:00
|
|
|
len += proxy_user_len;
|
2012-10-04 14:17:49 -04:00
|
|
|
socksreq[len++] = (unsigned char) proxy_password_len;
|
2019-04-05 16:50:22 -04:00
|
|
|
if(proxy_password && proxy_password_len) {
|
|
|
|
/* the length must fit in a single byte */
|
|
|
|
if(proxy_password_len > 255) {
|
|
|
|
failf(data, "Excessive password length for proxy auth");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_LONG_PASSWD;
|
2019-04-05 16:50:22 -04:00
|
|
|
}
|
2012-10-04 14:17:49 -04:00
|
|
|
memcpy(socksreq + len, proxy_password, proxy_password_len);
|
2019-04-05 16:50:22 -04:00
|
|
|
}
|
2012-10-04 14:17:49 -04:00
|
|
|
len += proxy_password_len;
|
2020-02-14 10:16:54 -05:00
|
|
|
sxstate(conn, CONNECT_AUTH_SEND);
|
|
|
|
sx->outstanding = len;
|
|
|
|
sx->outp = socksreq;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case CONNECT_AUTH_SEND:
|
|
|
|
result = Curl_write_plain(conn, sockfd, (char *)sx->outp,
|
|
|
|
sx->outstanding, &written);
|
|
|
|
if(result && (CURLE_AGAIN != result)) {
|
2006-09-23 15:07:20 -04:00
|
|
|
failf(data, "Failed to send SOCKS5 sub-negotiation request.");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_SEND_AUTH;
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
2020-02-14 10:16:54 -05:00
|
|
|
if(sx->outstanding != written) {
|
|
|
|
/* remain in state */
|
|
|
|
sx->outstanding -= written;
|
|
|
|
sx->outp += written;
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_OK;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
|
|
|
sx->outp = socksreq;
|
|
|
|
sx->outstanding = 2;
|
|
|
|
sxstate(conn, CONNECT_AUTH_READ);
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case CONNECT_AUTH_READ:
|
|
|
|
result = Curl_read_plain(sockfd, (char *)sx->outp,
|
|
|
|
sx->outstanding, &actualread);
|
|
|
|
if(result && (CURLE_AGAIN != result)) {
|
2006-09-23 15:07:20 -04:00
|
|
|
failf(data, "Unable to receive SOCKS5 sub-negotiation response.");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_RECV_AUTH;
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
2020-06-08 08:05:22 -04:00
|
|
|
else if(!result && !actualread) {
|
|
|
|
/* connection closed */
|
|
|
|
failf(data, "connection to proxy closed");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_CLOSED;
|
2020-06-08 08:05:22 -04:00
|
|
|
}
|
|
|
|
else if(actualread != sx->outstanding) {
|
2020-02-14 10:16:54 -05:00
|
|
|
/* remain in state */
|
|
|
|
sx->outstanding -= actualread;
|
|
|
|
sx->outp += actualread;
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_OK;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
2006-09-23 15:07:20 -04:00
|
|
|
/* ignore the first (VER) byte */
|
2020-06-08 08:05:22 -04:00
|
|
|
else if(socksreq[1] != 0) { /* status */
|
2006-09-23 15:07:20 -04:00
|
|
|
failf(data, "User was rejected by the SOCKS5 server (%d %d).",
|
|
|
|
socksreq[0], socksreq[1]);
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_USER_REJECTED;
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Everything is good so far, user was authenticated! */
|
2020-02-14 10:16:54 -05:00
|
|
|
sxstate(conn, CONNECT_REQ_INIT);
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
CONNECT_REQ_INIT:
|
|
|
|
case CONNECT_REQ_INIT:
|
|
|
|
if(socks5_resolve_local) {
|
|
|
|
enum resolve_t rc = Curl_resolv(conn, hostname, remote_port,
|
|
|
|
FALSE, &dns);
|
|
|
|
|
|
|
|
if(rc == CURLRESOLV_ERROR)
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_RESOLVE_HOST;
|
2020-02-14 10:16:54 -05:00
|
|
|
|
|
|
|
if(rc == CURLRESOLV_PENDING) {
|
|
|
|
sxstate(conn, CONNECT_RESOLVING);
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_OK;
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
2020-02-14 10:16:54 -05:00
|
|
|
sxstate(conn, CONNECT_RESOLVED);
|
|
|
|
goto CONNECT_RESOLVED;
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
2020-02-14 10:16:54 -05:00
|
|
|
goto CONNECT_RESOLVE_REMOTE;
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
case CONNECT_RESOLVING:
|
|
|
|
/* check if we have the name resolved by now */
|
|
|
|
dns = Curl_fetch_addr(conn, hostname, (int)conn->port);
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
if(dns) {
|
|
|
|
#ifdef CURLRES_ASYNCH
|
|
|
|
conn->async.dns = dns;
|
|
|
|
conn->async.done = TRUE;
|
|
|
|
#endif
|
|
|
|
infof(data, "SOCKS5: hostname '%s' found\n", hostname);
|
2010-04-16 16:58:04 -04:00
|
|
|
}
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
if(!dns) {
|
|
|
|
result = Curl_resolv_check(data->conn, &dns);
|
2020-08-24 02:39:29 -04:00
|
|
|
if(!dns) {
|
|
|
|
if(result)
|
|
|
|
return CURLPX_RESOLVE_HOST;
|
|
|
|
return CURLPX_OK;
|
|
|
|
}
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
CONNECT_RESOLVED:
|
|
|
|
case CONNECT_RESOLVED: {
|
2020-05-13 18:05:04 -04:00
|
|
|
struct Curl_addrinfo *hp = NULL;
|
2020-05-18 12:41:20 -04:00
|
|
|
size_t destlen;
|
2006-09-23 15:07:20 -04:00
|
|
|
if(dns)
|
2017-09-09 17:09:06 -04:00
|
|
|
hp = dns->addr;
|
2020-02-14 10:16:54 -05:00
|
|
|
if(!hp) {
|
|
|
|
failf(data, "Failed to resolve \"%s\" for SOCKS5 connect.",
|
|
|
|
hostname);
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_RESOLVE_HOST;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
2016-08-20 15:14:46 -04:00
|
|
|
|
2020-05-18 12:41:20 -04:00
|
|
|
Curl_printable_address(hp, dest, sizeof(dest));
|
|
|
|
destlen = strlen(dest);
|
|
|
|
msnprintf(dest + destlen, sizeof(dest) - destlen, ":%d", remote_port);
|
2016-08-20 15:14:46 -04:00
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
len = 0;
|
|
|
|
socksreq[len++] = 5; /* version (SOCKS5) */
|
|
|
|
socksreq[len++] = 1; /* connect */
|
|
|
|
socksreq[len++] = 0; /* must be zero */
|
|
|
|
if(hp->ai_family == AF_INET) {
|
|
|
|
int i;
|
|
|
|
struct sockaddr_in *saddr_in;
|
|
|
|
socksreq[len++] = 1; /* ATYP: IPv4 = 1 */
|
|
|
|
|
|
|
|
saddr_in = (struct sockaddr_in *)(void *)hp->ai_addr;
|
|
|
|
for(i = 0; i < 4; i++) {
|
|
|
|
socksreq[len++] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[i];
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
infof(data, "SOCKS5 connect to IPv4 %s (locally resolved)\n", dest);
|
|
|
|
}
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
else if(hp->ai_family == AF_INET6) {
|
|
|
|
int i;
|
|
|
|
struct sockaddr_in6 *saddr_in6;
|
|
|
|
socksreq[len++] = 4; /* ATYP: IPv6 = 4 */
|
|
|
|
|
|
|
|
saddr_in6 = (struct sockaddr_in6 *)(void *)hp->ai_addr;
|
|
|
|
for(i = 0; i < 16; i++) {
|
|
|
|
socksreq[len++] =
|
|
|
|
((unsigned char *)&saddr_in6->sin6_addr.s6_addr)[i];
|
2016-08-20 15:14:46 -04:00
|
|
|
}
|
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
infof(data, "SOCKS5 connect to IPv6 %s (locally resolved)\n", dest);
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
2020-02-14 10:16:54 -05:00
|
|
|
#endif
|
|
|
|
else {
|
|
|
|
hp = NULL; /* fail! */
|
|
|
|
failf(data, "SOCKS5 connection to %s not supported\n", dest);
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
2020-02-14 10:16:54 -05:00
|
|
|
|
|
|
|
Curl_resolv_unlock(data, dns); /* not used anymore from now on */
|
|
|
|
goto CONNECT_REQ_SEND;
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
2020-02-14 10:16:54 -05:00
|
|
|
CONNECT_RESOLVE_REMOTE:
|
|
|
|
case CONNECT_RESOLVE_REMOTE:
|
|
|
|
/* Authentication is complete, now specify destination to the proxy */
|
|
|
|
len = 0;
|
|
|
|
socksreq[len++] = 5; /* version (SOCKS5) */
|
|
|
|
socksreq[len++] = 1; /* connect */
|
|
|
|
socksreq[len++] = 0; /* must be zero */
|
|
|
|
|
|
|
|
if(!socks5_resolve_local) {
|
|
|
|
socksreq[len++] = 3; /* ATYP: domain name = 3 */
|
|
|
|
socksreq[len++] = (char) hostname_len; /* one byte address length */
|
|
|
|
memcpy(&socksreq[len], hostname, hostname_len); /* address w/o NULL */
|
|
|
|
len += hostname_len;
|
2020-02-18 06:54:02 -05:00
|
|
|
infof(data, "SOCKS5 connect to %s:%d (remotely resolved)\n",
|
2020-02-14 10:16:54 -05:00
|
|
|
hostname, remote_port);
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
CONNECT_REQ_SEND:
|
|
|
|
case CONNECT_REQ_SEND:
|
|
|
|
/* PORT MSB */
|
|
|
|
socksreq[len++] = (unsigned char)((remote_port >> 8) & 0xff);
|
|
|
|
/* PORT LSB */
|
|
|
|
socksreq[len++] = (unsigned char)(remote_port & 0xff);
|
2012-09-10 06:44:37 -04:00
|
|
|
|
2009-01-28 16:33:58 -05:00
|
|
|
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
|
2020-02-14 10:16:54 -05:00
|
|
|
if(conn->socks5_gssapi_enctype) {
|
|
|
|
failf(data, "SOCKS5 GSS-API protection not yet implemented.");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_GSSAPI_PROTECTION;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
2009-01-28 16:33:58 -05:00
|
|
|
#endif
|
2020-02-14 10:16:54 -05:00
|
|
|
sx->outp = socksreq;
|
|
|
|
sx->outstanding = len;
|
|
|
|
sxstate(conn, CONNECT_REQ_SENDING);
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case CONNECT_REQ_SENDING:
|
|
|
|
result = Curl_write_plain(conn, sockfd, (char *)sx->outp,
|
|
|
|
sx->outstanding, &written);
|
|
|
|
if(result && (CURLE_AGAIN != result)) {
|
|
|
|
failf(data, "Failed to send SOCKS5 connect request.");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_SEND_REQUEST;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
|
|
|
if(sx->outstanding != written) {
|
|
|
|
/* remain in state */
|
|
|
|
sx->outstanding -= written;
|
|
|
|
sx->outp += written;
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_OK;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
2009-01-28 16:33:58 -05:00
|
|
|
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
|
2020-02-14 10:16:54 -05:00
|
|
|
if(conn->socks5_gssapi_enctype) {
|
|
|
|
failf(data, "SOCKS5 GSS-API protection not yet implemented.");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_GSSAPI_PROTECTION;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
2009-01-28 16:33:58 -05:00
|
|
|
#endif
|
2020-02-14 10:16:54 -05:00
|
|
|
sx->outstanding = 10; /* minimum packet size is 10 */
|
|
|
|
sx->outp = socksreq;
|
|
|
|
sxstate(conn, CONNECT_REQ_READ);
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case CONNECT_REQ_READ:
|
|
|
|
result = Curl_read_plain(sockfd, (char *)sx->outp,
|
|
|
|
sx->outstanding, &actualread);
|
|
|
|
if(result && (CURLE_AGAIN != result)) {
|
|
|
|
failf(data, "Failed to receive SOCKS5 connect request ack.");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_RECV_REQACK;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
2020-06-08 08:05:22 -04:00
|
|
|
else if(!result && !actualread) {
|
|
|
|
/* connection closed */
|
|
|
|
failf(data, "connection to proxy closed");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_CLOSED;
|
2020-06-08 08:05:22 -04:00
|
|
|
}
|
2020-02-14 10:16:54 -05:00
|
|
|
else if(actualread != sx->outstanding) {
|
|
|
|
/* remain in state */
|
|
|
|
sx->outstanding -= actualread;
|
|
|
|
sx->outp += actualread;
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_OK;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
2008-01-04 18:01:00 -05:00
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
if(socksreq[0] != 5) { /* version */
|
|
|
|
failf(data,
|
|
|
|
"SOCKS5 reply has wrong version, version should be 5.");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_BAD_VERSION;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
|
|
|
else if(socksreq[1] != 0) { /* Anything besides 0 is an error */
|
2020-08-24 02:39:29 -04:00
|
|
|
CURLproxycode rc = CURLPX_REPLY_UNASSIGNED;
|
|
|
|
int code = socksreq[1];
|
2020-02-14 10:16:54 -05:00
|
|
|
failf(data, "Can't complete SOCKS5 connection to %s. (%d)",
|
|
|
|
hostname, (unsigned char)socksreq[1]);
|
2020-08-24 02:39:29 -04:00
|
|
|
if(code < 9) {
|
|
|
|
/* RFC 1928 section 6 lists: */
|
|
|
|
static const CURLproxycode lookup[] = {
|
|
|
|
CURLPX_OK,
|
|
|
|
CURLPX_REPLY_GENERAL_SERVER_FAILURE,
|
|
|
|
CURLPX_REPLY_NOT_ALLOWED,
|
|
|
|
CURLPX_REPLY_NETWORK_UNREACHABLE,
|
|
|
|
CURLPX_REPLY_HOST_UNREACHABLE,
|
|
|
|
CURLPX_REPLY_CONNECTION_REFUSED,
|
|
|
|
CURLPX_REPLY_TTL_EXPIRED,
|
|
|
|
CURLPX_REPLY_COMMAND_NOT_SUPPORTED,
|
|
|
|
CURLPX_REPLY_ADDRESS_TYPE_NOT_SUPPORTED,
|
|
|
|
};
|
|
|
|
rc = lookup[code];
|
|
|
|
}
|
|
|
|
return rc;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
2008-01-04 18:01:00 -05:00
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
/* Fix: in general, returned BND.ADDR is variable length parameter by RFC
|
|
|
|
1928, so the reply packet should be read until the end to avoid errors
|
|
|
|
at subsequent protocol level.
|
|
|
|
|
|
|
|
+----+-----+-------+------+----------+----------+
|
|
|
|
|VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
|
|
|
|
+----+-----+-------+------+----------+----------+
|
|
|
|
| 1 | 1 | X'00' | 1 | Variable | 2 |
|
|
|
|
+----+-----+-------+------+----------+----------+
|
|
|
|
|
|
|
|
ATYP:
|
|
|
|
o IP v4 address: X'01', BND.ADDR = 4 byte
|
|
|
|
o domain name: X'03', BND.ADDR = [ 1 byte length, string ]
|
|
|
|
o IP v6 address: X'04', BND.ADDR = 16 byte
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Calculate real packet size */
|
|
|
|
if(socksreq[3] == 3) {
|
|
|
|
/* domain name */
|
|
|
|
int addrlen = (int) socksreq[4];
|
|
|
|
len = 5 + addrlen + 2;
|
|
|
|
}
|
|
|
|
else if(socksreq[3] == 4) {
|
|
|
|
/* IPv6 */
|
|
|
|
len = 4 + 16 + 2;
|
|
|
|
}
|
2020-06-05 13:36:11 -04:00
|
|
|
else if(socksreq[3] == 1) {
|
|
|
|
len = 4 + 4 + 2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
failf(data, "SOCKS5 reply has wrong address type.");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_BAD_ADDRESS_TYPE;
|
2020-06-05 13:36:11 -04:00
|
|
|
}
|
2008-01-04 18:01:00 -05:00
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
/* At this point we already read first 10 bytes */
|
2009-01-28 16:33:58 -05:00
|
|
|
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
|
2020-02-14 10:16:54 -05:00
|
|
|
if(!conn->socks5_gssapi_enctype) {
|
|
|
|
/* decrypt_gssapi_blockread already read the whole packet */
|
2009-01-28 16:33:58 -05:00
|
|
|
#endif
|
2020-02-14 10:16:54 -05:00
|
|
|
if(len > 10) {
|
|
|
|
sx->outstanding = len - 10; /* get the rest */
|
|
|
|
sx->outp = &socksreq[10];
|
|
|
|
sxstate(conn, CONNECT_REQ_READ_MORE);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sxstate(conn, CONNECT_DONE);
|
|
|
|
break;
|
2009-01-28 16:33:58 -05:00
|
|
|
}
|
|
|
|
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
2009-01-28 16:33:58 -05:00
|
|
|
#endif
|
2020-02-14 10:16:54 -05:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case CONNECT_REQ_READ_MORE:
|
|
|
|
result = Curl_read_plain(sockfd, (char *)sx->outp,
|
|
|
|
sx->outstanding, &actualread);
|
|
|
|
if(result && (CURLE_AGAIN != result)) {
|
|
|
|
failf(data, "Failed to receive SOCKS5 connect request ack.");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_RECV_ADDRESS;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
2020-06-08 08:05:22 -04:00
|
|
|
else if(!result && !actualread) {
|
|
|
|
/* connection closed */
|
|
|
|
failf(data, "connection to proxy closed");
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_CLOSED;
|
2020-06-08 08:05:22 -04:00
|
|
|
}
|
|
|
|
else if(actualread != sx->outstanding) {
|
2020-02-14 10:16:54 -05:00
|
|
|
/* remain in state */
|
|
|
|
sx->outstanding -= actualread;
|
|
|
|
sx->outp += actualread;
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_OK;
|
2020-02-14 10:16:54 -05:00
|
|
|
}
|
|
|
|
sxstate(conn, CONNECT_DONE);
|
2016-08-14 11:01:13 -04:00
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
infof(data, "SOCKS5 request granted.\n");
|
2016-08-14 11:01:13 -04:00
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
*done = TRUE;
|
2020-08-24 02:39:29 -04:00
|
|
|
return CURLPX_OK; /* Proxy was successful! */
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
2008-09-29 17:46:04 -04:00
|
|
|
|
|
|
|
#endif /* CURL_DISABLE_PROXY */
|