2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * The main loop for the interactive session (client side).
7 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
14 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * SSH2 support added by Markus Friedl.
38 * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
49 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 RCSID("$OpenBSD: clientloop.c,v 1.104 2002/08/22 19:38:42 stevesk Exp $");
79 #include "clientloop.h"
87 extern Options options;
89 /* Flag indicating that stdin should be redirected from /dev/null. */
90 extern int stdin_null_flag;
93 * Name of the host we are connecting to. This is the name given on the
94 * command line, or the HostName specified for the user-supplied name in a
100 * Flag to indicate that we have received a window change signal which has
101 * not yet been processed. This will cause a message indicating the new
102 * window size to be sent to the server a little later. This is volatile
103 * because this is updated in a signal handler.
105 static volatile sig_atomic_t received_window_change_signal = 0;
106 static volatile sig_atomic_t received_signal = 0;
108 /* Flag indicating whether the user\'s terminal is in non-blocking mode. */
109 static int in_non_blocking_mode = 0;
111 /* Common data for the client loop code. */
112 static int quit_pending; /* Set to non-zero to quit the client loop. */
113 static int escape_char; /* Escape character. */
114 static int escape_pending; /* Last character was the escape character */
115 static int last_was_cr; /* Last character was a newline. */
116 static int exit_status; /* Used to store the exit status of the command. */
117 static int stdin_eof; /* EOF has been encountered on standard error. */
118 static Buffer stdin_buffer; /* Buffer for stdin data. */
119 static Buffer stdout_buffer; /* Buffer for stdout data. */
120 static Buffer stderr_buffer; /* Buffer for stderr data. */
121 static u_long stdin_bytes, stdout_bytes, stderr_bytes;
122 static u_int buffer_high;/* Soft max buffer size. */
123 static int connection_in; /* Connection to server (input). */
124 static int connection_out; /* Connection to server (output). */
125 static int need_rekeying; /* Set to non-zero if rekeying is requested. */
126 static int session_closed = 0; /* In SSH2: login session closed. */
128 static void client_init_dispatch(void);
129 int session_ident = -1;
134 /* Restores stdin to blocking mode. */
137 leave_non_blocking(void)
139 if (in_non_blocking_mode) {
140 (void) fcntl(fileno(stdin), F_SETFL, 0);
141 in_non_blocking_mode = 0;
142 fatal_remove_cleanup((void (*) (void *)) leave_non_blocking, NULL);
146 /* Puts stdin terminal in non-blocking mode. */
149 enter_non_blocking(void)
151 in_non_blocking_mode = 1;
152 (void) fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
153 fatal_add_cleanup((void (*) (void *)) leave_non_blocking, NULL);
157 * Signal handler for the window change signal (SIGWINCH). This just sets a
158 * flag indicating that the window has changed.
162 window_change_handler(int sig)
164 received_window_change_signal = 1;
165 signal(SIGWINCH, window_change_handler);
169 * Signal handler for signals that cause the program to terminate. These
170 * signals must be trapped to restore terminal modes.
174 signal_handler(int sig)
176 received_signal = sig;
181 * Returns current time in seconds from Jan 1, 1970 with the maximum
182 * available resolution.
186 get_current_time(void)
189 gettimeofday(&tv, NULL);
190 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
194 * This is called when the interactive is entered. This checks if there is
195 * an EOF coming on stdin. We must check this explicitly, as select() does
196 * not appear to wake up when redirecting from /dev/null.
200 client_check_initial_eof_on_stdin(void)
206 * If standard input is to be "redirected from /dev/null", we simply
207 * mark that we have seen an EOF and send an EOF message to the
208 * server. Otherwise, we try to read a single character; it appears
209 * that for some files, such /dev/null, select() never wakes up for
210 * read for this descriptor, which means that we never get EOF. This
211 * way we will get the EOF if stdin comes from /dev/null or similar.
213 if (stdin_null_flag) {
214 /* Fake EOF on stdin. */
215 debug("Sending eof.");
217 packet_start(SSH_CMSG_EOF);
220 enter_non_blocking();
222 /* Check for immediate EOF on stdin. */
223 len = read(fileno(stdin), buf, 1);
225 /* EOF. Record that we have seen it and send EOF to server. */
226 debug("Sending eof.");
228 packet_start(SSH_CMSG_EOF);
230 } else if (len > 0) {
232 * Got data. We must store the data in the buffer,
233 * and also process it as an escape character if
236 if ((u_char) buf[0] == escape_char)
239 buffer_append(&stdin_buffer, buf, 1);
241 leave_non_blocking();
247 * Make packets from buffered stdin data, and buffer them for sending to the
252 client_make_packets_from_stdin_data(void)
256 /* Send buffered stdin data to the server. */
257 while (buffer_len(&stdin_buffer) > 0 &&
258 packet_not_very_much_data_to_write()) {
259 len = buffer_len(&stdin_buffer);
260 /* Keep the packets at reasonable size. */
261 if (len > packet_get_maxsize())
262 len = packet_get_maxsize();
263 packet_start(SSH_CMSG_STDIN_DATA);
264 packet_put_string(buffer_ptr(&stdin_buffer), len);
266 buffer_consume(&stdin_buffer, len);
268 /* If we have a pending EOF, send it now. */
269 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
270 packet_start(SSH_CMSG_EOF);
277 * Checks if the client window has changed, and sends a packet about it to
278 * the server if so. The actual change is detected elsewhere (by a software
279 * interrupt on Unix); this just checks the flag and sends a message if
284 client_check_window_change(void)
288 if (! received_window_change_signal)
291 received_window_change_signal = 0;
293 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
296 debug2("client_check_window_change: changed");
299 channel_request_start(session_ident, "window-change", 0);
300 packet_put_int(ws.ws_col);
301 packet_put_int(ws.ws_row);
302 packet_put_int(ws.ws_xpixel);
303 packet_put_int(ws.ws_ypixel);
306 packet_start(SSH_CMSG_WINDOW_SIZE);
307 packet_put_int(ws.ws_row);
308 packet_put_int(ws.ws_col);
309 packet_put_int(ws.ws_xpixel);
310 packet_put_int(ws.ws_ypixel);
316 * Waits until the client can do something (some data becomes available on
317 * one of the file descriptors).
321 client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
322 int *maxfdp, int *nallocp, int rekeying)
324 /* Add any selections by the channel mechanism. */
325 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, rekeying);
328 /* Read from the connection, unless our buffers are full. */
329 if (buffer_len(&stdout_buffer) < buffer_high &&
330 buffer_len(&stderr_buffer) < buffer_high &&
331 channel_not_very_much_buffered_data())
332 FD_SET(connection_in, *readsetp);
334 * Read from stdin, unless we have seen EOF or have very much
335 * buffered data to send to the server.
337 if (!stdin_eof && packet_not_very_much_data_to_write())
338 FD_SET(fileno(stdin), *readsetp);
340 /* Select stdout/stderr if have data in buffer. */
341 if (buffer_len(&stdout_buffer) > 0)
342 FD_SET(fileno(stdout), *writesetp);
343 if (buffer_len(&stderr_buffer) > 0)
344 FD_SET(fileno(stderr), *writesetp);
346 /* channel_prepare_select could have closed the last channel */
347 if (session_closed && !channel_still_open() &&
348 !packet_have_data_to_write()) {
349 /* clear mask since we did not call select() */
350 memset(*readsetp, 0, *nallocp);
351 memset(*writesetp, 0, *nallocp);
354 FD_SET(connection_in, *readsetp);
358 /* Select server connection if have data to write to the server. */
359 if (packet_have_data_to_write())
360 FD_SET(connection_out, *writesetp);
363 * Wait for something to happen. This will suspend the process until
364 * some selected descriptor can be read, written, or has some other
365 * event pending. Note: if you want to implement SSH_MSG_IGNORE
366 * messages to fool traffic analysis, this might be the place to do
367 * it: just have a random timeout for the select, and send a random
368 * SSH_MSG_IGNORE packet when the timeout expires.
371 if (select((*maxfdp)+1, *readsetp, *writesetp, NULL, NULL) < 0) {
375 * We have to clear the select masks, because we return.
376 * We have to return, because the mainloop checks for the flags
377 * set by the signal handlers.
379 memset(*readsetp, 0, *nallocp);
380 memset(*writesetp, 0, *nallocp);
384 /* Note: we might still have data in the buffers. */
385 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
386 buffer_append(&stderr_buffer, buf, strlen(buf));
392 client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
394 struct winsize oldws, newws;
396 /* Flush stdout and stderr buffers. */
397 if (buffer_len(bout) > 0)
398 atomicio(write, fileno(stdout), buffer_ptr(bout), buffer_len(bout));
399 if (buffer_len(berr) > 0)
400 atomicio(write, fileno(stderr), buffer_ptr(berr), buffer_len(berr));
405 * Free (and clear) the buffer to reduce the amount of data that gets
412 /* Save old window size. */
413 ioctl(fileno(stdin), TIOCGWINSZ, &oldws);
415 /* Send the suspend signal to the program itself. */
416 kill(getpid(), SIGTSTP);
418 /* Check if the window size has changed. */
419 if (ioctl(fileno(stdin), TIOCGWINSZ, &newws) >= 0 &&
420 (oldws.ws_row != newws.ws_row ||
421 oldws.ws_col != newws.ws_col ||
422 oldws.ws_xpixel != newws.ws_xpixel ||
423 oldws.ws_ypixel != newws.ws_ypixel))
424 received_window_change_signal = 1;
426 /* OK, we have been continued by the user. Reinitialize buffers. */
435 client_process_net_input(fd_set * readset)
441 * Read input from the server, and add any such data to the buffer of
442 * the packet subsystem.
444 if (FD_ISSET(connection_in, readset)) {
445 /* Read as much as possible. */
446 len = read(connection_in, buf, sizeof(buf));
448 /* Received EOF. The remote host has closed the connection. */
449 snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n",
451 buffer_append(&stderr_buffer, buf, strlen(buf));
456 * There is a kernel bug on Solaris that causes select to
457 * sometimes wake up even though there is no data available.
459 if (len < 0 && (errno == EAGAIN || errno == EINTR))
463 /* An error has encountered. Perhaps there is a network problem. */
464 snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n",
465 host, strerror(errno));
466 buffer_append(&stderr_buffer, buf, strlen(buf));
470 packet_process_incoming(buf, len);
475 process_cmdline(void)
477 void (*handler)(int);
479 u_short fwd_port, fwd_host_port;
480 char buf[1024], sfwd_port[6], sfwd_host_port[6];
484 handler = signal(SIGINT, SIG_IGN);
485 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
488 while (*s && isspace(*s))
492 if (strlen(s) < 2 || s[0] != '-' || !(s[1] == 'L' || s[1] == 'R')) {
493 log("Invalid command.");
498 if (!local && !compat20) {
499 log("Not supported for SSH protocol version 1.");
503 while (*s && isspace(*s))
506 if (sscanf(s, "%5[0-9]:%255[^:]:%5[0-9]",
507 sfwd_port, buf, sfwd_host_port) != 3 &&
508 sscanf(s, "%5[0-9]/%255[^/]/%5[0-9]",
509 sfwd_port, buf, sfwd_host_port) != 3) {
510 log("Bad forwarding specification.");
513 if ((fwd_port = a2port(sfwd_port)) == 0 ||
514 (fwd_host_port = a2port(sfwd_host_port)) == 0) {
515 log("Bad forwarding port(s).");
519 if (channel_setup_local_fwd_listener(fwd_port, buf,
520 fwd_host_port, options.gateway_ports) < 0) {
521 log("Port forwarding failed.");
525 channel_request_remote_forwarding(fwd_port, buf,
527 log("Forwarding port.");
529 signal(SIGINT, handler);
535 /* process the characters one by one */
537 process_escapes(Buffer *bin, Buffer *bout, Buffer *berr, char *buf, int len)
546 for (i = 0; i < len; i++) {
547 /* Get one character at a time. */
550 if (escape_pending) {
551 /* We have previously seen an escape character. */
552 /* Clear the flag now. */
555 /* Process the escaped character. */
558 /* Terminate the connection. */
559 snprintf(string, sizeof string, "%c.\r\n", escape_char);
560 buffer_append(berr, string, strlen(string));
566 /* Suspend the program. */
567 /* Print a message to that effect to the user. */
568 snprintf(string, sizeof string, "%c^Z [suspend ssh]\r\n", escape_char);
569 buffer_append(berr, string, strlen(string));
571 /* Restore terminal modes and suspend. */
572 client_suspend_self(bin, bout, berr);
574 /* We have been continued. */
579 if (datafellows & SSH_BUG_NOREKEY)
580 log("Server does not support re-keying");
588 * Detach the program (continue to serve connections,
589 * but put in background and no more new connections).
591 /* Restore tty modes. */
594 /* Stop listening for new connections. */
595 channel_stop_listening();
597 snprintf(string, sizeof string,
598 "%c& [backgrounded]\n", escape_char);
599 buffer_append(berr, string, strlen(string));
601 /* Fork into background. */
604 error("fork: %.100s", strerror(errno));
607 if (pid != 0) { /* This is the parent. */
608 /* The parent just exits. */
611 /* The child continues serving connections. */
613 buffer_append(bin, "\004", 1);
614 /* fake EOF on stdin */
616 } else if (!stdin_eof) {
618 * Sending SSH_CMSG_EOF alone does not always appear
619 * to be enough. So we try to send an EOF character
622 packet_start(SSH_CMSG_STDIN_DATA);
623 packet_put_string("\004", 1);
627 if (buffer_len(bin) == 0) {
628 packet_start(SSH_CMSG_EOF);
635 snprintf(string, sizeof string,
637 Supported escape sequences:\r\n\
638 %c. - terminate connection\r\n\
639 %cC - open a command line\r\n\
640 %cR - Request rekey (SSH protocol 2 only)\r\n\
641 %c^Z - suspend ssh\r\n\
642 %c# - list forwarded connections\r\n\
643 %c& - background ssh (when waiting for connections to terminate)\r\n\
644 %c? - this message\r\n\
645 %c%c - send the escape character by typing it twice\r\n\
646 (Note that escapes are only recognized immediately after newline.)\r\n",
647 escape_char, escape_char, escape_char, escape_char,
648 escape_char, escape_char, escape_char, escape_char,
649 escape_char, escape_char);
650 buffer_append(berr, string, strlen(string));
654 snprintf(string, sizeof string, "%c#\r\n", escape_char);
655 buffer_append(berr, string, strlen(string));
656 s = channel_open_message();
657 buffer_append(berr, s, strlen(s));
666 if (ch != escape_char) {
667 buffer_put_char(bin, escape_char);
670 /* Escaped characters fall through here */
675 * The previous character was not an escape char. Check if this
678 if (last_was_cr && ch == escape_char) {
679 /* It is. Set the flag and continue to next character. */
686 * Normal character. Record whether it was a newline,
687 * and append it to the buffer.
689 last_was_cr = (ch == '\r' || ch == '\n');
690 buffer_put_char(bin, ch);
697 client_process_input(fd_set * readset)
702 /* Read input from stdin. */
703 if (FD_ISSET(fileno(stdin), readset)) {
704 /* Read as much as possible. */
705 len = read(fileno(stdin), buf, sizeof(buf));
706 if (len < 0 && (errno == EAGAIN || errno == EINTR))
707 return; /* we'll try again later */
710 * Received EOF or error. They are treated
711 * similarly, except that an error message is printed
712 * if it was an error condition.
715 snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno));
716 buffer_append(&stderr_buffer, buf, strlen(buf));
718 /* Mark that we have seen EOF. */
721 * Send an EOF message to the server unless there is
722 * data in the buffer. If there is data in the
723 * buffer, no message will be sent now. Code
724 * elsewhere will send the EOF when the buffer
725 * becomes empty if stdin_eof is set.
727 if (buffer_len(&stdin_buffer) == 0) {
728 packet_start(SSH_CMSG_EOF);
731 } else if (escape_char == SSH_ESCAPECHAR_NONE) {
733 * Normal successful read, and no escape character.
734 * Just append the data to buffer.
736 buffer_append(&stdin_buffer, buf, len);
739 * Normal, successful read. But we have an escape character
740 * and have to process the characters one by one.
742 if (process_escapes(&stdin_buffer, &stdout_buffer,
743 &stderr_buffer, buf, len) == -1)
750 client_process_output(fd_set * writeset)
755 /* Write buffered output to stdout. */
756 if (FD_ISSET(fileno(stdout), writeset)) {
757 /* Write as much data as possible. */
758 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
759 buffer_len(&stdout_buffer));
761 if (errno == EINTR || errno == EAGAIN)
765 * An error or EOF was encountered. Put an
766 * error message to stderr buffer.
768 snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno));
769 buffer_append(&stderr_buffer, buf, strlen(buf));
774 /* Consume printed data from the buffer. */
775 buffer_consume(&stdout_buffer, len);
778 /* Write buffered output to stderr. */
779 if (FD_ISSET(fileno(stderr), writeset)) {
780 /* Write as much data as possible. */
781 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
782 buffer_len(&stderr_buffer));
784 if (errno == EINTR || errno == EAGAIN)
787 /* EOF or error, but can't even print error message. */
792 /* Consume printed characters from the buffer. */
793 buffer_consume(&stderr_buffer, len);
799 * Get packets from the connection input buffer, and process them as long as
800 * there are packets available.
802 * Any unknown packets received during the actual
803 * session cause the session to terminate. This is
804 * intended to make debugging easier since no
805 * confirmations are sent. Any compatible protocol
806 * extensions must be negotiated during the
811 client_process_buffered_input_packets(void)
813 dispatch_run(DISPATCH_NONBLOCK, &quit_pending, compat20 ? xxx_kex : NULL);
816 /* scan buf[] for '~' before sending data to the peer */
819 simple_escape_filter(Channel *c, char *buf, int len)
821 /* XXX we assume c->extended is writeable */
822 return process_escapes(&c->input, &c->output, &c->extended, buf, len);
826 client_channel_closed(int id, void *arg)
828 if (id != session_ident)
829 error("client_channel_closed: id %d != session_ident %d",
831 channel_cancel_cleanup(id);
838 * Implements the interactive session with the server. This is called after
839 * the user has been authenticated, and a command has been started on the
840 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
841 * used as an escape character for terminating or suspending the session.
845 client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
847 fd_set *readset = NULL, *writeset = NULL;
848 double start_time, total_time;
849 int max_fd = 0, max_fd2 = 0, len, rekeying = 0, nalloc = 0;
852 debug("Entering interactive session.");
854 start_time = get_current_time();
856 /* Initialize variables. */
861 buffer_high = 64 * 1024;
862 connection_in = packet_get_connection_in();
863 connection_out = packet_get_connection_out();
864 max_fd = MAX(connection_in, connection_out);
867 /* enable nonblocking unless tty */
868 if (!isatty(fileno(stdin)))
869 set_nonblock(fileno(stdin));
870 if (!isatty(fileno(stdout)))
871 set_nonblock(fileno(stdout));
872 if (!isatty(fileno(stderr)))
873 set_nonblock(fileno(stderr));
874 max_fd = MAX(max_fd, fileno(stdin));
875 max_fd = MAX(max_fd, fileno(stdout));
876 max_fd = MAX(max_fd, fileno(stderr));
882 escape_char = escape_char_arg;
884 /* Initialize buffers. */
885 buffer_init(&stdin_buffer);
886 buffer_init(&stdout_buffer);
887 buffer_init(&stderr_buffer);
889 client_init_dispatch();
891 /* Set signal handlers to restore non-blocking mode. */
892 signal(SIGINT, signal_handler);
893 signal(SIGQUIT, signal_handler);
894 signal(SIGTERM, signal_handler);
896 signal(SIGWINCH, window_change_handler);
902 session_ident = ssh2_chan_id;
903 if (escape_char != SSH_ESCAPECHAR_NONE)
904 channel_register_filter(session_ident,
905 simple_escape_filter);
906 if (session_ident != -1)
907 channel_register_cleanup(session_ident,
908 client_channel_closed);
910 /* Check if we should immediately send eof on stdin. */
911 client_check_initial_eof_on_stdin();
914 /* Main loop of the client for the interactive session mode. */
915 while (!quit_pending) {
917 /* Process buffered packets sent by the server. */
918 client_process_buffered_input_packets();
920 if (compat20 && session_closed && !channel_still_open())
923 rekeying = (xxx_kex != NULL && !xxx_kex->done);
926 debug("rekeying in progress");
929 * Make packets of buffered stdin data, and buffer
930 * them for sending to the server.
933 client_make_packets_from_stdin_data();
936 * Make packets from buffered channel data, and
937 * enqueue them for sending to the server.
939 if (packet_not_very_much_data_to_write())
940 channel_output_poll();
943 * Check if the window size has changed, and buffer a
944 * message about it to the server if so.
946 client_check_window_change();
952 * Wait until we have something to do (something becomes
953 * available on one of the descriptors).
956 client_wait_until_can_do_something(&readset, &writeset,
957 &max_fd2, &nalloc, rekeying);
962 /* Do channel operations unless rekeying in progress. */
964 channel_after_select(readset, writeset);
967 debug("user requests rekeying");
969 kex_send_kexinit(xxx_kex);
974 /* Buffer input from the connection. */
975 client_process_net_input(readset);
981 /* Buffer data from stdin */
982 client_process_input(readset);
984 * Process output to stdout and stderr. Output to
985 * the connection is processed elsewhere (above).
987 client_process_output(writeset);
990 /* Send as much buffered packet data as possible to the sender. */
991 if (FD_ISSET(connection_out, writeset))
999 /* Terminate the session. */
1001 /* Stop watching for window change. */
1003 signal(SIGWINCH, SIG_DFL);
1010 /* restore blocking io */
1011 if (!isatty(fileno(stdin)))
1012 unset_nonblock(fileno(stdin));
1013 if (!isatty(fileno(stdout)))
1014 unset_nonblock(fileno(stdout));
1015 if (!isatty(fileno(stderr)))
1016 unset_nonblock(fileno(stderr));
1018 if (received_signal) {
1019 if (in_non_blocking_mode) /* XXX */
1020 leave_non_blocking();
1021 fatal("Killed by signal %d.", (int) received_signal);
1025 * In interactive mode (with pseudo tty) display a message indicating
1026 * that the connection has been closed.
1028 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
1029 snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host);
1030 buffer_append(&stderr_buffer, buf, strlen(buf));
1033 /* Output any buffered data for stdout. */
1034 while (buffer_len(&stdout_buffer) > 0) {
1035 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
1036 buffer_len(&stdout_buffer));
1038 error("Write failed flushing stdout buffer.");
1041 buffer_consume(&stdout_buffer, len);
1042 stdout_bytes += len;
1045 /* Output any buffered data for stderr. */
1046 while (buffer_len(&stderr_buffer) > 0) {
1047 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
1048 buffer_len(&stderr_buffer));
1050 error("Write failed flushing stderr buffer.");
1053 buffer_consume(&stderr_buffer, len);
1054 stderr_bytes += len;
1057 /* Clear and free any buffers. */
1058 memset(buf, 0, sizeof(buf));
1059 buffer_free(&stdin_buffer);
1060 buffer_free(&stdout_buffer);
1061 buffer_free(&stderr_buffer);
1063 /* Report bytes transferred, and transfer rates. */
1064 total_time = get_current_time() - start_time;
1065 debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds",
1066 stdin_bytes, stdout_bytes, stderr_bytes, total_time);
1068 debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f",
1069 stdin_bytes / total_time, stdout_bytes / total_time,
1070 stderr_bytes / total_time);
1072 /* Return the exit status of the program. */
1073 debug("Exit status %d", exit_status);
1080 client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
1083 char *data = packet_get_string(&data_len);
1085 buffer_append(&stdout_buffer, data, data_len);
1086 memset(data, 0, data_len);
1090 client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
1093 char *data = packet_get_string(&data_len);
1095 buffer_append(&stderr_buffer, data, data_len);
1096 memset(data, 0, data_len);
1100 client_input_exit_status(int type, u_int32_t seq, void *ctxt)
1102 exit_status = packet_get_int();
1104 /* Acknowledge the exit. */
1105 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1108 * Must wait for packet to be sent since we are
1111 packet_write_wait();
1112 /* Flag that we want to exit. */
1117 client_request_forwarded_tcpip(const char *request_type, int rchan)
1120 char *listen_address, *originator_address;
1121 int listen_port, originator_port;
1124 /* Get rest of the packet */
1125 listen_address = packet_get_string(NULL);
1126 listen_port = packet_get_int();
1127 originator_address = packet_get_string(NULL);
1128 originator_port = packet_get_int();
1131 debug("client_request_forwarded_tcpip: listen %s port %d, originator %s port %d",
1132 listen_address, listen_port, originator_address, originator_port);
1134 sock = channel_connect_by_listen_address(listen_port);
1136 xfree(originator_address);
1137 xfree(listen_address);
1140 c = channel_new("forwarded-tcpip",
1141 SSH_CHANNEL_CONNECTING, sock, sock, -1,
1142 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
1143 xstrdup(originator_address), 1);
1144 xfree(originator_address);
1145 xfree(listen_address);
1150 client_request_x11(const char *request_type, int rchan)
1154 int originator_port;
1157 if (!options.forward_x11) {
1158 error("Warning: ssh server tried X11 forwarding.");
1159 error("Warning: this is probably a break in attempt by a malicious server.");
1162 originator = packet_get_string(NULL);
1163 if (datafellows & SSH_BUG_X11FWD) {
1164 debug2("buggy server: x11 request w/o originator_port");
1165 originator_port = 0;
1167 originator_port = packet_get_int();
1170 /* XXX check permission */
1171 debug("client_request_x11: request from %s %d", originator,
1174 sock = x11_connect_display();
1177 c = channel_new("x11",
1178 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
1179 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0,
1186 client_request_agent(const char *request_type, int rchan)
1191 if (!options.forward_agent) {
1192 error("Warning: ssh server tried agent forwarding.");
1193 error("Warning: this is probably a break in attempt by a malicious server.");
1196 sock = ssh_get_authentication_socket();
1199 c = channel_new("authentication agent connection",
1200 SSH_CHANNEL_OPEN, sock, sock, -1,
1201 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
1202 xstrdup("authentication agent connection"), 1);
1207 /* XXXX move to generic input handler */
1209 client_input_channel_open(int type, u_int32_t seq, void *ctxt)
1214 u_int rmaxpack, rwindow, len;
1216 ctype = packet_get_string(&len);
1217 rchan = packet_get_int();
1218 rwindow = packet_get_int();
1219 rmaxpack = packet_get_int();
1221 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
1222 ctype, rchan, rwindow, rmaxpack);
1224 if (strcmp(ctype, "forwarded-tcpip") == 0) {
1225 c = client_request_forwarded_tcpip(ctype, rchan);
1226 } else if (strcmp(ctype, "x11") == 0) {
1227 c = client_request_x11(ctype, rchan);
1228 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1229 c = client_request_agent(ctype, rchan);
1231 /* XXX duplicate : */
1233 debug("confirm %s", ctype);
1234 c->remote_id = rchan;
1235 c->remote_window = rwindow;
1236 c->remote_maxpacket = rmaxpack;
1237 if (c->type != SSH_CHANNEL_CONNECTING) {
1238 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1239 packet_put_int(c->remote_id);
1240 packet_put_int(c->self);
1241 packet_put_int(c->local_window);
1242 packet_put_int(c->local_maxpacket);
1246 debug("failure %s", ctype);
1247 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1248 packet_put_int(rchan);
1249 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
1250 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1251 packet_put_cstring("open failed");
1252 packet_put_cstring("");
1259 client_input_channel_req(int type, u_int32_t seq, void *ctxt)
1262 int id, reply, success = 0;
1265 id = packet_get_int();
1266 rtype = packet_get_string(NULL);
1267 reply = packet_get_char();
1269 debug("client_input_channel_req: channel %d rtype %s reply %d",
1272 if (session_ident == -1) {
1273 error("client_input_channel_req: no channel %d", session_ident);
1274 } else if (id != session_ident) {
1275 error("client_input_channel_req: channel %d: wrong channel: %d",
1278 c = channel_lookup(id);
1280 error("client_input_channel_req: channel %d: unknown channel", id);
1281 } else if (strcmp(rtype, "exit-status") == 0) {
1283 exit_status = packet_get_int();
1287 packet_start(success ?
1288 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1289 packet_put_int(c->remote_id);
1295 client_input_global_request(int type, u_int32_t seq, void *ctxt)
1301 rtype = packet_get_string(NULL);
1302 want_reply = packet_get_char();
1303 debug("client_input_global_request: rtype %s want_reply %d", rtype, want_reply);
1305 packet_start(success ?
1306 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
1308 packet_write_wait();
1314 client_init_dispatch_20(void)
1316 dispatch_init(&dispatch_protocol_error);
1318 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
1319 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
1320 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
1321 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
1322 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
1323 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1324 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1325 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
1326 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
1327 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
1330 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
1332 /* global request reply messages */
1333 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
1334 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
1337 client_init_dispatch_13(void)
1339 dispatch_init(NULL);
1340 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
1341 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
1342 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
1343 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1344 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1345 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
1346 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
1347 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
1348 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
1350 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
1351 &auth_input_open_request : &deny_input_open);
1352 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
1353 &x11_input_open : &deny_input_open);
1356 client_init_dispatch_15(void)
1358 client_init_dispatch_13();
1359 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
1360 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
1363 client_init_dispatch(void)
1366 client_init_dispatch_20();
1368 client_init_dispatch_13();
1370 client_init_dispatch_15();