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