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