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