blob: 45a55e0ec7dba0ba39a92473abad996e3d0007d2 [file] [log] [blame]
Damien Miller70182522013-09-14 09:49:19 +10001/* $OpenBSD: clientloop.c,v 1.254 2013/09/12 01:41:12 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * The main loop for the interactive session (client side).
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
14 *
15 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 *
Damien Miller1383bd82000-04-06 12:32:37 +100038 * SSH2 support added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000039 * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110040 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110060 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
62#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110063
Damien Millerf17883e2006-03-15 11:45:54 +110064#include <sys/types.h>
Damien Miller9aec9192006-08-05 10:57:45 +100065#include <sys/ioctl.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100066#include <sys/param.h>
Damien Millerf17883e2006-03-15 11:45:54 +110067#ifdef HAVE_SYS_STAT_H
68# include <sys/stat.h>
69#endif
Damien Miller9aec9192006-08-05 10:57:45 +100070#ifdef HAVE_SYS_TIME_H
71# include <sys/time.h>
72#endif
Damien Millere3b60b52006-07-10 21:08:03 +100073#include <sys/socket.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110074
Damien Millerc7b06362006-03-15 11:53:45 +110075#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100076#include <errno.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110077#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110078#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110079#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110080#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100081#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100082#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100083#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100084#include <string.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110085#include <termios.h>
Damien Millerd7834352006-08-05 12:39:39 +100086#include <pwd.h>
Damien Millere6b3b612006-07-24 14:01:23 +100087#include <unistd.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
Damien Millerb84886b2008-05-19 15:05:07 +100089#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100090#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000092#include "ssh1.h"
93#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094#include "packet.h"
95#include "buffer.h"
Damien Millerb38eff82000-04-01 11:09:21 +100096#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000097#include "channels.h"
Damien Millerb38eff82000-04-01 11:09:21 +100098#include "dispatch.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110099#include "key.h"
Damien Millerd7834352006-08-05 12:39:39 +1000100#include "cipher.h"
Ben Lindstromf28f6342001-04-04 02:03:04 +0000101#include "kex.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000102#include "log.h"
103#include "readconf.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000104#include "clientloop.h"
Damien Milleraeb31d62005-12-13 19:29:36 +1100105#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000106#include "authfd.h"
107#include "atomicio.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +1000108#include "sshpty.h"
Ben Lindstrom302ea6f2001-04-16 02:01:25 +0000109#include "misc.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"
Damien Miller69b69aa2000-10-28 14:19:58 +1100113
114/* import options */
115extern Options options;
116
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117/* Flag indicating that stdin should be redirected from /dev/null. */
118extern int stdin_null_flag;
119
Damien Millerd6965512003-12-17 16:31:53 +1100120/* Flag indicating that no shell has been requested */
121extern int no_shell_flag;
122
Damien Miller0e220db2004-06-15 10:34:08 +1000123/* Control socket */
Damien Millere1537f92010-01-26 13:26:22 +1100124extern int muxserver_sock; /* XXX use mux_client_cleanup() instead */
Damien Miller0e220db2004-06-15 10:34:08 +1000125
Damien Miller5428f641999-11-25 11:54:57 +1100126/*
127 * Name of the host we are connecting to. This is the name given on the
128 * command line, or the HostName specified for the user-supplied name in a
129 * configuration file.
130 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131extern char *host;
132
Damien Miller5428f641999-11-25 11:54:57 +1100133/*
134 * Flag to indicate that we have received a window change signal which has
135 * not yet been processed. This will cause a message indicating the new
136 * window size to be sent to the server a little later. This is volatile
137 * because this is updated in a signal handler.
138 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000139static volatile sig_atomic_t received_window_change_signal = 0;
140static volatile sig_atomic_t received_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141
Damien Miller788f2122005-11-05 15:14:59 +1100142/* Flag indicating whether the user's terminal is in non-blocking mode. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143static int in_non_blocking_mode = 0;
144
Damien Millere11e1ea2010-08-03 16:04:46 +1000145/* Time when backgrounded control master using ControlPersist should exit */
146static time_t control_persist_exit_time = 0;
147
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148/* Common data for the client loop code. */
Damien Millere1537f92010-01-26 13:26:22 +1100149volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000150static int escape_char1; /* Escape character. (proto1 only) */
151static int escape_pending1; /* Last character was an escape (proto1 only) */
Damien Miller95def091999-11-25 00:26:21 +1100152static int last_was_cr; /* Last character was a newline. */
Darren Tucker4d5cd332008-06-13 04:51:14 +1000153static int exit_status; /* Used to store the command exit status. */
154static int stdin_eof; /* EOF has been encountered on stderr. */
Damien Miller95def091999-11-25 00:26:21 +1100155static Buffer stdin_buffer; /* Buffer for stdin data. */
156static Buffer stdout_buffer; /* Buffer for stdout data. */
157static Buffer stderr_buffer; /* Buffer for stderr data. */
Damien Miller1ab6a512010-06-26 10:02:24 +1000158static u_int buffer_high; /* Soft max buffer size. */
Damien Miller95def091999-11-25 00:26:21 +1100159static int connection_in; /* Connection to server (input). */
160static int connection_out; /* Connection to server (output). */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000161static int need_rekeying; /* Set to non-zero if rekeying is requested. */
Damien Miller1ab6a512010-06-26 10:02:24 +1000162static int session_closed; /* In SSH2: login session closed. */
163static int x11_refuse_time; /* If >0, refuse x11 opens after this time. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164
Ben Lindstrombba81212001-06-25 05:01:22 +0000165static void client_init_dispatch(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000166int session_ident = -1;
167
Darren Tuckere32cf432010-01-08 16:51:40 +1100168int session_resumed = 0;
169
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000170/* Track escape per proto2 channel */
171struct escape_filter_ctx {
172 int escape_pending;
173 int escape_char;
174};
175
176/* Context for channel confirmation replies */
Damien Miller5771ed72008-05-19 15:35:33 +1000177struct channel_reply_ctx {
178 const char *request_type;
Damien Miller555f3b82011-05-15 08:48:05 +1000179 int id;
180 enum confirm_action action;
Damien Miller5771ed72008-05-19 15:35:33 +1000181};
182
Darren Tucker9f407c42008-06-13 04:50:27 +1000183/* Global request success/failure callbacks */
184struct global_confirm {
185 TAILQ_ENTRY(global_confirm) entry;
186 global_confirm_cb *cb;
187 void *ctx;
188 int ref_count;
189};
190TAILQ_HEAD(global_confirms, global_confirm);
191static struct global_confirms global_confirms =
192 TAILQ_HEAD_INITIALIZER(global_confirms);
193
Ben Lindstromf28f6342001-04-04 02:03:04 +0000194/*XXX*/
195extern Kex *xxx_kex;
196
Damien Miller0e220db2004-06-15 10:34:08 +1000197void ssh_process_session2_setup(int, int, int, Buffer *);
198
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199/* Restores stdin to blocking mode. */
200
Ben Lindstrombba81212001-06-25 05:01:22 +0000201static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000202leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203{
Damien Miller95def091999-11-25 00:26:21 +1100204 if (in_non_blocking_mode) {
Damien Miller03e66f62004-06-15 15:47:51 +1000205 unset_nonblock(fileno(stdin));
Damien Miller95def091999-11-25 00:26:21 +1100206 in_non_blocking_mode = 0;
Damien Miller95def091999-11-25 00:26:21 +1100207 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000208}
209
Damien Miller95def091999-11-25 00:26:21 +1100210/* Puts stdin terminal in non-blocking mode. */
211
Ben Lindstrombba81212001-06-25 05:01:22 +0000212static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000213enter_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214{
Damien Miller95def091999-11-25 00:26:21 +1100215 in_non_blocking_mode = 1;
Damien Miller232711f2004-06-15 10:35:30 +1000216 set_nonblock(fileno(stdin));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217}
218
Damien Miller5428f641999-11-25 11:54:57 +1100219/*
220 * Signal handler for the window change signal (SIGWINCH). This just sets a
221 * flag indicating that the window has changed.
222 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100223/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000224static void
Damien Miller95def091999-11-25 00:26:21 +1100225window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000226{
Damien Miller95def091999-11-25 00:26:21 +1100227 received_window_change_signal = 1;
228 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000229}
230
Damien Miller5428f641999-11-25 11:54:57 +1100231/*
232 * Signal handler for signals that cause the program to terminate. These
233 * signals must be trapped to restore terminal modes.
234 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100235/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000236static void
Damien Miller95def091999-11-25 00:26:21 +1100237signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000239 received_signal = sig;
240 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241}
242
Damien Miller5428f641999-11-25 11:54:57 +1100243/*
244 * Returns current time in seconds from Jan 1, 1970 with the maximum
245 * available resolution.
246 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247
Ben Lindstrombba81212001-06-25 05:01:22 +0000248static double
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000249get_current_time(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250{
Damien Miller95def091999-11-25 00:26:21 +1100251 struct timeval tv;
252 gettimeofday(&tv, NULL);
253 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254}
255
Damien Millere11e1ea2010-08-03 16:04:46 +1000256/*
257 * Sets control_persist_exit_time to the absolute time when the
258 * backgrounded control master should exit due to expiry of the
259 * ControlPersist timeout. Sets it to 0 if we are not a backgrounded
260 * control master process, or if there is no ControlPersist timeout.
261 */
262static void
263set_control_persist_exit_time(void)
264{
265 if (muxserver_sock == -1 || !options.control_persist
Damien Miller6c3eec72011-05-05 14:16:22 +1000266 || options.control_persist_timeout == 0) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000267 /* not using a ControlPersist timeout */
268 control_persist_exit_time = 0;
Damien Miller6c3eec72011-05-05 14:16:22 +1000269 } else if (channel_still_open()) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000270 /* some client connections are still open */
271 if (control_persist_exit_time > 0)
272 debug2("%s: cancel scheduled exit", __func__);
273 control_persist_exit_time = 0;
274 } else if (control_persist_exit_time <= 0) {
275 /* a client connection has recently closed */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000276 control_persist_exit_time = monotime() +
Damien Millere11e1ea2010-08-03 16:04:46 +1000277 (time_t)options.control_persist_timeout;
278 debug2("%s: schedule exit in %d seconds", __func__,
279 options.control_persist_timeout);
280 }
281 /* else we are already counting down to the timeout */
282}
283
Damien Miller8d60be52012-02-11 08:18:17 +1100284#define SSH_X11_VALID_DISPLAY_CHARS ":/.-_"
285static int
286client_x11_display_valid(const char *display)
287{
288 size_t i, dlen;
289
290 dlen = strlen(display);
291 for (i = 0; i < dlen; i++) {
292 if (!isalnum(display[i]) &&
293 strchr(SSH_X11_VALID_DISPLAY_CHARS, display[i]) == NULL) {
294 debug("Invalid character '%c' in DISPLAY", display[i]);
295 return 0;
296 }
297 }
298 return 1;
299}
300
Damien Miller17e7ed02005-06-17 12:54:33 +1000301#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
302void
303client_x11_get_proto(const char *display, const char *xauth_path,
Damien Miller1ab6a512010-06-26 10:02:24 +1000304 u_int trusted, u_int timeout, char **_proto, char **_data)
Damien Miller17e7ed02005-06-17 12:54:33 +1000305{
306 char cmd[1024];
307 char line[512];
308 char xdisplay[512];
309 static char proto[512], data[512];
310 FILE *f;
311 int got_data = 0, generated = 0, do_unlink = 0, i;
312 char *xauthdir, *xauthfile;
313 struct stat st;
Damien Miller1ab6a512010-06-26 10:02:24 +1000314 u_int now;
Damien Miller17e7ed02005-06-17 12:54:33 +1000315
316 xauthdir = xauthfile = NULL;
317 *_proto = proto;
318 *_data = data;
319 proto[0] = data[0] = '\0';
320
321 if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) {
322 debug("No xauth program.");
Damien Miller8d60be52012-02-11 08:18:17 +1100323 } else if (!client_x11_display_valid(display)) {
324 logit("DISPLAY '%s' invalid, falling back to fake xauth data",
325 display);
Damien Miller17e7ed02005-06-17 12:54:33 +1000326 } else {
327 if (display == NULL) {
328 debug("x11_get_proto: DISPLAY not set");
329 return;
330 }
331 /*
332 * Handle FamilyLocal case where $DISPLAY does
333 * not match an authorization entry. For this we
334 * just try "xauth list unix:displaynum.screennum".
335 * XXX: "localhost" match to determine FamilyLocal
336 * is not perfect.
337 */
338 if (strncmp(display, "localhost:", 10) == 0) {
339 snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
340 display + 10);
341 display = xdisplay;
342 }
343 if (trusted == 0) {
344 xauthdir = xmalloc(MAXPATHLEN);
345 xauthfile = xmalloc(MAXPATHLEN);
Damien Miller2cd62932010-12-01 11:50:35 +1100346 mktemp_proto(xauthdir, MAXPATHLEN);
Damien Miller17e7ed02005-06-17 12:54:33 +1000347 if (mkdtemp(xauthdir) != NULL) {
348 do_unlink = 1;
349 snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile",
350 xauthdir);
351 snprintf(cmd, sizeof(cmd),
352 "%s -f %s generate %s " SSH_X11_PROTO
Damien Miller1ab6a512010-06-26 10:02:24 +1000353 " untrusted timeout %u 2>" _PATH_DEVNULL,
354 xauth_path, xauthfile, display, timeout);
Damien Miller17e7ed02005-06-17 12:54:33 +1000355 debug2("x11_get_proto: %s", cmd);
356 if (system(cmd) == 0)
357 generated = 1;
Damien Miller1ab6a512010-06-26 10:02:24 +1000358 if (x11_refuse_time == 0) {
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000359 now = monotime() + 1;
Damien Miller1ab6a512010-06-26 10:02:24 +1000360 if (UINT_MAX - timeout < now)
361 x11_refuse_time = UINT_MAX;
362 else
363 x11_refuse_time = now + timeout;
364 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000365 }
366 }
Darren Tucker513d13a2007-08-15 19:13:41 +1000367
368 /*
369 * When in untrusted mode, we read the cookie only if it was
370 * successfully generated as an untrusted one in the step
371 * above.
372 */
373 if (trusted || generated) {
374 snprintf(cmd, sizeof(cmd),
375 "%s %s%s list %s 2>" _PATH_DEVNULL,
376 xauth_path,
377 generated ? "-f " : "" ,
378 generated ? xauthfile : "",
379 display);
380 debug2("x11_get_proto: %s", cmd);
381 f = popen(cmd, "r");
382 if (f && fgets(line, sizeof(line), f) &&
383 sscanf(line, "%*s %511s %511s", proto, data) == 2)
384 got_data = 1;
385 if (f)
386 pclose(f);
387 } else
388 error("Warning: untrusted X11 forwarding setup failed: "
389 "xauth key data not generated");
Damien Miller17e7ed02005-06-17 12:54:33 +1000390 }
391
392 if (do_unlink) {
393 unlink(xauthfile);
394 rmdir(xauthdir);
395 }
Darren Tuckera627d422013-06-02 07:31:17 +1000396 free(xauthdir);
397 free(xauthfile);
Damien Miller17e7ed02005-06-17 12:54:33 +1000398
399 /*
400 * If we didn't get authentication data, just make up some
401 * data. The forwarding code will check the validity of the
402 * response anyway, and substitute this data. The X11
403 * server, however, will ignore this fake data and use
404 * whatever authentication mechanisms it was using otherwise
405 * for the local connection.
406 */
407 if (!got_data) {
408 u_int32_t rnd = 0;
409
410 logit("Warning: No xauth data; "
411 "using fake authentication data for X11 forwarding.");
412 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
413 for (i = 0; i < 16; i++) {
414 if (i % 4 == 0)
415 rnd = arc4random();
416 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
417 rnd & 0xff);
418 rnd >>= 8;
419 }
420 }
421}
422
Damien Miller5428f641999-11-25 11:54:57 +1100423/*
424 * This is called when the interactive is entered. This checks if there is
425 * an EOF coming on stdin. We must check this explicitly, as select() does
426 * not appear to wake up when redirecting from /dev/null.
427 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428
Ben Lindstrombba81212001-06-25 05:01:22 +0000429static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000430client_check_initial_eof_on_stdin(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431{
Damien Miller95def091999-11-25 00:26:21 +1100432 int len;
433 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434
Damien Miller5428f641999-11-25 11:54:57 +1100435 /*
436 * If standard input is to be "redirected from /dev/null", we simply
437 * mark that we have seen an EOF and send an EOF message to the
438 * server. Otherwise, we try to read a single character; it appears
439 * that for some files, such /dev/null, select() never wakes up for
440 * read for this descriptor, which means that we never get EOF. This
441 * way we will get the EOF if stdin comes from /dev/null or similar.
442 */
Damien Miller95def091999-11-25 00:26:21 +1100443 if (stdin_null_flag) {
444 /* Fake EOF on stdin. */
445 debug("Sending eof.");
446 stdin_eof = 1;
447 packet_start(SSH_CMSG_EOF);
448 packet_send();
449 } else {
Damien Miller95def091999-11-25 00:26:21 +1100450 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000451
Damien Miller95def091999-11-25 00:26:21 +1100452 /* Check for immediate EOF on stdin. */
453 len = read(fileno(stdin), buf, 1);
454 if (len == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000455 /*
456 * EOF. Record that we have seen it and send
457 * EOF to server.
458 */
Damien Miller95def091999-11-25 00:26:21 +1100459 debug("Sending eof.");
460 stdin_eof = 1;
461 packet_start(SSH_CMSG_EOF);
462 packet_send();
463 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100464 /*
465 * Got data. We must store the data in the buffer,
466 * and also process it as an escape character if
467 * appropriate.
468 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000469 if ((u_char) buf[0] == escape_char1)
470 escape_pending1 = 1;
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000471 else
Damien Miller95def091999-11-25 00:26:21 +1100472 buffer_append(&stdin_buffer, buf, 1);
Damien Miller95def091999-11-25 00:26:21 +1100473 }
Damien Miller95def091999-11-25 00:26:21 +1100474 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000476}
477
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000478
Damien Miller5428f641999-11-25 11:54:57 +1100479/*
480 * Make packets from buffered stdin data, and buffer them for sending to the
481 * connection.
482 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483
Ben Lindstrombba81212001-06-25 05:01:22 +0000484static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000485client_make_packets_from_stdin_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000487 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000488
Damien Miller95def091999-11-25 00:26:21 +1100489 /* Send buffered stdin data to the server. */
490 while (buffer_len(&stdin_buffer) > 0 &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100491 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100492 len = buffer_len(&stdin_buffer);
493 /* Keep the packets at reasonable size. */
494 if (len > packet_get_maxsize())
495 len = packet_get_maxsize();
496 packet_start(SSH_CMSG_STDIN_DATA);
497 packet_put_string(buffer_ptr(&stdin_buffer), len);
498 packet_send();
499 buffer_consume(&stdin_buffer, len);
500 /* If we have a pending EOF, send it now. */
501 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
502 packet_start(SSH_CMSG_EOF);
503 packet_send();
504 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000505 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000506}
507
Damien Miller5428f641999-11-25 11:54:57 +1100508/*
509 * Checks if the client window has changed, and sends a packet about it to
510 * the server if so. The actual change is detected elsewhere (by a software
511 * interrupt on Unix); this just checks the flag and sends a message if
512 * appropriate.
513 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000514
Ben Lindstrombba81212001-06-25 05:01:22 +0000515static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000516client_check_window_change(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000517{
Damien Miller1383bd82000-04-06 12:32:37 +1000518 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519
Damien Miller1383bd82000-04-06 12:32:37 +1000520 if (! received_window_change_signal)
521 return;
522 /** XXX race */
523 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000524
Damien Millerd3444942000-09-30 14:20:03 +1100525 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000526
527 if (compat20) {
Damien Miller0e220db2004-06-15 10:34:08 +1000528 channel_send_window_changes();
Damien Miller1383bd82000-04-06 12:32:37 +1000529 } else {
Damien Miller0e220db2004-06-15 10:34:08 +1000530 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
531 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000532 packet_start(SSH_CMSG_WINDOW_SIZE);
Damien Miller71a73672006-03-26 14:04:36 +1100533 packet_put_int((u_int)ws.ws_row);
534 packet_put_int((u_int)ws.ws_col);
535 packet_put_int((u_int)ws.ws_xpixel);
536 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller1383bd82000-04-06 12:32:37 +1000537 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000538 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000539}
540
Damien Miller509b0102003-12-17 16:33:10 +1100541static void
542client_global_request_reply(int type, u_int32_t seq, void *ctxt)
543{
Darren Tucker9f407c42008-06-13 04:50:27 +1000544 struct global_confirm *gc;
545
546 if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
547 return;
548 if (gc->cb != NULL)
549 gc->cb(type, seq, gc->ctx);
550 if (--gc->ref_count <= 0) {
551 TAILQ_REMOVE(&global_confirms, gc, entry);
552 bzero(gc, sizeof(*gc));
Darren Tuckera627d422013-06-02 07:31:17 +1000553 free(gc);
Darren Tucker9f407c42008-06-13 04:50:27 +1000554 }
555
Darren Tuckerf7288d72009-06-21 18:12:20 +1000556 packet_set_alive_timeouts(0);
Damien Miller509b0102003-12-17 16:33:10 +1100557}
558
559static void
560server_alive_check(void)
561{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000562 if (packet_inc_alive_timeouts() > options.server_alive_count_max) {
Damien Millerb73b6fd2011-01-11 17:18:56 +1100563 logit("Timeout, server %s not responding.", host);
Damien Miller985a4482006-10-24 03:02:41 +1000564 cleanup_exit(255);
565 }
Damien Miller509b0102003-12-17 16:33:10 +1100566 packet_start(SSH2_MSG_GLOBAL_REQUEST);
567 packet_put_cstring("keepalive@openssh.com");
568 packet_put_char(1); /* boolean: want reply */
569 packet_send();
Darren Tucker9f407c42008-06-13 04:50:27 +1000570 /* Insert an empty placeholder to maintain ordering */
571 client_register_global_confirm(NULL, NULL);
Damien Miller509b0102003-12-17 16:33:10 +1100572}
573
Damien Miller5428f641999-11-25 11:54:57 +1100574/*
575 * Waits until the client can do something (some data becomes available on
576 * one of the file descriptors).
577 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000578static void
Damien Miller5e953212001-01-30 09:14:00 +1100579client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000580 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000581{
Damien Miller509b0102003-12-17 16:33:10 +1100582 struct timeval tv, *tvp;
Damien Millere11e1ea2010-08-03 16:04:46 +1000583 int timeout_secs;
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000584 time_t minwait_secs = 0, server_alive_time = 0, now = monotime();
Damien Miller509b0102003-12-17 16:33:10 +1100585 int ret;
586
Damien Miller5e953212001-01-30 09:14:00 +1100587 /* Add any selections by the channel mechanism. */
Damien Millera6508752012-04-22 11:21:10 +1000588 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp,
589 &minwait_secs, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000590
Damien Miller1383bd82000-04-06 12:32:37 +1000591 if (!compat20) {
592 /* Read from the connection, unless our buffers are full. */
593 if (buffer_len(&stdout_buffer) < buffer_high &&
594 buffer_len(&stderr_buffer) < buffer_high &&
595 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100596 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000597 /*
598 * Read from stdin, unless we have seen EOF or have very much
599 * buffered data to send to the server.
600 */
601 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100602 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000603
604 /* Select stdout/stderr if have data in buffer. */
605 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100606 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000607 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100608 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000609 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000610 /* channel_prepare_select could have closed the last channel */
Damien Miller164a7f42001-10-12 11:36:09 +1000611 if (session_closed && !channel_still_open() &&
612 !packet_have_data_to_write()) {
613 /* clear mask since we did not call select() */
Damien Miller79faeff2001-11-12 11:06:32 +1100614 memset(*readsetp, 0, *nallocp);
615 memset(*writesetp, 0, *nallocp);
Damien Miller164a7f42001-10-12 11:36:09 +1000616 return;
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000617 } else {
618 FD_SET(connection_in, *readsetp);
619 }
Damien Miller1383bd82000-04-06 12:32:37 +1000620 }
621
Damien Miller95def091999-11-25 00:26:21 +1100622 /* Select server connection if have data to write to the server. */
623 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100624 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100625
Damien Miller5428f641999-11-25 11:54:57 +1100626 /*
627 * Wait for something to happen. This will suspend the process until
628 * some selected descriptor can be read, written, or has some other
Damien Millere11e1ea2010-08-03 16:04:46 +1000629 * event pending, or a timeout expires.
Damien Miller5428f641999-11-25 11:54:57 +1100630 */
Damien Miller95def091999-11-25 00:26:21 +1100631
Damien Millere11e1ea2010-08-03 16:04:46 +1000632 timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000633 if (options.server_alive_interval > 0 && compat20) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000634 timeout_secs = options.server_alive_interval;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000635 server_alive_time = now + options.server_alive_interval;
636 }
637 if (options.rekey_interval > 0 && compat20 && !rekeying)
638 timeout_secs = MIN(timeout_secs, packet_get_rekey_timeout());
Damien Millere11e1ea2010-08-03 16:04:46 +1000639 set_control_persist_exit_time();
640 if (control_persist_exit_time > 0) {
641 timeout_secs = MIN(timeout_secs,
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000642 control_persist_exit_time - now);
Damien Millere11e1ea2010-08-03 16:04:46 +1000643 if (timeout_secs < 0)
644 timeout_secs = 0;
645 }
Damien Millera6508752012-04-22 11:21:10 +1000646 if (minwait_secs != 0)
647 timeout_secs = MIN(timeout_secs, (int)minwait_secs);
Damien Millere11e1ea2010-08-03 16:04:46 +1000648 if (timeout_secs == INT_MAX)
Damien Miller509b0102003-12-17 16:33:10 +1100649 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000650 else {
Damien Millere11e1ea2010-08-03 16:04:46 +1000651 tv.tv_sec = timeout_secs;
Damien Miller509b0102003-12-17 16:33:10 +1100652 tv.tv_usec = 0;
653 tvp = &tv;
654 }
Damien Millere11e1ea2010-08-03 16:04:46 +1000655
Damien Miller509b0102003-12-17 16:33:10 +1100656 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
657 if (ret < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100658 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000659
660 /*
661 * We have to clear the select masks, because we return.
662 * We have to return, because the mainloop checks for the flags
663 * set by the signal handlers.
664 */
Damien Miller79faeff2001-11-12 11:06:32 +1100665 memset(*readsetp, 0, *nallocp);
666 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000667
Damien Miller95def091999-11-25 00:26:21 +1100668 if (errno == EINTR)
669 return;
670 /* Note: we might still have data in the buffers. */
671 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
672 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100673 quit_pending = 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000674 } else if (ret == 0) {
675 /*
676 * Timeout. Could have been either keepalive or rekeying.
677 * Keepalive we check here, rekeying is checked in clientloop.
678 */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000679 if (server_alive_time != 0 && server_alive_time <= monotime())
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000680 server_alive_check();
681 }
682
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000683}
684
Ben Lindstrombba81212001-06-25 05:01:22 +0000685static void
Damien Millerad833b32000-08-23 10:46:23 +1000686client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000687{
Damien Miller95def091999-11-25 00:26:21 +1100688 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000689 if (buffer_len(bout) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000690 atomicio(vwrite, fileno(stdout), buffer_ptr(bout),
691 buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000692 if (buffer_len(berr) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000693 atomicio(vwrite, fileno(stderr), buffer_ptr(berr),
694 buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000695
Damien Miller21771e22011-05-15 08:45:50 +1000696 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000697
Damien Miller5428f641999-11-25 11:54:57 +1100698 /*
699 * Free (and clear) the buffer to reduce the amount of data that gets
700 * written to swap.
701 */
Damien Millerad833b32000-08-23 10:46:23 +1000702 buffer_free(bin);
703 buffer_free(bout);
704 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000705
Damien Miller95def091999-11-25 00:26:21 +1100706 /* Send the suspend signal to the program itself. */
707 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000708
Darren Tucker5d78de62004-11-05 20:35:44 +1100709 /* Reset window sizes in case they have changed */
710 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000711
Damien Miller95def091999-11-25 00:26:21 +1100712 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000713 buffer_init(bin);
714 buffer_init(bout);
715 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000716
Damien Miller21771e22011-05-15 08:45:50 +1000717 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000718}
719
Ben Lindstrombba81212001-06-25 05:01:22 +0000720static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100721client_process_net_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000722{
Darren Tuckerc5564e12009-06-21 18:53:53 +1000723 int len, cont = 0;
Darren Tucker86e30a02009-08-28 11:21:06 +1000724 char buf[SSH_IOBUFSZ];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000725
Damien Miller5428f641999-11-25 11:54:57 +1100726 /*
727 * Read input from the server, and add any such data to the buffer of
728 * the packet subsystem.
729 */
Damien Miller95def091999-11-25 00:26:21 +1100730 if (FD_ISSET(connection_in, readset)) {
731 /* Read as much as possible. */
Darren Tuckerc5564e12009-06-21 18:53:53 +1000732 len = roaming_read(connection_in, buf, sizeof(buf), &cont);
733 if (len == 0 && cont == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000734 /*
735 * Received EOF. The remote host has closed the
736 * connection.
737 */
738 snprintf(buf, sizeof buf,
739 "Connection to %.300s closed by remote host.\r\n",
740 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000741 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000742 quit_pending = 1;
743 return;
Damien Miller95def091999-11-25 00:26:21 +1100744 }
Damien Miller5428f641999-11-25 11:54:57 +1100745 /*
746 * There is a kernel bug on Solaris that causes select to
747 * sometimes wake up even though there is no data available.
748 */
Damien Millerd8968ad2008-07-04 23:10:49 +1000749 if (len < 0 &&
750 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Damien Miller95def091999-11-25 00:26:21 +1100751 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000752
Damien Miller95def091999-11-25 00:26:21 +1100753 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000754 /*
755 * An error has encountered. Perhaps there is a
756 * network problem.
757 */
758 snprintf(buf, sizeof buf,
759 "Read from remote host %.300s: %.100s\r\n",
760 host, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100761 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100762 quit_pending = 1;
763 return;
764 }
765 packet_process_incoming(buf, len);
766 }
Damien Miller1383bd82000-04-06 12:32:37 +1000767}
768
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000769static void
Damien Miller5771ed72008-05-19 15:35:33 +1000770client_status_confirm(int type, Channel *c, void *ctx)
Damien Miller0e220db2004-06-15 10:34:08 +1000771{
Damien Miller5771ed72008-05-19 15:35:33 +1000772 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
773 char errmsg[256];
774 int tochan;
Darren Tuckerfc959702004-07-17 16:12:08 +1000775
Damien Miller555f3b82011-05-15 08:48:05 +1000776 /*
777 * If a TTY was explicitly requested, then a failure to allocate
778 * one is fatal.
779 */
780 if (cr->action == CONFIRM_TTY &&
781 (options.request_tty == REQUEST_TTY_FORCE ||
782 options.request_tty == REQUEST_TTY_YES))
783 cr->action = CONFIRM_CLOSE;
784
Damien Miller5771ed72008-05-19 15:35:33 +1000785 /* XXX supress on mux _client_ quietmode */
786 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
Damien Millere1537f92010-01-26 13:26:22 +1100787 c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
Damien Miller0e220db2004-06-15 10:34:08 +1000788
Damien Miller5771ed72008-05-19 15:35:33 +1000789 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
790 debug2("%s request accepted on channel %d",
791 cr->request_type, c->self);
792 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
793 if (tochan) {
794 snprintf(errmsg, sizeof(errmsg),
795 "%s request failed\r\n", cr->request_type);
796 } else {
797 snprintf(errmsg, sizeof(errmsg),
798 "%s request failed on channel %d",
799 cr->request_type, c->self);
800 }
801 /* If error occurred on primary session channel, then exit */
Damien Miller555f3b82011-05-15 08:48:05 +1000802 if (cr->action == CONFIRM_CLOSE && c->self == session_ident)
Damien Miller5771ed72008-05-19 15:35:33 +1000803 fatal("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000804 /*
805 * If error occurred on mux client, append to
806 * their stderr.
807 */
808 if (tochan) {
809 buffer_append(&c->extended, errmsg,
810 strlen(errmsg));
811 } else
Damien Miller5771ed72008-05-19 15:35:33 +1000812 error("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000813 if (cr->action == CONFIRM_TTY) {
814 /*
815 * If a TTY allocation error occurred, then arrange
816 * for the correct TTY to leave raw mode.
817 */
818 if (c->self == session_ident)
819 leave_raw_mode(0);
820 else
821 mux_tty_alloc_failed(c);
822 } else if (cr->action == CONFIRM_CLOSE) {
Damien Miller5771ed72008-05-19 15:35:33 +1000823 chan_read_failed(c);
824 chan_write_failed(c);
825 }
Damien Miller0e220db2004-06-15 10:34:08 +1000826 }
Darren Tuckera627d422013-06-02 07:31:17 +1000827 free(cr);
Damien Miller5771ed72008-05-19 15:35:33 +1000828}
Damien Miller0e220db2004-06-15 10:34:08 +1000829
Damien Miller5771ed72008-05-19 15:35:33 +1000830static void
831client_abandon_status_confirm(Channel *c, void *ctx)
832{
Darren Tuckera627d422013-06-02 07:31:17 +1000833 free(ctx);
Damien Miller5771ed72008-05-19 15:35:33 +1000834}
835
Damien Miller6d7b4372011-06-23 08:31:57 +1000836void
Damien Miller555f3b82011-05-15 08:48:05 +1000837client_expect_confirm(int id, const char *request,
838 enum confirm_action action)
Damien Miller5771ed72008-05-19 15:35:33 +1000839{
840 struct channel_reply_ctx *cr = xmalloc(sizeof(*cr));
841
842 cr->request_type = request;
Damien Miller555f3b82011-05-15 08:48:05 +1000843 cr->action = action;
Damien Miller5771ed72008-05-19 15:35:33 +1000844
845 channel_register_status_confirm(id, client_status_confirm,
846 client_abandon_status_confirm, cr);
Damien Miller0e220db2004-06-15 10:34:08 +1000847}
848
Darren Tucker9f407c42008-06-13 04:50:27 +1000849void
850client_register_global_confirm(global_confirm_cb *cb, void *ctx)
851{
Damien Millerb9d3bee2008-07-16 22:40:52 +1000852 struct global_confirm *gc, *last_gc;
Darren Tucker9f407c42008-06-13 04:50:27 +1000853
854 /* Coalesce identical callbacks */
Damien Millerb9d3bee2008-07-16 22:40:52 +1000855 last_gc = TAILQ_LAST(&global_confirms, global_confirms);
856 if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
857 if (++last_gc->ref_count >= INT_MAX)
858 fatal("%s: last_gc->ref_count = %d",
859 __func__, last_gc->ref_count);
Darren Tucker9f407c42008-06-13 04:50:27 +1000860 return;
861 }
862
863 gc = xmalloc(sizeof(*gc));
864 gc->cb = cb;
865 gc->ctx = ctx;
866 gc->ref_count = 1;
867 TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
868}
869
Damien Miller0e220db2004-06-15 10:34:08 +1000870static void
Ben Lindstrom681d9322002-03-22 03:53:00 +0000871process_cmdline(void)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000872{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000873 void (*handler)(int);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100874 char *s, *cmd, *cancel_host;
Damien Millerff773642011-09-22 21:39:48 +1000875 int delete = 0, local = 0, remote = 0, dynamic = 0;
876 int cancel_port, ok;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100877 Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000878
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100879 bzero(&fwd, sizeof(fwd));
880 fwd.listen_host = fwd.connect_host = NULL;
881
Damien Miller21771e22011-05-15 08:45:50 +1000882 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000883 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000884 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000885 if (s == NULL)
886 goto out;
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100887 while (isspace(*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000888 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000889 if (*s == '-')
890 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000891 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000892 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000893
Darren Tucker1973c882004-05-24 10:34:36 +1000894 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000895 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000896 logit(" -L[bind_address:]port:host:hostport "
897 "Request local forward");
898 logit(" -R[bind_address:]port:host:hostport "
899 "Request remote forward");
Damien Miller0164cb82008-11-05 16:30:31 +1100900 logit(" -D[bind_address:]port "
901 "Request dynamic forward");
Damien Millerff773642011-09-22 21:39:48 +1000902 logit(" -KL[bind_address:]port "
903 "Cancel local forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000904 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000905 "Cancel remote forward");
Damien Millerff773642011-09-22 21:39:48 +1000906 logit(" -KD[bind_address:]port "
907 "Cancel dynamic forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100908 if (!options.permit_local_command)
909 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000910 logit(" !args "
911 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100912 goto out;
913 }
914
915 if (*s == '!' && options.permit_local_command) {
916 s++;
917 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000918 goto out;
919 }
920
921 if (*s == 'K') {
922 delete = 1;
923 s++;
924 }
Damien Miller0164cb82008-11-05 16:30:31 +1100925 if (*s == 'L')
926 local = 1;
927 else if (*s == 'R')
928 remote = 1;
929 else if (*s == 'D')
930 dynamic = 1;
931 else {
Damien Miller996acd22003-04-09 20:59:48 +1000932 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000933 goto out;
934 }
Damien Miller0164cb82008-11-05 16:30:31 +1100935
Damien Millerff773642011-09-22 21:39:48 +1000936 if (delete && !compat20) {
Damien Miller996acd22003-04-09 20:59:48 +1000937 logit("Not supported for SSH protocol version 1.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000938 goto out;
939 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000940
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100941 while (isspace(*++s))
942 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000943
Damien Millere1537f92010-01-26 13:26:22 +1100944 /* XXX update list of forwards in options */
Darren Tuckere7066df2004-05-24 10:18:05 +1000945 if (delete) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100946 cancel_port = 0;
947 cancel_host = hpdelim(&s); /* may be NULL */
948 if (s != NULL) {
949 cancel_port = a2port(s);
950 cancel_host = cleanhostname(cancel_host);
951 } else {
952 cancel_port = a2port(cancel_host);
953 cancel_host = NULL;
954 }
Damien Miller3dc71ad2009-01-28 16:31:22 +1100955 if (cancel_port <= 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100956 logit("Bad forwarding close port");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000957 goto out;
958 }
Damien Millerff773642011-09-22 21:39:48 +1000959 if (remote)
960 ok = channel_request_rforward_cancel(cancel_host,
961 cancel_port) == 0;
962 else if (dynamic)
963 ok = channel_cancel_lport_listener(cancel_host,
964 cancel_port, 0, options.gateway_ports) > 0;
965 else
966 ok = channel_cancel_lport_listener(cancel_host,
967 cancel_port, CHANNEL_CANCEL_PORT_STATIC,
968 options.gateway_ports) > 0;
969 if (!ok) {
970 logit("Unkown port forwarding.");
971 goto out;
972 }
973 logit("Canceled forwarding.");
Darren Tuckere7066df2004-05-24 10:18:05 +1000974 } else {
Damien Miller4bf648f2009-02-14 16:28:21 +1100975 if (!parse_forward(&fwd, s, dynamic, remote)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000976 logit("Bad forwarding specification.");
977 goto out;
978 }
Damien Miller0164cb82008-11-05 16:30:31 +1100979 if (local || dynamic) {
Damien Miller73298f42013-01-09 15:55:50 +1100980 if (!channel_setup_local_fwd_listener(fwd.listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100981 fwd.listen_port, fwd.connect_host,
Damien Miller73298f42013-01-09 15:55:50 +1100982 fwd.connect_port, options.gateway_ports)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000983 logit("Port forwarding failed.");
984 goto out;
985 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100986 } else {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000987 if (channel_request_remote_forwarding(fwd.listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100988 fwd.listen_port, fwd.connect_host,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000989 fwd.connect_port) < 0) {
990 logit("Port forwarding failed.");
991 goto out;
992 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100993 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000994 logit("Forwarding port.");
995 }
996
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000997out:
998 signal(SIGINT, handler);
Damien Miller21771e22011-05-15 08:45:50 +1000999 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tuckera627d422013-06-02 07:31:17 +10001000 free(cmd);
1001 free(fwd.listen_host);
1002 free(fwd.connect_host);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001003}
1004
Darren Tucker92a39cf2012-09-07 11:20:20 +10001005/* reasons to suppress output of an escape command in help output */
1006#define SUPPRESS_NEVER 0 /* never suppress, always show */
1007#define SUPPRESS_PROTO1 1 /* don't show in protocol 1 sessions */
1008#define SUPPRESS_MUXCLIENT 2 /* don't show in mux client sessions */
1009#define SUPPRESS_MUXMASTER 4 /* don't show in mux master sessions */
1010#define SUPPRESS_SYSLOG 8 /* don't show when logging to syslog */
1011struct escape_help_text {
1012 const char *cmd;
1013 const char *text;
1014 unsigned int flags;
1015};
1016static struct escape_help_text esc_txt[] = {
1017 {".", "terminate session", SUPPRESS_MUXMASTER},
1018 {".", "terminate connection (and any multiplexed sessions)",
1019 SUPPRESS_MUXCLIENT},
1020 {"B", "send a BREAK to the remote system", SUPPRESS_PROTO1},
1021 {"C", "open a command line", SUPPRESS_MUXCLIENT},
1022 {"R", "request rekey", SUPPRESS_PROTO1},
Darren Tuckerca0d0fd2012-09-07 11:22:24 +10001023 {"V/v", "decrease/increase verbosity (LogLevel)", SUPPRESS_MUXCLIENT},
Darren Tucker92a39cf2012-09-07 11:20:20 +10001024 {"^Z", "suspend ssh", SUPPRESS_MUXCLIENT},
1025 {"#", "list forwarded connections", SUPPRESS_NEVER},
1026 {"&", "background ssh (when waiting for connections to terminate)",
1027 SUPPRESS_MUXCLIENT},
1028 {"?", "this message", SUPPRESS_NEVER},
1029};
1030
1031static void
1032print_escape_help(Buffer *b, int escape_char, int protocol2, int mux_client,
1033 int using_stderr)
1034{
1035 unsigned int i, suppress_flags;
1036 char string[1024];
1037
1038 snprintf(string, sizeof string, "%c?\r\n"
1039 "Supported escape sequences:\r\n", escape_char);
1040 buffer_append(b, string, strlen(string));
1041
1042 suppress_flags = (protocol2 ? 0 : SUPPRESS_PROTO1) |
1043 (mux_client ? SUPPRESS_MUXCLIENT : 0) |
1044 (mux_client ? 0 : SUPPRESS_MUXMASTER) |
1045 (using_stderr ? 0 : SUPPRESS_SYSLOG);
1046
1047 for (i = 0; i < sizeof(esc_txt)/sizeof(esc_txt[0]); i++) {
1048 if (esc_txt[i].flags & suppress_flags)
1049 continue;
Darren Tuckerca0d0fd2012-09-07 11:22:24 +10001050 snprintf(string, sizeof string, " %c%-3s - %s\r\n",
Darren Tucker92a39cf2012-09-07 11:20:20 +10001051 escape_char, esc_txt[i].cmd, esc_txt[i].text);
1052 buffer_append(b, string, strlen(string));
1053 }
1054
1055 snprintf(string, sizeof string,
Darren Tuckerca0d0fd2012-09-07 11:22:24 +10001056 " %c%c - send the escape character by typing it twice\r\n"
Darren Tucker92a39cf2012-09-07 11:20:20 +10001057 "(Note that escapes are only recognized immediately after "
1058 "newline.)\r\n", escape_char, escape_char);
1059 buffer_append(b, string, strlen(string));
1060}
1061
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001062/*
1063 * Process the characters one by one, call with c==NULL for proto1 case.
1064 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001065static int
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001066process_escapes(Channel *c, Buffer *bin, Buffer *bout, Buffer *berr,
1067 char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001068{
1069 char string[1024];
1070 pid_t pid;
1071 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001072 u_int i;
1073 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +10001074 char *s;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001075 int *escape_pendingp, escape_char;
1076 struct escape_filter_ctx *efc;
Damien Millerad833b32000-08-23 10:46:23 +10001077
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001078 if (c == NULL) {
1079 escape_pendingp = &escape_pending1;
1080 escape_char = escape_char1;
1081 } else {
1082 if (c->filter_ctx == NULL)
1083 return 0;
1084 efc = (struct escape_filter_ctx *)c->filter_ctx;
1085 escape_pendingp = &efc->escape_pending;
1086 escape_char = efc->escape_char;
1087 }
1088
Damien Millereccb9de2005-06-17 12:59:34 +10001089 if (len <= 0)
1090 return (0);
1091
1092 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +10001093 /* Get one character at a time. */
1094 ch = buf[i];
1095
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001096 if (*escape_pendingp) {
Damien Millerad833b32000-08-23 10:46:23 +10001097 /* We have previously seen an escape character. */
1098 /* Clear the flag now. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001099 *escape_pendingp = 0;
Damien Millerad833b32000-08-23 10:46:23 +10001100
1101 /* Process the escaped character. */
1102 switch (ch) {
1103 case '.':
1104 /* Terminate the connection. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001105 snprintf(string, sizeof string, "%c.\r\n",
1106 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001107 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001108
Damien Millere1537f92010-01-26 13:26:22 +11001109 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001110 chan_read_failed(c);
1111 chan_write_failed(c);
Darren Tuckerea8342c2013-06-06 08:11:40 +10001112 if (c->detach_user)
1113 c->detach_user(c->self, NULL);
Damien Miller36187092013-06-10 13:07:11 +10001114 c->type = SSH_CHANNEL_ABANDONED;
1115 buffer_clear(&c->input);
1116 chan_ibuf_empty(c);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001117 return 0;
1118 } else
1119 quit_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001120 return -1;
1121
1122 case 'Z' - 64:
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001123 /* XXX support this for mux clients */
Damien Millere1537f92010-01-26 13:26:22 +11001124 if (c && c->ctl_chan != -1) {
Darren Tuckerf111d402012-09-07 11:21:42 +10001125 char b[16];
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001126 noescape:
Darren Tuckerf111d402012-09-07 11:21:42 +10001127 if (ch == 'Z' - 64)
1128 snprintf(b, sizeof b, "^Z");
1129 else
1130 snprintf(b, sizeof b, "%c", ch);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001131 snprintf(string, sizeof string,
Darren Tuckerf111d402012-09-07 11:21:42 +10001132 "%c%s escape not available to "
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001133 "multiplexed sessions\r\n",
Darren Tuckerf111d402012-09-07 11:21:42 +10001134 escape_char, b);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001135 buffer_append(berr, string,
1136 strlen(string));
1137 continue;
1138 }
Darren Tucker4d5cd332008-06-13 04:51:14 +10001139 /* Suspend the program. Inform the user */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001140 snprintf(string, sizeof string,
1141 "%c^Z [suspend ssh]\r\n", escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001142 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001143
1144 /* Restore terminal modes and suspend. */
1145 client_suspend_self(bin, bout, berr);
1146
1147 /* We have been continued. */
1148 continue;
1149
Damien Miller54c45982003-05-15 10:20:13 +10001150 case 'B':
1151 if (compat20) {
1152 snprintf(string, sizeof string,
1153 "%cB\r\n", escape_char);
1154 buffer_append(berr, string,
1155 strlen(string));
Damien Miller70182522013-09-14 09:49:19 +10001156 channel_request_start(c->self,
Damien Miller54c45982003-05-15 10:20:13 +10001157 "break", 0);
1158 packet_put_int(1000);
1159 packet_send();
1160 }
1161 continue;
1162
Ben Lindstromf28f6342001-04-04 02:03:04 +00001163 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001164 if (compat20) {
1165 if (datafellows & SSH_BUG_NOREKEY)
Darren Tucker4d5cd332008-06-13 04:51:14 +10001166 logit("Server does not "
1167 "support re-keying");
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001168 else
1169 need_rekeying = 1;
1170 }
Ben Lindstromf28f6342001-04-04 02:03:04 +00001171 continue;
1172
Darren Tucker50a48d02012-09-06 21:25:37 +10001173 case 'V':
1174 /* FALLTHROUGH */
1175 case 'v':
1176 if (c && c->ctl_chan != -1)
1177 goto noescape;
1178 if (!log_is_on_stderr()) {
1179 snprintf(string, sizeof string,
1180 "%c%c [Logging to syslog]\r\n",
1181 escape_char, ch);
1182 buffer_append(berr, string,
1183 strlen(string));
1184 continue;
1185 }
1186 if (ch == 'V' && options.log_level >
1187 SYSLOG_LEVEL_QUIET)
1188 log_change_level(--options.log_level);
1189 if (ch == 'v' && options.log_level <
1190 SYSLOG_LEVEL_DEBUG3)
1191 log_change_level(++options.log_level);
1192 snprintf(string, sizeof string,
1193 "%c%c [LogLevel %s]\r\n", escape_char, ch,
1194 log_level_name(options.log_level));
1195 buffer_append(berr, string, strlen(string));
1196 continue;
1197
Damien Millerad833b32000-08-23 10:46:23 +10001198 case '&':
Damien Millere1537f92010-01-26 13:26:22 +11001199 if (c && c->ctl_chan != -1)
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001200 goto noescape;
Damien Millerad833b32000-08-23 10:46:23 +10001201 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001202 * Detach the program (continue to serve
1203 * connections, but put in background and no
1204 * more new connections).
Damien Millerad833b32000-08-23 10:46:23 +10001205 */
Damien Miller96507ef2001-11-12 10:52:25 +11001206 /* Restore tty modes. */
Damien Miller21771e22011-05-15 08:45:50 +10001207 leave_raw_mode(
1208 options.request_tty == REQUEST_TTY_FORCE);
Damien Miller96507ef2001-11-12 10:52:25 +11001209
1210 /* Stop listening for new connections. */
1211 channel_stop_listening();
1212
1213 snprintf(string, sizeof string,
1214 "%c& [backgrounded]\n", escape_char);
1215 buffer_append(berr, string, strlen(string));
1216
1217 /* Fork into background. */
1218 pid = fork();
1219 if (pid < 0) {
1220 error("fork: %.100s", strerror(errno));
1221 continue;
1222 }
1223 if (pid != 0) { /* This is the parent. */
1224 /* The parent just exits. */
1225 exit(0);
1226 }
1227 /* The child continues serving connections. */
1228 if (compat20) {
1229 buffer_append(bin, "\004", 1);
1230 /* fake EOF on stdin */
1231 return -1;
1232 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +10001233 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001234 * Sending SSH_CMSG_EOF alone does not
1235 * always appear to be enough. So we
1236 * try to send an EOF character first.
Damien Millerad833b32000-08-23 10:46:23 +10001237 */
1238 packet_start(SSH_CMSG_STDIN_DATA);
1239 packet_put_string("\004", 1);
1240 packet_send();
1241 /* Close stdin. */
1242 stdin_eof = 1;
1243 if (buffer_len(bin) == 0) {
1244 packet_start(SSH_CMSG_EOF);
1245 packet_send();
1246 }
1247 }
Damien Miller96507ef2001-11-12 10:52:25 +11001248 continue;
Damien Millerad833b32000-08-23 10:46:23 +10001249
1250 case '?':
Darren Tucker92a39cf2012-09-07 11:20:20 +10001251 print_escape_help(berr, escape_char, compat20,
1252 (c && c->ctl_chan != -1),
1253 log_is_on_stderr());
Damien Millerad833b32000-08-23 10:46:23 +10001254 continue;
1255
1256 case '#':
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001257 snprintf(string, sizeof string, "%c#\r\n",
1258 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001259 buffer_append(berr, string, strlen(string));
1260 s = channel_open_message();
1261 buffer_append(berr, s, strlen(s));
Darren Tuckera627d422013-06-02 07:31:17 +10001262 free(s);
Damien Millerad833b32000-08-23 10:46:23 +10001263 continue;
1264
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001265 case 'C':
Damien Millere1537f92010-01-26 13:26:22 +11001266 if (c && c->ctl_chan != -1)
Damien Miller586b0052008-12-09 14:11:32 +11001267 goto noescape;
Ben Lindstrom681d9322002-03-22 03:53:00 +00001268 process_cmdline();
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001269 continue;
1270
Damien Millerad833b32000-08-23 10:46:23 +10001271 default:
1272 if (ch != escape_char) {
1273 buffer_put_char(bin, escape_char);
1274 bytes++;
1275 }
1276 /* Escaped characters fall through here */
1277 break;
1278 }
1279 } else {
1280 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001281 * The previous character was not an escape char.
1282 * Check if this is an escape.
Damien Millerad833b32000-08-23 10:46:23 +10001283 */
1284 if (last_was_cr && ch == escape_char) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001285 /*
1286 * It is. Set the flag and continue to
1287 * next character.
1288 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001289 *escape_pendingp = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001290 continue;
1291 }
1292 }
1293
1294 /*
1295 * Normal character. Record whether it was a newline,
1296 * and append it to the buffer.
1297 */
1298 last_was_cr = (ch == '\r' || ch == '\n');
1299 buffer_put_char(bin, ch);
1300 bytes++;
1301 }
1302 return bytes;
1303}
1304
Ben Lindstrombba81212001-06-25 05:01:22 +00001305static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001306client_process_input(fd_set *readset)
Damien Miller1383bd82000-04-06 12:32:37 +10001307{
Damien Miller166fca82000-04-20 07:42:21 +10001308 int len;
Darren Tucker86e30a02009-08-28 11:21:06 +10001309 char buf[SSH_IOBUFSZ];
Damien Miller1383bd82000-04-06 12:32:37 +10001310
Damien Miller95def091999-11-25 00:26:21 +11001311 /* Read input from stdin. */
1312 if (FD_ISSET(fileno(stdin), readset)) {
1313 /* Read as much as possible. */
1314 len = read(fileno(stdin), buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +10001315 if (len < 0 &&
1316 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001317 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +11001318 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001319 /*
1320 * Received EOF or error. They are treated
1321 * similarly, except that an error message is printed
1322 * if it was an error condition.
1323 */
Damien Miller95def091999-11-25 00:26:21 +11001324 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001325 snprintf(buf, sizeof buf, "read: %.100s\r\n",
1326 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001327 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001328 }
1329 /* Mark that we have seen EOF. */
1330 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +11001331 /*
1332 * Send an EOF message to the server unless there is
1333 * data in the buffer. If there is data in the
1334 * buffer, no message will be sent now. Code
1335 * elsewhere will send the EOF when the buffer
1336 * becomes empty if stdin_eof is set.
1337 */
Damien Miller95def091999-11-25 00:26:21 +11001338 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001339 packet_start(SSH_CMSG_EOF);
1340 packet_send();
Damien Miller95def091999-11-25 00:26:21 +11001341 }
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001342 } else if (escape_char1 == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +11001343 /*
1344 * Normal successful read, and no escape character.
1345 * Just append the data to buffer.
1346 */
Damien Miller95def091999-11-25 00:26:21 +11001347 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +11001348 } else {
Damien Miller5428f641999-11-25 11:54:57 +11001349 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001350 * Normal, successful read. But we have an escape
1351 * character and have to process the characters one
1352 * by one.
Damien Miller5428f641999-11-25 11:54:57 +11001353 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001354 if (process_escapes(NULL, &stdin_buffer,
1355 &stdout_buffer, &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +10001356 return;
Damien Miller95def091999-11-25 00:26:21 +11001357 }
1358 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001359}
1360
Ben Lindstrombba81212001-06-25 05:01:22 +00001361static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001362client_process_output(fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001363{
Damien Miller95def091999-11-25 00:26:21 +11001364 int len;
1365 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001366
Damien Miller95def091999-11-25 00:26:21 +11001367 /* Write buffered output to stdout. */
1368 if (FD_ISSET(fileno(stdout), writeset)) {
1369 /* Write as much data as possible. */
1370 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001371 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001372 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001373 if (errno == EINTR || errno == EAGAIN ||
1374 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001375 len = 0;
1376 else {
Damien Miller5428f641999-11-25 11:54:57 +11001377 /*
1378 * An error or EOF was encountered. Put an
1379 * error message to stderr buffer.
1380 */
Darren Tucker4d5cd332008-06-13 04:51:14 +10001381 snprintf(buf, sizeof buf,
1382 "write stdout: %.50s\r\n", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001383 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001384 quit_pending = 1;
1385 return;
1386 }
1387 }
1388 /* Consume printed data from the buffer. */
1389 buffer_consume(&stdout_buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001390 }
Damien Miller95def091999-11-25 00:26:21 +11001391 /* Write buffered output to stderr. */
1392 if (FD_ISSET(fileno(stderr), writeset)) {
1393 /* Write as much data as possible. */
1394 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001395 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001396 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001397 if (errno == EINTR || errno == EAGAIN ||
1398 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001399 len = 0;
1400 else {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001401 /*
1402 * EOF or error, but can't even print
1403 * error message.
1404 */
Damien Miller95def091999-11-25 00:26:21 +11001405 quit_pending = 1;
1406 return;
1407 }
1408 }
1409 /* Consume printed characters from the buffer. */
1410 buffer_consume(&stderr_buffer, len);
1411 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001412}
1413
Damien Miller5428f641999-11-25 11:54:57 +11001414/*
Damien Millerb38eff82000-04-01 11:09:21 +10001415 * Get packets from the connection input buffer, and process them as long as
1416 * there are packets available.
1417 *
1418 * Any unknown packets received during the actual
1419 * session cause the session to terminate. This is
1420 * intended to make debugging easier since no
1421 * confirmations are sent. Any compatible protocol
1422 * extensions must be negotiated during the
1423 * preparatory phase.
1424 */
1425
Ben Lindstrombba81212001-06-25 05:01:22 +00001426static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001427client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001428{
Darren Tucker4d5cd332008-06-13 04:51:14 +10001429 dispatch_run(DISPATCH_NONBLOCK, &quit_pending,
1430 compat20 ? xxx_kex : NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001431}
1432
Damien Millerad833b32000-08-23 10:46:23 +10001433/* scan buf[] for '~' before sending data to the peer */
1434
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001435/* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1436void *
1437client_new_escape_filter_ctx(int escape_char)
1438{
1439 struct escape_filter_ctx *ret;
1440
1441 ret = xmalloc(sizeof(*ret));
1442 ret->escape_pending = 0;
1443 ret->escape_char = escape_char;
1444 return (void *)ret;
1445}
1446
Darren Tucker84c56f52008-06-13 04:55:46 +10001447/* Free the escape filter context on channel free */
1448void
1449client_filter_cleanup(int cid, void *ctx)
1450{
Darren Tuckera627d422013-06-02 07:31:17 +10001451 free(ctx);
Darren Tucker84c56f52008-06-13 04:55:46 +10001452}
1453
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001454int
1455client_simple_escape_filter(Channel *c, char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001456{
Damien Miller5771ed72008-05-19 15:35:33 +10001457 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1458 return 0;
1459
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001460 return process_escapes(c, &c->input, &c->output, &c->extended,
1461 buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001462}
1463
Ben Lindstrombba81212001-06-25 05:01:22 +00001464static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001465client_channel_closed(int id, void *arg)
1466{
Damien Miller3ec27592001-10-12 11:35:04 +10001467 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001468 session_closed = 1;
Damien Miller21771e22011-05-15 08:45:50 +10001469 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001470}
1471
Damien Millerb38eff82000-04-01 11:09:21 +10001472/*
Damien Miller5428f641999-11-25 11:54:57 +11001473 * Implements the interactive session with the server. This is called after
1474 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001475 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1476 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001477 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001478
Damien Miller4af51302000-04-16 11:18:38 +10001479int
Damien Millerad833b32000-08-23 10:46:23 +10001480client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001481{
Damien Miller5e953212001-01-30 09:14:00 +11001482 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001483 double start_time, total_time;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001484 int max_fd = 0, max_fd2 = 0, len, rekeying = 0;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001485 u_int64_t ibytes, obytes;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001486 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001487 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001488
Damien Miller95def091999-11-25 00:26:21 +11001489 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001490
Damien Miller95def091999-11-25 00:26:21 +11001491 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001492
Damien Miller95def091999-11-25 00:26:21 +11001493 /* Initialize variables. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001494 escape_pending1 = 0;
Damien Miller95def091999-11-25 00:26:21 +11001495 last_was_cr = 1;
1496 exit_status = -1;
1497 stdin_eof = 0;
1498 buffer_high = 64 * 1024;
1499 connection_in = packet_get_connection_in();
1500 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +11001501 max_fd = MAX(connection_in, connection_out);
1502
1503 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +00001504 /* enable nonblocking unless tty */
1505 if (!isatty(fileno(stdin)))
1506 set_nonblock(fileno(stdin));
1507 if (!isatty(fileno(stdout)))
1508 set_nonblock(fileno(stdout));
1509 if (!isatty(fileno(stderr)))
1510 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +11001511 max_fd = MAX(max_fd, fileno(stdin));
1512 max_fd = MAX(max_fd, fileno(stdout));
1513 max_fd = MAX(max_fd, fileno(stderr));
1514 }
Damien Miller95def091999-11-25 00:26:21 +11001515 quit_pending = 0;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001516 escape_char1 = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001517
Damien Miller95def091999-11-25 00:26:21 +11001518 /* Initialize buffers. */
1519 buffer_init(&stdin_buffer);
1520 buffer_init(&stdout_buffer);
1521 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001522
Damien Millerb38eff82000-04-01 11:09:21 +10001523 client_init_dispatch();
1524
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001525 /*
1526 * Set signal handlers, (e.g. to restore non-blocking mode)
1527 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1528 */
Darren Tucker07336da2004-11-05 20:02:16 +11001529 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1530 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001531 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1532 signal(SIGINT, signal_handler);
1533 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1534 signal(SIGQUIT, signal_handler);
1535 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1536 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001537 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001538
Damien Miller95def091999-11-25 00:26:21 +11001539 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001540 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001541
Ben Lindstrom5b828322001-02-09 01:34:36 +00001542 if (compat20) {
1543 session_ident = ssh2_chan_id;
Damien Miller6c3eec72011-05-05 14:16:22 +10001544 if (session_ident != -1) {
1545 if (escape_char_arg != SSH_ESCAPECHAR_NONE) {
1546 channel_register_filter(session_ident,
1547 client_simple_escape_filter, NULL,
1548 client_filter_cleanup,
1549 client_new_escape_filter_ctx(
1550 escape_char_arg));
1551 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001552 channel_register_cleanup(session_ident,
Damien Miller39eda6e2005-11-05 14:52:50 +11001553 client_channel_closed, 0);
Damien Miller6c3eec72011-05-05 14:16:22 +10001554 }
Ben Lindstrom5b828322001-02-09 01:34:36 +00001555 } else {
1556 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +10001557 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001558 }
Damien Millerad833b32000-08-23 10:46:23 +10001559
Damien Miller95def091999-11-25 00:26:21 +11001560 /* Main loop of the client for the interactive session mode. */
1561 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001562
Damien Miller5428f641999-11-25 11:54:57 +11001563 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001564 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001565
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001566 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +10001567 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001568
1569 rekeying = (xxx_kex != NULL && !xxx_kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +10001570
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001571 if (rekeying) {
1572 debug("rekeying in progress");
1573 } else {
1574 /*
1575 * Make packets of buffered stdin data, and buffer
1576 * them for sending to the server.
1577 */
1578 if (!compat20)
1579 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001580
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001581 /*
1582 * Make packets from buffered channel data, and
1583 * enqueue them for sending to the server.
1584 */
1585 if (packet_not_very_much_data_to_write())
1586 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001587
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001588 /*
1589 * Check if the window size has changed, and buffer a
1590 * message about it to the server if so.
1591 */
1592 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001593
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001594 if (quit_pending)
1595 break;
1596 }
Damien Miller5428f641999-11-25 11:54:57 +11001597 /*
1598 * Wait until we have something to do (something becomes
1599 * available on one of the descriptors).
1600 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001601 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001602 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001603 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001604
Damien Miller95def091999-11-25 00:26:21 +11001605 if (quit_pending)
1606 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001607
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001608 /* Do channel operations unless rekeying in progress. */
1609 if (!rekeying) {
1610 channel_after_select(readset, writeset);
Damien Millera5539d22003-04-09 20:50:06 +10001611 if (need_rekeying || packet_need_rekeying()) {
1612 debug("need rekeying");
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001613 xxx_kex->done = 0;
1614 kex_send_kexinit(xxx_kex);
1615 need_rekeying = 0;
1616 }
1617 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001618
Damien Miller1383bd82000-04-06 12:32:37 +10001619 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001620 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001621
Damien Miller1383bd82000-04-06 12:32:37 +10001622 if (quit_pending)
1623 break;
1624
1625 if (!compat20) {
1626 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +11001627 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +10001628 /*
1629 * Process output to stdout and stderr. Output to
1630 * the connection is processed elsewhere (above).
1631 */
Damien Miller5e953212001-01-30 09:14:00 +11001632 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +10001633 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001634
Darren Tuckere32cf432010-01-08 16:51:40 +11001635 if (session_resumed) {
1636 connection_in = packet_get_connection_in();
1637 connection_out = packet_get_connection_out();
1638 max_fd = MAX(max_fd, connection_out);
1639 max_fd = MAX(max_fd, connection_in);
1640 session_resumed = 0;
1641 }
1642
Darren Tucker4d5cd332008-06-13 04:51:14 +10001643 /*
1644 * Send as much buffered packet data as possible to the
1645 * sender.
1646 */
Damien Miller5e953212001-01-30 09:14:00 +11001647 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001648 packet_write_poll();
Damien Millere11e1ea2010-08-03 16:04:46 +10001649
1650 /*
1651 * If we are a backgrounded control master, and the
1652 * timeout has expired without any active client
1653 * connections, then quit.
1654 */
1655 if (control_persist_exit_time > 0) {
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001656 if (monotime() >= control_persist_exit_time) {
Damien Millere11e1ea2010-08-03 16:04:46 +10001657 debug("ControlPersist timeout expired");
1658 break;
1659 }
1660 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001661 }
Darren Tuckera627d422013-06-02 07:31:17 +10001662 free(readset);
1663 free(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001664
Damien Miller95def091999-11-25 00:26:21 +11001665 /* Terminate the session. */
1666
1667 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001668 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001669
Darren Tuckerde0c0252009-07-06 07:17:35 +10001670 if (compat20) {
1671 packet_start(SSH2_MSG_DISCONNECT);
1672 packet_put_int(SSH2_DISCONNECT_BY_APPLICATION);
1673 packet_put_cstring("disconnected by user");
Damien Miller8ddc71c2010-03-22 05:54:02 +11001674 packet_put_cstring(""); /* language tag */
Darren Tuckerde0c0252009-07-06 07:17:35 +10001675 packet_send();
1676 packet_write_wait();
1677 }
Darren Tucker12b4a652009-06-21 18:14:48 +10001678
Ben Lindstrom601e4362001-06-21 03:19:23 +00001679 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +11001680
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001681 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001682 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001683
1684 /* restore blocking io */
1685 if (!isatty(fileno(stdin)))
1686 unset_nonblock(fileno(stdin));
1687 if (!isatty(fileno(stdout)))
1688 unset_nonblock(fileno(stdout));
1689 if (!isatty(fileno(stderr)))
1690 unset_nonblock(fileno(stderr));
1691
Damien Millerd6965512003-12-17 16:31:53 +11001692 /*
1693 * If there was no shell or command requested, there will be no remote
1694 * exit status to be returned. In that case, clear error code if the
1695 * connection was deliberately terminated at this end.
1696 */
1697 if (no_shell_flag && received_signal == SIGTERM) {
1698 received_signal = 0;
1699 exit_status = 0;
1700 }
1701
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001702 if (received_signal)
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001703 fatal("Killed by signal %d.", (int) received_signal);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001704
1705 /*
1706 * In interactive mode (with pseudo tty) display a message indicating
1707 * that the connection has been closed.
1708 */
1709 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001710 snprintf(buf, sizeof buf,
1711 "Connection to %.64s closed.\r\n", host);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001712 buffer_append(&stderr_buffer, buf, strlen(buf));
1713 }
1714
Damien Miller95def091999-11-25 00:26:21 +11001715 /* Output any buffered data for stdout. */
Damien Millercfd6e4f2011-01-16 23:18:33 +11001716 if (buffer_len(&stdout_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001717 len = atomicio(vwrite, fileno(stdout),
1718 buffer_ptr(&stdout_buffer), buffer_len(&stdout_buffer));
Damien Millercfd6e4f2011-01-16 23:18:33 +11001719 if (len < 0 || (u_int)len != buffer_len(&stdout_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001720 error("Write failed flushing stdout buffer.");
Damien Millercfd6e4f2011-01-16 23:18:33 +11001721 else
1722 buffer_consume(&stdout_buffer, len);
Damien Miller95def091999-11-25 00:26:21 +11001723 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001724
Damien Miller95def091999-11-25 00:26:21 +11001725 /* Output any buffered data for stderr. */
Damien Millercfd6e4f2011-01-16 23:18:33 +11001726 if (buffer_len(&stderr_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001727 len = atomicio(vwrite, fileno(stderr),
1728 buffer_ptr(&stderr_buffer), buffer_len(&stderr_buffer));
Damien Millercfd6e4f2011-01-16 23:18:33 +11001729 if (len < 0 || (u_int)len != buffer_len(&stderr_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001730 error("Write failed flushing stderr buffer.");
Damien Millercfd6e4f2011-01-16 23:18:33 +11001731 else
1732 buffer_consume(&stderr_buffer, len);
Damien Miller95def091999-11-25 00:26:21 +11001733 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001734
Damien Miller95def091999-11-25 00:26:21 +11001735 /* Clear and free any buffers. */
1736 memset(buf, 0, sizeof(buf));
1737 buffer_free(&stdin_buffer);
1738 buffer_free(&stdout_buffer);
1739 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001740
Damien Miller95def091999-11-25 00:26:21 +11001741 /* Report bytes transferred, and transfer rates. */
1742 total_time = get_current_time() - start_time;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001743 packet_get_state(MODE_IN, NULL, NULL, NULL, &ibytes);
1744 packet_get_state(MODE_OUT, NULL, NULL, NULL, &obytes);
1745 verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
Damien Miller821de0a2011-01-11 17:20:29 +11001746 (unsigned long long)obytes, (unsigned long long)ibytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001747 if (total_time > 0)
Damien Millerb61f3fc2008-07-11 17:36:48 +10001748 verbose("Bytes per second: sent %.1f, received %.1f",
1749 obytes / total_time, ibytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001750 /* Return the exit status of the program. */
1751 debug("Exit status %d", exit_status);
1752 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001753}
Damien Millerb38eff82000-04-01 11:09:21 +10001754
1755/*********/
1756
Ben Lindstrombba81212001-06-25 05:01:22 +00001757static void
Damien Miller630d6f42002-01-22 23:17:30 +11001758client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001759{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001760 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001761 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001762 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001763 buffer_append(&stdout_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001764 memset(data, 0, data_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001765 free(data);
Damien Millerb38eff82000-04-01 11:09:21 +10001766}
Ben Lindstrombba81212001-06-25 05:01:22 +00001767static void
Damien Miller630d6f42002-01-22 23:17:30 +11001768client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001769{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001770 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001771 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001772 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001773 buffer_append(&stderr_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001774 memset(data, 0, data_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001775 free(data);
Damien Millerb38eff82000-04-01 11:09:21 +10001776}
Ben Lindstrombba81212001-06-25 05:01:22 +00001777static void
Damien Miller630d6f42002-01-22 23:17:30 +11001778client_input_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001779{
Damien Millerb38eff82000-04-01 11:09:21 +10001780 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001781 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001782 /* Acknowledge the exit. */
1783 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1784 packet_send();
1785 /*
1786 * Must wait for packet to be sent since we are
1787 * exiting the loop.
1788 */
1789 packet_write_wait();
1790 /* Flag that we want to exit. */
1791 quit_pending = 1;
1792}
Darren Tucker5dcdd212003-10-02 16:17:00 +10001793static void
1794client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1795{
1796 Channel *c = NULL;
1797 int remote_id, sock;
1798
1799 /* Read the remote channel number from the message. */
1800 remote_id = packet_get_int();
1801 packet_check_eom();
1802
1803 /*
1804 * Get a connection to the local authentication agent (this may again
1805 * get forwarded).
1806 */
1807 sock = ssh_get_authentication_socket();
1808
1809 /*
1810 * If we could not connect the agent, send an error message back to
1811 * the server. This should never happen unless the agent dies,
1812 * because authentication forwarding is only enabled if we have an
1813 * agent.
1814 */
1815 if (sock >= 0) {
1816 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1817 -1, 0, 0, 0, "authentication agent connection", 1);
1818 c->remote_id = remote_id;
1819 c->force_drain = 1;
1820 }
1821 if (c == NULL) {
1822 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1823 packet_put_int(remote_id);
1824 } else {
1825 /* Send a confirmation to the remote host. */
1826 debug("Forwarding authentication connection.");
1827 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1828 packet_put_int(remote_id);
1829 packet_put_int(c->self);
1830 }
1831 packet_send();
1832}
Damien Millerb38eff82000-04-01 11:09:21 +10001833
Ben Lindstrombba81212001-06-25 05:01:22 +00001834static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001835client_request_forwarded_tcpip(const char *request_type, int rchan)
1836{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001837 Channel *c = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001838 char *listen_address, *originator_address;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001839 u_short listen_port, originator_port;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001840
1841 /* Get rest of the packet */
1842 listen_address = packet_get_string(NULL);
1843 listen_port = packet_get_int();
1844 originator_address = packet_get_string(NULL);
1845 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001846 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001847
Damien Millerbd740252008-05-19 15:37:09 +10001848 debug("client_request_forwarded_tcpip: listen %s port %d, "
1849 "originator %s port %d", listen_address, listen_port,
1850 originator_address, originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001851
Damien Millerbd740252008-05-19 15:37:09 +10001852 c = channel_connect_by_listen_address(listen_port,
1853 "forwarded-tcpip", originator_address);
1854
Darren Tuckera627d422013-06-02 07:31:17 +10001855 free(originator_address);
1856 free(listen_address);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001857 return c;
1858}
1859
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001860static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001861client_request_x11(const char *request_type, int rchan)
1862{
1863 Channel *c = NULL;
1864 char *originator;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001865 u_short originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001866 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001867
1868 if (!options.forward_x11) {
1869 error("Warning: ssh server tried X11 forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001870 error("Warning: this is probably a break-in attempt by a "
1871 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001872 return NULL;
1873 }
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001874 if (x11_refuse_time != 0 && monotime() >= x11_refuse_time) {
Damien Miller1ab6a512010-06-26 10:02:24 +10001875 verbose("Rejected X11 connection after ForwardX11Timeout "
1876 "expired");
1877 return NULL;
1878 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001879 originator = packet_get_string(NULL);
1880 if (datafellows & SSH_BUG_X11FWD) {
1881 debug2("buggy server: x11 request w/o originator_port");
1882 originator_port = 0;
1883 } else {
1884 originator_port = packet_get_int();
1885 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001886 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001887 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001888 debug("client_request_x11: request from %s %d", originator,
1889 originator_port);
Darren Tuckera627d422013-06-02 07:31:17 +10001890 free(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001891 sock = x11_connect_display();
1892 if (sock < 0)
1893 return NULL;
1894 c = channel_new("x11",
1895 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001896 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001897 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001898 return c;
1899}
1900
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001901static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001902client_request_agent(const char *request_type, int rchan)
1903{
1904 Channel *c = NULL;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001905 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001906
1907 if (!options.forward_agent) {
1908 error("Warning: ssh server tried agent forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001909 error("Warning: this is probably a break-in attempt by a "
1910 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001911 return NULL;
1912 }
Darren Tucker82a3d2b2007-02-19 22:10:25 +11001913 sock = ssh_get_authentication_socket();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001914 if (sock < 0)
1915 return NULL;
1916 c = channel_new("authentication agent connection",
1917 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001918 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001919 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001920 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001921 return c;
1922}
1923
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001924int
1925client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun)
1926{
1927 Channel *c;
1928 int fd;
1929
1930 if (tun_mode == SSH_TUNMODE_NO)
1931 return 0;
1932
1933 if (!compat20) {
Damien Millerb3f2c9f2009-01-28 16:15:30 +11001934 error("Tunnel forwarding is not supported for protocol 1");
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001935 return -1;
1936 }
1937
1938 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1939
1940 /* Open local tunnel device */
1941 if ((fd = tun_open(local_tun, tun_mode)) == -1) {
1942 error("Tunnel device open failed.");
1943 return -1;
1944 }
1945
1946 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1947 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1948 c->datagram = 1;
1949
1950#if defined(SSH_TUN_FILTER)
1951 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1952 channel_register_filter(c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +10001953 sys_tun_outfilter, NULL, NULL);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001954#endif
1955
1956 packet_start(SSH2_MSG_CHANNEL_OPEN);
1957 packet_put_cstring("tun@openssh.com");
1958 packet_put_int(c->self);
1959 packet_put_int(c->local_window_max);
1960 packet_put_int(c->local_maxpacket);
1961 packet_put_int(tun_mode);
1962 packet_put_int(remote_tun);
1963 packet_send();
1964
1965 return 0;
1966}
1967
Damien Millerbd483e72000-04-30 10:00:53 +10001968/* XXXX move to generic input handler */
Ben Lindstrombba81212001-06-25 05:01:22 +00001969static void
Damien Miller630d6f42002-01-22 23:17:30 +11001970client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001971{
1972 Channel *c = NULL;
1973 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001974 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001975 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001976
1977 ctype = packet_get_string(&len);
1978 rchan = packet_get_int();
1979 rwindow = packet_get_int();
1980 rmaxpack = packet_get_int();
1981
Damien Millere247cc42000-05-07 12:03:14 +10001982 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001983 ctype, rchan, rwindow, rmaxpack);
1984
Damien Miller0bc1bd82000-11-13 22:57:25 +11001985 if (strcmp(ctype, "forwarded-tcpip") == 0) {
1986 c = client_request_forwarded_tcpip(ctype, rchan);
1987 } else if (strcmp(ctype, "x11") == 0) {
1988 c = client_request_x11(ctype, rchan);
1989 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1990 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001991 }
1992/* XXX duplicate : */
1993 if (c != NULL) {
1994 debug("confirm %s", ctype);
1995 c->remote_id = rchan;
1996 c->remote_window = rwindow;
1997 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001998 if (c->type != SSH_CHANNEL_CONNECTING) {
1999 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
2000 packet_put_int(c->remote_id);
2001 packet_put_int(c->self);
2002 packet_put_int(c->local_window);
2003 packet_put_int(c->local_maxpacket);
2004 packet_send();
2005 }
Damien Millerbd483e72000-04-30 10:00:53 +10002006 } else {
2007 debug("failure %s", ctype);
2008 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
2009 packet_put_int(rchan);
2010 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00002011 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00002012 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00002013 packet_put_cstring("");
2014 }
Damien Millerbd483e72000-04-30 10:00:53 +10002015 packet_send();
2016 }
Darren Tuckera627d422013-06-02 07:31:17 +10002017 free(ctype);
Damien Millerbd483e72000-04-30 10:00:53 +10002018}
Ben Lindstrombba81212001-06-25 05:01:22 +00002019static void
Damien Miller630d6f42002-01-22 23:17:30 +11002020client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00002021{
2022 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002023 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00002024 char *rtype;
2025
2026 id = packet_get_int();
2027 rtype = packet_get_string(NULL);
2028 reply = packet_get_char();
2029
2030 debug("client_input_channel_req: channel %d rtype %s reply %d",
2031 id, rtype, reply);
2032
Damien Miller3bbd8782004-06-18 22:23:22 +10002033 if (id == -1) {
2034 error("client_input_channel_req: request for channel -1");
2035 } else if ((c = channel_lookup(id)) == NULL) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10002036 error("client_input_channel_req: channel %d: "
2037 "unknown channel", id);
Damien Millerbab9bd42008-05-19 16:06:47 +10002038 } else if (strcmp(rtype, "eow@openssh.com") == 0) {
2039 packet_check_eom();
2040 chan_rcvd_eow(c);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002041 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10002042 exitval = packet_get_int();
Damien Millere1537f92010-01-26 13:26:22 +11002043 if (c->ctl_chan != -1) {
2044 mux_exit_message(c, exitval);
Damien Miller0e220db2004-06-15 10:34:08 +10002045 success = 1;
Darren Tucker29440822010-01-08 17:08:35 +11002046 } else if (id == session_ident) {
2047 /* Record exit value of local session */
2048 success = 1;
2049 exit_status = exitval;
2050 } else {
Damien Miller36f57eb2010-01-30 17:28:34 +11002051 /* Probably for a mux channel that has already closed */
2052 debug("%s: no sink for exit-status on channel %d",
2053 __func__, id);
Damien Miller0e220db2004-06-15 10:34:08 +10002054 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002055 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00002056 }
Damien Millerd0fdd682010-12-01 12:02:14 +11002057 if (reply && c != NULL) {
Ben Lindstrom5b828322001-02-09 01:34:36 +00002058 packet_start(success ?
2059 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller8533c782008-12-08 09:54:40 +11002060 packet_put_int(c->remote_id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002061 packet_send();
2062 }
Darren Tuckera627d422013-06-02 07:31:17 +10002063 free(rtype);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002064}
Damien Millerc3fa4072002-01-22 23:21:58 +11002065static void
2066client_input_global_request(int type, u_int32_t seq, void *ctxt)
2067{
2068 char *rtype;
2069 int want_reply;
2070 int success = 0;
2071
2072 rtype = packet_get_string(NULL);
2073 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11002074 debug("client_input_global_request: rtype %s want_reply %d",
2075 rtype, want_reply);
Damien Millerc3fa4072002-01-22 23:21:58 +11002076 if (want_reply) {
2077 packet_start(success ?
2078 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
2079 packet_send();
2080 packet_write_wait();
2081 }
Darren Tuckera627d422013-06-02 07:31:17 +10002082 free(rtype);
Damien Millerc3fa4072002-01-22 23:21:58 +11002083}
Damien Millerbd483e72000-04-30 10:00:53 +10002084
Damien Miller0e220db2004-06-15 10:34:08 +10002085void
Darren Tuckerfc959702004-07-17 16:12:08 +10002086client_session2_setup(int id, int want_tty, int want_subsystem,
Damien Miller5771ed72008-05-19 15:35:33 +10002087 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env)
Damien Miller0e220db2004-06-15 10:34:08 +10002088{
2089 int len;
Darren Tucker5d78de62004-11-05 20:35:44 +11002090 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002091
2092 debug2("%s: id %d", __func__, id);
2093
Darren Tucker5d78de62004-11-05 20:35:44 +11002094 if ((c = channel_lookup(id)) == NULL)
2095 fatal("client_session2_setup: channel %d: unknown channel", id);
2096
Damien Miller0dac6fb2010-11-20 15:19:38 +11002097 packet_set_interactive(want_tty,
2098 options.ip_qos_interactive, options.ip_qos_bulk);
2099
Damien Miller0e220db2004-06-15 10:34:08 +10002100 if (want_tty) {
2101 struct winsize ws;
Damien Miller0e220db2004-06-15 10:34:08 +10002102
2103 /* Store window size in the packet. */
2104 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
2105 memset(&ws, 0, sizeof(ws));
2106
Damien Miller5771ed72008-05-19 15:35:33 +10002107 channel_request_start(id, "pty-req", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002108 client_expect_confirm(id, "PTY allocation", CONFIRM_TTY);
Damien Miller0e220db2004-06-15 10:34:08 +10002109 packet_put_cstring(term != NULL ? term : "");
Damien Miller71a73672006-03-26 14:04:36 +11002110 packet_put_int((u_int)ws.ws_col);
2111 packet_put_int((u_int)ws.ws_row);
2112 packet_put_int((u_int)ws.ws_xpixel);
2113 packet_put_int((u_int)ws.ws_ypixel);
Darren Tuckerdf189fb2008-06-08 12:55:32 +10002114 if (tiop == NULL)
2115 tiop = get_saved_tio();
2116 tty_make_modes(-1, tiop);
Damien Miller0e220db2004-06-15 10:34:08 +10002117 packet_send();
2118 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11002119 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10002120 }
2121
2122 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10002123 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10002124 int i, j, matched;
Damien Miller0e220db2004-06-15 10:34:08 +10002125 char *name, *val;
2126
2127 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10002128 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10002129 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10002130 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10002131 if ((val = strchr(name, '=')) == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +10002132 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002133 continue;
2134 }
2135 *val++ = '\0';
2136
2137 matched = 0;
2138 for (j = 0; j < options.num_send_env; j++) {
2139 if (match_pattern(name, options.send_env[j])) {
2140 matched = 1;
2141 break;
2142 }
2143 }
2144 if (!matched) {
2145 debug3("Ignored env %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10002146 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002147 continue;
2148 }
2149
2150 debug("Sending env %s = %s", name, val);
2151 channel_request_start(id, "env", 0);
2152 packet_put_cstring(name);
2153 packet_put_cstring(val);
2154 packet_send();
Darren Tuckera627d422013-06-02 07:31:17 +10002155 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002156 }
2157 }
2158
2159 len = buffer_len(cmd);
2160 if (len > 0) {
2161 if (len > 900)
2162 len = 900;
2163 if (want_subsystem) {
Damien Miller5771ed72008-05-19 15:35:33 +10002164 debug("Sending subsystem: %.*s",
2165 len, (u_char*)buffer_ptr(cmd));
2166 channel_request_start(id, "subsystem", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002167 client_expect_confirm(id, "subsystem", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002168 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002169 debug("Sending command: %.*s",
2170 len, (u_char*)buffer_ptr(cmd));
2171 channel_request_start(id, "exec", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002172 client_expect_confirm(id, "exec", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002173 }
2174 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
2175 packet_send();
2176 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002177 channel_request_start(id, "shell", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002178 client_expect_confirm(id, "shell", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002179 packet_send();
2180 }
2181}
2182
Ben Lindstrombba81212001-06-25 05:01:22 +00002183static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002184client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10002185{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002186 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10002187
Damien Miller1383bd82000-04-06 12:32:37 +10002188 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2189 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2190 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2191 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10002192 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10002193 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2194 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002195 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10002196 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerb84886b2008-05-19 15:05:07 +10002197 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2198 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
Damien Millerc3fa4072002-01-22 23:21:58 +11002199 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002200
2201 /* rekeying */
2202 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10002203
2204 /* global request reply messages */
2205 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2206 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10002207}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002208
Ben Lindstrombba81212001-06-25 05:01:22 +00002209static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002210client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002211{
2212 dispatch_init(NULL);
2213 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
2214 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
2215 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10002216 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2217 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
2218 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002219 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
2220 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
2221 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11002222
2223 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
Darren Tucker5dcdd212003-10-02 16:17:00 +10002224 &client_input_agent_open : &deny_input_open);
Damien Miller69b69aa2000-10-28 14:19:58 +11002225 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
2226 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002227}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002228
Ben Lindstrombba81212001-06-25 05:01:22 +00002229static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002230client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002231{
2232 client_init_dispatch_13();
2233 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
2234 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
2235}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002236
Ben Lindstromdb47f382001-07-04 05:10:27 +00002237static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002238client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002239{
Damien Miller1383bd82000-04-06 12:32:37 +10002240 if (compat20)
2241 client_init_dispatch_20();
2242 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10002243 client_init_dispatch_13();
2244 else
2245 client_init_dispatch_15();
2246}
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002247
Damien Miller6c3eec72011-05-05 14:16:22 +10002248void
2249client_stop_mux(void)
2250{
2251 if (options.control_path != NULL && muxserver_sock != -1)
2252 unlink(options.control_path);
2253 /*
Darren Tucker48bf4b02012-09-07 16:38:53 +10002254 * If we are in persist mode, or don't have a shell, signal that we
2255 * should close when all active channels are closed.
Damien Miller6c3eec72011-05-05 14:16:22 +10002256 */
Darren Tucker48bf4b02012-09-07 16:38:53 +10002257 if (options.control_persist || no_shell_flag) {
Damien Miller6c3eec72011-05-05 14:16:22 +10002258 session_closed = 1;
Damien Miller4ac99c32011-06-20 14:43:31 +10002259 setproctitle("[stopped mux]");
2260 }
Damien Miller6c3eec72011-05-05 14:16:22 +10002261}
2262
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002263/* client specific fatal cleanup */
2264void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002265cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002266{
Damien Miller21771e22011-05-15 08:45:50 +10002267 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002268 leave_non_blocking();
Damien Millerb1cbfa22008-05-19 16:00:08 +10002269 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10002270 unlink(options.control_path);
Damien Millera41ccca2010-10-07 22:07:32 +11002271 ssh_kill_proxy_command();
Darren Tucker3e33cec2003-10-02 16:12:36 +10002272 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002273}