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