1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate *
22*7c478bd9Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
23*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
24*7c478bd9Sstevel@tonic-gate */
25*7c478bd9Sstevel@tonic-gate
26*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
27*7c478bd9Sstevel@tonic-gate
28*7c478bd9Sstevel@tonic-gate /*
29*7c478bd9Sstevel@tonic-gate * An http client that let's users 'ssh' to the
30*7c478bd9Sstevel@tonic-gate * outside of the firewall by opening up a connection
31*7c478bd9Sstevel@tonic-gate * through the http proxy.
32*7c478bd9Sstevel@tonic-gate */
33*7c478bd9Sstevel@tonic-gate
34*7c478bd9Sstevel@tonic-gate #include <stdio.h>
35*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
36*7c478bd9Sstevel@tonic-gate #include <string.h>
37*7c478bd9Sstevel@tonic-gate #include <netdb.h>
38*7c478bd9Sstevel@tonic-gate #include <strings.h>
39*7c478bd9Sstevel@tonic-gate #include <unistd.h>
40*7c478bd9Sstevel@tonic-gate #include <inttypes.h>
41*7c478bd9Sstevel@tonic-gate #include <errno.h>
42*7c478bd9Sstevel@tonic-gate #include <poll.h>
43*7c478bd9Sstevel@tonic-gate #include <signal.h>
44*7c478bd9Sstevel@tonic-gate #include <locale.h>
45*7c478bd9Sstevel@tonic-gate #include <libintl.h>
46*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
49*7c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
53*7c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
54*7c478bd9Sstevel@tonic-gate #include "proxy-io.h"
55*7c478bd9Sstevel@tonic-gate
56*7c478bd9Sstevel@tonic-gate #define DEFAULT_HTTPPROXYPORT "80"
57*7c478bd9Sstevel@tonic-gate #define CONNECT_STRLEN 256
58*7c478bd9Sstevel@tonic-gate
59*7c478bd9Sstevel@tonic-gate static int debug_flag = 0;
60*7c478bd9Sstevel@tonic-gate
61*7c478bd9Sstevel@tonic-gate static void
usage(void)62*7c478bd9Sstevel@tonic-gate usage(void)
63*7c478bd9Sstevel@tonic-gate {
64*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Usage: ssh-http-proxy-connect "
65*7c478bd9Sstevel@tonic-gate "[-h http_proxy_host] [-p http_proxy_port]\n"
66*7c478bd9Sstevel@tonic-gate "remote_host remote_port\n"));
67*7c478bd9Sstevel@tonic-gate exit(1);
68*7c478bd9Sstevel@tonic-gate }
69*7c478bd9Sstevel@tonic-gate
70*7c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */
71*7c478bd9Sstevel@tonic-gate static void
debug(const char * format,...)72*7c478bd9Sstevel@tonic-gate debug(const char *format, ...)
73*7c478bd9Sstevel@tonic-gate {
74*7c478bd9Sstevel@tonic-gate char fmtbuf[BUFFER_SIZ];
75*7c478bd9Sstevel@tonic-gate va_list args;
76*7c478bd9Sstevel@tonic-gate
77*7c478bd9Sstevel@tonic-gate if (debug_flag == 0) {
78*7c478bd9Sstevel@tonic-gate return;
79*7c478bd9Sstevel@tonic-gate }
80*7c478bd9Sstevel@tonic-gate va_start(args, format);
81*7c478bd9Sstevel@tonic-gate (void) snprintf(fmtbuf, sizeof (fmtbuf),
82*7c478bd9Sstevel@tonic-gate "ssh-http-proxy: %s\n", format);
83*7c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, fmtbuf, args);
84*7c478bd9Sstevel@tonic-gate va_end(args);
85*7c478bd9Sstevel@tonic-gate }
86*7c478bd9Sstevel@tonic-gate
87*7c478bd9Sstevel@tonic-gate static void
signal_handler(int sig)88*7c478bd9Sstevel@tonic-gate signal_handler(int sig)
89*7c478bd9Sstevel@tonic-gate {
90*7c478bd9Sstevel@tonic-gate exit(0);
91*7c478bd9Sstevel@tonic-gate }
92*7c478bd9Sstevel@tonic-gate
93*7c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)94*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
95*7c478bd9Sstevel@tonic-gate {
96*7c478bd9Sstevel@tonic-gate extern char *optarg;
97*7c478bd9Sstevel@tonic-gate extern int optind;
98*7c478bd9Sstevel@tonic-gate int retval, err_code, sock, ssh_port;
99*7c478bd9Sstevel@tonic-gate int version, ret_code;
100*7c478bd9Sstevel@tonic-gate char *httpproxy = NULL;
101*7c478bd9Sstevel@tonic-gate char *temp, *httpproxyport = NULL;
102*7c478bd9Sstevel@tonic-gate char *ssh_host;
103*7c478bd9Sstevel@tonic-gate char connect_str[CONNECT_STRLEN], connect_reply[BUFFER_SIZ];
104*7c478bd9Sstevel@tonic-gate char *ret_string;
105*7c478bd9Sstevel@tonic-gate struct addrinfo hints, *ai;
106*7c478bd9Sstevel@tonic-gate struct pollfd fds[2];
107*7c478bd9Sstevel@tonic-gate
108*7c478bd9Sstevel@tonic-gate /* Initialization for variables, set locale and textdomain */
109*7c478bd9Sstevel@tonic-gate
110*7c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
111*7c478bd9Sstevel@tonic-gate
112*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
113*7c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
114*7c478bd9Sstevel@tonic-gate #endif
115*7c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
116*7c478bd9Sstevel@tonic-gate
117*7c478bd9Sstevel@tonic-gate /* Set up the signal handler */
118*7c478bd9Sstevel@tonic-gate (void) signal(SIGINT, signal_handler);
119*7c478bd9Sstevel@tonic-gate (void) signal(SIGPIPE, signal_handler);
120*7c478bd9Sstevel@tonic-gate (void) signal(SIGPOLL, signal_handler);
121*7c478bd9Sstevel@tonic-gate
122*7c478bd9Sstevel@tonic-gate while ((retval = getopt(argc, argv, "dp:h:")) != -1) {
123*7c478bd9Sstevel@tonic-gate switch (retval) {
124*7c478bd9Sstevel@tonic-gate case 'h':
125*7c478bd9Sstevel@tonic-gate httpproxy = optarg;
126*7c478bd9Sstevel@tonic-gate break;
127*7c478bd9Sstevel@tonic-gate case 'p':
128*7c478bd9Sstevel@tonic-gate httpproxyport = optarg;
129*7c478bd9Sstevel@tonic-gate break;
130*7c478bd9Sstevel@tonic-gate case 'd':
131*7c478bd9Sstevel@tonic-gate debug_flag = 1;
132*7c478bd9Sstevel@tonic-gate break;
133*7c478bd9Sstevel@tonic-gate default:
134*7c478bd9Sstevel@tonic-gate break;
135*7c478bd9Sstevel@tonic-gate }
136*7c478bd9Sstevel@tonic-gate }
137*7c478bd9Sstevel@tonic-gate
138*7c478bd9Sstevel@tonic-gate if (optind != argc - 2) {
139*7c478bd9Sstevel@tonic-gate usage();
140*7c478bd9Sstevel@tonic-gate }
141*7c478bd9Sstevel@tonic-gate
142*7c478bd9Sstevel@tonic-gate ssh_host = argv[optind++];
143*7c478bd9Sstevel@tonic-gate ssh_port = atoi(argv[optind]);
144*7c478bd9Sstevel@tonic-gate
145*7c478bd9Sstevel@tonic-gate /*
146*7c478bd9Sstevel@tonic-gate * If the name of the http proxy were not
147*7c478bd9Sstevel@tonic-gate * passed on the command line, try the
148*7c478bd9Sstevel@tonic-gate * user's environment. First try HTTPPROXY.
149*7c478bd9Sstevel@tonic-gate * If it's not set, try http_proxy.
150*7c478bd9Sstevel@tonic-gate * Check the url specified for http_proxy
151*7c478bd9Sstevel@tonic-gate * for errors.
152*7c478bd9Sstevel@tonic-gate */
153*7c478bd9Sstevel@tonic-gate if (httpproxy == NULL) {
154*7c478bd9Sstevel@tonic-gate if ((httpproxy = getenv("HTTPPROXY")) == NULL) {
155*7c478bd9Sstevel@tonic-gate /* Try the other environment variable http_proxy */
156*7c478bd9Sstevel@tonic-gate if ((temp = getenv("http_proxy")) != NULL) {
157*7c478bd9Sstevel@tonic-gate temp += strlen("http://");
158*7c478bd9Sstevel@tonic-gate if (strpbrk(temp, ":") == NULL) {
159*7c478bd9Sstevel@tonic-gate /* Malformed url */
160*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("ssh-http-proxy: "
161*7c478bd9Sstevel@tonic-gate "Incorrect url specified for http_proxy "
162*7c478bd9Sstevel@tonic-gate "environment variable\n"));
163*7c478bd9Sstevel@tonic-gate exit(1);
164*7c478bd9Sstevel@tonic-gate }
165*7c478bd9Sstevel@tonic-gate httpproxy = strtok(temp, ":");
166*7c478bd9Sstevel@tonic-gate httpproxyport = strtok(NULL, "/");
167*7c478bd9Sstevel@tonic-gate } else {
168*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
169*7c478bd9Sstevel@tonic-gate gettext("ssh-http-proxy: http proxy not specified\n"));
170*7c478bd9Sstevel@tonic-gate exit(1);
171*7c478bd9Sstevel@tonic-gate }
172*7c478bd9Sstevel@tonic-gate }
173*7c478bd9Sstevel@tonic-gate }
174*7c478bd9Sstevel@tonic-gate
175*7c478bd9Sstevel@tonic-gate /*
176*7c478bd9Sstevel@tonic-gate * Extract the proxy port number from the user's environment.
177*7c478bd9Sstevel@tonic-gate * Ignored if HTTPPROXY is not set.
178*7c478bd9Sstevel@tonic-gate */
179*7c478bd9Sstevel@tonic-gate if ((httpproxy != NULL) && (httpproxyport == NULL)) {
180*7c478bd9Sstevel@tonic-gate if ((httpproxyport = getenv("HTTPPROXYPORT")) == NULL) {
181*7c478bd9Sstevel@tonic-gate httpproxyport = DEFAULT_HTTPPROXYPORT;
182*7c478bd9Sstevel@tonic-gate }
183*7c478bd9Sstevel@tonic-gate }
184*7c478bd9Sstevel@tonic-gate
185*7c478bd9Sstevel@tonic-gate debug("HTTPPROXY = %s", httpproxy);
186*7c478bd9Sstevel@tonic-gate debug("HTTPPROXYPORT = %s", httpproxyport);
187*7c478bd9Sstevel@tonic-gate
188*7c478bd9Sstevel@tonic-gate bzero(&hints, sizeof (struct addrinfo));
189*7c478bd9Sstevel@tonic-gate hints.ai_family = PF_UNSPEC;
190*7c478bd9Sstevel@tonic-gate hints.ai_socktype = SOCK_STREAM;
191*7c478bd9Sstevel@tonic-gate
192*7c478bd9Sstevel@tonic-gate if ((err_code = getaddrinfo(httpproxy, httpproxyport, &hints, &ai))
193*7c478bd9Sstevel@tonic-gate != 0) {
194*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ssh-http-proxy: Unable to "
195*7c478bd9Sstevel@tonic-gate "perform name lookup\n");
196*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s\n", httpproxy,
197*7c478bd9Sstevel@tonic-gate gai_strerror(err_code));
198*7c478bd9Sstevel@tonic-gate exit(1);
199*7c478bd9Sstevel@tonic-gate }
200*7c478bd9Sstevel@tonic-gate
201*7c478bd9Sstevel@tonic-gate if ((sock = socket(ai->ai_family, SOCK_STREAM, 0)) < 0) {
202*7c478bd9Sstevel@tonic-gate perror("socket");
203*7c478bd9Sstevel@tonic-gate exit(1);
204*7c478bd9Sstevel@tonic-gate }
205*7c478bd9Sstevel@tonic-gate
206*7c478bd9Sstevel@tonic-gate /* Connect to the http proxy */
207*7c478bd9Sstevel@tonic-gate if (connect(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
208*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("ssh-http-proxy: Unable to connect"
209*7c478bd9Sstevel@tonic-gate " to %s: %s\n"), httpproxy, strerror(errno));
210*7c478bd9Sstevel@tonic-gate (void) close(sock);
211*7c478bd9Sstevel@tonic-gate exit(1);
212*7c478bd9Sstevel@tonic-gate } else {
213*7c478bd9Sstevel@tonic-gate /* Successful connection. */
214*7c478bd9Sstevel@tonic-gate (void) snprintf(connect_str, sizeof (connect_str),
215*7c478bd9Sstevel@tonic-gate "CONNECT %s:%d HTTP/1.1\r\n\r\n", ssh_host, ssh_port);
216*7c478bd9Sstevel@tonic-gate if (write(sock, &connect_str, strlen(connect_str)) < 0) {
217*7c478bd9Sstevel@tonic-gate perror("write");
218*7c478bd9Sstevel@tonic-gate (void) close(sock);
219*7c478bd9Sstevel@tonic-gate exit(1);
220*7c478bd9Sstevel@tonic-gate }
221*7c478bd9Sstevel@tonic-gate
222*7c478bd9Sstevel@tonic-gate if (read(sock, connect_reply, sizeof (connect_reply)) == -1) {
223*7c478bd9Sstevel@tonic-gate perror("read");
224*7c478bd9Sstevel@tonic-gate (void) close(sock);
225*7c478bd9Sstevel@tonic-gate exit(1);
226*7c478bd9Sstevel@tonic-gate }
227*7c478bd9Sstevel@tonic-gate
228*7c478bd9Sstevel@tonic-gate if (sscanf(connect_reply, "HTTP/1.%d %d",
229*7c478bd9Sstevel@tonic-gate &version, &ret_code) != 2) {
230*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
231*7c478bd9Sstevel@tonic-gate gettext("ssh-http-proxy: HTTP reply not understood\n"));
232*7c478bd9Sstevel@tonic-gate (void) close(sock);
233*7c478bd9Sstevel@tonic-gate exit(1);
234*7c478bd9Sstevel@tonic-gate }
235*7c478bd9Sstevel@tonic-gate
236*7c478bd9Sstevel@tonic-gate ret_string = strtok(connect_reply, "\n");
237*7c478bd9Sstevel@tonic-gate
238*7c478bd9Sstevel@tonic-gate /* If the return error code is not 200, print an error and quit. */
239*7c478bd9Sstevel@tonic-gate if (ret_code != 200) {
240*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", ret_string);
241*7c478bd9Sstevel@tonic-gate (void) close(sock);
242*7c478bd9Sstevel@tonic-gate exit(1);
243*7c478bd9Sstevel@tonic-gate } else {
244*7c478bd9Sstevel@tonic-gate debug("%s", ret_string);
245*7c478bd9Sstevel@tonic-gate }
246*7c478bd9Sstevel@tonic-gate }
247*7c478bd9Sstevel@tonic-gate
248*7c478bd9Sstevel@tonic-gate fds[0].fd = STDIN_FILENO; /* Poll stdin for data. */
249*7c478bd9Sstevel@tonic-gate fds[1].fd = sock; /* Poll the socket for data. */
250*7c478bd9Sstevel@tonic-gate fds[0].events = fds[1].events = POLLIN;
251*7c478bd9Sstevel@tonic-gate
252*7c478bd9Sstevel@tonic-gate for (;;) {
253*7c478bd9Sstevel@tonic-gate if (poll(fds, 2, INFTIM) == -1) {
254*7c478bd9Sstevel@tonic-gate perror("poll");
255*7c478bd9Sstevel@tonic-gate (void) close(sock);
256*7c478bd9Sstevel@tonic-gate exit(1);
257*7c478bd9Sstevel@tonic-gate }
258*7c478bd9Sstevel@tonic-gate
259*7c478bd9Sstevel@tonic-gate /* Data arrived on stdin, write it to the socket */
260*7c478bd9Sstevel@tonic-gate if (fds[0].revents & POLLIN) {
261*7c478bd9Sstevel@tonic-gate if (proxy_read_write_loop(STDIN_FILENO, sock) == 0) {
262*7c478bd9Sstevel@tonic-gate (void) close(sock);
263*7c478bd9Sstevel@tonic-gate exit(1);
264*7c478bd9Sstevel@tonic-gate }
265*7c478bd9Sstevel@tonic-gate } else if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) {
266*7c478bd9Sstevel@tonic-gate (void) close(sock);
267*7c478bd9Sstevel@tonic-gate exit(1);
268*7c478bd9Sstevel@tonic-gate }
269*7c478bd9Sstevel@tonic-gate
270*7c478bd9Sstevel@tonic-gate /* Data arrived on the socket, write it to stdout */
271*7c478bd9Sstevel@tonic-gate if (fds[1].revents & POLLIN) {
272*7c478bd9Sstevel@tonic-gate if (proxy_read_write_loop(sock, STDOUT_FILENO) == 0) {
273*7c478bd9Sstevel@tonic-gate (void) close(sock);
274*7c478bd9Sstevel@tonic-gate exit(1);
275*7c478bd9Sstevel@tonic-gate }
276*7c478bd9Sstevel@tonic-gate } else if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL)) {
277*7c478bd9Sstevel@tonic-gate (void) close(sock);
278*7c478bd9Sstevel@tonic-gate exit(1);
279*7c478bd9Sstevel@tonic-gate }
280*7c478bd9Sstevel@tonic-gate }
281*7c478bd9Sstevel@tonic-gate
282*7c478bd9Sstevel@tonic-gate /* NOTREACHED */
283*7c478bd9Sstevel@tonic-gate return (0);
284*7c478bd9Sstevel@tonic-gate }
285