blob: 5bd757dfb147c8f51d995f7f4908189b398d343d [file] [log] [blame]
Damien Miller21771e22011-05-15 08:45:50 +10001/* $OpenBSD: clientloop.c,v 1.233 2011/05/06 21:34:32 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * The main loop for the interactive session (client side).
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
14 *
15 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 *
Damien Miller1383bd82000-04-06 12:32:37 +100038 * SSH2 support added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000039 * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110040 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110060 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
62#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110063
Damien Millerf17883e2006-03-15 11:45:54 +110064#include <sys/types.h>
Damien Miller9aec9192006-08-05 10:57:45 +100065#include <sys/ioctl.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100066#include <sys/param.h>
Damien Millerf17883e2006-03-15 11:45:54 +110067#ifdef HAVE_SYS_STAT_H
68# include <sys/stat.h>
69#endif
Damien Miller9aec9192006-08-05 10:57:45 +100070#ifdef HAVE_SYS_TIME_H
71# include <sys/time.h>
72#endif
Damien Millere3b60b52006-07-10 21:08:03 +100073#include <sys/socket.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110074
Damien Millerc7b06362006-03-15 11:53:45 +110075#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100076#include <errno.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110077#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110078#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110079#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110080#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100081#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100082#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100083#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100084#include <string.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110085#include <termios.h>
Damien Millerd7834352006-08-05 12:39:39 +100086#include <pwd.h>
Damien Millere6b3b612006-07-24 14:01:23 +100087#include <unistd.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
Damien Millerb84886b2008-05-19 15:05:07 +100089#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100090#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000092#include "ssh1.h"
93#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094#include "packet.h"
95#include "buffer.h"
Damien Millerb38eff82000-04-01 11:09:21 +100096#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000097#include "channels.h"
Damien Millerb38eff82000-04-01 11:09:21 +100098#include "dispatch.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110099#include "key.h"
Damien Millerd7834352006-08-05 12:39:39 +1000100#include "cipher.h"
Ben Lindstromf28f6342001-04-04 02:03:04 +0000101#include "kex.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000102#include "log.h"
103#include "readconf.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000104#include "clientloop.h"
Damien Milleraeb31d62005-12-13 19:29:36 +1100105#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000106#include "authfd.h"
107#include "atomicio.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +1000108#include "sshpty.h"
Ben Lindstrom302ea6f2001-04-16 02:01:25 +0000109#include "misc.h"
Damien Miller0e220db2004-06-15 10:34:08 +1000110#include "match.h"
111#include "msg.h"
Darren Tuckerc5564e12009-06-21 18:53:53 +1000112#include "roaming.h"
Damien Miller69b69aa2000-10-28 14:19:58 +1100113
114/* import options */
115extern Options options;
116
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117/* Flag indicating that stdin should be redirected from /dev/null. */
118extern int stdin_null_flag;
119
Damien Millerd6965512003-12-17 16:31:53 +1100120/* Flag indicating that no shell has been requested */
121extern int no_shell_flag;
122
Damien Miller0e220db2004-06-15 10:34:08 +1000123/* Control socket */
Damien Millere1537f92010-01-26 13:26:22 +1100124extern int muxserver_sock; /* XXX use mux_client_cleanup() instead */
Damien Miller0e220db2004-06-15 10:34:08 +1000125
Damien Miller5428f641999-11-25 11:54:57 +1100126/*
127 * Name of the host we are connecting to. This is the name given on the
128 * command line, or the HostName specified for the user-supplied name in a
129 * configuration file.
130 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131extern char *host;
132
Damien Miller5428f641999-11-25 11:54:57 +1100133/*
134 * Flag to indicate that we have received a window change signal which has
135 * not yet been processed. This will cause a message indicating the new
136 * window size to be sent to the server a little later. This is volatile
137 * because this is updated in a signal handler.
138 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000139static volatile sig_atomic_t received_window_change_signal = 0;
140static volatile sig_atomic_t received_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141
Damien Miller788f2122005-11-05 15:14:59 +1100142/* Flag indicating whether the user's terminal is in non-blocking mode. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143static int in_non_blocking_mode = 0;
144
Damien Millere11e1ea2010-08-03 16:04:46 +1000145/* Time when backgrounded control master using ControlPersist should exit */
146static time_t control_persist_exit_time = 0;
147
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148/* Common data for the client loop code. */
Damien Millere1537f92010-01-26 13:26:22 +1100149volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000150static int escape_char1; /* Escape character. (proto1 only) */
151static int escape_pending1; /* Last character was an escape (proto1 only) */
Damien Miller95def091999-11-25 00:26:21 +1100152static int last_was_cr; /* Last character was a newline. */
Darren Tucker4d5cd332008-06-13 04:51:14 +1000153static int exit_status; /* Used to store the command exit status. */
154static int stdin_eof; /* EOF has been encountered on stderr. */
Damien Miller95def091999-11-25 00:26:21 +1100155static Buffer stdin_buffer; /* Buffer for stdin data. */
156static Buffer stdout_buffer; /* Buffer for stdout data. */
157static Buffer stderr_buffer; /* Buffer for stderr data. */
Damien Miller1ab6a512010-06-26 10:02:24 +1000158static u_int buffer_high; /* Soft max buffer size. */
Damien Miller95def091999-11-25 00:26:21 +1100159static int connection_in; /* Connection to server (input). */
160static int connection_out; /* Connection to server (output). */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000161static int need_rekeying; /* Set to non-zero if rekeying is requested. */
Damien Miller1ab6a512010-06-26 10:02:24 +1000162static int session_closed; /* In SSH2: login session closed. */
163static int x11_refuse_time; /* If >0, refuse x11 opens after this time. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164
Ben Lindstrombba81212001-06-25 05:01:22 +0000165static void client_init_dispatch(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000166int session_ident = -1;
167
Darren Tuckere32cf432010-01-08 16:51:40 +1100168int session_resumed = 0;
169
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000170/* Track escape per proto2 channel */
171struct escape_filter_ctx {
172 int escape_pending;
173 int escape_char;
174};
175
176/* Context for channel confirmation replies */
Damien Miller5771ed72008-05-19 15:35:33 +1000177struct channel_reply_ctx {
178 const char *request_type;
179 int id, do_close;
180};
181
Darren Tucker9f407c42008-06-13 04:50:27 +1000182/* Global request success/failure callbacks */
183struct global_confirm {
184 TAILQ_ENTRY(global_confirm) entry;
185 global_confirm_cb *cb;
186 void *ctx;
187 int ref_count;
188};
189TAILQ_HEAD(global_confirms, global_confirm);
190static struct global_confirms global_confirms =
191 TAILQ_HEAD_INITIALIZER(global_confirms);
192
Ben Lindstromf28f6342001-04-04 02:03:04 +0000193/*XXX*/
194extern Kex *xxx_kex;
195
Damien Miller0e220db2004-06-15 10:34:08 +1000196void ssh_process_session2_setup(int, int, int, Buffer *);
197
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198/* Restores stdin to blocking mode. */
199
Ben Lindstrombba81212001-06-25 05:01:22 +0000200static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000201leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202{
Damien Miller95def091999-11-25 00:26:21 +1100203 if (in_non_blocking_mode) {
Damien Miller03e66f62004-06-15 15:47:51 +1000204 unset_nonblock(fileno(stdin));
Damien Miller95def091999-11-25 00:26:21 +1100205 in_non_blocking_mode = 0;
Damien Miller95def091999-11-25 00:26:21 +1100206 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000207}
208
Damien Miller95def091999-11-25 00:26:21 +1100209/* Puts stdin terminal in non-blocking mode. */
210
Ben Lindstrombba81212001-06-25 05:01:22 +0000211static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000212enter_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000213{
Damien Miller95def091999-11-25 00:26:21 +1100214 in_non_blocking_mode = 1;
Damien Miller232711f2004-06-15 10:35:30 +1000215 set_nonblock(fileno(stdin));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000216}
217
Damien Miller5428f641999-11-25 11:54:57 +1100218/*
219 * Signal handler for the window change signal (SIGWINCH). This just sets a
220 * flag indicating that the window has changed.
221 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100222/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000223static void
Damien Miller95def091999-11-25 00:26:21 +1100224window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225{
Damien Miller95def091999-11-25 00:26:21 +1100226 received_window_change_signal = 1;
227 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228}
229
Damien Miller5428f641999-11-25 11:54:57 +1100230/*
231 * Signal handler for signals that cause the program to terminate. These
232 * signals must be trapped to restore terminal modes.
233 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100234/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000235static void
Damien Miller95def091999-11-25 00:26:21 +1100236signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000238 received_signal = sig;
239 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240}
241
Damien Miller5428f641999-11-25 11:54:57 +1100242/*
243 * Returns current time in seconds from Jan 1, 1970 with the maximum
244 * available resolution.
245 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246
Ben Lindstrombba81212001-06-25 05:01:22 +0000247static double
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000248get_current_time(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000249{
Damien Miller95def091999-11-25 00:26:21 +1100250 struct timeval tv;
251 gettimeofday(&tv, NULL);
252 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253}
254
Damien Millere11e1ea2010-08-03 16:04:46 +1000255/*
256 * Sets control_persist_exit_time to the absolute time when the
257 * backgrounded control master should exit due to expiry of the
258 * ControlPersist timeout. Sets it to 0 if we are not a backgrounded
259 * control master process, or if there is no ControlPersist timeout.
260 */
261static void
262set_control_persist_exit_time(void)
263{
264 if (muxserver_sock == -1 || !options.control_persist
Damien Miller6c3eec72011-05-05 14:16:22 +1000265 || options.control_persist_timeout == 0) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000266 /* not using a ControlPersist timeout */
267 control_persist_exit_time = 0;
Damien Miller6c3eec72011-05-05 14:16:22 +1000268 } else if (channel_still_open()) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000269 /* some client connections are still open */
270 if (control_persist_exit_time > 0)
271 debug2("%s: cancel scheduled exit", __func__);
272 control_persist_exit_time = 0;
273 } else if (control_persist_exit_time <= 0) {
274 /* a client connection has recently closed */
275 control_persist_exit_time = time(NULL) +
276 (time_t)options.control_persist_timeout;
277 debug2("%s: schedule exit in %d seconds", __func__,
278 options.control_persist_timeout);
279 }
280 /* else we are already counting down to the timeout */
281}
282
Damien Miller17e7ed02005-06-17 12:54:33 +1000283#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
284void
285client_x11_get_proto(const char *display, const char *xauth_path,
Damien Miller1ab6a512010-06-26 10:02:24 +1000286 u_int trusted, u_int timeout, char **_proto, char **_data)
Damien Miller17e7ed02005-06-17 12:54:33 +1000287{
288 char cmd[1024];
289 char line[512];
290 char xdisplay[512];
291 static char proto[512], data[512];
292 FILE *f;
293 int got_data = 0, generated = 0, do_unlink = 0, i;
294 char *xauthdir, *xauthfile;
295 struct stat st;
Damien Miller1ab6a512010-06-26 10:02:24 +1000296 u_int now;
Damien Miller17e7ed02005-06-17 12:54:33 +1000297
298 xauthdir = xauthfile = NULL;
299 *_proto = proto;
300 *_data = data;
301 proto[0] = data[0] = '\0';
302
303 if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) {
304 debug("No xauth program.");
305 } else {
306 if (display == NULL) {
307 debug("x11_get_proto: DISPLAY not set");
308 return;
309 }
310 /*
311 * Handle FamilyLocal case where $DISPLAY does
312 * not match an authorization entry. For this we
313 * just try "xauth list unix:displaynum.screennum".
314 * XXX: "localhost" match to determine FamilyLocal
315 * is not perfect.
316 */
317 if (strncmp(display, "localhost:", 10) == 0) {
318 snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
319 display + 10);
320 display = xdisplay;
321 }
322 if (trusted == 0) {
323 xauthdir = xmalloc(MAXPATHLEN);
324 xauthfile = xmalloc(MAXPATHLEN);
Damien Miller2cd62932010-12-01 11:50:35 +1100325 mktemp_proto(xauthdir, MAXPATHLEN);
Damien Miller17e7ed02005-06-17 12:54:33 +1000326 if (mkdtemp(xauthdir) != NULL) {
327 do_unlink = 1;
328 snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile",
329 xauthdir);
330 snprintf(cmd, sizeof(cmd),
331 "%s -f %s generate %s " SSH_X11_PROTO
Damien Miller1ab6a512010-06-26 10:02:24 +1000332 " untrusted timeout %u 2>" _PATH_DEVNULL,
333 xauth_path, xauthfile, display, timeout);
Damien Miller17e7ed02005-06-17 12:54:33 +1000334 debug2("x11_get_proto: %s", cmd);
335 if (system(cmd) == 0)
336 generated = 1;
Damien Miller1ab6a512010-06-26 10:02:24 +1000337 if (x11_refuse_time == 0) {
338 now = time(NULL) + 1;
339 if (UINT_MAX - timeout < now)
340 x11_refuse_time = UINT_MAX;
341 else
342 x11_refuse_time = now + timeout;
343 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000344 }
345 }
Darren Tucker513d13a2007-08-15 19:13:41 +1000346
347 /*
348 * When in untrusted mode, we read the cookie only if it was
349 * successfully generated as an untrusted one in the step
350 * above.
351 */
352 if (trusted || generated) {
353 snprintf(cmd, sizeof(cmd),
354 "%s %s%s list %s 2>" _PATH_DEVNULL,
355 xauth_path,
356 generated ? "-f " : "" ,
357 generated ? xauthfile : "",
358 display);
359 debug2("x11_get_proto: %s", cmd);
360 f = popen(cmd, "r");
361 if (f && fgets(line, sizeof(line), f) &&
362 sscanf(line, "%*s %511s %511s", proto, data) == 2)
363 got_data = 1;
364 if (f)
365 pclose(f);
366 } else
367 error("Warning: untrusted X11 forwarding setup failed: "
368 "xauth key data not generated");
Damien Miller17e7ed02005-06-17 12:54:33 +1000369 }
370
371 if (do_unlink) {
372 unlink(xauthfile);
373 rmdir(xauthdir);
374 }
375 if (xauthdir)
376 xfree(xauthdir);
377 if (xauthfile)
378 xfree(xauthfile);
379
380 /*
381 * If we didn't get authentication data, just make up some
382 * data. The forwarding code will check the validity of the
383 * response anyway, and substitute this data. The X11
384 * server, however, will ignore this fake data and use
385 * whatever authentication mechanisms it was using otherwise
386 * for the local connection.
387 */
388 if (!got_data) {
389 u_int32_t rnd = 0;
390
391 logit("Warning: No xauth data; "
392 "using fake authentication data for X11 forwarding.");
393 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
394 for (i = 0; i < 16; i++) {
395 if (i % 4 == 0)
396 rnd = arc4random();
397 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
398 rnd & 0xff);
399 rnd >>= 8;
400 }
401 }
402}
403
Damien Miller5428f641999-11-25 11:54:57 +1100404/*
405 * This is called when the interactive is entered. This checks if there is
406 * an EOF coming on stdin. We must check this explicitly, as select() does
407 * not appear to wake up when redirecting from /dev/null.
408 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000409
Ben Lindstrombba81212001-06-25 05:01:22 +0000410static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000411client_check_initial_eof_on_stdin(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000412{
Damien Miller95def091999-11-25 00:26:21 +1100413 int len;
414 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000415
Damien Miller5428f641999-11-25 11:54:57 +1100416 /*
417 * If standard input is to be "redirected from /dev/null", we simply
418 * mark that we have seen an EOF and send an EOF message to the
419 * server. Otherwise, we try to read a single character; it appears
420 * that for some files, such /dev/null, select() never wakes up for
421 * read for this descriptor, which means that we never get EOF. This
422 * way we will get the EOF if stdin comes from /dev/null or similar.
423 */
Damien Miller95def091999-11-25 00:26:21 +1100424 if (stdin_null_flag) {
425 /* Fake EOF on stdin. */
426 debug("Sending eof.");
427 stdin_eof = 1;
428 packet_start(SSH_CMSG_EOF);
429 packet_send();
430 } else {
Damien Miller95def091999-11-25 00:26:21 +1100431 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432
Damien Miller95def091999-11-25 00:26:21 +1100433 /* Check for immediate EOF on stdin. */
434 len = read(fileno(stdin), buf, 1);
435 if (len == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000436 /*
437 * EOF. Record that we have seen it and send
438 * EOF to server.
439 */
Damien Miller95def091999-11-25 00:26:21 +1100440 debug("Sending eof.");
441 stdin_eof = 1;
442 packet_start(SSH_CMSG_EOF);
443 packet_send();
444 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100445 /*
446 * Got data. We must store the data in the buffer,
447 * and also process it as an escape character if
448 * appropriate.
449 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000450 if ((u_char) buf[0] == escape_char1)
451 escape_pending1 = 1;
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000452 else
Damien Miller95def091999-11-25 00:26:21 +1100453 buffer_append(&stdin_buffer, buf, 1);
Damien Miller95def091999-11-25 00:26:21 +1100454 }
Damien Miller95def091999-11-25 00:26:21 +1100455 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000456 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457}
458
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000459
Damien Miller5428f641999-11-25 11:54:57 +1100460/*
461 * Make packets from buffered stdin data, and buffer them for sending to the
462 * connection.
463 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464
Ben Lindstrombba81212001-06-25 05:01:22 +0000465static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000466client_make_packets_from_stdin_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000468 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000469
Damien Miller95def091999-11-25 00:26:21 +1100470 /* Send buffered stdin data to the server. */
471 while (buffer_len(&stdin_buffer) > 0 &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100472 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100473 len = buffer_len(&stdin_buffer);
474 /* Keep the packets at reasonable size. */
475 if (len > packet_get_maxsize())
476 len = packet_get_maxsize();
477 packet_start(SSH_CMSG_STDIN_DATA);
478 packet_put_string(buffer_ptr(&stdin_buffer), len);
479 packet_send();
480 buffer_consume(&stdin_buffer, len);
481 /* If we have a pending EOF, send it now. */
482 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
483 packet_start(SSH_CMSG_EOF);
484 packet_send();
485 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000487}
488
Damien Miller5428f641999-11-25 11:54:57 +1100489/*
490 * Checks if the client window has changed, and sends a packet about it to
491 * the server if so. The actual change is detected elsewhere (by a software
492 * interrupt on Unix); this just checks the flag and sends a message if
493 * appropriate.
494 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000495
Ben Lindstrombba81212001-06-25 05:01:22 +0000496static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000497client_check_window_change(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000498{
Damien Miller1383bd82000-04-06 12:32:37 +1000499 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000500
Damien Miller1383bd82000-04-06 12:32:37 +1000501 if (! received_window_change_signal)
502 return;
503 /** XXX race */
504 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000505
Damien Millerd3444942000-09-30 14:20:03 +1100506 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000507
508 if (compat20) {
Damien Miller0e220db2004-06-15 10:34:08 +1000509 channel_send_window_changes();
Damien Miller1383bd82000-04-06 12:32:37 +1000510 } else {
Damien Miller0e220db2004-06-15 10:34:08 +1000511 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
512 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000513 packet_start(SSH_CMSG_WINDOW_SIZE);
Damien Miller71a73672006-03-26 14:04:36 +1100514 packet_put_int((u_int)ws.ws_row);
515 packet_put_int((u_int)ws.ws_col);
516 packet_put_int((u_int)ws.ws_xpixel);
517 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller1383bd82000-04-06 12:32:37 +1000518 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000520}
521
Damien Miller509b0102003-12-17 16:33:10 +1100522static void
523client_global_request_reply(int type, u_int32_t seq, void *ctxt)
524{
Darren Tucker9f407c42008-06-13 04:50:27 +1000525 struct global_confirm *gc;
526
527 if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
528 return;
529 if (gc->cb != NULL)
530 gc->cb(type, seq, gc->ctx);
531 if (--gc->ref_count <= 0) {
532 TAILQ_REMOVE(&global_confirms, gc, entry);
533 bzero(gc, sizeof(*gc));
534 xfree(gc);
535 }
536
Darren Tuckerf7288d72009-06-21 18:12:20 +1000537 packet_set_alive_timeouts(0);
Damien Miller509b0102003-12-17 16:33:10 +1100538}
539
540static void
541server_alive_check(void)
542{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000543 if (packet_inc_alive_timeouts() > options.server_alive_count_max) {
Damien Millerb73b6fd2011-01-11 17:18:56 +1100544 logit("Timeout, server %s not responding.", host);
Damien Miller985a4482006-10-24 03:02:41 +1000545 cleanup_exit(255);
546 }
Damien Miller509b0102003-12-17 16:33:10 +1100547 packet_start(SSH2_MSG_GLOBAL_REQUEST);
548 packet_put_cstring("keepalive@openssh.com");
549 packet_put_char(1); /* boolean: want reply */
550 packet_send();
Darren Tucker9f407c42008-06-13 04:50:27 +1000551 /* Insert an empty placeholder to maintain ordering */
552 client_register_global_confirm(NULL, NULL);
Damien Miller509b0102003-12-17 16:33:10 +1100553}
554
Damien Miller5428f641999-11-25 11:54:57 +1100555/*
556 * Waits until the client can do something (some data becomes available on
557 * one of the file descriptors).
558 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000559static void
Damien Miller5e953212001-01-30 09:14:00 +1100560client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000561 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000562{
Damien Miller509b0102003-12-17 16:33:10 +1100563 struct timeval tv, *tvp;
Damien Millere11e1ea2010-08-03 16:04:46 +1000564 int timeout_secs;
Damien Miller509b0102003-12-17 16:33:10 +1100565 int ret;
566
Damien Miller5e953212001-01-30 09:14:00 +1100567 /* Add any selections by the channel mechanism. */
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000568 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000569
Damien Miller1383bd82000-04-06 12:32:37 +1000570 if (!compat20) {
571 /* Read from the connection, unless our buffers are full. */
572 if (buffer_len(&stdout_buffer) < buffer_high &&
573 buffer_len(&stderr_buffer) < buffer_high &&
574 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100575 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000576 /*
577 * Read from stdin, unless we have seen EOF or have very much
578 * buffered data to send to the server.
579 */
580 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100581 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000582
583 /* Select stdout/stderr if have data in buffer. */
584 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100585 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000586 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100587 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000588 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000589 /* channel_prepare_select could have closed the last channel */
Damien Miller164a7f42001-10-12 11:36:09 +1000590 if (session_closed && !channel_still_open() &&
591 !packet_have_data_to_write()) {
592 /* clear mask since we did not call select() */
Damien Miller79faeff2001-11-12 11:06:32 +1100593 memset(*readsetp, 0, *nallocp);
594 memset(*writesetp, 0, *nallocp);
Damien Miller164a7f42001-10-12 11:36:09 +1000595 return;
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000596 } else {
597 FD_SET(connection_in, *readsetp);
598 }
Damien Miller1383bd82000-04-06 12:32:37 +1000599 }
600
Damien Miller95def091999-11-25 00:26:21 +1100601 /* Select server connection if have data to write to the server. */
602 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100603 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100604
Damien Miller5428f641999-11-25 11:54:57 +1100605 /*
606 * Wait for something to happen. This will suspend the process until
607 * some selected descriptor can be read, written, or has some other
Damien Millere11e1ea2010-08-03 16:04:46 +1000608 * event pending, or a timeout expires.
Damien Miller5428f641999-11-25 11:54:57 +1100609 */
Damien Miller95def091999-11-25 00:26:21 +1100610
Damien Millere11e1ea2010-08-03 16:04:46 +1000611 timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */
612 if (options.server_alive_interval > 0 && compat20)
613 timeout_secs = options.server_alive_interval;
614 set_control_persist_exit_time();
615 if (control_persist_exit_time > 0) {
616 timeout_secs = MIN(timeout_secs,
617 control_persist_exit_time - time(NULL));
618 if (timeout_secs < 0)
619 timeout_secs = 0;
620 }
621 if (timeout_secs == INT_MAX)
Damien Miller509b0102003-12-17 16:33:10 +1100622 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000623 else {
Damien Millere11e1ea2010-08-03 16:04:46 +1000624 tv.tv_sec = timeout_secs;
Damien Miller509b0102003-12-17 16:33:10 +1100625 tv.tv_usec = 0;
626 tvp = &tv;
627 }
Damien Millere11e1ea2010-08-03 16:04:46 +1000628
Damien Miller509b0102003-12-17 16:33:10 +1100629 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
630 if (ret < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100631 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000632
633 /*
634 * We have to clear the select masks, because we return.
635 * We have to return, because the mainloop checks for the flags
636 * set by the signal handlers.
637 */
Damien Miller79faeff2001-11-12 11:06:32 +1100638 memset(*readsetp, 0, *nallocp);
639 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000640
Damien Miller95def091999-11-25 00:26:21 +1100641 if (errno == EINTR)
642 return;
643 /* Note: we might still have data in the buffers. */
644 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
645 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100646 quit_pending = 1;
Damien Miller509b0102003-12-17 16:33:10 +1100647 } else if (ret == 0)
648 server_alive_check();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000649}
650
Ben Lindstrombba81212001-06-25 05:01:22 +0000651static void
Damien Millerad833b32000-08-23 10:46:23 +1000652client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000653{
Damien Miller95def091999-11-25 00:26:21 +1100654 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000655 if (buffer_len(bout) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000656 atomicio(vwrite, fileno(stdout), buffer_ptr(bout),
657 buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000658 if (buffer_len(berr) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000659 atomicio(vwrite, fileno(stderr), buffer_ptr(berr),
660 buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000661
Damien Miller21771e22011-05-15 08:45:50 +1000662 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000663
Damien Miller5428f641999-11-25 11:54:57 +1100664 /*
665 * Free (and clear) the buffer to reduce the amount of data that gets
666 * written to swap.
667 */
Damien Millerad833b32000-08-23 10:46:23 +1000668 buffer_free(bin);
669 buffer_free(bout);
670 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000671
Damien Miller95def091999-11-25 00:26:21 +1100672 /* Send the suspend signal to the program itself. */
673 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000674
Darren Tucker5d78de62004-11-05 20:35:44 +1100675 /* Reset window sizes in case they have changed */
676 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000677
Damien Miller95def091999-11-25 00:26:21 +1100678 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000679 buffer_init(bin);
680 buffer_init(bout);
681 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000682
Damien Miller21771e22011-05-15 08:45:50 +1000683 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000684}
685
Ben Lindstrombba81212001-06-25 05:01:22 +0000686static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100687client_process_net_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000688{
Darren Tuckerc5564e12009-06-21 18:53:53 +1000689 int len, cont = 0;
Darren Tucker86e30a02009-08-28 11:21:06 +1000690 char buf[SSH_IOBUFSZ];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000691
Damien Miller5428f641999-11-25 11:54:57 +1100692 /*
693 * Read input from the server, and add any such data to the buffer of
694 * the packet subsystem.
695 */
Damien Miller95def091999-11-25 00:26:21 +1100696 if (FD_ISSET(connection_in, readset)) {
697 /* Read as much as possible. */
Darren Tuckerc5564e12009-06-21 18:53:53 +1000698 len = roaming_read(connection_in, buf, sizeof(buf), &cont);
699 if (len == 0 && cont == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000700 /*
701 * Received EOF. The remote host has closed the
702 * connection.
703 */
704 snprintf(buf, sizeof buf,
705 "Connection to %.300s closed by remote host.\r\n",
706 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000707 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000708 quit_pending = 1;
709 return;
Damien Miller95def091999-11-25 00:26:21 +1100710 }
Damien Miller5428f641999-11-25 11:54:57 +1100711 /*
712 * There is a kernel bug on Solaris that causes select to
713 * sometimes wake up even though there is no data available.
714 */
Damien Millerd8968ad2008-07-04 23:10:49 +1000715 if (len < 0 &&
716 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Damien Miller95def091999-11-25 00:26:21 +1100717 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000718
Damien Miller95def091999-11-25 00:26:21 +1100719 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000720 /*
721 * An error has encountered. Perhaps there is a
722 * network problem.
723 */
724 snprintf(buf, sizeof buf,
725 "Read from remote host %.300s: %.100s\r\n",
726 host, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100727 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100728 quit_pending = 1;
729 return;
730 }
731 packet_process_incoming(buf, len);
732 }
Damien Miller1383bd82000-04-06 12:32:37 +1000733}
734
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000735static void
Damien Miller5771ed72008-05-19 15:35:33 +1000736client_status_confirm(int type, Channel *c, void *ctx)
Damien Miller0e220db2004-06-15 10:34:08 +1000737{
Damien Miller5771ed72008-05-19 15:35:33 +1000738 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
739 char errmsg[256];
740 int tochan;
Darren Tuckerfc959702004-07-17 16:12:08 +1000741
Damien Miller5771ed72008-05-19 15:35:33 +1000742 /* XXX supress on mux _client_ quietmode */
743 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
Damien Millere1537f92010-01-26 13:26:22 +1100744 c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
Damien Miller0e220db2004-06-15 10:34:08 +1000745
Damien Miller5771ed72008-05-19 15:35:33 +1000746 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
747 debug2("%s request accepted on channel %d",
748 cr->request_type, c->self);
749 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
750 if (tochan) {
751 snprintf(errmsg, sizeof(errmsg),
752 "%s request failed\r\n", cr->request_type);
753 } else {
754 snprintf(errmsg, sizeof(errmsg),
755 "%s request failed on channel %d",
756 cr->request_type, c->self);
757 }
758 /* If error occurred on primary session channel, then exit */
759 if (cr->do_close && c->self == session_ident)
760 fatal("%s", errmsg);
761 /* If error occurred on mux client, append to their stderr */
762 if (tochan)
763 buffer_append(&c->extended, errmsg, strlen(errmsg));
764 else
765 error("%s", errmsg);
766 if (cr->do_close) {
767 chan_read_failed(c);
768 chan_write_failed(c);
769 }
Damien Miller0e220db2004-06-15 10:34:08 +1000770 }
Damien Miller5771ed72008-05-19 15:35:33 +1000771 xfree(cr);
772}
Damien Miller0e220db2004-06-15 10:34:08 +1000773
Damien Miller5771ed72008-05-19 15:35:33 +1000774static void
775client_abandon_status_confirm(Channel *c, void *ctx)
776{
777 xfree(ctx);
778}
779
780static void
781client_expect_confirm(int id, const char *request, int do_close)
782{
783 struct channel_reply_ctx *cr = xmalloc(sizeof(*cr));
784
785 cr->request_type = request;
786 cr->do_close = do_close;
787
788 channel_register_status_confirm(id, client_status_confirm,
789 client_abandon_status_confirm, cr);
Damien Miller0e220db2004-06-15 10:34:08 +1000790}
791
Darren Tucker9f407c42008-06-13 04:50:27 +1000792void
793client_register_global_confirm(global_confirm_cb *cb, void *ctx)
794{
Damien Millerb9d3bee2008-07-16 22:40:52 +1000795 struct global_confirm *gc, *last_gc;
Darren Tucker9f407c42008-06-13 04:50:27 +1000796
797 /* Coalesce identical callbacks */
Damien Millerb9d3bee2008-07-16 22:40:52 +1000798 last_gc = TAILQ_LAST(&global_confirms, global_confirms);
799 if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
800 if (++last_gc->ref_count >= INT_MAX)
801 fatal("%s: last_gc->ref_count = %d",
802 __func__, last_gc->ref_count);
Darren Tucker9f407c42008-06-13 04:50:27 +1000803 return;
804 }
805
806 gc = xmalloc(sizeof(*gc));
807 gc->cb = cb;
808 gc->ctx = ctx;
809 gc->ref_count = 1;
810 TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
811}
812
Damien Miller0e220db2004-06-15 10:34:08 +1000813static void
Ben Lindstrom681d9322002-03-22 03:53:00 +0000814process_cmdline(void)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000815{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000816 void (*handler)(int);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100817 char *s, *cmd, *cancel_host;
Darren Tuckere7066df2004-05-24 10:18:05 +1000818 int delete = 0;
Damien Miller0164cb82008-11-05 16:30:31 +1100819 int local = 0, remote = 0, dynamic = 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100820 int cancel_port;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100821 Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000822
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100823 bzero(&fwd, sizeof(fwd));
824 fwd.listen_host = fwd.connect_host = NULL;
825
Damien Miller21771e22011-05-15 08:45:50 +1000826 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000827 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000828 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000829 if (s == NULL)
830 goto out;
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100831 while (isspace(*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000832 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000833 if (*s == '-')
834 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000835 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000836 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000837
Darren Tucker1973c882004-05-24 10:34:36 +1000838 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000839 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000840 logit(" -L[bind_address:]port:host:hostport "
841 "Request local forward");
842 logit(" -R[bind_address:]port:host:hostport "
843 "Request remote forward");
Damien Miller0164cb82008-11-05 16:30:31 +1100844 logit(" -D[bind_address:]port "
845 "Request dynamic forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000846 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000847 "Cancel remote forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100848 if (!options.permit_local_command)
849 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000850 logit(" !args "
851 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100852 goto out;
853 }
854
855 if (*s == '!' && options.permit_local_command) {
856 s++;
857 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000858 goto out;
859 }
860
861 if (*s == 'K') {
862 delete = 1;
863 s++;
864 }
Damien Miller0164cb82008-11-05 16:30:31 +1100865 if (*s == 'L')
866 local = 1;
867 else if (*s == 'R')
868 remote = 1;
869 else if (*s == 'D')
870 dynamic = 1;
871 else {
Damien Miller996acd22003-04-09 20:59:48 +1000872 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000873 goto out;
874 }
Damien Miller0164cb82008-11-05 16:30:31 +1100875
876 if ((local || dynamic) && delete) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000877 logit("Not supported.");
878 goto out;
879 }
Damien Miller0164cb82008-11-05 16:30:31 +1100880 if (remote && delete && !compat20) {
Damien Miller996acd22003-04-09 20:59:48 +1000881 logit("Not supported for SSH protocol version 1.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000882 goto out;
883 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000884
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100885 while (isspace(*++s))
886 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000887
Damien Millere1537f92010-01-26 13:26:22 +1100888 /* XXX update list of forwards in options */
Darren Tuckere7066df2004-05-24 10:18:05 +1000889 if (delete) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100890 cancel_port = 0;
891 cancel_host = hpdelim(&s); /* may be NULL */
892 if (s != NULL) {
893 cancel_port = a2port(s);
894 cancel_host = cleanhostname(cancel_host);
895 } else {
896 cancel_port = a2port(cancel_host);
897 cancel_host = NULL;
898 }
Damien Miller3dc71ad2009-01-28 16:31:22 +1100899 if (cancel_port <= 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100900 logit("Bad forwarding close port");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000901 goto out;
902 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100903 channel_request_rforward_cancel(cancel_host, cancel_port);
Darren Tuckere7066df2004-05-24 10:18:05 +1000904 } else {
Damien Miller4bf648f2009-02-14 16:28:21 +1100905 if (!parse_forward(&fwd, s, dynamic, remote)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000906 logit("Bad forwarding specification.");
907 goto out;
908 }
Damien Miller0164cb82008-11-05 16:30:31 +1100909 if (local || dynamic) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100910 if (channel_setup_local_fwd_listener(fwd.listen_host,
911 fwd.listen_port, fwd.connect_host,
912 fwd.connect_port, options.gateway_ports) < 0) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000913 logit("Port forwarding failed.");
914 goto out;
915 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100916 } else {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000917 if (channel_request_remote_forwarding(fwd.listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100918 fwd.listen_port, fwd.connect_host,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000919 fwd.connect_port) < 0) {
920 logit("Port forwarding failed.");
921 goto out;
922 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100923 }
924
Darren Tuckere7066df2004-05-24 10:18:05 +1000925 logit("Forwarding port.");
926 }
927
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000928out:
929 signal(SIGINT, handler);
Damien Miller21771e22011-05-15 08:45:50 +1000930 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000931 if (cmd)
932 xfree(cmd);
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100933 if (fwd.listen_host != NULL)
934 xfree(fwd.listen_host);
935 if (fwd.connect_host != NULL)
936 xfree(fwd.connect_host);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000937}
938
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000939/*
940 * Process the characters one by one, call with c==NULL for proto1 case.
941 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000942static int
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000943process_escapes(Channel *c, Buffer *bin, Buffer *bout, Buffer *berr,
944 char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +1000945{
946 char string[1024];
947 pid_t pid;
948 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000949 u_int i;
950 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +1000951 char *s;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000952 int *escape_pendingp, escape_char;
953 struct escape_filter_ctx *efc;
Damien Millerad833b32000-08-23 10:46:23 +1000954
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000955 if (c == NULL) {
956 escape_pendingp = &escape_pending1;
957 escape_char = escape_char1;
958 } else {
959 if (c->filter_ctx == NULL)
960 return 0;
961 efc = (struct escape_filter_ctx *)c->filter_ctx;
962 escape_pendingp = &efc->escape_pending;
963 escape_char = efc->escape_char;
964 }
965
Damien Millereccb9de2005-06-17 12:59:34 +1000966 if (len <= 0)
967 return (0);
968
969 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +1000970 /* Get one character at a time. */
971 ch = buf[i];
972
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000973 if (*escape_pendingp) {
Damien Millerad833b32000-08-23 10:46:23 +1000974 /* We have previously seen an escape character. */
975 /* Clear the flag now. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000976 *escape_pendingp = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000977
978 /* Process the escaped character. */
979 switch (ch) {
980 case '.':
981 /* Terminate the connection. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000982 snprintf(string, sizeof string, "%c.\r\n",
983 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +1000984 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +1000985
Damien Millere1537f92010-01-26 13:26:22 +1100986 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000987 chan_read_failed(c);
988 chan_write_failed(c);
989 return 0;
990 } else
991 quit_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +1000992 return -1;
993
994 case 'Z' - 64:
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000995 /* XXX support this for mux clients */
Damien Millere1537f92010-01-26 13:26:22 +1100996 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000997 noescape:
998 snprintf(string, sizeof string,
999 "%c%c escape not available to "
1000 "multiplexed sessions\r\n",
1001 escape_char, ch);
1002 buffer_append(berr, string,
1003 strlen(string));
1004 continue;
1005 }
Darren Tucker4d5cd332008-06-13 04:51:14 +10001006 /* Suspend the program. Inform the user */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001007 snprintf(string, sizeof string,
1008 "%c^Z [suspend ssh]\r\n", escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001009 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001010
1011 /* Restore terminal modes and suspend. */
1012 client_suspend_self(bin, bout, berr);
1013
1014 /* We have been continued. */
1015 continue;
1016
Damien Miller54c45982003-05-15 10:20:13 +10001017 case 'B':
1018 if (compat20) {
1019 snprintf(string, sizeof string,
1020 "%cB\r\n", escape_char);
1021 buffer_append(berr, string,
1022 strlen(string));
1023 channel_request_start(session_ident,
1024 "break", 0);
1025 packet_put_int(1000);
1026 packet_send();
1027 }
1028 continue;
1029
Ben Lindstromf28f6342001-04-04 02:03:04 +00001030 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001031 if (compat20) {
1032 if (datafellows & SSH_BUG_NOREKEY)
Darren Tucker4d5cd332008-06-13 04:51:14 +10001033 logit("Server does not "
1034 "support re-keying");
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001035 else
1036 need_rekeying = 1;
1037 }
Ben Lindstromf28f6342001-04-04 02:03:04 +00001038 continue;
1039
Damien Millerad833b32000-08-23 10:46:23 +10001040 case '&':
Damien Millere1537f92010-01-26 13:26:22 +11001041 if (c && c->ctl_chan != -1)
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001042 goto noescape;
Damien Millerad833b32000-08-23 10:46:23 +10001043 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001044 * Detach the program (continue to serve
1045 * connections, but put in background and no
1046 * more new connections).
Damien Millerad833b32000-08-23 10:46:23 +10001047 */
Damien Miller96507ef2001-11-12 10:52:25 +11001048 /* Restore tty modes. */
Damien Miller21771e22011-05-15 08:45:50 +10001049 leave_raw_mode(
1050 options.request_tty == REQUEST_TTY_FORCE);
Damien Miller96507ef2001-11-12 10:52:25 +11001051
1052 /* Stop listening for new connections. */
1053 channel_stop_listening();
1054
1055 snprintf(string, sizeof string,
1056 "%c& [backgrounded]\n", escape_char);
1057 buffer_append(berr, string, strlen(string));
1058
1059 /* Fork into background. */
1060 pid = fork();
1061 if (pid < 0) {
1062 error("fork: %.100s", strerror(errno));
1063 continue;
1064 }
1065 if (pid != 0) { /* This is the parent. */
1066 /* The parent just exits. */
1067 exit(0);
1068 }
1069 /* The child continues serving connections. */
1070 if (compat20) {
1071 buffer_append(bin, "\004", 1);
1072 /* fake EOF on stdin */
1073 return -1;
1074 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +10001075 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001076 * Sending SSH_CMSG_EOF alone does not
1077 * always appear to be enough. So we
1078 * try to send an EOF character first.
Damien Millerad833b32000-08-23 10:46:23 +10001079 */
1080 packet_start(SSH_CMSG_STDIN_DATA);
1081 packet_put_string("\004", 1);
1082 packet_send();
1083 /* Close stdin. */
1084 stdin_eof = 1;
1085 if (buffer_len(bin) == 0) {
1086 packet_start(SSH_CMSG_EOF);
1087 packet_send();
1088 }
1089 }
Damien Miller96507ef2001-11-12 10:52:25 +11001090 continue;
Damien Millerad833b32000-08-23 10:46:23 +10001091
1092 case '?':
Damien Millere1537f92010-01-26 13:26:22 +11001093 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001094 snprintf(string, sizeof string,
Damien Millerad833b32000-08-23 10:46:23 +10001095"%c?\r\n\
1096Supported escape sequences:\r\n\
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001097 %c. - terminate session\r\n\
1098 %cB - send a BREAK to the remote system\r\n\
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001099 %cR - Request rekey (SSH protocol 2 only)\r\n\
1100 %c# - list forwarded connections\r\n\
1101 %c? - this message\r\n\
1102 %c%c - send the escape character by typing it twice\r\n\
Damien Millerad833b32000-08-23 10:46:23 +10001103(Note that escapes are only recognized immediately after newline.)\r\n",
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001104 escape_char, escape_char,
1105 escape_char, escape_char,
1106 escape_char, escape_char,
Damien Miller586b0052008-12-09 14:11:32 +11001107 escape_char, escape_char);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001108 } else {
1109 snprintf(string, sizeof string,
1110"%c?\r\n\
1111Supported escape sequences:\r\n\
1112 %c. - terminate connection (and any multiplexed sessions)\r\n\
1113 %cB - send a BREAK to the remote system\r\n\
1114 %cC - open a command line\r\n\
1115 %cR - Request rekey (SSH protocol 2 only)\r\n\
1116 %c^Z - suspend ssh\r\n\
1117 %c# - list forwarded connections\r\n\
1118 %c& - background ssh (when waiting for connections to terminate)\r\n\
1119 %c? - this message\r\n\
1120 %c%c - send the escape character by typing it twice\r\n\
1121(Note that escapes are only recognized immediately after newline.)\r\n",
1122 escape_char, escape_char,
1123 escape_char, escape_char,
1124 escape_char, escape_char,
1125 escape_char, escape_char,
1126 escape_char, escape_char,
1127 escape_char);
1128 }
Damien Millerad833b32000-08-23 10:46:23 +10001129 buffer_append(berr, string, strlen(string));
1130 continue;
1131
1132 case '#':
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001133 snprintf(string, sizeof string, "%c#\r\n",
1134 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001135 buffer_append(berr, string, strlen(string));
1136 s = channel_open_message();
1137 buffer_append(berr, s, strlen(s));
1138 xfree(s);
1139 continue;
1140
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001141 case 'C':
Damien Millere1537f92010-01-26 13:26:22 +11001142 if (c && c->ctl_chan != -1)
Damien Miller586b0052008-12-09 14:11:32 +11001143 goto noescape;
Ben Lindstrom681d9322002-03-22 03:53:00 +00001144 process_cmdline();
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001145 continue;
1146
Damien Millerad833b32000-08-23 10:46:23 +10001147 default:
1148 if (ch != escape_char) {
1149 buffer_put_char(bin, escape_char);
1150 bytes++;
1151 }
1152 /* Escaped characters fall through here */
1153 break;
1154 }
1155 } else {
1156 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001157 * The previous character was not an escape char.
1158 * Check if this is an escape.
Damien Millerad833b32000-08-23 10:46:23 +10001159 */
1160 if (last_was_cr && ch == escape_char) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001161 /*
1162 * It is. Set the flag and continue to
1163 * next character.
1164 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001165 *escape_pendingp = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001166 continue;
1167 }
1168 }
1169
1170 /*
1171 * Normal character. Record whether it was a newline,
1172 * and append it to the buffer.
1173 */
1174 last_was_cr = (ch == '\r' || ch == '\n');
1175 buffer_put_char(bin, ch);
1176 bytes++;
1177 }
1178 return bytes;
1179}
1180
Ben Lindstrombba81212001-06-25 05:01:22 +00001181static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001182client_process_input(fd_set *readset)
Damien Miller1383bd82000-04-06 12:32:37 +10001183{
Damien Miller166fca82000-04-20 07:42:21 +10001184 int len;
Darren Tucker86e30a02009-08-28 11:21:06 +10001185 char buf[SSH_IOBUFSZ];
Damien Miller1383bd82000-04-06 12:32:37 +10001186
Damien Miller95def091999-11-25 00:26:21 +11001187 /* Read input from stdin. */
1188 if (FD_ISSET(fileno(stdin), readset)) {
1189 /* Read as much as possible. */
1190 len = read(fileno(stdin), buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +10001191 if (len < 0 &&
1192 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001193 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +11001194 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001195 /*
1196 * Received EOF or error. They are treated
1197 * similarly, except that an error message is printed
1198 * if it was an error condition.
1199 */
Damien Miller95def091999-11-25 00:26:21 +11001200 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001201 snprintf(buf, sizeof buf, "read: %.100s\r\n",
1202 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001203 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001204 }
1205 /* Mark that we have seen EOF. */
1206 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +11001207 /*
1208 * Send an EOF message to the server unless there is
1209 * data in the buffer. If there is data in the
1210 * buffer, no message will be sent now. Code
1211 * elsewhere will send the EOF when the buffer
1212 * becomes empty if stdin_eof is set.
1213 */
Damien Miller95def091999-11-25 00:26:21 +11001214 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001215 packet_start(SSH_CMSG_EOF);
1216 packet_send();
Damien Miller95def091999-11-25 00:26:21 +11001217 }
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001218 } else if (escape_char1 == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +11001219 /*
1220 * Normal successful read, and no escape character.
1221 * Just append the data to buffer.
1222 */
Damien Miller95def091999-11-25 00:26:21 +11001223 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +11001224 } else {
Damien Miller5428f641999-11-25 11:54:57 +11001225 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001226 * Normal, successful read. But we have an escape
1227 * character and have to process the characters one
1228 * by one.
Damien Miller5428f641999-11-25 11:54:57 +11001229 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001230 if (process_escapes(NULL, &stdin_buffer,
1231 &stdout_buffer, &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +10001232 return;
Damien Miller95def091999-11-25 00:26:21 +11001233 }
1234 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001235}
1236
Ben Lindstrombba81212001-06-25 05:01:22 +00001237static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001238client_process_output(fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001239{
Damien Miller95def091999-11-25 00:26:21 +11001240 int len;
1241 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001242
Damien Miller95def091999-11-25 00:26:21 +11001243 /* Write buffered output to stdout. */
1244 if (FD_ISSET(fileno(stdout), writeset)) {
1245 /* Write as much data as possible. */
1246 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001247 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001248 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001249 if (errno == EINTR || errno == EAGAIN ||
1250 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001251 len = 0;
1252 else {
Damien Miller5428f641999-11-25 11:54:57 +11001253 /*
1254 * An error or EOF was encountered. Put an
1255 * error message to stderr buffer.
1256 */
Darren Tucker4d5cd332008-06-13 04:51:14 +10001257 snprintf(buf, sizeof buf,
1258 "write stdout: %.50s\r\n", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001259 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001260 quit_pending = 1;
1261 return;
1262 }
1263 }
1264 /* Consume printed data from the buffer. */
1265 buffer_consume(&stdout_buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001266 }
Damien Miller95def091999-11-25 00:26:21 +11001267 /* Write buffered output to stderr. */
1268 if (FD_ISSET(fileno(stderr), writeset)) {
1269 /* Write as much data as possible. */
1270 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001271 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001272 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001273 if (errno == EINTR || errno == EAGAIN ||
1274 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001275 len = 0;
1276 else {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001277 /*
1278 * EOF or error, but can't even print
1279 * error message.
1280 */
Damien Miller95def091999-11-25 00:26:21 +11001281 quit_pending = 1;
1282 return;
1283 }
1284 }
1285 /* Consume printed characters from the buffer. */
1286 buffer_consume(&stderr_buffer, len);
1287 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001288}
1289
Damien Miller5428f641999-11-25 11:54:57 +11001290/*
Damien Millerb38eff82000-04-01 11:09:21 +10001291 * Get packets from the connection input buffer, and process them as long as
1292 * there are packets available.
1293 *
1294 * Any unknown packets received during the actual
1295 * session cause the session to terminate. This is
1296 * intended to make debugging easier since no
1297 * confirmations are sent. Any compatible protocol
1298 * extensions must be negotiated during the
1299 * preparatory phase.
1300 */
1301
Ben Lindstrombba81212001-06-25 05:01:22 +00001302static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001303client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001304{
Darren Tucker4d5cd332008-06-13 04:51:14 +10001305 dispatch_run(DISPATCH_NONBLOCK, &quit_pending,
1306 compat20 ? xxx_kex : NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001307}
1308
Damien Millerad833b32000-08-23 10:46:23 +10001309/* scan buf[] for '~' before sending data to the peer */
1310
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001311/* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1312void *
1313client_new_escape_filter_ctx(int escape_char)
1314{
1315 struct escape_filter_ctx *ret;
1316
1317 ret = xmalloc(sizeof(*ret));
1318 ret->escape_pending = 0;
1319 ret->escape_char = escape_char;
1320 return (void *)ret;
1321}
1322
Darren Tucker84c56f52008-06-13 04:55:46 +10001323/* Free the escape filter context on channel free */
1324void
1325client_filter_cleanup(int cid, void *ctx)
1326{
1327 xfree(ctx);
1328}
1329
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001330int
1331client_simple_escape_filter(Channel *c, char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001332{
Damien Miller5771ed72008-05-19 15:35:33 +10001333 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1334 return 0;
1335
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001336 return process_escapes(c, &c->input, &c->output, &c->extended,
1337 buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001338}
1339
Ben Lindstrombba81212001-06-25 05:01:22 +00001340static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001341client_channel_closed(int id, void *arg)
1342{
Damien Miller3ec27592001-10-12 11:35:04 +10001343 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001344 session_closed = 1;
Damien Miller21771e22011-05-15 08:45:50 +10001345 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001346}
1347
Damien Millerb38eff82000-04-01 11:09:21 +10001348/*
Damien Miller5428f641999-11-25 11:54:57 +11001349 * Implements the interactive session with the server. This is called after
1350 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001351 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1352 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001353 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001354
Damien Miller4af51302000-04-16 11:18:38 +10001355int
Damien Millerad833b32000-08-23 10:46:23 +10001356client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001357{
Damien Miller5e953212001-01-30 09:14:00 +11001358 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001359 double start_time, total_time;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001360 int max_fd = 0, max_fd2 = 0, len, rekeying = 0;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001361 u_int64_t ibytes, obytes;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001362 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001363 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001364
Damien Miller95def091999-11-25 00:26:21 +11001365 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001366
Damien Miller95def091999-11-25 00:26:21 +11001367 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001368
Damien Miller95def091999-11-25 00:26:21 +11001369 /* Initialize variables. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001370 escape_pending1 = 0;
Damien Miller95def091999-11-25 00:26:21 +11001371 last_was_cr = 1;
1372 exit_status = -1;
1373 stdin_eof = 0;
1374 buffer_high = 64 * 1024;
1375 connection_in = packet_get_connection_in();
1376 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +11001377 max_fd = MAX(connection_in, connection_out);
1378
1379 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +00001380 /* enable nonblocking unless tty */
1381 if (!isatty(fileno(stdin)))
1382 set_nonblock(fileno(stdin));
1383 if (!isatty(fileno(stdout)))
1384 set_nonblock(fileno(stdout));
1385 if (!isatty(fileno(stderr)))
1386 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +11001387 max_fd = MAX(max_fd, fileno(stdin));
1388 max_fd = MAX(max_fd, fileno(stdout));
1389 max_fd = MAX(max_fd, fileno(stderr));
1390 }
Damien Miller95def091999-11-25 00:26:21 +11001391 quit_pending = 0;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001392 escape_char1 = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001393
Damien Miller95def091999-11-25 00:26:21 +11001394 /* Initialize buffers. */
1395 buffer_init(&stdin_buffer);
1396 buffer_init(&stdout_buffer);
1397 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001398
Damien Millerb38eff82000-04-01 11:09:21 +10001399 client_init_dispatch();
1400
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001401 /*
1402 * Set signal handlers, (e.g. to restore non-blocking mode)
1403 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1404 */
Darren Tucker07336da2004-11-05 20:02:16 +11001405 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1406 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001407 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1408 signal(SIGINT, signal_handler);
1409 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1410 signal(SIGQUIT, signal_handler);
1411 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1412 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001413 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001414
Damien Miller95def091999-11-25 00:26:21 +11001415 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001416 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001417
Ben Lindstrom5b828322001-02-09 01:34:36 +00001418 if (compat20) {
1419 session_ident = ssh2_chan_id;
Damien Miller6c3eec72011-05-05 14:16:22 +10001420 if (session_ident != -1) {
1421 if (escape_char_arg != SSH_ESCAPECHAR_NONE) {
1422 channel_register_filter(session_ident,
1423 client_simple_escape_filter, NULL,
1424 client_filter_cleanup,
1425 client_new_escape_filter_ctx(
1426 escape_char_arg));
1427 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001428 channel_register_cleanup(session_ident,
Damien Miller39eda6e2005-11-05 14:52:50 +11001429 client_channel_closed, 0);
Damien Miller6c3eec72011-05-05 14:16:22 +10001430 }
Ben Lindstrom5b828322001-02-09 01:34:36 +00001431 } else {
1432 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +10001433 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001434 }
Damien Millerad833b32000-08-23 10:46:23 +10001435
Damien Miller95def091999-11-25 00:26:21 +11001436 /* Main loop of the client for the interactive session mode. */
1437 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001438
Damien Miller5428f641999-11-25 11:54:57 +11001439 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001440 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001441
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001442 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +10001443 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001444
1445 rekeying = (xxx_kex != NULL && !xxx_kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +10001446
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001447 if (rekeying) {
1448 debug("rekeying in progress");
1449 } else {
1450 /*
1451 * Make packets of buffered stdin data, and buffer
1452 * them for sending to the server.
1453 */
1454 if (!compat20)
1455 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001456
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001457 /*
1458 * Make packets from buffered channel data, and
1459 * enqueue them for sending to the server.
1460 */
1461 if (packet_not_very_much_data_to_write())
1462 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001463
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001464 /*
1465 * Check if the window size has changed, and buffer a
1466 * message about it to the server if so.
1467 */
1468 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001469
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001470 if (quit_pending)
1471 break;
1472 }
Damien Miller5428f641999-11-25 11:54:57 +11001473 /*
1474 * Wait until we have something to do (something becomes
1475 * available on one of the descriptors).
1476 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001477 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001478 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001479 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001480
Damien Miller95def091999-11-25 00:26:21 +11001481 if (quit_pending)
1482 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001483
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001484 /* Do channel operations unless rekeying in progress. */
1485 if (!rekeying) {
1486 channel_after_select(readset, writeset);
Damien Millera5539d22003-04-09 20:50:06 +10001487 if (need_rekeying || packet_need_rekeying()) {
1488 debug("need rekeying");
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001489 xxx_kex->done = 0;
1490 kex_send_kexinit(xxx_kex);
1491 need_rekeying = 0;
1492 }
1493 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001494
Damien Miller1383bd82000-04-06 12:32:37 +10001495 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001496 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001497
Damien Miller1383bd82000-04-06 12:32:37 +10001498 if (quit_pending)
1499 break;
1500
1501 if (!compat20) {
1502 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +11001503 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +10001504 /*
1505 * Process output to stdout and stderr. Output to
1506 * the connection is processed elsewhere (above).
1507 */
Damien Miller5e953212001-01-30 09:14:00 +11001508 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +10001509 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001510
Darren Tuckere32cf432010-01-08 16:51:40 +11001511 if (session_resumed) {
1512 connection_in = packet_get_connection_in();
1513 connection_out = packet_get_connection_out();
1514 max_fd = MAX(max_fd, connection_out);
1515 max_fd = MAX(max_fd, connection_in);
1516 session_resumed = 0;
1517 }
1518
Darren Tucker4d5cd332008-06-13 04:51:14 +10001519 /*
1520 * Send as much buffered packet data as possible to the
1521 * sender.
1522 */
Damien Miller5e953212001-01-30 09:14:00 +11001523 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001524 packet_write_poll();
Damien Millere11e1ea2010-08-03 16:04:46 +10001525
1526 /*
1527 * If we are a backgrounded control master, and the
1528 * timeout has expired without any active client
1529 * connections, then quit.
1530 */
1531 if (control_persist_exit_time > 0) {
1532 if (time(NULL) >= control_persist_exit_time) {
1533 debug("ControlPersist timeout expired");
1534 break;
1535 }
1536 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001537 }
Damien Miller5e953212001-01-30 09:14:00 +11001538 if (readset)
1539 xfree(readset);
1540 if (writeset)
1541 xfree(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001542
Damien Miller95def091999-11-25 00:26:21 +11001543 /* Terminate the session. */
1544
1545 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001546 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001547
Darren Tuckerde0c0252009-07-06 07:17:35 +10001548 if (compat20) {
1549 packet_start(SSH2_MSG_DISCONNECT);
1550 packet_put_int(SSH2_DISCONNECT_BY_APPLICATION);
1551 packet_put_cstring("disconnected by user");
Damien Miller8ddc71c2010-03-22 05:54:02 +11001552 packet_put_cstring(""); /* language tag */
Darren Tuckerde0c0252009-07-06 07:17:35 +10001553 packet_send();
1554 packet_write_wait();
1555 }
Darren Tucker12b4a652009-06-21 18:14:48 +10001556
Ben Lindstrom601e4362001-06-21 03:19:23 +00001557 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +11001558
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001559 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001560 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001561
1562 /* restore blocking io */
1563 if (!isatty(fileno(stdin)))
1564 unset_nonblock(fileno(stdin));
1565 if (!isatty(fileno(stdout)))
1566 unset_nonblock(fileno(stdout));
1567 if (!isatty(fileno(stderr)))
1568 unset_nonblock(fileno(stderr));
1569
Damien Millerd6965512003-12-17 16:31:53 +11001570 /*
1571 * If there was no shell or command requested, there will be no remote
1572 * exit status to be returned. In that case, clear error code if the
1573 * connection was deliberately terminated at this end.
1574 */
1575 if (no_shell_flag && received_signal == SIGTERM) {
1576 received_signal = 0;
1577 exit_status = 0;
1578 }
1579
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001580 if (received_signal)
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001581 fatal("Killed by signal %d.", (int) received_signal);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001582
1583 /*
1584 * In interactive mode (with pseudo tty) display a message indicating
1585 * that the connection has been closed.
1586 */
1587 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001588 snprintf(buf, sizeof buf,
1589 "Connection to %.64s closed.\r\n", host);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001590 buffer_append(&stderr_buffer, buf, strlen(buf));
1591 }
1592
Damien Miller95def091999-11-25 00:26:21 +11001593 /* Output any buffered data for stdout. */
Damien Millercfd6e4f2011-01-16 23:18:33 +11001594 if (buffer_len(&stdout_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001595 len = atomicio(vwrite, fileno(stdout),
1596 buffer_ptr(&stdout_buffer), buffer_len(&stdout_buffer));
Damien Millercfd6e4f2011-01-16 23:18:33 +11001597 if (len < 0 || (u_int)len != buffer_len(&stdout_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001598 error("Write failed flushing stdout buffer.");
Damien Millercfd6e4f2011-01-16 23:18:33 +11001599 else
1600 buffer_consume(&stdout_buffer, len);
Damien Miller95def091999-11-25 00:26:21 +11001601 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001602
Damien Miller95def091999-11-25 00:26:21 +11001603 /* Output any buffered data for stderr. */
Damien Millercfd6e4f2011-01-16 23:18:33 +11001604 if (buffer_len(&stderr_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001605 len = atomicio(vwrite, fileno(stderr),
1606 buffer_ptr(&stderr_buffer), buffer_len(&stderr_buffer));
Damien Millercfd6e4f2011-01-16 23:18:33 +11001607 if (len < 0 || (u_int)len != buffer_len(&stderr_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001608 error("Write failed flushing stderr buffer.");
Damien Millercfd6e4f2011-01-16 23:18:33 +11001609 else
1610 buffer_consume(&stderr_buffer, len);
Damien Miller95def091999-11-25 00:26:21 +11001611 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001612
Damien Miller95def091999-11-25 00:26:21 +11001613 /* Clear and free any buffers. */
1614 memset(buf, 0, sizeof(buf));
1615 buffer_free(&stdin_buffer);
1616 buffer_free(&stdout_buffer);
1617 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001618
Damien Miller95def091999-11-25 00:26:21 +11001619 /* Report bytes transferred, and transfer rates. */
1620 total_time = get_current_time() - start_time;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001621 packet_get_state(MODE_IN, NULL, NULL, NULL, &ibytes);
1622 packet_get_state(MODE_OUT, NULL, NULL, NULL, &obytes);
1623 verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
Damien Miller821de0a2011-01-11 17:20:29 +11001624 (unsigned long long)obytes, (unsigned long long)ibytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001625 if (total_time > 0)
Damien Millerb61f3fc2008-07-11 17:36:48 +10001626 verbose("Bytes per second: sent %.1f, received %.1f",
1627 obytes / total_time, ibytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001628 /* Return the exit status of the program. */
1629 debug("Exit status %d", exit_status);
1630 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001631}
Damien Millerb38eff82000-04-01 11:09:21 +10001632
1633/*********/
1634
Ben Lindstrombba81212001-06-25 05:01:22 +00001635static void
Damien Miller630d6f42002-01-22 23:17:30 +11001636client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001637{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001638 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001639 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001640 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001641 buffer_append(&stdout_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001642 memset(data, 0, data_len);
1643 xfree(data);
1644}
Ben Lindstrombba81212001-06-25 05:01:22 +00001645static void
Damien Miller630d6f42002-01-22 23:17:30 +11001646client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001647{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001648 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001649 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001650 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001651 buffer_append(&stderr_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001652 memset(data, 0, data_len);
1653 xfree(data);
1654}
Ben Lindstrombba81212001-06-25 05:01:22 +00001655static void
Damien Miller630d6f42002-01-22 23:17:30 +11001656client_input_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001657{
Damien Millerb38eff82000-04-01 11:09:21 +10001658 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001659 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001660 /* Acknowledge the exit. */
1661 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1662 packet_send();
1663 /*
1664 * Must wait for packet to be sent since we are
1665 * exiting the loop.
1666 */
1667 packet_write_wait();
1668 /* Flag that we want to exit. */
1669 quit_pending = 1;
1670}
Darren Tucker5dcdd212003-10-02 16:17:00 +10001671static void
1672client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1673{
1674 Channel *c = NULL;
1675 int remote_id, sock;
1676
1677 /* Read the remote channel number from the message. */
1678 remote_id = packet_get_int();
1679 packet_check_eom();
1680
1681 /*
1682 * Get a connection to the local authentication agent (this may again
1683 * get forwarded).
1684 */
1685 sock = ssh_get_authentication_socket();
1686
1687 /*
1688 * If we could not connect the agent, send an error message back to
1689 * the server. This should never happen unless the agent dies,
1690 * because authentication forwarding is only enabled if we have an
1691 * agent.
1692 */
1693 if (sock >= 0) {
1694 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1695 -1, 0, 0, 0, "authentication agent connection", 1);
1696 c->remote_id = remote_id;
1697 c->force_drain = 1;
1698 }
1699 if (c == NULL) {
1700 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1701 packet_put_int(remote_id);
1702 } else {
1703 /* Send a confirmation to the remote host. */
1704 debug("Forwarding authentication connection.");
1705 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1706 packet_put_int(remote_id);
1707 packet_put_int(c->self);
1708 }
1709 packet_send();
1710}
Damien Millerb38eff82000-04-01 11:09:21 +10001711
Ben Lindstrombba81212001-06-25 05:01:22 +00001712static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001713client_request_forwarded_tcpip(const char *request_type, int rchan)
1714{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001715 Channel *c = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001716 char *listen_address, *originator_address;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001717 u_short listen_port, originator_port;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001718
1719 /* Get rest of the packet */
1720 listen_address = packet_get_string(NULL);
1721 listen_port = packet_get_int();
1722 originator_address = packet_get_string(NULL);
1723 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001724 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001725
Damien Millerbd740252008-05-19 15:37:09 +10001726 debug("client_request_forwarded_tcpip: listen %s port %d, "
1727 "originator %s port %d", listen_address, listen_port,
1728 originator_address, originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001729
Damien Millerbd740252008-05-19 15:37:09 +10001730 c = channel_connect_by_listen_address(listen_port,
1731 "forwarded-tcpip", originator_address);
1732
Damien Miller0bc1bd82000-11-13 22:57:25 +11001733 xfree(originator_address);
1734 xfree(listen_address);
1735 return c;
1736}
1737
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001738static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001739client_request_x11(const char *request_type, int rchan)
1740{
1741 Channel *c = NULL;
1742 char *originator;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001743 u_short originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001744 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001745
1746 if (!options.forward_x11) {
1747 error("Warning: ssh server tried X11 forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001748 error("Warning: this is probably a break-in attempt by a "
1749 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001750 return NULL;
1751 }
Damien Miller1ab6a512010-06-26 10:02:24 +10001752 if (x11_refuse_time != 0 && time(NULL) >= x11_refuse_time) {
1753 verbose("Rejected X11 connection after ForwardX11Timeout "
1754 "expired");
1755 return NULL;
1756 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001757 originator = packet_get_string(NULL);
1758 if (datafellows & SSH_BUG_X11FWD) {
1759 debug2("buggy server: x11 request w/o originator_port");
1760 originator_port = 0;
1761 } else {
1762 originator_port = packet_get_int();
1763 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001764 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001765 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001766 debug("client_request_x11: request from %s %d", originator,
1767 originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001768 xfree(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001769 sock = x11_connect_display();
1770 if (sock < 0)
1771 return NULL;
1772 c = channel_new("x11",
1773 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001774 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001775 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001776 return c;
1777}
1778
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001779static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001780client_request_agent(const char *request_type, int rchan)
1781{
1782 Channel *c = NULL;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001783 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001784
1785 if (!options.forward_agent) {
1786 error("Warning: ssh server tried agent forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001787 error("Warning: this is probably a break-in attempt by a "
1788 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001789 return NULL;
1790 }
Darren Tucker82a3d2b2007-02-19 22:10:25 +11001791 sock = ssh_get_authentication_socket();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001792 if (sock < 0)
1793 return NULL;
1794 c = channel_new("authentication agent connection",
1795 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001796 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001797 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001798 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001799 return c;
1800}
1801
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001802int
1803client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun)
1804{
1805 Channel *c;
1806 int fd;
1807
1808 if (tun_mode == SSH_TUNMODE_NO)
1809 return 0;
1810
1811 if (!compat20) {
Damien Millerb3f2c9f2009-01-28 16:15:30 +11001812 error("Tunnel forwarding is not supported for protocol 1");
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001813 return -1;
1814 }
1815
1816 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1817
1818 /* Open local tunnel device */
1819 if ((fd = tun_open(local_tun, tun_mode)) == -1) {
1820 error("Tunnel device open failed.");
1821 return -1;
1822 }
1823
1824 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1825 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1826 c->datagram = 1;
1827
1828#if defined(SSH_TUN_FILTER)
1829 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1830 channel_register_filter(c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +10001831 sys_tun_outfilter, NULL, NULL);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001832#endif
1833
1834 packet_start(SSH2_MSG_CHANNEL_OPEN);
1835 packet_put_cstring("tun@openssh.com");
1836 packet_put_int(c->self);
1837 packet_put_int(c->local_window_max);
1838 packet_put_int(c->local_maxpacket);
1839 packet_put_int(tun_mode);
1840 packet_put_int(remote_tun);
1841 packet_send();
1842
1843 return 0;
1844}
1845
Damien Millerbd483e72000-04-30 10:00:53 +10001846/* XXXX move to generic input handler */
Ben Lindstrombba81212001-06-25 05:01:22 +00001847static void
Damien Miller630d6f42002-01-22 23:17:30 +11001848client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001849{
1850 Channel *c = NULL;
1851 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001852 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001853 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001854
1855 ctype = packet_get_string(&len);
1856 rchan = packet_get_int();
1857 rwindow = packet_get_int();
1858 rmaxpack = packet_get_int();
1859
Damien Millere247cc42000-05-07 12:03:14 +10001860 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001861 ctype, rchan, rwindow, rmaxpack);
1862
Damien Miller0bc1bd82000-11-13 22:57:25 +11001863 if (strcmp(ctype, "forwarded-tcpip") == 0) {
1864 c = client_request_forwarded_tcpip(ctype, rchan);
1865 } else if (strcmp(ctype, "x11") == 0) {
1866 c = client_request_x11(ctype, rchan);
1867 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1868 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001869 }
1870/* XXX duplicate : */
1871 if (c != NULL) {
1872 debug("confirm %s", ctype);
1873 c->remote_id = rchan;
1874 c->remote_window = rwindow;
1875 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001876 if (c->type != SSH_CHANNEL_CONNECTING) {
1877 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1878 packet_put_int(c->remote_id);
1879 packet_put_int(c->self);
1880 packet_put_int(c->local_window);
1881 packet_put_int(c->local_maxpacket);
1882 packet_send();
1883 }
Damien Millerbd483e72000-04-30 10:00:53 +10001884 } else {
1885 debug("failure %s", ctype);
1886 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1887 packet_put_int(rchan);
1888 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00001889 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001890 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00001891 packet_put_cstring("");
1892 }
Damien Millerbd483e72000-04-30 10:00:53 +10001893 packet_send();
1894 }
1895 xfree(ctype);
1896}
Ben Lindstrombba81212001-06-25 05:01:22 +00001897static void
Damien Miller630d6f42002-01-22 23:17:30 +11001898client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001899{
1900 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001901 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001902 char *rtype;
1903
1904 id = packet_get_int();
1905 rtype = packet_get_string(NULL);
1906 reply = packet_get_char();
1907
1908 debug("client_input_channel_req: channel %d rtype %s reply %d",
1909 id, rtype, reply);
1910
Damien Miller3bbd8782004-06-18 22:23:22 +10001911 if (id == -1) {
1912 error("client_input_channel_req: request for channel -1");
1913 } else if ((c = channel_lookup(id)) == NULL) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001914 error("client_input_channel_req: channel %d: "
1915 "unknown channel", id);
Damien Millerbab9bd42008-05-19 16:06:47 +10001916 } else if (strcmp(rtype, "eow@openssh.com") == 0) {
1917 packet_check_eom();
1918 chan_rcvd_eow(c);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001919 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10001920 exitval = packet_get_int();
Damien Millere1537f92010-01-26 13:26:22 +11001921 if (c->ctl_chan != -1) {
1922 mux_exit_message(c, exitval);
Damien Miller0e220db2004-06-15 10:34:08 +10001923 success = 1;
Darren Tucker29440822010-01-08 17:08:35 +11001924 } else if (id == session_ident) {
1925 /* Record exit value of local session */
1926 success = 1;
1927 exit_status = exitval;
1928 } else {
Damien Miller36f57eb2010-01-30 17:28:34 +11001929 /* Probably for a mux channel that has already closed */
1930 debug("%s: no sink for exit-status on channel %d",
1931 __func__, id);
Damien Miller0e220db2004-06-15 10:34:08 +10001932 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001933 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001934 }
Damien Millerd0fdd682010-12-01 12:02:14 +11001935 if (reply && c != NULL) {
Ben Lindstrom5b828322001-02-09 01:34:36 +00001936 packet_start(success ?
1937 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller8533c782008-12-08 09:54:40 +11001938 packet_put_int(c->remote_id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001939 packet_send();
1940 }
1941 xfree(rtype);
1942}
Damien Millerc3fa4072002-01-22 23:21:58 +11001943static void
1944client_input_global_request(int type, u_int32_t seq, void *ctxt)
1945{
1946 char *rtype;
1947 int want_reply;
1948 int success = 0;
1949
1950 rtype = packet_get_string(NULL);
1951 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11001952 debug("client_input_global_request: rtype %s want_reply %d",
1953 rtype, want_reply);
Damien Millerc3fa4072002-01-22 23:21:58 +11001954 if (want_reply) {
1955 packet_start(success ?
1956 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
1957 packet_send();
1958 packet_write_wait();
1959 }
1960 xfree(rtype);
1961}
Damien Millerbd483e72000-04-30 10:00:53 +10001962
Damien Miller0e220db2004-06-15 10:34:08 +10001963void
Darren Tuckerfc959702004-07-17 16:12:08 +10001964client_session2_setup(int id, int want_tty, int want_subsystem,
Damien Miller5771ed72008-05-19 15:35:33 +10001965 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env)
Damien Miller0e220db2004-06-15 10:34:08 +10001966{
1967 int len;
Darren Tucker5d78de62004-11-05 20:35:44 +11001968 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001969
1970 debug2("%s: id %d", __func__, id);
1971
Darren Tucker5d78de62004-11-05 20:35:44 +11001972 if ((c = channel_lookup(id)) == NULL)
1973 fatal("client_session2_setup: channel %d: unknown channel", id);
1974
Damien Miller0dac6fb2010-11-20 15:19:38 +11001975 packet_set_interactive(want_tty,
1976 options.ip_qos_interactive, options.ip_qos_bulk);
1977
Damien Miller0e220db2004-06-15 10:34:08 +10001978 if (want_tty) {
1979 struct winsize ws;
Damien Miller0e220db2004-06-15 10:34:08 +10001980
1981 /* Store window size in the packet. */
1982 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
1983 memset(&ws, 0, sizeof(ws));
1984
Damien Miller5771ed72008-05-19 15:35:33 +10001985 channel_request_start(id, "pty-req", 1);
Damien Miller22c97f12010-04-16 15:53:23 +10001986 client_expect_confirm(id, "PTY allocation", 1);
Damien Miller0e220db2004-06-15 10:34:08 +10001987 packet_put_cstring(term != NULL ? term : "");
Damien Miller71a73672006-03-26 14:04:36 +11001988 packet_put_int((u_int)ws.ws_col);
1989 packet_put_int((u_int)ws.ws_row);
1990 packet_put_int((u_int)ws.ws_xpixel);
1991 packet_put_int((u_int)ws.ws_ypixel);
Darren Tuckerdf189fb2008-06-08 12:55:32 +10001992 if (tiop == NULL)
1993 tiop = get_saved_tio();
1994 tty_make_modes(-1, tiop);
Damien Miller0e220db2004-06-15 10:34:08 +10001995 packet_send();
1996 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11001997 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10001998 }
1999
2000 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10002001 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10002002 int i, j, matched;
Damien Miller0e220db2004-06-15 10:34:08 +10002003 char *name, *val;
2004
2005 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10002006 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10002007 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10002008 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10002009 if ((val = strchr(name, '=')) == NULL) {
Damien Miller0a0176e2005-11-05 15:07:59 +11002010 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002011 continue;
2012 }
2013 *val++ = '\0';
2014
2015 matched = 0;
2016 for (j = 0; j < options.num_send_env; j++) {
2017 if (match_pattern(name, options.send_env[j])) {
2018 matched = 1;
2019 break;
2020 }
2021 }
2022 if (!matched) {
2023 debug3("Ignored env %s", name);
Damien Miller0a0176e2005-11-05 15:07:59 +11002024 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002025 continue;
2026 }
2027
2028 debug("Sending env %s = %s", name, val);
2029 channel_request_start(id, "env", 0);
2030 packet_put_cstring(name);
2031 packet_put_cstring(val);
2032 packet_send();
Damien Miller0a0176e2005-11-05 15:07:59 +11002033 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002034 }
2035 }
2036
2037 len = buffer_len(cmd);
2038 if (len > 0) {
2039 if (len > 900)
2040 len = 900;
2041 if (want_subsystem) {
Damien Miller5771ed72008-05-19 15:35:33 +10002042 debug("Sending subsystem: %.*s",
2043 len, (u_char*)buffer_ptr(cmd));
2044 channel_request_start(id, "subsystem", 1);
2045 client_expect_confirm(id, "subsystem", 1);
Damien Miller0e220db2004-06-15 10:34:08 +10002046 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002047 debug("Sending command: %.*s",
2048 len, (u_char*)buffer_ptr(cmd));
2049 channel_request_start(id, "exec", 1);
2050 client_expect_confirm(id, "exec", 1);
Damien Miller0e220db2004-06-15 10:34:08 +10002051 }
2052 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
2053 packet_send();
2054 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002055 channel_request_start(id, "shell", 1);
2056 client_expect_confirm(id, "shell", 1);
Damien Miller0e220db2004-06-15 10:34:08 +10002057 packet_send();
2058 }
2059}
2060
Ben Lindstrombba81212001-06-25 05:01:22 +00002061static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002062client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10002063{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002064 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10002065
Damien Miller1383bd82000-04-06 12:32:37 +10002066 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2067 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2068 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2069 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10002070 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10002071 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2072 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002073 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10002074 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerb84886b2008-05-19 15:05:07 +10002075 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2076 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
Damien Millerc3fa4072002-01-22 23:21:58 +11002077 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002078
2079 /* rekeying */
2080 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10002081
2082 /* global request reply messages */
2083 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2084 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10002085}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002086
Ben Lindstrombba81212001-06-25 05:01:22 +00002087static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002088client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002089{
2090 dispatch_init(NULL);
2091 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
2092 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
2093 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10002094 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2095 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
2096 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002097 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
2098 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
2099 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11002100
2101 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
Darren Tucker5dcdd212003-10-02 16:17:00 +10002102 &client_input_agent_open : &deny_input_open);
Damien Miller69b69aa2000-10-28 14:19:58 +11002103 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
2104 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002105}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002106
Ben Lindstrombba81212001-06-25 05:01:22 +00002107static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002108client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002109{
2110 client_init_dispatch_13();
2111 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
2112 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
2113}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002114
Ben Lindstromdb47f382001-07-04 05:10:27 +00002115static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002116client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002117{
Damien Miller1383bd82000-04-06 12:32:37 +10002118 if (compat20)
2119 client_init_dispatch_20();
2120 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10002121 client_init_dispatch_13();
2122 else
2123 client_init_dispatch_15();
2124}
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002125
Damien Miller6c3eec72011-05-05 14:16:22 +10002126void
2127client_stop_mux(void)
2128{
2129 if (options.control_path != NULL && muxserver_sock != -1)
2130 unlink(options.control_path);
2131 /*
2132 * If we are in persist mode, signal that we should close when all
2133 * active channels are closed.
2134 */
2135 if (options.control_persist)
2136 session_closed = 1;
2137}
2138
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002139/* client specific fatal cleanup */
2140void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002141cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002142{
Damien Miller21771e22011-05-15 08:45:50 +10002143 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002144 leave_non_blocking();
Damien Millerb1cbfa22008-05-19 16:00:08 +10002145 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10002146 unlink(options.control_path);
Damien Millera41ccca2010-10-07 22:07:32 +11002147 ssh_kill_proxy_command();
Darren Tucker3e33cec2003-10-02 16:12:36 +10002148 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002149}