1278 lines
38 KiB
C
Raw Normal View History

2015-04-27 22:25:09 +02:00
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
2023-07-02 19:51:09 +02:00
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
2015-04-27 22:25:09 +02:00
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
2021-09-14 00:13:48 +02:00
* are also available at https://curl.se/docs/copyright.html.
2015-04-27 22:25:09 +02: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.
*
2022-11-16 20:14:03 +01:00
* SPDX-License-Identifier: curl
*
2015-04-27 22:25:09 +02:00
***************************************************************************/
#include "curl_setup.h"
#if !defined(CURL_DISABLE_PROXY)
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#include "urldata.h"
#include "sendf.h"
#include "select.h"
2023-05-23 16:38:00 +02:00
#include "cfilters.h"
2015-04-27 22:25:09 +02:00
#include "connect.h"
#include "timeval.h"
#include "socks.h"
2020-08-30 11:54:41 +02:00
#include "multiif.h" /* for getsock macros */
2022-03-29 21:10:50 +02:00
#include "inet_pton.h"
2023-05-23 16:38:00 +02:00
#include "url.h"
2015-04-27 22:25:09 +02:00
2020-08-30 11:54:41 +02:00
/* The last 3 #include files should be in this order */
#include "curl_printf.h"
#include "curl_memory.h"
2015-04-27 22:25:09 +02:00
#include "memdebug.h"
2023-05-23 16:38:00 +02:00
/* for the (SOCKS) connect state machine */
enum connect_t {
CONNECT_INIT,
CONNECT_SOCKS_INIT, /* 1 */
CONNECT_SOCKS_SEND, /* 2 waiting to send more first data */
CONNECT_SOCKS_READ_INIT, /* 3 set up read */
CONNECT_SOCKS_READ, /* 4 read server response */
CONNECT_GSSAPI_INIT, /* 5 */
CONNECT_AUTH_INIT, /* 6 setup outgoing auth buffer */
CONNECT_AUTH_SEND, /* 7 send auth */
CONNECT_AUTH_READ, /* 8 read auth response */
CONNECT_REQ_INIT, /* 9 init SOCKS "request" */
CONNECT_RESOLVING, /* 10 */
CONNECT_RESOLVED, /* 11 */
CONNECT_RESOLVE_REMOTE, /* 12 */
CONNECT_REQ_SEND, /* 13 */
CONNECT_REQ_SENDING, /* 14 */
CONNECT_REQ_READ, /* 15 */
CONNECT_REQ_READ_MORE, /* 16 */
CONNECT_DONE /* 17 connected fine to the remote or the SOCKS proxy */
};
2024-04-14 22:45:38 +02:00
#define CURL_SOCKS_BUF_SIZE 600
/* make sure we configure it not too low */
#if CURL_SOCKS_BUF_SIZE < 600
#error CURL_SOCKS_BUF_SIZE must be at least 600
#endif
2023-05-23 16:38:00 +02:00
struct socks_state {
enum connect_t state;
ssize_t outstanding; /* send this many bytes more */
2024-04-14 22:45:38 +02:00
unsigned char buffer[CURL_SOCKS_BUF_SIZE];
2023-05-23 16:38:00 +02:00
unsigned char *outp; /* send from this pointer */
const char *hostname;
int remote_port;
const char *proxy_user;
const char *proxy_password;
};
2020-08-30 11:54:41 +02:00
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
2015-04-27 22:25:09 +02: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-08-30 11:54:41 +02:00
* This is STUPID BLOCKING behavior. Only used by the SOCKS GSSAPI functions.
2015-04-27 22:25:09 +02:00
*/
2023-07-02 19:51:09 +02:00
int Curl_blockread_all(struct Curl_cfilter *cf,
struct Curl_easy *data, /* transfer */
2015-04-27 22:25:09 +02:00
char *buf, /* store read data here */
ssize_t buffersize, /* max amount to read */
ssize_t *n) /* amount bytes read */
{
2019-11-11 23:01:05 +01:00
ssize_t nread = 0;
2015-04-27 22:25:09 +02:00
ssize_t allread = 0;
int result;
2023-07-02 19:51:09 +02:00
CURLcode err = CURLE_OK;
2015-04-27 22:25:09 +02:00
*n = 0;
for(;;) {
2021-09-14 00:13:48 +02:00
timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
2020-08-30 11:54:41 +02:00
if(timeout_ms < 0) {
2015-04-27 22:25:09 +02:00
/* we already got the timeout */
result = CURLE_OPERATION_TIMEDOUT;
break;
}
2020-08-30 11:54:41 +02:00
if(!timeout_ms)
timeout_ms = TIMEDIFF_T_MAX;
2023-07-02 19:51:09 +02:00
if(SOCKET_READABLE(cf->conn->sock[cf->sockindex], timeout_ms) <= 0) {
2015-04-27 22:25:09 +02:00
result = ~CURLE_OK;
break;
}
2023-07-02 19:51:09 +02:00
nread = Curl_conn_cf_recv(cf->next, data, buf, buffersize, &err);
if(nread <= 0) {
2024-11-11 15:18:55 +01:00
result = (int)err;
2023-07-02 19:51:09 +02:00
if(CURLE_AGAIN == err)
continue;
if(err) {
break;
}
}
2015-04-27 22:25:09 +02:00
if(buffersize == nread) {
allread += nread;
*n = allread;
result = CURLE_OK;
break;
}
if(!nread) {
result = ~CURLE_OK;
break;
}
buffersize -= nread;
buf += nread;
allread += nread;
}
return result;
}
2020-08-30 11:54:41 +02:00
#endif
2021-11-20 13:41:27 +01:00
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
#define DEBUG_AND_VERBOSE
2023-05-23 16:38:00 +02:00
#define sxstate(x,d,y) socksstate(x,d,y, __LINE__)
2021-11-20 13:41:27 +01:00
#else
2023-05-23 16:38:00 +02:00
#define sxstate(x,d,y) socksstate(x,d,y)
2020-08-30 11:54:41 +02:00
#endif
/* always use this function to change state, to make debugging easier */
2023-05-23 16:38:00 +02:00
static void socksstate(struct socks_state *sx, struct Curl_easy *data,
2020-08-30 11:54:41 +02:00
enum connect_t state
2021-11-20 13:41:27 +01:00
#ifdef DEBUG_AND_VERBOSE
2020-08-30 11:54:41 +02:00
, int lineno
#endif
)
{
2023-05-23 16:38:00 +02:00
enum connect_t oldstate = sx->state;
2021-11-20 13:41:27 +01:00
#ifdef DEBUG_AND_VERBOSE
2020-08-30 11:54:41 +02:00
/* synced with the state list in urldata.h */
2023-12-07 09:12:54 +01:00
static const char * const socks_statename[] = {
2020-08-30 11:54:41 +02:00
"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
2023-05-23 16:38:00 +02:00
(void)data;
2020-08-30 11:54:41 +02:00
if(oldstate == state)
2024-11-11 15:18:55 +01:00
/* do not bother when the new state is the same as the old state */
2020-08-30 11:54:41 +02:00
return;
2023-05-23 16:38:00 +02:00
sx->state = state;
2020-08-30 11:54:41 +02:00
2021-11-20 13:41:27 +01:00
#ifdef DEBUG_AND_VERBOSE
2021-09-14 00:13:48 +02:00
infof(data,
2023-05-23 16:38:00 +02:00
"SXSTATE: %s => %s; line %d",
2023-12-07 09:12:54 +01:00
socks_statename[oldstate], socks_statename[sx->state],
2020-08-30 11:54:41 +02:00
lineno);
#endif
}
2023-07-02 19:51:09 +02:00
static CURLproxycode socks_state_send(struct Curl_cfilter *cf,
struct socks_state *sx,
struct Curl_easy *data,
CURLproxycode failcode,
const char *description)
{
ssize_t nwritten;
CURLcode result;
nwritten = Curl_conn_cf_send(cf->next, data, (char *)sx->outp,
2024-11-11 15:18:55 +01:00
sx->outstanding, FALSE, &result);
2023-07-02 19:51:09 +02:00
if(nwritten <= 0) {
if(CURLE_AGAIN == result) {
return CURLPX_OK;
}
else if(CURLE_OK == result) {
/* connection closed */
failf(data, "connection to proxy closed");
return CURLPX_CLOSED;
}
failf(data, "Failed to send %s: %s", description,
curl_easy_strerror(result));
return failcode;
}
DEBUGASSERT(sx->outstanding >= nwritten);
/* not done, remain in state */
sx->outstanding -= nwritten;
sx->outp += nwritten;
return CURLPX_OK;
}
static CURLproxycode socks_state_recv(struct Curl_cfilter *cf,
struct socks_state *sx,
struct Curl_easy *data,
CURLproxycode failcode,
const char *description)
{
ssize_t nread;
CURLcode result;
nread = Curl_conn_cf_recv(cf->next, data, (char *)sx->outp,
sx->outstanding, &result);
if(nread <= 0) {
if(CURLE_AGAIN == result) {
return CURLPX_OK;
}
else if(CURLE_OK == result) {
/* connection closed */
failf(data, "connection to proxy closed");
return CURLPX_CLOSED;
}
2024-04-14 22:45:38 +02:00
failf(data, "SOCKS: Failed receiving %s: %s", description,
2023-07-02 19:51:09 +02:00
curl_easy_strerror(result));
return failcode;
}
/* remain in reading state */
DEBUGASSERT(sx->outstanding >= nread);
sx->outstanding -= nread;
sx->outp += nread;
return CURLPX_OK;
}
2015-04-27 22:25:09 +02:00
/*
* This function logs in to a SOCKS4 proxy and sends the specifics to the final
* destination server.
*
* Reference :
2018-11-29 20:27:00 +01:00
* https://www.openssh.com/txt/socks4.protocol
2015-04-27 22:25:09 +02:00
*
* Note :
* Set protocol4a=true for "SOCKS 4A (Simple Extension to SOCKS 4 Protocol)"
* Nonsupport "Identification Protocol (RFC1413)"
*/
2023-05-23 16:38:00 +02:00
static CURLproxycode do_SOCKS4(struct Curl_cfilter *cf,
struct socks_state *sx,
struct Curl_easy *data)
2015-04-27 22:25:09 +02:00
{
2023-05-23 16:38:00 +02:00
struct connectdata *conn = cf->conn;
2017-07-20 19:35:53 +02:00
const bool protocol4a =
(conn->socks_proxy.proxytype == CURLPROXY_SOCKS4A) ? TRUE : FALSE;
2024-04-14 22:45:38 +02:00
unsigned char *socksreq = sx->buffer;
2020-08-30 11:54:41 +02:00
CURLcode result;
2023-07-02 19:51:09 +02:00
CURLproxycode presult;
2020-08-30 11:54:41 +02:00
struct Curl_dns_entry *dns = NULL;
2015-04-27 22:25:09 +02:00
2020-08-30 11:54:41 +02:00
switch(sx->state) {
case CONNECT_SOCKS_INIT:
/* SOCKS4 can only do IPv4, insist! */
conn->ip_version = CURL_IPRESOLVE_V4;
if(conn->bits.httpproxy)
2021-11-20 13:41:27 +01:00
infof(data, "SOCKS4%s: connecting to HTTP proxy %s port %d",
2023-05-23 16:38:00 +02:00
protocol4a ? "a" : "", sx->hostname, sx->remote_port);
2015-04-27 22:25:09 +02:00
2023-05-23 16:38:00 +02:00
infof(data, "SOCKS4 communication to %s:%d",
sx->hostname, sx->remote_port);
2015-04-27 22:25:09 +02:00
2020-08-30 11:54:41 +02:00
/*
* Compose socks4 request
*
* Request format
*
* +----+----+----+----+----+----+----+----+----+----+....+----+
* | VN | CD | DSTPORT | DSTIP | USERID |NULL|
* +----+----+----+----+----+----+----+----+----+----+....+----+
* # of bytes: 1 1 2 4 variable 1
*/
2015-04-27 22:25:09 +02:00
2020-08-30 11:54:41 +02:00
socksreq[0] = 4; /* version (SOCKS4) */
socksreq[1] = 1; /* connect */
2023-05-23 16:38:00 +02:00
socksreq[2] = (unsigned char)((sx->remote_port >> 8) & 0xff); /* MSB */
socksreq[3] = (unsigned char)(sx->remote_port & 0xff); /* LSB */
2020-08-30 11:54:41 +02:00
/* DNS resolve only for SOCKS4, not SOCKS4a */
if(!protocol4a) {
enum resolve_t rc =
2023-07-02 19:51:09 +02:00
Curl_resolv(data, sx->hostname, sx->remote_port, TRUE, &dns);
2020-08-30 11:54:41 +02:00
if(rc == CURLRESOLV_ERROR)
2021-09-14 00:13:48 +02:00
return CURLPX_RESOLVE_HOST;
2020-08-30 11:54:41 +02:00
else if(rc == CURLRESOLV_PENDING) {
2023-05-23 16:38:00 +02:00
sxstate(sx, data, CONNECT_RESOLVING);
infof(data, "SOCKS4 non-blocking resolve of %s", sx->hostname);
2021-09-14 00:13:48 +02:00
return CURLPX_OK;
2020-08-30 11:54:41 +02:00
}
2023-05-23 16:38:00 +02:00
sxstate(sx, data, CONNECT_RESOLVED);
2020-08-30 11:54:41 +02:00
goto CONNECT_RESOLVED;
}
2015-04-27 22:25:09 +02:00
2024-11-11 15:18:55 +01:00
/* socks4a does not resolve anything locally */
2023-05-23 16:38:00 +02:00
sxstate(sx, data, CONNECT_REQ_INIT);
2020-08-30 11:54:41 +02:00
goto CONNECT_REQ_INIT;
2015-04-27 22:25:09 +02:00
2020-08-30 11:54:41 +02:00
case CONNECT_RESOLVING:
/* check if we have the name resolved by now */
2024-07-09 14:46:46 +02:00
dns = Curl_fetch_addr(data, sx->hostname, conn->primary.remote_port);
2015-04-27 22:25:09 +02:00
2020-08-30 11:54:41 +02:00
if(dns) {
#ifdef CURLRES_ASYNCH
2021-09-14 00:13:48 +02:00
data->state.async.dns = dns;
data->state.async.done = TRUE;
2020-08-30 11:54:41 +02:00
#endif
2023-05-23 16:38:00 +02:00
infof(data, "Hostname '%s' was found", sx->hostname);
sxstate(sx, data, CONNECT_RESOLVED);
2020-08-30 11:54:41 +02:00
}
else {
2021-09-14 00:13:48 +02:00
result = Curl_resolv_check(data, &dns);
if(!dns) {
if(result)
return CURLPX_RESOLVE_HOST;
return CURLPX_OK;
}
2020-08-30 11:54:41 +02:00
}
2024-04-14 22:45:38 +02:00
FALLTHROUGH();
case CONNECT_RESOLVED:
2023-07-02 19:51:09 +02:00
CONNECT_RESOLVED:
2024-04-14 22:45:38 +02:00
{
2020-08-30 11:54:41 +02:00
struct Curl_addrinfo *hp = NULL;
2015-04-27 22:25:09 +02:00
/*
2024-11-11 15:18:55 +01:00
* We cannot use 'hostent' as a struct that Curl_resolv() returns. It
2015-04-27 22:25:09 +02:00
* returns a Curl_addrinfo pointer that may not always look the same.
*/
2021-11-20 13:41:27 +01:00
if(dns) {
2018-01-26 17:06:56 +01:00
hp = dns->addr;
2015-04-27 22:25:09 +02:00
2021-11-20 13:41:27 +01:00
/* scan for the first IPv4 address */
while(hp && (hp->ai_family != AF_INET))
hp = hp->ai_next;
if(hp) {
2017-04-14 19:02:05 +02:00
struct sockaddr_in *saddr_in;
2021-11-20 13:41:27 +01:00
char buf[64];
Curl_printable_address(hp, buf, sizeof(buf));
2017-04-14 19:02:05 +02:00
2017-07-20 19:35:53 +02: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];
2017-04-14 19:02:05 +02:00
2021-11-20 13:41:27 +01:00
infof(data, "SOCKS4 connect to IPv4 %s (locally resolved)", buf);
2015-04-27 22:25:09 +02:00
2024-11-11 15:18:55 +01:00
Curl_resolv_unlink(data, &dns); /* not used anymore from now on */
2021-11-20 13:41:27 +01:00
}
else
2023-05-23 16:38:00 +02:00
failf(data, "SOCKS4 connection to %s not supported", sx->hostname);
2015-04-27 22:25:09 +02:00
}
2021-11-20 13:41:27 +01:00
else
2015-04-27 22:25:09 +02:00
failf(data, "Failed to resolve \"%s\" for SOCKS4 connect.",
2023-05-23 16:38:00 +02:00
sx->hostname);
2021-11-20 13:41:27 +01:00
if(!hp)
2021-09-14 00:13:48 +02:00
return CURLPX_RESOLVE_HOST;
2015-04-27 22:25:09 +02:00
}
2024-04-14 22:45:38 +02:00
FALLTHROUGH();
2020-08-30 11:54:41 +02:00
case CONNECT_REQ_INIT:
2024-04-14 22:45:38 +02:00
CONNECT_REQ_INIT:
2020-08-30 11:54:41 +02:00
/*
* This is currently not supporting "Identification Protocol (RFC1413)".
*/
socksreq[8] = 0; /* ensure empty userid is NUL-terminated */
2023-05-23 16:38:00 +02:00
if(sx->proxy_user) {
size_t plen = strlen(sx->proxy_user);
2024-04-14 22:45:38 +02:00
if(plen > 255) {
/* there is no real size limit to this field in the protocol, but
SOCKS5 limits the proxy user field to 255 bytes and it seems likely
that a longer field is either a mistake or malicious input */
2024-11-11 15:18:55 +01:00
failf(data, "Too long SOCKS proxy username");
2021-09-14 00:13:48 +02:00
return CURLPX_LONG_USER;
2020-08-30 11:54:41 +02:00
}
/* copy the proxy name WITH trailing zero */
2023-05-23 16:38:00 +02:00
memcpy(socksreq + 8, sx->proxy_user, plen + 1);
2015-04-27 22:25:09 +02:00
}
2020-08-30 11:54:41 +02:00
/*
* Make connection
*/
{
2021-09-14 00:13:48 +02:00
size_t packetsize = 9 +
2020-08-30 11:54:41 +02:00
strlen((char *)socksreq + 8); /* size including NUL */
/* If SOCKS4a, set special invalid IP address 0.0.0.x */
if(protocol4a) {
2021-09-14 00:13:48 +02:00
size_t hostnamelen = 0;
2020-08-30 11:54:41 +02:00
socksreq[4] = 0;
socksreq[5] = 0;
socksreq[6] = 0;
socksreq[7] = 1;
/* append hostname */
2023-05-23 16:38:00 +02:00
hostnamelen = strlen(sx->hostname) + 1; /* length including NUL */
2024-04-14 22:45:38 +02:00
if((hostnamelen <= 255) &&
(packetsize + hostnamelen < sizeof(sx->buffer)))
2023-05-23 16:38:00 +02:00
strcpy((char *)socksreq + packetsize, sx->hostname);
2020-08-30 11:54:41 +02:00
else {
2024-11-11 15:18:55 +01:00
failf(data, "SOCKS4: too long hostname");
2021-09-14 00:13:48 +02:00
return CURLPX_LONG_HOSTNAME;
2020-08-30 11:54:41 +02:00
}
packetsize += hostnamelen;
}
sx->outp = socksreq;
2024-04-14 22:45:38 +02:00
DEBUGASSERT(packetsize <= sizeof(sx->buffer));
2020-08-30 11:54:41 +02:00
sx->outstanding = packetsize;
2023-05-23 16:38:00 +02:00
sxstate(sx, data, CONNECT_REQ_SENDING);
2015-04-27 22:25:09 +02:00
}
2024-04-14 22:45:38 +02:00
FALLTHROUGH();
2020-08-30 11:54:41 +02:00
case CONNECT_REQ_SENDING:
2015-04-27 22:25:09 +02:00
/* Send request */
2023-07-02 19:51:09 +02:00
presult = socks_state_send(cf, sx, data, CURLPX_SEND_CONNECT,
"SOCKS4 connect request");
if(CURLPX_OK != presult)
return presult;
else if(sx->outstanding) {
/* remain in sending state */
2021-09-14 00:13:48 +02:00
return CURLPX_OK;
2015-04-27 22:25:09 +02:00
}
2020-08-30 11:54:41 +02:00
/* done sending! */
sx->outstanding = 8; /* receive data size */
sx->outp = socksreq;
2023-05-23 16:38:00 +02:00
sxstate(sx, data, CONNECT_SOCKS_READ);
2015-04-27 22:25:09 +02:00
2024-04-14 22:45:38 +02:00
FALLTHROUGH();
2020-08-30 11:54:41 +02:00
case CONNECT_SOCKS_READ:
2015-04-27 22:25:09 +02:00
/* Receive response */
2023-07-02 19:51:09 +02:00
presult = socks_state_recv(cf, sx, data, CURLPX_RECV_CONNECT,
"connect request ack");
if(CURLPX_OK != presult)
return presult;
else if(sx->outstanding) {
2020-08-30 11:54:41 +02:00
/* remain in reading state */
2021-09-14 00:13:48 +02:00
return CURLPX_OK;
2015-04-27 22:25:09 +02:00
}
2023-05-23 16:38:00 +02:00
sxstate(sx, data, CONNECT_DONE);
2020-08-30 11:54:41 +02:00
break;
default: /* lots of unused states in SOCKS4 */
break;
}
/*
* 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 ? */
2021-09-14 00:13:48 +02:00
if(socksreq[0]) {
2020-08-30 11:54:41 +02:00
failf(data,
"SOCKS4 reply has wrong version, version should be 0.");
2021-09-14 00:13:48 +02:00
return CURLPX_BAD_VERSION;
2015-04-27 22:25:09 +02:00
}
2020-08-30 11:54:41 +02:00
/* Result */
switch(socksreq[1]) {
case 90:
2021-11-20 13:41:27 +01:00
infof(data, "SOCKS4%s request granted.", protocol4a?"a":"");
2020-08-30 11:54:41 +02:00
break;
case 91:
failf(data,
2024-11-11 15:18:55 +01:00
"cannot complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
2020-08-30 11:54:41 +02:00
", request rejected or failed.",
2021-09-14 00:13:48 +02:00
socksreq[4], socksreq[5], socksreq[6], socksreq[7],
2020-08-30 11:54:41 +02:00
(((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
(unsigned char)socksreq[1]);
2021-09-14 00:13:48 +02:00
return CURLPX_REQUEST_FAILED;
2020-08-30 11:54:41 +02:00
case 92:
failf(data,
2024-11-11 15:18:55 +01:00
"cannot complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
2020-08-30 11:54:41 +02:00
", request rejected because SOCKS server cannot connect to "
"identd on the client.",
2021-09-14 00:13:48 +02:00
socksreq[4], socksreq[5], socksreq[6], socksreq[7],
2020-08-30 11:54:41 +02:00
(((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
(unsigned char)socksreq[1]);
2021-09-14 00:13:48 +02:00
return CURLPX_IDENTD;
2020-08-30 11:54:41 +02:00
case 93:
failf(data,
2024-11-11 15:18:55 +01:00
"cannot complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
2020-08-30 11:54:41 +02:00
", request rejected because the client program and identd "
"report different user-ids.",
2021-09-14 00:13:48 +02:00
socksreq[4], socksreq[5], socksreq[6], socksreq[7],
2020-08-30 11:54:41 +02:00
(((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
(unsigned char)socksreq[1]);
2021-09-14 00:13:48 +02:00
return CURLPX_IDENTD_DIFFER;
2020-08-30 11:54:41 +02:00
default:
failf(data,
2024-11-11 15:18:55 +01:00
"cannot complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
2020-08-30 11:54:41 +02:00
", Unknown.",
2021-09-14 00:13:48 +02:00
socksreq[4], socksreq[5], socksreq[6], socksreq[7],
2020-08-30 11:54:41 +02:00
(((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
(unsigned char)socksreq[1]);
2021-09-14 00:13:48 +02:00
return CURLPX_UNKNOWN_FAIL;
2020-08-30 11:54:41 +02:00
}
2015-04-27 22:25:09 +02:00
2021-09-14 00:13:48 +02:00
return CURLPX_OK; /* Proxy was successful! */
2015-04-27 22:25:09 +02:00
}
/*
* This function logs in to a SOCKS5 proxy and sends the specifics to the final
* destination server.
*/
2023-05-23 16:38:00 +02:00
static CURLproxycode do_SOCKS5(struct Curl_cfilter *cf,
struct socks_state *sx,
struct Curl_easy *data)
2015-04-27 22:25:09 +02:00
{
/*
2024-11-11 15:18:55 +01:00
According to the RFC1928, section "6. Replies". This is what a SOCK5
2015-04-27 22:25:09 +02:00
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
*/
2023-05-23 16:38:00 +02:00
struct connectdata *conn = cf->conn;
2024-04-14 22:45:38 +02:00
unsigned char *socksreq = sx->buffer;
size_t idx;
2020-08-30 11:54:41 +02:00
CURLcode result;
2023-07-02 19:51:09 +02:00
CURLproxycode presult;
2017-07-20 19:35:53 +02:00
bool socks5_resolve_local =
(conn->socks_proxy.proxytype == CURLPROXY_SOCKS5) ? TRUE : FALSE;
2023-05-23 16:38:00 +02:00
const size_t hostname_len = strlen(sx->hostname);
2024-04-14 22:45:38 +02:00
size_t len = 0;
2023-05-23 16:38:00 +02:00
const unsigned char auth = data->set.socks5auth;
2018-01-26 17:06:56 +01:00
bool allow_gssapi = FALSE;
2020-08-30 11:54:41 +02:00
struct Curl_dns_entry *dns = NULL;
2023-05-23 16:38:00 +02:00
DEBUGASSERT(auth & (CURLAUTH_BASIC | CURLAUTH_GSSAPI));
2020-08-30 11:54:41 +02:00
switch(sx->state) {
case CONNECT_SOCKS_INIT:
if(conn->bits.httpproxy)
2021-11-20 13:41:27 +01:00
infof(data, "SOCKS5: connecting to HTTP proxy %s port %d",
2023-05-23 16:38:00 +02:00
sx->hostname, sx->remote_port);
2020-08-30 11:54:41 +02:00
/* RFC1928 chapter 5 specifies max 255 chars for domain name in packet */
if(!socks5_resolve_local && hostname_len > 255) {
2023-11-18 08:22:54 +01:00
failf(data, "SOCKS5: the destination hostname is too long to be "
"resolved remotely by the proxy.");
return CURLPX_LONG_HOSTNAME;
2020-08-30 11:54:41 +02:00
}
2015-04-27 22:25:09 +02:00
2020-08-30 11:54:41 +02:00
if(auth & ~(CURLAUTH_BASIC | CURLAUTH_GSSAPI))
2021-09-14 00:13:48 +02:00
infof(data,
2023-05-23 16:38:00 +02:00
"warning: unsupported value passed to CURLOPT_SOCKS5_AUTH: %u",
2020-08-30 11:54:41 +02:00
auth);
if(!(auth & CURLAUTH_BASIC))
/* disable username/password auth */
2023-05-23 16:38:00 +02:00
sx->proxy_user = NULL;
2015-04-27 22:25:09 +02:00
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
2020-08-30 11:54:41 +02:00
if(auth & CURLAUTH_GSSAPI)
allow_gssapi = TRUE;
2015-04-27 22:25:09 +02:00
#endif
2020-08-30 11:54:41 +02: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 */
2023-05-23 16:38:00 +02:00
if(sx->proxy_user)
2020-08-30 11:54:41 +02:00
socksreq[idx++] = 2; /* username/password */
/* write the number of authentication methods */
socksreq[1] = (unsigned char) (idx - 2);
2023-07-02 19:51:09 +02:00
sx->outp = socksreq;
2024-04-14 22:45:38 +02:00
DEBUGASSERT(idx <= sizeof(sx->buffer));
2023-07-02 19:51:09 +02:00
sx->outstanding = idx;
presult = socks_state_send(cf, sx, data, CURLPX_SEND_CONNECT,
"initial SOCKS5 request");
if(CURLPX_OK != presult)
return presult;
else if(sx->outstanding) {
/* remain in sending state */
2021-09-14 00:13:48 +02:00
return CURLPX_OK;
2020-08-30 11:54:41 +02:00
}
2023-05-23 16:38:00 +02:00
sxstate(sx, data, CONNECT_SOCKS_READ);
2020-08-30 11:54:41 +02:00
goto CONNECT_SOCKS_READ_INIT;
case CONNECT_SOCKS_SEND:
2023-07-02 19:51:09 +02:00
presult = socks_state_send(cf, sx, data, CURLPX_SEND_CONNECT,
"initial SOCKS5 request");
if(CURLPX_OK != presult)
return presult;
else if(sx->outstanding) {
/* remain in sending state */
2021-09-14 00:13:48 +02:00
return CURLPX_OK;
2020-08-30 11:54:41 +02:00
}
2024-04-14 22:45:38 +02:00
FALLTHROUGH();
2020-08-30 11:54:41 +02:00
case CONNECT_SOCKS_READ_INIT:
2024-04-14 22:45:38 +02:00
CONNECT_SOCKS_READ_INIT:
2020-08-30 11:54:41 +02:00
sx->outstanding = 2; /* expect two bytes */
sx->outp = socksreq; /* store it here */
2024-04-14 22:45:38 +02:00
FALLTHROUGH();
2020-08-30 11:54:41 +02:00
case CONNECT_SOCKS_READ:
2023-07-02 19:51:09 +02:00
presult = socks_state_recv(cf, sx, data, CURLPX_RECV_CONNECT,
"initial SOCKS5 response");
if(CURLPX_OK != presult)
return presult;
else if(sx->outstanding) {
2020-08-30 11:54:41 +02:00
/* remain in reading state */
2021-09-14 00:13:48 +02:00
return CURLPX_OK;
2020-08-30 11:54:41 +02:00
}
else if(socksreq[0] != 5) {
failf(data, "Received invalid version in initial SOCKS5 response.");
2021-09-14 00:13:48 +02:00
return CURLPX_BAD_VERSION;
2020-08-30 11:54:41 +02:00
}
else if(socksreq[1] == 0) {
/* DONE! No authentication needed. Send request. */
2023-05-23 16:38:00 +02:00
sxstate(sx, data, CONNECT_REQ_INIT);
2020-08-30 11:54:41 +02:00
goto CONNECT_REQ_INIT;
}
else if(socksreq[1] == 2) {
/* regular name + password authentication */
2023-05-23 16:38:00 +02:00
sxstate(sx, data, CONNECT_AUTH_INIT);
2020-08-30 11:54:41 +02:00
goto CONNECT_AUTH_INIT;
}
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
else if(allow_gssapi && (socksreq[1] == 1)) {
2023-05-23 16:38:00 +02:00
sxstate(sx, data, CONNECT_GSSAPI_INIT);
2023-07-02 19:51:09 +02:00
result = Curl_SOCKS5_gssapi_negotiate(cf, data);
2020-08-30 11:54:41 +02:00
if(result) {
failf(data, "Unable to negotiate SOCKS5 GSS-API context.");
2021-09-14 00:13:48 +02:00
return CURLPX_GSSAPI;
2020-08-30 11:54:41 +02:00
}
}
#endif
else {
/* error */
if(!allow_gssapi && (socksreq[1] == 1)) {
failf(data,
"SOCKS5 GSSAPI per-message authentication is not supported.");
2021-09-14 00:13:48 +02:00
return CURLPX_GSSAPI_PERMSG;
2020-08-30 11:54:41 +02:00
}
else if(socksreq[1] == 255) {
failf(data, "No authentication method was acceptable.");
2021-09-14 00:13:48 +02:00
return CURLPX_NO_AUTH;
2020-08-30 11:54:41 +02:00
}
}
failf(data,
"Undocumented SOCKS5 mode attempted to be used by server.");
2021-09-14 00:13:48 +02:00
return CURLPX_UNKNOWN_MODE;
2020-08-30 11:54:41 +02:00
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
case CONNECT_GSSAPI_INIT:
/* GSSAPI stuff done non-blocking */
break;
2015-04-27 22:25:09 +02:00
#endif
2020-08-30 11:54:41 +02:00
default: /* do nothing! */
break;
2023-07-02 19:51:09 +02:00
CONNECT_AUTH_INIT:
2020-08-30 11:54:41 +02:00
case CONNECT_AUTH_INIT: {
2024-11-11 15:18:55 +01:00
/* Needs username and password */
2018-01-26 17:06:56 +01:00
size_t proxy_user_len, proxy_password_len;
2023-05-23 16:38:00 +02:00
if(sx->proxy_user && sx->proxy_password) {
proxy_user_len = strlen(sx->proxy_user);
proxy_password_len = strlen(sx->proxy_password);
2015-04-27 22:25:09 +02:00
}
else {
2018-01-26 17:06:56 +01:00
proxy_user_len = 0;
2015-04-27 22:25:09 +02:00
proxy_password_len = 0;
}
/* 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 */
2018-01-26 17:06:56 +01:00
socksreq[len++] = (unsigned char) proxy_user_len;
2023-05-23 16:38:00 +02:00
if(sx->proxy_user && proxy_user_len) {
2019-11-11 23:01:05 +01:00
/* the length must fit in a single byte */
2023-05-23 16:38:00 +02:00
if(proxy_user_len > 255) {
2024-11-11 15:18:55 +01:00
failf(data, "Excessive username length for proxy auth");
2021-09-14 00:13:48 +02:00
return CURLPX_LONG_USER;
2019-11-11 23:01:05 +01:00
}
2023-05-23 16:38:00 +02:00
memcpy(socksreq + len, sx->proxy_user, proxy_user_len);
2019-11-11 23:01:05 +01:00
}
2018-01-26 17:06:56 +01:00
len += proxy_user_len;
2015-04-27 22:25:09 +02:00
socksreq[len++] = (unsigned char) proxy_password_len;
2023-05-23 16:38:00 +02:00
if(sx->proxy_password && proxy_password_len) {
2019-11-11 23:01:05 +01:00
/* the length must fit in a single byte */
if(proxy_password_len > 255) {
failf(data, "Excessive password length for proxy auth");
2021-09-14 00:13:48 +02:00
return CURLPX_LONG_PASSWD;
2019-11-11 23:01:05 +01:00
}
2023-05-23 16:38:00 +02:00
memcpy(socksreq + len, sx->proxy_password, proxy_password_len);
2019-11-11 23:01:05 +01:00
}
2015-04-27 22:25:09 +02:00
len += proxy_password_len;
2023-05-23 16:38:00 +02:00
sxstate(sx, data, CONNECT_AUTH_SEND);
2024-04-14 22:45:38 +02:00
DEBUGASSERT(len <= sizeof(sx->buffer));
2020-08-30 11:54:41 +02:00
sx->outstanding = len;
sx->outp = socksreq;
}
2024-04-14 22:45:38 +02:00
FALLTHROUGH();
2020-08-30 11:54:41 +02:00
case CONNECT_AUTH_SEND:
2023-07-02 19:51:09 +02:00
presult = socks_state_send(cf, sx, data, CURLPX_SEND_AUTH,
"SOCKS5 sub-negotiation request");
if(CURLPX_OK != presult)
return presult;
else if(sx->outstanding) {
/* remain in sending state */
2021-09-14 00:13:48 +02:00
return CURLPX_OK;
2020-08-30 11:54:41 +02:00
}
sx->outp = socksreq;
sx->outstanding = 2;
2023-05-23 16:38:00 +02:00
sxstate(sx, data, CONNECT_AUTH_READ);
2024-04-14 22:45:38 +02:00
FALLTHROUGH();
2020-08-30 11:54:41 +02:00
case CONNECT_AUTH_READ:
2023-07-02 19:51:09 +02:00
presult = socks_state_recv(cf, sx, data, CURLPX_RECV_AUTH,
"SOCKS5 sub-negotiation response");
if(CURLPX_OK != presult)
return presult;
else if(sx->outstanding) {
/* remain in reading state */
2021-09-14 00:13:48 +02:00
return CURLPX_OK;
2020-08-30 11:54:41 +02:00
}
2015-04-27 22:25:09 +02:00
/* ignore the first (VER) byte */
2021-09-14 00:13:48 +02:00
else if(socksreq[1]) { /* status */
2015-04-27 22:25:09 +02:00
failf(data, "User was rejected by the SOCKS5 server (%d %d).",
socksreq[0], socksreq[1]);
2021-09-14 00:13:48 +02:00
return CURLPX_USER_REJECTED;
2015-04-27 22:25:09 +02:00
}
/* Everything is good so far, user was authenticated! */
2023-05-23 16:38:00 +02:00
sxstate(sx, data, CONNECT_REQ_INIT);
2024-04-14 22:45:38 +02:00
FALLTHROUGH();
2020-08-30 11:54:41 +02:00
case CONNECT_REQ_INIT:
2024-04-14 22:45:38 +02:00
CONNECT_REQ_INIT:
2020-08-30 11:54:41 +02:00
if(socks5_resolve_local) {
2023-05-23 16:38:00 +02:00
enum resolve_t rc = Curl_resolv(data, sx->hostname, sx->remote_port,
2023-07-02 19:51:09 +02:00
TRUE, &dns);
2020-08-30 11:54:41 +02:00
if(rc == CURLRESOLV_ERROR)
2021-09-14 00:13:48 +02:00
return CURLPX_RESOLVE_HOST;
2020-08-30 11:54:41 +02:00
if(rc == CURLRESOLV_PENDING) {
2023-05-23 16:38:00 +02:00
sxstate(sx, data, CONNECT_RESOLVING);
2021-09-14 00:13:48 +02:00
return CURLPX_OK;
2015-04-27 22:25:09 +02:00
}
2023-05-23 16:38:00 +02:00
sxstate(sx, data, CONNECT_RESOLVED);
2020-08-30 11:54:41 +02:00
goto CONNECT_RESOLVED;
2015-04-27 22:25:09 +02:00
}
2020-08-30 11:54:41 +02:00
goto CONNECT_RESOLVE_REMOTE;
2015-04-27 22:25:09 +02:00
2020-08-30 11:54:41 +02:00
case CONNECT_RESOLVING:
/* check if we have the name resolved by now */
2023-05-23 16:38:00 +02:00
dns = Curl_fetch_addr(data, sx->hostname, sx->remote_port);
2015-04-27 22:25:09 +02:00
2020-08-30 11:54:41 +02:00
if(dns) {
#ifdef CURLRES_ASYNCH
2021-09-14 00:13:48 +02:00
data->state.async.dns = dns;
data->state.async.done = TRUE;
2020-08-30 11:54:41 +02:00
#endif
2023-05-23 16:38:00 +02:00
infof(data, "SOCKS5: hostname '%s' found", sx->hostname);
2015-04-27 22:25:09 +02:00
}
2020-08-30 11:54:41 +02:00
if(!dns) {
2021-09-14 00:13:48 +02:00
result = Curl_resolv_check(data, &dns);
if(!dns) {
if(result)
return CURLPX_RESOLVE_HOST;
return CURLPX_OK;
}
2020-08-30 11:54:41 +02:00
}
2024-04-14 22:45:38 +02:00
FALLTHROUGH();
case CONNECT_RESOLVED:
2023-07-02 19:51:09 +02:00
CONNECT_RESOLVED:
2024-04-14 22:45:38 +02:00
{
char dest[MAX_IPADR_LEN]; /* printable address */
2020-08-30 11:54:41 +02:00
struct Curl_addrinfo *hp = NULL;
2015-04-27 22:25:09 +02:00
if(dns)
2018-01-26 17:06:56 +01:00
hp = dns->addr;
2024-07-09 14:46:46 +02:00
#ifdef USE_IPV6
2024-04-14 22:45:38 +02:00
if(data->set.ipver != CURL_IPRESOLVE_WHATEVER) {
int wanted_family = data->set.ipver == CURL_IPRESOLVE_V4 ?
AF_INET : AF_INET6;
/* scan for the first proper address */
while(hp && (hp->ai_family != wanted_family))
hp = hp->ai_next;
}
#endif
2020-08-30 11:54:41 +02:00
if(!hp) {
failf(data, "Failed to resolve \"%s\" for SOCKS5 connect.",
2023-05-23 16:38:00 +02:00
sx->hostname);
2021-09-14 00:13:48 +02:00
return CURLPX_RESOLVE_HOST;
2020-08-30 11:54:41 +02:00
}
2017-04-14 19:02:05 +02:00
2020-08-30 11:54:41 +02:00
Curl_printable_address(hp, dest, sizeof(dest));
2017-04-14 19:02:05 +02:00
2020-08-30 11:54:41 +02: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];
2015-04-27 22:25:09 +02:00
}
2023-12-07 09:12:54 +01:00
infof(data, "SOCKS5 connect to %s:%d (locally resolved)", dest,
sx->remote_port);
2020-08-30 11:54:41 +02:00
}
2024-07-09 14:46:46 +02:00
#ifdef USE_IPV6
2020-08-30 11:54:41 +02:00
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];
2017-04-14 19:02:05 +02:00
}
2023-12-07 09:12:54 +01:00
infof(data, "SOCKS5 connect to [%s]:%d (locally resolved)", dest,
sx->remote_port);
2015-04-27 22:25:09 +02:00
}
2020-08-30 11:54:41 +02:00
#endif
else {
hp = NULL; /* fail! */
2021-09-14 00:13:48 +02:00
failf(data, "SOCKS5 connection to %s not supported", dest);
2015-04-27 22:25:09 +02:00
}
2020-08-30 11:54:41 +02:00
2024-11-11 15:18:55 +01:00
Curl_resolv_unlink(data, &dns); /* not used anymore from now on */
2020-08-30 11:54:41 +02:00
goto CONNECT_REQ_SEND;
2015-04-27 22:25:09 +02:00
}
2023-07-02 19:51:09 +02:00
CONNECT_RESOLVE_REMOTE:
2020-08-30 11:54:41 +02:00
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) {
2022-03-29 21:10:50 +02:00
/* ATYP: domain name = 3,
IPv6 == 4,
IPv4 == 1 */
unsigned char ip4[4];
2024-07-09 14:46:46 +02:00
#ifdef USE_IPV6
2022-03-29 21:10:50 +02:00
if(conn->bits.ipv6_ip) {
char ip6[16];
2023-05-23 16:38:00 +02:00
if(1 != Curl_inet_pton(AF_INET6, sx->hostname, ip6))
2022-03-29 21:10:50 +02:00
return CURLPX_BAD_ADDRESS_TYPE;
socksreq[len++] = 4;
memcpy(&socksreq[len], ip6, sizeof(ip6));
len += sizeof(ip6);
}
else
#endif
2023-05-23 16:38:00 +02:00
if(1 == Curl_inet_pton(AF_INET, sx->hostname, ip4)) {
2022-03-29 21:10:50 +02:00
socksreq[len++] = 1;
memcpy(&socksreq[len], ip4, sizeof(ip4));
len += sizeof(ip4);
}
else {
socksreq[len++] = 3;
2023-11-18 08:22:54 +01:00
socksreq[len++] = (unsigned char) hostname_len; /* one byte length */
2023-05-23 16:38:00 +02:00
memcpy(&socksreq[len], sx->hostname, hostname_len); /* w/o NULL */
2022-03-29 21:10:50 +02:00
len += hostname_len;
}
2021-11-20 13:41:27 +01:00
infof(data, "SOCKS5 connect to %s:%d (remotely resolved)",
2023-05-23 16:38:00 +02:00
sx->hostname, sx->remote_port);
2020-08-30 11:54:41 +02:00
}
2024-04-14 22:45:38 +02:00
FALLTHROUGH();
2015-04-27 22:25:09 +02:00
2020-08-30 11:54:41 +02:00
case CONNECT_REQ_SEND:
2024-04-14 22:45:38 +02:00
CONNECT_REQ_SEND:
2020-08-30 11:54:41 +02:00
/* PORT MSB */
2023-05-23 16:38:00 +02:00
socksreq[len++] = (unsigned char)((sx->remote_port >> 8) & 0xff);
2020-08-30 11:54:41 +02:00
/* PORT LSB */
2023-05-23 16:38:00 +02:00
socksreq[len++] = (unsigned char)(sx->remote_port & 0xff);
2015-04-27 22:25:09 +02:00
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
2020-08-30 11:54:41 +02:00
if(conn->socks5_gssapi_enctype) {
failf(data, "SOCKS5 GSS-API protection not yet implemented.");
2021-09-14 00:13:48 +02:00
return CURLPX_GSSAPI_PROTECTION;
2020-08-30 11:54:41 +02:00
}
2015-04-27 22:25:09 +02:00
#endif
2020-08-30 11:54:41 +02:00
sx->outp = socksreq;
2024-04-14 22:45:38 +02:00
DEBUGASSERT(len <= sizeof(sx->buffer));
2020-08-30 11:54:41 +02:00
sx->outstanding = len;
2023-05-23 16:38:00 +02:00
sxstate(sx, data, CONNECT_REQ_SENDING);
2024-04-14 22:45:38 +02:00
FALLTHROUGH();
2020-08-30 11:54:41 +02:00
case CONNECT_REQ_SENDING:
2023-07-02 19:51:09 +02:00
presult = socks_state_send(cf, sx, data, CURLPX_SEND_REQUEST,
"SOCKS5 connect request");
if(CURLPX_OK != presult)
return presult;
else if(sx->outstanding) {
/* remain in send state */
2021-09-14 00:13:48 +02:00
return CURLPX_OK;
2020-08-30 11:54:41 +02:00
}
2015-04-27 22:25:09 +02:00
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
2020-08-30 11:54:41 +02:00
if(conn->socks5_gssapi_enctype) {
failf(data, "SOCKS5 GSS-API protection not yet implemented.");
2021-09-14 00:13:48 +02:00
return CURLPX_GSSAPI_PROTECTION;
2020-08-30 11:54:41 +02:00
}
2015-04-27 22:25:09 +02:00
#endif
2020-08-30 11:54:41 +02:00
sx->outstanding = 10; /* minimum packet size is 10 */
sx->outp = socksreq;
2023-05-23 16:38:00 +02:00
sxstate(sx, data, CONNECT_REQ_READ);
2024-04-14 22:45:38 +02:00
FALLTHROUGH();
2020-08-30 11:54:41 +02:00
case CONNECT_REQ_READ:
2023-07-02 19:51:09 +02:00
presult = socks_state_recv(cf, sx, data, CURLPX_RECV_REQACK,
"SOCKS5 connect request ack");
if(CURLPX_OK != presult)
return presult;
else if(sx->outstanding) {
/* remain in reading state */
2021-09-14 00:13:48 +02:00
return CURLPX_OK;
2020-08-30 11:54:41 +02:00
}
2023-07-02 19:51:09 +02:00
else if(socksreq[0] != 5) { /* version */
2020-08-30 11:54:41 +02:00
failf(data,
"SOCKS5 reply has wrong version, version should be 5.");
2021-09-14 00:13:48 +02:00
return CURLPX_BAD_VERSION;
2020-08-30 11:54:41 +02:00
}
2021-09-14 00:13:48 +02:00
else if(socksreq[1]) { /* Anything besides 0 is an error */
CURLproxycode rc = CURLPX_REPLY_UNASSIGNED;
int code = socksreq[1];
2024-11-11 15:18:55 +01:00
failf(data, "cannot complete SOCKS5 connection to %s. (%d)",
2023-05-23 16:38:00 +02:00
sx->hostname, (unsigned char)socksreq[1]);
2021-09-14 00:13:48 +02: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-08-30 11:54:41 +02:00
}
2015-04-27 22:25:09 +02:00
2020-08-30 11:54:41 +02: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;
}
else if(socksreq[3] == 1) {
len = 4 + 4 + 2;
}
else {
failf(data, "SOCKS5 reply has wrong address type.");
2021-09-14 00:13:48 +02:00
return CURLPX_BAD_ADDRESS_TYPE;
2020-08-30 11:54:41 +02:00
}
2015-04-27 22:25:09 +02:00
2020-08-30 11:54:41 +02:00
/* At this point we already read first 10 bytes */
2015-04-27 22:25:09 +02:00
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
2020-08-30 11:54:41 +02:00
if(!conn->socks5_gssapi_enctype) {
/* decrypt_gssapi_blockread already read the whole packet */
2015-04-27 22:25:09 +02:00
#endif
2020-08-30 11:54:41 +02:00
if(len > 10) {
2024-04-14 22:45:38 +02:00
DEBUGASSERT(len <= sizeof(sx->buffer));
2020-08-30 11:54:41 +02:00
sx->outstanding = len - 10; /* get the rest */
sx->outp = &socksreq[10];
2023-05-23 16:38:00 +02:00
sxstate(sx, data, CONNECT_REQ_READ_MORE);
2020-08-30 11:54:41 +02:00
}
else {
2023-05-23 16:38:00 +02:00
sxstate(sx, data, CONNECT_DONE);
2020-08-30 11:54:41 +02:00
break;
2015-04-27 22:25:09 +02:00
}
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
2020-08-30 11:54:41 +02:00
}
2015-04-27 22:25:09 +02:00
#endif
2024-04-14 22:45:38 +02:00
FALLTHROUGH();
2020-08-30 11:54:41 +02:00
case CONNECT_REQ_READ_MORE:
2023-07-02 19:51:09 +02:00
presult = socks_state_recv(cf, sx, data, CURLPX_RECV_ADDRESS,
"SOCKS5 connect request address");
if(CURLPX_OK != presult)
return presult;
else if(sx->outstanding) {
/* remain in reading state */
2021-09-14 00:13:48 +02:00
return CURLPX_OK;
2020-08-30 11:54:41 +02:00
}
2023-05-23 16:38:00 +02:00
sxstate(sx, data, CONNECT_DONE);
2017-04-14 19:02:05 +02:00
}
2021-11-20 13:41:27 +01:00
infof(data, "SOCKS5 request granted.");
2017-04-14 19:02:05 +02:00
2021-09-14 00:13:48 +02:00
return CURLPX_OK; /* Proxy was successful! */
2015-04-27 22:25:09 +02:00
}
2023-05-23 16:38:00 +02:00
static CURLcode connect_SOCKS(struct Curl_cfilter *cf,
struct socks_state *sxstate,
struct Curl_easy *data)
{
CURLcode result = CURLE_OK;
CURLproxycode pxresult = CURLPX_OK;
struct connectdata *conn = cf->conn;
switch(conn->socks_proxy.proxytype) {
case CURLPROXY_SOCKS5:
case CURLPROXY_SOCKS5_HOSTNAME:
pxresult = do_SOCKS5(cf, sxstate, data);
break;
case CURLPROXY_SOCKS4:
case CURLPROXY_SOCKS4A:
pxresult = do_SOCKS4(cf, sxstate, data);
break;
default:
failf(data, "unknown proxytype option given");
result = CURLE_COULDNT_CONNECT;
} /* switch proxytype */
if(pxresult) {
result = CURLE_PROXY;
data->info.pxcode = pxresult;
}
return result;
}
static void socks_proxy_cf_free(struct Curl_cfilter *cf)
{
struct socks_state *sxstate = cf->ctx;
if(sxstate) {
free(sxstate);
cf->ctx = NULL;
}
}
/* After a TCP connection to the proxy has been verified, this function does
2024-11-11 15:18:55 +01:00
the next magic steps. If 'done' is not set TRUE, it is not done yet and
2023-05-23 16:38:00 +02:00
must be called again.
Note: this function's sub-functions call failf()
*/
static CURLcode socks_proxy_cf_connect(struct Curl_cfilter *cf,
struct Curl_easy *data,
bool blocking, bool *done)
{
CURLcode result;
struct connectdata *conn = cf->conn;
int sockindex = cf->sockindex;
struct socks_state *sx = cf->ctx;
if(cf->connected) {
*done = TRUE;
return CURLE_OK;
}
2023-12-07 09:12:54 +01:00
result = cf->next->cft->do_connect(cf->next, data, blocking, done);
2023-05-23 16:38:00 +02:00
if(result || !*done)
return result;
if(!sx) {
2024-04-14 22:45:38 +02:00
sx = calloc(1, sizeof(*sx));
2023-05-23 16:38:00 +02:00
if(!sx)
return CURLE_OUT_OF_MEMORY;
cf->ctx = sx;
}
if(sx->state == CONNECT_INIT) {
/* for the secondary socket (FTP), use the "connect to host"
* but ignore the "connect to port" (use the secondary port)
*/
sxstate(sx, data, CONNECT_SOCKS_INIT);
sx->hostname =
conn->bits.httpproxy ?
conn->http_proxy.host.name :
conn->bits.conn_to_host ?
conn->conn_to_host.name :
sockindex == SECONDARYSOCKET ?
conn->secondaryhostname : conn->host.name;
sx->remote_port =
conn->bits.httpproxy ? (int)conn->http_proxy.port :
sockindex == SECONDARYSOCKET ? conn->secondary_port :
conn->bits.conn_to_port ? conn->conn_to_port :
conn->remote_port;
sx->proxy_user = conn->socks_proxy.user;
sx->proxy_password = conn->socks_proxy.passwd;
}
result = connect_SOCKS(cf, sx, data);
if(!result && sx->state == CONNECT_DONE) {
cf->connected = TRUE;
2024-07-09 14:46:46 +02:00
Curl_verboseconnect(data, conn, cf->sockindex);
2023-05-23 16:38:00 +02:00
socks_proxy_cf_free(cf);
}
*done = cf->connected;
return result;
}
2024-04-14 22:45:38 +02:00
static void socks_cf_adjust_pollset(struct Curl_cfilter *cf,
2023-05-23 16:38:00 +02:00
struct Curl_easy *data,
2024-04-14 22:45:38 +02:00
struct easy_pollset *ps)
2023-05-23 16:38:00 +02:00
{
struct socks_state *sx = cf->ctx;
2024-04-14 22:45:38 +02:00
if(!cf->connected && sx) {
2023-05-23 16:38:00 +02:00
/* If we are not connected, the filter below is and has nothing
* to wait on, we determine what to wait for. */
2024-04-14 22:45:38 +02:00
curl_socket_t sock = Curl_conn_cf_get_socket(cf, data);
2023-05-23 16:38:00 +02:00
switch(sx->state) {
case CONNECT_RESOLVING:
case CONNECT_SOCKS_READ:
case CONNECT_AUTH_READ:
case CONNECT_REQ_READ:
case CONNECT_REQ_READ_MORE:
2024-04-14 22:45:38 +02:00
Curl_pollset_set_in_only(data, ps, sock);
2023-05-23 16:38:00 +02:00
break;
default:
2024-04-14 22:45:38 +02:00
Curl_pollset_set_out_only(data, ps, sock);
2023-05-23 16:38:00 +02:00
break;
}
}
}
static void socks_proxy_cf_close(struct Curl_cfilter *cf,
struct Curl_easy *data)
{
DEBUGASSERT(cf->next);
cf->connected = FALSE;
socks_proxy_cf_free(cf);
2023-12-07 09:12:54 +01:00
cf->next->cft->do_close(cf->next, data);
2023-05-23 16:38:00 +02:00
}
static void socks_proxy_cf_destroy(struct Curl_cfilter *cf,
struct Curl_easy *data)
{
(void)data;
socks_proxy_cf_free(cf);
}
static void socks_cf_get_host(struct Curl_cfilter *cf,
struct Curl_easy *data,
const char **phost,
const char **pdisplay_host,
int *pport)
{
(void)data;
if(!cf->connected) {
*phost = cf->conn->socks_proxy.host.name;
*pdisplay_host = cf->conn->http_proxy.host.dispname;
*pport = (int)cf->conn->socks_proxy.port;
}
else {
cf->next->cft->get_host(cf->next, data, phost, pdisplay_host, pport);
}
}
2023-07-02 19:51:09 +02:00
struct Curl_cftype Curl_cft_socks_proxy = {
2023-05-23 16:38:00 +02:00
"SOCKS-PROXYY",
2024-07-09 14:46:46 +02:00
CF_TYPE_IP_CONNECT|CF_TYPE_PROXY,
2023-07-02 19:51:09 +02:00
0,
2023-05-23 16:38:00 +02:00
socks_proxy_cf_destroy,
socks_proxy_cf_connect,
socks_proxy_cf_close,
2024-11-11 15:18:55 +01:00
Curl_cf_def_shutdown,
2023-05-23 16:38:00 +02:00
socks_cf_get_host,
2024-04-14 22:45:38 +02:00
socks_cf_adjust_pollset,
2023-05-23 16:38:00 +02:00
Curl_cf_def_data_pending,
Curl_cf_def_send,
Curl_cf_def_recv,
2023-07-02 19:51:09 +02:00
Curl_cf_def_cntrl,
Curl_cf_def_conn_is_alive,
Curl_cf_def_conn_keep_alive,
Curl_cf_def_query,
2023-05-23 16:38:00 +02:00
};
2023-07-02 19:51:09 +02:00
CURLcode Curl_cf_socks_proxy_insert_after(struct Curl_cfilter *cf_at,
struct Curl_easy *data)
2023-05-23 16:38:00 +02:00
{
struct Curl_cfilter *cf;
CURLcode result;
2023-07-02 19:51:09 +02:00
(void)data;
result = Curl_cf_create(&cf, &Curl_cft_socks_proxy, NULL);
2023-05-23 16:38:00 +02:00
if(!result)
2023-07-02 19:51:09 +02:00
Curl_conn_cf_insert_after(cf_at, cf);
2023-05-23 16:38:00 +02:00
return result;
}
2015-04-27 22:25:09 +02:00
#endif /* CURL_DISABLE_PROXY */