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