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