1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
25 #if !defined(CURL_DISABLE_PROXY) || defined(USE_WINDOWS_SSPI)
31 #ifdef HAVE_SYS_SOCKET_H
32 #include <sys/socket.h>
34 #ifdef HAVE_NETINET_IN_H
35 #include <netinet/in.h>
37 #ifdef HAVE_ARPA_INET_H
38 #include <arpa/inet.h>
49 /* The last #include file should be: */
53 * Helper read-from-socket functions. Does the same as Curl_read() but it
54 * blocks until all bytes amount of buffersize will be read. No more, no less.
56 * This is STUPID BLOCKING behaviour which we frown upon, but right now this
59 int Curl_blockread_all(struct connectdata *conn, /* connection data */
60 curl_socket_t sockfd, /* read from this socket */
61 char *buf, /* store read data here */
62 ssize_t buffersize, /* max amount to read */
63 ssize_t *n, /* amount bytes read */
64 long conn_timeout) /* timeout for data wait
76 /* calculating how long connection is establishing */
77 conntime = Curl_tvdiff(tvnow, conn->created);
78 if(conntime > conn_timeout) {
79 /* we already got the timeout */
83 if(Curl_socket_ready(sockfd, CURL_SOCKET_BAD,
84 (int)(conn_timeout - conntime)) <= 0) {
88 result = Curl_read_plain(sockfd, buf, buffersize, &nread);
92 if(buffersize == nread) {
111 * This function logs in to a SOCKS4 proxy and sends the specifics to the final
112 * destination server.
115 * http://socks.permeo.com/protocol/socks4.protocol
118 * Set protocol4a=true for "SOCKS 4A (Simple Extension to SOCKS 4 Protocol)"
119 * Nonsupport "Identification Protocol (RFC1413)"
121 CURLcode Curl_SOCKS4(const char *proxy_name,
122 const char *hostname,
125 struct connectdata *conn,
128 #define SOCKS4REQLEN 262
129 unsigned char socksreq[SOCKS4REQLEN]; /* room for SOCKS4 request incl. user
133 curl_socket_t sock = conn->sock[sockindex];
135 struct SessionHandle *data = conn->data;
138 timeout = Curl_timeleft(conn, NULL, TRUE);
141 /* time-out, bail out, go home */
142 failf(data, "Connection time-out");
143 return CURLE_OPERATION_TIMEDOUT;
146 curlx_nonblock(sock, FALSE);
149 * Compose socks4 request
153 * +----+----+----+----+----+----+----+----+----+----+....+----+
154 * | VN | CD | DSTPORT | DSTIP | USERID |NULL|
155 * +----+----+----+----+----+----+----+----+----+----+....+----+
156 * # of bytes: 1 1 2 4 variable 1
159 socksreq[0] = 4; /* version (SOCKS4) */
160 socksreq[1] = 1; /* connect */
161 *((unsigned short*)&socksreq[2]) = htons((unsigned short)remote_port);
163 /* DNS resolve only for SOCKS4, not SOCKS4a */
165 struct Curl_dns_entry *dns;
166 Curl_addrinfo *hp=NULL;
169 rc = Curl_resolv(conn, hostname, remote_port, &dns);
171 if(rc == CURLRESOLV_ERROR)
172 return CURLE_COULDNT_RESOLVE_PROXY;
174 if(rc == CURLRESOLV_PENDING)
175 /* ignores the return code, but 'dns' remains NULL on failure */
176 (void)Curl_wait_for_resolv(conn, &dns);
179 * We cannot use 'hostent' as a struct that Curl_resolv() returns. It
180 * returns a Curl_addrinfo pointer that may not always look the same.
186 unsigned short ip[4];
187 Curl_printable_address(hp, buf, sizeof(buf));
189 if(4 == sscanf( buf, "%hu.%hu.%hu.%hu",
190 &ip[0], &ip[1], &ip[2], &ip[3])) {
192 socksreq[4] = (unsigned char)ip[0];
193 socksreq[5] = (unsigned char)ip[1];
194 socksreq[6] = (unsigned char)ip[2];
195 socksreq[7] = (unsigned char)ip[3];
198 hp = NULL; /* fail! */
200 Curl_resolv_unlock(data, dns); /* not used anymore from now on */
204 failf(data, "Failed to resolve \"%s\" for SOCKS4 connect.",
206 return CURLE_COULDNT_RESOLVE_HOST;
211 * This is currently not supporting "Identification Protocol (RFC1413)".
213 socksreq[8] = 0; /* ensure empty userid is NUL-terminated */
215 strlcat((char*)socksreq + 8, proxy_name, sizeof(socksreq) - 8);
223 ssize_t hostnamelen = 0;
225 (int)strlen((char*)socksreq + 8); /* size including NUL */
227 /* If SOCKS4a, set special invalid IP address 0.0.0.x */
233 /* If still enough room in buffer, also append hostname */
234 hostnamelen = (ssize_t)strlen(hostname) + 1; /* length including NUL */
235 if (packetsize + hostnamelen <= SOCKS4REQLEN)
236 strcpy((char*)socksreq + packetsize, hostname);
238 hostnamelen = 0; /* Flag: hostname did not fit in buffer */
242 code = Curl_write_plain(conn, sock, (char *)socksreq,
243 packetsize + hostnamelen,
245 if((code != CURLE_OK) || (written != packetsize + hostnamelen)) {
246 failf(data, "Failed to send SOCKS4 connect request.");
247 return CURLE_COULDNT_CONNECT;
249 if (protocol4a && hostnamelen == 0) {
250 /* SOCKS4a with very long hostname - send that name separately */
251 hostnamelen = (ssize_t)strlen(hostname) + 1;
252 code = Curl_write_plain(conn, sock, (char *)hostname, hostnamelen,
254 if((code != CURLE_OK) || (written != hostnamelen)) {
255 failf(data, "Failed to send SOCKS4 connect request.");
256 return CURLE_COULDNT_CONNECT;
260 packetsize = 8; /* receive data size */
262 /* Receive response */
263 result = Curl_blockread_all(conn, sock, (char *)socksreq, packetsize,
264 &actualread, timeout);
265 if((result != CURLE_OK) || (actualread != packetsize)) {
266 failf(data, "Failed to receive SOCKS4 connect request ack.");
267 return CURLE_COULDNT_CONNECT;
273 * +----+----+----+----+----+----+----+----+
274 * | VN | CD | DSTPORT | DSTIP |
275 * +----+----+----+----+----+----+----+----+
276 * # of bytes: 1 1 2 4
278 * VN is the version of the reply code and should be 0. CD is the result
279 * code with one of the following values:
281 * 90: request granted
282 * 91: request rejected or failed
283 * 92: request rejected because SOCKS server cannot connect to
284 * identd on the client
285 * 93: request rejected because the client program and identd
286 * report different user-ids
289 /* wrong version ? */
290 if(socksreq[0] != 0) {
292 "SOCKS4 reply has wrong version, version should be 4.");
293 return CURLE_COULDNT_CONNECT;
301 infof(data, "SOCKS4a request granted.\n");
303 infof(data, "SOCKS4 request granted.\n");
307 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
308 ", request rejected or failed.",
309 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
310 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
311 (unsigned int)ntohs(*(unsigned short*)(&socksreq[8])),
313 return CURLE_COULDNT_CONNECT;
316 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
317 ", request rejected because SOCKS server cannot connect to "
318 "identd on the client.",
319 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
320 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
321 (unsigned int)ntohs(*(unsigned short*)(&socksreq[8])),
323 return CURLE_COULDNT_CONNECT;
326 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
327 ", request rejected because the client program and identd "
328 "report different user-ids.",
329 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
330 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
331 (unsigned int)ntohs(*(unsigned short*)(&socksreq[8])),
333 return CURLE_COULDNT_CONNECT;
336 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
338 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
339 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
340 (unsigned int)ntohs(*(unsigned short*)(&socksreq[8])),
342 return CURLE_COULDNT_CONNECT;
346 curlx_nonblock(sock, TRUE);
348 return CURLE_OK; /* Proxy was successful! */
352 * This function logs in to a SOCKS5 proxy and sends the specifics to the final
353 * destination server.
355 CURLcode Curl_SOCKS5(const char *proxy_name,
356 const char *proxy_password,
357 const char *hostname,
360 struct connectdata *conn)
363 According to the RFC1928, section "6. Replies". This is what a SOCK5
366 +----+-----+-------+------+----------+----------+
367 |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
368 +----+-----+-------+------+----------+----------+
369 | 1 | 1 | X'00' | 1 | Variable | 2 |
370 +----+-----+-------+------+----------+----------+
374 o VER protocol version: X'05'
379 unsigned char socksreq[600]; /* room for large user/pw (255 max each) */
384 curl_socket_t sock = conn->sock[sockindex];
385 struct SessionHandle *data = conn->data;
387 bool socks5_resolve_local = (bool)(data->set.proxytype == CURLPROXY_SOCKS5);
388 const size_t hostname_len = strlen(hostname);
389 ssize_t packetsize = 0;
391 /* RFC1928 chapter 5 specifies max 255 chars for domain name in packet */
392 if(!socks5_resolve_local && hostname_len > 255)
394 infof(conn->data,"SOCKS5: server resolving disabled for hostnames of "
395 "length > 255 [actual len=%zu]\n", hostname_len);
396 socks5_resolve_local = TRUE;
400 timeout = Curl_timeleft(conn, NULL, TRUE);
403 /* time-out, bail out, go home */
404 failf(data, "Connection time-out");
405 return CURLE_OPERATION_TIMEDOUT;
408 curlx_nonblock(sock, TRUE);
410 /* wait until socket gets connected */
411 result = Curl_socket_ready(CURL_SOCKET_BAD, sock, (int)timeout);
414 failf(conn->data, "SOCKS5: no connection here");
415 return CURLE_COULDNT_CONNECT;
417 else if(0 == result) {
418 failf(conn->data, "SOCKS5: connection timeout");
419 return CURLE_OPERATION_TIMEDOUT;
422 if(result & CURL_CSELECT_ERR) {
423 failf(conn->data, "SOCKS5: error occured during connection");
424 return CURLE_COULDNT_CONNECT;
427 socksreq[0] = 5; /* version */
428 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
429 socksreq[1] = (char)(proxy_name ? 3 : 2); /* number of methods (below) */
430 socksreq[2] = 0; /* no authentication */
431 socksreq[3] = 1; /* gssapi */
432 socksreq[4] = 2; /* username/password */
434 socksreq[1] = (char)(proxy_name ? 2 : 1); /* number of methods (below) */
435 socksreq[2] = 0; /* no authentication */
436 socksreq[3] = 2; /* username/password */
439 curlx_nonblock(sock, FALSE);
441 code = Curl_write_plain(conn, sock, (char *)socksreq, (2 + (int)socksreq[1]),
443 if((code != CURLE_OK) || (written != (2 + (int)socksreq[1]))) {
444 failf(data, "Unable to send initial SOCKS5 request.");
445 return CURLE_COULDNT_CONNECT;
448 curlx_nonblock(sock, TRUE);
450 result = Curl_socket_ready(sock, CURL_SOCKET_BAD, (int)timeout);
453 failf(conn->data, "SOCKS5 nothing to read");
454 return CURLE_COULDNT_CONNECT;
456 else if(0 == result) {
457 failf(conn->data, "SOCKS5 read timeout");
458 return CURLE_OPERATION_TIMEDOUT;
461 if(result & CURL_CSELECT_ERR) {
462 failf(conn->data, "SOCKS5 read error occured");
463 return CURLE_RECV_ERROR;
466 curlx_nonblock(sock, FALSE);
468 result=Curl_blockread_all(conn, sock, (char *)socksreq, 2, &actualread,
470 if((result != CURLE_OK) || (actualread != 2)) {
471 failf(data, "Unable to receive initial SOCKS5 response.");
472 return CURLE_COULDNT_CONNECT;
475 if(socksreq[0] != 5) {
476 failf(data, "Received invalid version in initial SOCKS5 response.");
477 return CURLE_COULDNT_CONNECT;
479 if(socksreq[1] == 0) {
480 /* Nothing to do, no authentication needed */
483 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
484 else if(socksreq[1] == 1) {
485 code = Curl_SOCKS5_gssapi_negotiate(sockindex, conn);
486 if(code != CURLE_OK) {
487 failf(data, "Unable to negotiate SOCKS5 gssapi context.");
488 return CURLE_COULDNT_CONNECT;
492 else if(socksreq[1] == 2) {
493 /* Needs user name and password */
494 size_t userlen, pwlen;
496 if(proxy_name && proxy_password) {
497 userlen = strlen(proxy_name);
498 pwlen = strlen(proxy_password);
505 /* username/password request looks like
506 * +----+------+----------+------+----------+
507 * |VER | ULEN | UNAME | PLEN | PASSWD |
508 * +----+------+----------+------+----------+
509 * | 1 | 1 | 1 to 255 | 1 | 1 to 255 |
510 * +----+------+----------+------+----------+
513 socksreq[len++] = 1; /* username/pw subnegotiation version */
514 socksreq[len++] = (char) userlen;
515 memcpy(socksreq + len, proxy_name, userlen);
517 socksreq[len++] = (char) pwlen;
518 memcpy(socksreq + len, proxy_password, pwlen);
521 code = Curl_write_plain(conn, sock, (char *)socksreq, len, &written);
522 if((code != CURLE_OK) || (len != written)) {
523 failf(data, "Failed to send SOCKS5 sub-negotiation request.");
524 return CURLE_COULDNT_CONNECT;
527 result=Curl_blockread_all(conn, sock, (char *)socksreq, 2, &actualread,
529 if((result != CURLE_OK) || (actualread != 2)) {
530 failf(data, "Unable to receive SOCKS5 sub-negotiation response.");
531 return CURLE_COULDNT_CONNECT;
534 /* ignore the first (VER) byte */
535 if(socksreq[1] != 0) { /* status */
536 failf(data, "User was rejected by the SOCKS5 server (%d %d).",
537 socksreq[0], socksreq[1]);
538 return CURLE_COULDNT_CONNECT;
541 /* Everything is good so far, user was authenticated! */
545 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
546 if(socksreq[1] == 255) {
548 if(socksreq[1] == 1) {
550 "SOCKS5 GSSAPI per-message authentication is not supported.");
551 return CURLE_COULDNT_CONNECT;
553 else if(socksreq[1] == 255) {
555 if(!proxy_name || !*proxy_name) {
557 "No authentication method was acceptable. (It is quite likely"
558 " that the SOCKS5 server wanted a username/password, since none"
559 " was supplied to the server on this connection.)");
562 failf(data, "No authentication method was acceptable.");
564 return CURLE_COULDNT_CONNECT;
568 "Undocumented SOCKS5 mode attempted to be used by server.");
569 return CURLE_COULDNT_CONNECT;
573 /* Authentication is complete, now specify destination to the proxy */
574 socksreq[0] = 5; /* version (SOCKS5) */
575 socksreq[1] = 1; /* connect */
576 socksreq[2] = 0; /* must be zero */
578 if(!socks5_resolve_local) {
579 packetsize = (ssize_t)(5 + hostname_len + 2);
581 socksreq[3] = 3; /* ATYP: domain name = 3 */
582 socksreq[4] = (char) hostname_len; /* address length */
583 memcpy(&socksreq[5], hostname, hostname_len); /* address bytes w/o NULL */
585 *((unsigned short*)&socksreq[hostname_len+5]) =
586 htons((unsigned short)remote_port);
589 struct Curl_dns_entry *dns;
590 Curl_addrinfo *hp=NULL;
591 int rc = Curl_resolv(conn, hostname, remote_port, &dns);
595 socksreq[3] = 1; /* IPv4 = 1 */
597 if(rc == CURLRESOLV_ERROR)
598 return CURLE_COULDNT_RESOLVE_HOST;
600 if(rc == CURLRESOLV_PENDING) {
601 /* this requires that we're in "wait for resolve" state */
602 rc = Curl_wait_for_resolv(conn, &dns);
608 * We cannot use 'hostent' as a struct that Curl_resolv() returns. It
609 * returns a Curl_addrinfo pointer that may not always look the same.
615 unsigned short ip[4];
616 Curl_printable_address(hp, buf, sizeof(buf));
618 if(4 == sscanf( buf, "%hu.%hu.%hu.%hu",
619 &ip[0], &ip[1], &ip[2], &ip[3])) {
620 socksreq[4] = (unsigned char)ip[0];
621 socksreq[5] = (unsigned char)ip[1];
622 socksreq[6] = (unsigned char)ip[2];
623 socksreq[7] = (unsigned char)ip[3];
626 hp = NULL; /* fail! */
628 Curl_resolv_unlock(data, dns); /* not used anymore from now on */
631 failf(data, "Failed to resolve \"%s\" for SOCKS5 connect.",
633 return CURLE_COULDNT_RESOLVE_HOST;
636 *((unsigned short*)&socksreq[8]) = htons((unsigned short)remote_port);
639 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
640 if(conn->socks5_gssapi_enctype) {
641 failf(data, "SOCKS5 gssapi protection not yet implemented.");
644 code = Curl_write_plain(conn, sock, (char *)socksreq, packetsize, &written);
645 if((code != CURLE_OK) || (written != packetsize)) {
646 failf(data, "Failed to send SOCKS5 connect request.");
647 return CURLE_COULDNT_CONNECT;
650 packetsize = 10; /* minimum packet size is 10 */
652 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
653 if(conn->socks5_gssapi_enctype) {
654 failf(data, "SOCKS5 gssapi protection not yet implemented.");
657 result = Curl_blockread_all(conn, sock, (char *)socksreq, packetsize,
658 &actualread, timeout);
659 if((result != CURLE_OK) || (actualread != packetsize)) {
660 failf(data, "Failed to receive SOCKS5 connect request ack.");
661 return CURLE_COULDNT_CONNECT;
664 if(socksreq[0] != 5) { /* version */
666 "SOCKS5 reply has wrong version, version should be 5.");
667 return CURLE_COULDNT_CONNECT;
669 if(socksreq[1] != 0) { /* Anything besides 0 is an error */
671 "Can't complete SOCKS5 connection to %d.%d.%d.%d:%d. (%d)",
672 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
673 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
674 (unsigned int)ntohs(*(unsigned short*)(&socksreq[8])),
676 return CURLE_COULDNT_CONNECT;
679 /* Fix: in general, returned BND.ADDR is variable length parameter by RFC
680 1928, so the reply packet should be read until the end to avoid errors at
681 subsequent protocol level.
683 +----+-----+-------+------+----------+----------+
684 |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
685 +----+-----+-------+------+----------+----------+
686 | 1 | 1 | X'00' | 1 | Variable | 2 |
687 +----+-----+-------+------+----------+----------+
690 o IP v4 address: X'01', BND.ADDR = 4 byte
691 o domain name: X'03', BND.ADDR = [ 1 byte length, string ]
692 o IP v6 address: X'04', BND.ADDR = 16 byte
695 /* Calculate real packet size */
696 if(socksreq[3] == 3) {
698 int addrlen = (int) socksreq[4];
699 packetsize = 5 + addrlen + 2;
701 else if(socksreq[3] == 4) {
703 packetsize = 4 + 16 + 2;
706 /* At this point we already read first 10 bytes */
707 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
708 if(!conn->socks5_gssapi_enctype) {
709 /* decrypt_gssapi_blockread already read the whole packet */
711 if(packetsize > 10) {
713 result = Curl_blockread_all(conn, sock, (char *)&socksreq[10],
714 packetsize, &actualread, timeout);
715 if((result != CURLE_OK) || (actualread != packetsize)) {
716 failf(data, "Failed to receive SOCKS5 connect request ack.");
717 return CURLE_COULDNT_CONNECT;
720 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
724 curlx_nonblock(sock, TRUE);
725 return CURLE_OK; /* Proxy was successful! */
728 #endif /* CURL_DISABLE_PROXY */