2006-09-23 15:09:39 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2011-01-04 17:07:58 -05:00
|
|
|
* Copyright (C) 1998 - 2011, 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
|
|
|
|
* are also available at http://curl.haxx.se/docs/copyright.html.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
2006-09-23 15:07:20 -04:00
|
|
|
#include "setup.h"
|
|
|
|
|
2009-03-15 09:43:47 -04:00
|
|
|
#if !defined(CURL_DISABLE_PROXY) || defined(USE_WINDOWS_SSPI)
|
2006-09-24 19:55:53 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_STDLIB_H
|
|
|
|
#include <stdlib.h>
|
|
|
|
#endif
|
2007-02-21 13:05:38 -05:00
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
#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
|
|
|
|
2006-09-23 15:07:20 -04:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "strequal.h"
|
|
|
|
#include "select.h"
|
|
|
|
#include "connect.h"
|
|
|
|
#include "timeval.h"
|
|
|
|
#include "socks.h"
|
|
|
|
|
|
|
|
/* The last #include file should be: */
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This is STUPID BLOCKING behaviour which we frown upon, but right now this
|
|
|
|
* is what we have...
|
|
|
|
*/
|
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 */
|
|
|
|
ssize_t *n, /* amount bytes read */
|
|
|
|
long conn_timeout) /* timeout for data wait
|
|
|
|
relative to
|
|
|
|
conn->created */
|
2006-09-23 15:07:20 -04:00
|
|
|
{
|
|
|
|
ssize_t nread;
|
|
|
|
ssize_t allread = 0;
|
|
|
|
int result;
|
|
|
|
struct timeval tvnow;
|
|
|
|
long conntime;
|
|
|
|
*n = 0;
|
2010-02-02 11:25:07 -05:00
|
|
|
for(;;) {
|
2006-09-23 15:07:20 -04:00
|
|
|
tvnow = Curl_tvnow();
|
|
|
|
/* calculating how long connection is establishing */
|
|
|
|
conntime = Curl_tvdiff(tvnow, conn->created);
|
|
|
|
if(conntime > conn_timeout) {
|
|
|
|
/* 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
|
|
|
}
|
2007-03-26 19:23:46 -04:00
|
|
|
if(Curl_socket_ready(sockfd, CURL_SOCKET_BAD,
|
2006-09-23 15:07:20 -04:00
|
|
|
(int)(conn_timeout - conntime)) <= 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;
|
|
|
|
else 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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function logs in to a SOCKS4 proxy and sends the specifics to the final
|
|
|
|
* destination server.
|
|
|
|
*
|
|
|
|
* Reference :
|
|
|
|
* http://socks.permeo.com/protocol/socks4.protocol
|
|
|
|
*
|
|
|
|
* 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)"
|
|
|
|
*/
|
|
|
|
CURLcode Curl_SOCKS4(const char *proxy_name,
|
2007-08-27 02:31:28 -04:00
|
|
|
const char *hostname,
|
2007-02-19 06:53:54 -05:00
|
|
|
int remote_port,
|
|
|
|
int sockindex,
|
2008-01-02 16:40:11 -05:00
|
|
|
struct connectdata *conn,
|
|
|
|
bool protocol4a)
|
2006-09-23 15:07:20 -04:00
|
|
|
{
|
2008-01-02 16:40:11 -05:00
|
|
|
#define SOCKS4REQLEN 262
|
|
|
|
unsigned char socksreq[SOCKS4REQLEN]; /* room for SOCKS4 request incl. user
|
|
|
|
id */
|
2006-09-23 15:07:20 -04:00
|
|
|
int result;
|
|
|
|
CURLcode code;
|
2007-02-19 06:53:54 -05:00
|
|
|
curl_socket_t sock = conn->sock[sockindex];
|
2006-09-23 15:07:20 -04:00
|
|
|
long timeout;
|
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
|
|
|
|
/* get timeout */
|
2011-01-04 17:07:58 -05:00
|
|
|
timeout = Curl_timeleft(data, NULL, TRUE);
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2008-02-11 17:03:31 -05:00
|
|
|
if(timeout < 0) {
|
|
|
|
/* time-out, bail out, go home */
|
|
|
|
failf(data, "Connection time-out");
|
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
|
|
|
}
|
|
|
|
|
2009-07-09 17:47:24 -04:00
|
|
|
curlx_nonblock(sock, FALSE);
|
2006-09-23 15:07:20 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Compose socks4 request
|
|
|
|
*
|
|
|
|
* Request format
|
|
|
|
*
|
|
|
|
* +----+----+----+----+----+----+----+----+----+----+....+----+
|
|
|
|
* | VN | CD | DSTPORT | DSTIP | USERID |NULL|
|
|
|
|
* +----+----+----+----+----+----+----+----+----+----+....+----+
|
|
|
|
* # of bytes: 1 1 2 4 variable 1
|
|
|
|
*/
|
|
|
|
|
|
|
|
socksreq[0] = 4; /* version (SOCKS4) */
|
|
|
|
socksreq[1] = 1; /* connect */
|
2011-05-26 08:53:13 -04:00
|
|
|
socksreq[2] = (unsigned char)((remote_port >> 8) & 0xff); /* PORT MSB */
|
|
|
|
socksreq[3] = (unsigned char)(remote_port & 0xff); /* PORT LSB */
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2008-01-02 16:40:11 -05:00
|
|
|
/* DNS resolve only for SOCKS4, not SOCKS4a */
|
2011-04-20 09:17:42 -04:00
|
|
|
if(!protocol4a) {
|
2006-09-23 15:07:20 -04:00
|
|
|
struct Curl_dns_entry *dns;
|
|
|
|
Curl_addrinfo *hp=NULL;
|
|
|
|
int rc;
|
|
|
|
|
2007-02-19 06:53:54 -05:00
|
|
|
rc = Curl_resolv(conn, hostname, remote_port, &dns);
|
2006-09-23 15:07:20 -04:00
|
|
|
|
|
|
|
if(rc == CURLRESOLV_ERROR)
|
|
|
|
return CURLE_COULDNT_RESOLVE_PROXY;
|
|
|
|
|
|
|
|
if(rc == CURLRESOLV_PENDING)
|
2010-04-16 17:03:55 -04:00
|
|
|
/* ignores the return code, but 'dns' remains NULL on failure */
|
2011-01-30 18:10:35 -05:00
|
|
|
(void)Curl_resolver_wait_resolv(conn, &dns);
|
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)
|
|
|
|
hp=dns->addr;
|
2007-11-05 04:45:09 -05:00
|
|
|
if(hp) {
|
2006-09-23 15:07:20 -04:00
|
|
|
char buf[64];
|
|
|
|
unsigned short ip[4];
|
|
|
|
Curl_printable_address(hp, buf, sizeof(buf));
|
|
|
|
|
|
|
|
if(4 == sscanf( buf, "%hu.%hu.%hu.%hu",
|
|
|
|
&ip[0], &ip[1], &ip[2], &ip[3])) {
|
|
|
|
/* Set DSTIP */
|
|
|
|
socksreq[4] = (unsigned char)ip[0];
|
|
|
|
socksreq[5] = (unsigned char)ip[1];
|
|
|
|
socksreq[6] = (unsigned char)ip[2];
|
|
|
|
socksreq[7] = (unsigned char)ip[3];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
hp = NULL; /* fail! */
|
|
|
|
|
|
|
|
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);
|
2006-09-23 15:07:20 -04:00
|
|
|
return CURLE_COULDNT_RESOLVE_HOST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is currently not supporting "Identification Protocol (RFC1413)".
|
|
|
|
*/
|
|
|
|
socksreq[8] = 0; /* ensure empty userid is NUL-terminated */
|
2007-11-05 04:45:09 -05:00
|
|
|
if(proxy_name)
|
2006-09-23 15:07:20 -04:00
|
|
|
strlcat((char*)socksreq + 8, proxy_name, sizeof(socksreq) - 8);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make connection
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
ssize_t actualread;
|
|
|
|
ssize_t written;
|
2008-01-02 16:40:11 -05:00
|
|
|
ssize_t hostnamelen = 0;
|
2006-09-23 15:07:20 -04:00
|
|
|
int packetsize = 9 +
|
|
|
|
(int)strlen((char*)socksreq + 8); /* size including NUL */
|
|
|
|
|
2008-01-02 16:40:11 -05:00
|
|
|
/* If SOCKS4a, set special invalid IP address 0.0.0.x */
|
2011-04-20 09:17:42 -04:00
|
|
|
if(protocol4a) {
|
2008-01-02 16:40:11 -05:00
|
|
|
socksreq[4] = 0;
|
|
|
|
socksreq[5] = 0;
|
|
|
|
socksreq[6] = 0;
|
|
|
|
socksreq[7] = 1;
|
|
|
|
/* If still enough room in buffer, also append hostname */
|
2008-01-09 14:11:56 -05:00
|
|
|
hostnamelen = (ssize_t)strlen(hostname) + 1; /* length including NUL */
|
2011-04-20 09:17:42 -04:00
|
|
|
if(packetsize + hostnamelen <= SOCKS4REQLEN)
|
2008-01-02 16:40:11 -05:00
|
|
|
strcpy((char*)socksreq + packetsize, hostname);
|
|
|
|
else
|
|
|
|
hostnamelen = 0; /* Flag: hostname did not fit in buffer */
|
|
|
|
}
|
|
|
|
|
2006-09-23 15:07:20 -04:00
|
|
|
/* Send request */
|
2008-06-20 06:43:32 -04:00
|
|
|
code = Curl_write_plain(conn, sock, (char *)socksreq,
|
|
|
|
packetsize + hostnamelen,
|
|
|
|
&written);
|
2008-01-02 16:40:11 -05:00
|
|
|
if((code != CURLE_OK) || (written != packetsize + hostnamelen)) {
|
2006-09-23 15:07:20 -04:00
|
|
|
failf(data, "Failed to send SOCKS4 connect request.");
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
2011-04-20 09:17:42 -04:00
|
|
|
if(protocol4a && hostnamelen == 0) {
|
2008-01-02 16:40:11 -05:00
|
|
|
/* SOCKS4a with very long hostname - send that name separately */
|
2008-01-09 14:11:56 -05:00
|
|
|
hostnamelen = (ssize_t)strlen(hostname) + 1;
|
2008-06-20 06:43:32 -04:00
|
|
|
code = Curl_write_plain(conn, sock, (char *)hostname, hostnamelen,
|
|
|
|
&written);
|
2008-01-02 16:40:11 -05:00
|
|
|
if((code != CURLE_OK) || (written != hostnamelen)) {
|
|
|
|
failf(data, "Failed to send SOCKS4 connect request.");
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
}
|
2006-09-23 15:07:20 -04:00
|
|
|
|
|
|
|
packetsize = 8; /* receive data size */
|
|
|
|
|
|
|
|
/* Receive response */
|
2009-01-28 16:33:58 -05:00
|
|
|
result = Curl_blockread_all(conn, sock, (char *)socksreq, packetsize,
|
2006-09-23 15:07:20 -04:00
|
|
|
&actualread, timeout);
|
2007-11-05 04:45:09 -05:00
|
|
|
if((result != CURLE_OK) || (actualread != packetsize)) {
|
2006-09-23 15:07:20 -04:00
|
|
|
failf(data, "Failed to receive SOCKS4 connect request ack.");
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 ? */
|
2007-11-05 04:45:09 -05:00
|
|
|
if(socksreq[0] != 0) {
|
2006-09-23 15:07:20 -04:00
|
|
|
failf(data,
|
|
|
|
"SOCKS4 reply has wrong version, version should be 4.");
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Result */
|
2011-04-20 09:17:42 -04:00
|
|
|
switch(socksreq[1]) {
|
2006-09-23 15:07:20 -04:00
|
|
|
case 90:
|
2011-04-20 09:17:42 -04:00
|
|
|
if(protocol4a)
|
2008-01-02 16:40:11 -05:00
|
|
|
infof(data, "SOCKS4a request granted.\n");
|
|
|
|
else
|
|
|
|
infof(data, "SOCKS4 request granted.\n");
|
2006-09-23 15:07:20 -04:00
|
|
|
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],
|
2011-05-26 08:53:13 -04:00
|
|
|
((socksreq[8] << 8) | socksreq[9]),
|
2006-09-23 15:07:20 -04:00
|
|
|
socksreq[1]);
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
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],
|
2011-05-26 08:53:13 -04:00
|
|
|
((socksreq[8] << 8) | socksreq[9]),
|
2006-09-23 15:07:20 -04:00
|
|
|
socksreq[1]);
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
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],
|
2011-05-26 08:53:13 -04:00
|
|
|
((socksreq[8] << 8) | socksreq[9]),
|
2006-09-23 15:07:20 -04:00
|
|
|
socksreq[1]);
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
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],
|
2011-05-26 08:53:13 -04:00
|
|
|
((socksreq[8] << 8) | socksreq[9]),
|
2006-09-23 15:07:20 -04:00
|
|
|
socksreq[1]);
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-09 17:47:24 -04:00
|
|
|
curlx_nonblock(sock, TRUE);
|
2006-09-23 15:07:20 -04:00
|
|
|
|
|
|
|
return CURLE_OK; /* Proxy was successful! */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function logs in to a SOCKS5 proxy and sends the specifics to the final
|
|
|
|
* destination server.
|
|
|
|
*/
|
|
|
|
CURLcode Curl_SOCKS5(const char *proxy_name,
|
|
|
|
const char *proxy_password,
|
2007-08-27 02:31:28 -04:00
|
|
|
const char *hostname,
|
2007-02-19 06:53:54 -05:00
|
|
|
int remote_port,
|
|
|
|
int sockindex,
|
2006-09-23 15:07:20 -04:00
|
|
|
struct connectdata *conn)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
unsigned char socksreq[600]; /* room for large user/pw (255 max each) */
|
|
|
|
ssize_t actualread;
|
|
|
|
ssize_t written;
|
|
|
|
int result;
|
|
|
|
CURLcode code;
|
2007-02-19 06:53:54 -05:00
|
|
|
curl_socket_t sock = conn->sock[sockindex];
|
2006-09-23 15:07:20 -04:00
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
long timeout;
|
2011-04-27 08:44:00 -04:00
|
|
|
bool socks5_resolve_local = (bool)(conn->proxytype == CURLPROXY_SOCKS5);
|
2008-01-04 18:01:00 -05:00
|
|
|
const size_t hostname_len = strlen(hostname);
|
2008-01-12 23:39:32 -05:00
|
|
|
ssize_t packetsize = 0;
|
2008-01-04 18:01:00 -05:00
|
|
|
|
|
|
|
/* RFC1928 chapter 5 specifies max 255 chars for domain name in packet */
|
2011-04-20 09:17:42 -04:00
|
|
|
if(!socks5_resolve_local && hostname_len > 255) {
|
2008-01-04 18:01:00 -05:00
|
|
|
infof(conn->data,"SOCKS5: server resolving disabled for hostnames of "
|
2010-02-04 14:44:31 -05:00
|
|
|
"length > 255 [actual len=%zu]\n", hostname_len);
|
2008-01-04 18:01:00 -05:00
|
|
|
socks5_resolve_local = TRUE;
|
|
|
|
}
|
2006-09-23 15:07:20 -04:00
|
|
|
|
|
|
|
/* get timeout */
|
2011-01-04 17:07:58 -05:00
|
|
|
timeout = Curl_timeleft(data, NULL, TRUE);
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2008-02-11 17:03:31 -05:00
|
|
|
if(timeout < 0) {
|
|
|
|
/* time-out, bail out, go home */
|
|
|
|
failf(data, "Connection time-out");
|
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
|
|
|
}
|
|
|
|
|
2009-07-09 17:47:24 -04:00
|
|
|
curlx_nonblock(sock, TRUE);
|
2006-09-23 15:07:20 -04:00
|
|
|
|
|
|
|
/* wait until socket gets connected */
|
2007-03-26 19:23:46 -04:00
|
|
|
result = Curl_socket_ready(CURL_SOCKET_BAD, sock, (int)timeout);
|
2006-09-23 15:07:20 -04:00
|
|
|
|
|
|
|
if(-1 == result) {
|
|
|
|
failf(conn->data, "SOCKS5: no connection here");
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
else if(0 == result) {
|
|
|
|
failf(conn->data, "SOCKS5: connection timeout");
|
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
|
|
|
}
|
|
|
|
|
2007-04-16 12:34:08 -04:00
|
|
|
if(result & CURL_CSELECT_ERR) {
|
2011-04-19 09:54:13 -04:00
|
|
|
failf(conn->data, "SOCKS5: error occurred during connection");
|
2006-09-23 15:07:20 -04:00
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
socksreq[0] = 5; /* version */
|
2009-01-28 16:33:58 -05:00
|
|
|
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
|
|
|
|
socksreq[1] = (char)(proxy_name ? 3 : 2); /* number of methods (below) */
|
|
|
|
socksreq[2] = 0; /* no authentication */
|
|
|
|
socksreq[3] = 1; /* gssapi */
|
|
|
|
socksreq[4] = 2; /* username/password */
|
|
|
|
#else
|
2006-09-23 15:07:20 -04:00
|
|
|
socksreq[1] = (char)(proxy_name ? 2 : 1); /* number of methods (below) */
|
|
|
|
socksreq[2] = 0; /* no authentication */
|
|
|
|
socksreq[3] = 2; /* username/password */
|
2009-01-28 16:33:58 -05:00
|
|
|
#endif
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2009-07-09 17:47:24 -04:00
|
|
|
curlx_nonblock(sock, FALSE);
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2008-06-20 06:43:32 -04:00
|
|
|
code = Curl_write_plain(conn, sock, (char *)socksreq, (2 + (int)socksreq[1]),
|
|
|
|
&written);
|
2007-11-05 04:45:09 -05:00
|
|
|
if((code != CURLE_OK) || (written != (2 + (int)socksreq[1]))) {
|
2006-09-23 15:07:20 -04:00
|
|
|
failf(data, "Unable to send initial SOCKS5 request.");
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
2009-07-09 17:47:24 -04:00
|
|
|
curlx_nonblock(sock, TRUE);
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2007-03-26 19:23:46 -04:00
|
|
|
result = Curl_socket_ready(sock, CURL_SOCKET_BAD, (int)timeout);
|
2006-09-23 15:07:20 -04:00
|
|
|
|
|
|
|
if(-1 == result) {
|
|
|
|
failf(conn->data, "SOCKS5 nothing to read");
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
else if(0 == result) {
|
|
|
|
failf(conn->data, "SOCKS5 read timeout");
|
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
|
|
|
}
|
|
|
|
|
2007-04-16 12:34:08 -04:00
|
|
|
if(result & CURL_CSELECT_ERR) {
|
2011-04-19 09:54:13 -04:00
|
|
|
failf(conn->data, "SOCKS5 read error occurred");
|
2006-09-23 15:07:20 -04:00
|
|
|
return CURLE_RECV_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-07-09 17:47:24 -04:00
|
|
|
curlx_nonblock(sock, FALSE);
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2009-01-28 16:33:58 -05:00
|
|
|
result=Curl_blockread_all(conn, sock, (char *)socksreq, 2, &actualread,
|
|
|
|
timeout);
|
2007-11-05 04:45:09 -05:00
|
|
|
if((result != CURLE_OK) || (actualread != 2)) {
|
2006-09-23 15:07:20 -04:00
|
|
|
failf(data, "Unable to receive initial SOCKS5 response.");
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
2007-11-05 04:45:09 -05:00
|
|
|
if(socksreq[0] != 5) {
|
2006-09-23 15:07:20 -04:00
|
|
|
failf(data, "Received invalid version in initial SOCKS5 response.");
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
2007-11-05 04:45:09 -05:00
|
|
|
if(socksreq[1] == 0) {
|
2006-09-23 15:07:20 -04:00
|
|
|
/* Nothing to do, no authentication needed */
|
|
|
|
;
|
|
|
|
}
|
2009-01-28 16:33:58 -05:00
|
|
|
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
|
|
|
|
else if(socksreq[1] == 1) {
|
|
|
|
code = Curl_SOCKS5_gssapi_negotiate(sockindex, conn);
|
|
|
|
if(code != CURLE_OK) {
|
|
|
|
failf(data, "Unable to negotiate SOCKS5 gssapi context.");
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2007-11-05 04:45:09 -05:00
|
|
|
else if(socksreq[1] == 2) {
|
2006-09-23 15:07:20 -04:00
|
|
|
/* Needs user name and password */
|
|
|
|
size_t userlen, pwlen;
|
|
|
|
int len;
|
|
|
|
if(proxy_name && proxy_password) {
|
|
|
|
userlen = strlen(proxy_name);
|
2007-03-30 15:59:15 -04:00
|
|
|
pwlen = strlen(proxy_password);
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
userlen = 0;
|
|
|
|
pwlen = 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 */
|
2010-04-24 06:40:00 -04:00
|
|
|
socksreq[len++] = (unsigned char) userlen;
|
|
|
|
if(proxy_name && userlen)
|
|
|
|
memcpy(socksreq + len, proxy_name, userlen);
|
2009-08-28 23:53:09 -04:00
|
|
|
len += (int)userlen;
|
2010-04-24 06:40:00 -04:00
|
|
|
socksreq[len++] = (unsigned char) pwlen;
|
|
|
|
if(proxy_password && pwlen)
|
|
|
|
memcpy(socksreq + len, proxy_password, pwlen);
|
2009-08-28 23:53:09 -04:00
|
|
|
len += (int)pwlen;
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2008-06-20 06:43:32 -04:00
|
|
|
code = Curl_write_plain(conn, sock, (char *)socksreq, len, &written);
|
2007-11-05 04:45:09 -05:00
|
|
|
if((code != CURLE_OK) || (len != written)) {
|
2006-09-23 15:07:20 -04:00
|
|
|
failf(data, "Failed to send SOCKS5 sub-negotiation request.");
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
2009-01-28 16:33:58 -05:00
|
|
|
result=Curl_blockread_all(conn, sock, (char *)socksreq, 2, &actualread,
|
2006-09-23 15:07:20 -04:00
|
|
|
timeout);
|
2007-11-05 04:45:09 -05:00
|
|
|
if((result != CURLE_OK) || (actualread != 2)) {
|
2006-09-23 15:07:20 -04:00
|
|
|
failf(data, "Unable to receive SOCKS5 sub-negotiation response.");
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ignore the first (VER) byte */
|
2007-11-05 04:45:09 -05:00
|
|
|
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]);
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Everything is good so far, user was authenticated! */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* error */
|
2009-01-28 16:33:58 -05:00
|
|
|
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
|
|
|
|
if(socksreq[1] == 255) {
|
|
|
|
#else
|
2007-11-05 04:45:09 -05:00
|
|
|
if(socksreq[1] == 1) {
|
2006-09-23 15:07:20 -04:00
|
|
|
failf(data,
|
|
|
|
"SOCKS5 GSSAPI per-message authentication is not supported.");
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
2007-11-05 04:45:09 -05:00
|
|
|
else if(socksreq[1] == 255) {
|
2009-01-28 16:33:58 -05:00
|
|
|
#endif
|
2007-11-05 04:45:09 -05:00
|
|
|
if(!proxy_name || !*proxy_name) {
|
2006-09-23 15:07:20 -04:00
|
|
|
failf(data,
|
|
|
|
"No authentication method was acceptable. (It is quite likely"
|
|
|
|
" that the SOCKS5 server wanted a username/password, since none"
|
|
|
|
" was supplied to the server on this connection.)");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
failf(data, "No authentication method was acceptable.");
|
|
|
|
}
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
failf(data,
|
|
|
|
"Undocumented SOCKS5 mode attempted to be used by server.");
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Authentication is complete, now specify destination to the proxy */
|
|
|
|
socksreq[0] = 5; /* version (SOCKS5) */
|
|
|
|
socksreq[1] = 1; /* connect */
|
|
|
|
socksreq[2] = 0; /* must be zero */
|
|
|
|
|
2008-01-04 18:01:00 -05:00
|
|
|
if(!socks5_resolve_local) {
|
2008-01-14 14:40:10 -05:00
|
|
|
packetsize = (ssize_t)(5 + hostname_len + 2);
|
2008-01-04 18:01:00 -05:00
|
|
|
|
|
|
|
socksreq[3] = 3; /* ATYP: domain name = 3 */
|
|
|
|
socksreq[4] = (char) hostname_len; /* address length */
|
|
|
|
memcpy(&socksreq[5], hostname, hostname_len); /* address bytes w/o NULL */
|
|
|
|
|
2011-05-26 08:53:13 -04:00
|
|
|
/* PORT MSB */
|
|
|
|
socksreq[hostname_len+5] = (unsigned char)((remote_port >> 8) & 0xff);
|
|
|
|
/* PORT LSB */
|
|
|
|
socksreq[hostname_len+6] = (unsigned char)(remote_port & 0xff);
|
2008-01-04 18:01:00 -05:00
|
|
|
}
|
|
|
|
else {
|
2006-09-23 15:07:20 -04:00
|
|
|
struct Curl_dns_entry *dns;
|
|
|
|
Curl_addrinfo *hp=NULL;
|
2007-02-19 06:53:54 -05:00
|
|
|
int rc = Curl_resolv(conn, hostname, remote_port, &dns);
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2008-01-04 18:01:00 -05:00
|
|
|
packetsize = 10;
|
|
|
|
|
|
|
|
socksreq[3] = 1; /* IPv4 = 1 */
|
|
|
|
|
2006-09-23 15:07:20 -04:00
|
|
|
if(rc == CURLRESOLV_ERROR)
|
|
|
|
return CURLE_COULDNT_RESOLVE_HOST;
|
|
|
|
|
2010-04-16 16:58:04 -04:00
|
|
|
if(rc == CURLRESOLV_PENDING) {
|
2006-09-23 15:07:20 -04:00
|
|
|
/* this requires that we're in "wait for resolve" state */
|
2011-01-30 18:10:35 -05:00
|
|
|
code = Curl_resolver_wait_resolv(conn, &dns);
|
2010-05-31 10:36:05 -04:00
|
|
|
if(code != CURLE_OK)
|
|
|
|
return code;
|
2010-04-16 16:58:04 -04:00
|
|
|
}
|
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)
|
|
|
|
hp=dns->addr;
|
2007-11-05 04:45:09 -05:00
|
|
|
if(hp) {
|
2006-09-23 15:07:20 -04:00
|
|
|
char buf[64];
|
|
|
|
unsigned short ip[4];
|
|
|
|
Curl_printable_address(hp, buf, sizeof(buf));
|
|
|
|
|
|
|
|
if(4 == sscanf( buf, "%hu.%hu.%hu.%hu",
|
|
|
|
&ip[0], &ip[1], &ip[2], &ip[3])) {
|
|
|
|
socksreq[4] = (unsigned char)ip[0];
|
|
|
|
socksreq[5] = (unsigned char)ip[1];
|
|
|
|
socksreq[6] = (unsigned char)ip[2];
|
|
|
|
socksreq[7] = (unsigned char)ip[3];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
hp = NULL; /* fail! */
|
|
|
|
|
|
|
|
Curl_resolv_unlock(data, dns); /* not used anymore from now on */
|
|
|
|
}
|
|
|
|
if(!hp) {
|
|
|
|
failf(data, "Failed to resolve \"%s\" for SOCKS5 connect.",
|
2007-02-19 06:53:54 -05:00
|
|
|
hostname);
|
2006-09-23 15:07:20 -04:00
|
|
|
return CURLE_COULDNT_RESOLVE_HOST;
|
|
|
|
}
|
2008-01-04 18:01:00 -05:00
|
|
|
|
2011-05-26 08:53:13 -04:00
|
|
|
socksreq[8] = (unsigned char)((remote_port >> 8) & 0xff); /* PORT MSB */
|
|
|
|
socksreq[9] = (unsigned char)(remote_port & 0xff); /* PORT LSB */
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
|
|
|
|
2009-01-28 16:33:58 -05:00
|
|
|
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
|
|
|
|
if(conn->socks5_gssapi_enctype) {
|
|
|
|
failf(data, "SOCKS5 gssapi protection not yet implemented.");
|
2011-04-20 09:17:42 -04:00
|
|
|
}
|
|
|
|
else
|
2009-01-28 16:33:58 -05:00
|
|
|
#endif
|
2011-04-20 09:17:42 -04:00
|
|
|
code = Curl_write_plain(conn, sock, (char *)socksreq, packetsize,
|
|
|
|
&written);
|
2008-01-04 18:01:00 -05:00
|
|
|
if((code != CURLE_OK) || (written != packetsize)) {
|
|
|
|
failf(data, "Failed to send SOCKS5 connect request.");
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2008-01-04 18:01:00 -05:00
|
|
|
packetsize = 10; /* minimum packet size is 10 */
|
|
|
|
|
2009-01-28 16:33:58 -05:00
|
|
|
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
|
|
|
|
if(conn->socks5_gssapi_enctype) {
|
|
|
|
failf(data, "SOCKS5 gssapi protection not yet implemented.");
|
2011-04-20 09:17:42 -04:00
|
|
|
}
|
|
|
|
else
|
2009-01-28 16:33:58 -05:00
|
|
|
#endif
|
|
|
|
result = Curl_blockread_all(conn, sock, (char *)socksreq, packetsize,
|
2008-01-04 18:01:00 -05:00
|
|
|
&actualread, timeout);
|
|
|
|
if((result != CURLE_OK) || (actualread != packetsize)) {
|
|
|
|
failf(data, "Failed to receive SOCKS5 connect request ack.");
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2008-01-04 18:01:00 -05:00
|
|
|
if(socksreq[0] != 5) { /* version */
|
|
|
|
failf(data,
|
|
|
|
"SOCKS5 reply has wrong version, version should be 5.");
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
if(socksreq[1] != 0) { /* Anything besides 0 is an error */
|
|
|
|
failf(data,
|
|
|
|
"Can't complete SOCKS5 connection to %d.%d.%d.%d:%d. (%d)",
|
|
|
|
(unsigned char)socksreq[4], (unsigned char)socksreq[5],
|
|
|
|
(unsigned char)socksreq[6], (unsigned char)socksreq[7],
|
2011-05-26 08:53:13 -04:00
|
|
|
((socksreq[8] << 8) | socksreq[9]),
|
2008-01-04 18:01:00 -05:00
|
|
|
socksreq[1]);
|
2006-09-24 19:55:53 -04:00
|
|
|
return CURLE_COULDNT_CONNECT;
|
2008-01-04 18:01:00 -05:00
|
|
|
}
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2008-01-04 18:01:00 -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];
|
|
|
|
packetsize = 5 + addrlen + 2;
|
|
|
|
}
|
|
|
|
else if(socksreq[3] == 4) {
|
|
|
|
/* IPv6 */
|
|
|
|
packetsize = 4 + 16 + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
if(!conn->socks5_gssapi_enctype) {
|
|
|
|
/* decrypt_gssapi_blockread already read the whole packet */
|
|
|
|
#endif
|
|
|
|
if(packetsize > 10) {
|
|
|
|
packetsize -= 10;
|
|
|
|
result = Curl_blockread_all(conn, sock, (char *)&socksreq[10],
|
|
|
|
packetsize, &actualread, timeout);
|
|
|
|
if((result != CURLE_OK) || (actualread != packetsize)) {
|
|
|
|
failf(data, "Failed to receive SOCKS5 connect request ack.");
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
2009-01-28 16:33:58 -05:00
|
|
|
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
|
2006-09-23 15:07:20 -04:00
|
|
|
}
|
2009-01-28 16:33:58 -05:00
|
|
|
#endif
|
2006-09-23 15:07:20 -04:00
|
|
|
|
2009-07-09 17:47:24 -04:00
|
|
|
curlx_nonblock(sock, TRUE);
|
2006-09-23 15:07:20 -04:00
|
|
|
return CURLE_OK; /* Proxy was successful! */
|
|
|
|
}
|
2008-09-29 17:46:04 -04:00
|
|
|
|
|
|
|
#endif /* CURL_DISABLE_PROXY */
|
|
|
|
|