blob: ed9bebdfaa8c595666276e575e0d88ccfeda4c22 [file] [log] [blame]
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001/* $OpenBSD: clientloop.c,v 1.310 2018/01/23 05:27:21 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;
213 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214}
215
Damien Miller5428f641999-11-25 11:54:57 +1100216/*
217 * Signal handler for signals that cause the program to terminate. These
218 * signals must be trapped to restore terminal modes.
219 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100220/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000221static void
Damien Miller95def091999-11-25 00:26:21 +1100222signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000224 received_signal = sig;
225 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000226}
227
Damien Miller5428f641999-11-25 11:54:57 +1100228/*
Damien Millere11e1ea2010-08-03 16:04:46 +1000229 * Sets control_persist_exit_time to the absolute time when the
230 * backgrounded control master should exit due to expiry of the
231 * ControlPersist timeout. Sets it to 0 if we are not a backgrounded
232 * control master process, or if there is no ControlPersist timeout.
233 */
234static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000235set_control_persist_exit_time(struct ssh *ssh)
Damien Millere11e1ea2010-08-03 16:04:46 +1000236{
237 if (muxserver_sock == -1 || !options.control_persist
Damien Miller6c3eec72011-05-05 14:16:22 +1000238 || options.control_persist_timeout == 0) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000239 /* not using a ControlPersist timeout */
240 control_persist_exit_time = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000241 } else if (channel_still_open(ssh)) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000242 /* some client connections are still open */
243 if (control_persist_exit_time > 0)
244 debug2("%s: cancel scheduled exit", __func__);
245 control_persist_exit_time = 0;
246 } else if (control_persist_exit_time <= 0) {
247 /* a client connection has recently closed */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000248 control_persist_exit_time = monotime() +
Damien Millere11e1ea2010-08-03 16:04:46 +1000249 (time_t)options.control_persist_timeout;
250 debug2("%s: schedule exit in %d seconds", __func__,
251 options.control_persist_timeout);
252 }
253 /* else we are already counting down to the timeout */
254}
255
Damien Miller8d60be52012-02-11 08:18:17 +1100256#define SSH_X11_VALID_DISPLAY_CHARS ":/.-_"
257static int
258client_x11_display_valid(const char *display)
259{
260 size_t i, dlen;
261
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000262 if (display == NULL)
263 return 0;
264
Damien Miller8d60be52012-02-11 08:18:17 +1100265 dlen = strlen(display);
266 for (i = 0; i < dlen; i++) {
Damien Millerfdb23062013-11-21 13:57:15 +1100267 if (!isalnum((u_char)display[i]) &&
Damien Miller8d60be52012-02-11 08:18:17 +1100268 strchr(SSH_X11_VALID_DISPLAY_CHARS, display[i]) == NULL) {
269 debug("Invalid character '%c' in DISPLAY", display[i]);
270 return 0;
271 }
272 }
273 return 1;
274}
275
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000276#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
277#define X11_TIMEOUT_SLACK 60
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000278int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000279client_x11_get_proto(struct ssh *ssh, const char *display,
280 const char *xauth_path, u_int trusted, u_int timeout,
281 char **_proto, char **_data)
Damien Miller17e7ed02005-06-17 12:54:33 +1000282{
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000283 char cmd[1024], line[512], xdisplay[512];
284 char xauthfile[PATH_MAX], xauthdir[PATH_MAX];
Damien Miller17e7ed02005-06-17 12:54:33 +1000285 static char proto[512], data[512];
286 FILE *f;
tedu@openbsd.org10363562016-09-17 18:00:27 +0000287 int got_data = 0, generated = 0, do_unlink = 0, r;
Damien Miller17e7ed02005-06-17 12:54:33 +1000288 struct stat st;
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000289 u_int now, x11_timeout_real;
Damien Miller17e7ed02005-06-17 12:54:33 +1000290
Damien Miller17e7ed02005-06-17 12:54:33 +1000291 *_proto = proto;
292 *_data = data;
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000293 proto[0] = data[0] = xauthfile[0] = xauthdir[0] = '\0';
Damien Miller17e7ed02005-06-17 12:54:33 +1000294
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000295 if (!client_x11_display_valid(display)) {
millert@openbsd.org5658ef22016-02-01 21:18:17 +0000296 if (display != NULL)
297 logit("DISPLAY \"%s\" invalid; disabling X11 forwarding",
298 display);
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000299 return -1;
300 }
301 if (xauth_path != NULL && stat(xauth_path, &st) == -1) {
302 debug("No xauth program.");
303 xauth_path = NULL;
304 }
305
306 if (xauth_path != NULL) {
Damien Miller17e7ed02005-06-17 12:54:33 +1000307 /*
308 * Handle FamilyLocal case where $DISPLAY does
309 * not match an authorization entry. For this we
310 * just try "xauth list unix:displaynum.screennum".
311 * XXX: "localhost" match to determine FamilyLocal
312 * is not perfect.
313 */
314 if (strncmp(display, "localhost:", 10) == 0) {
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000315 if ((r = snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
316 display + 10)) < 0 ||
317 (size_t)r >= sizeof(xdisplay)) {
318 error("%s: display name too long", __func__);
319 return -1;
320 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000321 display = xdisplay;
322 }
323 if (trusted == 0) {
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000324 /*
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000325 * Generate an untrusted X11 auth cookie.
326 *
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000327 * The authentication cookie should briefly outlive
328 * ssh's willingness to forward X11 connections to
329 * avoid nasty fail-open behaviour in the X server.
330 */
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000331 mktemp_proto(xauthdir, sizeof(xauthdir));
332 if (mkdtemp(xauthdir) == NULL) {
333 error("%s: mkdtemp: %s",
334 __func__, strerror(errno));
335 return -1;
336 }
337 do_unlink = 1;
338 if ((r = snprintf(xauthfile, sizeof(xauthfile),
339 "%s/xauthfile", xauthdir)) < 0 ||
340 (size_t)r >= sizeof(xauthfile)) {
341 error("%s: xauthfile path too long", __func__);
342 unlink(xauthfile);
343 rmdir(xauthdir);
344 return -1;
345 }
346
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000347 if (timeout >= UINT_MAX - X11_TIMEOUT_SLACK)
348 x11_timeout_real = UINT_MAX;
349 else
350 x11_timeout_real = timeout + X11_TIMEOUT_SLACK;
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000351 if ((r = snprintf(cmd, sizeof(cmd),
352 "%s -f %s generate %s " SSH_X11_PROTO
353 " untrusted timeout %u 2>" _PATH_DEVNULL,
354 xauth_path, xauthfile, display,
355 x11_timeout_real)) < 0 ||
356 (size_t)r >= sizeof(cmd))
357 fatal("%s: cmd too long", __func__);
358 debug2("%s: %s", __func__, cmd);
359 if (x11_refuse_time == 0) {
360 now = monotime() + 1;
361 if (UINT_MAX - timeout < now)
362 x11_refuse_time = UINT_MAX;
363 else
364 x11_refuse_time = now + timeout;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000365 channel_set_x11_refuse_time(ssh,
366 x11_refuse_time);
Damien Miller17e7ed02005-06-17 12:54:33 +1000367 }
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000368 if (system(cmd) == 0)
369 generated = 1;
Damien Miller17e7ed02005-06-17 12:54:33 +1000370 }
Darren Tucker513d13a2007-08-15 19:13:41 +1000371
372 /*
373 * When in untrusted mode, we read the cookie only if it was
374 * successfully generated as an untrusted one in the step
375 * above.
376 */
377 if (trusted || generated) {
378 snprintf(cmd, sizeof(cmd),
379 "%s %s%s list %s 2>" _PATH_DEVNULL,
380 xauth_path,
381 generated ? "-f " : "" ,
382 generated ? xauthfile : "",
383 display);
384 debug2("x11_get_proto: %s", cmd);
385 f = popen(cmd, "r");
386 if (f && fgets(line, sizeof(line), f) &&
387 sscanf(line, "%*s %511s %511s", proto, data) == 2)
388 got_data = 1;
389 if (f)
390 pclose(f);
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000391 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000392 }
393
394 if (do_unlink) {
395 unlink(xauthfile);
396 rmdir(xauthdir);
397 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000398
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000399 /* Don't fall back to fake X11 data for untrusted forwarding */
400 if (!trusted && !got_data) {
401 error("Warning: untrusted X11 forwarding setup failed: "
402 "xauth key data not generated");
403 return -1;
404 }
405
Damien Miller17e7ed02005-06-17 12:54:33 +1000406 /*
407 * If we didn't get authentication data, just make up some
408 * data. The forwarding code will check the validity of the
409 * response anyway, and substitute this data. The X11
410 * server, however, will ignore this fake data and use
411 * whatever authentication mechanisms it was using otherwise
412 * for the local connection.
413 */
414 if (!got_data) {
tedu@openbsd.org10363562016-09-17 18:00:27 +0000415 u_int8_t rnd[16];
416 u_int i;
Damien Miller17e7ed02005-06-17 12:54:33 +1000417
418 logit("Warning: No xauth data; "
419 "using fake authentication data for X11 forwarding.");
420 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
tedu@openbsd.org10363562016-09-17 18:00:27 +0000421 arc4random_buf(rnd, sizeof(rnd));
422 for (i = 0; i < sizeof(rnd); i++) {
Damien Miller17e7ed02005-06-17 12:54:33 +1000423 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
tedu@openbsd.org10363562016-09-17 18:00:27 +0000424 rnd[i]);
Damien Miller17e7ed02005-06-17 12:54:33 +1000425 }
426 }
djm@openbsd.orged4ce822016-01-13 23:04:47 +0000427
428 return 0;
Damien Miller17e7ed02005-06-17 12:54:33 +1000429}
430
Damien Miller5428f641999-11-25 11:54:57 +1100431/*
Damien Miller5428f641999-11-25 11:54:57 +1100432 * Checks if the client window has changed, and sends a packet about it to
433 * the server if so. The actual change is detected elsewhere (by a software
434 * interrupt on Unix); this just checks the flag and sends a message if
435 * appropriate.
436 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000437
Ben Lindstrombba81212001-06-25 05:01:22 +0000438static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000439client_check_window_change(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000440{
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000441 if (!received_window_change_signal)
Damien Miller1383bd82000-04-06 12:32:37 +1000442 return;
443 /** XXX race */
444 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000446 debug2("%s: changed", __func__);
Damien Miller1383bd82000-04-06 12:32:37 +1000447
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000448 channel_send_window_changes(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000449}
450
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000451static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000452client_global_request_reply(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller509b0102003-12-17 16:33:10 +1100453{
Darren Tucker9f407c42008-06-13 04:50:27 +1000454 struct global_confirm *gc;
455
456 if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000457 return 0;
Darren Tucker9f407c42008-06-13 04:50:27 +1000458 if (gc->cb != NULL)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000459 gc->cb(ssh, type, seq, gc->ctx);
Darren Tucker9f407c42008-06-13 04:50:27 +1000460 if (--gc->ref_count <= 0) {
461 TAILQ_REMOVE(&global_confirms, gc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +1100462 explicit_bzero(gc, sizeof(*gc));
Darren Tuckera627d422013-06-02 07:31:17 +1000463 free(gc);
Darren Tucker9f407c42008-06-13 04:50:27 +1000464 }
465
Darren Tuckerf7288d72009-06-21 18:12:20 +1000466 packet_set_alive_timeouts(0);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000467 return 0;
Damien Miller509b0102003-12-17 16:33:10 +1100468}
469
470static void
471server_alive_check(void)
472{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000473 if (packet_inc_alive_timeouts() > options.server_alive_count_max) {
Damien Millerb73b6fd2011-01-11 17:18:56 +1100474 logit("Timeout, server %s not responding.", host);
Damien Miller985a4482006-10-24 03:02:41 +1000475 cleanup_exit(255);
476 }
Damien Miller509b0102003-12-17 16:33:10 +1100477 packet_start(SSH2_MSG_GLOBAL_REQUEST);
478 packet_put_cstring("keepalive@openssh.com");
479 packet_put_char(1); /* boolean: want reply */
480 packet_send();
Darren Tucker9f407c42008-06-13 04:50:27 +1000481 /* Insert an empty placeholder to maintain ordering */
482 client_register_global_confirm(NULL, NULL);
Damien Miller509b0102003-12-17 16:33:10 +1100483}
484
Damien Miller5428f641999-11-25 11:54:57 +1100485/*
486 * Waits until the client can do something (some data becomes available on
487 * one of the file descriptors).
488 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000489static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000490client_wait_until_can_do_something(struct ssh *ssh,
491 fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000492 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000493{
Damien Miller509b0102003-12-17 16:33:10 +1100494 struct timeval tv, *tvp;
Damien Millere11e1ea2010-08-03 16:04:46 +1000495 int timeout_secs;
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000496 time_t minwait_secs = 0, server_alive_time = 0, now = monotime();
Damien Miller509b0102003-12-17 16:33:10 +1100497 int ret;
498
Damien Miller5e953212001-01-30 09:14:00 +1100499 /* Add any selections by the channel mechanism. */
djm@openbsd.org71e5a532017-08-30 03:59:08 +0000500 channel_prepare_select(active_state, readsetp, writesetp, maxfdp,
501 nallocp, &minwait_secs);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000502
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000503 /* channel_prepare_select could have closed the last channel */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000504 if (session_closed && !channel_still_open(ssh) &&
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000505 !packet_have_data_to_write()) {
506 /* clear mask since we did not call select() */
507 memset(*readsetp, 0, *nallocp);
508 memset(*writesetp, 0, *nallocp);
509 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000510 }
511
djm@openbsd.org05164352017-04-30 23:21:54 +0000512 FD_SET(connection_in, *readsetp);
513
Damien Miller95def091999-11-25 00:26:21 +1100514 /* Select server connection if have data to write to the server. */
515 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100516 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100517
Damien Miller5428f641999-11-25 11:54:57 +1100518 /*
519 * Wait for something to happen. This will suspend the process until
520 * some selected descriptor can be read, written, or has some other
Damien Millere11e1ea2010-08-03 16:04:46 +1000521 * event pending, or a timeout expires.
Damien Miller5428f641999-11-25 11:54:57 +1100522 */
Damien Miller95def091999-11-25 00:26:21 +1100523
Damien Millere11e1ea2010-08-03 16:04:46 +1000524 timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000525 if (options.server_alive_interval > 0) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000526 timeout_secs = options.server_alive_interval;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000527 server_alive_time = now + options.server_alive_interval;
528 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000529 if (options.rekey_interval > 0 && !rekeying)
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000530 timeout_secs = MINIMUM(timeout_secs, packet_get_rekey_timeout());
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000531 set_control_persist_exit_time(ssh);
Damien Millere11e1ea2010-08-03 16:04:46 +1000532 if (control_persist_exit_time > 0) {
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000533 timeout_secs = MINIMUM(timeout_secs,
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000534 control_persist_exit_time - now);
Damien Millere11e1ea2010-08-03 16:04:46 +1000535 if (timeout_secs < 0)
536 timeout_secs = 0;
537 }
Damien Millera6508752012-04-22 11:21:10 +1000538 if (minwait_secs != 0)
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000539 timeout_secs = MINIMUM(timeout_secs, (int)minwait_secs);
Damien Millere11e1ea2010-08-03 16:04:46 +1000540 if (timeout_secs == INT_MAX)
Damien Miller509b0102003-12-17 16:33:10 +1100541 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000542 else {
Damien Millere11e1ea2010-08-03 16:04:46 +1000543 tv.tv_sec = timeout_secs;
Damien Miller509b0102003-12-17 16:33:10 +1100544 tv.tv_usec = 0;
545 tvp = &tv;
546 }
Damien Millere11e1ea2010-08-03 16:04:46 +1000547
Damien Miller509b0102003-12-17 16:33:10 +1100548 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
549 if (ret < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100550 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000551
552 /*
553 * We have to clear the select masks, because we return.
554 * We have to return, because the mainloop checks for the flags
555 * set by the signal handlers.
556 */
Damien Miller79faeff2001-11-12 11:06:32 +1100557 memset(*readsetp, 0, *nallocp);
558 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000559
Damien Miller95def091999-11-25 00:26:21 +1100560 if (errno == EINTR)
561 return;
562 /* Note: we might still have data in the buffers. */
563 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
564 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100565 quit_pending = 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000566 } else if (ret == 0) {
567 /*
568 * Timeout. Could have been either keepalive or rekeying.
569 * Keepalive we check here, rekeying is checked in clientloop.
570 */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000571 if (server_alive_time != 0 && server_alive_time <= monotime())
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000572 server_alive_check();
573 }
574
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000575}
576
Ben Lindstrombba81212001-06-25 05:01:22 +0000577static void
Damien Millerad833b32000-08-23 10:46:23 +1000578client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000579{
Damien Miller95def091999-11-25 00:26:21 +1100580 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000581 if (buffer_len(bout) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000582 atomicio(vwrite, fileno(stdout), buffer_ptr(bout),
583 buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000584 if (buffer_len(berr) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000585 atomicio(vwrite, fileno(stderr), buffer_ptr(berr),
586 buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000587
Damien Miller21771e22011-05-15 08:45:50 +1000588 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000589
djm@openbsd.org5b8da1f2017-09-19 04:24:22 +0000590 sshbuf_reset(bin);
591 sshbuf_reset(bout);
592 sshbuf_reset(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000593
Damien Miller95def091999-11-25 00:26:21 +1100594 /* Send the suspend signal to the program itself. */
595 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000596
Darren Tucker5d78de62004-11-05 20:35:44 +1100597 /* Reset window sizes in case they have changed */
598 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000599
Damien Miller21771e22011-05-15 08:45:50 +1000600 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000601}
602
Ben Lindstrombba81212001-06-25 05:01:22 +0000603static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100604client_process_net_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000605{
markus@openbsd.orga3068632016-01-14 16:17:39 +0000606 int len;
Darren Tucker86e30a02009-08-28 11:21:06 +1000607 char buf[SSH_IOBUFSZ];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000608
Damien Miller5428f641999-11-25 11:54:57 +1100609 /*
610 * Read input from the server, and add any such data to the buffer of
611 * the packet subsystem.
612 */
Damien Miller95def091999-11-25 00:26:21 +1100613 if (FD_ISSET(connection_in, readset)) {
614 /* Read as much as possible. */
markus@openbsd.orga3068632016-01-14 16:17:39 +0000615 len = read(connection_in, buf, sizeof(buf));
616 if (len == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000617 /*
618 * Received EOF. The remote host has closed the
619 * connection.
620 */
621 snprintf(buf, sizeof buf,
622 "Connection to %.300s closed by remote host.\r\n",
623 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000624 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000625 quit_pending = 1;
626 return;
Damien Miller95def091999-11-25 00:26:21 +1100627 }
Damien Miller5428f641999-11-25 11:54:57 +1100628 /*
629 * There is a kernel bug on Solaris that causes select to
630 * sometimes wake up even though there is no data available.
631 */
Damien Millerd8968ad2008-07-04 23:10:49 +1000632 if (len < 0 &&
633 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Damien Miller95def091999-11-25 00:26:21 +1100634 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000635
Damien Miller95def091999-11-25 00:26:21 +1100636 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000637 /*
638 * An error has encountered. Perhaps there is a
639 * network problem.
640 */
641 snprintf(buf, sizeof buf,
642 "Read from remote host %.300s: %.100s\r\n",
643 host, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100644 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100645 quit_pending = 1;
646 return;
647 }
648 packet_process_incoming(buf, len);
649 }
Damien Miller1383bd82000-04-06 12:32:37 +1000650}
651
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000652static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000653client_status_confirm(struct ssh *ssh, int type, Channel *c, void *ctx)
Damien Miller0e220db2004-06-15 10:34:08 +1000654{
Damien Miller5771ed72008-05-19 15:35:33 +1000655 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
656 char errmsg[256];
657 int tochan;
Darren Tuckerfc959702004-07-17 16:12:08 +1000658
Damien Miller555f3b82011-05-15 08:48:05 +1000659 /*
660 * If a TTY was explicitly requested, then a failure to allocate
661 * one is fatal.
662 */
663 if (cr->action == CONFIRM_TTY &&
664 (options.request_tty == REQUEST_TTY_FORCE ||
665 options.request_tty == REQUEST_TTY_YES))
666 cr->action = CONFIRM_CLOSE;
667
Damien Miller5771ed72008-05-19 15:35:33 +1000668 /* XXX supress on mux _client_ quietmode */
669 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
Damien Millere1537f92010-01-26 13:26:22 +1100670 c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
Damien Miller0e220db2004-06-15 10:34:08 +1000671
Damien Miller5771ed72008-05-19 15:35:33 +1000672 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
673 debug2("%s request accepted on channel %d",
674 cr->request_type, c->self);
675 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
676 if (tochan) {
677 snprintf(errmsg, sizeof(errmsg),
678 "%s request failed\r\n", cr->request_type);
679 } else {
680 snprintf(errmsg, sizeof(errmsg),
681 "%s request failed on channel %d",
682 cr->request_type, c->self);
683 }
684 /* If error occurred on primary session channel, then exit */
Damien Miller555f3b82011-05-15 08:48:05 +1000685 if (cr->action == CONFIRM_CLOSE && c->self == session_ident)
Damien Miller5771ed72008-05-19 15:35:33 +1000686 fatal("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000687 /*
688 * If error occurred on mux client, append to
689 * their stderr.
690 */
691 if (tochan) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000692 buffer_append(c->extended, errmsg, strlen(errmsg));
Damien Miller555f3b82011-05-15 08:48:05 +1000693 } else
Damien Miller5771ed72008-05-19 15:35:33 +1000694 error("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000695 if (cr->action == CONFIRM_TTY) {
696 /*
697 * If a TTY allocation error occurred, then arrange
698 * for the correct TTY to leave raw mode.
699 */
700 if (c->self == session_ident)
701 leave_raw_mode(0);
702 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000703 mux_tty_alloc_failed(ssh, c);
Damien Miller555f3b82011-05-15 08:48:05 +1000704 } else if (cr->action == CONFIRM_CLOSE) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000705 chan_read_failed(ssh, c);
706 chan_write_failed(ssh, c);
Damien Miller5771ed72008-05-19 15:35:33 +1000707 }
Damien Miller0e220db2004-06-15 10:34:08 +1000708 }
Darren Tuckera627d422013-06-02 07:31:17 +1000709 free(cr);
Damien Miller5771ed72008-05-19 15:35:33 +1000710}
Damien Miller0e220db2004-06-15 10:34:08 +1000711
Damien Miller5771ed72008-05-19 15:35:33 +1000712static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000713client_abandon_status_confirm(struct ssh *ssh, Channel *c, void *ctx)
Damien Miller5771ed72008-05-19 15:35:33 +1000714{
Darren Tuckera627d422013-06-02 07:31:17 +1000715 free(ctx);
Damien Miller5771ed72008-05-19 15:35:33 +1000716}
717
Damien Miller6d7b4372011-06-23 08:31:57 +1000718void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000719client_expect_confirm(struct ssh *ssh, int id, const char *request,
Damien Miller555f3b82011-05-15 08:48:05 +1000720 enum confirm_action action)
Damien Miller5771ed72008-05-19 15:35:33 +1000721{
Damien Miller6c81fee2013-11-08 12:19:55 +1100722 struct channel_reply_ctx *cr = xcalloc(1, sizeof(*cr));
Damien Miller5771ed72008-05-19 15:35:33 +1000723
724 cr->request_type = request;
Damien Miller555f3b82011-05-15 08:48:05 +1000725 cr->action = action;
Damien Miller5771ed72008-05-19 15:35:33 +1000726
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000727 channel_register_status_confirm(ssh, id, client_status_confirm,
Damien Miller5771ed72008-05-19 15:35:33 +1000728 client_abandon_status_confirm, cr);
Damien Miller0e220db2004-06-15 10:34:08 +1000729}
730
Darren Tucker9f407c42008-06-13 04:50:27 +1000731void
732client_register_global_confirm(global_confirm_cb *cb, void *ctx)
733{
Damien Millerb9d3bee2008-07-16 22:40:52 +1000734 struct global_confirm *gc, *last_gc;
Darren Tucker9f407c42008-06-13 04:50:27 +1000735
736 /* Coalesce identical callbacks */
Damien Millerb9d3bee2008-07-16 22:40:52 +1000737 last_gc = TAILQ_LAST(&global_confirms, global_confirms);
738 if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
739 if (++last_gc->ref_count >= INT_MAX)
740 fatal("%s: last_gc->ref_count = %d",
741 __func__, last_gc->ref_count);
Darren Tucker9f407c42008-06-13 04:50:27 +1000742 return;
743 }
744
Damien Miller6c81fee2013-11-08 12:19:55 +1100745 gc = xcalloc(1, sizeof(*gc));
Darren Tucker9f407c42008-06-13 04:50:27 +1000746 gc->cb = cb;
747 gc->ctx = ctx;
748 gc->ref_count = 1;
749 TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
750}
751
Damien Miller0e220db2004-06-15 10:34:08 +1000752static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000753process_cmdline(struct ssh *ssh)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000754{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000755 void (*handler)(int);
Damien Miller7acefbb2014-07-18 14:11:24 +1000756 char *s, *cmd;
757 int ok, delete = 0, local = 0, remote = 0, dynamic = 0;
758 struct Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000759
Damien Miller1d2c4562014-02-04 11:18:20 +1100760 memset(&fwd, 0, sizeof(fwd));
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100761
Damien Miller21771e22011-05-15 08:45:50 +1000762 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000763 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000764 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000765 if (s == NULL)
766 goto out;
Damien Millerfdb23062013-11-21 13:57:15 +1100767 while (isspace((u_char)*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000768 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000769 if (*s == '-')
770 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000771 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000772 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000773
Darren Tucker1973c882004-05-24 10:34:36 +1000774 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000775 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000776 logit(" -L[bind_address:]port:host:hostport "
777 "Request local forward");
778 logit(" -R[bind_address:]port:host:hostport "
779 "Request remote forward");
Damien Miller0164cb82008-11-05 16:30:31 +1100780 logit(" -D[bind_address:]port "
781 "Request dynamic forward");
Damien Millerff773642011-09-22 21:39:48 +1000782 logit(" -KL[bind_address:]port "
783 "Cancel local forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000784 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000785 "Cancel remote forward");
Damien Millerff773642011-09-22 21:39:48 +1000786 logit(" -KD[bind_address:]port "
787 "Cancel dynamic forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100788 if (!options.permit_local_command)
789 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000790 logit(" !args "
791 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100792 goto out;
793 }
794
795 if (*s == '!' && options.permit_local_command) {
796 s++;
797 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000798 goto out;
799 }
800
801 if (*s == 'K') {
802 delete = 1;
803 s++;
804 }
Damien Miller0164cb82008-11-05 16:30:31 +1100805 if (*s == 'L')
806 local = 1;
807 else if (*s == 'R')
808 remote = 1;
809 else if (*s == 'D')
810 dynamic = 1;
811 else {
Damien Miller996acd22003-04-09 20:59:48 +1000812 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000813 goto out;
814 }
Damien Miller0164cb82008-11-05 16:30:31 +1100815
Damien Millerfdb23062013-11-21 13:57:15 +1100816 while (isspace((u_char)*++s))
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100817 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000818
Damien Millere1537f92010-01-26 13:26:22 +1100819 /* XXX update list of forwards in options */
Darren Tuckere7066df2004-05-24 10:18:05 +1000820 if (delete) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000821 /* We pass 1 for dynamicfwd to restrict to 1 or 2 fields. */
822 if (!parse_forward(&fwd, s, 1, 0)) {
823 logit("Bad forwarding close specification.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000824 goto out;
825 }
Damien Millerff773642011-09-22 21:39:48 +1000826 if (remote)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000827 ok = channel_request_rforward_cancel(ssh, &fwd) == 0;
Damien Millerff773642011-09-22 21:39:48 +1000828 else if (dynamic)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000829 ok = channel_cancel_lport_listener(ssh, &fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +1000830 0, &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000831 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000832 ok = channel_cancel_lport_listener(ssh, &fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +1000833 CHANNEL_CANCEL_PORT_STATIC,
834 &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000835 if (!ok) {
dtucker@openbsd.org9390b002017-01-29 21:35:23 +0000836 logit("Unknown port forwarding.");
Damien Millerff773642011-09-22 21:39:48 +1000837 goto out;
838 }
839 logit("Canceled forwarding.");
Darren Tuckere7066df2004-05-24 10:18:05 +1000840 } else {
Damien Miller4bf648f2009-02-14 16:28:21 +1100841 if (!parse_forward(&fwd, s, dynamic, remote)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000842 logit("Bad forwarding specification.");
843 goto out;
844 }
Damien Miller0164cb82008-11-05 16:30:31 +1100845 if (local || dynamic) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000846 if (!channel_setup_local_fwd_listener(ssh, &fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +1000847 &options.fwd_opts)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000848 logit("Port forwarding failed.");
849 goto out;
850 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100851 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000852 if (channel_request_remote_forwarding(ssh, &fwd) < 0) {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000853 logit("Port forwarding failed.");
854 goto out;
855 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100856 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000857 logit("Forwarding port.");
858 }
859
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000860out:
861 signal(SIGINT, handler);
Damien Miller21771e22011-05-15 08:45:50 +1000862 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tuckera627d422013-06-02 07:31:17 +1000863 free(cmd);
864 free(fwd.listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000865 free(fwd.listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000866 free(fwd.connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000867 free(fwd.connect_path);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000868}
869
Darren Tucker92a39cf2012-09-07 11:20:20 +1000870/* reasons to suppress output of an escape command in help output */
871#define SUPPRESS_NEVER 0 /* never suppress, always show */
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000872#define SUPPRESS_MUXCLIENT 1 /* don't show in mux client sessions */
873#define SUPPRESS_MUXMASTER 2 /* don't show in mux master sessions */
874#define SUPPRESS_SYSLOG 4 /* don't show when logging to syslog */
Darren Tucker92a39cf2012-09-07 11:20:20 +1000875struct escape_help_text {
876 const char *cmd;
877 const char *text;
878 unsigned int flags;
879};
880static struct escape_help_text esc_txt[] = {
881 {".", "terminate session", SUPPRESS_MUXMASTER},
882 {".", "terminate connection (and any multiplexed sessions)",
883 SUPPRESS_MUXCLIENT},
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000884 {"B", "send a BREAK to the remote system", SUPPRESS_NEVER},
Darren Tucker92a39cf2012-09-07 11:20:20 +1000885 {"C", "open a command line", SUPPRESS_MUXCLIENT},
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000886 {"R", "request rekey", SUPPRESS_NEVER},
Darren Tuckerca0d0fd2012-09-07 11:22:24 +1000887 {"V/v", "decrease/increase verbosity (LogLevel)", SUPPRESS_MUXCLIENT},
Darren Tucker92a39cf2012-09-07 11:20:20 +1000888 {"^Z", "suspend ssh", SUPPRESS_MUXCLIENT},
889 {"#", "list forwarded connections", SUPPRESS_NEVER},
890 {"&", "background ssh (when waiting for connections to terminate)",
891 SUPPRESS_MUXCLIENT},
892 {"?", "this message", SUPPRESS_NEVER},
893};
894
895static void
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000896print_escape_help(Buffer *b, int escape_char, int mux_client, int using_stderr)
Darren Tucker92a39cf2012-09-07 11:20:20 +1000897{
898 unsigned int i, suppress_flags;
899 char string[1024];
900
901 snprintf(string, sizeof string, "%c?\r\n"
902 "Supported escape sequences:\r\n", escape_char);
903 buffer_append(b, string, strlen(string));
904
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000905 suppress_flags =
Darren Tucker92a39cf2012-09-07 11:20:20 +1000906 (mux_client ? SUPPRESS_MUXCLIENT : 0) |
907 (mux_client ? 0 : SUPPRESS_MUXMASTER) |
908 (using_stderr ? 0 : SUPPRESS_SYSLOG);
909
910 for (i = 0; i < sizeof(esc_txt)/sizeof(esc_txt[0]); i++) {
911 if (esc_txt[i].flags & suppress_flags)
912 continue;
Darren Tuckerca0d0fd2012-09-07 11:22:24 +1000913 snprintf(string, sizeof string, " %c%-3s - %s\r\n",
Darren Tucker92a39cf2012-09-07 11:20:20 +1000914 escape_char, esc_txt[i].cmd, esc_txt[i].text);
915 buffer_append(b, string, strlen(string));
916 }
917
918 snprintf(string, sizeof string,
Darren Tuckerca0d0fd2012-09-07 11:22:24 +1000919 " %c%c - send the escape character by typing it twice\r\n"
Darren Tucker92a39cf2012-09-07 11:20:20 +1000920 "(Note that escapes are only recognized immediately after "
921 "newline.)\r\n", escape_char, escape_char);
922 buffer_append(b, string, strlen(string));
923}
924
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000925/*
naddy@openbsd.org768405f2017-05-03 21:08:09 +0000926 * Process the characters one by one.
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000927 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000928static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000929process_escapes(struct ssh *ssh, Channel *c,
930 Buffer *bin, Buffer *bout, Buffer *berr,
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000931 char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +1000932{
933 char string[1024];
934 pid_t pid;
935 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000936 u_int i;
937 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +1000938 char *s;
djm@openbsd.org05164352017-04-30 23:21:54 +0000939 struct escape_filter_ctx *efc = c->filter_ctx == NULL ?
940 NULL : (struct escape_filter_ctx *)c->filter_ctx;
Damien Millerad833b32000-08-23 10:46:23 +1000941
djm@openbsd.org05164352017-04-30 23:21:54 +0000942 if (c->filter_ctx == NULL)
943 return 0;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000944
Damien Millereccb9de2005-06-17 12:59:34 +1000945 if (len <= 0)
946 return (0);
947
948 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +1000949 /* Get one character at a time. */
950 ch = buf[i];
951
djm@openbsd.org05164352017-04-30 23:21:54 +0000952 if (efc->escape_pending) {
Damien Millerad833b32000-08-23 10:46:23 +1000953 /* We have previously seen an escape character. */
954 /* Clear the flag now. */
djm@openbsd.org05164352017-04-30 23:21:54 +0000955 efc->escape_pending = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000956
957 /* Process the escaped character. */
958 switch (ch) {
959 case '.':
960 /* Terminate the connection. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000961 snprintf(string, sizeof string, "%c.\r\n",
djm@openbsd.org05164352017-04-30 23:21:54 +0000962 efc->escape_char);
Damien Millerad833b32000-08-23 10:46:23 +1000963 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +1000964
Damien Millere1537f92010-01-26 13:26:22 +1100965 if (c && c->ctl_chan != -1) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000966 chan_read_failed(ssh, c);
967 chan_write_failed(ssh, c);
968 if (c->detach_user) {
969 c->detach_user(ssh,
970 c->self, NULL);
971 }
Damien Miller36187092013-06-10 13:07:11 +1000972 c->type = SSH_CHANNEL_ABANDONED;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000973 buffer_clear(c->input);
974 chan_ibuf_empty(ssh, c);
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000975 return 0;
976 } else
977 quit_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +1000978 return -1;
979
980 case 'Z' - 64:
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000981 /* XXX support this for mux clients */
Damien Millere1537f92010-01-26 13:26:22 +1100982 if (c && c->ctl_chan != -1) {
Darren Tuckerf111d402012-09-07 11:21:42 +1000983 char b[16];
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000984 noescape:
Darren Tuckerf111d402012-09-07 11:21:42 +1000985 if (ch == 'Z' - 64)
986 snprintf(b, sizeof b, "^Z");
987 else
988 snprintf(b, sizeof b, "%c", ch);
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000989 snprintf(string, sizeof string,
Darren Tuckerf111d402012-09-07 11:21:42 +1000990 "%c%s escape not available to "
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000991 "multiplexed sessions\r\n",
djm@openbsd.org05164352017-04-30 23:21:54 +0000992 efc->escape_char, b);
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000993 buffer_append(berr, string,
994 strlen(string));
995 continue;
996 }
Darren Tucker4d5cd332008-06-13 04:51:14 +1000997 /* Suspend the program. Inform the user */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000998 snprintf(string, sizeof string,
djm@openbsd.org05164352017-04-30 23:21:54 +0000999 "%c^Z [suspend ssh]\r\n", efc->escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001000 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001001
1002 /* Restore terminal modes and suspend. */
1003 client_suspend_self(bin, bout, berr);
1004
1005 /* We have been continued. */
1006 continue;
1007
Damien Miller54c45982003-05-15 10:20:13 +10001008 case 'B':
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001009 snprintf(string, sizeof string,
djm@openbsd.org05164352017-04-30 23:21:54 +00001010 "%cB\r\n", efc->escape_char);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001011 buffer_append(berr, string, strlen(string));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001012 channel_request_start(ssh, c->self, "break", 0);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001013 packet_put_int(1000);
1014 packet_send();
Damien Miller54c45982003-05-15 10:20:13 +10001015 continue;
1016
Ben Lindstromf28f6342001-04-04 02:03:04 +00001017 case 'R':
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001018 if (datafellows & SSH_BUG_NOREKEY)
1019 logit("Server does not "
1020 "support re-keying");
1021 else
1022 need_rekeying = 1;
Ben Lindstromf28f6342001-04-04 02:03:04 +00001023 continue;
1024
Darren Tucker50a48d02012-09-06 21:25:37 +10001025 case 'V':
1026 /* FALLTHROUGH */
1027 case 'v':
1028 if (c && c->ctl_chan != -1)
1029 goto noescape;
1030 if (!log_is_on_stderr()) {
1031 snprintf(string, sizeof string,
1032 "%c%c [Logging to syslog]\r\n",
djm@openbsd.org05164352017-04-30 23:21:54 +00001033 efc->escape_char, ch);
Darren Tucker50a48d02012-09-06 21:25:37 +10001034 buffer_append(berr, string,
1035 strlen(string));
1036 continue;
1037 }
1038 if (ch == 'V' && options.log_level >
1039 SYSLOG_LEVEL_QUIET)
1040 log_change_level(--options.log_level);
1041 if (ch == 'v' && options.log_level <
1042 SYSLOG_LEVEL_DEBUG3)
1043 log_change_level(++options.log_level);
1044 snprintf(string, sizeof string,
djm@openbsd.org05164352017-04-30 23:21:54 +00001045 "%c%c [LogLevel %s]\r\n",
1046 efc->escape_char, ch,
Darren Tucker50a48d02012-09-06 21:25:37 +10001047 log_level_name(options.log_level));
1048 buffer_append(berr, string, strlen(string));
1049 continue;
1050
Damien Millerad833b32000-08-23 10:46:23 +10001051 case '&':
Damien Millere1537f92010-01-26 13:26:22 +11001052 if (c && c->ctl_chan != -1)
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001053 goto noescape;
Damien Millerad833b32000-08-23 10:46:23 +10001054 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001055 * Detach the program (continue to serve
1056 * connections, but put in background and no
1057 * more new connections).
Damien Millerad833b32000-08-23 10:46:23 +10001058 */
Damien Miller96507ef2001-11-12 10:52:25 +11001059 /* Restore tty modes. */
Damien Miller21771e22011-05-15 08:45:50 +10001060 leave_raw_mode(
1061 options.request_tty == REQUEST_TTY_FORCE);
Damien Miller96507ef2001-11-12 10:52:25 +11001062
1063 /* Stop listening for new connections. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001064 channel_stop_listening(ssh);
Damien Miller96507ef2001-11-12 10:52:25 +11001065
1066 snprintf(string, sizeof string,
djm@openbsd.org05164352017-04-30 23:21:54 +00001067 "%c& [backgrounded]\n", efc->escape_char);
Damien Miller96507ef2001-11-12 10:52:25 +11001068 buffer_append(berr, string, strlen(string));
1069
1070 /* Fork into background. */
1071 pid = fork();
1072 if (pid < 0) {
1073 error("fork: %.100s", strerror(errno));
1074 continue;
1075 }
1076 if (pid != 0) { /* This is the parent. */
1077 /* The parent just exits. */
1078 exit(0);
1079 }
1080 /* The child continues serving connections. */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001081 buffer_append(bin, "\004", 1);
1082 /* fake EOF on stdin */
1083 return -1;
Damien Millerad833b32000-08-23 10:46:23 +10001084 case '?':
djm@openbsd.org05164352017-04-30 23:21:54 +00001085 print_escape_help(berr, efc->escape_char,
Darren Tucker92a39cf2012-09-07 11:20:20 +10001086 (c && c->ctl_chan != -1),
1087 log_is_on_stderr());
Damien Millerad833b32000-08-23 10:46:23 +10001088 continue;
1089
1090 case '#':
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001091 snprintf(string, sizeof string, "%c#\r\n",
djm@openbsd.org05164352017-04-30 23:21:54 +00001092 efc->escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001093 buffer_append(berr, string, strlen(string));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001094 s = channel_open_message(ssh);
Damien Millerad833b32000-08-23 10:46:23 +10001095 buffer_append(berr, s, strlen(s));
Darren Tuckera627d422013-06-02 07:31:17 +10001096 free(s);
Damien Millerad833b32000-08-23 10:46:23 +10001097 continue;
1098
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001099 case 'C':
Damien Millere1537f92010-01-26 13:26:22 +11001100 if (c && c->ctl_chan != -1)
Damien Miller586b0052008-12-09 14:11:32 +11001101 goto noescape;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001102 process_cmdline(ssh);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001103 continue;
1104
Damien Millerad833b32000-08-23 10:46:23 +10001105 default:
djm@openbsd.org05164352017-04-30 23:21:54 +00001106 if (ch != efc->escape_char) {
1107 buffer_put_char(bin, efc->escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001108 bytes++;
1109 }
1110 /* Escaped characters fall through here */
1111 break;
1112 }
1113 } else {
1114 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001115 * The previous character was not an escape char.
1116 * Check if this is an escape.
Damien Millerad833b32000-08-23 10:46:23 +10001117 */
djm@openbsd.org05164352017-04-30 23:21:54 +00001118 if (last_was_cr && ch == efc->escape_char) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001119 /*
1120 * It is. Set the flag and continue to
1121 * next character.
1122 */
djm@openbsd.org05164352017-04-30 23:21:54 +00001123 efc->escape_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001124 continue;
1125 }
1126 }
1127
1128 /*
1129 * Normal character. Record whether it was a newline,
1130 * and append it to the buffer.
1131 */
1132 last_was_cr = (ch == '\r' || ch == '\n');
1133 buffer_put_char(bin, ch);
1134 bytes++;
1135 }
1136 return bytes;
1137}
1138
Damien Miller5428f641999-11-25 11:54:57 +11001139/*
Damien Millerb38eff82000-04-01 11:09:21 +10001140 * Get packets from the connection input buffer, and process them as long as
1141 * there are packets available.
1142 *
1143 * Any unknown packets received during the actual
1144 * session cause the session to terminate. This is
1145 * intended to make debugging easier since no
1146 * confirmations are sent. Any compatible protocol
1147 * extensions must be negotiated during the
1148 * preparatory phase.
1149 */
1150
Ben Lindstrombba81212001-06-25 05:01:22 +00001151static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001152client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001153{
markus@openbsd.org92e9fe62017-05-31 07:00:13 +00001154 ssh_dispatch_run_fatal(active_state, DISPATCH_NONBLOCK, &quit_pending);
Damien Millerb38eff82000-04-01 11:09:21 +10001155}
1156
Damien Millerad833b32000-08-23 10:46:23 +10001157/* scan buf[] for '~' before sending data to the peer */
1158
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001159/* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1160void *
1161client_new_escape_filter_ctx(int escape_char)
1162{
1163 struct escape_filter_ctx *ret;
1164
Damien Miller6c81fee2013-11-08 12:19:55 +11001165 ret = xcalloc(1, sizeof(*ret));
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001166 ret->escape_pending = 0;
1167 ret->escape_char = escape_char;
1168 return (void *)ret;
1169}
1170
Darren Tucker84c56f52008-06-13 04:55:46 +10001171/* Free the escape filter context on channel free */
1172void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001173client_filter_cleanup(struct ssh *ssh, int cid, void *ctx)
Darren Tucker84c56f52008-06-13 04:55:46 +10001174{
Darren Tuckera627d422013-06-02 07:31:17 +10001175 free(ctx);
Darren Tucker84c56f52008-06-13 04:55:46 +10001176}
1177
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001178int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001179client_simple_escape_filter(struct ssh *ssh, Channel *c, char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001180{
Damien Miller5771ed72008-05-19 15:35:33 +10001181 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1182 return 0;
1183
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001184 return process_escapes(ssh, c, c->input, c->output, c->extended,
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001185 buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001186}
1187
Ben Lindstrombba81212001-06-25 05:01:22 +00001188static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001189client_channel_closed(struct ssh *ssh, int id, void *arg)
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001190{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001191 channel_cancel_cleanup(ssh, id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001192 session_closed = 1;
Damien Miller21771e22011-05-15 08:45:50 +10001193 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001194}
1195
Damien Millerb38eff82000-04-01 11:09:21 +10001196/*
Damien Miller5428f641999-11-25 11:54:57 +11001197 * Implements the interactive session with the server. This is called after
1198 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001199 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1200 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001201 */
Damien Miller4af51302000-04-16 11:18:38 +10001202int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001203client_loop(struct ssh *ssh, int have_pty, int escape_char_arg,
1204 int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001205{
Damien Miller5e953212001-01-30 09:14:00 +11001206 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001207 double start_time, total_time;
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001208 int r, max_fd = 0, max_fd2 = 0, len;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001209 u_int64_t ibytes, obytes;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001210 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001211 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001212
Damien Miller95def091999-11-25 00:26:21 +11001213 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001214
semarie@openbsd.orgd7d2bc92015-12-26 07:46:03 +00001215 if (options.control_master &&
djm@openbsd.org368dd972016-07-23 02:54:08 +00001216 !option_clear_or_none(options.control_path)) {
semarie@openbsd.orgd7d2bc92015-12-26 07:46:03 +00001217 debug("pledge: id");
tb@openbsd.org372807c2016-07-11 21:38:13 +00001218 if (pledge("stdio rpath wpath cpath unix inet dns recvfd proc exec id tty",
semarie@openbsd.orgd7d2bc92015-12-26 07:46:03 +00001219 NULL) == -1)
1220 fatal("%s pledge(): %s", __func__, strerror(errno));
1221
1222 } else if (options.forward_x11 || options.permit_local_command) {
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001223 debug("pledge: exec");
1224 if (pledge("stdio rpath wpath cpath unix inet dns proc exec tty",
1225 NULL) == -1)
1226 fatal("%s pledge(): %s", __func__, strerror(errno));
1227
1228 } else if (options.update_hostkeys) {
1229 debug("pledge: filesystem full");
1230 if (pledge("stdio rpath wpath cpath unix inet dns proc tty",
1231 NULL) == -1)
1232 fatal("%s pledge(): %s", __func__, strerror(errno));
1233
djm@openbsd.org368dd972016-07-23 02:54:08 +00001234 } else if (!option_clear_or_none(options.proxy_command) ||
1235 fork_after_authentication_flag) {
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001236 debug("pledge: proc");
1237 if (pledge("stdio cpath unix inet dns proc tty", NULL) == -1)
1238 fatal("%s pledge(): %s", __func__, strerror(errno));
1239
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001240 } else {
1241 debug("pledge: network");
mestre@openbsd.orge2386452017-06-23 07:24:48 +00001242 if (pledge("stdio unix inet dns proc tty", NULL) == -1)
semarie@openbsd.orgb91926a2015-12-03 17:00:18 +00001243 fatal("%s pledge(): %s", __func__, strerror(errno));
1244 }
1245
dtucker@openbsd.org@openbsd.org2d638e92017-11-25 05:58:47 +00001246 start_time = monotime_double();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001247
Damien Miller95def091999-11-25 00:26:21 +11001248 /* Initialize variables. */
Damien Miller95def091999-11-25 00:26:21 +11001249 last_was_cr = 1;
1250 exit_status = -1;
Damien Miller95def091999-11-25 00:26:21 +11001251 connection_in = packet_get_connection_in();
1252 connection_out = packet_get_connection_out();
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001253 max_fd = MAXIMUM(connection_in, connection_out);
Damien Miller5e953212001-01-30 09:14:00 +11001254
Damien Miller95def091999-11-25 00:26:21 +11001255 quit_pending = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001256
Damien Miller95def091999-11-25 00:26:21 +11001257 /* Initialize buffers. */
Damien Miller95def091999-11-25 00:26:21 +11001258 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001259
Damien Millerb38eff82000-04-01 11:09:21 +10001260 client_init_dispatch();
1261
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001262 /*
1263 * Set signal handlers, (e.g. to restore non-blocking mode)
1264 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1265 */
Darren Tucker07336da2004-11-05 20:02:16 +11001266 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1267 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001268 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1269 signal(SIGINT, signal_handler);
1270 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1271 signal(SIGQUIT, signal_handler);
1272 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1273 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001274 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001275
Damien Miller95def091999-11-25 00:26:21 +11001276 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001277 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001278
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001279 session_ident = ssh2_chan_id;
1280 if (session_ident != -1) {
1281 if (escape_char_arg != SSH_ESCAPECHAR_NONE) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001282 channel_register_filter(ssh, session_ident,
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001283 client_simple_escape_filter, NULL,
1284 client_filter_cleanup,
1285 client_new_escape_filter_ctx(
1286 escape_char_arg));
Damien Miller6c3eec72011-05-05 14:16:22 +10001287 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001288 channel_register_cleanup(ssh, session_ident,
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001289 client_channel_closed, 0);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001290 }
Damien Millerad833b32000-08-23 10:46:23 +10001291
Damien Miller95def091999-11-25 00:26:21 +11001292 /* Main loop of the client for the interactive session mode. */
1293 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001294
Damien Miller5428f641999-11-25 11:54:57 +11001295 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001296 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001297
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001298 if (session_closed && !channel_still_open(ssh))
Damien Miller1383bd82000-04-06 12:32:37 +10001299 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001300
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001301 if (ssh_packet_is_rekeying(ssh)) {
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001302 debug("rekeying in progress");
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001303 } else if (need_rekeying) {
1304 /* manual rekey request */
1305 debug("need rekeying");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001306 if ((r = kex_start_rekex(ssh)) != 0)
djm@openbsd.org19bcf2e2016-02-08 10:57:07 +00001307 fatal("%s: kex_start_rekex: %s", __func__,
1308 ssh_err(r));
1309 need_rekeying = 0;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001310 } else {
1311 /*
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001312 * Make packets from buffered channel data, and
1313 * enqueue them for sending to the server.
1314 */
1315 if (packet_not_very_much_data_to_write())
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001316 channel_output_poll(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001317
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001318 /*
1319 * Check if the window size has changed, and buffer a
1320 * message about it to the server if so.
1321 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001322 client_check_window_change(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001323
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001324 if (quit_pending)
1325 break;
1326 }
Damien Miller5428f641999-11-25 11:54:57 +11001327 /*
1328 * Wait until we have something to do (something becomes
1329 * available on one of the descriptors).
1330 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001331 max_fd2 = max_fd;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001332 client_wait_until_can_do_something(ssh, &readset, &writeset,
1333 &max_fd2, &nalloc, ssh_packet_is_rekeying(ssh));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001334
Damien Miller95def091999-11-25 00:26:21 +11001335 if (quit_pending)
1336 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001337
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001338 /* Do channel operations unless rekeying in progress. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001339 if (!ssh_packet_is_rekeying(ssh))
1340 channel_after_select(ssh, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001341
Damien Miller1383bd82000-04-06 12:32:37 +10001342 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001343 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001344
Damien Miller1383bd82000-04-06 12:32:37 +10001345 if (quit_pending)
1346 break;
1347
Darren Tucker4d5cd332008-06-13 04:51:14 +10001348 /*
1349 * Send as much buffered packet data as possible to the
1350 * sender.
1351 */
Damien Miller5e953212001-01-30 09:14:00 +11001352 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001353 packet_write_poll();
Damien Millere11e1ea2010-08-03 16:04:46 +10001354
1355 /*
1356 * If we are a backgrounded control master, and the
1357 * timeout has expired without any active client
1358 * connections, then quit.
1359 */
1360 if (control_persist_exit_time > 0) {
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001361 if (monotime() >= control_persist_exit_time) {
Damien Millere11e1ea2010-08-03 16:04:46 +10001362 debug("ControlPersist timeout expired");
1363 break;
1364 }
1365 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001366 }
Darren Tuckera627d422013-06-02 07:31:17 +10001367 free(readset);
1368 free(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001369
Damien Miller95def091999-11-25 00:26:21 +11001370 /* Terminate the session. */
1371
1372 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001373 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001374
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001375 packet_start(SSH2_MSG_DISCONNECT);
1376 packet_put_int(SSH2_DISCONNECT_BY_APPLICATION);
1377 packet_put_cstring("disconnected by user");
1378 packet_put_cstring(""); /* language tag */
1379 packet_send();
1380 packet_write_wait();
Darren Tucker12b4a652009-06-21 18:14:48 +10001381
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001382 channel_free_all(ssh);
Damien Miller95def091999-11-25 00:26:21 +11001383
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001384 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001385 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001386
1387 /* restore blocking io */
1388 if (!isatty(fileno(stdin)))
1389 unset_nonblock(fileno(stdin));
1390 if (!isatty(fileno(stdout)))
1391 unset_nonblock(fileno(stdout));
1392 if (!isatty(fileno(stderr)))
1393 unset_nonblock(fileno(stderr));
1394
Damien Millerd6965512003-12-17 16:31:53 +11001395 /*
1396 * If there was no shell or command requested, there will be no remote
1397 * exit status to be returned. In that case, clear error code if the
1398 * connection was deliberately terminated at this end.
1399 */
1400 if (no_shell_flag && received_signal == SIGTERM) {
1401 received_signal = 0;
1402 exit_status = 0;
1403 }
1404
dtucker@openbsd.orgb1e72df2017-07-14 03:18:21 +00001405 if (received_signal) {
1406 verbose("Killed by signal %d.", (int) received_signal);
1407 cleanup_exit(0);
1408 }
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001409
1410 /*
1411 * In interactive mode (with pseudo tty) display a message indicating
1412 * that the connection has been closed.
1413 */
1414 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001415 snprintf(buf, sizeof buf,
1416 "Connection to %.64s closed.\r\n", host);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001417 buffer_append(&stderr_buffer, buf, strlen(buf));
1418 }
1419
Damien Miller95def091999-11-25 00:26:21 +11001420 /* Output any buffered data for stderr. */
Damien Millercfd6e4f2011-01-16 23:18:33 +11001421 if (buffer_len(&stderr_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001422 len = atomicio(vwrite, fileno(stderr),
1423 buffer_ptr(&stderr_buffer), buffer_len(&stderr_buffer));
Damien Millercfd6e4f2011-01-16 23:18:33 +11001424 if (len < 0 || (u_int)len != buffer_len(&stderr_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001425 error("Write failed flushing stderr buffer.");
Damien Millercfd6e4f2011-01-16 23:18:33 +11001426 else
1427 buffer_consume(&stderr_buffer, len);
Damien Miller95def091999-11-25 00:26:21 +11001428 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001429
Damien Miller95def091999-11-25 00:26:21 +11001430 /* Clear and free any buffers. */
jsg@openbsd.org458abc22016-01-23 05:31:35 +00001431 explicit_bzero(buf, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11001432 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001433
Damien Miller95def091999-11-25 00:26:21 +11001434 /* Report bytes transferred, and transfer rates. */
dtucker@openbsd.org@openbsd.org2d638e92017-11-25 05:58:47 +00001435 total_time = monotime_double() - start_time;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001436 packet_get_bytes(&ibytes, &obytes);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001437 verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
Damien Miller821de0a2011-01-11 17:20:29 +11001438 (unsigned long long)obytes, (unsigned long long)ibytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001439 if (total_time > 0)
Damien Millerb61f3fc2008-07-11 17:36:48 +10001440 verbose("Bytes per second: sent %.1f, received %.1f",
1441 obytes / total_time, ibytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001442 /* Return the exit status of the program. */
1443 debug("Exit status %d", exit_status);
1444 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001445}
Damien Millerb38eff82000-04-01 11:09:21 +10001446
1447/*********/
1448
Ben Lindstrombba81212001-06-25 05:01:22 +00001449static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001450client_request_forwarded_tcpip(struct ssh *ssh, const char *request_type,
1451 int rchan, u_int rwindow, u_int rmaxpack)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001452{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001453 Channel *c = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00001454 struct sshbuf *b = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001455 char *listen_address, *originator_address;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001456 u_short listen_port, originator_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00001457 int r;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001458
1459 /* Get rest of the packet */
1460 listen_address = packet_get_string(NULL);
1461 listen_port = packet_get_int();
1462 originator_address = packet_get_string(NULL);
1463 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001464 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001465
Damien Miller7acefbb2014-07-18 14:11:24 +10001466 debug("%s: listen %s port %d, originator %s port %d", __func__,
1467 listen_address, listen_port, originator_address, originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001468
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001469 c = channel_connect_by_listen_address(ssh, listen_address, listen_port,
Damien Millerbd740252008-05-19 15:37:09 +10001470 "forwarded-tcpip", originator_address);
1471
markus@openbsd.org8d057842016-09-30 09:19:13 +00001472 if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) {
1473 if ((b = sshbuf_new()) == NULL) {
1474 error("%s: alloc reply", __func__);
1475 goto out;
1476 }
1477 /* reconstruct and send to muxclient */
1478 if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
1479 (r = sshbuf_put_u8(b, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
1480 (r = sshbuf_put_cstring(b, request_type)) != 0 ||
1481 (r = sshbuf_put_u32(b, rchan)) != 0 ||
1482 (r = sshbuf_put_u32(b, rwindow)) != 0 ||
1483 (r = sshbuf_put_u32(b, rmaxpack)) != 0 ||
1484 (r = sshbuf_put_cstring(b, listen_address)) != 0 ||
1485 (r = sshbuf_put_u32(b, listen_port)) != 0 ||
1486 (r = sshbuf_put_cstring(b, originator_address)) != 0 ||
1487 (r = sshbuf_put_u32(b, originator_port)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001488 (r = sshbuf_put_stringb(c->output, b)) != 0) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00001489 error("%s: compose for muxclient %s", __func__,
1490 ssh_err(r));
1491 goto out;
1492 }
1493 }
1494
1495 out:
1496 sshbuf_free(b);
Darren Tuckera627d422013-06-02 07:31:17 +10001497 free(originator_address);
1498 free(listen_address);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001499 return c;
1500}
1501
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001502static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001503client_request_forwarded_streamlocal(struct ssh *ssh,
1504 const char *request_type, int rchan)
Damien Miller7acefbb2014-07-18 14:11:24 +10001505{
1506 Channel *c = NULL;
1507 char *listen_path;
1508
1509 /* Get the remote path. */
1510 listen_path = packet_get_string(NULL);
1511 /* XXX: Skip reserved field for now. */
1512 if (packet_get_string_ptr(NULL) == NULL)
1513 fatal("%s: packet_get_string_ptr failed", __func__);
1514 packet_check_eom();
1515
1516 debug("%s: %s", __func__, listen_path);
1517
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001518 c = channel_connect_by_listen_path(ssh, listen_path,
Damien Miller7acefbb2014-07-18 14:11:24 +10001519 "forwarded-streamlocal@openssh.com", "forwarded-streamlocal");
1520 free(listen_path);
1521 return c;
1522}
1523
1524static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001525client_request_x11(struct ssh *ssh, const char *request_type, int rchan)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001526{
1527 Channel *c = NULL;
1528 char *originator;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001529 u_short originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001530 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001531
1532 if (!options.forward_x11) {
1533 error("Warning: ssh server tried X11 forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001534 error("Warning: this is probably a break-in attempt by a "
1535 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001536 return NULL;
1537 }
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001538 if (x11_refuse_time != 0 && (u_int)monotime() >= x11_refuse_time) {
Damien Miller1ab6a512010-06-26 10:02:24 +10001539 verbose("Rejected X11 connection after ForwardX11Timeout "
1540 "expired");
1541 return NULL;
1542 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001543 originator = packet_get_string(NULL);
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001544 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001545 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001546 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001547 debug("client_request_x11: request from %s %d", originator,
1548 originator_port);
Darren Tuckera627d422013-06-02 07:31:17 +10001549 free(originator);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001550 sock = x11_connect_display(ssh);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001551 if (sock < 0)
1552 return NULL;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001553 c = channel_new(ssh, "x11",
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001554 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001555 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001556 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001557 return c;
1558}
1559
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001560static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001561client_request_agent(struct ssh *ssh, const char *request_type, int rchan)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001562{
1563 Channel *c = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001564 int r, sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001565
1566 if (!options.forward_agent) {
1567 error("Warning: ssh server tried agent forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001568 error("Warning: this is probably a break-in attempt by a "
1569 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001570 return NULL;
1571 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001572 if ((r = ssh_get_authentication_socket(&sock)) != 0) {
1573 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1574 debug("%s: ssh_get_authentication_socket: %s",
1575 __func__, ssh_err(r));
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001576 return NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001577 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001578 c = channel_new(ssh, "authentication agent connection",
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001579 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001580 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001581 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001582 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001583 return c;
1584}
1585
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001586char *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001587client_request_tun_fwd(struct ssh *ssh, int tun_mode,
1588 int local_tun, int remote_tun)
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001589{
1590 Channel *c;
1591 int fd;
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001592 char *ifname = NULL;
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001593
1594 if (tun_mode == SSH_TUNMODE_NO)
1595 return 0;
1596
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001597 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1598
1599 /* Open local tunnel device */
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001600 if ((fd = tun_open(local_tun, tun_mode, &ifname)) == -1) {
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001601 error("Tunnel device open failed.");
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001602 return NULL;
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001603 }
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001604 debug("Tunnel forwarding using interface %s", ifname);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001605
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001606 c = channel_new(ssh, "tun", SSH_CHANNEL_OPENING, fd, fd, -1,
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001607 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1608 c->datagram = 1;
1609
1610#if defined(SSH_TUN_FILTER)
1611 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
Damien Miller871f1e42017-09-12 18:01:35 +10001612 channel_register_filter(ssh, c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +10001613 sys_tun_outfilter, NULL, NULL);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001614#endif
1615
1616 packet_start(SSH2_MSG_CHANNEL_OPEN);
1617 packet_put_cstring("tun@openssh.com");
1618 packet_put_int(c->self);
1619 packet_put_int(c->local_window_max);
1620 packet_put_int(c->local_maxpacket);
1621 packet_put_int(tun_mode);
1622 packet_put_int(remote_tun);
1623 packet_send();
1624
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001625 return ifname;
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001626}
1627
Damien Millerbd483e72000-04-30 10:00:53 +10001628/* XXXX move to generic input handler */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001629static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001630client_input_channel_open(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerbd483e72000-04-30 10:00:53 +10001631{
1632 Channel *c = NULL;
1633 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001634 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001635 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001636
1637 ctype = packet_get_string(&len);
1638 rchan = packet_get_int();
1639 rwindow = packet_get_int();
1640 rmaxpack = packet_get_int();
1641
Damien Millere247cc42000-05-07 12:03:14 +10001642 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001643 ctype, rchan, rwindow, rmaxpack);
1644
Damien Miller0bc1bd82000-11-13 22:57:25 +11001645 if (strcmp(ctype, "forwarded-tcpip") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001646 c = client_request_forwarded_tcpip(ssh, ctype, rchan, rwindow,
markus@openbsd.org8d057842016-09-30 09:19:13 +00001647 rmaxpack);
Damien Miller7acefbb2014-07-18 14:11:24 +10001648 } else if (strcmp(ctype, "forwarded-streamlocal@openssh.com") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001649 c = client_request_forwarded_streamlocal(ssh, ctype, rchan);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001650 } else if (strcmp(ctype, "x11") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001651 c = client_request_x11(ssh, ctype, rchan);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001652 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001653 c = client_request_agent(ssh, ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001654 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00001655 if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) {
1656 debug3("proxied to downstream: %s", ctype);
1657 } else if (c != NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10001658 debug("confirm %s", ctype);
1659 c->remote_id = rchan;
djm@openbsd.org9f532292017-09-12 06:35:31 +00001660 c->have_remote_id = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001661 c->remote_window = rwindow;
1662 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001663 if (c->type != SSH_CHANNEL_CONNECTING) {
1664 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1665 packet_put_int(c->remote_id);
1666 packet_put_int(c->self);
1667 packet_put_int(c->local_window);
1668 packet_put_int(c->local_maxpacket);
1669 packet_send();
1670 }
Damien Millerbd483e72000-04-30 10:00:53 +10001671 } else {
1672 debug("failure %s", ctype);
1673 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1674 packet_put_int(rchan);
1675 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001676 packet_put_cstring("open failed");
1677 packet_put_cstring("");
Damien Millerbd483e72000-04-30 10:00:53 +10001678 packet_send();
1679 }
Darren Tuckera627d422013-06-02 07:31:17 +10001680 free(ctype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001681 return 0;
Damien Millerbd483e72000-04-30 10:00:53 +10001682}
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001683
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001684static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001685client_input_channel_req(int type, u_int32_t seq, struct ssh *ssh)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001686{
1687 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001688 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001689 char *rtype;
1690
1691 id = packet_get_int();
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001692 c = channel_lookup(ssh, id);
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001693 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00001694 return 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001695 rtype = packet_get_string(NULL);
1696 reply = packet_get_char();
1697
1698 debug("client_input_channel_req: channel %d rtype %s reply %d",
1699 id, rtype, reply);
1700
Damien Miller3bbd8782004-06-18 22:23:22 +10001701 if (id == -1) {
1702 error("client_input_channel_req: request for channel -1");
markus@openbsd.org8d057842016-09-30 09:19:13 +00001703 } else if (c == NULL) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001704 error("client_input_channel_req: channel %d: "
1705 "unknown channel", id);
Damien Millerbab9bd42008-05-19 16:06:47 +10001706 } else if (strcmp(rtype, "eow@openssh.com") == 0) {
1707 packet_check_eom();
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001708 chan_rcvd_eow(ssh, c);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001709 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10001710 exitval = packet_get_int();
Damien Millere1537f92010-01-26 13:26:22 +11001711 if (c->ctl_chan != -1) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001712 mux_exit_message(ssh, c, exitval);
Damien Miller0e220db2004-06-15 10:34:08 +10001713 success = 1;
Darren Tucker29440822010-01-08 17:08:35 +11001714 } else if (id == session_ident) {
1715 /* Record exit value of local session */
1716 success = 1;
1717 exit_status = exitval;
1718 } else {
Damien Miller36f57eb2010-01-30 17:28:34 +11001719 /* Probably for a mux channel that has already closed */
1720 debug("%s: no sink for exit-status on channel %d",
1721 __func__, id);
Damien Miller0e220db2004-06-15 10:34:08 +10001722 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001723 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001724 }
Damien Millerc5893782014-05-15 13:48:49 +10001725 if (reply && c != NULL && !(c->flags & CHAN_CLOSE_SENT)) {
djm@openbsd.org9f532292017-09-12 06:35:31 +00001726 if (!c->have_remote_id)
1727 fatal("%s: channel %d: no remote_id",
1728 __func__, c->self);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001729 packet_start(success ?
1730 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller8533c782008-12-08 09:54:40 +11001731 packet_put_int(c->remote_id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001732 packet_send();
1733 }
Darren Tuckera627d422013-06-02 07:31:17 +10001734 free(rtype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001735 return 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001736}
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001737
djm@openbsd.org523463a2015-02-16 22:13:32 +00001738struct hostkeys_update_ctx {
1739 /* The hostname and (optionally) IP address string for the server */
1740 char *host_str, *ip_str;
1741
1742 /*
1743 * Keys received from the server and a flag for each indicating
1744 * whether they already exist in known_hosts.
1745 * keys_seen is filled in by hostkeys_find() and later (for new
1746 * keys) by client_global_hostkeys_private_confirm().
1747 */
1748 struct sshkey **keys;
1749 int *keys_seen;
djm@openbsd.org05164352017-04-30 23:21:54 +00001750 size_t nkeys, nnew;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001751
1752 /*
1753 * Keys that are in known_hosts, but were not present in the update
1754 * from the server (i.e. scheduled to be deleted).
1755 * Filled in by hostkeys_find().
1756 */
1757 struct sshkey **old_keys;
1758 size_t nold;
1759};
1760
1761static void
1762hostkeys_update_ctx_free(struct hostkeys_update_ctx *ctx)
1763{
1764 size_t i;
1765
1766 if (ctx == NULL)
1767 return;
1768 for (i = 0; i < ctx->nkeys; i++)
1769 sshkey_free(ctx->keys[i]);
1770 free(ctx->keys);
1771 free(ctx->keys_seen);
1772 for (i = 0; i < ctx->nold; i++)
1773 sshkey_free(ctx->old_keys[i]);
1774 free(ctx->old_keys);
1775 free(ctx->host_str);
1776 free(ctx->ip_str);
1777 free(ctx);
1778}
1779
1780static int
1781hostkeys_find(struct hostkey_foreach_line *l, void *_ctx)
1782{
1783 struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
1784 size_t i;
1785 struct sshkey **tmp;
1786
djm@openbsd.org873d3e72017-04-30 23:18:44 +00001787 if (l->status != HKF_STATUS_MATCHED || l->key == NULL)
djm@openbsd.org523463a2015-02-16 22:13:32 +00001788 return 0;
1789
1790 /* Mark off keys we've already seen for this host */
1791 for (i = 0; i < ctx->nkeys; i++) {
1792 if (sshkey_equal(l->key, ctx->keys[i])) {
1793 debug3("%s: found %s key at %s:%ld", __func__,
1794 sshkey_ssh_name(ctx->keys[i]), l->path, l->linenum);
1795 ctx->keys_seen[i] = 1;
1796 return 0;
1797 }
1798 }
1799 /* This line contained a key that not offered by the server */
1800 debug3("%s: deprecated %s key at %s:%ld", __func__,
1801 sshkey_ssh_name(l->key), l->path, l->linenum);
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001802 if ((tmp = recallocarray(ctx->old_keys, ctx->nold, ctx->nold + 1,
djm@openbsd.org523463a2015-02-16 22:13:32 +00001803 sizeof(*ctx->old_keys))) == NULL)
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001804 fatal("%s: recallocarray failed nold = %zu",
djm@openbsd.org523463a2015-02-16 22:13:32 +00001805 __func__, ctx->nold);
1806 ctx->old_keys = tmp;
1807 ctx->old_keys[ctx->nold++] = l->key;
1808 l->key = NULL;
1809
1810 return 0;
1811}
1812
1813static void
1814update_known_hosts(struct hostkeys_update_ctx *ctx)
1815{
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00001816 int r, was_raw = 0;
1817 int loglevel = options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK ?
djm@openbsd.org523463a2015-02-16 22:13:32 +00001818 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_VERBOSE;
1819 char *fp, *response;
1820 size_t i;
1821
1822 for (i = 0; i < ctx->nkeys; i++) {
1823 if (ctx->keys_seen[i] != 2)
1824 continue;
1825 if ((fp = sshkey_fingerprint(ctx->keys[i],
1826 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1827 fatal("%s: sshkey_fingerprint failed", __func__);
1828 do_log2(loglevel, "Learned new hostkey: %s %s",
1829 sshkey_type(ctx->keys[i]), fp);
1830 free(fp);
1831 }
1832 for (i = 0; i < ctx->nold; i++) {
1833 if ((fp = sshkey_fingerprint(ctx->old_keys[i],
1834 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1835 fatal("%s: sshkey_fingerprint failed", __func__);
1836 do_log2(loglevel, "Deprecating obsolete hostkey: %s %s",
1837 sshkey_type(ctx->old_keys[i]), fp);
1838 free(fp);
1839 }
1840 if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK) {
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00001841 if (get_saved_tio() != NULL) {
1842 leave_raw_mode(1);
1843 was_raw = 1;
1844 }
djm@openbsd.org523463a2015-02-16 22:13:32 +00001845 response = NULL;
1846 for (i = 0; !quit_pending && i < 3; i++) {
1847 free(response);
1848 response = read_passphrase("Accept updated hostkeys? "
1849 "(yes/no): ", RP_ECHO);
1850 if (strcasecmp(response, "yes") == 0)
1851 break;
1852 else if (quit_pending || response == NULL ||
1853 strcasecmp(response, "no") == 0) {
1854 options.update_hostkeys = 0;
1855 break;
1856 } else {
1857 do_log2(loglevel, "Please enter "
1858 "\"yes\" or \"no\"");
1859 }
1860 }
1861 if (quit_pending || i >= 3 || response == NULL)
1862 options.update_hostkeys = 0;
1863 free(response);
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00001864 if (was_raw)
1865 enter_raw_mode(1);
djm@openbsd.org523463a2015-02-16 22:13:32 +00001866 }
1867
1868 /*
1869 * Now that all the keys are verified, we can go ahead and replace
1870 * them in known_hosts (assuming SSH_UPDATE_HOSTKEYS_ASK didn't
1871 * cancel the operation).
1872 */
1873 if (options.update_hostkeys != 0 &&
1874 (r = hostfile_replace_entries(options.user_hostfiles[0],
1875 ctx->host_str, ctx->ip_str, ctx->keys, ctx->nkeys,
1876 options.hash_known_hosts, 0,
1877 options.fingerprint_hash)) != 0)
1878 error("%s: hostfile_replace_entries failed: %s",
1879 __func__, ssh_err(r));
1880}
1881
1882static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001883client_global_hostkeys_private_confirm(struct ssh *ssh, int type,
1884 u_int32_t seq, void *_ctx)
djm@openbsd.org523463a2015-02-16 22:13:32 +00001885{
djm@openbsd.org523463a2015-02-16 22:13:32 +00001886 struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
1887 size_t i, ndone;
1888 struct sshbuf *signdata;
djm@openbsd.org78607312017-12-18 23:16:23 +00001889 int r, kexsigtype, use_kexsigtype;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001890 const u_char *sig;
1891 size_t siglen;
1892
1893 if (ctx->nnew == 0)
1894 fatal("%s: ctx->nnew == 0", __func__); /* sanity */
1895 if (type != SSH2_MSG_REQUEST_SUCCESS) {
1896 error("Server failed to confirm ownership of "
1897 "private host keys");
1898 hostkeys_update_ctx_free(ctx);
1899 return;
1900 }
djm@openbsd.org78607312017-12-18 23:16:23 +00001901 kexsigtype = sshkey_type_plain(
1902 sshkey_type_from_name(ssh->kex->hostkey_alg));
1903
djm@openbsd.org523463a2015-02-16 22:13:32 +00001904 if ((signdata = sshbuf_new()) == NULL)
1905 fatal("%s: sshbuf_new failed", __func__);
1906 /* Don't want to accidentally accept an unbound signature */
1907 if (ssh->kex->session_id_len == 0)
1908 fatal("%s: ssh->kex->session_id_len == 0", __func__);
1909 /*
1910 * Expect a signature for each of the ctx->nnew private keys we
1911 * haven't seen before. They will be in the same order as the
1912 * ctx->keys where the corresponding ctx->keys_seen[i] == 0.
1913 */
1914 for (ndone = i = 0; i < ctx->nkeys; i++) {
1915 if (ctx->keys_seen[i])
1916 continue;
1917 /* Prepare data to be signed: session ID, unique string, key */
1918 sshbuf_reset(signdata);
djm@openbsd.org44732de2015-02-20 22:17:21 +00001919 if ( (r = sshbuf_put_cstring(signdata,
1920 "hostkeys-prove-00@openssh.com")) != 0 ||
1921 (r = sshbuf_put_string(signdata, ssh->kex->session_id,
djm@openbsd.org523463a2015-02-16 22:13:32 +00001922 ssh->kex->session_id_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +00001923 (r = sshkey_puts(ctx->keys[i], signdata)) != 0)
1924 fatal("%s: failed to prepare signature: %s",
1925 __func__, ssh_err(r));
1926 /* Extract and verify signature */
1927 if ((r = sshpkt_get_string_direct(ssh, &sig, &siglen)) != 0) {
1928 error("%s: couldn't parse message: %s",
1929 __func__, ssh_err(r));
1930 goto out;
1931 }
djm@openbsd.org78607312017-12-18 23:16:23 +00001932 /*
1933 * For RSA keys, prefer to use the signature type negotiated
1934 * during KEX to the default (SHA1).
1935 */
1936 use_kexsigtype = kexsigtype == KEY_RSA &&
1937 sshkey_type_plain(ctx->keys[i]->type) == KEY_RSA;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001938 if ((r = sshkey_verify(ctx->keys[i], sig, siglen,
djm@openbsd.org04c7e282017-12-18 02:25:15 +00001939 sshbuf_ptr(signdata), sshbuf_len(signdata),
djm@openbsd.org78607312017-12-18 23:16:23 +00001940 use_kexsigtype ? ssh->kex->hostkey_alg : NULL, 0)) != 0) {
djm@openbsd.org523463a2015-02-16 22:13:32 +00001941 error("%s: server gave bad signature for %s key %zu",
1942 __func__, sshkey_type(ctx->keys[i]), i);
1943 goto out;
1944 }
1945 /* Key is good. Mark it as 'seen' */
1946 ctx->keys_seen[i] = 2;
1947 ndone++;
1948 }
1949 if (ndone != ctx->nnew)
1950 fatal("%s: ndone != ctx->nnew (%zu / %zu)", __func__,
1951 ndone, ctx->nnew); /* Shouldn't happen */
1952 ssh_packet_check_eom(ssh);
1953
1954 /* Make the edits to known_hosts */
1955 update_known_hosts(ctx);
1956 out:
1957 hostkeys_update_ctx_free(ctx);
1958}
1959
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001960/*
djm@openbsd.org894221a2017-03-10 05:01:13 +00001961 * Returns non-zero if the key is accepted by HostkeyAlgorithms.
1962 * Made slightly less trivial by the multiple RSA signature algorithm names.
1963 */
1964static int
1965key_accepted_by_hostkeyalgs(const struct sshkey *key)
1966{
1967 const char *ktype = sshkey_ssh_name(key);
1968 const char *hostkeyalgs = options.hostkeyalgorithms != NULL ?
1969 options.hostkeyalgorithms : KEX_DEFAULT_PK_ALG;
1970
1971 if (key == NULL || key->type == KEY_UNSPEC)
1972 return 0;
1973 if (key->type == KEY_RSA &&
1974 (match_pattern_list("rsa-sha2-256", hostkeyalgs, 0) == 1 ||
1975 match_pattern_list("rsa-sha2-512", hostkeyalgs, 0) == 1))
1976 return 1;
1977 return match_pattern_list(ktype, hostkeyalgs, 0) == 1;
1978}
1979
1980/*
djm@openbsd.org44732de2015-02-20 22:17:21 +00001981 * Handle hostkeys-00@openssh.com global request to inform the client of all
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001982 * the server's hostkeys. The keys are checked against the user's
1983 * HostkeyAlgorithms preference before they are accepted.
1984 */
1985static int
1986client_input_hostkeys(void)
1987{
djm@openbsd.org523463a2015-02-16 22:13:32 +00001988 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001989 const u_char *blob = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001990 size_t i, len = 0;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001991 struct sshbuf *buf = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00001992 struct sshkey *key = NULL, **tmp;
1993 int r;
1994 char *fp;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001995 static int hostkeys_seen = 0; /* XXX use struct ssh */
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00001996 extern struct sockaddr_storage hostaddr; /* XXX from ssh.c */
djm@openbsd.orga63cfa22015-02-25 19:54:02 +00001997 struct hostkeys_update_ctx *ctx = NULL;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001998
1999 if (hostkeys_seen)
2000 fatal("%s: server already sent hostkeys", __func__);
djm@openbsd.org523463a2015-02-16 22:13:32 +00002001 if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK &&
2002 options.batch_mode)
2003 return 1; /* won't ask in batchmode, so don't even try */
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002004 if (!options.update_hostkeys || options.num_user_hostfiles <= 0)
2005 return 1;
djm@openbsd.orga63cfa22015-02-25 19:54:02 +00002006
2007 ctx = xcalloc(1, sizeof(*ctx));
djm@openbsd.org523463a2015-02-16 22:13:32 +00002008 while (ssh_packet_remaining(ssh) > 0) {
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002009 sshkey_free(key);
2010 key = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002011 if ((r = sshpkt_get_string_direct(ssh, &blob, &len)) != 0) {
2012 error("%s: couldn't parse message: %s",
2013 __func__, ssh_err(r));
2014 goto out;
2015 }
djm@openbsd.org44732de2015-02-20 22:17:21 +00002016 if ((r = sshkey_from_blob(blob, len, &key)) != 0) {
2017 error("%s: parse key: %s", __func__, ssh_err(r));
2018 goto out;
2019 }
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002020 fp = sshkey_fingerprint(key, options.fingerprint_hash,
2021 SSH_FP_DEFAULT);
2022 debug3("%s: received %s key %s", __func__,
2023 sshkey_type(key), fp);
2024 free(fp);
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00002025
djm@openbsd.org894221a2017-03-10 05:01:13 +00002026 if (!key_accepted_by_hostkeyalgs(key)) {
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002027 debug3("%s: %s key not permitted by HostkeyAlgorithms",
2028 __func__, sshkey_ssh_name(key));
2029 continue;
2030 }
djm@openbsd.org523463a2015-02-16 22:13:32 +00002031 /* Skip certs */
2032 if (sshkey_is_cert(key)) {
2033 debug3("%s: %s key is a certificate; skipping",
2034 __func__, sshkey_ssh_name(key));
2035 continue;
2036 }
2037 /* Ensure keys are unique */
2038 for (i = 0; i < ctx->nkeys; i++) {
2039 if (sshkey_equal(key, ctx->keys[i])) {
2040 error("%s: received duplicated %s host key",
2041 __func__, sshkey_ssh_name(key));
2042 goto out;
2043 }
2044 }
2045 /* Key is good, record it */
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00002046 if ((tmp = recallocarray(ctx->keys, ctx->nkeys, ctx->nkeys + 1,
djm@openbsd.org523463a2015-02-16 22:13:32 +00002047 sizeof(*ctx->keys))) == NULL)
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00002048 fatal("%s: recallocarray failed nkeys = %zu",
djm@openbsd.org523463a2015-02-16 22:13:32 +00002049 __func__, ctx->nkeys);
2050 ctx->keys = tmp;
2051 ctx->keys[ctx->nkeys++] = key;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002052 key = NULL;
2053 }
2054
djm@openbsd.org523463a2015-02-16 22:13:32 +00002055 if (ctx->nkeys == 0) {
djm@openbsd.org44732de2015-02-20 22:17:21 +00002056 debug("%s: server sent no hostkeys", __func__);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002057 goto out;
2058 }
djm@openbsd.org44732de2015-02-20 22:17:21 +00002059
djm@openbsd.org523463a2015-02-16 22:13:32 +00002060 if ((ctx->keys_seen = calloc(ctx->nkeys,
2061 sizeof(*ctx->keys_seen))) == NULL)
2062 fatal("%s: calloc failed", __func__);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002063
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00002064 get_hostfile_hostname_ipaddr(host,
2065 options.check_host_ip ? (struct sockaddr *)&hostaddr : NULL,
djm@openbsd.org523463a2015-02-16 22:13:32 +00002066 options.port, &ctx->host_str,
2067 options.check_host_ip ? &ctx->ip_str : NULL);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002068
djm@openbsd.org523463a2015-02-16 22:13:32 +00002069 /* Find which keys we already know about. */
2070 if ((r = hostkeys_foreach(options.user_hostfiles[0], hostkeys_find,
2071 ctx, ctx->host_str, ctx->ip_str,
2072 HKF_WANT_PARSE_KEY|HKF_WANT_MATCH)) != 0) {
2073 error("%s: hostkeys_foreach failed: %s", __func__, ssh_err(r));
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002074 goto out;
2075 }
2076
djm@openbsd.org523463a2015-02-16 22:13:32 +00002077 /* Figure out if we have any new keys to add */
2078 ctx->nnew = 0;
2079 for (i = 0; i < ctx->nkeys; i++) {
2080 if (!ctx->keys_seen[i])
2081 ctx->nnew++;
2082 }
2083
2084 debug3("%s: %zu keys from server: %zu new, %zu retained. %zu to remove",
2085 __func__, ctx->nkeys, ctx->nnew, ctx->nkeys - ctx->nnew, ctx->nold);
2086
2087 if (ctx->nnew == 0 && ctx->nold != 0) {
2088 /* We have some keys to remove. Just do it. */
2089 update_known_hosts(ctx);
2090 } else if (ctx->nnew != 0) {
2091 /*
2092 * We have received hitherto-unseen keys from the server.
2093 * Ask the server to confirm ownership of the private halves.
2094 */
2095 debug3("%s: asking server to prove ownership for %zu keys",
2096 __func__, ctx->nnew);
2097 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
2098 (r = sshpkt_put_cstring(ssh,
djm@openbsd.org44732de2015-02-20 22:17:21 +00002099 "hostkeys-prove-00@openssh.com")) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +00002100 (r = sshpkt_put_u8(ssh, 1)) != 0) /* bool: want reply */
2101 fatal("%s: cannot prepare packet: %s",
2102 __func__, ssh_err(r));
2103 if ((buf = sshbuf_new()) == NULL)
2104 fatal("%s: sshbuf_new", __func__);
2105 for (i = 0; i < ctx->nkeys; i++) {
2106 if (ctx->keys_seen[i])
2107 continue;
2108 sshbuf_reset(buf);
2109 if ((r = sshkey_putb(ctx->keys[i], buf)) != 0)
2110 fatal("%s: sshkey_putb: %s",
2111 __func__, ssh_err(r));
2112 if ((r = sshpkt_put_stringb(ssh, buf)) != 0)
2113 fatal("%s: sshpkt_put_string: %s",
2114 __func__, ssh_err(r));
2115 }
2116 if ((r = sshpkt_send(ssh)) != 0)
2117 fatal("%s: sshpkt_send: %s", __func__, ssh_err(r));
2118 client_register_global_confirm(
2119 client_global_hostkeys_private_confirm, ctx);
2120 ctx = NULL; /* will be freed in callback */
2121 }
2122
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002123 /* Success */
2124 out:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002125 hostkeys_update_ctx_free(ctx);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002126 sshkey_free(key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002127 sshbuf_free(buf);
djm@openbsd.org523463a2015-02-16 22:13:32 +00002128 /*
2129 * NB. Return success for all cases. The server doesn't need to know
2130 * what the client does with its hosts file.
2131 */
2132 return 1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002133}
2134
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002135static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002136client_input_global_request(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerc3fa4072002-01-22 23:21:58 +11002137{
2138 char *rtype;
2139 int want_reply;
2140 int success = 0;
2141
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002142 rtype = packet_get_cstring(NULL);
Damien Millerc3fa4072002-01-22 23:21:58 +11002143 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11002144 debug("client_input_global_request: rtype %s want_reply %d",
2145 rtype, want_reply);
djm@openbsd.org44732de2015-02-20 22:17:21 +00002146 if (strcmp(rtype, "hostkeys-00@openssh.com") == 0)
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002147 success = client_input_hostkeys();
Damien Millerc3fa4072002-01-22 23:21:58 +11002148 if (want_reply) {
2149 packet_start(success ?
2150 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
2151 packet_send();
2152 packet_write_wait();
2153 }
Darren Tuckera627d422013-06-02 07:31:17 +10002154 free(rtype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002155 return 0;
Damien Millerc3fa4072002-01-22 23:21:58 +11002156}
Damien Millerbd483e72000-04-30 10:00:53 +10002157
Damien Miller0e220db2004-06-15 10:34:08 +10002158void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002159client_session2_setup(struct ssh *ssh, int id, int want_tty, int want_subsystem,
Damien Miller5771ed72008-05-19 15:35:33 +10002160 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env)
Damien Miller0e220db2004-06-15 10:34:08 +10002161{
2162 int len;
Darren Tucker5d78de62004-11-05 20:35:44 +11002163 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002164
2165 debug2("%s: id %d", __func__, id);
2166
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002167 if ((c = channel_lookup(ssh, id)) == NULL)
2168 fatal("%s: channel %d: unknown channel", __func__, id);
Darren Tucker5d78de62004-11-05 20:35:44 +11002169
Damien Miller0dac6fb2010-11-20 15:19:38 +11002170 packet_set_interactive(want_tty,
2171 options.ip_qos_interactive, options.ip_qos_bulk);
2172
Damien Miller0e220db2004-06-15 10:34:08 +10002173 if (want_tty) {
2174 struct winsize ws;
Damien Miller0e220db2004-06-15 10:34:08 +10002175
2176 /* Store window size in the packet. */
2177 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
2178 memset(&ws, 0, sizeof(ws));
2179
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002180 channel_request_start(ssh, id, "pty-req", 1);
2181 client_expect_confirm(ssh, id, "PTY allocation", CONFIRM_TTY);
Damien Miller0e220db2004-06-15 10:34:08 +10002182 packet_put_cstring(term != NULL ? term : "");
Damien Miller71a73672006-03-26 14:04:36 +11002183 packet_put_int((u_int)ws.ws_col);
2184 packet_put_int((u_int)ws.ws_row);
2185 packet_put_int((u_int)ws.ws_xpixel);
2186 packet_put_int((u_int)ws.ws_ypixel);
Darren Tuckerdf189fb2008-06-08 12:55:32 +10002187 if (tiop == NULL)
2188 tiop = get_saved_tio();
2189 tty_make_modes(-1, tiop);
Damien Miller0e220db2004-06-15 10:34:08 +10002190 packet_send();
2191 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11002192 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10002193 }
2194
2195 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10002196 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10002197 int i, j, matched;
Damien Miller0e220db2004-06-15 10:34:08 +10002198 char *name, *val;
2199
2200 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10002201 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10002202 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10002203 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10002204 if ((val = strchr(name, '=')) == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +10002205 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002206 continue;
2207 }
2208 *val++ = '\0';
2209
2210 matched = 0;
2211 for (j = 0; j < options.num_send_env; j++) {
2212 if (match_pattern(name, options.send_env[j])) {
2213 matched = 1;
2214 break;
2215 }
2216 }
2217 if (!matched) {
2218 debug3("Ignored env %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10002219 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002220 continue;
2221 }
2222
2223 debug("Sending env %s = %s", name, val);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002224 channel_request_start(ssh, id, "env", 0);
Damien Miller0e220db2004-06-15 10:34:08 +10002225 packet_put_cstring(name);
2226 packet_put_cstring(val);
2227 packet_send();
Darren Tuckera627d422013-06-02 07:31:17 +10002228 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002229 }
2230 }
2231
2232 len = buffer_len(cmd);
2233 if (len > 0) {
2234 if (len > 900)
2235 len = 900;
2236 if (want_subsystem) {
Damien Miller5771ed72008-05-19 15:35:33 +10002237 debug("Sending subsystem: %.*s",
2238 len, (u_char*)buffer_ptr(cmd));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002239 channel_request_start(ssh, id, "subsystem", 1);
2240 client_expect_confirm(ssh, id, "subsystem",
2241 CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002242 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002243 debug("Sending command: %.*s",
2244 len, (u_char*)buffer_ptr(cmd));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002245 channel_request_start(ssh, id, "exec", 1);
2246 client_expect_confirm(ssh, id, "exec", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002247 }
2248 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
2249 packet_send();
2250 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002251 channel_request_start(ssh, id, "shell", 1);
2252 client_expect_confirm(ssh, id, "shell", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002253 packet_send();
2254 }
2255}
2256
Ben Lindstrombba81212001-06-25 05:01:22 +00002257static void
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002258client_init_dispatch(void)
Damien Miller1383bd82000-04-06 12:32:37 +10002259{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002260 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10002261
Damien Miller1383bd82000-04-06 12:32:37 +10002262 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2263 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2264 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2265 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10002266 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10002267 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2268 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002269 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10002270 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerb84886b2008-05-19 15:05:07 +10002271 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2272 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
Damien Millerc3fa4072002-01-22 23:21:58 +11002273 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002274
2275 /* rekeying */
2276 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10002277
2278 /* global request reply messages */
2279 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2280 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10002281}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002282
Damien Miller6c3eec72011-05-05 14:16:22 +10002283void
2284client_stop_mux(void)
2285{
2286 if (options.control_path != NULL && muxserver_sock != -1)
2287 unlink(options.control_path);
2288 /*
Darren Tucker48bf4b02012-09-07 16:38:53 +10002289 * If we are in persist mode, or don't have a shell, signal that we
2290 * should close when all active channels are closed.
Damien Miller6c3eec72011-05-05 14:16:22 +10002291 */
Darren Tucker48bf4b02012-09-07 16:38:53 +10002292 if (options.control_persist || no_shell_flag) {
Damien Miller6c3eec72011-05-05 14:16:22 +10002293 session_closed = 1;
Damien Miller4ac99c32011-06-20 14:43:31 +10002294 setproctitle("[stopped mux]");
2295 }
Damien Miller6c3eec72011-05-05 14:16:22 +10002296}
2297
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002298/* client specific fatal cleanup */
2299void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002300cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002301{
Damien Miller21771e22011-05-15 08:45:50 +10002302 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002303 leave_non_blocking();
Damien Millerb1cbfa22008-05-19 16:00:08 +10002304 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10002305 unlink(options.control_path);
Damien Millera41ccca2010-10-07 22:07:32 +11002306 ssh_kill_proxy_command();
Darren Tucker3e33cec2003-10-02 16:12:36 +10002307 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002308}