Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 5 | * The main loop for the interactive session (client side). |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 6 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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". |
| 12 | * |
| 13 | * |
| 14 | * Copyright (c) 1999 Theo de Raadt. All rights reserved. |
| 15 | * |
| 16 | * Redistribution and use in source and binary forms, with or without |
| 17 | * modification, are permitted provided that the following conditions |
| 18 | * are met: |
| 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. |
| 24 | * |
| 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. |
| 35 | * |
| 36 | * |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 37 | * SSH2 support added by Markus Friedl. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 38 | * Copyright (c) 1999,2000 Markus Friedl. All rights reserved. |
| 39 | * |
| 40 | * Redistribution and use in source and binary forms, with or without |
| 41 | * modification, are permitted provided that the following conditions |
| 42 | * are met: |
| 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. |
| 48 | * |
| 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. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 59 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 60 | |
| 61 | #include "includes.h" |
Ben Lindstrom | 302ea6f | 2001-04-16 02:01:25 +0000 | [diff] [blame] | 62 | RCSID("$OpenBSD: clientloop.c,v 1.63 2001/04/15 17:16:00 markus Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 63 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 64 | #include "ssh.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 65 | #include "ssh1.h" |
| 66 | #include "ssh2.h" |
| 67 | #include "xmalloc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 68 | #include "packet.h" |
| 69 | #include "buffer.h" |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 70 | #include "compat.h" |
| 71 | #include "channels.h" |
| 72 | #include "dispatch.h" |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 73 | #include "buffer.h" |
| 74 | #include "bufaux.h" |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 75 | #include "key.h" |
Ben Lindstrom | f28f634 | 2001-04-04 02:03:04 +0000 | [diff] [blame] | 76 | #include "kex.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 77 | #include "log.h" |
| 78 | #include "readconf.h" |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 79 | #include "clientloop.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 80 | #include "authfd.h" |
| 81 | #include "atomicio.h" |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 82 | #include "sshtty.h" |
Ben Lindstrom | 302ea6f | 2001-04-16 02:01:25 +0000 | [diff] [blame] | 83 | #include "misc.h" |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 84 | |
| 85 | /* import options */ |
| 86 | extern Options options; |
| 87 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 88 | /* Flag indicating that stdin should be redirected from /dev/null. */ |
| 89 | extern int stdin_null_flag; |
| 90 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 91 | /* |
| 92 | * Name of the host we are connecting to. This is the name given on the |
| 93 | * command line, or the HostName specified for the user-supplied name in a |
| 94 | * configuration file. |
| 95 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 96 | extern char *host; |
| 97 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 98 | /* |
| 99 | * Flag to indicate that we have received a window change signal which has |
| 100 | * not yet been processed. This will cause a message indicating the new |
| 101 | * window size to be sent to the server a little later. This is volatile |
| 102 | * because this is updated in a signal handler. |
| 103 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 104 | static volatile int received_window_change_signal = 0; |
| 105 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 106 | /* Flag indicating whether the user\'s terminal is in non-blocking mode. */ |
| 107 | static int in_non_blocking_mode = 0; |
| 108 | |
| 109 | /* Common data for the client loop code. */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 110 | static int quit_pending; /* Set to non-zero to quit the client loop. */ |
| 111 | static int escape_char; /* Escape character. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 112 | static int escape_pending; /* Last character was the escape character */ |
| 113 | static int last_was_cr; /* Last character was a newline. */ |
| 114 | static int exit_status; /* Used to store the exit status of the command. */ |
| 115 | static int stdin_eof; /* EOF has been encountered on standard error. */ |
| 116 | static Buffer stdin_buffer; /* Buffer for stdin data. */ |
| 117 | static Buffer stdout_buffer; /* Buffer for stdout data. */ |
| 118 | static Buffer stderr_buffer; /* Buffer for stderr data. */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 119 | static u_long stdin_bytes, stdout_bytes, stderr_bytes; |
| 120 | static u_int buffer_high;/* Soft max buffer size. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 121 | static int connection_in; /* Connection to server (input). */ |
| 122 | static int connection_out; /* Connection to server (output). */ |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 123 | static int need_rekeying; /* Set to non-zero if rekeying is requested. */ |
Ben Lindstrom | 4c3f77d | 2001-04-05 23:37:36 +0000 | [diff] [blame] | 124 | static int session_closed = 0; /* In SSH2: login session closed. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 125 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 126 | void client_init_dispatch(void); |
| 127 | int session_ident = -1; |
| 128 | |
Ben Lindstrom | f28f634 | 2001-04-04 02:03:04 +0000 | [diff] [blame] | 129 | /*XXX*/ |
| 130 | extern Kex *xxx_kex; |
| 131 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 132 | /* Restores stdin to blocking mode. */ |
| 133 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 134 | void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 135 | leave_non_blocking(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 136 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 137 | if (in_non_blocking_mode) { |
| 138 | (void) fcntl(fileno(stdin), F_SETFL, 0); |
| 139 | in_non_blocking_mode = 0; |
| 140 | fatal_remove_cleanup((void (*) (void *)) leave_non_blocking, NULL); |
| 141 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 142 | } |
| 143 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 144 | /* Puts stdin terminal in non-blocking mode. */ |
| 145 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 146 | void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 147 | enter_non_blocking(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 148 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 149 | in_non_blocking_mode = 1; |
| 150 | (void) fcntl(fileno(stdin), F_SETFL, O_NONBLOCK); |
| 151 | fatal_add_cleanup((void (*) (void *)) leave_non_blocking, NULL); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 152 | } |
| 153 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 154 | /* |
| 155 | * Signal handler for the window change signal (SIGWINCH). This just sets a |
| 156 | * flag indicating that the window has changed. |
| 157 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 158 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 159 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 160 | window_change_handler(int sig) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 161 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 162 | received_window_change_signal = 1; |
| 163 | signal(SIGWINCH, window_change_handler); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 164 | } |
| 165 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 166 | /* |
| 167 | * Signal handler for signals that cause the program to terminate. These |
| 168 | * signals must be trapped to restore terminal modes. |
| 169 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 170 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 171 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 172 | signal_handler(int sig) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 173 | { |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 174 | if (in_raw_mode()) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 175 | leave_raw_mode(); |
| 176 | if (in_non_blocking_mode) |
| 177 | leave_non_blocking(); |
| 178 | channel_stop_listening(); |
| 179 | packet_close(); |
| 180 | fatal("Killed by signal %d.", sig); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 181 | } |
| 182 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 183 | /* |
| 184 | * Returns current time in seconds from Jan 1, 1970 with the maximum |
| 185 | * available resolution. |
| 186 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 187 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 188 | double |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 189 | get_current_time(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 190 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 191 | struct timeval tv; |
| 192 | gettimeofday(&tv, NULL); |
| 193 | return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 194 | } |
| 195 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 196 | /* |
| 197 | * This is called when the interactive is entered. This checks if there is |
| 198 | * an EOF coming on stdin. We must check this explicitly, as select() does |
| 199 | * not appear to wake up when redirecting from /dev/null. |
| 200 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 201 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 202 | void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 203 | client_check_initial_eof_on_stdin(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 204 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 205 | int len; |
| 206 | char buf[1]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 207 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 208 | /* |
| 209 | * If standard input is to be "redirected from /dev/null", we simply |
| 210 | * mark that we have seen an EOF and send an EOF message to the |
| 211 | * server. Otherwise, we try to read a single character; it appears |
| 212 | * that for some files, such /dev/null, select() never wakes up for |
| 213 | * read for this descriptor, which means that we never get EOF. This |
| 214 | * way we will get the EOF if stdin comes from /dev/null or similar. |
| 215 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 216 | if (stdin_null_flag) { |
| 217 | /* Fake EOF on stdin. */ |
| 218 | debug("Sending eof."); |
| 219 | stdin_eof = 1; |
| 220 | packet_start(SSH_CMSG_EOF); |
| 221 | packet_send(); |
| 222 | } else { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 223 | enter_non_blocking(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 224 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 225 | /* Check for immediate EOF on stdin. */ |
| 226 | len = read(fileno(stdin), buf, 1); |
| 227 | if (len == 0) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 228 | /* EOF. Record that we have seen it and send EOF to server. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 229 | debug("Sending eof."); |
| 230 | stdin_eof = 1; |
| 231 | packet_start(SSH_CMSG_EOF); |
| 232 | packet_send(); |
| 233 | } else if (len > 0) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 234 | /* |
| 235 | * Got data. We must store the data in the buffer, |
| 236 | * and also process it as an escape character if |
| 237 | * appropriate. |
| 238 | */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 239 | if ((u_char) buf[0] == escape_char) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 240 | escape_pending = 1; |
Ben Lindstrom | e9613cf | 2001-03-05 06:14:02 +0000 | [diff] [blame] | 241 | else |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 242 | buffer_append(&stdin_buffer, buf, 1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 243 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 244 | leave_non_blocking(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 245 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 246 | } |
| 247 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 248 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 249 | /* |
| 250 | * Make packets from buffered stdin data, and buffer them for sending to the |
| 251 | * connection. |
| 252 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 253 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 254 | void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 255 | client_make_packets_from_stdin_data(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 256 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 257 | u_int len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 258 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 259 | /* Send buffered stdin data to the server. */ |
| 260 | while (buffer_len(&stdin_buffer) > 0 && |
| 261 | packet_not_very_much_data_to_write()) { |
| 262 | len = buffer_len(&stdin_buffer); |
| 263 | /* Keep the packets at reasonable size. */ |
| 264 | if (len > packet_get_maxsize()) |
| 265 | len = packet_get_maxsize(); |
| 266 | packet_start(SSH_CMSG_STDIN_DATA); |
| 267 | packet_put_string(buffer_ptr(&stdin_buffer), len); |
| 268 | packet_send(); |
| 269 | buffer_consume(&stdin_buffer, len); |
Ben Lindstrom | e9613cf | 2001-03-05 06:14:02 +0000 | [diff] [blame] | 270 | stdin_bytes += len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 271 | /* If we have a pending EOF, send it now. */ |
| 272 | if (stdin_eof && buffer_len(&stdin_buffer) == 0) { |
| 273 | packet_start(SSH_CMSG_EOF); |
| 274 | packet_send(); |
| 275 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 276 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 277 | } |
| 278 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 279 | /* |
| 280 | * Checks if the client window has changed, and sends a packet about it to |
| 281 | * the server if so. The actual change is detected elsewhere (by a software |
| 282 | * interrupt on Unix); this just checks the flag and sends a message if |
| 283 | * appropriate. |
| 284 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 285 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 286 | void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 287 | client_check_window_change(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 288 | { |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 289 | struct winsize ws; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 290 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 291 | if (! received_window_change_signal) |
| 292 | return; |
| 293 | /** XXX race */ |
| 294 | received_window_change_signal = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 295 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 296 | if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0) |
| 297 | return; |
| 298 | |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 299 | debug2("client_check_window_change: changed"); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 300 | |
| 301 | if (compat20) { |
| 302 | channel_request_start(session_ident, "window-change", 0); |
| 303 | packet_put_int(ws.ws_col); |
| 304 | packet_put_int(ws.ws_row); |
| 305 | packet_put_int(ws.ws_xpixel); |
| 306 | packet_put_int(ws.ws_ypixel); |
| 307 | packet_send(); |
| 308 | } else { |
| 309 | packet_start(SSH_CMSG_WINDOW_SIZE); |
| 310 | packet_put_int(ws.ws_row); |
| 311 | packet_put_int(ws.ws_col); |
| 312 | packet_put_int(ws.ws_xpixel); |
| 313 | packet_put_int(ws.ws_ypixel); |
| 314 | packet_send(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 315 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 316 | } |
| 317 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 318 | /* |
| 319 | * Waits until the client can do something (some data becomes available on |
| 320 | * one of the file descriptors). |
| 321 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 322 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 323 | void |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 324 | client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp, |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 325 | int *maxfdp, int rekeying) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 326 | { |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 327 | /* Add any selections by the channel mechanism. */ |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 328 | channel_prepare_select(readsetp, writesetp, maxfdp, rekeying); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 329 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 330 | if (!compat20) { |
| 331 | /* Read from the connection, unless our buffers are full. */ |
| 332 | if (buffer_len(&stdout_buffer) < buffer_high && |
| 333 | buffer_len(&stderr_buffer) < buffer_high && |
| 334 | channel_not_very_much_buffered_data()) |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 335 | FD_SET(connection_in, *readsetp); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 336 | /* |
| 337 | * Read from stdin, unless we have seen EOF or have very much |
| 338 | * buffered data to send to the server. |
| 339 | */ |
| 340 | if (!stdin_eof && packet_not_very_much_data_to_write()) |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 341 | FD_SET(fileno(stdin), *readsetp); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 342 | |
| 343 | /* Select stdout/stderr if have data in buffer. */ |
| 344 | if (buffer_len(&stdout_buffer) > 0) |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 345 | FD_SET(fileno(stdout), *writesetp); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 346 | if (buffer_len(&stderr_buffer) > 0) |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 347 | FD_SET(fileno(stderr), *writesetp); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 348 | } else { |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 349 | FD_SET(connection_in, *readsetp); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 350 | } |
| 351 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 352 | /* Select server connection if have data to write to the server. */ |
| 353 | if (packet_have_data_to_write()) |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 354 | FD_SET(connection_out, *writesetp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 355 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 356 | /* |
| 357 | * Wait for something to happen. This will suspend the process until |
| 358 | * some selected descriptor can be read, written, or has some other |
| 359 | * event pending. Note: if you want to implement SSH_MSG_IGNORE |
| 360 | * messages to fool traffic analysis, this might be the place to do |
| 361 | * it: just have a random timeout for the select, and send a random |
| 362 | * SSH_MSG_IGNORE packet when the timeout expires. |
| 363 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 364 | |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 365 | if (select((*maxfdp)+1, *readsetp, *writesetp, NULL, NULL) < 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 366 | char buf[100]; |
Ben Lindstrom | f945251 | 2001-02-15 03:12:08 +0000 | [diff] [blame] | 367 | |
| 368 | /* |
| 369 | * We have to clear the select masks, because we return. |
| 370 | * We have to return, because the mainloop checks for the flags |
| 371 | * set by the signal handlers. |
| 372 | */ |
| 373 | memset(*readsetp, 0, *maxfdp); |
| 374 | memset(*writesetp, 0, *maxfdp); |
| 375 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 376 | if (errno == EINTR) |
| 377 | return; |
| 378 | /* Note: we might still have data in the buffers. */ |
| 379 | snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno)); |
| 380 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 381 | quit_pending = 1; |
| 382 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 383 | } |
| 384 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 385 | void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 386 | client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 387 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 388 | struct winsize oldws, newws; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 389 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 390 | /* Flush stdout and stderr buffers. */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 391 | if (buffer_len(bout) > 0) |
| 392 | atomicio(write, fileno(stdout), buffer_ptr(bout), buffer_len(bout)); |
| 393 | if (buffer_len(berr) > 0) |
| 394 | atomicio(write, fileno(stderr), buffer_ptr(berr), buffer_len(berr)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 395 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 396 | leave_raw_mode(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 397 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 398 | /* |
| 399 | * Free (and clear) the buffer to reduce the amount of data that gets |
| 400 | * written to swap. |
| 401 | */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 402 | buffer_free(bin); |
| 403 | buffer_free(bout); |
| 404 | buffer_free(berr); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 405 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 406 | /* Save old window size. */ |
| 407 | ioctl(fileno(stdin), TIOCGWINSZ, &oldws); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 408 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 409 | /* Send the suspend signal to the program itself. */ |
| 410 | kill(getpid(), SIGTSTP); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 411 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 412 | /* Check if the window size has changed. */ |
| 413 | if (ioctl(fileno(stdin), TIOCGWINSZ, &newws) >= 0 && |
| 414 | (oldws.ws_row != newws.ws_row || |
| 415 | oldws.ws_col != newws.ws_col || |
| 416 | oldws.ws_xpixel != newws.ws_xpixel || |
| 417 | oldws.ws_ypixel != newws.ws_ypixel)) |
| 418 | received_window_change_signal = 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 419 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 420 | /* OK, we have been continued by the user. Reinitialize buffers. */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 421 | buffer_init(bin); |
| 422 | buffer_init(bout); |
| 423 | buffer_init(berr); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 424 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 425 | enter_raw_mode(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 426 | } |
| 427 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 428 | void |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 429 | client_process_net_input(fd_set * readset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 430 | { |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 431 | int len; |
| 432 | char buf[8192]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 433 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 434 | /* |
| 435 | * Read input from the server, and add any such data to the buffer of |
| 436 | * the packet subsystem. |
| 437 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 438 | if (FD_ISSET(connection_in, readset)) { |
| 439 | /* Read as much as possible. */ |
| 440 | len = read(connection_in, buf, sizeof(buf)); |
| 441 | if (len == 0) { |
| 442 | /* Received EOF. The remote host has closed the connection. */ |
| 443 | snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n", |
| 444 | host); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 445 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 446 | quit_pending = 1; |
| 447 | return; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 448 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 449 | /* |
| 450 | * There is a kernel bug on Solaris that causes select to |
| 451 | * sometimes wake up even though there is no data available. |
| 452 | */ |
Ben Lindstrom | ebc8827 | 2001-03-06 03:34:40 +0000 | [diff] [blame] | 453 | if (len < 0 && (errno == EAGAIN || errno == EINTR)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 454 | len = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 455 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 456 | if (len < 0) { |
| 457 | /* An error has encountered. Perhaps there is a network problem. */ |
| 458 | snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n", |
| 459 | host, strerror(errno)); |
| 460 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 461 | quit_pending = 1; |
| 462 | return; |
| 463 | } |
| 464 | packet_process_incoming(buf, len); |
| 465 | } |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 466 | } |
| 467 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 468 | /* process the characters one by one */ |
| 469 | int |
| 470 | process_escapes(Buffer *bin, Buffer *bout, Buffer *berr, char *buf, int len) |
| 471 | { |
| 472 | char string[1024]; |
| 473 | pid_t pid; |
| 474 | int bytes = 0; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 475 | u_int i; |
| 476 | u_char ch; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 477 | char *s; |
| 478 | |
| 479 | for (i = 0; i < len; i++) { |
| 480 | /* Get one character at a time. */ |
| 481 | ch = buf[i]; |
| 482 | |
| 483 | if (escape_pending) { |
| 484 | /* We have previously seen an escape character. */ |
| 485 | /* Clear the flag now. */ |
| 486 | escape_pending = 0; |
| 487 | |
| 488 | /* Process the escaped character. */ |
| 489 | switch (ch) { |
| 490 | case '.': |
| 491 | /* Terminate the connection. */ |
| 492 | snprintf(string, sizeof string, "%c.\r\n", escape_char); |
| 493 | buffer_append(berr, string, strlen(string)); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 494 | |
| 495 | quit_pending = 1; |
| 496 | return -1; |
| 497 | |
| 498 | case 'Z' - 64: |
| 499 | /* Suspend the program. */ |
| 500 | /* Print a message to that effect to the user. */ |
| 501 | snprintf(string, sizeof string, "%c^Z [suspend ssh]\r\n", escape_char); |
| 502 | buffer_append(berr, string, strlen(string)); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 503 | |
| 504 | /* Restore terminal modes and suspend. */ |
| 505 | client_suspend_self(bin, bout, berr); |
| 506 | |
| 507 | /* We have been continued. */ |
| 508 | continue; |
| 509 | |
Ben Lindstrom | f28f634 | 2001-04-04 02:03:04 +0000 | [diff] [blame] | 510 | case 'R': |
Ben Lindstrom | 11bd899 | 2001-04-05 23:34:29 +0000 | [diff] [blame] | 511 | if (compat20) { |
| 512 | if (datafellows & SSH_BUG_NOREKEY) |
| 513 | log("Server does not support re-keying"); |
| 514 | else |
| 515 | need_rekeying = 1; |
| 516 | } |
Ben Lindstrom | f28f634 | 2001-04-04 02:03:04 +0000 | [diff] [blame] | 517 | continue; |
| 518 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 519 | case '&': |
| 520 | /* XXX does not work yet with proto 2 */ |
| 521 | if (compat20) |
| 522 | continue; |
| 523 | /* |
| 524 | * Detach the program (continue to serve connections, |
| 525 | * but put in background and no more new connections). |
| 526 | */ |
| 527 | if (!stdin_eof) { |
| 528 | /* |
| 529 | * Sending SSH_CMSG_EOF alone does not always appear |
| 530 | * to be enough. So we try to send an EOF character |
| 531 | * first. |
| 532 | */ |
| 533 | packet_start(SSH_CMSG_STDIN_DATA); |
| 534 | packet_put_string("\004", 1); |
| 535 | packet_send(); |
| 536 | /* Close stdin. */ |
| 537 | stdin_eof = 1; |
| 538 | if (buffer_len(bin) == 0) { |
| 539 | packet_start(SSH_CMSG_EOF); |
| 540 | packet_send(); |
| 541 | } |
| 542 | } |
| 543 | /* Restore tty modes. */ |
| 544 | leave_raw_mode(); |
| 545 | |
| 546 | /* Stop listening for new connections. */ |
| 547 | channel_stop_listening(); |
| 548 | |
| 549 | printf("%c& [backgrounded]\n", escape_char); |
| 550 | |
| 551 | /* Fork into background. */ |
| 552 | pid = fork(); |
| 553 | if (pid < 0) { |
| 554 | error("fork: %.100s", strerror(errno)); |
| 555 | continue; |
| 556 | } |
| 557 | if (pid != 0) { /* This is the parent. */ |
| 558 | /* The parent just exits. */ |
| 559 | exit(0); |
| 560 | } |
| 561 | /* The child continues serving connections. */ |
| 562 | continue; /*XXX ? */ |
| 563 | |
| 564 | case '?': |
| 565 | snprintf(string, sizeof string, |
| 566 | "%c?\r\n\ |
| 567 | Supported escape sequences:\r\n\ |
| 568 | ~. - terminate connection\r\n\ |
| 569 | ~^Z - suspend ssh\r\n\ |
| 570 | ~# - list forwarded connections\r\n\ |
| 571 | ~& - background ssh (when waiting for connections to terminate)\r\n\ |
| 572 | ~? - this message\r\n\ |
| 573 | ~~ - send the escape character by typing it twice\r\n\ |
| 574 | (Note that escapes are only recognized immediately after newline.)\r\n", |
| 575 | escape_char); |
| 576 | buffer_append(berr, string, strlen(string)); |
| 577 | continue; |
| 578 | |
| 579 | case '#': |
| 580 | snprintf(string, sizeof string, "%c#\r\n", escape_char); |
| 581 | buffer_append(berr, string, strlen(string)); |
| 582 | s = channel_open_message(); |
| 583 | buffer_append(berr, s, strlen(s)); |
| 584 | xfree(s); |
| 585 | continue; |
| 586 | |
| 587 | default: |
| 588 | if (ch != escape_char) { |
| 589 | buffer_put_char(bin, escape_char); |
| 590 | bytes++; |
| 591 | } |
| 592 | /* Escaped characters fall through here */ |
| 593 | break; |
| 594 | } |
| 595 | } else { |
| 596 | /* |
| 597 | * The previous character was not an escape char. Check if this |
| 598 | * is an escape. |
| 599 | */ |
| 600 | if (last_was_cr && ch == escape_char) { |
| 601 | /* It is. Set the flag and continue to next character. */ |
| 602 | escape_pending = 1; |
| 603 | continue; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | /* |
| 608 | * Normal character. Record whether it was a newline, |
| 609 | * and append it to the buffer. |
| 610 | */ |
| 611 | last_was_cr = (ch == '\r' || ch == '\n'); |
| 612 | buffer_put_char(bin, ch); |
| 613 | bytes++; |
| 614 | } |
| 615 | return bytes; |
| 616 | } |
| 617 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 618 | void |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 619 | client_process_input(fd_set * readset) |
| 620 | { |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 621 | int len; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 622 | char buf[8192]; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 623 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 624 | /* Read input from stdin. */ |
| 625 | if (FD_ISSET(fileno(stdin), readset)) { |
| 626 | /* Read as much as possible. */ |
| 627 | len = read(fileno(stdin), buf, sizeof(buf)); |
| 628 | if (len <= 0) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 629 | /* |
| 630 | * Received EOF or error. They are treated |
| 631 | * similarly, except that an error message is printed |
| 632 | * if it was an error condition. |
| 633 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 634 | if (len < 0) { |
| 635 | snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno)); |
| 636 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 637 | } |
| 638 | /* Mark that we have seen EOF. */ |
| 639 | stdin_eof = 1; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 640 | /* |
| 641 | * Send an EOF message to the server unless there is |
| 642 | * data in the buffer. If there is data in the |
| 643 | * buffer, no message will be sent now. Code |
| 644 | * elsewhere will send the EOF when the buffer |
| 645 | * becomes empty if stdin_eof is set. |
| 646 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 647 | if (buffer_len(&stdin_buffer) == 0) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 648 | packet_start(SSH_CMSG_EOF); |
| 649 | packet_send(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 650 | } |
| 651 | } else if (escape_char == -1) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 652 | /* |
| 653 | * Normal successful read, and no escape character. |
| 654 | * Just append the data to buffer. |
| 655 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 656 | buffer_append(&stdin_buffer, buf, len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 657 | } else { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 658 | /* |
| 659 | * Normal, successful read. But we have an escape character |
| 660 | * and have to process the characters one by one. |
| 661 | */ |
Ben Lindstrom | e9613cf | 2001-03-05 06:14:02 +0000 | [diff] [blame] | 662 | if (process_escapes(&stdin_buffer, &stdout_buffer, |
| 663 | &stderr_buffer, buf, len) == -1) |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 664 | return; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 665 | } |
| 666 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 667 | } |
| 668 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 669 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 670 | client_process_output(fd_set * writeset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 671 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 672 | int len; |
| 673 | char buf[100]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 674 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 675 | /* Write buffered output to stdout. */ |
| 676 | if (FD_ISSET(fileno(stdout), writeset)) { |
| 677 | /* Write as much data as possible. */ |
| 678 | len = write(fileno(stdout), buffer_ptr(&stdout_buffer), |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 679 | buffer_len(&stdout_buffer)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 680 | if (len <= 0) { |
| 681 | if (errno == EAGAIN) |
| 682 | len = 0; |
| 683 | else { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 684 | /* |
| 685 | * An error or EOF was encountered. Put an |
| 686 | * error message to stderr buffer. |
| 687 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 688 | snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno)); |
| 689 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 690 | quit_pending = 1; |
| 691 | return; |
| 692 | } |
| 693 | } |
| 694 | /* Consume printed data from the buffer. */ |
| 695 | buffer_consume(&stdout_buffer, len); |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 696 | stdout_bytes += len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 697 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 698 | /* Write buffered output to stderr. */ |
| 699 | if (FD_ISSET(fileno(stderr), writeset)) { |
| 700 | /* Write as much data as possible. */ |
| 701 | len = write(fileno(stderr), buffer_ptr(&stderr_buffer), |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 702 | buffer_len(&stderr_buffer)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 703 | if (len <= 0) { |
| 704 | if (errno == EAGAIN) |
| 705 | len = 0; |
| 706 | else { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 707 | /* EOF or error, but can't even print error message. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 708 | quit_pending = 1; |
| 709 | return; |
| 710 | } |
| 711 | } |
| 712 | /* Consume printed characters from the buffer. */ |
| 713 | buffer_consume(&stderr_buffer, len); |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 714 | stderr_bytes += len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 715 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 716 | } |
| 717 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 718 | /* |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 719 | * Get packets from the connection input buffer, and process them as long as |
| 720 | * there are packets available. |
| 721 | * |
| 722 | * Any unknown packets received during the actual |
| 723 | * session cause the session to terminate. This is |
| 724 | * intended to make debugging easier since no |
| 725 | * confirmations are sent. Any compatible protocol |
| 726 | * extensions must be negotiated during the |
| 727 | * preparatory phase. |
| 728 | */ |
| 729 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 730 | void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 731 | client_process_buffered_input_packets(void) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 732 | { |
Ben Lindstrom | f28f634 | 2001-04-04 02:03:04 +0000 | [diff] [blame] | 733 | dispatch_run(DISPATCH_NONBLOCK, &quit_pending, compat20 ? xxx_kex : NULL); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 734 | } |
| 735 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 736 | /* scan buf[] for '~' before sending data to the peer */ |
| 737 | |
| 738 | int |
| 739 | simple_escape_filter(Channel *c, char *buf, int len) |
| 740 | { |
| 741 | /* XXX we assume c->extended is writeable */ |
| 742 | return process_escapes(&c->input, &c->output, &c->extended, buf, len); |
| 743 | } |
| 744 | |
Ben Lindstrom | 4c3f77d | 2001-04-05 23:37:36 +0000 | [diff] [blame] | 745 | void |
| 746 | client_channel_closed(int id, void *arg) |
| 747 | { |
| 748 | if (id != session_ident) |
| 749 | error("client_channel_closed: id %d != session_ident %d", |
| 750 | id, session_ident); |
| 751 | session_closed = 1; |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 752 | if (in_raw_mode()) |
Ben Lindstrom | d344763 | 2001-04-08 18:07:22 +0000 | [diff] [blame] | 753 | leave_raw_mode(); |
Ben Lindstrom | 4c3f77d | 2001-04-05 23:37:36 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 756 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 757 | * Implements the interactive session with the server. This is called after |
| 758 | * the user has been authenticated, and a command has been started on the |
| 759 | * remote host. If escape_char != -1, it is the character used as an escape |
| 760 | * character for terminating or suspending the session. |
| 761 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 762 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 763 | int |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 764 | client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 765 | { |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 766 | fd_set *readset = NULL, *writeset = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 767 | double start_time, total_time; |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 768 | int max_fd = 0, len, rekeying = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 769 | char buf[100]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 770 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 771 | debug("Entering interactive session."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 772 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 773 | start_time = get_current_time(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 774 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 775 | /* Initialize variables. */ |
| 776 | escape_pending = 0; |
| 777 | last_was_cr = 1; |
| 778 | exit_status = -1; |
| 779 | stdin_eof = 0; |
| 780 | buffer_high = 64 * 1024; |
| 781 | connection_in = packet_get_connection_in(); |
| 782 | connection_out = packet_get_connection_out(); |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 783 | max_fd = MAX(connection_in, connection_out); |
| 784 | |
| 785 | if (!compat20) { |
Ben Lindstrom | 302ea6f | 2001-04-16 02:01:25 +0000 | [diff] [blame] | 786 | /* enable nonblocking unless tty */ |
| 787 | if (!isatty(fileno(stdin))) |
| 788 | set_nonblock(fileno(stdin)); |
| 789 | if (!isatty(fileno(stdout))) |
| 790 | set_nonblock(fileno(stdout)); |
| 791 | if (!isatty(fileno(stderr))) |
| 792 | set_nonblock(fileno(stderr)); |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 793 | max_fd = MAX(max_fd, fileno(stdin)); |
| 794 | max_fd = MAX(max_fd, fileno(stdout)); |
| 795 | max_fd = MAX(max_fd, fileno(stderr)); |
| 796 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 797 | stdin_bytes = 0; |
| 798 | stdout_bytes = 0; |
| 799 | stderr_bytes = 0; |
| 800 | quit_pending = 0; |
| 801 | escape_char = escape_char_arg; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 802 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 803 | /* Initialize buffers. */ |
| 804 | buffer_init(&stdin_buffer); |
| 805 | buffer_init(&stdout_buffer); |
| 806 | buffer_init(&stderr_buffer); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 807 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 808 | client_init_dispatch(); |
| 809 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 810 | /* Set signal handlers to restore non-blocking mode. */ |
| 811 | signal(SIGINT, signal_handler); |
| 812 | signal(SIGQUIT, signal_handler); |
| 813 | signal(SIGTERM, signal_handler); |
| 814 | signal(SIGPIPE, SIG_IGN); |
| 815 | if (have_pty) |
| 816 | signal(SIGWINCH, window_change_handler); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 817 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 818 | if (have_pty) |
| 819 | enter_raw_mode(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 820 | |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 821 | if (compat20) { |
| 822 | session_ident = ssh2_chan_id; |
| 823 | if (escape_char != -1) |
| 824 | channel_register_filter(session_ident, |
| 825 | simple_escape_filter); |
Ben Lindstrom | 4c3f77d | 2001-04-05 23:37:36 +0000 | [diff] [blame] | 826 | if (session_ident != -1) |
| 827 | channel_register_cleanup(session_ident, |
| 828 | client_channel_closed); |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 829 | } else { |
| 830 | /* Check if we should immediately send eof on stdin. */ |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 831 | client_check_initial_eof_on_stdin(); |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 832 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 833 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 834 | /* Main loop of the client for the interactive session mode. */ |
| 835 | while (!quit_pending) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 836 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 837 | /* Process buffered packets sent by the server. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 838 | client_process_buffered_input_packets(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 839 | |
Ben Lindstrom | 4c3f77d | 2001-04-05 23:37:36 +0000 | [diff] [blame] | 840 | if (compat20 && session_closed && !channel_still_open()) |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 841 | break; |
Ben Lindstrom | 4c3f77d | 2001-04-05 23:37:36 +0000 | [diff] [blame] | 842 | |
| 843 | rekeying = (xxx_kex != NULL && !xxx_kex->done); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 844 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 845 | if (rekeying) { |
| 846 | debug("rekeying in progress"); |
| 847 | } else { |
| 848 | /* |
| 849 | * Make packets of buffered stdin data, and buffer |
| 850 | * them for sending to the server. |
| 851 | */ |
| 852 | if (!compat20) |
| 853 | client_make_packets_from_stdin_data(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 854 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 855 | /* |
| 856 | * Make packets from buffered channel data, and |
| 857 | * enqueue them for sending to the server. |
| 858 | */ |
| 859 | if (packet_not_very_much_data_to_write()) |
| 860 | channel_output_poll(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 861 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 862 | /* |
| 863 | * Check if the window size has changed, and buffer a |
| 864 | * message about it to the server if so. |
| 865 | */ |
| 866 | client_check_window_change(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 867 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 868 | if (quit_pending) |
| 869 | break; |
| 870 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 871 | /* |
| 872 | * Wait until we have something to do (something becomes |
| 873 | * available on one of the descriptors). |
| 874 | */ |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 875 | client_wait_until_can_do_something(&readset, &writeset, |
| 876 | &max_fd, rekeying); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 877 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 878 | if (quit_pending) |
| 879 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 880 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 881 | /* Do channel operations unless rekeying in progress. */ |
| 882 | if (!rekeying) { |
| 883 | channel_after_select(readset, writeset); |
| 884 | |
| 885 | if (need_rekeying) { |
| 886 | debug("user requests rekeying"); |
| 887 | xxx_kex->done = 0; |
| 888 | kex_send_kexinit(xxx_kex); |
| 889 | need_rekeying = 0; |
| 890 | } |
| 891 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 892 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 893 | /* Buffer input from the connection. */ |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 894 | client_process_net_input(readset); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 895 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 896 | if (quit_pending) |
| 897 | break; |
| 898 | |
| 899 | if (!compat20) { |
| 900 | /* Buffer data from stdin */ |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 901 | client_process_input(readset); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 902 | /* |
| 903 | * Process output to stdout and stderr. Output to |
| 904 | * the connection is processed elsewhere (above). |
| 905 | */ |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 906 | client_process_output(writeset); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 907 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 908 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 909 | /* Send as much buffered packet data as possible to the sender. */ |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 910 | if (FD_ISSET(connection_out, writeset)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 911 | packet_write_poll(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 912 | } |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 913 | if (readset) |
| 914 | xfree(readset); |
| 915 | if (writeset) |
| 916 | xfree(writeset); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 917 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 918 | /* Terminate the session. */ |
| 919 | |
| 920 | /* Stop watching for window change. */ |
| 921 | if (have_pty) |
| 922 | signal(SIGWINCH, SIG_DFL); |
| 923 | |
| 924 | /* Stop listening for connections. */ |
| 925 | channel_stop_listening(); |
| 926 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 927 | /* |
| 928 | * In interactive mode (with pseudo tty) display a message indicating |
| 929 | * that the connection has been closed. |
| 930 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 931 | if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) { |
| 932 | snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host); |
| 933 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 934 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 935 | /* Output any buffered data for stdout. */ |
| 936 | while (buffer_len(&stdout_buffer) > 0) { |
| 937 | len = write(fileno(stdout), buffer_ptr(&stdout_buffer), |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 938 | buffer_len(&stdout_buffer)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 939 | if (len <= 0) { |
| 940 | error("Write failed flushing stdout buffer."); |
| 941 | break; |
| 942 | } |
| 943 | buffer_consume(&stdout_buffer, len); |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 944 | stdout_bytes += len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 945 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 946 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 947 | /* Output any buffered data for stderr. */ |
| 948 | while (buffer_len(&stderr_buffer) > 0) { |
| 949 | len = write(fileno(stderr), buffer_ptr(&stderr_buffer), |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 950 | buffer_len(&stderr_buffer)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 951 | if (len <= 0) { |
| 952 | error("Write failed flushing stderr buffer."); |
| 953 | break; |
| 954 | } |
| 955 | buffer_consume(&stderr_buffer, len); |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 956 | stderr_bytes += len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 957 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 958 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 959 | if (have_pty) |
| 960 | leave_raw_mode(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 961 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 962 | /* Clear and free any buffers. */ |
| 963 | memset(buf, 0, sizeof(buf)); |
| 964 | buffer_free(&stdin_buffer); |
| 965 | buffer_free(&stdout_buffer); |
| 966 | buffer_free(&stderr_buffer); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 967 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 968 | /* Report bytes transferred, and transfer rates. */ |
| 969 | total_time = get_current_time() - start_time; |
| 970 | debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds", |
| 971 | stdin_bytes, stdout_bytes, stderr_bytes, total_time); |
| 972 | if (total_time > 0) |
| 973 | debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f", |
| 974 | stdin_bytes / total_time, stdout_bytes / total_time, |
| 975 | stderr_bytes / total_time); |
| 976 | |
| 977 | /* Return the exit status of the program. */ |
| 978 | debug("Exit status %d", exit_status); |
| 979 | return exit_status; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 980 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 981 | |
| 982 | /*********/ |
| 983 | |
| 984 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 985 | client_input_stdout_data(int type, int plen, void *ctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 986 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 987 | u_int data_len; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 988 | char *data = packet_get_string(&data_len); |
| 989 | packet_integrity_check(plen, 4 + data_len, type); |
| 990 | buffer_append(&stdout_buffer, data, data_len); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 991 | memset(data, 0, data_len); |
| 992 | xfree(data); |
| 993 | } |
| 994 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 995 | client_input_stderr_data(int type, int plen, void *ctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 996 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 997 | u_int data_len; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 998 | char *data = packet_get_string(&data_len); |
| 999 | packet_integrity_check(plen, 4 + data_len, type); |
| 1000 | buffer_append(&stderr_buffer, data, data_len); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1001 | memset(data, 0, data_len); |
| 1002 | xfree(data); |
| 1003 | } |
| 1004 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1005 | client_input_exit_status(int type, int plen, void *ctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1006 | { |
| 1007 | packet_integrity_check(plen, 4, type); |
| 1008 | exit_status = packet_get_int(); |
| 1009 | /* Acknowledge the exit. */ |
| 1010 | packet_start(SSH_CMSG_EXIT_CONFIRMATION); |
| 1011 | packet_send(); |
| 1012 | /* |
| 1013 | * Must wait for packet to be sent since we are |
| 1014 | * exiting the loop. |
| 1015 | */ |
| 1016 | packet_write_wait(); |
| 1017 | /* Flag that we want to exit. */ |
| 1018 | quit_pending = 1; |
| 1019 | } |
| 1020 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1021 | Channel * |
| 1022 | client_request_forwarded_tcpip(const char *request_type, int rchan) |
| 1023 | { |
| 1024 | Channel* c = NULL; |
| 1025 | char *listen_address, *originator_address; |
| 1026 | int listen_port, originator_port; |
| 1027 | int sock, newch; |
| 1028 | |
| 1029 | /* Get rest of the packet */ |
| 1030 | listen_address = packet_get_string(NULL); |
| 1031 | listen_port = packet_get_int(); |
| 1032 | originator_address = packet_get_string(NULL); |
| 1033 | originator_port = packet_get_int(); |
| 1034 | packet_done(); |
| 1035 | |
| 1036 | debug("client_request_forwarded_tcpip: listen %s port %d, originator %s port %d", |
| 1037 | listen_address, listen_port, originator_address, originator_port); |
| 1038 | |
| 1039 | sock = channel_connect_by_listen_adress(listen_port); |
| 1040 | if (sock >= 0) { |
| 1041 | newch = channel_new("forwarded-tcpip", |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1042 | SSH_CHANNEL_CONNECTING, sock, sock, -1, |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1043 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0, |
| 1044 | xstrdup(originator_address), 1); |
| 1045 | c = channel_lookup(newch); |
| 1046 | } |
| 1047 | xfree(originator_address); |
| 1048 | xfree(listen_address); |
| 1049 | return c; |
| 1050 | } |
| 1051 | |
| 1052 | Channel* |
| 1053 | client_request_x11(const char *request_type, int rchan) |
| 1054 | { |
| 1055 | Channel *c = NULL; |
| 1056 | char *originator; |
| 1057 | int originator_port; |
| 1058 | int sock, newch; |
| 1059 | |
| 1060 | if (!options.forward_x11) { |
| 1061 | error("Warning: ssh server tried X11 forwarding."); |
| 1062 | error("Warning: this is probably a break in attempt by a malicious server."); |
| 1063 | return NULL; |
| 1064 | } |
| 1065 | originator = packet_get_string(NULL); |
| 1066 | if (datafellows & SSH_BUG_X11FWD) { |
| 1067 | debug2("buggy server: x11 request w/o originator_port"); |
| 1068 | originator_port = 0; |
| 1069 | } else { |
| 1070 | originator_port = packet_get_int(); |
| 1071 | } |
| 1072 | packet_done(); |
| 1073 | /* XXX check permission */ |
Damien Miller | d83ff35 | 2001-01-30 09:19:34 +1100 | [diff] [blame] | 1074 | debug("client_request_x11: request from %s %d", originator, |
| 1075 | originator_port); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1076 | sock = x11_connect_display(); |
| 1077 | if (sock >= 0) { |
| 1078 | newch = channel_new("x11", |
| 1079 | SSH_CHANNEL_X11_OPEN, sock, sock, -1, |
| 1080 | CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, |
| 1081 | xstrdup("x11"), 1); |
| 1082 | c = channel_lookup(newch); |
| 1083 | } |
| 1084 | xfree(originator); |
| 1085 | return c; |
| 1086 | } |
| 1087 | |
| 1088 | Channel* |
| 1089 | client_request_agent(const char *request_type, int rchan) |
| 1090 | { |
| 1091 | Channel *c = NULL; |
| 1092 | int sock, newch; |
| 1093 | |
| 1094 | if (!options.forward_agent) { |
| 1095 | error("Warning: ssh server tried agent forwarding."); |
| 1096 | error("Warning: this is probably a break in attempt by a malicious server."); |
| 1097 | return NULL; |
| 1098 | } |
| 1099 | sock = ssh_get_authentication_socket(); |
| 1100 | if (sock >= 0) { |
| 1101 | newch = channel_new("authentication agent connection", |
| 1102 | SSH_CHANNEL_OPEN, sock, sock, -1, |
| 1103 | CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0, |
| 1104 | xstrdup("authentication agent connection"), 1); |
| 1105 | c = channel_lookup(newch); |
| 1106 | } |
| 1107 | return c; |
| 1108 | } |
| 1109 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1110 | /* XXXX move to generic input handler */ |
| 1111 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1112 | client_input_channel_open(int type, int plen, void *ctxt) |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1113 | { |
| 1114 | Channel *c = NULL; |
| 1115 | char *ctype; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1116 | u_int len; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1117 | int rchan; |
| 1118 | int rmaxpack; |
| 1119 | int rwindow; |
| 1120 | |
| 1121 | ctype = packet_get_string(&len); |
| 1122 | rchan = packet_get_int(); |
| 1123 | rwindow = packet_get_int(); |
| 1124 | rmaxpack = packet_get_int(); |
| 1125 | |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1126 | debug("client_input_channel_open: ctype %s rchan %d win %d max %d", |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1127 | ctype, rchan, rwindow, rmaxpack); |
| 1128 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1129 | if (strcmp(ctype, "forwarded-tcpip") == 0) { |
| 1130 | c = client_request_forwarded_tcpip(ctype, rchan); |
| 1131 | } else if (strcmp(ctype, "x11") == 0) { |
| 1132 | c = client_request_x11(ctype, rchan); |
| 1133 | } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) { |
| 1134 | c = client_request_agent(ctype, rchan); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1135 | } |
| 1136 | /* XXX duplicate : */ |
| 1137 | if (c != NULL) { |
| 1138 | debug("confirm %s", ctype); |
| 1139 | c->remote_id = rchan; |
| 1140 | c->remote_window = rwindow; |
| 1141 | c->remote_maxpacket = rmaxpack; |
| 1142 | |
| 1143 | packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); |
| 1144 | packet_put_int(c->remote_id); |
| 1145 | packet_put_int(c->self); |
| 1146 | packet_put_int(c->local_window); |
| 1147 | packet_put_int(c->local_maxpacket); |
| 1148 | packet_send(); |
| 1149 | } else { |
| 1150 | debug("failure %s", ctype); |
| 1151 | packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE); |
| 1152 | packet_put_int(rchan); |
| 1153 | packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED); |
| 1154 | packet_put_cstring("bla bla"); |
| 1155 | packet_put_cstring(""); |
| 1156 | packet_send(); |
| 1157 | } |
| 1158 | xfree(ctype); |
| 1159 | } |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 1160 | void |
| 1161 | client_input_channel_req(int type, int plen, void *ctxt) |
| 1162 | { |
| 1163 | Channel *c = NULL; |
| 1164 | int id, reply, success = 0; |
| 1165 | char *rtype; |
| 1166 | |
| 1167 | id = packet_get_int(); |
| 1168 | rtype = packet_get_string(NULL); |
| 1169 | reply = packet_get_char(); |
| 1170 | |
| 1171 | debug("client_input_channel_req: channel %d rtype %s reply %d", |
| 1172 | id, rtype, reply); |
| 1173 | |
| 1174 | if (session_ident == -1) { |
| 1175 | error("client_input_channel_req: no channel %d", session_ident); |
| 1176 | } else if (id != session_ident) { |
| 1177 | error("client_input_channel_req: channel %d: wrong channel: %d", |
| 1178 | session_ident, id); |
| 1179 | } |
| 1180 | c = channel_lookup(id); |
| 1181 | if (c == NULL) { |
| 1182 | error("client_input_channel_req: channel %d: unknown channel", id); |
| 1183 | } else if (strcmp(rtype, "exit-status") == 0) { |
| 1184 | success = 1; |
| 1185 | exit_status = packet_get_int(); |
| 1186 | packet_done(); |
| 1187 | } |
| 1188 | if (reply) { |
| 1189 | packet_start(success ? |
| 1190 | SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE); |
| 1191 | packet_put_int(c->remote_id); |
| 1192 | packet_send(); |
| 1193 | } |
| 1194 | xfree(rtype); |
| 1195 | } |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1196 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1197 | void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 1198 | client_init_dispatch_20(void) |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1199 | { |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 1200 | dispatch_init(&dispatch_protocol_error); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1201 | dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose); |
| 1202 | dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data); |
| 1203 | dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof); |
| 1204 | dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1205 | dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1206 | dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); |
| 1207 | dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 1208 | dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1209 | dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust); |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 1210 | |
| 1211 | /* rekeying */ |
| 1212 | dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1213 | } |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1214 | void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 1215 | client_init_dispatch_13(void) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1216 | { |
| 1217 | dispatch_init(NULL); |
| 1218 | dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close); |
| 1219 | dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation); |
| 1220 | dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1221 | dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); |
| 1222 | dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); |
| 1223 | dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1224 | dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status); |
| 1225 | dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data); |
| 1226 | dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data); |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 1227 | |
| 1228 | dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ? |
| 1229 | &auth_input_open_request : &deny_input_open); |
| 1230 | dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ? |
| 1231 | &x11_input_open : &deny_input_open); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1232 | } |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1233 | void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 1234 | client_init_dispatch_15(void) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1235 | { |
| 1236 | client_init_dispatch_13(); |
| 1237 | dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof); |
| 1238 | dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose); |
| 1239 | } |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1240 | void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 1241 | client_init_dispatch(void) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1242 | { |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1243 | if (compat20) |
| 1244 | client_init_dispatch_20(); |
| 1245 | else if (compat13) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1246 | client_init_dispatch_13(); |
| 1247 | else |
| 1248 | client_init_dispatch_15(); |
| 1249 | } |