dtucker@openbsd.org | 58c8190 | 2020-02-02 09:22:22 +0000 | [diff] [blame^] | 1 | /* $OpenBSD: clientloop.c,v 1.339 2020/02/02 09:22:22 dtucker 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 | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 80 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 81 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 82 | #include <string.h> |
deraadt@openbsd.org | 72687c8 | 2019-11-13 04:47:52 +0000 | [diff] [blame] | 83 | #include <stdarg.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 "ssh2.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 93 | #include "packet.h" |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 94 | #include "sshbuf.h" |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 95 | #include "compat.h" |
Ben Lindstrom | c763767 | 2001-06-09 00:36:26 +0000 | [diff] [blame] | 96 | #include "channels.h" |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 97 | #include "dispatch.h" |
markus@openbsd.org | 5467fbc | 2018-07-11 18:53:29 +0000 | [diff] [blame] | 98 | #include "sshkey.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 99 | #include "cipher.h" |
Ben Lindstrom | f28f634 | 2001-04-04 02:03:04 +0000 | [diff] [blame] | 100 | #include "kex.h" |
markus@openbsd.org | 3a1638d | 2015-07-10 06:21:53 +0000 | [diff] [blame] | 101 | #include "myproposal.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 102 | #include "log.h" |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 103 | #include "misc.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 104 | #include "readconf.h" |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 105 | #include "clientloop.h" |
Damien Miller | aeb31d6 | 2005-12-13 19:29:36 +1100 | [diff] [blame] | 106 | #include "sshconnect.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 107 | #include "authfd.h" |
| 108 | #include "atomicio.h" |
Darren Tucker | 06f2bd8 | 2004-05-13 16:06:46 +1000 | [diff] [blame] | 109 | #include "sshpty.h" |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 110 | #include "match.h" |
| 111 | #include "msg.h" |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 112 | #include "ssherr.h" |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 113 | #include "hostfile.h" |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 114 | |
| 115 | /* import options */ |
| 116 | extern Options options; |
| 117 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 118 | /* Flag indicating that stdin should be redirected from /dev/null. */ |
| 119 | extern int stdin_null_flag; |
| 120 | |
Damien Miller | d696551 | 2003-12-17 16:31:53 +1100 | [diff] [blame] | 121 | /* Flag indicating that no shell has been requested */ |
| 122 | extern int no_shell_flag; |
| 123 | |
djm@openbsd.org | 368dd97 | 2016-07-23 02:54:08 +0000 | [diff] [blame] | 124 | /* Flag indicating that ssh should daemonise after authentication is complete */ |
| 125 | extern int fork_after_authentication_flag; |
| 126 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 127 | /* Control socket */ |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 128 | extern int muxserver_sock; /* XXX use mux_client_cleanup() instead */ |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 129 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 130 | /* |
| 131 | * Name of the host we are connecting to. This is the name given on the |
jmc@openbsd.org | 7349149 | 2019-06-12 11:31:50 +0000 | [diff] [blame] | 132 | * command line, or the Hostname specified for the user-supplied name in a |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 133 | * configuration file. |
| 134 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 135 | extern char *host; |
| 136 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 137 | /* |
djm@openbsd.org | 40be78f | 2019-12-21 02:19:13 +0000 | [diff] [blame] | 138 | * If this field is not NULL, the ForwardAgent socket is this path and different |
| 139 | * instead of SSH_AUTH_SOCK. |
| 140 | */ |
| 141 | extern char *forward_agent_sock_path; |
| 142 | |
| 143 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 144 | * Flag to indicate that we have received a window change signal which has |
| 145 | * not yet been processed. This will cause a message indicating the new |
| 146 | * window size to be sent to the server a little later. This is volatile |
| 147 | * because this is updated in a signal handler. |
| 148 | */ |
Ben Lindstrom | 5e71c54 | 2001-12-06 16:48:14 +0000 | [diff] [blame] | 149 | static volatile sig_atomic_t received_window_change_signal = 0; |
| 150 | static volatile sig_atomic_t received_signal = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 151 | |
Damien Miller | 788f212 | 2005-11-05 15:14:59 +1100 | [diff] [blame] | 152 | /* Flag indicating whether the user's terminal is in non-blocking mode. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 153 | static int in_non_blocking_mode = 0; |
| 154 | |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 155 | /* Time when backgrounded control master using ControlPersist should exit */ |
| 156 | static time_t control_persist_exit_time = 0; |
| 157 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 158 | /* Common data for the client loop code. */ |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 159 | volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 160 | static int last_was_cr; /* Last character was a newline. */ |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 161 | static int exit_status; /* Used to store the command exit status. */ |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 162 | static struct sshbuf *stderr_buffer; /* Used for final exit message. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 163 | static int connection_in; /* Connection to server (input). */ |
| 164 | static int connection_out; /* Connection to server (output). */ |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 165 | static int need_rekeying; /* Set to non-zero if rekeying is requested. */ |
Damien Miller | 1ab6a51 | 2010-06-26 10:02:24 +1000 | [diff] [blame] | 166 | static int session_closed; /* In SSH2: login session closed. */ |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 167 | 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] | 168 | |
djm@openbsd.org | bc5e116 | 2019-01-19 21:39:12 +0000 | [diff] [blame] | 169 | static void client_init_dispatch(struct ssh *ssh); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 170 | int session_ident = -1; |
| 171 | |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 172 | /* Track escape per proto2 channel */ |
| 173 | struct escape_filter_ctx { |
| 174 | int escape_pending; |
| 175 | int escape_char; |
| 176 | }; |
| 177 | |
| 178 | /* Context for channel confirmation replies */ |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 179 | struct channel_reply_ctx { |
| 180 | const char *request_type; |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 181 | int id; |
| 182 | enum confirm_action action; |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 183 | }; |
| 184 | |
Darren Tucker | 9f407c4 | 2008-06-13 04:50:27 +1000 | [diff] [blame] | 185 | /* Global request success/failure callbacks */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 186 | /* XXX move to struct ssh? */ |
Darren Tucker | 9f407c4 | 2008-06-13 04:50:27 +1000 | [diff] [blame] | 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 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 197 | void ssh_process_session2_setup(int, int, int, struct sshbuf *); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 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 | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 210 | /* |
| 211 | * Signal handler for the window change signal (SIGWINCH). This just sets a |
| 212 | * flag indicating that the window has changed. |
| 213 | */ |
Damien Miller | f0b15df | 2006-03-26 13:59:20 +1100 | [diff] [blame] | 214 | /*ARGSUSED */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 215 | static void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 216 | window_change_handler(int sig) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 217 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 218 | received_window_change_signal = 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 219 | } |
| 220 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 221 | /* |
| 222 | * Signal handler for signals that cause the program to terminate. These |
| 223 | * signals must be trapped to restore terminal modes. |
| 224 | */ |
Damien Miller | f0b15df | 2006-03-26 13:59:20 +1100 | [diff] [blame] | 225 | /*ARGSUSED */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 226 | static void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 227 | signal_handler(int sig) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 228 | { |
Ben Lindstrom | ec46e0b | 2001-06-09 01:27:31 +0000 | [diff] [blame] | 229 | received_signal = sig; |
| 230 | quit_pending = 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 231 | } |
| 232 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 233 | /* |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 234 | * Sets control_persist_exit_time to the absolute time when the |
| 235 | * backgrounded control master should exit due to expiry of the |
| 236 | * ControlPersist timeout. Sets it to 0 if we are not a backgrounded |
| 237 | * control master process, or if there is no ControlPersist timeout. |
| 238 | */ |
| 239 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 240 | set_control_persist_exit_time(struct ssh *ssh) |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 241 | { |
| 242 | if (muxserver_sock == -1 || !options.control_persist |
Damien Miller | 6c3eec7 | 2011-05-05 14:16:22 +1000 | [diff] [blame] | 243 | || options.control_persist_timeout == 0) { |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 244 | /* not using a ControlPersist timeout */ |
| 245 | control_persist_exit_time = 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 246 | } else if (channel_still_open(ssh)) { |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 247 | /* some client connections are still open */ |
| 248 | if (control_persist_exit_time > 0) |
| 249 | debug2("%s: cancel scheduled exit", __func__); |
| 250 | control_persist_exit_time = 0; |
| 251 | } else if (control_persist_exit_time <= 0) { |
| 252 | /* a client connection has recently closed */ |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 253 | control_persist_exit_time = monotime() + |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 254 | (time_t)options.control_persist_timeout; |
| 255 | debug2("%s: schedule exit in %d seconds", __func__, |
| 256 | options.control_persist_timeout); |
| 257 | } |
| 258 | /* else we are already counting down to the timeout */ |
| 259 | } |
| 260 | |
Damien Miller | 8d60be5 | 2012-02-11 08:18:17 +1100 | [diff] [blame] | 261 | #define SSH_X11_VALID_DISPLAY_CHARS ":/.-_" |
| 262 | static int |
| 263 | client_x11_display_valid(const char *display) |
| 264 | { |
| 265 | size_t i, dlen; |
| 266 | |
djm@openbsd.org | ed4ce82 | 2016-01-13 23:04:47 +0000 | [diff] [blame] | 267 | if (display == NULL) |
| 268 | return 0; |
| 269 | |
Damien Miller | 8d60be5 | 2012-02-11 08:18:17 +1100 | [diff] [blame] | 270 | dlen = strlen(display); |
| 271 | for (i = 0; i < dlen; i++) { |
Damien Miller | fdb2306 | 2013-11-21 13:57:15 +1100 | [diff] [blame] | 272 | if (!isalnum((u_char)display[i]) && |
Damien Miller | 8d60be5 | 2012-02-11 08:18:17 +1100 | [diff] [blame] | 273 | strchr(SSH_X11_VALID_DISPLAY_CHARS, display[i]) == NULL) { |
| 274 | debug("Invalid character '%c' in DISPLAY", display[i]); |
| 275 | return 0; |
| 276 | } |
| 277 | } |
| 278 | return 1; |
| 279 | } |
| 280 | |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 281 | #define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1" |
| 282 | #define X11_TIMEOUT_SLACK 60 |
djm@openbsd.org | ed4ce82 | 2016-01-13 23:04:47 +0000 | [diff] [blame] | 283 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 284 | client_x11_get_proto(struct ssh *ssh, const char *display, |
| 285 | const char *xauth_path, u_int trusted, u_int timeout, |
| 286 | char **_proto, char **_data) |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 287 | { |
djm@openbsd.org | b5e412a | 2018-09-21 12:46:22 +0000 | [diff] [blame] | 288 | char *cmd, line[512], xdisplay[512]; |
djm@openbsd.org | ed4ce82 | 2016-01-13 23:04:47 +0000 | [diff] [blame] | 289 | char xauthfile[PATH_MAX], xauthdir[PATH_MAX]; |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 290 | static char proto[512], data[512]; |
| 291 | FILE *f; |
tedu@openbsd.org | 1036356 | 2016-09-17 18:00:27 +0000 | [diff] [blame] | 292 | int got_data = 0, generated = 0, do_unlink = 0, r; |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 293 | struct stat st; |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 294 | u_int now, x11_timeout_real; |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 295 | |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 296 | *_proto = proto; |
| 297 | *_data = data; |
djm@openbsd.org | ed4ce82 | 2016-01-13 23:04:47 +0000 | [diff] [blame] | 298 | proto[0] = data[0] = xauthfile[0] = xauthdir[0] = '\0'; |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 299 | |
djm@openbsd.org | ed4ce82 | 2016-01-13 23:04:47 +0000 | [diff] [blame] | 300 | if (!client_x11_display_valid(display)) { |
millert@openbsd.org | 5658ef2 | 2016-02-01 21:18:17 +0000 | [diff] [blame] | 301 | if (display != NULL) |
| 302 | logit("DISPLAY \"%s\" invalid; disabling X11 forwarding", |
| 303 | display); |
djm@openbsd.org | ed4ce82 | 2016-01-13 23:04:47 +0000 | [diff] [blame] | 304 | return -1; |
| 305 | } |
| 306 | if (xauth_path != NULL && stat(xauth_path, &st) == -1) { |
| 307 | debug("No xauth program."); |
| 308 | xauth_path = NULL; |
| 309 | } |
| 310 | |
| 311 | if (xauth_path != NULL) { |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 312 | /* |
| 313 | * Handle FamilyLocal case where $DISPLAY does |
| 314 | * not match an authorization entry. For this we |
| 315 | * just try "xauth list unix:displaynum.screennum". |
| 316 | * XXX: "localhost" match to determine FamilyLocal |
| 317 | * is not perfect. |
| 318 | */ |
| 319 | if (strncmp(display, "localhost:", 10) == 0) { |
djm@openbsd.org | ed4ce82 | 2016-01-13 23:04:47 +0000 | [diff] [blame] | 320 | if ((r = snprintf(xdisplay, sizeof(xdisplay), "unix:%s", |
| 321 | display + 10)) < 0 || |
| 322 | (size_t)r >= sizeof(xdisplay)) { |
| 323 | error("%s: display name too long", __func__); |
| 324 | return -1; |
| 325 | } |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 326 | display = xdisplay; |
| 327 | } |
| 328 | if (trusted == 0) { |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 329 | /* |
djm@openbsd.org | ed4ce82 | 2016-01-13 23:04:47 +0000 | [diff] [blame] | 330 | * Generate an untrusted X11 auth cookie. |
| 331 | * |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 332 | * The authentication cookie should briefly outlive |
| 333 | * ssh's willingness to forward X11 connections to |
| 334 | * avoid nasty fail-open behaviour in the X server. |
| 335 | */ |
djm@openbsd.org | ed4ce82 | 2016-01-13 23:04:47 +0000 | [diff] [blame] | 336 | mktemp_proto(xauthdir, sizeof(xauthdir)); |
| 337 | if (mkdtemp(xauthdir) == NULL) { |
| 338 | error("%s: mkdtemp: %s", |
| 339 | __func__, strerror(errno)); |
| 340 | return -1; |
| 341 | } |
| 342 | do_unlink = 1; |
| 343 | if ((r = snprintf(xauthfile, sizeof(xauthfile), |
| 344 | "%s/xauthfile", xauthdir)) < 0 || |
| 345 | (size_t)r >= sizeof(xauthfile)) { |
| 346 | error("%s: xauthfile path too long", __func__); |
djm@openbsd.org | ed4ce82 | 2016-01-13 23:04:47 +0000 | [diff] [blame] | 347 | rmdir(xauthdir); |
| 348 | return -1; |
| 349 | } |
| 350 | |
djm@openbsd.org | b5e412a | 2018-09-21 12:46:22 +0000 | [diff] [blame] | 351 | if (timeout == 0) { |
| 352 | /* auth doesn't time out */ |
| 353 | xasprintf(&cmd, "%s -f %s generate %s %s " |
| 354 | "untrusted 2>%s", |
| 355 | xauth_path, xauthfile, display, |
| 356 | SSH_X11_PROTO, _PATH_DEVNULL); |
| 357 | } else { |
| 358 | /* Add some slack to requested expiry */ |
| 359 | if (timeout < UINT_MAX - X11_TIMEOUT_SLACK) |
| 360 | x11_timeout_real = timeout + |
| 361 | X11_TIMEOUT_SLACK; |
| 362 | else { |
| 363 | /* Don't overflow on long timeouts */ |
| 364 | x11_timeout_real = UINT_MAX; |
| 365 | } |
| 366 | xasprintf(&cmd, "%s -f %s generate %s %s " |
| 367 | "untrusted timeout %u 2>%s", |
| 368 | xauth_path, xauthfile, display, |
| 369 | SSH_X11_PROTO, x11_timeout_real, |
| 370 | _PATH_DEVNULL); |
| 371 | } |
djm@openbsd.org | 9d7b288 | 2019-03-29 11:31:40 +0000 | [diff] [blame] | 372 | debug2("%s: xauth command: %s", __func__, cmd); |
djm@openbsd.org | b5e412a | 2018-09-21 12:46:22 +0000 | [diff] [blame] | 373 | |
| 374 | if (timeout != 0 && x11_refuse_time == 0) { |
djm@openbsd.org | ed4ce82 | 2016-01-13 23:04:47 +0000 | [diff] [blame] | 375 | now = monotime() + 1; |
| 376 | if (UINT_MAX - timeout < now) |
| 377 | x11_refuse_time = UINT_MAX; |
| 378 | else |
| 379 | x11_refuse_time = now + timeout; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 380 | channel_set_x11_refuse_time(ssh, |
| 381 | x11_refuse_time); |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 382 | } |
djm@openbsd.org | ed4ce82 | 2016-01-13 23:04:47 +0000 | [diff] [blame] | 383 | if (system(cmd) == 0) |
| 384 | generated = 1; |
djm@openbsd.org | b5e412a | 2018-09-21 12:46:22 +0000 | [diff] [blame] | 385 | free(cmd); |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 386 | } |
Darren Tucker | 513d13a | 2007-08-15 19:13:41 +1000 | [diff] [blame] | 387 | |
| 388 | /* |
| 389 | * When in untrusted mode, we read the cookie only if it was |
| 390 | * successfully generated as an untrusted one in the step |
| 391 | * above. |
| 392 | */ |
| 393 | if (trusted || generated) { |
djm@openbsd.org | b5e412a | 2018-09-21 12:46:22 +0000 | [diff] [blame] | 394 | xasprintf(&cmd, |
Darren Tucker | 513d13a | 2007-08-15 19:13:41 +1000 | [diff] [blame] | 395 | "%s %s%s list %s 2>" _PATH_DEVNULL, |
| 396 | xauth_path, |
| 397 | generated ? "-f " : "" , |
| 398 | generated ? xauthfile : "", |
| 399 | display); |
| 400 | debug2("x11_get_proto: %s", cmd); |
| 401 | f = popen(cmd, "r"); |
| 402 | if (f && fgets(line, sizeof(line), f) && |
| 403 | sscanf(line, "%*s %511s %511s", proto, data) == 2) |
| 404 | got_data = 1; |
| 405 | if (f) |
| 406 | pclose(f); |
djm@openbsd.org | b5e412a | 2018-09-21 12:46:22 +0000 | [diff] [blame] | 407 | free(cmd); |
djm@openbsd.org | ed4ce82 | 2016-01-13 23:04:47 +0000 | [diff] [blame] | 408 | } |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | if (do_unlink) { |
| 412 | unlink(xauthfile); |
| 413 | rmdir(xauthdir); |
| 414 | } |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 415 | |
djm@openbsd.org | ed4ce82 | 2016-01-13 23:04:47 +0000 | [diff] [blame] | 416 | /* Don't fall back to fake X11 data for untrusted forwarding */ |
| 417 | if (!trusted && !got_data) { |
| 418 | error("Warning: untrusted X11 forwarding setup failed: " |
| 419 | "xauth key data not generated"); |
| 420 | return -1; |
| 421 | } |
| 422 | |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 423 | /* |
| 424 | * If we didn't get authentication data, just make up some |
| 425 | * data. The forwarding code will check the validity of the |
| 426 | * response anyway, and substitute this data. The X11 |
| 427 | * server, however, will ignore this fake data and use |
| 428 | * whatever authentication mechanisms it was using otherwise |
| 429 | * for the local connection. |
| 430 | */ |
| 431 | if (!got_data) { |
tedu@openbsd.org | 1036356 | 2016-09-17 18:00:27 +0000 | [diff] [blame] | 432 | u_int8_t rnd[16]; |
| 433 | u_int i; |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 434 | |
| 435 | logit("Warning: No xauth data; " |
| 436 | "using fake authentication data for X11 forwarding."); |
| 437 | strlcpy(proto, SSH_X11_PROTO, sizeof proto); |
tedu@openbsd.org | 1036356 | 2016-09-17 18:00:27 +0000 | [diff] [blame] | 438 | arc4random_buf(rnd, sizeof(rnd)); |
| 439 | for (i = 0; i < sizeof(rnd); i++) { |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 440 | snprintf(data + 2 * i, sizeof data - 2 * i, "%02x", |
tedu@openbsd.org | 1036356 | 2016-09-17 18:00:27 +0000 | [diff] [blame] | 441 | rnd[i]); |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 442 | } |
| 443 | } |
djm@openbsd.org | ed4ce82 | 2016-01-13 23:04:47 +0000 | [diff] [blame] | 444 | |
| 445 | return 0; |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 446 | } |
| 447 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 448 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 449 | * Checks if the client window has changed, and sends a packet about it to |
| 450 | * the server if so. The actual change is detected elsewhere (by a software |
| 451 | * interrupt on Unix); this just checks the flag and sends a message if |
| 452 | * appropriate. |
| 453 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 454 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 455 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 456 | client_check_window_change(struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 457 | { |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 458 | if (!received_window_change_signal) |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 459 | return; |
| 460 | /** XXX race */ |
| 461 | received_window_change_signal = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 462 | |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 463 | debug2("%s: changed", __func__); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 464 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 465 | channel_send_window_changes(ssh); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 466 | } |
| 467 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 468 | static int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 469 | client_global_request_reply(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 470 | { |
Darren Tucker | 9f407c4 | 2008-06-13 04:50:27 +1000 | [diff] [blame] | 471 | struct global_confirm *gc; |
| 472 | |
| 473 | if ((gc = TAILQ_FIRST(&global_confirms)) == NULL) |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 474 | return 0; |
Darren Tucker | 9f407c4 | 2008-06-13 04:50:27 +1000 | [diff] [blame] | 475 | if (gc->cb != NULL) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 476 | gc->cb(ssh, type, seq, gc->ctx); |
Darren Tucker | 9f407c4 | 2008-06-13 04:50:27 +1000 | [diff] [blame] | 477 | if (--gc->ref_count <= 0) { |
| 478 | TAILQ_REMOVE(&global_confirms, gc, entry); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 479 | explicit_bzero(gc, sizeof(*gc)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 480 | free(gc); |
Darren Tucker | 9f407c4 | 2008-06-13 04:50:27 +1000 | [diff] [blame] | 481 | } |
| 482 | |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 483 | ssh_packet_set_alive_timeouts(ssh, 0); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 484 | return 0; |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | static void |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 488 | server_alive_check(struct ssh *ssh) |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 489 | { |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 490 | int r; |
| 491 | |
| 492 | if (ssh_packet_inc_alive_timeouts(ssh) > options.server_alive_count_max) { |
Damien Miller | b73b6fd | 2011-01-11 17:18:56 +1100 | [diff] [blame] | 493 | logit("Timeout, server %s not responding.", host); |
Damien Miller | 985a448 | 2006-10-24 03:02:41 +1000 | [diff] [blame] | 494 | cleanup_exit(255); |
| 495 | } |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 496 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 497 | (r = sshpkt_put_cstring(ssh, "keepalive@openssh.com")) != 0 || |
| 498 | (r = sshpkt_put_u8(ssh, 1)) != 0 || /* boolean: want reply */ |
| 499 | (r = sshpkt_send(ssh)) != 0) |
djm@openbsd.org | 9d7b288 | 2019-03-29 11:31:40 +0000 | [diff] [blame] | 500 | fatal("%s: send packet: %s", __func__, ssh_err(r)); |
Darren Tucker | 9f407c4 | 2008-06-13 04:50:27 +1000 | [diff] [blame] | 501 | /* Insert an empty placeholder to maintain ordering */ |
| 502 | client_register_global_confirm(NULL, NULL); |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 503 | } |
| 504 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 505 | /* |
| 506 | * Waits until the client can do something (some data becomes available on |
| 507 | * one of the file descriptors). |
| 508 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 509 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 510 | client_wait_until_can_do_something(struct ssh *ssh, |
| 511 | fd_set **readsetp, fd_set **writesetp, |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 512 | int *maxfdp, u_int *nallocp, int rekeying) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 513 | { |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 514 | struct timeval tv, *tvp; |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 515 | int timeout_secs; |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 516 | time_t minwait_secs = 0, server_alive_time = 0, now = monotime(); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 517 | int r, ret; |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 518 | |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 519 | /* Add any selections by the channel mechanism. */ |
djm@openbsd.org | bc5e116 | 2019-01-19 21:39:12 +0000 | [diff] [blame] | 520 | channel_prepare_select(ssh, readsetp, writesetp, maxfdp, |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 521 | nallocp, &minwait_secs); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 522 | |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 523 | /* channel_prepare_select could have closed the last channel */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 524 | if (session_closed && !channel_still_open(ssh) && |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 525 | !ssh_packet_have_data_to_write(ssh)) { |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 526 | /* clear mask since we did not call select() */ |
| 527 | memset(*readsetp, 0, *nallocp); |
| 528 | memset(*writesetp, 0, *nallocp); |
| 529 | return; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 530 | } |
| 531 | |
djm@openbsd.org | 0516435 | 2017-04-30 23:21:54 +0000 | [diff] [blame] | 532 | FD_SET(connection_in, *readsetp); |
| 533 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 534 | /* Select server connection if have data to write to the server. */ |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 535 | if (ssh_packet_have_data_to_write(ssh)) |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 536 | FD_SET(connection_out, *writesetp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 537 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 538 | /* |
| 539 | * Wait for something to happen. This will suspend the process until |
| 540 | * some selected descriptor can be read, written, or has some other |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 541 | * event pending, or a timeout expires. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 542 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 543 | |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 544 | timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 545 | if (options.server_alive_interval > 0) { |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 546 | timeout_secs = options.server_alive_interval; |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 547 | server_alive_time = now + options.server_alive_interval; |
| 548 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 549 | if (options.rekey_interval > 0 && !rekeying) |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 550 | timeout_secs = MINIMUM(timeout_secs, |
| 551 | ssh_packet_get_rekey_timeout(ssh)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 552 | set_control_persist_exit_time(ssh); |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 553 | if (control_persist_exit_time > 0) { |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 554 | timeout_secs = MINIMUM(timeout_secs, |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 555 | control_persist_exit_time - now); |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 556 | if (timeout_secs < 0) |
| 557 | timeout_secs = 0; |
| 558 | } |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 559 | if (minwait_secs != 0) |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 560 | timeout_secs = MINIMUM(timeout_secs, (int)minwait_secs); |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 561 | if (timeout_secs == INT_MAX) |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 562 | tvp = NULL; |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 563 | else { |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 564 | tv.tv_sec = timeout_secs; |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 565 | tv.tv_usec = 0; |
| 566 | tvp = &tv; |
| 567 | } |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 568 | |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 569 | ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp); |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 570 | if (ret == -1) { |
Ben Lindstrom | f945251 | 2001-02-15 03:12:08 +0000 | [diff] [blame] | 571 | /* |
| 572 | * We have to clear the select masks, because we return. |
| 573 | * We have to return, because the mainloop checks for the flags |
| 574 | * set by the signal handlers. |
| 575 | */ |
Damien Miller | 79faeff | 2001-11-12 11:06:32 +1100 | [diff] [blame] | 576 | memset(*readsetp, 0, *nallocp); |
| 577 | memset(*writesetp, 0, *nallocp); |
Ben Lindstrom | f945251 | 2001-02-15 03:12:08 +0000 | [diff] [blame] | 578 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 579 | if (errno == EINTR) |
| 580 | return; |
| 581 | /* Note: we might still have data in the buffers. */ |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 582 | if ((r = sshbuf_putf(stderr_buffer, |
| 583 | "select: %s\r\n", strerror(errno))) != 0) |
| 584 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 585 | quit_pending = 1; |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 586 | } else if (ret == 0) { |
| 587 | /* |
| 588 | * Timeout. Could have been either keepalive or rekeying. |
| 589 | * Keepalive we check here, rekeying is checked in clientloop. |
| 590 | */ |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 591 | if (server_alive_time != 0 && server_alive_time <= monotime()) |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 592 | server_alive_check(ssh); |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 593 | } |
| 594 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 595 | } |
| 596 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 597 | static void |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 598 | client_suspend_self(struct sshbuf *bin, struct sshbuf *bout, struct sshbuf *berr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 599 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 600 | /* Flush stdout and stderr buffers. */ |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 601 | if (sshbuf_len(bout) > 0) |
| 602 | atomicio(vwrite, fileno(stdout), sshbuf_mutable_ptr(bout), |
| 603 | sshbuf_len(bout)); |
| 604 | if (sshbuf_len(berr) > 0) |
| 605 | atomicio(vwrite, fileno(stderr), sshbuf_mutable_ptr(berr), |
| 606 | sshbuf_len(berr)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 607 | |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 608 | leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 609 | |
djm@openbsd.org | 5b8da1f | 2017-09-19 04:24:22 +0000 | [diff] [blame] | 610 | sshbuf_reset(bin); |
| 611 | sshbuf_reset(bout); |
| 612 | sshbuf_reset(berr); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 613 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 614 | /* Send the suspend signal to the program itself. */ |
| 615 | kill(getpid(), SIGTSTP); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 616 | |
Darren Tucker | 5d78de6 | 2004-11-05 20:35:44 +1100 | [diff] [blame] | 617 | /* Reset window sizes in case they have changed */ |
| 618 | received_window_change_signal = 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 619 | |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 620 | enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 621 | } |
| 622 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 623 | static void |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 624 | client_process_net_input(struct ssh *ssh, fd_set *readset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 625 | { |
Darren Tucker | 86e30a0 | 2009-08-28 11:21:06 +1000 | [diff] [blame] | 626 | char buf[SSH_IOBUFSZ]; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 627 | int r, len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 628 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 629 | /* |
| 630 | * Read input from the server, and add any such data to the buffer of |
| 631 | * the packet subsystem. |
| 632 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 633 | if (FD_ISSET(connection_in, readset)) { |
| 634 | /* Read as much as possible. */ |
markus@openbsd.org | a306863 | 2016-01-14 16:17:39 +0000 | [diff] [blame] | 635 | len = read(connection_in, buf, sizeof(buf)); |
| 636 | if (len == 0) { |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 637 | /* |
| 638 | * Received EOF. The remote host has closed the |
| 639 | * connection. |
| 640 | */ |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 641 | if ((r = sshbuf_putf(stderr_buffer, |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 642 | "Connection to %.300s closed by remote host.\r\n", |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 643 | host)) != 0) |
| 644 | fatal("%s: buffer error: %s", |
| 645 | __func__, ssh_err(r)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 646 | quit_pending = 1; |
| 647 | return; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 648 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 649 | /* |
| 650 | * There is a kernel bug on Solaris that causes select to |
| 651 | * sometimes wake up even though there is no data available. |
| 652 | */ |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 653 | if (len == -1 && |
Damien Miller | d8968ad | 2008-07-04 23:10:49 +1000 | [diff] [blame] | 654 | (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 655 | len = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 656 | |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 657 | if (len == -1) { |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 658 | /* |
| 659 | * An error has encountered. Perhaps there is a |
| 660 | * network problem. |
| 661 | */ |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 662 | if ((r = sshbuf_putf(stderr_buffer, |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 663 | "Read from remote host %.300s: %.100s\r\n", |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 664 | host, strerror(errno))) != 0) |
| 665 | fatal("%s: buffer error: %s", |
| 666 | __func__, ssh_err(r)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 667 | quit_pending = 1; |
| 668 | return; |
| 669 | } |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 670 | ssh_packet_process_incoming(ssh, buf, len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 671 | } |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 672 | } |
| 673 | |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 674 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 675 | client_status_confirm(struct ssh *ssh, int type, Channel *c, void *ctx) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 676 | { |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 677 | struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx; |
| 678 | char errmsg[256]; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 679 | int r, tochan; |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 680 | |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 681 | /* |
| 682 | * If a TTY was explicitly requested, then a failure to allocate |
| 683 | * one is fatal. |
| 684 | */ |
| 685 | if (cr->action == CONFIRM_TTY && |
| 686 | (options.request_tty == REQUEST_TTY_FORCE || |
| 687 | options.request_tty == REQUEST_TTY_YES)) |
| 688 | cr->action = CONFIRM_CLOSE; |
| 689 | |
djm@openbsd.org | 001aa55 | 2018-04-10 00:10:49 +0000 | [diff] [blame] | 690 | /* XXX suppress on mux _client_ quietmode */ |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 691 | tochan = options.log_level >= SYSLOG_LEVEL_ERROR && |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 692 | c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 693 | |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 694 | if (type == SSH2_MSG_CHANNEL_SUCCESS) { |
| 695 | debug2("%s request accepted on channel %d", |
| 696 | cr->request_type, c->self); |
| 697 | } else if (type == SSH2_MSG_CHANNEL_FAILURE) { |
| 698 | if (tochan) { |
| 699 | snprintf(errmsg, sizeof(errmsg), |
| 700 | "%s request failed\r\n", cr->request_type); |
| 701 | } else { |
| 702 | snprintf(errmsg, sizeof(errmsg), |
| 703 | "%s request failed on channel %d", |
| 704 | cr->request_type, c->self); |
| 705 | } |
| 706 | /* If error occurred on primary session channel, then exit */ |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 707 | if (cr->action == CONFIRM_CLOSE && c->self == session_ident) |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 708 | fatal("%s", errmsg); |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 709 | /* |
| 710 | * If error occurred on mux client, append to |
| 711 | * their stderr. |
| 712 | */ |
| 713 | if (tochan) { |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 714 | if ((r = sshbuf_put(c->extended, errmsg, |
| 715 | strlen(errmsg))) != 0) |
| 716 | fatal("%s: buffer error %s", __func__, |
| 717 | ssh_err(r)); |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 718 | } else |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 719 | error("%s", errmsg); |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 720 | if (cr->action == CONFIRM_TTY) { |
| 721 | /* |
| 722 | * If a TTY allocation error occurred, then arrange |
| 723 | * for the correct TTY to leave raw mode. |
| 724 | */ |
| 725 | if (c->self == session_ident) |
| 726 | leave_raw_mode(0); |
| 727 | else |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 728 | mux_tty_alloc_failed(ssh, c); |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 729 | } else if (cr->action == CONFIRM_CLOSE) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 730 | chan_read_failed(ssh, c); |
| 731 | chan_write_failed(ssh, c); |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 732 | } |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 733 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 734 | free(cr); |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 735 | } |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 736 | |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 737 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 738 | client_abandon_status_confirm(struct ssh *ssh, Channel *c, void *ctx) |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 739 | { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 740 | free(ctx); |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 741 | } |
| 742 | |
Damien Miller | 6d7b437 | 2011-06-23 08:31:57 +1000 | [diff] [blame] | 743 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 744 | client_expect_confirm(struct ssh *ssh, int id, const char *request, |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 745 | enum confirm_action action) |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 746 | { |
Damien Miller | 6c81fee | 2013-11-08 12:19:55 +1100 | [diff] [blame] | 747 | struct channel_reply_ctx *cr = xcalloc(1, sizeof(*cr)); |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 748 | |
| 749 | cr->request_type = request; |
Damien Miller | 555f3b8 | 2011-05-15 08:48:05 +1000 | [diff] [blame] | 750 | cr->action = action; |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 751 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 752 | channel_register_status_confirm(ssh, id, client_status_confirm, |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 753 | client_abandon_status_confirm, cr); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 754 | } |
| 755 | |
Darren Tucker | 9f407c4 | 2008-06-13 04:50:27 +1000 | [diff] [blame] | 756 | void |
| 757 | client_register_global_confirm(global_confirm_cb *cb, void *ctx) |
| 758 | { |
Damien Miller | b9d3bee | 2008-07-16 22:40:52 +1000 | [diff] [blame] | 759 | struct global_confirm *gc, *last_gc; |
Darren Tucker | 9f407c4 | 2008-06-13 04:50:27 +1000 | [diff] [blame] | 760 | |
| 761 | /* Coalesce identical callbacks */ |
Damien Miller | b9d3bee | 2008-07-16 22:40:52 +1000 | [diff] [blame] | 762 | last_gc = TAILQ_LAST(&global_confirms, global_confirms); |
| 763 | if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) { |
| 764 | if (++last_gc->ref_count >= INT_MAX) |
| 765 | fatal("%s: last_gc->ref_count = %d", |
| 766 | __func__, last_gc->ref_count); |
Darren Tucker | 9f407c4 | 2008-06-13 04:50:27 +1000 | [diff] [blame] | 767 | return; |
| 768 | } |
| 769 | |
Damien Miller | 6c81fee | 2013-11-08 12:19:55 +1100 | [diff] [blame] | 770 | gc = xcalloc(1, sizeof(*gc)); |
Darren Tucker | 9f407c4 | 2008-06-13 04:50:27 +1000 | [diff] [blame] | 771 | gc->cb = cb; |
| 772 | gc->ctx = ctx; |
| 773 | gc->ref_count = 1; |
| 774 | TAILQ_INSERT_TAIL(&global_confirms, gc, entry); |
| 775 | } |
| 776 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 777 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 778 | process_cmdline(struct ssh *ssh) |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 779 | { |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 780 | void (*handler)(int); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 781 | char *s, *cmd; |
| 782 | int ok, delete = 0, local = 0, remote = 0, dynamic = 0; |
| 783 | struct Forward fwd; |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 784 | |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 785 | memset(&fwd, 0, sizeof(fwd)); |
Darren Tucker | 23ae8ca | 2007-12-02 23:12:30 +1100 | [diff] [blame] | 786 | |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 787 | leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); |
dtucker@openbsd.org | 3bf2a6a | 2020-01-23 07:10:22 +0000 | [diff] [blame] | 788 | handler = ssh_signal(SIGINT, SIG_IGN); |
Ben Lindstrom | 681d932 | 2002-03-22 03:53:00 +0000 | [diff] [blame] | 789 | cmd = s = read_passphrase("\r\nssh> ", RP_ECHO); |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 790 | if (s == NULL) |
| 791 | goto out; |
Damien Miller | fdb2306 | 2013-11-21 13:57:15 +1100 | [diff] [blame] | 792 | while (isspace((u_char)*s)) |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 793 | s++; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 794 | if (*s == '-') |
| 795 | s++; /* Skip cmdline '-', if any */ |
Darren Tucker | e167582 | 2004-05-24 10:13:07 +1000 | [diff] [blame] | 796 | if (*s == '\0') |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 797 | goto out; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 798 | |
Darren Tucker | 1973c88 | 2004-05-24 10:34:36 +1000 | [diff] [blame] | 799 | if (*s == 'h' || *s == 'H' || *s == '?') { |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 800 | logit("Commands:"); |
Damien Miller | 4302095 | 2006-07-10 20:16:12 +1000 | [diff] [blame] | 801 | logit(" -L[bind_address:]port:host:hostport " |
| 802 | "Request local forward"); |
| 803 | logit(" -R[bind_address:]port:host:hostport " |
| 804 | "Request remote forward"); |
Damien Miller | 0164cb8 | 2008-11-05 16:30:31 +1100 | [diff] [blame] | 805 | logit(" -D[bind_address:]port " |
| 806 | "Request dynamic forward"); |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 807 | logit(" -KL[bind_address:]port " |
| 808 | "Cancel local forward"); |
Damien Miller | 57e8ad3 | 2006-07-10 20:20:52 +1000 | [diff] [blame] | 809 | logit(" -KR[bind_address:]port " |
Damien Miller | 4302095 | 2006-07-10 20:16:12 +1000 | [diff] [blame] | 810 | "Cancel remote forward"); |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 811 | logit(" -KD[bind_address:]port " |
| 812 | "Cancel dynamic forward"); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 813 | if (!options.permit_local_command) |
| 814 | goto out; |
Damien Miller | 4302095 | 2006-07-10 20:16:12 +1000 | [diff] [blame] | 815 | logit(" !args " |
| 816 | "Execute local command"); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 817 | goto out; |
| 818 | } |
| 819 | |
| 820 | if (*s == '!' && options.permit_local_command) { |
| 821 | s++; |
| 822 | ssh_local_cmd(s); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 823 | goto out; |
| 824 | } |
| 825 | |
| 826 | if (*s == 'K') { |
| 827 | delete = 1; |
| 828 | s++; |
| 829 | } |
Damien Miller | 0164cb8 | 2008-11-05 16:30:31 +1100 | [diff] [blame] | 830 | if (*s == 'L') |
| 831 | local = 1; |
| 832 | else if (*s == 'R') |
| 833 | remote = 1; |
| 834 | else if (*s == 'D') |
| 835 | dynamic = 1; |
| 836 | else { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 837 | logit("Invalid command."); |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 838 | goto out; |
| 839 | } |
Damien Miller | 0164cb8 | 2008-11-05 16:30:31 +1100 | [diff] [blame] | 840 | |
Damien Miller | fdb2306 | 2013-11-21 13:57:15 +1100 | [diff] [blame] | 841 | while (isspace((u_char)*++s)) |
Darren Tucker | 03b1cdb | 2007-03-21 20:46:03 +1100 | [diff] [blame] | 842 | ; |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 843 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 844 | /* XXX update list of forwards in options */ |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 845 | if (delete) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 846 | /* We pass 1 for dynamicfwd to restrict to 1 or 2 fields. */ |
| 847 | if (!parse_forward(&fwd, s, 1, 0)) { |
| 848 | logit("Bad forwarding close specification."); |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 849 | goto out; |
| 850 | } |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 851 | if (remote) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 852 | ok = channel_request_rforward_cancel(ssh, &fwd) == 0; |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 853 | else if (dynamic) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 854 | ok = channel_cancel_lport_listener(ssh, &fwd, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 855 | 0, &options.fwd_opts) > 0; |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 856 | else |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 857 | ok = channel_cancel_lport_listener(ssh, &fwd, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 858 | CHANNEL_CANCEL_PORT_STATIC, |
| 859 | &options.fwd_opts) > 0; |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 860 | if (!ok) { |
dtucker@openbsd.org | 9390b00 | 2017-01-29 21:35:23 +0000 | [diff] [blame] | 861 | logit("Unknown port forwarding."); |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 862 | goto out; |
| 863 | } |
| 864 | logit("Canceled forwarding."); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 865 | } else { |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 866 | if (!parse_forward(&fwd, s, dynamic, remote)) { |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 867 | logit("Bad forwarding specification."); |
| 868 | goto out; |
| 869 | } |
Damien Miller | 0164cb8 | 2008-11-05 16:30:31 +1100 | [diff] [blame] | 870 | if (local || dynamic) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 871 | if (!channel_setup_local_fwd_listener(ssh, &fwd, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 872 | &options.fwd_opts)) { |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 873 | logit("Port forwarding failed."); |
| 874 | goto out; |
| 875 | } |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 876 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 877 | if (channel_request_remote_forwarding(ssh, &fwd) < 0) { |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 878 | logit("Port forwarding failed."); |
| 879 | goto out; |
| 880 | } |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 881 | } |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 882 | logit("Forwarding port."); |
| 883 | } |
| 884 | |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 885 | out: |
dtucker@openbsd.org | 3bf2a6a | 2020-01-23 07:10:22 +0000 | [diff] [blame] | 886 | ssh_signal(SIGINT, handler); |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 887 | enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 888 | free(cmd); |
| 889 | free(fwd.listen_host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 890 | free(fwd.listen_path); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 891 | free(fwd.connect_host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 892 | free(fwd.connect_path); |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Darren Tucker | 92a39cf | 2012-09-07 11:20:20 +1000 | [diff] [blame] | 895 | /* reasons to suppress output of an escape command in help output */ |
| 896 | #define SUPPRESS_NEVER 0 /* never suppress, always show */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 897 | #define SUPPRESS_MUXCLIENT 1 /* don't show in mux client sessions */ |
| 898 | #define SUPPRESS_MUXMASTER 2 /* don't show in mux master sessions */ |
| 899 | #define SUPPRESS_SYSLOG 4 /* don't show when logging to syslog */ |
Darren Tucker | 92a39cf | 2012-09-07 11:20:20 +1000 | [diff] [blame] | 900 | struct escape_help_text { |
| 901 | const char *cmd; |
| 902 | const char *text; |
| 903 | unsigned int flags; |
| 904 | }; |
| 905 | static struct escape_help_text esc_txt[] = { |
| 906 | {".", "terminate session", SUPPRESS_MUXMASTER}, |
| 907 | {".", "terminate connection (and any multiplexed sessions)", |
| 908 | SUPPRESS_MUXCLIENT}, |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 909 | {"B", "send a BREAK to the remote system", SUPPRESS_NEVER}, |
Darren Tucker | 92a39cf | 2012-09-07 11:20:20 +1000 | [diff] [blame] | 910 | {"C", "open a command line", SUPPRESS_MUXCLIENT}, |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 911 | {"R", "request rekey", SUPPRESS_NEVER}, |
Darren Tucker | ca0d0fd | 2012-09-07 11:22:24 +1000 | [diff] [blame] | 912 | {"V/v", "decrease/increase verbosity (LogLevel)", SUPPRESS_MUXCLIENT}, |
Darren Tucker | 92a39cf | 2012-09-07 11:20:20 +1000 | [diff] [blame] | 913 | {"^Z", "suspend ssh", SUPPRESS_MUXCLIENT}, |
| 914 | {"#", "list forwarded connections", SUPPRESS_NEVER}, |
| 915 | {"&", "background ssh (when waiting for connections to terminate)", |
| 916 | SUPPRESS_MUXCLIENT}, |
| 917 | {"?", "this message", SUPPRESS_NEVER}, |
| 918 | }; |
| 919 | |
| 920 | static void |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 921 | print_escape_help(struct sshbuf *b, int escape_char, int mux_client, |
| 922 | int using_stderr) |
Darren Tucker | 92a39cf | 2012-09-07 11:20:20 +1000 | [diff] [blame] | 923 | { |
| 924 | unsigned int i, suppress_flags; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 925 | int r; |
Darren Tucker | 92a39cf | 2012-09-07 11:20:20 +1000 | [diff] [blame] | 926 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 927 | if ((r = sshbuf_putf(b, |
| 928 | "%c?\r\nSupported escape sequences:\r\n", escape_char)) != 0) |
| 929 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Darren Tucker | 92a39cf | 2012-09-07 11:20:20 +1000 | [diff] [blame] | 930 | |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 931 | suppress_flags = |
Darren Tucker | 92a39cf | 2012-09-07 11:20:20 +1000 | [diff] [blame] | 932 | (mux_client ? SUPPRESS_MUXCLIENT : 0) | |
| 933 | (mux_client ? 0 : SUPPRESS_MUXMASTER) | |
| 934 | (using_stderr ? 0 : SUPPRESS_SYSLOG); |
| 935 | |
| 936 | for (i = 0; i < sizeof(esc_txt)/sizeof(esc_txt[0]); i++) { |
| 937 | if (esc_txt[i].flags & suppress_flags) |
| 938 | continue; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 939 | if ((r = sshbuf_putf(b, " %c%-3s - %s\r\n", |
| 940 | escape_char, esc_txt[i].cmd, esc_txt[i].text)) != 0) |
| 941 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Darren Tucker | 92a39cf | 2012-09-07 11:20:20 +1000 | [diff] [blame] | 942 | } |
| 943 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 944 | if ((r = sshbuf_putf(b, |
Darren Tucker | ca0d0fd | 2012-09-07 11:22:24 +1000 | [diff] [blame] | 945 | " %c%c - send the escape character by typing it twice\r\n" |
Darren Tucker | 92a39cf | 2012-09-07 11:20:20 +1000 | [diff] [blame] | 946 | "(Note that escapes are only recognized immediately after " |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 947 | "newline.)\r\n", escape_char, escape_char)) != 0) |
| 948 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Darren Tucker | 92a39cf | 2012-09-07 11:20:20 +1000 | [diff] [blame] | 949 | } |
| 950 | |
djm@openbsd.org | 84623e0 | 2018-06-26 02:02:36 +0000 | [diff] [blame] | 951 | /* |
naddy@openbsd.org | 768405f | 2017-05-03 21:08:09 +0000 | [diff] [blame] | 952 | * Process the characters one by one. |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 953 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 954 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 955 | process_escapes(struct ssh *ssh, Channel *c, |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 956 | struct sshbuf *bin, struct sshbuf *bout, struct sshbuf *berr, |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 957 | char *buf, int len) |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 958 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 959 | pid_t pid; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 960 | int r, bytes = 0; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 961 | u_int i; |
| 962 | u_char ch; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 963 | char *s; |
djm@openbsd.org | 0516435 | 2017-04-30 23:21:54 +0000 | [diff] [blame] | 964 | struct escape_filter_ctx *efc = c->filter_ctx == NULL ? |
| 965 | NULL : (struct escape_filter_ctx *)c->filter_ctx; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 966 | |
djm@openbsd.org | 0516435 | 2017-04-30 23:21:54 +0000 | [diff] [blame] | 967 | if (c->filter_ctx == NULL) |
| 968 | return 0; |
djm@openbsd.org | 84623e0 | 2018-06-26 02:02:36 +0000 | [diff] [blame] | 969 | |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 970 | if (len <= 0) |
| 971 | return (0); |
| 972 | |
| 973 | for (i = 0; i < (u_int)len; i++) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 974 | /* Get one character at a time. */ |
| 975 | ch = buf[i]; |
| 976 | |
djm@openbsd.org | 0516435 | 2017-04-30 23:21:54 +0000 | [diff] [blame] | 977 | if (efc->escape_pending) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 978 | /* We have previously seen an escape character. */ |
| 979 | /* Clear the flag now. */ |
djm@openbsd.org | 0516435 | 2017-04-30 23:21:54 +0000 | [diff] [blame] | 980 | efc->escape_pending = 0; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 981 | |
| 982 | /* Process the escaped character. */ |
| 983 | switch (ch) { |
| 984 | case '.': |
| 985 | /* Terminate the connection. */ |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 986 | if ((r = sshbuf_putf(berr, "%c.\r\n", |
| 987 | efc->escape_char)) != 0) |
| 988 | fatal("%s: buffer error: %s", |
| 989 | __func__, ssh_err(r)); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 990 | if (c && c->ctl_chan != -1) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 991 | chan_read_failed(ssh, c); |
| 992 | chan_write_failed(ssh, c); |
| 993 | if (c->detach_user) { |
| 994 | c->detach_user(ssh, |
| 995 | c->self, NULL); |
| 996 | } |
Damien Miller | 3618709 | 2013-06-10 13:07:11 +1000 | [diff] [blame] | 997 | c->type = SSH_CHANNEL_ABANDONED; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 998 | sshbuf_reset(c->input); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 999 | chan_ibuf_empty(ssh, c); |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1000 | return 0; |
| 1001 | } else |
| 1002 | quit_pending = 1; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1003 | return -1; |
| 1004 | |
| 1005 | case 'Z' - 64: |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1006 | /* XXX support this for mux clients */ |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1007 | if (c && c->ctl_chan != -1) { |
Darren Tucker | f111d40 | 2012-09-07 11:21:42 +1000 | [diff] [blame] | 1008 | char b[16]; |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1009 | noescape: |
Darren Tucker | f111d40 | 2012-09-07 11:21:42 +1000 | [diff] [blame] | 1010 | if (ch == 'Z' - 64) |
| 1011 | snprintf(b, sizeof b, "^Z"); |
| 1012 | else |
| 1013 | snprintf(b, sizeof b, "%c", ch); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1014 | if ((r = sshbuf_putf(berr, |
Darren Tucker | f111d40 | 2012-09-07 11:21:42 +1000 | [diff] [blame] | 1015 | "%c%s escape not available to " |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1016 | "multiplexed sessions\r\n", |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1017 | efc->escape_char, b)) != 0) |
| 1018 | fatal("%s: buffer error: %s", |
| 1019 | __func__, ssh_err(r)); |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1020 | continue; |
| 1021 | } |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 1022 | /* Suspend the program. Inform the user */ |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1023 | if ((r = sshbuf_putf(berr, |
| 1024 | "%c^Z [suspend ssh]\r\n", |
| 1025 | efc->escape_char)) != 0) |
| 1026 | fatal("%s: buffer error: %s", |
| 1027 | __func__, ssh_err(r)); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1028 | |
| 1029 | /* Restore terminal modes and suspend. */ |
| 1030 | client_suspend_self(bin, bout, berr); |
| 1031 | |
| 1032 | /* We have been continued. */ |
| 1033 | continue; |
| 1034 | |
Damien Miller | 54c4598 | 2003-05-15 10:20:13 +1000 | [diff] [blame] | 1035 | case 'B': |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1036 | if ((r = sshbuf_putf(berr, |
| 1037 | "%cB\r\n", efc->escape_char)) != 0) |
| 1038 | fatal("%s: buffer error: %s", |
| 1039 | __func__, ssh_err(r)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1040 | channel_request_start(ssh, c->self, "break", 0); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1041 | if ((r = sshpkt_put_u32(ssh, 1000)) != 0 || |
| 1042 | (r = sshpkt_send(ssh)) != 0) |
djm@openbsd.org | 9d7b288 | 2019-03-29 11:31:40 +0000 | [diff] [blame] | 1043 | fatal("%s: send packet: %s", __func__, |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1044 | ssh_err(r)); |
Damien Miller | 54c4598 | 2003-05-15 10:20:13 +1000 | [diff] [blame] | 1045 | continue; |
| 1046 | |
Ben Lindstrom | f28f634 | 2001-04-04 02:03:04 +0000 | [diff] [blame] | 1047 | case 'R': |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1048 | if (datafellows & SSH_BUG_NOREKEY) |
| 1049 | logit("Server does not " |
| 1050 | "support re-keying"); |
| 1051 | else |
| 1052 | need_rekeying = 1; |
Ben Lindstrom | f28f634 | 2001-04-04 02:03:04 +0000 | [diff] [blame] | 1053 | continue; |
| 1054 | |
Darren Tucker | 50a48d0 | 2012-09-06 21:25:37 +1000 | [diff] [blame] | 1055 | case 'V': |
| 1056 | /* FALLTHROUGH */ |
| 1057 | case 'v': |
| 1058 | if (c && c->ctl_chan != -1) |
| 1059 | goto noescape; |
| 1060 | if (!log_is_on_stderr()) { |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1061 | if ((r = sshbuf_putf(berr, |
Darren Tucker | 50a48d0 | 2012-09-06 21:25:37 +1000 | [diff] [blame] | 1062 | "%c%c [Logging to syslog]\r\n", |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1063 | efc->escape_char, ch)) != 0) |
| 1064 | fatal("%s: buffer error: %s", |
| 1065 | __func__, ssh_err(r)); |
Darren Tucker | 50a48d0 | 2012-09-06 21:25:37 +1000 | [diff] [blame] | 1066 | continue; |
| 1067 | } |
| 1068 | if (ch == 'V' && options.log_level > |
| 1069 | SYSLOG_LEVEL_QUIET) |
| 1070 | log_change_level(--options.log_level); |
| 1071 | if (ch == 'v' && options.log_level < |
| 1072 | SYSLOG_LEVEL_DEBUG3) |
| 1073 | log_change_level(++options.log_level); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1074 | if ((r = sshbuf_putf(berr, |
djm@openbsd.org | 0516435 | 2017-04-30 23:21:54 +0000 | [diff] [blame] | 1075 | "%c%c [LogLevel %s]\r\n", |
| 1076 | efc->escape_char, ch, |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1077 | log_level_name(options.log_level))) != 0) |
| 1078 | fatal("%s: buffer error: %s", |
| 1079 | __func__, ssh_err(r)); |
Darren Tucker | 50a48d0 | 2012-09-06 21:25:37 +1000 | [diff] [blame] | 1080 | continue; |
| 1081 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1082 | case '&': |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1083 | if (c && c->ctl_chan != -1) |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1084 | goto noescape; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1085 | /* |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 1086 | * Detach the program (continue to serve |
| 1087 | * connections, but put in background and no |
| 1088 | * more new connections). |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1089 | */ |
Damien Miller | 96507ef | 2001-11-12 10:52:25 +1100 | [diff] [blame] | 1090 | /* Restore tty modes. */ |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 1091 | leave_raw_mode( |
| 1092 | options.request_tty == REQUEST_TTY_FORCE); |
Damien Miller | 96507ef | 2001-11-12 10:52:25 +1100 | [diff] [blame] | 1093 | |
| 1094 | /* Stop listening for new connections. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1095 | channel_stop_listening(ssh); |
Damien Miller | 96507ef | 2001-11-12 10:52:25 +1100 | [diff] [blame] | 1096 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1097 | if ((r = sshbuf_putf(berr, |
| 1098 | "%c& [backgrounded]\n", efc->escape_char)) |
| 1099 | != 0) |
| 1100 | fatal("%s: buffer error: %s", |
| 1101 | __func__, ssh_err(r)); |
Damien Miller | 96507ef | 2001-11-12 10:52:25 +1100 | [diff] [blame] | 1102 | |
| 1103 | /* Fork into background. */ |
| 1104 | pid = fork(); |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 1105 | if (pid == -1) { |
Damien Miller | 96507ef | 2001-11-12 10:52:25 +1100 | [diff] [blame] | 1106 | error("fork: %.100s", strerror(errno)); |
| 1107 | continue; |
| 1108 | } |
| 1109 | if (pid != 0) { /* This is the parent. */ |
| 1110 | /* The parent just exits. */ |
| 1111 | exit(0); |
| 1112 | } |
| 1113 | /* The child continues serving connections. */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1114 | /* fake EOF on stdin */ |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1115 | if ((r = sshbuf_put_u8(bin, 4)) != 0) |
| 1116 | fatal("%s: buffer error: %s", |
| 1117 | __func__, ssh_err(r)); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1118 | return -1; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1119 | case '?': |
djm@openbsd.org | 0516435 | 2017-04-30 23:21:54 +0000 | [diff] [blame] | 1120 | print_escape_help(berr, efc->escape_char, |
Darren Tucker | 92a39cf | 2012-09-07 11:20:20 +1000 | [diff] [blame] | 1121 | (c && c->ctl_chan != -1), |
| 1122 | log_is_on_stderr()); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1123 | continue; |
| 1124 | |
| 1125 | case '#': |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1126 | if ((r = sshbuf_putf(berr, "%c#\r\n", |
| 1127 | efc->escape_char)) != 0) |
| 1128 | fatal("%s: buffer error: %s", |
| 1129 | __func__, ssh_err(r)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1130 | s = channel_open_message(ssh); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1131 | if ((r = sshbuf_put(berr, s, strlen(s))) != 0) |
| 1132 | fatal("%s: buffer error: %s", |
| 1133 | __func__, ssh_err(r)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1134 | free(s); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1135 | continue; |
| 1136 | |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 1137 | case 'C': |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1138 | if (c && c->ctl_chan != -1) |
Damien Miller | 586b005 | 2008-12-09 14:11:32 +1100 | [diff] [blame] | 1139 | goto noescape; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1140 | process_cmdline(ssh); |
Ben Lindstrom | 5589f4b | 2002-03-22 03:24:32 +0000 | [diff] [blame] | 1141 | continue; |
| 1142 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1143 | default: |
djm@openbsd.org | 0516435 | 2017-04-30 23:21:54 +0000 | [diff] [blame] | 1144 | if (ch != efc->escape_char) { |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1145 | if ((r = sshbuf_put_u8(bin, |
| 1146 | efc->escape_char)) != 0) |
| 1147 | fatal("%s: buffer error: %s", |
| 1148 | __func__, ssh_err(r)); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1149 | bytes++; |
| 1150 | } |
| 1151 | /* Escaped characters fall through here */ |
| 1152 | break; |
| 1153 | } |
| 1154 | } else { |
| 1155 | /* |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 1156 | * The previous character was not an escape char. |
| 1157 | * Check if this is an escape. |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1158 | */ |
djm@openbsd.org | 0516435 | 2017-04-30 23:21:54 +0000 | [diff] [blame] | 1159 | if (last_was_cr && ch == efc->escape_char) { |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 1160 | /* |
| 1161 | * It is. Set the flag and continue to |
| 1162 | * next character. |
| 1163 | */ |
djm@openbsd.org | 0516435 | 2017-04-30 23:21:54 +0000 | [diff] [blame] | 1164 | efc->escape_pending = 1; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1165 | continue; |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | /* |
| 1170 | * Normal character. Record whether it was a newline, |
| 1171 | * and append it to the buffer. |
| 1172 | */ |
| 1173 | last_was_cr = (ch == '\r' || ch == '\n'); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1174 | if ((r = sshbuf_put_u8(bin, ch)) != 0) |
| 1175 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1176 | bytes++; |
| 1177 | } |
| 1178 | return bytes; |
| 1179 | } |
| 1180 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1181 | /* |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1182 | * Get packets from the connection input buffer, and process them as long as |
| 1183 | * there are packets available. |
| 1184 | * |
| 1185 | * Any unknown packets received during the actual |
| 1186 | * session cause the session to terminate. This is |
| 1187 | * intended to make debugging easier since no |
| 1188 | * confirmations are sent. Any compatible protocol |
| 1189 | * extensions must be negotiated during the |
| 1190 | * preparatory phase. |
| 1191 | */ |
| 1192 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1193 | static void |
djm@openbsd.org | bc5e116 | 2019-01-19 21:39:12 +0000 | [diff] [blame] | 1194 | client_process_buffered_input_packets(struct ssh *ssh) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1195 | { |
djm@openbsd.org | bc5e116 | 2019-01-19 21:39:12 +0000 | [diff] [blame] | 1196 | ssh_dispatch_run_fatal(ssh, DISPATCH_NONBLOCK, &quit_pending); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1197 | } |
| 1198 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1199 | /* scan buf[] for '~' before sending data to the peer */ |
| 1200 | |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1201 | /* Helper: allocate a new escape_filter_ctx and fill in its escape char */ |
| 1202 | void * |
| 1203 | client_new_escape_filter_ctx(int escape_char) |
| 1204 | { |
| 1205 | struct escape_filter_ctx *ret; |
| 1206 | |
Damien Miller | 6c81fee | 2013-11-08 12:19:55 +1100 | [diff] [blame] | 1207 | ret = xcalloc(1, sizeof(*ret)); |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1208 | ret->escape_pending = 0; |
| 1209 | ret->escape_char = escape_char; |
| 1210 | return (void *)ret; |
| 1211 | } |
| 1212 | |
Darren Tucker | 84c56f5 | 2008-06-13 04:55:46 +1000 | [diff] [blame] | 1213 | /* Free the escape filter context on channel free */ |
| 1214 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1215 | client_filter_cleanup(struct ssh *ssh, int cid, void *ctx) |
Darren Tucker | 84c56f5 | 2008-06-13 04:55:46 +1000 | [diff] [blame] | 1216 | { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1217 | free(ctx); |
Darren Tucker | 84c56f5 | 2008-06-13 04:55:46 +1000 | [diff] [blame] | 1218 | } |
| 1219 | |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1220 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1221 | client_simple_escape_filter(struct ssh *ssh, Channel *c, char *buf, int len) |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1222 | { |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 1223 | if (c->extended_usage != CHAN_EXTENDED_WRITE) |
| 1224 | return 0; |
| 1225 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1226 | return process_escapes(ssh, c, c->input, c->output, c->extended, |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1227 | buf, len); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1228 | } |
| 1229 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1230 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1231 | client_channel_closed(struct ssh *ssh, int id, void *arg) |
Ben Lindstrom | 4c3f77d | 2001-04-05 23:37:36 +0000 | [diff] [blame] | 1232 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1233 | channel_cancel_cleanup(ssh, id); |
Ben Lindstrom | 4c3f77d | 2001-04-05 23:37:36 +0000 | [diff] [blame] | 1234 | session_closed = 1; |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 1235 | leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); |
Ben Lindstrom | 4c3f77d | 2001-04-05 23:37:36 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1238 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1239 | * Implements the interactive session with the server. This is called after |
| 1240 | * 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] | 1241 | * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character |
| 1242 | * used as an escape character for terminating or suspending the session. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1243 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1244 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1245 | client_loop(struct ssh *ssh, int have_pty, int escape_char_arg, |
| 1246 | int ssh2_chan_id) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1247 | { |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 1248 | fd_set *readset = NULL, *writeset = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1249 | double start_time, total_time; |
djm@openbsd.org | 19bcf2e | 2016-02-08 10:57:07 +0000 | [diff] [blame] | 1250 | int r, max_fd = 0, max_fd2 = 0, len; |
Damien Miller | b61f3fc | 2008-07-11 17:36:48 +1000 | [diff] [blame] | 1251 | u_int64_t ibytes, obytes; |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 1252 | u_int nalloc = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1253 | char buf[100]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1254 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1255 | debug("Entering interactive session."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1256 | |
semarie@openbsd.org | d7d2bc9 | 2015-12-26 07:46:03 +0000 | [diff] [blame] | 1257 | if (options.control_master && |
djm@openbsd.org | 368dd97 | 2016-07-23 02:54:08 +0000 | [diff] [blame] | 1258 | !option_clear_or_none(options.control_path)) { |
semarie@openbsd.org | d7d2bc9 | 2015-12-26 07:46:03 +0000 | [diff] [blame] | 1259 | debug("pledge: id"); |
mestre@openbsd.org | 7adf6c4 | 2019-07-24 08:57:00 +0000 | [diff] [blame] | 1260 | if (pledge("stdio rpath wpath cpath unix inet dns recvfd sendfd proc exec id tty", |
semarie@openbsd.org | d7d2bc9 | 2015-12-26 07:46:03 +0000 | [diff] [blame] | 1261 | NULL) == -1) |
| 1262 | fatal("%s pledge(): %s", __func__, strerror(errno)); |
| 1263 | |
| 1264 | } else if (options.forward_x11 || options.permit_local_command) { |
semarie@openbsd.org | b91926a | 2015-12-03 17:00:18 +0000 | [diff] [blame] | 1265 | debug("pledge: exec"); |
| 1266 | if (pledge("stdio rpath wpath cpath unix inet dns proc exec tty", |
| 1267 | NULL) == -1) |
| 1268 | fatal("%s pledge(): %s", __func__, strerror(errno)); |
| 1269 | |
| 1270 | } else if (options.update_hostkeys) { |
| 1271 | debug("pledge: filesystem full"); |
| 1272 | if (pledge("stdio rpath wpath cpath unix inet dns proc tty", |
| 1273 | NULL) == -1) |
| 1274 | fatal("%s pledge(): %s", __func__, strerror(errno)); |
| 1275 | |
djm@openbsd.org | 368dd97 | 2016-07-23 02:54:08 +0000 | [diff] [blame] | 1276 | } else if (!option_clear_or_none(options.proxy_command) || |
| 1277 | fork_after_authentication_flag) { |
semarie@openbsd.org | b91926a | 2015-12-03 17:00:18 +0000 | [diff] [blame] | 1278 | debug("pledge: proc"); |
| 1279 | if (pledge("stdio cpath unix inet dns proc tty", NULL) == -1) |
| 1280 | fatal("%s pledge(): %s", __func__, strerror(errno)); |
| 1281 | |
semarie@openbsd.org | b91926a | 2015-12-03 17:00:18 +0000 | [diff] [blame] | 1282 | } else { |
| 1283 | debug("pledge: network"); |
mestre@openbsd.org | e238645 | 2017-06-23 07:24:48 +0000 | [diff] [blame] | 1284 | if (pledge("stdio unix inet dns proc tty", NULL) == -1) |
semarie@openbsd.org | b91926a | 2015-12-03 17:00:18 +0000 | [diff] [blame] | 1285 | fatal("%s pledge(): %s", __func__, strerror(errno)); |
| 1286 | } |
| 1287 | |
dtucker@openbsd.org@openbsd.org | 2d638e9 | 2017-11-25 05:58:47 +0000 | [diff] [blame] | 1288 | start_time = monotime_double(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1289 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1290 | /* Initialize variables. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1291 | last_was_cr = 1; |
| 1292 | exit_status = -1; |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1293 | connection_in = ssh_packet_get_connection_in(ssh); |
| 1294 | connection_out = ssh_packet_get_connection_out(ssh); |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 1295 | max_fd = MAXIMUM(connection_in, connection_out); |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 1296 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1297 | quit_pending = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1298 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1299 | /* Initialize buffer. */ |
| 1300 | if ((stderr_buffer = sshbuf_new()) == NULL) |
| 1301 | fatal("%s: sshbuf_new failed", __func__); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1302 | |
djm@openbsd.org | bc5e116 | 2019-01-19 21:39:12 +0000 | [diff] [blame] | 1303 | client_init_dispatch(ssh); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1304 | |
Ben Lindstrom | f49dbff | 2002-12-23 02:01:55 +0000 | [diff] [blame] | 1305 | /* |
| 1306 | * Set signal handlers, (e.g. to restore non-blocking mode) |
| 1307 | * but don't overwrite SIG_IGN, matches behaviour from rsh(1) |
| 1308 | */ |
dtucker@openbsd.org | 3bf2a6a | 2020-01-23 07:10:22 +0000 | [diff] [blame] | 1309 | if (ssh_signal(SIGHUP, SIG_IGN) != SIG_IGN) |
| 1310 | ssh_signal(SIGHUP, signal_handler); |
| 1311 | if (ssh_signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 1312 | ssh_signal(SIGINT, signal_handler); |
| 1313 | if (ssh_signal(SIGQUIT, SIG_IGN) != SIG_IGN) |
| 1314 | ssh_signal(SIGQUIT, signal_handler); |
| 1315 | if (ssh_signal(SIGTERM, SIG_IGN) != SIG_IGN) |
| 1316 | ssh_signal(SIGTERM, signal_handler); |
| 1317 | ssh_signal(SIGWINCH, window_change_handler); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1318 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1319 | if (have_pty) |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 1320 | enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1321 | |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1322 | session_ident = ssh2_chan_id; |
| 1323 | if (session_ident != -1) { |
| 1324 | if (escape_char_arg != SSH_ESCAPECHAR_NONE) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1325 | channel_register_filter(ssh, session_ident, |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1326 | client_simple_escape_filter, NULL, |
| 1327 | client_filter_cleanup, |
| 1328 | client_new_escape_filter_ctx( |
| 1329 | escape_char_arg)); |
Damien Miller | 6c3eec7 | 2011-05-05 14:16:22 +1000 | [diff] [blame] | 1330 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1331 | channel_register_cleanup(ssh, session_ident, |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1332 | client_channel_closed, 0); |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 1333 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1334 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1335 | /* Main loop of the client for the interactive session mode. */ |
| 1336 | while (!quit_pending) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1337 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1338 | /* Process buffered packets sent by the server. */ |
djm@openbsd.org | bc5e116 | 2019-01-19 21:39:12 +0000 | [diff] [blame] | 1339 | client_process_buffered_input_packets(ssh); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1340 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1341 | if (session_closed && !channel_still_open(ssh)) |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1342 | break; |
Ben Lindstrom | 4c3f77d | 2001-04-05 23:37:36 +0000 | [diff] [blame] | 1343 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1344 | if (ssh_packet_is_rekeying(ssh)) { |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 1345 | debug("rekeying in progress"); |
djm@openbsd.org | 19bcf2e | 2016-02-08 10:57:07 +0000 | [diff] [blame] | 1346 | } else if (need_rekeying) { |
| 1347 | /* manual rekey request */ |
| 1348 | debug("need rekeying"); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1349 | if ((r = kex_start_rekex(ssh)) != 0) |
djm@openbsd.org | 19bcf2e | 2016-02-08 10:57:07 +0000 | [diff] [blame] | 1350 | fatal("%s: kex_start_rekex: %s", __func__, |
| 1351 | ssh_err(r)); |
| 1352 | need_rekeying = 0; |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 1353 | } else { |
| 1354 | /* |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 1355 | * Make packets from buffered channel data, and |
| 1356 | * enqueue them for sending to the server. |
| 1357 | */ |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1358 | if (ssh_packet_not_very_much_data_to_write(ssh)) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1359 | channel_output_poll(ssh); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1360 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 1361 | /* |
| 1362 | * Check if the window size has changed, and buffer a |
| 1363 | * message about it to the server if so. |
| 1364 | */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1365 | client_check_window_change(ssh); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1366 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 1367 | if (quit_pending) |
| 1368 | break; |
| 1369 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1370 | /* |
| 1371 | * Wait until we have something to do (something becomes |
| 1372 | * available on one of the descriptors). |
| 1373 | */ |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 1374 | max_fd2 = max_fd; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1375 | client_wait_until_can_do_something(ssh, &readset, &writeset, |
| 1376 | &max_fd2, &nalloc, ssh_packet_is_rekeying(ssh)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1377 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1378 | if (quit_pending) |
| 1379 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1380 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 1381 | /* Do channel operations unless rekeying in progress. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1382 | if (!ssh_packet_is_rekeying(ssh)) |
| 1383 | channel_after_select(ssh, readset, writeset); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1384 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1385 | /* Buffer input from the connection. */ |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1386 | client_process_net_input(ssh, readset); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1387 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1388 | if (quit_pending) |
| 1389 | break; |
| 1390 | |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 1391 | /* |
| 1392 | * Send as much buffered packet data as possible to the |
| 1393 | * sender. |
| 1394 | */ |
djm@openbsd.org | 65d6fd0 | 2020-01-30 07:20:57 +0000 | [diff] [blame] | 1395 | if (FD_ISSET(connection_out, writeset)) { |
| 1396 | if ((r = ssh_packet_write_poll(ssh)) != 0) { |
| 1397 | sshpkt_fatal(ssh, r, |
| 1398 | "%s: ssh_packet_write_poll", __func__); |
| 1399 | } |
| 1400 | } |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 1401 | |
| 1402 | /* |
| 1403 | * If we are a backgrounded control master, and the |
| 1404 | * timeout has expired without any active client |
| 1405 | * connections, then quit. |
| 1406 | */ |
| 1407 | if (control_persist_exit_time > 0) { |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 1408 | if (monotime() >= control_persist_exit_time) { |
Damien Miller | e11e1ea | 2010-08-03 16:04:46 +1000 | [diff] [blame] | 1409 | debug("ControlPersist timeout expired"); |
| 1410 | break; |
| 1411 | } |
| 1412 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1413 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1414 | free(readset); |
| 1415 | free(writeset); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1416 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1417 | /* Terminate the session. */ |
| 1418 | |
| 1419 | /* Stop watching for window change. */ |
dtucker@openbsd.org | 3bf2a6a | 2020-01-23 07:10:22 +0000 | [diff] [blame] | 1420 | ssh_signal(SIGWINCH, SIG_DFL); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1421 | |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1422 | if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 || |
| 1423 | (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_BY_APPLICATION)) != 0 || |
| 1424 | (r = sshpkt_put_cstring(ssh, "disconnected by user")) != 0 || |
| 1425 | (r = sshpkt_put_cstring(ssh, "")) != 0 || /* language tag */ |
| 1426 | (r = sshpkt_send(ssh)) != 0 || |
| 1427 | (r = ssh_packet_write_wait(ssh)) != 0) |
djm@openbsd.org | 9d7b288 | 2019-03-29 11:31:40 +0000 | [diff] [blame] | 1428 | fatal("%s: send disconnect: %s", __func__, ssh_err(r)); |
Darren Tucker | 12b4a65 | 2009-06-21 18:14:48 +1000 | [diff] [blame] | 1429 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1430 | channel_free_all(ssh); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1431 | |
Ben Lindstrom | ec46e0b | 2001-06-09 01:27:31 +0000 | [diff] [blame] | 1432 | if (have_pty) |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 1433 | leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 1434 | |
| 1435 | /* restore blocking io */ |
| 1436 | if (!isatty(fileno(stdin))) |
| 1437 | unset_nonblock(fileno(stdin)); |
| 1438 | if (!isatty(fileno(stdout))) |
| 1439 | unset_nonblock(fileno(stdout)); |
| 1440 | if (!isatty(fileno(stderr))) |
| 1441 | unset_nonblock(fileno(stderr)); |
| 1442 | |
Damien Miller | d696551 | 2003-12-17 16:31:53 +1100 | [diff] [blame] | 1443 | /* |
| 1444 | * If there was no shell or command requested, there will be no remote |
| 1445 | * exit status to be returned. In that case, clear error code if the |
| 1446 | * connection was deliberately terminated at this end. |
| 1447 | */ |
| 1448 | if (no_shell_flag && received_signal == SIGTERM) { |
| 1449 | received_signal = 0; |
| 1450 | exit_status = 0; |
| 1451 | } |
| 1452 | |
dtucker@openbsd.org | b1e72df | 2017-07-14 03:18:21 +0000 | [diff] [blame] | 1453 | if (received_signal) { |
| 1454 | verbose("Killed by signal %d.", (int) received_signal); |
| 1455 | cleanup_exit(0); |
| 1456 | } |
Ben Lindstrom | ec46e0b | 2001-06-09 01:27:31 +0000 | [diff] [blame] | 1457 | |
| 1458 | /* |
| 1459 | * In interactive mode (with pseudo tty) display a message indicating |
| 1460 | * that the connection has been closed. |
| 1461 | */ |
| 1462 | if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) { |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1463 | if ((r = sshbuf_putf(stderr_buffer, |
| 1464 | "Connection to %.64s closed.\r\n", host)) != 0) |
| 1465 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Ben Lindstrom | ec46e0b | 2001-06-09 01:27:31 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1468 | /* Output any buffered data for stderr. */ |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1469 | if (sshbuf_len(stderr_buffer) > 0) { |
Damien Miller | 4791f9d | 2011-01-16 23:16:53 +1100 | [diff] [blame] | 1470 | len = atomicio(vwrite, fileno(stderr), |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1471 | (u_char *)sshbuf_ptr(stderr_buffer), |
| 1472 | sshbuf_len(stderr_buffer)); |
| 1473 | if (len < 0 || (u_int)len != sshbuf_len(stderr_buffer)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1474 | error("Write failed flushing stderr buffer."); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1475 | else if ((r = sshbuf_consume(stderr_buffer, len)) != 0) |
| 1476 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1477 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1478 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1479 | /* Clear and free any buffers. */ |
jsg@openbsd.org | 458abc2 | 2016-01-23 05:31:35 +0000 | [diff] [blame] | 1480 | explicit_bzero(buf, sizeof(buf)); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1481 | sshbuf_free(stderr_buffer); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1482 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1483 | /* Report bytes transferred, and transfer rates. */ |
dtucker@openbsd.org@openbsd.org | 2d638e9 | 2017-11-25 05:58:47 +0000 | [diff] [blame] | 1484 | total_time = monotime_double() - start_time; |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1485 | ssh_packet_get_bytes(ssh, &ibytes, &obytes); |
Damien Miller | b61f3fc | 2008-07-11 17:36:48 +1000 | [diff] [blame] | 1486 | verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds", |
Damien Miller | 821de0a | 2011-01-11 17:20:29 +1100 | [diff] [blame] | 1487 | (unsigned long long)obytes, (unsigned long long)ibytes, total_time); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1488 | if (total_time > 0) |
Damien Miller | b61f3fc | 2008-07-11 17:36:48 +1000 | [diff] [blame] | 1489 | verbose("Bytes per second: sent %.1f, received %.1f", |
| 1490 | obytes / total_time, ibytes / total_time); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1491 | /* Return the exit status of the program. */ |
| 1492 | debug("Exit status %d", exit_status); |
| 1493 | return exit_status; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1494 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1495 | |
| 1496 | /*********/ |
| 1497 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1498 | static Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1499 | client_request_forwarded_tcpip(struct ssh *ssh, const char *request_type, |
| 1500 | int rchan, u_int rwindow, u_int rmaxpack) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1501 | { |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 1502 | Channel *c = NULL; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 1503 | struct sshbuf *b = NULL; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1504 | char *listen_address, *originator_address; |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1505 | u_int listen_port, originator_port; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 1506 | int r; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1507 | |
| 1508 | /* Get rest of the packet */ |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1509 | if ((r = sshpkt_get_cstring(ssh, &listen_address, NULL)) != 0 || |
| 1510 | (r = sshpkt_get_u32(ssh, &listen_port)) != 0 || |
| 1511 | (r = sshpkt_get_cstring(ssh, &originator_address, NULL)) != 0 || |
| 1512 | (r = sshpkt_get_u32(ssh, &originator_port)) != 0 || |
| 1513 | (r = sshpkt_get_end(ssh)) != 0) |
djm@openbsd.org | 9d7b288 | 2019-03-29 11:31:40 +0000 | [diff] [blame] | 1514 | fatal("%s: parse packet: %s", __func__, ssh_err(r)); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1515 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1516 | debug("%s: listen %s port %d, originator %s port %d", __func__, |
| 1517 | listen_address, listen_port, originator_address, originator_port); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1518 | |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1519 | if (listen_port > 0xffff) |
| 1520 | error("%s: invalid listen port", __func__); |
| 1521 | else if (originator_port > 0xffff) |
| 1522 | error("%s: invalid originator port", __func__); |
| 1523 | else { |
| 1524 | c = channel_connect_by_listen_address(ssh, |
| 1525 | listen_address, listen_port, "forwarded-tcpip", |
| 1526 | originator_address); |
| 1527 | } |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 1528 | |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 1529 | if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) { |
| 1530 | if ((b = sshbuf_new()) == NULL) { |
| 1531 | error("%s: alloc reply", __func__); |
| 1532 | goto out; |
| 1533 | } |
| 1534 | /* reconstruct and send to muxclient */ |
| 1535 | if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */ |
| 1536 | (r = sshbuf_put_u8(b, SSH2_MSG_CHANNEL_OPEN)) != 0 || |
| 1537 | (r = sshbuf_put_cstring(b, request_type)) != 0 || |
| 1538 | (r = sshbuf_put_u32(b, rchan)) != 0 || |
| 1539 | (r = sshbuf_put_u32(b, rwindow)) != 0 || |
| 1540 | (r = sshbuf_put_u32(b, rmaxpack)) != 0 || |
| 1541 | (r = sshbuf_put_cstring(b, listen_address)) != 0 || |
| 1542 | (r = sshbuf_put_u32(b, listen_port)) != 0 || |
| 1543 | (r = sshbuf_put_cstring(b, originator_address)) != 0 || |
| 1544 | (r = sshbuf_put_u32(b, originator_port)) != 0 || |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1545 | (r = sshbuf_put_stringb(c->output, b)) != 0) { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 1546 | error("%s: compose for muxclient %s", __func__, |
| 1547 | ssh_err(r)); |
| 1548 | goto out; |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | out: |
| 1553 | sshbuf_free(b); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1554 | free(originator_address); |
| 1555 | free(listen_address); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1556 | return c; |
| 1557 | } |
| 1558 | |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 1559 | static Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1560 | client_request_forwarded_streamlocal(struct ssh *ssh, |
| 1561 | const char *request_type, int rchan) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1562 | { |
| 1563 | Channel *c = NULL; |
| 1564 | char *listen_path; |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1565 | int r; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1566 | |
| 1567 | /* Get the remote path. */ |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1568 | if ((r = sshpkt_get_cstring(ssh, &listen_path, NULL)) != 0 || |
| 1569 | (r = sshpkt_get_string(ssh, NULL, NULL)) != 0 || /* reserved */ |
| 1570 | (r = sshpkt_get_end(ssh)) != 0) |
djm@openbsd.org | 9d7b288 | 2019-03-29 11:31:40 +0000 | [diff] [blame] | 1571 | fatal("%s: parse packet: %s", __func__, ssh_err(r)); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1572 | |
djm@openbsd.org | 9d7b288 | 2019-03-29 11:31:40 +0000 | [diff] [blame] | 1573 | debug("%s: request: %s", __func__, listen_path); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1574 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1575 | c = channel_connect_by_listen_path(ssh, listen_path, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1576 | "forwarded-streamlocal@openssh.com", "forwarded-streamlocal"); |
| 1577 | free(listen_path); |
| 1578 | return c; |
| 1579 | } |
| 1580 | |
| 1581 | static Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1582 | client_request_x11(struct ssh *ssh, const char *request_type, int rchan) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1583 | { |
| 1584 | Channel *c = NULL; |
| 1585 | char *originator; |
djm@openbsd.org | bc5e116 | 2019-01-19 21:39:12 +0000 | [diff] [blame] | 1586 | u_int originator_port; |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1587 | int r, sock; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1588 | |
| 1589 | if (!options.forward_x11) { |
| 1590 | error("Warning: ssh server tried X11 forwarding."); |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 1591 | error("Warning: this is probably a break-in attempt by a " |
| 1592 | "malicious server."); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1593 | return NULL; |
| 1594 | } |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 1595 | if (x11_refuse_time != 0 && (u_int)monotime() >= x11_refuse_time) { |
Damien Miller | 1ab6a51 | 2010-06-26 10:02:24 +1000 | [diff] [blame] | 1596 | verbose("Rejected X11 connection after ForwardX11Timeout " |
| 1597 | "expired"); |
| 1598 | return NULL; |
| 1599 | } |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1600 | if ((r = sshpkt_get_cstring(ssh, &originator, NULL)) != 0 || |
djm@openbsd.org | bc5e116 | 2019-01-19 21:39:12 +0000 | [diff] [blame] | 1601 | (r = sshpkt_get_u32(ssh, &originator_port)) != 0 || |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1602 | (r = sshpkt_get_end(ssh)) != 0) |
djm@openbsd.org | 9d7b288 | 2019-03-29 11:31:40 +0000 | [diff] [blame] | 1603 | fatal("%s: parse packet: %s", __func__, ssh_err(r)); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1604 | /* XXX check permission */ |
djm@openbsd.org | bc5e116 | 2019-01-19 21:39:12 +0000 | [diff] [blame] | 1605 | /* XXX range check originator port? */ |
| 1606 | debug("client_request_x11: request from %s %u", originator, |
Damien Miller | d83ff35 | 2001-01-30 09:19:34 +1100 | [diff] [blame] | 1607 | originator_port); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1608 | free(originator); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1609 | sock = x11_connect_display(ssh); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1610 | if (sock < 0) |
| 1611 | return NULL; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1612 | c = channel_new(ssh, "x11", |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1613 | SSH_CHANNEL_X11_OPEN, sock, sock, -1, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 1614 | CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 1615 | c->force_drain = 1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1616 | return c; |
| 1617 | } |
| 1618 | |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 1619 | static Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1620 | client_request_agent(struct ssh *ssh, const char *request_type, int rchan) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1621 | { |
| 1622 | Channel *c = NULL; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1623 | int r, sock; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1624 | |
| 1625 | if (!options.forward_agent) { |
| 1626 | error("Warning: ssh server tried agent forwarding."); |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 1627 | error("Warning: this is probably a break-in attempt by a " |
| 1628 | "malicious server."); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1629 | return NULL; |
| 1630 | } |
djm@openbsd.org | 40be78f | 2019-12-21 02:19:13 +0000 | [diff] [blame] | 1631 | if (forward_agent_sock_path == NULL) { |
| 1632 | r = ssh_get_authentication_socket(&sock); |
| 1633 | } else { |
| 1634 | r = ssh_get_authentication_socket_path(forward_agent_sock_path, &sock); |
| 1635 | } |
| 1636 | if (r != 0) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1637 | if (r != SSH_ERR_AGENT_NOT_PRESENT) |
| 1638 | debug("%s: ssh_get_authentication_socket: %s", |
| 1639 | __func__, ssh_err(r)); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1640 | return NULL; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1641 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1642 | c = channel_new(ssh, "authentication agent connection", |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1643 | SSH_CHANNEL_OPEN, sock, sock, -1, |
Darren Tucker | 5baa170 | 2007-12-29 09:37:10 +1100 | [diff] [blame] | 1644 | CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 1645 | "authentication agent connection", 1); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 1646 | c->force_drain = 1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1647 | return c; |
| 1648 | } |
| 1649 | |
djm@openbsd.org | b7548b1 | 2017-10-23 05:08:00 +0000 | [diff] [blame] | 1650 | char * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1651 | client_request_tun_fwd(struct ssh *ssh, int tun_mode, |
| 1652 | int local_tun, int remote_tun) |
Damien Miller | b3ce9fe | 2007-08-08 14:32:41 +1000 | [diff] [blame] | 1653 | { |
| 1654 | Channel *c; |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1655 | int r, fd; |
djm@openbsd.org | b7548b1 | 2017-10-23 05:08:00 +0000 | [diff] [blame] | 1656 | char *ifname = NULL; |
Damien Miller | b3ce9fe | 2007-08-08 14:32:41 +1000 | [diff] [blame] | 1657 | |
| 1658 | if (tun_mode == SSH_TUNMODE_NO) |
| 1659 | return 0; |
| 1660 | |
Damien Miller | b3ce9fe | 2007-08-08 14:32:41 +1000 | [diff] [blame] | 1661 | debug("Requesting tun unit %d in mode %d", local_tun, tun_mode); |
| 1662 | |
| 1663 | /* Open local tunnel device */ |
djm@openbsd.org | b7548b1 | 2017-10-23 05:08:00 +0000 | [diff] [blame] | 1664 | if ((fd = tun_open(local_tun, tun_mode, &ifname)) == -1) { |
Damien Miller | b3ce9fe | 2007-08-08 14:32:41 +1000 | [diff] [blame] | 1665 | error("Tunnel device open failed."); |
djm@openbsd.org | b7548b1 | 2017-10-23 05:08:00 +0000 | [diff] [blame] | 1666 | return NULL; |
Damien Miller | b3ce9fe | 2007-08-08 14:32:41 +1000 | [diff] [blame] | 1667 | } |
djm@openbsd.org | b7548b1 | 2017-10-23 05:08:00 +0000 | [diff] [blame] | 1668 | debug("Tunnel forwarding using interface %s", ifname); |
Damien Miller | b3ce9fe | 2007-08-08 14:32:41 +1000 | [diff] [blame] | 1669 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1670 | c = channel_new(ssh, "tun", SSH_CHANNEL_OPENING, fd, fd, -1, |
Damien Miller | b3ce9fe | 2007-08-08 14:32:41 +1000 | [diff] [blame] | 1671 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1); |
| 1672 | c->datagram = 1; |
| 1673 | |
| 1674 | #if defined(SSH_TUN_FILTER) |
| 1675 | if (options.tun_open == SSH_TUNMODE_POINTOPOINT) |
Damien Miller | 871f1e4 | 2017-09-12 18:01:35 +1000 | [diff] [blame] | 1676 | channel_register_filter(ssh, c->self, sys_tun_infilter, |
Darren Tucker | 1cf65ae | 2008-06-13 05:09:18 +1000 | [diff] [blame] | 1677 | sys_tun_outfilter, NULL, NULL); |
Damien Miller | b3ce9fe | 2007-08-08 14:32:41 +1000 | [diff] [blame] | 1678 | #endif |
| 1679 | |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1680 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN)) != 0 || |
| 1681 | (r = sshpkt_put_cstring(ssh, "tun@openssh.com")) != 0 || |
| 1682 | (r = sshpkt_put_u32(ssh, c->self)) != 0 || |
| 1683 | (r = sshpkt_put_u32(ssh, c->local_window_max)) != 0 || |
| 1684 | (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0 || |
| 1685 | (r = sshpkt_put_u32(ssh, tun_mode)) != 0 || |
| 1686 | (r = sshpkt_put_u32(ssh, remote_tun)) != 0 || |
| 1687 | (r = sshpkt_send(ssh)) != 0) |
djm@openbsd.org | bc5e116 | 2019-01-19 21:39:12 +0000 | [diff] [blame] | 1688 | sshpkt_fatal(ssh, r, "%s: send reply", __func__); |
Damien Miller | b3ce9fe | 2007-08-08 14:32:41 +1000 | [diff] [blame] | 1689 | |
djm@openbsd.org | b7548b1 | 2017-10-23 05:08:00 +0000 | [diff] [blame] | 1690 | return ifname; |
Damien Miller | b3ce9fe | 2007-08-08 14:32:41 +1000 | [diff] [blame] | 1691 | } |
| 1692 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1693 | /* XXXX move to generic input handler */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 1694 | static int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 1695 | client_input_channel_open(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1696 | { |
| 1697 | Channel *c = NULL; |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1698 | char *ctype = NULL; |
| 1699 | int r; |
| 1700 | u_int rchan; |
| 1701 | size_t len; |
| 1702 | u_int rmaxpack, rwindow; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1703 | |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1704 | if ((r = sshpkt_get_cstring(ssh, &ctype, &len)) != 0 || |
| 1705 | (r = sshpkt_get_u32(ssh, &rchan)) != 0 || |
| 1706 | (r = sshpkt_get_u32(ssh, &rwindow)) != 0 || |
| 1707 | (r = sshpkt_get_u32(ssh, &rmaxpack)) != 0) |
| 1708 | goto out; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1709 | |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1710 | 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] | 1711 | ctype, rchan, rwindow, rmaxpack); |
| 1712 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1713 | if (strcmp(ctype, "forwarded-tcpip") == 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1714 | c = client_request_forwarded_tcpip(ssh, ctype, rchan, rwindow, |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 1715 | rmaxpack); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 1716 | } else if (strcmp(ctype, "forwarded-streamlocal@openssh.com") == 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1717 | c = client_request_forwarded_streamlocal(ssh, ctype, rchan); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1718 | } else if (strcmp(ctype, "x11") == 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1719 | c = client_request_x11(ssh, ctype, rchan); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1720 | } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1721 | c = client_request_agent(ssh, ctype, rchan); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1722 | } |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 1723 | if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) { |
| 1724 | debug3("proxied to downstream: %s", ctype); |
| 1725 | } else if (c != NULL) { |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1726 | debug("confirm %s", ctype); |
| 1727 | c->remote_id = rchan; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 1728 | c->have_remote_id = 1; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1729 | c->remote_window = rwindow; |
| 1730 | c->remote_maxpacket = rmaxpack; |
Ben Lindstrom | a69d89b | 2001-05-09 00:01:18 +0000 | [diff] [blame] | 1731 | if (c->type != SSH_CHANNEL_CONNECTING) { |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1732 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 || |
| 1733 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 1734 | (r = sshpkt_put_u32(ssh, c->self)) != 0 || |
| 1735 | (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || |
| 1736 | (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0 || |
| 1737 | (r = sshpkt_send(ssh)) != 0) |
| 1738 | sshpkt_fatal(ssh, r, "%s: send reply", __func__); |
Ben Lindstrom | a69d89b | 2001-05-09 00:01:18 +0000 | [diff] [blame] | 1739 | } |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1740 | } else { |
| 1741 | debug("failure %s", ctype); |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1742 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 || |
| 1743 | (r = sshpkt_put_u32(ssh, rchan)) != 0 || |
| 1744 | (r = sshpkt_put_u32(ssh, SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED)) != 0 || |
| 1745 | (r = sshpkt_put_cstring(ssh, "open failed")) != 0 || |
| 1746 | (r = sshpkt_put_cstring(ssh, "")) != 0 || |
| 1747 | (r = sshpkt_send(ssh)) != 0) |
| 1748 | sshpkt_fatal(ssh, r, "%s: send failure", __func__); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1749 | } |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1750 | r = 0; |
| 1751 | out: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1752 | free(ctype); |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1753 | return r; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1754 | } |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 1755 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 1756 | static int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 1757 | client_input_channel_req(int type, u_int32_t seq, struct ssh *ssh) |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 1758 | { |
| 1759 | Channel *c = NULL; |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1760 | char *rtype = NULL; |
| 1761 | u_char reply; |
| 1762 | u_int id, exitval; |
| 1763 | int r, success = 0; |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 1764 | |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1765 | if ((r = sshpkt_get_u32(ssh, &id)) != 0) |
| 1766 | return r; |
| 1767 | if (id <= INT_MAX) |
| 1768 | c = channel_lookup(ssh, id); |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 1769 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 1770 | return 0; |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1771 | if ((r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 || |
| 1772 | (r = sshpkt_get_u8(ssh, &reply)) != 0) |
| 1773 | goto out; |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 1774 | |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1775 | debug("client_input_channel_req: channel %u rtype %s reply %d", |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 1776 | id, rtype, reply); |
| 1777 | |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1778 | if (c == NULL) { |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 1779 | error("client_input_channel_req: channel %d: " |
| 1780 | "unknown channel", id); |
Damien Miller | bab9bd4 | 2008-05-19 16:06:47 +1000 | [diff] [blame] | 1781 | } else if (strcmp(rtype, "eow@openssh.com") == 0) { |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1782 | if ((r = sshpkt_get_end(ssh)) != 0) |
| 1783 | goto out; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1784 | chan_rcvd_eow(ssh, c); |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 1785 | } else if (strcmp(rtype, "exit-status") == 0) { |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1786 | if ((r = sshpkt_get_u32(ssh, &exitval)) != 0) |
| 1787 | goto out; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1788 | if (c->ctl_chan != -1) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1789 | mux_exit_message(ssh, c, exitval); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1790 | success = 1; |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1791 | } else if ((int)id == session_ident) { |
Darren Tucker | 2944082 | 2010-01-08 17:08:35 +1100 | [diff] [blame] | 1792 | /* Record exit value of local session */ |
| 1793 | success = 1; |
| 1794 | exit_status = exitval; |
| 1795 | } else { |
Damien Miller | 36f57eb | 2010-01-30 17:28:34 +1100 | [diff] [blame] | 1796 | /* Probably for a mux channel that has already closed */ |
| 1797 | debug("%s: no sink for exit-status on channel %d", |
| 1798 | __func__, id); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1799 | } |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1800 | if ((r = sshpkt_get_end(ssh)) != 0) |
| 1801 | goto out; |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 1802 | } |
Damien Miller | c589378 | 2014-05-15 13:48:49 +1000 | [diff] [blame] | 1803 | if (reply && c != NULL && !(c->flags & CHAN_CLOSE_SENT)) { |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 1804 | if (!c->have_remote_id) |
| 1805 | fatal("%s: channel %d: no remote_id", |
| 1806 | __func__, c->self); |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1807 | if ((r = sshpkt_start(ssh, success ? |
| 1808 | SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE)) != 0 || |
| 1809 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 1810 | (r = sshpkt_send(ssh)) != 0) |
| 1811 | sshpkt_fatal(ssh, r, "%s: send failure", __func__); |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 1812 | } |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1813 | r = 0; |
| 1814 | out: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1815 | free(rtype); |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 1816 | return r; |
Ben Lindstrom | 5b82832 | 2001-02-09 01:34:36 +0000 | [diff] [blame] | 1817 | } |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 1818 | |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 1819 | struct hostkeys_update_ctx { |
| 1820 | /* The hostname and (optionally) IP address string for the server */ |
| 1821 | char *host_str, *ip_str; |
| 1822 | |
| 1823 | /* |
| 1824 | * Keys received from the server and a flag for each indicating |
| 1825 | * whether they already exist in known_hosts. |
| 1826 | * keys_seen is filled in by hostkeys_find() and later (for new |
| 1827 | * keys) by client_global_hostkeys_private_confirm(). |
| 1828 | */ |
| 1829 | struct sshkey **keys; |
| 1830 | int *keys_seen; |
djm@openbsd.org | 84623e0 | 2018-06-26 02:02:36 +0000 | [diff] [blame] | 1831 | size_t nkeys, nnew; |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 1832 | |
| 1833 | /* |
| 1834 | * Keys that are in known_hosts, but were not present in the update |
| 1835 | * from the server (i.e. scheduled to be deleted). |
| 1836 | * Filled in by hostkeys_find(). |
| 1837 | */ |
| 1838 | struct sshkey **old_keys; |
| 1839 | size_t nold; |
| 1840 | }; |
| 1841 | |
| 1842 | static void |
| 1843 | hostkeys_update_ctx_free(struct hostkeys_update_ctx *ctx) |
| 1844 | { |
| 1845 | size_t i; |
| 1846 | |
| 1847 | if (ctx == NULL) |
| 1848 | return; |
| 1849 | for (i = 0; i < ctx->nkeys; i++) |
| 1850 | sshkey_free(ctx->keys[i]); |
| 1851 | free(ctx->keys); |
| 1852 | free(ctx->keys_seen); |
| 1853 | for (i = 0; i < ctx->nold; i++) |
| 1854 | sshkey_free(ctx->old_keys[i]); |
| 1855 | free(ctx->old_keys); |
| 1856 | free(ctx->host_str); |
| 1857 | free(ctx->ip_str); |
| 1858 | free(ctx); |
| 1859 | } |
| 1860 | |
| 1861 | static int |
| 1862 | hostkeys_find(struct hostkey_foreach_line *l, void *_ctx) |
| 1863 | { |
| 1864 | struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx; |
| 1865 | size_t i; |
| 1866 | struct sshkey **tmp; |
| 1867 | |
djm@openbsd.org | 873d3e7 | 2017-04-30 23:18:44 +0000 | [diff] [blame] | 1868 | if (l->status != HKF_STATUS_MATCHED || l->key == NULL) |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 1869 | return 0; |
| 1870 | |
| 1871 | /* Mark off keys we've already seen for this host */ |
| 1872 | for (i = 0; i < ctx->nkeys; i++) { |
| 1873 | if (sshkey_equal(l->key, ctx->keys[i])) { |
| 1874 | debug3("%s: found %s key at %s:%ld", __func__, |
| 1875 | sshkey_ssh_name(ctx->keys[i]), l->path, l->linenum); |
| 1876 | ctx->keys_seen[i] = 1; |
| 1877 | return 0; |
| 1878 | } |
| 1879 | } |
| 1880 | /* This line contained a key that not offered by the server */ |
| 1881 | debug3("%s: deprecated %s key at %s:%ld", __func__, |
| 1882 | sshkey_ssh_name(l->key), l->path, l->linenum); |
deraadt@openbsd.org | 9e509d4 | 2017-05-31 09:15:42 +0000 | [diff] [blame] | 1883 | if ((tmp = recallocarray(ctx->old_keys, ctx->nold, ctx->nold + 1, |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 1884 | sizeof(*ctx->old_keys))) == NULL) |
deraadt@openbsd.org | 9e509d4 | 2017-05-31 09:15:42 +0000 | [diff] [blame] | 1885 | fatal("%s: recallocarray failed nold = %zu", |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 1886 | __func__, ctx->nold); |
| 1887 | ctx->old_keys = tmp; |
| 1888 | ctx->old_keys[ctx->nold++] = l->key; |
| 1889 | l->key = NULL; |
| 1890 | |
| 1891 | return 0; |
| 1892 | } |
| 1893 | |
| 1894 | static void |
djm@openbsd.org | 14ef4ef | 2020-01-29 08:17:49 +0000 | [diff] [blame] | 1895 | hostkey_change_preamble(LogLevel loglevel) |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 1896 | { |
djm@openbsd.org | 101ebc3 | 2020-01-29 07:51:30 +0000 | [diff] [blame] | 1897 | do_log2(loglevel, "The server has updated its host keys."); |
| 1898 | do_log2(loglevel, "These changes were verified by the server's " |
| 1899 | "existing trusted key."); |
| 1900 | } |
| 1901 | |
| 1902 | static void |
| 1903 | update_known_hosts(struct hostkeys_update_ctx *ctx) |
| 1904 | { |
| 1905 | int r, was_raw = 0, first = 1; |
| 1906 | int asking = options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK; |
| 1907 | LogLevel loglevel = asking ? SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_VERBOSE; |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 1908 | char *fp, *response; |
| 1909 | size_t i; |
djm@openbsd.org | 7955633 | 2020-01-25 00:21:08 +0000 | [diff] [blame] | 1910 | struct stat sb; |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 1911 | |
| 1912 | for (i = 0; i < ctx->nkeys; i++) { |
| 1913 | if (ctx->keys_seen[i] != 2) |
| 1914 | continue; |
| 1915 | if ((fp = sshkey_fingerprint(ctx->keys[i], |
| 1916 | options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) |
| 1917 | fatal("%s: sshkey_fingerprint failed", __func__); |
djm@openbsd.org | 101ebc3 | 2020-01-29 07:51:30 +0000 | [diff] [blame] | 1918 | if (first && asking) |
djm@openbsd.org | 14ef4ef | 2020-01-29 08:17:49 +0000 | [diff] [blame] | 1919 | hostkey_change_preamble(loglevel); |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 1920 | do_log2(loglevel, "Learned new hostkey: %s %s", |
| 1921 | sshkey_type(ctx->keys[i]), fp); |
djm@openbsd.org | 101ebc3 | 2020-01-29 07:51:30 +0000 | [diff] [blame] | 1922 | first = 0; |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 1923 | free(fp); |
| 1924 | } |
| 1925 | for (i = 0; i < ctx->nold; i++) { |
| 1926 | if ((fp = sshkey_fingerprint(ctx->old_keys[i], |
| 1927 | options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) |
| 1928 | fatal("%s: sshkey_fingerprint failed", __func__); |
djm@openbsd.org | 101ebc3 | 2020-01-29 07:51:30 +0000 | [diff] [blame] | 1929 | if (first && asking) |
djm@openbsd.org | 14ef4ef | 2020-01-29 08:17:49 +0000 | [diff] [blame] | 1930 | hostkey_change_preamble(loglevel); |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 1931 | do_log2(loglevel, "Deprecating obsolete hostkey: %s %s", |
| 1932 | sshkey_type(ctx->old_keys[i]), fp); |
djm@openbsd.org | 101ebc3 | 2020-01-29 07:51:30 +0000 | [diff] [blame] | 1933 | first = 0; |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 1934 | free(fp); |
| 1935 | } |
| 1936 | if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK) { |
djm@openbsd.org | f2293a6 | 2015-02-23 16:33:25 +0000 | [diff] [blame] | 1937 | if (get_saved_tio() != NULL) { |
| 1938 | leave_raw_mode(1); |
| 1939 | was_raw = 1; |
| 1940 | } |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 1941 | response = NULL; |
| 1942 | for (i = 0; !quit_pending && i < 3; i++) { |
| 1943 | free(response); |
| 1944 | response = read_passphrase("Accept updated hostkeys? " |
| 1945 | "(yes/no): ", RP_ECHO); |
| 1946 | if (strcasecmp(response, "yes") == 0) |
| 1947 | break; |
| 1948 | else if (quit_pending || response == NULL || |
| 1949 | strcasecmp(response, "no") == 0) { |
| 1950 | options.update_hostkeys = 0; |
| 1951 | break; |
| 1952 | } else { |
| 1953 | do_log2(loglevel, "Please enter " |
| 1954 | "\"yes\" or \"no\""); |
| 1955 | } |
| 1956 | } |
| 1957 | if (quit_pending || i >= 3 || response == NULL) |
| 1958 | options.update_hostkeys = 0; |
| 1959 | free(response); |
djm@openbsd.org | f2293a6 | 2015-02-23 16:33:25 +0000 | [diff] [blame] | 1960 | if (was_raw) |
| 1961 | enter_raw_mode(1); |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 1962 | } |
djm@openbsd.org | 7955633 | 2020-01-25 00:21:08 +0000 | [diff] [blame] | 1963 | if (options.update_hostkeys == 0) |
| 1964 | return; |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 1965 | /* |
| 1966 | * Now that all the keys are verified, we can go ahead and replace |
| 1967 | * them in known_hosts (assuming SSH_UPDATE_HOSTKEYS_ASK didn't |
| 1968 | * cancel the operation). |
| 1969 | */ |
djm@openbsd.org | 7955633 | 2020-01-25 00:21:08 +0000 | [diff] [blame] | 1970 | for (i = 0; i < options.num_user_hostfiles; i++) { |
| 1971 | /* |
| 1972 | * NB. keys are only added to hostfiles[0], for the rest we |
| 1973 | * just delete the hostname entries. |
| 1974 | */ |
| 1975 | if (stat(options.user_hostfiles[i], &sb) != 0) { |
| 1976 | if (errno == ENOENT) { |
| 1977 | debug("%s: known hosts file %s does not exist", |
| 1978 | __func__, strerror(errno)); |
| 1979 | } else { |
| 1980 | error("%s: known hosts file %s inaccessible", |
| 1981 | __func__, strerror(errno)); |
| 1982 | } |
| 1983 | continue; |
| 1984 | } |
| 1985 | if ((r = hostfile_replace_entries(options.user_hostfiles[i], |
| 1986 | ctx->host_str, ctx->ip_str, |
| 1987 | i == 0 ? ctx->keys : NULL, i == 0 ? ctx->nkeys : 0, |
| 1988 | options.hash_known_hosts, 0, |
| 1989 | options.fingerprint_hash)) != 0) { |
| 1990 | error("%s: hostfile_replace_entries failed for %s: %s", |
| 1991 | __func__, options.user_hostfiles[i], ssh_err(r)); |
| 1992 | } |
| 1993 | } |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1997 | client_global_hostkeys_private_confirm(struct ssh *ssh, int type, |
| 1998 | u_int32_t seq, void *_ctx) |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 1999 | { |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2000 | struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx; |
| 2001 | size_t i, ndone; |
| 2002 | struct sshbuf *signdata; |
djm@openbsd.org | 7860731 | 2017-12-18 23:16:23 +0000 | [diff] [blame] | 2003 | int r, kexsigtype, use_kexsigtype; |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2004 | const u_char *sig; |
| 2005 | size_t siglen; |
| 2006 | |
| 2007 | if (ctx->nnew == 0) |
| 2008 | fatal("%s: ctx->nnew == 0", __func__); /* sanity */ |
| 2009 | if (type != SSH2_MSG_REQUEST_SUCCESS) { |
| 2010 | error("Server failed to confirm ownership of " |
| 2011 | "private host keys"); |
| 2012 | hostkeys_update_ctx_free(ctx); |
| 2013 | return; |
| 2014 | } |
djm@openbsd.org | 7860731 | 2017-12-18 23:16:23 +0000 | [diff] [blame] | 2015 | kexsigtype = sshkey_type_plain( |
| 2016 | sshkey_type_from_name(ssh->kex->hostkey_alg)); |
| 2017 | |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2018 | if ((signdata = sshbuf_new()) == NULL) |
| 2019 | fatal("%s: sshbuf_new failed", __func__); |
| 2020 | /* Don't want to accidentally accept an unbound signature */ |
| 2021 | if (ssh->kex->session_id_len == 0) |
| 2022 | fatal("%s: ssh->kex->session_id_len == 0", __func__); |
| 2023 | /* |
| 2024 | * Expect a signature for each of the ctx->nnew private keys we |
| 2025 | * haven't seen before. They will be in the same order as the |
| 2026 | * ctx->keys where the corresponding ctx->keys_seen[i] == 0. |
| 2027 | */ |
| 2028 | for (ndone = i = 0; i < ctx->nkeys; i++) { |
| 2029 | if (ctx->keys_seen[i]) |
| 2030 | continue; |
| 2031 | /* Prepare data to be signed: session ID, unique string, key */ |
| 2032 | sshbuf_reset(signdata); |
djm@openbsd.org | 44732de | 2015-02-20 22:17:21 +0000 | [diff] [blame] | 2033 | if ( (r = sshbuf_put_cstring(signdata, |
| 2034 | "hostkeys-prove-00@openssh.com")) != 0 || |
| 2035 | (r = sshbuf_put_string(signdata, ssh->kex->session_id, |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2036 | ssh->kex->session_id_len)) != 0 || |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2037 | (r = sshkey_puts(ctx->keys[i], signdata)) != 0) |
| 2038 | fatal("%s: failed to prepare signature: %s", |
| 2039 | __func__, ssh_err(r)); |
| 2040 | /* Extract and verify signature */ |
| 2041 | if ((r = sshpkt_get_string_direct(ssh, &sig, &siglen)) != 0) { |
| 2042 | error("%s: couldn't parse message: %s", |
| 2043 | __func__, ssh_err(r)); |
| 2044 | goto out; |
| 2045 | } |
djm@openbsd.org | 7860731 | 2017-12-18 23:16:23 +0000 | [diff] [blame] | 2046 | /* |
| 2047 | * For RSA keys, prefer to use the signature type negotiated |
| 2048 | * during KEX to the default (SHA1). |
| 2049 | */ |
| 2050 | use_kexsigtype = kexsigtype == KEY_RSA && |
| 2051 | sshkey_type_plain(ctx->keys[i]->type) == KEY_RSA; |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2052 | if ((r = sshkey_verify(ctx->keys[i], sig, siglen, |
djm@openbsd.org | 04c7e28 | 2017-12-18 02:25:15 +0000 | [diff] [blame] | 2053 | sshbuf_ptr(signdata), sshbuf_len(signdata), |
djm@openbsd.org | b7e74ea | 2019-11-25 00:51:37 +0000 | [diff] [blame] | 2054 | use_kexsigtype ? ssh->kex->hostkey_alg : NULL, 0, |
| 2055 | NULL)) != 0) { |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2056 | error("%s: server gave bad signature for %s key %zu", |
| 2057 | __func__, sshkey_type(ctx->keys[i]), i); |
| 2058 | goto out; |
| 2059 | } |
| 2060 | /* Key is good. Mark it as 'seen' */ |
| 2061 | ctx->keys_seen[i] = 2; |
| 2062 | ndone++; |
| 2063 | } |
| 2064 | if (ndone != ctx->nnew) |
| 2065 | fatal("%s: ndone != ctx->nnew (%zu / %zu)", __func__, |
| 2066 | ndone, ctx->nnew); /* Shouldn't happen */ |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 2067 | if ((r = sshpkt_get_end(ssh)) != 0) { |
| 2068 | error("%s: protocol error", __func__); |
| 2069 | goto out; |
| 2070 | } |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2071 | |
| 2072 | /* Make the edits to known_hosts */ |
| 2073 | update_known_hosts(ctx); |
| 2074 | out: |
| 2075 | hostkeys_update_ctx_free(ctx); |
| 2076 | } |
| 2077 | |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2078 | /* |
djm@openbsd.org | 894221a | 2017-03-10 05:01:13 +0000 | [diff] [blame] | 2079 | * Returns non-zero if the key is accepted by HostkeyAlgorithms. |
| 2080 | * Made slightly less trivial by the multiple RSA signature algorithm names. |
| 2081 | */ |
| 2082 | static int |
| 2083 | key_accepted_by_hostkeyalgs(const struct sshkey *key) |
| 2084 | { |
| 2085 | const char *ktype = sshkey_ssh_name(key); |
dtucker@openbsd.org | c4b3a12 | 2020-01-23 02:46:49 +0000 | [diff] [blame] | 2086 | const char *hostkeyalgs = options.hostkeyalgorithms; |
djm@openbsd.org | 894221a | 2017-03-10 05:01:13 +0000 | [diff] [blame] | 2087 | |
| 2088 | if (key == NULL || key->type == KEY_UNSPEC) |
| 2089 | return 0; |
| 2090 | if (key->type == KEY_RSA && |
| 2091 | (match_pattern_list("rsa-sha2-256", hostkeyalgs, 0) == 1 || |
| 2092 | match_pattern_list("rsa-sha2-512", hostkeyalgs, 0) == 1)) |
| 2093 | return 1; |
| 2094 | return match_pattern_list(ktype, hostkeyalgs, 0) == 1; |
| 2095 | } |
| 2096 | |
| 2097 | /* |
djm@openbsd.org | 44732de | 2015-02-20 22:17:21 +0000 | [diff] [blame] | 2098 | * 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] | 2099 | * the server's hostkeys. The keys are checked against the user's |
| 2100 | * HostkeyAlgorithms preference before they are accepted. |
| 2101 | */ |
| 2102 | static int |
djm@openbsd.org | bc5e116 | 2019-01-19 21:39:12 +0000 | [diff] [blame] | 2103 | client_input_hostkeys(struct ssh *ssh) |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2104 | { |
| 2105 | const u_char *blob = NULL; |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2106 | size_t i, len = 0; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2107 | struct sshbuf *buf = NULL; |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2108 | struct sshkey *key = NULL, **tmp; |
| 2109 | int r; |
| 2110 | char *fp; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2111 | static int hostkeys_seen = 0; /* XXX use struct ssh */ |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 2112 | extern struct sockaddr_storage hostaddr; /* XXX from ssh.c */ |
djm@openbsd.org | a63cfa2 | 2015-02-25 19:54:02 +0000 | [diff] [blame] | 2113 | struct hostkeys_update_ctx *ctx = NULL; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2114 | |
| 2115 | if (hostkeys_seen) |
| 2116 | fatal("%s: server already sent hostkeys", __func__); |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2117 | if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK && |
| 2118 | options.batch_mode) |
| 2119 | 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] | 2120 | if (!options.update_hostkeys || options.num_user_hostfiles <= 0) |
| 2121 | return 1; |
djm@openbsd.org | a63cfa2 | 2015-02-25 19:54:02 +0000 | [diff] [blame] | 2122 | |
| 2123 | ctx = xcalloc(1, sizeof(*ctx)); |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2124 | while (ssh_packet_remaining(ssh) > 0) { |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2125 | sshkey_free(key); |
| 2126 | key = NULL; |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2127 | if ((r = sshpkt_get_string_direct(ssh, &blob, &len)) != 0) { |
| 2128 | error("%s: couldn't parse message: %s", |
| 2129 | __func__, ssh_err(r)); |
| 2130 | goto out; |
| 2131 | } |
djm@openbsd.org | 44732de | 2015-02-20 22:17:21 +0000 | [diff] [blame] | 2132 | if ((r = sshkey_from_blob(blob, len, &key)) != 0) { |
djm@openbsd.org | 74dfc2c | 2020-01-26 00:14:45 +0000 | [diff] [blame] | 2133 | do_log2(r == SSH_ERR_KEY_TYPE_UNKNOWN ? |
| 2134 | SYSLOG_LEVEL_DEBUG1 : SYSLOG_LEVEL_ERROR, |
| 2135 | "%s: parse key: %s", __func__, ssh_err(r)); |
| 2136 | continue; |
djm@openbsd.org | 44732de | 2015-02-20 22:17:21 +0000 | [diff] [blame] | 2137 | } |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2138 | fp = sshkey_fingerprint(key, options.fingerprint_hash, |
| 2139 | SSH_FP_DEFAULT); |
| 2140 | debug3("%s: received %s key %s", __func__, |
| 2141 | sshkey_type(key), fp); |
| 2142 | free(fp); |
markus@openbsd.org | 3a1638d | 2015-07-10 06:21:53 +0000 | [diff] [blame] | 2143 | |
djm@openbsd.org | 894221a | 2017-03-10 05:01:13 +0000 | [diff] [blame] | 2144 | if (!key_accepted_by_hostkeyalgs(key)) { |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2145 | debug3("%s: %s key not permitted by HostkeyAlgorithms", |
| 2146 | __func__, sshkey_ssh_name(key)); |
| 2147 | continue; |
| 2148 | } |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2149 | /* Skip certs */ |
| 2150 | if (sshkey_is_cert(key)) { |
| 2151 | debug3("%s: %s key is a certificate; skipping", |
| 2152 | __func__, sshkey_ssh_name(key)); |
| 2153 | continue; |
| 2154 | } |
| 2155 | /* Ensure keys are unique */ |
| 2156 | for (i = 0; i < ctx->nkeys; i++) { |
| 2157 | if (sshkey_equal(key, ctx->keys[i])) { |
| 2158 | error("%s: received duplicated %s host key", |
| 2159 | __func__, sshkey_ssh_name(key)); |
| 2160 | goto out; |
| 2161 | } |
| 2162 | } |
| 2163 | /* Key is good, record it */ |
deraadt@openbsd.org | 9e509d4 | 2017-05-31 09:15:42 +0000 | [diff] [blame] | 2164 | if ((tmp = recallocarray(ctx->keys, ctx->nkeys, ctx->nkeys + 1, |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2165 | sizeof(*ctx->keys))) == NULL) |
deraadt@openbsd.org | 9e509d4 | 2017-05-31 09:15:42 +0000 | [diff] [blame] | 2166 | fatal("%s: recallocarray failed nkeys = %zu", |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2167 | __func__, ctx->nkeys); |
| 2168 | ctx->keys = tmp; |
| 2169 | ctx->keys[ctx->nkeys++] = key; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2170 | key = NULL; |
| 2171 | } |
| 2172 | |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2173 | if (ctx->nkeys == 0) { |
djm@openbsd.org | 44732de | 2015-02-20 22:17:21 +0000 | [diff] [blame] | 2174 | debug("%s: server sent no hostkeys", __func__); |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2175 | goto out; |
| 2176 | } |
djm@openbsd.org | 44732de | 2015-02-20 22:17:21 +0000 | [diff] [blame] | 2177 | |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2178 | if ((ctx->keys_seen = calloc(ctx->nkeys, |
| 2179 | sizeof(*ctx->keys_seen))) == NULL) |
| 2180 | fatal("%s: calloc failed", __func__); |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2181 | |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 2182 | get_hostfile_hostname_ipaddr(host, |
| 2183 | options.check_host_ip ? (struct sockaddr *)&hostaddr : NULL, |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2184 | options.port, &ctx->host_str, |
| 2185 | options.check_host_ip ? &ctx->ip_str : NULL); |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2186 | |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2187 | /* Find which keys we already know about. */ |
djm@openbsd.org | 7955633 | 2020-01-25 00:21:08 +0000 | [diff] [blame] | 2188 | for (i = 0; i < options.num_user_hostfiles; i++) { |
| 2189 | debug("%s: searching %s for %s / %s", __func__, |
dtucker@openbsd.org | 58c8190 | 2020-02-02 09:22:22 +0000 | [diff] [blame^] | 2190 | options.user_hostfiles[i], ctx->host_str, |
| 2191 | ctx->ip_str ? ctx->ip_str : "(null)"); |
djm@openbsd.org | 7955633 | 2020-01-25 00:21:08 +0000 | [diff] [blame] | 2192 | if ((r = hostkeys_foreach(options.user_hostfiles[i], |
| 2193 | hostkeys_find, ctx, ctx->host_str, ctx->ip_str, |
| 2194 | HKF_WANT_PARSE_KEY|HKF_WANT_MATCH)) != 0) { |
| 2195 | if (r == SSH_ERR_SYSTEM_ERROR && errno == ENOENT) { |
djm@openbsd.org | b596182 | 2020-01-26 00:13:20 +0000 | [diff] [blame] | 2196 | debug("%s: hostkeys file %s does not exist", |
djm@openbsd.org | 7955633 | 2020-01-25 00:21:08 +0000 | [diff] [blame] | 2197 | __func__, options.user_hostfiles[i]); |
| 2198 | continue; |
| 2199 | } |
| 2200 | error("%s: hostkeys_foreach failed for %s: %s", |
| 2201 | __func__, options.user_hostfiles[i], ssh_err(r)); |
| 2202 | goto out; |
| 2203 | } |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2204 | } |
| 2205 | |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2206 | /* Figure out if we have any new keys to add */ |
| 2207 | ctx->nnew = 0; |
| 2208 | for (i = 0; i < ctx->nkeys; i++) { |
| 2209 | if (!ctx->keys_seen[i]) |
| 2210 | ctx->nnew++; |
| 2211 | } |
| 2212 | |
| 2213 | debug3("%s: %zu keys from server: %zu new, %zu retained. %zu to remove", |
| 2214 | __func__, ctx->nkeys, ctx->nnew, ctx->nkeys - ctx->nnew, ctx->nold); |
| 2215 | |
| 2216 | if (ctx->nnew == 0 && ctx->nold != 0) { |
| 2217 | /* We have some keys to remove. Just do it. */ |
| 2218 | update_known_hosts(ctx); |
| 2219 | } else if (ctx->nnew != 0) { |
| 2220 | /* |
| 2221 | * We have received hitherto-unseen keys from the server. |
| 2222 | * Ask the server to confirm ownership of the private halves. |
| 2223 | */ |
| 2224 | debug3("%s: asking server to prove ownership for %zu keys", |
| 2225 | __func__, ctx->nnew); |
| 2226 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 2227 | (r = sshpkt_put_cstring(ssh, |
djm@openbsd.org | 44732de | 2015-02-20 22:17:21 +0000 | [diff] [blame] | 2228 | "hostkeys-prove-00@openssh.com")) != 0 || |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2229 | (r = sshpkt_put_u8(ssh, 1)) != 0) /* bool: want reply */ |
| 2230 | fatal("%s: cannot prepare packet: %s", |
| 2231 | __func__, ssh_err(r)); |
| 2232 | if ((buf = sshbuf_new()) == NULL) |
| 2233 | fatal("%s: sshbuf_new", __func__); |
| 2234 | for (i = 0; i < ctx->nkeys; i++) { |
| 2235 | if (ctx->keys_seen[i]) |
| 2236 | continue; |
| 2237 | sshbuf_reset(buf); |
| 2238 | if ((r = sshkey_putb(ctx->keys[i], buf)) != 0) |
| 2239 | fatal("%s: sshkey_putb: %s", |
| 2240 | __func__, ssh_err(r)); |
| 2241 | if ((r = sshpkt_put_stringb(ssh, buf)) != 0) |
| 2242 | fatal("%s: sshpkt_put_string: %s", |
| 2243 | __func__, ssh_err(r)); |
| 2244 | } |
| 2245 | if ((r = sshpkt_send(ssh)) != 0) |
| 2246 | fatal("%s: sshpkt_send: %s", __func__, ssh_err(r)); |
| 2247 | client_register_global_confirm( |
| 2248 | client_global_hostkeys_private_confirm, ctx); |
| 2249 | ctx = NULL; /* will be freed in callback */ |
| 2250 | } |
| 2251 | |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2252 | /* Success */ |
| 2253 | out: |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2254 | hostkeys_update_ctx_free(ctx); |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2255 | sshkey_free(key); |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2256 | sshbuf_free(buf); |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 2257 | /* |
| 2258 | * NB. Return success for all cases. The server doesn't need to know |
| 2259 | * what the client does with its hosts file. |
| 2260 | */ |
| 2261 | return 1; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2264 | static int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2265 | client_input_global_request(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | c3fa407 | 2002-01-22 23:21:58 +1100 | [diff] [blame] | 2266 | { |
| 2267 | char *rtype; |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 2268 | u_char want_reply; |
| 2269 | int r, success = 0; |
Damien Miller | c3fa407 | 2002-01-22 23:21:58 +1100 | [diff] [blame] | 2270 | |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 2271 | if ((r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 || |
| 2272 | (r = sshpkt_get_u8(ssh, &want_reply)) != 0) |
| 2273 | goto out; |
Damien Miller | 509b010 | 2003-12-17 16:33:10 +1100 | [diff] [blame] | 2274 | debug("client_input_global_request: rtype %s want_reply %d", |
| 2275 | rtype, want_reply); |
djm@openbsd.org | 44732de | 2015-02-20 22:17:21 +0000 | [diff] [blame] | 2276 | if (strcmp(rtype, "hostkeys-00@openssh.com") == 0) |
djm@openbsd.org | bc5e116 | 2019-01-19 21:39:12 +0000 | [diff] [blame] | 2277 | success = client_input_hostkeys(ssh); |
Damien Miller | c3fa407 | 2002-01-22 23:21:58 +1100 | [diff] [blame] | 2278 | if (want_reply) { |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 2279 | if ((r = sshpkt_start(ssh, success ? SSH2_MSG_REQUEST_SUCCESS : |
| 2280 | SSH2_MSG_REQUEST_FAILURE)) != 0 || |
| 2281 | (r = sshpkt_send(ssh)) != 0 || |
| 2282 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 2283 | goto out; |
Damien Miller | c3fa407 | 2002-01-22 23:21:58 +1100 | [diff] [blame] | 2284 | } |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 2285 | r = 0; |
| 2286 | out: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2287 | free(rtype); |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 2288 | return r; |
Damien Miller | c3fa407 | 2002-01-22 23:21:58 +1100 | [diff] [blame] | 2289 | } |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2290 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2291 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2292 | client_session2_setup(struct ssh *ssh, int id, int want_tty, int want_subsystem, |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 2293 | const char *term, struct termios *tiop, int in_fd, struct sshbuf *cmd, |
| 2294 | char **env) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2295 | { |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 2296 | int i, j, matched, len, r; |
djm@openbsd.org | 7082bb5 | 2018-06-09 03:01:12 +0000 | [diff] [blame] | 2297 | char *name, *val; |
Darren Tucker | 5d78de6 | 2004-11-05 20:35:44 +1100 | [diff] [blame] | 2298 | Channel *c = NULL; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2299 | |
| 2300 | debug2("%s: id %d", __func__, id); |
| 2301 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2302 | if ((c = channel_lookup(ssh, id)) == NULL) |
| 2303 | fatal("%s: channel %d: unknown channel", __func__, id); |
Darren Tucker | 5d78de6 | 2004-11-05 20:35:44 +1100 | [diff] [blame] | 2304 | |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 2305 | ssh_packet_set_interactive(ssh, want_tty, |
Damien Miller | 0dac6fb | 2010-11-20 15:19:38 +1100 | [diff] [blame] | 2306 | options.ip_qos_interactive, options.ip_qos_bulk); |
| 2307 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2308 | if (want_tty) { |
| 2309 | struct winsize ws; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2310 | |
| 2311 | /* Store window size in the packet. */ |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 2312 | if (ioctl(in_fd, TIOCGWINSZ, &ws) == -1) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2313 | memset(&ws, 0, sizeof(ws)); |
| 2314 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2315 | channel_request_start(ssh, id, "pty-req", 1); |
| 2316 | client_expect_confirm(ssh, id, "PTY allocation", CONFIRM_TTY); |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 2317 | if ((r = sshpkt_put_cstring(ssh, term != NULL ? term : "")) |
| 2318 | != 0 || |
| 2319 | (r = sshpkt_put_u32(ssh, (u_int)ws.ws_col)) != 0 || |
| 2320 | (r = sshpkt_put_u32(ssh, (u_int)ws.ws_row)) != 0 || |
| 2321 | (r = sshpkt_put_u32(ssh, (u_int)ws.ws_xpixel)) != 0 || |
| 2322 | (r = sshpkt_put_u32(ssh, (u_int)ws.ws_ypixel)) != 0) |
djm@openbsd.org | 9d7b288 | 2019-03-29 11:31:40 +0000 | [diff] [blame] | 2323 | fatal("%s: build packet: %s", __func__, ssh_err(r)); |
Darren Tucker | df189fb | 2008-06-08 12:55:32 +1000 | [diff] [blame] | 2324 | if (tiop == NULL) |
| 2325 | tiop = get_saved_tio(); |
markus@openbsd.org | 89dd615 | 2018-07-09 21:20:26 +0000 | [diff] [blame] | 2326 | ssh_tty_make_modes(ssh, -1, tiop); |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 2327 | if ((r = sshpkt_send(ssh)) != 0) |
djm@openbsd.org | 9d7b288 | 2019-03-29 11:31:40 +0000 | [diff] [blame] | 2328 | fatal("%s: send packet: %s", __func__, ssh_err(r)); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2329 | /* XXX wait for reply */ |
Darren Tucker | 5d78de6 | 2004-11-05 20:35:44 +1100 | [diff] [blame] | 2330 | c->client_tty = 1; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2331 | } |
| 2332 | |
| 2333 | /* Transfer any environment variables from client to server */ |
Damien Miller | 3756dce | 2004-06-18 01:17:29 +1000 | [diff] [blame] | 2334 | if (options.num_send_env != 0 && env != NULL) { |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2335 | debug("Sending environment."); |
Damien Miller | 3756dce | 2004-06-18 01:17:29 +1000 | [diff] [blame] | 2336 | for (i = 0; env[i] != NULL; i++) { |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2337 | /* Split */ |
Damien Miller | 3756dce | 2004-06-18 01:17:29 +1000 | [diff] [blame] | 2338 | name = xstrdup(env[i]); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2339 | if ((val = strchr(name, '=')) == NULL) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2340 | free(name); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2341 | continue; |
| 2342 | } |
| 2343 | *val++ = '\0'; |
| 2344 | |
| 2345 | matched = 0; |
| 2346 | for (j = 0; j < options.num_send_env; j++) { |
| 2347 | if (match_pattern(name, options.send_env[j])) { |
| 2348 | matched = 1; |
| 2349 | break; |
| 2350 | } |
| 2351 | } |
| 2352 | if (!matched) { |
| 2353 | debug3("Ignored env %s", name); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2354 | free(name); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2355 | continue; |
| 2356 | } |
| 2357 | |
| 2358 | debug("Sending env %s = %s", name, val); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2359 | channel_request_start(ssh, id, "env", 0); |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 2360 | if ((r = sshpkt_put_cstring(ssh, name)) != 0 || |
| 2361 | (r = sshpkt_put_cstring(ssh, val)) != 0 || |
djm@openbsd.org | 9d7b288 | 2019-03-29 11:31:40 +0000 | [diff] [blame] | 2362 | (r = sshpkt_send(ssh)) != 0) { |
| 2363 | fatal("%s: send packet: %s", |
| 2364 | __func__, ssh_err(r)); |
| 2365 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2366 | free(name); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2367 | } |
| 2368 | } |
djm@openbsd.org | 7082bb5 | 2018-06-09 03:01:12 +0000 | [diff] [blame] | 2369 | for (i = 0; i < options.num_setenv; i++) { |
| 2370 | /* Split */ |
| 2371 | name = xstrdup(options.setenv[i]); |
| 2372 | if ((val = strchr(name, '=')) == NULL) { |
| 2373 | free(name); |
| 2374 | continue; |
| 2375 | } |
| 2376 | *val++ = '\0'; |
| 2377 | |
| 2378 | debug("Setting env %s = %s", name, val); |
| 2379 | channel_request_start(ssh, id, "env", 0); |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 2380 | if ((r = sshpkt_put_cstring(ssh, name)) != 0 || |
| 2381 | (r = sshpkt_put_cstring(ssh, val)) != 0 || |
| 2382 | (r = sshpkt_send(ssh)) != 0) |
djm@openbsd.org | 9d7b288 | 2019-03-29 11:31:40 +0000 | [diff] [blame] | 2383 | fatal("%s: send packet: %s", __func__, ssh_err(r)); |
djm@openbsd.org | 7082bb5 | 2018-06-09 03:01:12 +0000 | [diff] [blame] | 2384 | free(name); |
| 2385 | } |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2386 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 2387 | len = sshbuf_len(cmd); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2388 | if (len > 0) { |
| 2389 | if (len > 900) |
| 2390 | len = 900; |
| 2391 | if (want_subsystem) { |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 2392 | debug("Sending subsystem: %.*s", |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 2393 | len, (const u_char*)sshbuf_ptr(cmd)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2394 | channel_request_start(ssh, id, "subsystem", 1); |
| 2395 | client_expect_confirm(ssh, id, "subsystem", |
| 2396 | CONFIRM_CLOSE); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2397 | } else { |
Damien Miller | 5771ed7 | 2008-05-19 15:35:33 +1000 | [diff] [blame] | 2398 | debug("Sending command: %.*s", |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 2399 | len, (const u_char*)sshbuf_ptr(cmd)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2400 | channel_request_start(ssh, id, "exec", 1); |
| 2401 | client_expect_confirm(ssh, id, "exec", CONFIRM_CLOSE); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2402 | } |
djm@openbsd.org | 23f22a4 | 2019-01-19 21:33:57 +0000 | [diff] [blame] | 2403 | if ((r = sshpkt_put_stringb(ssh, cmd)) != 0 || |
| 2404 | (r = sshpkt_send(ssh)) != 0) |
djm@openbsd.org | 9d7b288 | 2019-03-29 11:31:40 +0000 | [diff] [blame] | 2405 | fatal("%s: send command: %s", __func__, ssh_err(r)); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2406 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2407 | channel_request_start(ssh, id, "shell", 1); |
| 2408 | client_expect_confirm(ssh, id, "shell", CONFIRM_CLOSE); |
djm@openbsd.org | 9d7b288 | 2019-03-29 11:31:40 +0000 | [diff] [blame] | 2409 | if ((r = sshpkt_send(ssh)) != 0) { |
| 2410 | fatal("%s: send shell request: %s", |
| 2411 | __func__, ssh_err(r)); |
| 2412 | } |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2413 | } |
| 2414 | } |
| 2415 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2416 | static void |
djm@openbsd.org | bc5e116 | 2019-01-19 21:39:12 +0000 | [diff] [blame] | 2417 | client_init_dispatch(struct ssh *ssh) |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 2418 | { |
djm@openbsd.org | bc5e116 | 2019-01-19 21:39:12 +0000 | [diff] [blame] | 2419 | ssh_dispatch_init(ssh, &dispatch_protocol_error); |
Damien Miller | 2797f7f | 2002-04-23 21:09:44 +1000 | [diff] [blame] | 2420 | |
djm@openbsd.org | bc5e116 | 2019-01-19 21:39:12 +0000 | [diff] [blame] | 2421 | ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose); |
| 2422 | ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_DATA, &channel_input_data); |
| 2423 | ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_EOF, &channel_input_ieof); |
| 2424 | ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data); |
| 2425 | ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open); |
| 2426 | ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); |
| 2427 | ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); |
| 2428 | ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req); |
| 2429 | ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust); |
| 2430 | ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm); |
| 2431 | ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm); |
| 2432 | ssh_dispatch_set(ssh, SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request); |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 2433 | |
| 2434 | /* rekeying */ |
djm@openbsd.org | bc5e116 | 2019-01-19 21:39:12 +0000 | [diff] [blame] | 2435 | ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit); |
Damien Miller | 2797f7f | 2002-04-23 21:09:44 +1000 | [diff] [blame] | 2436 | |
| 2437 | /* global request reply messages */ |
djm@openbsd.org | bc5e116 | 2019-01-19 21:39:12 +0000 | [diff] [blame] | 2438 | ssh_dispatch_set(ssh, SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply); |
| 2439 | ssh_dispatch_set(ssh, SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 2440 | } |
Darren Tucker | 4d5cd33 | 2008-06-13 04:51:14 +1000 | [diff] [blame] | 2441 | |
Damien Miller | 6c3eec7 | 2011-05-05 14:16:22 +1000 | [diff] [blame] | 2442 | void |
| 2443 | client_stop_mux(void) |
| 2444 | { |
| 2445 | if (options.control_path != NULL && muxserver_sock != -1) |
| 2446 | unlink(options.control_path); |
| 2447 | /* |
Darren Tucker | 48bf4b0 | 2012-09-07 16:38:53 +1000 | [diff] [blame] | 2448 | * If we are in persist mode, or don't have a shell, signal that we |
| 2449 | * should close when all active channels are closed. |
Damien Miller | 6c3eec7 | 2011-05-05 14:16:22 +1000 | [diff] [blame] | 2450 | */ |
Darren Tucker | 48bf4b0 | 2012-09-07 16:38:53 +1000 | [diff] [blame] | 2451 | if (options.control_persist || no_shell_flag) { |
Damien Miller | 6c3eec7 | 2011-05-05 14:16:22 +1000 | [diff] [blame] | 2452 | session_closed = 1; |
Damien Miller | 4ac99c3 | 2011-06-20 14:43:31 +1000 | [diff] [blame] | 2453 | setproctitle("[stopped mux]"); |
| 2454 | } |
Damien Miller | 6c3eec7 | 2011-05-05 14:16:22 +1000 | [diff] [blame] | 2455 | } |
| 2456 | |
Darren Tucker | 9a2c4cd | 2003-09-22 21:16:05 +1000 | [diff] [blame] | 2457 | /* client specific fatal cleanup */ |
| 2458 | void |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 2459 | cleanup_exit(int i) |
Darren Tucker | 9a2c4cd | 2003-09-22 21:16:05 +1000 | [diff] [blame] | 2460 | { |
Damien Miller | 21771e2 | 2011-05-15 08:45:50 +1000 | [diff] [blame] | 2461 | leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); |
Darren Tucker | 9a2c4cd | 2003-09-22 21:16:05 +1000 | [diff] [blame] | 2462 | leave_non_blocking(); |
Damien Miller | b1cbfa2 | 2008-05-19 16:00:08 +1000 | [diff] [blame] | 2463 | if (options.control_path != NULL && muxserver_sock != -1) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2464 | unlink(options.control_path); |
Damien Miller | a41ccca | 2010-10-07 22:07:32 +1100 | [diff] [blame] | 2465 | ssh_kill_proxy_command(); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 2466 | _exit(i); |
Darren Tucker | 9a2c4cd | 2003-09-22 21:16:05 +1000 | [diff] [blame] | 2467 | } |