blob: 672d06989a6d16d974f73554ab8247306135d526 [file] [log] [blame]
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001/* $OpenBSD: clientloop.c,v 1.264 2015/01/19 20:07:45 markus Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * The main loop for the interactive session (client side).
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
14 *
15 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 *
Damien Miller1383bd82000-04-06 12:32:37 +100038 * SSH2 support added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000039 * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110040 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110060 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
62#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110063
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
Damien Miller0e220db2004-06-15 10:34:08 +1000195void ssh_process_session2_setup(int, int, int, Buffer *);
196
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197/* Restores stdin to blocking mode. */
198
Ben Lindstrombba81212001-06-25 05:01:22 +0000199static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000200leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000201{
Damien Miller95def091999-11-25 00:26:21 +1100202 if (in_non_blocking_mode) {
Damien Miller03e66f62004-06-15 15:47:51 +1000203 unset_nonblock(fileno(stdin));
Damien Miller95def091999-11-25 00:26:21 +1100204 in_non_blocking_mode = 0;
Damien Miller95def091999-11-25 00:26:21 +1100205 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206}
207
Damien Miller95def091999-11-25 00:26:21 +1100208/* Puts stdin terminal in non-blocking mode. */
209
Ben Lindstrombba81212001-06-25 05:01:22 +0000210static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000211enter_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000212{
Damien Miller95def091999-11-25 00:26:21 +1100213 in_non_blocking_mode = 1;
Damien Miller232711f2004-06-15 10:35:30 +1000214 set_nonblock(fileno(stdin));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215}
216
Damien Miller5428f641999-11-25 11:54:57 +1100217/*
218 * Signal handler for the window change signal (SIGWINCH). This just sets a
219 * flag indicating that the window has changed.
220 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100221/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000222static void
Damien Miller95def091999-11-25 00:26:21 +1100223window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000224{
Damien Miller95def091999-11-25 00:26:21 +1100225 received_window_change_signal = 1;
226 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000227}
228
Damien Miller5428f641999-11-25 11:54:57 +1100229/*
230 * Signal handler for signals that cause the program to terminate. These
231 * signals must be trapped to restore terminal modes.
232 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100233/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000234static void
Damien Miller95def091999-11-25 00:26:21 +1100235signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000237 received_signal = sig;
238 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239}
240
Damien Miller5428f641999-11-25 11:54:57 +1100241/*
242 * Returns current time in seconds from Jan 1, 1970 with the maximum
243 * available resolution.
244 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000245
Ben Lindstrombba81212001-06-25 05:01:22 +0000246static double
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000247get_current_time(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248{
Damien Miller95def091999-11-25 00:26:21 +1100249 struct timeval tv;
250 gettimeofday(&tv, NULL);
251 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252}
253
Damien Millere11e1ea2010-08-03 16:04:46 +1000254/*
255 * Sets control_persist_exit_time to the absolute time when the
256 * backgrounded control master should exit due to expiry of the
257 * ControlPersist timeout. Sets it to 0 if we are not a backgrounded
258 * control master process, or if there is no ControlPersist timeout.
259 */
260static void
261set_control_persist_exit_time(void)
262{
263 if (muxserver_sock == -1 || !options.control_persist
Damien Miller6c3eec72011-05-05 14:16:22 +1000264 || options.control_persist_timeout == 0) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000265 /* not using a ControlPersist timeout */
266 control_persist_exit_time = 0;
Damien Miller6c3eec72011-05-05 14:16:22 +1000267 } else if (channel_still_open()) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000268 /* some client connections are still open */
269 if (control_persist_exit_time > 0)
270 debug2("%s: cancel scheduled exit", __func__);
271 control_persist_exit_time = 0;
272 } else if (control_persist_exit_time <= 0) {
273 /* a client connection has recently closed */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000274 control_persist_exit_time = monotime() +
Damien Millere11e1ea2010-08-03 16:04:46 +1000275 (time_t)options.control_persist_timeout;
276 debug2("%s: schedule exit in %d seconds", __func__,
277 options.control_persist_timeout);
278 }
279 /* else we are already counting down to the timeout */
280}
281
Damien Miller8d60be52012-02-11 08:18:17 +1100282#define SSH_X11_VALID_DISPLAY_CHARS ":/.-_"
283static int
284client_x11_display_valid(const char *display)
285{
286 size_t i, dlen;
287
288 dlen = strlen(display);
289 for (i = 0; i < dlen; i++) {
Damien Millerfdb23062013-11-21 13:57:15 +1100290 if (!isalnum((u_char)display[i]) &&
Damien Miller8d60be52012-02-11 08:18:17 +1100291 strchr(SSH_X11_VALID_DISPLAY_CHARS, display[i]) == NULL) {
292 debug("Invalid character '%c' in DISPLAY", display[i]);
293 return 0;
294 }
295 }
296 return 1;
297}
298
Damien Miller17e7ed02005-06-17 12:54:33 +1000299#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
300void
301client_x11_get_proto(const char *display, const char *xauth_path,
Damien Miller1ab6a512010-06-26 10:02:24 +1000302 u_int trusted, u_int timeout, char **_proto, char **_data)
Damien Miller17e7ed02005-06-17 12:54:33 +1000303{
304 char cmd[1024];
305 char line[512];
306 char xdisplay[512];
307 static char proto[512], data[512];
308 FILE *f;
309 int got_data = 0, generated = 0, do_unlink = 0, i;
310 char *xauthdir, *xauthfile;
311 struct stat st;
Damien Miller1ab6a512010-06-26 10:02:24 +1000312 u_int now;
Damien Miller17e7ed02005-06-17 12:54:33 +1000313
314 xauthdir = xauthfile = NULL;
315 *_proto = proto;
316 *_data = data;
317 proto[0] = data[0] = '\0';
318
319 if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) {
320 debug("No xauth program.");
Damien Miller8d60be52012-02-11 08:18:17 +1100321 } else if (!client_x11_display_valid(display)) {
322 logit("DISPLAY '%s' invalid, falling back to fake xauth data",
323 display);
Damien Miller17e7ed02005-06-17 12:54:33 +1000324 } else {
325 if (display == NULL) {
326 debug("x11_get_proto: DISPLAY not set");
327 return;
328 }
329 /*
330 * Handle FamilyLocal case where $DISPLAY does
331 * not match an authorization entry. For this we
332 * just try "xauth list unix:displaynum.screennum".
333 * XXX: "localhost" match to determine FamilyLocal
334 * is not perfect.
335 */
336 if (strncmp(display, "localhost:", 10) == 0) {
337 snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
338 display + 10);
339 display = xdisplay;
340 }
341 if (trusted == 0) {
342 xauthdir = xmalloc(MAXPATHLEN);
343 xauthfile = xmalloc(MAXPATHLEN);
Damien Miller2cd62932010-12-01 11:50:35 +1100344 mktemp_proto(xauthdir, MAXPATHLEN);
Damien Miller17e7ed02005-06-17 12:54:33 +1000345 if (mkdtemp(xauthdir) != NULL) {
346 do_unlink = 1;
347 snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile",
348 xauthdir);
349 snprintf(cmd, sizeof(cmd),
350 "%s -f %s generate %s " SSH_X11_PROTO
Damien Miller1ab6a512010-06-26 10:02:24 +1000351 " untrusted timeout %u 2>" _PATH_DEVNULL,
352 xauth_path, xauthfile, display, timeout);
Damien Miller17e7ed02005-06-17 12:54:33 +1000353 debug2("x11_get_proto: %s", cmd);
354 if (system(cmd) == 0)
355 generated = 1;
Damien Miller1ab6a512010-06-26 10:02:24 +1000356 if (x11_refuse_time == 0) {
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000357 now = monotime() + 1;
Damien Miller1ab6a512010-06-26 10:02:24 +1000358 if (UINT_MAX - timeout < now)
359 x11_refuse_time = UINT_MAX;
360 else
361 x11_refuse_time = now + timeout;
362 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000363 }
364 }
Darren Tucker513d13a2007-08-15 19:13:41 +1000365
366 /*
367 * When in untrusted mode, we read the cookie only if it was
368 * successfully generated as an untrusted one in the step
369 * above.
370 */
371 if (trusted || generated) {
372 snprintf(cmd, sizeof(cmd),
373 "%s %s%s list %s 2>" _PATH_DEVNULL,
374 xauth_path,
375 generated ? "-f " : "" ,
376 generated ? xauthfile : "",
377 display);
378 debug2("x11_get_proto: %s", cmd);
379 f = popen(cmd, "r");
380 if (f && fgets(line, sizeof(line), f) &&
381 sscanf(line, "%*s %511s %511s", proto, data) == 2)
382 got_data = 1;
383 if (f)
384 pclose(f);
385 } else
386 error("Warning: untrusted X11 forwarding setup failed: "
387 "xauth key data not generated");
Damien Miller17e7ed02005-06-17 12:54:33 +1000388 }
389
390 if (do_unlink) {
391 unlink(xauthfile);
392 rmdir(xauthdir);
393 }
Darren Tuckera627d422013-06-02 07:31:17 +1000394 free(xauthdir);
395 free(xauthfile);
Damien Miller17e7ed02005-06-17 12:54:33 +1000396
397 /*
398 * If we didn't get authentication data, just make up some
399 * data. The forwarding code will check the validity of the
400 * response anyway, and substitute this data. The X11
401 * server, however, will ignore this fake data and use
402 * whatever authentication mechanisms it was using otherwise
403 * for the local connection.
404 */
405 if (!got_data) {
406 u_int32_t rnd = 0;
407
408 logit("Warning: No xauth data; "
409 "using fake authentication data for X11 forwarding.");
410 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
411 for (i = 0; i < 16; i++) {
412 if (i % 4 == 0)
413 rnd = arc4random();
414 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
415 rnd & 0xff);
416 rnd >>= 8;
417 }
418 }
419}
420
Damien Miller5428f641999-11-25 11:54:57 +1100421/*
422 * This is called when the interactive is entered. This checks if there is
423 * an EOF coming on stdin. We must check this explicitly, as select() does
424 * not appear to wake up when redirecting from /dev/null.
425 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000426
Ben Lindstrombba81212001-06-25 05:01:22 +0000427static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000428client_check_initial_eof_on_stdin(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000429{
Damien Miller95def091999-11-25 00:26:21 +1100430 int len;
431 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432
Damien Miller5428f641999-11-25 11:54:57 +1100433 /*
434 * If standard input is to be "redirected from /dev/null", we simply
435 * mark that we have seen an EOF and send an EOF message to the
436 * server. Otherwise, we try to read a single character; it appears
437 * that for some files, such /dev/null, select() never wakes up for
438 * read for this descriptor, which means that we never get EOF. This
439 * way we will get the EOF if stdin comes from /dev/null or similar.
440 */
Damien Miller95def091999-11-25 00:26:21 +1100441 if (stdin_null_flag) {
442 /* Fake EOF on stdin. */
443 debug("Sending eof.");
444 stdin_eof = 1;
445 packet_start(SSH_CMSG_EOF);
446 packet_send();
447 } else {
Damien Miller95def091999-11-25 00:26:21 +1100448 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000449
Damien Miller95def091999-11-25 00:26:21 +1100450 /* Check for immediate EOF on stdin. */
451 len = read(fileno(stdin), buf, 1);
452 if (len == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000453 /*
454 * EOF. Record that we have seen it and send
455 * EOF to server.
456 */
Damien Miller95def091999-11-25 00:26:21 +1100457 debug("Sending eof.");
458 stdin_eof = 1;
459 packet_start(SSH_CMSG_EOF);
460 packet_send();
461 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100462 /*
463 * Got data. We must store the data in the buffer,
464 * and also process it as an escape character if
465 * appropriate.
466 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000467 if ((u_char) buf[0] == escape_char1)
468 escape_pending1 = 1;
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000469 else
Damien Miller95def091999-11-25 00:26:21 +1100470 buffer_append(&stdin_buffer, buf, 1);
Damien Miller95def091999-11-25 00:26:21 +1100471 }
Damien Miller95def091999-11-25 00:26:21 +1100472 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474}
475
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000476
Damien Miller5428f641999-11-25 11:54:57 +1100477/*
478 * Make packets from buffered stdin data, and buffer them for sending to the
479 * connection.
480 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000481
Ben Lindstrombba81212001-06-25 05:01:22 +0000482static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000483client_make_packets_from_stdin_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000484{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000485 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486
Damien Miller95def091999-11-25 00:26:21 +1100487 /* Send buffered stdin data to the server. */
488 while (buffer_len(&stdin_buffer) > 0 &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100489 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100490 len = buffer_len(&stdin_buffer);
491 /* Keep the packets at reasonable size. */
492 if (len > packet_get_maxsize())
493 len = packet_get_maxsize();
494 packet_start(SSH_CMSG_STDIN_DATA);
495 packet_put_string(buffer_ptr(&stdin_buffer), len);
496 packet_send();
497 buffer_consume(&stdin_buffer, len);
498 /* If we have a pending EOF, send it now. */
499 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
500 packet_start(SSH_CMSG_EOF);
501 packet_send();
502 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000503 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000504}
505
Damien Miller5428f641999-11-25 11:54:57 +1100506/*
507 * Checks if the client window has changed, and sends a packet about it to
508 * the server if so. The actual change is detected elsewhere (by a software
509 * interrupt on Unix); this just checks the flag and sends a message if
510 * appropriate.
511 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000512
Ben Lindstrombba81212001-06-25 05:01:22 +0000513static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000514client_check_window_change(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000515{
Damien Miller1383bd82000-04-06 12:32:37 +1000516 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000517
Damien Miller1383bd82000-04-06 12:32:37 +1000518 if (! received_window_change_signal)
519 return;
520 /** XXX race */
521 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000522
Damien Millerd3444942000-09-30 14:20:03 +1100523 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000524
525 if (compat20) {
Damien Miller0e220db2004-06-15 10:34:08 +1000526 channel_send_window_changes();
Damien Miller1383bd82000-04-06 12:32:37 +1000527 } else {
Damien Miller0e220db2004-06-15 10:34:08 +1000528 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
529 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000530 packet_start(SSH_CMSG_WINDOW_SIZE);
Damien Miller71a73672006-03-26 14:04:36 +1100531 packet_put_int((u_int)ws.ws_row);
532 packet_put_int((u_int)ws.ws_col);
533 packet_put_int((u_int)ws.ws_xpixel);
534 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller1383bd82000-04-06 12:32:37 +1000535 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000536 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000537}
538
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000539static int
Damien Miller509b0102003-12-17 16:33:10 +1100540client_global_request_reply(int type, u_int32_t seq, void *ctxt)
541{
Darren Tucker9f407c42008-06-13 04:50:27 +1000542 struct global_confirm *gc;
543
544 if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000545 return 0;
Darren Tucker9f407c42008-06-13 04:50:27 +1000546 if (gc->cb != NULL)
547 gc->cb(type, seq, gc->ctx);
548 if (--gc->ref_count <= 0) {
549 TAILQ_REMOVE(&global_confirms, gc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +1100550 explicit_bzero(gc, sizeof(*gc));
Darren Tuckera627d422013-06-02 07:31:17 +1000551 free(gc);
Darren Tucker9f407c42008-06-13 04:50:27 +1000552 }
553
Darren Tuckerf7288d72009-06-21 18:12:20 +1000554 packet_set_alive_timeouts(0);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000555 return 0;
Damien Miller509b0102003-12-17 16:33:10 +1100556}
557
558static void
559server_alive_check(void)
560{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000561 if (packet_inc_alive_timeouts() > options.server_alive_count_max) {
Damien Millerb73b6fd2011-01-11 17:18:56 +1100562 logit("Timeout, server %s not responding.", host);
Damien Miller985a4482006-10-24 03:02:41 +1000563 cleanup_exit(255);
564 }
Damien Miller509b0102003-12-17 16:33:10 +1100565 packet_start(SSH2_MSG_GLOBAL_REQUEST);
566 packet_put_cstring("keepalive@openssh.com");
567 packet_put_char(1); /* boolean: want reply */
568 packet_send();
Darren Tucker9f407c42008-06-13 04:50:27 +1000569 /* Insert an empty placeholder to maintain ordering */
570 client_register_global_confirm(NULL, NULL);
Damien Miller509b0102003-12-17 16:33:10 +1100571}
572
Damien Miller5428f641999-11-25 11:54:57 +1100573/*
574 * Waits until the client can do something (some data becomes available on
575 * one of the file descriptors).
576 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000577static void
Damien Miller5e953212001-01-30 09:14:00 +1100578client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000579 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000580{
Damien Miller509b0102003-12-17 16:33:10 +1100581 struct timeval tv, *tvp;
Damien Millere11e1ea2010-08-03 16:04:46 +1000582 int timeout_secs;
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000583 time_t minwait_secs = 0, server_alive_time = 0, now = monotime();
Damien Miller509b0102003-12-17 16:33:10 +1100584 int ret;
585
Damien Miller5e953212001-01-30 09:14:00 +1100586 /* Add any selections by the channel mechanism. */
Damien Millera6508752012-04-22 11:21:10 +1000587 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp,
588 &minwait_secs, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000589
Damien Miller1383bd82000-04-06 12:32:37 +1000590 if (!compat20) {
591 /* Read from the connection, unless our buffers are full. */
592 if (buffer_len(&stdout_buffer) < buffer_high &&
593 buffer_len(&stderr_buffer) < buffer_high &&
594 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100595 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000596 /*
597 * Read from stdin, unless we have seen EOF or have very much
598 * buffered data to send to the server.
599 */
600 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100601 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000602
603 /* Select stdout/stderr if have data in buffer. */
604 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100605 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000606 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100607 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000608 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000609 /* channel_prepare_select could have closed the last channel */
Damien Miller164a7f42001-10-12 11:36:09 +1000610 if (session_closed && !channel_still_open() &&
611 !packet_have_data_to_write()) {
612 /* clear mask since we did not call select() */
Damien Miller79faeff2001-11-12 11:06:32 +1100613 memset(*readsetp, 0, *nallocp);
614 memset(*writesetp, 0, *nallocp);
Damien Miller164a7f42001-10-12 11:36:09 +1000615 return;
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000616 } else {
617 FD_SET(connection_in, *readsetp);
618 }
Damien Miller1383bd82000-04-06 12:32:37 +1000619 }
620
Damien Miller95def091999-11-25 00:26:21 +1100621 /* Select server connection if have data to write to the server. */
622 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100623 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100624
Damien Miller5428f641999-11-25 11:54:57 +1100625 /*
626 * Wait for something to happen. This will suspend the process until
627 * some selected descriptor can be read, written, or has some other
Damien Millere11e1ea2010-08-03 16:04:46 +1000628 * event pending, or a timeout expires.
Damien Miller5428f641999-11-25 11:54:57 +1100629 */
Damien Miller95def091999-11-25 00:26:21 +1100630
Damien Millere11e1ea2010-08-03 16:04:46 +1000631 timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000632 if (options.server_alive_interval > 0 && compat20) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000633 timeout_secs = options.server_alive_interval;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000634 server_alive_time = now + options.server_alive_interval;
635 }
636 if (options.rekey_interval > 0 && compat20 && !rekeying)
637 timeout_secs = MIN(timeout_secs, packet_get_rekey_timeout());
Damien Millere11e1ea2010-08-03 16:04:46 +1000638 set_control_persist_exit_time();
639 if (control_persist_exit_time > 0) {
640 timeout_secs = MIN(timeout_secs,
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000641 control_persist_exit_time - now);
Damien Millere11e1ea2010-08-03 16:04:46 +1000642 if (timeout_secs < 0)
643 timeout_secs = 0;
644 }
Damien Millera6508752012-04-22 11:21:10 +1000645 if (minwait_secs != 0)
646 timeout_secs = MIN(timeout_secs, (int)minwait_secs);
Damien Millere11e1ea2010-08-03 16:04:46 +1000647 if (timeout_secs == INT_MAX)
Damien Miller509b0102003-12-17 16:33:10 +1100648 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000649 else {
Damien Millere11e1ea2010-08-03 16:04:46 +1000650 tv.tv_sec = timeout_secs;
Damien Miller509b0102003-12-17 16:33:10 +1100651 tv.tv_usec = 0;
652 tvp = &tv;
653 }
Damien Millere11e1ea2010-08-03 16:04:46 +1000654
Damien Miller509b0102003-12-17 16:33:10 +1100655 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
656 if (ret < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100657 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000658
659 /*
660 * We have to clear the select masks, because we return.
661 * We have to return, because the mainloop checks for the flags
662 * set by the signal handlers.
663 */
Damien Miller79faeff2001-11-12 11:06:32 +1100664 memset(*readsetp, 0, *nallocp);
665 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000666
Damien Miller95def091999-11-25 00:26:21 +1100667 if (errno == EINTR)
668 return;
669 /* Note: we might still have data in the buffers. */
670 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
671 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100672 quit_pending = 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000673 } else if (ret == 0) {
674 /*
675 * Timeout. Could have been either keepalive or rekeying.
676 * Keepalive we check here, rekeying is checked in clientloop.
677 */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000678 if (server_alive_time != 0 && server_alive_time <= monotime())
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000679 server_alive_check();
680 }
681
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000682}
683
Ben Lindstrombba81212001-06-25 05:01:22 +0000684static void
Damien Millerad833b32000-08-23 10:46:23 +1000685client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000686{
Damien Miller95def091999-11-25 00:26:21 +1100687 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000688 if (buffer_len(bout) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000689 atomicio(vwrite, fileno(stdout), buffer_ptr(bout),
690 buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000691 if (buffer_len(berr) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000692 atomicio(vwrite, fileno(stderr), buffer_ptr(berr),
693 buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000694
Damien Miller21771e22011-05-15 08:45:50 +1000695 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000696
Damien Miller5428f641999-11-25 11:54:57 +1100697 /*
698 * Free (and clear) the buffer to reduce the amount of data that gets
699 * written to swap.
700 */
Damien Millerad833b32000-08-23 10:46:23 +1000701 buffer_free(bin);
702 buffer_free(bout);
703 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000704
Damien Miller95def091999-11-25 00:26:21 +1100705 /* Send the suspend signal to the program itself. */
706 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000707
Darren Tucker5d78de62004-11-05 20:35:44 +1100708 /* Reset window sizes in case they have changed */
709 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000710
Damien Miller95def091999-11-25 00:26:21 +1100711 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000712 buffer_init(bin);
713 buffer_init(bout);
714 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000715
Damien Miller21771e22011-05-15 08:45:50 +1000716 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000717}
718
Ben Lindstrombba81212001-06-25 05:01:22 +0000719static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100720client_process_net_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000721{
Darren Tuckerc5564e12009-06-21 18:53:53 +1000722 int len, cont = 0;
Darren Tucker86e30a02009-08-28 11:21:06 +1000723 char buf[SSH_IOBUFSZ];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000724
Damien Miller5428f641999-11-25 11:54:57 +1100725 /*
726 * Read input from the server, and add any such data to the buffer of
727 * the packet subsystem.
728 */
Damien Miller95def091999-11-25 00:26:21 +1100729 if (FD_ISSET(connection_in, readset)) {
730 /* Read as much as possible. */
Darren Tuckerc5564e12009-06-21 18:53:53 +1000731 len = roaming_read(connection_in, buf, sizeof(buf), &cont);
732 if (len == 0 && cont == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000733 /*
734 * Received EOF. The remote host has closed the
735 * connection.
736 */
737 snprintf(buf, sizeof buf,
738 "Connection to %.300s closed by remote host.\r\n",
739 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000740 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000741 quit_pending = 1;
742 return;
Damien Miller95def091999-11-25 00:26:21 +1100743 }
Damien Miller5428f641999-11-25 11:54:57 +1100744 /*
745 * There is a kernel bug on Solaris that causes select to
746 * sometimes wake up even though there is no data available.
747 */
Damien Millerd8968ad2008-07-04 23:10:49 +1000748 if (len < 0 &&
749 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Damien Miller95def091999-11-25 00:26:21 +1100750 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000751
Damien Miller95def091999-11-25 00:26:21 +1100752 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000753 /*
754 * An error has encountered. Perhaps there is a
755 * network problem.
756 */
757 snprintf(buf, sizeof buf,
758 "Read from remote host %.300s: %.100s\r\n",
759 host, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100760 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100761 quit_pending = 1;
762 return;
763 }
764 packet_process_incoming(buf, len);
765 }
Damien Miller1383bd82000-04-06 12:32:37 +1000766}
767
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000768static void
Damien Miller5771ed72008-05-19 15:35:33 +1000769client_status_confirm(int type, Channel *c, void *ctx)
Damien Miller0e220db2004-06-15 10:34:08 +1000770{
Damien Miller5771ed72008-05-19 15:35:33 +1000771 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
772 char errmsg[256];
773 int tochan;
Darren Tuckerfc959702004-07-17 16:12:08 +1000774
Damien Miller555f3b82011-05-15 08:48:05 +1000775 /*
776 * If a TTY was explicitly requested, then a failure to allocate
777 * one is fatal.
778 */
779 if (cr->action == CONFIRM_TTY &&
780 (options.request_tty == REQUEST_TTY_FORCE ||
781 options.request_tty == REQUEST_TTY_YES))
782 cr->action = CONFIRM_CLOSE;
783
Damien Miller5771ed72008-05-19 15:35:33 +1000784 /* XXX supress on mux _client_ quietmode */
785 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
Damien Millere1537f92010-01-26 13:26:22 +1100786 c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
Damien Miller0e220db2004-06-15 10:34:08 +1000787
Damien Miller5771ed72008-05-19 15:35:33 +1000788 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
789 debug2("%s request accepted on channel %d",
790 cr->request_type, c->self);
791 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
792 if (tochan) {
793 snprintf(errmsg, sizeof(errmsg),
794 "%s request failed\r\n", cr->request_type);
795 } else {
796 snprintf(errmsg, sizeof(errmsg),
797 "%s request failed on channel %d",
798 cr->request_type, c->self);
799 }
800 /* If error occurred on primary session channel, then exit */
Damien Miller555f3b82011-05-15 08:48:05 +1000801 if (cr->action == CONFIRM_CLOSE && c->self == session_ident)
Damien Miller5771ed72008-05-19 15:35:33 +1000802 fatal("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000803 /*
804 * If error occurred on mux client, append to
805 * their stderr.
806 */
807 if (tochan) {
808 buffer_append(&c->extended, errmsg,
809 strlen(errmsg));
810 } else
Damien Miller5771ed72008-05-19 15:35:33 +1000811 error("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000812 if (cr->action == CONFIRM_TTY) {
813 /*
814 * If a TTY allocation error occurred, then arrange
815 * for the correct TTY to leave raw mode.
816 */
817 if (c->self == session_ident)
818 leave_raw_mode(0);
819 else
820 mux_tty_alloc_failed(c);
821 } else if (cr->action == CONFIRM_CLOSE) {
Damien Miller5771ed72008-05-19 15:35:33 +1000822 chan_read_failed(c);
823 chan_write_failed(c);
824 }
Damien Miller0e220db2004-06-15 10:34:08 +1000825 }
Darren Tuckera627d422013-06-02 07:31:17 +1000826 free(cr);
Damien Miller5771ed72008-05-19 15:35:33 +1000827}
Damien Miller0e220db2004-06-15 10:34:08 +1000828
Damien Miller5771ed72008-05-19 15:35:33 +1000829static void
830client_abandon_status_confirm(Channel *c, void *ctx)
831{
Darren Tuckera627d422013-06-02 07:31:17 +1000832 free(ctx);
Damien Miller5771ed72008-05-19 15:35:33 +1000833}
834
Damien Miller6d7b4372011-06-23 08:31:57 +1000835void
Damien Miller555f3b82011-05-15 08:48:05 +1000836client_expect_confirm(int id, const char *request,
837 enum confirm_action action)
Damien Miller5771ed72008-05-19 15:35:33 +1000838{
Damien Miller6c81fee2013-11-08 12:19:55 +1100839 struct channel_reply_ctx *cr = xcalloc(1, sizeof(*cr));
Damien Miller5771ed72008-05-19 15:35:33 +1000840
841 cr->request_type = request;
Damien Miller555f3b82011-05-15 08:48:05 +1000842 cr->action = action;
Damien Miller5771ed72008-05-19 15:35:33 +1000843
844 channel_register_status_confirm(id, client_status_confirm,
845 client_abandon_status_confirm, cr);
Damien Miller0e220db2004-06-15 10:34:08 +1000846}
847
Darren Tucker9f407c42008-06-13 04:50:27 +1000848void
849client_register_global_confirm(global_confirm_cb *cb, void *ctx)
850{
Damien Millerb9d3bee2008-07-16 22:40:52 +1000851 struct global_confirm *gc, *last_gc;
Darren Tucker9f407c42008-06-13 04:50:27 +1000852
853 /* Coalesce identical callbacks */
Damien Millerb9d3bee2008-07-16 22:40:52 +1000854 last_gc = TAILQ_LAST(&global_confirms, global_confirms);
855 if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
856 if (++last_gc->ref_count >= INT_MAX)
857 fatal("%s: last_gc->ref_count = %d",
858 __func__, last_gc->ref_count);
Darren Tucker9f407c42008-06-13 04:50:27 +1000859 return;
860 }
861
Damien Miller6c81fee2013-11-08 12:19:55 +1100862 gc = xcalloc(1, sizeof(*gc));
Darren Tucker9f407c42008-06-13 04:50:27 +1000863 gc->cb = cb;
864 gc->ctx = ctx;
865 gc->ref_count = 1;
866 TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
867}
868
Damien Miller0e220db2004-06-15 10:34:08 +1000869static void
Ben Lindstrom681d9322002-03-22 03:53:00 +0000870process_cmdline(void)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000871{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000872 void (*handler)(int);
Damien Miller7acefbb2014-07-18 14:11:24 +1000873 char *s, *cmd;
874 int ok, delete = 0, local = 0, remote = 0, dynamic = 0;
875 struct Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000876
Damien Miller1d2c4562014-02-04 11:18:20 +1100877 memset(&fwd, 0, sizeof(fwd));
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100878
Damien Miller21771e22011-05-15 08:45:50 +1000879 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000880 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000881 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000882 if (s == NULL)
883 goto out;
Damien Millerfdb23062013-11-21 13:57:15 +1100884 while (isspace((u_char)*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000885 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000886 if (*s == '-')
887 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000888 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000889 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000890
Darren Tucker1973c882004-05-24 10:34:36 +1000891 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000892 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000893 logit(" -L[bind_address:]port:host:hostport "
894 "Request local forward");
895 logit(" -R[bind_address:]port:host:hostport "
896 "Request remote forward");
Damien Miller0164cb82008-11-05 16:30:31 +1100897 logit(" -D[bind_address:]port "
898 "Request dynamic forward");
Damien Millerff773642011-09-22 21:39:48 +1000899 logit(" -KL[bind_address:]port "
900 "Cancel local forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000901 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000902 "Cancel remote forward");
Damien Millerff773642011-09-22 21:39:48 +1000903 logit(" -KD[bind_address:]port "
904 "Cancel dynamic forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100905 if (!options.permit_local_command)
906 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000907 logit(" !args "
908 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100909 goto out;
910 }
911
912 if (*s == '!' && options.permit_local_command) {
913 s++;
914 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000915 goto out;
916 }
917
918 if (*s == 'K') {
919 delete = 1;
920 s++;
921 }
Damien Miller0164cb82008-11-05 16:30:31 +1100922 if (*s == 'L')
923 local = 1;
924 else if (*s == 'R')
925 remote = 1;
926 else if (*s == 'D')
927 dynamic = 1;
928 else {
Damien Miller996acd22003-04-09 20:59:48 +1000929 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000930 goto out;
931 }
Damien Miller0164cb82008-11-05 16:30:31 +1100932
Damien Millerff773642011-09-22 21:39:48 +1000933 if (delete && !compat20) {
Damien Miller996acd22003-04-09 20:59:48 +1000934 logit("Not supported for SSH protocol version 1.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000935 goto out;
936 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000937
Damien Millerfdb23062013-11-21 13:57:15 +1100938 while (isspace((u_char)*++s))
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100939 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000940
Damien Millere1537f92010-01-26 13:26:22 +1100941 /* XXX update list of forwards in options */
Darren Tuckere7066df2004-05-24 10:18:05 +1000942 if (delete) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000943 /* We pass 1 for dynamicfwd to restrict to 1 or 2 fields. */
944 if (!parse_forward(&fwd, s, 1, 0)) {
945 logit("Bad forwarding close specification.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000946 goto out;
947 }
Damien Millerff773642011-09-22 21:39:48 +1000948 if (remote)
Damien Miller7acefbb2014-07-18 14:11:24 +1000949 ok = channel_request_rforward_cancel(&fwd) == 0;
Damien Millerff773642011-09-22 21:39:48 +1000950 else if (dynamic)
Damien Miller7acefbb2014-07-18 14:11:24 +1000951 ok = channel_cancel_lport_listener(&fwd,
952 0, &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000953 else
Damien Miller7acefbb2014-07-18 14:11:24 +1000954 ok = channel_cancel_lport_listener(&fwd,
955 CHANNEL_CANCEL_PORT_STATIC,
956 &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000957 if (!ok) {
958 logit("Unkown port forwarding.");
959 goto out;
960 }
961 logit("Canceled forwarding.");
Darren Tuckere7066df2004-05-24 10:18:05 +1000962 } else {
Damien Miller4bf648f2009-02-14 16:28:21 +1100963 if (!parse_forward(&fwd, s, dynamic, remote)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000964 logit("Bad forwarding specification.");
965 goto out;
966 }
Damien Miller0164cb82008-11-05 16:30:31 +1100967 if (local || dynamic) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000968 if (!channel_setup_local_fwd_listener(&fwd,
969 &options.fwd_opts)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000970 logit("Port forwarding failed.");
971 goto out;
972 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100973 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +1000974 if (channel_request_remote_forwarding(&fwd) < 0) {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000975 logit("Port forwarding failed.");
976 goto out;
977 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100978 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000979 logit("Forwarding port.");
980 }
981
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000982out:
983 signal(SIGINT, handler);
Damien Miller21771e22011-05-15 08:45:50 +1000984 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tuckera627d422013-06-02 07:31:17 +1000985 free(cmd);
986 free(fwd.listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000987 free(fwd.listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000988 free(fwd.connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000989 free(fwd.connect_path);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000990}
991
Darren Tucker92a39cf2012-09-07 11:20:20 +1000992/* reasons to suppress output of an escape command in help output */
993#define SUPPRESS_NEVER 0 /* never suppress, always show */
994#define SUPPRESS_PROTO1 1 /* don't show in protocol 1 sessions */
995#define SUPPRESS_MUXCLIENT 2 /* don't show in mux client sessions */
996#define SUPPRESS_MUXMASTER 4 /* don't show in mux master sessions */
997#define SUPPRESS_SYSLOG 8 /* don't show when logging to syslog */
998struct escape_help_text {
999 const char *cmd;
1000 const char *text;
1001 unsigned int flags;
1002};
1003static struct escape_help_text esc_txt[] = {
1004 {".", "terminate session", SUPPRESS_MUXMASTER},
1005 {".", "terminate connection (and any multiplexed sessions)",
1006 SUPPRESS_MUXCLIENT},
1007 {"B", "send a BREAK to the remote system", SUPPRESS_PROTO1},
1008 {"C", "open a command line", SUPPRESS_MUXCLIENT},
1009 {"R", "request rekey", SUPPRESS_PROTO1},
Darren Tuckerca0d0fd2012-09-07 11:22:24 +10001010 {"V/v", "decrease/increase verbosity (LogLevel)", SUPPRESS_MUXCLIENT},
Darren Tucker92a39cf2012-09-07 11:20:20 +10001011 {"^Z", "suspend ssh", SUPPRESS_MUXCLIENT},
1012 {"#", "list forwarded connections", SUPPRESS_NEVER},
1013 {"&", "background ssh (when waiting for connections to terminate)",
1014 SUPPRESS_MUXCLIENT},
1015 {"?", "this message", SUPPRESS_NEVER},
1016};
1017
1018static void
1019print_escape_help(Buffer *b, int escape_char, int protocol2, int mux_client,
1020 int using_stderr)
1021{
1022 unsigned int i, suppress_flags;
1023 char string[1024];
1024
1025 snprintf(string, sizeof string, "%c?\r\n"
1026 "Supported escape sequences:\r\n", escape_char);
1027 buffer_append(b, string, strlen(string));
1028
1029 suppress_flags = (protocol2 ? 0 : SUPPRESS_PROTO1) |
1030 (mux_client ? SUPPRESS_MUXCLIENT : 0) |
1031 (mux_client ? 0 : SUPPRESS_MUXMASTER) |
1032 (using_stderr ? 0 : SUPPRESS_SYSLOG);
1033
1034 for (i = 0; i < sizeof(esc_txt)/sizeof(esc_txt[0]); i++) {
1035 if (esc_txt[i].flags & suppress_flags)
1036 continue;
Darren Tuckerca0d0fd2012-09-07 11:22:24 +10001037 snprintf(string, sizeof string, " %c%-3s - %s\r\n",
Darren Tucker92a39cf2012-09-07 11:20:20 +10001038 escape_char, esc_txt[i].cmd, esc_txt[i].text);
1039 buffer_append(b, string, strlen(string));
1040 }
1041
1042 snprintf(string, sizeof string,
Darren Tuckerca0d0fd2012-09-07 11:22:24 +10001043 " %c%c - send the escape character by typing it twice\r\n"
Darren Tucker92a39cf2012-09-07 11:20:20 +10001044 "(Note that escapes are only recognized immediately after "
1045 "newline.)\r\n", escape_char, escape_char);
1046 buffer_append(b, string, strlen(string));
1047}
1048
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001049/*
1050 * Process the characters one by one, call with c==NULL for proto1 case.
1051 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001052static int
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001053process_escapes(Channel *c, Buffer *bin, Buffer *bout, Buffer *berr,
1054 char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001055{
1056 char string[1024];
1057 pid_t pid;
1058 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001059 u_int i;
1060 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +10001061 char *s;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001062 int *escape_pendingp, escape_char;
1063 struct escape_filter_ctx *efc;
Damien Millerad833b32000-08-23 10:46:23 +10001064
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001065 if (c == NULL) {
1066 escape_pendingp = &escape_pending1;
1067 escape_char = escape_char1;
1068 } else {
1069 if (c->filter_ctx == NULL)
1070 return 0;
1071 efc = (struct escape_filter_ctx *)c->filter_ctx;
1072 escape_pendingp = &efc->escape_pending;
1073 escape_char = efc->escape_char;
1074 }
1075
Damien Millereccb9de2005-06-17 12:59:34 +10001076 if (len <= 0)
1077 return (0);
1078
1079 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +10001080 /* Get one character at a time. */
1081 ch = buf[i];
1082
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001083 if (*escape_pendingp) {
Damien Millerad833b32000-08-23 10:46:23 +10001084 /* We have previously seen an escape character. */
1085 /* Clear the flag now. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001086 *escape_pendingp = 0;
Damien Millerad833b32000-08-23 10:46:23 +10001087
1088 /* Process the escaped character. */
1089 switch (ch) {
1090 case '.':
1091 /* Terminate the connection. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001092 snprintf(string, sizeof string, "%c.\r\n",
1093 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001094 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001095
Damien Millere1537f92010-01-26 13:26:22 +11001096 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001097 chan_read_failed(c);
1098 chan_write_failed(c);
Darren Tuckerea8342c2013-06-06 08:11:40 +10001099 if (c->detach_user)
1100 c->detach_user(c->self, NULL);
Damien Miller36187092013-06-10 13:07:11 +10001101 c->type = SSH_CHANNEL_ABANDONED;
1102 buffer_clear(&c->input);
1103 chan_ibuf_empty(c);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001104 return 0;
1105 } else
1106 quit_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001107 return -1;
1108
1109 case 'Z' - 64:
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001110 /* XXX support this for mux clients */
Damien Millere1537f92010-01-26 13:26:22 +11001111 if (c && c->ctl_chan != -1) {
Darren Tuckerf111d402012-09-07 11:21:42 +10001112 char b[16];
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001113 noescape:
Darren Tuckerf111d402012-09-07 11:21:42 +10001114 if (ch == 'Z' - 64)
1115 snprintf(b, sizeof b, "^Z");
1116 else
1117 snprintf(b, sizeof b, "%c", ch);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001118 snprintf(string, sizeof string,
Darren Tuckerf111d402012-09-07 11:21:42 +10001119 "%c%s escape not available to "
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001120 "multiplexed sessions\r\n",
Darren Tuckerf111d402012-09-07 11:21:42 +10001121 escape_char, b);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001122 buffer_append(berr, string,
1123 strlen(string));
1124 continue;
1125 }
Darren Tucker4d5cd332008-06-13 04:51:14 +10001126 /* Suspend the program. Inform the user */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001127 snprintf(string, sizeof string,
1128 "%c^Z [suspend ssh]\r\n", escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001129 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001130
1131 /* Restore terminal modes and suspend. */
1132 client_suspend_self(bin, bout, berr);
1133
1134 /* We have been continued. */
1135 continue;
1136
Damien Miller54c45982003-05-15 10:20:13 +10001137 case 'B':
1138 if (compat20) {
1139 snprintf(string, sizeof string,
1140 "%cB\r\n", escape_char);
1141 buffer_append(berr, string,
1142 strlen(string));
Damien Miller70182522013-09-14 09:49:19 +10001143 channel_request_start(c->self,
Damien Miller54c45982003-05-15 10:20:13 +10001144 "break", 0);
1145 packet_put_int(1000);
1146 packet_send();
1147 }
1148 continue;
1149
Ben Lindstromf28f6342001-04-04 02:03:04 +00001150 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001151 if (compat20) {
1152 if (datafellows & SSH_BUG_NOREKEY)
Darren Tucker4d5cd332008-06-13 04:51:14 +10001153 logit("Server does not "
1154 "support re-keying");
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001155 else
1156 need_rekeying = 1;
1157 }
Ben Lindstromf28f6342001-04-04 02:03:04 +00001158 continue;
1159
Darren Tucker50a48d02012-09-06 21:25:37 +10001160 case 'V':
1161 /* FALLTHROUGH */
1162 case 'v':
1163 if (c && c->ctl_chan != -1)
1164 goto noescape;
1165 if (!log_is_on_stderr()) {
1166 snprintf(string, sizeof string,
1167 "%c%c [Logging to syslog]\r\n",
1168 escape_char, ch);
1169 buffer_append(berr, string,
1170 strlen(string));
1171 continue;
1172 }
1173 if (ch == 'V' && options.log_level >
1174 SYSLOG_LEVEL_QUIET)
1175 log_change_level(--options.log_level);
1176 if (ch == 'v' && options.log_level <
1177 SYSLOG_LEVEL_DEBUG3)
1178 log_change_level(++options.log_level);
1179 snprintf(string, sizeof string,
1180 "%c%c [LogLevel %s]\r\n", escape_char, ch,
1181 log_level_name(options.log_level));
1182 buffer_append(berr, string, strlen(string));
1183 continue;
1184
Damien Millerad833b32000-08-23 10:46:23 +10001185 case '&':
Damien Millere1537f92010-01-26 13:26:22 +11001186 if (c && c->ctl_chan != -1)
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001187 goto noescape;
Damien Millerad833b32000-08-23 10:46:23 +10001188 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001189 * Detach the program (continue to serve
1190 * connections, but put in background and no
1191 * more new connections).
Damien Millerad833b32000-08-23 10:46:23 +10001192 */
Damien Miller96507ef2001-11-12 10:52:25 +11001193 /* Restore tty modes. */
Damien Miller21771e22011-05-15 08:45:50 +10001194 leave_raw_mode(
1195 options.request_tty == REQUEST_TTY_FORCE);
Damien Miller96507ef2001-11-12 10:52:25 +11001196
1197 /* Stop listening for new connections. */
1198 channel_stop_listening();
1199
1200 snprintf(string, sizeof string,
1201 "%c& [backgrounded]\n", escape_char);
1202 buffer_append(berr, string, strlen(string));
1203
1204 /* Fork into background. */
1205 pid = fork();
1206 if (pid < 0) {
1207 error("fork: %.100s", strerror(errno));
1208 continue;
1209 }
1210 if (pid != 0) { /* This is the parent. */
1211 /* The parent just exits. */
1212 exit(0);
1213 }
1214 /* The child continues serving connections. */
1215 if (compat20) {
1216 buffer_append(bin, "\004", 1);
1217 /* fake EOF on stdin */
1218 return -1;
1219 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +10001220 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001221 * Sending SSH_CMSG_EOF alone does not
1222 * always appear to be enough. So we
1223 * try to send an EOF character first.
Damien Millerad833b32000-08-23 10:46:23 +10001224 */
1225 packet_start(SSH_CMSG_STDIN_DATA);
1226 packet_put_string("\004", 1);
1227 packet_send();
1228 /* Close stdin. */
1229 stdin_eof = 1;
1230 if (buffer_len(bin) == 0) {
1231 packet_start(SSH_CMSG_EOF);
1232 packet_send();
1233 }
1234 }
Damien Miller96507ef2001-11-12 10:52:25 +11001235 continue;
Damien Millerad833b32000-08-23 10:46:23 +10001236
1237 case '?':
Darren Tucker92a39cf2012-09-07 11:20:20 +10001238 print_escape_help(berr, escape_char, compat20,
1239 (c && c->ctl_chan != -1),
1240 log_is_on_stderr());
Damien Millerad833b32000-08-23 10:46:23 +10001241 continue;
1242
1243 case '#':
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001244 snprintf(string, sizeof string, "%c#\r\n",
1245 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001246 buffer_append(berr, string, strlen(string));
1247 s = channel_open_message();
1248 buffer_append(berr, s, strlen(s));
Darren Tuckera627d422013-06-02 07:31:17 +10001249 free(s);
Damien Millerad833b32000-08-23 10:46:23 +10001250 continue;
1251
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001252 case 'C':
Damien Millere1537f92010-01-26 13:26:22 +11001253 if (c && c->ctl_chan != -1)
Damien Miller586b0052008-12-09 14:11:32 +11001254 goto noescape;
Ben Lindstrom681d9322002-03-22 03:53:00 +00001255 process_cmdline();
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001256 continue;
1257
Damien Millerad833b32000-08-23 10:46:23 +10001258 default:
1259 if (ch != escape_char) {
1260 buffer_put_char(bin, escape_char);
1261 bytes++;
1262 }
1263 /* Escaped characters fall through here */
1264 break;
1265 }
1266 } else {
1267 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001268 * The previous character was not an escape char.
1269 * Check if this is an escape.
Damien Millerad833b32000-08-23 10:46:23 +10001270 */
1271 if (last_was_cr && ch == escape_char) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001272 /*
1273 * It is. Set the flag and continue to
1274 * next character.
1275 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001276 *escape_pendingp = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001277 continue;
1278 }
1279 }
1280
1281 /*
1282 * Normal character. Record whether it was a newline,
1283 * and append it to the buffer.
1284 */
1285 last_was_cr = (ch == '\r' || ch == '\n');
1286 buffer_put_char(bin, ch);
1287 bytes++;
1288 }
1289 return bytes;
1290}
1291
Ben Lindstrombba81212001-06-25 05:01:22 +00001292static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001293client_process_input(fd_set *readset)
Damien Miller1383bd82000-04-06 12:32:37 +10001294{
Damien Miller166fca82000-04-20 07:42:21 +10001295 int len;
Darren Tucker86e30a02009-08-28 11:21:06 +10001296 char buf[SSH_IOBUFSZ];
Damien Miller1383bd82000-04-06 12:32:37 +10001297
Damien Miller95def091999-11-25 00:26:21 +11001298 /* Read input from stdin. */
1299 if (FD_ISSET(fileno(stdin), readset)) {
1300 /* Read as much as possible. */
1301 len = read(fileno(stdin), buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +10001302 if (len < 0 &&
1303 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001304 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +11001305 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001306 /*
1307 * Received EOF or error. They are treated
1308 * similarly, except that an error message is printed
1309 * if it was an error condition.
1310 */
Damien Miller95def091999-11-25 00:26:21 +11001311 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001312 snprintf(buf, sizeof buf, "read: %.100s\r\n",
1313 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001314 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001315 }
1316 /* Mark that we have seen EOF. */
1317 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +11001318 /*
1319 * Send an EOF message to the server unless there is
1320 * data in the buffer. If there is data in the
1321 * buffer, no message will be sent now. Code
1322 * elsewhere will send the EOF when the buffer
1323 * becomes empty if stdin_eof is set.
1324 */
Damien Miller95def091999-11-25 00:26:21 +11001325 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001326 packet_start(SSH_CMSG_EOF);
1327 packet_send();
Damien Miller95def091999-11-25 00:26:21 +11001328 }
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001329 } else if (escape_char1 == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +11001330 /*
1331 * Normal successful read, and no escape character.
1332 * Just append the data to buffer.
1333 */
Damien Miller95def091999-11-25 00:26:21 +11001334 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +11001335 } else {
Damien Miller5428f641999-11-25 11:54:57 +11001336 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001337 * Normal, successful read. But we have an escape
1338 * character and have to process the characters one
1339 * by one.
Damien Miller5428f641999-11-25 11:54:57 +11001340 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001341 if (process_escapes(NULL, &stdin_buffer,
1342 &stdout_buffer, &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +10001343 return;
Damien Miller95def091999-11-25 00:26:21 +11001344 }
1345 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001346}
1347
Ben Lindstrombba81212001-06-25 05:01:22 +00001348static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001349client_process_output(fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001350{
Damien Miller95def091999-11-25 00:26:21 +11001351 int len;
1352 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001353
Damien Miller95def091999-11-25 00:26:21 +11001354 /* Write buffered output to stdout. */
1355 if (FD_ISSET(fileno(stdout), writeset)) {
1356 /* Write as much data as possible. */
1357 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001358 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001359 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001360 if (errno == EINTR || errno == EAGAIN ||
1361 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001362 len = 0;
1363 else {
Damien Miller5428f641999-11-25 11:54:57 +11001364 /*
1365 * An error or EOF was encountered. Put an
1366 * error message to stderr buffer.
1367 */
Darren Tucker4d5cd332008-06-13 04:51:14 +10001368 snprintf(buf, sizeof buf,
1369 "write stdout: %.50s\r\n", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001370 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001371 quit_pending = 1;
1372 return;
1373 }
1374 }
1375 /* Consume printed data from the buffer. */
1376 buffer_consume(&stdout_buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001377 }
Damien Miller95def091999-11-25 00:26:21 +11001378 /* Write buffered output to stderr. */
1379 if (FD_ISSET(fileno(stderr), writeset)) {
1380 /* Write as much data as possible. */
1381 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001382 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001383 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001384 if (errno == EINTR || errno == EAGAIN ||
1385 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001386 len = 0;
1387 else {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001388 /*
1389 * EOF or error, but can't even print
1390 * error message.
1391 */
Damien Miller95def091999-11-25 00:26:21 +11001392 quit_pending = 1;
1393 return;
1394 }
1395 }
1396 /* Consume printed characters from the buffer. */
1397 buffer_consume(&stderr_buffer, len);
1398 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001399}
1400
Damien Miller5428f641999-11-25 11:54:57 +11001401/*
Damien Millerb38eff82000-04-01 11:09:21 +10001402 * Get packets from the connection input buffer, and process them as long as
1403 * there are packets available.
1404 *
1405 * Any unknown packets received during the actual
1406 * session cause the session to terminate. This is
1407 * intended to make debugging easier since no
1408 * confirmations are sent. Any compatible protocol
1409 * extensions must be negotiated during the
1410 * preparatory phase.
1411 */
1412
Ben Lindstrombba81212001-06-25 05:01:22 +00001413static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001414client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001415{
Darren Tucker4d5cd332008-06-13 04:51:14 +10001416 dispatch_run(DISPATCH_NONBLOCK, &quit_pending,
markus@openbsd.org091c3022015-01-19 19:52:16 +00001417 compat20 ? active_state->kex : NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001418}
1419
Damien Millerad833b32000-08-23 10:46:23 +10001420/* scan buf[] for '~' before sending data to the peer */
1421
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001422/* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1423void *
1424client_new_escape_filter_ctx(int escape_char)
1425{
1426 struct escape_filter_ctx *ret;
1427
Damien Miller6c81fee2013-11-08 12:19:55 +11001428 ret = xcalloc(1, sizeof(*ret));
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001429 ret->escape_pending = 0;
1430 ret->escape_char = escape_char;
1431 return (void *)ret;
1432}
1433
Darren Tucker84c56f52008-06-13 04:55:46 +10001434/* Free the escape filter context on channel free */
1435void
1436client_filter_cleanup(int cid, void *ctx)
1437{
Darren Tuckera627d422013-06-02 07:31:17 +10001438 free(ctx);
Darren Tucker84c56f52008-06-13 04:55:46 +10001439}
1440
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001441int
1442client_simple_escape_filter(Channel *c, char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001443{
Damien Miller5771ed72008-05-19 15:35:33 +10001444 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1445 return 0;
1446
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001447 return process_escapes(c, &c->input, &c->output, &c->extended,
1448 buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001449}
1450
Ben Lindstrombba81212001-06-25 05:01:22 +00001451static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001452client_channel_closed(int id, void *arg)
1453{
Damien Miller3ec27592001-10-12 11:35:04 +10001454 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001455 session_closed = 1;
Damien Miller21771e22011-05-15 08:45:50 +10001456 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001457}
1458
Damien Millerb38eff82000-04-01 11:09:21 +10001459/*
Damien Miller5428f641999-11-25 11:54:57 +11001460 * Implements the interactive session with the server. This is called after
1461 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001462 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1463 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001464 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001465
Damien Miller4af51302000-04-16 11:18:38 +10001466int
Damien Millerad833b32000-08-23 10:46:23 +10001467client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001468{
Damien Miller5e953212001-01-30 09:14:00 +11001469 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001470 double start_time, total_time;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001471 int max_fd = 0, max_fd2 = 0, len, rekeying = 0;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001472 u_int64_t ibytes, obytes;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001473 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001474 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001475
Damien Miller95def091999-11-25 00:26:21 +11001476 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001477
Damien Miller95def091999-11-25 00:26:21 +11001478 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001479
Damien Miller95def091999-11-25 00:26:21 +11001480 /* Initialize variables. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001481 escape_pending1 = 0;
Damien Miller95def091999-11-25 00:26:21 +11001482 last_was_cr = 1;
1483 exit_status = -1;
1484 stdin_eof = 0;
1485 buffer_high = 64 * 1024;
1486 connection_in = packet_get_connection_in();
1487 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +11001488 max_fd = MAX(connection_in, connection_out);
1489
1490 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +00001491 /* enable nonblocking unless tty */
1492 if (!isatty(fileno(stdin)))
1493 set_nonblock(fileno(stdin));
1494 if (!isatty(fileno(stdout)))
1495 set_nonblock(fileno(stdout));
1496 if (!isatty(fileno(stderr)))
1497 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +11001498 max_fd = MAX(max_fd, fileno(stdin));
1499 max_fd = MAX(max_fd, fileno(stdout));
1500 max_fd = MAX(max_fd, fileno(stderr));
1501 }
Damien Miller95def091999-11-25 00:26:21 +11001502 quit_pending = 0;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001503 escape_char1 = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001504
Damien Miller95def091999-11-25 00:26:21 +11001505 /* Initialize buffers. */
1506 buffer_init(&stdin_buffer);
1507 buffer_init(&stdout_buffer);
1508 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001509
Damien Millerb38eff82000-04-01 11:09:21 +10001510 client_init_dispatch();
1511
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001512 /*
1513 * Set signal handlers, (e.g. to restore non-blocking mode)
1514 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1515 */
Darren Tucker07336da2004-11-05 20:02:16 +11001516 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1517 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001518 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1519 signal(SIGINT, signal_handler);
1520 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1521 signal(SIGQUIT, signal_handler);
1522 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1523 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001524 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001525
Damien Miller95def091999-11-25 00:26:21 +11001526 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001527 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001528
Ben Lindstrom5b828322001-02-09 01:34:36 +00001529 if (compat20) {
1530 session_ident = ssh2_chan_id;
Damien Miller6c3eec72011-05-05 14:16:22 +10001531 if (session_ident != -1) {
1532 if (escape_char_arg != SSH_ESCAPECHAR_NONE) {
1533 channel_register_filter(session_ident,
1534 client_simple_escape_filter, NULL,
1535 client_filter_cleanup,
1536 client_new_escape_filter_ctx(
1537 escape_char_arg));
1538 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001539 channel_register_cleanup(session_ident,
Damien Miller39eda6e2005-11-05 14:52:50 +11001540 client_channel_closed, 0);
Damien Miller6c3eec72011-05-05 14:16:22 +10001541 }
Ben Lindstrom5b828322001-02-09 01:34:36 +00001542 } else {
1543 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +10001544 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001545 }
Damien Millerad833b32000-08-23 10:46:23 +10001546
Damien Miller95def091999-11-25 00:26:21 +11001547 /* Main loop of the client for the interactive session mode. */
1548 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001549
Damien Miller5428f641999-11-25 11:54:57 +11001550 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001551 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001552
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001553 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +10001554 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001555
markus@openbsd.org091c3022015-01-19 19:52:16 +00001556 rekeying = (active_state->kex != NULL && !active_state->kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +10001557
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001558 if (rekeying) {
1559 debug("rekeying in progress");
1560 } else {
1561 /*
1562 * Make packets of buffered stdin data, and buffer
1563 * them for sending to the server.
1564 */
1565 if (!compat20)
1566 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001567
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001568 /*
1569 * Make packets from buffered channel data, and
1570 * enqueue them for sending to the server.
1571 */
1572 if (packet_not_very_much_data_to_write())
1573 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001574
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001575 /*
1576 * Check if the window size has changed, and buffer a
1577 * message about it to the server if so.
1578 */
1579 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001580
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001581 if (quit_pending)
1582 break;
1583 }
Damien Miller5428f641999-11-25 11:54:57 +11001584 /*
1585 * Wait until we have something to do (something becomes
1586 * available on one of the descriptors).
1587 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001588 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001589 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001590 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001591
Damien Miller95def091999-11-25 00:26:21 +11001592 if (quit_pending)
1593 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001594
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001595 /* Do channel operations unless rekeying in progress. */
1596 if (!rekeying) {
1597 channel_after_select(readset, writeset);
Damien Millera5539d22003-04-09 20:50:06 +10001598 if (need_rekeying || packet_need_rekeying()) {
1599 debug("need rekeying");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001600 active_state->kex->done = 0;
1601 kex_send_kexinit(active_state->kex);
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001602 need_rekeying = 0;
1603 }
1604 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001605
Damien Miller1383bd82000-04-06 12:32:37 +10001606 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001607 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001608
Damien Miller1383bd82000-04-06 12:32:37 +10001609 if (quit_pending)
1610 break;
1611
1612 if (!compat20) {
1613 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +11001614 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +10001615 /*
1616 * Process output to stdout and stderr. Output to
1617 * the connection is processed elsewhere (above).
1618 */
Damien Miller5e953212001-01-30 09:14:00 +11001619 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +10001620 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001621
Darren Tuckere32cf432010-01-08 16:51:40 +11001622 if (session_resumed) {
1623 connection_in = packet_get_connection_in();
1624 connection_out = packet_get_connection_out();
1625 max_fd = MAX(max_fd, connection_out);
1626 max_fd = MAX(max_fd, connection_in);
1627 session_resumed = 0;
1628 }
1629
Darren Tucker4d5cd332008-06-13 04:51:14 +10001630 /*
1631 * Send as much buffered packet data as possible to the
1632 * sender.
1633 */
Damien Miller5e953212001-01-30 09:14:00 +11001634 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001635 packet_write_poll();
Damien Millere11e1ea2010-08-03 16:04:46 +10001636
1637 /*
1638 * If we are a backgrounded control master, and the
1639 * timeout has expired without any active client
1640 * connections, then quit.
1641 */
1642 if (control_persist_exit_time > 0) {
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001643 if (monotime() >= control_persist_exit_time) {
Damien Millere11e1ea2010-08-03 16:04:46 +10001644 debug("ControlPersist timeout expired");
1645 break;
1646 }
1647 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001648 }
Darren Tuckera627d422013-06-02 07:31:17 +10001649 free(readset);
1650 free(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001651
Damien Miller95def091999-11-25 00:26:21 +11001652 /* Terminate the session. */
1653
1654 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001655 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001656
Darren Tuckerde0c0252009-07-06 07:17:35 +10001657 if (compat20) {
1658 packet_start(SSH2_MSG_DISCONNECT);
1659 packet_put_int(SSH2_DISCONNECT_BY_APPLICATION);
1660 packet_put_cstring("disconnected by user");
Damien Miller8ddc71c2010-03-22 05:54:02 +11001661 packet_put_cstring(""); /* language tag */
Darren Tuckerde0c0252009-07-06 07:17:35 +10001662 packet_send();
1663 packet_write_wait();
1664 }
Darren Tucker12b4a652009-06-21 18:14:48 +10001665
Ben Lindstrom601e4362001-06-21 03:19:23 +00001666 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +11001667
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001668 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001669 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001670
1671 /* restore blocking io */
1672 if (!isatty(fileno(stdin)))
1673 unset_nonblock(fileno(stdin));
1674 if (!isatty(fileno(stdout)))
1675 unset_nonblock(fileno(stdout));
1676 if (!isatty(fileno(stderr)))
1677 unset_nonblock(fileno(stderr));
1678
Damien Millerd6965512003-12-17 16:31:53 +11001679 /*
1680 * If there was no shell or command requested, there will be no remote
1681 * exit status to be returned. In that case, clear error code if the
1682 * connection was deliberately terminated at this end.
1683 */
1684 if (no_shell_flag && received_signal == SIGTERM) {
1685 received_signal = 0;
1686 exit_status = 0;
1687 }
1688
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001689 if (received_signal)
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001690 fatal("Killed by signal %d.", (int) received_signal);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001691
1692 /*
1693 * In interactive mode (with pseudo tty) display a message indicating
1694 * that the connection has been closed.
1695 */
1696 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001697 snprintf(buf, sizeof buf,
1698 "Connection to %.64s closed.\r\n", host);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001699 buffer_append(&stderr_buffer, buf, strlen(buf));
1700 }
1701
Damien Miller95def091999-11-25 00:26:21 +11001702 /* Output any buffered data for stdout. */
Damien Millercfd6e4f2011-01-16 23:18:33 +11001703 if (buffer_len(&stdout_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001704 len = atomicio(vwrite, fileno(stdout),
1705 buffer_ptr(&stdout_buffer), buffer_len(&stdout_buffer));
Damien Millercfd6e4f2011-01-16 23:18:33 +11001706 if (len < 0 || (u_int)len != buffer_len(&stdout_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001707 error("Write failed flushing stdout buffer.");
Damien Millercfd6e4f2011-01-16 23:18:33 +11001708 else
1709 buffer_consume(&stdout_buffer, len);
Damien Miller95def091999-11-25 00:26:21 +11001710 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001711
Damien Miller95def091999-11-25 00:26:21 +11001712 /* Output any buffered data for stderr. */
Damien Millercfd6e4f2011-01-16 23:18:33 +11001713 if (buffer_len(&stderr_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001714 len = atomicio(vwrite, fileno(stderr),
1715 buffer_ptr(&stderr_buffer), buffer_len(&stderr_buffer));
Damien Millercfd6e4f2011-01-16 23:18:33 +11001716 if (len < 0 || (u_int)len != buffer_len(&stderr_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001717 error("Write failed flushing stderr buffer.");
Damien Millercfd6e4f2011-01-16 23:18:33 +11001718 else
1719 buffer_consume(&stderr_buffer, len);
Damien Miller95def091999-11-25 00:26:21 +11001720 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001721
Damien Miller95def091999-11-25 00:26:21 +11001722 /* Clear and free any buffers. */
1723 memset(buf, 0, sizeof(buf));
1724 buffer_free(&stdin_buffer);
1725 buffer_free(&stdout_buffer);
1726 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001727
Damien Miller95def091999-11-25 00:26:21 +11001728 /* Report bytes transferred, and transfer rates. */
1729 total_time = get_current_time() - start_time;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001730 packet_get_bytes(&ibytes, &obytes);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001731 verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
Damien Miller821de0a2011-01-11 17:20:29 +11001732 (unsigned long long)obytes, (unsigned long long)ibytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001733 if (total_time > 0)
Damien Millerb61f3fc2008-07-11 17:36:48 +10001734 verbose("Bytes per second: sent %.1f, received %.1f",
1735 obytes / total_time, ibytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001736 /* Return the exit status of the program. */
1737 debug("Exit status %d", exit_status);
1738 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001739}
Damien Millerb38eff82000-04-01 11:09:21 +10001740
1741/*********/
1742
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001743static int
Damien Miller630d6f42002-01-22 23:17:30 +11001744client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001745{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001746 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001747 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001748 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001749 buffer_append(&stdout_buffer, data, data_len);
Damien Millera5103f42014-02-04 11:20:14 +11001750 explicit_bzero(data, data_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001751 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001752 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001753}
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001754static int
Damien Miller630d6f42002-01-22 23:17:30 +11001755client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001756{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001757 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001758 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001759 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001760 buffer_append(&stderr_buffer, data, data_len);
Damien Millera5103f42014-02-04 11:20:14 +11001761 explicit_bzero(data, data_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001762 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001763 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001764}
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001765static int
Damien Miller630d6f42002-01-22 23:17:30 +11001766client_input_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001767{
Damien Millerb38eff82000-04-01 11:09:21 +10001768 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001769 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001770 /* Acknowledge the exit. */
1771 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1772 packet_send();
1773 /*
1774 * Must wait for packet to be sent since we are
1775 * exiting the loop.
1776 */
1777 packet_write_wait();
1778 /* Flag that we want to exit. */
1779 quit_pending = 1;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001780 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001781}
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001782static int
Darren Tucker5dcdd212003-10-02 16:17:00 +10001783client_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();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001825 return 0;
Darren Tucker5dcdd212003-10-02 16:17:00 +10001826}
Damien Millerb38eff82000-04-01 11:09:21 +10001827
Ben Lindstrombba81212001-06-25 05:01:22 +00001828static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001829client_request_forwarded_tcpip(const char *request_type, int rchan)
1830{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001831 Channel *c = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001832 char *listen_address, *originator_address;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001833 u_short listen_port, originator_port;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001834
1835 /* Get rest of the packet */
1836 listen_address = packet_get_string(NULL);
1837 listen_port = packet_get_int();
1838 originator_address = packet_get_string(NULL);
1839 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001840 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001841
Damien Miller7acefbb2014-07-18 14:11:24 +10001842 debug("%s: listen %s port %d, originator %s port %d", __func__,
1843 listen_address, listen_port, originator_address, originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001844
Damien Miller4b3ed642014-07-02 15:29:40 +10001845 c = channel_connect_by_listen_address(listen_address, listen_port,
Damien Millerbd740252008-05-19 15:37:09 +10001846 "forwarded-tcpip", originator_address);
1847
Darren Tuckera627d422013-06-02 07:31:17 +10001848 free(originator_address);
1849 free(listen_address);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001850 return c;
1851}
1852
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001853static Channel *
Damien Miller7acefbb2014-07-18 14:11:24 +10001854client_request_forwarded_streamlocal(const char *request_type, int rchan)
1855{
1856 Channel *c = NULL;
1857 char *listen_path;
1858
1859 /* Get the remote path. */
1860 listen_path = packet_get_string(NULL);
1861 /* XXX: Skip reserved field for now. */
1862 if (packet_get_string_ptr(NULL) == NULL)
1863 fatal("%s: packet_get_string_ptr failed", __func__);
1864 packet_check_eom();
1865
1866 debug("%s: %s", __func__, listen_path);
1867
1868 c = channel_connect_by_listen_path(listen_path,
1869 "forwarded-streamlocal@openssh.com", "forwarded-streamlocal");
1870 free(listen_path);
1871 return c;
1872}
1873
1874static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001875client_request_x11(const char *request_type, int rchan)
1876{
1877 Channel *c = NULL;
1878 char *originator;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001879 u_short originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001880 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001881
1882 if (!options.forward_x11) {
1883 error("Warning: ssh server tried X11 forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001884 error("Warning: this is probably a break-in attempt by a "
1885 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001886 return NULL;
1887 }
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001888 if (x11_refuse_time != 0 && monotime() >= x11_refuse_time) {
Damien Miller1ab6a512010-06-26 10:02:24 +10001889 verbose("Rejected X11 connection after ForwardX11Timeout "
1890 "expired");
1891 return NULL;
1892 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001893 originator = packet_get_string(NULL);
1894 if (datafellows & SSH_BUG_X11FWD) {
1895 debug2("buggy server: x11 request w/o originator_port");
1896 originator_port = 0;
1897 } else {
1898 originator_port = packet_get_int();
1899 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001900 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001901 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001902 debug("client_request_x11: request from %s %d", originator,
1903 originator_port);
Darren Tuckera627d422013-06-02 07:31:17 +10001904 free(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001905 sock = x11_connect_display();
1906 if (sock < 0)
1907 return NULL;
1908 c = channel_new("x11",
1909 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001910 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001911 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001912 return c;
1913}
1914
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001915static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001916client_request_agent(const char *request_type, int rchan)
1917{
1918 Channel *c = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001919 int r, sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001920
1921 if (!options.forward_agent) {
1922 error("Warning: ssh server tried agent forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001923 error("Warning: this is probably a break-in attempt by a "
1924 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001925 return NULL;
1926 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001927 if ((r = ssh_get_authentication_socket(&sock)) != 0) {
1928 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1929 debug("%s: ssh_get_authentication_socket: %s",
1930 __func__, ssh_err(r));
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001931 return NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001932 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001933 c = channel_new("authentication agent connection",
1934 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001935 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001936 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001937 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001938 return c;
1939}
1940
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001941int
1942client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun)
1943{
1944 Channel *c;
1945 int fd;
1946
1947 if (tun_mode == SSH_TUNMODE_NO)
1948 return 0;
1949
1950 if (!compat20) {
Damien Millerb3f2c9f2009-01-28 16:15:30 +11001951 error("Tunnel forwarding is not supported for protocol 1");
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001952 return -1;
1953 }
1954
1955 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1956
1957 /* Open local tunnel device */
1958 if ((fd = tun_open(local_tun, tun_mode)) == -1) {
1959 error("Tunnel device open failed.");
1960 return -1;
1961 }
1962
1963 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1964 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1965 c->datagram = 1;
1966
1967#if defined(SSH_TUN_FILTER)
1968 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1969 channel_register_filter(c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +10001970 sys_tun_outfilter, NULL, NULL);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001971#endif
1972
1973 packet_start(SSH2_MSG_CHANNEL_OPEN);
1974 packet_put_cstring("tun@openssh.com");
1975 packet_put_int(c->self);
1976 packet_put_int(c->local_window_max);
1977 packet_put_int(c->local_maxpacket);
1978 packet_put_int(tun_mode);
1979 packet_put_int(remote_tun);
1980 packet_send();
1981
1982 return 0;
1983}
1984
Damien Millerbd483e72000-04-30 10:00:53 +10001985/* XXXX move to generic input handler */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001986static int
Damien Miller630d6f42002-01-22 23:17:30 +11001987client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001988{
1989 Channel *c = NULL;
1990 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001991 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001992 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001993
1994 ctype = packet_get_string(&len);
1995 rchan = packet_get_int();
1996 rwindow = packet_get_int();
1997 rmaxpack = packet_get_int();
1998
Damien Millere247cc42000-05-07 12:03:14 +10001999 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10002000 ctype, rchan, rwindow, rmaxpack);
2001
Damien Miller0bc1bd82000-11-13 22:57:25 +11002002 if (strcmp(ctype, "forwarded-tcpip") == 0) {
2003 c = client_request_forwarded_tcpip(ctype, rchan);
Damien Miller7acefbb2014-07-18 14:11:24 +10002004 } else if (strcmp(ctype, "forwarded-streamlocal@openssh.com") == 0) {
2005 c = client_request_forwarded_streamlocal(ctype, rchan);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002006 } else if (strcmp(ctype, "x11") == 0) {
2007 c = client_request_x11(ctype, rchan);
2008 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
2009 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10002010 }
2011/* XXX duplicate : */
2012 if (c != NULL) {
2013 debug("confirm %s", ctype);
2014 c->remote_id = rchan;
2015 c->remote_window = rwindow;
2016 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00002017 if (c->type != SSH_CHANNEL_CONNECTING) {
2018 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
2019 packet_put_int(c->remote_id);
2020 packet_put_int(c->self);
2021 packet_put_int(c->local_window);
2022 packet_put_int(c->local_maxpacket);
2023 packet_send();
2024 }
Damien Millerbd483e72000-04-30 10:00:53 +10002025 } else {
2026 debug("failure %s", ctype);
2027 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
2028 packet_put_int(rchan);
2029 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00002030 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00002031 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00002032 packet_put_cstring("");
2033 }
Damien Millerbd483e72000-04-30 10:00:53 +10002034 packet_send();
2035 }
Darren Tuckera627d422013-06-02 07:31:17 +10002036 free(ctype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002037 return 0;
Damien Millerbd483e72000-04-30 10:00:53 +10002038}
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002039static int
Damien Miller630d6f42002-01-22 23:17:30 +11002040client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00002041{
2042 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002043 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00002044 char *rtype;
2045
2046 id = packet_get_int();
2047 rtype = packet_get_string(NULL);
2048 reply = packet_get_char();
2049
2050 debug("client_input_channel_req: channel %d rtype %s reply %d",
2051 id, rtype, reply);
2052
Damien Miller3bbd8782004-06-18 22:23:22 +10002053 if (id == -1) {
2054 error("client_input_channel_req: request for channel -1");
2055 } else if ((c = channel_lookup(id)) == NULL) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10002056 error("client_input_channel_req: channel %d: "
2057 "unknown channel", id);
Damien Millerbab9bd42008-05-19 16:06:47 +10002058 } else if (strcmp(rtype, "eow@openssh.com") == 0) {
2059 packet_check_eom();
2060 chan_rcvd_eow(c);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002061 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10002062 exitval = packet_get_int();
Damien Millere1537f92010-01-26 13:26:22 +11002063 if (c->ctl_chan != -1) {
2064 mux_exit_message(c, exitval);
Damien Miller0e220db2004-06-15 10:34:08 +10002065 success = 1;
Darren Tucker29440822010-01-08 17:08:35 +11002066 } else if (id == session_ident) {
2067 /* Record exit value of local session */
2068 success = 1;
2069 exit_status = exitval;
2070 } else {
Damien Miller36f57eb2010-01-30 17:28:34 +11002071 /* Probably for a mux channel that has already closed */
2072 debug("%s: no sink for exit-status on channel %d",
2073 __func__, id);
Damien Miller0e220db2004-06-15 10:34:08 +10002074 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002075 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00002076 }
Damien Millerc5893782014-05-15 13:48:49 +10002077 if (reply && c != NULL && !(c->flags & CHAN_CLOSE_SENT)) {
Ben Lindstrom5b828322001-02-09 01:34:36 +00002078 packet_start(success ?
2079 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller8533c782008-12-08 09:54:40 +11002080 packet_put_int(c->remote_id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002081 packet_send();
2082 }
Darren Tuckera627d422013-06-02 07:31:17 +10002083 free(rtype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002084 return 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00002085}
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002086static int
Damien Millerc3fa4072002-01-22 23:21:58 +11002087client_input_global_request(int type, u_int32_t seq, void *ctxt)
2088{
2089 char *rtype;
2090 int want_reply;
2091 int success = 0;
2092
2093 rtype = packet_get_string(NULL);
2094 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11002095 debug("client_input_global_request: rtype %s want_reply %d",
2096 rtype, want_reply);
Damien Millerc3fa4072002-01-22 23:21:58 +11002097 if (want_reply) {
2098 packet_start(success ?
2099 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
2100 packet_send();
2101 packet_write_wait();
2102 }
Darren Tuckera627d422013-06-02 07:31:17 +10002103 free(rtype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002104 return 0;
Damien Millerc3fa4072002-01-22 23:21:58 +11002105}
Damien Millerbd483e72000-04-30 10:00:53 +10002106
Damien Miller0e220db2004-06-15 10:34:08 +10002107void
Darren Tuckerfc959702004-07-17 16:12:08 +10002108client_session2_setup(int id, int want_tty, int want_subsystem,
Damien Miller5771ed72008-05-19 15:35:33 +10002109 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env)
Damien Miller0e220db2004-06-15 10:34:08 +10002110{
2111 int len;
Darren Tucker5d78de62004-11-05 20:35:44 +11002112 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002113
2114 debug2("%s: id %d", __func__, id);
2115
Darren Tucker5d78de62004-11-05 20:35:44 +11002116 if ((c = channel_lookup(id)) == NULL)
2117 fatal("client_session2_setup: channel %d: unknown channel", id);
2118
Damien Miller0dac6fb2010-11-20 15:19:38 +11002119 packet_set_interactive(want_tty,
2120 options.ip_qos_interactive, options.ip_qos_bulk);
2121
Damien Miller0e220db2004-06-15 10:34:08 +10002122 if (want_tty) {
2123 struct winsize ws;
Damien Miller0e220db2004-06-15 10:34:08 +10002124
2125 /* Store window size in the packet. */
2126 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
2127 memset(&ws, 0, sizeof(ws));
2128
Damien Miller5771ed72008-05-19 15:35:33 +10002129 channel_request_start(id, "pty-req", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002130 client_expect_confirm(id, "PTY allocation", CONFIRM_TTY);
Damien Miller0e220db2004-06-15 10:34:08 +10002131 packet_put_cstring(term != NULL ? term : "");
Damien Miller71a73672006-03-26 14:04:36 +11002132 packet_put_int((u_int)ws.ws_col);
2133 packet_put_int((u_int)ws.ws_row);
2134 packet_put_int((u_int)ws.ws_xpixel);
2135 packet_put_int((u_int)ws.ws_ypixel);
Darren Tuckerdf189fb2008-06-08 12:55:32 +10002136 if (tiop == NULL)
2137 tiop = get_saved_tio();
2138 tty_make_modes(-1, tiop);
Damien Miller0e220db2004-06-15 10:34:08 +10002139 packet_send();
2140 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11002141 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10002142 }
2143
2144 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10002145 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10002146 int i, j, matched;
Damien Miller0e220db2004-06-15 10:34:08 +10002147 char *name, *val;
2148
2149 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10002150 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10002151 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10002152 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10002153 if ((val = strchr(name, '=')) == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +10002154 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002155 continue;
2156 }
2157 *val++ = '\0';
2158
2159 matched = 0;
2160 for (j = 0; j < options.num_send_env; j++) {
2161 if (match_pattern(name, options.send_env[j])) {
2162 matched = 1;
2163 break;
2164 }
2165 }
2166 if (!matched) {
2167 debug3("Ignored env %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10002168 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002169 continue;
2170 }
2171
2172 debug("Sending env %s = %s", name, val);
2173 channel_request_start(id, "env", 0);
2174 packet_put_cstring(name);
2175 packet_put_cstring(val);
2176 packet_send();
Darren Tuckera627d422013-06-02 07:31:17 +10002177 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002178 }
2179 }
2180
2181 len = buffer_len(cmd);
2182 if (len > 0) {
2183 if (len > 900)
2184 len = 900;
2185 if (want_subsystem) {
Damien Miller5771ed72008-05-19 15:35:33 +10002186 debug("Sending subsystem: %.*s",
2187 len, (u_char*)buffer_ptr(cmd));
2188 channel_request_start(id, "subsystem", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002189 client_expect_confirm(id, "subsystem", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002190 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002191 debug("Sending command: %.*s",
2192 len, (u_char*)buffer_ptr(cmd));
2193 channel_request_start(id, "exec", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002194 client_expect_confirm(id, "exec", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002195 }
2196 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
2197 packet_send();
2198 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002199 channel_request_start(id, "shell", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002200 client_expect_confirm(id, "shell", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002201 packet_send();
2202 }
2203}
2204
Ben Lindstrombba81212001-06-25 05:01:22 +00002205static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002206client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10002207{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002208 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10002209
Damien Miller1383bd82000-04-06 12:32:37 +10002210 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2211 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2212 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2213 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10002214 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10002215 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2216 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002217 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10002218 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerb84886b2008-05-19 15:05:07 +10002219 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2220 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
Damien Millerc3fa4072002-01-22 23:21:58 +11002221 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002222
2223 /* rekeying */
2224 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10002225
2226 /* global request reply messages */
2227 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2228 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10002229}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002230
Ben Lindstrombba81212001-06-25 05:01:22 +00002231static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002232client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002233{
2234 dispatch_init(NULL);
2235 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
2236 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
2237 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10002238 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2239 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
2240 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002241 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
2242 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
2243 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11002244
2245 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
Darren Tucker5dcdd212003-10-02 16:17:00 +10002246 &client_input_agent_open : &deny_input_open);
Damien Miller69b69aa2000-10-28 14:19:58 +11002247 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
2248 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002249}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002250
Ben Lindstrombba81212001-06-25 05:01:22 +00002251static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002252client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002253{
2254 client_init_dispatch_13();
2255 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
2256 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
2257}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002258
Ben Lindstromdb47f382001-07-04 05:10:27 +00002259static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002260client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002261{
Damien Miller1383bd82000-04-06 12:32:37 +10002262 if (compat20)
2263 client_init_dispatch_20();
2264 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10002265 client_init_dispatch_13();
2266 else
2267 client_init_dispatch_15();
2268}
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002269
Damien Miller6c3eec72011-05-05 14:16:22 +10002270void
2271client_stop_mux(void)
2272{
2273 if (options.control_path != NULL && muxserver_sock != -1)
2274 unlink(options.control_path);
2275 /*
Darren Tucker48bf4b02012-09-07 16:38:53 +10002276 * If we are in persist mode, or don't have a shell, signal that we
2277 * should close when all active channels are closed.
Damien Miller6c3eec72011-05-05 14:16:22 +10002278 */
Darren Tucker48bf4b02012-09-07 16:38:53 +10002279 if (options.control_persist || no_shell_flag) {
Damien Miller6c3eec72011-05-05 14:16:22 +10002280 session_closed = 1;
Damien Miller4ac99c32011-06-20 14:43:31 +10002281 setproctitle("[stopped mux]");
2282 }
Damien Miller6c3eec72011-05-05 14:16:22 +10002283}
2284
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002285/* client specific fatal cleanup */
2286void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002287cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002288{
Damien Miller21771e22011-05-15 08:45:50 +10002289 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002290 leave_non_blocking();
Damien Millerb1cbfa22008-05-19 16:00:08 +10002291 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10002292 unlink(options.control_path);
Damien Millera41ccca2010-10-07 22:07:32 +11002293 ssh_kill_proxy_command();
Darren Tucker3e33cec2003-10-02 16:12:36 +10002294 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002295}