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