blob: ad35cb7ba398905e6d3a02d8ea00db790137e28c [file] [log] [blame]
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001/* $OpenBSD: clientloop.c,v 1.317 2018/07/11 18:53:29 markus 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.orged4ce822016-01-13 23:04:47 +0000282 char cmd[1024], line[512], xdisplay[512];
283 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.org1bf477d2015-07-01 02:26:31 +0000346 if (timeout >= UINT_MAX - X11_TIMEOUT_SLACK)
347 x11_timeout_real = UINT_MAX;
348 else
349 x11_timeout_real = timeout + X11_TIMEOUT_SLACK;
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000350 if ((r = snprintf(cmd, sizeof(cmd),
351 "%s -f %s generate %s " SSH_X11_PROTO
352 " untrusted timeout %u 2>" _PATH_DEVNULL,
353 xauth_path, xauthfile, display,
354 x11_timeout_real)) < 0 ||
355 (size_t)r >= sizeof(cmd))
356 fatal("%s: cmd too long", __func__);
357 debug2("%s: %s", __func__, cmd);
358 if (x11_refuse_time == 0) {
359 now = monotime() + 1;
360 if (UINT_MAX - timeout < now)
361 x11_refuse_time = UINT_MAX;
362 else
363 x11_refuse_time = now + timeout;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000364 channel_set_x11_refuse_time(ssh,
365 x11_refuse_time);
Damien Miller17e7ed02005-06-17 12:54:33 +1000366 }
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000367 if (system(cmd) == 0)
368 generated = 1;
Damien Miller17e7ed02005-06-17 12:54:33 +1000369 }
Darren Tucker513d13a2007-08-15 19:13:41 +1000370
371 /*
372 * When in untrusted mode, we read the cookie only if it was
373 * successfully generated as an untrusted one in the step
374 * above.
375 */
376 if (trusted || generated) {
377 snprintf(cmd, sizeof(cmd),
378 "%s %s%s list %s 2>" _PATH_DEVNULL,
379 xauth_path,
380 generated ? "-f " : "" ,
381 generated ? xauthfile : "",
382 display);
383 debug2("x11_get_proto: %s", cmd);
384 f = popen(cmd, "r");
385 if (f && fgets(line, sizeof(line), f) &&
386 sscanf(line, "%*s %511s %511s", proto, data) == 2)
387 got_data = 1;
388 if (f)
389 pclose(f);
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000390 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000391 }
392
393 if (do_unlink) {
394 unlink(xauthfile);
395 rmdir(xauthdir);
396 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000397
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000398 /* Don't fall back to fake X11 data for untrusted forwarding */
399 if (!trusted && !got_data) {
400 error("Warning: untrusted X11 forwarding setup failed: "
401 "xauth key data not generated");
402 return -1;
403 }
404
Damien Miller17e7ed02005-06-17 12:54:33 +1000405 /*
406 * If we didn't get authentication data, just make up some
407 * data. The forwarding code will check the validity of the
408 * response anyway, and substitute this data. The X11
409 * server, however, will ignore this fake data and use
410 * whatever authentication mechanisms it was using otherwise
411 * for the local connection.
412 */
413 if (!got_data) {
tedu@openbsd.org10363562016-09-17 18:00:27 +0000414 u_int8_t rnd[16];
415 u_int i;
Damien Miller17e7ed02005-06-17 12:54:33 +1000416
417 logit("Warning: No xauth data; "
418 "using fake authentication data for X11 forwarding.");
419 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
tedu@openbsd.org10363562016-09-17 18:00:27 +0000420 arc4random_buf(rnd, sizeof(rnd));
421 for (i = 0; i < sizeof(rnd); i++) {
Damien Miller17e7ed02005-06-17 12:54:33 +1000422 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
tedu@openbsd.org10363562016-09-17 18:00:27 +0000423 rnd[i]);
Damien Miller17e7ed02005-06-17 12:54:33 +1000424 }
425 }
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000426
427 return 0;
Damien Miller17e7ed02005-06-17 12:54:33 +1000428}
429
Damien Miller5428f641999-11-25 11:54:57 +1100430/*
Damien Miller5428f641999-11-25 11:54:57 +1100431 * Checks if the client window has changed, and sends a packet about it to
432 * the server if so. The actual change is detected elsewhere (by a software
433 * interrupt on Unix); this just checks the flag and sends a message if
434 * appropriate.
435 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436
Ben Lindstrombba81212001-06-25 05:01:22 +0000437static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000438client_check_window_change(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000439{
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000440 if (!received_window_change_signal)
Damien Miller1383bd82000-04-06 12:32:37 +1000441 return;
442 /** XXX race */
443 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000444
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000445 debug2("%s: changed", __func__);
Damien Miller1383bd82000-04-06 12:32:37 +1000446
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000447 channel_send_window_changes(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000448}
449
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000450static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000451client_global_request_reply(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller509b0102003-12-17 16:33:10 +1100452{
Darren Tucker9f407c42008-06-13 04:50:27 +1000453 struct global_confirm *gc;
454
455 if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000456 return 0;
Darren Tucker9f407c42008-06-13 04:50:27 +1000457 if (gc->cb != NULL)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000458 gc->cb(ssh, type, seq, gc->ctx);
Darren Tucker9f407c42008-06-13 04:50:27 +1000459 if (--gc->ref_count <= 0) {
460 TAILQ_REMOVE(&global_confirms, gc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +1100461 explicit_bzero(gc, sizeof(*gc));
Darren Tuckera627d422013-06-02 07:31:17 +1000462 free(gc);
Darren Tucker9f407c42008-06-13 04:50:27 +1000463 }
464
Darren Tuckerf7288d72009-06-21 18:12:20 +1000465 packet_set_alive_timeouts(0);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000466 return 0;
Damien Miller509b0102003-12-17 16:33:10 +1100467}
468
469static void
470server_alive_check(void)
471{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000472 if (packet_inc_alive_timeouts() > options.server_alive_count_max) {
Damien Millerb73b6fd2011-01-11 17:18:56 +1100473 logit("Timeout, server %s not responding.", host);
Damien Miller985a4482006-10-24 03:02:41 +1000474 cleanup_exit(255);
475 }
Damien Miller509b0102003-12-17 16:33:10 +1100476 packet_start(SSH2_MSG_GLOBAL_REQUEST);
477 packet_put_cstring("keepalive@openssh.com");
478 packet_put_char(1); /* boolean: want reply */
479 packet_send();
Darren Tucker9f407c42008-06-13 04:50:27 +1000480 /* Insert an empty placeholder to maintain ordering */
481 client_register_global_confirm(NULL, NULL);
Damien Miller509b0102003-12-17 16:33:10 +1100482}
483
Damien Miller5428f641999-11-25 11:54:57 +1100484/*
485 * Waits until the client can do something (some data becomes available on
486 * one of the file descriptors).
487 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000488static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000489client_wait_until_can_do_something(struct ssh *ssh,
490 fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000491 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000492{
Damien Miller509b0102003-12-17 16:33:10 +1100493 struct timeval tv, *tvp;
Damien Millere11e1ea2010-08-03 16:04:46 +1000494 int timeout_secs;
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000495 time_t minwait_secs = 0, server_alive_time = 0, now = monotime();
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000496 int r, ret;
Damien Miller509b0102003-12-17 16:33:10 +1100497
Damien Miller5e953212001-01-30 09:14:00 +1100498 /* Add any selections by the channel mechanism. */
djm@openbsd.org71e5a532017-08-30 03:59:08 +0000499 channel_prepare_select(active_state, readsetp, writesetp, maxfdp,
500 nallocp, &minwait_secs);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000501
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000502 /* channel_prepare_select could have closed the last channel */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000503 if (session_closed && !channel_still_open(ssh) &&
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000504 !packet_have_data_to_write()) {
505 /* clear mask since we did not call select() */
506 memset(*readsetp, 0, *nallocp);
507 memset(*writesetp, 0, *nallocp);
508 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000509 }
510
djm@openbsd.org05164352017-04-30 23:21:54 +0000511 FD_SET(connection_in, *readsetp);
512
Damien Miller95def091999-11-25 00:26:21 +1100513 /* Select server connection if have data to write to the server. */
514 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100515 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100516
Damien Miller5428f641999-11-25 11:54:57 +1100517 /*
518 * Wait for something to happen. This will suspend the process until
519 * some selected descriptor can be read, written, or has some other
Damien Millere11e1ea2010-08-03 16:04:46 +1000520 * event pending, or a timeout expires.
Damien Miller5428f641999-11-25 11:54:57 +1100521 */
Damien Miller95def091999-11-25 00:26:21 +1100522
Damien Millere11e1ea2010-08-03 16:04:46 +1000523 timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000524 if (options.server_alive_interval > 0) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000525 timeout_secs = options.server_alive_interval;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000526 server_alive_time = now + options.server_alive_interval;
527 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000528 if (options.rekey_interval > 0 && !rekeying)
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000529 timeout_secs = MINIMUM(timeout_secs, packet_get_rekey_timeout());
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000530 set_control_persist_exit_time(ssh);
Damien Millere11e1ea2010-08-03 16:04:46 +1000531 if (control_persist_exit_time > 0) {
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000532 timeout_secs = MINIMUM(timeout_secs,
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000533 control_persist_exit_time - now);
Damien Millere11e1ea2010-08-03 16:04:46 +1000534 if (timeout_secs < 0)
535 timeout_secs = 0;
536 }
Damien Millera6508752012-04-22 11:21:10 +1000537 if (minwait_secs != 0)
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000538 timeout_secs = MINIMUM(timeout_secs, (int)minwait_secs);
Damien Millere11e1ea2010-08-03 16:04:46 +1000539 if (timeout_secs == INT_MAX)
Damien Miller509b0102003-12-17 16:33:10 +1100540 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000541 else {
Damien Millere11e1ea2010-08-03 16:04:46 +1000542 tv.tv_sec = timeout_secs;
Damien Miller509b0102003-12-17 16:33:10 +1100543 tv.tv_usec = 0;
544 tvp = &tv;
545 }
Damien Millere11e1ea2010-08-03 16:04:46 +1000546
Damien Miller509b0102003-12-17 16:33:10 +1100547 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
548 if (ret < 0) {
Ben Lindstromf9452512001-02-15 03:12:08 +0000549 /*
550 * We have to clear the select masks, because we return.
551 * We have to return, because the mainloop checks for the flags
552 * set by the signal handlers.
553 */
Damien Miller79faeff2001-11-12 11:06:32 +1100554 memset(*readsetp, 0, *nallocp);
555 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000556
Damien Miller95def091999-11-25 00:26:21 +1100557 if (errno == EINTR)
558 return;
559 /* Note: we might still have data in the buffers. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000560 if ((r = sshbuf_putf(stderr_buffer,
561 "select: %s\r\n", strerror(errno))) != 0)
562 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +1100563 quit_pending = 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000564 } else if (ret == 0) {
565 /*
566 * Timeout. Could have been either keepalive or rekeying.
567 * Keepalive we check here, rekeying is checked in clientloop.
568 */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000569 if (server_alive_time != 0 && server_alive_time <= monotime())
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000570 server_alive_check();
571 }
572
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000573}
574
Ben Lindstrombba81212001-06-25 05:01:22 +0000575static void
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000576client_suspend_self(struct sshbuf *bin, struct sshbuf *bout, struct sshbuf *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000577{
Damien Miller95def091999-11-25 00:26:21 +1100578 /* Flush stdout and stderr buffers. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000579 if (sshbuf_len(bout) > 0)
580 atomicio(vwrite, fileno(stdout), sshbuf_mutable_ptr(bout),
581 sshbuf_len(bout));
582 if (sshbuf_len(berr) > 0)
583 atomicio(vwrite, fileno(stderr), sshbuf_mutable_ptr(berr),
584 sshbuf_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000585
Damien Miller21771e22011-05-15 08:45:50 +1000586 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000587
djm@openbsd.org5b8da1f2017-09-19 04:24:22 +0000588 sshbuf_reset(bin);
589 sshbuf_reset(bout);
590 sshbuf_reset(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000591
Damien Miller95def091999-11-25 00:26:21 +1100592 /* Send the suspend signal to the program itself. */
593 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000594
Darren Tucker5d78de62004-11-05 20:35:44 +1100595 /* Reset window sizes in case they have changed */
596 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000597
Damien Miller21771e22011-05-15 08:45:50 +1000598 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000599}
600
Ben Lindstrombba81212001-06-25 05:01:22 +0000601static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100602client_process_net_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000603{
Darren Tucker86e30a02009-08-28 11:21:06 +1000604 char buf[SSH_IOBUFSZ];
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000605 int r, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000606
Damien Miller5428f641999-11-25 11:54:57 +1100607 /*
608 * Read input from the server, and add any such data to the buffer of
609 * the packet subsystem.
610 */
Damien Miller95def091999-11-25 00:26:21 +1100611 if (FD_ISSET(connection_in, readset)) {
612 /* Read as much as possible. */
markus@openbsd.orga3068632016-01-14 16:17:39 +0000613 len = read(connection_in, buf, sizeof(buf));
614 if (len == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000615 /*
616 * Received EOF. The remote host has closed the
617 * connection.
618 */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000619 if ((r = sshbuf_putf(stderr_buffer,
Darren Tucker4d5cd332008-06-13 04:51:14 +1000620 "Connection to %.300s closed by remote host.\r\n",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000621 host)) != 0)
622 fatal("%s: buffer error: %s",
623 __func__, ssh_err(r));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000624 quit_pending = 1;
625 return;
Damien Miller95def091999-11-25 00:26:21 +1100626 }
Damien Miller5428f641999-11-25 11:54:57 +1100627 /*
628 * There is a kernel bug on Solaris that causes select to
629 * sometimes wake up even though there is no data available.
630 */
Damien Millerd8968ad2008-07-04 23:10:49 +1000631 if (len < 0 &&
632 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Damien Miller95def091999-11-25 00:26:21 +1100633 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000634
Damien Miller95def091999-11-25 00:26:21 +1100635 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000636 /*
637 * An error has encountered. Perhaps there is a
638 * network problem.
639 */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000640 if ((r = sshbuf_putf(stderr_buffer,
Darren Tucker4d5cd332008-06-13 04:51:14 +1000641 "Read from remote host %.300s: %.100s\r\n",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000642 host, strerror(errno))) != 0)
643 fatal("%s: buffer error: %s",
644 __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +1100645 quit_pending = 1;
646 return;
647 }
648 packet_process_incoming(buf, len);
649 }
Damien Miller1383bd82000-04-06 12:32:37 +1000650}
651
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000652static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000653client_status_confirm(struct ssh *ssh, int type, Channel *c, void *ctx)
Damien Miller0e220db2004-06-15 10:34:08 +1000654{
Damien Miller5771ed72008-05-19 15:35:33 +1000655 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
656 char errmsg[256];
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000657 int r, tochan;
Darren Tuckerfc959702004-07-17 16:12:08 +1000658
Damien Miller555f3b82011-05-15 08:48:05 +1000659 /*
660 * If a TTY was explicitly requested, then a failure to allocate
661 * one is fatal.
662 */
663 if (cr->action == CONFIRM_TTY &&
664 (options.request_tty == REQUEST_TTY_FORCE ||
665 options.request_tty == REQUEST_TTY_YES))
666 cr->action = CONFIRM_CLOSE;
667
djm@openbsd.org001aa552018-04-10 00:10:49 +0000668 /* XXX suppress on mux _client_ quietmode */
Damien Miller5771ed72008-05-19 15:35:33 +1000669 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
Damien Millere1537f92010-01-26 13:26:22 +1100670 c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
Damien Miller0e220db2004-06-15 10:34:08 +1000671
Damien Miller5771ed72008-05-19 15:35:33 +1000672 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
673 debug2("%s request accepted on channel %d",
674 cr->request_type, c->self);
675 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
676 if (tochan) {
677 snprintf(errmsg, sizeof(errmsg),
678 "%s request failed\r\n", cr->request_type);
679 } else {
680 snprintf(errmsg, sizeof(errmsg),
681 "%s request failed on channel %d",
682 cr->request_type, c->self);
683 }
684 /* If error occurred on primary session channel, then exit */
Damien Miller555f3b82011-05-15 08:48:05 +1000685 if (cr->action == CONFIRM_CLOSE && c->self == session_ident)
Damien Miller5771ed72008-05-19 15:35:33 +1000686 fatal("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000687 /*
688 * If error occurred on mux client, append to
689 * their stderr.
690 */
691 if (tochan) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000692 if ((r = sshbuf_put(c->extended, errmsg,
693 strlen(errmsg))) != 0)
694 fatal("%s: buffer error %s", __func__,
695 ssh_err(r));
Damien Miller555f3b82011-05-15 08:48:05 +1000696 } else
Damien Miller5771ed72008-05-19 15:35:33 +1000697 error("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000698 if (cr->action == CONFIRM_TTY) {
699 /*
700 * If a TTY allocation error occurred, then arrange
701 * for the correct TTY to leave raw mode.
702 */
703 if (c->self == session_ident)
704 leave_raw_mode(0);
705 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000706 mux_tty_alloc_failed(ssh, c);
Damien Miller555f3b82011-05-15 08:48:05 +1000707 } else if (cr->action == CONFIRM_CLOSE) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000708 chan_read_failed(ssh, c);
709 chan_write_failed(ssh, c);
Damien Miller5771ed72008-05-19 15:35:33 +1000710 }
Damien Miller0e220db2004-06-15 10:34:08 +1000711 }
Darren Tuckera627d422013-06-02 07:31:17 +1000712 free(cr);
Damien Miller5771ed72008-05-19 15:35:33 +1000713}
Damien Miller0e220db2004-06-15 10:34:08 +1000714
Damien Miller5771ed72008-05-19 15:35:33 +1000715static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000716client_abandon_status_confirm(struct ssh *ssh, Channel *c, void *ctx)
Damien Miller5771ed72008-05-19 15:35:33 +1000717{
Darren Tuckera627d422013-06-02 07:31:17 +1000718 free(ctx);
Damien Miller5771ed72008-05-19 15:35:33 +1000719}
720
Damien Miller6d7b4372011-06-23 08:31:57 +1000721void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000722client_expect_confirm(struct ssh *ssh, int id, const char *request,
Damien Miller555f3b82011-05-15 08:48:05 +1000723 enum confirm_action action)
Damien Miller5771ed72008-05-19 15:35:33 +1000724{
Damien Miller6c81fee2013-11-08 12:19:55 +1100725 struct channel_reply_ctx *cr = xcalloc(1, sizeof(*cr));
Damien Miller5771ed72008-05-19 15:35:33 +1000726
727 cr->request_type = request;
Damien Miller555f3b82011-05-15 08:48:05 +1000728 cr->action = action;
Damien Miller5771ed72008-05-19 15:35:33 +1000729
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000730 channel_register_status_confirm(ssh, id, client_status_confirm,
Damien Miller5771ed72008-05-19 15:35:33 +1000731 client_abandon_status_confirm, cr);
Damien Miller0e220db2004-06-15 10:34:08 +1000732}
733
Darren Tucker9f407c42008-06-13 04:50:27 +1000734void
735client_register_global_confirm(global_confirm_cb *cb, void *ctx)
736{
Damien Millerb9d3bee2008-07-16 22:40:52 +1000737 struct global_confirm *gc, *last_gc;
Darren Tucker9f407c42008-06-13 04:50:27 +1000738
739 /* Coalesce identical callbacks */
Damien Millerb9d3bee2008-07-16 22:40:52 +1000740 last_gc = TAILQ_LAST(&global_confirms, global_confirms);
741 if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
742 if (++last_gc->ref_count >= INT_MAX)
743 fatal("%s: last_gc->ref_count = %d",
744 __func__, last_gc->ref_count);
Darren Tucker9f407c42008-06-13 04:50:27 +1000745 return;
746 }
747
Damien Miller6c81fee2013-11-08 12:19:55 +1100748 gc = xcalloc(1, sizeof(*gc));
Darren Tucker9f407c42008-06-13 04:50:27 +1000749 gc->cb = cb;
750 gc->ctx = ctx;
751 gc->ref_count = 1;
752 TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
753}
754
Damien Miller0e220db2004-06-15 10:34:08 +1000755static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000756process_cmdline(struct ssh *ssh)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000757{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000758 void (*handler)(int);
Damien Miller7acefbb2014-07-18 14:11:24 +1000759 char *s, *cmd;
760 int ok, delete = 0, local = 0, remote = 0, dynamic = 0;
761 struct Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000762
Damien Miller1d2c4562014-02-04 11:18:20 +1100763 memset(&fwd, 0, sizeof(fwd));
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100764
Damien Miller21771e22011-05-15 08:45:50 +1000765 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000766 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000767 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000768 if (s == NULL)
769 goto out;
Damien Millerfdb23062013-11-21 13:57:15 +1100770 while (isspace((u_char)*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000771 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000772 if (*s == '-')
773 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000774 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000775 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000776
Darren Tucker1973c882004-05-24 10:34:36 +1000777 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000778 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000779 logit(" -L[bind_address:]port:host:hostport "
780 "Request local forward");
781 logit(" -R[bind_address:]port:host:hostport "
782 "Request remote forward");
Damien Miller0164cb82008-11-05 16:30:31 +1100783 logit(" -D[bind_address:]port "
784 "Request dynamic forward");
Damien Millerff773642011-09-22 21:39:48 +1000785 logit(" -KL[bind_address:]port "
786 "Cancel local forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000787 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000788 "Cancel remote forward");
Damien Millerff773642011-09-22 21:39:48 +1000789 logit(" -KD[bind_address:]port "
790 "Cancel dynamic forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100791 if (!options.permit_local_command)
792 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000793 logit(" !args "
794 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100795 goto out;
796 }
797
798 if (*s == '!' && options.permit_local_command) {
799 s++;
800 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000801 goto out;
802 }
803
804 if (*s == 'K') {
805 delete = 1;
806 s++;
807 }
Damien Miller0164cb82008-11-05 16:30:31 +1100808 if (*s == 'L')
809 local = 1;
810 else if (*s == 'R')
811 remote = 1;
812 else if (*s == 'D')
813 dynamic = 1;
814 else {
Damien Miller996acd22003-04-09 20:59:48 +1000815 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000816 goto out;
817 }
Damien Miller0164cb82008-11-05 16:30:31 +1100818
Damien Millerfdb23062013-11-21 13:57:15 +1100819 while (isspace((u_char)*++s))
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100820 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000821
Damien Millere1537f92010-01-26 13:26:22 +1100822 /* XXX update list of forwards in options */
Darren Tuckere7066df2004-05-24 10:18:05 +1000823 if (delete) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000824 /* We pass 1 for dynamicfwd to restrict to 1 or 2 fields. */
825 if (!parse_forward(&fwd, s, 1, 0)) {
826 logit("Bad forwarding close specification.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000827 goto out;
828 }
Damien Millerff773642011-09-22 21:39:48 +1000829 if (remote)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000830 ok = channel_request_rforward_cancel(ssh, &fwd) == 0;
Damien Millerff773642011-09-22 21:39:48 +1000831 else if (dynamic)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000832 ok = channel_cancel_lport_listener(ssh, &fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +1000833 0, &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000834 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000835 ok = channel_cancel_lport_listener(ssh, &fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +1000836 CHANNEL_CANCEL_PORT_STATIC,
837 &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000838 if (!ok) {
dtucker@openbsd.org9390b002017-01-29 21:35:23 +0000839 logit("Unknown port forwarding.");
Damien Millerff773642011-09-22 21:39:48 +1000840 goto out;
841 }
842 logit("Canceled forwarding.");
Darren Tuckere7066df2004-05-24 10:18:05 +1000843 } else {
Damien Miller4bf648f2009-02-14 16:28:21 +1100844 if (!parse_forward(&fwd, s, dynamic, remote)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000845 logit("Bad forwarding specification.");
846 goto out;
847 }
Damien Miller0164cb82008-11-05 16:30:31 +1100848 if (local || dynamic) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000849 if (!channel_setup_local_fwd_listener(ssh, &fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +1000850 &options.fwd_opts)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000851 logit("Port forwarding failed.");
852 goto out;
853 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100854 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000855 if (channel_request_remote_forwarding(ssh, &fwd) < 0) {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000856 logit("Port forwarding failed.");
857 goto out;
858 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100859 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000860 logit("Forwarding port.");
861 }
862
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000863out:
864 signal(SIGINT, handler);
Damien Miller21771e22011-05-15 08:45:50 +1000865 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tuckera627d422013-06-02 07:31:17 +1000866 free(cmd);
867 free(fwd.listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000868 free(fwd.listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000869 free(fwd.connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000870 free(fwd.connect_path);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000871}
872
Darren Tucker92a39cf2012-09-07 11:20:20 +1000873/* reasons to suppress output of an escape command in help output */
874#define SUPPRESS_NEVER 0 /* never suppress, always show */
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000875#define SUPPRESS_MUXCLIENT 1 /* don't show in mux client sessions */
876#define SUPPRESS_MUXMASTER 2 /* don't show in mux master sessions */
877#define SUPPRESS_SYSLOG 4 /* don't show when logging to syslog */
Darren Tucker92a39cf2012-09-07 11:20:20 +1000878struct escape_help_text {
879 const char *cmd;
880 const char *text;
881 unsigned int flags;
882};
883static struct escape_help_text esc_txt[] = {
884 {".", "terminate session", SUPPRESS_MUXMASTER},
885 {".", "terminate connection (and any multiplexed sessions)",
886 SUPPRESS_MUXCLIENT},
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000887 {"B", "send a BREAK to the remote system", SUPPRESS_NEVER},
Darren Tucker92a39cf2012-09-07 11:20:20 +1000888 {"C", "open a command line", SUPPRESS_MUXCLIENT},
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000889 {"R", "request rekey", SUPPRESS_NEVER},
Darren Tuckerca0d0fd2012-09-07 11:22:24 +1000890 {"V/v", "decrease/increase verbosity (LogLevel)", SUPPRESS_MUXCLIENT},
Darren Tucker92a39cf2012-09-07 11:20:20 +1000891 {"^Z", "suspend ssh", SUPPRESS_MUXCLIENT},
892 {"#", "list forwarded connections", SUPPRESS_NEVER},
893 {"&", "background ssh (when waiting for connections to terminate)",
894 SUPPRESS_MUXCLIENT},
895 {"?", "this message", SUPPRESS_NEVER},
896};
897
898static void
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000899print_escape_help(struct sshbuf *b, int escape_char, int mux_client,
900 int using_stderr)
Darren Tucker92a39cf2012-09-07 11:20:20 +1000901{
902 unsigned int i, suppress_flags;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000903 int r;
Darren Tucker92a39cf2012-09-07 11:20:20 +1000904
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000905 if ((r = sshbuf_putf(b,
906 "%c?\r\nSupported escape sequences:\r\n", escape_char)) != 0)
907 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker92a39cf2012-09-07 11:20:20 +1000908
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000909 suppress_flags =
Darren Tucker92a39cf2012-09-07 11:20:20 +1000910 (mux_client ? SUPPRESS_MUXCLIENT : 0) |
911 (mux_client ? 0 : SUPPRESS_MUXMASTER) |
912 (using_stderr ? 0 : SUPPRESS_SYSLOG);
913
914 for (i = 0; i < sizeof(esc_txt)/sizeof(esc_txt[0]); i++) {
915 if (esc_txt[i].flags & suppress_flags)
916 continue;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000917 if ((r = sshbuf_putf(b, " %c%-3s - %s\r\n",
918 escape_char, esc_txt[i].cmd, esc_txt[i].text)) != 0)
919 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker92a39cf2012-09-07 11:20:20 +1000920 }
921
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000922 if ((r = sshbuf_putf(b,
Darren Tuckerca0d0fd2012-09-07 11:22:24 +1000923 " %c%c - send the escape character by typing it twice\r\n"
Darren Tucker92a39cf2012-09-07 11:20:20 +1000924 "(Note that escapes are only recognized immediately after "
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000925 "newline.)\r\n", escape_char, escape_char)) != 0)
926 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker92a39cf2012-09-07 11:20:20 +1000927}
928
djm@openbsd.org84623e02018-06-26 02:02:36 +0000929/*
naddy@openbsd.org768405f2017-05-03 21:08:09 +0000930 * Process the characters one by one.
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000931 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000932static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000933process_escapes(struct ssh *ssh, Channel *c,
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000934 struct sshbuf *bin, struct sshbuf *bout, struct sshbuf *berr,
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000935 char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +1000936{
Damien Millerad833b32000-08-23 10:46:23 +1000937 pid_t pid;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000938 int r, bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000939 u_int i;
940 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +1000941 char *s;
djm@openbsd.org05164352017-04-30 23:21:54 +0000942 struct escape_filter_ctx *efc = c->filter_ctx == NULL ?
943 NULL : (struct escape_filter_ctx *)c->filter_ctx;
Damien Millerad833b32000-08-23 10:46:23 +1000944
djm@openbsd.org05164352017-04-30 23:21:54 +0000945 if (c->filter_ctx == NULL)
946 return 0;
djm@openbsd.org84623e02018-06-26 02:02:36 +0000947
Damien Millereccb9de2005-06-17 12:59:34 +1000948 if (len <= 0)
949 return (0);
950
951 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +1000952 /* Get one character at a time. */
953 ch = buf[i];
954
djm@openbsd.org05164352017-04-30 23:21:54 +0000955 if (efc->escape_pending) {
Damien Millerad833b32000-08-23 10:46:23 +1000956 /* We have previously seen an escape character. */
957 /* Clear the flag now. */
djm@openbsd.org05164352017-04-30 23:21:54 +0000958 efc->escape_pending = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000959
960 /* Process the escaped character. */
961 switch (ch) {
962 case '.':
963 /* Terminate the connection. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000964 if ((r = sshbuf_putf(berr, "%c.\r\n",
965 efc->escape_char)) != 0)
966 fatal("%s: buffer error: %s",
967 __func__, ssh_err(r));
Damien Millere1537f92010-01-26 13:26:22 +1100968 if (c && c->ctl_chan != -1) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000969 chan_read_failed(ssh, c);
970 chan_write_failed(ssh, c);
971 if (c->detach_user) {
972 c->detach_user(ssh,
973 c->self, NULL);
974 }
Damien Miller36187092013-06-10 13:07:11 +1000975 c->type = SSH_CHANNEL_ABANDONED;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000976 sshbuf_reset(c->input);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000977 chan_ibuf_empty(ssh, c);
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000978 return 0;
979 } else
980 quit_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +1000981 return -1;
982
983 case 'Z' - 64:
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000984 /* XXX support this for mux clients */
Damien Millere1537f92010-01-26 13:26:22 +1100985 if (c && c->ctl_chan != -1) {
Darren Tuckerf111d402012-09-07 11:21:42 +1000986 char b[16];
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000987 noescape:
Darren Tuckerf111d402012-09-07 11:21:42 +1000988 if (ch == 'Z' - 64)
989 snprintf(b, sizeof b, "^Z");
990 else
991 snprintf(b, sizeof b, "%c", ch);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000992 if ((r = sshbuf_putf(berr,
Darren Tuckerf111d402012-09-07 11:21:42 +1000993 "%c%s escape not available to "
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000994 "multiplexed sessions\r\n",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000995 efc->escape_char, b)) != 0)
996 fatal("%s: buffer error: %s",
997 __func__, ssh_err(r));
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000998 continue;
999 }
Darren Tucker4d5cd332008-06-13 04:51:14 +10001000 /* Suspend the program. Inform the user */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001001 if ((r = sshbuf_putf(berr,
1002 "%c^Z [suspend ssh]\r\n",
1003 efc->escape_char)) != 0)
1004 fatal("%s: buffer error: %s",
1005 __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +10001006
1007 /* Restore terminal modes and suspend. */
1008 client_suspend_self(bin, bout, berr);
1009
1010 /* We have been continued. */
1011 continue;
1012
Damien Miller54c45982003-05-15 10:20:13 +10001013 case 'B':
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001014 if ((r = sshbuf_putf(berr,
1015 "%cB\r\n", efc->escape_char)) != 0)
1016 fatal("%s: buffer error: %s",
1017 __func__, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001018 channel_request_start(ssh, c->self, "break", 0);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001019 if ((r = sshpkt_put_u32(ssh, 1000)) != 0 ||
1020 (r = sshpkt_send(ssh)) != 0)
1021 fatal("%s: %s", __func__,
1022 ssh_err(r));
Damien Miller54c45982003-05-15 10:20:13 +10001023 continue;
1024
Ben Lindstromf28f6342001-04-04 02:03:04 +00001025 case 'R':
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001026 if (datafellows & SSH_BUG_NOREKEY)
1027 logit("Server does not "
1028 "support re-keying");
1029 else
1030 need_rekeying = 1;
Ben Lindstromf28f6342001-04-04 02:03:04 +00001031 continue;
1032
Darren Tucker50a48d02012-09-06 21:25:37 +10001033 case 'V':
1034 /* FALLTHROUGH */
1035 case 'v':
1036 if (c && c->ctl_chan != -1)
1037 goto noescape;
1038 if (!log_is_on_stderr()) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001039 if ((r = sshbuf_putf(berr,
Darren Tucker50a48d02012-09-06 21:25:37 +10001040 "%c%c [Logging to syslog]\r\n",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001041 efc->escape_char, ch)) != 0)
1042 fatal("%s: buffer error: %s",
1043 __func__, ssh_err(r));
Darren Tucker50a48d02012-09-06 21:25:37 +10001044 continue;
1045 }
1046 if (ch == 'V' && options.log_level >
1047 SYSLOG_LEVEL_QUIET)
1048 log_change_level(--options.log_level);
1049 if (ch == 'v' && options.log_level <
1050 SYSLOG_LEVEL_DEBUG3)
1051 log_change_level(++options.log_level);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001052 if ((r = sshbuf_putf(berr,
djm@openbsd.org05164352017-04-30 23:21:54 +00001053 "%c%c [LogLevel %s]\r\n",
1054 efc->escape_char, ch,
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001055 log_level_name(options.log_level))) != 0)
1056 fatal("%s: buffer error: %s",
1057 __func__, ssh_err(r));
Darren Tucker50a48d02012-09-06 21:25:37 +10001058 continue;
1059
Damien Millerad833b32000-08-23 10:46:23 +10001060 case '&':
Damien Millere1537f92010-01-26 13:26:22 +11001061 if (c && c->ctl_chan != -1)
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001062 goto noescape;
Damien Millerad833b32000-08-23 10:46:23 +10001063 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001064 * Detach the program (continue to serve
1065 * connections, but put in background and no
1066 * more new connections).
Damien Millerad833b32000-08-23 10:46:23 +10001067 */
Damien Miller96507ef2001-11-12 10:52:25 +11001068 /* Restore tty modes. */
Damien Miller21771e22011-05-15 08:45:50 +10001069 leave_raw_mode(
1070 options.request_tty == REQUEST_TTY_FORCE);
Damien Miller96507ef2001-11-12 10:52:25 +11001071
1072 /* Stop listening for new connections. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001073 channel_stop_listening(ssh);
Damien Miller96507ef2001-11-12 10:52:25 +11001074
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001075 if ((r = sshbuf_putf(berr,
1076 "%c& [backgrounded]\n", efc->escape_char))
1077 != 0)
1078 fatal("%s: buffer error: %s",
1079 __func__, ssh_err(r));
Damien Miller96507ef2001-11-12 10:52:25 +11001080
1081 /* Fork into background. */
1082 pid = fork();
1083 if (pid < 0) {
1084 error("fork: %.100s", strerror(errno));
1085 continue;
1086 }
1087 if (pid != 0) { /* This is the parent. */
1088 /* The parent just exits. */
1089 exit(0);
1090 }
1091 /* The child continues serving connections. */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001092 /* fake EOF on stdin */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001093 if ((r = sshbuf_put_u8(bin, 4)) != 0)
1094 fatal("%s: buffer error: %s",
1095 __func__, ssh_err(r));
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001096 return -1;
Damien Millerad833b32000-08-23 10:46:23 +10001097 case '?':
djm@openbsd.org05164352017-04-30 23:21:54 +00001098 print_escape_help(berr, efc->escape_char,
Darren Tucker92a39cf2012-09-07 11:20:20 +10001099 (c && c->ctl_chan != -1),
1100 log_is_on_stderr());
Damien Millerad833b32000-08-23 10:46:23 +10001101 continue;
1102
1103 case '#':
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001104 if ((r = sshbuf_putf(berr, "%c#\r\n",
1105 efc->escape_char)) != 0)
1106 fatal("%s: buffer error: %s",
1107 __func__, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001108 s = channel_open_message(ssh);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001109 if ((r = sshbuf_put(berr, s, strlen(s))) != 0)
1110 fatal("%s: buffer error: %s",
1111 __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +10001112 free(s);
Damien Millerad833b32000-08-23 10:46:23 +10001113 continue;
1114
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001115 case 'C':
Damien Millere1537f92010-01-26 13:26:22 +11001116 if (c && c->ctl_chan != -1)
Damien Miller586b0052008-12-09 14:11:32 +11001117 goto noescape;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001118 process_cmdline(ssh);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001119 continue;
1120
Damien Millerad833b32000-08-23 10:46:23 +10001121 default:
djm@openbsd.org05164352017-04-30 23:21:54 +00001122 if (ch != efc->escape_char) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001123 if ((r = sshbuf_put_u8(bin,
1124 efc->escape_char)) != 0)
1125 fatal("%s: buffer error: %s",
1126 __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +10001127 bytes++;
1128 }
1129 /* Escaped characters fall through here */
1130 break;
1131 }
1132 } else {
1133 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001134 * The previous character was not an escape char.
1135 * Check if this is an escape.
Damien Millerad833b32000-08-23 10:46:23 +10001136 */
djm@openbsd.org05164352017-04-30 23:21:54 +00001137 if (last_was_cr && ch == efc->escape_char) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001138 /*
1139 * It is. Set the flag and continue to
1140 * next character.
1141 */
djm@openbsd.org05164352017-04-30 23:21:54 +00001142 efc->escape_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001143 continue;
1144 }
1145 }
1146
1147 /*
1148 * Normal character. Record whether it was a newline,
1149 * and append it to the buffer.
1150 */
1151 last_was_cr = (ch == '\r' || ch == '\n');
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001152 if ((r = sshbuf_put_u8(bin, ch)) != 0)
1153 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +10001154 bytes++;
1155 }
1156 return bytes;
1157}
1158
Damien Miller5428f641999-11-25 11:54:57 +11001159/*
Damien Millerb38eff82000-04-01 11:09:21 +10001160 * Get packets from the connection input buffer, and process them as long as
1161 * there are packets available.
1162 *
1163 * Any unknown packets received during the actual
1164 * session cause the session to terminate. This is
1165 * intended to make debugging easier since no
1166 * confirmations are sent. Any compatible protocol
1167 * extensions must be negotiated during the
1168 * preparatory phase.
1169 */
1170
Ben Lindstrombba81212001-06-25 05:01:22 +00001171static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001172client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001173{
markus@openbsd.org92e9fe62017-05-31 07:00:13 +00001174 ssh_dispatch_run_fatal(active_state, DISPATCH_NONBLOCK, &quit_pending);
Damien Millerb38eff82000-04-01 11:09:21 +10001175}
1176
Damien Millerad833b32000-08-23 10:46:23 +10001177/* scan buf[] for '~' before sending data to the peer */
1178
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001179/* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1180void *
1181client_new_escape_filter_ctx(int escape_char)
1182{
1183 struct escape_filter_ctx *ret;
1184
Damien Miller6c81fee2013-11-08 12:19:55 +11001185 ret = xcalloc(1, sizeof(*ret));
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001186 ret->escape_pending = 0;
1187 ret->escape_char = escape_char;
1188 return (void *)ret;
1189}
1190
Darren Tucker84c56f52008-06-13 04:55:46 +10001191/* Free the escape filter context on channel free */
1192void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001193client_filter_cleanup(struct ssh *ssh, int cid, void *ctx)
Darren Tucker84c56f52008-06-13 04:55:46 +10001194{
Darren Tuckera627d422013-06-02 07:31:17 +10001195 free(ctx);
Darren Tucker84c56f52008-06-13 04:55:46 +10001196}
1197
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001198int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001199client_simple_escape_filter(struct ssh *ssh, Channel *c, char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001200{
Damien Miller5771ed72008-05-19 15:35:33 +10001201 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1202 return 0;
1203
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001204 return process_escapes(ssh, c, c->input, c->output, c->extended,
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001205 buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001206}
1207
Ben Lindstrombba81212001-06-25 05:01:22 +00001208static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001209client_channel_closed(struct ssh *ssh, int id, void *arg)
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001210{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001211 channel_cancel_cleanup(ssh, id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001212 session_closed = 1;
Damien Miller21771e22011-05-15 08:45:50 +10001213 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001214}
1215
Damien Millerb38eff82000-04-01 11:09:21 +10001216/*
Damien Miller5428f641999-11-25 11:54:57 +11001217 * Implements the interactive session with the server. This is called after
1218 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001219 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1220 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001221 */
Damien Miller4af51302000-04-16 11:18:38 +10001222int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001223client_loop(struct ssh *ssh, int have_pty, int escape_char_arg,
1224 int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001225{
Damien Miller5e953212001-01-30 09:14:00 +11001226 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001227 double start_time, total_time;
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001228 int r, max_fd = 0, max_fd2 = 0, len;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001229 u_int64_t ibytes, obytes;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001230 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001231 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001232
Damien Miller95def091999-11-25 00:26:21 +11001233 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001234
semarie@openbsd.orgd7d2bc92015-12-26 07:46:03 +00001235 if (options.control_master &&
djm@openbsd.org368dd972016-07-23 02:54:08 +00001236 !option_clear_or_none(options.control_path)) {
semarie@openbsd.orgd7d2bc92015-12-26 07:46:03 +00001237 debug("pledge: id");
tb@openbsd.org372807c2016-07-11 21:38:13 +00001238 if (pledge("stdio rpath wpath cpath unix inet dns recvfd proc exec id tty",
semarie@openbsd.orgd7d2bc92015-12-26 07:46:03 +00001239 NULL) == -1)
1240 fatal("%s pledge(): %s", __func__, strerror(errno));
1241
1242 } else if (options.forward_x11 || options.permit_local_command) {
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001243 debug("pledge: exec");
1244 if (pledge("stdio rpath wpath cpath unix inet dns proc exec tty",
1245 NULL) == -1)
1246 fatal("%s pledge(): %s", __func__, strerror(errno));
1247
1248 } else if (options.update_hostkeys) {
1249 debug("pledge: filesystem full");
1250 if (pledge("stdio rpath wpath cpath unix inet dns proc tty",
1251 NULL) == -1)
1252 fatal("%s pledge(): %s", __func__, strerror(errno));
1253
djm@openbsd.org368dd972016-07-23 02:54:08 +00001254 } else if (!option_clear_or_none(options.proxy_command) ||
1255 fork_after_authentication_flag) {
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001256 debug("pledge: proc");
1257 if (pledge("stdio cpath unix inet dns proc tty", NULL) == -1)
1258 fatal("%s pledge(): %s", __func__, strerror(errno));
1259
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001260 } else {
1261 debug("pledge: network");
mestre@openbsd.orge2386452017-06-23 07:24:48 +00001262 if (pledge("stdio unix inet dns proc tty", NULL) == -1)
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001263 fatal("%s pledge(): %s", __func__, strerror(errno));
1264 }
1265
dtucker@openbsd.org@openbsd.org2d638e92017-11-25 05:58:47 +00001266 start_time = monotime_double();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001267
Damien Miller95def091999-11-25 00:26:21 +11001268 /* Initialize variables. */
Damien Miller95def091999-11-25 00:26:21 +11001269 last_was_cr = 1;
1270 exit_status = -1;
Damien Miller95def091999-11-25 00:26:21 +11001271 connection_in = packet_get_connection_in();
1272 connection_out = packet_get_connection_out();
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001273 max_fd = MAXIMUM(connection_in, connection_out);
Damien Miller5e953212001-01-30 09:14:00 +11001274
Damien Miller95def091999-11-25 00:26:21 +11001275 quit_pending = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001276
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001277 /* Initialize buffer. */
1278 if ((stderr_buffer = sshbuf_new()) == NULL)
1279 fatal("%s: sshbuf_new failed", __func__);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001280
Damien Millerb38eff82000-04-01 11:09:21 +10001281 client_init_dispatch();
1282
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001283 /*
1284 * Set signal handlers, (e.g. to restore non-blocking mode)
1285 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1286 */
Darren Tucker07336da2004-11-05 20:02:16 +11001287 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1288 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001289 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1290 signal(SIGINT, signal_handler);
1291 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1292 signal(SIGQUIT, signal_handler);
1293 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1294 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001295 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001296
Damien Miller95def091999-11-25 00:26:21 +11001297 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001298 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001299
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001300 session_ident = ssh2_chan_id;
1301 if (session_ident != -1) {
1302 if (escape_char_arg != SSH_ESCAPECHAR_NONE) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001303 channel_register_filter(ssh, session_ident,
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001304 client_simple_escape_filter, NULL,
1305 client_filter_cleanup,
1306 client_new_escape_filter_ctx(
1307 escape_char_arg));
Damien Miller6c3eec72011-05-05 14:16:22 +10001308 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001309 channel_register_cleanup(ssh, session_ident,
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001310 client_channel_closed, 0);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001311 }
Damien Millerad833b32000-08-23 10:46:23 +10001312
Damien Miller95def091999-11-25 00:26:21 +11001313 /* Main loop of the client for the interactive session mode. */
1314 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001315
Damien Miller5428f641999-11-25 11:54:57 +11001316 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001317 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001318
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001319 if (session_closed && !channel_still_open(ssh))
Damien Miller1383bd82000-04-06 12:32:37 +10001320 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001321
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001322 if (ssh_packet_is_rekeying(ssh)) {
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001323 debug("rekeying in progress");
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001324 } else if (need_rekeying) {
1325 /* manual rekey request */
1326 debug("need rekeying");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001327 if ((r = kex_start_rekex(ssh)) != 0)
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001328 fatal("%s: kex_start_rekex: %s", __func__,
1329 ssh_err(r));
1330 need_rekeying = 0;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001331 } else {
1332 /*
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001333 * Make packets from buffered channel data, and
1334 * enqueue them for sending to the server.
1335 */
1336 if (packet_not_very_much_data_to_write())
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001337 channel_output_poll(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001338
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001339 /*
1340 * Check if the window size has changed, and buffer a
1341 * message about it to the server if so.
1342 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001343 client_check_window_change(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001344
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001345 if (quit_pending)
1346 break;
1347 }
Damien Miller5428f641999-11-25 11:54:57 +11001348 /*
1349 * Wait until we have something to do (something becomes
1350 * available on one of the descriptors).
1351 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001352 max_fd2 = max_fd;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001353 client_wait_until_can_do_something(ssh, &readset, &writeset,
1354 &max_fd2, &nalloc, ssh_packet_is_rekeying(ssh));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001355
Damien Miller95def091999-11-25 00:26:21 +11001356 if (quit_pending)
1357 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001358
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001359 /* Do channel operations unless rekeying in progress. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001360 if (!ssh_packet_is_rekeying(ssh))
1361 channel_after_select(ssh, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001362
Damien Miller1383bd82000-04-06 12:32:37 +10001363 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001364 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001365
Damien Miller1383bd82000-04-06 12:32:37 +10001366 if (quit_pending)
1367 break;
1368
Darren Tucker4d5cd332008-06-13 04:51:14 +10001369 /*
1370 * Send as much buffered packet data as possible to the
1371 * sender.
1372 */
Damien Miller5e953212001-01-30 09:14:00 +11001373 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001374 packet_write_poll();
Damien Millere11e1ea2010-08-03 16:04:46 +10001375
1376 /*
1377 * If we are a backgrounded control master, and the
1378 * timeout has expired without any active client
1379 * connections, then quit.
1380 */
1381 if (control_persist_exit_time > 0) {
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001382 if (monotime() >= control_persist_exit_time) {
Damien Millere11e1ea2010-08-03 16:04:46 +10001383 debug("ControlPersist timeout expired");
1384 break;
1385 }
1386 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001387 }
Darren Tuckera627d422013-06-02 07:31:17 +10001388 free(readset);
1389 free(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001390
Damien Miller95def091999-11-25 00:26:21 +11001391 /* Terminate the session. */
1392
1393 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001394 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001395
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001396 packet_start(SSH2_MSG_DISCONNECT);
1397 packet_put_int(SSH2_DISCONNECT_BY_APPLICATION);
1398 packet_put_cstring("disconnected by user");
1399 packet_put_cstring(""); /* language tag */
1400 packet_send();
1401 packet_write_wait();
Darren Tucker12b4a652009-06-21 18:14:48 +10001402
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001403 channel_free_all(ssh);
Damien Miller95def091999-11-25 00:26:21 +11001404
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001405 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001406 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001407
1408 /* restore blocking io */
1409 if (!isatty(fileno(stdin)))
1410 unset_nonblock(fileno(stdin));
1411 if (!isatty(fileno(stdout)))
1412 unset_nonblock(fileno(stdout));
1413 if (!isatty(fileno(stderr)))
1414 unset_nonblock(fileno(stderr));
1415
Damien Millerd6965512003-12-17 16:31:53 +11001416 /*
1417 * If there was no shell or command requested, there will be no remote
1418 * exit status to be returned. In that case, clear error code if the
1419 * connection was deliberately terminated at this end.
1420 */
1421 if (no_shell_flag && received_signal == SIGTERM) {
1422 received_signal = 0;
1423 exit_status = 0;
1424 }
1425
dtucker@openbsd.orgb1e72df2017-07-14 03:18:21 +00001426 if (received_signal) {
1427 verbose("Killed by signal %d.", (int) received_signal);
1428 cleanup_exit(0);
1429 }
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001430
1431 /*
1432 * In interactive mode (with pseudo tty) display a message indicating
1433 * that the connection has been closed.
1434 */
1435 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001436 if ((r = sshbuf_putf(stderr_buffer,
1437 "Connection to %.64s closed.\r\n", host)) != 0)
1438 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001439 }
1440
Damien Miller95def091999-11-25 00:26:21 +11001441 /* Output any buffered data for stderr. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001442 if (sshbuf_len(stderr_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001443 len = atomicio(vwrite, fileno(stderr),
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001444 (u_char *)sshbuf_ptr(stderr_buffer),
1445 sshbuf_len(stderr_buffer));
1446 if (len < 0 || (u_int)len != sshbuf_len(stderr_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001447 error("Write failed flushing stderr buffer.");
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001448 else if ((r = sshbuf_consume(stderr_buffer, len)) != 0)
1449 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11001450 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001451
Damien Miller95def091999-11-25 00:26:21 +11001452 /* Clear and free any buffers. */
jsg@openbsd.org458abc22016-01-23 05:31:35 +00001453 explicit_bzero(buf, sizeof(buf));
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001454 sshbuf_free(stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001455
Damien Miller95def091999-11-25 00:26:21 +11001456 /* Report bytes transferred, and transfer rates. */
dtucker@openbsd.org@openbsd.org2d638e92017-11-25 05:58:47 +00001457 total_time = monotime_double() - start_time;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001458 packet_get_bytes(&ibytes, &obytes);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001459 verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
Damien Miller821de0a2011-01-11 17:20:29 +11001460 (unsigned long long)obytes, (unsigned long long)ibytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001461 if (total_time > 0)
Damien Millerb61f3fc2008-07-11 17:36:48 +10001462 verbose("Bytes per second: sent %.1f, received %.1f",
1463 obytes / total_time, ibytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001464 /* Return the exit status of the program. */
1465 debug("Exit status %d", exit_status);
1466 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001467}
Damien Millerb38eff82000-04-01 11:09:21 +10001468
1469/*********/
1470
Ben Lindstrombba81212001-06-25 05:01:22 +00001471static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001472client_request_forwarded_tcpip(struct ssh *ssh, const char *request_type,
1473 int rchan, u_int rwindow, u_int rmaxpack)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001474{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001475 Channel *c = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00001476 struct sshbuf *b = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001477 char *listen_address, *originator_address;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001478 u_short listen_port, originator_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00001479 int r;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001480
1481 /* Get rest of the packet */
1482 listen_address = packet_get_string(NULL);
1483 listen_port = packet_get_int();
1484 originator_address = packet_get_string(NULL);
1485 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001486 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001487
Damien Miller7acefbb2014-07-18 14:11:24 +10001488 debug("%s: listen %s port %d, originator %s port %d", __func__,
1489 listen_address, listen_port, originator_address, originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001490
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001491 c = channel_connect_by_listen_address(ssh, listen_address, listen_port,
Damien Millerbd740252008-05-19 15:37:09 +10001492 "forwarded-tcpip", originator_address);
1493
markus@openbsd.org8d057842016-09-30 09:19:13 +00001494 if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) {
1495 if ((b = sshbuf_new()) == NULL) {
1496 error("%s: alloc reply", __func__);
1497 goto out;
1498 }
1499 /* reconstruct and send to muxclient */
1500 if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
1501 (r = sshbuf_put_u8(b, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
1502 (r = sshbuf_put_cstring(b, request_type)) != 0 ||
1503 (r = sshbuf_put_u32(b, rchan)) != 0 ||
1504 (r = sshbuf_put_u32(b, rwindow)) != 0 ||
1505 (r = sshbuf_put_u32(b, rmaxpack)) != 0 ||
1506 (r = sshbuf_put_cstring(b, listen_address)) != 0 ||
1507 (r = sshbuf_put_u32(b, listen_port)) != 0 ||
1508 (r = sshbuf_put_cstring(b, originator_address)) != 0 ||
1509 (r = sshbuf_put_u32(b, originator_port)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001510 (r = sshbuf_put_stringb(c->output, b)) != 0) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00001511 error("%s: compose for muxclient %s", __func__,
1512 ssh_err(r));
1513 goto out;
1514 }
1515 }
1516
1517 out:
1518 sshbuf_free(b);
Darren Tuckera627d422013-06-02 07:31:17 +10001519 free(originator_address);
1520 free(listen_address);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001521 return c;
1522}
1523
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001524static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001525client_request_forwarded_streamlocal(struct ssh *ssh,
1526 const char *request_type, int rchan)
Damien Miller7acefbb2014-07-18 14:11:24 +10001527{
1528 Channel *c = NULL;
1529 char *listen_path;
1530
1531 /* Get the remote path. */
1532 listen_path = packet_get_string(NULL);
1533 /* XXX: Skip reserved field for now. */
1534 if (packet_get_string_ptr(NULL) == NULL)
1535 fatal("%s: packet_get_string_ptr failed", __func__);
1536 packet_check_eom();
1537
1538 debug("%s: %s", __func__, listen_path);
1539
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001540 c = channel_connect_by_listen_path(ssh, listen_path,
Damien Miller7acefbb2014-07-18 14:11:24 +10001541 "forwarded-streamlocal@openssh.com", "forwarded-streamlocal");
1542 free(listen_path);
1543 return c;
1544}
1545
1546static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001547client_request_x11(struct ssh *ssh, const char *request_type, int rchan)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001548{
1549 Channel *c = NULL;
1550 char *originator;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001551 u_short originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001552 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001553
1554 if (!options.forward_x11) {
1555 error("Warning: ssh server tried X11 forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001556 error("Warning: this is probably a break-in attempt by a "
1557 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001558 return NULL;
1559 }
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001560 if (x11_refuse_time != 0 && (u_int)monotime() >= x11_refuse_time) {
Damien Miller1ab6a512010-06-26 10:02:24 +10001561 verbose("Rejected X11 connection after ForwardX11Timeout "
1562 "expired");
1563 return NULL;
1564 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001565 originator = packet_get_string(NULL);
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001566 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001567 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001568 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001569 debug("client_request_x11: request from %s %d", originator,
1570 originator_port);
Darren Tuckera627d422013-06-02 07:31:17 +10001571 free(originator);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001572 sock = x11_connect_display(ssh);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001573 if (sock < 0)
1574 return NULL;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001575 c = channel_new(ssh, "x11",
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001576 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001577 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001578 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001579 return c;
1580}
1581
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001582static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001583client_request_agent(struct ssh *ssh, const char *request_type, int rchan)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001584{
1585 Channel *c = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001586 int r, sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001587
1588 if (!options.forward_agent) {
1589 error("Warning: ssh server tried agent forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001590 error("Warning: this is probably a break-in attempt by a "
1591 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001592 return NULL;
1593 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001594 if ((r = ssh_get_authentication_socket(&sock)) != 0) {
1595 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1596 debug("%s: ssh_get_authentication_socket: %s",
1597 __func__, ssh_err(r));
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001598 return NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001599 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001600 c = channel_new(ssh, "authentication agent connection",
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001601 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001602 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001603 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001604 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001605 return c;
1606}
1607
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001608char *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001609client_request_tun_fwd(struct ssh *ssh, int tun_mode,
1610 int local_tun, int remote_tun)
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001611{
1612 Channel *c;
1613 int fd;
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001614 char *ifname = NULL;
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001615
1616 if (tun_mode == SSH_TUNMODE_NO)
1617 return 0;
1618
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001619 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1620
1621 /* Open local tunnel device */
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001622 if ((fd = tun_open(local_tun, tun_mode, &ifname)) == -1) {
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001623 error("Tunnel device open failed.");
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001624 return NULL;
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001625 }
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001626 debug("Tunnel forwarding using interface %s", ifname);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001627
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001628 c = channel_new(ssh, "tun", SSH_CHANNEL_OPENING, fd, fd, -1,
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001629 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1630 c->datagram = 1;
1631
1632#if defined(SSH_TUN_FILTER)
1633 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
Damien Miller871f1e42017-09-12 18:01:35 +10001634 channel_register_filter(ssh, c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +10001635 sys_tun_outfilter, NULL, NULL);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001636#endif
1637
1638 packet_start(SSH2_MSG_CHANNEL_OPEN);
1639 packet_put_cstring("tun@openssh.com");
1640 packet_put_int(c->self);
1641 packet_put_int(c->local_window_max);
1642 packet_put_int(c->local_maxpacket);
1643 packet_put_int(tun_mode);
1644 packet_put_int(remote_tun);
1645 packet_send();
1646
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001647 return ifname;
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001648}
1649
Damien Millerbd483e72000-04-30 10:00:53 +10001650/* XXXX move to generic input handler */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001651static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001652client_input_channel_open(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerbd483e72000-04-30 10:00:53 +10001653{
1654 Channel *c = NULL;
1655 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001656 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001657 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001658
1659 ctype = packet_get_string(&len);
1660 rchan = packet_get_int();
1661 rwindow = packet_get_int();
1662 rmaxpack = packet_get_int();
1663
Damien Millere247cc42000-05-07 12:03:14 +10001664 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001665 ctype, rchan, rwindow, rmaxpack);
1666
Damien Miller0bc1bd82000-11-13 22:57:25 +11001667 if (strcmp(ctype, "forwarded-tcpip") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001668 c = client_request_forwarded_tcpip(ssh, ctype, rchan, rwindow,
markus@openbsd.org8d057842016-09-30 09:19:13 +00001669 rmaxpack);
Damien Miller7acefbb2014-07-18 14:11:24 +10001670 } else if (strcmp(ctype, "forwarded-streamlocal@openssh.com") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001671 c = client_request_forwarded_streamlocal(ssh, ctype, rchan);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001672 } else if (strcmp(ctype, "x11") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001673 c = client_request_x11(ssh, ctype, rchan);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001674 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001675 c = client_request_agent(ssh, ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001676 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00001677 if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) {
1678 debug3("proxied to downstream: %s", ctype);
1679 } else if (c != NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10001680 debug("confirm %s", ctype);
1681 c->remote_id = rchan;
djm@openbsd.org9f532292017-09-12 06:35:31 +00001682 c->have_remote_id = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001683 c->remote_window = rwindow;
1684 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001685 if (c->type != SSH_CHANNEL_CONNECTING) {
1686 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1687 packet_put_int(c->remote_id);
1688 packet_put_int(c->self);
1689 packet_put_int(c->local_window);
1690 packet_put_int(c->local_maxpacket);
1691 packet_send();
1692 }
Damien Millerbd483e72000-04-30 10:00:53 +10001693 } else {
1694 debug("failure %s", ctype);
1695 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1696 packet_put_int(rchan);
1697 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001698 packet_put_cstring("open failed");
1699 packet_put_cstring("");
Damien Millerbd483e72000-04-30 10:00:53 +10001700 packet_send();
1701 }
Darren Tuckera627d422013-06-02 07:31:17 +10001702 free(ctype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001703 return 0;
Damien Millerbd483e72000-04-30 10:00:53 +10001704}
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001705
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001706static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001707client_input_channel_req(int type, u_int32_t seq, struct ssh *ssh)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001708{
1709 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001710 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001711 char *rtype;
1712
1713 id = packet_get_int();
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001714 c = channel_lookup(ssh, id);
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001715 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00001716 return 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001717 rtype = packet_get_string(NULL);
1718 reply = packet_get_char();
1719
1720 debug("client_input_channel_req: channel %d rtype %s reply %d",
1721 id, rtype, reply);
1722
Damien Miller3bbd8782004-06-18 22:23:22 +10001723 if (id == -1) {
1724 error("client_input_channel_req: request for channel -1");
markus@openbsd.org8d057842016-09-30 09:19:13 +00001725 } else if (c == NULL) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001726 error("client_input_channel_req: channel %d: "
1727 "unknown channel", id);
Damien Millerbab9bd42008-05-19 16:06:47 +10001728 } else if (strcmp(rtype, "eow@openssh.com") == 0) {
1729 packet_check_eom();
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001730 chan_rcvd_eow(ssh, c);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001731 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10001732 exitval = packet_get_int();
Damien Millere1537f92010-01-26 13:26:22 +11001733 if (c->ctl_chan != -1) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001734 mux_exit_message(ssh, c, exitval);
Damien Miller0e220db2004-06-15 10:34:08 +10001735 success = 1;
Darren Tucker29440822010-01-08 17:08:35 +11001736 } else if (id == session_ident) {
1737 /* Record exit value of local session */
1738 success = 1;
1739 exit_status = exitval;
1740 } else {
Damien Miller36f57eb2010-01-30 17:28:34 +11001741 /* Probably for a mux channel that has already closed */
1742 debug("%s: no sink for exit-status on channel %d",
1743 __func__, id);
Damien Miller0e220db2004-06-15 10:34:08 +10001744 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001745 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001746 }
Damien Millerc5893782014-05-15 13:48:49 +10001747 if (reply && c != NULL && !(c->flags & CHAN_CLOSE_SENT)) {
djm@openbsd.org9f532292017-09-12 06:35:31 +00001748 if (!c->have_remote_id)
1749 fatal("%s: channel %d: no remote_id",
1750 __func__, c->self);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001751 packet_start(success ?
1752 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller8533c782008-12-08 09:54:40 +11001753 packet_put_int(c->remote_id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001754 packet_send();
1755 }
Darren Tuckera627d422013-06-02 07:31:17 +10001756 free(rtype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001757 return 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001758}
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001759
djm@openbsd.org523463a2015-02-16 22:13:32 +00001760struct hostkeys_update_ctx {
1761 /* The hostname and (optionally) IP address string for the server */
1762 char *host_str, *ip_str;
1763
1764 /*
1765 * Keys received from the server and a flag for each indicating
1766 * whether they already exist in known_hosts.
1767 * keys_seen is filled in by hostkeys_find() and later (for new
1768 * keys) by client_global_hostkeys_private_confirm().
1769 */
1770 struct sshkey **keys;
1771 int *keys_seen;
djm@openbsd.org84623e02018-06-26 02:02:36 +00001772 size_t nkeys, nnew;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001773
1774 /*
1775 * Keys that are in known_hosts, but were not present in the update
1776 * from the server (i.e. scheduled to be deleted).
1777 * Filled in by hostkeys_find().
1778 */
1779 struct sshkey **old_keys;
1780 size_t nold;
1781};
1782
1783static void
1784hostkeys_update_ctx_free(struct hostkeys_update_ctx *ctx)
1785{
1786 size_t i;
1787
1788 if (ctx == NULL)
1789 return;
1790 for (i = 0; i < ctx->nkeys; i++)
1791 sshkey_free(ctx->keys[i]);
1792 free(ctx->keys);
1793 free(ctx->keys_seen);
1794 for (i = 0; i < ctx->nold; i++)
1795 sshkey_free(ctx->old_keys[i]);
1796 free(ctx->old_keys);
1797 free(ctx->host_str);
1798 free(ctx->ip_str);
1799 free(ctx);
1800}
1801
1802static int
1803hostkeys_find(struct hostkey_foreach_line *l, void *_ctx)
1804{
1805 struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
1806 size_t i;
1807 struct sshkey **tmp;
1808
djm@openbsd.org873d3e72017-04-30 23:18:44 +00001809 if (l->status != HKF_STATUS_MATCHED || l->key == NULL)
djm@openbsd.org523463a2015-02-16 22:13:32 +00001810 return 0;
1811
1812 /* Mark off keys we've already seen for this host */
1813 for (i = 0; i < ctx->nkeys; i++) {
1814 if (sshkey_equal(l->key, ctx->keys[i])) {
1815 debug3("%s: found %s key at %s:%ld", __func__,
1816 sshkey_ssh_name(ctx->keys[i]), l->path, l->linenum);
1817 ctx->keys_seen[i] = 1;
1818 return 0;
1819 }
1820 }
1821 /* This line contained a key that not offered by the server */
1822 debug3("%s: deprecated %s key at %s:%ld", __func__,
1823 sshkey_ssh_name(l->key), l->path, l->linenum);
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001824 if ((tmp = recallocarray(ctx->old_keys, ctx->nold, ctx->nold + 1,
djm@openbsd.org523463a2015-02-16 22:13:32 +00001825 sizeof(*ctx->old_keys))) == NULL)
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001826 fatal("%s: recallocarray failed nold = %zu",
djm@openbsd.org523463a2015-02-16 22:13:32 +00001827 __func__, ctx->nold);
1828 ctx->old_keys = tmp;
1829 ctx->old_keys[ctx->nold++] = l->key;
1830 l->key = NULL;
1831
1832 return 0;
1833}
1834
1835static void
1836update_known_hosts(struct hostkeys_update_ctx *ctx)
1837{
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00001838 int r, was_raw = 0;
1839 int loglevel = options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK ?
djm@openbsd.org523463a2015-02-16 22:13:32 +00001840 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_VERBOSE;
1841 char *fp, *response;
1842 size_t i;
1843
1844 for (i = 0; i < ctx->nkeys; i++) {
1845 if (ctx->keys_seen[i] != 2)
1846 continue;
1847 if ((fp = sshkey_fingerprint(ctx->keys[i],
1848 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1849 fatal("%s: sshkey_fingerprint failed", __func__);
1850 do_log2(loglevel, "Learned new hostkey: %s %s",
1851 sshkey_type(ctx->keys[i]), fp);
1852 free(fp);
1853 }
1854 for (i = 0; i < ctx->nold; i++) {
1855 if ((fp = sshkey_fingerprint(ctx->old_keys[i],
1856 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1857 fatal("%s: sshkey_fingerprint failed", __func__);
1858 do_log2(loglevel, "Deprecating obsolete hostkey: %s %s",
1859 sshkey_type(ctx->old_keys[i]), fp);
1860 free(fp);
1861 }
1862 if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK) {
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00001863 if (get_saved_tio() != NULL) {
1864 leave_raw_mode(1);
1865 was_raw = 1;
1866 }
djm@openbsd.org523463a2015-02-16 22:13:32 +00001867 response = NULL;
1868 for (i = 0; !quit_pending && i < 3; i++) {
1869 free(response);
1870 response = read_passphrase("Accept updated hostkeys? "
1871 "(yes/no): ", RP_ECHO);
1872 if (strcasecmp(response, "yes") == 0)
1873 break;
1874 else if (quit_pending || response == NULL ||
1875 strcasecmp(response, "no") == 0) {
1876 options.update_hostkeys = 0;
1877 break;
1878 } else {
1879 do_log2(loglevel, "Please enter "
1880 "\"yes\" or \"no\"");
1881 }
1882 }
1883 if (quit_pending || i >= 3 || response == NULL)
1884 options.update_hostkeys = 0;
1885 free(response);
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00001886 if (was_raw)
1887 enter_raw_mode(1);
djm@openbsd.org523463a2015-02-16 22:13:32 +00001888 }
1889
1890 /*
1891 * Now that all the keys are verified, we can go ahead and replace
1892 * them in known_hosts (assuming SSH_UPDATE_HOSTKEYS_ASK didn't
1893 * cancel the operation).
1894 */
1895 if (options.update_hostkeys != 0 &&
1896 (r = hostfile_replace_entries(options.user_hostfiles[0],
1897 ctx->host_str, ctx->ip_str, ctx->keys, ctx->nkeys,
1898 options.hash_known_hosts, 0,
1899 options.fingerprint_hash)) != 0)
1900 error("%s: hostfile_replace_entries failed: %s",
1901 __func__, ssh_err(r));
1902}
1903
1904static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001905client_global_hostkeys_private_confirm(struct ssh *ssh, int type,
1906 u_int32_t seq, void *_ctx)
djm@openbsd.org523463a2015-02-16 22:13:32 +00001907{
djm@openbsd.org523463a2015-02-16 22:13:32 +00001908 struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
1909 size_t i, ndone;
1910 struct sshbuf *signdata;
djm@openbsd.org78607312017-12-18 23:16:23 +00001911 int r, kexsigtype, use_kexsigtype;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001912 const u_char *sig;
1913 size_t siglen;
1914
1915 if (ctx->nnew == 0)
1916 fatal("%s: ctx->nnew == 0", __func__); /* sanity */
1917 if (type != SSH2_MSG_REQUEST_SUCCESS) {
1918 error("Server failed to confirm ownership of "
1919 "private host keys");
1920 hostkeys_update_ctx_free(ctx);
1921 return;
1922 }
djm@openbsd.org78607312017-12-18 23:16:23 +00001923 kexsigtype = sshkey_type_plain(
1924 sshkey_type_from_name(ssh->kex->hostkey_alg));
1925
djm@openbsd.org523463a2015-02-16 22:13:32 +00001926 if ((signdata = sshbuf_new()) == NULL)
1927 fatal("%s: sshbuf_new failed", __func__);
1928 /* Don't want to accidentally accept an unbound signature */
1929 if (ssh->kex->session_id_len == 0)
1930 fatal("%s: ssh->kex->session_id_len == 0", __func__);
1931 /*
1932 * Expect a signature for each of the ctx->nnew private keys we
1933 * haven't seen before. They will be in the same order as the
1934 * ctx->keys where the corresponding ctx->keys_seen[i] == 0.
1935 */
1936 for (ndone = i = 0; i < ctx->nkeys; i++) {
1937 if (ctx->keys_seen[i])
1938 continue;
1939 /* Prepare data to be signed: session ID, unique string, key */
1940 sshbuf_reset(signdata);
djm@openbsd.org44732de2015-02-20 22:17:21 +00001941 if ( (r = sshbuf_put_cstring(signdata,
1942 "hostkeys-prove-00@openssh.com")) != 0 ||
1943 (r = sshbuf_put_string(signdata, ssh->kex->session_id,
djm@openbsd.org523463a2015-02-16 22:13:32 +00001944 ssh->kex->session_id_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +00001945 (r = sshkey_puts(ctx->keys[i], signdata)) != 0)
1946 fatal("%s: failed to prepare signature: %s",
1947 __func__, ssh_err(r));
1948 /* Extract and verify signature */
1949 if ((r = sshpkt_get_string_direct(ssh, &sig, &siglen)) != 0) {
1950 error("%s: couldn't parse message: %s",
1951 __func__, ssh_err(r));
1952 goto out;
1953 }
djm@openbsd.org78607312017-12-18 23:16:23 +00001954 /*
1955 * For RSA keys, prefer to use the signature type negotiated
1956 * during KEX to the default (SHA1).
1957 */
1958 use_kexsigtype = kexsigtype == KEY_RSA &&
1959 sshkey_type_plain(ctx->keys[i]->type) == KEY_RSA;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001960 if ((r = sshkey_verify(ctx->keys[i], sig, siglen,
djm@openbsd.org04c7e282017-12-18 02:25:15 +00001961 sshbuf_ptr(signdata), sshbuf_len(signdata),
djm@openbsd.org78607312017-12-18 23:16:23 +00001962 use_kexsigtype ? ssh->kex->hostkey_alg : NULL, 0)) != 0) {
djm@openbsd.org523463a2015-02-16 22:13:32 +00001963 error("%s: server gave bad signature for %s key %zu",
1964 __func__, sshkey_type(ctx->keys[i]), i);
1965 goto out;
1966 }
1967 /* Key is good. Mark it as 'seen' */
1968 ctx->keys_seen[i] = 2;
1969 ndone++;
1970 }
1971 if (ndone != ctx->nnew)
1972 fatal("%s: ndone != ctx->nnew (%zu / %zu)", __func__,
1973 ndone, ctx->nnew); /* Shouldn't happen */
1974 ssh_packet_check_eom(ssh);
1975
1976 /* Make the edits to known_hosts */
1977 update_known_hosts(ctx);
1978 out:
1979 hostkeys_update_ctx_free(ctx);
1980}
1981
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001982/*
djm@openbsd.org894221a2017-03-10 05:01:13 +00001983 * Returns non-zero if the key is accepted by HostkeyAlgorithms.
1984 * Made slightly less trivial by the multiple RSA signature algorithm names.
1985 */
1986static int
1987key_accepted_by_hostkeyalgs(const struct sshkey *key)
1988{
1989 const char *ktype = sshkey_ssh_name(key);
1990 const char *hostkeyalgs = options.hostkeyalgorithms != NULL ?
1991 options.hostkeyalgorithms : KEX_DEFAULT_PK_ALG;
1992
1993 if (key == NULL || key->type == KEY_UNSPEC)
1994 return 0;
1995 if (key->type == KEY_RSA &&
1996 (match_pattern_list("rsa-sha2-256", hostkeyalgs, 0) == 1 ||
1997 match_pattern_list("rsa-sha2-512", hostkeyalgs, 0) == 1))
1998 return 1;
1999 return match_pattern_list(ktype, hostkeyalgs, 0) == 1;
2000}
2001
2002/*
djm@openbsd.org44732de2015-02-20 22:17:21 +00002003 * Handle hostkeys-00@openssh.com global request to inform the client of all
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002004 * the server's hostkeys. The keys are checked against the user's
2005 * HostkeyAlgorithms preference before they are accepted.
2006 */
2007static int
2008client_input_hostkeys(void)
2009{
djm@openbsd.org523463a2015-02-16 22:13:32 +00002010 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002011 const u_char *blob = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002012 size_t i, len = 0;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002013 struct sshbuf *buf = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002014 struct sshkey *key = NULL, **tmp;
2015 int r;
2016 char *fp;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002017 static int hostkeys_seen = 0; /* XXX use struct ssh */
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00002018 extern struct sockaddr_storage hostaddr; /* XXX from ssh.c */
djm@openbsd.orga63cfa22015-02-25 19:54:02 +00002019 struct hostkeys_update_ctx *ctx = NULL;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002020
2021 if (hostkeys_seen)
2022 fatal("%s: server already sent hostkeys", __func__);
djm@openbsd.org523463a2015-02-16 22:13:32 +00002023 if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK &&
2024 options.batch_mode)
2025 return 1; /* won't ask in batchmode, so don't even try */
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002026 if (!options.update_hostkeys || options.num_user_hostfiles <= 0)
2027 return 1;
djm@openbsd.orga63cfa22015-02-25 19:54:02 +00002028
2029 ctx = xcalloc(1, sizeof(*ctx));
djm@openbsd.org523463a2015-02-16 22:13:32 +00002030 while (ssh_packet_remaining(ssh) > 0) {
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002031 sshkey_free(key);
2032 key = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002033 if ((r = sshpkt_get_string_direct(ssh, &blob, &len)) != 0) {
2034 error("%s: couldn't parse message: %s",
2035 __func__, ssh_err(r));
2036 goto out;
2037 }
djm@openbsd.org44732de2015-02-20 22:17:21 +00002038 if ((r = sshkey_from_blob(blob, len, &key)) != 0) {
2039 error("%s: parse key: %s", __func__, ssh_err(r));
2040 goto out;
2041 }
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002042 fp = sshkey_fingerprint(key, options.fingerprint_hash,
2043 SSH_FP_DEFAULT);
2044 debug3("%s: received %s key %s", __func__,
2045 sshkey_type(key), fp);
2046 free(fp);
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00002047
djm@openbsd.org894221a2017-03-10 05:01:13 +00002048 if (!key_accepted_by_hostkeyalgs(key)) {
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002049 debug3("%s: %s key not permitted by HostkeyAlgorithms",
2050 __func__, sshkey_ssh_name(key));
2051 continue;
2052 }
djm@openbsd.org523463a2015-02-16 22:13:32 +00002053 /* Skip certs */
2054 if (sshkey_is_cert(key)) {
2055 debug3("%s: %s key is a certificate; skipping",
2056 __func__, sshkey_ssh_name(key));
2057 continue;
2058 }
2059 /* Ensure keys are unique */
2060 for (i = 0; i < ctx->nkeys; i++) {
2061 if (sshkey_equal(key, ctx->keys[i])) {
2062 error("%s: received duplicated %s host key",
2063 __func__, sshkey_ssh_name(key));
2064 goto out;
2065 }
2066 }
2067 /* Key is good, record it */
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00002068 if ((tmp = recallocarray(ctx->keys, ctx->nkeys, ctx->nkeys + 1,
djm@openbsd.org523463a2015-02-16 22:13:32 +00002069 sizeof(*ctx->keys))) == NULL)
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00002070 fatal("%s: recallocarray failed nkeys = %zu",
djm@openbsd.org523463a2015-02-16 22:13:32 +00002071 __func__, ctx->nkeys);
2072 ctx->keys = tmp;
2073 ctx->keys[ctx->nkeys++] = key;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002074 key = NULL;
2075 }
2076
djm@openbsd.org523463a2015-02-16 22:13:32 +00002077 if (ctx->nkeys == 0) {
djm@openbsd.org44732de2015-02-20 22:17:21 +00002078 debug("%s: server sent no hostkeys", __func__);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002079 goto out;
2080 }
djm@openbsd.org44732de2015-02-20 22:17:21 +00002081
djm@openbsd.org523463a2015-02-16 22:13:32 +00002082 if ((ctx->keys_seen = calloc(ctx->nkeys,
2083 sizeof(*ctx->keys_seen))) == NULL)
2084 fatal("%s: calloc failed", __func__);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002085
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00002086 get_hostfile_hostname_ipaddr(host,
2087 options.check_host_ip ? (struct sockaddr *)&hostaddr : NULL,
djm@openbsd.org523463a2015-02-16 22:13:32 +00002088 options.port, &ctx->host_str,
2089 options.check_host_ip ? &ctx->ip_str : NULL);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002090
djm@openbsd.org523463a2015-02-16 22:13:32 +00002091 /* Find which keys we already know about. */
2092 if ((r = hostkeys_foreach(options.user_hostfiles[0], hostkeys_find,
2093 ctx, ctx->host_str, ctx->ip_str,
2094 HKF_WANT_PARSE_KEY|HKF_WANT_MATCH)) != 0) {
2095 error("%s: hostkeys_foreach failed: %s", __func__, ssh_err(r));
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002096 goto out;
2097 }
2098
djm@openbsd.org523463a2015-02-16 22:13:32 +00002099 /* Figure out if we have any new keys to add */
2100 ctx->nnew = 0;
2101 for (i = 0; i < ctx->nkeys; i++) {
2102 if (!ctx->keys_seen[i])
2103 ctx->nnew++;
2104 }
2105
2106 debug3("%s: %zu keys from server: %zu new, %zu retained. %zu to remove",
2107 __func__, ctx->nkeys, ctx->nnew, ctx->nkeys - ctx->nnew, ctx->nold);
2108
2109 if (ctx->nnew == 0 && ctx->nold != 0) {
2110 /* We have some keys to remove. Just do it. */
2111 update_known_hosts(ctx);
2112 } else if (ctx->nnew != 0) {
2113 /*
2114 * We have received hitherto-unseen keys from the server.
2115 * Ask the server to confirm ownership of the private halves.
2116 */
2117 debug3("%s: asking server to prove ownership for %zu keys",
2118 __func__, ctx->nnew);
2119 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
2120 (r = sshpkt_put_cstring(ssh,
djm@openbsd.org44732de2015-02-20 22:17:21 +00002121 "hostkeys-prove-00@openssh.com")) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +00002122 (r = sshpkt_put_u8(ssh, 1)) != 0) /* bool: want reply */
2123 fatal("%s: cannot prepare packet: %s",
2124 __func__, ssh_err(r));
2125 if ((buf = sshbuf_new()) == NULL)
2126 fatal("%s: sshbuf_new", __func__);
2127 for (i = 0; i < ctx->nkeys; i++) {
2128 if (ctx->keys_seen[i])
2129 continue;
2130 sshbuf_reset(buf);
2131 if ((r = sshkey_putb(ctx->keys[i], buf)) != 0)
2132 fatal("%s: sshkey_putb: %s",
2133 __func__, ssh_err(r));
2134 if ((r = sshpkt_put_stringb(ssh, buf)) != 0)
2135 fatal("%s: sshpkt_put_string: %s",
2136 __func__, ssh_err(r));
2137 }
2138 if ((r = sshpkt_send(ssh)) != 0)
2139 fatal("%s: sshpkt_send: %s", __func__, ssh_err(r));
2140 client_register_global_confirm(
2141 client_global_hostkeys_private_confirm, ctx);
2142 ctx = NULL; /* will be freed in callback */
2143 }
2144
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002145 /* Success */
2146 out:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002147 hostkeys_update_ctx_free(ctx);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002148 sshkey_free(key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002149 sshbuf_free(buf);
djm@openbsd.org523463a2015-02-16 22:13:32 +00002150 /*
2151 * NB. Return success for all cases. The server doesn't need to know
2152 * what the client does with its hosts file.
2153 */
2154 return 1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002155}
2156
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002157static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002158client_input_global_request(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerc3fa4072002-01-22 23:21:58 +11002159{
2160 char *rtype;
2161 int want_reply;
2162 int success = 0;
2163
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002164 rtype = packet_get_cstring(NULL);
Damien Millerc3fa4072002-01-22 23:21:58 +11002165 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11002166 debug("client_input_global_request: rtype %s want_reply %d",
2167 rtype, want_reply);
djm@openbsd.org44732de2015-02-20 22:17:21 +00002168 if (strcmp(rtype, "hostkeys-00@openssh.com") == 0)
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002169 success = client_input_hostkeys();
Damien Millerc3fa4072002-01-22 23:21:58 +11002170 if (want_reply) {
2171 packet_start(success ?
2172 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
2173 packet_send();
2174 packet_write_wait();
2175 }
Darren Tuckera627d422013-06-02 07:31:17 +10002176 free(rtype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002177 return 0;
Damien Millerc3fa4072002-01-22 23:21:58 +11002178}
Damien Millerbd483e72000-04-30 10:00:53 +10002179
Damien Miller0e220db2004-06-15 10:34:08 +10002180void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002181client_session2_setup(struct ssh *ssh, int id, int want_tty, int want_subsystem,
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002182 const char *term, struct termios *tiop, int in_fd, struct sshbuf *cmd,
2183 char **env)
Damien Miller0e220db2004-06-15 10:34:08 +10002184{
djm@openbsd.org7082bb52018-06-09 03:01:12 +00002185 int i, j, matched, len;
2186 char *name, *val;
Darren Tucker5d78de62004-11-05 20:35:44 +11002187 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002188
2189 debug2("%s: id %d", __func__, id);
2190
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002191 if ((c = channel_lookup(ssh, id)) == NULL)
2192 fatal("%s: channel %d: unknown channel", __func__, id);
Darren Tucker5d78de62004-11-05 20:35:44 +11002193
Damien Miller0dac6fb2010-11-20 15:19:38 +11002194 packet_set_interactive(want_tty,
2195 options.ip_qos_interactive, options.ip_qos_bulk);
2196
Damien Miller0e220db2004-06-15 10:34:08 +10002197 if (want_tty) {
2198 struct winsize ws;
Damien Miller0e220db2004-06-15 10:34:08 +10002199
2200 /* Store window size in the packet. */
2201 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
2202 memset(&ws, 0, sizeof(ws));
2203
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002204 channel_request_start(ssh, id, "pty-req", 1);
2205 client_expect_confirm(ssh, id, "PTY allocation", CONFIRM_TTY);
Damien Miller0e220db2004-06-15 10:34:08 +10002206 packet_put_cstring(term != NULL ? term : "");
Damien Miller71a73672006-03-26 14:04:36 +11002207 packet_put_int((u_int)ws.ws_col);
2208 packet_put_int((u_int)ws.ws_row);
2209 packet_put_int((u_int)ws.ws_xpixel);
2210 packet_put_int((u_int)ws.ws_ypixel);
Darren Tuckerdf189fb2008-06-08 12:55:32 +10002211 if (tiop == NULL)
2212 tiop = get_saved_tio();
markus@openbsd.org89dd6152018-07-09 21:20:26 +00002213 ssh_tty_make_modes(ssh, -1, tiop);
Damien Miller0e220db2004-06-15 10:34:08 +10002214 packet_send();
2215 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11002216 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10002217 }
2218
2219 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10002220 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10002221 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10002222 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10002223 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10002224 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10002225 if ((val = strchr(name, '=')) == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +10002226 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002227 continue;
2228 }
2229 *val++ = '\0';
2230
2231 matched = 0;
2232 for (j = 0; j < options.num_send_env; j++) {
2233 if (match_pattern(name, options.send_env[j])) {
2234 matched = 1;
2235 break;
2236 }
2237 }
2238 if (!matched) {
2239 debug3("Ignored env %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10002240 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002241 continue;
2242 }
2243
2244 debug("Sending env %s = %s", name, val);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002245 channel_request_start(ssh, id, "env", 0);
Damien Miller0e220db2004-06-15 10:34:08 +10002246 packet_put_cstring(name);
2247 packet_put_cstring(val);
2248 packet_send();
Darren Tuckera627d422013-06-02 07:31:17 +10002249 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002250 }
2251 }
djm@openbsd.org7082bb52018-06-09 03:01:12 +00002252 for (i = 0; i < options.num_setenv; i++) {
2253 /* Split */
2254 name = xstrdup(options.setenv[i]);
2255 if ((val = strchr(name, '=')) == NULL) {
2256 free(name);
2257 continue;
2258 }
2259 *val++ = '\0';
2260
2261 debug("Setting env %s = %s", name, val);
2262 channel_request_start(ssh, id, "env", 0);
2263 packet_put_cstring(name);
2264 packet_put_cstring(val);
2265 packet_send();
2266 free(name);
2267 }
Damien Miller0e220db2004-06-15 10:34:08 +10002268
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002269 len = sshbuf_len(cmd);
Damien Miller0e220db2004-06-15 10:34:08 +10002270 if (len > 0) {
2271 if (len > 900)
2272 len = 900;
2273 if (want_subsystem) {
Damien Miller5771ed72008-05-19 15:35:33 +10002274 debug("Sending subsystem: %.*s",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002275 len, (const u_char*)sshbuf_ptr(cmd));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002276 channel_request_start(ssh, id, "subsystem", 1);
2277 client_expect_confirm(ssh, id, "subsystem",
2278 CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002279 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002280 debug("Sending command: %.*s",
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002281 len, (const u_char*)sshbuf_ptr(cmd));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002282 channel_request_start(ssh, id, "exec", 1);
2283 client_expect_confirm(ssh, id, "exec", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002284 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002285 packet_put_string(sshbuf_ptr(cmd), sshbuf_len(cmd));
Damien Miller0e220db2004-06-15 10:34:08 +10002286 packet_send();
2287 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002288 channel_request_start(ssh, id, "shell", 1);
2289 client_expect_confirm(ssh, id, "shell", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002290 packet_send();
2291 }
2292}
2293
Ben Lindstrombba81212001-06-25 05:01:22 +00002294static void
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002295client_init_dispatch(void)
Damien Miller1383bd82000-04-06 12:32:37 +10002296{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002297 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10002298
Damien Miller1383bd82000-04-06 12:32:37 +10002299 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2300 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2301 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2302 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10002303 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10002304 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2305 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002306 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10002307 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerb84886b2008-05-19 15:05:07 +10002308 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2309 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
Damien Millerc3fa4072002-01-22 23:21:58 +11002310 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002311
2312 /* rekeying */
2313 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10002314
2315 /* global request reply messages */
2316 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2317 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10002318}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002319
Damien Miller6c3eec72011-05-05 14:16:22 +10002320void
2321client_stop_mux(void)
2322{
2323 if (options.control_path != NULL && muxserver_sock != -1)
2324 unlink(options.control_path);
2325 /*
Darren Tucker48bf4b02012-09-07 16:38:53 +10002326 * If we are in persist mode, or don't have a shell, signal that we
2327 * should close when all active channels are closed.
Damien Miller6c3eec72011-05-05 14:16:22 +10002328 */
Darren Tucker48bf4b02012-09-07 16:38:53 +10002329 if (options.control_persist || no_shell_flag) {
Damien Miller6c3eec72011-05-05 14:16:22 +10002330 session_closed = 1;
Damien Miller4ac99c32011-06-20 14:43:31 +10002331 setproctitle("[stopped mux]");
2332 }
Damien Miller6c3eec72011-05-05 14:16:22 +10002333}
2334
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002335/* client specific fatal cleanup */
2336void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002337cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002338{
Damien Miller21771e22011-05-15 08:45:50 +10002339 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002340 leave_non_blocking();
Damien Millerb1cbfa22008-05-19 16:00:08 +10002341 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10002342 unlink(options.control_path);
Damien Millera41ccca2010-10-07 22:07:32 +11002343 ssh_kill_proxy_command();
Darren Tucker3e33cec2003-10-02 16:12:36 +10002344 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002345}