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