blob: 4522a63321639d6db52390e7b5b2b13d009149e1 [file] [log] [blame]
deraadt@openbsd.org087266e2015-01-20 23:14:00 +00001/* $OpenBSD: clientloop.c,v 1.266 2015/01/20 23:14:00 deraadt 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"
Damien Miller69b69aa2000-10-28 14:19:58 +1100115
116/* import options */
117extern Options options;
118
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119/* Flag indicating that stdin should be redirected from /dev/null. */
120extern int stdin_null_flag;
121
Damien Millerd6965512003-12-17 16:31:53 +1100122/* Flag indicating that no shell has been requested */
123extern int no_shell_flag;
124
Damien Miller0e220db2004-06-15 10:34:08 +1000125/* Control socket */
Damien Millere1537f92010-01-26 13:26:22 +1100126extern int muxserver_sock; /* XXX use mux_client_cleanup() instead */
Damien Miller0e220db2004-06-15 10:34:08 +1000127
Damien Miller5428f641999-11-25 11:54:57 +1100128/*
129 * Name of the host we are connecting to. This is the name given on the
130 * command line, or the HostName specified for the user-supplied name in a
131 * configuration file.
132 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133extern char *host;
134
Damien Miller5428f641999-11-25 11:54:57 +1100135/*
136 * Flag to indicate that we have received a window change signal which has
137 * not yet been processed. This will cause a message indicating the new
138 * window size to be sent to the server a little later. This is volatile
139 * because this is updated in a signal handler.
140 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000141static volatile sig_atomic_t received_window_change_signal = 0;
142static volatile sig_atomic_t received_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143
Damien Miller788f2122005-11-05 15:14:59 +1100144/* Flag indicating whether the user's terminal is in non-blocking mode. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145static int in_non_blocking_mode = 0;
146
Damien Millere11e1ea2010-08-03 16:04:46 +1000147/* Time when backgrounded control master using ControlPersist should exit */
148static time_t control_persist_exit_time = 0;
149
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150/* Common data for the client loop code. */
Damien Millere1537f92010-01-26 13:26:22 +1100151volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000152static int escape_char1; /* Escape character. (proto1 only) */
153static int escape_pending1; /* Last character was an escape (proto1 only) */
Damien Miller95def091999-11-25 00:26:21 +1100154static int last_was_cr; /* Last character was a newline. */
Darren Tucker4d5cd332008-06-13 04:51:14 +1000155static int exit_status; /* Used to store the command exit status. */
156static int stdin_eof; /* EOF has been encountered on stderr. */
Damien Miller95def091999-11-25 00:26:21 +1100157static Buffer stdin_buffer; /* Buffer for stdin data. */
158static Buffer stdout_buffer; /* Buffer for stdout data. */
159static Buffer stderr_buffer; /* Buffer for stderr data. */
Damien Miller1ab6a512010-06-26 10:02:24 +1000160static u_int buffer_high; /* Soft max buffer size. */
Damien Miller95def091999-11-25 00:26:21 +1100161static int connection_in; /* Connection to server (input). */
162static int connection_out; /* Connection to server (output). */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000163static int need_rekeying; /* Set to non-zero if rekeying is requested. */
Damien Miller1ab6a512010-06-26 10:02:24 +1000164static int session_closed; /* In SSH2: login session closed. */
165static int x11_refuse_time; /* If >0, refuse x11 opens after this time. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166
Ben Lindstrombba81212001-06-25 05:01:22 +0000167static void client_init_dispatch(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000168int session_ident = -1;
169
Darren Tuckere32cf432010-01-08 16:51:40 +1100170int session_resumed = 0;
171
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000172/* Track escape per proto2 channel */
173struct escape_filter_ctx {
174 int escape_pending;
175 int escape_char;
176};
177
178/* Context for channel confirmation replies */
Damien Miller5771ed72008-05-19 15:35:33 +1000179struct channel_reply_ctx {
180 const char *request_type;
Damien Miller555f3b82011-05-15 08:48:05 +1000181 int id;
182 enum confirm_action action;
Damien Miller5771ed72008-05-19 15:35:33 +1000183};
184
Darren Tucker9f407c42008-06-13 04:50:27 +1000185/* Global request success/failure callbacks */
186struct global_confirm {
187 TAILQ_ENTRY(global_confirm) entry;
188 global_confirm_cb *cb;
189 void *ctx;
190 int ref_count;
191};
192TAILQ_HEAD(global_confirms, global_confirm);
193static struct global_confirms global_confirms =
194 TAILQ_HEAD_INITIALIZER(global_confirms);
195
Damien Miller0e220db2004-06-15 10:34:08 +1000196void ssh_process_session2_setup(int, int, int, Buffer *);
197
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198/* Restores stdin to blocking mode. */
199
Ben Lindstrombba81212001-06-25 05:01:22 +0000200static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000201leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202{
Damien Miller95def091999-11-25 00:26:21 +1100203 if (in_non_blocking_mode) {
Damien Miller03e66f62004-06-15 15:47:51 +1000204 unset_nonblock(fileno(stdin));
Damien Miller95def091999-11-25 00:26:21 +1100205 in_non_blocking_mode = 0;
Damien Miller95def091999-11-25 00:26:21 +1100206 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000207}
208
Damien Miller95def091999-11-25 00:26:21 +1100209/* Puts stdin terminal in non-blocking mode. */
210
Ben Lindstrombba81212001-06-25 05:01:22 +0000211static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000212enter_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000213{
Damien Miller95def091999-11-25 00:26:21 +1100214 in_non_blocking_mode = 1;
Damien Miller232711f2004-06-15 10:35:30 +1000215 set_nonblock(fileno(stdin));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000216}
217
Damien Miller5428f641999-11-25 11:54:57 +1100218/*
219 * Signal handler for the window change signal (SIGWINCH). This just sets a
220 * flag indicating that the window has changed.
221 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100222/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000223static void
Damien Miller95def091999-11-25 00:26:21 +1100224window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225{
Damien Miller95def091999-11-25 00:26:21 +1100226 received_window_change_signal = 1;
227 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228}
229
Damien Miller5428f641999-11-25 11:54:57 +1100230/*
231 * Signal handler for signals that cause the program to terminate. These
232 * signals must be trapped to restore terminal modes.
233 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100234/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000235static void
Damien Miller95def091999-11-25 00:26:21 +1100236signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000238 received_signal = sig;
239 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240}
241
Damien Miller5428f641999-11-25 11:54:57 +1100242/*
243 * Returns current time in seconds from Jan 1, 1970 with the maximum
244 * available resolution.
245 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246
Ben Lindstrombba81212001-06-25 05:01:22 +0000247static double
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000248get_current_time(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000249{
Damien Miller95def091999-11-25 00:26:21 +1100250 struct timeval tv;
251 gettimeofday(&tv, NULL);
252 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253}
254
Damien Millere11e1ea2010-08-03 16:04:46 +1000255/*
256 * Sets control_persist_exit_time to the absolute time when the
257 * backgrounded control master should exit due to expiry of the
258 * ControlPersist timeout. Sets it to 0 if we are not a backgrounded
259 * control master process, or if there is no ControlPersist timeout.
260 */
261static void
262set_control_persist_exit_time(void)
263{
264 if (muxserver_sock == -1 || !options.control_persist
Damien Miller6c3eec72011-05-05 14:16:22 +1000265 || options.control_persist_timeout == 0) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000266 /* not using a ControlPersist timeout */
267 control_persist_exit_time = 0;
Damien Miller6c3eec72011-05-05 14:16:22 +1000268 } else if (channel_still_open()) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000269 /* some client connections are still open */
270 if (control_persist_exit_time > 0)
271 debug2("%s: cancel scheduled exit", __func__);
272 control_persist_exit_time = 0;
273 } else if (control_persist_exit_time <= 0) {
274 /* a client connection has recently closed */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000275 control_persist_exit_time = monotime() +
Damien Millere11e1ea2010-08-03 16:04:46 +1000276 (time_t)options.control_persist_timeout;
277 debug2("%s: schedule exit in %d seconds", __func__,
278 options.control_persist_timeout);
279 }
280 /* else we are already counting down to the timeout */
281}
282
Damien Miller8d60be52012-02-11 08:18:17 +1100283#define SSH_X11_VALID_DISPLAY_CHARS ":/.-_"
284static int
285client_x11_display_valid(const char *display)
286{
287 size_t i, dlen;
288
289 dlen = strlen(display);
290 for (i = 0; i < dlen; i++) {
Damien Millerfdb23062013-11-21 13:57:15 +1100291 if (!isalnum((u_char)display[i]) &&
Damien Miller8d60be52012-02-11 08:18:17 +1100292 strchr(SSH_X11_VALID_DISPLAY_CHARS, display[i]) == NULL) {
293 debug("Invalid character '%c' in DISPLAY", display[i]);
294 return 0;
295 }
296 }
297 return 1;
298}
299
Damien Miller17e7ed02005-06-17 12:54:33 +1000300#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
301void
302client_x11_get_proto(const char *display, const char *xauth_path,
Damien Miller1ab6a512010-06-26 10:02:24 +1000303 u_int trusted, u_int timeout, char **_proto, char **_data)
Damien Miller17e7ed02005-06-17 12:54:33 +1000304{
305 char cmd[1024];
306 char line[512];
307 char xdisplay[512];
308 static char proto[512], data[512];
309 FILE *f;
310 int got_data = 0, generated = 0, do_unlink = 0, i;
311 char *xauthdir, *xauthfile;
312 struct stat st;
Damien Miller1ab6a512010-06-26 10:02:24 +1000313 u_int now;
Damien Miller17e7ed02005-06-17 12:54:33 +1000314
315 xauthdir = xauthfile = NULL;
316 *_proto = proto;
317 *_data = data;
318 proto[0] = data[0] = '\0';
319
320 if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) {
321 debug("No xauth program.");
Damien Miller8d60be52012-02-11 08:18:17 +1100322 } else if (!client_x11_display_valid(display)) {
323 logit("DISPLAY '%s' invalid, falling back to fake xauth data",
324 display);
Damien Miller17e7ed02005-06-17 12:54:33 +1000325 } else {
326 if (display == NULL) {
327 debug("x11_get_proto: DISPLAY not set");
328 return;
329 }
330 /*
331 * Handle FamilyLocal case where $DISPLAY does
332 * not match an authorization entry. For this we
333 * just try "xauth list unix:displaynum.screennum".
334 * XXX: "localhost" match to determine FamilyLocal
335 * is not perfect.
336 */
337 if (strncmp(display, "localhost:", 10) == 0) {
338 snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
339 display + 10);
340 display = xdisplay;
341 }
342 if (trusted == 0) {
deraadt@openbsd.org087266e2015-01-20 23:14:00 +0000343 xauthdir = xmalloc(PATH_MAX);
344 xauthfile = xmalloc(PATH_MAX);
345 mktemp_proto(xauthdir, PATH_MAX);
Damien Miller17e7ed02005-06-17 12:54:33 +1000346 if (mkdtemp(xauthdir) != NULL) {
347 do_unlink = 1;
deraadt@openbsd.org087266e2015-01-20 23:14:00 +0000348 snprintf(xauthfile, PATH_MAX, "%s/xauthfile",
Damien Miller17e7ed02005-06-17 12:54:33 +1000349 xauthdir);
350 snprintf(cmd, sizeof(cmd),
351 "%s -f %s generate %s " SSH_X11_PROTO
Damien Miller1ab6a512010-06-26 10:02:24 +1000352 " untrusted timeout %u 2>" _PATH_DEVNULL,
353 xauth_path, xauthfile, display, timeout);
Damien Miller17e7ed02005-06-17 12:54:33 +1000354 debug2("x11_get_proto: %s", cmd);
355 if (system(cmd) == 0)
356 generated = 1;
Damien Miller1ab6a512010-06-26 10:02:24 +1000357 if (x11_refuse_time == 0) {
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000358 now = monotime() + 1;
Damien Miller1ab6a512010-06-26 10:02:24 +1000359 if (UINT_MAX - timeout < now)
360 x11_refuse_time = UINT_MAX;
361 else
362 x11_refuse_time = now + timeout;
363 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000364 }
365 }
Darren Tucker513d13a2007-08-15 19:13:41 +1000366
367 /*
368 * When in untrusted mode, we read the cookie only if it was
369 * successfully generated as an untrusted one in the step
370 * above.
371 */
372 if (trusted || generated) {
373 snprintf(cmd, sizeof(cmd),
374 "%s %s%s list %s 2>" _PATH_DEVNULL,
375 xauth_path,
376 generated ? "-f " : "" ,
377 generated ? xauthfile : "",
378 display);
379 debug2("x11_get_proto: %s", cmd);
380 f = popen(cmd, "r");
381 if (f && fgets(line, sizeof(line), f) &&
382 sscanf(line, "%*s %511s %511s", proto, data) == 2)
383 got_data = 1;
384 if (f)
385 pclose(f);
386 } else
387 error("Warning: untrusted X11 forwarding setup failed: "
388 "xauth key data not generated");
Damien Miller17e7ed02005-06-17 12:54:33 +1000389 }
390
391 if (do_unlink) {
392 unlink(xauthfile);
393 rmdir(xauthdir);
394 }
Darren Tuckera627d422013-06-02 07:31:17 +1000395 free(xauthdir);
396 free(xauthfile);
Damien Miller17e7ed02005-06-17 12:54:33 +1000397
398 /*
399 * If we didn't get authentication data, just make up some
400 * data. The forwarding code will check the validity of the
401 * response anyway, and substitute this data. The X11
402 * server, however, will ignore this fake data and use
403 * whatever authentication mechanisms it was using otherwise
404 * for the local connection.
405 */
406 if (!got_data) {
407 u_int32_t rnd = 0;
408
409 logit("Warning: No xauth data; "
410 "using fake authentication data for X11 forwarding.");
411 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
412 for (i = 0; i < 16; i++) {
413 if (i % 4 == 0)
414 rnd = arc4random();
415 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
416 rnd & 0xff);
417 rnd >>= 8;
418 }
419 }
420}
421
Damien Miller5428f641999-11-25 11:54:57 +1100422/*
423 * This is called when the interactive is entered. This checks if there is
424 * an EOF coming on stdin. We must check this explicitly, as select() does
425 * not appear to wake up when redirecting from /dev/null.
426 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000427
Ben Lindstrombba81212001-06-25 05:01:22 +0000428static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000429client_check_initial_eof_on_stdin(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430{
Damien Miller95def091999-11-25 00:26:21 +1100431 int len;
432 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433
Damien Miller5428f641999-11-25 11:54:57 +1100434 /*
435 * If standard input is to be "redirected from /dev/null", we simply
436 * mark that we have seen an EOF and send an EOF message to the
437 * server. Otherwise, we try to read a single character; it appears
438 * that for some files, such /dev/null, select() never wakes up for
439 * read for this descriptor, which means that we never get EOF. This
440 * way we will get the EOF if stdin comes from /dev/null or similar.
441 */
Damien Miller95def091999-11-25 00:26:21 +1100442 if (stdin_null_flag) {
443 /* Fake EOF on stdin. */
444 debug("Sending eof.");
445 stdin_eof = 1;
446 packet_start(SSH_CMSG_EOF);
447 packet_send();
448 } else {
Damien Miller95def091999-11-25 00:26:21 +1100449 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000450
Damien Miller95def091999-11-25 00:26:21 +1100451 /* Check for immediate EOF on stdin. */
452 len = read(fileno(stdin), buf, 1);
453 if (len == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000454 /*
455 * EOF. Record that we have seen it and send
456 * EOF to server.
457 */
Damien Miller95def091999-11-25 00:26:21 +1100458 debug("Sending eof.");
459 stdin_eof = 1;
460 packet_start(SSH_CMSG_EOF);
461 packet_send();
462 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100463 /*
464 * Got data. We must store the data in the buffer,
465 * and also process it as an escape character if
466 * appropriate.
467 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000468 if ((u_char) buf[0] == escape_char1)
469 escape_pending1 = 1;
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000470 else
Damien Miller95def091999-11-25 00:26:21 +1100471 buffer_append(&stdin_buffer, buf, 1);
Damien Miller95def091999-11-25 00:26:21 +1100472 }
Damien Miller95def091999-11-25 00:26:21 +1100473 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475}
476
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477
Damien Miller5428f641999-11-25 11:54:57 +1100478/*
479 * Make packets from buffered stdin data, and buffer them for sending to the
480 * connection.
481 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000482
Ben Lindstrombba81212001-06-25 05:01:22 +0000483static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000484client_make_packets_from_stdin_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000485{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000486 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000487
Damien Miller95def091999-11-25 00:26:21 +1100488 /* Send buffered stdin data to the server. */
489 while (buffer_len(&stdin_buffer) > 0 &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100490 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100491 len = buffer_len(&stdin_buffer);
492 /* Keep the packets at reasonable size. */
493 if (len > packet_get_maxsize())
494 len = packet_get_maxsize();
495 packet_start(SSH_CMSG_STDIN_DATA);
496 packet_put_string(buffer_ptr(&stdin_buffer), len);
497 packet_send();
498 buffer_consume(&stdin_buffer, len);
499 /* If we have a pending EOF, send it now. */
500 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
501 packet_start(SSH_CMSG_EOF);
502 packet_send();
503 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000504 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000505}
506
Damien Miller5428f641999-11-25 11:54:57 +1100507/*
508 * Checks if the client window has changed, and sends a packet about it to
509 * the server if so. The actual change is detected elsewhere (by a software
510 * interrupt on Unix); this just checks the flag and sends a message if
511 * appropriate.
512 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000513
Ben Lindstrombba81212001-06-25 05:01:22 +0000514static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000515client_check_window_change(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000516{
Damien Miller1383bd82000-04-06 12:32:37 +1000517 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518
Damien Miller1383bd82000-04-06 12:32:37 +1000519 if (! received_window_change_signal)
520 return;
521 /** XXX race */
522 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000523
Damien Millerd3444942000-09-30 14:20:03 +1100524 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000525
526 if (compat20) {
Damien Miller0e220db2004-06-15 10:34:08 +1000527 channel_send_window_changes();
Damien Miller1383bd82000-04-06 12:32:37 +1000528 } else {
Damien Miller0e220db2004-06-15 10:34:08 +1000529 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
530 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000531 packet_start(SSH_CMSG_WINDOW_SIZE);
Damien Miller71a73672006-03-26 14:04:36 +1100532 packet_put_int((u_int)ws.ws_row);
533 packet_put_int((u_int)ws.ws_col);
534 packet_put_int((u_int)ws.ws_xpixel);
535 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller1383bd82000-04-06 12:32:37 +1000536 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000537 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000538}
539
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000540static int
Damien Miller509b0102003-12-17 16:33:10 +1100541client_global_request_reply(int type, u_int32_t seq, void *ctxt)
542{
Darren Tucker9f407c42008-06-13 04:50:27 +1000543 struct global_confirm *gc;
544
545 if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000546 return 0;
Darren Tucker9f407c42008-06-13 04:50:27 +1000547 if (gc->cb != NULL)
548 gc->cb(type, seq, gc->ctx);
549 if (--gc->ref_count <= 0) {
550 TAILQ_REMOVE(&global_confirms, gc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +1100551 explicit_bzero(gc, sizeof(*gc));
Darren Tuckera627d422013-06-02 07:31:17 +1000552 free(gc);
Darren Tucker9f407c42008-06-13 04:50:27 +1000553 }
554
Darren Tuckerf7288d72009-06-21 18:12:20 +1000555 packet_set_alive_timeouts(0);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000556 return 0;
Damien Miller509b0102003-12-17 16:33:10 +1100557}
558
559static void
560server_alive_check(void)
561{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000562 if (packet_inc_alive_timeouts() > options.server_alive_count_max) {
Damien Millerb73b6fd2011-01-11 17:18:56 +1100563 logit("Timeout, server %s not responding.", host);
Damien Miller985a4482006-10-24 03:02:41 +1000564 cleanup_exit(255);
565 }
Damien Miller509b0102003-12-17 16:33:10 +1100566 packet_start(SSH2_MSG_GLOBAL_REQUEST);
567 packet_put_cstring("keepalive@openssh.com");
568 packet_put_char(1); /* boolean: want reply */
569 packet_send();
Darren Tucker9f407c42008-06-13 04:50:27 +1000570 /* Insert an empty placeholder to maintain ordering */
571 client_register_global_confirm(NULL, NULL);
Damien Miller509b0102003-12-17 16:33:10 +1100572}
573
Damien Miller5428f641999-11-25 11:54:57 +1100574/*
575 * Waits until the client can do something (some data becomes available on
576 * one of the file descriptors).
577 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000578static void
Damien Miller5e953212001-01-30 09:14:00 +1100579client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000580 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000581{
Damien Miller509b0102003-12-17 16:33:10 +1100582 struct timeval tv, *tvp;
Damien Millere11e1ea2010-08-03 16:04:46 +1000583 int timeout_secs;
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000584 time_t minwait_secs = 0, server_alive_time = 0, now = monotime();
Damien Miller509b0102003-12-17 16:33:10 +1100585 int ret;
586
Damien Miller5e953212001-01-30 09:14:00 +1100587 /* Add any selections by the channel mechanism. */
Damien Millera6508752012-04-22 11:21:10 +1000588 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp,
589 &minwait_secs, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000590
Damien Miller1383bd82000-04-06 12:32:37 +1000591 if (!compat20) {
592 /* Read from the connection, unless our buffers are full. */
593 if (buffer_len(&stdout_buffer) < buffer_high &&
594 buffer_len(&stderr_buffer) < buffer_high &&
595 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100596 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000597 /*
598 * Read from stdin, unless we have seen EOF or have very much
599 * buffered data to send to the server.
600 */
601 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100602 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000603
604 /* Select stdout/stderr if have data in buffer. */
605 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100606 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000607 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100608 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000609 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000610 /* channel_prepare_select could have closed the last channel */
Damien Miller164a7f42001-10-12 11:36:09 +1000611 if (session_closed && !channel_still_open() &&
612 !packet_have_data_to_write()) {
613 /* clear mask since we did not call select() */
Damien Miller79faeff2001-11-12 11:06:32 +1100614 memset(*readsetp, 0, *nallocp);
615 memset(*writesetp, 0, *nallocp);
Damien Miller164a7f42001-10-12 11:36:09 +1000616 return;
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000617 } else {
618 FD_SET(connection_in, *readsetp);
619 }
Damien Miller1383bd82000-04-06 12:32:37 +1000620 }
621
Damien Miller95def091999-11-25 00:26:21 +1100622 /* Select server connection if have data to write to the server. */
623 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100624 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100625
Damien Miller5428f641999-11-25 11:54:57 +1100626 /*
627 * Wait for something to happen. This will suspend the process until
628 * some selected descriptor can be read, written, or has some other
Damien Millere11e1ea2010-08-03 16:04:46 +1000629 * event pending, or a timeout expires.
Damien Miller5428f641999-11-25 11:54:57 +1100630 */
Damien Miller95def091999-11-25 00:26:21 +1100631
Damien Millere11e1ea2010-08-03 16:04:46 +1000632 timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000633 if (options.server_alive_interval > 0 && compat20) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000634 timeout_secs = options.server_alive_interval;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000635 server_alive_time = now + options.server_alive_interval;
636 }
637 if (options.rekey_interval > 0 && compat20 && !rekeying)
638 timeout_secs = MIN(timeout_secs, packet_get_rekey_timeout());
Damien Millere11e1ea2010-08-03 16:04:46 +1000639 set_control_persist_exit_time();
640 if (control_persist_exit_time > 0) {
641 timeout_secs = MIN(timeout_secs,
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000642 control_persist_exit_time - now);
Damien Millere11e1ea2010-08-03 16:04:46 +1000643 if (timeout_secs < 0)
644 timeout_secs = 0;
645 }
Damien Millera6508752012-04-22 11:21:10 +1000646 if (minwait_secs != 0)
647 timeout_secs = MIN(timeout_secs, (int)minwait_secs);
Damien Millere11e1ea2010-08-03 16:04:46 +1000648 if (timeout_secs == INT_MAX)
Damien Miller509b0102003-12-17 16:33:10 +1100649 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000650 else {
Damien Millere11e1ea2010-08-03 16:04:46 +1000651 tv.tv_sec = timeout_secs;
Damien Miller509b0102003-12-17 16:33:10 +1100652 tv.tv_usec = 0;
653 tvp = &tv;
654 }
Damien Millere11e1ea2010-08-03 16:04:46 +1000655
Damien Miller509b0102003-12-17 16:33:10 +1100656 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
657 if (ret < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100658 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000659
660 /*
661 * We have to clear the select masks, because we return.
662 * We have to return, because the mainloop checks for the flags
663 * set by the signal handlers.
664 */
Damien Miller79faeff2001-11-12 11:06:32 +1100665 memset(*readsetp, 0, *nallocp);
666 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000667
Damien Miller95def091999-11-25 00:26:21 +1100668 if (errno == EINTR)
669 return;
670 /* Note: we might still have data in the buffers. */
671 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
672 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100673 quit_pending = 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000674 } else if (ret == 0) {
675 /*
676 * Timeout. Could have been either keepalive or rekeying.
677 * Keepalive we check here, rekeying is checked in clientloop.
678 */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000679 if (server_alive_time != 0 && server_alive_time <= monotime())
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000680 server_alive_check();
681 }
682
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000683}
684
Ben Lindstrombba81212001-06-25 05:01:22 +0000685static void
Damien Millerad833b32000-08-23 10:46:23 +1000686client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000687{
Damien Miller95def091999-11-25 00:26:21 +1100688 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000689 if (buffer_len(bout) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000690 atomicio(vwrite, fileno(stdout), buffer_ptr(bout),
691 buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000692 if (buffer_len(berr) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000693 atomicio(vwrite, fileno(stderr), buffer_ptr(berr),
694 buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000695
Damien Miller21771e22011-05-15 08:45:50 +1000696 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000697
Damien Miller5428f641999-11-25 11:54:57 +1100698 /*
699 * Free (and clear) the buffer to reduce the amount of data that gets
700 * written to swap.
701 */
Damien Millerad833b32000-08-23 10:46:23 +1000702 buffer_free(bin);
703 buffer_free(bout);
704 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000705
Damien Miller95def091999-11-25 00:26:21 +1100706 /* Send the suspend signal to the program itself. */
707 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000708
Darren Tucker5d78de62004-11-05 20:35:44 +1100709 /* Reset window sizes in case they have changed */
710 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000711
Damien Miller95def091999-11-25 00:26:21 +1100712 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000713 buffer_init(bin);
714 buffer_init(bout);
715 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000716
Damien Miller21771e22011-05-15 08:45:50 +1000717 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000718}
719
Ben Lindstrombba81212001-06-25 05:01:22 +0000720static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100721client_process_net_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000722{
Darren Tuckerc5564e12009-06-21 18:53:53 +1000723 int len, cont = 0;
Darren Tucker86e30a02009-08-28 11:21:06 +1000724 char buf[SSH_IOBUFSZ];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000725
Damien Miller5428f641999-11-25 11:54:57 +1100726 /*
727 * Read input from the server, and add any such data to the buffer of
728 * the packet subsystem.
729 */
Damien Miller95def091999-11-25 00:26:21 +1100730 if (FD_ISSET(connection_in, readset)) {
731 /* Read as much as possible. */
Darren Tuckerc5564e12009-06-21 18:53:53 +1000732 len = roaming_read(connection_in, buf, sizeof(buf), &cont);
733 if (len == 0 && cont == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000734 /*
735 * Received EOF. The remote host has closed the
736 * connection.
737 */
738 snprintf(buf, sizeof buf,
739 "Connection to %.300s closed by remote host.\r\n",
740 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000741 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000742 quit_pending = 1;
743 return;
Damien Miller95def091999-11-25 00:26:21 +1100744 }
Damien Miller5428f641999-11-25 11:54:57 +1100745 /*
746 * There is a kernel bug on Solaris that causes select to
747 * sometimes wake up even though there is no data available.
748 */
Damien Millerd8968ad2008-07-04 23:10:49 +1000749 if (len < 0 &&
750 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Damien Miller95def091999-11-25 00:26:21 +1100751 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000752
Damien Miller95def091999-11-25 00:26:21 +1100753 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000754 /*
755 * An error has encountered. Perhaps there is a
756 * network problem.
757 */
758 snprintf(buf, sizeof buf,
759 "Read from remote host %.300s: %.100s\r\n",
760 host, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100761 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100762 quit_pending = 1;
763 return;
764 }
765 packet_process_incoming(buf, len);
766 }
Damien Miller1383bd82000-04-06 12:32:37 +1000767}
768
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000769static void
Damien Miller5771ed72008-05-19 15:35:33 +1000770client_status_confirm(int type, Channel *c, void *ctx)
Damien Miller0e220db2004-06-15 10:34:08 +1000771{
Damien Miller5771ed72008-05-19 15:35:33 +1000772 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
773 char errmsg[256];
774 int tochan;
Darren Tuckerfc959702004-07-17 16:12:08 +1000775
Damien Miller555f3b82011-05-15 08:48:05 +1000776 /*
777 * If a TTY was explicitly requested, then a failure to allocate
778 * one is fatal.
779 */
780 if (cr->action == CONFIRM_TTY &&
781 (options.request_tty == REQUEST_TTY_FORCE ||
782 options.request_tty == REQUEST_TTY_YES))
783 cr->action = CONFIRM_CLOSE;
784
Damien Miller5771ed72008-05-19 15:35:33 +1000785 /* XXX supress on mux _client_ quietmode */
786 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
Damien Millere1537f92010-01-26 13:26:22 +1100787 c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
Damien Miller0e220db2004-06-15 10:34:08 +1000788
Damien Miller5771ed72008-05-19 15:35:33 +1000789 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
790 debug2("%s request accepted on channel %d",
791 cr->request_type, c->self);
792 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
793 if (tochan) {
794 snprintf(errmsg, sizeof(errmsg),
795 "%s request failed\r\n", cr->request_type);
796 } else {
797 snprintf(errmsg, sizeof(errmsg),
798 "%s request failed on channel %d",
799 cr->request_type, c->self);
800 }
801 /* If error occurred on primary session channel, then exit */
Damien Miller555f3b82011-05-15 08:48:05 +1000802 if (cr->action == CONFIRM_CLOSE && c->self == session_ident)
Damien Miller5771ed72008-05-19 15:35:33 +1000803 fatal("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000804 /*
805 * If error occurred on mux client, append to
806 * their stderr.
807 */
808 if (tochan) {
809 buffer_append(&c->extended, errmsg,
810 strlen(errmsg));
811 } else
Damien Miller5771ed72008-05-19 15:35:33 +1000812 error("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000813 if (cr->action == CONFIRM_TTY) {
814 /*
815 * If a TTY allocation error occurred, then arrange
816 * for the correct TTY to leave raw mode.
817 */
818 if (c->self == session_ident)
819 leave_raw_mode(0);
820 else
821 mux_tty_alloc_failed(c);
822 } else if (cr->action == CONFIRM_CLOSE) {
Damien Miller5771ed72008-05-19 15:35:33 +1000823 chan_read_failed(c);
824 chan_write_failed(c);
825 }
Damien Miller0e220db2004-06-15 10:34:08 +1000826 }
Darren Tuckera627d422013-06-02 07:31:17 +1000827 free(cr);
Damien Miller5771ed72008-05-19 15:35:33 +1000828}
Damien Miller0e220db2004-06-15 10:34:08 +1000829
Damien Miller5771ed72008-05-19 15:35:33 +1000830static void
831client_abandon_status_confirm(Channel *c, void *ctx)
832{
Darren Tuckera627d422013-06-02 07:31:17 +1000833 free(ctx);
Damien Miller5771ed72008-05-19 15:35:33 +1000834}
835
Damien Miller6d7b4372011-06-23 08:31:57 +1000836void
Damien Miller555f3b82011-05-15 08:48:05 +1000837client_expect_confirm(int id, const char *request,
838 enum confirm_action action)
Damien Miller5771ed72008-05-19 15:35:33 +1000839{
Damien Miller6c81fee2013-11-08 12:19:55 +1100840 struct channel_reply_ctx *cr = xcalloc(1, sizeof(*cr));
Damien Miller5771ed72008-05-19 15:35:33 +1000841
842 cr->request_type = request;
Damien Miller555f3b82011-05-15 08:48:05 +1000843 cr->action = action;
Damien Miller5771ed72008-05-19 15:35:33 +1000844
845 channel_register_status_confirm(id, client_status_confirm,
846 client_abandon_status_confirm, cr);
Damien Miller0e220db2004-06-15 10:34:08 +1000847}
848
Darren Tucker9f407c42008-06-13 04:50:27 +1000849void
850client_register_global_confirm(global_confirm_cb *cb, void *ctx)
851{
Damien Millerb9d3bee2008-07-16 22:40:52 +1000852 struct global_confirm *gc, *last_gc;
Darren Tucker9f407c42008-06-13 04:50:27 +1000853
854 /* Coalesce identical callbacks */
Damien Millerb9d3bee2008-07-16 22:40:52 +1000855 last_gc = TAILQ_LAST(&global_confirms, global_confirms);
856 if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
857 if (++last_gc->ref_count >= INT_MAX)
858 fatal("%s: last_gc->ref_count = %d",
859 __func__, last_gc->ref_count);
Darren Tucker9f407c42008-06-13 04:50:27 +1000860 return;
861 }
862
Damien Miller6c81fee2013-11-08 12:19:55 +1100863 gc = xcalloc(1, sizeof(*gc));
Darren Tucker9f407c42008-06-13 04:50:27 +1000864 gc->cb = cb;
865 gc->ctx = ctx;
866 gc->ref_count = 1;
867 TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
868}
869
Damien Miller0e220db2004-06-15 10:34:08 +1000870static void
Ben Lindstrom681d9322002-03-22 03:53:00 +0000871process_cmdline(void)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000872{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000873 void (*handler)(int);
Damien Miller7acefbb2014-07-18 14:11:24 +1000874 char *s, *cmd;
875 int ok, delete = 0, local = 0, remote = 0, dynamic = 0;
876 struct Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000877
Damien Miller1d2c4562014-02-04 11:18:20 +1100878 memset(&fwd, 0, sizeof(fwd));
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100879
Damien Miller21771e22011-05-15 08:45:50 +1000880 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000881 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000882 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000883 if (s == NULL)
884 goto out;
Damien Millerfdb23062013-11-21 13:57:15 +1100885 while (isspace((u_char)*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000886 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000887 if (*s == '-')
888 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000889 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000890 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000891
Darren Tucker1973c882004-05-24 10:34:36 +1000892 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000893 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000894 logit(" -L[bind_address:]port:host:hostport "
895 "Request local forward");
896 logit(" -R[bind_address:]port:host:hostport "
897 "Request remote forward");
Damien Miller0164cb82008-11-05 16:30:31 +1100898 logit(" -D[bind_address:]port "
899 "Request dynamic forward");
Damien Millerff773642011-09-22 21:39:48 +1000900 logit(" -KL[bind_address:]port "
901 "Cancel local forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000902 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000903 "Cancel remote forward");
Damien Millerff773642011-09-22 21:39:48 +1000904 logit(" -KD[bind_address:]port "
905 "Cancel dynamic forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100906 if (!options.permit_local_command)
907 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000908 logit(" !args "
909 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100910 goto out;
911 }
912
913 if (*s == '!' && options.permit_local_command) {
914 s++;
915 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000916 goto out;
917 }
918
919 if (*s == 'K') {
920 delete = 1;
921 s++;
922 }
Damien Miller0164cb82008-11-05 16:30:31 +1100923 if (*s == 'L')
924 local = 1;
925 else if (*s == 'R')
926 remote = 1;
927 else if (*s == 'D')
928 dynamic = 1;
929 else {
Damien Miller996acd22003-04-09 20:59:48 +1000930 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000931 goto out;
932 }
Damien Miller0164cb82008-11-05 16:30:31 +1100933
Damien Millerff773642011-09-22 21:39:48 +1000934 if (delete && !compat20) {
Damien Miller996acd22003-04-09 20:59:48 +1000935 logit("Not supported for SSH protocol version 1.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000936 goto out;
937 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000938
Damien Millerfdb23062013-11-21 13:57:15 +1100939 while (isspace((u_char)*++s))
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100940 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000941
Damien Millere1537f92010-01-26 13:26:22 +1100942 /* XXX update list of forwards in options */
Darren Tuckere7066df2004-05-24 10:18:05 +1000943 if (delete) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000944 /* We pass 1 for dynamicfwd to restrict to 1 or 2 fields. */
945 if (!parse_forward(&fwd, s, 1, 0)) {
946 logit("Bad forwarding close specification.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000947 goto out;
948 }
Damien Millerff773642011-09-22 21:39:48 +1000949 if (remote)
Damien Miller7acefbb2014-07-18 14:11:24 +1000950 ok = channel_request_rforward_cancel(&fwd) == 0;
Damien Millerff773642011-09-22 21:39:48 +1000951 else if (dynamic)
Damien Miller7acefbb2014-07-18 14:11:24 +1000952 ok = channel_cancel_lport_listener(&fwd,
953 0, &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000954 else
Damien Miller7acefbb2014-07-18 14:11:24 +1000955 ok = channel_cancel_lport_listener(&fwd,
956 CHANNEL_CANCEL_PORT_STATIC,
957 &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000958 if (!ok) {
959 logit("Unkown port forwarding.");
960 goto out;
961 }
962 logit("Canceled forwarding.");
Darren Tuckere7066df2004-05-24 10:18:05 +1000963 } else {
Damien Miller4bf648f2009-02-14 16:28:21 +1100964 if (!parse_forward(&fwd, s, dynamic, remote)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000965 logit("Bad forwarding specification.");
966 goto out;
967 }
Damien Miller0164cb82008-11-05 16:30:31 +1100968 if (local || dynamic) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000969 if (!channel_setup_local_fwd_listener(&fwd,
970 &options.fwd_opts)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000971 logit("Port forwarding failed.");
972 goto out;
973 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100974 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +1000975 if (channel_request_remote_forwarding(&fwd) < 0) {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000976 logit("Port forwarding failed.");
977 goto out;
978 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100979 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000980 logit("Forwarding port.");
981 }
982
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000983out:
984 signal(SIGINT, handler);
Damien Miller21771e22011-05-15 08:45:50 +1000985 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tuckera627d422013-06-02 07:31:17 +1000986 free(cmd);
987 free(fwd.listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000988 free(fwd.listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000989 free(fwd.connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000990 free(fwd.connect_path);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000991}
992
Darren Tucker92a39cf2012-09-07 11:20:20 +1000993/* reasons to suppress output of an escape command in help output */
994#define SUPPRESS_NEVER 0 /* never suppress, always show */
995#define SUPPRESS_PROTO1 1 /* don't show in protocol 1 sessions */
996#define SUPPRESS_MUXCLIENT 2 /* don't show in mux client sessions */
997#define SUPPRESS_MUXMASTER 4 /* don't show in mux master sessions */
998#define SUPPRESS_SYSLOG 8 /* don't show when logging to syslog */
999struct escape_help_text {
1000 const char *cmd;
1001 const char *text;
1002 unsigned int flags;
1003};
1004static struct escape_help_text esc_txt[] = {
1005 {".", "terminate session", SUPPRESS_MUXMASTER},
1006 {".", "terminate connection (and any multiplexed sessions)",
1007 SUPPRESS_MUXCLIENT},
1008 {"B", "send a BREAK to the remote system", SUPPRESS_PROTO1},
1009 {"C", "open a command line", SUPPRESS_MUXCLIENT},
1010 {"R", "request rekey", SUPPRESS_PROTO1},
Darren Tuckerca0d0fd2012-09-07 11:22:24 +10001011 {"V/v", "decrease/increase verbosity (LogLevel)", SUPPRESS_MUXCLIENT},
Darren Tucker92a39cf2012-09-07 11:20:20 +10001012 {"^Z", "suspend ssh", SUPPRESS_MUXCLIENT},
1013 {"#", "list forwarded connections", SUPPRESS_NEVER},
1014 {"&", "background ssh (when waiting for connections to terminate)",
1015 SUPPRESS_MUXCLIENT},
1016 {"?", "this message", SUPPRESS_NEVER},
1017};
1018
1019static void
1020print_escape_help(Buffer *b, int escape_char, int protocol2, int mux_client,
1021 int using_stderr)
1022{
1023 unsigned int i, suppress_flags;
1024 char string[1024];
1025
1026 snprintf(string, sizeof string, "%c?\r\n"
1027 "Supported escape sequences:\r\n", escape_char);
1028 buffer_append(b, string, strlen(string));
1029
1030 suppress_flags = (protocol2 ? 0 : SUPPRESS_PROTO1) |
1031 (mux_client ? SUPPRESS_MUXCLIENT : 0) |
1032 (mux_client ? 0 : SUPPRESS_MUXMASTER) |
1033 (using_stderr ? 0 : SUPPRESS_SYSLOG);
1034
1035 for (i = 0; i < sizeof(esc_txt)/sizeof(esc_txt[0]); i++) {
1036 if (esc_txt[i].flags & suppress_flags)
1037 continue;
Darren Tuckerca0d0fd2012-09-07 11:22:24 +10001038 snprintf(string, sizeof string, " %c%-3s - %s\r\n",
Darren Tucker92a39cf2012-09-07 11:20:20 +10001039 escape_char, esc_txt[i].cmd, esc_txt[i].text);
1040 buffer_append(b, string, strlen(string));
1041 }
1042
1043 snprintf(string, sizeof string,
Darren Tuckerca0d0fd2012-09-07 11:22:24 +10001044 " %c%c - send the escape character by typing it twice\r\n"
Darren Tucker92a39cf2012-09-07 11:20:20 +10001045 "(Note that escapes are only recognized immediately after "
1046 "newline.)\r\n", escape_char, escape_char);
1047 buffer_append(b, string, strlen(string));
1048}
1049
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001050/*
1051 * Process the characters one by one, call with c==NULL for proto1 case.
1052 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001053static int
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001054process_escapes(Channel *c, Buffer *bin, Buffer *bout, Buffer *berr,
1055 char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001056{
1057 char string[1024];
1058 pid_t pid;
1059 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001060 u_int i;
1061 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +10001062 char *s;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001063 int *escape_pendingp, escape_char;
1064 struct escape_filter_ctx *efc;
Damien Millerad833b32000-08-23 10:46:23 +10001065
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001066 if (c == NULL) {
1067 escape_pendingp = &escape_pending1;
1068 escape_char = escape_char1;
1069 } else {
1070 if (c->filter_ctx == NULL)
1071 return 0;
1072 efc = (struct escape_filter_ctx *)c->filter_ctx;
1073 escape_pendingp = &efc->escape_pending;
1074 escape_char = efc->escape_char;
1075 }
1076
Damien Millereccb9de2005-06-17 12:59:34 +10001077 if (len <= 0)
1078 return (0);
1079
1080 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +10001081 /* Get one character at a time. */
1082 ch = buf[i];
1083
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001084 if (*escape_pendingp) {
Damien Millerad833b32000-08-23 10:46:23 +10001085 /* We have previously seen an escape character. */
1086 /* Clear the flag now. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001087 *escape_pendingp = 0;
Damien Millerad833b32000-08-23 10:46:23 +10001088
1089 /* Process the escaped character. */
1090 switch (ch) {
1091 case '.':
1092 /* Terminate the connection. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001093 snprintf(string, sizeof string, "%c.\r\n",
1094 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001095 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001096
Damien Millere1537f92010-01-26 13:26:22 +11001097 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001098 chan_read_failed(c);
1099 chan_write_failed(c);
Darren Tuckerea8342c2013-06-06 08:11:40 +10001100 if (c->detach_user)
1101 c->detach_user(c->self, NULL);
Damien Miller36187092013-06-10 13:07:11 +10001102 c->type = SSH_CHANNEL_ABANDONED;
1103 buffer_clear(&c->input);
1104 chan_ibuf_empty(c);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001105 return 0;
1106 } else
1107 quit_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001108 return -1;
1109
1110 case 'Z' - 64:
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001111 /* XXX support this for mux clients */
Damien Millere1537f92010-01-26 13:26:22 +11001112 if (c && c->ctl_chan != -1) {
Darren Tuckerf111d402012-09-07 11:21:42 +10001113 char b[16];
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001114 noescape:
Darren Tuckerf111d402012-09-07 11:21:42 +10001115 if (ch == 'Z' - 64)
1116 snprintf(b, sizeof b, "^Z");
1117 else
1118 snprintf(b, sizeof b, "%c", ch);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001119 snprintf(string, sizeof string,
Darren Tuckerf111d402012-09-07 11:21:42 +10001120 "%c%s escape not available to "
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001121 "multiplexed sessions\r\n",
Darren Tuckerf111d402012-09-07 11:21:42 +10001122 escape_char, b);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001123 buffer_append(berr, string,
1124 strlen(string));
1125 continue;
1126 }
Darren Tucker4d5cd332008-06-13 04:51:14 +10001127 /* Suspend the program. Inform the user */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001128 snprintf(string, sizeof string,
1129 "%c^Z [suspend ssh]\r\n", escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001130 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001131
1132 /* Restore terminal modes and suspend. */
1133 client_suspend_self(bin, bout, berr);
1134
1135 /* We have been continued. */
1136 continue;
1137
Damien Miller54c45982003-05-15 10:20:13 +10001138 case 'B':
1139 if (compat20) {
1140 snprintf(string, sizeof string,
1141 "%cB\r\n", escape_char);
1142 buffer_append(berr, string,
1143 strlen(string));
Damien Miller70182522013-09-14 09:49:19 +10001144 channel_request_start(c->self,
Damien Miller54c45982003-05-15 10:20:13 +10001145 "break", 0);
1146 packet_put_int(1000);
1147 packet_send();
1148 }
1149 continue;
1150
Ben Lindstromf28f6342001-04-04 02:03:04 +00001151 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001152 if (compat20) {
1153 if (datafellows & SSH_BUG_NOREKEY)
Darren Tucker4d5cd332008-06-13 04:51:14 +10001154 logit("Server does not "
1155 "support re-keying");
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001156 else
1157 need_rekeying = 1;
1158 }
Ben Lindstromf28f6342001-04-04 02:03:04 +00001159 continue;
1160
Darren Tucker50a48d02012-09-06 21:25:37 +10001161 case 'V':
1162 /* FALLTHROUGH */
1163 case 'v':
1164 if (c && c->ctl_chan != -1)
1165 goto noescape;
1166 if (!log_is_on_stderr()) {
1167 snprintf(string, sizeof string,
1168 "%c%c [Logging to syslog]\r\n",
1169 escape_char, ch);
1170 buffer_append(berr, string,
1171 strlen(string));
1172 continue;
1173 }
1174 if (ch == 'V' && options.log_level >
1175 SYSLOG_LEVEL_QUIET)
1176 log_change_level(--options.log_level);
1177 if (ch == 'v' && options.log_level <
1178 SYSLOG_LEVEL_DEBUG3)
1179 log_change_level(++options.log_level);
1180 snprintf(string, sizeof string,
1181 "%c%c [LogLevel %s]\r\n", escape_char, ch,
1182 log_level_name(options.log_level));
1183 buffer_append(berr, string, strlen(string));
1184 continue;
1185
Damien Millerad833b32000-08-23 10:46:23 +10001186 case '&':
Damien Millere1537f92010-01-26 13:26:22 +11001187 if (c && c->ctl_chan != -1)
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001188 goto noescape;
Damien Millerad833b32000-08-23 10:46:23 +10001189 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001190 * Detach the program (continue to serve
1191 * connections, but put in background and no
1192 * more new connections).
Damien Millerad833b32000-08-23 10:46:23 +10001193 */
Damien Miller96507ef2001-11-12 10:52:25 +11001194 /* Restore tty modes. */
Damien Miller21771e22011-05-15 08:45:50 +10001195 leave_raw_mode(
1196 options.request_tty == REQUEST_TTY_FORCE);
Damien Miller96507ef2001-11-12 10:52:25 +11001197
1198 /* Stop listening for new connections. */
1199 channel_stop_listening();
1200
1201 snprintf(string, sizeof string,
1202 "%c& [backgrounded]\n", escape_char);
1203 buffer_append(berr, string, strlen(string));
1204
1205 /* Fork into background. */
1206 pid = fork();
1207 if (pid < 0) {
1208 error("fork: %.100s", strerror(errno));
1209 continue;
1210 }
1211 if (pid != 0) { /* This is the parent. */
1212 /* The parent just exits. */
1213 exit(0);
1214 }
1215 /* The child continues serving connections. */
1216 if (compat20) {
1217 buffer_append(bin, "\004", 1);
1218 /* fake EOF on stdin */
1219 return -1;
1220 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +10001221 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001222 * Sending SSH_CMSG_EOF alone does not
1223 * always appear to be enough. So we
1224 * try to send an EOF character first.
Damien Millerad833b32000-08-23 10:46:23 +10001225 */
1226 packet_start(SSH_CMSG_STDIN_DATA);
1227 packet_put_string("\004", 1);
1228 packet_send();
1229 /* Close stdin. */
1230 stdin_eof = 1;
1231 if (buffer_len(bin) == 0) {
1232 packet_start(SSH_CMSG_EOF);
1233 packet_send();
1234 }
1235 }
Damien Miller96507ef2001-11-12 10:52:25 +11001236 continue;
Damien Millerad833b32000-08-23 10:46:23 +10001237
1238 case '?':
Darren Tucker92a39cf2012-09-07 11:20:20 +10001239 print_escape_help(berr, escape_char, compat20,
1240 (c && c->ctl_chan != -1),
1241 log_is_on_stderr());
Damien Millerad833b32000-08-23 10:46:23 +10001242 continue;
1243
1244 case '#':
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001245 snprintf(string, sizeof string, "%c#\r\n",
1246 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001247 buffer_append(berr, string, strlen(string));
1248 s = channel_open_message();
1249 buffer_append(berr, s, strlen(s));
Darren Tuckera627d422013-06-02 07:31:17 +10001250 free(s);
Damien Millerad833b32000-08-23 10:46:23 +10001251 continue;
1252
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001253 case 'C':
Damien Millere1537f92010-01-26 13:26:22 +11001254 if (c && c->ctl_chan != -1)
Damien Miller586b0052008-12-09 14:11:32 +11001255 goto noescape;
Ben Lindstrom681d9322002-03-22 03:53:00 +00001256 process_cmdline();
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001257 continue;
1258
Damien Millerad833b32000-08-23 10:46:23 +10001259 default:
1260 if (ch != escape_char) {
1261 buffer_put_char(bin, escape_char);
1262 bytes++;
1263 }
1264 /* Escaped characters fall through here */
1265 break;
1266 }
1267 } else {
1268 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001269 * The previous character was not an escape char.
1270 * Check if this is an escape.
Damien Millerad833b32000-08-23 10:46:23 +10001271 */
1272 if (last_was_cr && ch == escape_char) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001273 /*
1274 * It is. Set the flag and continue to
1275 * next character.
1276 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001277 *escape_pendingp = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001278 continue;
1279 }
1280 }
1281
1282 /*
1283 * Normal character. Record whether it was a newline,
1284 * and append it to the buffer.
1285 */
1286 last_was_cr = (ch == '\r' || ch == '\n');
1287 buffer_put_char(bin, ch);
1288 bytes++;
1289 }
1290 return bytes;
1291}
1292
Ben Lindstrombba81212001-06-25 05:01:22 +00001293static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001294client_process_input(fd_set *readset)
Damien Miller1383bd82000-04-06 12:32:37 +10001295{
Damien Miller166fca82000-04-20 07:42:21 +10001296 int len;
Darren Tucker86e30a02009-08-28 11:21:06 +10001297 char buf[SSH_IOBUFSZ];
Damien Miller1383bd82000-04-06 12:32:37 +10001298
Damien Miller95def091999-11-25 00:26:21 +11001299 /* Read input from stdin. */
1300 if (FD_ISSET(fileno(stdin), readset)) {
1301 /* Read as much as possible. */
1302 len = read(fileno(stdin), buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +10001303 if (len < 0 &&
1304 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001305 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +11001306 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001307 /*
1308 * Received EOF or error. They are treated
1309 * similarly, except that an error message is printed
1310 * if it was an error condition.
1311 */
Damien Miller95def091999-11-25 00:26:21 +11001312 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001313 snprintf(buf, sizeof buf, "read: %.100s\r\n",
1314 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001315 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001316 }
1317 /* Mark that we have seen EOF. */
1318 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +11001319 /*
1320 * Send an EOF message to the server unless there is
1321 * data in the buffer. If there is data in the
1322 * buffer, no message will be sent now. Code
1323 * elsewhere will send the EOF when the buffer
1324 * becomes empty if stdin_eof is set.
1325 */
Damien Miller95def091999-11-25 00:26:21 +11001326 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001327 packet_start(SSH_CMSG_EOF);
1328 packet_send();
Damien Miller95def091999-11-25 00:26:21 +11001329 }
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001330 } else if (escape_char1 == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +11001331 /*
1332 * Normal successful read, and no escape character.
1333 * Just append the data to buffer.
1334 */
Damien Miller95def091999-11-25 00:26:21 +11001335 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +11001336 } else {
Damien Miller5428f641999-11-25 11:54:57 +11001337 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001338 * Normal, successful read. But we have an escape
1339 * character and have to process the characters one
1340 * by one.
Damien Miller5428f641999-11-25 11:54:57 +11001341 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001342 if (process_escapes(NULL, &stdin_buffer,
1343 &stdout_buffer, &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +10001344 return;
Damien Miller95def091999-11-25 00:26:21 +11001345 }
1346 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001347}
1348
Ben Lindstrombba81212001-06-25 05:01:22 +00001349static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001350client_process_output(fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001351{
Damien Miller95def091999-11-25 00:26:21 +11001352 int len;
1353 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001354
Damien Miller95def091999-11-25 00:26:21 +11001355 /* Write buffered output to stdout. */
1356 if (FD_ISSET(fileno(stdout), writeset)) {
1357 /* Write as much data as possible. */
1358 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001359 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001360 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001361 if (errno == EINTR || errno == EAGAIN ||
1362 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001363 len = 0;
1364 else {
Damien Miller5428f641999-11-25 11:54:57 +11001365 /*
1366 * An error or EOF was encountered. Put an
1367 * error message to stderr buffer.
1368 */
Darren Tucker4d5cd332008-06-13 04:51:14 +10001369 snprintf(buf, sizeof buf,
1370 "write stdout: %.50s\r\n", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001371 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001372 quit_pending = 1;
1373 return;
1374 }
1375 }
1376 /* Consume printed data from the buffer. */
1377 buffer_consume(&stdout_buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001378 }
Damien Miller95def091999-11-25 00:26:21 +11001379 /* Write buffered output to stderr. */
1380 if (FD_ISSET(fileno(stderr), writeset)) {
1381 /* Write as much data as possible. */
1382 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001383 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001384 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001385 if (errno == EINTR || errno == EAGAIN ||
1386 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001387 len = 0;
1388 else {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001389 /*
1390 * EOF or error, but can't even print
1391 * error message.
1392 */
Damien Miller95def091999-11-25 00:26:21 +11001393 quit_pending = 1;
1394 return;
1395 }
1396 }
1397 /* Consume printed characters from the buffer. */
1398 buffer_consume(&stderr_buffer, len);
1399 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001400}
1401
Damien Miller5428f641999-11-25 11:54:57 +11001402/*
Damien Millerb38eff82000-04-01 11:09:21 +10001403 * Get packets from the connection input buffer, and process them as long as
1404 * there are packets available.
1405 *
1406 * Any unknown packets received during the actual
1407 * session cause the session to terminate. This is
1408 * intended to make debugging easier since no
1409 * confirmations are sent. Any compatible protocol
1410 * extensions must be negotiated during the
1411 * preparatory phase.
1412 */
1413
Ben Lindstrombba81212001-06-25 05:01:22 +00001414static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001415client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001416{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +00001417 dispatch_run(DISPATCH_NONBLOCK, &quit_pending, active_state);
Damien Millerb38eff82000-04-01 11:09:21 +10001418}
1419
Damien Millerad833b32000-08-23 10:46:23 +10001420/* scan buf[] for '~' before sending data to the peer */
1421
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001422/* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1423void *
1424client_new_escape_filter_ctx(int escape_char)
1425{
1426 struct escape_filter_ctx *ret;
1427
Damien Miller6c81fee2013-11-08 12:19:55 +11001428 ret = xcalloc(1, sizeof(*ret));
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001429 ret->escape_pending = 0;
1430 ret->escape_char = escape_char;
1431 return (void *)ret;
1432}
1433
Darren Tucker84c56f52008-06-13 04:55:46 +10001434/* Free the escape filter context on channel free */
1435void
1436client_filter_cleanup(int cid, void *ctx)
1437{
Darren Tuckera627d422013-06-02 07:31:17 +10001438 free(ctx);
Darren Tucker84c56f52008-06-13 04:55:46 +10001439}
1440
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001441int
1442client_simple_escape_filter(Channel *c, char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001443{
Damien Miller5771ed72008-05-19 15:35:33 +10001444 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1445 return 0;
1446
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001447 return process_escapes(c, &c->input, &c->output, &c->extended,
1448 buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001449}
1450
Ben Lindstrombba81212001-06-25 05:01:22 +00001451static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001452client_channel_closed(int id, void *arg)
1453{
Damien Miller3ec27592001-10-12 11:35:04 +10001454 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001455 session_closed = 1;
Damien Miller21771e22011-05-15 08:45:50 +10001456 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001457}
1458
Damien Millerb38eff82000-04-01 11:09:21 +10001459/*
Damien Miller5428f641999-11-25 11:54:57 +11001460 * Implements the interactive session with the server. This is called after
1461 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001462 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1463 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001464 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001465
Damien Miller4af51302000-04-16 11:18:38 +10001466int
Damien Millerad833b32000-08-23 10:46:23 +10001467client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001468{
Damien Miller5e953212001-01-30 09:14:00 +11001469 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001470 double start_time, total_time;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +00001471 int r, max_fd = 0, max_fd2 = 0, len, rekeying = 0;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001472 u_int64_t ibytes, obytes;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001473 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001474 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001475
Damien Miller95def091999-11-25 00:26:21 +11001476 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001477
Damien Miller95def091999-11-25 00:26:21 +11001478 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001479
Damien Miller95def091999-11-25 00:26:21 +11001480 /* Initialize variables. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001481 escape_pending1 = 0;
Damien Miller95def091999-11-25 00:26:21 +11001482 last_was_cr = 1;
1483 exit_status = -1;
1484 stdin_eof = 0;
1485 buffer_high = 64 * 1024;
1486 connection_in = packet_get_connection_in();
1487 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +11001488 max_fd = MAX(connection_in, connection_out);
1489
1490 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +00001491 /* enable nonblocking unless tty */
1492 if (!isatty(fileno(stdin)))
1493 set_nonblock(fileno(stdin));
1494 if (!isatty(fileno(stdout)))
1495 set_nonblock(fileno(stdout));
1496 if (!isatty(fileno(stderr)))
1497 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +11001498 max_fd = MAX(max_fd, fileno(stdin));
1499 max_fd = MAX(max_fd, fileno(stdout));
1500 max_fd = MAX(max_fd, fileno(stderr));
1501 }
Damien Miller95def091999-11-25 00:26:21 +11001502 quit_pending = 0;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001503 escape_char1 = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001504
Damien Miller95def091999-11-25 00:26:21 +11001505 /* Initialize buffers. */
1506 buffer_init(&stdin_buffer);
1507 buffer_init(&stdout_buffer);
1508 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001509
Damien Millerb38eff82000-04-01 11:09:21 +10001510 client_init_dispatch();
1511
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001512 /*
1513 * Set signal handlers, (e.g. to restore non-blocking mode)
1514 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1515 */
Darren Tucker07336da2004-11-05 20:02:16 +11001516 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1517 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001518 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1519 signal(SIGINT, signal_handler);
1520 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1521 signal(SIGQUIT, signal_handler);
1522 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1523 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001524 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001525
Damien Miller95def091999-11-25 00:26:21 +11001526 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001527 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001528
Ben Lindstrom5b828322001-02-09 01:34:36 +00001529 if (compat20) {
1530 session_ident = ssh2_chan_id;
Damien Miller6c3eec72011-05-05 14:16:22 +10001531 if (session_ident != -1) {
1532 if (escape_char_arg != SSH_ESCAPECHAR_NONE) {
1533 channel_register_filter(session_ident,
1534 client_simple_escape_filter, NULL,
1535 client_filter_cleanup,
1536 client_new_escape_filter_ctx(
1537 escape_char_arg));
1538 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001539 channel_register_cleanup(session_ident,
Damien Miller39eda6e2005-11-05 14:52:50 +11001540 client_channel_closed, 0);
Damien Miller6c3eec72011-05-05 14:16:22 +10001541 }
Ben Lindstrom5b828322001-02-09 01:34:36 +00001542 } else {
1543 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +10001544 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001545 }
Damien Millerad833b32000-08-23 10:46:23 +10001546
Damien Miller95def091999-11-25 00:26:21 +11001547 /* Main loop of the client for the interactive session mode. */
1548 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001549
Damien Miller5428f641999-11-25 11:54:57 +11001550 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001551 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001552
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001553 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +10001554 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001555
markus@openbsd.org091c3022015-01-19 19:52:16 +00001556 rekeying = (active_state->kex != NULL && !active_state->kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +10001557
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001558 if (rekeying) {
1559 debug("rekeying in progress");
1560 } else {
1561 /*
1562 * Make packets of buffered stdin data, and buffer
1563 * them for sending to the server.
1564 */
1565 if (!compat20)
1566 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001567
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001568 /*
1569 * Make packets from buffered channel data, and
1570 * enqueue them for sending to the server.
1571 */
1572 if (packet_not_very_much_data_to_write())
1573 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001574
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001575 /*
1576 * Check if the window size has changed, and buffer a
1577 * message about it to the server if so.
1578 */
1579 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001580
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001581 if (quit_pending)
1582 break;
1583 }
Damien Miller5428f641999-11-25 11:54:57 +11001584 /*
1585 * Wait until we have something to do (something becomes
1586 * available on one of the descriptors).
1587 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001588 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001589 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001590 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001591
Damien Miller95def091999-11-25 00:26:21 +11001592 if (quit_pending)
1593 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001594
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001595 /* Do channel operations unless rekeying in progress. */
1596 if (!rekeying) {
1597 channel_after_select(readset, writeset);
Damien Millera5539d22003-04-09 20:50:06 +10001598 if (need_rekeying || packet_need_rekeying()) {
1599 debug("need rekeying");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001600 active_state->kex->done = 0;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +00001601 if ((r = kex_send_kexinit(active_state)) != 0)
1602 fatal("%s: kex_send_kexinit: %s",
1603 __func__, ssh_err(r));
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001604 need_rekeying = 0;
1605 }
1606 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001607
Damien Miller1383bd82000-04-06 12:32:37 +10001608 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001609 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001610
Damien Miller1383bd82000-04-06 12:32:37 +10001611 if (quit_pending)
1612 break;
1613
1614 if (!compat20) {
1615 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +11001616 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +10001617 /*
1618 * Process output to stdout and stderr. Output to
1619 * the connection is processed elsewhere (above).
1620 */
Damien Miller5e953212001-01-30 09:14:00 +11001621 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +10001622 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001623
Darren Tuckere32cf432010-01-08 16:51:40 +11001624 if (session_resumed) {
1625 connection_in = packet_get_connection_in();
1626 connection_out = packet_get_connection_out();
1627 max_fd = MAX(max_fd, connection_out);
1628 max_fd = MAX(max_fd, connection_in);
1629 session_resumed = 0;
1630 }
1631
Darren Tucker4d5cd332008-06-13 04:51:14 +10001632 /*
1633 * Send as much buffered packet data as possible to the
1634 * sender.
1635 */
Damien Miller5e953212001-01-30 09:14:00 +11001636 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001637 packet_write_poll();
Damien Millere11e1ea2010-08-03 16:04:46 +10001638
1639 /*
1640 * If we are a backgrounded control master, and the
1641 * timeout has expired without any active client
1642 * connections, then quit.
1643 */
1644 if (control_persist_exit_time > 0) {
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001645 if (monotime() >= control_persist_exit_time) {
Damien Millere11e1ea2010-08-03 16:04:46 +10001646 debug("ControlPersist timeout expired");
1647 break;
1648 }
1649 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001650 }
Darren Tuckera627d422013-06-02 07:31:17 +10001651 free(readset);
1652 free(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001653
Damien Miller95def091999-11-25 00:26:21 +11001654 /* Terminate the session. */
1655
1656 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001657 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001658
Darren Tuckerde0c0252009-07-06 07:17:35 +10001659 if (compat20) {
1660 packet_start(SSH2_MSG_DISCONNECT);
1661 packet_put_int(SSH2_DISCONNECT_BY_APPLICATION);
1662 packet_put_cstring("disconnected by user");
Damien Miller8ddc71c2010-03-22 05:54:02 +11001663 packet_put_cstring(""); /* language tag */
Darren Tuckerde0c0252009-07-06 07:17:35 +10001664 packet_send();
1665 packet_write_wait();
1666 }
Darren Tucker12b4a652009-06-21 18:14:48 +10001667
Ben Lindstrom601e4362001-06-21 03:19:23 +00001668 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +11001669
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001670 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001671 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001672
1673 /* restore blocking io */
1674 if (!isatty(fileno(stdin)))
1675 unset_nonblock(fileno(stdin));
1676 if (!isatty(fileno(stdout)))
1677 unset_nonblock(fileno(stdout));
1678 if (!isatty(fileno(stderr)))
1679 unset_nonblock(fileno(stderr));
1680
Damien Millerd6965512003-12-17 16:31:53 +11001681 /*
1682 * If there was no shell or command requested, there will be no remote
1683 * exit status to be returned. In that case, clear error code if the
1684 * connection was deliberately terminated at this end.
1685 */
1686 if (no_shell_flag && received_signal == SIGTERM) {
1687 received_signal = 0;
1688 exit_status = 0;
1689 }
1690
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001691 if (received_signal)
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001692 fatal("Killed by signal %d.", (int) received_signal);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001693
1694 /*
1695 * In interactive mode (with pseudo tty) display a message indicating
1696 * that the connection has been closed.
1697 */
1698 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001699 snprintf(buf, sizeof buf,
1700 "Connection to %.64s closed.\r\n", host);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001701 buffer_append(&stderr_buffer, buf, strlen(buf));
1702 }
1703
Damien Miller95def091999-11-25 00:26:21 +11001704 /* Output any buffered data for stdout. */
Damien Millercfd6e4f2011-01-16 23:18:33 +11001705 if (buffer_len(&stdout_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001706 len = atomicio(vwrite, fileno(stdout),
1707 buffer_ptr(&stdout_buffer), buffer_len(&stdout_buffer));
Damien Millercfd6e4f2011-01-16 23:18:33 +11001708 if (len < 0 || (u_int)len != buffer_len(&stdout_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001709 error("Write failed flushing stdout buffer.");
Damien Millercfd6e4f2011-01-16 23:18:33 +11001710 else
1711 buffer_consume(&stdout_buffer, len);
Damien Miller95def091999-11-25 00:26:21 +11001712 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001713
Damien Miller95def091999-11-25 00:26:21 +11001714 /* Output any buffered data for stderr. */
Damien Millercfd6e4f2011-01-16 23:18:33 +11001715 if (buffer_len(&stderr_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001716 len = atomicio(vwrite, fileno(stderr),
1717 buffer_ptr(&stderr_buffer), buffer_len(&stderr_buffer));
Damien Millercfd6e4f2011-01-16 23:18:33 +11001718 if (len < 0 || (u_int)len != buffer_len(&stderr_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001719 error("Write failed flushing stderr buffer.");
Damien Millercfd6e4f2011-01-16 23:18:33 +11001720 else
1721 buffer_consume(&stderr_buffer, len);
Damien Miller95def091999-11-25 00:26:21 +11001722 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001723
Damien Miller95def091999-11-25 00:26:21 +11001724 /* Clear and free any buffers. */
1725 memset(buf, 0, sizeof(buf));
1726 buffer_free(&stdin_buffer);
1727 buffer_free(&stdout_buffer);
1728 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001729
Damien Miller95def091999-11-25 00:26:21 +11001730 /* Report bytes transferred, and transfer rates. */
1731 total_time = get_current_time() - start_time;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001732 packet_get_bytes(&ibytes, &obytes);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001733 verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
Damien Miller821de0a2011-01-11 17:20:29 +11001734 (unsigned long long)obytes, (unsigned long long)ibytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001735 if (total_time > 0)
Damien Millerb61f3fc2008-07-11 17:36:48 +10001736 verbose("Bytes per second: sent %.1f, received %.1f",
1737 obytes / total_time, ibytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001738 /* Return the exit status of the program. */
1739 debug("Exit status %d", exit_status);
1740 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001741}
Damien Millerb38eff82000-04-01 11:09:21 +10001742
1743/*********/
1744
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001745static int
Damien Miller630d6f42002-01-22 23:17:30 +11001746client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001747{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001748 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001749 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001750 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001751 buffer_append(&stdout_buffer, data, data_len);
Damien Millera5103f42014-02-04 11:20:14 +11001752 explicit_bzero(data, data_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001753 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001754 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001755}
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001756static int
Damien Miller630d6f42002-01-22 23:17:30 +11001757client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001758{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001759 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001760 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001761 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001762 buffer_append(&stderr_buffer, data, data_len);
Damien Millera5103f42014-02-04 11:20:14 +11001763 explicit_bzero(data, data_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001764 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001765 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001766}
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001767static int
Damien Miller630d6f42002-01-22 23:17:30 +11001768client_input_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001769{
Damien Millerb38eff82000-04-01 11:09:21 +10001770 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001771 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001772 /* Acknowledge the exit. */
1773 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1774 packet_send();
1775 /*
1776 * Must wait for packet to be sent since we are
1777 * exiting the loop.
1778 */
1779 packet_write_wait();
1780 /* Flag that we want to exit. */
1781 quit_pending = 1;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001782 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001783}
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001784static int
Darren Tucker5dcdd212003-10-02 16:17:00 +10001785client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1786{
1787 Channel *c = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001788 int r, remote_id, sock;
Darren Tucker5dcdd212003-10-02 16:17:00 +10001789
1790 /* Read the remote channel number from the message. */
1791 remote_id = packet_get_int();
1792 packet_check_eom();
1793
1794 /*
1795 * Get a connection to the local authentication agent (this may again
1796 * get forwarded).
1797 */
djm@openbsd.org141efe42015-01-14 20:05:27 +00001798 if ((r = ssh_get_authentication_socket(&sock)) != 0 &&
1799 r != SSH_ERR_AGENT_NOT_PRESENT)
1800 debug("%s: ssh_get_authentication_socket: %s",
1801 __func__, ssh_err(r));
1802
Darren Tucker5dcdd212003-10-02 16:17:00 +10001803
1804 /*
1805 * If we could not connect the agent, send an error message back to
1806 * the server. This should never happen unless the agent dies,
1807 * because authentication forwarding is only enabled if we have an
1808 * agent.
1809 */
1810 if (sock >= 0) {
1811 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1812 -1, 0, 0, 0, "authentication agent connection", 1);
1813 c->remote_id = remote_id;
1814 c->force_drain = 1;
1815 }
1816 if (c == NULL) {
1817 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1818 packet_put_int(remote_id);
1819 } else {
1820 /* Send a confirmation to the remote host. */
1821 debug("Forwarding authentication connection.");
1822 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1823 packet_put_int(remote_id);
1824 packet_put_int(c->self);
1825 }
1826 packet_send();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001827 return 0;
Darren Tucker5dcdd212003-10-02 16:17:00 +10001828}
Damien Millerb38eff82000-04-01 11:09:21 +10001829
Ben Lindstrombba81212001-06-25 05:01:22 +00001830static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001831client_request_forwarded_tcpip(const char *request_type, int rchan)
1832{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001833 Channel *c = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001834 char *listen_address, *originator_address;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001835 u_short listen_port, originator_port;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001836
1837 /* Get rest of the packet */
1838 listen_address = packet_get_string(NULL);
1839 listen_port = packet_get_int();
1840 originator_address = packet_get_string(NULL);
1841 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001842 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001843
Damien Miller7acefbb2014-07-18 14:11:24 +10001844 debug("%s: listen %s port %d, originator %s port %d", __func__,
1845 listen_address, listen_port, originator_address, originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001846
Damien Miller4b3ed642014-07-02 15:29:40 +10001847 c = channel_connect_by_listen_address(listen_address, listen_port,
Damien Millerbd740252008-05-19 15:37:09 +10001848 "forwarded-tcpip", originator_address);
1849
Darren Tuckera627d422013-06-02 07:31:17 +10001850 free(originator_address);
1851 free(listen_address);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001852 return c;
1853}
1854
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001855static Channel *
Damien Miller7acefbb2014-07-18 14:11:24 +10001856client_request_forwarded_streamlocal(const char *request_type, int rchan)
1857{
1858 Channel *c = NULL;
1859 char *listen_path;
1860
1861 /* Get the remote path. */
1862 listen_path = packet_get_string(NULL);
1863 /* XXX: Skip reserved field for now. */
1864 if (packet_get_string_ptr(NULL) == NULL)
1865 fatal("%s: packet_get_string_ptr failed", __func__);
1866 packet_check_eom();
1867
1868 debug("%s: %s", __func__, listen_path);
1869
1870 c = channel_connect_by_listen_path(listen_path,
1871 "forwarded-streamlocal@openssh.com", "forwarded-streamlocal");
1872 free(listen_path);
1873 return c;
1874}
1875
1876static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001877client_request_x11(const char *request_type, int rchan)
1878{
1879 Channel *c = NULL;
1880 char *originator;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001881 u_short originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001882 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001883
1884 if (!options.forward_x11) {
1885 error("Warning: ssh server tried X11 forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001886 error("Warning: this is probably a break-in attempt by a "
1887 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001888 return NULL;
1889 }
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001890 if (x11_refuse_time != 0 && monotime() >= x11_refuse_time) {
Damien Miller1ab6a512010-06-26 10:02:24 +10001891 verbose("Rejected X11 connection after ForwardX11Timeout "
1892 "expired");
1893 return NULL;
1894 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001895 originator = packet_get_string(NULL);
1896 if (datafellows & SSH_BUG_X11FWD) {
1897 debug2("buggy server: x11 request w/o originator_port");
1898 originator_port = 0;
1899 } else {
1900 originator_port = packet_get_int();
1901 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001902 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001903 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001904 debug("client_request_x11: request from %s %d", originator,
1905 originator_port);
Darren Tuckera627d422013-06-02 07:31:17 +10001906 free(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001907 sock = x11_connect_display();
1908 if (sock < 0)
1909 return NULL;
1910 c = channel_new("x11",
1911 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001912 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001913 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001914 return c;
1915}
1916
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001917static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001918client_request_agent(const char *request_type, int rchan)
1919{
1920 Channel *c = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001921 int r, sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001922
1923 if (!options.forward_agent) {
1924 error("Warning: ssh server tried agent forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001925 error("Warning: this is probably a break-in attempt by a "
1926 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001927 return NULL;
1928 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001929 if ((r = ssh_get_authentication_socket(&sock)) != 0) {
1930 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1931 debug("%s: ssh_get_authentication_socket: %s",
1932 __func__, ssh_err(r));
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001933 return NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001934 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001935 c = channel_new("authentication agent connection",
1936 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001937 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001938 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001939 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001940 return c;
1941}
1942
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001943int
1944client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun)
1945{
1946 Channel *c;
1947 int fd;
1948
1949 if (tun_mode == SSH_TUNMODE_NO)
1950 return 0;
1951
1952 if (!compat20) {
Damien Millerb3f2c9f2009-01-28 16:15:30 +11001953 error("Tunnel forwarding is not supported for protocol 1");
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001954 return -1;
1955 }
1956
1957 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1958
1959 /* Open local tunnel device */
1960 if ((fd = tun_open(local_tun, tun_mode)) == -1) {
1961 error("Tunnel device open failed.");
1962 return -1;
1963 }
1964
1965 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1966 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1967 c->datagram = 1;
1968
1969#if defined(SSH_TUN_FILTER)
1970 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1971 channel_register_filter(c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +10001972 sys_tun_outfilter, NULL, NULL);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001973#endif
1974
1975 packet_start(SSH2_MSG_CHANNEL_OPEN);
1976 packet_put_cstring("tun@openssh.com");
1977 packet_put_int(c->self);
1978 packet_put_int(c->local_window_max);
1979 packet_put_int(c->local_maxpacket);
1980 packet_put_int(tun_mode);
1981 packet_put_int(remote_tun);
1982 packet_send();
1983
1984 return 0;
1985}
1986
Damien Millerbd483e72000-04-30 10:00:53 +10001987/* XXXX move to generic input handler */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001988static int
Damien Miller630d6f42002-01-22 23:17:30 +11001989client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001990{
1991 Channel *c = NULL;
1992 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001993 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001994 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001995
1996 ctype = packet_get_string(&len);
1997 rchan = packet_get_int();
1998 rwindow = packet_get_int();
1999 rmaxpack = packet_get_int();
2000
Damien Millere247cc42000-05-07 12:03:14 +10002001 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10002002 ctype, rchan, rwindow, rmaxpack);
2003
Damien Miller0bc1bd82000-11-13 22:57:25 +11002004 if (strcmp(ctype, "forwarded-tcpip") == 0) {
2005 c = client_request_forwarded_tcpip(ctype, rchan);
Damien Miller7acefbb2014-07-18 14:11:24 +10002006 } else if (strcmp(ctype, "forwarded-streamlocal@openssh.com") == 0) {
2007 c = client_request_forwarded_streamlocal(ctype, rchan);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002008 } else if (strcmp(ctype, "x11") == 0) {
2009 c = client_request_x11(ctype, rchan);
2010 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
2011 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10002012 }
2013/* XXX duplicate : */
2014 if (c != NULL) {
2015 debug("confirm %s", ctype);
2016 c->remote_id = rchan;
2017 c->remote_window = rwindow;
2018 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00002019 if (c->type != SSH_CHANNEL_CONNECTING) {
2020 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
2021 packet_put_int(c->remote_id);
2022 packet_put_int(c->self);
2023 packet_put_int(c->local_window);
2024 packet_put_int(c->local_maxpacket);
2025 packet_send();
2026 }
Damien Millerbd483e72000-04-30 10:00:53 +10002027 } else {
2028 debug("failure %s", ctype);
2029 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
2030 packet_put_int(rchan);
2031 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00002032 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00002033 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00002034 packet_put_cstring("");
2035 }
Damien Millerbd483e72000-04-30 10:00:53 +10002036 packet_send();
2037 }
Darren Tuckera627d422013-06-02 07:31:17 +10002038 free(ctype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002039 return 0;
Damien Millerbd483e72000-04-30 10:00:53 +10002040}
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002041static int
Damien Miller630d6f42002-01-22 23:17:30 +11002042client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00002043{
2044 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002045 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00002046 char *rtype;
2047
2048 id = packet_get_int();
2049 rtype = packet_get_string(NULL);
2050 reply = packet_get_char();
2051
2052 debug("client_input_channel_req: channel %d rtype %s reply %d",
2053 id, rtype, reply);
2054
Damien Miller3bbd8782004-06-18 22:23:22 +10002055 if (id == -1) {
2056 error("client_input_channel_req: request for channel -1");
2057 } else if ((c = channel_lookup(id)) == NULL) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10002058 error("client_input_channel_req: channel %d: "
2059 "unknown channel", id);
Damien Millerbab9bd42008-05-19 16:06:47 +10002060 } else if (strcmp(rtype, "eow@openssh.com") == 0) {
2061 packet_check_eom();
2062 chan_rcvd_eow(c);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002063 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10002064 exitval = packet_get_int();
Damien Millere1537f92010-01-26 13:26:22 +11002065 if (c->ctl_chan != -1) {
2066 mux_exit_message(c, exitval);
Damien Miller0e220db2004-06-15 10:34:08 +10002067 success = 1;
Darren Tucker29440822010-01-08 17:08:35 +11002068 } else if (id == session_ident) {
2069 /* Record exit value of local session */
2070 success = 1;
2071 exit_status = exitval;
2072 } else {
Damien Miller36f57eb2010-01-30 17:28:34 +11002073 /* Probably for a mux channel that has already closed */
2074 debug("%s: no sink for exit-status on channel %d",
2075 __func__, id);
Damien Miller0e220db2004-06-15 10:34:08 +10002076 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002077 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00002078 }
Damien Millerc5893782014-05-15 13:48:49 +10002079 if (reply && c != NULL && !(c->flags & CHAN_CLOSE_SENT)) {
Ben Lindstrom5b828322001-02-09 01:34:36 +00002080 packet_start(success ?
2081 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller8533c782008-12-08 09:54:40 +11002082 packet_put_int(c->remote_id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002083 packet_send();
2084 }
Darren Tuckera627d422013-06-02 07:31:17 +10002085 free(rtype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002086 return 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00002087}
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002088static int
Damien Millerc3fa4072002-01-22 23:21:58 +11002089client_input_global_request(int type, u_int32_t seq, void *ctxt)
2090{
2091 char *rtype;
2092 int want_reply;
2093 int success = 0;
2094
2095 rtype = packet_get_string(NULL);
2096 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11002097 debug("client_input_global_request: rtype %s want_reply %d",
2098 rtype, want_reply);
Damien Millerc3fa4072002-01-22 23:21:58 +11002099 if (want_reply) {
2100 packet_start(success ?
2101 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
2102 packet_send();
2103 packet_write_wait();
2104 }
Darren Tuckera627d422013-06-02 07:31:17 +10002105 free(rtype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002106 return 0;
Damien Millerc3fa4072002-01-22 23:21:58 +11002107}
Damien Millerbd483e72000-04-30 10:00:53 +10002108
Damien Miller0e220db2004-06-15 10:34:08 +10002109void
Darren Tuckerfc959702004-07-17 16:12:08 +10002110client_session2_setup(int id, int want_tty, int want_subsystem,
Damien Miller5771ed72008-05-19 15:35:33 +10002111 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env)
Damien Miller0e220db2004-06-15 10:34:08 +10002112{
2113 int len;
Darren Tucker5d78de62004-11-05 20:35:44 +11002114 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002115
2116 debug2("%s: id %d", __func__, id);
2117
Darren Tucker5d78de62004-11-05 20:35:44 +11002118 if ((c = channel_lookup(id)) == NULL)
2119 fatal("client_session2_setup: channel %d: unknown channel", id);
2120
Damien Miller0dac6fb2010-11-20 15:19:38 +11002121 packet_set_interactive(want_tty,
2122 options.ip_qos_interactive, options.ip_qos_bulk);
2123
Damien Miller0e220db2004-06-15 10:34:08 +10002124 if (want_tty) {
2125 struct winsize ws;
Damien Miller0e220db2004-06-15 10:34:08 +10002126
2127 /* Store window size in the packet. */
2128 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
2129 memset(&ws, 0, sizeof(ws));
2130
Damien Miller5771ed72008-05-19 15:35:33 +10002131 channel_request_start(id, "pty-req", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002132 client_expect_confirm(id, "PTY allocation", CONFIRM_TTY);
Damien Miller0e220db2004-06-15 10:34:08 +10002133 packet_put_cstring(term != NULL ? term : "");
Damien Miller71a73672006-03-26 14:04:36 +11002134 packet_put_int((u_int)ws.ws_col);
2135 packet_put_int((u_int)ws.ws_row);
2136 packet_put_int((u_int)ws.ws_xpixel);
2137 packet_put_int((u_int)ws.ws_ypixel);
Darren Tuckerdf189fb2008-06-08 12:55:32 +10002138 if (tiop == NULL)
2139 tiop = get_saved_tio();
2140 tty_make_modes(-1, tiop);
Damien Miller0e220db2004-06-15 10:34:08 +10002141 packet_send();
2142 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11002143 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10002144 }
2145
2146 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10002147 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10002148 int i, j, matched;
Damien Miller0e220db2004-06-15 10:34:08 +10002149 char *name, *val;
2150
2151 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10002152 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10002153 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10002154 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10002155 if ((val = strchr(name, '=')) == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +10002156 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002157 continue;
2158 }
2159 *val++ = '\0';
2160
2161 matched = 0;
2162 for (j = 0; j < options.num_send_env; j++) {
2163 if (match_pattern(name, options.send_env[j])) {
2164 matched = 1;
2165 break;
2166 }
2167 }
2168 if (!matched) {
2169 debug3("Ignored env %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10002170 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002171 continue;
2172 }
2173
2174 debug("Sending env %s = %s", name, val);
2175 channel_request_start(id, "env", 0);
2176 packet_put_cstring(name);
2177 packet_put_cstring(val);
2178 packet_send();
Darren Tuckera627d422013-06-02 07:31:17 +10002179 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002180 }
2181 }
2182
2183 len = buffer_len(cmd);
2184 if (len > 0) {
2185 if (len > 900)
2186 len = 900;
2187 if (want_subsystem) {
Damien Miller5771ed72008-05-19 15:35:33 +10002188 debug("Sending subsystem: %.*s",
2189 len, (u_char*)buffer_ptr(cmd));
2190 channel_request_start(id, "subsystem", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002191 client_expect_confirm(id, "subsystem", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002192 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002193 debug("Sending command: %.*s",
2194 len, (u_char*)buffer_ptr(cmd));
2195 channel_request_start(id, "exec", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002196 client_expect_confirm(id, "exec", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002197 }
2198 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
2199 packet_send();
2200 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002201 channel_request_start(id, "shell", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002202 client_expect_confirm(id, "shell", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002203 packet_send();
2204 }
2205}
2206
Ben Lindstrombba81212001-06-25 05:01:22 +00002207static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002208client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10002209{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002210 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10002211
Damien Miller1383bd82000-04-06 12:32:37 +10002212 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2213 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2214 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2215 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10002216 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10002217 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2218 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002219 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10002220 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerb84886b2008-05-19 15:05:07 +10002221 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2222 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
Damien Millerc3fa4072002-01-22 23:21:58 +11002223 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002224
2225 /* rekeying */
2226 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10002227
2228 /* global request reply messages */
2229 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2230 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10002231}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002232
Ben Lindstrombba81212001-06-25 05:01:22 +00002233static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002234client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002235{
2236 dispatch_init(NULL);
2237 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
2238 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
2239 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10002240 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2241 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
2242 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002243 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
2244 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
2245 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11002246
2247 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
Darren Tucker5dcdd212003-10-02 16:17:00 +10002248 &client_input_agent_open : &deny_input_open);
Damien Miller69b69aa2000-10-28 14:19:58 +11002249 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
2250 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002251}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002252
Ben Lindstrombba81212001-06-25 05:01:22 +00002253static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002254client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002255{
2256 client_init_dispatch_13();
2257 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
2258 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
2259}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002260
Ben Lindstromdb47f382001-07-04 05:10:27 +00002261static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002262client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002263{
Damien Miller1383bd82000-04-06 12:32:37 +10002264 if (compat20)
2265 client_init_dispatch_20();
2266 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10002267 client_init_dispatch_13();
2268 else
2269 client_init_dispatch_15();
2270}
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002271
Damien Miller6c3eec72011-05-05 14:16:22 +10002272void
2273client_stop_mux(void)
2274{
2275 if (options.control_path != NULL && muxserver_sock != -1)
2276 unlink(options.control_path);
2277 /*
Darren Tucker48bf4b02012-09-07 16:38:53 +10002278 * If we are in persist mode, or don't have a shell, signal that we
2279 * should close when all active channels are closed.
Damien Miller6c3eec72011-05-05 14:16:22 +10002280 */
Darren Tucker48bf4b02012-09-07 16:38:53 +10002281 if (options.control_persist || no_shell_flag) {
Damien Miller6c3eec72011-05-05 14:16:22 +10002282 session_closed = 1;
Damien Miller4ac99c32011-06-20 14:43:31 +10002283 setproctitle("[stopped mux]");
2284 }
Damien Miller6c3eec72011-05-05 14:16:22 +10002285}
2286
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002287/* client specific fatal cleanup */
2288void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002289cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002290{
Damien Miller21771e22011-05-15 08:45:50 +10002291 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002292 leave_non_blocking();
Damien Millerb1cbfa22008-05-19 16:00:08 +10002293 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10002294 unlink(options.control_path);
Damien Millera41ccca2010-10-07 22:07:32 +11002295 ssh_kill_proxy_command();
Darren Tucker3e33cec2003-10-02 16:12:36 +10002296 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002297}