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