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