blob: 9079bcda8b621be3c42bc02972aa67b5c380e358 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * The main loop for the interactive session (client side).
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
12 *
13 *
14 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 *
Damien Miller1383bd82000-04-06 12:32:37 +100037 * SSH2 support added by Markus Friedl.
Damien Millere4340be2000-09-16 13:29:08 +110038 * Copyright (c) 1999,2000 Markus Friedl. All rights reserved.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110059 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060
61#include "includes.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000062RCSID("$OpenBSD: clientloop.c,v 1.43 2001/01/13 19:14:08 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063
64#include "xmalloc.h"
65#include "ssh.h"
66#include "packet.h"
67#include "buffer.h"
Damien Miller5ce662a1999-11-11 17:57:39 +110068#include "readconf.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069
Damien Miller1383bd82000-04-06 12:32:37 +100070#include "ssh2.h"
Damien Millerb38eff82000-04-01 11:09:21 +100071#include "compat.h"
72#include "channels.h"
73#include "dispatch.h"
74
Damien Millerad833b32000-08-23 10:46:23 +100075#include "buffer.h"
76#include "bufaux.h"
77
Damien Miller0bc1bd82000-11-13 22:57:25 +110078#include <openssl/dsa.h>
79#include <openssl/rsa.h>
80#include "key.h"
81#include "authfd.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000082#include "clientloop.h"
Damien Miller69b69aa2000-10-28 14:19:58 +110083
84/* import options */
85extern Options options;
86
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087/* Flag indicating that stdin should be redirected from /dev/null. */
88extern int stdin_null_flag;
89
Damien Miller5428f641999-11-25 11:54:57 +110090/*
91 * Name of the host we are connecting to. This is the name given on the
92 * command line, or the HostName specified for the user-supplied name in a
93 * configuration file.
94 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095extern char *host;
96
Damien Miller5428f641999-11-25 11:54:57 +110097/*
98 * Flag to indicate that we have received a window change signal which has
99 * not yet been processed. This will cause a message indicating the new
100 * window size to be sent to the server a little later. This is volatile
101 * because this is updated in a signal handler.
102 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103static volatile int received_window_change_signal = 0;
104
105/* Terminal modes, as saved by enter_raw_mode. */
106static struct termios saved_tio;
107
Damien Miller5428f641999-11-25 11:54:57 +1100108/*
109 * Flag indicating whether we are in raw mode. This is used by
110 * enter_raw_mode and leave_raw_mode.
111 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112static int in_raw_mode = 0;
113
114/* Flag indicating whether the user\'s terminal is in non-blocking mode. */
115static int in_non_blocking_mode = 0;
116
117/* Common data for the client loop code. */
Damien Millerad833b32000-08-23 10:46:23 +1000118static int quit_pending; /* Set to non-zero to quit the client loop. */
119static int escape_char; /* Escape character. */
Damien Miller95def091999-11-25 00:26:21 +1100120static int escape_pending; /* Last character was the escape character */
121static int last_was_cr; /* Last character was a newline. */
122static int exit_status; /* Used to store the exit status of the command. */
123static int stdin_eof; /* EOF has been encountered on standard error. */
124static Buffer stdin_buffer; /* Buffer for stdin data. */
125static Buffer stdout_buffer; /* Buffer for stdout data. */
126static Buffer stderr_buffer; /* Buffer for stderr data. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000127static u_long stdin_bytes, stdout_bytes, stderr_bytes;
128static u_int buffer_high;/* Soft max buffer size. */
Damien Miller95def091999-11-25 00:26:21 +1100129static int max_fd; /* Maximum file descriptor number in select(). */
130static int connection_in; /* Connection to server (input). */
131static int connection_out; /* Connection to server (output). */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132
Damien Miller1383bd82000-04-06 12:32:37 +1000133
134void client_init_dispatch(void);
135int session_ident = -1;
136
Damien Miller5428f641999-11-25 11:54:57 +1100137/* Returns the user\'s terminal to normal mode if it had been put in raw mode. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138
Damien Miller4af51302000-04-16 11:18:38 +1000139void
Damien Miller95def091999-11-25 00:26:21 +1100140leave_raw_mode()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141{
Damien Miller95def091999-11-25 00:26:21 +1100142 if (!in_raw_mode)
143 return;
144 in_raw_mode = 0;
145 if (tcsetattr(fileno(stdin), TCSADRAIN, &saved_tio) < 0)
146 perror("tcsetattr");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000147
Damien Miller95def091999-11-25 00:26:21 +1100148 fatal_remove_cleanup((void (*) (void *)) leave_raw_mode, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149}
150
151/* Puts the user\'s terminal in raw mode. */
152
Damien Miller4af51302000-04-16 11:18:38 +1000153void
Damien Miller95def091999-11-25 00:26:21 +1100154enter_raw_mode()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155{
Damien Miller95def091999-11-25 00:26:21 +1100156 struct termios tio;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000157
Damien Miller95def091999-11-25 00:26:21 +1100158 if (tcgetattr(fileno(stdin), &tio) < 0)
159 perror("tcgetattr");
160 saved_tio = tio;
161 tio.c_iflag |= IGNPAR;
162 tio.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF);
163 tio.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164#ifdef IEXTEN
Damien Miller95def091999-11-25 00:26:21 +1100165 tio.c_lflag &= ~IEXTEN;
166#endif /* IEXTEN */
167 tio.c_oflag &= ~OPOST;
168 tio.c_cc[VMIN] = 1;
169 tio.c_cc[VTIME] = 0;
170 if (tcsetattr(fileno(stdin), TCSADRAIN, &tio) < 0)
171 perror("tcsetattr");
172 in_raw_mode = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173
Damien Miller95def091999-11-25 00:26:21 +1100174 fatal_add_cleanup((void (*) (void *)) leave_raw_mode, NULL);
175}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000176
177/* Restores stdin to blocking mode. */
178
Damien Miller4af51302000-04-16 11:18:38 +1000179void
Damien Miller95def091999-11-25 00:26:21 +1100180leave_non_blocking()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000181{
Damien Miller95def091999-11-25 00:26:21 +1100182 if (in_non_blocking_mode) {
183 (void) fcntl(fileno(stdin), F_SETFL, 0);
184 in_non_blocking_mode = 0;
185 fatal_remove_cleanup((void (*) (void *)) leave_non_blocking, NULL);
186 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000187}
188
Damien Miller95def091999-11-25 00:26:21 +1100189/* Puts stdin terminal in non-blocking mode. */
190
Damien Miller4af51302000-04-16 11:18:38 +1000191void
Damien Miller95def091999-11-25 00:26:21 +1100192enter_non_blocking()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193{
Damien Miller95def091999-11-25 00:26:21 +1100194 in_non_blocking_mode = 1;
195 (void) fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
196 fatal_add_cleanup((void (*) (void *)) leave_non_blocking, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197}
198
Damien Miller5428f641999-11-25 11:54:57 +1100199/*
200 * Signal handler for the window change signal (SIGWINCH). This just sets a
201 * flag indicating that the window has changed.
202 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203
Damien Miller4af51302000-04-16 11:18:38 +1000204void
Damien Miller95def091999-11-25 00:26:21 +1100205window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206{
Damien Miller95def091999-11-25 00:26:21 +1100207 received_window_change_signal = 1;
208 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209}
210
Damien Miller5428f641999-11-25 11:54:57 +1100211/*
212 * Signal handler for signals that cause the program to terminate. These
213 * signals must be trapped to restore terminal modes.
214 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215
Damien Miller4af51302000-04-16 11:18:38 +1000216void
Damien Miller95def091999-11-25 00:26:21 +1100217signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000218{
Damien Miller95def091999-11-25 00:26:21 +1100219 if (in_raw_mode)
220 leave_raw_mode();
221 if (in_non_blocking_mode)
222 leave_non_blocking();
223 channel_stop_listening();
224 packet_close();
225 fatal("Killed by signal %d.", sig);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000226}
227
Damien Miller5428f641999-11-25 11:54:57 +1100228/*
229 * Returns current time in seconds from Jan 1, 1970 with the maximum
230 * available resolution.
231 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000232
Damien Miller4af51302000-04-16 11:18:38 +1000233double
Damien Miller95def091999-11-25 00:26:21 +1100234get_current_time()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000235{
Damien Miller95def091999-11-25 00:26:21 +1100236 struct timeval tv;
237 gettimeofday(&tv, NULL);
238 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239}
240
Damien Miller5428f641999-11-25 11:54:57 +1100241/*
242 * This is called when the interactive is entered. This checks if there is
243 * an EOF coming on stdin. We must check this explicitly, as select() does
244 * not appear to wake up when redirecting from /dev/null.
245 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246
Damien Miller4af51302000-04-16 11:18:38 +1000247void
Damien Miller95def091999-11-25 00:26:21 +1100248client_check_initial_eof_on_stdin()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000249{
Damien Miller95def091999-11-25 00:26:21 +1100250 int len;
251 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252
Damien Miller5428f641999-11-25 11:54:57 +1100253 /*
254 * If standard input is to be "redirected from /dev/null", we simply
255 * mark that we have seen an EOF and send an EOF message to the
256 * server. Otherwise, we try to read a single character; it appears
257 * that for some files, such /dev/null, select() never wakes up for
258 * read for this descriptor, which means that we never get EOF. This
259 * way we will get the EOF if stdin comes from /dev/null or similar.
260 */
Damien Miller95def091999-11-25 00:26:21 +1100261 if (stdin_null_flag) {
262 /* Fake EOF on stdin. */
263 debug("Sending eof.");
264 stdin_eof = 1;
265 packet_start(SSH_CMSG_EOF);
266 packet_send();
267 } else {
Damien Miller95def091999-11-25 00:26:21 +1100268 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000269
Damien Miller95def091999-11-25 00:26:21 +1100270 /* Check for immediate EOF on stdin. */
271 len = read(fileno(stdin), buf, 1);
272 if (len == 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100273 /* EOF. Record that we have seen it and send EOF to server. */
Damien Miller95def091999-11-25 00:26:21 +1100274 debug("Sending eof.");
275 stdin_eof = 1;
276 packet_start(SSH_CMSG_EOF);
277 packet_send();
278 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100279 /*
280 * Got data. We must store the data in the buffer,
281 * and also process it as an escape character if
282 * appropriate.
283 */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000284 if ((u_char) buf[0] == escape_char)
Damien Miller95def091999-11-25 00:26:21 +1100285 escape_pending = 1;
286 else {
287 buffer_append(&stdin_buffer, buf, 1);
288 stdin_bytes += 1;
289 }
290 }
Damien Miller95def091999-11-25 00:26:21 +1100291 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000293}
294
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000295
Damien Miller5428f641999-11-25 11:54:57 +1100296/*
297 * Make packets from buffered stdin data, and buffer them for sending to the
298 * connection.
299 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000300
Damien Miller4af51302000-04-16 11:18:38 +1000301void
Damien Miller95def091999-11-25 00:26:21 +1100302client_make_packets_from_stdin_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000304 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305
Damien Miller95def091999-11-25 00:26:21 +1100306 /* Send buffered stdin data to the server. */
307 while (buffer_len(&stdin_buffer) > 0 &&
308 packet_not_very_much_data_to_write()) {
309 len = buffer_len(&stdin_buffer);
310 /* Keep the packets at reasonable size. */
311 if (len > packet_get_maxsize())
312 len = packet_get_maxsize();
313 packet_start(SSH_CMSG_STDIN_DATA);
314 packet_put_string(buffer_ptr(&stdin_buffer), len);
315 packet_send();
316 buffer_consume(&stdin_buffer, len);
317 /* If we have a pending EOF, send it now. */
318 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
319 packet_start(SSH_CMSG_EOF);
320 packet_send();
321 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323}
324
Damien Miller5428f641999-11-25 11:54:57 +1100325/*
326 * Checks if the client window has changed, and sends a packet about it to
327 * the server if so. The actual change is detected elsewhere (by a software
328 * interrupt on Unix); this just checks the flag and sends a message if
329 * appropriate.
330 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331
Damien Miller4af51302000-04-16 11:18:38 +1000332void
Damien Miller95def091999-11-25 00:26:21 +1100333client_check_window_change()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334{
Damien Miller1383bd82000-04-06 12:32:37 +1000335 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336
Damien Miller1383bd82000-04-06 12:32:37 +1000337 if (! received_window_change_signal)
338 return;
339 /** XXX race */
340 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341
Damien Miller1383bd82000-04-06 12:32:37 +1000342 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
343 return;
344
Damien Millerd3444942000-09-30 14:20:03 +1100345 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000346
347 if (compat20) {
348 channel_request_start(session_ident, "window-change", 0);
349 packet_put_int(ws.ws_col);
350 packet_put_int(ws.ws_row);
351 packet_put_int(ws.ws_xpixel);
352 packet_put_int(ws.ws_ypixel);
353 packet_send();
354 } else {
355 packet_start(SSH_CMSG_WINDOW_SIZE);
356 packet_put_int(ws.ws_row);
357 packet_put_int(ws.ws_col);
358 packet_put_int(ws.ws_xpixel);
359 packet_put_int(ws.ws_ypixel);
360 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000361 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000362}
363
Damien Miller5428f641999-11-25 11:54:57 +1100364/*
365 * Waits until the client can do something (some data becomes available on
366 * one of the file descriptors).
367 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000368
Damien Miller4af51302000-04-16 11:18:38 +1000369void
Damien Miller95def091999-11-25 00:26:21 +1100370client_wait_until_can_do_something(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000371{
Damien Miller95def091999-11-25 00:26:21 +1100372 /* Initialize select masks. */
373 FD_ZERO(readset);
Damien Miller95def091999-11-25 00:26:21 +1100374 FD_ZERO(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000375
Damien Miller1383bd82000-04-06 12:32:37 +1000376 if (!compat20) {
377 /* Read from the connection, unless our buffers are full. */
378 if (buffer_len(&stdout_buffer) < buffer_high &&
379 buffer_len(&stderr_buffer) < buffer_high &&
380 channel_not_very_much_buffered_data())
381 FD_SET(connection_in, readset);
382 /*
383 * Read from stdin, unless we have seen EOF or have very much
384 * buffered data to send to the server.
385 */
386 if (!stdin_eof && packet_not_very_much_data_to_write())
387 FD_SET(fileno(stdin), readset);
388
389 /* Select stdout/stderr if have data in buffer. */
390 if (buffer_len(&stdout_buffer) > 0)
391 FD_SET(fileno(stdout), writeset);
392 if (buffer_len(&stderr_buffer) > 0)
393 FD_SET(fileno(stderr), writeset);
394 } else {
395 FD_SET(connection_in, readset);
396 }
397
Damien Miller95def091999-11-25 00:26:21 +1100398 /* Add any selections by the channel mechanism. */
399 channel_prepare_select(readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000400
Damien Miller95def091999-11-25 00:26:21 +1100401 /* Select server connection if have data to write to the server. */
402 if (packet_have_data_to_write())
403 FD_SET(connection_out, writeset);
404
Damien Miller1383bd82000-04-06 12:32:37 +1000405/* move UP XXX */
Damien Miller95def091999-11-25 00:26:21 +1100406 /* Update maximum file descriptor number, if appropriate. */
407 if (channel_max_fd() > max_fd)
408 max_fd = channel_max_fd();
409
Damien Miller5428f641999-11-25 11:54:57 +1100410 /*
411 * Wait for something to happen. This will suspend the process until
412 * some selected descriptor can be read, written, or has some other
413 * event pending. Note: if you want to implement SSH_MSG_IGNORE
414 * messages to fool traffic analysis, this might be the place to do
415 * it: just have a random timeout for the select, and send a random
416 * SSH_MSG_IGNORE packet when the timeout expires.
417 */
Damien Miller95def091999-11-25 00:26:21 +1100418
419 if (select(max_fd + 1, readset, writeset, NULL, NULL) < 0) {
420 char buf[100];
421 /* Some systems fail to clear these automatically. */
422 FD_ZERO(readset);
423 FD_ZERO(writeset);
424 if (errno == EINTR)
425 return;
426 /* Note: we might still have data in the buffers. */
427 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
428 buffer_append(&stderr_buffer, buf, strlen(buf));
429 stderr_bytes += strlen(buf);
430 quit_pending = 1;
431 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432}
433
Damien Miller4af51302000-04-16 11:18:38 +1000434void
Damien Millerad833b32000-08-23 10:46:23 +1000435client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436{
Damien Miller95def091999-11-25 00:26:21 +1100437 struct winsize oldws, newws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000438
Damien Miller95def091999-11-25 00:26:21 +1100439 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000440 if (buffer_len(bout) > 0)
441 atomicio(write, fileno(stdout), buffer_ptr(bout), buffer_len(bout));
442 if (buffer_len(berr) > 0)
443 atomicio(write, fileno(stderr), buffer_ptr(berr), buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000444
Damien Miller95def091999-11-25 00:26:21 +1100445 leave_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000446
Damien Miller5428f641999-11-25 11:54:57 +1100447 /*
448 * Free (and clear) the buffer to reduce the amount of data that gets
449 * written to swap.
450 */
Damien Millerad833b32000-08-23 10:46:23 +1000451 buffer_free(bin);
452 buffer_free(bout);
453 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000454
Damien Miller95def091999-11-25 00:26:21 +1100455 /* Save old window size. */
456 ioctl(fileno(stdin), TIOCGWINSZ, &oldws);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457
Damien Miller95def091999-11-25 00:26:21 +1100458 /* Send the suspend signal to the program itself. */
459 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460
Damien Miller95def091999-11-25 00:26:21 +1100461 /* Check if the window size has changed. */
462 if (ioctl(fileno(stdin), TIOCGWINSZ, &newws) >= 0 &&
463 (oldws.ws_row != newws.ws_row ||
464 oldws.ws_col != newws.ws_col ||
465 oldws.ws_xpixel != newws.ws_xpixel ||
466 oldws.ws_ypixel != newws.ws_ypixel))
467 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468
Damien Miller95def091999-11-25 00:26:21 +1100469 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000470 buffer_init(bin);
471 buffer_init(bout);
472 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473
Damien Miller95def091999-11-25 00:26:21 +1100474 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475}
476
Damien Miller4af51302000-04-16 11:18:38 +1000477void
Damien Miller1383bd82000-04-06 12:32:37 +1000478client_process_net_input(fd_set * readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000479{
Damien Miller1383bd82000-04-06 12:32:37 +1000480 int len;
481 char buf[8192];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000482
Damien Miller5428f641999-11-25 11:54:57 +1100483 /*
484 * Read input from the server, and add any such data to the buffer of
485 * the packet subsystem.
486 */
Damien Miller95def091999-11-25 00:26:21 +1100487 if (FD_ISSET(connection_in, readset)) {
488 /* Read as much as possible. */
489 len = read(connection_in, buf, sizeof(buf));
490 if (len == 0) {
491 /* Received EOF. The remote host has closed the connection. */
492 snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n",
493 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000494 buffer_append(&stderr_buffer, buf, strlen(buf));
495 stderr_bytes += strlen(buf);
496 quit_pending = 1;
497 return;
Damien Miller95def091999-11-25 00:26:21 +1100498 }
Damien Miller5428f641999-11-25 11:54:57 +1100499 /*
500 * There is a kernel bug on Solaris that causes select to
501 * sometimes wake up even though there is no data available.
502 */
Damien Miller95def091999-11-25 00:26:21 +1100503 if (len < 0 && errno == EAGAIN)
504 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000505
Damien Miller95def091999-11-25 00:26:21 +1100506 if (len < 0) {
507 /* An error has encountered. Perhaps there is a network problem. */
508 snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n",
509 host, strerror(errno));
510 buffer_append(&stderr_buffer, buf, strlen(buf));
511 stderr_bytes += strlen(buf);
512 quit_pending = 1;
513 return;
514 }
515 packet_process_incoming(buf, len);
516 }
Damien Miller1383bd82000-04-06 12:32:37 +1000517}
518
Damien Millerad833b32000-08-23 10:46:23 +1000519/* process the characters one by one */
520int
521process_escapes(Buffer *bin, Buffer *bout, Buffer *berr, char *buf, int len)
522{
523 char string[1024];
524 pid_t pid;
525 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000526 u_int i;
527 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +1000528 char *s;
529
530 for (i = 0; i < len; i++) {
531 /* Get one character at a time. */
532 ch = buf[i];
533
534 if (escape_pending) {
535 /* We have previously seen an escape character. */
536 /* Clear the flag now. */
537 escape_pending = 0;
538
539 /* Process the escaped character. */
540 switch (ch) {
541 case '.':
542 /* Terminate the connection. */
543 snprintf(string, sizeof string, "%c.\r\n", escape_char);
544 buffer_append(berr, string, strlen(string));
545 /*stderr_bytes += strlen(string); XXX*/
546
547 quit_pending = 1;
548 return -1;
549
550 case 'Z' - 64:
551 /* Suspend the program. */
552 /* Print a message to that effect to the user. */
553 snprintf(string, sizeof string, "%c^Z [suspend ssh]\r\n", escape_char);
554 buffer_append(berr, string, strlen(string));
555 /*stderr_bytes += strlen(string); XXX*/
556
557 /* Restore terminal modes and suspend. */
558 client_suspend_self(bin, bout, berr);
559
560 /* We have been continued. */
561 continue;
562
563 case '&':
564 /* XXX does not work yet with proto 2 */
565 if (compat20)
566 continue;
567 /*
568 * Detach the program (continue to serve connections,
569 * but put in background and no more new connections).
570 */
571 if (!stdin_eof) {
572 /*
573 * Sending SSH_CMSG_EOF alone does not always appear
574 * to be enough. So we try to send an EOF character
575 * first.
576 */
577 packet_start(SSH_CMSG_STDIN_DATA);
578 packet_put_string("\004", 1);
579 packet_send();
580 /* Close stdin. */
581 stdin_eof = 1;
582 if (buffer_len(bin) == 0) {
583 packet_start(SSH_CMSG_EOF);
584 packet_send();
585 }
586 }
587 /* Restore tty modes. */
588 leave_raw_mode();
589
590 /* Stop listening for new connections. */
591 channel_stop_listening();
592
593 printf("%c& [backgrounded]\n", escape_char);
594
595 /* Fork into background. */
596 pid = fork();
597 if (pid < 0) {
598 error("fork: %.100s", strerror(errno));
599 continue;
600 }
601 if (pid != 0) { /* This is the parent. */
602 /* The parent just exits. */
603 exit(0);
604 }
605 /* The child continues serving connections. */
606 continue; /*XXX ? */
607
608 case '?':
609 snprintf(string, sizeof string,
610"%c?\r\n\
611Supported escape sequences:\r\n\
612~. - terminate connection\r\n\
613~^Z - suspend ssh\r\n\
614~# - list forwarded connections\r\n\
615~& - background ssh (when waiting for connections to terminate)\r\n\
616~? - this message\r\n\
617~~ - send the escape character by typing it twice\r\n\
618(Note that escapes are only recognized immediately after newline.)\r\n",
619 escape_char);
620 buffer_append(berr, string, strlen(string));
621 continue;
622
623 case '#':
624 snprintf(string, sizeof string, "%c#\r\n", escape_char);
625 buffer_append(berr, string, strlen(string));
626 s = channel_open_message();
627 buffer_append(berr, s, strlen(s));
628 xfree(s);
629 continue;
630
631 default:
632 if (ch != escape_char) {
633 buffer_put_char(bin, escape_char);
634 bytes++;
635 }
636 /* Escaped characters fall through here */
637 break;
638 }
639 } else {
640 /*
641 * The previous character was not an escape char. Check if this
642 * is an escape.
643 */
644 if (last_was_cr && ch == escape_char) {
645 /* It is. Set the flag and continue to next character. */
646 escape_pending = 1;
647 continue;
648 }
649 }
650
651 /*
652 * Normal character. Record whether it was a newline,
653 * and append it to the buffer.
654 */
655 last_was_cr = (ch == '\r' || ch == '\n');
656 buffer_put_char(bin, ch);
657 bytes++;
658 }
659 return bytes;
660}
661
Damien Miller4af51302000-04-16 11:18:38 +1000662void
Damien Miller1383bd82000-04-06 12:32:37 +1000663client_process_input(fd_set * readset)
664{
Damien Millerad833b32000-08-23 10:46:23 +1000665 int ret;
Damien Miller166fca82000-04-20 07:42:21 +1000666 int len;
Damien Millerad833b32000-08-23 10:46:23 +1000667 char buf[8192];
Damien Miller1383bd82000-04-06 12:32:37 +1000668
Damien Miller95def091999-11-25 00:26:21 +1100669 /* Read input from stdin. */
670 if (FD_ISSET(fileno(stdin), readset)) {
671 /* Read as much as possible. */
672 len = read(fileno(stdin), buf, sizeof(buf));
673 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100674 /*
675 * Received EOF or error. They are treated
676 * similarly, except that an error message is printed
677 * if it was an error condition.
678 */
Damien Miller95def091999-11-25 00:26:21 +1100679 if (len < 0) {
680 snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno));
681 buffer_append(&stderr_buffer, buf, strlen(buf));
682 stderr_bytes += strlen(buf);
683 }
684 /* Mark that we have seen EOF. */
685 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +1100686 /*
687 * Send an EOF message to the server unless there is
688 * data in the buffer. If there is data in the
689 * buffer, no message will be sent now. Code
690 * elsewhere will send the EOF when the buffer
691 * becomes empty if stdin_eof is set.
692 */
Damien Miller95def091999-11-25 00:26:21 +1100693 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000694 packet_start(SSH_CMSG_EOF);
695 packet_send();
Damien Miller95def091999-11-25 00:26:21 +1100696 }
697 } else if (escape_char == -1) {
Damien Miller5428f641999-11-25 11:54:57 +1100698 /*
699 * Normal successful read, and no escape character.
700 * Just append the data to buffer.
701 */
Damien Miller95def091999-11-25 00:26:21 +1100702 buffer_append(&stdin_buffer, buf, len);
703 stdin_bytes += len;
704 } else {
Damien Miller5428f641999-11-25 11:54:57 +1100705 /*
706 * Normal, successful read. But we have an escape character
707 * and have to process the characters one by one.
708 */
Damien Millerad833b32000-08-23 10:46:23 +1000709 ret = process_escapes(&stdin_buffer, &stdout_buffer, &stderr_buffer, buf, len);
710 if (ret == -1)
711 return;
712 stdout_bytes += ret;
Damien Miller95def091999-11-25 00:26:21 +1100713 }
714 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000715}
716
Damien Miller4af51302000-04-16 11:18:38 +1000717void
Damien Miller95def091999-11-25 00:26:21 +1100718client_process_output(fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000719{
Damien Miller95def091999-11-25 00:26:21 +1100720 int len;
721 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000722
Damien Miller95def091999-11-25 00:26:21 +1100723 /* Write buffered output to stdout. */
724 if (FD_ISSET(fileno(stdout), writeset)) {
725 /* Write as much data as possible. */
726 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100727 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100728 if (len <= 0) {
729 if (errno == EAGAIN)
730 len = 0;
731 else {
Damien Miller5428f641999-11-25 11:54:57 +1100732 /*
733 * An error or EOF was encountered. Put an
734 * error message to stderr buffer.
735 */
Damien Miller95def091999-11-25 00:26:21 +1100736 snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno));
737 buffer_append(&stderr_buffer, buf, strlen(buf));
738 stderr_bytes += strlen(buf);
739 quit_pending = 1;
740 return;
741 }
742 }
743 /* Consume printed data from the buffer. */
744 buffer_consume(&stdout_buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000745 }
Damien Miller95def091999-11-25 00:26:21 +1100746 /* Write buffered output to stderr. */
747 if (FD_ISSET(fileno(stderr), writeset)) {
748 /* Write as much data as possible. */
749 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100750 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100751 if (len <= 0) {
752 if (errno == EAGAIN)
753 len = 0;
754 else {
Damien Miller5428f641999-11-25 11:54:57 +1100755 /* EOF or error, but can't even print error message. */
Damien Miller95def091999-11-25 00:26:21 +1100756 quit_pending = 1;
757 return;
758 }
759 }
760 /* Consume printed characters from the buffer. */
761 buffer_consume(&stderr_buffer, len);
762 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000763}
764
Damien Miller5428f641999-11-25 11:54:57 +1100765/*
Damien Millerb38eff82000-04-01 11:09:21 +1000766 * Get packets from the connection input buffer, and process them as long as
767 * there are packets available.
768 *
769 * Any unknown packets received during the actual
770 * session cause the session to terminate. This is
771 * intended to make debugging easier since no
772 * confirmations are sent. Any compatible protocol
773 * extensions must be negotiated during the
774 * preparatory phase.
775 */
776
Damien Miller4af51302000-04-16 11:18:38 +1000777void
Damien Millerb38eff82000-04-01 11:09:21 +1000778client_process_buffered_input_packets()
779{
Damien Miller62cee002000-09-23 17:15:56 +1100780 dispatch_run(DISPATCH_NONBLOCK, &quit_pending, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000781}
782
Damien Millerad833b32000-08-23 10:46:23 +1000783/* scan buf[] for '~' before sending data to the peer */
784
785int
786simple_escape_filter(Channel *c, char *buf, int len)
787{
788 /* XXX we assume c->extended is writeable */
789 return process_escapes(&c->input, &c->output, &c->extended, buf, len);
790}
791
Damien Millerb38eff82000-04-01 11:09:21 +1000792/*
Damien Miller5428f641999-11-25 11:54:57 +1100793 * Implements the interactive session with the server. This is called after
794 * the user has been authenticated, and a command has been started on the
795 * remote host. If escape_char != -1, it is the character used as an escape
796 * character for terminating or suspending the session.
797 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000798
Damien Miller4af51302000-04-16 11:18:38 +1000799int
Damien Millerad833b32000-08-23 10:46:23 +1000800client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000801{
Damien Miller95def091999-11-25 00:26:21 +1100802 double start_time, total_time;
803 int len;
804 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000805
Damien Miller95def091999-11-25 00:26:21 +1100806 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000807
Damien Miller95def091999-11-25 00:26:21 +1100808 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000809
Damien Miller95def091999-11-25 00:26:21 +1100810 /* Initialize variables. */
811 escape_pending = 0;
812 last_was_cr = 1;
813 exit_status = -1;
814 stdin_eof = 0;
815 buffer_high = 64 * 1024;
816 connection_in = packet_get_connection_in();
817 connection_out = packet_get_connection_out();
818 max_fd = connection_in;
819 if (connection_out > max_fd)
820 max_fd = connection_out;
821 stdin_bytes = 0;
822 stdout_bytes = 0;
823 stderr_bytes = 0;
824 quit_pending = 0;
825 escape_char = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000826
Damien Miller95def091999-11-25 00:26:21 +1100827 /* Initialize buffers. */
828 buffer_init(&stdin_buffer);
829 buffer_init(&stdout_buffer);
830 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000831
Damien Millerb38eff82000-04-01 11:09:21 +1000832 client_init_dispatch();
833
Damien Miller95def091999-11-25 00:26:21 +1100834 /* Set signal handlers to restore non-blocking mode. */
835 signal(SIGINT, signal_handler);
836 signal(SIGQUIT, signal_handler);
837 signal(SIGTERM, signal_handler);
838 signal(SIGPIPE, SIG_IGN);
839 if (have_pty)
840 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000841
Damien Miller95def091999-11-25 00:26:21 +1100842 if (have_pty)
843 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000844
Damien Millerbe484b52000-07-15 14:14:16 +1000845 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +1000846 if (!compat20)
847 client_check_initial_eof_on_stdin();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000848
Damien Millerad833b32000-08-23 10:46:23 +1000849 if (compat20 && escape_char != -1)
850 channel_register_filter(ssh2_chan_id, simple_escape_filter);
851
Damien Miller95def091999-11-25 00:26:21 +1100852 /* Main loop of the client for the interactive session mode. */
853 while (!quit_pending) {
854 fd_set readset, writeset;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000855
Damien Miller5428f641999-11-25 11:54:57 +1100856 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +1100857 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000858
Damien Miller1383bd82000-04-06 12:32:37 +1000859 if (compat20 && !channel_still_open()) {
Damien Millerd3444942000-09-30 14:20:03 +1100860 debug2("!channel_still_open.");
Damien Miller1383bd82000-04-06 12:32:37 +1000861 break;
862 }
863
Damien Miller5428f641999-11-25 11:54:57 +1100864 /*
865 * Make packets of buffered stdin data, and buffer them for
866 * sending to the server.
867 */
Damien Miller1383bd82000-04-06 12:32:37 +1000868 if (!compat20)
869 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000870
Damien Miller5428f641999-11-25 11:54:57 +1100871 /*
872 * Make packets from buffered channel data, and buffer them
873 * for sending to the server.
874 */
Damien Miller95def091999-11-25 00:26:21 +1100875 if (packet_not_very_much_data_to_write())
876 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000877
Damien Miller5428f641999-11-25 11:54:57 +1100878 /*
879 * Check if the window size has changed, and buffer a message
880 * about it to the server if so.
881 */
Damien Miller95def091999-11-25 00:26:21 +1100882 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000883
Damien Miller95def091999-11-25 00:26:21 +1100884 if (quit_pending)
885 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000886
Damien Miller5428f641999-11-25 11:54:57 +1100887 /*
888 * Wait until we have something to do (something becomes
889 * available on one of the descriptors).
890 */
Damien Miller95def091999-11-25 00:26:21 +1100891 client_wait_until_can_do_something(&readset, &writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000892
Damien Miller95def091999-11-25 00:26:21 +1100893 if (quit_pending)
894 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000895
Damien Miller95def091999-11-25 00:26:21 +1100896 /* Do channel operations. */
897 channel_after_select(&readset, &writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000898
Damien Miller1383bd82000-04-06 12:32:37 +1000899 /* Buffer input from the connection. */
900 client_process_net_input(&readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000901
Damien Miller1383bd82000-04-06 12:32:37 +1000902 if (quit_pending)
903 break;
904
905 if (!compat20) {
906 /* Buffer data from stdin */
907 client_process_input(&readset);
908 /*
909 * Process output to stdout and stderr. Output to
910 * the connection is processed elsewhere (above).
911 */
912 client_process_output(&writeset);
913 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914
Damien Miller5428f641999-11-25 11:54:57 +1100915 /* Send as much buffered packet data as possible to the sender. */
Damien Miller95def091999-11-25 00:26:21 +1100916 if (FD_ISSET(connection_out, &writeset))
917 packet_write_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000918 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000919
Damien Miller95def091999-11-25 00:26:21 +1100920 /* Terminate the session. */
921
922 /* Stop watching for window change. */
923 if (have_pty)
924 signal(SIGWINCH, SIG_DFL);
925
926 /* Stop listening for connections. */
927 channel_stop_listening();
928
Damien Miller5428f641999-11-25 11:54:57 +1100929 /*
930 * In interactive mode (with pseudo tty) display a message indicating
931 * that the connection has been closed.
932 */
Damien Miller95def091999-11-25 00:26:21 +1100933 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
934 snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host);
935 buffer_append(&stderr_buffer, buf, strlen(buf));
936 stderr_bytes += strlen(buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000937 }
Damien Miller95def091999-11-25 00:26:21 +1100938 /* Output any buffered data for stdout. */
939 while (buffer_len(&stdout_buffer) > 0) {
940 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100941 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100942 if (len <= 0) {
943 error("Write failed flushing stdout buffer.");
944 break;
945 }
946 buffer_consume(&stdout_buffer, len);
947 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000948
Damien Miller95def091999-11-25 00:26:21 +1100949 /* Output any buffered data for stderr. */
950 while (buffer_len(&stderr_buffer) > 0) {
951 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100952 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100953 if (len <= 0) {
954 error("Write failed flushing stderr buffer.");
955 break;
956 }
957 buffer_consume(&stderr_buffer, len);
958 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000959
Damien Miller95def091999-11-25 00:26:21 +1100960 if (have_pty)
961 leave_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000962
Damien Miller95def091999-11-25 00:26:21 +1100963 /* Clear and free any buffers. */
964 memset(buf, 0, sizeof(buf));
965 buffer_free(&stdin_buffer);
966 buffer_free(&stdout_buffer);
967 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000968
Damien Miller95def091999-11-25 00:26:21 +1100969 /* Report bytes transferred, and transfer rates. */
970 total_time = get_current_time() - start_time;
971 debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds",
972 stdin_bytes, stdout_bytes, stderr_bytes, total_time);
973 if (total_time > 0)
974 debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f",
975 stdin_bytes / total_time, stdout_bytes / total_time,
976 stderr_bytes / total_time);
977
978 /* Return the exit status of the program. */
979 debug("Exit status %d", exit_status);
980 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000981}
Damien Millerb38eff82000-04-01 11:09:21 +1000982
983/*********/
984
985void
Damien Miller62cee002000-09-23 17:15:56 +1100986client_input_stdout_data(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000987{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000988 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000989 char *data = packet_get_string(&data_len);
990 packet_integrity_check(plen, 4 + data_len, type);
991 buffer_append(&stdout_buffer, data, data_len);
992 stdout_bytes += data_len;
993 memset(data, 0, data_len);
994 xfree(data);
995}
996void
Damien Miller62cee002000-09-23 17:15:56 +1100997client_input_stderr_data(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000998{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000999 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001000 char *data = packet_get_string(&data_len);
1001 packet_integrity_check(plen, 4 + data_len, type);
1002 buffer_append(&stderr_buffer, data, data_len);
1003 stdout_bytes += data_len;
1004 memset(data, 0, data_len);
1005 xfree(data);
1006}
1007void
Damien Miller62cee002000-09-23 17:15:56 +11001008client_input_exit_status(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001009{
1010 packet_integrity_check(plen, 4, type);
1011 exit_status = packet_get_int();
1012 /* Acknowledge the exit. */
1013 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1014 packet_send();
1015 /*
1016 * Must wait for packet to be sent since we are
1017 * exiting the loop.
1018 */
1019 packet_write_wait();
1020 /* Flag that we want to exit. */
1021 quit_pending = 1;
1022}
1023
Damien Miller0bc1bd82000-11-13 22:57:25 +11001024Channel *
1025client_request_forwarded_tcpip(const char *request_type, int rchan)
1026{
1027 Channel* c = NULL;
1028 char *listen_address, *originator_address;
1029 int listen_port, originator_port;
1030 int sock, newch;
1031
1032 /* Get rest of the packet */
1033 listen_address = packet_get_string(NULL);
1034 listen_port = packet_get_int();
1035 originator_address = packet_get_string(NULL);
1036 originator_port = packet_get_int();
1037 packet_done();
1038
1039 debug("client_request_forwarded_tcpip: listen %s port %d, originator %s port %d",
1040 listen_address, listen_port, originator_address, originator_port);
1041
1042 sock = channel_connect_by_listen_adress(listen_port);
1043 if (sock >= 0) {
1044 newch = channel_new("forwarded-tcpip",
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001045 SSH_CHANNEL_CONNECTING, sock, sock, -1,
Damien Miller0bc1bd82000-11-13 22:57:25 +11001046 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
1047 xstrdup(originator_address), 1);
1048 c = channel_lookup(newch);
1049 }
1050 xfree(originator_address);
1051 xfree(listen_address);
1052 return c;
1053}
1054
1055Channel*
1056client_request_x11(const char *request_type, int rchan)
1057{
1058 Channel *c = NULL;
1059 char *originator;
1060 int originator_port;
1061 int sock, newch;
1062
1063 if (!options.forward_x11) {
1064 error("Warning: ssh server tried X11 forwarding.");
1065 error("Warning: this is probably a break in attempt by a malicious server.");
1066 return NULL;
1067 }
1068 originator = packet_get_string(NULL);
1069 if (datafellows & SSH_BUG_X11FWD) {
1070 debug2("buggy server: x11 request w/o originator_port");
1071 originator_port = 0;
1072 } else {
1073 originator_port = packet_get_int();
1074 }
1075 packet_done();
1076 /* XXX check permission */
1077 sock = x11_connect_display();
1078 if (sock >= 0) {
1079 newch = channel_new("x11",
1080 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
1081 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0,
1082 xstrdup("x11"), 1);
1083 c = channel_lookup(newch);
1084 }
1085 xfree(originator);
1086 return c;
1087}
1088
1089Channel*
1090client_request_agent(const char *request_type, int rchan)
1091{
1092 Channel *c = NULL;
1093 int sock, newch;
1094
1095 if (!options.forward_agent) {
1096 error("Warning: ssh server tried agent forwarding.");
1097 error("Warning: this is probably a break in attempt by a malicious server.");
1098 return NULL;
1099 }
1100 sock = ssh_get_authentication_socket();
1101 if (sock >= 0) {
1102 newch = channel_new("authentication agent connection",
1103 SSH_CHANNEL_OPEN, sock, sock, -1,
1104 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
1105 xstrdup("authentication agent connection"), 1);
1106 c = channel_lookup(newch);
1107 }
1108 return c;
1109}
1110
Damien Millerbd483e72000-04-30 10:00:53 +10001111/* XXXX move to generic input handler */
1112void
Damien Miller62cee002000-09-23 17:15:56 +11001113client_input_channel_open(int type, int plen, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001114{
1115 Channel *c = NULL;
1116 char *ctype;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001117 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001118 int rchan;
1119 int rmaxpack;
1120 int rwindow;
1121
1122 ctype = packet_get_string(&len);
1123 rchan = packet_get_int();
1124 rwindow = packet_get_int();
1125 rmaxpack = packet_get_int();
1126
Damien Millere247cc42000-05-07 12:03:14 +10001127 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001128 ctype, rchan, rwindow, rmaxpack);
1129
Damien Miller0bc1bd82000-11-13 22:57:25 +11001130 if (strcmp(ctype, "forwarded-tcpip") == 0) {
1131 c = client_request_forwarded_tcpip(ctype, rchan);
1132 } else if (strcmp(ctype, "x11") == 0) {
1133 c = client_request_x11(ctype, rchan);
1134 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1135 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001136 }
1137/* XXX duplicate : */
1138 if (c != NULL) {
1139 debug("confirm %s", ctype);
1140 c->remote_id = rchan;
1141 c->remote_window = rwindow;
1142 c->remote_maxpacket = rmaxpack;
1143
1144 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1145 packet_put_int(c->remote_id);
1146 packet_put_int(c->self);
1147 packet_put_int(c->local_window);
1148 packet_put_int(c->local_maxpacket);
1149 packet_send();
1150 } else {
1151 debug("failure %s", ctype);
1152 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1153 packet_put_int(rchan);
1154 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
1155 packet_put_cstring("bla bla");
1156 packet_put_cstring("");
1157 packet_send();
1158 }
1159 xfree(ctype);
1160}
1161
Damien Miller4af51302000-04-16 11:18:38 +10001162void
Damien Miller1383bd82000-04-06 12:32:37 +10001163client_init_dispatch_20()
1164{
1165 dispatch_init(&dispatch_protocol_error);
1166 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
1167 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
1168 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
1169 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10001170 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10001171 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1172 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1173 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &channel_input_channel_request);
1174 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
1175}
Damien Miller4af51302000-04-16 11:18:38 +10001176void
Damien Millerb38eff82000-04-01 11:09:21 +10001177client_init_dispatch_13()
1178{
1179 dispatch_init(NULL);
1180 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
1181 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
1182 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001183 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1184 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1185 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10001186 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
1187 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
1188 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11001189
1190 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
1191 &auth_input_open_request : &deny_input_open);
1192 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
1193 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10001194}
Damien Miller4af51302000-04-16 11:18:38 +10001195void
Damien Millerb38eff82000-04-01 11:09:21 +10001196client_init_dispatch_15()
1197{
1198 client_init_dispatch_13();
1199 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
1200 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
1201}
Damien Miller4af51302000-04-16 11:18:38 +10001202void
Damien Millerb38eff82000-04-01 11:09:21 +10001203client_init_dispatch()
1204{
Damien Miller1383bd82000-04-06 12:32:37 +10001205 if (compat20)
1206 client_init_dispatch_20();
1207 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001208 client_init_dispatch_13();
1209 else
1210 client_init_dispatch_15();
1211}
Damien Miller1383bd82000-04-06 12:32:37 +10001212
1213void
1214client_input_channel_req(int id, void *arg)
1215{
1216 Channel *c = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001217 u_int len;
Damien Miller1383bd82000-04-06 12:32:37 +10001218 int success = 0;
1219 int reply;
1220 char *rtype;
1221
1222 rtype = packet_get_string(&len);
1223 reply = packet_get_char();
1224
Damien Millere247cc42000-05-07 12:03:14 +10001225 debug("client_input_channel_req: rtype %s reply %d", rtype, reply);
Damien Miller1383bd82000-04-06 12:32:37 +10001226
1227 c = channel_lookup(id);
1228 if (c == NULL)
Damien Millere4340be2000-09-16 13:29:08 +11001229 fatal("client_input_channel_req: channel %d: bad channel", id);
Damien Miller1383bd82000-04-06 12:32:37 +10001230
1231 if (session_ident == -1) {
1232 error("client_input_channel_req: no channel %d", id);
1233 } else if (id != session_ident) {
1234 error("client_input_channel_req: bad channel %d != %d",
1235 id, session_ident);
1236 } else if (strcmp(rtype, "exit-status") == 0) {
1237 success = 1;
1238 exit_status = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001239 packet_done();
Damien Miller1383bd82000-04-06 12:32:37 +10001240 }
1241 if (reply) {
1242 packet_start(success ?
1243 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1244 packet_put_int(c->remote_id);
1245 packet_send();
1246 }
1247 xfree(rtype);
1248}
1249
1250void
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001251clientloop_set_session_ident(int id)
Damien Miller1383bd82000-04-06 12:32:37 +10001252{
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001253 debug2("clientloop_set_session_ident: id %d", id);
Damien Miller1383bd82000-04-06 12:32:37 +10001254 session_ident = id;
1255 channel_register_callback(id, SSH2_MSG_CHANNEL_REQUEST,
1256 client_input_channel_req, (void *)0);
1257}