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