blob: d4c23d5541f9d959c82d2e17e4d039bfabebf441 [file] [log] [blame]
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +00001/* $OpenBSD: clientloop.c,v 1.332 2020/01/23 07:10:22 dtucker Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * The main loop for the interactive session (client side).
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * 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 Miller1383bd82000-04-06 12:32:37 +100038 * SSH2 support added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000039 * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110040 *
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 Miller95def091999-11-25 00:26:21 +110060 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
62#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110063
Damien Millerf17883e2006-03-15 11:45:54 +110064#include <sys/types.h>
Damien Miller9aec9192006-08-05 10:57:45 +100065#include <sys/ioctl.h>
Damien Millerf17883e2006-03-15 11:45:54 +110066#ifdef HAVE_SYS_STAT_H
67# include <sys/stat.h>
68#endif
Damien Miller9aec9192006-08-05 10:57:45 +100069#ifdef HAVE_SYS_TIME_H
70# include <sys/time.h>
71#endif
Damien Millere3b60b52006-07-10 21:08:03 +100072#include <sys/socket.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110073
Damien Millerc7b06362006-03-15 11:53:45 +110074#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100075#include <errno.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110076#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110077#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110078#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110079#include <signal.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100080#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100081#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100082#include <string.h>
deraadt@openbsd.org72687c82019-11-13 04:47:52 +000083#include <stdarg.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110084#include <termios.h>
Damien Millerd7834352006-08-05 12:39:39 +100085#include <pwd.h>
Damien Millere6b3b612006-07-24 14:01:23 +100086#include <unistd.h>
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000087#include <limits.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
Damien Millerb84886b2008-05-19 15:05:07 +100089#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100090#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000092#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093#include "packet.h"
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +000094#include "sshbuf.h"
Damien Millerb38eff82000-04-01 11:09:21 +100095#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000096#include "channels.h"
Damien Millerb38eff82000-04-01 11:09:21 +100097#include "dispatch.h"
markus@openbsd.org5467fbc2018-07-11 18:53:29 +000098#include "sshkey.h"
Damien Millerd7834352006-08-05 12:39:39 +100099#include "cipher.h"
Ben Lindstromf28f6342001-04-04 02:03:04 +0000100#include "kex.h"
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000101#include "myproposal.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000102#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +1000103#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000104#include "readconf.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000105#include "clientloop.h"
Damien Milleraeb31d62005-12-13 19:29:36 +1100106#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000107#include "authfd.h"
108#include "atomicio.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +1000109#include "sshpty.h"
Damien Miller0e220db2004-06-15 10:34:08 +1000110#include "match.h"
111#include "msg.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +0000112#include "ssherr.h"
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000113#include "hostfile.h"
Damien Miller69b69aa2000-10-28 14:19:58 +1100114
115/* import options */
116extern Options options;
117
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118/* Flag indicating that stdin should be redirected from /dev/null. */
119extern int stdin_null_flag;
120
Damien Millerd6965512003-12-17 16:31:53 +1100121/* Flag indicating that no shell has been requested */
122extern int no_shell_flag;
123
djm@openbsd.org368dd972016-07-23 02:54:08 +0000124/* Flag indicating that ssh should daemonise after authentication is complete */
125extern int fork_after_authentication_flag;
126
Damien Miller0e220db2004-06-15 10:34:08 +1000127/* Control socket */
Damien Millere1537f92010-01-26 13:26:22 +1100128extern int muxserver_sock; /* XXX use mux_client_cleanup() instead */
Damien Miller0e220db2004-06-15 10:34:08 +1000129
Damien Miller5428f641999-11-25 11:54:57 +1100130/*
131 * Name of the host we are connecting to. This is the name given on the
jmc@openbsd.org73491492019-06-12 11:31:50 +0000132 * command line, or the Hostname specified for the user-supplied name in a
Damien Miller5428f641999-11-25 11:54:57 +1100133 * configuration file.
134 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135extern char *host;
136
Damien Miller5428f641999-11-25 11:54:57 +1100137/*
djm@openbsd.org40be78f2019-12-21 02:19:13 +0000138 * If this field is not NULL, the ForwardAgent socket is this path and different
139 * instead of SSH_AUTH_SOCK.
140 */
141extern char *forward_agent_sock_path;
142
143/*
Damien Miller5428f641999-11-25 11:54:57 +1100144 * Flag to indicate that we have received a window change signal which has
145 * not yet been processed. This will cause a message indicating the new
146 * window size to be sent to the server a little later. This is volatile
147 * because this is updated in a signal handler.
148 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000149static volatile sig_atomic_t received_window_change_signal = 0;
150static volatile sig_atomic_t received_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151
Damien Miller788f2122005-11-05 15:14:59 +1100152/* Flag indicating whether the user's terminal is in non-blocking mode. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153static int in_non_blocking_mode = 0;
154
Damien Millere11e1ea2010-08-03 16:04:46 +1000155/* Time when backgrounded control master using ControlPersist should exit */
156static time_t control_persist_exit_time = 0;
157
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158/* Common data for the client loop code. */
Damien Millere1537f92010-01-26 13:26:22 +1100159volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
Damien Miller95def091999-11-25 00:26:21 +1100160static int last_was_cr; /* Last character was a newline. */
Darren Tucker4d5cd332008-06-13 04:51:14 +1000161static int exit_status; /* Used to store the command exit status. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000162static struct sshbuf *stderr_buffer; /* Used for final exit message. */
Damien Miller95def091999-11-25 00:26:21 +1100163static int connection_in; /* Connection to server (input). */
164static int connection_out; /* Connection to server (output). */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000165static int need_rekeying; /* Set to non-zero if rekeying is requested. */
Damien Miller1ab6a512010-06-26 10:02:24 +1000166static int session_closed; /* In SSH2: login session closed. */
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000167static u_int x11_refuse_time; /* If >0, refuse x11 opens after this time. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +0000169static void client_init_dispatch(struct ssh *ssh);
Damien Miller1383bd82000-04-06 12:32:37 +1000170int session_ident = -1;
171
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000172/* Track escape per proto2 channel */
173struct escape_filter_ctx {
174 int escape_pending;
175 int escape_char;
176};
177
178/* Context for channel confirmation replies */
Damien Miller5771ed72008-05-19 15:35:33 +1000179struct channel_reply_ctx {
180 const char *request_type;
Damien Miller555f3b82011-05-15 08:48:05 +1000181 int id;
182 enum confirm_action action;
Damien Miller5771ed72008-05-19 15:35:33 +1000183};
184
Darren Tucker9f407c42008-06-13 04:50:27 +1000185/* Global request success/failure callbacks */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000186/* XXX move to struct ssh? */
Darren Tucker9f407c42008-06-13 04:50:27 +1000187struct global_confirm {
188 TAILQ_ENTRY(global_confirm) entry;
189 global_confirm_cb *cb;
190 void *ctx;
191 int ref_count;
192};
193TAILQ_HEAD(global_confirms, global_confirm);
194static struct global_confirms global_confirms =
195 TAILQ_HEAD_INITIALIZER(global_confirms);
196
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000197void ssh_process_session2_setup(int, int, int, struct sshbuf *);
Damien Miller0e220db2004-06-15 10:34:08 +1000198
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199/* Restores stdin to blocking mode. */
200
Ben Lindstrombba81212001-06-25 05:01:22 +0000201static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000202leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203{
Damien Miller95def091999-11-25 00:26:21 +1100204 if (in_non_blocking_mode) {
Damien Miller03e66f62004-06-15 15:47:51 +1000205 unset_nonblock(fileno(stdin));
Damien Miller95def091999-11-25 00:26:21 +1100206 in_non_blocking_mode = 0;
Damien Miller95def091999-11-25 00:26:21 +1100207 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000208}
209
Damien Miller5428f641999-11-25 11:54:57 +1100210/*
211 * Signal handler for the window change signal (SIGWINCH). This just sets a
212 * flag indicating that the window has changed.
213 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100214/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000215static void
Damien Miller95def091999-11-25 00:26:21 +1100216window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217{
Damien Miller95def091999-11-25 00:26:21 +1100218 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000219}
220
Damien Miller5428f641999-11-25 11:54:57 +1100221/*
222 * Signal handler for signals that cause the program to terminate. These
223 * signals must be trapped to restore terminal modes.
224 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100225/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000226static void
Damien Miller95def091999-11-25 00:26:21 +1100227signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000229 received_signal = sig;
230 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231}
232
Damien Miller5428f641999-11-25 11:54:57 +1100233/*
Damien Millere11e1ea2010-08-03 16:04:46 +1000234 * Sets control_persist_exit_time to the absolute time when the
235 * backgrounded control master should exit due to expiry of the
236 * ControlPersist timeout. Sets it to 0 if we are not a backgrounded
237 * control master process, or if there is no ControlPersist timeout.
238 */
239static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000240set_control_persist_exit_time(struct ssh *ssh)
Damien Millere11e1ea2010-08-03 16:04:46 +1000241{
242 if (muxserver_sock == -1 || !options.control_persist
Damien Miller6c3eec72011-05-05 14:16:22 +1000243 || options.control_persist_timeout == 0) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000244 /* not using a ControlPersist timeout */
245 control_persist_exit_time = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000246 } else if (channel_still_open(ssh)) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000247 /* some client connections are still open */
248 if (control_persist_exit_time > 0)
249 debug2("%s: cancel scheduled exit", __func__);
250 control_persist_exit_time = 0;
251 } else if (control_persist_exit_time <= 0) {
252 /* a client connection has recently closed */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000253 control_persist_exit_time = monotime() +
Damien Millere11e1ea2010-08-03 16:04:46 +1000254 (time_t)options.control_persist_timeout;
255 debug2("%s: schedule exit in %d seconds", __func__,
256 options.control_persist_timeout);
257 }
258 /* else we are already counting down to the timeout */
259}
260
Damien Miller8d60be52012-02-11 08:18:17 +1100261#define SSH_X11_VALID_DISPLAY_CHARS ":/.-_"
262static int
263client_x11_display_valid(const char *display)
264{
265 size_t i, dlen;
266
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000267 if (display == NULL)
268 return 0;
269
Damien Miller8d60be52012-02-11 08:18:17 +1100270 dlen = strlen(display);
271 for (i = 0; i < dlen; i++) {
Damien Millerfdb23062013-11-21 13:57:15 +1100272 if (!isalnum((u_char)display[i]) &&
Damien Miller8d60be52012-02-11 08:18:17 +1100273 strchr(SSH_X11_VALID_DISPLAY_CHARS, display[i]) == NULL) {
274 debug("Invalid character '%c' in DISPLAY", display[i]);
275 return 0;
276 }
277 }
278 return 1;
279}
280
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000281#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
282#define X11_TIMEOUT_SLACK 60
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000283int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000284client_x11_get_proto(struct ssh *ssh, const char *display,
285 const char *xauth_path, u_int trusted, u_int timeout,
286 char **_proto, char **_data)
Damien Miller17e7ed02005-06-17 12:54:33 +1000287{
djm@openbsd.orgb5e412a2018-09-21 12:46:22 +0000288 char *cmd, line[512], xdisplay[512];
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000289 char xauthfile[PATH_MAX], xauthdir[PATH_MAX];
Damien Miller17e7ed02005-06-17 12:54:33 +1000290 static char proto[512], data[512];
291 FILE *f;
tedu@openbsd.org10363562016-09-17 18:00:27 +0000292 int got_data = 0, generated = 0, do_unlink = 0, r;
Damien Miller17e7ed02005-06-17 12:54:33 +1000293 struct stat st;
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000294 u_int now, x11_timeout_real;
Damien Miller17e7ed02005-06-17 12:54:33 +1000295
Damien Miller17e7ed02005-06-17 12:54:33 +1000296 *_proto = proto;
297 *_data = data;
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000298 proto[0] = data[0] = xauthfile[0] = xauthdir[0] = '\0';
Damien Miller17e7ed02005-06-17 12:54:33 +1000299
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000300 if (!client_x11_display_valid(display)) {
millert@openbsd.org5658ef22016-02-01 21:18:17 +0000301 if (display != NULL)
302 logit("DISPLAY \"%s\" invalid; disabling X11 forwarding",
303 display);
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000304 return -1;
305 }
306 if (xauth_path != NULL && stat(xauth_path, &st) == -1) {
307 debug("No xauth program.");
308 xauth_path = NULL;
309 }
310
311 if (xauth_path != NULL) {
Damien Miller17e7ed02005-06-17 12:54:33 +1000312 /*
313 * Handle FamilyLocal case where $DISPLAY does
314 * not match an authorization entry. For this we
315 * just try "xauth list unix:displaynum.screennum".
316 * XXX: "localhost" match to determine FamilyLocal
317 * is not perfect.
318 */
319 if (strncmp(display, "localhost:", 10) == 0) {
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000320 if ((r = snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
321 display + 10)) < 0 ||
322 (size_t)r >= sizeof(xdisplay)) {
323 error("%s: display name too long", __func__);
324 return -1;
325 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000326 display = xdisplay;
327 }
328 if (trusted == 0) {
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000329 /*
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000330 * Generate an untrusted X11 auth cookie.
331 *
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000332 * The authentication cookie should briefly outlive
333 * ssh's willingness to forward X11 connections to
334 * avoid nasty fail-open behaviour in the X server.
335 */
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000336 mktemp_proto(xauthdir, sizeof(xauthdir));
337 if (mkdtemp(xauthdir) == NULL) {
338 error("%s: mkdtemp: %s",
339 __func__, strerror(errno));
340 return -1;
341 }
342 do_unlink = 1;
343 if ((r = snprintf(xauthfile, sizeof(xauthfile),
344 "%s/xauthfile", xauthdir)) < 0 ||
345 (size_t)r >= sizeof(xauthfile)) {
346 error("%s: xauthfile path too long", __func__);
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000347 rmdir(xauthdir);
348 return -1;
349 }
350
djm@openbsd.orgb5e412a2018-09-21 12:46:22 +0000351 if (timeout == 0) {
352 /* auth doesn't time out */
353 xasprintf(&cmd, "%s -f %s generate %s %s "
354 "untrusted 2>%s",
355 xauth_path, xauthfile, display,
356 SSH_X11_PROTO, _PATH_DEVNULL);
357 } else {
358 /* Add some slack to requested expiry */
359 if (timeout < UINT_MAX - X11_TIMEOUT_SLACK)
360 x11_timeout_real = timeout +
361 X11_TIMEOUT_SLACK;
362 else {
363 /* Don't overflow on long timeouts */
364 x11_timeout_real = UINT_MAX;
365 }
366 xasprintf(&cmd, "%s -f %s generate %s %s "
367 "untrusted timeout %u 2>%s",
368 xauth_path, xauthfile, display,
369 SSH_X11_PROTO, x11_timeout_real,
370 _PATH_DEVNULL);
371 }
djm@openbsd.org9d7b2882019-03-29 11:31:40 +0000372 debug2("%s: xauth command: %s", __func__, cmd);
djm@openbsd.orgb5e412a2018-09-21 12:46:22 +0000373
374 if (timeout != 0 && x11_refuse_time == 0) {
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000375 now = monotime() + 1;
376 if (UINT_MAX - timeout < now)
377 x11_refuse_time = UINT_MAX;
378 else
379 x11_refuse_time = now + timeout;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000380 channel_set_x11_refuse_time(ssh,
381 x11_refuse_time);
Damien Miller17e7ed02005-06-17 12:54:33 +1000382 }
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000383 if (system(cmd) == 0)
384 generated = 1;
djm@openbsd.orgb5e412a2018-09-21 12:46:22 +0000385 free(cmd);
Damien Miller17e7ed02005-06-17 12:54:33 +1000386 }
Darren Tucker513d13a2007-08-15 19:13:41 +1000387
388 /*
389 * When in untrusted mode, we read the cookie only if it was
390 * successfully generated as an untrusted one in the step
391 * above.
392 */
393 if (trusted || generated) {
djm@openbsd.orgb5e412a2018-09-21 12:46:22 +0000394 xasprintf(&cmd,
Darren Tucker513d13a2007-08-15 19:13:41 +1000395 "%s %s%s list %s 2>" _PATH_DEVNULL,
396 xauth_path,
397 generated ? "-f " : "" ,
398 generated ? xauthfile : "",
399 display);
400 debug2("x11_get_proto: %s", cmd);
401 f = popen(cmd, "r");
402 if (f && fgets(line, sizeof(line), f) &&
403 sscanf(line, "%*s %511s %511s", proto, data) == 2)
404 got_data = 1;
405 if (f)
406 pclose(f);
djm@openbsd.orgb5e412a2018-09-21 12:46:22 +0000407 free(cmd);
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000408 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000409 }
410
411 if (do_unlink) {
412 unlink(xauthfile);
413 rmdir(xauthdir);
414 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000415
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000416 /* Don't fall back to fake X11 data for untrusted forwarding */
417 if (!trusted && !got_data) {
418 error("Warning: untrusted X11 forwarding setup failed: "
419 "xauth key data not generated");
420 return -1;
421 }
422
Damien Miller17e7ed02005-06-17 12:54:33 +1000423 /*
424 * If we didn't get authentication data, just make up some
425 * data. The forwarding code will check the validity of the
426 * response anyway, and substitute this data. The X11
427 * server, however, will ignore this fake data and use
428 * whatever authentication mechanisms it was using otherwise
429 * for the local connection.
430 */
431 if (!got_data) {
tedu@openbsd.org10363562016-09-17 18:00:27 +0000432 u_int8_t rnd[16];
433 u_int i;
Damien Miller17e7ed02005-06-17 12:54:33 +1000434
435 logit("Warning: No xauth data; "
436 "using fake authentication data for X11 forwarding.");
437 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
tedu@openbsd.org10363562016-09-17 18:00:27 +0000438 arc4random_buf(rnd, sizeof(rnd));
439 for (i = 0; i < sizeof(rnd); i++) {
Damien Miller17e7ed02005-06-17 12:54:33 +1000440 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
tedu@openbsd.org10363562016-09-17 18:00:27 +0000441 rnd[i]);
Damien Miller17e7ed02005-06-17 12:54:33 +1000442 }
443 }
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000444
445 return 0;
Damien Miller17e7ed02005-06-17 12:54:33 +1000446}
447
Damien Miller5428f641999-11-25 11:54:57 +1100448/*
Damien Miller5428f641999-11-25 11:54:57 +1100449 * Checks if the client window has changed, and sends a packet about it to
450 * the server if so. The actual change is detected elsewhere (by a software
451 * interrupt on Unix); this just checks the flag and sends a message if
452 * appropriate.
453 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000454
Ben Lindstrombba81212001-06-25 05:01:22 +0000455static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000456client_check_window_change(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457{
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000458 if (!received_window_change_signal)
Damien Miller1383bd82000-04-06 12:32:37 +1000459 return;
460 /** XXX race */
461 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000462
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000463 debug2("%s: changed", __func__);
Damien Miller1383bd82000-04-06 12:32:37 +1000464
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000465 channel_send_window_changes(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000466}
467
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000468static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000469client_global_request_reply(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller509b0102003-12-17 16:33:10 +1100470{
Darren Tucker9f407c42008-06-13 04:50:27 +1000471 struct global_confirm *gc;
472
473 if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000474 return 0;
Darren Tucker9f407c42008-06-13 04:50:27 +1000475 if (gc->cb != NULL)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000476 gc->cb(ssh, type, seq, gc->ctx);
Darren Tucker9f407c42008-06-13 04:50:27 +1000477 if (--gc->ref_count <= 0) {
478 TAILQ_REMOVE(&global_confirms, gc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +1100479 explicit_bzero(gc, sizeof(*gc));
Darren Tuckera627d422013-06-02 07:31:17 +1000480 free(gc);
Darren Tucker9f407c42008-06-13 04:50:27 +1000481 }
482
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000483 ssh_packet_set_alive_timeouts(ssh, 0);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000484 return 0;
Damien Miller509b0102003-12-17 16:33:10 +1100485}
486
487static void
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000488server_alive_check(struct ssh *ssh)
Damien Miller509b0102003-12-17 16:33:10 +1100489{
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000490 int r;
491
492 if (ssh_packet_inc_alive_timeouts(ssh) > options.server_alive_count_max) {
Damien Millerb73b6fd2011-01-11 17:18:56 +1100493 logit("Timeout, server %s not responding.", host);
Damien Miller985a4482006-10-24 03:02:41 +1000494 cleanup_exit(255);
495 }
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000496 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
497 (r = sshpkt_put_cstring(ssh, "keepalive@openssh.com")) != 0 ||
498 (r = sshpkt_put_u8(ssh, 1)) != 0 || /* boolean: want reply */
499 (r = sshpkt_send(ssh)) != 0)
djm@openbsd.org9d7b2882019-03-29 11:31:40 +0000500 fatal("%s: send packet: %s", __func__, ssh_err(r));
Darren Tucker9f407c42008-06-13 04:50:27 +1000501 /* Insert an empty placeholder to maintain ordering */
502 client_register_global_confirm(NULL, NULL);
Damien Miller509b0102003-12-17 16:33:10 +1100503}
504
Damien Miller5428f641999-11-25 11:54:57 +1100505/*
506 * Waits until the client can do something (some data becomes available on
507 * one of the file descriptors).
508 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000509static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000510client_wait_until_can_do_something(struct ssh *ssh,
511 fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000512 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000513{
Damien Miller509b0102003-12-17 16:33:10 +1100514 struct timeval tv, *tvp;
Damien Millere11e1ea2010-08-03 16:04:46 +1000515 int timeout_secs;
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000516 time_t minwait_secs = 0, server_alive_time = 0, now = monotime();
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000517 int r, ret;
Damien Miller509b0102003-12-17 16:33:10 +1100518
Damien Miller5e953212001-01-30 09:14:00 +1100519 /* Add any selections by the channel mechanism. */
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +0000520 channel_prepare_select(ssh, readsetp, writesetp, maxfdp,
djm@openbsd.org71e5a532017-08-30 03:59:08 +0000521 nallocp, &minwait_secs);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000522
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000523 /* channel_prepare_select could have closed the last channel */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000524 if (session_closed && !channel_still_open(ssh) &&
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000525 !ssh_packet_have_data_to_write(ssh)) {
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000526 /* clear mask since we did not call select() */
527 memset(*readsetp, 0, *nallocp);
528 memset(*writesetp, 0, *nallocp);
529 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000530 }
531
djm@openbsd.org05164352017-04-30 23:21:54 +0000532 FD_SET(connection_in, *readsetp);
533
Damien Miller95def091999-11-25 00:26:21 +1100534 /* Select server connection if have data to write to the server. */
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000535 if (ssh_packet_have_data_to_write(ssh))
Damien Miller5e953212001-01-30 09:14:00 +1100536 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100537
Damien Miller5428f641999-11-25 11:54:57 +1100538 /*
539 * Wait for something to happen. This will suspend the process until
540 * some selected descriptor can be read, written, or has some other
Damien Millere11e1ea2010-08-03 16:04:46 +1000541 * event pending, or a timeout expires.
Damien Miller5428f641999-11-25 11:54:57 +1100542 */
Damien Miller95def091999-11-25 00:26:21 +1100543
Damien Millere11e1ea2010-08-03 16:04:46 +1000544 timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000545 if (options.server_alive_interval > 0) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000546 timeout_secs = options.server_alive_interval;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000547 server_alive_time = now + options.server_alive_interval;
548 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000549 if (options.rekey_interval > 0 && !rekeying)
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000550 timeout_secs = MINIMUM(timeout_secs,
551 ssh_packet_get_rekey_timeout(ssh));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000552 set_control_persist_exit_time(ssh);
Damien Millere11e1ea2010-08-03 16:04:46 +1000553 if (control_persist_exit_time > 0) {
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000554 timeout_secs = MINIMUM(timeout_secs,
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000555 control_persist_exit_time - now);
Damien Millere11e1ea2010-08-03 16:04:46 +1000556 if (timeout_secs < 0)
557 timeout_secs = 0;
558 }
Damien Millera6508752012-04-22 11:21:10 +1000559 if (minwait_secs != 0)
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000560 timeout_secs = MINIMUM(timeout_secs, (int)minwait_secs);
Damien Millere11e1ea2010-08-03 16:04:46 +1000561 if (timeout_secs == INT_MAX)
Damien Miller509b0102003-12-17 16:33:10 +1100562 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000563 else {
Damien Millere11e1ea2010-08-03 16:04:46 +1000564 tv.tv_sec = timeout_secs;
Damien Miller509b0102003-12-17 16:33:10 +1100565 tv.tv_usec = 0;
566 tvp = &tv;
567 }
Damien Millere11e1ea2010-08-03 16:04:46 +1000568
Damien Miller509b0102003-12-17 16:33:10 +1100569 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000570 if (ret == -1) {
Ben Lindstromf9452512001-02-15 03:12:08 +0000571 /*
572 * We have to clear the select masks, because we return.
573 * We have to return, because the mainloop checks for the flags
574 * set by the signal handlers.
575 */
Damien Miller79faeff2001-11-12 11:06:32 +1100576 memset(*readsetp, 0, *nallocp);
577 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000578
Damien Miller95def091999-11-25 00:26:21 +1100579 if (errno == EINTR)
580 return;
581 /* Note: we might still have data in the buffers. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000582 if ((r = sshbuf_putf(stderr_buffer,
583 "select: %s\r\n", strerror(errno))) != 0)
584 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +1100585 quit_pending = 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000586 } else if (ret == 0) {
587 /*
588 * Timeout. Could have been either keepalive or rekeying.
589 * Keepalive we check here, rekeying is checked in clientloop.
590 */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000591 if (server_alive_time != 0 && server_alive_time <= monotime())
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000592 server_alive_check(ssh);
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000593 }
594
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000595}
596
Ben Lindstrombba81212001-06-25 05:01:22 +0000597static void
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000598client_suspend_self(struct sshbuf *bin, struct sshbuf *bout, struct sshbuf *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000599{
Damien Miller95def091999-11-25 00:26:21 +1100600 /* Flush stdout and stderr buffers. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000601 if (sshbuf_len(bout) > 0)
602 atomicio(vwrite, fileno(stdout), sshbuf_mutable_ptr(bout),
603 sshbuf_len(bout));
604 if (sshbuf_len(berr) > 0)
605 atomicio(vwrite, fileno(stderr), sshbuf_mutable_ptr(berr),
606 sshbuf_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000607
Damien Miller21771e22011-05-15 08:45:50 +1000608 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000609
djm@openbsd.org5b8da1f2017-09-19 04:24:22 +0000610 sshbuf_reset(bin);
611 sshbuf_reset(bout);
612 sshbuf_reset(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000613
Damien Miller95def091999-11-25 00:26:21 +1100614 /* Send the suspend signal to the program itself. */
615 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000616
Darren Tucker5d78de62004-11-05 20:35:44 +1100617 /* Reset window sizes in case they have changed */
618 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000619
Damien Miller21771e22011-05-15 08:45:50 +1000620 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000621}
622
Ben Lindstrombba81212001-06-25 05:01:22 +0000623static void
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000624client_process_net_input(struct ssh *ssh, fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000625{
Darren Tucker86e30a02009-08-28 11:21:06 +1000626 char buf[SSH_IOBUFSZ];
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000627 int r, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000628
Damien Miller5428f641999-11-25 11:54:57 +1100629 /*
630 * Read input from the server, and add any such data to the buffer of
631 * the packet subsystem.
632 */
Damien Miller95def091999-11-25 00:26:21 +1100633 if (FD_ISSET(connection_in, readset)) {
634 /* Read as much as possible. */
markus@openbsd.orga3068632016-01-14 16:17:39 +0000635 len = read(connection_in, buf, sizeof(buf));
636 if (len == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000637 /*
638 * Received EOF. The remote host has closed the
639 * connection.
640 */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000641 if ((r = sshbuf_putf(stderr_buffer,
Darren Tucker4d5cd332008-06-13 04:51:14 +1000642 "Connection to %.300s closed by remote host.\r\n",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000643 host)) != 0)
644 fatal("%s: buffer error: %s",
645 __func__, ssh_err(r));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000646 quit_pending = 1;
647 return;
Damien Miller95def091999-11-25 00:26:21 +1100648 }
Damien Miller5428f641999-11-25 11:54:57 +1100649 /*
650 * There is a kernel bug on Solaris that causes select to
651 * sometimes wake up even though there is no data available.
652 */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000653 if (len == -1 &&
Damien Millerd8968ad2008-07-04 23:10:49 +1000654 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Damien Miller95def091999-11-25 00:26:21 +1100655 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000656
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000657 if (len == -1) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000658 /*
659 * An error has encountered. Perhaps there is a
660 * network problem.
661 */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000662 if ((r = sshbuf_putf(stderr_buffer,
Darren Tucker4d5cd332008-06-13 04:51:14 +1000663 "Read from remote host %.300s: %.100s\r\n",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000664 host, strerror(errno))) != 0)
665 fatal("%s: buffer error: %s",
666 __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +1100667 quit_pending = 1;
668 return;
669 }
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000670 ssh_packet_process_incoming(ssh, buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100671 }
Damien Miller1383bd82000-04-06 12:32:37 +1000672}
673
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000674static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000675client_status_confirm(struct ssh *ssh, int type, Channel *c, void *ctx)
Damien Miller0e220db2004-06-15 10:34:08 +1000676{
Damien Miller5771ed72008-05-19 15:35:33 +1000677 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
678 char errmsg[256];
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000679 int r, tochan;
Darren Tuckerfc959702004-07-17 16:12:08 +1000680
Damien Miller555f3b82011-05-15 08:48:05 +1000681 /*
682 * If a TTY was explicitly requested, then a failure to allocate
683 * one is fatal.
684 */
685 if (cr->action == CONFIRM_TTY &&
686 (options.request_tty == REQUEST_TTY_FORCE ||
687 options.request_tty == REQUEST_TTY_YES))
688 cr->action = CONFIRM_CLOSE;
689
djm@openbsd.org001aa552018-04-10 00:10:49 +0000690 /* XXX suppress on mux _client_ quietmode */
Damien Miller5771ed72008-05-19 15:35:33 +1000691 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
Damien Millere1537f92010-01-26 13:26:22 +1100692 c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
Damien Miller0e220db2004-06-15 10:34:08 +1000693
Damien Miller5771ed72008-05-19 15:35:33 +1000694 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
695 debug2("%s request accepted on channel %d",
696 cr->request_type, c->self);
697 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
698 if (tochan) {
699 snprintf(errmsg, sizeof(errmsg),
700 "%s request failed\r\n", cr->request_type);
701 } else {
702 snprintf(errmsg, sizeof(errmsg),
703 "%s request failed on channel %d",
704 cr->request_type, c->self);
705 }
706 /* If error occurred on primary session channel, then exit */
Damien Miller555f3b82011-05-15 08:48:05 +1000707 if (cr->action == CONFIRM_CLOSE && c->self == session_ident)
Damien Miller5771ed72008-05-19 15:35:33 +1000708 fatal("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000709 /*
710 * If error occurred on mux client, append to
711 * their stderr.
712 */
713 if (tochan) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000714 if ((r = sshbuf_put(c->extended, errmsg,
715 strlen(errmsg))) != 0)
716 fatal("%s: buffer error %s", __func__,
717 ssh_err(r));
Damien Miller555f3b82011-05-15 08:48:05 +1000718 } else
Damien Miller5771ed72008-05-19 15:35:33 +1000719 error("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000720 if (cr->action == CONFIRM_TTY) {
721 /*
722 * If a TTY allocation error occurred, then arrange
723 * for the correct TTY to leave raw mode.
724 */
725 if (c->self == session_ident)
726 leave_raw_mode(0);
727 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000728 mux_tty_alloc_failed(ssh, c);
Damien Miller555f3b82011-05-15 08:48:05 +1000729 } else if (cr->action == CONFIRM_CLOSE) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000730 chan_read_failed(ssh, c);
731 chan_write_failed(ssh, c);
Damien Miller5771ed72008-05-19 15:35:33 +1000732 }
Damien Miller0e220db2004-06-15 10:34:08 +1000733 }
Darren Tuckera627d422013-06-02 07:31:17 +1000734 free(cr);
Damien Miller5771ed72008-05-19 15:35:33 +1000735}
Damien Miller0e220db2004-06-15 10:34:08 +1000736
Damien Miller5771ed72008-05-19 15:35:33 +1000737static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000738client_abandon_status_confirm(struct ssh *ssh, Channel *c, void *ctx)
Damien Miller5771ed72008-05-19 15:35:33 +1000739{
Darren Tuckera627d422013-06-02 07:31:17 +1000740 free(ctx);
Damien Miller5771ed72008-05-19 15:35:33 +1000741}
742
Damien Miller6d7b4372011-06-23 08:31:57 +1000743void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000744client_expect_confirm(struct ssh *ssh, int id, const char *request,
Damien Miller555f3b82011-05-15 08:48:05 +1000745 enum confirm_action action)
Damien Miller5771ed72008-05-19 15:35:33 +1000746{
Damien Miller6c81fee2013-11-08 12:19:55 +1100747 struct channel_reply_ctx *cr = xcalloc(1, sizeof(*cr));
Damien Miller5771ed72008-05-19 15:35:33 +1000748
749 cr->request_type = request;
Damien Miller555f3b82011-05-15 08:48:05 +1000750 cr->action = action;
Damien Miller5771ed72008-05-19 15:35:33 +1000751
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000752 channel_register_status_confirm(ssh, id, client_status_confirm,
Damien Miller5771ed72008-05-19 15:35:33 +1000753 client_abandon_status_confirm, cr);
Damien Miller0e220db2004-06-15 10:34:08 +1000754}
755
Darren Tucker9f407c42008-06-13 04:50:27 +1000756void
757client_register_global_confirm(global_confirm_cb *cb, void *ctx)
758{
Damien Millerb9d3bee2008-07-16 22:40:52 +1000759 struct global_confirm *gc, *last_gc;
Darren Tucker9f407c42008-06-13 04:50:27 +1000760
761 /* Coalesce identical callbacks */
Damien Millerb9d3bee2008-07-16 22:40:52 +1000762 last_gc = TAILQ_LAST(&global_confirms, global_confirms);
763 if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
764 if (++last_gc->ref_count >= INT_MAX)
765 fatal("%s: last_gc->ref_count = %d",
766 __func__, last_gc->ref_count);
Darren Tucker9f407c42008-06-13 04:50:27 +1000767 return;
768 }
769
Damien Miller6c81fee2013-11-08 12:19:55 +1100770 gc = xcalloc(1, sizeof(*gc));
Darren Tucker9f407c42008-06-13 04:50:27 +1000771 gc->cb = cb;
772 gc->ctx = ctx;
773 gc->ref_count = 1;
774 TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
775}
776
Damien Miller0e220db2004-06-15 10:34:08 +1000777static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000778process_cmdline(struct ssh *ssh)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000779{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000780 void (*handler)(int);
Damien Miller7acefbb2014-07-18 14:11:24 +1000781 char *s, *cmd;
782 int ok, delete = 0, local = 0, remote = 0, dynamic = 0;
783 struct Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000784
Damien Miller1d2c4562014-02-04 11:18:20 +1100785 memset(&fwd, 0, sizeof(fwd));
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100786
Damien Miller21771e22011-05-15 08:45:50 +1000787 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +0000788 handler = ssh_signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000789 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000790 if (s == NULL)
791 goto out;
Damien Millerfdb23062013-11-21 13:57:15 +1100792 while (isspace((u_char)*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000793 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000794 if (*s == '-')
795 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000796 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000797 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000798
Darren Tucker1973c882004-05-24 10:34:36 +1000799 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000800 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000801 logit(" -L[bind_address:]port:host:hostport "
802 "Request local forward");
803 logit(" -R[bind_address:]port:host:hostport "
804 "Request remote forward");
Damien Miller0164cb82008-11-05 16:30:31 +1100805 logit(" -D[bind_address:]port "
806 "Request dynamic forward");
Damien Millerff773642011-09-22 21:39:48 +1000807 logit(" -KL[bind_address:]port "
808 "Cancel local forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000809 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000810 "Cancel remote forward");
Damien Millerff773642011-09-22 21:39:48 +1000811 logit(" -KD[bind_address:]port "
812 "Cancel dynamic forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100813 if (!options.permit_local_command)
814 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000815 logit(" !args "
816 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100817 goto out;
818 }
819
820 if (*s == '!' && options.permit_local_command) {
821 s++;
822 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000823 goto out;
824 }
825
826 if (*s == 'K') {
827 delete = 1;
828 s++;
829 }
Damien Miller0164cb82008-11-05 16:30:31 +1100830 if (*s == 'L')
831 local = 1;
832 else if (*s == 'R')
833 remote = 1;
834 else if (*s == 'D')
835 dynamic = 1;
836 else {
Damien Miller996acd22003-04-09 20:59:48 +1000837 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000838 goto out;
839 }
Damien Miller0164cb82008-11-05 16:30:31 +1100840
Damien Millerfdb23062013-11-21 13:57:15 +1100841 while (isspace((u_char)*++s))
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100842 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000843
Damien Millere1537f92010-01-26 13:26:22 +1100844 /* XXX update list of forwards in options */
Darren Tuckere7066df2004-05-24 10:18:05 +1000845 if (delete) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000846 /* We pass 1 for dynamicfwd to restrict to 1 or 2 fields. */
847 if (!parse_forward(&fwd, s, 1, 0)) {
848 logit("Bad forwarding close specification.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000849 goto out;
850 }
Damien Millerff773642011-09-22 21:39:48 +1000851 if (remote)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000852 ok = channel_request_rforward_cancel(ssh, &fwd) == 0;
Damien Millerff773642011-09-22 21:39:48 +1000853 else if (dynamic)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000854 ok = channel_cancel_lport_listener(ssh, &fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +1000855 0, &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000856 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000857 ok = channel_cancel_lport_listener(ssh, &fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +1000858 CHANNEL_CANCEL_PORT_STATIC,
859 &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000860 if (!ok) {
dtucker@openbsd.org9390b002017-01-29 21:35:23 +0000861 logit("Unknown port forwarding.");
Damien Millerff773642011-09-22 21:39:48 +1000862 goto out;
863 }
864 logit("Canceled forwarding.");
Darren Tuckere7066df2004-05-24 10:18:05 +1000865 } else {
Damien Miller4bf648f2009-02-14 16:28:21 +1100866 if (!parse_forward(&fwd, s, dynamic, remote)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000867 logit("Bad forwarding specification.");
868 goto out;
869 }
Damien Miller0164cb82008-11-05 16:30:31 +1100870 if (local || dynamic) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000871 if (!channel_setup_local_fwd_listener(ssh, &fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +1000872 &options.fwd_opts)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000873 logit("Port forwarding failed.");
874 goto out;
875 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100876 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000877 if (channel_request_remote_forwarding(ssh, &fwd) < 0) {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000878 logit("Port forwarding failed.");
879 goto out;
880 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100881 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000882 logit("Forwarding port.");
883 }
884
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000885out:
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +0000886 ssh_signal(SIGINT, handler);
Damien Miller21771e22011-05-15 08:45:50 +1000887 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tuckera627d422013-06-02 07:31:17 +1000888 free(cmd);
889 free(fwd.listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000890 free(fwd.listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000891 free(fwd.connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000892 free(fwd.connect_path);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000893}
894
Darren Tucker92a39cf2012-09-07 11:20:20 +1000895/* reasons to suppress output of an escape command in help output */
896#define SUPPRESS_NEVER 0 /* never suppress, always show */
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000897#define SUPPRESS_MUXCLIENT 1 /* don't show in mux client sessions */
898#define SUPPRESS_MUXMASTER 2 /* don't show in mux master sessions */
899#define SUPPRESS_SYSLOG 4 /* don't show when logging to syslog */
Darren Tucker92a39cf2012-09-07 11:20:20 +1000900struct escape_help_text {
901 const char *cmd;
902 const char *text;
903 unsigned int flags;
904};
905static struct escape_help_text esc_txt[] = {
906 {".", "terminate session", SUPPRESS_MUXMASTER},
907 {".", "terminate connection (and any multiplexed sessions)",
908 SUPPRESS_MUXCLIENT},
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000909 {"B", "send a BREAK to the remote system", SUPPRESS_NEVER},
Darren Tucker92a39cf2012-09-07 11:20:20 +1000910 {"C", "open a command line", SUPPRESS_MUXCLIENT},
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000911 {"R", "request rekey", SUPPRESS_NEVER},
Darren Tuckerca0d0fd2012-09-07 11:22:24 +1000912 {"V/v", "decrease/increase verbosity (LogLevel)", SUPPRESS_MUXCLIENT},
Darren Tucker92a39cf2012-09-07 11:20:20 +1000913 {"^Z", "suspend ssh", SUPPRESS_MUXCLIENT},
914 {"#", "list forwarded connections", SUPPRESS_NEVER},
915 {"&", "background ssh (when waiting for connections to terminate)",
916 SUPPRESS_MUXCLIENT},
917 {"?", "this message", SUPPRESS_NEVER},
918};
919
920static void
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000921print_escape_help(struct sshbuf *b, int escape_char, int mux_client,
922 int using_stderr)
Darren Tucker92a39cf2012-09-07 11:20:20 +1000923{
924 unsigned int i, suppress_flags;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000925 int r;
Darren Tucker92a39cf2012-09-07 11:20:20 +1000926
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000927 if ((r = sshbuf_putf(b,
928 "%c?\r\nSupported escape sequences:\r\n", escape_char)) != 0)
929 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker92a39cf2012-09-07 11:20:20 +1000930
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000931 suppress_flags =
Darren Tucker92a39cf2012-09-07 11:20:20 +1000932 (mux_client ? SUPPRESS_MUXCLIENT : 0) |
933 (mux_client ? 0 : SUPPRESS_MUXMASTER) |
934 (using_stderr ? 0 : SUPPRESS_SYSLOG);
935
936 for (i = 0; i < sizeof(esc_txt)/sizeof(esc_txt[0]); i++) {
937 if (esc_txt[i].flags & suppress_flags)
938 continue;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000939 if ((r = sshbuf_putf(b, " %c%-3s - %s\r\n",
940 escape_char, esc_txt[i].cmd, esc_txt[i].text)) != 0)
941 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker92a39cf2012-09-07 11:20:20 +1000942 }
943
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000944 if ((r = sshbuf_putf(b,
Darren Tuckerca0d0fd2012-09-07 11:22:24 +1000945 " %c%c - send the escape character by typing it twice\r\n"
Darren Tucker92a39cf2012-09-07 11:20:20 +1000946 "(Note that escapes are only recognized immediately after "
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000947 "newline.)\r\n", escape_char, escape_char)) != 0)
948 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker92a39cf2012-09-07 11:20:20 +1000949}
950
djm@openbsd.org84623e02018-06-26 02:02:36 +0000951/*
naddy@openbsd.org768405f2017-05-03 21:08:09 +0000952 * Process the characters one by one.
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000953 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000954static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000955process_escapes(struct ssh *ssh, Channel *c,
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000956 struct sshbuf *bin, struct sshbuf *bout, struct sshbuf *berr,
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000957 char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +1000958{
Damien Millerad833b32000-08-23 10:46:23 +1000959 pid_t pid;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000960 int r, bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000961 u_int i;
962 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +1000963 char *s;
djm@openbsd.org05164352017-04-30 23:21:54 +0000964 struct escape_filter_ctx *efc = c->filter_ctx == NULL ?
965 NULL : (struct escape_filter_ctx *)c->filter_ctx;
Damien Millerad833b32000-08-23 10:46:23 +1000966
djm@openbsd.org05164352017-04-30 23:21:54 +0000967 if (c->filter_ctx == NULL)
968 return 0;
djm@openbsd.org84623e02018-06-26 02:02:36 +0000969
Damien Millereccb9de2005-06-17 12:59:34 +1000970 if (len <= 0)
971 return (0);
972
973 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +1000974 /* Get one character at a time. */
975 ch = buf[i];
976
djm@openbsd.org05164352017-04-30 23:21:54 +0000977 if (efc->escape_pending) {
Damien Millerad833b32000-08-23 10:46:23 +1000978 /* We have previously seen an escape character. */
979 /* Clear the flag now. */
djm@openbsd.org05164352017-04-30 23:21:54 +0000980 efc->escape_pending = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000981
982 /* Process the escaped character. */
983 switch (ch) {
984 case '.':
985 /* Terminate the connection. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000986 if ((r = sshbuf_putf(berr, "%c.\r\n",
987 efc->escape_char)) != 0)
988 fatal("%s: buffer error: %s",
989 __func__, ssh_err(r));
Damien Millere1537f92010-01-26 13:26:22 +1100990 if (c && c->ctl_chan != -1) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000991 chan_read_failed(ssh, c);
992 chan_write_failed(ssh, c);
993 if (c->detach_user) {
994 c->detach_user(ssh,
995 c->self, NULL);
996 }
Damien Miller36187092013-06-10 13:07:11 +1000997 c->type = SSH_CHANNEL_ABANDONED;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000998 sshbuf_reset(c->input);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000999 chan_ibuf_empty(ssh, c);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001000 return 0;
1001 } else
1002 quit_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001003 return -1;
1004
1005 case 'Z' - 64:
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001006 /* XXX support this for mux clients */
Damien Millere1537f92010-01-26 13:26:22 +11001007 if (c && c->ctl_chan != -1) {
Darren Tuckerf111d402012-09-07 11:21:42 +10001008 char b[16];
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001009 noescape:
Darren Tuckerf111d402012-09-07 11:21:42 +10001010 if (ch == 'Z' - 64)
1011 snprintf(b, sizeof b, "^Z");
1012 else
1013 snprintf(b, sizeof b, "%c", ch);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001014 if ((r = sshbuf_putf(berr,
Darren Tuckerf111d402012-09-07 11:21:42 +10001015 "%c%s escape not available to "
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001016 "multiplexed sessions\r\n",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001017 efc->escape_char, b)) != 0)
1018 fatal("%s: buffer error: %s",
1019 __func__, ssh_err(r));
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001020 continue;
1021 }
Darren Tucker4d5cd332008-06-13 04:51:14 +10001022 /* Suspend the program. Inform the user */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001023 if ((r = sshbuf_putf(berr,
1024 "%c^Z [suspend ssh]\r\n",
1025 efc->escape_char)) != 0)
1026 fatal("%s: buffer error: %s",
1027 __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +10001028
1029 /* Restore terminal modes and suspend. */
1030 client_suspend_self(bin, bout, berr);
1031
1032 /* We have been continued. */
1033 continue;
1034
Damien Miller54c45982003-05-15 10:20:13 +10001035 case 'B':
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001036 if ((r = sshbuf_putf(berr,
1037 "%cB\r\n", efc->escape_char)) != 0)
1038 fatal("%s: buffer error: %s",
1039 __func__, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001040 channel_request_start(ssh, c->self, "break", 0);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001041 if ((r = sshpkt_put_u32(ssh, 1000)) != 0 ||
1042 (r = sshpkt_send(ssh)) != 0)
djm@openbsd.org9d7b2882019-03-29 11:31:40 +00001043 fatal("%s: send packet: %s", __func__,
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001044 ssh_err(r));
Damien Miller54c45982003-05-15 10:20:13 +10001045 continue;
1046
Ben Lindstromf28f6342001-04-04 02:03:04 +00001047 case 'R':
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001048 if (datafellows & SSH_BUG_NOREKEY)
1049 logit("Server does not "
1050 "support re-keying");
1051 else
1052 need_rekeying = 1;
Ben Lindstromf28f6342001-04-04 02:03:04 +00001053 continue;
1054
Darren Tucker50a48d02012-09-06 21:25:37 +10001055 case 'V':
1056 /* FALLTHROUGH */
1057 case 'v':
1058 if (c && c->ctl_chan != -1)
1059 goto noescape;
1060 if (!log_is_on_stderr()) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001061 if ((r = sshbuf_putf(berr,
Darren Tucker50a48d02012-09-06 21:25:37 +10001062 "%c%c [Logging to syslog]\r\n",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001063 efc->escape_char, ch)) != 0)
1064 fatal("%s: buffer error: %s",
1065 __func__, ssh_err(r));
Darren Tucker50a48d02012-09-06 21:25:37 +10001066 continue;
1067 }
1068 if (ch == 'V' && options.log_level >
1069 SYSLOG_LEVEL_QUIET)
1070 log_change_level(--options.log_level);
1071 if (ch == 'v' && options.log_level <
1072 SYSLOG_LEVEL_DEBUG3)
1073 log_change_level(++options.log_level);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001074 if ((r = sshbuf_putf(berr,
djm@openbsd.org05164352017-04-30 23:21:54 +00001075 "%c%c [LogLevel %s]\r\n",
1076 efc->escape_char, ch,
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001077 log_level_name(options.log_level))) != 0)
1078 fatal("%s: buffer error: %s",
1079 __func__, ssh_err(r));
Darren Tucker50a48d02012-09-06 21:25:37 +10001080 continue;
1081
Damien Millerad833b32000-08-23 10:46:23 +10001082 case '&':
Damien Millere1537f92010-01-26 13:26:22 +11001083 if (c && c->ctl_chan != -1)
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001084 goto noescape;
Damien Millerad833b32000-08-23 10:46:23 +10001085 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001086 * Detach the program (continue to serve
1087 * connections, but put in background and no
1088 * more new connections).
Damien Millerad833b32000-08-23 10:46:23 +10001089 */
Damien Miller96507ef2001-11-12 10:52:25 +11001090 /* Restore tty modes. */
Damien Miller21771e22011-05-15 08:45:50 +10001091 leave_raw_mode(
1092 options.request_tty == REQUEST_TTY_FORCE);
Damien Miller96507ef2001-11-12 10:52:25 +11001093
1094 /* Stop listening for new connections. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001095 channel_stop_listening(ssh);
Damien Miller96507ef2001-11-12 10:52:25 +11001096
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001097 if ((r = sshbuf_putf(berr,
1098 "%c& [backgrounded]\n", efc->escape_char))
1099 != 0)
1100 fatal("%s: buffer error: %s",
1101 __func__, ssh_err(r));
Damien Miller96507ef2001-11-12 10:52:25 +11001102
1103 /* Fork into background. */
1104 pid = fork();
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001105 if (pid == -1) {
Damien Miller96507ef2001-11-12 10:52:25 +11001106 error("fork: %.100s", strerror(errno));
1107 continue;
1108 }
1109 if (pid != 0) { /* This is the parent. */
1110 /* The parent just exits. */
1111 exit(0);
1112 }
1113 /* The child continues serving connections. */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001114 /* fake EOF on stdin */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001115 if ((r = sshbuf_put_u8(bin, 4)) != 0)
1116 fatal("%s: buffer error: %s",
1117 __func__, ssh_err(r));
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001118 return -1;
Damien Millerad833b32000-08-23 10:46:23 +10001119 case '?':
djm@openbsd.org05164352017-04-30 23:21:54 +00001120 print_escape_help(berr, efc->escape_char,
Darren Tucker92a39cf2012-09-07 11:20:20 +10001121 (c && c->ctl_chan != -1),
1122 log_is_on_stderr());
Damien Millerad833b32000-08-23 10:46:23 +10001123 continue;
1124
1125 case '#':
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001126 if ((r = sshbuf_putf(berr, "%c#\r\n",
1127 efc->escape_char)) != 0)
1128 fatal("%s: buffer error: %s",
1129 __func__, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001130 s = channel_open_message(ssh);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001131 if ((r = sshbuf_put(berr, s, strlen(s))) != 0)
1132 fatal("%s: buffer error: %s",
1133 __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +10001134 free(s);
Damien Millerad833b32000-08-23 10:46:23 +10001135 continue;
1136
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001137 case 'C':
Damien Millere1537f92010-01-26 13:26:22 +11001138 if (c && c->ctl_chan != -1)
Damien Miller586b0052008-12-09 14:11:32 +11001139 goto noescape;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001140 process_cmdline(ssh);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001141 continue;
1142
Damien Millerad833b32000-08-23 10:46:23 +10001143 default:
djm@openbsd.org05164352017-04-30 23:21:54 +00001144 if (ch != efc->escape_char) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001145 if ((r = sshbuf_put_u8(bin,
1146 efc->escape_char)) != 0)
1147 fatal("%s: buffer error: %s",
1148 __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +10001149 bytes++;
1150 }
1151 /* Escaped characters fall through here */
1152 break;
1153 }
1154 } else {
1155 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001156 * The previous character was not an escape char.
1157 * Check if this is an escape.
Damien Millerad833b32000-08-23 10:46:23 +10001158 */
djm@openbsd.org05164352017-04-30 23:21:54 +00001159 if (last_was_cr && ch == efc->escape_char) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001160 /*
1161 * It is. Set the flag and continue to
1162 * next character.
1163 */
djm@openbsd.org05164352017-04-30 23:21:54 +00001164 efc->escape_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001165 continue;
1166 }
1167 }
1168
1169 /*
1170 * Normal character. Record whether it was a newline,
1171 * and append it to the buffer.
1172 */
1173 last_was_cr = (ch == '\r' || ch == '\n');
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001174 if ((r = sshbuf_put_u8(bin, ch)) != 0)
1175 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +10001176 bytes++;
1177 }
1178 return bytes;
1179}
1180
Damien Miller5428f641999-11-25 11:54:57 +11001181/*
Damien Millerb38eff82000-04-01 11:09:21 +10001182 * Get packets from the connection input buffer, and process them as long as
1183 * there are packets available.
1184 *
1185 * Any unknown packets received during the actual
1186 * session cause the session to terminate. This is
1187 * intended to make debugging easier since no
1188 * confirmations are sent. Any compatible protocol
1189 * extensions must be negotiated during the
1190 * preparatory phase.
1191 */
1192
Ben Lindstrombba81212001-06-25 05:01:22 +00001193static void
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00001194client_process_buffered_input_packets(struct ssh *ssh)
Damien Millerb38eff82000-04-01 11:09:21 +10001195{
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00001196 ssh_dispatch_run_fatal(ssh, DISPATCH_NONBLOCK, &quit_pending);
Damien Millerb38eff82000-04-01 11:09:21 +10001197}
1198
Damien Millerad833b32000-08-23 10:46:23 +10001199/* scan buf[] for '~' before sending data to the peer */
1200
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001201/* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1202void *
1203client_new_escape_filter_ctx(int escape_char)
1204{
1205 struct escape_filter_ctx *ret;
1206
Damien Miller6c81fee2013-11-08 12:19:55 +11001207 ret = xcalloc(1, sizeof(*ret));
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001208 ret->escape_pending = 0;
1209 ret->escape_char = escape_char;
1210 return (void *)ret;
1211}
1212
Darren Tucker84c56f52008-06-13 04:55:46 +10001213/* Free the escape filter context on channel free */
1214void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001215client_filter_cleanup(struct ssh *ssh, int cid, void *ctx)
Darren Tucker84c56f52008-06-13 04:55:46 +10001216{
Darren Tuckera627d422013-06-02 07:31:17 +10001217 free(ctx);
Darren Tucker84c56f52008-06-13 04:55:46 +10001218}
1219
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001220int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001221client_simple_escape_filter(struct ssh *ssh, Channel *c, char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001222{
Damien Miller5771ed72008-05-19 15:35:33 +10001223 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1224 return 0;
1225
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001226 return process_escapes(ssh, c, c->input, c->output, c->extended,
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001227 buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001228}
1229
Ben Lindstrombba81212001-06-25 05:01:22 +00001230static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001231client_channel_closed(struct ssh *ssh, int id, void *arg)
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001232{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001233 channel_cancel_cleanup(ssh, id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001234 session_closed = 1;
Damien Miller21771e22011-05-15 08:45:50 +10001235 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001236}
1237
Damien Millerb38eff82000-04-01 11:09:21 +10001238/*
Damien Miller5428f641999-11-25 11:54:57 +11001239 * Implements the interactive session with the server. This is called after
1240 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001241 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1242 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001243 */
Damien Miller4af51302000-04-16 11:18:38 +10001244int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001245client_loop(struct ssh *ssh, int have_pty, int escape_char_arg,
1246 int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001247{
Damien Miller5e953212001-01-30 09:14:00 +11001248 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001249 double start_time, total_time;
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001250 int r, max_fd = 0, max_fd2 = 0, len;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001251 u_int64_t ibytes, obytes;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001252 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001253 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001254
Damien Miller95def091999-11-25 00:26:21 +11001255 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001256
semarie@openbsd.orgd7d2bc92015-12-26 07:46:03 +00001257 if (options.control_master &&
djm@openbsd.org368dd972016-07-23 02:54:08 +00001258 !option_clear_or_none(options.control_path)) {
semarie@openbsd.orgd7d2bc92015-12-26 07:46:03 +00001259 debug("pledge: id");
mestre@openbsd.org7adf6c42019-07-24 08:57:00 +00001260 if (pledge("stdio rpath wpath cpath unix inet dns recvfd sendfd proc exec id tty",
semarie@openbsd.orgd7d2bc92015-12-26 07:46:03 +00001261 NULL) == -1)
1262 fatal("%s pledge(): %s", __func__, strerror(errno));
1263
1264 } else if (options.forward_x11 || options.permit_local_command) {
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001265 debug("pledge: exec");
1266 if (pledge("stdio rpath wpath cpath unix inet dns proc exec tty",
1267 NULL) == -1)
1268 fatal("%s pledge(): %s", __func__, strerror(errno));
1269
1270 } else if (options.update_hostkeys) {
1271 debug("pledge: filesystem full");
1272 if (pledge("stdio rpath wpath cpath unix inet dns proc tty",
1273 NULL) == -1)
1274 fatal("%s pledge(): %s", __func__, strerror(errno));
1275
djm@openbsd.org368dd972016-07-23 02:54:08 +00001276 } else if (!option_clear_or_none(options.proxy_command) ||
1277 fork_after_authentication_flag) {
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001278 debug("pledge: proc");
1279 if (pledge("stdio cpath unix inet dns proc tty", NULL) == -1)
1280 fatal("%s pledge(): %s", __func__, strerror(errno));
1281
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001282 } else {
1283 debug("pledge: network");
mestre@openbsd.orge2386452017-06-23 07:24:48 +00001284 if (pledge("stdio unix inet dns proc tty", NULL) == -1)
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001285 fatal("%s pledge(): %s", __func__, strerror(errno));
1286 }
1287
dtucker@openbsd.org@openbsd.org2d638e92017-11-25 05:58:47 +00001288 start_time = monotime_double();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001289
Damien Miller95def091999-11-25 00:26:21 +11001290 /* Initialize variables. */
Damien Miller95def091999-11-25 00:26:21 +11001291 last_was_cr = 1;
1292 exit_status = -1;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001293 connection_in = ssh_packet_get_connection_in(ssh);
1294 connection_out = ssh_packet_get_connection_out(ssh);
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001295 max_fd = MAXIMUM(connection_in, connection_out);
Damien Miller5e953212001-01-30 09:14:00 +11001296
Damien Miller95def091999-11-25 00:26:21 +11001297 quit_pending = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001298
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001299 /* Initialize buffer. */
1300 if ((stderr_buffer = sshbuf_new()) == NULL)
1301 fatal("%s: sshbuf_new failed", __func__);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001302
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00001303 client_init_dispatch(ssh);
Damien Millerb38eff82000-04-01 11:09:21 +10001304
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001305 /*
1306 * Set signal handlers, (e.g. to restore non-blocking mode)
1307 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1308 */
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +00001309 if (ssh_signal(SIGHUP, SIG_IGN) != SIG_IGN)
1310 ssh_signal(SIGHUP, signal_handler);
1311 if (ssh_signal(SIGINT, SIG_IGN) != SIG_IGN)
1312 ssh_signal(SIGINT, signal_handler);
1313 if (ssh_signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1314 ssh_signal(SIGQUIT, signal_handler);
1315 if (ssh_signal(SIGTERM, SIG_IGN) != SIG_IGN)
1316 ssh_signal(SIGTERM, signal_handler);
1317 ssh_signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001318
Damien Miller95def091999-11-25 00:26:21 +11001319 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001320 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001321
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001322 session_ident = ssh2_chan_id;
1323 if (session_ident != -1) {
1324 if (escape_char_arg != SSH_ESCAPECHAR_NONE) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001325 channel_register_filter(ssh, session_ident,
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001326 client_simple_escape_filter, NULL,
1327 client_filter_cleanup,
1328 client_new_escape_filter_ctx(
1329 escape_char_arg));
Damien Miller6c3eec72011-05-05 14:16:22 +10001330 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001331 channel_register_cleanup(ssh, session_ident,
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001332 client_channel_closed, 0);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001333 }
Damien Millerad833b32000-08-23 10:46:23 +10001334
Damien Miller95def091999-11-25 00:26:21 +11001335 /* Main loop of the client for the interactive session mode. */
1336 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001337
Damien Miller5428f641999-11-25 11:54:57 +11001338 /* Process buffered packets sent by the server. */
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00001339 client_process_buffered_input_packets(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001340
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001341 if (session_closed && !channel_still_open(ssh))
Damien Miller1383bd82000-04-06 12:32:37 +10001342 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001343
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001344 if (ssh_packet_is_rekeying(ssh)) {
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001345 debug("rekeying in progress");
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001346 } else if (need_rekeying) {
1347 /* manual rekey request */
1348 debug("need rekeying");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001349 if ((r = kex_start_rekex(ssh)) != 0)
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001350 fatal("%s: kex_start_rekex: %s", __func__,
1351 ssh_err(r));
1352 need_rekeying = 0;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001353 } else {
1354 /*
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001355 * Make packets from buffered channel data, and
1356 * enqueue them for sending to the server.
1357 */
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001358 if (ssh_packet_not_very_much_data_to_write(ssh))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001359 channel_output_poll(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001360
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001361 /*
1362 * Check if the window size has changed, and buffer a
1363 * message about it to the server if so.
1364 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001365 client_check_window_change(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001366
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001367 if (quit_pending)
1368 break;
1369 }
Damien Miller5428f641999-11-25 11:54:57 +11001370 /*
1371 * Wait until we have something to do (something becomes
1372 * available on one of the descriptors).
1373 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001374 max_fd2 = max_fd;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001375 client_wait_until_can_do_something(ssh, &readset, &writeset,
1376 &max_fd2, &nalloc, ssh_packet_is_rekeying(ssh));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001377
Damien Miller95def091999-11-25 00:26:21 +11001378 if (quit_pending)
1379 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001380
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001381 /* Do channel operations unless rekeying in progress. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001382 if (!ssh_packet_is_rekeying(ssh))
1383 channel_after_select(ssh, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001384
Damien Miller1383bd82000-04-06 12:32:37 +10001385 /* Buffer input from the connection. */
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001386 client_process_net_input(ssh, readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001387
Damien Miller1383bd82000-04-06 12:32:37 +10001388 if (quit_pending)
1389 break;
1390
Darren Tucker4d5cd332008-06-13 04:51:14 +10001391 /*
1392 * Send as much buffered packet data as possible to the
1393 * sender.
1394 */
Damien Miller5e953212001-01-30 09:14:00 +11001395 if (FD_ISSET(connection_out, writeset))
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001396 ssh_packet_write_poll(ssh);
Damien Millere11e1ea2010-08-03 16:04:46 +10001397
1398 /*
1399 * If we are a backgrounded control master, and the
1400 * timeout has expired without any active client
1401 * connections, then quit.
1402 */
1403 if (control_persist_exit_time > 0) {
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001404 if (monotime() >= control_persist_exit_time) {
Damien Millere11e1ea2010-08-03 16:04:46 +10001405 debug("ControlPersist timeout expired");
1406 break;
1407 }
1408 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001409 }
Darren Tuckera627d422013-06-02 07:31:17 +10001410 free(readset);
1411 free(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001412
Damien Miller95def091999-11-25 00:26:21 +11001413 /* Terminate the session. */
1414
1415 /* Stop watching for window change. */
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +00001416 ssh_signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001417
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001418 if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
1419 (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_BY_APPLICATION)) != 0 ||
1420 (r = sshpkt_put_cstring(ssh, "disconnected by user")) != 0 ||
1421 (r = sshpkt_put_cstring(ssh, "")) != 0 || /* language tag */
1422 (r = sshpkt_send(ssh)) != 0 ||
1423 (r = ssh_packet_write_wait(ssh)) != 0)
djm@openbsd.org9d7b2882019-03-29 11:31:40 +00001424 fatal("%s: send disconnect: %s", __func__, ssh_err(r));
Darren Tucker12b4a652009-06-21 18:14:48 +10001425
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001426 channel_free_all(ssh);
Damien Miller95def091999-11-25 00:26:21 +11001427
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001428 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001429 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001430
1431 /* restore blocking io */
1432 if (!isatty(fileno(stdin)))
1433 unset_nonblock(fileno(stdin));
1434 if (!isatty(fileno(stdout)))
1435 unset_nonblock(fileno(stdout));
1436 if (!isatty(fileno(stderr)))
1437 unset_nonblock(fileno(stderr));
1438
Damien Millerd6965512003-12-17 16:31:53 +11001439 /*
1440 * If there was no shell or command requested, there will be no remote
1441 * exit status to be returned. In that case, clear error code if the
1442 * connection was deliberately terminated at this end.
1443 */
1444 if (no_shell_flag && received_signal == SIGTERM) {
1445 received_signal = 0;
1446 exit_status = 0;
1447 }
1448
dtucker@openbsd.orgb1e72df2017-07-14 03:18:21 +00001449 if (received_signal) {
1450 verbose("Killed by signal %d.", (int) received_signal);
1451 cleanup_exit(0);
1452 }
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001453
1454 /*
1455 * In interactive mode (with pseudo tty) display a message indicating
1456 * that the connection has been closed.
1457 */
1458 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001459 if ((r = sshbuf_putf(stderr_buffer,
1460 "Connection to %.64s closed.\r\n", host)) != 0)
1461 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001462 }
1463
Damien Miller95def091999-11-25 00:26:21 +11001464 /* Output any buffered data for stderr. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001465 if (sshbuf_len(stderr_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001466 len = atomicio(vwrite, fileno(stderr),
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001467 (u_char *)sshbuf_ptr(stderr_buffer),
1468 sshbuf_len(stderr_buffer));
1469 if (len < 0 || (u_int)len != sshbuf_len(stderr_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001470 error("Write failed flushing stderr buffer.");
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001471 else if ((r = sshbuf_consume(stderr_buffer, len)) != 0)
1472 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11001473 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001474
Damien Miller95def091999-11-25 00:26:21 +11001475 /* Clear and free any buffers. */
jsg@openbsd.org458abc22016-01-23 05:31:35 +00001476 explicit_bzero(buf, sizeof(buf));
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001477 sshbuf_free(stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001478
Damien Miller95def091999-11-25 00:26:21 +11001479 /* Report bytes transferred, and transfer rates. */
dtucker@openbsd.org@openbsd.org2d638e92017-11-25 05:58:47 +00001480 total_time = monotime_double() - start_time;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001481 ssh_packet_get_bytes(ssh, &ibytes, &obytes);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001482 verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
Damien Miller821de0a2011-01-11 17:20:29 +11001483 (unsigned long long)obytes, (unsigned long long)ibytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001484 if (total_time > 0)
Damien Millerb61f3fc2008-07-11 17:36:48 +10001485 verbose("Bytes per second: sent %.1f, received %.1f",
1486 obytes / total_time, ibytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001487 /* Return the exit status of the program. */
1488 debug("Exit status %d", exit_status);
1489 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001490}
Damien Millerb38eff82000-04-01 11:09:21 +10001491
1492/*********/
1493
Ben Lindstrombba81212001-06-25 05:01:22 +00001494static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001495client_request_forwarded_tcpip(struct ssh *ssh, const char *request_type,
1496 int rchan, u_int rwindow, u_int rmaxpack)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001497{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001498 Channel *c = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00001499 struct sshbuf *b = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001500 char *listen_address, *originator_address;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001501 u_int listen_port, originator_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00001502 int r;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001503
1504 /* Get rest of the packet */
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001505 if ((r = sshpkt_get_cstring(ssh, &listen_address, NULL)) != 0 ||
1506 (r = sshpkt_get_u32(ssh, &listen_port)) != 0 ||
1507 (r = sshpkt_get_cstring(ssh, &originator_address, NULL)) != 0 ||
1508 (r = sshpkt_get_u32(ssh, &originator_port)) != 0 ||
1509 (r = sshpkt_get_end(ssh)) != 0)
djm@openbsd.org9d7b2882019-03-29 11:31:40 +00001510 fatal("%s: parse packet: %s", __func__, ssh_err(r));
Damien Miller0bc1bd82000-11-13 22:57:25 +11001511
Damien Miller7acefbb2014-07-18 14:11:24 +10001512 debug("%s: listen %s port %d, originator %s port %d", __func__,
1513 listen_address, listen_port, originator_address, originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001514
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001515 if (listen_port > 0xffff)
1516 error("%s: invalid listen port", __func__);
1517 else if (originator_port > 0xffff)
1518 error("%s: invalid originator port", __func__);
1519 else {
1520 c = channel_connect_by_listen_address(ssh,
1521 listen_address, listen_port, "forwarded-tcpip",
1522 originator_address);
1523 }
Damien Millerbd740252008-05-19 15:37:09 +10001524
markus@openbsd.org8d057842016-09-30 09:19:13 +00001525 if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) {
1526 if ((b = sshbuf_new()) == NULL) {
1527 error("%s: alloc reply", __func__);
1528 goto out;
1529 }
1530 /* reconstruct and send to muxclient */
1531 if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
1532 (r = sshbuf_put_u8(b, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
1533 (r = sshbuf_put_cstring(b, request_type)) != 0 ||
1534 (r = sshbuf_put_u32(b, rchan)) != 0 ||
1535 (r = sshbuf_put_u32(b, rwindow)) != 0 ||
1536 (r = sshbuf_put_u32(b, rmaxpack)) != 0 ||
1537 (r = sshbuf_put_cstring(b, listen_address)) != 0 ||
1538 (r = sshbuf_put_u32(b, listen_port)) != 0 ||
1539 (r = sshbuf_put_cstring(b, originator_address)) != 0 ||
1540 (r = sshbuf_put_u32(b, originator_port)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001541 (r = sshbuf_put_stringb(c->output, b)) != 0) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00001542 error("%s: compose for muxclient %s", __func__,
1543 ssh_err(r));
1544 goto out;
1545 }
1546 }
1547
1548 out:
1549 sshbuf_free(b);
Darren Tuckera627d422013-06-02 07:31:17 +10001550 free(originator_address);
1551 free(listen_address);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001552 return c;
1553}
1554
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001555static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001556client_request_forwarded_streamlocal(struct ssh *ssh,
1557 const char *request_type, int rchan)
Damien Miller7acefbb2014-07-18 14:11:24 +10001558{
1559 Channel *c = NULL;
1560 char *listen_path;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001561 int r;
Damien Miller7acefbb2014-07-18 14:11:24 +10001562
1563 /* Get the remote path. */
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001564 if ((r = sshpkt_get_cstring(ssh, &listen_path, NULL)) != 0 ||
1565 (r = sshpkt_get_string(ssh, NULL, NULL)) != 0 || /* reserved */
1566 (r = sshpkt_get_end(ssh)) != 0)
djm@openbsd.org9d7b2882019-03-29 11:31:40 +00001567 fatal("%s: parse packet: %s", __func__, ssh_err(r));
Damien Miller7acefbb2014-07-18 14:11:24 +10001568
djm@openbsd.org9d7b2882019-03-29 11:31:40 +00001569 debug("%s: request: %s", __func__, listen_path);
Damien Miller7acefbb2014-07-18 14:11:24 +10001570
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001571 c = channel_connect_by_listen_path(ssh, listen_path,
Damien Miller7acefbb2014-07-18 14:11:24 +10001572 "forwarded-streamlocal@openssh.com", "forwarded-streamlocal");
1573 free(listen_path);
1574 return c;
1575}
1576
1577static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001578client_request_x11(struct ssh *ssh, const char *request_type, int rchan)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001579{
1580 Channel *c = NULL;
1581 char *originator;
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00001582 u_int originator_port;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001583 int r, sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001584
1585 if (!options.forward_x11) {
1586 error("Warning: ssh server tried X11 forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001587 error("Warning: this is probably a break-in attempt by a "
1588 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001589 return NULL;
1590 }
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001591 if (x11_refuse_time != 0 && (u_int)monotime() >= x11_refuse_time) {
Damien Miller1ab6a512010-06-26 10:02:24 +10001592 verbose("Rejected X11 connection after ForwardX11Timeout "
1593 "expired");
1594 return NULL;
1595 }
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001596 if ((r = sshpkt_get_cstring(ssh, &originator, NULL)) != 0 ||
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00001597 (r = sshpkt_get_u32(ssh, &originator_port)) != 0 ||
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001598 (r = sshpkt_get_end(ssh)) != 0)
djm@openbsd.org9d7b2882019-03-29 11:31:40 +00001599 fatal("%s: parse packet: %s", __func__, ssh_err(r));
Damien Miller0bc1bd82000-11-13 22:57:25 +11001600 /* XXX check permission */
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00001601 /* XXX range check originator port? */
1602 debug("client_request_x11: request from %s %u", originator,
Damien Millerd83ff352001-01-30 09:19:34 +11001603 originator_port);
Darren Tuckera627d422013-06-02 07:31:17 +10001604 free(originator);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001605 sock = x11_connect_display(ssh);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001606 if (sock < 0)
1607 return NULL;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001608 c = channel_new(ssh, "x11",
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001609 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001610 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001611 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001612 return c;
1613}
1614
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001615static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001616client_request_agent(struct ssh *ssh, const char *request_type, int rchan)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001617{
1618 Channel *c = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001619 int r, sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001620
1621 if (!options.forward_agent) {
1622 error("Warning: ssh server tried agent forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001623 error("Warning: this is probably a break-in attempt by a "
1624 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001625 return NULL;
1626 }
djm@openbsd.org40be78f2019-12-21 02:19:13 +00001627 if (forward_agent_sock_path == NULL) {
1628 r = ssh_get_authentication_socket(&sock);
1629 } else {
1630 r = ssh_get_authentication_socket_path(forward_agent_sock_path, &sock);
1631 }
1632 if (r != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001633 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1634 debug("%s: ssh_get_authentication_socket: %s",
1635 __func__, ssh_err(r));
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001636 return NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001637 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001638 c = channel_new(ssh, "authentication agent connection",
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001639 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001640 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001641 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001642 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001643 return c;
1644}
1645
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001646char *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001647client_request_tun_fwd(struct ssh *ssh, int tun_mode,
1648 int local_tun, int remote_tun)
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001649{
1650 Channel *c;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001651 int r, fd;
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001652 char *ifname = NULL;
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001653
1654 if (tun_mode == SSH_TUNMODE_NO)
1655 return 0;
1656
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001657 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1658
1659 /* Open local tunnel device */
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001660 if ((fd = tun_open(local_tun, tun_mode, &ifname)) == -1) {
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001661 error("Tunnel device open failed.");
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001662 return NULL;
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001663 }
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001664 debug("Tunnel forwarding using interface %s", ifname);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001665
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001666 c = channel_new(ssh, "tun", SSH_CHANNEL_OPENING, fd, fd, -1,
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001667 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1668 c->datagram = 1;
1669
1670#if defined(SSH_TUN_FILTER)
1671 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
Damien Miller871f1e42017-09-12 18:01:35 +10001672 channel_register_filter(ssh, c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +10001673 sys_tun_outfilter, NULL, NULL);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001674#endif
1675
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001676 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
1677 (r = sshpkt_put_cstring(ssh, "tun@openssh.com")) != 0 ||
1678 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
1679 (r = sshpkt_put_u32(ssh, c->local_window_max)) != 0 ||
1680 (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0 ||
1681 (r = sshpkt_put_u32(ssh, tun_mode)) != 0 ||
1682 (r = sshpkt_put_u32(ssh, remote_tun)) != 0 ||
1683 (r = sshpkt_send(ssh)) != 0)
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00001684 sshpkt_fatal(ssh, r, "%s: send reply", __func__);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001685
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001686 return ifname;
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001687}
1688
Damien Millerbd483e72000-04-30 10:00:53 +10001689/* XXXX move to generic input handler */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001690static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001691client_input_channel_open(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerbd483e72000-04-30 10:00:53 +10001692{
1693 Channel *c = NULL;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001694 char *ctype = NULL;
1695 int r;
1696 u_int rchan;
1697 size_t len;
1698 u_int rmaxpack, rwindow;
Damien Millerbd483e72000-04-30 10:00:53 +10001699
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001700 if ((r = sshpkt_get_cstring(ssh, &ctype, &len)) != 0 ||
1701 (r = sshpkt_get_u32(ssh, &rchan)) != 0 ||
1702 (r = sshpkt_get_u32(ssh, &rwindow)) != 0 ||
1703 (r = sshpkt_get_u32(ssh, &rmaxpack)) != 0)
1704 goto out;
Damien Millerbd483e72000-04-30 10:00:53 +10001705
Damien Millere247cc42000-05-07 12:03:14 +10001706 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001707 ctype, rchan, rwindow, rmaxpack);
1708
Damien Miller0bc1bd82000-11-13 22:57:25 +11001709 if (strcmp(ctype, "forwarded-tcpip") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001710 c = client_request_forwarded_tcpip(ssh, ctype, rchan, rwindow,
markus@openbsd.org8d057842016-09-30 09:19:13 +00001711 rmaxpack);
Damien Miller7acefbb2014-07-18 14:11:24 +10001712 } else if (strcmp(ctype, "forwarded-streamlocal@openssh.com") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001713 c = client_request_forwarded_streamlocal(ssh, ctype, rchan);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001714 } else if (strcmp(ctype, "x11") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001715 c = client_request_x11(ssh, ctype, rchan);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001716 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001717 c = client_request_agent(ssh, ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001718 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00001719 if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) {
1720 debug3("proxied to downstream: %s", ctype);
1721 } else if (c != NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10001722 debug("confirm %s", ctype);
1723 c->remote_id = rchan;
djm@openbsd.org9f532292017-09-12 06:35:31 +00001724 c->have_remote_id = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001725 c->remote_window = rwindow;
1726 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001727 if (c->type != SSH_CHANNEL_CONNECTING) {
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001728 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 ||
1729 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1730 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
1731 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
1732 (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0 ||
1733 (r = sshpkt_send(ssh)) != 0)
1734 sshpkt_fatal(ssh, r, "%s: send reply", __func__);
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001735 }
Damien Millerbd483e72000-04-30 10:00:53 +10001736 } else {
1737 debug("failure %s", ctype);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001738 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 ||
1739 (r = sshpkt_put_u32(ssh, rchan)) != 0 ||
1740 (r = sshpkt_put_u32(ssh, SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED)) != 0 ||
1741 (r = sshpkt_put_cstring(ssh, "open failed")) != 0 ||
1742 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
1743 (r = sshpkt_send(ssh)) != 0)
1744 sshpkt_fatal(ssh, r, "%s: send failure", __func__);
Damien Millerbd483e72000-04-30 10:00:53 +10001745 }
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001746 r = 0;
1747 out:
Darren Tuckera627d422013-06-02 07:31:17 +10001748 free(ctype);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001749 return r;
Damien Millerbd483e72000-04-30 10:00:53 +10001750}
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001751
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001752static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001753client_input_channel_req(int type, u_int32_t seq, struct ssh *ssh)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001754{
1755 Channel *c = NULL;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001756 char *rtype = NULL;
1757 u_char reply;
1758 u_int id, exitval;
1759 int r, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001760
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001761 if ((r = sshpkt_get_u32(ssh, &id)) != 0)
1762 return r;
1763 if (id <= INT_MAX)
1764 c = channel_lookup(ssh, id);
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001765 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00001766 return 0;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001767 if ((r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 ||
1768 (r = sshpkt_get_u8(ssh, &reply)) != 0)
1769 goto out;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001770
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001771 debug("client_input_channel_req: channel %u rtype %s reply %d",
Ben Lindstrom5b828322001-02-09 01:34:36 +00001772 id, rtype, reply);
1773
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001774 if (c == NULL) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001775 error("client_input_channel_req: channel %d: "
1776 "unknown channel", id);
Damien Millerbab9bd42008-05-19 16:06:47 +10001777 } else if (strcmp(rtype, "eow@openssh.com") == 0) {
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001778 if ((r = sshpkt_get_end(ssh)) != 0)
1779 goto out;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001780 chan_rcvd_eow(ssh, c);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001781 } else if (strcmp(rtype, "exit-status") == 0) {
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001782 if ((r = sshpkt_get_u32(ssh, &exitval)) != 0)
1783 goto out;
Damien Millere1537f92010-01-26 13:26:22 +11001784 if (c->ctl_chan != -1) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001785 mux_exit_message(ssh, c, exitval);
Damien Miller0e220db2004-06-15 10:34:08 +10001786 success = 1;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001787 } else if ((int)id == session_ident) {
Darren Tucker29440822010-01-08 17:08:35 +11001788 /* Record exit value of local session */
1789 success = 1;
1790 exit_status = exitval;
1791 } else {
Damien Miller36f57eb2010-01-30 17:28:34 +11001792 /* Probably for a mux channel that has already closed */
1793 debug("%s: no sink for exit-status on channel %d",
1794 __func__, id);
Damien Miller0e220db2004-06-15 10:34:08 +10001795 }
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001796 if ((r = sshpkt_get_end(ssh)) != 0)
1797 goto out;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001798 }
Damien Millerc5893782014-05-15 13:48:49 +10001799 if (reply && c != NULL && !(c->flags & CHAN_CLOSE_SENT)) {
djm@openbsd.org9f532292017-09-12 06:35:31 +00001800 if (!c->have_remote_id)
1801 fatal("%s: channel %d: no remote_id",
1802 __func__, c->self);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001803 if ((r = sshpkt_start(ssh, success ?
1804 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE)) != 0 ||
1805 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1806 (r = sshpkt_send(ssh)) != 0)
1807 sshpkt_fatal(ssh, r, "%s: send failure", __func__);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001808 }
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001809 r = 0;
1810 out:
Darren Tuckera627d422013-06-02 07:31:17 +10001811 free(rtype);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001812 return r;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001813}
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001814
djm@openbsd.org523463a2015-02-16 22:13:32 +00001815struct hostkeys_update_ctx {
1816 /* The hostname and (optionally) IP address string for the server */
1817 char *host_str, *ip_str;
1818
1819 /*
1820 * Keys received from the server and a flag for each indicating
1821 * whether they already exist in known_hosts.
1822 * keys_seen is filled in by hostkeys_find() and later (for new
1823 * keys) by client_global_hostkeys_private_confirm().
1824 */
1825 struct sshkey **keys;
1826 int *keys_seen;
djm@openbsd.org84623e02018-06-26 02:02:36 +00001827 size_t nkeys, nnew;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001828
1829 /*
1830 * Keys that are in known_hosts, but were not present in the update
1831 * from the server (i.e. scheduled to be deleted).
1832 * Filled in by hostkeys_find().
1833 */
1834 struct sshkey **old_keys;
1835 size_t nold;
1836};
1837
1838static void
1839hostkeys_update_ctx_free(struct hostkeys_update_ctx *ctx)
1840{
1841 size_t i;
1842
1843 if (ctx == NULL)
1844 return;
1845 for (i = 0; i < ctx->nkeys; i++)
1846 sshkey_free(ctx->keys[i]);
1847 free(ctx->keys);
1848 free(ctx->keys_seen);
1849 for (i = 0; i < ctx->nold; i++)
1850 sshkey_free(ctx->old_keys[i]);
1851 free(ctx->old_keys);
1852 free(ctx->host_str);
1853 free(ctx->ip_str);
1854 free(ctx);
1855}
1856
1857static int
1858hostkeys_find(struct hostkey_foreach_line *l, void *_ctx)
1859{
1860 struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
1861 size_t i;
1862 struct sshkey **tmp;
1863
djm@openbsd.org873d3e72017-04-30 23:18:44 +00001864 if (l->status != HKF_STATUS_MATCHED || l->key == NULL)
djm@openbsd.org523463a2015-02-16 22:13:32 +00001865 return 0;
1866
1867 /* Mark off keys we've already seen for this host */
1868 for (i = 0; i < ctx->nkeys; i++) {
1869 if (sshkey_equal(l->key, ctx->keys[i])) {
1870 debug3("%s: found %s key at %s:%ld", __func__,
1871 sshkey_ssh_name(ctx->keys[i]), l->path, l->linenum);
1872 ctx->keys_seen[i] = 1;
1873 return 0;
1874 }
1875 }
1876 /* This line contained a key that not offered by the server */
1877 debug3("%s: deprecated %s key at %s:%ld", __func__,
1878 sshkey_ssh_name(l->key), l->path, l->linenum);
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001879 if ((tmp = recallocarray(ctx->old_keys, ctx->nold, ctx->nold + 1,
djm@openbsd.org523463a2015-02-16 22:13:32 +00001880 sizeof(*ctx->old_keys))) == NULL)
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001881 fatal("%s: recallocarray failed nold = %zu",
djm@openbsd.org523463a2015-02-16 22:13:32 +00001882 __func__, ctx->nold);
1883 ctx->old_keys = tmp;
1884 ctx->old_keys[ctx->nold++] = l->key;
1885 l->key = NULL;
1886
1887 return 0;
1888}
1889
1890static void
1891update_known_hosts(struct hostkeys_update_ctx *ctx)
1892{
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00001893 int r, was_raw = 0;
dtucker@openbsd.org3a7db912019-04-23 11:56:41 +00001894 LogLevel loglevel = options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK ?
djm@openbsd.org523463a2015-02-16 22:13:32 +00001895 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_VERBOSE;
1896 char *fp, *response;
1897 size_t i;
1898
1899 for (i = 0; i < ctx->nkeys; i++) {
1900 if (ctx->keys_seen[i] != 2)
1901 continue;
1902 if ((fp = sshkey_fingerprint(ctx->keys[i],
1903 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1904 fatal("%s: sshkey_fingerprint failed", __func__);
1905 do_log2(loglevel, "Learned new hostkey: %s %s",
1906 sshkey_type(ctx->keys[i]), fp);
1907 free(fp);
1908 }
1909 for (i = 0; i < ctx->nold; i++) {
1910 if ((fp = sshkey_fingerprint(ctx->old_keys[i],
1911 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1912 fatal("%s: sshkey_fingerprint failed", __func__);
1913 do_log2(loglevel, "Deprecating obsolete hostkey: %s %s",
1914 sshkey_type(ctx->old_keys[i]), fp);
1915 free(fp);
1916 }
1917 if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK) {
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00001918 if (get_saved_tio() != NULL) {
1919 leave_raw_mode(1);
1920 was_raw = 1;
1921 }
djm@openbsd.org523463a2015-02-16 22:13:32 +00001922 response = NULL;
1923 for (i = 0; !quit_pending && i < 3; i++) {
1924 free(response);
1925 response = read_passphrase("Accept updated hostkeys? "
1926 "(yes/no): ", RP_ECHO);
1927 if (strcasecmp(response, "yes") == 0)
1928 break;
1929 else if (quit_pending || response == NULL ||
1930 strcasecmp(response, "no") == 0) {
1931 options.update_hostkeys = 0;
1932 break;
1933 } else {
1934 do_log2(loglevel, "Please enter "
1935 "\"yes\" or \"no\"");
1936 }
1937 }
1938 if (quit_pending || i >= 3 || response == NULL)
1939 options.update_hostkeys = 0;
1940 free(response);
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00001941 if (was_raw)
1942 enter_raw_mode(1);
djm@openbsd.org523463a2015-02-16 22:13:32 +00001943 }
1944
1945 /*
1946 * Now that all the keys are verified, we can go ahead and replace
1947 * them in known_hosts (assuming SSH_UPDATE_HOSTKEYS_ASK didn't
1948 * cancel the operation).
1949 */
1950 if (options.update_hostkeys != 0 &&
1951 (r = hostfile_replace_entries(options.user_hostfiles[0],
1952 ctx->host_str, ctx->ip_str, ctx->keys, ctx->nkeys,
1953 options.hash_known_hosts, 0,
1954 options.fingerprint_hash)) != 0)
1955 error("%s: hostfile_replace_entries failed: %s",
1956 __func__, ssh_err(r));
1957}
1958
1959static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001960client_global_hostkeys_private_confirm(struct ssh *ssh, int type,
1961 u_int32_t seq, void *_ctx)
djm@openbsd.org523463a2015-02-16 22:13:32 +00001962{
djm@openbsd.org523463a2015-02-16 22:13:32 +00001963 struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
1964 size_t i, ndone;
1965 struct sshbuf *signdata;
djm@openbsd.org78607312017-12-18 23:16:23 +00001966 int r, kexsigtype, use_kexsigtype;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001967 const u_char *sig;
1968 size_t siglen;
1969
1970 if (ctx->nnew == 0)
1971 fatal("%s: ctx->nnew == 0", __func__); /* sanity */
1972 if (type != SSH2_MSG_REQUEST_SUCCESS) {
1973 error("Server failed to confirm ownership of "
1974 "private host keys");
1975 hostkeys_update_ctx_free(ctx);
1976 return;
1977 }
djm@openbsd.org78607312017-12-18 23:16:23 +00001978 kexsigtype = sshkey_type_plain(
1979 sshkey_type_from_name(ssh->kex->hostkey_alg));
1980
djm@openbsd.org523463a2015-02-16 22:13:32 +00001981 if ((signdata = sshbuf_new()) == NULL)
1982 fatal("%s: sshbuf_new failed", __func__);
1983 /* Don't want to accidentally accept an unbound signature */
1984 if (ssh->kex->session_id_len == 0)
1985 fatal("%s: ssh->kex->session_id_len == 0", __func__);
1986 /*
1987 * Expect a signature for each of the ctx->nnew private keys we
1988 * haven't seen before. They will be in the same order as the
1989 * ctx->keys where the corresponding ctx->keys_seen[i] == 0.
1990 */
1991 for (ndone = i = 0; i < ctx->nkeys; i++) {
1992 if (ctx->keys_seen[i])
1993 continue;
1994 /* Prepare data to be signed: session ID, unique string, key */
1995 sshbuf_reset(signdata);
djm@openbsd.org44732de2015-02-20 22:17:21 +00001996 if ( (r = sshbuf_put_cstring(signdata,
1997 "hostkeys-prove-00@openssh.com")) != 0 ||
1998 (r = sshbuf_put_string(signdata, ssh->kex->session_id,
djm@openbsd.org523463a2015-02-16 22:13:32 +00001999 ssh->kex->session_id_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +00002000 (r = sshkey_puts(ctx->keys[i], signdata)) != 0)
2001 fatal("%s: failed to prepare signature: %s",
2002 __func__, ssh_err(r));
2003 /* Extract and verify signature */
2004 if ((r = sshpkt_get_string_direct(ssh, &sig, &siglen)) != 0) {
2005 error("%s: couldn't parse message: %s",
2006 __func__, ssh_err(r));
2007 goto out;
2008 }
djm@openbsd.org78607312017-12-18 23:16:23 +00002009 /*
2010 * For RSA keys, prefer to use the signature type negotiated
2011 * during KEX to the default (SHA1).
2012 */
2013 use_kexsigtype = kexsigtype == KEY_RSA &&
2014 sshkey_type_plain(ctx->keys[i]->type) == KEY_RSA;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002015 if ((r = sshkey_verify(ctx->keys[i], sig, siglen,
djm@openbsd.org04c7e282017-12-18 02:25:15 +00002016 sshbuf_ptr(signdata), sshbuf_len(signdata),
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +00002017 use_kexsigtype ? ssh->kex->hostkey_alg : NULL, 0,
2018 NULL)) != 0) {
djm@openbsd.org523463a2015-02-16 22:13:32 +00002019 error("%s: server gave bad signature for %s key %zu",
2020 __func__, sshkey_type(ctx->keys[i]), i);
2021 goto out;
2022 }
2023 /* Key is good. Mark it as 'seen' */
2024 ctx->keys_seen[i] = 2;
2025 ndone++;
2026 }
2027 if (ndone != ctx->nnew)
2028 fatal("%s: ndone != ctx->nnew (%zu / %zu)", __func__,
2029 ndone, ctx->nnew); /* Shouldn't happen */
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002030 if ((r = sshpkt_get_end(ssh)) != 0) {
2031 error("%s: protocol error", __func__);
2032 goto out;
2033 }
djm@openbsd.org523463a2015-02-16 22:13:32 +00002034
2035 /* Make the edits to known_hosts */
2036 update_known_hosts(ctx);
2037 out:
2038 hostkeys_update_ctx_free(ctx);
2039}
2040
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002041/*
djm@openbsd.org894221a2017-03-10 05:01:13 +00002042 * Returns non-zero if the key is accepted by HostkeyAlgorithms.
2043 * Made slightly less trivial by the multiple RSA signature algorithm names.
2044 */
2045static int
2046key_accepted_by_hostkeyalgs(const struct sshkey *key)
2047{
2048 const char *ktype = sshkey_ssh_name(key);
dtucker@openbsd.orgc4b3a122020-01-23 02:46:49 +00002049 const char *hostkeyalgs = options.hostkeyalgorithms;
djm@openbsd.org894221a2017-03-10 05:01:13 +00002050
2051 if (key == NULL || key->type == KEY_UNSPEC)
2052 return 0;
2053 if (key->type == KEY_RSA &&
2054 (match_pattern_list("rsa-sha2-256", hostkeyalgs, 0) == 1 ||
2055 match_pattern_list("rsa-sha2-512", hostkeyalgs, 0) == 1))
2056 return 1;
2057 return match_pattern_list(ktype, hostkeyalgs, 0) == 1;
2058}
2059
2060/*
djm@openbsd.org44732de2015-02-20 22:17:21 +00002061 * Handle hostkeys-00@openssh.com global request to inform the client of all
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002062 * the server's hostkeys. The keys are checked against the user's
2063 * HostkeyAlgorithms preference before they are accepted.
2064 */
2065static int
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00002066client_input_hostkeys(struct ssh *ssh)
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002067{
2068 const u_char *blob = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002069 size_t i, len = 0;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002070 struct sshbuf *buf = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002071 struct sshkey *key = NULL, **tmp;
2072 int r;
2073 char *fp;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002074 static int hostkeys_seen = 0; /* XXX use struct ssh */
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00002075 extern struct sockaddr_storage hostaddr; /* XXX from ssh.c */
djm@openbsd.orga63cfa22015-02-25 19:54:02 +00002076 struct hostkeys_update_ctx *ctx = NULL;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002077
2078 if (hostkeys_seen)
2079 fatal("%s: server already sent hostkeys", __func__);
djm@openbsd.org523463a2015-02-16 22:13:32 +00002080 if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK &&
2081 options.batch_mode)
2082 return 1; /* won't ask in batchmode, so don't even try */
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002083 if (!options.update_hostkeys || options.num_user_hostfiles <= 0)
2084 return 1;
djm@openbsd.orga63cfa22015-02-25 19:54:02 +00002085
2086 ctx = xcalloc(1, sizeof(*ctx));
djm@openbsd.org523463a2015-02-16 22:13:32 +00002087 while (ssh_packet_remaining(ssh) > 0) {
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002088 sshkey_free(key);
2089 key = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002090 if ((r = sshpkt_get_string_direct(ssh, &blob, &len)) != 0) {
2091 error("%s: couldn't parse message: %s",
2092 __func__, ssh_err(r));
2093 goto out;
2094 }
djm@openbsd.org44732de2015-02-20 22:17:21 +00002095 if ((r = sshkey_from_blob(blob, len, &key)) != 0) {
2096 error("%s: parse key: %s", __func__, ssh_err(r));
2097 goto out;
2098 }
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002099 fp = sshkey_fingerprint(key, options.fingerprint_hash,
2100 SSH_FP_DEFAULT);
2101 debug3("%s: received %s key %s", __func__,
2102 sshkey_type(key), fp);
2103 free(fp);
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00002104
djm@openbsd.org894221a2017-03-10 05:01:13 +00002105 if (!key_accepted_by_hostkeyalgs(key)) {
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002106 debug3("%s: %s key not permitted by HostkeyAlgorithms",
2107 __func__, sshkey_ssh_name(key));
2108 continue;
2109 }
djm@openbsd.org523463a2015-02-16 22:13:32 +00002110 /* Skip certs */
2111 if (sshkey_is_cert(key)) {
2112 debug3("%s: %s key is a certificate; skipping",
2113 __func__, sshkey_ssh_name(key));
2114 continue;
2115 }
2116 /* Ensure keys are unique */
2117 for (i = 0; i < ctx->nkeys; i++) {
2118 if (sshkey_equal(key, ctx->keys[i])) {
2119 error("%s: received duplicated %s host key",
2120 __func__, sshkey_ssh_name(key));
2121 goto out;
2122 }
2123 }
2124 /* Key is good, record it */
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00002125 if ((tmp = recallocarray(ctx->keys, ctx->nkeys, ctx->nkeys + 1,
djm@openbsd.org523463a2015-02-16 22:13:32 +00002126 sizeof(*ctx->keys))) == NULL)
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00002127 fatal("%s: recallocarray failed nkeys = %zu",
djm@openbsd.org523463a2015-02-16 22:13:32 +00002128 __func__, ctx->nkeys);
2129 ctx->keys = tmp;
2130 ctx->keys[ctx->nkeys++] = key;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002131 key = NULL;
2132 }
2133
djm@openbsd.org523463a2015-02-16 22:13:32 +00002134 if (ctx->nkeys == 0) {
djm@openbsd.org44732de2015-02-20 22:17:21 +00002135 debug("%s: server sent no hostkeys", __func__);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002136 goto out;
2137 }
djm@openbsd.org44732de2015-02-20 22:17:21 +00002138
djm@openbsd.org523463a2015-02-16 22:13:32 +00002139 if ((ctx->keys_seen = calloc(ctx->nkeys,
2140 sizeof(*ctx->keys_seen))) == NULL)
2141 fatal("%s: calloc failed", __func__);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002142
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00002143 get_hostfile_hostname_ipaddr(host,
2144 options.check_host_ip ? (struct sockaddr *)&hostaddr : NULL,
djm@openbsd.org523463a2015-02-16 22:13:32 +00002145 options.port, &ctx->host_str,
2146 options.check_host_ip ? &ctx->ip_str : NULL);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002147
djm@openbsd.org523463a2015-02-16 22:13:32 +00002148 /* Find which keys we already know about. */
2149 if ((r = hostkeys_foreach(options.user_hostfiles[0], hostkeys_find,
2150 ctx, ctx->host_str, ctx->ip_str,
2151 HKF_WANT_PARSE_KEY|HKF_WANT_MATCH)) != 0) {
2152 error("%s: hostkeys_foreach failed: %s", __func__, ssh_err(r));
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002153 goto out;
2154 }
2155
djm@openbsd.org523463a2015-02-16 22:13:32 +00002156 /* Figure out if we have any new keys to add */
2157 ctx->nnew = 0;
2158 for (i = 0; i < ctx->nkeys; i++) {
2159 if (!ctx->keys_seen[i])
2160 ctx->nnew++;
2161 }
2162
2163 debug3("%s: %zu keys from server: %zu new, %zu retained. %zu to remove",
2164 __func__, ctx->nkeys, ctx->nnew, ctx->nkeys - ctx->nnew, ctx->nold);
2165
2166 if (ctx->nnew == 0 && ctx->nold != 0) {
2167 /* We have some keys to remove. Just do it. */
2168 update_known_hosts(ctx);
2169 } else if (ctx->nnew != 0) {
2170 /*
2171 * We have received hitherto-unseen keys from the server.
2172 * Ask the server to confirm ownership of the private halves.
2173 */
2174 debug3("%s: asking server to prove ownership for %zu keys",
2175 __func__, ctx->nnew);
2176 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
2177 (r = sshpkt_put_cstring(ssh,
djm@openbsd.org44732de2015-02-20 22:17:21 +00002178 "hostkeys-prove-00@openssh.com")) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +00002179 (r = sshpkt_put_u8(ssh, 1)) != 0) /* bool: want reply */
2180 fatal("%s: cannot prepare packet: %s",
2181 __func__, ssh_err(r));
2182 if ((buf = sshbuf_new()) == NULL)
2183 fatal("%s: sshbuf_new", __func__);
2184 for (i = 0; i < ctx->nkeys; i++) {
2185 if (ctx->keys_seen[i])
2186 continue;
2187 sshbuf_reset(buf);
2188 if ((r = sshkey_putb(ctx->keys[i], buf)) != 0)
2189 fatal("%s: sshkey_putb: %s",
2190 __func__, ssh_err(r));
2191 if ((r = sshpkt_put_stringb(ssh, buf)) != 0)
2192 fatal("%s: sshpkt_put_string: %s",
2193 __func__, ssh_err(r));
2194 }
2195 if ((r = sshpkt_send(ssh)) != 0)
2196 fatal("%s: sshpkt_send: %s", __func__, ssh_err(r));
2197 client_register_global_confirm(
2198 client_global_hostkeys_private_confirm, ctx);
2199 ctx = NULL; /* will be freed in callback */
2200 }
2201
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002202 /* Success */
2203 out:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002204 hostkeys_update_ctx_free(ctx);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002205 sshkey_free(key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002206 sshbuf_free(buf);
djm@openbsd.org523463a2015-02-16 22:13:32 +00002207 /*
2208 * NB. Return success for all cases. The server doesn't need to know
2209 * what the client does with its hosts file.
2210 */
2211 return 1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002212}
2213
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002214static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002215client_input_global_request(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerc3fa4072002-01-22 23:21:58 +11002216{
2217 char *rtype;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002218 u_char want_reply;
2219 int r, success = 0;
Damien Millerc3fa4072002-01-22 23:21:58 +11002220
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002221 if ((r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 ||
2222 (r = sshpkt_get_u8(ssh, &want_reply)) != 0)
2223 goto out;
Damien Miller509b0102003-12-17 16:33:10 +11002224 debug("client_input_global_request: rtype %s want_reply %d",
2225 rtype, want_reply);
djm@openbsd.org44732de2015-02-20 22:17:21 +00002226 if (strcmp(rtype, "hostkeys-00@openssh.com") == 0)
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00002227 success = client_input_hostkeys(ssh);
Damien Millerc3fa4072002-01-22 23:21:58 +11002228 if (want_reply) {
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002229 if ((r = sshpkt_start(ssh, success ? SSH2_MSG_REQUEST_SUCCESS :
2230 SSH2_MSG_REQUEST_FAILURE)) != 0 ||
2231 (r = sshpkt_send(ssh)) != 0 ||
2232 (r = ssh_packet_write_wait(ssh)) != 0)
2233 goto out;
Damien Millerc3fa4072002-01-22 23:21:58 +11002234 }
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002235 r = 0;
2236 out:
Darren Tuckera627d422013-06-02 07:31:17 +10002237 free(rtype);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002238 return r;
Damien Millerc3fa4072002-01-22 23:21:58 +11002239}
Damien Millerbd483e72000-04-30 10:00:53 +10002240
Damien Miller0e220db2004-06-15 10:34:08 +10002241void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002242client_session2_setup(struct ssh *ssh, int id, int want_tty, int want_subsystem,
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002243 const char *term, struct termios *tiop, int in_fd, struct sshbuf *cmd,
2244 char **env)
Damien Miller0e220db2004-06-15 10:34:08 +10002245{
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002246 int i, j, matched, len, r;
djm@openbsd.org7082bb52018-06-09 03:01:12 +00002247 char *name, *val;
Darren Tucker5d78de62004-11-05 20:35:44 +11002248 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002249
2250 debug2("%s: id %d", __func__, id);
2251
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002252 if ((c = channel_lookup(ssh, id)) == NULL)
2253 fatal("%s: channel %d: unknown channel", __func__, id);
Darren Tucker5d78de62004-11-05 20:35:44 +11002254
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002255 ssh_packet_set_interactive(ssh, want_tty,
Damien Miller0dac6fb2010-11-20 15:19:38 +11002256 options.ip_qos_interactive, options.ip_qos_bulk);
2257
Damien Miller0e220db2004-06-15 10:34:08 +10002258 if (want_tty) {
2259 struct winsize ws;
Damien Miller0e220db2004-06-15 10:34:08 +10002260
2261 /* Store window size in the packet. */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00002262 if (ioctl(in_fd, TIOCGWINSZ, &ws) == -1)
Damien Miller0e220db2004-06-15 10:34:08 +10002263 memset(&ws, 0, sizeof(ws));
2264
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002265 channel_request_start(ssh, id, "pty-req", 1);
2266 client_expect_confirm(ssh, id, "PTY allocation", CONFIRM_TTY);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002267 if ((r = sshpkt_put_cstring(ssh, term != NULL ? term : ""))
2268 != 0 ||
2269 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_col)) != 0 ||
2270 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_row)) != 0 ||
2271 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_xpixel)) != 0 ||
2272 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_ypixel)) != 0)
djm@openbsd.org9d7b2882019-03-29 11:31:40 +00002273 fatal("%s: build packet: %s", __func__, ssh_err(r));
Darren Tuckerdf189fb2008-06-08 12:55:32 +10002274 if (tiop == NULL)
2275 tiop = get_saved_tio();
markus@openbsd.org89dd6152018-07-09 21:20:26 +00002276 ssh_tty_make_modes(ssh, -1, tiop);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002277 if ((r = sshpkt_send(ssh)) != 0)
djm@openbsd.org9d7b2882019-03-29 11:31:40 +00002278 fatal("%s: send packet: %s", __func__, ssh_err(r));
Damien Miller0e220db2004-06-15 10:34:08 +10002279 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11002280 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10002281 }
2282
2283 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10002284 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10002285 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10002286 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10002287 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10002288 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10002289 if ((val = strchr(name, '=')) == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +10002290 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002291 continue;
2292 }
2293 *val++ = '\0';
2294
2295 matched = 0;
2296 for (j = 0; j < options.num_send_env; j++) {
2297 if (match_pattern(name, options.send_env[j])) {
2298 matched = 1;
2299 break;
2300 }
2301 }
2302 if (!matched) {
2303 debug3("Ignored env %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10002304 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002305 continue;
2306 }
2307
2308 debug("Sending env %s = %s", name, val);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002309 channel_request_start(ssh, id, "env", 0);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002310 if ((r = sshpkt_put_cstring(ssh, name)) != 0 ||
2311 (r = sshpkt_put_cstring(ssh, val)) != 0 ||
djm@openbsd.org9d7b2882019-03-29 11:31:40 +00002312 (r = sshpkt_send(ssh)) != 0) {
2313 fatal("%s: send packet: %s",
2314 __func__, ssh_err(r));
2315 }
Darren Tuckera627d422013-06-02 07:31:17 +10002316 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002317 }
2318 }
djm@openbsd.org7082bb52018-06-09 03:01:12 +00002319 for (i = 0; i < options.num_setenv; i++) {
2320 /* Split */
2321 name = xstrdup(options.setenv[i]);
2322 if ((val = strchr(name, '=')) == NULL) {
2323 free(name);
2324 continue;
2325 }
2326 *val++ = '\0';
2327
2328 debug("Setting env %s = %s", name, val);
2329 channel_request_start(ssh, id, "env", 0);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002330 if ((r = sshpkt_put_cstring(ssh, name)) != 0 ||
2331 (r = sshpkt_put_cstring(ssh, val)) != 0 ||
2332 (r = sshpkt_send(ssh)) != 0)
djm@openbsd.org9d7b2882019-03-29 11:31:40 +00002333 fatal("%s: send packet: %s", __func__, ssh_err(r));
djm@openbsd.org7082bb52018-06-09 03:01:12 +00002334 free(name);
2335 }
Damien Miller0e220db2004-06-15 10:34:08 +10002336
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002337 len = sshbuf_len(cmd);
Damien Miller0e220db2004-06-15 10:34:08 +10002338 if (len > 0) {
2339 if (len > 900)
2340 len = 900;
2341 if (want_subsystem) {
Damien Miller5771ed72008-05-19 15:35:33 +10002342 debug("Sending subsystem: %.*s",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002343 len, (const u_char*)sshbuf_ptr(cmd));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002344 channel_request_start(ssh, id, "subsystem", 1);
2345 client_expect_confirm(ssh, id, "subsystem",
2346 CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002347 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002348 debug("Sending command: %.*s",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002349 len, (const u_char*)sshbuf_ptr(cmd));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002350 channel_request_start(ssh, id, "exec", 1);
2351 client_expect_confirm(ssh, id, "exec", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002352 }
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002353 if ((r = sshpkt_put_stringb(ssh, cmd)) != 0 ||
2354 (r = sshpkt_send(ssh)) != 0)
djm@openbsd.org9d7b2882019-03-29 11:31:40 +00002355 fatal("%s: send command: %s", __func__, ssh_err(r));
Damien Miller0e220db2004-06-15 10:34:08 +10002356 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002357 channel_request_start(ssh, id, "shell", 1);
2358 client_expect_confirm(ssh, id, "shell", CONFIRM_CLOSE);
djm@openbsd.org9d7b2882019-03-29 11:31:40 +00002359 if ((r = sshpkt_send(ssh)) != 0) {
2360 fatal("%s: send shell request: %s",
2361 __func__, ssh_err(r));
2362 }
Damien Miller0e220db2004-06-15 10:34:08 +10002363 }
2364}
2365
Ben Lindstrombba81212001-06-25 05:01:22 +00002366static void
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00002367client_init_dispatch(struct ssh *ssh)
Damien Miller1383bd82000-04-06 12:32:37 +10002368{
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00002369 ssh_dispatch_init(ssh, &dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10002370
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00002371 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2372 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2373 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2374 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
2375 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
2376 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2377 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
2378 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
2379 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
2380 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2381 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
2382 ssh_dispatch_set(ssh, SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002383
2384 /* rekeying */
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00002385 ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10002386
2387 /* global request reply messages */
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00002388 ssh_dispatch_set(ssh, SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2389 ssh_dispatch_set(ssh, SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10002390}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002391
Damien Miller6c3eec72011-05-05 14:16:22 +10002392void
2393client_stop_mux(void)
2394{
2395 if (options.control_path != NULL && muxserver_sock != -1)
2396 unlink(options.control_path);
2397 /*
Darren Tucker48bf4b02012-09-07 16:38:53 +10002398 * If we are in persist mode, or don't have a shell, signal that we
2399 * should close when all active channels are closed.
Damien Miller6c3eec72011-05-05 14:16:22 +10002400 */
Darren Tucker48bf4b02012-09-07 16:38:53 +10002401 if (options.control_persist || no_shell_flag) {
Damien Miller6c3eec72011-05-05 14:16:22 +10002402 session_closed = 1;
Damien Miller4ac99c32011-06-20 14:43:31 +10002403 setproctitle("[stopped mux]");
2404 }
Damien Miller6c3eec72011-05-05 14:16:22 +10002405}
2406
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002407/* client specific fatal cleanup */
2408void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002409cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002410{
Damien Miller21771e22011-05-15 08:45:50 +10002411 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002412 leave_non_blocking();
Damien Millerb1cbfa22008-05-19 16:00:08 +10002413 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10002414 unlink(options.control_path);
Damien Millera41ccca2010-10-07 22:07:32 +11002415 ssh_kill_proxy_command();
Darren Tucker3e33cec2003-10-02 16:12:36 +10002416 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002417}