1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate * 22*0Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 23*0Sstevel@tonic-gate * Use is subject to license terms. 24*0Sstevel@tonic-gate */ 25*0Sstevel@tonic-gate 26*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 27*0Sstevel@tonic-gate 28*0Sstevel@tonic-gate /* 29*0Sstevel@tonic-gate * A SOCKS client that let's users 'ssh' to the 30*0Sstevel@tonic-gate * outside of the firewall by opening up a connection 31*0Sstevel@tonic-gate * through the SOCKS server. Supports only SOCKS v5. 32*0Sstevel@tonic-gate */ 33*0Sstevel@tonic-gate 34*0Sstevel@tonic-gate #include <stdio.h> 35*0Sstevel@tonic-gate #include <stdlib.h> 36*0Sstevel@tonic-gate #include <string.h> 37*0Sstevel@tonic-gate #include <netdb.h> 38*0Sstevel@tonic-gate #include <strings.h> 39*0Sstevel@tonic-gate #include <unistd.h> 40*0Sstevel@tonic-gate #include <inttypes.h> 41*0Sstevel@tonic-gate #include <errno.h> 42*0Sstevel@tonic-gate #include <poll.h> 43*0Sstevel@tonic-gate #include <signal.h> 44*0Sstevel@tonic-gate #include <locale.h> 45*0Sstevel@tonic-gate #include <libintl.h> 46*0Sstevel@tonic-gate #include <netinet/in.h> 47*0Sstevel@tonic-gate #include <sys/types.h> 48*0Sstevel@tonic-gate #include <sys/socket.h> 49*0Sstevel@tonic-gate #include <arpa/inet.h> 50*0Sstevel@tonic-gate #include <sys/time.h> 51*0Sstevel@tonic-gate #include <sys/stropts.h> 52*0Sstevel@tonic-gate #include <sys/stat.h> 53*0Sstevel@tonic-gate #include <sys/varargs.h> 54*0Sstevel@tonic-gate #include "proxy-io.h" 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate #define DEFAULT_SOCKS5_PORT "1080" 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate static int debug_flag = 0; 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate static void 61*0Sstevel@tonic-gate usage(void) 62*0Sstevel@tonic-gate { 63*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("Usage: ssh-socks5-proxy-connect " 64*0Sstevel@tonic-gate "[-h socks5_proxy_host] [-p socks5_proxy_port] \n" 65*0Sstevel@tonic-gate "remote_host remote_port\n")); 66*0Sstevel@tonic-gate exit(1); 67*0Sstevel@tonic-gate } 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gate /* PRINTFLIKE1 */ 70*0Sstevel@tonic-gate static void 71*0Sstevel@tonic-gate debug(const char *format, ...) 72*0Sstevel@tonic-gate { 73*0Sstevel@tonic-gate char fmtbuf[BUFFER_SIZ]; 74*0Sstevel@tonic-gate va_list args; 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gate if (debug_flag == 0) { 77*0Sstevel@tonic-gate return; 78*0Sstevel@tonic-gate } 79*0Sstevel@tonic-gate va_start(args, format); 80*0Sstevel@tonic-gate (void) snprintf(fmtbuf, sizeof (fmtbuf), 81*0Sstevel@tonic-gate "ssh-socks5-proxy: %s\n", format); 82*0Sstevel@tonic-gate (void) vfprintf(stderr, fmtbuf, args); 83*0Sstevel@tonic-gate va_end(args); 84*0Sstevel@tonic-gate } 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate static void 87*0Sstevel@tonic-gate signal_handler(int sig) 88*0Sstevel@tonic-gate { 89*0Sstevel@tonic-gate exit(0); 90*0Sstevel@tonic-gate } 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate static int 93*0Sstevel@tonic-gate do_version_exchange(int sockfd) 94*0Sstevel@tonic-gate { 95*0Sstevel@tonic-gate char buffer[3], recv_buf[2]; 96*0Sstevel@tonic-gate 97*0Sstevel@tonic-gate buffer[0] = 0x05; /* VER */ 98*0Sstevel@tonic-gate buffer[1] = 0x01; /* NMETHODS */ 99*0Sstevel@tonic-gate buffer[2] = 0x00; /* METHODS */ 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate if (write(sockfd, &buffer, sizeof (buffer)) < 0) { 102*0Sstevel@tonic-gate perror("write"); 103*0Sstevel@tonic-gate return (0); 104*0Sstevel@tonic-gate } 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate if (read(sockfd, &recv_buf, sizeof (recv_buf)) == -1) { 107*0Sstevel@tonic-gate perror("read"); 108*0Sstevel@tonic-gate return (0); 109*0Sstevel@tonic-gate } 110*0Sstevel@tonic-gate 111*0Sstevel@tonic-gate /* 112*0Sstevel@tonic-gate * No need to check the server's version as per 113*0Sstevel@tonic-gate * the protocol spec. Check the method supported 114*0Sstevel@tonic-gate * by the server. Currently if the server does not 115*0Sstevel@tonic-gate * support NO AUTH, we disconnect. 116*0Sstevel@tonic-gate */ 117*0Sstevel@tonic-gate if (recv_buf[1] != 0x00) { 118*0Sstevel@tonic-gate debug("Unsupported Authentication Method"); 119*0Sstevel@tonic-gate return (0); 120*0Sstevel@tonic-gate } 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gate /* Return success. */ 123*0Sstevel@tonic-gate return (1); 124*0Sstevel@tonic-gate } 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate static void 127*0Sstevel@tonic-gate send_request( 128*0Sstevel@tonic-gate int sockfd, 129*0Sstevel@tonic-gate const char *ssh_host, 130*0Sstevel@tonic-gate uchar_t ssh_host_len, 131*0Sstevel@tonic-gate uint16_t *ssh_port) 132*0Sstevel@tonic-gate { 133*0Sstevel@tonic-gate int failure = 1; 134*0Sstevel@tonic-gate char *buffer, *temp, recv_buf[BUFFER_SIZ]; 135*0Sstevel@tonic-gate uchar_t version = 0x05, cmd = 0x01, rsv = 0x00, atyp = 0x03; 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gate buffer = malloc(strlen(ssh_host) + 7); 138*0Sstevel@tonic-gate 139*0Sstevel@tonic-gate temp = buffer; 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gate /* Assemble the request packet */ 142*0Sstevel@tonic-gate (void) memcpy(temp, &version, sizeof (version)); 143*0Sstevel@tonic-gate temp += sizeof (version); 144*0Sstevel@tonic-gate (void) memcpy(temp, &cmd, sizeof (cmd)); 145*0Sstevel@tonic-gate temp += sizeof (cmd); 146*0Sstevel@tonic-gate (void) memcpy(temp, &rsv, sizeof (rsv)); 147*0Sstevel@tonic-gate temp += sizeof (rsv); 148*0Sstevel@tonic-gate (void) memcpy(temp, &atyp, sizeof (atyp)); 149*0Sstevel@tonic-gate temp += sizeof (atyp); 150*0Sstevel@tonic-gate (void) memcpy(temp, &ssh_host_len, sizeof (ssh_host_len)); 151*0Sstevel@tonic-gate temp += sizeof (ssh_host_len); 152*0Sstevel@tonic-gate (void) memcpy(temp, ssh_host, strlen(ssh_host)); 153*0Sstevel@tonic-gate temp += strlen(ssh_host); 154*0Sstevel@tonic-gate (void) memcpy(temp, ssh_port, sizeof (*ssh_port)); 155*0Sstevel@tonic-gate temp += sizeof (*ssh_port); 156*0Sstevel@tonic-gate 157*0Sstevel@tonic-gate if (write(sockfd, buffer, temp - buffer) == -1) { 158*0Sstevel@tonic-gate perror("write"); 159*0Sstevel@tonic-gate exit(1); 160*0Sstevel@tonic-gate } 161*0Sstevel@tonic-gate 162*0Sstevel@tonic-gate if (read(sockfd, &recv_buf, sizeof (recv_buf)) == -1) { 163*0Sstevel@tonic-gate perror("read"); 164*0Sstevel@tonic-gate exit(1); 165*0Sstevel@tonic-gate } 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gate /* temp now points to the recieve buffer. */ 168*0Sstevel@tonic-gate temp = recv_buf; 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gate /* Check the server's version. */ 171*0Sstevel@tonic-gate if (*temp++ != 0x05) { 172*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("Unsupported SOCKS version: %x\n"), 173*0Sstevel@tonic-gate recv_buf[0]); 174*0Sstevel@tonic-gate exit(1); 175*0Sstevel@tonic-gate } 176*0Sstevel@tonic-gate 177*0Sstevel@tonic-gate /* Check server's reply */ 178*0Sstevel@tonic-gate switch (*temp++) { 179*0Sstevel@tonic-gate case 0x00: 180*0Sstevel@tonic-gate failure = 0; 181*0Sstevel@tonic-gate debug("CONNECT command Succeeded."); 182*0Sstevel@tonic-gate break; 183*0Sstevel@tonic-gate case 0x01: 184*0Sstevel@tonic-gate debug("General SOCKS server failure."); 185*0Sstevel@tonic-gate break; 186*0Sstevel@tonic-gate case 0x02: 187*0Sstevel@tonic-gate debug("Connection not allowed by ruleset."); 188*0Sstevel@tonic-gate break; 189*0Sstevel@tonic-gate case 0x03: 190*0Sstevel@tonic-gate debug("Network Unreachable."); 191*0Sstevel@tonic-gate break; 192*0Sstevel@tonic-gate case 0x04: 193*0Sstevel@tonic-gate debug("Host unreachable."); 194*0Sstevel@tonic-gate break; 195*0Sstevel@tonic-gate case 0x05: 196*0Sstevel@tonic-gate debug("Connection refused."); 197*0Sstevel@tonic-gate break; 198*0Sstevel@tonic-gate case 0x06: 199*0Sstevel@tonic-gate debug("TTL expired."); 200*0Sstevel@tonic-gate break; 201*0Sstevel@tonic-gate case 0x07: 202*0Sstevel@tonic-gate debug("Command not supported"); 203*0Sstevel@tonic-gate break; 204*0Sstevel@tonic-gate case 0x08: 205*0Sstevel@tonic-gate debug("Address type not supported."); 206*0Sstevel@tonic-gate break; 207*0Sstevel@tonic-gate default: 208*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("ssh-socks5-proxy: " 209*0Sstevel@tonic-gate "SOCKS Server reply not understood\n")); 210*0Sstevel@tonic-gate } 211*0Sstevel@tonic-gate 212*0Sstevel@tonic-gate if (failure == 1) { 213*0Sstevel@tonic-gate exit(1); 214*0Sstevel@tonic-gate } 215*0Sstevel@tonic-gate 216*0Sstevel@tonic-gate /* Parse the rest of the packet */ 217*0Sstevel@tonic-gate 218*0Sstevel@tonic-gate /* Ignore RSV */ 219*0Sstevel@tonic-gate temp++; 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate /* Check ATYP */ 222*0Sstevel@tonic-gate if (*temp != 0x01) { 223*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("ssh-socks5-proxy: " 224*0Sstevel@tonic-gate "Address type not supported: %u\n"), *temp); 225*0Sstevel@tonic-gate exit(1); 226*0Sstevel@tonic-gate } 227*0Sstevel@tonic-gate 228*0Sstevel@tonic-gate free(buffer); 229*0Sstevel@tonic-gate } 230*0Sstevel@tonic-gate 231*0Sstevel@tonic-gate int 232*0Sstevel@tonic-gate main(int argc, char **argv) 233*0Sstevel@tonic-gate { 234*0Sstevel@tonic-gate extern char *optarg; 235*0Sstevel@tonic-gate extern int optind; 236*0Sstevel@tonic-gate int retval, err_code, sock; 237*0Sstevel@tonic-gate uint16_t ssh_port; 238*0Sstevel@tonic-gate uchar_t ssh_host_len; 239*0Sstevel@tonic-gate char *socks_server = NULL, *socks_port = NULL; 240*0Sstevel@tonic-gate char *ssh_host; 241*0Sstevel@tonic-gate struct addrinfo hints, *ai; 242*0Sstevel@tonic-gate struct pollfd fds[2]; 243*0Sstevel@tonic-gate 244*0Sstevel@tonic-gate /* Initialization for variables, set locale and textdomain */ 245*0Sstevel@tonic-gate 246*0Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 247*0Sstevel@tonic-gate 248*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 249*0Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 250*0Sstevel@tonic-gate #endif 251*0Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 252*0Sstevel@tonic-gate 253*0Sstevel@tonic-gate /* Set up the signal handler */ 254*0Sstevel@tonic-gate (void) signal(SIGINT, signal_handler); 255*0Sstevel@tonic-gate (void) signal(SIGPIPE, signal_handler); 256*0Sstevel@tonic-gate (void) signal(SIGPOLL, signal_handler); 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gate while ((retval = getopt(argc, argv, "dp:h:")) != -1) { 259*0Sstevel@tonic-gate switch (retval) { 260*0Sstevel@tonic-gate case 'h': 261*0Sstevel@tonic-gate socks_server = optarg; 262*0Sstevel@tonic-gate break; 263*0Sstevel@tonic-gate case 'p': 264*0Sstevel@tonic-gate socks_port = optarg; 265*0Sstevel@tonic-gate break; 266*0Sstevel@tonic-gate case 'd': 267*0Sstevel@tonic-gate debug_flag = 1; 268*0Sstevel@tonic-gate break; 269*0Sstevel@tonic-gate default: 270*0Sstevel@tonic-gate break; 271*0Sstevel@tonic-gate } 272*0Sstevel@tonic-gate } 273*0Sstevel@tonic-gate 274*0Sstevel@tonic-gate if (optind != argc - 2) { 275*0Sstevel@tonic-gate usage(); 276*0Sstevel@tonic-gate } 277*0Sstevel@tonic-gate 278*0Sstevel@tonic-gate ssh_host = argv[optind++]; 279*0Sstevel@tonic-gate ssh_host_len = (uchar_t)strlen(ssh_host); 280*0Sstevel@tonic-gate ssh_port = htons(atoi(argv[optind])); 281*0Sstevel@tonic-gate 282*0Sstevel@tonic-gate /* 283*0Sstevel@tonic-gate * If the name and/or port number of the 284*0Sstevel@tonic-gate * socks server were not passed on the 285*0Sstevel@tonic-gate * command line, try the user's environment. 286*0Sstevel@tonic-gate */ 287*0Sstevel@tonic-gate if (socks_server == NULL) { 288*0Sstevel@tonic-gate if ((socks_server = getenv("SOCKS5_SERVER")) == NULL) { 289*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("ssh-socks5-proxy: " 290*0Sstevel@tonic-gate "SOCKS5 SERVER not specified\n")); 291*0Sstevel@tonic-gate exit(1); 292*0Sstevel@tonic-gate } 293*0Sstevel@tonic-gate } 294*0Sstevel@tonic-gate if (socks_port == NULL) { 295*0Sstevel@tonic-gate if ((socks_port = getenv("SOCKS5_PORT")) == NULL) { 296*0Sstevel@tonic-gate socks_port = DEFAULT_SOCKS5_PORT; 297*0Sstevel@tonic-gate } 298*0Sstevel@tonic-gate } 299*0Sstevel@tonic-gate 300*0Sstevel@tonic-gate debug("SOCKS5_SERVER = %s", socks_server); 301*0Sstevel@tonic-gate debug("SOCKS5_PORT = %s", socks_port); 302*0Sstevel@tonic-gate 303*0Sstevel@tonic-gate bzero(&hints, sizeof (struct addrinfo)); 304*0Sstevel@tonic-gate hints.ai_family = PF_UNSPEC; 305*0Sstevel@tonic-gate hints.ai_socktype = SOCK_STREAM; 306*0Sstevel@tonic-gate 307*0Sstevel@tonic-gate if ((err_code = getaddrinfo(socks_server, socks_port, &hints, &ai)) 308*0Sstevel@tonic-gate != 0) { 309*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s\n", socks_server, 310*0Sstevel@tonic-gate gai_strerror(err_code)); 311*0Sstevel@tonic-gate exit(1); 312*0Sstevel@tonic-gate } 313*0Sstevel@tonic-gate 314*0Sstevel@tonic-gate if ((sock = socket(ai->ai_family, SOCK_STREAM, 0)) < 0) { 315*0Sstevel@tonic-gate perror("socket"); 316*0Sstevel@tonic-gate exit(1); 317*0Sstevel@tonic-gate } 318*0Sstevel@tonic-gate 319*0Sstevel@tonic-gate /* Connect to the SOCKS server */ 320*0Sstevel@tonic-gate if (connect(sock, ai->ai_addr, ai->ai_addrlen) == 0) { 321*0Sstevel@tonic-gate debug("Connected to the SOCKS server"); 322*0Sstevel@tonic-gate /* Do the SOCKS v5 communication with the server. */ 323*0Sstevel@tonic-gate if (do_version_exchange(sock) > 0) { 324*0Sstevel@tonic-gate debug("Done version exchange"); 325*0Sstevel@tonic-gate send_request(sock, ssh_host, ssh_host_len, &ssh_port); 326*0Sstevel@tonic-gate } else { 327*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("ssh-socks5-proxy: Client and " 328*0Sstevel@tonic-gate "Server versions differ.\n")); 329*0Sstevel@tonic-gate (void) close(sock); 330*0Sstevel@tonic-gate exit(1); 331*0Sstevel@tonic-gate } 332*0Sstevel@tonic-gate } else { 333*0Sstevel@tonic-gate perror("connect"); 334*0Sstevel@tonic-gate (void) close(sock); 335*0Sstevel@tonic-gate exit(1); 336*0Sstevel@tonic-gate } 337*0Sstevel@tonic-gate 338*0Sstevel@tonic-gate fds[0].fd = STDIN_FILENO; /* Poll stdin for data. */ 339*0Sstevel@tonic-gate fds[1].fd = sock; /* Poll the socket for data. */ 340*0Sstevel@tonic-gate fds[0].events = fds[1].events = POLLIN; 341*0Sstevel@tonic-gate 342*0Sstevel@tonic-gate for (;;) { 343*0Sstevel@tonic-gate if (poll(fds, 2, INFTIM) == -1) { 344*0Sstevel@tonic-gate perror("poll"); 345*0Sstevel@tonic-gate (void) close(sock); 346*0Sstevel@tonic-gate exit(1); 347*0Sstevel@tonic-gate } 348*0Sstevel@tonic-gate 349*0Sstevel@tonic-gate /* Data arrived on stdin, write it to the socket */ 350*0Sstevel@tonic-gate if (fds[0].revents & POLLIN) { 351*0Sstevel@tonic-gate if (proxy_read_write_loop(STDIN_FILENO, sock) == 0) { 352*0Sstevel@tonic-gate (void) close(sock); 353*0Sstevel@tonic-gate exit(1); 354*0Sstevel@tonic-gate } 355*0Sstevel@tonic-gate } else if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) { 356*0Sstevel@tonic-gate (void) close(sock); 357*0Sstevel@tonic-gate exit(1); 358*0Sstevel@tonic-gate } 359*0Sstevel@tonic-gate 360*0Sstevel@tonic-gate /* Data arrived on the socket, write it to stdout */ 361*0Sstevel@tonic-gate if (fds[1].revents & POLLIN) { 362*0Sstevel@tonic-gate if (proxy_read_write_loop(sock, STDOUT_FILENO) == 0) { 363*0Sstevel@tonic-gate (void) close(sock); 364*0Sstevel@tonic-gate exit(1); 365*0Sstevel@tonic-gate } 366*0Sstevel@tonic-gate } else if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL)) { 367*0Sstevel@tonic-gate (void) close(sock); 368*0Sstevel@tonic-gate exit(1); 369*0Sstevel@tonic-gate } 370*0Sstevel@tonic-gate } 371*0Sstevel@tonic-gate 372*0Sstevel@tonic-gate /* NOTREACHED */ 373*0Sstevel@tonic-gate return (0); 374*0Sstevel@tonic-gate } 375