blob: 8d312cdaa75586aa996852898a795fc64d4711c1 [file] [log] [blame]
djm@openbsd.orgb5e412a2018-09-21 12:46:22 +00001/* $OpenBSD: clientloop.c,v 1.318 2018/09/21 12:46:22 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * The main loop for the interactive session (client side).
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
14 *
15 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 *
Damien Miller1383bd82000-04-06 12:32:37 +100038 * SSH2 support added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000039 * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110040 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110060 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
62#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110063
Damien Millerf17883e2006-03-15 11:45:54 +110064#include <sys/types.h>
Damien Miller9aec9192006-08-05 10:57:45 +100065#include <sys/ioctl.h>
Damien Millerf17883e2006-03-15 11:45:54 +110066#ifdef HAVE_SYS_STAT_H
67# include <sys/stat.h>
68#endif
Damien Miller9aec9192006-08-05 10:57:45 +100069#ifdef HAVE_SYS_TIME_H
70# include <sys/time.h>
71#endif
Damien Millere3b60b52006-07-10 21:08:03 +100072#include <sys/socket.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110073
Damien Millerc7b06362006-03-15 11:53:45 +110074#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100075#include <errno.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110076#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110077#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110078#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110079#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100080#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100081#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100082#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100083#include <string.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110084#include <termios.h>
Damien Millerd7834352006-08-05 12:39:39 +100085#include <pwd.h>
Damien Millere6b3b612006-07-24 14:01:23 +100086#include <unistd.h>
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000087#include <limits.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
Damien Millerb84886b2008-05-19 15:05:07 +100089#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100090#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000092#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093#include "packet.h"
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +000094#include "sshbuf.h"
Damien Millerb38eff82000-04-01 11:09:21 +100095#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000096#include "channels.h"
Damien Millerb38eff82000-04-01 11:09:21 +100097#include "dispatch.h"
markus@openbsd.org5467fbc2018-07-11 18:53:29 +000098#include "sshkey.h"
Damien Millerd7834352006-08-05 12:39:39 +100099#include "cipher.h"
Ben Lindstromf28f6342001-04-04 02:03:04 +0000100#include "kex.h"
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000101#include "myproposal.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000102#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +1000103#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000104#include "readconf.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000105#include "clientloop.h"
Damien Milleraeb31d62005-12-13 19:29:36 +1100106#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000107#include "authfd.h"
108#include "atomicio.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +1000109#include "sshpty.h"
Damien Miller0e220db2004-06-15 10:34:08 +1000110#include "match.h"
111#include "msg.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +0000112#include "ssherr.h"
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000113#include "hostfile.h"
Damien Miller69b69aa2000-10-28 14:19:58 +1100114
115/* import options */
116extern Options options;
117
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118/* Flag indicating that stdin should be redirected from /dev/null. */
119extern int stdin_null_flag;
120
Damien Millerd6965512003-12-17 16:31:53 +1100121/* Flag indicating that no shell has been requested */
122extern int no_shell_flag;
123
djm@openbsd.org368dd972016-07-23 02:54:08 +0000124/* Flag indicating that ssh should daemonise after authentication is complete */
125extern int fork_after_authentication_flag;
126
Damien Miller0e220db2004-06-15 10:34:08 +1000127/* Control socket */
Damien Millere1537f92010-01-26 13:26:22 +1100128extern int muxserver_sock; /* XXX use mux_client_cleanup() instead */
Damien Miller0e220db2004-06-15 10:34:08 +1000129
Damien Miller5428f641999-11-25 11:54:57 +1100130/*
131 * Name of the host we are connecting to. This is the name given on the
132 * command line, or the HostName specified for the user-supplied name in a
133 * configuration file.
134 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135extern char *host;
136
Damien Miller5428f641999-11-25 11:54:57 +1100137/*
138 * Flag to indicate that we have received a window change signal which has
139 * not yet been processed. This will cause a message indicating the new
140 * window size to be sent to the server a little later. This is volatile
141 * because this is updated in a signal handler.
142 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000143static volatile sig_atomic_t received_window_change_signal = 0;
144static volatile sig_atomic_t received_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145
Damien Miller788f2122005-11-05 15:14:59 +1100146/* Flag indicating whether the user's terminal is in non-blocking mode. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000147static int in_non_blocking_mode = 0;
148
Damien Millere11e1ea2010-08-03 16:04:46 +1000149/* Time when backgrounded control master using ControlPersist should exit */
150static time_t control_persist_exit_time = 0;
151
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152/* Common data for the client loop code. */
Damien Millere1537f92010-01-26 13:26:22 +1100153volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
Damien Miller95def091999-11-25 00:26:21 +1100154static int last_was_cr; /* Last character was a newline. */
Darren Tucker4d5cd332008-06-13 04:51:14 +1000155static int exit_status; /* Used to store the command exit status. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000156static struct sshbuf *stderr_buffer; /* Used for final exit message. */
Damien Miller95def091999-11-25 00:26:21 +1100157static int connection_in; /* Connection to server (input). */
158static int connection_out; /* Connection to server (output). */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000159static int need_rekeying; /* Set to non-zero if rekeying is requested. */
Damien Miller1ab6a512010-06-26 10:02:24 +1000160static int session_closed; /* In SSH2: login session closed. */
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000161static u_int x11_refuse_time; /* If >0, refuse x11 opens after this time. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162
Ben Lindstrombba81212001-06-25 05:01:22 +0000163static void client_init_dispatch(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000164int session_ident = -1;
165
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000166/* Track escape per proto2 channel */
167struct escape_filter_ctx {
168 int escape_pending;
169 int escape_char;
170};
171
172/* Context for channel confirmation replies */
Damien Miller5771ed72008-05-19 15:35:33 +1000173struct channel_reply_ctx {
174 const char *request_type;
Damien Miller555f3b82011-05-15 08:48:05 +1000175 int id;
176 enum confirm_action action;
Damien Miller5771ed72008-05-19 15:35:33 +1000177};
178
Darren Tucker9f407c42008-06-13 04:50:27 +1000179/* Global request success/failure callbacks */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000180/* XXX move to struct ssh? */
Darren Tucker9f407c42008-06-13 04:50:27 +1000181struct global_confirm {
182 TAILQ_ENTRY(global_confirm) entry;
183 global_confirm_cb *cb;
184 void *ctx;
185 int ref_count;
186};
187TAILQ_HEAD(global_confirms, global_confirm);
188static struct global_confirms global_confirms =
189 TAILQ_HEAD_INITIALIZER(global_confirms);
190
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000191void ssh_process_session2_setup(int, int, int, struct sshbuf *);
Damien Miller0e220db2004-06-15 10:34:08 +1000192
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193/* Restores stdin to blocking mode. */
194
Ben Lindstrombba81212001-06-25 05:01:22 +0000195static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000196leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197{
Damien Miller95def091999-11-25 00:26:21 +1100198 if (in_non_blocking_mode) {
Damien Miller03e66f62004-06-15 15:47:51 +1000199 unset_nonblock(fileno(stdin));
Damien Miller95def091999-11-25 00:26:21 +1100200 in_non_blocking_mode = 0;
Damien Miller95def091999-11-25 00:26:21 +1100201 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202}
203
Damien Miller5428f641999-11-25 11:54:57 +1100204/*
205 * Signal handler for the window change signal (SIGWINCH). This just sets a
206 * flag indicating that the window has changed.
207 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100208/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000209static void
Damien Miller95def091999-11-25 00:26:21 +1100210window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000211{
Damien Miller95def091999-11-25 00:26:21 +1100212 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000213}
214
Damien Miller5428f641999-11-25 11:54:57 +1100215/*
216 * Signal handler for signals that cause the program to terminate. These
217 * signals must be trapped to restore terminal modes.
218 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100219/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000220static void
Damien Miller95def091999-11-25 00:26:21 +1100221signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000222{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000223 received_signal = sig;
224 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225}
226
Damien Miller5428f641999-11-25 11:54:57 +1100227/*
Damien Millere11e1ea2010-08-03 16:04:46 +1000228 * Sets control_persist_exit_time to the absolute time when the
229 * backgrounded control master should exit due to expiry of the
230 * ControlPersist timeout. Sets it to 0 if we are not a backgrounded
231 * control master process, or if there is no ControlPersist timeout.
232 */
233static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000234set_control_persist_exit_time(struct ssh *ssh)
Damien Millere11e1ea2010-08-03 16:04:46 +1000235{
236 if (muxserver_sock == -1 || !options.control_persist
Damien Miller6c3eec72011-05-05 14:16:22 +1000237 || options.control_persist_timeout == 0) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000238 /* not using a ControlPersist timeout */
239 control_persist_exit_time = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000240 } else if (channel_still_open(ssh)) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000241 /* some client connections are still open */
242 if (control_persist_exit_time > 0)
243 debug2("%s: cancel scheduled exit", __func__);
244 control_persist_exit_time = 0;
245 } else if (control_persist_exit_time <= 0) {
246 /* a client connection has recently closed */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000247 control_persist_exit_time = monotime() +
Damien Millere11e1ea2010-08-03 16:04:46 +1000248 (time_t)options.control_persist_timeout;
249 debug2("%s: schedule exit in %d seconds", __func__,
250 options.control_persist_timeout);
251 }
252 /* else we are already counting down to the timeout */
253}
254
Damien Miller8d60be52012-02-11 08:18:17 +1100255#define SSH_X11_VALID_DISPLAY_CHARS ":/.-_"
256static int
257client_x11_display_valid(const char *display)
258{
259 size_t i, dlen;
260
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000261 if (display == NULL)
262 return 0;
263
Damien Miller8d60be52012-02-11 08:18:17 +1100264 dlen = strlen(display);
265 for (i = 0; i < dlen; i++) {
Damien Millerfdb23062013-11-21 13:57:15 +1100266 if (!isalnum((u_char)display[i]) &&
Damien Miller8d60be52012-02-11 08:18:17 +1100267 strchr(SSH_X11_VALID_DISPLAY_CHARS, display[i]) == NULL) {
268 debug("Invalid character '%c' in DISPLAY", display[i]);
269 return 0;
270 }
271 }
272 return 1;
273}
274
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000275#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
276#define X11_TIMEOUT_SLACK 60
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000277int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000278client_x11_get_proto(struct ssh *ssh, const char *display,
279 const char *xauth_path, u_int trusted, u_int timeout,
280 char **_proto, char **_data)
Damien Miller17e7ed02005-06-17 12:54:33 +1000281{
djm@openbsd.orgb5e412a2018-09-21 12:46:22 +0000282 char *cmd, line[512], xdisplay[512];
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000283 char xauthfile[PATH_MAX], xauthdir[PATH_MAX];
Damien Miller17e7ed02005-06-17 12:54:33 +1000284 static char proto[512], data[512];
285 FILE *f;
tedu@openbsd.org10363562016-09-17 18:00:27 +0000286 int got_data = 0, generated = 0, do_unlink = 0, r;
Damien Miller17e7ed02005-06-17 12:54:33 +1000287 struct stat st;
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000288 u_int now, x11_timeout_real;
Damien Miller17e7ed02005-06-17 12:54:33 +1000289
Damien Miller17e7ed02005-06-17 12:54:33 +1000290 *_proto = proto;
291 *_data = data;
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000292 proto[0] = data[0] = xauthfile[0] = xauthdir[0] = '\0';
Damien Miller17e7ed02005-06-17 12:54:33 +1000293
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000294 if (!client_x11_display_valid(display)) {
millert@openbsd.org5658ef22016-02-01 21:18:17 +0000295 if (display != NULL)
296 logit("DISPLAY \"%s\" invalid; disabling X11 forwarding",
297 display);
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000298 return -1;
299 }
300 if (xauth_path != NULL && stat(xauth_path, &st) == -1) {
301 debug("No xauth program.");
302 xauth_path = NULL;
303 }
304
305 if (xauth_path != NULL) {
Damien Miller17e7ed02005-06-17 12:54:33 +1000306 /*
307 * Handle FamilyLocal case where $DISPLAY does
308 * not match an authorization entry. For this we
309 * just try "xauth list unix:displaynum.screennum".
310 * XXX: "localhost" match to determine FamilyLocal
311 * is not perfect.
312 */
313 if (strncmp(display, "localhost:", 10) == 0) {
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000314 if ((r = snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
315 display + 10)) < 0 ||
316 (size_t)r >= sizeof(xdisplay)) {
317 error("%s: display name too long", __func__);
318 return -1;
319 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000320 display = xdisplay;
321 }
322 if (trusted == 0) {
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000323 /*
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000324 * Generate an untrusted X11 auth cookie.
325 *
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000326 * The authentication cookie should briefly outlive
327 * ssh's willingness to forward X11 connections to
328 * avoid nasty fail-open behaviour in the X server.
329 */
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000330 mktemp_proto(xauthdir, sizeof(xauthdir));
331 if (mkdtemp(xauthdir) == NULL) {
332 error("%s: mkdtemp: %s",
333 __func__, strerror(errno));
334 return -1;
335 }
336 do_unlink = 1;
337 if ((r = snprintf(xauthfile, sizeof(xauthfile),
338 "%s/xauthfile", xauthdir)) < 0 ||
339 (size_t)r >= sizeof(xauthfile)) {
340 error("%s: xauthfile path too long", __func__);
341 unlink(xauthfile);
342 rmdir(xauthdir);
343 return -1;
344 }
345
djm@openbsd.orgb5e412a2018-09-21 12:46:22 +0000346 if (timeout == 0) {
347 /* auth doesn't time out */
348 xasprintf(&cmd, "%s -f %s generate %s %s "
349 "untrusted 2>%s",
350 xauth_path, xauthfile, display,
351 SSH_X11_PROTO, _PATH_DEVNULL);
352 } else {
353 /* Add some slack to requested expiry */
354 if (timeout < UINT_MAX - X11_TIMEOUT_SLACK)
355 x11_timeout_real = timeout +
356 X11_TIMEOUT_SLACK;
357 else {
358 /* Don't overflow on long timeouts */
359 x11_timeout_real = UINT_MAX;
360 }
361 xasprintf(&cmd, "%s -f %s generate %s %s "
362 "untrusted timeout %u 2>%s",
363 xauth_path, xauthfile, display,
364 SSH_X11_PROTO, x11_timeout_real,
365 _PATH_DEVNULL);
366 }
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000367 debug2("%s: %s", __func__, cmd);
djm@openbsd.orgb5e412a2018-09-21 12:46:22 +0000368
369 if (timeout != 0 && x11_refuse_time == 0) {
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000370 now = monotime() + 1;
371 if (UINT_MAX - timeout < now)
372 x11_refuse_time = UINT_MAX;
373 else
374 x11_refuse_time = now + timeout;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000375 channel_set_x11_refuse_time(ssh,
376 x11_refuse_time);
Damien Miller17e7ed02005-06-17 12:54:33 +1000377 }
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000378 if (system(cmd) == 0)
379 generated = 1;
djm@openbsd.orgb5e412a2018-09-21 12:46:22 +0000380 free(cmd);
Damien Miller17e7ed02005-06-17 12:54:33 +1000381 }
Darren Tucker513d13a2007-08-15 19:13:41 +1000382
383 /*
384 * When in untrusted mode, we read the cookie only if it was
385 * successfully generated as an untrusted one in the step
386 * above.
387 */
388 if (trusted || generated) {
djm@openbsd.orgb5e412a2018-09-21 12:46:22 +0000389 xasprintf(&cmd,
Darren Tucker513d13a2007-08-15 19:13:41 +1000390 "%s %s%s list %s 2>" _PATH_DEVNULL,
391 xauth_path,
392 generated ? "-f " : "" ,
393 generated ? xauthfile : "",
394 display);
395 debug2("x11_get_proto: %s", cmd);
396 f = popen(cmd, "r");
397 if (f && fgets(line, sizeof(line), f) &&
398 sscanf(line, "%*s %511s %511s", proto, data) == 2)
399 got_data = 1;
400 if (f)
401 pclose(f);
djm@openbsd.orgb5e412a2018-09-21 12:46:22 +0000402 free(cmd);
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000403 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000404 }
405
406 if (do_unlink) {
407 unlink(xauthfile);
408 rmdir(xauthdir);
409 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000410
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000411 /* Don't fall back to fake X11 data for untrusted forwarding */
412 if (!trusted && !got_data) {
413 error("Warning: untrusted X11 forwarding setup failed: "
414 "xauth key data not generated");
415 return -1;
416 }
417
Damien Miller17e7ed02005-06-17 12:54:33 +1000418 /*
419 * If we didn't get authentication data, just make up some
420 * data. The forwarding code will check the validity of the
421 * response anyway, and substitute this data. The X11
422 * server, however, will ignore this fake data and use
423 * whatever authentication mechanisms it was using otherwise
424 * for the local connection.
425 */
426 if (!got_data) {
tedu@openbsd.org10363562016-09-17 18:00:27 +0000427 u_int8_t rnd[16];
428 u_int i;
Damien Miller17e7ed02005-06-17 12:54:33 +1000429
430 logit("Warning: No xauth data; "
431 "using fake authentication data for X11 forwarding.");
432 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
tedu@openbsd.org10363562016-09-17 18:00:27 +0000433 arc4random_buf(rnd, sizeof(rnd));
434 for (i = 0; i < sizeof(rnd); i++) {
Damien Miller17e7ed02005-06-17 12:54:33 +1000435 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
tedu@openbsd.org10363562016-09-17 18:00:27 +0000436 rnd[i]);
Damien Miller17e7ed02005-06-17 12:54:33 +1000437 }
438 }
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000439
440 return 0;
Damien Miller17e7ed02005-06-17 12:54:33 +1000441}
442
Damien Miller5428f641999-11-25 11:54:57 +1100443/*
Damien Miller5428f641999-11-25 11:54:57 +1100444 * Checks if the client window has changed, and sends a packet about it to
445 * the server if so. The actual change is detected elsewhere (by a software
446 * interrupt on Unix); this just checks the flag and sends a message if
447 * appropriate.
448 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000449
Ben Lindstrombba81212001-06-25 05:01:22 +0000450static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000451client_check_window_change(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000452{
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000453 if (!received_window_change_signal)
Damien Miller1383bd82000-04-06 12:32:37 +1000454 return;
455 /** XXX race */
456 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000458 debug2("%s: changed", __func__);
Damien Miller1383bd82000-04-06 12:32:37 +1000459
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000460 channel_send_window_changes(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461}
462
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000463static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000464client_global_request_reply(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller509b0102003-12-17 16:33:10 +1100465{
Darren Tucker9f407c42008-06-13 04:50:27 +1000466 struct global_confirm *gc;
467
468 if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000469 return 0;
Darren Tucker9f407c42008-06-13 04:50:27 +1000470 if (gc->cb != NULL)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000471 gc->cb(ssh, type, seq, gc->ctx);
Darren Tucker9f407c42008-06-13 04:50:27 +1000472 if (--gc->ref_count <= 0) {
473 TAILQ_REMOVE(&global_confirms, gc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +1100474 explicit_bzero(gc, sizeof(*gc));
Darren Tuckera627d422013-06-02 07:31:17 +1000475 free(gc);
Darren Tucker9f407c42008-06-13 04:50:27 +1000476 }
477
Darren Tuckerf7288d72009-06-21 18:12:20 +1000478 packet_set_alive_timeouts(0);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000479 return 0;
Damien Miller509b0102003-12-17 16:33:10 +1100480}
481
482static void
483server_alive_check(void)
484{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000485 if (packet_inc_alive_timeouts() > options.server_alive_count_max) {
Damien Millerb73b6fd2011-01-11 17:18:56 +1100486 logit("Timeout, server %s not responding.", host);
Damien Miller985a4482006-10-24 03:02:41 +1000487 cleanup_exit(255);
488 }
Damien Miller509b0102003-12-17 16:33:10 +1100489 packet_start(SSH2_MSG_GLOBAL_REQUEST);
490 packet_put_cstring("keepalive@openssh.com");
491 packet_put_char(1); /* boolean: want reply */
492 packet_send();
Darren Tucker9f407c42008-06-13 04:50:27 +1000493 /* Insert an empty placeholder to maintain ordering */
494 client_register_global_confirm(NULL, NULL);
Damien Miller509b0102003-12-17 16:33:10 +1100495}
496
Damien Miller5428f641999-11-25 11:54:57 +1100497/*
498 * Waits until the client can do something (some data becomes available on
499 * one of the file descriptors).
500 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000501static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000502client_wait_until_can_do_something(struct ssh *ssh,
503 fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000504 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000505{
Damien Miller509b0102003-12-17 16:33:10 +1100506 struct timeval tv, *tvp;
Damien Millere11e1ea2010-08-03 16:04:46 +1000507 int timeout_secs;
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000508 time_t minwait_secs = 0, server_alive_time = 0, now = monotime();
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000509 int r, ret;
Damien Miller509b0102003-12-17 16:33:10 +1100510
Damien Miller5e953212001-01-30 09:14:00 +1100511 /* Add any selections by the channel mechanism. */
djm@openbsd.org71e5a532017-08-30 03:59:08 +0000512 channel_prepare_select(active_state, readsetp, writesetp, maxfdp,
513 nallocp, &minwait_secs);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000514
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000515 /* channel_prepare_select could have closed the last channel */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000516 if (session_closed && !channel_still_open(ssh) &&
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000517 !packet_have_data_to_write()) {
518 /* clear mask since we did not call select() */
519 memset(*readsetp, 0, *nallocp);
520 memset(*writesetp, 0, *nallocp);
521 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000522 }
523
djm@openbsd.org05164352017-04-30 23:21:54 +0000524 FD_SET(connection_in, *readsetp);
525
Damien Miller95def091999-11-25 00:26:21 +1100526 /* Select server connection if have data to write to the server. */
527 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100528 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100529
Damien Miller5428f641999-11-25 11:54:57 +1100530 /*
531 * Wait for something to happen. This will suspend the process until
532 * some selected descriptor can be read, written, or has some other
Damien Millere11e1ea2010-08-03 16:04:46 +1000533 * event pending, or a timeout expires.
Damien Miller5428f641999-11-25 11:54:57 +1100534 */
Damien Miller95def091999-11-25 00:26:21 +1100535
Damien Millere11e1ea2010-08-03 16:04:46 +1000536 timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000537 if (options.server_alive_interval > 0) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000538 timeout_secs = options.server_alive_interval;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000539 server_alive_time = now + options.server_alive_interval;
540 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000541 if (options.rekey_interval > 0 && !rekeying)
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000542 timeout_secs = MINIMUM(timeout_secs, packet_get_rekey_timeout());
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000543 set_control_persist_exit_time(ssh);
Damien Millere11e1ea2010-08-03 16:04:46 +1000544 if (control_persist_exit_time > 0) {
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000545 timeout_secs = MINIMUM(timeout_secs,
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000546 control_persist_exit_time - now);
Damien Millere11e1ea2010-08-03 16:04:46 +1000547 if (timeout_secs < 0)
548 timeout_secs = 0;
549 }
Damien Millera6508752012-04-22 11:21:10 +1000550 if (minwait_secs != 0)
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000551 timeout_secs = MINIMUM(timeout_secs, (int)minwait_secs);
Damien Millere11e1ea2010-08-03 16:04:46 +1000552 if (timeout_secs == INT_MAX)
Damien Miller509b0102003-12-17 16:33:10 +1100553 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000554 else {
Damien Millere11e1ea2010-08-03 16:04:46 +1000555 tv.tv_sec = timeout_secs;
Damien Miller509b0102003-12-17 16:33:10 +1100556 tv.tv_usec = 0;
557 tvp = &tv;
558 }
Damien Millere11e1ea2010-08-03 16:04:46 +1000559
Damien Miller509b0102003-12-17 16:33:10 +1100560 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
561 if (ret < 0) {
Ben Lindstromf9452512001-02-15 03:12:08 +0000562 /*
563 * We have to clear the select masks, because we return.
564 * We have to return, because the mainloop checks for the flags
565 * set by the signal handlers.
566 */
Damien Miller79faeff2001-11-12 11:06:32 +1100567 memset(*readsetp, 0, *nallocp);
568 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000569
Damien Miller95def091999-11-25 00:26:21 +1100570 if (errno == EINTR)
571 return;
572 /* Note: we might still have data in the buffers. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000573 if ((r = sshbuf_putf(stderr_buffer,
574 "select: %s\r\n", strerror(errno))) != 0)
575 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +1100576 quit_pending = 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000577 } else if (ret == 0) {
578 /*
579 * Timeout. Could have been either keepalive or rekeying.
580 * Keepalive we check here, rekeying is checked in clientloop.
581 */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000582 if (server_alive_time != 0 && server_alive_time <= monotime())
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000583 server_alive_check();
584 }
585
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000586}
587
Ben Lindstrombba81212001-06-25 05:01:22 +0000588static void
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000589client_suspend_self(struct sshbuf *bin, struct sshbuf *bout, struct sshbuf *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000590{
Damien Miller95def091999-11-25 00:26:21 +1100591 /* Flush stdout and stderr buffers. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000592 if (sshbuf_len(bout) > 0)
593 atomicio(vwrite, fileno(stdout), sshbuf_mutable_ptr(bout),
594 sshbuf_len(bout));
595 if (sshbuf_len(berr) > 0)
596 atomicio(vwrite, fileno(stderr), sshbuf_mutable_ptr(berr),
597 sshbuf_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000598
Damien Miller21771e22011-05-15 08:45:50 +1000599 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000600
djm@openbsd.org5b8da1f2017-09-19 04:24:22 +0000601 sshbuf_reset(bin);
602 sshbuf_reset(bout);
603 sshbuf_reset(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000604
Damien Miller95def091999-11-25 00:26:21 +1100605 /* Send the suspend signal to the program itself. */
606 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000607
Darren Tucker5d78de62004-11-05 20:35:44 +1100608 /* Reset window sizes in case they have changed */
609 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000610
Damien Miller21771e22011-05-15 08:45:50 +1000611 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000612}
613
Ben Lindstrombba81212001-06-25 05:01:22 +0000614static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100615client_process_net_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000616{
Darren Tucker86e30a02009-08-28 11:21:06 +1000617 char buf[SSH_IOBUFSZ];
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000618 int r, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000619
Damien Miller5428f641999-11-25 11:54:57 +1100620 /*
621 * Read input from the server, and add any such data to the buffer of
622 * the packet subsystem.
623 */
Damien Miller95def091999-11-25 00:26:21 +1100624 if (FD_ISSET(connection_in, readset)) {
625 /* Read as much as possible. */
markus@openbsd.orga3068632016-01-14 16:17:39 +0000626 len = read(connection_in, buf, sizeof(buf));
627 if (len == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000628 /*
629 * Received EOF. The remote host has closed the
630 * connection.
631 */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000632 if ((r = sshbuf_putf(stderr_buffer,
Darren Tucker4d5cd332008-06-13 04:51:14 +1000633 "Connection to %.300s closed by remote host.\r\n",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000634 host)) != 0)
635 fatal("%s: buffer error: %s",
636 __func__, ssh_err(r));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000637 quit_pending = 1;
638 return;
Damien Miller95def091999-11-25 00:26:21 +1100639 }
Damien Miller5428f641999-11-25 11:54:57 +1100640 /*
641 * There is a kernel bug on Solaris that causes select to
642 * sometimes wake up even though there is no data available.
643 */
Damien Millerd8968ad2008-07-04 23:10:49 +1000644 if (len < 0 &&
645 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Damien Miller95def091999-11-25 00:26:21 +1100646 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000647
Damien Miller95def091999-11-25 00:26:21 +1100648 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000649 /*
650 * An error has encountered. Perhaps there is a
651 * network problem.
652 */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000653 if ((r = sshbuf_putf(stderr_buffer,
Darren Tucker4d5cd332008-06-13 04:51:14 +1000654 "Read from remote host %.300s: %.100s\r\n",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000655 host, strerror(errno))) != 0)
656 fatal("%s: buffer error: %s",
657 __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +1100658 quit_pending = 1;
659 return;
660 }
661 packet_process_incoming(buf, len);
662 }
Damien Miller1383bd82000-04-06 12:32:37 +1000663}
664
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000665static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000666client_status_confirm(struct ssh *ssh, int type, Channel *c, void *ctx)
Damien Miller0e220db2004-06-15 10:34:08 +1000667{
Damien Miller5771ed72008-05-19 15:35:33 +1000668 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
669 char errmsg[256];
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000670 int r, tochan;
Darren Tuckerfc959702004-07-17 16:12:08 +1000671
Damien Miller555f3b82011-05-15 08:48:05 +1000672 /*
673 * If a TTY was explicitly requested, then a failure to allocate
674 * one is fatal.
675 */
676 if (cr->action == CONFIRM_TTY &&
677 (options.request_tty == REQUEST_TTY_FORCE ||
678 options.request_tty == REQUEST_TTY_YES))
679 cr->action = CONFIRM_CLOSE;
680
djm@openbsd.org001aa552018-04-10 00:10:49 +0000681 /* XXX suppress on mux _client_ quietmode */
Damien Miller5771ed72008-05-19 15:35:33 +1000682 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
Damien Millere1537f92010-01-26 13:26:22 +1100683 c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
Damien Miller0e220db2004-06-15 10:34:08 +1000684
Damien Miller5771ed72008-05-19 15:35:33 +1000685 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
686 debug2("%s request accepted on channel %d",
687 cr->request_type, c->self);
688 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
689 if (tochan) {
690 snprintf(errmsg, sizeof(errmsg),
691 "%s request failed\r\n", cr->request_type);
692 } else {
693 snprintf(errmsg, sizeof(errmsg),
694 "%s request failed on channel %d",
695 cr->request_type, c->self);
696 }
697 /* If error occurred on primary session channel, then exit */
Damien Miller555f3b82011-05-15 08:48:05 +1000698 if (cr->action == CONFIRM_CLOSE && c->self == session_ident)
Damien Miller5771ed72008-05-19 15:35:33 +1000699 fatal("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000700 /*
701 * If error occurred on mux client, append to
702 * their stderr.
703 */
704 if (tochan) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000705 if ((r = sshbuf_put(c->extended, errmsg,
706 strlen(errmsg))) != 0)
707 fatal("%s: buffer error %s", __func__,
708 ssh_err(r));
Damien Miller555f3b82011-05-15 08:48:05 +1000709 } else
Damien Miller5771ed72008-05-19 15:35:33 +1000710 error("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000711 if (cr->action == CONFIRM_TTY) {
712 /*
713 * If a TTY allocation error occurred, then arrange
714 * for the correct TTY to leave raw mode.
715 */
716 if (c->self == session_ident)
717 leave_raw_mode(0);
718 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000719 mux_tty_alloc_failed(ssh, c);
Damien Miller555f3b82011-05-15 08:48:05 +1000720 } else if (cr->action == CONFIRM_CLOSE) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000721 chan_read_failed(ssh, c);
722 chan_write_failed(ssh, c);
Damien Miller5771ed72008-05-19 15:35:33 +1000723 }
Damien Miller0e220db2004-06-15 10:34:08 +1000724 }
Darren Tuckera627d422013-06-02 07:31:17 +1000725 free(cr);
Damien Miller5771ed72008-05-19 15:35:33 +1000726}
Damien Miller0e220db2004-06-15 10:34:08 +1000727
Damien Miller5771ed72008-05-19 15:35:33 +1000728static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000729client_abandon_status_confirm(struct ssh *ssh, Channel *c, void *ctx)
Damien Miller5771ed72008-05-19 15:35:33 +1000730{
Darren Tuckera627d422013-06-02 07:31:17 +1000731 free(ctx);
Damien Miller5771ed72008-05-19 15:35:33 +1000732}
733
Damien Miller6d7b4372011-06-23 08:31:57 +1000734void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000735client_expect_confirm(struct ssh *ssh, int id, const char *request,
Damien Miller555f3b82011-05-15 08:48:05 +1000736 enum confirm_action action)
Damien Miller5771ed72008-05-19 15:35:33 +1000737{
Damien Miller6c81fee2013-11-08 12:19:55 +1100738 struct channel_reply_ctx *cr = xcalloc(1, sizeof(*cr));
Damien Miller5771ed72008-05-19 15:35:33 +1000739
740 cr->request_type = request;
Damien Miller555f3b82011-05-15 08:48:05 +1000741 cr->action = action;
Damien Miller5771ed72008-05-19 15:35:33 +1000742
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000743 channel_register_status_confirm(ssh, id, client_status_confirm,
Damien Miller5771ed72008-05-19 15:35:33 +1000744 client_abandon_status_confirm, cr);
Damien Miller0e220db2004-06-15 10:34:08 +1000745}
746
Darren Tucker9f407c42008-06-13 04:50:27 +1000747void
748client_register_global_confirm(global_confirm_cb *cb, void *ctx)
749{
Damien Millerb9d3bee2008-07-16 22:40:52 +1000750 struct global_confirm *gc, *last_gc;
Darren Tucker9f407c42008-06-13 04:50:27 +1000751
752 /* Coalesce identical callbacks */
Damien Millerb9d3bee2008-07-16 22:40:52 +1000753 last_gc = TAILQ_LAST(&global_confirms, global_confirms);
754 if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
755 if (++last_gc->ref_count >= INT_MAX)
756 fatal("%s: last_gc->ref_count = %d",
757 __func__, last_gc->ref_count);
Darren Tucker9f407c42008-06-13 04:50:27 +1000758 return;
759 }
760
Damien Miller6c81fee2013-11-08 12:19:55 +1100761 gc = xcalloc(1, sizeof(*gc));
Darren Tucker9f407c42008-06-13 04:50:27 +1000762 gc->cb = cb;
763 gc->ctx = ctx;
764 gc->ref_count = 1;
765 TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
766}
767
Damien Miller0e220db2004-06-15 10:34:08 +1000768static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000769process_cmdline(struct ssh *ssh)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000770{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000771 void (*handler)(int);
Damien Miller7acefbb2014-07-18 14:11:24 +1000772 char *s, *cmd;
773 int ok, delete = 0, local = 0, remote = 0, dynamic = 0;
774 struct Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000775
Damien Miller1d2c4562014-02-04 11:18:20 +1100776 memset(&fwd, 0, sizeof(fwd));
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100777
Damien Miller21771e22011-05-15 08:45:50 +1000778 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000779 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000780 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000781 if (s == NULL)
782 goto out;
Damien Millerfdb23062013-11-21 13:57:15 +1100783 while (isspace((u_char)*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000784 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000785 if (*s == '-')
786 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000787 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000788 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000789
Darren Tucker1973c882004-05-24 10:34:36 +1000790 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000791 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000792 logit(" -L[bind_address:]port:host:hostport "
793 "Request local forward");
794 logit(" -R[bind_address:]port:host:hostport "
795 "Request remote forward");
Damien Miller0164cb82008-11-05 16:30:31 +1100796 logit(" -D[bind_address:]port "
797 "Request dynamic forward");
Damien Millerff773642011-09-22 21:39:48 +1000798 logit(" -KL[bind_address:]port "
799 "Cancel local forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000800 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000801 "Cancel remote forward");
Damien Millerff773642011-09-22 21:39:48 +1000802 logit(" -KD[bind_address:]port "
803 "Cancel dynamic forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100804 if (!options.permit_local_command)
805 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000806 logit(" !args "
807 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100808 goto out;
809 }
810
811 if (*s == '!' && options.permit_local_command) {
812 s++;
813 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000814 goto out;
815 }
816
817 if (*s == 'K') {
818 delete = 1;
819 s++;
820 }
Damien Miller0164cb82008-11-05 16:30:31 +1100821 if (*s == 'L')
822 local = 1;
823 else if (*s == 'R')
824 remote = 1;
825 else if (*s == 'D')
826 dynamic = 1;
827 else {
Damien Miller996acd22003-04-09 20:59:48 +1000828 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000829 goto out;
830 }
Damien Miller0164cb82008-11-05 16:30:31 +1100831
Damien Millerfdb23062013-11-21 13:57:15 +1100832 while (isspace((u_char)*++s))
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100833 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000834
Damien Millere1537f92010-01-26 13:26:22 +1100835 /* XXX update list of forwards in options */
Darren Tuckere7066df2004-05-24 10:18:05 +1000836 if (delete) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000837 /* We pass 1 for dynamicfwd to restrict to 1 or 2 fields. */
838 if (!parse_forward(&fwd, s, 1, 0)) {
839 logit("Bad forwarding close specification.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000840 goto out;
841 }
Damien Millerff773642011-09-22 21:39:48 +1000842 if (remote)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000843 ok = channel_request_rforward_cancel(ssh, &fwd) == 0;
Damien Millerff773642011-09-22 21:39:48 +1000844 else if (dynamic)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000845 ok = channel_cancel_lport_listener(ssh, &fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +1000846 0, &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000847 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000848 ok = channel_cancel_lport_listener(ssh, &fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +1000849 CHANNEL_CANCEL_PORT_STATIC,
850 &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000851 if (!ok) {
dtucker@openbsd.org9390b002017-01-29 21:35:23 +0000852 logit("Unknown port forwarding.");
Damien Millerff773642011-09-22 21:39:48 +1000853 goto out;
854 }
855 logit("Canceled forwarding.");
Darren Tuckere7066df2004-05-24 10:18:05 +1000856 } else {
Damien Miller4bf648f2009-02-14 16:28:21 +1100857 if (!parse_forward(&fwd, s, dynamic, remote)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000858 logit("Bad forwarding specification.");
859 goto out;
860 }
Damien Miller0164cb82008-11-05 16:30:31 +1100861 if (local || dynamic) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000862 if (!channel_setup_local_fwd_listener(ssh, &fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +1000863 &options.fwd_opts)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000864 logit("Port forwarding failed.");
865 goto out;
866 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100867 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000868 if (channel_request_remote_forwarding(ssh, &fwd) < 0) {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000869 logit("Port forwarding failed.");
870 goto out;
871 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100872 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000873 logit("Forwarding port.");
874 }
875
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000876out:
877 signal(SIGINT, handler);
Damien Miller21771e22011-05-15 08:45:50 +1000878 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tuckera627d422013-06-02 07:31:17 +1000879 free(cmd);
880 free(fwd.listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000881 free(fwd.listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000882 free(fwd.connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000883 free(fwd.connect_path);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000884}
885
Darren Tucker92a39cf2012-09-07 11:20:20 +1000886/* reasons to suppress output of an escape command in help output */
887#define SUPPRESS_NEVER 0 /* never suppress, always show */
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000888#define SUPPRESS_MUXCLIENT 1 /* don't show in mux client sessions */
889#define SUPPRESS_MUXMASTER 2 /* don't show in mux master sessions */
890#define SUPPRESS_SYSLOG 4 /* don't show when logging to syslog */
Darren Tucker92a39cf2012-09-07 11:20:20 +1000891struct escape_help_text {
892 const char *cmd;
893 const char *text;
894 unsigned int flags;
895};
896static struct escape_help_text esc_txt[] = {
897 {".", "terminate session", SUPPRESS_MUXMASTER},
898 {".", "terminate connection (and any multiplexed sessions)",
899 SUPPRESS_MUXCLIENT},
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000900 {"B", "send a BREAK to the remote system", SUPPRESS_NEVER},
Darren Tucker92a39cf2012-09-07 11:20:20 +1000901 {"C", "open a command line", SUPPRESS_MUXCLIENT},
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000902 {"R", "request rekey", SUPPRESS_NEVER},
Darren Tuckerca0d0fd2012-09-07 11:22:24 +1000903 {"V/v", "decrease/increase verbosity (LogLevel)", SUPPRESS_MUXCLIENT},
Darren Tucker92a39cf2012-09-07 11:20:20 +1000904 {"^Z", "suspend ssh", SUPPRESS_MUXCLIENT},
905 {"#", "list forwarded connections", SUPPRESS_NEVER},
906 {"&", "background ssh (when waiting for connections to terminate)",
907 SUPPRESS_MUXCLIENT},
908 {"?", "this message", SUPPRESS_NEVER},
909};
910
911static void
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000912print_escape_help(struct sshbuf *b, int escape_char, int mux_client,
913 int using_stderr)
Darren Tucker92a39cf2012-09-07 11:20:20 +1000914{
915 unsigned int i, suppress_flags;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000916 int r;
Darren Tucker92a39cf2012-09-07 11:20:20 +1000917
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000918 if ((r = sshbuf_putf(b,
919 "%c?\r\nSupported escape sequences:\r\n", escape_char)) != 0)
920 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker92a39cf2012-09-07 11:20:20 +1000921
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000922 suppress_flags =
Darren Tucker92a39cf2012-09-07 11:20:20 +1000923 (mux_client ? SUPPRESS_MUXCLIENT : 0) |
924 (mux_client ? 0 : SUPPRESS_MUXMASTER) |
925 (using_stderr ? 0 : SUPPRESS_SYSLOG);
926
927 for (i = 0; i < sizeof(esc_txt)/sizeof(esc_txt[0]); i++) {
928 if (esc_txt[i].flags & suppress_flags)
929 continue;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000930 if ((r = sshbuf_putf(b, " %c%-3s - %s\r\n",
931 escape_char, esc_txt[i].cmd, esc_txt[i].text)) != 0)
932 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker92a39cf2012-09-07 11:20:20 +1000933 }
934
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000935 if ((r = sshbuf_putf(b,
Darren Tuckerca0d0fd2012-09-07 11:22:24 +1000936 " %c%c - send the escape character by typing it twice\r\n"
Darren Tucker92a39cf2012-09-07 11:20:20 +1000937 "(Note that escapes are only recognized immediately after "
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000938 "newline.)\r\n", escape_char, escape_char)) != 0)
939 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker92a39cf2012-09-07 11:20:20 +1000940}
941
djm@openbsd.org84623e02018-06-26 02:02:36 +0000942/*
naddy@openbsd.org768405f2017-05-03 21:08:09 +0000943 * Process the characters one by one.
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000944 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000945static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000946process_escapes(struct ssh *ssh, Channel *c,
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000947 struct sshbuf *bin, struct sshbuf *bout, struct sshbuf *berr,
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000948 char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +1000949{
Damien Millerad833b32000-08-23 10:46:23 +1000950 pid_t pid;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000951 int r, bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000952 u_int i;
953 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +1000954 char *s;
djm@openbsd.org05164352017-04-30 23:21:54 +0000955 struct escape_filter_ctx *efc = c->filter_ctx == NULL ?
956 NULL : (struct escape_filter_ctx *)c->filter_ctx;
Damien Millerad833b32000-08-23 10:46:23 +1000957
djm@openbsd.org05164352017-04-30 23:21:54 +0000958 if (c->filter_ctx == NULL)
959 return 0;
djm@openbsd.org84623e02018-06-26 02:02:36 +0000960
Damien Millereccb9de2005-06-17 12:59:34 +1000961 if (len <= 0)
962 return (0);
963
964 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +1000965 /* Get one character at a time. */
966 ch = buf[i];
967
djm@openbsd.org05164352017-04-30 23:21:54 +0000968 if (efc->escape_pending) {
Damien Millerad833b32000-08-23 10:46:23 +1000969 /* We have previously seen an escape character. */
970 /* Clear the flag now. */
djm@openbsd.org05164352017-04-30 23:21:54 +0000971 efc->escape_pending = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000972
973 /* Process the escaped character. */
974 switch (ch) {
975 case '.':
976 /* Terminate the connection. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000977 if ((r = sshbuf_putf(berr, "%c.\r\n",
978 efc->escape_char)) != 0)
979 fatal("%s: buffer error: %s",
980 __func__, ssh_err(r));
Damien Millere1537f92010-01-26 13:26:22 +1100981 if (c && c->ctl_chan != -1) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000982 chan_read_failed(ssh, c);
983 chan_write_failed(ssh, c);
984 if (c->detach_user) {
985 c->detach_user(ssh,
986 c->self, NULL);
987 }
Damien Miller36187092013-06-10 13:07:11 +1000988 c->type = SSH_CHANNEL_ABANDONED;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000989 sshbuf_reset(c->input);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000990 chan_ibuf_empty(ssh, c);
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000991 return 0;
992 } else
993 quit_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +1000994 return -1;
995
996 case 'Z' - 64:
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000997 /* XXX support this for mux clients */
Damien Millere1537f92010-01-26 13:26:22 +1100998 if (c && c->ctl_chan != -1) {
Darren Tuckerf111d402012-09-07 11:21:42 +1000999 char b[16];
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001000 noescape:
Darren Tuckerf111d402012-09-07 11:21:42 +10001001 if (ch == 'Z' - 64)
1002 snprintf(b, sizeof b, "^Z");
1003 else
1004 snprintf(b, sizeof b, "%c", ch);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001005 if ((r = sshbuf_putf(berr,
Darren Tuckerf111d402012-09-07 11:21:42 +10001006 "%c%s escape not available to "
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001007 "multiplexed sessions\r\n",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001008 efc->escape_char, b)) != 0)
1009 fatal("%s: buffer error: %s",
1010 __func__, ssh_err(r));
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001011 continue;
1012 }
Darren Tucker4d5cd332008-06-13 04:51:14 +10001013 /* Suspend the program. Inform the user */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001014 if ((r = sshbuf_putf(berr,
1015 "%c^Z [suspend ssh]\r\n",
1016 efc->escape_char)) != 0)
1017 fatal("%s: buffer error: %s",
1018 __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +10001019
1020 /* Restore terminal modes and suspend. */
1021 client_suspend_self(bin, bout, berr);
1022
1023 /* We have been continued. */
1024 continue;
1025
Damien Miller54c45982003-05-15 10:20:13 +10001026 case 'B':
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001027 if ((r = sshbuf_putf(berr,
1028 "%cB\r\n", efc->escape_char)) != 0)
1029 fatal("%s: buffer error: %s",
1030 __func__, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001031 channel_request_start(ssh, c->self, "break", 0);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001032 if ((r = sshpkt_put_u32(ssh, 1000)) != 0 ||
1033 (r = sshpkt_send(ssh)) != 0)
1034 fatal("%s: %s", __func__,
1035 ssh_err(r));
Damien Miller54c45982003-05-15 10:20:13 +10001036 continue;
1037
Ben Lindstromf28f6342001-04-04 02:03:04 +00001038 case 'R':
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001039 if (datafellows & SSH_BUG_NOREKEY)
1040 logit("Server does not "
1041 "support re-keying");
1042 else
1043 need_rekeying = 1;
Ben Lindstromf28f6342001-04-04 02:03:04 +00001044 continue;
1045
Darren Tucker50a48d02012-09-06 21:25:37 +10001046 case 'V':
1047 /* FALLTHROUGH */
1048 case 'v':
1049 if (c && c->ctl_chan != -1)
1050 goto noescape;
1051 if (!log_is_on_stderr()) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001052 if ((r = sshbuf_putf(berr,
Darren Tucker50a48d02012-09-06 21:25:37 +10001053 "%c%c [Logging to syslog]\r\n",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001054 efc->escape_char, ch)) != 0)
1055 fatal("%s: buffer error: %s",
1056 __func__, ssh_err(r));
Darren Tucker50a48d02012-09-06 21:25:37 +10001057 continue;
1058 }
1059 if (ch == 'V' && options.log_level >
1060 SYSLOG_LEVEL_QUIET)
1061 log_change_level(--options.log_level);
1062 if (ch == 'v' && options.log_level <
1063 SYSLOG_LEVEL_DEBUG3)
1064 log_change_level(++options.log_level);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001065 if ((r = sshbuf_putf(berr,
djm@openbsd.org05164352017-04-30 23:21:54 +00001066 "%c%c [LogLevel %s]\r\n",
1067 efc->escape_char, ch,
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001068 log_level_name(options.log_level))) != 0)
1069 fatal("%s: buffer error: %s",
1070 __func__, ssh_err(r));
Darren Tucker50a48d02012-09-06 21:25:37 +10001071 continue;
1072
Damien Millerad833b32000-08-23 10:46:23 +10001073 case '&':
Damien Millere1537f92010-01-26 13:26:22 +11001074 if (c && c->ctl_chan != -1)
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001075 goto noescape;
Damien Millerad833b32000-08-23 10:46:23 +10001076 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001077 * Detach the program (continue to serve
1078 * connections, but put in background and no
1079 * more new connections).
Damien Millerad833b32000-08-23 10:46:23 +10001080 */
Damien Miller96507ef2001-11-12 10:52:25 +11001081 /* Restore tty modes. */
Damien Miller21771e22011-05-15 08:45:50 +10001082 leave_raw_mode(
1083 options.request_tty == REQUEST_TTY_FORCE);
Damien Miller96507ef2001-11-12 10:52:25 +11001084
1085 /* Stop listening for new connections. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001086 channel_stop_listening(ssh);
Damien Miller96507ef2001-11-12 10:52:25 +11001087
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001088 if ((r = sshbuf_putf(berr,
1089 "%c& [backgrounded]\n", efc->escape_char))
1090 != 0)
1091 fatal("%s: buffer error: %s",
1092 __func__, ssh_err(r));
Damien Miller96507ef2001-11-12 10:52:25 +11001093
1094 /* Fork into background. */
1095 pid = fork();
1096 if (pid < 0) {
1097 error("fork: %.100s", strerror(errno));
1098 continue;
1099 }
1100 if (pid != 0) { /* This is the parent. */
1101 /* The parent just exits. */
1102 exit(0);
1103 }
1104 /* The child continues serving connections. */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001105 /* fake EOF on stdin */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001106 if ((r = sshbuf_put_u8(bin, 4)) != 0)
1107 fatal("%s: buffer error: %s",
1108 __func__, ssh_err(r));
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001109 return -1;
Damien Millerad833b32000-08-23 10:46:23 +10001110 case '?':
djm@openbsd.org05164352017-04-30 23:21:54 +00001111 print_escape_help(berr, efc->escape_char,
Darren Tucker92a39cf2012-09-07 11:20:20 +10001112 (c && c->ctl_chan != -1),
1113 log_is_on_stderr());
Damien Millerad833b32000-08-23 10:46:23 +10001114 continue;
1115
1116 case '#':
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001117 if ((r = sshbuf_putf(berr, "%c#\r\n",
1118 efc->escape_char)) != 0)
1119 fatal("%s: buffer error: %s",
1120 __func__, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001121 s = channel_open_message(ssh);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001122 if ((r = sshbuf_put(berr, s, strlen(s))) != 0)
1123 fatal("%s: buffer error: %s",
1124 __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +10001125 free(s);
Damien Millerad833b32000-08-23 10:46:23 +10001126 continue;
1127
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001128 case 'C':
Damien Millere1537f92010-01-26 13:26:22 +11001129 if (c && c->ctl_chan != -1)
Damien Miller586b0052008-12-09 14:11:32 +11001130 goto noescape;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001131 process_cmdline(ssh);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001132 continue;
1133
Damien Millerad833b32000-08-23 10:46:23 +10001134 default:
djm@openbsd.org05164352017-04-30 23:21:54 +00001135 if (ch != efc->escape_char) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001136 if ((r = sshbuf_put_u8(bin,
1137 efc->escape_char)) != 0)
1138 fatal("%s: buffer error: %s",
1139 __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +10001140 bytes++;
1141 }
1142 /* Escaped characters fall through here */
1143 break;
1144 }
1145 } else {
1146 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001147 * The previous character was not an escape char.
1148 * Check if this is an escape.
Damien Millerad833b32000-08-23 10:46:23 +10001149 */
djm@openbsd.org05164352017-04-30 23:21:54 +00001150 if (last_was_cr && ch == efc->escape_char) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001151 /*
1152 * It is. Set the flag and continue to
1153 * next character.
1154 */
djm@openbsd.org05164352017-04-30 23:21:54 +00001155 efc->escape_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001156 continue;
1157 }
1158 }
1159
1160 /*
1161 * Normal character. Record whether it was a newline,
1162 * and append it to the buffer.
1163 */
1164 last_was_cr = (ch == '\r' || ch == '\n');
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001165 if ((r = sshbuf_put_u8(bin, ch)) != 0)
1166 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +10001167 bytes++;
1168 }
1169 return bytes;
1170}
1171
Damien Miller5428f641999-11-25 11:54:57 +11001172/*
Damien Millerb38eff82000-04-01 11:09:21 +10001173 * Get packets from the connection input buffer, and process them as long as
1174 * there are packets available.
1175 *
1176 * Any unknown packets received during the actual
1177 * session cause the session to terminate. This is
1178 * intended to make debugging easier since no
1179 * confirmations are sent. Any compatible protocol
1180 * extensions must be negotiated during the
1181 * preparatory phase.
1182 */
1183
Ben Lindstrombba81212001-06-25 05:01:22 +00001184static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001185client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001186{
markus@openbsd.org92e9fe62017-05-31 07:00:13 +00001187 ssh_dispatch_run_fatal(active_state, DISPATCH_NONBLOCK, &quit_pending);
Damien Millerb38eff82000-04-01 11:09:21 +10001188}
1189
Damien Millerad833b32000-08-23 10:46:23 +10001190/* scan buf[] for '~' before sending data to the peer */
1191
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001192/* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1193void *
1194client_new_escape_filter_ctx(int escape_char)
1195{
1196 struct escape_filter_ctx *ret;
1197
Damien Miller6c81fee2013-11-08 12:19:55 +11001198 ret = xcalloc(1, sizeof(*ret));
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001199 ret->escape_pending = 0;
1200 ret->escape_char = escape_char;
1201 return (void *)ret;
1202}
1203
Darren Tucker84c56f52008-06-13 04:55:46 +10001204/* Free the escape filter context on channel free */
1205void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001206client_filter_cleanup(struct ssh *ssh, int cid, void *ctx)
Darren Tucker84c56f52008-06-13 04:55:46 +10001207{
Darren Tuckera627d422013-06-02 07:31:17 +10001208 free(ctx);
Darren Tucker84c56f52008-06-13 04:55:46 +10001209}
1210
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001211int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001212client_simple_escape_filter(struct ssh *ssh, Channel *c, char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001213{
Damien Miller5771ed72008-05-19 15:35:33 +10001214 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1215 return 0;
1216
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001217 return process_escapes(ssh, c, c->input, c->output, c->extended,
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001218 buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001219}
1220
Ben Lindstrombba81212001-06-25 05:01:22 +00001221static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001222client_channel_closed(struct ssh *ssh, int id, void *arg)
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001223{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001224 channel_cancel_cleanup(ssh, id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001225 session_closed = 1;
Damien Miller21771e22011-05-15 08:45:50 +10001226 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001227}
1228
Damien Millerb38eff82000-04-01 11:09:21 +10001229/*
Damien Miller5428f641999-11-25 11:54:57 +11001230 * Implements the interactive session with the server. This is called after
1231 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001232 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1233 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001234 */
Damien Miller4af51302000-04-16 11:18:38 +10001235int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001236client_loop(struct ssh *ssh, int have_pty, int escape_char_arg,
1237 int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001238{
Damien Miller5e953212001-01-30 09:14:00 +11001239 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001240 double start_time, total_time;
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001241 int r, max_fd = 0, max_fd2 = 0, len;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001242 u_int64_t ibytes, obytes;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001243 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001244 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001245
Damien Miller95def091999-11-25 00:26:21 +11001246 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001247
semarie@openbsd.orgd7d2bc92015-12-26 07:46:03 +00001248 if (options.control_master &&
djm@openbsd.org368dd972016-07-23 02:54:08 +00001249 !option_clear_or_none(options.control_path)) {
semarie@openbsd.orgd7d2bc92015-12-26 07:46:03 +00001250 debug("pledge: id");
tb@openbsd.org372807c2016-07-11 21:38:13 +00001251 if (pledge("stdio rpath wpath cpath unix inet dns recvfd proc exec id tty",
semarie@openbsd.orgd7d2bc92015-12-26 07:46:03 +00001252 NULL) == -1)
1253 fatal("%s pledge(): %s", __func__, strerror(errno));
1254
1255 } else if (options.forward_x11 || options.permit_local_command) {
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001256 debug("pledge: exec");
1257 if (pledge("stdio rpath wpath cpath unix inet dns proc exec tty",
1258 NULL) == -1)
1259 fatal("%s pledge(): %s", __func__, strerror(errno));
1260
1261 } else if (options.update_hostkeys) {
1262 debug("pledge: filesystem full");
1263 if (pledge("stdio rpath wpath cpath unix inet dns proc tty",
1264 NULL) == -1)
1265 fatal("%s pledge(): %s", __func__, strerror(errno));
1266
djm@openbsd.org368dd972016-07-23 02:54:08 +00001267 } else if (!option_clear_or_none(options.proxy_command) ||
1268 fork_after_authentication_flag) {
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001269 debug("pledge: proc");
1270 if (pledge("stdio cpath unix inet dns proc tty", NULL) == -1)
1271 fatal("%s pledge(): %s", __func__, strerror(errno));
1272
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001273 } else {
1274 debug("pledge: network");
mestre@openbsd.orge2386452017-06-23 07:24:48 +00001275 if (pledge("stdio unix inet dns proc tty", NULL) == -1)
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001276 fatal("%s pledge(): %s", __func__, strerror(errno));
1277 }
1278
dtucker@openbsd.org@openbsd.org2d638e92017-11-25 05:58:47 +00001279 start_time = monotime_double();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001280
Damien Miller95def091999-11-25 00:26:21 +11001281 /* Initialize variables. */
Damien Miller95def091999-11-25 00:26:21 +11001282 last_was_cr = 1;
1283 exit_status = -1;
Damien Miller95def091999-11-25 00:26:21 +11001284 connection_in = packet_get_connection_in();
1285 connection_out = packet_get_connection_out();
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001286 max_fd = MAXIMUM(connection_in, connection_out);
Damien Miller5e953212001-01-30 09:14:00 +11001287
Damien Miller95def091999-11-25 00:26:21 +11001288 quit_pending = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001289
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001290 /* Initialize buffer. */
1291 if ((stderr_buffer = sshbuf_new()) == NULL)
1292 fatal("%s: sshbuf_new failed", __func__);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001293
Damien Millerb38eff82000-04-01 11:09:21 +10001294 client_init_dispatch();
1295
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001296 /*
1297 * Set signal handlers, (e.g. to restore non-blocking mode)
1298 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1299 */
Darren Tucker07336da2004-11-05 20:02:16 +11001300 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1301 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001302 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1303 signal(SIGINT, signal_handler);
1304 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1305 signal(SIGQUIT, signal_handler);
1306 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1307 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001308 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001309
Damien Miller95def091999-11-25 00:26:21 +11001310 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001311 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001312
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001313 session_ident = ssh2_chan_id;
1314 if (session_ident != -1) {
1315 if (escape_char_arg != SSH_ESCAPECHAR_NONE) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001316 channel_register_filter(ssh, session_ident,
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001317 client_simple_escape_filter, NULL,
1318 client_filter_cleanup,
1319 client_new_escape_filter_ctx(
1320 escape_char_arg));
Damien Miller6c3eec72011-05-05 14:16:22 +10001321 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001322 channel_register_cleanup(ssh, session_ident,
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001323 client_channel_closed, 0);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001324 }
Damien Millerad833b32000-08-23 10:46:23 +10001325
Damien Miller95def091999-11-25 00:26:21 +11001326 /* Main loop of the client for the interactive session mode. */
1327 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001328
Damien Miller5428f641999-11-25 11:54:57 +11001329 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001330 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001331
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001332 if (session_closed && !channel_still_open(ssh))
Damien Miller1383bd82000-04-06 12:32:37 +10001333 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001334
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001335 if (ssh_packet_is_rekeying(ssh)) {
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001336 debug("rekeying in progress");
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001337 } else if (need_rekeying) {
1338 /* manual rekey request */
1339 debug("need rekeying");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001340 if ((r = kex_start_rekex(ssh)) != 0)
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001341 fatal("%s: kex_start_rekex: %s", __func__,
1342 ssh_err(r));
1343 need_rekeying = 0;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001344 } else {
1345 /*
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001346 * Make packets from buffered channel data, and
1347 * enqueue them for sending to the server.
1348 */
1349 if (packet_not_very_much_data_to_write())
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001350 channel_output_poll(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001351
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001352 /*
1353 * Check if the window size has changed, and buffer a
1354 * message about it to the server if so.
1355 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001356 client_check_window_change(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001357
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001358 if (quit_pending)
1359 break;
1360 }
Damien Miller5428f641999-11-25 11:54:57 +11001361 /*
1362 * Wait until we have something to do (something becomes
1363 * available on one of the descriptors).
1364 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001365 max_fd2 = max_fd;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001366 client_wait_until_can_do_something(ssh, &readset, &writeset,
1367 &max_fd2, &nalloc, ssh_packet_is_rekeying(ssh));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001368
Damien Miller95def091999-11-25 00:26:21 +11001369 if (quit_pending)
1370 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001371
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001372 /* Do channel operations unless rekeying in progress. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001373 if (!ssh_packet_is_rekeying(ssh))
1374 channel_after_select(ssh, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001375
Damien Miller1383bd82000-04-06 12:32:37 +10001376 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001377 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001378
Damien Miller1383bd82000-04-06 12:32:37 +10001379 if (quit_pending)
1380 break;
1381
Darren Tucker4d5cd332008-06-13 04:51:14 +10001382 /*
1383 * Send as much buffered packet data as possible to the
1384 * sender.
1385 */
Damien Miller5e953212001-01-30 09:14:00 +11001386 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001387 packet_write_poll();
Damien Millere11e1ea2010-08-03 16:04:46 +10001388
1389 /*
1390 * If we are a backgrounded control master, and the
1391 * timeout has expired without any active client
1392 * connections, then quit.
1393 */
1394 if (control_persist_exit_time > 0) {
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001395 if (monotime() >= control_persist_exit_time) {
Damien Millere11e1ea2010-08-03 16:04:46 +10001396 debug("ControlPersist timeout expired");
1397 break;
1398 }
1399 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001400 }
Darren Tuckera627d422013-06-02 07:31:17 +10001401 free(readset);
1402 free(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001403
Damien Miller95def091999-11-25 00:26:21 +11001404 /* Terminate the session. */
1405
1406 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001407 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001408
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001409 packet_start(SSH2_MSG_DISCONNECT);
1410 packet_put_int(SSH2_DISCONNECT_BY_APPLICATION);
1411 packet_put_cstring("disconnected by user");
1412 packet_put_cstring(""); /* language tag */
1413 packet_send();
1414 packet_write_wait();
Darren Tucker12b4a652009-06-21 18:14:48 +10001415
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001416 channel_free_all(ssh);
Damien Miller95def091999-11-25 00:26:21 +11001417
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001418 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001419 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001420
1421 /* restore blocking io */
1422 if (!isatty(fileno(stdin)))
1423 unset_nonblock(fileno(stdin));
1424 if (!isatty(fileno(stdout)))
1425 unset_nonblock(fileno(stdout));
1426 if (!isatty(fileno(stderr)))
1427 unset_nonblock(fileno(stderr));
1428
Damien Millerd6965512003-12-17 16:31:53 +11001429 /*
1430 * If there was no shell or command requested, there will be no remote
1431 * exit status to be returned. In that case, clear error code if the
1432 * connection was deliberately terminated at this end.
1433 */
1434 if (no_shell_flag && received_signal == SIGTERM) {
1435 received_signal = 0;
1436 exit_status = 0;
1437 }
1438
dtucker@openbsd.orgb1e72df2017-07-14 03:18:21 +00001439 if (received_signal) {
1440 verbose("Killed by signal %d.", (int) received_signal);
1441 cleanup_exit(0);
1442 }
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001443
1444 /*
1445 * In interactive mode (with pseudo tty) display a message indicating
1446 * that the connection has been closed.
1447 */
1448 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001449 if ((r = sshbuf_putf(stderr_buffer,
1450 "Connection to %.64s closed.\r\n", host)) != 0)
1451 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001452 }
1453
Damien Miller95def091999-11-25 00:26:21 +11001454 /* Output any buffered data for stderr. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001455 if (sshbuf_len(stderr_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001456 len = atomicio(vwrite, fileno(stderr),
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001457 (u_char *)sshbuf_ptr(stderr_buffer),
1458 sshbuf_len(stderr_buffer));
1459 if (len < 0 || (u_int)len != sshbuf_len(stderr_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001460 error("Write failed flushing stderr buffer.");
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001461 else if ((r = sshbuf_consume(stderr_buffer, len)) != 0)
1462 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11001463 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001464
Damien Miller95def091999-11-25 00:26:21 +11001465 /* Clear and free any buffers. */
jsg@openbsd.org458abc22016-01-23 05:31:35 +00001466 explicit_bzero(buf, sizeof(buf));
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001467 sshbuf_free(stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001468
Damien Miller95def091999-11-25 00:26:21 +11001469 /* Report bytes transferred, and transfer rates. */
dtucker@openbsd.org@openbsd.org2d638e92017-11-25 05:58:47 +00001470 total_time = monotime_double() - start_time;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001471 packet_get_bytes(&ibytes, &obytes);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001472 verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
Damien Miller821de0a2011-01-11 17:20:29 +11001473 (unsigned long long)obytes, (unsigned long long)ibytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001474 if (total_time > 0)
Damien Millerb61f3fc2008-07-11 17:36:48 +10001475 verbose("Bytes per second: sent %.1f, received %.1f",
1476 obytes / total_time, ibytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001477 /* Return the exit status of the program. */
1478 debug("Exit status %d", exit_status);
1479 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001480}
Damien Millerb38eff82000-04-01 11:09:21 +10001481
1482/*********/
1483
Ben Lindstrombba81212001-06-25 05:01:22 +00001484static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001485client_request_forwarded_tcpip(struct ssh *ssh, const char *request_type,
1486 int rchan, u_int rwindow, u_int rmaxpack)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001487{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001488 Channel *c = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00001489 struct sshbuf *b = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001490 char *listen_address, *originator_address;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001491 u_short listen_port, originator_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00001492 int r;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001493
1494 /* Get rest of the packet */
1495 listen_address = packet_get_string(NULL);
1496 listen_port = packet_get_int();
1497 originator_address = packet_get_string(NULL);
1498 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001499 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001500
Damien Miller7acefbb2014-07-18 14:11:24 +10001501 debug("%s: listen %s port %d, originator %s port %d", __func__,
1502 listen_address, listen_port, originator_address, originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001503
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001504 c = channel_connect_by_listen_address(ssh, listen_address, listen_port,
Damien Millerbd740252008-05-19 15:37:09 +10001505 "forwarded-tcpip", originator_address);
1506
markus@openbsd.org8d057842016-09-30 09:19:13 +00001507 if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) {
1508 if ((b = sshbuf_new()) == NULL) {
1509 error("%s: alloc reply", __func__);
1510 goto out;
1511 }
1512 /* reconstruct and send to muxclient */
1513 if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
1514 (r = sshbuf_put_u8(b, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
1515 (r = sshbuf_put_cstring(b, request_type)) != 0 ||
1516 (r = sshbuf_put_u32(b, rchan)) != 0 ||
1517 (r = sshbuf_put_u32(b, rwindow)) != 0 ||
1518 (r = sshbuf_put_u32(b, rmaxpack)) != 0 ||
1519 (r = sshbuf_put_cstring(b, listen_address)) != 0 ||
1520 (r = sshbuf_put_u32(b, listen_port)) != 0 ||
1521 (r = sshbuf_put_cstring(b, originator_address)) != 0 ||
1522 (r = sshbuf_put_u32(b, originator_port)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001523 (r = sshbuf_put_stringb(c->output, b)) != 0) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00001524 error("%s: compose for muxclient %s", __func__,
1525 ssh_err(r));
1526 goto out;
1527 }
1528 }
1529
1530 out:
1531 sshbuf_free(b);
Darren Tuckera627d422013-06-02 07:31:17 +10001532 free(originator_address);
1533 free(listen_address);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001534 return c;
1535}
1536
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001537static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001538client_request_forwarded_streamlocal(struct ssh *ssh,
1539 const char *request_type, int rchan)
Damien Miller7acefbb2014-07-18 14:11:24 +10001540{
1541 Channel *c = NULL;
1542 char *listen_path;
1543
1544 /* Get the remote path. */
1545 listen_path = packet_get_string(NULL);
1546 /* XXX: Skip reserved field for now. */
1547 if (packet_get_string_ptr(NULL) == NULL)
1548 fatal("%s: packet_get_string_ptr failed", __func__);
1549 packet_check_eom();
1550
1551 debug("%s: %s", __func__, listen_path);
1552
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001553 c = channel_connect_by_listen_path(ssh, listen_path,
Damien Miller7acefbb2014-07-18 14:11:24 +10001554 "forwarded-streamlocal@openssh.com", "forwarded-streamlocal");
1555 free(listen_path);
1556 return c;
1557}
1558
1559static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001560client_request_x11(struct ssh *ssh, const char *request_type, int rchan)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001561{
1562 Channel *c = NULL;
1563 char *originator;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001564 u_short originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001565 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001566
1567 if (!options.forward_x11) {
1568 error("Warning: ssh server tried X11 forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001569 error("Warning: this is probably a break-in attempt by a "
1570 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001571 return NULL;
1572 }
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001573 if (x11_refuse_time != 0 && (u_int)monotime() >= x11_refuse_time) {
Damien Miller1ab6a512010-06-26 10:02:24 +10001574 verbose("Rejected X11 connection after ForwardX11Timeout "
1575 "expired");
1576 return NULL;
1577 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001578 originator = packet_get_string(NULL);
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001579 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001580 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001581 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001582 debug("client_request_x11: request from %s %d", originator,
1583 originator_port);
Darren Tuckera627d422013-06-02 07:31:17 +10001584 free(originator);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001585 sock = x11_connect_display(ssh);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001586 if (sock < 0)
1587 return NULL;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001588 c = channel_new(ssh, "x11",
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001589 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001590 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001591 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001592 return c;
1593}
1594
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001595static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001596client_request_agent(struct ssh *ssh, const char *request_type, int rchan)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001597{
1598 Channel *c = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001599 int r, sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001600
1601 if (!options.forward_agent) {
1602 error("Warning: ssh server tried agent forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001603 error("Warning: this is probably a break-in attempt by a "
1604 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001605 return NULL;
1606 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001607 if ((r = ssh_get_authentication_socket(&sock)) != 0) {
1608 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1609 debug("%s: ssh_get_authentication_socket: %s",
1610 __func__, ssh_err(r));
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001611 return NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001612 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001613 c = channel_new(ssh, "authentication agent connection",
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001614 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001615 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001616 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001617 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001618 return c;
1619}
1620
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001621char *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001622client_request_tun_fwd(struct ssh *ssh, int tun_mode,
1623 int local_tun, int remote_tun)
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001624{
1625 Channel *c;
1626 int fd;
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001627 char *ifname = NULL;
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001628
1629 if (tun_mode == SSH_TUNMODE_NO)
1630 return 0;
1631
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001632 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1633
1634 /* Open local tunnel device */
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001635 if ((fd = tun_open(local_tun, tun_mode, &ifname)) == -1) {
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001636 error("Tunnel device open failed.");
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001637 return NULL;
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001638 }
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001639 debug("Tunnel forwarding using interface %s", ifname);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001640
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001641 c = channel_new(ssh, "tun", SSH_CHANNEL_OPENING, fd, fd, -1,
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001642 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1643 c->datagram = 1;
1644
1645#if defined(SSH_TUN_FILTER)
1646 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
Damien Miller871f1e42017-09-12 18:01:35 +10001647 channel_register_filter(ssh, c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +10001648 sys_tun_outfilter, NULL, NULL);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001649#endif
1650
1651 packet_start(SSH2_MSG_CHANNEL_OPEN);
1652 packet_put_cstring("tun@openssh.com");
1653 packet_put_int(c->self);
1654 packet_put_int(c->local_window_max);
1655 packet_put_int(c->local_maxpacket);
1656 packet_put_int(tun_mode);
1657 packet_put_int(remote_tun);
1658 packet_send();
1659
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001660 return ifname;
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001661}
1662
Damien Millerbd483e72000-04-30 10:00:53 +10001663/* XXXX move to generic input handler */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001664static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001665client_input_channel_open(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerbd483e72000-04-30 10:00:53 +10001666{
1667 Channel *c = NULL;
1668 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001669 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001670 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001671
1672 ctype = packet_get_string(&len);
1673 rchan = packet_get_int();
1674 rwindow = packet_get_int();
1675 rmaxpack = packet_get_int();
1676
Damien Millere247cc42000-05-07 12:03:14 +10001677 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001678 ctype, rchan, rwindow, rmaxpack);
1679
Damien Miller0bc1bd82000-11-13 22:57:25 +11001680 if (strcmp(ctype, "forwarded-tcpip") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001681 c = client_request_forwarded_tcpip(ssh, ctype, rchan, rwindow,
markus@openbsd.org8d057842016-09-30 09:19:13 +00001682 rmaxpack);
Damien Miller7acefbb2014-07-18 14:11:24 +10001683 } else if (strcmp(ctype, "forwarded-streamlocal@openssh.com") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001684 c = client_request_forwarded_streamlocal(ssh, ctype, rchan);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001685 } else if (strcmp(ctype, "x11") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001686 c = client_request_x11(ssh, ctype, rchan);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001687 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001688 c = client_request_agent(ssh, ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001689 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00001690 if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) {
1691 debug3("proxied to downstream: %s", ctype);
1692 } else if (c != NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10001693 debug("confirm %s", ctype);
1694 c->remote_id = rchan;
djm@openbsd.org9f532292017-09-12 06:35:31 +00001695 c->have_remote_id = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001696 c->remote_window = rwindow;
1697 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001698 if (c->type != SSH_CHANNEL_CONNECTING) {
1699 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1700 packet_put_int(c->remote_id);
1701 packet_put_int(c->self);
1702 packet_put_int(c->local_window);
1703 packet_put_int(c->local_maxpacket);
1704 packet_send();
1705 }
Damien Millerbd483e72000-04-30 10:00:53 +10001706 } else {
1707 debug("failure %s", ctype);
1708 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1709 packet_put_int(rchan);
1710 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001711 packet_put_cstring("open failed");
1712 packet_put_cstring("");
Damien Millerbd483e72000-04-30 10:00:53 +10001713 packet_send();
1714 }
Darren Tuckera627d422013-06-02 07:31:17 +10001715 free(ctype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001716 return 0;
Damien Millerbd483e72000-04-30 10:00:53 +10001717}
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001718
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001719static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001720client_input_channel_req(int type, u_int32_t seq, struct ssh *ssh)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001721{
1722 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001723 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001724 char *rtype;
1725
1726 id = packet_get_int();
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001727 c = channel_lookup(ssh, id);
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001728 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00001729 return 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001730 rtype = packet_get_string(NULL);
1731 reply = packet_get_char();
1732
1733 debug("client_input_channel_req: channel %d rtype %s reply %d",
1734 id, rtype, reply);
1735
Damien Miller3bbd8782004-06-18 22:23:22 +10001736 if (id == -1) {
1737 error("client_input_channel_req: request for channel -1");
markus@openbsd.org8d057842016-09-30 09:19:13 +00001738 } else if (c == NULL) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001739 error("client_input_channel_req: channel %d: "
1740 "unknown channel", id);
Damien Millerbab9bd42008-05-19 16:06:47 +10001741 } else if (strcmp(rtype, "eow@openssh.com") == 0) {
1742 packet_check_eom();
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001743 chan_rcvd_eow(ssh, c);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001744 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10001745 exitval = packet_get_int();
Damien Millere1537f92010-01-26 13:26:22 +11001746 if (c->ctl_chan != -1) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001747 mux_exit_message(ssh, c, exitval);
Damien Miller0e220db2004-06-15 10:34:08 +10001748 success = 1;
Darren Tucker29440822010-01-08 17:08:35 +11001749 } else if (id == session_ident) {
1750 /* Record exit value of local session */
1751 success = 1;
1752 exit_status = exitval;
1753 } else {
Damien Miller36f57eb2010-01-30 17:28:34 +11001754 /* Probably for a mux channel that has already closed */
1755 debug("%s: no sink for exit-status on channel %d",
1756 __func__, id);
Damien Miller0e220db2004-06-15 10:34:08 +10001757 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001758 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001759 }
Damien Millerc5893782014-05-15 13:48:49 +10001760 if (reply && c != NULL && !(c->flags & CHAN_CLOSE_SENT)) {
djm@openbsd.org9f532292017-09-12 06:35:31 +00001761 if (!c->have_remote_id)
1762 fatal("%s: channel %d: no remote_id",
1763 __func__, c->self);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001764 packet_start(success ?
1765 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller8533c782008-12-08 09:54:40 +11001766 packet_put_int(c->remote_id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001767 packet_send();
1768 }
Darren Tuckera627d422013-06-02 07:31:17 +10001769 free(rtype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001770 return 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001771}
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001772
djm@openbsd.org523463a2015-02-16 22:13:32 +00001773struct hostkeys_update_ctx {
1774 /* The hostname and (optionally) IP address string for the server */
1775 char *host_str, *ip_str;
1776
1777 /*
1778 * Keys received from the server and a flag for each indicating
1779 * whether they already exist in known_hosts.
1780 * keys_seen is filled in by hostkeys_find() and later (for new
1781 * keys) by client_global_hostkeys_private_confirm().
1782 */
1783 struct sshkey **keys;
1784 int *keys_seen;
djm@openbsd.org84623e02018-06-26 02:02:36 +00001785 size_t nkeys, nnew;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001786
1787 /*
1788 * Keys that are in known_hosts, but were not present in the update
1789 * from the server (i.e. scheduled to be deleted).
1790 * Filled in by hostkeys_find().
1791 */
1792 struct sshkey **old_keys;
1793 size_t nold;
1794};
1795
1796static void
1797hostkeys_update_ctx_free(struct hostkeys_update_ctx *ctx)
1798{
1799 size_t i;
1800
1801 if (ctx == NULL)
1802 return;
1803 for (i = 0; i < ctx->nkeys; i++)
1804 sshkey_free(ctx->keys[i]);
1805 free(ctx->keys);
1806 free(ctx->keys_seen);
1807 for (i = 0; i < ctx->nold; i++)
1808 sshkey_free(ctx->old_keys[i]);
1809 free(ctx->old_keys);
1810 free(ctx->host_str);
1811 free(ctx->ip_str);
1812 free(ctx);
1813}
1814
1815static int
1816hostkeys_find(struct hostkey_foreach_line *l, void *_ctx)
1817{
1818 struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
1819 size_t i;
1820 struct sshkey **tmp;
1821
djm@openbsd.org873d3e72017-04-30 23:18:44 +00001822 if (l->status != HKF_STATUS_MATCHED || l->key == NULL)
djm@openbsd.org523463a2015-02-16 22:13:32 +00001823 return 0;
1824
1825 /* Mark off keys we've already seen for this host */
1826 for (i = 0; i < ctx->nkeys; i++) {
1827 if (sshkey_equal(l->key, ctx->keys[i])) {
1828 debug3("%s: found %s key at %s:%ld", __func__,
1829 sshkey_ssh_name(ctx->keys[i]), l->path, l->linenum);
1830 ctx->keys_seen[i] = 1;
1831 return 0;
1832 }
1833 }
1834 /* This line contained a key that not offered by the server */
1835 debug3("%s: deprecated %s key at %s:%ld", __func__,
1836 sshkey_ssh_name(l->key), l->path, l->linenum);
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001837 if ((tmp = recallocarray(ctx->old_keys, ctx->nold, ctx->nold + 1,
djm@openbsd.org523463a2015-02-16 22:13:32 +00001838 sizeof(*ctx->old_keys))) == NULL)
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001839 fatal("%s: recallocarray failed nold = %zu",
djm@openbsd.org523463a2015-02-16 22:13:32 +00001840 __func__, ctx->nold);
1841 ctx->old_keys = tmp;
1842 ctx->old_keys[ctx->nold++] = l->key;
1843 l->key = NULL;
1844
1845 return 0;
1846}
1847
1848static void
1849update_known_hosts(struct hostkeys_update_ctx *ctx)
1850{
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00001851 int r, was_raw = 0;
1852 int loglevel = options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK ?
djm@openbsd.org523463a2015-02-16 22:13:32 +00001853 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_VERBOSE;
1854 char *fp, *response;
1855 size_t i;
1856
1857 for (i = 0; i < ctx->nkeys; i++) {
1858 if (ctx->keys_seen[i] != 2)
1859 continue;
1860 if ((fp = sshkey_fingerprint(ctx->keys[i],
1861 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1862 fatal("%s: sshkey_fingerprint failed", __func__);
1863 do_log2(loglevel, "Learned new hostkey: %s %s",
1864 sshkey_type(ctx->keys[i]), fp);
1865 free(fp);
1866 }
1867 for (i = 0; i < ctx->nold; i++) {
1868 if ((fp = sshkey_fingerprint(ctx->old_keys[i],
1869 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1870 fatal("%s: sshkey_fingerprint failed", __func__);
1871 do_log2(loglevel, "Deprecating obsolete hostkey: %s %s",
1872 sshkey_type(ctx->old_keys[i]), fp);
1873 free(fp);
1874 }
1875 if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK) {
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00001876 if (get_saved_tio() != NULL) {
1877 leave_raw_mode(1);
1878 was_raw = 1;
1879 }
djm@openbsd.org523463a2015-02-16 22:13:32 +00001880 response = NULL;
1881 for (i = 0; !quit_pending && i < 3; i++) {
1882 free(response);
1883 response = read_passphrase("Accept updated hostkeys? "
1884 "(yes/no): ", RP_ECHO);
1885 if (strcasecmp(response, "yes") == 0)
1886 break;
1887 else if (quit_pending || response == NULL ||
1888 strcasecmp(response, "no") == 0) {
1889 options.update_hostkeys = 0;
1890 break;
1891 } else {
1892 do_log2(loglevel, "Please enter "
1893 "\"yes\" or \"no\"");
1894 }
1895 }
1896 if (quit_pending || i >= 3 || response == NULL)
1897 options.update_hostkeys = 0;
1898 free(response);
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00001899 if (was_raw)
1900 enter_raw_mode(1);
djm@openbsd.org523463a2015-02-16 22:13:32 +00001901 }
1902
1903 /*
1904 * Now that all the keys are verified, we can go ahead and replace
1905 * them in known_hosts (assuming SSH_UPDATE_HOSTKEYS_ASK didn't
1906 * cancel the operation).
1907 */
1908 if (options.update_hostkeys != 0 &&
1909 (r = hostfile_replace_entries(options.user_hostfiles[0],
1910 ctx->host_str, ctx->ip_str, ctx->keys, ctx->nkeys,
1911 options.hash_known_hosts, 0,
1912 options.fingerprint_hash)) != 0)
1913 error("%s: hostfile_replace_entries failed: %s",
1914 __func__, ssh_err(r));
1915}
1916
1917static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001918client_global_hostkeys_private_confirm(struct ssh *ssh, int type,
1919 u_int32_t seq, void *_ctx)
djm@openbsd.org523463a2015-02-16 22:13:32 +00001920{
djm@openbsd.org523463a2015-02-16 22:13:32 +00001921 struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
1922 size_t i, ndone;
1923 struct sshbuf *signdata;
djm@openbsd.org78607312017-12-18 23:16:23 +00001924 int r, kexsigtype, use_kexsigtype;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001925 const u_char *sig;
1926 size_t siglen;
1927
1928 if (ctx->nnew == 0)
1929 fatal("%s: ctx->nnew == 0", __func__); /* sanity */
1930 if (type != SSH2_MSG_REQUEST_SUCCESS) {
1931 error("Server failed to confirm ownership of "
1932 "private host keys");
1933 hostkeys_update_ctx_free(ctx);
1934 return;
1935 }
djm@openbsd.org78607312017-12-18 23:16:23 +00001936 kexsigtype = sshkey_type_plain(
1937 sshkey_type_from_name(ssh->kex->hostkey_alg));
1938
djm@openbsd.org523463a2015-02-16 22:13:32 +00001939 if ((signdata = sshbuf_new()) == NULL)
1940 fatal("%s: sshbuf_new failed", __func__);
1941 /* Don't want to accidentally accept an unbound signature */
1942 if (ssh->kex->session_id_len == 0)
1943 fatal("%s: ssh->kex->session_id_len == 0", __func__);
1944 /*
1945 * Expect a signature for each of the ctx->nnew private keys we
1946 * haven't seen before. They will be in the same order as the
1947 * ctx->keys where the corresponding ctx->keys_seen[i] == 0.
1948 */
1949 for (ndone = i = 0; i < ctx->nkeys; i++) {
1950 if (ctx->keys_seen[i])
1951 continue;
1952 /* Prepare data to be signed: session ID, unique string, key */
1953 sshbuf_reset(signdata);
djm@openbsd.org44732de2015-02-20 22:17:21 +00001954 if ( (r = sshbuf_put_cstring(signdata,
1955 "hostkeys-prove-00@openssh.com")) != 0 ||
1956 (r = sshbuf_put_string(signdata, ssh->kex->session_id,
djm@openbsd.org523463a2015-02-16 22:13:32 +00001957 ssh->kex->session_id_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +00001958 (r = sshkey_puts(ctx->keys[i], signdata)) != 0)
1959 fatal("%s: failed to prepare signature: %s",
1960 __func__, ssh_err(r));
1961 /* Extract and verify signature */
1962 if ((r = sshpkt_get_string_direct(ssh, &sig, &siglen)) != 0) {
1963 error("%s: couldn't parse message: %s",
1964 __func__, ssh_err(r));
1965 goto out;
1966 }
djm@openbsd.org78607312017-12-18 23:16:23 +00001967 /*
1968 * For RSA keys, prefer to use the signature type negotiated
1969 * during KEX to the default (SHA1).
1970 */
1971 use_kexsigtype = kexsigtype == KEY_RSA &&
1972 sshkey_type_plain(ctx->keys[i]->type) == KEY_RSA;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001973 if ((r = sshkey_verify(ctx->keys[i], sig, siglen,
djm@openbsd.org04c7e282017-12-18 02:25:15 +00001974 sshbuf_ptr(signdata), sshbuf_len(signdata),
djm@openbsd.org78607312017-12-18 23:16:23 +00001975 use_kexsigtype ? ssh->kex->hostkey_alg : NULL, 0)) != 0) {
djm@openbsd.org523463a2015-02-16 22:13:32 +00001976 error("%s: server gave bad signature for %s key %zu",
1977 __func__, sshkey_type(ctx->keys[i]), i);
1978 goto out;
1979 }
1980 /* Key is good. Mark it as 'seen' */
1981 ctx->keys_seen[i] = 2;
1982 ndone++;
1983 }
1984 if (ndone != ctx->nnew)
1985 fatal("%s: ndone != ctx->nnew (%zu / %zu)", __func__,
1986 ndone, ctx->nnew); /* Shouldn't happen */
1987 ssh_packet_check_eom(ssh);
1988
1989 /* Make the edits to known_hosts */
1990 update_known_hosts(ctx);
1991 out:
1992 hostkeys_update_ctx_free(ctx);
1993}
1994
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001995/*
djm@openbsd.org894221a2017-03-10 05:01:13 +00001996 * Returns non-zero if the key is accepted by HostkeyAlgorithms.
1997 * Made slightly less trivial by the multiple RSA signature algorithm names.
1998 */
1999static int
2000key_accepted_by_hostkeyalgs(const struct sshkey *key)
2001{
2002 const char *ktype = sshkey_ssh_name(key);
2003 const char *hostkeyalgs = options.hostkeyalgorithms != NULL ?
2004 options.hostkeyalgorithms : KEX_DEFAULT_PK_ALG;
2005
2006 if (key == NULL || key->type == KEY_UNSPEC)
2007 return 0;
2008 if (key->type == KEY_RSA &&
2009 (match_pattern_list("rsa-sha2-256", hostkeyalgs, 0) == 1 ||
2010 match_pattern_list("rsa-sha2-512", hostkeyalgs, 0) == 1))
2011 return 1;
2012 return match_pattern_list(ktype, hostkeyalgs, 0) == 1;
2013}
2014
2015/*
djm@openbsd.org44732de2015-02-20 22:17:21 +00002016 * Handle hostkeys-00@openssh.com global request to inform the client of all
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002017 * the server's hostkeys. The keys are checked against the user's
2018 * HostkeyAlgorithms preference before they are accepted.
2019 */
2020static int
2021client_input_hostkeys(void)
2022{
djm@openbsd.org523463a2015-02-16 22:13:32 +00002023 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002024 const u_char *blob = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002025 size_t i, len = 0;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002026 struct sshbuf *buf = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002027 struct sshkey *key = NULL, **tmp;
2028 int r;
2029 char *fp;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002030 static int hostkeys_seen = 0; /* XXX use struct ssh */
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00002031 extern struct sockaddr_storage hostaddr; /* XXX from ssh.c */
djm@openbsd.orga63cfa22015-02-25 19:54:02 +00002032 struct hostkeys_update_ctx *ctx = NULL;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002033
2034 if (hostkeys_seen)
2035 fatal("%s: server already sent hostkeys", __func__);
djm@openbsd.org523463a2015-02-16 22:13:32 +00002036 if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK &&
2037 options.batch_mode)
2038 return 1; /* won't ask in batchmode, so don't even try */
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002039 if (!options.update_hostkeys || options.num_user_hostfiles <= 0)
2040 return 1;
djm@openbsd.orga63cfa22015-02-25 19:54:02 +00002041
2042 ctx = xcalloc(1, sizeof(*ctx));
djm@openbsd.org523463a2015-02-16 22:13:32 +00002043 while (ssh_packet_remaining(ssh) > 0) {
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002044 sshkey_free(key);
2045 key = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002046 if ((r = sshpkt_get_string_direct(ssh, &blob, &len)) != 0) {
2047 error("%s: couldn't parse message: %s",
2048 __func__, ssh_err(r));
2049 goto out;
2050 }
djm@openbsd.org44732de2015-02-20 22:17:21 +00002051 if ((r = sshkey_from_blob(blob, len, &key)) != 0) {
2052 error("%s: parse key: %s", __func__, ssh_err(r));
2053 goto out;
2054 }
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002055 fp = sshkey_fingerprint(key, options.fingerprint_hash,
2056 SSH_FP_DEFAULT);
2057 debug3("%s: received %s key %s", __func__,
2058 sshkey_type(key), fp);
2059 free(fp);
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00002060
djm@openbsd.org894221a2017-03-10 05:01:13 +00002061 if (!key_accepted_by_hostkeyalgs(key)) {
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002062 debug3("%s: %s key not permitted by HostkeyAlgorithms",
2063 __func__, sshkey_ssh_name(key));
2064 continue;
2065 }
djm@openbsd.org523463a2015-02-16 22:13:32 +00002066 /* Skip certs */
2067 if (sshkey_is_cert(key)) {
2068 debug3("%s: %s key is a certificate; skipping",
2069 __func__, sshkey_ssh_name(key));
2070 continue;
2071 }
2072 /* Ensure keys are unique */
2073 for (i = 0; i < ctx->nkeys; i++) {
2074 if (sshkey_equal(key, ctx->keys[i])) {
2075 error("%s: received duplicated %s host key",
2076 __func__, sshkey_ssh_name(key));
2077 goto out;
2078 }
2079 }
2080 /* Key is good, record it */
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00002081 if ((tmp = recallocarray(ctx->keys, ctx->nkeys, ctx->nkeys + 1,
djm@openbsd.org523463a2015-02-16 22:13:32 +00002082 sizeof(*ctx->keys))) == NULL)
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00002083 fatal("%s: recallocarray failed nkeys = %zu",
djm@openbsd.org523463a2015-02-16 22:13:32 +00002084 __func__, ctx->nkeys);
2085 ctx->keys = tmp;
2086 ctx->keys[ctx->nkeys++] = key;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002087 key = NULL;
2088 }
2089
djm@openbsd.org523463a2015-02-16 22:13:32 +00002090 if (ctx->nkeys == 0) {
djm@openbsd.org44732de2015-02-20 22:17:21 +00002091 debug("%s: server sent no hostkeys", __func__);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002092 goto out;
2093 }
djm@openbsd.org44732de2015-02-20 22:17:21 +00002094
djm@openbsd.org523463a2015-02-16 22:13:32 +00002095 if ((ctx->keys_seen = calloc(ctx->nkeys,
2096 sizeof(*ctx->keys_seen))) == NULL)
2097 fatal("%s: calloc failed", __func__);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002098
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00002099 get_hostfile_hostname_ipaddr(host,
2100 options.check_host_ip ? (struct sockaddr *)&hostaddr : NULL,
djm@openbsd.org523463a2015-02-16 22:13:32 +00002101 options.port, &ctx->host_str,
2102 options.check_host_ip ? &ctx->ip_str : NULL);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002103
djm@openbsd.org523463a2015-02-16 22:13:32 +00002104 /* Find which keys we already know about. */
2105 if ((r = hostkeys_foreach(options.user_hostfiles[0], hostkeys_find,
2106 ctx, ctx->host_str, ctx->ip_str,
2107 HKF_WANT_PARSE_KEY|HKF_WANT_MATCH)) != 0) {
2108 error("%s: hostkeys_foreach failed: %s", __func__, ssh_err(r));
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002109 goto out;
2110 }
2111
djm@openbsd.org523463a2015-02-16 22:13:32 +00002112 /* Figure out if we have any new keys to add */
2113 ctx->nnew = 0;
2114 for (i = 0; i < ctx->nkeys; i++) {
2115 if (!ctx->keys_seen[i])
2116 ctx->nnew++;
2117 }
2118
2119 debug3("%s: %zu keys from server: %zu new, %zu retained. %zu to remove",
2120 __func__, ctx->nkeys, ctx->nnew, ctx->nkeys - ctx->nnew, ctx->nold);
2121
2122 if (ctx->nnew == 0 && ctx->nold != 0) {
2123 /* We have some keys to remove. Just do it. */
2124 update_known_hosts(ctx);
2125 } else if (ctx->nnew != 0) {
2126 /*
2127 * We have received hitherto-unseen keys from the server.
2128 * Ask the server to confirm ownership of the private halves.
2129 */
2130 debug3("%s: asking server to prove ownership for %zu keys",
2131 __func__, ctx->nnew);
2132 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
2133 (r = sshpkt_put_cstring(ssh,
djm@openbsd.org44732de2015-02-20 22:17:21 +00002134 "hostkeys-prove-00@openssh.com")) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +00002135 (r = sshpkt_put_u8(ssh, 1)) != 0) /* bool: want reply */
2136 fatal("%s: cannot prepare packet: %s",
2137 __func__, ssh_err(r));
2138 if ((buf = sshbuf_new()) == NULL)
2139 fatal("%s: sshbuf_new", __func__);
2140 for (i = 0; i < ctx->nkeys; i++) {
2141 if (ctx->keys_seen[i])
2142 continue;
2143 sshbuf_reset(buf);
2144 if ((r = sshkey_putb(ctx->keys[i], buf)) != 0)
2145 fatal("%s: sshkey_putb: %s",
2146 __func__, ssh_err(r));
2147 if ((r = sshpkt_put_stringb(ssh, buf)) != 0)
2148 fatal("%s: sshpkt_put_string: %s",
2149 __func__, ssh_err(r));
2150 }
2151 if ((r = sshpkt_send(ssh)) != 0)
2152 fatal("%s: sshpkt_send: %s", __func__, ssh_err(r));
2153 client_register_global_confirm(
2154 client_global_hostkeys_private_confirm, ctx);
2155 ctx = NULL; /* will be freed in callback */
2156 }
2157
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002158 /* Success */
2159 out:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002160 hostkeys_update_ctx_free(ctx);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002161 sshkey_free(key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002162 sshbuf_free(buf);
djm@openbsd.org523463a2015-02-16 22:13:32 +00002163 /*
2164 * NB. Return success for all cases. The server doesn't need to know
2165 * what the client does with its hosts file.
2166 */
2167 return 1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002168}
2169
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002170static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002171client_input_global_request(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerc3fa4072002-01-22 23:21:58 +11002172{
2173 char *rtype;
2174 int want_reply;
2175 int success = 0;
2176
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002177 rtype = packet_get_cstring(NULL);
Damien Millerc3fa4072002-01-22 23:21:58 +11002178 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11002179 debug("client_input_global_request: rtype %s want_reply %d",
2180 rtype, want_reply);
djm@openbsd.org44732de2015-02-20 22:17:21 +00002181 if (strcmp(rtype, "hostkeys-00@openssh.com") == 0)
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002182 success = client_input_hostkeys();
Damien Millerc3fa4072002-01-22 23:21:58 +11002183 if (want_reply) {
2184 packet_start(success ?
2185 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
2186 packet_send();
2187 packet_write_wait();
2188 }
Darren Tuckera627d422013-06-02 07:31:17 +10002189 free(rtype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002190 return 0;
Damien Millerc3fa4072002-01-22 23:21:58 +11002191}
Damien Millerbd483e72000-04-30 10:00:53 +10002192
Damien Miller0e220db2004-06-15 10:34:08 +10002193void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002194client_session2_setup(struct ssh *ssh, int id, int want_tty, int want_subsystem,
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002195 const char *term, struct termios *tiop, int in_fd, struct sshbuf *cmd,
2196 char **env)
Damien Miller0e220db2004-06-15 10:34:08 +10002197{
djm@openbsd.org7082bb52018-06-09 03:01:12 +00002198 int i, j, matched, len;
2199 char *name, *val;
Darren Tucker5d78de62004-11-05 20:35:44 +11002200 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002201
2202 debug2("%s: id %d", __func__, id);
2203
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002204 if ((c = channel_lookup(ssh, id)) == NULL)
2205 fatal("%s: channel %d: unknown channel", __func__, id);
Darren Tucker5d78de62004-11-05 20:35:44 +11002206
Damien Miller0dac6fb2010-11-20 15:19:38 +11002207 packet_set_interactive(want_tty,
2208 options.ip_qos_interactive, options.ip_qos_bulk);
2209
Damien Miller0e220db2004-06-15 10:34:08 +10002210 if (want_tty) {
2211 struct winsize ws;
Damien Miller0e220db2004-06-15 10:34:08 +10002212
2213 /* Store window size in the packet. */
2214 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
2215 memset(&ws, 0, sizeof(ws));
2216
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002217 channel_request_start(ssh, id, "pty-req", 1);
2218 client_expect_confirm(ssh, id, "PTY allocation", CONFIRM_TTY);
Damien Miller0e220db2004-06-15 10:34:08 +10002219 packet_put_cstring(term != NULL ? term : "");
Damien Miller71a73672006-03-26 14:04:36 +11002220 packet_put_int((u_int)ws.ws_col);
2221 packet_put_int((u_int)ws.ws_row);
2222 packet_put_int((u_int)ws.ws_xpixel);
2223 packet_put_int((u_int)ws.ws_ypixel);
Darren Tuckerdf189fb2008-06-08 12:55:32 +10002224 if (tiop == NULL)
2225 tiop = get_saved_tio();
markus@openbsd.org89dd6152018-07-09 21:20:26 +00002226 ssh_tty_make_modes(ssh, -1, tiop);
Damien Miller0e220db2004-06-15 10:34:08 +10002227 packet_send();
2228 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11002229 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10002230 }
2231
2232 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10002233 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10002234 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10002235 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10002236 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10002237 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10002238 if ((val = strchr(name, '=')) == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +10002239 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002240 continue;
2241 }
2242 *val++ = '\0';
2243
2244 matched = 0;
2245 for (j = 0; j < options.num_send_env; j++) {
2246 if (match_pattern(name, options.send_env[j])) {
2247 matched = 1;
2248 break;
2249 }
2250 }
2251 if (!matched) {
2252 debug3("Ignored env %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10002253 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002254 continue;
2255 }
2256
2257 debug("Sending env %s = %s", name, val);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002258 channel_request_start(ssh, id, "env", 0);
Damien Miller0e220db2004-06-15 10:34:08 +10002259 packet_put_cstring(name);
2260 packet_put_cstring(val);
2261 packet_send();
Darren Tuckera627d422013-06-02 07:31:17 +10002262 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002263 }
2264 }
djm@openbsd.org7082bb52018-06-09 03:01:12 +00002265 for (i = 0; i < options.num_setenv; i++) {
2266 /* Split */
2267 name = xstrdup(options.setenv[i]);
2268 if ((val = strchr(name, '=')) == NULL) {
2269 free(name);
2270 continue;
2271 }
2272 *val++ = '\0';
2273
2274 debug("Setting env %s = %s", name, val);
2275 channel_request_start(ssh, id, "env", 0);
2276 packet_put_cstring(name);
2277 packet_put_cstring(val);
2278 packet_send();
2279 free(name);
2280 }
Damien Miller0e220db2004-06-15 10:34:08 +10002281
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002282 len = sshbuf_len(cmd);
Damien Miller0e220db2004-06-15 10:34:08 +10002283 if (len > 0) {
2284 if (len > 900)
2285 len = 900;
2286 if (want_subsystem) {
Damien Miller5771ed72008-05-19 15:35:33 +10002287 debug("Sending subsystem: %.*s",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002288 len, (const u_char*)sshbuf_ptr(cmd));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002289 channel_request_start(ssh, id, "subsystem", 1);
2290 client_expect_confirm(ssh, id, "subsystem",
2291 CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002292 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002293 debug("Sending command: %.*s",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002294 len, (const u_char*)sshbuf_ptr(cmd));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002295 channel_request_start(ssh, id, "exec", 1);
2296 client_expect_confirm(ssh, id, "exec", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002297 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002298 packet_put_string(sshbuf_ptr(cmd), sshbuf_len(cmd));
Damien Miller0e220db2004-06-15 10:34:08 +10002299 packet_send();
2300 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002301 channel_request_start(ssh, id, "shell", 1);
2302 client_expect_confirm(ssh, id, "shell", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002303 packet_send();
2304 }
2305}
2306
Ben Lindstrombba81212001-06-25 05:01:22 +00002307static void
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002308client_init_dispatch(void)
Damien Miller1383bd82000-04-06 12:32:37 +10002309{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002310 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10002311
Damien Miller1383bd82000-04-06 12:32:37 +10002312 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2313 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2314 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2315 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10002316 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10002317 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2318 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002319 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10002320 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerb84886b2008-05-19 15:05:07 +10002321 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2322 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
Damien Millerc3fa4072002-01-22 23:21:58 +11002323 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002324
2325 /* rekeying */
2326 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10002327
2328 /* global request reply messages */
2329 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2330 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10002331}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002332
Damien Miller6c3eec72011-05-05 14:16:22 +10002333void
2334client_stop_mux(void)
2335{
2336 if (options.control_path != NULL && muxserver_sock != -1)
2337 unlink(options.control_path);
2338 /*
Darren Tucker48bf4b02012-09-07 16:38:53 +10002339 * If we are in persist mode, or don't have a shell, signal that we
2340 * should close when all active channels are closed.
Damien Miller6c3eec72011-05-05 14:16:22 +10002341 */
Darren Tucker48bf4b02012-09-07 16:38:53 +10002342 if (options.control_persist || no_shell_flag) {
Damien Miller6c3eec72011-05-05 14:16:22 +10002343 session_closed = 1;
Damien Miller4ac99c32011-06-20 14:43:31 +10002344 setproctitle("[stopped mux]");
2345 }
Damien Miller6c3eec72011-05-05 14:16:22 +10002346}
2347
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002348/* client specific fatal cleanup */
2349void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002350cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002351{
Damien Miller21771e22011-05-15 08:45:50 +10002352 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002353 leave_non_blocking();
Damien Millerb1cbfa22008-05-19 16:00:08 +10002354 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10002355 unlink(options.control_path);
Damien Millera41ccca2010-10-07 22:07:32 +11002356 ssh_kill_proxy_command();
Darren Tucker3e33cec2003-10-02 16:12:36 +10002357 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002358}