blob: 521467bd244b5778785536c18b68603a811b3a5f [file] [log] [blame]
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00001/* $OpenBSD: clientloop.c,v 1.321 2019/01/19 21:39:12 djm 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 Millerded319c2006-09-01 15:38:36 +100080#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100081#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100082#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100083#include <string.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
132 * command line, or the HostName specified for the user-supplied name in a
133 * configuration file.
134 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135extern char *host;
136
Damien Miller5428f641999-11-25 11:54:57 +1100137/*
138 * Flag to indicate that we have received a window change signal which has
139 * not yet been processed. This will cause a message indicating the new
140 * window size to be sent to the server a little later. This is volatile
141 * because this is updated in a signal handler.
142 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000143static volatile sig_atomic_t received_window_change_signal = 0;
144static volatile sig_atomic_t received_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145
Damien Miller788f2122005-11-05 15:14:59 +1100146/* Flag indicating whether the user's terminal is in non-blocking mode. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000147static int in_non_blocking_mode = 0;
148
Damien Millere11e1ea2010-08-03 16:04:46 +1000149/* Time when backgrounded control master using ControlPersist should exit */
150static time_t control_persist_exit_time = 0;
151
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152/* Common data for the client loop code. */
Damien Millere1537f92010-01-26 13:26:22 +1100153volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
Damien Miller95def091999-11-25 00:26:21 +1100154static int last_was_cr; /* Last character was a newline. */
Darren Tucker4d5cd332008-06-13 04:51:14 +1000155static int exit_status; /* Used to store the command exit status. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000156static struct sshbuf *stderr_buffer; /* Used for final exit message. */
Damien Miller95def091999-11-25 00:26:21 +1100157static int connection_in; /* Connection to server (input). */
158static int connection_out; /* Connection to server (output). */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000159static int need_rekeying; /* Set to non-zero if rekeying is requested. */
Damien Miller1ab6a512010-06-26 10:02:24 +1000160static int session_closed; /* In SSH2: login session closed. */
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000161static u_int x11_refuse_time; /* If >0, refuse x11 opens after this time. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +0000163static void client_init_dispatch(struct ssh *ssh);
Damien Miller1383bd82000-04-06 12:32:37 +1000164int session_ident = -1;
165
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000166/* Track escape per proto2 channel */
167struct escape_filter_ctx {
168 int escape_pending;
169 int escape_char;
170};
171
172/* Context for channel confirmation replies */
Damien Miller5771ed72008-05-19 15:35:33 +1000173struct channel_reply_ctx {
174 const char *request_type;
Damien Miller555f3b82011-05-15 08:48:05 +1000175 int id;
176 enum confirm_action action;
Damien Miller5771ed72008-05-19 15:35:33 +1000177};
178
Darren Tucker9f407c42008-06-13 04:50:27 +1000179/* Global request success/failure callbacks */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000180/* XXX move to struct ssh? */
Darren Tucker9f407c42008-06-13 04:50:27 +1000181struct global_confirm {
182 TAILQ_ENTRY(global_confirm) entry;
183 global_confirm_cb *cb;
184 void *ctx;
185 int ref_count;
186};
187TAILQ_HEAD(global_confirms, global_confirm);
188static struct global_confirms global_confirms =
189 TAILQ_HEAD_INITIALIZER(global_confirms);
190
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000191void ssh_process_session2_setup(int, int, int, struct sshbuf *);
Damien Miller0e220db2004-06-15 10:34:08 +1000192
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193/* Restores stdin to blocking mode. */
194
Ben Lindstrombba81212001-06-25 05:01:22 +0000195static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000196leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197{
Damien Miller95def091999-11-25 00:26:21 +1100198 if (in_non_blocking_mode) {
Damien Miller03e66f62004-06-15 15:47:51 +1000199 unset_nonblock(fileno(stdin));
Damien Miller95def091999-11-25 00:26:21 +1100200 in_non_blocking_mode = 0;
Damien Miller95def091999-11-25 00:26:21 +1100201 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202}
203
Damien Miller5428f641999-11-25 11:54:57 +1100204/*
205 * Signal handler for the window change signal (SIGWINCH). This just sets a
206 * flag indicating that the window has changed.
207 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100208/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000209static void
Damien Miller95def091999-11-25 00:26:21 +1100210window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000211{
Damien Miller95def091999-11-25 00:26:21 +1100212 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000213}
214
Damien Miller5428f641999-11-25 11:54:57 +1100215/*
216 * Signal handler for signals that cause the program to terminate. These
217 * signals must be trapped to restore terminal modes.
218 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100219/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000220static void
Damien Miller95def091999-11-25 00:26:21 +1100221signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000222{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000223 received_signal = sig;
224 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225}
226
Damien Miller5428f641999-11-25 11:54:57 +1100227/*
Damien Millere11e1ea2010-08-03 16:04:46 +1000228 * Sets control_persist_exit_time to the absolute time when the
229 * backgrounded control master should exit due to expiry of the
230 * ControlPersist timeout. Sets it to 0 if we are not a backgrounded
231 * control master process, or if there is no ControlPersist timeout.
232 */
233static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000234set_control_persist_exit_time(struct ssh *ssh)
Damien Millere11e1ea2010-08-03 16:04:46 +1000235{
236 if (muxserver_sock == -1 || !options.control_persist
Damien Miller6c3eec72011-05-05 14:16:22 +1000237 || options.control_persist_timeout == 0) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000238 /* not using a ControlPersist timeout */
239 control_persist_exit_time = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000240 } else if (channel_still_open(ssh)) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000241 /* some client connections are still open */
242 if (control_persist_exit_time > 0)
243 debug2("%s: cancel scheduled exit", __func__);
244 control_persist_exit_time = 0;
245 } else if (control_persist_exit_time <= 0) {
246 /* a client connection has recently closed */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000247 control_persist_exit_time = monotime() +
Damien Millere11e1ea2010-08-03 16:04:46 +1000248 (time_t)options.control_persist_timeout;
249 debug2("%s: schedule exit in %d seconds", __func__,
250 options.control_persist_timeout);
251 }
252 /* else we are already counting down to the timeout */
253}
254
Damien Miller8d60be52012-02-11 08:18:17 +1100255#define SSH_X11_VALID_DISPLAY_CHARS ":/.-_"
256static int
257client_x11_display_valid(const char *display)
258{
259 size_t i, dlen;
260
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000261 if (display == NULL)
262 return 0;
263
Damien Miller8d60be52012-02-11 08:18:17 +1100264 dlen = strlen(display);
265 for (i = 0; i < dlen; i++) {
Damien Millerfdb23062013-11-21 13:57:15 +1100266 if (!isalnum((u_char)display[i]) &&
Damien Miller8d60be52012-02-11 08:18:17 +1100267 strchr(SSH_X11_VALID_DISPLAY_CHARS, display[i]) == NULL) {
268 debug("Invalid character '%c' in DISPLAY", display[i]);
269 return 0;
270 }
271 }
272 return 1;
273}
274
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000275#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
276#define X11_TIMEOUT_SLACK 60
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000277int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000278client_x11_get_proto(struct ssh *ssh, const char *display,
279 const char *xauth_path, u_int trusted, u_int timeout,
280 char **_proto, char **_data)
Damien Miller17e7ed02005-06-17 12:54:33 +1000281{
djm@openbsd.orgb5e412a2018-09-21 12:46:22 +0000282 char *cmd, line[512], xdisplay[512];
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000283 char xauthfile[PATH_MAX], xauthdir[PATH_MAX];
Damien Miller17e7ed02005-06-17 12:54:33 +1000284 static char proto[512], data[512];
285 FILE *f;
tedu@openbsd.org10363562016-09-17 18:00:27 +0000286 int got_data = 0, generated = 0, do_unlink = 0, r;
Damien Miller17e7ed02005-06-17 12:54:33 +1000287 struct stat st;
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000288 u_int now, x11_timeout_real;
Damien Miller17e7ed02005-06-17 12:54:33 +1000289
Damien Miller17e7ed02005-06-17 12:54:33 +1000290 *_proto = proto;
291 *_data = data;
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000292 proto[0] = data[0] = xauthfile[0] = xauthdir[0] = '\0';
Damien Miller17e7ed02005-06-17 12:54:33 +1000293
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000294 if (!client_x11_display_valid(display)) {
millert@openbsd.org5658ef22016-02-01 21:18:17 +0000295 if (display != NULL)
296 logit("DISPLAY \"%s\" invalid; disabling X11 forwarding",
297 display);
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000298 return -1;
299 }
300 if (xauth_path != NULL && stat(xauth_path, &st) == -1) {
301 debug("No xauth program.");
302 xauth_path = NULL;
303 }
304
305 if (xauth_path != NULL) {
Damien Miller17e7ed02005-06-17 12:54:33 +1000306 /*
307 * Handle FamilyLocal case where $DISPLAY does
308 * not match an authorization entry. For this we
309 * just try "xauth list unix:displaynum.screennum".
310 * XXX: "localhost" match to determine FamilyLocal
311 * is not perfect.
312 */
313 if (strncmp(display, "localhost:", 10) == 0) {
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000314 if ((r = snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
315 display + 10)) < 0 ||
316 (size_t)r >= sizeof(xdisplay)) {
317 error("%s: display name too long", __func__);
318 return -1;
319 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000320 display = xdisplay;
321 }
322 if (trusted == 0) {
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000323 /*
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000324 * Generate an untrusted X11 auth cookie.
325 *
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000326 * The authentication cookie should briefly outlive
327 * ssh's willingness to forward X11 connections to
328 * avoid nasty fail-open behaviour in the X server.
329 */
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000330 mktemp_proto(xauthdir, sizeof(xauthdir));
331 if (mkdtemp(xauthdir) == NULL) {
332 error("%s: mkdtemp: %s",
333 __func__, strerror(errno));
334 return -1;
335 }
336 do_unlink = 1;
337 if ((r = snprintf(xauthfile, sizeof(xauthfile),
338 "%s/xauthfile", xauthdir)) < 0 ||
339 (size_t)r >= sizeof(xauthfile)) {
340 error("%s: xauthfile path too long", __func__);
341 unlink(xauthfile);
342 rmdir(xauthdir);
343 return -1;
344 }
345
djm@openbsd.orgb5e412a2018-09-21 12:46:22 +0000346 if (timeout == 0) {
347 /* auth doesn't time out */
348 xasprintf(&cmd, "%s -f %s generate %s %s "
349 "untrusted 2>%s",
350 xauth_path, xauthfile, display,
351 SSH_X11_PROTO, _PATH_DEVNULL);
352 } else {
353 /* Add some slack to requested expiry */
354 if (timeout < UINT_MAX - X11_TIMEOUT_SLACK)
355 x11_timeout_real = timeout +
356 X11_TIMEOUT_SLACK;
357 else {
358 /* Don't overflow on long timeouts */
359 x11_timeout_real = UINT_MAX;
360 }
361 xasprintf(&cmd, "%s -f %s generate %s %s "
362 "untrusted timeout %u 2>%s",
363 xauth_path, xauthfile, display,
364 SSH_X11_PROTO, x11_timeout_real,
365 _PATH_DEVNULL);
366 }
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000367 debug2("%s: %s", __func__, cmd);
djm@openbsd.orgb5e412a2018-09-21 12:46:22 +0000368
369 if (timeout != 0 && x11_refuse_time == 0) {
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000370 now = monotime() + 1;
371 if (UINT_MAX - timeout < now)
372 x11_refuse_time = UINT_MAX;
373 else
374 x11_refuse_time = now + timeout;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000375 channel_set_x11_refuse_time(ssh,
376 x11_refuse_time);
Damien Miller17e7ed02005-06-17 12:54:33 +1000377 }
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000378 if (system(cmd) == 0)
379 generated = 1;
djm@openbsd.orgb5e412a2018-09-21 12:46:22 +0000380 free(cmd);
Damien Miller17e7ed02005-06-17 12:54:33 +1000381 }
Darren Tucker513d13a2007-08-15 19:13:41 +1000382
383 /*
384 * When in untrusted mode, we read the cookie only if it was
385 * successfully generated as an untrusted one in the step
386 * above.
387 */
388 if (trusted || generated) {
djm@openbsd.orgb5e412a2018-09-21 12:46:22 +0000389 xasprintf(&cmd,
Darren Tucker513d13a2007-08-15 19:13:41 +1000390 "%s %s%s list %s 2>" _PATH_DEVNULL,
391 xauth_path,
392 generated ? "-f " : "" ,
393 generated ? xauthfile : "",
394 display);
395 debug2("x11_get_proto: %s", cmd);
396 f = popen(cmd, "r");
397 if (f && fgets(line, sizeof(line), f) &&
398 sscanf(line, "%*s %511s %511s", proto, data) == 2)
399 got_data = 1;
400 if (f)
401 pclose(f);
djm@openbsd.orgb5e412a2018-09-21 12:46:22 +0000402 free(cmd);
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000403 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000404 }
405
406 if (do_unlink) {
407 unlink(xauthfile);
408 rmdir(xauthdir);
409 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000410
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000411 /* Don't fall back to fake X11 data for untrusted forwarding */
412 if (!trusted && !got_data) {
413 error("Warning: untrusted X11 forwarding setup failed: "
414 "xauth key data not generated");
415 return -1;
416 }
417
Damien Miller17e7ed02005-06-17 12:54:33 +1000418 /*
419 * If we didn't get authentication data, just make up some
420 * data. The forwarding code will check the validity of the
421 * response anyway, and substitute this data. The X11
422 * server, however, will ignore this fake data and use
423 * whatever authentication mechanisms it was using otherwise
424 * for the local connection.
425 */
426 if (!got_data) {
tedu@openbsd.org10363562016-09-17 18:00:27 +0000427 u_int8_t rnd[16];
428 u_int i;
Damien Miller17e7ed02005-06-17 12:54:33 +1000429
430 logit("Warning: No xauth data; "
431 "using fake authentication data for X11 forwarding.");
432 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
tedu@openbsd.org10363562016-09-17 18:00:27 +0000433 arc4random_buf(rnd, sizeof(rnd));
434 for (i = 0; i < sizeof(rnd); i++) {
Damien Miller17e7ed02005-06-17 12:54:33 +1000435 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
tedu@openbsd.org10363562016-09-17 18:00:27 +0000436 rnd[i]);
Damien Miller17e7ed02005-06-17 12:54:33 +1000437 }
438 }
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000439
440 return 0;
Damien Miller17e7ed02005-06-17 12:54:33 +1000441}
442
Damien Miller5428f641999-11-25 11:54:57 +1100443/*
Damien Miller5428f641999-11-25 11:54:57 +1100444 * Checks if the client window has changed, and sends a packet about it to
445 * the server if so. The actual change is detected elsewhere (by a software
446 * interrupt on Unix); this just checks the flag and sends a message if
447 * appropriate.
448 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000449
Ben Lindstrombba81212001-06-25 05:01:22 +0000450static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000451client_check_window_change(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000452{
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000453 if (!received_window_change_signal)
Damien Miller1383bd82000-04-06 12:32:37 +1000454 return;
455 /** XXX race */
456 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000458 debug2("%s: changed", __func__);
Damien Miller1383bd82000-04-06 12:32:37 +1000459
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000460 channel_send_window_changes(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461}
462
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000463static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000464client_global_request_reply(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller509b0102003-12-17 16:33:10 +1100465{
Darren Tucker9f407c42008-06-13 04:50:27 +1000466 struct global_confirm *gc;
467
468 if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000469 return 0;
Darren Tucker9f407c42008-06-13 04:50:27 +1000470 if (gc->cb != NULL)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000471 gc->cb(ssh, type, seq, gc->ctx);
Darren Tucker9f407c42008-06-13 04:50:27 +1000472 if (--gc->ref_count <= 0) {
473 TAILQ_REMOVE(&global_confirms, gc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +1100474 explicit_bzero(gc, sizeof(*gc));
Darren Tuckera627d422013-06-02 07:31:17 +1000475 free(gc);
Darren Tucker9f407c42008-06-13 04:50:27 +1000476 }
477
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000478 ssh_packet_set_alive_timeouts(ssh, 0);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000479 return 0;
Damien Miller509b0102003-12-17 16:33:10 +1100480}
481
482static void
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000483server_alive_check(struct ssh *ssh)
Damien Miller509b0102003-12-17 16:33:10 +1100484{
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000485 int r;
486
487 if (ssh_packet_inc_alive_timeouts(ssh) > options.server_alive_count_max) {
Damien Millerb73b6fd2011-01-11 17:18:56 +1100488 logit("Timeout, server %s not responding.", host);
Damien Miller985a4482006-10-24 03:02:41 +1000489 cleanup_exit(255);
490 }
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000491 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
492 (r = sshpkt_put_cstring(ssh, "keepalive@openssh.com")) != 0 ||
493 (r = sshpkt_put_u8(ssh, 1)) != 0 || /* boolean: want reply */
494 (r = sshpkt_send(ssh)) != 0)
495 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker9f407c42008-06-13 04:50:27 +1000496 /* Insert an empty placeholder to maintain ordering */
497 client_register_global_confirm(NULL, NULL);
Damien Miller509b0102003-12-17 16:33:10 +1100498}
499
Damien Miller5428f641999-11-25 11:54:57 +1100500/*
501 * Waits until the client can do something (some data becomes available on
502 * one of the file descriptors).
503 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000504static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000505client_wait_until_can_do_something(struct ssh *ssh,
506 fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000507 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000508{
Damien Miller509b0102003-12-17 16:33:10 +1100509 struct timeval tv, *tvp;
Damien Millere11e1ea2010-08-03 16:04:46 +1000510 int timeout_secs;
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000511 time_t minwait_secs = 0, server_alive_time = 0, now = monotime();
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000512 int r, ret;
Damien Miller509b0102003-12-17 16:33:10 +1100513
Damien Miller5e953212001-01-30 09:14:00 +1100514 /* Add any selections by the channel mechanism. */
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +0000515 channel_prepare_select(ssh, readsetp, writesetp, maxfdp,
djm@openbsd.org71e5a532017-08-30 03:59:08 +0000516 nallocp, &minwait_secs);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000517
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000518 /* channel_prepare_select could have closed the last channel */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000519 if (session_closed && !channel_still_open(ssh) &&
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000520 !ssh_packet_have_data_to_write(ssh)) {
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000521 /* clear mask since we did not call select() */
522 memset(*readsetp, 0, *nallocp);
523 memset(*writesetp, 0, *nallocp);
524 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000525 }
526
djm@openbsd.org05164352017-04-30 23:21:54 +0000527 FD_SET(connection_in, *readsetp);
528
Damien Miller95def091999-11-25 00:26:21 +1100529 /* Select server connection if have data to write to the server. */
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000530 if (ssh_packet_have_data_to_write(ssh))
Damien Miller5e953212001-01-30 09:14:00 +1100531 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100532
Damien Miller5428f641999-11-25 11:54:57 +1100533 /*
534 * Wait for something to happen. This will suspend the process until
535 * some selected descriptor can be read, written, or has some other
Damien Millere11e1ea2010-08-03 16:04:46 +1000536 * event pending, or a timeout expires.
Damien Miller5428f641999-11-25 11:54:57 +1100537 */
Damien Miller95def091999-11-25 00:26:21 +1100538
Damien Millere11e1ea2010-08-03 16:04:46 +1000539 timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000540 if (options.server_alive_interval > 0) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000541 timeout_secs = options.server_alive_interval;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000542 server_alive_time = now + options.server_alive_interval;
543 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000544 if (options.rekey_interval > 0 && !rekeying)
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000545 timeout_secs = MINIMUM(timeout_secs,
546 ssh_packet_get_rekey_timeout(ssh));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000547 set_control_persist_exit_time(ssh);
Damien Millere11e1ea2010-08-03 16:04:46 +1000548 if (control_persist_exit_time > 0) {
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000549 timeout_secs = MINIMUM(timeout_secs,
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000550 control_persist_exit_time - now);
Damien Millere11e1ea2010-08-03 16:04:46 +1000551 if (timeout_secs < 0)
552 timeout_secs = 0;
553 }
Damien Millera6508752012-04-22 11:21:10 +1000554 if (minwait_secs != 0)
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000555 timeout_secs = MINIMUM(timeout_secs, (int)minwait_secs);
Damien Millere11e1ea2010-08-03 16:04:46 +1000556 if (timeout_secs == INT_MAX)
Damien Miller509b0102003-12-17 16:33:10 +1100557 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000558 else {
Damien Millere11e1ea2010-08-03 16:04:46 +1000559 tv.tv_sec = timeout_secs;
Damien Miller509b0102003-12-17 16:33:10 +1100560 tv.tv_usec = 0;
561 tvp = &tv;
562 }
Damien Millere11e1ea2010-08-03 16:04:46 +1000563
Damien Miller509b0102003-12-17 16:33:10 +1100564 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
565 if (ret < 0) {
Ben Lindstromf9452512001-02-15 03:12:08 +0000566 /*
567 * We have to clear the select masks, because we return.
568 * We have to return, because the mainloop checks for the flags
569 * set by the signal handlers.
570 */
Damien Miller79faeff2001-11-12 11:06:32 +1100571 memset(*readsetp, 0, *nallocp);
572 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000573
Damien Miller95def091999-11-25 00:26:21 +1100574 if (errno == EINTR)
575 return;
576 /* Note: we might still have data in the buffers. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000577 if ((r = sshbuf_putf(stderr_buffer,
578 "select: %s\r\n", strerror(errno))) != 0)
579 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +1100580 quit_pending = 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000581 } else if (ret == 0) {
582 /*
583 * Timeout. Could have been either keepalive or rekeying.
584 * Keepalive we check here, rekeying is checked in clientloop.
585 */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000586 if (server_alive_time != 0 && server_alive_time <= monotime())
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000587 server_alive_check(ssh);
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000588 }
589
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000590}
591
Ben Lindstrombba81212001-06-25 05:01:22 +0000592static void
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000593client_suspend_self(struct sshbuf *bin, struct sshbuf *bout, struct sshbuf *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000594{
Damien Miller95def091999-11-25 00:26:21 +1100595 /* Flush stdout and stderr buffers. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000596 if (sshbuf_len(bout) > 0)
597 atomicio(vwrite, fileno(stdout), sshbuf_mutable_ptr(bout),
598 sshbuf_len(bout));
599 if (sshbuf_len(berr) > 0)
600 atomicio(vwrite, fileno(stderr), sshbuf_mutable_ptr(berr),
601 sshbuf_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000602
Damien Miller21771e22011-05-15 08:45:50 +1000603 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000604
djm@openbsd.org5b8da1f2017-09-19 04:24:22 +0000605 sshbuf_reset(bin);
606 sshbuf_reset(bout);
607 sshbuf_reset(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000608
Damien Miller95def091999-11-25 00:26:21 +1100609 /* Send the suspend signal to the program itself. */
610 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000611
Darren Tucker5d78de62004-11-05 20:35:44 +1100612 /* Reset window sizes in case they have changed */
613 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000614
Damien Miller21771e22011-05-15 08:45:50 +1000615 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000616}
617
Ben Lindstrombba81212001-06-25 05:01:22 +0000618static void
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000619client_process_net_input(struct ssh *ssh, fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000620{
Darren Tucker86e30a02009-08-28 11:21:06 +1000621 char buf[SSH_IOBUFSZ];
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000622 int r, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000623
Damien Miller5428f641999-11-25 11:54:57 +1100624 /*
625 * Read input from the server, and add any such data to the buffer of
626 * the packet subsystem.
627 */
Damien Miller95def091999-11-25 00:26:21 +1100628 if (FD_ISSET(connection_in, readset)) {
629 /* Read as much as possible. */
markus@openbsd.orga3068632016-01-14 16:17:39 +0000630 len = read(connection_in, buf, sizeof(buf));
631 if (len == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000632 /*
633 * Received EOF. The remote host has closed the
634 * connection.
635 */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000636 if ((r = sshbuf_putf(stderr_buffer,
Darren Tucker4d5cd332008-06-13 04:51:14 +1000637 "Connection to %.300s closed by remote host.\r\n",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000638 host)) != 0)
639 fatal("%s: buffer error: %s",
640 __func__, ssh_err(r));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000641 quit_pending = 1;
642 return;
Damien Miller95def091999-11-25 00:26:21 +1100643 }
Damien Miller5428f641999-11-25 11:54:57 +1100644 /*
645 * There is a kernel bug on Solaris that causes select to
646 * sometimes wake up even though there is no data available.
647 */
Damien Millerd8968ad2008-07-04 23:10:49 +1000648 if (len < 0 &&
649 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Damien Miller95def091999-11-25 00:26:21 +1100650 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000651
Damien Miller95def091999-11-25 00:26:21 +1100652 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000653 /*
654 * An error has encountered. Perhaps there is a
655 * network problem.
656 */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000657 if ((r = sshbuf_putf(stderr_buffer,
Darren Tucker4d5cd332008-06-13 04:51:14 +1000658 "Read from remote host %.300s: %.100s\r\n",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000659 host, strerror(errno))) != 0)
660 fatal("%s: buffer error: %s",
661 __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +1100662 quit_pending = 1;
663 return;
664 }
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000665 ssh_packet_process_incoming(ssh, buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100666 }
Damien Miller1383bd82000-04-06 12:32:37 +1000667}
668
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000669static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000670client_status_confirm(struct ssh *ssh, int type, Channel *c, void *ctx)
Damien Miller0e220db2004-06-15 10:34:08 +1000671{
Damien Miller5771ed72008-05-19 15:35:33 +1000672 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
673 char errmsg[256];
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000674 int r, tochan;
Darren Tuckerfc959702004-07-17 16:12:08 +1000675
Damien Miller555f3b82011-05-15 08:48:05 +1000676 /*
677 * If a TTY was explicitly requested, then a failure to allocate
678 * one is fatal.
679 */
680 if (cr->action == CONFIRM_TTY &&
681 (options.request_tty == REQUEST_TTY_FORCE ||
682 options.request_tty == REQUEST_TTY_YES))
683 cr->action = CONFIRM_CLOSE;
684
djm@openbsd.org001aa552018-04-10 00:10:49 +0000685 /* XXX suppress on mux _client_ quietmode */
Damien Miller5771ed72008-05-19 15:35:33 +1000686 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
Damien Millere1537f92010-01-26 13:26:22 +1100687 c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
Damien Miller0e220db2004-06-15 10:34:08 +1000688
Damien Miller5771ed72008-05-19 15:35:33 +1000689 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
690 debug2("%s request accepted on channel %d",
691 cr->request_type, c->self);
692 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
693 if (tochan) {
694 snprintf(errmsg, sizeof(errmsg),
695 "%s request failed\r\n", cr->request_type);
696 } else {
697 snprintf(errmsg, sizeof(errmsg),
698 "%s request failed on channel %d",
699 cr->request_type, c->self);
700 }
701 /* If error occurred on primary session channel, then exit */
Damien Miller555f3b82011-05-15 08:48:05 +1000702 if (cr->action == CONFIRM_CLOSE && c->self == session_ident)
Damien Miller5771ed72008-05-19 15:35:33 +1000703 fatal("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000704 /*
705 * If error occurred on mux client, append to
706 * their stderr.
707 */
708 if (tochan) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000709 if ((r = sshbuf_put(c->extended, errmsg,
710 strlen(errmsg))) != 0)
711 fatal("%s: buffer error %s", __func__,
712 ssh_err(r));
Damien Miller555f3b82011-05-15 08:48:05 +1000713 } else
Damien Miller5771ed72008-05-19 15:35:33 +1000714 error("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000715 if (cr->action == CONFIRM_TTY) {
716 /*
717 * If a TTY allocation error occurred, then arrange
718 * for the correct TTY to leave raw mode.
719 */
720 if (c->self == session_ident)
721 leave_raw_mode(0);
722 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000723 mux_tty_alloc_failed(ssh, c);
Damien Miller555f3b82011-05-15 08:48:05 +1000724 } else if (cr->action == CONFIRM_CLOSE) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000725 chan_read_failed(ssh, c);
726 chan_write_failed(ssh, c);
Damien Miller5771ed72008-05-19 15:35:33 +1000727 }
Damien Miller0e220db2004-06-15 10:34:08 +1000728 }
Darren Tuckera627d422013-06-02 07:31:17 +1000729 free(cr);
Damien Miller5771ed72008-05-19 15:35:33 +1000730}
Damien Miller0e220db2004-06-15 10:34:08 +1000731
Damien Miller5771ed72008-05-19 15:35:33 +1000732static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000733client_abandon_status_confirm(struct ssh *ssh, Channel *c, void *ctx)
Damien Miller5771ed72008-05-19 15:35:33 +1000734{
Darren Tuckera627d422013-06-02 07:31:17 +1000735 free(ctx);
Damien Miller5771ed72008-05-19 15:35:33 +1000736}
737
Damien Miller6d7b4372011-06-23 08:31:57 +1000738void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000739client_expect_confirm(struct ssh *ssh, int id, const char *request,
Damien Miller555f3b82011-05-15 08:48:05 +1000740 enum confirm_action action)
Damien Miller5771ed72008-05-19 15:35:33 +1000741{
Damien Miller6c81fee2013-11-08 12:19:55 +1100742 struct channel_reply_ctx *cr = xcalloc(1, sizeof(*cr));
Damien Miller5771ed72008-05-19 15:35:33 +1000743
744 cr->request_type = request;
Damien Miller555f3b82011-05-15 08:48:05 +1000745 cr->action = action;
Damien Miller5771ed72008-05-19 15:35:33 +1000746
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000747 channel_register_status_confirm(ssh, id, client_status_confirm,
Damien Miller5771ed72008-05-19 15:35:33 +1000748 client_abandon_status_confirm, cr);
Damien Miller0e220db2004-06-15 10:34:08 +1000749}
750
Darren Tucker9f407c42008-06-13 04:50:27 +1000751void
752client_register_global_confirm(global_confirm_cb *cb, void *ctx)
753{
Damien Millerb9d3bee2008-07-16 22:40:52 +1000754 struct global_confirm *gc, *last_gc;
Darren Tucker9f407c42008-06-13 04:50:27 +1000755
756 /* Coalesce identical callbacks */
Damien Millerb9d3bee2008-07-16 22:40:52 +1000757 last_gc = TAILQ_LAST(&global_confirms, global_confirms);
758 if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
759 if (++last_gc->ref_count >= INT_MAX)
760 fatal("%s: last_gc->ref_count = %d",
761 __func__, last_gc->ref_count);
Darren Tucker9f407c42008-06-13 04:50:27 +1000762 return;
763 }
764
Damien Miller6c81fee2013-11-08 12:19:55 +1100765 gc = xcalloc(1, sizeof(*gc));
Darren Tucker9f407c42008-06-13 04:50:27 +1000766 gc->cb = cb;
767 gc->ctx = ctx;
768 gc->ref_count = 1;
769 TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
770}
771
Damien Miller0e220db2004-06-15 10:34:08 +1000772static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000773process_cmdline(struct ssh *ssh)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000774{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000775 void (*handler)(int);
Damien Miller7acefbb2014-07-18 14:11:24 +1000776 char *s, *cmd;
777 int ok, delete = 0, local = 0, remote = 0, dynamic = 0;
778 struct Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000779
Damien Miller1d2c4562014-02-04 11:18:20 +1100780 memset(&fwd, 0, sizeof(fwd));
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100781
Damien Miller21771e22011-05-15 08:45:50 +1000782 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000783 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000784 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000785 if (s == NULL)
786 goto out;
Damien Millerfdb23062013-11-21 13:57:15 +1100787 while (isspace((u_char)*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000788 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000789 if (*s == '-')
790 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000791 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000792 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000793
Darren Tucker1973c882004-05-24 10:34:36 +1000794 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000795 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000796 logit(" -L[bind_address:]port:host:hostport "
797 "Request local forward");
798 logit(" -R[bind_address:]port:host:hostport "
799 "Request remote forward");
Damien Miller0164cb82008-11-05 16:30:31 +1100800 logit(" -D[bind_address:]port "
801 "Request dynamic forward");
Damien Millerff773642011-09-22 21:39:48 +1000802 logit(" -KL[bind_address:]port "
803 "Cancel local forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000804 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000805 "Cancel remote forward");
Damien Millerff773642011-09-22 21:39:48 +1000806 logit(" -KD[bind_address:]port "
807 "Cancel dynamic forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100808 if (!options.permit_local_command)
809 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000810 logit(" !args "
811 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100812 goto out;
813 }
814
815 if (*s == '!' && options.permit_local_command) {
816 s++;
817 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000818 goto out;
819 }
820
821 if (*s == 'K') {
822 delete = 1;
823 s++;
824 }
Damien Miller0164cb82008-11-05 16:30:31 +1100825 if (*s == 'L')
826 local = 1;
827 else if (*s == 'R')
828 remote = 1;
829 else if (*s == 'D')
830 dynamic = 1;
831 else {
Damien Miller996acd22003-04-09 20:59:48 +1000832 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000833 goto out;
834 }
Damien Miller0164cb82008-11-05 16:30:31 +1100835
Damien Millerfdb23062013-11-21 13:57:15 +1100836 while (isspace((u_char)*++s))
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100837 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000838
Damien Millere1537f92010-01-26 13:26:22 +1100839 /* XXX update list of forwards in options */
Darren Tuckere7066df2004-05-24 10:18:05 +1000840 if (delete) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000841 /* We pass 1 for dynamicfwd to restrict to 1 or 2 fields. */
842 if (!parse_forward(&fwd, s, 1, 0)) {
843 logit("Bad forwarding close specification.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000844 goto out;
845 }
Damien Millerff773642011-09-22 21:39:48 +1000846 if (remote)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000847 ok = channel_request_rforward_cancel(ssh, &fwd) == 0;
Damien Millerff773642011-09-22 21:39:48 +1000848 else if (dynamic)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000849 ok = channel_cancel_lport_listener(ssh, &fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +1000850 0, &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000851 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000852 ok = channel_cancel_lport_listener(ssh, &fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +1000853 CHANNEL_CANCEL_PORT_STATIC,
854 &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000855 if (!ok) {
dtucker@openbsd.org9390b002017-01-29 21:35:23 +0000856 logit("Unknown port forwarding.");
Damien Millerff773642011-09-22 21:39:48 +1000857 goto out;
858 }
859 logit("Canceled forwarding.");
Darren Tuckere7066df2004-05-24 10:18:05 +1000860 } else {
Damien Miller4bf648f2009-02-14 16:28:21 +1100861 if (!parse_forward(&fwd, s, dynamic, remote)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000862 logit("Bad forwarding specification.");
863 goto out;
864 }
Damien Miller0164cb82008-11-05 16:30:31 +1100865 if (local || dynamic) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000866 if (!channel_setup_local_fwd_listener(ssh, &fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +1000867 &options.fwd_opts)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000868 logit("Port forwarding failed.");
869 goto out;
870 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100871 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000872 if (channel_request_remote_forwarding(ssh, &fwd) < 0) {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000873 logit("Port forwarding failed.");
874 goto out;
875 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100876 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000877 logit("Forwarding port.");
878 }
879
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000880out:
881 signal(SIGINT, handler);
Damien Miller21771e22011-05-15 08:45:50 +1000882 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tuckera627d422013-06-02 07:31:17 +1000883 free(cmd);
884 free(fwd.listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000885 free(fwd.listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000886 free(fwd.connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000887 free(fwd.connect_path);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000888}
889
Darren Tucker92a39cf2012-09-07 11:20:20 +1000890/* reasons to suppress output of an escape command in help output */
891#define SUPPRESS_NEVER 0 /* never suppress, always show */
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000892#define SUPPRESS_MUXCLIENT 1 /* don't show in mux client sessions */
893#define SUPPRESS_MUXMASTER 2 /* don't show in mux master sessions */
894#define SUPPRESS_SYSLOG 4 /* don't show when logging to syslog */
Darren Tucker92a39cf2012-09-07 11:20:20 +1000895struct escape_help_text {
896 const char *cmd;
897 const char *text;
898 unsigned int flags;
899};
900static struct escape_help_text esc_txt[] = {
901 {".", "terminate session", SUPPRESS_MUXMASTER},
902 {".", "terminate connection (and any multiplexed sessions)",
903 SUPPRESS_MUXCLIENT},
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000904 {"B", "send a BREAK to the remote system", SUPPRESS_NEVER},
Darren Tucker92a39cf2012-09-07 11:20:20 +1000905 {"C", "open a command line", SUPPRESS_MUXCLIENT},
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000906 {"R", "request rekey", SUPPRESS_NEVER},
Darren Tuckerca0d0fd2012-09-07 11:22:24 +1000907 {"V/v", "decrease/increase verbosity (LogLevel)", SUPPRESS_MUXCLIENT},
Darren Tucker92a39cf2012-09-07 11:20:20 +1000908 {"^Z", "suspend ssh", SUPPRESS_MUXCLIENT},
909 {"#", "list forwarded connections", SUPPRESS_NEVER},
910 {"&", "background ssh (when waiting for connections to terminate)",
911 SUPPRESS_MUXCLIENT},
912 {"?", "this message", SUPPRESS_NEVER},
913};
914
915static void
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000916print_escape_help(struct sshbuf *b, int escape_char, int mux_client,
917 int using_stderr)
Darren Tucker92a39cf2012-09-07 11:20:20 +1000918{
919 unsigned int i, suppress_flags;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000920 int r;
Darren Tucker92a39cf2012-09-07 11:20:20 +1000921
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000922 if ((r = sshbuf_putf(b,
923 "%c?\r\nSupported escape sequences:\r\n", escape_char)) != 0)
924 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker92a39cf2012-09-07 11:20:20 +1000925
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000926 suppress_flags =
Darren Tucker92a39cf2012-09-07 11:20:20 +1000927 (mux_client ? SUPPRESS_MUXCLIENT : 0) |
928 (mux_client ? 0 : SUPPRESS_MUXMASTER) |
929 (using_stderr ? 0 : SUPPRESS_SYSLOG);
930
931 for (i = 0; i < sizeof(esc_txt)/sizeof(esc_txt[0]); i++) {
932 if (esc_txt[i].flags & suppress_flags)
933 continue;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000934 if ((r = sshbuf_putf(b, " %c%-3s - %s\r\n",
935 escape_char, esc_txt[i].cmd, esc_txt[i].text)) != 0)
936 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker92a39cf2012-09-07 11:20:20 +1000937 }
938
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000939 if ((r = sshbuf_putf(b,
Darren Tuckerca0d0fd2012-09-07 11:22:24 +1000940 " %c%c - send the escape character by typing it twice\r\n"
Darren Tucker92a39cf2012-09-07 11:20:20 +1000941 "(Note that escapes are only recognized immediately after "
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000942 "newline.)\r\n", escape_char, escape_char)) != 0)
943 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker92a39cf2012-09-07 11:20:20 +1000944}
945
djm@openbsd.org84623e02018-06-26 02:02:36 +0000946/*
naddy@openbsd.org768405f2017-05-03 21:08:09 +0000947 * Process the characters one by one.
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000948 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000949static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000950process_escapes(struct ssh *ssh, Channel *c,
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000951 struct sshbuf *bin, struct sshbuf *bout, struct sshbuf *berr,
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000952 char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +1000953{
Damien Millerad833b32000-08-23 10:46:23 +1000954 pid_t pid;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000955 int r, bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000956 u_int i;
957 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +1000958 char *s;
djm@openbsd.org05164352017-04-30 23:21:54 +0000959 struct escape_filter_ctx *efc = c->filter_ctx == NULL ?
960 NULL : (struct escape_filter_ctx *)c->filter_ctx;
Damien Millerad833b32000-08-23 10:46:23 +1000961
djm@openbsd.org05164352017-04-30 23:21:54 +0000962 if (c->filter_ctx == NULL)
963 return 0;
djm@openbsd.org84623e02018-06-26 02:02:36 +0000964
Damien Millereccb9de2005-06-17 12:59:34 +1000965 if (len <= 0)
966 return (0);
967
968 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +1000969 /* Get one character at a time. */
970 ch = buf[i];
971
djm@openbsd.org05164352017-04-30 23:21:54 +0000972 if (efc->escape_pending) {
Damien Millerad833b32000-08-23 10:46:23 +1000973 /* We have previously seen an escape character. */
974 /* Clear the flag now. */
djm@openbsd.org05164352017-04-30 23:21:54 +0000975 efc->escape_pending = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000976
977 /* Process the escaped character. */
978 switch (ch) {
979 case '.':
980 /* Terminate the connection. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000981 if ((r = sshbuf_putf(berr, "%c.\r\n",
982 efc->escape_char)) != 0)
983 fatal("%s: buffer error: %s",
984 __func__, ssh_err(r));
Damien Millere1537f92010-01-26 13:26:22 +1100985 if (c && c->ctl_chan != -1) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000986 chan_read_failed(ssh, c);
987 chan_write_failed(ssh, c);
988 if (c->detach_user) {
989 c->detach_user(ssh,
990 c->self, NULL);
991 }
Damien Miller36187092013-06-10 13:07:11 +1000992 c->type = SSH_CHANNEL_ABANDONED;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000993 sshbuf_reset(c->input);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000994 chan_ibuf_empty(ssh, c);
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000995 return 0;
996 } else
997 quit_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +1000998 return -1;
999
1000 case 'Z' - 64:
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001001 /* XXX support this for mux clients */
Damien Millere1537f92010-01-26 13:26:22 +11001002 if (c && c->ctl_chan != -1) {
Darren Tuckerf111d402012-09-07 11:21:42 +10001003 char b[16];
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001004 noescape:
Darren Tuckerf111d402012-09-07 11:21:42 +10001005 if (ch == 'Z' - 64)
1006 snprintf(b, sizeof b, "^Z");
1007 else
1008 snprintf(b, sizeof b, "%c", ch);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001009 if ((r = sshbuf_putf(berr,
Darren Tuckerf111d402012-09-07 11:21:42 +10001010 "%c%s escape not available to "
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001011 "multiplexed sessions\r\n",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001012 efc->escape_char, b)) != 0)
1013 fatal("%s: buffer error: %s",
1014 __func__, ssh_err(r));
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001015 continue;
1016 }
Darren Tucker4d5cd332008-06-13 04:51:14 +10001017 /* Suspend the program. Inform the user */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001018 if ((r = sshbuf_putf(berr,
1019 "%c^Z [suspend ssh]\r\n",
1020 efc->escape_char)) != 0)
1021 fatal("%s: buffer error: %s",
1022 __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +10001023
1024 /* Restore terminal modes and suspend. */
1025 client_suspend_self(bin, bout, berr);
1026
1027 /* We have been continued. */
1028 continue;
1029
Damien Miller54c45982003-05-15 10:20:13 +10001030 case 'B':
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001031 if ((r = sshbuf_putf(berr,
1032 "%cB\r\n", efc->escape_char)) != 0)
1033 fatal("%s: buffer error: %s",
1034 __func__, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001035 channel_request_start(ssh, c->self, "break", 0);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001036 if ((r = sshpkt_put_u32(ssh, 1000)) != 0 ||
1037 (r = sshpkt_send(ssh)) != 0)
1038 fatal("%s: %s", __func__,
1039 ssh_err(r));
Damien Miller54c45982003-05-15 10:20:13 +10001040 continue;
1041
Ben Lindstromf28f6342001-04-04 02:03:04 +00001042 case 'R':
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001043 if (datafellows & SSH_BUG_NOREKEY)
1044 logit("Server does not "
1045 "support re-keying");
1046 else
1047 need_rekeying = 1;
Ben Lindstromf28f6342001-04-04 02:03:04 +00001048 continue;
1049
Darren Tucker50a48d02012-09-06 21:25:37 +10001050 case 'V':
1051 /* FALLTHROUGH */
1052 case 'v':
1053 if (c && c->ctl_chan != -1)
1054 goto noescape;
1055 if (!log_is_on_stderr()) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001056 if ((r = sshbuf_putf(berr,
Darren Tucker50a48d02012-09-06 21:25:37 +10001057 "%c%c [Logging to syslog]\r\n",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001058 efc->escape_char, ch)) != 0)
1059 fatal("%s: buffer error: %s",
1060 __func__, ssh_err(r));
Darren Tucker50a48d02012-09-06 21:25:37 +10001061 continue;
1062 }
1063 if (ch == 'V' && options.log_level >
1064 SYSLOG_LEVEL_QUIET)
1065 log_change_level(--options.log_level);
1066 if (ch == 'v' && options.log_level <
1067 SYSLOG_LEVEL_DEBUG3)
1068 log_change_level(++options.log_level);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001069 if ((r = sshbuf_putf(berr,
djm@openbsd.org05164352017-04-30 23:21:54 +00001070 "%c%c [LogLevel %s]\r\n",
1071 efc->escape_char, ch,
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001072 log_level_name(options.log_level))) != 0)
1073 fatal("%s: buffer error: %s",
1074 __func__, ssh_err(r));
Darren Tucker50a48d02012-09-06 21:25:37 +10001075 continue;
1076
Damien Millerad833b32000-08-23 10:46:23 +10001077 case '&':
Damien Millere1537f92010-01-26 13:26:22 +11001078 if (c && c->ctl_chan != -1)
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001079 goto noescape;
Damien Millerad833b32000-08-23 10:46:23 +10001080 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001081 * Detach the program (continue to serve
1082 * connections, but put in background and no
1083 * more new connections).
Damien Millerad833b32000-08-23 10:46:23 +10001084 */
Damien Miller96507ef2001-11-12 10:52:25 +11001085 /* Restore tty modes. */
Damien Miller21771e22011-05-15 08:45:50 +10001086 leave_raw_mode(
1087 options.request_tty == REQUEST_TTY_FORCE);
Damien Miller96507ef2001-11-12 10:52:25 +11001088
1089 /* Stop listening for new connections. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001090 channel_stop_listening(ssh);
Damien Miller96507ef2001-11-12 10:52:25 +11001091
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001092 if ((r = sshbuf_putf(berr,
1093 "%c& [backgrounded]\n", efc->escape_char))
1094 != 0)
1095 fatal("%s: buffer error: %s",
1096 __func__, ssh_err(r));
Damien Miller96507ef2001-11-12 10:52:25 +11001097
1098 /* Fork into background. */
1099 pid = fork();
1100 if (pid < 0) {
1101 error("fork: %.100s", strerror(errno));
1102 continue;
1103 }
1104 if (pid != 0) { /* This is the parent. */
1105 /* The parent just exits. */
1106 exit(0);
1107 }
1108 /* The child continues serving connections. */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001109 /* fake EOF on stdin */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001110 if ((r = sshbuf_put_u8(bin, 4)) != 0)
1111 fatal("%s: buffer error: %s",
1112 __func__, ssh_err(r));
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001113 return -1;
Damien Millerad833b32000-08-23 10:46:23 +10001114 case '?':
djm@openbsd.org05164352017-04-30 23:21:54 +00001115 print_escape_help(berr, efc->escape_char,
Darren Tucker92a39cf2012-09-07 11:20:20 +10001116 (c && c->ctl_chan != -1),
1117 log_is_on_stderr());
Damien Millerad833b32000-08-23 10:46:23 +10001118 continue;
1119
1120 case '#':
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001121 if ((r = sshbuf_putf(berr, "%c#\r\n",
1122 efc->escape_char)) != 0)
1123 fatal("%s: buffer error: %s",
1124 __func__, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001125 s = channel_open_message(ssh);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001126 if ((r = sshbuf_put(berr, s, strlen(s))) != 0)
1127 fatal("%s: buffer error: %s",
1128 __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +10001129 free(s);
Damien Millerad833b32000-08-23 10:46:23 +10001130 continue;
1131
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001132 case 'C':
Damien Millere1537f92010-01-26 13:26:22 +11001133 if (c && c->ctl_chan != -1)
Damien Miller586b0052008-12-09 14:11:32 +11001134 goto noescape;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001135 process_cmdline(ssh);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001136 continue;
1137
Damien Millerad833b32000-08-23 10:46:23 +10001138 default:
djm@openbsd.org05164352017-04-30 23:21:54 +00001139 if (ch != efc->escape_char) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001140 if ((r = sshbuf_put_u8(bin,
1141 efc->escape_char)) != 0)
1142 fatal("%s: buffer error: %s",
1143 __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +10001144 bytes++;
1145 }
1146 /* Escaped characters fall through here */
1147 break;
1148 }
1149 } else {
1150 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001151 * The previous character was not an escape char.
1152 * Check if this is an escape.
Damien Millerad833b32000-08-23 10:46:23 +10001153 */
djm@openbsd.org05164352017-04-30 23:21:54 +00001154 if (last_was_cr && ch == efc->escape_char) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001155 /*
1156 * It is. Set the flag and continue to
1157 * next character.
1158 */
djm@openbsd.org05164352017-04-30 23:21:54 +00001159 efc->escape_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001160 continue;
1161 }
1162 }
1163
1164 /*
1165 * Normal character. Record whether it was a newline,
1166 * and append it to the buffer.
1167 */
1168 last_was_cr = (ch == '\r' || ch == '\n');
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001169 if ((r = sshbuf_put_u8(bin, ch)) != 0)
1170 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +10001171 bytes++;
1172 }
1173 return bytes;
1174}
1175
Damien Miller5428f641999-11-25 11:54:57 +11001176/*
Damien Millerb38eff82000-04-01 11:09:21 +10001177 * Get packets from the connection input buffer, and process them as long as
1178 * there are packets available.
1179 *
1180 * Any unknown packets received during the actual
1181 * session cause the session to terminate. This is
1182 * intended to make debugging easier since no
1183 * confirmations are sent. Any compatible protocol
1184 * extensions must be negotiated during the
1185 * preparatory phase.
1186 */
1187
Ben Lindstrombba81212001-06-25 05:01:22 +00001188static void
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00001189client_process_buffered_input_packets(struct ssh *ssh)
Damien Millerb38eff82000-04-01 11:09:21 +10001190{
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00001191 ssh_dispatch_run_fatal(ssh, DISPATCH_NONBLOCK, &quit_pending);
Damien Millerb38eff82000-04-01 11:09:21 +10001192}
1193
Damien Millerad833b32000-08-23 10:46:23 +10001194/* scan buf[] for '~' before sending data to the peer */
1195
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001196/* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1197void *
1198client_new_escape_filter_ctx(int escape_char)
1199{
1200 struct escape_filter_ctx *ret;
1201
Damien Miller6c81fee2013-11-08 12:19:55 +11001202 ret = xcalloc(1, sizeof(*ret));
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001203 ret->escape_pending = 0;
1204 ret->escape_char = escape_char;
1205 return (void *)ret;
1206}
1207
Darren Tucker84c56f52008-06-13 04:55:46 +10001208/* Free the escape filter context on channel free */
1209void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001210client_filter_cleanup(struct ssh *ssh, int cid, void *ctx)
Darren Tucker84c56f52008-06-13 04:55:46 +10001211{
Darren Tuckera627d422013-06-02 07:31:17 +10001212 free(ctx);
Darren Tucker84c56f52008-06-13 04:55:46 +10001213}
1214
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001215int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001216client_simple_escape_filter(struct ssh *ssh, Channel *c, char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001217{
Damien Miller5771ed72008-05-19 15:35:33 +10001218 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1219 return 0;
1220
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001221 return process_escapes(ssh, c, c->input, c->output, c->extended,
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001222 buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001223}
1224
Ben Lindstrombba81212001-06-25 05:01:22 +00001225static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001226client_channel_closed(struct ssh *ssh, int id, void *arg)
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001227{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001228 channel_cancel_cleanup(ssh, id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001229 session_closed = 1;
Damien Miller21771e22011-05-15 08:45:50 +10001230 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001231}
1232
Damien Millerb38eff82000-04-01 11:09:21 +10001233/*
Damien Miller5428f641999-11-25 11:54:57 +11001234 * Implements the interactive session with the server. This is called after
1235 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001236 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1237 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001238 */
Damien Miller4af51302000-04-16 11:18:38 +10001239int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001240client_loop(struct ssh *ssh, int have_pty, int escape_char_arg,
1241 int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001242{
Damien Miller5e953212001-01-30 09:14:00 +11001243 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001244 double start_time, total_time;
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001245 int r, max_fd = 0, max_fd2 = 0, len;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001246 u_int64_t ibytes, obytes;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001247 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001248 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001249
Damien Miller95def091999-11-25 00:26:21 +11001250 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001251
semarie@openbsd.orgd7d2bc92015-12-26 07:46:03 +00001252 if (options.control_master &&
djm@openbsd.org368dd972016-07-23 02:54:08 +00001253 !option_clear_or_none(options.control_path)) {
semarie@openbsd.orgd7d2bc92015-12-26 07:46:03 +00001254 debug("pledge: id");
tb@openbsd.org372807c2016-07-11 21:38:13 +00001255 if (pledge("stdio rpath wpath cpath unix inet dns recvfd proc exec id tty",
semarie@openbsd.orgd7d2bc92015-12-26 07:46:03 +00001256 NULL) == -1)
1257 fatal("%s pledge(): %s", __func__, strerror(errno));
1258
1259 } else if (options.forward_x11 || options.permit_local_command) {
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001260 debug("pledge: exec");
1261 if (pledge("stdio rpath wpath cpath unix inet dns proc exec tty",
1262 NULL) == -1)
1263 fatal("%s pledge(): %s", __func__, strerror(errno));
1264
1265 } else if (options.update_hostkeys) {
1266 debug("pledge: filesystem full");
1267 if (pledge("stdio rpath wpath cpath unix inet dns proc tty",
1268 NULL) == -1)
1269 fatal("%s pledge(): %s", __func__, strerror(errno));
1270
djm@openbsd.org368dd972016-07-23 02:54:08 +00001271 } else if (!option_clear_or_none(options.proxy_command) ||
1272 fork_after_authentication_flag) {
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001273 debug("pledge: proc");
1274 if (pledge("stdio cpath unix inet dns proc tty", NULL) == -1)
1275 fatal("%s pledge(): %s", __func__, strerror(errno));
1276
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001277 } else {
1278 debug("pledge: network");
mestre@openbsd.orge2386452017-06-23 07:24:48 +00001279 if (pledge("stdio unix inet dns proc tty", NULL) == -1)
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001280 fatal("%s pledge(): %s", __func__, strerror(errno));
1281 }
1282
dtucker@openbsd.org@openbsd.org2d638e92017-11-25 05:58:47 +00001283 start_time = monotime_double();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001284
Damien Miller95def091999-11-25 00:26:21 +11001285 /* Initialize variables. */
Damien Miller95def091999-11-25 00:26:21 +11001286 last_was_cr = 1;
1287 exit_status = -1;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001288 connection_in = ssh_packet_get_connection_in(ssh);
1289 connection_out = ssh_packet_get_connection_out(ssh);
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001290 max_fd = MAXIMUM(connection_in, connection_out);
Damien Miller5e953212001-01-30 09:14:00 +11001291
Damien Miller95def091999-11-25 00:26:21 +11001292 quit_pending = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001293
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001294 /* Initialize buffer. */
1295 if ((stderr_buffer = sshbuf_new()) == NULL)
1296 fatal("%s: sshbuf_new failed", __func__);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001297
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00001298 client_init_dispatch(ssh);
Damien Millerb38eff82000-04-01 11:09:21 +10001299
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001300 /*
1301 * Set signal handlers, (e.g. to restore non-blocking mode)
1302 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1303 */
Darren Tucker07336da2004-11-05 20:02:16 +11001304 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1305 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001306 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1307 signal(SIGINT, signal_handler);
1308 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1309 signal(SIGQUIT, signal_handler);
1310 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1311 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001312 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001313
Damien Miller95def091999-11-25 00:26:21 +11001314 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001315 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001316
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001317 session_ident = ssh2_chan_id;
1318 if (session_ident != -1) {
1319 if (escape_char_arg != SSH_ESCAPECHAR_NONE) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001320 channel_register_filter(ssh, session_ident,
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001321 client_simple_escape_filter, NULL,
1322 client_filter_cleanup,
1323 client_new_escape_filter_ctx(
1324 escape_char_arg));
Damien Miller6c3eec72011-05-05 14:16:22 +10001325 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001326 channel_register_cleanup(ssh, session_ident,
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001327 client_channel_closed, 0);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001328 }
Damien Millerad833b32000-08-23 10:46:23 +10001329
Damien Miller95def091999-11-25 00:26:21 +11001330 /* Main loop of the client for the interactive session mode. */
1331 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001332
Damien Miller5428f641999-11-25 11:54:57 +11001333 /* Process buffered packets sent by the server. */
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00001334 client_process_buffered_input_packets(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001335
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001336 if (session_closed && !channel_still_open(ssh))
Damien Miller1383bd82000-04-06 12:32:37 +10001337 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001338
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001339 if (ssh_packet_is_rekeying(ssh)) {
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001340 debug("rekeying in progress");
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001341 } else if (need_rekeying) {
1342 /* manual rekey request */
1343 debug("need rekeying");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001344 if ((r = kex_start_rekex(ssh)) != 0)
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001345 fatal("%s: kex_start_rekex: %s", __func__,
1346 ssh_err(r));
1347 need_rekeying = 0;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001348 } else {
1349 /*
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001350 * Make packets from buffered channel data, and
1351 * enqueue them for sending to the server.
1352 */
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001353 if (ssh_packet_not_very_much_data_to_write(ssh))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001354 channel_output_poll(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001355
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001356 /*
1357 * Check if the window size has changed, and buffer a
1358 * message about it to the server if so.
1359 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001360 client_check_window_change(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001361
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001362 if (quit_pending)
1363 break;
1364 }
Damien Miller5428f641999-11-25 11:54:57 +11001365 /*
1366 * Wait until we have something to do (something becomes
1367 * available on one of the descriptors).
1368 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001369 max_fd2 = max_fd;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001370 client_wait_until_can_do_something(ssh, &readset, &writeset,
1371 &max_fd2, &nalloc, ssh_packet_is_rekeying(ssh));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001372
Damien Miller95def091999-11-25 00:26:21 +11001373 if (quit_pending)
1374 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001375
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001376 /* Do channel operations unless rekeying in progress. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001377 if (!ssh_packet_is_rekeying(ssh))
1378 channel_after_select(ssh, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001379
Damien Miller1383bd82000-04-06 12:32:37 +10001380 /* Buffer input from the connection. */
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001381 client_process_net_input(ssh, readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001382
Damien Miller1383bd82000-04-06 12:32:37 +10001383 if (quit_pending)
1384 break;
1385
Darren Tucker4d5cd332008-06-13 04:51:14 +10001386 /*
1387 * Send as much buffered packet data as possible to the
1388 * sender.
1389 */
Damien Miller5e953212001-01-30 09:14:00 +11001390 if (FD_ISSET(connection_out, writeset))
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001391 ssh_packet_write_poll(ssh);
Damien Millere11e1ea2010-08-03 16:04:46 +10001392
1393 /*
1394 * If we are a backgrounded control master, and the
1395 * timeout has expired without any active client
1396 * connections, then quit.
1397 */
1398 if (control_persist_exit_time > 0) {
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001399 if (monotime() >= control_persist_exit_time) {
Damien Millere11e1ea2010-08-03 16:04:46 +10001400 debug("ControlPersist timeout expired");
1401 break;
1402 }
1403 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001404 }
Darren Tuckera627d422013-06-02 07:31:17 +10001405 free(readset);
1406 free(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001407
Damien Miller95def091999-11-25 00:26:21 +11001408 /* Terminate the session. */
1409
1410 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001411 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001412
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001413 if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
1414 (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_BY_APPLICATION)) != 0 ||
1415 (r = sshpkt_put_cstring(ssh, "disconnected by user")) != 0 ||
1416 (r = sshpkt_put_cstring(ssh, "")) != 0 || /* language tag */
1417 (r = sshpkt_send(ssh)) != 0 ||
1418 (r = ssh_packet_write_wait(ssh)) != 0)
1419 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker12b4a652009-06-21 18:14:48 +10001420
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001421 channel_free_all(ssh);
Damien Miller95def091999-11-25 00:26:21 +11001422
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001423 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001424 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001425
1426 /* restore blocking io */
1427 if (!isatty(fileno(stdin)))
1428 unset_nonblock(fileno(stdin));
1429 if (!isatty(fileno(stdout)))
1430 unset_nonblock(fileno(stdout));
1431 if (!isatty(fileno(stderr)))
1432 unset_nonblock(fileno(stderr));
1433
Damien Millerd6965512003-12-17 16:31:53 +11001434 /*
1435 * If there was no shell or command requested, there will be no remote
1436 * exit status to be returned. In that case, clear error code if the
1437 * connection was deliberately terminated at this end.
1438 */
1439 if (no_shell_flag && received_signal == SIGTERM) {
1440 received_signal = 0;
1441 exit_status = 0;
1442 }
1443
dtucker@openbsd.orgb1e72df2017-07-14 03:18:21 +00001444 if (received_signal) {
1445 verbose("Killed by signal %d.", (int) received_signal);
1446 cleanup_exit(0);
1447 }
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001448
1449 /*
1450 * In interactive mode (with pseudo tty) display a message indicating
1451 * that the connection has been closed.
1452 */
1453 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001454 if ((r = sshbuf_putf(stderr_buffer,
1455 "Connection to %.64s closed.\r\n", host)) != 0)
1456 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001457 }
1458
Damien Miller95def091999-11-25 00:26:21 +11001459 /* Output any buffered data for stderr. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001460 if (sshbuf_len(stderr_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001461 len = atomicio(vwrite, fileno(stderr),
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001462 (u_char *)sshbuf_ptr(stderr_buffer),
1463 sshbuf_len(stderr_buffer));
1464 if (len < 0 || (u_int)len != sshbuf_len(stderr_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001465 error("Write failed flushing stderr buffer.");
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001466 else if ((r = sshbuf_consume(stderr_buffer, len)) != 0)
1467 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11001468 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001469
Damien Miller95def091999-11-25 00:26:21 +11001470 /* Clear and free any buffers. */
jsg@openbsd.org458abc22016-01-23 05:31:35 +00001471 explicit_bzero(buf, sizeof(buf));
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001472 sshbuf_free(stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001473
Damien Miller95def091999-11-25 00:26:21 +11001474 /* Report bytes transferred, and transfer rates. */
dtucker@openbsd.org@openbsd.org2d638e92017-11-25 05:58:47 +00001475 total_time = monotime_double() - start_time;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001476 ssh_packet_get_bytes(ssh, &ibytes, &obytes);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001477 verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
Damien Miller821de0a2011-01-11 17:20:29 +11001478 (unsigned long long)obytes, (unsigned long long)ibytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001479 if (total_time > 0)
Damien Millerb61f3fc2008-07-11 17:36:48 +10001480 verbose("Bytes per second: sent %.1f, received %.1f",
1481 obytes / total_time, ibytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001482 /* Return the exit status of the program. */
1483 debug("Exit status %d", exit_status);
1484 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001485}
Damien Millerb38eff82000-04-01 11:09:21 +10001486
1487/*********/
1488
Ben Lindstrombba81212001-06-25 05:01:22 +00001489static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001490client_request_forwarded_tcpip(struct ssh *ssh, const char *request_type,
1491 int rchan, u_int rwindow, u_int rmaxpack)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001492{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001493 Channel *c = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00001494 struct sshbuf *b = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001495 char *listen_address, *originator_address;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001496 u_int listen_port, originator_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00001497 int r;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001498
1499 /* Get rest of the packet */
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001500 if ((r = sshpkt_get_cstring(ssh, &listen_address, NULL)) != 0 ||
1501 (r = sshpkt_get_u32(ssh, &listen_port)) != 0 ||
1502 (r = sshpkt_get_cstring(ssh, &originator_address, NULL)) != 0 ||
1503 (r = sshpkt_get_u32(ssh, &originator_port)) != 0 ||
1504 (r = sshpkt_get_end(ssh)) != 0)
1505 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller0bc1bd82000-11-13 22:57:25 +11001506
Damien Miller7acefbb2014-07-18 14:11:24 +10001507 debug("%s: listen %s port %d, originator %s port %d", __func__,
1508 listen_address, listen_port, originator_address, originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001509
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001510 if (listen_port > 0xffff)
1511 error("%s: invalid listen port", __func__);
1512 else if (originator_port > 0xffff)
1513 error("%s: invalid originator port", __func__);
1514 else {
1515 c = channel_connect_by_listen_address(ssh,
1516 listen_address, listen_port, "forwarded-tcpip",
1517 originator_address);
1518 }
Damien Millerbd740252008-05-19 15:37:09 +10001519
markus@openbsd.org8d057842016-09-30 09:19:13 +00001520 if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) {
1521 if ((b = sshbuf_new()) == NULL) {
1522 error("%s: alloc reply", __func__);
1523 goto out;
1524 }
1525 /* reconstruct and send to muxclient */
1526 if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
1527 (r = sshbuf_put_u8(b, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
1528 (r = sshbuf_put_cstring(b, request_type)) != 0 ||
1529 (r = sshbuf_put_u32(b, rchan)) != 0 ||
1530 (r = sshbuf_put_u32(b, rwindow)) != 0 ||
1531 (r = sshbuf_put_u32(b, rmaxpack)) != 0 ||
1532 (r = sshbuf_put_cstring(b, listen_address)) != 0 ||
1533 (r = sshbuf_put_u32(b, listen_port)) != 0 ||
1534 (r = sshbuf_put_cstring(b, originator_address)) != 0 ||
1535 (r = sshbuf_put_u32(b, originator_port)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001536 (r = sshbuf_put_stringb(c->output, b)) != 0) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00001537 error("%s: compose for muxclient %s", __func__,
1538 ssh_err(r));
1539 goto out;
1540 }
1541 }
1542
1543 out:
1544 sshbuf_free(b);
Darren Tuckera627d422013-06-02 07:31:17 +10001545 free(originator_address);
1546 free(listen_address);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001547 return c;
1548}
1549
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001550static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001551client_request_forwarded_streamlocal(struct ssh *ssh,
1552 const char *request_type, int rchan)
Damien Miller7acefbb2014-07-18 14:11:24 +10001553{
1554 Channel *c = NULL;
1555 char *listen_path;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001556 int r;
Damien Miller7acefbb2014-07-18 14:11:24 +10001557
1558 /* Get the remote path. */
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001559 if ((r = sshpkt_get_cstring(ssh, &listen_path, NULL)) != 0 ||
1560 (r = sshpkt_get_string(ssh, NULL, NULL)) != 0 || /* reserved */
1561 (r = sshpkt_get_end(ssh)) != 0)
1562 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7acefbb2014-07-18 14:11:24 +10001563
1564 debug("%s: %s", __func__, listen_path);
1565
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001566 c = channel_connect_by_listen_path(ssh, listen_path,
Damien Miller7acefbb2014-07-18 14:11:24 +10001567 "forwarded-streamlocal@openssh.com", "forwarded-streamlocal");
1568 free(listen_path);
1569 return c;
1570}
1571
1572static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001573client_request_x11(struct ssh *ssh, const char *request_type, int rchan)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001574{
1575 Channel *c = NULL;
1576 char *originator;
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00001577 u_int originator_port;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001578 int r, sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001579
1580 if (!options.forward_x11) {
1581 error("Warning: ssh server tried X11 forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001582 error("Warning: this is probably a break-in attempt by a "
1583 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001584 return NULL;
1585 }
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001586 if (x11_refuse_time != 0 && (u_int)monotime() >= x11_refuse_time) {
Damien Miller1ab6a512010-06-26 10:02:24 +10001587 verbose("Rejected X11 connection after ForwardX11Timeout "
1588 "expired");
1589 return NULL;
1590 }
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001591 if ((r = sshpkt_get_cstring(ssh, &originator, NULL)) != 0 ||
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00001592 (r = sshpkt_get_u32(ssh, &originator_port)) != 0 ||
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001593 (r = sshpkt_get_end(ssh)) != 0)
1594 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller0bc1bd82000-11-13 22:57:25 +11001595 /* XXX check permission */
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00001596 /* XXX range check originator port? */
1597 debug("client_request_x11: request from %s %u", originator,
Damien Millerd83ff352001-01-30 09:19:34 +11001598 originator_port);
Darren Tuckera627d422013-06-02 07:31:17 +10001599 free(originator);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001600 sock = x11_connect_display(ssh);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001601 if (sock < 0)
1602 return NULL;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001603 c = channel_new(ssh, "x11",
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001604 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001605 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001606 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001607 return c;
1608}
1609
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001610static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001611client_request_agent(struct ssh *ssh, const char *request_type, int rchan)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001612{
1613 Channel *c = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001614 int r, sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001615
1616 if (!options.forward_agent) {
1617 error("Warning: ssh server tried agent forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001618 error("Warning: this is probably a break-in attempt by a "
1619 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001620 return NULL;
1621 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001622 if ((r = ssh_get_authentication_socket(&sock)) != 0) {
1623 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1624 debug("%s: ssh_get_authentication_socket: %s",
1625 __func__, ssh_err(r));
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001626 return NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001627 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001628 c = channel_new(ssh, "authentication agent connection",
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001629 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001630 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001631 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001632 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001633 return c;
1634}
1635
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001636char *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001637client_request_tun_fwd(struct ssh *ssh, int tun_mode,
1638 int local_tun, int remote_tun)
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001639{
1640 Channel *c;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001641 int r, fd;
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001642 char *ifname = NULL;
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001643
1644 if (tun_mode == SSH_TUNMODE_NO)
1645 return 0;
1646
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001647 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1648
1649 /* Open local tunnel device */
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001650 if ((fd = tun_open(local_tun, tun_mode, &ifname)) == -1) {
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001651 error("Tunnel device open failed.");
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001652 return NULL;
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001653 }
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001654 debug("Tunnel forwarding using interface %s", ifname);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001655
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001656 c = channel_new(ssh, "tun", SSH_CHANNEL_OPENING, fd, fd, -1,
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001657 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1658 c->datagram = 1;
1659
1660#if defined(SSH_TUN_FILTER)
1661 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
Damien Miller871f1e42017-09-12 18:01:35 +10001662 channel_register_filter(ssh, c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +10001663 sys_tun_outfilter, NULL, NULL);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001664#endif
1665
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001666 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
1667 (r = sshpkt_put_cstring(ssh, "tun@openssh.com")) != 0 ||
1668 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
1669 (r = sshpkt_put_u32(ssh, c->local_window_max)) != 0 ||
1670 (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0 ||
1671 (r = sshpkt_put_u32(ssh, tun_mode)) != 0 ||
1672 (r = sshpkt_put_u32(ssh, remote_tun)) != 0 ||
1673 (r = sshpkt_send(ssh)) != 0)
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00001674 sshpkt_fatal(ssh, r, "%s: send reply", __func__);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001675
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001676 return ifname;
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001677}
1678
Damien Millerbd483e72000-04-30 10:00:53 +10001679/* XXXX move to generic input handler */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001680static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001681client_input_channel_open(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerbd483e72000-04-30 10:00:53 +10001682{
1683 Channel *c = NULL;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001684 char *ctype = NULL;
1685 int r;
1686 u_int rchan;
1687 size_t len;
1688 u_int rmaxpack, rwindow;
Damien Millerbd483e72000-04-30 10:00:53 +10001689
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001690 if ((r = sshpkt_get_cstring(ssh, &ctype, &len)) != 0 ||
1691 (r = sshpkt_get_u32(ssh, &rchan)) != 0 ||
1692 (r = sshpkt_get_u32(ssh, &rwindow)) != 0 ||
1693 (r = sshpkt_get_u32(ssh, &rmaxpack)) != 0)
1694 goto out;
Damien Millerbd483e72000-04-30 10:00:53 +10001695
Damien Millere247cc42000-05-07 12:03:14 +10001696 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001697 ctype, rchan, rwindow, rmaxpack);
1698
Damien Miller0bc1bd82000-11-13 22:57:25 +11001699 if (strcmp(ctype, "forwarded-tcpip") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001700 c = client_request_forwarded_tcpip(ssh, ctype, rchan, rwindow,
markus@openbsd.org8d057842016-09-30 09:19:13 +00001701 rmaxpack);
Damien Miller7acefbb2014-07-18 14:11:24 +10001702 } else if (strcmp(ctype, "forwarded-streamlocal@openssh.com") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001703 c = client_request_forwarded_streamlocal(ssh, ctype, rchan);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001704 } else if (strcmp(ctype, "x11") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001705 c = client_request_x11(ssh, ctype, rchan);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001706 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001707 c = client_request_agent(ssh, ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001708 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00001709 if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) {
1710 debug3("proxied to downstream: %s", ctype);
1711 } else if (c != NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10001712 debug("confirm %s", ctype);
1713 c->remote_id = rchan;
djm@openbsd.org9f532292017-09-12 06:35:31 +00001714 c->have_remote_id = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001715 c->remote_window = rwindow;
1716 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001717 if (c->type != SSH_CHANNEL_CONNECTING) {
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001718 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 ||
1719 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1720 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
1721 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
1722 (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0 ||
1723 (r = sshpkt_send(ssh)) != 0)
1724 sshpkt_fatal(ssh, r, "%s: send reply", __func__);
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001725 }
Damien Millerbd483e72000-04-30 10:00:53 +10001726 } else {
1727 debug("failure %s", ctype);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001728 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 ||
1729 (r = sshpkt_put_u32(ssh, rchan)) != 0 ||
1730 (r = sshpkt_put_u32(ssh, SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED)) != 0 ||
1731 (r = sshpkt_put_cstring(ssh, "open failed")) != 0 ||
1732 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
1733 (r = sshpkt_send(ssh)) != 0)
1734 sshpkt_fatal(ssh, r, "%s: send failure", __func__);
Damien Millerbd483e72000-04-30 10:00:53 +10001735 }
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001736 r = 0;
1737 out:
Darren Tuckera627d422013-06-02 07:31:17 +10001738 free(ctype);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001739 return r;
Damien Millerbd483e72000-04-30 10:00:53 +10001740}
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001741
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001742static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001743client_input_channel_req(int type, u_int32_t seq, struct ssh *ssh)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001744{
1745 Channel *c = NULL;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001746 char *rtype = NULL;
1747 u_char reply;
1748 u_int id, exitval;
1749 int r, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001750
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001751 if ((r = sshpkt_get_u32(ssh, &id)) != 0)
1752 return r;
1753 if (id <= INT_MAX)
1754 c = channel_lookup(ssh, id);
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001755 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00001756 return 0;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001757 if ((r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 ||
1758 (r = sshpkt_get_u8(ssh, &reply)) != 0)
1759 goto out;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001760
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001761 debug("client_input_channel_req: channel %u rtype %s reply %d",
Ben Lindstrom5b828322001-02-09 01:34:36 +00001762 id, rtype, reply);
1763
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001764 if (c == NULL) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001765 error("client_input_channel_req: channel %d: "
1766 "unknown channel", id);
Damien Millerbab9bd42008-05-19 16:06:47 +10001767 } else if (strcmp(rtype, "eow@openssh.com") == 0) {
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001768 if ((r = sshpkt_get_end(ssh)) != 0)
1769 goto out;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001770 chan_rcvd_eow(ssh, c);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001771 } else if (strcmp(rtype, "exit-status") == 0) {
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001772 if ((r = sshpkt_get_u32(ssh, &exitval)) != 0)
1773 goto out;
Damien Millere1537f92010-01-26 13:26:22 +11001774 if (c->ctl_chan != -1) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001775 mux_exit_message(ssh, c, exitval);
Damien Miller0e220db2004-06-15 10:34:08 +10001776 success = 1;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001777 } else if ((int)id == session_ident) {
Darren Tucker29440822010-01-08 17:08:35 +11001778 /* Record exit value of local session */
1779 success = 1;
1780 exit_status = exitval;
1781 } else {
Damien Miller36f57eb2010-01-30 17:28:34 +11001782 /* Probably for a mux channel that has already closed */
1783 debug("%s: no sink for exit-status on channel %d",
1784 __func__, id);
Damien Miller0e220db2004-06-15 10:34:08 +10001785 }
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001786 if ((r = sshpkt_get_end(ssh)) != 0)
1787 goto out;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001788 }
Damien Millerc5893782014-05-15 13:48:49 +10001789 if (reply && c != NULL && !(c->flags & CHAN_CLOSE_SENT)) {
djm@openbsd.org9f532292017-09-12 06:35:31 +00001790 if (!c->have_remote_id)
1791 fatal("%s: channel %d: no remote_id",
1792 __func__, c->self);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001793 if ((r = sshpkt_start(ssh, success ?
1794 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE)) != 0 ||
1795 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1796 (r = sshpkt_send(ssh)) != 0)
1797 sshpkt_fatal(ssh, r, "%s: send failure", __func__);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001798 }
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001799 r = 0;
1800 out:
Darren Tuckera627d422013-06-02 07:31:17 +10001801 free(rtype);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001802 return r;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001803}
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001804
djm@openbsd.org523463a2015-02-16 22:13:32 +00001805struct hostkeys_update_ctx {
1806 /* The hostname and (optionally) IP address string for the server */
1807 char *host_str, *ip_str;
1808
1809 /*
1810 * Keys received from the server and a flag for each indicating
1811 * whether they already exist in known_hosts.
1812 * keys_seen is filled in by hostkeys_find() and later (for new
1813 * keys) by client_global_hostkeys_private_confirm().
1814 */
1815 struct sshkey **keys;
1816 int *keys_seen;
djm@openbsd.org84623e02018-06-26 02:02:36 +00001817 size_t nkeys, nnew;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001818
1819 /*
1820 * Keys that are in known_hosts, but were not present in the update
1821 * from the server (i.e. scheduled to be deleted).
1822 * Filled in by hostkeys_find().
1823 */
1824 struct sshkey **old_keys;
1825 size_t nold;
1826};
1827
1828static void
1829hostkeys_update_ctx_free(struct hostkeys_update_ctx *ctx)
1830{
1831 size_t i;
1832
1833 if (ctx == NULL)
1834 return;
1835 for (i = 0; i < ctx->nkeys; i++)
1836 sshkey_free(ctx->keys[i]);
1837 free(ctx->keys);
1838 free(ctx->keys_seen);
1839 for (i = 0; i < ctx->nold; i++)
1840 sshkey_free(ctx->old_keys[i]);
1841 free(ctx->old_keys);
1842 free(ctx->host_str);
1843 free(ctx->ip_str);
1844 free(ctx);
1845}
1846
1847static int
1848hostkeys_find(struct hostkey_foreach_line *l, void *_ctx)
1849{
1850 struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
1851 size_t i;
1852 struct sshkey **tmp;
1853
djm@openbsd.org873d3e72017-04-30 23:18:44 +00001854 if (l->status != HKF_STATUS_MATCHED || l->key == NULL)
djm@openbsd.org523463a2015-02-16 22:13:32 +00001855 return 0;
1856
1857 /* Mark off keys we've already seen for this host */
1858 for (i = 0; i < ctx->nkeys; i++) {
1859 if (sshkey_equal(l->key, ctx->keys[i])) {
1860 debug3("%s: found %s key at %s:%ld", __func__,
1861 sshkey_ssh_name(ctx->keys[i]), l->path, l->linenum);
1862 ctx->keys_seen[i] = 1;
1863 return 0;
1864 }
1865 }
1866 /* This line contained a key that not offered by the server */
1867 debug3("%s: deprecated %s key at %s:%ld", __func__,
1868 sshkey_ssh_name(l->key), l->path, l->linenum);
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001869 if ((tmp = recallocarray(ctx->old_keys, ctx->nold, ctx->nold + 1,
djm@openbsd.org523463a2015-02-16 22:13:32 +00001870 sizeof(*ctx->old_keys))) == NULL)
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001871 fatal("%s: recallocarray failed nold = %zu",
djm@openbsd.org523463a2015-02-16 22:13:32 +00001872 __func__, ctx->nold);
1873 ctx->old_keys = tmp;
1874 ctx->old_keys[ctx->nold++] = l->key;
1875 l->key = NULL;
1876
1877 return 0;
1878}
1879
1880static void
1881update_known_hosts(struct hostkeys_update_ctx *ctx)
1882{
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00001883 int r, was_raw = 0;
1884 int loglevel = options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK ?
djm@openbsd.org523463a2015-02-16 22:13:32 +00001885 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_VERBOSE;
1886 char *fp, *response;
1887 size_t i;
1888
1889 for (i = 0; i < ctx->nkeys; i++) {
1890 if (ctx->keys_seen[i] != 2)
1891 continue;
1892 if ((fp = sshkey_fingerprint(ctx->keys[i],
1893 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1894 fatal("%s: sshkey_fingerprint failed", __func__);
1895 do_log2(loglevel, "Learned new hostkey: %s %s",
1896 sshkey_type(ctx->keys[i]), fp);
1897 free(fp);
1898 }
1899 for (i = 0; i < ctx->nold; i++) {
1900 if ((fp = sshkey_fingerprint(ctx->old_keys[i],
1901 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1902 fatal("%s: sshkey_fingerprint failed", __func__);
1903 do_log2(loglevel, "Deprecating obsolete hostkey: %s %s",
1904 sshkey_type(ctx->old_keys[i]), fp);
1905 free(fp);
1906 }
1907 if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK) {
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00001908 if (get_saved_tio() != NULL) {
1909 leave_raw_mode(1);
1910 was_raw = 1;
1911 }
djm@openbsd.org523463a2015-02-16 22:13:32 +00001912 response = NULL;
1913 for (i = 0; !quit_pending && i < 3; i++) {
1914 free(response);
1915 response = read_passphrase("Accept updated hostkeys? "
1916 "(yes/no): ", RP_ECHO);
1917 if (strcasecmp(response, "yes") == 0)
1918 break;
1919 else if (quit_pending || response == NULL ||
1920 strcasecmp(response, "no") == 0) {
1921 options.update_hostkeys = 0;
1922 break;
1923 } else {
1924 do_log2(loglevel, "Please enter "
1925 "\"yes\" or \"no\"");
1926 }
1927 }
1928 if (quit_pending || i >= 3 || response == NULL)
1929 options.update_hostkeys = 0;
1930 free(response);
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00001931 if (was_raw)
1932 enter_raw_mode(1);
djm@openbsd.org523463a2015-02-16 22:13:32 +00001933 }
1934
1935 /*
1936 * Now that all the keys are verified, we can go ahead and replace
1937 * them in known_hosts (assuming SSH_UPDATE_HOSTKEYS_ASK didn't
1938 * cancel the operation).
1939 */
1940 if (options.update_hostkeys != 0 &&
1941 (r = hostfile_replace_entries(options.user_hostfiles[0],
1942 ctx->host_str, ctx->ip_str, ctx->keys, ctx->nkeys,
1943 options.hash_known_hosts, 0,
1944 options.fingerprint_hash)) != 0)
1945 error("%s: hostfile_replace_entries failed: %s",
1946 __func__, ssh_err(r));
1947}
1948
1949static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001950client_global_hostkeys_private_confirm(struct ssh *ssh, int type,
1951 u_int32_t seq, void *_ctx)
djm@openbsd.org523463a2015-02-16 22:13:32 +00001952{
djm@openbsd.org523463a2015-02-16 22:13:32 +00001953 struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
1954 size_t i, ndone;
1955 struct sshbuf *signdata;
djm@openbsd.org78607312017-12-18 23:16:23 +00001956 int r, kexsigtype, use_kexsigtype;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001957 const u_char *sig;
1958 size_t siglen;
1959
1960 if (ctx->nnew == 0)
1961 fatal("%s: ctx->nnew == 0", __func__); /* sanity */
1962 if (type != SSH2_MSG_REQUEST_SUCCESS) {
1963 error("Server failed to confirm ownership of "
1964 "private host keys");
1965 hostkeys_update_ctx_free(ctx);
1966 return;
1967 }
djm@openbsd.org78607312017-12-18 23:16:23 +00001968 kexsigtype = sshkey_type_plain(
1969 sshkey_type_from_name(ssh->kex->hostkey_alg));
1970
djm@openbsd.org523463a2015-02-16 22:13:32 +00001971 if ((signdata = sshbuf_new()) == NULL)
1972 fatal("%s: sshbuf_new failed", __func__);
1973 /* Don't want to accidentally accept an unbound signature */
1974 if (ssh->kex->session_id_len == 0)
1975 fatal("%s: ssh->kex->session_id_len == 0", __func__);
1976 /*
1977 * Expect a signature for each of the ctx->nnew private keys we
1978 * haven't seen before. They will be in the same order as the
1979 * ctx->keys where the corresponding ctx->keys_seen[i] == 0.
1980 */
1981 for (ndone = i = 0; i < ctx->nkeys; i++) {
1982 if (ctx->keys_seen[i])
1983 continue;
1984 /* Prepare data to be signed: session ID, unique string, key */
1985 sshbuf_reset(signdata);
djm@openbsd.org44732de2015-02-20 22:17:21 +00001986 if ( (r = sshbuf_put_cstring(signdata,
1987 "hostkeys-prove-00@openssh.com")) != 0 ||
1988 (r = sshbuf_put_string(signdata, ssh->kex->session_id,
djm@openbsd.org523463a2015-02-16 22:13:32 +00001989 ssh->kex->session_id_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +00001990 (r = sshkey_puts(ctx->keys[i], signdata)) != 0)
1991 fatal("%s: failed to prepare signature: %s",
1992 __func__, ssh_err(r));
1993 /* Extract and verify signature */
1994 if ((r = sshpkt_get_string_direct(ssh, &sig, &siglen)) != 0) {
1995 error("%s: couldn't parse message: %s",
1996 __func__, ssh_err(r));
1997 goto out;
1998 }
djm@openbsd.org78607312017-12-18 23:16:23 +00001999 /*
2000 * For RSA keys, prefer to use the signature type negotiated
2001 * during KEX to the default (SHA1).
2002 */
2003 use_kexsigtype = kexsigtype == KEY_RSA &&
2004 sshkey_type_plain(ctx->keys[i]->type) == KEY_RSA;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002005 if ((r = sshkey_verify(ctx->keys[i], sig, siglen,
djm@openbsd.org04c7e282017-12-18 02:25:15 +00002006 sshbuf_ptr(signdata), sshbuf_len(signdata),
djm@openbsd.org78607312017-12-18 23:16:23 +00002007 use_kexsigtype ? ssh->kex->hostkey_alg : NULL, 0)) != 0) {
djm@openbsd.org523463a2015-02-16 22:13:32 +00002008 error("%s: server gave bad signature for %s key %zu",
2009 __func__, sshkey_type(ctx->keys[i]), i);
2010 goto out;
2011 }
2012 /* Key is good. Mark it as 'seen' */
2013 ctx->keys_seen[i] = 2;
2014 ndone++;
2015 }
2016 if (ndone != ctx->nnew)
2017 fatal("%s: ndone != ctx->nnew (%zu / %zu)", __func__,
2018 ndone, ctx->nnew); /* Shouldn't happen */
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002019 if ((r = sshpkt_get_end(ssh)) != 0) {
2020 error("%s: protocol error", __func__);
2021 goto out;
2022 }
djm@openbsd.org523463a2015-02-16 22:13:32 +00002023
2024 /* Make the edits to known_hosts */
2025 update_known_hosts(ctx);
2026 out:
2027 hostkeys_update_ctx_free(ctx);
2028}
2029
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002030/*
djm@openbsd.org894221a2017-03-10 05:01:13 +00002031 * Returns non-zero if the key is accepted by HostkeyAlgorithms.
2032 * Made slightly less trivial by the multiple RSA signature algorithm names.
2033 */
2034static int
2035key_accepted_by_hostkeyalgs(const struct sshkey *key)
2036{
2037 const char *ktype = sshkey_ssh_name(key);
2038 const char *hostkeyalgs = options.hostkeyalgorithms != NULL ?
2039 options.hostkeyalgorithms : KEX_DEFAULT_PK_ALG;
2040
2041 if (key == NULL || key->type == KEY_UNSPEC)
2042 return 0;
2043 if (key->type == KEY_RSA &&
2044 (match_pattern_list("rsa-sha2-256", hostkeyalgs, 0) == 1 ||
2045 match_pattern_list("rsa-sha2-512", hostkeyalgs, 0) == 1))
2046 return 1;
2047 return match_pattern_list(ktype, hostkeyalgs, 0) == 1;
2048}
2049
2050/*
djm@openbsd.org44732de2015-02-20 22:17:21 +00002051 * Handle hostkeys-00@openssh.com global request to inform the client of all
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002052 * the server's hostkeys. The keys are checked against the user's
2053 * HostkeyAlgorithms preference before they are accepted.
2054 */
2055static int
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00002056client_input_hostkeys(struct ssh *ssh)
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002057{
2058 const u_char *blob = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002059 size_t i, len = 0;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002060 struct sshbuf *buf = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002061 struct sshkey *key = NULL, **tmp;
2062 int r;
2063 char *fp;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002064 static int hostkeys_seen = 0; /* XXX use struct ssh */
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00002065 extern struct sockaddr_storage hostaddr; /* XXX from ssh.c */
djm@openbsd.orga63cfa22015-02-25 19:54:02 +00002066 struct hostkeys_update_ctx *ctx = NULL;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002067
2068 if (hostkeys_seen)
2069 fatal("%s: server already sent hostkeys", __func__);
djm@openbsd.org523463a2015-02-16 22:13:32 +00002070 if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK &&
2071 options.batch_mode)
2072 return 1; /* won't ask in batchmode, so don't even try */
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002073 if (!options.update_hostkeys || options.num_user_hostfiles <= 0)
2074 return 1;
djm@openbsd.orga63cfa22015-02-25 19:54:02 +00002075
2076 ctx = xcalloc(1, sizeof(*ctx));
djm@openbsd.org523463a2015-02-16 22:13:32 +00002077 while (ssh_packet_remaining(ssh) > 0) {
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002078 sshkey_free(key);
2079 key = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002080 if ((r = sshpkt_get_string_direct(ssh, &blob, &len)) != 0) {
2081 error("%s: couldn't parse message: %s",
2082 __func__, ssh_err(r));
2083 goto out;
2084 }
djm@openbsd.org44732de2015-02-20 22:17:21 +00002085 if ((r = sshkey_from_blob(blob, len, &key)) != 0) {
2086 error("%s: parse key: %s", __func__, ssh_err(r));
2087 goto out;
2088 }
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002089 fp = sshkey_fingerprint(key, options.fingerprint_hash,
2090 SSH_FP_DEFAULT);
2091 debug3("%s: received %s key %s", __func__,
2092 sshkey_type(key), fp);
2093 free(fp);
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00002094
djm@openbsd.org894221a2017-03-10 05:01:13 +00002095 if (!key_accepted_by_hostkeyalgs(key)) {
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002096 debug3("%s: %s key not permitted by HostkeyAlgorithms",
2097 __func__, sshkey_ssh_name(key));
2098 continue;
2099 }
djm@openbsd.org523463a2015-02-16 22:13:32 +00002100 /* Skip certs */
2101 if (sshkey_is_cert(key)) {
2102 debug3("%s: %s key is a certificate; skipping",
2103 __func__, sshkey_ssh_name(key));
2104 continue;
2105 }
2106 /* Ensure keys are unique */
2107 for (i = 0; i < ctx->nkeys; i++) {
2108 if (sshkey_equal(key, ctx->keys[i])) {
2109 error("%s: received duplicated %s host key",
2110 __func__, sshkey_ssh_name(key));
2111 goto out;
2112 }
2113 }
2114 /* Key is good, record it */
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00002115 if ((tmp = recallocarray(ctx->keys, ctx->nkeys, ctx->nkeys + 1,
djm@openbsd.org523463a2015-02-16 22:13:32 +00002116 sizeof(*ctx->keys))) == NULL)
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00002117 fatal("%s: recallocarray failed nkeys = %zu",
djm@openbsd.org523463a2015-02-16 22:13:32 +00002118 __func__, ctx->nkeys);
2119 ctx->keys = tmp;
2120 ctx->keys[ctx->nkeys++] = key;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002121 key = NULL;
2122 }
2123
djm@openbsd.org523463a2015-02-16 22:13:32 +00002124 if (ctx->nkeys == 0) {
djm@openbsd.org44732de2015-02-20 22:17:21 +00002125 debug("%s: server sent no hostkeys", __func__);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002126 goto out;
2127 }
djm@openbsd.org44732de2015-02-20 22:17:21 +00002128
djm@openbsd.org523463a2015-02-16 22:13:32 +00002129 if ((ctx->keys_seen = calloc(ctx->nkeys,
2130 sizeof(*ctx->keys_seen))) == NULL)
2131 fatal("%s: calloc failed", __func__);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002132
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00002133 get_hostfile_hostname_ipaddr(host,
2134 options.check_host_ip ? (struct sockaddr *)&hostaddr : NULL,
djm@openbsd.org523463a2015-02-16 22:13:32 +00002135 options.port, &ctx->host_str,
2136 options.check_host_ip ? &ctx->ip_str : NULL);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002137
djm@openbsd.org523463a2015-02-16 22:13:32 +00002138 /* Find which keys we already know about. */
2139 if ((r = hostkeys_foreach(options.user_hostfiles[0], hostkeys_find,
2140 ctx, ctx->host_str, ctx->ip_str,
2141 HKF_WANT_PARSE_KEY|HKF_WANT_MATCH)) != 0) {
2142 error("%s: hostkeys_foreach failed: %s", __func__, ssh_err(r));
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002143 goto out;
2144 }
2145
djm@openbsd.org523463a2015-02-16 22:13:32 +00002146 /* Figure out if we have any new keys to add */
2147 ctx->nnew = 0;
2148 for (i = 0; i < ctx->nkeys; i++) {
2149 if (!ctx->keys_seen[i])
2150 ctx->nnew++;
2151 }
2152
2153 debug3("%s: %zu keys from server: %zu new, %zu retained. %zu to remove",
2154 __func__, ctx->nkeys, ctx->nnew, ctx->nkeys - ctx->nnew, ctx->nold);
2155
2156 if (ctx->nnew == 0 && ctx->nold != 0) {
2157 /* We have some keys to remove. Just do it. */
2158 update_known_hosts(ctx);
2159 } else if (ctx->nnew != 0) {
2160 /*
2161 * We have received hitherto-unseen keys from the server.
2162 * Ask the server to confirm ownership of the private halves.
2163 */
2164 debug3("%s: asking server to prove ownership for %zu keys",
2165 __func__, ctx->nnew);
2166 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
2167 (r = sshpkt_put_cstring(ssh,
djm@openbsd.org44732de2015-02-20 22:17:21 +00002168 "hostkeys-prove-00@openssh.com")) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +00002169 (r = sshpkt_put_u8(ssh, 1)) != 0) /* bool: want reply */
2170 fatal("%s: cannot prepare packet: %s",
2171 __func__, ssh_err(r));
2172 if ((buf = sshbuf_new()) == NULL)
2173 fatal("%s: sshbuf_new", __func__);
2174 for (i = 0; i < ctx->nkeys; i++) {
2175 if (ctx->keys_seen[i])
2176 continue;
2177 sshbuf_reset(buf);
2178 if ((r = sshkey_putb(ctx->keys[i], buf)) != 0)
2179 fatal("%s: sshkey_putb: %s",
2180 __func__, ssh_err(r));
2181 if ((r = sshpkt_put_stringb(ssh, buf)) != 0)
2182 fatal("%s: sshpkt_put_string: %s",
2183 __func__, ssh_err(r));
2184 }
2185 if ((r = sshpkt_send(ssh)) != 0)
2186 fatal("%s: sshpkt_send: %s", __func__, ssh_err(r));
2187 client_register_global_confirm(
2188 client_global_hostkeys_private_confirm, ctx);
2189 ctx = NULL; /* will be freed in callback */
2190 }
2191
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002192 /* Success */
2193 out:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002194 hostkeys_update_ctx_free(ctx);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002195 sshkey_free(key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002196 sshbuf_free(buf);
djm@openbsd.org523463a2015-02-16 22:13:32 +00002197 /*
2198 * NB. Return success for all cases. The server doesn't need to know
2199 * what the client does with its hosts file.
2200 */
2201 return 1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002202}
2203
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002204static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002205client_input_global_request(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerc3fa4072002-01-22 23:21:58 +11002206{
2207 char *rtype;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002208 u_char want_reply;
2209 int r, success = 0;
Damien Millerc3fa4072002-01-22 23:21:58 +11002210
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002211 if ((r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 ||
2212 (r = sshpkt_get_u8(ssh, &want_reply)) != 0)
2213 goto out;
Damien Miller509b0102003-12-17 16:33:10 +11002214 debug("client_input_global_request: rtype %s want_reply %d",
2215 rtype, want_reply);
djm@openbsd.org44732de2015-02-20 22:17:21 +00002216 if (strcmp(rtype, "hostkeys-00@openssh.com") == 0)
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00002217 success = client_input_hostkeys(ssh);
Damien Millerc3fa4072002-01-22 23:21:58 +11002218 if (want_reply) {
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002219 if ((r = sshpkt_start(ssh, success ? SSH2_MSG_REQUEST_SUCCESS :
2220 SSH2_MSG_REQUEST_FAILURE)) != 0 ||
2221 (r = sshpkt_send(ssh)) != 0 ||
2222 (r = ssh_packet_write_wait(ssh)) != 0)
2223 goto out;
Damien Millerc3fa4072002-01-22 23:21:58 +11002224 }
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002225 r = 0;
2226 out:
Darren Tuckera627d422013-06-02 07:31:17 +10002227 free(rtype);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002228 return r;
Damien Millerc3fa4072002-01-22 23:21:58 +11002229}
Damien Millerbd483e72000-04-30 10:00:53 +10002230
Damien Miller0e220db2004-06-15 10:34:08 +10002231void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002232client_session2_setup(struct ssh *ssh, int id, int want_tty, int want_subsystem,
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002233 const char *term, struct termios *tiop, int in_fd, struct sshbuf *cmd,
2234 char **env)
Damien Miller0e220db2004-06-15 10:34:08 +10002235{
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002236 int i, j, matched, len, r;
djm@openbsd.org7082bb52018-06-09 03:01:12 +00002237 char *name, *val;
Darren Tucker5d78de62004-11-05 20:35:44 +11002238 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002239
2240 debug2("%s: id %d", __func__, id);
2241
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002242 if ((c = channel_lookup(ssh, id)) == NULL)
2243 fatal("%s: channel %d: unknown channel", __func__, id);
Darren Tucker5d78de62004-11-05 20:35:44 +11002244
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002245 ssh_packet_set_interactive(ssh, want_tty,
Damien Miller0dac6fb2010-11-20 15:19:38 +11002246 options.ip_qos_interactive, options.ip_qos_bulk);
2247
Damien Miller0e220db2004-06-15 10:34:08 +10002248 if (want_tty) {
2249 struct winsize ws;
Damien Miller0e220db2004-06-15 10:34:08 +10002250
2251 /* Store window size in the packet. */
2252 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
2253 memset(&ws, 0, sizeof(ws));
2254
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002255 channel_request_start(ssh, id, "pty-req", 1);
2256 client_expect_confirm(ssh, id, "PTY allocation", CONFIRM_TTY);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002257 if ((r = sshpkt_put_cstring(ssh, term != NULL ? term : ""))
2258 != 0 ||
2259 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_col)) != 0 ||
2260 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_row)) != 0 ||
2261 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_xpixel)) != 0 ||
2262 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_ypixel)) != 0)
2263 fatal("%s: %s", __func__, ssh_err(r));
Darren Tuckerdf189fb2008-06-08 12:55:32 +10002264 if (tiop == NULL)
2265 tiop = get_saved_tio();
markus@openbsd.org89dd6152018-07-09 21:20:26 +00002266 ssh_tty_make_modes(ssh, -1, tiop);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002267 if ((r = sshpkt_send(ssh)) != 0)
2268 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller0e220db2004-06-15 10:34:08 +10002269 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11002270 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10002271 }
2272
2273 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10002274 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10002275 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10002276 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10002277 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10002278 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10002279 if ((val = strchr(name, '=')) == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +10002280 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002281 continue;
2282 }
2283 *val++ = '\0';
2284
2285 matched = 0;
2286 for (j = 0; j < options.num_send_env; j++) {
2287 if (match_pattern(name, options.send_env[j])) {
2288 matched = 1;
2289 break;
2290 }
2291 }
2292 if (!matched) {
2293 debug3("Ignored env %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10002294 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002295 continue;
2296 }
2297
2298 debug("Sending env %s = %s", name, val);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002299 channel_request_start(ssh, id, "env", 0);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002300 if ((r = sshpkt_put_cstring(ssh, name)) != 0 ||
2301 (r = sshpkt_put_cstring(ssh, val)) != 0 ||
2302 (r = sshpkt_send(ssh)) != 0)
2303 fatal("%s: %s", __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +10002304 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002305 }
2306 }
djm@openbsd.org7082bb52018-06-09 03:01:12 +00002307 for (i = 0; i < options.num_setenv; i++) {
2308 /* Split */
2309 name = xstrdup(options.setenv[i]);
2310 if ((val = strchr(name, '=')) == NULL) {
2311 free(name);
2312 continue;
2313 }
2314 *val++ = '\0';
2315
2316 debug("Setting env %s = %s", name, val);
2317 channel_request_start(ssh, id, "env", 0);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002318 if ((r = sshpkt_put_cstring(ssh, name)) != 0 ||
2319 (r = sshpkt_put_cstring(ssh, val)) != 0 ||
2320 (r = sshpkt_send(ssh)) != 0)
2321 fatal("%s: %s", __func__, ssh_err(r));
djm@openbsd.org7082bb52018-06-09 03:01:12 +00002322 free(name);
2323 }
Damien Miller0e220db2004-06-15 10:34:08 +10002324
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002325 len = sshbuf_len(cmd);
Damien Miller0e220db2004-06-15 10:34:08 +10002326 if (len > 0) {
2327 if (len > 900)
2328 len = 900;
2329 if (want_subsystem) {
Damien Miller5771ed72008-05-19 15:35:33 +10002330 debug("Sending subsystem: %.*s",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002331 len, (const u_char*)sshbuf_ptr(cmd));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002332 channel_request_start(ssh, id, "subsystem", 1);
2333 client_expect_confirm(ssh, id, "subsystem",
2334 CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002335 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002336 debug("Sending command: %.*s",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002337 len, (const u_char*)sshbuf_ptr(cmd));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002338 channel_request_start(ssh, id, "exec", 1);
2339 client_expect_confirm(ssh, id, "exec", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002340 }
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002341 if ((r = sshpkt_put_stringb(ssh, cmd)) != 0 ||
2342 (r = sshpkt_send(ssh)) != 0)
2343 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller0e220db2004-06-15 10:34:08 +10002344 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002345 channel_request_start(ssh, id, "shell", 1);
2346 client_expect_confirm(ssh, id, "shell", CONFIRM_CLOSE);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002347 if ((r = sshpkt_send(ssh)) != 0)
2348 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller0e220db2004-06-15 10:34:08 +10002349 }
2350}
2351
Ben Lindstrombba81212001-06-25 05:01:22 +00002352static void
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00002353client_init_dispatch(struct ssh *ssh)
Damien Miller1383bd82000-04-06 12:32:37 +10002354{
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00002355 ssh_dispatch_init(ssh, &dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10002356
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00002357 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2358 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2359 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2360 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
2361 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
2362 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2363 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
2364 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
2365 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
2366 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2367 ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
2368 ssh_dispatch_set(ssh, SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002369
2370 /* rekeying */
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00002371 ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10002372
2373 /* global request reply messages */
djm@openbsd.orgbc5e1162019-01-19 21:39:12 +00002374 ssh_dispatch_set(ssh, SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2375 ssh_dispatch_set(ssh, SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10002376}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002377
Damien Miller6c3eec72011-05-05 14:16:22 +10002378void
2379client_stop_mux(void)
2380{
2381 if (options.control_path != NULL && muxserver_sock != -1)
2382 unlink(options.control_path);
2383 /*
Darren Tucker48bf4b02012-09-07 16:38:53 +10002384 * If we are in persist mode, or don't have a shell, signal that we
2385 * should close when all active channels are closed.
Damien Miller6c3eec72011-05-05 14:16:22 +10002386 */
Darren Tucker48bf4b02012-09-07 16:38:53 +10002387 if (options.control_persist || no_shell_flag) {
Damien Miller6c3eec72011-05-05 14:16:22 +10002388 session_closed = 1;
Damien Miller4ac99c32011-06-20 14:43:31 +10002389 setproctitle("[stopped mux]");
2390 }
Damien Miller6c3eec72011-05-05 14:16:22 +10002391}
2392
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002393/* client specific fatal cleanup */
2394void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002395cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002396{
Damien Miller21771e22011-05-15 08:45:50 +10002397 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002398 leave_non_blocking();
Damien Millerb1cbfa22008-05-19 16:00:08 +10002399 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10002400 unlink(options.control_path);
Damien Millera41ccca2010-10-07 22:07:32 +11002401 ssh_kill_proxy_command();
Darren Tucker3e33cec2003-10-02 16:12:36 +10002402 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002403}