blob: 2137a81ce617fca1e3e8acc29824f8a2fdbd26dd [file] [log] [blame]
djm@openbsd.org141efe42015-01-14 20:05:27 +00001/* $OpenBSD: clientloop.c,v 1.262 2015/01/14 20:05:27 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * The main loop for the interactive session (client side).
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
14 *
15 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 *
Damien Miller1383bd82000-04-06 12:32:37 +100038 * SSH2 support added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000039 * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110040 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110060 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
62#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110063
Damien Millerf17883e2006-03-15 11:45:54 +110064#include <sys/types.h>
Damien Miller9aec9192006-08-05 10:57:45 +100065#include <sys/ioctl.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100066#include <sys/param.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>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
Damien Millerb84886b2008-05-19 15:05:07 +100089#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100090#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000092#include "ssh1.h"
93#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094#include "packet.h"
95#include "buffer.h"
Damien Millerb38eff82000-04-01 11:09:21 +100096#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000097#include "channels.h"
Damien Millerb38eff82000-04-01 11:09:21 +100098#include "dispatch.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110099#include "key.h"
Damien Millerd7834352006-08-05 12:39:39 +1000100#include "cipher.h"
Ben Lindstromf28f6342001-04-04 02:03:04 +0000101#include "kex.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000102#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +1000103#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000104#include "readconf.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000105#include "clientloop.h"
Damien Milleraeb31d62005-12-13 19:29:36 +1100106#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000107#include "authfd.h"
108#include "atomicio.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +1000109#include "sshpty.h"
Damien Miller0e220db2004-06-15 10:34:08 +1000110#include "match.h"
111#include "msg.h"
Darren Tuckerc5564e12009-06-21 18:53:53 +1000112#include "roaming.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +0000113#include "ssherr.h"
Damien Miller69b69aa2000-10-28 14:19:58 +1100114
115/* import options */
116extern Options options;
117
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118/* Flag indicating that stdin should be redirected from /dev/null. */
119extern int stdin_null_flag;
120
Damien Millerd6965512003-12-17 16:31:53 +1100121/* Flag indicating that no shell has been requested */
122extern int no_shell_flag;
123
Damien Miller0e220db2004-06-15 10:34:08 +1000124/* Control socket */
Damien Millere1537f92010-01-26 13:26:22 +1100125extern int muxserver_sock; /* XXX use mux_client_cleanup() instead */
Damien Miller0e220db2004-06-15 10:34:08 +1000126
Damien Miller5428f641999-11-25 11:54:57 +1100127/*
128 * Name of the host we are connecting to. This is the name given on the
129 * command line, or the HostName specified for the user-supplied name in a
130 * configuration file.
131 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132extern char *host;
133
Damien Miller5428f641999-11-25 11:54:57 +1100134/*
135 * Flag to indicate that we have received a window change signal which has
136 * not yet been processed. This will cause a message indicating the new
137 * window size to be sent to the server a little later. This is volatile
138 * because this is updated in a signal handler.
139 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000140static volatile sig_atomic_t received_window_change_signal = 0;
141static volatile sig_atomic_t received_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142
Damien Miller788f2122005-11-05 15:14:59 +1100143/* Flag indicating whether the user's terminal is in non-blocking mode. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144static int in_non_blocking_mode = 0;
145
Damien Millere11e1ea2010-08-03 16:04:46 +1000146/* Time when backgrounded control master using ControlPersist should exit */
147static time_t control_persist_exit_time = 0;
148
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149/* Common data for the client loop code. */
Damien Millere1537f92010-01-26 13:26:22 +1100150volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000151static int escape_char1; /* Escape character. (proto1 only) */
152static int escape_pending1; /* Last character was an escape (proto1 only) */
Damien Miller95def091999-11-25 00:26:21 +1100153static int last_was_cr; /* Last character was a newline. */
Darren Tucker4d5cd332008-06-13 04:51:14 +1000154static int exit_status; /* Used to store the command exit status. */
155static int stdin_eof; /* EOF has been encountered on stderr. */
Damien Miller95def091999-11-25 00:26:21 +1100156static Buffer stdin_buffer; /* Buffer for stdin data. */
157static Buffer stdout_buffer; /* Buffer for stdout data. */
158static Buffer stderr_buffer; /* Buffer for stderr data. */
Damien Miller1ab6a512010-06-26 10:02:24 +1000159static u_int buffer_high; /* Soft max buffer size. */
Damien Miller95def091999-11-25 00:26:21 +1100160static int connection_in; /* Connection to server (input). */
161static int connection_out; /* Connection to server (output). */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000162static int need_rekeying; /* Set to non-zero if rekeying is requested. */
Damien Miller1ab6a512010-06-26 10:02:24 +1000163static int session_closed; /* In SSH2: login session closed. */
164static int x11_refuse_time; /* If >0, refuse x11 opens after this time. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165
Ben Lindstrombba81212001-06-25 05:01:22 +0000166static void client_init_dispatch(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000167int session_ident = -1;
168
Darren Tuckere32cf432010-01-08 16:51:40 +1100169int session_resumed = 0;
170
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000171/* Track escape per proto2 channel */
172struct escape_filter_ctx {
173 int escape_pending;
174 int escape_char;
175};
176
177/* Context for channel confirmation replies */
Damien Miller5771ed72008-05-19 15:35:33 +1000178struct channel_reply_ctx {
179 const char *request_type;
Damien Miller555f3b82011-05-15 08:48:05 +1000180 int id;
181 enum confirm_action action;
Damien Miller5771ed72008-05-19 15:35:33 +1000182};
183
Darren Tucker9f407c42008-06-13 04:50:27 +1000184/* Global request success/failure callbacks */
185struct global_confirm {
186 TAILQ_ENTRY(global_confirm) entry;
187 global_confirm_cb *cb;
188 void *ctx;
189 int ref_count;
190};
191TAILQ_HEAD(global_confirms, global_confirm);
192static struct global_confirms global_confirms =
193 TAILQ_HEAD_INITIALIZER(global_confirms);
194
Ben Lindstromf28f6342001-04-04 02:03:04 +0000195/*XXX*/
196extern Kex *xxx_kex;
197
Damien Miller0e220db2004-06-15 10:34:08 +1000198void ssh_process_session2_setup(int, int, int, Buffer *);
199
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200/* Restores stdin to blocking mode. */
201
Ben Lindstrombba81212001-06-25 05:01:22 +0000202static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000203leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204{
Damien Miller95def091999-11-25 00:26:21 +1100205 if (in_non_blocking_mode) {
Damien Miller03e66f62004-06-15 15:47:51 +1000206 unset_nonblock(fileno(stdin));
Damien Miller95def091999-11-25 00:26:21 +1100207 in_non_blocking_mode = 0;
Damien Miller95def091999-11-25 00:26:21 +1100208 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209}
210
Damien Miller95def091999-11-25 00:26:21 +1100211/* Puts stdin terminal in non-blocking mode. */
212
Ben Lindstrombba81212001-06-25 05:01:22 +0000213static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000214enter_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215{
Damien Miller95def091999-11-25 00:26:21 +1100216 in_non_blocking_mode = 1;
Damien Miller232711f2004-06-15 10:35:30 +1000217 set_nonblock(fileno(stdin));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000218}
219
Damien Miller5428f641999-11-25 11:54:57 +1100220/*
221 * Signal handler for the window change signal (SIGWINCH). This just sets a
222 * flag indicating that the window has changed.
223 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100224/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000225static void
Damien Miller95def091999-11-25 00:26:21 +1100226window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000227{
Damien Miller95def091999-11-25 00:26:21 +1100228 received_window_change_signal = 1;
229 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230}
231
Damien Miller5428f641999-11-25 11:54:57 +1100232/*
233 * Signal handler for signals that cause the program to terminate. These
234 * signals must be trapped to restore terminal modes.
235 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100236/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000237static void
Damien Miller95def091999-11-25 00:26:21 +1100238signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000240 received_signal = sig;
241 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242}
243
Damien Miller5428f641999-11-25 11:54:57 +1100244/*
245 * Returns current time in seconds from Jan 1, 1970 with the maximum
246 * available resolution.
247 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248
Ben Lindstrombba81212001-06-25 05:01:22 +0000249static double
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000250get_current_time(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251{
Damien Miller95def091999-11-25 00:26:21 +1100252 struct timeval tv;
253 gettimeofday(&tv, NULL);
254 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000255}
256
Damien Millere11e1ea2010-08-03 16:04:46 +1000257/*
258 * Sets control_persist_exit_time to the absolute time when the
259 * backgrounded control master should exit due to expiry of the
260 * ControlPersist timeout. Sets it to 0 if we are not a backgrounded
261 * control master process, or if there is no ControlPersist timeout.
262 */
263static void
264set_control_persist_exit_time(void)
265{
266 if (muxserver_sock == -1 || !options.control_persist
Damien Miller6c3eec72011-05-05 14:16:22 +1000267 || options.control_persist_timeout == 0) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000268 /* not using a ControlPersist timeout */
269 control_persist_exit_time = 0;
Damien Miller6c3eec72011-05-05 14:16:22 +1000270 } else if (channel_still_open()) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000271 /* some client connections are still open */
272 if (control_persist_exit_time > 0)
273 debug2("%s: cancel scheduled exit", __func__);
274 control_persist_exit_time = 0;
275 } else if (control_persist_exit_time <= 0) {
276 /* a client connection has recently closed */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000277 control_persist_exit_time = monotime() +
Damien Millere11e1ea2010-08-03 16:04:46 +1000278 (time_t)options.control_persist_timeout;
279 debug2("%s: schedule exit in %d seconds", __func__,
280 options.control_persist_timeout);
281 }
282 /* else we are already counting down to the timeout */
283}
284
Damien Miller8d60be52012-02-11 08:18:17 +1100285#define SSH_X11_VALID_DISPLAY_CHARS ":/.-_"
286static int
287client_x11_display_valid(const char *display)
288{
289 size_t i, dlen;
290
291 dlen = strlen(display);
292 for (i = 0; i < dlen; i++) {
Damien Millerfdb23062013-11-21 13:57:15 +1100293 if (!isalnum((u_char)display[i]) &&
Damien Miller8d60be52012-02-11 08:18:17 +1100294 strchr(SSH_X11_VALID_DISPLAY_CHARS, display[i]) == NULL) {
295 debug("Invalid character '%c' in DISPLAY", display[i]);
296 return 0;
297 }
298 }
299 return 1;
300}
301
Damien Miller17e7ed02005-06-17 12:54:33 +1000302#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
303void
304client_x11_get_proto(const char *display, const char *xauth_path,
Damien Miller1ab6a512010-06-26 10:02:24 +1000305 u_int trusted, u_int timeout, char **_proto, char **_data)
Damien Miller17e7ed02005-06-17 12:54:33 +1000306{
307 char cmd[1024];
308 char line[512];
309 char xdisplay[512];
310 static char proto[512], data[512];
311 FILE *f;
312 int got_data = 0, generated = 0, do_unlink = 0, i;
313 char *xauthdir, *xauthfile;
314 struct stat st;
Damien Miller1ab6a512010-06-26 10:02:24 +1000315 u_int now;
Damien Miller17e7ed02005-06-17 12:54:33 +1000316
317 xauthdir = xauthfile = NULL;
318 *_proto = proto;
319 *_data = data;
320 proto[0] = data[0] = '\0';
321
322 if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) {
323 debug("No xauth program.");
Damien Miller8d60be52012-02-11 08:18:17 +1100324 } else if (!client_x11_display_valid(display)) {
325 logit("DISPLAY '%s' invalid, falling back to fake xauth data",
326 display);
Damien Miller17e7ed02005-06-17 12:54:33 +1000327 } else {
328 if (display == NULL) {
329 debug("x11_get_proto: DISPLAY not set");
330 return;
331 }
332 /*
333 * Handle FamilyLocal case where $DISPLAY does
334 * not match an authorization entry. For this we
335 * just try "xauth list unix:displaynum.screennum".
336 * XXX: "localhost" match to determine FamilyLocal
337 * is not perfect.
338 */
339 if (strncmp(display, "localhost:", 10) == 0) {
340 snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
341 display + 10);
342 display = xdisplay;
343 }
344 if (trusted == 0) {
345 xauthdir = xmalloc(MAXPATHLEN);
346 xauthfile = xmalloc(MAXPATHLEN);
Damien Miller2cd62932010-12-01 11:50:35 +1100347 mktemp_proto(xauthdir, MAXPATHLEN);
Damien Miller17e7ed02005-06-17 12:54:33 +1000348 if (mkdtemp(xauthdir) != NULL) {
349 do_unlink = 1;
350 snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile",
351 xauthdir);
352 snprintf(cmd, sizeof(cmd),
353 "%s -f %s generate %s " SSH_X11_PROTO
Damien Miller1ab6a512010-06-26 10:02:24 +1000354 " untrusted timeout %u 2>" _PATH_DEVNULL,
355 xauth_path, xauthfile, display, timeout);
Damien Miller17e7ed02005-06-17 12:54:33 +1000356 debug2("x11_get_proto: %s", cmd);
357 if (system(cmd) == 0)
358 generated = 1;
Damien Miller1ab6a512010-06-26 10:02:24 +1000359 if (x11_refuse_time == 0) {
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000360 now = monotime() + 1;
Damien Miller1ab6a512010-06-26 10:02:24 +1000361 if (UINT_MAX - timeout < now)
362 x11_refuse_time = UINT_MAX;
363 else
364 x11_refuse_time = now + timeout;
365 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000366 }
367 }
Darren Tucker513d13a2007-08-15 19:13:41 +1000368
369 /*
370 * When in untrusted mode, we read the cookie only if it was
371 * successfully generated as an untrusted one in the step
372 * above.
373 */
374 if (trusted || generated) {
375 snprintf(cmd, sizeof(cmd),
376 "%s %s%s list %s 2>" _PATH_DEVNULL,
377 xauth_path,
378 generated ? "-f " : "" ,
379 generated ? xauthfile : "",
380 display);
381 debug2("x11_get_proto: %s", cmd);
382 f = popen(cmd, "r");
383 if (f && fgets(line, sizeof(line), f) &&
384 sscanf(line, "%*s %511s %511s", proto, data) == 2)
385 got_data = 1;
386 if (f)
387 pclose(f);
388 } else
389 error("Warning: untrusted X11 forwarding setup failed: "
390 "xauth key data not generated");
Damien Miller17e7ed02005-06-17 12:54:33 +1000391 }
392
393 if (do_unlink) {
394 unlink(xauthfile);
395 rmdir(xauthdir);
396 }
Darren Tuckera627d422013-06-02 07:31:17 +1000397 free(xauthdir);
398 free(xauthfile);
Damien Miller17e7ed02005-06-17 12:54:33 +1000399
400 /*
401 * If we didn't get authentication data, just make up some
402 * data. The forwarding code will check the validity of the
403 * response anyway, and substitute this data. The X11
404 * server, however, will ignore this fake data and use
405 * whatever authentication mechanisms it was using otherwise
406 * for the local connection.
407 */
408 if (!got_data) {
409 u_int32_t rnd = 0;
410
411 logit("Warning: No xauth data; "
412 "using fake authentication data for X11 forwarding.");
413 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
414 for (i = 0; i < 16; i++) {
415 if (i % 4 == 0)
416 rnd = arc4random();
417 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
418 rnd & 0xff);
419 rnd >>= 8;
420 }
421 }
422}
423
Damien Miller5428f641999-11-25 11:54:57 +1100424/*
425 * This is called when the interactive is entered. This checks if there is
426 * an EOF coming on stdin. We must check this explicitly, as select() does
427 * not appear to wake up when redirecting from /dev/null.
428 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000429
Ben Lindstrombba81212001-06-25 05:01:22 +0000430static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000431client_check_initial_eof_on_stdin(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432{
Damien Miller95def091999-11-25 00:26:21 +1100433 int len;
434 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000435
Damien Miller5428f641999-11-25 11:54:57 +1100436 /*
437 * If standard input is to be "redirected from /dev/null", we simply
438 * mark that we have seen an EOF and send an EOF message to the
439 * server. Otherwise, we try to read a single character; it appears
440 * that for some files, such /dev/null, select() never wakes up for
441 * read for this descriptor, which means that we never get EOF. This
442 * way we will get the EOF if stdin comes from /dev/null or similar.
443 */
Damien Miller95def091999-11-25 00:26:21 +1100444 if (stdin_null_flag) {
445 /* Fake EOF on stdin. */
446 debug("Sending eof.");
447 stdin_eof = 1;
448 packet_start(SSH_CMSG_EOF);
449 packet_send();
450 } else {
Damien Miller95def091999-11-25 00:26:21 +1100451 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000452
Damien Miller95def091999-11-25 00:26:21 +1100453 /* Check for immediate EOF on stdin. */
454 len = read(fileno(stdin), buf, 1);
455 if (len == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000456 /*
457 * EOF. Record that we have seen it and send
458 * EOF to server.
459 */
Damien Miller95def091999-11-25 00:26:21 +1100460 debug("Sending eof.");
461 stdin_eof = 1;
462 packet_start(SSH_CMSG_EOF);
463 packet_send();
464 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100465 /*
466 * Got data. We must store the data in the buffer,
467 * and also process it as an escape character if
468 * appropriate.
469 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000470 if ((u_char) buf[0] == escape_char1)
471 escape_pending1 = 1;
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000472 else
Damien Miller95def091999-11-25 00:26:21 +1100473 buffer_append(&stdin_buffer, buf, 1);
Damien Miller95def091999-11-25 00:26:21 +1100474 }
Damien Miller95def091999-11-25 00:26:21 +1100475 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000476 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477}
478
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000479
Damien Miller5428f641999-11-25 11:54:57 +1100480/*
481 * Make packets from buffered stdin data, and buffer them for sending to the
482 * connection.
483 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000484
Ben Lindstrombba81212001-06-25 05:01:22 +0000485static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000486client_make_packets_from_stdin_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000487{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000488 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489
Damien Miller95def091999-11-25 00:26:21 +1100490 /* Send buffered stdin data to the server. */
491 while (buffer_len(&stdin_buffer) > 0 &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100492 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100493 len = buffer_len(&stdin_buffer);
494 /* Keep the packets at reasonable size. */
495 if (len > packet_get_maxsize())
496 len = packet_get_maxsize();
497 packet_start(SSH_CMSG_STDIN_DATA);
498 packet_put_string(buffer_ptr(&stdin_buffer), len);
499 packet_send();
500 buffer_consume(&stdin_buffer, len);
501 /* If we have a pending EOF, send it now. */
502 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
503 packet_start(SSH_CMSG_EOF);
504 packet_send();
505 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000506 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000507}
508
Damien Miller5428f641999-11-25 11:54:57 +1100509/*
510 * Checks if the client window has changed, and sends a packet about it to
511 * the server if so. The actual change is detected elsewhere (by a software
512 * interrupt on Unix); this just checks the flag and sends a message if
513 * appropriate.
514 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000515
Ben Lindstrombba81212001-06-25 05:01:22 +0000516static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000517client_check_window_change(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518{
Damien Miller1383bd82000-04-06 12:32:37 +1000519 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000520
Damien Miller1383bd82000-04-06 12:32:37 +1000521 if (! received_window_change_signal)
522 return;
523 /** XXX race */
524 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000525
Damien Millerd3444942000-09-30 14:20:03 +1100526 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000527
528 if (compat20) {
Damien Miller0e220db2004-06-15 10:34:08 +1000529 channel_send_window_changes();
Damien Miller1383bd82000-04-06 12:32:37 +1000530 } else {
Damien Miller0e220db2004-06-15 10:34:08 +1000531 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
532 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000533 packet_start(SSH_CMSG_WINDOW_SIZE);
Damien Miller71a73672006-03-26 14:04:36 +1100534 packet_put_int((u_int)ws.ws_row);
535 packet_put_int((u_int)ws.ws_col);
536 packet_put_int((u_int)ws.ws_xpixel);
537 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller1383bd82000-04-06 12:32:37 +1000538 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000539 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000540}
541
Damien Miller509b0102003-12-17 16:33:10 +1100542static void
543client_global_request_reply(int type, u_int32_t seq, void *ctxt)
544{
Darren Tucker9f407c42008-06-13 04:50:27 +1000545 struct global_confirm *gc;
546
547 if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
548 return;
549 if (gc->cb != NULL)
550 gc->cb(type, seq, gc->ctx);
551 if (--gc->ref_count <= 0) {
552 TAILQ_REMOVE(&global_confirms, gc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +1100553 explicit_bzero(gc, sizeof(*gc));
Darren Tuckera627d422013-06-02 07:31:17 +1000554 free(gc);
Darren Tucker9f407c42008-06-13 04:50:27 +1000555 }
556
Darren Tuckerf7288d72009-06-21 18:12:20 +1000557 packet_set_alive_timeouts(0);
Damien Miller509b0102003-12-17 16:33:10 +1100558}
559
560static void
561server_alive_check(void)
562{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000563 if (packet_inc_alive_timeouts() > options.server_alive_count_max) {
Damien Millerb73b6fd2011-01-11 17:18:56 +1100564 logit("Timeout, server %s not responding.", host);
Damien Miller985a4482006-10-24 03:02:41 +1000565 cleanup_exit(255);
566 }
Damien Miller509b0102003-12-17 16:33:10 +1100567 packet_start(SSH2_MSG_GLOBAL_REQUEST);
568 packet_put_cstring("keepalive@openssh.com");
569 packet_put_char(1); /* boolean: want reply */
570 packet_send();
Darren Tucker9f407c42008-06-13 04:50:27 +1000571 /* Insert an empty placeholder to maintain ordering */
572 client_register_global_confirm(NULL, NULL);
Damien Miller509b0102003-12-17 16:33:10 +1100573}
574
Damien Miller5428f641999-11-25 11:54:57 +1100575/*
576 * Waits until the client can do something (some data becomes available on
577 * one of the file descriptors).
578 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000579static void
Damien Miller5e953212001-01-30 09:14:00 +1100580client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000581 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000582{
Damien Miller509b0102003-12-17 16:33:10 +1100583 struct timeval tv, *tvp;
Damien Millere11e1ea2010-08-03 16:04:46 +1000584 int timeout_secs;
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000585 time_t minwait_secs = 0, server_alive_time = 0, now = monotime();
Damien Miller509b0102003-12-17 16:33:10 +1100586 int ret;
587
Damien Miller5e953212001-01-30 09:14:00 +1100588 /* Add any selections by the channel mechanism. */
Damien Millera6508752012-04-22 11:21:10 +1000589 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp,
590 &minwait_secs, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000591
Damien Miller1383bd82000-04-06 12:32:37 +1000592 if (!compat20) {
593 /* Read from the connection, unless our buffers are full. */
594 if (buffer_len(&stdout_buffer) < buffer_high &&
595 buffer_len(&stderr_buffer) < buffer_high &&
596 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100597 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000598 /*
599 * Read from stdin, unless we have seen EOF or have very much
600 * buffered data to send to the server.
601 */
602 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100603 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000604
605 /* Select stdout/stderr if have data in buffer. */
606 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100607 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000608 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100609 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000610 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000611 /* channel_prepare_select could have closed the last channel */
Damien Miller164a7f42001-10-12 11:36:09 +1000612 if (session_closed && !channel_still_open() &&
613 !packet_have_data_to_write()) {
614 /* clear mask since we did not call select() */
Damien Miller79faeff2001-11-12 11:06:32 +1100615 memset(*readsetp, 0, *nallocp);
616 memset(*writesetp, 0, *nallocp);
Damien Miller164a7f42001-10-12 11:36:09 +1000617 return;
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000618 } else {
619 FD_SET(connection_in, *readsetp);
620 }
Damien Miller1383bd82000-04-06 12:32:37 +1000621 }
622
Damien Miller95def091999-11-25 00:26:21 +1100623 /* Select server connection if have data to write to the server. */
624 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100625 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100626
Damien Miller5428f641999-11-25 11:54:57 +1100627 /*
628 * Wait for something to happen. This will suspend the process until
629 * some selected descriptor can be read, written, or has some other
Damien Millere11e1ea2010-08-03 16:04:46 +1000630 * event pending, or a timeout expires.
Damien Miller5428f641999-11-25 11:54:57 +1100631 */
Damien Miller95def091999-11-25 00:26:21 +1100632
Damien Millere11e1ea2010-08-03 16:04:46 +1000633 timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000634 if (options.server_alive_interval > 0 && compat20) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000635 timeout_secs = options.server_alive_interval;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000636 server_alive_time = now + options.server_alive_interval;
637 }
638 if (options.rekey_interval > 0 && compat20 && !rekeying)
639 timeout_secs = MIN(timeout_secs, packet_get_rekey_timeout());
Damien Millere11e1ea2010-08-03 16:04:46 +1000640 set_control_persist_exit_time();
641 if (control_persist_exit_time > 0) {
642 timeout_secs = MIN(timeout_secs,
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000643 control_persist_exit_time - now);
Damien Millere11e1ea2010-08-03 16:04:46 +1000644 if (timeout_secs < 0)
645 timeout_secs = 0;
646 }
Damien Millera6508752012-04-22 11:21:10 +1000647 if (minwait_secs != 0)
648 timeout_secs = MIN(timeout_secs, (int)minwait_secs);
Damien Millere11e1ea2010-08-03 16:04:46 +1000649 if (timeout_secs == INT_MAX)
Damien Miller509b0102003-12-17 16:33:10 +1100650 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000651 else {
Damien Millere11e1ea2010-08-03 16:04:46 +1000652 tv.tv_sec = timeout_secs;
Damien Miller509b0102003-12-17 16:33:10 +1100653 tv.tv_usec = 0;
654 tvp = &tv;
655 }
Damien Millere11e1ea2010-08-03 16:04:46 +1000656
Damien Miller509b0102003-12-17 16:33:10 +1100657 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
658 if (ret < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100659 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000660
661 /*
662 * We have to clear the select masks, because we return.
663 * We have to return, because the mainloop checks for the flags
664 * set by the signal handlers.
665 */
Damien Miller79faeff2001-11-12 11:06:32 +1100666 memset(*readsetp, 0, *nallocp);
667 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000668
Damien Miller95def091999-11-25 00:26:21 +1100669 if (errno == EINTR)
670 return;
671 /* Note: we might still have data in the buffers. */
672 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
673 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100674 quit_pending = 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000675 } else if (ret == 0) {
676 /*
677 * Timeout. Could have been either keepalive or rekeying.
678 * Keepalive we check here, rekeying is checked in clientloop.
679 */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000680 if (server_alive_time != 0 && server_alive_time <= monotime())
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000681 server_alive_check();
682 }
683
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000684}
685
Ben Lindstrombba81212001-06-25 05:01:22 +0000686static void
Damien Millerad833b32000-08-23 10:46:23 +1000687client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000688{
Damien Miller95def091999-11-25 00:26:21 +1100689 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000690 if (buffer_len(bout) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000691 atomicio(vwrite, fileno(stdout), buffer_ptr(bout),
692 buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000693 if (buffer_len(berr) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000694 atomicio(vwrite, fileno(stderr), buffer_ptr(berr),
695 buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000696
Damien Miller21771e22011-05-15 08:45:50 +1000697 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000698
Damien Miller5428f641999-11-25 11:54:57 +1100699 /*
700 * Free (and clear) the buffer to reduce the amount of data that gets
701 * written to swap.
702 */
Damien Millerad833b32000-08-23 10:46:23 +1000703 buffer_free(bin);
704 buffer_free(bout);
705 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000706
Damien Miller95def091999-11-25 00:26:21 +1100707 /* Send the suspend signal to the program itself. */
708 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000709
Darren Tucker5d78de62004-11-05 20:35:44 +1100710 /* Reset window sizes in case they have changed */
711 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000712
Damien Miller95def091999-11-25 00:26:21 +1100713 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000714 buffer_init(bin);
715 buffer_init(bout);
716 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000717
Damien Miller21771e22011-05-15 08:45:50 +1000718 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000719}
720
Ben Lindstrombba81212001-06-25 05:01:22 +0000721static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100722client_process_net_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000723{
Darren Tuckerc5564e12009-06-21 18:53:53 +1000724 int len, cont = 0;
Darren Tucker86e30a02009-08-28 11:21:06 +1000725 char buf[SSH_IOBUFSZ];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000726
Damien Miller5428f641999-11-25 11:54:57 +1100727 /*
728 * Read input from the server, and add any such data to the buffer of
729 * the packet subsystem.
730 */
Damien Miller95def091999-11-25 00:26:21 +1100731 if (FD_ISSET(connection_in, readset)) {
732 /* Read as much as possible. */
Darren Tuckerc5564e12009-06-21 18:53:53 +1000733 len = roaming_read(connection_in, buf, sizeof(buf), &cont);
734 if (len == 0 && cont == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000735 /*
736 * Received EOF. The remote host has closed the
737 * connection.
738 */
739 snprintf(buf, sizeof buf,
740 "Connection to %.300s closed by remote host.\r\n",
741 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000742 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000743 quit_pending = 1;
744 return;
Damien Miller95def091999-11-25 00:26:21 +1100745 }
Damien Miller5428f641999-11-25 11:54:57 +1100746 /*
747 * There is a kernel bug on Solaris that causes select to
748 * sometimes wake up even though there is no data available.
749 */
Damien Millerd8968ad2008-07-04 23:10:49 +1000750 if (len < 0 &&
751 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Damien Miller95def091999-11-25 00:26:21 +1100752 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000753
Damien Miller95def091999-11-25 00:26:21 +1100754 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000755 /*
756 * An error has encountered. Perhaps there is a
757 * network problem.
758 */
759 snprintf(buf, sizeof buf,
760 "Read from remote host %.300s: %.100s\r\n",
761 host, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100762 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100763 quit_pending = 1;
764 return;
765 }
766 packet_process_incoming(buf, len);
767 }
Damien Miller1383bd82000-04-06 12:32:37 +1000768}
769
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000770static void
Damien Miller5771ed72008-05-19 15:35:33 +1000771client_status_confirm(int type, Channel *c, void *ctx)
Damien Miller0e220db2004-06-15 10:34:08 +1000772{
Damien Miller5771ed72008-05-19 15:35:33 +1000773 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
774 char errmsg[256];
775 int tochan;
Darren Tuckerfc959702004-07-17 16:12:08 +1000776
Damien Miller555f3b82011-05-15 08:48:05 +1000777 /*
778 * If a TTY was explicitly requested, then a failure to allocate
779 * one is fatal.
780 */
781 if (cr->action == CONFIRM_TTY &&
782 (options.request_tty == REQUEST_TTY_FORCE ||
783 options.request_tty == REQUEST_TTY_YES))
784 cr->action = CONFIRM_CLOSE;
785
Damien Miller5771ed72008-05-19 15:35:33 +1000786 /* XXX supress on mux _client_ quietmode */
787 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
Damien Millere1537f92010-01-26 13:26:22 +1100788 c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
Damien Miller0e220db2004-06-15 10:34:08 +1000789
Damien Miller5771ed72008-05-19 15:35:33 +1000790 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
791 debug2("%s request accepted on channel %d",
792 cr->request_type, c->self);
793 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
794 if (tochan) {
795 snprintf(errmsg, sizeof(errmsg),
796 "%s request failed\r\n", cr->request_type);
797 } else {
798 snprintf(errmsg, sizeof(errmsg),
799 "%s request failed on channel %d",
800 cr->request_type, c->self);
801 }
802 /* If error occurred on primary session channel, then exit */
Damien Miller555f3b82011-05-15 08:48:05 +1000803 if (cr->action == CONFIRM_CLOSE && c->self == session_ident)
Damien Miller5771ed72008-05-19 15:35:33 +1000804 fatal("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000805 /*
806 * If error occurred on mux client, append to
807 * their stderr.
808 */
809 if (tochan) {
810 buffer_append(&c->extended, errmsg,
811 strlen(errmsg));
812 } else
Damien Miller5771ed72008-05-19 15:35:33 +1000813 error("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000814 if (cr->action == CONFIRM_TTY) {
815 /*
816 * If a TTY allocation error occurred, then arrange
817 * for the correct TTY to leave raw mode.
818 */
819 if (c->self == session_ident)
820 leave_raw_mode(0);
821 else
822 mux_tty_alloc_failed(c);
823 } else if (cr->action == CONFIRM_CLOSE) {
Damien Miller5771ed72008-05-19 15:35:33 +1000824 chan_read_failed(c);
825 chan_write_failed(c);
826 }
Damien Miller0e220db2004-06-15 10:34:08 +1000827 }
Darren Tuckera627d422013-06-02 07:31:17 +1000828 free(cr);
Damien Miller5771ed72008-05-19 15:35:33 +1000829}
Damien Miller0e220db2004-06-15 10:34:08 +1000830
Damien Miller5771ed72008-05-19 15:35:33 +1000831static void
832client_abandon_status_confirm(Channel *c, void *ctx)
833{
Darren Tuckera627d422013-06-02 07:31:17 +1000834 free(ctx);
Damien Miller5771ed72008-05-19 15:35:33 +1000835}
836
Damien Miller6d7b4372011-06-23 08:31:57 +1000837void
Damien Miller555f3b82011-05-15 08:48:05 +1000838client_expect_confirm(int id, const char *request,
839 enum confirm_action action)
Damien Miller5771ed72008-05-19 15:35:33 +1000840{
Damien Miller6c81fee2013-11-08 12:19:55 +1100841 struct channel_reply_ctx *cr = xcalloc(1, sizeof(*cr));
Damien Miller5771ed72008-05-19 15:35:33 +1000842
843 cr->request_type = request;
Damien Miller555f3b82011-05-15 08:48:05 +1000844 cr->action = action;
Damien Miller5771ed72008-05-19 15:35:33 +1000845
846 channel_register_status_confirm(id, client_status_confirm,
847 client_abandon_status_confirm, cr);
Damien Miller0e220db2004-06-15 10:34:08 +1000848}
849
Darren Tucker9f407c42008-06-13 04:50:27 +1000850void
851client_register_global_confirm(global_confirm_cb *cb, void *ctx)
852{
Damien Millerb9d3bee2008-07-16 22:40:52 +1000853 struct global_confirm *gc, *last_gc;
Darren Tucker9f407c42008-06-13 04:50:27 +1000854
855 /* Coalesce identical callbacks */
Damien Millerb9d3bee2008-07-16 22:40:52 +1000856 last_gc = TAILQ_LAST(&global_confirms, global_confirms);
857 if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
858 if (++last_gc->ref_count >= INT_MAX)
859 fatal("%s: last_gc->ref_count = %d",
860 __func__, last_gc->ref_count);
Darren Tucker9f407c42008-06-13 04:50:27 +1000861 return;
862 }
863
Damien Miller6c81fee2013-11-08 12:19:55 +1100864 gc = xcalloc(1, sizeof(*gc));
Darren Tucker9f407c42008-06-13 04:50:27 +1000865 gc->cb = cb;
866 gc->ctx = ctx;
867 gc->ref_count = 1;
868 TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
869}
870
Damien Miller0e220db2004-06-15 10:34:08 +1000871static void
Ben Lindstrom681d9322002-03-22 03:53:00 +0000872process_cmdline(void)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000873{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000874 void (*handler)(int);
Damien Miller7acefbb2014-07-18 14:11:24 +1000875 char *s, *cmd;
876 int ok, delete = 0, local = 0, remote = 0, dynamic = 0;
877 struct Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000878
Damien Miller1d2c4562014-02-04 11:18:20 +1100879 memset(&fwd, 0, sizeof(fwd));
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100880
Damien Miller21771e22011-05-15 08:45:50 +1000881 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000882 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000883 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000884 if (s == NULL)
885 goto out;
Damien Millerfdb23062013-11-21 13:57:15 +1100886 while (isspace((u_char)*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000887 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000888 if (*s == '-')
889 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000890 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000891 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000892
Darren Tucker1973c882004-05-24 10:34:36 +1000893 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000894 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000895 logit(" -L[bind_address:]port:host:hostport "
896 "Request local forward");
897 logit(" -R[bind_address:]port:host:hostport "
898 "Request remote forward");
Damien Miller0164cb82008-11-05 16:30:31 +1100899 logit(" -D[bind_address:]port "
900 "Request dynamic forward");
Damien Millerff773642011-09-22 21:39:48 +1000901 logit(" -KL[bind_address:]port "
902 "Cancel local forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000903 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000904 "Cancel remote forward");
Damien Millerff773642011-09-22 21:39:48 +1000905 logit(" -KD[bind_address:]port "
906 "Cancel dynamic forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100907 if (!options.permit_local_command)
908 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000909 logit(" !args "
910 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100911 goto out;
912 }
913
914 if (*s == '!' && options.permit_local_command) {
915 s++;
916 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000917 goto out;
918 }
919
920 if (*s == 'K') {
921 delete = 1;
922 s++;
923 }
Damien Miller0164cb82008-11-05 16:30:31 +1100924 if (*s == 'L')
925 local = 1;
926 else if (*s == 'R')
927 remote = 1;
928 else if (*s == 'D')
929 dynamic = 1;
930 else {
Damien Miller996acd22003-04-09 20:59:48 +1000931 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000932 goto out;
933 }
Damien Miller0164cb82008-11-05 16:30:31 +1100934
Damien Millerff773642011-09-22 21:39:48 +1000935 if (delete && !compat20) {
Damien Miller996acd22003-04-09 20:59:48 +1000936 logit("Not supported for SSH protocol version 1.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000937 goto out;
938 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000939
Damien Millerfdb23062013-11-21 13:57:15 +1100940 while (isspace((u_char)*++s))
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100941 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000942
Damien Millere1537f92010-01-26 13:26:22 +1100943 /* XXX update list of forwards in options */
Darren Tuckere7066df2004-05-24 10:18:05 +1000944 if (delete) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000945 /* We pass 1 for dynamicfwd to restrict to 1 or 2 fields. */
946 if (!parse_forward(&fwd, s, 1, 0)) {
947 logit("Bad forwarding close specification.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000948 goto out;
949 }
Damien Millerff773642011-09-22 21:39:48 +1000950 if (remote)
Damien Miller7acefbb2014-07-18 14:11:24 +1000951 ok = channel_request_rforward_cancel(&fwd) == 0;
Damien Millerff773642011-09-22 21:39:48 +1000952 else if (dynamic)
Damien Miller7acefbb2014-07-18 14:11:24 +1000953 ok = channel_cancel_lport_listener(&fwd,
954 0, &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000955 else
Damien Miller7acefbb2014-07-18 14:11:24 +1000956 ok = channel_cancel_lport_listener(&fwd,
957 CHANNEL_CANCEL_PORT_STATIC,
958 &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000959 if (!ok) {
960 logit("Unkown port forwarding.");
961 goto out;
962 }
963 logit("Canceled forwarding.");
Darren Tuckere7066df2004-05-24 10:18:05 +1000964 } else {
Damien Miller4bf648f2009-02-14 16:28:21 +1100965 if (!parse_forward(&fwd, s, dynamic, remote)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000966 logit("Bad forwarding specification.");
967 goto out;
968 }
Damien Miller0164cb82008-11-05 16:30:31 +1100969 if (local || dynamic) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000970 if (!channel_setup_local_fwd_listener(&fwd,
971 &options.fwd_opts)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000972 logit("Port forwarding failed.");
973 goto out;
974 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100975 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +1000976 if (channel_request_remote_forwarding(&fwd) < 0) {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000977 logit("Port forwarding failed.");
978 goto out;
979 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100980 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000981 logit("Forwarding port.");
982 }
983
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000984out:
985 signal(SIGINT, handler);
Damien Miller21771e22011-05-15 08:45:50 +1000986 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tuckera627d422013-06-02 07:31:17 +1000987 free(cmd);
988 free(fwd.listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000989 free(fwd.listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000990 free(fwd.connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000991 free(fwd.connect_path);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000992}
993
Darren Tucker92a39cf2012-09-07 11:20:20 +1000994/* reasons to suppress output of an escape command in help output */
995#define SUPPRESS_NEVER 0 /* never suppress, always show */
996#define SUPPRESS_PROTO1 1 /* don't show in protocol 1 sessions */
997#define SUPPRESS_MUXCLIENT 2 /* don't show in mux client sessions */
998#define SUPPRESS_MUXMASTER 4 /* don't show in mux master sessions */
999#define SUPPRESS_SYSLOG 8 /* don't show when logging to syslog */
1000struct escape_help_text {
1001 const char *cmd;
1002 const char *text;
1003 unsigned int flags;
1004};
1005static struct escape_help_text esc_txt[] = {
1006 {".", "terminate session", SUPPRESS_MUXMASTER},
1007 {".", "terminate connection (and any multiplexed sessions)",
1008 SUPPRESS_MUXCLIENT},
1009 {"B", "send a BREAK to the remote system", SUPPRESS_PROTO1},
1010 {"C", "open a command line", SUPPRESS_MUXCLIENT},
1011 {"R", "request rekey", SUPPRESS_PROTO1},
Darren Tuckerca0d0fd2012-09-07 11:22:24 +10001012 {"V/v", "decrease/increase verbosity (LogLevel)", SUPPRESS_MUXCLIENT},
Darren Tucker92a39cf2012-09-07 11:20:20 +10001013 {"^Z", "suspend ssh", SUPPRESS_MUXCLIENT},
1014 {"#", "list forwarded connections", SUPPRESS_NEVER},
1015 {"&", "background ssh (when waiting for connections to terminate)",
1016 SUPPRESS_MUXCLIENT},
1017 {"?", "this message", SUPPRESS_NEVER},
1018};
1019
1020static void
1021print_escape_help(Buffer *b, int escape_char, int protocol2, int mux_client,
1022 int using_stderr)
1023{
1024 unsigned int i, suppress_flags;
1025 char string[1024];
1026
1027 snprintf(string, sizeof string, "%c?\r\n"
1028 "Supported escape sequences:\r\n", escape_char);
1029 buffer_append(b, string, strlen(string));
1030
1031 suppress_flags = (protocol2 ? 0 : SUPPRESS_PROTO1) |
1032 (mux_client ? SUPPRESS_MUXCLIENT : 0) |
1033 (mux_client ? 0 : SUPPRESS_MUXMASTER) |
1034 (using_stderr ? 0 : SUPPRESS_SYSLOG);
1035
1036 for (i = 0; i < sizeof(esc_txt)/sizeof(esc_txt[0]); i++) {
1037 if (esc_txt[i].flags & suppress_flags)
1038 continue;
Darren Tuckerca0d0fd2012-09-07 11:22:24 +10001039 snprintf(string, sizeof string, " %c%-3s - %s\r\n",
Darren Tucker92a39cf2012-09-07 11:20:20 +10001040 escape_char, esc_txt[i].cmd, esc_txt[i].text);
1041 buffer_append(b, string, strlen(string));
1042 }
1043
1044 snprintf(string, sizeof string,
Darren Tuckerca0d0fd2012-09-07 11:22:24 +10001045 " %c%c - send the escape character by typing it twice\r\n"
Darren Tucker92a39cf2012-09-07 11:20:20 +10001046 "(Note that escapes are only recognized immediately after "
1047 "newline.)\r\n", escape_char, escape_char);
1048 buffer_append(b, string, strlen(string));
1049}
1050
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001051/*
1052 * Process the characters one by one, call with c==NULL for proto1 case.
1053 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001054static int
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001055process_escapes(Channel *c, Buffer *bin, Buffer *bout, Buffer *berr,
1056 char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001057{
1058 char string[1024];
1059 pid_t pid;
1060 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001061 u_int i;
1062 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +10001063 char *s;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001064 int *escape_pendingp, escape_char;
1065 struct escape_filter_ctx *efc;
Damien Millerad833b32000-08-23 10:46:23 +10001066
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001067 if (c == NULL) {
1068 escape_pendingp = &escape_pending1;
1069 escape_char = escape_char1;
1070 } else {
1071 if (c->filter_ctx == NULL)
1072 return 0;
1073 efc = (struct escape_filter_ctx *)c->filter_ctx;
1074 escape_pendingp = &efc->escape_pending;
1075 escape_char = efc->escape_char;
1076 }
1077
Damien Millereccb9de2005-06-17 12:59:34 +10001078 if (len <= 0)
1079 return (0);
1080
1081 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +10001082 /* Get one character at a time. */
1083 ch = buf[i];
1084
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001085 if (*escape_pendingp) {
Damien Millerad833b32000-08-23 10:46:23 +10001086 /* We have previously seen an escape character. */
1087 /* Clear the flag now. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001088 *escape_pendingp = 0;
Damien Millerad833b32000-08-23 10:46:23 +10001089
1090 /* Process the escaped character. */
1091 switch (ch) {
1092 case '.':
1093 /* Terminate the connection. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001094 snprintf(string, sizeof string, "%c.\r\n",
1095 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001096 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001097
Damien Millere1537f92010-01-26 13:26:22 +11001098 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001099 chan_read_failed(c);
1100 chan_write_failed(c);
Darren Tuckerea8342c2013-06-06 08:11:40 +10001101 if (c->detach_user)
1102 c->detach_user(c->self, NULL);
Damien Miller36187092013-06-10 13:07:11 +10001103 c->type = SSH_CHANNEL_ABANDONED;
1104 buffer_clear(&c->input);
1105 chan_ibuf_empty(c);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001106 return 0;
1107 } else
1108 quit_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001109 return -1;
1110
1111 case 'Z' - 64:
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001112 /* XXX support this for mux clients */
Damien Millere1537f92010-01-26 13:26:22 +11001113 if (c && c->ctl_chan != -1) {
Darren Tuckerf111d402012-09-07 11:21:42 +10001114 char b[16];
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001115 noescape:
Darren Tuckerf111d402012-09-07 11:21:42 +10001116 if (ch == 'Z' - 64)
1117 snprintf(b, sizeof b, "^Z");
1118 else
1119 snprintf(b, sizeof b, "%c", ch);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001120 snprintf(string, sizeof string,
Darren Tuckerf111d402012-09-07 11:21:42 +10001121 "%c%s escape not available to "
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001122 "multiplexed sessions\r\n",
Darren Tuckerf111d402012-09-07 11:21:42 +10001123 escape_char, b);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001124 buffer_append(berr, string,
1125 strlen(string));
1126 continue;
1127 }
Darren Tucker4d5cd332008-06-13 04:51:14 +10001128 /* Suspend the program. Inform the user */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001129 snprintf(string, sizeof string,
1130 "%c^Z [suspend ssh]\r\n", escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001131 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001132
1133 /* Restore terminal modes and suspend. */
1134 client_suspend_self(bin, bout, berr);
1135
1136 /* We have been continued. */
1137 continue;
1138
Damien Miller54c45982003-05-15 10:20:13 +10001139 case 'B':
1140 if (compat20) {
1141 snprintf(string, sizeof string,
1142 "%cB\r\n", escape_char);
1143 buffer_append(berr, string,
1144 strlen(string));
Damien Miller70182522013-09-14 09:49:19 +10001145 channel_request_start(c->self,
Damien Miller54c45982003-05-15 10:20:13 +10001146 "break", 0);
1147 packet_put_int(1000);
1148 packet_send();
1149 }
1150 continue;
1151
Ben Lindstromf28f6342001-04-04 02:03:04 +00001152 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001153 if (compat20) {
1154 if (datafellows & SSH_BUG_NOREKEY)
Darren Tucker4d5cd332008-06-13 04:51:14 +10001155 logit("Server does not "
1156 "support re-keying");
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001157 else
1158 need_rekeying = 1;
1159 }
Ben Lindstromf28f6342001-04-04 02:03:04 +00001160 continue;
1161
Darren Tucker50a48d02012-09-06 21:25:37 +10001162 case 'V':
1163 /* FALLTHROUGH */
1164 case 'v':
1165 if (c && c->ctl_chan != -1)
1166 goto noescape;
1167 if (!log_is_on_stderr()) {
1168 snprintf(string, sizeof string,
1169 "%c%c [Logging to syslog]\r\n",
1170 escape_char, ch);
1171 buffer_append(berr, string,
1172 strlen(string));
1173 continue;
1174 }
1175 if (ch == 'V' && options.log_level >
1176 SYSLOG_LEVEL_QUIET)
1177 log_change_level(--options.log_level);
1178 if (ch == 'v' && options.log_level <
1179 SYSLOG_LEVEL_DEBUG3)
1180 log_change_level(++options.log_level);
1181 snprintf(string, sizeof string,
1182 "%c%c [LogLevel %s]\r\n", escape_char, ch,
1183 log_level_name(options.log_level));
1184 buffer_append(berr, string, strlen(string));
1185 continue;
1186
Damien Millerad833b32000-08-23 10:46:23 +10001187 case '&':
Damien Millere1537f92010-01-26 13:26:22 +11001188 if (c && c->ctl_chan != -1)
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001189 goto noescape;
Damien Millerad833b32000-08-23 10:46:23 +10001190 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001191 * Detach the program (continue to serve
1192 * connections, but put in background and no
1193 * more new connections).
Damien Millerad833b32000-08-23 10:46:23 +10001194 */
Damien Miller96507ef2001-11-12 10:52:25 +11001195 /* Restore tty modes. */
Damien Miller21771e22011-05-15 08:45:50 +10001196 leave_raw_mode(
1197 options.request_tty == REQUEST_TTY_FORCE);
Damien Miller96507ef2001-11-12 10:52:25 +11001198
1199 /* Stop listening for new connections. */
1200 channel_stop_listening();
1201
1202 snprintf(string, sizeof string,
1203 "%c& [backgrounded]\n", escape_char);
1204 buffer_append(berr, string, strlen(string));
1205
1206 /* Fork into background. */
1207 pid = fork();
1208 if (pid < 0) {
1209 error("fork: %.100s", strerror(errno));
1210 continue;
1211 }
1212 if (pid != 0) { /* This is the parent. */
1213 /* The parent just exits. */
1214 exit(0);
1215 }
1216 /* The child continues serving connections. */
1217 if (compat20) {
1218 buffer_append(bin, "\004", 1);
1219 /* fake EOF on stdin */
1220 return -1;
1221 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +10001222 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001223 * Sending SSH_CMSG_EOF alone does not
1224 * always appear to be enough. So we
1225 * try to send an EOF character first.
Damien Millerad833b32000-08-23 10:46:23 +10001226 */
1227 packet_start(SSH_CMSG_STDIN_DATA);
1228 packet_put_string("\004", 1);
1229 packet_send();
1230 /* Close stdin. */
1231 stdin_eof = 1;
1232 if (buffer_len(bin) == 0) {
1233 packet_start(SSH_CMSG_EOF);
1234 packet_send();
1235 }
1236 }
Damien Miller96507ef2001-11-12 10:52:25 +11001237 continue;
Damien Millerad833b32000-08-23 10:46:23 +10001238
1239 case '?':
Darren Tucker92a39cf2012-09-07 11:20:20 +10001240 print_escape_help(berr, escape_char, compat20,
1241 (c && c->ctl_chan != -1),
1242 log_is_on_stderr());
Damien Millerad833b32000-08-23 10:46:23 +10001243 continue;
1244
1245 case '#':
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001246 snprintf(string, sizeof string, "%c#\r\n",
1247 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001248 buffer_append(berr, string, strlen(string));
1249 s = channel_open_message();
1250 buffer_append(berr, s, strlen(s));
Darren Tuckera627d422013-06-02 07:31:17 +10001251 free(s);
Damien Millerad833b32000-08-23 10:46:23 +10001252 continue;
1253
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001254 case 'C':
Damien Millere1537f92010-01-26 13:26:22 +11001255 if (c && c->ctl_chan != -1)
Damien Miller586b0052008-12-09 14:11:32 +11001256 goto noescape;
Ben Lindstrom681d9322002-03-22 03:53:00 +00001257 process_cmdline();
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001258 continue;
1259
Damien Millerad833b32000-08-23 10:46:23 +10001260 default:
1261 if (ch != escape_char) {
1262 buffer_put_char(bin, escape_char);
1263 bytes++;
1264 }
1265 /* Escaped characters fall through here */
1266 break;
1267 }
1268 } else {
1269 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001270 * The previous character was not an escape char.
1271 * Check if this is an escape.
Damien Millerad833b32000-08-23 10:46:23 +10001272 */
1273 if (last_was_cr && ch == escape_char) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001274 /*
1275 * It is. Set the flag and continue to
1276 * next character.
1277 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001278 *escape_pendingp = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001279 continue;
1280 }
1281 }
1282
1283 /*
1284 * Normal character. Record whether it was a newline,
1285 * and append it to the buffer.
1286 */
1287 last_was_cr = (ch == '\r' || ch == '\n');
1288 buffer_put_char(bin, ch);
1289 bytes++;
1290 }
1291 return bytes;
1292}
1293
Ben Lindstrombba81212001-06-25 05:01:22 +00001294static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001295client_process_input(fd_set *readset)
Damien Miller1383bd82000-04-06 12:32:37 +10001296{
Damien Miller166fca82000-04-20 07:42:21 +10001297 int len;
Darren Tucker86e30a02009-08-28 11:21:06 +10001298 char buf[SSH_IOBUFSZ];
Damien Miller1383bd82000-04-06 12:32:37 +10001299
Damien Miller95def091999-11-25 00:26:21 +11001300 /* Read input from stdin. */
1301 if (FD_ISSET(fileno(stdin), readset)) {
1302 /* Read as much as possible. */
1303 len = read(fileno(stdin), buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +10001304 if (len < 0 &&
1305 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001306 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +11001307 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001308 /*
1309 * Received EOF or error. They are treated
1310 * similarly, except that an error message is printed
1311 * if it was an error condition.
1312 */
Damien Miller95def091999-11-25 00:26:21 +11001313 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001314 snprintf(buf, sizeof buf, "read: %.100s\r\n",
1315 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001316 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001317 }
1318 /* Mark that we have seen EOF. */
1319 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +11001320 /*
1321 * Send an EOF message to the server unless there is
1322 * data in the buffer. If there is data in the
1323 * buffer, no message will be sent now. Code
1324 * elsewhere will send the EOF when the buffer
1325 * becomes empty if stdin_eof is set.
1326 */
Damien Miller95def091999-11-25 00:26:21 +11001327 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001328 packet_start(SSH_CMSG_EOF);
1329 packet_send();
Damien Miller95def091999-11-25 00:26:21 +11001330 }
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001331 } else if (escape_char1 == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +11001332 /*
1333 * Normal successful read, and no escape character.
1334 * Just append the data to buffer.
1335 */
Damien Miller95def091999-11-25 00:26:21 +11001336 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +11001337 } else {
Damien Miller5428f641999-11-25 11:54:57 +11001338 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001339 * Normal, successful read. But we have an escape
1340 * character and have to process the characters one
1341 * by one.
Damien Miller5428f641999-11-25 11:54:57 +11001342 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001343 if (process_escapes(NULL, &stdin_buffer,
1344 &stdout_buffer, &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +10001345 return;
Damien Miller95def091999-11-25 00:26:21 +11001346 }
1347 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001348}
1349
Ben Lindstrombba81212001-06-25 05:01:22 +00001350static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001351client_process_output(fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001352{
Damien Miller95def091999-11-25 00:26:21 +11001353 int len;
1354 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001355
Damien Miller95def091999-11-25 00:26:21 +11001356 /* Write buffered output to stdout. */
1357 if (FD_ISSET(fileno(stdout), writeset)) {
1358 /* Write as much data as possible. */
1359 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001360 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001361 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001362 if (errno == EINTR || errno == EAGAIN ||
1363 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001364 len = 0;
1365 else {
Damien Miller5428f641999-11-25 11:54:57 +11001366 /*
1367 * An error or EOF was encountered. Put an
1368 * error message to stderr buffer.
1369 */
Darren Tucker4d5cd332008-06-13 04:51:14 +10001370 snprintf(buf, sizeof buf,
1371 "write stdout: %.50s\r\n", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001372 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001373 quit_pending = 1;
1374 return;
1375 }
1376 }
1377 /* Consume printed data from the buffer. */
1378 buffer_consume(&stdout_buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001379 }
Damien Miller95def091999-11-25 00:26:21 +11001380 /* Write buffered output to stderr. */
1381 if (FD_ISSET(fileno(stderr), writeset)) {
1382 /* Write as much data as possible. */
1383 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001384 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001385 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001386 if (errno == EINTR || errno == EAGAIN ||
1387 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001388 len = 0;
1389 else {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001390 /*
1391 * EOF or error, but can't even print
1392 * error message.
1393 */
Damien Miller95def091999-11-25 00:26:21 +11001394 quit_pending = 1;
1395 return;
1396 }
1397 }
1398 /* Consume printed characters from the buffer. */
1399 buffer_consume(&stderr_buffer, len);
1400 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001401}
1402
Damien Miller5428f641999-11-25 11:54:57 +11001403/*
Damien Millerb38eff82000-04-01 11:09:21 +10001404 * Get packets from the connection input buffer, and process them as long as
1405 * there are packets available.
1406 *
1407 * Any unknown packets received during the actual
1408 * session cause the session to terminate. This is
1409 * intended to make debugging easier since no
1410 * confirmations are sent. Any compatible protocol
1411 * extensions must be negotiated during the
1412 * preparatory phase.
1413 */
1414
Ben Lindstrombba81212001-06-25 05:01:22 +00001415static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001416client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001417{
Darren Tucker4d5cd332008-06-13 04:51:14 +10001418 dispatch_run(DISPATCH_NONBLOCK, &quit_pending,
1419 compat20 ? xxx_kex : NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001420}
1421
Damien Millerad833b32000-08-23 10:46:23 +10001422/* scan buf[] for '~' before sending data to the peer */
1423
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001424/* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1425void *
1426client_new_escape_filter_ctx(int escape_char)
1427{
1428 struct escape_filter_ctx *ret;
1429
Damien Miller6c81fee2013-11-08 12:19:55 +11001430 ret = xcalloc(1, sizeof(*ret));
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001431 ret->escape_pending = 0;
1432 ret->escape_char = escape_char;
1433 return (void *)ret;
1434}
1435
Darren Tucker84c56f52008-06-13 04:55:46 +10001436/* Free the escape filter context on channel free */
1437void
1438client_filter_cleanup(int cid, void *ctx)
1439{
Darren Tuckera627d422013-06-02 07:31:17 +10001440 free(ctx);
Darren Tucker84c56f52008-06-13 04:55:46 +10001441}
1442
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001443int
1444client_simple_escape_filter(Channel *c, char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001445{
Damien Miller5771ed72008-05-19 15:35:33 +10001446 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1447 return 0;
1448
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001449 return process_escapes(c, &c->input, &c->output, &c->extended,
1450 buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001451}
1452
Ben Lindstrombba81212001-06-25 05:01:22 +00001453static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001454client_channel_closed(int id, void *arg)
1455{
Damien Miller3ec27592001-10-12 11:35:04 +10001456 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001457 session_closed = 1;
Damien Miller21771e22011-05-15 08:45:50 +10001458 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001459}
1460
Damien Millerb38eff82000-04-01 11:09:21 +10001461/*
Damien Miller5428f641999-11-25 11:54:57 +11001462 * Implements the interactive session with the server. This is called after
1463 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001464 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1465 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001466 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001467
Damien Miller4af51302000-04-16 11:18:38 +10001468int
Damien Millerad833b32000-08-23 10:46:23 +10001469client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001470{
Damien Miller5e953212001-01-30 09:14:00 +11001471 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001472 double start_time, total_time;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001473 int max_fd = 0, max_fd2 = 0, len, rekeying = 0;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001474 u_int64_t ibytes, obytes;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001475 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001476 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001477
Damien Miller95def091999-11-25 00:26:21 +11001478 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001479
Damien Miller95def091999-11-25 00:26:21 +11001480 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001481
Damien Miller95def091999-11-25 00:26:21 +11001482 /* Initialize variables. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001483 escape_pending1 = 0;
Damien Miller95def091999-11-25 00:26:21 +11001484 last_was_cr = 1;
1485 exit_status = -1;
1486 stdin_eof = 0;
1487 buffer_high = 64 * 1024;
1488 connection_in = packet_get_connection_in();
1489 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +11001490 max_fd = MAX(connection_in, connection_out);
1491
1492 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +00001493 /* enable nonblocking unless tty */
1494 if (!isatty(fileno(stdin)))
1495 set_nonblock(fileno(stdin));
1496 if (!isatty(fileno(stdout)))
1497 set_nonblock(fileno(stdout));
1498 if (!isatty(fileno(stderr)))
1499 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +11001500 max_fd = MAX(max_fd, fileno(stdin));
1501 max_fd = MAX(max_fd, fileno(stdout));
1502 max_fd = MAX(max_fd, fileno(stderr));
1503 }
Damien Miller95def091999-11-25 00:26:21 +11001504 quit_pending = 0;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001505 escape_char1 = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001506
Damien Miller95def091999-11-25 00:26:21 +11001507 /* Initialize buffers. */
1508 buffer_init(&stdin_buffer);
1509 buffer_init(&stdout_buffer);
1510 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001511
Damien Millerb38eff82000-04-01 11:09:21 +10001512 client_init_dispatch();
1513
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001514 /*
1515 * Set signal handlers, (e.g. to restore non-blocking mode)
1516 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1517 */
Darren Tucker07336da2004-11-05 20:02:16 +11001518 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1519 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001520 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1521 signal(SIGINT, signal_handler);
1522 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1523 signal(SIGQUIT, signal_handler);
1524 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1525 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001526 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001527
Damien Miller95def091999-11-25 00:26:21 +11001528 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001529 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001530
Ben Lindstrom5b828322001-02-09 01:34:36 +00001531 if (compat20) {
1532 session_ident = ssh2_chan_id;
Damien Miller6c3eec72011-05-05 14:16:22 +10001533 if (session_ident != -1) {
1534 if (escape_char_arg != SSH_ESCAPECHAR_NONE) {
1535 channel_register_filter(session_ident,
1536 client_simple_escape_filter, NULL,
1537 client_filter_cleanup,
1538 client_new_escape_filter_ctx(
1539 escape_char_arg));
1540 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001541 channel_register_cleanup(session_ident,
Damien Miller39eda6e2005-11-05 14:52:50 +11001542 client_channel_closed, 0);
Damien Miller6c3eec72011-05-05 14:16:22 +10001543 }
Ben Lindstrom5b828322001-02-09 01:34:36 +00001544 } else {
1545 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +10001546 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001547 }
Damien Millerad833b32000-08-23 10:46:23 +10001548
Damien Miller95def091999-11-25 00:26:21 +11001549 /* Main loop of the client for the interactive session mode. */
1550 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001551
Damien Miller5428f641999-11-25 11:54:57 +11001552 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001553 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001554
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001555 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +10001556 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001557
1558 rekeying = (xxx_kex != NULL && !xxx_kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +10001559
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001560 if (rekeying) {
1561 debug("rekeying in progress");
1562 } else {
1563 /*
1564 * Make packets of buffered stdin data, and buffer
1565 * them for sending to the server.
1566 */
1567 if (!compat20)
1568 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001569
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001570 /*
1571 * Make packets from buffered channel data, and
1572 * enqueue them for sending to the server.
1573 */
1574 if (packet_not_very_much_data_to_write())
1575 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001576
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001577 /*
1578 * Check if the window size has changed, and buffer a
1579 * message about it to the server if so.
1580 */
1581 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001582
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001583 if (quit_pending)
1584 break;
1585 }
Damien Miller5428f641999-11-25 11:54:57 +11001586 /*
1587 * Wait until we have something to do (something becomes
1588 * available on one of the descriptors).
1589 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001590 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001591 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001592 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001593
Damien Miller95def091999-11-25 00:26:21 +11001594 if (quit_pending)
1595 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001596
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001597 /* Do channel operations unless rekeying in progress. */
1598 if (!rekeying) {
1599 channel_after_select(readset, writeset);
Damien Millera5539d22003-04-09 20:50:06 +10001600 if (need_rekeying || packet_need_rekeying()) {
1601 debug("need rekeying");
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001602 xxx_kex->done = 0;
1603 kex_send_kexinit(xxx_kex);
1604 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;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001732 packet_get_state(MODE_IN, NULL, NULL, NULL, &ibytes);
1733 packet_get_state(MODE_OUT, NULL, NULL, NULL, &obytes);
1734 verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
Damien Miller821de0a2011-01-11 17:20:29 +11001735 (unsigned long long)obytes, (unsigned long long)ibytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001736 if (total_time > 0)
Damien Millerb61f3fc2008-07-11 17:36:48 +10001737 verbose("Bytes per second: sent %.1f, received %.1f",
1738 obytes / total_time, ibytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001739 /* Return the exit status of the program. */
1740 debug("Exit status %d", exit_status);
1741 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001742}
Damien Millerb38eff82000-04-01 11:09:21 +10001743
1744/*********/
1745
Ben Lindstrombba81212001-06-25 05:01:22 +00001746static void
Damien Miller630d6f42002-01-22 23:17:30 +11001747client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001748{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001749 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001750 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001751 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001752 buffer_append(&stdout_buffer, data, data_len);
Damien Millera5103f42014-02-04 11:20:14 +11001753 explicit_bzero(data, data_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001754 free(data);
Damien Millerb38eff82000-04-01 11:09:21 +10001755}
Ben Lindstrombba81212001-06-25 05:01:22 +00001756static void
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);
Damien Millerb38eff82000-04-01 11:09:21 +10001765}
Ben Lindstrombba81212001-06-25 05:01:22 +00001766static void
Damien Miller630d6f42002-01-22 23:17:30 +11001767client_input_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001768{
Damien Millerb38eff82000-04-01 11:09:21 +10001769 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001770 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001771 /* Acknowledge the exit. */
1772 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1773 packet_send();
1774 /*
1775 * Must wait for packet to be sent since we are
1776 * exiting the loop.
1777 */
1778 packet_write_wait();
1779 /* Flag that we want to exit. */
1780 quit_pending = 1;
1781}
Darren Tucker5dcdd212003-10-02 16:17:00 +10001782static void
1783client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1784{
1785 Channel *c = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001786 int r, remote_id, sock;
Darren Tucker5dcdd212003-10-02 16:17:00 +10001787
1788 /* Read the remote channel number from the message. */
1789 remote_id = packet_get_int();
1790 packet_check_eom();
1791
1792 /*
1793 * Get a connection to the local authentication agent (this may again
1794 * get forwarded).
1795 */
djm@openbsd.org141efe42015-01-14 20:05:27 +00001796 if ((r = ssh_get_authentication_socket(&sock)) != 0 &&
1797 r != SSH_ERR_AGENT_NOT_PRESENT)
1798 debug("%s: ssh_get_authentication_socket: %s",
1799 __func__, ssh_err(r));
1800
Darren Tucker5dcdd212003-10-02 16:17:00 +10001801
1802 /*
1803 * If we could not connect the agent, send an error message back to
1804 * the server. This should never happen unless the agent dies,
1805 * because authentication forwarding is only enabled if we have an
1806 * agent.
1807 */
1808 if (sock >= 0) {
1809 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1810 -1, 0, 0, 0, "authentication agent connection", 1);
1811 c->remote_id = remote_id;
1812 c->force_drain = 1;
1813 }
1814 if (c == NULL) {
1815 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1816 packet_put_int(remote_id);
1817 } else {
1818 /* Send a confirmation to the remote host. */
1819 debug("Forwarding authentication connection.");
1820 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1821 packet_put_int(remote_id);
1822 packet_put_int(c->self);
1823 }
1824 packet_send();
1825}
Damien Millerb38eff82000-04-01 11:09:21 +10001826
Ben Lindstrombba81212001-06-25 05:01:22 +00001827static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001828client_request_forwarded_tcpip(const char *request_type, int rchan)
1829{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001830 Channel *c = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001831 char *listen_address, *originator_address;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001832 u_short listen_port, originator_port;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001833
1834 /* Get rest of the packet */
1835 listen_address = packet_get_string(NULL);
1836 listen_port = packet_get_int();
1837 originator_address = packet_get_string(NULL);
1838 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001839 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001840
Damien Miller7acefbb2014-07-18 14:11:24 +10001841 debug("%s: listen %s port %d, originator %s port %d", __func__,
1842 listen_address, listen_port, originator_address, originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001843
Damien Miller4b3ed642014-07-02 15:29:40 +10001844 c = channel_connect_by_listen_address(listen_address, listen_port,
Damien Millerbd740252008-05-19 15:37:09 +10001845 "forwarded-tcpip", originator_address);
1846
Darren Tuckera627d422013-06-02 07:31:17 +10001847 free(originator_address);
1848 free(listen_address);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001849 return c;
1850}
1851
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001852static Channel *
Damien Miller7acefbb2014-07-18 14:11:24 +10001853client_request_forwarded_streamlocal(const char *request_type, int rchan)
1854{
1855 Channel *c = NULL;
1856 char *listen_path;
1857
1858 /* Get the remote path. */
1859 listen_path = packet_get_string(NULL);
1860 /* XXX: Skip reserved field for now. */
1861 if (packet_get_string_ptr(NULL) == NULL)
1862 fatal("%s: packet_get_string_ptr failed", __func__);
1863 packet_check_eom();
1864
1865 debug("%s: %s", __func__, listen_path);
1866
1867 c = channel_connect_by_listen_path(listen_path,
1868 "forwarded-streamlocal@openssh.com", "forwarded-streamlocal");
1869 free(listen_path);
1870 return c;
1871}
1872
1873static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001874client_request_x11(const char *request_type, int rchan)
1875{
1876 Channel *c = NULL;
1877 char *originator;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001878 u_short originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001879 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001880
1881 if (!options.forward_x11) {
1882 error("Warning: ssh server tried X11 forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001883 error("Warning: this is probably a break-in attempt by a "
1884 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001885 return NULL;
1886 }
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001887 if (x11_refuse_time != 0 && monotime() >= x11_refuse_time) {
Damien Miller1ab6a512010-06-26 10:02:24 +10001888 verbose("Rejected X11 connection after ForwardX11Timeout "
1889 "expired");
1890 return NULL;
1891 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001892 originator = packet_get_string(NULL);
1893 if (datafellows & SSH_BUG_X11FWD) {
1894 debug2("buggy server: x11 request w/o originator_port");
1895 originator_port = 0;
1896 } else {
1897 originator_port = packet_get_int();
1898 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001899 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001900 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001901 debug("client_request_x11: request from %s %d", originator,
1902 originator_port);
Darren Tuckera627d422013-06-02 07:31:17 +10001903 free(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001904 sock = x11_connect_display();
1905 if (sock < 0)
1906 return NULL;
1907 c = channel_new("x11",
1908 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001909 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001910 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001911 return c;
1912}
1913
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001914static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001915client_request_agent(const char *request_type, int rchan)
1916{
1917 Channel *c = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001918 int r, sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001919
1920 if (!options.forward_agent) {
1921 error("Warning: ssh server tried agent forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001922 error("Warning: this is probably a break-in attempt by a "
1923 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001924 return NULL;
1925 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001926 if ((r = ssh_get_authentication_socket(&sock)) != 0) {
1927 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1928 debug("%s: ssh_get_authentication_socket: %s",
1929 __func__, ssh_err(r));
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001930 return NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001931 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001932 c = channel_new("authentication agent connection",
1933 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001934 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001935 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001936 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001937 return c;
1938}
1939
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001940int
1941client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun)
1942{
1943 Channel *c;
1944 int fd;
1945
1946 if (tun_mode == SSH_TUNMODE_NO)
1947 return 0;
1948
1949 if (!compat20) {
Damien Millerb3f2c9f2009-01-28 16:15:30 +11001950 error("Tunnel forwarding is not supported for protocol 1");
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001951 return -1;
1952 }
1953
1954 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1955
1956 /* Open local tunnel device */
1957 if ((fd = tun_open(local_tun, tun_mode)) == -1) {
1958 error("Tunnel device open failed.");
1959 return -1;
1960 }
1961
1962 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1963 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1964 c->datagram = 1;
1965
1966#if defined(SSH_TUN_FILTER)
1967 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1968 channel_register_filter(c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +10001969 sys_tun_outfilter, NULL, NULL);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001970#endif
1971
1972 packet_start(SSH2_MSG_CHANNEL_OPEN);
1973 packet_put_cstring("tun@openssh.com");
1974 packet_put_int(c->self);
1975 packet_put_int(c->local_window_max);
1976 packet_put_int(c->local_maxpacket);
1977 packet_put_int(tun_mode);
1978 packet_put_int(remote_tun);
1979 packet_send();
1980
1981 return 0;
1982}
1983
Damien Millerbd483e72000-04-30 10:00:53 +10001984/* XXXX move to generic input handler */
Ben Lindstrombba81212001-06-25 05:01:22 +00001985static void
Damien Miller630d6f42002-01-22 23:17:30 +11001986client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001987{
1988 Channel *c = NULL;
1989 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001990 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001991 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001992
1993 ctype = packet_get_string(&len);
1994 rchan = packet_get_int();
1995 rwindow = packet_get_int();
1996 rmaxpack = packet_get_int();
1997
Damien Millere247cc42000-05-07 12:03:14 +10001998 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001999 ctype, rchan, rwindow, rmaxpack);
2000
Damien Miller0bc1bd82000-11-13 22:57:25 +11002001 if (strcmp(ctype, "forwarded-tcpip") == 0) {
2002 c = client_request_forwarded_tcpip(ctype, rchan);
Damien Miller7acefbb2014-07-18 14:11:24 +10002003 } else if (strcmp(ctype, "forwarded-streamlocal@openssh.com") == 0) {
2004 c = client_request_forwarded_streamlocal(ctype, rchan);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002005 } else if (strcmp(ctype, "x11") == 0) {
2006 c = client_request_x11(ctype, rchan);
2007 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
2008 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10002009 }
2010/* XXX duplicate : */
2011 if (c != NULL) {
2012 debug("confirm %s", ctype);
2013 c->remote_id = rchan;
2014 c->remote_window = rwindow;
2015 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00002016 if (c->type != SSH_CHANNEL_CONNECTING) {
2017 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
2018 packet_put_int(c->remote_id);
2019 packet_put_int(c->self);
2020 packet_put_int(c->local_window);
2021 packet_put_int(c->local_maxpacket);
2022 packet_send();
2023 }
Damien Millerbd483e72000-04-30 10:00:53 +10002024 } else {
2025 debug("failure %s", ctype);
2026 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
2027 packet_put_int(rchan);
2028 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00002029 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00002030 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00002031 packet_put_cstring("");
2032 }
Damien Millerbd483e72000-04-30 10:00:53 +10002033 packet_send();
2034 }
Darren Tuckera627d422013-06-02 07:31:17 +10002035 free(ctype);
Damien Millerbd483e72000-04-30 10:00:53 +10002036}
Ben Lindstrombba81212001-06-25 05:01:22 +00002037static void
Damien Miller630d6f42002-01-22 23:17:30 +11002038client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00002039{
2040 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002041 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00002042 char *rtype;
2043
2044 id = packet_get_int();
2045 rtype = packet_get_string(NULL);
2046 reply = packet_get_char();
2047
2048 debug("client_input_channel_req: channel %d rtype %s reply %d",
2049 id, rtype, reply);
2050
Damien Miller3bbd8782004-06-18 22:23:22 +10002051 if (id == -1) {
2052 error("client_input_channel_req: request for channel -1");
2053 } else if ((c = channel_lookup(id)) == NULL) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10002054 error("client_input_channel_req: channel %d: "
2055 "unknown channel", id);
Damien Millerbab9bd42008-05-19 16:06:47 +10002056 } else if (strcmp(rtype, "eow@openssh.com") == 0) {
2057 packet_check_eom();
2058 chan_rcvd_eow(c);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002059 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10002060 exitval = packet_get_int();
Damien Millere1537f92010-01-26 13:26:22 +11002061 if (c->ctl_chan != -1) {
2062 mux_exit_message(c, exitval);
Damien Miller0e220db2004-06-15 10:34:08 +10002063 success = 1;
Darren Tucker29440822010-01-08 17:08:35 +11002064 } else if (id == session_ident) {
2065 /* Record exit value of local session */
2066 success = 1;
2067 exit_status = exitval;
2068 } else {
Damien Miller36f57eb2010-01-30 17:28:34 +11002069 /* Probably for a mux channel that has already closed */
2070 debug("%s: no sink for exit-status on channel %d",
2071 __func__, id);
Damien Miller0e220db2004-06-15 10:34:08 +10002072 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002073 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00002074 }
Damien Millerc5893782014-05-15 13:48:49 +10002075 if (reply && c != NULL && !(c->flags & CHAN_CLOSE_SENT)) {
Ben Lindstrom5b828322001-02-09 01:34:36 +00002076 packet_start(success ?
2077 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller8533c782008-12-08 09:54:40 +11002078 packet_put_int(c->remote_id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002079 packet_send();
2080 }
Darren Tuckera627d422013-06-02 07:31:17 +10002081 free(rtype);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002082}
Damien Millerc3fa4072002-01-22 23:21:58 +11002083static void
2084client_input_global_request(int type, u_int32_t seq, void *ctxt)
2085{
2086 char *rtype;
2087 int want_reply;
2088 int success = 0;
2089
2090 rtype = packet_get_string(NULL);
2091 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11002092 debug("client_input_global_request: rtype %s want_reply %d",
2093 rtype, want_reply);
Damien Millerc3fa4072002-01-22 23:21:58 +11002094 if (want_reply) {
2095 packet_start(success ?
2096 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
2097 packet_send();
2098 packet_write_wait();
2099 }
Darren Tuckera627d422013-06-02 07:31:17 +10002100 free(rtype);
Damien Millerc3fa4072002-01-22 23:21:58 +11002101}
Damien Millerbd483e72000-04-30 10:00:53 +10002102
Damien Miller0e220db2004-06-15 10:34:08 +10002103void
Darren Tuckerfc959702004-07-17 16:12:08 +10002104client_session2_setup(int id, int want_tty, int want_subsystem,
Damien Miller5771ed72008-05-19 15:35:33 +10002105 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env)
Damien Miller0e220db2004-06-15 10:34:08 +10002106{
2107 int len;
Darren Tucker5d78de62004-11-05 20:35:44 +11002108 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002109
2110 debug2("%s: id %d", __func__, id);
2111
Darren Tucker5d78de62004-11-05 20:35:44 +11002112 if ((c = channel_lookup(id)) == NULL)
2113 fatal("client_session2_setup: channel %d: unknown channel", id);
2114
Damien Miller0dac6fb2010-11-20 15:19:38 +11002115 packet_set_interactive(want_tty,
2116 options.ip_qos_interactive, options.ip_qos_bulk);
2117
Damien Miller0e220db2004-06-15 10:34:08 +10002118 if (want_tty) {
2119 struct winsize ws;
Damien Miller0e220db2004-06-15 10:34:08 +10002120
2121 /* Store window size in the packet. */
2122 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
2123 memset(&ws, 0, sizeof(ws));
2124
Damien Miller5771ed72008-05-19 15:35:33 +10002125 channel_request_start(id, "pty-req", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002126 client_expect_confirm(id, "PTY allocation", CONFIRM_TTY);
Damien Miller0e220db2004-06-15 10:34:08 +10002127 packet_put_cstring(term != NULL ? term : "");
Damien Miller71a73672006-03-26 14:04:36 +11002128 packet_put_int((u_int)ws.ws_col);
2129 packet_put_int((u_int)ws.ws_row);
2130 packet_put_int((u_int)ws.ws_xpixel);
2131 packet_put_int((u_int)ws.ws_ypixel);
Darren Tuckerdf189fb2008-06-08 12:55:32 +10002132 if (tiop == NULL)
2133 tiop = get_saved_tio();
2134 tty_make_modes(-1, tiop);
Damien Miller0e220db2004-06-15 10:34:08 +10002135 packet_send();
2136 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11002137 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10002138 }
2139
2140 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10002141 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10002142 int i, j, matched;
Damien Miller0e220db2004-06-15 10:34:08 +10002143 char *name, *val;
2144
2145 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10002146 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10002147 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10002148 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10002149 if ((val = strchr(name, '=')) == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +10002150 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002151 continue;
2152 }
2153 *val++ = '\0';
2154
2155 matched = 0;
2156 for (j = 0; j < options.num_send_env; j++) {
2157 if (match_pattern(name, options.send_env[j])) {
2158 matched = 1;
2159 break;
2160 }
2161 }
2162 if (!matched) {
2163 debug3("Ignored env %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10002164 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002165 continue;
2166 }
2167
2168 debug("Sending env %s = %s", name, val);
2169 channel_request_start(id, "env", 0);
2170 packet_put_cstring(name);
2171 packet_put_cstring(val);
2172 packet_send();
Darren Tuckera627d422013-06-02 07:31:17 +10002173 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002174 }
2175 }
2176
2177 len = buffer_len(cmd);
2178 if (len > 0) {
2179 if (len > 900)
2180 len = 900;
2181 if (want_subsystem) {
Damien Miller5771ed72008-05-19 15:35:33 +10002182 debug("Sending subsystem: %.*s",
2183 len, (u_char*)buffer_ptr(cmd));
2184 channel_request_start(id, "subsystem", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002185 client_expect_confirm(id, "subsystem", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002186 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002187 debug("Sending command: %.*s",
2188 len, (u_char*)buffer_ptr(cmd));
2189 channel_request_start(id, "exec", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002190 client_expect_confirm(id, "exec", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002191 }
2192 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
2193 packet_send();
2194 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002195 channel_request_start(id, "shell", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002196 client_expect_confirm(id, "shell", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002197 packet_send();
2198 }
2199}
2200
Ben Lindstrombba81212001-06-25 05:01:22 +00002201static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002202client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10002203{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002204 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10002205
Damien Miller1383bd82000-04-06 12:32:37 +10002206 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2207 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2208 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2209 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10002210 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10002211 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2212 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002213 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10002214 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerb84886b2008-05-19 15:05:07 +10002215 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2216 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
Damien Millerc3fa4072002-01-22 23:21:58 +11002217 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002218
2219 /* rekeying */
2220 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10002221
2222 /* global request reply messages */
2223 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2224 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10002225}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002226
Ben Lindstrombba81212001-06-25 05:01:22 +00002227static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002228client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002229{
2230 dispatch_init(NULL);
2231 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
2232 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
2233 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10002234 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2235 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
2236 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002237 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
2238 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
2239 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11002240
2241 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
Darren Tucker5dcdd212003-10-02 16:17:00 +10002242 &client_input_agent_open : &deny_input_open);
Damien Miller69b69aa2000-10-28 14:19:58 +11002243 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
2244 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002245}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002246
Ben Lindstrombba81212001-06-25 05:01:22 +00002247static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002248client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002249{
2250 client_init_dispatch_13();
2251 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
2252 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
2253}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002254
Ben Lindstromdb47f382001-07-04 05:10:27 +00002255static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002256client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002257{
Damien Miller1383bd82000-04-06 12:32:37 +10002258 if (compat20)
2259 client_init_dispatch_20();
2260 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10002261 client_init_dispatch_13();
2262 else
2263 client_init_dispatch_15();
2264}
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002265
Damien Miller6c3eec72011-05-05 14:16:22 +10002266void
2267client_stop_mux(void)
2268{
2269 if (options.control_path != NULL && muxserver_sock != -1)
2270 unlink(options.control_path);
2271 /*
Darren Tucker48bf4b02012-09-07 16:38:53 +10002272 * If we are in persist mode, or don't have a shell, signal that we
2273 * should close when all active channels are closed.
Damien Miller6c3eec72011-05-05 14:16:22 +10002274 */
Darren Tucker48bf4b02012-09-07 16:38:53 +10002275 if (options.control_persist || no_shell_flag) {
Damien Miller6c3eec72011-05-05 14:16:22 +10002276 session_closed = 1;
Damien Miller4ac99c32011-06-20 14:43:31 +10002277 setproctitle("[stopped mux]");
2278 }
Damien Miller6c3eec72011-05-05 14:16:22 +10002279}
2280
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002281/* client specific fatal cleanup */
2282void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002283cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002284{
Damien Miller21771e22011-05-15 08:45:50 +10002285 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002286 leave_non_blocking();
Damien Millerb1cbfa22008-05-19 16:00:08 +10002287 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10002288 unlink(options.control_path);
Damien Millera41ccca2010-10-07 22:07:32 +11002289 ssh_kill_proxy_command();
Darren Tucker3e33cec2003-10-02 16:12:36 +10002290 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002291}