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) || defined(USE_WINDOWS_SSPI)
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]) {
288 infof(data, "SOCKS4a request granted.\n");
290 infof(data, "SOCKS4 request granted.\n");
294 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
295 ", request rejected or failed.",
296 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
297 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
298 ((socksreq[8] << 8) | socksreq[9]),
300 return CURLE_COULDNT_CONNECT;
303 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
304 ", request rejected because SOCKS server cannot connect to "
305 "identd on the client.",
306 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
307 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
308 ((socksreq[8] << 8) | socksreq[9]),
310 return CURLE_COULDNT_CONNECT;
313 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
314 ", request rejected because the client program and identd "
315 "report different user-ids.",
316 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
317 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
318 ((socksreq[8] << 8) | socksreq[9]),
320 return CURLE_COULDNT_CONNECT;
323 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
325 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
326 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
327 ((socksreq[8] << 8) | socksreq[9]),
329 return CURLE_COULDNT_CONNECT;
333 curlx_nonblock(sock, TRUE);
335 return CURLE_OK; /* Proxy was successful! */
339 * This function logs in to a SOCKS5 proxy and sends the specifics to the final
340 * destination server.
342 CURLcode Curl_SOCKS5(const char *proxy_name,
343 const char *proxy_password,
344 const char *hostname,
347 struct connectdata *conn)
350 According to the RFC1928, section "6. Replies". This is what a SOCK5
353 +----+-----+-------+------+----------+----------+
354 |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
355 +----+-----+-------+------+----------+----------+
356 | 1 | 1 | X'00' | 1 | Variable | 2 |
357 +----+-----+-------+------+----------+----------+
361 o VER protocol version: X'05'
366 unsigned char socksreq[600]; /* room for large user/pw (255 max each) */
371 curl_socket_t sock = conn->sock[sockindex];
372 struct SessionHandle *data = conn->data;
374 bool socks5_resolve_local = (bool)(conn->proxytype == CURLPROXY_SOCKS5);
375 const size_t hostname_len = strlen(hostname);
376 ssize_t packetsize = 0;
378 /* RFC1928 chapter 5 specifies max 255 chars for domain name in packet */
379 if(!socks5_resolve_local && hostname_len > 255) {
380 infof(conn->data,"SOCKS5: server resolving disabled for hostnames of "
381 "length > 255 [actual len=%zu]\n", hostname_len);
382 socks5_resolve_local = TRUE;
386 timeout = Curl_timeleft(data, NULL, TRUE);
389 /* time-out, bail out, go home */
390 failf(data, "Connection time-out");
391 return CURLE_OPERATION_TIMEDOUT;
394 curlx_nonblock(sock, TRUE);
396 /* wait until socket gets connected */
397 result = Curl_socket_ready(CURL_SOCKET_BAD, sock, timeout);
400 failf(conn->data, "SOCKS5: no connection here");
401 return CURLE_COULDNT_CONNECT;
403 else if(0 == result) {
404 failf(conn->data, "SOCKS5: connection timeout");
405 return CURLE_OPERATION_TIMEDOUT;
408 if(result & CURL_CSELECT_ERR) {
409 failf(conn->data, "SOCKS5: error occurred during connection");
410 return CURLE_COULDNT_CONNECT;
413 socksreq[0] = 5; /* version */
414 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
415 socksreq[1] = (char)(proxy_name ? 3 : 2); /* number of methods (below) */
416 socksreq[2] = 0; /* no authentication */
417 socksreq[3] = 1; /* gssapi */
418 socksreq[4] = 2; /* username/password */
420 socksreq[1] = (char)(proxy_name ? 2 : 1); /* number of methods (below) */
421 socksreq[2] = 0; /* no authentication */
422 socksreq[3] = 2; /* username/password */
425 curlx_nonblock(sock, FALSE);
427 code = Curl_write_plain(conn, sock, (char *)socksreq, (2 + (int)socksreq[1]),
429 if((code != CURLE_OK) || (written != (2 + (int)socksreq[1]))) {
430 failf(data, "Unable to send initial SOCKS5 request.");
431 return CURLE_COULDNT_CONNECT;
434 curlx_nonblock(sock, TRUE);
436 result = Curl_socket_ready(sock, CURL_SOCKET_BAD, timeout);
439 failf(conn->data, "SOCKS5 nothing to read");
440 return CURLE_COULDNT_CONNECT;
442 else if(0 == result) {
443 failf(conn->data, "SOCKS5 read timeout");
444 return CURLE_OPERATION_TIMEDOUT;
447 if(result & CURL_CSELECT_ERR) {
448 failf(conn->data, "SOCKS5 read error occurred");
449 return CURLE_RECV_ERROR;
452 curlx_nonblock(sock, FALSE);
454 result=Curl_blockread_all(conn, sock, (char *)socksreq, 2, &actualread);
455 if((result != CURLE_OK) || (actualread != 2)) {
456 failf(data, "Unable to receive initial SOCKS5 response.");
457 return CURLE_COULDNT_CONNECT;
460 if(socksreq[0] != 5) {
461 failf(data, "Received invalid version in initial SOCKS5 response.");
462 return CURLE_COULDNT_CONNECT;
464 if(socksreq[1] == 0) {
465 /* Nothing to do, no authentication needed */
468 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
469 else if(socksreq[1] == 1) {
470 code = Curl_SOCKS5_gssapi_negotiate(sockindex, conn);
471 if(code != CURLE_OK) {
472 failf(data, "Unable to negotiate SOCKS5 gssapi context.");
473 return CURLE_COULDNT_CONNECT;
477 else if(socksreq[1] == 2) {
478 /* Needs user name and password */
479 size_t userlen, pwlen;
481 if(proxy_name && proxy_password) {
482 userlen = strlen(proxy_name);
483 pwlen = strlen(proxy_password);
490 /* username/password request looks like
491 * +----+------+----------+------+----------+
492 * |VER | ULEN | UNAME | PLEN | PASSWD |
493 * +----+------+----------+------+----------+
494 * | 1 | 1 | 1 to 255 | 1 | 1 to 255 |
495 * +----+------+----------+------+----------+
498 socksreq[len++] = 1; /* username/pw subnegotiation version */
499 socksreq[len++] = (unsigned char) userlen;
500 if(proxy_name && userlen)
501 memcpy(socksreq + len, proxy_name, userlen);
503 socksreq[len++] = (unsigned char) pwlen;
504 if(proxy_password && pwlen)
505 memcpy(socksreq + len, proxy_password, pwlen);
508 code = Curl_write_plain(conn, sock, (char *)socksreq, len, &written);
509 if((code != CURLE_OK) || (len != written)) {
510 failf(data, "Failed to send SOCKS5 sub-negotiation request.");
511 return CURLE_COULDNT_CONNECT;
514 result=Curl_blockread_all(conn, sock, (char *)socksreq, 2, &actualread);
515 if((result != CURLE_OK) || (actualread != 2)) {
516 failf(data, "Unable to receive SOCKS5 sub-negotiation response.");
517 return CURLE_COULDNT_CONNECT;
520 /* ignore the first (VER) byte */
521 if(socksreq[1] != 0) { /* status */
522 failf(data, "User was rejected by the SOCKS5 server (%d %d).",
523 socksreq[0], socksreq[1]);
524 return CURLE_COULDNT_CONNECT;
527 /* Everything is good so far, user was authenticated! */
531 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
532 if(socksreq[1] == 255) {
534 if(socksreq[1] == 1) {
536 "SOCKS5 GSSAPI per-message authentication is not supported.");
537 return CURLE_COULDNT_CONNECT;
539 else if(socksreq[1] == 255) {
541 if(!proxy_name || !*proxy_name) {
543 "No authentication method was acceptable. (It is quite likely"
544 " that the SOCKS5 server wanted a username/password, since none"
545 " was supplied to the server on this connection.)");
548 failf(data, "No authentication method was acceptable.");
550 return CURLE_COULDNT_CONNECT;
554 "Undocumented SOCKS5 mode attempted to be used by server.");
555 return CURLE_COULDNT_CONNECT;
559 /* Authentication is complete, now specify destination to the proxy */
560 socksreq[0] = 5; /* version (SOCKS5) */
561 socksreq[1] = 1; /* connect */
562 socksreq[2] = 0; /* must be zero */
564 if(!socks5_resolve_local) {
565 packetsize = (ssize_t)(5 + hostname_len + 2);
567 socksreq[3] = 3; /* ATYP: domain name = 3 */
568 socksreq[4] = (char) hostname_len; /* address length */
569 memcpy(&socksreq[5], hostname, hostname_len); /* address bytes w/o NULL */
572 socksreq[hostname_len+5] = (unsigned char)((remote_port >> 8) & 0xff);
574 socksreq[hostname_len+6] = (unsigned char)(remote_port & 0xff);
577 struct Curl_dns_entry *dns;
578 Curl_addrinfo *hp=NULL;
579 int rc = Curl_resolv(conn, hostname, remote_port, &dns);
583 socksreq[3] = 1; /* IPv4 = 1 */
585 if(rc == CURLRESOLV_ERROR)
586 return CURLE_COULDNT_RESOLVE_HOST;
588 if(rc == CURLRESOLV_PENDING) {
589 /* this requires that we're in "wait for resolve" state */
590 code = Curl_resolver_wait_resolv(conn, &dns);
596 * We cannot use 'hostent' as a struct that Curl_resolv() returns. It
597 * returns a Curl_addrinfo pointer that may not always look the same.
603 unsigned short ip[4];
604 Curl_printable_address(hp, buf, sizeof(buf));
606 if(4 == sscanf( buf, "%hu.%hu.%hu.%hu",
607 &ip[0], &ip[1], &ip[2], &ip[3])) {
608 socksreq[4] = (unsigned char)ip[0];
609 socksreq[5] = (unsigned char)ip[1];
610 socksreq[6] = (unsigned char)ip[2];
611 socksreq[7] = (unsigned char)ip[3];
614 hp = NULL; /* fail! */
616 Curl_resolv_unlock(data, dns); /* not used anymore from now on */
619 failf(data, "Failed to resolve \"%s\" for SOCKS5 connect.",
621 return CURLE_COULDNT_RESOLVE_HOST;
624 socksreq[8] = (unsigned char)((remote_port >> 8) & 0xff); /* PORT MSB */
625 socksreq[9] = (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 */
663 "Can't complete SOCKS5 connection to %d.%d.%d.%d:%d. (%d)",
664 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
665 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
666 ((socksreq[8] << 8) | socksreq[9]),
668 return CURLE_COULDNT_CONNECT;
671 /* Fix: in general, returned BND.ADDR is variable length parameter by RFC
672 1928, so the reply packet should be read until the end to avoid errors at
673 subsequent protocol level.
675 +----+-----+-------+------+----------+----------+
676 |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
677 +----+-----+-------+------+----------+----------+
678 | 1 | 1 | X'00' | 1 | Variable | 2 |
679 +----+-----+-------+------+----------+----------+
682 o IP v4 address: X'01', BND.ADDR = 4 byte
683 o domain name: X'03', BND.ADDR = [ 1 byte length, string ]
684 o IP v6 address: X'04', BND.ADDR = 16 byte
687 /* Calculate real packet size */
688 if(socksreq[3] == 3) {
690 int addrlen = (int) socksreq[4];
691 packetsize = 5 + addrlen + 2;
693 else if(socksreq[3] == 4) {
695 packetsize = 4 + 16 + 2;
698 /* At this point we already read first 10 bytes */
699 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
700 if(!conn->socks5_gssapi_enctype) {
701 /* decrypt_gssapi_blockread already read the whole packet */
703 if(packetsize > 10) {
705 result = Curl_blockread_all(conn, sock, (char *)&socksreq[10],
706 packetsize, &actualread);
707 if((result != CURLE_OK) || (actualread != packetsize)) {
708 failf(data, "Failed to receive SOCKS5 connect request ack.");
709 return CURLE_COULDNT_CONNECT;
712 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
716 curlx_nonblock(sock, TRUE);
717 return CURLE_OK; /* Proxy was successful! */
720 #endif /* CURL_DISABLE_PROXY */