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