blob: 7b7349bd76c27da235c97a355d2e6dd39440c496 [file] [log] [blame]
Damien Miller4ac99c32011-06-20 14:43:31 +10001/* $OpenBSD: clientloop.c,v 1.235 2011/06/17 21:57:25 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 Miller555f3b82011-05-15 08:48:05 +1000177enum confirm_action { CONFIRM_WARN = 0, CONFIRM_CLOSE, CONFIRM_TTY };
Damien Miller5771ed72008-05-19 15:35:33 +1000178struct channel_reply_ctx {
179 const char *request_type;
Damien Miller555f3b82011-05-15 08:48:05 +1000180 int id;
181 enum confirm_action action;
Damien Miller5771ed72008-05-19 15:35:33 +1000182};
183
Darren Tucker9f407c42008-06-13 04:50:27 +1000184/* Global request success/failure callbacks */
185struct global_confirm {
186 TAILQ_ENTRY(global_confirm) entry;
187 global_confirm_cb *cb;
188 void *ctx;
189 int ref_count;
190};
191TAILQ_HEAD(global_confirms, global_confirm);
192static struct global_confirms global_confirms =
193 TAILQ_HEAD_INITIALIZER(global_confirms);
194
Ben Lindstromf28f6342001-04-04 02:03:04 +0000195/*XXX*/
196extern Kex *xxx_kex;
197
Damien Miller0e220db2004-06-15 10:34:08 +1000198void ssh_process_session2_setup(int, int, int, Buffer *);
199
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200/* Restores stdin to blocking mode. */
201
Ben Lindstrombba81212001-06-25 05:01:22 +0000202static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000203leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204{
Damien Miller95def091999-11-25 00:26:21 +1100205 if (in_non_blocking_mode) {
Damien Miller03e66f62004-06-15 15:47:51 +1000206 unset_nonblock(fileno(stdin));
Damien Miller95def091999-11-25 00:26:21 +1100207 in_non_blocking_mode = 0;
Damien Miller95def091999-11-25 00:26:21 +1100208 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209}
210
Damien Miller95def091999-11-25 00:26:21 +1100211/* Puts stdin terminal in non-blocking mode. */
212
Ben Lindstrombba81212001-06-25 05:01:22 +0000213static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000214enter_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215{
Damien Miller95def091999-11-25 00:26:21 +1100216 in_non_blocking_mode = 1;
Damien Miller232711f2004-06-15 10:35:30 +1000217 set_nonblock(fileno(stdin));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000218}
219
Damien Miller5428f641999-11-25 11:54:57 +1100220/*
221 * Signal handler for the window change signal (SIGWINCH). This just sets a
222 * flag indicating that the window has changed.
223 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100224/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000225static void
Damien Miller95def091999-11-25 00:26:21 +1100226window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000227{
Damien Miller95def091999-11-25 00:26:21 +1100228 received_window_change_signal = 1;
229 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230}
231
Damien Miller5428f641999-11-25 11:54:57 +1100232/*
233 * Signal handler for signals that cause the program to terminate. These
234 * signals must be trapped to restore terminal modes.
235 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100236/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000237static void
Damien Miller95def091999-11-25 00:26:21 +1100238signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000240 received_signal = sig;
241 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242}
243
Damien Miller5428f641999-11-25 11:54:57 +1100244/*
245 * Returns current time in seconds from Jan 1, 1970 with the maximum
246 * available resolution.
247 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248
Ben Lindstrombba81212001-06-25 05:01:22 +0000249static double
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000250get_current_time(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251{
Damien Miller95def091999-11-25 00:26:21 +1100252 struct timeval tv;
253 gettimeofday(&tv, NULL);
254 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000255}
256
Damien Millere11e1ea2010-08-03 16:04:46 +1000257/*
258 * Sets control_persist_exit_time to the absolute time when the
259 * backgrounded control master should exit due to expiry of the
260 * ControlPersist timeout. Sets it to 0 if we are not a backgrounded
261 * control master process, or if there is no ControlPersist timeout.
262 */
263static void
264set_control_persist_exit_time(void)
265{
266 if (muxserver_sock == -1 || !options.control_persist
Damien Miller6c3eec72011-05-05 14:16:22 +1000267 || options.control_persist_timeout == 0) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000268 /* not using a ControlPersist timeout */
269 control_persist_exit_time = 0;
Damien Miller6c3eec72011-05-05 14:16:22 +1000270 } else if (channel_still_open()) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000271 /* some client connections are still open */
272 if (control_persist_exit_time > 0)
273 debug2("%s: cancel scheduled exit", __func__);
274 control_persist_exit_time = 0;
275 } else if (control_persist_exit_time <= 0) {
276 /* a client connection has recently closed */
277 control_persist_exit_time = time(NULL) +
278 (time_t)options.control_persist_timeout;
279 debug2("%s: schedule exit in %d seconds", __func__,
280 options.control_persist_timeout);
281 }
282 /* else we are already counting down to the timeout */
283}
284
Damien Miller17e7ed02005-06-17 12:54:33 +1000285#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
286void
287client_x11_get_proto(const char *display, const char *xauth_path,
Damien Miller1ab6a512010-06-26 10:02:24 +1000288 u_int trusted, u_int timeout, char **_proto, char **_data)
Damien Miller17e7ed02005-06-17 12:54:33 +1000289{
290 char cmd[1024];
291 char line[512];
292 char xdisplay[512];
293 static char proto[512], data[512];
294 FILE *f;
295 int got_data = 0, generated = 0, do_unlink = 0, i;
296 char *xauthdir, *xauthfile;
297 struct stat st;
Damien Miller1ab6a512010-06-26 10:02:24 +1000298 u_int now;
Damien Miller17e7ed02005-06-17 12:54:33 +1000299
300 xauthdir = xauthfile = NULL;
301 *_proto = proto;
302 *_data = data;
303 proto[0] = data[0] = '\0';
304
305 if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) {
306 debug("No xauth program.");
307 } else {
308 if (display == NULL) {
309 debug("x11_get_proto: DISPLAY not set");
310 return;
311 }
312 /*
313 * Handle FamilyLocal case where $DISPLAY does
314 * not match an authorization entry. For this we
315 * just try "xauth list unix:displaynum.screennum".
316 * XXX: "localhost" match to determine FamilyLocal
317 * is not perfect.
318 */
319 if (strncmp(display, "localhost:", 10) == 0) {
320 snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
321 display + 10);
322 display = xdisplay;
323 }
324 if (trusted == 0) {
325 xauthdir = xmalloc(MAXPATHLEN);
326 xauthfile = xmalloc(MAXPATHLEN);
Damien Miller2cd62932010-12-01 11:50:35 +1100327 mktemp_proto(xauthdir, MAXPATHLEN);
Damien Miller17e7ed02005-06-17 12:54:33 +1000328 if (mkdtemp(xauthdir) != NULL) {
329 do_unlink = 1;
330 snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile",
331 xauthdir);
332 snprintf(cmd, sizeof(cmd),
333 "%s -f %s generate %s " SSH_X11_PROTO
Damien Miller1ab6a512010-06-26 10:02:24 +1000334 " untrusted timeout %u 2>" _PATH_DEVNULL,
335 xauth_path, xauthfile, display, timeout);
Damien Miller17e7ed02005-06-17 12:54:33 +1000336 debug2("x11_get_proto: %s", cmd);
337 if (system(cmd) == 0)
338 generated = 1;
Damien Miller1ab6a512010-06-26 10:02:24 +1000339 if (x11_refuse_time == 0) {
340 now = time(NULL) + 1;
341 if (UINT_MAX - timeout < now)
342 x11_refuse_time = UINT_MAX;
343 else
344 x11_refuse_time = now + timeout;
345 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000346 }
347 }
Darren Tucker513d13a2007-08-15 19:13:41 +1000348
349 /*
350 * When in untrusted mode, we read the cookie only if it was
351 * successfully generated as an untrusted one in the step
352 * above.
353 */
354 if (trusted || generated) {
355 snprintf(cmd, sizeof(cmd),
356 "%s %s%s list %s 2>" _PATH_DEVNULL,
357 xauth_path,
358 generated ? "-f " : "" ,
359 generated ? xauthfile : "",
360 display);
361 debug2("x11_get_proto: %s", cmd);
362 f = popen(cmd, "r");
363 if (f && fgets(line, sizeof(line), f) &&
364 sscanf(line, "%*s %511s %511s", proto, data) == 2)
365 got_data = 1;
366 if (f)
367 pclose(f);
368 } else
369 error("Warning: untrusted X11 forwarding setup failed: "
370 "xauth key data not generated");
Damien Miller17e7ed02005-06-17 12:54:33 +1000371 }
372
373 if (do_unlink) {
374 unlink(xauthfile);
375 rmdir(xauthdir);
376 }
377 if (xauthdir)
378 xfree(xauthdir);
379 if (xauthfile)
380 xfree(xauthfile);
381
382 /*
383 * If we didn't get authentication data, just make up some
384 * data. The forwarding code will check the validity of the
385 * response anyway, and substitute this data. The X11
386 * server, however, will ignore this fake data and use
387 * whatever authentication mechanisms it was using otherwise
388 * for the local connection.
389 */
390 if (!got_data) {
391 u_int32_t rnd = 0;
392
393 logit("Warning: No xauth data; "
394 "using fake authentication data for X11 forwarding.");
395 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
396 for (i = 0; i < 16; i++) {
397 if (i % 4 == 0)
398 rnd = arc4random();
399 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
400 rnd & 0xff);
401 rnd >>= 8;
402 }
403 }
404}
405
Damien Miller5428f641999-11-25 11:54:57 +1100406/*
407 * This is called when the interactive is entered. This checks if there is
408 * an EOF coming on stdin. We must check this explicitly, as select() does
409 * not appear to wake up when redirecting from /dev/null.
410 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000411
Ben Lindstrombba81212001-06-25 05:01:22 +0000412static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000413client_check_initial_eof_on_stdin(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000414{
Damien Miller95def091999-11-25 00:26:21 +1100415 int len;
416 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417
Damien Miller5428f641999-11-25 11:54:57 +1100418 /*
419 * If standard input is to be "redirected from /dev/null", we simply
420 * mark that we have seen an EOF and send an EOF message to the
421 * server. Otherwise, we try to read a single character; it appears
422 * that for some files, such /dev/null, select() never wakes up for
423 * read for this descriptor, which means that we never get EOF. This
424 * way we will get the EOF if stdin comes from /dev/null or similar.
425 */
Damien Miller95def091999-11-25 00:26:21 +1100426 if (stdin_null_flag) {
427 /* Fake EOF on stdin. */
428 debug("Sending eof.");
429 stdin_eof = 1;
430 packet_start(SSH_CMSG_EOF);
431 packet_send();
432 } else {
Damien Miller95def091999-11-25 00:26:21 +1100433 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434
Damien Miller95def091999-11-25 00:26:21 +1100435 /* Check for immediate EOF on stdin. */
436 len = read(fileno(stdin), buf, 1);
437 if (len == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000438 /*
439 * EOF. Record that we have seen it and send
440 * EOF to server.
441 */
Damien Miller95def091999-11-25 00:26:21 +1100442 debug("Sending eof.");
443 stdin_eof = 1;
444 packet_start(SSH_CMSG_EOF);
445 packet_send();
446 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100447 /*
448 * Got data. We must store the data in the buffer,
449 * and also process it as an escape character if
450 * appropriate.
451 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000452 if ((u_char) buf[0] == escape_char1)
453 escape_pending1 = 1;
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000454 else
Damien Miller95def091999-11-25 00:26:21 +1100455 buffer_append(&stdin_buffer, buf, 1);
Damien Miller95def091999-11-25 00:26:21 +1100456 }
Damien Miller95def091999-11-25 00:26:21 +1100457 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000458 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000459}
460
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461
Damien Miller5428f641999-11-25 11:54:57 +1100462/*
463 * Make packets from buffered stdin data, and buffer them for sending to the
464 * connection.
465 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000466
Ben Lindstrombba81212001-06-25 05:01:22 +0000467static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000468client_make_packets_from_stdin_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000469{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000470 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471
Damien Miller95def091999-11-25 00:26:21 +1100472 /* Send buffered stdin data to the server. */
473 while (buffer_len(&stdin_buffer) > 0 &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100474 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100475 len = buffer_len(&stdin_buffer);
476 /* Keep the packets at reasonable size. */
477 if (len > packet_get_maxsize())
478 len = packet_get_maxsize();
479 packet_start(SSH_CMSG_STDIN_DATA);
480 packet_put_string(buffer_ptr(&stdin_buffer), len);
481 packet_send();
482 buffer_consume(&stdin_buffer, len);
483 /* If we have a pending EOF, send it now. */
484 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
485 packet_start(SSH_CMSG_EOF);
486 packet_send();
487 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000488 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489}
490
Damien Miller5428f641999-11-25 11:54:57 +1100491/*
492 * Checks if the client window has changed, and sends a packet about it to
493 * the server if so. The actual change is detected elsewhere (by a software
494 * interrupt on Unix); this just checks the flag and sends a message if
495 * appropriate.
496 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000497
Ben Lindstrombba81212001-06-25 05:01:22 +0000498static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000499client_check_window_change(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000500{
Damien Miller1383bd82000-04-06 12:32:37 +1000501 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000502
Damien Miller1383bd82000-04-06 12:32:37 +1000503 if (! received_window_change_signal)
504 return;
505 /** XXX race */
506 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000507
Damien Millerd3444942000-09-30 14:20:03 +1100508 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000509
510 if (compat20) {
Damien Miller0e220db2004-06-15 10:34:08 +1000511 channel_send_window_changes();
Damien Miller1383bd82000-04-06 12:32:37 +1000512 } else {
Damien Miller0e220db2004-06-15 10:34:08 +1000513 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
514 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000515 packet_start(SSH_CMSG_WINDOW_SIZE);
Damien Miller71a73672006-03-26 14:04:36 +1100516 packet_put_int((u_int)ws.ws_row);
517 packet_put_int((u_int)ws.ws_col);
518 packet_put_int((u_int)ws.ws_xpixel);
519 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller1383bd82000-04-06 12:32:37 +1000520 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000521 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000522}
523
Damien Miller509b0102003-12-17 16:33:10 +1100524static void
525client_global_request_reply(int type, u_int32_t seq, void *ctxt)
526{
Darren Tucker9f407c42008-06-13 04:50:27 +1000527 struct global_confirm *gc;
528
529 if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
530 return;
531 if (gc->cb != NULL)
532 gc->cb(type, seq, gc->ctx);
533 if (--gc->ref_count <= 0) {
534 TAILQ_REMOVE(&global_confirms, gc, entry);
535 bzero(gc, sizeof(*gc));
536 xfree(gc);
537 }
538
Darren Tuckerf7288d72009-06-21 18:12:20 +1000539 packet_set_alive_timeouts(0);
Damien Miller509b0102003-12-17 16:33:10 +1100540}
541
542static void
543server_alive_check(void)
544{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000545 if (packet_inc_alive_timeouts() > options.server_alive_count_max) {
Damien Millerb73b6fd2011-01-11 17:18:56 +1100546 logit("Timeout, server %s not responding.", host);
Damien Miller985a4482006-10-24 03:02:41 +1000547 cleanup_exit(255);
548 }
Damien Miller509b0102003-12-17 16:33:10 +1100549 packet_start(SSH2_MSG_GLOBAL_REQUEST);
550 packet_put_cstring("keepalive@openssh.com");
551 packet_put_char(1); /* boolean: want reply */
552 packet_send();
Darren Tucker9f407c42008-06-13 04:50:27 +1000553 /* Insert an empty placeholder to maintain ordering */
554 client_register_global_confirm(NULL, NULL);
Damien Miller509b0102003-12-17 16:33:10 +1100555}
556
Damien Miller5428f641999-11-25 11:54:57 +1100557/*
558 * Waits until the client can do something (some data becomes available on
559 * one of the file descriptors).
560 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000561static void
Damien Miller5e953212001-01-30 09:14:00 +1100562client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000563 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000564{
Damien Miller509b0102003-12-17 16:33:10 +1100565 struct timeval tv, *tvp;
Damien Millere11e1ea2010-08-03 16:04:46 +1000566 int timeout_secs;
Damien Miller509b0102003-12-17 16:33:10 +1100567 int ret;
568
Damien Miller5e953212001-01-30 09:14:00 +1100569 /* Add any selections by the channel mechanism. */
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000570 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000571
Damien Miller1383bd82000-04-06 12:32:37 +1000572 if (!compat20) {
573 /* Read from the connection, unless our buffers are full. */
574 if (buffer_len(&stdout_buffer) < buffer_high &&
575 buffer_len(&stderr_buffer) < buffer_high &&
576 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100577 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000578 /*
579 * Read from stdin, unless we have seen EOF or have very much
580 * buffered data to send to the server.
581 */
582 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100583 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000584
585 /* Select stdout/stderr if have data in buffer. */
586 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100587 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000588 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100589 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000590 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000591 /* channel_prepare_select could have closed the last channel */
Damien Miller164a7f42001-10-12 11:36:09 +1000592 if (session_closed && !channel_still_open() &&
593 !packet_have_data_to_write()) {
594 /* clear mask since we did not call select() */
Damien Miller79faeff2001-11-12 11:06:32 +1100595 memset(*readsetp, 0, *nallocp);
596 memset(*writesetp, 0, *nallocp);
Damien Miller164a7f42001-10-12 11:36:09 +1000597 return;
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000598 } else {
599 FD_SET(connection_in, *readsetp);
600 }
Damien Miller1383bd82000-04-06 12:32:37 +1000601 }
602
Damien Miller95def091999-11-25 00:26:21 +1100603 /* Select server connection if have data to write to the server. */
604 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100605 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100606
Damien Miller5428f641999-11-25 11:54:57 +1100607 /*
608 * Wait for something to happen. This will suspend the process until
609 * some selected descriptor can be read, written, or has some other
Damien Millere11e1ea2010-08-03 16:04:46 +1000610 * event pending, or a timeout expires.
Damien Miller5428f641999-11-25 11:54:57 +1100611 */
Damien Miller95def091999-11-25 00:26:21 +1100612
Damien Millere11e1ea2010-08-03 16:04:46 +1000613 timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */
614 if (options.server_alive_interval > 0 && compat20)
615 timeout_secs = options.server_alive_interval;
616 set_control_persist_exit_time();
617 if (control_persist_exit_time > 0) {
618 timeout_secs = MIN(timeout_secs,
619 control_persist_exit_time - time(NULL));
620 if (timeout_secs < 0)
621 timeout_secs = 0;
622 }
623 if (timeout_secs == INT_MAX)
Damien Miller509b0102003-12-17 16:33:10 +1100624 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000625 else {
Damien Millere11e1ea2010-08-03 16:04:46 +1000626 tv.tv_sec = timeout_secs;
Damien Miller509b0102003-12-17 16:33:10 +1100627 tv.tv_usec = 0;
628 tvp = &tv;
629 }
Damien Millere11e1ea2010-08-03 16:04:46 +1000630
Damien Miller509b0102003-12-17 16:33:10 +1100631 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
632 if (ret < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100633 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000634
635 /*
636 * We have to clear the select masks, because we return.
637 * We have to return, because the mainloop checks for the flags
638 * set by the signal handlers.
639 */
Damien Miller79faeff2001-11-12 11:06:32 +1100640 memset(*readsetp, 0, *nallocp);
641 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000642
Damien Miller95def091999-11-25 00:26:21 +1100643 if (errno == EINTR)
644 return;
645 /* Note: we might still have data in the buffers. */
646 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
647 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100648 quit_pending = 1;
Damien Miller509b0102003-12-17 16:33:10 +1100649 } else if (ret == 0)
650 server_alive_check();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000651}
652
Ben Lindstrombba81212001-06-25 05:01:22 +0000653static void
Damien Millerad833b32000-08-23 10:46:23 +1000654client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000655{
Damien Miller95def091999-11-25 00:26:21 +1100656 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000657 if (buffer_len(bout) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000658 atomicio(vwrite, fileno(stdout), buffer_ptr(bout),
659 buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000660 if (buffer_len(berr) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000661 atomicio(vwrite, fileno(stderr), buffer_ptr(berr),
662 buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000663
Damien Miller21771e22011-05-15 08:45:50 +1000664 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000665
Damien Miller5428f641999-11-25 11:54:57 +1100666 /*
667 * Free (and clear) the buffer to reduce the amount of data that gets
668 * written to swap.
669 */
Damien Millerad833b32000-08-23 10:46:23 +1000670 buffer_free(bin);
671 buffer_free(bout);
672 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000673
Damien Miller95def091999-11-25 00:26:21 +1100674 /* Send the suspend signal to the program itself. */
675 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000676
Darren Tucker5d78de62004-11-05 20:35:44 +1100677 /* Reset window sizes in case they have changed */
678 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000679
Damien Miller95def091999-11-25 00:26:21 +1100680 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000681 buffer_init(bin);
682 buffer_init(bout);
683 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000684
Damien Miller21771e22011-05-15 08:45:50 +1000685 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000686}
687
Ben Lindstrombba81212001-06-25 05:01:22 +0000688static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100689client_process_net_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000690{
Darren Tuckerc5564e12009-06-21 18:53:53 +1000691 int len, cont = 0;
Darren Tucker86e30a02009-08-28 11:21:06 +1000692 char buf[SSH_IOBUFSZ];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000693
Damien Miller5428f641999-11-25 11:54:57 +1100694 /*
695 * Read input from the server, and add any such data to the buffer of
696 * the packet subsystem.
697 */
Damien Miller95def091999-11-25 00:26:21 +1100698 if (FD_ISSET(connection_in, readset)) {
699 /* Read as much as possible. */
Darren Tuckerc5564e12009-06-21 18:53:53 +1000700 len = roaming_read(connection_in, buf, sizeof(buf), &cont);
701 if (len == 0 && cont == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000702 /*
703 * Received EOF. The remote host has closed the
704 * connection.
705 */
706 snprintf(buf, sizeof buf,
707 "Connection to %.300s closed by remote host.\r\n",
708 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000709 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000710 quit_pending = 1;
711 return;
Damien Miller95def091999-11-25 00:26:21 +1100712 }
Damien Miller5428f641999-11-25 11:54:57 +1100713 /*
714 * There is a kernel bug on Solaris that causes select to
715 * sometimes wake up even though there is no data available.
716 */
Damien Millerd8968ad2008-07-04 23:10:49 +1000717 if (len < 0 &&
718 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Damien Miller95def091999-11-25 00:26:21 +1100719 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000720
Damien Miller95def091999-11-25 00:26:21 +1100721 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000722 /*
723 * An error has encountered. Perhaps there is a
724 * network problem.
725 */
726 snprintf(buf, sizeof buf,
727 "Read from remote host %.300s: %.100s\r\n",
728 host, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100729 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100730 quit_pending = 1;
731 return;
732 }
733 packet_process_incoming(buf, len);
734 }
Damien Miller1383bd82000-04-06 12:32:37 +1000735}
736
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000737static void
Damien Miller5771ed72008-05-19 15:35:33 +1000738client_status_confirm(int type, Channel *c, void *ctx)
Damien Miller0e220db2004-06-15 10:34:08 +1000739{
Damien Miller5771ed72008-05-19 15:35:33 +1000740 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
741 char errmsg[256];
742 int tochan;
Darren Tuckerfc959702004-07-17 16:12:08 +1000743
Damien Miller555f3b82011-05-15 08:48:05 +1000744 /*
745 * If a TTY was explicitly requested, then a failure to allocate
746 * one is fatal.
747 */
748 if (cr->action == CONFIRM_TTY &&
749 (options.request_tty == REQUEST_TTY_FORCE ||
750 options.request_tty == REQUEST_TTY_YES))
751 cr->action = CONFIRM_CLOSE;
752
Damien Miller5771ed72008-05-19 15:35:33 +1000753 /* XXX supress on mux _client_ quietmode */
754 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
Damien Millere1537f92010-01-26 13:26:22 +1100755 c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
Damien Miller0e220db2004-06-15 10:34:08 +1000756
Damien Miller5771ed72008-05-19 15:35:33 +1000757 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
758 debug2("%s request accepted on channel %d",
759 cr->request_type, c->self);
760 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
761 if (tochan) {
762 snprintf(errmsg, sizeof(errmsg),
763 "%s request failed\r\n", cr->request_type);
764 } else {
765 snprintf(errmsg, sizeof(errmsg),
766 "%s request failed on channel %d",
767 cr->request_type, c->self);
768 }
769 /* If error occurred on primary session channel, then exit */
Damien Miller555f3b82011-05-15 08:48:05 +1000770 if (cr->action == CONFIRM_CLOSE && c->self == session_ident)
Damien Miller5771ed72008-05-19 15:35:33 +1000771 fatal("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000772 /*
773 * If error occurred on mux client, append to
774 * their stderr.
775 */
776 if (tochan) {
777 buffer_append(&c->extended, errmsg,
778 strlen(errmsg));
779 } else
Damien Miller5771ed72008-05-19 15:35:33 +1000780 error("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000781 if (cr->action == CONFIRM_TTY) {
782 /*
783 * If a TTY allocation error occurred, then arrange
784 * for the correct TTY to leave raw mode.
785 */
786 if (c->self == session_ident)
787 leave_raw_mode(0);
788 else
789 mux_tty_alloc_failed(c);
790 } else if (cr->action == CONFIRM_CLOSE) {
Damien Miller5771ed72008-05-19 15:35:33 +1000791 chan_read_failed(c);
792 chan_write_failed(c);
793 }
Damien Miller0e220db2004-06-15 10:34:08 +1000794 }
Damien Miller5771ed72008-05-19 15:35:33 +1000795 xfree(cr);
796}
Damien Miller0e220db2004-06-15 10:34:08 +1000797
Damien Miller5771ed72008-05-19 15:35:33 +1000798static void
799client_abandon_status_confirm(Channel *c, void *ctx)
800{
801 xfree(ctx);
802}
803
804static void
Damien Miller555f3b82011-05-15 08:48:05 +1000805client_expect_confirm(int id, const char *request,
806 enum confirm_action action)
Damien Miller5771ed72008-05-19 15:35:33 +1000807{
808 struct channel_reply_ctx *cr = xmalloc(sizeof(*cr));
809
810 cr->request_type = request;
Damien Miller555f3b82011-05-15 08:48:05 +1000811 cr->action = action;
Damien Miller5771ed72008-05-19 15:35:33 +1000812
813 channel_register_status_confirm(id, client_status_confirm,
814 client_abandon_status_confirm, cr);
Damien Miller0e220db2004-06-15 10:34:08 +1000815}
816
Darren Tucker9f407c42008-06-13 04:50:27 +1000817void
818client_register_global_confirm(global_confirm_cb *cb, void *ctx)
819{
Damien Millerb9d3bee2008-07-16 22:40:52 +1000820 struct global_confirm *gc, *last_gc;
Darren Tucker9f407c42008-06-13 04:50:27 +1000821
822 /* Coalesce identical callbacks */
Damien Millerb9d3bee2008-07-16 22:40:52 +1000823 last_gc = TAILQ_LAST(&global_confirms, global_confirms);
824 if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
825 if (++last_gc->ref_count >= INT_MAX)
826 fatal("%s: last_gc->ref_count = %d",
827 __func__, last_gc->ref_count);
Darren Tucker9f407c42008-06-13 04:50:27 +1000828 return;
829 }
830
831 gc = xmalloc(sizeof(*gc));
832 gc->cb = cb;
833 gc->ctx = ctx;
834 gc->ref_count = 1;
835 TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
836}
837
Damien Miller0e220db2004-06-15 10:34:08 +1000838static void
Ben Lindstrom681d9322002-03-22 03:53:00 +0000839process_cmdline(void)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000840{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000841 void (*handler)(int);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100842 char *s, *cmd, *cancel_host;
Darren Tuckere7066df2004-05-24 10:18:05 +1000843 int delete = 0;
Damien Miller0164cb82008-11-05 16:30:31 +1100844 int local = 0, remote = 0, dynamic = 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100845 int cancel_port;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100846 Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000847
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100848 bzero(&fwd, sizeof(fwd));
849 fwd.listen_host = fwd.connect_host = NULL;
850
Damien Miller21771e22011-05-15 08:45:50 +1000851 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000852 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000853 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000854 if (s == NULL)
855 goto out;
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100856 while (isspace(*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000857 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000858 if (*s == '-')
859 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000860 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000861 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000862
Darren Tucker1973c882004-05-24 10:34:36 +1000863 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000864 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000865 logit(" -L[bind_address:]port:host:hostport "
866 "Request local forward");
867 logit(" -R[bind_address:]port:host:hostport "
868 "Request remote forward");
Damien Miller0164cb82008-11-05 16:30:31 +1100869 logit(" -D[bind_address:]port "
870 "Request dynamic forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000871 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000872 "Cancel remote forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100873 if (!options.permit_local_command)
874 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000875 logit(" !args "
876 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100877 goto out;
878 }
879
880 if (*s == '!' && options.permit_local_command) {
881 s++;
882 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000883 goto out;
884 }
885
886 if (*s == 'K') {
887 delete = 1;
888 s++;
889 }
Damien Miller0164cb82008-11-05 16:30:31 +1100890 if (*s == 'L')
891 local = 1;
892 else if (*s == 'R')
893 remote = 1;
894 else if (*s == 'D')
895 dynamic = 1;
896 else {
Damien Miller996acd22003-04-09 20:59:48 +1000897 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000898 goto out;
899 }
Damien Miller0164cb82008-11-05 16:30:31 +1100900
901 if ((local || dynamic) && delete) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000902 logit("Not supported.");
903 goto out;
904 }
Damien Miller0164cb82008-11-05 16:30:31 +1100905 if (remote && delete && !compat20) {
Damien Miller996acd22003-04-09 20:59:48 +1000906 logit("Not supported for SSH protocol version 1.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000907 goto out;
908 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000909
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100910 while (isspace(*++s))
911 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000912
Damien Millere1537f92010-01-26 13:26:22 +1100913 /* XXX update list of forwards in options */
Darren Tuckere7066df2004-05-24 10:18:05 +1000914 if (delete) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100915 cancel_port = 0;
916 cancel_host = hpdelim(&s); /* may be NULL */
917 if (s != NULL) {
918 cancel_port = a2port(s);
919 cancel_host = cleanhostname(cancel_host);
920 } else {
921 cancel_port = a2port(cancel_host);
922 cancel_host = NULL;
923 }
Damien Miller3dc71ad2009-01-28 16:31:22 +1100924 if (cancel_port <= 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100925 logit("Bad forwarding close port");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000926 goto out;
927 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100928 channel_request_rforward_cancel(cancel_host, cancel_port);
Darren Tuckere7066df2004-05-24 10:18:05 +1000929 } else {
Damien Miller4bf648f2009-02-14 16:28:21 +1100930 if (!parse_forward(&fwd, s, dynamic, remote)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000931 logit("Bad forwarding specification.");
932 goto out;
933 }
Damien Miller0164cb82008-11-05 16:30:31 +1100934 if (local || dynamic) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100935 if (channel_setup_local_fwd_listener(fwd.listen_host,
936 fwd.listen_port, fwd.connect_host,
937 fwd.connect_port, options.gateway_ports) < 0) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000938 logit("Port forwarding failed.");
939 goto out;
940 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100941 } else {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000942 if (channel_request_remote_forwarding(fwd.listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100943 fwd.listen_port, fwd.connect_host,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000944 fwd.connect_port) < 0) {
945 logit("Port forwarding failed.");
946 goto out;
947 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100948 }
949
Darren Tuckere7066df2004-05-24 10:18:05 +1000950 logit("Forwarding port.");
951 }
952
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000953out:
954 signal(SIGINT, handler);
Damien Miller21771e22011-05-15 08:45:50 +1000955 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000956 if (cmd)
957 xfree(cmd);
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100958 if (fwd.listen_host != NULL)
959 xfree(fwd.listen_host);
960 if (fwd.connect_host != NULL)
961 xfree(fwd.connect_host);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000962}
963
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000964/*
965 * Process the characters one by one, call with c==NULL for proto1 case.
966 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000967static int
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000968process_escapes(Channel *c, Buffer *bin, Buffer *bout, Buffer *berr,
969 char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +1000970{
971 char string[1024];
972 pid_t pid;
973 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000974 u_int i;
975 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +1000976 char *s;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000977 int *escape_pendingp, escape_char;
978 struct escape_filter_ctx *efc;
Damien Millerad833b32000-08-23 10:46:23 +1000979
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000980 if (c == NULL) {
981 escape_pendingp = &escape_pending1;
982 escape_char = escape_char1;
983 } else {
984 if (c->filter_ctx == NULL)
985 return 0;
986 efc = (struct escape_filter_ctx *)c->filter_ctx;
987 escape_pendingp = &efc->escape_pending;
988 escape_char = efc->escape_char;
989 }
990
Damien Millereccb9de2005-06-17 12:59:34 +1000991 if (len <= 0)
992 return (0);
993
994 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +1000995 /* Get one character at a time. */
996 ch = buf[i];
997
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000998 if (*escape_pendingp) {
Damien Millerad833b32000-08-23 10:46:23 +1000999 /* We have previously seen an escape character. */
1000 /* Clear the flag now. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001001 *escape_pendingp = 0;
Damien Millerad833b32000-08-23 10:46:23 +10001002
1003 /* Process the escaped character. */
1004 switch (ch) {
1005 case '.':
1006 /* Terminate the connection. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001007 snprintf(string, sizeof string, "%c.\r\n",
1008 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001009 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001010
Damien Millere1537f92010-01-26 13:26:22 +11001011 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001012 chan_read_failed(c);
1013 chan_write_failed(c);
1014 return 0;
1015 } else
1016 quit_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001017 return -1;
1018
1019 case 'Z' - 64:
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001020 /* XXX support this for mux clients */
Damien Millere1537f92010-01-26 13:26:22 +11001021 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001022 noescape:
1023 snprintf(string, sizeof string,
1024 "%c%c escape not available to "
1025 "multiplexed sessions\r\n",
1026 escape_char, ch);
1027 buffer_append(berr, string,
1028 strlen(string));
1029 continue;
1030 }
Darren Tucker4d5cd332008-06-13 04:51:14 +10001031 /* Suspend the program. Inform the user */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001032 snprintf(string, sizeof string,
1033 "%c^Z [suspend ssh]\r\n", escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001034 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001035
1036 /* Restore terminal modes and suspend. */
1037 client_suspend_self(bin, bout, berr);
1038
1039 /* We have been continued. */
1040 continue;
1041
Damien Miller54c45982003-05-15 10:20:13 +10001042 case 'B':
1043 if (compat20) {
1044 snprintf(string, sizeof string,
1045 "%cB\r\n", escape_char);
1046 buffer_append(berr, string,
1047 strlen(string));
1048 channel_request_start(session_ident,
1049 "break", 0);
1050 packet_put_int(1000);
1051 packet_send();
1052 }
1053 continue;
1054
Ben Lindstromf28f6342001-04-04 02:03:04 +00001055 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001056 if (compat20) {
1057 if (datafellows & SSH_BUG_NOREKEY)
Darren Tucker4d5cd332008-06-13 04:51:14 +10001058 logit("Server does not "
1059 "support re-keying");
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001060 else
1061 need_rekeying = 1;
1062 }
Ben Lindstromf28f6342001-04-04 02:03:04 +00001063 continue;
1064
Damien Millerad833b32000-08-23 10:46:23 +10001065 case '&':
Damien Millere1537f92010-01-26 13:26:22 +11001066 if (c && c->ctl_chan != -1)
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001067 goto noescape;
Damien Millerad833b32000-08-23 10:46:23 +10001068 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001069 * Detach the program (continue to serve
1070 * connections, but put in background and no
1071 * more new connections).
Damien Millerad833b32000-08-23 10:46:23 +10001072 */
Damien Miller96507ef2001-11-12 10:52:25 +11001073 /* Restore tty modes. */
Damien Miller21771e22011-05-15 08:45:50 +10001074 leave_raw_mode(
1075 options.request_tty == REQUEST_TTY_FORCE);
Damien Miller96507ef2001-11-12 10:52:25 +11001076
1077 /* Stop listening for new connections. */
1078 channel_stop_listening();
1079
1080 snprintf(string, sizeof string,
1081 "%c& [backgrounded]\n", escape_char);
1082 buffer_append(berr, string, strlen(string));
1083
1084 /* Fork into background. */
1085 pid = fork();
1086 if (pid < 0) {
1087 error("fork: %.100s", strerror(errno));
1088 continue;
1089 }
1090 if (pid != 0) { /* This is the parent. */
1091 /* The parent just exits. */
1092 exit(0);
1093 }
1094 /* The child continues serving connections. */
1095 if (compat20) {
1096 buffer_append(bin, "\004", 1);
1097 /* fake EOF on stdin */
1098 return -1;
1099 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +10001100 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001101 * Sending SSH_CMSG_EOF alone does not
1102 * always appear to be enough. So we
1103 * try to send an EOF character first.
Damien Millerad833b32000-08-23 10:46:23 +10001104 */
1105 packet_start(SSH_CMSG_STDIN_DATA);
1106 packet_put_string("\004", 1);
1107 packet_send();
1108 /* Close stdin. */
1109 stdin_eof = 1;
1110 if (buffer_len(bin) == 0) {
1111 packet_start(SSH_CMSG_EOF);
1112 packet_send();
1113 }
1114 }
Damien Miller96507ef2001-11-12 10:52:25 +11001115 continue;
Damien Millerad833b32000-08-23 10:46:23 +10001116
1117 case '?':
Damien Millere1537f92010-01-26 13:26:22 +11001118 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001119 snprintf(string, sizeof string,
Damien Millerad833b32000-08-23 10:46:23 +10001120"%c?\r\n\
1121Supported escape sequences:\r\n\
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001122 %c. - terminate session\r\n\
1123 %cB - send a BREAK to the remote system\r\n\
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001124 %cR - Request rekey (SSH protocol 2 only)\r\n\
1125 %c# - list forwarded connections\r\n\
1126 %c? - this message\r\n\
1127 %c%c - send the escape character by typing it twice\r\n\
Damien Millerad833b32000-08-23 10:46:23 +10001128(Note that escapes are only recognized immediately after newline.)\r\n",
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001129 escape_char, escape_char,
1130 escape_char, escape_char,
1131 escape_char, escape_char,
Damien Miller586b0052008-12-09 14:11:32 +11001132 escape_char, escape_char);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001133 } else {
1134 snprintf(string, sizeof string,
1135"%c?\r\n\
1136Supported escape sequences:\r\n\
1137 %c. - terminate connection (and any multiplexed sessions)\r\n\
1138 %cB - send a BREAK to the remote system\r\n\
1139 %cC - open a command line\r\n\
1140 %cR - Request rekey (SSH protocol 2 only)\r\n\
1141 %c^Z - suspend ssh\r\n\
1142 %c# - list forwarded connections\r\n\
1143 %c& - background ssh (when waiting for connections to terminate)\r\n\
1144 %c? - this message\r\n\
1145 %c%c - send the escape character by typing it twice\r\n\
1146(Note that escapes are only recognized immediately after newline.)\r\n",
1147 escape_char, escape_char,
1148 escape_char, escape_char,
1149 escape_char, escape_char,
1150 escape_char, escape_char,
1151 escape_char, escape_char,
1152 escape_char);
1153 }
Damien Millerad833b32000-08-23 10:46:23 +10001154 buffer_append(berr, string, strlen(string));
1155 continue;
1156
1157 case '#':
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001158 snprintf(string, sizeof string, "%c#\r\n",
1159 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001160 buffer_append(berr, string, strlen(string));
1161 s = channel_open_message();
1162 buffer_append(berr, s, strlen(s));
1163 xfree(s);
1164 continue;
1165
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001166 case 'C':
Damien Millere1537f92010-01-26 13:26:22 +11001167 if (c && c->ctl_chan != -1)
Damien Miller586b0052008-12-09 14:11:32 +11001168 goto noescape;
Ben Lindstrom681d9322002-03-22 03:53:00 +00001169 process_cmdline();
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001170 continue;
1171
Damien Millerad833b32000-08-23 10:46:23 +10001172 default:
1173 if (ch != escape_char) {
1174 buffer_put_char(bin, escape_char);
1175 bytes++;
1176 }
1177 /* Escaped characters fall through here */
1178 break;
1179 }
1180 } else {
1181 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001182 * The previous character was not an escape char.
1183 * Check if this is an escape.
Damien Millerad833b32000-08-23 10:46:23 +10001184 */
1185 if (last_was_cr && ch == escape_char) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001186 /*
1187 * It is. Set the flag and continue to
1188 * next character.
1189 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001190 *escape_pendingp = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001191 continue;
1192 }
1193 }
1194
1195 /*
1196 * Normal character. Record whether it was a newline,
1197 * and append it to the buffer.
1198 */
1199 last_was_cr = (ch == '\r' || ch == '\n');
1200 buffer_put_char(bin, ch);
1201 bytes++;
1202 }
1203 return bytes;
1204}
1205
Ben Lindstrombba81212001-06-25 05:01:22 +00001206static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001207client_process_input(fd_set *readset)
Damien Miller1383bd82000-04-06 12:32:37 +10001208{
Damien Miller166fca82000-04-20 07:42:21 +10001209 int len;
Darren Tucker86e30a02009-08-28 11:21:06 +10001210 char buf[SSH_IOBUFSZ];
Damien Miller1383bd82000-04-06 12:32:37 +10001211
Damien Miller95def091999-11-25 00:26:21 +11001212 /* Read input from stdin. */
1213 if (FD_ISSET(fileno(stdin), readset)) {
1214 /* Read as much as possible. */
1215 len = read(fileno(stdin), buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +10001216 if (len < 0 &&
1217 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001218 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +11001219 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001220 /*
1221 * Received EOF or error. They are treated
1222 * similarly, except that an error message is printed
1223 * if it was an error condition.
1224 */
Damien Miller95def091999-11-25 00:26:21 +11001225 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001226 snprintf(buf, sizeof buf, "read: %.100s\r\n",
1227 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001228 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001229 }
1230 /* Mark that we have seen EOF. */
1231 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +11001232 /*
1233 * Send an EOF message to the server unless there is
1234 * data in the buffer. If there is data in the
1235 * buffer, no message will be sent now. Code
1236 * elsewhere will send the EOF when the buffer
1237 * becomes empty if stdin_eof is set.
1238 */
Damien Miller95def091999-11-25 00:26:21 +11001239 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001240 packet_start(SSH_CMSG_EOF);
1241 packet_send();
Damien Miller95def091999-11-25 00:26:21 +11001242 }
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001243 } else if (escape_char1 == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +11001244 /*
1245 * Normal successful read, and no escape character.
1246 * Just append the data to buffer.
1247 */
Damien Miller95def091999-11-25 00:26:21 +11001248 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +11001249 } else {
Damien Miller5428f641999-11-25 11:54:57 +11001250 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001251 * Normal, successful read. But we have an escape
1252 * character and have to process the characters one
1253 * by one.
Damien Miller5428f641999-11-25 11:54:57 +11001254 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001255 if (process_escapes(NULL, &stdin_buffer,
1256 &stdout_buffer, &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +10001257 return;
Damien Miller95def091999-11-25 00:26:21 +11001258 }
1259 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001260}
1261
Ben Lindstrombba81212001-06-25 05:01:22 +00001262static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001263client_process_output(fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001264{
Damien Miller95def091999-11-25 00:26:21 +11001265 int len;
1266 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001267
Damien Miller95def091999-11-25 00:26:21 +11001268 /* Write buffered output to stdout. */
1269 if (FD_ISSET(fileno(stdout), writeset)) {
1270 /* Write as much data as possible. */
1271 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001272 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001273 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001274 if (errno == EINTR || errno == EAGAIN ||
1275 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001276 len = 0;
1277 else {
Damien Miller5428f641999-11-25 11:54:57 +11001278 /*
1279 * An error or EOF was encountered. Put an
1280 * error message to stderr buffer.
1281 */
Darren Tucker4d5cd332008-06-13 04:51:14 +10001282 snprintf(buf, sizeof buf,
1283 "write stdout: %.50s\r\n", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001284 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001285 quit_pending = 1;
1286 return;
1287 }
1288 }
1289 /* Consume printed data from the buffer. */
1290 buffer_consume(&stdout_buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001291 }
Damien Miller95def091999-11-25 00:26:21 +11001292 /* Write buffered output to stderr. */
1293 if (FD_ISSET(fileno(stderr), writeset)) {
1294 /* Write as much data as possible. */
1295 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001296 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001297 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001298 if (errno == EINTR || errno == EAGAIN ||
1299 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001300 len = 0;
1301 else {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001302 /*
1303 * EOF or error, but can't even print
1304 * error message.
1305 */
Damien Miller95def091999-11-25 00:26:21 +11001306 quit_pending = 1;
1307 return;
1308 }
1309 }
1310 /* Consume printed characters from the buffer. */
1311 buffer_consume(&stderr_buffer, len);
1312 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001313}
1314
Damien Miller5428f641999-11-25 11:54:57 +11001315/*
Damien Millerb38eff82000-04-01 11:09:21 +10001316 * Get packets from the connection input buffer, and process them as long as
1317 * there are packets available.
1318 *
1319 * Any unknown packets received during the actual
1320 * session cause the session to terminate. This is
1321 * intended to make debugging easier since no
1322 * confirmations are sent. Any compatible protocol
1323 * extensions must be negotiated during the
1324 * preparatory phase.
1325 */
1326
Ben Lindstrombba81212001-06-25 05:01:22 +00001327static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001328client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001329{
Darren Tucker4d5cd332008-06-13 04:51:14 +10001330 dispatch_run(DISPATCH_NONBLOCK, &quit_pending,
1331 compat20 ? xxx_kex : NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001332}
1333
Damien Millerad833b32000-08-23 10:46:23 +10001334/* scan buf[] for '~' before sending data to the peer */
1335
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001336/* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1337void *
1338client_new_escape_filter_ctx(int escape_char)
1339{
1340 struct escape_filter_ctx *ret;
1341
1342 ret = xmalloc(sizeof(*ret));
1343 ret->escape_pending = 0;
1344 ret->escape_char = escape_char;
1345 return (void *)ret;
1346}
1347
Darren Tucker84c56f52008-06-13 04:55:46 +10001348/* Free the escape filter context on channel free */
1349void
1350client_filter_cleanup(int cid, void *ctx)
1351{
1352 xfree(ctx);
1353}
1354
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001355int
1356client_simple_escape_filter(Channel *c, char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001357{
Damien Miller5771ed72008-05-19 15:35:33 +10001358 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1359 return 0;
1360
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001361 return process_escapes(c, &c->input, &c->output, &c->extended,
1362 buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001363}
1364
Ben Lindstrombba81212001-06-25 05:01:22 +00001365static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001366client_channel_closed(int id, void *arg)
1367{
Damien Miller3ec27592001-10-12 11:35:04 +10001368 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001369 session_closed = 1;
Damien Miller21771e22011-05-15 08:45:50 +10001370 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001371}
1372
Damien Millerb38eff82000-04-01 11:09:21 +10001373/*
Damien Miller5428f641999-11-25 11:54:57 +11001374 * Implements the interactive session with the server. This is called after
1375 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001376 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1377 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001378 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001379
Damien Miller4af51302000-04-16 11:18:38 +10001380int
Damien Millerad833b32000-08-23 10:46:23 +10001381client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001382{
Damien Miller5e953212001-01-30 09:14:00 +11001383 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001384 double start_time, total_time;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001385 int max_fd = 0, max_fd2 = 0, len, rekeying = 0;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001386 u_int64_t ibytes, obytes;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001387 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001388 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001389
Damien Miller95def091999-11-25 00:26:21 +11001390 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001391
Damien Miller95def091999-11-25 00:26:21 +11001392 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001393
Damien Miller95def091999-11-25 00:26:21 +11001394 /* Initialize variables. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001395 escape_pending1 = 0;
Damien Miller95def091999-11-25 00:26:21 +11001396 last_was_cr = 1;
1397 exit_status = -1;
1398 stdin_eof = 0;
1399 buffer_high = 64 * 1024;
1400 connection_in = packet_get_connection_in();
1401 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +11001402 max_fd = MAX(connection_in, connection_out);
1403
1404 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +00001405 /* enable nonblocking unless tty */
1406 if (!isatty(fileno(stdin)))
1407 set_nonblock(fileno(stdin));
1408 if (!isatty(fileno(stdout)))
1409 set_nonblock(fileno(stdout));
1410 if (!isatty(fileno(stderr)))
1411 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +11001412 max_fd = MAX(max_fd, fileno(stdin));
1413 max_fd = MAX(max_fd, fileno(stdout));
1414 max_fd = MAX(max_fd, fileno(stderr));
1415 }
Damien Miller95def091999-11-25 00:26:21 +11001416 quit_pending = 0;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001417 escape_char1 = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001418
Damien Miller95def091999-11-25 00:26:21 +11001419 /* Initialize buffers. */
1420 buffer_init(&stdin_buffer);
1421 buffer_init(&stdout_buffer);
1422 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001423
Damien Millerb38eff82000-04-01 11:09:21 +10001424 client_init_dispatch();
1425
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001426 /*
1427 * Set signal handlers, (e.g. to restore non-blocking mode)
1428 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1429 */
Darren Tucker07336da2004-11-05 20:02:16 +11001430 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1431 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001432 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1433 signal(SIGINT, signal_handler);
1434 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1435 signal(SIGQUIT, signal_handler);
1436 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1437 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001438 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001439
Damien Miller95def091999-11-25 00:26:21 +11001440 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001441 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001442
Ben Lindstrom5b828322001-02-09 01:34:36 +00001443 if (compat20) {
1444 session_ident = ssh2_chan_id;
Damien Miller6c3eec72011-05-05 14:16:22 +10001445 if (session_ident != -1) {
1446 if (escape_char_arg != SSH_ESCAPECHAR_NONE) {
1447 channel_register_filter(session_ident,
1448 client_simple_escape_filter, NULL,
1449 client_filter_cleanup,
1450 client_new_escape_filter_ctx(
1451 escape_char_arg));
1452 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001453 channel_register_cleanup(session_ident,
Damien Miller39eda6e2005-11-05 14:52:50 +11001454 client_channel_closed, 0);
Damien Miller6c3eec72011-05-05 14:16:22 +10001455 }
Ben Lindstrom5b828322001-02-09 01:34:36 +00001456 } else {
1457 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +10001458 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001459 }
Damien Millerad833b32000-08-23 10:46:23 +10001460
Damien Miller95def091999-11-25 00:26:21 +11001461 /* Main loop of the client for the interactive session mode. */
1462 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001463
Damien Miller5428f641999-11-25 11:54:57 +11001464 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001465 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001466
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001467 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +10001468 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001469
1470 rekeying = (xxx_kex != NULL && !xxx_kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +10001471
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001472 if (rekeying) {
1473 debug("rekeying in progress");
1474 } else {
1475 /*
1476 * Make packets of buffered stdin data, and buffer
1477 * them for sending to the server.
1478 */
1479 if (!compat20)
1480 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001481
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001482 /*
1483 * Make packets from buffered channel data, and
1484 * enqueue them for sending to the server.
1485 */
1486 if (packet_not_very_much_data_to_write())
1487 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001488
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001489 /*
1490 * Check if the window size has changed, and buffer a
1491 * message about it to the server if so.
1492 */
1493 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001494
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001495 if (quit_pending)
1496 break;
1497 }
Damien Miller5428f641999-11-25 11:54:57 +11001498 /*
1499 * Wait until we have something to do (something becomes
1500 * available on one of the descriptors).
1501 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001502 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001503 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001504 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001505
Damien Miller95def091999-11-25 00:26:21 +11001506 if (quit_pending)
1507 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001508
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001509 /* Do channel operations unless rekeying in progress. */
1510 if (!rekeying) {
1511 channel_after_select(readset, writeset);
Damien Millera5539d22003-04-09 20:50:06 +10001512 if (need_rekeying || packet_need_rekeying()) {
1513 debug("need rekeying");
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001514 xxx_kex->done = 0;
1515 kex_send_kexinit(xxx_kex);
1516 need_rekeying = 0;
1517 }
1518 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001519
Damien Miller1383bd82000-04-06 12:32:37 +10001520 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001521 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001522
Damien Miller1383bd82000-04-06 12:32:37 +10001523 if (quit_pending)
1524 break;
1525
1526 if (!compat20) {
1527 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +11001528 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +10001529 /*
1530 * Process output to stdout and stderr. Output to
1531 * the connection is processed elsewhere (above).
1532 */
Damien Miller5e953212001-01-30 09:14:00 +11001533 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +10001534 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001535
Darren Tuckere32cf432010-01-08 16:51:40 +11001536 if (session_resumed) {
1537 connection_in = packet_get_connection_in();
1538 connection_out = packet_get_connection_out();
1539 max_fd = MAX(max_fd, connection_out);
1540 max_fd = MAX(max_fd, connection_in);
1541 session_resumed = 0;
1542 }
1543
Darren Tucker4d5cd332008-06-13 04:51:14 +10001544 /*
1545 * Send as much buffered packet data as possible to the
1546 * sender.
1547 */
Damien Miller5e953212001-01-30 09:14:00 +11001548 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001549 packet_write_poll();
Damien Millere11e1ea2010-08-03 16:04:46 +10001550
1551 /*
1552 * If we are a backgrounded control master, and the
1553 * timeout has expired without any active client
1554 * connections, then quit.
1555 */
1556 if (control_persist_exit_time > 0) {
1557 if (time(NULL) >= control_persist_exit_time) {
1558 debug("ControlPersist timeout expired");
1559 break;
1560 }
1561 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001562 }
Damien Miller5e953212001-01-30 09:14:00 +11001563 if (readset)
1564 xfree(readset);
1565 if (writeset)
1566 xfree(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001567
Damien Miller95def091999-11-25 00:26:21 +11001568 /* Terminate the session. */
1569
1570 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001571 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001572
Darren Tuckerde0c0252009-07-06 07:17:35 +10001573 if (compat20) {
1574 packet_start(SSH2_MSG_DISCONNECT);
1575 packet_put_int(SSH2_DISCONNECT_BY_APPLICATION);
1576 packet_put_cstring("disconnected by user");
Damien Miller8ddc71c2010-03-22 05:54:02 +11001577 packet_put_cstring(""); /* language tag */
Darren Tuckerde0c0252009-07-06 07:17:35 +10001578 packet_send();
1579 packet_write_wait();
1580 }
Darren Tucker12b4a652009-06-21 18:14:48 +10001581
Ben Lindstrom601e4362001-06-21 03:19:23 +00001582 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +11001583
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001584 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001585 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001586
1587 /* restore blocking io */
1588 if (!isatty(fileno(stdin)))
1589 unset_nonblock(fileno(stdin));
1590 if (!isatty(fileno(stdout)))
1591 unset_nonblock(fileno(stdout));
1592 if (!isatty(fileno(stderr)))
1593 unset_nonblock(fileno(stderr));
1594
Damien Millerd6965512003-12-17 16:31:53 +11001595 /*
1596 * If there was no shell or command requested, there will be no remote
1597 * exit status to be returned. In that case, clear error code if the
1598 * connection was deliberately terminated at this end.
1599 */
1600 if (no_shell_flag && received_signal == SIGTERM) {
1601 received_signal = 0;
1602 exit_status = 0;
1603 }
1604
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001605 if (received_signal)
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001606 fatal("Killed by signal %d.", (int) received_signal);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001607
1608 /*
1609 * In interactive mode (with pseudo tty) display a message indicating
1610 * that the connection has been closed.
1611 */
1612 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001613 snprintf(buf, sizeof buf,
1614 "Connection to %.64s closed.\r\n", host);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001615 buffer_append(&stderr_buffer, buf, strlen(buf));
1616 }
1617
Damien Miller95def091999-11-25 00:26:21 +11001618 /* Output any buffered data for stdout. */
Damien Millercfd6e4f2011-01-16 23:18:33 +11001619 if (buffer_len(&stdout_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001620 len = atomicio(vwrite, fileno(stdout),
1621 buffer_ptr(&stdout_buffer), buffer_len(&stdout_buffer));
Damien Millercfd6e4f2011-01-16 23:18:33 +11001622 if (len < 0 || (u_int)len != buffer_len(&stdout_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001623 error("Write failed flushing stdout buffer.");
Damien Millercfd6e4f2011-01-16 23:18:33 +11001624 else
1625 buffer_consume(&stdout_buffer, len);
Damien Miller95def091999-11-25 00:26:21 +11001626 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001627
Damien Miller95def091999-11-25 00:26:21 +11001628 /* Output any buffered data for stderr. */
Damien Millercfd6e4f2011-01-16 23:18:33 +11001629 if (buffer_len(&stderr_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001630 len = atomicio(vwrite, fileno(stderr),
1631 buffer_ptr(&stderr_buffer), buffer_len(&stderr_buffer));
Damien Millercfd6e4f2011-01-16 23:18:33 +11001632 if (len < 0 || (u_int)len != buffer_len(&stderr_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001633 error("Write failed flushing stderr buffer.");
Damien Millercfd6e4f2011-01-16 23:18:33 +11001634 else
1635 buffer_consume(&stderr_buffer, len);
Damien Miller95def091999-11-25 00:26:21 +11001636 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001637
Damien Miller95def091999-11-25 00:26:21 +11001638 /* Clear and free any buffers. */
1639 memset(buf, 0, sizeof(buf));
1640 buffer_free(&stdin_buffer);
1641 buffer_free(&stdout_buffer);
1642 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001643
Damien Miller95def091999-11-25 00:26:21 +11001644 /* Report bytes transferred, and transfer rates. */
1645 total_time = get_current_time() - start_time;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001646 packet_get_state(MODE_IN, NULL, NULL, NULL, &ibytes);
1647 packet_get_state(MODE_OUT, NULL, NULL, NULL, &obytes);
1648 verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
Damien Miller821de0a2011-01-11 17:20:29 +11001649 (unsigned long long)obytes, (unsigned long long)ibytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001650 if (total_time > 0)
Damien Millerb61f3fc2008-07-11 17:36:48 +10001651 verbose("Bytes per second: sent %.1f, received %.1f",
1652 obytes / total_time, ibytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001653 /* Return the exit status of the program. */
1654 debug("Exit status %d", exit_status);
1655 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001656}
Damien Millerb38eff82000-04-01 11:09:21 +10001657
1658/*********/
1659
Ben Lindstrombba81212001-06-25 05:01:22 +00001660static void
Damien Miller630d6f42002-01-22 23:17:30 +11001661client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001662{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001663 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001664 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001665 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001666 buffer_append(&stdout_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001667 memset(data, 0, data_len);
1668 xfree(data);
1669}
Ben Lindstrombba81212001-06-25 05:01:22 +00001670static void
Damien Miller630d6f42002-01-22 23:17:30 +11001671client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001672{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001673 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001674 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001675 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001676 buffer_append(&stderr_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001677 memset(data, 0, data_len);
1678 xfree(data);
1679}
Ben Lindstrombba81212001-06-25 05:01:22 +00001680static void
Damien Miller630d6f42002-01-22 23:17:30 +11001681client_input_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001682{
Damien Millerb38eff82000-04-01 11:09:21 +10001683 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001684 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001685 /* Acknowledge the exit. */
1686 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1687 packet_send();
1688 /*
1689 * Must wait for packet to be sent since we are
1690 * exiting the loop.
1691 */
1692 packet_write_wait();
1693 /* Flag that we want to exit. */
1694 quit_pending = 1;
1695}
Darren Tucker5dcdd212003-10-02 16:17:00 +10001696static void
1697client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1698{
1699 Channel *c = NULL;
1700 int remote_id, sock;
1701
1702 /* Read the remote channel number from the message. */
1703 remote_id = packet_get_int();
1704 packet_check_eom();
1705
1706 /*
1707 * Get a connection to the local authentication agent (this may again
1708 * get forwarded).
1709 */
1710 sock = ssh_get_authentication_socket();
1711
1712 /*
1713 * If we could not connect the agent, send an error message back to
1714 * the server. This should never happen unless the agent dies,
1715 * because authentication forwarding is only enabled if we have an
1716 * agent.
1717 */
1718 if (sock >= 0) {
1719 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1720 -1, 0, 0, 0, "authentication agent connection", 1);
1721 c->remote_id = remote_id;
1722 c->force_drain = 1;
1723 }
1724 if (c == NULL) {
1725 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1726 packet_put_int(remote_id);
1727 } else {
1728 /* Send a confirmation to the remote host. */
1729 debug("Forwarding authentication connection.");
1730 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1731 packet_put_int(remote_id);
1732 packet_put_int(c->self);
1733 }
1734 packet_send();
1735}
Damien Millerb38eff82000-04-01 11:09:21 +10001736
Ben Lindstrombba81212001-06-25 05:01:22 +00001737static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001738client_request_forwarded_tcpip(const char *request_type, int rchan)
1739{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001740 Channel *c = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001741 char *listen_address, *originator_address;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001742 u_short listen_port, originator_port;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001743
1744 /* Get rest of the packet */
1745 listen_address = packet_get_string(NULL);
1746 listen_port = packet_get_int();
1747 originator_address = packet_get_string(NULL);
1748 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001749 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001750
Damien Millerbd740252008-05-19 15:37:09 +10001751 debug("client_request_forwarded_tcpip: listen %s port %d, "
1752 "originator %s port %d", listen_address, listen_port,
1753 originator_address, originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001754
Damien Millerbd740252008-05-19 15:37:09 +10001755 c = channel_connect_by_listen_address(listen_port,
1756 "forwarded-tcpip", originator_address);
1757
Damien Miller0bc1bd82000-11-13 22:57:25 +11001758 xfree(originator_address);
1759 xfree(listen_address);
1760 return c;
1761}
1762
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001763static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001764client_request_x11(const char *request_type, int rchan)
1765{
1766 Channel *c = NULL;
1767 char *originator;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001768 u_short originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001769 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001770
1771 if (!options.forward_x11) {
1772 error("Warning: ssh server tried X11 forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001773 error("Warning: this is probably a break-in attempt by a "
1774 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001775 return NULL;
1776 }
Damien Miller1ab6a512010-06-26 10:02:24 +10001777 if (x11_refuse_time != 0 && time(NULL) >= x11_refuse_time) {
1778 verbose("Rejected X11 connection after ForwardX11Timeout "
1779 "expired");
1780 return NULL;
1781 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001782 originator = packet_get_string(NULL);
1783 if (datafellows & SSH_BUG_X11FWD) {
1784 debug2("buggy server: x11 request w/o originator_port");
1785 originator_port = 0;
1786 } else {
1787 originator_port = packet_get_int();
1788 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001789 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001790 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001791 debug("client_request_x11: request from %s %d", originator,
1792 originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001793 xfree(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001794 sock = x11_connect_display();
1795 if (sock < 0)
1796 return NULL;
1797 c = channel_new("x11",
1798 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001799 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001800 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001801 return c;
1802}
1803
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001804static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001805client_request_agent(const char *request_type, int rchan)
1806{
1807 Channel *c = NULL;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001808 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001809
1810 if (!options.forward_agent) {
1811 error("Warning: ssh server tried agent forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001812 error("Warning: this is probably a break-in attempt by a "
1813 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001814 return NULL;
1815 }
Darren Tucker82a3d2b2007-02-19 22:10:25 +11001816 sock = ssh_get_authentication_socket();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001817 if (sock < 0)
1818 return NULL;
1819 c = channel_new("authentication agent connection",
1820 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001821 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001822 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001823 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001824 return c;
1825}
1826
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001827int
1828client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun)
1829{
1830 Channel *c;
1831 int fd;
1832
1833 if (tun_mode == SSH_TUNMODE_NO)
1834 return 0;
1835
1836 if (!compat20) {
Damien Millerb3f2c9f2009-01-28 16:15:30 +11001837 error("Tunnel forwarding is not supported for protocol 1");
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001838 return -1;
1839 }
1840
1841 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1842
1843 /* Open local tunnel device */
1844 if ((fd = tun_open(local_tun, tun_mode)) == -1) {
1845 error("Tunnel device open failed.");
1846 return -1;
1847 }
1848
1849 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1850 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1851 c->datagram = 1;
1852
1853#if defined(SSH_TUN_FILTER)
1854 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1855 channel_register_filter(c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +10001856 sys_tun_outfilter, NULL, NULL);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001857#endif
1858
1859 packet_start(SSH2_MSG_CHANNEL_OPEN);
1860 packet_put_cstring("tun@openssh.com");
1861 packet_put_int(c->self);
1862 packet_put_int(c->local_window_max);
1863 packet_put_int(c->local_maxpacket);
1864 packet_put_int(tun_mode);
1865 packet_put_int(remote_tun);
1866 packet_send();
1867
1868 return 0;
1869}
1870
Damien Millerbd483e72000-04-30 10:00:53 +10001871/* XXXX move to generic input handler */
Ben Lindstrombba81212001-06-25 05:01:22 +00001872static void
Damien Miller630d6f42002-01-22 23:17:30 +11001873client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001874{
1875 Channel *c = NULL;
1876 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001877 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001878 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001879
1880 ctype = packet_get_string(&len);
1881 rchan = packet_get_int();
1882 rwindow = packet_get_int();
1883 rmaxpack = packet_get_int();
1884
Damien Millere247cc42000-05-07 12:03:14 +10001885 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001886 ctype, rchan, rwindow, rmaxpack);
1887
Damien Miller0bc1bd82000-11-13 22:57:25 +11001888 if (strcmp(ctype, "forwarded-tcpip") == 0) {
1889 c = client_request_forwarded_tcpip(ctype, rchan);
1890 } else if (strcmp(ctype, "x11") == 0) {
1891 c = client_request_x11(ctype, rchan);
1892 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1893 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001894 }
1895/* XXX duplicate : */
1896 if (c != NULL) {
1897 debug("confirm %s", ctype);
1898 c->remote_id = rchan;
1899 c->remote_window = rwindow;
1900 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001901 if (c->type != SSH_CHANNEL_CONNECTING) {
1902 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1903 packet_put_int(c->remote_id);
1904 packet_put_int(c->self);
1905 packet_put_int(c->local_window);
1906 packet_put_int(c->local_maxpacket);
1907 packet_send();
1908 }
Damien Millerbd483e72000-04-30 10:00:53 +10001909 } else {
1910 debug("failure %s", ctype);
1911 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1912 packet_put_int(rchan);
1913 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00001914 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001915 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00001916 packet_put_cstring("");
1917 }
Damien Millerbd483e72000-04-30 10:00:53 +10001918 packet_send();
1919 }
1920 xfree(ctype);
1921}
Ben Lindstrombba81212001-06-25 05:01:22 +00001922static void
Damien Miller630d6f42002-01-22 23:17:30 +11001923client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001924{
1925 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001926 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001927 char *rtype;
1928
1929 id = packet_get_int();
1930 rtype = packet_get_string(NULL);
1931 reply = packet_get_char();
1932
1933 debug("client_input_channel_req: channel %d rtype %s reply %d",
1934 id, rtype, reply);
1935
Damien Miller3bbd8782004-06-18 22:23:22 +10001936 if (id == -1) {
1937 error("client_input_channel_req: request for channel -1");
1938 } else if ((c = channel_lookup(id)) == NULL) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001939 error("client_input_channel_req: channel %d: "
1940 "unknown channel", id);
Damien Millerbab9bd42008-05-19 16:06:47 +10001941 } else if (strcmp(rtype, "eow@openssh.com") == 0) {
1942 packet_check_eom();
1943 chan_rcvd_eow(c);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001944 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10001945 exitval = packet_get_int();
Damien Millere1537f92010-01-26 13:26:22 +11001946 if (c->ctl_chan != -1) {
1947 mux_exit_message(c, exitval);
Damien Miller0e220db2004-06-15 10:34:08 +10001948 success = 1;
Darren Tucker29440822010-01-08 17:08:35 +11001949 } else if (id == session_ident) {
1950 /* Record exit value of local session */
1951 success = 1;
1952 exit_status = exitval;
1953 } else {
Damien Miller36f57eb2010-01-30 17:28:34 +11001954 /* Probably for a mux channel that has already closed */
1955 debug("%s: no sink for exit-status on channel %d",
1956 __func__, id);
Damien Miller0e220db2004-06-15 10:34:08 +10001957 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001958 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001959 }
Damien Millerd0fdd682010-12-01 12:02:14 +11001960 if (reply && c != NULL) {
Ben Lindstrom5b828322001-02-09 01:34:36 +00001961 packet_start(success ?
1962 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller8533c782008-12-08 09:54:40 +11001963 packet_put_int(c->remote_id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001964 packet_send();
1965 }
1966 xfree(rtype);
1967}
Damien Millerc3fa4072002-01-22 23:21:58 +11001968static void
1969client_input_global_request(int type, u_int32_t seq, void *ctxt)
1970{
1971 char *rtype;
1972 int want_reply;
1973 int success = 0;
1974
1975 rtype = packet_get_string(NULL);
1976 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11001977 debug("client_input_global_request: rtype %s want_reply %d",
1978 rtype, want_reply);
Damien Millerc3fa4072002-01-22 23:21:58 +11001979 if (want_reply) {
1980 packet_start(success ?
1981 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
1982 packet_send();
1983 packet_write_wait();
1984 }
1985 xfree(rtype);
1986}
Damien Millerbd483e72000-04-30 10:00:53 +10001987
Damien Miller0e220db2004-06-15 10:34:08 +10001988void
Darren Tuckerfc959702004-07-17 16:12:08 +10001989client_session2_setup(int id, int want_tty, int want_subsystem,
Damien Miller5771ed72008-05-19 15:35:33 +10001990 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env)
Damien Miller0e220db2004-06-15 10:34:08 +10001991{
1992 int len;
Darren Tucker5d78de62004-11-05 20:35:44 +11001993 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001994
1995 debug2("%s: id %d", __func__, id);
1996
Darren Tucker5d78de62004-11-05 20:35:44 +11001997 if ((c = channel_lookup(id)) == NULL)
1998 fatal("client_session2_setup: channel %d: unknown channel", id);
1999
Damien Miller0dac6fb2010-11-20 15:19:38 +11002000 packet_set_interactive(want_tty,
2001 options.ip_qos_interactive, options.ip_qos_bulk);
2002
Damien Miller0e220db2004-06-15 10:34:08 +10002003 if (want_tty) {
2004 struct winsize ws;
Damien Miller0e220db2004-06-15 10:34:08 +10002005
2006 /* Store window size in the packet. */
2007 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
2008 memset(&ws, 0, sizeof(ws));
2009
Damien Miller5771ed72008-05-19 15:35:33 +10002010 channel_request_start(id, "pty-req", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002011 client_expect_confirm(id, "PTY allocation", CONFIRM_TTY);
Damien Miller0e220db2004-06-15 10:34:08 +10002012 packet_put_cstring(term != NULL ? term : "");
Damien Miller71a73672006-03-26 14:04:36 +11002013 packet_put_int((u_int)ws.ws_col);
2014 packet_put_int((u_int)ws.ws_row);
2015 packet_put_int((u_int)ws.ws_xpixel);
2016 packet_put_int((u_int)ws.ws_ypixel);
Darren Tuckerdf189fb2008-06-08 12:55:32 +10002017 if (tiop == NULL)
2018 tiop = get_saved_tio();
2019 tty_make_modes(-1, tiop);
Damien Miller0e220db2004-06-15 10:34:08 +10002020 packet_send();
2021 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11002022 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10002023 }
2024
2025 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10002026 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10002027 int i, j, matched;
Damien Miller0e220db2004-06-15 10:34:08 +10002028 char *name, *val;
2029
2030 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10002031 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10002032 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10002033 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10002034 if ((val = strchr(name, '=')) == NULL) {
Damien Miller0a0176e2005-11-05 15:07:59 +11002035 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002036 continue;
2037 }
2038 *val++ = '\0';
2039
2040 matched = 0;
2041 for (j = 0; j < options.num_send_env; j++) {
2042 if (match_pattern(name, options.send_env[j])) {
2043 matched = 1;
2044 break;
2045 }
2046 }
2047 if (!matched) {
2048 debug3("Ignored env %s", name);
Damien Miller0a0176e2005-11-05 15:07:59 +11002049 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002050 continue;
2051 }
2052
2053 debug("Sending env %s = %s", name, val);
2054 channel_request_start(id, "env", 0);
2055 packet_put_cstring(name);
2056 packet_put_cstring(val);
2057 packet_send();
Damien Miller0a0176e2005-11-05 15:07:59 +11002058 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002059 }
2060 }
2061
2062 len = buffer_len(cmd);
2063 if (len > 0) {
2064 if (len > 900)
2065 len = 900;
2066 if (want_subsystem) {
Damien Miller5771ed72008-05-19 15:35:33 +10002067 debug("Sending subsystem: %.*s",
2068 len, (u_char*)buffer_ptr(cmd));
2069 channel_request_start(id, "subsystem", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002070 client_expect_confirm(id, "subsystem", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002071 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002072 debug("Sending command: %.*s",
2073 len, (u_char*)buffer_ptr(cmd));
2074 channel_request_start(id, "exec", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002075 client_expect_confirm(id, "exec", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002076 }
2077 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
2078 packet_send();
2079 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002080 channel_request_start(id, "shell", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002081 client_expect_confirm(id, "shell", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002082 packet_send();
2083 }
2084}
2085
Ben Lindstrombba81212001-06-25 05:01:22 +00002086static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002087client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10002088{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002089 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10002090
Damien Miller1383bd82000-04-06 12:32:37 +10002091 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2092 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2093 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2094 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10002095 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10002096 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2097 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002098 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10002099 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerb84886b2008-05-19 15:05:07 +10002100 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2101 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
Damien Millerc3fa4072002-01-22 23:21:58 +11002102 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002103
2104 /* rekeying */
2105 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10002106
2107 /* global request reply messages */
2108 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2109 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10002110}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002111
Ben Lindstrombba81212001-06-25 05:01:22 +00002112static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002113client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002114{
2115 dispatch_init(NULL);
2116 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
2117 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
2118 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10002119 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2120 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
2121 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002122 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
2123 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
2124 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11002125
2126 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
Darren Tucker5dcdd212003-10-02 16:17:00 +10002127 &client_input_agent_open : &deny_input_open);
Damien Miller69b69aa2000-10-28 14:19:58 +11002128 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
2129 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002130}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002131
Ben Lindstrombba81212001-06-25 05:01:22 +00002132static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002133client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002134{
2135 client_init_dispatch_13();
2136 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
2137 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
2138}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002139
Ben Lindstromdb47f382001-07-04 05:10:27 +00002140static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002141client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002142{
Damien Miller1383bd82000-04-06 12:32:37 +10002143 if (compat20)
2144 client_init_dispatch_20();
2145 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10002146 client_init_dispatch_13();
2147 else
2148 client_init_dispatch_15();
2149}
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002150
Damien Miller6c3eec72011-05-05 14:16:22 +10002151void
2152client_stop_mux(void)
2153{
2154 if (options.control_path != NULL && muxserver_sock != -1)
2155 unlink(options.control_path);
2156 /*
2157 * If we are in persist mode, signal that we should close when all
2158 * active channels are closed.
2159 */
Damien Miller4ac99c32011-06-20 14:43:31 +10002160 if (options.control_persist) {
Damien Miller6c3eec72011-05-05 14:16:22 +10002161 session_closed = 1;
Damien Miller4ac99c32011-06-20 14:43:31 +10002162 setproctitle("[stopped mux]");
2163 }
Damien Miller6c3eec72011-05-05 14:16:22 +10002164}
2165
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002166/* client specific fatal cleanup */
2167void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002168cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002169{
Damien Miller21771e22011-05-15 08:45:50 +10002170 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002171 leave_non_blocking();
Damien Millerb1cbfa22008-05-19 16:00:08 +10002172 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10002173 unlink(options.control_path);
Damien Millera41ccca2010-10-07 22:07:32 +11002174 ssh_kill_proxy_command();
Darren Tucker3e33cec2003-10-02 16:12:36 +10002175 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002176}