blob: 978289b6182fc4e61192a35610b08590698de904 [file] [log] [blame]
Darren Tucker39972492006-07-12 22:22:46 +10001/* $OpenBSD: clientloop.c,v 1.168 2006/07/11 20:07:25 stevesk Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * The main loop for the interactive session (client side).
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
14 *
15 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 *
Damien Miller1383bd82000-04-06 12:32:37 +100038 * SSH2 support added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000039 * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110040 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110060 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
62#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110063
Damien Millerf17883e2006-03-15 11:45:54 +110064#include <sys/types.h>
65#ifdef HAVE_SYS_STAT_H
66# include <sys/stat.h>
67#endif
Damien Millere3b60b52006-07-10 21:08:03 +100068#include <sys/socket.h>
Damien Miller17e91c02006-03-15 11:28:34 +110069#include <sys/ioctl.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110070
Damien Millerc7b06362006-03-15 11:53:45 +110071#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100072#include <errno.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110073#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110074#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110075#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110076#include <signal.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110077#include <termios.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000080#include "ssh1.h"
81#include "ssh2.h"
82#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083#include "packet.h"
84#include "buffer.h"
Damien Millerb38eff82000-04-01 11:09:21 +100085#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000086#include "channels.h"
Damien Millerb38eff82000-04-01 11:09:21 +100087#include "dispatch.h"
Damien Millerad833b32000-08-23 10:46:23 +100088#include "buffer.h"
89#include "bufaux.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110090#include "key.h"
Ben Lindstromf28f6342001-04-04 02:03:04 +000091#include "kex.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000092#include "log.h"
93#include "readconf.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000094#include "clientloop.h"
Damien Milleraeb31d62005-12-13 19:29:36 +110095#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000096#include "authfd.h"
97#include "atomicio.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +100098#include "sshpty.h"
Ben Lindstrom302ea6f2001-04-16 02:01:25 +000099#include "misc.h"
Damien Miller0e220db2004-06-15 10:34:08 +1000100#include "monitor_fdpass.h"
101#include "match.h"
102#include "msg.h"
Damien Miller69b69aa2000-10-28 14:19:58 +1100103
104/* import options */
105extern Options options;
106
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107/* Flag indicating that stdin should be redirected from /dev/null. */
108extern int stdin_null_flag;
109
Damien Millerd6965512003-12-17 16:31:53 +1100110/* Flag indicating that no shell has been requested */
111extern int no_shell_flag;
112
Damien Miller0e220db2004-06-15 10:34:08 +1000113/* Control socket */
114extern int control_fd;
115
Damien Miller5428f641999-11-25 11:54:57 +1100116/*
117 * Name of the host we are connecting to. This is the name given on the
118 * command line, or the HostName specified for the user-supplied name in a
119 * configuration file.
120 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121extern char *host;
122
Damien Miller5428f641999-11-25 11:54:57 +1100123/*
124 * Flag to indicate that we have received a window change signal which has
125 * not yet been processed. This will cause a message indicating the new
126 * window size to be sent to the server a little later. This is volatile
127 * because this is updated in a signal handler.
128 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000129static volatile sig_atomic_t received_window_change_signal = 0;
130static volatile sig_atomic_t received_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131
Damien Miller788f2122005-11-05 15:14:59 +1100132/* Flag indicating whether the user's terminal is in non-blocking mode. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133static int in_non_blocking_mode = 0;
134
135/* Common data for the client loop code. */
Damien Miller97c91f62006-04-23 12:08:37 +1000136static volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
Damien Millerad833b32000-08-23 10:46:23 +1000137static int escape_char; /* Escape character. */
Damien Miller95def091999-11-25 00:26:21 +1100138static int escape_pending; /* Last character was the escape character */
139static int last_was_cr; /* Last character was a newline. */
140static int exit_status; /* Used to store the exit status of the command. */
141static int stdin_eof; /* EOF has been encountered on standard error. */
142static Buffer stdin_buffer; /* Buffer for stdin data. */
143static Buffer stdout_buffer; /* Buffer for stdout data. */
144static Buffer stderr_buffer; /* Buffer for stderr data. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000145static u_long stdin_bytes, stdout_bytes, stderr_bytes;
146static u_int buffer_high;/* Soft max buffer size. */
Damien Miller95def091999-11-25 00:26:21 +1100147static int connection_in; /* Connection to server (input). */
148static int connection_out; /* Connection to server (output). */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000149static int need_rekeying; /* Set to non-zero if rekeying is requested. */
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000150static int session_closed = 0; /* In SSH2: login session closed. */
Damien Miller509b0102003-12-17 16:33:10 +1100151static int server_alive_timeouts = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152
Ben Lindstrombba81212001-06-25 05:01:22 +0000153static void client_init_dispatch(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000154int session_ident = -1;
155
Damien Miller0e220db2004-06-15 10:34:08 +1000156struct confirm_ctx {
157 int want_tty;
158 int want_subsys;
Damien Miller13390022005-07-06 09:44:19 +1000159 int want_x_fwd;
160 int want_agent_fwd;
Damien Miller0e220db2004-06-15 10:34:08 +1000161 Buffer cmd;
162 char *term;
163 struct termios tio;
Damien Miller3756dce2004-06-18 01:17:29 +1000164 char **env;
Damien Miller0e220db2004-06-15 10:34:08 +1000165};
166
Ben Lindstromf28f6342001-04-04 02:03:04 +0000167/*XXX*/
168extern Kex *xxx_kex;
169
Damien Miller0e220db2004-06-15 10:34:08 +1000170void ssh_process_session2_setup(int, int, int, Buffer *);
171
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000172/* Restores stdin to blocking mode. */
173
Ben Lindstrombba81212001-06-25 05:01:22 +0000174static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000175leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000176{
Damien Miller95def091999-11-25 00:26:21 +1100177 if (in_non_blocking_mode) {
Damien Miller03e66f62004-06-15 15:47:51 +1000178 unset_nonblock(fileno(stdin));
Damien Miller95def091999-11-25 00:26:21 +1100179 in_non_blocking_mode = 0;
Damien Miller95def091999-11-25 00:26:21 +1100180 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000181}
182
Damien Miller95def091999-11-25 00:26:21 +1100183/* Puts stdin terminal in non-blocking mode. */
184
Ben Lindstrombba81212001-06-25 05:01:22 +0000185static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000186enter_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000187{
Damien Miller95def091999-11-25 00:26:21 +1100188 in_non_blocking_mode = 1;
Damien Miller232711f2004-06-15 10:35:30 +1000189 set_nonblock(fileno(stdin));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190}
191
Damien Miller5428f641999-11-25 11:54:57 +1100192/*
193 * Signal handler for the window change signal (SIGWINCH). This just sets a
194 * flag indicating that the window has changed.
195 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100196/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000197static void
Damien Miller95def091999-11-25 00:26:21 +1100198window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199{
Damien Miller95def091999-11-25 00:26:21 +1100200 received_window_change_signal = 1;
201 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202}
203
Damien Miller5428f641999-11-25 11:54:57 +1100204/*
205 * Signal handler for signals that cause the program to terminate. These
206 * signals must be trapped to restore terminal modes.
207 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100208/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000209static void
Damien Miller95def091999-11-25 00:26:21 +1100210signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000211{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000212 received_signal = sig;
213 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214}
215
Damien Miller5428f641999-11-25 11:54:57 +1100216/*
217 * Returns current time in seconds from Jan 1, 1970 with the maximum
218 * available resolution.
219 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000220
Ben Lindstrombba81212001-06-25 05:01:22 +0000221static double
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000222get_current_time(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223{
Damien Miller95def091999-11-25 00:26:21 +1100224 struct timeval tv;
225 gettimeofday(&tv, NULL);
226 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000227}
228
Damien Miller17e7ed02005-06-17 12:54:33 +1000229#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
230void
231client_x11_get_proto(const char *display, const char *xauth_path,
232 u_int trusted, char **_proto, char **_data)
233{
234 char cmd[1024];
235 char line[512];
236 char xdisplay[512];
237 static char proto[512], data[512];
238 FILE *f;
239 int got_data = 0, generated = 0, do_unlink = 0, i;
240 char *xauthdir, *xauthfile;
241 struct stat st;
242
243 xauthdir = xauthfile = NULL;
244 *_proto = proto;
245 *_data = data;
246 proto[0] = data[0] = '\0';
247
248 if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) {
249 debug("No xauth program.");
250 } else {
251 if (display == NULL) {
252 debug("x11_get_proto: DISPLAY not set");
253 return;
254 }
255 /*
256 * Handle FamilyLocal case where $DISPLAY does
257 * not match an authorization entry. For this we
258 * just try "xauth list unix:displaynum.screennum".
259 * XXX: "localhost" match to determine FamilyLocal
260 * is not perfect.
261 */
262 if (strncmp(display, "localhost:", 10) == 0) {
263 snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
264 display + 10);
265 display = xdisplay;
266 }
267 if (trusted == 0) {
268 xauthdir = xmalloc(MAXPATHLEN);
269 xauthfile = xmalloc(MAXPATHLEN);
270 strlcpy(xauthdir, "/tmp/ssh-XXXXXXXXXX", MAXPATHLEN);
271 if (mkdtemp(xauthdir) != NULL) {
272 do_unlink = 1;
273 snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile",
274 xauthdir);
275 snprintf(cmd, sizeof(cmd),
276 "%s -f %s generate %s " SSH_X11_PROTO
277 " untrusted timeout 1200 2>" _PATH_DEVNULL,
278 xauth_path, xauthfile, display);
279 debug2("x11_get_proto: %s", cmd);
280 if (system(cmd) == 0)
281 generated = 1;
282 }
283 }
284 snprintf(cmd, sizeof(cmd),
Darren Tuckerd89dbf22005-10-03 18:05:26 +1000285 "%s %s%s list %s 2>" _PATH_DEVNULL,
Damien Miller17e7ed02005-06-17 12:54:33 +1000286 xauth_path,
287 generated ? "-f " : "" ,
288 generated ? xauthfile : "",
289 display);
290 debug2("x11_get_proto: %s", cmd);
291 f = popen(cmd, "r");
292 if (f && fgets(line, sizeof(line), f) &&
293 sscanf(line, "%*s %511s %511s", proto, data) == 2)
294 got_data = 1;
295 if (f)
296 pclose(f);
297 }
298
299 if (do_unlink) {
300 unlink(xauthfile);
301 rmdir(xauthdir);
302 }
303 if (xauthdir)
304 xfree(xauthdir);
305 if (xauthfile)
306 xfree(xauthfile);
307
308 /*
309 * If we didn't get authentication data, just make up some
310 * data. The forwarding code will check the validity of the
311 * response anyway, and substitute this data. The X11
312 * server, however, will ignore this fake data and use
313 * whatever authentication mechanisms it was using otherwise
314 * for the local connection.
315 */
316 if (!got_data) {
317 u_int32_t rnd = 0;
318
319 logit("Warning: No xauth data; "
320 "using fake authentication data for X11 forwarding.");
321 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
322 for (i = 0; i < 16; i++) {
323 if (i % 4 == 0)
324 rnd = arc4random();
325 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
326 rnd & 0xff);
327 rnd >>= 8;
328 }
329 }
330}
331
Damien Miller5428f641999-11-25 11:54:57 +1100332/*
333 * This is called when the interactive is entered. This checks if there is
334 * an EOF coming on stdin. We must check this explicitly, as select() does
335 * not appear to wake up when redirecting from /dev/null.
336 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000337
Ben Lindstrombba81212001-06-25 05:01:22 +0000338static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000339client_check_initial_eof_on_stdin(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000340{
Damien Miller95def091999-11-25 00:26:21 +1100341 int len;
342 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343
Damien Miller5428f641999-11-25 11:54:57 +1100344 /*
345 * If standard input is to be "redirected from /dev/null", we simply
346 * mark that we have seen an EOF and send an EOF message to the
347 * server. Otherwise, we try to read a single character; it appears
348 * that for some files, such /dev/null, select() never wakes up for
349 * read for this descriptor, which means that we never get EOF. This
350 * way we will get the EOF if stdin comes from /dev/null or similar.
351 */
Damien Miller95def091999-11-25 00:26:21 +1100352 if (stdin_null_flag) {
353 /* Fake EOF on stdin. */
354 debug("Sending eof.");
355 stdin_eof = 1;
356 packet_start(SSH_CMSG_EOF);
357 packet_send();
358 } else {
Damien Miller95def091999-11-25 00:26:21 +1100359 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360
Damien Miller95def091999-11-25 00:26:21 +1100361 /* Check for immediate EOF on stdin. */
362 len = read(fileno(stdin), buf, 1);
363 if (len == 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100364 /* EOF. Record that we have seen it and send EOF to server. */
Damien Miller95def091999-11-25 00:26:21 +1100365 debug("Sending eof.");
366 stdin_eof = 1;
367 packet_start(SSH_CMSG_EOF);
368 packet_send();
369 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100370 /*
371 * Got data. We must store the data in the buffer,
372 * and also process it as an escape character if
373 * appropriate.
374 */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000375 if ((u_char) buf[0] == escape_char)
Damien Miller95def091999-11-25 00:26:21 +1100376 escape_pending = 1;
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000377 else
Damien Miller95def091999-11-25 00:26:21 +1100378 buffer_append(&stdin_buffer, buf, 1);
Damien Miller95def091999-11-25 00:26:21 +1100379 }
Damien Miller95def091999-11-25 00:26:21 +1100380 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000381 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000382}
383
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000384
Damien Miller5428f641999-11-25 11:54:57 +1100385/*
386 * Make packets from buffered stdin data, and buffer them for sending to the
387 * connection.
388 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000389
Ben Lindstrombba81212001-06-25 05:01:22 +0000390static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000391client_make_packets_from_stdin_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000392{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000393 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000394
Damien Miller95def091999-11-25 00:26:21 +1100395 /* Send buffered stdin data to the server. */
396 while (buffer_len(&stdin_buffer) > 0 &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100397 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100398 len = buffer_len(&stdin_buffer);
399 /* Keep the packets at reasonable size. */
400 if (len > packet_get_maxsize())
401 len = packet_get_maxsize();
402 packet_start(SSH_CMSG_STDIN_DATA);
403 packet_put_string(buffer_ptr(&stdin_buffer), len);
404 packet_send();
405 buffer_consume(&stdin_buffer, len);
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000406 stdin_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +1100407 /* If we have a pending EOF, send it now. */
408 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
409 packet_start(SSH_CMSG_EOF);
410 packet_send();
411 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000412 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413}
414
Damien Miller5428f641999-11-25 11:54:57 +1100415/*
416 * Checks if the client window has changed, and sends a packet about it to
417 * the server if so. The actual change is detected elsewhere (by a software
418 * interrupt on Unix); this just checks the flag and sends a message if
419 * appropriate.
420 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000421
Ben Lindstrombba81212001-06-25 05:01:22 +0000422static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000423client_check_window_change(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000424{
Damien Miller1383bd82000-04-06 12:32:37 +1000425 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000426
Damien Miller1383bd82000-04-06 12:32:37 +1000427 if (! received_window_change_signal)
428 return;
429 /** XXX race */
430 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431
Damien Millerd3444942000-09-30 14:20:03 +1100432 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000433
434 if (compat20) {
Damien Miller0e220db2004-06-15 10:34:08 +1000435 channel_send_window_changes();
Damien Miller1383bd82000-04-06 12:32:37 +1000436 } else {
Damien Miller0e220db2004-06-15 10:34:08 +1000437 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
438 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000439 packet_start(SSH_CMSG_WINDOW_SIZE);
Damien Miller71a73672006-03-26 14:04:36 +1100440 packet_put_int((u_int)ws.ws_row);
441 packet_put_int((u_int)ws.ws_col);
442 packet_put_int((u_int)ws.ws_xpixel);
443 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller1383bd82000-04-06 12:32:37 +1000444 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000446}
447
Damien Miller509b0102003-12-17 16:33:10 +1100448static void
449client_global_request_reply(int type, u_int32_t seq, void *ctxt)
450{
451 server_alive_timeouts = 0;
452 client_global_request_reply_fwd(type, seq, ctxt);
453}
454
455static void
456server_alive_check(void)
457{
458 if (++server_alive_timeouts > options.server_alive_count_max)
459 packet_disconnect("Timeout, server not responding.");
460 packet_start(SSH2_MSG_GLOBAL_REQUEST);
461 packet_put_cstring("keepalive@openssh.com");
462 packet_put_char(1); /* boolean: want reply */
463 packet_send();
464}
465
Damien Miller5428f641999-11-25 11:54:57 +1100466/*
467 * Waits until the client can do something (some data becomes available on
468 * one of the file descriptors).
469 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000470static void
Damien Miller5e953212001-01-30 09:14:00 +1100471client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000472 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473{
Damien Miller509b0102003-12-17 16:33:10 +1100474 struct timeval tv, *tvp;
475 int ret;
476
Damien Miller5e953212001-01-30 09:14:00 +1100477 /* Add any selections by the channel mechanism. */
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000478 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000479
Damien Miller1383bd82000-04-06 12:32:37 +1000480 if (!compat20) {
481 /* Read from the connection, unless our buffers are full. */
482 if (buffer_len(&stdout_buffer) < buffer_high &&
483 buffer_len(&stderr_buffer) < buffer_high &&
484 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100485 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000486 /*
487 * Read from stdin, unless we have seen EOF or have very much
488 * buffered data to send to the server.
489 */
490 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100491 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000492
493 /* Select stdout/stderr if have data in buffer. */
494 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100495 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000496 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100497 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000498 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000499 /* channel_prepare_select could have closed the last channel */
Damien Miller164a7f42001-10-12 11:36:09 +1000500 if (session_closed && !channel_still_open() &&
501 !packet_have_data_to_write()) {
502 /* clear mask since we did not call select() */
Damien Miller79faeff2001-11-12 11:06:32 +1100503 memset(*readsetp, 0, *nallocp);
504 memset(*writesetp, 0, *nallocp);
Damien Miller164a7f42001-10-12 11:36:09 +1000505 return;
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000506 } else {
507 FD_SET(connection_in, *readsetp);
508 }
Damien Miller1383bd82000-04-06 12:32:37 +1000509 }
510
Damien Miller95def091999-11-25 00:26:21 +1100511 /* Select server connection if have data to write to the server. */
512 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100513 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100514
Damien Miller0e220db2004-06-15 10:34:08 +1000515 if (control_fd != -1)
516 FD_SET(control_fd, *readsetp);
517
Damien Miller5428f641999-11-25 11:54:57 +1100518 /*
519 * Wait for something to happen. This will suspend the process until
520 * some selected descriptor can be read, written, or has some other
Damien Miller509b0102003-12-17 16:33:10 +1100521 * event pending.
Damien Miller5428f641999-11-25 11:54:57 +1100522 */
Damien Miller95def091999-11-25 00:26:21 +1100523
Damien Miller509b0102003-12-17 16:33:10 +1100524 if (options.server_alive_interval == 0 || !compat20)
525 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000526 else {
Damien Miller509b0102003-12-17 16:33:10 +1100527 tv.tv_sec = options.server_alive_interval;
528 tv.tv_usec = 0;
529 tvp = &tv;
530 }
531 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
532 if (ret < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100533 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000534
535 /*
536 * We have to clear the select masks, because we return.
537 * We have to return, because the mainloop checks for the flags
538 * set by the signal handlers.
539 */
Damien Miller79faeff2001-11-12 11:06:32 +1100540 memset(*readsetp, 0, *nallocp);
541 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000542
Damien Miller95def091999-11-25 00:26:21 +1100543 if (errno == EINTR)
544 return;
545 /* Note: we might still have data in the buffers. */
546 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
547 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100548 quit_pending = 1;
Damien Miller509b0102003-12-17 16:33:10 +1100549 } else if (ret == 0)
550 server_alive_check();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000551}
552
Ben Lindstrombba81212001-06-25 05:01:22 +0000553static void
Damien Millerad833b32000-08-23 10:46:23 +1000554client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000555{
Damien Miller95def091999-11-25 00:26:21 +1100556 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000557 if (buffer_len(bout) > 0)
Darren Tucker9f63f222003-07-03 13:46:56 +1000558 atomicio(vwrite, fileno(stdout), buffer_ptr(bout), buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000559 if (buffer_len(berr) > 0)
Darren Tucker9f63f222003-07-03 13:46:56 +1000560 atomicio(vwrite, fileno(stderr), buffer_ptr(berr), buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000561
Damien Miller95def091999-11-25 00:26:21 +1100562 leave_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000563
Damien Miller5428f641999-11-25 11:54:57 +1100564 /*
565 * Free (and clear) the buffer to reduce the amount of data that gets
566 * written to swap.
567 */
Damien Millerad833b32000-08-23 10:46:23 +1000568 buffer_free(bin);
569 buffer_free(bout);
570 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000571
Damien Miller95def091999-11-25 00:26:21 +1100572 /* Send the suspend signal to the program itself. */
573 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000574
Darren Tucker5d78de62004-11-05 20:35:44 +1100575 /* Reset window sizes in case they have changed */
576 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000577
Damien Miller95def091999-11-25 00:26:21 +1100578 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000579 buffer_init(bin);
580 buffer_init(bout);
581 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000582
Damien Miller95def091999-11-25 00:26:21 +1100583 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000584}
585
Ben Lindstrombba81212001-06-25 05:01:22 +0000586static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100587client_process_net_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000588{
Damien Miller1383bd82000-04-06 12:32:37 +1000589 int len;
590 char buf[8192];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000591
Damien Miller5428f641999-11-25 11:54:57 +1100592 /*
593 * Read input from the server, and add any such data to the buffer of
594 * the packet subsystem.
595 */
Damien Miller95def091999-11-25 00:26:21 +1100596 if (FD_ISSET(connection_in, readset)) {
597 /* Read as much as possible. */
598 len = read(connection_in, buf, sizeof(buf));
599 if (len == 0) {
600 /* Received EOF. The remote host has closed the connection. */
601 snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n",
602 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000603 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000604 quit_pending = 1;
605 return;
Damien Miller95def091999-11-25 00:26:21 +1100606 }
Damien Miller5428f641999-11-25 11:54:57 +1100607 /*
608 * There is a kernel bug on Solaris that causes select to
609 * sometimes wake up even though there is no data available.
610 */
Ben Lindstromebc88272001-03-06 03:34:40 +0000611 if (len < 0 && (errno == EAGAIN || errno == EINTR))
Damien Miller95def091999-11-25 00:26:21 +1100612 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000613
Damien Miller95def091999-11-25 00:26:21 +1100614 if (len < 0) {
615 /* An error has encountered. Perhaps there is a network problem. */
616 snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n",
617 host, strerror(errno));
618 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100619 quit_pending = 1;
620 return;
621 }
622 packet_process_incoming(buf, len);
623 }
Damien Miller1383bd82000-04-06 12:32:37 +1000624}
625
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000626static void
Damien Miller0e220db2004-06-15 10:34:08 +1000627client_subsystem_reply(int type, u_int32_t seq, void *ctxt)
628{
629 int id;
630 Channel *c;
Darren Tuckerfc959702004-07-17 16:12:08 +1000631
Damien Miller0e220db2004-06-15 10:34:08 +1000632 id = packet_get_int();
633 packet_check_eom();
634
635 if ((c = channel_lookup(id)) == NULL) {
636 error("%s: no channel for id %d", __func__, id);
637 return;
638 }
639
640 if (type == SSH2_MSG_CHANNEL_SUCCESS)
641 debug2("Request suceeded on channel %d", id);
642 else if (type == SSH2_MSG_CHANNEL_FAILURE) {
643 error("Request failed on channel %d", id);
644 channel_free(c);
645 }
646}
647
648static void
649client_extra_session2_setup(int id, void *arg)
650{
651 struct confirm_ctx *cctx = arg;
Damien Miller13390022005-07-06 09:44:19 +1000652 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +1000653 Channel *c;
Damien Miller3756dce2004-06-18 01:17:29 +1000654 int i;
Darren Tuckerfc959702004-07-17 16:12:08 +1000655
Damien Miller0e220db2004-06-15 10:34:08 +1000656 if (cctx == NULL)
657 fatal("%s: cctx == NULL", __func__);
658 if ((c = channel_lookup(id)) == NULL)
659 fatal("%s: no channel for id %d", __func__, id);
660
Damien Miller46d38de2005-07-17 17:02:09 +1000661 display = getenv("DISPLAY");
Damien Miller13390022005-07-06 09:44:19 +1000662 if (cctx->want_x_fwd && options.forward_x11 && display != NULL) {
663 char *proto, *data;
664 /* Get reasonable local authentication information. */
665 client_x11_get_proto(display, options.xauth_location,
666 options.forward_x11_trusted, &proto, &data);
667 /* Request forwarding with authentication spoofing. */
668 debug("Requesting X11 forwarding with authentication spoofing.");
669 x11_request_forwarding_with_spoofing(id, display, proto, data);
670 /* XXX wait for reply */
671 }
672
673 if (cctx->want_agent_fwd && options.forward_agent) {
674 debug("Requesting authentication agent forwarding.");
675 channel_request_start(id, "auth-agent-req@openssh.com", 0);
676 packet_send();
677 }
678
Darren Tuckerfc959702004-07-17 16:12:08 +1000679 client_session2_setup(id, cctx->want_tty, cctx->want_subsys,
Damien Miller3756dce2004-06-18 01:17:29 +1000680 cctx->term, &cctx->tio, c->rfd, &cctx->cmd, cctx->env,
Damien Miller0e220db2004-06-15 10:34:08 +1000681 client_subsystem_reply);
Damien Miller23f07702004-06-18 01:19:03 +1000682
Damien Miller0e220db2004-06-15 10:34:08 +1000683 c->confirm_ctx = NULL;
684 buffer_free(&cctx->cmd);
Damien Miller3756dce2004-06-18 01:17:29 +1000685 xfree(cctx->term);
686 if (cctx->env != NULL) {
687 for (i = 0; cctx->env[i] != NULL; i++)
688 xfree(cctx->env[i]);
689 xfree(cctx->env);
Darren Tuckerfc959702004-07-17 16:12:08 +1000690 }
Damien Miller3756dce2004-06-18 01:17:29 +1000691 xfree(cctx);
Damien Miller0e220db2004-06-15 10:34:08 +1000692}
693
694static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100695client_process_control(fd_set *readset)
Damien Miller0e220db2004-06-15 10:34:08 +1000696{
697 Buffer m;
698 Channel *c;
Damien Millereccb9de2005-06-17 12:59:34 +1000699 int client_fd, new_fd[3], ver, allowed;
Damien Miller0e220db2004-06-15 10:34:08 +1000700 socklen_t addrlen;
701 struct sockaddr_storage addr;
702 struct confirm_ctx *cctx;
703 char *cmd;
Damien Millereccb9de2005-06-17 12:59:34 +1000704 u_int i, len, env_len, command, flags;
Damien Miller0e220db2004-06-15 10:34:08 +1000705 uid_t euid;
706 gid_t egid;
707
708 /*
709 * Accept connection on control socket
710 */
711 if (control_fd == -1 || !FD_ISSET(control_fd, readset))
712 return;
713
714 memset(&addr, 0, sizeof(addr));
715 addrlen = sizeof(addr);
716 if ((client_fd = accept(control_fd,
717 (struct sockaddr*)&addr, &addrlen)) == -1) {
718 error("%s accept: %s", __func__, strerror(errno));
719 return;
720 }
721
722 if (getpeereid(client_fd, &euid, &egid) < 0) {
723 error("%s getpeereid failed: %s", __func__, strerror(errno));
724 close(client_fd);
725 return;
726 }
727 if ((euid != 0) && (getuid() != euid)) {
728 error("control mode uid mismatch: peer euid %u != uid %u",
729 (u_int) euid, (u_int) getuid());
730 close(client_fd);
731 return;
732 }
Damien Miller23f07702004-06-18 01:19:03 +1000733
Damien Miller0e220db2004-06-15 10:34:08 +1000734 unset_nonblock(client_fd);
735
Darren Tucker7ebfc102004-11-07 20:06:19 +1100736 /* Read command */
Damien Miller0e220db2004-06-15 10:34:08 +1000737 buffer_init(&m);
Darren Tucker7ebfc102004-11-07 20:06:19 +1100738 if (ssh_msg_recv(client_fd, &m) == -1) {
739 error("%s: client msg_recv failed", __func__);
740 close(client_fd);
741 buffer_free(&m);
742 return;
743 }
Damien Miller13390022005-07-06 09:44:19 +1000744 if ((ver = buffer_get_char(&m)) != SSHMUX_VER) {
Darren Tucker7ebfc102004-11-07 20:06:19 +1100745 error("%s: wrong client version %d", __func__, ver);
746 buffer_free(&m);
747 close(client_fd);
748 return;
749 }
Damien Miller0e220db2004-06-15 10:34:08 +1000750
Darren Tucker7ebfc102004-11-07 20:06:19 +1100751 allowed = 1;
752 command = buffer_get_int(&m);
753 flags = buffer_get_int(&m);
754
755 buffer_clear(&m);
756
757 switch (command) {
758 case SSHMUX_COMMAND_OPEN:
Damien Millerd14b1e72005-06-16 13:19:41 +1000759 if (options.control_master == SSHCTL_MASTER_ASK ||
760 options.control_master == SSHCTL_MASTER_AUTO_ASK)
Darren Tucker7ebfc102004-11-07 20:06:19 +1100761 allowed = ask_permission("Allow shared connection "
762 "to %s? ", host);
763 /* continue below */
764 break;
765 case SSHMUX_COMMAND_TERMINATE:
Damien Millerd14b1e72005-06-16 13:19:41 +1000766 if (options.control_master == SSHCTL_MASTER_ASK ||
767 options.control_master == SSHCTL_MASTER_AUTO_ASK)
Darren Tucker7ebfc102004-11-07 20:06:19 +1100768 allowed = ask_permission("Terminate shared connection "
769 "to %s? ", host);
770 if (allowed)
771 quit_pending = 1;
Darren Tucker47eede72005-03-14 23:08:12 +1100772 /* FALLTHROUGH */
Darren Tucker7ebfc102004-11-07 20:06:19 +1100773 case SSHMUX_COMMAND_ALIVE_CHECK:
774 /* Reply for SSHMUX_COMMAND_TERMINATE and ALIVE_CHECK */
775 buffer_clear(&m);
776 buffer_put_int(&m, allowed);
777 buffer_put_int(&m, getpid());
Damien Miller13390022005-07-06 09:44:19 +1000778 if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
Darren Tucker7ebfc102004-11-07 20:06:19 +1100779 error("%s: client msg_send failed", __func__);
780 close(client_fd);
781 buffer_free(&m);
782 return;
783 }
784 buffer_free(&m);
785 close(client_fd);
786 return;
787 default:
788 error("Unsupported command %d", command);
789 buffer_free(&m);
790 close(client_fd);
791 return;
792 }
793
794 /* Reply for SSHMUX_COMMAND_OPEN */
795 buffer_clear(&m);
Damien Miller23f07702004-06-18 01:19:03 +1000796 buffer_put_int(&m, allowed);
Damien Miller0e220db2004-06-15 10:34:08 +1000797 buffer_put_int(&m, getpid());
Damien Miller13390022005-07-06 09:44:19 +1000798 if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +1000799 error("%s: client msg_send failed", __func__);
800 close(client_fd);
Damien Miller23f07702004-06-18 01:19:03 +1000801 buffer_free(&m);
Damien Miller0e220db2004-06-15 10:34:08 +1000802 return;
803 }
Damien Miller0e220db2004-06-15 10:34:08 +1000804
Damien Miller23f07702004-06-18 01:19:03 +1000805 if (!allowed) {
806 error("Refused control connection");
807 close(client_fd);
808 buffer_free(&m);
809 return;
810 }
811
Darren Tucker7ebfc102004-11-07 20:06:19 +1100812 buffer_clear(&m);
Damien Miller0e220db2004-06-15 10:34:08 +1000813 if (ssh_msg_recv(client_fd, &m) == -1) {
814 error("%s: client msg_recv failed", __func__);
815 close(client_fd);
Damien Miller23f07702004-06-18 01:19:03 +1000816 buffer_free(&m);
Damien Miller0e220db2004-06-15 10:34:08 +1000817 return;
818 }
Damien Miller13390022005-07-06 09:44:19 +1000819 if ((ver = buffer_get_char(&m)) != SSHMUX_VER) {
Damien Miller0e220db2004-06-15 10:34:08 +1000820 error("%s: wrong client version %d", __func__, ver);
821 buffer_free(&m);
822 close(client_fd);
823 return;
824 }
825
Damien Miller07d86be2006-03-26 14:19:21 +1100826 cctx = xcalloc(1, sizeof(*cctx));
Darren Tucker7ebfc102004-11-07 20:06:19 +1100827 cctx->want_tty = (flags & SSHMUX_FLAG_TTY) != 0;
828 cctx->want_subsys = (flags & SSHMUX_FLAG_SUBSYS) != 0;
Damien Miller13390022005-07-06 09:44:19 +1000829 cctx->want_x_fwd = (flags & SSHMUX_FLAG_X11_FWD) != 0;
830 cctx->want_agent_fwd = (flags & SSHMUX_FLAG_AGENT_FWD) != 0;
Damien Miller0e220db2004-06-15 10:34:08 +1000831 cctx->term = buffer_get_string(&m, &len);
832
833 cmd = buffer_get_string(&m, &len);
834 buffer_init(&cctx->cmd);
835 buffer_append(&cctx->cmd, cmd, strlen(cmd));
836
Damien Miller3756dce2004-06-18 01:17:29 +1000837 env_len = buffer_get_int(&m);
838 env_len = MIN(env_len, 4096);
839 debug3("%s: receiving %d env vars", __func__, env_len);
840 if (env_len != 0) {
Damien Miller07d86be2006-03-26 14:19:21 +1100841 cctx->env = xcalloc(env_len + 1, sizeof(*cctx->env));
Damien Miller3756dce2004-06-18 01:17:29 +1000842 for (i = 0; i < env_len; i++)
843 cctx->env[i] = buffer_get_string(&m, &len);
844 cctx->env[i] = NULL;
845 }
846
Damien Miller0e220db2004-06-15 10:34:08 +1000847 debug2("%s: accepted tty %d, subsys %d, cmd %s", __func__,
848 cctx->want_tty, cctx->want_subsys, cmd);
Damien Miller24fd8dd2006-06-13 13:00:09 +1000849 xfree(cmd);
Damien Miller0e220db2004-06-15 10:34:08 +1000850
851 /* Gather fds from client */
852 new_fd[0] = mm_receive_fd(client_fd);
853 new_fd[1] = mm_receive_fd(client_fd);
854 new_fd[2] = mm_receive_fd(client_fd);
855
856 debug2("%s: got fds stdin %d, stdout %d, stderr %d", __func__,
857 new_fd[0], new_fd[1], new_fd[2]);
858
859 /* Try to pick up ttymodes from client before it goes raw */
860 if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1)
861 error("%s: tcgetattr: %s", __func__, strerror(errno));
862
Darren Tucker7ebfc102004-11-07 20:06:19 +1100863 /* This roundtrip is just for synchronisation of ttymodes */
Damien Miller0e220db2004-06-15 10:34:08 +1000864 buffer_clear(&m);
Damien Miller13390022005-07-06 09:44:19 +1000865 if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +1000866 error("%s: client msg_send failed", __func__);
867 close(client_fd);
868 close(new_fd[0]);
869 close(new_fd[1]);
870 close(new_fd[2]);
Damien Miller23f07702004-06-18 01:19:03 +1000871 buffer_free(&m);
Darren Tucker7ebfc102004-11-07 20:06:19 +1100872 xfree(cctx->term);
873 if (env_len != 0) {
874 for (i = 0; i < env_len; i++)
875 xfree(cctx->env[i]);
876 xfree(cctx->env);
877 }
Damien Miller0e220db2004-06-15 10:34:08 +1000878 return;
879 }
880 buffer_free(&m);
881
882 /* enable nonblocking unless tty */
883 if (!isatty(new_fd[0]))
884 set_nonblock(new_fd[0]);
885 if (!isatty(new_fd[1]))
886 set_nonblock(new_fd[1]);
887 if (!isatty(new_fd[2]))
888 set_nonblock(new_fd[2]);
889
890 set_nonblock(client_fd);
891
Darren Tuckerfc959702004-07-17 16:12:08 +1000892 c = channel_new("session", SSH_CHANNEL_OPENING,
Damien Miller0e220db2004-06-15 10:34:08 +1000893 new_fd[0], new_fd[1], new_fd[2],
894 CHAN_SES_WINDOW_DEFAULT, CHAN_SES_PACKET_DEFAULT,
895 CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0);
896
897 /* XXX */
898 c->ctl_fd = client_fd;
899
900 debug3("%s: channel_new: %d", __func__, c->self);
901
902 channel_send_open(c->self);
903 channel_register_confirm(c->self, client_extra_session2_setup, cctx);
904}
905
906static void
Ben Lindstrom681d9322002-03-22 03:53:00 +0000907process_cmdline(void)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000908{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000909 void (*handler)(int);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100910 char *s, *cmd, *cancel_host;
Darren Tuckere7066df2004-05-24 10:18:05 +1000911 int delete = 0;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000912 int local = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100913 u_short cancel_port;
914 Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000915
916 leave_raw_mode();
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000917 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000918 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000919 if (s == NULL)
920 goto out;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000921 while (*s && isspace(*s))
922 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000923 if (*s == '-')
924 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000925 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000926 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000927
Darren Tucker1973c882004-05-24 10:34:36 +1000928 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000929 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000930 logit(" -L[bind_address:]port:host:hostport "
931 "Request local forward");
932 logit(" -R[bind_address:]port:host:hostport "
933 "Request remote forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000934 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000935 "Cancel remote forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100936 if (!options.permit_local_command)
937 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000938 logit(" !args "
939 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100940 goto out;
941 }
942
943 if (*s == '!' && options.permit_local_command) {
944 s++;
945 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000946 goto out;
947 }
948
949 if (*s == 'K') {
950 delete = 1;
951 s++;
952 }
953 if (*s != 'L' && *s != 'R') {
Damien Miller996acd22003-04-09 20:59:48 +1000954 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000955 goto out;
956 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000957 if (*s == 'L')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000958 local = 1;
Darren Tuckere7066df2004-05-24 10:18:05 +1000959 if (local && delete) {
960 logit("Not supported.");
961 goto out;
962 }
963 if ((!local || delete) && !compat20) {
Damien Miller996acd22003-04-09 20:59:48 +1000964 logit("Not supported for SSH protocol version 1.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000965 goto out;
966 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000967
968 s++;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000969 while (*s && isspace(*s))
970 s++;
971
Darren Tuckere7066df2004-05-24 10:18:05 +1000972 if (delete) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100973 cancel_port = 0;
974 cancel_host = hpdelim(&s); /* may be NULL */
975 if (s != NULL) {
976 cancel_port = a2port(s);
977 cancel_host = cleanhostname(cancel_host);
978 } else {
979 cancel_port = a2port(cancel_host);
980 cancel_host = NULL;
981 }
982 if (cancel_port == 0) {
983 logit("Bad forwarding close port");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000984 goto out;
985 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100986 channel_request_rforward_cancel(cancel_host, cancel_port);
Darren Tuckere7066df2004-05-24 10:18:05 +1000987 } else {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100988 if (!parse_forward(&fwd, s)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000989 logit("Bad forwarding specification.");
990 goto out;
991 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000992 if (local) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100993 if (channel_setup_local_fwd_listener(fwd.listen_host,
994 fwd.listen_port, fwd.connect_host,
995 fwd.connect_port, options.gateway_ports) < 0) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000996 logit("Port forwarding failed.");
997 goto out;
998 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100999 } else {
Darren Tuckere7d4b192006-07-12 22:17:10 +10001000 if (channel_request_remote_forwarding(fwd.listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +11001001 fwd.listen_port, fwd.connect_host,
Darren Tuckere7d4b192006-07-12 22:17:10 +10001002 fwd.connect_port) < 0) {
1003 logit("Port forwarding failed.");
1004 goto out;
1005 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001006 }
1007
Darren Tuckere7066df2004-05-24 10:18:05 +10001008 logit("Forwarding port.");
1009 }
1010
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001011out:
1012 signal(SIGINT, handler);
1013 enter_raw_mode();
1014 if (cmd)
1015 xfree(cmd);
1016}
1017
Damien Millerad833b32000-08-23 10:46:23 +10001018/* process the characters one by one */
Ben Lindstrombba81212001-06-25 05:01:22 +00001019static int
Damien Millerad833b32000-08-23 10:46:23 +10001020process_escapes(Buffer *bin, Buffer *bout, Buffer *berr, char *buf, int len)
1021{
1022 char string[1024];
1023 pid_t pid;
1024 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001025 u_int i;
1026 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +10001027 char *s;
1028
Damien Millereccb9de2005-06-17 12:59:34 +10001029 if (len <= 0)
1030 return (0);
1031
1032 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +10001033 /* Get one character at a time. */
1034 ch = buf[i];
1035
1036 if (escape_pending) {
1037 /* We have previously seen an escape character. */
1038 /* Clear the flag now. */
1039 escape_pending = 0;
1040
1041 /* Process the escaped character. */
1042 switch (ch) {
1043 case '.':
1044 /* Terminate the connection. */
1045 snprintf(string, sizeof string, "%c.\r\n", escape_char);
1046 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001047
1048 quit_pending = 1;
1049 return -1;
1050
1051 case 'Z' - 64:
1052 /* Suspend the program. */
1053 /* Print a message to that effect to the user. */
1054 snprintf(string, sizeof string, "%c^Z [suspend ssh]\r\n", escape_char);
1055 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001056
1057 /* Restore terminal modes and suspend. */
1058 client_suspend_self(bin, bout, berr);
1059
1060 /* We have been continued. */
1061 continue;
1062
Damien Miller54c45982003-05-15 10:20:13 +10001063 case 'B':
1064 if (compat20) {
1065 snprintf(string, sizeof string,
1066 "%cB\r\n", escape_char);
1067 buffer_append(berr, string,
1068 strlen(string));
1069 channel_request_start(session_ident,
1070 "break", 0);
1071 packet_put_int(1000);
1072 packet_send();
1073 }
1074 continue;
1075
Ben Lindstromf28f6342001-04-04 02:03:04 +00001076 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001077 if (compat20) {
1078 if (datafellows & SSH_BUG_NOREKEY)
Damien Miller996acd22003-04-09 20:59:48 +10001079 logit("Server does not support re-keying");
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001080 else
1081 need_rekeying = 1;
1082 }
Ben Lindstromf28f6342001-04-04 02:03:04 +00001083 continue;
1084
Damien Millerad833b32000-08-23 10:46:23 +10001085 case '&':
Damien Millerad833b32000-08-23 10:46:23 +10001086 /*
1087 * Detach the program (continue to serve connections,
1088 * but put in background and no more new connections).
1089 */
Damien Miller96507ef2001-11-12 10:52:25 +11001090 /* Restore tty modes. */
1091 leave_raw_mode();
1092
1093 /* Stop listening for new connections. */
1094 channel_stop_listening();
1095
1096 snprintf(string, sizeof string,
1097 "%c& [backgrounded]\n", escape_char);
1098 buffer_append(berr, string, strlen(string));
1099
1100 /* Fork into background. */
1101 pid = fork();
1102 if (pid < 0) {
1103 error("fork: %.100s", strerror(errno));
1104 continue;
1105 }
1106 if (pid != 0) { /* This is the parent. */
1107 /* The parent just exits. */
1108 exit(0);
1109 }
1110 /* The child continues serving connections. */
1111 if (compat20) {
1112 buffer_append(bin, "\004", 1);
1113 /* fake EOF on stdin */
1114 return -1;
1115 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +10001116 /*
1117 * Sending SSH_CMSG_EOF alone does not always appear
1118 * to be enough. So we try to send an EOF character
1119 * first.
1120 */
1121 packet_start(SSH_CMSG_STDIN_DATA);
1122 packet_put_string("\004", 1);
1123 packet_send();
1124 /* Close stdin. */
1125 stdin_eof = 1;
1126 if (buffer_len(bin) == 0) {
1127 packet_start(SSH_CMSG_EOF);
1128 packet_send();
1129 }
1130 }
Damien Miller96507ef2001-11-12 10:52:25 +11001131 continue;
Damien Millerad833b32000-08-23 10:46:23 +10001132
1133 case '?':
1134 snprintf(string, sizeof string,
1135"%c?\r\n\
1136Supported escape sequences:\r\n\
Damien Miller06692862002-09-04 16:32:10 +10001137%c. - terminate connection\r\n\
Damien Miller54c45982003-05-15 10:20:13 +10001138%cB - send a BREAK to the remote system\r\n\
Damien Miller06692862002-09-04 16:32:10 +10001139%cC - open a command line\r\n\
1140%cR - Request rekey (SSH protocol 2 only)\r\n\
1141%c^Z - suspend ssh\r\n\
1142%c# - list forwarded connections\r\n\
1143%c& - background ssh (when waiting for connections to terminate)\r\n\
1144%c? - this message\r\n\
1145%c%c - send the escape character by typing it twice\r\n\
Damien Millerad833b32000-08-23 10:46:23 +10001146(Note that escapes are only recognized immediately after newline.)\r\n",
Damien Miller06692862002-09-04 16:32:10 +10001147 escape_char, escape_char, escape_char, escape_char,
1148 escape_char, escape_char, escape_char, escape_char,
Damien Miller54c45982003-05-15 10:20:13 +10001149 escape_char, escape_char, escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001150 buffer_append(berr, string, strlen(string));
1151 continue;
1152
1153 case '#':
1154 snprintf(string, sizeof string, "%c#\r\n", escape_char);
1155 buffer_append(berr, string, strlen(string));
1156 s = channel_open_message();
1157 buffer_append(berr, s, strlen(s));
1158 xfree(s);
1159 continue;
1160
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001161 case 'C':
Ben Lindstrom681d9322002-03-22 03:53:00 +00001162 process_cmdline();
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001163 continue;
1164
Damien Millerad833b32000-08-23 10:46:23 +10001165 default:
1166 if (ch != escape_char) {
1167 buffer_put_char(bin, escape_char);
1168 bytes++;
1169 }
1170 /* Escaped characters fall through here */
1171 break;
1172 }
1173 } else {
1174 /*
1175 * The previous character was not an escape char. Check if this
1176 * is an escape.
1177 */
1178 if (last_was_cr && ch == escape_char) {
1179 /* It is. Set the flag and continue to next character. */
1180 escape_pending = 1;
1181 continue;
1182 }
1183 }
1184
1185 /*
1186 * Normal character. Record whether it was a newline,
1187 * and append it to the buffer.
1188 */
1189 last_was_cr = (ch == '\r' || ch == '\n');
1190 buffer_put_char(bin, ch);
1191 bytes++;
1192 }
1193 return bytes;
1194}
1195
Ben Lindstrombba81212001-06-25 05:01:22 +00001196static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001197client_process_input(fd_set *readset)
Damien Miller1383bd82000-04-06 12:32:37 +10001198{
Damien Miller166fca82000-04-20 07:42:21 +10001199 int len;
Damien Millerad833b32000-08-23 10:46:23 +10001200 char buf[8192];
Damien Miller1383bd82000-04-06 12:32:37 +10001201
Damien Miller95def091999-11-25 00:26:21 +11001202 /* Read input from stdin. */
1203 if (FD_ISSET(fileno(stdin), readset)) {
1204 /* Read as much as possible. */
1205 len = read(fileno(stdin), buf, sizeof(buf));
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001206 if (len < 0 && (errno == EAGAIN || errno == EINTR))
1207 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +11001208 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001209 /*
1210 * Received EOF or error. They are treated
1211 * similarly, except that an error message is printed
1212 * if it was an error condition.
1213 */
Damien Miller95def091999-11-25 00:26:21 +11001214 if (len < 0) {
1215 snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno));
1216 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001217 }
1218 /* Mark that we have seen EOF. */
1219 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +11001220 /*
1221 * Send an EOF message to the server unless there is
1222 * data in the buffer. If there is data in the
1223 * buffer, no message will be sent now. Code
1224 * elsewhere will send the EOF when the buffer
1225 * becomes empty if stdin_eof is set.
1226 */
Damien Miller95def091999-11-25 00:26:21 +11001227 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001228 packet_start(SSH_CMSG_EOF);
1229 packet_send();
Damien Miller95def091999-11-25 00:26:21 +11001230 }
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001231 } else if (escape_char == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +11001232 /*
1233 * Normal successful read, and no escape character.
1234 * Just append the data to buffer.
1235 */
Damien Miller95def091999-11-25 00:26:21 +11001236 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +11001237 } else {
Damien Miller5428f641999-11-25 11:54:57 +11001238 /*
1239 * Normal, successful read. But we have an escape character
1240 * and have to process the characters one by one.
1241 */
Ben Lindstrome9613cf2001-03-05 06:14:02 +00001242 if (process_escapes(&stdin_buffer, &stdout_buffer,
1243 &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +10001244 return;
Damien Miller95def091999-11-25 00:26:21 +11001245 }
1246 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001247}
1248
Ben Lindstrombba81212001-06-25 05:01:22 +00001249static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001250client_process_output(fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001251{
Damien Miller95def091999-11-25 00:26:21 +11001252 int len;
1253 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001254
Damien Miller95def091999-11-25 00:26:21 +11001255 /* Write buffered output to stdout. */
1256 if (FD_ISSET(fileno(stdout), writeset)) {
1257 /* Write as much data as possible. */
1258 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001259 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001260 if (len <= 0) {
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001261 if (errno == EINTR || errno == EAGAIN)
Damien Miller95def091999-11-25 00:26:21 +11001262 len = 0;
1263 else {
Damien Miller5428f641999-11-25 11:54:57 +11001264 /*
1265 * An error or EOF was encountered. Put an
1266 * error message to stderr buffer.
1267 */
Damien Miller95def091999-11-25 00:26:21 +11001268 snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno));
1269 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001270 quit_pending = 1;
1271 return;
1272 }
1273 }
1274 /* Consume printed data from the buffer. */
1275 buffer_consume(&stdout_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001276 stdout_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001277 }
Damien Miller95def091999-11-25 00:26:21 +11001278 /* Write buffered output to stderr. */
1279 if (FD_ISSET(fileno(stderr), writeset)) {
1280 /* Write as much data as possible. */
1281 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001282 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001283 if (len <= 0) {
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001284 if (errno == EINTR || errno == EAGAIN)
Damien Miller95def091999-11-25 00:26:21 +11001285 len = 0;
1286 else {
Damien Miller5428f641999-11-25 11:54:57 +11001287 /* EOF or error, but can't even print error message. */
Damien Miller95def091999-11-25 00:26:21 +11001288 quit_pending = 1;
1289 return;
1290 }
1291 }
1292 /* Consume printed characters from the buffer. */
1293 buffer_consume(&stderr_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001294 stderr_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001295 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001296}
1297
Damien Miller5428f641999-11-25 11:54:57 +11001298/*
Damien Millerb38eff82000-04-01 11:09:21 +10001299 * Get packets from the connection input buffer, and process them as long as
1300 * there are packets available.
1301 *
1302 * Any unknown packets received during the actual
1303 * session cause the session to terminate. This is
1304 * intended to make debugging easier since no
1305 * confirmations are sent. Any compatible protocol
1306 * extensions must be negotiated during the
1307 * preparatory phase.
1308 */
1309
Ben Lindstrombba81212001-06-25 05:01:22 +00001310static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001311client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001312{
Ben Lindstromf28f6342001-04-04 02:03:04 +00001313 dispatch_run(DISPATCH_NONBLOCK, &quit_pending, compat20 ? xxx_kex : NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001314}
1315
Damien Millerad833b32000-08-23 10:46:23 +10001316/* scan buf[] for '~' before sending data to the peer */
1317
Ben Lindstrombba81212001-06-25 05:01:22 +00001318static int
Damien Millerad833b32000-08-23 10:46:23 +10001319simple_escape_filter(Channel *c, char *buf, int len)
1320{
1321 /* XXX we assume c->extended is writeable */
1322 return process_escapes(&c->input, &c->output, &c->extended, buf, len);
1323}
1324
Ben Lindstrombba81212001-06-25 05:01:22 +00001325static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001326client_channel_closed(int id, void *arg)
1327{
Damien Miller3ec27592001-10-12 11:35:04 +10001328 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001329 session_closed = 1;
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001330 leave_raw_mode();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001331}
1332
Damien Millerb38eff82000-04-01 11:09:21 +10001333/*
Damien Miller5428f641999-11-25 11:54:57 +11001334 * Implements the interactive session with the server. This is called after
1335 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001336 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1337 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001338 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001339
Damien Miller4af51302000-04-16 11:18:38 +10001340int
Damien Millerad833b32000-08-23 10:46:23 +10001341client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001342{
Damien Miller5e953212001-01-30 09:14:00 +11001343 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001344 double start_time, total_time;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001345 int max_fd = 0, max_fd2 = 0, len, rekeying = 0;
1346 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001347 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001348
Damien Miller95def091999-11-25 00:26:21 +11001349 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001350
Damien Miller95def091999-11-25 00:26:21 +11001351 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001352
Damien Miller95def091999-11-25 00:26:21 +11001353 /* Initialize variables. */
1354 escape_pending = 0;
1355 last_was_cr = 1;
1356 exit_status = -1;
1357 stdin_eof = 0;
1358 buffer_high = 64 * 1024;
1359 connection_in = packet_get_connection_in();
1360 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +11001361 max_fd = MAX(connection_in, connection_out);
Damien Miller0e220db2004-06-15 10:34:08 +10001362 if (control_fd != -1)
1363 max_fd = MAX(max_fd, control_fd);
Damien Miller5e953212001-01-30 09:14:00 +11001364
1365 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +00001366 /* enable nonblocking unless tty */
1367 if (!isatty(fileno(stdin)))
1368 set_nonblock(fileno(stdin));
1369 if (!isatty(fileno(stdout)))
1370 set_nonblock(fileno(stdout));
1371 if (!isatty(fileno(stderr)))
1372 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +11001373 max_fd = MAX(max_fd, fileno(stdin));
1374 max_fd = MAX(max_fd, fileno(stdout));
1375 max_fd = MAX(max_fd, fileno(stderr));
1376 }
Damien Miller95def091999-11-25 00:26:21 +11001377 stdin_bytes = 0;
1378 stdout_bytes = 0;
1379 stderr_bytes = 0;
1380 quit_pending = 0;
1381 escape_char = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001382
Damien Miller95def091999-11-25 00:26:21 +11001383 /* Initialize buffers. */
1384 buffer_init(&stdin_buffer);
1385 buffer_init(&stdout_buffer);
1386 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001387
Damien Millerb38eff82000-04-01 11:09:21 +10001388 client_init_dispatch();
1389
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001390 /*
1391 * Set signal handlers, (e.g. to restore non-blocking mode)
1392 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1393 */
Darren Tucker07336da2004-11-05 20:02:16 +11001394 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1395 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001396 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1397 signal(SIGINT, signal_handler);
1398 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1399 signal(SIGQUIT, signal_handler);
1400 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1401 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001402 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001403
Damien Miller95def091999-11-25 00:26:21 +11001404 if (have_pty)
1405 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001406
Ben Lindstrom5b828322001-02-09 01:34:36 +00001407 if (compat20) {
1408 session_ident = ssh2_chan_id;
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001409 if (escape_char != SSH_ESCAPECHAR_NONE)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001410 channel_register_filter(session_ident,
Damien Miller077b2382005-12-31 16:22:32 +11001411 simple_escape_filter, NULL);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001412 if (session_ident != -1)
1413 channel_register_cleanup(session_ident,
Damien Miller39eda6e2005-11-05 14:52:50 +11001414 client_channel_closed, 0);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001415 } else {
1416 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +10001417 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001418 }
Damien Millerad833b32000-08-23 10:46:23 +10001419
Damien Miller95def091999-11-25 00:26:21 +11001420 /* Main loop of the client for the interactive session mode. */
1421 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001422
Damien Miller5428f641999-11-25 11:54:57 +11001423 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001424 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001425
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001426 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +10001427 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001428
1429 rekeying = (xxx_kex != NULL && !xxx_kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +10001430
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001431 if (rekeying) {
1432 debug("rekeying in progress");
1433 } else {
1434 /*
1435 * Make packets of buffered stdin data, and buffer
1436 * them for sending to the server.
1437 */
1438 if (!compat20)
1439 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001440
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001441 /*
1442 * Make packets from buffered channel data, and
1443 * enqueue them for sending to the server.
1444 */
1445 if (packet_not_very_much_data_to_write())
1446 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001447
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001448 /*
1449 * Check if the window size has changed, and buffer a
1450 * message about it to the server if so.
1451 */
1452 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001453
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001454 if (quit_pending)
1455 break;
1456 }
Damien Miller5428f641999-11-25 11:54:57 +11001457 /*
1458 * Wait until we have something to do (something becomes
1459 * available on one of the descriptors).
1460 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001461 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001462 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001463 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001464
Damien Miller95def091999-11-25 00:26:21 +11001465 if (quit_pending)
1466 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001467
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001468 /* Do channel operations unless rekeying in progress. */
1469 if (!rekeying) {
1470 channel_after_select(readset, writeset);
Damien Millera5539d22003-04-09 20:50:06 +10001471 if (need_rekeying || packet_need_rekeying()) {
1472 debug("need rekeying");
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001473 xxx_kex->done = 0;
1474 kex_send_kexinit(xxx_kex);
1475 need_rekeying = 0;
1476 }
1477 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001478
Damien Miller1383bd82000-04-06 12:32:37 +10001479 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001480 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001481
Damien Miller0e220db2004-06-15 10:34:08 +10001482 /* Accept control connections. */
1483 client_process_control(readset);
1484
Damien Miller1383bd82000-04-06 12:32:37 +10001485 if (quit_pending)
1486 break;
1487
1488 if (!compat20) {
1489 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +11001490 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +10001491 /*
1492 * Process output to stdout and stderr. Output to
1493 * the connection is processed elsewhere (above).
1494 */
Damien Miller5e953212001-01-30 09:14:00 +11001495 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +10001496 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001497
Damien Miller5428f641999-11-25 11:54:57 +11001498 /* Send as much buffered packet data as possible to the sender. */
Damien Miller5e953212001-01-30 09:14:00 +11001499 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001500 packet_write_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001501 }
Damien Miller5e953212001-01-30 09:14:00 +11001502 if (readset)
1503 xfree(readset);
1504 if (writeset)
1505 xfree(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001506
Damien Miller95def091999-11-25 00:26:21 +11001507 /* Terminate the session. */
1508
1509 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001510 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001511
Ben Lindstrom601e4362001-06-21 03:19:23 +00001512 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +11001513
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001514 if (have_pty)
1515 leave_raw_mode();
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001516
1517 /* restore blocking io */
1518 if (!isatty(fileno(stdin)))
1519 unset_nonblock(fileno(stdin));
1520 if (!isatty(fileno(stdout)))
1521 unset_nonblock(fileno(stdout));
1522 if (!isatty(fileno(stderr)))
1523 unset_nonblock(fileno(stderr));
1524
Damien Millerd6965512003-12-17 16:31:53 +11001525 /*
1526 * If there was no shell or command requested, there will be no remote
1527 * exit status to be returned. In that case, clear error code if the
1528 * connection was deliberately terminated at this end.
1529 */
1530 if (no_shell_flag && received_signal == SIGTERM) {
1531 received_signal = 0;
1532 exit_status = 0;
1533 }
1534
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001535 if (received_signal)
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001536 fatal("Killed by signal %d.", (int) received_signal);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001537
1538 /*
1539 * In interactive mode (with pseudo tty) display a message indicating
1540 * that the connection has been closed.
1541 */
1542 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
1543 snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host);
1544 buffer_append(&stderr_buffer, buf, strlen(buf));
1545 }
1546
Damien Miller95def091999-11-25 00:26:21 +11001547 /* Output any buffered data for stdout. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001548 while (buffer_len(&stdout_buffer) > 0) {
1549 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001550 buffer_len(&stdout_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001551 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001552 error("Write failed flushing stdout buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001553 break;
1554 }
Damien Miller95def091999-11-25 00:26:21 +11001555 buffer_consume(&stdout_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001556 stdout_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001557 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001558
Damien Miller95def091999-11-25 00:26:21 +11001559 /* Output any buffered data for stderr. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001560 while (buffer_len(&stderr_buffer) > 0) {
1561 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001562 buffer_len(&stderr_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001563 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001564 error("Write failed flushing stderr buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001565 break;
1566 }
Damien Miller95def091999-11-25 00:26:21 +11001567 buffer_consume(&stderr_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001568 stderr_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001569 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001570
Damien Miller95def091999-11-25 00:26:21 +11001571 /* Clear and free any buffers. */
1572 memset(buf, 0, sizeof(buf));
1573 buffer_free(&stdin_buffer);
1574 buffer_free(&stdout_buffer);
1575 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001576
Damien Miller95def091999-11-25 00:26:21 +11001577 /* Report bytes transferred, and transfer rates. */
1578 total_time = get_current_time() - start_time;
1579 debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001580 stdin_bytes, stdout_bytes, stderr_bytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001581 if (total_time > 0)
1582 debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001583 stdin_bytes / total_time, stdout_bytes / total_time,
1584 stderr_bytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001585
1586 /* Return the exit status of the program. */
1587 debug("Exit status %d", exit_status);
1588 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001589}
Damien Millerb38eff82000-04-01 11:09:21 +10001590
1591/*********/
1592
Ben Lindstrombba81212001-06-25 05:01:22 +00001593static void
Damien Miller630d6f42002-01-22 23:17:30 +11001594client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001595{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001596 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001597 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001598 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001599 buffer_append(&stdout_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001600 memset(data, 0, data_len);
1601 xfree(data);
1602}
Ben Lindstrombba81212001-06-25 05:01:22 +00001603static void
Damien Miller630d6f42002-01-22 23:17:30 +11001604client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001605{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001606 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001607 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001608 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001609 buffer_append(&stderr_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001610 memset(data, 0, data_len);
1611 xfree(data);
1612}
Ben Lindstrombba81212001-06-25 05:01:22 +00001613static void
Damien Miller630d6f42002-01-22 23:17:30 +11001614client_input_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001615{
Damien Millerb38eff82000-04-01 11:09:21 +10001616 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001617 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001618 /* Acknowledge the exit. */
1619 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1620 packet_send();
1621 /*
1622 * Must wait for packet to be sent since we are
1623 * exiting the loop.
1624 */
1625 packet_write_wait();
1626 /* Flag that we want to exit. */
1627 quit_pending = 1;
1628}
Darren Tucker5dcdd212003-10-02 16:17:00 +10001629static void
1630client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1631{
1632 Channel *c = NULL;
1633 int remote_id, sock;
1634
1635 /* Read the remote channel number from the message. */
1636 remote_id = packet_get_int();
1637 packet_check_eom();
1638
1639 /*
1640 * Get a connection to the local authentication agent (this may again
1641 * get forwarded).
1642 */
1643 sock = ssh_get_authentication_socket();
1644
1645 /*
1646 * If we could not connect the agent, send an error message back to
1647 * the server. This should never happen unless the agent dies,
1648 * because authentication forwarding is only enabled if we have an
1649 * agent.
1650 */
1651 if (sock >= 0) {
1652 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1653 -1, 0, 0, 0, "authentication agent connection", 1);
1654 c->remote_id = remote_id;
1655 c->force_drain = 1;
1656 }
1657 if (c == NULL) {
1658 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1659 packet_put_int(remote_id);
1660 } else {
1661 /* Send a confirmation to the remote host. */
1662 debug("Forwarding authentication connection.");
1663 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1664 packet_put_int(remote_id);
1665 packet_put_int(c->self);
1666 }
1667 packet_send();
1668}
Damien Millerb38eff82000-04-01 11:09:21 +10001669
Ben Lindstrombba81212001-06-25 05:01:22 +00001670static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001671client_request_forwarded_tcpip(const char *request_type, int rchan)
1672{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001673 Channel *c = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001674 char *listen_address, *originator_address;
1675 int listen_port, originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001676 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001677
1678 /* Get rest of the packet */
1679 listen_address = packet_get_string(NULL);
1680 listen_port = packet_get_int();
1681 originator_address = packet_get_string(NULL);
1682 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001683 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001684
1685 debug("client_request_forwarded_tcpip: listen %s port %d, originator %s port %d",
1686 listen_address, listen_port, originator_address, originator_port);
1687
Ben Lindstrom173e6462001-07-04 05:15:15 +00001688 sock = channel_connect_by_listen_address(listen_port);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001689 if (sock < 0) {
1690 xfree(originator_address);
1691 xfree(listen_address);
1692 return NULL;
1693 }
1694 c = channel_new("forwarded-tcpip",
1695 SSH_CHANNEL_CONNECTING, sock, sock, -1,
1696 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001697 originator_address, 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001698 xfree(originator_address);
1699 xfree(listen_address);
1700 return c;
1701}
1702
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001703static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001704client_request_x11(const char *request_type, int rchan)
1705{
1706 Channel *c = NULL;
1707 char *originator;
1708 int originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001709 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001710
1711 if (!options.forward_x11) {
1712 error("Warning: ssh server tried X11 forwarding.");
Damien Miller5eb137c2005-12-31 16:19:53 +11001713 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001714 return NULL;
1715 }
1716 originator = packet_get_string(NULL);
1717 if (datafellows & SSH_BUG_X11FWD) {
1718 debug2("buggy server: x11 request w/o originator_port");
1719 originator_port = 0;
1720 } else {
1721 originator_port = packet_get_int();
1722 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001723 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001724 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001725 debug("client_request_x11: request from %s %d", originator,
1726 originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001727 xfree(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001728 sock = x11_connect_display();
1729 if (sock < 0)
1730 return NULL;
1731 c = channel_new("x11",
1732 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001733 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001734 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001735 return c;
1736}
1737
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001738static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001739client_request_agent(const char *request_type, int rchan)
1740{
1741 Channel *c = NULL;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001742 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001743
1744 if (!options.forward_agent) {
1745 error("Warning: ssh server tried agent forwarding.");
Damien Miller5eb137c2005-12-31 16:19:53 +11001746 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001747 return NULL;
1748 }
1749 sock = ssh_get_authentication_socket();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001750 if (sock < 0)
1751 return NULL;
1752 c = channel_new("authentication agent connection",
1753 SSH_CHANNEL_OPEN, sock, sock, -1,
1754 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001755 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001756 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001757 return c;
1758}
1759
Damien Millerbd483e72000-04-30 10:00:53 +10001760/* XXXX move to generic input handler */
Ben Lindstrombba81212001-06-25 05:01:22 +00001761static void
Damien Miller630d6f42002-01-22 23:17:30 +11001762client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001763{
1764 Channel *c = NULL;
1765 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001766 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001767 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001768
1769 ctype = packet_get_string(&len);
1770 rchan = packet_get_int();
1771 rwindow = packet_get_int();
1772 rmaxpack = packet_get_int();
1773
Damien Millere247cc42000-05-07 12:03:14 +10001774 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001775 ctype, rchan, rwindow, rmaxpack);
1776
Damien Miller0bc1bd82000-11-13 22:57:25 +11001777 if (strcmp(ctype, "forwarded-tcpip") == 0) {
1778 c = client_request_forwarded_tcpip(ctype, rchan);
1779 } else if (strcmp(ctype, "x11") == 0) {
1780 c = client_request_x11(ctype, rchan);
1781 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1782 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001783 }
1784/* XXX duplicate : */
1785 if (c != NULL) {
1786 debug("confirm %s", ctype);
1787 c->remote_id = rchan;
1788 c->remote_window = rwindow;
1789 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001790 if (c->type != SSH_CHANNEL_CONNECTING) {
1791 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1792 packet_put_int(c->remote_id);
1793 packet_put_int(c->self);
1794 packet_put_int(c->local_window);
1795 packet_put_int(c->local_maxpacket);
1796 packet_send();
1797 }
Damien Millerbd483e72000-04-30 10:00:53 +10001798 } else {
1799 debug("failure %s", ctype);
1800 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1801 packet_put_int(rchan);
1802 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00001803 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001804 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00001805 packet_put_cstring("");
1806 }
Damien Millerbd483e72000-04-30 10:00:53 +10001807 packet_send();
1808 }
1809 xfree(ctype);
1810}
Ben Lindstrombba81212001-06-25 05:01:22 +00001811static void
Damien Miller630d6f42002-01-22 23:17:30 +11001812client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001813{
1814 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001815 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001816 char *rtype;
1817
1818 id = packet_get_int();
1819 rtype = packet_get_string(NULL);
1820 reply = packet_get_char();
1821
1822 debug("client_input_channel_req: channel %d rtype %s reply %d",
1823 id, rtype, reply);
1824
Damien Miller3bbd8782004-06-18 22:23:22 +10001825 if (id == -1) {
1826 error("client_input_channel_req: request for channel -1");
1827 } else if ((c = channel_lookup(id)) == NULL) {
Ben Lindstrom5b828322001-02-09 01:34:36 +00001828 error("client_input_channel_req: channel %d: unknown channel", id);
1829 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10001830 exitval = packet_get_int();
1831 if (id == session_ident) {
1832 success = 1;
1833 exit_status = exitval;
1834 } else if (c->ctl_fd == -1) {
1835 error("client_input_channel_req: unexpected channel %d",
1836 session_ident);
1837 } else {
1838 atomicio(vwrite, c->ctl_fd, &exitval, sizeof(exitval));
1839 success = 1;
1840 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001841 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001842 }
1843 if (reply) {
1844 packet_start(success ?
1845 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller3bbd8782004-06-18 22:23:22 +10001846 packet_put_int(id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001847 packet_send();
1848 }
1849 xfree(rtype);
1850}
Damien Millerc3fa4072002-01-22 23:21:58 +11001851static void
1852client_input_global_request(int type, u_int32_t seq, void *ctxt)
1853{
1854 char *rtype;
1855 int want_reply;
1856 int success = 0;
1857
1858 rtype = packet_get_string(NULL);
1859 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11001860 debug("client_input_global_request: rtype %s want_reply %d",
1861 rtype, want_reply);
Damien Millerc3fa4072002-01-22 23:21:58 +11001862 if (want_reply) {
1863 packet_start(success ?
1864 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
1865 packet_send();
1866 packet_write_wait();
1867 }
1868 xfree(rtype);
1869}
Damien Millerbd483e72000-04-30 10:00:53 +10001870
Damien Miller0e220db2004-06-15 10:34:08 +10001871void
Darren Tuckerfc959702004-07-17 16:12:08 +10001872client_session2_setup(int id, int want_tty, int want_subsystem,
Damien Miller3756dce2004-06-18 01:17:29 +10001873 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env,
Damien Miller0e220db2004-06-15 10:34:08 +10001874 dispatch_fn *subsys_repl)
1875{
1876 int len;
Darren Tucker5d78de62004-11-05 20:35:44 +11001877 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001878
1879 debug2("%s: id %d", __func__, id);
1880
Darren Tucker5d78de62004-11-05 20:35:44 +11001881 if ((c = channel_lookup(id)) == NULL)
1882 fatal("client_session2_setup: channel %d: unknown channel", id);
1883
Damien Miller0e220db2004-06-15 10:34:08 +10001884 if (want_tty) {
1885 struct winsize ws;
1886 struct termios tio;
1887
1888 /* Store window size in the packet. */
1889 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
1890 memset(&ws, 0, sizeof(ws));
1891
1892 channel_request_start(id, "pty-req", 0);
1893 packet_put_cstring(term != NULL ? term : "");
Damien Miller71a73672006-03-26 14:04:36 +11001894 packet_put_int((u_int)ws.ws_col);
1895 packet_put_int((u_int)ws.ws_row);
1896 packet_put_int((u_int)ws.ws_xpixel);
1897 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller0e220db2004-06-15 10:34:08 +10001898 tio = get_saved_tio();
1899 tty_make_modes(-1, tiop != NULL ? tiop : &tio);
1900 packet_send();
1901 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11001902 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10001903 }
1904
1905 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10001906 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10001907 int i, j, matched;
Damien Miller0e220db2004-06-15 10:34:08 +10001908 char *name, *val;
1909
1910 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10001911 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10001912 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10001913 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10001914 if ((val = strchr(name, '=')) == NULL) {
Damien Miller0a0176e2005-11-05 15:07:59 +11001915 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10001916 continue;
1917 }
1918 *val++ = '\0';
1919
1920 matched = 0;
1921 for (j = 0; j < options.num_send_env; j++) {
1922 if (match_pattern(name, options.send_env[j])) {
1923 matched = 1;
1924 break;
1925 }
1926 }
1927 if (!matched) {
1928 debug3("Ignored env %s", name);
Damien Miller0a0176e2005-11-05 15:07:59 +11001929 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10001930 continue;
1931 }
1932
1933 debug("Sending env %s = %s", name, val);
1934 channel_request_start(id, "env", 0);
1935 packet_put_cstring(name);
1936 packet_put_cstring(val);
1937 packet_send();
Damien Miller0a0176e2005-11-05 15:07:59 +11001938 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10001939 }
1940 }
1941
1942 len = buffer_len(cmd);
1943 if (len > 0) {
1944 if (len > 900)
1945 len = 900;
1946 if (want_subsystem) {
1947 debug("Sending subsystem: %.*s", len, (u_char*)buffer_ptr(cmd));
1948 channel_request_start(id, "subsystem", subsys_repl != NULL);
1949 if (subsys_repl != NULL) {
1950 /* register callback for reply */
1951 /* XXX we assume that client_loop has already been called */
1952 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, subsys_repl);
1953 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, subsys_repl);
1954 }
1955 } else {
1956 debug("Sending command: %.*s", len, (u_char*)buffer_ptr(cmd));
1957 channel_request_start(id, "exec", 0);
1958 }
1959 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
1960 packet_send();
1961 } else {
1962 channel_request_start(id, "shell", 0);
1963 packet_send();
1964 }
1965}
1966
Ben Lindstrombba81212001-06-25 05:01:22 +00001967static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001968client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001969{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001970 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10001971
Damien Miller1383bd82000-04-06 12:32:37 +10001972 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
1973 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
1974 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
1975 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10001976 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10001977 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1978 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001979 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10001980 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerc3fa4072002-01-22 23:21:58 +11001981 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001982
1983 /* rekeying */
1984 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10001985
1986 /* global request reply messages */
1987 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
1988 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10001989}
Ben Lindstrombba81212001-06-25 05:01:22 +00001990static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001991client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001992{
1993 dispatch_init(NULL);
1994 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
1995 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
1996 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001997 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1998 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1999 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002000 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
2001 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
2002 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11002003
2004 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
Darren Tucker5dcdd212003-10-02 16:17:00 +10002005 &client_input_agent_open : &deny_input_open);
Damien Miller69b69aa2000-10-28 14:19:58 +11002006 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
2007 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002008}
Ben Lindstrombba81212001-06-25 05:01:22 +00002009static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002010client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002011{
2012 client_init_dispatch_13();
2013 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
2014 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
2015}
Ben Lindstromdb47f382001-07-04 05:10:27 +00002016static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002017client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002018{
Damien Miller1383bd82000-04-06 12:32:37 +10002019 if (compat20)
2020 client_init_dispatch_20();
2021 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10002022 client_init_dispatch_13();
2023 else
2024 client_init_dispatch_15();
2025}
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002026
2027/* client specific fatal cleanup */
2028void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002029cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002030{
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002031 leave_raw_mode();
2032 leave_non_blocking();
Damien Miller0e220db2004-06-15 10:34:08 +10002033 if (options.control_path != NULL && control_fd != -1)
2034 unlink(options.control_path);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002035 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002036}