blob: c49346c2c7b536c3668628c21ad3f644d071fb48 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 *
3 * clientloop.c
4 *
5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
6 *
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
9 *
10 *
11 * Created: Sat Sep 23 12:23:57 1995 ylo
12 *
13 * The main loop for the interactive session (client side).
14 *
15 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
17#include "includes.h"
Damien Miller95def091999-11-25 00:26:21 +110018RCSID("$Id: clientloop.c,v 1.5 1999/11/24 13:26:22 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100019
20#include "xmalloc.h"
21#include "ssh.h"
22#include "packet.h"
23#include "buffer.h"
24#include "authfd.h"
Damien Miller5ce662a1999-11-11 17:57:39 +110025#include "readconf.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100026
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027/* Flag indicating that stdin should be redirected from /dev/null. */
28extern int stdin_null_flag;
29
30/* Name of the host we are connecting to. This is the name given on the
31 command line, or the HostName specified for the user-supplied name
32 in a configuration file. */
33extern char *host;
34
35/* Flag to indicate that we have received a window change signal which has
36 not yet been processed. This will cause a message indicating the new
37 window size to be sent to the server a little later. This is volatile
38 because this is updated in a signal handler. */
39static volatile int received_window_change_signal = 0;
40
41/* Terminal modes, as saved by enter_raw_mode. */
42static struct termios saved_tio;
43
44/* Flag indicating whether we are in raw mode. This is used by enter_raw_mode
45 and leave_raw_mode. */
46static int in_raw_mode = 0;
47
48/* Flag indicating whether the user\'s terminal is in non-blocking mode. */
49static int in_non_blocking_mode = 0;
50
51/* Common data for the client loop code. */
Damien Miller95def091999-11-25 00:26:21 +110052static int escape_pending; /* Last character was the escape character */
53static int last_was_cr; /* Last character was a newline. */
54static int exit_status; /* Used to store the exit status of the command. */
55static int stdin_eof; /* EOF has been encountered on standard error. */
56static Buffer stdin_buffer; /* Buffer for stdin data. */
57static Buffer stdout_buffer; /* Buffer for stdout data. */
58static Buffer stderr_buffer; /* Buffer for stderr data. */
59static unsigned int buffer_high;/* Soft max buffer size. */
60static int max_fd; /* Maximum file descriptor number in select(). */
61static int connection_in; /* Connection to server (input). */
62static int connection_out; /* Connection to server (output). */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063static unsigned long stdin_bytes, stdout_bytes, stderr_bytes;
Damien Miller95def091999-11-25 00:26:21 +110064static int quit_pending; /* Set to non-zero to quit the client loop. */
65static int escape_char; /* Escape character. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066
Damien Miller95def091999-11-25 00:26:21 +110067/* Returns the user\'s terminal to normal mode if it had been put in raw
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068 mode. */
69
Damien Miller95def091999-11-25 00:26:21 +110070void
71leave_raw_mode()
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072{
Damien Miller95def091999-11-25 00:26:21 +110073 if (!in_raw_mode)
74 return;
75 in_raw_mode = 0;
76 if (tcsetattr(fileno(stdin), TCSADRAIN, &saved_tio) < 0)
77 perror("tcsetattr");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078
Damien Miller95def091999-11-25 00:26:21 +110079 fatal_remove_cleanup((void (*) (void *)) leave_raw_mode, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080}
81
82/* Puts the user\'s terminal in raw mode. */
83
Damien Miller95def091999-11-25 00:26:21 +110084void
85enter_raw_mode()
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086{
Damien Miller95def091999-11-25 00:26:21 +110087 struct termios tio;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
Damien Miller95def091999-11-25 00:26:21 +110089 if (tcgetattr(fileno(stdin), &tio) < 0)
90 perror("tcgetattr");
91 saved_tio = tio;
92 tio.c_iflag |= IGNPAR;
93 tio.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF);
94 tio.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095#ifdef IEXTEN
Damien Miller95def091999-11-25 00:26:21 +110096 tio.c_lflag &= ~IEXTEN;
97#endif /* IEXTEN */
98 tio.c_oflag &= ~OPOST;
99 tio.c_cc[VMIN] = 1;
100 tio.c_cc[VTIME] = 0;
101 if (tcsetattr(fileno(stdin), TCSADRAIN, &tio) < 0)
102 perror("tcsetattr");
103 in_raw_mode = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104
Damien Miller95def091999-11-25 00:26:21 +1100105 fatal_add_cleanup((void (*) (void *)) leave_raw_mode, NULL);
106}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107
108/* Restores stdin to blocking mode. */
109
Damien Miller95def091999-11-25 00:26:21 +1100110void
111leave_non_blocking()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112{
Damien Miller95def091999-11-25 00:26:21 +1100113 if (in_non_blocking_mode) {
114 (void) fcntl(fileno(stdin), F_SETFL, 0);
115 in_non_blocking_mode = 0;
116 fatal_remove_cleanup((void (*) (void *)) leave_non_blocking, NULL);
117 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118}
119
Damien Miller95def091999-11-25 00:26:21 +1100120/* Puts stdin terminal in non-blocking mode. */
121
122void
123enter_non_blocking()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124{
Damien Miller95def091999-11-25 00:26:21 +1100125 in_non_blocking_mode = 1;
126 (void) fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
127 fatal_add_cleanup((void (*) (void *)) leave_non_blocking, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128}
129
130/* Signal handler for the window change signal (SIGWINCH). This just
131 sets a flag indicating that the window has changed. */
132
Damien Miller95def091999-11-25 00:26:21 +1100133void
134window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135{
Damien Miller95def091999-11-25 00:26:21 +1100136 received_window_change_signal = 1;
137 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138}
139
140/* Signal handler for signals that cause the program to terminate. These
141 signals must be trapped to restore terminal modes. */
142
Damien Miller95def091999-11-25 00:26:21 +1100143void
144signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145{
Damien Miller95def091999-11-25 00:26:21 +1100146 if (in_raw_mode)
147 leave_raw_mode();
148 if (in_non_blocking_mode)
149 leave_non_blocking();
150 channel_stop_listening();
151 packet_close();
152 fatal("Killed by signal %d.", sig);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153}
154
155/* Returns current time in seconds from Jan 1, 1970 with the maximum available
156 resolution. */
157
Damien Miller95def091999-11-25 00:26:21 +1100158double
159get_current_time()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160{
Damien Miller95def091999-11-25 00:26:21 +1100161 struct timeval tv;
162 gettimeofday(&tv, NULL);
163 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164}
165
166/* This is called when the interactive is entered. This checks if there
167 is an EOF coming on stdin. We must check this explicitly, as select()
168 does not appear to wake up when redirecting from /dev/null. */
169
Damien Miller95def091999-11-25 00:26:21 +1100170void
171client_check_initial_eof_on_stdin()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000172{
Damien Miller95def091999-11-25 00:26:21 +1100173 int len;
174 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000175
Damien Miller95def091999-11-25 00:26:21 +1100176 /* If standard input is to be "redirected from /dev/null", we
177 simply mark that we have seen an EOF and send an EOF message to
178 the server. Otherwise, we try to read a single character; it
179 appears that for some files, such /dev/null, select() never
180 wakes up for read for this descriptor, which means that we
181 never get EOF. This way we will get the EOF if stdin comes
182 from /dev/null or similar. */
183 if (stdin_null_flag) {
184 /* Fake EOF on stdin. */
185 debug("Sending eof.");
186 stdin_eof = 1;
187 packet_start(SSH_CMSG_EOF);
188 packet_send();
189 } else {
190 /* Enter non-blocking mode for stdin. */
191 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000192
Damien Miller95def091999-11-25 00:26:21 +1100193 /* Check for immediate EOF on stdin. */
194 len = read(fileno(stdin), buf, 1);
195 if (len == 0) {
196 /* EOF. Record that we have seen it and send EOF
197 to server. */
198 debug("Sending eof.");
199 stdin_eof = 1;
200 packet_start(SSH_CMSG_EOF);
201 packet_send();
202 } else if (len > 0) {
203 /* Got data. We must store the data in the
204 buffer, and also process it as an escape
205 character if appropriate. */
206 if ((unsigned char) buf[0] == escape_char)
207 escape_pending = 1;
208 else {
209 buffer_append(&stdin_buffer, buf, 1);
210 stdin_bytes += 1;
211 }
212 }
213 /* Leave non-blocking mode. */
214 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000216}
217
218/* Get packets from the connection input buffer, and process them as long
219 as there are packets available. */
220
Damien Miller95def091999-11-25 00:26:21 +1100221void
222client_process_buffered_input_packets()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223{
Damien Miller95def091999-11-25 00:26:21 +1100224 int type;
225 char *data;
226 unsigned int data_len;
227 int payload_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228
Damien Miller95def091999-11-25 00:26:21 +1100229 /* Process any buffered packets from the server. */
230 while (!quit_pending &&
231 (type = packet_read_poll(&payload_len)) != SSH_MSG_NONE) {
232 switch (type) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000233
Damien Miller95def091999-11-25 00:26:21 +1100234 case SSH_SMSG_STDOUT_DATA:
235 data = packet_get_string(&data_len);
236 packet_integrity_check(payload_len, 4 + data_len, type);
237 buffer_append(&stdout_buffer, data, data_len);
238 stdout_bytes += data_len;
239 memset(data, 0, data_len);
240 xfree(data);
241 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242
Damien Miller95def091999-11-25 00:26:21 +1100243 case SSH_SMSG_STDERR_DATA:
244 data = packet_get_string(&data_len);
245 packet_integrity_check(payload_len, 4 + data_len, type);
246 buffer_append(&stderr_buffer, data, data_len);
247 stdout_bytes += data_len;
248 memset(data, 0, data_len);
249 xfree(data);
250 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251
Damien Miller95def091999-11-25 00:26:21 +1100252 case SSH_SMSG_EXITSTATUS:
253 packet_integrity_check(payload_len, 4, type);
254 exit_status = packet_get_int();
255 /* Acknowledge the exit. */
256 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
257 packet_send();
258 /* Must wait for packet to be sent since we are
259 exiting the loop. */
260 packet_write_wait();
261 /* Flag that we want to exit. */
262 quit_pending = 1;
263 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000264
Damien Miller95def091999-11-25 00:26:21 +1100265 case SSH_SMSG_X11_OPEN:
266 x11_input_open(payload_len);
267 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000268
Damien Miller95def091999-11-25 00:26:21 +1100269 case SSH_MSG_PORT_OPEN:
270 channel_input_port_open(payload_len);
271 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000272
Damien Miller95def091999-11-25 00:26:21 +1100273 case SSH_SMSG_AGENT_OPEN:
274 packet_integrity_check(payload_len, 4, type);
275 auth_input_open_request();
276 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000277
Damien Miller95def091999-11-25 00:26:21 +1100278 case SSH_MSG_CHANNEL_OPEN_CONFIRMATION:
279 packet_integrity_check(payload_len, 4 + 4, type);
280 channel_input_open_confirmation();
281 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000282
Damien Miller95def091999-11-25 00:26:21 +1100283 case SSH_MSG_CHANNEL_OPEN_FAILURE:
284 packet_integrity_check(payload_len, 4, type);
285 channel_input_open_failure();
286 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000287
Damien Miller95def091999-11-25 00:26:21 +1100288 case SSH_MSG_CHANNEL_DATA:
289 channel_input_data(payload_len);
290 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000291
Damien Miller95def091999-11-25 00:26:21 +1100292 case SSH_MSG_CHANNEL_CLOSE:
293 packet_integrity_check(payload_len, 4, type);
294 channel_input_close();
295 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000296
Damien Miller95def091999-11-25 00:26:21 +1100297 case SSH_MSG_CHANNEL_CLOSE_CONFIRMATION:
298 packet_integrity_check(payload_len, 4, type);
299 channel_input_close_confirmation();
300 break;
301
302 default:
303 /* Any unknown packets received during the actual
304 session cause the session to terminate. This
305 is intended to make debugging easier since no
306 confirmations are sent. Any compatible
307 protocol extensions must be negotiated during
308 the preparatory phase. */
309 packet_disconnect("Protocol error during session: type %d",
310 type);
311 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000313}
314
315/* Make packets from buffered stdin data, and buffer them for sending to
316 the connection. */
317
Damien Miller95def091999-11-25 00:26:21 +1100318void
319client_make_packets_from_stdin_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000320{
Damien Miller95def091999-11-25 00:26:21 +1100321 unsigned int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322
Damien Miller95def091999-11-25 00:26:21 +1100323 /* Send buffered stdin data to the server. */
324 while (buffer_len(&stdin_buffer) > 0 &&
325 packet_not_very_much_data_to_write()) {
326 len = buffer_len(&stdin_buffer);
327 /* Keep the packets at reasonable size. */
328 if (len > packet_get_maxsize())
329 len = packet_get_maxsize();
330 packet_start(SSH_CMSG_STDIN_DATA);
331 packet_put_string(buffer_ptr(&stdin_buffer), len);
332 packet_send();
333 buffer_consume(&stdin_buffer, len);
334 /* If we have a pending EOF, send it now. */
335 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
336 packet_start(SSH_CMSG_EOF);
337 packet_send();
338 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000340}
341
342/* Checks if the client window has changed, and sends a packet about it to
343 the server if so. The actual change is detected elsewhere (by a software
344 interrupt on Unix); this just checks the flag and sends a message if
345 appropriate. */
346
Damien Miller95def091999-11-25 00:26:21 +1100347void
348client_check_window_change()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349{
Damien Miller95def091999-11-25 00:26:21 +1100350 /* Send possible window change message to the server. */
351 if (received_window_change_signal) {
352 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000353
Damien Miller95def091999-11-25 00:26:21 +1100354 /* Clear the window change indicator. */
355 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356
Damien Miller95def091999-11-25 00:26:21 +1100357 /* Read new window size. */
358 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) >= 0) {
359 /* Successful, send the packet now. */
360 packet_start(SSH_CMSG_WINDOW_SIZE);
361 packet_put_int(ws.ws_row);
362 packet_put_int(ws.ws_col);
363 packet_put_int(ws.ws_xpixel);
364 packet_put_int(ws.ws_ypixel);
365 packet_send();
366 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000367 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000368}
369
370/* Waits until the client can do something (some data becomes available on
371 one of the file descriptors). */
372
Damien Miller95def091999-11-25 00:26:21 +1100373void
374client_wait_until_can_do_something(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000375{
Damien Miller95def091999-11-25 00:26:21 +1100376 /* Initialize select masks. */
377 FD_ZERO(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000378
Damien Miller95def091999-11-25 00:26:21 +1100379 /* Read from the connection, unless our buffers are full. */
380 if (buffer_len(&stdout_buffer) < buffer_high &&
381 buffer_len(&stderr_buffer) < buffer_high &&
382 channel_not_very_much_buffered_data())
383 FD_SET(connection_in, readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000384
Damien Miller95def091999-11-25 00:26:21 +1100385 /* Read from stdin, unless we have seen EOF or have very much
386 buffered data to send to the server. */
387 if (!stdin_eof && packet_not_very_much_data_to_write())
388 FD_SET(fileno(stdin), readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000389
Damien Miller95def091999-11-25 00:26:21 +1100390 FD_ZERO(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000391
Damien Miller95def091999-11-25 00:26:21 +1100392 /* Add any selections by the channel mechanism. */
393 channel_prepare_select(readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000394
Damien Miller95def091999-11-25 00:26:21 +1100395 /* Select server connection if have data to write to the server. */
396 if (packet_have_data_to_write())
397 FD_SET(connection_out, writeset);
398
399 /* Select stdout if have data in buffer. */
400 if (buffer_len(&stdout_buffer) > 0)
401 FD_SET(fileno(stdout), writeset);
402
403 /* Select stderr if have data in buffer. */
404 if (buffer_len(&stderr_buffer) > 0)
405 FD_SET(fileno(stderr), writeset);
406
407 /* Update maximum file descriptor number, if appropriate. */
408 if (channel_max_fd() > max_fd)
409 max_fd = channel_max_fd();
410
411 /* Wait for something to happen. This will suspend the process
412 until some selected descriptor can be read, written, or has
413 some other event pending.
414 Note: if you want to implement SSH_MSG_IGNORE messages to fool
415 traffic analysis, this might be the place to do it:
416 just have a random timeout for the select, and send a random
417 SSH_MSG_IGNORE packet when the timeout expires. */
418
419 if (select(max_fd + 1, readset, writeset, NULL, NULL) < 0) {
420 char buf[100];
421 /* Some systems fail to clear these automatically. */
422 FD_ZERO(readset);
423 FD_ZERO(writeset);
424 if (errno == EINTR)
425 return;
426 /* Note: we might still have data in the buffers. */
427 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
428 buffer_append(&stderr_buffer, buf, strlen(buf));
429 stderr_bytes += strlen(buf);
430 quit_pending = 1;
431 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432}
433
Damien Miller95def091999-11-25 00:26:21 +1100434void
435client_suspend_self()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436{
Damien Miller95def091999-11-25 00:26:21 +1100437 struct winsize oldws, newws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000438
Damien Miller95def091999-11-25 00:26:21 +1100439 /* Flush stdout and stderr buffers. */
440 if (buffer_len(&stdout_buffer) > 0)
441 write(fileno(stdout),
442 buffer_ptr(&stdout_buffer),
443 buffer_len(&stdout_buffer));
444 if (buffer_len(&stderr_buffer) > 0)
445 write(fileno(stderr),
446 buffer_ptr(&stderr_buffer),
447 buffer_len(&stderr_buffer));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000448
Damien Miller95def091999-11-25 00:26:21 +1100449 /* Leave raw mode. */
450 leave_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000451
Damien Miller95def091999-11-25 00:26:21 +1100452 /* Free (and clear) the buffer to reduce the amount of data that
453 gets written to swap. */
454 buffer_free(&stdin_buffer);
455 buffer_free(&stdout_buffer);
456 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457
Damien Miller95def091999-11-25 00:26:21 +1100458 /* Save old window size. */
459 ioctl(fileno(stdin), TIOCGWINSZ, &oldws);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460
Damien Miller95def091999-11-25 00:26:21 +1100461 /* Send the suspend signal to the program itself. */
462 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000463
Damien Miller95def091999-11-25 00:26:21 +1100464 /* Check if the window size has changed. */
465 if (ioctl(fileno(stdin), TIOCGWINSZ, &newws) >= 0 &&
466 (oldws.ws_row != newws.ws_row ||
467 oldws.ws_col != newws.ws_col ||
468 oldws.ws_xpixel != newws.ws_xpixel ||
469 oldws.ws_ypixel != newws.ws_ypixel))
470 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471
Damien Miller95def091999-11-25 00:26:21 +1100472 /* OK, we have been continued by the user. Reinitialize buffers. */
473 buffer_init(&stdin_buffer);
474 buffer_init(&stdout_buffer);
475 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000476
Damien Miller95def091999-11-25 00:26:21 +1100477 /* Re-enter raw mode. */
478 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000479}
480
Damien Miller95def091999-11-25 00:26:21 +1100481void
482client_process_input(fd_set * readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483{
Damien Miller95def091999-11-25 00:26:21 +1100484 int len, pid;
485 char buf[8192], *s;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486
Damien Miller95def091999-11-25 00:26:21 +1100487 /* Read input from the server, and add any such data to the buffer
488 of the packet subsystem. */
489 if (FD_ISSET(connection_in, readset)) {
490 /* Read as much as possible. */
491 len = read(connection_in, buf, sizeof(buf));
492 if (len == 0) {
493 /* Received EOF. The remote host has closed the connection. */
494 snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n",
495 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000496 buffer_append(&stderr_buffer, buf, strlen(buf));
497 stderr_bytes += strlen(buf);
498 quit_pending = 1;
499 return;
Damien Miller95def091999-11-25 00:26:21 +1100500 }
501 /* There is a kernel bug on Solaris that causes select to
502 sometimes wake up even though there is no data
503 available. */
504 if (len < 0 && errno == EAGAIN)
505 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000506
Damien Miller95def091999-11-25 00:26:21 +1100507 if (len < 0) {
508 /* An error has encountered. Perhaps there is a network problem. */
509 snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n",
510 host, strerror(errno));
511 buffer_append(&stderr_buffer, buf, strlen(buf));
512 stderr_bytes += strlen(buf);
513 quit_pending = 1;
514 return;
515 }
516 packet_process_incoming(buf, len);
517 }
518 /* Read input from stdin. */
519 if (FD_ISSET(fileno(stdin), readset)) {
520 /* Read as much as possible. */
521 len = read(fileno(stdin), buf, sizeof(buf));
522 if (len <= 0) {
523 /* Received EOF or error. They are treated
524 similarly, except that an error message is
525 printed if it was an error condition. */
526 if (len < 0) {
527 snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno));
528 buffer_append(&stderr_buffer, buf, strlen(buf));
529 stderr_bytes += strlen(buf);
530 }
531 /* Mark that we have seen EOF. */
532 stdin_eof = 1;
533 /* Send an EOF message to the server unless there
534 is data in the buffer. If there is data in the
535 buffer, no message will be sent now. Code
536 elsewhere will send the EOF when the buffer
537 becomes empty if stdin_eof is set. */
538 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000539 packet_start(SSH_CMSG_EOF);
540 packet_send();
Damien Miller95def091999-11-25 00:26:21 +1100541 }
542 } else if (escape_char == -1) {
543 /* Normal successful read, and no escape
544 character. Just append the data to buffer. */
545 buffer_append(&stdin_buffer, buf, len);
546 stdin_bytes += len;
547 } else {
548 /* Normal, successful read. But we have an escape
549 character and have to process the characters
550 one by one. */
551 unsigned int i;
552 for (i = 0; i < len; i++) {
553 unsigned char ch;
554 /* Get one character at a time. */
555 ch = buf[i];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000556
Damien Miller95def091999-11-25 00:26:21 +1100557 /* Check if we have a pending escape
558 character. */
559 if (escape_pending) {
560 /* We have previously seen an escape character. */
561 /* Clear the flag now. */
562 escape_pending = 0;
563 /* Process the escaped character. */
564 switch (ch) {
565 case '.':
566 /* Terminate the connection. */
567 snprintf(buf, sizeof buf, "%c.\r\n", escape_char);
568 buffer_append(&stderr_buffer, buf, strlen(buf));
569 stderr_bytes += strlen(buf);
570 quit_pending = 1;
571 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000572
Damien Miller95def091999-11-25 00:26:21 +1100573 case 'Z' - 64:
574 /* Suspend the program. */
575 /* Print a message to that effect to the user. */
576 snprintf(buf, sizeof buf, "%c^Z\r\n", escape_char);
577 buffer_append(&stderr_buffer, buf, strlen(buf));
578 stderr_bytes += strlen(buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000579
Damien Miller95def091999-11-25 00:26:21 +1100580 /* Restore terminal modes and suspend. */
581 client_suspend_self();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000582
Damien Miller95def091999-11-25 00:26:21 +1100583 /* We have been continued. */
584 continue;
585
586 case '&':
587 /* Detach the program (continue to serve connections,
588 but put in background and no more new connections). */
589 if (!stdin_eof) {
590 /* Sending SSH_CMSG_EOF alone does not always appear
591 to be enough. So we try to send an EOF character
592 first. */
593 packet_start(SSH_CMSG_STDIN_DATA);
594 packet_put_string("\004", 1);
595 packet_send();
596 /* Close stdin. */
597 stdin_eof = 1;
598 if (buffer_len(&stdin_buffer) == 0) {
599 packet_start(SSH_CMSG_EOF);
600 packet_send();
601 }
602 }
603 /* Restore tty modes. */
604 leave_raw_mode();
605
606 /* Stop listening for new connections. */
607 channel_stop_listening();
608
609 printf("%c& [backgrounded]\n", escape_char);
610
611 /* Fork into background. */
612 pid = fork();
613 if (pid < 0) {
614 error("fork: %.100s", strerror(errno));
615 continue;
616 }
617 if (pid != 0) { /* This is the parent. */
618 /* The parent just exits. */
619 exit(0);
620 }
621 /* The child continues serving connections. */
622 continue;
623
624 case '?':
625 snprintf(buf, sizeof buf,
626"%c?\r\n\
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000627Supported escape sequences:\r\n\
628~. - terminate connection\r\n\
629~^Z - suspend ssh\r\n\
630~# - list forwarded connections\r\n\
631~& - background ssh (when waiting for connections to terminate)\r\n\
632~? - this message\r\n\
633~~ - send the escape character by typing it twice\r\n\
634(Note that escapes are only recognized immediately after newline.)\r\n",
Damien Miller95def091999-11-25 00:26:21 +1100635 escape_char);
636 buffer_append(&stderr_buffer, buf, strlen(buf));
637 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000638
Damien Miller95def091999-11-25 00:26:21 +1100639 case '#':
640 snprintf(buf, sizeof buf, "%c#\r\n", escape_char);
641 buffer_append(&stderr_buffer, buf, strlen(buf));
642 s = channel_open_message();
643 buffer_append(&stderr_buffer, s, strlen(s));
644 xfree(s);
645 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000646
Damien Miller95def091999-11-25 00:26:21 +1100647 default:
648 if (ch != escape_char) {
649 /* Escape character followed by non-special character.
650 Append both to the input buffer. */
651 buf[0] = escape_char;
652 buf[1] = ch;
653 buffer_append(&stdin_buffer, buf, 2);
654 stdin_bytes += 2;
655 continue;
656 }
657 /* Note that escape character typed twice
658 falls through here; the latter gets processed
659 as a normal character below. */
660 break;
661 }
662 } else {
663 /* The previous character was not an escape char. Check if this
664 is an escape. */
665 if (last_was_cr && ch == escape_char) {
666 /* It is. Set the flag and continue to next character. */
667 escape_pending = 1;
668 continue;
669 }
670 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000671
Damien Miller95def091999-11-25 00:26:21 +1100672 /* Normal character. Record whether it was a newline, and append it to the
673 buffer. */
674 last_was_cr = (ch == '\r' || ch == '\n');
675 buf[0] = ch;
676 buffer_append(&stdin_buffer, buf, 1);
677 stdin_bytes += 1;
678 continue;
679 }
680 }
681 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000682}
683
Damien Miller95def091999-11-25 00:26:21 +1100684void
685client_process_output(fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000686{
Damien Miller95def091999-11-25 00:26:21 +1100687 int len;
688 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000689
Damien Miller95def091999-11-25 00:26:21 +1100690 /* Write buffered output to stdout. */
691 if (FD_ISSET(fileno(stdout), writeset)) {
692 /* Write as much data as possible. */
693 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
694 buffer_len(&stdout_buffer));
695 if (len <= 0) {
696 if (errno == EAGAIN)
697 len = 0;
698 else {
699 /* An error or EOF was encountered. Put
700 an error message to stderr buffer. */
701 snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno));
702 buffer_append(&stderr_buffer, buf, strlen(buf));
703 stderr_bytes += strlen(buf);
704 quit_pending = 1;
705 return;
706 }
707 }
708 /* Consume printed data from the buffer. */
709 buffer_consume(&stdout_buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000710 }
Damien Miller95def091999-11-25 00:26:21 +1100711 /* Write buffered output to stderr. */
712 if (FD_ISSET(fileno(stderr), writeset)) {
713 /* Write as much data as possible. */
714 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
715 buffer_len(&stderr_buffer));
716 if (len <= 0) {
717 if (errno == EAGAIN)
718 len = 0;
719 else {
720 /* EOF or error, but can't even print
721 error message. */
722 quit_pending = 1;
723 return;
724 }
725 }
726 /* Consume printed characters from the buffer. */
727 buffer_consume(&stderr_buffer, len);
728 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000729}
730
731/* Implements the interactive session with the server. This is called
732 after the user has been authenticated, and a command has been
733 started on the remote host. If escape_char != -1, it is the character
734 used as an escape character for terminating or suspending the
735 session. */
736
Damien Miller95def091999-11-25 00:26:21 +1100737int
738client_loop(int have_pty, int escape_char_arg)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000739{
Damien Miller95def091999-11-25 00:26:21 +1100740 extern Options options;
741 double start_time, total_time;
742 int len;
743 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000744
Damien Miller95def091999-11-25 00:26:21 +1100745 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000746
Damien Miller95def091999-11-25 00:26:21 +1100747 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000748
Damien Miller95def091999-11-25 00:26:21 +1100749 /* Initialize variables. */
750 escape_pending = 0;
751 last_was_cr = 1;
752 exit_status = -1;
753 stdin_eof = 0;
754 buffer_high = 64 * 1024;
755 connection_in = packet_get_connection_in();
756 connection_out = packet_get_connection_out();
757 max_fd = connection_in;
758 if (connection_out > max_fd)
759 max_fd = connection_out;
760 stdin_bytes = 0;
761 stdout_bytes = 0;
762 stderr_bytes = 0;
763 quit_pending = 0;
764 escape_char = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000765
Damien Miller95def091999-11-25 00:26:21 +1100766 /* Initialize buffers. */
767 buffer_init(&stdin_buffer);
768 buffer_init(&stdout_buffer);
769 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000770
Damien Miller95def091999-11-25 00:26:21 +1100771 /* Set signal handlers to restore non-blocking mode. */
772 signal(SIGINT, signal_handler);
773 signal(SIGQUIT, signal_handler);
774 signal(SIGTERM, signal_handler);
775 signal(SIGPIPE, SIG_IGN);
776 if (have_pty)
777 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000778
Damien Miller95def091999-11-25 00:26:21 +1100779 /* Enter raw mode if have a pseudo terminal. */
780 if (have_pty)
781 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000782
Damien Miller95def091999-11-25 00:26:21 +1100783 /* Check if we should immediately send of on stdin. */
784 client_check_initial_eof_on_stdin();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000785
Damien Miller95def091999-11-25 00:26:21 +1100786 /* Main loop of the client for the interactive session mode. */
787 while (!quit_pending) {
788 fd_set readset, writeset;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000789
Damien Miller95def091999-11-25 00:26:21 +1100790 /* Precess buffered packets sent by the server. */
791 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000792
Damien Miller95def091999-11-25 00:26:21 +1100793 /* Make packets of buffered stdin data, and buffer them
794 for sending to the server. */
795 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000796
Damien Miller95def091999-11-25 00:26:21 +1100797 /* Make packets from buffered channel data, and buffer
798 them for sending to the server. */
799 if (packet_not_very_much_data_to_write())
800 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000801
Damien Miller95def091999-11-25 00:26:21 +1100802 /* Check if the window size has changed, and buffer a
803 message about it to the server if so. */
804 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000805
Damien Miller95def091999-11-25 00:26:21 +1100806 if (quit_pending)
807 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000808
Damien Miller95def091999-11-25 00:26:21 +1100809 /* Wait until we have something to do (something becomes
810 available on one of the descriptors). */
811 client_wait_until_can_do_something(&readset, &writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000812
Damien Miller95def091999-11-25 00:26:21 +1100813 if (quit_pending)
814 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000815
Damien Miller95def091999-11-25 00:26:21 +1100816 /* Do channel operations. */
817 channel_after_select(&readset, &writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000818
Damien Miller95def091999-11-25 00:26:21 +1100819 /* Process input from the connection and from stdin.
820 Buffer any data that is available. */
821 client_process_input(&readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000822
Damien Miller95def091999-11-25 00:26:21 +1100823 /* Process output to stdout and stderr. Output to the
824 connection is processed elsewhere (above). */
825 client_process_output(&writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000826
Damien Miller95def091999-11-25 00:26:21 +1100827 /* Send as much buffered packet data as possible to the
828 sender. */
829 if (FD_ISSET(connection_out, &writeset))
830 packet_write_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000831 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000832
Damien Miller95def091999-11-25 00:26:21 +1100833 /* Terminate the session. */
834
835 /* Stop watching for window change. */
836 if (have_pty)
837 signal(SIGWINCH, SIG_DFL);
838
839 /* Stop listening for connections. */
840 channel_stop_listening();
841
842 /* In interactive mode (with pseudo tty) display a message
843 indicating that the connection has been closed. */
844 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
845 snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host);
846 buffer_append(&stderr_buffer, buf, strlen(buf));
847 stderr_bytes += strlen(buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000848 }
Damien Miller95def091999-11-25 00:26:21 +1100849 /* Output any buffered data for stdout. */
850 while (buffer_len(&stdout_buffer) > 0) {
851 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
852 buffer_len(&stdout_buffer));
853 if (len <= 0) {
854 error("Write failed flushing stdout buffer.");
855 break;
856 }
857 buffer_consume(&stdout_buffer, len);
858 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000859
Damien Miller95def091999-11-25 00:26:21 +1100860 /* Output any buffered data for stderr. */
861 while (buffer_len(&stderr_buffer) > 0) {
862 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
863 buffer_len(&stderr_buffer));
864 if (len <= 0) {
865 error("Write failed flushing stderr buffer.");
866 break;
867 }
868 buffer_consume(&stderr_buffer, len);
869 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000870
Damien Miller95def091999-11-25 00:26:21 +1100871 /* Leave raw mode. */
872 if (have_pty)
873 leave_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000874
Damien Miller95def091999-11-25 00:26:21 +1100875 /* Clear and free any buffers. */
876 memset(buf, 0, sizeof(buf));
877 buffer_free(&stdin_buffer);
878 buffer_free(&stdout_buffer);
879 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000880
Damien Miller95def091999-11-25 00:26:21 +1100881 /* Report bytes transferred, and transfer rates. */
882 total_time = get_current_time() - start_time;
883 debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds",
884 stdin_bytes, stdout_bytes, stderr_bytes, total_time);
885 if (total_time > 0)
886 debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f",
887 stdin_bytes / total_time, stdout_bytes / total_time,
888 stderr_bytes / total_time);
889
890 /* Return the exit status of the program. */
891 debug("Exit status %d", exit_status);
892 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000893}