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