10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*5600Sjp161948 * Common Development and Distribution License (the "License").
6*5600Sjp161948 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate *
21*5600Sjp161948 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
220Sstevel@tonic-gate * Use is subject to license terms.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
260Sstevel@tonic-gate
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate * A SOCKS client that let's users 'ssh' to the
290Sstevel@tonic-gate * outside of the firewall by opening up a connection
300Sstevel@tonic-gate * through the SOCKS server. Supports only SOCKS v5.
310Sstevel@tonic-gate */
320Sstevel@tonic-gate
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <string.h>
360Sstevel@tonic-gate #include <netdb.h>
370Sstevel@tonic-gate #include <strings.h>
380Sstevel@tonic-gate #include <unistd.h>
390Sstevel@tonic-gate #include <inttypes.h>
400Sstevel@tonic-gate #include <errno.h>
410Sstevel@tonic-gate #include <poll.h>
420Sstevel@tonic-gate #include <signal.h>
430Sstevel@tonic-gate #include <locale.h>
440Sstevel@tonic-gate #include <libintl.h>
450Sstevel@tonic-gate #include <netinet/in.h>
460Sstevel@tonic-gate #include <sys/types.h>
470Sstevel@tonic-gate #include <sys/socket.h>
480Sstevel@tonic-gate #include <arpa/inet.h>
490Sstevel@tonic-gate #include <sys/time.h>
500Sstevel@tonic-gate #include <sys/stropts.h>
510Sstevel@tonic-gate #include <sys/stat.h>
520Sstevel@tonic-gate #include <sys/varargs.h>
530Sstevel@tonic-gate #include "proxy-io.h"
540Sstevel@tonic-gate
550Sstevel@tonic-gate #define DEFAULT_SOCKS5_PORT "1080"
560Sstevel@tonic-gate
570Sstevel@tonic-gate static int debug_flag = 0;
580Sstevel@tonic-gate
590Sstevel@tonic-gate static void
usage(void)600Sstevel@tonic-gate usage(void)
610Sstevel@tonic-gate {
620Sstevel@tonic-gate (void) fprintf(stderr, gettext("Usage: ssh-socks5-proxy-connect "
630Sstevel@tonic-gate "[-h socks5_proxy_host] [-p socks5_proxy_port] \n"
640Sstevel@tonic-gate "remote_host remote_port\n"));
650Sstevel@tonic-gate exit(1);
660Sstevel@tonic-gate }
670Sstevel@tonic-gate
680Sstevel@tonic-gate /* PRINTFLIKE1 */
690Sstevel@tonic-gate static void
debug(const char * format,...)700Sstevel@tonic-gate debug(const char *format, ...)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate char fmtbuf[BUFFER_SIZ];
730Sstevel@tonic-gate va_list args;
740Sstevel@tonic-gate
750Sstevel@tonic-gate if (debug_flag == 0) {
760Sstevel@tonic-gate return;
770Sstevel@tonic-gate }
780Sstevel@tonic-gate va_start(args, format);
790Sstevel@tonic-gate (void) snprintf(fmtbuf, sizeof (fmtbuf),
800Sstevel@tonic-gate "ssh-socks5-proxy: %s\n", format);
810Sstevel@tonic-gate (void) vfprintf(stderr, fmtbuf, args);
820Sstevel@tonic-gate va_end(args);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate
850Sstevel@tonic-gate static void
signal_handler(int sig)860Sstevel@tonic-gate signal_handler(int sig)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate exit(0);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate
910Sstevel@tonic-gate static int
do_version_exchange(int sockfd)920Sstevel@tonic-gate do_version_exchange(int sockfd)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate char buffer[3], recv_buf[2];
950Sstevel@tonic-gate
960Sstevel@tonic-gate buffer[0] = 0x05; /* VER */
970Sstevel@tonic-gate buffer[1] = 0x01; /* NMETHODS */
980Sstevel@tonic-gate buffer[2] = 0x00; /* METHODS */
990Sstevel@tonic-gate
1000Sstevel@tonic-gate if (write(sockfd, &buffer, sizeof (buffer)) < 0) {
1010Sstevel@tonic-gate perror("write");
1020Sstevel@tonic-gate return (0);
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate if (read(sockfd, &recv_buf, sizeof (recv_buf)) == -1) {
1060Sstevel@tonic-gate perror("read");
1070Sstevel@tonic-gate return (0);
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate /*
1110Sstevel@tonic-gate * No need to check the server's version as per
1120Sstevel@tonic-gate * the protocol spec. Check the method supported
1130Sstevel@tonic-gate * by the server. Currently if the server does not
1140Sstevel@tonic-gate * support NO AUTH, we disconnect.
1150Sstevel@tonic-gate */
1160Sstevel@tonic-gate if (recv_buf[1] != 0x00) {
1170Sstevel@tonic-gate debug("Unsupported Authentication Method");
1180Sstevel@tonic-gate return (0);
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate /* Return success. */
1220Sstevel@tonic-gate return (1);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate static void
send_request(int sockfd,const char * ssh_host,uchar_t ssh_host_len,uint16_t * ssh_port)1260Sstevel@tonic-gate send_request(
1270Sstevel@tonic-gate int sockfd,
1280Sstevel@tonic-gate const char *ssh_host,
1290Sstevel@tonic-gate uchar_t ssh_host_len,
1300Sstevel@tonic-gate uint16_t *ssh_port)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate int failure = 1;
1330Sstevel@tonic-gate char *buffer, *temp, recv_buf[BUFFER_SIZ];
1340Sstevel@tonic-gate uchar_t version = 0x05, cmd = 0x01, rsv = 0x00, atyp = 0x03;
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate buffer = malloc(strlen(ssh_host) + 7);
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate temp = buffer;
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate /* Assemble the request packet */
1410Sstevel@tonic-gate (void) memcpy(temp, &version, sizeof (version));
1420Sstevel@tonic-gate temp += sizeof (version);
1430Sstevel@tonic-gate (void) memcpy(temp, &cmd, sizeof (cmd));
1440Sstevel@tonic-gate temp += sizeof (cmd);
1450Sstevel@tonic-gate (void) memcpy(temp, &rsv, sizeof (rsv));
1460Sstevel@tonic-gate temp += sizeof (rsv);
1470Sstevel@tonic-gate (void) memcpy(temp, &atyp, sizeof (atyp));
1480Sstevel@tonic-gate temp += sizeof (atyp);
1490Sstevel@tonic-gate (void) memcpy(temp, &ssh_host_len, sizeof (ssh_host_len));
1500Sstevel@tonic-gate temp += sizeof (ssh_host_len);
1510Sstevel@tonic-gate (void) memcpy(temp, ssh_host, strlen(ssh_host));
1520Sstevel@tonic-gate temp += strlen(ssh_host);
1530Sstevel@tonic-gate (void) memcpy(temp, ssh_port, sizeof (*ssh_port));
1540Sstevel@tonic-gate temp += sizeof (*ssh_port);
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate if (write(sockfd, buffer, temp - buffer) == -1) {
1570Sstevel@tonic-gate perror("write");
1580Sstevel@tonic-gate exit(1);
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate
161*5600Sjp161948 /*
162*5600Sjp161948 * The maximum size of the protocol message we are waiting for is 10
163*5600Sjp161948 * bytes -- VER[1], REP[1], RSV[1], ATYP[1], BND.ADDR[4] and
164*5600Sjp161948 * BND.PORT[2]; see RFC 1928, section "6. Replies" for more details.
165*5600Sjp161948 * Everything else is already a part of the data we are supposed to
166*5600Sjp161948 * deliver to the requester. We know that BND.ADDR is exactly 4 bytes
167*5600Sjp161948 * since as you can see below, we accept only ATYP == 1 which specifies
168*5600Sjp161948 * that the IPv4 address is in a binary format.
169*5600Sjp161948 */
170*5600Sjp161948 if (read(sockfd, &recv_buf, 10) == -1) {
1710Sstevel@tonic-gate perror("read");
1720Sstevel@tonic-gate exit(1);
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate /* temp now points to the recieve buffer. */
1760Sstevel@tonic-gate temp = recv_buf;
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate /* Check the server's version. */
1790Sstevel@tonic-gate if (*temp++ != 0x05) {
1800Sstevel@tonic-gate (void) fprintf(stderr, gettext("Unsupported SOCKS version: %x\n"),
1810Sstevel@tonic-gate recv_buf[0]);
1820Sstevel@tonic-gate exit(1);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate /* Check server's reply */
1860Sstevel@tonic-gate switch (*temp++) {
1870Sstevel@tonic-gate case 0x00:
1880Sstevel@tonic-gate failure = 0;
1890Sstevel@tonic-gate debug("CONNECT command Succeeded.");
1900Sstevel@tonic-gate break;
1910Sstevel@tonic-gate case 0x01:
1920Sstevel@tonic-gate debug("General SOCKS server failure.");
1930Sstevel@tonic-gate break;
1940Sstevel@tonic-gate case 0x02:
1950Sstevel@tonic-gate debug("Connection not allowed by ruleset.");
1960Sstevel@tonic-gate break;
1970Sstevel@tonic-gate case 0x03:
1980Sstevel@tonic-gate debug("Network Unreachable.");
1990Sstevel@tonic-gate break;
2000Sstevel@tonic-gate case 0x04:
2010Sstevel@tonic-gate debug("Host unreachable.");
2020Sstevel@tonic-gate break;
2030Sstevel@tonic-gate case 0x05:
2040Sstevel@tonic-gate debug("Connection refused.");
2050Sstevel@tonic-gate break;
2060Sstevel@tonic-gate case 0x06:
2070Sstevel@tonic-gate debug("TTL expired.");
2080Sstevel@tonic-gate break;
2090Sstevel@tonic-gate case 0x07:
2100Sstevel@tonic-gate debug("Command not supported");
2110Sstevel@tonic-gate break;
2120Sstevel@tonic-gate case 0x08:
2130Sstevel@tonic-gate debug("Address type not supported.");
2140Sstevel@tonic-gate break;
2150Sstevel@tonic-gate default:
2160Sstevel@tonic-gate (void) fprintf(stderr, gettext("ssh-socks5-proxy: "
2170Sstevel@tonic-gate "SOCKS Server reply not understood\n"));
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate if (failure == 1) {
2210Sstevel@tonic-gate exit(1);
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate /* Parse the rest of the packet */
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate /* Ignore RSV */
2270Sstevel@tonic-gate temp++;
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate /* Check ATYP */
2300Sstevel@tonic-gate if (*temp != 0x01) {
2310Sstevel@tonic-gate (void) fprintf(stderr, gettext("ssh-socks5-proxy: "
2320Sstevel@tonic-gate "Address type not supported: %u\n"), *temp);
2330Sstevel@tonic-gate exit(1);
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate free(buffer);
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate int
main(int argc,char ** argv)2400Sstevel@tonic-gate main(int argc, char **argv)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate extern char *optarg;
2430Sstevel@tonic-gate extern int optind;
2440Sstevel@tonic-gate int retval, err_code, sock;
2450Sstevel@tonic-gate uint16_t ssh_port;
2460Sstevel@tonic-gate uchar_t ssh_host_len;
2470Sstevel@tonic-gate char *socks_server = NULL, *socks_port = NULL;
2480Sstevel@tonic-gate char *ssh_host;
2490Sstevel@tonic-gate struct addrinfo hints, *ai;
2500Sstevel@tonic-gate struct pollfd fds[2];
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate /* Initialization for variables, set locale and textdomain */
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
2570Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
2580Sstevel@tonic-gate #endif
2590Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate /* Set up the signal handler */
2620Sstevel@tonic-gate (void) signal(SIGINT, signal_handler);
2630Sstevel@tonic-gate (void) signal(SIGPIPE, signal_handler);
2640Sstevel@tonic-gate (void) signal(SIGPOLL, signal_handler);
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate while ((retval = getopt(argc, argv, "dp:h:")) != -1) {
2670Sstevel@tonic-gate switch (retval) {
2680Sstevel@tonic-gate case 'h':
2690Sstevel@tonic-gate socks_server = optarg;
2700Sstevel@tonic-gate break;
2710Sstevel@tonic-gate case 'p':
2720Sstevel@tonic-gate socks_port = optarg;
2730Sstevel@tonic-gate break;
2740Sstevel@tonic-gate case 'd':
2750Sstevel@tonic-gate debug_flag = 1;
2760Sstevel@tonic-gate break;
2770Sstevel@tonic-gate default:
2780Sstevel@tonic-gate break;
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate }
2810Sstevel@tonic-gate
2820Sstevel@tonic-gate if (optind != argc - 2) {
2830Sstevel@tonic-gate usage();
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate
2860Sstevel@tonic-gate ssh_host = argv[optind++];
2870Sstevel@tonic-gate ssh_host_len = (uchar_t)strlen(ssh_host);
2880Sstevel@tonic-gate ssh_port = htons(atoi(argv[optind]));
2890Sstevel@tonic-gate
2900Sstevel@tonic-gate /*
2910Sstevel@tonic-gate * If the name and/or port number of the
2920Sstevel@tonic-gate * socks server were not passed on the
2930Sstevel@tonic-gate * command line, try the user's environment.
2940Sstevel@tonic-gate */
2950Sstevel@tonic-gate if (socks_server == NULL) {
2960Sstevel@tonic-gate if ((socks_server = getenv("SOCKS5_SERVER")) == NULL) {
2970Sstevel@tonic-gate (void) fprintf(stderr, gettext("ssh-socks5-proxy: "
2980Sstevel@tonic-gate "SOCKS5 SERVER not specified\n"));
2990Sstevel@tonic-gate exit(1);
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate if (socks_port == NULL) {
3030Sstevel@tonic-gate if ((socks_port = getenv("SOCKS5_PORT")) == NULL) {
3040Sstevel@tonic-gate socks_port = DEFAULT_SOCKS5_PORT;
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate debug("SOCKS5_SERVER = %s", socks_server);
3090Sstevel@tonic-gate debug("SOCKS5_PORT = %s", socks_port);
3100Sstevel@tonic-gate
3110Sstevel@tonic-gate bzero(&hints, sizeof (struct addrinfo));
3120Sstevel@tonic-gate hints.ai_family = PF_UNSPEC;
3130Sstevel@tonic-gate hints.ai_socktype = SOCK_STREAM;
3140Sstevel@tonic-gate
3150Sstevel@tonic-gate if ((err_code = getaddrinfo(socks_server, socks_port, &hints, &ai))
3160Sstevel@tonic-gate != 0) {
3170Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s\n", socks_server,
3180Sstevel@tonic-gate gai_strerror(err_code));
3190Sstevel@tonic-gate exit(1);
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate
3220Sstevel@tonic-gate if ((sock = socket(ai->ai_family, SOCK_STREAM, 0)) < 0) {
3230Sstevel@tonic-gate perror("socket");
3240Sstevel@tonic-gate exit(1);
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate /* Connect to the SOCKS server */
3280Sstevel@tonic-gate if (connect(sock, ai->ai_addr, ai->ai_addrlen) == 0) {
3290Sstevel@tonic-gate debug("Connected to the SOCKS server");
3300Sstevel@tonic-gate /* Do the SOCKS v5 communication with the server. */
3310Sstevel@tonic-gate if (do_version_exchange(sock) > 0) {
3320Sstevel@tonic-gate debug("Done version exchange");
3330Sstevel@tonic-gate send_request(sock, ssh_host, ssh_host_len, &ssh_port);
3340Sstevel@tonic-gate } else {
3350Sstevel@tonic-gate (void) fprintf(stderr, gettext("ssh-socks5-proxy: Client and "
3360Sstevel@tonic-gate "Server versions differ.\n"));
3370Sstevel@tonic-gate (void) close(sock);
3380Sstevel@tonic-gate exit(1);
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate } else {
3410Sstevel@tonic-gate perror("connect");
3420Sstevel@tonic-gate (void) close(sock);
3430Sstevel@tonic-gate exit(1);
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate fds[0].fd = STDIN_FILENO; /* Poll stdin for data. */
3470Sstevel@tonic-gate fds[1].fd = sock; /* Poll the socket for data. */
3480Sstevel@tonic-gate fds[0].events = fds[1].events = POLLIN;
3490Sstevel@tonic-gate
3500Sstevel@tonic-gate for (;;) {
3510Sstevel@tonic-gate if (poll(fds, 2, INFTIM) == -1) {
3520Sstevel@tonic-gate perror("poll");
3530Sstevel@tonic-gate (void) close(sock);
3540Sstevel@tonic-gate exit(1);
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate
3570Sstevel@tonic-gate /* Data arrived on stdin, write it to the socket */
3580Sstevel@tonic-gate if (fds[0].revents & POLLIN) {
3590Sstevel@tonic-gate if (proxy_read_write_loop(STDIN_FILENO, sock) == 0) {
3600Sstevel@tonic-gate (void) close(sock);
3610Sstevel@tonic-gate exit(1);
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate } else if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) {
3640Sstevel@tonic-gate (void) close(sock);
3650Sstevel@tonic-gate exit(1);
3660Sstevel@tonic-gate }
3670Sstevel@tonic-gate
3680Sstevel@tonic-gate /* Data arrived on the socket, write it to stdout */
3690Sstevel@tonic-gate if (fds[1].revents & POLLIN) {
3700Sstevel@tonic-gate if (proxy_read_write_loop(sock, STDOUT_FILENO) == 0) {
3710Sstevel@tonic-gate (void) close(sock);
3720Sstevel@tonic-gate exit(1);
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate } else if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL)) {
3750Sstevel@tonic-gate (void) close(sock);
3760Sstevel@tonic-gate exit(1);
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate
3800Sstevel@tonic-gate /* NOTREACHED */
3810Sstevel@tonic-gate return (0);
3820Sstevel@tonic-gate }
383