1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, 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)
27 #ifdef HAVE_SYS_SOCKET_H
28 #include <sys/socket.h>
30 #ifdef HAVE_NETINET_IN_H
31 #include <netinet/in.h>
33 #ifdef HAVE_ARPA_INET_H
34 #include <arpa/inet.h>
45 /* The last #include file should be: */
49 * Helper read-from-socket functions. Does the same as Curl_read() but it
50 * blocks until all bytes amount of buffersize will be read. No more, no less.
52 * This is STUPID BLOCKING behaviour which we frown upon, but right now this
55 int Curl_blockread_all(struct connectdata *conn, /* connection data */
56 curl_socket_t sockfd, /* read from this socket */
57 char *buf, /* store read data here */
58 ssize_t buffersize, /* max amount to read */
59 ssize_t *n) /* amount bytes read */
67 timeleft = Curl_timeleft(conn->data, NULL, TRUE);
69 /* we already got the timeout */
70 result = CURLE_OPERATION_TIMEDOUT;
73 if(Curl_socket_ready(sockfd, CURL_SOCKET_BAD, timeleft) <= 0) {
77 result = Curl_read_plain(sockfd, buf, buffersize, &nread);
78 if(CURLE_AGAIN == result)
83 if(buffersize == nread) {
102 * This function logs in to a SOCKS4 proxy and sends the specifics to the final
103 * destination server.
106 * http://socks.permeo.com/protocol/socks4.protocol
109 * Set protocol4a=true for "SOCKS 4A (Simple Extension to SOCKS 4 Protocol)"
110 * Nonsupport "Identification Protocol (RFC1413)"
112 CURLcode Curl_SOCKS4(const char *proxy_name,
113 const char *hostname,
116 struct connectdata *conn,
119 #define SOCKS4REQLEN 262
120 unsigned char socksreq[SOCKS4REQLEN]; /* room for SOCKS4 request incl. user
124 curl_socket_t sock = conn->sock[sockindex];
125 struct SessionHandle *data = conn->data;
127 if(Curl_timeleft(data, NULL, TRUE) < 0) {
128 /* time-out, bail out, go home */
129 failf(data, "Connection time-out");
130 return CURLE_OPERATION_TIMEDOUT;
133 curlx_nonblock(sock, FALSE);
136 * Compose socks4 request
140 * +----+----+----+----+----+----+----+----+----+----+....+----+
141 * | VN | CD | DSTPORT | DSTIP | USERID |NULL|
142 * +----+----+----+----+----+----+----+----+----+----+....+----+
143 * # of bytes: 1 1 2 4 variable 1
146 socksreq[0] = 4; /* version (SOCKS4) */
147 socksreq[1] = 1; /* connect */
148 socksreq[2] = (unsigned char)((remote_port >> 8) & 0xff); /* PORT MSB */
149 socksreq[3] = (unsigned char)(remote_port & 0xff); /* PORT LSB */
151 /* DNS resolve only for SOCKS4, not SOCKS4a */
153 struct Curl_dns_entry *dns;
154 Curl_addrinfo *hp=NULL;
157 rc = Curl_resolv(conn, hostname, remote_port, &dns);
159 if(rc == CURLRESOLV_ERROR)
160 return CURLE_COULDNT_RESOLVE_PROXY;
162 if(rc == CURLRESOLV_PENDING)
163 /* ignores the return code, but 'dns' remains NULL on failure */
164 (void)Curl_resolver_wait_resolv(conn, &dns);
167 * We cannot use 'hostent' as a struct that Curl_resolv() returns. It
168 * returns a Curl_addrinfo pointer that may not always look the same.
174 unsigned short ip[4];
175 Curl_printable_address(hp, buf, sizeof(buf));
177 if(4 == sscanf( buf, "%hu.%hu.%hu.%hu",
178 &ip[0], &ip[1], &ip[2], &ip[3])) {
180 socksreq[4] = (unsigned char)ip[0];
181 socksreq[5] = (unsigned char)ip[1];
182 socksreq[6] = (unsigned char)ip[2];
183 socksreq[7] = (unsigned char)ip[3];
186 hp = NULL; /* fail! */
188 Curl_resolv_unlock(data, dns); /* not used anymore from now on */
192 failf(data, "Failed to resolve \"%s\" for SOCKS4 connect.",
194 return CURLE_COULDNT_RESOLVE_HOST;
199 * This is currently not supporting "Identification Protocol (RFC1413)".
201 socksreq[8] = 0; /* ensure empty userid is NUL-terminated */
203 strlcat((char*)socksreq + 8, proxy_name, sizeof(socksreq) - 8);
211 ssize_t hostnamelen = 0;
213 (int)strlen((char*)socksreq + 8); /* size including NUL */
215 /* If SOCKS4a, set special invalid IP address 0.0.0.x */
221 /* If still enough room in buffer, also append hostname */
222 hostnamelen = (ssize_t)strlen(hostname) + 1; /* length including NUL */
223 if(packetsize + hostnamelen <= SOCKS4REQLEN)
224 strcpy((char*)socksreq + packetsize, hostname);
226 hostnamelen = 0; /* Flag: hostname did not fit in buffer */
230 code = Curl_write_plain(conn, sock, (char *)socksreq,
231 packetsize + hostnamelen,
233 if((code != CURLE_OK) || (written != packetsize + hostnamelen)) {
234 failf(data, "Failed to send SOCKS4 connect request.");
235 return CURLE_COULDNT_CONNECT;
237 if(protocol4a && hostnamelen == 0) {
238 /* SOCKS4a with very long hostname - send that name separately */
239 hostnamelen = (ssize_t)strlen(hostname) + 1;
240 code = Curl_write_plain(conn, sock, (char *)hostname, hostnamelen,
242 if((code != CURLE_OK) || (written != hostnamelen)) {
243 failf(data, "Failed to send SOCKS4 connect request.");
244 return CURLE_COULDNT_CONNECT;
248 packetsize = 8; /* receive data size */
250 /* Receive response */
251 result = Curl_blockread_all(conn, sock, (char *)socksreq, packetsize,
253 if((result != CURLE_OK) || (actualread != packetsize)) {
254 failf(data, "Failed to receive SOCKS4 connect request ack.");
255 return CURLE_COULDNT_CONNECT;
261 * +----+----+----+----+----+----+----+----+
262 * | VN | CD | DSTPORT | DSTIP |
263 * +----+----+----+----+----+----+----+----+
264 * # of bytes: 1 1 2 4
266 * VN is the version of the reply code and should be 0. CD is the result
267 * code with one of the following values:
269 * 90: request granted
270 * 91: request rejected or failed
271 * 92: request rejected because SOCKS server cannot connect to
272 * identd on the client
273 * 93: request rejected because the client program and identd
274 * report different user-ids
277 /* wrong version ? */
278 if(socksreq[0] != 0) {
280 "SOCKS4 reply has wrong version, version should be 4.");
281 return CURLE_COULDNT_CONNECT;
285 switch(socksreq[1]) {
287 infof(data, "SOCKS4%s request granted.\n", protocol4a?"a":"");
291 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
292 ", request rejected or failed.",
293 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
294 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
295 ((socksreq[8] << 8) | socksreq[9]),
297 return CURLE_COULDNT_CONNECT;
300 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
301 ", request rejected because SOCKS server cannot connect to "
302 "identd on the client.",
303 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
304 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
305 ((socksreq[8] << 8) | socksreq[9]),
307 return CURLE_COULDNT_CONNECT;
310 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
311 ", request rejected because the client program and identd "
312 "report different user-ids.",
313 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
314 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
315 ((socksreq[8] << 8) | socksreq[9]),
317 return CURLE_COULDNT_CONNECT;
320 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
322 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
323 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
324 ((socksreq[8] << 8) | socksreq[9]),
326 return CURLE_COULDNT_CONNECT;
330 curlx_nonblock(sock, TRUE);
332 return CURLE_OK; /* Proxy was successful! */
336 * This function logs in to a SOCKS5 proxy and sends the specifics to the final
337 * destination server.
339 CURLcode Curl_SOCKS5(const char *proxy_name,
340 const char *proxy_password,
341 const char *hostname,
344 struct connectdata *conn)
347 According to the RFC1928, section "6. Replies". This is what a SOCK5
350 +----+-----+-------+------+----------+----------+
351 |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
352 +----+-----+-------+------+----------+----------+
353 | 1 | 1 | X'00' | 1 | Variable | 2 |
354 +----+-----+-------+------+----------+----------+
358 o VER protocol version: X'05'
363 unsigned char socksreq[600]; /* room for large user/pw (255 max each) */
368 curl_socket_t sock = conn->sock[sockindex];
369 struct SessionHandle *data = conn->data;
371 bool socks5_resolve_local = (conn->proxytype == CURLPROXY_SOCKS5)?TRUE:FALSE;
372 const size_t hostname_len = strlen(hostname);
373 ssize_t packetsize = 0;
376 /* RFC1928 chapter 5 specifies max 255 chars for domain name in packet */
377 if(!socks5_resolve_local && hostname_len > 255) {
378 infof(conn->data,"SOCKS5: server resolving disabled for hostnames of "
379 "length > 255 [actual len=%zu]\n", hostname_len);
380 socks5_resolve_local = TRUE;
384 timeout = Curl_timeleft(data, NULL, TRUE);
387 /* time-out, bail out, go home */
388 failf(data, "Connection time-out");
389 return CURLE_OPERATION_TIMEDOUT;
392 curlx_nonblock(sock, TRUE);
394 /* wait until socket gets connected */
395 result = Curl_socket_ready(CURL_SOCKET_BAD, sock, timeout);
398 failf(conn->data, "SOCKS5: no connection here");
399 return CURLE_COULDNT_CONNECT;
401 else if(0 == result) {
402 failf(conn->data, "SOCKS5: connection timeout");
403 return CURLE_OPERATION_TIMEDOUT;
406 if(result & CURL_CSELECT_ERR) {
407 failf(conn->data, "SOCKS5: error occurred during connection");
408 return CURLE_COULDNT_CONNECT;
411 socksreq[0] = 5; /* version */
412 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
413 socksreq[1] = (char)(proxy_name ? 3 : 2); /* number of methods (below) */
414 socksreq[2] = 0; /* no authentication */
415 socksreq[3] = 1; /* gssapi */
416 socksreq[4] = 2; /* username/password */
418 socksreq[1] = (char)(proxy_name ? 2 : 1); /* number of methods (below) */
419 socksreq[2] = 0; /* no authentication */
420 socksreq[3] = 2; /* username/password */
423 curlx_nonblock(sock, FALSE);
425 code = Curl_write_plain(conn, sock, (char *)socksreq, (2 + (int)socksreq[1]),
427 if((code != CURLE_OK) || (written != (2 + (int)socksreq[1]))) {
428 failf(data, "Unable to send initial SOCKS5 request.");
429 return CURLE_COULDNT_CONNECT;
432 curlx_nonblock(sock, TRUE);
434 result = Curl_socket_ready(sock, CURL_SOCKET_BAD, timeout);
437 failf(conn->data, "SOCKS5 nothing to read");
438 return CURLE_COULDNT_CONNECT;
440 else if(0 == result) {
441 failf(conn->data, "SOCKS5 read timeout");
442 return CURLE_OPERATION_TIMEDOUT;
445 if(result & CURL_CSELECT_ERR) {
446 failf(conn->data, "SOCKS5 read error occurred");
447 return CURLE_RECV_ERROR;
450 curlx_nonblock(sock, FALSE);
452 result=Curl_blockread_all(conn, sock, (char *)socksreq, 2, &actualread);
453 if((result != CURLE_OK) || (actualread != 2)) {
454 failf(data, "Unable to receive initial SOCKS5 response.");
455 return CURLE_COULDNT_CONNECT;
458 if(socksreq[0] != 5) {
459 failf(data, "Received invalid version in initial SOCKS5 response.");
460 return CURLE_COULDNT_CONNECT;
462 if(socksreq[1] == 0) {
463 /* Nothing to do, no authentication needed */
466 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
467 else if(socksreq[1] == 1) {
468 code = Curl_SOCKS5_gssapi_negotiate(sockindex, conn);
469 if(code != CURLE_OK) {
470 failf(data, "Unable to negotiate SOCKS5 gssapi context.");
471 return CURLE_COULDNT_CONNECT;
475 else if(socksreq[1] == 2) {
476 /* Needs user name and password */
477 size_t userlen, pwlen;
478 if(proxy_name && proxy_password) {
479 userlen = strlen(proxy_name);
480 pwlen = strlen(proxy_password);
487 /* username/password request looks like
488 * +----+------+----------+------+----------+
489 * |VER | ULEN | UNAME | PLEN | PASSWD |
490 * +----+------+----------+------+----------+
491 * | 1 | 1 | 1 to 255 | 1 | 1 to 255 |
492 * +----+------+----------+------+----------+
495 socksreq[len++] = 1; /* username/pw subnegotiation version */
496 socksreq[len++] = (unsigned char) userlen;
497 if(proxy_name && userlen)
498 memcpy(socksreq + len, proxy_name, userlen);
500 socksreq[len++] = (unsigned char) pwlen;
501 if(proxy_password && pwlen)
502 memcpy(socksreq + len, proxy_password, pwlen);
505 code = Curl_write_plain(conn, sock, (char *)socksreq, len, &written);
506 if((code != CURLE_OK) || (len != written)) {
507 failf(data, "Failed to send SOCKS5 sub-negotiation request.");
508 return CURLE_COULDNT_CONNECT;
511 result=Curl_blockread_all(conn, sock, (char *)socksreq, 2, &actualread);
512 if((result != CURLE_OK) || (actualread != 2)) {
513 failf(data, "Unable to receive SOCKS5 sub-negotiation response.");
514 return CURLE_COULDNT_CONNECT;
517 /* ignore the first (VER) byte */
518 if(socksreq[1] != 0) { /* status */
519 failf(data, "User was rejected by the SOCKS5 server (%d %d).",
520 socksreq[0], socksreq[1]);
521 return CURLE_COULDNT_CONNECT;
524 /* Everything is good so far, user was authenticated! */
528 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
529 if(socksreq[1] == 255) {
531 if(socksreq[1] == 1) {
533 "SOCKS5 GSSAPI per-message authentication is not supported.");
534 return CURLE_COULDNT_CONNECT;
536 else if(socksreq[1] == 255) {
538 if(!proxy_name || !*proxy_name) {
540 "No authentication method was acceptable. (It is quite likely"
541 " that the SOCKS5 server wanted a username/password, since none"
542 " was supplied to the server on this connection.)");
545 failf(data, "No authentication method was acceptable.");
547 return CURLE_COULDNT_CONNECT;
551 "Undocumented SOCKS5 mode attempted to be used by server.");
552 return CURLE_COULDNT_CONNECT;
556 /* Authentication is complete, now specify destination to the proxy */
558 socksreq[len++] = 5; /* version (SOCKS5) */
559 socksreq[len++] = 1; /* connect */
560 socksreq[len++] = 0; /* must be zero */
562 if(!socks5_resolve_local) {
563 socksreq[len++] = 3; /* ATYP: domain name = 3 */
564 socksreq[len++] = (char) hostname_len; /* address length */
565 memcpy(&socksreq[len], hostname, hostname_len); /* address bytes w/o NULL */
569 struct Curl_dns_entry *dns;
570 Curl_addrinfo *hp = NULL;
571 int rc = Curl_resolv(conn, hostname, remote_port, &dns);
573 if(rc == CURLRESOLV_ERROR)
574 return CURLE_COULDNT_RESOLVE_HOST;
576 if(rc == CURLRESOLV_PENDING) {
577 /* this requires that we're in "wait for resolve" state */
578 code = Curl_resolver_wait_resolv(conn, &dns);
584 * We cannot use 'hostent' as a struct that Curl_resolv() returns. It
585 * returns a Curl_addrinfo pointer that may not always look the same.
590 struct sockaddr_in *saddr_in;
591 struct sockaddr_in6 *saddr_in6;
594 if(hp->ai_family == AF_INET) {
595 socksreq[len++] = 1; /* IPv4 = 1 */
597 saddr_in = (struct sockaddr_in*)hp->ai_addr;
598 for(i = 0; i < 4; i++) {
599 socksreq[len++] = ((unsigned char*)&saddr_in->sin_addr.s_addr)[i];
600 infof(data, "%d\n", socksreq[len-1]);
603 else if(hp->ai_family == AF_INET6) {
604 socksreq[len++] = 4; /* IPv6 = 4 */
606 saddr_in6 = (struct sockaddr_in6*)hp->ai_addr;
607 for(i = 0; i < 16; i++) {
608 socksreq[len++] = ((unsigned char*)&saddr_in6->sin6_addr.s6_addr)[i];
612 hp = NULL; /* fail! */
614 Curl_resolv_unlock(data, dns); /* not used anymore from now on */
617 failf(data, "Failed to resolve \"%s\" for SOCKS5 connect.",
619 return CURLE_COULDNT_RESOLVE_HOST;
623 socksreq[len++] = (unsigned char)((remote_port >> 8) & 0xff); /* PORT MSB */
624 socksreq[len++] = (unsigned char)(remote_port & 0xff); /* PORT LSB */
628 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
629 if(conn->socks5_gssapi_enctype) {
630 failf(data, "SOCKS5 gssapi protection not yet implemented.");
634 code = Curl_write_plain(conn, sock, (char *)socksreq, packetsize,
636 if((code != CURLE_OK) || (written != packetsize)) {
637 failf(data, "Failed to send SOCKS5 connect request.");
638 return CURLE_COULDNT_CONNECT;
641 packetsize = 10; /* minimum packet size is 10 */
643 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
644 if(conn->socks5_gssapi_enctype) {
645 failf(data, "SOCKS5 gssapi protection not yet implemented.");
649 result = Curl_blockread_all(conn, sock, (char *)socksreq, packetsize,
651 if((result != CURLE_OK) || (actualread != packetsize)) {
652 failf(data, "Failed to receive SOCKS5 connect request ack.");
653 return CURLE_COULDNT_CONNECT;
656 if(socksreq[0] != 5) { /* version */
658 "SOCKS5 reply has wrong version, version should be 5.");
659 return CURLE_COULDNT_CONNECT;
661 if(socksreq[1] != 0) { /* Anything besides 0 is an error */
662 if(socksreq[3] == 1) {
664 "Can't complete SOCKS5 connection to %d.%d.%d.%d:%d. (%d)",
665 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
666 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
667 ((socksreq[8] << 8) | socksreq[9]),
670 else if(socksreq[3] == 3) {
672 "Can't complete SOCKS5 connection to %s:%d. (%d)",
674 ((socksreq[8] << 8) | socksreq[9]),
677 else if(socksreq[3] == 4) {
679 "Can't complete SOCKS5 connection to %02x%02x:%02x%02x:"
680 "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%d. (%d)",
681 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
682 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
683 (unsigned char)socksreq[8], (unsigned char)socksreq[9],
684 (unsigned char)socksreq[10], (unsigned char)socksreq[11],
685 (unsigned char)socksreq[12], (unsigned char)socksreq[13],
686 (unsigned char)socksreq[14], (unsigned char)socksreq[15],
687 (unsigned char)socksreq[16], (unsigned char)socksreq[17],
688 (unsigned char)socksreq[18], (unsigned char)socksreq[19],
689 ((socksreq[8] << 8) | socksreq[9]),
692 return CURLE_COULDNT_CONNECT;
695 /* Fix: in general, returned BND.ADDR is variable length parameter by RFC
696 1928, so the reply packet should be read until the end to avoid errors at
697 subsequent protocol level.
699 +----+-----+-------+------+----------+----------+
700 |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
701 +----+-----+-------+------+----------+----------+
702 | 1 | 1 | X'00' | 1 | Variable | 2 |
703 +----+-----+-------+------+----------+----------+
706 o IP v4 address: X'01', BND.ADDR = 4 byte
707 o domain name: X'03', BND.ADDR = [ 1 byte length, string ]
708 o IP v6 address: X'04', BND.ADDR = 16 byte
711 /* Calculate real packet size */
712 if(socksreq[3] == 3) {
714 int addrlen = (int) socksreq[4];
715 packetsize = 5 + addrlen + 2;
717 else if(socksreq[3] == 4) {
719 packetsize = 4 + 16 + 2;
722 /* At this point we already read first 10 bytes */
723 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
724 if(!conn->socks5_gssapi_enctype) {
725 /* decrypt_gssapi_blockread already read the whole packet */
727 if(packetsize > 10) {
729 result = Curl_blockread_all(conn, sock, (char *)&socksreq[10],
730 packetsize, &actualread);
731 if((result != CURLE_OK) || (actualread != packetsize)) {
732 failf(data, "Failed to receive SOCKS5 connect request ack.");
733 return CURLE_COULDNT_CONNECT;
736 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
740 curlx_nonblock(sock, TRUE);
741 return CURLE_OK; /* Proxy was successful! */
744 #endif /* CURL_DISABLE_PROXY */