123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784 |
- #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"
- #include "connect.h"
- #include "timeval.h"
- #include "socks.h"
- #include "memdebug.h"
- int Curl_blockread_all(struct connectdata *conn,
- curl_socket_t sockfd,
- char *buf,
- ssize_t buffersize,
- ssize_t *n)
- {
- ssize_t nread;
- ssize_t allread = 0;
- int result;
- time_t timeleft;
- *n = 0;
- for(;;) {
- timeleft = Curl_timeleft(conn->data, NULL, TRUE);
- if(timeleft < 0) {
-
- result = CURLE_OPERATION_TIMEDOUT;
- break;
- }
- if(SOCKET_READABLE(sockfd, timeleft) <= 0) {
- result = ~CURLE_OK;
- break;
- }
- result = Curl_read_plain(sockfd, buf, buffersize, &nread);
- if(CURLE_AGAIN == result)
- continue;
- else if(result)
- break;
- 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;
- }
- CURLcode Curl_SOCKS4(const char *proxy_name,
- const char *hostname,
- int remote_port,
- int sockindex,
- struct connectdata *conn)
- {
- const bool protocol4a =
- (conn->socks_proxy.proxytype == CURLPROXY_SOCKS4A) ? TRUE : FALSE;
- #define SOCKS4REQLEN 262
- unsigned char socksreq[SOCKS4REQLEN];
- int result;
- CURLcode code;
- curl_socket_t sock = conn->sock[sockindex];
- struct Curl_easy *data = conn->data;
- if(Curl_timeleft(data, NULL, TRUE) < 0) {
-
- failf(data, "Connection time-out");
- return CURLE_OPERATION_TIMEDOUT;
- }
- if(conn->bits.httpproxy)
- infof(conn->data, "SOCKS4%s: connecting to HTTP proxy %s port %d\n",
- protocol4a ? "a" : "", hostname, remote_port);
- (void)curlx_nonblock(sock, FALSE);
- infof(data, "SOCKS4 communication to %s:%d\n", hostname, remote_port);
-
- socksreq[0] = 4;
- socksreq[1] = 1;
- socksreq[2] = (unsigned char)((remote_port >> 8) & 0xff);
- socksreq[3] = (unsigned char)(remote_port & 0xff);
-
- if(!protocol4a) {
- struct Curl_dns_entry *dns;
- Curl_addrinfo *hp=NULL;
- int rc;
- rc = Curl_resolv(conn, hostname, remote_port, &dns);
- if(rc == CURLRESOLV_ERROR)
- return CURLE_COULDNT_RESOLVE_PROXY;
- if(rc == CURLRESOLV_PENDING)
-
- (void)Curl_resolver_wait_resolv(conn, &dns);
-
- if(dns)
- hp=dns->addr;
- if(hp) {
- char buf[64];
- Curl_printable_address(hp, buf, sizeof(buf));
- if(hp->ai_family == AF_INET) {
- struct sockaddr_in *saddr_in;
- 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];
- infof(data, "SOCKS4 connect to IPv4 %s (locally resolved)\n", buf);
- }
- else {
- hp = NULL;
- failf(data, "SOCKS4 connection to %s not supported\n", buf);
- }
- Curl_resolv_unlock(data, dns);
- }
- if(!hp) {
- failf(data, "Failed to resolve \"%s\" for SOCKS4 connect.",
- hostname);
- return CURLE_COULDNT_RESOLVE_HOST;
- }
- }
-
- socksreq[8] = 0;
- if(proxy_name) {
- size_t plen = strlen(proxy_name);
- if(plen >= sizeof(socksreq) - 8) {
- failf(data, "Too long SOCKS proxy name, can't use!\n");
- return CURLE_COULDNT_CONNECT;
- }
-
- memcpy(socksreq + 8, proxy_name, plen+1);
- }
-
- {
- ssize_t actualread;
- ssize_t written;
- ssize_t hostnamelen = 0;
- int packetsize = 9 +
- (int)strlen((char *)socksreq + 8);
-
- if(protocol4a) {
- socksreq[4] = 0;
- socksreq[5] = 0;
- socksreq[6] = 0;
- socksreq[7] = 1;
-
- hostnamelen = (ssize_t)strlen(hostname) + 1;
- if(packetsize + hostnamelen <= SOCKS4REQLEN)
- strcpy((char *)socksreq + packetsize, hostname);
- else
- hostnamelen = 0;
- }
-
- code = Curl_write_plain(conn, sock, (char *)socksreq,
- packetsize + hostnamelen,
- &written);
- if(code || (written != packetsize + hostnamelen)) {
- failf(data, "Failed to send SOCKS4 connect request.");
- return CURLE_COULDNT_CONNECT;
- }
- if(protocol4a && hostnamelen == 0) {
-
- hostnamelen = (ssize_t)strlen(hostname) + 1;
- code = Curl_write_plain(conn, sock, (char *)hostname, hostnamelen,
- &written);
- if(code || (written != hostnamelen)) {
- failf(data, "Failed to send SOCKS4 connect request.");
- return CURLE_COULDNT_CONNECT;
- }
- }
- packetsize = 8;
-
- result = Curl_blockread_all(conn, sock, (char *)socksreq, packetsize,
- &actualread);
- if(result || (actualread != packetsize)) {
- failf(data, "Failed to receive SOCKS4 connect request ack.");
- return CURLE_COULDNT_CONNECT;
- }
-
-
- if(socksreq[0] != 0) {
- failf(data,
- "SOCKS4 reply has wrong version, version should be 4.");
- return CURLE_COULDNT_CONNECT;
- }
-
- 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[8] << 8) | (unsigned char)socksreq[9]),
- (unsigned char)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],
- (((unsigned char)socksreq[8] << 8) | (unsigned char)socksreq[9]),
- (unsigned char)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],
- (((unsigned char)socksreq[8] << 8) | (unsigned char)socksreq[9]),
- (unsigned char)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],
- (((unsigned char)socksreq[8] << 8) | (unsigned char)socksreq[9]),
- (unsigned char)socksreq[1]);
- return CURLE_COULDNT_CONNECT;
- }
- }
- (void)curlx_nonblock(sock, TRUE);
- return CURLE_OK;
- }
- CURLcode Curl_SOCKS5(const char *proxy_name,
- const char *proxy_password,
- const char *hostname,
- int remote_port,
- int sockindex,
- struct connectdata *conn)
- {
-
- unsigned char socksreq[600];
- ssize_t actualread;
- ssize_t written;
- int result;
- CURLcode code;
- curl_socket_t sock = conn->sock[sockindex];
- struct Curl_easy *data = conn->data;
- time_t timeout;
- bool socks5_resolve_local =
- (conn->socks_proxy.proxytype == CURLPROXY_SOCKS5) ? TRUE : FALSE;
- const size_t hostname_len = strlen(hostname);
- ssize_t len = 0;
- if(conn->bits.httpproxy)
- infof(conn->data, "SOCKS5: connecting to HTTP proxy %s port %d\n",
- hostname, remote_port);
-
- 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;
- }
-
- timeout = Curl_timeleft(data, NULL, TRUE);
- if(timeout < 0) {
-
- failf(data, "Connection time-out");
- return CURLE_OPERATION_TIMEDOUT;
- }
- (void)curlx_nonblock(sock, TRUE);
-
- result = SOCKET_WRITABLE(sock, timeout);
- 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;
- }
- if(result & CURL_CSELECT_ERR) {
- failf(conn->data, "SOCKS5: error occurred during connection");
- return CURLE_COULDNT_CONNECT;
- }
- socksreq[0] = 5;
- #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
- socksreq[1] = (char)(proxy_name ? 3 : 2);
- socksreq[2] = 0;
- socksreq[3] = 1;
- socksreq[4] = 2;
- #else
- socksreq[1] = (char)(proxy_name ? 2 : 1);
- socksreq[2] = 0;
- socksreq[3] = 2;
- #endif
- (void)curlx_nonblock(sock, FALSE);
- infof(data, "SOCKS5 communication to %s:%d\n", hostname, remote_port);
- code = Curl_write_plain(conn, sock, (char *)socksreq, (2 + (int)socksreq[1]),
- &written);
- if(code || (written != (2 + (int)socksreq[1]))) {
- failf(data, "Unable to send initial SOCKS5 request.");
- return CURLE_COULDNT_CONNECT;
- }
- (void)curlx_nonblock(sock, TRUE);
- result = SOCKET_READABLE(sock, timeout);
- 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;
- }
- if(result & CURL_CSELECT_ERR) {
- failf(conn->data, "SOCKS5 read error occurred");
- return CURLE_RECV_ERROR;
- }
- (void)curlx_nonblock(sock, FALSE);
- result=Curl_blockread_all(conn, sock, (char *)socksreq, 2, &actualread);
- if(result || (actualread != 2)) {
- failf(data, "Unable to receive initial SOCKS5 response.");
- return CURLE_COULDNT_CONNECT;
- }
- if(socksreq[0] != 5) {
- failf(data, "Received invalid version in initial SOCKS5 response.");
- return CURLE_COULDNT_CONNECT;
- }
- if(socksreq[1] == 0) {
-
- ;
- }
- #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
- else if(socksreq[1] == 1) {
- code = Curl_SOCKS5_gssapi_negotiate(sockindex, conn);
- if(code) {
- failf(data, "Unable to negotiate SOCKS5 GSS-API context.");
- return CURLE_COULDNT_CONNECT;
- }
- }
- #endif
- else if(socksreq[1] == 2) {
-
- size_t proxy_name_len, proxy_password_len;
- if(proxy_name && proxy_password) {
- proxy_name_len = strlen(proxy_name);
- proxy_password_len = strlen(proxy_password);
- }
- else {
- proxy_name_len = 0;
- proxy_password_len = 0;
- }
-
- len = 0;
- socksreq[len++] = 1;
- socksreq[len++] = (unsigned char) proxy_name_len;
- if(proxy_name && proxy_name_len)
- memcpy(socksreq + len, proxy_name, proxy_name_len);
- len += proxy_name_len;
- socksreq[len++] = (unsigned char) proxy_password_len;
- if(proxy_password && proxy_password_len)
- memcpy(socksreq + len, proxy_password, proxy_password_len);
- len += proxy_password_len;
- code = Curl_write_plain(conn, sock, (char *)socksreq, len, &written);
- if(code || (len != written)) {
- failf(data, "Failed to send SOCKS5 sub-negotiation request.");
- return CURLE_COULDNT_CONNECT;
- }
- result=Curl_blockread_all(conn, sock, (char *)socksreq, 2, &actualread);
- if(result || (actualread != 2)) {
- failf(data, "Unable to receive SOCKS5 sub-negotiation response.");
- return CURLE_COULDNT_CONNECT;
- }
-
- if(socksreq[1] != 0) {
- failf(data, "User was rejected by the SOCKS5 server (%d %d).",
- socksreq[0], socksreq[1]);
- return CURLE_COULDNT_CONNECT;
- }
-
- }
- else {
-
- #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
- if(socksreq[1] == 255) {
- #else
- if(socksreq[1] == 1) {
- failf(data,
- "SOCKS5 GSSAPI per-message authentication is not supported.");
- return CURLE_COULDNT_CONNECT;
- }
- else if(socksreq[1] == 255) {
- #endif
- if(!proxy_name || !*proxy_name) {
- 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;
- }
- }
-
- len = 0;
- socksreq[len++] = 5;
- socksreq[len++] = 1;
- socksreq[len++] = 0;
- if(!socks5_resolve_local) {
- socksreq[len++] = 3;
- socksreq[len++] = (char) hostname_len;
- memcpy(&socksreq[len], hostname, hostname_len);
- len += hostname_len;
- }
- else {
- struct Curl_dns_entry *dns;
- Curl_addrinfo *hp = NULL;
- int rc = Curl_resolv(conn, hostname, remote_port, &dns);
- if(rc == CURLRESOLV_ERROR)
- return CURLE_COULDNT_RESOLVE_HOST;
- if(rc == CURLRESOLV_PENDING) {
-
- code = Curl_resolver_wait_resolv(conn, &dns);
- if(code)
- return code;
- }
-
- if(dns)
- hp=dns->addr;
- if(hp) {
- int i;
- char buf[64];
- Curl_printable_address(hp, buf, sizeof(buf));
- if(hp->ai_family == AF_INET) {
- struct sockaddr_in *saddr_in;
- socksreq[len++] = 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];
- }
- infof(data, "SOCKS5 connect to IPv4 %s (locally resolved)\n", buf);
- }
- #ifdef ENABLE_IPV6
- else if(hp->ai_family == AF_INET6) {
- struct sockaddr_in6 *saddr_in6;
- socksreq[len++] = 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];
- }
- infof(data, "SOCKS5 connect to IPv6 %s (locally resolved)\n", buf);
- }
- #endif
- else {
- hp = NULL;
- failf(data, "SOCKS5 connection to %s not supported\n", buf);
- }
- Curl_resolv_unlock(data, dns);
- }
- if(!hp) {
- failf(data, "Failed to resolve \"%s\" for SOCKS5 connect.",
- hostname);
- return CURLE_COULDNT_RESOLVE_HOST;
- }
- }
- socksreq[len++] = (unsigned char)((remote_port >> 8) & 0xff);
- socksreq[len++] = (unsigned char)(remote_port & 0xff);
- #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
- if(conn->socks5_gssapi_enctype) {
- failf(data, "SOCKS5 GSS-API protection not yet implemented.");
- }
- else
- #endif
- code = Curl_write_plain(conn, sock, (char *)socksreq, len, &written);
- if(code || (len != written)) {
- failf(data, "Failed to send SOCKS5 connect request.");
- return CURLE_COULDNT_CONNECT;
- }
- len = 10;
- #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
- if(conn->socks5_gssapi_enctype) {
- failf(data, "SOCKS5 GSS-API protection not yet implemented.");
- }
- else
- #endif
- result = Curl_blockread_all(conn, sock, (char *)socksreq,
- len, &actualread);
- if(result || (len != actualread)) {
- failf(data, "Failed to receive SOCKS5 connect request ack.");
- return CURLE_COULDNT_CONNECT;
- }
- if(socksreq[0] != 5) {
- failf(data,
- "SOCKS5 reply has wrong version, version should be 5.");
- return CURLE_COULDNT_CONNECT;
- }
-
-
- if(socksreq[3] == 3) {
-
- int addrlen = (int) socksreq[4];
- len = 5 + addrlen + 2;
- }
- else if(socksreq[3] == 4) {
-
- len = 4 + 16 + 2;
- }
-
- #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
- if(!conn->socks5_gssapi_enctype) {
-
- #endif
- if(len > 10) {
- result = Curl_blockread_all(conn, sock, (char *)&socksreq[10],
- len - 10, &actualread);
- if(result || ((len - 10) != actualread)) {
- failf(data, "Failed to receive SOCKS5 connect request ack.");
- return CURLE_COULDNT_CONNECT;
- }
- }
- #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
- }
- #endif
- if(socksreq[1] != 0) {
- if(socksreq[3] == 1) {
- 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],
- (((unsigned char)socksreq[8] << 8) |
- (unsigned char)socksreq[9]),
- (unsigned char)socksreq[1]);
- }
- else if(socksreq[3] == 3) {
- unsigned char port_upper = (unsigned char)socksreq[len - 2];
- socksreq[len - 2] = 0;
- failf(data,
- "Can't complete SOCKS5 connection to %s:%d. (%d)",
- (char *)&socksreq[5],
- ((port_upper << 8) |
- (unsigned char)socksreq[len - 1]),
- (unsigned char)socksreq[1]);
- socksreq[len - 2] = port_upper;
- }
- else if(socksreq[3] == 4) {
- failf(data,
- "Can't complete SOCKS5 connection to %02x%02x:%02x%02x:"
- "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%d. (%d)",
- (unsigned char)socksreq[4], (unsigned char)socksreq[5],
- (unsigned char)socksreq[6], (unsigned char)socksreq[7],
- (unsigned char)socksreq[8], (unsigned char)socksreq[9],
- (unsigned char)socksreq[10], (unsigned char)socksreq[11],
- (unsigned char)socksreq[12], (unsigned char)socksreq[13],
- (unsigned char)socksreq[14], (unsigned char)socksreq[15],
- (unsigned char)socksreq[16], (unsigned char)socksreq[17],
- (unsigned char)socksreq[18], (unsigned char)socksreq[19],
- (((unsigned char)socksreq[20] << 8) |
- (unsigned char)socksreq[21]),
- (unsigned char)socksreq[1]);
- }
- return CURLE_COULDNT_CONNECT;
- }
- else {
- infof(data, "SOCKS5 request granted.\n");
- }
- (void)curlx_nonblock(sock, TRUE);
- return CURLE_OK;
- }
- #endif
|