blob: 87ceb3dab9e6c553a2c6f628e57813645e292277 [file] [log] [blame]
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001/* $OpenBSD: clientloop.c,v 1.275 2015/07/10 06:21:53 markus Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * The main loop for the interactive session (client side).
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
14 *
15 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 *
Damien Miller1383bd82000-04-06 12:32:37 +100038 * SSH2 support added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000039 * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110040 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110060 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
62#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110063
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000064#include <sys/param.h> /* MIN MAX */
Damien Millerf17883e2006-03-15 11:45:54 +110065#include <sys/types.h>
Damien Miller9aec9192006-08-05 10:57:45 +100066#include <sys/ioctl.h>
Damien Millerf17883e2006-03-15 11:45:54 +110067#ifdef HAVE_SYS_STAT_H
68# include <sys/stat.h>
69#endif
Damien Miller9aec9192006-08-05 10:57:45 +100070#ifdef HAVE_SYS_TIME_H
71# include <sys/time.h>
72#endif
Damien Millere3b60b52006-07-10 21:08:03 +100073#include <sys/socket.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110074
Damien Millerc7b06362006-03-15 11:53:45 +110075#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100076#include <errno.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110077#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110078#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110079#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110080#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100081#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100082#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100083#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100084#include <string.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110085#include <termios.h>
Damien Millerd7834352006-08-05 12:39:39 +100086#include <pwd.h>
Damien Millere6b3b612006-07-24 14:01:23 +100087#include <unistd.h>
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000088#include <limits.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
Damien Millerb84886b2008-05-19 15:05:07 +100090#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100091#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000093#include "ssh1.h"
94#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095#include "packet.h"
96#include "buffer.h"
Damien Millerb38eff82000-04-01 11:09:21 +100097#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000098#include "channels.h"
Damien Millerb38eff82000-04-01 11:09:21 +100099#include "dispatch.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +1100100#include "key.h"
Damien Millerd7834352006-08-05 12:39:39 +1000101#include "cipher.h"
Ben Lindstromf28f6342001-04-04 02:03:04 +0000102#include "kex.h"
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000103#include "myproposal.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000104#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +1000105#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000106#include "readconf.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000107#include "clientloop.h"
Damien Milleraeb31d62005-12-13 19:29:36 +1100108#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000109#include "authfd.h"
110#include "atomicio.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +1000111#include "sshpty.h"
Damien Miller0e220db2004-06-15 10:34:08 +1000112#include "match.h"
113#include "msg.h"
Darren Tuckerc5564e12009-06-21 18:53:53 +1000114#include "roaming.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +0000115#include "ssherr.h"
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000116#include "hostfile.h"
Damien Miller69b69aa2000-10-28 14:19:58 +1100117
118/* import options */
119extern Options options;
120
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121/* Flag indicating that stdin should be redirected from /dev/null. */
122extern int stdin_null_flag;
123
Damien Millerd6965512003-12-17 16:31:53 +1100124/* Flag indicating that no shell has been requested */
125extern int no_shell_flag;
126
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. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000154static int escape_char1; /* Escape character. (proto1 only) */
155static int escape_pending1; /* Last character was an escape (proto1 only) */
Damien Miller95def091999-11-25 00:26:21 +1100156static int last_was_cr; /* Last character was a newline. */
Darren Tucker4d5cd332008-06-13 04:51:14 +1000157static int exit_status; /* Used to store the command exit status. */
158static int stdin_eof; /* EOF has been encountered on stderr. */
Damien Miller95def091999-11-25 00:26:21 +1100159static Buffer stdin_buffer; /* Buffer for stdin data. */
160static Buffer stdout_buffer; /* Buffer for stdout data. */
161static Buffer stderr_buffer; /* Buffer for stderr data. */
Damien Miller1ab6a512010-06-26 10:02:24 +1000162static u_int buffer_high; /* Soft max buffer size. */
Damien Miller95def091999-11-25 00:26:21 +1100163static int connection_in; /* Connection to server (input). */
164static int connection_out; /* Connection to server (output). */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000165static int need_rekeying; /* Set to non-zero if rekeying is requested. */
Damien Miller1ab6a512010-06-26 10:02:24 +1000166static int session_closed; /* In SSH2: login session closed. */
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000167static u_int x11_refuse_time; /* If >0, refuse x11 opens after this time. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168
Ben Lindstrombba81212001-06-25 05:01:22 +0000169static void client_init_dispatch(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000170int session_ident = -1;
171
Darren Tuckere32cf432010-01-08 16:51:40 +1100172int session_resumed = 0;
173
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000174/* Track escape per proto2 channel */
175struct escape_filter_ctx {
176 int escape_pending;
177 int escape_char;
178};
179
180/* Context for channel confirmation replies */
Damien Miller5771ed72008-05-19 15:35:33 +1000181struct channel_reply_ctx {
182 const char *request_type;
Damien Miller555f3b82011-05-15 08:48:05 +1000183 int id;
184 enum confirm_action action;
Damien Miller5771ed72008-05-19 15:35:33 +1000185};
186
Darren Tucker9f407c42008-06-13 04:50:27 +1000187/* Global request success/failure callbacks */
188struct global_confirm {
189 TAILQ_ENTRY(global_confirm) entry;
190 global_confirm_cb *cb;
191 void *ctx;
192 int ref_count;
193};
194TAILQ_HEAD(global_confirms, global_confirm);
195static struct global_confirms global_confirms =
196 TAILQ_HEAD_INITIALIZER(global_confirms);
197
Damien Miller0e220db2004-06-15 10:34:08 +1000198void ssh_process_session2_setup(int, int, int, Buffer *);
199
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200/* Restores stdin to blocking mode. */
201
Ben Lindstrombba81212001-06-25 05:01:22 +0000202static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000203leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204{
Damien Miller95def091999-11-25 00:26:21 +1100205 if (in_non_blocking_mode) {
Damien Miller03e66f62004-06-15 15:47:51 +1000206 unset_nonblock(fileno(stdin));
Damien Miller95def091999-11-25 00:26:21 +1100207 in_non_blocking_mode = 0;
Damien Miller95def091999-11-25 00:26:21 +1100208 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209}
210
Damien Miller95def091999-11-25 00:26:21 +1100211/* Puts stdin terminal in non-blocking mode. */
212
Ben Lindstrombba81212001-06-25 05:01:22 +0000213static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000214enter_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215{
Damien Miller95def091999-11-25 00:26:21 +1100216 in_non_blocking_mode = 1;
Damien Miller232711f2004-06-15 10:35:30 +1000217 set_nonblock(fileno(stdin));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000218}
219
Damien Miller5428f641999-11-25 11:54:57 +1100220/*
221 * Signal handler for the window change signal (SIGWINCH). This just sets a
222 * flag indicating that the window has changed.
223 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100224/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000225static void
Damien Miller95def091999-11-25 00:26:21 +1100226window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000227{
Damien Miller95def091999-11-25 00:26:21 +1100228 received_window_change_signal = 1;
229 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230}
231
Damien Miller5428f641999-11-25 11:54:57 +1100232/*
233 * Signal handler for signals that cause the program to terminate. These
234 * signals must be trapped to restore terminal modes.
235 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100236/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000237static void
Damien Miller95def091999-11-25 00:26:21 +1100238signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000240 received_signal = sig;
241 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242}
243
Damien Miller5428f641999-11-25 11:54:57 +1100244/*
245 * Returns current time in seconds from Jan 1, 1970 with the maximum
246 * available resolution.
247 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248
Ben Lindstrombba81212001-06-25 05:01:22 +0000249static double
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000250get_current_time(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251{
Damien Miller95def091999-11-25 00:26:21 +1100252 struct timeval tv;
253 gettimeofday(&tv, NULL);
254 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000255}
256
Damien Millere11e1ea2010-08-03 16:04:46 +1000257/*
258 * Sets control_persist_exit_time to the absolute time when the
259 * backgrounded control master should exit due to expiry of the
260 * ControlPersist timeout. Sets it to 0 if we are not a backgrounded
261 * control master process, or if there is no ControlPersist timeout.
262 */
263static void
264set_control_persist_exit_time(void)
265{
266 if (muxserver_sock == -1 || !options.control_persist
Damien Miller6c3eec72011-05-05 14:16:22 +1000267 || options.control_persist_timeout == 0) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000268 /* not using a ControlPersist timeout */
269 control_persist_exit_time = 0;
Damien Miller6c3eec72011-05-05 14:16:22 +1000270 } else if (channel_still_open()) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000271 /* some client connections are still open */
272 if (control_persist_exit_time > 0)
273 debug2("%s: cancel scheduled exit", __func__);
274 control_persist_exit_time = 0;
275 } else if (control_persist_exit_time <= 0) {
276 /* a client connection has recently closed */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000277 control_persist_exit_time = monotime() +
Damien Millere11e1ea2010-08-03 16:04:46 +1000278 (time_t)options.control_persist_timeout;
279 debug2("%s: schedule exit in %d seconds", __func__,
280 options.control_persist_timeout);
281 }
282 /* else we are already counting down to the timeout */
283}
284
Damien Miller8d60be52012-02-11 08:18:17 +1100285#define SSH_X11_VALID_DISPLAY_CHARS ":/.-_"
286static int
287client_x11_display_valid(const char *display)
288{
289 size_t i, dlen;
290
291 dlen = strlen(display);
292 for (i = 0; i < dlen; i++) {
Damien Millerfdb23062013-11-21 13:57:15 +1100293 if (!isalnum((u_char)display[i]) &&
Damien Miller8d60be52012-02-11 08:18:17 +1100294 strchr(SSH_X11_VALID_DISPLAY_CHARS, display[i]) == NULL) {
295 debug("Invalid character '%c' in DISPLAY", display[i]);
296 return 0;
297 }
298 }
299 return 1;
300}
301
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000302#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
303#define X11_TIMEOUT_SLACK 60
Damien Miller17e7ed02005-06-17 12:54:33 +1000304void
305client_x11_get_proto(const char *display, const char *xauth_path,
Damien Miller1ab6a512010-06-26 10:02:24 +1000306 u_int trusted, u_int timeout, char **_proto, char **_data)
Damien Miller17e7ed02005-06-17 12:54:33 +1000307{
308 char cmd[1024];
309 char line[512];
310 char xdisplay[512];
311 static char proto[512], data[512];
312 FILE *f;
313 int got_data = 0, generated = 0, do_unlink = 0, i;
314 char *xauthdir, *xauthfile;
315 struct stat st;
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000316 u_int now, x11_timeout_real;
Damien Miller17e7ed02005-06-17 12:54:33 +1000317
318 xauthdir = xauthfile = NULL;
319 *_proto = proto;
320 *_data = data;
321 proto[0] = data[0] = '\0';
322
323 if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) {
324 debug("No xauth program.");
Damien Miller8d60be52012-02-11 08:18:17 +1100325 } else if (!client_x11_display_valid(display)) {
326 logit("DISPLAY '%s' invalid, falling back to fake xauth data",
327 display);
Damien Miller17e7ed02005-06-17 12:54:33 +1000328 } else {
329 if (display == NULL) {
330 debug("x11_get_proto: DISPLAY not set");
331 return;
332 }
333 /*
334 * Handle FamilyLocal case where $DISPLAY does
335 * not match an authorization entry. For this we
336 * just try "xauth list unix:displaynum.screennum".
337 * XXX: "localhost" match to determine FamilyLocal
338 * is not perfect.
339 */
340 if (strncmp(display, "localhost:", 10) == 0) {
341 snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
342 display + 10);
343 display = xdisplay;
344 }
345 if (trusted == 0) {
deraadt@openbsd.org087266e2015-01-20 23:14:00 +0000346 xauthdir = xmalloc(PATH_MAX);
347 xauthfile = xmalloc(PATH_MAX);
348 mktemp_proto(xauthdir, PATH_MAX);
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000349 /*
350 * The authentication cookie should briefly outlive
351 * ssh's willingness to forward X11 connections to
352 * avoid nasty fail-open behaviour in the X server.
353 */
354 if (timeout >= UINT_MAX - X11_TIMEOUT_SLACK)
355 x11_timeout_real = UINT_MAX;
356 else
357 x11_timeout_real = timeout + X11_TIMEOUT_SLACK;
Damien Miller17e7ed02005-06-17 12:54:33 +1000358 if (mkdtemp(xauthdir) != NULL) {
359 do_unlink = 1;
deraadt@openbsd.org087266e2015-01-20 23:14:00 +0000360 snprintf(xauthfile, PATH_MAX, "%s/xauthfile",
Damien Miller17e7ed02005-06-17 12:54:33 +1000361 xauthdir);
362 snprintf(cmd, sizeof(cmd),
363 "%s -f %s generate %s " SSH_X11_PROTO
Damien Miller1ab6a512010-06-26 10:02:24 +1000364 " untrusted timeout %u 2>" _PATH_DEVNULL,
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000365 xauth_path, xauthfile, display,
366 x11_timeout_real);
Damien Miller17e7ed02005-06-17 12:54:33 +1000367 debug2("x11_get_proto: %s", cmd);
Damien Miller1ab6a512010-06-26 10:02:24 +1000368 if (x11_refuse_time == 0) {
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000369 now = monotime() + 1;
Damien Miller1ab6a512010-06-26 10:02:24 +1000370 if (UINT_MAX - timeout < now)
371 x11_refuse_time = UINT_MAX;
372 else
373 x11_refuse_time = now + timeout;
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000374 channel_set_x11_refuse_time(
375 x11_refuse_time);
Damien Miller1ab6a512010-06-26 10:02:24 +1000376 }
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000377 if (system(cmd) == 0)
378 generated = 1;
Damien Miller17e7ed02005-06-17 12:54:33 +1000379 }
380 }
Darren Tucker513d13a2007-08-15 19:13:41 +1000381
382 /*
383 * When in untrusted mode, we read the cookie only if it was
384 * successfully generated as an untrusted one in the step
385 * above.
386 */
387 if (trusted || generated) {
388 snprintf(cmd, sizeof(cmd),
389 "%s %s%s list %s 2>" _PATH_DEVNULL,
390 xauth_path,
391 generated ? "-f " : "" ,
392 generated ? xauthfile : "",
393 display);
394 debug2("x11_get_proto: %s", cmd);
395 f = popen(cmd, "r");
396 if (f && fgets(line, sizeof(line), f) &&
397 sscanf(line, "%*s %511s %511s", proto, data) == 2)
398 got_data = 1;
399 if (f)
400 pclose(f);
401 } else
402 error("Warning: untrusted X11 forwarding setup failed: "
403 "xauth key data not generated");
Damien Miller17e7ed02005-06-17 12:54:33 +1000404 }
405
406 if (do_unlink) {
407 unlink(xauthfile);
408 rmdir(xauthdir);
409 }
Darren Tuckera627d422013-06-02 07:31:17 +1000410 free(xauthdir);
411 free(xauthfile);
Damien Miller17e7ed02005-06-17 12:54:33 +1000412
413 /*
414 * If we didn't get authentication data, just make up some
415 * data. The forwarding code will check the validity of the
416 * response anyway, and substitute this data. The X11
417 * server, however, will ignore this fake data and use
418 * whatever authentication mechanisms it was using otherwise
419 * for the local connection.
420 */
421 if (!got_data) {
422 u_int32_t rnd = 0;
423
424 logit("Warning: No xauth data; "
425 "using fake authentication data for X11 forwarding.");
426 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
427 for (i = 0; i < 16; i++) {
428 if (i % 4 == 0)
429 rnd = arc4random();
430 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
431 rnd & 0xff);
432 rnd >>= 8;
433 }
434 }
435}
436
Damien Miller5428f641999-11-25 11:54:57 +1100437/*
438 * This is called when the interactive is entered. This checks if there is
439 * an EOF coming on stdin. We must check this explicitly, as select() does
440 * not appear to wake up when redirecting from /dev/null.
441 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000442
Ben Lindstrombba81212001-06-25 05:01:22 +0000443static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000444client_check_initial_eof_on_stdin(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445{
Damien Miller95def091999-11-25 00:26:21 +1100446 int len;
447 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000448
Damien Miller5428f641999-11-25 11:54:57 +1100449 /*
450 * If standard input is to be "redirected from /dev/null", we simply
451 * mark that we have seen an EOF and send an EOF message to the
452 * server. Otherwise, we try to read a single character; it appears
453 * that for some files, such /dev/null, select() never wakes up for
454 * read for this descriptor, which means that we never get EOF. This
455 * way we will get the EOF if stdin comes from /dev/null or similar.
456 */
Damien Miller95def091999-11-25 00:26:21 +1100457 if (stdin_null_flag) {
458 /* Fake EOF on stdin. */
459 debug("Sending eof.");
460 stdin_eof = 1;
461 packet_start(SSH_CMSG_EOF);
462 packet_send();
463 } else {
Damien Miller95def091999-11-25 00:26:21 +1100464 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000465
Damien Miller95def091999-11-25 00:26:21 +1100466 /* Check for immediate EOF on stdin. */
467 len = read(fileno(stdin), buf, 1);
468 if (len == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000469 /*
470 * EOF. Record that we have seen it and send
471 * EOF to server.
472 */
Damien Miller95def091999-11-25 00:26:21 +1100473 debug("Sending eof.");
474 stdin_eof = 1;
475 packet_start(SSH_CMSG_EOF);
476 packet_send();
477 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100478 /*
479 * Got data. We must store the data in the buffer,
480 * and also process it as an escape character if
481 * appropriate.
482 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000483 if ((u_char) buf[0] == escape_char1)
484 escape_pending1 = 1;
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000485 else
Damien Miller95def091999-11-25 00:26:21 +1100486 buffer_append(&stdin_buffer, buf, 1);
Damien Miller95def091999-11-25 00:26:21 +1100487 }
Damien Miller95def091999-11-25 00:26:21 +1100488 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490}
491
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000492
Damien Miller5428f641999-11-25 11:54:57 +1100493/*
494 * Make packets from buffered stdin data, and buffer them for sending to the
495 * connection.
496 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000497
Ben Lindstrombba81212001-06-25 05:01:22 +0000498static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000499client_make_packets_from_stdin_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000500{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000501 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000502
Damien Miller95def091999-11-25 00:26:21 +1100503 /* Send buffered stdin data to the server. */
504 while (buffer_len(&stdin_buffer) > 0 &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100505 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100506 len = buffer_len(&stdin_buffer);
507 /* Keep the packets at reasonable size. */
508 if (len > packet_get_maxsize())
509 len = packet_get_maxsize();
510 packet_start(SSH_CMSG_STDIN_DATA);
511 packet_put_string(buffer_ptr(&stdin_buffer), len);
512 packet_send();
513 buffer_consume(&stdin_buffer, len);
514 /* If we have a pending EOF, send it now. */
515 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
516 packet_start(SSH_CMSG_EOF);
517 packet_send();
518 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000520}
521
Damien Miller5428f641999-11-25 11:54:57 +1100522/*
523 * Checks if the client window has changed, and sends a packet about it to
524 * the server if so. The actual change is detected elsewhere (by a software
525 * interrupt on Unix); this just checks the flag and sends a message if
526 * appropriate.
527 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000528
Ben Lindstrombba81212001-06-25 05:01:22 +0000529static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000530client_check_window_change(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000531{
Damien Miller1383bd82000-04-06 12:32:37 +1000532 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000533
Damien Miller1383bd82000-04-06 12:32:37 +1000534 if (! received_window_change_signal)
535 return;
536 /** XXX race */
537 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000538
Damien Millerd3444942000-09-30 14:20:03 +1100539 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000540
541 if (compat20) {
Damien Miller0e220db2004-06-15 10:34:08 +1000542 channel_send_window_changes();
Damien Miller1383bd82000-04-06 12:32:37 +1000543 } else {
Damien Miller0e220db2004-06-15 10:34:08 +1000544 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
545 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000546 packet_start(SSH_CMSG_WINDOW_SIZE);
Damien Miller71a73672006-03-26 14:04:36 +1100547 packet_put_int((u_int)ws.ws_row);
548 packet_put_int((u_int)ws.ws_col);
549 packet_put_int((u_int)ws.ws_xpixel);
550 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller1383bd82000-04-06 12:32:37 +1000551 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000552 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000553}
554
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000555static int
Damien Miller509b0102003-12-17 16:33:10 +1100556client_global_request_reply(int type, u_int32_t seq, void *ctxt)
557{
Darren Tucker9f407c42008-06-13 04:50:27 +1000558 struct global_confirm *gc;
559
560 if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000561 return 0;
Darren Tucker9f407c42008-06-13 04:50:27 +1000562 if (gc->cb != NULL)
563 gc->cb(type, seq, gc->ctx);
564 if (--gc->ref_count <= 0) {
565 TAILQ_REMOVE(&global_confirms, gc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +1100566 explicit_bzero(gc, sizeof(*gc));
Darren Tuckera627d422013-06-02 07:31:17 +1000567 free(gc);
Darren Tucker9f407c42008-06-13 04:50:27 +1000568 }
569
Darren Tuckerf7288d72009-06-21 18:12:20 +1000570 packet_set_alive_timeouts(0);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000571 return 0;
Damien Miller509b0102003-12-17 16:33:10 +1100572}
573
574static void
575server_alive_check(void)
576{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000577 if (packet_inc_alive_timeouts() > options.server_alive_count_max) {
Damien Millerb73b6fd2011-01-11 17:18:56 +1100578 logit("Timeout, server %s not responding.", host);
Damien Miller985a4482006-10-24 03:02:41 +1000579 cleanup_exit(255);
580 }
Damien Miller509b0102003-12-17 16:33:10 +1100581 packet_start(SSH2_MSG_GLOBAL_REQUEST);
582 packet_put_cstring("keepalive@openssh.com");
583 packet_put_char(1); /* boolean: want reply */
584 packet_send();
Darren Tucker9f407c42008-06-13 04:50:27 +1000585 /* Insert an empty placeholder to maintain ordering */
586 client_register_global_confirm(NULL, NULL);
Damien Miller509b0102003-12-17 16:33:10 +1100587}
588
Damien Miller5428f641999-11-25 11:54:57 +1100589/*
590 * Waits until the client can do something (some data becomes available on
591 * one of the file descriptors).
592 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000593static void
Damien Miller5e953212001-01-30 09:14:00 +1100594client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000595 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000596{
Damien Miller509b0102003-12-17 16:33:10 +1100597 struct timeval tv, *tvp;
Damien Millere11e1ea2010-08-03 16:04:46 +1000598 int timeout_secs;
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000599 time_t minwait_secs = 0, server_alive_time = 0, now = monotime();
Damien Miller509b0102003-12-17 16:33:10 +1100600 int ret;
601
Damien Miller5e953212001-01-30 09:14:00 +1100602 /* Add any selections by the channel mechanism. */
Damien Millera6508752012-04-22 11:21:10 +1000603 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp,
604 &minwait_secs, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000605
Damien Miller1383bd82000-04-06 12:32:37 +1000606 if (!compat20) {
607 /* Read from the connection, unless our buffers are full. */
608 if (buffer_len(&stdout_buffer) < buffer_high &&
609 buffer_len(&stderr_buffer) < buffer_high &&
610 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100611 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000612 /*
613 * Read from stdin, unless we have seen EOF or have very much
614 * buffered data to send to the server.
615 */
616 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100617 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000618
619 /* Select stdout/stderr if have data in buffer. */
620 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100621 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000622 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100623 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000624 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000625 /* channel_prepare_select could have closed the last channel */
Damien Miller164a7f42001-10-12 11:36:09 +1000626 if (session_closed && !channel_still_open() &&
627 !packet_have_data_to_write()) {
628 /* clear mask since we did not call select() */
Damien Miller79faeff2001-11-12 11:06:32 +1100629 memset(*readsetp, 0, *nallocp);
630 memset(*writesetp, 0, *nallocp);
Damien Miller164a7f42001-10-12 11:36:09 +1000631 return;
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000632 } else {
633 FD_SET(connection_in, *readsetp);
634 }
Damien Miller1383bd82000-04-06 12:32:37 +1000635 }
636
Damien Miller95def091999-11-25 00:26:21 +1100637 /* Select server connection if have data to write to the server. */
638 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100639 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100640
Damien Miller5428f641999-11-25 11:54:57 +1100641 /*
642 * Wait for something to happen. This will suspend the process until
643 * some selected descriptor can be read, written, or has some other
Damien Millere11e1ea2010-08-03 16:04:46 +1000644 * event pending, or a timeout expires.
Damien Miller5428f641999-11-25 11:54:57 +1100645 */
Damien Miller95def091999-11-25 00:26:21 +1100646
Damien Millere11e1ea2010-08-03 16:04:46 +1000647 timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000648 if (options.server_alive_interval > 0 && compat20) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000649 timeout_secs = options.server_alive_interval;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000650 server_alive_time = now + options.server_alive_interval;
651 }
652 if (options.rekey_interval > 0 && compat20 && !rekeying)
653 timeout_secs = MIN(timeout_secs, packet_get_rekey_timeout());
Damien Millere11e1ea2010-08-03 16:04:46 +1000654 set_control_persist_exit_time();
655 if (control_persist_exit_time > 0) {
656 timeout_secs = MIN(timeout_secs,
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000657 control_persist_exit_time - now);
Damien Millere11e1ea2010-08-03 16:04:46 +1000658 if (timeout_secs < 0)
659 timeout_secs = 0;
660 }
Damien Millera6508752012-04-22 11:21:10 +1000661 if (minwait_secs != 0)
662 timeout_secs = MIN(timeout_secs, (int)minwait_secs);
Damien Millere11e1ea2010-08-03 16:04:46 +1000663 if (timeout_secs == INT_MAX)
Damien Miller509b0102003-12-17 16:33:10 +1100664 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000665 else {
Damien Millere11e1ea2010-08-03 16:04:46 +1000666 tv.tv_sec = timeout_secs;
Damien Miller509b0102003-12-17 16:33:10 +1100667 tv.tv_usec = 0;
668 tvp = &tv;
669 }
Damien Millere11e1ea2010-08-03 16:04:46 +1000670
Damien Miller509b0102003-12-17 16:33:10 +1100671 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
672 if (ret < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100673 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000674
675 /*
676 * We have to clear the select masks, because we return.
677 * We have to return, because the mainloop checks for the flags
678 * set by the signal handlers.
679 */
Damien Miller79faeff2001-11-12 11:06:32 +1100680 memset(*readsetp, 0, *nallocp);
681 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000682
Damien Miller95def091999-11-25 00:26:21 +1100683 if (errno == EINTR)
684 return;
685 /* Note: we might still have data in the buffers. */
686 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
687 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100688 quit_pending = 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000689 } else if (ret == 0) {
690 /*
691 * Timeout. Could have been either keepalive or rekeying.
692 * Keepalive we check here, rekeying is checked in clientloop.
693 */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000694 if (server_alive_time != 0 && server_alive_time <= monotime())
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000695 server_alive_check();
696 }
697
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000698}
699
Ben Lindstrombba81212001-06-25 05:01:22 +0000700static void
Damien Millerad833b32000-08-23 10:46:23 +1000701client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000702{
Damien Miller95def091999-11-25 00:26:21 +1100703 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000704 if (buffer_len(bout) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000705 atomicio(vwrite, fileno(stdout), buffer_ptr(bout),
706 buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000707 if (buffer_len(berr) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000708 atomicio(vwrite, fileno(stderr), buffer_ptr(berr),
709 buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000710
Damien Miller21771e22011-05-15 08:45:50 +1000711 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000712
Damien Miller5428f641999-11-25 11:54:57 +1100713 /*
714 * Free (and clear) the buffer to reduce the amount of data that gets
715 * written to swap.
716 */
Damien Millerad833b32000-08-23 10:46:23 +1000717 buffer_free(bin);
718 buffer_free(bout);
719 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000720
Damien Miller95def091999-11-25 00:26:21 +1100721 /* Send the suspend signal to the program itself. */
722 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000723
Darren Tucker5d78de62004-11-05 20:35:44 +1100724 /* Reset window sizes in case they have changed */
725 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000726
Damien Miller95def091999-11-25 00:26:21 +1100727 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000728 buffer_init(bin);
729 buffer_init(bout);
730 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000731
Damien Miller21771e22011-05-15 08:45:50 +1000732 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000733}
734
Ben Lindstrombba81212001-06-25 05:01:22 +0000735static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100736client_process_net_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000737{
Darren Tuckerc5564e12009-06-21 18:53:53 +1000738 int len, cont = 0;
Darren Tucker86e30a02009-08-28 11:21:06 +1000739 char buf[SSH_IOBUFSZ];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000740
Damien Miller5428f641999-11-25 11:54:57 +1100741 /*
742 * Read input from the server, and add any such data to the buffer of
743 * the packet subsystem.
744 */
Damien Miller95def091999-11-25 00:26:21 +1100745 if (FD_ISSET(connection_in, readset)) {
746 /* Read as much as possible. */
Darren Tuckerc5564e12009-06-21 18:53:53 +1000747 len = roaming_read(connection_in, buf, sizeof(buf), &cont);
748 if (len == 0 && cont == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000749 /*
750 * Received EOF. The remote host has closed the
751 * connection.
752 */
753 snprintf(buf, sizeof buf,
754 "Connection to %.300s closed by remote host.\r\n",
755 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000756 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000757 quit_pending = 1;
758 return;
Damien Miller95def091999-11-25 00:26:21 +1100759 }
Damien Miller5428f641999-11-25 11:54:57 +1100760 /*
761 * There is a kernel bug on Solaris that causes select to
762 * sometimes wake up even though there is no data available.
763 */
Damien Millerd8968ad2008-07-04 23:10:49 +1000764 if (len < 0 &&
765 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Damien Miller95def091999-11-25 00:26:21 +1100766 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000767
Damien Miller95def091999-11-25 00:26:21 +1100768 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000769 /*
770 * An error has encountered. Perhaps there is a
771 * network problem.
772 */
773 snprintf(buf, sizeof buf,
774 "Read from remote host %.300s: %.100s\r\n",
775 host, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100776 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100777 quit_pending = 1;
778 return;
779 }
780 packet_process_incoming(buf, len);
781 }
Damien Miller1383bd82000-04-06 12:32:37 +1000782}
783
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000784static void
Damien Miller5771ed72008-05-19 15:35:33 +1000785client_status_confirm(int type, Channel *c, void *ctx)
Damien Miller0e220db2004-06-15 10:34:08 +1000786{
Damien Miller5771ed72008-05-19 15:35:33 +1000787 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
788 char errmsg[256];
789 int tochan;
Darren Tuckerfc959702004-07-17 16:12:08 +1000790
Damien Miller555f3b82011-05-15 08:48:05 +1000791 /*
792 * If a TTY was explicitly requested, then a failure to allocate
793 * one is fatal.
794 */
795 if (cr->action == CONFIRM_TTY &&
796 (options.request_tty == REQUEST_TTY_FORCE ||
797 options.request_tty == REQUEST_TTY_YES))
798 cr->action = CONFIRM_CLOSE;
799
Damien Miller5771ed72008-05-19 15:35:33 +1000800 /* XXX supress on mux _client_ quietmode */
801 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
Damien Millere1537f92010-01-26 13:26:22 +1100802 c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
Damien Miller0e220db2004-06-15 10:34:08 +1000803
Damien Miller5771ed72008-05-19 15:35:33 +1000804 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
805 debug2("%s request accepted on channel %d",
806 cr->request_type, c->self);
807 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
808 if (tochan) {
809 snprintf(errmsg, sizeof(errmsg),
810 "%s request failed\r\n", cr->request_type);
811 } else {
812 snprintf(errmsg, sizeof(errmsg),
813 "%s request failed on channel %d",
814 cr->request_type, c->self);
815 }
816 /* If error occurred on primary session channel, then exit */
Damien Miller555f3b82011-05-15 08:48:05 +1000817 if (cr->action == CONFIRM_CLOSE && c->self == session_ident)
Damien Miller5771ed72008-05-19 15:35:33 +1000818 fatal("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000819 /*
820 * If error occurred on mux client, append to
821 * their stderr.
822 */
823 if (tochan) {
824 buffer_append(&c->extended, errmsg,
825 strlen(errmsg));
826 } else
Damien Miller5771ed72008-05-19 15:35:33 +1000827 error("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000828 if (cr->action == CONFIRM_TTY) {
829 /*
830 * If a TTY allocation error occurred, then arrange
831 * for the correct TTY to leave raw mode.
832 */
833 if (c->self == session_ident)
834 leave_raw_mode(0);
835 else
836 mux_tty_alloc_failed(c);
837 } else if (cr->action == CONFIRM_CLOSE) {
Damien Miller5771ed72008-05-19 15:35:33 +1000838 chan_read_failed(c);
839 chan_write_failed(c);
840 }
Damien Miller0e220db2004-06-15 10:34:08 +1000841 }
Darren Tuckera627d422013-06-02 07:31:17 +1000842 free(cr);
Damien Miller5771ed72008-05-19 15:35:33 +1000843}
Damien Miller0e220db2004-06-15 10:34:08 +1000844
Damien Miller5771ed72008-05-19 15:35:33 +1000845static void
846client_abandon_status_confirm(Channel *c, void *ctx)
847{
Darren Tuckera627d422013-06-02 07:31:17 +1000848 free(ctx);
Damien Miller5771ed72008-05-19 15:35:33 +1000849}
850
Damien Miller6d7b4372011-06-23 08:31:57 +1000851void
Damien Miller555f3b82011-05-15 08:48:05 +1000852client_expect_confirm(int id, const char *request,
853 enum confirm_action action)
Damien Miller5771ed72008-05-19 15:35:33 +1000854{
Damien Miller6c81fee2013-11-08 12:19:55 +1100855 struct channel_reply_ctx *cr = xcalloc(1, sizeof(*cr));
Damien Miller5771ed72008-05-19 15:35:33 +1000856
857 cr->request_type = request;
Damien Miller555f3b82011-05-15 08:48:05 +1000858 cr->action = action;
Damien Miller5771ed72008-05-19 15:35:33 +1000859
860 channel_register_status_confirm(id, client_status_confirm,
861 client_abandon_status_confirm, cr);
Damien Miller0e220db2004-06-15 10:34:08 +1000862}
863
Darren Tucker9f407c42008-06-13 04:50:27 +1000864void
865client_register_global_confirm(global_confirm_cb *cb, void *ctx)
866{
Damien Millerb9d3bee2008-07-16 22:40:52 +1000867 struct global_confirm *gc, *last_gc;
Darren Tucker9f407c42008-06-13 04:50:27 +1000868
869 /* Coalesce identical callbacks */
Damien Millerb9d3bee2008-07-16 22:40:52 +1000870 last_gc = TAILQ_LAST(&global_confirms, global_confirms);
871 if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
872 if (++last_gc->ref_count >= INT_MAX)
873 fatal("%s: last_gc->ref_count = %d",
874 __func__, last_gc->ref_count);
Darren Tucker9f407c42008-06-13 04:50:27 +1000875 return;
876 }
877
Damien Miller6c81fee2013-11-08 12:19:55 +1100878 gc = xcalloc(1, sizeof(*gc));
Darren Tucker9f407c42008-06-13 04:50:27 +1000879 gc->cb = cb;
880 gc->ctx = ctx;
881 gc->ref_count = 1;
882 TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
883}
884
Damien Miller0e220db2004-06-15 10:34:08 +1000885static void
Ben Lindstrom681d9322002-03-22 03:53:00 +0000886process_cmdline(void)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000887{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000888 void (*handler)(int);
Damien Miller7acefbb2014-07-18 14:11:24 +1000889 char *s, *cmd;
890 int ok, delete = 0, local = 0, remote = 0, dynamic = 0;
891 struct Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000892
Damien Miller1d2c4562014-02-04 11:18:20 +1100893 memset(&fwd, 0, sizeof(fwd));
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100894
Damien Miller21771e22011-05-15 08:45:50 +1000895 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000896 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000897 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000898 if (s == NULL)
899 goto out;
Damien Millerfdb23062013-11-21 13:57:15 +1100900 while (isspace((u_char)*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000901 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000902 if (*s == '-')
903 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000904 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000905 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000906
Darren Tucker1973c882004-05-24 10:34:36 +1000907 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000908 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000909 logit(" -L[bind_address:]port:host:hostport "
910 "Request local forward");
911 logit(" -R[bind_address:]port:host:hostport "
912 "Request remote forward");
Damien Miller0164cb82008-11-05 16:30:31 +1100913 logit(" -D[bind_address:]port "
914 "Request dynamic forward");
Damien Millerff773642011-09-22 21:39:48 +1000915 logit(" -KL[bind_address:]port "
916 "Cancel local forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000917 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000918 "Cancel remote forward");
Damien Millerff773642011-09-22 21:39:48 +1000919 logit(" -KD[bind_address:]port "
920 "Cancel dynamic forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100921 if (!options.permit_local_command)
922 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000923 logit(" !args "
924 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100925 goto out;
926 }
927
928 if (*s == '!' && options.permit_local_command) {
929 s++;
930 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000931 goto out;
932 }
933
934 if (*s == 'K') {
935 delete = 1;
936 s++;
937 }
Damien Miller0164cb82008-11-05 16:30:31 +1100938 if (*s == 'L')
939 local = 1;
940 else if (*s == 'R')
941 remote = 1;
942 else if (*s == 'D')
943 dynamic = 1;
944 else {
Damien Miller996acd22003-04-09 20:59:48 +1000945 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000946 goto out;
947 }
Damien Miller0164cb82008-11-05 16:30:31 +1100948
Damien Millerff773642011-09-22 21:39:48 +1000949 if (delete && !compat20) {
Damien Miller996acd22003-04-09 20:59:48 +1000950 logit("Not supported for SSH protocol version 1.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000951 goto out;
952 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000953
Damien Millerfdb23062013-11-21 13:57:15 +1100954 while (isspace((u_char)*++s))
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100955 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000956
Damien Millere1537f92010-01-26 13:26:22 +1100957 /* XXX update list of forwards in options */
Darren Tuckere7066df2004-05-24 10:18:05 +1000958 if (delete) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000959 /* We pass 1 for dynamicfwd to restrict to 1 or 2 fields. */
960 if (!parse_forward(&fwd, s, 1, 0)) {
961 logit("Bad forwarding close specification.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000962 goto out;
963 }
Damien Millerff773642011-09-22 21:39:48 +1000964 if (remote)
Damien Miller7acefbb2014-07-18 14:11:24 +1000965 ok = channel_request_rforward_cancel(&fwd) == 0;
Damien Millerff773642011-09-22 21:39:48 +1000966 else if (dynamic)
Damien Miller7acefbb2014-07-18 14:11:24 +1000967 ok = channel_cancel_lport_listener(&fwd,
968 0, &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000969 else
Damien Miller7acefbb2014-07-18 14:11:24 +1000970 ok = channel_cancel_lport_listener(&fwd,
971 CHANNEL_CANCEL_PORT_STATIC,
972 &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000973 if (!ok) {
974 logit("Unkown port forwarding.");
975 goto out;
976 }
977 logit("Canceled forwarding.");
Darren Tuckere7066df2004-05-24 10:18:05 +1000978 } else {
Damien Miller4bf648f2009-02-14 16:28:21 +1100979 if (!parse_forward(&fwd, s, dynamic, remote)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000980 logit("Bad forwarding specification.");
981 goto out;
982 }
Damien Miller0164cb82008-11-05 16:30:31 +1100983 if (local || dynamic) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000984 if (!channel_setup_local_fwd_listener(&fwd,
985 &options.fwd_opts)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000986 logit("Port forwarding failed.");
987 goto out;
988 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100989 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +1000990 if (channel_request_remote_forwarding(&fwd) < 0) {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000991 logit("Port forwarding failed.");
992 goto out;
993 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100994 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000995 logit("Forwarding port.");
996 }
997
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000998out:
999 signal(SIGINT, handler);
Damien Miller21771e22011-05-15 08:45:50 +10001000 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tuckera627d422013-06-02 07:31:17 +10001001 free(cmd);
1002 free(fwd.listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +10001003 free(fwd.listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +10001004 free(fwd.connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +10001005 free(fwd.connect_path);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001006}
1007
Darren Tucker92a39cf2012-09-07 11:20:20 +10001008/* reasons to suppress output of an escape command in help output */
1009#define SUPPRESS_NEVER 0 /* never suppress, always show */
1010#define SUPPRESS_PROTO1 1 /* don't show in protocol 1 sessions */
1011#define SUPPRESS_MUXCLIENT 2 /* don't show in mux client sessions */
1012#define SUPPRESS_MUXMASTER 4 /* don't show in mux master sessions */
1013#define SUPPRESS_SYSLOG 8 /* don't show when logging to syslog */
1014struct escape_help_text {
1015 const char *cmd;
1016 const char *text;
1017 unsigned int flags;
1018};
1019static struct escape_help_text esc_txt[] = {
1020 {".", "terminate session", SUPPRESS_MUXMASTER},
1021 {".", "terminate connection (and any multiplexed sessions)",
1022 SUPPRESS_MUXCLIENT},
1023 {"B", "send a BREAK to the remote system", SUPPRESS_PROTO1},
1024 {"C", "open a command line", SUPPRESS_MUXCLIENT},
1025 {"R", "request rekey", SUPPRESS_PROTO1},
Darren Tuckerca0d0fd2012-09-07 11:22:24 +10001026 {"V/v", "decrease/increase verbosity (LogLevel)", SUPPRESS_MUXCLIENT},
Darren Tucker92a39cf2012-09-07 11:20:20 +10001027 {"^Z", "suspend ssh", SUPPRESS_MUXCLIENT},
1028 {"#", "list forwarded connections", SUPPRESS_NEVER},
1029 {"&", "background ssh (when waiting for connections to terminate)",
1030 SUPPRESS_MUXCLIENT},
1031 {"?", "this message", SUPPRESS_NEVER},
1032};
1033
1034static void
1035print_escape_help(Buffer *b, int escape_char, int protocol2, int mux_client,
1036 int using_stderr)
1037{
1038 unsigned int i, suppress_flags;
1039 char string[1024];
1040
1041 snprintf(string, sizeof string, "%c?\r\n"
1042 "Supported escape sequences:\r\n", escape_char);
1043 buffer_append(b, string, strlen(string));
1044
1045 suppress_flags = (protocol2 ? 0 : SUPPRESS_PROTO1) |
1046 (mux_client ? SUPPRESS_MUXCLIENT : 0) |
1047 (mux_client ? 0 : SUPPRESS_MUXMASTER) |
1048 (using_stderr ? 0 : SUPPRESS_SYSLOG);
1049
1050 for (i = 0; i < sizeof(esc_txt)/sizeof(esc_txt[0]); i++) {
1051 if (esc_txt[i].flags & suppress_flags)
1052 continue;
Darren Tuckerca0d0fd2012-09-07 11:22:24 +10001053 snprintf(string, sizeof string, " %c%-3s - %s\r\n",
Darren Tucker92a39cf2012-09-07 11:20:20 +10001054 escape_char, esc_txt[i].cmd, esc_txt[i].text);
1055 buffer_append(b, string, strlen(string));
1056 }
1057
1058 snprintf(string, sizeof string,
Darren Tuckerca0d0fd2012-09-07 11:22:24 +10001059 " %c%c - send the escape character by typing it twice\r\n"
Darren Tucker92a39cf2012-09-07 11:20:20 +10001060 "(Note that escapes are only recognized immediately after "
1061 "newline.)\r\n", escape_char, escape_char);
1062 buffer_append(b, string, strlen(string));
1063}
1064
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001065/*
1066 * Process the characters one by one, call with c==NULL for proto1 case.
1067 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001068static int
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001069process_escapes(Channel *c, Buffer *bin, Buffer *bout, Buffer *berr,
1070 char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001071{
1072 char string[1024];
1073 pid_t pid;
1074 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001075 u_int i;
1076 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +10001077 char *s;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001078 int *escape_pendingp, escape_char;
1079 struct escape_filter_ctx *efc;
Damien Millerad833b32000-08-23 10:46:23 +10001080
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001081 if (c == NULL) {
1082 escape_pendingp = &escape_pending1;
1083 escape_char = escape_char1;
1084 } else {
1085 if (c->filter_ctx == NULL)
1086 return 0;
1087 efc = (struct escape_filter_ctx *)c->filter_ctx;
1088 escape_pendingp = &efc->escape_pending;
1089 escape_char = efc->escape_char;
1090 }
1091
Damien Millereccb9de2005-06-17 12:59:34 +10001092 if (len <= 0)
1093 return (0);
1094
1095 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +10001096 /* Get one character at a time. */
1097 ch = buf[i];
1098
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001099 if (*escape_pendingp) {
Damien Millerad833b32000-08-23 10:46:23 +10001100 /* We have previously seen an escape character. */
1101 /* Clear the flag now. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001102 *escape_pendingp = 0;
Damien Millerad833b32000-08-23 10:46:23 +10001103
1104 /* Process the escaped character. */
1105 switch (ch) {
1106 case '.':
1107 /* Terminate the connection. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001108 snprintf(string, sizeof string, "%c.\r\n",
1109 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001110 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001111
Damien Millere1537f92010-01-26 13:26:22 +11001112 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001113 chan_read_failed(c);
1114 chan_write_failed(c);
Darren Tuckerea8342c2013-06-06 08:11:40 +10001115 if (c->detach_user)
1116 c->detach_user(c->self, NULL);
Damien Miller36187092013-06-10 13:07:11 +10001117 c->type = SSH_CHANNEL_ABANDONED;
1118 buffer_clear(&c->input);
1119 chan_ibuf_empty(c);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001120 return 0;
1121 } else
1122 quit_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001123 return -1;
1124
1125 case 'Z' - 64:
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001126 /* XXX support this for mux clients */
Damien Millere1537f92010-01-26 13:26:22 +11001127 if (c && c->ctl_chan != -1) {
Darren Tuckerf111d402012-09-07 11:21:42 +10001128 char b[16];
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001129 noescape:
Darren Tuckerf111d402012-09-07 11:21:42 +10001130 if (ch == 'Z' - 64)
1131 snprintf(b, sizeof b, "^Z");
1132 else
1133 snprintf(b, sizeof b, "%c", ch);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001134 snprintf(string, sizeof string,
Darren Tuckerf111d402012-09-07 11:21:42 +10001135 "%c%s escape not available to "
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001136 "multiplexed sessions\r\n",
Darren Tuckerf111d402012-09-07 11:21:42 +10001137 escape_char, b);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001138 buffer_append(berr, string,
1139 strlen(string));
1140 continue;
1141 }
Darren Tucker4d5cd332008-06-13 04:51:14 +10001142 /* Suspend the program. Inform the user */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001143 snprintf(string, sizeof string,
1144 "%c^Z [suspend ssh]\r\n", escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001145 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001146
1147 /* Restore terminal modes and suspend. */
1148 client_suspend_self(bin, bout, berr);
1149
1150 /* We have been continued. */
1151 continue;
1152
Damien Miller54c45982003-05-15 10:20:13 +10001153 case 'B':
1154 if (compat20) {
1155 snprintf(string, sizeof string,
1156 "%cB\r\n", escape_char);
1157 buffer_append(berr, string,
1158 strlen(string));
Damien Miller70182522013-09-14 09:49:19 +10001159 channel_request_start(c->self,
Damien Miller54c45982003-05-15 10:20:13 +10001160 "break", 0);
1161 packet_put_int(1000);
1162 packet_send();
1163 }
1164 continue;
1165
Ben Lindstromf28f6342001-04-04 02:03:04 +00001166 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001167 if (compat20) {
1168 if (datafellows & SSH_BUG_NOREKEY)
Darren Tucker4d5cd332008-06-13 04:51:14 +10001169 logit("Server does not "
1170 "support re-keying");
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001171 else
1172 need_rekeying = 1;
1173 }
Ben Lindstromf28f6342001-04-04 02:03:04 +00001174 continue;
1175
Darren Tucker50a48d02012-09-06 21:25:37 +10001176 case 'V':
1177 /* FALLTHROUGH */
1178 case 'v':
1179 if (c && c->ctl_chan != -1)
1180 goto noescape;
1181 if (!log_is_on_stderr()) {
1182 snprintf(string, sizeof string,
1183 "%c%c [Logging to syslog]\r\n",
1184 escape_char, ch);
1185 buffer_append(berr, string,
1186 strlen(string));
1187 continue;
1188 }
1189 if (ch == 'V' && options.log_level >
1190 SYSLOG_LEVEL_QUIET)
1191 log_change_level(--options.log_level);
1192 if (ch == 'v' && options.log_level <
1193 SYSLOG_LEVEL_DEBUG3)
1194 log_change_level(++options.log_level);
1195 snprintf(string, sizeof string,
1196 "%c%c [LogLevel %s]\r\n", escape_char, ch,
1197 log_level_name(options.log_level));
1198 buffer_append(berr, string, strlen(string));
1199 continue;
1200
Damien Millerad833b32000-08-23 10:46:23 +10001201 case '&':
Damien Millere1537f92010-01-26 13:26:22 +11001202 if (c && c->ctl_chan != -1)
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001203 goto noescape;
Damien Millerad833b32000-08-23 10:46:23 +10001204 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001205 * Detach the program (continue to serve
1206 * connections, but put in background and no
1207 * more new connections).
Damien Millerad833b32000-08-23 10:46:23 +10001208 */
Damien Miller96507ef2001-11-12 10:52:25 +11001209 /* Restore tty modes. */
Damien Miller21771e22011-05-15 08:45:50 +10001210 leave_raw_mode(
1211 options.request_tty == REQUEST_TTY_FORCE);
Damien Miller96507ef2001-11-12 10:52:25 +11001212
1213 /* Stop listening for new connections. */
1214 channel_stop_listening();
1215
1216 snprintf(string, sizeof string,
1217 "%c& [backgrounded]\n", escape_char);
1218 buffer_append(berr, string, strlen(string));
1219
1220 /* Fork into background. */
1221 pid = fork();
1222 if (pid < 0) {
1223 error("fork: %.100s", strerror(errno));
1224 continue;
1225 }
1226 if (pid != 0) { /* This is the parent. */
1227 /* The parent just exits. */
1228 exit(0);
1229 }
1230 /* The child continues serving connections. */
1231 if (compat20) {
1232 buffer_append(bin, "\004", 1);
1233 /* fake EOF on stdin */
1234 return -1;
1235 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +10001236 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001237 * Sending SSH_CMSG_EOF alone does not
1238 * always appear to be enough. So we
1239 * try to send an EOF character first.
Damien Millerad833b32000-08-23 10:46:23 +10001240 */
1241 packet_start(SSH_CMSG_STDIN_DATA);
1242 packet_put_string("\004", 1);
1243 packet_send();
1244 /* Close stdin. */
1245 stdin_eof = 1;
1246 if (buffer_len(bin) == 0) {
1247 packet_start(SSH_CMSG_EOF);
1248 packet_send();
1249 }
1250 }
Damien Miller96507ef2001-11-12 10:52:25 +11001251 continue;
Damien Millerad833b32000-08-23 10:46:23 +10001252
1253 case '?':
Darren Tucker92a39cf2012-09-07 11:20:20 +10001254 print_escape_help(berr, escape_char, compat20,
1255 (c && c->ctl_chan != -1),
1256 log_is_on_stderr());
Damien Millerad833b32000-08-23 10:46:23 +10001257 continue;
1258
1259 case '#':
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001260 snprintf(string, sizeof string, "%c#\r\n",
1261 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001262 buffer_append(berr, string, strlen(string));
1263 s = channel_open_message();
1264 buffer_append(berr, s, strlen(s));
Darren Tuckera627d422013-06-02 07:31:17 +10001265 free(s);
Damien Millerad833b32000-08-23 10:46:23 +10001266 continue;
1267
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001268 case 'C':
Damien Millere1537f92010-01-26 13:26:22 +11001269 if (c && c->ctl_chan != -1)
Damien Miller586b0052008-12-09 14:11:32 +11001270 goto noescape;
Ben Lindstrom681d9322002-03-22 03:53:00 +00001271 process_cmdline();
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001272 continue;
1273
Damien Millerad833b32000-08-23 10:46:23 +10001274 default:
1275 if (ch != escape_char) {
1276 buffer_put_char(bin, escape_char);
1277 bytes++;
1278 }
1279 /* Escaped characters fall through here */
1280 break;
1281 }
1282 } else {
1283 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001284 * The previous character was not an escape char.
1285 * Check if this is an escape.
Damien Millerad833b32000-08-23 10:46:23 +10001286 */
1287 if (last_was_cr && ch == escape_char) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001288 /*
1289 * It is. Set the flag and continue to
1290 * next character.
1291 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001292 *escape_pendingp = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001293 continue;
1294 }
1295 }
1296
1297 /*
1298 * Normal character. Record whether it was a newline,
1299 * and append it to the buffer.
1300 */
1301 last_was_cr = (ch == '\r' || ch == '\n');
1302 buffer_put_char(bin, ch);
1303 bytes++;
1304 }
1305 return bytes;
1306}
1307
Ben Lindstrombba81212001-06-25 05:01:22 +00001308static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001309client_process_input(fd_set *readset)
Damien Miller1383bd82000-04-06 12:32:37 +10001310{
Damien Miller166fca82000-04-20 07:42:21 +10001311 int len;
Darren Tucker86e30a02009-08-28 11:21:06 +10001312 char buf[SSH_IOBUFSZ];
Damien Miller1383bd82000-04-06 12:32:37 +10001313
Damien Miller95def091999-11-25 00:26:21 +11001314 /* Read input from stdin. */
1315 if (FD_ISSET(fileno(stdin), readset)) {
1316 /* Read as much as possible. */
1317 len = read(fileno(stdin), buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +10001318 if (len < 0 &&
1319 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001320 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +11001321 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001322 /*
1323 * Received EOF or error. They are treated
1324 * similarly, except that an error message is printed
1325 * if it was an error condition.
1326 */
Damien Miller95def091999-11-25 00:26:21 +11001327 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001328 snprintf(buf, sizeof buf, "read: %.100s\r\n",
1329 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001330 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001331 }
1332 /* Mark that we have seen EOF. */
1333 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +11001334 /*
1335 * Send an EOF message to the server unless there is
1336 * data in the buffer. If there is data in the
1337 * buffer, no message will be sent now. Code
1338 * elsewhere will send the EOF when the buffer
1339 * becomes empty if stdin_eof is set.
1340 */
Damien Miller95def091999-11-25 00:26:21 +11001341 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001342 packet_start(SSH_CMSG_EOF);
1343 packet_send();
Damien Miller95def091999-11-25 00:26:21 +11001344 }
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001345 } else if (escape_char1 == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +11001346 /*
1347 * Normal successful read, and no escape character.
1348 * Just append the data to buffer.
1349 */
Damien Miller95def091999-11-25 00:26:21 +11001350 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +11001351 } else {
Damien Miller5428f641999-11-25 11:54:57 +11001352 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001353 * Normal, successful read. But we have an escape
1354 * character and have to process the characters one
1355 * by one.
Damien Miller5428f641999-11-25 11:54:57 +11001356 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001357 if (process_escapes(NULL, &stdin_buffer,
1358 &stdout_buffer, &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +10001359 return;
Damien Miller95def091999-11-25 00:26:21 +11001360 }
1361 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001362}
1363
Ben Lindstrombba81212001-06-25 05:01:22 +00001364static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001365client_process_output(fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001366{
Damien Miller95def091999-11-25 00:26:21 +11001367 int len;
1368 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001369
Damien Miller95def091999-11-25 00:26:21 +11001370 /* Write buffered output to stdout. */
1371 if (FD_ISSET(fileno(stdout), writeset)) {
1372 /* Write as much data as possible. */
1373 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001374 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001375 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001376 if (errno == EINTR || errno == EAGAIN ||
1377 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001378 len = 0;
1379 else {
Damien Miller5428f641999-11-25 11:54:57 +11001380 /*
1381 * An error or EOF was encountered. Put an
1382 * error message to stderr buffer.
1383 */
Darren Tucker4d5cd332008-06-13 04:51:14 +10001384 snprintf(buf, sizeof buf,
1385 "write stdout: %.50s\r\n", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001386 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001387 quit_pending = 1;
1388 return;
1389 }
1390 }
1391 /* Consume printed data from the buffer. */
1392 buffer_consume(&stdout_buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001393 }
Damien Miller95def091999-11-25 00:26:21 +11001394 /* Write buffered output to stderr. */
1395 if (FD_ISSET(fileno(stderr), writeset)) {
1396 /* Write as much data as possible. */
1397 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001398 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001399 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001400 if (errno == EINTR || errno == EAGAIN ||
1401 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001402 len = 0;
1403 else {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001404 /*
1405 * EOF or error, but can't even print
1406 * error message.
1407 */
Damien Miller95def091999-11-25 00:26:21 +11001408 quit_pending = 1;
1409 return;
1410 }
1411 }
1412 /* Consume printed characters from the buffer. */
1413 buffer_consume(&stderr_buffer, len);
1414 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001415}
1416
Damien Miller5428f641999-11-25 11:54:57 +11001417/*
Damien Millerb38eff82000-04-01 11:09:21 +10001418 * Get packets from the connection input buffer, and process them as long as
1419 * there are packets available.
1420 *
1421 * Any unknown packets received during the actual
1422 * session cause the session to terminate. This is
1423 * intended to make debugging easier since no
1424 * confirmations are sent. Any compatible protocol
1425 * extensions must be negotiated during the
1426 * preparatory phase.
1427 */
1428
Ben Lindstrombba81212001-06-25 05:01:22 +00001429static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001430client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001431{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +00001432 dispatch_run(DISPATCH_NONBLOCK, &quit_pending, active_state);
Damien Millerb38eff82000-04-01 11:09:21 +10001433}
1434
Damien Millerad833b32000-08-23 10:46:23 +10001435/* scan buf[] for '~' before sending data to the peer */
1436
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001437/* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1438void *
1439client_new_escape_filter_ctx(int escape_char)
1440{
1441 struct escape_filter_ctx *ret;
1442
Damien Miller6c81fee2013-11-08 12:19:55 +11001443 ret = xcalloc(1, sizeof(*ret));
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001444 ret->escape_pending = 0;
1445 ret->escape_char = escape_char;
1446 return (void *)ret;
1447}
1448
Darren Tucker84c56f52008-06-13 04:55:46 +10001449/* Free the escape filter context on channel free */
1450void
1451client_filter_cleanup(int cid, void *ctx)
1452{
Darren Tuckera627d422013-06-02 07:31:17 +10001453 free(ctx);
Darren Tucker84c56f52008-06-13 04:55:46 +10001454}
1455
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001456int
1457client_simple_escape_filter(Channel *c, char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001458{
Damien Miller5771ed72008-05-19 15:35:33 +10001459 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1460 return 0;
1461
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001462 return process_escapes(c, &c->input, &c->output, &c->extended,
1463 buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001464}
1465
Ben Lindstrombba81212001-06-25 05:01:22 +00001466static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001467client_channel_closed(int id, void *arg)
1468{
Damien Miller3ec27592001-10-12 11:35:04 +10001469 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001470 session_closed = 1;
Damien Miller21771e22011-05-15 08:45:50 +10001471 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001472}
1473
Damien Millerb38eff82000-04-01 11:09:21 +10001474/*
Damien Miller5428f641999-11-25 11:54:57 +11001475 * Implements the interactive session with the server. This is called after
1476 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001477 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1478 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001479 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001480
Damien Miller4af51302000-04-16 11:18:38 +10001481int
Damien Millerad833b32000-08-23 10:46:23 +10001482client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001483{
Damien Miller5e953212001-01-30 09:14:00 +11001484 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001485 double start_time, total_time;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +00001486 int r, max_fd = 0, max_fd2 = 0, len, rekeying = 0;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001487 u_int64_t ibytes, obytes;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001488 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001489 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001490
Damien Miller95def091999-11-25 00:26:21 +11001491 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001492
Damien Miller95def091999-11-25 00:26:21 +11001493 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001494
Damien Miller95def091999-11-25 00:26:21 +11001495 /* Initialize variables. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001496 escape_pending1 = 0;
Damien Miller95def091999-11-25 00:26:21 +11001497 last_was_cr = 1;
1498 exit_status = -1;
1499 stdin_eof = 0;
1500 buffer_high = 64 * 1024;
1501 connection_in = packet_get_connection_in();
1502 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +11001503 max_fd = MAX(connection_in, connection_out);
1504
1505 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +00001506 /* enable nonblocking unless tty */
1507 if (!isatty(fileno(stdin)))
1508 set_nonblock(fileno(stdin));
1509 if (!isatty(fileno(stdout)))
1510 set_nonblock(fileno(stdout));
1511 if (!isatty(fileno(stderr)))
1512 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +11001513 max_fd = MAX(max_fd, fileno(stdin));
1514 max_fd = MAX(max_fd, fileno(stdout));
1515 max_fd = MAX(max_fd, fileno(stderr));
1516 }
Damien Miller95def091999-11-25 00:26:21 +11001517 quit_pending = 0;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001518 escape_char1 = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001519
Damien Miller95def091999-11-25 00:26:21 +11001520 /* Initialize buffers. */
1521 buffer_init(&stdin_buffer);
1522 buffer_init(&stdout_buffer);
1523 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001524
Damien Millerb38eff82000-04-01 11:09:21 +10001525 client_init_dispatch();
1526
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001527 /*
1528 * Set signal handlers, (e.g. to restore non-blocking mode)
1529 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1530 */
Darren Tucker07336da2004-11-05 20:02:16 +11001531 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1532 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001533 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1534 signal(SIGINT, signal_handler);
1535 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1536 signal(SIGQUIT, signal_handler);
1537 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1538 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001539 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001540
Damien Miller95def091999-11-25 00:26:21 +11001541 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001542 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001543
Ben Lindstrom5b828322001-02-09 01:34:36 +00001544 if (compat20) {
1545 session_ident = ssh2_chan_id;
Damien Miller6c3eec72011-05-05 14:16:22 +10001546 if (session_ident != -1) {
1547 if (escape_char_arg != SSH_ESCAPECHAR_NONE) {
1548 channel_register_filter(session_ident,
1549 client_simple_escape_filter, NULL,
1550 client_filter_cleanup,
1551 client_new_escape_filter_ctx(
1552 escape_char_arg));
1553 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001554 channel_register_cleanup(session_ident,
Damien Miller39eda6e2005-11-05 14:52:50 +11001555 client_channel_closed, 0);
Damien Miller6c3eec72011-05-05 14:16:22 +10001556 }
Ben Lindstrom5b828322001-02-09 01:34:36 +00001557 } else {
1558 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +10001559 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001560 }
Damien Millerad833b32000-08-23 10:46:23 +10001561
Damien Miller95def091999-11-25 00:26:21 +11001562 /* Main loop of the client for the interactive session mode. */
1563 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001564
Damien Miller5428f641999-11-25 11:54:57 +11001565 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001566 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001567
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001568 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +10001569 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001570
markus@openbsd.org091c3022015-01-19 19:52:16 +00001571 rekeying = (active_state->kex != NULL && !active_state->kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +10001572
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001573 if (rekeying) {
1574 debug("rekeying in progress");
1575 } else {
1576 /*
1577 * Make packets of buffered stdin data, and buffer
1578 * them for sending to the server.
1579 */
1580 if (!compat20)
1581 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001582
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001583 /*
1584 * Make packets from buffered channel data, and
1585 * enqueue them for sending to the server.
1586 */
1587 if (packet_not_very_much_data_to_write())
1588 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001589
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001590 /*
1591 * Check if the window size has changed, and buffer a
1592 * message about it to the server if so.
1593 */
1594 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001595
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001596 if (quit_pending)
1597 break;
1598 }
Damien Miller5428f641999-11-25 11:54:57 +11001599 /*
1600 * Wait until we have something to do (something becomes
1601 * available on one of the descriptors).
1602 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001603 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001604 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001605 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001606
Damien Miller95def091999-11-25 00:26:21 +11001607 if (quit_pending)
1608 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001609
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001610 /* Do channel operations unless rekeying in progress. */
1611 if (!rekeying) {
1612 channel_after_select(readset, writeset);
Damien Millera5539d22003-04-09 20:50:06 +10001613 if (need_rekeying || packet_need_rekeying()) {
1614 debug("need rekeying");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001615 active_state->kex->done = 0;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +00001616 if ((r = kex_send_kexinit(active_state)) != 0)
1617 fatal("%s: kex_send_kexinit: %s",
1618 __func__, ssh_err(r));
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001619 need_rekeying = 0;
1620 }
1621 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001622
Damien Miller1383bd82000-04-06 12:32:37 +10001623 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001624 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001625
Damien Miller1383bd82000-04-06 12:32:37 +10001626 if (quit_pending)
1627 break;
1628
1629 if (!compat20) {
1630 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +11001631 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +10001632 /*
1633 * Process output to stdout and stderr. Output to
1634 * the connection is processed elsewhere (above).
1635 */
Damien Miller5e953212001-01-30 09:14:00 +11001636 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +10001637 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001638
Darren Tuckere32cf432010-01-08 16:51:40 +11001639 if (session_resumed) {
1640 connection_in = packet_get_connection_in();
1641 connection_out = packet_get_connection_out();
1642 max_fd = MAX(max_fd, connection_out);
1643 max_fd = MAX(max_fd, connection_in);
1644 session_resumed = 0;
1645 }
1646
Darren Tucker4d5cd332008-06-13 04:51:14 +10001647 /*
1648 * Send as much buffered packet data as possible to the
1649 * sender.
1650 */
Damien Miller5e953212001-01-30 09:14:00 +11001651 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001652 packet_write_poll();
Damien Millere11e1ea2010-08-03 16:04:46 +10001653
1654 /*
1655 * If we are a backgrounded control master, and the
1656 * timeout has expired without any active client
1657 * connections, then quit.
1658 */
1659 if (control_persist_exit_time > 0) {
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001660 if (monotime() >= control_persist_exit_time) {
Damien Millere11e1ea2010-08-03 16:04:46 +10001661 debug("ControlPersist timeout expired");
1662 break;
1663 }
1664 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001665 }
Darren Tuckera627d422013-06-02 07:31:17 +10001666 free(readset);
1667 free(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001668
Damien Miller95def091999-11-25 00:26:21 +11001669 /* Terminate the session. */
1670
1671 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001672 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001673
Darren Tuckerde0c0252009-07-06 07:17:35 +10001674 if (compat20) {
1675 packet_start(SSH2_MSG_DISCONNECT);
1676 packet_put_int(SSH2_DISCONNECT_BY_APPLICATION);
1677 packet_put_cstring("disconnected by user");
Damien Miller8ddc71c2010-03-22 05:54:02 +11001678 packet_put_cstring(""); /* language tag */
Darren Tuckerde0c0252009-07-06 07:17:35 +10001679 packet_send();
1680 packet_write_wait();
1681 }
Darren Tucker12b4a652009-06-21 18:14:48 +10001682
Ben Lindstrom601e4362001-06-21 03:19:23 +00001683 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +11001684
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001685 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001686 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001687
1688 /* restore blocking io */
1689 if (!isatty(fileno(stdin)))
1690 unset_nonblock(fileno(stdin));
1691 if (!isatty(fileno(stdout)))
1692 unset_nonblock(fileno(stdout));
1693 if (!isatty(fileno(stderr)))
1694 unset_nonblock(fileno(stderr));
1695
Damien Millerd6965512003-12-17 16:31:53 +11001696 /*
1697 * If there was no shell or command requested, there will be no remote
1698 * exit status to be returned. In that case, clear error code if the
1699 * connection was deliberately terminated at this end.
1700 */
1701 if (no_shell_flag && received_signal == SIGTERM) {
1702 received_signal = 0;
1703 exit_status = 0;
1704 }
1705
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001706 if (received_signal)
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001707 fatal("Killed by signal %d.", (int) received_signal);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001708
1709 /*
1710 * In interactive mode (with pseudo tty) display a message indicating
1711 * that the connection has been closed.
1712 */
1713 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001714 snprintf(buf, sizeof buf,
1715 "Connection to %.64s closed.\r\n", host);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001716 buffer_append(&stderr_buffer, buf, strlen(buf));
1717 }
1718
Damien Miller95def091999-11-25 00:26:21 +11001719 /* Output any buffered data for stdout. */
Damien Millercfd6e4f2011-01-16 23:18:33 +11001720 if (buffer_len(&stdout_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001721 len = atomicio(vwrite, fileno(stdout),
1722 buffer_ptr(&stdout_buffer), buffer_len(&stdout_buffer));
Damien Millercfd6e4f2011-01-16 23:18:33 +11001723 if (len < 0 || (u_int)len != buffer_len(&stdout_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001724 error("Write failed flushing stdout buffer.");
Damien Millercfd6e4f2011-01-16 23:18:33 +11001725 else
1726 buffer_consume(&stdout_buffer, len);
Damien Miller95def091999-11-25 00:26:21 +11001727 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001728
Damien Miller95def091999-11-25 00:26:21 +11001729 /* Output any buffered data for stderr. */
Damien Millercfd6e4f2011-01-16 23:18:33 +11001730 if (buffer_len(&stderr_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001731 len = atomicio(vwrite, fileno(stderr),
1732 buffer_ptr(&stderr_buffer), buffer_len(&stderr_buffer));
Damien Millercfd6e4f2011-01-16 23:18:33 +11001733 if (len < 0 || (u_int)len != buffer_len(&stderr_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001734 error("Write failed flushing stderr buffer.");
Damien Millercfd6e4f2011-01-16 23:18:33 +11001735 else
1736 buffer_consume(&stderr_buffer, len);
Damien Miller95def091999-11-25 00:26:21 +11001737 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001738
Damien Miller95def091999-11-25 00:26:21 +11001739 /* Clear and free any buffers. */
1740 memset(buf, 0, sizeof(buf));
1741 buffer_free(&stdin_buffer);
1742 buffer_free(&stdout_buffer);
1743 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001744
Damien Miller95def091999-11-25 00:26:21 +11001745 /* Report bytes transferred, and transfer rates. */
1746 total_time = get_current_time() - start_time;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001747 packet_get_bytes(&ibytes, &obytes);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001748 verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
Damien Miller821de0a2011-01-11 17:20:29 +11001749 (unsigned long long)obytes, (unsigned long long)ibytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001750 if (total_time > 0)
Damien Millerb61f3fc2008-07-11 17:36:48 +10001751 verbose("Bytes per second: sent %.1f, received %.1f",
1752 obytes / total_time, ibytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001753 /* Return the exit status of the program. */
1754 debug("Exit status %d", exit_status);
1755 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001756}
Damien Millerb38eff82000-04-01 11:09:21 +10001757
1758/*********/
1759
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001760static int
Damien Miller630d6f42002-01-22 23:17:30 +11001761client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001762{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001763 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001764 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001765 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001766 buffer_append(&stdout_buffer, data, data_len);
Damien Millera5103f42014-02-04 11:20:14 +11001767 explicit_bzero(data, data_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001768 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001769 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001770}
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001771static int
Damien Miller630d6f42002-01-22 23:17:30 +11001772client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001773{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001774 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001775 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001776 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001777 buffer_append(&stderr_buffer, data, data_len);
Damien Millera5103f42014-02-04 11:20:14 +11001778 explicit_bzero(data, data_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001779 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001780 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001781}
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001782static int
Damien Miller630d6f42002-01-22 23:17:30 +11001783client_input_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001784{
Damien Millerb38eff82000-04-01 11:09:21 +10001785 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001786 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001787 /* Acknowledge the exit. */
1788 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1789 packet_send();
1790 /*
1791 * Must wait for packet to be sent since we are
1792 * exiting the loop.
1793 */
1794 packet_write_wait();
1795 /* Flag that we want to exit. */
1796 quit_pending = 1;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001797 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001798}
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001799
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001800static int
Darren Tucker5dcdd212003-10-02 16:17:00 +10001801client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1802{
1803 Channel *c = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001804 int r, remote_id, sock;
Darren Tucker5dcdd212003-10-02 16:17:00 +10001805
1806 /* Read the remote channel number from the message. */
1807 remote_id = packet_get_int();
1808 packet_check_eom();
1809
1810 /*
1811 * Get a connection to the local authentication agent (this may again
1812 * get forwarded).
1813 */
djm@openbsd.org141efe42015-01-14 20:05:27 +00001814 if ((r = ssh_get_authentication_socket(&sock)) != 0 &&
1815 r != SSH_ERR_AGENT_NOT_PRESENT)
1816 debug("%s: ssh_get_authentication_socket: %s",
1817 __func__, ssh_err(r));
1818
Darren Tucker5dcdd212003-10-02 16:17:00 +10001819
1820 /*
1821 * If we could not connect the agent, send an error message back to
1822 * the server. This should never happen unless the agent dies,
1823 * because authentication forwarding is only enabled if we have an
1824 * agent.
1825 */
1826 if (sock >= 0) {
1827 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1828 -1, 0, 0, 0, "authentication agent connection", 1);
1829 c->remote_id = remote_id;
1830 c->force_drain = 1;
1831 }
1832 if (c == NULL) {
1833 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1834 packet_put_int(remote_id);
1835 } else {
1836 /* Send a confirmation to the remote host. */
1837 debug("Forwarding authentication connection.");
1838 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1839 packet_put_int(remote_id);
1840 packet_put_int(c->self);
1841 }
1842 packet_send();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001843 return 0;
Darren Tucker5dcdd212003-10-02 16:17:00 +10001844}
Damien Millerb38eff82000-04-01 11:09:21 +10001845
Ben Lindstrombba81212001-06-25 05:01:22 +00001846static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001847client_request_forwarded_tcpip(const char *request_type, int rchan)
1848{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001849 Channel *c = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001850 char *listen_address, *originator_address;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001851 u_short listen_port, originator_port;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001852
1853 /* Get rest of the packet */
1854 listen_address = packet_get_string(NULL);
1855 listen_port = packet_get_int();
1856 originator_address = packet_get_string(NULL);
1857 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001858 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001859
Damien Miller7acefbb2014-07-18 14:11:24 +10001860 debug("%s: listen %s port %d, originator %s port %d", __func__,
1861 listen_address, listen_port, originator_address, originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001862
Damien Miller4b3ed642014-07-02 15:29:40 +10001863 c = channel_connect_by_listen_address(listen_address, listen_port,
Damien Millerbd740252008-05-19 15:37:09 +10001864 "forwarded-tcpip", originator_address);
1865
Darren Tuckera627d422013-06-02 07:31:17 +10001866 free(originator_address);
1867 free(listen_address);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001868 return c;
1869}
1870
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001871static Channel *
Damien Miller7acefbb2014-07-18 14:11:24 +10001872client_request_forwarded_streamlocal(const char *request_type, int rchan)
1873{
1874 Channel *c = NULL;
1875 char *listen_path;
1876
1877 /* Get the remote path. */
1878 listen_path = packet_get_string(NULL);
1879 /* XXX: Skip reserved field for now. */
1880 if (packet_get_string_ptr(NULL) == NULL)
1881 fatal("%s: packet_get_string_ptr failed", __func__);
1882 packet_check_eom();
1883
1884 debug("%s: %s", __func__, listen_path);
1885
1886 c = channel_connect_by_listen_path(listen_path,
1887 "forwarded-streamlocal@openssh.com", "forwarded-streamlocal");
1888 free(listen_path);
1889 return c;
1890}
1891
1892static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001893client_request_x11(const char *request_type, int rchan)
1894{
1895 Channel *c = NULL;
1896 char *originator;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001897 u_short originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001898 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001899
1900 if (!options.forward_x11) {
1901 error("Warning: ssh server tried X11 forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001902 error("Warning: this is probably a break-in attempt by a "
1903 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001904 return NULL;
1905 }
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001906 if (x11_refuse_time != 0 && (u_int)monotime() >= x11_refuse_time) {
Damien Miller1ab6a512010-06-26 10:02:24 +10001907 verbose("Rejected X11 connection after ForwardX11Timeout "
1908 "expired");
1909 return NULL;
1910 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001911 originator = packet_get_string(NULL);
1912 if (datafellows & SSH_BUG_X11FWD) {
1913 debug2("buggy server: x11 request w/o originator_port");
1914 originator_port = 0;
1915 } else {
1916 originator_port = packet_get_int();
1917 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001918 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001919 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001920 debug("client_request_x11: request from %s %d", originator,
1921 originator_port);
Darren Tuckera627d422013-06-02 07:31:17 +10001922 free(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001923 sock = x11_connect_display();
1924 if (sock < 0)
1925 return NULL;
1926 c = channel_new("x11",
1927 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001928 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001929 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001930 return c;
1931}
1932
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001933static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001934client_request_agent(const char *request_type, int rchan)
1935{
1936 Channel *c = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001937 int r, sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001938
1939 if (!options.forward_agent) {
1940 error("Warning: ssh server tried agent forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001941 error("Warning: this is probably a break-in attempt by a "
1942 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001943 return NULL;
1944 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001945 if ((r = ssh_get_authentication_socket(&sock)) != 0) {
1946 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1947 debug("%s: ssh_get_authentication_socket: %s",
1948 __func__, ssh_err(r));
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001949 return NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001950 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001951 c = channel_new("authentication agent connection",
1952 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001953 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001954 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001955 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001956 return c;
1957}
1958
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001959int
1960client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun)
1961{
1962 Channel *c;
1963 int fd;
1964
1965 if (tun_mode == SSH_TUNMODE_NO)
1966 return 0;
1967
1968 if (!compat20) {
Damien Millerb3f2c9f2009-01-28 16:15:30 +11001969 error("Tunnel forwarding is not supported for protocol 1");
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001970 return -1;
1971 }
1972
1973 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1974
1975 /* Open local tunnel device */
1976 if ((fd = tun_open(local_tun, tun_mode)) == -1) {
1977 error("Tunnel device open failed.");
1978 return -1;
1979 }
1980
1981 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1982 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1983 c->datagram = 1;
1984
1985#if defined(SSH_TUN_FILTER)
1986 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1987 channel_register_filter(c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +10001988 sys_tun_outfilter, NULL, NULL);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001989#endif
1990
1991 packet_start(SSH2_MSG_CHANNEL_OPEN);
1992 packet_put_cstring("tun@openssh.com");
1993 packet_put_int(c->self);
1994 packet_put_int(c->local_window_max);
1995 packet_put_int(c->local_maxpacket);
1996 packet_put_int(tun_mode);
1997 packet_put_int(remote_tun);
1998 packet_send();
1999
2000 return 0;
2001}
2002
Damien Millerbd483e72000-04-30 10:00:53 +10002003/* XXXX move to generic input handler */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002004static int
Damien Miller630d6f42002-01-22 23:17:30 +11002005client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10002006{
2007 Channel *c = NULL;
2008 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10002009 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002010 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10002011
2012 ctype = packet_get_string(&len);
2013 rchan = packet_get_int();
2014 rwindow = packet_get_int();
2015 rmaxpack = packet_get_int();
2016
Damien Millere247cc42000-05-07 12:03:14 +10002017 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10002018 ctype, rchan, rwindow, rmaxpack);
2019
Damien Miller0bc1bd82000-11-13 22:57:25 +11002020 if (strcmp(ctype, "forwarded-tcpip") == 0) {
2021 c = client_request_forwarded_tcpip(ctype, rchan);
Damien Miller7acefbb2014-07-18 14:11:24 +10002022 } else if (strcmp(ctype, "forwarded-streamlocal@openssh.com") == 0) {
2023 c = client_request_forwarded_streamlocal(ctype, rchan);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002024 } else if (strcmp(ctype, "x11") == 0) {
2025 c = client_request_x11(ctype, rchan);
2026 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
2027 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10002028 }
2029/* XXX duplicate : */
2030 if (c != NULL) {
2031 debug("confirm %s", ctype);
2032 c->remote_id = rchan;
2033 c->remote_window = rwindow;
2034 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00002035 if (c->type != SSH_CHANNEL_CONNECTING) {
2036 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
2037 packet_put_int(c->remote_id);
2038 packet_put_int(c->self);
2039 packet_put_int(c->local_window);
2040 packet_put_int(c->local_maxpacket);
2041 packet_send();
2042 }
Damien Millerbd483e72000-04-30 10:00:53 +10002043 } else {
2044 debug("failure %s", ctype);
2045 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
2046 packet_put_int(rchan);
2047 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00002048 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00002049 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00002050 packet_put_cstring("");
2051 }
Damien Millerbd483e72000-04-30 10:00:53 +10002052 packet_send();
2053 }
Darren Tuckera627d422013-06-02 07:31:17 +10002054 free(ctype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002055 return 0;
Damien Millerbd483e72000-04-30 10:00:53 +10002056}
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002057
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002058static int
Damien Miller630d6f42002-01-22 23:17:30 +11002059client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00002060{
2061 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002062 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00002063 char *rtype;
2064
2065 id = packet_get_int();
2066 rtype = packet_get_string(NULL);
2067 reply = packet_get_char();
2068
2069 debug("client_input_channel_req: channel %d rtype %s reply %d",
2070 id, rtype, reply);
2071
Damien Miller3bbd8782004-06-18 22:23:22 +10002072 if (id == -1) {
2073 error("client_input_channel_req: request for channel -1");
2074 } else if ((c = channel_lookup(id)) == NULL) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10002075 error("client_input_channel_req: channel %d: "
2076 "unknown channel", id);
Damien Millerbab9bd42008-05-19 16:06:47 +10002077 } else if (strcmp(rtype, "eow@openssh.com") == 0) {
2078 packet_check_eom();
2079 chan_rcvd_eow(c);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002080 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10002081 exitval = packet_get_int();
Damien Millere1537f92010-01-26 13:26:22 +11002082 if (c->ctl_chan != -1) {
2083 mux_exit_message(c, exitval);
Damien Miller0e220db2004-06-15 10:34:08 +10002084 success = 1;
Darren Tucker29440822010-01-08 17:08:35 +11002085 } else if (id == session_ident) {
2086 /* Record exit value of local session */
2087 success = 1;
2088 exit_status = exitval;
2089 } else {
Damien Miller36f57eb2010-01-30 17:28:34 +11002090 /* Probably for a mux channel that has already closed */
2091 debug("%s: no sink for exit-status on channel %d",
2092 __func__, id);
Damien Miller0e220db2004-06-15 10:34:08 +10002093 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002094 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00002095 }
Damien Millerc5893782014-05-15 13:48:49 +10002096 if (reply && c != NULL && !(c->flags & CHAN_CLOSE_SENT)) {
Ben Lindstrom5b828322001-02-09 01:34:36 +00002097 packet_start(success ?
2098 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller8533c782008-12-08 09:54:40 +11002099 packet_put_int(c->remote_id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002100 packet_send();
2101 }
Darren Tuckera627d422013-06-02 07:31:17 +10002102 free(rtype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002103 return 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00002104}
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002105
djm@openbsd.org523463a2015-02-16 22:13:32 +00002106struct hostkeys_update_ctx {
2107 /* The hostname and (optionally) IP address string for the server */
2108 char *host_str, *ip_str;
2109
2110 /*
2111 * Keys received from the server and a flag for each indicating
2112 * whether they already exist in known_hosts.
2113 * keys_seen is filled in by hostkeys_find() and later (for new
2114 * keys) by client_global_hostkeys_private_confirm().
2115 */
2116 struct sshkey **keys;
2117 int *keys_seen;
2118 size_t nkeys;
2119
2120 size_t nnew;
2121
2122 /*
2123 * Keys that are in known_hosts, but were not present in the update
2124 * from the server (i.e. scheduled to be deleted).
2125 * Filled in by hostkeys_find().
2126 */
2127 struct sshkey **old_keys;
2128 size_t nold;
2129};
2130
2131static void
2132hostkeys_update_ctx_free(struct hostkeys_update_ctx *ctx)
2133{
2134 size_t i;
2135
2136 if (ctx == NULL)
2137 return;
2138 for (i = 0; i < ctx->nkeys; i++)
2139 sshkey_free(ctx->keys[i]);
2140 free(ctx->keys);
2141 free(ctx->keys_seen);
2142 for (i = 0; i < ctx->nold; i++)
2143 sshkey_free(ctx->old_keys[i]);
2144 free(ctx->old_keys);
2145 free(ctx->host_str);
2146 free(ctx->ip_str);
2147 free(ctx);
2148}
2149
2150static int
2151hostkeys_find(struct hostkey_foreach_line *l, void *_ctx)
2152{
2153 struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
2154 size_t i;
2155 struct sshkey **tmp;
2156
2157 if (l->status != HKF_STATUS_MATCHED || l->key == NULL ||
2158 l->key->type == KEY_RSA1)
2159 return 0;
2160
2161 /* Mark off keys we've already seen for this host */
2162 for (i = 0; i < ctx->nkeys; i++) {
2163 if (sshkey_equal(l->key, ctx->keys[i])) {
2164 debug3("%s: found %s key at %s:%ld", __func__,
2165 sshkey_ssh_name(ctx->keys[i]), l->path, l->linenum);
2166 ctx->keys_seen[i] = 1;
2167 return 0;
2168 }
2169 }
2170 /* This line contained a key that not offered by the server */
2171 debug3("%s: deprecated %s key at %s:%ld", __func__,
2172 sshkey_ssh_name(l->key), l->path, l->linenum);
2173 if ((tmp = reallocarray(ctx->old_keys, ctx->nold + 1,
2174 sizeof(*ctx->old_keys))) == NULL)
2175 fatal("%s: reallocarray failed nold = %zu",
2176 __func__, ctx->nold);
2177 ctx->old_keys = tmp;
2178 ctx->old_keys[ctx->nold++] = l->key;
2179 l->key = NULL;
2180
2181 return 0;
2182}
2183
2184static void
2185update_known_hosts(struct hostkeys_update_ctx *ctx)
2186{
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00002187 int r, was_raw = 0;
2188 int loglevel = options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK ?
djm@openbsd.org523463a2015-02-16 22:13:32 +00002189 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_VERBOSE;
2190 char *fp, *response;
2191 size_t i;
2192
2193 for (i = 0; i < ctx->nkeys; i++) {
2194 if (ctx->keys_seen[i] != 2)
2195 continue;
2196 if ((fp = sshkey_fingerprint(ctx->keys[i],
2197 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
2198 fatal("%s: sshkey_fingerprint failed", __func__);
2199 do_log2(loglevel, "Learned new hostkey: %s %s",
2200 sshkey_type(ctx->keys[i]), fp);
2201 free(fp);
2202 }
2203 for (i = 0; i < ctx->nold; i++) {
2204 if ((fp = sshkey_fingerprint(ctx->old_keys[i],
2205 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
2206 fatal("%s: sshkey_fingerprint failed", __func__);
2207 do_log2(loglevel, "Deprecating obsolete hostkey: %s %s",
2208 sshkey_type(ctx->old_keys[i]), fp);
2209 free(fp);
2210 }
2211 if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK) {
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00002212 if (get_saved_tio() != NULL) {
2213 leave_raw_mode(1);
2214 was_raw = 1;
2215 }
djm@openbsd.org523463a2015-02-16 22:13:32 +00002216 response = NULL;
2217 for (i = 0; !quit_pending && i < 3; i++) {
2218 free(response);
2219 response = read_passphrase("Accept updated hostkeys? "
2220 "(yes/no): ", RP_ECHO);
2221 if (strcasecmp(response, "yes") == 0)
2222 break;
2223 else if (quit_pending || response == NULL ||
2224 strcasecmp(response, "no") == 0) {
2225 options.update_hostkeys = 0;
2226 break;
2227 } else {
2228 do_log2(loglevel, "Please enter "
2229 "\"yes\" or \"no\"");
2230 }
2231 }
2232 if (quit_pending || i >= 3 || response == NULL)
2233 options.update_hostkeys = 0;
2234 free(response);
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00002235 if (was_raw)
2236 enter_raw_mode(1);
djm@openbsd.org523463a2015-02-16 22:13:32 +00002237 }
2238
2239 /*
2240 * Now that all the keys are verified, we can go ahead and replace
2241 * them in known_hosts (assuming SSH_UPDATE_HOSTKEYS_ASK didn't
2242 * cancel the operation).
2243 */
2244 if (options.update_hostkeys != 0 &&
2245 (r = hostfile_replace_entries(options.user_hostfiles[0],
2246 ctx->host_str, ctx->ip_str, ctx->keys, ctx->nkeys,
2247 options.hash_known_hosts, 0,
2248 options.fingerprint_hash)) != 0)
2249 error("%s: hostfile_replace_entries failed: %s",
2250 __func__, ssh_err(r));
2251}
2252
2253static void
2254client_global_hostkeys_private_confirm(int type, u_int32_t seq, void *_ctx)
2255{
2256 struct ssh *ssh = active_state; /* XXX */
2257 struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
2258 size_t i, ndone;
2259 struct sshbuf *signdata;
2260 int r;
2261 const u_char *sig;
2262 size_t siglen;
2263
2264 if (ctx->nnew == 0)
2265 fatal("%s: ctx->nnew == 0", __func__); /* sanity */
2266 if (type != SSH2_MSG_REQUEST_SUCCESS) {
2267 error("Server failed to confirm ownership of "
2268 "private host keys");
2269 hostkeys_update_ctx_free(ctx);
2270 return;
2271 }
2272 if ((signdata = sshbuf_new()) == NULL)
2273 fatal("%s: sshbuf_new failed", __func__);
2274 /* Don't want to accidentally accept an unbound signature */
2275 if (ssh->kex->session_id_len == 0)
2276 fatal("%s: ssh->kex->session_id_len == 0", __func__);
2277 /*
2278 * Expect a signature for each of the ctx->nnew private keys we
2279 * haven't seen before. They will be in the same order as the
2280 * ctx->keys where the corresponding ctx->keys_seen[i] == 0.
2281 */
2282 for (ndone = i = 0; i < ctx->nkeys; i++) {
2283 if (ctx->keys_seen[i])
2284 continue;
2285 /* Prepare data to be signed: session ID, unique string, key */
2286 sshbuf_reset(signdata);
djm@openbsd.org44732de2015-02-20 22:17:21 +00002287 if ( (r = sshbuf_put_cstring(signdata,
2288 "hostkeys-prove-00@openssh.com")) != 0 ||
2289 (r = sshbuf_put_string(signdata, ssh->kex->session_id,
djm@openbsd.org523463a2015-02-16 22:13:32 +00002290 ssh->kex->session_id_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +00002291 (r = sshkey_puts(ctx->keys[i], signdata)) != 0)
2292 fatal("%s: failed to prepare signature: %s",
2293 __func__, ssh_err(r));
2294 /* Extract and verify signature */
2295 if ((r = sshpkt_get_string_direct(ssh, &sig, &siglen)) != 0) {
2296 error("%s: couldn't parse message: %s",
2297 __func__, ssh_err(r));
2298 goto out;
2299 }
2300 if ((r = sshkey_verify(ctx->keys[i], sig, siglen,
2301 sshbuf_ptr(signdata), sshbuf_len(signdata), 0)) != 0) {
2302 error("%s: server gave bad signature for %s key %zu",
2303 __func__, sshkey_type(ctx->keys[i]), i);
2304 goto out;
2305 }
2306 /* Key is good. Mark it as 'seen' */
2307 ctx->keys_seen[i] = 2;
2308 ndone++;
2309 }
2310 if (ndone != ctx->nnew)
2311 fatal("%s: ndone != ctx->nnew (%zu / %zu)", __func__,
2312 ndone, ctx->nnew); /* Shouldn't happen */
2313 ssh_packet_check_eom(ssh);
2314
2315 /* Make the edits to known_hosts */
2316 update_known_hosts(ctx);
2317 out:
2318 hostkeys_update_ctx_free(ctx);
2319}
2320
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002321/*
djm@openbsd.org44732de2015-02-20 22:17:21 +00002322 * Handle hostkeys-00@openssh.com global request to inform the client of all
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002323 * the server's hostkeys. The keys are checked against the user's
2324 * HostkeyAlgorithms preference before they are accepted.
2325 */
2326static int
2327client_input_hostkeys(void)
2328{
djm@openbsd.org523463a2015-02-16 22:13:32 +00002329 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002330 const u_char *blob = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002331 size_t i, len = 0;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002332 struct sshbuf *buf = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002333 struct sshkey *key = NULL, **tmp;
2334 int r;
2335 char *fp;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002336 static int hostkeys_seen = 0; /* XXX use struct ssh */
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00002337 extern struct sockaddr_storage hostaddr; /* XXX from ssh.c */
djm@openbsd.orga63cfa22015-02-25 19:54:02 +00002338 struct hostkeys_update_ctx *ctx = NULL;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002339
2340 if (hostkeys_seen)
2341 fatal("%s: server already sent hostkeys", __func__);
djm@openbsd.org523463a2015-02-16 22:13:32 +00002342 if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK &&
2343 options.batch_mode)
2344 return 1; /* won't ask in batchmode, so don't even try */
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002345 if (!options.update_hostkeys || options.num_user_hostfiles <= 0)
2346 return 1;
djm@openbsd.orga63cfa22015-02-25 19:54:02 +00002347
2348 ctx = xcalloc(1, sizeof(*ctx));
djm@openbsd.org523463a2015-02-16 22:13:32 +00002349 while (ssh_packet_remaining(ssh) > 0) {
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002350 sshkey_free(key);
2351 key = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002352 if ((r = sshpkt_get_string_direct(ssh, &blob, &len)) != 0) {
2353 error("%s: couldn't parse message: %s",
2354 __func__, ssh_err(r));
2355 goto out;
2356 }
djm@openbsd.org44732de2015-02-20 22:17:21 +00002357 if ((r = sshkey_from_blob(blob, len, &key)) != 0) {
2358 error("%s: parse key: %s", __func__, ssh_err(r));
2359 goto out;
2360 }
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002361 fp = sshkey_fingerprint(key, options.fingerprint_hash,
2362 SSH_FP_DEFAULT);
2363 debug3("%s: received %s key %s", __func__,
2364 sshkey_type(key), fp);
2365 free(fp);
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00002366
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002367 /* Check that the key is accepted in HostkeyAlgorithms */
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00002368 if (match_pattern_list(sshkey_ssh_name(key),
2369 options.hostkeyalgorithms ? options.hostkeyalgorithms :
2370 KEX_DEFAULT_PK_ALG, 0) != 1) {
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002371 debug3("%s: %s key not permitted by HostkeyAlgorithms",
2372 __func__, sshkey_ssh_name(key));
2373 continue;
2374 }
djm@openbsd.org523463a2015-02-16 22:13:32 +00002375 /* Skip certs */
2376 if (sshkey_is_cert(key)) {
2377 debug3("%s: %s key is a certificate; skipping",
2378 __func__, sshkey_ssh_name(key));
2379 continue;
2380 }
2381 /* Ensure keys are unique */
2382 for (i = 0; i < ctx->nkeys; i++) {
2383 if (sshkey_equal(key, ctx->keys[i])) {
2384 error("%s: received duplicated %s host key",
2385 __func__, sshkey_ssh_name(key));
2386 goto out;
2387 }
2388 }
2389 /* Key is good, record it */
2390 if ((tmp = reallocarray(ctx->keys, ctx->nkeys + 1,
2391 sizeof(*ctx->keys))) == NULL)
2392 fatal("%s: reallocarray failed nkeys = %zu",
2393 __func__, ctx->nkeys);
2394 ctx->keys = tmp;
2395 ctx->keys[ctx->nkeys++] = key;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002396 key = NULL;
2397 }
2398
djm@openbsd.org523463a2015-02-16 22:13:32 +00002399 if (ctx->nkeys == 0) {
djm@openbsd.org44732de2015-02-20 22:17:21 +00002400 debug("%s: server sent no hostkeys", __func__);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002401 goto out;
2402 }
djm@openbsd.org44732de2015-02-20 22:17:21 +00002403
djm@openbsd.org523463a2015-02-16 22:13:32 +00002404 if ((ctx->keys_seen = calloc(ctx->nkeys,
2405 sizeof(*ctx->keys_seen))) == NULL)
2406 fatal("%s: calloc failed", __func__);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002407
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00002408 get_hostfile_hostname_ipaddr(host,
2409 options.check_host_ip ? (struct sockaddr *)&hostaddr : NULL,
djm@openbsd.org523463a2015-02-16 22:13:32 +00002410 options.port, &ctx->host_str,
2411 options.check_host_ip ? &ctx->ip_str : NULL);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002412
djm@openbsd.org523463a2015-02-16 22:13:32 +00002413 /* Find which keys we already know about. */
2414 if ((r = hostkeys_foreach(options.user_hostfiles[0], hostkeys_find,
2415 ctx, ctx->host_str, ctx->ip_str,
2416 HKF_WANT_PARSE_KEY|HKF_WANT_MATCH)) != 0) {
2417 error("%s: hostkeys_foreach failed: %s", __func__, ssh_err(r));
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002418 goto out;
2419 }
2420
djm@openbsd.org523463a2015-02-16 22:13:32 +00002421 /* Figure out if we have any new keys to add */
2422 ctx->nnew = 0;
2423 for (i = 0; i < ctx->nkeys; i++) {
2424 if (!ctx->keys_seen[i])
2425 ctx->nnew++;
2426 }
2427
2428 debug3("%s: %zu keys from server: %zu new, %zu retained. %zu to remove",
2429 __func__, ctx->nkeys, ctx->nnew, ctx->nkeys - ctx->nnew, ctx->nold);
2430
2431 if (ctx->nnew == 0 && ctx->nold != 0) {
2432 /* We have some keys to remove. Just do it. */
2433 update_known_hosts(ctx);
2434 } else if (ctx->nnew != 0) {
2435 /*
2436 * We have received hitherto-unseen keys from the server.
2437 * Ask the server to confirm ownership of the private halves.
2438 */
2439 debug3("%s: asking server to prove ownership for %zu keys",
2440 __func__, ctx->nnew);
2441 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
2442 (r = sshpkt_put_cstring(ssh,
djm@openbsd.org44732de2015-02-20 22:17:21 +00002443 "hostkeys-prove-00@openssh.com")) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +00002444 (r = sshpkt_put_u8(ssh, 1)) != 0) /* bool: want reply */
2445 fatal("%s: cannot prepare packet: %s",
2446 __func__, ssh_err(r));
2447 if ((buf = sshbuf_new()) == NULL)
2448 fatal("%s: sshbuf_new", __func__);
2449 for (i = 0; i < ctx->nkeys; i++) {
2450 if (ctx->keys_seen[i])
2451 continue;
2452 sshbuf_reset(buf);
2453 if ((r = sshkey_putb(ctx->keys[i], buf)) != 0)
2454 fatal("%s: sshkey_putb: %s",
2455 __func__, ssh_err(r));
2456 if ((r = sshpkt_put_stringb(ssh, buf)) != 0)
2457 fatal("%s: sshpkt_put_string: %s",
2458 __func__, ssh_err(r));
2459 }
2460 if ((r = sshpkt_send(ssh)) != 0)
2461 fatal("%s: sshpkt_send: %s", __func__, ssh_err(r));
2462 client_register_global_confirm(
2463 client_global_hostkeys_private_confirm, ctx);
2464 ctx = NULL; /* will be freed in callback */
2465 }
2466
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002467 /* Success */
2468 out:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002469 hostkeys_update_ctx_free(ctx);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002470 sshkey_free(key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002471 sshbuf_free(buf);
djm@openbsd.org523463a2015-02-16 22:13:32 +00002472 /*
2473 * NB. Return success for all cases. The server doesn't need to know
2474 * what the client does with its hosts file.
2475 */
2476 return 1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002477}
2478
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002479static int
Damien Millerc3fa4072002-01-22 23:21:58 +11002480client_input_global_request(int type, u_int32_t seq, void *ctxt)
2481{
2482 char *rtype;
2483 int want_reply;
2484 int success = 0;
2485
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002486 rtype = packet_get_cstring(NULL);
Damien Millerc3fa4072002-01-22 23:21:58 +11002487 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11002488 debug("client_input_global_request: rtype %s want_reply %d",
2489 rtype, want_reply);
djm@openbsd.org44732de2015-02-20 22:17:21 +00002490 if (strcmp(rtype, "hostkeys-00@openssh.com") == 0)
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002491 success = client_input_hostkeys();
Damien Millerc3fa4072002-01-22 23:21:58 +11002492 if (want_reply) {
2493 packet_start(success ?
2494 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
2495 packet_send();
2496 packet_write_wait();
2497 }
Darren Tuckera627d422013-06-02 07:31:17 +10002498 free(rtype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002499 return 0;
Damien Millerc3fa4072002-01-22 23:21:58 +11002500}
Damien Millerbd483e72000-04-30 10:00:53 +10002501
Damien Miller0e220db2004-06-15 10:34:08 +10002502void
Darren Tuckerfc959702004-07-17 16:12:08 +10002503client_session2_setup(int id, int want_tty, int want_subsystem,
Damien Miller5771ed72008-05-19 15:35:33 +10002504 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env)
Damien Miller0e220db2004-06-15 10:34:08 +10002505{
2506 int len;
Darren Tucker5d78de62004-11-05 20:35:44 +11002507 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002508
2509 debug2("%s: id %d", __func__, id);
2510
Darren Tucker5d78de62004-11-05 20:35:44 +11002511 if ((c = channel_lookup(id)) == NULL)
2512 fatal("client_session2_setup: channel %d: unknown channel", id);
2513
Damien Miller0dac6fb2010-11-20 15:19:38 +11002514 packet_set_interactive(want_tty,
2515 options.ip_qos_interactive, options.ip_qos_bulk);
2516
Damien Miller0e220db2004-06-15 10:34:08 +10002517 if (want_tty) {
2518 struct winsize ws;
Damien Miller0e220db2004-06-15 10:34:08 +10002519
2520 /* Store window size in the packet. */
2521 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
2522 memset(&ws, 0, sizeof(ws));
2523
Damien Miller5771ed72008-05-19 15:35:33 +10002524 channel_request_start(id, "pty-req", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002525 client_expect_confirm(id, "PTY allocation", CONFIRM_TTY);
Damien Miller0e220db2004-06-15 10:34:08 +10002526 packet_put_cstring(term != NULL ? term : "");
Damien Miller71a73672006-03-26 14:04:36 +11002527 packet_put_int((u_int)ws.ws_col);
2528 packet_put_int((u_int)ws.ws_row);
2529 packet_put_int((u_int)ws.ws_xpixel);
2530 packet_put_int((u_int)ws.ws_ypixel);
Darren Tuckerdf189fb2008-06-08 12:55:32 +10002531 if (tiop == NULL)
2532 tiop = get_saved_tio();
2533 tty_make_modes(-1, tiop);
Damien Miller0e220db2004-06-15 10:34:08 +10002534 packet_send();
2535 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11002536 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10002537 }
2538
2539 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10002540 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10002541 int i, j, matched;
Damien Miller0e220db2004-06-15 10:34:08 +10002542 char *name, *val;
2543
2544 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10002545 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10002546 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10002547 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10002548 if ((val = strchr(name, '=')) == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +10002549 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002550 continue;
2551 }
2552 *val++ = '\0';
2553
2554 matched = 0;
2555 for (j = 0; j < options.num_send_env; j++) {
2556 if (match_pattern(name, options.send_env[j])) {
2557 matched = 1;
2558 break;
2559 }
2560 }
2561 if (!matched) {
2562 debug3("Ignored env %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10002563 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002564 continue;
2565 }
2566
2567 debug("Sending env %s = %s", name, val);
2568 channel_request_start(id, "env", 0);
2569 packet_put_cstring(name);
2570 packet_put_cstring(val);
2571 packet_send();
Darren Tuckera627d422013-06-02 07:31:17 +10002572 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002573 }
2574 }
2575
2576 len = buffer_len(cmd);
2577 if (len > 0) {
2578 if (len > 900)
2579 len = 900;
2580 if (want_subsystem) {
Damien Miller5771ed72008-05-19 15:35:33 +10002581 debug("Sending subsystem: %.*s",
2582 len, (u_char*)buffer_ptr(cmd));
2583 channel_request_start(id, "subsystem", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002584 client_expect_confirm(id, "subsystem", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002585 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002586 debug("Sending command: %.*s",
2587 len, (u_char*)buffer_ptr(cmd));
2588 channel_request_start(id, "exec", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002589 client_expect_confirm(id, "exec", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002590 }
2591 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
2592 packet_send();
2593 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002594 channel_request_start(id, "shell", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002595 client_expect_confirm(id, "shell", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002596 packet_send();
2597 }
2598}
2599
Ben Lindstrombba81212001-06-25 05:01:22 +00002600static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002601client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10002602{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002603 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10002604
Damien Miller1383bd82000-04-06 12:32:37 +10002605 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2606 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2607 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2608 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10002609 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10002610 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2611 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002612 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10002613 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerb84886b2008-05-19 15:05:07 +10002614 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2615 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
Damien Millerc3fa4072002-01-22 23:21:58 +11002616 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002617
2618 /* rekeying */
2619 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10002620
2621 /* global request reply messages */
2622 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2623 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10002624}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002625
Ben Lindstrombba81212001-06-25 05:01:22 +00002626static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002627client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002628{
2629 dispatch_init(NULL);
2630 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
2631 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
2632 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10002633 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2634 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
2635 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002636 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
2637 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
2638 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11002639
2640 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
Darren Tucker5dcdd212003-10-02 16:17:00 +10002641 &client_input_agent_open : &deny_input_open);
Damien Miller69b69aa2000-10-28 14:19:58 +11002642 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
2643 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002644}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002645
Ben Lindstrombba81212001-06-25 05:01:22 +00002646static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002647client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002648{
2649 client_init_dispatch_13();
2650 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
2651 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
2652}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002653
Ben Lindstromdb47f382001-07-04 05:10:27 +00002654static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002655client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002656{
Damien Miller1383bd82000-04-06 12:32:37 +10002657 if (compat20)
2658 client_init_dispatch_20();
2659 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10002660 client_init_dispatch_13();
2661 else
2662 client_init_dispatch_15();
2663}
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002664
Damien Miller6c3eec72011-05-05 14:16:22 +10002665void
2666client_stop_mux(void)
2667{
2668 if (options.control_path != NULL && muxserver_sock != -1)
2669 unlink(options.control_path);
2670 /*
Darren Tucker48bf4b02012-09-07 16:38:53 +10002671 * If we are in persist mode, or don't have a shell, signal that we
2672 * should close when all active channels are closed.
Damien Miller6c3eec72011-05-05 14:16:22 +10002673 */
Darren Tucker48bf4b02012-09-07 16:38:53 +10002674 if (options.control_persist || no_shell_flag) {
Damien Miller6c3eec72011-05-05 14:16:22 +10002675 session_closed = 1;
Damien Miller4ac99c32011-06-20 14:43:31 +10002676 setproctitle("[stopped mux]");
2677 }
Damien Miller6c3eec72011-05-05 14:16:22 +10002678}
2679
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002680/* client specific fatal cleanup */
2681void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002682cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002683{
Damien Miller21771e22011-05-15 08:45:50 +10002684 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002685 leave_non_blocking();
Damien Millerb1cbfa22008-05-19 16:00:08 +10002686 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10002687 unlink(options.control_path);
Damien Millera41ccca2010-10-07 22:07:32 +11002688 ssh_kill_proxy_command();
Darren Tucker3e33cec2003-10-02 16:12:36 +10002689 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002690}