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