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