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