blob: 4801f4a773c49736573aa37bb88eef51fd3a8823 [file] [log] [blame]
djm@openbsd.org001aa552018-04-10 00:10:49 +00001/* $OpenBSD: clientloop.c,v 1.312 2018/04/10 00:10:49 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * The main loop for the interactive session (client side).
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
14 *
15 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 *
Damien Miller1383bd82000-04-06 12:32:37 +100038 * SSH2 support added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000039 * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110040 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110060 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
62#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110063
Damien Millerf17883e2006-03-15 11:45:54 +110064#include <sys/types.h>
Damien Miller9aec9192006-08-05 10:57:45 +100065#include <sys/ioctl.h>
Damien Millerf17883e2006-03-15 11:45:54 +110066#ifdef HAVE_SYS_STAT_H
67# include <sys/stat.h>
68#endif
Damien Miller9aec9192006-08-05 10:57:45 +100069#ifdef HAVE_SYS_TIME_H
70# include <sys/time.h>
71#endif
Damien Millere3b60b52006-07-10 21:08:03 +100072#include <sys/socket.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110073
Damien Millerc7b06362006-03-15 11:53:45 +110074#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100075#include <errno.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110076#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110077#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110078#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110079#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100080#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100081#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100082#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100083#include <string.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110084#include <termios.h>
Damien Millerd7834352006-08-05 12:39:39 +100085#include <pwd.h>
Damien Millere6b3b612006-07-24 14:01:23 +100086#include <unistd.h>
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000087#include <limits.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
Damien Millerb84886b2008-05-19 15:05:07 +100089#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100090#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000092#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093#include "packet.h"
94#include "buffer.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"
Damien Miller0bc1bd82000-11-13 22:57:25 +110098#include "key.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. */
djm@openbsd.org05164352017-04-30 23:21:54 +0000156static Buffer 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
Damien Miller0e220db2004-06-15 10:34:08 +1000191void ssh_process_session2_setup(int, int, int, Buffer *);
192
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();
Damien Miller509b0102003-12-17 16:33:10 +1100496 int ret;
497
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) {
Damien Miller95def091999-11-25 00:26:21 +1100549 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000550
551 /*
552 * We have to clear the select masks, because we return.
553 * We have to return, because the mainloop checks for the flags
554 * set by the signal handlers.
555 */
Damien Miller79faeff2001-11-12 11:06:32 +1100556 memset(*readsetp, 0, *nallocp);
557 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000558
Damien Miller95def091999-11-25 00:26:21 +1100559 if (errno == EINTR)
560 return;
561 /* Note: we might still have data in the buffers. */
562 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
563 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100564 quit_pending = 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000565 } else if (ret == 0) {
566 /*
567 * Timeout. Could have been either keepalive or rekeying.
568 * Keepalive we check here, rekeying is checked in clientloop.
569 */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000570 if (server_alive_time != 0 && server_alive_time <= monotime())
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000571 server_alive_check();
572 }
573
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000574}
575
Ben Lindstrombba81212001-06-25 05:01:22 +0000576static void
Damien Millerad833b32000-08-23 10:46:23 +1000577client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000578{
Damien Miller95def091999-11-25 00:26:21 +1100579 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000580 if (buffer_len(bout) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000581 atomicio(vwrite, fileno(stdout), buffer_ptr(bout),
582 buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000583 if (buffer_len(berr) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000584 atomicio(vwrite, fileno(stderr), buffer_ptr(berr),
585 buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000586
Damien Miller21771e22011-05-15 08:45:50 +1000587 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000588
djm@openbsd.org5b8da1f2017-09-19 04:24:22 +0000589 sshbuf_reset(bin);
590 sshbuf_reset(bout);
591 sshbuf_reset(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000592
Damien Miller95def091999-11-25 00:26:21 +1100593 /* Send the suspend signal to the program itself. */
594 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000595
Darren Tucker5d78de62004-11-05 20:35:44 +1100596 /* Reset window sizes in case they have changed */
597 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000598
Damien Miller21771e22011-05-15 08:45:50 +1000599 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000600}
601
Ben Lindstrombba81212001-06-25 05:01:22 +0000602static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100603client_process_net_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000604{
markus@openbsd.orga3068632016-01-14 16:17:39 +0000605 int len;
Darren Tucker86e30a02009-08-28 11:21:06 +1000606 char buf[SSH_IOBUFSZ];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000607
Damien Miller5428f641999-11-25 11:54:57 +1100608 /*
609 * Read input from the server, and add any such data to the buffer of
610 * the packet subsystem.
611 */
Damien Miller95def091999-11-25 00:26:21 +1100612 if (FD_ISSET(connection_in, readset)) {
613 /* Read as much as possible. */
markus@openbsd.orga3068632016-01-14 16:17:39 +0000614 len = read(connection_in, buf, sizeof(buf));
615 if (len == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000616 /*
617 * Received EOF. The remote host has closed the
618 * connection.
619 */
620 snprintf(buf, sizeof buf,
621 "Connection to %.300s closed by remote host.\r\n",
622 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000623 buffer_append(&stderr_buffer, buf, strlen(buf));
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 */
640 snprintf(buf, sizeof buf,
641 "Read from remote host %.300s: %.100s\r\n",
642 host, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100643 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100644 quit_pending = 1;
645 return;
646 }
647 packet_process_incoming(buf, len);
648 }
Damien Miller1383bd82000-04-06 12:32:37 +1000649}
650
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000651static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000652client_status_confirm(struct ssh *ssh, int type, Channel *c, void *ctx)
Damien Miller0e220db2004-06-15 10:34:08 +1000653{
Damien Miller5771ed72008-05-19 15:35:33 +1000654 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
655 char errmsg[256];
656 int tochan;
Darren Tuckerfc959702004-07-17 16:12:08 +1000657
Damien Miller555f3b82011-05-15 08:48:05 +1000658 /*
659 * If a TTY was explicitly requested, then a failure to allocate
660 * one is fatal.
661 */
662 if (cr->action == CONFIRM_TTY &&
663 (options.request_tty == REQUEST_TTY_FORCE ||
664 options.request_tty == REQUEST_TTY_YES))
665 cr->action = CONFIRM_CLOSE;
666
djm@openbsd.org001aa552018-04-10 00:10:49 +0000667 /* XXX suppress on mux _client_ quietmode */
Damien Miller5771ed72008-05-19 15:35:33 +1000668 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
Damien Millere1537f92010-01-26 13:26:22 +1100669 c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
Damien Miller0e220db2004-06-15 10:34:08 +1000670
Damien Miller5771ed72008-05-19 15:35:33 +1000671 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
672 debug2("%s request accepted on channel %d",
673 cr->request_type, c->self);
674 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
675 if (tochan) {
676 snprintf(errmsg, sizeof(errmsg),
677 "%s request failed\r\n", cr->request_type);
678 } else {
679 snprintf(errmsg, sizeof(errmsg),
680 "%s request failed on channel %d",
681 cr->request_type, c->self);
682 }
683 /* If error occurred on primary session channel, then exit */
Damien Miller555f3b82011-05-15 08:48:05 +1000684 if (cr->action == CONFIRM_CLOSE && c->self == session_ident)
Damien Miller5771ed72008-05-19 15:35:33 +1000685 fatal("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000686 /*
687 * If error occurred on mux client, append to
688 * their stderr.
689 */
690 if (tochan) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000691 buffer_append(c->extended, errmsg, strlen(errmsg));
Damien Miller555f3b82011-05-15 08:48:05 +1000692 } else
Damien Miller5771ed72008-05-19 15:35:33 +1000693 error("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000694 if (cr->action == CONFIRM_TTY) {
695 /*
696 * If a TTY allocation error occurred, then arrange
697 * for the correct TTY to leave raw mode.
698 */
699 if (c->self == session_ident)
700 leave_raw_mode(0);
701 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000702 mux_tty_alloc_failed(ssh, c);
Damien Miller555f3b82011-05-15 08:48:05 +1000703 } else if (cr->action == CONFIRM_CLOSE) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000704 chan_read_failed(ssh, c);
705 chan_write_failed(ssh, c);
Damien Miller5771ed72008-05-19 15:35:33 +1000706 }
Damien Miller0e220db2004-06-15 10:34:08 +1000707 }
Darren Tuckera627d422013-06-02 07:31:17 +1000708 free(cr);
Damien Miller5771ed72008-05-19 15:35:33 +1000709}
Damien Miller0e220db2004-06-15 10:34:08 +1000710
Damien Miller5771ed72008-05-19 15:35:33 +1000711static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000712client_abandon_status_confirm(struct ssh *ssh, Channel *c, void *ctx)
Damien Miller5771ed72008-05-19 15:35:33 +1000713{
Darren Tuckera627d422013-06-02 07:31:17 +1000714 free(ctx);
Damien Miller5771ed72008-05-19 15:35:33 +1000715}
716
Damien Miller6d7b4372011-06-23 08:31:57 +1000717void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000718client_expect_confirm(struct ssh *ssh, int id, const char *request,
Damien Miller555f3b82011-05-15 08:48:05 +1000719 enum confirm_action action)
Damien Miller5771ed72008-05-19 15:35:33 +1000720{
Damien Miller6c81fee2013-11-08 12:19:55 +1100721 struct channel_reply_ctx *cr = xcalloc(1, sizeof(*cr));
Damien Miller5771ed72008-05-19 15:35:33 +1000722
723 cr->request_type = request;
Damien Miller555f3b82011-05-15 08:48:05 +1000724 cr->action = action;
Damien Miller5771ed72008-05-19 15:35:33 +1000725
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000726 channel_register_status_confirm(ssh, id, client_status_confirm,
Damien Miller5771ed72008-05-19 15:35:33 +1000727 client_abandon_status_confirm, cr);
Damien Miller0e220db2004-06-15 10:34:08 +1000728}
729
Darren Tucker9f407c42008-06-13 04:50:27 +1000730void
731client_register_global_confirm(global_confirm_cb *cb, void *ctx)
732{
Damien Millerb9d3bee2008-07-16 22:40:52 +1000733 struct global_confirm *gc, *last_gc;
Darren Tucker9f407c42008-06-13 04:50:27 +1000734
735 /* Coalesce identical callbacks */
Damien Millerb9d3bee2008-07-16 22:40:52 +1000736 last_gc = TAILQ_LAST(&global_confirms, global_confirms);
737 if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
738 if (++last_gc->ref_count >= INT_MAX)
739 fatal("%s: last_gc->ref_count = %d",
740 __func__, last_gc->ref_count);
Darren Tucker9f407c42008-06-13 04:50:27 +1000741 return;
742 }
743
Damien Miller6c81fee2013-11-08 12:19:55 +1100744 gc = xcalloc(1, sizeof(*gc));
Darren Tucker9f407c42008-06-13 04:50:27 +1000745 gc->cb = cb;
746 gc->ctx = ctx;
747 gc->ref_count = 1;
748 TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
749}
750
Damien Miller0e220db2004-06-15 10:34:08 +1000751static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000752process_cmdline(struct ssh *ssh)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000753{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000754 void (*handler)(int);
Damien Miller7acefbb2014-07-18 14:11:24 +1000755 char *s, *cmd;
756 int ok, delete = 0, local = 0, remote = 0, dynamic = 0;
757 struct Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000758
Damien Miller1d2c4562014-02-04 11:18:20 +1100759 memset(&fwd, 0, sizeof(fwd));
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100760
Damien Miller21771e22011-05-15 08:45:50 +1000761 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000762 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000763 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000764 if (s == NULL)
765 goto out;
Damien Millerfdb23062013-11-21 13:57:15 +1100766 while (isspace((u_char)*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000767 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000768 if (*s == '-')
769 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000770 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000771 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000772
Darren Tucker1973c882004-05-24 10:34:36 +1000773 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000774 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000775 logit(" -L[bind_address:]port:host:hostport "
776 "Request local forward");
777 logit(" -R[bind_address:]port:host:hostport "
778 "Request remote forward");
Damien Miller0164cb82008-11-05 16:30:31 +1100779 logit(" -D[bind_address:]port "
780 "Request dynamic forward");
Damien Millerff773642011-09-22 21:39:48 +1000781 logit(" -KL[bind_address:]port "
782 "Cancel local forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000783 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000784 "Cancel remote forward");
Damien Millerff773642011-09-22 21:39:48 +1000785 logit(" -KD[bind_address:]port "
786 "Cancel dynamic forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100787 if (!options.permit_local_command)
788 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000789 logit(" !args "
790 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100791 goto out;
792 }
793
794 if (*s == '!' && options.permit_local_command) {
795 s++;
796 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000797 goto out;
798 }
799
800 if (*s == 'K') {
801 delete = 1;
802 s++;
803 }
Damien Miller0164cb82008-11-05 16:30:31 +1100804 if (*s == 'L')
805 local = 1;
806 else if (*s == 'R')
807 remote = 1;
808 else if (*s == 'D')
809 dynamic = 1;
810 else {
Damien Miller996acd22003-04-09 20:59:48 +1000811 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000812 goto out;
813 }
Damien Miller0164cb82008-11-05 16:30:31 +1100814
Damien Millerfdb23062013-11-21 13:57:15 +1100815 while (isspace((u_char)*++s))
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100816 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000817
Damien Millere1537f92010-01-26 13:26:22 +1100818 /* XXX update list of forwards in options */
Darren Tuckere7066df2004-05-24 10:18:05 +1000819 if (delete) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000820 /* We pass 1 for dynamicfwd to restrict to 1 or 2 fields. */
821 if (!parse_forward(&fwd, s, 1, 0)) {
822 logit("Bad forwarding close specification.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000823 goto out;
824 }
Damien Millerff773642011-09-22 21:39:48 +1000825 if (remote)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000826 ok = channel_request_rforward_cancel(ssh, &fwd) == 0;
Damien Millerff773642011-09-22 21:39:48 +1000827 else if (dynamic)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000828 ok = channel_cancel_lport_listener(ssh, &fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +1000829 0, &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000830 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000831 ok = channel_cancel_lport_listener(ssh, &fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +1000832 CHANNEL_CANCEL_PORT_STATIC,
833 &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000834 if (!ok) {
dtucker@openbsd.org9390b002017-01-29 21:35:23 +0000835 logit("Unknown port forwarding.");
Damien Millerff773642011-09-22 21:39:48 +1000836 goto out;
837 }
838 logit("Canceled forwarding.");
Darren Tuckere7066df2004-05-24 10:18:05 +1000839 } else {
Damien Miller4bf648f2009-02-14 16:28:21 +1100840 if (!parse_forward(&fwd, s, dynamic, remote)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000841 logit("Bad forwarding specification.");
842 goto out;
843 }
Damien Miller0164cb82008-11-05 16:30:31 +1100844 if (local || dynamic) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000845 if (!channel_setup_local_fwd_listener(ssh, &fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +1000846 &options.fwd_opts)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000847 logit("Port forwarding failed.");
848 goto out;
849 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100850 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000851 if (channel_request_remote_forwarding(ssh, &fwd) < 0) {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000852 logit("Port forwarding failed.");
853 goto out;
854 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100855 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000856 logit("Forwarding port.");
857 }
858
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000859out:
860 signal(SIGINT, handler);
Damien Miller21771e22011-05-15 08:45:50 +1000861 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tuckera627d422013-06-02 07:31:17 +1000862 free(cmd);
863 free(fwd.listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000864 free(fwd.listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000865 free(fwd.connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000866 free(fwd.connect_path);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000867}
868
Darren Tucker92a39cf2012-09-07 11:20:20 +1000869/* reasons to suppress output of an escape command in help output */
870#define SUPPRESS_NEVER 0 /* never suppress, always show */
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000871#define SUPPRESS_MUXCLIENT 1 /* don't show in mux client sessions */
872#define SUPPRESS_MUXMASTER 2 /* don't show in mux master sessions */
873#define SUPPRESS_SYSLOG 4 /* don't show when logging to syslog */
Darren Tucker92a39cf2012-09-07 11:20:20 +1000874struct escape_help_text {
875 const char *cmd;
876 const char *text;
877 unsigned int flags;
878};
879static struct escape_help_text esc_txt[] = {
880 {".", "terminate session", SUPPRESS_MUXMASTER},
881 {".", "terminate connection (and any multiplexed sessions)",
882 SUPPRESS_MUXCLIENT},
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000883 {"B", "send a BREAK to the remote system", SUPPRESS_NEVER},
Darren Tucker92a39cf2012-09-07 11:20:20 +1000884 {"C", "open a command line", SUPPRESS_MUXCLIENT},
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000885 {"R", "request rekey", SUPPRESS_NEVER},
Darren Tuckerca0d0fd2012-09-07 11:22:24 +1000886 {"V/v", "decrease/increase verbosity (LogLevel)", SUPPRESS_MUXCLIENT},
Darren Tucker92a39cf2012-09-07 11:20:20 +1000887 {"^Z", "suspend ssh", SUPPRESS_MUXCLIENT},
888 {"#", "list forwarded connections", SUPPRESS_NEVER},
889 {"&", "background ssh (when waiting for connections to terminate)",
890 SUPPRESS_MUXCLIENT},
891 {"?", "this message", SUPPRESS_NEVER},
892};
893
894static void
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000895print_escape_help(Buffer *b, int escape_char, int mux_client, int using_stderr)
Darren Tucker92a39cf2012-09-07 11:20:20 +1000896{
897 unsigned int i, suppress_flags;
898 char string[1024];
899
900 snprintf(string, sizeof string, "%c?\r\n"
901 "Supported escape sequences:\r\n", escape_char);
902 buffer_append(b, string, strlen(string));
903
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000904 suppress_flags =
Darren Tucker92a39cf2012-09-07 11:20:20 +1000905 (mux_client ? SUPPRESS_MUXCLIENT : 0) |
906 (mux_client ? 0 : SUPPRESS_MUXMASTER) |
907 (using_stderr ? 0 : SUPPRESS_SYSLOG);
908
909 for (i = 0; i < sizeof(esc_txt)/sizeof(esc_txt[0]); i++) {
910 if (esc_txt[i].flags & suppress_flags)
911 continue;
Darren Tuckerca0d0fd2012-09-07 11:22:24 +1000912 snprintf(string, sizeof string, " %c%-3s - %s\r\n",
Darren Tucker92a39cf2012-09-07 11:20:20 +1000913 escape_char, esc_txt[i].cmd, esc_txt[i].text);
914 buffer_append(b, string, strlen(string));
915 }
916
917 snprintf(string, sizeof string,
Darren Tuckerca0d0fd2012-09-07 11:22:24 +1000918 " %c%c - send the escape character by typing it twice\r\n"
Darren Tucker92a39cf2012-09-07 11:20:20 +1000919 "(Note that escapes are only recognized immediately after "
920 "newline.)\r\n", escape_char, escape_char);
921 buffer_append(b, string, strlen(string));
922}
923
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000924/*
naddy@openbsd.org768405f2017-05-03 21:08:09 +0000925 * Process the characters one by one.
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000926 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000927static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000928process_escapes(struct ssh *ssh, Channel *c,
929 Buffer *bin, Buffer *bout, Buffer *berr,
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000930 char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +1000931{
932 char string[1024];
933 pid_t pid;
934 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000935 u_int i;
936 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +1000937 char *s;
djm@openbsd.org05164352017-04-30 23:21:54 +0000938 struct escape_filter_ctx *efc = c->filter_ctx == NULL ?
939 NULL : (struct escape_filter_ctx *)c->filter_ctx;
Damien Millerad833b32000-08-23 10:46:23 +1000940
djm@openbsd.org05164352017-04-30 23:21:54 +0000941 if (c->filter_ctx == NULL)
942 return 0;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000943
Damien Millereccb9de2005-06-17 12:59:34 +1000944 if (len <= 0)
945 return (0);
946
947 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +1000948 /* Get one character at a time. */
949 ch = buf[i];
950
djm@openbsd.org05164352017-04-30 23:21:54 +0000951 if (efc->escape_pending) {
Damien Millerad833b32000-08-23 10:46:23 +1000952 /* We have previously seen an escape character. */
953 /* Clear the flag now. */
djm@openbsd.org05164352017-04-30 23:21:54 +0000954 efc->escape_pending = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000955
956 /* Process the escaped character. */
957 switch (ch) {
958 case '.':
959 /* Terminate the connection. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000960 snprintf(string, sizeof string, "%c.\r\n",
djm@openbsd.org05164352017-04-30 23:21:54 +0000961 efc->escape_char);
Damien Millerad833b32000-08-23 10:46:23 +1000962 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +1000963
Damien Millere1537f92010-01-26 13:26:22 +1100964 if (c && c->ctl_chan != -1) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000965 chan_read_failed(ssh, c);
966 chan_write_failed(ssh, c);
967 if (c->detach_user) {
968 c->detach_user(ssh,
969 c->self, NULL);
970 }
Damien Miller36187092013-06-10 13:07:11 +1000971 c->type = SSH_CHANNEL_ABANDONED;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000972 buffer_clear(c->input);
973 chan_ibuf_empty(ssh, c);
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000974 return 0;
975 } else
976 quit_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +1000977 return -1;
978
979 case 'Z' - 64:
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000980 /* XXX support this for mux clients */
Damien Millere1537f92010-01-26 13:26:22 +1100981 if (c && c->ctl_chan != -1) {
Darren Tuckerf111d402012-09-07 11:21:42 +1000982 char b[16];
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000983 noescape:
Darren Tuckerf111d402012-09-07 11:21:42 +1000984 if (ch == 'Z' - 64)
985 snprintf(b, sizeof b, "^Z");
986 else
987 snprintf(b, sizeof b, "%c", ch);
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000988 snprintf(string, sizeof string,
Darren Tuckerf111d402012-09-07 11:21:42 +1000989 "%c%s escape not available to "
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000990 "multiplexed sessions\r\n",
djm@openbsd.org05164352017-04-30 23:21:54 +0000991 efc->escape_char, b);
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000992 buffer_append(berr, string,
993 strlen(string));
994 continue;
995 }
Darren Tucker4d5cd332008-06-13 04:51:14 +1000996 /* Suspend the program. Inform the user */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000997 snprintf(string, sizeof string,
djm@openbsd.org05164352017-04-30 23:21:54 +0000998 "%c^Z [suspend ssh]\r\n", efc->escape_char);
Damien Millerad833b32000-08-23 10:46:23 +1000999 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001000
1001 /* Restore terminal modes and suspend. */
1002 client_suspend_self(bin, bout, berr);
1003
1004 /* We have been continued. */
1005 continue;
1006
Damien Miller54c45982003-05-15 10:20:13 +10001007 case 'B':
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001008 snprintf(string, sizeof string,
djm@openbsd.org05164352017-04-30 23:21:54 +00001009 "%cB\r\n", efc->escape_char);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001010 buffer_append(berr, string, strlen(string));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001011 channel_request_start(ssh, c->self, "break", 0);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001012 packet_put_int(1000);
1013 packet_send();
Damien Miller54c45982003-05-15 10:20:13 +10001014 continue;
1015
Ben Lindstromf28f6342001-04-04 02:03:04 +00001016 case 'R':
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001017 if (datafellows & SSH_BUG_NOREKEY)
1018 logit("Server does not "
1019 "support re-keying");
1020 else
1021 need_rekeying = 1;
Ben Lindstromf28f6342001-04-04 02:03:04 +00001022 continue;
1023
Darren Tucker50a48d02012-09-06 21:25:37 +10001024 case 'V':
1025 /* FALLTHROUGH */
1026 case 'v':
1027 if (c && c->ctl_chan != -1)
1028 goto noescape;
1029 if (!log_is_on_stderr()) {
1030 snprintf(string, sizeof string,
1031 "%c%c [Logging to syslog]\r\n",
djm@openbsd.org05164352017-04-30 23:21:54 +00001032 efc->escape_char, ch);
Darren Tucker50a48d02012-09-06 21:25:37 +10001033 buffer_append(berr, string,
1034 strlen(string));
1035 continue;
1036 }
1037 if (ch == 'V' && options.log_level >
1038 SYSLOG_LEVEL_QUIET)
1039 log_change_level(--options.log_level);
1040 if (ch == 'v' && options.log_level <
1041 SYSLOG_LEVEL_DEBUG3)
1042 log_change_level(++options.log_level);
1043 snprintf(string, sizeof string,
djm@openbsd.org05164352017-04-30 23:21:54 +00001044 "%c%c [LogLevel %s]\r\n",
1045 efc->escape_char, ch,
Darren Tucker50a48d02012-09-06 21:25:37 +10001046 log_level_name(options.log_level));
1047 buffer_append(berr, string, strlen(string));
1048 continue;
1049
Damien Millerad833b32000-08-23 10:46:23 +10001050 case '&':
Damien Millere1537f92010-01-26 13:26:22 +11001051 if (c && c->ctl_chan != -1)
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001052 goto noescape;
Damien Millerad833b32000-08-23 10:46:23 +10001053 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001054 * Detach the program (continue to serve
1055 * connections, but put in background and no
1056 * more new connections).
Damien Millerad833b32000-08-23 10:46:23 +10001057 */
Damien Miller96507ef2001-11-12 10:52:25 +11001058 /* Restore tty modes. */
Damien Miller21771e22011-05-15 08:45:50 +10001059 leave_raw_mode(
1060 options.request_tty == REQUEST_TTY_FORCE);
Damien Miller96507ef2001-11-12 10:52:25 +11001061
1062 /* Stop listening for new connections. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001063 channel_stop_listening(ssh);
Damien Miller96507ef2001-11-12 10:52:25 +11001064
1065 snprintf(string, sizeof string,
djm@openbsd.org05164352017-04-30 23:21:54 +00001066 "%c& [backgrounded]\n", efc->escape_char);
Damien Miller96507ef2001-11-12 10:52:25 +11001067 buffer_append(berr, string, strlen(string));
1068
1069 /* Fork into background. */
1070 pid = fork();
1071 if (pid < 0) {
1072 error("fork: %.100s", strerror(errno));
1073 continue;
1074 }
1075 if (pid != 0) { /* This is the parent. */
1076 /* The parent just exits. */
1077 exit(0);
1078 }
1079 /* The child continues serving connections. */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001080 buffer_append(bin, "\004", 1);
1081 /* fake EOF on stdin */
1082 return -1;
Damien Millerad833b32000-08-23 10:46:23 +10001083 case '?':
djm@openbsd.org05164352017-04-30 23:21:54 +00001084 print_escape_help(berr, efc->escape_char,
Darren Tucker92a39cf2012-09-07 11:20:20 +10001085 (c && c->ctl_chan != -1),
1086 log_is_on_stderr());
Damien Millerad833b32000-08-23 10:46:23 +10001087 continue;
1088
1089 case '#':
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001090 snprintf(string, sizeof string, "%c#\r\n",
djm@openbsd.org05164352017-04-30 23:21:54 +00001091 efc->escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001092 buffer_append(berr, string, strlen(string));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001093 s = channel_open_message(ssh);
Damien Millerad833b32000-08-23 10:46:23 +10001094 buffer_append(berr, s, strlen(s));
Darren Tuckera627d422013-06-02 07:31:17 +10001095 free(s);
Damien Millerad833b32000-08-23 10:46:23 +10001096 continue;
1097
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001098 case 'C':
Damien Millere1537f92010-01-26 13:26:22 +11001099 if (c && c->ctl_chan != -1)
Damien Miller586b0052008-12-09 14:11:32 +11001100 goto noescape;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001101 process_cmdline(ssh);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001102 continue;
1103
Damien Millerad833b32000-08-23 10:46:23 +10001104 default:
djm@openbsd.org05164352017-04-30 23:21:54 +00001105 if (ch != efc->escape_char) {
1106 buffer_put_char(bin, efc->escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001107 bytes++;
1108 }
1109 /* Escaped characters fall through here */
1110 break;
1111 }
1112 } else {
1113 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001114 * The previous character was not an escape char.
1115 * Check if this is an escape.
Damien Millerad833b32000-08-23 10:46:23 +10001116 */
djm@openbsd.org05164352017-04-30 23:21:54 +00001117 if (last_was_cr && ch == efc->escape_char) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001118 /*
1119 * It is. Set the flag and continue to
1120 * next character.
1121 */
djm@openbsd.org05164352017-04-30 23:21:54 +00001122 efc->escape_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001123 continue;
1124 }
1125 }
1126
1127 /*
1128 * Normal character. Record whether it was a newline,
1129 * and append it to the buffer.
1130 */
1131 last_was_cr = (ch == '\r' || ch == '\n');
1132 buffer_put_char(bin, ch);
1133 bytes++;
1134 }
1135 return bytes;
1136}
1137
Damien Miller5428f641999-11-25 11:54:57 +11001138/*
Damien Millerb38eff82000-04-01 11:09:21 +10001139 * Get packets from the connection input buffer, and process them as long as
1140 * there are packets available.
1141 *
1142 * Any unknown packets received during the actual
1143 * session cause the session to terminate. This is
1144 * intended to make debugging easier since no
1145 * confirmations are sent. Any compatible protocol
1146 * extensions must be negotiated during the
1147 * preparatory phase.
1148 */
1149
Ben Lindstrombba81212001-06-25 05:01:22 +00001150static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001151client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001152{
markus@openbsd.org92e9fe62017-05-31 07:00:13 +00001153 ssh_dispatch_run_fatal(active_state, DISPATCH_NONBLOCK, &quit_pending);
Damien Millerb38eff82000-04-01 11:09:21 +10001154}
1155
Damien Millerad833b32000-08-23 10:46:23 +10001156/* scan buf[] for '~' before sending data to the peer */
1157
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001158/* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1159void *
1160client_new_escape_filter_ctx(int escape_char)
1161{
1162 struct escape_filter_ctx *ret;
1163
Damien Miller6c81fee2013-11-08 12:19:55 +11001164 ret = xcalloc(1, sizeof(*ret));
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001165 ret->escape_pending = 0;
1166 ret->escape_char = escape_char;
1167 return (void *)ret;
1168}
1169
Darren Tucker84c56f52008-06-13 04:55:46 +10001170/* Free the escape filter context on channel free */
1171void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001172client_filter_cleanup(struct ssh *ssh, int cid, void *ctx)
Darren Tucker84c56f52008-06-13 04:55:46 +10001173{
Darren Tuckera627d422013-06-02 07:31:17 +10001174 free(ctx);
Darren Tucker84c56f52008-06-13 04:55:46 +10001175}
1176
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001177int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001178client_simple_escape_filter(struct ssh *ssh, Channel *c, char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001179{
Damien Miller5771ed72008-05-19 15:35:33 +10001180 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1181 return 0;
1182
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001183 return process_escapes(ssh, c, c->input, c->output, c->extended,
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001184 buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001185}
1186
Ben Lindstrombba81212001-06-25 05:01:22 +00001187static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001188client_channel_closed(struct ssh *ssh, int id, void *arg)
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001189{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001190 channel_cancel_cleanup(ssh, id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001191 session_closed = 1;
Damien Miller21771e22011-05-15 08:45:50 +10001192 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001193}
1194
Damien Millerb38eff82000-04-01 11:09:21 +10001195/*
Damien Miller5428f641999-11-25 11:54:57 +11001196 * Implements the interactive session with the server. This is called after
1197 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001198 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1199 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001200 */
Damien Miller4af51302000-04-16 11:18:38 +10001201int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001202client_loop(struct ssh *ssh, int have_pty, int escape_char_arg,
1203 int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001204{
Damien Miller5e953212001-01-30 09:14:00 +11001205 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001206 double start_time, total_time;
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001207 int r, max_fd = 0, max_fd2 = 0, len;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001208 u_int64_t ibytes, obytes;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001209 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001210 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001211
Damien Miller95def091999-11-25 00:26:21 +11001212 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001213
semarie@openbsd.orgd7d2bc92015-12-26 07:46:03 +00001214 if (options.control_master &&
djm@openbsd.org368dd972016-07-23 02:54:08 +00001215 !option_clear_or_none(options.control_path)) {
semarie@openbsd.orgd7d2bc92015-12-26 07:46:03 +00001216 debug("pledge: id");
tb@openbsd.org372807c2016-07-11 21:38:13 +00001217 if (pledge("stdio rpath wpath cpath unix inet dns recvfd proc exec id tty",
semarie@openbsd.orgd7d2bc92015-12-26 07:46:03 +00001218 NULL) == -1)
1219 fatal("%s pledge(): %s", __func__, strerror(errno));
1220
1221 } else if (options.forward_x11 || options.permit_local_command) {
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001222 debug("pledge: exec");
1223 if (pledge("stdio rpath wpath cpath unix inet dns proc exec tty",
1224 NULL) == -1)
1225 fatal("%s pledge(): %s", __func__, strerror(errno));
1226
1227 } else if (options.update_hostkeys) {
1228 debug("pledge: filesystem full");
1229 if (pledge("stdio rpath wpath cpath unix inet dns proc tty",
1230 NULL) == -1)
1231 fatal("%s pledge(): %s", __func__, strerror(errno));
1232
djm@openbsd.org368dd972016-07-23 02:54:08 +00001233 } else if (!option_clear_or_none(options.proxy_command) ||
1234 fork_after_authentication_flag) {
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001235 debug("pledge: proc");
1236 if (pledge("stdio cpath unix inet dns proc tty", NULL) == -1)
1237 fatal("%s pledge(): %s", __func__, strerror(errno));
1238
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001239 } else {
1240 debug("pledge: network");
mestre@openbsd.orge2386452017-06-23 07:24:48 +00001241 if (pledge("stdio unix inet dns proc tty", NULL) == -1)
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001242 fatal("%s pledge(): %s", __func__, strerror(errno));
1243 }
1244
dtucker@openbsd.org@openbsd.org2d638e92017-11-25 05:58:47 +00001245 start_time = monotime_double();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001246
Damien Miller95def091999-11-25 00:26:21 +11001247 /* Initialize variables. */
Damien Miller95def091999-11-25 00:26:21 +11001248 last_was_cr = 1;
1249 exit_status = -1;
Damien Miller95def091999-11-25 00:26:21 +11001250 connection_in = packet_get_connection_in();
1251 connection_out = packet_get_connection_out();
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001252 max_fd = MAXIMUM(connection_in, connection_out);
Damien Miller5e953212001-01-30 09:14:00 +11001253
Damien Miller95def091999-11-25 00:26:21 +11001254 quit_pending = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001255
Damien Miller95def091999-11-25 00:26:21 +11001256 /* Initialize buffers. */
Damien Miller95def091999-11-25 00:26:21 +11001257 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001258
Damien Millerb38eff82000-04-01 11:09:21 +10001259 client_init_dispatch();
1260
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001261 /*
1262 * Set signal handlers, (e.g. to restore non-blocking mode)
1263 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1264 */
Darren Tucker07336da2004-11-05 20:02:16 +11001265 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1266 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001267 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1268 signal(SIGINT, signal_handler);
1269 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1270 signal(SIGQUIT, signal_handler);
1271 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1272 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001273 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001274
Damien Miller95def091999-11-25 00:26:21 +11001275 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001276 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001277
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001278 session_ident = ssh2_chan_id;
1279 if (session_ident != -1) {
1280 if (escape_char_arg != SSH_ESCAPECHAR_NONE) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001281 channel_register_filter(ssh, session_ident,
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001282 client_simple_escape_filter, NULL,
1283 client_filter_cleanup,
1284 client_new_escape_filter_ctx(
1285 escape_char_arg));
Damien Miller6c3eec72011-05-05 14:16:22 +10001286 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001287 channel_register_cleanup(ssh, session_ident,
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001288 client_channel_closed, 0);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001289 }
Damien Millerad833b32000-08-23 10:46:23 +10001290
Damien Miller95def091999-11-25 00:26:21 +11001291 /* Main loop of the client for the interactive session mode. */
1292 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001293
Damien Miller5428f641999-11-25 11:54:57 +11001294 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001295 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001296
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001297 if (session_closed && !channel_still_open(ssh))
Damien Miller1383bd82000-04-06 12:32:37 +10001298 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001299
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001300 if (ssh_packet_is_rekeying(ssh)) {
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001301 debug("rekeying in progress");
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001302 } else if (need_rekeying) {
1303 /* manual rekey request */
1304 debug("need rekeying");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001305 if ((r = kex_start_rekex(ssh)) != 0)
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001306 fatal("%s: kex_start_rekex: %s", __func__,
1307 ssh_err(r));
1308 need_rekeying = 0;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001309 } else {
1310 /*
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001311 * Make packets from buffered channel data, and
1312 * enqueue them for sending to the server.
1313 */
1314 if (packet_not_very_much_data_to_write())
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001315 channel_output_poll(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001316
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001317 /*
1318 * Check if the window size has changed, and buffer a
1319 * message about it to the server if so.
1320 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001321 client_check_window_change(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001322
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001323 if (quit_pending)
1324 break;
1325 }
Damien Miller5428f641999-11-25 11:54:57 +11001326 /*
1327 * Wait until we have something to do (something becomes
1328 * available on one of the descriptors).
1329 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001330 max_fd2 = max_fd;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001331 client_wait_until_can_do_something(ssh, &readset, &writeset,
1332 &max_fd2, &nalloc, ssh_packet_is_rekeying(ssh));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001333
Damien Miller95def091999-11-25 00:26:21 +11001334 if (quit_pending)
1335 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001336
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001337 /* Do channel operations unless rekeying in progress. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001338 if (!ssh_packet_is_rekeying(ssh))
1339 channel_after_select(ssh, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001340
Damien Miller1383bd82000-04-06 12:32:37 +10001341 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001342 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001343
Damien Miller1383bd82000-04-06 12:32:37 +10001344 if (quit_pending)
1345 break;
1346
Darren Tucker4d5cd332008-06-13 04:51:14 +10001347 /*
1348 * Send as much buffered packet data as possible to the
1349 * sender.
1350 */
Damien Miller5e953212001-01-30 09:14:00 +11001351 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001352 packet_write_poll();
Damien Millere11e1ea2010-08-03 16:04:46 +10001353
1354 /*
1355 * If we are a backgrounded control master, and the
1356 * timeout has expired without any active client
1357 * connections, then quit.
1358 */
1359 if (control_persist_exit_time > 0) {
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001360 if (monotime() >= control_persist_exit_time) {
Damien Millere11e1ea2010-08-03 16:04:46 +10001361 debug("ControlPersist timeout expired");
1362 break;
1363 }
1364 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001365 }
Darren Tuckera627d422013-06-02 07:31:17 +10001366 free(readset);
1367 free(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001368
Damien Miller95def091999-11-25 00:26:21 +11001369 /* Terminate the session. */
1370
1371 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001372 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001373
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001374 packet_start(SSH2_MSG_DISCONNECT);
1375 packet_put_int(SSH2_DISCONNECT_BY_APPLICATION);
1376 packet_put_cstring("disconnected by user");
1377 packet_put_cstring(""); /* language tag */
1378 packet_send();
1379 packet_write_wait();
Darren Tucker12b4a652009-06-21 18:14:48 +10001380
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001381 channel_free_all(ssh);
Damien Miller95def091999-11-25 00:26:21 +11001382
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001383 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001384 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001385
1386 /* restore blocking io */
1387 if (!isatty(fileno(stdin)))
1388 unset_nonblock(fileno(stdin));
1389 if (!isatty(fileno(stdout)))
1390 unset_nonblock(fileno(stdout));
1391 if (!isatty(fileno(stderr)))
1392 unset_nonblock(fileno(stderr));
1393
Damien Millerd6965512003-12-17 16:31:53 +11001394 /*
1395 * If there was no shell or command requested, there will be no remote
1396 * exit status to be returned. In that case, clear error code if the
1397 * connection was deliberately terminated at this end.
1398 */
1399 if (no_shell_flag && received_signal == SIGTERM) {
1400 received_signal = 0;
1401 exit_status = 0;
1402 }
1403
dtucker@openbsd.orgb1e72df2017-07-14 03:18:21 +00001404 if (received_signal) {
1405 verbose("Killed by signal %d.", (int) received_signal);
1406 cleanup_exit(0);
1407 }
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001408
1409 /*
1410 * In interactive mode (with pseudo tty) display a message indicating
1411 * that the connection has been closed.
1412 */
1413 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001414 snprintf(buf, sizeof buf,
1415 "Connection to %.64s closed.\r\n", host);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001416 buffer_append(&stderr_buffer, buf, strlen(buf));
1417 }
1418
Damien Miller95def091999-11-25 00:26:21 +11001419 /* Output any buffered data for stderr. */
Damien Millercfd6e4f2011-01-16 23:18:33 +11001420 if (buffer_len(&stderr_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001421 len = atomicio(vwrite, fileno(stderr),
1422 buffer_ptr(&stderr_buffer), buffer_len(&stderr_buffer));
Damien Millercfd6e4f2011-01-16 23:18:33 +11001423 if (len < 0 || (u_int)len != buffer_len(&stderr_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001424 error("Write failed flushing stderr buffer.");
Damien Millercfd6e4f2011-01-16 23:18:33 +11001425 else
1426 buffer_consume(&stderr_buffer, len);
Damien Miller95def091999-11-25 00:26:21 +11001427 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001428
Damien Miller95def091999-11-25 00:26:21 +11001429 /* Clear and free any buffers. */
jsg@openbsd.org458abc22016-01-23 05:31:35 +00001430 explicit_bzero(buf, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11001431 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001432
Damien Miller95def091999-11-25 00:26:21 +11001433 /* Report bytes transferred, and transfer rates. */
dtucker@openbsd.org@openbsd.org2d638e92017-11-25 05:58:47 +00001434 total_time = monotime_double() - start_time;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001435 packet_get_bytes(&ibytes, &obytes);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001436 verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
Damien Miller821de0a2011-01-11 17:20:29 +11001437 (unsigned long long)obytes, (unsigned long long)ibytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001438 if (total_time > 0)
Damien Millerb61f3fc2008-07-11 17:36:48 +10001439 verbose("Bytes per second: sent %.1f, received %.1f",
1440 obytes / total_time, ibytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001441 /* Return the exit status of the program. */
1442 debug("Exit status %d", exit_status);
1443 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001444}
Damien Millerb38eff82000-04-01 11:09:21 +10001445
1446/*********/
1447
Ben Lindstrombba81212001-06-25 05:01:22 +00001448static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001449client_request_forwarded_tcpip(struct ssh *ssh, const char *request_type,
1450 int rchan, u_int rwindow, u_int rmaxpack)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001451{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001452 Channel *c = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00001453 struct sshbuf *b = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001454 char *listen_address, *originator_address;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001455 u_short listen_port, originator_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00001456 int r;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001457
1458 /* Get rest of the packet */
1459 listen_address = packet_get_string(NULL);
1460 listen_port = packet_get_int();
1461 originator_address = packet_get_string(NULL);
1462 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001463 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001464
Damien Miller7acefbb2014-07-18 14:11:24 +10001465 debug("%s: listen %s port %d, originator %s port %d", __func__,
1466 listen_address, listen_port, originator_address, originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001467
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001468 c = channel_connect_by_listen_address(ssh, listen_address, listen_port,
Damien Millerbd740252008-05-19 15:37:09 +10001469 "forwarded-tcpip", originator_address);
1470
markus@openbsd.org8d057842016-09-30 09:19:13 +00001471 if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) {
1472 if ((b = sshbuf_new()) == NULL) {
1473 error("%s: alloc reply", __func__);
1474 goto out;
1475 }
1476 /* reconstruct and send to muxclient */
1477 if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
1478 (r = sshbuf_put_u8(b, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
1479 (r = sshbuf_put_cstring(b, request_type)) != 0 ||
1480 (r = sshbuf_put_u32(b, rchan)) != 0 ||
1481 (r = sshbuf_put_u32(b, rwindow)) != 0 ||
1482 (r = sshbuf_put_u32(b, rmaxpack)) != 0 ||
1483 (r = sshbuf_put_cstring(b, listen_address)) != 0 ||
1484 (r = sshbuf_put_u32(b, listen_port)) != 0 ||
1485 (r = sshbuf_put_cstring(b, originator_address)) != 0 ||
1486 (r = sshbuf_put_u32(b, originator_port)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001487 (r = sshbuf_put_stringb(c->output, b)) != 0) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00001488 error("%s: compose for muxclient %s", __func__,
1489 ssh_err(r));
1490 goto out;
1491 }
1492 }
1493
1494 out:
1495 sshbuf_free(b);
Darren Tuckera627d422013-06-02 07:31:17 +10001496 free(originator_address);
1497 free(listen_address);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001498 return c;
1499}
1500
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001501static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001502client_request_forwarded_streamlocal(struct ssh *ssh,
1503 const char *request_type, int rchan)
Damien Miller7acefbb2014-07-18 14:11:24 +10001504{
1505 Channel *c = NULL;
1506 char *listen_path;
1507
1508 /* Get the remote path. */
1509 listen_path = packet_get_string(NULL);
1510 /* XXX: Skip reserved field for now. */
1511 if (packet_get_string_ptr(NULL) == NULL)
1512 fatal("%s: packet_get_string_ptr failed", __func__);
1513 packet_check_eom();
1514
1515 debug("%s: %s", __func__, listen_path);
1516
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001517 c = channel_connect_by_listen_path(ssh, listen_path,
Damien Miller7acefbb2014-07-18 14:11:24 +10001518 "forwarded-streamlocal@openssh.com", "forwarded-streamlocal");
1519 free(listen_path);
1520 return c;
1521}
1522
1523static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001524client_request_x11(struct ssh *ssh, const char *request_type, int rchan)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001525{
1526 Channel *c = NULL;
1527 char *originator;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001528 u_short originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001529 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001530
1531 if (!options.forward_x11) {
1532 error("Warning: ssh server tried X11 forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001533 error("Warning: this is probably a break-in attempt by a "
1534 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001535 return NULL;
1536 }
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001537 if (x11_refuse_time != 0 && (u_int)monotime() >= x11_refuse_time) {
Damien Miller1ab6a512010-06-26 10:02:24 +10001538 verbose("Rejected X11 connection after ForwardX11Timeout "
1539 "expired");
1540 return NULL;
1541 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001542 originator = packet_get_string(NULL);
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001543 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001544 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001545 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001546 debug("client_request_x11: request from %s %d", originator,
1547 originator_port);
Darren Tuckera627d422013-06-02 07:31:17 +10001548 free(originator);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001549 sock = x11_connect_display(ssh);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001550 if (sock < 0)
1551 return NULL;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001552 c = channel_new(ssh, "x11",
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001553 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001554 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001555 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001556 return c;
1557}
1558
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001559static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001560client_request_agent(struct ssh *ssh, const char *request_type, int rchan)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001561{
1562 Channel *c = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001563 int r, sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001564
1565 if (!options.forward_agent) {
1566 error("Warning: ssh server tried agent forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001567 error("Warning: this is probably a break-in attempt by a "
1568 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001569 return NULL;
1570 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001571 if ((r = ssh_get_authentication_socket(&sock)) != 0) {
1572 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1573 debug("%s: ssh_get_authentication_socket: %s",
1574 __func__, ssh_err(r));
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001575 return NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001576 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001577 c = channel_new(ssh, "authentication agent connection",
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001578 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001579 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001580 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001581 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001582 return c;
1583}
1584
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001585char *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001586client_request_tun_fwd(struct ssh *ssh, int tun_mode,
1587 int local_tun, int remote_tun)
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001588{
1589 Channel *c;
1590 int fd;
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001591 char *ifname = NULL;
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001592
1593 if (tun_mode == SSH_TUNMODE_NO)
1594 return 0;
1595
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001596 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1597
1598 /* Open local tunnel device */
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001599 if ((fd = tun_open(local_tun, tun_mode, &ifname)) == -1) {
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001600 error("Tunnel device open failed.");
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001601 return NULL;
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001602 }
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001603 debug("Tunnel forwarding using interface %s", ifname);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001604
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001605 c = channel_new(ssh, "tun", SSH_CHANNEL_OPENING, fd, fd, -1,
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001606 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1607 c->datagram = 1;
1608
1609#if defined(SSH_TUN_FILTER)
1610 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
Damien Miller871f1e42017-09-12 18:01:35 +10001611 channel_register_filter(ssh, c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +10001612 sys_tun_outfilter, NULL, NULL);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001613#endif
1614
1615 packet_start(SSH2_MSG_CHANNEL_OPEN);
1616 packet_put_cstring("tun@openssh.com");
1617 packet_put_int(c->self);
1618 packet_put_int(c->local_window_max);
1619 packet_put_int(c->local_maxpacket);
1620 packet_put_int(tun_mode);
1621 packet_put_int(remote_tun);
1622 packet_send();
1623
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001624 return ifname;
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001625}
1626
Damien Millerbd483e72000-04-30 10:00:53 +10001627/* XXXX move to generic input handler */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001628static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001629client_input_channel_open(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerbd483e72000-04-30 10:00:53 +10001630{
1631 Channel *c = NULL;
1632 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001633 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001634 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001635
1636 ctype = packet_get_string(&len);
1637 rchan = packet_get_int();
1638 rwindow = packet_get_int();
1639 rmaxpack = packet_get_int();
1640
Damien Millere247cc42000-05-07 12:03:14 +10001641 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001642 ctype, rchan, rwindow, rmaxpack);
1643
Damien Miller0bc1bd82000-11-13 22:57:25 +11001644 if (strcmp(ctype, "forwarded-tcpip") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001645 c = client_request_forwarded_tcpip(ssh, ctype, rchan, rwindow,
markus@openbsd.org8d057842016-09-30 09:19:13 +00001646 rmaxpack);
Damien Miller7acefbb2014-07-18 14:11:24 +10001647 } else if (strcmp(ctype, "forwarded-streamlocal@openssh.com") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001648 c = client_request_forwarded_streamlocal(ssh, ctype, rchan);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001649 } else if (strcmp(ctype, "x11") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001650 c = client_request_x11(ssh, ctype, rchan);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001651 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001652 c = client_request_agent(ssh, ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001653 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00001654 if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) {
1655 debug3("proxied to downstream: %s", ctype);
1656 } else if (c != NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10001657 debug("confirm %s", ctype);
1658 c->remote_id = rchan;
djm@openbsd.org9f532292017-09-12 06:35:31 +00001659 c->have_remote_id = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001660 c->remote_window = rwindow;
1661 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001662 if (c->type != SSH_CHANNEL_CONNECTING) {
1663 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1664 packet_put_int(c->remote_id);
1665 packet_put_int(c->self);
1666 packet_put_int(c->local_window);
1667 packet_put_int(c->local_maxpacket);
1668 packet_send();
1669 }
Damien Millerbd483e72000-04-30 10:00:53 +10001670 } else {
1671 debug("failure %s", ctype);
1672 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1673 packet_put_int(rchan);
1674 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001675 packet_put_cstring("open failed");
1676 packet_put_cstring("");
Damien Millerbd483e72000-04-30 10:00:53 +10001677 packet_send();
1678 }
Darren Tuckera627d422013-06-02 07:31:17 +10001679 free(ctype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001680 return 0;
Damien Millerbd483e72000-04-30 10:00:53 +10001681}
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001682
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001683static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001684client_input_channel_req(int type, u_int32_t seq, struct ssh *ssh)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001685{
1686 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001687 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001688 char *rtype;
1689
1690 id = packet_get_int();
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001691 c = channel_lookup(ssh, id);
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001692 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00001693 return 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001694 rtype = packet_get_string(NULL);
1695 reply = packet_get_char();
1696
1697 debug("client_input_channel_req: channel %d rtype %s reply %d",
1698 id, rtype, reply);
1699
Damien Miller3bbd8782004-06-18 22:23:22 +10001700 if (id == -1) {
1701 error("client_input_channel_req: request for channel -1");
markus@openbsd.org8d057842016-09-30 09:19:13 +00001702 } else if (c == NULL) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001703 error("client_input_channel_req: channel %d: "
1704 "unknown channel", id);
Damien Millerbab9bd42008-05-19 16:06:47 +10001705 } else if (strcmp(rtype, "eow@openssh.com") == 0) {
1706 packet_check_eom();
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001707 chan_rcvd_eow(ssh, c);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001708 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10001709 exitval = packet_get_int();
Damien Millere1537f92010-01-26 13:26:22 +11001710 if (c->ctl_chan != -1) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001711 mux_exit_message(ssh, c, exitval);
Damien Miller0e220db2004-06-15 10:34:08 +10001712 success = 1;
Darren Tucker29440822010-01-08 17:08:35 +11001713 } else if (id == session_ident) {
1714 /* Record exit value of local session */
1715 success = 1;
1716 exit_status = exitval;
1717 } else {
Damien Miller36f57eb2010-01-30 17:28:34 +11001718 /* Probably for a mux channel that has already closed */
1719 debug("%s: no sink for exit-status on channel %d",
1720 __func__, id);
Damien Miller0e220db2004-06-15 10:34:08 +10001721 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001722 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001723 }
Damien Millerc5893782014-05-15 13:48:49 +10001724 if (reply && c != NULL && !(c->flags & CHAN_CLOSE_SENT)) {
djm@openbsd.org9f532292017-09-12 06:35:31 +00001725 if (!c->have_remote_id)
1726 fatal("%s: channel %d: no remote_id",
1727 __func__, c->self);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001728 packet_start(success ?
1729 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller8533c782008-12-08 09:54:40 +11001730 packet_put_int(c->remote_id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001731 packet_send();
1732 }
Darren Tuckera627d422013-06-02 07:31:17 +10001733 free(rtype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001734 return 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001735}
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001736
djm@openbsd.org523463a2015-02-16 22:13:32 +00001737struct hostkeys_update_ctx {
1738 /* The hostname and (optionally) IP address string for the server */
1739 char *host_str, *ip_str;
1740
1741 /*
1742 * Keys received from the server and a flag for each indicating
1743 * whether they already exist in known_hosts.
1744 * keys_seen is filled in by hostkeys_find() and later (for new
1745 * keys) by client_global_hostkeys_private_confirm().
1746 */
1747 struct sshkey **keys;
1748 int *keys_seen;
djm@openbsd.org05164352017-04-30 23:21:54 +00001749 size_t nkeys, nnew;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001750
1751 /*
1752 * Keys that are in known_hosts, but were not present in the update
1753 * from the server (i.e. scheduled to be deleted).
1754 * Filled in by hostkeys_find().
1755 */
1756 struct sshkey **old_keys;
1757 size_t nold;
1758};
1759
1760static void
1761hostkeys_update_ctx_free(struct hostkeys_update_ctx *ctx)
1762{
1763 size_t i;
1764
1765 if (ctx == NULL)
1766 return;
1767 for (i = 0; i < ctx->nkeys; i++)
1768 sshkey_free(ctx->keys[i]);
1769 free(ctx->keys);
1770 free(ctx->keys_seen);
1771 for (i = 0; i < ctx->nold; i++)
1772 sshkey_free(ctx->old_keys[i]);
1773 free(ctx->old_keys);
1774 free(ctx->host_str);
1775 free(ctx->ip_str);
1776 free(ctx);
1777}
1778
1779static int
1780hostkeys_find(struct hostkey_foreach_line *l, void *_ctx)
1781{
1782 struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
1783 size_t i;
1784 struct sshkey **tmp;
1785
djm@openbsd.org873d3e72017-04-30 23:18:44 +00001786 if (l->status != HKF_STATUS_MATCHED || l->key == NULL)
djm@openbsd.org523463a2015-02-16 22:13:32 +00001787 return 0;
1788
1789 /* Mark off keys we've already seen for this host */
1790 for (i = 0; i < ctx->nkeys; i++) {
1791 if (sshkey_equal(l->key, ctx->keys[i])) {
1792 debug3("%s: found %s key at %s:%ld", __func__,
1793 sshkey_ssh_name(ctx->keys[i]), l->path, l->linenum);
1794 ctx->keys_seen[i] = 1;
1795 return 0;
1796 }
1797 }
1798 /* This line contained a key that not offered by the server */
1799 debug3("%s: deprecated %s key at %s:%ld", __func__,
1800 sshkey_ssh_name(l->key), l->path, l->linenum);
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001801 if ((tmp = recallocarray(ctx->old_keys, ctx->nold, ctx->nold + 1,
djm@openbsd.org523463a2015-02-16 22:13:32 +00001802 sizeof(*ctx->old_keys))) == NULL)
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001803 fatal("%s: recallocarray failed nold = %zu",
djm@openbsd.org523463a2015-02-16 22:13:32 +00001804 __func__, ctx->nold);
1805 ctx->old_keys = tmp;
1806 ctx->old_keys[ctx->nold++] = l->key;
1807 l->key = NULL;
1808
1809 return 0;
1810}
1811
1812static void
1813update_known_hosts(struct hostkeys_update_ctx *ctx)
1814{
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00001815 int r, was_raw = 0;
1816 int loglevel = options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK ?
djm@openbsd.org523463a2015-02-16 22:13:32 +00001817 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_VERBOSE;
1818 char *fp, *response;
1819 size_t i;
1820
1821 for (i = 0; i < ctx->nkeys; i++) {
1822 if (ctx->keys_seen[i] != 2)
1823 continue;
1824 if ((fp = sshkey_fingerprint(ctx->keys[i],
1825 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1826 fatal("%s: sshkey_fingerprint failed", __func__);
1827 do_log2(loglevel, "Learned new hostkey: %s %s",
1828 sshkey_type(ctx->keys[i]), fp);
1829 free(fp);
1830 }
1831 for (i = 0; i < ctx->nold; i++) {
1832 if ((fp = sshkey_fingerprint(ctx->old_keys[i],
1833 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1834 fatal("%s: sshkey_fingerprint failed", __func__);
1835 do_log2(loglevel, "Deprecating obsolete hostkey: %s %s",
1836 sshkey_type(ctx->old_keys[i]), fp);
1837 free(fp);
1838 }
1839 if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK) {
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00001840 if (get_saved_tio() != NULL) {
1841 leave_raw_mode(1);
1842 was_raw = 1;
1843 }
djm@openbsd.org523463a2015-02-16 22:13:32 +00001844 response = NULL;
1845 for (i = 0; !quit_pending && i < 3; i++) {
1846 free(response);
1847 response = read_passphrase("Accept updated hostkeys? "
1848 "(yes/no): ", RP_ECHO);
1849 if (strcasecmp(response, "yes") == 0)
1850 break;
1851 else if (quit_pending || response == NULL ||
1852 strcasecmp(response, "no") == 0) {
1853 options.update_hostkeys = 0;
1854 break;
1855 } else {
1856 do_log2(loglevel, "Please enter "
1857 "\"yes\" or \"no\"");
1858 }
1859 }
1860 if (quit_pending || i >= 3 || response == NULL)
1861 options.update_hostkeys = 0;
1862 free(response);
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00001863 if (was_raw)
1864 enter_raw_mode(1);
djm@openbsd.org523463a2015-02-16 22:13:32 +00001865 }
1866
1867 /*
1868 * Now that all the keys are verified, we can go ahead and replace
1869 * them in known_hosts (assuming SSH_UPDATE_HOSTKEYS_ASK didn't
1870 * cancel the operation).
1871 */
1872 if (options.update_hostkeys != 0 &&
1873 (r = hostfile_replace_entries(options.user_hostfiles[0],
1874 ctx->host_str, ctx->ip_str, ctx->keys, ctx->nkeys,
1875 options.hash_known_hosts, 0,
1876 options.fingerprint_hash)) != 0)
1877 error("%s: hostfile_replace_entries failed: %s",
1878 __func__, ssh_err(r));
1879}
1880
1881static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001882client_global_hostkeys_private_confirm(struct ssh *ssh, int type,
1883 u_int32_t seq, void *_ctx)
djm@openbsd.org523463a2015-02-16 22:13:32 +00001884{
djm@openbsd.org523463a2015-02-16 22:13:32 +00001885 struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
1886 size_t i, ndone;
1887 struct sshbuf *signdata;
djm@openbsd.org78607312017-12-18 23:16:23 +00001888 int r, kexsigtype, use_kexsigtype;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001889 const u_char *sig;
1890 size_t siglen;
1891
1892 if (ctx->nnew == 0)
1893 fatal("%s: ctx->nnew == 0", __func__); /* sanity */
1894 if (type != SSH2_MSG_REQUEST_SUCCESS) {
1895 error("Server failed to confirm ownership of "
1896 "private host keys");
1897 hostkeys_update_ctx_free(ctx);
1898 return;
1899 }
djm@openbsd.org78607312017-12-18 23:16:23 +00001900 kexsigtype = sshkey_type_plain(
1901 sshkey_type_from_name(ssh->kex->hostkey_alg));
1902
djm@openbsd.org523463a2015-02-16 22:13:32 +00001903 if ((signdata = sshbuf_new()) == NULL)
1904 fatal("%s: sshbuf_new failed", __func__);
1905 /* Don't want to accidentally accept an unbound signature */
1906 if (ssh->kex->session_id_len == 0)
1907 fatal("%s: ssh->kex->session_id_len == 0", __func__);
1908 /*
1909 * Expect a signature for each of the ctx->nnew private keys we
1910 * haven't seen before. They will be in the same order as the
1911 * ctx->keys where the corresponding ctx->keys_seen[i] == 0.
1912 */
1913 for (ndone = i = 0; i < ctx->nkeys; i++) {
1914 if (ctx->keys_seen[i])
1915 continue;
1916 /* Prepare data to be signed: session ID, unique string, key */
1917 sshbuf_reset(signdata);
djm@openbsd.org44732de2015-02-20 22:17:21 +00001918 if ( (r = sshbuf_put_cstring(signdata,
1919 "hostkeys-prove-00@openssh.com")) != 0 ||
1920 (r = sshbuf_put_string(signdata, ssh->kex->session_id,
djm@openbsd.org523463a2015-02-16 22:13:32 +00001921 ssh->kex->session_id_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +00001922 (r = sshkey_puts(ctx->keys[i], signdata)) != 0)
1923 fatal("%s: failed to prepare signature: %s",
1924 __func__, ssh_err(r));
1925 /* Extract and verify signature */
1926 if ((r = sshpkt_get_string_direct(ssh, &sig, &siglen)) != 0) {
1927 error("%s: couldn't parse message: %s",
1928 __func__, ssh_err(r));
1929 goto out;
1930 }
djm@openbsd.org78607312017-12-18 23:16:23 +00001931 /*
1932 * For RSA keys, prefer to use the signature type negotiated
1933 * during KEX to the default (SHA1).
1934 */
1935 use_kexsigtype = kexsigtype == KEY_RSA &&
1936 sshkey_type_plain(ctx->keys[i]->type) == KEY_RSA;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001937 if ((r = sshkey_verify(ctx->keys[i], sig, siglen,
djm@openbsd.org04c7e282017-12-18 02:25:15 +00001938 sshbuf_ptr(signdata), sshbuf_len(signdata),
djm@openbsd.org78607312017-12-18 23:16:23 +00001939 use_kexsigtype ? ssh->kex->hostkey_alg : NULL, 0)) != 0) {
djm@openbsd.org523463a2015-02-16 22:13:32 +00001940 error("%s: server gave bad signature for %s key %zu",
1941 __func__, sshkey_type(ctx->keys[i]), i);
1942 goto out;
1943 }
1944 /* Key is good. Mark it as 'seen' */
1945 ctx->keys_seen[i] = 2;
1946 ndone++;
1947 }
1948 if (ndone != ctx->nnew)
1949 fatal("%s: ndone != ctx->nnew (%zu / %zu)", __func__,
1950 ndone, ctx->nnew); /* Shouldn't happen */
1951 ssh_packet_check_eom(ssh);
1952
1953 /* Make the edits to known_hosts */
1954 update_known_hosts(ctx);
1955 out:
1956 hostkeys_update_ctx_free(ctx);
1957}
1958
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001959/*
djm@openbsd.org894221a2017-03-10 05:01:13 +00001960 * Returns non-zero if the key is accepted by HostkeyAlgorithms.
1961 * Made slightly less trivial by the multiple RSA signature algorithm names.
1962 */
1963static int
1964key_accepted_by_hostkeyalgs(const struct sshkey *key)
1965{
1966 const char *ktype = sshkey_ssh_name(key);
1967 const char *hostkeyalgs = options.hostkeyalgorithms != NULL ?
1968 options.hostkeyalgorithms : KEX_DEFAULT_PK_ALG;
1969
1970 if (key == NULL || key->type == KEY_UNSPEC)
1971 return 0;
1972 if (key->type == KEY_RSA &&
1973 (match_pattern_list("rsa-sha2-256", hostkeyalgs, 0) == 1 ||
1974 match_pattern_list("rsa-sha2-512", hostkeyalgs, 0) == 1))
1975 return 1;
1976 return match_pattern_list(ktype, hostkeyalgs, 0) == 1;
1977}
1978
1979/*
djm@openbsd.org44732de2015-02-20 22:17:21 +00001980 * Handle hostkeys-00@openssh.com global request to inform the client of all
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001981 * the server's hostkeys. The keys are checked against the user's
1982 * HostkeyAlgorithms preference before they are accepted.
1983 */
1984static int
1985client_input_hostkeys(void)
1986{
djm@openbsd.org523463a2015-02-16 22:13:32 +00001987 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001988 const u_char *blob = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001989 size_t i, len = 0;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001990 struct sshbuf *buf = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001991 struct sshkey *key = NULL, **tmp;
1992 int r;
1993 char *fp;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001994 static int hostkeys_seen = 0; /* XXX use struct ssh */
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00001995 extern struct sockaddr_storage hostaddr; /* XXX from ssh.c */
djm@openbsd.orga63cfa22015-02-25 19:54:02 +00001996 struct hostkeys_update_ctx *ctx = NULL;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001997
1998 if (hostkeys_seen)
1999 fatal("%s: server already sent hostkeys", __func__);
djm@openbsd.org523463a2015-02-16 22:13:32 +00002000 if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK &&
2001 options.batch_mode)
2002 return 1; /* won't ask in batchmode, so don't even try */
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002003 if (!options.update_hostkeys || options.num_user_hostfiles <= 0)
2004 return 1;
djm@openbsd.orga63cfa22015-02-25 19:54:02 +00002005
2006 ctx = xcalloc(1, sizeof(*ctx));
djm@openbsd.org523463a2015-02-16 22:13:32 +00002007 while (ssh_packet_remaining(ssh) > 0) {
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002008 sshkey_free(key);
2009 key = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002010 if ((r = sshpkt_get_string_direct(ssh, &blob, &len)) != 0) {
2011 error("%s: couldn't parse message: %s",
2012 __func__, ssh_err(r));
2013 goto out;
2014 }
djm@openbsd.org44732de2015-02-20 22:17:21 +00002015 if ((r = sshkey_from_blob(blob, len, &key)) != 0) {
2016 error("%s: parse key: %s", __func__, ssh_err(r));
2017 goto out;
2018 }
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002019 fp = sshkey_fingerprint(key, options.fingerprint_hash,
2020 SSH_FP_DEFAULT);
2021 debug3("%s: received %s key %s", __func__,
2022 sshkey_type(key), fp);
2023 free(fp);
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00002024
djm@openbsd.org894221a2017-03-10 05:01:13 +00002025 if (!key_accepted_by_hostkeyalgs(key)) {
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002026 debug3("%s: %s key not permitted by HostkeyAlgorithms",
2027 __func__, sshkey_ssh_name(key));
2028 continue;
2029 }
djm@openbsd.org523463a2015-02-16 22:13:32 +00002030 /* Skip certs */
2031 if (sshkey_is_cert(key)) {
2032 debug3("%s: %s key is a certificate; skipping",
2033 __func__, sshkey_ssh_name(key));
2034 continue;
2035 }
2036 /* Ensure keys are unique */
2037 for (i = 0; i < ctx->nkeys; i++) {
2038 if (sshkey_equal(key, ctx->keys[i])) {
2039 error("%s: received duplicated %s host key",
2040 __func__, sshkey_ssh_name(key));
2041 goto out;
2042 }
2043 }
2044 /* Key is good, record it */
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00002045 if ((tmp = recallocarray(ctx->keys, ctx->nkeys, ctx->nkeys + 1,
djm@openbsd.org523463a2015-02-16 22:13:32 +00002046 sizeof(*ctx->keys))) == NULL)
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00002047 fatal("%s: recallocarray failed nkeys = %zu",
djm@openbsd.org523463a2015-02-16 22:13:32 +00002048 __func__, ctx->nkeys);
2049 ctx->keys = tmp;
2050 ctx->keys[ctx->nkeys++] = key;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002051 key = NULL;
2052 }
2053
djm@openbsd.org523463a2015-02-16 22:13:32 +00002054 if (ctx->nkeys == 0) {
djm@openbsd.org44732de2015-02-20 22:17:21 +00002055 debug("%s: server sent no hostkeys", __func__);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002056 goto out;
2057 }
djm@openbsd.org44732de2015-02-20 22:17:21 +00002058
djm@openbsd.org523463a2015-02-16 22:13:32 +00002059 if ((ctx->keys_seen = calloc(ctx->nkeys,
2060 sizeof(*ctx->keys_seen))) == NULL)
2061 fatal("%s: calloc failed", __func__);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002062
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00002063 get_hostfile_hostname_ipaddr(host,
2064 options.check_host_ip ? (struct sockaddr *)&hostaddr : NULL,
djm@openbsd.org523463a2015-02-16 22:13:32 +00002065 options.port, &ctx->host_str,
2066 options.check_host_ip ? &ctx->ip_str : NULL);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002067
djm@openbsd.org523463a2015-02-16 22:13:32 +00002068 /* Find which keys we already know about. */
2069 if ((r = hostkeys_foreach(options.user_hostfiles[0], hostkeys_find,
2070 ctx, ctx->host_str, ctx->ip_str,
2071 HKF_WANT_PARSE_KEY|HKF_WANT_MATCH)) != 0) {
2072 error("%s: hostkeys_foreach failed: %s", __func__, ssh_err(r));
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002073 goto out;
2074 }
2075
djm@openbsd.org523463a2015-02-16 22:13:32 +00002076 /* Figure out if we have any new keys to add */
2077 ctx->nnew = 0;
2078 for (i = 0; i < ctx->nkeys; i++) {
2079 if (!ctx->keys_seen[i])
2080 ctx->nnew++;
2081 }
2082
2083 debug3("%s: %zu keys from server: %zu new, %zu retained. %zu to remove",
2084 __func__, ctx->nkeys, ctx->nnew, ctx->nkeys - ctx->nnew, ctx->nold);
2085
2086 if (ctx->nnew == 0 && ctx->nold != 0) {
2087 /* We have some keys to remove. Just do it. */
2088 update_known_hosts(ctx);
2089 } else if (ctx->nnew != 0) {
2090 /*
2091 * We have received hitherto-unseen keys from the server.
2092 * Ask the server to confirm ownership of the private halves.
2093 */
2094 debug3("%s: asking server to prove ownership for %zu keys",
2095 __func__, ctx->nnew);
2096 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
2097 (r = sshpkt_put_cstring(ssh,
djm@openbsd.org44732de2015-02-20 22:17:21 +00002098 "hostkeys-prove-00@openssh.com")) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +00002099 (r = sshpkt_put_u8(ssh, 1)) != 0) /* bool: want reply */
2100 fatal("%s: cannot prepare packet: %s",
2101 __func__, ssh_err(r));
2102 if ((buf = sshbuf_new()) == NULL)
2103 fatal("%s: sshbuf_new", __func__);
2104 for (i = 0; i < ctx->nkeys; i++) {
2105 if (ctx->keys_seen[i])
2106 continue;
2107 sshbuf_reset(buf);
2108 if ((r = sshkey_putb(ctx->keys[i], buf)) != 0)
2109 fatal("%s: sshkey_putb: %s",
2110 __func__, ssh_err(r));
2111 if ((r = sshpkt_put_stringb(ssh, buf)) != 0)
2112 fatal("%s: sshpkt_put_string: %s",
2113 __func__, ssh_err(r));
2114 }
2115 if ((r = sshpkt_send(ssh)) != 0)
2116 fatal("%s: sshpkt_send: %s", __func__, ssh_err(r));
2117 client_register_global_confirm(
2118 client_global_hostkeys_private_confirm, ctx);
2119 ctx = NULL; /* will be freed in callback */
2120 }
2121
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002122 /* Success */
2123 out:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002124 hostkeys_update_ctx_free(ctx);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002125 sshkey_free(key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002126 sshbuf_free(buf);
djm@openbsd.org523463a2015-02-16 22:13:32 +00002127 /*
2128 * NB. Return success for all cases. The server doesn't need to know
2129 * what the client does with its hosts file.
2130 */
2131 return 1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002132}
2133
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002134static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002135client_input_global_request(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerc3fa4072002-01-22 23:21:58 +11002136{
2137 char *rtype;
2138 int want_reply;
2139 int success = 0;
2140
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002141 rtype = packet_get_cstring(NULL);
Damien Millerc3fa4072002-01-22 23:21:58 +11002142 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11002143 debug("client_input_global_request: rtype %s want_reply %d",
2144 rtype, want_reply);
djm@openbsd.org44732de2015-02-20 22:17:21 +00002145 if (strcmp(rtype, "hostkeys-00@openssh.com") == 0)
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002146 success = client_input_hostkeys();
Damien Millerc3fa4072002-01-22 23:21:58 +11002147 if (want_reply) {
2148 packet_start(success ?
2149 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
2150 packet_send();
2151 packet_write_wait();
2152 }
Darren Tuckera627d422013-06-02 07:31:17 +10002153 free(rtype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002154 return 0;
Damien Millerc3fa4072002-01-22 23:21:58 +11002155}
Damien Millerbd483e72000-04-30 10:00:53 +10002156
Damien Miller0e220db2004-06-15 10:34:08 +10002157void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002158client_session2_setup(struct ssh *ssh, int id, int want_tty, int want_subsystem,
Damien Miller5771ed72008-05-19 15:35:33 +10002159 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env)
Damien Miller0e220db2004-06-15 10:34:08 +10002160{
2161 int len;
Darren Tucker5d78de62004-11-05 20:35:44 +11002162 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002163
2164 debug2("%s: id %d", __func__, id);
2165
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002166 if ((c = channel_lookup(ssh, id)) == NULL)
2167 fatal("%s: channel %d: unknown channel", __func__, id);
Darren Tucker5d78de62004-11-05 20:35:44 +11002168
Damien Miller0dac6fb2010-11-20 15:19:38 +11002169 packet_set_interactive(want_tty,
2170 options.ip_qos_interactive, options.ip_qos_bulk);
2171
Damien Miller0e220db2004-06-15 10:34:08 +10002172 if (want_tty) {
2173 struct winsize ws;
Damien Miller0e220db2004-06-15 10:34:08 +10002174
2175 /* Store window size in the packet. */
2176 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
2177 memset(&ws, 0, sizeof(ws));
2178
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002179 channel_request_start(ssh, id, "pty-req", 1);
2180 client_expect_confirm(ssh, id, "PTY allocation", CONFIRM_TTY);
Damien Miller0e220db2004-06-15 10:34:08 +10002181 packet_put_cstring(term != NULL ? term : "");
Damien Miller71a73672006-03-26 14:04:36 +11002182 packet_put_int((u_int)ws.ws_col);
2183 packet_put_int((u_int)ws.ws_row);
2184 packet_put_int((u_int)ws.ws_xpixel);
2185 packet_put_int((u_int)ws.ws_ypixel);
Darren Tuckerdf189fb2008-06-08 12:55:32 +10002186 if (tiop == NULL)
2187 tiop = get_saved_tio();
2188 tty_make_modes(-1, tiop);
Damien Miller0e220db2004-06-15 10:34:08 +10002189 packet_send();
2190 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11002191 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10002192 }
2193
2194 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10002195 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10002196 int i, j, matched;
Damien Miller0e220db2004-06-15 10:34:08 +10002197 char *name, *val;
2198
2199 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10002200 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10002201 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10002202 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10002203 if ((val = strchr(name, '=')) == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +10002204 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002205 continue;
2206 }
2207 *val++ = '\0';
2208
2209 matched = 0;
2210 for (j = 0; j < options.num_send_env; j++) {
2211 if (match_pattern(name, options.send_env[j])) {
2212 matched = 1;
2213 break;
2214 }
2215 }
2216 if (!matched) {
2217 debug3("Ignored env %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10002218 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002219 continue;
2220 }
2221
2222 debug("Sending env %s = %s", name, val);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002223 channel_request_start(ssh, id, "env", 0);
Damien Miller0e220db2004-06-15 10:34:08 +10002224 packet_put_cstring(name);
2225 packet_put_cstring(val);
2226 packet_send();
Darren Tuckera627d422013-06-02 07:31:17 +10002227 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002228 }
2229 }
2230
2231 len = buffer_len(cmd);
2232 if (len > 0) {
2233 if (len > 900)
2234 len = 900;
2235 if (want_subsystem) {
Damien Miller5771ed72008-05-19 15:35:33 +10002236 debug("Sending subsystem: %.*s",
2237 len, (u_char*)buffer_ptr(cmd));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002238 channel_request_start(ssh, id, "subsystem", 1);
2239 client_expect_confirm(ssh, id, "subsystem",
2240 CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002241 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002242 debug("Sending command: %.*s",
2243 len, (u_char*)buffer_ptr(cmd));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002244 channel_request_start(ssh, id, "exec", 1);
2245 client_expect_confirm(ssh, id, "exec", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002246 }
2247 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
2248 packet_send();
2249 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002250 channel_request_start(ssh, id, "shell", 1);
2251 client_expect_confirm(ssh, id, "shell", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002252 packet_send();
2253 }
2254}
2255
Ben Lindstrombba81212001-06-25 05:01:22 +00002256static void
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002257client_init_dispatch(void)
Damien Miller1383bd82000-04-06 12:32:37 +10002258{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002259 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10002260
Damien Miller1383bd82000-04-06 12:32:37 +10002261 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2262 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2263 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2264 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10002265 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10002266 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2267 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002268 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10002269 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerb84886b2008-05-19 15:05:07 +10002270 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2271 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
Damien Millerc3fa4072002-01-22 23:21:58 +11002272 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002273
2274 /* rekeying */
2275 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10002276
2277 /* global request reply messages */
2278 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2279 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10002280}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002281
Damien Miller6c3eec72011-05-05 14:16:22 +10002282void
2283client_stop_mux(void)
2284{
2285 if (options.control_path != NULL && muxserver_sock != -1)
2286 unlink(options.control_path);
2287 /*
Darren Tucker48bf4b02012-09-07 16:38:53 +10002288 * If we are in persist mode, or don't have a shell, signal that we
2289 * should close when all active channels are closed.
Damien Miller6c3eec72011-05-05 14:16:22 +10002290 */
Darren Tucker48bf4b02012-09-07 16:38:53 +10002291 if (options.control_persist || no_shell_flag) {
Damien Miller6c3eec72011-05-05 14:16:22 +10002292 session_closed = 1;
Damien Miller4ac99c32011-06-20 14:43:31 +10002293 setproctitle("[stopped mux]");
2294 }
Damien Miller6c3eec72011-05-05 14:16:22 +10002295}
2296
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002297/* client specific fatal cleanup */
2298void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002299cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002300{
Damien Miller21771e22011-05-15 08:45:50 +10002301 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002302 leave_non_blocking();
Damien Millerb1cbfa22008-05-19 16:00:08 +10002303 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10002304 unlink(options.control_path);
Damien Millera41ccca2010-10-07 22:07:32 +11002305 ssh_kill_proxy_command();
Darren Tucker3e33cec2003-10-02 16:12:36 +10002306 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002307}