blob: 5f87b24a2e362c51124747c44f82138034bc0cf6 [file] [log] [blame]
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001/* $OpenBSD: clientloop.c,v 1.320 2019/01/19 21:33:57 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
djm@openbsd.org0fa174e2019-01-19 21:31:32 +0000115#include "opacket.h" /* XXX */
116extern struct ssh *active_state; /* XXX */
117
Damien Miller69b69aa2000-10-28 14:19:58 +1100118/* import options */
119extern Options options;
120
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121/* Flag indicating that stdin should be redirected from /dev/null. */
122extern int stdin_null_flag;
123
Damien Millerd6965512003-12-17 16:31:53 +1100124/* Flag indicating that no shell has been requested */
125extern int no_shell_flag;
126
djm@openbsd.org368dd972016-07-23 02:54:08 +0000127/* Flag indicating that ssh should daemonise after authentication is complete */
128extern int fork_after_authentication_flag;
129
Damien Miller0e220db2004-06-15 10:34:08 +1000130/* Control socket */
Damien Millere1537f92010-01-26 13:26:22 +1100131extern int muxserver_sock; /* XXX use mux_client_cleanup() instead */
Damien Miller0e220db2004-06-15 10:34:08 +1000132
Damien Miller5428f641999-11-25 11:54:57 +1100133/*
134 * Name of the host we are connecting to. This is the name given on the
135 * command line, or the HostName specified for the user-supplied name in a
136 * configuration file.
137 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138extern char *host;
139
Damien Miller5428f641999-11-25 11:54:57 +1100140/*
141 * Flag to indicate that we have received a window change signal which has
142 * not yet been processed. This will cause a message indicating the new
143 * window size to be sent to the server a little later. This is volatile
144 * because this is updated in a signal handler.
145 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000146static volatile sig_atomic_t received_window_change_signal = 0;
147static volatile sig_atomic_t received_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148
Damien Miller788f2122005-11-05 15:14:59 +1100149/* Flag indicating whether the user's terminal is in non-blocking mode. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150static int in_non_blocking_mode = 0;
151
Damien Millere11e1ea2010-08-03 16:04:46 +1000152/* Time when backgrounded control master using ControlPersist should exit */
153static time_t control_persist_exit_time = 0;
154
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155/* Common data for the client loop code. */
Damien Millere1537f92010-01-26 13:26:22 +1100156volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
Damien Miller95def091999-11-25 00:26:21 +1100157static int last_was_cr; /* Last character was a newline. */
Darren Tucker4d5cd332008-06-13 04:51:14 +1000158static int exit_status; /* Used to store the command exit status. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000159static struct sshbuf *stderr_buffer; /* Used for final exit message. */
Damien Miller95def091999-11-25 00:26:21 +1100160static int connection_in; /* Connection to server (input). */
161static int connection_out; /* Connection to server (output). */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000162static int need_rekeying; /* Set to non-zero if rekeying is requested. */
Damien Miller1ab6a512010-06-26 10:02:24 +1000163static int session_closed; /* In SSH2: login session closed. */
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000164static u_int x11_refuse_time; /* If >0, refuse x11 opens after this time. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165
Ben Lindstrombba81212001-06-25 05:01:22 +0000166static void client_init_dispatch(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000167int session_ident = -1;
168
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000169/* Track escape per proto2 channel */
170struct escape_filter_ctx {
171 int escape_pending;
172 int escape_char;
173};
174
175/* Context for channel confirmation replies */
Damien Miller5771ed72008-05-19 15:35:33 +1000176struct channel_reply_ctx {
177 const char *request_type;
Damien Miller555f3b82011-05-15 08:48:05 +1000178 int id;
179 enum confirm_action action;
Damien Miller5771ed72008-05-19 15:35:33 +1000180};
181
Darren Tucker9f407c42008-06-13 04:50:27 +1000182/* Global request success/failure callbacks */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000183/* XXX move to struct ssh? */
Darren Tucker9f407c42008-06-13 04:50:27 +1000184struct global_confirm {
185 TAILQ_ENTRY(global_confirm) entry;
186 global_confirm_cb *cb;
187 void *ctx;
188 int ref_count;
189};
190TAILQ_HEAD(global_confirms, global_confirm);
191static struct global_confirms global_confirms =
192 TAILQ_HEAD_INITIALIZER(global_confirms);
193
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000194void ssh_process_session2_setup(int, int, int, struct sshbuf *);
Damien Miller0e220db2004-06-15 10:34:08 +1000195
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000196/* Restores stdin to blocking mode. */
197
Ben Lindstrombba81212001-06-25 05:01:22 +0000198static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000199leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200{
Damien Miller95def091999-11-25 00:26:21 +1100201 if (in_non_blocking_mode) {
Damien Miller03e66f62004-06-15 15:47:51 +1000202 unset_nonblock(fileno(stdin));
Damien Miller95def091999-11-25 00:26:21 +1100203 in_non_blocking_mode = 0;
Damien Miller95def091999-11-25 00:26:21 +1100204 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000205}
206
Damien Miller5428f641999-11-25 11:54:57 +1100207/*
208 * Signal handler for the window change signal (SIGWINCH). This just sets a
209 * flag indicating that the window has changed.
210 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100211/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000212static void
Damien Miller95def091999-11-25 00:26:21 +1100213window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214{
Damien Miller95def091999-11-25 00:26:21 +1100215 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000216}
217
Damien Miller5428f641999-11-25 11:54:57 +1100218/*
219 * Signal handler for signals that cause the program to terminate. These
220 * signals must be trapped to restore terminal modes.
221 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100222/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000223static void
Damien Miller95def091999-11-25 00:26:21 +1100224signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000226 received_signal = sig;
227 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228}
229
Damien Miller5428f641999-11-25 11:54:57 +1100230/*
Damien Millere11e1ea2010-08-03 16:04:46 +1000231 * Sets control_persist_exit_time to the absolute time when the
232 * backgrounded control master should exit due to expiry of the
233 * ControlPersist timeout. Sets it to 0 if we are not a backgrounded
234 * control master process, or if there is no ControlPersist timeout.
235 */
236static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000237set_control_persist_exit_time(struct ssh *ssh)
Damien Millere11e1ea2010-08-03 16:04:46 +1000238{
239 if (muxserver_sock == -1 || !options.control_persist
Damien Miller6c3eec72011-05-05 14:16:22 +1000240 || options.control_persist_timeout == 0) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000241 /* not using a ControlPersist timeout */
242 control_persist_exit_time = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000243 } else if (channel_still_open(ssh)) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000244 /* some client connections are still open */
245 if (control_persist_exit_time > 0)
246 debug2("%s: cancel scheduled exit", __func__);
247 control_persist_exit_time = 0;
248 } else if (control_persist_exit_time <= 0) {
249 /* a client connection has recently closed */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000250 control_persist_exit_time = monotime() +
Damien Millere11e1ea2010-08-03 16:04:46 +1000251 (time_t)options.control_persist_timeout;
252 debug2("%s: schedule exit in %d seconds", __func__,
253 options.control_persist_timeout);
254 }
255 /* else we are already counting down to the timeout */
256}
257
Damien Miller8d60be52012-02-11 08:18:17 +1100258#define SSH_X11_VALID_DISPLAY_CHARS ":/.-_"
259static int
260client_x11_display_valid(const char *display)
261{
262 size_t i, dlen;
263
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000264 if (display == NULL)
265 return 0;
266
Damien Miller8d60be52012-02-11 08:18:17 +1100267 dlen = strlen(display);
268 for (i = 0; i < dlen; i++) {
Damien Millerfdb23062013-11-21 13:57:15 +1100269 if (!isalnum((u_char)display[i]) &&
Damien Miller8d60be52012-02-11 08:18:17 +1100270 strchr(SSH_X11_VALID_DISPLAY_CHARS, display[i]) == NULL) {
271 debug("Invalid character '%c' in DISPLAY", display[i]);
272 return 0;
273 }
274 }
275 return 1;
276}
277
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000278#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
279#define X11_TIMEOUT_SLACK 60
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000280int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000281client_x11_get_proto(struct ssh *ssh, const char *display,
282 const char *xauth_path, u_int trusted, u_int timeout,
283 char **_proto, char **_data)
Damien Miller17e7ed02005-06-17 12:54:33 +1000284{
djm@openbsd.orgb5e412a2018-09-21 12:46:22 +0000285 char *cmd, line[512], xdisplay[512];
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000286 char xauthfile[PATH_MAX], xauthdir[PATH_MAX];
Damien Miller17e7ed02005-06-17 12:54:33 +1000287 static char proto[512], data[512];
288 FILE *f;
tedu@openbsd.org10363562016-09-17 18:00:27 +0000289 int got_data = 0, generated = 0, do_unlink = 0, r;
Damien Miller17e7ed02005-06-17 12:54:33 +1000290 struct stat st;
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000291 u_int now, x11_timeout_real;
Damien Miller17e7ed02005-06-17 12:54:33 +1000292
Damien Miller17e7ed02005-06-17 12:54:33 +1000293 *_proto = proto;
294 *_data = data;
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000295 proto[0] = data[0] = xauthfile[0] = xauthdir[0] = '\0';
Damien Miller17e7ed02005-06-17 12:54:33 +1000296
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000297 if (!client_x11_display_valid(display)) {
millert@openbsd.org5658ef22016-02-01 21:18:17 +0000298 if (display != NULL)
299 logit("DISPLAY \"%s\" invalid; disabling X11 forwarding",
300 display);
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000301 return -1;
302 }
303 if (xauth_path != NULL && stat(xauth_path, &st) == -1) {
304 debug("No xauth program.");
305 xauth_path = NULL;
306 }
307
308 if (xauth_path != NULL) {
Damien Miller17e7ed02005-06-17 12:54:33 +1000309 /*
310 * Handle FamilyLocal case where $DISPLAY does
311 * not match an authorization entry. For this we
312 * just try "xauth list unix:displaynum.screennum".
313 * XXX: "localhost" match to determine FamilyLocal
314 * is not perfect.
315 */
316 if (strncmp(display, "localhost:", 10) == 0) {
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000317 if ((r = snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
318 display + 10)) < 0 ||
319 (size_t)r >= sizeof(xdisplay)) {
320 error("%s: display name too long", __func__);
321 return -1;
322 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000323 display = xdisplay;
324 }
325 if (trusted == 0) {
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000326 /*
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000327 * Generate an untrusted X11 auth cookie.
328 *
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000329 * The authentication cookie should briefly outlive
330 * ssh's willingness to forward X11 connections to
331 * avoid nasty fail-open behaviour in the X server.
332 */
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000333 mktemp_proto(xauthdir, sizeof(xauthdir));
334 if (mkdtemp(xauthdir) == NULL) {
335 error("%s: mkdtemp: %s",
336 __func__, strerror(errno));
337 return -1;
338 }
339 do_unlink = 1;
340 if ((r = snprintf(xauthfile, sizeof(xauthfile),
341 "%s/xauthfile", xauthdir)) < 0 ||
342 (size_t)r >= sizeof(xauthfile)) {
343 error("%s: xauthfile path too long", __func__);
344 unlink(xauthfile);
345 rmdir(xauthdir);
346 return -1;
347 }
348
djm@openbsd.orgb5e412a2018-09-21 12:46:22 +0000349 if (timeout == 0) {
350 /* auth doesn't time out */
351 xasprintf(&cmd, "%s -f %s generate %s %s "
352 "untrusted 2>%s",
353 xauth_path, xauthfile, display,
354 SSH_X11_PROTO, _PATH_DEVNULL);
355 } else {
356 /* Add some slack to requested expiry */
357 if (timeout < UINT_MAX - X11_TIMEOUT_SLACK)
358 x11_timeout_real = timeout +
359 X11_TIMEOUT_SLACK;
360 else {
361 /* Don't overflow on long timeouts */
362 x11_timeout_real = UINT_MAX;
363 }
364 xasprintf(&cmd, "%s -f %s generate %s %s "
365 "untrusted timeout %u 2>%s",
366 xauth_path, xauthfile, display,
367 SSH_X11_PROTO, x11_timeout_real,
368 _PATH_DEVNULL);
369 }
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000370 debug2("%s: %s", __func__, cmd);
djm@openbsd.orgb5e412a2018-09-21 12:46:22 +0000371
372 if (timeout != 0 && x11_refuse_time == 0) {
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000373 now = monotime() + 1;
374 if (UINT_MAX - timeout < now)
375 x11_refuse_time = UINT_MAX;
376 else
377 x11_refuse_time = now + timeout;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000378 channel_set_x11_refuse_time(ssh,
379 x11_refuse_time);
Damien Miller17e7ed02005-06-17 12:54:33 +1000380 }
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000381 if (system(cmd) == 0)
382 generated = 1;
djm@openbsd.orgb5e412a2018-09-21 12:46:22 +0000383 free(cmd);
Damien Miller17e7ed02005-06-17 12:54:33 +1000384 }
Darren Tucker513d13a2007-08-15 19:13:41 +1000385
386 /*
387 * When in untrusted mode, we read the cookie only if it was
388 * successfully generated as an untrusted one in the step
389 * above.
390 */
391 if (trusted || generated) {
djm@openbsd.orgb5e412a2018-09-21 12:46:22 +0000392 xasprintf(&cmd,
Darren Tucker513d13a2007-08-15 19:13:41 +1000393 "%s %s%s list %s 2>" _PATH_DEVNULL,
394 xauth_path,
395 generated ? "-f " : "" ,
396 generated ? xauthfile : "",
397 display);
398 debug2("x11_get_proto: %s", cmd);
399 f = popen(cmd, "r");
400 if (f && fgets(line, sizeof(line), f) &&
401 sscanf(line, "%*s %511s %511s", proto, data) == 2)
402 got_data = 1;
403 if (f)
404 pclose(f);
djm@openbsd.orgb5e412a2018-09-21 12:46:22 +0000405 free(cmd);
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000406 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000407 }
408
409 if (do_unlink) {
410 unlink(xauthfile);
411 rmdir(xauthdir);
412 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000413
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000414 /* Don't fall back to fake X11 data for untrusted forwarding */
415 if (!trusted && !got_data) {
416 error("Warning: untrusted X11 forwarding setup failed: "
417 "xauth key data not generated");
418 return -1;
419 }
420
Damien Miller17e7ed02005-06-17 12:54:33 +1000421 /*
422 * If we didn't get authentication data, just make up some
423 * data. The forwarding code will check the validity of the
424 * response anyway, and substitute this data. The X11
425 * server, however, will ignore this fake data and use
426 * whatever authentication mechanisms it was using otherwise
427 * for the local connection.
428 */
429 if (!got_data) {
tedu@openbsd.org10363562016-09-17 18:00:27 +0000430 u_int8_t rnd[16];
431 u_int i;
Damien Miller17e7ed02005-06-17 12:54:33 +1000432
433 logit("Warning: No xauth data; "
434 "using fake authentication data for X11 forwarding.");
435 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
tedu@openbsd.org10363562016-09-17 18:00:27 +0000436 arc4random_buf(rnd, sizeof(rnd));
437 for (i = 0; i < sizeof(rnd); i++) {
Damien Miller17e7ed02005-06-17 12:54:33 +1000438 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
tedu@openbsd.org10363562016-09-17 18:00:27 +0000439 rnd[i]);
Damien Miller17e7ed02005-06-17 12:54:33 +1000440 }
441 }
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000442
443 return 0;
Damien Miller17e7ed02005-06-17 12:54:33 +1000444}
445
Damien Miller5428f641999-11-25 11:54:57 +1100446/*
Damien Miller5428f641999-11-25 11:54:57 +1100447 * Checks if the client window has changed, and sends a packet about it to
448 * the server if so. The actual change is detected elsewhere (by a software
449 * interrupt on Unix); this just checks the flag and sends a message if
450 * appropriate.
451 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000452
Ben Lindstrombba81212001-06-25 05:01:22 +0000453static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000454client_check_window_change(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000455{
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000456 if (!received_window_change_signal)
Damien Miller1383bd82000-04-06 12:32:37 +1000457 return;
458 /** XXX race */
459 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000461 debug2("%s: changed", __func__);
Damien Miller1383bd82000-04-06 12:32:37 +1000462
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000463 channel_send_window_changes(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464}
465
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000466static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000467client_global_request_reply(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller509b0102003-12-17 16:33:10 +1100468{
Darren Tucker9f407c42008-06-13 04:50:27 +1000469 struct global_confirm *gc;
470
471 if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000472 return 0;
Darren Tucker9f407c42008-06-13 04:50:27 +1000473 if (gc->cb != NULL)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000474 gc->cb(ssh, type, seq, gc->ctx);
Darren Tucker9f407c42008-06-13 04:50:27 +1000475 if (--gc->ref_count <= 0) {
476 TAILQ_REMOVE(&global_confirms, gc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +1100477 explicit_bzero(gc, sizeof(*gc));
Darren Tuckera627d422013-06-02 07:31:17 +1000478 free(gc);
Darren Tucker9f407c42008-06-13 04:50:27 +1000479 }
480
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000481 ssh_packet_set_alive_timeouts(ssh, 0);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000482 return 0;
Damien Miller509b0102003-12-17 16:33:10 +1100483}
484
485static void
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000486server_alive_check(struct ssh *ssh)
Damien Miller509b0102003-12-17 16:33:10 +1100487{
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000488 int r;
489
490 if (ssh_packet_inc_alive_timeouts(ssh) > options.server_alive_count_max) {
Damien Millerb73b6fd2011-01-11 17:18:56 +1100491 logit("Timeout, server %s not responding.", host);
Damien Miller985a4482006-10-24 03:02:41 +1000492 cleanup_exit(255);
493 }
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000494 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
495 (r = sshpkt_put_cstring(ssh, "keepalive@openssh.com")) != 0 ||
496 (r = sshpkt_put_u8(ssh, 1)) != 0 || /* boolean: want reply */
497 (r = sshpkt_send(ssh)) != 0)
498 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker9f407c42008-06-13 04:50:27 +1000499 /* Insert an empty placeholder to maintain ordering */
500 client_register_global_confirm(NULL, NULL);
Damien Miller509b0102003-12-17 16:33:10 +1100501}
502
Damien Miller5428f641999-11-25 11:54:57 +1100503/*
504 * Waits until the client can do something (some data becomes available on
505 * one of the file descriptors).
506 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000507static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000508client_wait_until_can_do_something(struct ssh *ssh,
509 fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000510 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000511{
Damien Miller509b0102003-12-17 16:33:10 +1100512 struct timeval tv, *tvp;
Damien Millere11e1ea2010-08-03 16:04:46 +1000513 int timeout_secs;
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000514 time_t minwait_secs = 0, server_alive_time = 0, now = monotime();
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000515 int r, ret;
Damien Miller509b0102003-12-17 16:33:10 +1100516
Damien Miller5e953212001-01-30 09:14:00 +1100517 /* Add any selections by the channel mechanism. */
djm@openbsd.org71e5a532017-08-30 03:59:08 +0000518 channel_prepare_select(active_state, readsetp, writesetp, maxfdp,
519 nallocp, &minwait_secs);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000520
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000521 /* channel_prepare_select could have closed the last channel */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000522 if (session_closed && !channel_still_open(ssh) &&
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000523 !ssh_packet_have_data_to_write(ssh)) {
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000524 /* clear mask since we did not call select() */
525 memset(*readsetp, 0, *nallocp);
526 memset(*writesetp, 0, *nallocp);
527 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000528 }
529
djm@openbsd.org05164352017-04-30 23:21:54 +0000530 FD_SET(connection_in, *readsetp);
531
Damien Miller95def091999-11-25 00:26:21 +1100532 /* Select server connection if have data to write to the server. */
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000533 if (ssh_packet_have_data_to_write(ssh))
Damien Miller5e953212001-01-30 09:14:00 +1100534 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100535
Damien Miller5428f641999-11-25 11:54:57 +1100536 /*
537 * Wait for something to happen. This will suspend the process until
538 * some selected descriptor can be read, written, or has some other
Damien Millere11e1ea2010-08-03 16:04:46 +1000539 * event pending, or a timeout expires.
Damien Miller5428f641999-11-25 11:54:57 +1100540 */
Damien Miller95def091999-11-25 00:26:21 +1100541
Damien Millere11e1ea2010-08-03 16:04:46 +1000542 timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000543 if (options.server_alive_interval > 0) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000544 timeout_secs = options.server_alive_interval;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000545 server_alive_time = now + options.server_alive_interval;
546 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000547 if (options.rekey_interval > 0 && !rekeying)
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000548 timeout_secs = MINIMUM(timeout_secs,
549 ssh_packet_get_rekey_timeout(ssh));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000550 set_control_persist_exit_time(ssh);
Damien Millere11e1ea2010-08-03 16:04:46 +1000551 if (control_persist_exit_time > 0) {
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000552 timeout_secs = MINIMUM(timeout_secs,
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000553 control_persist_exit_time - now);
Damien Millere11e1ea2010-08-03 16:04:46 +1000554 if (timeout_secs < 0)
555 timeout_secs = 0;
556 }
Damien Millera6508752012-04-22 11:21:10 +1000557 if (minwait_secs != 0)
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000558 timeout_secs = MINIMUM(timeout_secs, (int)minwait_secs);
Damien Millere11e1ea2010-08-03 16:04:46 +1000559 if (timeout_secs == INT_MAX)
Damien Miller509b0102003-12-17 16:33:10 +1100560 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000561 else {
Damien Millere11e1ea2010-08-03 16:04:46 +1000562 tv.tv_sec = timeout_secs;
Damien Miller509b0102003-12-17 16:33:10 +1100563 tv.tv_usec = 0;
564 tvp = &tv;
565 }
Damien Millere11e1ea2010-08-03 16:04:46 +1000566
Damien Miller509b0102003-12-17 16:33:10 +1100567 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
568 if (ret < 0) {
Ben Lindstromf9452512001-02-15 03:12:08 +0000569 /*
570 * We have to clear the select masks, because we return.
571 * We have to return, because the mainloop checks for the flags
572 * set by the signal handlers.
573 */
Damien Miller79faeff2001-11-12 11:06:32 +1100574 memset(*readsetp, 0, *nallocp);
575 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000576
Damien Miller95def091999-11-25 00:26:21 +1100577 if (errno == EINTR)
578 return;
579 /* Note: we might still have data in the buffers. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000580 if ((r = sshbuf_putf(stderr_buffer,
581 "select: %s\r\n", strerror(errno))) != 0)
582 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +1100583 quit_pending = 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000584 } else if (ret == 0) {
585 /*
586 * Timeout. Could have been either keepalive or rekeying.
587 * Keepalive we check here, rekeying is checked in clientloop.
588 */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000589 if (server_alive_time != 0 && server_alive_time <= monotime())
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000590 server_alive_check(ssh);
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000591 }
592
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000593}
594
Ben Lindstrombba81212001-06-25 05:01:22 +0000595static void
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000596client_suspend_self(struct sshbuf *bin, struct sshbuf *bout, struct sshbuf *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000597{
Damien Miller95def091999-11-25 00:26:21 +1100598 /* Flush stdout and stderr buffers. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000599 if (sshbuf_len(bout) > 0)
600 atomicio(vwrite, fileno(stdout), sshbuf_mutable_ptr(bout),
601 sshbuf_len(bout));
602 if (sshbuf_len(berr) > 0)
603 atomicio(vwrite, fileno(stderr), sshbuf_mutable_ptr(berr),
604 sshbuf_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000605
Damien Miller21771e22011-05-15 08:45:50 +1000606 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000607
djm@openbsd.org5b8da1f2017-09-19 04:24:22 +0000608 sshbuf_reset(bin);
609 sshbuf_reset(bout);
610 sshbuf_reset(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000611
Damien Miller95def091999-11-25 00:26:21 +1100612 /* Send the suspend signal to the program itself. */
613 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000614
Darren Tucker5d78de62004-11-05 20:35:44 +1100615 /* Reset window sizes in case they have changed */
616 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000617
Damien Miller21771e22011-05-15 08:45:50 +1000618 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000619}
620
Ben Lindstrombba81212001-06-25 05:01:22 +0000621static void
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000622client_process_net_input(struct ssh *ssh, fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000623{
Darren Tucker86e30a02009-08-28 11:21:06 +1000624 char buf[SSH_IOBUFSZ];
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000625 int r, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000626
Damien Miller5428f641999-11-25 11:54:57 +1100627 /*
628 * Read input from the server, and add any such data to the buffer of
629 * the packet subsystem.
630 */
Damien Miller95def091999-11-25 00:26:21 +1100631 if (FD_ISSET(connection_in, readset)) {
632 /* Read as much as possible. */
markus@openbsd.orga3068632016-01-14 16:17:39 +0000633 len = read(connection_in, buf, sizeof(buf));
634 if (len == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000635 /*
636 * Received EOF. The remote host has closed the
637 * connection.
638 */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000639 if ((r = sshbuf_putf(stderr_buffer,
Darren Tucker4d5cd332008-06-13 04:51:14 +1000640 "Connection to %.300s closed by remote host.\r\n",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000641 host)) != 0)
642 fatal("%s: buffer error: %s",
643 __func__, ssh_err(r));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000644 quit_pending = 1;
645 return;
Damien Miller95def091999-11-25 00:26:21 +1100646 }
Damien Miller5428f641999-11-25 11:54:57 +1100647 /*
648 * There is a kernel bug on Solaris that causes select to
649 * sometimes wake up even though there is no data available.
650 */
Damien Millerd8968ad2008-07-04 23:10:49 +1000651 if (len < 0 &&
652 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Damien Miller95def091999-11-25 00:26:21 +1100653 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000654
Damien Miller95def091999-11-25 00:26:21 +1100655 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000656 /*
657 * An error has encountered. Perhaps there is a
658 * network problem.
659 */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000660 if ((r = sshbuf_putf(stderr_buffer,
Darren Tucker4d5cd332008-06-13 04:51:14 +1000661 "Read from remote host %.300s: %.100s\r\n",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000662 host, strerror(errno))) != 0)
663 fatal("%s: buffer error: %s",
664 __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +1100665 quit_pending = 1;
666 return;
667 }
djm@openbsd.org23f22a42019-01-19 21:33:57 +0000668 ssh_packet_process_incoming(ssh, buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100669 }
Damien Miller1383bd82000-04-06 12:32:37 +1000670}
671
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000672static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000673client_status_confirm(struct ssh *ssh, int type, Channel *c, void *ctx)
Damien Miller0e220db2004-06-15 10:34:08 +1000674{
Damien Miller5771ed72008-05-19 15:35:33 +1000675 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
676 char errmsg[256];
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000677 int r, tochan;
Darren Tuckerfc959702004-07-17 16:12:08 +1000678
Damien Miller555f3b82011-05-15 08:48:05 +1000679 /*
680 * If a TTY was explicitly requested, then a failure to allocate
681 * one is fatal.
682 */
683 if (cr->action == CONFIRM_TTY &&
684 (options.request_tty == REQUEST_TTY_FORCE ||
685 options.request_tty == REQUEST_TTY_YES))
686 cr->action = CONFIRM_CLOSE;
687
djm@openbsd.org001aa552018-04-10 00:10:49 +0000688 /* XXX suppress on mux _client_ quietmode */
Damien Miller5771ed72008-05-19 15:35:33 +1000689 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
Damien Millere1537f92010-01-26 13:26:22 +1100690 c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
Damien Miller0e220db2004-06-15 10:34:08 +1000691
Damien Miller5771ed72008-05-19 15:35:33 +1000692 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
693 debug2("%s request accepted on channel %d",
694 cr->request_type, c->self);
695 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
696 if (tochan) {
697 snprintf(errmsg, sizeof(errmsg),
698 "%s request failed\r\n", cr->request_type);
699 } else {
700 snprintf(errmsg, sizeof(errmsg),
701 "%s request failed on channel %d",
702 cr->request_type, c->self);
703 }
704 /* If error occurred on primary session channel, then exit */
Damien Miller555f3b82011-05-15 08:48:05 +1000705 if (cr->action == CONFIRM_CLOSE && c->self == session_ident)
Damien Miller5771ed72008-05-19 15:35:33 +1000706 fatal("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000707 /*
708 * If error occurred on mux client, append to
709 * their stderr.
710 */
711 if (tochan) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000712 if ((r = sshbuf_put(c->extended, errmsg,
713 strlen(errmsg))) != 0)
714 fatal("%s: buffer error %s", __func__,
715 ssh_err(r));
Damien Miller555f3b82011-05-15 08:48:05 +1000716 } else
Damien Miller5771ed72008-05-19 15:35:33 +1000717 error("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000718 if (cr->action == CONFIRM_TTY) {
719 /*
720 * If a TTY allocation error occurred, then arrange
721 * for the correct TTY to leave raw mode.
722 */
723 if (c->self == session_ident)
724 leave_raw_mode(0);
725 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000726 mux_tty_alloc_failed(ssh, c);
Damien Miller555f3b82011-05-15 08:48:05 +1000727 } else if (cr->action == CONFIRM_CLOSE) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000728 chan_read_failed(ssh, c);
729 chan_write_failed(ssh, c);
Damien Miller5771ed72008-05-19 15:35:33 +1000730 }
Damien Miller0e220db2004-06-15 10:34:08 +1000731 }
Darren Tuckera627d422013-06-02 07:31:17 +1000732 free(cr);
Damien Miller5771ed72008-05-19 15:35:33 +1000733}
Damien Miller0e220db2004-06-15 10:34:08 +1000734
Damien Miller5771ed72008-05-19 15:35:33 +1000735static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000736client_abandon_status_confirm(struct ssh *ssh, Channel *c, void *ctx)
Damien Miller5771ed72008-05-19 15:35:33 +1000737{
Darren Tuckera627d422013-06-02 07:31:17 +1000738 free(ctx);
Damien Miller5771ed72008-05-19 15:35:33 +1000739}
740
Damien Miller6d7b4372011-06-23 08:31:57 +1000741void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000742client_expect_confirm(struct ssh *ssh, int id, const char *request,
Damien Miller555f3b82011-05-15 08:48:05 +1000743 enum confirm_action action)
Damien Miller5771ed72008-05-19 15:35:33 +1000744{
Damien Miller6c81fee2013-11-08 12:19:55 +1100745 struct channel_reply_ctx *cr = xcalloc(1, sizeof(*cr));
Damien Miller5771ed72008-05-19 15:35:33 +1000746
747 cr->request_type = request;
Damien Miller555f3b82011-05-15 08:48:05 +1000748 cr->action = action;
Damien Miller5771ed72008-05-19 15:35:33 +1000749
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000750 channel_register_status_confirm(ssh, id, client_status_confirm,
Damien Miller5771ed72008-05-19 15:35:33 +1000751 client_abandon_status_confirm, cr);
Damien Miller0e220db2004-06-15 10:34:08 +1000752}
753
Darren Tucker9f407c42008-06-13 04:50:27 +1000754void
755client_register_global_confirm(global_confirm_cb *cb, void *ctx)
756{
Damien Millerb9d3bee2008-07-16 22:40:52 +1000757 struct global_confirm *gc, *last_gc;
Darren Tucker9f407c42008-06-13 04:50:27 +1000758
759 /* Coalesce identical callbacks */
Damien Millerb9d3bee2008-07-16 22:40:52 +1000760 last_gc = TAILQ_LAST(&global_confirms, global_confirms);
761 if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
762 if (++last_gc->ref_count >= INT_MAX)
763 fatal("%s: last_gc->ref_count = %d",
764 __func__, last_gc->ref_count);
Darren Tucker9f407c42008-06-13 04:50:27 +1000765 return;
766 }
767
Damien Miller6c81fee2013-11-08 12:19:55 +1100768 gc = xcalloc(1, sizeof(*gc));
Darren Tucker9f407c42008-06-13 04:50:27 +1000769 gc->cb = cb;
770 gc->ctx = ctx;
771 gc->ref_count = 1;
772 TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
773}
774
Damien Miller0e220db2004-06-15 10:34:08 +1000775static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000776process_cmdline(struct ssh *ssh)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000777{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000778 void (*handler)(int);
Damien Miller7acefbb2014-07-18 14:11:24 +1000779 char *s, *cmd;
780 int ok, delete = 0, local = 0, remote = 0, dynamic = 0;
781 struct Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000782
Damien Miller1d2c4562014-02-04 11:18:20 +1100783 memset(&fwd, 0, sizeof(fwd));
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100784
Damien Miller21771e22011-05-15 08:45:50 +1000785 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000786 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000787 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000788 if (s == NULL)
789 goto out;
Damien Millerfdb23062013-11-21 13:57:15 +1100790 while (isspace((u_char)*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000791 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000792 if (*s == '-')
793 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000794 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000795 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000796
Darren Tucker1973c882004-05-24 10:34:36 +1000797 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000798 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000799 logit(" -L[bind_address:]port:host:hostport "
800 "Request local forward");
801 logit(" -R[bind_address:]port:host:hostport "
802 "Request remote forward");
Damien Miller0164cb82008-11-05 16:30:31 +1100803 logit(" -D[bind_address:]port "
804 "Request dynamic forward");
Damien Millerff773642011-09-22 21:39:48 +1000805 logit(" -KL[bind_address:]port "
806 "Cancel local forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000807 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000808 "Cancel remote forward");
Damien Millerff773642011-09-22 21:39:48 +1000809 logit(" -KD[bind_address:]port "
810 "Cancel dynamic forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100811 if (!options.permit_local_command)
812 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000813 logit(" !args "
814 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100815 goto out;
816 }
817
818 if (*s == '!' && options.permit_local_command) {
819 s++;
820 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000821 goto out;
822 }
823
824 if (*s == 'K') {
825 delete = 1;
826 s++;
827 }
Damien Miller0164cb82008-11-05 16:30:31 +1100828 if (*s == 'L')
829 local = 1;
830 else if (*s == 'R')
831 remote = 1;
832 else if (*s == 'D')
833 dynamic = 1;
834 else {
Damien Miller996acd22003-04-09 20:59:48 +1000835 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000836 goto out;
837 }
Damien Miller0164cb82008-11-05 16:30:31 +1100838
Damien Millerfdb23062013-11-21 13:57:15 +1100839 while (isspace((u_char)*++s))
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100840 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000841
Damien Millere1537f92010-01-26 13:26:22 +1100842 /* XXX update list of forwards in options */
Darren Tuckere7066df2004-05-24 10:18:05 +1000843 if (delete) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000844 /* We pass 1 for dynamicfwd to restrict to 1 or 2 fields. */
845 if (!parse_forward(&fwd, s, 1, 0)) {
846 logit("Bad forwarding close specification.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000847 goto out;
848 }
Damien Millerff773642011-09-22 21:39:48 +1000849 if (remote)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000850 ok = channel_request_rforward_cancel(ssh, &fwd) == 0;
Damien Millerff773642011-09-22 21:39:48 +1000851 else if (dynamic)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000852 ok = channel_cancel_lport_listener(ssh, &fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +1000853 0, &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000854 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000855 ok = channel_cancel_lport_listener(ssh, &fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +1000856 CHANNEL_CANCEL_PORT_STATIC,
857 &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000858 if (!ok) {
dtucker@openbsd.org9390b002017-01-29 21:35:23 +0000859 logit("Unknown port forwarding.");
Damien Millerff773642011-09-22 21:39:48 +1000860 goto out;
861 }
862 logit("Canceled forwarding.");
Darren Tuckere7066df2004-05-24 10:18:05 +1000863 } else {
Damien Miller4bf648f2009-02-14 16:28:21 +1100864 if (!parse_forward(&fwd, s, dynamic, remote)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000865 logit("Bad forwarding specification.");
866 goto out;
867 }
Damien Miller0164cb82008-11-05 16:30:31 +1100868 if (local || dynamic) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000869 if (!channel_setup_local_fwd_listener(ssh, &fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +1000870 &options.fwd_opts)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000871 logit("Port forwarding failed.");
872 goto out;
873 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100874 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000875 if (channel_request_remote_forwarding(ssh, &fwd) < 0) {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000876 logit("Port forwarding failed.");
877 goto out;
878 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100879 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000880 logit("Forwarding port.");
881 }
882
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000883out:
884 signal(SIGINT, handler);
Damien Miller21771e22011-05-15 08:45:50 +1000885 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tuckera627d422013-06-02 07:31:17 +1000886 free(cmd);
887 free(fwd.listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000888 free(fwd.listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000889 free(fwd.connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000890 free(fwd.connect_path);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000891}
892
Darren Tucker92a39cf2012-09-07 11:20:20 +1000893/* reasons to suppress output of an escape command in help output */
894#define SUPPRESS_NEVER 0 /* never suppress, always show */
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000895#define SUPPRESS_MUXCLIENT 1 /* don't show in mux client sessions */
896#define SUPPRESS_MUXMASTER 2 /* don't show in mux master sessions */
897#define SUPPRESS_SYSLOG 4 /* don't show when logging to syslog */
Darren Tucker92a39cf2012-09-07 11:20:20 +1000898struct escape_help_text {
899 const char *cmd;
900 const char *text;
901 unsigned int flags;
902};
903static struct escape_help_text esc_txt[] = {
904 {".", "terminate session", SUPPRESS_MUXMASTER},
905 {".", "terminate connection (and any multiplexed sessions)",
906 SUPPRESS_MUXCLIENT},
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000907 {"B", "send a BREAK to the remote system", SUPPRESS_NEVER},
Darren Tucker92a39cf2012-09-07 11:20:20 +1000908 {"C", "open a command line", SUPPRESS_MUXCLIENT},
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000909 {"R", "request rekey", SUPPRESS_NEVER},
Darren Tuckerca0d0fd2012-09-07 11:22:24 +1000910 {"V/v", "decrease/increase verbosity (LogLevel)", SUPPRESS_MUXCLIENT},
Darren Tucker92a39cf2012-09-07 11:20:20 +1000911 {"^Z", "suspend ssh", SUPPRESS_MUXCLIENT},
912 {"#", "list forwarded connections", SUPPRESS_NEVER},
913 {"&", "background ssh (when waiting for connections to terminate)",
914 SUPPRESS_MUXCLIENT},
915 {"?", "this message", SUPPRESS_NEVER},
916};
917
918static void
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000919print_escape_help(struct sshbuf *b, int escape_char, int mux_client,
920 int using_stderr)
Darren Tucker92a39cf2012-09-07 11:20:20 +1000921{
922 unsigned int i, suppress_flags;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000923 int r;
Darren Tucker92a39cf2012-09-07 11:20:20 +1000924
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000925 if ((r = sshbuf_putf(b,
926 "%c?\r\nSupported escape sequences:\r\n", escape_char)) != 0)
927 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker92a39cf2012-09-07 11:20:20 +1000928
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000929 suppress_flags =
Darren Tucker92a39cf2012-09-07 11:20:20 +1000930 (mux_client ? SUPPRESS_MUXCLIENT : 0) |
931 (mux_client ? 0 : SUPPRESS_MUXMASTER) |
932 (using_stderr ? 0 : SUPPRESS_SYSLOG);
933
934 for (i = 0; i < sizeof(esc_txt)/sizeof(esc_txt[0]); i++) {
935 if (esc_txt[i].flags & suppress_flags)
936 continue;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000937 if ((r = sshbuf_putf(b, " %c%-3s - %s\r\n",
938 escape_char, esc_txt[i].cmd, esc_txt[i].text)) != 0)
939 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker92a39cf2012-09-07 11:20:20 +1000940 }
941
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000942 if ((r = sshbuf_putf(b,
Darren Tuckerca0d0fd2012-09-07 11:22:24 +1000943 " %c%c - send the escape character by typing it twice\r\n"
Darren Tucker92a39cf2012-09-07 11:20:20 +1000944 "(Note that escapes are only recognized immediately after "
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000945 "newline.)\r\n", escape_char, escape_char)) != 0)
946 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker92a39cf2012-09-07 11:20:20 +1000947}
948
djm@openbsd.org84623e02018-06-26 02:02:36 +0000949/*
naddy@openbsd.org768405f2017-05-03 21:08:09 +0000950 * Process the characters one by one.
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000951 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000952static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000953process_escapes(struct ssh *ssh, Channel *c,
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000954 struct sshbuf *bin, struct sshbuf *bout, struct sshbuf *berr,
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000955 char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +1000956{
Damien Millerad833b32000-08-23 10:46:23 +1000957 pid_t pid;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000958 int r, bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000959 u_int i;
960 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +1000961 char *s;
djm@openbsd.org05164352017-04-30 23:21:54 +0000962 struct escape_filter_ctx *efc = c->filter_ctx == NULL ?
963 NULL : (struct escape_filter_ctx *)c->filter_ctx;
Damien Millerad833b32000-08-23 10:46:23 +1000964
djm@openbsd.org05164352017-04-30 23:21:54 +0000965 if (c->filter_ctx == NULL)
966 return 0;
djm@openbsd.org84623e02018-06-26 02:02:36 +0000967
Damien Millereccb9de2005-06-17 12:59:34 +1000968 if (len <= 0)
969 return (0);
970
971 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +1000972 /* Get one character at a time. */
973 ch = buf[i];
974
djm@openbsd.org05164352017-04-30 23:21:54 +0000975 if (efc->escape_pending) {
Damien Millerad833b32000-08-23 10:46:23 +1000976 /* We have previously seen an escape character. */
977 /* Clear the flag now. */
djm@openbsd.org05164352017-04-30 23:21:54 +0000978 efc->escape_pending = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000979
980 /* Process the escaped character. */
981 switch (ch) {
982 case '.':
983 /* Terminate the connection. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000984 if ((r = sshbuf_putf(berr, "%c.\r\n",
985 efc->escape_char)) != 0)
986 fatal("%s: buffer error: %s",
987 __func__, ssh_err(r));
Damien Millere1537f92010-01-26 13:26:22 +1100988 if (c && c->ctl_chan != -1) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000989 chan_read_failed(ssh, c);
990 chan_write_failed(ssh, c);
991 if (c->detach_user) {
992 c->detach_user(ssh,
993 c->self, NULL);
994 }
Damien Miller36187092013-06-10 13:07:11 +1000995 c->type = SSH_CHANNEL_ABANDONED;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000996 sshbuf_reset(c->input);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000997 chan_ibuf_empty(ssh, c);
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000998 return 0;
999 } else
1000 quit_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001001 return -1;
1002
1003 case 'Z' - 64:
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001004 /* XXX support this for mux clients */
Damien Millere1537f92010-01-26 13:26:22 +11001005 if (c && c->ctl_chan != -1) {
Darren Tuckerf111d402012-09-07 11:21:42 +10001006 char b[16];
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001007 noescape:
Darren Tuckerf111d402012-09-07 11:21:42 +10001008 if (ch == 'Z' - 64)
1009 snprintf(b, sizeof b, "^Z");
1010 else
1011 snprintf(b, sizeof b, "%c", ch);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001012 if ((r = sshbuf_putf(berr,
Darren Tuckerf111d402012-09-07 11:21:42 +10001013 "%c%s escape not available to "
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001014 "multiplexed sessions\r\n",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001015 efc->escape_char, b)) != 0)
1016 fatal("%s: buffer error: %s",
1017 __func__, ssh_err(r));
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001018 continue;
1019 }
Darren Tucker4d5cd332008-06-13 04:51:14 +10001020 /* Suspend the program. Inform the user */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001021 if ((r = sshbuf_putf(berr,
1022 "%c^Z [suspend ssh]\r\n",
1023 efc->escape_char)) != 0)
1024 fatal("%s: buffer error: %s",
1025 __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +10001026
1027 /* Restore terminal modes and suspend. */
1028 client_suspend_self(bin, bout, berr);
1029
1030 /* We have been continued. */
1031 continue;
1032
Damien Miller54c45982003-05-15 10:20:13 +10001033 case 'B':
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001034 if ((r = sshbuf_putf(berr,
1035 "%cB\r\n", efc->escape_char)) != 0)
1036 fatal("%s: buffer error: %s",
1037 __func__, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001038 channel_request_start(ssh, c->self, "break", 0);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001039 if ((r = sshpkt_put_u32(ssh, 1000)) != 0 ||
1040 (r = sshpkt_send(ssh)) != 0)
1041 fatal("%s: %s", __func__,
1042 ssh_err(r));
Damien Miller54c45982003-05-15 10:20:13 +10001043 continue;
1044
Ben Lindstromf28f6342001-04-04 02:03:04 +00001045 case 'R':
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001046 if (datafellows & SSH_BUG_NOREKEY)
1047 logit("Server does not "
1048 "support re-keying");
1049 else
1050 need_rekeying = 1;
Ben Lindstromf28f6342001-04-04 02:03:04 +00001051 continue;
1052
Darren Tucker50a48d02012-09-06 21:25:37 +10001053 case 'V':
1054 /* FALLTHROUGH */
1055 case 'v':
1056 if (c && c->ctl_chan != -1)
1057 goto noescape;
1058 if (!log_is_on_stderr()) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001059 if ((r = sshbuf_putf(berr,
Darren Tucker50a48d02012-09-06 21:25:37 +10001060 "%c%c [Logging to syslog]\r\n",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001061 efc->escape_char, ch)) != 0)
1062 fatal("%s: buffer error: %s",
1063 __func__, ssh_err(r));
Darren Tucker50a48d02012-09-06 21:25:37 +10001064 continue;
1065 }
1066 if (ch == 'V' && options.log_level >
1067 SYSLOG_LEVEL_QUIET)
1068 log_change_level(--options.log_level);
1069 if (ch == 'v' && options.log_level <
1070 SYSLOG_LEVEL_DEBUG3)
1071 log_change_level(++options.log_level);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001072 if ((r = sshbuf_putf(berr,
djm@openbsd.org05164352017-04-30 23:21:54 +00001073 "%c%c [LogLevel %s]\r\n",
1074 efc->escape_char, ch,
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001075 log_level_name(options.log_level))) != 0)
1076 fatal("%s: buffer error: %s",
1077 __func__, ssh_err(r));
Darren Tucker50a48d02012-09-06 21:25:37 +10001078 continue;
1079
Damien Millerad833b32000-08-23 10:46:23 +10001080 case '&':
Damien Millere1537f92010-01-26 13:26:22 +11001081 if (c && c->ctl_chan != -1)
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001082 goto noescape;
Damien Millerad833b32000-08-23 10:46:23 +10001083 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001084 * Detach the program (continue to serve
1085 * connections, but put in background and no
1086 * more new connections).
Damien Millerad833b32000-08-23 10:46:23 +10001087 */
Damien Miller96507ef2001-11-12 10:52:25 +11001088 /* Restore tty modes. */
Damien Miller21771e22011-05-15 08:45:50 +10001089 leave_raw_mode(
1090 options.request_tty == REQUEST_TTY_FORCE);
Damien Miller96507ef2001-11-12 10:52:25 +11001091
1092 /* Stop listening for new connections. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001093 channel_stop_listening(ssh);
Damien Miller96507ef2001-11-12 10:52:25 +11001094
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001095 if ((r = sshbuf_putf(berr,
1096 "%c& [backgrounded]\n", efc->escape_char))
1097 != 0)
1098 fatal("%s: buffer error: %s",
1099 __func__, ssh_err(r));
Damien Miller96507ef2001-11-12 10:52:25 +11001100
1101 /* Fork into background. */
1102 pid = fork();
1103 if (pid < 0) {
1104 error("fork: %.100s", strerror(errno));
1105 continue;
1106 }
1107 if (pid != 0) { /* This is the parent. */
1108 /* The parent just exits. */
1109 exit(0);
1110 }
1111 /* The child continues serving connections. */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001112 /* fake EOF on stdin */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001113 if ((r = sshbuf_put_u8(bin, 4)) != 0)
1114 fatal("%s: buffer error: %s",
1115 __func__, ssh_err(r));
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001116 return -1;
Damien Millerad833b32000-08-23 10:46:23 +10001117 case '?':
djm@openbsd.org05164352017-04-30 23:21:54 +00001118 print_escape_help(berr, efc->escape_char,
Darren Tucker92a39cf2012-09-07 11:20:20 +10001119 (c && c->ctl_chan != -1),
1120 log_is_on_stderr());
Damien Millerad833b32000-08-23 10:46:23 +10001121 continue;
1122
1123 case '#':
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001124 if ((r = sshbuf_putf(berr, "%c#\r\n",
1125 efc->escape_char)) != 0)
1126 fatal("%s: buffer error: %s",
1127 __func__, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001128 s = channel_open_message(ssh);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001129 if ((r = sshbuf_put(berr, s, strlen(s))) != 0)
1130 fatal("%s: buffer error: %s",
1131 __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +10001132 free(s);
Damien Millerad833b32000-08-23 10:46:23 +10001133 continue;
1134
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001135 case 'C':
Damien Millere1537f92010-01-26 13:26:22 +11001136 if (c && c->ctl_chan != -1)
Damien Miller586b0052008-12-09 14:11:32 +11001137 goto noescape;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001138 process_cmdline(ssh);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001139 continue;
1140
Damien Millerad833b32000-08-23 10:46:23 +10001141 default:
djm@openbsd.org05164352017-04-30 23:21:54 +00001142 if (ch != efc->escape_char) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001143 if ((r = sshbuf_put_u8(bin,
1144 efc->escape_char)) != 0)
1145 fatal("%s: buffer error: %s",
1146 __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +10001147 bytes++;
1148 }
1149 /* Escaped characters fall through here */
1150 break;
1151 }
1152 } else {
1153 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001154 * The previous character was not an escape char.
1155 * Check if this is an escape.
Damien Millerad833b32000-08-23 10:46:23 +10001156 */
djm@openbsd.org05164352017-04-30 23:21:54 +00001157 if (last_was_cr && ch == efc->escape_char) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001158 /*
1159 * It is. Set the flag and continue to
1160 * next character.
1161 */
djm@openbsd.org05164352017-04-30 23:21:54 +00001162 efc->escape_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001163 continue;
1164 }
1165 }
1166
1167 /*
1168 * Normal character. Record whether it was a newline,
1169 * and append it to the buffer.
1170 */
1171 last_was_cr = (ch == '\r' || ch == '\n');
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001172 if ((r = sshbuf_put_u8(bin, ch)) != 0)
1173 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +10001174 bytes++;
1175 }
1176 return bytes;
1177}
1178
Damien Miller5428f641999-11-25 11:54:57 +11001179/*
Damien Millerb38eff82000-04-01 11:09:21 +10001180 * Get packets from the connection input buffer, and process them as long as
1181 * there are packets available.
1182 *
1183 * Any unknown packets received during the actual
1184 * session cause the session to terminate. This is
1185 * intended to make debugging easier since no
1186 * confirmations are sent. Any compatible protocol
1187 * extensions must be negotiated during the
1188 * preparatory phase.
1189 */
1190
Ben Lindstrombba81212001-06-25 05:01:22 +00001191static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001192client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001193{
markus@openbsd.org92e9fe62017-05-31 07:00:13 +00001194 ssh_dispatch_run_fatal(active_state, DISPATCH_NONBLOCK, &quit_pending);
Damien Millerb38eff82000-04-01 11:09:21 +10001195}
1196
Damien Millerad833b32000-08-23 10:46:23 +10001197/* scan buf[] for '~' before sending data to the peer */
1198
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001199/* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1200void *
1201client_new_escape_filter_ctx(int escape_char)
1202{
1203 struct escape_filter_ctx *ret;
1204
Damien Miller6c81fee2013-11-08 12:19:55 +11001205 ret = xcalloc(1, sizeof(*ret));
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001206 ret->escape_pending = 0;
1207 ret->escape_char = escape_char;
1208 return (void *)ret;
1209}
1210
Darren Tucker84c56f52008-06-13 04:55:46 +10001211/* Free the escape filter context on channel free */
1212void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001213client_filter_cleanup(struct ssh *ssh, int cid, void *ctx)
Darren Tucker84c56f52008-06-13 04:55:46 +10001214{
Darren Tuckera627d422013-06-02 07:31:17 +10001215 free(ctx);
Darren Tucker84c56f52008-06-13 04:55:46 +10001216}
1217
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001218int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001219client_simple_escape_filter(struct ssh *ssh, Channel *c, char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001220{
Damien Miller5771ed72008-05-19 15:35:33 +10001221 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1222 return 0;
1223
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001224 return process_escapes(ssh, c, c->input, c->output, c->extended,
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001225 buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001226}
1227
Ben Lindstrombba81212001-06-25 05:01:22 +00001228static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001229client_channel_closed(struct ssh *ssh, int id, void *arg)
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001230{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001231 channel_cancel_cleanup(ssh, id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001232 session_closed = 1;
Damien Miller21771e22011-05-15 08:45:50 +10001233 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001234}
1235
Damien Millerb38eff82000-04-01 11:09:21 +10001236/*
Damien Miller5428f641999-11-25 11:54:57 +11001237 * Implements the interactive session with the server. This is called after
1238 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001239 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1240 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001241 */
Damien Miller4af51302000-04-16 11:18:38 +10001242int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001243client_loop(struct ssh *ssh, int have_pty, int escape_char_arg,
1244 int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001245{
Damien Miller5e953212001-01-30 09:14:00 +11001246 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001247 double start_time, total_time;
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001248 int r, max_fd = 0, max_fd2 = 0, len;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001249 u_int64_t ibytes, obytes;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001250 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001251 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001252
Damien Miller95def091999-11-25 00:26:21 +11001253 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001254
semarie@openbsd.orgd7d2bc92015-12-26 07:46:03 +00001255 if (options.control_master &&
djm@openbsd.org368dd972016-07-23 02:54:08 +00001256 !option_clear_or_none(options.control_path)) {
semarie@openbsd.orgd7d2bc92015-12-26 07:46:03 +00001257 debug("pledge: id");
tb@openbsd.org372807c2016-07-11 21:38:13 +00001258 if (pledge("stdio rpath wpath cpath unix inet dns recvfd proc exec id tty",
semarie@openbsd.orgd7d2bc92015-12-26 07:46:03 +00001259 NULL) == -1)
1260 fatal("%s pledge(): %s", __func__, strerror(errno));
1261
1262 } else if (options.forward_x11 || options.permit_local_command) {
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001263 debug("pledge: exec");
1264 if (pledge("stdio rpath wpath cpath unix inet dns proc exec tty",
1265 NULL) == -1)
1266 fatal("%s pledge(): %s", __func__, strerror(errno));
1267
1268 } else if (options.update_hostkeys) {
1269 debug("pledge: filesystem full");
1270 if (pledge("stdio rpath wpath cpath unix inet dns proc tty",
1271 NULL) == -1)
1272 fatal("%s pledge(): %s", __func__, strerror(errno));
1273
djm@openbsd.org368dd972016-07-23 02:54:08 +00001274 } else if (!option_clear_or_none(options.proxy_command) ||
1275 fork_after_authentication_flag) {
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001276 debug("pledge: proc");
1277 if (pledge("stdio cpath unix inet dns proc tty", NULL) == -1)
1278 fatal("%s pledge(): %s", __func__, strerror(errno));
1279
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001280 } else {
1281 debug("pledge: network");
mestre@openbsd.orge2386452017-06-23 07:24:48 +00001282 if (pledge("stdio unix inet dns proc tty", NULL) == -1)
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001283 fatal("%s pledge(): %s", __func__, strerror(errno));
1284 }
1285
dtucker@openbsd.org@openbsd.org2d638e92017-11-25 05:58:47 +00001286 start_time = monotime_double();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001287
Damien Miller95def091999-11-25 00:26:21 +11001288 /* Initialize variables. */
Damien Miller95def091999-11-25 00:26:21 +11001289 last_was_cr = 1;
1290 exit_status = -1;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001291 connection_in = ssh_packet_get_connection_in(ssh);
1292 connection_out = ssh_packet_get_connection_out(ssh);
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001293 max_fd = MAXIMUM(connection_in, connection_out);
Damien Miller5e953212001-01-30 09:14:00 +11001294
Damien Miller95def091999-11-25 00:26:21 +11001295 quit_pending = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001296
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001297 /* Initialize buffer. */
1298 if ((stderr_buffer = sshbuf_new()) == NULL)
1299 fatal("%s: sshbuf_new failed", __func__);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001300
Damien Millerb38eff82000-04-01 11:09:21 +10001301 client_init_dispatch();
1302
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001303 /*
1304 * Set signal handlers, (e.g. to restore non-blocking mode)
1305 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1306 */
Darren Tucker07336da2004-11-05 20:02:16 +11001307 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1308 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001309 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1310 signal(SIGINT, signal_handler);
1311 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1312 signal(SIGQUIT, signal_handler);
1313 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1314 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001315 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001316
Damien Miller95def091999-11-25 00:26:21 +11001317 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001318 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001319
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001320 session_ident = ssh2_chan_id;
1321 if (session_ident != -1) {
1322 if (escape_char_arg != SSH_ESCAPECHAR_NONE) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001323 channel_register_filter(ssh, session_ident,
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001324 client_simple_escape_filter, NULL,
1325 client_filter_cleanup,
1326 client_new_escape_filter_ctx(
1327 escape_char_arg));
Damien Miller6c3eec72011-05-05 14:16:22 +10001328 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001329 channel_register_cleanup(ssh, session_ident,
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001330 client_channel_closed, 0);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001331 }
Damien Millerad833b32000-08-23 10:46:23 +10001332
Damien Miller95def091999-11-25 00:26:21 +11001333 /* Main loop of the client for the interactive session mode. */
1334 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001335
Damien Miller5428f641999-11-25 11:54:57 +11001336 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001337 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001338
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001339 if (session_closed && !channel_still_open(ssh))
Damien Miller1383bd82000-04-06 12:32:37 +10001340 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001341
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001342 if (ssh_packet_is_rekeying(ssh)) {
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001343 debug("rekeying in progress");
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001344 } else if (need_rekeying) {
1345 /* manual rekey request */
1346 debug("need rekeying");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001347 if ((r = kex_start_rekex(ssh)) != 0)
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001348 fatal("%s: kex_start_rekex: %s", __func__,
1349 ssh_err(r));
1350 need_rekeying = 0;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001351 } else {
1352 /*
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001353 * Make packets from buffered channel data, and
1354 * enqueue them for sending to the server.
1355 */
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001356 if (ssh_packet_not_very_much_data_to_write(ssh))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001357 channel_output_poll(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001358
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001359 /*
1360 * Check if the window size has changed, and buffer a
1361 * message about it to the server if so.
1362 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001363 client_check_window_change(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001364
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001365 if (quit_pending)
1366 break;
1367 }
Damien Miller5428f641999-11-25 11:54:57 +11001368 /*
1369 * Wait until we have something to do (something becomes
1370 * available on one of the descriptors).
1371 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001372 max_fd2 = max_fd;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001373 client_wait_until_can_do_something(ssh, &readset, &writeset,
1374 &max_fd2, &nalloc, ssh_packet_is_rekeying(ssh));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001375
Damien Miller95def091999-11-25 00:26:21 +11001376 if (quit_pending)
1377 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001378
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001379 /* Do channel operations unless rekeying in progress. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001380 if (!ssh_packet_is_rekeying(ssh))
1381 channel_after_select(ssh, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001382
Damien Miller1383bd82000-04-06 12:32:37 +10001383 /* Buffer input from the connection. */
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001384 client_process_net_input(ssh, readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001385
Damien Miller1383bd82000-04-06 12:32:37 +10001386 if (quit_pending)
1387 break;
1388
Darren Tucker4d5cd332008-06-13 04:51:14 +10001389 /*
1390 * Send as much buffered packet data as possible to the
1391 * sender.
1392 */
Damien Miller5e953212001-01-30 09:14:00 +11001393 if (FD_ISSET(connection_out, writeset))
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001394 ssh_packet_write_poll(ssh);
Damien Millere11e1ea2010-08-03 16:04:46 +10001395
1396 /*
1397 * If we are a backgrounded control master, and the
1398 * timeout has expired without any active client
1399 * connections, then quit.
1400 */
1401 if (control_persist_exit_time > 0) {
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001402 if (monotime() >= control_persist_exit_time) {
Damien Millere11e1ea2010-08-03 16:04:46 +10001403 debug("ControlPersist timeout expired");
1404 break;
1405 }
1406 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001407 }
Darren Tuckera627d422013-06-02 07:31:17 +10001408 free(readset);
1409 free(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001410
Damien Miller95def091999-11-25 00:26:21 +11001411 /* Terminate the session. */
1412
1413 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001414 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001415
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001416 if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
1417 (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_BY_APPLICATION)) != 0 ||
1418 (r = sshpkt_put_cstring(ssh, "disconnected by user")) != 0 ||
1419 (r = sshpkt_put_cstring(ssh, "")) != 0 || /* language tag */
1420 (r = sshpkt_send(ssh)) != 0 ||
1421 (r = ssh_packet_write_wait(ssh)) != 0)
1422 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker12b4a652009-06-21 18:14:48 +10001423
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001424 channel_free_all(ssh);
Damien Miller95def091999-11-25 00:26:21 +11001425
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001426 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001427 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001428
1429 /* restore blocking io */
1430 if (!isatty(fileno(stdin)))
1431 unset_nonblock(fileno(stdin));
1432 if (!isatty(fileno(stdout)))
1433 unset_nonblock(fileno(stdout));
1434 if (!isatty(fileno(stderr)))
1435 unset_nonblock(fileno(stderr));
1436
Damien Millerd6965512003-12-17 16:31:53 +11001437 /*
1438 * If there was no shell or command requested, there will be no remote
1439 * exit status to be returned. In that case, clear error code if the
1440 * connection was deliberately terminated at this end.
1441 */
1442 if (no_shell_flag && received_signal == SIGTERM) {
1443 received_signal = 0;
1444 exit_status = 0;
1445 }
1446
dtucker@openbsd.orgb1e72df2017-07-14 03:18:21 +00001447 if (received_signal) {
1448 verbose("Killed by signal %d.", (int) received_signal);
1449 cleanup_exit(0);
1450 }
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001451
1452 /*
1453 * In interactive mode (with pseudo tty) display a message indicating
1454 * that the connection has been closed.
1455 */
1456 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001457 if ((r = sshbuf_putf(stderr_buffer,
1458 "Connection to %.64s closed.\r\n", host)) != 0)
1459 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001460 }
1461
Damien Miller95def091999-11-25 00:26:21 +11001462 /* Output any buffered data for stderr. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001463 if (sshbuf_len(stderr_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001464 len = atomicio(vwrite, fileno(stderr),
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001465 (u_char *)sshbuf_ptr(stderr_buffer),
1466 sshbuf_len(stderr_buffer));
1467 if (len < 0 || (u_int)len != sshbuf_len(stderr_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001468 error("Write failed flushing stderr buffer.");
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001469 else if ((r = sshbuf_consume(stderr_buffer, len)) != 0)
1470 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11001471 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001472
Damien Miller95def091999-11-25 00:26:21 +11001473 /* Clear and free any buffers. */
jsg@openbsd.org458abc22016-01-23 05:31:35 +00001474 explicit_bzero(buf, sizeof(buf));
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001475 sshbuf_free(stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001476
Damien Miller95def091999-11-25 00:26:21 +11001477 /* Report bytes transferred, and transfer rates. */
dtucker@openbsd.org@openbsd.org2d638e92017-11-25 05:58:47 +00001478 total_time = monotime_double() - start_time;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001479 ssh_packet_get_bytes(ssh, &ibytes, &obytes);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001480 verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
Damien Miller821de0a2011-01-11 17:20:29 +11001481 (unsigned long long)obytes, (unsigned long long)ibytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001482 if (total_time > 0)
Damien Millerb61f3fc2008-07-11 17:36:48 +10001483 verbose("Bytes per second: sent %.1f, received %.1f",
1484 obytes / total_time, ibytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001485 /* Return the exit status of the program. */
1486 debug("Exit status %d", exit_status);
1487 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001488}
Damien Millerb38eff82000-04-01 11:09:21 +10001489
1490/*********/
1491
Ben Lindstrombba81212001-06-25 05:01:22 +00001492static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001493client_request_forwarded_tcpip(struct ssh *ssh, const char *request_type,
1494 int rchan, u_int rwindow, u_int rmaxpack)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001495{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001496 Channel *c = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00001497 struct sshbuf *b = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001498 char *listen_address, *originator_address;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001499 u_int listen_port, originator_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00001500 int r;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001501
1502 /* Get rest of the packet */
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001503 if ((r = sshpkt_get_cstring(ssh, &listen_address, NULL)) != 0 ||
1504 (r = sshpkt_get_u32(ssh, &listen_port)) != 0 ||
1505 (r = sshpkt_get_cstring(ssh, &originator_address, NULL)) != 0 ||
1506 (r = sshpkt_get_u32(ssh, &originator_port)) != 0 ||
1507 (r = sshpkt_get_end(ssh)) != 0)
1508 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller0bc1bd82000-11-13 22:57:25 +11001509
Damien Miller7acefbb2014-07-18 14:11:24 +10001510 debug("%s: listen %s port %d, originator %s port %d", __func__,
1511 listen_address, listen_port, originator_address, originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001512
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001513 if (listen_port > 0xffff)
1514 error("%s: invalid listen port", __func__);
1515 else if (originator_port > 0xffff)
1516 error("%s: invalid originator port", __func__);
1517 else {
1518 c = channel_connect_by_listen_address(ssh,
1519 listen_address, listen_port, "forwarded-tcpip",
1520 originator_address);
1521 }
Damien Millerbd740252008-05-19 15:37:09 +10001522
markus@openbsd.org8d057842016-09-30 09:19:13 +00001523 if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) {
1524 if ((b = sshbuf_new()) == NULL) {
1525 error("%s: alloc reply", __func__);
1526 goto out;
1527 }
1528 /* reconstruct and send to muxclient */
1529 if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
1530 (r = sshbuf_put_u8(b, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
1531 (r = sshbuf_put_cstring(b, request_type)) != 0 ||
1532 (r = sshbuf_put_u32(b, rchan)) != 0 ||
1533 (r = sshbuf_put_u32(b, rwindow)) != 0 ||
1534 (r = sshbuf_put_u32(b, rmaxpack)) != 0 ||
1535 (r = sshbuf_put_cstring(b, listen_address)) != 0 ||
1536 (r = sshbuf_put_u32(b, listen_port)) != 0 ||
1537 (r = sshbuf_put_cstring(b, originator_address)) != 0 ||
1538 (r = sshbuf_put_u32(b, originator_port)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001539 (r = sshbuf_put_stringb(c->output, b)) != 0) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00001540 error("%s: compose for muxclient %s", __func__,
1541 ssh_err(r));
1542 goto out;
1543 }
1544 }
1545
1546 out:
1547 sshbuf_free(b);
Darren Tuckera627d422013-06-02 07:31:17 +10001548 free(originator_address);
1549 free(listen_address);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001550 return c;
1551}
1552
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001553static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001554client_request_forwarded_streamlocal(struct ssh *ssh,
1555 const char *request_type, int rchan)
Damien Miller7acefbb2014-07-18 14:11:24 +10001556{
1557 Channel *c = NULL;
1558 char *listen_path;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001559 int r;
Damien Miller7acefbb2014-07-18 14:11:24 +10001560
1561 /* Get the remote path. */
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001562 if ((r = sshpkt_get_cstring(ssh, &listen_path, NULL)) != 0 ||
1563 (r = sshpkt_get_string(ssh, NULL, NULL)) != 0 || /* reserved */
1564 (r = sshpkt_get_end(ssh)) != 0)
1565 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller7acefbb2014-07-18 14:11:24 +10001566
1567 debug("%s: %s", __func__, listen_path);
1568
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001569 c = channel_connect_by_listen_path(ssh, listen_path,
Damien Miller7acefbb2014-07-18 14:11:24 +10001570 "forwarded-streamlocal@openssh.com", "forwarded-streamlocal");
1571 free(listen_path);
1572 return c;
1573}
1574
1575static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001576client_request_x11(struct ssh *ssh, const char *request_type, int rchan)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001577{
1578 Channel *c = NULL;
1579 char *originator;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001580 int originator_port;
1581 int r, sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001582
1583 if (!options.forward_x11) {
1584 error("Warning: ssh server tried X11 forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001585 error("Warning: this is probably a break-in attempt by a "
1586 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001587 return NULL;
1588 }
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001589 if (x11_refuse_time != 0 && (u_int)monotime() >= x11_refuse_time) {
Damien Miller1ab6a512010-06-26 10:02:24 +10001590 verbose("Rejected X11 connection after ForwardX11Timeout "
1591 "expired");
1592 return NULL;
1593 }
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001594 if ((r = sshpkt_get_cstring(ssh, &originator, NULL)) != 0 ||
1595 (r = sshpkt_get_u32(ssh, (u_int *)&originator_port)) != 0 ||
1596 (r = sshpkt_get_end(ssh)) != 0)
1597 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller0bc1bd82000-11-13 22:57:25 +11001598 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001599 debug("client_request_x11: request from %s %d", originator,
1600 originator_port);
Darren Tuckera627d422013-06-02 07:31:17 +10001601 free(originator);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001602 sock = x11_connect_display(ssh);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001603 if (sock < 0)
1604 return NULL;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001605 c = channel_new(ssh, "x11",
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001606 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001607 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001608 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001609 return c;
1610}
1611
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001612static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001613client_request_agent(struct ssh *ssh, const char *request_type, int rchan)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001614{
1615 Channel *c = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001616 int r, sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001617
1618 if (!options.forward_agent) {
1619 error("Warning: ssh server tried agent forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001620 error("Warning: this is probably a break-in attempt by a "
1621 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001622 return NULL;
1623 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001624 if ((r = ssh_get_authentication_socket(&sock)) != 0) {
1625 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1626 debug("%s: ssh_get_authentication_socket: %s",
1627 __func__, ssh_err(r));
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001628 return NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001629 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001630 c = channel_new(ssh, "authentication agent connection",
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001631 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001632 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001633 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001634 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001635 return c;
1636}
1637
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001638char *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001639client_request_tun_fwd(struct ssh *ssh, int tun_mode,
1640 int local_tun, int remote_tun)
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001641{
1642 Channel *c;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001643 int r, fd;
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001644 char *ifname = NULL;
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001645
1646 if (tun_mode == SSH_TUNMODE_NO)
1647 return 0;
1648
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001649 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1650
1651 /* Open local tunnel device */
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001652 if ((fd = tun_open(local_tun, tun_mode, &ifname)) == -1) {
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001653 error("Tunnel device open failed.");
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001654 return NULL;
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001655 }
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001656 debug("Tunnel forwarding using interface %s", ifname);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001657
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001658 c = channel_new(ssh, "tun", SSH_CHANNEL_OPENING, fd, fd, -1,
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001659 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1660 c->datagram = 1;
1661
1662#if defined(SSH_TUN_FILTER)
1663 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
Damien Miller871f1e42017-09-12 18:01:35 +10001664 channel_register_filter(ssh, c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +10001665 sys_tun_outfilter, NULL, NULL);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001666#endif
1667
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001668 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
1669 (r = sshpkt_put_cstring(ssh, "tun@openssh.com")) != 0 ||
1670 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
1671 (r = sshpkt_put_u32(ssh, c->local_window_max)) != 0 ||
1672 (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0 ||
1673 (r = sshpkt_put_u32(ssh, tun_mode)) != 0 ||
1674 (r = sshpkt_put_u32(ssh, remote_tun)) != 0 ||
1675 (r = sshpkt_send(ssh)) != 0)
1676 fatal("%s: %s", __func__, ssh_err(r));
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001677
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001678 return ifname;
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001679}
1680
Damien Millerbd483e72000-04-30 10:00:53 +10001681/* XXXX move to generic input handler */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001682static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001683client_input_channel_open(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerbd483e72000-04-30 10:00:53 +10001684{
1685 Channel *c = NULL;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001686 char *ctype = NULL;
1687 int r;
1688 u_int rchan;
1689 size_t len;
1690 u_int rmaxpack, rwindow;
Damien Millerbd483e72000-04-30 10:00:53 +10001691
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001692 if ((r = sshpkt_get_cstring(ssh, &ctype, &len)) != 0 ||
1693 (r = sshpkt_get_u32(ssh, &rchan)) != 0 ||
1694 (r = sshpkt_get_u32(ssh, &rwindow)) != 0 ||
1695 (r = sshpkt_get_u32(ssh, &rmaxpack)) != 0)
1696 goto out;
Damien Millerbd483e72000-04-30 10:00:53 +10001697
Damien Millere247cc42000-05-07 12:03:14 +10001698 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001699 ctype, rchan, rwindow, rmaxpack);
1700
Damien Miller0bc1bd82000-11-13 22:57:25 +11001701 if (strcmp(ctype, "forwarded-tcpip") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001702 c = client_request_forwarded_tcpip(ssh, ctype, rchan, rwindow,
markus@openbsd.org8d057842016-09-30 09:19:13 +00001703 rmaxpack);
Damien Miller7acefbb2014-07-18 14:11:24 +10001704 } else if (strcmp(ctype, "forwarded-streamlocal@openssh.com") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001705 c = client_request_forwarded_streamlocal(ssh, ctype, rchan);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001706 } else if (strcmp(ctype, "x11") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001707 c = client_request_x11(ssh, ctype, rchan);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001708 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001709 c = client_request_agent(ssh, ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001710 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00001711 if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) {
1712 debug3("proxied to downstream: %s", ctype);
1713 } else if (c != NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10001714 debug("confirm %s", ctype);
1715 c->remote_id = rchan;
djm@openbsd.org9f532292017-09-12 06:35:31 +00001716 c->have_remote_id = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001717 c->remote_window = rwindow;
1718 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001719 if (c->type != SSH_CHANNEL_CONNECTING) {
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001720 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 ||
1721 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1722 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
1723 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
1724 (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0 ||
1725 (r = sshpkt_send(ssh)) != 0)
1726 sshpkt_fatal(ssh, r, "%s: send reply", __func__);
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001727 }
Damien Millerbd483e72000-04-30 10:00:53 +10001728 } else {
1729 debug("failure %s", ctype);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001730 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 ||
1731 (r = sshpkt_put_u32(ssh, rchan)) != 0 ||
1732 (r = sshpkt_put_u32(ssh, SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED)) != 0 ||
1733 (r = sshpkt_put_cstring(ssh, "open failed")) != 0 ||
1734 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
1735 (r = sshpkt_send(ssh)) != 0)
1736 sshpkt_fatal(ssh, r, "%s: send failure", __func__);
Damien Millerbd483e72000-04-30 10:00:53 +10001737 }
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001738 r = 0;
1739 out:
Darren Tuckera627d422013-06-02 07:31:17 +10001740 free(ctype);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001741 return r;
Damien Millerbd483e72000-04-30 10:00:53 +10001742}
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001743
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001744static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001745client_input_channel_req(int type, u_int32_t seq, struct ssh *ssh)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001746{
1747 Channel *c = NULL;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001748 char *rtype = NULL;
1749 u_char reply;
1750 u_int id, exitval;
1751 int r, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001752
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001753 if ((r = sshpkt_get_u32(ssh, &id)) != 0)
1754 return r;
1755 if (id <= INT_MAX)
1756 c = channel_lookup(ssh, id);
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001757 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00001758 return 0;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001759 if ((r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 ||
1760 (r = sshpkt_get_u8(ssh, &reply)) != 0)
1761 goto out;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001762
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001763 debug("client_input_channel_req: channel %u rtype %s reply %d",
Ben Lindstrom5b828322001-02-09 01:34:36 +00001764 id, rtype, reply);
1765
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001766 if (c == NULL) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001767 error("client_input_channel_req: channel %d: "
1768 "unknown channel", id);
Damien Millerbab9bd42008-05-19 16:06:47 +10001769 } else if (strcmp(rtype, "eow@openssh.com") == 0) {
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001770 if ((r = sshpkt_get_end(ssh)) != 0)
1771 goto out;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001772 chan_rcvd_eow(ssh, c);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001773 } else if (strcmp(rtype, "exit-status") == 0) {
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001774 if ((r = sshpkt_get_u32(ssh, &exitval)) != 0)
1775 goto out;
Damien Millere1537f92010-01-26 13:26:22 +11001776 if (c->ctl_chan != -1) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001777 mux_exit_message(ssh, c, exitval);
Damien Miller0e220db2004-06-15 10:34:08 +10001778 success = 1;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001779 } else if ((int)id == session_ident) {
Darren Tucker29440822010-01-08 17:08:35 +11001780 /* Record exit value of local session */
1781 success = 1;
1782 exit_status = exitval;
1783 } else {
Damien Miller36f57eb2010-01-30 17:28:34 +11001784 /* Probably for a mux channel that has already closed */
1785 debug("%s: no sink for exit-status on channel %d",
1786 __func__, id);
Damien Miller0e220db2004-06-15 10:34:08 +10001787 }
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001788 if ((r = sshpkt_get_end(ssh)) != 0)
1789 goto out;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001790 }
Damien Millerc5893782014-05-15 13:48:49 +10001791 if (reply && c != NULL && !(c->flags & CHAN_CLOSE_SENT)) {
djm@openbsd.org9f532292017-09-12 06:35:31 +00001792 if (!c->have_remote_id)
1793 fatal("%s: channel %d: no remote_id",
1794 __func__, c->self);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001795 if ((r = sshpkt_start(ssh, success ?
1796 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE)) != 0 ||
1797 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1798 (r = sshpkt_send(ssh)) != 0)
1799 sshpkt_fatal(ssh, r, "%s: send failure", __func__);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001800 }
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001801 r = 0;
1802 out:
Darren Tuckera627d422013-06-02 07:31:17 +10001803 free(rtype);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00001804 return r;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001805}
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001806
djm@openbsd.org523463a2015-02-16 22:13:32 +00001807struct hostkeys_update_ctx {
1808 /* The hostname and (optionally) IP address string for the server */
1809 char *host_str, *ip_str;
1810
1811 /*
1812 * Keys received from the server and a flag for each indicating
1813 * whether they already exist in known_hosts.
1814 * keys_seen is filled in by hostkeys_find() and later (for new
1815 * keys) by client_global_hostkeys_private_confirm().
1816 */
1817 struct sshkey **keys;
1818 int *keys_seen;
djm@openbsd.org84623e02018-06-26 02:02:36 +00001819 size_t nkeys, nnew;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001820
1821 /*
1822 * Keys that are in known_hosts, but were not present in the update
1823 * from the server (i.e. scheduled to be deleted).
1824 * Filled in by hostkeys_find().
1825 */
1826 struct sshkey **old_keys;
1827 size_t nold;
1828};
1829
1830static void
1831hostkeys_update_ctx_free(struct hostkeys_update_ctx *ctx)
1832{
1833 size_t i;
1834
1835 if (ctx == NULL)
1836 return;
1837 for (i = 0; i < ctx->nkeys; i++)
1838 sshkey_free(ctx->keys[i]);
1839 free(ctx->keys);
1840 free(ctx->keys_seen);
1841 for (i = 0; i < ctx->nold; i++)
1842 sshkey_free(ctx->old_keys[i]);
1843 free(ctx->old_keys);
1844 free(ctx->host_str);
1845 free(ctx->ip_str);
1846 free(ctx);
1847}
1848
1849static int
1850hostkeys_find(struct hostkey_foreach_line *l, void *_ctx)
1851{
1852 struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
1853 size_t i;
1854 struct sshkey **tmp;
1855
djm@openbsd.org873d3e72017-04-30 23:18:44 +00001856 if (l->status != HKF_STATUS_MATCHED || l->key == NULL)
djm@openbsd.org523463a2015-02-16 22:13:32 +00001857 return 0;
1858
1859 /* Mark off keys we've already seen for this host */
1860 for (i = 0; i < ctx->nkeys; i++) {
1861 if (sshkey_equal(l->key, ctx->keys[i])) {
1862 debug3("%s: found %s key at %s:%ld", __func__,
1863 sshkey_ssh_name(ctx->keys[i]), l->path, l->linenum);
1864 ctx->keys_seen[i] = 1;
1865 return 0;
1866 }
1867 }
1868 /* This line contained a key that not offered by the server */
1869 debug3("%s: deprecated %s key at %s:%ld", __func__,
1870 sshkey_ssh_name(l->key), l->path, l->linenum);
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001871 if ((tmp = recallocarray(ctx->old_keys, ctx->nold, ctx->nold + 1,
djm@openbsd.org523463a2015-02-16 22:13:32 +00001872 sizeof(*ctx->old_keys))) == NULL)
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001873 fatal("%s: recallocarray failed nold = %zu",
djm@openbsd.org523463a2015-02-16 22:13:32 +00001874 __func__, ctx->nold);
1875 ctx->old_keys = tmp;
1876 ctx->old_keys[ctx->nold++] = l->key;
1877 l->key = NULL;
1878
1879 return 0;
1880}
1881
1882static void
1883update_known_hosts(struct hostkeys_update_ctx *ctx)
1884{
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00001885 int r, was_raw = 0;
1886 int loglevel = options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK ?
djm@openbsd.org523463a2015-02-16 22:13:32 +00001887 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_VERBOSE;
1888 char *fp, *response;
1889 size_t i;
1890
1891 for (i = 0; i < ctx->nkeys; i++) {
1892 if (ctx->keys_seen[i] != 2)
1893 continue;
1894 if ((fp = sshkey_fingerprint(ctx->keys[i],
1895 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1896 fatal("%s: sshkey_fingerprint failed", __func__);
1897 do_log2(loglevel, "Learned new hostkey: %s %s",
1898 sshkey_type(ctx->keys[i]), fp);
1899 free(fp);
1900 }
1901 for (i = 0; i < ctx->nold; i++) {
1902 if ((fp = sshkey_fingerprint(ctx->old_keys[i],
1903 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1904 fatal("%s: sshkey_fingerprint failed", __func__);
1905 do_log2(loglevel, "Deprecating obsolete hostkey: %s %s",
1906 sshkey_type(ctx->old_keys[i]), fp);
1907 free(fp);
1908 }
1909 if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK) {
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00001910 if (get_saved_tio() != NULL) {
1911 leave_raw_mode(1);
1912 was_raw = 1;
1913 }
djm@openbsd.org523463a2015-02-16 22:13:32 +00001914 response = NULL;
1915 for (i = 0; !quit_pending && i < 3; i++) {
1916 free(response);
1917 response = read_passphrase("Accept updated hostkeys? "
1918 "(yes/no): ", RP_ECHO);
1919 if (strcasecmp(response, "yes") == 0)
1920 break;
1921 else if (quit_pending || response == NULL ||
1922 strcasecmp(response, "no") == 0) {
1923 options.update_hostkeys = 0;
1924 break;
1925 } else {
1926 do_log2(loglevel, "Please enter "
1927 "\"yes\" or \"no\"");
1928 }
1929 }
1930 if (quit_pending || i >= 3 || response == NULL)
1931 options.update_hostkeys = 0;
1932 free(response);
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00001933 if (was_raw)
1934 enter_raw_mode(1);
djm@openbsd.org523463a2015-02-16 22:13:32 +00001935 }
1936
1937 /*
1938 * Now that all the keys are verified, we can go ahead and replace
1939 * them in known_hosts (assuming SSH_UPDATE_HOSTKEYS_ASK didn't
1940 * cancel the operation).
1941 */
1942 if (options.update_hostkeys != 0 &&
1943 (r = hostfile_replace_entries(options.user_hostfiles[0],
1944 ctx->host_str, ctx->ip_str, ctx->keys, ctx->nkeys,
1945 options.hash_known_hosts, 0,
1946 options.fingerprint_hash)) != 0)
1947 error("%s: hostfile_replace_entries failed: %s",
1948 __func__, ssh_err(r));
1949}
1950
1951static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001952client_global_hostkeys_private_confirm(struct ssh *ssh, int type,
1953 u_int32_t seq, void *_ctx)
djm@openbsd.org523463a2015-02-16 22:13:32 +00001954{
djm@openbsd.org523463a2015-02-16 22:13:32 +00001955 struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
1956 size_t i, ndone;
1957 struct sshbuf *signdata;
djm@openbsd.org78607312017-12-18 23:16:23 +00001958 int r, kexsigtype, use_kexsigtype;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001959 const u_char *sig;
1960 size_t siglen;
1961
1962 if (ctx->nnew == 0)
1963 fatal("%s: ctx->nnew == 0", __func__); /* sanity */
1964 if (type != SSH2_MSG_REQUEST_SUCCESS) {
1965 error("Server failed to confirm ownership of "
1966 "private host keys");
1967 hostkeys_update_ctx_free(ctx);
1968 return;
1969 }
djm@openbsd.org78607312017-12-18 23:16:23 +00001970 kexsigtype = sshkey_type_plain(
1971 sshkey_type_from_name(ssh->kex->hostkey_alg));
1972
djm@openbsd.org523463a2015-02-16 22:13:32 +00001973 if ((signdata = sshbuf_new()) == NULL)
1974 fatal("%s: sshbuf_new failed", __func__);
1975 /* Don't want to accidentally accept an unbound signature */
1976 if (ssh->kex->session_id_len == 0)
1977 fatal("%s: ssh->kex->session_id_len == 0", __func__);
1978 /*
1979 * Expect a signature for each of the ctx->nnew private keys we
1980 * haven't seen before. They will be in the same order as the
1981 * ctx->keys where the corresponding ctx->keys_seen[i] == 0.
1982 */
1983 for (ndone = i = 0; i < ctx->nkeys; i++) {
1984 if (ctx->keys_seen[i])
1985 continue;
1986 /* Prepare data to be signed: session ID, unique string, key */
1987 sshbuf_reset(signdata);
djm@openbsd.org44732de2015-02-20 22:17:21 +00001988 if ( (r = sshbuf_put_cstring(signdata,
1989 "hostkeys-prove-00@openssh.com")) != 0 ||
1990 (r = sshbuf_put_string(signdata, ssh->kex->session_id,
djm@openbsd.org523463a2015-02-16 22:13:32 +00001991 ssh->kex->session_id_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +00001992 (r = sshkey_puts(ctx->keys[i], signdata)) != 0)
1993 fatal("%s: failed to prepare signature: %s",
1994 __func__, ssh_err(r));
1995 /* Extract and verify signature */
1996 if ((r = sshpkt_get_string_direct(ssh, &sig, &siglen)) != 0) {
1997 error("%s: couldn't parse message: %s",
1998 __func__, ssh_err(r));
1999 goto out;
2000 }
djm@openbsd.org78607312017-12-18 23:16:23 +00002001 /*
2002 * For RSA keys, prefer to use the signature type negotiated
2003 * during KEX to the default (SHA1).
2004 */
2005 use_kexsigtype = kexsigtype == KEY_RSA &&
2006 sshkey_type_plain(ctx->keys[i]->type) == KEY_RSA;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002007 if ((r = sshkey_verify(ctx->keys[i], sig, siglen,
djm@openbsd.org04c7e282017-12-18 02:25:15 +00002008 sshbuf_ptr(signdata), sshbuf_len(signdata),
djm@openbsd.org78607312017-12-18 23:16:23 +00002009 use_kexsigtype ? ssh->kex->hostkey_alg : NULL, 0)) != 0) {
djm@openbsd.org523463a2015-02-16 22:13:32 +00002010 error("%s: server gave bad signature for %s key %zu",
2011 __func__, sshkey_type(ctx->keys[i]), i);
2012 goto out;
2013 }
2014 /* Key is good. Mark it as 'seen' */
2015 ctx->keys_seen[i] = 2;
2016 ndone++;
2017 }
2018 if (ndone != ctx->nnew)
2019 fatal("%s: ndone != ctx->nnew (%zu / %zu)", __func__,
2020 ndone, ctx->nnew); /* Shouldn't happen */
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002021 if ((r = sshpkt_get_end(ssh)) != 0) {
2022 error("%s: protocol error", __func__);
2023 goto out;
2024 }
djm@openbsd.org523463a2015-02-16 22:13:32 +00002025
2026 /* Make the edits to known_hosts */
2027 update_known_hosts(ctx);
2028 out:
2029 hostkeys_update_ctx_free(ctx);
2030}
2031
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002032/*
djm@openbsd.org894221a2017-03-10 05:01:13 +00002033 * Returns non-zero if the key is accepted by HostkeyAlgorithms.
2034 * Made slightly less trivial by the multiple RSA signature algorithm names.
2035 */
2036static int
2037key_accepted_by_hostkeyalgs(const struct sshkey *key)
2038{
2039 const char *ktype = sshkey_ssh_name(key);
2040 const char *hostkeyalgs = options.hostkeyalgorithms != NULL ?
2041 options.hostkeyalgorithms : KEX_DEFAULT_PK_ALG;
2042
2043 if (key == NULL || key->type == KEY_UNSPEC)
2044 return 0;
2045 if (key->type == KEY_RSA &&
2046 (match_pattern_list("rsa-sha2-256", hostkeyalgs, 0) == 1 ||
2047 match_pattern_list("rsa-sha2-512", hostkeyalgs, 0) == 1))
2048 return 1;
2049 return match_pattern_list(ktype, hostkeyalgs, 0) == 1;
2050}
2051
2052/*
djm@openbsd.org44732de2015-02-20 22:17:21 +00002053 * Handle hostkeys-00@openssh.com global request to inform the client of all
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002054 * the server's hostkeys. The keys are checked against the user's
2055 * HostkeyAlgorithms preference before they are accepted.
2056 */
2057static int
2058client_input_hostkeys(void)
2059{
djm@openbsd.org523463a2015-02-16 22:13:32 +00002060 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002061 const u_char *blob = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002062 size_t i, len = 0;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002063 struct sshbuf *buf = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002064 struct sshkey *key = NULL, **tmp;
2065 int r;
2066 char *fp;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002067 static int hostkeys_seen = 0; /* XXX use struct ssh */
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00002068 extern struct sockaddr_storage hostaddr; /* XXX from ssh.c */
djm@openbsd.orga63cfa22015-02-25 19:54:02 +00002069 struct hostkeys_update_ctx *ctx = NULL;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002070
2071 if (hostkeys_seen)
2072 fatal("%s: server already sent hostkeys", __func__);
djm@openbsd.org523463a2015-02-16 22:13:32 +00002073 if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK &&
2074 options.batch_mode)
2075 return 1; /* won't ask in batchmode, so don't even try */
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002076 if (!options.update_hostkeys || options.num_user_hostfiles <= 0)
2077 return 1;
djm@openbsd.orga63cfa22015-02-25 19:54:02 +00002078
2079 ctx = xcalloc(1, sizeof(*ctx));
djm@openbsd.org523463a2015-02-16 22:13:32 +00002080 while (ssh_packet_remaining(ssh) > 0) {
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002081 sshkey_free(key);
2082 key = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002083 if ((r = sshpkt_get_string_direct(ssh, &blob, &len)) != 0) {
2084 error("%s: couldn't parse message: %s",
2085 __func__, ssh_err(r));
2086 goto out;
2087 }
djm@openbsd.org44732de2015-02-20 22:17:21 +00002088 if ((r = sshkey_from_blob(blob, len, &key)) != 0) {
2089 error("%s: parse key: %s", __func__, ssh_err(r));
2090 goto out;
2091 }
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002092 fp = sshkey_fingerprint(key, options.fingerprint_hash,
2093 SSH_FP_DEFAULT);
2094 debug3("%s: received %s key %s", __func__,
2095 sshkey_type(key), fp);
2096 free(fp);
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00002097
djm@openbsd.org894221a2017-03-10 05:01:13 +00002098 if (!key_accepted_by_hostkeyalgs(key)) {
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002099 debug3("%s: %s key not permitted by HostkeyAlgorithms",
2100 __func__, sshkey_ssh_name(key));
2101 continue;
2102 }
djm@openbsd.org523463a2015-02-16 22:13:32 +00002103 /* Skip certs */
2104 if (sshkey_is_cert(key)) {
2105 debug3("%s: %s key is a certificate; skipping",
2106 __func__, sshkey_ssh_name(key));
2107 continue;
2108 }
2109 /* Ensure keys are unique */
2110 for (i = 0; i < ctx->nkeys; i++) {
2111 if (sshkey_equal(key, ctx->keys[i])) {
2112 error("%s: received duplicated %s host key",
2113 __func__, sshkey_ssh_name(key));
2114 goto out;
2115 }
2116 }
2117 /* Key is good, record it */
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00002118 if ((tmp = recallocarray(ctx->keys, ctx->nkeys, ctx->nkeys + 1,
djm@openbsd.org523463a2015-02-16 22:13:32 +00002119 sizeof(*ctx->keys))) == NULL)
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00002120 fatal("%s: recallocarray failed nkeys = %zu",
djm@openbsd.org523463a2015-02-16 22:13:32 +00002121 __func__, ctx->nkeys);
2122 ctx->keys = tmp;
2123 ctx->keys[ctx->nkeys++] = key;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002124 key = NULL;
2125 }
2126
djm@openbsd.org523463a2015-02-16 22:13:32 +00002127 if (ctx->nkeys == 0) {
djm@openbsd.org44732de2015-02-20 22:17:21 +00002128 debug("%s: server sent no hostkeys", __func__);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002129 goto out;
2130 }
djm@openbsd.org44732de2015-02-20 22:17:21 +00002131
djm@openbsd.org523463a2015-02-16 22:13:32 +00002132 if ((ctx->keys_seen = calloc(ctx->nkeys,
2133 sizeof(*ctx->keys_seen))) == NULL)
2134 fatal("%s: calloc failed", __func__);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002135
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00002136 get_hostfile_hostname_ipaddr(host,
2137 options.check_host_ip ? (struct sockaddr *)&hostaddr : NULL,
djm@openbsd.org523463a2015-02-16 22:13:32 +00002138 options.port, &ctx->host_str,
2139 options.check_host_ip ? &ctx->ip_str : NULL);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002140
djm@openbsd.org523463a2015-02-16 22:13:32 +00002141 /* Find which keys we already know about. */
2142 if ((r = hostkeys_foreach(options.user_hostfiles[0], hostkeys_find,
2143 ctx, ctx->host_str, ctx->ip_str,
2144 HKF_WANT_PARSE_KEY|HKF_WANT_MATCH)) != 0) {
2145 error("%s: hostkeys_foreach failed: %s", __func__, ssh_err(r));
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002146 goto out;
2147 }
2148
djm@openbsd.org523463a2015-02-16 22:13:32 +00002149 /* Figure out if we have any new keys to add */
2150 ctx->nnew = 0;
2151 for (i = 0; i < ctx->nkeys; i++) {
2152 if (!ctx->keys_seen[i])
2153 ctx->nnew++;
2154 }
2155
2156 debug3("%s: %zu keys from server: %zu new, %zu retained. %zu to remove",
2157 __func__, ctx->nkeys, ctx->nnew, ctx->nkeys - ctx->nnew, ctx->nold);
2158
2159 if (ctx->nnew == 0 && ctx->nold != 0) {
2160 /* We have some keys to remove. Just do it. */
2161 update_known_hosts(ctx);
2162 } else if (ctx->nnew != 0) {
2163 /*
2164 * We have received hitherto-unseen keys from the server.
2165 * Ask the server to confirm ownership of the private halves.
2166 */
2167 debug3("%s: asking server to prove ownership for %zu keys",
2168 __func__, ctx->nnew);
2169 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
2170 (r = sshpkt_put_cstring(ssh,
djm@openbsd.org44732de2015-02-20 22:17:21 +00002171 "hostkeys-prove-00@openssh.com")) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +00002172 (r = sshpkt_put_u8(ssh, 1)) != 0) /* bool: want reply */
2173 fatal("%s: cannot prepare packet: %s",
2174 __func__, ssh_err(r));
2175 if ((buf = sshbuf_new()) == NULL)
2176 fatal("%s: sshbuf_new", __func__);
2177 for (i = 0; i < ctx->nkeys; i++) {
2178 if (ctx->keys_seen[i])
2179 continue;
2180 sshbuf_reset(buf);
2181 if ((r = sshkey_putb(ctx->keys[i], buf)) != 0)
2182 fatal("%s: sshkey_putb: %s",
2183 __func__, ssh_err(r));
2184 if ((r = sshpkt_put_stringb(ssh, buf)) != 0)
2185 fatal("%s: sshpkt_put_string: %s",
2186 __func__, ssh_err(r));
2187 }
2188 if ((r = sshpkt_send(ssh)) != 0)
2189 fatal("%s: sshpkt_send: %s", __func__, ssh_err(r));
2190 client_register_global_confirm(
2191 client_global_hostkeys_private_confirm, ctx);
2192 ctx = NULL; /* will be freed in callback */
2193 }
2194
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002195 /* Success */
2196 out:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002197 hostkeys_update_ctx_free(ctx);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002198 sshkey_free(key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002199 sshbuf_free(buf);
djm@openbsd.org523463a2015-02-16 22:13:32 +00002200 /*
2201 * NB. Return success for all cases. The server doesn't need to know
2202 * what the client does with its hosts file.
2203 */
2204 return 1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002205}
2206
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002207static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002208client_input_global_request(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerc3fa4072002-01-22 23:21:58 +11002209{
2210 char *rtype;
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002211 u_char want_reply;
2212 int r, success = 0;
Damien Millerc3fa4072002-01-22 23:21:58 +11002213
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002214 if ((r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 ||
2215 (r = sshpkt_get_u8(ssh, &want_reply)) != 0)
2216 goto out;
Damien Miller509b0102003-12-17 16:33:10 +11002217 debug("client_input_global_request: rtype %s want_reply %d",
2218 rtype, want_reply);
djm@openbsd.org44732de2015-02-20 22:17:21 +00002219 if (strcmp(rtype, "hostkeys-00@openssh.com") == 0)
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002220 success = client_input_hostkeys();
Damien Millerc3fa4072002-01-22 23:21:58 +11002221 if (want_reply) {
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002222 if ((r = sshpkt_start(ssh, success ? SSH2_MSG_REQUEST_SUCCESS :
2223 SSH2_MSG_REQUEST_FAILURE)) != 0 ||
2224 (r = sshpkt_send(ssh)) != 0 ||
2225 (r = ssh_packet_write_wait(ssh)) != 0)
2226 goto out;
Damien Millerc3fa4072002-01-22 23:21:58 +11002227 }
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002228 r = 0;
2229 out:
Darren Tuckera627d422013-06-02 07:31:17 +10002230 free(rtype);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002231 return r;
Damien Millerc3fa4072002-01-22 23:21:58 +11002232}
Damien Millerbd483e72000-04-30 10:00:53 +10002233
Damien Miller0e220db2004-06-15 10:34:08 +10002234void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002235client_session2_setup(struct ssh *ssh, int id, int want_tty, int want_subsystem,
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002236 const char *term, struct termios *tiop, int in_fd, struct sshbuf *cmd,
2237 char **env)
Damien Miller0e220db2004-06-15 10:34:08 +10002238{
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002239 int i, j, matched, len, r;
djm@openbsd.org7082bb52018-06-09 03:01:12 +00002240 char *name, *val;
Darren Tucker5d78de62004-11-05 20:35:44 +11002241 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002242
2243 debug2("%s: id %d", __func__, id);
2244
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002245 if ((c = channel_lookup(ssh, id)) == NULL)
2246 fatal("%s: channel %d: unknown channel", __func__, id);
Darren Tucker5d78de62004-11-05 20:35:44 +11002247
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002248 ssh_packet_set_interactive(ssh, want_tty,
Damien Miller0dac6fb2010-11-20 15:19:38 +11002249 options.ip_qos_interactive, options.ip_qos_bulk);
2250
Damien Miller0e220db2004-06-15 10:34:08 +10002251 if (want_tty) {
2252 struct winsize ws;
Damien Miller0e220db2004-06-15 10:34:08 +10002253
2254 /* Store window size in the packet. */
2255 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
2256 memset(&ws, 0, sizeof(ws));
2257
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002258 channel_request_start(ssh, id, "pty-req", 1);
2259 client_expect_confirm(ssh, id, "PTY allocation", CONFIRM_TTY);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002260 if ((r = sshpkt_put_cstring(ssh, term != NULL ? term : ""))
2261 != 0 ||
2262 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_col)) != 0 ||
2263 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_row)) != 0 ||
2264 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_xpixel)) != 0 ||
2265 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_ypixel)) != 0)
2266 fatal("%s: %s", __func__, ssh_err(r));
Darren Tuckerdf189fb2008-06-08 12:55:32 +10002267 if (tiop == NULL)
2268 tiop = get_saved_tio();
markus@openbsd.org89dd6152018-07-09 21:20:26 +00002269 ssh_tty_make_modes(ssh, -1, tiop);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002270 if ((r = sshpkt_send(ssh)) != 0)
2271 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller0e220db2004-06-15 10:34:08 +10002272 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11002273 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10002274 }
2275
2276 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10002277 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10002278 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10002279 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10002280 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10002281 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10002282 if ((val = strchr(name, '=')) == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +10002283 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002284 continue;
2285 }
2286 *val++ = '\0';
2287
2288 matched = 0;
2289 for (j = 0; j < options.num_send_env; j++) {
2290 if (match_pattern(name, options.send_env[j])) {
2291 matched = 1;
2292 break;
2293 }
2294 }
2295 if (!matched) {
2296 debug3("Ignored env %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10002297 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002298 continue;
2299 }
2300
2301 debug("Sending env %s = %s", name, val);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002302 channel_request_start(ssh, id, "env", 0);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002303 if ((r = sshpkt_put_cstring(ssh, name)) != 0 ||
2304 (r = sshpkt_put_cstring(ssh, val)) != 0 ||
2305 (r = sshpkt_send(ssh)) != 0)
2306 fatal("%s: %s", __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +10002307 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002308 }
2309 }
djm@openbsd.org7082bb52018-06-09 03:01:12 +00002310 for (i = 0; i < options.num_setenv; i++) {
2311 /* Split */
2312 name = xstrdup(options.setenv[i]);
2313 if ((val = strchr(name, '=')) == NULL) {
2314 free(name);
2315 continue;
2316 }
2317 *val++ = '\0';
2318
2319 debug("Setting env %s = %s", name, val);
2320 channel_request_start(ssh, id, "env", 0);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002321 if ((r = sshpkt_put_cstring(ssh, name)) != 0 ||
2322 (r = sshpkt_put_cstring(ssh, val)) != 0 ||
2323 (r = sshpkt_send(ssh)) != 0)
2324 fatal("%s: %s", __func__, ssh_err(r));
djm@openbsd.org7082bb52018-06-09 03:01:12 +00002325 free(name);
2326 }
Damien Miller0e220db2004-06-15 10:34:08 +10002327
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002328 len = sshbuf_len(cmd);
Damien Miller0e220db2004-06-15 10:34:08 +10002329 if (len > 0) {
2330 if (len > 900)
2331 len = 900;
2332 if (want_subsystem) {
Damien Miller5771ed72008-05-19 15:35:33 +10002333 debug("Sending subsystem: %.*s",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002334 len, (const u_char*)sshbuf_ptr(cmd));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002335 channel_request_start(ssh, id, "subsystem", 1);
2336 client_expect_confirm(ssh, id, "subsystem",
2337 CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002338 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002339 debug("Sending command: %.*s",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002340 len, (const u_char*)sshbuf_ptr(cmd));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002341 channel_request_start(ssh, id, "exec", 1);
2342 client_expect_confirm(ssh, id, "exec", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002343 }
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002344 if ((r = sshpkt_put_stringb(ssh, cmd)) != 0 ||
2345 (r = sshpkt_send(ssh)) != 0)
2346 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller0e220db2004-06-15 10:34:08 +10002347 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002348 channel_request_start(ssh, id, "shell", 1);
2349 client_expect_confirm(ssh, id, "shell", CONFIRM_CLOSE);
djm@openbsd.org23f22a42019-01-19 21:33:57 +00002350 if ((r = sshpkt_send(ssh)) != 0)
2351 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller0e220db2004-06-15 10:34:08 +10002352 }
2353}
2354
Ben Lindstrombba81212001-06-25 05:01:22 +00002355static void
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002356client_init_dispatch(void)
Damien Miller1383bd82000-04-06 12:32:37 +10002357{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002358 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10002359
Damien Miller1383bd82000-04-06 12:32:37 +10002360 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2361 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2362 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2363 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10002364 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10002365 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2366 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002367 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10002368 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerb84886b2008-05-19 15:05:07 +10002369 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2370 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
Damien Millerc3fa4072002-01-22 23:21:58 +11002371 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002372
2373 /* rekeying */
2374 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10002375
2376 /* global request reply messages */
2377 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2378 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10002379}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002380
Damien Miller6c3eec72011-05-05 14:16:22 +10002381void
2382client_stop_mux(void)
2383{
2384 if (options.control_path != NULL && muxserver_sock != -1)
2385 unlink(options.control_path);
2386 /*
Darren Tucker48bf4b02012-09-07 16:38:53 +10002387 * If we are in persist mode, or don't have a shell, signal that we
2388 * should close when all active channels are closed.
Damien Miller6c3eec72011-05-05 14:16:22 +10002389 */
Darren Tucker48bf4b02012-09-07 16:38:53 +10002390 if (options.control_persist || no_shell_flag) {
Damien Miller6c3eec72011-05-05 14:16:22 +10002391 session_closed = 1;
Damien Miller4ac99c32011-06-20 14:43:31 +10002392 setproctitle("[stopped mux]");
2393 }
Damien Miller6c3eec72011-05-05 14:16:22 +10002394}
2395
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002396/* client specific fatal cleanup */
2397void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002398cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002399{
Damien Miller21771e22011-05-15 08:45:50 +10002400 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002401 leave_non_blocking();
Damien Millerb1cbfa22008-05-19 16:00:08 +10002402 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10002403 unlink(options.control_path);
Damien Millera41ccca2010-10-07 22:07:32 +11002404 ssh_kill_proxy_command();
Darren Tucker3e33cec2003-10-02 16:12:36 +10002405 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002406}