Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame^] | 1 | /* $OpenBSD: clientloop.c,v 1.167 2006/07/11 18:50:47 markus Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 6 | * The main loop for the interactive session (client side). |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 7 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 8 | * As far as I am concerned, the code I have written for this software |
| 9 | * can be used freely for any purpose. Any derived versions of this |
| 10 | * software must be clearly marked as such, and if the derived work is |
| 11 | * incompatible with the protocol description in the RFC file, it must be |
| 12 | * called by a name other than "ssh" or "Secure Shell". |
| 13 | * |
| 14 | * |
| 15 | * Copyright (c) 1999 Theo de Raadt. All rights reserved. |
| 16 | * |
| 17 | * Redistribution and use in source and binary forms, with or without |
| 18 | * modification, are permitted provided that the following conditions |
| 19 | * are met: |
| 20 | * 1. Redistributions of source code must retain the above copyright |
| 21 | * notice, this list of conditions and the following disclaimer. |
| 22 | * 2. Redistributions in binary form must reproduce the above copyright |
| 23 | * notice, this list of conditions and the following disclaimer in the |
| 24 | * documentation and/or other materials provided with the distribution. |
| 25 | * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 27 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 28 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 29 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 30 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 31 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 32 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 33 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 34 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 35 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 | * |
| 37 | * |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 38 | * SSH2 support added by Markus Friedl. |
Ben Lindstrom | 4469723 | 2001-07-04 03:32:30 +0000 | [diff] [blame] | 39 | * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 40 | * |
| 41 | * Redistribution and use in source and binary forms, with or without |
| 42 | * modification, are permitted provided that the following conditions |
| 43 | * are met: |
| 44 | * 1. Redistributions of source code must retain the above copyright |
| 45 | * notice, this list of conditions and the following disclaimer. |
| 46 | * 2. Redistributions in binary form must reproduce the above copyright |
| 47 | * notice, this list of conditions and the following disclaimer in the |
| 48 | * documentation and/or other materials provided with the distribution. |
| 49 | * |
| 50 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 51 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 52 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 53 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 54 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 55 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 56 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 57 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 58 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 59 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 60 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 61 | |
| 62 | #include "includes.h" |
Damien Miller | 17e91c0 | 2006-03-15 11:28:34 +1100 | [diff] [blame] | 63 | |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 64 | #include <sys/types.h> |
| 65 | #ifdef HAVE_SYS_STAT_H |
| 66 | # include <sys/stat.h> |
| 67 | #endif |
Damien Miller | e3b60b5 | 2006-07-10 21:08:03 +1000 | [diff] [blame] | 68 | #include <sys/socket.h> |
Damien Miller | 17e91c0 | 2006-03-15 11:28:34 +1100 | [diff] [blame] | 69 | #include <sys/ioctl.h> |
Damien Miller | 99bd21e | 2006-03-15 11:11:28 +1100 | [diff] [blame] | 70 | |
Damien Miller | c7b0636 | 2006-03-15 11:53:45 +1100 | [diff] [blame] | 71 | #include <ctype.h> |
Damien Miller | 6645e7a | 2006-03-15 14:42:54 +1100 | [diff] [blame] | 72 | #ifdef HAVE_PATHS_H |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 73 | #include <paths.h> |
Damien Miller | 6645e7a | 2006-03-15 14:42:54 +1100 | [diff] [blame] | 74 | #endif |
Damien Miller | 6ff3cad | 2006-03-15 11:52:09 +1100 | [diff] [blame] | 75 | #include <signal.h> |
Damien Miller | 99bd21e | 2006-03-15 11:11:28 +1100 | [diff] [blame] | 76 | #include <termios.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 77 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 78 | #include "ssh.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 79 | #include "ssh1.h" |
| 80 | #include "ssh2.h" |
| 81 | #include "xmalloc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 82 | #include "packet.h" |
| 83 | #include "buffer.h" |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 84 | #include "compat.h" |
Ben Lindstrom | c763767 | 2001-06-09 00:36:26 +0000 | [diff] [blame] | 85 | #include "channels.h" |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 86 | #include "dispatch.h" |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 87 | #include "buffer.h" |
| 88 | #include "bufaux.h" |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 89 | #include "key.h" |
Ben Lindstrom | f28f634 | 2001-04-04 02:03:04 +0000 | [diff] [blame] | 90 | #include "kex.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 91 | #include "log.h" |
| 92 | #include "readconf.h" |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 93 | #include "clientloop.h" |
Damien Miller | aeb31d6 | 2005-12-13 19:29:36 +1100 | [diff] [blame] | 94 | #include "sshconnect.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 95 | #include "authfd.h" |
| 96 | #include "atomicio.h" |
Darren Tucker | 06f2bd8 | 2004-05-13 16:06:46 +1000 | [diff] [blame] | 97 | #include "sshpty.h" |
Ben Lindstrom | 302ea6f | 2001-04-16 02:01:25 +0000 | [diff] [blame] | 98 | #include "misc.h" |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 99 | #include "monitor_fdpass.h" |
| 100 | #include "match.h" |
| 101 | #include "msg.h" |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 102 | |
| 103 | /* import options */ |
| 104 | extern Options options; |
| 105 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 106 | /* Flag indicating that stdin should be redirected from /dev/null. */ |
| 107 | extern int stdin_null_flag; |
| 108 | |
Damien Miller | d696551 | 2003-12-17 16:31:53 +1100 | [diff] [blame] | 109 | /* Flag indicating that no shell has been requested */ |
| 110 | extern int no_shell_flag; |
| 111 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 112 | /* Control socket */ |
| 113 | extern int control_fd; |
| 114 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 115 | /* |
| 116 | * Name of the host we are connecting to. This is the name given on the |
| 117 | * command line, or the HostName specified for the user-supplied name in a |
| 118 | * configuration file. |
| 119 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 120 | extern char *host; |
| 121 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 122 | /* |
| 123 | * Flag to indicate that we have received a window change signal which has |
| 124 | * not yet been processed. This will cause a message indicating the new |
| 125 | * window size to be sent to the server a little later. This is volatile |
| 126 | * because this is updated in a signal handler. |
| 127 | */ |
Ben Lindstrom | 5e71c54 | 2001-12-06 16:48:14 +0000 | [diff] [blame] | 128 | static volatile sig_atomic_t received_window_change_signal = 0; |
| 129 | static volatile sig_atomic_t received_signal = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 130 | |
Damien Miller | 788f212 | 2005-11-05 15:14:59 +1100 | [diff] [blame] | 131 | /* Flag indicating whether the user's terminal is in non-blocking mode. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 132 | static int in_non_blocking_mode = 0; |
| 133 | |
| 134 | /* Common data for the client loop code. */ |
Damien Miller | 97c91f6 | 2006-04-23 12:08:37 +1000 | [diff] [blame] | 135 | static volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 136 | static int escape_char; /* Escape character. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 137 | static int escape_pending; /* Last character was the escape character */ |
| 138 | static int last_was_cr; /* Last character was a newline. */ |
| 139 | static int exit_status; /* Used to store the exit status of the command. */ |
| 140 | static int stdin_eof; /* EOF has been encountered on standard error. */ |
| 141 | static Buffer stdin_buffer; /* Buffer for stdin data. */ |
| 142 | static Buffer stdout_buffer; /* Buffer for stdout data. */ |
| 143 | static Buffer stderr_buffer; /* Buffer for stderr data. */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 144 | static u_long stdin_bytes, stdout_bytes, stderr_bytes; |
| 145 | static u_int buffer_high;/* Soft max buffer size. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 146 | static int connection_in; /* Connection to server (input). */ |
| 147 | static int connection_out; /* Connection to server (output). */ |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 148 | static int need_rekeying; /* Set to non-zero if rekeying is requested. */ |
Ben Lindstrom | 4c3f77d | 2001-04-05 23:37:36 +0000 | [diff] [blame] | 149 | static int session_closed = 0; /* In SSH2: login session closed. */ |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 150 | static int server_alive_timeouts = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 151 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 152 | static void client_init_dispatch(void); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 153 | int session_ident = -1; |
| 154 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 155 | struct confirm_ctx { |
| 156 | int want_tty; |
| 157 | int want_subsys; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 158 | int want_x_fwd; |
| 159 | int want_agent_fwd; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 160 | Buffer cmd; |
| 161 | char *term; |
| 162 | struct termios tio; |
Damien Miller | 3756dce | 2004-06-18 01:17:29 +1000 | [diff] [blame] | 163 | char **env; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 164 | }; |
| 165 | |
Ben Lindstrom | f28f634 | 2001-04-04 02:03:04 +0000 | [diff] [blame] | 166 | /*XXX*/ |
| 167 | extern Kex *xxx_kex; |
| 168 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 169 | void ssh_process_session2_setup(int, int, int, Buffer *); |
| 170 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 171 | /* Restores stdin to blocking mode. */ |
| 172 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 173 | static void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 174 | leave_non_blocking(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 175 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 176 | if (in_non_blocking_mode) { |
Damien Miller | 03e66f6 | 2004-06-15 15:47:51 +1000 | [diff] [blame] | 177 | unset_nonblock(fileno(stdin)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 178 | in_non_blocking_mode = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 179 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 180 | } |
| 181 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 182 | /* Puts stdin terminal in non-blocking mode. */ |
| 183 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 184 | static void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 185 | enter_non_blocking(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 186 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 187 | in_non_blocking_mode = 1; |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 188 | set_nonblock(fileno(stdin)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 189 | } |
| 190 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 191 | /* |
| 192 | * Signal handler for the window change signal (SIGWINCH). This just sets a |
| 193 | * flag indicating that the window has changed. |
| 194 | */ |
Damien Miller | f0b15df | 2006-03-26 13:59:20 +1100 | [diff] [blame] | 195 | /*ARGSUSED */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 196 | static void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 197 | window_change_handler(int sig) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 198 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 199 | received_window_change_signal = 1; |
| 200 | signal(SIGWINCH, window_change_handler); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 201 | } |
| 202 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 203 | /* |
| 204 | * Signal handler for signals that cause the program to terminate. These |
| 205 | * signals must be trapped to restore terminal modes. |
| 206 | */ |
Damien Miller | f0b15df | 2006-03-26 13:59:20 +1100 | [diff] [blame] | 207 | /*ARGSUSED */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 208 | static void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 209 | signal_handler(int sig) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 210 | { |
Ben Lindstrom | ec46e0b | 2001-06-09 01:27:31 +0000 | [diff] [blame] | 211 | received_signal = sig; |
| 212 | quit_pending = 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 213 | } |
| 214 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 215 | /* |
| 216 | * Returns current time in seconds from Jan 1, 1970 with the maximum |
| 217 | * available resolution. |
| 218 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 219 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 220 | static double |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 221 | get_current_time(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 222 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 223 | struct timeval tv; |
| 224 | gettimeofday(&tv, NULL); |
| 225 | return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 226 | } |
| 227 | |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 228 | #define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1" |
| 229 | void |
| 230 | client_x11_get_proto(const char *display, const char *xauth_path, |
| 231 | u_int trusted, char **_proto, char **_data) |
| 232 | { |
| 233 | char cmd[1024]; |
| 234 | char line[512]; |
| 235 | char xdisplay[512]; |
| 236 | static char proto[512], data[512]; |
| 237 | FILE *f; |
| 238 | int got_data = 0, generated = 0, do_unlink = 0, i; |
| 239 | char *xauthdir, *xauthfile; |
| 240 | struct stat st; |
| 241 | |
| 242 | xauthdir = xauthfile = NULL; |
| 243 | *_proto = proto; |
| 244 | *_data = data; |
| 245 | proto[0] = data[0] = '\0'; |
| 246 | |
| 247 | if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) { |
| 248 | debug("No xauth program."); |
| 249 | } else { |
| 250 | if (display == NULL) { |
| 251 | debug("x11_get_proto: DISPLAY not set"); |
| 252 | return; |
| 253 | } |
| 254 | /* |
| 255 | * Handle FamilyLocal case where $DISPLAY does |
| 256 | * not match an authorization entry. For this we |
| 257 | * just try "xauth list unix:displaynum.screennum". |
| 258 | * XXX: "localhost" match to determine FamilyLocal |
| 259 | * is not perfect. |
| 260 | */ |
| 261 | if (strncmp(display, "localhost:", 10) == 0) { |
| 262 | snprintf(xdisplay, sizeof(xdisplay), "unix:%s", |
| 263 | display + 10); |
| 264 | display = xdisplay; |
| 265 | } |
| 266 | if (trusted == 0) { |
| 267 | xauthdir = xmalloc(MAXPATHLEN); |
| 268 | xauthfile = xmalloc(MAXPATHLEN); |
| 269 | strlcpy(xauthdir, "/tmp/ssh-XXXXXXXXXX", MAXPATHLEN); |
| 270 | if (mkdtemp(xauthdir) != NULL) { |
| 271 | do_unlink = 1; |
| 272 | snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile", |
| 273 | xauthdir); |
| 274 | snprintf(cmd, sizeof(cmd), |
| 275 | "%s -f %s generate %s " SSH_X11_PROTO |
| 276 | " untrusted timeout 1200 2>" _PATH_DEVNULL, |
| 277 | xauth_path, xauthfile, display); |
| 278 | debug2("x11_get_proto: %s", cmd); |
| 279 | if (system(cmd) == 0) |
| 280 | generated = 1; |
| 281 | } |
| 282 | } |
| 283 | snprintf(cmd, sizeof(cmd), |
Darren Tucker | d89dbf2 | 2005-10-03 18:05:26 +1000 | [diff] [blame] | 284 | "%s %s%s list %s 2>" _PATH_DEVNULL, |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 285 | xauth_path, |
| 286 | generated ? "-f " : "" , |
| 287 | generated ? xauthfile : "", |
| 288 | display); |
| 289 | debug2("x11_get_proto: %s", cmd); |
| 290 | f = popen(cmd, "r"); |
| 291 | if (f && fgets(line, sizeof(line), f) && |
| 292 | sscanf(line, "%*s %511s %511s", proto, data) == 2) |
| 293 | got_data = 1; |
| 294 | if (f) |
| 295 | pclose(f); |
| 296 | } |
| 297 | |
| 298 | if (do_unlink) { |
| 299 | unlink(xauthfile); |
| 300 | rmdir(xauthdir); |
| 301 | } |
| 302 | if (xauthdir) |
| 303 | xfree(xauthdir); |
| 304 | if (xauthfile) |
| 305 | xfree(xauthfile); |
| 306 | |
| 307 | /* |
| 308 | * If we didn't get authentication data, just make up some |
| 309 | * data. The forwarding code will check the validity of the |
| 310 | * response anyway, and substitute this data. The X11 |
| 311 | * server, however, will ignore this fake data and use |
| 312 | * whatever authentication mechanisms it was using otherwise |
| 313 | * for the local connection. |
| 314 | */ |
| 315 | if (!got_data) { |
| 316 | u_int32_t rnd = 0; |
| 317 | |
| 318 | logit("Warning: No xauth data; " |
| 319 | "using fake authentication data for X11 forwarding."); |
| 320 | strlcpy(proto, SSH_X11_PROTO, sizeof proto); |
| 321 | for (i = 0; i < 16; i++) { |
| 322 | if (i % 4 == 0) |
| 323 | rnd = arc4random(); |
| 324 | snprintf(data + 2 * i, sizeof data - 2 * i, "%02x", |
| 325 | rnd & 0xff); |
| 326 | rnd >>= 8; |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 331 | /* |
| 332 | * This is called when the interactive is entered. This checks if there is |
| 333 | * an EOF coming on stdin. We must check this explicitly, as select() does |
| 334 | * not appear to wake up when redirecting from /dev/null. |
| 335 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 336 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 337 | static void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 338 | client_check_initial_eof_on_stdin(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 339 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 340 | int len; |
| 341 | char buf[1]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 342 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 343 | /* |
| 344 | * If standard input is to be "redirected from /dev/null", we simply |
| 345 | * mark that we have seen an EOF and send an EOF message to the |
| 346 | * server. Otherwise, we try to read a single character; it appears |
| 347 | * that for some files, such /dev/null, select() never wakes up for |
| 348 | * read for this descriptor, which means that we never get EOF. This |
| 349 | * way we will get the EOF if stdin comes from /dev/null or similar. |
| 350 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 351 | if (stdin_null_flag) { |
| 352 | /* Fake EOF on stdin. */ |
| 353 | debug("Sending eof."); |
| 354 | stdin_eof = 1; |
| 355 | packet_start(SSH_CMSG_EOF); |
| 356 | packet_send(); |
| 357 | } else { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 358 | enter_non_blocking(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 359 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 360 | /* Check for immediate EOF on stdin. */ |
| 361 | len = read(fileno(stdin), buf, 1); |
| 362 | if (len == 0) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 363 | /* EOF. Record that we have seen it and send EOF to server. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 364 | debug("Sending eof."); |
| 365 | stdin_eof = 1; |
| 366 | packet_start(SSH_CMSG_EOF); |
| 367 | packet_send(); |
| 368 | } else if (len > 0) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 369 | /* |
| 370 | * Got data. We must store the data in the buffer, |
| 371 | * and also process it as an escape character if |
| 372 | * appropriate. |
| 373 | */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 374 | if ((u_char) buf[0] == escape_char) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 375 | escape_pending = 1; |
Ben Lindstrom | e9613cf | 2001-03-05 06:14:02 +0000 | [diff] [blame] | 376 | else |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 377 | buffer_append(&stdin_buffer, buf, 1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 378 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 379 | leave_non_blocking(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 380 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 381 | } |
| 382 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 383 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 384 | /* |
| 385 | * Make packets from buffered stdin data, and buffer them for sending to the |
| 386 | * connection. |
| 387 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 388 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 389 | static void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 390 | client_make_packets_from_stdin_data(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 391 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 392 | u_int len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 393 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 394 | /* Send buffered stdin data to the server. */ |
| 395 | while (buffer_len(&stdin_buffer) > 0 && |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 396 | packet_not_very_much_data_to_write()) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 397 | len = buffer_len(&stdin_buffer); |
| 398 | /* Keep the packets at reasonable size. */ |
| 399 | if (len > packet_get_maxsize()) |
| 400 | len = packet_get_maxsize(); |
| 401 | packet_start(SSH_CMSG_STDIN_DATA); |
| 402 | packet_put_string(buffer_ptr(&stdin_buffer), len); |
| 403 | packet_send(); |
| 404 | buffer_consume(&stdin_buffer, len); |
Ben Lindstrom | e9613cf | 2001-03-05 06:14:02 +0000 | [diff] [blame] | 405 | stdin_bytes += len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 406 | /* If we have a pending EOF, send it now. */ |
| 407 | if (stdin_eof && buffer_len(&stdin_buffer) == 0) { |
| 408 | packet_start(SSH_CMSG_EOF); |
| 409 | packet_send(); |
| 410 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 411 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 412 | } |
| 413 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 414 | /* |
| 415 | * Checks if the client window has changed, and sends a packet about it to |
| 416 | * the server if so. The actual change is detected elsewhere (by a software |
| 417 | * interrupt on Unix); this just checks the flag and sends a message if |
| 418 | * appropriate. |
| 419 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 420 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 421 | static void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 422 | client_check_window_change(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 423 | { |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 424 | struct winsize ws; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 425 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 426 | if (! received_window_change_signal) |
| 427 | return; |
| 428 | /** XXX race */ |
| 429 | received_window_change_signal = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 430 | |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 431 | debug2("client_check_window_change: changed"); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 432 | |
| 433 | if (compat20) { |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 434 | channel_send_window_changes(); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 435 | } else { |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 436 | if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0) |
| 437 | return; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 438 | packet_start(SSH_CMSG_WINDOW_SIZE); |
Damien Miller | 71a7367 | 2006-03-26 14:04:36 +1100 | [diff] [blame] | 439 | packet_put_int((u_int)ws.ws_row); |
| 440 | packet_put_int((u_int)ws.ws_col); |
| 441 | packet_put_int((u_int)ws.ws_xpixel); |
| 442 | packet_put_int((u_int)ws.ws_ypixel); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 443 | packet_send(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 444 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 445 | } |
| 446 | |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 447 | static void |
| 448 | client_global_request_reply(int type, u_int32_t seq, void *ctxt) |
| 449 | { |
| 450 | server_alive_timeouts = 0; |
| 451 | client_global_request_reply_fwd(type, seq, ctxt); |
| 452 | } |
| 453 | |
| 454 | static void |
| 455 | server_alive_check(void) |
| 456 | { |
| 457 | if (++server_alive_timeouts > options.server_alive_count_max) |
| 458 | packet_disconnect("Timeout, server not responding."); |
| 459 | packet_start(SSH2_MSG_GLOBAL_REQUEST); |
| 460 | packet_put_cstring("keepalive@openssh.com"); |
| 461 | packet_put_char(1); /* boolean: want reply */ |
| 462 | packet_send(); |
| 463 | } |
| 464 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 465 | /* |
| 466 | * Waits until the client can do something (some data becomes available on |
| 467 | * one of the file descriptors). |
| 468 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 469 | static void |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 470 | client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp, |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 471 | int *maxfdp, u_int *nallocp, int rekeying) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 472 | { |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 473 | struct timeval tv, *tvp; |
| 474 | int ret; |
| 475 | |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 476 | /* Add any selections by the channel mechanism. */ |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 477 | channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, rekeying); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 478 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 479 | if (!compat20) { |
| 480 | /* Read from the connection, unless our buffers are full. */ |
| 481 | if (buffer_len(&stdout_buffer) < buffer_high && |
| 482 | buffer_len(&stderr_buffer) < buffer_high && |
| 483 | channel_not_very_much_buffered_data()) |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 484 | FD_SET(connection_in, *readsetp); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 485 | /* |
| 486 | * Read from stdin, unless we have seen EOF or have very much |
| 487 | * buffered data to send to the server. |
| 488 | */ |
| 489 | if (!stdin_eof && packet_not_very_much_data_to_write()) |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 490 | FD_SET(fileno(stdin), *readsetp); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 491 | |
| 492 | /* Select stdout/stderr if have data in buffer. */ |
| 493 | if (buffer_len(&stdout_buffer) > 0) |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 494 | FD_SET(fileno(stdout), *writesetp); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 495 | if (buffer_len(&stderr_buffer) > 0) |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 496 | FD_SET(fileno(stderr), *writesetp); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 497 | } else { |
Ben Lindstrom | c8b3f47 | 2001-05-17 03:19:40 +0000 | [diff] [blame] | 498 | /* channel_prepare_select could have closed the last channel */ |
Damien Miller | 164a7f4 | 2001-10-12 11:36:09 +1000 | [diff] [blame] | 499 | if (session_closed && !channel_still_open() && |
| 500 | !packet_have_data_to_write()) { |
| 501 | /* clear mask since we did not call select() */ |
Damien Miller | 79faeff | 2001-11-12 11:06:32 +1100 | [diff] [blame] | 502 | memset(*readsetp, 0, *nallocp); |
| 503 | memset(*writesetp, 0, *nallocp); |
Damien Miller | 164a7f4 | 2001-10-12 11:36:09 +1000 | [diff] [blame] | 504 | return; |
Ben Lindstrom | c8b3f47 | 2001-05-17 03:19:40 +0000 | [diff] [blame] | 505 | } else { |
| 506 | FD_SET(connection_in, *readsetp); |
| 507 | } |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 508 | } |
| 509 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 510 | /* Select server connection if have data to write to the server. */ |
| 511 | if (packet_have_data_to_write()) |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 512 | FD_SET(connection_out, *writesetp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 513 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 514 | if (control_fd != -1) |
| 515 | FD_SET(control_fd, *readsetp); |
| 516 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 517 | /* |
| 518 | * Wait for something to happen. This will suspend the process until |
| 519 | * some selected descriptor can be read, written, or has some other |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 520 | * event pending. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 521 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 522 | |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 523 | if (options.server_alive_interval == 0 || !compat20) |
| 524 | tvp = NULL; |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 525 | else { |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 526 | tv.tv_sec = options.server_alive_interval; |
| 527 | tv.tv_usec = 0; |
| 528 | tvp = &tv; |
| 529 | } |
| 530 | ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp); |
| 531 | if (ret < 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 532 | char buf[100]; |
Ben Lindstrom | f945251 | 2001-02-15 03:12:08 +0000 | [diff] [blame] | 533 | |
| 534 | /* |
| 535 | * We have to clear the select masks, because we return. |
| 536 | * We have to return, because the mainloop checks for the flags |
| 537 | * set by the signal handlers. |
| 538 | */ |
Damien Miller | 79faeff | 2001-11-12 11:06:32 +1100 | [diff] [blame] | 539 | memset(*readsetp, 0, *nallocp); |
| 540 | memset(*writesetp, 0, *nallocp); |
Ben Lindstrom | f945251 | 2001-02-15 03:12:08 +0000 | [diff] [blame] | 541 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 542 | if (errno == EINTR) |
| 543 | return; |
| 544 | /* Note: we might still have data in the buffers. */ |
| 545 | snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno)); |
| 546 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 547 | quit_pending = 1; |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 548 | } else if (ret == 0) |
| 549 | server_alive_check(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 550 | } |
| 551 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 552 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 553 | client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 554 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 555 | /* Flush stdout and stderr buffers. */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 556 | if (buffer_len(bout) > 0) |
Darren Tucker | 9f63f22 | 2003-07-03 13:46:56 +1000 | [diff] [blame] | 557 | atomicio(vwrite, fileno(stdout), buffer_ptr(bout), buffer_len(bout)); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 558 | if (buffer_len(berr) > 0) |
Darren Tucker | 9f63f22 | 2003-07-03 13:46:56 +1000 | [diff] [blame] | 559 | atomicio(vwrite, fileno(stderr), buffer_ptr(berr), buffer_len(berr)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 560 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 561 | leave_raw_mode(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 562 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 563 | /* |
| 564 | * Free (and clear) the buffer to reduce the amount of data that gets |
| 565 | * written to swap. |
| 566 | */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 567 | buffer_free(bin); |
| 568 | buffer_free(bout); |
| 569 | buffer_free(berr); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 570 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 571 | /* Send the suspend signal to the program itself. */ |
| 572 | kill(getpid(), SIGTSTP); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 573 | |
Darren Tucker | 5d78de6 | 2004-11-05 20:35:44 +1100 | [diff] [blame] | 574 | /* Reset window sizes in case they have changed */ |
| 575 | received_window_change_signal = 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 576 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 577 | /* OK, we have been continued by the user. Reinitialize buffers. */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 578 | buffer_init(bin); |
| 579 | buffer_init(bout); |
| 580 | buffer_init(berr); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 581 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 582 | enter_raw_mode(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 583 | } |
| 584 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 585 | static void |
Damien Miller | 90fdfaf | 2006-03-26 14:25:37 +1100 | [diff] [blame] | 586 | client_process_net_input(fd_set *readset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 587 | { |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 588 | int len; |
| 589 | char buf[8192]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 590 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 591 | /* |
| 592 | * Read input from the server, and add any such data to the buffer of |
| 593 | * the packet subsystem. |
| 594 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 595 | if (FD_ISSET(connection_in, readset)) { |
| 596 | /* Read as much as possible. */ |
| 597 | len = read(connection_in, buf, sizeof(buf)); |
| 598 | if (len == 0) { |
| 599 | /* Received EOF. The remote host has closed the connection. */ |
| 600 | snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n", |
| 601 | host); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 602 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 603 | quit_pending = 1; |
| 604 | return; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 605 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 606 | /* |
| 607 | * There is a kernel bug on Solaris that causes select to |
| 608 | * sometimes wake up even though there is no data available. |
| 609 | */ |
Ben Lindstrom | ebc8827 | 2001-03-06 03:34:40 +0000 | [diff] [blame] | 610 | if (len < 0 && (errno == EAGAIN || errno == EINTR)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 611 | len = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 612 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 613 | if (len < 0) { |
| 614 | /* An error has encountered. Perhaps there is a network problem. */ |
| 615 | snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n", |
| 616 | host, strerror(errno)); |
| 617 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 618 | quit_pending = 1; |
| 619 | return; |
| 620 | } |
| 621 | packet_process_incoming(buf, len); |
| 622 | } |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 623 | } |
| 624 | |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 625 | static void |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 626 | client_subsystem_reply(int type, u_int32_t seq, void *ctxt) |
| 627 | { |
| 628 | int id; |
| 629 | Channel *c; |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 630 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 631 | id = packet_get_int(); |
| 632 | packet_check_eom(); |
| 633 | |
| 634 | if ((c = channel_lookup(id)) == NULL) { |
| 635 | error("%s: no channel for id %d", __func__, id); |
| 636 | return; |
| 637 | } |
| 638 | |
| 639 | if (type == SSH2_MSG_CHANNEL_SUCCESS) |
| 640 | debug2("Request suceeded on channel %d", id); |
| 641 | else if (type == SSH2_MSG_CHANNEL_FAILURE) { |
| 642 | error("Request failed on channel %d", id); |
| 643 | channel_free(c); |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | static void |
| 648 | client_extra_session2_setup(int id, void *arg) |
| 649 | { |
| 650 | struct confirm_ctx *cctx = arg; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 651 | const char *display; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 652 | Channel *c; |
Damien Miller | 3756dce | 2004-06-18 01:17:29 +1000 | [diff] [blame] | 653 | int i; |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 654 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 655 | if (cctx == NULL) |
| 656 | fatal("%s: cctx == NULL", __func__); |
| 657 | if ((c = channel_lookup(id)) == NULL) |
| 658 | fatal("%s: no channel for id %d", __func__, id); |
| 659 | |
Damien Miller | 46d38de | 2005-07-17 17:02:09 +1000 | [diff] [blame] | 660 | display = getenv("DISPLAY"); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 661 | if (cctx->want_x_fwd && options.forward_x11 && display != NULL) { |
| 662 | char *proto, *data; |
| 663 | /* Get reasonable local authentication information. */ |
| 664 | client_x11_get_proto(display, options.xauth_location, |
| 665 | options.forward_x11_trusted, &proto, &data); |
| 666 | /* Request forwarding with authentication spoofing. */ |
| 667 | debug("Requesting X11 forwarding with authentication spoofing."); |
| 668 | x11_request_forwarding_with_spoofing(id, display, proto, data); |
| 669 | /* XXX wait for reply */ |
| 670 | } |
| 671 | |
| 672 | if (cctx->want_agent_fwd && options.forward_agent) { |
| 673 | debug("Requesting authentication agent forwarding."); |
| 674 | channel_request_start(id, "auth-agent-req@openssh.com", 0); |
| 675 | packet_send(); |
| 676 | } |
| 677 | |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 678 | client_session2_setup(id, cctx->want_tty, cctx->want_subsys, |
Damien Miller | 3756dce | 2004-06-18 01:17:29 +1000 | [diff] [blame] | 679 | cctx->term, &cctx->tio, c->rfd, &cctx->cmd, cctx->env, |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 680 | client_subsystem_reply); |
Damien Miller | 23f0770 | 2004-06-18 01:19:03 +1000 | [diff] [blame] | 681 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 682 | c->confirm_ctx = NULL; |
| 683 | buffer_free(&cctx->cmd); |
Damien Miller | 3756dce | 2004-06-18 01:17:29 +1000 | [diff] [blame] | 684 | xfree(cctx->term); |
| 685 | if (cctx->env != NULL) { |
| 686 | for (i = 0; cctx->env[i] != NULL; i++) |
| 687 | xfree(cctx->env[i]); |
| 688 | xfree(cctx->env); |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 689 | } |
Damien Miller | 3756dce | 2004-06-18 01:17:29 +1000 | [diff] [blame] | 690 | xfree(cctx); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | static void |
Damien Miller | 90fdfaf | 2006-03-26 14:25:37 +1100 | [diff] [blame] | 694 | client_process_control(fd_set *readset) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 695 | { |
| 696 | Buffer m; |
| 697 | Channel *c; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 698 | int client_fd, new_fd[3], ver, allowed; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 699 | socklen_t addrlen; |
| 700 | struct sockaddr_storage addr; |
| 701 | struct confirm_ctx *cctx; |
| 702 | char *cmd; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 703 | u_int i, len, env_len, command, flags; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 704 | uid_t euid; |
| 705 | gid_t egid; |
| 706 | |
| 707 | /* |
| 708 | * Accept connection on control socket |
| 709 | */ |
| 710 | if (control_fd == -1 || !FD_ISSET(control_fd, readset)) |
| 711 | return; |
| 712 | |
| 713 | memset(&addr, 0, sizeof(addr)); |
| 714 | addrlen = sizeof(addr); |
| 715 | if ((client_fd = accept(control_fd, |
| 716 | (struct sockaddr*)&addr, &addrlen)) == -1) { |
| 717 | error("%s accept: %s", __func__, strerror(errno)); |
| 718 | return; |
| 719 | } |
| 720 | |
| 721 | if (getpeereid(client_fd, &euid, &egid) < 0) { |
| 722 | error("%s getpeereid failed: %s", __func__, strerror(errno)); |
| 723 | close(client_fd); |
| 724 | return; |
| 725 | } |
| 726 | if ((euid != 0) && (getuid() != euid)) { |
| 727 | error("control mode uid mismatch: peer euid %u != uid %u", |
| 728 | (u_int) euid, (u_int) getuid()); |
| 729 | close(client_fd); |
| 730 | return; |
| 731 | } |
Damien Miller | 23f0770 | 2004-06-18 01:19:03 +1000 | [diff] [blame] | 732 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 733 | unset_nonblock(client_fd); |
| 734 | |
Darren Tucker | 7ebfc10 | 2004-11-07 20:06:19 +1100 | [diff] [blame] | 735 | /* Read command */ |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 736 | buffer_init(&m); |
Darren Tucker | 7ebfc10 | 2004-11-07 20:06:19 +1100 | [diff] [blame] | 737 | if (ssh_msg_recv(client_fd, &m) == -1) { |
| 738 | error("%s: client msg_recv failed", __func__); |
| 739 | close(client_fd); |
| 740 | buffer_free(&m); |
| 741 | return; |
| 742 | } |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 743 | if ((ver = buffer_get_char(&m)) != SSHMUX_VER) { |
Darren Tucker | 7ebfc10 | 2004-11-07 20:06:19 +1100 | [diff] [blame] | 744 | error("%s: wrong client version %d", __func__, ver); |
| 745 | buffer_free(&m); |
| 746 | close(client_fd); |
| 747 | return; |
| 748 | } |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 749 | |
Darren Tucker | 7ebfc10 | 2004-11-07 20:06:19 +1100 | [diff] [blame] | 750 | allowed = 1; |
| 751 | command = buffer_get_int(&m); |
| 752 | flags = buffer_get_int(&m); |
| 753 | |
| 754 | buffer_clear(&m); |
| 755 | |
| 756 | switch (command) { |
| 757 | case SSHMUX_COMMAND_OPEN: |
Damien Miller | d14b1e7 | 2005-06-16 13:19:41 +1000 | [diff] [blame] | 758 | if (options.control_master == SSHCTL_MASTER_ASK || |
| 759 | options.control_master == SSHCTL_MASTER_AUTO_ASK) |
Darren Tucker | 7ebfc10 | 2004-11-07 20:06:19 +1100 | [diff] [blame] | 760 | allowed = ask_permission("Allow shared connection " |
| 761 | "to %s? ", host); |
| 762 | /* continue below */ |
| 763 | break; |
| 764 | case SSHMUX_COMMAND_TERMINATE: |
Damien Miller | d14b1e7 | 2005-06-16 13:19:41 +1000 | [diff] [blame] | 765 | if (options.control_master == SSHCTL_MASTER_ASK || |
| 766 | options.control_master == SSHCTL_MASTER_AUTO_ASK) |
Darren Tucker | 7ebfc10 | 2004-11-07 20:06:19 +1100 | [diff] [blame] | 767 | allowed = ask_permission("Terminate shared connection " |
| 768 | "to %s? ", host); |
| 769 | if (allowed) |
| 770 | quit_pending = 1; |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 771 | /* FALLTHROUGH */ |
Darren Tucker | 7ebfc10 | 2004-11-07 20:06:19 +1100 | [diff] [blame] | 772 | case SSHMUX_COMMAND_ALIVE_CHECK: |
| 773 | /* Reply for SSHMUX_COMMAND_TERMINATE and ALIVE_CHECK */ |
| 774 | buffer_clear(&m); |
| 775 | buffer_put_int(&m, allowed); |
| 776 | buffer_put_int(&m, getpid()); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 777 | if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) { |
Darren Tucker | 7ebfc10 | 2004-11-07 20:06:19 +1100 | [diff] [blame] | 778 | error("%s: client msg_send failed", __func__); |
| 779 | close(client_fd); |
| 780 | buffer_free(&m); |
| 781 | return; |
| 782 | } |
| 783 | buffer_free(&m); |
| 784 | close(client_fd); |
| 785 | return; |
| 786 | default: |
| 787 | error("Unsupported command %d", command); |
| 788 | buffer_free(&m); |
| 789 | close(client_fd); |
| 790 | return; |
| 791 | } |
| 792 | |
| 793 | /* Reply for SSHMUX_COMMAND_OPEN */ |
| 794 | buffer_clear(&m); |
Damien Miller | 23f0770 | 2004-06-18 01:19:03 +1000 | [diff] [blame] | 795 | buffer_put_int(&m, allowed); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 796 | buffer_put_int(&m, getpid()); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 797 | if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) { |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 798 | error("%s: client msg_send failed", __func__); |
| 799 | close(client_fd); |
Damien Miller | 23f0770 | 2004-06-18 01:19:03 +1000 | [diff] [blame] | 800 | buffer_free(&m); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 801 | return; |
| 802 | } |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 803 | |
Damien Miller | 23f0770 | 2004-06-18 01:19:03 +1000 | [diff] [blame] | 804 | if (!allowed) { |
| 805 | error("Refused control connection"); |
| 806 | close(client_fd); |
| 807 | buffer_free(&m); |
| 808 | return; |
| 809 | } |
| 810 | |
Darren Tucker | 7ebfc10 | 2004-11-07 20:06:19 +1100 | [diff] [blame] | 811 | buffer_clear(&m); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 812 | if (ssh_msg_recv(client_fd, &m) == -1) { |
| 813 | error("%s: client msg_recv failed", __func__); |
| 814 | close(client_fd); |
Damien Miller | 23f0770 | 2004-06-18 01:19:03 +1000 | [diff] [blame] | 815 | buffer_free(&m); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 816 | return; |
| 817 | } |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 818 | if ((ver = buffer_get_char(&m)) != SSHMUX_VER) { |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 819 | error("%s: wrong client version %d", __func__, ver); |
| 820 | buffer_free(&m); |
| 821 | close(client_fd); |
| 822 | return; |
| 823 | } |
| 824 | |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 825 | cctx = xcalloc(1, sizeof(*cctx)); |
Darren Tucker | 7ebfc10 | 2004-11-07 20:06:19 +1100 | [diff] [blame] | 826 | cctx->want_tty = (flags & SSHMUX_FLAG_TTY) != 0; |
| 827 | cctx->want_subsys = (flags & SSHMUX_FLAG_SUBSYS) != 0; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 828 | cctx->want_x_fwd = (flags & SSHMUX_FLAG_X11_FWD) != 0; |
| 829 | cctx->want_agent_fwd = (flags & SSHMUX_FLAG_AGENT_FWD) != 0; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 830 | cctx->term = buffer_get_string(&m, &len); |
| 831 | |
| 832 | cmd = buffer_get_string(&m, &len); |
| 833 | buffer_init(&cctx->cmd); |
| 834 | buffer_append(&cctx->cmd, cmd, strlen(cmd)); |
| 835 | |
Damien Miller | 3756dce | 2004-06-18 01:17:29 +1000 | [diff] [blame] | 836 | env_len = buffer_get_int(&m); |
| 837 | env_len = MIN(env_len, 4096); |
| 838 | debug3("%s: receiving %d env vars", __func__, env_len); |
| 839 | if (env_len != 0) { |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 840 | cctx->env = xcalloc(env_len + 1, sizeof(*cctx->env)); |
Damien Miller | 3756dce | 2004-06-18 01:17:29 +1000 | [diff] [blame] | 841 | for (i = 0; i < env_len; i++) |
| 842 | cctx->env[i] = buffer_get_string(&m, &len); |
| 843 | cctx->env[i] = NULL; |
| 844 | } |
| 845 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 846 | debug2("%s: accepted tty %d, subsys %d, cmd %s", __func__, |
| 847 | cctx->want_tty, cctx->want_subsys, cmd); |
Damien Miller | 24fd8dd | 2006-06-13 13:00:09 +1000 | [diff] [blame] | 848 | xfree(cmd); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 849 | |
| 850 | /* Gather fds from client */ |
| 851 | new_fd[0] = mm_receive_fd(client_fd); |
| 852 | new_fd[1] = mm_receive_fd(client_fd); |
| 853 | new_fd[2] = mm_receive_fd(client_fd); |
| 854 | |
| 855 | debug2("%s: got fds stdin %d, stdout %d, stderr %d", __func__, |
| 856 | new_fd[0], new_fd[1], new_fd[2]); |
| 857 | |
| 858 | /* Try to pick up ttymodes from client before it goes raw */ |
| 859 | if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1) |
| 860 | error("%s: tcgetattr: %s", __func__, strerror(errno)); |
| 861 | |
Darren Tucker | 7ebfc10 | 2004-11-07 20:06:19 +1100 | [diff] [blame] | 862 | /* This roundtrip is just for synchronisation of ttymodes */ |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 863 | buffer_clear(&m); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 864 | if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) { |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 865 | error("%s: client msg_send failed", __func__); |
| 866 | close(client_fd); |
| 867 | close(new_fd[0]); |
| 868 | close(new_fd[1]); |
| 869 | close(new_fd[2]); |
Damien Miller | 23f0770 | 2004-06-18 01:19:03 +1000 | [diff] [blame] | 870 | buffer_free(&m); |
Darren Tucker | 7ebfc10 | 2004-11-07 20:06:19 +1100 | [diff] [blame] | 871 | xfree(cctx->term); |
| 872 | if (env_len != 0) { |
| 873 | for (i = 0; i < env_len; i++) |
| 874 | xfree(cctx->env[i]); |
| 875 | xfree(cctx->env); |
| 876 | } |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 877 | return; |
| 878 | } |
| 879 | buffer_free(&m); |
| 880 | |
| 881 | /* enable nonblocking unless tty */ |
| 882 | if (!isatty(new_fd[0])) |
| 883 | set_nonblock(new_fd[0]); |
| 884 | if (!isatty(new_fd[1])) |
| 885 | set_nonblock(new_fd[1]); |
| 886 | if (!isatty(new_fd[2])) |
| 887 | set_nonblock(new_fd[2]); |
| 888 | |
| 889 | set_nonblock(client_fd); |
| 890 | |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 891 | c = channel_new("session", SSH_CHANNEL_OPENING, |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 892 | new_fd[0], new_fd[1], new_fd[2], |
| 893 | CHAN_SES_WINDOW_DEFAULT, CHAN_SES_PACKET_DEFAULT, |
| 894 | CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0); |
| 895 | |
| 896 | /* XXX */ |
| 897 | c->ctl_fd = client_fd; |
| 898 | |
| 899 | debug3("%s: channel_new: %d", __func__, c->self); |
| 900 | |
| 901 | channel_send_open(c->self); |
| 902 | channel_register_confirm(c->self, client_extra_session2_setup, cctx); |
| 903 | } |
| 904 | |
| 905 | static void |
Ben Lindstrom | 681d932 | 2002-03-22 03:53:00 +0000 | [diff] [blame] | 906 | process_cmdline(void) |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 907 | { |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 908 | void (*handler)(int); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 909 | char *s, *cmd, *cancel_host; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 910 | int delete = 0; |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 911 | int local = 0; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 912 | u_short cancel_port; |
| 913 | Forward fwd; |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 914 | |
| 915 | leave_raw_mode(); |
Ben Lindstrom | 5a6abda | 2002-06-09 19:41:48 +0000 | [diff] [blame] | 916 | handler = signal(SIGINT, SIG_IGN); |
Ben Lindstrom | 681d932 | 2002-03-22 03:53:00 +0000 | [diff] [blame] | 917 | cmd = s = read_passphrase("\r\nssh> ", RP_ECHO); |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 918 | if (s == NULL) |
| 919 | goto out; |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 920 | while (*s && isspace(*s)) |
| 921 | s++; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 922 | if (*s == '-') |
| 923 | s++; /* Skip cmdline '-', if any */ |
Darren Tucker | e167582 | 2004-05-24 10:13:07 +1000 | [diff] [blame] | 924 | if (*s == '\0') |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 925 | goto out; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 926 | |
Darren Tucker | 1973c88 | 2004-05-24 10:34:36 +1000 | [diff] [blame] | 927 | if (*s == 'h' || *s == 'H' || *s == '?') { |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 928 | logit("Commands:"); |
Damien Miller | 4302095 | 2006-07-10 20:16:12 +1000 | [diff] [blame] | 929 | logit(" -L[bind_address:]port:host:hostport " |
| 930 | "Request local forward"); |
| 931 | logit(" -R[bind_address:]port:host:hostport " |
| 932 | "Request remote forward"); |
Damien Miller | 57e8ad3 | 2006-07-10 20:20:52 +1000 | [diff] [blame] | 933 | logit(" -KR[bind_address:]port " |
Damien Miller | 4302095 | 2006-07-10 20:16:12 +1000 | [diff] [blame] | 934 | "Cancel remote forward"); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 935 | if (!options.permit_local_command) |
| 936 | goto out; |
Damien Miller | 4302095 | 2006-07-10 20:16:12 +1000 | [diff] [blame] | 937 | logit(" !args " |
| 938 | "Execute local command"); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 939 | goto out; |
| 940 | } |
| 941 | |
| 942 | if (*s == '!' && options.permit_local_command) { |
| 943 | s++; |
| 944 | ssh_local_cmd(s); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 945 | goto out; |
| 946 | } |
| 947 | |
| 948 | if (*s == 'K') { |
| 949 | delete = 1; |
| 950 | s++; |
| 951 | } |
| 952 | if (*s != 'L' && *s != 'R') { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 953 | logit("Invalid command."); |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 954 | goto out; |
| 955 | } |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 956 | if (*s == 'L') |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 957 | local = 1; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 958 | if (local && delete) { |
| 959 | logit("Not supported."); |
| 960 | goto out; |
| 961 | } |
| 962 | if ((!local || delete) && !compat20) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 963 | logit("Not supported for SSH protocol version 1."); |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 964 | goto out; |
| 965 | } |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 966 | |
| 967 | s++; |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 968 | while (*s && isspace(*s)) |
| 969 | s++; |
| 970 | |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 971 | if (delete) { |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 972 | cancel_port = 0; |
| 973 | cancel_host = hpdelim(&s); /* may be NULL */ |
| 974 | if (s != NULL) { |
| 975 | cancel_port = a2port(s); |
| 976 | cancel_host = cleanhostname(cancel_host); |
| 977 | } else { |
| 978 | cancel_port = a2port(cancel_host); |
| 979 | cancel_host = NULL; |
| 980 | } |
| 981 | if (cancel_port == 0) { |
| 982 | logit("Bad forwarding close port"); |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 983 | goto out; |
| 984 | } |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 985 | channel_request_rforward_cancel(cancel_host, cancel_port); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 986 | } else { |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 987 | if (!parse_forward(&fwd, s)) { |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 988 | logit("Bad forwarding specification."); |
| 989 | goto out; |
| 990 | } |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 991 | if (local) { |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 992 | if (channel_setup_local_fwd_listener(fwd.listen_host, |
| 993 | fwd.listen_port, fwd.connect_host, |
| 994 | fwd.connect_port, options.gateway_ports) < 0) { |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 995 | logit("Port forwarding failed."); |
| 996 | goto out; |
| 997 | } |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 998 | } else { |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame^] | 999 | if (channel_request_remote_forwarding(fwd.listen_host, |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1000 | fwd.listen_port, fwd.connect_host, |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame^] | 1001 | fwd.connect_port) < 0) { |
| 1002 | logit("Port forwarding failed."); |
| 1003 | goto out; |
| 1004 | } |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 1005 | } |
| 1006 | |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 1007 | logit("Forwarding port."); |
| 1008 | } |
| 1009 | |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 1010 | out: |
| 1011 | signal(SIGINT, handler); |
| 1012 | enter_raw_mode(); |
| 1013 | if (cmd) |
| 1014 | xfree(cmd); |
| 1015 | } |
| 1016 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1017 | /* process the characters one by one */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1018 | static int |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1019 | process_escapes(Buffer *bin, Buffer *bout, Buffer *berr, char *buf, int len) |
| 1020 | { |
| 1021 | char string[1024]; |
| 1022 | pid_t pid; |
| 1023 | int bytes = 0; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1024 | u_int i; |
| 1025 | u_char ch; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1026 | char *s; |
| 1027 | |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 1028 | if (len <= 0) |
| 1029 | return (0); |
| 1030 | |
| 1031 | for (i = 0; i < (u_int)len; i++) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1032 | /* Get one character at a time. */ |
| 1033 | ch = buf[i]; |
| 1034 | |
| 1035 | if (escape_pending) { |
| 1036 | /* We have previously seen an escape character. */ |
| 1037 | /* Clear the flag now. */ |
| 1038 | escape_pending = 0; |
| 1039 | |
| 1040 | /* Process the escaped character. */ |
| 1041 | switch (ch) { |
| 1042 | case '.': |
| 1043 | /* Terminate the connection. */ |
| 1044 | snprintf(string, sizeof string, "%c.\r\n", escape_char); |
| 1045 | buffer_append(berr, string, strlen(string)); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1046 | |
| 1047 | quit_pending = 1; |
| 1048 | return -1; |
| 1049 | |
| 1050 | case 'Z' - 64: |
| 1051 | /* Suspend the program. */ |
| 1052 | /* Print a message to that effect to the user. */ |
| 1053 | snprintf(string, sizeof string, "%c^Z [suspend ssh]\r\n", escape_char); |
| 1054 | buffer_append(berr, string, strlen(string)); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1055 | |
| 1056 | /* Restore terminal modes and suspend. */ |
| 1057 | client_suspend_self(bin, bout, berr); |
| 1058 | |
| 1059 | /* We have been continued. */ |
| 1060 | continue; |
| 1061 | |
Damien Miller | 54c4598 | 2003-05-15 10:20:13 +1000 | [diff] [blame] | 1062 | case 'B': |
| 1063 | if (compat20) { |
| 1064 | snprintf(string, sizeof string, |
| 1065 | "%cB\r\n", escape_char); |
| 1066 | buffer_append(berr, string, |
| 1067 | strlen(string)); |
| 1068 | channel_request_start(session_ident, |
| 1069 | "break", 0); |
| 1070 | packet_put_int(1000); |
| 1071 | packet_send(); |
| 1072 | } |
| 1073 | continue; |
| 1074 | |
Ben Lindstrom | f28f634 | 2001-04-04 02:03:04 +0000 | [diff] [blame] | 1075 | case 'R': |
Ben Lindstrom | 11bd899 | 2001-04-05 23:34:29 +0000 | [diff] [blame] | 1076 | if (compat20) { |
| 1077 | if (datafellows & SSH_BUG_NOREKEY) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1078 | logit("Server does not support re-keying"); |
Ben Lindstrom | 11bd899 | 2001-04-05 23:34:29 +0000 | [diff] [blame] | 1079 | else |
| 1080 | need_rekeying = 1; |
| 1081 | } |
Ben Lindstrom | f28f634 | 2001-04-04 02:03:04 +0000 | [diff] [blame] | 1082 | continue; |
| 1083 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1084 | case '&': |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1085 | /* |
| 1086 | * Detach the program (continue to serve connections, |
| 1087 | * but put in background and no more new connections). |
| 1088 | */ |
Damien Miller | 96507ef | 2001-11-12 10:52:25 +1100 | [diff] [blame] | 1089 | /* Restore tty modes. */ |
| 1090 | leave_raw_mode(); |
| 1091 | |
| 1092 | /* Stop listening for new connections. */ |
| 1093 | channel_stop_listening(); |
| 1094 | |
| 1095 | snprintf(string, sizeof string, |
| 1096 | "%c& [backgrounded]\n", escape_char); |
| 1097 | buffer_append(berr, string, strlen(string)); |
| 1098 | |
| 1099 | /* Fork into background. */ |
| 1100 | pid = fork(); |
| 1101 | if (pid < 0) { |
| 1102 | error("fork: %.100s", strerror(errno)); |
| 1103 | continue; |
| 1104 | } |
| 1105 | if (pid != 0) { /* This is the parent. */ |
| 1106 | /* The parent just exits. */ |
| 1107 | exit(0); |
| 1108 | } |
| 1109 | /* The child continues serving connections. */ |
| 1110 | if (compat20) { |
| 1111 | buffer_append(bin, "\004", 1); |
| 1112 | /* fake EOF on stdin */ |
| 1113 | return -1; |
| 1114 | } else if (!stdin_eof) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1115 | /* |
| 1116 | * Sending SSH_CMSG_EOF alone does not always appear |
| 1117 | * to be enough. So we try to send an EOF character |
| 1118 | * first. |
| 1119 | */ |
| 1120 | packet_start(SSH_CMSG_STDIN_DATA); |
| 1121 | packet_put_string("\004", 1); |
| 1122 | packet_send(); |
| 1123 | /* Close stdin. */ |
| 1124 | stdin_eof = 1; |
| 1125 | if (buffer_len(bin) == 0) { |
| 1126 | packet_start(SSH_CMSG_EOF); |
| 1127 | packet_send(); |
| 1128 | } |
| 1129 | } |
Damien Miller | 96507ef | 2001-11-12 10:52:25 +1100 | [diff] [blame] | 1130 | continue; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1131 | |
| 1132 | case '?': |
| 1133 | snprintf(string, sizeof string, |
| 1134 | "%c?\r\n\ |
| 1135 | Supported escape sequences:\r\n\ |
Damien Miller | 0669286 | 2002-09-04 16:32:10 +1000 | [diff] [blame] | 1136 | %c. - terminate connection\r\n\ |
Damien Miller | 54c4598 | 2003-05-15 10:20:13 +1000 | [diff] [blame] | 1137 | %cB - send a BREAK to the remote system\r\n\ |
Damien Miller | 0669286 | 2002-09-04 16:32:10 +1000 | [diff] [blame] | 1138 | %cC - open a command line\r\n\ |
| 1139 | %cR - Request rekey (SSH protocol 2 only)\r\n\ |
| 1140 | %c^Z - suspend ssh\r\n\ |
| 1141 | %c# - list forwarded connections\r\n\ |
| 1142 | %c& - background ssh (when waiting for connections to terminate)\r\n\ |
| 1143 | %c? - this message\r\n\ |
| 1144 | %c%c - send the escape character by typing it twice\r\n\ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1145 | (Note that escapes are only recognized immediately after newline.)\r\n", |
Damien Miller | 0669286 | 2002-09-04 16:32:10 +1000 | [diff] [blame] | 1146 | escape_char, escape_char, escape_char, escape_char, |
| 1147 | escape_char, escape_char, escape_char, escape_char, |
Damien Miller | 54c4598 | 2003-05-15 10:20:13 +1000 | [diff] [blame] | 1148 | escape_char, escape_char, escape_char); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1149 | buffer_append(berr, string, strlen(string)); |
| 1150 | continue; |
| 1151 | |
| 1152 | case '#': |
| 1153 | snprintf(string, sizeof string, "%c#\r\n", escape_char); |
| 1154 | buffer_append(berr, string, strlen(string)); |
| 1155 | s = channel_open_message(); |
| 1156 | buffer_append(berr, s, strlen(s)); |
| 1157 | xfree(s); |
| 1158 | continue; |
| 1159 | |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 1160 | case 'C': |
Ben Lindstrom | 681d932 | 2002-03-22 03:53:00 +0000 | [diff] [blame] | 1161 | process_cmdline(); |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 1162 | continue; |
| 1163 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1164 | default: |
| 1165 | if (ch != escape_char) { |
| 1166 | buffer_put_char(bin, escape_char); |
| 1167 | bytes++; |
| 1168 | } |
| 1169 | /* Escaped characters fall through here */ |
| 1170 | break; |
| 1171 | } |
| 1172 | } else { |
| 1173 | /* |
| 1174 | * The previous character was not an escape char. Check if this |
| 1175 | * is an escape. |
| 1176 | */ |
| 1177 | if (last_was_cr && ch == escape_char) { |
| 1178 | /* It is. Set the flag and continue to next character. */ |
| 1179 | escape_pending = 1; |
| 1180 | continue; |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | /* |
| 1185 | * Normal character. Record whether it was a newline, |
| 1186 | * and append it to the buffer. |
| 1187 | */ |
| 1188 | last_was_cr = (ch == '\r' || ch == '\n'); |
| 1189 | buffer_put_char(bin, ch); |
| 1190 | bytes++; |
| 1191 | } |
| 1192 | return bytes; |
| 1193 | } |
| 1194 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1195 | static void |
Damien Miller | 90fdfaf | 2006-03-26 14:25:37 +1100 | [diff] [blame] | 1196 | client_process_input(fd_set *readset) |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1197 | { |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 1198 | int len; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1199 | char buf[8192]; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1200 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1201 | /* Read input from stdin. */ |
| 1202 | if (FD_ISSET(fileno(stdin), readset)) { |
| 1203 | /* Read as much as possible. */ |
| 1204 | len = read(fileno(stdin), buf, sizeof(buf)); |
Ben Lindstrom | 4c8cff1 | 2001-04-17 18:09:42 +0000 | [diff] [blame] | 1205 | if (len < 0 && (errno == EAGAIN || errno == EINTR)) |
| 1206 | return; /* we'll try again later */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1207 | if (len <= 0) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1208 | /* |
| 1209 | * Received EOF or error. They are treated |
| 1210 | * similarly, except that an error message is printed |
| 1211 | * if it was an error condition. |
| 1212 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1213 | if (len < 0) { |
| 1214 | snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno)); |
| 1215 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1216 | } |
| 1217 | /* Mark that we have seen EOF. */ |
| 1218 | stdin_eof = 1; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1219 | /* |
| 1220 | * Send an EOF message to the server unless there is |
| 1221 | * data in the buffer. If there is data in the |
| 1222 | * buffer, no message will be sent now. Code |
| 1223 | * elsewhere will send the EOF when the buffer |
| 1224 | * becomes empty if stdin_eof is set. |
| 1225 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1226 | if (buffer_len(&stdin_buffer) == 0) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1227 | packet_start(SSH_CMSG_EOF); |
| 1228 | packet_send(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1229 | } |
Ben Lindstrom | 2b1f71b | 2001-06-05 20:32:21 +0000 | [diff] [blame] | 1230 | } else if (escape_char == SSH_ESCAPECHAR_NONE) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1231 | /* |
| 1232 | * Normal successful read, and no escape character. |
| 1233 | * Just append the data to buffer. |
| 1234 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1235 | buffer_append(&stdin_buffer, buf, len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1236 | } else { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1237 | /* |
| 1238 | * Normal, successful read. But we have an escape character |
| 1239 | * and have to process the characters one by one. |
| 1240 | */ |
Ben Lindstrom | e9613cf | 2001-03-05 06:14:02 +0000 | [diff] [blame] | 1241 | if (process_escapes(&stdin_buffer, &stdout_buffer, |
| 1242 | &stderr_buffer, buf, len) == -1) |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1243 | return; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1244 | } |
| 1245 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1246 | } |
| 1247 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1248 | static void |
Damien Miller | 90fdfaf | 2006-03-26 14:25:37 +1100 | [diff] [blame] | 1249 | client_process_output(fd_set *writeset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1250 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1251 | int len; |
| 1252 | char buf[100]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1253 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1254 | /* Write buffered output to stdout. */ |
| 1255 | if (FD_ISSET(fileno(stdout), writeset)) { |
| 1256 | /* Write as much data as possible. */ |
| 1257 | len = write(fileno(stdout), buffer_ptr(&stdout_buffer), |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 1258 | buffer_len(&stdout_buffer)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1259 | if (len <= 0) { |
Ben Lindstrom | 4c8cff1 | 2001-04-17 18:09:42 +0000 | [diff] [blame] | 1260 | if (errno == EINTR || errno == EAGAIN) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1261 | len = 0; |
| 1262 | else { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1263 | /* |
| 1264 | * An error or EOF was encountered. Put an |
| 1265 | * error message to stderr buffer. |
| 1266 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1267 | snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno)); |
| 1268 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1269 | quit_pending = 1; |
| 1270 | return; |
| 1271 | } |
| 1272 | } |
| 1273 | /* Consume printed data from the buffer. */ |
| 1274 | buffer_consume(&stdout_buffer, len); |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 1275 | stdout_bytes += len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1276 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1277 | /* Write buffered output to stderr. */ |
| 1278 | if (FD_ISSET(fileno(stderr), writeset)) { |
| 1279 | /* Write as much data as possible. */ |
| 1280 | len = write(fileno(stderr), buffer_ptr(&stderr_buffer), |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 1281 | buffer_len(&stderr_buffer)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1282 | if (len <= 0) { |
Ben Lindstrom | 4c8cff1 | 2001-04-17 18:09:42 +0000 | [diff] [blame] | 1283 | if (errno == EINTR || errno == EAGAIN) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1284 | len = 0; |
| 1285 | else { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1286 | /* EOF or error, but can't even print error message. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1287 | quit_pending = 1; |
| 1288 | return; |
| 1289 | } |
| 1290 | } |
| 1291 | /* Consume printed characters from the buffer. */ |
| 1292 | buffer_consume(&stderr_buffer, len); |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 1293 | stderr_bytes += len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1294 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1295 | } |
| 1296 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1297 | /* |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1298 | * Get packets from the connection input buffer, and process them as long as |
| 1299 | * there are packets available. |
| 1300 | * |
| 1301 | * Any unknown packets received during the actual |
| 1302 | * session cause the session to terminate. This is |
| 1303 | * intended to make debugging easier since no |
| 1304 | * confirmations are sent. Any compatible protocol |
| 1305 | * extensions must be negotiated during the |
| 1306 | * preparatory phase. |
| 1307 | */ |
| 1308 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1309 | static void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 1310 | client_process_buffered_input_packets(void) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1311 | { |
Ben Lindstrom | f28f634 | 2001-04-04 02:03:04 +0000 | [diff] [blame] | 1312 | dispatch_run(DISPATCH_NONBLOCK, &quit_pending, compat20 ? xxx_kex : NULL); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1313 | } |
| 1314 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1315 | /* scan buf[] for '~' before sending data to the peer */ |
| 1316 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1317 | static int |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1318 | simple_escape_filter(Channel *c, char *buf, int len) |
| 1319 | { |
| 1320 | /* XXX we assume c->extended is writeable */ |
| 1321 | return process_escapes(&c->input, &c->output, &c->extended, buf, len); |
| 1322 | } |
| 1323 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1324 | static void |
Ben Lindstrom | 4c3f77d | 2001-04-05 23:37:36 +0000 | [diff] [blame] | 1325 | client_channel_closed(int id, void *arg) |
| 1326 | { |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 1327 | channel_cancel_cleanup(id); |
Ben Lindstrom | 4c3f77d | 2001-04-05 23:37:36 +0000 | [diff] [blame] | 1328 | session_closed = 1; |
Darren Tucker | 9a2c4cd | 2003-09-22 21:16:05 +1000 | [diff] [blame] | 1329 | leave_raw_mode(); |
Ben Lindstrom | 4c3f77d | 2001-04-05 23:37:36 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1332 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1333 | * Implements the interactive session with the server. This is called after |
| 1334 | * 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] | 1335 | * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character |
| 1336 | * used as an escape character for terminating or suspending the session. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1337 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1338 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1339 | int |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1340 | 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] | 1341 | { |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 1342 | fd_set *readset = NULL, *writeset = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1343 | double start_time, total_time; |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 1344 | int max_fd = 0, max_fd2 = 0, len, rekeying = 0; |
| 1345 | u_int nalloc = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1346 | char buf[100]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1347 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1348 | debug("Entering interactive session."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1349 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1350 | start_time = get_current_time(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1351 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1352 | /* Initialize variables. */ |
| 1353 | escape_pending = 0; |
| 1354 | last_was_cr = 1; |
| 1355 | exit_status = -1; |
| 1356 | stdin_eof = 0; |
| 1357 | buffer_high = 64 * 1024; |
| 1358 | connection_in = packet_get_connection_in(); |
| 1359 | connection_out = packet_get_connection_out(); |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 1360 | max_fd = MAX(connection_in, connection_out); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1361 | if (control_fd != -1) |
| 1362 | max_fd = MAX(max_fd, control_fd); |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 1363 | |
| 1364 | if (!compat20) { |
Ben Lindstrom | 302ea6f | 2001-04-16 02:01:25 +0000 | [diff] [blame] | 1365 | /* enable nonblocking unless tty */ |
| 1366 | if (!isatty(fileno(stdin))) |
| 1367 | set_nonblock(fileno(stdin)); |
| 1368 | if (!isatty(fileno(stdout))) |
| 1369 | set_nonblock(fileno(stdout)); |
| 1370 | if (!isatty(fileno(stderr))) |
| 1371 | set_nonblock(fileno(stderr)); |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 1372 | max_fd = MAX(max_fd, fileno(stdin)); |
| 1373 | max_fd = MAX(max_fd, fileno(stdout)); |
| 1374 | max_fd = MAX(max_fd, fileno(stderr)); |
| 1375 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1376 | stdin_bytes = 0; |
| 1377 | stdout_bytes = 0; |
| 1378 | stderr_bytes = 0; |
| 1379 | quit_pending = 0; |
| 1380 | escape_char = escape_char_arg; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1381 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1382 | /* Initialize buffers. */ |
| 1383 | buffer_init(&stdin_buffer); |
| 1384 | buffer_init(&stdout_buffer); |
| 1385 | buffer_init(&stderr_buffer); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1386 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1387 | client_init_dispatch(); |
| 1388 | |
Ben Lindstrom | f49dbff | 2002-12-23 02:01:55 +0000 | [diff] [blame] | 1389 | /* |
| 1390 | * Set signal handlers, (e.g. to restore non-blocking mode) |
| 1391 | * but don't overwrite SIG_IGN, matches behaviour from rsh(1) |
| 1392 | */ |
Darren Tucker | 07336da | 2004-11-05 20:02:16 +1100 | [diff] [blame] | 1393 | if (signal(SIGHUP, SIG_IGN) != SIG_IGN) |
| 1394 | signal(SIGHUP, signal_handler); |
Ben Lindstrom | f49dbff | 2002-12-23 02:01:55 +0000 | [diff] [blame] | 1395 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 1396 | signal(SIGINT, signal_handler); |
| 1397 | if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) |
| 1398 | signal(SIGQUIT, signal_handler); |
| 1399 | if (signal(SIGTERM, SIG_IGN) != SIG_IGN) |
| 1400 | signal(SIGTERM, signal_handler); |
Darren Tucker | 5d78de6 | 2004-11-05 20:35:44 +1100 | [diff] [blame] | 1401 | signal(SIGWINCH, window_change_handler); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1402 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1403 | if (have_pty) |
| 1404 | enter_raw_mode(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1405 | |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 1406 | if (compat20) { |
| 1407 | session_ident = ssh2_chan_id; |
Ben Lindstrom | 2b1f71b | 2001-06-05 20:32:21 +0000 | [diff] [blame] | 1408 | if (escape_char != SSH_ESCAPECHAR_NONE) |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 1409 | channel_register_filter(session_ident, |
Damien Miller | 077b238 | 2005-12-31 16:22:32 +1100 | [diff] [blame] | 1410 | simple_escape_filter, NULL); |
Ben Lindstrom | 4c3f77d | 2001-04-05 23:37:36 +0000 | [diff] [blame] | 1411 | if (session_ident != -1) |
| 1412 | channel_register_cleanup(session_ident, |
Damien Miller | 39eda6e | 2005-11-05 14:52:50 +1100 | [diff] [blame] | 1413 | client_channel_closed, 0); |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 1414 | } else { |
| 1415 | /* Check if we should immediately send eof on stdin. */ |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1416 | client_check_initial_eof_on_stdin(); |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 1417 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1418 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1419 | /* Main loop of the client for the interactive session mode. */ |
| 1420 | while (!quit_pending) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1421 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1422 | /* Process buffered packets sent by the server. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1423 | client_process_buffered_input_packets(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1424 | |
Ben Lindstrom | 4c3f77d | 2001-04-05 23:37:36 +0000 | [diff] [blame] | 1425 | if (compat20 && session_closed && !channel_still_open()) |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1426 | break; |
Ben Lindstrom | 4c3f77d | 2001-04-05 23:37:36 +0000 | [diff] [blame] | 1427 | |
| 1428 | rekeying = (xxx_kex != NULL && !xxx_kex->done); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1429 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 1430 | if (rekeying) { |
| 1431 | debug("rekeying in progress"); |
| 1432 | } else { |
| 1433 | /* |
| 1434 | * Make packets of buffered stdin data, and buffer |
| 1435 | * them for sending to the server. |
| 1436 | */ |
| 1437 | if (!compat20) |
| 1438 | client_make_packets_from_stdin_data(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1439 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 1440 | /* |
| 1441 | * Make packets from buffered channel data, and |
| 1442 | * enqueue them for sending to the server. |
| 1443 | */ |
| 1444 | if (packet_not_very_much_data_to_write()) |
| 1445 | channel_output_poll(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1446 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 1447 | /* |
| 1448 | * Check if the window size has changed, and buffer a |
| 1449 | * message about it to the server if so. |
| 1450 | */ |
| 1451 | client_check_window_change(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1452 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 1453 | if (quit_pending) |
| 1454 | break; |
| 1455 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1456 | /* |
| 1457 | * Wait until we have something to do (something becomes |
| 1458 | * available on one of the descriptors). |
| 1459 | */ |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 1460 | max_fd2 = max_fd; |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 1461 | client_wait_until_can_do_something(&readset, &writeset, |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 1462 | &max_fd2, &nalloc, rekeying); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1463 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1464 | if (quit_pending) |
| 1465 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1466 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 1467 | /* Do channel operations unless rekeying in progress. */ |
| 1468 | if (!rekeying) { |
| 1469 | channel_after_select(readset, writeset); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1470 | if (need_rekeying || packet_need_rekeying()) { |
| 1471 | debug("need rekeying"); |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 1472 | xxx_kex->done = 0; |
| 1473 | kex_send_kexinit(xxx_kex); |
| 1474 | need_rekeying = 0; |
| 1475 | } |
| 1476 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1477 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1478 | /* Buffer input from the connection. */ |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 1479 | client_process_net_input(readset); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1480 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1481 | /* Accept control connections. */ |
| 1482 | client_process_control(readset); |
| 1483 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1484 | if (quit_pending) |
| 1485 | break; |
| 1486 | |
| 1487 | if (!compat20) { |
| 1488 | /* Buffer data from stdin */ |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 1489 | client_process_input(readset); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1490 | /* |
| 1491 | * Process output to stdout and stderr. Output to |
| 1492 | * the connection is processed elsewhere (above). |
| 1493 | */ |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 1494 | client_process_output(writeset); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1495 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1496 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1497 | /* Send as much buffered packet data as possible to the sender. */ |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 1498 | if (FD_ISSET(connection_out, writeset)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1499 | packet_write_poll(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1500 | } |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 1501 | if (readset) |
| 1502 | xfree(readset); |
| 1503 | if (writeset) |
| 1504 | xfree(writeset); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1505 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1506 | /* Terminate the session. */ |
| 1507 | |
| 1508 | /* Stop watching for window change. */ |
Darren Tucker | 5d78de6 | 2004-11-05 20:35:44 +1100 | [diff] [blame] | 1509 | signal(SIGWINCH, SIG_DFL); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1510 | |
Ben Lindstrom | 601e436 | 2001-06-21 03:19:23 +0000 | [diff] [blame] | 1511 | channel_free_all(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1512 | |
Ben Lindstrom | ec46e0b | 2001-06-09 01:27:31 +0000 | [diff] [blame] | 1513 | if (have_pty) |
| 1514 | leave_raw_mode(); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 1515 | |
| 1516 | /* restore blocking io */ |
| 1517 | if (!isatty(fileno(stdin))) |
| 1518 | unset_nonblock(fileno(stdin)); |
| 1519 | if (!isatty(fileno(stdout))) |
| 1520 | unset_nonblock(fileno(stdout)); |
| 1521 | if (!isatty(fileno(stderr))) |
| 1522 | unset_nonblock(fileno(stderr)); |
| 1523 | |
Damien Miller | d696551 | 2003-12-17 16:31:53 +1100 | [diff] [blame] | 1524 | /* |
| 1525 | * If there was no shell or command requested, there will be no remote |
| 1526 | * exit status to be returned. In that case, clear error code if the |
| 1527 | * connection was deliberately terminated at this end. |
| 1528 | */ |
| 1529 | if (no_shell_flag && received_signal == SIGTERM) { |
| 1530 | received_signal = 0; |
| 1531 | exit_status = 0; |
| 1532 | } |
| 1533 | |
Darren Tucker | 9a2c4cd | 2003-09-22 21:16:05 +1000 | [diff] [blame] | 1534 | if (received_signal) |
Ben Lindstrom | f8f065b | 2001-12-06 17:52:16 +0000 | [diff] [blame] | 1535 | fatal("Killed by signal %d.", (int) received_signal); |
Ben Lindstrom | ec46e0b | 2001-06-09 01:27:31 +0000 | [diff] [blame] | 1536 | |
| 1537 | /* |
| 1538 | * In interactive mode (with pseudo tty) display a message indicating |
| 1539 | * that the connection has been closed. |
| 1540 | */ |
| 1541 | if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) { |
| 1542 | snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host); |
| 1543 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
| 1544 | } |
| 1545 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1546 | /* Output any buffered data for stdout. */ |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 1547 | while (buffer_len(&stdout_buffer) > 0) { |
| 1548 | len = write(fileno(stdout), buffer_ptr(&stdout_buffer), |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 1549 | buffer_len(&stdout_buffer)); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 1550 | if (len <= 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1551 | error("Write failed flushing stdout buffer."); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 1552 | break; |
| 1553 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1554 | buffer_consume(&stdout_buffer, len); |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 1555 | stdout_bytes += len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1556 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1557 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1558 | /* Output any buffered data for stderr. */ |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 1559 | while (buffer_len(&stderr_buffer) > 0) { |
| 1560 | len = write(fileno(stderr), buffer_ptr(&stderr_buffer), |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 1561 | buffer_len(&stderr_buffer)); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 1562 | if (len <= 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1563 | error("Write failed flushing stderr buffer."); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 1564 | break; |
| 1565 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1566 | buffer_consume(&stderr_buffer, len); |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 1567 | stderr_bytes += len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1568 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1569 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1570 | /* Clear and free any buffers. */ |
| 1571 | memset(buf, 0, sizeof(buf)); |
| 1572 | buffer_free(&stdin_buffer); |
| 1573 | buffer_free(&stdout_buffer); |
| 1574 | buffer_free(&stderr_buffer); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1575 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1576 | /* Report bytes transferred, and transfer rates. */ |
| 1577 | total_time = get_current_time() - start_time; |
| 1578 | debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1579 | stdin_bytes, stdout_bytes, stderr_bytes, total_time); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1580 | if (total_time > 0) |
| 1581 | debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1582 | stdin_bytes / total_time, stdout_bytes / total_time, |
| 1583 | stderr_bytes / total_time); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1584 | |
| 1585 | /* Return the exit status of the program. */ |
| 1586 | debug("Exit status %d", exit_status); |
| 1587 | return exit_status; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1588 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1589 | |
| 1590 | /*********/ |
| 1591 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1592 | static void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 1593 | client_input_stdout_data(int type, u_int32_t seq, void *ctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1594 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1595 | u_int data_len; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1596 | char *data = packet_get_string(&data_len); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1597 | packet_check_eom(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1598 | buffer_append(&stdout_buffer, data, data_len); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1599 | memset(data, 0, data_len); |
| 1600 | xfree(data); |
| 1601 | } |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1602 | static void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 1603 | client_input_stderr_data(int type, u_int32_t seq, void *ctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1604 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1605 | u_int data_len; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1606 | char *data = packet_get_string(&data_len); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1607 | packet_check_eom(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1608 | buffer_append(&stderr_buffer, data, data_len); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1609 | memset(data, 0, data_len); |
| 1610 | xfree(data); |
| 1611 | } |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1612 | static void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 1613 | client_input_exit_status(int type, u_int32_t seq, void *ctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1614 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1615 | exit_status = packet_get_int(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1616 | packet_check_eom(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1617 | /* Acknowledge the exit. */ |
| 1618 | packet_start(SSH_CMSG_EXIT_CONFIRMATION); |
| 1619 | packet_send(); |
| 1620 | /* |
| 1621 | * Must wait for packet to be sent since we are |
| 1622 | * exiting the loop. |
| 1623 | */ |
| 1624 | packet_write_wait(); |
| 1625 | /* Flag that we want to exit. */ |
| 1626 | quit_pending = 1; |
| 1627 | } |
Darren Tucker | 5dcdd21 | 2003-10-02 16:17:00 +1000 | [diff] [blame] | 1628 | static void |
| 1629 | client_input_agent_open(int type, u_int32_t seq, void *ctxt) |
| 1630 | { |
| 1631 | Channel *c = NULL; |
| 1632 | int remote_id, sock; |
| 1633 | |
| 1634 | /* Read the remote channel number from the message. */ |
| 1635 | remote_id = packet_get_int(); |
| 1636 | packet_check_eom(); |
| 1637 | |
| 1638 | /* |
| 1639 | * Get a connection to the local authentication agent (this may again |
| 1640 | * get forwarded). |
| 1641 | */ |
| 1642 | sock = ssh_get_authentication_socket(); |
| 1643 | |
| 1644 | /* |
| 1645 | * If we could not connect the agent, send an error message back to |
| 1646 | * the server. This should never happen unless the agent dies, |
| 1647 | * because authentication forwarding is only enabled if we have an |
| 1648 | * agent. |
| 1649 | */ |
| 1650 | if (sock >= 0) { |
| 1651 | c = channel_new("", SSH_CHANNEL_OPEN, sock, sock, |
| 1652 | -1, 0, 0, 0, "authentication agent connection", 1); |
| 1653 | c->remote_id = remote_id; |
| 1654 | c->force_drain = 1; |
| 1655 | } |
| 1656 | if (c == NULL) { |
| 1657 | packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE); |
| 1658 | packet_put_int(remote_id); |
| 1659 | } else { |
| 1660 | /* Send a confirmation to the remote host. */ |
| 1661 | debug("Forwarding authentication connection."); |
| 1662 | packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION); |
| 1663 | packet_put_int(remote_id); |
| 1664 | packet_put_int(c->self); |
| 1665 | } |
| 1666 | packet_send(); |
| 1667 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1668 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1669 | static Channel * |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1670 | client_request_forwarded_tcpip(const char *request_type, int rchan) |
| 1671 | { |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 1672 | Channel *c = NULL; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1673 | char *listen_address, *originator_address; |
| 1674 | int listen_port, originator_port; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1675 | int sock; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1676 | |
| 1677 | /* Get rest of the packet */ |
| 1678 | listen_address = packet_get_string(NULL); |
| 1679 | listen_port = packet_get_int(); |
| 1680 | originator_address = packet_get_string(NULL); |
| 1681 | originator_port = packet_get_int(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1682 | packet_check_eom(); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1683 | |
| 1684 | debug("client_request_forwarded_tcpip: listen %s port %d, originator %s port %d", |
| 1685 | listen_address, listen_port, originator_address, originator_port); |
| 1686 | |
Ben Lindstrom | 173e646 | 2001-07-04 05:15:15 +0000 | [diff] [blame] | 1687 | sock = channel_connect_by_listen_address(listen_port); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1688 | if (sock < 0) { |
| 1689 | xfree(originator_address); |
| 1690 | xfree(listen_address); |
| 1691 | return NULL; |
| 1692 | } |
| 1693 | c = channel_new("forwarded-tcpip", |
| 1694 | SSH_CHANNEL_CONNECTING, sock, sock, -1, |
| 1695 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 1696 | originator_address, 1); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1697 | xfree(originator_address); |
| 1698 | xfree(listen_address); |
| 1699 | return c; |
| 1700 | } |
| 1701 | |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 1702 | static Channel * |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1703 | client_request_x11(const char *request_type, int rchan) |
| 1704 | { |
| 1705 | Channel *c = NULL; |
| 1706 | char *originator; |
| 1707 | int originator_port; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1708 | int sock; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1709 | |
| 1710 | if (!options.forward_x11) { |
| 1711 | error("Warning: ssh server tried X11 forwarding."); |
Damien Miller | 5eb137c | 2005-12-31 16:19:53 +1100 | [diff] [blame] | 1712 | error("Warning: this is probably a break-in attempt by a malicious server."); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1713 | return NULL; |
| 1714 | } |
| 1715 | originator = packet_get_string(NULL); |
| 1716 | if (datafellows & SSH_BUG_X11FWD) { |
| 1717 | debug2("buggy server: x11 request w/o originator_port"); |
| 1718 | originator_port = 0; |
| 1719 | } else { |
| 1720 | originator_port = packet_get_int(); |
| 1721 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1722 | packet_check_eom(); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1723 | /* XXX check permission */ |
Damien Miller | d83ff35 | 2001-01-30 09:19:34 +1100 | [diff] [blame] | 1724 | debug("client_request_x11: request from %s %d", originator, |
| 1725 | originator_port); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1726 | xfree(originator); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1727 | sock = x11_connect_display(); |
| 1728 | if (sock < 0) |
| 1729 | return NULL; |
| 1730 | c = channel_new("x11", |
| 1731 | SSH_CHANNEL_X11_OPEN, sock, sock, -1, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 1732 | CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 1733 | c->force_drain = 1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1734 | return c; |
| 1735 | } |
| 1736 | |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 1737 | static Channel * |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1738 | client_request_agent(const char *request_type, int rchan) |
| 1739 | { |
| 1740 | Channel *c = NULL; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1741 | int sock; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1742 | |
| 1743 | if (!options.forward_agent) { |
| 1744 | error("Warning: ssh server tried agent forwarding."); |
Damien Miller | 5eb137c | 2005-12-31 16:19:53 +1100 | [diff] [blame] | 1745 | error("Warning: this is probably a break-in attempt by a malicious server."); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1746 | return NULL; |
| 1747 | } |
| 1748 | sock = ssh_get_authentication_socket(); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1749 | if (sock < 0) |
| 1750 | return NULL; |
| 1751 | c = channel_new("authentication agent connection", |
| 1752 | SSH_CHANNEL_OPEN, sock, sock, -1, |
| 1753 | CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 1754 | "authentication agent connection", 1); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 1755 | c->force_drain = 1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1756 | return c; |
| 1757 | } |
| 1758 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1759 | /* XXXX move to generic input handler */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1760 | static void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 1761 | client_input_channel_open(int type, u_int32_t seq, void *ctxt) |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1762 | { |
| 1763 | Channel *c = NULL; |
| 1764 | char *ctype; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1765 | int rchan; |
Ben Lindstrom | 4fed2be | 2002-06-25 23:17:36 +0000 | [diff] [blame] | 1766 | u_int rmaxpack, rwindow, len; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1767 | |
| 1768 | ctype = packet_get_string(&len); |
| 1769 | rchan = packet_get_int(); |
| 1770 | rwindow = packet_get_int(); |
| 1771 | rmaxpack = packet_get_int(); |
| 1772 | |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1773 | 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] | 1774 | ctype, rchan, rwindow, rmaxpack); |
| 1775 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1776 | if (strcmp(ctype, "forwarded-tcpip") == 0) { |
| 1777 | c = client_request_forwarded_tcpip(ctype, rchan); |
| 1778 | } else if (strcmp(ctype, "x11") == 0) { |
| 1779 | c = client_request_x11(ctype, rchan); |
| 1780 | } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) { |
| 1781 | c = client_request_agent(ctype, rchan); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1782 | } |
| 1783 | /* XXX duplicate : */ |
| 1784 | if (c != NULL) { |
| 1785 | debug("confirm %s", ctype); |
| 1786 | c->remote_id = rchan; |
| 1787 | c->remote_window = rwindow; |
| 1788 | c->remote_maxpacket = rmaxpack; |
Ben Lindstrom | a69d89b | 2001-05-09 00:01:18 +0000 | [diff] [blame] | 1789 | if (c->type != SSH_CHANNEL_CONNECTING) { |
| 1790 | packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); |
| 1791 | packet_put_int(c->remote_id); |
| 1792 | packet_put_int(c->self); |
| 1793 | packet_put_int(c->local_window); |
| 1794 | packet_put_int(c->local_maxpacket); |
| 1795 | packet_send(); |
| 1796 | } |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1797 | } else { |
| 1798 | debug("failure %s", ctype); |
| 1799 | packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE); |
| 1800 | packet_put_int(rchan); |
| 1801 | packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED); |
Ben Lindstrom | f343674 | 2001-04-29 19:52:00 +0000 | [diff] [blame] | 1802 | if (!(datafellows & SSH_BUG_OPENFAILURE)) { |
Ben Lindstrom | a69d89b | 2001-05-09 00:01:18 +0000 | [diff] [blame] | 1803 | packet_put_cstring("open failed"); |
Ben Lindstrom | f343674 | 2001-04-29 19:52:00 +0000 | [diff] [blame] | 1804 | packet_put_cstring(""); |
| 1805 | } |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1806 | packet_send(); |
| 1807 | } |
| 1808 | xfree(ctype); |
| 1809 | } |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1810 | static void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 1811 | client_input_channel_req(int type, u_int32_t seq, void *ctxt) |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 1812 | { |
| 1813 | Channel *c = NULL; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1814 | int exitval, id, reply, success = 0; |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 1815 | char *rtype; |
| 1816 | |
| 1817 | id = packet_get_int(); |
| 1818 | rtype = packet_get_string(NULL); |
| 1819 | reply = packet_get_char(); |
| 1820 | |
| 1821 | debug("client_input_channel_req: channel %d rtype %s reply %d", |
| 1822 | id, rtype, reply); |
| 1823 | |
Damien Miller | 3bbd878 | 2004-06-18 22:23:22 +1000 | [diff] [blame] | 1824 | if (id == -1) { |
| 1825 | error("client_input_channel_req: request for channel -1"); |
| 1826 | } else if ((c = channel_lookup(id)) == NULL) { |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 1827 | error("client_input_channel_req: channel %d: unknown channel", id); |
| 1828 | } else if (strcmp(rtype, "exit-status") == 0) { |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1829 | exitval = packet_get_int(); |
| 1830 | if (id == session_ident) { |
| 1831 | success = 1; |
| 1832 | exit_status = exitval; |
| 1833 | } else if (c->ctl_fd == -1) { |
| 1834 | error("client_input_channel_req: unexpected channel %d", |
| 1835 | session_ident); |
| 1836 | } else { |
| 1837 | atomicio(vwrite, c->ctl_fd, &exitval, sizeof(exitval)); |
| 1838 | success = 1; |
| 1839 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1840 | packet_check_eom(); |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 1841 | } |
| 1842 | if (reply) { |
| 1843 | packet_start(success ? |
| 1844 | SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE); |
Damien Miller | 3bbd878 | 2004-06-18 22:23:22 +1000 | [diff] [blame] | 1845 | packet_put_int(id); |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 1846 | packet_send(); |
| 1847 | } |
| 1848 | xfree(rtype); |
| 1849 | } |
Damien Miller | c3fa407 | 2002-01-22 23:21:58 +1100 | [diff] [blame] | 1850 | static void |
| 1851 | client_input_global_request(int type, u_int32_t seq, void *ctxt) |
| 1852 | { |
| 1853 | char *rtype; |
| 1854 | int want_reply; |
| 1855 | int success = 0; |
| 1856 | |
| 1857 | rtype = packet_get_string(NULL); |
| 1858 | want_reply = packet_get_char(); |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 1859 | debug("client_input_global_request: rtype %s want_reply %d", |
| 1860 | rtype, want_reply); |
Damien Miller | c3fa407 | 2002-01-22 23:21:58 +1100 | [diff] [blame] | 1861 | if (want_reply) { |
| 1862 | packet_start(success ? |
| 1863 | SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE); |
| 1864 | packet_send(); |
| 1865 | packet_write_wait(); |
| 1866 | } |
| 1867 | xfree(rtype); |
| 1868 | } |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1869 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1870 | void |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 1871 | client_session2_setup(int id, int want_tty, int want_subsystem, |
Damien Miller | 3756dce | 2004-06-18 01:17:29 +1000 | [diff] [blame] | 1872 | const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env, |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1873 | dispatch_fn *subsys_repl) |
| 1874 | { |
| 1875 | int len; |
Darren Tucker | 5d78de6 | 2004-11-05 20:35:44 +1100 | [diff] [blame] | 1876 | Channel *c = NULL; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1877 | |
| 1878 | debug2("%s: id %d", __func__, id); |
| 1879 | |
Darren Tucker | 5d78de6 | 2004-11-05 20:35:44 +1100 | [diff] [blame] | 1880 | if ((c = channel_lookup(id)) == NULL) |
| 1881 | fatal("client_session2_setup: channel %d: unknown channel", id); |
| 1882 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1883 | if (want_tty) { |
| 1884 | struct winsize ws; |
| 1885 | struct termios tio; |
| 1886 | |
| 1887 | /* Store window size in the packet. */ |
| 1888 | if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0) |
| 1889 | memset(&ws, 0, sizeof(ws)); |
| 1890 | |
| 1891 | channel_request_start(id, "pty-req", 0); |
| 1892 | packet_put_cstring(term != NULL ? term : ""); |
Damien Miller | 71a7367 | 2006-03-26 14:04:36 +1100 | [diff] [blame] | 1893 | packet_put_int((u_int)ws.ws_col); |
| 1894 | packet_put_int((u_int)ws.ws_row); |
| 1895 | packet_put_int((u_int)ws.ws_xpixel); |
| 1896 | packet_put_int((u_int)ws.ws_ypixel); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1897 | tio = get_saved_tio(); |
| 1898 | tty_make_modes(-1, tiop != NULL ? tiop : &tio); |
| 1899 | packet_send(); |
| 1900 | /* XXX wait for reply */ |
Darren Tucker | 5d78de6 | 2004-11-05 20:35:44 +1100 | [diff] [blame] | 1901 | c->client_tty = 1; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1902 | } |
| 1903 | |
| 1904 | /* Transfer any environment variables from client to server */ |
Damien Miller | 3756dce | 2004-06-18 01:17:29 +1000 | [diff] [blame] | 1905 | if (options.num_send_env != 0 && env != NULL) { |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1906 | int i, j, matched; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1907 | char *name, *val; |
| 1908 | |
| 1909 | debug("Sending environment."); |
Damien Miller | 3756dce | 2004-06-18 01:17:29 +1000 | [diff] [blame] | 1910 | for (i = 0; env[i] != NULL; i++) { |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1911 | /* Split */ |
Damien Miller | 3756dce | 2004-06-18 01:17:29 +1000 | [diff] [blame] | 1912 | name = xstrdup(env[i]); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1913 | if ((val = strchr(name, '=')) == NULL) { |
Damien Miller | 0a0176e | 2005-11-05 15:07:59 +1100 | [diff] [blame] | 1914 | xfree(name); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1915 | continue; |
| 1916 | } |
| 1917 | *val++ = '\0'; |
| 1918 | |
| 1919 | matched = 0; |
| 1920 | for (j = 0; j < options.num_send_env; j++) { |
| 1921 | if (match_pattern(name, options.send_env[j])) { |
| 1922 | matched = 1; |
| 1923 | break; |
| 1924 | } |
| 1925 | } |
| 1926 | if (!matched) { |
| 1927 | debug3("Ignored env %s", name); |
Damien Miller | 0a0176e | 2005-11-05 15:07:59 +1100 | [diff] [blame] | 1928 | xfree(name); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1929 | continue; |
| 1930 | } |
| 1931 | |
| 1932 | debug("Sending env %s = %s", name, val); |
| 1933 | channel_request_start(id, "env", 0); |
| 1934 | packet_put_cstring(name); |
| 1935 | packet_put_cstring(val); |
| 1936 | packet_send(); |
Damien Miller | 0a0176e | 2005-11-05 15:07:59 +1100 | [diff] [blame] | 1937 | xfree(name); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1938 | } |
| 1939 | } |
| 1940 | |
| 1941 | len = buffer_len(cmd); |
| 1942 | if (len > 0) { |
| 1943 | if (len > 900) |
| 1944 | len = 900; |
| 1945 | if (want_subsystem) { |
| 1946 | debug("Sending subsystem: %.*s", len, (u_char*)buffer_ptr(cmd)); |
| 1947 | channel_request_start(id, "subsystem", subsys_repl != NULL); |
| 1948 | if (subsys_repl != NULL) { |
| 1949 | /* register callback for reply */ |
| 1950 | /* XXX we assume that client_loop has already been called */ |
| 1951 | dispatch_set(SSH2_MSG_CHANNEL_FAILURE, subsys_repl); |
| 1952 | dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, subsys_repl); |
| 1953 | } |
| 1954 | } else { |
| 1955 | debug("Sending command: %.*s", len, (u_char*)buffer_ptr(cmd)); |
| 1956 | channel_request_start(id, "exec", 0); |
| 1957 | } |
| 1958 | packet_put_string(buffer_ptr(cmd), buffer_len(cmd)); |
| 1959 | packet_send(); |
| 1960 | } else { |
| 1961 | channel_request_start(id, "shell", 0); |
| 1962 | packet_send(); |
| 1963 | } |
| 1964 | } |
| 1965 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1966 | static void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 1967 | client_init_dispatch_20(void) |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1968 | { |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 1969 | dispatch_init(&dispatch_protocol_error); |
Damien Miller | 2797f7f | 2002-04-23 21:09:44 +1000 | [diff] [blame] | 1970 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1971 | dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose); |
| 1972 | dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data); |
| 1973 | dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof); |
| 1974 | dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1975 | dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1976 | dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); |
| 1977 | dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 1978 | dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1979 | dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust); |
Damien Miller | c3fa407 | 2002-01-22 23:21:58 +1100 | [diff] [blame] | 1980 | dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request); |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 1981 | |
| 1982 | /* rekeying */ |
| 1983 | dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit); |
Damien Miller | 2797f7f | 2002-04-23 21:09:44 +1000 | [diff] [blame] | 1984 | |
| 1985 | /* global request reply messages */ |
| 1986 | dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply); |
| 1987 | dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1988 | } |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1989 | static void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 1990 | client_init_dispatch_13(void) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1991 | { |
| 1992 | dispatch_init(NULL); |
| 1993 | dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close); |
| 1994 | dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation); |
| 1995 | dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1996 | dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); |
| 1997 | dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); |
| 1998 | dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1999 | dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status); |
| 2000 | dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data); |
| 2001 | dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data); |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 2002 | |
| 2003 | dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ? |
Darren Tucker | 5dcdd21 | 2003-10-02 16:17:00 +1000 | [diff] [blame] | 2004 | &client_input_agent_open : &deny_input_open); |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 2005 | dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ? |
| 2006 | &x11_input_open : &deny_input_open); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2007 | } |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2008 | static void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 2009 | client_init_dispatch_15(void) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2010 | { |
| 2011 | client_init_dispatch_13(); |
| 2012 | dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof); |
| 2013 | dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose); |
| 2014 | } |
Ben Lindstrom | db47f38 | 2001-07-04 05:10:27 +0000 | [diff] [blame] | 2015 | static void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 2016 | client_init_dispatch(void) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2017 | { |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 2018 | if (compat20) |
| 2019 | client_init_dispatch_20(); |
| 2020 | else if (compat13) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2021 | client_init_dispatch_13(); |
| 2022 | else |
| 2023 | client_init_dispatch_15(); |
| 2024 | } |
Darren Tucker | 9a2c4cd | 2003-09-22 21:16:05 +1000 | [diff] [blame] | 2025 | |
| 2026 | /* client specific fatal cleanup */ |
| 2027 | void |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 2028 | cleanup_exit(int i) |
Darren Tucker | 9a2c4cd | 2003-09-22 21:16:05 +1000 | [diff] [blame] | 2029 | { |
Darren Tucker | 9a2c4cd | 2003-09-22 21:16:05 +1000 | [diff] [blame] | 2030 | leave_raw_mode(); |
| 2031 | leave_non_blocking(); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2032 | if (options.control_path != NULL && control_fd != -1) |
| 2033 | unlink(options.control_path); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 2034 | _exit(i); |
Darren Tucker | 9a2c4cd | 2003-09-22 21:16:05 +1000 | [diff] [blame] | 2035 | } |