blob: cc25ca550605da1681c47f33c1b05713b778e55d [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller4af51302000-04-16 11:18:38 +10002 *
Damien Miller95def091999-11-25 00:26:21 +11003 * clientloop.c
Damien Miller4af51302000-04-16 11:18:38 +10004 *
Damien Miller95def091999-11-25 00:26:21 +11005 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Miller95def091999-11-25 00:26:21 +11007 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10009 *
10 *
Damien Miller95def091999-11-25 00:26:21 +110011 * Created: Sat Sep 23 12:23:57 1995 ylo
Damien Miller4af51302000-04-16 11:18:38 +100012 *
Damien Miller95def091999-11-25 00:26:21 +110013 * The main loop for the interactive session (client side).
Damien Miller4af51302000-04-16 11:18:38 +100014 *
Damien Miller1383bd82000-04-06 12:32:37 +100015 * SSH2 support added by Markus Friedl.
Damien Miller95def091999-11-25 00:26:21 +110016 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100017
18#include "includes.h"
Damien Miller4af51302000-04-16 11:18:38 +100019RCSID("$Id: clientloop.c,v 1.11 2000/04/16 01:18:41 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100020
21#include "xmalloc.h"
22#include "ssh.h"
23#include "packet.h"
24#include "buffer.h"
25#include "authfd.h"
Damien Miller5ce662a1999-11-11 17:57:39 +110026#include "readconf.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Miller1383bd82000-04-06 12:32:37 +100028#include "ssh2.h"
Damien Millerb38eff82000-04-01 11:09:21 +100029#include "compat.h"
30#include "channels.h"
31#include "dispatch.h"
32
33
Damien Millerd4a8b7e1999-10-27 13:42:43 +100034/* Flag indicating that stdin should be redirected from /dev/null. */
35extern int stdin_null_flag;
36
Damien Miller5428f641999-11-25 11:54:57 +110037/*
38 * Name of the host we are connecting to. This is the name given on the
39 * command line, or the HostName specified for the user-supplied name in a
40 * configuration file.
41 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042extern char *host;
43
Damien Miller5428f641999-11-25 11:54:57 +110044/*
45 * Flag to indicate that we have received a window change signal which has
46 * not yet been processed. This will cause a message indicating the new
47 * window size to be sent to the server a little later. This is volatile
48 * because this is updated in a signal handler.
49 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050static volatile int received_window_change_signal = 0;
51
52/* Terminal modes, as saved by enter_raw_mode. */
53static struct termios saved_tio;
54
Damien Miller5428f641999-11-25 11:54:57 +110055/*
56 * Flag indicating whether we are in raw mode. This is used by
57 * enter_raw_mode and leave_raw_mode.
58 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059static int in_raw_mode = 0;
60
61/* Flag indicating whether the user\'s terminal is in non-blocking mode. */
62static int in_non_blocking_mode = 0;
63
64/* Common data for the client loop code. */
Damien Miller95def091999-11-25 00:26:21 +110065static int escape_pending; /* Last character was the escape character */
66static int last_was_cr; /* Last character was a newline. */
67static int exit_status; /* Used to store the exit status of the command. */
68static int stdin_eof; /* EOF has been encountered on standard error. */
69static Buffer stdin_buffer; /* Buffer for stdin data. */
70static Buffer stdout_buffer; /* Buffer for stdout data. */
71static Buffer stderr_buffer; /* Buffer for stderr data. */
72static unsigned int buffer_high;/* Soft max buffer size. */
73static int max_fd; /* Maximum file descriptor number in select(). */
74static int connection_in; /* Connection to server (input). */
75static int connection_out; /* Connection to server (output). */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076static unsigned long stdin_bytes, stdout_bytes, stderr_bytes;
Damien Miller95def091999-11-25 00:26:21 +110077static int quit_pending; /* Set to non-zero to quit the client loop. */
78static int escape_char; /* Escape character. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
Damien Miller1383bd82000-04-06 12:32:37 +100080
81void client_init_dispatch(void);
82int session_ident = -1;
83
Damien Miller5428f641999-11-25 11:54:57 +110084/* Returns the user\'s terminal to normal mode if it had been put in raw mode. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Damien Miller4af51302000-04-16 11:18:38 +100086void
Damien Miller95def091999-11-25 00:26:21 +110087leave_raw_mode()
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088{
Damien Miller95def091999-11-25 00:26:21 +110089 if (!in_raw_mode)
90 return;
91 in_raw_mode = 0;
92 if (tcsetattr(fileno(stdin), TCSADRAIN, &saved_tio) < 0)
93 perror("tcsetattr");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094
Damien Miller95def091999-11-25 00:26:21 +110095 fatal_remove_cleanup((void (*) (void *)) leave_raw_mode, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096}
97
98/* Puts the user\'s terminal in raw mode. */
99
Damien Miller4af51302000-04-16 11:18:38 +1000100void
Damien Miller95def091999-11-25 00:26:21 +1100101enter_raw_mode()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102{
Damien Miller95def091999-11-25 00:26:21 +1100103 struct termios tio;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104
Damien Miller95def091999-11-25 00:26:21 +1100105 if (tcgetattr(fileno(stdin), &tio) < 0)
106 perror("tcgetattr");
107 saved_tio = tio;
108 tio.c_iflag |= IGNPAR;
109 tio.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF);
110 tio.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111#ifdef IEXTEN
Damien Miller95def091999-11-25 00:26:21 +1100112 tio.c_lflag &= ~IEXTEN;
113#endif /* IEXTEN */
114 tio.c_oflag &= ~OPOST;
115 tio.c_cc[VMIN] = 1;
116 tio.c_cc[VTIME] = 0;
117 if (tcsetattr(fileno(stdin), TCSADRAIN, &tio) < 0)
118 perror("tcsetattr");
119 in_raw_mode = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120
Damien Miller95def091999-11-25 00:26:21 +1100121 fatal_add_cleanup((void (*) (void *)) leave_raw_mode, NULL);
122}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123
124/* Restores stdin to blocking mode. */
125
Damien Miller4af51302000-04-16 11:18:38 +1000126void
Damien Miller95def091999-11-25 00:26:21 +1100127leave_non_blocking()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128{
Damien Miller95def091999-11-25 00:26:21 +1100129 if (in_non_blocking_mode) {
130 (void) fcntl(fileno(stdin), F_SETFL, 0);
131 in_non_blocking_mode = 0;
132 fatal_remove_cleanup((void (*) (void *)) leave_non_blocking, NULL);
133 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134}
135
Damien Miller95def091999-11-25 00:26:21 +1100136/* Puts stdin terminal in non-blocking mode. */
137
Damien Miller4af51302000-04-16 11:18:38 +1000138void
Damien Miller95def091999-11-25 00:26:21 +1100139enter_non_blocking()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140{
Damien Miller95def091999-11-25 00:26:21 +1100141 in_non_blocking_mode = 1;
142 (void) fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
143 fatal_add_cleanup((void (*) (void *)) leave_non_blocking, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144}
145
Damien Miller5428f641999-11-25 11:54:57 +1100146/*
147 * Signal handler for the window change signal (SIGWINCH). This just sets a
148 * flag indicating that the window has changed.
149 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150
Damien Miller4af51302000-04-16 11:18:38 +1000151void
Damien Miller95def091999-11-25 00:26:21 +1100152window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153{
Damien Miller95def091999-11-25 00:26:21 +1100154 received_window_change_signal = 1;
155 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156}
157
Damien Miller5428f641999-11-25 11:54:57 +1100158/*
159 * Signal handler for signals that cause the program to terminate. These
160 * signals must be trapped to restore terminal modes.
161 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162
Damien Miller4af51302000-04-16 11:18:38 +1000163void
Damien Miller95def091999-11-25 00:26:21 +1100164signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165{
Damien Miller95def091999-11-25 00:26:21 +1100166 if (in_raw_mode)
167 leave_raw_mode();
168 if (in_non_blocking_mode)
169 leave_non_blocking();
170 channel_stop_listening();
171 packet_close();
172 fatal("Killed by signal %d.", sig);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173}
174
Damien Miller5428f641999-11-25 11:54:57 +1100175/*
176 * Returns current time in seconds from Jan 1, 1970 with the maximum
177 * available resolution.
178 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179
Damien Miller4af51302000-04-16 11:18:38 +1000180double
Damien Miller95def091999-11-25 00:26:21 +1100181get_current_time()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182{
Damien Miller95def091999-11-25 00:26:21 +1100183 struct timeval tv;
184 gettimeofday(&tv, NULL);
185 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000186}
187
Damien Miller5428f641999-11-25 11:54:57 +1100188/*
189 * This is called when the interactive is entered. This checks if there is
190 * an EOF coming on stdin. We must check this explicitly, as select() does
191 * not appear to wake up when redirecting from /dev/null.
192 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193
Damien Miller4af51302000-04-16 11:18:38 +1000194void
Damien Miller95def091999-11-25 00:26:21 +1100195client_check_initial_eof_on_stdin()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000196{
Damien Miller95def091999-11-25 00:26:21 +1100197 int len;
198 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199
Damien Miller5428f641999-11-25 11:54:57 +1100200 /*
201 * If standard input is to be "redirected from /dev/null", we simply
202 * mark that we have seen an EOF and send an EOF message to the
203 * server. Otherwise, we try to read a single character; it appears
204 * that for some files, such /dev/null, select() never wakes up for
205 * read for this descriptor, which means that we never get EOF. This
206 * way we will get the EOF if stdin comes from /dev/null or similar.
207 */
Damien Miller95def091999-11-25 00:26:21 +1100208 if (stdin_null_flag) {
209 /* Fake EOF on stdin. */
210 debug("Sending eof.");
211 stdin_eof = 1;
212 packet_start(SSH_CMSG_EOF);
213 packet_send();
214 } else {
Damien Miller95def091999-11-25 00:26:21 +1100215 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000216
Damien Miller95def091999-11-25 00:26:21 +1100217 /* Check for immediate EOF on stdin. */
218 len = read(fileno(stdin), buf, 1);
219 if (len == 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100220 /* EOF. Record that we have seen it and send EOF to server. */
Damien Miller95def091999-11-25 00:26:21 +1100221 debug("Sending eof.");
222 stdin_eof = 1;
223 packet_start(SSH_CMSG_EOF);
224 packet_send();
225 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100226 /*
227 * Got data. We must store the data in the buffer,
228 * and also process it as an escape character if
229 * appropriate.
230 */
Damien Miller95def091999-11-25 00:26:21 +1100231 if ((unsigned char) buf[0] == escape_char)
232 escape_pending = 1;
233 else {
234 buffer_append(&stdin_buffer, buf, 1);
235 stdin_bytes += 1;
236 }
237 }
Damien Miller95def091999-11-25 00:26:21 +1100238 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240}
241
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242
Damien Miller5428f641999-11-25 11:54:57 +1100243/*
244 * Make packets from buffered stdin data, and buffer them for sending to the
245 * connection.
246 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247
Damien Miller4af51302000-04-16 11:18:38 +1000248void
Damien Miller95def091999-11-25 00:26:21 +1100249client_make_packets_from_stdin_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250{
Damien Miller95def091999-11-25 00:26:21 +1100251 unsigned int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252
Damien Miller95def091999-11-25 00:26:21 +1100253 /* Send buffered stdin data to the server. */
254 while (buffer_len(&stdin_buffer) > 0 &&
255 packet_not_very_much_data_to_write()) {
256 len = buffer_len(&stdin_buffer);
257 /* Keep the packets at reasonable size. */
258 if (len > packet_get_maxsize())
259 len = packet_get_maxsize();
260 packet_start(SSH_CMSG_STDIN_DATA);
261 packet_put_string(buffer_ptr(&stdin_buffer), len);
262 packet_send();
263 buffer_consume(&stdin_buffer, len);
264 /* If we have a pending EOF, send it now. */
265 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
266 packet_start(SSH_CMSG_EOF);
267 packet_send();
268 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000269 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270}
271
Damien Miller5428f641999-11-25 11:54:57 +1100272/*
273 * Checks if the client window has changed, and sends a packet about it to
274 * the server if so. The actual change is detected elsewhere (by a software
275 * interrupt on Unix); this just checks the flag and sends a message if
276 * appropriate.
277 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000278
Damien Miller4af51302000-04-16 11:18:38 +1000279void
Damien Miller95def091999-11-25 00:26:21 +1100280client_check_window_change()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000281{
Damien Miller1383bd82000-04-06 12:32:37 +1000282 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000283
Damien Miller1383bd82000-04-06 12:32:37 +1000284 if (! received_window_change_signal)
285 return;
286 /** XXX race */
287 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288
Damien Miller1383bd82000-04-06 12:32:37 +1000289 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
290 return;
291
292 debug("client_check_window_change: changed");
293
294 if (compat20) {
295 channel_request_start(session_ident, "window-change", 0);
296 packet_put_int(ws.ws_col);
297 packet_put_int(ws.ws_row);
298 packet_put_int(ws.ws_xpixel);
299 packet_put_int(ws.ws_ypixel);
300 packet_send();
301 } else {
302 packet_start(SSH_CMSG_WINDOW_SIZE);
303 packet_put_int(ws.ws_row);
304 packet_put_int(ws.ws_col);
305 packet_put_int(ws.ws_xpixel);
306 packet_put_int(ws.ws_ypixel);
307 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000308 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000309}
310
Damien Miller5428f641999-11-25 11:54:57 +1100311/*
312 * Waits until the client can do something (some data becomes available on
313 * one of the file descriptors).
314 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000315
Damien Miller4af51302000-04-16 11:18:38 +1000316void
Damien Miller95def091999-11-25 00:26:21 +1100317client_wait_until_can_do_something(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000318{
Damien Miller1383bd82000-04-06 12:32:37 +1000319 /*debug("client_wait_until_can_do_something"); */
320
Damien Miller95def091999-11-25 00:26:21 +1100321 /* Initialize select masks. */
322 FD_ZERO(readset);
Damien Miller95def091999-11-25 00:26:21 +1100323 FD_ZERO(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000324
Damien Miller1383bd82000-04-06 12:32:37 +1000325 if (!compat20) {
326 /* Read from the connection, unless our buffers are full. */
327 if (buffer_len(&stdout_buffer) < buffer_high &&
328 buffer_len(&stderr_buffer) < buffer_high &&
329 channel_not_very_much_buffered_data())
330 FD_SET(connection_in, readset);
331 /*
332 * Read from stdin, unless we have seen EOF or have very much
333 * buffered data to send to the server.
334 */
335 if (!stdin_eof && packet_not_very_much_data_to_write())
336 FD_SET(fileno(stdin), readset);
337
338 /* Select stdout/stderr if have data in buffer. */
339 if (buffer_len(&stdout_buffer) > 0)
340 FD_SET(fileno(stdout), writeset);
341 if (buffer_len(&stderr_buffer) > 0)
342 FD_SET(fileno(stderr), writeset);
343 } else {
344 FD_SET(connection_in, readset);
345 }
346
Damien Miller95def091999-11-25 00:26:21 +1100347 /* Add any selections by the channel mechanism. */
348 channel_prepare_select(readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349
Damien Miller95def091999-11-25 00:26:21 +1100350 /* Select server connection if have data to write to the server. */
351 if (packet_have_data_to_write())
352 FD_SET(connection_out, writeset);
353
Damien Miller1383bd82000-04-06 12:32:37 +1000354/* move UP XXX */
Damien Miller95def091999-11-25 00:26:21 +1100355 /* Update maximum file descriptor number, if appropriate. */
356 if (channel_max_fd() > max_fd)
357 max_fd = channel_max_fd();
358
Damien Miller5428f641999-11-25 11:54:57 +1100359 /*
360 * Wait for something to happen. This will suspend the process until
361 * some selected descriptor can be read, written, or has some other
362 * event pending. Note: if you want to implement SSH_MSG_IGNORE
363 * messages to fool traffic analysis, this might be the place to do
364 * it: just have a random timeout for the select, and send a random
365 * SSH_MSG_IGNORE packet when the timeout expires.
366 */
Damien Miller95def091999-11-25 00:26:21 +1100367
368 if (select(max_fd + 1, readset, writeset, NULL, NULL) < 0) {
369 char buf[100];
370 /* Some systems fail to clear these automatically. */
371 FD_ZERO(readset);
372 FD_ZERO(writeset);
373 if (errno == EINTR)
374 return;
375 /* Note: we might still have data in the buffers. */
376 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
377 buffer_append(&stderr_buffer, buf, strlen(buf));
378 stderr_bytes += strlen(buf);
379 quit_pending = 1;
380 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000381}
382
Damien Miller4af51302000-04-16 11:18:38 +1000383void
Damien Miller95def091999-11-25 00:26:21 +1100384client_suspend_self()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000385{
Damien Miller95def091999-11-25 00:26:21 +1100386 struct winsize oldws, newws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000387
Damien Miller95def091999-11-25 00:26:21 +1100388 /* Flush stdout and stderr buffers. */
389 if (buffer_len(&stdout_buffer) > 0)
Damien Miller037a0dc1999-12-07 15:38:31 +1100390 atomicio(write, fileno(stdout), buffer_ptr(&stdout_buffer),
391 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100392 if (buffer_len(&stderr_buffer) > 0)
Damien Miller037a0dc1999-12-07 15:38:31 +1100393 atomicio(write, fileno(stderr), buffer_ptr(&stderr_buffer),
394 buffer_len(&stderr_buffer));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000395
Damien Miller95def091999-11-25 00:26:21 +1100396 leave_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000397
Damien Miller5428f641999-11-25 11:54:57 +1100398 /*
399 * Free (and clear) the buffer to reduce the amount of data that gets
400 * written to swap.
401 */
Damien Miller95def091999-11-25 00:26:21 +1100402 buffer_free(&stdin_buffer);
403 buffer_free(&stdout_buffer);
404 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000405
Damien Miller95def091999-11-25 00:26:21 +1100406 /* Save old window size. */
407 ioctl(fileno(stdin), TIOCGWINSZ, &oldws);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000408
Damien Miller95def091999-11-25 00:26:21 +1100409 /* Send the suspend signal to the program itself. */
410 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000411
Damien Miller95def091999-11-25 00:26:21 +1100412 /* Check if the window size has changed. */
413 if (ioctl(fileno(stdin), TIOCGWINSZ, &newws) >= 0 &&
414 (oldws.ws_row != newws.ws_row ||
415 oldws.ws_col != newws.ws_col ||
416 oldws.ws_xpixel != newws.ws_xpixel ||
417 oldws.ws_ypixel != newws.ws_ypixel))
418 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000419
Damien Miller95def091999-11-25 00:26:21 +1100420 /* OK, we have been continued by the user. Reinitialize buffers. */
421 buffer_init(&stdin_buffer);
422 buffer_init(&stdout_buffer);
423 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000424
Damien Miller95def091999-11-25 00:26:21 +1100425 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000426}
427
Damien Miller4af51302000-04-16 11:18:38 +1000428void
Damien Miller1383bd82000-04-06 12:32:37 +1000429client_process_net_input(fd_set * readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430{
Damien Miller1383bd82000-04-06 12:32:37 +1000431 int len;
432 char buf[8192];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433
Damien Miller5428f641999-11-25 11:54:57 +1100434 /*
435 * Read input from the server, and add any such data to the buffer of
436 * the packet subsystem.
437 */
Damien Miller95def091999-11-25 00:26:21 +1100438 if (FD_ISSET(connection_in, readset)) {
439 /* Read as much as possible. */
440 len = read(connection_in, buf, sizeof(buf));
Damien Miller1383bd82000-04-06 12:32:37 +1000441/*debug("read connection_in len %d", len); XXX */
Damien Miller95def091999-11-25 00:26:21 +1100442 if (len == 0) {
443 /* Received EOF. The remote host has closed the connection. */
444 snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n",
445 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000446 buffer_append(&stderr_buffer, buf, strlen(buf));
447 stderr_bytes += strlen(buf);
448 quit_pending = 1;
449 return;
Damien Miller95def091999-11-25 00:26:21 +1100450 }
Damien Miller5428f641999-11-25 11:54:57 +1100451 /*
452 * There is a kernel bug on Solaris that causes select to
453 * sometimes wake up even though there is no data available.
454 */
Damien Miller95def091999-11-25 00:26:21 +1100455 if (len < 0 && errno == EAGAIN)
456 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457
Damien Miller95def091999-11-25 00:26:21 +1100458 if (len < 0) {
459 /* An error has encountered. Perhaps there is a network problem. */
460 snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n",
461 host, strerror(errno));
462 buffer_append(&stderr_buffer, buf, strlen(buf));
463 stderr_bytes += strlen(buf);
464 quit_pending = 1;
465 return;
466 }
467 packet_process_incoming(buf, len);
468 }
Damien Miller1383bd82000-04-06 12:32:37 +1000469}
470
Damien Miller4af51302000-04-16 11:18:38 +1000471void
Damien Miller1383bd82000-04-06 12:32:37 +1000472client_process_input(fd_set * readset)
473{
474 int len, pid;
475 char buf[8192], *s;
476
Damien Miller95def091999-11-25 00:26:21 +1100477 /* Read input from stdin. */
478 if (FD_ISSET(fileno(stdin), readset)) {
479 /* Read as much as possible. */
480 len = read(fileno(stdin), buf, sizeof(buf));
481 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100482 /*
483 * Received EOF or error. They are treated
484 * similarly, except that an error message is printed
485 * if it was an error condition.
486 */
Damien Miller95def091999-11-25 00:26:21 +1100487 if (len < 0) {
488 snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno));
489 buffer_append(&stderr_buffer, buf, strlen(buf));
490 stderr_bytes += strlen(buf);
491 }
492 /* Mark that we have seen EOF. */
493 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +1100494 /*
495 * Send an EOF message to the server unless there is
496 * data in the buffer. If there is data in the
497 * buffer, no message will be sent now. Code
498 * elsewhere will send the EOF when the buffer
499 * becomes empty if stdin_eof is set.
500 */
Damien Miller95def091999-11-25 00:26:21 +1100501 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000502 packet_start(SSH_CMSG_EOF);
503 packet_send();
Damien Miller95def091999-11-25 00:26:21 +1100504 }
505 } else if (escape_char == -1) {
Damien Miller5428f641999-11-25 11:54:57 +1100506 /*
507 * Normal successful read, and no escape character.
508 * Just append the data to buffer.
509 */
Damien Miller95def091999-11-25 00:26:21 +1100510 buffer_append(&stdin_buffer, buf, len);
511 stdin_bytes += len;
512 } else {
Damien Miller5428f641999-11-25 11:54:57 +1100513 /*
514 * Normal, successful read. But we have an escape character
515 * and have to process the characters one by one.
516 */
Damien Miller95def091999-11-25 00:26:21 +1100517 unsigned int i;
518 for (i = 0; i < len; i++) {
519 unsigned char ch;
520 /* Get one character at a time. */
521 ch = buf[i];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000522
Damien Miller95def091999-11-25 00:26:21 +1100523 if (escape_pending) {
524 /* We have previously seen an escape character. */
525 /* Clear the flag now. */
526 escape_pending = 0;
527 /* Process the escaped character. */
528 switch (ch) {
529 case '.':
530 /* Terminate the connection. */
531 snprintf(buf, sizeof buf, "%c.\r\n", escape_char);
532 buffer_append(&stderr_buffer, buf, strlen(buf));
533 stderr_bytes += strlen(buf);
534 quit_pending = 1;
535 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000536
Damien Miller95def091999-11-25 00:26:21 +1100537 case 'Z' - 64:
538 /* Suspend the program. */
539 /* Print a message to that effect to the user. */
540 snprintf(buf, sizeof buf, "%c^Z\r\n", escape_char);
541 buffer_append(&stderr_buffer, buf, strlen(buf));
542 stderr_bytes += strlen(buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000543
Damien Miller95def091999-11-25 00:26:21 +1100544 /* Restore terminal modes and suspend. */
545 client_suspend_self();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000546
Damien Miller95def091999-11-25 00:26:21 +1100547 /* We have been continued. */
548 continue;
549
550 case '&':
Damien Miller5428f641999-11-25 11:54:57 +1100551 /*
552 * Detach the program (continue to serve connections,
553 * but put in background and no more new connections).
554 */
Damien Miller95def091999-11-25 00:26:21 +1100555 if (!stdin_eof) {
Damien Miller5428f641999-11-25 11:54:57 +1100556 /*
557 * Sending SSH_CMSG_EOF alone does not always appear
558 * to be enough. So we try to send an EOF character
559 * first.
560 */
Damien Miller95def091999-11-25 00:26:21 +1100561 packet_start(SSH_CMSG_STDIN_DATA);
562 packet_put_string("\004", 1);
563 packet_send();
564 /* Close stdin. */
565 stdin_eof = 1;
566 if (buffer_len(&stdin_buffer) == 0) {
567 packet_start(SSH_CMSG_EOF);
568 packet_send();
569 }
570 }
571 /* Restore tty modes. */
572 leave_raw_mode();
573
574 /* Stop listening for new connections. */
575 channel_stop_listening();
576
577 printf("%c& [backgrounded]\n", escape_char);
578
579 /* Fork into background. */
580 pid = fork();
581 if (pid < 0) {
582 error("fork: %.100s", strerror(errno));
583 continue;
584 }
585 if (pid != 0) { /* This is the parent. */
586 /* The parent just exits. */
587 exit(0);
588 }
589 /* The child continues serving connections. */
590 continue;
591
592 case '?':
593 snprintf(buf, sizeof buf,
594"%c?\r\n\
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000595Supported escape sequences:\r\n\
596~. - terminate connection\r\n\
597~^Z - suspend ssh\r\n\
598~# - list forwarded connections\r\n\
599~& - background ssh (when waiting for connections to terminate)\r\n\
600~? - this message\r\n\
601~~ - send the escape character by typing it twice\r\n\
602(Note that escapes are only recognized immediately after newline.)\r\n",
Damien Miller95def091999-11-25 00:26:21 +1100603 escape_char);
604 buffer_append(&stderr_buffer, buf, strlen(buf));
605 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000606
Damien Miller95def091999-11-25 00:26:21 +1100607 case '#':
608 snprintf(buf, sizeof buf, "%c#\r\n", escape_char);
609 buffer_append(&stderr_buffer, buf, strlen(buf));
610 s = channel_open_message();
611 buffer_append(&stderr_buffer, s, strlen(s));
612 xfree(s);
613 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000614
Damien Miller95def091999-11-25 00:26:21 +1100615 default:
616 if (ch != escape_char) {
Damien Miller5428f641999-11-25 11:54:57 +1100617 /*
618 * Escape character followed by non-special character.
619 * Append both to the input buffer.
620 */
Damien Miller95def091999-11-25 00:26:21 +1100621 buf[0] = escape_char;
622 buf[1] = ch;
623 buffer_append(&stdin_buffer, buf, 2);
624 stdin_bytes += 2;
625 continue;
626 }
Damien Miller5428f641999-11-25 11:54:57 +1100627 /*
628 * Note that escape character typed twice
629 * falls through here; the latter gets processed
630 * as a normal character below.
631 */
Damien Miller95def091999-11-25 00:26:21 +1100632 break;
633 }
634 } else {
Damien Miller5428f641999-11-25 11:54:57 +1100635 /*
636 * The previous character was not an escape char. Check if this
637 * is an escape.
638 */
Damien Miller95def091999-11-25 00:26:21 +1100639 if (last_was_cr && ch == escape_char) {
640 /* It is. Set the flag and continue to next character. */
641 escape_pending = 1;
642 continue;
643 }
644 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000645
Damien Miller5428f641999-11-25 11:54:57 +1100646 /*
647 * Normal character. Record whether it was a newline,
648 * and append it to the buffer.
649 */
Damien Miller95def091999-11-25 00:26:21 +1100650 last_was_cr = (ch == '\r' || ch == '\n');
651 buf[0] = ch;
652 buffer_append(&stdin_buffer, buf, 1);
653 stdin_bytes += 1;
654 continue;
655 }
656 }
657 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000658}
659
Damien Miller4af51302000-04-16 11:18:38 +1000660void
Damien Miller95def091999-11-25 00:26:21 +1100661client_process_output(fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000662{
Damien Miller95def091999-11-25 00:26:21 +1100663 int len;
664 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000665
Damien Miller95def091999-11-25 00:26:21 +1100666 /* Write buffered output to stdout. */
667 if (FD_ISSET(fileno(stdout), writeset)) {
668 /* Write as much data as possible. */
669 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100670 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100671 if (len <= 0) {
672 if (errno == EAGAIN)
673 len = 0;
674 else {
Damien Miller5428f641999-11-25 11:54:57 +1100675 /*
676 * An error or EOF was encountered. Put an
677 * error message to stderr buffer.
678 */
Damien Miller95def091999-11-25 00:26:21 +1100679 snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno));
680 buffer_append(&stderr_buffer, buf, strlen(buf));
681 stderr_bytes += strlen(buf);
682 quit_pending = 1;
683 return;
684 }
685 }
686 /* Consume printed data from the buffer. */
687 buffer_consume(&stdout_buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000688 }
Damien Miller95def091999-11-25 00:26:21 +1100689 /* Write buffered output to stderr. */
690 if (FD_ISSET(fileno(stderr), writeset)) {
691 /* Write as much data as possible. */
692 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100693 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100694 if (len <= 0) {
695 if (errno == EAGAIN)
696 len = 0;
697 else {
Damien Miller5428f641999-11-25 11:54:57 +1100698 /* EOF or error, but can't even print error message. */
Damien Miller95def091999-11-25 00:26:21 +1100699 quit_pending = 1;
700 return;
701 }
702 }
703 /* Consume printed characters from the buffer. */
704 buffer_consume(&stderr_buffer, len);
705 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000706}
707
Damien Miller5428f641999-11-25 11:54:57 +1100708/*
Damien Millerb38eff82000-04-01 11:09:21 +1000709 * Get packets from the connection input buffer, and process them as long as
710 * there are packets available.
711 *
712 * Any unknown packets received during the actual
713 * session cause the session to terminate. This is
714 * intended to make debugging easier since no
715 * confirmations are sent. Any compatible protocol
716 * extensions must be negotiated during the
717 * preparatory phase.
718 */
719
Damien Miller4af51302000-04-16 11:18:38 +1000720void
Damien Millerb38eff82000-04-01 11:09:21 +1000721client_process_buffered_input_packets()
722{
723 dispatch_run(DISPATCH_NONBLOCK, &quit_pending);
724}
725
726/*
Damien Miller5428f641999-11-25 11:54:57 +1100727 * Implements the interactive session with the server. This is called after
728 * the user has been authenticated, and a command has been started on the
729 * remote host. If escape_char != -1, it is the character used as an escape
730 * character for terminating or suspending the session.
731 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000732
Damien Miller4af51302000-04-16 11:18:38 +1000733int
Damien Miller95def091999-11-25 00:26:21 +1100734client_loop(int have_pty, int escape_char_arg)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000735{
Damien Miller95def091999-11-25 00:26:21 +1100736 extern Options options;
737 double start_time, total_time;
738 int len;
739 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000740
Damien Miller95def091999-11-25 00:26:21 +1100741 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000742
Damien Miller95def091999-11-25 00:26:21 +1100743 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000744
Damien Miller95def091999-11-25 00:26:21 +1100745 /* Initialize variables. */
746 escape_pending = 0;
747 last_was_cr = 1;
748 exit_status = -1;
749 stdin_eof = 0;
750 buffer_high = 64 * 1024;
751 connection_in = packet_get_connection_in();
752 connection_out = packet_get_connection_out();
753 max_fd = connection_in;
754 if (connection_out > max_fd)
755 max_fd = connection_out;
756 stdin_bytes = 0;
757 stdout_bytes = 0;
758 stderr_bytes = 0;
759 quit_pending = 0;
760 escape_char = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000761
Damien Miller95def091999-11-25 00:26:21 +1100762 /* Initialize buffers. */
763 buffer_init(&stdin_buffer);
764 buffer_init(&stdout_buffer);
765 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000766
Damien Millerb38eff82000-04-01 11:09:21 +1000767 client_init_dispatch();
768
Damien Miller95def091999-11-25 00:26:21 +1100769 /* Set signal handlers to restore non-blocking mode. */
770 signal(SIGINT, signal_handler);
771 signal(SIGQUIT, signal_handler);
772 signal(SIGTERM, signal_handler);
773 signal(SIGPIPE, SIG_IGN);
774 if (have_pty)
775 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000776
Damien Miller95def091999-11-25 00:26:21 +1100777 if (have_pty)
778 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000779
Damien Miller95def091999-11-25 00:26:21 +1100780 /* Check if we should immediately send of on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +1000781 if (!compat20)
782 client_check_initial_eof_on_stdin();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000783
Damien Miller95def091999-11-25 00:26:21 +1100784 /* Main loop of the client for the interactive session mode. */
785 while (!quit_pending) {
786 fd_set readset, writeset;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000787
Damien Miller5428f641999-11-25 11:54:57 +1100788 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +1100789 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000790
Damien Miller1383bd82000-04-06 12:32:37 +1000791 if (compat20 && !channel_still_open()) {
792 debug("!channel_still_open.");
793 break;
794 }
795
Damien Miller5428f641999-11-25 11:54:57 +1100796 /*
797 * Make packets of buffered stdin data, and buffer them for
798 * sending to the server.
799 */
Damien Miller1383bd82000-04-06 12:32:37 +1000800 if (!compat20)
801 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000802
Damien Miller5428f641999-11-25 11:54:57 +1100803 /*
804 * Make packets from buffered channel data, and buffer them
805 * for sending to the server.
806 */
Damien Miller95def091999-11-25 00:26:21 +1100807 if (packet_not_very_much_data_to_write())
808 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000809
Damien Miller5428f641999-11-25 11:54:57 +1100810 /*
811 * Check if the window size has changed, and buffer a message
812 * about it to the server if so.
813 */
Damien Miller95def091999-11-25 00:26:21 +1100814 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000815
Damien Miller95def091999-11-25 00:26:21 +1100816 if (quit_pending)
817 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000818
Damien Miller5428f641999-11-25 11:54:57 +1100819 /*
820 * Wait until we have something to do (something becomes
821 * available on one of the descriptors).
822 */
Damien Miller95def091999-11-25 00:26:21 +1100823 client_wait_until_can_do_something(&readset, &writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000824
Damien Miller95def091999-11-25 00:26:21 +1100825 if (quit_pending)
826 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000827
Damien Miller95def091999-11-25 00:26:21 +1100828 /* Do channel operations. */
829 channel_after_select(&readset, &writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000830
Damien Miller1383bd82000-04-06 12:32:37 +1000831 /* Buffer input from the connection. */
832 client_process_net_input(&readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000833
Damien Miller1383bd82000-04-06 12:32:37 +1000834 if (quit_pending)
835 break;
836
837 if (!compat20) {
838 /* Buffer data from stdin */
839 client_process_input(&readset);
840 /*
841 * Process output to stdout and stderr. Output to
842 * the connection is processed elsewhere (above).
843 */
844 client_process_output(&writeset);
845 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000846
Damien Miller5428f641999-11-25 11:54:57 +1100847 /* Send as much buffered packet data as possible to the sender. */
Damien Miller95def091999-11-25 00:26:21 +1100848 if (FD_ISSET(connection_out, &writeset))
849 packet_write_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000850 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000851
Damien Miller95def091999-11-25 00:26:21 +1100852 /* Terminate the session. */
853
854 /* Stop watching for window change. */
855 if (have_pty)
856 signal(SIGWINCH, SIG_DFL);
857
858 /* Stop listening for connections. */
859 channel_stop_listening();
860
Damien Miller5428f641999-11-25 11:54:57 +1100861 /*
862 * In interactive mode (with pseudo tty) display a message indicating
863 * that the connection has been closed.
864 */
Damien Miller95def091999-11-25 00:26:21 +1100865 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
866 snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host);
867 buffer_append(&stderr_buffer, buf, strlen(buf));
868 stderr_bytes += strlen(buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000869 }
Damien Miller95def091999-11-25 00:26:21 +1100870 /* Output any buffered data for stdout. */
871 while (buffer_len(&stdout_buffer) > 0) {
872 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100873 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100874 if (len <= 0) {
875 error("Write failed flushing stdout buffer.");
876 break;
877 }
878 buffer_consume(&stdout_buffer, len);
879 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000880
Damien Miller95def091999-11-25 00:26:21 +1100881 /* Output any buffered data for stderr. */
882 while (buffer_len(&stderr_buffer) > 0) {
883 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100884 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100885 if (len <= 0) {
886 error("Write failed flushing stderr buffer.");
887 break;
888 }
889 buffer_consume(&stderr_buffer, len);
890 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000891
Damien Miller95def091999-11-25 00:26:21 +1100892 if (have_pty)
893 leave_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000894
Damien Miller95def091999-11-25 00:26:21 +1100895 /* Clear and free any buffers. */
896 memset(buf, 0, sizeof(buf));
897 buffer_free(&stdin_buffer);
898 buffer_free(&stdout_buffer);
899 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000900
Damien Miller95def091999-11-25 00:26:21 +1100901 /* Report bytes transferred, and transfer rates. */
902 total_time = get_current_time() - start_time;
903 debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds",
904 stdin_bytes, stdout_bytes, stderr_bytes, total_time);
905 if (total_time > 0)
906 debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f",
907 stdin_bytes / total_time, stdout_bytes / total_time,
908 stderr_bytes / total_time);
909
910 /* Return the exit status of the program. */
911 debug("Exit status %d", exit_status);
912 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000913}
Damien Millerb38eff82000-04-01 11:09:21 +1000914
915/*********/
916
917void
918client_input_stdout_data(int type, int plen)
919{
920 unsigned int data_len;
921 char *data = packet_get_string(&data_len);
922 packet_integrity_check(plen, 4 + data_len, type);
923 buffer_append(&stdout_buffer, data, data_len);
924 stdout_bytes += data_len;
925 memset(data, 0, data_len);
926 xfree(data);
927}
928void
929client_input_stderr_data(int type, int plen)
930{
931 unsigned int data_len;
932 char *data = packet_get_string(&data_len);
933 packet_integrity_check(plen, 4 + data_len, type);
934 buffer_append(&stderr_buffer, data, data_len);
935 stdout_bytes += data_len;
936 memset(data, 0, data_len);
937 xfree(data);
938}
939void
940client_input_exit_status(int type, int plen)
941{
942 packet_integrity_check(plen, 4, type);
943 exit_status = packet_get_int();
944 /* Acknowledge the exit. */
945 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
946 packet_send();
947 /*
948 * Must wait for packet to be sent since we are
949 * exiting the loop.
950 */
951 packet_write_wait();
952 /* Flag that we want to exit. */
953 quit_pending = 1;
954}
955
Damien Miller4af51302000-04-16 11:18:38 +1000956void
Damien Miller1383bd82000-04-06 12:32:37 +1000957client_init_dispatch_20()
958{
959 dispatch_init(&dispatch_protocol_error);
960 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
961 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
962 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
963 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
964 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
965 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
966 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &channel_input_channel_request);
967 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
968}
Damien Miller4af51302000-04-16 11:18:38 +1000969void
Damien Millerb38eff82000-04-01 11:09:21 +1000970client_init_dispatch_13()
971{
972 dispatch_init(NULL);
973 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
974 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
975 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
976 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
977 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
978 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
979 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
980 dispatch_set(SSH_SMSG_AGENT_OPEN, &auth_input_open_request);
981 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
982 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
983 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
984 dispatch_set(SSH_SMSG_X11_OPEN, &x11_input_open);
985}
Damien Miller4af51302000-04-16 11:18:38 +1000986void
Damien Millerb38eff82000-04-01 11:09:21 +1000987client_init_dispatch_15()
988{
989 client_init_dispatch_13();
990 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
991 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
992}
Damien Miller4af51302000-04-16 11:18:38 +1000993void
Damien Millerb38eff82000-04-01 11:09:21 +1000994client_init_dispatch()
995{
Damien Miller1383bd82000-04-06 12:32:37 +1000996 if (compat20)
997 client_init_dispatch_20();
998 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +1000999 client_init_dispatch_13();
1000 else
1001 client_init_dispatch_15();
1002}
Damien Miller1383bd82000-04-06 12:32:37 +10001003
1004void
1005client_input_channel_req(int id, void *arg)
1006{
1007 Channel *c = NULL;
1008 unsigned int len;
1009 int success = 0;
1010 int reply;
1011 char *rtype;
1012
1013 rtype = packet_get_string(&len);
1014 reply = packet_get_char();
1015
Damien Miller78928792000-04-12 20:17:38 +10001016 debug("session_input_channel_req: rtype %s reply %d", rtype, reply);
Damien Miller1383bd82000-04-06 12:32:37 +10001017
1018 c = channel_lookup(id);
1019 if (c == NULL)
1020 fatal("session_input_channel_req: channel %d: bad channel", id);
1021
1022 if (session_ident == -1) {
1023 error("client_input_channel_req: no channel %d", id);
1024 } else if (id != session_ident) {
1025 error("client_input_channel_req: bad channel %d != %d",
1026 id, session_ident);
1027 } else if (strcmp(rtype, "exit-status") == 0) {
1028 success = 1;
1029 exit_status = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001030 packet_done();
Damien Miller1383bd82000-04-06 12:32:37 +10001031 }
1032 if (reply) {
1033 packet_start(success ?
1034 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1035 packet_put_int(c->remote_id);
1036 packet_send();
1037 }
1038 xfree(rtype);
1039}
1040
1041void
1042client_set_session_ident(int id)
1043{
1044 debug("client_set_session_ident: id %d", id);
1045 session_ident = id;
1046 channel_register_callback(id, SSH2_MSG_CHANNEL_REQUEST,
1047 client_input_channel_req, (void *)0);
1048}