blob: d339e1274b474ec1f21a60a8c4ca47143437afa5 [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 Millerad833b32000-08-23 10:46:23 +100019RCSID("$OpenBSD: clientloop.c,v 1.32 2000/08/19 22:21:19 markus 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"
Damien Miller5ce662a1999-11-11 17:57:39 +110025#include "readconf.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100026
Damien Miller1383bd82000-04-06 12:32:37 +100027#include "ssh2.h"
Damien Millerb38eff82000-04-01 11:09:21 +100028#include "compat.h"
29#include "channels.h"
30#include "dispatch.h"
31
Damien Millerad833b32000-08-23 10:46:23 +100032#include "buffer.h"
33#include "bufaux.h"
34
Damien Millerd4a8b7e1999-10-27 13:42:43 +100035/* Flag indicating that stdin should be redirected from /dev/null. */
36extern int stdin_null_flag;
37
Damien Miller5428f641999-11-25 11:54:57 +110038/*
39 * Name of the host we are connecting to. This is the name given on the
40 * command line, or the HostName specified for the user-supplied name in a
41 * configuration file.
42 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043extern char *host;
44
Damien Miller5428f641999-11-25 11:54:57 +110045/*
46 * Flag to indicate that we have received a window change signal which has
47 * not yet been processed. This will cause a message indicating the new
48 * window size to be sent to the server a little later. This is volatile
49 * because this is updated in a signal handler.
50 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051static volatile int received_window_change_signal = 0;
52
53/* Terminal modes, as saved by enter_raw_mode. */
54static struct termios saved_tio;
55
Damien Miller5428f641999-11-25 11:54:57 +110056/*
57 * Flag indicating whether we are in raw mode. This is used by
58 * enter_raw_mode and leave_raw_mode.
59 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060static int in_raw_mode = 0;
61
62/* Flag indicating whether the user\'s terminal is in non-blocking mode. */
63static int in_non_blocking_mode = 0;
64
65/* Common data for the client loop code. */
Damien Millerad833b32000-08-23 10:46:23 +100066static int quit_pending; /* Set to non-zero to quit the client loop. */
67static int escape_char; /* Escape character. */
Damien Miller95def091999-11-25 00:26:21 +110068static int escape_pending; /* Last character was the escape character */
69static int last_was_cr; /* Last character was a newline. */
70static int exit_status; /* Used to store the exit status of the command. */
71static int stdin_eof; /* EOF has been encountered on standard error. */
72static Buffer stdin_buffer; /* Buffer for stdin data. */
73static Buffer stdout_buffer; /* Buffer for stdout data. */
74static Buffer stderr_buffer; /* Buffer for stderr data. */
Damien Millerad833b32000-08-23 10:46:23 +100075static unsigned long stdin_bytes, stdout_bytes, stderr_bytes;
Damien Miller95def091999-11-25 00:26:21 +110076static unsigned int buffer_high;/* Soft max buffer size. */
77static int max_fd; /* Maximum file descriptor number in select(). */
78static int connection_in; /* Connection to server (input). */
79static int connection_out; /* Connection to server (output). */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080
Damien Miller1383bd82000-04-06 12:32:37 +100081
82void client_init_dispatch(void);
83int session_ident = -1;
84
Damien Miller5428f641999-11-25 11:54:57 +110085/* Returns the user\'s terminal to normal mode if it had been put in raw mode. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
Damien Miller4af51302000-04-16 11:18:38 +100087void
Damien Miller95def091999-11-25 00:26:21 +110088leave_raw_mode()
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089{
Damien Miller95def091999-11-25 00:26:21 +110090 if (!in_raw_mode)
91 return;
92 in_raw_mode = 0;
93 if (tcsetattr(fileno(stdin), TCSADRAIN, &saved_tio) < 0)
94 perror("tcsetattr");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095
Damien Miller95def091999-11-25 00:26:21 +110096 fatal_remove_cleanup((void (*) (void *)) leave_raw_mode, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097}
98
99/* Puts the user\'s terminal in raw mode. */
100
Damien Miller4af51302000-04-16 11:18:38 +1000101void
Damien Miller95def091999-11-25 00:26:21 +1100102enter_raw_mode()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103{
Damien Miller95def091999-11-25 00:26:21 +1100104 struct termios tio;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105
Damien Miller95def091999-11-25 00:26:21 +1100106 if (tcgetattr(fileno(stdin), &tio) < 0)
107 perror("tcgetattr");
108 saved_tio = tio;
109 tio.c_iflag |= IGNPAR;
110 tio.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF);
111 tio.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112#ifdef IEXTEN
Damien Miller95def091999-11-25 00:26:21 +1100113 tio.c_lflag &= ~IEXTEN;
114#endif /* IEXTEN */
115 tio.c_oflag &= ~OPOST;
116 tio.c_cc[VMIN] = 1;
117 tio.c_cc[VTIME] = 0;
118 if (tcsetattr(fileno(stdin), TCSADRAIN, &tio) < 0)
119 perror("tcsetattr");
120 in_raw_mode = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121
Damien Miller95def091999-11-25 00:26:21 +1100122 fatal_add_cleanup((void (*) (void *)) leave_raw_mode, NULL);
123}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124
125/* Restores stdin to blocking mode. */
126
Damien Miller4af51302000-04-16 11:18:38 +1000127void
Damien Miller95def091999-11-25 00:26:21 +1100128leave_non_blocking()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129{
Damien Miller95def091999-11-25 00:26:21 +1100130 if (in_non_blocking_mode) {
131 (void) fcntl(fileno(stdin), F_SETFL, 0);
132 in_non_blocking_mode = 0;
133 fatal_remove_cleanup((void (*) (void *)) leave_non_blocking, NULL);
134 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135}
136
Damien Miller95def091999-11-25 00:26:21 +1100137/* Puts stdin terminal in non-blocking mode. */
138
Damien Miller4af51302000-04-16 11:18:38 +1000139void
Damien Miller95def091999-11-25 00:26:21 +1100140enter_non_blocking()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141{
Damien Miller95def091999-11-25 00:26:21 +1100142 in_non_blocking_mode = 1;
143 (void) fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
144 fatal_add_cleanup((void (*) (void *)) leave_non_blocking, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145}
146
Damien Miller5428f641999-11-25 11:54:57 +1100147/*
148 * Signal handler for the window change signal (SIGWINCH). This just sets a
149 * flag indicating that the window has changed.
150 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151
Damien Miller4af51302000-04-16 11:18:38 +1000152void
Damien Miller95def091999-11-25 00:26:21 +1100153window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154{
Damien Miller95def091999-11-25 00:26:21 +1100155 received_window_change_signal = 1;
156 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000157}
158
Damien Miller5428f641999-11-25 11:54:57 +1100159/*
160 * Signal handler for signals that cause the program to terminate. These
161 * signals must be trapped to restore terminal modes.
162 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000163
Damien Miller4af51302000-04-16 11:18:38 +1000164void
Damien Miller95def091999-11-25 00:26:21 +1100165signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166{
Damien Miller95def091999-11-25 00:26:21 +1100167 if (in_raw_mode)
168 leave_raw_mode();
169 if (in_non_blocking_mode)
170 leave_non_blocking();
171 channel_stop_listening();
172 packet_close();
173 fatal("Killed by signal %d.", sig);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000174}
175
Damien Miller5428f641999-11-25 11:54:57 +1100176/*
177 * Returns current time in seconds from Jan 1, 1970 with the maximum
178 * available resolution.
179 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000180
Damien Miller4af51302000-04-16 11:18:38 +1000181double
Damien Miller95def091999-11-25 00:26:21 +1100182get_current_time()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000183{
Damien Miller95def091999-11-25 00:26:21 +1100184 struct timeval tv;
185 gettimeofday(&tv, NULL);
186 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000187}
188
Damien Miller5428f641999-11-25 11:54:57 +1100189/*
190 * This is called when the interactive is entered. This checks if there is
191 * an EOF coming on stdin. We must check this explicitly, as select() does
192 * not appear to wake up when redirecting from /dev/null.
193 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194
Damien Miller4af51302000-04-16 11:18:38 +1000195void
Damien Miller95def091999-11-25 00:26:21 +1100196client_check_initial_eof_on_stdin()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197{
Damien Miller95def091999-11-25 00:26:21 +1100198 int len;
199 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200
Damien Miller5428f641999-11-25 11:54:57 +1100201 /*
202 * If standard input is to be "redirected from /dev/null", we simply
203 * mark that we have seen an EOF and send an EOF message to the
204 * server. Otherwise, we try to read a single character; it appears
205 * that for some files, such /dev/null, select() never wakes up for
206 * read for this descriptor, which means that we never get EOF. This
207 * way we will get the EOF if stdin comes from /dev/null or similar.
208 */
Damien Miller95def091999-11-25 00:26:21 +1100209 if (stdin_null_flag) {
210 /* Fake EOF on stdin. */
211 debug("Sending eof.");
212 stdin_eof = 1;
213 packet_start(SSH_CMSG_EOF);
214 packet_send();
215 } else {
Damien Miller95def091999-11-25 00:26:21 +1100216 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217
Damien Miller95def091999-11-25 00:26:21 +1100218 /* Check for immediate EOF on stdin. */
219 len = read(fileno(stdin), buf, 1);
220 if (len == 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100221 /* EOF. Record that we have seen it and send EOF to server. */
Damien Miller95def091999-11-25 00:26:21 +1100222 debug("Sending eof.");
223 stdin_eof = 1;
224 packet_start(SSH_CMSG_EOF);
225 packet_send();
226 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100227 /*
228 * Got data. We must store the data in the buffer,
229 * and also process it as an escape character if
230 * appropriate.
231 */
Damien Miller95def091999-11-25 00:26:21 +1100232 if ((unsigned char) buf[0] == escape_char)
233 escape_pending = 1;
234 else {
235 buffer_append(&stdin_buffer, buf, 1);
236 stdin_bytes += 1;
237 }
238 }
Damien Miller95def091999-11-25 00:26:21 +1100239 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241}
242
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243
Damien Miller5428f641999-11-25 11:54:57 +1100244/*
245 * Make packets from buffered stdin data, and buffer them for sending to the
246 * connection.
247 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248
Damien Miller4af51302000-04-16 11:18:38 +1000249void
Damien Miller95def091999-11-25 00:26:21 +1100250client_make_packets_from_stdin_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251{
Damien Miller95def091999-11-25 00:26:21 +1100252 unsigned int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253
Damien Miller95def091999-11-25 00:26:21 +1100254 /* Send buffered stdin data to the server. */
255 while (buffer_len(&stdin_buffer) > 0 &&
256 packet_not_very_much_data_to_write()) {
257 len = buffer_len(&stdin_buffer);
258 /* Keep the packets at reasonable size. */
259 if (len > packet_get_maxsize())
260 len = packet_get_maxsize();
261 packet_start(SSH_CMSG_STDIN_DATA);
262 packet_put_string(buffer_ptr(&stdin_buffer), len);
263 packet_send();
264 buffer_consume(&stdin_buffer, len);
265 /* If we have a pending EOF, send it now. */
266 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
267 packet_start(SSH_CMSG_EOF);
268 packet_send();
269 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000271}
272
Damien Miller5428f641999-11-25 11:54:57 +1100273/*
274 * Checks if the client window has changed, and sends a packet about it to
275 * the server if so. The actual change is detected elsewhere (by a software
276 * interrupt on Unix); this just checks the flag and sends a message if
277 * appropriate.
278 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000279
Damien Miller4af51302000-04-16 11:18:38 +1000280void
Damien Miller95def091999-11-25 00:26:21 +1100281client_check_window_change()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000282{
Damien Miller1383bd82000-04-06 12:32:37 +1000283 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000284
Damien Miller1383bd82000-04-06 12:32:37 +1000285 if (! received_window_change_signal)
286 return;
287 /** XXX race */
288 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000289
Damien Miller1383bd82000-04-06 12:32:37 +1000290 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
291 return;
292
293 debug("client_check_window_change: changed");
294
295 if (compat20) {
296 channel_request_start(session_ident, "window-change", 0);
297 packet_put_int(ws.ws_col);
298 packet_put_int(ws.ws_row);
299 packet_put_int(ws.ws_xpixel);
300 packet_put_int(ws.ws_ypixel);
301 packet_send();
302 } else {
303 packet_start(SSH_CMSG_WINDOW_SIZE);
304 packet_put_int(ws.ws_row);
305 packet_put_int(ws.ws_col);
306 packet_put_int(ws.ws_xpixel);
307 packet_put_int(ws.ws_ypixel);
308 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000309 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000310}
311
Damien Miller5428f641999-11-25 11:54:57 +1100312/*
313 * Waits until the client can do something (some data becomes available on
314 * one of the file descriptors).
315 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000316
Damien Miller4af51302000-04-16 11:18:38 +1000317void
Damien Miller95def091999-11-25 00:26:21 +1100318client_wait_until_can_do_something(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000319{
Damien Miller1383bd82000-04-06 12:32:37 +1000320 /*debug("client_wait_until_can_do_something"); */
321
Damien Miller95def091999-11-25 00:26:21 +1100322 /* Initialize select masks. */
323 FD_ZERO(readset);
Damien Miller95def091999-11-25 00:26:21 +1100324 FD_ZERO(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000325
Damien Miller1383bd82000-04-06 12:32:37 +1000326 if (!compat20) {
327 /* Read from the connection, unless our buffers are full. */
328 if (buffer_len(&stdout_buffer) < buffer_high &&
329 buffer_len(&stderr_buffer) < buffer_high &&
330 channel_not_very_much_buffered_data())
331 FD_SET(connection_in, readset);
332 /*
333 * Read from stdin, unless we have seen EOF or have very much
334 * buffered data to send to the server.
335 */
336 if (!stdin_eof && packet_not_very_much_data_to_write())
337 FD_SET(fileno(stdin), readset);
338
339 /* Select stdout/stderr if have data in buffer. */
340 if (buffer_len(&stdout_buffer) > 0)
341 FD_SET(fileno(stdout), writeset);
342 if (buffer_len(&stderr_buffer) > 0)
343 FD_SET(fileno(stderr), writeset);
344 } else {
345 FD_SET(connection_in, readset);
346 }
347
Damien Miller95def091999-11-25 00:26:21 +1100348 /* Add any selections by the channel mechanism. */
349 channel_prepare_select(readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350
Damien Miller95def091999-11-25 00:26:21 +1100351 /* Select server connection if have data to write to the server. */
352 if (packet_have_data_to_write())
353 FD_SET(connection_out, writeset);
354
Damien Miller1383bd82000-04-06 12:32:37 +1000355/* move UP XXX */
Damien Miller95def091999-11-25 00:26:21 +1100356 /* Update maximum file descriptor number, if appropriate. */
357 if (channel_max_fd() > max_fd)
358 max_fd = channel_max_fd();
359
Damien Miller5428f641999-11-25 11:54:57 +1100360 /*
361 * Wait for something to happen. This will suspend the process until
362 * some selected descriptor can be read, written, or has some other
363 * event pending. Note: if you want to implement SSH_MSG_IGNORE
364 * messages to fool traffic analysis, this might be the place to do
365 * it: just have a random timeout for the select, and send a random
366 * SSH_MSG_IGNORE packet when the timeout expires.
367 */
Damien Miller95def091999-11-25 00:26:21 +1100368
369 if (select(max_fd + 1, readset, writeset, NULL, NULL) < 0) {
370 char buf[100];
371 /* Some systems fail to clear these automatically. */
372 FD_ZERO(readset);
373 FD_ZERO(writeset);
374 if (errno == EINTR)
375 return;
376 /* Note: we might still have data in the buffers. */
377 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
378 buffer_append(&stderr_buffer, buf, strlen(buf));
379 stderr_bytes += strlen(buf);
380 quit_pending = 1;
381 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000382}
383
Damien Miller4af51302000-04-16 11:18:38 +1000384void
Damien Millerad833b32000-08-23 10:46:23 +1000385client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000386{
Damien Miller95def091999-11-25 00:26:21 +1100387 struct winsize oldws, newws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000388
Damien Miller95def091999-11-25 00:26:21 +1100389 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000390 if (buffer_len(bout) > 0)
391 atomicio(write, fileno(stdout), buffer_ptr(bout), buffer_len(bout));
392 if (buffer_len(berr) > 0)
393 atomicio(write, fileno(stderr), buffer_ptr(berr), buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000394
Damien Miller95def091999-11-25 00:26:21 +1100395 leave_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000396
Damien Miller5428f641999-11-25 11:54:57 +1100397 /*
398 * Free (and clear) the buffer to reduce the amount of data that gets
399 * written to swap.
400 */
Damien Millerad833b32000-08-23 10:46:23 +1000401 buffer_free(bin);
402 buffer_free(bout);
403 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000404
Damien Miller95def091999-11-25 00:26:21 +1100405 /* Save old window size. */
406 ioctl(fileno(stdin), TIOCGWINSZ, &oldws);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000407
Damien Miller95def091999-11-25 00:26:21 +1100408 /* Send the suspend signal to the program itself. */
409 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000410
Damien Miller95def091999-11-25 00:26:21 +1100411 /* Check if the window size has changed. */
412 if (ioctl(fileno(stdin), TIOCGWINSZ, &newws) >= 0 &&
413 (oldws.ws_row != newws.ws_row ||
414 oldws.ws_col != newws.ws_col ||
415 oldws.ws_xpixel != newws.ws_xpixel ||
416 oldws.ws_ypixel != newws.ws_ypixel))
417 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418
Damien Miller95def091999-11-25 00:26:21 +1100419 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000420 buffer_init(bin);
421 buffer_init(bout);
422 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423
Damien Miller95def091999-11-25 00:26:21 +1100424 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425}
426
Damien Miller4af51302000-04-16 11:18:38 +1000427void
Damien Miller1383bd82000-04-06 12:32:37 +1000428client_process_net_input(fd_set * readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000429{
Damien Miller1383bd82000-04-06 12:32:37 +1000430 int len;
431 char buf[8192];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432
Damien Miller5428f641999-11-25 11:54:57 +1100433 /*
434 * Read input from the server, and add any such data to the buffer of
435 * the packet subsystem.
436 */
Damien Miller95def091999-11-25 00:26:21 +1100437 if (FD_ISSET(connection_in, readset)) {
438 /* Read as much as possible. */
439 len = read(connection_in, buf, sizeof(buf));
Damien Miller1383bd82000-04-06 12:32:37 +1000440/*debug("read connection_in len %d", len); XXX */
Damien Miller95def091999-11-25 00:26:21 +1100441 if (len == 0) {
442 /* Received EOF. The remote host has closed the connection. */
443 snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n",
444 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445 buffer_append(&stderr_buffer, buf, strlen(buf));
446 stderr_bytes += strlen(buf);
447 quit_pending = 1;
448 return;
Damien Miller95def091999-11-25 00:26:21 +1100449 }
Damien Miller5428f641999-11-25 11:54:57 +1100450 /*
451 * There is a kernel bug on Solaris that causes select to
452 * sometimes wake up even though there is no data available.
453 */
Damien Miller95def091999-11-25 00:26:21 +1100454 if (len < 0 && errno == EAGAIN)
455 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000456
Damien Miller95def091999-11-25 00:26:21 +1100457 if (len < 0) {
458 /* An error has encountered. Perhaps there is a network problem. */
459 snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n",
460 host, strerror(errno));
461 buffer_append(&stderr_buffer, buf, strlen(buf));
462 stderr_bytes += strlen(buf);
463 quit_pending = 1;
464 return;
465 }
466 packet_process_incoming(buf, len);
467 }
Damien Miller1383bd82000-04-06 12:32:37 +1000468}
469
Damien Millerad833b32000-08-23 10:46:23 +1000470/* process the characters one by one */
471int
472process_escapes(Buffer *bin, Buffer *bout, Buffer *berr, char *buf, int len)
473{
474 char string[1024];
475 pid_t pid;
476 int bytes = 0;
477 unsigned int i;
478 unsigned char ch;
479 char *s;
480
481 for (i = 0; i < len; i++) {
482 /* Get one character at a time. */
483 ch = buf[i];
484
485 if (escape_pending) {
486 /* We have previously seen an escape character. */
487 /* Clear the flag now. */
488 escape_pending = 0;
489
490 /* Process the escaped character. */
491 switch (ch) {
492 case '.':
493 /* Terminate the connection. */
494 snprintf(string, sizeof string, "%c.\r\n", escape_char);
495 buffer_append(berr, string, strlen(string));
496 /*stderr_bytes += strlen(string); XXX*/
497
498 quit_pending = 1;
499 return -1;
500
501 case 'Z' - 64:
502 /* Suspend the program. */
503 /* Print a message to that effect to the user. */
504 snprintf(string, sizeof string, "%c^Z [suspend ssh]\r\n", escape_char);
505 buffer_append(berr, string, strlen(string));
506 /*stderr_bytes += strlen(string); XXX*/
507
508 /* Restore terminal modes and suspend. */
509 client_suspend_self(bin, bout, berr);
510
511 /* We have been continued. */
512 continue;
513
514 case '&':
515 /* XXX does not work yet with proto 2 */
516 if (compat20)
517 continue;
518 /*
519 * Detach the program (continue to serve connections,
520 * but put in background and no more new connections).
521 */
522 if (!stdin_eof) {
523 /*
524 * Sending SSH_CMSG_EOF alone does not always appear
525 * to be enough. So we try to send an EOF character
526 * first.
527 */
528 packet_start(SSH_CMSG_STDIN_DATA);
529 packet_put_string("\004", 1);
530 packet_send();
531 /* Close stdin. */
532 stdin_eof = 1;
533 if (buffer_len(bin) == 0) {
534 packet_start(SSH_CMSG_EOF);
535 packet_send();
536 }
537 }
538 /* Restore tty modes. */
539 leave_raw_mode();
540
541 /* Stop listening for new connections. */
542 channel_stop_listening();
543
544 printf("%c& [backgrounded]\n", escape_char);
545
546 /* Fork into background. */
547 pid = fork();
548 if (pid < 0) {
549 error("fork: %.100s", strerror(errno));
550 continue;
551 }
552 if (pid != 0) { /* This is the parent. */
553 /* The parent just exits. */
554 exit(0);
555 }
556 /* The child continues serving connections. */
557 continue; /*XXX ? */
558
559 case '?':
560 snprintf(string, sizeof string,
561"%c?\r\n\
562Supported escape sequences:\r\n\
563~. - terminate connection\r\n\
564~^Z - suspend ssh\r\n\
565~# - list forwarded connections\r\n\
566~& - background ssh (when waiting for connections to terminate)\r\n\
567~? - this message\r\n\
568~~ - send the escape character by typing it twice\r\n\
569(Note that escapes are only recognized immediately after newline.)\r\n",
570 escape_char);
571 buffer_append(berr, string, strlen(string));
572 continue;
573
574 case '#':
575 snprintf(string, sizeof string, "%c#\r\n", escape_char);
576 buffer_append(berr, string, strlen(string));
577 s = channel_open_message();
578 buffer_append(berr, s, strlen(s));
579 xfree(s);
580 continue;
581
582 default:
583 if (ch != escape_char) {
584 buffer_put_char(bin, escape_char);
585 bytes++;
586 }
587 /* Escaped characters fall through here */
588 break;
589 }
590 } else {
591 /*
592 * The previous character was not an escape char. Check if this
593 * is an escape.
594 */
595 if (last_was_cr && ch == escape_char) {
596 /* It is. Set the flag and continue to next character. */
597 escape_pending = 1;
598 continue;
599 }
600 }
601
602 /*
603 * Normal character. Record whether it was a newline,
604 * and append it to the buffer.
605 */
606 last_was_cr = (ch == '\r' || ch == '\n');
607 buffer_put_char(bin, ch);
608 bytes++;
609 }
610 return bytes;
611}
612
Damien Miller4af51302000-04-16 11:18:38 +1000613void
Damien Miller1383bd82000-04-06 12:32:37 +1000614client_process_input(fd_set * readset)
615{
Damien Millerad833b32000-08-23 10:46:23 +1000616 int ret;
Damien Miller166fca82000-04-20 07:42:21 +1000617 int len;
Damien Millerad833b32000-08-23 10:46:23 +1000618 char buf[8192];
Damien Miller1383bd82000-04-06 12:32:37 +1000619
Damien Miller95def091999-11-25 00:26:21 +1100620 /* Read input from stdin. */
621 if (FD_ISSET(fileno(stdin), readset)) {
622 /* Read as much as possible. */
623 len = read(fileno(stdin), buf, sizeof(buf));
624 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100625 /*
626 * Received EOF or error. They are treated
627 * similarly, except that an error message is printed
628 * if it was an error condition.
629 */
Damien Miller95def091999-11-25 00:26:21 +1100630 if (len < 0) {
631 snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno));
632 buffer_append(&stderr_buffer, buf, strlen(buf));
633 stderr_bytes += strlen(buf);
634 }
635 /* Mark that we have seen EOF. */
636 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +1100637 /*
638 * Send an EOF message to the server unless there is
639 * data in the buffer. If there is data in the
640 * buffer, no message will be sent now. Code
641 * elsewhere will send the EOF when the buffer
642 * becomes empty if stdin_eof is set.
643 */
Damien Miller95def091999-11-25 00:26:21 +1100644 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000645 packet_start(SSH_CMSG_EOF);
646 packet_send();
Damien Miller95def091999-11-25 00:26:21 +1100647 }
648 } else if (escape_char == -1) {
Damien Miller5428f641999-11-25 11:54:57 +1100649 /*
650 * Normal successful read, and no escape character.
651 * Just append the data to buffer.
652 */
Damien Miller95def091999-11-25 00:26:21 +1100653 buffer_append(&stdin_buffer, buf, len);
654 stdin_bytes += len;
655 } else {
Damien Miller5428f641999-11-25 11:54:57 +1100656 /*
657 * Normal, successful read. But we have an escape character
658 * and have to process the characters one by one.
659 */
Damien Millerad833b32000-08-23 10:46:23 +1000660 ret = process_escapes(&stdin_buffer, &stdout_buffer, &stderr_buffer, buf, len);
661 if (ret == -1)
662 return;
663 stdout_bytes += ret;
Damien Miller95def091999-11-25 00:26:21 +1100664 }
665 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000666}
667
Damien Miller4af51302000-04-16 11:18:38 +1000668void
Damien Miller95def091999-11-25 00:26:21 +1100669client_process_output(fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000670{
Damien Miller95def091999-11-25 00:26:21 +1100671 int len;
672 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000673
Damien Miller95def091999-11-25 00:26:21 +1100674 /* Write buffered output to stdout. */
675 if (FD_ISSET(fileno(stdout), writeset)) {
676 /* Write as much data as possible. */
677 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100678 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100679 if (len <= 0) {
680 if (errno == EAGAIN)
681 len = 0;
682 else {
Damien Miller5428f641999-11-25 11:54:57 +1100683 /*
684 * An error or EOF was encountered. Put an
685 * error message to stderr buffer.
686 */
Damien Miller95def091999-11-25 00:26:21 +1100687 snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno));
688 buffer_append(&stderr_buffer, buf, strlen(buf));
689 stderr_bytes += strlen(buf);
690 quit_pending = 1;
691 return;
692 }
693 }
694 /* Consume printed data from the buffer. */
695 buffer_consume(&stdout_buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000696 }
Damien Miller95def091999-11-25 00:26:21 +1100697 /* Write buffered output to stderr. */
698 if (FD_ISSET(fileno(stderr), writeset)) {
699 /* Write as much data as possible. */
700 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100701 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100702 if (len <= 0) {
703 if (errno == EAGAIN)
704 len = 0;
705 else {
Damien Miller5428f641999-11-25 11:54:57 +1100706 /* EOF or error, but can't even print error message. */
Damien Miller95def091999-11-25 00:26:21 +1100707 quit_pending = 1;
708 return;
709 }
710 }
711 /* Consume printed characters from the buffer. */
712 buffer_consume(&stderr_buffer, len);
713 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000714}
715
Damien Miller5428f641999-11-25 11:54:57 +1100716/*
Damien Millerb38eff82000-04-01 11:09:21 +1000717 * Get packets from the connection input buffer, and process them as long as
718 * there are packets available.
719 *
720 * Any unknown packets received during the actual
721 * session cause the session to terminate. This is
722 * intended to make debugging easier since no
723 * confirmations are sent. Any compatible protocol
724 * extensions must be negotiated during the
725 * preparatory phase.
726 */
727
Damien Miller4af51302000-04-16 11:18:38 +1000728void
Damien Millerb38eff82000-04-01 11:09:21 +1000729client_process_buffered_input_packets()
730{
731 dispatch_run(DISPATCH_NONBLOCK, &quit_pending);
732}
733
Damien Millerad833b32000-08-23 10:46:23 +1000734/* scan buf[] for '~' before sending data to the peer */
735
736int
737simple_escape_filter(Channel *c, char *buf, int len)
738{
739 /* XXX we assume c->extended is writeable */
740 return process_escapes(&c->input, &c->output, &c->extended, buf, len);
741}
742
Damien Millerb38eff82000-04-01 11:09:21 +1000743/*
Damien Miller5428f641999-11-25 11:54:57 +1100744 * Implements the interactive session with the server. This is called after
745 * the user has been authenticated, and a command has been started on the
746 * remote host. If escape_char != -1, it is the character used as an escape
747 * character for terminating or suspending the session.
748 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000749
Damien Miller4af51302000-04-16 11:18:38 +1000750int
Damien Millerad833b32000-08-23 10:46:23 +1000751client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000752{
Damien Miller95def091999-11-25 00:26:21 +1100753 extern Options options;
754 double start_time, total_time;
755 int len;
756 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000757
Damien Miller95def091999-11-25 00:26:21 +1100758 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000759
Damien Miller95def091999-11-25 00:26:21 +1100760 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000761
Damien Miller95def091999-11-25 00:26:21 +1100762 /* Initialize variables. */
763 escape_pending = 0;
764 last_was_cr = 1;
765 exit_status = -1;
766 stdin_eof = 0;
767 buffer_high = 64 * 1024;
768 connection_in = packet_get_connection_in();
769 connection_out = packet_get_connection_out();
770 max_fd = connection_in;
771 if (connection_out > max_fd)
772 max_fd = connection_out;
773 stdin_bytes = 0;
774 stdout_bytes = 0;
775 stderr_bytes = 0;
776 quit_pending = 0;
777 escape_char = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000778
Damien Miller95def091999-11-25 00:26:21 +1100779 /* Initialize buffers. */
780 buffer_init(&stdin_buffer);
781 buffer_init(&stdout_buffer);
782 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000783
Damien Millerb38eff82000-04-01 11:09:21 +1000784 client_init_dispatch();
785
Damien Miller95def091999-11-25 00:26:21 +1100786 /* Set signal handlers to restore non-blocking mode. */
787 signal(SIGINT, signal_handler);
788 signal(SIGQUIT, signal_handler);
789 signal(SIGTERM, signal_handler);
790 signal(SIGPIPE, SIG_IGN);
791 if (have_pty)
792 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000793
Damien Miller95def091999-11-25 00:26:21 +1100794 if (have_pty)
795 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000796
Damien Millerbe484b52000-07-15 14:14:16 +1000797 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +1000798 if (!compat20)
799 client_check_initial_eof_on_stdin();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000800
Damien Millerad833b32000-08-23 10:46:23 +1000801 if (compat20 && escape_char != -1)
802 channel_register_filter(ssh2_chan_id, simple_escape_filter);
803
Damien Miller95def091999-11-25 00:26:21 +1100804 /* Main loop of the client for the interactive session mode. */
805 while (!quit_pending) {
806 fd_set readset, writeset;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000807
Damien Miller5428f641999-11-25 11:54:57 +1100808 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +1100809 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000810
Damien Miller1383bd82000-04-06 12:32:37 +1000811 if (compat20 && !channel_still_open()) {
812 debug("!channel_still_open.");
813 break;
814 }
815
Damien Miller5428f641999-11-25 11:54:57 +1100816 /*
817 * Make packets of buffered stdin data, and buffer them for
818 * sending to the server.
819 */
Damien Miller1383bd82000-04-06 12:32:37 +1000820 if (!compat20)
821 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000822
Damien Miller5428f641999-11-25 11:54:57 +1100823 /*
824 * Make packets from buffered channel data, and buffer them
825 * for sending to the server.
826 */
Damien Miller95def091999-11-25 00:26:21 +1100827 if (packet_not_very_much_data_to_write())
828 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000829
Damien Miller5428f641999-11-25 11:54:57 +1100830 /*
831 * Check if the window size has changed, and buffer a message
832 * about it to the server if so.
833 */
Damien Miller95def091999-11-25 00:26:21 +1100834 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000835
Damien Miller95def091999-11-25 00:26:21 +1100836 if (quit_pending)
837 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000838
Damien Miller5428f641999-11-25 11:54:57 +1100839 /*
840 * Wait until we have something to do (something becomes
841 * available on one of the descriptors).
842 */
Damien Miller95def091999-11-25 00:26:21 +1100843 client_wait_until_can_do_something(&readset, &writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000844
Damien Miller95def091999-11-25 00:26:21 +1100845 if (quit_pending)
846 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000847
Damien Miller95def091999-11-25 00:26:21 +1100848 /* Do channel operations. */
849 channel_after_select(&readset, &writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000850
Damien Miller1383bd82000-04-06 12:32:37 +1000851 /* Buffer input from the connection. */
852 client_process_net_input(&readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000853
Damien Miller1383bd82000-04-06 12:32:37 +1000854 if (quit_pending)
855 break;
856
857 if (!compat20) {
858 /* Buffer data from stdin */
859 client_process_input(&readset);
860 /*
861 * Process output to stdout and stderr. Output to
862 * the connection is processed elsewhere (above).
863 */
864 client_process_output(&writeset);
865 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000866
Damien Miller5428f641999-11-25 11:54:57 +1100867 /* Send as much buffered packet data as possible to the sender. */
Damien Miller95def091999-11-25 00:26:21 +1100868 if (FD_ISSET(connection_out, &writeset))
869 packet_write_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000870 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000871
Damien Miller95def091999-11-25 00:26:21 +1100872 /* Terminate the session. */
873
874 /* Stop watching for window change. */
875 if (have_pty)
876 signal(SIGWINCH, SIG_DFL);
877
878 /* Stop listening for connections. */
879 channel_stop_listening();
880
Damien Miller5428f641999-11-25 11:54:57 +1100881 /*
882 * In interactive mode (with pseudo tty) display a message indicating
883 * that the connection has been closed.
884 */
Damien Miller95def091999-11-25 00:26:21 +1100885 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
886 snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host);
887 buffer_append(&stderr_buffer, buf, strlen(buf));
888 stderr_bytes += strlen(buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000889 }
Damien Miller95def091999-11-25 00:26:21 +1100890 /* Output any buffered data for stdout. */
891 while (buffer_len(&stdout_buffer) > 0) {
892 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100893 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100894 if (len <= 0) {
895 error("Write failed flushing stdout buffer.");
896 break;
897 }
898 buffer_consume(&stdout_buffer, len);
899 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000900
Damien Miller95def091999-11-25 00:26:21 +1100901 /* Output any buffered data for stderr. */
902 while (buffer_len(&stderr_buffer) > 0) {
903 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100904 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100905 if (len <= 0) {
906 error("Write failed flushing stderr buffer.");
907 break;
908 }
909 buffer_consume(&stderr_buffer, len);
910 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000911
Damien Miller95def091999-11-25 00:26:21 +1100912 if (have_pty)
913 leave_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914
Damien Miller95def091999-11-25 00:26:21 +1100915 /* Clear and free any buffers. */
916 memset(buf, 0, sizeof(buf));
917 buffer_free(&stdin_buffer);
918 buffer_free(&stdout_buffer);
919 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000920
Damien Miller95def091999-11-25 00:26:21 +1100921 /* Report bytes transferred, and transfer rates. */
922 total_time = get_current_time() - start_time;
923 debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds",
924 stdin_bytes, stdout_bytes, stderr_bytes, total_time);
925 if (total_time > 0)
926 debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f",
927 stdin_bytes / total_time, stdout_bytes / total_time,
928 stderr_bytes / total_time);
929
930 /* Return the exit status of the program. */
931 debug("Exit status %d", exit_status);
932 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000933}
Damien Millerb38eff82000-04-01 11:09:21 +1000934
935/*********/
936
937void
938client_input_stdout_data(int type, int plen)
939{
940 unsigned int data_len;
941 char *data = packet_get_string(&data_len);
942 packet_integrity_check(plen, 4 + data_len, type);
943 buffer_append(&stdout_buffer, data, data_len);
944 stdout_bytes += data_len;
945 memset(data, 0, data_len);
946 xfree(data);
947}
948void
949client_input_stderr_data(int type, int plen)
950{
951 unsigned int data_len;
952 char *data = packet_get_string(&data_len);
953 packet_integrity_check(plen, 4 + data_len, type);
954 buffer_append(&stderr_buffer, data, data_len);
955 stdout_bytes += data_len;
956 memset(data, 0, data_len);
957 xfree(data);
958}
959void
960client_input_exit_status(int type, int plen)
961{
962 packet_integrity_check(plen, 4, type);
963 exit_status = packet_get_int();
964 /* Acknowledge the exit. */
965 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
966 packet_send();
967 /*
968 * Must wait for packet to be sent since we are
969 * exiting the loop.
970 */
971 packet_write_wait();
972 /* Flag that we want to exit. */
973 quit_pending = 1;
974}
975
Damien Millerbd483e72000-04-30 10:00:53 +1000976/* XXXX move to generic input handler */
977void
978client_input_channel_open(int type, int plen)
979{
980 Channel *c = NULL;
981 char *ctype;
982 int id;
983 unsigned int len;
984 int rchan;
985 int rmaxpack;
986 int rwindow;
987
988 ctype = packet_get_string(&len);
989 rchan = packet_get_int();
990 rwindow = packet_get_int();
991 rmaxpack = packet_get_int();
992
Damien Millere247cc42000-05-07 12:03:14 +1000993 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +1000994 ctype, rchan, rwindow, rmaxpack);
995
996 if (strcmp(ctype, "x11") == 0) {
997 int sock;
998 char *originator;
999 int originator_port;
1000 originator = packet_get_string(NULL);
Damien Miller30c3d422000-05-09 11:02:59 +10001001 if (datafellows & SSH_BUG_X11FWD) {
Damien Miller6d488712000-05-08 13:44:52 +10001002 debug("buggy server: x11 request w/o originator_port");
1003 originator_port = 0;
Damien Miller30c3d422000-05-09 11:02:59 +10001004 } else {
1005 originator_port = packet_get_int();
Damien Miller6d488712000-05-08 13:44:52 +10001006 }
Damien Millerbd483e72000-04-30 10:00:53 +10001007 packet_done();
1008 /* XXX check permission */
1009 xfree(originator);
1010 /* XXX move to channels.c */
1011 sock = x11_connect_display();
1012 if (sock >= 0) {
Damien Millerad833b32000-08-23 10:46:23 +10001013/*XXX MAXPACK */
Damien Millerbd483e72000-04-30 10:00:53 +10001014 id = channel_new("x11", SSH_CHANNEL_X11_OPEN,
1015 sock, sock, -1, 4*1024, 32*1024, 0,
1016 xstrdup("x11"));
1017 c = channel_lookup(id);
1018 }
1019 }
1020/* XXX duplicate : */
1021 if (c != NULL) {
1022 debug("confirm %s", ctype);
1023 c->remote_id = rchan;
1024 c->remote_window = rwindow;
1025 c->remote_maxpacket = rmaxpack;
1026
1027 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1028 packet_put_int(c->remote_id);
1029 packet_put_int(c->self);
1030 packet_put_int(c->local_window);
1031 packet_put_int(c->local_maxpacket);
1032 packet_send();
1033 } else {
1034 debug("failure %s", ctype);
1035 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1036 packet_put_int(rchan);
1037 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
1038 packet_put_cstring("bla bla");
1039 packet_put_cstring("");
1040 packet_send();
1041 }
1042 xfree(ctype);
1043}
1044
Damien Miller4af51302000-04-16 11:18:38 +10001045void
Damien Miller1383bd82000-04-06 12:32:37 +10001046client_init_dispatch_20()
1047{
1048 dispatch_init(&dispatch_protocol_error);
1049 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
1050 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
1051 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
1052 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10001053 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10001054 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1055 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1056 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &channel_input_channel_request);
1057 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
1058}
Damien Miller4af51302000-04-16 11:18:38 +10001059void
Damien Millerb38eff82000-04-01 11:09:21 +10001060client_init_dispatch_13()
1061{
1062 dispatch_init(NULL);
1063 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
1064 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
1065 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001066 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1067 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1068 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
1069 dispatch_set(SSH_SMSG_AGENT_OPEN, &auth_input_open_request);
1070 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
1071 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
1072 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
1073 dispatch_set(SSH_SMSG_X11_OPEN, &x11_input_open);
1074}
Damien Miller4af51302000-04-16 11:18:38 +10001075void
Damien Millerb38eff82000-04-01 11:09:21 +10001076client_init_dispatch_15()
1077{
1078 client_init_dispatch_13();
1079 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
1080 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
1081}
Damien Miller4af51302000-04-16 11:18:38 +10001082void
Damien Millerb38eff82000-04-01 11:09:21 +10001083client_init_dispatch()
1084{
Damien Miller1383bd82000-04-06 12:32:37 +10001085 if (compat20)
1086 client_init_dispatch_20();
1087 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001088 client_init_dispatch_13();
1089 else
1090 client_init_dispatch_15();
1091}
Damien Miller1383bd82000-04-06 12:32:37 +10001092
1093void
1094client_input_channel_req(int id, void *arg)
1095{
1096 Channel *c = NULL;
1097 unsigned int len;
1098 int success = 0;
1099 int reply;
1100 char *rtype;
1101
1102 rtype = packet_get_string(&len);
1103 reply = packet_get_char();
1104
Damien Millere247cc42000-05-07 12:03:14 +10001105 debug("client_input_channel_req: rtype %s reply %d", rtype, reply);
Damien Miller1383bd82000-04-06 12:32:37 +10001106
1107 c = channel_lookup(id);
1108 if (c == NULL)
1109 fatal("session_input_channel_req: channel %d: bad channel", id);
1110
1111 if (session_ident == -1) {
1112 error("client_input_channel_req: no channel %d", id);
1113 } else if (id != session_ident) {
1114 error("client_input_channel_req: bad channel %d != %d",
1115 id, session_ident);
1116 } else if (strcmp(rtype, "exit-status") == 0) {
1117 success = 1;
1118 exit_status = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001119 packet_done();
Damien Miller1383bd82000-04-06 12:32:37 +10001120 }
1121 if (reply) {
1122 packet_start(success ?
1123 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1124 packet_put_int(c->remote_id);
1125 packet_send();
1126 }
1127 xfree(rtype);
1128}
1129
1130void
1131client_set_session_ident(int id)
1132{
1133 debug("client_set_session_ident: id %d", id);
1134 session_ident = id;
1135 channel_register_callback(id, SSH2_MSG_CHANNEL_REQUEST,
1136 client_input_channel_req, (void *)0);
1137}