blob: dc0e557ad67850ad717bce0183005b44f4a1c7ff [file] [log] [blame]
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001/* $OpenBSD: clientloop.c,v 1.274 2015/07/01 02:26:31 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * The main loop for the interactive session (client side).
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
14 *
15 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 *
Damien Miller1383bd82000-04-06 12:32:37 +100038 * SSH2 support added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000039 * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110040 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110060 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
62#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110063
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"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000103#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +1000104#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000105#include "readconf.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000106#include "clientloop.h"
Damien Milleraeb31d62005-12-13 19:29:36 +1100107#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000108#include "authfd.h"
109#include "atomicio.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +1000110#include "sshpty.h"
Damien Miller0e220db2004-06-15 10:34:08 +1000111#include "match.h"
112#include "msg.h"
Darren Tuckerc5564e12009-06-21 18:53:53 +1000113#include "roaming.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +0000114#include "ssherr.h"
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000115#include "hostfile.h"
Damien Miller69b69aa2000-10-28 14:19:58 +1100116
117/* import options */
118extern Options options;
119
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120/* Flag indicating that stdin should be redirected from /dev/null. */
121extern int stdin_null_flag;
122
Damien Millerd6965512003-12-17 16:31:53 +1100123/* Flag indicating that no shell has been requested */
124extern int no_shell_flag;
125
Damien Miller0e220db2004-06-15 10:34:08 +1000126/* Control socket */
Damien Millere1537f92010-01-26 13:26:22 +1100127extern int muxserver_sock; /* XXX use mux_client_cleanup() instead */
Damien Miller0e220db2004-06-15 10:34:08 +1000128
Damien Miller5428f641999-11-25 11:54:57 +1100129/*
130 * Name of the host we are connecting to. This is the name given on the
131 * command line, or the HostName specified for the user-supplied name in a
132 * configuration file.
133 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134extern char *host;
135
Damien Miller5428f641999-11-25 11:54:57 +1100136/*
137 * Flag to indicate that we have received a window change signal which has
138 * not yet been processed. This will cause a message indicating the new
139 * window size to be sent to the server a little later. This is volatile
140 * because this is updated in a signal handler.
141 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000142static volatile sig_atomic_t received_window_change_signal = 0;
143static volatile sig_atomic_t received_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144
Damien Miller788f2122005-11-05 15:14:59 +1100145/* Flag indicating whether the user's terminal is in non-blocking mode. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146static int in_non_blocking_mode = 0;
147
Damien Millere11e1ea2010-08-03 16:04:46 +1000148/* Time when backgrounded control master using ControlPersist should exit */
149static time_t control_persist_exit_time = 0;
150
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151/* Common data for the client loop code. */
Damien Millere1537f92010-01-26 13:26:22 +1100152volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000153static int escape_char1; /* Escape character. (proto1 only) */
154static int escape_pending1; /* Last character was an escape (proto1 only) */
Damien Miller95def091999-11-25 00:26:21 +1100155static int last_was_cr; /* Last character was a newline. */
Darren Tucker4d5cd332008-06-13 04:51:14 +1000156static int exit_status; /* Used to store the command exit status. */
157static int stdin_eof; /* EOF has been encountered on stderr. */
Damien Miller95def091999-11-25 00:26:21 +1100158static Buffer stdin_buffer; /* Buffer for stdin data. */
159static Buffer stdout_buffer; /* Buffer for stdout data. */
160static Buffer stderr_buffer; /* Buffer for stderr data. */
Damien Miller1ab6a512010-06-26 10:02:24 +1000161static u_int buffer_high; /* Soft max buffer size. */
Damien Miller95def091999-11-25 00:26:21 +1100162static int connection_in; /* Connection to server (input). */
163static int connection_out; /* Connection to server (output). */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000164static int need_rekeying; /* Set to non-zero if rekeying is requested. */
Damien Miller1ab6a512010-06-26 10:02:24 +1000165static int session_closed; /* In SSH2: login session closed. */
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000166static u_int x11_refuse_time; /* If >0, refuse x11 opens after this time. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000167
Ben Lindstrombba81212001-06-25 05:01:22 +0000168static void client_init_dispatch(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000169int session_ident = -1;
170
Darren Tuckere32cf432010-01-08 16:51:40 +1100171int session_resumed = 0;
172
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000173/* Track escape per proto2 channel */
174struct escape_filter_ctx {
175 int escape_pending;
176 int escape_char;
177};
178
179/* Context for channel confirmation replies */
Damien Miller5771ed72008-05-19 15:35:33 +1000180struct channel_reply_ctx {
181 const char *request_type;
Damien Miller555f3b82011-05-15 08:48:05 +1000182 int id;
183 enum confirm_action action;
Damien Miller5771ed72008-05-19 15:35:33 +1000184};
185
Darren Tucker9f407c42008-06-13 04:50:27 +1000186/* Global request success/failure callbacks */
187struct global_confirm {
188 TAILQ_ENTRY(global_confirm) entry;
189 global_confirm_cb *cb;
190 void *ctx;
191 int ref_count;
192};
193TAILQ_HEAD(global_confirms, global_confirm);
194static struct global_confirms global_confirms =
195 TAILQ_HEAD_INITIALIZER(global_confirms);
196
Damien Miller0e220db2004-06-15 10:34:08 +1000197void ssh_process_session2_setup(int, int, int, Buffer *);
198
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199/* Restores stdin to blocking mode. */
200
Ben Lindstrombba81212001-06-25 05:01:22 +0000201static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000202leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203{
Damien Miller95def091999-11-25 00:26:21 +1100204 if (in_non_blocking_mode) {
Damien Miller03e66f62004-06-15 15:47:51 +1000205 unset_nonblock(fileno(stdin));
Damien Miller95def091999-11-25 00:26:21 +1100206 in_non_blocking_mode = 0;
Damien Miller95def091999-11-25 00:26:21 +1100207 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000208}
209
Damien Miller95def091999-11-25 00:26:21 +1100210/* Puts stdin terminal in non-blocking mode. */
211
Ben Lindstrombba81212001-06-25 05:01:22 +0000212static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000213enter_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214{
Damien Miller95def091999-11-25 00:26:21 +1100215 in_non_blocking_mode = 1;
Damien Miller232711f2004-06-15 10:35:30 +1000216 set_nonblock(fileno(stdin));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217}
218
Damien Miller5428f641999-11-25 11:54:57 +1100219/*
220 * Signal handler for the window change signal (SIGWINCH). This just sets a
221 * flag indicating that the window has changed.
222 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100223/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000224static void
Damien Miller95def091999-11-25 00:26:21 +1100225window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000226{
Damien Miller95def091999-11-25 00:26:21 +1100227 received_window_change_signal = 1;
228 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000229}
230
Damien Miller5428f641999-11-25 11:54:57 +1100231/*
232 * Signal handler for signals that cause the program to terminate. These
233 * signals must be trapped to restore terminal modes.
234 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100235/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000236static void
Damien Miller95def091999-11-25 00:26:21 +1100237signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000239 received_signal = sig;
240 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241}
242
Damien Miller5428f641999-11-25 11:54:57 +1100243/*
244 * Returns current time in seconds from Jan 1, 1970 with the maximum
245 * available resolution.
246 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247
Ben Lindstrombba81212001-06-25 05:01:22 +0000248static double
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000249get_current_time(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250{
Damien Miller95def091999-11-25 00:26:21 +1100251 struct timeval tv;
252 gettimeofday(&tv, NULL);
253 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254}
255
Damien Millere11e1ea2010-08-03 16:04:46 +1000256/*
257 * Sets control_persist_exit_time to the absolute time when the
258 * backgrounded control master should exit due to expiry of the
259 * ControlPersist timeout. Sets it to 0 if we are not a backgrounded
260 * control master process, or if there is no ControlPersist timeout.
261 */
262static void
263set_control_persist_exit_time(void)
264{
265 if (muxserver_sock == -1 || !options.control_persist
Damien Miller6c3eec72011-05-05 14:16:22 +1000266 || options.control_persist_timeout == 0) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000267 /* not using a ControlPersist timeout */
268 control_persist_exit_time = 0;
Damien Miller6c3eec72011-05-05 14:16:22 +1000269 } else if (channel_still_open()) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000270 /* some client connections are still open */
271 if (control_persist_exit_time > 0)
272 debug2("%s: cancel scheduled exit", __func__);
273 control_persist_exit_time = 0;
274 } else if (control_persist_exit_time <= 0) {
275 /* a client connection has recently closed */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000276 control_persist_exit_time = monotime() +
Damien Millere11e1ea2010-08-03 16:04:46 +1000277 (time_t)options.control_persist_timeout;
278 debug2("%s: schedule exit in %d seconds", __func__,
279 options.control_persist_timeout);
280 }
281 /* else we are already counting down to the timeout */
282}
283
Damien Miller8d60be52012-02-11 08:18:17 +1100284#define SSH_X11_VALID_DISPLAY_CHARS ":/.-_"
285static int
286client_x11_display_valid(const char *display)
287{
288 size_t i, dlen;
289
290 dlen = strlen(display);
291 for (i = 0; i < dlen; i++) {
Damien Millerfdb23062013-11-21 13:57:15 +1100292 if (!isalnum((u_char)display[i]) &&
Damien Miller8d60be52012-02-11 08:18:17 +1100293 strchr(SSH_X11_VALID_DISPLAY_CHARS, display[i]) == NULL) {
294 debug("Invalid character '%c' in DISPLAY", display[i]);
295 return 0;
296 }
297 }
298 return 1;
299}
300
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000301#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
302#define X11_TIMEOUT_SLACK 60
Damien Miller17e7ed02005-06-17 12:54:33 +1000303void
304client_x11_get_proto(const char *display, const char *xauth_path,
Damien Miller1ab6a512010-06-26 10:02:24 +1000305 u_int trusted, u_int timeout, char **_proto, char **_data)
Damien Miller17e7ed02005-06-17 12:54:33 +1000306{
307 char cmd[1024];
308 char line[512];
309 char xdisplay[512];
310 static char proto[512], data[512];
311 FILE *f;
312 int got_data = 0, generated = 0, do_unlink = 0, i;
313 char *xauthdir, *xauthfile;
314 struct stat st;
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000315 u_int now, x11_timeout_real;
Damien Miller17e7ed02005-06-17 12:54:33 +1000316
317 xauthdir = xauthfile = NULL;
318 *_proto = proto;
319 *_data = data;
320 proto[0] = data[0] = '\0';
321
322 if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) {
323 debug("No xauth program.");
Damien Miller8d60be52012-02-11 08:18:17 +1100324 } else if (!client_x11_display_valid(display)) {
325 logit("DISPLAY '%s' invalid, falling back to fake xauth data",
326 display);
Damien Miller17e7ed02005-06-17 12:54:33 +1000327 } else {
328 if (display == NULL) {
329 debug("x11_get_proto: DISPLAY not set");
330 return;
331 }
332 /*
333 * Handle FamilyLocal case where $DISPLAY does
334 * not match an authorization entry. For this we
335 * just try "xauth list unix:displaynum.screennum".
336 * XXX: "localhost" match to determine FamilyLocal
337 * is not perfect.
338 */
339 if (strncmp(display, "localhost:", 10) == 0) {
340 snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
341 display + 10);
342 display = xdisplay;
343 }
344 if (trusted == 0) {
deraadt@openbsd.org087266e2015-01-20 23:14:00 +0000345 xauthdir = xmalloc(PATH_MAX);
346 xauthfile = xmalloc(PATH_MAX);
347 mktemp_proto(xauthdir, PATH_MAX);
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000348 /*
349 * The authentication cookie should briefly outlive
350 * ssh's willingness to forward X11 connections to
351 * avoid nasty fail-open behaviour in the X server.
352 */
353 if (timeout >= UINT_MAX - X11_TIMEOUT_SLACK)
354 x11_timeout_real = UINT_MAX;
355 else
356 x11_timeout_real = timeout + X11_TIMEOUT_SLACK;
Damien Miller17e7ed02005-06-17 12:54:33 +1000357 if (mkdtemp(xauthdir) != NULL) {
358 do_unlink = 1;
deraadt@openbsd.org087266e2015-01-20 23:14:00 +0000359 snprintf(xauthfile, PATH_MAX, "%s/xauthfile",
Damien Miller17e7ed02005-06-17 12:54:33 +1000360 xauthdir);
361 snprintf(cmd, sizeof(cmd),
362 "%s -f %s generate %s " SSH_X11_PROTO
Damien Miller1ab6a512010-06-26 10:02:24 +1000363 " untrusted timeout %u 2>" _PATH_DEVNULL,
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000364 xauth_path, xauthfile, display,
365 x11_timeout_real);
Damien Miller17e7ed02005-06-17 12:54:33 +1000366 debug2("x11_get_proto: %s", cmd);
Damien Miller1ab6a512010-06-26 10:02:24 +1000367 if (x11_refuse_time == 0) {
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000368 now = monotime() + 1;
Damien Miller1ab6a512010-06-26 10:02:24 +1000369 if (UINT_MAX - timeout < now)
370 x11_refuse_time = UINT_MAX;
371 else
372 x11_refuse_time = now + timeout;
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000373 channel_set_x11_refuse_time(
374 x11_refuse_time);
Damien Miller1ab6a512010-06-26 10:02:24 +1000375 }
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000376 if (system(cmd) == 0)
377 generated = 1;
Damien Miller17e7ed02005-06-17 12:54:33 +1000378 }
379 }
Darren Tucker513d13a2007-08-15 19:13:41 +1000380
381 /*
382 * When in untrusted mode, we read the cookie only if it was
383 * successfully generated as an untrusted one in the step
384 * above.
385 */
386 if (trusted || generated) {
387 snprintf(cmd, sizeof(cmd),
388 "%s %s%s list %s 2>" _PATH_DEVNULL,
389 xauth_path,
390 generated ? "-f " : "" ,
391 generated ? xauthfile : "",
392 display);
393 debug2("x11_get_proto: %s", cmd);
394 f = popen(cmd, "r");
395 if (f && fgets(line, sizeof(line), f) &&
396 sscanf(line, "%*s %511s %511s", proto, data) == 2)
397 got_data = 1;
398 if (f)
399 pclose(f);
400 } else
401 error("Warning: untrusted X11 forwarding setup failed: "
402 "xauth key data not generated");
Damien Miller17e7ed02005-06-17 12:54:33 +1000403 }
404
405 if (do_unlink) {
406 unlink(xauthfile);
407 rmdir(xauthdir);
408 }
Darren Tuckera627d422013-06-02 07:31:17 +1000409 free(xauthdir);
410 free(xauthfile);
Damien Miller17e7ed02005-06-17 12:54:33 +1000411
412 /*
413 * If we didn't get authentication data, just make up some
414 * data. The forwarding code will check the validity of the
415 * response anyway, and substitute this data. The X11
416 * server, however, will ignore this fake data and use
417 * whatever authentication mechanisms it was using otherwise
418 * for the local connection.
419 */
420 if (!got_data) {
421 u_int32_t rnd = 0;
422
423 logit("Warning: No xauth data; "
424 "using fake authentication data for X11 forwarding.");
425 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
426 for (i = 0; i < 16; i++) {
427 if (i % 4 == 0)
428 rnd = arc4random();
429 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
430 rnd & 0xff);
431 rnd >>= 8;
432 }
433 }
434}
435
Damien Miller5428f641999-11-25 11:54:57 +1100436/*
437 * This is called when the interactive is entered. This checks if there is
438 * an EOF coming on stdin. We must check this explicitly, as select() does
439 * not appear to wake up when redirecting from /dev/null.
440 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000441
Ben Lindstrombba81212001-06-25 05:01:22 +0000442static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000443client_check_initial_eof_on_stdin(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000444{
Damien Miller95def091999-11-25 00:26:21 +1100445 int len;
446 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000447
Damien Miller5428f641999-11-25 11:54:57 +1100448 /*
449 * If standard input is to be "redirected from /dev/null", we simply
450 * mark that we have seen an EOF and send an EOF message to the
451 * server. Otherwise, we try to read a single character; it appears
452 * that for some files, such /dev/null, select() never wakes up for
453 * read for this descriptor, which means that we never get EOF. This
454 * way we will get the EOF if stdin comes from /dev/null or similar.
455 */
Damien Miller95def091999-11-25 00:26:21 +1100456 if (stdin_null_flag) {
457 /* Fake EOF on stdin. */
458 debug("Sending eof.");
459 stdin_eof = 1;
460 packet_start(SSH_CMSG_EOF);
461 packet_send();
462 } else {
Damien Miller95def091999-11-25 00:26:21 +1100463 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464
Damien Miller95def091999-11-25 00:26:21 +1100465 /* Check for immediate EOF on stdin. */
466 len = read(fileno(stdin), buf, 1);
467 if (len == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000468 /*
469 * EOF. Record that we have seen it and send
470 * EOF to server.
471 */
Damien Miller95def091999-11-25 00:26:21 +1100472 debug("Sending eof.");
473 stdin_eof = 1;
474 packet_start(SSH_CMSG_EOF);
475 packet_send();
476 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100477 /*
478 * Got data. We must store the data in the buffer,
479 * and also process it as an escape character if
480 * appropriate.
481 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000482 if ((u_char) buf[0] == escape_char1)
483 escape_pending1 = 1;
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000484 else
Damien Miller95def091999-11-25 00:26:21 +1100485 buffer_append(&stdin_buffer, buf, 1);
Damien Miller95def091999-11-25 00:26:21 +1100486 }
Damien Miller95def091999-11-25 00:26:21 +1100487 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000488 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489}
490
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491
Damien Miller5428f641999-11-25 11:54:57 +1100492/*
493 * Make packets from buffered stdin data, and buffer them for sending to the
494 * connection.
495 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000496
Ben Lindstrombba81212001-06-25 05:01:22 +0000497static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000498client_make_packets_from_stdin_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000499{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000500 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000501
Damien Miller95def091999-11-25 00:26:21 +1100502 /* Send buffered stdin data to the server. */
503 while (buffer_len(&stdin_buffer) > 0 &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100504 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100505 len = buffer_len(&stdin_buffer);
506 /* Keep the packets at reasonable size. */
507 if (len > packet_get_maxsize())
508 len = packet_get_maxsize();
509 packet_start(SSH_CMSG_STDIN_DATA);
510 packet_put_string(buffer_ptr(&stdin_buffer), len);
511 packet_send();
512 buffer_consume(&stdin_buffer, len);
513 /* If we have a pending EOF, send it now. */
514 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
515 packet_start(SSH_CMSG_EOF);
516 packet_send();
517 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519}
520
Damien Miller5428f641999-11-25 11:54:57 +1100521/*
522 * Checks if the client window has changed, and sends a packet about it to
523 * the server if so. The actual change is detected elsewhere (by a software
524 * interrupt on Unix); this just checks the flag and sends a message if
525 * appropriate.
526 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000527
Ben Lindstrombba81212001-06-25 05:01:22 +0000528static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000529client_check_window_change(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000530{
Damien Miller1383bd82000-04-06 12:32:37 +1000531 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000532
Damien Miller1383bd82000-04-06 12:32:37 +1000533 if (! received_window_change_signal)
534 return;
535 /** XXX race */
536 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000537
Damien Millerd3444942000-09-30 14:20:03 +1100538 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000539
540 if (compat20) {
Damien Miller0e220db2004-06-15 10:34:08 +1000541 channel_send_window_changes();
Damien Miller1383bd82000-04-06 12:32:37 +1000542 } else {
Damien Miller0e220db2004-06-15 10:34:08 +1000543 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
544 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000545 packet_start(SSH_CMSG_WINDOW_SIZE);
Damien Miller71a73672006-03-26 14:04:36 +1100546 packet_put_int((u_int)ws.ws_row);
547 packet_put_int((u_int)ws.ws_col);
548 packet_put_int((u_int)ws.ws_xpixel);
549 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller1383bd82000-04-06 12:32:37 +1000550 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000551 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000552}
553
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000554static int
Damien Miller509b0102003-12-17 16:33:10 +1100555client_global_request_reply(int type, u_int32_t seq, void *ctxt)
556{
Darren Tucker9f407c42008-06-13 04:50:27 +1000557 struct global_confirm *gc;
558
559 if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000560 return 0;
Darren Tucker9f407c42008-06-13 04:50:27 +1000561 if (gc->cb != NULL)
562 gc->cb(type, seq, gc->ctx);
563 if (--gc->ref_count <= 0) {
564 TAILQ_REMOVE(&global_confirms, gc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +1100565 explicit_bzero(gc, sizeof(*gc));
Darren Tuckera627d422013-06-02 07:31:17 +1000566 free(gc);
Darren Tucker9f407c42008-06-13 04:50:27 +1000567 }
568
Darren Tuckerf7288d72009-06-21 18:12:20 +1000569 packet_set_alive_timeouts(0);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000570 return 0;
Damien Miller509b0102003-12-17 16:33:10 +1100571}
572
573static void
574server_alive_check(void)
575{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000576 if (packet_inc_alive_timeouts() > options.server_alive_count_max) {
Damien Millerb73b6fd2011-01-11 17:18:56 +1100577 logit("Timeout, server %s not responding.", host);
Damien Miller985a4482006-10-24 03:02:41 +1000578 cleanup_exit(255);
579 }
Damien Miller509b0102003-12-17 16:33:10 +1100580 packet_start(SSH2_MSG_GLOBAL_REQUEST);
581 packet_put_cstring("keepalive@openssh.com");
582 packet_put_char(1); /* boolean: want reply */
583 packet_send();
Darren Tucker9f407c42008-06-13 04:50:27 +1000584 /* Insert an empty placeholder to maintain ordering */
585 client_register_global_confirm(NULL, NULL);
Damien Miller509b0102003-12-17 16:33:10 +1100586}
587
Damien Miller5428f641999-11-25 11:54:57 +1100588/*
589 * Waits until the client can do something (some data becomes available on
590 * one of the file descriptors).
591 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000592static void
Damien Miller5e953212001-01-30 09:14:00 +1100593client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000594 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000595{
Damien Miller509b0102003-12-17 16:33:10 +1100596 struct timeval tv, *tvp;
Damien Millere11e1ea2010-08-03 16:04:46 +1000597 int timeout_secs;
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000598 time_t minwait_secs = 0, server_alive_time = 0, now = monotime();
Damien Miller509b0102003-12-17 16:33:10 +1100599 int ret;
600
Damien Miller5e953212001-01-30 09:14:00 +1100601 /* Add any selections by the channel mechanism. */
Damien Millera6508752012-04-22 11:21:10 +1000602 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp,
603 &minwait_secs, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000604
Damien Miller1383bd82000-04-06 12:32:37 +1000605 if (!compat20) {
606 /* Read from the connection, unless our buffers are full. */
607 if (buffer_len(&stdout_buffer) < buffer_high &&
608 buffer_len(&stderr_buffer) < buffer_high &&
609 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100610 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000611 /*
612 * Read from stdin, unless we have seen EOF or have very much
613 * buffered data to send to the server.
614 */
615 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100616 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000617
618 /* Select stdout/stderr if have data in buffer. */
619 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100620 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000621 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100622 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000623 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000624 /* channel_prepare_select could have closed the last channel */
Damien Miller164a7f42001-10-12 11:36:09 +1000625 if (session_closed && !channel_still_open() &&
626 !packet_have_data_to_write()) {
627 /* clear mask since we did not call select() */
Damien Miller79faeff2001-11-12 11:06:32 +1100628 memset(*readsetp, 0, *nallocp);
629 memset(*writesetp, 0, *nallocp);
Damien Miller164a7f42001-10-12 11:36:09 +1000630 return;
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000631 } else {
632 FD_SET(connection_in, *readsetp);
633 }
Damien Miller1383bd82000-04-06 12:32:37 +1000634 }
635
Damien Miller95def091999-11-25 00:26:21 +1100636 /* Select server connection if have data to write to the server. */
637 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100638 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100639
Damien Miller5428f641999-11-25 11:54:57 +1100640 /*
641 * Wait for something to happen. This will suspend the process until
642 * some selected descriptor can be read, written, or has some other
Damien Millere11e1ea2010-08-03 16:04:46 +1000643 * event pending, or a timeout expires.
Damien Miller5428f641999-11-25 11:54:57 +1100644 */
Damien Miller95def091999-11-25 00:26:21 +1100645
Damien Millere11e1ea2010-08-03 16:04:46 +1000646 timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000647 if (options.server_alive_interval > 0 && compat20) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000648 timeout_secs = options.server_alive_interval;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000649 server_alive_time = now + options.server_alive_interval;
650 }
651 if (options.rekey_interval > 0 && compat20 && !rekeying)
652 timeout_secs = MIN(timeout_secs, packet_get_rekey_timeout());
Damien Millere11e1ea2010-08-03 16:04:46 +1000653 set_control_persist_exit_time();
654 if (control_persist_exit_time > 0) {
655 timeout_secs = MIN(timeout_secs,
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000656 control_persist_exit_time - now);
Damien Millere11e1ea2010-08-03 16:04:46 +1000657 if (timeout_secs < 0)
658 timeout_secs = 0;
659 }
Damien Millera6508752012-04-22 11:21:10 +1000660 if (minwait_secs != 0)
661 timeout_secs = MIN(timeout_secs, (int)minwait_secs);
Damien Millere11e1ea2010-08-03 16:04:46 +1000662 if (timeout_secs == INT_MAX)
Damien Miller509b0102003-12-17 16:33:10 +1100663 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000664 else {
Damien Millere11e1ea2010-08-03 16:04:46 +1000665 tv.tv_sec = timeout_secs;
Damien Miller509b0102003-12-17 16:33:10 +1100666 tv.tv_usec = 0;
667 tvp = &tv;
668 }
Damien Millere11e1ea2010-08-03 16:04:46 +1000669
Damien Miller509b0102003-12-17 16:33:10 +1100670 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
671 if (ret < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100672 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000673
674 /*
675 * We have to clear the select masks, because we return.
676 * We have to return, because the mainloop checks for the flags
677 * set by the signal handlers.
678 */
Damien Miller79faeff2001-11-12 11:06:32 +1100679 memset(*readsetp, 0, *nallocp);
680 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000681
Damien Miller95def091999-11-25 00:26:21 +1100682 if (errno == EINTR)
683 return;
684 /* Note: we might still have data in the buffers. */
685 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
686 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100687 quit_pending = 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000688 } else if (ret == 0) {
689 /*
690 * Timeout. Could have been either keepalive or rekeying.
691 * Keepalive we check here, rekeying is checked in clientloop.
692 */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000693 if (server_alive_time != 0 && server_alive_time <= monotime())
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000694 server_alive_check();
695 }
696
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000697}
698
Ben Lindstrombba81212001-06-25 05:01:22 +0000699static void
Damien Millerad833b32000-08-23 10:46:23 +1000700client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000701{
Damien Miller95def091999-11-25 00:26:21 +1100702 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000703 if (buffer_len(bout) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000704 atomicio(vwrite, fileno(stdout), buffer_ptr(bout),
705 buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000706 if (buffer_len(berr) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000707 atomicio(vwrite, fileno(stderr), buffer_ptr(berr),
708 buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000709
Damien Miller21771e22011-05-15 08:45:50 +1000710 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000711
Damien Miller5428f641999-11-25 11:54:57 +1100712 /*
713 * Free (and clear) the buffer to reduce the amount of data that gets
714 * written to swap.
715 */
Damien Millerad833b32000-08-23 10:46:23 +1000716 buffer_free(bin);
717 buffer_free(bout);
718 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000719
Damien Miller95def091999-11-25 00:26:21 +1100720 /* Send the suspend signal to the program itself. */
721 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000722
Darren Tucker5d78de62004-11-05 20:35:44 +1100723 /* Reset window sizes in case they have changed */
724 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000725
Damien Miller95def091999-11-25 00:26:21 +1100726 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000727 buffer_init(bin);
728 buffer_init(bout);
729 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000730
Damien Miller21771e22011-05-15 08:45:50 +1000731 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000732}
733
Ben Lindstrombba81212001-06-25 05:01:22 +0000734static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100735client_process_net_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000736{
Darren Tuckerc5564e12009-06-21 18:53:53 +1000737 int len, cont = 0;
Darren Tucker86e30a02009-08-28 11:21:06 +1000738 char buf[SSH_IOBUFSZ];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000739
Damien Miller5428f641999-11-25 11:54:57 +1100740 /*
741 * Read input from the server, and add any such data to the buffer of
742 * the packet subsystem.
743 */
Damien Miller95def091999-11-25 00:26:21 +1100744 if (FD_ISSET(connection_in, readset)) {
745 /* Read as much as possible. */
Darren Tuckerc5564e12009-06-21 18:53:53 +1000746 len = roaming_read(connection_in, buf, sizeof(buf), &cont);
747 if (len == 0 && cont == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000748 /*
749 * Received EOF. The remote host has closed the
750 * connection.
751 */
752 snprintf(buf, sizeof buf,
753 "Connection to %.300s closed by remote host.\r\n",
754 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000755 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000756 quit_pending = 1;
757 return;
Damien Miller95def091999-11-25 00:26:21 +1100758 }
Damien Miller5428f641999-11-25 11:54:57 +1100759 /*
760 * There is a kernel bug on Solaris that causes select to
761 * sometimes wake up even though there is no data available.
762 */
Damien Millerd8968ad2008-07-04 23:10:49 +1000763 if (len < 0 &&
764 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Damien Miller95def091999-11-25 00:26:21 +1100765 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000766
Damien Miller95def091999-11-25 00:26:21 +1100767 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000768 /*
769 * An error has encountered. Perhaps there is a
770 * network problem.
771 */
772 snprintf(buf, sizeof buf,
773 "Read from remote host %.300s: %.100s\r\n",
774 host, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100775 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100776 quit_pending = 1;
777 return;
778 }
779 packet_process_incoming(buf, len);
780 }
Damien Miller1383bd82000-04-06 12:32:37 +1000781}
782
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000783static void
Damien Miller5771ed72008-05-19 15:35:33 +1000784client_status_confirm(int type, Channel *c, void *ctx)
Damien Miller0e220db2004-06-15 10:34:08 +1000785{
Damien Miller5771ed72008-05-19 15:35:33 +1000786 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
787 char errmsg[256];
788 int tochan;
Darren Tuckerfc959702004-07-17 16:12:08 +1000789
Damien Miller555f3b82011-05-15 08:48:05 +1000790 /*
791 * If a TTY was explicitly requested, then a failure to allocate
792 * one is fatal.
793 */
794 if (cr->action == CONFIRM_TTY &&
795 (options.request_tty == REQUEST_TTY_FORCE ||
796 options.request_tty == REQUEST_TTY_YES))
797 cr->action = CONFIRM_CLOSE;
798
Damien Miller5771ed72008-05-19 15:35:33 +1000799 /* XXX supress on mux _client_ quietmode */
800 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
Damien Millere1537f92010-01-26 13:26:22 +1100801 c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
Damien Miller0e220db2004-06-15 10:34:08 +1000802
Damien Miller5771ed72008-05-19 15:35:33 +1000803 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
804 debug2("%s request accepted on channel %d",
805 cr->request_type, c->self);
806 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
807 if (tochan) {
808 snprintf(errmsg, sizeof(errmsg),
809 "%s request failed\r\n", cr->request_type);
810 } else {
811 snprintf(errmsg, sizeof(errmsg),
812 "%s request failed on channel %d",
813 cr->request_type, c->self);
814 }
815 /* If error occurred on primary session channel, then exit */
Damien Miller555f3b82011-05-15 08:48:05 +1000816 if (cr->action == CONFIRM_CLOSE && c->self == session_ident)
Damien Miller5771ed72008-05-19 15:35:33 +1000817 fatal("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000818 /*
819 * If error occurred on mux client, append to
820 * their stderr.
821 */
822 if (tochan) {
823 buffer_append(&c->extended, errmsg,
824 strlen(errmsg));
825 } else
Damien Miller5771ed72008-05-19 15:35:33 +1000826 error("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000827 if (cr->action == CONFIRM_TTY) {
828 /*
829 * If a TTY allocation error occurred, then arrange
830 * for the correct TTY to leave raw mode.
831 */
832 if (c->self == session_ident)
833 leave_raw_mode(0);
834 else
835 mux_tty_alloc_failed(c);
836 } else if (cr->action == CONFIRM_CLOSE) {
Damien Miller5771ed72008-05-19 15:35:33 +1000837 chan_read_failed(c);
838 chan_write_failed(c);
839 }
Damien Miller0e220db2004-06-15 10:34:08 +1000840 }
Darren Tuckera627d422013-06-02 07:31:17 +1000841 free(cr);
Damien Miller5771ed72008-05-19 15:35:33 +1000842}
Damien Miller0e220db2004-06-15 10:34:08 +1000843
Damien Miller5771ed72008-05-19 15:35:33 +1000844static void
845client_abandon_status_confirm(Channel *c, void *ctx)
846{
Darren Tuckera627d422013-06-02 07:31:17 +1000847 free(ctx);
Damien Miller5771ed72008-05-19 15:35:33 +1000848}
849
Damien Miller6d7b4372011-06-23 08:31:57 +1000850void
Damien Miller555f3b82011-05-15 08:48:05 +1000851client_expect_confirm(int id, const char *request,
852 enum confirm_action action)
Damien Miller5771ed72008-05-19 15:35:33 +1000853{
Damien Miller6c81fee2013-11-08 12:19:55 +1100854 struct channel_reply_ctx *cr = xcalloc(1, sizeof(*cr));
Damien Miller5771ed72008-05-19 15:35:33 +1000855
856 cr->request_type = request;
Damien Miller555f3b82011-05-15 08:48:05 +1000857 cr->action = action;
Damien Miller5771ed72008-05-19 15:35:33 +1000858
859 channel_register_status_confirm(id, client_status_confirm,
860 client_abandon_status_confirm, cr);
Damien Miller0e220db2004-06-15 10:34:08 +1000861}
862
Darren Tucker9f407c42008-06-13 04:50:27 +1000863void
864client_register_global_confirm(global_confirm_cb *cb, void *ctx)
865{
Damien Millerb9d3bee2008-07-16 22:40:52 +1000866 struct global_confirm *gc, *last_gc;
Darren Tucker9f407c42008-06-13 04:50:27 +1000867
868 /* Coalesce identical callbacks */
Damien Millerb9d3bee2008-07-16 22:40:52 +1000869 last_gc = TAILQ_LAST(&global_confirms, global_confirms);
870 if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
871 if (++last_gc->ref_count >= INT_MAX)
872 fatal("%s: last_gc->ref_count = %d",
873 __func__, last_gc->ref_count);
Darren Tucker9f407c42008-06-13 04:50:27 +1000874 return;
875 }
876
Damien Miller6c81fee2013-11-08 12:19:55 +1100877 gc = xcalloc(1, sizeof(*gc));
Darren Tucker9f407c42008-06-13 04:50:27 +1000878 gc->cb = cb;
879 gc->ctx = ctx;
880 gc->ref_count = 1;
881 TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
882}
883
Damien Miller0e220db2004-06-15 10:34:08 +1000884static void
Ben Lindstrom681d9322002-03-22 03:53:00 +0000885process_cmdline(void)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000886{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000887 void (*handler)(int);
Damien Miller7acefbb2014-07-18 14:11:24 +1000888 char *s, *cmd;
889 int ok, delete = 0, local = 0, remote = 0, dynamic = 0;
890 struct Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000891
Damien Miller1d2c4562014-02-04 11:18:20 +1100892 memset(&fwd, 0, sizeof(fwd));
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100893
Damien Miller21771e22011-05-15 08:45:50 +1000894 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000895 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000896 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000897 if (s == NULL)
898 goto out;
Damien Millerfdb23062013-11-21 13:57:15 +1100899 while (isspace((u_char)*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000900 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000901 if (*s == '-')
902 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000903 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000904 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000905
Darren Tucker1973c882004-05-24 10:34:36 +1000906 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000907 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000908 logit(" -L[bind_address:]port:host:hostport "
909 "Request local forward");
910 logit(" -R[bind_address:]port:host:hostport "
911 "Request remote forward");
Damien Miller0164cb82008-11-05 16:30:31 +1100912 logit(" -D[bind_address:]port "
913 "Request dynamic forward");
Damien Millerff773642011-09-22 21:39:48 +1000914 logit(" -KL[bind_address:]port "
915 "Cancel local forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000916 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000917 "Cancel remote forward");
Damien Millerff773642011-09-22 21:39:48 +1000918 logit(" -KD[bind_address:]port "
919 "Cancel dynamic forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100920 if (!options.permit_local_command)
921 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000922 logit(" !args "
923 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100924 goto out;
925 }
926
927 if (*s == '!' && options.permit_local_command) {
928 s++;
929 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000930 goto out;
931 }
932
933 if (*s == 'K') {
934 delete = 1;
935 s++;
936 }
Damien Miller0164cb82008-11-05 16:30:31 +1100937 if (*s == 'L')
938 local = 1;
939 else if (*s == 'R')
940 remote = 1;
941 else if (*s == 'D')
942 dynamic = 1;
943 else {
Damien Miller996acd22003-04-09 20:59:48 +1000944 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000945 goto out;
946 }
Damien Miller0164cb82008-11-05 16:30:31 +1100947
Damien Millerff773642011-09-22 21:39:48 +1000948 if (delete && !compat20) {
Damien Miller996acd22003-04-09 20:59:48 +1000949 logit("Not supported for SSH protocol version 1.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000950 goto out;
951 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000952
Damien Millerfdb23062013-11-21 13:57:15 +1100953 while (isspace((u_char)*++s))
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100954 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000955
Damien Millere1537f92010-01-26 13:26:22 +1100956 /* XXX update list of forwards in options */
Darren Tuckere7066df2004-05-24 10:18:05 +1000957 if (delete) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000958 /* We pass 1 for dynamicfwd to restrict to 1 or 2 fields. */
959 if (!parse_forward(&fwd, s, 1, 0)) {
960 logit("Bad forwarding close specification.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000961 goto out;
962 }
Damien Millerff773642011-09-22 21:39:48 +1000963 if (remote)
Damien Miller7acefbb2014-07-18 14:11:24 +1000964 ok = channel_request_rforward_cancel(&fwd) == 0;
Damien Millerff773642011-09-22 21:39:48 +1000965 else if (dynamic)
Damien Miller7acefbb2014-07-18 14:11:24 +1000966 ok = channel_cancel_lport_listener(&fwd,
967 0, &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000968 else
Damien Miller7acefbb2014-07-18 14:11:24 +1000969 ok = channel_cancel_lport_listener(&fwd,
970 CHANNEL_CANCEL_PORT_STATIC,
971 &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000972 if (!ok) {
973 logit("Unkown port forwarding.");
974 goto out;
975 }
976 logit("Canceled forwarding.");
Darren Tuckere7066df2004-05-24 10:18:05 +1000977 } else {
Damien Miller4bf648f2009-02-14 16:28:21 +1100978 if (!parse_forward(&fwd, s, dynamic, remote)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000979 logit("Bad forwarding specification.");
980 goto out;
981 }
Damien Miller0164cb82008-11-05 16:30:31 +1100982 if (local || dynamic) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000983 if (!channel_setup_local_fwd_listener(&fwd,
984 &options.fwd_opts)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000985 logit("Port forwarding failed.");
986 goto out;
987 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100988 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +1000989 if (channel_request_remote_forwarding(&fwd) < 0) {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000990 logit("Port forwarding failed.");
991 goto out;
992 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100993 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000994 logit("Forwarding port.");
995 }
996
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000997out:
998 signal(SIGINT, handler);
Damien Miller21771e22011-05-15 08:45:50 +1000999 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tuckera627d422013-06-02 07:31:17 +10001000 free(cmd);
1001 free(fwd.listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +10001002 free(fwd.listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +10001003 free(fwd.connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +10001004 free(fwd.connect_path);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001005}
1006
Darren Tucker92a39cf2012-09-07 11:20:20 +10001007/* reasons to suppress output of an escape command in help output */
1008#define SUPPRESS_NEVER 0 /* never suppress, always show */
1009#define SUPPRESS_PROTO1 1 /* don't show in protocol 1 sessions */
1010#define SUPPRESS_MUXCLIENT 2 /* don't show in mux client sessions */
1011#define SUPPRESS_MUXMASTER 4 /* don't show in mux master sessions */
1012#define SUPPRESS_SYSLOG 8 /* don't show when logging to syslog */
1013struct escape_help_text {
1014 const char *cmd;
1015 const char *text;
1016 unsigned int flags;
1017};
1018static struct escape_help_text esc_txt[] = {
1019 {".", "terminate session", SUPPRESS_MUXMASTER},
1020 {".", "terminate connection (and any multiplexed sessions)",
1021 SUPPRESS_MUXCLIENT},
1022 {"B", "send a BREAK to the remote system", SUPPRESS_PROTO1},
1023 {"C", "open a command line", SUPPRESS_MUXCLIENT},
1024 {"R", "request rekey", SUPPRESS_PROTO1},
Darren Tuckerca0d0fd2012-09-07 11:22:24 +10001025 {"V/v", "decrease/increase verbosity (LogLevel)", SUPPRESS_MUXCLIENT},
Darren Tucker92a39cf2012-09-07 11:20:20 +10001026 {"^Z", "suspend ssh", SUPPRESS_MUXCLIENT},
1027 {"#", "list forwarded connections", SUPPRESS_NEVER},
1028 {"&", "background ssh (when waiting for connections to terminate)",
1029 SUPPRESS_MUXCLIENT},
1030 {"?", "this message", SUPPRESS_NEVER},
1031};
1032
1033static void
1034print_escape_help(Buffer *b, int escape_char, int protocol2, int mux_client,
1035 int using_stderr)
1036{
1037 unsigned int i, suppress_flags;
1038 char string[1024];
1039
1040 snprintf(string, sizeof string, "%c?\r\n"
1041 "Supported escape sequences:\r\n", escape_char);
1042 buffer_append(b, string, strlen(string));
1043
1044 suppress_flags = (protocol2 ? 0 : SUPPRESS_PROTO1) |
1045 (mux_client ? SUPPRESS_MUXCLIENT : 0) |
1046 (mux_client ? 0 : SUPPRESS_MUXMASTER) |
1047 (using_stderr ? 0 : SUPPRESS_SYSLOG);
1048
1049 for (i = 0; i < sizeof(esc_txt)/sizeof(esc_txt[0]); i++) {
1050 if (esc_txt[i].flags & suppress_flags)
1051 continue;
Darren Tuckerca0d0fd2012-09-07 11:22:24 +10001052 snprintf(string, sizeof string, " %c%-3s - %s\r\n",
Darren Tucker92a39cf2012-09-07 11:20:20 +10001053 escape_char, esc_txt[i].cmd, esc_txt[i].text);
1054 buffer_append(b, string, strlen(string));
1055 }
1056
1057 snprintf(string, sizeof string,
Darren Tuckerca0d0fd2012-09-07 11:22:24 +10001058 " %c%c - send the escape character by typing it twice\r\n"
Darren Tucker92a39cf2012-09-07 11:20:20 +10001059 "(Note that escapes are only recognized immediately after "
1060 "newline.)\r\n", escape_char, escape_char);
1061 buffer_append(b, string, strlen(string));
1062}
1063
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001064/*
1065 * Process the characters one by one, call with c==NULL for proto1 case.
1066 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001067static int
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001068process_escapes(Channel *c, Buffer *bin, Buffer *bout, Buffer *berr,
1069 char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001070{
1071 char string[1024];
1072 pid_t pid;
1073 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001074 u_int i;
1075 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +10001076 char *s;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001077 int *escape_pendingp, escape_char;
1078 struct escape_filter_ctx *efc;
Damien Millerad833b32000-08-23 10:46:23 +10001079
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001080 if (c == NULL) {
1081 escape_pendingp = &escape_pending1;
1082 escape_char = escape_char1;
1083 } else {
1084 if (c->filter_ctx == NULL)
1085 return 0;
1086 efc = (struct escape_filter_ctx *)c->filter_ctx;
1087 escape_pendingp = &efc->escape_pending;
1088 escape_char = efc->escape_char;
1089 }
1090
Damien Millereccb9de2005-06-17 12:59:34 +10001091 if (len <= 0)
1092 return (0);
1093
1094 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +10001095 /* Get one character at a time. */
1096 ch = buf[i];
1097
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001098 if (*escape_pendingp) {
Damien Millerad833b32000-08-23 10:46:23 +10001099 /* We have previously seen an escape character. */
1100 /* Clear the flag now. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001101 *escape_pendingp = 0;
Damien Millerad833b32000-08-23 10:46:23 +10001102
1103 /* Process the escaped character. */
1104 switch (ch) {
1105 case '.':
1106 /* Terminate the connection. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001107 snprintf(string, sizeof string, "%c.\r\n",
1108 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001109 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001110
Damien Millere1537f92010-01-26 13:26:22 +11001111 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001112 chan_read_failed(c);
1113 chan_write_failed(c);
Darren Tuckerea8342c2013-06-06 08:11:40 +10001114 if (c->detach_user)
1115 c->detach_user(c->self, NULL);
Damien Miller36187092013-06-10 13:07:11 +10001116 c->type = SSH_CHANNEL_ABANDONED;
1117 buffer_clear(&c->input);
1118 chan_ibuf_empty(c);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001119 return 0;
1120 } else
1121 quit_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001122 return -1;
1123
1124 case 'Z' - 64:
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001125 /* XXX support this for mux clients */
Damien Millere1537f92010-01-26 13:26:22 +11001126 if (c && c->ctl_chan != -1) {
Darren Tuckerf111d402012-09-07 11:21:42 +10001127 char b[16];
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001128 noescape:
Darren Tuckerf111d402012-09-07 11:21:42 +10001129 if (ch == 'Z' - 64)
1130 snprintf(b, sizeof b, "^Z");
1131 else
1132 snprintf(b, sizeof b, "%c", ch);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001133 snprintf(string, sizeof string,
Darren Tuckerf111d402012-09-07 11:21:42 +10001134 "%c%s escape not available to "
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001135 "multiplexed sessions\r\n",
Darren Tuckerf111d402012-09-07 11:21:42 +10001136 escape_char, b);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001137 buffer_append(berr, string,
1138 strlen(string));
1139 continue;
1140 }
Darren Tucker4d5cd332008-06-13 04:51:14 +10001141 /* Suspend the program. Inform the user */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001142 snprintf(string, sizeof string,
1143 "%c^Z [suspend ssh]\r\n", escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001144 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001145
1146 /* Restore terminal modes and suspend. */
1147 client_suspend_self(bin, bout, berr);
1148
1149 /* We have been continued. */
1150 continue;
1151
Damien Miller54c45982003-05-15 10:20:13 +10001152 case 'B':
1153 if (compat20) {
1154 snprintf(string, sizeof string,
1155 "%cB\r\n", escape_char);
1156 buffer_append(berr, string,
1157 strlen(string));
Damien Miller70182522013-09-14 09:49:19 +10001158 channel_request_start(c->self,
Damien Miller54c45982003-05-15 10:20:13 +10001159 "break", 0);
1160 packet_put_int(1000);
1161 packet_send();
1162 }
1163 continue;
1164
Ben Lindstromf28f6342001-04-04 02:03:04 +00001165 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001166 if (compat20) {
1167 if (datafellows & SSH_BUG_NOREKEY)
Darren Tucker4d5cd332008-06-13 04:51:14 +10001168 logit("Server does not "
1169 "support re-keying");
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001170 else
1171 need_rekeying = 1;
1172 }
Ben Lindstromf28f6342001-04-04 02:03:04 +00001173 continue;
1174
Darren Tucker50a48d02012-09-06 21:25:37 +10001175 case 'V':
1176 /* FALLTHROUGH */
1177 case 'v':
1178 if (c && c->ctl_chan != -1)
1179 goto noescape;
1180 if (!log_is_on_stderr()) {
1181 snprintf(string, sizeof string,
1182 "%c%c [Logging to syslog]\r\n",
1183 escape_char, ch);
1184 buffer_append(berr, string,
1185 strlen(string));
1186 continue;
1187 }
1188 if (ch == 'V' && options.log_level >
1189 SYSLOG_LEVEL_QUIET)
1190 log_change_level(--options.log_level);
1191 if (ch == 'v' && options.log_level <
1192 SYSLOG_LEVEL_DEBUG3)
1193 log_change_level(++options.log_level);
1194 snprintf(string, sizeof string,
1195 "%c%c [LogLevel %s]\r\n", escape_char, ch,
1196 log_level_name(options.log_level));
1197 buffer_append(berr, string, strlen(string));
1198 continue;
1199
Damien Millerad833b32000-08-23 10:46:23 +10001200 case '&':
Damien Millere1537f92010-01-26 13:26:22 +11001201 if (c && c->ctl_chan != -1)
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001202 goto noescape;
Damien Millerad833b32000-08-23 10:46:23 +10001203 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001204 * Detach the program (continue to serve
1205 * connections, but put in background and no
1206 * more new connections).
Damien Millerad833b32000-08-23 10:46:23 +10001207 */
Damien Miller96507ef2001-11-12 10:52:25 +11001208 /* Restore tty modes. */
Damien Miller21771e22011-05-15 08:45:50 +10001209 leave_raw_mode(
1210 options.request_tty == REQUEST_TTY_FORCE);
Damien Miller96507ef2001-11-12 10:52:25 +11001211
1212 /* Stop listening for new connections. */
1213 channel_stop_listening();
1214
1215 snprintf(string, sizeof string,
1216 "%c& [backgrounded]\n", escape_char);
1217 buffer_append(berr, string, strlen(string));
1218
1219 /* Fork into background. */
1220 pid = fork();
1221 if (pid < 0) {
1222 error("fork: %.100s", strerror(errno));
1223 continue;
1224 }
1225 if (pid != 0) { /* This is the parent. */
1226 /* The parent just exits. */
1227 exit(0);
1228 }
1229 /* The child continues serving connections. */
1230 if (compat20) {
1231 buffer_append(bin, "\004", 1);
1232 /* fake EOF on stdin */
1233 return -1;
1234 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +10001235 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001236 * Sending SSH_CMSG_EOF alone does not
1237 * always appear to be enough. So we
1238 * try to send an EOF character first.
Damien Millerad833b32000-08-23 10:46:23 +10001239 */
1240 packet_start(SSH_CMSG_STDIN_DATA);
1241 packet_put_string("\004", 1);
1242 packet_send();
1243 /* Close stdin. */
1244 stdin_eof = 1;
1245 if (buffer_len(bin) == 0) {
1246 packet_start(SSH_CMSG_EOF);
1247 packet_send();
1248 }
1249 }
Damien Miller96507ef2001-11-12 10:52:25 +11001250 continue;
Damien Millerad833b32000-08-23 10:46:23 +10001251
1252 case '?':
Darren Tucker92a39cf2012-09-07 11:20:20 +10001253 print_escape_help(berr, escape_char, compat20,
1254 (c && c->ctl_chan != -1),
1255 log_is_on_stderr());
Damien Millerad833b32000-08-23 10:46:23 +10001256 continue;
1257
1258 case '#':
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001259 snprintf(string, sizeof string, "%c#\r\n",
1260 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001261 buffer_append(berr, string, strlen(string));
1262 s = channel_open_message();
1263 buffer_append(berr, s, strlen(s));
Darren Tuckera627d422013-06-02 07:31:17 +10001264 free(s);
Damien Millerad833b32000-08-23 10:46:23 +10001265 continue;
1266
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001267 case 'C':
Damien Millere1537f92010-01-26 13:26:22 +11001268 if (c && c->ctl_chan != -1)
Damien Miller586b0052008-12-09 14:11:32 +11001269 goto noescape;
Ben Lindstrom681d9322002-03-22 03:53:00 +00001270 process_cmdline();
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001271 continue;
1272
Damien Millerad833b32000-08-23 10:46:23 +10001273 default:
1274 if (ch != escape_char) {
1275 buffer_put_char(bin, escape_char);
1276 bytes++;
1277 }
1278 /* Escaped characters fall through here */
1279 break;
1280 }
1281 } else {
1282 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001283 * The previous character was not an escape char.
1284 * Check if this is an escape.
Damien Millerad833b32000-08-23 10:46:23 +10001285 */
1286 if (last_was_cr && ch == escape_char) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001287 /*
1288 * It is. Set the flag and continue to
1289 * next character.
1290 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001291 *escape_pendingp = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001292 continue;
1293 }
1294 }
1295
1296 /*
1297 * Normal character. Record whether it was a newline,
1298 * and append it to the buffer.
1299 */
1300 last_was_cr = (ch == '\r' || ch == '\n');
1301 buffer_put_char(bin, ch);
1302 bytes++;
1303 }
1304 return bytes;
1305}
1306
Ben Lindstrombba81212001-06-25 05:01:22 +00001307static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001308client_process_input(fd_set *readset)
Damien Miller1383bd82000-04-06 12:32:37 +10001309{
Damien Miller166fca82000-04-20 07:42:21 +10001310 int len;
Darren Tucker86e30a02009-08-28 11:21:06 +10001311 char buf[SSH_IOBUFSZ];
Damien Miller1383bd82000-04-06 12:32:37 +10001312
Damien Miller95def091999-11-25 00:26:21 +11001313 /* Read input from stdin. */
1314 if (FD_ISSET(fileno(stdin), readset)) {
1315 /* Read as much as possible. */
1316 len = read(fileno(stdin), buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +10001317 if (len < 0 &&
1318 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001319 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +11001320 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001321 /*
1322 * Received EOF or error. They are treated
1323 * similarly, except that an error message is printed
1324 * if it was an error condition.
1325 */
Damien Miller95def091999-11-25 00:26:21 +11001326 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001327 snprintf(buf, sizeof buf, "read: %.100s\r\n",
1328 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001329 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001330 }
1331 /* Mark that we have seen EOF. */
1332 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +11001333 /*
1334 * Send an EOF message to the server unless there is
1335 * data in the buffer. If there is data in the
1336 * buffer, no message will be sent now. Code
1337 * elsewhere will send the EOF when the buffer
1338 * becomes empty if stdin_eof is set.
1339 */
Damien Miller95def091999-11-25 00:26:21 +11001340 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001341 packet_start(SSH_CMSG_EOF);
1342 packet_send();
Damien Miller95def091999-11-25 00:26:21 +11001343 }
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001344 } else if (escape_char1 == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +11001345 /*
1346 * Normal successful read, and no escape character.
1347 * Just append the data to buffer.
1348 */
Damien Miller95def091999-11-25 00:26:21 +11001349 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +11001350 } else {
Damien Miller5428f641999-11-25 11:54:57 +11001351 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001352 * Normal, successful read. But we have an escape
1353 * character and have to process the characters one
1354 * by one.
Damien Miller5428f641999-11-25 11:54:57 +11001355 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001356 if (process_escapes(NULL, &stdin_buffer,
1357 &stdout_buffer, &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +10001358 return;
Damien Miller95def091999-11-25 00:26:21 +11001359 }
1360 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001361}
1362
Ben Lindstrombba81212001-06-25 05:01:22 +00001363static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001364client_process_output(fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001365{
Damien Miller95def091999-11-25 00:26:21 +11001366 int len;
1367 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001368
Damien Miller95def091999-11-25 00:26:21 +11001369 /* Write buffered output to stdout. */
1370 if (FD_ISSET(fileno(stdout), writeset)) {
1371 /* Write as much data as possible. */
1372 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001373 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001374 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001375 if (errno == EINTR || errno == EAGAIN ||
1376 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001377 len = 0;
1378 else {
Damien Miller5428f641999-11-25 11:54:57 +11001379 /*
1380 * An error or EOF was encountered. Put an
1381 * error message to stderr buffer.
1382 */
Darren Tucker4d5cd332008-06-13 04:51:14 +10001383 snprintf(buf, sizeof buf,
1384 "write stdout: %.50s\r\n", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001385 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001386 quit_pending = 1;
1387 return;
1388 }
1389 }
1390 /* Consume printed data from the buffer. */
1391 buffer_consume(&stdout_buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001392 }
Damien Miller95def091999-11-25 00:26:21 +11001393 /* Write buffered output to stderr. */
1394 if (FD_ISSET(fileno(stderr), writeset)) {
1395 /* Write as much data as possible. */
1396 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001397 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001398 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001399 if (errno == EINTR || errno == EAGAIN ||
1400 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001401 len = 0;
1402 else {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001403 /*
1404 * EOF or error, but can't even print
1405 * error message.
1406 */
Damien Miller95def091999-11-25 00:26:21 +11001407 quit_pending = 1;
1408 return;
1409 }
1410 }
1411 /* Consume printed characters from the buffer. */
1412 buffer_consume(&stderr_buffer, len);
1413 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001414}
1415
Damien Miller5428f641999-11-25 11:54:57 +11001416/*
Damien Millerb38eff82000-04-01 11:09:21 +10001417 * Get packets from the connection input buffer, and process them as long as
1418 * there are packets available.
1419 *
1420 * Any unknown packets received during the actual
1421 * session cause the session to terminate. This is
1422 * intended to make debugging easier since no
1423 * confirmations are sent. Any compatible protocol
1424 * extensions must be negotiated during the
1425 * preparatory phase.
1426 */
1427
Ben Lindstrombba81212001-06-25 05:01:22 +00001428static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001429client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001430{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +00001431 dispatch_run(DISPATCH_NONBLOCK, &quit_pending, active_state);
Damien Millerb38eff82000-04-01 11:09:21 +10001432}
1433
Damien Millerad833b32000-08-23 10:46:23 +10001434/* scan buf[] for '~' before sending data to the peer */
1435
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001436/* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1437void *
1438client_new_escape_filter_ctx(int escape_char)
1439{
1440 struct escape_filter_ctx *ret;
1441
Damien Miller6c81fee2013-11-08 12:19:55 +11001442 ret = xcalloc(1, sizeof(*ret));
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001443 ret->escape_pending = 0;
1444 ret->escape_char = escape_char;
1445 return (void *)ret;
1446}
1447
Darren Tucker84c56f52008-06-13 04:55:46 +10001448/* Free the escape filter context on channel free */
1449void
1450client_filter_cleanup(int cid, void *ctx)
1451{
Darren Tuckera627d422013-06-02 07:31:17 +10001452 free(ctx);
Darren Tucker84c56f52008-06-13 04:55:46 +10001453}
1454
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001455int
1456client_simple_escape_filter(Channel *c, char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001457{
Damien Miller5771ed72008-05-19 15:35:33 +10001458 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1459 return 0;
1460
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001461 return process_escapes(c, &c->input, &c->output, &c->extended,
1462 buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001463}
1464
Ben Lindstrombba81212001-06-25 05:01:22 +00001465static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001466client_channel_closed(int id, void *arg)
1467{
Damien Miller3ec27592001-10-12 11:35:04 +10001468 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001469 session_closed = 1;
Damien Miller21771e22011-05-15 08:45:50 +10001470 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001471}
1472
Damien Millerb38eff82000-04-01 11:09:21 +10001473/*
Damien Miller5428f641999-11-25 11:54:57 +11001474 * Implements the interactive session with the server. This is called after
1475 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001476 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1477 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001478 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001479
Damien Miller4af51302000-04-16 11:18:38 +10001480int
Damien Millerad833b32000-08-23 10:46:23 +10001481client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001482{
Damien Miller5e953212001-01-30 09:14:00 +11001483 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001484 double start_time, total_time;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +00001485 int r, max_fd = 0, max_fd2 = 0, len, rekeying = 0;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001486 u_int64_t ibytes, obytes;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001487 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001488 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001489
Damien Miller95def091999-11-25 00:26:21 +11001490 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001491
Damien Miller95def091999-11-25 00:26:21 +11001492 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001493
Damien Miller95def091999-11-25 00:26:21 +11001494 /* Initialize variables. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001495 escape_pending1 = 0;
Damien Miller95def091999-11-25 00:26:21 +11001496 last_was_cr = 1;
1497 exit_status = -1;
1498 stdin_eof = 0;
1499 buffer_high = 64 * 1024;
1500 connection_in = packet_get_connection_in();
1501 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +11001502 max_fd = MAX(connection_in, connection_out);
1503
1504 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +00001505 /* enable nonblocking unless tty */
1506 if (!isatty(fileno(stdin)))
1507 set_nonblock(fileno(stdin));
1508 if (!isatty(fileno(stdout)))
1509 set_nonblock(fileno(stdout));
1510 if (!isatty(fileno(stderr)))
1511 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +11001512 max_fd = MAX(max_fd, fileno(stdin));
1513 max_fd = MAX(max_fd, fileno(stdout));
1514 max_fd = MAX(max_fd, fileno(stderr));
1515 }
Damien Miller95def091999-11-25 00:26:21 +11001516 quit_pending = 0;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001517 escape_char1 = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001518
Damien Miller95def091999-11-25 00:26:21 +11001519 /* Initialize buffers. */
1520 buffer_init(&stdin_buffer);
1521 buffer_init(&stdout_buffer);
1522 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001523
Damien Millerb38eff82000-04-01 11:09:21 +10001524 client_init_dispatch();
1525
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001526 /*
1527 * Set signal handlers, (e.g. to restore non-blocking mode)
1528 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1529 */
Darren Tucker07336da2004-11-05 20:02:16 +11001530 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1531 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001532 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1533 signal(SIGINT, signal_handler);
1534 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1535 signal(SIGQUIT, signal_handler);
1536 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1537 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001538 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001539
Damien Miller95def091999-11-25 00:26:21 +11001540 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001541 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001542
Ben Lindstrom5b828322001-02-09 01:34:36 +00001543 if (compat20) {
1544 session_ident = ssh2_chan_id;
Damien Miller6c3eec72011-05-05 14:16:22 +10001545 if (session_ident != -1) {
1546 if (escape_char_arg != SSH_ESCAPECHAR_NONE) {
1547 channel_register_filter(session_ident,
1548 client_simple_escape_filter, NULL,
1549 client_filter_cleanup,
1550 client_new_escape_filter_ctx(
1551 escape_char_arg));
1552 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001553 channel_register_cleanup(session_ident,
Damien Miller39eda6e2005-11-05 14:52:50 +11001554 client_channel_closed, 0);
Damien Miller6c3eec72011-05-05 14:16:22 +10001555 }
Ben Lindstrom5b828322001-02-09 01:34:36 +00001556 } else {
1557 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +10001558 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001559 }
Damien Millerad833b32000-08-23 10:46:23 +10001560
Damien Miller95def091999-11-25 00:26:21 +11001561 /* Main loop of the client for the interactive session mode. */
1562 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001563
Damien Miller5428f641999-11-25 11:54:57 +11001564 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001565 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001566
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001567 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +10001568 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001569
markus@openbsd.org091c3022015-01-19 19:52:16 +00001570 rekeying = (active_state->kex != NULL && !active_state->kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +10001571
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001572 if (rekeying) {
1573 debug("rekeying in progress");
1574 } else {
1575 /*
1576 * Make packets of buffered stdin data, and buffer
1577 * them for sending to the server.
1578 */
1579 if (!compat20)
1580 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001581
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001582 /*
1583 * Make packets from buffered channel data, and
1584 * enqueue them for sending to the server.
1585 */
1586 if (packet_not_very_much_data_to_write())
1587 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001588
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001589 /*
1590 * Check if the window size has changed, and buffer a
1591 * message about it to the server if so.
1592 */
1593 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001594
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001595 if (quit_pending)
1596 break;
1597 }
Damien Miller5428f641999-11-25 11:54:57 +11001598 /*
1599 * Wait until we have something to do (something becomes
1600 * available on one of the descriptors).
1601 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001602 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001603 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001604 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001605
Damien Miller95def091999-11-25 00:26:21 +11001606 if (quit_pending)
1607 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001608
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001609 /* Do channel operations unless rekeying in progress. */
1610 if (!rekeying) {
1611 channel_after_select(readset, writeset);
Damien Millera5539d22003-04-09 20:50:06 +10001612 if (need_rekeying || packet_need_rekeying()) {
1613 debug("need rekeying");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001614 active_state->kex->done = 0;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +00001615 if ((r = kex_send_kexinit(active_state)) != 0)
1616 fatal("%s: kex_send_kexinit: %s",
1617 __func__, ssh_err(r));
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001618 need_rekeying = 0;
1619 }
1620 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001621
Damien Miller1383bd82000-04-06 12:32:37 +10001622 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001623 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001624
Damien Miller1383bd82000-04-06 12:32:37 +10001625 if (quit_pending)
1626 break;
1627
1628 if (!compat20) {
1629 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +11001630 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +10001631 /*
1632 * Process output to stdout and stderr. Output to
1633 * the connection is processed elsewhere (above).
1634 */
Damien Miller5e953212001-01-30 09:14:00 +11001635 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +10001636 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001637
Darren Tuckere32cf432010-01-08 16:51:40 +11001638 if (session_resumed) {
1639 connection_in = packet_get_connection_in();
1640 connection_out = packet_get_connection_out();
1641 max_fd = MAX(max_fd, connection_out);
1642 max_fd = MAX(max_fd, connection_in);
1643 session_resumed = 0;
1644 }
1645
Darren Tucker4d5cd332008-06-13 04:51:14 +10001646 /*
1647 * Send as much buffered packet data as possible to the
1648 * sender.
1649 */
Damien Miller5e953212001-01-30 09:14:00 +11001650 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001651 packet_write_poll();
Damien Millere11e1ea2010-08-03 16:04:46 +10001652
1653 /*
1654 * If we are a backgrounded control master, and the
1655 * timeout has expired without any active client
1656 * connections, then quit.
1657 */
1658 if (control_persist_exit_time > 0) {
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001659 if (monotime() >= control_persist_exit_time) {
Damien Millere11e1ea2010-08-03 16:04:46 +10001660 debug("ControlPersist timeout expired");
1661 break;
1662 }
1663 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001664 }
Darren Tuckera627d422013-06-02 07:31:17 +10001665 free(readset);
1666 free(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001667
Damien Miller95def091999-11-25 00:26:21 +11001668 /* Terminate the session. */
1669
1670 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001671 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001672
Darren Tuckerde0c0252009-07-06 07:17:35 +10001673 if (compat20) {
1674 packet_start(SSH2_MSG_DISCONNECT);
1675 packet_put_int(SSH2_DISCONNECT_BY_APPLICATION);
1676 packet_put_cstring("disconnected by user");
Damien Miller8ddc71c2010-03-22 05:54:02 +11001677 packet_put_cstring(""); /* language tag */
Darren Tuckerde0c0252009-07-06 07:17:35 +10001678 packet_send();
1679 packet_write_wait();
1680 }
Darren Tucker12b4a652009-06-21 18:14:48 +10001681
Ben Lindstrom601e4362001-06-21 03:19:23 +00001682 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +11001683
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001684 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001685 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001686
1687 /* restore blocking io */
1688 if (!isatty(fileno(stdin)))
1689 unset_nonblock(fileno(stdin));
1690 if (!isatty(fileno(stdout)))
1691 unset_nonblock(fileno(stdout));
1692 if (!isatty(fileno(stderr)))
1693 unset_nonblock(fileno(stderr));
1694
Damien Millerd6965512003-12-17 16:31:53 +11001695 /*
1696 * If there was no shell or command requested, there will be no remote
1697 * exit status to be returned. In that case, clear error code if the
1698 * connection was deliberately terminated at this end.
1699 */
1700 if (no_shell_flag && received_signal == SIGTERM) {
1701 received_signal = 0;
1702 exit_status = 0;
1703 }
1704
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001705 if (received_signal)
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001706 fatal("Killed by signal %d.", (int) received_signal);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001707
1708 /*
1709 * In interactive mode (with pseudo tty) display a message indicating
1710 * that the connection has been closed.
1711 */
1712 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001713 snprintf(buf, sizeof buf,
1714 "Connection to %.64s closed.\r\n", host);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001715 buffer_append(&stderr_buffer, buf, strlen(buf));
1716 }
1717
Damien Miller95def091999-11-25 00:26:21 +11001718 /* Output any buffered data for stdout. */
Damien Millercfd6e4f2011-01-16 23:18:33 +11001719 if (buffer_len(&stdout_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001720 len = atomicio(vwrite, fileno(stdout),
1721 buffer_ptr(&stdout_buffer), buffer_len(&stdout_buffer));
Damien Millercfd6e4f2011-01-16 23:18:33 +11001722 if (len < 0 || (u_int)len != buffer_len(&stdout_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001723 error("Write failed flushing stdout buffer.");
Damien Millercfd6e4f2011-01-16 23:18:33 +11001724 else
1725 buffer_consume(&stdout_buffer, len);
Damien Miller95def091999-11-25 00:26:21 +11001726 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001727
Damien Miller95def091999-11-25 00:26:21 +11001728 /* Output any buffered data for stderr. */
Damien Millercfd6e4f2011-01-16 23:18:33 +11001729 if (buffer_len(&stderr_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001730 len = atomicio(vwrite, fileno(stderr),
1731 buffer_ptr(&stderr_buffer), buffer_len(&stderr_buffer));
Damien Millercfd6e4f2011-01-16 23:18:33 +11001732 if (len < 0 || (u_int)len != buffer_len(&stderr_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001733 error("Write failed flushing stderr buffer.");
Damien Millercfd6e4f2011-01-16 23:18:33 +11001734 else
1735 buffer_consume(&stderr_buffer, len);
Damien Miller95def091999-11-25 00:26:21 +11001736 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001737
Damien Miller95def091999-11-25 00:26:21 +11001738 /* Clear and free any buffers. */
1739 memset(buf, 0, sizeof(buf));
1740 buffer_free(&stdin_buffer);
1741 buffer_free(&stdout_buffer);
1742 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001743
Damien Miller95def091999-11-25 00:26:21 +11001744 /* Report bytes transferred, and transfer rates. */
1745 total_time = get_current_time() - start_time;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001746 packet_get_bytes(&ibytes, &obytes);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001747 verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
Damien Miller821de0a2011-01-11 17:20:29 +11001748 (unsigned long long)obytes, (unsigned long long)ibytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001749 if (total_time > 0)
Damien Millerb61f3fc2008-07-11 17:36:48 +10001750 verbose("Bytes per second: sent %.1f, received %.1f",
1751 obytes / total_time, ibytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001752 /* Return the exit status of the program. */
1753 debug("Exit status %d", exit_status);
1754 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001755}
Damien Millerb38eff82000-04-01 11:09:21 +10001756
1757/*********/
1758
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001759static int
Damien Miller630d6f42002-01-22 23:17:30 +11001760client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001761{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001762 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001763 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001764 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001765 buffer_append(&stdout_buffer, data, data_len);
Damien Millera5103f42014-02-04 11:20:14 +11001766 explicit_bzero(data, data_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001767 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001768 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001769}
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001770static int
Damien Miller630d6f42002-01-22 23:17:30 +11001771client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001772{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001773 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001774 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001775 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001776 buffer_append(&stderr_buffer, data, data_len);
Damien Millera5103f42014-02-04 11:20:14 +11001777 explicit_bzero(data, data_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001778 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001779 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001780}
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001781static int
Damien Miller630d6f42002-01-22 23:17:30 +11001782client_input_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001783{
Damien Millerb38eff82000-04-01 11:09:21 +10001784 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001785 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001786 /* Acknowledge the exit. */
1787 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1788 packet_send();
1789 /*
1790 * Must wait for packet to be sent since we are
1791 * exiting the loop.
1792 */
1793 packet_write_wait();
1794 /* Flag that we want to exit. */
1795 quit_pending = 1;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001796 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001797}
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001798
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001799static int
Darren Tucker5dcdd212003-10-02 16:17:00 +10001800client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1801{
1802 Channel *c = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001803 int r, remote_id, sock;
Darren Tucker5dcdd212003-10-02 16:17:00 +10001804
1805 /* Read the remote channel number from the message. */
1806 remote_id = packet_get_int();
1807 packet_check_eom();
1808
1809 /*
1810 * Get a connection to the local authentication agent (this may again
1811 * get forwarded).
1812 */
djm@openbsd.org141efe42015-01-14 20:05:27 +00001813 if ((r = ssh_get_authentication_socket(&sock)) != 0 &&
1814 r != SSH_ERR_AGENT_NOT_PRESENT)
1815 debug("%s: ssh_get_authentication_socket: %s",
1816 __func__, ssh_err(r));
1817
Darren Tucker5dcdd212003-10-02 16:17:00 +10001818
1819 /*
1820 * If we could not connect the agent, send an error message back to
1821 * the server. This should never happen unless the agent dies,
1822 * because authentication forwarding is only enabled if we have an
1823 * agent.
1824 */
1825 if (sock >= 0) {
1826 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1827 -1, 0, 0, 0, "authentication agent connection", 1);
1828 c->remote_id = remote_id;
1829 c->force_drain = 1;
1830 }
1831 if (c == NULL) {
1832 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1833 packet_put_int(remote_id);
1834 } else {
1835 /* Send a confirmation to the remote host. */
1836 debug("Forwarding authentication connection.");
1837 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1838 packet_put_int(remote_id);
1839 packet_put_int(c->self);
1840 }
1841 packet_send();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001842 return 0;
Darren Tucker5dcdd212003-10-02 16:17:00 +10001843}
Damien Millerb38eff82000-04-01 11:09:21 +10001844
Ben Lindstrombba81212001-06-25 05:01:22 +00001845static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001846client_request_forwarded_tcpip(const char *request_type, int rchan)
1847{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001848 Channel *c = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001849 char *listen_address, *originator_address;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001850 u_short listen_port, originator_port;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001851
1852 /* Get rest of the packet */
1853 listen_address = packet_get_string(NULL);
1854 listen_port = packet_get_int();
1855 originator_address = packet_get_string(NULL);
1856 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001857 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001858
Damien Miller7acefbb2014-07-18 14:11:24 +10001859 debug("%s: listen %s port %d, originator %s port %d", __func__,
1860 listen_address, listen_port, originator_address, originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001861
Damien Miller4b3ed642014-07-02 15:29:40 +10001862 c = channel_connect_by_listen_address(listen_address, listen_port,
Damien Millerbd740252008-05-19 15:37:09 +10001863 "forwarded-tcpip", originator_address);
1864
Darren Tuckera627d422013-06-02 07:31:17 +10001865 free(originator_address);
1866 free(listen_address);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001867 return c;
1868}
1869
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001870static Channel *
Damien Miller7acefbb2014-07-18 14:11:24 +10001871client_request_forwarded_streamlocal(const char *request_type, int rchan)
1872{
1873 Channel *c = NULL;
1874 char *listen_path;
1875
1876 /* Get the remote path. */
1877 listen_path = packet_get_string(NULL);
1878 /* XXX: Skip reserved field for now. */
1879 if (packet_get_string_ptr(NULL) == NULL)
1880 fatal("%s: packet_get_string_ptr failed", __func__);
1881 packet_check_eom();
1882
1883 debug("%s: %s", __func__, listen_path);
1884
1885 c = channel_connect_by_listen_path(listen_path,
1886 "forwarded-streamlocal@openssh.com", "forwarded-streamlocal");
1887 free(listen_path);
1888 return c;
1889}
1890
1891static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001892client_request_x11(const char *request_type, int rchan)
1893{
1894 Channel *c = NULL;
1895 char *originator;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001896 u_short originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001897 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001898
1899 if (!options.forward_x11) {
1900 error("Warning: ssh server tried X11 forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001901 error("Warning: this is probably a break-in attempt by a "
1902 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001903 return NULL;
1904 }
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001905 if (x11_refuse_time != 0 && (u_int)monotime() >= x11_refuse_time) {
Damien Miller1ab6a512010-06-26 10:02:24 +10001906 verbose("Rejected X11 connection after ForwardX11Timeout "
1907 "expired");
1908 return NULL;
1909 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001910 originator = packet_get_string(NULL);
1911 if (datafellows & SSH_BUG_X11FWD) {
1912 debug2("buggy server: x11 request w/o originator_port");
1913 originator_port = 0;
1914 } else {
1915 originator_port = packet_get_int();
1916 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001917 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001918 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001919 debug("client_request_x11: request from %s %d", originator,
1920 originator_port);
Darren Tuckera627d422013-06-02 07:31:17 +10001921 free(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001922 sock = x11_connect_display();
1923 if (sock < 0)
1924 return NULL;
1925 c = channel_new("x11",
1926 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001927 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001928 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001929 return c;
1930}
1931
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001932static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001933client_request_agent(const char *request_type, int rchan)
1934{
1935 Channel *c = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001936 int r, sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001937
1938 if (!options.forward_agent) {
1939 error("Warning: ssh server tried agent forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001940 error("Warning: this is probably a break-in attempt by a "
1941 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001942 return NULL;
1943 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001944 if ((r = ssh_get_authentication_socket(&sock)) != 0) {
1945 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1946 debug("%s: ssh_get_authentication_socket: %s",
1947 __func__, ssh_err(r));
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001948 return NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001949 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001950 c = channel_new("authentication agent connection",
1951 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001952 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001953 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001954 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001955 return c;
1956}
1957
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001958int
1959client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun)
1960{
1961 Channel *c;
1962 int fd;
1963
1964 if (tun_mode == SSH_TUNMODE_NO)
1965 return 0;
1966
1967 if (!compat20) {
Damien Millerb3f2c9f2009-01-28 16:15:30 +11001968 error("Tunnel forwarding is not supported for protocol 1");
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001969 return -1;
1970 }
1971
1972 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1973
1974 /* Open local tunnel device */
1975 if ((fd = tun_open(local_tun, tun_mode)) == -1) {
1976 error("Tunnel device open failed.");
1977 return -1;
1978 }
1979
1980 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1981 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1982 c->datagram = 1;
1983
1984#if defined(SSH_TUN_FILTER)
1985 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1986 channel_register_filter(c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +10001987 sys_tun_outfilter, NULL, NULL);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001988#endif
1989
1990 packet_start(SSH2_MSG_CHANNEL_OPEN);
1991 packet_put_cstring("tun@openssh.com");
1992 packet_put_int(c->self);
1993 packet_put_int(c->local_window_max);
1994 packet_put_int(c->local_maxpacket);
1995 packet_put_int(tun_mode);
1996 packet_put_int(remote_tun);
1997 packet_send();
1998
1999 return 0;
2000}
2001
Damien Millerbd483e72000-04-30 10:00:53 +10002002/* XXXX move to generic input handler */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002003static int
Damien Miller630d6f42002-01-22 23:17:30 +11002004client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10002005{
2006 Channel *c = NULL;
2007 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10002008 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002009 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10002010
2011 ctype = packet_get_string(&len);
2012 rchan = packet_get_int();
2013 rwindow = packet_get_int();
2014 rmaxpack = packet_get_int();
2015
Damien Millere247cc42000-05-07 12:03:14 +10002016 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10002017 ctype, rchan, rwindow, rmaxpack);
2018
Damien Miller0bc1bd82000-11-13 22:57:25 +11002019 if (strcmp(ctype, "forwarded-tcpip") == 0) {
2020 c = client_request_forwarded_tcpip(ctype, rchan);
Damien Miller7acefbb2014-07-18 14:11:24 +10002021 } else if (strcmp(ctype, "forwarded-streamlocal@openssh.com") == 0) {
2022 c = client_request_forwarded_streamlocal(ctype, rchan);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002023 } else if (strcmp(ctype, "x11") == 0) {
2024 c = client_request_x11(ctype, rchan);
2025 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
2026 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10002027 }
2028/* XXX duplicate : */
2029 if (c != NULL) {
2030 debug("confirm %s", ctype);
2031 c->remote_id = rchan;
2032 c->remote_window = rwindow;
2033 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00002034 if (c->type != SSH_CHANNEL_CONNECTING) {
2035 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
2036 packet_put_int(c->remote_id);
2037 packet_put_int(c->self);
2038 packet_put_int(c->local_window);
2039 packet_put_int(c->local_maxpacket);
2040 packet_send();
2041 }
Damien Millerbd483e72000-04-30 10:00:53 +10002042 } else {
2043 debug("failure %s", ctype);
2044 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
2045 packet_put_int(rchan);
2046 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00002047 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00002048 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00002049 packet_put_cstring("");
2050 }
Damien Millerbd483e72000-04-30 10:00:53 +10002051 packet_send();
2052 }
Darren Tuckera627d422013-06-02 07:31:17 +10002053 free(ctype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002054 return 0;
Damien Millerbd483e72000-04-30 10:00:53 +10002055}
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002056
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002057static int
Damien Miller630d6f42002-01-22 23:17:30 +11002058client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00002059{
2060 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002061 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00002062 char *rtype;
2063
2064 id = packet_get_int();
2065 rtype = packet_get_string(NULL);
2066 reply = packet_get_char();
2067
2068 debug("client_input_channel_req: channel %d rtype %s reply %d",
2069 id, rtype, reply);
2070
Damien Miller3bbd8782004-06-18 22:23:22 +10002071 if (id == -1) {
2072 error("client_input_channel_req: request for channel -1");
2073 } else if ((c = channel_lookup(id)) == NULL) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10002074 error("client_input_channel_req: channel %d: "
2075 "unknown channel", id);
Damien Millerbab9bd42008-05-19 16:06:47 +10002076 } else if (strcmp(rtype, "eow@openssh.com") == 0) {
2077 packet_check_eom();
2078 chan_rcvd_eow(c);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002079 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10002080 exitval = packet_get_int();
Damien Millere1537f92010-01-26 13:26:22 +11002081 if (c->ctl_chan != -1) {
2082 mux_exit_message(c, exitval);
Damien Miller0e220db2004-06-15 10:34:08 +10002083 success = 1;
Darren Tucker29440822010-01-08 17:08:35 +11002084 } else if (id == session_ident) {
2085 /* Record exit value of local session */
2086 success = 1;
2087 exit_status = exitval;
2088 } else {
Damien Miller36f57eb2010-01-30 17:28:34 +11002089 /* Probably for a mux channel that has already closed */
2090 debug("%s: no sink for exit-status on channel %d",
2091 __func__, id);
Damien Miller0e220db2004-06-15 10:34:08 +10002092 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002093 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00002094 }
Damien Millerc5893782014-05-15 13:48:49 +10002095 if (reply && c != NULL && !(c->flags & CHAN_CLOSE_SENT)) {
Ben Lindstrom5b828322001-02-09 01:34:36 +00002096 packet_start(success ?
2097 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller8533c782008-12-08 09:54:40 +11002098 packet_put_int(c->remote_id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002099 packet_send();
2100 }
Darren Tuckera627d422013-06-02 07:31:17 +10002101 free(rtype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002102 return 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00002103}
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002104
djm@openbsd.org523463a2015-02-16 22:13:32 +00002105struct hostkeys_update_ctx {
2106 /* The hostname and (optionally) IP address string for the server */
2107 char *host_str, *ip_str;
2108
2109 /*
2110 * Keys received from the server and a flag for each indicating
2111 * whether they already exist in known_hosts.
2112 * keys_seen is filled in by hostkeys_find() and later (for new
2113 * keys) by client_global_hostkeys_private_confirm().
2114 */
2115 struct sshkey **keys;
2116 int *keys_seen;
2117 size_t nkeys;
2118
2119 size_t nnew;
2120
2121 /*
2122 * Keys that are in known_hosts, but were not present in the update
2123 * from the server (i.e. scheduled to be deleted).
2124 * Filled in by hostkeys_find().
2125 */
2126 struct sshkey **old_keys;
2127 size_t nold;
2128};
2129
2130static void
2131hostkeys_update_ctx_free(struct hostkeys_update_ctx *ctx)
2132{
2133 size_t i;
2134
2135 if (ctx == NULL)
2136 return;
2137 for (i = 0; i < ctx->nkeys; i++)
2138 sshkey_free(ctx->keys[i]);
2139 free(ctx->keys);
2140 free(ctx->keys_seen);
2141 for (i = 0; i < ctx->nold; i++)
2142 sshkey_free(ctx->old_keys[i]);
2143 free(ctx->old_keys);
2144 free(ctx->host_str);
2145 free(ctx->ip_str);
2146 free(ctx);
2147}
2148
2149static int
2150hostkeys_find(struct hostkey_foreach_line *l, void *_ctx)
2151{
2152 struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
2153 size_t i;
2154 struct sshkey **tmp;
2155
2156 if (l->status != HKF_STATUS_MATCHED || l->key == NULL ||
2157 l->key->type == KEY_RSA1)
2158 return 0;
2159
2160 /* Mark off keys we've already seen for this host */
2161 for (i = 0; i < ctx->nkeys; i++) {
2162 if (sshkey_equal(l->key, ctx->keys[i])) {
2163 debug3("%s: found %s key at %s:%ld", __func__,
2164 sshkey_ssh_name(ctx->keys[i]), l->path, l->linenum);
2165 ctx->keys_seen[i] = 1;
2166 return 0;
2167 }
2168 }
2169 /* This line contained a key that not offered by the server */
2170 debug3("%s: deprecated %s key at %s:%ld", __func__,
2171 sshkey_ssh_name(l->key), l->path, l->linenum);
2172 if ((tmp = reallocarray(ctx->old_keys, ctx->nold + 1,
2173 sizeof(*ctx->old_keys))) == NULL)
2174 fatal("%s: reallocarray failed nold = %zu",
2175 __func__, ctx->nold);
2176 ctx->old_keys = tmp;
2177 ctx->old_keys[ctx->nold++] = l->key;
2178 l->key = NULL;
2179
2180 return 0;
2181}
2182
2183static void
2184update_known_hosts(struct hostkeys_update_ctx *ctx)
2185{
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00002186 int r, was_raw = 0;
2187 int loglevel = options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK ?
djm@openbsd.org523463a2015-02-16 22:13:32 +00002188 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_VERBOSE;
2189 char *fp, *response;
2190 size_t i;
2191
2192 for (i = 0; i < ctx->nkeys; i++) {
2193 if (ctx->keys_seen[i] != 2)
2194 continue;
2195 if ((fp = sshkey_fingerprint(ctx->keys[i],
2196 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
2197 fatal("%s: sshkey_fingerprint failed", __func__);
2198 do_log2(loglevel, "Learned new hostkey: %s %s",
2199 sshkey_type(ctx->keys[i]), fp);
2200 free(fp);
2201 }
2202 for (i = 0; i < ctx->nold; i++) {
2203 if ((fp = sshkey_fingerprint(ctx->old_keys[i],
2204 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
2205 fatal("%s: sshkey_fingerprint failed", __func__);
2206 do_log2(loglevel, "Deprecating obsolete hostkey: %s %s",
2207 sshkey_type(ctx->old_keys[i]), fp);
2208 free(fp);
2209 }
2210 if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK) {
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00002211 if (get_saved_tio() != NULL) {
2212 leave_raw_mode(1);
2213 was_raw = 1;
2214 }
djm@openbsd.org523463a2015-02-16 22:13:32 +00002215 response = NULL;
2216 for (i = 0; !quit_pending && i < 3; i++) {
2217 free(response);
2218 response = read_passphrase("Accept updated hostkeys? "
2219 "(yes/no): ", RP_ECHO);
2220 if (strcasecmp(response, "yes") == 0)
2221 break;
2222 else if (quit_pending || response == NULL ||
2223 strcasecmp(response, "no") == 0) {
2224 options.update_hostkeys = 0;
2225 break;
2226 } else {
2227 do_log2(loglevel, "Please enter "
2228 "\"yes\" or \"no\"");
2229 }
2230 }
2231 if (quit_pending || i >= 3 || response == NULL)
2232 options.update_hostkeys = 0;
2233 free(response);
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00002234 if (was_raw)
2235 enter_raw_mode(1);
djm@openbsd.org523463a2015-02-16 22:13:32 +00002236 }
2237
2238 /*
2239 * Now that all the keys are verified, we can go ahead and replace
2240 * them in known_hosts (assuming SSH_UPDATE_HOSTKEYS_ASK didn't
2241 * cancel the operation).
2242 */
2243 if (options.update_hostkeys != 0 &&
2244 (r = hostfile_replace_entries(options.user_hostfiles[0],
2245 ctx->host_str, ctx->ip_str, ctx->keys, ctx->nkeys,
2246 options.hash_known_hosts, 0,
2247 options.fingerprint_hash)) != 0)
2248 error("%s: hostfile_replace_entries failed: %s",
2249 __func__, ssh_err(r));
2250}
2251
2252static void
2253client_global_hostkeys_private_confirm(int type, u_int32_t seq, void *_ctx)
2254{
2255 struct ssh *ssh = active_state; /* XXX */
2256 struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
2257 size_t i, ndone;
2258 struct sshbuf *signdata;
2259 int r;
2260 const u_char *sig;
2261 size_t siglen;
2262
2263 if (ctx->nnew == 0)
2264 fatal("%s: ctx->nnew == 0", __func__); /* sanity */
2265 if (type != SSH2_MSG_REQUEST_SUCCESS) {
2266 error("Server failed to confirm ownership of "
2267 "private host keys");
2268 hostkeys_update_ctx_free(ctx);
2269 return;
2270 }
2271 if ((signdata = sshbuf_new()) == NULL)
2272 fatal("%s: sshbuf_new failed", __func__);
2273 /* Don't want to accidentally accept an unbound signature */
2274 if (ssh->kex->session_id_len == 0)
2275 fatal("%s: ssh->kex->session_id_len == 0", __func__);
2276 /*
2277 * Expect a signature for each of the ctx->nnew private keys we
2278 * haven't seen before. They will be in the same order as the
2279 * ctx->keys where the corresponding ctx->keys_seen[i] == 0.
2280 */
2281 for (ndone = i = 0; i < ctx->nkeys; i++) {
2282 if (ctx->keys_seen[i])
2283 continue;
2284 /* Prepare data to be signed: session ID, unique string, key */
2285 sshbuf_reset(signdata);
djm@openbsd.org44732de2015-02-20 22:17:21 +00002286 if ( (r = sshbuf_put_cstring(signdata,
2287 "hostkeys-prove-00@openssh.com")) != 0 ||
2288 (r = sshbuf_put_string(signdata, ssh->kex->session_id,
djm@openbsd.org523463a2015-02-16 22:13:32 +00002289 ssh->kex->session_id_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +00002290 (r = sshkey_puts(ctx->keys[i], signdata)) != 0)
2291 fatal("%s: failed to prepare signature: %s",
2292 __func__, ssh_err(r));
2293 /* Extract and verify signature */
2294 if ((r = sshpkt_get_string_direct(ssh, &sig, &siglen)) != 0) {
2295 error("%s: couldn't parse message: %s",
2296 __func__, ssh_err(r));
2297 goto out;
2298 }
2299 if ((r = sshkey_verify(ctx->keys[i], sig, siglen,
2300 sshbuf_ptr(signdata), sshbuf_len(signdata), 0)) != 0) {
2301 error("%s: server gave bad signature for %s key %zu",
2302 __func__, sshkey_type(ctx->keys[i]), i);
2303 goto out;
2304 }
2305 /* Key is good. Mark it as 'seen' */
2306 ctx->keys_seen[i] = 2;
2307 ndone++;
2308 }
2309 if (ndone != ctx->nnew)
2310 fatal("%s: ndone != ctx->nnew (%zu / %zu)", __func__,
2311 ndone, ctx->nnew); /* Shouldn't happen */
2312 ssh_packet_check_eom(ssh);
2313
2314 /* Make the edits to known_hosts */
2315 update_known_hosts(ctx);
2316 out:
2317 hostkeys_update_ctx_free(ctx);
2318}
2319
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002320/*
djm@openbsd.org44732de2015-02-20 22:17:21 +00002321 * Handle hostkeys-00@openssh.com global request to inform the client of all
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002322 * the server's hostkeys. The keys are checked against the user's
2323 * HostkeyAlgorithms preference before they are accepted.
2324 */
2325static int
2326client_input_hostkeys(void)
2327{
djm@openbsd.org523463a2015-02-16 22:13:32 +00002328 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002329 const u_char *blob = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002330 size_t i, len = 0;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002331 struct sshbuf *buf = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002332 struct sshkey *key = NULL, **tmp;
2333 int r;
2334 char *fp;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002335 static int hostkeys_seen = 0; /* XXX use struct ssh */
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00002336 extern struct sockaddr_storage hostaddr; /* XXX from ssh.c */
djm@openbsd.orga63cfa22015-02-25 19:54:02 +00002337 struct hostkeys_update_ctx *ctx = NULL;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002338
2339 if (hostkeys_seen)
2340 fatal("%s: server already sent hostkeys", __func__);
djm@openbsd.org523463a2015-02-16 22:13:32 +00002341 if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK &&
2342 options.batch_mode)
2343 return 1; /* won't ask in batchmode, so don't even try */
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002344 if (!options.update_hostkeys || options.num_user_hostfiles <= 0)
2345 return 1;
djm@openbsd.orga63cfa22015-02-25 19:54:02 +00002346
2347 ctx = xcalloc(1, sizeof(*ctx));
djm@openbsd.org523463a2015-02-16 22:13:32 +00002348 while (ssh_packet_remaining(ssh) > 0) {
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002349 sshkey_free(key);
2350 key = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002351 if ((r = sshpkt_get_string_direct(ssh, &blob, &len)) != 0) {
2352 error("%s: couldn't parse message: %s",
2353 __func__, ssh_err(r));
2354 goto out;
2355 }
djm@openbsd.org44732de2015-02-20 22:17:21 +00002356 if ((r = sshkey_from_blob(blob, len, &key)) != 0) {
2357 error("%s: parse key: %s", __func__, ssh_err(r));
2358 goto out;
2359 }
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002360 fp = sshkey_fingerprint(key, options.fingerprint_hash,
2361 SSH_FP_DEFAULT);
2362 debug3("%s: received %s key %s", __func__,
2363 sshkey_type(key), fp);
2364 free(fp);
2365 /* Check that the key is accepted in HostkeyAlgorithms */
2366 if (options.hostkeyalgorithms != NULL &&
2367 match_pattern_list(sshkey_ssh_name(key),
djm@openbsd.orge661a862015-05-04 06:10:48 +00002368 options.hostkeyalgorithms, 0) != 1) {
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002369 debug3("%s: %s key not permitted by HostkeyAlgorithms",
2370 __func__, sshkey_ssh_name(key));
2371 continue;
2372 }
djm@openbsd.org523463a2015-02-16 22:13:32 +00002373 /* Skip certs */
2374 if (sshkey_is_cert(key)) {
2375 debug3("%s: %s key is a certificate; skipping",
2376 __func__, sshkey_ssh_name(key));
2377 continue;
2378 }
2379 /* Ensure keys are unique */
2380 for (i = 0; i < ctx->nkeys; i++) {
2381 if (sshkey_equal(key, ctx->keys[i])) {
2382 error("%s: received duplicated %s host key",
2383 __func__, sshkey_ssh_name(key));
2384 goto out;
2385 }
2386 }
2387 /* Key is good, record it */
2388 if ((tmp = reallocarray(ctx->keys, ctx->nkeys + 1,
2389 sizeof(*ctx->keys))) == NULL)
2390 fatal("%s: reallocarray failed nkeys = %zu",
2391 __func__, ctx->nkeys);
2392 ctx->keys = tmp;
2393 ctx->keys[ctx->nkeys++] = key;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002394 key = NULL;
2395 }
2396
djm@openbsd.org523463a2015-02-16 22:13:32 +00002397 if (ctx->nkeys == 0) {
djm@openbsd.org44732de2015-02-20 22:17:21 +00002398 debug("%s: server sent no hostkeys", __func__);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002399 goto out;
2400 }
djm@openbsd.org44732de2015-02-20 22:17:21 +00002401
djm@openbsd.org523463a2015-02-16 22:13:32 +00002402 if ((ctx->keys_seen = calloc(ctx->nkeys,
2403 sizeof(*ctx->keys_seen))) == NULL)
2404 fatal("%s: calloc failed", __func__);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002405
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00002406 get_hostfile_hostname_ipaddr(host,
2407 options.check_host_ip ? (struct sockaddr *)&hostaddr : NULL,
djm@openbsd.org523463a2015-02-16 22:13:32 +00002408 options.port, &ctx->host_str,
2409 options.check_host_ip ? &ctx->ip_str : NULL);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002410
djm@openbsd.org523463a2015-02-16 22:13:32 +00002411 /* Find which keys we already know about. */
2412 if ((r = hostkeys_foreach(options.user_hostfiles[0], hostkeys_find,
2413 ctx, ctx->host_str, ctx->ip_str,
2414 HKF_WANT_PARSE_KEY|HKF_WANT_MATCH)) != 0) {
2415 error("%s: hostkeys_foreach failed: %s", __func__, ssh_err(r));
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002416 goto out;
2417 }
2418
djm@openbsd.org523463a2015-02-16 22:13:32 +00002419 /* Figure out if we have any new keys to add */
2420 ctx->nnew = 0;
2421 for (i = 0; i < ctx->nkeys; i++) {
2422 if (!ctx->keys_seen[i])
2423 ctx->nnew++;
2424 }
2425
2426 debug3("%s: %zu keys from server: %zu new, %zu retained. %zu to remove",
2427 __func__, ctx->nkeys, ctx->nnew, ctx->nkeys - ctx->nnew, ctx->nold);
2428
2429 if (ctx->nnew == 0 && ctx->nold != 0) {
2430 /* We have some keys to remove. Just do it. */
2431 update_known_hosts(ctx);
2432 } else if (ctx->nnew != 0) {
2433 /*
2434 * We have received hitherto-unseen keys from the server.
2435 * Ask the server to confirm ownership of the private halves.
2436 */
2437 debug3("%s: asking server to prove ownership for %zu keys",
2438 __func__, ctx->nnew);
2439 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
2440 (r = sshpkt_put_cstring(ssh,
djm@openbsd.org44732de2015-02-20 22:17:21 +00002441 "hostkeys-prove-00@openssh.com")) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +00002442 (r = sshpkt_put_u8(ssh, 1)) != 0) /* bool: want reply */
2443 fatal("%s: cannot prepare packet: %s",
2444 __func__, ssh_err(r));
2445 if ((buf = sshbuf_new()) == NULL)
2446 fatal("%s: sshbuf_new", __func__);
2447 for (i = 0; i < ctx->nkeys; i++) {
2448 if (ctx->keys_seen[i])
2449 continue;
2450 sshbuf_reset(buf);
2451 if ((r = sshkey_putb(ctx->keys[i], buf)) != 0)
2452 fatal("%s: sshkey_putb: %s",
2453 __func__, ssh_err(r));
2454 if ((r = sshpkt_put_stringb(ssh, buf)) != 0)
2455 fatal("%s: sshpkt_put_string: %s",
2456 __func__, ssh_err(r));
2457 }
2458 if ((r = sshpkt_send(ssh)) != 0)
2459 fatal("%s: sshpkt_send: %s", __func__, ssh_err(r));
2460 client_register_global_confirm(
2461 client_global_hostkeys_private_confirm, ctx);
2462 ctx = NULL; /* will be freed in callback */
2463 }
2464
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002465 /* Success */
2466 out:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002467 hostkeys_update_ctx_free(ctx);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002468 sshkey_free(key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002469 sshbuf_free(buf);
djm@openbsd.org523463a2015-02-16 22:13:32 +00002470 /*
2471 * NB. Return success for all cases. The server doesn't need to know
2472 * what the client does with its hosts file.
2473 */
2474 return 1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002475}
2476
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002477static int
Damien Millerc3fa4072002-01-22 23:21:58 +11002478client_input_global_request(int type, u_int32_t seq, void *ctxt)
2479{
2480 char *rtype;
2481 int want_reply;
2482 int success = 0;
2483
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002484 rtype = packet_get_cstring(NULL);
Damien Millerc3fa4072002-01-22 23:21:58 +11002485 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11002486 debug("client_input_global_request: rtype %s want_reply %d",
2487 rtype, want_reply);
djm@openbsd.org44732de2015-02-20 22:17:21 +00002488 if (strcmp(rtype, "hostkeys-00@openssh.com") == 0)
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002489 success = client_input_hostkeys();
Damien Millerc3fa4072002-01-22 23:21:58 +11002490 if (want_reply) {
2491 packet_start(success ?
2492 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
2493 packet_send();
2494 packet_write_wait();
2495 }
Darren Tuckera627d422013-06-02 07:31:17 +10002496 free(rtype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002497 return 0;
Damien Millerc3fa4072002-01-22 23:21:58 +11002498}
Damien Millerbd483e72000-04-30 10:00:53 +10002499
Damien Miller0e220db2004-06-15 10:34:08 +10002500void
Darren Tuckerfc959702004-07-17 16:12:08 +10002501client_session2_setup(int id, int want_tty, int want_subsystem,
Damien Miller5771ed72008-05-19 15:35:33 +10002502 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env)
Damien Miller0e220db2004-06-15 10:34:08 +10002503{
2504 int len;
Darren Tucker5d78de62004-11-05 20:35:44 +11002505 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002506
2507 debug2("%s: id %d", __func__, id);
2508
Darren Tucker5d78de62004-11-05 20:35:44 +11002509 if ((c = channel_lookup(id)) == NULL)
2510 fatal("client_session2_setup: channel %d: unknown channel", id);
2511
Damien Miller0dac6fb2010-11-20 15:19:38 +11002512 packet_set_interactive(want_tty,
2513 options.ip_qos_interactive, options.ip_qos_bulk);
2514
Damien Miller0e220db2004-06-15 10:34:08 +10002515 if (want_tty) {
2516 struct winsize ws;
Damien Miller0e220db2004-06-15 10:34:08 +10002517
2518 /* Store window size in the packet. */
2519 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
2520 memset(&ws, 0, sizeof(ws));
2521
Damien Miller5771ed72008-05-19 15:35:33 +10002522 channel_request_start(id, "pty-req", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002523 client_expect_confirm(id, "PTY allocation", CONFIRM_TTY);
Damien Miller0e220db2004-06-15 10:34:08 +10002524 packet_put_cstring(term != NULL ? term : "");
Damien Miller71a73672006-03-26 14:04:36 +11002525 packet_put_int((u_int)ws.ws_col);
2526 packet_put_int((u_int)ws.ws_row);
2527 packet_put_int((u_int)ws.ws_xpixel);
2528 packet_put_int((u_int)ws.ws_ypixel);
Darren Tuckerdf189fb2008-06-08 12:55:32 +10002529 if (tiop == NULL)
2530 tiop = get_saved_tio();
2531 tty_make_modes(-1, tiop);
Damien Miller0e220db2004-06-15 10:34:08 +10002532 packet_send();
2533 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11002534 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10002535 }
2536
2537 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10002538 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10002539 int i, j, matched;
Damien Miller0e220db2004-06-15 10:34:08 +10002540 char *name, *val;
2541
2542 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10002543 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10002544 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10002545 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10002546 if ((val = strchr(name, '=')) == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +10002547 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002548 continue;
2549 }
2550 *val++ = '\0';
2551
2552 matched = 0;
2553 for (j = 0; j < options.num_send_env; j++) {
2554 if (match_pattern(name, options.send_env[j])) {
2555 matched = 1;
2556 break;
2557 }
2558 }
2559 if (!matched) {
2560 debug3("Ignored env %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10002561 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002562 continue;
2563 }
2564
2565 debug("Sending env %s = %s", name, val);
2566 channel_request_start(id, "env", 0);
2567 packet_put_cstring(name);
2568 packet_put_cstring(val);
2569 packet_send();
Darren Tuckera627d422013-06-02 07:31:17 +10002570 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002571 }
2572 }
2573
2574 len = buffer_len(cmd);
2575 if (len > 0) {
2576 if (len > 900)
2577 len = 900;
2578 if (want_subsystem) {
Damien Miller5771ed72008-05-19 15:35:33 +10002579 debug("Sending subsystem: %.*s",
2580 len, (u_char*)buffer_ptr(cmd));
2581 channel_request_start(id, "subsystem", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002582 client_expect_confirm(id, "subsystem", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002583 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002584 debug("Sending command: %.*s",
2585 len, (u_char*)buffer_ptr(cmd));
2586 channel_request_start(id, "exec", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002587 client_expect_confirm(id, "exec", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002588 }
2589 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
2590 packet_send();
2591 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002592 channel_request_start(id, "shell", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002593 client_expect_confirm(id, "shell", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002594 packet_send();
2595 }
2596}
2597
Ben Lindstrombba81212001-06-25 05:01:22 +00002598static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002599client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10002600{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002601 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10002602
Damien Miller1383bd82000-04-06 12:32:37 +10002603 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2604 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2605 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2606 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10002607 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10002608 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2609 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002610 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10002611 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerb84886b2008-05-19 15:05:07 +10002612 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2613 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
Damien Millerc3fa4072002-01-22 23:21:58 +11002614 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002615
2616 /* rekeying */
2617 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10002618
2619 /* global request reply messages */
2620 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2621 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10002622}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002623
Ben Lindstrombba81212001-06-25 05:01:22 +00002624static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002625client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002626{
2627 dispatch_init(NULL);
2628 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
2629 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
2630 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10002631 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2632 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
2633 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002634 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
2635 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
2636 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11002637
2638 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
Darren Tucker5dcdd212003-10-02 16:17:00 +10002639 &client_input_agent_open : &deny_input_open);
Damien Miller69b69aa2000-10-28 14:19:58 +11002640 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
2641 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002642}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002643
Ben Lindstrombba81212001-06-25 05:01:22 +00002644static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002645client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002646{
2647 client_init_dispatch_13();
2648 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
2649 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
2650}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002651
Ben Lindstromdb47f382001-07-04 05:10:27 +00002652static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002653client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002654{
Damien Miller1383bd82000-04-06 12:32:37 +10002655 if (compat20)
2656 client_init_dispatch_20();
2657 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10002658 client_init_dispatch_13();
2659 else
2660 client_init_dispatch_15();
2661}
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002662
Damien Miller6c3eec72011-05-05 14:16:22 +10002663void
2664client_stop_mux(void)
2665{
2666 if (options.control_path != NULL && muxserver_sock != -1)
2667 unlink(options.control_path);
2668 /*
Darren Tucker48bf4b02012-09-07 16:38:53 +10002669 * If we are in persist mode, or don't have a shell, signal that we
2670 * should close when all active channels are closed.
Damien Miller6c3eec72011-05-05 14:16:22 +10002671 */
Darren Tucker48bf4b02012-09-07 16:38:53 +10002672 if (options.control_persist || no_shell_flag) {
Damien Miller6c3eec72011-05-05 14:16:22 +10002673 session_closed = 1;
Damien Miller4ac99c32011-06-20 14:43:31 +10002674 setproctitle("[stopped mux]");
2675 }
Damien Miller6c3eec72011-05-05 14:16:22 +10002676}
2677
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002678/* client specific fatal cleanup */
2679void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002680cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002681{
Damien Miller21771e22011-05-15 08:45:50 +10002682 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002683 leave_non_blocking();
Damien Millerb1cbfa22008-05-19 16:00:08 +10002684 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10002685 unlink(options.control_path);
Damien Millera41ccca2010-10-07 22:07:32 +11002686 ssh_kill_proxy_command();
Darren Tucker3e33cec2003-10-02 16:12:36 +10002687 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002688}