xref: /titanic_41/usr/src/cmd/ssh/ssh-socks5-proxy-connect/ssh-socks5-proxy-connect.c (revision 743541abe0006f1be576e21a976c36fb2e87accc)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*743541abSjp161948  * Common Development and Distribution License (the "License").
6*743541abSjp161948  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  *
21*743541abSjp161948  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
227c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * A SOCKS client that let's users 'ssh' to the
297c478bd9Sstevel@tonic-gate  * outside of the firewall by opening up a connection
307c478bd9Sstevel@tonic-gate  * through the SOCKS server. Supports only SOCKS v5.
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <netdb.h>
377c478bd9Sstevel@tonic-gate #include <strings.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <inttypes.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <poll.h>
427c478bd9Sstevel@tonic-gate #include <signal.h>
437c478bd9Sstevel@tonic-gate #include <locale.h>
447c478bd9Sstevel@tonic-gate #include <libintl.h>
457c478bd9Sstevel@tonic-gate #include <netinet/in.h>
467c478bd9Sstevel@tonic-gate #include <sys/types.h>
477c478bd9Sstevel@tonic-gate #include <sys/socket.h>
487c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
497c478bd9Sstevel@tonic-gate #include <sys/time.h>
507c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
517c478bd9Sstevel@tonic-gate #include <sys/stat.h>
527c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
537c478bd9Sstevel@tonic-gate #include "proxy-io.h"
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	DEFAULT_SOCKS5_PORT	"1080"
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static int debug_flag = 0;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static void
usage(void)607c478bd9Sstevel@tonic-gate usage(void)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("Usage: ssh-socks5-proxy-connect "
637c478bd9Sstevel@tonic-gate 	    "[-h socks5_proxy_host] [-p socks5_proxy_port] \n"
647c478bd9Sstevel@tonic-gate 	    "remote_host remote_port\n"));
657c478bd9Sstevel@tonic-gate 	exit(1);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */
697c478bd9Sstevel@tonic-gate static void
debug(const char * format,...)707c478bd9Sstevel@tonic-gate debug(const char *format, ...)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 	char fmtbuf[BUFFER_SIZ];
737c478bd9Sstevel@tonic-gate 	va_list args;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	if (debug_flag == 0) {
767c478bd9Sstevel@tonic-gate 	    return;
777c478bd9Sstevel@tonic-gate 	}
787c478bd9Sstevel@tonic-gate 	va_start(args, format);
797c478bd9Sstevel@tonic-gate 	(void) snprintf(fmtbuf, sizeof (fmtbuf),
807c478bd9Sstevel@tonic-gate 	    "ssh-socks5-proxy: %s\n", format);
817c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmtbuf, args);
827c478bd9Sstevel@tonic-gate 	va_end(args);
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate static void
signal_handler(int sig)867c478bd9Sstevel@tonic-gate signal_handler(int sig)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	exit(0);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static int
do_version_exchange(int sockfd)927c478bd9Sstevel@tonic-gate do_version_exchange(int sockfd)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 	char buffer[3], recv_buf[2];
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	buffer[0] = 0x05;			/* VER */
977c478bd9Sstevel@tonic-gate 	buffer[1] = 0x01;			/* NMETHODS */
987c478bd9Sstevel@tonic-gate 	buffer[2] = 0x00;			/* METHODS */
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if (write(sockfd, &buffer, sizeof (buffer)) < 0) {
1017c478bd9Sstevel@tonic-gate 	    perror("write");
1027c478bd9Sstevel@tonic-gate 	    return (0);
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	if (read(sockfd, &recv_buf, sizeof (recv_buf)) == -1) {
1067c478bd9Sstevel@tonic-gate 	    perror("read");
1077c478bd9Sstevel@tonic-gate 	    return (0);
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	/*
1117c478bd9Sstevel@tonic-gate 	 * No need to check the server's version as per
1127c478bd9Sstevel@tonic-gate 	 * the protocol spec. Check the method supported
1137c478bd9Sstevel@tonic-gate 	 * by the server. Currently if the server does not
1147c478bd9Sstevel@tonic-gate 	 * support NO AUTH, we disconnect.
1157c478bd9Sstevel@tonic-gate 	 */
1167c478bd9Sstevel@tonic-gate 	if (recv_buf[1] != 0x00) {
1177c478bd9Sstevel@tonic-gate 	    debug("Unsupported Authentication Method");
1187c478bd9Sstevel@tonic-gate 	    return (0);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	/* Return success. */
1227c478bd9Sstevel@tonic-gate 	return (1);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate static void
send_request(int sockfd,const char * ssh_host,uchar_t ssh_host_len,uint16_t * ssh_port)1267c478bd9Sstevel@tonic-gate send_request(
1277c478bd9Sstevel@tonic-gate     int sockfd,
1287c478bd9Sstevel@tonic-gate     const char *ssh_host,
1297c478bd9Sstevel@tonic-gate     uchar_t ssh_host_len,
1307c478bd9Sstevel@tonic-gate     uint16_t *ssh_port)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	int failure = 1;
1337c478bd9Sstevel@tonic-gate 	char *buffer, *temp, recv_buf[BUFFER_SIZ];
1347c478bd9Sstevel@tonic-gate 	uchar_t version = 0x05, cmd = 0x01, rsv = 0x00, atyp = 0x03;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	buffer = malloc(strlen(ssh_host) + 7);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	temp = buffer;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	/* Assemble the request packet */
1417c478bd9Sstevel@tonic-gate 	(void) memcpy(temp, &version, sizeof (version));
1427c478bd9Sstevel@tonic-gate 	temp += sizeof (version);
1437c478bd9Sstevel@tonic-gate 	(void) memcpy(temp, &cmd, sizeof (cmd));
1447c478bd9Sstevel@tonic-gate 	temp += sizeof (cmd);
1457c478bd9Sstevel@tonic-gate 	(void) memcpy(temp, &rsv, sizeof (rsv));
1467c478bd9Sstevel@tonic-gate 	temp += sizeof (rsv);
1477c478bd9Sstevel@tonic-gate 	(void) memcpy(temp, &atyp, sizeof (atyp));
1487c478bd9Sstevel@tonic-gate 	temp += sizeof (atyp);
1497c478bd9Sstevel@tonic-gate 	(void) memcpy(temp, &ssh_host_len, sizeof (ssh_host_len));
1507c478bd9Sstevel@tonic-gate 	temp += sizeof (ssh_host_len);
1517c478bd9Sstevel@tonic-gate 	(void) memcpy(temp, ssh_host, strlen(ssh_host));
1527c478bd9Sstevel@tonic-gate 	temp += strlen(ssh_host);
1537c478bd9Sstevel@tonic-gate 	(void) memcpy(temp, ssh_port, sizeof (*ssh_port));
1547c478bd9Sstevel@tonic-gate 	temp += sizeof (*ssh_port);
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	if (write(sockfd, buffer, temp - buffer) == -1) {
1577c478bd9Sstevel@tonic-gate 	    perror("write");
1587c478bd9Sstevel@tonic-gate 	    exit(1);
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 
161*743541abSjp161948 	/*
162*743541abSjp161948 	 * The maximum size of the protocol message we are waiting for is 10
163*743541abSjp161948 	 * bytes -- VER[1], REP[1], RSV[1], ATYP[1], BND.ADDR[4] and
164*743541abSjp161948 	 * BND.PORT[2]; see RFC 1928, section "6. Replies" for more details.
165*743541abSjp161948 	 * Everything else is already a part of the data we are supposed to
166*743541abSjp161948 	 * deliver to the requester. We know that BND.ADDR is exactly 4 bytes
167*743541abSjp161948 	 * since as you can see below, we accept only ATYP == 1 which specifies
168*743541abSjp161948 	 * that the IPv4 address is in a binary format.
169*743541abSjp161948 	 */
170*743541abSjp161948 	if (read(sockfd, &recv_buf, 10) == -1) {
1717c478bd9Sstevel@tonic-gate 	    perror("read");
1727c478bd9Sstevel@tonic-gate 	    exit(1);
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	/* temp now points to the recieve buffer. */
1767c478bd9Sstevel@tonic-gate 	temp = recv_buf;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	/* Check the server's version. */
1797c478bd9Sstevel@tonic-gate 	if (*temp++ != 0x05) {
1807c478bd9Sstevel@tonic-gate 	    (void) fprintf(stderr, gettext("Unsupported SOCKS version: %x\n"),
1817c478bd9Sstevel@tonic-gate 		recv_buf[0]);
1827c478bd9Sstevel@tonic-gate 	    exit(1);
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	/* Check server's reply */
1867c478bd9Sstevel@tonic-gate 	switch (*temp++) {
1877c478bd9Sstevel@tonic-gate 	    case 0x00:
1887c478bd9Sstevel@tonic-gate 		failure = 0;
1897c478bd9Sstevel@tonic-gate 		debug("CONNECT command Succeeded.");
1907c478bd9Sstevel@tonic-gate 		break;
1917c478bd9Sstevel@tonic-gate 	    case 0x01:
1927c478bd9Sstevel@tonic-gate 		debug("General SOCKS server failure.");
1937c478bd9Sstevel@tonic-gate 		break;
1947c478bd9Sstevel@tonic-gate 	    case 0x02:
1957c478bd9Sstevel@tonic-gate 		debug("Connection not allowed by ruleset.");
1967c478bd9Sstevel@tonic-gate 		break;
1977c478bd9Sstevel@tonic-gate 	    case 0x03:
1987c478bd9Sstevel@tonic-gate 		debug("Network Unreachable.");
1997c478bd9Sstevel@tonic-gate 		break;
2007c478bd9Sstevel@tonic-gate 	    case 0x04:
2017c478bd9Sstevel@tonic-gate 		debug("Host unreachable.");
2027c478bd9Sstevel@tonic-gate 		break;
2037c478bd9Sstevel@tonic-gate 	    case 0x05:
2047c478bd9Sstevel@tonic-gate 		debug("Connection refused.");
2057c478bd9Sstevel@tonic-gate 		break;
2067c478bd9Sstevel@tonic-gate 	    case 0x06:
2077c478bd9Sstevel@tonic-gate 		debug("TTL expired.");
2087c478bd9Sstevel@tonic-gate 		break;
2097c478bd9Sstevel@tonic-gate 	    case 0x07:
2107c478bd9Sstevel@tonic-gate 		debug("Command not supported");
2117c478bd9Sstevel@tonic-gate 		break;
2127c478bd9Sstevel@tonic-gate 	    case 0x08:
2137c478bd9Sstevel@tonic-gate 		debug("Address type not supported.");
2147c478bd9Sstevel@tonic-gate 		break;
2157c478bd9Sstevel@tonic-gate 	    default:
2167c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("ssh-socks5-proxy: "
2177c478bd9Sstevel@tonic-gate 		    "SOCKS Server reply not understood\n"));
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	if (failure == 1) {
2217c478bd9Sstevel@tonic-gate 	    exit(1);
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	/* Parse the rest of the packet */
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	/* Ignore RSV */
2277c478bd9Sstevel@tonic-gate 	temp++;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	/* Check ATYP */
2307c478bd9Sstevel@tonic-gate 	if (*temp != 0x01) {
2317c478bd9Sstevel@tonic-gate 	    (void) fprintf(stderr, gettext("ssh-socks5-proxy: "
2327c478bd9Sstevel@tonic-gate 		"Address type not supported: %u\n"), *temp);
2337c478bd9Sstevel@tonic-gate 	    exit(1);
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	free(buffer);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)2407c478bd9Sstevel@tonic-gate main(int argc, char **argv)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	extern char 	*optarg;
2437c478bd9Sstevel@tonic-gate 	extern int	optind;
2447c478bd9Sstevel@tonic-gate 	int 		retval, err_code, sock;
2457c478bd9Sstevel@tonic-gate 	uint16_t 	ssh_port;
2467c478bd9Sstevel@tonic-gate 	uchar_t 	ssh_host_len;
2477c478bd9Sstevel@tonic-gate 	char 		*socks_server = NULL, *socks_port = NULL;
2487c478bd9Sstevel@tonic-gate 	char 		*ssh_host;
2497c478bd9Sstevel@tonic-gate 	struct 		addrinfo hints, *ai;
2507c478bd9Sstevel@tonic-gate 	struct 		pollfd fds[2];
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/* Initialization for variables, set locale and textdomain */
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
2577c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"  /* Use this only if it weren't */
2587c478bd9Sstevel@tonic-gate #endif
2597c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	/* Set up the signal handler */
2627c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, signal_handler);
2637c478bd9Sstevel@tonic-gate 	(void) signal(SIGPIPE, signal_handler);
2647c478bd9Sstevel@tonic-gate 	(void) signal(SIGPOLL, signal_handler);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	while ((retval = getopt(argc, argv, "dp:h:")) != -1) {
2677c478bd9Sstevel@tonic-gate 	    switch (retval) {
2687c478bd9Sstevel@tonic-gate 		case 'h':
2697c478bd9Sstevel@tonic-gate 		    socks_server = optarg;
2707c478bd9Sstevel@tonic-gate 		    break;
2717c478bd9Sstevel@tonic-gate 		case 'p':
2727c478bd9Sstevel@tonic-gate 		    socks_port = optarg;
2737c478bd9Sstevel@tonic-gate 		    break;
2747c478bd9Sstevel@tonic-gate 		case 'd':
2757c478bd9Sstevel@tonic-gate 		    debug_flag = 1;
2767c478bd9Sstevel@tonic-gate 		    break;
2777c478bd9Sstevel@tonic-gate 		default:
2787c478bd9Sstevel@tonic-gate 		    break;
2797c478bd9Sstevel@tonic-gate 	    }
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	if (optind != argc - 2) {
2837c478bd9Sstevel@tonic-gate 		usage();
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	ssh_host = argv[optind++];
2877c478bd9Sstevel@tonic-gate 	ssh_host_len = (uchar_t)strlen(ssh_host);
2887c478bd9Sstevel@tonic-gate 	ssh_port = htons(atoi(argv[optind]));
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	/*
2917c478bd9Sstevel@tonic-gate 	 * If the name and/or port number of the
2927c478bd9Sstevel@tonic-gate 	 * socks server were not passed on the
2937c478bd9Sstevel@tonic-gate 	 * command line, try the user's environment.
2947c478bd9Sstevel@tonic-gate 	 */
2957c478bd9Sstevel@tonic-gate 	if (socks_server == NULL) {
2967c478bd9Sstevel@tonic-gate 	    if ((socks_server = getenv("SOCKS5_SERVER")) == NULL) {
2977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("ssh-socks5-proxy: "
2987c478bd9Sstevel@tonic-gate 		    "SOCKS5 SERVER not specified\n"));
2997c478bd9Sstevel@tonic-gate 		exit(1);
3007c478bd9Sstevel@tonic-gate 	    }
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 	if (socks_port == NULL) {
3037c478bd9Sstevel@tonic-gate 	    if ((socks_port = getenv("SOCKS5_PORT")) == NULL) {
3047c478bd9Sstevel@tonic-gate 		socks_port = DEFAULT_SOCKS5_PORT;
3057c478bd9Sstevel@tonic-gate 	    }
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	debug("SOCKS5_SERVER = %s", socks_server);
3097c478bd9Sstevel@tonic-gate 	debug("SOCKS5_PORT = %s", socks_port);
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	bzero(&hints, sizeof (struct addrinfo));
3127c478bd9Sstevel@tonic-gate 	hints.ai_family = PF_UNSPEC;
3137c478bd9Sstevel@tonic-gate 	hints.ai_socktype = SOCK_STREAM;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	if ((err_code = getaddrinfo(socks_server, socks_port, &hints, &ai))
3167c478bd9Sstevel@tonic-gate 	    != 0) {
3177c478bd9Sstevel@tonic-gate 	    (void) fprintf(stderr, "%s: %s\n", socks_server,
3187c478bd9Sstevel@tonic-gate 		gai_strerror(err_code));
3197c478bd9Sstevel@tonic-gate 	    exit(1);
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	if ((sock = socket(ai->ai_family, SOCK_STREAM, 0)) < 0) {
3237c478bd9Sstevel@tonic-gate 	    perror("socket");
3247c478bd9Sstevel@tonic-gate 	    exit(1);
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	/* Connect to the SOCKS server */
3287c478bd9Sstevel@tonic-gate 	if (connect(sock, ai->ai_addr, ai->ai_addrlen) == 0) {
3297c478bd9Sstevel@tonic-gate 	    debug("Connected to the SOCKS server");
3307c478bd9Sstevel@tonic-gate 	    /* Do the SOCKS v5 communication with the server. */
3317c478bd9Sstevel@tonic-gate 	    if (do_version_exchange(sock) > 0) {
3327c478bd9Sstevel@tonic-gate 		debug("Done version exchange");
3337c478bd9Sstevel@tonic-gate 		send_request(sock, ssh_host, ssh_host_len, &ssh_port);
3347c478bd9Sstevel@tonic-gate 	    } else {
3357c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("ssh-socks5-proxy: Client and "
3367c478bd9Sstevel@tonic-gate 		    "Server versions differ.\n"));
3377c478bd9Sstevel@tonic-gate 		(void) close(sock);
3387c478bd9Sstevel@tonic-gate 		exit(1);
3397c478bd9Sstevel@tonic-gate 	    }
3407c478bd9Sstevel@tonic-gate 	} else {
3417c478bd9Sstevel@tonic-gate 	    perror("connect");
3427c478bd9Sstevel@tonic-gate 	    (void) close(sock);
3437c478bd9Sstevel@tonic-gate 	    exit(1);
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	fds[0].fd = STDIN_FILENO; 	/* Poll stdin for data. */
3477c478bd9Sstevel@tonic-gate 	fds[1].fd = sock; 		/* Poll the socket for data. */
3487c478bd9Sstevel@tonic-gate 	fds[0].events = fds[1].events = POLLIN;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	for (;;) {
3517c478bd9Sstevel@tonic-gate 	    if (poll(fds, 2, INFTIM) == -1) {
3527c478bd9Sstevel@tonic-gate 		perror("poll");
3537c478bd9Sstevel@tonic-gate 		(void) close(sock);
3547c478bd9Sstevel@tonic-gate 		exit(1);
3557c478bd9Sstevel@tonic-gate 	    }
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	    /* Data arrived on stdin, write it to the socket */
3587c478bd9Sstevel@tonic-gate 	    if (fds[0].revents & POLLIN) {
3597c478bd9Sstevel@tonic-gate 		if (proxy_read_write_loop(STDIN_FILENO, sock) == 0) {
3607c478bd9Sstevel@tonic-gate 			(void) close(sock);
3617c478bd9Sstevel@tonic-gate 			exit(1);
3627c478bd9Sstevel@tonic-gate 		}
3637c478bd9Sstevel@tonic-gate 	    } else if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) {
3647c478bd9Sstevel@tonic-gate 		(void) close(sock);
3657c478bd9Sstevel@tonic-gate 		exit(1);
3667c478bd9Sstevel@tonic-gate 	    }
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	    /* Data arrived on the socket, write it to stdout */
3697c478bd9Sstevel@tonic-gate 	    if (fds[1].revents & POLLIN) {
3707c478bd9Sstevel@tonic-gate 		if (proxy_read_write_loop(sock, STDOUT_FILENO) == 0) {
3717c478bd9Sstevel@tonic-gate 			(void) close(sock);
3727c478bd9Sstevel@tonic-gate 			exit(1);
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 	    } else if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL)) {
3757c478bd9Sstevel@tonic-gate 		(void) close(sock);
3767c478bd9Sstevel@tonic-gate 		exit(1);
3777c478bd9Sstevel@tonic-gate 	    }
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
3817c478bd9Sstevel@tonic-gate 	return (0);
3827c478bd9Sstevel@tonic-gate }
383