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