djm@openbsd.org | 44732de | 2015-02-20 22:17:21 +0000 | [diff] [blame^] | 1 | /* $OpenBSD: clientloop.c,v 1.270 2015/02/20 22:17:21 djm Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 6 | * The main loop for the interactive session (client side). |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 7 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 8 | * As far as I am concerned, the code I have written for this software |
| 9 | * can be used freely for any purpose. Any derived versions of this |
| 10 | * software must be clearly marked as such, and if the derived work is |
| 11 | * incompatible with the protocol description in the RFC file, it must be |
| 12 | * called by a name other than "ssh" or "Secure Shell". |
| 13 | * |
| 14 | * |
| 15 | * Copyright (c) 1999 Theo de Raadt. All rights reserved. |
| 16 | * |
| 17 | * Redistribution and use in source and binary forms, with or without |
| 18 | * modification, are permitted provided that the following conditions |
| 19 | * are met: |
| 20 | * 1. Redistributions of source code must retain the above copyright |
| 21 | * notice, this list of conditions and the following disclaimer. |
| 22 | * 2. Redistributions in binary form must reproduce the above copyright |
| 23 | * notice, this list of conditions and the following disclaimer in the |
| 24 | * documentation and/or other materials provided with the distribution. |
| 25 | * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 27 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 28 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 29 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 30 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 31 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 32 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 33 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 34 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 35 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 | * |
| 37 | * |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 38 | * SSH2 support added by Markus Friedl. |
Ben Lindstrom | 4469723 | 2001-07-04 03:32:30 +0000 | [diff] [blame] | 39 | * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 40 | * |
| 41 | * Redistribution and use in source and binary forms, with or without |
| 42 | * modification, are permitted provided that the following conditions |
| 43 | * are met: |
| 44 | * 1. Redistributions of source code must retain the above copyright |
| 45 | * notice, this list of conditions and the following disclaimer. |
| 46 | * 2. Redistributions in binary form must reproduce the above copyright |
| 47 | * notice, this list of conditions and the following disclaimer in the |
| 48 | * documentation and/or other materials provided with the distribution. |
| 49 | * |
| 50 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 51 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 52 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 53 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 54 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 55 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 56 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 57 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 58 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 59 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 60 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 61 | |
| 62 | #include "includes.h" |
Damien Miller | 17e91c0 | 2006-03-15 11:28:34 +1100 | [diff] [blame] | 63 | |
deraadt@openbsd.org | 087266e | 2015-01-20 23:14:00 +0000 | [diff] [blame] | 64 | #include <sys/param.h> /* MIN MAX */ |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 65 | #include <sys/types.h> |
Damien Miller | 9aec919 | 2006-08-05 10:57:45 +1000 | [diff] [blame] | 66 | #include <sys/ioctl.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> |
deraadt@openbsd.org | 087266e | 2015-01-20 23:14:00 +0000 | [diff] [blame] | 88 | #include <limits.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 89 | |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 90 | #include "openbsd-compat/sys-queue.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 91 | #include "xmalloc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 92 | #include "ssh.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 93 | #include "ssh1.h" |
| 94 | #include "ssh2.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 95 | #include "packet.h" |
| 96 | #include "buffer.h" |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 97 | #include "compat.h" |
Ben Lindstrom | c763767 | 2001-06-09 00:36:26 +0000 | [diff] [blame] | 98 | #include "channels.h" |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 99 | #include "dispatch.h" |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 100 | #include "key.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 101 | #include "cipher.h" |
Ben Lindstrom | f28f634 | 2001-04-04 02:03:04 +0000 | [diff] [blame] | 102 | #include "kex.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 103 | #include "log.h" |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 104 | #include "misc.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 105 | #include "readconf.h" |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 106 | #include "clientloop.h" |
Damien Miller | aeb31d6 | 2005-12-13 19:29:36 +1100 | [diff] [blame] | 107 | #include "sshconnect.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 108 | #include "authfd.h" |
| 109 | #include "atomicio.h" |
Darren Tucker | 06f2bd8 | 2004-05-13 16:06:46 +1000 | [diff] [blame] | 110 | #include "sshpty.h" |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 111 | #include "match.h" |
| 112 | #include "msg.h" |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 113 | #include "roaming.h" |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 114 | #include "ssherr.h" |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 115 | #include "hostfile.h" |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 116 | |
| 117 | /* import options */ |
| 118 | extern Options options; |
| 119 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 120 | /* Flag indicating that stdin should be redirected from /dev/null. */ |
| 121 | extern int stdin_null_flag; |
| 122 | |
Damien Miller | d696551 | 2003-12-17 16:31:53 +1100 | [diff] [blame] | 123 | /* Flag indicating that no shell has been requested */ |
| 124 | extern int no_shell_flag; |
| 125 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 126 | /* Control socket */ |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 127 | extern int muxserver_sock; /* XXX use mux_client_cleanup() instead */ |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 128 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 129 | /* |
| 130 | * Name of the host we are connecting to. This is the name given on the |
| 131 | * command line, or the HostName specified for the user-supplied name in a |
| 132 | * configuration file. |
| 133 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 134 | extern char *host; |
| 135 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 136 | /* |
| 137 | * Flag to indicate that we have received a window change signal which has |
| 138 | * not yet been processed. This will cause a message indicating the new |
| 139 | * window size to be sent to the server a little later. This is volatile |
| 140 | * because this is updated in a signal handler. |
| 141 | */ |
Ben Lindstrom | 5e71c54 | 2001-12-06 16:48:14 +0000 | [diff] [blame] | 142 | static volatile sig_atomic_t received_window_change_signal = 0; |
| 143 | static volatile sig_atomic_t received_signal = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 144 | |
Damien Miller | 788f212 | 2005-11-05 15:14:59 +1100 | [diff] [blame] | 145 | /* Flag indicating whether the user's terminal is in non-blocking mode. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 146 | static int in_non_blocking_mode = 0; |
| 147 | |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 148 | /* Time when backgrounded control master using ControlPersist should exit */ |
| 149 | static time_t control_persist_exit_time = 0; |
| 150 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 151 | /* Common data for the client loop code. */ |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 152 | volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */ |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 153 | static int escape_char1; /* Escape character. (proto1 only) */ |
| 154 | static int escape_pending1; /* Last character was an escape (proto1 only) */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 155 | static int last_was_cr; /* Last character was a newline. */ |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 156 | static int exit_status; /* Used to store the command exit status. */ |
| 157 | static int stdin_eof; /* EOF has been encountered on stderr. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 158 | static Buffer stdin_buffer; /* Buffer for stdin data. */ |
| 159 | static Buffer stdout_buffer; /* Buffer for stdout data. */ |
| 160 | static Buffer stderr_buffer; /* Buffer for stderr data. */ |
Damien Miller | 1ab6a51 | 2010-06-26 10:02:24 +1000 | [diff] [blame] | 161 | static u_int buffer_high; /* Soft max buffer size. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 162 | static int connection_in; /* Connection to server (input). */ |
| 163 | static int connection_out; /* Connection to server (output). */ |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 164 | static int need_rekeying; /* Set to non-zero if rekeying is requested. */ |
Damien Miller | 1ab6a51 | 2010-06-26 10:02:24 +1000 | [diff] [blame] | 165 | static int session_closed; /* In SSH2: login session closed. */ |
| 166 | static int x11_refuse_time; /* If >0, refuse x11 opens after this time. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 167 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 168 | static void client_init_dispatch(void); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 169 | int session_ident = -1; |
| 170 | |
Darren Tucker | e32cf43 | 2010-01-08 16:51:40 +1100 | [diff] [blame] | 171 | int session_resumed = 0; |
| 172 | |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 173 | /* Track escape per proto2 channel */ |
| 174 | struct escape_filter_ctx { |
| 175 | int escape_pending; |
| 176 | int escape_char; |
| 177 | }; |
| 178 | |
| 179 | /* Context for channel confirmation replies */ |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 180 | struct channel_reply_ctx { |
| 181 | const char *request_type; |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 182 | int id; |
| 183 | enum confirm_action action; |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 184 | }; |
| 185 | |
Darren Tucker | 9f407c4 | 2008-06-13 04:50:27 +1000 | [diff] [blame] | 186 | /* Global request success/failure callbacks */ |
| 187 | struct global_confirm { |
| 188 | TAILQ_ENTRY(global_confirm) entry; |
| 189 | global_confirm_cb *cb; |
| 190 | void *ctx; |
| 191 | int ref_count; |
| 192 | }; |
| 193 | TAILQ_HEAD(global_confirms, global_confirm); |
| 194 | static struct global_confirms global_confirms = |
| 195 | TAILQ_HEAD_INITIALIZER(global_confirms); |
| 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) { |
deraadt@openbsd.org | 087266e | 2015-01-20 23:14:00 +0000 | [diff] [blame] | 344 | xauthdir = xmalloc(PATH_MAX); |
| 345 | xauthfile = xmalloc(PATH_MAX); |
| 346 | mktemp_proto(xauthdir, PATH_MAX); |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 347 | if (mkdtemp(xauthdir) != NULL) { |
| 348 | do_unlink = 1; |
deraadt@openbsd.org | 087266e | 2015-01-20 23:14:00 +0000 | [diff] [blame] | 349 | snprintf(xauthfile, PATH_MAX, "%s/xauthfile", |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 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 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 541 | static int |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 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) |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 547 | return 0; |
Darren Tucker | 9f407c4 | 2008-06-13 04:50:27 +1000 | [diff] [blame] | 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); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 557 | return 0; |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | static void |
| 561 | server_alive_check(void) |
| 562 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 563 | if (packet_inc_alive_timeouts() > options.server_alive_count_max) { |
Damien Miller | b73b6fd | 2011-01-11 17:18:56 +1100 | [diff] [blame] | 564 | logit("Timeout, server %s not responding.", host); |
Damien Miller | 985a448 | 2006-10-24 03:02:41 +1000 | [diff] [blame] | 565 | cleanup_exit(255); |
| 566 | } |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 567 | packet_start(SSH2_MSG_GLOBAL_REQUEST); |
| 568 | packet_put_cstring("keepalive@openssh.com"); |
| 569 | packet_put_char(1); /* boolean: want reply */ |
| 570 | packet_send(); |
Darren Tucker | 9f407c4 | 2008-06-13 04:50:27 +1000 | [diff] [blame] | 571 | /* Insert an empty placeholder to maintain ordering */ |
| 572 | client_register_global_confirm(NULL, NULL); |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 573 | } |
| 574 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 575 | /* |
| 576 | * Waits until the client can do something (some data becomes available on |
| 577 | * one of the file descriptors). |
| 578 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 579 | static void |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 580 | client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp, |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 581 | int *maxfdp, u_int *nallocp, int rekeying) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 582 | { |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 583 | struct timeval tv, *tvp; |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 584 | int timeout_secs; |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 585 | time_t minwait_secs = 0, server_alive_time = 0, now = monotime(); |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 586 | int ret; |
| 587 | |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 588 | /* Add any selections by the channel mechanism. */ |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 589 | channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, |
| 590 | &minwait_secs, rekeying); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 591 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 592 | if (!compat20) { |
| 593 | /* Read from the connection, unless our buffers are full. */ |
| 594 | if (buffer_len(&stdout_buffer) < buffer_high && |
| 595 | buffer_len(&stderr_buffer) < buffer_high && |
| 596 | channel_not_very_much_buffered_data()) |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 597 | FD_SET(connection_in, *readsetp); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 598 | /* |
| 599 | * Read from stdin, unless we have seen EOF or have very much |
| 600 | * buffered data to send to the server. |
| 601 | */ |
| 602 | if (!stdin_eof && packet_not_very_much_data_to_write()) |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 603 | FD_SET(fileno(stdin), *readsetp); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 604 | |
| 605 | /* Select stdout/stderr if have data in buffer. */ |
| 606 | if (buffer_len(&stdout_buffer) > 0) |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 607 | FD_SET(fileno(stdout), *writesetp); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 608 | if (buffer_len(&stderr_buffer) > 0) |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 609 | FD_SET(fileno(stderr), *writesetp); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 610 | } else { |
Ben Lindstrom | c8b3f47 | 2001-05-17 03:19:40 +0000 | [diff] [blame] | 611 | /* channel_prepare_select could have closed the last channel */ |
Damien Miller | 164a7f4 | 2001-10-12 11:36:09 +1000 | [diff] [blame] | 612 | if (session_closed && !channel_still_open() && |
| 613 | !packet_have_data_to_write()) { |
| 614 | /* clear mask since we did not call select() */ |
Damien Miller | 79faeff | 2001-11-12 11:06:32 +1100 | [diff] [blame] | 615 | memset(*readsetp, 0, *nallocp); |
| 616 | memset(*writesetp, 0, *nallocp); |
Damien Miller | 164a7f4 | 2001-10-12 11:36:09 +1000 | [diff] [blame] | 617 | return; |
Ben Lindstrom | c8b3f47 | 2001-05-17 03:19:40 +0000 | [diff] [blame] | 618 | } else { |
| 619 | FD_SET(connection_in, *readsetp); |
| 620 | } |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 621 | } |
| 622 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 623 | /* Select server connection if have data to write to the server. */ |
| 624 | if (packet_have_data_to_write()) |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 625 | FD_SET(connection_out, *writesetp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 626 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 627 | /* |
| 628 | * Wait for something to happen. This will suspend the process until |
| 629 | * some selected descriptor can be read, written, or has some other |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 630 | * event pending, or a timeout expires. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 631 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 632 | |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 633 | timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */ |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 634 | if (options.server_alive_interval > 0 && compat20) { |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 635 | timeout_secs = options.server_alive_interval; |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 636 | server_alive_time = now + options.server_alive_interval; |
| 637 | } |
| 638 | if (options.rekey_interval > 0 && compat20 && !rekeying) |
| 639 | timeout_secs = MIN(timeout_secs, packet_get_rekey_timeout()); |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 640 | set_control_persist_exit_time(); |
| 641 | if (control_persist_exit_time > 0) { |
| 642 | timeout_secs = MIN(timeout_secs, |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 643 | control_persist_exit_time - now); |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 644 | if (timeout_secs < 0) |
| 645 | timeout_secs = 0; |
| 646 | } |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 647 | if (minwait_secs != 0) |
| 648 | timeout_secs = MIN(timeout_secs, (int)minwait_secs); |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 649 | if (timeout_secs == INT_MAX) |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 650 | tvp = NULL; |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 651 | else { |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 652 | tv.tv_sec = timeout_secs; |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 653 | tv.tv_usec = 0; |
| 654 | tvp = &tv; |
| 655 | } |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 656 | |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 657 | ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp); |
| 658 | if (ret < 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 659 | char buf[100]; |
Ben Lindstrom | f945251 | 2001-02-15 03:12:08 +0000 | [diff] [blame] | 660 | |
| 661 | /* |
| 662 | * We have to clear the select masks, because we return. |
| 663 | * We have to return, because the mainloop checks for the flags |
| 664 | * set by the signal handlers. |
| 665 | */ |
Damien Miller | 79faeff | 2001-11-12 11:06:32 +1100 | [diff] [blame] | 666 | memset(*readsetp, 0, *nallocp); |
| 667 | memset(*writesetp, 0, *nallocp); |
Ben Lindstrom | f945251 | 2001-02-15 03:12:08 +0000 | [diff] [blame] | 668 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 669 | if (errno == EINTR) |
| 670 | return; |
| 671 | /* Note: we might still have data in the buffers. */ |
| 672 | snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno)); |
| 673 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 674 | quit_pending = 1; |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 675 | } else if (ret == 0) { |
| 676 | /* |
| 677 | * Timeout. Could have been either keepalive or rekeying. |
| 678 | * Keepalive we check here, rekeying is checked in clientloop. |
| 679 | */ |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 680 | if (server_alive_time != 0 && server_alive_time <= monotime()) |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 681 | server_alive_check(); |
| 682 | } |
| 683 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 684 | } |
| 685 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 686 | static void |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 687 | client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 688 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 689 | /* Flush stdout and stderr buffers. */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 690 | if (buffer_len(bout) > 0) |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 691 | atomicio(vwrite, fileno(stdout), buffer_ptr(bout), |
| 692 | buffer_len(bout)); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 693 | if (buffer_len(berr) > 0) |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 694 | atomicio(vwrite, fileno(stderr), buffer_ptr(berr), |
| 695 | buffer_len(berr)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 696 | |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 697 | leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 698 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 699 | /* |
| 700 | * Free (and clear) the buffer to reduce the amount of data that gets |
| 701 | * written to swap. |
| 702 | */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 703 | buffer_free(bin); |
| 704 | buffer_free(bout); |
| 705 | buffer_free(berr); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 706 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 707 | /* Send the suspend signal to the program itself. */ |
| 708 | kill(getpid(), SIGTSTP); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 709 | |
Darren Tucker | 5d78de6 | 2004-11-05 20:35:44 +1100 | [diff] [blame] | 710 | /* Reset window sizes in case they have changed */ |
| 711 | received_window_change_signal = 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 712 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 713 | /* OK, we have been continued by the user. Reinitialize buffers. */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 714 | buffer_init(bin); |
| 715 | buffer_init(bout); |
| 716 | buffer_init(berr); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 717 | |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 718 | enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 719 | } |
| 720 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 721 | static void |
Damien Miller | 90fdfaf | 2006-03-26 14:25:37 +1100 | [diff] [blame] | 722 | client_process_net_input(fd_set *readset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 723 | { |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 724 | int len, cont = 0; |
Darren Tucker | 86e30a0 | 2009-08-28 11:21:06 +1000 | [diff] [blame] | 725 | char buf[SSH_IOBUFSZ]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 726 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 727 | /* |
| 728 | * Read input from the server, and add any such data to the buffer of |
| 729 | * the packet subsystem. |
| 730 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 731 | if (FD_ISSET(connection_in, readset)) { |
| 732 | /* Read as much as possible. */ |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 733 | len = roaming_read(connection_in, buf, sizeof(buf), &cont); |
| 734 | if (len == 0 && cont == 0) { |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 735 | /* |
| 736 | * Received EOF. The remote host has closed the |
| 737 | * connection. |
| 738 | */ |
| 739 | snprintf(buf, sizeof buf, |
| 740 | "Connection to %.300s closed by remote host.\r\n", |
| 741 | host); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 742 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 743 | quit_pending = 1; |
| 744 | return; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 745 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 746 | /* |
| 747 | * There is a kernel bug on Solaris that causes select to |
| 748 | * sometimes wake up even though there is no data available. |
| 749 | */ |
Damien Miller | d8968ad | 2008-07-04 23:10:49 +1000 | [diff] [blame] | 750 | if (len < 0 && |
| 751 | (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 752 | len = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 753 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 754 | if (len < 0) { |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 755 | /* |
| 756 | * An error has encountered. Perhaps there is a |
| 757 | * network problem. |
| 758 | */ |
| 759 | snprintf(buf, sizeof buf, |
| 760 | "Read from remote host %.300s: %.100s\r\n", |
| 761 | host, strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 762 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 763 | quit_pending = 1; |
| 764 | return; |
| 765 | } |
| 766 | packet_process_incoming(buf, len); |
| 767 | } |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 768 | } |
| 769 | |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 770 | static void |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 771 | client_status_confirm(int type, Channel *c, void *ctx) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 772 | { |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 773 | struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx; |
| 774 | char errmsg[256]; |
| 775 | int tochan; |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 776 | |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 777 | /* |
| 778 | * If a TTY was explicitly requested, then a failure to allocate |
| 779 | * one is fatal. |
| 780 | */ |
| 781 | if (cr->action == CONFIRM_TTY && |
| 782 | (options.request_tty == REQUEST_TTY_FORCE || |
| 783 | options.request_tty == REQUEST_TTY_YES)) |
| 784 | cr->action = CONFIRM_CLOSE; |
| 785 | |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 786 | /* XXX supress on mux _client_ quietmode */ |
| 787 | tochan = options.log_level >= SYSLOG_LEVEL_ERROR && |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 788 | c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 789 | |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 790 | if (type == SSH2_MSG_CHANNEL_SUCCESS) { |
| 791 | debug2("%s request accepted on channel %d", |
| 792 | cr->request_type, c->self); |
| 793 | } else if (type == SSH2_MSG_CHANNEL_FAILURE) { |
| 794 | if (tochan) { |
| 795 | snprintf(errmsg, sizeof(errmsg), |
| 796 | "%s request failed\r\n", cr->request_type); |
| 797 | } else { |
| 798 | snprintf(errmsg, sizeof(errmsg), |
| 799 | "%s request failed on channel %d", |
| 800 | cr->request_type, c->self); |
| 801 | } |
| 802 | /* If error occurred on primary session channel, then exit */ |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 803 | if (cr->action == CONFIRM_CLOSE && c->self == session_ident) |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 804 | fatal("%s", errmsg); |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 805 | /* |
| 806 | * If error occurred on mux client, append to |
| 807 | * their stderr. |
| 808 | */ |
| 809 | if (tochan) { |
| 810 | buffer_append(&c->extended, errmsg, |
| 811 | strlen(errmsg)); |
| 812 | } else |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 813 | error("%s", errmsg); |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 814 | if (cr->action == CONFIRM_TTY) { |
| 815 | /* |
| 816 | * If a TTY allocation error occurred, then arrange |
| 817 | * for the correct TTY to leave raw mode. |
| 818 | */ |
| 819 | if (c->self == session_ident) |
| 820 | leave_raw_mode(0); |
| 821 | else |
| 822 | mux_tty_alloc_failed(c); |
| 823 | } else if (cr->action == CONFIRM_CLOSE) { |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 824 | chan_read_failed(c); |
| 825 | chan_write_failed(c); |
| 826 | } |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 827 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 828 | free(cr); |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 829 | } |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 830 | |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 831 | static void |
| 832 | client_abandon_status_confirm(Channel *c, void *ctx) |
| 833 | { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 834 | free(ctx); |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 835 | } |
| 836 | |
Damien Miller | 6d7b437 | 2011-06-23 08:31:57 +1000 | [diff] [blame] | 837 | void |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 838 | client_expect_confirm(int id, const char *request, |
| 839 | enum confirm_action action) |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 840 | { |
Damien Miller | 6c81fee | 2013-11-08 12:19:55 +1100 | [diff] [blame] | 841 | struct channel_reply_ctx *cr = xcalloc(1, sizeof(*cr)); |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 842 | |
| 843 | cr->request_type = request; |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 844 | cr->action = action; |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 845 | |
| 846 | channel_register_status_confirm(id, client_status_confirm, |
| 847 | client_abandon_status_confirm, cr); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 848 | } |
| 849 | |
Darren Tucker | 9f407c4 | 2008-06-13 04:50:27 +1000 | [diff] [blame] | 850 | void |
| 851 | client_register_global_confirm(global_confirm_cb *cb, void *ctx) |
| 852 | { |
Damien Miller | b9d3bee | 2008-07-16 22:40:52 +1000 | [diff] [blame] | 853 | struct global_confirm *gc, *last_gc; |
Darren Tucker | 9f407c4 | 2008-06-13 04:50:27 +1000 | [diff] [blame] | 854 | |
| 855 | /* Coalesce identical callbacks */ |
Damien Miller | b9d3bee | 2008-07-16 22:40:52 +1000 | [diff] [blame] | 856 | last_gc = TAILQ_LAST(&global_confirms, global_confirms); |
| 857 | if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) { |
| 858 | if (++last_gc->ref_count >= INT_MAX) |
| 859 | fatal("%s: last_gc->ref_count = %d", |
| 860 | __func__, last_gc->ref_count); |
Darren Tucker | 9f407c4 | 2008-06-13 04:50:27 +1000 | [diff] [blame] | 861 | return; |
| 862 | } |
| 863 | |
Damien Miller | 6c81fee | 2013-11-08 12:19:55 +1100 | [diff] [blame] | 864 | gc = xcalloc(1, sizeof(*gc)); |
Darren Tucker | 9f407c4 | 2008-06-13 04:50:27 +1000 | [diff] [blame] | 865 | gc->cb = cb; |
| 866 | gc->ctx = ctx; |
| 867 | gc->ref_count = 1; |
| 868 | TAILQ_INSERT_TAIL(&global_confirms, gc, entry); |
| 869 | } |
| 870 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 871 | static void |
Ben Lindstrom | 681d932 | 2002-03-22 03:53:00 +0000 | [diff] [blame] | 872 | process_cmdline(void) |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 873 | { |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 874 | void (*handler)(int); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 875 | char *s, *cmd; |
| 876 | int ok, delete = 0, local = 0, remote = 0, dynamic = 0; |
| 877 | struct Forward fwd; |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 878 | |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 879 | memset(&fwd, 0, sizeof(fwd)); |
Darren Tucker | 23ae8ca | 2007-12-02 23:12:30 +1100 | [diff] [blame] | 880 | |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 881 | leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); |
Ben Lindstrom | 5a6abda | 2002-06-09 19:41:48 +0000 | [diff] [blame] | 882 | handler = signal(SIGINT, SIG_IGN); |
Ben Lindstrom | 681d932 | 2002-03-22 03:53:00 +0000 | [diff] [blame] | 883 | cmd = s = read_passphrase("\r\nssh> ", RP_ECHO); |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 884 | if (s == NULL) |
| 885 | goto out; |
Damien Miller | fdb2306 | 2013-11-21 13:57:15 +1100 | [diff] [blame] | 886 | while (isspace((u_char)*s)) |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 887 | s++; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 888 | if (*s == '-') |
| 889 | s++; /* Skip cmdline '-', if any */ |
Darren Tucker | e167582 | 2004-05-24 10:13:07 +1000 | [diff] [blame] | 890 | if (*s == '\0') |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 891 | goto out; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 892 | |
Darren Tucker | 1973c88 | 2004-05-24 10:34:36 +1000 | [diff] [blame] | 893 | if (*s == 'h' || *s == 'H' || *s == '?') { |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 894 | logit("Commands:"); |
Damien Miller | 4302095 | 2006-07-10 20:16:12 +1000 | [diff] [blame] | 895 | logit(" -L[bind_address:]port:host:hostport " |
| 896 | "Request local forward"); |
| 897 | logit(" -R[bind_address:]port:host:hostport " |
| 898 | "Request remote forward"); |
Damien Miller | 0164cb8 | 2008-11-05 16:30:31 +1100 | [diff] [blame] | 899 | logit(" -D[bind_address:]port " |
| 900 | "Request dynamic forward"); |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 901 | logit(" -KL[bind_address:]port " |
| 902 | "Cancel local forward"); |
Damien Miller | 57e8ad3 | 2006-07-10 20:20:52 +1000 | [diff] [blame] | 903 | logit(" -KR[bind_address:]port " |
Damien Miller | 4302095 | 2006-07-10 20:16:12 +1000 | [diff] [blame] | 904 | "Cancel remote forward"); |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 905 | logit(" -KD[bind_address:]port " |
| 906 | "Cancel dynamic forward"); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 907 | if (!options.permit_local_command) |
| 908 | goto out; |
Damien Miller | 4302095 | 2006-07-10 20:16:12 +1000 | [diff] [blame] | 909 | logit(" !args " |
| 910 | "Execute local command"); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 911 | goto out; |
| 912 | } |
| 913 | |
| 914 | if (*s == '!' && options.permit_local_command) { |
| 915 | s++; |
| 916 | ssh_local_cmd(s); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 917 | goto out; |
| 918 | } |
| 919 | |
| 920 | if (*s == 'K') { |
| 921 | delete = 1; |
| 922 | s++; |
| 923 | } |
Damien Miller | 0164cb8 | 2008-11-05 16:30:31 +1100 | [diff] [blame] | 924 | if (*s == 'L') |
| 925 | local = 1; |
| 926 | else if (*s == 'R') |
| 927 | remote = 1; |
| 928 | else if (*s == 'D') |
| 929 | dynamic = 1; |
| 930 | else { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 931 | logit("Invalid command."); |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 932 | goto out; |
| 933 | } |
Damien Miller | 0164cb8 | 2008-11-05 16:30:31 +1100 | [diff] [blame] | 934 | |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 935 | if (delete && !compat20) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 936 | logit("Not supported for SSH protocol version 1."); |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 937 | goto out; |
| 938 | } |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 939 | |
Damien Miller | fdb2306 | 2013-11-21 13:57:15 +1100 | [diff] [blame] | 940 | while (isspace((u_char)*++s)) |
Darren Tucker | 03b1cdb | 2007-03-21 20:46:03 +1100 | [diff] [blame] | 941 | ; |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 942 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 943 | /* XXX update list of forwards in options */ |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 944 | if (delete) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 945 | /* We pass 1 for dynamicfwd to restrict to 1 or 2 fields. */ |
| 946 | if (!parse_forward(&fwd, s, 1, 0)) { |
| 947 | logit("Bad forwarding close specification."); |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 948 | goto out; |
| 949 | } |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 950 | if (remote) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 951 | ok = channel_request_rforward_cancel(&fwd) == 0; |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 952 | else if (dynamic) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 953 | ok = channel_cancel_lport_listener(&fwd, |
| 954 | 0, &options.fwd_opts) > 0; |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 955 | else |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 956 | ok = channel_cancel_lport_listener(&fwd, |
| 957 | CHANNEL_CANCEL_PORT_STATIC, |
| 958 | &options.fwd_opts) > 0; |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 959 | if (!ok) { |
| 960 | logit("Unkown port forwarding."); |
| 961 | goto out; |
| 962 | } |
| 963 | logit("Canceled forwarding."); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 964 | } else { |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 965 | if (!parse_forward(&fwd, s, dynamic, remote)) { |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 966 | logit("Bad forwarding specification."); |
| 967 | goto out; |
| 968 | } |
Damien Miller | 0164cb8 | 2008-11-05 16:30:31 +1100 | [diff] [blame] | 969 | if (local || dynamic) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 970 | if (!channel_setup_local_fwd_listener(&fwd, |
| 971 | &options.fwd_opts)) { |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 972 | logit("Port forwarding failed."); |
| 973 | goto out; |
| 974 | } |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 975 | } else { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 976 | if (channel_request_remote_forwarding(&fwd) < 0) { |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 977 | logit("Port forwarding failed."); |
| 978 | goto out; |
| 979 | } |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 980 | } |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 981 | logit("Forwarding port."); |
| 982 | } |
| 983 | |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 984 | out: |
| 985 | signal(SIGINT, handler); |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 986 | enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 987 | free(cmd); |
| 988 | free(fwd.listen_host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 989 | free(fwd.listen_path); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 990 | free(fwd.connect_host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 991 | free(fwd.connect_path); |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Darren Tucker | 92a39cf | 2012-09-07 11:20:20 +1000 | [diff] [blame] | 994 | /* reasons to suppress output of an escape command in help output */ |
| 995 | #define SUPPRESS_NEVER 0 /* never suppress, always show */ |
| 996 | #define SUPPRESS_PROTO1 1 /* don't show in protocol 1 sessions */ |
| 997 | #define SUPPRESS_MUXCLIENT 2 /* don't show in mux client sessions */ |
| 998 | #define SUPPRESS_MUXMASTER 4 /* don't show in mux master sessions */ |
| 999 | #define SUPPRESS_SYSLOG 8 /* don't show when logging to syslog */ |
| 1000 | struct escape_help_text { |
| 1001 | const char *cmd; |
| 1002 | const char *text; |
| 1003 | unsigned int flags; |
| 1004 | }; |
| 1005 | static struct escape_help_text esc_txt[] = { |
| 1006 | {".", "terminate session", SUPPRESS_MUXMASTER}, |
| 1007 | {".", "terminate connection (and any multiplexed sessions)", |
| 1008 | SUPPRESS_MUXCLIENT}, |
| 1009 | {"B", "send a BREAK to the remote system", SUPPRESS_PROTO1}, |
| 1010 | {"C", "open a command line", SUPPRESS_MUXCLIENT}, |
| 1011 | {"R", "request rekey", SUPPRESS_PROTO1}, |
Darren Tucker | ca0d0fd | 2012-09-07 11:22:24 +1000 | [diff] [blame] | 1012 | {"V/v", "decrease/increase verbosity (LogLevel)", SUPPRESS_MUXCLIENT}, |
Darren Tucker | 92a39cf | 2012-09-07 11:20:20 +1000 | [diff] [blame] | 1013 | {"^Z", "suspend ssh", SUPPRESS_MUXCLIENT}, |
| 1014 | {"#", "list forwarded connections", SUPPRESS_NEVER}, |
| 1015 | {"&", "background ssh (when waiting for connections to terminate)", |
| 1016 | SUPPRESS_MUXCLIENT}, |
| 1017 | {"?", "this message", SUPPRESS_NEVER}, |
| 1018 | }; |
| 1019 | |
| 1020 | static void |
| 1021 | print_escape_help(Buffer *b, int escape_char, int protocol2, int mux_client, |
| 1022 | int using_stderr) |
| 1023 | { |
| 1024 | unsigned int i, suppress_flags; |
| 1025 | char string[1024]; |
| 1026 | |
| 1027 | snprintf(string, sizeof string, "%c?\r\n" |
| 1028 | "Supported escape sequences:\r\n", escape_char); |
| 1029 | buffer_append(b, string, strlen(string)); |
| 1030 | |
| 1031 | suppress_flags = (protocol2 ? 0 : SUPPRESS_PROTO1) | |
| 1032 | (mux_client ? SUPPRESS_MUXCLIENT : 0) | |
| 1033 | (mux_client ? 0 : SUPPRESS_MUXMASTER) | |
| 1034 | (using_stderr ? 0 : SUPPRESS_SYSLOG); |
| 1035 | |
| 1036 | for (i = 0; i < sizeof(esc_txt)/sizeof(esc_txt[0]); i++) { |
| 1037 | if (esc_txt[i].flags & suppress_flags) |
| 1038 | continue; |
Darren Tucker | ca0d0fd | 2012-09-07 11:22:24 +1000 | [diff] [blame] | 1039 | snprintf(string, sizeof string, " %c%-3s - %s\r\n", |
Darren Tucker | 92a39cf | 2012-09-07 11:20:20 +1000 | [diff] [blame] | 1040 | escape_char, esc_txt[i].cmd, esc_txt[i].text); |
| 1041 | buffer_append(b, string, strlen(string)); |
| 1042 | } |
| 1043 | |
| 1044 | snprintf(string, sizeof string, |
Darren Tucker | ca0d0fd | 2012-09-07 11:22:24 +1000 | [diff] [blame] | 1045 | " %c%c - send the escape character by typing it twice\r\n" |
Darren Tucker | 92a39cf | 2012-09-07 11:20:20 +1000 | [diff] [blame] | 1046 | "(Note that escapes are only recognized immediately after " |
| 1047 | "newline.)\r\n", escape_char, escape_char); |
| 1048 | buffer_append(b, string, strlen(string)); |
| 1049 | } |
| 1050 | |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1051 | /* |
| 1052 | * Process the characters one by one, call with c==NULL for proto1 case. |
| 1053 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1054 | static int |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1055 | process_escapes(Channel *c, Buffer *bin, Buffer *bout, Buffer *berr, |
| 1056 | char *buf, int len) |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1057 | { |
| 1058 | char string[1024]; |
| 1059 | pid_t pid; |
| 1060 | int bytes = 0; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1061 | u_int i; |
| 1062 | u_char ch; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1063 | char *s; |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1064 | int *escape_pendingp, escape_char; |
| 1065 | struct escape_filter_ctx *efc; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1066 | |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1067 | if (c == NULL) { |
| 1068 | escape_pendingp = &escape_pending1; |
| 1069 | escape_char = escape_char1; |
| 1070 | } else { |
| 1071 | if (c->filter_ctx == NULL) |
| 1072 | return 0; |
| 1073 | efc = (struct escape_filter_ctx *)c->filter_ctx; |
| 1074 | escape_pendingp = &efc->escape_pending; |
| 1075 | escape_char = efc->escape_char; |
| 1076 | } |
| 1077 | |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 1078 | if (len <= 0) |
| 1079 | return (0); |
| 1080 | |
| 1081 | for (i = 0; i < (u_int)len; i++) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1082 | /* Get one character at a time. */ |
| 1083 | ch = buf[i]; |
| 1084 | |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1085 | if (*escape_pendingp) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1086 | /* We have previously seen an escape character. */ |
| 1087 | /* Clear the flag now. */ |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1088 | *escape_pendingp = 0; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1089 | |
| 1090 | /* Process the escaped character. */ |
| 1091 | switch (ch) { |
| 1092 | case '.': |
| 1093 | /* Terminate the connection. */ |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1094 | snprintf(string, sizeof string, "%c.\r\n", |
| 1095 | escape_char); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1096 | buffer_append(berr, string, strlen(string)); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1097 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1098 | if (c && c->ctl_chan != -1) { |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1099 | chan_read_failed(c); |
| 1100 | chan_write_failed(c); |
Darren Tucker | ea8342c | 2013-06-06 08:11:40 +1000 | [diff] [blame] | 1101 | if (c->detach_user) |
| 1102 | c->detach_user(c->self, NULL); |
Damien Miller | 3618709 | 2013-06-10 13:07:11 +1000 | [diff] [blame] | 1103 | c->type = SSH_CHANNEL_ABANDONED; |
| 1104 | buffer_clear(&c->input); |
| 1105 | chan_ibuf_empty(c); |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1106 | return 0; |
| 1107 | } else |
| 1108 | quit_pending = 1; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1109 | return -1; |
| 1110 | |
| 1111 | case 'Z' - 64: |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1112 | /* XXX support this for mux clients */ |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1113 | if (c && c->ctl_chan != -1) { |
Darren Tucker | f111d40 | 2012-09-07 11:21:42 +1000 | [diff] [blame] | 1114 | char b[16]; |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1115 | noescape: |
Darren Tucker | f111d40 | 2012-09-07 11:21:42 +1000 | [diff] [blame] | 1116 | if (ch == 'Z' - 64) |
| 1117 | snprintf(b, sizeof b, "^Z"); |
| 1118 | else |
| 1119 | snprintf(b, sizeof b, "%c", ch); |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1120 | snprintf(string, sizeof string, |
Darren Tucker | f111d40 | 2012-09-07 11:21:42 +1000 | [diff] [blame] | 1121 | "%c%s escape not available to " |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1122 | "multiplexed sessions\r\n", |
Darren Tucker | f111d40 | 2012-09-07 11:21:42 +1000 | [diff] [blame] | 1123 | escape_char, b); |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1124 | buffer_append(berr, string, |
| 1125 | strlen(string)); |
| 1126 | continue; |
| 1127 | } |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 1128 | /* Suspend the program. Inform the user */ |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1129 | snprintf(string, sizeof string, |
| 1130 | "%c^Z [suspend ssh]\r\n", escape_char); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1131 | buffer_append(berr, string, strlen(string)); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1132 | |
| 1133 | /* Restore terminal modes and suspend. */ |
| 1134 | client_suspend_self(bin, bout, berr); |
| 1135 | |
| 1136 | /* We have been continued. */ |
| 1137 | continue; |
| 1138 | |
Damien Miller | 54c4598 | 2003-05-15 10:20:13 +1000 | [diff] [blame] | 1139 | case 'B': |
| 1140 | if (compat20) { |
| 1141 | snprintf(string, sizeof string, |
| 1142 | "%cB\r\n", escape_char); |
| 1143 | buffer_append(berr, string, |
| 1144 | strlen(string)); |
Damien Miller | 7018252 | 2013-09-14 09:49:19 +1000 | [diff] [blame] | 1145 | channel_request_start(c->self, |
Damien Miller | 54c4598 | 2003-05-15 10:20:13 +1000 | [diff] [blame] | 1146 | "break", 0); |
| 1147 | packet_put_int(1000); |
| 1148 | packet_send(); |
| 1149 | } |
| 1150 | continue; |
| 1151 | |
Ben Lindstrom | f28f634 | 2001-04-04 02:03:04 +0000 | [diff] [blame] | 1152 | case 'R': |
Ben Lindstrom | 11bd899 | 2001-04-05 23:34:29 +0000 | [diff] [blame] | 1153 | if (compat20) { |
| 1154 | if (datafellows & SSH_BUG_NOREKEY) |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 1155 | logit("Server does not " |
| 1156 | "support re-keying"); |
Ben Lindstrom | 11bd899 | 2001-04-05 23:34:29 +0000 | [diff] [blame] | 1157 | else |
| 1158 | need_rekeying = 1; |
| 1159 | } |
Ben Lindstrom | f28f634 | 2001-04-04 02:03:04 +0000 | [diff] [blame] | 1160 | continue; |
| 1161 | |
Darren Tucker | 50a48d0 | 2012-09-06 21:25:37 +1000 | [diff] [blame] | 1162 | case 'V': |
| 1163 | /* FALLTHROUGH */ |
| 1164 | case 'v': |
| 1165 | if (c && c->ctl_chan != -1) |
| 1166 | goto noescape; |
| 1167 | if (!log_is_on_stderr()) { |
| 1168 | snprintf(string, sizeof string, |
| 1169 | "%c%c [Logging to syslog]\r\n", |
| 1170 | escape_char, ch); |
| 1171 | buffer_append(berr, string, |
| 1172 | strlen(string)); |
| 1173 | continue; |
| 1174 | } |
| 1175 | if (ch == 'V' && options.log_level > |
| 1176 | SYSLOG_LEVEL_QUIET) |
| 1177 | log_change_level(--options.log_level); |
| 1178 | if (ch == 'v' && options.log_level < |
| 1179 | SYSLOG_LEVEL_DEBUG3) |
| 1180 | log_change_level(++options.log_level); |
| 1181 | snprintf(string, sizeof string, |
| 1182 | "%c%c [LogLevel %s]\r\n", escape_char, ch, |
| 1183 | log_level_name(options.log_level)); |
| 1184 | buffer_append(berr, string, strlen(string)); |
| 1185 | continue; |
| 1186 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1187 | case '&': |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1188 | if (c && c->ctl_chan != -1) |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1189 | goto noescape; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1190 | /* |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 1191 | * Detach the program (continue to serve |
| 1192 | * connections, but put in background and no |
| 1193 | * more new connections). |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1194 | */ |
Damien Miller | 96507ef | 2001-11-12 10:52:25 +1100 | [diff] [blame] | 1195 | /* Restore tty modes. */ |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 1196 | leave_raw_mode( |
| 1197 | options.request_tty == REQUEST_TTY_FORCE); |
Damien Miller | 96507ef | 2001-11-12 10:52:25 +1100 | [diff] [blame] | 1198 | |
| 1199 | /* Stop listening for new connections. */ |
| 1200 | channel_stop_listening(); |
| 1201 | |
| 1202 | snprintf(string, sizeof string, |
| 1203 | "%c& [backgrounded]\n", escape_char); |
| 1204 | buffer_append(berr, string, strlen(string)); |
| 1205 | |
| 1206 | /* Fork into background. */ |
| 1207 | pid = fork(); |
| 1208 | if (pid < 0) { |
| 1209 | error("fork: %.100s", strerror(errno)); |
| 1210 | continue; |
| 1211 | } |
| 1212 | if (pid != 0) { /* This is the parent. */ |
| 1213 | /* The parent just exits. */ |
| 1214 | exit(0); |
| 1215 | } |
| 1216 | /* The child continues serving connections. */ |
| 1217 | if (compat20) { |
| 1218 | buffer_append(bin, "\004", 1); |
| 1219 | /* fake EOF on stdin */ |
| 1220 | return -1; |
| 1221 | } else if (!stdin_eof) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1222 | /* |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 1223 | * Sending SSH_CMSG_EOF alone does not |
| 1224 | * always appear to be enough. So we |
| 1225 | * try to send an EOF character first. |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1226 | */ |
| 1227 | packet_start(SSH_CMSG_STDIN_DATA); |
| 1228 | packet_put_string("\004", 1); |
| 1229 | packet_send(); |
| 1230 | /* Close stdin. */ |
| 1231 | stdin_eof = 1; |
| 1232 | if (buffer_len(bin) == 0) { |
| 1233 | packet_start(SSH_CMSG_EOF); |
| 1234 | packet_send(); |
| 1235 | } |
| 1236 | } |
Damien Miller | 96507ef | 2001-11-12 10:52:25 +1100 | [diff] [blame] | 1237 | continue; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1238 | |
| 1239 | case '?': |
Darren Tucker | 92a39cf | 2012-09-07 11:20:20 +1000 | [diff] [blame] | 1240 | print_escape_help(berr, escape_char, compat20, |
| 1241 | (c && c->ctl_chan != -1), |
| 1242 | log_is_on_stderr()); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1243 | continue; |
| 1244 | |
| 1245 | case '#': |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1246 | snprintf(string, sizeof string, "%c#\r\n", |
| 1247 | escape_char); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1248 | buffer_append(berr, string, strlen(string)); |
| 1249 | s = channel_open_message(); |
| 1250 | buffer_append(berr, s, strlen(s)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1251 | free(s); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1252 | continue; |
| 1253 | |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 1254 | case 'C': |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1255 | if (c && c->ctl_chan != -1) |
Damien Miller | 586b005 | 2008-12-09 14:11:32 +1100 | [diff] [blame] | 1256 | goto noescape; |
Ben Lindstrom | 681d932 | 2002-03-22 03:53:00 +0000 | [diff] [blame] | 1257 | process_cmdline(); |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 1258 | continue; |
| 1259 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1260 | default: |
| 1261 | if (ch != escape_char) { |
| 1262 | buffer_put_char(bin, escape_char); |
| 1263 | bytes++; |
| 1264 | } |
| 1265 | /* Escaped characters fall through here */ |
| 1266 | break; |
| 1267 | } |
| 1268 | } else { |
| 1269 | /* |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 1270 | * The previous character was not an escape char. |
| 1271 | * Check if this is an escape. |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1272 | */ |
| 1273 | if (last_was_cr && ch == escape_char) { |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 1274 | /* |
| 1275 | * It is. Set the flag and continue to |
| 1276 | * next character. |
| 1277 | */ |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1278 | *escape_pendingp = 1; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1279 | continue; |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | /* |
| 1284 | * Normal character. Record whether it was a newline, |
| 1285 | * and append it to the buffer. |
| 1286 | */ |
| 1287 | last_was_cr = (ch == '\r' || ch == '\n'); |
| 1288 | buffer_put_char(bin, ch); |
| 1289 | bytes++; |
| 1290 | } |
| 1291 | return bytes; |
| 1292 | } |
| 1293 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1294 | static void |
Damien Miller | 90fdfaf | 2006-03-26 14:25:37 +1100 | [diff] [blame] | 1295 | client_process_input(fd_set *readset) |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1296 | { |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 1297 | int len; |
Darren Tucker | 86e30a0 | 2009-08-28 11:21:06 +1000 | [diff] [blame] | 1298 | char buf[SSH_IOBUFSZ]; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1299 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1300 | /* Read input from stdin. */ |
| 1301 | if (FD_ISSET(fileno(stdin), readset)) { |
| 1302 | /* Read as much as possible. */ |
| 1303 | len = read(fileno(stdin), buf, sizeof(buf)); |
Damien Miller | d8968ad | 2008-07-04 23:10:49 +1000 | [diff] [blame] | 1304 | if (len < 0 && |
| 1305 | (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK)) |
Ben Lindstrom | 4c8cff1 | 2001-04-17 18:09:42 +0000 | [diff] [blame] | 1306 | return; /* we'll try again later */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1307 | if (len <= 0) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1308 | /* |
| 1309 | * Received EOF or error. They are treated |
| 1310 | * similarly, except that an error message is printed |
| 1311 | * if it was an error condition. |
| 1312 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1313 | if (len < 0) { |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 1314 | snprintf(buf, sizeof buf, "read: %.100s\r\n", |
| 1315 | strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1316 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1317 | } |
| 1318 | /* Mark that we have seen EOF. */ |
| 1319 | stdin_eof = 1; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1320 | /* |
| 1321 | * Send an EOF message to the server unless there is |
| 1322 | * data in the buffer. If there is data in the |
| 1323 | * buffer, no message will be sent now. Code |
| 1324 | * elsewhere will send the EOF when the buffer |
| 1325 | * becomes empty if stdin_eof is set. |
| 1326 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1327 | if (buffer_len(&stdin_buffer) == 0) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1328 | packet_start(SSH_CMSG_EOF); |
| 1329 | packet_send(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1330 | } |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1331 | } else if (escape_char1 == SSH_ESCAPECHAR_NONE) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1332 | /* |
| 1333 | * Normal successful read, and no escape character. |
| 1334 | * Just append the data to buffer. |
| 1335 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1336 | buffer_append(&stdin_buffer, buf, len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1337 | } else { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1338 | /* |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 1339 | * Normal, successful read. But we have an escape |
| 1340 | * character and have to process the characters one |
| 1341 | * by one. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1342 | */ |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1343 | if (process_escapes(NULL, &stdin_buffer, |
| 1344 | &stdout_buffer, &stderr_buffer, buf, len) == -1) |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1345 | return; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1346 | } |
| 1347 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1348 | } |
| 1349 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1350 | static void |
Damien Miller | 90fdfaf | 2006-03-26 14:25:37 +1100 | [diff] [blame] | 1351 | client_process_output(fd_set *writeset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1352 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1353 | int len; |
| 1354 | char buf[100]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1355 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1356 | /* Write buffered output to stdout. */ |
| 1357 | if (FD_ISSET(fileno(stdout), writeset)) { |
| 1358 | /* Write as much data as possible. */ |
| 1359 | len = write(fileno(stdout), buffer_ptr(&stdout_buffer), |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 1360 | buffer_len(&stdout_buffer)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1361 | if (len <= 0) { |
Damien Miller | d8968ad | 2008-07-04 23:10:49 +1000 | [diff] [blame] | 1362 | if (errno == EINTR || errno == EAGAIN || |
| 1363 | errno == EWOULDBLOCK) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1364 | len = 0; |
| 1365 | else { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1366 | /* |
| 1367 | * An error or EOF was encountered. Put an |
| 1368 | * error message to stderr buffer. |
| 1369 | */ |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 1370 | snprintf(buf, sizeof buf, |
| 1371 | "write stdout: %.50s\r\n", strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1372 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1373 | quit_pending = 1; |
| 1374 | return; |
| 1375 | } |
| 1376 | } |
| 1377 | /* Consume printed data from the buffer. */ |
| 1378 | buffer_consume(&stdout_buffer, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1379 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1380 | /* Write buffered output to stderr. */ |
| 1381 | if (FD_ISSET(fileno(stderr), writeset)) { |
| 1382 | /* Write as much data as possible. */ |
| 1383 | len = write(fileno(stderr), buffer_ptr(&stderr_buffer), |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 1384 | buffer_len(&stderr_buffer)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1385 | if (len <= 0) { |
Damien Miller | d8968ad | 2008-07-04 23:10:49 +1000 | [diff] [blame] | 1386 | if (errno == EINTR || errno == EAGAIN || |
| 1387 | errno == EWOULDBLOCK) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1388 | len = 0; |
| 1389 | else { |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 1390 | /* |
| 1391 | * EOF or error, but can't even print |
| 1392 | * error message. |
| 1393 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1394 | quit_pending = 1; |
| 1395 | return; |
| 1396 | } |
| 1397 | } |
| 1398 | /* Consume printed characters from the buffer. */ |
| 1399 | buffer_consume(&stderr_buffer, len); |
| 1400 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1401 | } |
| 1402 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1403 | /* |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1404 | * Get packets from the connection input buffer, and process them as long as |
| 1405 | * there are packets available. |
| 1406 | * |
| 1407 | * Any unknown packets received during the actual |
| 1408 | * session cause the session to terminate. This is |
| 1409 | * intended to make debugging easier since no |
| 1410 | * confirmations are sent. Any compatible protocol |
| 1411 | * extensions must be negotiated during the |
| 1412 | * preparatory phase. |
| 1413 | */ |
| 1414 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1415 | static void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 1416 | client_process_buffered_input_packets(void) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1417 | { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 1418 | dispatch_run(DISPATCH_NONBLOCK, &quit_pending, active_state); |
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; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 1472 | int r, 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 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1557 | rekeying = (active_state->kex != NULL && !active_state->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"); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1601 | active_state->kex->done = 0; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 1602 | if ((r = kex_send_kexinit(active_state)) != 0) |
| 1603 | fatal("%s: kex_send_kexinit: %s", |
| 1604 | __func__, ssh_err(r)); |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 1605 | need_rekeying = 0; |
| 1606 | } |
| 1607 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1608 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1609 | /* Buffer input from the connection. */ |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 1610 | client_process_net_input(readset); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1611 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1612 | if (quit_pending) |
| 1613 | break; |
| 1614 | |
| 1615 | if (!compat20) { |
| 1616 | /* Buffer data from stdin */ |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 1617 | client_process_input(readset); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1618 | /* |
| 1619 | * Process output to stdout and stderr. Output to |
| 1620 | * the connection is processed elsewhere (above). |
| 1621 | */ |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 1622 | client_process_output(writeset); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1623 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1624 | |
Darren Tucker | e32cf43 | 2010-01-08 16:51:40 +1100 | [diff] [blame] | 1625 | if (session_resumed) { |
| 1626 | connection_in = packet_get_connection_in(); |
| 1627 | connection_out = packet_get_connection_out(); |
| 1628 | max_fd = MAX(max_fd, connection_out); |
| 1629 | max_fd = MAX(max_fd, connection_in); |
| 1630 | session_resumed = 0; |
| 1631 | } |
| 1632 | |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 1633 | /* |
| 1634 | * Send as much buffered packet data as possible to the |
| 1635 | * sender. |
| 1636 | */ |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 1637 | if (FD_ISSET(connection_out, writeset)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1638 | packet_write_poll(); |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 1639 | |
| 1640 | /* |
| 1641 | * If we are a backgrounded control master, and the |
| 1642 | * timeout has expired without any active client |
| 1643 | * connections, then quit. |
| 1644 | */ |
| 1645 | if (control_persist_exit_time > 0) { |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 1646 | if (monotime() >= control_persist_exit_time) { |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 1647 | debug("ControlPersist timeout expired"); |
| 1648 | break; |
| 1649 | } |
| 1650 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1651 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1652 | free(readset); |
| 1653 | free(writeset); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1654 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1655 | /* Terminate the session. */ |
| 1656 | |
| 1657 | /* Stop watching for window change. */ |
Darren Tucker | 5d78de6 | 2004-11-05 20:35:44 +1100 | [diff] [blame] | 1658 | signal(SIGWINCH, SIG_DFL); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1659 | |
Darren Tucker | de0c025 | 2009-07-06 07:17:35 +1000 | [diff] [blame] | 1660 | if (compat20) { |
| 1661 | packet_start(SSH2_MSG_DISCONNECT); |
| 1662 | packet_put_int(SSH2_DISCONNECT_BY_APPLICATION); |
| 1663 | packet_put_cstring("disconnected by user"); |
Damien Miller | 8ddc71c | 2010-03-22 05:54:02 +1100 | [diff] [blame] | 1664 | packet_put_cstring(""); /* language tag */ |
Darren Tucker | de0c025 | 2009-07-06 07:17:35 +1000 | [diff] [blame] | 1665 | packet_send(); |
| 1666 | packet_write_wait(); |
| 1667 | } |
Darren Tucker | 12b4a65 | 2009-06-21 18:14:48 +1000 | [diff] [blame] | 1668 | |
Ben Lindstrom | 601e436 | 2001-06-21 03:19:23 +0000 | [diff] [blame] | 1669 | channel_free_all(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1670 | |
Ben Lindstrom | ec46e0b | 2001-06-09 01:27:31 +0000 | [diff] [blame] | 1671 | if (have_pty) |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 1672 | leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 1673 | |
| 1674 | /* restore blocking io */ |
| 1675 | if (!isatty(fileno(stdin))) |
| 1676 | unset_nonblock(fileno(stdin)); |
| 1677 | if (!isatty(fileno(stdout))) |
| 1678 | unset_nonblock(fileno(stdout)); |
| 1679 | if (!isatty(fileno(stderr))) |
| 1680 | unset_nonblock(fileno(stderr)); |
| 1681 | |
Damien Miller | d696551 | 2003-12-17 16:31:53 +1100 | [diff] [blame] | 1682 | /* |
| 1683 | * If there was no shell or command requested, there will be no remote |
| 1684 | * exit status to be returned. In that case, clear error code if the |
| 1685 | * connection was deliberately terminated at this end. |
| 1686 | */ |
| 1687 | if (no_shell_flag && received_signal == SIGTERM) { |
| 1688 | received_signal = 0; |
| 1689 | exit_status = 0; |
| 1690 | } |
| 1691 | |
Darren Tucker | 9a2c4cd | 2003-09-22 21:16:05 +1000 | [diff] [blame] | 1692 | if (received_signal) |
Ben Lindstrom | f8f065b | 2001-12-06 17:52:16 +0000 | [diff] [blame] | 1693 | fatal("Killed by signal %d.", (int) received_signal); |
Ben Lindstrom | ec46e0b | 2001-06-09 01:27:31 +0000 | [diff] [blame] | 1694 | |
| 1695 | /* |
| 1696 | * In interactive mode (with pseudo tty) display a message indicating |
| 1697 | * that the connection has been closed. |
| 1698 | */ |
| 1699 | if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) { |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 1700 | snprintf(buf, sizeof buf, |
| 1701 | "Connection to %.64s closed.\r\n", host); |
Ben Lindstrom | ec46e0b | 2001-06-09 01:27:31 +0000 | [diff] [blame] | 1702 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
| 1703 | } |
| 1704 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1705 | /* Output any buffered data for stdout. */ |
Damien Miller | cfd6e4f | 2011-01-16 23:18:33 +1100 | [diff] [blame] | 1706 | if (buffer_len(&stdout_buffer) > 0) { |
Damien Miller | 4791f9d | 2011-01-16 23:16:53 +1100 | [diff] [blame] | 1707 | len = atomicio(vwrite, fileno(stdout), |
| 1708 | buffer_ptr(&stdout_buffer), buffer_len(&stdout_buffer)); |
Damien Miller | cfd6e4f | 2011-01-16 23:18:33 +1100 | [diff] [blame] | 1709 | if (len < 0 || (u_int)len != buffer_len(&stdout_buffer)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1710 | error("Write failed flushing stdout buffer."); |
Damien Miller | cfd6e4f | 2011-01-16 23:18:33 +1100 | [diff] [blame] | 1711 | else |
| 1712 | buffer_consume(&stdout_buffer, len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1713 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1714 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1715 | /* Output any buffered data for stderr. */ |
Damien Miller | cfd6e4f | 2011-01-16 23:18:33 +1100 | [diff] [blame] | 1716 | if (buffer_len(&stderr_buffer) > 0) { |
Damien Miller | 4791f9d | 2011-01-16 23:16:53 +1100 | [diff] [blame] | 1717 | len = atomicio(vwrite, fileno(stderr), |
| 1718 | buffer_ptr(&stderr_buffer), buffer_len(&stderr_buffer)); |
Damien Miller | cfd6e4f | 2011-01-16 23:18:33 +1100 | [diff] [blame] | 1719 | if (len < 0 || (u_int)len != buffer_len(&stderr_buffer)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1720 | error("Write failed flushing stderr buffer."); |
Damien Miller | cfd6e4f | 2011-01-16 23:18:33 +1100 | [diff] [blame] | 1721 | else |
| 1722 | buffer_consume(&stderr_buffer, len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1723 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1724 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1725 | /* Clear and free any buffers. */ |
| 1726 | memset(buf, 0, sizeof(buf)); |
| 1727 | buffer_free(&stdin_buffer); |
| 1728 | buffer_free(&stdout_buffer); |
| 1729 | buffer_free(&stderr_buffer); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1730 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1731 | /* Report bytes transferred, and transfer rates. */ |
| 1732 | total_time = get_current_time() - start_time; |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1733 | packet_get_bytes(&ibytes, &obytes); |
Damien Miller | b61f3fc | 2008-07-11 17:36:48 +1000 | [diff] [blame] | 1734 | verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds", |
Damien Miller | 821de0a | 2011-01-11 17:20:29 +1100 | [diff] [blame] | 1735 | (unsigned long long)obytes, (unsigned long long)ibytes, total_time); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1736 | if (total_time > 0) |
Damien Miller | b61f3fc | 2008-07-11 17:36:48 +1000 | [diff] [blame] | 1737 | verbose("Bytes per second: sent %.1f, received %.1f", |
| 1738 | obytes / total_time, ibytes / total_time); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1739 | /* Return the exit status of the program. */ |
| 1740 | debug("Exit status %d", exit_status); |
| 1741 | return exit_status; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1742 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1743 | |
| 1744 | /*********/ |
| 1745 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 1746 | static int |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 1747 | client_input_stdout_data(int type, u_int32_t seq, void *ctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1748 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1749 | u_int data_len; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1750 | char *data = packet_get_string(&data_len); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1751 | packet_check_eom(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1752 | buffer_append(&stdout_buffer, data, data_len); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 1753 | explicit_bzero(data, data_len); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1754 | free(data); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 1755 | return 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1756 | } |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 1757 | static int |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 1758 | client_input_stderr_data(int type, u_int32_t seq, void *ctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1759 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1760 | u_int data_len; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1761 | char *data = packet_get_string(&data_len); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1762 | packet_check_eom(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1763 | buffer_append(&stderr_buffer, data, data_len); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 1764 | explicit_bzero(data, data_len); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1765 | free(data); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 1766 | return 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1767 | } |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 1768 | static int |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 1769 | client_input_exit_status(int type, u_int32_t seq, void *ctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1770 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1771 | exit_status = packet_get_int(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1772 | packet_check_eom(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1773 | /* Acknowledge the exit. */ |
| 1774 | packet_start(SSH_CMSG_EXIT_CONFIRMATION); |
| 1775 | packet_send(); |
| 1776 | /* |
| 1777 | * Must wait for packet to be sent since we are |
| 1778 | * exiting the loop. |
| 1779 | */ |
| 1780 | packet_write_wait(); |
| 1781 | /* Flag that we want to exit. */ |
| 1782 | quit_pending = 1; |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 1783 | return 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1784 | } |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 1785 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 1786 | static int |
Darren Tucker | 5dcdd21 | 2003-10-02 16:17:00 +1000 | [diff] [blame] | 1787 | client_input_agent_open(int type, u_int32_t seq, void *ctxt) |
| 1788 | { |
| 1789 | Channel *c = NULL; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1790 | int r, remote_id, sock; |
Darren Tucker | 5dcdd21 | 2003-10-02 16:17:00 +1000 | [diff] [blame] | 1791 | |
| 1792 | /* Read the remote channel number from the message. */ |
| 1793 | remote_id = packet_get_int(); |
| 1794 | packet_check_eom(); |
| 1795 | |
| 1796 | /* |
| 1797 | * Get a connection to the local authentication agent (this may again |
| 1798 | * get forwarded). |
| 1799 | */ |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1800 | if ((r = ssh_get_authentication_socket(&sock)) != 0 && |
| 1801 | r != SSH_ERR_AGENT_NOT_PRESENT) |
| 1802 | debug("%s: ssh_get_authentication_socket: %s", |
| 1803 | __func__, ssh_err(r)); |
| 1804 | |
Darren Tucker | 5dcdd21 | 2003-10-02 16:17:00 +1000 | [diff] [blame] | 1805 | |
| 1806 | /* |
| 1807 | * If we could not connect the agent, send an error message back to |
| 1808 | * the server. This should never happen unless the agent dies, |
| 1809 | * because authentication forwarding is only enabled if we have an |
| 1810 | * agent. |
| 1811 | */ |
| 1812 | if (sock >= 0) { |
| 1813 | c = channel_new("", SSH_CHANNEL_OPEN, sock, sock, |
| 1814 | -1, 0, 0, 0, "authentication agent connection", 1); |
| 1815 | c->remote_id = remote_id; |
| 1816 | c->force_drain = 1; |
| 1817 | } |
| 1818 | if (c == NULL) { |
| 1819 | packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE); |
| 1820 | packet_put_int(remote_id); |
| 1821 | } else { |
| 1822 | /* Send a confirmation to the remote host. */ |
| 1823 | debug("Forwarding authentication connection."); |
| 1824 | packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION); |
| 1825 | packet_put_int(remote_id); |
| 1826 | packet_put_int(c->self); |
| 1827 | } |
| 1828 | packet_send(); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 1829 | return 0; |
Darren Tucker | 5dcdd21 | 2003-10-02 16:17:00 +1000 | [diff] [blame] | 1830 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1831 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1832 | static Channel * |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1833 | client_request_forwarded_tcpip(const char *request_type, int rchan) |
| 1834 | { |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 1835 | Channel *c = NULL; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1836 | char *listen_address, *originator_address; |
Damien Miller | 3dc71ad | 2009-01-28 16:31:22 +1100 | [diff] [blame] | 1837 | u_short listen_port, originator_port; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1838 | |
| 1839 | /* Get rest of the packet */ |
| 1840 | listen_address = packet_get_string(NULL); |
| 1841 | listen_port = packet_get_int(); |
| 1842 | originator_address = packet_get_string(NULL); |
| 1843 | originator_port = packet_get_int(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1844 | packet_check_eom(); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1845 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1846 | debug("%s: listen %s port %d, originator %s port %d", __func__, |
| 1847 | listen_address, listen_port, originator_address, originator_port); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1848 | |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 1849 | c = channel_connect_by_listen_address(listen_address, listen_port, |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 1850 | "forwarded-tcpip", originator_address); |
| 1851 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1852 | free(originator_address); |
| 1853 | free(listen_address); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1854 | return c; |
| 1855 | } |
| 1856 | |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 1857 | static Channel * |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1858 | client_request_forwarded_streamlocal(const char *request_type, int rchan) |
| 1859 | { |
| 1860 | Channel *c = NULL; |
| 1861 | char *listen_path; |
| 1862 | |
| 1863 | /* Get the remote path. */ |
| 1864 | listen_path = packet_get_string(NULL); |
| 1865 | /* XXX: Skip reserved field for now. */ |
| 1866 | if (packet_get_string_ptr(NULL) == NULL) |
| 1867 | fatal("%s: packet_get_string_ptr failed", __func__); |
| 1868 | packet_check_eom(); |
| 1869 | |
| 1870 | debug("%s: %s", __func__, listen_path); |
| 1871 | |
| 1872 | c = channel_connect_by_listen_path(listen_path, |
| 1873 | "forwarded-streamlocal@openssh.com", "forwarded-streamlocal"); |
| 1874 | free(listen_path); |
| 1875 | return c; |
| 1876 | } |
| 1877 | |
| 1878 | static Channel * |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1879 | client_request_x11(const char *request_type, int rchan) |
| 1880 | { |
| 1881 | Channel *c = NULL; |
| 1882 | char *originator; |
Damien Miller | 3dc71ad | 2009-01-28 16:31:22 +1100 | [diff] [blame] | 1883 | u_short originator_port; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1884 | int sock; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1885 | |
| 1886 | if (!options.forward_x11) { |
| 1887 | error("Warning: ssh server tried X11 forwarding."); |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 1888 | error("Warning: this is probably a break-in attempt by a " |
| 1889 | "malicious server."); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1890 | return NULL; |
| 1891 | } |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 1892 | if (x11_refuse_time != 0 && monotime() >= x11_refuse_time) { |
Damien Miller | 1ab6a51 | 2010-06-26 10:02:24 +1000 | [diff] [blame] | 1893 | verbose("Rejected X11 connection after ForwardX11Timeout " |
| 1894 | "expired"); |
| 1895 | return NULL; |
| 1896 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1897 | originator = packet_get_string(NULL); |
| 1898 | if (datafellows & SSH_BUG_X11FWD) { |
| 1899 | debug2("buggy server: x11 request w/o originator_port"); |
| 1900 | originator_port = 0; |
| 1901 | } else { |
| 1902 | originator_port = packet_get_int(); |
| 1903 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1904 | packet_check_eom(); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1905 | /* XXX check permission */ |
Damien Miller | d83ff35 | 2001-01-30 09:19:34 +1100 | [diff] [blame] | 1906 | debug("client_request_x11: request from %s %d", originator, |
| 1907 | originator_port); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1908 | free(originator); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1909 | sock = x11_connect_display(); |
| 1910 | if (sock < 0) |
| 1911 | return NULL; |
| 1912 | c = channel_new("x11", |
| 1913 | SSH_CHANNEL_X11_OPEN, sock, sock, -1, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 1914 | CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 1915 | c->force_drain = 1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1916 | return c; |
| 1917 | } |
| 1918 | |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 1919 | static Channel * |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1920 | client_request_agent(const char *request_type, int rchan) |
| 1921 | { |
| 1922 | Channel *c = NULL; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1923 | int r, sock; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1924 | |
| 1925 | if (!options.forward_agent) { |
| 1926 | error("Warning: ssh server tried agent forwarding."); |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 1927 | error("Warning: this is probably a break-in attempt by a " |
| 1928 | "malicious server."); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1929 | return NULL; |
| 1930 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1931 | if ((r = ssh_get_authentication_socket(&sock)) != 0) { |
| 1932 | if (r != SSH_ERR_AGENT_NOT_PRESENT) |
| 1933 | debug("%s: ssh_get_authentication_socket: %s", |
| 1934 | __func__, ssh_err(r)); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1935 | return NULL; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1936 | } |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1937 | c = channel_new("authentication agent connection", |
| 1938 | SSH_CHANNEL_OPEN, sock, sock, -1, |
Darren Tucker | 5baa170 | 2007-12-29 09:37:10 +1100 | [diff] [blame] | 1939 | CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 1940 | "authentication agent connection", 1); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 1941 | c->force_drain = 1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1942 | return c; |
| 1943 | } |
| 1944 | |
Damien Miller | b3ce9fe | 2007-08-08 14:32:41 +1000 | [diff] [blame] | 1945 | int |
| 1946 | client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun) |
| 1947 | { |
| 1948 | Channel *c; |
| 1949 | int fd; |
| 1950 | |
| 1951 | if (tun_mode == SSH_TUNMODE_NO) |
| 1952 | return 0; |
| 1953 | |
| 1954 | if (!compat20) { |
Damien Miller | b3f2c9f | 2009-01-28 16:15:30 +1100 | [diff] [blame] | 1955 | error("Tunnel forwarding is not supported for protocol 1"); |
Damien Miller | b3ce9fe | 2007-08-08 14:32:41 +1000 | [diff] [blame] | 1956 | return -1; |
| 1957 | } |
| 1958 | |
| 1959 | debug("Requesting tun unit %d in mode %d", local_tun, tun_mode); |
| 1960 | |
| 1961 | /* Open local tunnel device */ |
| 1962 | if ((fd = tun_open(local_tun, tun_mode)) == -1) { |
| 1963 | error("Tunnel device open failed."); |
| 1964 | return -1; |
| 1965 | } |
| 1966 | |
| 1967 | c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1, |
| 1968 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1); |
| 1969 | c->datagram = 1; |
| 1970 | |
| 1971 | #if defined(SSH_TUN_FILTER) |
| 1972 | if (options.tun_open == SSH_TUNMODE_POINTOPOINT) |
| 1973 | channel_register_filter(c->self, sys_tun_infilter, |
Darren Tucker | 1cf65ae | 2008-06-13 05:09:18 +1000 | [diff] [blame] | 1974 | sys_tun_outfilter, NULL, NULL); |
Damien Miller | b3ce9fe | 2007-08-08 14:32:41 +1000 | [diff] [blame] | 1975 | #endif |
| 1976 | |
| 1977 | packet_start(SSH2_MSG_CHANNEL_OPEN); |
| 1978 | packet_put_cstring("tun@openssh.com"); |
| 1979 | packet_put_int(c->self); |
| 1980 | packet_put_int(c->local_window_max); |
| 1981 | packet_put_int(c->local_maxpacket); |
| 1982 | packet_put_int(tun_mode); |
| 1983 | packet_put_int(remote_tun); |
| 1984 | packet_send(); |
| 1985 | |
| 1986 | return 0; |
| 1987 | } |
| 1988 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1989 | /* XXXX move to generic input handler */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 1990 | static int |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 1991 | client_input_channel_open(int type, u_int32_t seq, void *ctxt) |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1992 | { |
| 1993 | Channel *c = NULL; |
| 1994 | char *ctype; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1995 | int rchan; |
Ben Lindstrom | 4fed2be | 2002-06-25 23:17:36 +0000 | [diff] [blame] | 1996 | u_int rmaxpack, rwindow, len; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1997 | |
| 1998 | ctype = packet_get_string(&len); |
| 1999 | rchan = packet_get_int(); |
| 2000 | rwindow = packet_get_int(); |
| 2001 | rmaxpack = packet_get_int(); |
| 2002 | |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 2003 | 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] | 2004 | ctype, rchan, rwindow, rmaxpack); |
| 2005 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2006 | if (strcmp(ctype, "forwarded-tcpip") == 0) { |
| 2007 | c = client_request_forwarded_tcpip(ctype, rchan); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2008 | } else if (strcmp(ctype, "forwarded-streamlocal@openssh.com") == 0) { |
| 2009 | c = client_request_forwarded_streamlocal(ctype, rchan); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2010 | } else if (strcmp(ctype, "x11") == 0) { |
| 2011 | c = client_request_x11(ctype, rchan); |
| 2012 | } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) { |
| 2013 | c = client_request_agent(ctype, rchan); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2014 | } |
| 2015 | /* XXX duplicate : */ |
| 2016 | if (c != NULL) { |
| 2017 | debug("confirm %s", ctype); |
| 2018 | c->remote_id = rchan; |
| 2019 | c->remote_window = rwindow; |
| 2020 | c->remote_maxpacket = rmaxpack; |
Ben Lindstrom | a69d89b | 2001-05-09 00:01:18 +0000 | [diff] [blame] | 2021 | if (c->type != SSH_CHANNEL_CONNECTING) { |
| 2022 | packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); |
| 2023 | packet_put_int(c->remote_id); |
| 2024 | packet_put_int(c->self); |
| 2025 | packet_put_int(c->local_window); |
| 2026 | packet_put_int(c->local_maxpacket); |
| 2027 | packet_send(); |
| 2028 | } |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2029 | } else { |
| 2030 | debug("failure %s", ctype); |
| 2031 | packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE); |
| 2032 | packet_put_int(rchan); |
| 2033 | packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED); |
Ben Lindstrom | f343674 | 2001-04-29 19:52:00 +0000 | [diff] [blame] | 2034 | if (!(datafellows & SSH_BUG_OPENFAILURE)) { |
Ben Lindstrom | a69d89b | 2001-05-09 00:01:18 +0000 | [diff] [blame] | 2035 | packet_put_cstring("open failed"); |
Ben Lindstrom | f343674 | 2001-04-29 19:52:00 +0000 | [diff] [blame] | 2036 | packet_put_cstring(""); |
| 2037 | } |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2038 | packet_send(); |
| 2039 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2040 | free(ctype); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2041 | return 0; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2042 | } |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2043 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2044 | static int |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 2045 | client_input_channel_req(int type, u_int32_t seq, void *ctxt) |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 2046 | { |
| 2047 | Channel *c = NULL; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2048 | int exitval, id, reply, success = 0; |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 2049 | char *rtype; |
| 2050 | |
| 2051 | id = packet_get_int(); |
| 2052 | rtype = packet_get_string(NULL); |
| 2053 | reply = packet_get_char(); |
| 2054 | |
| 2055 | debug("client_input_channel_req: channel %d rtype %s reply %d", |
| 2056 | id, rtype, reply); |
| 2057 | |
Damien Miller | 3bbd878 | 2004-06-18 22:23:22 +1000 | [diff] [blame] | 2058 | if (id == -1) { |
| 2059 | error("client_input_channel_req: request for channel -1"); |
| 2060 | } else if ((c = channel_lookup(id)) == NULL) { |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 2061 | error("client_input_channel_req: channel %d: " |
| 2062 | "unknown channel", id); |
Damien Miller | bab9bd4 | 2008-05-19 16:06:47 +1000 | [diff] [blame] | 2063 | } else if (strcmp(rtype, "eow@openssh.com") == 0) { |
| 2064 | packet_check_eom(); |
| 2065 | chan_rcvd_eow(c); |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 2066 | } else if (strcmp(rtype, "exit-status") == 0) { |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2067 | exitval = packet_get_int(); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2068 | if (c->ctl_chan != -1) { |
| 2069 | mux_exit_message(c, exitval); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2070 | success = 1; |
Darren Tucker | 2944082 | 2010-01-08 17:08:35 +1100 | [diff] [blame] | 2071 | } else if (id == session_ident) { |
| 2072 | /* Record exit value of local session */ |
| 2073 | success = 1; |
| 2074 | exit_status = exitval; |
| 2075 | } else { |
Damien Miller | 36f57eb | 2010-01-30 17:28:34 +1100 | [diff] [blame] | 2076 | /* Probably for a mux channel that has already closed */ |
| 2077 | debug("%s: no sink for exit-status on channel %d", |
| 2078 | __func__, id); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2079 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 2080 | packet_check_eom(); |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 2081 | } |
Damien Miller | c589378 | 2014-05-15 13:48:49 +1000 | [diff] [blame] | 2082 | if (reply && c != NULL && !(c->flags & CHAN_CLOSE_SENT)) { |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 2083 | packet_start(success ? |
| 2084 | SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE); |
Damien Miller | 8533c78 | 2008-12-08 09:54:40 +1100 | [diff] [blame] | 2085 | packet_put_int(c->remote_id); |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 2086 | packet_send(); |
| 2087 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2088 | free(rtype); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2089 | return 0; |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 2090 | } |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2091 | |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2092 | struct hostkeys_update_ctx { |
| 2093 | /* The hostname and (optionally) IP address string for the server */ |
| 2094 | char *host_str, *ip_str; |
| 2095 | |
| 2096 | /* |
| 2097 | * Keys received from the server and a flag for each indicating |
| 2098 | * whether they already exist in known_hosts. |
| 2099 | * keys_seen is filled in by hostkeys_find() and later (for new |
| 2100 | * keys) by client_global_hostkeys_private_confirm(). |
| 2101 | */ |
| 2102 | struct sshkey **keys; |
| 2103 | int *keys_seen; |
| 2104 | size_t nkeys; |
| 2105 | |
| 2106 | size_t nnew; |
| 2107 | |
| 2108 | /* |
| 2109 | * Keys that are in known_hosts, but were not present in the update |
| 2110 | * from the server (i.e. scheduled to be deleted). |
| 2111 | * Filled in by hostkeys_find(). |
| 2112 | */ |
| 2113 | struct sshkey **old_keys; |
| 2114 | size_t nold; |
| 2115 | }; |
| 2116 | |
| 2117 | static void |
| 2118 | hostkeys_update_ctx_free(struct hostkeys_update_ctx *ctx) |
| 2119 | { |
| 2120 | size_t i; |
| 2121 | |
| 2122 | if (ctx == NULL) |
| 2123 | return; |
| 2124 | for (i = 0; i < ctx->nkeys; i++) |
| 2125 | sshkey_free(ctx->keys[i]); |
| 2126 | free(ctx->keys); |
| 2127 | free(ctx->keys_seen); |
| 2128 | for (i = 0; i < ctx->nold; i++) |
| 2129 | sshkey_free(ctx->old_keys[i]); |
| 2130 | free(ctx->old_keys); |
| 2131 | free(ctx->host_str); |
| 2132 | free(ctx->ip_str); |
| 2133 | free(ctx); |
| 2134 | } |
| 2135 | |
| 2136 | static int |
| 2137 | hostkeys_find(struct hostkey_foreach_line *l, void *_ctx) |
| 2138 | { |
| 2139 | struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx; |
| 2140 | size_t i; |
| 2141 | struct sshkey **tmp; |
| 2142 | |
| 2143 | if (l->status != HKF_STATUS_MATCHED || l->key == NULL || |
| 2144 | l->key->type == KEY_RSA1) |
| 2145 | return 0; |
| 2146 | |
| 2147 | /* Mark off keys we've already seen for this host */ |
| 2148 | for (i = 0; i < ctx->nkeys; i++) { |
| 2149 | if (sshkey_equal(l->key, ctx->keys[i])) { |
| 2150 | debug3("%s: found %s key at %s:%ld", __func__, |
| 2151 | sshkey_ssh_name(ctx->keys[i]), l->path, l->linenum); |
| 2152 | ctx->keys_seen[i] = 1; |
| 2153 | return 0; |
| 2154 | } |
| 2155 | } |
| 2156 | /* This line contained a key that not offered by the server */ |
| 2157 | debug3("%s: deprecated %s key at %s:%ld", __func__, |
| 2158 | sshkey_ssh_name(l->key), l->path, l->linenum); |
| 2159 | if ((tmp = reallocarray(ctx->old_keys, ctx->nold + 1, |
| 2160 | sizeof(*ctx->old_keys))) == NULL) |
| 2161 | fatal("%s: reallocarray failed nold = %zu", |
| 2162 | __func__, ctx->nold); |
| 2163 | ctx->old_keys = tmp; |
| 2164 | ctx->old_keys[ctx->nold++] = l->key; |
| 2165 | l->key = NULL; |
| 2166 | |
| 2167 | return 0; |
| 2168 | } |
| 2169 | |
| 2170 | static void |
| 2171 | update_known_hosts(struct hostkeys_update_ctx *ctx) |
| 2172 | { |
| 2173 | int r, loglevel = options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK ? |
| 2174 | SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_VERBOSE; |
| 2175 | char *fp, *response; |
| 2176 | size_t i; |
| 2177 | |
| 2178 | for (i = 0; i < ctx->nkeys; i++) { |
| 2179 | if (ctx->keys_seen[i] != 2) |
| 2180 | continue; |
| 2181 | if ((fp = sshkey_fingerprint(ctx->keys[i], |
| 2182 | options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) |
| 2183 | fatal("%s: sshkey_fingerprint failed", __func__); |
| 2184 | do_log2(loglevel, "Learned new hostkey: %s %s", |
| 2185 | sshkey_type(ctx->keys[i]), fp); |
| 2186 | free(fp); |
| 2187 | } |
| 2188 | for (i = 0; i < ctx->nold; i++) { |
| 2189 | if ((fp = sshkey_fingerprint(ctx->old_keys[i], |
| 2190 | options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) |
| 2191 | fatal("%s: sshkey_fingerprint failed", __func__); |
| 2192 | do_log2(loglevel, "Deprecating obsolete hostkey: %s %s", |
| 2193 | sshkey_type(ctx->old_keys[i]), fp); |
| 2194 | free(fp); |
| 2195 | } |
| 2196 | if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK) { |
| 2197 | leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); |
| 2198 | response = NULL; |
| 2199 | for (i = 0; !quit_pending && i < 3; i++) { |
| 2200 | free(response); |
| 2201 | response = read_passphrase("Accept updated hostkeys? " |
| 2202 | "(yes/no): ", RP_ECHO); |
| 2203 | if (strcasecmp(response, "yes") == 0) |
| 2204 | break; |
| 2205 | else if (quit_pending || response == NULL || |
| 2206 | strcasecmp(response, "no") == 0) { |
| 2207 | options.update_hostkeys = 0; |
| 2208 | break; |
| 2209 | } else { |
| 2210 | do_log2(loglevel, "Please enter " |
| 2211 | "\"yes\" or \"no\""); |
| 2212 | } |
| 2213 | } |
| 2214 | if (quit_pending || i >= 3 || response == NULL) |
| 2215 | options.update_hostkeys = 0; |
| 2216 | free(response); |
| 2217 | enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE); |
| 2218 | } |
| 2219 | |
| 2220 | /* |
| 2221 | * Now that all the keys are verified, we can go ahead and replace |
| 2222 | * them in known_hosts (assuming SSH_UPDATE_HOSTKEYS_ASK didn't |
| 2223 | * cancel the operation). |
| 2224 | */ |
| 2225 | if (options.update_hostkeys != 0 && |
| 2226 | (r = hostfile_replace_entries(options.user_hostfiles[0], |
| 2227 | ctx->host_str, ctx->ip_str, ctx->keys, ctx->nkeys, |
| 2228 | options.hash_known_hosts, 0, |
| 2229 | options.fingerprint_hash)) != 0) |
| 2230 | error("%s: hostfile_replace_entries failed: %s", |
| 2231 | __func__, ssh_err(r)); |
| 2232 | } |
| 2233 | |
| 2234 | static void |
| 2235 | client_global_hostkeys_private_confirm(int type, u_int32_t seq, void *_ctx) |
| 2236 | { |
| 2237 | struct ssh *ssh = active_state; /* XXX */ |
| 2238 | struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx; |
| 2239 | size_t i, ndone; |
| 2240 | struct sshbuf *signdata; |
| 2241 | int r; |
| 2242 | const u_char *sig; |
| 2243 | size_t siglen; |
| 2244 | |
| 2245 | if (ctx->nnew == 0) |
| 2246 | fatal("%s: ctx->nnew == 0", __func__); /* sanity */ |
| 2247 | if (type != SSH2_MSG_REQUEST_SUCCESS) { |
| 2248 | error("Server failed to confirm ownership of " |
| 2249 | "private host keys"); |
| 2250 | hostkeys_update_ctx_free(ctx); |
| 2251 | return; |
| 2252 | } |
| 2253 | if ((signdata = sshbuf_new()) == NULL) |
| 2254 | fatal("%s: sshbuf_new failed", __func__); |
| 2255 | /* Don't want to accidentally accept an unbound signature */ |
| 2256 | if (ssh->kex->session_id_len == 0) |
| 2257 | fatal("%s: ssh->kex->session_id_len == 0", __func__); |
| 2258 | /* |
| 2259 | * Expect a signature for each of the ctx->nnew private keys we |
| 2260 | * haven't seen before. They will be in the same order as the |
| 2261 | * ctx->keys where the corresponding ctx->keys_seen[i] == 0. |
| 2262 | */ |
| 2263 | for (ndone = i = 0; i < ctx->nkeys; i++) { |
| 2264 | if (ctx->keys_seen[i]) |
| 2265 | continue; |
| 2266 | /* Prepare data to be signed: session ID, unique string, key */ |
| 2267 | sshbuf_reset(signdata); |
djm@openbsd.org | 44732de | 2015-02-20 22:17:21 +0000 | [diff] [blame^] | 2268 | if ( (r = sshbuf_put_cstring(signdata, |
| 2269 | "hostkeys-prove-00@openssh.com")) != 0 || |
| 2270 | (r = sshbuf_put_string(signdata, ssh->kex->session_id, |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2271 | ssh->kex->session_id_len)) != 0 || |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2272 | (r = sshkey_puts(ctx->keys[i], signdata)) != 0) |
| 2273 | fatal("%s: failed to prepare signature: %s", |
| 2274 | __func__, ssh_err(r)); |
| 2275 | /* Extract and verify signature */ |
| 2276 | if ((r = sshpkt_get_string_direct(ssh, &sig, &siglen)) != 0) { |
| 2277 | error("%s: couldn't parse message: %s", |
| 2278 | __func__, ssh_err(r)); |
| 2279 | goto out; |
| 2280 | } |
| 2281 | if ((r = sshkey_verify(ctx->keys[i], sig, siglen, |
| 2282 | sshbuf_ptr(signdata), sshbuf_len(signdata), 0)) != 0) { |
| 2283 | error("%s: server gave bad signature for %s key %zu", |
| 2284 | __func__, sshkey_type(ctx->keys[i]), i); |
| 2285 | goto out; |
| 2286 | } |
| 2287 | /* Key is good. Mark it as 'seen' */ |
| 2288 | ctx->keys_seen[i] = 2; |
| 2289 | ndone++; |
| 2290 | } |
| 2291 | if (ndone != ctx->nnew) |
| 2292 | fatal("%s: ndone != ctx->nnew (%zu / %zu)", __func__, |
| 2293 | ndone, ctx->nnew); /* Shouldn't happen */ |
| 2294 | ssh_packet_check_eom(ssh); |
| 2295 | |
| 2296 | /* Make the edits to known_hosts */ |
| 2297 | update_known_hosts(ctx); |
| 2298 | out: |
| 2299 | hostkeys_update_ctx_free(ctx); |
| 2300 | } |
| 2301 | |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2302 | /* |
djm@openbsd.org | 44732de | 2015-02-20 22:17:21 +0000 | [diff] [blame^] | 2303 | * Handle hostkeys-00@openssh.com global request to inform the client of all |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2304 | * the server's hostkeys. The keys are checked against the user's |
| 2305 | * HostkeyAlgorithms preference before they are accepted. |
| 2306 | */ |
| 2307 | static int |
| 2308 | client_input_hostkeys(void) |
| 2309 | { |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2310 | struct ssh *ssh = active_state; /* XXX */ |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2311 | const u_char *blob = NULL; |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2312 | size_t i, len = 0; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2313 | struct sshbuf *buf = NULL; |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2314 | struct sshkey *key = NULL, **tmp; |
| 2315 | int r; |
| 2316 | char *fp; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2317 | static int hostkeys_seen = 0; /* XXX use struct ssh */ |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 2318 | extern struct sockaddr_storage hostaddr; /* XXX from ssh.c */ |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2319 | struct hostkeys_update_ctx *ctx; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2320 | |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2321 | ctx = xcalloc(1, sizeof(*ctx)); |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2322 | |
| 2323 | if (hostkeys_seen) |
| 2324 | fatal("%s: server already sent hostkeys", __func__); |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2325 | if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK && |
| 2326 | options.batch_mode) |
| 2327 | return 1; /* won't ask in batchmode, so don't even try */ |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2328 | if (!options.update_hostkeys || options.num_user_hostfiles <= 0) |
| 2329 | return 1; |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2330 | while (ssh_packet_remaining(ssh) > 0) { |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2331 | sshkey_free(key); |
| 2332 | key = NULL; |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2333 | if ((r = sshpkt_get_string_direct(ssh, &blob, &len)) != 0) { |
| 2334 | error("%s: couldn't parse message: %s", |
| 2335 | __func__, ssh_err(r)); |
| 2336 | goto out; |
| 2337 | } |
djm@openbsd.org | 44732de | 2015-02-20 22:17:21 +0000 | [diff] [blame^] | 2338 | if ((r = sshkey_from_blob(blob, len, &key)) != 0) { |
| 2339 | error("%s: parse key: %s", __func__, ssh_err(r)); |
| 2340 | goto out; |
| 2341 | } |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2342 | fp = sshkey_fingerprint(key, options.fingerprint_hash, |
| 2343 | SSH_FP_DEFAULT); |
| 2344 | debug3("%s: received %s key %s", __func__, |
| 2345 | sshkey_type(key), fp); |
| 2346 | free(fp); |
| 2347 | /* Check that the key is accepted in HostkeyAlgorithms */ |
| 2348 | if (options.hostkeyalgorithms != NULL && |
| 2349 | match_pattern_list(sshkey_ssh_name(key), |
| 2350 | options.hostkeyalgorithms, |
| 2351 | strlen(options.hostkeyalgorithms), 0) != 1) { |
| 2352 | debug3("%s: %s key not permitted by HostkeyAlgorithms", |
| 2353 | __func__, sshkey_ssh_name(key)); |
| 2354 | continue; |
| 2355 | } |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2356 | /* Skip certs */ |
| 2357 | if (sshkey_is_cert(key)) { |
| 2358 | debug3("%s: %s key is a certificate; skipping", |
| 2359 | __func__, sshkey_ssh_name(key)); |
| 2360 | continue; |
| 2361 | } |
| 2362 | /* Ensure keys are unique */ |
| 2363 | for (i = 0; i < ctx->nkeys; i++) { |
| 2364 | if (sshkey_equal(key, ctx->keys[i])) { |
| 2365 | error("%s: received duplicated %s host key", |
| 2366 | __func__, sshkey_ssh_name(key)); |
| 2367 | goto out; |
| 2368 | } |
| 2369 | } |
| 2370 | /* Key is good, record it */ |
| 2371 | if ((tmp = reallocarray(ctx->keys, ctx->nkeys + 1, |
| 2372 | sizeof(*ctx->keys))) == NULL) |
| 2373 | fatal("%s: reallocarray failed nkeys = %zu", |
| 2374 | __func__, ctx->nkeys); |
| 2375 | ctx->keys = tmp; |
| 2376 | ctx->keys[ctx->nkeys++] = key; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2377 | key = NULL; |
| 2378 | } |
| 2379 | |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2380 | if (ctx->nkeys == 0) { |
djm@openbsd.org | 44732de | 2015-02-20 22:17:21 +0000 | [diff] [blame^] | 2381 | debug("%s: server sent no hostkeys", __func__); |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2382 | goto out; |
| 2383 | } |
djm@openbsd.org | 44732de | 2015-02-20 22:17:21 +0000 | [diff] [blame^] | 2384 | |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2385 | if ((ctx->keys_seen = calloc(ctx->nkeys, |
| 2386 | sizeof(*ctx->keys_seen))) == NULL) |
| 2387 | fatal("%s: calloc failed", __func__); |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2388 | |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 2389 | get_hostfile_hostname_ipaddr(host, |
| 2390 | options.check_host_ip ? (struct sockaddr *)&hostaddr : NULL, |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2391 | options.port, &ctx->host_str, |
| 2392 | options.check_host_ip ? &ctx->ip_str : NULL); |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2393 | |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2394 | /* Find which keys we already know about. */ |
| 2395 | if ((r = hostkeys_foreach(options.user_hostfiles[0], hostkeys_find, |
| 2396 | ctx, ctx->host_str, ctx->ip_str, |
| 2397 | HKF_WANT_PARSE_KEY|HKF_WANT_MATCH)) != 0) { |
| 2398 | error("%s: hostkeys_foreach failed: %s", __func__, ssh_err(r)); |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2399 | goto out; |
| 2400 | } |
| 2401 | |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2402 | /* Figure out if we have any new keys to add */ |
| 2403 | ctx->nnew = 0; |
| 2404 | for (i = 0; i < ctx->nkeys; i++) { |
| 2405 | if (!ctx->keys_seen[i]) |
| 2406 | ctx->nnew++; |
| 2407 | } |
| 2408 | |
| 2409 | debug3("%s: %zu keys from server: %zu new, %zu retained. %zu to remove", |
| 2410 | __func__, ctx->nkeys, ctx->nnew, ctx->nkeys - ctx->nnew, ctx->nold); |
| 2411 | |
| 2412 | if (ctx->nnew == 0 && ctx->nold != 0) { |
| 2413 | /* We have some keys to remove. Just do it. */ |
| 2414 | update_known_hosts(ctx); |
| 2415 | } else if (ctx->nnew != 0) { |
| 2416 | /* |
| 2417 | * We have received hitherto-unseen keys from the server. |
| 2418 | * Ask the server to confirm ownership of the private halves. |
| 2419 | */ |
| 2420 | debug3("%s: asking server to prove ownership for %zu keys", |
| 2421 | __func__, ctx->nnew); |
| 2422 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 2423 | (r = sshpkt_put_cstring(ssh, |
djm@openbsd.org | 44732de | 2015-02-20 22:17:21 +0000 | [diff] [blame^] | 2424 | "hostkeys-prove-00@openssh.com")) != 0 || |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2425 | (r = sshpkt_put_u8(ssh, 1)) != 0) /* bool: want reply */ |
| 2426 | fatal("%s: cannot prepare packet: %s", |
| 2427 | __func__, ssh_err(r)); |
| 2428 | if ((buf = sshbuf_new()) == NULL) |
| 2429 | fatal("%s: sshbuf_new", __func__); |
| 2430 | for (i = 0; i < ctx->nkeys; i++) { |
| 2431 | if (ctx->keys_seen[i]) |
| 2432 | continue; |
| 2433 | sshbuf_reset(buf); |
| 2434 | if ((r = sshkey_putb(ctx->keys[i], buf)) != 0) |
| 2435 | fatal("%s: sshkey_putb: %s", |
| 2436 | __func__, ssh_err(r)); |
| 2437 | if ((r = sshpkt_put_stringb(ssh, buf)) != 0) |
| 2438 | fatal("%s: sshpkt_put_string: %s", |
| 2439 | __func__, ssh_err(r)); |
| 2440 | } |
| 2441 | if ((r = sshpkt_send(ssh)) != 0) |
| 2442 | fatal("%s: sshpkt_send: %s", __func__, ssh_err(r)); |
| 2443 | client_register_global_confirm( |
| 2444 | client_global_hostkeys_private_confirm, ctx); |
| 2445 | ctx = NULL; /* will be freed in callback */ |
| 2446 | } |
| 2447 | |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2448 | /* Success */ |
| 2449 | out: |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2450 | hostkeys_update_ctx_free(ctx); |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2451 | sshkey_free(key); |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2452 | sshbuf_free(buf); |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2453 | /* |
| 2454 | * NB. Return success for all cases. The server doesn't need to know |
| 2455 | * what the client does with its hosts file. |
| 2456 | */ |
| 2457 | return 1; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2458 | } |
| 2459 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2460 | static int |
Damien Miller | c3fa407 | 2002-01-22 23:21:58 +1100 | [diff] [blame] | 2461 | client_input_global_request(int type, u_int32_t seq, void *ctxt) |
| 2462 | { |
| 2463 | char *rtype; |
| 2464 | int want_reply; |
| 2465 | int success = 0; |
| 2466 | |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2467 | rtype = packet_get_cstring(NULL); |
Damien Miller | c3fa407 | 2002-01-22 23:21:58 +1100 | [diff] [blame] | 2468 | want_reply = packet_get_char(); |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 2469 | debug("client_input_global_request: rtype %s want_reply %d", |
| 2470 | rtype, want_reply); |
djm@openbsd.org | 44732de | 2015-02-20 22:17:21 +0000 | [diff] [blame^] | 2471 | if (strcmp(rtype, "hostkeys-00@openssh.com") == 0) |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2472 | success = client_input_hostkeys(); |
Damien Miller | c3fa407 | 2002-01-22 23:21:58 +1100 | [diff] [blame] | 2473 | if (want_reply) { |
| 2474 | packet_start(success ? |
| 2475 | SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE); |
| 2476 | packet_send(); |
| 2477 | packet_write_wait(); |
| 2478 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2479 | free(rtype); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2480 | return 0; |
Damien Miller | c3fa407 | 2002-01-22 23:21:58 +1100 | [diff] [blame] | 2481 | } |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2482 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2483 | void |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 2484 | client_session2_setup(int id, int want_tty, int want_subsystem, |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 2485 | 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] | 2486 | { |
| 2487 | int len; |
Darren Tucker | 5d78de6 | 2004-11-05 20:35:44 +1100 | [diff] [blame] | 2488 | Channel *c = NULL; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2489 | |
| 2490 | debug2("%s: id %d", __func__, id); |
| 2491 | |
Darren Tucker | 5d78de6 | 2004-11-05 20:35:44 +1100 | [diff] [blame] | 2492 | if ((c = channel_lookup(id)) == NULL) |
| 2493 | fatal("client_session2_setup: channel %d: unknown channel", id); |
| 2494 | |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 2495 | packet_set_interactive(want_tty, |
| 2496 | options.ip_qos_interactive, options.ip_qos_bulk); |
| 2497 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2498 | if (want_tty) { |
| 2499 | struct winsize ws; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2500 | |
| 2501 | /* Store window size in the packet. */ |
| 2502 | if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0) |
| 2503 | memset(&ws, 0, sizeof(ws)); |
| 2504 | |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 2505 | channel_request_start(id, "pty-req", 1); |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 2506 | client_expect_confirm(id, "PTY allocation", CONFIRM_TTY); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2507 | packet_put_cstring(term != NULL ? term : ""); |
Damien Miller | 71a7367 | 2006-03-26 14:04:36 +1100 | [diff] [blame] | 2508 | packet_put_int((u_int)ws.ws_col); |
| 2509 | packet_put_int((u_int)ws.ws_row); |
| 2510 | packet_put_int((u_int)ws.ws_xpixel); |
| 2511 | packet_put_int((u_int)ws.ws_ypixel); |
Darren Tucker | df189fb | 2008-06-08 12:55:32 +1000 | [diff] [blame] | 2512 | if (tiop == NULL) |
| 2513 | tiop = get_saved_tio(); |
| 2514 | tty_make_modes(-1, tiop); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2515 | packet_send(); |
| 2516 | /* XXX wait for reply */ |
Darren Tucker | 5d78de6 | 2004-11-05 20:35:44 +1100 | [diff] [blame] | 2517 | c->client_tty = 1; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2518 | } |
| 2519 | |
| 2520 | /* Transfer any environment variables from client to server */ |
Damien Miller | 3756dce | 2004-06-18 01:17:29 +1000 | [diff] [blame] | 2521 | if (options.num_send_env != 0 && env != NULL) { |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2522 | int i, j, matched; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2523 | char *name, *val; |
| 2524 | |
| 2525 | debug("Sending environment."); |
Damien Miller | 3756dce | 2004-06-18 01:17:29 +1000 | [diff] [blame] | 2526 | for (i = 0; env[i] != NULL; i++) { |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2527 | /* Split */ |
Damien Miller | 3756dce | 2004-06-18 01:17:29 +1000 | [diff] [blame] | 2528 | name = xstrdup(env[i]); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2529 | if ((val = strchr(name, '=')) == NULL) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2530 | free(name); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2531 | continue; |
| 2532 | } |
| 2533 | *val++ = '\0'; |
| 2534 | |
| 2535 | matched = 0; |
| 2536 | for (j = 0; j < options.num_send_env; j++) { |
| 2537 | if (match_pattern(name, options.send_env[j])) { |
| 2538 | matched = 1; |
| 2539 | break; |
| 2540 | } |
| 2541 | } |
| 2542 | if (!matched) { |
| 2543 | debug3("Ignored env %s", name); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2544 | free(name); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2545 | continue; |
| 2546 | } |
| 2547 | |
| 2548 | debug("Sending env %s = %s", name, val); |
| 2549 | channel_request_start(id, "env", 0); |
| 2550 | packet_put_cstring(name); |
| 2551 | packet_put_cstring(val); |
| 2552 | packet_send(); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2553 | free(name); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2554 | } |
| 2555 | } |
| 2556 | |
| 2557 | len = buffer_len(cmd); |
| 2558 | if (len > 0) { |
| 2559 | if (len > 900) |
| 2560 | len = 900; |
| 2561 | if (want_subsystem) { |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 2562 | debug("Sending subsystem: %.*s", |
| 2563 | len, (u_char*)buffer_ptr(cmd)); |
| 2564 | channel_request_start(id, "subsystem", 1); |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 2565 | client_expect_confirm(id, "subsystem", CONFIRM_CLOSE); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2566 | } else { |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 2567 | debug("Sending command: %.*s", |
| 2568 | len, (u_char*)buffer_ptr(cmd)); |
| 2569 | channel_request_start(id, "exec", 1); |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 2570 | client_expect_confirm(id, "exec", CONFIRM_CLOSE); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2571 | } |
| 2572 | packet_put_string(buffer_ptr(cmd), buffer_len(cmd)); |
| 2573 | packet_send(); |
| 2574 | } else { |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 2575 | channel_request_start(id, "shell", 1); |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 2576 | client_expect_confirm(id, "shell", CONFIRM_CLOSE); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2577 | packet_send(); |
| 2578 | } |
| 2579 | } |
| 2580 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2581 | static void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 2582 | client_init_dispatch_20(void) |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 2583 | { |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 2584 | dispatch_init(&dispatch_protocol_error); |
Damien Miller | 2797f7f | 2002-04-23 21:09:44 +1000 | [diff] [blame] | 2585 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 2586 | dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose); |
| 2587 | dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data); |
| 2588 | dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof); |
| 2589 | dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2590 | dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 2591 | dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); |
| 2592 | dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 2593 | dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 2594 | dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 2595 | dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm); |
| 2596 | dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm); |
Damien Miller | c3fa407 | 2002-01-22 23:21:58 +1100 | [diff] [blame] | 2597 | dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request); |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 2598 | |
| 2599 | /* rekeying */ |
| 2600 | dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit); |
Damien Miller | 2797f7f | 2002-04-23 21:09:44 +1000 | [diff] [blame] | 2601 | |
| 2602 | /* global request reply messages */ |
| 2603 | dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply); |
| 2604 | dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 2605 | } |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 2606 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2607 | static void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 2608 | client_init_dispatch_13(void) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2609 | { |
| 2610 | dispatch_init(NULL); |
| 2611 | dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close); |
| 2612 | dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation); |
| 2613 | dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2614 | dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); |
| 2615 | dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); |
| 2616 | dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2617 | dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status); |
| 2618 | dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data); |
| 2619 | dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data); |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 2620 | |
| 2621 | dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ? |
Darren Tucker | 5dcdd21 | 2003-10-02 16:17:00 +1000 | [diff] [blame] | 2622 | &client_input_agent_open : &deny_input_open); |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 2623 | dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ? |
| 2624 | &x11_input_open : &deny_input_open); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2625 | } |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 2626 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2627 | static void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 2628 | client_init_dispatch_15(void) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2629 | { |
| 2630 | client_init_dispatch_13(); |
| 2631 | dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof); |
| 2632 | dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose); |
| 2633 | } |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 2634 | |
Ben Lindstrom | db47f38 | 2001-07-04 05:10:27 +0000 | [diff] [blame] | 2635 | static void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 2636 | client_init_dispatch(void) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2637 | { |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 2638 | if (compat20) |
| 2639 | client_init_dispatch_20(); |
| 2640 | else if (compat13) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2641 | client_init_dispatch_13(); |
| 2642 | else |
| 2643 | client_init_dispatch_15(); |
| 2644 | } |
Darren Tucker | 9a2c4cd | 2003-09-22 21:16:05 +1000 | [diff] [blame] | 2645 | |
Damien Miller | 6c3eec7 | 2011-05-05 14:16:22 +1000 | [diff] [blame] | 2646 | void |
| 2647 | client_stop_mux(void) |
| 2648 | { |
| 2649 | if (options.control_path != NULL && muxserver_sock != -1) |
| 2650 | unlink(options.control_path); |
| 2651 | /* |
Darren Tucker | 48bf4b0 | 2012-09-07 16:38:53 +1000 | [diff] [blame] | 2652 | * If we are in persist mode, or don't have a shell, signal that we |
| 2653 | * should close when all active channels are closed. |
Damien Miller | 6c3eec7 | 2011-05-05 14:16:22 +1000 | [diff] [blame] | 2654 | */ |
Darren Tucker | 48bf4b0 | 2012-09-07 16:38:53 +1000 | [diff] [blame] | 2655 | if (options.control_persist || no_shell_flag) { |
Damien Miller | 6c3eec7 | 2011-05-05 14:16:22 +1000 | [diff] [blame] | 2656 | session_closed = 1; |
Damien Miller | 4ac99c3 | 2011-06-20 14:43:31 +1000 | [diff] [blame] | 2657 | setproctitle("[stopped mux]"); |
| 2658 | } |
Damien Miller | 6c3eec7 | 2011-05-05 14:16:22 +1000 | [diff] [blame] | 2659 | } |
| 2660 | |
Darren Tucker | 9a2c4cd | 2003-09-22 21:16:05 +1000 | [diff] [blame] | 2661 | /* client specific fatal cleanup */ |
| 2662 | void |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 2663 | cleanup_exit(int i) |
Darren Tucker | 9a2c4cd | 2003-09-22 21:16:05 +1000 | [diff] [blame] | 2664 | { |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 2665 | leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); |
Darren Tucker | 9a2c4cd | 2003-09-22 21:16:05 +1000 | [diff] [blame] | 2666 | leave_non_blocking(); |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 2667 | if (options.control_path != NULL && muxserver_sock != -1) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2668 | unlink(options.control_path); |
Damien Miller | a41ccca | 2010-10-07 22:07:32 +1100 | [diff] [blame] | 2669 | ssh_kill_proxy_command(); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 2670 | _exit(i); |
Darren Tucker | 9a2c4cd | 2003-09-22 21:16:05 +1000 | [diff] [blame] | 2671 | } |