5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
11 * Created: Sat Sep 23 12:23:57 1995 ylo
13 * The main loop for the interactive session (client side).
32 /* Flag indicating that stdin should be redirected from /dev/null. */
33 extern int stdin_null_flag;
36 * Name of the host we are connecting to. This is the name given on the
37 * command line, or the HostName specified for the user-supplied name in a
43 * Flag to indicate that we have received a window change signal which has
44 * not yet been processed. This will cause a message indicating the new
45 * window size to be sent to the server a little later. This is volatile
46 * because this is updated in a signal handler.
48 static volatile int received_window_change_signal = 0;
50 /* Terminal modes, as saved by enter_raw_mode. */
51 static struct termios saved_tio;
54 * Flag indicating whether we are in raw mode. This is used by
55 * enter_raw_mode and leave_raw_mode.
57 static int in_raw_mode = 0;
59 /* Flag indicating whether the user\'s terminal is in non-blocking mode. */
60 static int in_non_blocking_mode = 0;
62 /* Common data for the client loop code. */
63 static int escape_pending; /* Last character was the escape character */
64 static int last_was_cr; /* Last character was a newline. */
65 static int exit_status; /* Used to store the exit status of the command. */
66 static int stdin_eof; /* EOF has been encountered on standard error. */
67 static Buffer stdin_buffer; /* Buffer for stdin data. */
68 static Buffer stdout_buffer; /* Buffer for stdout data. */
69 static Buffer stderr_buffer; /* Buffer for stderr data. */
70 static unsigned int buffer_high;/* Soft max buffer size. */
71 static int max_fd; /* Maximum file descriptor number in select(). */
72 static int connection_in; /* Connection to server (input). */
73 static int connection_out; /* Connection to server (output). */
74 static unsigned long stdin_bytes, stdout_bytes, stderr_bytes;
75 static int quit_pending; /* Set to non-zero to quit the client loop. */
76 static int escape_char; /* Escape character. */
78 /* Returns the user\'s terminal to normal mode if it had been put in raw mode. */
86 if (tcsetattr(fileno(stdin), TCSADRAIN, &saved_tio) < 0)
89 fatal_remove_cleanup((void (*) (void *)) leave_raw_mode, NULL);
92 /* Puts the user\'s terminal in raw mode. */
99 if (tcgetattr(fileno(stdin), &tio) < 0)
102 tio.c_iflag |= IGNPAR;
103 tio.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF);
104 tio.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
106 tio.c_lflag &= ~IEXTEN;
108 tio.c_oflag &= ~OPOST;
111 if (tcsetattr(fileno(stdin), TCSADRAIN, &tio) < 0)
115 fatal_add_cleanup((void (*) (void *)) leave_raw_mode, NULL);
118 /* Restores stdin to blocking mode. */
123 if (in_non_blocking_mode) {
124 (void) fcntl(fileno(stdin), F_SETFL, 0);
125 in_non_blocking_mode = 0;
126 fatal_remove_cleanup((void (*) (void *)) leave_non_blocking, NULL);
130 /* Puts stdin terminal in non-blocking mode. */
135 in_non_blocking_mode = 1;
136 (void) fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
137 fatal_add_cleanup((void (*) (void *)) leave_non_blocking, NULL);
141 * Signal handler for the window change signal (SIGWINCH). This just sets a
142 * flag indicating that the window has changed.
146 window_change_handler(int sig)
148 received_window_change_signal = 1;
149 signal(SIGWINCH, window_change_handler);
153 * Signal handler for signals that cause the program to terminate. These
154 * signals must be trapped to restore terminal modes.
158 signal_handler(int sig)
162 if (in_non_blocking_mode)
163 leave_non_blocking();
164 channel_stop_listening();
166 fatal("Killed by signal %d.", sig);
170 * Returns current time in seconds from Jan 1, 1970 with the maximum
171 * available resolution.
178 gettimeofday(&tv, NULL);
179 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
183 * This is called when the interactive is entered. This checks if there is
184 * an EOF coming on stdin. We must check this explicitly, as select() does
185 * not appear to wake up when redirecting from /dev/null.
189 client_check_initial_eof_on_stdin()
195 * If standard input is to be "redirected from /dev/null", we simply
196 * mark that we have seen an EOF and send an EOF message to the
197 * server. Otherwise, we try to read a single character; it appears
198 * that for some files, such /dev/null, select() never wakes up for
199 * read for this descriptor, which means that we never get EOF. This
200 * way we will get the EOF if stdin comes from /dev/null or similar.
202 if (stdin_null_flag) {
203 /* Fake EOF on stdin. */
204 debug("Sending eof.");
206 packet_start(SSH_CMSG_EOF);
209 enter_non_blocking();
211 /* Check for immediate EOF on stdin. */
212 len = read(fileno(stdin), buf, 1);
214 /* EOF. Record that we have seen it and send EOF to server. */
215 debug("Sending eof.");
217 packet_start(SSH_CMSG_EOF);
219 } else if (len > 0) {
221 * Got data. We must store the data in the buffer,
222 * and also process it as an escape character if
225 if ((unsigned char) buf[0] == escape_char)
228 buffer_append(&stdin_buffer, buf, 1);
232 leave_non_blocking();
238 * Make packets from buffered stdin data, and buffer them for sending to the
243 client_make_packets_from_stdin_data()
247 /* Send buffered stdin data to the server. */
248 while (buffer_len(&stdin_buffer) > 0 &&
249 packet_not_very_much_data_to_write()) {
250 len = buffer_len(&stdin_buffer);
251 /* Keep the packets at reasonable size. */
252 if (len > packet_get_maxsize())
253 len = packet_get_maxsize();
254 packet_start(SSH_CMSG_STDIN_DATA);
255 packet_put_string(buffer_ptr(&stdin_buffer), len);
257 buffer_consume(&stdin_buffer, len);
258 /* If we have a pending EOF, send it now. */
259 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
260 packet_start(SSH_CMSG_EOF);
267 * Checks if the client window has changed, and sends a packet about it to
268 * the server if so. The actual change is detected elsewhere (by a software
269 * interrupt on Unix); this just checks the flag and sends a message if
274 client_check_window_change()
276 /* Send possible window change message to the server. */
277 if (received_window_change_signal) {
280 /* Clear the window change indicator. */
281 received_window_change_signal = 0;
283 /* Read new window size. */
284 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) >= 0) {
285 /* Successful, send the packet now. */
286 packet_start(SSH_CMSG_WINDOW_SIZE);
287 packet_put_int(ws.ws_row);
288 packet_put_int(ws.ws_col);
289 packet_put_int(ws.ws_xpixel);
290 packet_put_int(ws.ws_ypixel);
297 * Waits until the client can do something (some data becomes available on
298 * one of the file descriptors).
302 client_wait_until_can_do_something(fd_set * readset, fd_set * writeset)
304 /* Initialize select masks. */
307 /* Read from the connection, unless our buffers are full. */
308 if (buffer_len(&stdout_buffer) < buffer_high &&
309 buffer_len(&stderr_buffer) < buffer_high &&
310 channel_not_very_much_buffered_data())
311 FD_SET(connection_in, readset);
314 * Read from stdin, unless we have seen EOF or have very much
315 * buffered data to send to the server.
317 if (!stdin_eof && packet_not_very_much_data_to_write())
318 FD_SET(fileno(stdin), readset);
322 /* Add any selections by the channel mechanism. */
323 channel_prepare_select(readset, writeset);
325 /* Select server connection if have data to write to the server. */
326 if (packet_have_data_to_write())
327 FD_SET(connection_out, writeset);
329 /* Select stdout if have data in buffer. */
330 if (buffer_len(&stdout_buffer) > 0)
331 FD_SET(fileno(stdout), writeset);
333 /* Select stderr if have data in buffer. */
334 if (buffer_len(&stderr_buffer) > 0)
335 FD_SET(fileno(stderr), writeset);
337 /* Update maximum file descriptor number, if appropriate. */
338 if (channel_max_fd() > max_fd)
339 max_fd = channel_max_fd();
342 * Wait for something to happen. This will suspend the process until
343 * some selected descriptor can be read, written, or has some other
344 * event pending. Note: if you want to implement SSH_MSG_IGNORE
345 * messages to fool traffic analysis, this might be the place to do
346 * it: just have a random timeout for the select, and send a random
347 * SSH_MSG_IGNORE packet when the timeout expires.
350 if (select(max_fd + 1, readset, writeset, NULL, NULL) < 0) {
352 /* Some systems fail to clear these automatically. */
357 /* Note: we might still have data in the buffers. */
358 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
359 buffer_append(&stderr_buffer, buf, strlen(buf));
360 stderr_bytes += strlen(buf);
366 client_suspend_self()
368 struct winsize oldws, newws;
370 /* Flush stdout and stderr buffers. */
371 if (buffer_len(&stdout_buffer) > 0)
372 atomicio(write, fileno(stdout), buffer_ptr(&stdout_buffer),
373 buffer_len(&stdout_buffer));
374 if (buffer_len(&stderr_buffer) > 0)
375 atomicio(write, fileno(stderr), buffer_ptr(&stderr_buffer),
376 buffer_len(&stderr_buffer));
381 * Free (and clear) the buffer to reduce the amount of data that gets
384 buffer_free(&stdin_buffer);
385 buffer_free(&stdout_buffer);
386 buffer_free(&stderr_buffer);
388 /* Save old window size. */
389 ioctl(fileno(stdin), TIOCGWINSZ, &oldws);
391 /* Send the suspend signal to the program itself. */
392 kill(getpid(), SIGTSTP);
394 /* Check if the window size has changed. */
395 if (ioctl(fileno(stdin), TIOCGWINSZ, &newws) >= 0 &&
396 (oldws.ws_row != newws.ws_row ||
397 oldws.ws_col != newws.ws_col ||
398 oldws.ws_xpixel != newws.ws_xpixel ||
399 oldws.ws_ypixel != newws.ws_ypixel))
400 received_window_change_signal = 1;
402 /* OK, we have been continued by the user. Reinitialize buffers. */
403 buffer_init(&stdin_buffer);
404 buffer_init(&stdout_buffer);
405 buffer_init(&stderr_buffer);
411 client_process_input(fd_set * readset)
417 * Read input from the server, and add any such data to the buffer of
418 * the packet subsystem.
420 if (FD_ISSET(connection_in, readset)) {
421 /* Read as much as possible. */
422 len = read(connection_in, buf, sizeof(buf));
424 /* Received EOF. The remote host has closed the connection. */
425 snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n",
427 buffer_append(&stderr_buffer, buf, strlen(buf));
428 stderr_bytes += strlen(buf);
433 * There is a kernel bug on Solaris that causes select to
434 * sometimes wake up even though there is no data available.
436 if (len < 0 && errno == EAGAIN)
440 /* An error has encountered. Perhaps there is a network problem. */
441 snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n",
442 host, strerror(errno));
443 buffer_append(&stderr_buffer, buf, strlen(buf));
444 stderr_bytes += strlen(buf);
448 packet_process_incoming(buf, len);
450 /* Read input from stdin. */
451 if (FD_ISSET(fileno(stdin), readset)) {
452 /* Read as much as possible. */
453 len = read(fileno(stdin), buf, sizeof(buf));
456 * Received EOF or error. They are treated
457 * similarly, except that an error message is printed
458 * if it was an error condition.
461 snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno));
462 buffer_append(&stderr_buffer, buf, strlen(buf));
463 stderr_bytes += strlen(buf);
465 /* Mark that we have seen EOF. */
468 * Send an EOF message to the server unless there is
469 * data in the buffer. If there is data in the
470 * buffer, no message will be sent now. Code
471 * elsewhere will send the EOF when the buffer
472 * becomes empty if stdin_eof is set.
474 if (buffer_len(&stdin_buffer) == 0) {
475 packet_start(SSH_CMSG_EOF);
478 } else if (escape_char == -1) {
480 * Normal successful read, and no escape character.
481 * Just append the data to buffer.
483 buffer_append(&stdin_buffer, buf, len);
487 * Normal, successful read. But we have an escape character
488 * and have to process the characters one by one.
491 for (i = 0; i < len; i++) {
493 /* Get one character at a time. */
496 if (escape_pending) {
497 /* We have previously seen an escape character. */
498 /* Clear the flag now. */
500 /* Process the escaped character. */
503 /* Terminate the connection. */
504 snprintf(buf, sizeof buf, "%c.\r\n", escape_char);
505 buffer_append(&stderr_buffer, buf, strlen(buf));
506 stderr_bytes += strlen(buf);
511 /* Suspend the program. */
512 /* Print a message to that effect to the user. */
513 snprintf(buf, sizeof buf, "%c^Z\r\n", escape_char);
514 buffer_append(&stderr_buffer, buf, strlen(buf));
515 stderr_bytes += strlen(buf);
517 /* Restore terminal modes and suspend. */
518 client_suspend_self();
520 /* We have been continued. */
525 * Detach the program (continue to serve connections,
526 * but put in background and no more new connections).
530 * Sending SSH_CMSG_EOF alone does not always appear
531 * to be enough. So we try to send an EOF character
534 packet_start(SSH_CMSG_STDIN_DATA);
535 packet_put_string("\004", 1);
539 if (buffer_len(&stdin_buffer) == 0) {
540 packet_start(SSH_CMSG_EOF);
544 /* Restore tty modes. */
547 /* Stop listening for new connections. */
548 channel_stop_listening();
550 printf("%c& [backgrounded]\n", escape_char);
552 /* Fork into background. */
555 error("fork: %.100s", strerror(errno));
558 if (pid != 0) { /* This is the parent. */
559 /* The parent just exits. */
562 /* The child continues serving connections. */
566 snprintf(buf, sizeof buf,
568 Supported escape sequences:\r\n\
569 ~. - terminate connection\r\n\
570 ~^Z - suspend ssh\r\n\
571 ~# - list forwarded connections\r\n\
572 ~& - background ssh (when waiting for connections to terminate)\r\n\
573 ~? - this message\r\n\
574 ~~ - send the escape character by typing it twice\r\n\
575 (Note that escapes are only recognized immediately after newline.)\r\n",
577 buffer_append(&stderr_buffer, buf, strlen(buf));
581 snprintf(buf, sizeof buf, "%c#\r\n", escape_char);
582 buffer_append(&stderr_buffer, buf, strlen(buf));
583 s = channel_open_message();
584 buffer_append(&stderr_buffer, s, strlen(s));
589 if (ch != escape_char) {
591 * Escape character followed by non-special character.
592 * Append both to the input buffer.
594 buf[0] = escape_char;
596 buffer_append(&stdin_buffer, buf, 2);
601 * Note that escape character typed twice
602 * falls through here; the latter gets processed
603 * as a normal character below.
609 * The previous character was not an escape char. Check if this
612 if (last_was_cr && ch == escape_char) {
613 /* It is. Set the flag and continue to next character. */
620 * Normal character. Record whether it was a newline,
621 * and append it to the buffer.
623 last_was_cr = (ch == '\r' || ch == '\n');
625 buffer_append(&stdin_buffer, buf, 1);
634 client_process_output(fd_set * writeset)
639 /* Write buffered output to stdout. */
640 if (FD_ISSET(fileno(stdout), writeset)) {
641 /* Write as much data as possible. */
642 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
643 buffer_len(&stdout_buffer));
649 * An error or EOF was encountered. Put an
650 * error message to stderr buffer.
652 snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno));
653 buffer_append(&stderr_buffer, buf, strlen(buf));
654 stderr_bytes += strlen(buf);
659 /* Consume printed data from the buffer. */
660 buffer_consume(&stdout_buffer, len);
662 /* Write buffered output to stderr. */
663 if (FD_ISSET(fileno(stderr), writeset)) {
664 /* Write as much data as possible. */
665 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
666 buffer_len(&stderr_buffer));
671 /* EOF or error, but can't even print error message. */
676 /* Consume printed characters from the buffer. */
677 buffer_consume(&stderr_buffer, len);
682 * Get packets from the connection input buffer, and process them as long as
683 * there are packets available.
685 * Any unknown packets received during the actual
686 * session cause the session to terminate. This is
687 * intended to make debugging easier since no
688 * confirmations are sent. Any compatible protocol
689 * extensions must be negotiated during the
694 client_process_buffered_input_packets()
696 dispatch_run(DISPATCH_NONBLOCK, &quit_pending);
700 * Implements the interactive session with the server. This is called after
701 * the user has been authenticated, and a command has been started on the
702 * remote host. If escape_char != -1, it is the character used as an escape
703 * character for terminating or suspending the session.
706 void client_init_dispatch(void);
709 client_loop(int have_pty, int escape_char_arg)
711 extern Options options;
712 double start_time, total_time;
716 debug("Entering interactive session.");
718 start_time = get_current_time();
720 /* Initialize variables. */
725 buffer_high = 64 * 1024;
726 connection_in = packet_get_connection_in();
727 connection_out = packet_get_connection_out();
728 max_fd = connection_in;
729 if (connection_out > max_fd)
730 max_fd = connection_out;
735 escape_char = escape_char_arg;
737 /* Initialize buffers. */
738 buffer_init(&stdin_buffer);
739 buffer_init(&stdout_buffer);
740 buffer_init(&stderr_buffer);
742 client_init_dispatch();
744 /* Set signal handlers to restore non-blocking mode. */
745 signal(SIGINT, signal_handler);
746 signal(SIGQUIT, signal_handler);
747 signal(SIGTERM, signal_handler);
748 signal(SIGPIPE, SIG_IGN);
750 signal(SIGWINCH, window_change_handler);
755 /* Check if we should immediately send of on stdin. */
756 client_check_initial_eof_on_stdin();
758 /* Main loop of the client for the interactive session mode. */
759 while (!quit_pending) {
760 fd_set readset, writeset;
762 /* Process buffered packets sent by the server. */
763 client_process_buffered_input_packets();
766 * Make packets of buffered stdin data, and buffer them for
767 * sending to the server.
769 client_make_packets_from_stdin_data();
772 * Make packets from buffered channel data, and buffer them
773 * for sending to the server.
775 if (packet_not_very_much_data_to_write())
776 channel_output_poll();
779 * Check if the window size has changed, and buffer a message
780 * about it to the server if so.
782 client_check_window_change();
788 * Wait until we have something to do (something becomes
789 * available on one of the descriptors).
791 client_wait_until_can_do_something(&readset, &writeset);
796 /* Do channel operations. */
797 channel_after_select(&readset, &writeset);
800 * Process input from the connection and from stdin. Buffer
801 * any data that is available.
803 client_process_input(&readset);
806 * Process output to stdout and stderr. Output to the
807 * connection is processed elsewhere (above).
809 client_process_output(&writeset);
811 /* Send as much buffered packet data as possible to the sender. */
812 if (FD_ISSET(connection_out, &writeset))
816 /* Terminate the session. */
818 /* Stop watching for window change. */
820 signal(SIGWINCH, SIG_DFL);
822 /* Stop listening for connections. */
823 channel_stop_listening();
826 * In interactive mode (with pseudo tty) display a message indicating
827 * that the connection has been closed.
829 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
830 snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host);
831 buffer_append(&stderr_buffer, buf, strlen(buf));
832 stderr_bytes += strlen(buf);
834 /* Output any buffered data for stdout. */
835 while (buffer_len(&stdout_buffer) > 0) {
836 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
837 buffer_len(&stdout_buffer));
839 error("Write failed flushing stdout buffer.");
842 buffer_consume(&stdout_buffer, len);
845 /* Output any buffered data for stderr. */
846 while (buffer_len(&stderr_buffer) > 0) {
847 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
848 buffer_len(&stderr_buffer));
850 error("Write failed flushing stderr buffer.");
853 buffer_consume(&stderr_buffer, len);
859 /* Clear and free any buffers. */
860 memset(buf, 0, sizeof(buf));
861 buffer_free(&stdin_buffer);
862 buffer_free(&stdout_buffer);
863 buffer_free(&stderr_buffer);
865 /* Report bytes transferred, and transfer rates. */
866 total_time = get_current_time() - start_time;
867 debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds",
868 stdin_bytes, stdout_bytes, stderr_bytes, total_time);
870 debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f",
871 stdin_bytes / total_time, stdout_bytes / total_time,
872 stderr_bytes / total_time);
874 /* Return the exit status of the program. */
875 debug("Exit status %d", exit_status);
882 client_input_stdout_data(int type, int plen)
884 unsigned int data_len;
885 char *data = packet_get_string(&data_len);
886 packet_integrity_check(plen, 4 + data_len, type);
887 buffer_append(&stdout_buffer, data, data_len);
888 stdout_bytes += data_len;
889 memset(data, 0, data_len);
893 client_input_stderr_data(int type, int plen)
895 unsigned int data_len;
896 char *data = packet_get_string(&data_len);
897 packet_integrity_check(plen, 4 + data_len, type);
898 buffer_append(&stderr_buffer, data, data_len);
899 stdout_bytes += data_len;
900 memset(data, 0, data_len);
904 client_input_exit_status(int type, int plen)
906 packet_integrity_check(plen, 4, type);
907 exit_status = packet_get_int();
908 /* Acknowledge the exit. */
909 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
912 * Must wait for packet to be sent since we are
916 /* Flag that we want to exit. */
921 client_init_dispatch_13()
924 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
925 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
926 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
927 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
928 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
929 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
930 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
931 dispatch_set(SSH_SMSG_AGENT_OPEN, &auth_input_open_request);
932 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
933 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
934 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
935 dispatch_set(SSH_SMSG_X11_OPEN, &x11_input_open);
938 client_init_dispatch_15()
940 client_init_dispatch_13();
941 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
942 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
945 client_init_dispatch()
948 client_init_dispatch_13();
950 client_init_dispatch_15();