1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2009, 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.
22 ***************************************************************************/
26 #if !defined(CURL_DISABLE_PROXY) || defined(USE_WINDOWS_SSPI)
35 #ifdef HAVE_SYS_SOCKET_H
36 #include <sys/socket.h>
38 #ifdef HAVE_NETINET_IN_H
39 #include <netinet/in.h>
41 #ifdef HAVE_ARPA_INET_H
42 #include <arpa/inet.h>
53 /* The last #include file should be: */
57 * Helper read-from-socket functions. Does the same as Curl_read() but it
58 * blocks until all bytes amount of buffersize will be read. No more, no less.
60 * This is STUPID BLOCKING behaviour which we frown upon, but right now this
63 int Curl_blockread_all(struct connectdata *conn, /* connection data */
64 curl_socket_t sockfd, /* read from this socket */
65 char *buf, /* store read data here */
66 ssize_t buffersize, /* max amount to read */
67 ssize_t *n, /* amount bytes read */
68 long conn_timeout) /* timeout for data wait
80 /* calculating how long connection is establishing */
81 conntime = Curl_tvdiff(tvnow, conn->created);
82 if(conntime > conn_timeout) {
83 /* we already got the timeout */
87 if(Curl_socket_ready(sockfd, CURL_SOCKET_BAD,
88 (int)(conn_timeout - conntime)) <= 0) {
92 result = Curl_read_plain(sockfd, buf, buffersize, &nread);
96 if(buffersize == nread) {
115 * This function logs in to a SOCKS4 proxy and sends the specifics to the final
116 * destination server.
119 * http://socks.permeo.com/protocol/socks4.protocol
122 * Set protocol4a=true for "SOCKS 4A (Simple Extension to SOCKS 4 Protocol)"
123 * Nonsupport "Identification Protocol (RFC1413)"
125 CURLcode Curl_SOCKS4(const char *proxy_name,
126 const char *hostname,
129 struct connectdata *conn,
132 #define SOCKS4REQLEN 262
133 unsigned char socksreq[SOCKS4REQLEN]; /* room for SOCKS4 request incl. user
137 curl_socket_t sock = conn->sock[sockindex];
139 struct SessionHandle *data = conn->data;
142 timeout = Curl_timeleft(conn, NULL, TRUE);
145 /* time-out, bail out, go home */
146 failf(data, "Connection time-out");
147 return CURLE_OPERATION_TIMEDOUT;
150 Curl_nonblock(sock, FALSE);
153 * Compose socks4 request
157 * +----+----+----+----+----+----+----+----+----+----+....+----+
158 * | VN | CD | DSTPORT | DSTIP | USERID |NULL|
159 * +----+----+----+----+----+----+----+----+----+----+....+----+
160 * # of bytes: 1 1 2 4 variable 1
163 socksreq[0] = 4; /* version (SOCKS4) */
164 socksreq[1] = 1; /* connect */
165 *((unsigned short*)&socksreq[2]) = htons((unsigned short)remote_port);
167 /* DNS resolve only for SOCKS4, not SOCKS4a */
169 struct Curl_dns_entry *dns;
170 Curl_addrinfo *hp=NULL;
173 rc = Curl_resolv(conn, hostname, remote_port, &dns);
175 if(rc == CURLRESOLV_ERROR)
176 return CURLE_COULDNT_RESOLVE_PROXY;
178 if(rc == CURLRESOLV_PENDING)
179 /* this requires that we're in "wait for resolve" state */
180 rc = Curl_wait_for_resolv(conn, &dns);
183 * We cannot use 'hostent' as a struct that Curl_resolv() returns. It
184 * returns a Curl_addrinfo pointer that may not always look the same.
190 unsigned short ip[4];
191 Curl_printable_address(hp, buf, sizeof(buf));
193 if(4 == sscanf( buf, "%hu.%hu.%hu.%hu",
194 &ip[0], &ip[1], &ip[2], &ip[3])) {
196 socksreq[4] = (unsigned char)ip[0];
197 socksreq[5] = (unsigned char)ip[1];
198 socksreq[6] = (unsigned char)ip[2];
199 socksreq[7] = (unsigned char)ip[3];
202 hp = NULL; /* fail! */
204 Curl_resolv_unlock(data, dns); /* not used anymore from now on */
208 failf(data, "Failed to resolve \"%s\" for SOCKS4 connect.",
210 return CURLE_COULDNT_RESOLVE_HOST;
215 * This is currently not supporting "Identification Protocol (RFC1413)".
217 socksreq[8] = 0; /* ensure empty userid is NUL-terminated */
219 strlcat((char*)socksreq + 8, proxy_name, sizeof(socksreq) - 8);
227 ssize_t hostnamelen = 0;
229 (int)strlen((char*)socksreq + 8); /* size including NUL */
231 /* If SOCKS4a, set special invalid IP address 0.0.0.x */
237 /* If still enough room in buffer, also append hostname */
238 hostnamelen = (ssize_t)strlen(hostname) + 1; /* length including NUL */
239 if (packetsize + hostnamelen <= SOCKS4REQLEN)
240 strcpy((char*)socksreq + packetsize, hostname);
242 hostnamelen = 0; /* Flag: hostname did not fit in buffer */
246 code = Curl_write_plain(conn, sock, (char *)socksreq,
247 packetsize + hostnamelen,
249 if((code != CURLE_OK) || (written != packetsize + hostnamelen)) {
250 failf(data, "Failed to send SOCKS4 connect request.");
251 return CURLE_COULDNT_CONNECT;
253 if (protocol4a && hostnamelen == 0) {
254 /* SOCKS4a with very long hostname - send that name separately */
255 hostnamelen = (ssize_t)strlen(hostname) + 1;
256 code = Curl_write_plain(conn, sock, (char *)hostname, hostnamelen,
258 if((code != CURLE_OK) || (written != hostnamelen)) {
259 failf(data, "Failed to send SOCKS4 connect request.");
260 return CURLE_COULDNT_CONNECT;
264 packetsize = 8; /* receive data size */
266 /* Receive response */
267 result = Curl_blockread_all(conn, sock, (char *)socksreq, packetsize,
268 &actualread, timeout);
269 if((result != CURLE_OK) || (actualread != packetsize)) {
270 failf(data, "Failed to receive SOCKS4 connect request ack.");
271 return CURLE_COULDNT_CONNECT;
277 * +----+----+----+----+----+----+----+----+
278 * | VN | CD | DSTPORT | DSTIP |
279 * +----+----+----+----+----+----+----+----+
280 * # of bytes: 1 1 2 4
282 * VN is the version of the reply code and should be 0. CD is the result
283 * code with one of the following values:
285 * 90: request granted
286 * 91: request rejected or failed
287 * 92: request rejected because SOCKS server cannot connect to
288 * identd on the client
289 * 93: request rejected because the client program and identd
290 * report different user-ids
293 /* wrong version ? */
294 if(socksreq[0] != 0) {
296 "SOCKS4 reply has wrong version, version should be 4.");
297 return CURLE_COULDNT_CONNECT;
305 infof(data, "SOCKS4a request granted.\n");
307 infof(data, "SOCKS4 request granted.\n");
311 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
312 ", request rejected or failed.",
313 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
314 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
315 (unsigned int)ntohs(*(unsigned short*)(&socksreq[8])),
317 return CURLE_COULDNT_CONNECT;
320 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
321 ", request rejected because SOCKS server cannot connect to "
322 "identd on the client.",
323 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
324 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
325 (unsigned int)ntohs(*(unsigned short*)(&socksreq[8])),
327 return CURLE_COULDNT_CONNECT;
330 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
331 ", request rejected because the client program and identd "
332 "report different user-ids.",
333 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
334 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
335 (unsigned int)ntohs(*(unsigned short*)(&socksreq[8])),
337 return CURLE_COULDNT_CONNECT;
340 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
342 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
343 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
344 (unsigned int)ntohs(*(unsigned short*)(&socksreq[8])),
346 return CURLE_COULDNT_CONNECT;
350 Curl_nonblock(sock, TRUE);
352 return CURLE_OK; /* Proxy was successful! */
356 * This function logs in to a SOCKS5 proxy and sends the specifics to the final
357 * destination server.
359 CURLcode Curl_SOCKS5(const char *proxy_name,
360 const char *proxy_password,
361 const char *hostname,
364 struct connectdata *conn)
367 According to the RFC1928, section "6. Replies". This is what a SOCK5
370 +----+-----+-------+------+----------+----------+
371 |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
372 +----+-----+-------+------+----------+----------+
373 | 1 | 1 | X'00' | 1 | Variable | 2 |
374 +----+-----+-------+------+----------+----------+
378 o VER protocol version: X'05'
383 unsigned char socksreq[600]; /* room for large user/pw (255 max each) */
388 curl_socket_t sock = conn->sock[sockindex];
389 struct SessionHandle *data = conn->data;
391 bool socks5_resolve_local = (bool)(data->set.proxytype == CURLPROXY_SOCKS5);
392 const size_t hostname_len = strlen(hostname);
393 ssize_t packetsize = 0;
395 /* RFC1928 chapter 5 specifies max 255 chars for domain name in packet */
396 if(!socks5_resolve_local && hostname_len > 255)
398 infof(conn->data,"SOCKS5: server resolving disabled for hostnames of "
399 "length > 255 [actual len=%d]\n", hostname_len);
400 socks5_resolve_local = TRUE;
404 timeout = Curl_timeleft(conn, NULL, TRUE);
407 /* time-out, bail out, go home */
408 failf(data, "Connection time-out");
409 return CURLE_OPERATION_TIMEDOUT;
412 Curl_nonblock(sock, TRUE);
414 /* wait until socket gets connected */
415 result = Curl_socket_ready(CURL_SOCKET_BAD, sock, (int)timeout);
418 failf(conn->data, "SOCKS5: no connection here");
419 return CURLE_COULDNT_CONNECT;
421 else if(0 == result) {
422 failf(conn->data, "SOCKS5: connection timeout");
423 return CURLE_OPERATION_TIMEDOUT;
426 if(result & CURL_CSELECT_ERR) {
427 failf(conn->data, "SOCKS5: error occured during connection");
428 return CURLE_COULDNT_CONNECT;
431 socksreq[0] = 5; /* version */
432 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
433 socksreq[1] = (char)(proxy_name ? 3 : 2); /* number of methods (below) */
434 socksreq[2] = 0; /* no authentication */
435 socksreq[3] = 1; /* gssapi */
436 socksreq[4] = 2; /* username/password */
438 socksreq[1] = (char)(proxy_name ? 2 : 1); /* number of methods (below) */
439 socksreq[2] = 0; /* no authentication */
440 socksreq[3] = 2; /* username/password */
443 Curl_nonblock(sock, FALSE);
445 code = Curl_write_plain(conn, sock, (char *)socksreq, (2 + (int)socksreq[1]),
447 if((code != CURLE_OK) || (written != (2 + (int)socksreq[1]))) {
448 failf(data, "Unable to send initial SOCKS5 request.");
449 return CURLE_COULDNT_CONNECT;
452 Curl_nonblock(sock, TRUE);
454 result = Curl_socket_ready(sock, CURL_SOCKET_BAD, (int)timeout);
457 failf(conn->data, "SOCKS5 nothing to read");
458 return CURLE_COULDNT_CONNECT;
460 else if(0 == result) {
461 failf(conn->data, "SOCKS5 read timeout");
462 return CURLE_OPERATION_TIMEDOUT;
465 if(result & CURL_CSELECT_ERR) {
466 failf(conn->data, "SOCKS5 read error occured");
467 return CURLE_RECV_ERROR;
470 Curl_nonblock(sock, FALSE);
472 result=Curl_blockread_all(conn, sock, (char *)socksreq, 2, &actualread,
474 if((result != CURLE_OK) || (actualread != 2)) {
475 failf(data, "Unable to receive initial SOCKS5 response.");
476 return CURLE_COULDNT_CONNECT;
479 if(socksreq[0] != 5) {
480 failf(data, "Received invalid version in initial SOCKS5 response.");
481 return CURLE_COULDNT_CONNECT;
483 if(socksreq[1] == 0) {
484 /* Nothing to do, no authentication needed */
487 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
488 else if(socksreq[1] == 1) {
489 code = Curl_SOCKS5_gssapi_negotiate(sockindex, conn);
490 if(code != CURLE_OK) {
491 failf(data, "Unable to negotiate SOCKS5 gssapi context.");
492 return CURLE_COULDNT_CONNECT;
496 else if(socksreq[1] == 2) {
497 /* Needs user name and password */
498 size_t userlen, pwlen;
500 if(proxy_name && proxy_password) {
501 userlen = strlen(proxy_name);
502 pwlen = strlen(proxy_password);
509 /* username/password request looks like
510 * +----+------+----------+------+----------+
511 * |VER | ULEN | UNAME | PLEN | PASSWD |
512 * +----+------+----------+------+----------+
513 * | 1 | 1 | 1 to 255 | 1 | 1 to 255 |
514 * +----+------+----------+------+----------+
517 socksreq[len++] = 1; /* username/pw subnegotiation version */
518 socksreq[len++] = (char) userlen;
519 memcpy(socksreq + len, proxy_name, (int) userlen);
521 socksreq[len++] = (char) pwlen;
522 memcpy(socksreq + len, proxy_password, (int) pwlen);
525 code = Curl_write_plain(conn, sock, (char *)socksreq, len, &written);
526 if((code != CURLE_OK) || (len != written)) {
527 failf(data, "Failed to send SOCKS5 sub-negotiation request.");
528 return CURLE_COULDNT_CONNECT;
531 result=Curl_blockread_all(conn, sock, (char *)socksreq, 2, &actualread,
533 if((result != CURLE_OK) || (actualread != 2)) {
534 failf(data, "Unable to receive SOCKS5 sub-negotiation response.");
535 return CURLE_COULDNT_CONNECT;
538 /* ignore the first (VER) byte */
539 if(socksreq[1] != 0) { /* status */
540 failf(data, "User was rejected by the SOCKS5 server (%d %d).",
541 socksreq[0], socksreq[1]);
542 return CURLE_COULDNT_CONNECT;
545 /* Everything is good so far, user was authenticated! */
549 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
550 if(socksreq[1] == 255) {
552 if(socksreq[1] == 1) {
554 "SOCKS5 GSSAPI per-message authentication is not supported.");
555 return CURLE_COULDNT_CONNECT;
557 else if(socksreq[1] == 255) {
559 if(!proxy_name || !*proxy_name) {
561 "No authentication method was acceptable. (It is quite likely"
562 " that the SOCKS5 server wanted a username/password, since none"
563 " was supplied to the server on this connection.)");
566 failf(data, "No authentication method was acceptable.");
568 return CURLE_COULDNT_CONNECT;
572 "Undocumented SOCKS5 mode attempted to be used by server.");
573 return CURLE_COULDNT_CONNECT;
577 /* Authentication is complete, now specify destination to the proxy */
578 socksreq[0] = 5; /* version (SOCKS5) */
579 socksreq[1] = 1; /* connect */
580 socksreq[2] = 0; /* must be zero */
582 if(!socks5_resolve_local) {
583 packetsize = (ssize_t)(5 + hostname_len + 2);
585 socksreq[3] = 3; /* ATYP: domain name = 3 */
586 socksreq[4] = (char) hostname_len; /* address length */
587 memcpy(&socksreq[5], hostname, hostname_len); /* address bytes w/o NULL */
589 *((unsigned short*)&socksreq[hostname_len+5]) =
590 htons((unsigned short)remote_port);
593 struct Curl_dns_entry *dns;
594 Curl_addrinfo *hp=NULL;
595 int rc = Curl_resolv(conn, hostname, remote_port, &dns);
599 socksreq[3] = 1; /* IPv4 = 1 */
601 if(rc == CURLRESOLV_ERROR)
602 return CURLE_COULDNT_RESOLVE_HOST;
604 if(rc == CURLRESOLV_PENDING)
605 /* this requires that we're in "wait for resolve" state */
606 rc = Curl_wait_for_resolv(conn, &dns);
609 * We cannot use 'hostent' as a struct that Curl_resolv() returns. It
610 * returns a Curl_addrinfo pointer that may not always look the same.
616 unsigned short ip[4];
617 Curl_printable_address(hp, buf, sizeof(buf));
619 if(4 == sscanf( buf, "%hu.%hu.%hu.%hu",
620 &ip[0], &ip[1], &ip[2], &ip[3])) {
621 socksreq[4] = (unsigned char)ip[0];
622 socksreq[5] = (unsigned char)ip[1];
623 socksreq[6] = (unsigned char)ip[2];
624 socksreq[7] = (unsigned char)ip[3];
627 hp = NULL; /* fail! */
629 Curl_resolv_unlock(data, dns); /* not used anymore from now on */
632 failf(data, "Failed to resolve \"%s\" for SOCKS5 connect.",
634 return CURLE_COULDNT_RESOLVE_HOST;
637 *((unsigned short*)&socksreq[8]) = htons((unsigned short)remote_port);
640 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
641 if(conn->socks5_gssapi_enctype) {
642 failf(data, "SOCKS5 gssapi protection not yet implemented.");
645 code = Curl_write_plain(conn, sock, (char *)socksreq, packetsize, &written);
646 if((code != CURLE_OK) || (written != packetsize)) {
647 failf(data, "Failed to send SOCKS5 connect request.");
648 return CURLE_COULDNT_CONNECT;
651 packetsize = 10; /* minimum packet size is 10 */
653 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
654 if(conn->socks5_gssapi_enctype) {
655 failf(data, "SOCKS5 gssapi protection not yet implemented.");
658 result = Curl_blockread_all(conn, sock, (char *)socksreq, packetsize,
659 &actualread, timeout);
660 if((result != CURLE_OK) || (actualread != packetsize)) {
661 failf(data, "Failed to receive SOCKS5 connect request ack.");
662 return CURLE_COULDNT_CONNECT;
665 if(socksreq[0] != 5) { /* version */
667 "SOCKS5 reply has wrong version, version should be 5.");
668 return CURLE_COULDNT_CONNECT;
670 if(socksreq[1] != 0) { /* Anything besides 0 is an error */
672 "Can't complete SOCKS5 connection to %d.%d.%d.%d:%d. (%d)",
673 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
674 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
675 (unsigned int)ntohs(*(unsigned short*)(&socksreq[8])),
677 return CURLE_COULDNT_CONNECT;
680 /* Fix: in general, returned BND.ADDR is variable length parameter by RFC
681 1928, so the reply packet should be read until the end to avoid errors at
682 subsequent protocol level.
684 +----+-----+-------+------+----------+----------+
685 |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
686 +----+-----+-------+------+----------+----------+
687 | 1 | 1 | X'00' | 1 | Variable | 2 |
688 +----+-----+-------+------+----------+----------+
691 o IP v4 address: X'01', BND.ADDR = 4 byte
692 o domain name: X'03', BND.ADDR = [ 1 byte length, string ]
693 o IP v6 address: X'04', BND.ADDR = 16 byte
696 /* Calculate real packet size */
697 if(socksreq[3] == 3) {
699 int addrlen = (int) socksreq[4];
700 packetsize = 5 + addrlen + 2;
702 else if(socksreq[3] == 4) {
704 packetsize = 4 + 16 + 2;
707 /* At this point we already read first 10 bytes */
708 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
709 if(!conn->socks5_gssapi_enctype) {
710 /* decrypt_gssapi_blockread already read the whole packet */
712 if(packetsize > 10) {
714 result = Curl_blockread_all(conn, sock, (char *)&socksreq[10],
715 packetsize, &actualread, timeout);
716 if((result != CURLE_OK) || (actualread != packetsize)) {
717 failf(data, "Failed to receive SOCKS5 connect request ack.");
718 return CURLE_COULDNT_CONNECT;
721 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
725 Curl_nonblock(sock, TRUE);
726 return CURLE_OK; /* Proxy was successful! */
729 #endif /* CURL_DISABLE_PROXY */