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  * An http 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 http proxy.
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_HTTPPROXYPORT	"80"
57*0Sstevel@tonic-gate #define	CONNECT_STRLEN		256
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate static int debug_flag = 0;
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate static void
62*0Sstevel@tonic-gate usage(void)
63*0Sstevel@tonic-gate {
64*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("Usage: ssh-http-proxy-connect "
65*0Sstevel@tonic-gate 	    "[-h http_proxy_host] [-p http_proxy_port]\n"
66*0Sstevel@tonic-gate 	    "remote_host remote_port\n"));
67*0Sstevel@tonic-gate 	exit(1);
68*0Sstevel@tonic-gate }
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate /* PRINTFLIKE1 */
71*0Sstevel@tonic-gate static void
72*0Sstevel@tonic-gate debug(const char *format, ...)
73*0Sstevel@tonic-gate {
74*0Sstevel@tonic-gate 	char fmtbuf[BUFFER_SIZ];
75*0Sstevel@tonic-gate 	va_list args;
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 	if (debug_flag == 0) {
78*0Sstevel@tonic-gate 	    return;
79*0Sstevel@tonic-gate 	}
80*0Sstevel@tonic-gate 	va_start(args, format);
81*0Sstevel@tonic-gate 	(void) snprintf(fmtbuf, sizeof (fmtbuf),
82*0Sstevel@tonic-gate 		"ssh-http-proxy: %s\n", format);
83*0Sstevel@tonic-gate 	(void) vfprintf(stderr, fmtbuf, args);
84*0Sstevel@tonic-gate 	va_end(args);
85*0Sstevel@tonic-gate }
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate static void
88*0Sstevel@tonic-gate signal_handler(int sig)
89*0Sstevel@tonic-gate {
90*0Sstevel@tonic-gate 	exit(0);
91*0Sstevel@tonic-gate }
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate int
94*0Sstevel@tonic-gate main(int argc, char **argv)
95*0Sstevel@tonic-gate {
96*0Sstevel@tonic-gate 	extern char 	*optarg;
97*0Sstevel@tonic-gate 	extern int	optind;
98*0Sstevel@tonic-gate 	int 		retval, err_code, sock, ssh_port;
99*0Sstevel@tonic-gate 	int		version, ret_code;
100*0Sstevel@tonic-gate 	char 		*httpproxy = NULL;
101*0Sstevel@tonic-gate 	char		*temp, *httpproxyport = NULL;
102*0Sstevel@tonic-gate 	char 		*ssh_host;
103*0Sstevel@tonic-gate 	char		connect_str[CONNECT_STRLEN], connect_reply[BUFFER_SIZ];
104*0Sstevel@tonic-gate 	char		*ret_string;
105*0Sstevel@tonic-gate 	struct 		addrinfo hints, *ai;
106*0Sstevel@tonic-gate 	struct 		pollfd fds[2];
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	/* Initialization for variables, set locale and textdomain */
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
113*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"  /* Use this only if it weren't */
114*0Sstevel@tonic-gate #endif
115*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	/* Set up the signal handler */
118*0Sstevel@tonic-gate 	(void) signal(SIGINT, signal_handler);
119*0Sstevel@tonic-gate 	(void) signal(SIGPIPE, signal_handler);
120*0Sstevel@tonic-gate 	(void) signal(SIGPOLL, signal_handler);
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	while ((retval = getopt(argc, argv, "dp:h:")) != -1) {
123*0Sstevel@tonic-gate 	    switch (retval) {
124*0Sstevel@tonic-gate 		case 'h':
125*0Sstevel@tonic-gate 		    httpproxy = optarg;
126*0Sstevel@tonic-gate 		    break;
127*0Sstevel@tonic-gate 		case 'p':
128*0Sstevel@tonic-gate 		    httpproxyport = optarg;
129*0Sstevel@tonic-gate 		    break;
130*0Sstevel@tonic-gate 		case 'd':
131*0Sstevel@tonic-gate 		    debug_flag = 1;
132*0Sstevel@tonic-gate 		    break;
133*0Sstevel@tonic-gate 		default:
134*0Sstevel@tonic-gate 		    break;
135*0Sstevel@tonic-gate 	    }
136*0Sstevel@tonic-gate 	}
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	if (optind != argc - 2) {
139*0Sstevel@tonic-gate 		usage();
140*0Sstevel@tonic-gate 	}
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 	ssh_host = argv[optind++];
143*0Sstevel@tonic-gate 	ssh_port = atoi(argv[optind]);
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	/*
146*0Sstevel@tonic-gate 	 * If the name of the http proxy were not
147*0Sstevel@tonic-gate 	 * passed on the command line, try the
148*0Sstevel@tonic-gate 	 * user's environment. First try HTTPPROXY.
149*0Sstevel@tonic-gate 	 * If it's not set, try http_proxy.
150*0Sstevel@tonic-gate 	 * Check the url specified for http_proxy
151*0Sstevel@tonic-gate 	 * for errors.
152*0Sstevel@tonic-gate 	 */
153*0Sstevel@tonic-gate 	if (httpproxy == NULL) {
154*0Sstevel@tonic-gate 	    if ((httpproxy = getenv("HTTPPROXY")) == NULL) {
155*0Sstevel@tonic-gate 		/* Try the other environment variable http_proxy */
156*0Sstevel@tonic-gate 		if ((temp = getenv("http_proxy")) != NULL) {
157*0Sstevel@tonic-gate 		    temp += strlen("http://");
158*0Sstevel@tonic-gate 		    if (strpbrk(temp, ":") == NULL) {
159*0Sstevel@tonic-gate 			/* Malformed url */
160*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("ssh-http-proxy: "
161*0Sstevel@tonic-gate 			    "Incorrect url specified for http_proxy "
162*0Sstevel@tonic-gate 			    "environment variable\n"));
163*0Sstevel@tonic-gate 			exit(1);
164*0Sstevel@tonic-gate 		    }
165*0Sstevel@tonic-gate 		    httpproxy = strtok(temp, ":");
166*0Sstevel@tonic-gate 		    httpproxyport = strtok(NULL, "/");
167*0Sstevel@tonic-gate 		} else {
168*0Sstevel@tonic-gate 		    (void) fprintf(stderr,
169*0Sstevel@tonic-gate 			gettext("ssh-http-proxy: http proxy not specified\n"));
170*0Sstevel@tonic-gate 		    exit(1);
171*0Sstevel@tonic-gate 		}
172*0Sstevel@tonic-gate 	    }
173*0Sstevel@tonic-gate 	}
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	/*
176*0Sstevel@tonic-gate 	 * Extract the proxy port number from the user's environment.
177*0Sstevel@tonic-gate 	 * Ignored if HTTPPROXY is not set.
178*0Sstevel@tonic-gate 	 */
179*0Sstevel@tonic-gate 	if ((httpproxy != NULL) && (httpproxyport == NULL)) {
180*0Sstevel@tonic-gate 	    if ((httpproxyport = getenv("HTTPPROXYPORT")) == NULL) {
181*0Sstevel@tonic-gate 		    httpproxyport = DEFAULT_HTTPPROXYPORT;
182*0Sstevel@tonic-gate 	    }
183*0Sstevel@tonic-gate 	}
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	debug("HTTPPROXY = %s", httpproxy);
186*0Sstevel@tonic-gate 	debug("HTTPPROXYPORT = %s", httpproxyport);
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	bzero(&hints, sizeof (struct addrinfo));
189*0Sstevel@tonic-gate 	hints.ai_family = PF_UNSPEC;
190*0Sstevel@tonic-gate 	hints.ai_socktype = SOCK_STREAM;
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 	if ((err_code = getaddrinfo(httpproxy, httpproxyport, &hints, &ai))
193*0Sstevel@tonic-gate 	    != 0) {
194*0Sstevel@tonic-gate 	    (void) fprintf(stderr, "ssh-http-proxy: Unable to "
195*0Sstevel@tonic-gate 		"perform name lookup\n");
196*0Sstevel@tonic-gate 	    (void) fprintf(stderr, "%s: %s\n", httpproxy,
197*0Sstevel@tonic-gate 		gai_strerror(err_code));
198*0Sstevel@tonic-gate 	    exit(1);
199*0Sstevel@tonic-gate 	}
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	if ((sock = socket(ai->ai_family, SOCK_STREAM, 0)) < 0) {
202*0Sstevel@tonic-gate 	    perror("socket");
203*0Sstevel@tonic-gate 	    exit(1);
204*0Sstevel@tonic-gate 	}
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	/* Connect to the http proxy */
207*0Sstevel@tonic-gate 	if (connect(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
208*0Sstevel@tonic-gate 	    (void) fprintf(stderr, gettext("ssh-http-proxy: Unable to connect"
209*0Sstevel@tonic-gate 		" to %s: %s\n"), httpproxy, strerror(errno));
210*0Sstevel@tonic-gate 	    (void) close(sock);
211*0Sstevel@tonic-gate 	    exit(1);
212*0Sstevel@tonic-gate 	} else {
213*0Sstevel@tonic-gate 	    /* Successful connection. */
214*0Sstevel@tonic-gate 	    (void) snprintf(connect_str, sizeof (connect_str),
215*0Sstevel@tonic-gate 		"CONNECT %s:%d HTTP/1.1\r\n\r\n", ssh_host, ssh_port);
216*0Sstevel@tonic-gate 	    if (write(sock, &connect_str, strlen(connect_str)) < 0) {
217*0Sstevel@tonic-gate 		perror("write");
218*0Sstevel@tonic-gate 		(void) close(sock);
219*0Sstevel@tonic-gate 		exit(1);
220*0Sstevel@tonic-gate 	    }
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	    if (read(sock, connect_reply, sizeof (connect_reply)) == -1) {
223*0Sstevel@tonic-gate 		perror("read");
224*0Sstevel@tonic-gate 		(void) close(sock);
225*0Sstevel@tonic-gate 		exit(1);
226*0Sstevel@tonic-gate 	    }
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 	    if (sscanf(connect_reply, "HTTP/1.%d %d",
229*0Sstevel@tonic-gate 		&version, &ret_code) != 2) {
230*0Sstevel@tonic-gate 		(void) fprintf(stderr,
231*0Sstevel@tonic-gate 		    gettext("ssh-http-proxy: HTTP reply not understood\n"));
232*0Sstevel@tonic-gate 		(void) close(sock);
233*0Sstevel@tonic-gate 		exit(1);
234*0Sstevel@tonic-gate 	    }
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 	    ret_string = strtok(connect_reply, "\n");
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 	    /* If the return error code is not 200, print an error and quit. */
239*0Sstevel@tonic-gate 	    if (ret_code != 200) {
240*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s\n", ret_string);
241*0Sstevel@tonic-gate 		(void) close(sock);
242*0Sstevel@tonic-gate 		exit(1);
243*0Sstevel@tonic-gate 	    } else {
244*0Sstevel@tonic-gate 		debug("%s", ret_string);
245*0Sstevel@tonic-gate 	    }
246*0Sstevel@tonic-gate 	}
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 	fds[0].fd = STDIN_FILENO; 	/* Poll stdin for data. */
249*0Sstevel@tonic-gate 	fds[1].fd = sock; 		/* Poll the socket for data. */
250*0Sstevel@tonic-gate 	fds[0].events = fds[1].events = POLLIN;
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 	for (;;) {
253*0Sstevel@tonic-gate 	    if (poll(fds, 2, INFTIM) == -1) {
254*0Sstevel@tonic-gate 		perror("poll");
255*0Sstevel@tonic-gate 		(void) close(sock);
256*0Sstevel@tonic-gate 		exit(1);
257*0Sstevel@tonic-gate 	    }
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate 	    /* Data arrived on stdin, write it to the socket */
260*0Sstevel@tonic-gate 	    if (fds[0].revents & POLLIN) {
261*0Sstevel@tonic-gate 		if (proxy_read_write_loop(STDIN_FILENO, sock) == 0) {
262*0Sstevel@tonic-gate 			(void) close(sock);
263*0Sstevel@tonic-gate 			exit(1);
264*0Sstevel@tonic-gate 		}
265*0Sstevel@tonic-gate 	    } else if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) {
266*0Sstevel@tonic-gate 		(void) close(sock);
267*0Sstevel@tonic-gate 		exit(1);
268*0Sstevel@tonic-gate 	    }
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 	    /* Data arrived on the socket, write it to stdout */
271*0Sstevel@tonic-gate 	    if (fds[1].revents & POLLIN) {
272*0Sstevel@tonic-gate 		if (proxy_read_write_loop(sock, STDOUT_FILENO) == 0) {
273*0Sstevel@tonic-gate 			(void) close(sock);
274*0Sstevel@tonic-gate 			exit(1);
275*0Sstevel@tonic-gate 		}
276*0Sstevel@tonic-gate 	    } else if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL)) {
277*0Sstevel@tonic-gate 		(void) close(sock);
278*0Sstevel@tonic-gate 		exit(1);
279*0Sstevel@tonic-gate 	    }
280*0Sstevel@tonic-gate 	}
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	/* NOTREACHED */
283*0Sstevel@tonic-gate 	return (0);
284*0Sstevel@tonic-gate }
285